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)
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
, true, "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 #if defined(MODULE) || defined(CONFIG_RCU_PERF_TEST_RUNNABLE)
100 #define RCUPERF_RUNNABLE_INIT 1
102 #define RCUPERF_RUNNABLE_INIT 0
104 static int perf_runnable
= RCUPERF_RUNNABLE_INIT
;
105 module_param(perf_runnable
, int, 0444);
106 MODULE_PARM_DESC(perf_runnable
, "Start rcuperf at boot");
109 * Operations vector for selecting different types of tests.
112 struct rcu_perf_ops
{
115 void (*cleanup
)(void);
116 int (*readlock
)(void);
117 void (*readunlock
)(int idx
);
118 unsigned long (*started
)(void);
119 unsigned long (*completed
)(void);
120 unsigned long (*exp_completed
)(void);
122 void (*exp_sync
)(void);
126 static struct rcu_perf_ops
*cur_ops
;
129 * Definitions for rcu perf testing.
132 static int rcu_perf_read_lock(void) __acquires(RCU
)
138 static void rcu_perf_read_unlock(int idx
) __releases(RCU
)
143 static unsigned long __maybe_unused
rcu_no_completed(void)
148 static void rcu_sync_perf_init(void)
152 static struct rcu_perf_ops rcu_ops
= {
154 .init
= rcu_sync_perf_init
,
155 .readlock
= rcu_perf_read_lock
,
156 .readunlock
= rcu_perf_read_unlock
,
157 .started
= rcu_batches_started
,
158 .completed
= rcu_batches_completed
,
159 .exp_completed
= rcu_exp_batches_completed
,
160 .sync
= synchronize_rcu
,
161 .exp_sync
= synchronize_rcu_expedited
,
166 * Definitions for rcu_bh perf testing.
169 static int rcu_bh_perf_read_lock(void) __acquires(RCU_BH
)
175 static void rcu_bh_perf_read_unlock(int idx
) __releases(RCU_BH
)
177 rcu_read_unlock_bh();
180 static struct rcu_perf_ops rcu_bh_ops
= {
181 .ptype
= RCU_BH_FLAVOR
,
182 .init
= rcu_sync_perf_init
,
183 .readlock
= rcu_bh_perf_read_lock
,
184 .readunlock
= rcu_bh_perf_read_unlock
,
185 .started
= rcu_batches_started_bh
,
186 .completed
= rcu_batches_completed_bh
,
187 .exp_completed
= rcu_exp_batches_completed_sched
,
188 .sync
= synchronize_rcu_bh
,
189 .exp_sync
= synchronize_rcu_bh_expedited
,
194 * Definitions for srcu perf testing.
197 DEFINE_STATIC_SRCU(srcu_ctl_perf
);
198 static struct srcu_struct
*srcu_ctlp
= &srcu_ctl_perf
;
200 static int srcu_perf_read_lock(void) __acquires(srcu_ctlp
)
202 return srcu_read_lock(srcu_ctlp
);
205 static void srcu_perf_read_unlock(int idx
) __releases(srcu_ctlp
)
207 srcu_read_unlock(srcu_ctlp
, idx
);
210 static unsigned long srcu_perf_completed(void)
212 return srcu_batches_completed(srcu_ctlp
);
215 static void srcu_perf_synchronize(void)
217 synchronize_srcu(srcu_ctlp
);
220 static void srcu_perf_synchronize_expedited(void)
222 synchronize_srcu_expedited(srcu_ctlp
);
225 static struct rcu_perf_ops srcu_ops
= {
226 .ptype
= SRCU_FLAVOR
,
227 .init
= rcu_sync_perf_init
,
228 .readlock
= srcu_perf_read_lock
,
229 .readunlock
= srcu_perf_read_unlock
,
231 .completed
= srcu_perf_completed
,
232 .exp_completed
= srcu_perf_completed
,
233 .sync
= srcu_perf_synchronize
,
234 .exp_sync
= srcu_perf_synchronize_expedited
,
239 * Definitions for sched perf testing.
242 static int sched_perf_read_lock(void)
248 static void sched_perf_read_unlock(int idx
)
253 static struct rcu_perf_ops sched_ops
= {
254 .ptype
= RCU_SCHED_FLAVOR
,
255 .init
= rcu_sync_perf_init
,
256 .readlock
= sched_perf_read_lock
,
257 .readunlock
= sched_perf_read_unlock
,
258 .started
= rcu_batches_started_sched
,
259 .completed
= rcu_batches_completed_sched
,
260 .exp_completed
= rcu_exp_batches_completed_sched
,
261 .sync
= synchronize_sched
,
262 .exp_sync
= synchronize_sched_expedited
,
266 #ifdef CONFIG_TASKS_RCU
269 * Definitions for RCU-tasks perf testing.
272 static int tasks_perf_read_lock(void)
277 static void tasks_perf_read_unlock(int idx
)
281 static struct rcu_perf_ops tasks_ops
= {
282 .ptype
= RCU_TASKS_FLAVOR
,
283 .init
= rcu_sync_perf_init
,
284 .readlock
= tasks_perf_read_lock
,
285 .readunlock
= tasks_perf_read_unlock
,
286 .started
= rcu_no_completed
,
287 .completed
= rcu_no_completed
,
288 .sync
= synchronize_rcu_tasks
,
289 .exp_sync
= synchronize_rcu_tasks
,
293 #define RCUPERF_TASKS_OPS &tasks_ops,
295 static bool __maybe_unused
torturing_tasks(void)
297 return cur_ops
== &tasks_ops
;
300 #else /* #ifdef CONFIG_TASKS_RCU */
302 #define RCUPERF_TASKS_OPS
304 static bool __maybe_unused
torturing_tasks(void)
309 #endif /* #else #ifdef CONFIG_TASKS_RCU */
312 * If performance tests complete, wait for shutdown to commence.
314 static void rcu_perf_wait_shutdown(void)
316 cond_resched_rcu_qs();
317 if (atomic_read(&n_rcu_perf_writer_finished
) < nrealwriters
)
319 while (!torture_must_stop())
320 schedule_timeout_uninterruptible(1);
324 * RCU perf reader kthread. Repeatedly does empty RCU read-side
325 * critical section, minimizing update-side interference.
328 rcu_perf_reader(void *arg
)
334 VERBOSE_PERFOUT_STRING("rcu_perf_reader task started");
335 set_cpus_allowed_ptr(current
, cpumask_of(me
% nr_cpu_ids
));
336 set_user_nice(current
, MAX_NICE
);
337 atomic_inc(&n_rcu_perf_reader_started
);
340 local_irq_save(flags
);
341 idx
= cur_ops
->readlock();
342 cur_ops
->readunlock(idx
);
343 local_irq_restore(flags
);
344 rcu_perf_wait_shutdown();
345 } while (!torture_must_stop());
346 torture_kthread_stopping("rcu_perf_reader");
351 * RCU perf writer kthread. Repeatedly does a grace period.
354 rcu_perf_writer(void *arg
)
359 struct sched_param sp
;
360 bool started
= false, done
= false, alldone
= false;
363 u64
*wdpp
= writer_durations
[me
];
365 VERBOSE_PERFOUT_STRING("rcu_perf_writer task started");
366 WARN_ON(rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp
);
367 WARN_ON(rcu_gp_is_normal() && gp_exp
);
369 set_cpus_allowed_ptr(current
, cpumask_of(me
% nr_cpu_ids
));
370 sp
.sched_priority
= 1;
371 sched_setscheduler_nocheck(current
, SCHED_FIFO
, &sp
);
374 schedule_timeout_uninterruptible(holdoff
* HZ
);
376 t
= ktime_get_mono_fast_ns();
377 if (atomic_inc_return(&n_rcu_perf_writer_started
) >= nrealwriters
) {
378 t_rcu_perf_writer_started
= t
;
380 b_rcu_perf_writer_started
=
381 cur_ops
->exp_completed() / 2;
383 b_rcu_perf_writer_started
=
384 cur_ops
->completed();
390 *wdp
= ktime_get_mono_fast_ns();
392 rcu_perf_writer_state
= RTWS_EXP_SYNC
;
395 rcu_perf_writer_state
= RTWS_SYNC
;
398 rcu_perf_writer_state
= RTWS_IDLE
;
399 t
= ktime_get_mono_fast_ns();
403 atomic_read(&n_rcu_perf_writer_started
) >= nrealwriters
)
405 if (!done
&& i
>= MIN_MEAS
) {
407 sp
.sched_priority
= 0;
408 sched_setscheduler_nocheck(current
,
410 pr_alert("%s" PERF_FLAG
411 "rcu_perf_writer %ld has %d measurements\n",
412 perf_type
, me
, MIN_MEAS
);
413 if (atomic_inc_return(&n_rcu_perf_writer_finished
) >=
415 schedule_timeout_interruptible(10);
416 rcu_ftrace_dump(DUMP_ALL
);
417 PERFOUT_STRING("Test complete");
418 t_rcu_perf_writer_finished
= t
;
420 b_rcu_perf_writer_finished
=
421 cur_ops
->exp_completed() / 2;
423 b_rcu_perf_writer_finished
=
424 cur_ops
->completed();
427 smp_mb(); /* Assign before wake. */
428 wake_up(&shutdown_wq
);
432 if (done
&& !alldone
&&
433 atomic_read(&n_rcu_perf_writer_finished
) >= nrealwriters
)
435 if (started
&& !alldone
&& i
< MAX_MEAS
- 1)
437 rcu_perf_wait_shutdown();
438 } while (!torture_must_stop());
439 rcu_perf_writer_state
= RTWS_STOPPING
;
440 writer_n_durations
[me
] = i_max
;
441 torture_kthread_stopping("rcu_perf_writer");
446 rcu_perf_print_module_parms(struct rcu_perf_ops
*cur_ops
, const char *tag
)
448 pr_alert("%s" PERF_FLAG
449 "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
450 perf_type
, tag
, nrealreaders
, nrealwriters
, verbose
, shutdown
);
454 rcu_perf_cleanup(void)
462 if (torture_cleanup_begin())
466 for (i
= 0; i
< nrealreaders
; i
++)
467 torture_stop_kthread(rcu_perf_reader
,
473 for (i
= 0; i
< nrealwriters
; i
++) {
474 torture_stop_kthread(rcu_perf_writer
,
476 if (!writer_n_durations
)
478 j
= writer_n_durations
[i
];
479 pr_alert("%s%s writer %d gps: %d\n",
480 perf_type
, PERF_FLAG
, i
, j
);
483 pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
484 perf_type
, PERF_FLAG
,
485 t_rcu_perf_writer_started
, t_rcu_perf_writer_finished
,
486 t_rcu_perf_writer_finished
-
487 t_rcu_perf_writer_started
,
489 b_rcu_perf_writer_finished
-
490 b_rcu_perf_writer_started
);
491 for (i
= 0; i
< nrealwriters
; i
++) {
492 if (!writer_durations
)
494 if (!writer_n_durations
)
496 wdpp
= writer_durations
[i
];
499 for (j
= 0; j
<= writer_n_durations
[i
]; j
++) {
501 pr_alert("%s%s %4d writer-duration: %5d %llu\n",
502 perf_type
, PERF_FLAG
,
505 schedule_timeout_uninterruptible(1);
507 kfree(writer_durations
[i
]);
510 kfree(writer_durations
);
511 kfree(writer_n_durations
);
514 /* Do flavor-specific cleanup operations. */
515 if (cur_ops
->cleanup
!= NULL
)
518 torture_cleanup_end();
522 * Return the number if non-negative. If -1, the number of CPUs.
523 * If less than -1, that much less than the number of CPUs, but
526 static int compute_real(int n
)
533 nr
= num_online_cpus() + 1 + n
;
541 * RCU perf shutdown kthread. Just waits to be awakened, then shuts
545 rcu_perf_shutdown(void *arg
)
548 wait_event(shutdown_wq
,
549 atomic_read(&n_rcu_perf_writer_finished
) >=
551 } while (atomic_read(&n_rcu_perf_writer_finished
) < nrealwriters
);
552 smp_mb(); /* Wake before output. */
563 static struct rcu_perf_ops
*perf_ops
[] = {
564 &rcu_ops
, &rcu_bh_ops
, &srcu_ops
, &sched_ops
,
568 if (!torture_init_begin(perf_type
, verbose
, &perf_runnable
))
571 /* Process args and tell the world that the perf'er is on the job. */
572 for (i
= 0; i
< ARRAY_SIZE(perf_ops
); i
++) {
573 cur_ops
= perf_ops
[i
];
574 if (strcmp(perf_type
, cur_ops
->name
) == 0)
577 if (i
== ARRAY_SIZE(perf_ops
)) {
578 pr_alert("rcu-perf: invalid perf type: \"%s\"\n",
580 pr_alert("rcu-perf types:");
581 for (i
= 0; i
< ARRAY_SIZE(perf_ops
); i
++)
582 pr_alert(" %s", perf_ops
[i
]->name
);
590 nrealwriters
= compute_real(nwriters
);
591 nrealreaders
= compute_real(nreaders
);
592 atomic_set(&n_rcu_perf_reader_started
, 0);
593 atomic_set(&n_rcu_perf_writer_started
, 0);
594 atomic_set(&n_rcu_perf_writer_finished
, 0);
595 rcu_perf_print_module_parms(cur_ops
, "Start of test");
597 /* Start up the kthreads. */
600 init_waitqueue_head(&shutdown_wq
);
601 firsterr
= torture_create_kthread(rcu_perf_shutdown
, NULL
,
605 schedule_timeout_uninterruptible(1);
607 reader_tasks
= kcalloc(nrealreaders
, sizeof(reader_tasks
[0]),
609 if (reader_tasks
== NULL
) {
610 VERBOSE_PERFOUT_ERRSTRING("out of memory");
614 for (i
= 0; i
< nrealreaders
; i
++) {
615 firsterr
= torture_create_kthread(rcu_perf_reader
, (void *)i
,
620 while (atomic_read(&n_rcu_perf_reader_started
) < nrealreaders
)
621 schedule_timeout_uninterruptible(1);
622 writer_tasks
= kcalloc(nrealwriters
, sizeof(reader_tasks
[0]),
624 writer_durations
= kcalloc(nrealwriters
, sizeof(*writer_durations
),
627 kcalloc(nrealwriters
, sizeof(*writer_n_durations
),
629 if (!writer_tasks
|| !writer_durations
|| !writer_n_durations
) {
630 VERBOSE_PERFOUT_ERRSTRING("out of memory");
634 for (i
= 0; i
< nrealwriters
; i
++) {
635 writer_durations
[i
] =
636 kcalloc(MAX_MEAS
, sizeof(*writer_durations
[i
]),
638 if (!writer_durations
[i
])
640 firsterr
= torture_create_kthread(rcu_perf_writer
, (void *)i
,
654 module_init(rcu_perf_init
);
655 module_exit(rcu_perf_cleanup
);