Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
18632: tests/thread_float: do not overload slow MCUs with IRQs r=benpicco 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 #16908 maybe?

18950: tests/unittests: add unit tests for core_mbox r=benpicco a=maribu

### Contribution description

As the title says

### Testing procedure

The test cases are run on `native` by Murdock anyway.

### Issues/PRs references

Split out of #18949

19030: tests/periph_timer_short_relative_set: improve test r=benpicco a=maribu

### Contribution description

Reduce the number lines to output by only testing for intervals 0..15 to speed up the test.

In addition, run each test case 128 repetitions (it is still faster than before) to give some confidence the short relative set actually succeeded.

### Testing procedure

The test application should consistently fail or succeed, rather than occasionally passing.

### Issues/PRs references

None

19085: makefiles/tests/tests.inc.mk: fix test/available target r=benpicco a=maribu

### Contribution description

`dist/tools/compile_and_test_for_board/compile_and_test_for_board.py` relies on `make test/available` to check if a test if available. However, this so far did not take `TEST_ON_CI_BLACKLIST` and `TEST_ON_CI_WHITELIST` into account, resulting in tests being executed for boards which they are not available. This should fix the issue.

### Testing procedure


#### Expected to fail

```
$ make BOARD=nrf52840dk -C tests/gcoap_fileserver test/available
$ make BOARD=microbit -C tests/log_color test/available
```

(On `master`, they succeed, but fail in this PR.)

#### Expected to succeed

```
$ make BOARD=native -C tests/gcoap_fileserver test/available
$ make BOARD=nrf52840dk -C tests/pkg_edhoc_c test/available
$ make BOARD=nrf52840dk -C tests/log_color test/available
```

(Succeed in both `master` and this PR.)

### Issues/PRs references

None

Co-authored-by: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
  • Loading branch information
bors[bot] and maribu authored Jan 3, 2023
5 parents 0fed4c9 + 837c857 + 9b3df5b + 05bb4bb + 6352e4c commit d846f9e
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 23 deletions.
10 changes: 10 additions & 0 deletions makefiles/tests/tests.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ test: $(TEST_DEPS)
done

test/available:
ifneq (,$(TEST_ON_CI_WHITELIST))
ifeq (,$(filter $(BOARD),$(TEST_ON_CI_WHITELIST)))
@echo "Board $(BOARD) not in TEST_ON_CI_WHITELIST"
$(Q)false
endif
endif
ifneq (,$(filter $(BOARD) all,$(TEST_ON_CI_BLACKLIST)))
@echo "Board $(BOARD) is in TEST_ON_CI_BLACKLIST"
$(Q)false
endif
$(Q)test -n "$(strip $(TESTS))"

# Tests that require root privileges
Expand Down
37 changes: 20 additions & 17 deletions tests/periph_timer_short_relative_set/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,36 @@ int main(void)
puts("\nTest for peripheral TIMER short timer_set()\n");

printf("This test tries timer_set() with decreasing intervals down to 0.\n"
"You should see lines like 'interval <n> ok', followed by a success"
"You should see lines like 'interval <n>: OK', followed by a success"
" message.\n"
"On failure, this test prints an error message.\n\n");

printf("testing periph_timer %u, freq %lu\n", TEST_TIMER_DEV, TEST_TIMER_FREQ);
printf("testing periph_timer %u, freq %lu, bits = %u\n",
TEST_TIMER_DEV, TEST_TIMER_FREQ, TEST_TIMER_WIDTH);
timer_init(TEST_TIMER_DEV, TEST_TIMER_FREQ, cb, thread_get_active());

