1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
18632: tests/thread_float: do not overload slow MCUs with IRQs r=kaspar030 a=maribu

### Contribution description

If the regular context switches are triggered too fast, slow MCUs will be able to spent little time on actually progressing in the test. This will scale the IRQ rate with the CPU clock as a crude way too keep load within limits.

### Testing procedure

The unit test should now pass on the Microduino CoreRF

```
$ make BOARD=microduino-corerf AVRDUDE_PROGRAMMER=dragon_jtag -C tests/thread_float flash test
make: Entering directory '/home/maribu/Repos/software/RIOT/tests/thread_float'
Building application "tests_thread_float" for "microduino-corerf" with MCU "atmega128rfa1".
[...]
   text	  data	   bss	   dec	   hex	filename
  12834	   520	  3003	 16357	  3fe5	/home/maribu/Repos/software/RIOT/tests/thread_float/bin/microduino-corerf/tests_thread_float.elf
avrdude -c dragon_jtag -p m128rfa1  -U flash:w:/home/maribu/Repos/software/RIOT/tests/thread_float/bin/microduino-corerf/tests_thread_float.hex
[...]
Welcome to pyterm!
Type '/exit' to exit.
READY
s
START
main(): This is RIOT! (Version: 2022.10-devel-858-g18566-tests/thread_float)
THREADS CREATED

Context switch every 3125 µs
{ "threads": [{ "name": "idle", "stack_size": 192, "stack_used": 88 }]}
{ "threads": [{ "name": "main", "stack_size": 640, "stack_used": 220 }]}
THREAD t1 start
THREAD t2 start
THREAD t3 start
t1: 141.443770
t3: 141.466810
t1: 141.443770
t3: 141.466810
t1: 141.443770
t3: 141.466810
t1: 141.443770
t3: 141.466810
t1: 141.443770
t3: 141.466810
t1: 141.443770
t3: 141.466810
t1: 141.443770

make: Leaving directory '/home/maribu/Repos/software/RIOT/tests/thread_float'
```

(~~Note: The idle thread exiting is something that should never occur. I guess the culprit may be `cpu_switch_context_exit()` messing things up when the main thread exits. But that is not directly related to what this PR aims to fix. Adding a `thread_sleep()` at the end of `main()` does indeed prevent the idle thread from exiting.~~
Update: That's expected. The idle thread stats are printed on exit of the main thread, the idle thread does not actually exit.)

### Issues/PRs references

Fixes https://github.com/RIOT-OS/RIOT/issues/16908 maybe?

Co-authored-by: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
This commit is contained in:
bors[bot] 2023-01-04 16:25:23 +00:00 committed by GitHub
commit 9a45f4bc49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,10 +23,14 @@
#include <stdio.h>
#include <string.h>
#include "thread.h"
#include "board.h"
#include "clk.h"
#include "macros/units.h"
#include "msg.h"
#include "periph_conf.h"
#include "thread.h"
#include "time_units.h"
#include "ztimer.h"
#include "timex.h"
static char t1_stack[THREAD_STACKSIZE_MAIN];
static char t2_stack[THREAD_STACKSIZE_MAIN];
@ -36,15 +40,13 @@ static kernel_pid_t p1, p2, p3;
static ztimer_t timer;
#define OFFSET (100)
static mutex_t lock = MUTEX_INIT;
static void timer_cb(void *arg)
{
(void)arg;
uint32_t *timeout = arg;
thread_yield();
ztimer_set(ZTIMER_USEC, &timer, OFFSET);
ztimer_set(ZTIMER_USEC, &timer, *timeout);
}
static void *thread_1_2_3(void *_arg)
@ -81,6 +83,16 @@ int main(void)
const char *t2_name = "t2";
const char *t3_name = "t3";
/* Let's not overwhelm boards by firing IRQs faster than they can handle and
* give them 50 billion CPU cycles per timeout.
*
* (Note: The `static` is required as this variable will be accessed from
* the ISR, which will occur even after the main thread has exited.) */
static uint32_t timeout = 0;
/* Note: It must be initialized dynamically, as coreclk() is not
* constant. */
timeout = 50000000000U / coreclk();
p1 = thread_create(t1_stack, sizeof(t1_stack), THREAD_PRIORITY_MAIN + 1,
THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST,
thread_1_2_3, (void *)t1_name, t1_name);
@ -92,8 +104,11 @@ int main(void)
thread_1_2_3, (void *)t3_name, t3_name);
puts("THREADS CREATED\n");
printf("Context switch every %" PRIu32 " µs\n", timeout);
timer.callback = timer_cb;
ztimer_set(ZTIMER_USEC, &timer, OFFSET);
timer.arg = &timeout;
ztimer_set(ZTIMER_USEC, &timer, timeout);
return 0;
}