From 927420a42359ea69655093a001fe4ccfdf7835f1 Mon Sep 17 00:00:00 2001 From: Yong Cong Sin Date: Thu, 7 Nov 2024 15:08:31 +0800 Subject: [PATCH] tests: sched: Add busy threads for SMP The sched benchmark is designed for systems with a single CPU. Otherwise, the timestamps would be wrong when the partner thread is scheduled on another CPU, i.e. negative values: ``` unpend 63 ready 62 switch -16562 pend 18937 tot 2500 (avg 928) ``` When the system allows for multiple CPUs, spawn a non-preemptible thread to keep the other CPUs busy. Signed-off-by: Yong Cong Sin --- tests/benchmarks/sched/src/main.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/benchmarks/sched/src/main.c b/tests/benchmarks/sched/src/main.c index a372ba3c1a8..e40b6a1afd0 100644 --- a/tests/benchmarks/sched/src/main.c +++ b/tests/benchmarks/sched/src/main.c @@ -36,6 +36,15 @@ static K_THREAD_STACK_DEFINE(partner_stack, 1024); static struct k_thread partner_thread; +#if (CONFIG_MP_MAX_NUM_CPUS > 1) +static struct k_thread busy_thread[CONFIG_MP_MAX_NUM_CPUS - 1]; + +#define BUSY_THREAD_STACK_SIZE (1024 + CONFIG_TEST_EXTRA_STACK_SIZE) + +static K_THREAD_STACK_ARRAY_DEFINE(busy_thread_stack, CONFIG_MP_MAX_NUM_CPUS - 1, + BUSY_THREAD_STACK_SIZE); +#endif /* (CONFIG_MP_MAX_NUM_CPUS > 1) */ + _wait_q_t waitq; enum { @@ -88,8 +97,26 @@ static void partner_fn(void *arg1, void *arg2, void *arg3) } } +#if (CONFIG_MP_MAX_NUM_CPUS > 1) +static void busy_thread_entry(void *arg1, void *arg2, void *arg3) +{ + while (true) { + } +} +#endif /* (CONFIG_MP_MAX_NUM_CPUS > 1) */ + int main(void) { +#if (CONFIG_MP_MAX_NUM_CPUS > 1) + /* Spawn busy threads that will execute on the other cores */ + for (uint32_t i = 0; i < CONFIG_MP_MAX_NUM_CPUS - 1; i++) { + k_thread_create(&busy_thread[i], busy_thread_stack[i], + BUSY_THREAD_STACK_SIZE, busy_thread_entry, + NULL, NULL, NULL, + K_HIGHEST_THREAD_PRIO, 0, K_NO_WAIT); + } +#endif /* (CONFIG_MP_MAX_NUM_CPUS > 1) */ + z_waitq_init(&waitq); int main_prio = k_thread_priority_get(k_current_get());