1 // SPDX-FileCopyrightText: 2008 Paul E. McKenney, IBM Corporation.
3 // SPDX-License-Identifier: GPL-2.0-or-later
6 * rcutorture.h: simple user-level performance/stress test of RCU.
9 * ./rcu <nreaders> rperf [ <cpustride> ]
10 * Run a read-side performance test with the specified
11 * number of readers spaced by <cpustride>.
12 * Thus "./rcu 16 rperf 2" would run 16 readers on even-numbered
14 * ./rcu <nupdaters> uperf [ <cpustride> ]
15 * Run an update-side performance test with the specified
16 * number of updaters and specified CPU spacing.
17 * ./rcu <nreaders> perf [ <cpustride> ]
18 * Run a combined read/update performance test with the specified
19 * number of readers and one updater and specified CPU spacing.
20 * The readers run on the low-numbered CPUs and the updater
21 * of the highest-numbered CPU.
23 * The above tests produce output as follows:
25 * n_reads: 46008000 n_updates: 146026 nreaders: 2 nupdaters: 1 duration: 1
26 * ns/read: 43.4707 ns/update: 6848.1
28 * The first line lists the total number of RCU reads and updates executed
29 * during the test, the number of reader threads, the number of updater
30 * threads, and the duration of the test in seconds. The second line
31 * lists the average duration of each type of operation in nanoseconds,
32 * or "nan" if the corresponding type of operation was not performed.
34 * ./rcu <nreaders> stress
35 * Run a stress test with the specified number of readers and
36 * one updater. None of the threads are affinitied to any
39 * This test produces output as follows:
41 * n_reads: 114633217 n_updates: 3903415 n_mberror: 0
42 * rcu_stress_count: 114618391 14826 0 0 0 0 0 0 0 0 0
44 * The first line lists the number of RCU read and update operations
45 * executed, followed by the number of memory-ordering violations
46 * (which will be zero in a correct RCU implementation). The second
47 * line lists the number of readers observing progressively more stale
48 * data. A correct RCU implementation will have all but the first two
61 DEFINE_PER_THREAD(long long, n_reads_pt
);
62 DEFINE_PER_THREAD(long long, n_updates_pt
);
71 WRITER_STATE_SYNC_RCU
,
72 WRITER_STATE_CALL_RCU
,
73 WRITER_STATE_POLL_RCU
,
76 static enum callrcu_type callrcu_type
= CALLRCU_GLOBAL
;
78 long long n_reads
= 0LL;
87 volatile int goflag
__attribute__((__aligned__(CAA_CACHE_LINE_SIZE
)))
90 #define RCU_READ_RUN 1000
93 #define RCU_READ_NESTABLE
95 #ifdef RCU_READ_NESTABLE
96 #define rcu_read_lock_nest() rcu_read_lock()
97 #define rcu_read_unlock_nest() rcu_read_unlock()
98 #else /* #ifdef RCU_READ_NESTABLE */
99 #define rcu_read_lock_nest()
100 #define rcu_read_unlock_nest()
101 #endif /* #else #ifdef RCU_READ_NESTABLE */
104 #define mark_rcu_quiescent_state rcu_quiescent_state
105 #define put_thread_offline rcu_thread_offline
106 #define put_thread_online rcu_thread_online
109 #ifndef mark_rcu_quiescent_state
110 #define mark_rcu_quiescent_state() do {} while (0)
111 #endif /* #ifdef mark_rcu_quiescent_state */
113 #ifndef put_thread_offline
114 #define put_thread_offline() do {} while (0)
115 #define put_thread_online() do {} while (0)
116 #define put_thread_online_delay() do {} while (0)
117 #else /* #ifndef put_thread_offline */
118 #define put_thread_online_delay() synchronize_rcu()
119 #endif /* #else #ifndef put_thread_offline */
126 void *rcu_read_perf_test(void *arg
)
130 long long n_reads_local
= 0;
132 rcu_register_thread();
134 uatomic_inc(&nthreadsrunning
);
135 put_thread_offline();
136 while (goflag
== GOFLAG_INIT
)
137 (void) poll(NULL
, 0, 1);
139 while (goflag
== GOFLAG_RUN
) {
140 for (i
= 0; i
< RCU_READ_RUN
; i
++) {
142 /* rcu_read_lock_nest(); */
143 /* rcu_read_unlock_nest(); */
146 n_reads_local
+= RCU_READ_RUN
;
147 mark_rcu_quiescent_state();
149 __get_thread_var(n_reads_pt
) += n_reads_local
;
150 put_thread_offline();
151 rcu_unregister_thread();
157 void *rcu_update_perf_test(void *arg
__attribute__((unused
)))
159 long long n_updates_local
= 0;
161 if (callrcu_type
== CALLRCU_PERTHREAD
) {
162 struct call_rcu_data
*crdp
;
164 crdp
= create_call_rcu_data(0, -1);
166 diag("Successfully 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
;
178 if (callrcu_type
== CALLRCU_PERTHREAD
) {
179 struct call_rcu_data
*crdp
;
181 crdp
= get_thread_call_rcu_data();
182 set_thread_call_rcu_data(NULL
);
183 call_rcu_data_free(crdp
);
189 void perftestinit(void)
191 init_per_thread(n_reads_pt
, 0LL);
192 init_per_thread(n_updates_pt
, 0LL);
193 uatomic_set(&nthreadsrunning
, 0);
197 int perftestrun(int nthreads
, int nreaders
, int nupdaters
)
203 while (uatomic_read(&nthreadsrunning
) < nthreads
)
204 (void) poll(NULL
, 0, 1);
209 goflag
= GOFLAG_STOP
;
213 n_reads
+= per_thread(n_reads_pt
, t
);
214 n_updates
+= per_thread(n_updates_pt
, t
);
216 diag("n_reads: %lld n_updates: %ld nreaders: %d nupdaters: %d duration: %d",
217 n_reads
, n_updates
, nreaders
, nupdaters
, duration
);
218 diag("ns/read: %g ns/update: %g",
219 ((duration
* 1000*1000*1000.*(double)nreaders
) /
221 ((duration
* 1000*1000*1000.*(double)nupdaters
) /
223 if (get_cpu_call_rcu_data(0)) {
224 diag("Deallocating per-CPU call_rcu threads.\n");
225 free_all_cpu_call_rcu_data();
231 int perftest(int nreaders
, int cpustride
)
237 for (i
= 0; i
< nreaders
; i
++) {
238 arg
= (long)(i
* cpustride
);
239 create_thread(rcu_read_perf_test
, (void *)arg
);
241 arg
= (long)(i
* cpustride
);
242 create_thread(rcu_update_perf_test
, (void *)arg
);
243 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);
262 int uperftest(int nupdaters
, int cpustride
)
268 init_per_thread(n_reads_pt
, 0LL);
269 for (i
= 0; i
< nupdaters
; i
++) {
270 arg
= (long)(i
* cpustride
);
271 create_thread(rcu_update_perf_test
, (void *)arg
);
273 return perftestrun(i
, 0, nupdaters
);
280 #define RCU_STRESS_PIPE_LEN 10
287 struct rcu_stress rcu_stress_array
[RCU_STRESS_PIPE_LEN
] = { { 0, 0 } };
288 struct rcu_stress
*rcu_stress_current
;
289 int rcu_stress_idx
= 0;
292 DEFINE_PER_THREAD(long long [RCU_STRESS_PIPE_LEN
+ 1], rcu_stress_count
);
297 void *rcu_read_stress_test(void *arg
__attribute__((unused
)))
301 struct rcu_stress
*p
;
304 rcu_register_thread();
305 put_thread_offline();
306 while (goflag
== GOFLAG_INIT
)
307 (void) poll(NULL
, 0, 1);
309 while (goflag
== GOFLAG_RUN
) {
311 p
= rcu_dereference(rcu_stress_current
);
314 rcu_read_lock_nest();
315 for (i
= 0; i
< 100; i
++)
317 rcu_read_unlock_nest();
320 if ((pc
> RCU_STRESS_PIPE_LEN
) || (pc
< 0))
321 pc
= RCU_STRESS_PIPE_LEN
;
322 __get_thread_var(rcu_stress_count
)[pc
]++;
323 __get_thread_var(n_reads_pt
)++;
324 mark_rcu_quiescent_state();
325 if ((++itercnt
% 0x1000) == 0) {
326 put_thread_offline();
327 put_thread_online_delay();
331 put_thread_offline();
332 rcu_unregister_thread();
337 static pthread_mutex_t call_rcu_test_mutex
= PTHREAD_MUTEX_INITIALIZER
;
338 static pthread_cond_t call_rcu_test_cond
= PTHREAD_COND_INITIALIZER
;
339 static bool call_rcu_wait
;
342 void rcu_update_stress_test_rcu(struct rcu_head
*head
__attribute__((unused
)))
346 ret
= pthread_mutex_lock(&call_rcu_test_mutex
);
349 diag("pthread_mutex_lock: %s",
353 ret
= pthread_cond_signal(&call_rcu_test_cond
);
356 diag("pthread_cond_signal: %s",
360 call_rcu_wait
= false;
361 ret
= pthread_mutex_unlock(&call_rcu_test_mutex
);
364 diag("pthread_mutex_unlock: %s",
371 void advance_writer_state(enum writer_state
*state
)
374 case WRITER_STATE_SYNC_RCU
:
375 *state
= WRITER_STATE_CALL_RCU
;
377 case WRITER_STATE_CALL_RCU
:
378 *state
= WRITER_STATE_POLL_RCU
;
380 case WRITER_STATE_POLL_RCU
:
381 *state
= WRITER_STATE_SYNC_RCU
;
387 void *rcu_update_stress_test(void *arg
__attribute__((unused
)))
390 struct rcu_stress
*p
;
392 enum writer_state writer_state
= WRITER_STATE_SYNC_RCU
;
394 rcu_register_thread();
396 /* Offline for poll. */
397 put_thread_offline();
398 while (goflag
== GOFLAG_INIT
)
399 (void) poll(NULL
, 0, 1);
402 while (goflag
== GOFLAG_RUN
) {
403 i
= rcu_stress_idx
+ 1;
404 if (i
>= RCU_STRESS_PIPE_LEN
)
406 p
= &rcu_stress_array
[i
];
411 rcu_assign_pointer(rcu_stress_current
, p
);
413 for (i
= 0; i
< RCU_STRESS_PIPE_LEN
; i
++)
414 if (i
!= rcu_stress_idx
)
415 rcu_stress_array
[i
].pipe_count
++;
416 switch (writer_state
) {
417 case WRITER_STATE_SYNC_RCU
:
420 case WRITER_STATE_CALL_RCU
:
424 ret
= pthread_mutex_lock(&call_rcu_test_mutex
);
427 diag("pthread_mutex_lock: %s",
431 call_rcu(&rh
, rcu_update_stress_test_rcu
);
434 * Our MacOS X test machine with the following
436 * 15.6.0 Darwin Kernel Version 15.6.0
437 * root:xnu-3248.60.10~1/RELEASE_X86_64
438 * appears to have issues with liburcu-signal
439 * signal being delivered on top of
440 * pthread_cond_wait. It seems to make the
441 * thread continue, and therefore corrupt the
442 * rcu_head. Work around this issue by
443 * unregistering the RCU read-side thread
444 * immediately after call_rcu (call_rcu needs
445 * us to be registered RCU readers).
447 call_rcu_wait
= true;
448 /* Offline for pthread_cond_wait. */
449 put_thread_offline();
451 ret
= pthread_cond_wait(&call_rcu_test_cond
,
452 &call_rcu_test_mutex
);
453 } while (call_rcu_wait
);
457 diag("pthread_cond_signal: %s",
461 ret
= pthread_mutex_unlock(&call_rcu_test_mutex
);
464 diag("pthread_mutex_unlock: %s",
470 case WRITER_STATE_POLL_RCU
:
472 struct urcu_gp_poll_state poll_state
;
474 poll_state
= start_poll_synchronize_rcu();
476 /* Offline for poll. */
477 put_thread_offline();
478 while (!poll_state_synchronize_rcu(poll_state
))
479 (void) poll(NULL
, 0, 1); /* Wait for 1ms */
485 advance_writer_state(&writer_state
);
488 rcu_unregister_thread();
494 void *rcu_fake_update_stress_test(void *arg
__attribute__((unused
)))
496 if (callrcu_type
== CALLRCU_PERTHREAD
) {
497 struct call_rcu_data
*crdp
;
499 crdp
= create_call_rcu_data(0, -1);
501 diag("Successfully using per-thread call_rcu() worker.");
502 set_thread_call_rcu_data(crdp
);
505 while (goflag
== GOFLAG_INIT
)
506 (void) poll(NULL
, 0, 1);
507 while (goflag
== GOFLAG_RUN
) {
509 (void) poll(NULL
, 0, 1);
511 if (callrcu_type
== CALLRCU_PERTHREAD
) {
512 struct call_rcu_data
*crdp
;
514 crdp
= get_thread_call_rcu_data();
515 set_thread_call_rcu_data(NULL
);
516 call_rcu_data_free(crdp
);
522 int stresstest(int nreaders
)
529 init_per_thread(n_reads_pt
, 0LL);
531 p
= &per_thread(rcu_stress_count
,t
)[0];
532 for (i
= 0; i
<= RCU_STRESS_PIPE_LEN
; i
++)
535 rcu_stress_current
= &rcu_stress_array
[0];
536 rcu_stress_current
->pipe_count
= 0;
537 rcu_stress_current
->mbtest
= 1;
538 for (i
= 0; i
< nreaders
; i
++)
539 create_thread(rcu_read_stress_test
, NULL
);
540 create_thread(rcu_update_stress_test
, NULL
);
541 for (i
= 0; i
< 5; i
++)
542 create_thread(rcu_fake_update_stress_test
, NULL
);
548 goflag
= GOFLAG_STOP
;
552 n_reads
+= per_thread(n_reads_pt
, t
);
553 diag("n_reads: %lld n_updates: %ld n_mberror: %d",
554 n_reads
, n_updates
, n_mberror
);
556 rdiag("rcu_stress_count:");
557 for (i
= 0; i
<= RCU_STRESS_PIPE_LEN
; i
++) {
560 sum
+= per_thread(rcu_stress_count
, t
)[i
];
565 if (get_cpu_call_rcu_data(0)) {
566 diag("Deallocating per-CPU call_rcu threads.");
567 free_all_cpu_call_rcu_data();
580 void usage(char *argv
[]) __attribute__((__noreturn__
));
583 void usage(char *argv
[])
585 diag("Usage: %s nreaders [ perf | rperf | uperf | stress ] [ stride ] [ callrcu_global | callrcu_percpu | callrcu_perthread ]\n", argv
[0]);
589 int main(int argc
, char *argv
[])
594 plan_tests(NR_TESTS
);
599 const char *callrcu_str
= argv
[4];;
601 if (strcmp(callrcu_str
, "callrcu_global") == 0) {
602 callrcu_type
= CALLRCU_GLOBAL
;
603 } else if (strcmp(callrcu_str
, "callrcu_percpu") == 0) {
604 callrcu_type
= CALLRCU_PERCPU
;
605 } else if (strcmp(callrcu_str
, "callrcu_perthread") == 0) {
606 callrcu_type
= CALLRCU_PERTHREAD
;
613 switch (callrcu_type
) {
615 diag("Using global per-process call_rcu thread.");
618 diag("Using per-CPU call_rcu threads.");
619 if (create_all_cpu_call_rcu_data(0))
620 diag("create_all_cpu_call_rcu_data: %s",
623 case CALLRCU_PERTHREAD
:
624 diag("Using per-thread call_rcu() worker.");
631 yield_active
|= YIELD_READ
;
632 yield_active
|= YIELD_WRITE
;
636 if (strcmp(argv
[1], "-h") == 0
637 || strcmp(argv
[1], "--help") == 0) {
641 nreaders
= strtoul(argv
[1], NULL
, 0);
643 ok(!perftest(nreaders
, cpustride
),
644 "perftest readers: %d, stride: %d",
645 nreaders
, cpustride
);
649 cpustride
= strtoul(argv
[3], NULL
, 0);
650 if (strcmp(argv
[2], "perf") == 0)
651 ok(!perftest(nreaders
, cpustride
),
652 "perftest readers: %d, stride: %d",
653 nreaders
, cpustride
);
654 else if (strcmp(argv
[2], "rperf") == 0)
655 ok(!rperftest(nreaders
, cpustride
),
656 "rperftest readers: %d, stride: %d",
657 nreaders
, cpustride
);
658 else if (strcmp(argv
[2], "uperf") == 0)
659 ok(!uperftest(nreaders
, cpustride
),
660 "uperftest readers: %d, stride: %d",
661 nreaders
, cpustride
);
662 else if (strcmp(argv
[2], "stress") == 0)
663 ok(!stresstest(nreaders
),
664 "stresstest readers: %d, stride: %d",
665 nreaders
, cpustride
);
672 return exit_status();