Skip to content

Commit 18d9ba4

Browse files
TaiJuWuhenrikbrixandersen
authored andcommitted
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>
1 parent 49c1f97 commit 18d9ba4

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

tests/kernel/smp/src/main.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,50 @@ ZTEST(smp, test_smp_switch_torture)
11721172
}
11731173
}
11741174

1175+
/**
1176+
* @brief Torture test for cpu affinity code
1177+
*
1178+
* @ingroup kernel_smp_tests
1179+
*
1180+
* @details Pin thread to a specific cpu. Once thread gets cpu, check
1181+
* the cpu id is correct and then thread will give up cpu.
1182+
*/
1183+
#ifdef CONFIG_SCHED_CPU_MASK
1184+
static void check_affinity(void *arg0, void *arg1, void *arg2)
1185+
{
1186+
ARG_UNUSED(arg1);
1187+
ARG_UNUSED(arg2);
1188+
1189+
int affinity = POINTER_TO_INT(arg0);
1190+
int counter = 30;
1191+
1192+
while (counter != 0) {
1193+
zassert_equal(affinity, curr_cpu(), "Affinity test failed.");
1194+
counter--;
1195+
k_yield();
1196+
}
1197+
}
1198+
1199+
ZTEST(smp, test_smp_affinity)
1200+
{
1201+
int num_threads = arch_num_cpus();
1202+
1203+
for (int i = 0; i < num_threads; ++i) {
1204+
k_thread_create(&tthread[i], tstack[i],
1205+
STACK_SIZE, check_affinity,
1206+
INT_TO_POINTER(i), NULL, NULL,
1207+
0, 0, K_FOREVER);
1208+
1209+
k_thread_cpu_pin(&tthread[i], i);
1210+
k_thread_start(&tthread[i]);
1211+
}
1212+
1213+
for (int i = 0; i < num_threads; i++) {
1214+
k_thread_join(&tthread[i], K_FOREVER);
1215+
}
1216+
}
1217+
#endif
1218+
11751219
static void *smp_tests_setup(void)
11761220
{
11771221
/* Sleep a bit to guarantee that both CPUs enter an idle

tests/kernel/smp/testcase.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ tests:
1414
filter: (CONFIG_MP_MAX_NUM_CPUS > 1) and CONFIG_MINIMAL_LIBC_SUPPORTED
1515
extra_configs:
1616
- CONFIG_MINIMAL_LIBC=y
17+
kernel.multiprocessing.smp.affinity:
18+
tags:
19+
- kernel
20+
- smp
21+
ignore_faults: true
22+
filter: (CONFIG_MP_MAX_NUM_CPUS > 1)
23+
extra_configs:
24+
- CONFIG_SCHED_CPU_MASK=y

0 commit comments

Comments
 (0)