Skip to content

Commit 31ebd60

Browse files
peter-mitsiskartben
authored andcommitted
tests: Test k_reschedule() with deadline scheduler
Setting a deadline is not a schedule point. This makes it a perfect place to verify the behavior of the new k_reschedule() routine at both thread and ISR level. Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
1 parent 669f2c4 commit 31ebd60

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

tests/kernel/sched/deadline/prj.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ CONFIG_BT=n
77
# Deadline is not compatible with MULTIQ, so we have to pick something
88
# specific instead of using the board-level default.
99
CONFIG_SCHED_DUMB=y
10+
11+
CONFIG_IRQ_OFFLOAD=y
12+
CONFIG_IRQ_OFFLOAD_NESTED=n

tests/kernel/sched/deadline/src/main.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
*/
1414
#define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
1515

16+
#define MSEC_TO_CYCLES(msec) (int)(((uint64_t)(msec) * \
17+
(uint64_t)CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC) / \
18+
(uint64_t)MSEC_PER_SEC)
19+
1620
struct k_thread worker_threads[NUM_THREADS];
21+
volatile struct k_thread *expected_thread;
1722
k_tid_t worker_tids[NUM_THREADS];
1823

1924
K_THREAD_STACK_ARRAY_DEFINE(worker_stacks, NUM_THREADS, STACK_SIZE);
@@ -215,4 +220,105 @@ ZTEST(suite_deadline, test_unqueued)
215220
}
216221
}
217222

223+
#if (CONFIG_MP_MAX_NUM_CPUS == 1)
224+
static void reschedule_wrapper(const void *param)
225+
{
226+
ARG_UNUSED(param);
227+
228+
k_reschedule();
229+
}
230+
231+
static void test_reschedule_helper0(void *p1, void *p2, void *p3)
232+
{
233+
/* 4. Reschedule brings us here */
234+
235+
zassert_true(expected_thread == arch_current_thread(), "");
236+
237+
expected_thread = &worker_threads[1];
238+
}
239+
240+
static void test_reschedule_helper1(void *p1, void *p2, void *p3)
241+
{
242+
void (*offload)(void (*f)(const void *p), const void *param) = p1;
243+
244+
/* 1. First helper expected to execute */
245+
246+
zassert_true(expected_thread == arch_current_thread(), "");
247+
248+
offload(reschedule_wrapper, NULL);
249+
250+
/* 2. Deadlines have not changed. Expected no changes */
251+
252+
zassert_true(expected_thread == arch_current_thread(), "");
253+
254+
k_thread_deadline_set(arch_current_thread(), MSEC_TO_CYCLES(1000));
255+
256+
/* 3. Deadline changed, but there was no reschedule */
257+
258+
zassert_true(expected_thread == arch_current_thread(), "");
259+
260+
expected_thread = &worker_threads[0];
261+
offload(reschedule_wrapper, NULL);
262+
263+
/* 5. test_thread_reschedule_helper0 executed */
264+
265+
zassert_true(expected_thread == arch_current_thread(), "");
266+
}
267+
268+
static void thread_offload(void (*f)(const void *p), const void *param)
269+
{
270+
f(param);
271+
}
272+
273+
ZTEST(suite_deadline, test_thread_reschedule)
274+
{
275+
k_thread_create(&worker_threads[0], worker_stacks[0], STACK_SIZE,
276+
test_reschedule_helper0,
277+
thread_offload, NULL, NULL,
278+
K_LOWEST_APPLICATION_THREAD_PRIO,
279+
0, K_NO_WAIT);
280+
281+
k_thread_create(&worker_threads[1], worker_stacks[1], STACK_SIZE,
282+
test_reschedule_helper1,
283+
thread_offload, NULL, NULL,
284+
K_LOWEST_APPLICATION_THREAD_PRIO,
285+
0, K_NO_WAIT);
286+
287+
k_thread_deadline_set(&worker_threads[0], MSEC_TO_CYCLES(500));
288+
k_thread_deadline_set(&worker_threads[1], MSEC_TO_CYCLES(10));
289+
290+
expected_thread = &worker_threads[1];
291+
292+
k_thread_join(&worker_threads[1], K_FOREVER);
293+
k_thread_join(&worker_threads[0], K_FOREVER);
294+
295+
#ifndef CONFIG_SMP
296+
/*
297+
* When SMP is enabled, there is always a reschedule performed
298+
* at the end of the ISR.
299+
*/
300+
k_thread_create(&worker_threads[0], worker_stacks[0], STACK_SIZE,
301+
test_reschedule_helper0,
302+
irq_offload, NULL, NULL,
303+
K_LOWEST_APPLICATION_THREAD_PRIO,
304+
0, K_NO_WAIT);
305+
306+
k_thread_create(&worker_threads[1], worker_stacks[1], STACK_SIZE,
307+
test_reschedule_helper1,
308+
irq_offload, NULL, NULL,
309+
K_LOWEST_APPLICATION_THREAD_PRIO,
310+
0, K_NO_WAIT);
311+
312+
k_thread_deadline_set(&worker_threads[0], MSEC_TO_CYCLES(500));
313+
k_thread_deadline_set(&worker_threads[1], MSEC_TO_CYCLES(10));
314+
315+
expected_thread = &worker_threads[1];
316+
317+
k_thread_join(&worker_threads[1], K_FOREVER);
318+
k_thread_join(&worker_threads[0], K_FOREVER);
319+
320+
#endif /* !CONFIG_SMP */
321+
}
322+
#endif /* CONFIG_MP_MAX_NUM_CPUS == 1 */
323+
218324
ZTEST_SUITE(suite_deadline, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)