2 * rcutorture.h: simple user-level performance/stress test of RCU.
5 * ./rcu <nreaders> rperf [ <cpustride> ]
6 * Run a read-side performance test with the specified
7 * number of readers spaced by <cpustride>.
8 * Thus "./rcu 16 rperf 2" would run 16 readers on even-numbered
10 * ./rcu <nupdaters> uperf [ <cpustride> ]
11 * Run an update-side performance test with the specified
12 * number of updaters and specified CPU spacing.
13 * ./rcu <nreaders> perf [ <cpustride> ]
14 * Run a combined read/update performance test with the specified
15 * number of readers and one updater and specified CPU spacing.
16 * The readers run on the low-numbered CPUs and the updater
17 * of the highest-numbered CPU.
19 * The above tests produce output as follows:
21 * n_reads: 46008000 n_updates: 146026 nreaders: 2 nupdaters: 1 duration: 1
22 * ns/read: 43.4707 ns/update: 6848.1
24 * The first line lists the total number of RCU reads and updates executed
25 * during the test, the number of reader threads, the number of updater
26 * threads, and the duration of the test in seconds. The second line
27 * lists the average duration of each type of operation in nanoseconds,
28 * or "nan" if the corresponding type of operation was not performed.
30 * ./rcu <nreaders> stress
31 * Run a stress test with the specified number of readers and
32 * one updater. None of the threads are affinitied to any
35 * This test produces output as follows:
37 * n_reads: 114633217 n_updates: 3903415 n_mberror: 0
38 * rcu_stress_count: 114618391 14826 0 0 0 0 0 0 0 0 0
40 * The first line lists the number of RCU read and update operations
41 * executed, followed by the number of memory-ordering violations
42 * (which will be zero in a correct RCU implementation). The second
43 * line lists the number of readers observing progressively more stale
44 * data. A correct RCU implementation will have all but the first two
47 * This program is free software; you can redistribute it and/or modify
48 * it under the terms of the GNU General Public License as published by
49 * the Free Software Foundation; either version 2 of the License, or
50 * (at your option) any later version.
52 * This program is distributed in the hope that it will be useful,
53 * but WITHOUT ANY WARRANTY; without even the implied warranty of
54 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
55 * GNU General Public License for more details.
57 * You should have received a copy of the GNU General Public License
58 * along with this program; if not, write to the Free Software
59 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
61 * Copyright (c) 2008 Paul E. McKenney, IBM Corporation.
73 DEFINE_PER_THREAD(long long, n_reads_pt
);
74 DEFINE_PER_THREAD(long long, n_updates_pt
);
76 long long n_reads
= 0LL;
85 volatile int goflag
__attribute__((__aligned__(CAA_CACHE_LINE_SIZE
)))
88 #define RCU_READ_RUN 1000
91 #define RCU_READ_NESTABLE
93 #ifdef RCU_READ_NESTABLE
94 #define rcu_read_lock_nest() rcu_read_lock()
95 #define rcu_read_unlock_nest() rcu_read_unlock()
96 #else /* #ifdef RCU_READ_NESTABLE */
97 #define rcu_read_lock_nest()
98 #define rcu_read_unlock_nest()
99 #endif /* #else #ifdef RCU_READ_NESTABLE */
102 #define mark_rcu_quiescent_state rcu_quiescent_state
103 #define put_thread_offline rcu_thread_offline
104 #define put_thread_online rcu_thread_online
107 #ifndef mark_rcu_quiescent_state
108 #define mark_rcu_quiescent_state() do ; while (0)
109 #endif /* #ifdef mark_rcu_quiescent_state */
111 #ifndef put_thread_offline
112 #define put_thread_offline() do ; while (0)
113 #define put_thread_online() do ; while (0)
114 #define put_thread_online_delay() do ; while (0)
115 #else /* #ifndef put_thread_offline */
116 #define put_thread_online_delay() synchronize_rcu()
117 #endif /* #else #ifndef put_thread_offline */
123 void *rcu_read_perf_test(void *arg
)
125 struct call_rcu_data
*crdp
;
128 long long n_reads_local
= 0;
130 rcu_register_thread();
132 uatomic_inc(&nthreadsrunning
);
133 put_thread_offline();
134 while (goflag
== GOFLAG_INIT
)
135 (void) poll(NULL
, 0, 1);
137 while (goflag
== GOFLAG_RUN
) {
138 for (i
= 0; i
< RCU_READ_RUN
; i
++) {
140 /* rcu_read_lock_nest(); */
141 /* rcu_read_unlock_nest(); */
144 n_reads_local
+= RCU_READ_RUN
;
145 mark_rcu_quiescent_state();
147 __get_thread_var(n_reads_pt
) += n_reads_local
;
148 put_thread_offline();
149 crdp
= get_thread_call_rcu_data();
150 set_thread_call_rcu_data(NULL
);
151 call_rcu_data_free(crdp
);
152 rcu_unregister_thread();
157 void *rcu_update_perf_test(void *arg
)
159 long long n_updates_local
= 0;
161 if ((random() & 0xf00) == 0) {
162 struct call_rcu_data
*crdp
;
164 crdp
= create_call_rcu_data(0, -1);
166 diag("Using per-thread call_rcu() worker.");
167 set_thread_call_rcu_data(crdp
);
170 uatomic_inc(&nthreadsrunning
);
171 while (goflag
== GOFLAG_INIT
)
172 (void) poll(NULL
, 0, 1);
173 while (goflag
== GOFLAG_RUN
) {
177 __get_thread_var(n_updates_pt
) += n_updates_local
;
181 void perftestinit(void)
183 init_per_thread(n_reads_pt
, 0LL);
184 init_per_thread(n_updates_pt
, 0LL);
185 uatomic_set(&nthreadsrunning
, 0);
188 int perftestrun(int nthreads
, int nreaders
, int nupdaters
)
194 while (uatomic_read(&nthreadsrunning
) < nthreads
)
195 (void) poll(NULL
, 0, 1);
200 goflag
= GOFLAG_STOP
;
204 n_reads
+= per_thread(n_reads_pt
, t
);
205 n_updates
+= per_thread(n_updates_pt
, t
);
207 diag("n_reads: %lld n_updates: %ld nreaders: %d nupdaters: %d duration: %d",
208 n_reads
, n_updates
, nreaders
, nupdaters
, duration
);
209 diag("ns/read: %g ns/update: %g",
210 ((duration
* 1000*1000*1000.*(double)nreaders
) /
212 ((duration
* 1000*1000*1000.*(double)nupdaters
) /
214 if (get_cpu_call_rcu_data(0)) {
215 diag("Deallocating per-CPU call_rcu threads.\n");
216 free_all_cpu_call_rcu_data();
221 int perftest(int nreaders
, int cpustride
)
227 for (i
= 0; i
< nreaders
; i
++) {
228 arg
= (long)(i
* cpustride
);
229 create_thread(rcu_read_perf_test
, (void *)arg
);
231 arg
= (long)(i
* cpustride
);
232 create_thread(rcu_update_perf_test
, (void *)arg
);
233 return perftestrun(i
+ 1, nreaders
, 1);
236 int rperftest(int nreaders
, int cpustride
)
242 init_per_thread(n_reads_pt
, 0LL);
243 for (i
= 0; i
< nreaders
; i
++) {
244 arg
= (long)(i
* cpustride
);
245 create_thread(rcu_read_perf_test
, (void *)arg
);
247 return perftestrun(i
, nreaders
, 0);
250 int uperftest(int nupdaters
, int cpustride
)
256 init_per_thread(n_reads_pt
, 0LL);
257 for (i
= 0; i
< nupdaters
; i
++) {
258 arg
= (long)(i
* cpustride
);
259 create_thread(rcu_update_perf_test
, (void *)arg
);
261 return perftestrun(i
, 0, nupdaters
);
268 #define RCU_STRESS_PIPE_LEN 10
275 struct rcu_stress rcu_stress_array
[RCU_STRESS_PIPE_LEN
] = { { 0 } };
276 struct rcu_stress
*rcu_stress_current
;
277 int rcu_stress_idx
= 0;
280 DEFINE_PER_THREAD(long long [RCU_STRESS_PIPE_LEN
+ 1], rcu_stress_count
);
284 void *rcu_read_stress_test(void *arg
)
288 struct rcu_stress
*p
;
291 rcu_register_thread();
292 put_thread_offline();
293 while (goflag
== GOFLAG_INIT
)
294 (void) poll(NULL
, 0, 1);
296 while (goflag
== GOFLAG_RUN
) {
298 p
= rcu_dereference(rcu_stress_current
);
301 rcu_read_lock_nest();
302 for (i
= 0; i
< 100; i
++)
304 rcu_read_unlock_nest();
307 if ((pc
> RCU_STRESS_PIPE_LEN
) || (pc
< 0))
308 pc
= RCU_STRESS_PIPE_LEN
;
309 __get_thread_var(rcu_stress_count
)[pc
]++;
310 __get_thread_var(n_reads_pt
)++;
311 mark_rcu_quiescent_state();
312 if ((++itercnt
% 0x1000) == 0) {
313 put_thread_offline();
314 put_thread_online_delay();
318 put_thread_offline();
319 rcu_unregister_thread();
324 static pthread_mutex_t call_rcu_test_mutex
= PTHREAD_MUTEX_INITIALIZER
;
325 static pthread_cond_t call_rcu_test_cond
= PTHREAD_COND_INITIALIZER
;
327 void rcu_update_stress_test_rcu(struct rcu_head
*head
)
331 ret
= pthread_mutex_lock(&call_rcu_test_mutex
);
334 diag("pthread_mutex_lock: %s",
338 ret
= pthread_cond_signal(&call_rcu_test_cond
);
341 diag("pthread_cond_signal: %s",
345 ret
= pthread_mutex_unlock(&call_rcu_test_mutex
);
348 diag("pthread_mutex_unlock: %s",
354 void *rcu_update_stress_test(void *arg
)
357 struct rcu_stress
*p
;
360 while (goflag
== GOFLAG_INIT
)
361 (void) poll(NULL
, 0, 1);
362 while (goflag
== GOFLAG_RUN
) {
363 i
= rcu_stress_idx
+ 1;
364 if (i
>= RCU_STRESS_PIPE_LEN
)
366 p
= &rcu_stress_array
[i
];
371 rcu_assign_pointer(rcu_stress_current
, p
);
373 for (i
= 0; i
< RCU_STRESS_PIPE_LEN
; i
++)
374 if (i
!= rcu_stress_idx
)
375 rcu_stress_array
[i
].pipe_count
++;
381 ret
= pthread_mutex_lock(&call_rcu_test_mutex
);
384 diag("pthread_mutex_lock: %s",
388 call_rcu(&rh
, rcu_update_stress_test_rcu
);
389 ret
= pthread_cond_wait(&call_rcu_test_cond
,
390 &call_rcu_test_mutex
);
393 diag("pthread_cond_signal: %s",
397 ret
= pthread_mutex_unlock(&call_rcu_test_mutex
);
400 diag("pthread_mutex_unlock: %s",
410 void *rcu_fake_update_stress_test(void *arg
)
412 if ((random() & 0xf00) == 0) {
413 struct call_rcu_data
*crdp
;
415 crdp
= create_call_rcu_data(0, -1);
417 diag("Using per-thread call_rcu() worker.");
418 set_thread_call_rcu_data(crdp
);
421 while (goflag
== GOFLAG_INIT
)
422 (void) poll(NULL
, 0, 1);
423 while (goflag
== GOFLAG_RUN
) {
425 (void) poll(NULL
, 0, 1);
430 int stresstest(int nreaders
)
437 init_per_thread(n_reads_pt
, 0LL);
439 p
= &per_thread(rcu_stress_count
,t
)[0];
440 for (i
= 0; i
<= RCU_STRESS_PIPE_LEN
; i
++)
443 rcu_stress_current
= &rcu_stress_array
[0];
444 rcu_stress_current
->pipe_count
= 0;
445 rcu_stress_current
->mbtest
= 1;
446 for (i
= 0; i
< nreaders
; i
++)
447 create_thread(rcu_read_stress_test
, NULL
);
448 create_thread(rcu_update_stress_test
, NULL
);
449 for (i
= 0; i
< 5; i
++)
450 create_thread(rcu_fake_update_stress_test
, NULL
);
456 goflag
= GOFLAG_STOP
;
460 n_reads
+= per_thread(n_reads_pt
, t
);
461 diag("n_reads: %lld n_updates: %ld n_mberror: %d",
462 n_reads
, n_updates
, n_mberror
);
464 rdiag("rcu_stress_count:");
465 for (i
= 0; i
<= RCU_STRESS_PIPE_LEN
; i
++) {
468 sum
+= per_thread(rcu_stress_count
, t
)[i
];
473 if (get_cpu_call_rcu_data(0)) {
474 diag("Deallocating per-CPU call_rcu threads.");
475 free_all_cpu_call_rcu_data();
487 void usage(int argc
, char *argv
[])
489 diag("Usage: %s [nreaders [ perf | rperf | uperf | stress ] ]\n", argv
[0]);
493 int main(int argc
, char *argv
[])
498 plan_tests(NR_TESTS
);
503 if (random() & 0x100) {
504 diag("Allocating per-CPU call_rcu threads.");
505 if (create_all_cpu_call_rcu_data(0))
506 diag("create_all_cpu_call_rcu_data: %s",
511 yield_active
|= YIELD_READ
;
512 yield_active
|= YIELD_WRITE
;
516 nreaders
= strtoul(argv
[1], NULL
, 0);
518 ok(!perftest(nreaders
, cpustride
),
519 "perftest readers: %d, stride: %d",
520 nreaders
, cpustride
);
524 cpustride
= strtoul(argv
[3], NULL
, 0);
525 if (strcmp(argv
[2], "perf") == 0)
526 ok(!perftest(nreaders
, cpustride
),
527 "perftest readers: %d, stride: %d",
528 nreaders
, cpustride
);
529 else if (strcmp(argv
[2], "rperf") == 0)
530 ok(!rperftest(nreaders
, cpustride
),
531 "rperftest readers: %d, stride: %d",
532 nreaders
, cpustride
);
533 else if (strcmp(argv
[2], "uperf") == 0)
534 ok(!uperftest(nreaders
, cpustride
),
535 "uperftest readers: %d, stride: %d",
536 nreaders
, cpustride
);
537 else if (strcmp(argv
[2], "stress") == 0)
538 ok(!stresstest(nreaders
),
539 "stresstest readers: %d, stride: %d",
540 nreaders
, cpustride
);
544 ok(!perftest(nreaders
, cpustride
),
545 "perftest readers: %d, stride: %d",
546 nreaders
, cpustride
);
549 return exit_status();