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
);
82 static enum callrcu_type callrcu_type
= CALLRCU_GLOBAL
;
84 long long n_reads
= 0LL;
93 volatile int goflag
__attribute__((__aligned__(CAA_CACHE_LINE_SIZE
)))
96 #define RCU_READ_RUN 1000
99 #define RCU_READ_NESTABLE
101 #ifdef RCU_READ_NESTABLE
102 #define rcu_read_lock_nest() rcu_read_lock()
103 #define rcu_read_unlock_nest() rcu_read_unlock()
104 #else /* #ifdef RCU_READ_NESTABLE */
105 #define rcu_read_lock_nest()
106 #define rcu_read_unlock_nest()
107 #endif /* #else #ifdef RCU_READ_NESTABLE */
110 #define mark_rcu_quiescent_state rcu_quiescent_state
111 #define put_thread_offline rcu_thread_offline
112 #define put_thread_online rcu_thread_online
115 #ifndef mark_rcu_quiescent_state
116 #define mark_rcu_quiescent_state() do ; while (0)
117 #endif /* #ifdef mark_rcu_quiescent_state */
119 #ifndef put_thread_offline
120 #define put_thread_offline() do ; while (0)
121 #define put_thread_online() do ; while (0)
122 #define put_thread_online_delay() do ; while (0)
123 #else /* #ifndef put_thread_offline */
124 #define put_thread_online_delay() synchronize_rcu()
125 #endif /* #else #ifndef put_thread_offline */
131 void *rcu_read_perf_test(void *arg
)
135 long long n_reads_local
= 0;
137 rcu_register_thread();
139 uatomic_inc(&nthreadsrunning
);
140 put_thread_offline();
141 while (goflag
== GOFLAG_INIT
)
142 (void) poll(NULL
, 0, 1);
144 while (goflag
== GOFLAG_RUN
) {
145 for (i
= 0; i
< RCU_READ_RUN
; i
++) {
147 /* rcu_read_lock_nest(); */
148 /* rcu_read_unlock_nest(); */
151 n_reads_local
+= RCU_READ_RUN
;
152 mark_rcu_quiescent_state();
154 __get_thread_var(n_reads_pt
) += n_reads_local
;
155 put_thread_offline();
156 rcu_unregister_thread();
161 void *rcu_update_perf_test(void *arg
)
163 long long n_updates_local
= 0;
165 if (callrcu_type
== CALLRCU_PERTHREAD
) {
166 struct call_rcu_data
*crdp
;
168 crdp
= create_call_rcu_data(0, -1);
170 diag("Successfully using per-thread call_rcu() worker.");
171 set_thread_call_rcu_data(crdp
);
174 uatomic_inc(&nthreadsrunning
);
175 while (goflag
== GOFLAG_INIT
)
176 (void) poll(NULL
, 0, 1);
177 while (goflag
== GOFLAG_RUN
) {
181 __get_thread_var(n_updates_pt
) += n_updates_local
;
182 if (callrcu_type
== CALLRCU_PERTHREAD
) {
183 struct call_rcu_data
*crdp
;
185 crdp
= get_thread_call_rcu_data();
186 set_thread_call_rcu_data(NULL
);
187 call_rcu_data_free(crdp
);
192 void perftestinit(void)
194 init_per_thread(n_reads_pt
, 0LL);
195 init_per_thread(n_updates_pt
, 0LL);
196 uatomic_set(&nthreadsrunning
, 0);
199 int perftestrun(int nthreads
, int nreaders
, int nupdaters
)
205 while (uatomic_read(&nthreadsrunning
) < nthreads
)
206 (void) poll(NULL
, 0, 1);
211 goflag
= GOFLAG_STOP
;
215 n_reads
+= per_thread(n_reads_pt
, t
);
216 n_updates
+= per_thread(n_updates_pt
, t
);
218 diag("n_reads: %lld n_updates: %ld nreaders: %d nupdaters: %d duration: %d",
219 n_reads
, n_updates
, nreaders
, nupdaters
, duration
);
220 diag("ns/read: %g ns/update: %g",
221 ((duration
* 1000*1000*1000.*(double)nreaders
) /
223 ((duration
* 1000*1000*1000.*(double)nupdaters
) /
225 if (get_cpu_call_rcu_data(0)) {
226 diag("Deallocating per-CPU call_rcu threads.\n");
227 free_all_cpu_call_rcu_data();
232 int perftest(int nreaders
, int cpustride
)
238 for (i
= 0; i
< nreaders
; i
++) {
239 arg
= (long)(i
* cpustride
);
240 create_thread(rcu_read_perf_test
, (void *)arg
);
242 arg
= (long)(i
* cpustride
);
243 create_thread(rcu_update_perf_test
, (void *)arg
);
244 return perftestrun(i
+ 1, nreaders
, 1);
247 int rperftest(int nreaders
, int cpustride
)
253 init_per_thread(n_reads_pt
, 0LL);
254 for (i
= 0; i
< nreaders
; i
++) {
255 arg
= (long)(i
* cpustride
);
256 create_thread(rcu_read_perf_test
, (void *)arg
);
258 return perftestrun(i
, nreaders
, 0);
261 int uperftest(int nupdaters
, int cpustride
)
267 init_per_thread(n_reads_pt
, 0LL);
268 for (i
= 0; i
< nupdaters
; i
++) {
269 arg
= (long)(i
* cpustride
);
270 create_thread(rcu_update_perf_test
, (void *)arg
);
272 return perftestrun(i
, 0, nupdaters
);
279 #define RCU_STRESS_PIPE_LEN 10
286 struct rcu_stress rcu_stress_array
[RCU_STRESS_PIPE_LEN
] = { { 0 } };
287 struct rcu_stress
*rcu_stress_current
;
288 int rcu_stress_idx
= 0;
291 DEFINE_PER_THREAD(long long [RCU_STRESS_PIPE_LEN
+ 1], rcu_stress_count
);
295 void *rcu_read_stress_test(void *arg
)
299 struct rcu_stress
*p
;
302 rcu_register_thread();
303 put_thread_offline();
304 while (goflag
== GOFLAG_INIT
)
305 (void) poll(NULL
, 0, 1);
307 while (goflag
== GOFLAG_RUN
) {
309 p
= rcu_dereference(rcu_stress_current
);
312 rcu_read_lock_nest();
313 for (i
= 0; i
< 100; i
++)
315 rcu_read_unlock_nest();
318 if ((pc
> RCU_STRESS_PIPE_LEN
) || (pc
< 0))
319 pc
= RCU_STRESS_PIPE_LEN
;
320 __get_thread_var(rcu_stress_count
)[pc
]++;
321 __get_thread_var(n_reads_pt
)++;
322 mark_rcu_quiescent_state();
323 if ((++itercnt
% 0x1000) == 0) {
324 put_thread_offline();
325 put_thread_online_delay();
329 put_thread_offline();
330 rcu_unregister_thread();
335 static pthread_mutex_t call_rcu_test_mutex
= PTHREAD_MUTEX_INITIALIZER
;
336 static pthread_cond_t call_rcu_test_cond
= PTHREAD_COND_INITIALIZER
;
338 void rcu_update_stress_test_rcu(struct rcu_head
*head
)
342 ret
= pthread_mutex_lock(&call_rcu_test_mutex
);
345 diag("pthread_mutex_lock: %s",
349 ret
= pthread_cond_signal(&call_rcu_test_cond
);
352 diag("pthread_cond_signal: %s",
356 ret
= pthread_mutex_unlock(&call_rcu_test_mutex
);
359 diag("pthread_mutex_unlock: %s",
365 void *rcu_update_stress_test(void *arg
)
368 struct rcu_stress
*p
;
371 while (goflag
== GOFLAG_INIT
)
372 (void) poll(NULL
, 0, 1);
373 while (goflag
== GOFLAG_RUN
) {
374 i
= rcu_stress_idx
+ 1;
375 if (i
>= RCU_STRESS_PIPE_LEN
)
377 p
= &rcu_stress_array
[i
];
382 rcu_assign_pointer(rcu_stress_current
, p
);
384 for (i
= 0; i
< RCU_STRESS_PIPE_LEN
; i
++)
385 if (i
!= rcu_stress_idx
)
386 rcu_stress_array
[i
].pipe_count
++;
392 ret
= pthread_mutex_lock(&call_rcu_test_mutex
);
395 diag("pthread_mutex_lock: %s",
399 call_rcu(&rh
, rcu_update_stress_test_rcu
);
400 ret
= pthread_cond_wait(&call_rcu_test_cond
,
401 &call_rcu_test_mutex
);
404 diag("pthread_cond_signal: %s",
408 ret
= pthread_mutex_unlock(&call_rcu_test_mutex
);
411 diag("pthread_mutex_unlock: %s",
421 void *rcu_fake_update_stress_test(void *arg
)
423 if (callrcu_type
== CALLRCU_PERTHREAD
) {
424 struct call_rcu_data
*crdp
;
426 crdp
= create_call_rcu_data(0, -1);
428 diag("Successfully using per-thread call_rcu() worker.");
429 set_thread_call_rcu_data(crdp
);
432 while (goflag
== GOFLAG_INIT
)
433 (void) poll(NULL
, 0, 1);
434 while (goflag
== GOFLAG_RUN
) {
436 (void) poll(NULL
, 0, 1);
438 if (callrcu_type
== CALLRCU_PERTHREAD
) {
439 struct call_rcu_data
*crdp
;
441 crdp
= get_thread_call_rcu_data();
442 set_thread_call_rcu_data(NULL
);
443 call_rcu_data_free(crdp
);
448 int stresstest(int nreaders
)
455 init_per_thread(n_reads_pt
, 0LL);
457 p
= &per_thread(rcu_stress_count
,t
)[0];
458 for (i
= 0; i
<= RCU_STRESS_PIPE_LEN
; i
++)
461 rcu_stress_current
= &rcu_stress_array
[0];
462 rcu_stress_current
->pipe_count
= 0;
463 rcu_stress_current
->mbtest
= 1;
464 for (i
= 0; i
< nreaders
; i
++)
465 create_thread(rcu_read_stress_test
, NULL
);
466 create_thread(rcu_update_stress_test
, NULL
);
467 for (i
= 0; i
< 5; i
++)
468 create_thread(rcu_fake_update_stress_test
, NULL
);
474 goflag
= GOFLAG_STOP
;
478 n_reads
+= per_thread(n_reads_pt
, t
);
479 diag("n_reads: %lld n_updates: %ld n_mberror: %d",
480 n_reads
, n_updates
, n_mberror
);
482 rdiag("rcu_stress_count:");
483 for (i
= 0; i
<= RCU_STRESS_PIPE_LEN
; i
++) {
486 sum
+= per_thread(rcu_stress_count
, t
)[i
];
491 if (get_cpu_call_rcu_data(0)) {
492 diag("Deallocating per-CPU call_rcu threads.");
493 free_all_cpu_call_rcu_data();
505 void usage(int argc
, char *argv
[])
507 diag("Usage: %s nreaders [ perf | rperf | uperf | stress ] [ stride ] [ callrcu_global | callrcu_percpu | callrcu_perthread ]\n", argv
[0]);
511 int main(int argc
, char *argv
[])
516 plan_tests(NR_TESTS
);
521 const char *callrcu_str
= argv
[4];;
523 if (strcmp(callrcu_str
, "callrcu_global") == 0) {
524 callrcu_type
= CALLRCU_GLOBAL
;
525 } else if (strcmp(callrcu_str
, "callrcu_percpu") == 0) {
526 callrcu_type
= CALLRCU_PERCPU
;
527 } else if (strcmp(callrcu_str
, "callrcu_perthread") == 0) {
528 callrcu_type
= CALLRCU_PERTHREAD
;
535 switch (callrcu_type
) {
537 diag("Using global per-process call_rcu thread.");
540 diag("Using per-CPU call_rcu threads.");
541 if (create_all_cpu_call_rcu_data(0))
542 diag("create_all_cpu_call_rcu_data: %s",
545 case CALLRCU_PERTHREAD
:
546 diag("Using per-thread call_rcu() worker.");
553 yield_active
|= YIELD_READ
;
554 yield_active
|= YIELD_WRITE
;
558 if (strcmp(argv
[1], "-h") == 0
559 || strcmp(argv
[1], "--help") == 0) {
563 nreaders
= strtoul(argv
[1], NULL
, 0);
565 ok(!perftest(nreaders
, cpustride
),
566 "perftest readers: %d, stride: %d",
567 nreaders
, cpustride
);
571 cpustride
= strtoul(argv
[3], NULL
, 0);
572 if (strcmp(argv
[2], "perf") == 0)
573 ok(!perftest(nreaders
, cpustride
),
574 "perftest readers: %d, stride: %d",
575 nreaders
, cpustride
);
576 else if (strcmp(argv
[2], "rperf") == 0)
577 ok(!rperftest(nreaders
, cpustride
),
578 "rperftest readers: %d, stride: %d",
579 nreaders
, cpustride
);
580 else if (strcmp(argv
[2], "uperf") == 0)
581 ok(!uperftest(nreaders
, cpustride
),
582 "uperftest readers: %d, stride: %d",
583 nreaders
, cpustride
);
584 else if (strcmp(argv
[2], "stress") == 0)
585 ok(!stresstest(nreaders
),
586 "stresstest readers: %d, stride: %d",
587 nreaders
, cpustride
);
594 return exit_status();