test: Add a testcase for cpu affinity

Pin thread to a specific cpu. Once thread gets cpu, check
the cpu id is correct and then thread will give up cpu.

Signed-off-by: TaiJu Wu <tjwu1217@gmail.com>
This commit is contained in:
TaiJu Wu 2024-03-07 00:49:50 +00:00 committed by Henrik Brix Andersen
commit 18d9ba4b82
2 changed files with 52 additions and 0 deletions

View file

@ -1172,6 +1172,50 @@ ZTEST(smp, test_smp_switch_torture)
}
}
/**
* @brief Torture test for cpu affinity code
*
* @ingroup kernel_smp_tests
*
* @details Pin thread to a specific cpu. Once thread gets cpu, check
* the cpu id is correct and then thread will give up cpu.
*/
#ifdef CONFIG_SCHED_CPU_MASK
static void check_affinity(void *arg0, void *arg1, void *arg2)
{
ARG_UNUSED(arg1);
ARG_UNUSED(arg2);
int affinity = POINTER_TO_INT(arg0);
int counter = 30;
while (counter != 0) {
zassert_equal(affinity, curr_cpu(), "Affinity test failed.");
counter--;
k_yield();
}
}
ZTEST(smp, test_smp_affinity)
{
int num_threads = arch_num_cpus();
for (int i = 0; i < num_threads; ++i) {
k_thread_create(&tthread[i], tstack[i],
STACK_SIZE, check_affinity,
INT_TO_POINTER(i), NULL, NULL,
0, 0, K_FOREVER);
k_thread_cpu_pin(&tthread[i], i);
k_thread_start(&tthread[i]);
}
for (int i = 0; i < num_threads; i++) {
k_thread_join(&tthread[i], K_FOREVER);
}
}
#endif
static void *smp_tests_setup(void)
{
/* Sleep a bit to guarantee that both CPUs enter an idle

View file

@ -14,3 +14,11 @@ tests:
filter: (CONFIG_MP_MAX_NUM_CPUS > 1) and CONFIG_MINIMAL_LIBC_SUPPORTED
extra_configs:
- CONFIG_MINIMAL_LIBC=y
kernel.multiprocessing.smp.affinity:
tags:
- kernel
- smp
ignore_faults: true
filter: (CONFIG_MP_MAX_NUM_CPUS > 1)
extra_configs:
- CONFIG_SCHED_CPU_MASK=y