Skip to content

Commit

Permalink
tests: Test k_reschedule() with deadline scheduler
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
peter-mitsis authored and kartben committed Dec 24, 2024
1 parent 669f2c4 commit 31ebd60
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/kernel/sched/deadline/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ CONFIG_BT=n
# Deadline is not compatible with MULTIQ, so we have to pick something
# specific instead of using the board-level default.
CONFIG_SCHED_DUMB=y

CONFIG_IRQ_OFFLOAD=y
CONFIG_IRQ_OFFLOAD_NESTED=n
106 changes: 106 additions & 0 deletions tests/kernel/sched/deadline/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
*/
#define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACK_SIZE)

#define MSEC_TO_CYCLES(msec) (int)(((uint64_t)(msec) * \
(uint64_t)CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC) / \
(uint64_t)MSEC_PER_SEC)

struct k_thread worker_threads[NUM_THREADS];
volatile struct k_thread *expected_thread;
k_tid_t worker_tids[NUM_THREADS];

K_THREAD_STACK_ARRAY_DEFINE(worker_stacks, NUM_THREADS, STACK_SIZE);
Expand Down Expand Up @@ -215,4 +220,105 @@ ZTEST(suite_deadline, test_unqueued)
}
}

#if (CONFIG_MP_MAX_NUM_CPUS == 1)
static void reschedule_wrapper(const void *param)
{
ARG_UNUSED(param);

k_reschedule();
}

static void test_reschedule_helper0(void *p1, void *p2, void *p3)
{
/* 4. Reschedule brings us here */

zassert_true(expected_thread == arch_current_thread(), "");

expected_thread = &worker_threads[1];
}

static void test_reschedule_helper1(void *p1, void *p2, void *p3)
{
void (*offload)(void (*f)(const void *p), const void *param) = p1;

/* 1. First helper expected to execute */

zassert_true(expected_thread == arch_current_thread(), "");

offload(reschedule_wrapper, NULL);

/* 2. Deadlines have not changed. Expected no changes */

zassert_true(expected_thread == arch_current_thread(), "");

k_thread_deadline_set(arch_current_thread(), MSEC_TO_CYCLES(1000));

/* 3. Deadline changed, but there was no reschedule */

zassert_true(expected_thread == arch_current_thread(), "");

expected_thread = &worker_threads[0];
offload(reschedule_wrapper, NULL);

/* 5. test_thread_reschedule_helper0 executed */

zassert_true(expected_thread == arch_current_thread(), "");
}

static void thread_offload(void (*f)(const void *p), const void *param)
{
f(param);
}

ZTEST(suite_deadline, test_thread_reschedule)
{
k_thread_create(&worker_threads[0], worker_stacks[0], STACK_SIZE,
test_reschedule_helper0,
thread_offload, NULL, NULL,
K_LOWEST_APPLICATION_THREAD_PRIO,
0, K_NO_WAIT);

k_thread_create(&worker_threads[1], worker_stacks[1], STACK_SIZE,
test_reschedule_helper1,
thread_offload, NULL, NULL,
K_LOWEST_APPLICATION_THREAD_PRIO,
0, K_NO_WAIT);

k_thread_deadline_set(&worker_threads[0], MSEC_TO_CYCLES(500));
k_thread_deadline_set(&worker_threads[1], MSEC_TO_CYCLES(10));

expected_thread = &worker_threads[1];

k_thread_join(&worker_threads[1], K_FOREVER);
k_thread_join(&worker_threads[0], K_FOREVER);

#ifndef CONFIG_SMP
/*
* When SMP is enabled, there is always a reschedule performed
* at the end of the ISR.
*/
k_thread_create(&worker_threads[0], worker_stacks[0], STACK_SIZE,
test_reschedule_helper0,
irq_offload, NULL, NULL,
K_LOWEST_APPLICATION_THREAD_PRIO,
0, K_NO_WAIT);

k_thread_create(&worker_threads[1], worker_stacks[1], STACK_SIZE,
test_reschedule_helper1,
irq_offload, NULL, NULL,
K_LOWEST_APPLICATION_THREAD_PRIO,
0, K_NO_WAIT);

k_thread_deadline_set(&worker_threads[0], MSEC_TO_CYCLES(500));
k_thread_deadline_set(&worker_threads[1], MSEC_TO_CYCLES(10));

expected_thread = &worker_threads[1];

k_thread_join(&worker_threads[1], K_FOREVER);
k_thread_join(&worker_threads[0], K_FOREVER);

#endif /* !CONFIG_SMP */
}
#endif /* CONFIG_MP_MAX_NUM_CPUS == 1 */

ZTEST_SUITE(suite_deadline, NULL, NULL, NULL, NULL, NULL);

0 comments on commit 31ebd60

Please sign in to comment.