2 * rcutorture.c: simple user-level performance/stress test of RCU.
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.
15 * The above tests produce output as follows:
17 * n_reads: 46008000 n_updates: 146026 nreaders: 2 nupdaters: 1 duration: 1
18 * ns/read: 43.4707 ns/update: 6848.1
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.
26 * ./rcu <nreaders> stress [ <seconds> ]
27 * Run a stress test with the specified number of readers and
30 * This test produces output as follows:
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
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
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.
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.
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.
56 * Copyright (c) 2008 Paul E. McKenney, IBM Corporation.
67 #include "qemu/atomic.h"
69 #include "qemu/compiler.h"
70 #include "qemu/thread.h"
72 long long n_reads
= 0LL;
80 static volatile int goflag
= GOFLAG_INIT
;
82 #define RCU_READ_RUN 1000
84 #define NR_THREADS 100
85 static QemuThread threads
[NR_THREADS
];
86 static struct rcu_reader_data
*data
[NR_THREADS
];
89 static void create_thread(void *(*func
)(void *))
91 if (n_threads
>= NR_THREADS
) {
92 fprintf(stderr
, "Thread limit of %d exceeded!\n", NR_THREADS
);
95 qemu_thread_create(&threads
[n_threads
], "test", func
, &data
[n_threads
],
96 QEMU_THREAD_JOINABLE
);
100 static void wait_all_threads(void)
104 for (i
= 0; i
< n_threads
; i
++) {
105 qemu_thread_join(&threads
[i
]);
114 static void *rcu_read_perf_test(void *arg
)
117 long long n_reads_local
= 0;
119 rcu_register_thread();
121 *(struct rcu_reader_data
**)arg
= &rcu_reader
;
122 atomic_inc(&nthreadsrunning
);
123 while (goflag
== GOFLAG_INIT
) {
126 while (goflag
== GOFLAG_RUN
) {
127 for (i
= 0; i
< RCU_READ_RUN
; i
++) {
131 n_reads_local
+= RCU_READ_RUN
;
133 atomic_add(&n_reads
, n_reads_local
);
135 rcu_unregister_thread();
139 static void *rcu_update_perf_test(void *arg
)
141 long long n_updates_local
= 0;
143 rcu_register_thread();
145 *(struct rcu_reader_data
**)arg
= &rcu_reader
;
146 atomic_inc(&nthreadsrunning
);
147 while (goflag
== GOFLAG_INIT
) {
150 while (goflag
== GOFLAG_RUN
) {
154 atomic_add(&n_updates
, n_updates_local
);
156 rcu_unregister_thread();
160 static void perftestinit(void)
165 static void perftestrun(int nthreads
, int duration
, int nreaders
, int nupdaters
)
167 while (atomic_read(&nthreadsrunning
) < nthreads
) {
171 g_usleep(duration
* G_USEC_PER_SEC
);
172 goflag
= GOFLAG_STOP
;
174 printf("n_reads: %lld n_updates: %ld nreaders: %d nupdaters: %d duration: %d\n",
175 n_reads
, n_updates
, nreaders
, nupdaters
, duration
);
176 printf("ns/read: %g ns/update: %g\n",
177 ((duration
* 1000*1000*1000.*(double)nreaders
) /
179 ((duration
* 1000*1000*1000.*(double)nupdaters
) /
184 static void perftest(int nreaders
, int duration
)
189 for (i
= 0; i
< nreaders
; i
++) {
190 create_thread(rcu_read_perf_test
);
192 create_thread(rcu_update_perf_test
);
193 perftestrun(i
+ 1, duration
, nreaders
, 1);
196 static void rperftest(int nreaders
, int duration
)
201 for (i
= 0; i
< nreaders
; i
++) {
202 create_thread(rcu_read_perf_test
);
204 perftestrun(i
, duration
, nreaders
, 0);
207 static void uperftest(int nupdaters
, int duration
)
212 for (i
= 0; i
< nupdaters
; i
++) {
213 create_thread(rcu_update_perf_test
);
215 perftestrun(i
, duration
, 0, nupdaters
);
222 #define RCU_STRESS_PIPE_LEN 10
229 struct rcu_stress rcu_stress_array
[RCU_STRESS_PIPE_LEN
] = { { 0 } };
230 struct rcu_stress
*rcu_stress_current
;
234 long long rcu_stress_count
[RCU_STRESS_PIPE_LEN
+ 1];
237 static void *rcu_read_stress_test(void *arg
)
241 struct rcu_stress
*p
;
243 long long n_reads_local
= 0;
244 volatile int garbage
= 0;
246 rcu_register_thread();
248 *(struct rcu_reader_data
**)arg
= &rcu_reader
;
249 while (goflag
== GOFLAG_INIT
) {
252 while (goflag
== GOFLAG_RUN
) {
254 p
= atomic_rcu_read(&rcu_stress_current
);
255 if (p
->mbtest
== 0) {
258 for (i
= 0; i
< 100; i
++) {
263 if ((pc
> RCU_STRESS_PIPE_LEN
) || (pc
< 0)) {
264 pc
= RCU_STRESS_PIPE_LEN
;
266 atomic_inc(&rcu_stress_count
[pc
]);
268 if ((++itercnt
% 0x1000) == 0) {
272 atomic_add(&n_reads
, n_reads_local
);
274 rcu_unregister_thread();
278 static void *rcu_update_stress_test(void *arg
)
281 struct rcu_stress
*p
;
283 rcu_register_thread();
285 *(struct rcu_reader_data
**)arg
= &rcu_reader
;
286 while (goflag
== GOFLAG_INIT
) {
289 while (goflag
== GOFLAG_RUN
) {
290 i
= rcu_stress_idx
+ 1;
291 if (i
>= RCU_STRESS_PIPE_LEN
) {
294 p
= &rcu_stress_array
[i
];
299 atomic_rcu_set(&rcu_stress_current
, p
);
301 for (i
= 0; i
< RCU_STRESS_PIPE_LEN
; i
++) {
302 if (i
!= rcu_stress_idx
) {
303 rcu_stress_array
[i
].pipe_count
++;
310 rcu_unregister_thread();
314 static void *rcu_fake_update_stress_test(void *arg
)
316 rcu_register_thread();
318 *(struct rcu_reader_data
**)arg
= &rcu_reader
;
319 while (goflag
== GOFLAG_INIT
) {
322 while (goflag
== GOFLAG_RUN
) {
327 rcu_unregister_thread();
331 static void stresstest(int nreaders
, int duration
)
335 rcu_stress_current
= &rcu_stress_array
[0];
336 rcu_stress_current
->pipe_count
= 0;
337 rcu_stress_current
->mbtest
= 1;
338 for (i
= 0; i
< nreaders
; i
++) {
339 create_thread(rcu_read_stress_test
);
341 create_thread(rcu_update_stress_test
);
342 for (i
= 0; i
< 5; i
++) {
343 create_thread(rcu_fake_update_stress_test
);
346 g_usleep(duration
* G_USEC_PER_SEC
);
347 goflag
= GOFLAG_STOP
;
349 printf("n_reads: %lld n_updates: %ld n_mberror: %d\n",
350 n_reads
, n_updates
, n_mberror
);
351 printf("rcu_stress_count:");
352 for (i
= 0; i
<= RCU_STRESS_PIPE_LEN
; i
++) {
353 printf(" %lld", rcu_stress_count
[i
]);
359 /* GTest interface */
361 static void gtest_stress(int nreaders
, int duration
)
365 rcu_stress_current
= &rcu_stress_array
[0];
366 rcu_stress_current
->pipe_count
= 0;
367 rcu_stress_current
->mbtest
= 1;
368 for (i
= 0; i
< nreaders
; i
++) {
369 create_thread(rcu_read_stress_test
);
371 create_thread(rcu_update_stress_test
);
372 for (i
= 0; i
< 5; i
++) {
373 create_thread(rcu_fake_update_stress_test
);
376 g_usleep(duration
* G_USEC_PER_SEC
);
377 goflag
= GOFLAG_STOP
;
379 g_assert_cmpint(n_mberror
, ==, 0);
380 for (i
= 2; i
<= RCU_STRESS_PIPE_LEN
; i
++) {
381 g_assert_cmpint(rcu_stress_count
[i
], ==, 0);
385 static void gtest_stress_1_1(void)
390 static void gtest_stress_10_1(void)
395 static void gtest_stress_1_5(void)
400 static void gtest_stress_10_5(void)
409 static void usage(int argc
, char *argv
[])
411 fprintf(stderr
, "Usage: %s [nreaders [ perf | stress ] ]\n", argv
[0]);
415 int main(int argc
, char *argv
[])
420 if (argc
>= 2 && argv
[1][0] == '-') {
421 g_test_init(&argc
, &argv
, NULL
);
422 if (g_test_quick()) {
423 g_test_add_func("/rcu/torture/1reader", gtest_stress_1_1
);
424 g_test_add_func("/rcu/torture/10readers", gtest_stress_10_1
);
426 g_test_add_func("/rcu/torture/1reader", gtest_stress_1_5
);
427 g_test_add_func("/rcu/torture/10readers", gtest_stress_10_5
);
433 nreaders
= strtoul(argv
[1], NULL
, 0);
436 duration
= strtoul(argv
[3], NULL
, 0);
438 if (argc
< 3 || strcmp(argv
[2], "stress") == 0) {
439 stresstest(nreaders
, duration
);
440 } else if (strcmp(argv
[2], "rperf") == 0) {
441 rperftest(nreaders
, duration
);
442 } else if (strcmp(argv
[2], "uperf") == 0) {
443 uperftest(nreaders
, duration
);
444 } else if (strcmp(argv
[2], "perf") == 0) {
445 perftest(nreaders
, duration
);