Merge branch 'master' into lfqueue-dev
[urcu.git] / tests / rcutorture.h
CommitLineData
8a953620
MD
1/*
2 * rcutorture.h: simple user-level performance/stress test of RCU.
3 *
4 * Usage:
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
9 * CPUs from 0 to 30.
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.
18 *
19 * The above tests produce output as follows:
20 *
21 * n_reads: 46008000 n_updates: 146026 nreaders: 2 nupdaters: 1 duration: 1
22 * ns/read: 43.4707 ns/update: 6848.1
23 *
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.
29 *
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
33 * particular CPU.
34 *
35 * This test produces output as follows:
36 *
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
39 *
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
45 * numbers non-zero.
46 *
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.
51 *
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.
56 *
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.
60 *
61 * Copyright (c) 2008 Paul E. McKenney, IBM Corporation.
62 */
63
64/*
65 * Test variables.
66 */
67
b57aee66 68#include <stdlib.h>
b57aee66 69
8a953620
MD
70DEFINE_PER_THREAD(long long, n_reads_pt);
71DEFINE_PER_THREAD(long long, n_updates_pt);
72
73long long n_reads = 0LL;
74long n_updates = 0L;
6ee91d83 75int nthreadsrunning;
8a953620
MD
76char argsbuf[64];
77
78#define GOFLAG_INIT 0
79#define GOFLAG_RUN 1
80#define GOFLAG_STOP 2
81
4967f005
PB
82volatile int goflag __attribute__((__aligned__(CAA_CACHE_LINE_SIZE)))
83 = GOFLAG_INIT;
8a953620
MD
84
85#define RCU_READ_RUN 1000
86
87//MD
88#define RCU_READ_NESTABLE
89
90#ifdef RCU_READ_NESTABLE
91#define rcu_read_lock_nest() rcu_read_lock()
92#define rcu_read_unlock_nest() rcu_read_unlock()
93#else /* #ifdef RCU_READ_NESTABLE */
94#define rcu_read_lock_nest()
95#define rcu_read_unlock_nest()
96#endif /* #else #ifdef RCU_READ_NESTABLE */
97
1a43bbd8
MD
98#ifdef TORTURE_QSBR
99#define mark_rcu_quiescent_state rcu_quiescent_state
100#define put_thread_offline rcu_thread_offline
101#define put_thread_online rcu_thread_online
102#endif
103
8a953620
MD
104#ifndef mark_rcu_quiescent_state
105#define mark_rcu_quiescent_state() do ; while (0)
106#endif /* #ifdef mark_rcu_quiescent_state */
107
108#ifndef put_thread_offline
109#define put_thread_offline() do ; while (0)
110#define put_thread_online() do ; while (0)
111#define put_thread_online_delay() do ; while (0)
112#else /* #ifndef put_thread_offline */
113#define put_thread_online_delay() synchronize_rcu()
114#endif /* #else #ifndef put_thread_offline */
115
116/*
117 * Performance test.
118 */
119
120void *rcu_read_perf_test(void *arg)
121{
7106ddf8 122 struct call_rcu_data *crdp;
8a953620
MD
123 int i;
124 int me = (long)arg;
8a953620
MD
125 long long n_reads_local = 0;
126
121a5d44 127 rcu_register_thread();
8a953620 128 run_on(me);
ec4e58a3 129 uatomic_inc(&nthreadsrunning);
8a953620
MD
130 while (goflag == GOFLAG_INIT)
131 poll(NULL, 0, 1);
132 mark_rcu_quiescent_state();
133 while (goflag == GOFLAG_RUN) {
134 for (i = 0; i < RCU_READ_RUN; i++) {
135 rcu_read_lock();
136 /* rcu_read_lock_nest(); */
137 /* rcu_read_unlock_nest(); */
138 rcu_read_unlock();
139 }
140 n_reads_local += RCU_READ_RUN;
141 mark_rcu_quiescent_state();
142 }
143 __get_thread_var(n_reads_pt) += n_reads_local;
144 put_thread_offline();
7106ddf8
PM
145 crdp = get_thread_call_rcu_data();
146 set_thread_call_rcu_data(NULL);
147 call_rcu_data_free(crdp);
121a5d44 148 rcu_unregister_thread();
8a953620
MD
149
150 return (NULL);
151}
152
153void *rcu_update_perf_test(void *arg)
154{
155 long long n_updates_local = 0;
156
b57aee66
PM
157 if ((random() & 0xf00) == 0) {
158 struct call_rcu_data *crdp;
159
c1d2c60b 160 crdp = create_call_rcu_data(0, -1);
b57aee66
PM
161 if (crdp != NULL) {
162 fprintf(stderr,
163 "Using per-thread call_rcu() worker.\n");
164 set_thread_call_rcu_data(crdp);
165 }
166 }
ec4e58a3 167 uatomic_inc(&nthreadsrunning);
8a953620
MD
168 while (goflag == GOFLAG_INIT)
169 poll(NULL, 0, 1);
170 while (goflag == GOFLAG_RUN) {
171 synchronize_rcu();
172 n_updates_local++;
173 }
174 __get_thread_var(n_updates_pt) += n_updates_local;
b0b31506 175 return NULL;
8a953620
MD
176}
177
178void perftestinit(void)
179{
180 init_per_thread(n_reads_pt, 0LL);
181 init_per_thread(n_updates_pt, 0LL);
ec4e58a3 182 uatomic_set(&nthreadsrunning, 0);
8a953620
MD
183}
184
185void perftestrun(int nthreads, int nreaders, int nupdaters)
186{
187 int t;
188 int duration = 1;
189
5481ddb3 190 cmm_smp_mb();
ec4e58a3 191 while (uatomic_read(&nthreadsrunning) < nthreads)
8a953620
MD
192 poll(NULL, 0, 1);
193 goflag = GOFLAG_RUN;
5481ddb3 194 cmm_smp_mb();
8a953620 195 sleep(duration);
5481ddb3 196 cmm_smp_mb();
8a953620 197 goflag = GOFLAG_STOP;
5481ddb3 198 cmm_smp_mb();
8a953620
MD
199 wait_all_threads();
200 for_each_thread(t) {
201 n_reads += per_thread(n_reads_pt, t);
202 n_updates += per_thread(n_updates_pt, t);
203 }
204 printf("n_reads: %lld n_updates: %ld nreaders: %d nupdaters: %d duration: %d\n",
205 n_reads, n_updates, nreaders, nupdaters, duration);
206 printf("ns/read: %g ns/update: %g\n",
207 ((duration * 1000*1000*1000.*(double)nreaders) /
208 (double)n_reads),
209 ((duration * 1000*1000*1000.*(double)nupdaters) /
210 (double)n_updates));
7106ddf8
PM
211 if (get_cpu_call_rcu_data(0)) {
212 fprintf(stderr, "Deallocating per-CPU call_rcu threads.\n");
213 free_all_cpu_call_rcu_data();
214 }
8a953620
MD
215 exit(0);
216}
217
218void perftest(int nreaders, int cpustride)
219{
220 int i;
221 long arg;
222
223 perftestinit();
224 for (i = 0; i < nreaders; i++) {
225 arg = (long)(i * cpustride);
226 create_thread(rcu_read_perf_test, (void *)arg);
227 }
228 arg = (long)(i * cpustride);
229 create_thread(rcu_update_perf_test, (void *)arg);
230 perftestrun(i + 1, nreaders, 1);
231}
232
233void rperftest(int nreaders, int cpustride)
234{
235 int i;
236 long arg;
237
238 perftestinit();
239 init_per_thread(n_reads_pt, 0LL);
240 for (i = 0; i < nreaders; i++) {
241 arg = (long)(i * cpustride);
242 create_thread(rcu_read_perf_test, (void *)arg);
243 }
244 perftestrun(i, nreaders, 0);
245}
246
247void uperftest(int nupdaters, int cpustride)
248{
249 int i;
250 long arg;
251
252 perftestinit();
253 init_per_thread(n_reads_pt, 0LL);
254 for (i = 0; i < nupdaters; i++) {
255 arg = (long)(i * cpustride);
256 create_thread(rcu_update_perf_test, (void *)arg);
257 }
258 perftestrun(i, 0, nupdaters);
259}
260
261/*
262 * Stress test.
263 */
264
265#define RCU_STRESS_PIPE_LEN 10
266
267struct rcu_stress {
268 int pipe_count;
269 int mbtest;
270};
271
b0b31506 272struct rcu_stress rcu_stress_array[RCU_STRESS_PIPE_LEN] = { { 0 } };
8a953620
MD
273struct rcu_stress *rcu_stress_current;
274int rcu_stress_idx = 0;
275
276int n_mberror = 0;
277DEFINE_PER_THREAD(long long [RCU_STRESS_PIPE_LEN + 1], rcu_stress_count);
278
279int garbage = 0;
280
281void *rcu_read_stress_test(void *arg)
282{
283 int i;
284 int itercnt = 0;
285 struct rcu_stress *p;
286 int pc;
287
121a5d44 288 rcu_register_thread();
8a953620
MD
289 while (goflag == GOFLAG_INIT)
290 poll(NULL, 0, 1);
291 mark_rcu_quiescent_state();
292 while (goflag == GOFLAG_RUN) {
293 rcu_read_lock();
294 p = rcu_dereference(rcu_stress_current);
295 if (p->mbtest == 0)
296 n_mberror++;
297 rcu_read_lock_nest();
298 for (i = 0; i < 100; i++)
299 garbage++;
300 rcu_read_unlock_nest();
301 pc = p->pipe_count;
302 rcu_read_unlock();
303 if ((pc > RCU_STRESS_PIPE_LEN) || (pc < 0))
304 pc = RCU_STRESS_PIPE_LEN;
305 __get_thread_var(rcu_stress_count)[pc]++;
306 __get_thread_var(n_reads_pt)++;
307 mark_rcu_quiescent_state();
308 if ((++itercnt % 0x1000) == 0) {
309 put_thread_offline();
310 put_thread_online_delay();
311 put_thread_online();
312 }
313 }
314 put_thread_offline();
121a5d44 315 rcu_unregister_thread();
8a953620
MD
316
317 return (NULL);
318}
319
b57aee66
PM
320static pthread_mutex_t call_rcu_test_mutex = PTHREAD_MUTEX_INITIALIZER;
321static pthread_cond_t call_rcu_test_cond = PTHREAD_COND_INITIALIZER;
322
323void rcu_update_stress_test_rcu(struct rcu_head *head)
324{
325 if (pthread_mutex_lock(&call_rcu_test_mutex) != 0) {
326 perror("pthread_mutex_lock");
327 exit(-1);
328 }
329 if (pthread_cond_signal(&call_rcu_test_cond) != 0) {
330 perror("pthread_cond_signal");
331 exit(-1);
332 }
333 if (pthread_mutex_unlock(&call_rcu_test_mutex) != 0) {
334 perror("pthread_mutex_unlock");
335 exit(-1);
336 }
337}
338
8a953620
MD
339void *rcu_update_stress_test(void *arg)
340{
341 int i;
342 struct rcu_stress *p;
b57aee66 343 struct rcu_head rh;
8a953620
MD
344
345 while (goflag == GOFLAG_INIT)
346 poll(NULL, 0, 1);
347 while (goflag == GOFLAG_RUN) {
348 i = rcu_stress_idx + 1;
349 if (i >= RCU_STRESS_PIPE_LEN)
350 i = 0;
351 p = &rcu_stress_array[i];
352 p->mbtest = 0;
5481ddb3 353 cmm_smp_mb();
8a953620
MD
354 p->pipe_count = 0;
355 p->mbtest = 1;
356 rcu_assign_pointer(rcu_stress_current, p);
357 rcu_stress_idx = i;
358 for (i = 0; i < RCU_STRESS_PIPE_LEN; i++)
359 if (i != rcu_stress_idx)
360 rcu_stress_array[i].pipe_count++;
b57aee66
PM
361 if (n_updates & 0x1)
362 synchronize_rcu();
363 else {
364 if (pthread_mutex_lock(&call_rcu_test_mutex) != 0) {
365 perror("pthread_mutex_lock");
366 exit(-1);
367 }
368 call_rcu(&rh, rcu_update_stress_test_rcu);
369 if (pthread_cond_wait(&call_rcu_test_cond,
370 &call_rcu_test_mutex) != 0) {
371 perror("pthread_cond_wait");
372 exit(-1);
373 }
374 if (pthread_mutex_unlock(&call_rcu_test_mutex) != 0) {
375 perror("pthread_mutex_unlock");
376 exit(-1);
377 }
378 }
8a953620
MD
379 n_updates++;
380 }
b0b31506 381 return NULL;
8a953620
MD
382}
383
384void *rcu_fake_update_stress_test(void *arg)
385{
b57aee66
PM
386 if ((random() & 0xf00) == 0) {
387 struct call_rcu_data *crdp;
388
c1d2c60b 389 crdp = create_call_rcu_data(0, -1);
b57aee66
PM
390 if (crdp != NULL) {
391 fprintf(stderr,
392 "Using per-thread call_rcu() worker.\n");
393 set_thread_call_rcu_data(crdp);
394 }
395 }
8a953620
MD
396 while (goflag == GOFLAG_INIT)
397 poll(NULL, 0, 1);
398 while (goflag == GOFLAG_RUN) {
399 synchronize_rcu();
400 poll(NULL, 0, 1);
401 }
b0b31506 402 return NULL;
8a953620
MD
403}
404
405void stresstest(int nreaders)
406{
407 int i;
408 int t;
409 long long *p;
410 long long sum;
411
412 init_per_thread(n_reads_pt, 0LL);
413 for_each_thread(t) {
414 p = &per_thread(rcu_stress_count,t)[0];
415 for (i = 0; i <= RCU_STRESS_PIPE_LEN; i++)
416 p[i] = 0LL;
417 }
418 rcu_stress_current = &rcu_stress_array[0];
419 rcu_stress_current->pipe_count = 0;
420 rcu_stress_current->mbtest = 1;
421 for (i = 0; i < nreaders; i++)
422 create_thread(rcu_read_stress_test, NULL);
423 create_thread(rcu_update_stress_test, NULL);
424 for (i = 0; i < 5; i++)
425 create_thread(rcu_fake_update_stress_test, NULL);
5481ddb3 426 cmm_smp_mb();
8a953620 427 goflag = GOFLAG_RUN;
5481ddb3 428 cmm_smp_mb();
8a953620 429 sleep(10);
5481ddb3 430 cmm_smp_mb();
8a953620 431 goflag = GOFLAG_STOP;
5481ddb3 432 cmm_smp_mb();
8a953620
MD
433 wait_all_threads();
434 for_each_thread(t)
435 n_reads += per_thread(n_reads_pt, t);
b0b31506 436 printf("n_reads: %lld n_updates: %ld n_mberror: %d\n",
8a953620
MD
437 n_reads, n_updates, n_mberror);
438 printf("rcu_stress_count:");
439 for (i = 0; i <= RCU_STRESS_PIPE_LEN; i++) {
440 sum = 0LL;
441 for_each_thread(t) {
442 sum += per_thread(rcu_stress_count, t)[i];
443 }
444 printf(" %lld", sum);
445 }
446 printf("\n");
7106ddf8
PM
447 if (get_cpu_call_rcu_data(0)) {
448 fprintf(stderr, "Deallocating per-CPU call_rcu threads.\n");
449 free_all_cpu_call_rcu_data();
450 }
8a953620
MD
451 exit(0);
452}
453
454/*
455 * Mainprogram.
456 */
457
458void usage(int argc, char *argv[])
459{
460 fprintf(stderr, "Usage: %s [nreaders [ perf | stress ] ]\n", argv[0]);
461 exit(-1);
462}
463
464int main(int argc, char *argv[])
465{
466 int nreaders = 1;
467 int cpustride = 1;
468
469 smp_init();
470 //rcu_init();
b57aee66
PM
471 srandom(time(NULL));
472 if (random() & 0x100) {
473 fprintf(stderr, "Allocating per-CPU call_rcu threads.\n");
474 if (create_all_cpu_call_rcu_data(0))
475 perror("create_all_cpu_call_rcu_data");
476 }
8a953620 477
9b171f46
MD
478#ifdef DEBUG_YIELD
479 yield_active |= YIELD_READ;
480 yield_active |= YIELD_WRITE;
481#endif
482
8a953620
MD
483 if (argc > 1) {
484 nreaders = strtoul(argv[1], NULL, 0);
485 if (argc == 2)
486 perftest(nreaders, cpustride);
487 if (argc > 3)
488 cpustride = strtoul(argv[3], NULL, 0);
489 if (strcmp(argv[2], "perf") == 0)
490 perftest(nreaders, cpustride);
491 else if (strcmp(argv[2], "rperf") == 0)
492 rperftest(nreaders, cpustride);
493 else if (strcmp(argv[2], "uperf") == 0)
494 uperftest(nreaders, cpustride);
495 else if (strcmp(argv[2], "stress") == 0)
496 stresstest(nreaders);
497 usage(argc, argv);
498 }
499 perftest(nreaders, cpustride);
0578089f 500 return 0;
8a953620 501}
This page took 0.071919 seconds and 4 git commands to generate.