2 * Read-Copy Update module-based performance-test facility
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, you can access it online at
16 * http://www.gnu.org/licenses/gpl-2.0.html.
18 * Copyright (C) IBM Corporation, 2015
20 * Authors: Paul E. McKenney <paulmck@us.ibm.com>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/kthread.h>
27 #include <linux/err.h>
28 #include <linux/spinlock.h>
29 #include <linux/smp.h>
30 #include <linux/rcupdate.h>
31 #include <linux/interrupt.h>
32 #include <linux/sched.h>
33 #include <uapi/linux/sched/types.h>
34 #include <linux/atomic.h>
35 #include <linux/bitops.h>
36 #include <linux/completion.h>
37 #include <linux/moduleparam.h>
38 #include <linux/percpu.h>
39 #include <linux/notifier.h>
40 #include <linux/reboot.h>
41 #include <linux/freezer.h>
42 #include <linux/cpu.h>
43 #include <linux/delay.h>
44 #include <linux/stat.h>
45 #include <linux/srcu.h>
46 #include <linux/slab.h>
47 #include <asm/byteorder.h>
48 #include <linux/torture.h>
49 #include <linux/vmalloc.h>
53 MODULE_LICENSE("GPL");
54 MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.vnet.ibm.com>");
56 #define PERF_FLAG "-perf:"
57 #define PERFOUT_STRING(s) \
58 pr_alert("%s" PERF_FLAG " %s\n", perf_type, s)
59 #define VERBOSE_PERFOUT_STRING(s) \
60 do { if (verbose) pr_alert("%s" PERF_FLAG " %s\n", perf_type, s); } while (0)
61 #define VERBOSE_PERFOUT_ERRSTRING(s) \
62 do { if (verbose) pr_alert("%s" PERF_FLAG "!!! %s\n", perf_type, s); } while (0)
64 torture_param(bool, gp_async, false, "Use asynchronous GP wait primitives");
65 torture_param(int, gp_async_max, 1000, "Max # outstanding waits per reader");
66 torture_param(bool, gp_exp, false, "Use expedited GP wait primitives");
67 torture_param(int, holdoff, 10, "Holdoff time before test start (s)");
68 torture_param(int, nreaders, 0, "Number of RCU reader threads");
69 torture_param(int, nwriters, -1, "Number of RCU updater threads");
70 torture_param(bool, shutdown, !IS_ENABLED(MODULE),
71 "Shutdown at end of performance tests.");
72 torture_param(bool, verbose, true, "Enable verbose debugging printk()s");
73 torture_param(int, writer_holdoff, 0, "Holdoff (us) between GPs, zero to disable");
75 static char *perf_type = "rcu";
76 module_param(perf_type, charp, 0444);
77 MODULE_PARM_DESC(perf_type, "Type of RCU to performance-test (rcu, rcu_bh, ...)");
79 static int nrealreaders;
80 static int nrealwriters;
81 static struct task_struct **writer_tasks;
82 static struct task_struct **reader_tasks;
83 static struct task_struct *shutdown_task;
85 static u64 **writer_durations;
86 static int *writer_n_durations;
87 static atomic_t n_rcu_perf_reader_started;
88 static atomic_t n_rcu_perf_writer_started;
89 static atomic_t n_rcu_perf_writer_finished;
90 static wait_queue_head_t shutdown_wq;
91 static u64 t_rcu_perf_writer_started;
92 static u64 t_rcu_perf_writer_finished;
93 static unsigned long b_rcu_perf_writer_started;
94 static unsigned long b_rcu_perf_writer_finished;
95 static DEFINE_PER_CPU(atomic_t, n_async_inflight);
97 static int rcu_perf_writer_state;
100 #define RTWS_BARRIER 2
101 #define RTWS_EXP_SYNC 3
104 #define RTWS_STOPPING 6
106 #define MAX_MEAS 10000
109 static int perf_runnable = IS_ENABLED(MODULE);
110 module_param(perf_runnable, int, 0444);
111 MODULE_PARM_DESC(perf_runnable, "Start rcuperf at boot");
114 * Operations vector for selecting different types of tests.
117 struct rcu_perf_ops {
120 void (*cleanup)(void);
121 int (*readlock)(void);
122 void (*readunlock)(int idx);
123 unsigned long (*started)(void);
124 unsigned long (*completed)(void);
125 unsigned long (*exp_completed)(void);
126 void (*async)(struct rcu_head *head, rcu_callback_t func);
127 void (*gp_barrier)(void);
129 void (*exp_sync)(void);
133 static struct rcu_perf_ops *cur_ops;
136 * Definitions for rcu perf testing.
139 static int rcu_perf_read_lock(void) __acquires(RCU)
145 static void rcu_perf_read_unlock(int idx) __releases(RCU)
150 static unsigned long __maybe_unused rcu_no_completed(void)
155 static void rcu_sync_perf_init(void)
159 static struct rcu_perf_ops rcu_ops = {
161 .init = rcu_sync_perf_init,
162 .readlock = rcu_perf_read_lock,
163 .readunlock = rcu_perf_read_unlock,
164 .started = rcu_batches_started,
165 .completed = rcu_batches_completed,
166 .exp_completed = rcu_exp_batches_completed,
168 .gp_barrier = rcu_barrier,
169 .sync = synchronize_rcu,
170 .exp_sync = synchronize_rcu_expedited,
175 * Definitions for rcu_bh perf testing.
178 static int rcu_bh_perf_read_lock(void) __acquires(RCU_BH)
184 static void rcu_bh_perf_read_unlock(int idx) __releases(RCU_BH)
186 rcu_read_unlock_bh();
189 static struct rcu_perf_ops rcu_bh_ops = {
190 .ptype = RCU_BH_FLAVOR,
191 .init = rcu_sync_perf_init,
192 .readlock = rcu_bh_perf_read_lock,
193 .readunlock = rcu_bh_perf_read_unlock,
194 .started = rcu_batches_started_bh,
195 .completed = rcu_batches_completed_bh,
196 .exp_completed = rcu_exp_batches_completed_sched,
197 .async = call_rcu_bh,
198 .gp_barrier = rcu_barrier_bh,
199 .sync = synchronize_rcu_bh,
200 .exp_sync = synchronize_rcu_bh_expedited,
205 * Definitions for srcu perf testing.
208 DEFINE_STATIC_SRCU(srcu_ctl_perf);
209 static struct srcu_struct *srcu_ctlp = &srcu_ctl_perf;
211 static int srcu_perf_read_lock(void) __acquires(srcu_ctlp)
213 return srcu_read_lock(srcu_ctlp);
216 static void srcu_perf_read_unlock(int idx) __releases(srcu_ctlp)
218 srcu_read_unlock(srcu_ctlp, idx);
221 static unsigned long srcu_perf_completed(void)
223 return srcu_batches_completed(srcu_ctlp);
226 static void srcu_call_rcu(struct rcu_head *head, rcu_callback_t func)
228 call_srcu(srcu_ctlp, head, func);
231 static void srcu_rcu_barrier(void)
233 srcu_barrier(srcu_ctlp);
236 static void srcu_perf_synchronize(void)
238 synchronize_srcu(srcu_ctlp);
241 static void srcu_perf_synchronize_expedited(void)
243 synchronize_srcu_expedited(srcu_ctlp);
246 static struct rcu_perf_ops srcu_ops = {
247 .ptype = SRCU_FLAVOR,
248 .init = rcu_sync_perf_init,
249 .readlock = srcu_perf_read_lock,
250 .readunlock = srcu_perf_read_unlock,
252 .completed = srcu_perf_completed,
253 .exp_completed = srcu_perf_completed,
254 .async = srcu_call_rcu,
255 .gp_barrier = srcu_rcu_barrier,
256 .sync = srcu_perf_synchronize,
257 .exp_sync = srcu_perf_synchronize_expedited,
261 static struct srcu_struct srcud;
263 static void srcu_sync_perf_init(void)
266 init_srcu_struct(srcu_ctlp);
269 static void srcu_sync_perf_cleanup(void)
271 cleanup_srcu_struct(srcu_ctlp);
274 static struct rcu_perf_ops srcud_ops = {
275 .ptype = SRCU_FLAVOR,
276 .init = srcu_sync_perf_init,
277 .cleanup = srcu_sync_perf_cleanup,
278 .readlock = srcu_perf_read_lock,
279 .readunlock = srcu_perf_read_unlock,
281 .completed = srcu_perf_completed,
282 .exp_completed = srcu_perf_completed,
283 .async = srcu_call_rcu,
284 .gp_barrier = srcu_rcu_barrier,
285 .sync = srcu_perf_synchronize,
286 .exp_sync = srcu_perf_synchronize_expedited,
291 * Definitions for sched perf testing.
294 static int sched_perf_read_lock(void)
300 static void sched_perf_read_unlock(int idx)
305 static struct rcu_perf_ops sched_ops = {
306 .ptype = RCU_SCHED_FLAVOR,
307 .init = rcu_sync_perf_init,
308 .readlock = sched_perf_read_lock,
309 .readunlock = sched_perf_read_unlock,
310 .started = rcu_batches_started_sched,
311 .completed = rcu_batches_completed_sched,
312 .exp_completed = rcu_exp_batches_completed_sched,
313 .async = call_rcu_sched,
314 .gp_barrier = rcu_barrier_sched,
315 .sync = synchronize_sched,
316 .exp_sync = synchronize_sched_expedited,
321 * Definitions for RCU-tasks perf testing.
324 static int tasks_perf_read_lock(void)
329 static void tasks_perf_read_unlock(int idx)
333 static struct rcu_perf_ops tasks_ops = {
334 .ptype = RCU_TASKS_FLAVOR,
335 .init = rcu_sync_perf_init,
336 .readlock = tasks_perf_read_lock,
337 .readunlock = tasks_perf_read_unlock,
338 .started = rcu_no_completed,
339 .completed = rcu_no_completed,
340 .async = call_rcu_tasks,
341 .gp_barrier = rcu_barrier_tasks,
342 .sync = synchronize_rcu_tasks,
343 .exp_sync = synchronize_rcu_tasks,
347 static bool __maybe_unused torturing_tasks(void)
349 return cur_ops == &tasks_ops;
353 * If performance tests complete, wait for shutdown to commence.
355 static void rcu_perf_wait_shutdown(void)
357 cond_resched_rcu_qs();
358 if (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters)
360 while (!torture_must_stop())
361 schedule_timeout_uninterruptible(1);
365 * RCU perf reader kthread. Repeatedly does empty RCU read-side
366 * critical section, minimizing update-side interference.
369 rcu_perf_reader(void *arg)
375 VERBOSE_PERFOUT_STRING("rcu_perf_reader task started");
376 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
377 set_user_nice(current, MAX_NICE);
378 atomic_inc(&n_rcu_perf_reader_started);
381 local_irq_save(flags);
382 idx = cur_ops->readlock();
383 cur_ops->readunlock(idx);
384 local_irq_restore(flags);
385 rcu_perf_wait_shutdown();
386 } while (!torture_must_stop());
387 torture_kthread_stopping("rcu_perf_reader");
392 * Callback function for asynchronous grace periods from rcu_perf_writer().
394 static void rcu_perf_async_cb(struct rcu_head *rhp)
396 atomic_dec(this_cpu_ptr(&n_async_inflight));
401 * RCU perf writer kthread. Repeatedly does a grace period.
404 rcu_perf_writer(void *arg)
409 struct rcu_head *rhp = NULL;
410 struct sched_param sp;
411 bool started = false, done = false, alldone = false;
414 u64 *wdpp = writer_durations[me];
416 VERBOSE_PERFOUT_STRING("rcu_perf_writer task started");
418 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
419 sp.sched_priority = 1;
420 sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
423 schedule_timeout_uninterruptible(holdoff * HZ);
425 t = ktime_get_mono_fast_ns();
426 if (atomic_inc_return(&n_rcu_perf_writer_started) >= nrealwriters) {
427 t_rcu_perf_writer_started = t;
429 b_rcu_perf_writer_started =
430 cur_ops->exp_completed() / 2;
432 b_rcu_perf_writer_started =
433 cur_ops->completed();
439 udelay(writer_holdoff);
441 *wdp = ktime_get_mono_fast_ns();
445 rhp = kmalloc(sizeof(*rhp), GFP_KERNEL);
446 if (rhp && atomic_read(this_cpu_ptr(&n_async_inflight)) < gp_async_max) {
447 rcu_perf_writer_state = RTWS_ASYNC;
448 atomic_inc(this_cpu_ptr(&n_async_inflight));
449 cur_ops->async(rhp, rcu_perf_async_cb);
451 } else if (!kthread_should_stop()) {
452 rcu_perf_writer_state = RTWS_BARRIER;
453 cur_ops->gp_barrier();
456 kfree(rhp); /* Because we are stopping. */
459 rcu_perf_writer_state = RTWS_EXP_SYNC;
462 rcu_perf_writer_state = RTWS_SYNC;
465 rcu_perf_writer_state = RTWS_IDLE;
466 t = ktime_get_mono_fast_ns();
470 atomic_read(&n_rcu_perf_writer_started) >= nrealwriters)
472 if (!done && i >= MIN_MEAS) {
474 sp.sched_priority = 0;
475 sched_setscheduler_nocheck(current,
477 pr_alert("%s%s rcu_perf_writer %ld has %d measurements\n",
478 perf_type, PERF_FLAG, me, MIN_MEAS);
479 if (atomic_inc_return(&n_rcu_perf_writer_finished) >=
481 schedule_timeout_interruptible(10);
482 rcu_ftrace_dump(DUMP_ALL);
483 PERFOUT_STRING("Test complete");
484 t_rcu_perf_writer_finished = t;
486 b_rcu_perf_writer_finished =
487 cur_ops->exp_completed() / 2;
489 b_rcu_perf_writer_finished =
490 cur_ops->completed();
493 smp_mb(); /* Assign before wake. */
494 wake_up(&shutdown_wq);
498 if (done && !alldone &&
499 atomic_read(&n_rcu_perf_writer_finished) >= nrealwriters)
501 if (started && !alldone && i < MAX_MEAS - 1)
503 rcu_perf_wait_shutdown();
504 } while (!torture_must_stop());
506 rcu_perf_writer_state = RTWS_BARRIER;
507 cur_ops->gp_barrier();
509 rcu_perf_writer_state = RTWS_STOPPING;
510 writer_n_durations[me] = i_max;
511 torture_kthread_stopping("rcu_perf_writer");
516 rcu_perf_print_module_parms(struct rcu_perf_ops *cur_ops, const char *tag)
518 pr_alert("%s" PERF_FLAG
519 "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
520 perf_type, tag, nrealreaders, nrealwriters, verbose, shutdown);
524 rcu_perf_cleanup(void)
533 * Would like warning at start, but everything is expedited
534 * during the mid-boot phase, so have to wait till the end.
536 if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp)
537 VERBOSE_PERFOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!");
538 if (rcu_gp_is_normal() && gp_exp)
539 VERBOSE_PERFOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!");
540 if (gp_exp && gp_async)
541 VERBOSE_PERFOUT_ERRSTRING("No expedited async GPs, so went with async!");
543 if (torture_cleanup_begin())
547 for (i = 0; i < nrealreaders; i++)
548 torture_stop_kthread(rcu_perf_reader,
554 for (i = 0; i < nrealwriters; i++) {
555 torture_stop_kthread(rcu_perf_writer,
557 if (!writer_n_durations)
559 j = writer_n_durations[i];
560 pr_alert("%s%s writer %d gps: %d\n",
561 perf_type, PERF_FLAG, i, j);
564 pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
565 perf_type, PERF_FLAG,
566 t_rcu_perf_writer_started, t_rcu_perf_writer_finished,
567 t_rcu_perf_writer_finished -
568 t_rcu_perf_writer_started,
570 b_rcu_perf_writer_finished -
571 b_rcu_perf_writer_started);
572 for (i = 0; i < nrealwriters; i++) {
573 if (!writer_durations)
575 if (!writer_n_durations)
577 wdpp = writer_durations[i];
580 for (j = 0; j <= writer_n_durations[i]; j++) {
582 pr_alert("%s%s %4d writer-duration: %5d %llu\n",
583 perf_type, PERF_FLAG,
586 schedule_timeout_uninterruptible(1);
588 kfree(writer_durations[i]);
591 kfree(writer_durations);
592 kfree(writer_n_durations);
595 /* Do flavor-specific cleanup operations. */
596 if (cur_ops->cleanup != NULL)
599 torture_cleanup_end();
603 * Return the number if non-negative. If -1, the number of CPUs.
604 * If less than -1, that much less than the number of CPUs, but
607 static int compute_real(int n)
614 nr = num_online_cpus() + 1 + n;
622 * RCU perf shutdown kthread. Just waits to be awakened, then shuts
626 rcu_perf_shutdown(void *arg)
629 wait_event(shutdown_wq,
630 atomic_read(&n_rcu_perf_writer_finished) >=
632 } while (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters);
633 smp_mb(); /* Wake before output. */
644 static struct rcu_perf_ops *perf_ops[] = {
645 &rcu_ops, &rcu_bh_ops, &srcu_ops, &srcud_ops, &sched_ops,
649 if (!torture_init_begin(perf_type, verbose, &perf_runnable))
652 /* Process args and tell the world that the perf'er is on the job. */
653 for (i = 0; i < ARRAY_SIZE(perf_ops); i++) {
654 cur_ops = perf_ops[i];
655 if (strcmp(perf_type, cur_ops->name) == 0)
658 if (i == ARRAY_SIZE(perf_ops)) {
659 pr_alert("rcu-perf: invalid perf type: \"%s\"\n",
661 pr_alert("rcu-perf types:");
662 for (i = 0; i < ARRAY_SIZE(perf_ops); i++)
663 pr_alert(" %s", perf_ops[i]->name);
671 nrealwriters = compute_real(nwriters);
672 nrealreaders = compute_real(nreaders);
673 atomic_set(&n_rcu_perf_reader_started, 0);
674 atomic_set(&n_rcu_perf_writer_started, 0);
675 atomic_set(&n_rcu_perf_writer_finished, 0);
676 rcu_perf_print_module_parms(cur_ops, "Start of test");
678 /* Start up the kthreads. */
681 init_waitqueue_head(&shutdown_wq);
682 firsterr = torture_create_kthread(rcu_perf_shutdown, NULL,
686 schedule_timeout_uninterruptible(1);
688 reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
690 if (reader_tasks == NULL) {
691 VERBOSE_PERFOUT_ERRSTRING("out of memory");
695 for (i = 0; i < nrealreaders; i++) {
696 firsterr = torture_create_kthread(rcu_perf_reader, (void *)i,
701 while (atomic_read(&n_rcu_perf_reader_started) < nrealreaders)
702 schedule_timeout_uninterruptible(1);
703 writer_tasks = kcalloc(nrealwriters, sizeof(reader_tasks[0]),
705 writer_durations = kcalloc(nrealwriters, sizeof(*writer_durations),
708 kcalloc(nrealwriters, sizeof(*writer_n_durations),
710 if (!writer_tasks || !writer_durations || !writer_n_durations) {
711 VERBOSE_PERFOUT_ERRSTRING("out of memory");
715 for (i = 0; i < nrealwriters; i++) {
716 writer_durations[i] =
717 kcalloc(MAX_MEAS, sizeof(*writer_durations[i]),
719 if (!writer_durations[i]) {
723 firsterr = torture_create_kthread(rcu_perf_writer, (void *)i,
737 module_init(rcu_perf_init);
738 module_exit(rcu_perf_cleanup);