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>
51 MODULE_LICENSE("GPL");
52 MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.vnet.ibm.com>");
54 #define PERF_FLAG "-perf:"
55 #define PERFOUT_STRING(s) \
56 pr_alert("%s" PERF_FLAG " %s\n", perf_type, s)
57 #define VERBOSE_PERFOUT_STRING(s) \
58 do { if (verbose) pr_alert("%s" PERF_FLAG " %s\n", perf_type, s); } while (0)
59 #define VERBOSE_PERFOUT_ERRSTRING(s) \
60 do { if (verbose) pr_alert("%s" PERF_FLAG "!!! %s\n", perf_type, s); } while (0)
62 torture_param(bool, gp_exp
, false, "Use expedited GP wait primitives");
63 torture_param(int, holdoff
, 10, "Holdoff time before test start (s)");
64 torture_param(int, nreaders
, -1, "Number of RCU reader threads");
65 torture_param(int, nwriters
, -1, "Number of RCU updater threads");
66 torture_param(bool, shutdown
, false, "Shutdown at end of performance tests.");
67 torture_param(bool, verbose
, true, "Enable verbose debugging printk()s");
69 static char *perf_type
= "rcu";
70 module_param(perf_type
, charp
, 0444);
71 MODULE_PARM_DESC(perf_type
, "Type of RCU to performance-test (rcu, rcu_bh, ...)");
73 static int nrealreaders
;
74 static int nrealwriters
;
75 static struct task_struct
**writer_tasks
;
76 static struct task_struct
**reader_tasks
;
77 static struct task_struct
*shutdown_task
;
79 static u64
**writer_durations
;
80 static int *writer_n_durations
;
81 static atomic_t n_rcu_perf_reader_started
;
82 static atomic_t n_rcu_perf_writer_started
;
83 static atomic_t n_rcu_perf_writer_finished
;
84 static wait_queue_head_t shutdown_wq
;
85 static u64 t_rcu_perf_writer_started
;
86 static u64 t_rcu_perf_writer_finished
;
87 static unsigned long b_rcu_perf_writer_started
;
88 static unsigned long b_rcu_perf_writer_finished
;
90 static int rcu_perf_writer_state
;
92 #define RTWS_EXP_SYNC 1
95 #define RTWS_STOPPING 3
97 #define MAX_MEAS 10000
100 static int perf_runnable
= IS_ENABLED(MODULE
);
101 module_param(perf_runnable
, int, 0444);
102 MODULE_PARM_DESC(perf_runnable
, "Start rcuperf at boot");
105 * Operations vector for selecting different types of tests.
108 struct rcu_perf_ops
{
111 void (*cleanup
)(void);
112 int (*readlock
)(void);
113 void (*readunlock
)(int idx
);
114 unsigned long (*started
)(void);
115 unsigned long (*completed
)(void);
116 unsigned long (*exp_completed
)(void);
118 void (*exp_sync
)(void);
122 static struct rcu_perf_ops
*cur_ops
;
125 * Definitions for rcu perf testing.
128 static int rcu_perf_read_lock(void) __acquires(RCU
)
134 static void rcu_perf_read_unlock(int idx
) __releases(RCU
)
139 static unsigned long __maybe_unused
rcu_no_completed(void)
144 static void rcu_sync_perf_init(void)
148 static struct rcu_perf_ops rcu_ops
= {
150 .init
= rcu_sync_perf_init
,
151 .readlock
= rcu_perf_read_lock
,
152 .readunlock
= rcu_perf_read_unlock
,
153 .started
= rcu_batches_started
,
154 .completed
= rcu_batches_completed
,
155 .exp_completed
= rcu_exp_batches_completed
,
156 .sync
= synchronize_rcu
,
157 .exp_sync
= synchronize_rcu_expedited
,
162 * Definitions for rcu_bh perf testing.
165 static int rcu_bh_perf_read_lock(void) __acquires(RCU_BH
)
171 static void rcu_bh_perf_read_unlock(int idx
) __releases(RCU_BH
)
173 rcu_read_unlock_bh();
176 static struct rcu_perf_ops rcu_bh_ops
= {
177 .ptype
= RCU_BH_FLAVOR
,
178 .init
= rcu_sync_perf_init
,
179 .readlock
= rcu_bh_perf_read_lock
,
180 .readunlock
= rcu_bh_perf_read_unlock
,
181 .started
= rcu_batches_started_bh
,
182 .completed
= rcu_batches_completed_bh
,
183 .exp_completed
= rcu_exp_batches_completed_sched
,
184 .sync
= synchronize_rcu_bh
,
185 .exp_sync
= synchronize_rcu_bh_expedited
,
190 * Definitions for srcu perf testing.
193 DEFINE_STATIC_SRCU(srcu_ctl_perf
);
194 static struct srcu_struct
*srcu_ctlp
= &srcu_ctl_perf
;
196 static int srcu_perf_read_lock(void) __acquires(srcu_ctlp
)
198 return srcu_read_lock(srcu_ctlp
);
201 static void srcu_perf_read_unlock(int idx
) __releases(srcu_ctlp
)
203 srcu_read_unlock(srcu_ctlp
, idx
);
206 static unsigned long srcu_perf_completed(void)
208 return srcu_batches_completed(srcu_ctlp
);
211 static void srcu_perf_synchronize(void)
213 synchronize_srcu(srcu_ctlp
);
216 static void srcu_perf_synchronize_expedited(void)
218 synchronize_srcu_expedited(srcu_ctlp
);
221 static struct rcu_perf_ops srcu_ops
= {
222 .ptype
= SRCU_FLAVOR
,
223 .init
= rcu_sync_perf_init
,
224 .readlock
= srcu_perf_read_lock
,
225 .readunlock
= srcu_perf_read_unlock
,
227 .completed
= srcu_perf_completed
,
228 .exp_completed
= srcu_perf_completed
,
229 .sync
= srcu_perf_synchronize
,
230 .exp_sync
= srcu_perf_synchronize_expedited
,
235 * Definitions for sched perf testing.
238 static int sched_perf_read_lock(void)
244 static void sched_perf_read_unlock(int idx
)
249 static struct rcu_perf_ops sched_ops
= {
250 .ptype
= RCU_SCHED_FLAVOR
,
251 .init
= rcu_sync_perf_init
,
252 .readlock
= sched_perf_read_lock
,
253 .readunlock
= sched_perf_read_unlock
,
254 .started
= rcu_batches_started_sched
,
255 .completed
= rcu_batches_completed_sched
,
256 .exp_completed
= rcu_exp_batches_completed_sched
,
257 .sync
= synchronize_sched
,
258 .exp_sync
= synchronize_sched_expedited
,
262 #ifdef CONFIG_TASKS_RCU
265 * Definitions for RCU-tasks perf testing.
268 static int tasks_perf_read_lock(void)
273 static void tasks_perf_read_unlock(int idx
)
277 static struct rcu_perf_ops tasks_ops
= {
278 .ptype
= RCU_TASKS_FLAVOR
,
279 .init
= rcu_sync_perf_init
,
280 .readlock
= tasks_perf_read_lock
,
281 .readunlock
= tasks_perf_read_unlock
,
282 .started
= rcu_no_completed
,
283 .completed
= rcu_no_completed
,
284 .sync
= synchronize_rcu_tasks
,
285 .exp_sync
= synchronize_rcu_tasks
,
289 #define RCUPERF_TASKS_OPS &tasks_ops,
291 static bool __maybe_unused
torturing_tasks(void)
293 return cur_ops
== &tasks_ops
;
296 #else /* #ifdef CONFIG_TASKS_RCU */
298 #define RCUPERF_TASKS_OPS
300 static bool __maybe_unused
torturing_tasks(void)
305 #endif /* #else #ifdef CONFIG_TASKS_RCU */
308 * If performance tests complete, wait for shutdown to commence.
310 static void rcu_perf_wait_shutdown(void)
312 cond_resched_rcu_qs();
313 if (atomic_read(&n_rcu_perf_writer_finished
) < nrealwriters
)
315 while (!torture_must_stop())
316 schedule_timeout_uninterruptible(1);
320 * RCU perf reader kthread. Repeatedly does empty RCU read-side
321 * critical section, minimizing update-side interference.
324 rcu_perf_reader(void *arg
)
330 VERBOSE_PERFOUT_STRING("rcu_perf_reader task started");
331 set_cpus_allowed_ptr(current
, cpumask_of(me
% nr_cpu_ids
));
332 set_user_nice(current
, MAX_NICE
);
333 atomic_inc(&n_rcu_perf_reader_started
);
336 local_irq_save(flags
);
337 idx
= cur_ops
->readlock();
338 cur_ops
->readunlock(idx
);
339 local_irq_restore(flags
);
340 rcu_perf_wait_shutdown();
341 } while (!torture_must_stop());
342 torture_kthread_stopping("rcu_perf_reader");
347 * RCU perf writer kthread. Repeatedly does a grace period.
350 rcu_perf_writer(void *arg
)
355 struct sched_param sp
;
356 bool started
= false, done
= false, alldone
= false;
359 u64
*wdpp
= writer_durations
[me
];
361 VERBOSE_PERFOUT_STRING("rcu_perf_writer task started");
363 set_cpus_allowed_ptr(current
, cpumask_of(me
% nr_cpu_ids
));
364 sp
.sched_priority
= 1;
365 sched_setscheduler_nocheck(current
, SCHED_FIFO
, &sp
);
368 schedule_timeout_uninterruptible(holdoff
* HZ
);
370 t
= ktime_get_mono_fast_ns();
371 if (atomic_inc_return(&n_rcu_perf_writer_started
) >= nrealwriters
) {
372 t_rcu_perf_writer_started
= t
;
374 b_rcu_perf_writer_started
=
375 cur_ops
->exp_completed() / 2;
377 b_rcu_perf_writer_started
=
378 cur_ops
->completed();
384 *wdp
= ktime_get_mono_fast_ns();
386 rcu_perf_writer_state
= RTWS_EXP_SYNC
;
389 rcu_perf_writer_state
= RTWS_SYNC
;
392 rcu_perf_writer_state
= RTWS_IDLE
;
393 t
= ktime_get_mono_fast_ns();
397 atomic_read(&n_rcu_perf_writer_started
) >= nrealwriters
)
399 if (!done
&& i
>= MIN_MEAS
) {
401 sp
.sched_priority
= 0;
402 sched_setscheduler_nocheck(current
,
404 pr_alert("%s%s rcu_perf_writer %ld has %d measurements\n",
405 perf_type
, PERF_FLAG
, me
, MIN_MEAS
);
406 if (atomic_inc_return(&n_rcu_perf_writer_finished
) >=
408 schedule_timeout_interruptible(10);
409 rcu_ftrace_dump(DUMP_ALL
);
410 PERFOUT_STRING("Test complete");
411 t_rcu_perf_writer_finished
= t
;
413 b_rcu_perf_writer_finished
=
414 cur_ops
->exp_completed() / 2;
416 b_rcu_perf_writer_finished
=
417 cur_ops
->completed();
420 smp_mb(); /* Assign before wake. */
421 wake_up(&shutdown_wq
);
425 if (done
&& !alldone
&&
426 atomic_read(&n_rcu_perf_writer_finished
) >= nrealwriters
)
428 if (started
&& !alldone
&& i
< MAX_MEAS
- 1)
430 rcu_perf_wait_shutdown();
431 } while (!torture_must_stop());
432 rcu_perf_writer_state
= RTWS_STOPPING
;
433 writer_n_durations
[me
] = i_max
;
434 torture_kthread_stopping("rcu_perf_writer");
439 rcu_perf_print_module_parms(struct rcu_perf_ops
*cur_ops
, const char *tag
)
441 pr_alert("%s" PERF_FLAG
442 "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
443 perf_type
, tag
, nrealreaders
, nrealwriters
, verbose
, shutdown
);
447 rcu_perf_cleanup(void)
455 if (torture_cleanup_begin())
459 for (i
= 0; i
< nrealreaders
; i
++)
460 torture_stop_kthread(rcu_perf_reader
,
466 for (i
= 0; i
< nrealwriters
; i
++) {
467 torture_stop_kthread(rcu_perf_writer
,
469 if (!writer_n_durations
)
471 j
= writer_n_durations
[i
];
472 pr_alert("%s%s writer %d gps: %d\n",
473 perf_type
, PERF_FLAG
, i
, j
);
476 pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
477 perf_type
, PERF_FLAG
,
478 t_rcu_perf_writer_started
, t_rcu_perf_writer_finished
,
479 t_rcu_perf_writer_finished
-
480 t_rcu_perf_writer_started
,
482 b_rcu_perf_writer_finished
-
483 b_rcu_perf_writer_started
);
484 for (i
= 0; i
< nrealwriters
; i
++) {
485 if (!writer_durations
)
487 if (!writer_n_durations
)
489 wdpp
= writer_durations
[i
];
492 for (j
= 0; j
<= writer_n_durations
[i
]; j
++) {
494 pr_alert("%s%s %4d writer-duration: %5d %llu\n",
495 perf_type
, PERF_FLAG
,
498 schedule_timeout_uninterruptible(1);
500 kfree(writer_durations
[i
]);
503 kfree(writer_durations
);
504 kfree(writer_n_durations
);
507 /* Do flavor-specific cleanup operations. */
508 if (cur_ops
->cleanup
!= NULL
)
511 torture_cleanup_end();
515 * Return the number if non-negative. If -1, the number of CPUs.
516 * If less than -1, that much less than the number of CPUs, but
519 static int compute_real(int n
)
526 nr
= num_online_cpus() + 1 + n
;
534 * RCU perf shutdown kthread. Just waits to be awakened, then shuts
538 rcu_perf_shutdown(void *arg
)
541 wait_event(shutdown_wq
,
542 atomic_read(&n_rcu_perf_writer_finished
) >=
544 } while (atomic_read(&n_rcu_perf_writer_finished
) < nrealwriters
);
545 smp_mb(); /* Wake before output. */
556 static struct rcu_perf_ops
*perf_ops
[] = {
557 &rcu_ops
, &rcu_bh_ops
, &srcu_ops
, &sched_ops
,
561 if (!torture_init_begin(perf_type
, verbose
, &perf_runnable
))
564 /* Process args and tell the world that the perf'er is on the job. */
565 for (i
= 0; i
< ARRAY_SIZE(perf_ops
); i
++) {
566 cur_ops
= perf_ops
[i
];
567 if (strcmp(perf_type
, cur_ops
->name
) == 0)
570 if (i
== ARRAY_SIZE(perf_ops
)) {
571 pr_alert("rcu-perf: invalid perf type: \"%s\"\n",
573 pr_alert("rcu-perf types:");
574 for (i
= 0; i
< ARRAY_SIZE(perf_ops
); i
++)
575 pr_alert(" %s", perf_ops
[i
]->name
);
583 nrealwriters
= compute_real(nwriters
);
584 nrealreaders
= compute_real(nreaders
);
585 atomic_set(&n_rcu_perf_reader_started
, 0);
586 atomic_set(&n_rcu_perf_writer_started
, 0);
587 atomic_set(&n_rcu_perf_writer_finished
, 0);
588 rcu_perf_print_module_parms(cur_ops
, "Start of test");
590 /* Start up the kthreads. */
593 init_waitqueue_head(&shutdown_wq
);
594 firsterr
= torture_create_kthread(rcu_perf_shutdown
, NULL
,
598 schedule_timeout_uninterruptible(1);
600 reader_tasks
= kcalloc(nrealreaders
, sizeof(reader_tasks
[0]),
602 if (reader_tasks
== NULL
) {
603 VERBOSE_PERFOUT_ERRSTRING("out of memory");
607 for (i
= 0; i
< nrealreaders
; i
++) {
608 firsterr
= torture_create_kthread(rcu_perf_reader
, (void *)i
,
613 while (atomic_read(&n_rcu_perf_reader_started
) < nrealreaders
)
614 schedule_timeout_uninterruptible(1);
615 writer_tasks
= kcalloc(nrealwriters
, sizeof(reader_tasks
[0]),
617 writer_durations
= kcalloc(nrealwriters
, sizeof(*writer_durations
),
620 kcalloc(nrealwriters
, sizeof(*writer_n_durations
),
622 if (!writer_tasks
|| !writer_durations
|| !writer_n_durations
) {
623 VERBOSE_PERFOUT_ERRSTRING("out of memory");
627 if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp
) {
628 VERBOSE_PERFOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!");
632 if (rcu_gp_is_normal() && gp_exp
) {
633 VERBOSE_PERFOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!");
637 for (i
= 0; i
< nrealwriters
; i
++) {
638 writer_durations
[i
] =
639 kcalloc(MAX_MEAS
, sizeof(*writer_durations
[i
]),
641 if (!writer_durations
[i
]) {
645 firsterr
= torture_create_kthread(rcu_perf_writer
, (void *)i
,
659 module_init(rcu_perf_init
);
660 module_exit(rcu_perf_cleanup
);