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 <linux/atomic.h>
34 #include <linux/bitops.h>
35 #include <linux/completion.h>
36 #include <linux/moduleparam.h>
37 #include <linux/percpu.h>
38 #include <linux/notifier.h>
39 #include <linux/reboot.h>
40 #include <linux/freezer.h>
41 #include <linux/cpu.h>
42 #include <linux/delay.h>
43 #include <linux/stat.h>
44 #include <linux/srcu.h>
45 #include <linux/slab.h>
46 #include <asm/byteorder.h>
47 #include <linux/torture.h>
48 #include <linux/vmalloc.h>
50 MODULE_LICENSE("GPL");
51 MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.vnet.ibm.com>");
53 #define PERF_FLAG "-perf:"
54 #define PERFOUT_STRING(s) \
55 pr_alert("%s" PERF_FLAG " %s\n", perf_type, s)
56 #define VERBOSE_PERFOUT_STRING(s) \
57 do { if (verbose) pr_alert("%s" PERF_FLAG " %s\n", perf_type, s); } while (0)
58 #define VERBOSE_PERFOUT_ERRSTRING(s) \
59 do { if (verbose) pr_alert("%s" PERF_FLAG "!!! %s\n", perf_type, s); } while (0)
61 torture_param(bool, gp_exp
, false, "Use expedited GP wait primitives");
62 torture_param(int, holdoff
, 10, "Holdoff time before test start (s)");
63 torture_param(int, nreaders
, -1, "Number of RCU reader threads");
64 torture_param(int, nwriters
, -1, "Number of RCU updater threads");
65 torture_param(bool, shutdown
, false, "Shutdown at end of performance tests.");
66 torture_param(bool, verbose
, true, "Enable verbose debugging printk()s");
68 static char *perf_type
= "rcu";
69 module_param(perf_type
, charp
, 0444);
70 MODULE_PARM_DESC(perf_type
, "Type of RCU to performance-test (rcu, rcu_bh, ...)");
72 static int nrealreaders
;
73 static int nrealwriters
;
74 static struct task_struct
**writer_tasks
;
75 static struct task_struct
**reader_tasks
;
76 static struct task_struct
*shutdown_task
;
78 static u64
**writer_durations
;
79 static int *writer_n_durations
;
80 static atomic_t n_rcu_perf_reader_started
;
81 static atomic_t n_rcu_perf_writer_started
;
82 static atomic_t n_rcu_perf_writer_finished
;
83 static wait_queue_head_t shutdown_wq
;
84 static u64 t_rcu_perf_writer_started
;
85 static u64 t_rcu_perf_writer_finished
;
86 static unsigned long b_rcu_perf_writer_started
;
87 static unsigned long b_rcu_perf_writer_finished
;
89 static int rcu_perf_writer_state
;
91 #define RTWS_EXP_SYNC 1
94 #define RTWS_STOPPING 3
96 #define MAX_MEAS 10000
99 static int perf_runnable
= IS_ENABLED(MODULE
);
100 module_param(perf_runnable
, int, 0444);
101 MODULE_PARM_DESC(perf_runnable
, "Start rcuperf at boot");
104 * Operations vector for selecting different types of tests.
107 struct rcu_perf_ops
{
110 void (*cleanup
)(void);
111 int (*readlock
)(void);
112 void (*readunlock
)(int idx
);
113 unsigned long (*started
)(void);
114 unsigned long (*completed
)(void);
115 unsigned long (*exp_completed
)(void);
117 void (*exp_sync
)(void);
121 static struct rcu_perf_ops
*cur_ops
;
124 * Definitions for rcu perf testing.
127 static int rcu_perf_read_lock(void) __acquires(RCU
)
133 static void rcu_perf_read_unlock(int idx
) __releases(RCU
)
138 static unsigned long __maybe_unused
rcu_no_completed(void)
143 static void rcu_sync_perf_init(void)
147 static struct rcu_perf_ops rcu_ops
= {
149 .init
= rcu_sync_perf_init
,
150 .readlock
= rcu_perf_read_lock
,
151 .readunlock
= rcu_perf_read_unlock
,
152 .started
= rcu_batches_started
,
153 .completed
= rcu_batches_completed
,
154 .exp_completed
= rcu_exp_batches_completed
,
155 .sync
= synchronize_rcu
,
156 .exp_sync
= synchronize_rcu_expedited
,
161 * Definitions for rcu_bh perf testing.
164 static int rcu_bh_perf_read_lock(void) __acquires(RCU_BH
)
170 static void rcu_bh_perf_read_unlock(int idx
) __releases(RCU_BH
)
172 rcu_read_unlock_bh();
175 static struct rcu_perf_ops rcu_bh_ops
= {
176 .ptype
= RCU_BH_FLAVOR
,
177 .init
= rcu_sync_perf_init
,
178 .readlock
= rcu_bh_perf_read_lock
,
179 .readunlock
= rcu_bh_perf_read_unlock
,
180 .started
= rcu_batches_started_bh
,
181 .completed
= rcu_batches_completed_bh
,
182 .exp_completed
= rcu_exp_batches_completed_sched
,
183 .sync
= synchronize_rcu_bh
,
184 .exp_sync
= synchronize_rcu_bh_expedited
,
189 * Definitions for srcu perf testing.
192 DEFINE_STATIC_SRCU(srcu_ctl_perf
);
193 static struct srcu_struct
*srcu_ctlp
= &srcu_ctl_perf
;
195 static int srcu_perf_read_lock(void) __acquires(srcu_ctlp
)
197 return srcu_read_lock(srcu_ctlp
);
200 static void srcu_perf_read_unlock(int idx
) __releases(srcu_ctlp
)
202 srcu_read_unlock(srcu_ctlp
, idx
);
205 static unsigned long srcu_perf_completed(void)
207 return srcu_batches_completed(srcu_ctlp
);
210 static void srcu_perf_synchronize(void)
212 synchronize_srcu(srcu_ctlp
);
215 static void srcu_perf_synchronize_expedited(void)
217 synchronize_srcu_expedited(srcu_ctlp
);
220 static struct rcu_perf_ops srcu_ops
= {
221 .ptype
= SRCU_FLAVOR
,
222 .init
= rcu_sync_perf_init
,
223 .readlock
= srcu_perf_read_lock
,
224 .readunlock
= srcu_perf_read_unlock
,
226 .completed
= srcu_perf_completed
,
227 .exp_completed
= srcu_perf_completed
,
228 .sync
= srcu_perf_synchronize
,
229 .exp_sync
= srcu_perf_synchronize_expedited
,
234 * Definitions for sched perf testing.
237 static int sched_perf_read_lock(void)
243 static void sched_perf_read_unlock(int idx
)
248 static struct rcu_perf_ops sched_ops
= {
249 .ptype
= RCU_SCHED_FLAVOR
,
250 .init
= rcu_sync_perf_init
,
251 .readlock
= sched_perf_read_lock
,
252 .readunlock
= sched_perf_read_unlock
,
253 .started
= rcu_batches_started_sched
,
254 .completed
= rcu_batches_completed_sched
,
255 .exp_completed
= rcu_exp_batches_completed_sched
,
256 .sync
= synchronize_sched
,
257 .exp_sync
= synchronize_sched_expedited
,
261 #ifdef CONFIG_TASKS_RCU
264 * Definitions for RCU-tasks perf testing.
267 static int tasks_perf_read_lock(void)
272 static void tasks_perf_read_unlock(int idx
)
276 static struct rcu_perf_ops tasks_ops
= {
277 .ptype
= RCU_TASKS_FLAVOR
,
278 .init
= rcu_sync_perf_init
,
279 .readlock
= tasks_perf_read_lock
,
280 .readunlock
= tasks_perf_read_unlock
,
281 .started
= rcu_no_completed
,
282 .completed
= rcu_no_completed
,
283 .sync
= synchronize_rcu_tasks
,
284 .exp_sync
= synchronize_rcu_tasks
,
288 #define RCUPERF_TASKS_OPS &tasks_ops,
290 static bool __maybe_unused
torturing_tasks(void)
292 return cur_ops
== &tasks_ops
;
295 #else /* #ifdef CONFIG_TASKS_RCU */
297 #define RCUPERF_TASKS_OPS
299 static bool __maybe_unused
torturing_tasks(void)
304 #endif /* #else #ifdef CONFIG_TASKS_RCU */
307 * If performance tests complete, wait for shutdown to commence.
309 static void rcu_perf_wait_shutdown(void)
311 cond_resched_rcu_qs();
312 if (atomic_read(&n_rcu_perf_writer_finished
) < nrealwriters
)
314 while (!torture_must_stop())
315 schedule_timeout_uninterruptible(1);
319 * RCU perf reader kthread. Repeatedly does empty RCU read-side
320 * critical section, minimizing update-side interference.
323 rcu_perf_reader(void *arg
)
329 VERBOSE_PERFOUT_STRING("rcu_perf_reader task started");
330 set_cpus_allowed_ptr(current
, cpumask_of(me
% nr_cpu_ids
));
331 set_user_nice(current
, MAX_NICE
);
332 atomic_inc(&n_rcu_perf_reader_started
);
335 local_irq_save(flags
);
336 idx
= cur_ops
->readlock();
337 cur_ops
->readunlock(idx
);
338 local_irq_restore(flags
);
339 rcu_perf_wait_shutdown();
340 } while (!torture_must_stop());
341 torture_kthread_stopping("rcu_perf_reader");
346 * RCU perf writer kthread. Repeatedly does a grace period.
349 rcu_perf_writer(void *arg
)
354 struct sched_param sp
;
355 bool started
= false, done
= false, alldone
= false;
358 u64
*wdpp
= writer_durations
[me
];
360 VERBOSE_PERFOUT_STRING("rcu_perf_writer task started");
362 set_cpus_allowed_ptr(current
, cpumask_of(me
% nr_cpu_ids
));
363 sp
.sched_priority
= 1;
364 sched_setscheduler_nocheck(current
, SCHED_FIFO
, &sp
);
367 schedule_timeout_uninterruptible(holdoff
* HZ
);
369 t
= ktime_get_mono_fast_ns();
370 if (atomic_inc_return(&n_rcu_perf_writer_started
) >= nrealwriters
) {
371 t_rcu_perf_writer_started
= t
;
373 b_rcu_perf_writer_started
=
374 cur_ops
->exp_completed() / 2;
376 b_rcu_perf_writer_started
=
377 cur_ops
->completed();
383 *wdp
= ktime_get_mono_fast_ns();
385 rcu_perf_writer_state
= RTWS_EXP_SYNC
;
388 rcu_perf_writer_state
= RTWS_SYNC
;
391 rcu_perf_writer_state
= RTWS_IDLE
;
392 t
= ktime_get_mono_fast_ns();
396 atomic_read(&n_rcu_perf_writer_started
) >= nrealwriters
)
398 if (!done
&& i
>= MIN_MEAS
) {
400 sp
.sched_priority
= 0;
401 sched_setscheduler_nocheck(current
,
403 pr_alert("%s%s rcu_perf_writer %ld has %d measurements\n",
404 perf_type
, PERF_FLAG
, me
, MIN_MEAS
);
405 if (atomic_inc_return(&n_rcu_perf_writer_finished
) >=
407 schedule_timeout_interruptible(10);
408 rcu_ftrace_dump(DUMP_ALL
);
409 PERFOUT_STRING("Test complete");
410 t_rcu_perf_writer_finished
= t
;
412 b_rcu_perf_writer_finished
=
413 cur_ops
->exp_completed() / 2;
415 b_rcu_perf_writer_finished
=
416 cur_ops
->completed();
419 smp_mb(); /* Assign before wake. */
420 wake_up(&shutdown_wq
);
424 if (done
&& !alldone
&&
425 atomic_read(&n_rcu_perf_writer_finished
) >= nrealwriters
)
427 if (started
&& !alldone
&& i
< MAX_MEAS
- 1)
429 rcu_perf_wait_shutdown();
430 } while (!torture_must_stop());
431 rcu_perf_writer_state
= RTWS_STOPPING
;
432 writer_n_durations
[me
] = i_max
;
433 torture_kthread_stopping("rcu_perf_writer");
438 rcu_perf_print_module_parms(struct rcu_perf_ops
*cur_ops
, const char *tag
)
440 pr_alert("%s" PERF_FLAG
441 "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
442 perf_type
, tag
, nrealreaders
, nrealwriters
, verbose
, shutdown
);
446 rcu_perf_cleanup(void)
454 if (torture_cleanup_begin())
458 for (i
= 0; i
< nrealreaders
; i
++)
459 torture_stop_kthread(rcu_perf_reader
,
465 for (i
= 0; i
< nrealwriters
; i
++) {
466 torture_stop_kthread(rcu_perf_writer
,
468 if (!writer_n_durations
)
470 j
= writer_n_durations
[i
];
471 pr_alert("%s%s writer %d gps: %d\n",
472 perf_type
, PERF_FLAG
, i
, j
);
475 pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
476 perf_type
, PERF_FLAG
,
477 t_rcu_perf_writer_started
, t_rcu_perf_writer_finished
,
478 t_rcu_perf_writer_finished
-
479 t_rcu_perf_writer_started
,
481 b_rcu_perf_writer_finished
-
482 b_rcu_perf_writer_started
);
483 for (i
= 0; i
< nrealwriters
; i
++) {
484 if (!writer_durations
)
486 if (!writer_n_durations
)
488 wdpp
= writer_durations
[i
];
491 for (j
= 0; j
<= writer_n_durations
[i
]; j
++) {
493 pr_alert("%s%s %4d writer-duration: %5d %llu\n",
494 perf_type
, PERF_FLAG
,
497 schedule_timeout_uninterruptible(1);
499 kfree(writer_durations
[i
]);
502 kfree(writer_durations
);
503 kfree(writer_n_durations
);
506 /* Do flavor-specific cleanup operations. */
507 if (cur_ops
->cleanup
!= NULL
)
510 torture_cleanup_end();
514 * Return the number if non-negative. If -1, the number of CPUs.
515 * If less than -1, that much less than the number of CPUs, but
518 static int compute_real(int n
)
525 nr
= num_online_cpus() + 1 + n
;
533 * RCU perf shutdown kthread. Just waits to be awakened, then shuts
537 rcu_perf_shutdown(void *arg
)
540 wait_event(shutdown_wq
,
541 atomic_read(&n_rcu_perf_writer_finished
) >=
543 } while (atomic_read(&n_rcu_perf_writer_finished
) < nrealwriters
);
544 smp_mb(); /* Wake before output. */
555 static struct rcu_perf_ops
*perf_ops
[] = {
556 &rcu_ops
, &rcu_bh_ops
, &srcu_ops
, &sched_ops
,
560 if (!torture_init_begin(perf_type
, verbose
, &perf_runnable
))
563 /* Process args and tell the world that the perf'er is on the job. */
564 for (i
= 0; i
< ARRAY_SIZE(perf_ops
); i
++) {
565 cur_ops
= perf_ops
[i
];
566 if (strcmp(perf_type
, cur_ops
->name
) == 0)
569 if (i
== ARRAY_SIZE(perf_ops
)) {
570 pr_alert("rcu-perf: invalid perf type: \"%s\"\n",
572 pr_alert("rcu-perf types:");
573 for (i
= 0; i
< ARRAY_SIZE(perf_ops
); i
++)
574 pr_alert(" %s", perf_ops
[i
]->name
);
582 nrealwriters
= compute_real(nwriters
);
583 nrealreaders
= compute_real(nreaders
);
584 atomic_set(&n_rcu_perf_reader_started
, 0);
585 atomic_set(&n_rcu_perf_writer_started
, 0);
586 atomic_set(&n_rcu_perf_writer_finished
, 0);
587 rcu_perf_print_module_parms(cur_ops
, "Start of test");
589 /* Start up the kthreads. */
592 init_waitqueue_head(&shutdown_wq
);
593 firsterr
= torture_create_kthread(rcu_perf_shutdown
, NULL
,
597 schedule_timeout_uninterruptible(1);
599 reader_tasks
= kcalloc(nrealreaders
, sizeof(reader_tasks
[0]),
601 if (reader_tasks
== NULL
) {
602 VERBOSE_PERFOUT_ERRSTRING("out of memory");
606 for (i
= 0; i
< nrealreaders
; i
++) {
607 firsterr
= torture_create_kthread(rcu_perf_reader
, (void *)i
,
612 while (atomic_read(&n_rcu_perf_reader_started
) < nrealreaders
)
613 schedule_timeout_uninterruptible(1);
614 writer_tasks
= kcalloc(nrealwriters
, sizeof(reader_tasks
[0]),
616 writer_durations
= kcalloc(nrealwriters
, sizeof(*writer_durations
),
619 kcalloc(nrealwriters
, sizeof(*writer_n_durations
),
621 if (!writer_tasks
|| !writer_durations
|| !writer_n_durations
) {
622 VERBOSE_PERFOUT_ERRSTRING("out of memory");
626 if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp
) {
627 VERBOSE_PERFOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!");
631 if (rcu_gp_is_normal() && gp_exp
) {
632 VERBOSE_PERFOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!");
636 for (i
= 0; i
< nrealwriters
; i
++) {
637 writer_durations
[i
] =
638 kcalloc(MAX_MEAS
, sizeof(*writer_durations
[i
]),
640 if (!writer_durations
[i
]) {
644 firsterr
= torture_create_kthread(rcu_perf_writer
, (void *)i
,
658 module_init(rcu_perf_init
);
659 module_exit(rcu_perf_cleanup
);