1717 * @note Create multiple threads untied core threads, run them for a while on each core to see
1818 * if the threads are automatically distributed evenly, run for a while to output the threads
1919 * running on each core.
20+ *
21+ * Test Case Name: [smp_assigned_idle_cores]
22+ *
23+ * Test Objectives:
24+ * - Test whether ready threads unbound to cores can be automatically allocated
25+ * - to idle cores under the SMP architecture.
26+ *
27+ * Test Scenarios:
28+ * - Under the SMP architecture, each core spends most of its time running the
29+ * - idle thread after the system starts. At this point, create RT_CPUS_NR-1 cyclic
30+ * - tasks and observe whether these tasks can be evenly distributed across all
31+ * - cores for execution. Since the thread running the utest occupies one core, it
32+ * - is only necessary to observe whether the remaining (RT_CPUS_NR - 1) cores can
33+ * - be allocated the newly created threads and execute them.
34+ *
35+ * Verification Metrics:
36+ * - After running this test case, it is necessary to observe the printed thread
37+ * - list, where all threads created with names from T0 to T(RT_CPUS_NR-2) must
38+ * - be in the running state. RT_CPUS_NR must be greater than or equal to 2.
39+ *
40+ * Dependencies:
41+ * - RT_USING_SMP needs to be enabled.
42+ *
43+ * Expected Results:
44+ * - Print the thread list on the terminal, and observe that T0 to T(RT_CPUS_NR-2)
45+ * - are all in the running state, with the output "[ PASSED ] [ result ] testcase
46+ * - (core.smp_assigned_idle_cores)".
2047 */
2148
2249#define THREAD_STACK_SIZE UTEST_THR_STACK_SIZE
2350#define THREAD_PRIORITY 20
24- static rt_thread_t threads [RT_CPUS_NR ];
25- static int tick = 0 , finsh_flag = 0 ;
51+ static rt_thread_t threads [RT_CPUS_NR - 1 ];
52+ static int tick = 0 , finish_flag = 0 ;
2653static int num = 0 ;
2754/* thread entry function */
2855static void thread_entry (void * parameter )
2956{
57+ int value = * (int * )parameter ;
3058 while (1 )
3159 {
3260 tick ++ ;
33- if (tick == 100 )
61+ if (tick >= 100 && ( finish_flag & ( 1 << value )) == 0 )
3462 {
35- /* Output the current core running threads */
36- extern long list_thread (void );
37- list_thread ();
38- finsh_flag = 0xA55A ;
63+ rt_atomic_or ((volatile rt_atomic_t * )& finish_flag , (1 << value ));
3964 uassert_true (1 );
4065 }
41- rt_thread_delay (5 );
4266 }
4367}
4468
@@ -54,8 +78,8 @@ static void thread_on_idle_core_tc(void)
5478 params [i ] = i ;
5579 }
5680
57- /* Create RT_CPUS_NR threads and pass the entry parameters for each thread */
58- for (i = 0 ; i < RT_CPUS_NR ; i ++ )
81+ /* Create RT_CPUS_NR-1 threads and pass the entry parameters for each thread */
82+ for (i = 0 ; i < RT_CPUS_NR - 1 ; i ++ )
5983 {
6084 rt_snprintf (thread_name , sizeof (thread_name ), "T%d" , i );
6185 threads [i ] = rt_thread_create (thread_name , thread_entry , & params [i ], THREAD_STACK_SIZE , THREAD_PRIORITY , 20 );
@@ -66,18 +90,23 @@ static void thread_on_idle_core_tc(void)
6690 }
6791 }
6892 /* Waiting for test cases to finish */
69- while (finsh_flag != 0xA55A );
93+ while (finish_flag != (1 <<(RT_CPUS_NR - 1 ))- 1 );
94+ /* Output the current core running threads */
95+ extern long list_thread (void );
96+ list_thread ();
7097}
7198
7299static rt_err_t utest_tc_init (void )
73100{
101+ finish_flag = 0 ;
102+ tick = 0 ;
74103 rt_kprintf ("[Test case]: created threads are automatically assigned to run on idle cores\r\n" );
75104 return RT_EOK ;
76105}
77106
78107static rt_err_t utest_tc_cleanup (void )
79108{
80- for (num = 0 ; num < RT_CPUS_NR ; num ++ )
109+ for (num = 0 ; num < RT_CPUS_NR - 1 ; num ++ )
81110 {
82111 rt_thread_delete (threads [num ]);
83112 }
@@ -89,3 +118,4 @@ static void testcase(void)
89118 UTEST_UNIT_RUN (thread_on_idle_core_tc );
90119}
91120UTEST_TC_EXPORT (testcase , "core.smp_assigned_idle_cores" , utest_tc_init , utest_tc_cleanup , 10 );
121+
0 commit comments