uint32_t interval = 100;
uint32_t interval = 16;
const unsigned max_repetitions = 128;
while (interval--) {
uint32_t before = timer_read(TEST_TIMER_DEV);
timer_set(TEST_TIMER_DEV, 0, interval);
while(!thread_flags_clear(1)) {
uint32_t diff = (timer_read(TEST_TIMER_DEV) - before)
& TEST_TIMER_MAX;
if (diff > TEST_MAX_DIFF) {
printf("ERROR: too long delay, aborted after %" PRIu32
" (TEST_MAX_DIFF=%lu)\n"
"TEST FAILED\n"
"Note: This is currently expected to fail on most boards.\n",
diff, TEST_MAX_DIFF);
while(1) {}
for (unsigned rep = 0; rep < max_repetitions; rep++) {
uint32_t before = timer_read(TEST_TIMER_DEV);
timer_set(TEST_TIMER_DEV, 0, interval);
while (!thread_flags_clear(1)) {
uint32_t diff = (timer_read(TEST_TIMER_DEV) - before)
& TEST_TIMER_MAX;
if (diff > TEST_MAX_DIFF) {
printf("ERROR: too long delay, aborted after %" PRIu32
" (TEST_MAX_DIFF=%lu) on repetition %u\n"
"TEST FAILED\n",
diff, TEST_MAX_DIFF, rep);
return EXIT_FAILURE;
}
}
}
printf("interval %" PRIu32 " ok\n", interval);
printf("interval %" PRIu32 ": OK\n", interval);
}

puts("\nTEST SUCCEEDED");

return 0;
return EXIT_SUCCESS;
}
29 changes: 23 additions & 6 deletions tests/thread_float/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
#include <stdio.h>
#include <string.h>

#include "thread.h"
#include "board.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];
Expand All @@ -36,15 +39,19 @@ static kernel_pid_t p1, p2, p3;

static ztimer_t timer;

#define OFFSET (100)
#ifndef CLOCK_CORECLOCK
/* native is missing access to the CPU clock frequency, let's provide a
* conservative fallback */
#define CLOCK_CORECLOCK MHZ(100)
#endif

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)
Expand Down Expand Up @@ -81,6 +88,13 @@ 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 = 50000000000U / CLOCK_CORECLOCK;

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);
Expand All @@ -92,8 +106,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;
}
1 change: 1 addition & 0 deletions tests/unittests/tests-core/Makefile.include
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
USEMODULE += core_mbox
71 changes: 71 additions & 0 deletions tests/unittests/tests-core/tests-core-mbox.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (C) 2022 Otto-von-Guericke-Universität Magdeburg
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

#include <limits.h>

#include "embUnit.h"

#include "mbox.h"

#include "tests-core.h"

#define QUEUE_SIZE 8

static unsigned gen_val(unsigned i) {
return i + 1337;
}

static void test_mbox_put_get(void)
{
mbox_t mbox;
msg_t queue[QUEUE_SIZE];
msg_t msg = { .type = 0 };
mbox_init(&mbox, queue, ARRAY_SIZE(queue));
TEST_ASSERT_EQUAL_INT(0, mbox_avail(&mbox));

/* Filling the queue up to capacity one item at a time. This should
* succeed every single time. */
for (unsigned i = 0; i < ARRAY_SIZE(queue); i++) {
msg.type = i;
msg.content.value = gen_val(i);
TEST_ASSERT_EQUAL_INT(1, mbox_try_put(&mbox, &msg));
}

/* Adding one item over capacity must fail. */
msg.type = 4242;
msg.content.value = 4242;
TEST_ASSERT_EQUAL_INT(0, mbox_try_put(&mbox, &msg));

/* The queue must contain the items we filled in and in that order. */
for (unsigned i = 0; i < ARRAY_SIZE(queue); i++) {
TEST_ASSERT_EQUAL_INT(i, queue[i].type);
TEST_ASSERT_EQUAL_INT(gen_val(i), queue[i].content.value);
}

/* Now we drain the queue one item at a time. We expect to get the exact
* items we filled in and in that order. */
for (unsigned i = 0; i < ARRAY_SIZE(queue); i++) {
TEST_ASSERT_EQUAL_INT(1, mbox_try_get(&mbox, &msg));
TEST_ASSERT_EQUAL_INT(i, msg.type);
TEST_ASSERT_EQUAL_INT(gen_val(i), msg.content.value);
}

/* The queue is now empty. Getting one more item (non-blocking) must fail */
TEST_ASSERT_EQUAL_INT(0, mbox_try_get(&mbox, &msg));
}

Test *tests_core_mbox_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_mbox_put_get),
};

EMB_UNIT_TESTCALLER(core_mbox_tests, NULL, NULL, fixtures);

return (Test *)&core_mbox_tests;
}
1 change: 1 addition & 0 deletions tests/unittests/tests-core/tests-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void tests_core(void)
TESTS_RUN(tests_core_cib_tests());
TESTS_RUN(tests_core_clist_tests());
TESTS_RUN(tests_core_list_tests());
TESTS_RUN(tests_core_mbox_tests());
TESTS_RUN(tests_core_priority_queue_tests());
TESTS_RUN(tests_core_byteorder_tests());
TESTS_RUN(tests_core_ringbuffer_tests());
Expand Down
7 changes: 7 additions & 0 deletions tests/unittests/tests-core/tests-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ Test *tests_core_clist_tests(void);
*/
Test *tests_core_list_tests(void);

/**
* @brief Generates tests for mbox.h
*
* @return embUnit tests if successful, NULL if not.
*/
Test *tests_core_mbox_tests(void);

/**
* @brief Generates tests for priority_queue.h
*
Expand Down

0 comments on commit d846f9e

Please sign in to comment.