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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
61 * Copyright (c) 2008 Paul E. McKenney, IBM Corporation.
68 DEFINE_PER_THREAD(long long, n_reads_pt
);
69 DEFINE_PER_THREAD(long long, n_updates_pt
);
71 long long n_reads
= 0LL;
80 int goflag
__attribute__((__aligned__(CACHE_LINE_SIZE
))) = GOFLAG_INIT
;
82 #define RCU_READ_RUN 1000
85 #define RCU_READ_NESTABLE
87 #ifdef RCU_READ_NESTABLE
88 #define rcu_read_lock_nest() rcu_read_lock()
89 #define rcu_read_unlock_nest() rcu_read_unlock()
90 #else /* #ifdef RCU_READ_NESTABLE */
91 #define rcu_read_lock_nest()
92 #define rcu_read_unlock_nest()
93 #endif /* #else #ifdef RCU_READ_NESTABLE */
96 #define mark_rcu_quiescent_state rcu_quiescent_state
97 #define put_thread_offline rcu_thread_offline
98 #define put_thread_online rcu_thread_online
101 #ifndef mark_rcu_quiescent_state
102 #define mark_rcu_quiescent_state() do ; while (0)
103 #endif /* #ifdef mark_rcu_quiescent_state */
105 #ifndef put_thread_offline
106 #define put_thread_offline() do ; while (0)
107 #define put_thread_online() do ; while (0)
108 #define put_thread_online_delay() do ; while (0)
109 #else /* #ifndef put_thread_offline */
110 #define put_thread_online_delay() synchronize_rcu()
111 #endif /* #else #ifndef put_thread_offline */
117 void *rcu_read_perf_test(void *arg
)
121 long long n_reads_local
= 0;
123 rcu_register_thread();
125 uatomic_inc(&nthreadsrunning
);
126 while (goflag
== GOFLAG_INIT
)
128 mark_rcu_quiescent_state();
129 while (goflag
== GOFLAG_RUN
) {
130 for (i
= 0; i
< RCU_READ_RUN
; i
++) {
132 /* rcu_read_lock_nest(); */
133 /* rcu_read_unlock_nest(); */
136 n_reads_local
+= RCU_READ_RUN
;
137 mark_rcu_quiescent_state();
139 __get_thread_var(n_reads_pt
) += n_reads_local
;
140 put_thread_offline();
141 rcu_unregister_thread();
146 void *rcu_update_perf_test(void *arg
)
148 long long n_updates_local
= 0;
150 uatomic_inc(&nthreadsrunning
);
151 while (goflag
== GOFLAG_INIT
)
153 while (goflag
== GOFLAG_RUN
) {
157 __get_thread_var(n_updates_pt
) += n_updates_local
;
161 void perftestinit(void)
163 init_per_thread(n_reads_pt
, 0LL);
164 init_per_thread(n_updates_pt
, 0LL);
165 uatomic_set(&nthreadsrunning
, 0);
168 void perftestrun(int nthreads
, int nreaders
, int nupdaters
)
174 while (uatomic_read(&nthreadsrunning
) < nthreads
)
180 goflag
= GOFLAG_STOP
;
184 n_reads
+= per_thread(n_reads_pt
, t
);
185 n_updates
+= per_thread(n_updates_pt
, t
);
187 printf("n_reads: %lld n_updates: %ld nreaders: %d nupdaters: %d duration: %d\n",
188 n_reads
, n_updates
, nreaders
, nupdaters
, duration
);
189 printf("ns/read: %g ns/update: %g\n",
190 ((duration
* 1000*1000*1000.*(double)nreaders
) /
192 ((duration
* 1000*1000*1000.*(double)nupdaters
) /
197 void perftest(int nreaders
, int cpustride
)
203 for (i
= 0; i
< nreaders
; i
++) {
204 arg
= (long)(i
* cpustride
);
205 create_thread(rcu_read_perf_test
, (void *)arg
);
207 arg
= (long)(i
* cpustride
);
208 create_thread(rcu_update_perf_test
, (void *)arg
);
209 perftestrun(i
+ 1, nreaders
, 1);
212 void rperftest(int nreaders
, int cpustride
)
218 init_per_thread(n_reads_pt
, 0LL);
219 for (i
= 0; i
< nreaders
; i
++) {
220 arg
= (long)(i
* cpustride
);
221 create_thread(rcu_read_perf_test
, (void *)arg
);
223 perftestrun(i
, nreaders
, 0);
226 void uperftest(int nupdaters
, int cpustride
)
232 init_per_thread(n_reads_pt
, 0LL);
233 for (i
= 0; i
< nupdaters
; i
++) {
234 arg
= (long)(i
* cpustride
);
235 create_thread(rcu_update_perf_test
, (void *)arg
);
237 perftestrun(i
, 0, nupdaters
);
244 #define RCU_STRESS_PIPE_LEN 10
251 struct rcu_stress rcu_stress_array
[RCU_STRESS_PIPE_LEN
] = { { 0 } };
252 struct rcu_stress
*rcu_stress_current
;
253 int rcu_stress_idx
= 0;
256 DEFINE_PER_THREAD(long long [RCU_STRESS_PIPE_LEN
+ 1], rcu_stress_count
);
260 void *rcu_read_stress_test(void *arg
)
264 struct rcu_stress
*p
;
267 rcu_register_thread();
268 while (goflag
== GOFLAG_INIT
)
270 mark_rcu_quiescent_state();
271 while (goflag
== GOFLAG_RUN
) {
273 p
= rcu_dereference(rcu_stress_current
);
276 rcu_read_lock_nest();
277 for (i
= 0; i
< 100; i
++)
279 rcu_read_unlock_nest();
282 if ((pc
> RCU_STRESS_PIPE_LEN
) || (pc
< 0))
283 pc
= RCU_STRESS_PIPE_LEN
;
284 __get_thread_var(rcu_stress_count
)[pc
]++;
285 __get_thread_var(n_reads_pt
)++;
286 mark_rcu_quiescent_state();
287 if ((++itercnt
% 0x1000) == 0) {
288 put_thread_offline();
289 put_thread_online_delay();
293 put_thread_offline();
294 rcu_unregister_thread();
299 void *rcu_update_stress_test(void *arg
)
302 struct rcu_stress
*p
;
304 while (goflag
== GOFLAG_INIT
)
306 while (goflag
== GOFLAG_RUN
) {
307 i
= rcu_stress_idx
+ 1;
308 if (i
>= RCU_STRESS_PIPE_LEN
)
310 p
= &rcu_stress_array
[i
];
315 rcu_assign_pointer(rcu_stress_current
, p
);
317 for (i
= 0; i
< RCU_STRESS_PIPE_LEN
; i
++)
318 if (i
!= rcu_stress_idx
)
319 rcu_stress_array
[i
].pipe_count
++;
326 void *rcu_fake_update_stress_test(void *arg
)
328 while (goflag
== GOFLAG_INIT
)
330 while (goflag
== GOFLAG_RUN
) {
337 void stresstest(int nreaders
)
344 init_per_thread(n_reads_pt
, 0LL);
346 p
= &per_thread(rcu_stress_count
,t
)[0];
347 for (i
= 0; i
<= RCU_STRESS_PIPE_LEN
; i
++)
350 rcu_stress_current
= &rcu_stress_array
[0];
351 rcu_stress_current
->pipe_count
= 0;
352 rcu_stress_current
->mbtest
= 1;
353 for (i
= 0; i
< nreaders
; i
++)
354 create_thread(rcu_read_stress_test
, NULL
);
355 create_thread(rcu_update_stress_test
, NULL
);
356 for (i
= 0; i
< 5; i
++)
357 create_thread(rcu_fake_update_stress_test
, NULL
);
363 goflag
= GOFLAG_STOP
;
367 n_reads
+= per_thread(n_reads_pt
, t
);
368 printf("n_reads: %lld n_updates: %ld n_mberror: %d\n",
369 n_reads
, n_updates
, n_mberror
);
370 printf("rcu_stress_count:");
371 for (i
= 0; i
<= RCU_STRESS_PIPE_LEN
; i
++) {
374 sum
+= per_thread(rcu_stress_count
, t
)[i
];
376 printf(" %lld", sum
);
386 void usage(int argc
, char *argv
[])
388 fprintf(stderr
, "Usage: %s [nreaders [ perf | stress ] ]\n", argv
[0]);
392 int main(int argc
, char *argv
[])
401 yield_active
|= YIELD_READ
;
402 yield_active
|= YIELD_WRITE
;
406 nreaders
= strtoul(argv
[1], NULL
, 0);
408 perftest(nreaders
, cpustride
);
410 cpustride
= strtoul(argv
[3], NULL
, 0);
411 if (strcmp(argv
[2], "perf") == 0)
412 perftest(nreaders
, cpustride
);
413 else if (strcmp(argv
[2], "rperf") == 0)
414 rperftest(nreaders
, cpustride
);
415 else if (strcmp(argv
[2], "uperf") == 0)
416 uperftest(nreaders
, cpustride
);
417 else if (strcmp(argv
[2], "stress") == 0)
418 stresstest(nreaders
);
421 perftest(nreaders
, cpustride
);