1 /*
2 * Copyright (c) 2018, Google Inc. All rights reserved
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <kernel/mp.h>
25 #include <kernel/thread.h>
26 #include <lib/unittest/unittest.h>
27 #include <lk/init.h>
28 #include <stdbool.h>
29 #include <stdio.h>
30
31 #define THREAD_DELAY_MS 1
32
33 #define SMPTEST_CYCLES 16
34
35 static struct smptest_thread {
36 thread_t* thread;
37
38 volatile bool started;
39 volatile uint unblock_count;
40 volatile uint error_count;
41 volatile uint done_count;
42
43 } smptest_thread[SMP_MAX_CPUS];
44
45 /* Check if a thread is blocked, using volatile to ensure re-read */
thread_is_blocked(volatile thread_t * thread)46 static bool thread_is_blocked(volatile thread_t* thread) {
47 return thread->state == THREAD_BLOCKED;
48 }
49
smptest_thread_func(void * arg)50 static int smptest_thread_func(void* arg) {
51 const uint i = (uintptr_t)arg;
52 const uint expected_cpu = i;
53 struct smptest_thread* const smpt = &smptest_thread[i];
54
55 /* Note thread as started so main thread sees which CPUs are available */
56 smpt->started = true;
57
58 uint cpu = arch_curr_cpu_num();
59 if (cpu != expected_cpu) {
60 /* Warn if the thread starts on another CPU than it was pinned to */
61 printf("%s: thread %d started on wrong cpu: %d\n", __func__, i, cpu);
62 smpt->error_count++;
63 }
64
65 while (true) {
66 THREAD_LOCK(state1);
67 get_current_thread()->state = THREAD_BLOCKED;
68 thread_block();
69
70 cpu = arch_curr_cpu_num();
71 if (cpu != expected_cpu) {
72 /* Don't update any state if the thread runs on the wrong CPU. */
73 printf("%s: thread %d ran on wrong cpu: %d\n", __func__, i, cpu);
74 smpt->error_count++;
75 continue;
76 }
77
78 /*
79 * Update unblock count for this cpu so the main test thread can see
80 * that it ran.
81 */
82 smpt->unblock_count++;
83 THREAD_UNLOCK(state1);
84
85 /* Sleep to allow other threads to block */
86 thread_sleep(THREAD_DELAY_MS);
87
88 THREAD_LOCK(state2);
89
90 /* Find and unblock the next started cpu */
91 for (uint next_cpu = i + 1; next_cpu < SMP_MAX_CPUS; next_cpu++) {
92 if (smptest_thread[next_cpu].started) {
93 thread_t* next = smptest_thread[next_cpu].thread;
94
95 /* Next CPU should be blocked; wake it up */
96 if (thread_is_blocked(next)) {
97 thread_unblock(next, false);
98 } else {
99 printf("%s: thread %d not blocked\n", __func__, i + 1);
100 smpt->error_count++;
101 }
102
103 break;
104 }
105 }
106
107 /*
108 * Update unblock count for this cpu so the main test thread can see
109 * that it completed.
110 */
111 smpt->done_count++;
112 THREAD_UNLOCK(state2);
113 }
114 return 0;
115 }
116
TEST(smptest,check_cpu_active)117 TEST(smptest, check_cpu_active) {
118 uint active_cpu_count = 0;
119 for (uint i = 0; i < SMP_MAX_CPUS; i++) {
120 if (mp_is_cpu_active(i)) {
121 active_cpu_count++;
122 }
123 }
124 EXPECT_GE(active_cpu_count, SMPTEST_MIN_CPU_COUNT);
125 }
126
TEST(smptest,run)127 TEST(smptest, run) {
128 bool wait_for_cpus = false;
129
130 for (uint i = 0; i < SMP_MAX_CPUS; i++) {
131 if (!thread_is_blocked(smptest_thread[i].thread)) {
132 unittest_printf("[ INFO ] thread %d not ready\n", i);
133 wait_for_cpus = true;
134 }
135 }
136
137 /*
138 * test-runner can start the test before all CPUs have finished booting.
139 * Wait another second for all the CPUs we need to be ready if needed.
140 */
141 if (wait_for_cpus) {
142 unittest_printf("[ INFO ] waiting for threads to be ready\n");
143 thread_sleep(1000);
144 }
145
146 for (uint i = 0; i < SMP_MAX_CPUS; i++) {
147 ASSERT_EQ(!mp_is_cpu_active(i) ||
148 thread_is_blocked(smptest_thread[i].thread),
149 true, "thread %d not ready\n", i);
150 }
151
152 for (uint i = 0; i < SMP_MAX_CPUS; i++) {
153 smptest_thread[i].unblock_count = 0;
154 smptest_thread[i].error_count = 0;
155 smptest_thread[i].done_count = 0;
156 }
157
158 /*
159 * Repeat the test, in case the CPUs don't go back to the same state
160 * after the first wake-up
161 */
162 for (uint j = 1; j < SMPTEST_CYCLES; j++) {
163 THREAD_LOCK(state);
164 /*
165 * Wake up thread on CPU 0 to start a test run. Each thread 'n' should
166 * wake-up thread 'n+1' until the last thread stops.
167 * Check thread is blocked before unblocking to avoid asserts.
168 */
169 if (thread_is_blocked(smptest_thread[0].thread)) {
170 thread_unblock(smptest_thread[0].thread, false);
171 }
172
173 THREAD_UNLOCK(state);
174
175 /* Sleep to allow all CPUs to run with some margin */
176 thread_sleep((THREAD_DELAY_MS + 5) * SMP_MAX_CPUS);
177
178 /*
179 * Check that every CPU-thread ran exactly once each time we woke up the
180 * first thread.
181 */
182 for (uint cpu = 0; cpu < SMP_MAX_CPUS; cpu++) {
183 const struct smptest_thread* const smpt = &smptest_thread[cpu];
184
185 /*
186 * Some cpus can still execute the thread body (e.g. if they are
187 * interrupted by some other jobs), let them time to finish
188 * (up to 1 sec, then think they got stuck).
189 */
190 for (int i = 0; i < 10; i++) {
191 if (smpt->started &&
192 (smpt->unblock_count != j || smpt->done_count != j)) {
193 thread_sleep(100);
194 }
195 }
196
197 const int unblock_count = smpt->unblock_count;
198 const int error_count = smpt->error_count;
199 const int done_count = smpt->done_count;
200
201 if (smpt->started) {
202 EXPECT_EQ(unblock_count, j, "cpu %d FAILED block count\n", cpu);
203 EXPECT_EQ(error_count, 0, "cpu %d FAILED error count\n", cpu);
204 EXPECT_EQ(done_count, j, "cpu %d FAILED done count\n", cpu);
205
206 if (j == SMPTEST_CYCLES - 1) {
207 unittest_printf(
208 "[ INFO ] smptest cpu %d ran %d times\n", cpu,
209 SMPTEST_CYCLES);
210 }
211 } else {
212 EXPECT_EQ(mp_is_cpu_active(cpu), false,
213 "cpu %d active but not running", cpu);
214 EXPECT_EQ(unblock_count, 0, "cpu %d FAILED block count\n", cpu);
215 EXPECT_EQ(error_count, 0, "cpu %d FAILED error count\n", cpu);
216 EXPECT_EQ(done_count, 0, "cpu %d FAILED done count\n", cpu);
217 }
218 }
219 }
220
221 test_abort:;
222 }
223
smptest_setup(uint level)224 static void smptest_setup(uint level) {
225 /* Create a thread for each possible CPU */
226 for (uint cpu = 0; cpu < SMP_MAX_CPUS; cpu++) {
227 struct smptest_thread* smpt = &smptest_thread[cpu];
228 char thread_name[32];
229
230 snprintf(thread_name, sizeof(thread_name), "smptest-%u", cpu);
231 smpt->thread = thread_create(thread_name, smptest_thread_func,
232 (void*)(uintptr_t)cpu, HIGH_PRIORITY,
233 DEFAULT_STACK_SIZE);
234 thread_set_pinned_cpu(smpt->thread, cpu);
235 }
236
237 /* Allow threads to run */
238 for (uint cpu = 0; cpu < SMP_MAX_CPUS; cpu++) {
239 thread_resume(smptest_thread[cpu].thread);
240 }
241 }
242
243 LK_INIT_HOOK(smptest_hook, smptest_setup, LK_INIT_LEVEL_APPS);
244
245 PORT_TEST(smptest, "com.android.kernel.smp-unittest");
246