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>
23 #define pr_fmt(fmt) fmt
25 #include <linux/types.h>
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/kthread.h>
30 #include <linux/err.h>
31 #include <linux/spinlock.h>
32 #include <linux/smp.h>
33 #include <linux/rcupdate.h>
34 #include <linux/interrupt.h>
35 #include <linux/sched.h>
36 #include <uapi/linux/sched/types.h>
37 #include <linux/atomic.h>
38 #include <linux/bitops.h>
39 #include <linux/completion.h>
40 #include <linux/moduleparam.h>
41 #include <linux/percpu.h>
42 #include <linux/notifier.h>
43 #include <linux/reboot.h>
44 #include <linux/freezer.h>
45 #include <linux/cpu.h>
46 #include <linux/delay.h>
47 #include <linux/stat.h>
48 #include <linux/srcu.h>
49 #include <linux/slab.h>
50 #include <asm/byteorder.h>
51 #include <linux/torture.h>
52 #include <linux/vmalloc.h>
56 MODULE_LICENSE("GPL");
57 MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.vnet.ibm.com>");
59 #define PERF_FLAG "-perf:"
60 #define PERFOUT_STRING(s) \
61 pr_alert("%s" PERF_FLAG " %s\n", perf_type, s)
62 #define VERBOSE_PERFOUT_STRING(s) \
63 do { if (verbose) pr_alert("%s" PERF_FLAG " %s\n", perf_type, s); } while (0)
64 #define VERBOSE_PERFOUT_ERRSTRING(s) \
65 do { if (verbose) pr_alert("%s" PERF_FLAG "!!! %s\n", perf_type, s); } while (0)
68 * The intended use cases for the nreaders and nwriters module parameters
71 * 1. Specify only the nr_cpus kernel boot parameter. This will
72 * set both nreaders and nwriters to the value specified by
73 * nr_cpus for a mixed reader/writer test.
75 * 2. Specify the nr_cpus kernel boot parameter, but set
76 * rcuperf.nreaders to zero. This will set nwriters to the
77 * value specified by nr_cpus for an update-only test.
79 * 3. Specify the nr_cpus kernel boot parameter, but set
80 * rcuperf.nwriters to zero. This will set nreaders to the
81 * value specified by nr_cpus for a read-only test.
83 * Various other use cases may of course be specified.
86 torture_param(bool, gp_async
, false, "Use asynchronous GP wait primitives");
87 torture_param(int, gp_async_max
, 1000, "Max # outstanding waits per reader");
88 torture_param(bool, gp_exp
, false, "Use expedited GP wait primitives");
89 torture_param(int, holdoff
, 10, "Holdoff time before test start (s)");
90 torture_param(int, nreaders
, -1, "Number of RCU reader threads");
91 torture_param(int, nwriters
, -1, "Number of RCU updater threads");
92 torture_param(bool, shutdown
, !IS_ENABLED(MODULE
),
93 "Shutdown at end of performance tests.");
94 torture_param(int, verbose
, 1, "Enable verbose debugging printk()s");
95 torture_param(int, writer_holdoff
, 0, "Holdoff (us) between GPs, zero to disable");
97 static char *perf_type
= "rcu";
98 module_param(perf_type
, charp
, 0444);
99 MODULE_PARM_DESC(perf_type
, "Type of RCU to performance-test (rcu, rcu_bh, ...)");
101 static int nrealreaders
;
102 static int nrealwriters
;
103 static struct task_struct
**writer_tasks
;
104 static struct task_struct
**reader_tasks
;
105 static struct task_struct
*shutdown_task
;
107 static u64
**writer_durations
;
108 static int *writer_n_durations
;
109 static atomic_t n_rcu_perf_reader_started
;
110 static atomic_t n_rcu_perf_writer_started
;
111 static atomic_t n_rcu_perf_writer_finished
;
112 static wait_queue_head_t shutdown_wq
;
113 static u64 t_rcu_perf_writer_started
;
114 static u64 t_rcu_perf_writer_finished
;
115 static unsigned long b_rcu_perf_writer_started
;
116 static unsigned long b_rcu_perf_writer_finished
;
117 static DEFINE_PER_CPU(atomic_t
, n_async_inflight
);
119 static int rcu_perf_writer_state
;
122 #define RTWS_BARRIER 2
123 #define RTWS_EXP_SYNC 3
126 #define RTWS_STOPPING 6
128 #define MAX_MEAS 10000
132 * Operations vector for selecting different types of tests.
135 struct rcu_perf_ops
{
138 void (*cleanup
)(void);
139 int (*readlock
)(void);
140 void (*readunlock
)(int idx
);
141 unsigned long (*get_gp_seq
)(void);
142 unsigned long (*gp_diff
)(unsigned long new, unsigned long old
);
143 unsigned long (*exp_completed
)(void);
144 void (*async
)(struct rcu_head
*head
, rcu_callback_t func
);
145 void (*gp_barrier
)(void);
147 void (*exp_sync
)(void);
151 static struct rcu_perf_ops
*cur_ops
;
154 * Definitions for rcu perf testing.
157 static int rcu_perf_read_lock(void) __acquires(RCU
)
163 static void rcu_perf_read_unlock(int idx
) __releases(RCU
)
168 static unsigned long __maybe_unused
rcu_no_completed(void)
173 static void rcu_sync_perf_init(void)
177 static struct rcu_perf_ops rcu_ops
= {
179 .init
= rcu_sync_perf_init
,
180 .readlock
= rcu_perf_read_lock
,
181 .readunlock
= rcu_perf_read_unlock
,
182 .get_gp_seq
= rcu_get_gp_seq
,
183 .gp_diff
= rcu_seq_diff
,
184 .exp_completed
= rcu_exp_batches_completed
,
186 .gp_barrier
= rcu_barrier
,
187 .sync
= synchronize_rcu
,
188 .exp_sync
= synchronize_rcu_expedited
,
193 * Definitions for srcu perf testing.
196 DEFINE_STATIC_SRCU(srcu_ctl_perf
);
197 static struct srcu_struct
*srcu_ctlp
= &srcu_ctl_perf
;
199 static int srcu_perf_read_lock(void) __acquires(srcu_ctlp
)
201 return srcu_read_lock(srcu_ctlp
);
204 static void srcu_perf_read_unlock(int idx
) __releases(srcu_ctlp
)
206 srcu_read_unlock(srcu_ctlp
, idx
);
209 static unsigned long srcu_perf_completed(void)
211 return srcu_batches_completed(srcu_ctlp
);
214 static void srcu_call_rcu(struct rcu_head
*head
, rcu_callback_t func
)
216 call_srcu(srcu_ctlp
, head
, func
);
219 static void srcu_rcu_barrier(void)
221 srcu_barrier(srcu_ctlp
);
224 static void srcu_perf_synchronize(void)
226 synchronize_srcu(srcu_ctlp
);
229 static void srcu_perf_synchronize_expedited(void)
231 synchronize_srcu_expedited(srcu_ctlp
);
234 static struct rcu_perf_ops srcu_ops
= {
235 .ptype
= SRCU_FLAVOR
,
236 .init
= rcu_sync_perf_init
,
237 .readlock
= srcu_perf_read_lock
,
238 .readunlock
= srcu_perf_read_unlock
,
239 .get_gp_seq
= srcu_perf_completed
,
240 .gp_diff
= rcu_seq_diff
,
241 .exp_completed
= srcu_perf_completed
,
242 .async
= srcu_call_rcu
,
243 .gp_barrier
= srcu_rcu_barrier
,
244 .sync
= srcu_perf_synchronize
,
245 .exp_sync
= srcu_perf_synchronize_expedited
,
249 static struct srcu_struct srcud
;
251 static void srcu_sync_perf_init(void)
254 init_srcu_struct(srcu_ctlp
);
257 static void srcu_sync_perf_cleanup(void)
259 cleanup_srcu_struct(srcu_ctlp
);
262 static struct rcu_perf_ops srcud_ops
= {
263 .ptype
= SRCU_FLAVOR
,
264 .init
= srcu_sync_perf_init
,
265 .cleanup
= srcu_sync_perf_cleanup
,
266 .readlock
= srcu_perf_read_lock
,
267 .readunlock
= srcu_perf_read_unlock
,
268 .get_gp_seq
= srcu_perf_completed
,
269 .gp_diff
= rcu_seq_diff
,
270 .exp_completed
= srcu_perf_completed
,
271 .async
= srcu_call_rcu
,
272 .gp_barrier
= srcu_rcu_barrier
,
273 .sync
= srcu_perf_synchronize
,
274 .exp_sync
= srcu_perf_synchronize_expedited
,
279 * Definitions for RCU-tasks perf testing.
282 static int tasks_perf_read_lock(void)
287 static void tasks_perf_read_unlock(int idx
)
291 static struct rcu_perf_ops tasks_ops
= {
292 .ptype
= RCU_TASKS_FLAVOR
,
293 .init
= rcu_sync_perf_init
,
294 .readlock
= tasks_perf_read_lock
,
295 .readunlock
= tasks_perf_read_unlock
,
296 .get_gp_seq
= rcu_no_completed
,
297 .gp_diff
= rcu_seq_diff
,
298 .async
= call_rcu_tasks
,
299 .gp_barrier
= rcu_barrier_tasks
,
300 .sync
= synchronize_rcu_tasks
,
301 .exp_sync
= synchronize_rcu_tasks
,
305 static unsigned long rcuperf_seq_diff(unsigned long new, unsigned long old
)
307 if (!cur_ops
->gp_diff
)
309 return cur_ops
->gp_diff(new, old
);
313 * If performance tests complete, wait for shutdown to commence.
315 static void rcu_perf_wait_shutdown(void)
317 cond_resched_tasks_rcu_qs();
318 if (atomic_read(&n_rcu_perf_writer_finished
) < nrealwriters
)
320 while (!torture_must_stop())
321 schedule_timeout_uninterruptible(1);
325 * RCU perf reader kthread. Repeatedly does empty RCU read-side
326 * critical section, minimizing update-side interference.
329 rcu_perf_reader(void *arg
)
335 VERBOSE_PERFOUT_STRING("rcu_perf_reader task started");
336 set_cpus_allowed_ptr(current
, cpumask_of(me
% nr_cpu_ids
));
337 set_user_nice(current
, MAX_NICE
);
338 atomic_inc(&n_rcu_perf_reader_started
);
341 local_irq_save(flags
);
342 idx
= cur_ops
->readlock();
343 cur_ops
->readunlock(idx
);
344 local_irq_restore(flags
);
345 rcu_perf_wait_shutdown();
346 } while (!torture_must_stop());
347 torture_kthread_stopping("rcu_perf_reader");
352 * Callback function for asynchronous grace periods from rcu_perf_writer().
354 static void rcu_perf_async_cb(struct rcu_head
*rhp
)
356 atomic_dec(this_cpu_ptr(&n_async_inflight
));
361 * RCU perf writer kthread. Repeatedly does a grace period.
364 rcu_perf_writer(void *arg
)
369 struct rcu_head
*rhp
= NULL
;
370 struct sched_param sp
;
371 bool started
= false, done
= false, alldone
= false;
374 u64
*wdpp
= writer_durations
[me
];
376 VERBOSE_PERFOUT_STRING("rcu_perf_writer task started");
378 set_cpus_allowed_ptr(current
, cpumask_of(me
% nr_cpu_ids
));
379 sp
.sched_priority
= 1;
380 sched_setscheduler_nocheck(current
, SCHED_FIFO
, &sp
);
383 schedule_timeout_uninterruptible(holdoff
* HZ
);
385 t
= ktime_get_mono_fast_ns();
386 if (atomic_inc_return(&n_rcu_perf_writer_started
) >= nrealwriters
) {
387 t_rcu_perf_writer_started
= t
;
389 b_rcu_perf_writer_started
=
390 cur_ops
->exp_completed() / 2;
392 b_rcu_perf_writer_started
= cur_ops
->get_gp_seq();
398 udelay(writer_holdoff
);
400 *wdp
= ktime_get_mono_fast_ns();
404 rhp
= kmalloc(sizeof(*rhp
), GFP_KERNEL
);
405 if (rhp
&& atomic_read(this_cpu_ptr(&n_async_inflight
)) < gp_async_max
) {
406 rcu_perf_writer_state
= RTWS_ASYNC
;
407 atomic_inc(this_cpu_ptr(&n_async_inflight
));
408 cur_ops
->async(rhp
, rcu_perf_async_cb
);
410 } else if (!kthread_should_stop()) {
411 rcu_perf_writer_state
= RTWS_BARRIER
;
412 cur_ops
->gp_barrier();
415 kfree(rhp
); /* Because we are stopping. */
418 rcu_perf_writer_state
= RTWS_EXP_SYNC
;
421 rcu_perf_writer_state
= RTWS_SYNC
;
424 rcu_perf_writer_state
= RTWS_IDLE
;
425 t
= ktime_get_mono_fast_ns();
429 atomic_read(&n_rcu_perf_writer_started
) >= nrealwriters
)
431 if (!done
&& i
>= MIN_MEAS
) {
433 sp
.sched_priority
= 0;
434 sched_setscheduler_nocheck(current
,
436 pr_alert("%s%s rcu_perf_writer %ld has %d measurements\n",
437 perf_type
, PERF_FLAG
, me
, MIN_MEAS
);
438 if (atomic_inc_return(&n_rcu_perf_writer_finished
) >=
440 schedule_timeout_interruptible(10);
441 rcu_ftrace_dump(DUMP_ALL
);
442 PERFOUT_STRING("Test complete");
443 t_rcu_perf_writer_finished
= t
;
445 b_rcu_perf_writer_finished
=
446 cur_ops
->exp_completed() / 2;
448 b_rcu_perf_writer_finished
=
449 cur_ops
->get_gp_seq();
452 smp_mb(); /* Assign before wake. */
453 wake_up(&shutdown_wq
);
457 if (done
&& !alldone
&&
458 atomic_read(&n_rcu_perf_writer_finished
) >= nrealwriters
)
460 if (started
&& !alldone
&& i
< MAX_MEAS
- 1)
462 rcu_perf_wait_shutdown();
463 } while (!torture_must_stop());
465 rcu_perf_writer_state
= RTWS_BARRIER
;
466 cur_ops
->gp_barrier();
468 rcu_perf_writer_state
= RTWS_STOPPING
;
469 writer_n_durations
[me
] = i_max
;
470 torture_kthread_stopping("rcu_perf_writer");
475 rcu_perf_print_module_parms(struct rcu_perf_ops
*cur_ops
, const char *tag
)
477 pr_alert("%s" PERF_FLAG
478 "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
479 perf_type
, tag
, nrealreaders
, nrealwriters
, verbose
, shutdown
);
483 rcu_perf_cleanup(void)
492 * Would like warning at start, but everything is expedited
493 * during the mid-boot phase, so have to wait till the end.
495 if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp
)
496 VERBOSE_PERFOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!");
497 if (rcu_gp_is_normal() && gp_exp
)
498 VERBOSE_PERFOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!");
499 if (gp_exp
&& gp_async
)
500 VERBOSE_PERFOUT_ERRSTRING("No expedited async GPs, so went with async!");
502 if (torture_cleanup_begin())
506 for (i
= 0; i
< nrealreaders
; i
++)
507 torture_stop_kthread(rcu_perf_reader
,
513 for (i
= 0; i
< nrealwriters
; i
++) {
514 torture_stop_kthread(rcu_perf_writer
,
516 if (!writer_n_durations
)
518 j
= writer_n_durations
[i
];
519 pr_alert("%s%s writer %d gps: %d\n",
520 perf_type
, PERF_FLAG
, i
, j
);
523 pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
524 perf_type
, PERF_FLAG
,
525 t_rcu_perf_writer_started
, t_rcu_perf_writer_finished
,
526 t_rcu_perf_writer_finished
-
527 t_rcu_perf_writer_started
,
529 rcuperf_seq_diff(b_rcu_perf_writer_finished
,
530 b_rcu_perf_writer_started
));
531 for (i
= 0; i
< nrealwriters
; i
++) {
532 if (!writer_durations
)
534 if (!writer_n_durations
)
536 wdpp
= writer_durations
[i
];
539 for (j
= 0; j
<= writer_n_durations
[i
]; j
++) {
541 pr_alert("%s%s %4d writer-duration: %5d %llu\n",
542 perf_type
, PERF_FLAG
,
545 schedule_timeout_uninterruptible(1);
547 kfree(writer_durations
[i
]);
550 kfree(writer_durations
);
551 kfree(writer_n_durations
);
554 /* Do torture-type-specific cleanup operations. */
555 if (cur_ops
->cleanup
!= NULL
)
558 torture_cleanup_end();
562 * Return the number if non-negative. If -1, the number of CPUs.
563 * If less than -1, that much less than the number of CPUs, but
566 static int compute_real(int n
)
573 nr
= num_online_cpus() + 1 + n
;
581 * RCU perf shutdown kthread. Just waits to be awakened, then shuts
585 rcu_perf_shutdown(void *arg
)
588 wait_event(shutdown_wq
,
589 atomic_read(&n_rcu_perf_writer_finished
) >=
591 } while (atomic_read(&n_rcu_perf_writer_finished
) < nrealwriters
);
592 smp_mb(); /* Wake before output. */
603 static struct rcu_perf_ops
*perf_ops
[] = {
604 &rcu_ops
, &srcu_ops
, &srcud_ops
, &tasks_ops
,
607 if (!torture_init_begin(perf_type
, verbose
))
610 /* Process args and tell the world that the perf'er is on the job. */
611 for (i
= 0; i
< ARRAY_SIZE(perf_ops
); i
++) {
612 cur_ops
= perf_ops
[i
];
613 if (strcmp(perf_type
, cur_ops
->name
) == 0)
616 if (i
== ARRAY_SIZE(perf_ops
)) {
617 pr_alert("rcu-perf: invalid perf type: \"%s\"\n", perf_type
);
618 pr_alert("rcu-perf types:");
619 for (i
= 0; i
< ARRAY_SIZE(perf_ops
); i
++)
620 pr_cont(" %s", perf_ops
[i
]->name
);
622 WARN_ON(!IS_MODULE(CONFIG_RCU_PERF_TEST
));
629 nrealwriters
= compute_real(nwriters
);
630 nrealreaders
= compute_real(nreaders
);
631 atomic_set(&n_rcu_perf_reader_started
, 0);
632 atomic_set(&n_rcu_perf_writer_started
, 0);
633 atomic_set(&n_rcu_perf_writer_finished
, 0);
634 rcu_perf_print_module_parms(cur_ops
, "Start of test");
636 /* Start up the kthreads. */
639 init_waitqueue_head(&shutdown_wq
);
640 firsterr
= torture_create_kthread(rcu_perf_shutdown
, NULL
,
644 schedule_timeout_uninterruptible(1);
646 reader_tasks
= kcalloc(nrealreaders
, sizeof(reader_tasks
[0]),
648 if (reader_tasks
== NULL
) {
649 VERBOSE_PERFOUT_ERRSTRING("out of memory");
653 for (i
= 0; i
< nrealreaders
; i
++) {
654 firsterr
= torture_create_kthread(rcu_perf_reader
, (void *)i
,
659 while (atomic_read(&n_rcu_perf_reader_started
) < nrealreaders
)
660 schedule_timeout_uninterruptible(1);
661 writer_tasks
= kcalloc(nrealwriters
, sizeof(reader_tasks
[0]),
663 writer_durations
= kcalloc(nrealwriters
, sizeof(*writer_durations
),
666 kcalloc(nrealwriters
, sizeof(*writer_n_durations
),
668 if (!writer_tasks
|| !writer_durations
|| !writer_n_durations
) {
669 VERBOSE_PERFOUT_ERRSTRING("out of memory");
673 for (i
= 0; i
< nrealwriters
; i
++) {
674 writer_durations
[i
] =
675 kcalloc(MAX_MEAS
, sizeof(*writer_durations
[i
]),
677 if (!writer_durations
[i
]) {
681 firsterr
= torture_create_kthread(rcu_perf_writer
, (void *)i
,
695 module_init(rcu_perf_init
);
696 module_exit(rcu_perf_cleanup
);