Fix: tests/rcutorture: Put thread offline on busy-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
c92c9904
OD
408 /* Offline for busy-wait. */
409 put_thread_offline();
0d48d10d 410 urcu_adaptative_busy_wait(&wait);
c92c9904 411 put_thread_online();
eb218d4f
MD
412 break;
413 }
414 case WRITER_STATE_POLL_RCU:
415 {
416 struct urcu_gp_poll_state poll_state;
417
eb218d4f 418 poll_state = start_poll_synchronize_rcu();
b21d2eb5
OD
419
420 /* Offline for poll. */
421 put_thread_offline();
eb218d4f
MD
422 while (!poll_state_synchronize_rcu(poll_state))
423 (void) poll(NULL, 0, 1); /* Wait for 1ms */
b21d2eb5 424 put_thread_online();
eb218d4f
MD
425 break;
426 }
b57aee66 427 }
8a953620 428 n_updates++;
eb218d4f 429 advance_writer_state(&writer_state);
8a953620 430 }
a13ef613 431
b21d2eb5
OD
432 rcu_unregister_thread();
433
b0b31506 434 return NULL;
8a953620
MD
435}
436
61c3fb60 437static
70469b43 438void *rcu_fake_update_stress_test(void *arg __attribute__((unused)))
8a953620 439{
26b5a74b 440 if (callrcu_type == CALLRCU_PERTHREAD) {
b57aee66
PM
441 struct call_rcu_data *crdp;
442
c1d2c60b 443 crdp = create_call_rcu_data(0, -1);
b57aee66 444 if (crdp != NULL) {
26b5a74b 445 diag("Successfully using per-thread call_rcu() worker.");
b57aee66
PM
446 set_thread_call_rcu_data(crdp);
447 }
448 }
8a953620 449 while (goflag == GOFLAG_INIT)
775aff2e 450 (void) poll(NULL, 0, 1);
8a953620
MD
451 while (goflag == GOFLAG_RUN) {
452 synchronize_rcu();
775aff2e 453 (void) poll(NULL, 0, 1);
8a953620 454 }
26b5a74b
MD
455 if (callrcu_type == CALLRCU_PERTHREAD) {
456 struct call_rcu_data *crdp;
457
458 crdp = get_thread_call_rcu_data();
459 set_thread_call_rcu_data(NULL);
460 call_rcu_data_free(crdp);
461 }
b0b31506 462 return NULL;
8a953620
MD
463}
464
61c3fb60 465static
ad460058 466int stresstest(int nreaders)
8a953620
MD
467{
468 int i;
469 int t;
470 long long *p;
471 long long sum;
472
473 init_per_thread(n_reads_pt, 0LL);
474 for_each_thread(t) {
475 p = &per_thread(rcu_stress_count,t)[0];
476 for (i = 0; i <= RCU_STRESS_PIPE_LEN; i++)
477 p[i] = 0LL;
478 }
479 rcu_stress_current = &rcu_stress_array[0];
480 rcu_stress_current->pipe_count = 0;
481 rcu_stress_current->mbtest = 1;
482 for (i = 0; i < nreaders; i++)
483 create_thread(rcu_read_stress_test, NULL);
484 create_thread(rcu_update_stress_test, NULL);
485 for (i = 0; i < 5; i++)
486 create_thread(rcu_fake_update_stress_test, NULL);
5481ddb3 487 cmm_smp_mb();
8a953620 488 goflag = GOFLAG_RUN;
5481ddb3 489 cmm_smp_mb();
8a953620 490 sleep(10);
5481ddb3 491 cmm_smp_mb();
8a953620 492 goflag = GOFLAG_STOP;
5481ddb3 493 cmm_smp_mb();
8a953620
MD
494 wait_all_threads();
495 for_each_thread(t)
496 n_reads += per_thread(n_reads_pt, t);
ad460058 497 diag("n_reads: %lld n_updates: %ld n_mberror: %d",
8a953620 498 n_reads, n_updates, n_mberror);
ad460058
MD
499 rdiag_start();
500 rdiag("rcu_stress_count:");
8a953620
MD
501 for (i = 0; i <= RCU_STRESS_PIPE_LEN; i++) {
502 sum = 0LL;
503 for_each_thread(t) {
504 sum += per_thread(rcu_stress_count, t)[i];
505 }
ad460058 506 rdiag(" %lld", sum);
8a953620 507 }
ad460058 508 rdiag_end();
7106ddf8 509 if (get_cpu_call_rcu_data(0)) {
ad460058 510 diag("Deallocating per-CPU call_rcu threads.");
7106ddf8
PM
511 free_all_cpu_call_rcu_data();
512 }
ad460058
MD
513 if (!n_mberror)
514 return 0;
515 else
516 return -1;
8a953620
MD
517}
518
519/*
520 * Mainprogram.
521 */
522
a142df4e 523static
6fa8b4f8 524void usage(char *argv[]) __attribute__((__noreturn__));
a142df4e 525
61c3fb60 526static
70469b43 527void usage(char *argv[])
8a953620 528{
26b5a74b 529 diag("Usage: %s nreaders [ perf | rperf | uperf | stress ] [ stride ] [ callrcu_global | callrcu_percpu | callrcu_perthread ]\n", argv[0]);
8a953620
MD
530 exit(-1);
531}
532
533int main(int argc, char *argv[])
534{
535 int nreaders = 1;
536 int cpustride = 1;
537
ad460058
MD
538 plan_tests(NR_TESTS);
539
8a953620
MD
540 smp_init();
541 //rcu_init();
26b5a74b
MD
542 if (argc > 4) {
543 const char *callrcu_str = argv[4];;
544
545 if (strcmp(callrcu_str, "callrcu_global") == 0) {
546 callrcu_type = CALLRCU_GLOBAL;
547 } else if (strcmp(callrcu_str, "callrcu_percpu") == 0) {
548 callrcu_type = CALLRCU_PERCPU;
549 } else if (strcmp(callrcu_str, "callrcu_perthread") == 0) {
550 callrcu_type = CALLRCU_PERTHREAD;
551 } else {
70469b43 552 usage(argv);
26b5a74b
MD
553 goto end;
554 }
555 }
929cfaff 556
26b5a74b
MD
557 switch (callrcu_type) {
558 case CALLRCU_GLOBAL:
559 diag("Using global per-process call_rcu thread.");
560 break;
561 case CALLRCU_PERCPU:
562 diag("Using per-CPU call_rcu threads.");
b57aee66 563 if (create_all_cpu_call_rcu_data(0))
ad460058
MD
564 diag("create_all_cpu_call_rcu_data: %s",
565 strerror(errno));
26b5a74b
MD
566 break;
567 case CALLRCU_PERTHREAD:
568 diag("Using per-thread call_rcu() worker.");
569 break;
570 default:
571 abort();
b57aee66 572 }
8a953620 573
9b171f46
MD
574#ifdef DEBUG_YIELD
575 yield_active |= YIELD_READ;
576 yield_active |= YIELD_WRITE;
577#endif
578
8a953620 579 if (argc > 1) {
26b5a74b
MD
580 if (strcmp(argv[1], "-h") == 0
581 || strcmp(argv[1], "--help") == 0) {
70469b43 582 usage(argv);
26b5a74b
MD
583 goto end;
584 }
8a953620 585 nreaders = strtoul(argv[1], NULL, 0);
ad460058
MD
586 if (argc == 2) {
587 ok(!perftest(nreaders, cpustride),
588 "perftest readers: %d, stride: %d",
589 nreaders, cpustride);
590 goto end;
591 }
8a953620
MD
592 if (argc > 3)
593 cpustride = strtoul(argv[3], NULL, 0);
594 if (strcmp(argv[2], "perf") == 0)
ad460058
MD
595 ok(!perftest(nreaders, cpustride),
596 "perftest readers: %d, stride: %d",
597 nreaders, cpustride);
8a953620 598 else if (strcmp(argv[2], "rperf") == 0)
ad460058
MD
599 ok(!rperftest(nreaders, cpustride),
600 "rperftest readers: %d, stride: %d",
601 nreaders, cpustride);
8a953620 602 else if (strcmp(argv[2], "uperf") == 0)
ad460058
MD
603 ok(!uperftest(nreaders, cpustride),
604 "uperftest readers: %d, stride: %d",
605 nreaders, cpustride);
8a953620 606 else if (strcmp(argv[2], "stress") == 0)
ad460058
MD
607 ok(!stresstest(nreaders),
608 "stresstest readers: %d, stride: %d",
609 nreaders, cpustride);
610 else
70469b43 611 usage(argv);
ad460058 612 } else {
70469b43 613 usage(argv);
8a953620 614 }
ad460058
MD
615end:
616 return exit_status();
8a953620 617}
This page took 0.075018 seconds and 4 git commands to generate.