From 9319fde7eb9747a0751209c05a3ff757ddc910ce Mon Sep 17 00:00:00 2001 From: Tm-C-mT <490534897@qq.com> Date: Mon, 17 Nov 2025 10:00:30 +0800 Subject: [PATCH 1/2] [utest]: Modify the utest logic for thread allocation of idle harts Currently, this utest cannot determine whether threads are evenly distributed across idle harts by observing the result of list_thread(). This is because the presence of rt_thread_delay(5); causes all other threads to be in the suspended state when thread information is printed. For example, if RT_CPUS_NR=4, T0 executes list_thread() to print information, while T1~T3 are in hibernation and thus it is impossible to observe which hart they are running on. Solution:Here, the completion judgment condition has been modified. For example, when RT_CPUS_NR=4, only RT_CPUS_NR-1 threads will be created (i.e., T0 to T2), because running the utest occupies one hart. The execution is judged as completed when finish_flag=0x0007, and the thread running the utest will call list_thread() to print the information. Observe whether T0 to T2 are running on different harts simultaneously. Signed-off-by: Mengchen Teng --- src/utest/smp/smp_assigned_idle_cores_tc.c | 27 ++++++++++++---------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/utest/smp/smp_assigned_idle_cores_tc.c b/src/utest/smp/smp_assigned_idle_cores_tc.c index f7edad29f7f..26a8efed5c1 100644 --- a/src/utest/smp/smp_assigned_idle_cores_tc.c +++ b/src/utest/smp/smp_assigned_idle_cores_tc.c @@ -21,24 +21,21 @@ #define THREAD_STACK_SIZE UTEST_THR_STACK_SIZE #define THREAD_PRIORITY 20 -static rt_thread_t threads[RT_CPUS_NR]; -static int tick = 0, finsh_flag = 0; +static rt_thread_t threads[RT_CPUS_NR - 1]; +static int tick = 0, finish_flag = 0; static int num = 0; /* thread entry function */ static void thread_entry(void *parameter) { + int value = *(int *)parameter; while (1) { tick++; - if (tick == 100) + if (tick >= 100 && (finish_flag & (1 << value)) == 0) { - /* Output the current core running threads */ - extern long list_thread(void); - list_thread(); - finsh_flag = 0xA55A; + rt_atomic_or((volatile rt_atomic_t *)&finish_flag, (1 << value)); uassert_true(1); } - rt_thread_delay(5); } } @@ -54,8 +51,8 @@ static void thread_on_idle_core_tc(void) params[i] = i; } - /* Create RT_CPUS_NR threads and pass the entry parameters for each thread */ - for (i = 0; i < RT_CPUS_NR; i++) + /* Create RT_CPUS_NR-1 threads and pass the entry parameters for each thread */ + for (i = 0; i < RT_CPUS_NR - 1; i++) { rt_snprintf(thread_name, sizeof(thread_name), "T%d", i); threads[i] = rt_thread_create(thread_name, thread_entry, ¶ms[i], THREAD_STACK_SIZE, THREAD_PRIORITY, 20); @@ -66,18 +63,23 @@ static void thread_on_idle_core_tc(void) } } /* Waiting for test cases to finish */ - while (finsh_flag != 0xA55A); + while (finish_flag != (1<<(RT_CPUS_NR-1))-1); + /* Output the current core running threads */ + extern long list_thread(void); + list_thread(); } static rt_err_t utest_tc_init(void) { + finish_flag = 0; + tick = 0; rt_kprintf("[Test case]: created threads are automatically assigned to run on idle cores\r\n"); return RT_EOK; } static rt_err_t utest_tc_cleanup(void) { - for (num = 0; num < RT_CPUS_NR; num++) + for (num = 0; num < RT_CPUS_NR - 1; num++) { rt_thread_delete(threads[num]); } @@ -89,3 +91,4 @@ static void testcase(void) UTEST_UNIT_RUN(thread_on_idle_core_tc); } UTEST_TC_EXPORT(testcase, "core.smp_assigned_idle_cores", utest_tc_init, utest_tc_cleanup, 10); + From 661d0750f1fac383883566de17346dca82b5b5d3 Mon Sep 17 00:00:00 2001 From: Tm-C-mT <490534897@qq.com> Date: Wed, 19 Nov 2025 16:02:53 +0800 Subject: [PATCH 2/2] [docs][utest]:Add explanatory comments for the utest of thread allocation for idle harts Add explanatory comments for the utest of smp_assigned_idle_cores. Signed-off-by: Mengchen Teng --- src/utest/smp/smp_assigned_idle_cores_tc.c | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/utest/smp/smp_assigned_idle_cores_tc.c b/src/utest/smp/smp_assigned_idle_cores_tc.c index 26a8efed5c1..74e31760f33 100644 --- a/src/utest/smp/smp_assigned_idle_cores_tc.c +++ b/src/utest/smp/smp_assigned_idle_cores_tc.c @@ -17,6 +17,33 @@ * @note Create multiple threads untied core threads, run them for a while on each core to see * if the threads are automatically distributed evenly, run for a while to output the threads * running on each core. + * + * Test Case Name: [smp_assigned_idle_cores] + * + * Test Objectives: + * - Test whether ready threads unbound to cores can be automatically allocated + * - to idle cores under the SMP architecture. + * + * Test Scenarios: + * - Under the SMP architecture, each core spends most of its time running the + * - idle thread after the system starts. At this point, create RT_CPUS_NR-1 cyclic + * - tasks and observe whether these tasks can be evenly distributed across all + * - cores for execution. Since the thread running the utest occupies one core, it + * - is only necessary to observe whether the remaining (RT_CPUS_NR - 1) cores can + * - be allocated the newly created threads and execute them. + * + * Verification Metrics: + * - After running this test case, it is necessary to observe the printed thread + * - list, where all threads created with names from T0 to T(RT_CPUS_NR-2) must + * - be in the running state. RT_CPUS_NR must be greater than or equal to 2. + * + * Dependencies: + * - RT_USING_SMP needs to be enabled. + * + * Expected Results: + * - Print the thread list on the terminal, and observe that T0 to T(RT_CPUS_NR-2) + * - are all in the running state, with the output "[ PASSED ] [ result ] testcase + * - (core.smp_assigned_idle_cores)". */ #define THREAD_STACK_SIZE UTEST_THR_STACK_SIZE