uatomic/x86: Remove redundant memory barriers
[urcu.git] / tests / regression / rcutorture.h
1 // SPDX-FileCopyrightText: 2008 Paul E. McKenney, IBM Corporation.
2 //
3 // SPDX-License-Identifier: GPL-2.0-or-later
4
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.
50 *
51 * rcu_stress_count: Histogram of "ages" of structures seen by readers. If any
52 * entries past the first two are non-zero, RCU is broken. The age of a newly
53 * allocated structure is zero, it becomes one when removed from reader
54 * visibility, and is incremented once per grace period subsequently -- and is
55 * freed after passing through (RCU_STRESS_PIPE_LEN-2) grace periods. Since
56 * this tests only has one true writer (there are fake writers), only buckets at
57 * indexes 0 and 1 should be none-zero.
58 *
59 * This program is free software; you can redistribute it and/or modify
60 * it under the terms of the GNU General Public License as published by
61 * the Free Software Foundation; either version 2 of the License, or
62 * (at your option) any later version.
63 *
64 * This program is distributed in the hope that it will be useful,
65 * but WITHOUT ANY WARRANTY; without even the implied warranty of
66 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
67 * GNU General Public License for more details.
68 *
69 * You should have received a copy of the GNU General Public License
70 * along with this program; if not, write to the Free Software
71 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
72 *
73 * Copyright (c) 2008 Paul E. McKenney, IBM Corporation.
74 */
75
76 /*
77 * Test variables.
78 */
79
80 #include <stdlib.h>
81 #include "tap.h"
82
83 #include <urcu/uatomic.h>
84
85 #include "urcu-wait.h"
86
87 #define NR_TESTS 1
88
89 DEFINE_PER_THREAD(long long, n_reads_pt);
90 DEFINE_PER_THREAD(long long, n_updates_pt);
91
92 enum callrcu_type {
93 CALLRCU_GLOBAL,
94 CALLRCU_PERCPU,
95 CALLRCU_PERTHREAD,
96 };
97
98 enum writer_state {
99 WRITER_STATE_SYNC_RCU,
100 WRITER_STATE_CALL_RCU,
101 WRITER_STATE_POLL_RCU,
102 };
103
104 static enum callrcu_type callrcu_type = CALLRCU_GLOBAL;
105
106 long long n_reads = 0LL;
107 long n_updates = 0L;
108 int nthreadsrunning;
109 char argsbuf[64];
110
111 #define GOFLAG_INIT 0
112 #define GOFLAG_RUN 1
113 #define GOFLAG_STOP 2
114
115 volatile int goflag __attribute__((__aligned__(CAA_CACHE_LINE_SIZE)))
116 = GOFLAG_INIT;
117
118 #define RCU_READ_RUN 1000
119
120 //MD
121 #define RCU_READ_NESTABLE
122
123 #ifdef RCU_READ_NESTABLE
124 #define rcu_read_lock_nest() rcu_read_lock()
125 #define rcu_read_unlock_nest() rcu_read_unlock()
126 #else /* #ifdef RCU_READ_NESTABLE */
127 #define rcu_read_lock_nest()
128 #define rcu_read_unlock_nest()
129 #endif /* #else #ifdef RCU_READ_NESTABLE */
130
131 #ifdef TORTURE_QSBR
132 #define mark_rcu_quiescent_state rcu_quiescent_state
133 #define put_thread_offline rcu_thread_offline
134 #define put_thread_online rcu_thread_online
135 #endif
136
137 #ifndef mark_rcu_quiescent_state
138 #define mark_rcu_quiescent_state() do {} while (0)
139 #endif /* #ifdef mark_rcu_quiescent_state */
140
141 #ifndef put_thread_offline
142 #define put_thread_offline() do {} while (0)
143 #define put_thread_online() do {} while (0)
144 #define put_thread_online_delay() do {} while (0)
145 #else /* #ifndef put_thread_offline */
146 #define put_thread_online_delay() synchronize_rcu()
147 #endif /* #else #ifndef put_thread_offline */
148
149 /*
150 * Performance test.
151 */
152
153 static
154 void *rcu_read_perf_test(void *arg)
155 {
156 int i;
157 int me = (long)arg;
158 long long n_reads_local = 0;
159
160 rcu_register_thread();
161 run_on(me);
162 uatomic_inc(&nthreadsrunning);
163 put_thread_offline();
164 while (uatomic_read(&goflag) == GOFLAG_INIT)
165 (void) poll(NULL, 0, 1);
166 put_thread_online();
167 while (uatomic_read(&goflag) == GOFLAG_RUN) {
168 for (i = 0; i < RCU_READ_RUN; i++) {
169 rcu_read_lock();
170 /* rcu_read_lock_nest(); */
171 /* rcu_read_unlock_nest(); */
172 rcu_read_unlock();
173 }
174 n_reads_local += RCU_READ_RUN;
175 mark_rcu_quiescent_state();
176 }
177 __get_thread_var(n_reads_pt) += n_reads_local;
178 put_thread_offline();
179 rcu_unregister_thread();
180
181 return (NULL);
182 }
183
184 static
185 void *rcu_update_perf_test(void *arg __attribute__((unused)))
186 {
187 long long n_updates_local = 0;
188
189 if (callrcu_type == CALLRCU_PERTHREAD) {
190 struct call_rcu_data *crdp;
191
192 crdp = create_call_rcu_data(0, -1);
193 if (crdp != NULL) {
194 diag("Successfully using per-thread call_rcu() worker.");
195 set_thread_call_rcu_data(crdp);
196 }
197 }
198 uatomic_inc(&nthreadsrunning);
199 while (uatomic_read(&goflag) == GOFLAG_INIT)
200 (void) poll(NULL, 0, 1);
201 while (uatomic_read(&goflag) == GOFLAG_RUN) {
202 synchronize_rcu();
203 n_updates_local++;
204 }
205 __get_thread_var(n_updates_pt) += n_updates_local;
206 if (callrcu_type == CALLRCU_PERTHREAD) {
207 struct call_rcu_data *crdp;
208
209 crdp = get_thread_call_rcu_data();
210 set_thread_call_rcu_data(NULL);
211 call_rcu_data_free(crdp);
212 }
213 return NULL;
214 }
215
216 static
217 void perftestinit(void)
218 {
219 init_per_thread(n_reads_pt, 0LL);
220 init_per_thread(n_updates_pt, 0LL);
221 uatomic_set(&nthreadsrunning, 0);
222 }
223
224 static
225 int perftestrun(int nthreads, int nreaders, int nupdaters)
226 {
227 int t;
228 int duration = 1;
229
230 while (uatomic_read(&nthreadsrunning) < nthreads)
231 (void) poll(NULL, 0, 1);
232 uatomic_set(&goflag, GOFLAG_RUN);
233 sleep(duration);
234 uatomic_set(&goflag, GOFLAG_STOP);
235 wait_all_threads();
236 for_each_thread(t) {
237 n_reads += per_thread(n_reads_pt, t);
238 n_updates += per_thread(n_updates_pt, t);
239 }
240 diag("n_reads: %lld n_updates: %ld nreaders: %d nupdaters: %d duration: %d",
241 n_reads, n_updates, nreaders, nupdaters, duration);
242 diag("ns/read: %g ns/update: %g",
243 ((duration * 1000*1000*1000.*(double)nreaders) /
244 (double)n_reads),
245 ((duration * 1000*1000*1000.*(double)nupdaters) /
246 (double)n_updates));
247 if (get_cpu_call_rcu_data(0)) {
248 diag("Deallocating per-CPU call_rcu threads.\n");
249 free_all_cpu_call_rcu_data();
250 }
251 return 0;
252 }
253
254 static
255 int perftest(int nreaders, int cpustride)
256 {
257 int i;
258 long arg;
259
260 perftestinit();
261 for (i = 0; i < nreaders; i++) {
262 arg = (long)(i * cpustride);
263 create_thread(rcu_read_perf_test, (void *)arg);
264 }
265 arg = (long)(i * cpustride);
266 create_thread(rcu_update_perf_test, (void *)arg);
267 return perftestrun(i + 1, nreaders, 1);
268 }
269
270 static
271 int rperftest(int nreaders, int cpustride)
272 {
273 int i;
274 long arg;
275
276 perftestinit();
277 init_per_thread(n_reads_pt, 0LL);
278 for (i = 0; i < nreaders; i++) {
279 arg = (long)(i * cpustride);
280 create_thread(rcu_read_perf_test, (void *)arg);
281 }
282 return perftestrun(i, nreaders, 0);
283 }
284
285 static
286 int uperftest(int nupdaters, int cpustride)
287 {
288 int i;
289 long arg;
290
291 perftestinit();
292 init_per_thread(n_reads_pt, 0LL);
293 for (i = 0; i < nupdaters; i++) {
294 arg = (long)(i * cpustride);
295 create_thread(rcu_update_perf_test, (void *)arg);
296 }
297 return perftestrun(i, 0, nupdaters);
298 }
299
300 /*
301 * Stress test.
302 */
303
304 #define RCU_STRESS_PIPE_LEN 10
305
306 struct rcu_stress {
307 int pipe_count;
308 int mbtest;
309 };
310
311 struct rcu_stress rcu_stress_array[RCU_STRESS_PIPE_LEN] = { { 0, 0 } };
312 struct rcu_stress *rcu_stress_current;
313 int rcu_stress_idx = 0;
314
315 /*
316 * How many time a reader has seen something that should not be visible. It is
317 * an error if this value is different than zero at the end of the stress test.
318 *
319 * Here, the something that should not be visibile is an old pipe that has been
320 * freed (mbtest = 0).
321 */
322 int n_mberror = 0;
323 DEFINE_PER_THREAD(long long [RCU_STRESS_PIPE_LEN + 1], rcu_stress_count);
324
325 int garbage = 0;
326
327 static
328 void *rcu_read_stress_test(void *arg __attribute__((unused)))
329 {
330 int i;
331 int itercnt = 0;
332 struct rcu_stress *p;
333 int pc;
334
335 rcu_register_thread();
336 put_thread_offline();
337 while (uatomic_read(&goflag) == GOFLAG_INIT)
338 (void) poll(NULL, 0, 1);
339 put_thread_online();
340 while (uatomic_read(&goflag) == GOFLAG_RUN) {
341 rcu_read_lock();
342 p = rcu_dereference(rcu_stress_current);
343 if (p->mbtest == 0)
344 uatomic_inc_mo(&n_mberror, CMM_RELAXED);
345 rcu_read_lock_nest();
346 /*
347 * The value of garbage is nothing important. This is
348 * essentially a busy loop. The atomic operation -- while not
349 * important here -- helps tools such as TSAN to not flag this
350 * as a race condition.
351 */
352 for (i = 0; i < 100; i++)
353 uatomic_inc(&garbage);
354 rcu_read_unlock_nest();
355 pc = uatomic_read(&p->pipe_count);
356 rcu_read_unlock();
357 if ((pc > RCU_STRESS_PIPE_LEN) || (pc < 0))
358 pc = RCU_STRESS_PIPE_LEN;
359 __get_thread_var(rcu_stress_count)[pc]++;
360 __get_thread_var(n_reads_pt)++;
361 mark_rcu_quiescent_state();
362 if ((++itercnt % 0x1000) == 0) {
363 put_thread_offline();
364 put_thread_online_delay();
365 put_thread_online();
366 }
367 }
368 put_thread_offline();
369 rcu_unregister_thread();
370
371 return (NULL);
372 }
373
374 static DEFINE_URCU_WAIT_QUEUE(call_rcu_waiters);
375
376 static
377 void rcu_update_stress_test_rcu(struct rcu_head *head __attribute__((unused)))
378 {
379 struct urcu_waiters waiters;
380
381 urcu_move_waiters(&waiters, &call_rcu_waiters);
382 urcu_wake_all_waiters(&waiters);
383 }
384
385 static
386 void advance_writer_state(enum writer_state *state)
387 {
388 switch (*state) {
389 case WRITER_STATE_SYNC_RCU:
390 *state = WRITER_STATE_CALL_RCU;
391 break;
392 case WRITER_STATE_CALL_RCU:
393 *state = WRITER_STATE_POLL_RCU;
394 break;
395 case WRITER_STATE_POLL_RCU:
396 *state = WRITER_STATE_SYNC_RCU;
397 break;
398 }
399 }
400
401 static
402 void *rcu_update_stress_test(void *arg __attribute__((unused)))
403 {
404 int i;
405 struct rcu_stress *p, *old_p;
406 struct rcu_head rh;
407 enum writer_state writer_state = WRITER_STATE_SYNC_RCU;
408
409 rcu_register_thread();
410
411 /* Offline for poll. */
412 put_thread_offline();
413 while (uatomic_read(&goflag) == GOFLAG_INIT)
414 (void) poll(NULL, 0, 1);
415 put_thread_online();
416
417 old_p = NULL;
418 while (uatomic_read(&goflag) == GOFLAG_RUN) {
419 i = rcu_stress_idx + 1;
420 if (i >= RCU_STRESS_PIPE_LEN)
421 i = 0;
422
423 rcu_read_lock();
424 old_p = rcu_dereference(rcu_stress_current);
425 rcu_read_unlock();
426
427 /*
428 * Allocate a new pipe.
429 */
430 p = &rcu_stress_array[i];
431 p->pipe_count = 0;
432 p->mbtest = 1;
433
434 rcu_assign_pointer(rcu_stress_current, p);
435 rcu_stress_idx = i;
436
437 /*
438 * Increment every pipe except the freshly allocated one. A
439 * reader should only see either the old pipe or the new
440 * pipe. This is reflected in the rcu_stress_count histogram.
441 */
442 for (i = 0; i < RCU_STRESS_PIPE_LEN; i++)
443 if (i != rcu_stress_idx)
444 uatomic_inc(&rcu_stress_array[i].pipe_count);
445
446 switch (writer_state) {
447 case WRITER_STATE_SYNC_RCU:
448 synchronize_rcu();
449 break;
450 case WRITER_STATE_CALL_RCU:
451 {
452 DEFINE_URCU_WAIT_NODE(wait, URCU_WAIT_WAITING);
453
454 urcu_wait_add(&call_rcu_waiters, &wait);
455
456 call_rcu(&rh, rcu_update_stress_test_rcu);
457
458 /* Offline for busy-wait. */
459 put_thread_offline();
460 urcu_adaptative_busy_wait(&wait);
461 put_thread_online();
462 break;
463 }
464 case WRITER_STATE_POLL_RCU:
465 {
466 struct urcu_gp_poll_state poll_state;
467
468 poll_state = start_poll_synchronize_rcu();
469
470 /* Offline for poll. */
471 put_thread_offline();
472 while (!poll_state_synchronize_rcu(poll_state))
473 (void) poll(NULL, 0, 1); /* Wait for 1ms */
474 put_thread_online();
475 break;
476 }
477 }
478 /*
479 * No readers should see that old pipe now. Setting mbtest to 0
480 * to mark it as "freed".
481 */
482 if (old_p) {
483 old_p->mbtest = 0;
484 }
485 old_p = p;
486 n_updates++;
487 advance_writer_state(&writer_state);
488 }
489
490 rcu_unregister_thread();
491
492 return NULL;
493 }
494
495 static
496 void *rcu_fake_update_stress_test(void *arg __attribute__((unused)))
497 {
498 if (callrcu_type == CALLRCU_PERTHREAD) {
499 struct call_rcu_data *crdp;
500
501 crdp = create_call_rcu_data(0, -1);
502 if (crdp != NULL) {
503 diag("Successfully using per-thread call_rcu() worker.");
504 set_thread_call_rcu_data(crdp);
505 }
506 }
507 while (uatomic_read(&goflag) == GOFLAG_INIT)
508 (void) poll(NULL, 0, 1);
509 while (uatomic_read(&goflag) == GOFLAG_RUN) {
510 synchronize_rcu();
511 (void) poll(NULL, 0, 1);
512 }
513 if (callrcu_type == CALLRCU_PERTHREAD) {
514 struct call_rcu_data *crdp;
515
516 crdp = get_thread_call_rcu_data();
517 set_thread_call_rcu_data(NULL);
518 call_rcu_data_free(crdp);
519 }
520 return NULL;
521 }
522
523 static
524 int stresstest(int nreaders)
525 {
526 int i;
527 int t;
528 long long *p;
529 long long sum;
530 int ret;
531
532 init_per_thread(n_reads_pt, 0LL);
533 for_each_thread(t) {
534 p = &per_thread(rcu_stress_count,t)[0];
535 for (i = 0; i <= RCU_STRESS_PIPE_LEN; i++)
536 p[i] = 0LL;
537 }
538 rcu_stress_current = &rcu_stress_array[0];
539 rcu_stress_current->pipe_count = 0;
540 rcu_stress_current->mbtest = 1;
541 for (i = 0; i < nreaders; i++)
542 create_thread(rcu_read_stress_test, NULL);
543 create_thread(rcu_update_stress_test, NULL);
544 for (i = 0; i < 5; i++)
545 create_thread(rcu_fake_update_stress_test, NULL);
546 uatomic_set(&goflag, GOFLAG_RUN);
547 sleep(10);
548 uatomic_set(&goflag, GOFLAG_STOP);
549 wait_all_threads();
550 for_each_thread(t)
551 n_reads += per_thread(n_reads_pt, t);
552 diag("n_reads: %lld n_updates: %ld n_mberror: %d",
553 n_reads, n_updates, n_mberror);
554 rdiag_start();
555 rdiag("rcu_stress_count:");
556 ret = 0;
557 for (i = 0; i <= RCU_STRESS_PIPE_LEN; i++) {
558 sum = 0LL;
559 for_each_thread(t) {
560 sum += per_thread(rcu_stress_count, t)[i];
561 }
562 /*
563 * If any entries past the first two are non-zero, RCU is
564 * broken. See details above about rcu_stress_count.
565 */
566 if (i > 1 && sum != 0) {
567 ret = -1;
568 }
569 rdiag(" %lld", sum);
570 }
571 rdiag_end();
572 if (get_cpu_call_rcu_data(0)) {
573 diag("Deallocating per-CPU call_rcu threads.");
574 free_all_cpu_call_rcu_data();
575 }
576 if (n_mberror)
577 ret = -1;
578 return ret;
579 }
580
581 /*
582 * Mainprogram.
583 */
584
585 static
586 void usage(char *argv[]) __attribute__((__noreturn__));
587
588 static
589 void usage(char *argv[])
590 {
591 diag("Usage: %s nreaders [ perf | rperf | uperf | stress ] [ stride ] [ callrcu_global | callrcu_percpu | callrcu_perthread ]\n", argv[0]);
592 exit(-1);
593 }
594
595 int main(int argc, char *argv[])
596 {
597 int nreaders = 1;
598 int cpustride = 1;
599
600 plan_tests(NR_TESTS);
601
602 smp_init();
603 //rcu_init();
604 if (argc > 4) {
605 const char *callrcu_str = argv[4];;
606
607 if (strcmp(callrcu_str, "callrcu_global") == 0) {
608 callrcu_type = CALLRCU_GLOBAL;
609 } else if (strcmp(callrcu_str, "callrcu_percpu") == 0) {
610 callrcu_type = CALLRCU_PERCPU;
611 } else if (strcmp(callrcu_str, "callrcu_perthread") == 0) {
612 callrcu_type = CALLRCU_PERTHREAD;
613 } else {
614 usage(argv);
615 goto end;
616 }
617 }
618
619 switch (callrcu_type) {
620 case CALLRCU_GLOBAL:
621 diag("Using global per-process call_rcu thread.");
622 break;
623 case CALLRCU_PERCPU:
624 diag("Using per-CPU call_rcu threads.");
625 if (create_all_cpu_call_rcu_data(0))
626 diag("create_all_cpu_call_rcu_data: %s",
627 strerror(errno));
628 break;
629 case CALLRCU_PERTHREAD:
630 diag("Using per-thread call_rcu() worker.");
631 break;
632 default:
633 abort();
634 }
635
636 #ifdef DEBUG_YIELD
637 yield_active |= YIELD_READ;
638 yield_active |= YIELD_WRITE;
639 #endif
640
641 if (argc > 1) {
642 if (strcmp(argv[1], "-h") == 0
643 || strcmp(argv[1], "--help") == 0) {
644 usage(argv);
645 goto end;
646 }
647 nreaders = strtoul(argv[1], NULL, 0);
648 if (argc == 2) {
649 ok(!perftest(nreaders, cpustride),
650 "perftest readers: %d, stride: %d",
651 nreaders, cpustride);
652 goto end;
653 }
654 if (argc > 3)
655 cpustride = strtoul(argv[3], NULL, 0);
656 if (strcmp(argv[2], "perf") == 0)
657 ok(!perftest(nreaders, cpustride),
658 "perftest readers: %d, stride: %d",
659 nreaders, cpustride);
660 else if (strcmp(argv[2], "rperf") == 0)
661 ok(!rperftest(nreaders, cpustride),
662 "rperftest readers: %d, stride: %d",
663 nreaders, cpustride);
664 else if (strcmp(argv[2], "uperf") == 0)
665 ok(!uperftest(nreaders, cpustride),
666 "uperftest readers: %d, stride: %d",
667 nreaders, cpustride);
668 else if (strcmp(argv[2], "stress") == 0)
669 ok(!stresstest(nreaders),
670 "stresstest readers: %d, stride: %d",
671 nreaders, cpustride);
672 else
673 usage(argv);
674 } else {
675 usage(argv);
676 }
677 end:
678 return exit_status();
679 }
This page took 0.046088 seconds and 5 git commands to generate.