Commit | Line | Data |
---|---|---|
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> | |
ae7e81c0 | 33 | #include <uapi/linux/sched/types.h> |
8704baab PM |
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> | |
50 | ||
25c36329 PM |
51 | #include "rcu.h" |
52 | ||
8704baab PM |
53 | MODULE_LICENSE("GPL"); |
54 | MODULE_AUTHOR("Paul E. McKenney <paulmck@linux.vnet.ibm.com>"); | |
55 | ||
56 | #define PERF_FLAG "-perf:" | |
57 | #define PERFOUT_STRING(s) \ | |
a56fefa2 | 58 | pr_alert("%s" PERF_FLAG " %s\n", perf_type, s) |
8704baab PM |
59 | #define VERBOSE_PERFOUT_STRING(s) \ |
60 | do { if (verbose) pr_alert("%s" PERF_FLAG " %s\n", perf_type, s); } while (0) | |
61 | #define VERBOSE_PERFOUT_ERRSTRING(s) \ | |
62 | do { if (verbose) pr_alert("%s" PERF_FLAG "!!! %s\n", perf_type, s); } while (0) | |
63 | ||
881ed593 PM |
64 | torture_param(bool, gp_async, false, "Use asynchronous GP wait primitives"); |
65 | torture_param(int, gp_async_max, 1000, "Max # outstanding waits per reader"); | |
af06d4f7 | 66 | torture_param(bool, gp_exp, false, "Use expedited GP wait primitives"); |
df37e66b | 67 | torture_param(int, holdoff, 10, "Holdoff time before test start (s)"); |
492b95e5 | 68 | torture_param(int, nreaders, 0, "Number of RCU reader threads"); |
8704baab | 69 | torture_param(int, nwriters, -1, "Number of RCU updater threads"); |
492b95e5 PM |
70 | torture_param(bool, shutdown, !IS_ENABLED(MODULE), |
71 | "Shutdown at end of performance tests."); | |
8704baab | 72 | torture_param(bool, verbose, true, "Enable verbose debugging printk()s"); |
820687a7 | 73 | torture_param(int, writer_holdoff, 0, "Holdoff (us) between GPs, zero to disable"); |
8704baab PM |
74 | |
75 | static char *perf_type = "rcu"; | |
76 | module_param(perf_type, charp, 0444); | |
77 | MODULE_PARM_DESC(perf_type, "Type of RCU to performance-test (rcu, rcu_bh, ...)"); | |
78 | ||
79 | static int nrealreaders; | |
80 | static int nrealwriters; | |
81 | static struct task_struct **writer_tasks; | |
82 | static struct task_struct **reader_tasks; | |
83 | static struct task_struct *shutdown_task; | |
84 | ||
85 | static u64 **writer_durations; | |
86 | static int *writer_n_durations; | |
87 | static atomic_t n_rcu_perf_reader_started; | |
88 | static atomic_t n_rcu_perf_writer_started; | |
89 | static atomic_t n_rcu_perf_writer_finished; | |
90 | static wait_queue_head_t shutdown_wq; | |
91 | static u64 t_rcu_perf_writer_started; | |
92 | static u64 t_rcu_perf_writer_finished; | |
93 | static unsigned long b_rcu_perf_writer_started; | |
94 | static unsigned long b_rcu_perf_writer_finished; | |
881ed593 | 95 | static DEFINE_PER_CPU(atomic_t, n_async_inflight); |
8704baab PM |
96 | |
97 | static int rcu_perf_writer_state; | |
98 | #define RTWS_INIT 0 | |
881ed593 PM |
99 | #define RTWS_ASYNC 1 |
100 | #define RTWS_BARRIER 2 | |
101 | #define RTWS_EXP_SYNC 3 | |
102 | #define RTWS_SYNC 4 | |
103 | #define RTWS_IDLE 5 | |
104 | #define RTWS_STOPPING 6 | |
8704baab PM |
105 | |
106 | #define MAX_MEAS 10000 | |
107 | #define MIN_MEAS 100 | |
108 | ||
8704baab PM |
109 | /* |
110 | * Operations vector for selecting different types of tests. | |
111 | */ | |
112 | ||
113 | struct rcu_perf_ops { | |
114 | int ptype; | |
115 | void (*init)(void); | |
116 | void (*cleanup)(void); | |
117 | int (*readlock)(void); | |
118 | void (*readunlock)(int idx); | |
119 | unsigned long (*started)(void); | |
120 | unsigned long (*completed)(void); | |
121 | unsigned long (*exp_completed)(void); | |
881ed593 PM |
122 | void (*async)(struct rcu_head *head, rcu_callback_t func); |
123 | void (*gp_barrier)(void); | |
8704baab PM |
124 | void (*sync)(void); |
125 | void (*exp_sync)(void); | |
126 | const char *name; | |
127 | }; | |
128 | ||
129 | static struct rcu_perf_ops *cur_ops; | |
130 | ||
131 | /* | |
132 | * Definitions for rcu perf testing. | |
133 | */ | |
134 | ||
135 | static int rcu_perf_read_lock(void) __acquires(RCU) | |
136 | { | |
137 | rcu_read_lock(); | |
138 | return 0; | |
139 | } | |
140 | ||
141 | static void rcu_perf_read_unlock(int idx) __releases(RCU) | |
142 | { | |
143 | rcu_read_unlock(); | |
144 | } | |
145 | ||
146 | static unsigned long __maybe_unused rcu_no_completed(void) | |
147 | { | |
148 | return 0; | |
149 | } | |
150 | ||
151 | static void rcu_sync_perf_init(void) | |
152 | { | |
153 | } | |
154 | ||
155 | static struct rcu_perf_ops rcu_ops = { | |
156 | .ptype = RCU_FLAVOR, | |
157 | .init = rcu_sync_perf_init, | |
158 | .readlock = rcu_perf_read_lock, | |
159 | .readunlock = rcu_perf_read_unlock, | |
160 | .started = rcu_batches_started, | |
161 | .completed = rcu_batches_completed, | |
162 | .exp_completed = rcu_exp_batches_completed, | |
881ed593 PM |
163 | .async = call_rcu, |
164 | .gp_barrier = rcu_barrier, | |
8704baab PM |
165 | .sync = synchronize_rcu, |
166 | .exp_sync = synchronize_rcu_expedited, | |
167 | .name = "rcu" | |
168 | }; | |
169 | ||
170 | /* | |
171 | * Definitions for rcu_bh perf testing. | |
172 | */ | |
173 | ||
174 | static int rcu_bh_perf_read_lock(void) __acquires(RCU_BH) | |
175 | { | |
176 | rcu_read_lock_bh(); | |
177 | return 0; | |
178 | } | |
179 | ||
180 | static void rcu_bh_perf_read_unlock(int idx) __releases(RCU_BH) | |
181 | { | |
182 | rcu_read_unlock_bh(); | |
183 | } | |
184 | ||
185 | static struct rcu_perf_ops rcu_bh_ops = { | |
186 | .ptype = RCU_BH_FLAVOR, | |
187 | .init = rcu_sync_perf_init, | |
188 | .readlock = rcu_bh_perf_read_lock, | |
189 | .readunlock = rcu_bh_perf_read_unlock, | |
190 | .started = rcu_batches_started_bh, | |
191 | .completed = rcu_batches_completed_bh, | |
192 | .exp_completed = rcu_exp_batches_completed_sched, | |
881ed593 PM |
193 | .async = call_rcu_bh, |
194 | .gp_barrier = rcu_barrier_bh, | |
8704baab PM |
195 | .sync = synchronize_rcu_bh, |
196 | .exp_sync = synchronize_rcu_bh_expedited, | |
197 | .name = "rcu_bh" | |
198 | }; | |
199 | ||
200 | /* | |
201 | * Definitions for srcu perf testing. | |
202 | */ | |
203 | ||
204 | DEFINE_STATIC_SRCU(srcu_ctl_perf); | |
205 | static struct srcu_struct *srcu_ctlp = &srcu_ctl_perf; | |
206 | ||
207 | static int srcu_perf_read_lock(void) __acquires(srcu_ctlp) | |
208 | { | |
209 | return srcu_read_lock(srcu_ctlp); | |
210 | } | |
211 | ||
212 | static void srcu_perf_read_unlock(int idx) __releases(srcu_ctlp) | |
213 | { | |
214 | srcu_read_unlock(srcu_ctlp, idx); | |
215 | } | |
216 | ||
217 | static unsigned long srcu_perf_completed(void) | |
218 | { | |
219 | return srcu_batches_completed(srcu_ctlp); | |
220 | } | |
221 | ||
881ed593 PM |
222 | static void srcu_call_rcu(struct rcu_head *head, rcu_callback_t func) |
223 | { | |
224 | call_srcu(srcu_ctlp, head, func); | |
225 | } | |
226 | ||
227 | static void srcu_rcu_barrier(void) | |
228 | { | |
229 | srcu_barrier(srcu_ctlp); | |
230 | } | |
231 | ||
8704baab PM |
232 | static void srcu_perf_synchronize(void) |
233 | { | |
234 | synchronize_srcu(srcu_ctlp); | |
235 | } | |
236 | ||
237 | static void srcu_perf_synchronize_expedited(void) | |
238 | { | |
239 | synchronize_srcu_expedited(srcu_ctlp); | |
240 | } | |
241 | ||
242 | static struct rcu_perf_ops srcu_ops = { | |
243 | .ptype = SRCU_FLAVOR, | |
244 | .init = rcu_sync_perf_init, | |
245 | .readlock = srcu_perf_read_lock, | |
246 | .readunlock = srcu_perf_read_unlock, | |
247 | .started = NULL, | |
248 | .completed = srcu_perf_completed, | |
249 | .exp_completed = srcu_perf_completed, | |
881ed593 PM |
250 | .async = srcu_call_rcu, |
251 | .gp_barrier = srcu_rcu_barrier, | |
8704baab PM |
252 | .sync = srcu_perf_synchronize, |
253 | .exp_sync = srcu_perf_synchronize_expedited, | |
254 | .name = "srcu" | |
255 | }; | |
256 | ||
f60cb4d4 PM |
257 | static struct srcu_struct srcud; |
258 | ||
259 | static void srcu_sync_perf_init(void) | |
260 | { | |
261 | srcu_ctlp = &srcud; | |
262 | init_srcu_struct(srcu_ctlp); | |
263 | } | |
264 | ||
265 | static void srcu_sync_perf_cleanup(void) | |
266 | { | |
267 | cleanup_srcu_struct(srcu_ctlp); | |
268 | } | |
269 | ||
270 | static struct rcu_perf_ops srcud_ops = { | |
271 | .ptype = SRCU_FLAVOR, | |
272 | .init = srcu_sync_perf_init, | |
273 | .cleanup = srcu_sync_perf_cleanup, | |
274 | .readlock = srcu_perf_read_lock, | |
275 | .readunlock = srcu_perf_read_unlock, | |
276 | .started = NULL, | |
277 | .completed = srcu_perf_completed, | |
278 | .exp_completed = srcu_perf_completed, | |
279 | .async = srcu_call_rcu, | |
280 | .gp_barrier = srcu_rcu_barrier, | |
281 | .sync = srcu_perf_synchronize, | |
282 | .exp_sync = srcu_perf_synchronize_expedited, | |
283 | .name = "srcud" | |
284 | }; | |
285 | ||
8704baab PM |
286 | /* |
287 | * Definitions for sched perf testing. | |
288 | */ | |
289 | ||
290 | static int sched_perf_read_lock(void) | |
291 | { | |
292 | preempt_disable(); | |
293 | return 0; | |
294 | } | |
295 | ||
296 | static void sched_perf_read_unlock(int idx) | |
297 | { | |
298 | preempt_enable(); | |
299 | } | |
300 | ||
301 | static struct rcu_perf_ops sched_ops = { | |
302 | .ptype = RCU_SCHED_FLAVOR, | |
303 | .init = rcu_sync_perf_init, | |
304 | .readlock = sched_perf_read_lock, | |
305 | .readunlock = sched_perf_read_unlock, | |
306 | .started = rcu_batches_started_sched, | |
307 | .completed = rcu_batches_completed_sched, | |
308 | .exp_completed = rcu_exp_batches_completed_sched, | |
881ed593 PM |
309 | .async = call_rcu_sched, |
310 | .gp_barrier = rcu_barrier_sched, | |
8704baab PM |
311 | .sync = synchronize_sched, |
312 | .exp_sync = synchronize_sched_expedited, | |
313 | .name = "sched" | |
314 | }; | |
315 | ||
8704baab PM |
316 | /* |
317 | * Definitions for RCU-tasks perf testing. | |
318 | */ | |
319 | ||
320 | static int tasks_perf_read_lock(void) | |
321 | { | |
322 | return 0; | |
323 | } | |
324 | ||
325 | static void tasks_perf_read_unlock(int idx) | |
326 | { | |
327 | } | |
328 | ||
329 | static struct rcu_perf_ops tasks_ops = { | |
330 | .ptype = RCU_TASKS_FLAVOR, | |
331 | .init = rcu_sync_perf_init, | |
332 | .readlock = tasks_perf_read_lock, | |
333 | .readunlock = tasks_perf_read_unlock, | |
334 | .started = rcu_no_completed, | |
335 | .completed = rcu_no_completed, | |
881ed593 PM |
336 | .async = call_rcu_tasks, |
337 | .gp_barrier = rcu_barrier_tasks, | |
8704baab PM |
338 | .sync = synchronize_rcu_tasks, |
339 | .exp_sync = synchronize_rcu_tasks, | |
340 | .name = "tasks" | |
341 | }; | |
342 | ||
8704baab PM |
343 | static bool __maybe_unused torturing_tasks(void) |
344 | { | |
345 | return cur_ops == &tasks_ops; | |
346 | } | |
347 | ||
8704baab PM |
348 | /* |
349 | * If performance tests complete, wait for shutdown to commence. | |
350 | */ | |
351 | static void rcu_perf_wait_shutdown(void) | |
352 | { | |
353 | cond_resched_rcu_qs(); | |
354 | if (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters) | |
355 | return; | |
356 | while (!torture_must_stop()) | |
357 | schedule_timeout_uninterruptible(1); | |
358 | } | |
359 | ||
360 | /* | |
361 | * RCU perf reader kthread. Repeatedly does empty RCU read-side | |
362 | * critical section, minimizing update-side interference. | |
363 | */ | |
364 | static int | |
365 | rcu_perf_reader(void *arg) | |
366 | { | |
367 | unsigned long flags; | |
368 | int idx; | |
6b558c4c | 369 | long me = (long)arg; |
8704baab PM |
370 | |
371 | VERBOSE_PERFOUT_STRING("rcu_perf_reader task started"); | |
6b558c4c | 372 | set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids)); |
8704baab PM |
373 | set_user_nice(current, MAX_NICE); |
374 | atomic_inc(&n_rcu_perf_reader_started); | |
375 | ||
376 | do { | |
377 | local_irq_save(flags); | |
378 | idx = cur_ops->readlock(); | |
379 | cur_ops->readunlock(idx); | |
380 | local_irq_restore(flags); | |
381 | rcu_perf_wait_shutdown(); | |
382 | } while (!torture_must_stop()); | |
383 | torture_kthread_stopping("rcu_perf_reader"); | |
384 | return 0; | |
385 | } | |
386 | ||
881ed593 PM |
387 | /* |
388 | * Callback function for asynchronous grace periods from rcu_perf_writer(). | |
389 | */ | |
390 | static void rcu_perf_async_cb(struct rcu_head *rhp) | |
391 | { | |
392 | atomic_dec(this_cpu_ptr(&n_async_inflight)); | |
393 | kfree(rhp); | |
394 | } | |
395 | ||
8704baab PM |
396 | /* |
397 | * RCU perf writer kthread. Repeatedly does a grace period. | |
398 | */ | |
399 | static int | |
400 | rcu_perf_writer(void *arg) | |
401 | { | |
402 | int i = 0; | |
403 | int i_max; | |
404 | long me = (long)arg; | |
881ed593 | 405 | struct rcu_head *rhp = NULL; |
2094c995 | 406 | struct sched_param sp; |
8704baab PM |
407 | bool started = false, done = false, alldone = false; |
408 | u64 t; | |
409 | u64 *wdp; | |
410 | u64 *wdpp = writer_durations[me]; | |
411 | ||
412 | VERBOSE_PERFOUT_STRING("rcu_perf_writer task started"); | |
8704baab | 413 | WARN_ON(!wdpp); |
6b558c4c | 414 | set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids)); |
2094c995 PM |
415 | sp.sched_priority = 1; |
416 | sched_setscheduler_nocheck(current, SCHED_FIFO, &sp); | |
df37e66b PM |
417 | |
418 | if (holdoff) | |
419 | schedule_timeout_uninterruptible(holdoff * HZ); | |
420 | ||
8704baab PM |
421 | t = ktime_get_mono_fast_ns(); |
422 | if (atomic_inc_return(&n_rcu_perf_writer_started) >= nrealwriters) { | |
423 | t_rcu_perf_writer_started = t; | |
424 | if (gp_exp) { | |
425 | b_rcu_perf_writer_started = | |
426 | cur_ops->exp_completed() / 2; | |
427 | } else { | |
428 | b_rcu_perf_writer_started = | |
429 | cur_ops->completed(); | |
430 | } | |
431 | } | |
432 | ||
433 | do { | |
820687a7 PM |
434 | if (writer_holdoff) |
435 | udelay(writer_holdoff); | |
8704baab PM |
436 | wdp = &wdpp[i]; |
437 | *wdp = ktime_get_mono_fast_ns(); | |
881ed593 PM |
438 | if (gp_async) { |
439 | retry: | |
440 | if (!rhp) | |
441 | rhp = kmalloc(sizeof(*rhp), GFP_KERNEL); | |
442 | if (rhp && atomic_read(this_cpu_ptr(&n_async_inflight)) < gp_async_max) { | |
443 | rcu_perf_writer_state = RTWS_ASYNC; | |
444 | atomic_inc(this_cpu_ptr(&n_async_inflight)); | |
445 | cur_ops->async(rhp, rcu_perf_async_cb); | |
446 | rhp = NULL; | |
447 | } else if (!kthread_should_stop()) { | |
448 | rcu_perf_writer_state = RTWS_BARRIER; | |
449 | cur_ops->gp_barrier(); | |
450 | goto retry; | |
451 | } else { | |
452 | kfree(rhp); /* Because we are stopping. */ | |
453 | } | |
454 | } else if (gp_exp) { | |
8704baab PM |
455 | rcu_perf_writer_state = RTWS_EXP_SYNC; |
456 | cur_ops->exp_sync(); | |
457 | } else { | |
458 | rcu_perf_writer_state = RTWS_SYNC; | |
459 | cur_ops->sync(); | |
460 | } | |
461 | rcu_perf_writer_state = RTWS_IDLE; | |
462 | t = ktime_get_mono_fast_ns(); | |
463 | *wdp = t - *wdp; | |
464 | i_max = i; | |
465 | if (!started && | |
466 | atomic_read(&n_rcu_perf_writer_started) >= nrealwriters) | |
467 | started = true; | |
468 | if (!done && i >= MIN_MEAS) { | |
469 | done = true; | |
620316e5 PM |
470 | sp.sched_priority = 0; |
471 | sched_setscheduler_nocheck(current, | |
472 | SCHED_NORMAL, &sp); | |
a56fefa2 SP |
473 | pr_alert("%s%s rcu_perf_writer %ld has %d measurements\n", |
474 | perf_type, PERF_FLAG, me, MIN_MEAS); | |
8704baab PM |
475 | if (atomic_inc_return(&n_rcu_perf_writer_finished) >= |
476 | nrealwriters) { | |
620316e5 | 477 | schedule_timeout_interruptible(10); |
ac2bb275 | 478 | rcu_ftrace_dump(DUMP_ALL); |
8704baab PM |
479 | PERFOUT_STRING("Test complete"); |
480 | t_rcu_perf_writer_finished = t; | |
481 | if (gp_exp) { | |
482 | b_rcu_perf_writer_finished = | |
483 | cur_ops->exp_completed() / 2; | |
484 | } else { | |
485 | b_rcu_perf_writer_finished = | |
486 | cur_ops->completed(); | |
487 | } | |
e6fb1fc1 AS |
488 | if (shutdown) { |
489 | smp_mb(); /* Assign before wake. */ | |
490 | wake_up(&shutdown_wq); | |
491 | } | |
8704baab PM |
492 | } |
493 | } | |
494 | if (done && !alldone && | |
495 | atomic_read(&n_rcu_perf_writer_finished) >= nrealwriters) | |
496 | alldone = true; | |
497 | if (started && !alldone && i < MAX_MEAS - 1) | |
498 | i++; | |
499 | rcu_perf_wait_shutdown(); | |
500 | } while (!torture_must_stop()); | |
881ed593 PM |
501 | if (gp_async) { |
502 | rcu_perf_writer_state = RTWS_BARRIER; | |
503 | cur_ops->gp_barrier(); | |
504 | } | |
8704baab PM |
505 | rcu_perf_writer_state = RTWS_STOPPING; |
506 | writer_n_durations[me] = i_max; | |
507 | torture_kthread_stopping("rcu_perf_writer"); | |
508 | return 0; | |
509 | } | |
510 | ||
511 | static inline void | |
512 | rcu_perf_print_module_parms(struct rcu_perf_ops *cur_ops, const char *tag) | |
513 | { | |
514 | pr_alert("%s" PERF_FLAG | |
515 | "--- %s: nreaders=%d nwriters=%d verbose=%d shutdown=%d\n", | |
516 | perf_type, tag, nrealreaders, nrealwriters, verbose, shutdown); | |
517 | } | |
518 | ||
519 | static void | |
520 | rcu_perf_cleanup(void) | |
521 | { | |
522 | int i; | |
523 | int j; | |
524 | int ngps = 0; | |
525 | u64 *wdp; | |
526 | u64 *wdpp; | |
527 | ||
9683937d PM |
528 | /* |
529 | * Would like warning at start, but everything is expedited | |
530 | * during the mid-boot phase, so have to wait till the end. | |
531 | */ | |
532 | if (rcu_gp_is_expedited() && !rcu_gp_is_normal() && !gp_exp) | |
533 | VERBOSE_PERFOUT_ERRSTRING("All grace periods expedited, no normal ones to measure!"); | |
534 | if (rcu_gp_is_normal() && gp_exp) | |
535 | VERBOSE_PERFOUT_ERRSTRING("All grace periods normal, no expedited ones to measure!"); | |
881ed593 PM |
536 | if (gp_exp && gp_async) |
537 | VERBOSE_PERFOUT_ERRSTRING("No expedited async GPs, so went with async!"); | |
9683937d | 538 | |
8704baab PM |
539 | if (torture_cleanup_begin()) |
540 | return; | |
541 | ||
542 | if (reader_tasks) { | |
543 | for (i = 0; i < nrealreaders; i++) | |
544 | torture_stop_kthread(rcu_perf_reader, | |
545 | reader_tasks[i]); | |
546 | kfree(reader_tasks); | |
547 | } | |
548 | ||
549 | if (writer_tasks) { | |
550 | for (i = 0; i < nrealwriters; i++) { | |
551 | torture_stop_kthread(rcu_perf_writer, | |
552 | writer_tasks[i]); | |
553 | if (!writer_n_durations) | |
554 | continue; | |
555 | j = writer_n_durations[i]; | |
556 | pr_alert("%s%s writer %d gps: %d\n", | |
557 | perf_type, PERF_FLAG, i, j); | |
558 | ngps += j; | |
559 | } | |
560 | pr_alert("%s%s start: %llu end: %llu duration: %llu gps: %d batches: %ld\n", | |
561 | perf_type, PERF_FLAG, | |
562 | t_rcu_perf_writer_started, t_rcu_perf_writer_finished, | |
563 | t_rcu_perf_writer_finished - | |
564 | t_rcu_perf_writer_started, | |
565 | ngps, | |
566 | b_rcu_perf_writer_finished - | |
567 | b_rcu_perf_writer_started); | |
568 | for (i = 0; i < nrealwriters; i++) { | |
569 | if (!writer_durations) | |
570 | break; | |
571 | if (!writer_n_durations) | |
572 | continue; | |
573 | wdpp = writer_durations[i]; | |
574 | if (!wdpp) | |
575 | continue; | |
576 | for (j = 0; j <= writer_n_durations[i]; j++) { | |
577 | wdp = &wdpp[j]; | |
578 | pr_alert("%s%s %4d writer-duration: %5d %llu\n", | |
579 | perf_type, PERF_FLAG, | |
580 | i, j, *wdp); | |
581 | if (j % 100 == 0) | |
582 | schedule_timeout_uninterruptible(1); | |
583 | } | |
584 | kfree(writer_durations[i]); | |
585 | } | |
586 | kfree(writer_tasks); | |
587 | kfree(writer_durations); | |
588 | kfree(writer_n_durations); | |
589 | } | |
590 | ||
591 | /* Do flavor-specific cleanup operations. */ | |
592 | if (cur_ops->cleanup != NULL) | |
593 | cur_ops->cleanup(); | |
594 | ||
595 | torture_cleanup_end(); | |
596 | } | |
597 | ||
598 | /* | |
599 | * Return the number if non-negative. If -1, the number of CPUs. | |
600 | * If less than -1, that much less than the number of CPUs, but | |
601 | * at least one. | |
602 | */ | |
603 | static int compute_real(int n) | |
604 | { | |
605 | int nr; | |
606 | ||
607 | if (n >= 0) { | |
608 | nr = n; | |
609 | } else { | |
610 | nr = num_online_cpus() + 1 + n; | |
611 | if (nr <= 0) | |
612 | nr = 1; | |
613 | } | |
614 | return nr; | |
615 | } | |
616 | ||
617 | /* | |
618 | * RCU perf shutdown kthread. Just waits to be awakened, then shuts | |
619 | * down system. | |
620 | */ | |
621 | static int | |
622 | rcu_perf_shutdown(void *arg) | |
623 | { | |
624 | do { | |
625 | wait_event(shutdown_wq, | |
626 | atomic_read(&n_rcu_perf_writer_finished) >= | |
627 | nrealwriters); | |
628 | } while (atomic_read(&n_rcu_perf_writer_finished) < nrealwriters); | |
629 | smp_mb(); /* Wake before output. */ | |
630 | rcu_perf_cleanup(); | |
631 | kernel_power_off(); | |
632 | return -EINVAL; | |
633 | } | |
634 | ||
635 | static int __init | |
636 | rcu_perf_init(void) | |
637 | { | |
638 | long i; | |
639 | int firsterr = 0; | |
640 | static struct rcu_perf_ops *perf_ops[] = { | |
f60cb4d4 | 641 | &rcu_ops, &rcu_bh_ops, &srcu_ops, &srcud_ops, &sched_ops, |
f1dbc54b | 642 | &tasks_ops, |
8704baab PM |
643 | }; |
644 | ||
a2f2577d | 645 | if (!torture_init_begin(perf_type, verbose)) |
8704baab PM |
646 | return -EBUSY; |
647 | ||
648 | /* Process args and tell the world that the perf'er is on the job. */ | |
649 | for (i = 0; i < ARRAY_SIZE(perf_ops); i++) { | |
650 | cur_ops = perf_ops[i]; | |
651 | if (strcmp(perf_type, cur_ops->name) == 0) | |
652 | break; | |
653 | } | |
654 | if (i == ARRAY_SIZE(perf_ops)) { | |
655 | pr_alert("rcu-perf: invalid perf type: \"%s\"\n", | |
656 | perf_type); | |
657 | pr_alert("rcu-perf types:"); | |
658 | for (i = 0; i < ARRAY_SIZE(perf_ops); i++) | |
659 | pr_alert(" %s", perf_ops[i]->name); | |
660 | pr_alert("\n"); | |
661 | firsterr = -EINVAL; | |
662 | goto unwind; | |
663 | } | |
664 | if (cur_ops->init) | |
665 | cur_ops->init(); | |
666 | ||
667 | nrealwriters = compute_real(nwriters); | |
668 | nrealreaders = compute_real(nreaders); | |
669 | atomic_set(&n_rcu_perf_reader_started, 0); | |
670 | atomic_set(&n_rcu_perf_writer_started, 0); | |
671 | atomic_set(&n_rcu_perf_writer_finished, 0); | |
672 | rcu_perf_print_module_parms(cur_ops, "Start of test"); | |
673 | ||
674 | /* Start up the kthreads. */ | |
675 | ||
676 | if (shutdown) { | |
677 | init_waitqueue_head(&shutdown_wq); | |
678 | firsterr = torture_create_kthread(rcu_perf_shutdown, NULL, | |
679 | shutdown_task); | |
680 | if (firsterr) | |
681 | goto unwind; | |
682 | schedule_timeout_uninterruptible(1); | |
683 | } | |
684 | reader_tasks = kcalloc(nrealreaders, sizeof(reader_tasks[0]), | |
685 | GFP_KERNEL); | |
686 | if (reader_tasks == NULL) { | |
687 | VERBOSE_PERFOUT_ERRSTRING("out of memory"); | |
688 | firsterr = -ENOMEM; | |
689 | goto unwind; | |
690 | } | |
691 | for (i = 0; i < nrealreaders; i++) { | |
6b558c4c | 692 | firsterr = torture_create_kthread(rcu_perf_reader, (void *)i, |
8704baab PM |
693 | reader_tasks[i]); |
694 | if (firsterr) | |
695 | goto unwind; | |
696 | } | |
697 | while (atomic_read(&n_rcu_perf_reader_started) < nrealreaders) | |
698 | schedule_timeout_uninterruptible(1); | |
699 | writer_tasks = kcalloc(nrealwriters, sizeof(reader_tasks[0]), | |
700 | GFP_KERNEL); | |
701 | writer_durations = kcalloc(nrealwriters, sizeof(*writer_durations), | |
702 | GFP_KERNEL); | |
703 | writer_n_durations = | |
704 | kcalloc(nrealwriters, sizeof(*writer_n_durations), | |
705 | GFP_KERNEL); | |
706 | if (!writer_tasks || !writer_durations || !writer_n_durations) { | |
707 | VERBOSE_PERFOUT_ERRSTRING("out of memory"); | |
708 | firsterr = -ENOMEM; | |
709 | goto unwind; | |
710 | } | |
711 | for (i = 0; i < nrealwriters; i++) { | |
712 | writer_durations[i] = | |
713 | kcalloc(MAX_MEAS, sizeof(*writer_durations[i]), | |
714 | GFP_KERNEL); | |
05dbbfe7 WY |
715 | if (!writer_durations[i]) { |
716 | firsterr = -ENOMEM; | |
8704baab | 717 | goto unwind; |
05dbbfe7 | 718 | } |
8704baab PM |
719 | firsterr = torture_create_kthread(rcu_perf_writer, (void *)i, |
720 | writer_tasks[i]); | |
721 | if (firsterr) | |
722 | goto unwind; | |
723 | } | |
724 | torture_init_end(); | |
725 | return 0; | |
726 | ||
727 | unwind: | |
728 | torture_init_end(); | |
729 | rcu_perf_cleanup(); | |
730 | return firsterr; | |
731 | } | |
732 | ||
733 | module_init(rcu_perf_init); | |
734 | module_exit(rcu_perf_cleanup); |