-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[utest]: Modification of the SMP Threads Auto Assign to Idle Core uTest #10942
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,28 +17,52 @@ | |||||||
| * @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 | ||||||||
| #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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加延时会让线程处于suspend态,导致观察不到新创建的线程是否被均匀分配到了空闲hart上。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加延时会让线程处于suspend态,导致观察不到新创建的线程是否被均匀分配到了空闲hart上。
是的,你这个改动合理
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Concurrency/并发]: Potential race condition on shared variable
tickEnglish: Multiple threads increment the shared variable
tickwithout synchronization (line 60). In an SMP environment, this creates a race condition where concurrent increments from different cores can lead to lost updates. While this may not affect the test's primary purpose (observing thread distribution), it could cause thetick >= 100condition to be reached unreliably. Consider using atomic operations (rt_atomic_tand atomic increment) or protecting the increment with a lock.中文: 多个线程在没有同步的情况下递增共享变量
tick(第60行)。在 SMP 环境中,这会产生竞态条件,来自不同核心的并发递增可能导致更新丢失。虽然这可能不影响测试的主要目的(观察线程分布),但可能导致tick >= 100条件不可靠地到达。建议使用原子操作(rt_atomic_t和原子递增)或使用锁保护递增操作。