]> git.proxmox.com Git - mirror_qemu.git/blob - tests/rcutorture.c
Merge remote-tracking branch 'remotes/maxreitz/tags/pull-block-2017-04-03' into staging
[mirror_qemu.git] / tests / rcutorture.c
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 "qemu/osdep.h"
64 #include "qemu/atomic.h"
65 #include "qemu/rcu.h"
66 #include "qemu/thread.h"
67
68 long long n_reads = 0LL;
69 long n_updates = 0L;
70 int nthreadsrunning;
71
72 #define GOFLAG_INIT 0
73 #define GOFLAG_RUN 1
74 #define GOFLAG_STOP 2
75
76 static volatile int goflag = GOFLAG_INIT;
77
78 #define RCU_READ_RUN 1000
79
80 #define NR_THREADS 100
81 static QemuMutex counts_mutex;
82 static QemuThread threads[NR_THREADS];
83 static struct rcu_reader_data *data[NR_THREADS];
84 static int n_threads;
85
86 static void create_thread(void *(*func)(void *))
87 {
88 if (n_threads >= NR_THREADS) {
89 fprintf(stderr, "Thread limit of %d exceeded!\n", NR_THREADS);
90 exit(-1);
91 }
92 qemu_thread_create(&threads[n_threads], "test", func, &data[n_threads],
93 QEMU_THREAD_JOINABLE);
94 n_threads++;
95 }
96
97 static void wait_all_threads(void)
98 {
99 int i;
100
101 for (i = 0; i < n_threads; i++) {
102 qemu_thread_join(&threads[i]);
103 }
104 n_threads = 0;
105 }
106
107 /*
108 * Performance test.
109 */
110
111 static void *rcu_read_perf_test(void *arg)
112 {
113 int i;
114 long long n_reads_local = 0;
115
116 rcu_register_thread();
117
118 *(struct rcu_reader_data **)arg = &rcu_reader;
119 atomic_inc(&nthreadsrunning);
120 while (goflag == GOFLAG_INIT) {
121 g_usleep(1000);
122 }
123 while (goflag == GOFLAG_RUN) {
124 for (i = 0; i < RCU_READ_RUN; i++) {
125 rcu_read_lock();
126 rcu_read_unlock();
127 }
128 n_reads_local += RCU_READ_RUN;
129 }
130 qemu_mutex_lock(&counts_mutex);
131 n_reads += n_reads_local;
132 qemu_mutex_unlock(&counts_mutex);
133
134 rcu_unregister_thread();
135 return NULL;
136 }
137
138 static void *rcu_update_perf_test(void *arg)
139 {
140 long long n_updates_local = 0;
141
142 rcu_register_thread();
143
144 *(struct rcu_reader_data **)arg = &rcu_reader;
145 atomic_inc(&nthreadsrunning);
146 while (goflag == GOFLAG_INIT) {
147 g_usleep(1000);
148 }
149 while (goflag == GOFLAG_RUN) {
150 synchronize_rcu();
151 n_updates_local++;
152 }
153 qemu_mutex_lock(&counts_mutex);
154 n_updates += n_updates_local;
155 qemu_mutex_unlock(&counts_mutex);
156
157 rcu_unregister_thread();
158 return NULL;
159 }
160
161 static void perftestinit(void)
162 {
163 nthreadsrunning = 0;
164 }
165
166 static void perftestrun(int nthreads, int duration, int nreaders, int nupdaters)
167 {
168 while (atomic_read(&nthreadsrunning) < nthreads) {
169 g_usleep(1000);
170 }
171 goflag = GOFLAG_RUN;
172 g_usleep(duration * G_USEC_PER_SEC);
173 goflag = GOFLAG_STOP;
174 wait_all_threads();
175 printf("n_reads: %lld n_updates: %ld nreaders: %d nupdaters: %d duration: %d\n",
176 n_reads, n_updates, nreaders, nupdaters, duration);
177 printf("ns/read: %g ns/update: %g\n",
178 ((duration * 1000*1000*1000.*(double)nreaders) /
179 (double)n_reads),
180 ((duration * 1000*1000*1000.*(double)nupdaters) /
181 (double)n_updates));
182 exit(0);
183 }
184
185 static void perftest(int nreaders, int duration)
186 {
187 int i;
188
189 perftestinit();
190 for (i = 0; i < nreaders; i++) {
191 create_thread(rcu_read_perf_test);
192 }
193 create_thread(rcu_update_perf_test);
194 perftestrun(i + 1, duration, nreaders, 1);
195 }
196
197 static void rperftest(int nreaders, int duration)
198 {
199 int i;
200
201 perftestinit();
202 for (i = 0; i < nreaders; i++) {
203 create_thread(rcu_read_perf_test);
204 }
205 perftestrun(i, duration, nreaders, 0);
206 }
207
208 static void uperftest(int nupdaters, int duration)
209 {
210 int i;
211
212 perftestinit();
213 for (i = 0; i < nupdaters; i++) {
214 create_thread(rcu_update_perf_test);
215 }
216 perftestrun(i, duration, 0, nupdaters);
217 }
218
219 /*
220 * Stress test.
221 */
222
223 #define RCU_STRESS_PIPE_LEN 10
224
225 struct rcu_stress {
226 int pipe_count;
227 int mbtest;
228 };
229
230 struct rcu_stress rcu_stress_array[RCU_STRESS_PIPE_LEN] = { { 0 } };
231 struct rcu_stress *rcu_stress_current;
232 int rcu_stress_idx;
233
234 int n_mberror;
235 long long rcu_stress_count[RCU_STRESS_PIPE_LEN + 1];
236
237
238 static void *rcu_read_stress_test(void *arg)
239 {
240 int i;
241 int itercnt = 0;
242 struct rcu_stress *p;
243 int pc;
244 long long n_reads_local = 0;
245 long long rcu_stress_local[RCU_STRESS_PIPE_LEN + 1] = { 0 };
246 volatile int garbage = 0;
247
248 rcu_register_thread();
249
250 *(struct rcu_reader_data **)arg = &rcu_reader;
251 while (goflag == GOFLAG_INIT) {
252 g_usleep(1000);
253 }
254 while (goflag == GOFLAG_RUN) {
255 rcu_read_lock();
256 p = atomic_rcu_read(&rcu_stress_current);
257 if (p->mbtest == 0) {
258 n_mberror++;
259 }
260 rcu_read_lock();
261 for (i = 0; i < 100; i++) {
262 garbage++;
263 }
264 rcu_read_unlock();
265 pc = p->pipe_count;
266 rcu_read_unlock();
267 if ((pc > RCU_STRESS_PIPE_LEN) || (pc < 0)) {
268 pc = RCU_STRESS_PIPE_LEN;
269 }
270 rcu_stress_local[pc]++;
271 n_reads_local++;
272 if ((++itercnt % 0x1000) == 0) {
273 synchronize_rcu();
274 }
275 }
276 qemu_mutex_lock(&counts_mutex);
277 n_reads += n_reads_local;
278 for (i = 0; i <= RCU_STRESS_PIPE_LEN; i++) {
279 rcu_stress_count[i] += rcu_stress_local[i];
280 }
281 qemu_mutex_unlock(&counts_mutex);
282
283 rcu_unregister_thread();
284 return NULL;
285 }
286
287 static void *rcu_update_stress_test(void *arg)
288 {
289 int i;
290 struct rcu_stress *p;
291
292 rcu_register_thread();
293
294 *(struct rcu_reader_data **)arg = &rcu_reader;
295 while (goflag == GOFLAG_INIT) {
296 g_usleep(1000);
297 }
298 while (goflag == GOFLAG_RUN) {
299 i = rcu_stress_idx + 1;
300 if (i >= RCU_STRESS_PIPE_LEN) {
301 i = 0;
302 }
303 p = &rcu_stress_array[i];
304 p->mbtest = 0;
305 smp_mb();
306 p->pipe_count = 0;
307 p->mbtest = 1;
308 atomic_rcu_set(&rcu_stress_current, p);
309 rcu_stress_idx = i;
310 for (i = 0; i < RCU_STRESS_PIPE_LEN; i++) {
311 if (i != rcu_stress_idx) {
312 rcu_stress_array[i].pipe_count++;
313 }
314 }
315 synchronize_rcu();
316 n_updates++;
317 }
318
319 rcu_unregister_thread();
320 return NULL;
321 }
322
323 static void *rcu_fake_update_stress_test(void *arg)
324 {
325 rcu_register_thread();
326
327 *(struct rcu_reader_data **)arg = &rcu_reader;
328 while (goflag == GOFLAG_INIT) {
329 g_usleep(1000);
330 }
331 while (goflag == GOFLAG_RUN) {
332 synchronize_rcu();
333 g_usleep(1000);
334 }
335
336 rcu_unregister_thread();
337 return NULL;
338 }
339
340 static void stresstest(int nreaders, int duration)
341 {
342 int i;
343
344 rcu_stress_current = &rcu_stress_array[0];
345 rcu_stress_current->pipe_count = 0;
346 rcu_stress_current->mbtest = 1;
347 for (i = 0; i < nreaders; i++) {
348 create_thread(rcu_read_stress_test);
349 }
350 create_thread(rcu_update_stress_test);
351 for (i = 0; i < 5; i++) {
352 create_thread(rcu_fake_update_stress_test);
353 }
354 goflag = GOFLAG_RUN;
355 g_usleep(duration * G_USEC_PER_SEC);
356 goflag = GOFLAG_STOP;
357 wait_all_threads();
358 printf("n_reads: %lld n_updates: %ld n_mberror: %d\n",
359 n_reads, n_updates, n_mberror);
360 printf("rcu_stress_count:");
361 for (i = 0; i <= RCU_STRESS_PIPE_LEN; i++) {
362 printf(" %lld", rcu_stress_count[i]);
363 }
364 printf("\n");
365 exit(0);
366 }
367
368 /* GTest interface */
369
370 static void gtest_stress(int nreaders, int duration)
371 {
372 int i;
373
374 rcu_stress_current = &rcu_stress_array[0];
375 rcu_stress_current->pipe_count = 0;
376 rcu_stress_current->mbtest = 1;
377 for (i = 0; i < nreaders; i++) {
378 create_thread(rcu_read_stress_test);
379 }
380 create_thread(rcu_update_stress_test);
381 for (i = 0; i < 5; i++) {
382 create_thread(rcu_fake_update_stress_test);
383 }
384 goflag = GOFLAG_RUN;
385 g_usleep(duration * G_USEC_PER_SEC);
386 goflag = GOFLAG_STOP;
387 wait_all_threads();
388 g_assert_cmpint(n_mberror, ==, 0);
389 for (i = 2; i <= RCU_STRESS_PIPE_LEN; i++) {
390 g_assert_cmpint(rcu_stress_count[i], ==, 0);
391 }
392 }
393
394 static void gtest_stress_1_1(void)
395 {
396 gtest_stress(1, 1);
397 }
398
399 static void gtest_stress_10_1(void)
400 {
401 gtest_stress(10, 1);
402 }
403
404 static void gtest_stress_1_5(void)
405 {
406 gtest_stress(1, 5);
407 }
408
409 static void gtest_stress_10_5(void)
410 {
411 gtest_stress(10, 5);
412 }
413
414 /*
415 * Mainprogram.
416 */
417
418 static void usage(int argc, char *argv[])
419 {
420 fprintf(stderr, "Usage: %s [nreaders [ perf | stress ] ]\n", argv[0]);
421 exit(-1);
422 }
423
424 int main(int argc, char *argv[])
425 {
426 int nreaders = 1;
427 int duration = 1;
428
429 qemu_mutex_init(&counts_mutex);
430 if (argc >= 2 && argv[1][0] == '-') {
431 g_test_init(&argc, &argv, NULL);
432 if (g_test_quick()) {
433 g_test_add_func("/rcu/torture/1reader", gtest_stress_1_1);
434 g_test_add_func("/rcu/torture/10readers", gtest_stress_10_1);
435 } else {
436 g_test_add_func("/rcu/torture/1reader", gtest_stress_1_5);
437 g_test_add_func("/rcu/torture/10readers", gtest_stress_10_5);
438 }
439 return g_test_run();
440 }
441
442 if (argc >= 2) {
443 nreaders = strtoul(argv[1], NULL, 0);
444 }
445 if (argc > 3) {
446 duration = strtoul(argv[3], NULL, 0);
447 }
448 if (argc < 3 || strcmp(argv[2], "stress") == 0) {
449 stresstest(nreaders, duration);
450 } else if (strcmp(argv[2], "rperf") == 0) {
451 rperftest(nreaders, duration);
452 } else if (strcmp(argv[2], "uperf") == 0) {
453 uperftest(nreaders, duration);
454 } else if (strcmp(argv[2], "perf") == 0) {
455 perftest(nreaders, duration);
456 }
457 usage(argc, argv);
458 return 0;
459 }