1 /*
2 * Copyright (c) 2006-2024, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2024-08-10 RV the first version
9 */
10
11 #include <rtthread.h>
12 #include "utest.h"
13
14 /**
15 * @brief Binding core affinity testcase.
16 *
17 * @note Create RT_CPUS_NR threads, thread 0 is bound to core 0, other threads are not bound to specific cores,
18 * after running for a set number of times, count the number of times each core is run on the corresponding core,
19 * thread 0 should always be run on core 0, other threads will be run on different cores.
20 */
21
22 /* Number of thread runs */
23 static int run_num = 100;
24 #define THREAD_STACK_SIZE UTEST_THR_STACK_SIZE
25 #define THREAD_PRIORITY 20
26 static rt_thread_t threads[RT_CPUS_NR];
27 static struct rt_spinlock lock;
28 static int thread_inc[RT_CPUS_NR] = {0};
29 static int thread_tic[RT_CPUS_NR] = {0};
30 static int finsh_flag = 0;
31 static int num = 0;
32
thread_entry(void * parameter)33 static void thread_entry(void *parameter)
34 {
35 int id = 0;
36 int para = *(int *)parameter;
37 while (1)
38 {
39 thread_tic[para]++;
40 id = rt_hw_cpu_id();
41 if (para == id)
42 {
43 thread_inc[para]++;
44 }
45
46 if (thread_tic[para] == run_num)
47 {
48 if (para == 0)
49 uassert_int_equal(thread_inc[para], thread_tic[para]);
50 else
51 uassert_int_not_equal(thread_inc[para], thread_tic[para]);
52 finsh_flag ++;
53 }
54 rt_thread_delay(5);
55 }
56 }
57
thread_bind_affinity_tc(void)58 static void thread_bind_affinity_tc(void)
59 {
60 static int params[RT_CPUS_NR] = {0};
61 char thread_name[8];
62 int i, j;
63
64 for (i = 0; i < RT_CPUS_NR; i++)
65 {
66 params[i] = i;
67 }
68
69 /* Create RT_CPUS_NR threads Thread 0 is bound to core 0 Other threads are not bound */
70 for (i = 0; i < RT_CPUS_NR; i++)
71 {
72 rt_snprintf(thread_name, sizeof(thread_name), "thread%d", i);
73 threads[i] = rt_thread_create(thread_name, thread_entry, (int *)¶ms[i], THREAD_STACK_SIZE, THREAD_PRIORITY, 20);
74 if (i == 0)
75 {
76 rt_thread_control(threads[0], RT_THREAD_CTRL_BIND_CPU, (void *)0);
77 }
78 if (threads[i] != RT_NULL)
79 {
80 rt_thread_startup(threads[i]);
81 }
82 }
83
84 while (finsh_flag != RT_CPUS_NR);
85
86 /* Displays the number of times a thread was executed on the relevant core */
87 for (j = 0; j < RT_CPUS_NR; j++)
88 {
89 rt_spin_lock(&lock);
90 rt_kprintf("Total runs[%d], Number of times thread[%d] run on [core%d]: [%4d], always run at core%d ? %s \r\n", run_num, j, j, thread_inc[j], j, (thread_inc[j] == run_num) ? "yes" : "no");
91 rt_spin_unlock(&lock);
92 }
93 }
94
utest_tc_init(void)95 static rt_err_t utest_tc_init(void)
96 {
97 rt_spin_lock_init(&lock);
98 return RT_EOK;
99 }
100
utest_tc_cleanup(void)101 static rt_err_t utest_tc_cleanup(void)
102 {
103 for (num = 0; num < RT_CPUS_NR; num++)
104 {
105 rt_thread_delete(threads[num]);
106 }
107 return RT_EOK;
108 }
109
testcase(void)110 static void testcase(void)
111 {
112 UTEST_UNIT_RUN(thread_bind_affinity_tc);
113 }
114 UTEST_TC_EXPORT(testcase, "testcases.smp.bind_affinity_tc", utest_tc_init, utest_tc_cleanup, 10);
115