Skip to content

Commit 178bf48

Browse files
committed
tests: kernel: Do not use exact time in timing checks.
This commit replaces exact time compassion by a range check, allowing the tests to pass on platforms which needs rounding in __ticks_to_ms() and _ms_to_ticks(). Signed-off-by: Piotr Zięcik <[email protected]>
1 parent 262daf1 commit 178bf48

File tree

4 files changed

+50
-27
lines changed

4 files changed

+50
-27
lines changed

tests/kernel/sched/schedule_api/src/test_sched_timeslice_reset.c

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,31 @@ static K_THREAD_STACK_ARRAY_DEFINE(tstack, NUM_THREAD, STACK_SIZE);
1919
K_SEM_DEFINE(sema, 0, NUM_THREAD);
2020
/*elapsed_slice taken by last thread*/
2121
static s64_t elapsed_slice;
22-
/*expected elapsed duration*/
23-
static s64_t expected_slice[NUM_THREAD] = {
24-
HALF_SLICE_SIZE, /* the ztest native thread taking a half timeslice*/
25-
SLICE_SIZE, /* the spawned thread taking a full timeslice, reset*/
26-
SLICE_SIZE /* the spawned thread taking a full timeslice, reset*/
27-
};
2822
static int thread_idx;
2923

3024
static void thread_tslice(void *p1, void *p2, void *p3)
3125
{
3226
s64_t t = k_uptime_delta(&elapsed_slice);
27+
s64_t expected_slice_min, expected_slice_max;
28+
29+
if (thread_idx == 0) {
30+
/*thread number 0 releases CPU after HALF_SLICE_SIZE*/
31+
expected_slice_min = HALF_SLICE_SIZE;
32+
expected_slice_max = HALF_SLICE_SIZE;
33+
} else {
34+
/*other threads are sliced with tick granulity*/
35+
expected_slice_min = __ticks_to_ms(_ms_to_ticks(SLICE_SIZE));
36+
expected_slice_max = __ticks_to_ms(_ms_to_ticks(SLICE_SIZE)+1);
37+
}
3338

3439
#ifdef CONFIG_DEBUG
35-
TC_PRINT("thread[%d] elapsed slice %lld, ", thread_idx, t);
36-
TC_PRINT("expected %lld\n", expected_slice[thread_idx]);
40+
TC_PRINT("thread[%d] elapsed slice: %lld, expected: <%lld, %lld>\n",
41+
thread_idx, t, expected_slice_min, expected_slice_max);
3742
#endif
43+
3844
/** TESTPOINT: timeslice should be reset for each preemptive thread*/
39-
zassert_true(t <= expected_slice[thread_idx], NULL);
45+
zassert_true(t >= expected_slice_min, NULL);
46+
zassert_true(t <= expected_slice_max, NULL);
4047
thread_idx = (thread_idx + 1) % NUM_THREAD;
4148

4249
u32_t t32 = k_uptime_get_32();

tests/kernel/sched/schedule_api/src/test_slice_scheduling.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,23 @@ static void thread_tslice(void *p1, void *p2, void *p3)
3737
int thread_parameter = ((int)p1 == (NUM_THREAD - 1)) ? '\n' :
3838
((int)p1 + 'A');
3939

40+
s64_t expected_slice_min = __ticks_to_ms(_ms_to_ticks(SLICE_SIZE));
41+
s64_t expected_slice_max = __ticks_to_ms(_ms_to_ticks(SLICE_SIZE) + 1);
42+
4043
while (1) {
4144
s64_t tdelta = k_uptime_delta(&elapsed_slice);
42-
4345
TC_PRINT("%c", thread_parameter);
4446
/* Test Fails if thread exceed allocated time slice or
4547
* Any thread is scheduled out of order.
4648
*/
47-
zassert_true(((tdelta <= SLICE_SIZE) &&
49+
zassert_true(((tdelta >= expected_slice_min) &&
50+
(tdelta <= expected_slice_max) &&
4851
((int)p1 == thread_idx)), NULL);
4952
thread_idx = (thread_idx + 1) % (NUM_THREAD);
5053
u32_t t32 = k_uptime_get_32();
5154

5255
/* Keep the current thread busy for more than one slice,
53-
* even though, when timeslice used up the next thread
56+
* even though, when timeslice used up the next thread
5457
* should be scheduled in.
5558
*/
5659
while (k_uptime_get_32() - t32 < BUSY_MS) {
@@ -102,9 +105,12 @@ void test_slice_scheduling(void)
102105
while (count < ITRERATION_COUNT) {
103106
k_uptime_delta(&elapsed_slice);
104107

105-
/* current thread (ztest native) consumed a half timeslice*/
108+
/* Keep the current thread busy for more than one slice,
109+
* even though, when timeslice used up the next thread
110+
* should be scheduled in.
111+
*/
106112
t32 = k_uptime_get_32();
107-
while (k_uptime_get_32() - t32 < SLICE_SIZE) {
113+
while (k_uptime_get_32() - t32 < BUSY_MS) {
108114
#if defined(CONFIG_ARCH_POSIX)
109115
posix_halt_cpu(); /*sleep until next irq*/
110116
#else

tests/kernel/tickless/tickless_concept/src/main.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@ static struct k_thread tdata[NUM_THREAD];
1515
#ifndef CONFIG_TICKLESS_IDLE
1616
#define CONFIG_TICKLESS_IDLE_THRESH 20
1717
#endif
18-
/*millisecond per tick*/
19-
#define MSEC_PER_TICK (__ticks_to_ms(1))
2018
/*sleep duration tickless*/
21-
#define SLEEP_TICKLESS (CONFIG_TICKLESS_IDLE_THRESH * MSEC_PER_TICK)
19+
#define SLEEP_TICKLESS __ticks_to_ms(CONFIG_TICKLESS_IDLE_THRESH)
20+
2221
/*sleep duration with tick*/
23-
#define SLEEP_TICKFUL ((CONFIG_TICKLESS_IDLE_THRESH - 1) * MSEC_PER_TICK)
22+
#define SLEEP_TICKFUL __ticks_to_ms(CONFIG_TICKLESS_IDLE_THRESH - 1)
23+
2424
/*slice size is set as half of the sleep duration*/
25-
#define SLICE_SIZE ((CONFIG_TICKLESS_IDLE_THRESH >> 1) * MSEC_PER_TICK)
25+
#define SLICE_SIZE __ticks_to_ms(CONFIG_TICKLESS_IDLE_THRESH >> 1)
26+
27+
/*maximum slice duration accepted by the test*/
28+
#define SLICE_SIZE_LIMIT __ticks_to_ms((CONFIG_TICKLESS_IDLE_THRESH >> 1) + 1)
29+
2630
/*align to millisecond boundary*/
2731
#if defined(CONFIG_ARCH_POSIX)
2832
#define ALIGN_MS_BOUNDARY() \
@@ -46,11 +50,13 @@ static void thread_tslice(void *p1, void *p2, void *p3)
4650
{
4751
s64_t t = k_uptime_delta(&elapsed_slice);
4852

49-
TC_PRINT("elapsed slice %lld\n", t);
53+
TC_PRINT("elapsed slice %lld, expected: <%lld, %lld>\n",
54+
t, SLICE_SIZE, SLICE_SIZE_LIMIT);
55+
5056
/**TESTPOINT: verify slicing scheduler behaves as expected*/
5157
zassert_true(t >= SLICE_SIZE, NULL);
5258
/*less than one tick delay*/
53-
zassert_true(t <= (SLICE_SIZE + MSEC_PER_TICK), NULL);
59+
zassert_true(t <= SLICE_SIZE_LIMIT, NULL);
5460

5561
u32_t t32 = k_uptime_get_32();
5662

tests/kernel/workq/work_queue/src/main.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
/* Each work item takes 100ms */
1616
#define WORK_ITEM_WAIT 100
1717

18+
/* In fact, each work item could take up to this value */
19+
#define WORK_ITEM_WAIT_ALIGNED \
20+
__ticks_to_ms(_ms_to_ticks(WORK_ITEM_WAIT) + _TICK_ALIGN)
21+
1822
/*
1923
* Wait 50ms between work submissions, to ensure co-op and prempt
2024
* preempt thread submit alternatively.
@@ -139,7 +143,7 @@ static void test_sequence(void)
139143
test_items_submit();
140144

141145
TC_PRINT(" - Waiting for work to finish\n");
142-
k_sleep((NUM_TEST_ITEMS + 1) * WORK_ITEM_WAIT);
146+
k_sleep(NUM_TEST_ITEMS * WORK_ITEM_WAIT_ALIGNED);
143147

144148
check_results(NUM_TEST_ITEMS);
145149
reset_results();
@@ -179,7 +183,7 @@ static void test_resubmit(void)
179183
k_work_submit(&tests[0].work.work);
180184

181185
TC_PRINT(" - Waiting for work to finish\n");
182-
k_sleep((NUM_TEST_ITEMS + 1) * WORK_ITEM_WAIT);
186+
k_sleep(NUM_TEST_ITEMS * WORK_ITEM_WAIT_ALIGNED);
183187

184188
TC_PRINT(" - Checking results\n");
185189
check_results(NUM_TEST_ITEMS);
@@ -294,7 +298,7 @@ static void test_delayed_cancel(void)
294298
NULL, NULL, NULL, K_HIGHEST_THREAD_PRIO, 0, 0);
295299

296300
TC_PRINT(" - Waiting for work to finish\n");
297-
k_sleep(2 * WORK_ITEM_WAIT);
301+
k_sleep(WORK_ITEM_WAIT_ALIGNED);
298302

299303
TC_PRINT(" - Checking results\n");
300304
check_results(0);
@@ -331,7 +335,7 @@ static void test_delayed_resubmit(void)
331335
k_delayed_work_submit(&tests[0].work, WORK_ITEM_WAIT);
332336

333337
TC_PRINT(" - Waiting for work to finish\n");
334-
k_sleep((NUM_TEST_ITEMS + 1) * WORK_ITEM_WAIT);
338+
k_sleep(NUM_TEST_ITEMS * WORK_ITEM_WAIT_ALIGNED);
335339

336340
TC_PRINT(" - Checking results\n");
337341
check_results(NUM_TEST_ITEMS);
@@ -378,7 +382,7 @@ static void test_delayed_resubmit_thread(void)
378382
NULL, NULL, NULL, K_PRIO_COOP(10), 0, 0);
379383

380384
TC_PRINT(" - Waiting for work to finish\n");
381-
k_sleep(WORK_ITEM_WAIT);
385+
k_sleep(WORK_ITEM_WAIT_ALIGNED);
382386

383387
TC_PRINT(" - Checking results\n");
384388
check_results(1);
@@ -403,7 +407,7 @@ static void test_delayed(void)
403407
test_delayed_submit();
404408

405409
TC_PRINT(" - Waiting for delayed work to finish\n");
406-
k_sleep((NUM_TEST_ITEMS + 2) * WORK_ITEM_WAIT);
410+
k_sleep(NUM_TEST_ITEMS * WORK_ITEM_WAIT_ALIGNED);
407411

408412
TC_PRINT(" - Checking results\n");
409413
check_results(NUM_TEST_ITEMS);

0 commit comments

Comments
 (0)