rcutorture: Make scripts analyze rcuperf trace data, if present
[deliverable/linux.git] / kernel / rcu / rcuperf.c
CommitLineData
8704baab
PM
1/*
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");
62torture_param(int, nreaders, -1, "Number of RCU reader threads");
63torture_param(int, nwriters, -1, "Number of RCU updater threads");
64torture_param(bool, shutdown, false, "Shutdown at end of performance tests.");
65torture_param(bool, verbose, true, "Enable verbose debugging printk()s");
66
67static char *perf_type = "rcu";
68module_param(perf_type, charp, 0444);
69MODULE_PARM_DESC(perf_type, "Type of RCU to performance-test (rcu, rcu_bh, ...)");
70
71static int nrealreaders;
72static int nrealwriters;
73static struct task_struct **writer_tasks;
74static struct task_struct **reader_tasks;
75static struct task_struct *shutdown_task;
76
77static u64 **writer_durations;
78static int *writer_n_durations;
79static atomic_t n_rcu_perf_reader_started;
80static atomic_t n_rcu_perf_writer_started;
81static atomic_t n_rcu_perf_writer_finished;
82static wait_queue_head_t shutdown_wq;
83static u64 t_rcu_perf_writer_started;
84static u64 t_rcu_perf_writer_finished;
85static unsigned long b_rcu_perf_writer_started;
86static unsigned long b_rcu_perf_writer_finished;
87
88static int rcu_perf_writer_state;
89#define RTWS_INIT 0
90#define RTWS_EXP_SYNC 1
91#define RTWS_SYNC 2
92#define RTWS_IDLE 2
93#define RTWS_STOPPING 3
94
95#define MAX_MEAS 10000
96#define MIN_MEAS 100
97
98#if defined(MODULE) || defined(CONFIG_RCU_PERF_TEST_RUNNABLE)
99#define RCUPERF_RUNNABLE_INIT 1
100#else
101#define RCUPERF_RUNNABLE_INIT 0
102#endif
103static int perf_runnable = RCUPERF_RUNNABLE_INIT;
104module_param(perf_runnable, int, 0444);
105MODULE_PARM_DESC(perf_runnable, "Start rcuperf at boot");
106
107/*
108 * Operations vector for selecting different types of tests.
109 */
110
111struct rcu_perf_ops {
112 int ptype;
113 void (*init)(void);
114 void (*cleanup)(void);
115 int (*readlock)(void);
116 void (*readunlock)(int idx);
117 unsigned long (*started)(void);
118 unsigned long (*completed)(void);
119 unsigned long (*exp_completed)(void);
120 void (*sync)(void);
121 void (*exp_sync)(void);
122 const char *name;
123};
124
125static struct rcu_perf_ops *cur_ops;
126
127/*
128 * Definitions for rcu perf testing.
129 */
130
131static int rcu_perf_read_lock(void) __acquires(RCU)
132{
133 rcu_read_lock();
134 return 0;
135}
136
137static void rcu_perf_read_unlock(int idx) __releases(RCU)
138{
139 rcu_read_unlock();
140}
141
142static unsigned long __maybe_unused rcu_no_completed(void)
143{
144 return 0;
145}
146
147static void rcu_sync_perf_init(void)
148{
149}
150
151static struct rcu_perf_ops rcu_ops = {
152 .ptype = RCU_FLAVOR,
153 .init = rcu_sync_perf_init,
154 .readlock = rcu_perf_read_lock,
155 .readunlock = rcu_perf_read_unlock,
156 .started = rcu_batches_started,
157 .completed = rcu_batches_completed,
158 .exp_completed = rcu_exp_batches_completed,
159 .sync = synchronize_rcu,
160 .exp_sync = synchronize_rcu_expedited,
161 .name = "rcu"
162};
163
164/*
165 * Definitions for rcu_bh perf testing.
166 */
167
168static int rcu_bh_perf_read_lock(void) __acquires(RCU_BH)
169{
170 rcu_read_lock_bh();
171 return 0;
172}
173
174static void rcu_bh_perf_read_unlock(int idx) __releases(RCU_BH)
175{
176 rcu_read_unlock_bh();
177}
178
179static struct rcu_perf_ops rcu_bh_ops = {
180 .ptype = RCU_BH_FLAVOR,
181 .init = rcu_sync_perf_init,
182 .readlock = rcu_bh_perf_read_lock,
183 .readunlock = rcu_bh_perf_read_unlock,
184 .started = rcu_batches_started_bh,
185 .completed = rcu_batches_completed_bh,
186 .exp_completed = rcu_exp_batches_completed_sched,
187 .sync = synchronize_rcu_bh,
188 .exp_sync = synchronize_rcu_bh_expedited,
189 .name = "rcu_bh"
190};
191
192/*
193 * Definitions for srcu perf testing.
194 */
195
196DEFINE_STATIC_SRCU(srcu_ctl_perf);
197static struct srcu_struct *srcu_ctlp = &srcu_ctl_perf;
198
199static int srcu_perf_read_lock(void) __acquires(srcu_ctlp)
200{
201 return srcu_read_lock(srcu_ctlp);
202}
203
204static void srcu_perf_read_unlock(int idx) __releases(srcu_ctlp)
205{
206 srcu_read_unlock(srcu_ctlp, idx);
207}
208
209static unsigned long srcu_perf_completed(void)
210{
211 return srcu_batches_completed(srcu_ctlp);
212}
213
214static void srcu_perf_synchronize(void)
215{
216 synchronize_srcu(srcu_ctlp);
217}
218
219static void srcu_perf_synchronize_expedited(void)
220{
221 synchronize_srcu_expedited(srcu_ctlp);
222}
223
224static struct rcu_perf_ops srcu_ops = {
225 .ptype = SRCU_FLAVOR,
226 .init = rcu_sync_perf_init,
227 .readlock = srcu_perf_read_lock,
228 .readunlock = srcu_perf_read_unlock,
229 .started = NULL,
230 .completed = srcu_perf_completed,
231 .exp_completed = srcu_perf_completed,
232 .sync = srcu_perf_synchronize,
233 .exp_sync = srcu_perf_synchronize_expedited,
234 .name = "srcu"
235};
236
237/*
238 * Definitions for sched perf testing.
239 */
240
241static int sched_perf_read_lock(void)
242{
243 preempt_disable();
244 return 0;
245}
246
247static void sched_perf_read_unlock(int idx)
248{
249 preempt_enable();
250}
251
252static struct rcu_perf_ops sched_ops = {
253 .ptype = RCU_SCHED_FLAVOR,
254 .init = rcu_sync_perf_init,
255 .readlock = sched_perf_read_lock,
256 .readunlock = sched_perf_read_unlock,
257 .started = rcu_batches_started_sched,
258 .completed = rcu_batches_completed_sched,
259 .exp_completed = rcu_exp_batches_completed_sched,
260 .sync = synchronize_sched,
261 .exp_sync = synchronize_sched_expedited,
262 .name = "sched"
263};
264
265#ifdef CONFIG_TASKS_RCU
266
267/*
268 * Definitions for RCU-tasks perf testing.
269 */
270
271static int tasks_perf_read_lock(void)
272{
273 return 0;
274}
275
276static void tasks_perf_read_unlock(int idx)
277{
278}
279
280static struct rcu_perf_ops tasks_ops = {
281 .ptype = RCU_TASKS_FLAVOR,
282 .init = rcu_sync_perf_init,
283 .readlock = tasks_perf_read_lock,
284 .readunlock = tasks_perf_read_unlock,
285 .started = rcu_no_completed,
286 .completed = rcu_no_completed,
287 .sync = synchronize_rcu_tasks,
288 .exp_sync = synchronize_rcu_tasks,
289 .name = "tasks"
290};
291
292#define RCUPERF_TASKS_OPS &tasks_ops,
293
294static bool __maybe_unused torturing_tasks(void)
295{
296 return cur_ops == &tasks_ops;
297}
298
299#else /* #ifdef CONFIG_TASKS_RCU */
300
301#define RCUPERF_TASKS_OPS
302
303static bool __maybe_unused torturing_tasks(void)
304{
305 return false;
306}
307
308#endif /* #else #ifdef CONFIG_TASKS_RCU */
309
310/*
311 * If performance tests complete, wait for shutdown to commence.
312 */
313static void rcu_perf_wait_shutdown(void)
314{
315 cond_resched_rcu_qs();
316 if (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters)
317 return;
318 while (!torture_must_stop())
319 schedule_timeout_uninterruptible(1);
320}
321
322/*
323 * RCU perf reader kthread. Repeatedly does empty RCU read-side
324 * critical section, minimizing update-side interference.
325 */
326static int
327rcu_perf_reader(void *arg)
328{
329 unsigned long flags;
330 int idx;
6b558c4c 331 long me = (long)arg;
8704baab
PM
332
333 VERBOSE_PERFOUT_STRING("rcu_perf_reader task started");
6b558c4c 334 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
8704baab
PM
335 set_user_nice(current, MAX_NICE);
336 atomic_inc(&n_rcu_perf_reader_started);
337
338 do {
339 local_irq_save(flags);
340 idx = cur_ops->readlock();
341 cur_ops->readunlock(idx);
342 local_irq_restore(flags);
343 rcu_perf_wait_shutdown();
344 } while (!torture_must_stop());
345 torture_kthread_stopping("rcu_perf_reader");
346 return 0;
347}
348
349/*
350 * RCU perf writer kthread. Repeatedly does a grace period.
351 */
352static int
353rcu_perf_writer(void *arg)
354{
355 int i = 0;
356 int i_max;
357 long me = (long)arg;
2094c995 358 struct sched_param sp;
8704baab
PM
359 bool started = false, done = false, alldone = false;
360 u64 t;
361 u64 *wdp;
362 u64 *wdpp = writer_durations[me];
363
364 VERBOSE_PERFOUT_STRING("rcu_perf_writer task started");
365 WARN_ON(rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp);
366 WARN_ON(rcu_gp_is_normal() && gp_exp);
367 WARN_ON(!wdpp);
6b558c4c 368 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
2094c995
PM
369 sp.sched_priority = 1;
370 sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
8704baab
PM
371 t = ktime_get_mono_fast_ns();
372 if (atomic_inc_return(&n_rcu_perf_writer_started) >= nrealwriters) {
373 t_rcu_perf_writer_started = t;
374 if (gp_exp) {
375 b_rcu_perf_writer_started =
376 cur_ops->exp_completed() / 2;
377 } else {
378 b_rcu_perf_writer_started =
379 cur_ops->completed();
380 }
381 }
382
383 do {
384 wdp = &wdpp[i];
385 *wdp = ktime_get_mono_fast_ns();
386 if (gp_exp) {
387 rcu_perf_writer_state = RTWS_EXP_SYNC;
388 cur_ops->exp_sync();
389 } else {
390 rcu_perf_writer_state = RTWS_SYNC;
391 cur_ops->sync();
392 }
393 rcu_perf_writer_state = RTWS_IDLE;
394 t = ktime_get_mono_fast_ns();
395 *wdp = t - *wdp;
396 i_max = i;
397 if (!started &&
398 atomic_read(&n_rcu_perf_writer_started) >= nrealwriters)
399 started = true;
400 if (!done && i >= MIN_MEAS) {
401 done = true;
402 pr_alert("%s" PERF_FLAG
403 "rcu_perf_writer %ld has %d measurements\n",
404 perf_type, me, MIN_MEAS);
405 if (atomic_inc_return(&n_rcu_perf_writer_finished) >=
406 nrealwriters) {
ac2bb275 407 rcu_ftrace_dump(DUMP_ALL);
8704baab
PM
408 PERFOUT_STRING("Test complete");
409 t_rcu_perf_writer_finished = t;
410 if (gp_exp) {
411 b_rcu_perf_writer_finished =
412 cur_ops->exp_completed() / 2;
413 } else {
414 b_rcu_perf_writer_finished =
415 cur_ops->completed();
416 }
417 smp_mb(); /* Assign before wake. */
418 wake_up(&shutdown_wq);
419 }
420 }
421 if (done && !alldone &&
422 atomic_read(&n_rcu_perf_writer_finished) >= nrealwriters)
423 alldone = true;
424 if (started && !alldone && i < MAX_MEAS - 1)
425 i++;
426 rcu_perf_wait_shutdown();
427 } while (!torture_must_stop());
428 rcu_perf_writer_state = RTWS_STOPPING;
429 writer_n_durations[me] = i_max;
430 torture_kthread_stopping("rcu_perf_writer");
431 return 0;
432}
433
434static inline void
435rcu_perf_print_module_parms(struct rcu_perf_ops *cur_ops, const char *tag)
436{
437 pr_alert("%s" PERF_FLAG
438 "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n",
439 perf_type, tag, nrealreaders, nrealwriters, verbose, shutdown);
440}
441
442static void
443rcu_perf_cleanup(void)
444{
445 int i;
446 int j;
447 int ngps = 0;
448 u64 *wdp;
449 u64 *wdpp;
450
451 if (torture_cleanup_begin())
452 return;
453
454 if (reader_tasks) {
455 for (i = 0; i < nrealreaders; i++)
456 torture_stop_kthread(rcu_perf_reader,
457 reader_tasks[i]);
458 kfree(reader_tasks);
459 }
460
461 if (writer_tasks) {
462 for (i = 0; i < nrealwriters; i++) {
463 torture_stop_kthread(rcu_perf_writer,
464 writer_tasks[i]);
465 if (!writer_n_durations)
466 continue;
467 j = writer_n_durations[i];
468 pr_alert("%s%s writer %d gps: %d\n",
469 perf_type, PERF_FLAG, i, j);
470 ngps += j;
471 }
472 pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n",
473 perf_type, PERF_FLAG,
474 t_rcu_perf_writer_started, t_rcu_perf_writer_finished,
475 t_rcu_perf_writer_finished -
476 t_rcu_perf_writer_started,
477 ngps,
478 b_rcu_perf_writer_finished -
479 b_rcu_perf_writer_started);
480 for (i = 0; i < nrealwriters; i++) {
481 if (!writer_durations)
482 break;
483 if (!writer_n_durations)
484 continue;
485 wdpp = writer_durations[i];
486 if (!wdpp)
487 continue;
488 for (j = 0; j <= writer_n_durations[i]; j++) {
489 wdp = &wdpp[j];
490 pr_alert("%s%s %4d writer-duration: %5d %llu\n",
491 perf_type, PERF_FLAG,
492 i, j, *wdp);
493 if (j % 100 == 0)
494 schedule_timeout_uninterruptible(1);
495 }
496 kfree(writer_durations[i]);
497 }
498 kfree(writer_tasks);
499 kfree(writer_durations);
500 kfree(writer_n_durations);
501 }
502
503 /* Do flavor-specific cleanup operations. */
504 if (cur_ops->cleanup != NULL)
505 cur_ops->cleanup();
506
507 torture_cleanup_end();
508}
509
510/*
511 * Return the number if non-negative. If -1, the number of CPUs.
512 * If less than -1, that much less than the number of CPUs, but
513 * at least one.
514 */
515static int compute_real(int n)
516{
517 int nr;
518
519 if (n >= 0) {
520 nr = n;
521 } else {
522 nr = num_online_cpus() + 1 + n;
523 if (nr <= 0)
524 nr = 1;
525 }
526 return nr;
527}
528
529/*
530 * RCU perf shutdown kthread. Just waits to be awakened, then shuts
531 * down system.
532 */
533static int
534rcu_perf_shutdown(void *arg)
535{
536 do {
537 wait_event(shutdown_wq,
538 atomic_read(&n_rcu_perf_writer_finished) >=
539 nrealwriters);
540 } while (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters);
541 smp_mb(); /* Wake before output. */
542 rcu_perf_cleanup();
543 kernel_power_off();
544 return -EINVAL;
545}
546
547static int __init
548rcu_perf_init(void)
549{
550 long i;
551 int firsterr = 0;
552 static struct rcu_perf_ops *perf_ops[] = {
553 &rcu_ops, &rcu_bh_ops, &srcu_ops, &sched_ops,
554 RCUPERF_TASKS_OPS
555 };
556
557 if (!torture_init_begin(perf_type, verbose, &perf_runnable))
558 return -EBUSY;
559
560 /* Process args and tell the world that the perf'er is on the job. */
561 for (i = 0; i < ARRAY_SIZE(perf_ops); i++) {
562 cur_ops = perf_ops[i];
563 if (strcmp(perf_type, cur_ops->name) == 0)
564 break;
565 }
566 if (i == ARRAY_SIZE(perf_ops)) {
567 pr_alert("rcu-perf: invalid perf type: \"%s\"\n",
568 perf_type);
569 pr_alert("rcu-perf types:");
570 for (i = 0; i < ARRAY_SIZE(perf_ops); i++)
571 pr_alert(" %s", perf_ops[i]->name);
572 pr_alert("\n");
573 firsterr = -EINVAL;
574 goto unwind;
575 }
576 if (cur_ops->init)
577 cur_ops->init();
578
579 nrealwriters = compute_real(nwriters);
580 nrealreaders = compute_real(nreaders);
581 atomic_set(&n_rcu_perf_reader_started, 0);
582 atomic_set(&n_rcu_perf_writer_started, 0);
583 atomic_set(&n_rcu_perf_writer_finished, 0);
584 rcu_perf_print_module_parms(cur_ops, "Start of test");
585
586 /* Start up the kthreads. */
587
588 if (shutdown) {
589 init_waitqueue_head(&shutdown_wq);
590 firsterr = torture_create_kthread(rcu_perf_shutdown, NULL,
591 shutdown_task);
592 if (firsterr)
593 goto unwind;
594 schedule_timeout_uninterruptible(1);
595 }
596 reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]),
597 GFP_KERNEL);
598 if (reader_tasks == NULL) {
599 VERBOSE_PERFOUT_ERRSTRING("out of memory");
600 firsterr = -ENOMEM;
601 goto unwind;
602 }
603 for (i = 0; i < nrealreaders; i++) {
6b558c4c 604 firsterr = torture_create_kthread(rcu_perf_reader, (void *)i,
8704baab
PM
605 reader_tasks[i]);
606 if (firsterr)
607 goto unwind;
608 }
609 while (atomic_read(&n_rcu_perf_reader_started) < nrealreaders)
610 schedule_timeout_uninterruptible(1);
611 writer_tasks = kcalloc(nrealwriters, sizeof(reader_tasks[0]),
612 GFP_KERNEL);
613 writer_durations = kcalloc(nrealwriters, sizeof(*writer_durations),
614 GFP_KERNEL);
615 writer_n_durations =
616 kcalloc(nrealwriters, sizeof(*writer_n_durations),
617 GFP_KERNEL);
618 if (!writer_tasks || !writer_durations || !writer_n_durations) {
619 VERBOSE_PERFOUT_ERRSTRING("out of memory");
620 firsterr = -ENOMEM;
621 goto unwind;
622 }
623 for (i = 0; i < nrealwriters; i++) {
624 writer_durations[i] =
625 kcalloc(MAX_MEAS, sizeof(*writer_durations[i]),
626 GFP_KERNEL);
627 if (!writer_durations[i])
628 goto unwind;
629 firsterr = torture_create_kthread(rcu_perf_writer, (void *)i,
630 writer_tasks[i]);
631 if (firsterr)
632 goto unwind;
633 }
634 torture_init_end();
635 return 0;
636
637unwind:
638 torture_init_end();
639 rcu_perf_cleanup();
640 return firsterr;
641}
642
643module_init(rcu_perf_init);
644module_exit(rcu_perf_cleanup);
This page took 0.04778 seconds and 5 git commands to generate.