tests/regression/rcutorture: Use urcu-wait
[urcu.git] / tests / regression / rcutorture.h
CommitLineData
ce29b371
MJ
1// SPDX-FileCopyrightText: 2008 Paul E. McKenney, IBM Corporation.
2//
3// SPDX-License-Identifier: GPL-2.0-or-later
4
8a953620
MD
5/*
6 * rcutorture.h: simple user-level performance/stress test of RCU.
7 *
8 * Usage:
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
13 * CPUs from 0 to 30.
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.
22 *
23 * The above tests produce output as follows:
24 *
25 * n_reads: 46008000 n_updates: 146026 nreaders: 2 nupdaters: 1 duration: 1
26 * ns/read: 43.4707 ns/update: 6848.1
27 *
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.
33 *
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
37 * particular CPU.
38 *
39 * This test produces output as follows:
40 *
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
43 *
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
49 * numbers non-zero.
8a953620
MD
50 */
51
52/*
53 * Test variables.
54 */
55
b57aee66 56#include <stdlib.h>
ad460058
MD
57#include "tap.h"
58
0d48d10d
OD
59#include "urcu-wait.h"
60
ad460058 61#define NR_TESTS 1
b57aee66 62
8a953620
MD
63DEFINE_PER_THREAD(long long, n_reads_pt);
64DEFINE_PER_THREAD(long long, n_updates_pt);
65
26b5a74b
MD
66enum callrcu_type {
67 CALLRCU_GLOBAL,
68 CALLRCU_PERCPU,
69 CALLRCU_PERTHREAD,
70};
71
eb218d4f
MD
72enum writer_state {
73 WRITER_STATE_SYNC_RCU,
74 WRITER_STATE_CALL_RCU,
75 WRITER_STATE_POLL_RCU,
76};
77
26b5a74b
MD
78static enum callrcu_type callrcu_type = CALLRCU_GLOBAL;
79
8a953620
MD
80long long n_reads = 0LL;
81long n_updates = 0L;
6ee91d83 82int nthreadsrunning;
8a953620
MD
83char argsbuf[64];
84
85#define GOFLAG_INIT 0
86#define GOFLAG_RUN 1
87#define GOFLAG_STOP 2
88
4967f005
PB
89volatile int goflag __attribute__((__aligned__(CAA_CACHE_LINE_SIZE)))
90 = GOFLAG_INIT;
8a953620
MD
91
92#define RCU_READ_RUN 1000
93
94//MD
95#define RCU_READ_NESTABLE
96
97#ifdef RCU_READ_NESTABLE
98#define rcu_read_lock_nest() rcu_read_lock()
99#define rcu_read_unlock_nest() rcu_read_unlock()
100#else /* #ifdef RCU_READ_NESTABLE */
101#define rcu_read_lock_nest()
102#define rcu_read_unlock_nest()
103#endif /* #else #ifdef RCU_READ_NESTABLE */
104
1a43bbd8
MD
105#ifdef TORTURE_QSBR
106#define mark_rcu_quiescent_state rcu_quiescent_state
107#define put_thread_offline rcu_thread_offline
108#define put_thread_online rcu_thread_online
109#endif
110
8a953620 111#ifndef mark_rcu_quiescent_state
447c9339 112#define mark_rcu_quiescent_state() do {} while (0)
8a953620
MD
113#endif /* #ifdef mark_rcu_quiescent_state */
114
115#ifndef put_thread_offline
447c9339
MJ
116#define put_thread_offline() do {} while (0)
117#define put_thread_online() do {} while (0)
118#define put_thread_online_delay() do {} while (0)
8a953620
MD
119#else /* #ifndef put_thread_offline */
120#define put_thread_online_delay() synchronize_rcu()
121#endif /* #else #ifndef put_thread_offline */
122
123/*
124 * Performance test.
125 */
126
61c3fb60 127static
8a953620
MD
128void *rcu_read_perf_test(void *arg)
129{
130 int i;
131 int me = (long)arg;
8a953620
MD
132 long long n_reads_local = 0;
133
121a5d44 134 rcu_register_thread();
8a953620 135 run_on(me);
ec4e58a3 136 uatomic_inc(&nthreadsrunning);
3bf02b5b 137 put_thread_offline();
8a953620 138 while (goflag == GOFLAG_INIT)
775aff2e 139 (void) poll(NULL, 0, 1);
3bf02b5b 140 put_thread_online();
8a953620
MD
141 while (goflag == GOFLAG_RUN) {
142 for (i = 0; i < RCU_READ_RUN; i++) {
143 rcu_read_lock();
144 /* rcu_read_lock_nest(); */
145 /* rcu_read_unlock_nest(); */
146 rcu_read_unlock();
147 }
148 n_reads_local += RCU_READ_RUN;
149 mark_rcu_quiescent_state();
150 }
151 __get_thread_var(n_reads_pt) += n_reads_local;
152 put_thread_offline();
121a5d44 153 rcu_unregister_thread();
8a953620
MD
154
155 return (NULL);
156}
157
61c3fb60 158static
70469b43 159void *rcu_update_perf_test(void *arg __attribute__((unused)))
8a953620
MD
160{
161 long long n_updates_local = 0;
162
26b5a74b 163 if (callrcu_type == CALLRCU_PERTHREAD) {
b57aee66
PM
164 struct call_rcu_data *crdp;
165
c1d2c60b 166 crdp = create_call_rcu_data(0, -1);
b57aee66 167 if (crdp != NULL) {
26b5a74b 168 diag("Successfully using per-thread call_rcu() worker.");
b57aee66
PM
169 set_thread_call_rcu_data(crdp);
170 }
171 }
ec4e58a3 172 uatomic_inc(&nthreadsrunning);
8a953620 173 while (goflag == GOFLAG_INIT)
775aff2e 174 (void) poll(NULL, 0, 1);
8a953620
MD
175 while (goflag == GOFLAG_RUN) {
176 synchronize_rcu();
177 n_updates_local++;
178 }
179 __get_thread_var(n_updates_pt) += n_updates_local;
26b5a74b
MD
180 if (callrcu_type == CALLRCU_PERTHREAD) {
181 struct call_rcu_data *crdp;
182
183 crdp = get_thread_call_rcu_data();
184 set_thread_call_rcu_data(NULL);
185 call_rcu_data_free(crdp);
186 }
b0b31506 187 return NULL;
8a953620
MD
188}
189
61c3fb60 190static
8a953620
MD
191void perftestinit(void)
192{
193 init_per_thread(n_reads_pt, 0LL);
194 init_per_thread(n_updates_pt, 0LL);
ec4e58a3 195 uatomic_set(&nthreadsrunning, 0);
8a953620
MD
196}
197
61c3fb60 198static
ad460058 199int perftestrun(int nthreads, int nreaders, int nupdaters)
8a953620
MD
200{
201 int t;
202 int duration = 1;
203
5481ddb3 204 cmm_smp_mb();
ec4e58a3 205 while (uatomic_read(&nthreadsrunning) < nthreads)
775aff2e 206 (void) poll(NULL, 0, 1);
8a953620 207 goflag = GOFLAG_RUN;
5481ddb3 208 cmm_smp_mb();
8a953620 209 sleep(duration);
5481ddb3 210 cmm_smp_mb();
8a953620 211 goflag = GOFLAG_STOP;
5481ddb3 212 cmm_smp_mb();
8a953620
MD
213 wait_all_threads();
214 for_each_thread(t) {
215 n_reads += per_thread(n_reads_pt, t);
216 n_updates += per_thread(n_updates_pt, t);
217 }
ad460058 218 diag("n_reads: %lld n_updates: %ld nreaders: %d nupdaters: %d duration: %d",
8a953620 219 n_reads, n_updates, nreaders, nupdaters, duration);
ad460058 220 diag("ns/read: %g ns/update: %g",
8a953620
MD
221 ((duration * 1000*1000*1000.*(double)nreaders) /
222 (double)n_reads),
223 ((duration * 1000*1000*1000.*(double)nupdaters) /
224 (double)n_updates));
7106ddf8 225 if (get_cpu_call_rcu_data(0)) {
ad460058 226 diag("Deallocating per-CPU call_rcu threads.\n");
7106ddf8
PM
227 free_all_cpu_call_rcu_data();
228 }
ad460058 229 return 0;
8a953620
MD
230}
231
61c3fb60 232static
ad460058 233int perftest(int nreaders, int cpustride)
8a953620
MD
234{
235 int i;
236 long arg;
237
238 perftestinit();
239 for (i = 0; i < nreaders; i++) {
240 arg = (long)(i * cpustride);
241 create_thread(rcu_read_perf_test, (void *)arg);
242 }
243 arg = (long)(i * cpustride);
244 create_thread(rcu_update_perf_test, (void *)arg);
ad460058 245 return perftestrun(i + 1, nreaders, 1);
8a953620
MD
246}
247
61c3fb60 248static
ad460058 249int rperftest(int nreaders, int cpustride)
8a953620
MD
250{
251 int i;
252 long arg;
253
254 perftestinit();
255 init_per_thread(n_reads_pt, 0LL);
256 for (i = 0; i < nreaders; i++) {
257 arg = (long)(i * cpustride);
258 create_thread(rcu_read_perf_test, (void *)arg);
259 }
ad460058 260 return perftestrun(i, nreaders, 0);
8a953620
MD
261}
262
61c3fb60 263static
ad460058 264int uperftest(int nupdaters, int cpustride)
8a953620
MD
265{
266 int i;
267 long arg;
268
269 perftestinit();
270 init_per_thread(n_reads_pt, 0LL);
271 for (i = 0; i < nupdaters; i++) {
272 arg = (long)(i * cpustride);
273 create_thread(rcu_update_perf_test, (void *)arg);
274 }
ad460058 275 return perftestrun(i, 0, nupdaters);
8a953620
MD
276}
277
278/*
279 * Stress test.
280 */
281
282#define RCU_STRESS_PIPE_LEN 10
283
284struct rcu_stress {
285 int pipe_count;
286 int mbtest;
287};
288
153b081a 289struct rcu_stress rcu_stress_array[RCU_STRESS_PIPE_LEN] = { { 0, 0 } };
8a953620
MD
290struct rcu_stress *rcu_stress_current;
291int rcu_stress_idx = 0;
292
293int n_mberror = 0;
294DEFINE_PER_THREAD(long long [RCU_STRESS_PIPE_LEN + 1], rcu_stress_count);
295
296int garbage = 0;
297
61c3fb60 298static
70469b43 299void *rcu_read_stress_test(void *arg __attribute__((unused)))
8a953620
MD
300{
301 int i;
302 int itercnt = 0;
303 struct rcu_stress *p;
304 int pc;
305
121a5d44 306 rcu_register_thread();
3bf02b5b 307 put_thread_offline();
8a953620 308 while (goflag == GOFLAG_INIT)
775aff2e 309 (void) poll(NULL, 0, 1);
3bf02b5b 310 put_thread_online();
8a953620
MD
311 while (goflag == GOFLAG_RUN) {
312 rcu_read_lock();
313 p = rcu_dereference(rcu_stress_current);
314 if (p->mbtest == 0)
315 n_mberror++;
316 rcu_read_lock_nest();
317 for (i = 0; i < 100; i++)
318 garbage++;
319 rcu_read_unlock_nest();
320 pc = p->pipe_count;
321 rcu_read_unlock();
322 if ((pc > RCU_STRESS_PIPE_LEN) || (pc < 0))
323 pc = RCU_STRESS_PIPE_LEN;
324 __get_thread_var(rcu_stress_count)[pc]++;
325 __get_thread_var(n_reads_pt)++;
326 mark_rcu_quiescent_state();
327 if ((++itercnt % 0x1000) == 0) {
328 put_thread_offline();
329 put_thread_online_delay();
330 put_thread_online();
331 }
332 }
333 put_thread_offline();
121a5d44 334 rcu_unregister_thread();
8a953620
MD
335
336 return (NULL);
337}
338
0d48d10d 339static DEFINE_URCU_WAIT_QUEUE(call_rcu_waiters);
b57aee66 340
61c3fb60 341static
70469b43 342void rcu_update_stress_test_rcu(struct rcu_head *head __attribute__((unused)))
b57aee66 343{
0d48d10d
OD
344 struct urcu_waiters waiters;
345
346 urcu_move_waiters(&waiters, &call_rcu_waiters);
347 urcu_wake_all_waiters(&waiters);
b57aee66
PM
348}
349
eb218d4f
MD
350static
351void advance_writer_state(enum writer_state *state)
352{
353 switch (*state) {
354 case WRITER_STATE_SYNC_RCU:
355 *state = WRITER_STATE_CALL_RCU;
356 break;
357 case WRITER_STATE_CALL_RCU:
358 *state = WRITER_STATE_POLL_RCU;
359 break;
360 case WRITER_STATE_POLL_RCU:
361 *state = WRITER_STATE_SYNC_RCU;
362 break;
363 }
364}
365
61c3fb60 366static
70469b43 367void *rcu_update_stress_test(void *arg __attribute__((unused)))
8a953620
MD
368{
369 int i;
370 struct rcu_stress *p;
b57aee66 371 struct rcu_head rh;
eb218d4f 372 enum writer_state writer_state = WRITER_STATE_SYNC_RCU;
8a953620 373
b21d2eb5
OD
374 rcu_register_thread();
375
376 /* Offline for poll. */
377 put_thread_offline();
8a953620 378 while (goflag == GOFLAG_INIT)
775aff2e 379 (void) poll(NULL, 0, 1);
b21d2eb5
OD
380 put_thread_online();
381
8a953620
MD
382 while (goflag == GOFLAG_RUN) {
383 i = rcu_stress_idx + 1;
384 if (i >= RCU_STRESS_PIPE_LEN)
385 i = 0;
386 p = &rcu_stress_array[i];
387 p->mbtest = 0;
5481ddb3 388 cmm_smp_mb();
8a953620
MD
389 p->pipe_count = 0;
390 p->mbtest = 1;
391 rcu_assign_pointer(rcu_stress_current, p);
392 rcu_stress_idx = i;
393 for (i = 0; i < RCU_STRESS_PIPE_LEN; i++)
394 if (i != rcu_stress_idx)
395 rcu_stress_array[i].pipe_count++;
eb218d4f
MD
396 switch (writer_state) {
397 case WRITER_STATE_SYNC_RCU:
b57aee66 398 synchronize_rcu();
eb218d4f
MD
399 break;
400 case WRITER_STATE_CALL_RCU:
401 {
0d48d10d
OD
402 DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
403
404 urcu_wait_add(&call_rcu_waiters, &wait);
405
b57aee66 406 call_rcu(&rh, rcu_update_stress_test_rcu);
b21d2eb5 407
0d48d10d 408 urcu_adaptative_busy_wait(&wait);
eb218d4f
MD
409 break;
410 }
411 case WRITER_STATE_POLL_RCU:
412 {
413 struct urcu_gp_poll_state poll_state;
414
eb218d4f 415 poll_state = start_poll_synchronize_rcu();
b21d2eb5
OD
416
417 /* Offline for poll. */
418 put_thread_offline();
eb218d4f
MD
419 while (!poll_state_synchronize_rcu(poll_state))
420 (void) poll(NULL, 0, 1); /* Wait for 1ms */
b21d2eb5 421 put_thread_online();
eb218d4f
MD
422 break;
423 }
b57aee66 424 }
8a953620 425 n_updates++;
eb218d4f 426 advance_writer_state(&writer_state);
8a953620 427 }
a13ef613 428
b21d2eb5
OD
429 rcu_unregister_thread();
430
b0b31506 431 return NULL;
8a953620
MD
432}
433
61c3fb60 434static
70469b43 435void *rcu_fake_update_stress_test(void *arg __attribute__((unused)))
8a953620 436{
26b5a74b 437 if (callrcu_type == CALLRCU_PERTHREAD) {
b57aee66
PM
438 struct call_rcu_data *crdp;
439
c1d2c60b 440 crdp = create_call_rcu_data(0, -1);
b57aee66 441 if (crdp != NULL) {
26b5a74b 442 diag("Successfully using per-thread call_rcu() worker.");
b57aee66
PM
443 set_thread_call_rcu_data(crdp);
444 }
445 }
8a953620 446 while (goflag == GOFLAG_INIT)
775aff2e 447 (void) poll(NULL, 0, 1);
8a953620
MD
448 while (goflag == GOFLAG_RUN) {
449 synchronize_rcu();
775aff2e 450 (void) poll(NULL, 0, 1);
8a953620 451 }
26b5a74b
MD
452 if (callrcu_type == CALLRCU_PERTHREAD) {
453 struct call_rcu_data *crdp;
454
455 crdp = get_thread_call_rcu_data();
456 set_thread_call_rcu_data(NULL);
457 call_rcu_data_free(crdp);
458 }
b0b31506 459 return NULL;
8a953620
MD
460}
461
61c3fb60 462static
ad460058 463int stresstest(int nreaders)
8a953620
MD
464{
465 int i;
466 int t;
467 long long *p;
468 long long sum;
469
470 init_per_thread(n_reads_pt, 0LL);
471 for_each_thread(t) {
472 p = &per_thread(rcu_stress_count,t)[0];
473 for (i = 0; i <= RCU_STRESS_PIPE_LEN; i++)
474 p[i] = 0LL;
475 }
476 rcu_stress_current = &rcu_stress_array[0];
477 rcu_stress_current->pipe_count = 0;
478 rcu_stress_current->mbtest = 1;
479 for (i = 0; i < nreaders; i++)
480 create_thread(rcu_read_stress_test, NULL);
481 create_thread(rcu_update_stress_test, NULL);
482 for (i = 0; i < 5; i++)
483 create_thread(rcu_fake_update_stress_test, NULL);
5481ddb3 484 cmm_smp_mb();
8a953620 485 goflag = GOFLAG_RUN;
5481ddb3 486 cmm_smp_mb();
8a953620 487 sleep(10);
5481ddb3 488 cmm_smp_mb();
8a953620 489 goflag = GOFLAG_STOP;
5481ddb3 490 cmm_smp_mb();
8a953620
MD
491 wait_all_threads();
492 for_each_thread(t)
493 n_reads += per_thread(n_reads_pt, t);
ad460058 494 diag("n_reads: %lld n_updates: %ld n_mberror: %d",
8a953620 495 n_reads, n_updates, n_mberror);
ad460058
MD
496 rdiag_start();
497 rdiag("rcu_stress_count:");
8a953620
MD
498 for (i = 0; i <= RCU_STRESS_PIPE_LEN; i++) {
499 sum = 0LL;
500 for_each_thread(t) {
501 sum += per_thread(rcu_stress_count, t)[i];
502 }
ad460058 503 rdiag(" %lld", sum);
8a953620 504 }
ad460058 505 rdiag_end();
7106ddf8 506 if (get_cpu_call_rcu_data(0)) {
ad460058 507 diag("Deallocating per-CPU call_rcu threads.");
7106ddf8
PM
508 free_all_cpu_call_rcu_data();
509 }
ad460058
MD
510 if (!n_mberror)
511 return 0;
512 else
513 return -1;
8a953620
MD
514}
515
516/*
517 * Mainprogram.
518 */
519
a142df4e 520static
6fa8b4f8 521void usage(char *argv[]) __attribute__((__noreturn__));
a142df4e 522
61c3fb60 523static
70469b43 524void usage(char *argv[])
8a953620 525{
26b5a74b 526 diag("Usage: %s nreaders [ perf | rperf | uperf | stress ] [ stride ] [ callrcu_global | callrcu_percpu | callrcu_perthread ]\n", argv[0]);
8a953620
MD
527 exit(-1);
528}
529
530int main(int argc, char *argv[])
531{
532 int nreaders = 1;
533 int cpustride = 1;
534
ad460058
MD
535 plan_tests(NR_TESTS);
536
8a953620
MD
537 smp_init();
538 //rcu_init();
26b5a74b
MD
539 if (argc > 4) {
540 const char *callrcu_str = argv[4];;
541
542 if (strcmp(callrcu_str, "callrcu_global") == 0) {
543 callrcu_type = CALLRCU_GLOBAL;
544 } else if (strcmp(callrcu_str, "callrcu_percpu") == 0) {
545 callrcu_type = CALLRCU_PERCPU;
546 } else if (strcmp(callrcu_str, "callrcu_perthread") == 0) {
547 callrcu_type = CALLRCU_PERTHREAD;
548 } else {
70469b43 549 usage(argv);
26b5a74b
MD
550 goto end;
551 }
552 }
929cfaff 553
26b5a74b
MD
554 switch (callrcu_type) {
555 case CALLRCU_GLOBAL:
556 diag("Using global per-process call_rcu thread.");
557 break;
558 case CALLRCU_PERCPU:
559 diag("Using per-CPU call_rcu threads.");
b57aee66 560 if (create_all_cpu_call_rcu_data(0))
ad460058
MD
561 diag("create_all_cpu_call_rcu_data: %s",
562 strerror(errno));
26b5a74b
MD
563 break;
564 case CALLRCU_PERTHREAD:
565 diag("Using per-thread call_rcu() worker.");
566 break;
567 default:
568 abort();
b57aee66 569 }
8a953620 570
9b171f46
MD
571#ifdef DEBUG_YIELD
572 yield_active |= YIELD_READ;
573 yield_active |= YIELD_WRITE;
574#endif
575
8a953620 576 if (argc > 1) {
26b5a74b
MD
577 if (strcmp(argv[1], "-h") == 0
578 || strcmp(argv[1], "--help") == 0) {
70469b43 579 usage(argv);
26b5a74b
MD
580 goto end;
581 }
8a953620 582 nreaders = strtoul(argv[1], NULL, 0);
ad460058
MD
583 if (argc == 2) {
584 ok(!perftest(nreaders, cpustride),
585 "perftest readers: %d, stride: %d",
586 nreaders, cpustride);
587 goto end;
588 }
8a953620
MD
589 if (argc > 3)
590 cpustride = strtoul(argv[3], NULL, 0);
591 if (strcmp(argv[2], "perf") == 0)
ad460058
MD
592 ok(!perftest(nreaders, cpustride),
593 "perftest readers: %d, stride: %d",
594 nreaders, cpustride);
8a953620 595 else if (strcmp(argv[2], "rperf") == 0)
ad460058
MD
596 ok(!rperftest(nreaders, cpustride),
597 "rperftest readers: %d, stride: %d",
598 nreaders, cpustride);
8a953620 599 else if (strcmp(argv[2], "uperf") == 0)
ad460058
MD
600 ok(!uperftest(nreaders, cpustride),
601 "uperftest readers: %d, stride: %d",
602 nreaders, cpustride);
8a953620 603 else if (strcmp(argv[2], "stress") == 0)
ad460058
MD
604 ok(!stresstest(nreaders),
605 "stresstest readers: %d, stride: %d",
606 nreaders, cpustride);
607 else
70469b43 608 usage(argv);
ad460058 609 } else {
70469b43 610 usage(argv);
8a953620 611 }
ad460058
MD
612end:
613 return exit_status();
8a953620 614}
This page took 0.075344 seconds and 4 git commands to generate.