blob: 4c0572859ff0fc24b69b1a922a944b1c299ce9aa [file] [log] [blame]
Paul E. McKenney8704baa2015-12-31 18:33:22 -08001/*
2 * Read-Copy Update module-based performance-test facility
3 *
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.
8 *
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.
13 *
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.
17 *
18 * Copyright (C) IBM Corporation, 2015
19 *
20 * Authors: Paul E. McKenney <paulmck@us.ibm.com>
21 */
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>
49
50MODULE_LICENSE("GPL");
51MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.vnet.ibm.com>");
52
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)
60
61torture_param(bool, gp_exp, true, "Use expedited GP wait primitives");
Paul E. McKenneydf37e662016-01-30 20:56:38 -080062torture_param(int, holdoff, 10, "Holdoff time before test start (s)");
Paul E. McKenney8704baa2015-12-31 18:33:22 -080063torture_param(int, nreaders, -1, "Number of RCU reader threads");
64torture_param(int, nwriters, -1, "Number of RCU updater threads");
65torture_param(bool, shutdown, false, "Shutdown at end of performance tests.");
66torture_param(bool, verbose, true, "Enable verbose debugging printk()s");
67
68static char *perf_type = "rcu";
69module_param(perf_type, charp, 0444);
70MODULE_PARM_DESC(perf_type, "Type of RCU to performance-test (rcu, rcu_bh, ...)");
71
72static int nrealreaders;
73static int nrealwriters;
74static struct task_struct **writer_tasks;
75static struct task_struct **reader_tasks;
76static struct task_struct *shutdown_task;
77
78static u64 **writer_durations;
79static int *writer_n_durations;
80static atomic_t n_rcu_perf_reader_started;
81static atomic_t n_rcu_perf_writer_started;
82static atomic_t n_rcu_perf_writer_finished;
83static wait_queue_head_t shutdown_wq;
84static u64 t_rcu_perf_writer_started;
85static u64 t_rcu_perf_writer_finished;
86static unsigned long b_rcu_perf_writer_started;
87static unsigned long b_rcu_perf_writer_finished;
88
89static int rcu_perf_writer_state;
90#define RTWS_INIT 0
91#define RTWS_EXP_SYNC 1
92#define RTWS_SYNC 2
93#define RTWS_IDLE 2
94#define RTWS_STOPPING 3
95
96#define MAX_MEAS 10000
97#define MIN_MEAS 100
98
99#if defined(MODULE) || defined(CONFIG_RCU_PERF_TEST_RUNNABLE)
100#define RCUPERF_RUNNABLE_INIT 1
101#else
102#define RCUPERF_RUNNABLE_INIT 0
103#endif
104static int perf_runnable = RCUPERF_RUNNABLE_INIT;
105module_param(perf_runnable, int, 0444);
106MODULE_PARM_DESC(perf_runnable, "Start rcuperf at boot");
107
108/*
109 * Operations vector for selecting different types of tests.
110 */
111
112struct rcu_perf_ops {
113 int ptype;
114 void (*init)(void);
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);
121 void (*sync)(void);
122 void (*exp_sync)(void);
123 const char *name;
124};
125
126static struct rcu_perf_ops *cur_ops;
127
128/*
129 * Definitions for rcu perf testing.
130 */
131
132static int rcu_perf_read_lock(void) __acquires(RCU)
133{
134 rcu_read_lock();
135 return 0;
136}
137
138static void rcu_perf_read_unlock(int idx) __releases(RCU)
139{
140 rcu_read_unlock();
141}
142
143static unsigned long __maybe_unused rcu_no_completed(void)
144{
145 return 0;
146}
147
148static void rcu_sync_perf_init(void)
149{
150}
151
152static struct rcu_perf_ops rcu_ops = {
153 .ptype = RCU_FLAVOR,
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,
162 .name = "rcu"
163};
164
165/*
166 * Definitions for rcu_bh perf testing.
167 */
168
169static int rcu_bh_perf_read_lock(void) __acquires(RCU_BH)
170{
171 rcu_read_lock_bh();
172 return 0;
173}
174
175static void rcu_bh_perf_read_unlock(int idx) __releases(RCU_BH)
176{
177 rcu_read_unlock_bh();
178}
179
180static 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,
190 .name = "rcu_bh"
191};
192
193/*
194 * Definitions for srcu perf testing.
195 */
196
197DEFINE_STATIC_SRCU(srcu_ctl_perf);
198static struct srcu_struct *srcu_ctlp = &srcu_ctl_perf;
199
200static int srcu_perf_read_lock(void) __acquires(srcu_ctlp)
201{
202 return srcu_read_lock(srcu_ctlp);
203}
204
205static void srcu_perf_read_unlock(int idx) __releases(srcu_ctlp)
206{
207 srcu_read_unlock(srcu_ctlp, idx);
208}
209
210static unsigned long srcu_perf_completed(void)
211{
212 return srcu_batches_completed(srcu_ctlp);
213}
214
215static void srcu_perf_synchronize(void)
216{
217 synchronize_srcu(srcu_ctlp);
218}
219
220static void srcu_perf_synchronize_expedited(void)
221{
222 synchronize_srcu_expedited(srcu_ctlp);
223}
224
225static 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,
230 .started = NULL,
231 .completed = srcu_perf_completed,
232 .exp_completed = srcu_perf_completed,
233 .sync = srcu_perf_synchronize,
234 .exp_sync = srcu_perf_synchronize_expedited,
235 .name = "srcu"
236};
237
238/*
239 * Definitions for sched perf testing.
240 */
241
242static int sched_perf_read_lock(void)
243{
244 preempt_disable();
245 return 0;
246}
247
248static void sched_perf_read_unlock(int idx)
249{
250 preempt_enable();
251}
252
253static 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,
263 .name = "sched"
264};
265
266#ifdef CONFIG_TASKS_RCU
267
268/*
269 * Definitions for RCU-tasks perf testing.
270 */
271
272static int tasks_perf_read_lock(void)
273{
274 return 0;
275}
276
277static void tasks_perf_read_unlock(int idx)
278{
279}
280
281static 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,
290 .name = "tasks"
291};
292
293#define RCUPERF_TASKS_OPS &tasks_ops,
294
295static bool __maybe_unused torturing_tasks(void)
296{
297 return cur_ops == &tasks_ops;
298}
299
300#else /* #ifdef CONFIG_TASKS_RCU */
301
302#define RCUPERF_TASKS_OPS
303
304static bool __maybe_unused torturing_tasks(void)
305{
306 return false;
307}
308
309#endif /* #else #ifdef CONFIG_TASKS_RCU */
310
311/*
312 * If performance tests complete, wait for shutdown to commence.
313 */
314static void rcu_perf_wait_shutdown(void)
315{
316 cond_resched_rcu_qs();
317 if (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters)
318 return;
319 while (!torture_must_stop())
320 schedule_timeout_uninterruptible(1);
321}
322
323/*
324 * RCU perf reader kthread. Repeatedly does empty RCU read-side
325 * critical section, minimizing update-side interference.
326 */
327static int
328rcu_perf_reader(void *arg)
329{
330 unsigned long flags;
331 int idx;
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800332 long me = (long)arg;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800333
334 VERBOSE_PERFOUT_STRING("rcu_perf_reader task started");
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800335 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800336 set_user_nice(current, MAX_NICE);
337 atomic_inc(&n_rcu_perf_reader_started);
338
339 do {
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");
347 return 0;
348}
349
350/*
351 * RCU perf writer kthread. Repeatedly does a grace period.
352 */
353static int
354rcu_perf_writer(void *arg)
355{
356 int i = 0;
357 int i_max;
358 long me = (long)arg;
Paul E. McKenney2094c992016-01-12 15:17:21 -0800359 struct sched_param sp;
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800360 bool started = false, done = false, alldone = false;
361 u64 t;
362 u64 *wdp;
363 u64 *wdpp = writer_durations[me];
364
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);
368 WARN_ON(!wdpp);
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800369 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
Paul E. McKenney2094c992016-01-12 15:17:21 -0800370 sp.sched_priority = 1;
371 sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
Paul E. McKenneydf37e662016-01-30 20:56:38 -0800372
373 if (holdoff)
374 schedule_timeout_uninterruptible(holdoff * HZ);
375
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800376 t = ktime_get_mono_fast_ns();
377 if (atomic_inc_return(&n_rcu_perf_writer_started) >= nrealwriters) {
378 t_rcu_perf_writer_started = t;
379 if (gp_exp) {
380 b_rcu_perf_writer_started =
381 cur_ops->exp_completed() / 2;
382 } else {
383 b_rcu_perf_writer_started =
384 cur_ops->completed();
385 }
386 }
387
388 do {
389 wdp = &wdpp[i];
390 *wdp = ktime_get_mono_fast_ns();
391 if (gp_exp) {
392 rcu_perf_writer_state = RTWS_EXP_SYNC;
393 cur_ops->exp_sync();
394 } else {
395 rcu_perf_writer_state = RTWS_SYNC;
396 cur_ops->sync();
397 }
398 rcu_perf_writer_state = RTWS_IDLE;
399 t = ktime_get_mono_fast_ns();
400 *wdp = t - *wdp;
401 i_max = i;
402 if (!started &&
403 atomic_read(&n_rcu_perf_writer_started) >= nrealwriters)
404 started = true;
405 if (!done && i >= MIN_MEAS) {
406 done = true;
Paul E. McKenney620316e2016-01-30 21:32:09 -0800407 sp.sched_priority = 0;
408 sched_setscheduler_nocheck(current,
409 SCHED_NORMAL, &sp);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800410 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) >=
414 nrealwriters) {
Paul E. McKenney620316e2016-01-30 21:32:09 -0800415 schedule_timeout_interruptible(10);
Paul E. McKenneyac2bb272016-01-29 14:58:17 -0800416 rcu_ftrace_dump(DUMP_ALL);
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800417 PERFOUT_STRING("Test complete");
418 t_rcu_perf_writer_finished = t;
419 if (gp_exp) {
420 b_rcu_perf_writer_finished =
421 cur_ops->exp_completed() / 2;
422 } else {
423 b_rcu_perf_writer_finished =
424 cur_ops->completed();
425 }
426 smp_mb(); /* Assign before wake. */
427 wake_up(&shutdown_wq);
428 }
429 }
430 if (done && !alldone &&
431 atomic_read(&n_rcu_perf_writer_finished) >= nrealwriters)
432 alldone = true;
433 if (started && !alldone && i < MAX_MEAS - 1)
434 i++;
435 rcu_perf_wait_shutdown();
436 } while (!torture_must_stop());
437 rcu_perf_writer_state = RTWS_STOPPING;
438 writer_n_durations[me] = i_max;
439 torture_kthread_stopping("rcu_perf_writer");
440 return 0;
441}
442
443static inline void
444rcu_perf_print_module_parms(struct rcu_perf_ops *cur_ops, const char *tag)
445{
446 pr_alert("%s" PERF_FLAG
447 "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
448 perf_type, tag, nrealreaders, nrealwriters, verbose, shutdown);
449}
450
451static void
452rcu_perf_cleanup(void)
453{
454 int i;
455 int j;
456 int ngps = 0;
457 u64 *wdp;
458 u64 *wdpp;
459
460 if (torture_cleanup_begin())
461 return;
462
463 if (reader_tasks) {
464 for (i = 0; i < nrealreaders; i++)
465 torture_stop_kthread(rcu_perf_reader,
466 reader_tasks[i]);
467 kfree(reader_tasks);
468 }
469
470 if (writer_tasks) {
471 for (i = 0; i < nrealwriters; i++) {
472 torture_stop_kthread(rcu_perf_writer,
473 writer_tasks[i]);
474 if (!writer_n_durations)
475 continue;
476 j = writer_n_durations[i];
477 pr_alert("%s%s writer %d gps: %d\n",
478 perf_type, PERF_FLAG, i, j);
479 ngps += j;
480 }
481 pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
482 perf_type, PERF_FLAG,
483 t_rcu_perf_writer_started, t_rcu_perf_writer_finished,
484 t_rcu_perf_writer_finished -
485 t_rcu_perf_writer_started,
486 ngps,
487 b_rcu_perf_writer_finished -
488 b_rcu_perf_writer_started);
489 for (i = 0; i < nrealwriters; i++) {
490 if (!writer_durations)
491 break;
492 if (!writer_n_durations)
493 continue;
494 wdpp = writer_durations[i];
495 if (!wdpp)
496 continue;
497 for (j = 0; j <= writer_n_durations[i]; j++) {
498 wdp = &wdpp[j];
499 pr_alert("%s%s %4d writer-duration: %5d %llu\n",
500 perf_type, PERF_FLAG,
501 i, j, *wdp);
502 if (j % 100 == 0)
503 schedule_timeout_uninterruptible(1);
504 }
505 kfree(writer_durations[i]);
506 }
507 kfree(writer_tasks);
508 kfree(writer_durations);
509 kfree(writer_n_durations);
510 }
511
512 /* Do flavor-specific cleanup operations. */
513 if (cur_ops->cleanup != NULL)
514 cur_ops->cleanup();
515
516 torture_cleanup_end();
517}
518
519/*
520 * Return the number if non-negative. If -1, the number of CPUs.
521 * If less than -1, that much less than the number of CPUs, but
522 * at least one.
523 */
524static int compute_real(int n)
525{
526 int nr;
527
528 if (n >= 0) {
529 nr = n;
530 } else {
531 nr = num_online_cpus() + 1 + n;
532 if (nr <= 0)
533 nr = 1;
534 }
535 return nr;
536}
537
538/*
539 * RCU perf shutdown kthread. Just waits to be awakened, then shuts
540 * down system.
541 */
542static int
543rcu_perf_shutdown(void *arg)
544{
545 do {
546 wait_event(shutdown_wq,
547 atomic_read(&n_rcu_perf_writer_finished) >=
548 nrealwriters);
549 } while (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters);
550 smp_mb(); /* Wake before output. */
551 rcu_perf_cleanup();
552 kernel_power_off();
553 return -EINVAL;
554}
555
556static int __init
557rcu_perf_init(void)
558{
559 long i;
560 int firsterr = 0;
561 static struct rcu_perf_ops *perf_ops[] = {
562 &rcu_ops, &rcu_bh_ops, &srcu_ops, &sched_ops,
563 RCUPERF_TASKS_OPS
564 };
565
566 if (!torture_init_begin(perf_type, verbose, &perf_runnable))
567 return -EBUSY;
568
569 /* Process args and tell the world that the perf'er is on the job. */
570 for (i = 0; i < ARRAY_SIZE(perf_ops); i++) {
571 cur_ops = perf_ops[i];
572 if (strcmp(perf_type, cur_ops->name) == 0)
573 break;
574 }
575 if (i == ARRAY_SIZE(perf_ops)) {
576 pr_alert("rcu-perf: invalid perf type: \"%s\"\n",
577 perf_type);
578 pr_alert("rcu-perf types:");
579 for (i = 0; i < ARRAY_SIZE(perf_ops); i++)
580 pr_alert(" %s", perf_ops[i]->name);
581 pr_alert("\n");
582 firsterr = -EINVAL;
583 goto unwind;
584 }
585 if (cur_ops->init)
586 cur_ops->init();
587
588 nrealwriters = compute_real(nwriters);
589 nrealreaders = compute_real(nreaders);
590 atomic_set(&n_rcu_perf_reader_started, 0);
591 atomic_set(&n_rcu_perf_writer_started, 0);
592 atomic_set(&n_rcu_perf_writer_finished, 0);
593 rcu_perf_print_module_parms(cur_ops, "Start of test");
594
595 /* Start up the kthreads. */
596
597 if (shutdown) {
598 init_waitqueue_head(&shutdown_wq);
599 firsterr = torture_create_kthread(rcu_perf_shutdown, NULL,
600 shutdown_task);
601 if (firsterr)
602 goto unwind;
603 schedule_timeout_uninterruptible(1);
604 }
605 reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
606 GFP_KERNEL);
607 if (reader_tasks == NULL) {
608 VERBOSE_PERFOUT_ERRSTRING("out of memory");
609 firsterr = -ENOMEM;
610 goto unwind;
611 }
612 for (i = 0; i < nrealreaders; i++) {
Paul E. McKenney6b558c42016-01-12 14:15:40 -0800613 firsterr = torture_create_kthread(rcu_perf_reader, (void *)i,
Paul E. McKenney8704baa2015-12-31 18:33:22 -0800614 reader_tasks[i]);
615 if (firsterr)
616 goto unwind;
617 }
618 while (atomic_read(&n_rcu_perf_reader_started) < nrealreaders)
619 schedule_timeout_uninterruptible(1);
620 writer_tasks = kcalloc(nrealwriters, sizeof(reader_tasks[0]),
621 GFP_KERNEL);
622 writer_durations = kcalloc(nrealwriters, sizeof(*writer_durations),
623 GFP_KERNEL);
624 writer_n_durations =
625 kcalloc(nrealwriters, sizeof(*writer_n_durations),
626 GFP_KERNEL);
627 if (!writer_tasks || !writer_durations || !writer_n_durations) {
628 VERBOSE_PERFOUT_ERRSTRING("out of memory");
629 firsterr = -ENOMEM;
630 goto unwind;
631 }
632 for (i = 0; i < nrealwriters; i++) {
633 writer_durations[i] =
634 kcalloc(MAX_MEAS, sizeof(*writer_durations[i]),
635 GFP_KERNEL);
636 if (!writer_durations[i])
637 goto unwind;
638 firsterr = torture_create_kthread(rcu_perf_writer, (void *)i,
639 writer_tasks[i]);
640 if (firsterr)
641 goto unwind;
642 }
643 torture_init_end();
644 return 0;
645
646unwind:
647 torture_init_end();
648 rcu_perf_cleanup();
649 return firsterr;
650}
651
652module_init(rcu_perf_init);
653module_exit(rcu_perf_cleanup);