]>
Commit | Line | Data |
---|---|---|
8fda74a5 PB |
1 | /* |
2 | * rcutorture.c: simple user-level performance/stress test of RCU. | |
3 | * | |
4 | * Usage: | |
5 | * ./rcu <nreaders> rperf [ <seconds> ] | |
6 | * Run a read-side performance test with the specified | |
7 | * number of readers for <seconds> seconds. | |
8 | * ./rcu <nupdaters> uperf [ <seconds> ] | |
9 | * Run an update-side performance test with the specified | |
10 | * number of updaters and specified duration. | |
11 | * ./rcu <nreaders> perf [ <seconds> ] | |
12 | * Run a combined read/update performance test with the specified | |
13 | * number of readers and one updater and specified duration. | |
14 | * | |
15 | * The above tests produce output as follows: | |
16 | * | |
17 | * n_reads: 46008000 n_updates: 146026 nreaders: 2 nupdaters: 1 duration: 1 | |
18 | * ns/read: 43.4707 ns/update: 6848.1 | |
19 | * | |
20 | * The first line lists the total number of RCU reads and updates executed | |
21 | * during the test, the number of reader threads, the number of updater | |
22 | * threads, and the duration of the test in seconds. The second line | |
23 | * lists the average duration of each type of operation in nanoseconds, | |
24 | * or "nan" if the corresponding type of operation was not performed. | |
25 | * | |
26 | * ./rcu <nreaders> stress [ <seconds> ] | |
27 | * Run a stress test with the specified number of readers and | |
28 | * one updater. | |
29 | * | |
30 | * This test produces output as follows: | |
31 | * | |
32 | * n_reads: 114633217 n_updates: 3903415 n_mberror: 0 | |
33 | * rcu_stress_count: 114618391 14826 0 0 0 0 0 0 0 0 0 | |
34 | * | |
35 | * The first line lists the number of RCU read and update operations | |
36 | * executed, followed by the number of memory-ordering violations | |
37 | * (which will be zero in a correct RCU implementation). The second | |
38 | * line lists the number of readers observing progressively more stale | |
39 | * data. A correct RCU implementation will have all but the first two | |
40 | * numbers non-zero. | |
41 | * | |
42 | * This program is free software; you can redistribute it and/or modify | |
43 | * it under the terms of the GNU General Public License as published by | |
44 | * the Free Software Foundation; either version 2 of the License, or | |
45 | * (at your option) any later version. | |
46 | * | |
47 | * This program is distributed in the hope that it will be useful, | |
48 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
49 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
50 | * GNU General Public License for more details. | |
51 | * | |
52 | * You should have received a copy of the GNU General Public License | |
53 | * along with this program; if not, write to the Free Software | |
54 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
55 | * | |
56 | * Copyright (c) 2008 Paul E. McKenney, IBM Corporation. | |
57 | */ | |
58 | ||
59 | /* | |
60 | * Test variables. | |
61 | */ | |
62 | ||
63 | #include <glib.h> | |
64 | #include <stdlib.h> | |
65 | #include <stdio.h> | |
66 | #include <string.h> | |
67 | #include "qemu/atomic.h" | |
68 | #include "qemu/rcu.h" | |
69 | #include "qemu/compiler.h" | |
70 | #include "qemu/thread.h" | |
71 | ||
72 | long long n_reads = 0LL; | |
73 | long n_updates = 0L; | |
74 | int nthreadsrunning; | |
75 | ||
76 | #define GOFLAG_INIT 0 | |
77 | #define GOFLAG_RUN 1 | |
78 | #define GOFLAG_STOP 2 | |
79 | ||
80 | static volatile int goflag = GOFLAG_INIT; | |
81 | ||
82 | #define RCU_READ_RUN 1000 | |
83 | ||
84 | #define NR_THREADS 100 | |
8a5956ad | 85 | static QemuMutex counts_mutex; |
8fda74a5 PB |
86 | static QemuThread threads[NR_THREADS]; |
87 | static struct rcu_reader_data *data[NR_THREADS]; | |
88 | static int n_threads; | |
89 | ||
90 | static void create_thread(void *(*func)(void *)) | |
91 | { | |
92 | if (n_threads >= NR_THREADS) { | |
93 | fprintf(stderr, "Thread limit of %d exceeded!\n", NR_THREADS); | |
94 | exit(-1); | |
95 | } | |
96 | qemu_thread_create(&threads[n_threads], "test", func, &data[n_threads], | |
97 | QEMU_THREAD_JOINABLE); | |
98 | n_threads++; | |
99 | } | |
100 | ||
101 | static void wait_all_threads(void) | |
102 | { | |
103 | int i; | |
104 | ||
105 | for (i = 0; i < n_threads; i++) { | |
106 | qemu_thread_join(&threads[i]); | |
107 | } | |
108 | n_threads = 0; | |
109 | } | |
110 | ||
111 | /* | |
112 | * Performance test. | |
113 | */ | |
114 | ||
115 | static void *rcu_read_perf_test(void *arg) | |
116 | { | |
117 | int i; | |
118 | long long n_reads_local = 0; | |
119 | ||
120 | rcu_register_thread(); | |
121 | ||
122 | *(struct rcu_reader_data **)arg = &rcu_reader; | |
123 | atomic_inc(&nthreadsrunning); | |
124 | while (goflag == GOFLAG_INIT) { | |
125 | g_usleep(1000); | |
126 | } | |
127 | while (goflag == GOFLAG_RUN) { | |
128 | for (i = 0; i < RCU_READ_RUN; i++) { | |
129 | rcu_read_lock(); | |
130 | rcu_read_unlock(); | |
131 | } | |
132 | n_reads_local += RCU_READ_RUN; | |
133 | } | |
8a5956ad PB |
134 | qemu_mutex_lock(&counts_mutex); |
135 | n_reads += n_reads_local; | |
136 | qemu_mutex_unlock(&counts_mutex); | |
8fda74a5 PB |
137 | |
138 | rcu_unregister_thread(); | |
139 | return NULL; | |
140 | } | |
141 | ||
142 | static void *rcu_update_perf_test(void *arg) | |
143 | { | |
144 | long long n_updates_local = 0; | |
145 | ||
146 | rcu_register_thread(); | |
147 | ||
148 | *(struct rcu_reader_data **)arg = &rcu_reader; | |
149 | atomic_inc(&nthreadsrunning); | |
150 | while (goflag == GOFLAG_INIT) { | |
151 | g_usleep(1000); | |
152 | } | |
153 | while (goflag == GOFLAG_RUN) { | |
154 | synchronize_rcu(); | |
155 | n_updates_local++; | |
156 | } | |
8a5956ad PB |
157 | qemu_mutex_lock(&counts_mutex); |
158 | n_updates += n_updates_local; | |
159 | qemu_mutex_unlock(&counts_mutex); | |
8fda74a5 PB |
160 | |
161 | rcu_unregister_thread(); | |
162 | return NULL; | |
163 | } | |
164 | ||
165 | static void perftestinit(void) | |
166 | { | |
167 | nthreadsrunning = 0; | |
168 | } | |
169 | ||
170 | static void perftestrun(int nthreads, int duration, int nreaders, int nupdaters) | |
171 | { | |
172 | while (atomic_read(&nthreadsrunning) < nthreads) { | |
173 | g_usleep(1000); | |
174 | } | |
175 | goflag = GOFLAG_RUN; | |
176 | g_usleep(duration * G_USEC_PER_SEC); | |
177 | goflag = GOFLAG_STOP; | |
178 | wait_all_threads(); | |
179 | printf("n_reads: %lld n_updates: %ld nreaders: %d nupdaters: %d duration: %d\n", | |
180 | n_reads, n_updates, nreaders, nupdaters, duration); | |
181 | printf("ns/read: %g ns/update: %g\n", | |
182 | ((duration * 1000*1000*1000.*(double)nreaders) / | |
183 | (double)n_reads), | |
184 | ((duration * 1000*1000*1000.*(double)nupdaters) / | |
185 | (double)n_updates)); | |
186 | exit(0); | |
187 | } | |
188 | ||
189 | static void perftest(int nreaders, int duration) | |
190 | { | |
191 | int i; | |
192 | ||
193 | perftestinit(); | |
194 | for (i = 0; i < nreaders; i++) { | |
195 | create_thread(rcu_read_perf_test); | |
196 | } | |
197 | create_thread(rcu_update_perf_test); | |
198 | perftestrun(i + 1, duration, nreaders, 1); | |
199 | } | |
200 | ||
201 | static void rperftest(int nreaders, int duration) | |
202 | { | |
203 | int i; | |
204 | ||
205 | perftestinit(); | |
206 | for (i = 0; i < nreaders; i++) { | |
207 | create_thread(rcu_read_perf_test); | |
208 | } | |
209 | perftestrun(i, duration, nreaders, 0); | |
210 | } | |
211 | ||
212 | static void uperftest(int nupdaters, int duration) | |
213 | { | |
214 | int i; | |
215 | ||
216 | perftestinit(); | |
217 | for (i = 0; i < nupdaters; i++) { | |
218 | create_thread(rcu_update_perf_test); | |
219 | } | |
220 | perftestrun(i, duration, 0, nupdaters); | |
221 | } | |
222 | ||
223 | /* | |
224 | * Stress test. | |
225 | */ | |
226 | ||
227 | #define RCU_STRESS_PIPE_LEN 10 | |
228 | ||
229 | struct rcu_stress { | |
230 | int pipe_count; | |
231 | int mbtest; | |
232 | }; | |
233 | ||
234 | struct rcu_stress rcu_stress_array[RCU_STRESS_PIPE_LEN] = { { 0 } }; | |
235 | struct rcu_stress *rcu_stress_current; | |
236 | int rcu_stress_idx; | |
237 | ||
238 | int n_mberror; | |
239 | long long rcu_stress_count[RCU_STRESS_PIPE_LEN + 1]; | |
240 | ||
241 | ||
242 | static void *rcu_read_stress_test(void *arg) | |
243 | { | |
244 | int i; | |
245 | int itercnt = 0; | |
246 | struct rcu_stress *p; | |
247 | int pc; | |
248 | long long n_reads_local = 0; | |
8a5956ad | 249 | long long rcu_stress_local[RCU_STRESS_PIPE_LEN + 1] = { 0 }; |
8fda74a5 PB |
250 | volatile int garbage = 0; |
251 | ||
252 | rcu_register_thread(); | |
253 | ||
254 | *(struct rcu_reader_data **)arg = &rcu_reader; | |
255 | while (goflag == GOFLAG_INIT) { | |
256 | g_usleep(1000); | |
257 | } | |
258 | while (goflag == GOFLAG_RUN) { | |
259 | rcu_read_lock(); | |
260 | p = atomic_rcu_read(&rcu_stress_current); | |
261 | if (p->mbtest == 0) { | |
262 | n_mberror++; | |
263 | } | |
d62cb4f2 | 264 | rcu_read_lock(); |
8fda74a5 PB |
265 | for (i = 0; i < 100; i++) { |
266 | garbage++; | |
267 | } | |
d62cb4f2 | 268 | rcu_read_unlock(); |
8fda74a5 PB |
269 | pc = p->pipe_count; |
270 | rcu_read_unlock(); | |
271 | if ((pc > RCU_STRESS_PIPE_LEN) || (pc < 0)) { | |
272 | pc = RCU_STRESS_PIPE_LEN; | |
273 | } | |
8a5956ad | 274 | rcu_stress_local[pc]++; |
8fda74a5 PB |
275 | n_reads_local++; |
276 | if ((++itercnt % 0x1000) == 0) { | |
277 | synchronize_rcu(); | |
278 | } | |
279 | } | |
8a5956ad PB |
280 | qemu_mutex_lock(&counts_mutex); |
281 | n_reads += n_reads_local; | |
282 | for (i = 0; i <= RCU_STRESS_PIPE_LEN; i++) { | |
283 | rcu_stress_count[i] += rcu_stress_local[i]; | |
284 | } | |
285 | qemu_mutex_unlock(&counts_mutex); | |
8fda74a5 PB |
286 | |
287 | rcu_unregister_thread(); | |
288 | return NULL; | |
289 | } | |
290 | ||
291 | static void *rcu_update_stress_test(void *arg) | |
292 | { | |
293 | int i; | |
294 | struct rcu_stress *p; | |
295 | ||
296 | rcu_register_thread(); | |
297 | ||
298 | *(struct rcu_reader_data **)arg = &rcu_reader; | |
299 | while (goflag == GOFLAG_INIT) { | |
300 | g_usleep(1000); | |
301 | } | |
302 | while (goflag == GOFLAG_RUN) { | |
303 | i = rcu_stress_idx + 1; | |
304 | if (i >= RCU_STRESS_PIPE_LEN) { | |
305 | i = 0; | |
306 | } | |
307 | p = &rcu_stress_array[i]; | |
308 | p->mbtest = 0; | |
309 | smp_mb(); | |
310 | p->pipe_count = 0; | |
311 | p->mbtest = 1; | |
312 | atomic_rcu_set(&rcu_stress_current, p); | |
313 | rcu_stress_idx = i; | |
314 | for (i = 0; i < RCU_STRESS_PIPE_LEN; i++) { | |
315 | if (i != rcu_stress_idx) { | |
316 | rcu_stress_array[i].pipe_count++; | |
317 | } | |
318 | } | |
319 | synchronize_rcu(); | |
320 | n_updates++; | |
321 | } | |
322 | ||
323 | rcu_unregister_thread(); | |
324 | return NULL; | |
325 | } | |
326 | ||
327 | static void *rcu_fake_update_stress_test(void *arg) | |
328 | { | |
329 | rcu_register_thread(); | |
330 | ||
331 | *(struct rcu_reader_data **)arg = &rcu_reader; | |
332 | while (goflag == GOFLAG_INIT) { | |
333 | g_usleep(1000); | |
334 | } | |
335 | while (goflag == GOFLAG_RUN) { | |
336 | synchronize_rcu(); | |
337 | g_usleep(1000); | |
338 | } | |
339 | ||
340 | rcu_unregister_thread(); | |
341 | return NULL; | |
342 | } | |
343 | ||
344 | static void stresstest(int nreaders, int duration) | |
345 | { | |
346 | int i; | |
347 | ||
348 | rcu_stress_current = &rcu_stress_array[0]; | |
349 | rcu_stress_current->pipe_count = 0; | |
350 | rcu_stress_current->mbtest = 1; | |
351 | for (i = 0; i < nreaders; i++) { | |
352 | create_thread(rcu_read_stress_test); | |
353 | } | |
354 | create_thread(rcu_update_stress_test); | |
355 | for (i = 0; i < 5; i++) { | |
356 | create_thread(rcu_fake_update_stress_test); | |
357 | } | |
358 | goflag = GOFLAG_RUN; | |
359 | g_usleep(duration * G_USEC_PER_SEC); | |
360 | goflag = GOFLAG_STOP; | |
361 | wait_all_threads(); | |
362 | printf("n_reads: %lld n_updates: %ld n_mberror: %d\n", | |
363 | n_reads, n_updates, n_mberror); | |
364 | printf("rcu_stress_count:"); | |
365 | for (i = 0; i <= RCU_STRESS_PIPE_LEN; i++) { | |
366 | printf(" %lld", rcu_stress_count[i]); | |
367 | } | |
368 | printf("\n"); | |
369 | exit(0); | |
370 | } | |
371 | ||
372 | /* GTest interface */ | |
373 | ||
374 | static void gtest_stress(int nreaders, int duration) | |
375 | { | |
376 | int i; | |
377 | ||
378 | rcu_stress_current = &rcu_stress_array[0]; | |
379 | rcu_stress_current->pipe_count = 0; | |
380 | rcu_stress_current->mbtest = 1; | |
381 | for (i = 0; i < nreaders; i++) { | |
382 | create_thread(rcu_read_stress_test); | |
383 | } | |
384 | create_thread(rcu_update_stress_test); | |
385 | for (i = 0; i < 5; i++) { | |
386 | create_thread(rcu_fake_update_stress_test); | |
387 | } | |
388 | goflag = GOFLAG_RUN; | |
389 | g_usleep(duration * G_USEC_PER_SEC); | |
390 | goflag = GOFLAG_STOP; | |
391 | wait_all_threads(); | |
392 | g_assert_cmpint(n_mberror, ==, 0); | |
393 | for (i = 2; i <= RCU_STRESS_PIPE_LEN; i++) { | |
394 | g_assert_cmpint(rcu_stress_count[i], ==, 0); | |
395 | } | |
396 | } | |
397 | ||
398 | static void gtest_stress_1_1(void) | |
399 | { | |
400 | gtest_stress(1, 1); | |
401 | } | |
402 | ||
403 | static void gtest_stress_10_1(void) | |
404 | { | |
405 | gtest_stress(10, 1); | |
406 | } | |
407 | ||
408 | static void gtest_stress_1_5(void) | |
409 | { | |
410 | gtest_stress(1, 5); | |
411 | } | |
412 | ||
413 | static void gtest_stress_10_5(void) | |
414 | { | |
415 | gtest_stress(10, 5); | |
416 | } | |
417 | ||
418 | /* | |
419 | * Mainprogram. | |
420 | */ | |
421 | ||
422 | static void usage(int argc, char *argv[]) | |
423 | { | |
424 | fprintf(stderr, "Usage: %s [nreaders [ perf | stress ] ]\n", argv[0]); | |
425 | exit(-1); | |
426 | } | |
427 | ||
428 | int main(int argc, char *argv[]) | |
429 | { | |
430 | int nreaders = 1; | |
431 | int duration = 1; | |
432 | ||
8a5956ad | 433 | qemu_mutex_init(&counts_mutex); |
8fda74a5 PB |
434 | if (argc >= 2 && argv[1][0] == '-') { |
435 | g_test_init(&argc, &argv, NULL); | |
436 | if (g_test_quick()) { | |
437 | g_test_add_func("/rcu/torture/1reader", gtest_stress_1_1); | |
438 | g_test_add_func("/rcu/torture/10readers", gtest_stress_10_1); | |
439 | } else { | |
440 | g_test_add_func("/rcu/torture/1reader", gtest_stress_1_5); | |
441 | g_test_add_func("/rcu/torture/10readers", gtest_stress_10_5); | |
442 | } | |
443 | return g_test_run(); | |
444 | } | |
445 | ||
446 | if (argc >= 2) { | |
447 | nreaders = strtoul(argv[1], NULL, 0); | |
448 | } | |
449 | if (argc > 3) { | |
450 | duration = strtoul(argv[3], NULL, 0); | |
451 | } | |
452 | if (argc < 3 || strcmp(argv[2], "stress") == 0) { | |
453 | stresstest(nreaders, duration); | |
454 | } else if (strcmp(argv[2], "rperf") == 0) { | |
455 | rperftest(nreaders, duration); | |
456 | } else if (strcmp(argv[2], "uperf") == 0) { | |
457 | uperftest(nreaders, duration); | |
458 | } else if (strcmp(argv[2], "perf") == 0) { | |
459 | perftest(nreaders, duration); | |
460 | } | |
461 | usage(argc, argv); | |
462 | return 0; | |
463 | } |