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;
73 atomic_t nthreadsrunning
;
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 */
95 #ifndef mark_rcu_quiescent_state
96 #define mark_rcu_quiescent_state() do ; while (0)
97 #endif /* #ifdef mark_rcu_quiescent_state */
99 #ifndef put_thread_offline
100 #define put_thread_offline() do ; while (0)
101 #define put_thread_online() do ; while (0)
102 #define put_thread_online_delay() do ; while (0)
103 #else /* #ifndef put_thread_offline */
104 #define put_thread_online_delay() synchronize_rcu()
105 #endif /* #else #ifndef put_thread_offline */
111 void *rcu_read_perf_test(void *arg
)
115 long long n_reads_local
= 0;
117 rcu_register_thread();
119 atomic_inc(&nthreadsrunning
);
120 while (goflag
== GOFLAG_INIT
)
122 mark_rcu_quiescent_state();
123 while (goflag
== GOFLAG_RUN
) {
124 for (i
= 0; i
< RCU_READ_RUN
; i
++) {
126 /* rcu_read_lock_nest(); */
127 /* rcu_read_unlock_nest(); */
130 n_reads_local
+= RCU_READ_RUN
;
131 mark_rcu_quiescent_state();
133 __get_thread_var(n_reads_pt
) += n_reads_local
;
134 put_thread_offline();
135 rcu_unregister_thread();
140 void *rcu_update_perf_test(void *arg
)
142 long long n_updates_local
= 0;
144 atomic_inc(&nthreadsrunning
);
145 while (goflag
== GOFLAG_INIT
)
147 while (goflag
== GOFLAG_RUN
) {
151 __get_thread_var(n_updates_pt
) += n_updates_local
;
155 void perftestinit(void)
157 init_per_thread(n_reads_pt
, 0LL);
158 init_per_thread(n_updates_pt
, 0LL);
159 atomic_set(&nthreadsrunning
, 0);
162 void perftestrun(int nthreads
, int nreaders
, int nupdaters
)
168 while (atomic_read(&nthreadsrunning
) < nthreads
)
174 goflag
= GOFLAG_STOP
;
178 n_reads
+= per_thread(n_reads_pt
, t
);
179 n_updates
+= per_thread(n_updates_pt
, t
);
181 printf("n_reads: %lld n_updates: %ld nreaders: %d nupdaters: %d duration: %d\n",
182 n_reads
, n_updates
, nreaders
, nupdaters
, duration
);
183 printf("ns/read: %g ns/update: %g\n",
184 ((duration
* 1000*1000*1000.*(double)nreaders
) /
186 ((duration
* 1000*1000*1000.*(double)nupdaters
) /
191 void perftest(int nreaders
, int cpustride
)
197 for (i
= 0; i
< nreaders
; i
++) {
198 arg
= (long)(i
* cpustride
);
199 create_thread(rcu_read_perf_test
, (void *)arg
);
201 arg
= (long)(i
* cpustride
);
202 create_thread(rcu_update_perf_test
, (void *)arg
);
203 perftestrun(i
+ 1, nreaders
, 1);
206 void rperftest(int nreaders
, int cpustride
)
212 init_per_thread(n_reads_pt
, 0LL);
213 for (i
= 0; i
< nreaders
; i
++) {
214 arg
= (long)(i
* cpustride
);
215 create_thread(rcu_read_perf_test
, (void *)arg
);
217 perftestrun(i
, nreaders
, 0);
220 void uperftest(int nupdaters
, int cpustride
)
226 init_per_thread(n_reads_pt
, 0LL);
227 for (i
= 0; i
< nupdaters
; i
++) {
228 arg
= (long)(i
* cpustride
);
229 create_thread(rcu_update_perf_test
, (void *)arg
);
231 perftestrun(i
, 0, nupdaters
);
238 #define RCU_STRESS_PIPE_LEN 10
245 struct rcu_stress rcu_stress_array
[RCU_STRESS_PIPE_LEN
] = { { 0 } };
246 struct rcu_stress
*rcu_stress_current
;
247 int rcu_stress_idx
= 0;
250 DEFINE_PER_THREAD(long long [RCU_STRESS_PIPE_LEN
+ 1], rcu_stress_count
);
254 void *rcu_read_stress_test(void *arg
)
258 struct rcu_stress
*p
;
261 rcu_register_thread();
262 while (goflag
== GOFLAG_INIT
)
264 mark_rcu_quiescent_state();
265 while (goflag
== GOFLAG_RUN
) {
267 p
= rcu_dereference(rcu_stress_current
);
270 rcu_read_lock_nest();
271 for (i
= 0; i
< 100; i
++)
273 rcu_read_unlock_nest();
276 if ((pc
> RCU_STRESS_PIPE_LEN
) || (pc
< 0))
277 pc
= RCU_STRESS_PIPE_LEN
;
278 __get_thread_var(rcu_stress_count
)[pc
]++;
279 __get_thread_var(n_reads_pt
)++;
280 mark_rcu_quiescent_state();
281 if ((++itercnt
% 0x1000) == 0) {
282 put_thread_offline();
283 put_thread_online_delay();
287 put_thread_offline();
288 rcu_unregister_thread();
293 void *rcu_update_stress_test(void *arg
)
296 struct rcu_stress
*p
;
298 while (goflag
== GOFLAG_INIT
)
300 while (goflag
== GOFLAG_RUN
) {
301 i
= rcu_stress_idx
+ 1;
302 if (i
>= RCU_STRESS_PIPE_LEN
)
304 p
= &rcu_stress_array
[i
];
309 rcu_assign_pointer(rcu_stress_current
, p
);
311 for (i
= 0; i
< RCU_STRESS_PIPE_LEN
; i
++)
312 if (i
!= rcu_stress_idx
)
313 rcu_stress_array
[i
].pipe_count
++;
320 void *rcu_fake_update_stress_test(void *arg
)
322 while (goflag
== GOFLAG_INIT
)
324 while (goflag
== GOFLAG_RUN
) {
331 void stresstest(int nreaders
)
338 init_per_thread(n_reads_pt
, 0LL);
340 p
= &per_thread(rcu_stress_count
,t
)[0];
341 for (i
= 0; i
<= RCU_STRESS_PIPE_LEN
; i
++)
344 rcu_stress_current
= &rcu_stress_array
[0];
345 rcu_stress_current
->pipe_count
= 0;
346 rcu_stress_current
->mbtest
= 1;
347 for (i
= 0; i
< nreaders
; i
++)
348 create_thread(rcu_read_stress_test
, NULL
);
349 create_thread(rcu_update_stress_test
, NULL
);
350 for (i
= 0; i
< 5; i
++)
351 create_thread(rcu_fake_update_stress_test
, NULL
);
357 goflag
= GOFLAG_STOP
;
361 n_reads
+= per_thread(n_reads_pt
, t
);
362 printf("n_reads: %lld n_updates: %ld n_mberror: %d\n",
363 n_reads
, n_updates
, n_mberror
);
364 printf("rcu_stress_count:");
365 for (i
= 0; i
<= RCU_STRESS_PIPE_LEN
; i
++) {
368 sum
+= per_thread(rcu_stress_count
, t
)[i
];
370 printf(" %lld", sum
);
380 void usage(int argc
, char *argv
[])
382 fprintf(stderr
, "Usage: %s [nreaders [ perf | stress ] ]\n", argv
[0]);
386 int main(int argc
, char *argv
[])
395 yield_active
|= YIELD_READ
;
396 yield_active
|= YIELD_WRITE
;
400 nreaders
= strtoul(argv
[1], NULL
, 0);
402 perftest(nreaders
, cpustride
);
404 cpustride
= strtoul(argv
[3], NULL
, 0);
405 if (strcmp(argv
[2], "perf") == 0)
406 perftest(nreaders
, cpustride
);
407 else if (strcmp(argv
[2], "rperf") == 0)
408 rperftest(nreaders
, cpustride
);
409 else if (strcmp(argv
[2], "uperf") == 0)
410 uperftest(nreaders
, cpustride
);
411 else if (strcmp(argv
[2], "stress") == 0)
412 stresstest(nreaders
);
415 perftest(nreaders
, cpustride
);