Commit | Line | Data |
---|---|---|
653ed64b JFG |
1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | // | |
8e4ec3d0 | 3 | // Scalability test comparing RCU vs other mechanisms |
653ed64b JFG |
4 | // for acquiring references on objects. |
5 | // | |
6 | // Copyright (C) Google, 2020. | |
7 | // | |
8 | // Author: Joel Fernandes <joel@joelfernandes.org> | |
9 | ||
10 | #define pr_fmt(fmt) fmt | |
11 | ||
12 | #include <linux/atomic.h> | |
13 | #include <linux/bitops.h> | |
14 | #include <linux/completion.h> | |
15 | #include <linux/cpu.h> | |
16 | #include <linux/delay.h> | |
17 | #include <linux/err.h> | |
18 | #include <linux/init.h> | |
19 | #include <linux/interrupt.h> | |
20 | #include <linux/kthread.h> | |
21 | #include <linux/kernel.h> | |
22 | #include <linux/mm.h> | |
23 | #include <linux/module.h> | |
24 | #include <linux/moduleparam.h> | |
25 | #include <linux/notifier.h> | |
26 | #include <linux/percpu.h> | |
27 | #include <linux/rcupdate.h> | |
72bb749e | 28 | #include <linux/rcupdate_trace.h> |
653ed64b JFG |
29 | #include <linux/reboot.h> |
30 | #include <linux/sched.h> | |
4e39bb49 | 31 | #include <linux/seq_buf.h> |
653ed64b JFG |
32 | #include <linux/spinlock.h> |
33 | #include <linux/smp.h> | |
34 | #include <linux/stat.h> | |
35 | #include <linux/srcu.h> | |
36 | #include <linux/slab.h> | |
37 | #include <linux/torture.h> | |
38 | #include <linux/types.h> | |
1806b1f9 | 39 | #include <linux/sched/clock.h> |
653ed64b JFG |
40 | |
41 | #include "rcu.h" | |
42 | ||
1fbeb3a8 | 43 | #define SCALE_FLAG "-ref-scale: " |
653ed64b | 44 | |
1fbeb3a8 PM |
45 | #define SCALEOUT(s, x...) \ |
46 | pr_alert("%s" SCALE_FLAG s, scale_type, ## x) | |
653ed64b | 47 | |
1fbeb3a8 | 48 | #define VERBOSE_SCALEOUT(s, x...) \ |
f71f22b6 LZ |
49 | do { \ |
50 | if (verbose) \ | |
51 | pr_alert("%s" SCALE_FLAG s "\n", scale_type, ## x); \ | |
52 | } while (0) | |
653ed64b | 53 | |
e76506f0 PM |
54 | static atomic_t verbose_batch_ctr; |
55 | ||
56 | #define VERBOSE_SCALEOUT_BATCH(s, x...) \ | |
57 | do { \ | |
58 | if (verbose && \ | |
59 | (verbose_batched <= 0 || \ | |
414c116e PM |
60 | !(atomic_inc_return(&verbose_batch_ctr) % verbose_batched))) { \ |
61 | schedule_timeout_uninterruptible(1); \ | |
f71f22b6 | 62 | pr_alert("%s" SCALE_FLAG s "\n", scale_type, ## x); \ |
414c116e | 63 | } \ |
e76506f0 PM |
64 | } while (0) |
65 | ||
f71f22b6 | 66 | #define SCALEOUT_ERRSTRING(s, x...) pr_alert("%s" SCALE_FLAG "!!! " s "\n", scale_type, ## x) |
653ed64b | 67 | |
b9f147cd | 68 | MODULE_DESCRIPTION("Scalability test for object reference mechanisms"); |
653ed64b JFG |
69 | MODULE_LICENSE("GPL"); |
70 | MODULE_AUTHOR("Joel Fernandes (Google) <joel@joelfernandes.org>"); | |
71 | ||
1fbeb3a8 PM |
72 | static char *scale_type = "rcu"; |
73 | module_param(scale_type, charp, 0444); | |
74 | MODULE_PARM_DESC(scale_type, "Type of test (rcu, srcu, refcnt, rwsem, rwlock."); | |
653ed64b JFG |
75 | |
76 | torture_param(int, verbose, 0, "Enable verbose debugging printk()s"); | |
e76506f0 | 77 | torture_param(int, verbose_batched, 0, "Batch verbose debugging printk()s"); |
653ed64b | 78 | |
ff9ba8db PM |
79 | // Number of seconds to extend warm-up and cool-down for multiple guest OSes |
80 | torture_param(long, guest_os_delay, 0, | |
81 | "Number of seconds to extend warm-up/cool-down for multiple guest OSes."); | |
777a54c9 | 82 | // Wait until there are multiple CPUs before starting test. |
8e4ec3d0 | 83 | torture_param(int, holdoff, IS_BUILTIN(CONFIG_RCU_REF_SCALE_TEST) ? 10 : 0, |
777a54c9 | 84 | "Holdoff time before test start (s)"); |
a6889bec PM |
85 | // Number of typesafe_lookup structures, that is, the degree of concurrency. |
86 | torture_param(long, lookup_instances, 0, "Number of typesafe_lookup structures."); | |
777a54c9 | 87 | // Number of loops per experiment, all readers execute operations concurrently. |
4dd72a33 | 88 | torture_param(long, loops, 10000, "Number of loops per experiment."); |
8fc28783 PM |
89 | // Number of readers, with -1 defaulting to about 75% of the CPUs. |
90 | torture_param(int, nreaders, -1, "Number of readers, -1 for 75% of CPUs."); | |
91 | // Number of runs. | |
92 | torture_param(int, nruns, 30, "Number of experiments to run."); | |
918b351d PM |
93 | // Reader delay in nanoseconds, 0 for no delay. |
94 | torture_param(int, readdelay, 0, "Read-side delay in nanoseconds."); | |
653ed64b JFG |
95 | |
96 | #ifdef MODULE | |
1fbeb3a8 | 97 | # define REFSCALE_SHUTDOWN 0 |
653ed64b | 98 | #else |
1fbeb3a8 | 99 | # define REFSCALE_SHUTDOWN 1 |
653ed64b JFG |
100 | #endif |
101 | ||
1fbeb3a8 PM |
102 | torture_param(bool, shutdown, REFSCALE_SHUTDOWN, |
103 | "Shutdown at end of scalability tests."); | |
653ed64b JFG |
104 | |
105 | struct reader_task { | |
106 | struct task_struct *task; | |
af2789db | 107 | int start_reader; |
653ed64b JFG |
108 | wait_queue_head_t wq; |
109 | u64 last_duration_ns; | |
653ed64b JFG |
110 | }; |
111 | ||
112 | static struct task_struct *shutdown_task; | |
113 | static wait_queue_head_t shutdown_wq; | |
114 | ||
115 | static struct task_struct *main_task; | |
116 | static wait_queue_head_t main_wq; | |
117 | static int shutdown_start; | |
118 | ||
119 | static struct reader_task *reader_tasks; | |
653ed64b JFG |
120 | |
121 | // Number of readers that are part of the current experiment. | |
122 | static atomic_t nreaders_exp; | |
123 | ||
124 | // Use to wait for all threads to start. | |
125 | static atomic_t n_init; | |
86e0da2b | 126 | static atomic_t n_started; |
2db0bda3 PM |
127 | static atomic_t n_warmedup; |
128 | static atomic_t n_cooleddown; | |
653ed64b JFG |
129 | |
130 | // Track which experiment is currently running. | |
131 | static int exp_idx; | |
132 | ||
133 | // Operations vector for selecting different types of tests. | |
1fbeb3a8 | 134 | struct ref_scale_ops { |
3c6496c8 | 135 | bool (*init)(void); |
653ed64b | 136 | void (*cleanup)(void); |
75dd8efe | 137 | void (*readsection)(const int nloops); |
918b351d | 138 | void (*delaysection)(const int nloops, const int udl, const int ndl); |
653ed64b JFG |
139 | const char *name; |
140 | }; | |
141 | ||
8f35fefa | 142 | static const struct ref_scale_ops *cur_ops; |
653ed64b | 143 | |
918b351d PM |
144 | static void un_delay(const int udl, const int ndl) |
145 | { | |
146 | if (udl) | |
147 | udelay(udl); | |
148 | if (ndl) | |
149 | ndelay(ndl); | |
150 | } | |
151 | ||
75dd8efe | 152 | static void ref_rcu_read_section(const int nloops) |
653ed64b | 153 | { |
75dd8efe | 154 | int i; |
653ed64b | 155 | |
75dd8efe PM |
156 | for (i = nloops; i >= 0; i--) { |
157 | rcu_read_lock(); | |
158 | rcu_read_unlock(); | |
159 | } | |
653ed64b JFG |
160 | } |
161 | ||
918b351d | 162 | static void ref_rcu_delay_section(const int nloops, const int udl, const int ndl) |
b4d1e34f PM |
163 | { |
164 | int i; | |
165 | ||
166 | for (i = nloops; i >= 0; i--) { | |
167 | rcu_read_lock(); | |
918b351d | 168 | un_delay(udl, ndl); |
b4d1e34f PM |
169 | rcu_read_unlock(); |
170 | } | |
171 | } | |
172 | ||
3c6496c8 | 173 | static bool rcu_sync_scale_init(void) |
653ed64b | 174 | { |
3c6496c8 | 175 | return true; |
653ed64b JFG |
176 | } |
177 | ||
8f35fefa | 178 | static const struct ref_scale_ops rcu_ops = { |
1fbeb3a8 | 179 | .init = rcu_sync_scale_init, |
75dd8efe | 180 | .readsection = ref_rcu_read_section, |
b4d1e34f | 181 | .delaysection = ref_rcu_delay_section, |
653ed64b JFG |
182 | .name = "rcu" |
183 | }; | |
184 | ||
1fbeb3a8 PM |
185 | // Definitions for SRCU ref scale testing. |
186 | DEFINE_STATIC_SRCU(srcu_refctl_scale); | |
187 | static struct srcu_struct *srcu_ctlp = &srcu_refctl_scale; | |
653ed64b | 188 | |
1fbeb3a8 | 189 | static void srcu_ref_scale_read_section(const int nloops) |
653ed64b | 190 | { |
75dd8efe PM |
191 | int i; |
192 | int idx; | |
653ed64b | 193 | |
75dd8efe PM |
194 | for (i = nloops; i >= 0; i--) { |
195 | idx = srcu_read_lock(srcu_ctlp); | |
196 | srcu_read_unlock(srcu_ctlp, idx); | |
197 | } | |
653ed64b JFG |
198 | } |
199 | ||
1fbeb3a8 | 200 | static void srcu_ref_scale_delay_section(const int nloops, const int udl, const int ndl) |
b4d1e34f PM |
201 | { |
202 | int i; | |
203 | int idx; | |
204 | ||
205 | for (i = nloops; i >= 0; i--) { | |
206 | idx = srcu_read_lock(srcu_ctlp); | |
918b351d | 207 | un_delay(udl, ndl); |
b4d1e34f PM |
208 | srcu_read_unlock(srcu_ctlp, idx); |
209 | } | |
210 | } | |
211 | ||
8f35fefa | 212 | static const struct ref_scale_ops srcu_ops = { |
1fbeb3a8 PM |
213 | .init = rcu_sync_scale_init, |
214 | .readsection = srcu_ref_scale_read_section, | |
215 | .delaysection = srcu_ref_scale_delay_section, | |
653ed64b JFG |
216 | .name = "srcu" |
217 | }; | |
218 | ||
4c3fca0f PM |
219 | static void srcu_fast_ref_scale_read_section(const int nloops) |
220 | { | |
221 | int i; | |
222 | struct srcu_ctr __percpu *scp; | |
223 | ||
224 | for (i = nloops; i >= 0; i--) { | |
225 | scp = srcu_read_lock_fast(srcu_ctlp); | |
226 | srcu_read_unlock_fast(srcu_ctlp, scp); | |
227 | } | |
228 | } | |
229 | ||
230 | static void srcu_fast_ref_scale_delay_section(const int nloops, const int udl, const int ndl) | |
231 | { | |
232 | int i; | |
233 | struct srcu_ctr __percpu *scp; | |
234 | ||
235 | for (i = nloops; i >= 0; i--) { | |
236 | scp = srcu_read_lock_fast(srcu_ctlp); | |
237 | un_delay(udl, ndl); | |
238 | srcu_read_unlock_fast(srcu_ctlp, scp); | |
239 | } | |
240 | } | |
241 | ||
242 | static const struct ref_scale_ops srcu_fast_ops = { | |
243 | .init = rcu_sync_scale_init, | |
244 | .readsection = srcu_fast_ref_scale_read_section, | |
245 | .delaysection = srcu_fast_ref_scale_delay_section, | |
246 | .name = "srcu-fast" | |
247 | }; | |
248 | ||
6a2c0255 PM |
249 | static void srcu_lite_ref_scale_read_section(const int nloops) |
250 | { | |
251 | int i; | |
252 | int idx; | |
253 | ||
254 | for (i = nloops; i >= 0; i--) { | |
255 | idx = srcu_read_lock_lite(srcu_ctlp); | |
256 | srcu_read_unlock_lite(srcu_ctlp, idx); | |
257 | } | |
258 | } | |
259 | ||
260 | static void srcu_lite_ref_scale_delay_section(const int nloops, const int udl, const int ndl) | |
261 | { | |
262 | int i; | |
263 | int idx; | |
264 | ||
265 | for (i = nloops; i >= 0; i--) { | |
266 | idx = srcu_read_lock_lite(srcu_ctlp); | |
267 | un_delay(udl, ndl); | |
268 | srcu_read_unlock_lite(srcu_ctlp, idx); | |
269 | } | |
270 | } | |
271 | ||
272 | static const struct ref_scale_ops srcu_lite_ops = { | |
273 | .init = rcu_sync_scale_init, | |
274 | .readsection = srcu_lite_ref_scale_read_section, | |
275 | .delaysection = srcu_lite_ref_scale_delay_section, | |
276 | .name = "srcu-lite" | |
277 | }; | |
278 | ||
5f654af1 PM |
279 | #ifdef CONFIG_TASKS_RCU |
280 | ||
1fbeb3a8 | 281 | // Definitions for RCU Tasks ref scale testing: Empty read markers. |
e13ef442 | 282 | // These definitions also work for RCU Rude readers. |
1fbeb3a8 | 283 | static void rcu_tasks_ref_scale_read_section(const int nloops) |
e13ef442 PM |
284 | { |
285 | int i; | |
286 | ||
287 | for (i = nloops; i >= 0; i--) | |
288 | continue; | |
289 | } | |
290 | ||
1fbeb3a8 | 291 | static void rcu_tasks_ref_scale_delay_section(const int nloops, const int udl, const int ndl) |
e13ef442 PM |
292 | { |
293 | int i; | |
294 | ||
295 | for (i = nloops; i >= 0; i--) | |
296 | un_delay(udl, ndl); | |
297 | } | |
298 | ||
8f35fefa | 299 | static const struct ref_scale_ops rcu_tasks_ops = { |
1fbeb3a8 PM |
300 | .init = rcu_sync_scale_init, |
301 | .readsection = rcu_tasks_ref_scale_read_section, | |
302 | .delaysection = rcu_tasks_ref_scale_delay_section, | |
e13ef442 PM |
303 | .name = "rcu-tasks" |
304 | }; | |
305 | ||
5f654af1 PM |
306 | #define RCU_TASKS_OPS &rcu_tasks_ops, |
307 | ||
308 | #else // #ifdef CONFIG_TASKS_RCU | |
309 | ||
310 | #define RCU_TASKS_OPS | |
311 | ||
312 | #endif // #else // #ifdef CONFIG_TASKS_RCU | |
313 | ||
dec86781 PM |
314 | #ifdef CONFIG_TASKS_TRACE_RCU |
315 | ||
1fbeb3a8 PM |
316 | // Definitions for RCU Tasks Trace ref scale testing. |
317 | static void rcu_trace_ref_scale_read_section(const int nloops) | |
72bb749e PM |
318 | { |
319 | int i; | |
320 | ||
321 | for (i = nloops; i >= 0; i--) { | |
322 | rcu_read_lock_trace(); | |
323 | rcu_read_unlock_trace(); | |
324 | } | |
325 | } | |
326 | ||
1fbeb3a8 | 327 | static void rcu_trace_ref_scale_delay_section(const int nloops, const int udl, const int ndl) |
72bb749e PM |
328 | { |
329 | int i; | |
330 | ||
331 | for (i = nloops; i >= 0; i--) { | |
332 | rcu_read_lock_trace(); | |
333 | un_delay(udl, ndl); | |
334 | rcu_read_unlock_trace(); | |
335 | } | |
336 | } | |
337 | ||
8f35fefa | 338 | static const struct ref_scale_ops rcu_trace_ops = { |
1fbeb3a8 PM |
339 | .init = rcu_sync_scale_init, |
340 | .readsection = rcu_trace_ref_scale_read_section, | |
341 | .delaysection = rcu_trace_ref_scale_delay_section, | |
72bb749e PM |
342 | .name = "rcu-trace" |
343 | }; | |
344 | ||
dec86781 PM |
345 | #define RCU_TRACE_OPS &rcu_trace_ops, |
346 | ||
347 | #else // #ifdef CONFIG_TASKS_TRACE_RCU | |
348 | ||
349 | #define RCU_TRACE_OPS | |
350 | ||
351 | #endif // #else // #ifdef CONFIG_TASKS_TRACE_RCU | |
352 | ||
653ed64b JFG |
353 | // Definitions for reference count |
354 | static atomic_t refcnt; | |
355 | ||
b4d1e34f | 356 | static void ref_refcnt_section(const int nloops) |
653ed64b | 357 | { |
75dd8efe | 358 | int i; |
653ed64b | 359 | |
75dd8efe PM |
360 | for (i = nloops; i >= 0; i--) { |
361 | atomic_inc(&refcnt); | |
362 | atomic_dec(&refcnt); | |
363 | } | |
653ed64b JFG |
364 | } |
365 | ||
918b351d | 366 | static void ref_refcnt_delay_section(const int nloops, const int udl, const int ndl) |
b4d1e34f PM |
367 | { |
368 | int i; | |
369 | ||
370 | for (i = nloops; i >= 0; i--) { | |
371 | atomic_inc(&refcnt); | |
918b351d | 372 | un_delay(udl, ndl); |
b4d1e34f PM |
373 | atomic_dec(&refcnt); |
374 | } | |
375 | } | |
376 | ||
8f35fefa | 377 | static const struct ref_scale_ops refcnt_ops = { |
1fbeb3a8 | 378 | .init = rcu_sync_scale_init, |
b4d1e34f PM |
379 | .readsection = ref_refcnt_section, |
380 | .delaysection = ref_refcnt_delay_section, | |
653ed64b JFG |
381 | .name = "refcnt" |
382 | }; | |
383 | ||
384 | // Definitions for rwlock | |
385 | static rwlock_t test_rwlock; | |
386 | ||
3c6496c8 | 387 | static bool ref_rwlock_init(void) |
653ed64b JFG |
388 | { |
389 | rwlock_init(&test_rwlock); | |
3c6496c8 | 390 | return true; |
653ed64b JFG |
391 | } |
392 | ||
b4d1e34f PM |
393 | static void ref_rwlock_section(const int nloops) |
394 | { | |
395 | int i; | |
396 | ||
397 | for (i = nloops; i >= 0; i--) { | |
398 | read_lock(&test_rwlock); | |
399 | read_unlock(&test_rwlock); | |
400 | } | |
401 | } | |
402 | ||
918b351d | 403 | static void ref_rwlock_delay_section(const int nloops, const int udl, const int ndl) |
653ed64b | 404 | { |
75dd8efe | 405 | int i; |
653ed64b | 406 | |
75dd8efe PM |
407 | for (i = nloops; i >= 0; i--) { |
408 | read_lock(&test_rwlock); | |
918b351d | 409 | un_delay(udl, ndl); |
75dd8efe PM |
410 | read_unlock(&test_rwlock); |
411 | } | |
653ed64b JFG |
412 | } |
413 | ||
8f35fefa | 414 | static const struct ref_scale_ops rwlock_ops = { |
b4d1e34f PM |
415 | .init = ref_rwlock_init, |
416 | .readsection = ref_rwlock_section, | |
417 | .delaysection = ref_rwlock_delay_section, | |
653ed64b JFG |
418 | .name = "rwlock" |
419 | }; | |
420 | ||
421 | // Definitions for rwsem | |
422 | static struct rw_semaphore test_rwsem; | |
423 | ||
3c6496c8 | 424 | static bool ref_rwsem_init(void) |
653ed64b JFG |
425 | { |
426 | init_rwsem(&test_rwsem); | |
3c6496c8 | 427 | return true; |
653ed64b JFG |
428 | } |
429 | ||
b4d1e34f | 430 | static void ref_rwsem_section(const int nloops) |
653ed64b | 431 | { |
75dd8efe | 432 | int i; |
653ed64b | 433 | |
75dd8efe PM |
434 | for (i = nloops; i >= 0; i--) { |
435 | down_read(&test_rwsem); | |
436 | up_read(&test_rwsem); | |
437 | } | |
653ed64b JFG |
438 | } |
439 | ||
918b351d | 440 | static void ref_rwsem_delay_section(const int nloops, const int udl, const int ndl) |
b4d1e34f PM |
441 | { |
442 | int i; | |
443 | ||
444 | for (i = nloops; i >= 0; i--) { | |
445 | down_read(&test_rwsem); | |
918b351d | 446 | un_delay(udl, ndl); |
b4d1e34f PM |
447 | up_read(&test_rwsem); |
448 | } | |
449 | } | |
450 | ||
8f35fefa | 451 | static const struct ref_scale_ops rwsem_ops = { |
b4d1e34f PM |
452 | .init = ref_rwsem_init, |
453 | .readsection = ref_rwsem_section, | |
454 | .delaysection = ref_rwsem_delay_section, | |
653ed64b JFG |
455 | .name = "rwsem" |
456 | }; | |
457 | ||
e9b800db | 458 | // Definitions for global spinlock |
7bf336fb | 459 | static DEFINE_RAW_SPINLOCK(test_lock); |
e9b800db PM |
460 | |
461 | static void ref_lock_section(const int nloops) | |
462 | { | |
463 | int i; | |
464 | ||
465 | preempt_disable(); | |
466 | for (i = nloops; i >= 0; i--) { | |
7bf336fb Z |
467 | raw_spin_lock(&test_lock); |
468 | raw_spin_unlock(&test_lock); | |
e9b800db PM |
469 | } |
470 | preempt_enable(); | |
471 | } | |
472 | ||
473 | static void ref_lock_delay_section(const int nloops, const int udl, const int ndl) | |
474 | { | |
475 | int i; | |
476 | ||
477 | preempt_disable(); | |
478 | for (i = nloops; i >= 0; i--) { | |
7bf336fb | 479 | raw_spin_lock(&test_lock); |
e9b800db | 480 | un_delay(udl, ndl); |
7bf336fb | 481 | raw_spin_unlock(&test_lock); |
e9b800db PM |
482 | } |
483 | preempt_enable(); | |
484 | } | |
485 | ||
8f35fefa | 486 | static const struct ref_scale_ops lock_ops = { |
e9b800db PM |
487 | .readsection = ref_lock_section, |
488 | .delaysection = ref_lock_delay_section, | |
489 | .name = "lock" | |
490 | }; | |
491 | ||
492 | // Definitions for global irq-save spinlock | |
493 | ||
494 | static void ref_lock_irq_section(const int nloops) | |
495 | { | |
496 | unsigned long flags; | |
497 | int i; | |
498 | ||
499 | preempt_disable(); | |
500 | for (i = nloops; i >= 0; i--) { | |
7bf336fb Z |
501 | raw_spin_lock_irqsave(&test_lock, flags); |
502 | raw_spin_unlock_irqrestore(&test_lock, flags); | |
e9b800db PM |
503 | } |
504 | preempt_enable(); | |
505 | } | |
506 | ||
507 | static void ref_lock_irq_delay_section(const int nloops, const int udl, const int ndl) | |
508 | { | |
509 | unsigned long flags; | |
510 | int i; | |
511 | ||
512 | preempt_disable(); | |
513 | for (i = nloops; i >= 0; i--) { | |
7bf336fb | 514 | raw_spin_lock_irqsave(&test_lock, flags); |
e9b800db | 515 | un_delay(udl, ndl); |
7bf336fb | 516 | raw_spin_unlock_irqrestore(&test_lock, flags); |
e9b800db PM |
517 | } |
518 | preempt_enable(); | |
519 | } | |
520 | ||
8f35fefa | 521 | static const struct ref_scale_ops lock_irq_ops = { |
e9b800db PM |
522 | .readsection = ref_lock_irq_section, |
523 | .delaysection = ref_lock_irq_delay_section, | |
524 | .name = "lock-irq" | |
525 | }; | |
526 | ||
527 | // Definitions acquire-release. | |
528 | static DEFINE_PER_CPU(unsigned long, test_acqrel); | |
529 | ||
530 | static void ref_acqrel_section(const int nloops) | |
531 | { | |
532 | unsigned long x; | |
533 | int i; | |
534 | ||
535 | preempt_disable(); | |
536 | for (i = nloops; i >= 0; i--) { | |
537 | x = smp_load_acquire(this_cpu_ptr(&test_acqrel)); | |
538 | smp_store_release(this_cpu_ptr(&test_acqrel), x + 1); | |
539 | } | |
540 | preempt_enable(); | |
541 | } | |
542 | ||
543 | static void ref_acqrel_delay_section(const int nloops, const int udl, const int ndl) | |
544 | { | |
545 | unsigned long x; | |
546 | int i; | |
547 | ||
548 | preempt_disable(); | |
549 | for (i = nloops; i >= 0; i--) { | |
550 | x = smp_load_acquire(this_cpu_ptr(&test_acqrel)); | |
551 | un_delay(udl, ndl); | |
552 | smp_store_release(this_cpu_ptr(&test_acqrel), x + 1); | |
553 | } | |
554 | preempt_enable(); | |
555 | } | |
556 | ||
8f35fefa | 557 | static const struct ref_scale_ops acqrel_ops = { |
e9b800db PM |
558 | .readsection = ref_acqrel_section, |
559 | .delaysection = ref_acqrel_delay_section, | |
560 | .name = "acqrel" | |
561 | }; | |
562 | ||
25f6fa53 PM |
563 | static volatile u64 stopopts; |
564 | ||
1806b1f9 PM |
565 | static void ref_sched_clock_section(const int nloops) |
566 | { | |
567 | u64 x = 0; | |
568 | int i; | |
569 | ||
570 | preempt_disable(); | |
571 | for (i = nloops; i >= 0; i--) | |
572 | x += sched_clock(); | |
573 | preempt_enable(); | |
574 | stopopts = x; | |
575 | } | |
576 | ||
577 | static void ref_sched_clock_delay_section(const int nloops, const int udl, const int ndl) | |
578 | { | |
579 | u64 x = 0; | |
580 | int i; | |
581 | ||
582 | preempt_disable(); | |
583 | for (i = nloops; i >= 0; i--) { | |
584 | x += sched_clock(); | |
585 | un_delay(udl, ndl); | |
586 | } | |
587 | preempt_enable(); | |
588 | stopopts = x; | |
589 | } | |
590 | ||
591 | static const struct ref_scale_ops sched_clock_ops = { | |
592 | .readsection = ref_sched_clock_section, | |
593 | .delaysection = ref_sched_clock_delay_section, | |
594 | .name = "sched-clock" | |
595 | }; | |
596 | ||
597 | ||
25f6fa53 PM |
598 | static void ref_clock_section(const int nloops) |
599 | { | |
600 | u64 x = 0; | |
601 | int i; | |
602 | ||
603 | preempt_disable(); | |
604 | for (i = nloops; i >= 0; i--) | |
605 | x += ktime_get_real_fast_ns(); | |
606 | preempt_enable(); | |
607 | stopopts = x; | |
608 | } | |
609 | ||
610 | static void ref_clock_delay_section(const int nloops, const int udl, const int ndl) | |
611 | { | |
612 | u64 x = 0; | |
613 | int i; | |
614 | ||
615 | preempt_disable(); | |
616 | for (i = nloops; i >= 0; i--) { | |
617 | x += ktime_get_real_fast_ns(); | |
618 | un_delay(udl, ndl); | |
619 | } | |
620 | preempt_enable(); | |
621 | stopopts = x; | |
622 | } | |
623 | ||
8f35fefa | 624 | static const struct ref_scale_ops clock_ops = { |
25f6fa53 PM |
625 | .readsection = ref_clock_section, |
626 | .delaysection = ref_clock_delay_section, | |
627 | .name = "clock" | |
628 | }; | |
629 | ||
b5a2801f PM |
630 | static void ref_jiffies_section(const int nloops) |
631 | { | |
632 | u64 x = 0; | |
633 | int i; | |
634 | ||
635 | preempt_disable(); | |
636 | for (i = nloops; i >= 0; i--) | |
637 | x += jiffies; | |
638 | preempt_enable(); | |
639 | stopopts = x; | |
640 | } | |
641 | ||
642 | static void ref_jiffies_delay_section(const int nloops, const int udl, const int ndl) | |
643 | { | |
644 | u64 x = 0; | |
645 | int i; | |
646 | ||
647 | preempt_disable(); | |
648 | for (i = nloops; i >= 0; i--) { | |
649 | x += jiffies; | |
650 | un_delay(udl, ndl); | |
651 | } | |
652 | preempt_enable(); | |
653 | stopopts = x; | |
654 | } | |
655 | ||
8f35fefa | 656 | static const struct ref_scale_ops jiffies_ops = { |
b5a2801f PM |
657 | .readsection = ref_jiffies_section, |
658 | .delaysection = ref_jiffies_delay_section, | |
659 | .name = "jiffies" | |
660 | }; | |
661 | ||
a6889bec PM |
662 | //////////////////////////////////////////////////////////////////////// |
663 | // | |
664 | // Methods leveraging SLAB_TYPESAFE_BY_RCU. | |
665 | // | |
666 | ||
667 | // Item to look up in a typesafe manner. Array of pointers to these. | |
668 | struct refscale_typesafe { | |
669 | atomic_t rts_refctr; // Used by all flavors | |
670 | spinlock_t rts_lock; | |
671 | seqlock_t rts_seqlock; | |
672 | unsigned int a; | |
673 | unsigned int b; | |
674 | }; | |
675 | ||
676 | static struct kmem_cache *typesafe_kmem_cachep; | |
677 | static struct refscale_typesafe **rtsarray; | |
678 | static long rtsarray_size; | |
679 | static DEFINE_TORTURE_RANDOM_PERCPU(refscale_rand); | |
680 | static bool (*rts_acquire)(struct refscale_typesafe *rtsp, unsigned int *start); | |
681 | static bool (*rts_release)(struct refscale_typesafe *rtsp, unsigned int start); | |
682 | ||
683 | // Conditionally acquire an explicit in-structure reference count. | |
684 | static bool typesafe_ref_acquire(struct refscale_typesafe *rtsp, unsigned int *start) | |
685 | { | |
686 | return atomic_inc_not_zero(&rtsp->rts_refctr); | |
687 | } | |
688 | ||
689 | // Unconditionally release an explicit in-structure reference count. | |
690 | static bool typesafe_ref_release(struct refscale_typesafe *rtsp, unsigned int start) | |
691 | { | |
692 | if (!atomic_dec_return(&rtsp->rts_refctr)) { | |
693 | WRITE_ONCE(rtsp->a, rtsp->a + 1); | |
694 | kmem_cache_free(typesafe_kmem_cachep, rtsp); | |
695 | } | |
696 | return true; | |
697 | } | |
698 | ||
699 | // Unconditionally acquire an explicit in-structure spinlock. | |
700 | static bool typesafe_lock_acquire(struct refscale_typesafe *rtsp, unsigned int *start) | |
701 | { | |
702 | spin_lock(&rtsp->rts_lock); | |
703 | return true; | |
704 | } | |
705 | ||
706 | // Unconditionally release an explicit in-structure spinlock. | |
707 | static bool typesafe_lock_release(struct refscale_typesafe *rtsp, unsigned int start) | |
708 | { | |
709 | spin_unlock(&rtsp->rts_lock); | |
710 | return true; | |
711 | } | |
712 | ||
713 | // Unconditionally acquire an explicit in-structure sequence lock. | |
714 | static bool typesafe_seqlock_acquire(struct refscale_typesafe *rtsp, unsigned int *start) | |
715 | { | |
716 | *start = read_seqbegin(&rtsp->rts_seqlock); | |
717 | return true; | |
718 | } | |
719 | ||
720 | // Conditionally release an explicit in-structure sequence lock. Return | |
721 | // true if this release was successful, that is, if no retry is required. | |
722 | static bool typesafe_seqlock_release(struct refscale_typesafe *rtsp, unsigned int start) | |
723 | { | |
724 | return !read_seqretry(&rtsp->rts_seqlock, start); | |
725 | } | |
726 | ||
727 | // Do a read-side critical section with the specified delay in | |
728 | // microseconds and nanoseconds inserted so as to increase probability | |
729 | // of failure. | |
730 | static void typesafe_delay_section(const int nloops, const int udl, const int ndl) | |
731 | { | |
732 | unsigned int a; | |
733 | unsigned int b; | |
734 | int i; | |
735 | long idx; | |
736 | struct refscale_typesafe *rtsp; | |
737 | unsigned int start; | |
738 | ||
739 | for (i = nloops; i >= 0; i--) { | |
740 | preempt_disable(); | |
741 | idx = torture_random(this_cpu_ptr(&refscale_rand)) % rtsarray_size; | |
742 | preempt_enable(); | |
743 | retry: | |
744 | rcu_read_lock(); | |
745 | rtsp = rcu_dereference(rtsarray[idx]); | |
746 | a = READ_ONCE(rtsp->a); | |
747 | if (!rts_acquire(rtsp, &start)) { | |
748 | rcu_read_unlock(); | |
749 | goto retry; | |
750 | } | |
751 | if (a != READ_ONCE(rtsp->a)) { | |
752 | (void)rts_release(rtsp, start); | |
753 | rcu_read_unlock(); | |
754 | goto retry; | |
755 | } | |
756 | un_delay(udl, ndl); | |
730c3ed4 | 757 | b = READ_ONCE(rtsp->a); |
a6889bec PM |
758 | // Remember, seqlock read-side release can fail. |
759 | if (!rts_release(rtsp, start)) { | |
760 | rcu_read_unlock(); | |
761 | goto retry; | |
762 | } | |
a6889bec PM |
763 | WARN_ONCE(a != b, "Re-read of ->a changed from %u to %u.\n", a, b); |
764 | b = rtsp->b; | |
765 | rcu_read_unlock(); | |
766 | WARN_ON_ONCE(a * a != b); | |
767 | } | |
768 | } | |
769 | ||
770 | // Because the acquisition and release methods are expensive, there | |
771 | // is no point in optimizing away the un_delay() function's two checks. | |
772 | // Thus simply define typesafe_read_section() as a simple wrapper around | |
773 | // typesafe_delay_section(). | |
774 | static void typesafe_read_section(const int nloops) | |
775 | { | |
776 | typesafe_delay_section(nloops, 0, 0); | |
777 | } | |
778 | ||
779 | // Allocate and initialize one refscale_typesafe structure. | |
780 | static struct refscale_typesafe *typesafe_alloc_one(void) | |
781 | { | |
782 | struct refscale_typesafe *rtsp; | |
783 | ||
784 | rtsp = kmem_cache_alloc(typesafe_kmem_cachep, GFP_KERNEL); | |
785 | if (!rtsp) | |
786 | return NULL; | |
787 | atomic_set(&rtsp->rts_refctr, 1); | |
788 | WRITE_ONCE(rtsp->a, rtsp->a + 1); | |
789 | WRITE_ONCE(rtsp->b, rtsp->a * rtsp->a); | |
790 | return rtsp; | |
791 | } | |
792 | ||
793 | // Slab-allocator constructor for refscale_typesafe structures created | |
794 | // out of a new slab of system memory. | |
795 | static void refscale_typesafe_ctor(void *rtsp_in) | |
796 | { | |
797 | struct refscale_typesafe *rtsp = rtsp_in; | |
798 | ||
799 | spin_lock_init(&rtsp->rts_lock); | |
800 | seqlock_init(&rtsp->rts_seqlock); | |
801 | preempt_disable(); | |
802 | rtsp->a = torture_random(this_cpu_ptr(&refscale_rand)); | |
803 | preempt_enable(); | |
804 | } | |
805 | ||
8f35fefa CJ |
806 | static const struct ref_scale_ops typesafe_ref_ops; |
807 | static const struct ref_scale_ops typesafe_lock_ops; | |
808 | static const struct ref_scale_ops typesafe_seqlock_ops; | |
a6889bec PM |
809 | |
810 | // Initialize for a typesafe test. | |
811 | static bool typesafe_init(void) | |
812 | { | |
813 | long idx; | |
814 | long si = lookup_instances; | |
815 | ||
816 | typesafe_kmem_cachep = kmem_cache_create("refscale_typesafe", | |
817 | sizeof(struct refscale_typesafe), sizeof(void *), | |
818 | SLAB_TYPESAFE_BY_RCU, refscale_typesafe_ctor); | |
819 | if (!typesafe_kmem_cachep) | |
820 | return false; | |
821 | if (si < 0) | |
822 | si = -si * nr_cpu_ids; | |
823 | else if (si == 0) | |
824 | si = nr_cpu_ids; | |
825 | rtsarray_size = si; | |
826 | rtsarray = kcalloc(si, sizeof(*rtsarray), GFP_KERNEL); | |
827 | if (!rtsarray) | |
828 | return false; | |
829 | for (idx = 0; idx < rtsarray_size; idx++) { | |
830 | rtsarray[idx] = typesafe_alloc_one(); | |
831 | if (!rtsarray[idx]) | |
832 | return false; | |
833 | } | |
834 | if (cur_ops == &typesafe_ref_ops) { | |
835 | rts_acquire = typesafe_ref_acquire; | |
836 | rts_release = typesafe_ref_release; | |
837 | } else if (cur_ops == &typesafe_lock_ops) { | |
838 | rts_acquire = typesafe_lock_acquire; | |
839 | rts_release = typesafe_lock_release; | |
840 | } else if (cur_ops == &typesafe_seqlock_ops) { | |
841 | rts_acquire = typesafe_seqlock_acquire; | |
842 | rts_release = typesafe_seqlock_release; | |
843 | } else { | |
844 | WARN_ON_ONCE(1); | |
845 | return false; | |
846 | } | |
847 | return true; | |
848 | } | |
849 | ||
850 | // Clean up after a typesafe test. | |
851 | static void typesafe_cleanup(void) | |
852 | { | |
853 | long idx; | |
854 | ||
855 | if (rtsarray) { | |
856 | for (idx = 0; idx < rtsarray_size; idx++) | |
857 | kmem_cache_free(typesafe_kmem_cachep, rtsarray[idx]); | |
858 | kfree(rtsarray); | |
859 | rtsarray = NULL; | |
860 | rtsarray_size = 0; | |
861 | } | |
862 | kmem_cache_destroy(typesafe_kmem_cachep); | |
863 | typesafe_kmem_cachep = NULL; | |
864 | rts_acquire = NULL; | |
865 | rts_release = NULL; | |
866 | } | |
867 | ||
868 | // The typesafe_init() function distinguishes these structures by address. | |
8f35fefa | 869 | static const struct ref_scale_ops typesafe_ref_ops = { |
a6889bec PM |
870 | .init = typesafe_init, |
871 | .cleanup = typesafe_cleanup, | |
872 | .readsection = typesafe_read_section, | |
873 | .delaysection = typesafe_delay_section, | |
874 | .name = "typesafe_ref" | |
875 | }; | |
876 | ||
8f35fefa | 877 | static const struct ref_scale_ops typesafe_lock_ops = { |
a6889bec PM |
878 | .init = typesafe_init, |
879 | .cleanup = typesafe_cleanup, | |
880 | .readsection = typesafe_read_section, | |
881 | .delaysection = typesafe_delay_section, | |
882 | .name = "typesafe_lock" | |
883 | }; | |
884 | ||
8f35fefa | 885 | static const struct ref_scale_ops typesafe_seqlock_ops = { |
a6889bec PM |
886 | .init = typesafe_init, |
887 | .cleanup = typesafe_cleanup, | |
888 | .readsection = typesafe_read_section, | |
889 | .delaysection = typesafe_delay_section, | |
890 | .name = "typesafe_seqlock" | |
891 | }; | |
892 | ||
1fbeb3a8 | 893 | static void rcu_scale_one_reader(void) |
b4d1e34f PM |
894 | { |
895 | if (readdelay <= 0) | |
896 | cur_ops->readsection(loops); | |
897 | else | |
918b351d | 898 | cur_ops->delaysection(loops, readdelay / 1000, readdelay % 1000); |
b4d1e34f PM |
899 | } |
900 | ||
ff9ba8db PM |
901 | // Warm up cache, or, if needed run a series of rcu_scale_one_reader() |
902 | // to allow multiple rcuscale guest OSes to collect mutually valid data. | |
903 | static void rcu_scale_warm_cool(void) | |
904 | { | |
905 | unsigned long jdone = jiffies + (guest_os_delay > 0 ? guest_os_delay * HZ : -1); | |
906 | ||
907 | do { | |
908 | rcu_scale_one_reader(); | |
909 | cond_resched(); | |
910 | } while (time_before(jiffies, jdone)); | |
911 | } | |
912 | ||
653ed64b JFG |
913 | // Reader kthread. Repeatedly does empty RCU read-side |
914 | // critical section, minimizing update-side interference. | |
915 | static int | |
1fbeb3a8 | 916 | ref_scale_reader(void *arg) |
653ed64b JFG |
917 | { |
918 | unsigned long flags; | |
919 | long me = (long)arg; | |
920 | struct reader_task *rt = &(reader_tasks[me]); | |
653ed64b JFG |
921 | u64 start; |
922 | s64 duration; | |
923 | ||
e76506f0 | 924 | VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: task started", me); |
05bc276c | 925 | WARN_ON_ONCE(set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids))); |
653ed64b JFG |
926 | set_user_nice(current, MAX_NICE); |
927 | atomic_inc(&n_init); | |
777a54c9 PM |
928 | if (holdoff) |
929 | schedule_timeout_interruptible(holdoff * HZ); | |
653ed64b | 930 | repeat: |
05bc276c | 931 | VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: waiting to start next experiment on cpu %d", me, raw_smp_processor_id()); |
653ed64b JFG |
932 | |
933 | // Wait for signal that this reader can start. | |
af2789db | 934 | wait_event(rt->wq, (atomic_read(&nreaders_exp) && smp_load_acquire(&rt->start_reader)) || |
653ed64b JFG |
935 | torture_must_stop()); |
936 | ||
937 | if (torture_must_stop()) | |
938 | goto end; | |
939 | ||
940 | // Make sure that the CPU is affinitized appropriately during testing. | |
046c06f5 | 941 | WARN_ON_ONCE(raw_smp_processor_id() != me % nr_cpu_ids); |
653ed64b | 942 | |
af2789db | 943 | WRITE_ONCE(rt->start_reader, 0); |
86e0da2b PM |
944 | if (!atomic_dec_return(&n_started)) |
945 | while (atomic_read_acquire(&n_started)) | |
946 | cpu_relax(); | |
653ed64b | 947 | |
e76506f0 | 948 | VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: experiment %d started", me, exp_idx); |
b864f89f | 949 | |
2db0bda3 PM |
950 | |
951 | // To reduce noise, do an initial cache-warming invocation, check | |
952 | // in, and then keep warming until everyone has checked in. | |
1fbeb3a8 | 953 | rcu_scale_one_reader(); |
2db0bda3 PM |
954 | if (!atomic_dec_return(&n_warmedup)) |
955 | while (atomic_read_acquire(&n_warmedup)) | |
1fbeb3a8 | 956 | rcu_scale_one_reader(); |
2db0bda3 PM |
957 | // Also keep interrupts disabled. This also has the effect |
958 | // of preventing entries into slow path for rcu_read_unlock(). | |
653ed64b JFG |
959 | local_irq_save(flags); |
960 | start = ktime_get_mono_fast_ns(); | |
961 | ||
1fbeb3a8 | 962 | rcu_scale_one_reader(); |
653ed64b JFG |
963 | |
964 | duration = ktime_get_mono_fast_ns() - start; | |
965 | local_irq_restore(flags); | |
966 | ||
967 | rt->last_duration_ns = WARN_ON_ONCE(duration < 0) ? 0 : duration; | |
2db0bda3 PM |
968 | // To reduce runtime-skew noise, do maintain-load invocations until |
969 | // everyone is done. | |
970 | if (!atomic_dec_return(&n_cooleddown)) | |
971 | while (atomic_read_acquire(&n_cooleddown)) | |
1fbeb3a8 | 972 | rcu_scale_one_reader(); |
653ed64b | 973 | |
b864f89f PM |
974 | if (atomic_dec_and_test(&nreaders_exp)) |
975 | wake_up(&main_wq); | |
653ed64b | 976 | |
e76506f0 PM |
977 | VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: experiment %d ended, (readers remaining=%d)", |
978 | me, exp_idx, atomic_read(&nreaders_exp)); | |
653ed64b | 979 | |
653ed64b JFG |
980 | if (!torture_must_stop()) |
981 | goto repeat; | |
982 | end: | |
1fbeb3a8 | 983 | torture_kthread_stopping("ref_scale_reader"); |
653ed64b JFG |
984 | return 0; |
985 | } | |
986 | ||
2990750b | 987 | static void reset_readers(void) |
653ed64b JFG |
988 | { |
989 | int i; | |
990 | struct reader_task *rt; | |
991 | ||
dbf28efd | 992 | for (i = 0; i < nreaders; i++) { |
653ed64b JFG |
993 | rt = &(reader_tasks[i]); |
994 | ||
995 | rt->last_duration_ns = 0; | |
996 | } | |
997 | } | |
998 | ||
999 | // Print the results of each reader and return the sum of all their durations. | |
2990750b | 1000 | static u64 process_durations(int n) |
653ed64b JFG |
1001 | { |
1002 | int i; | |
1003 | struct reader_task *rt; | |
4e39bb49 | 1004 | struct seq_buf s; |
2e90de76 | 1005 | char *buf; |
653ed64b JFG |
1006 | u64 sum = 0; |
1007 | ||
9880eb87 | 1008 | buf = kmalloc(800 + 64, GFP_KERNEL); |
2e90de76 PM |
1009 | if (!buf) |
1010 | return 0; | |
4e39bb49 CJ |
1011 | seq_buf_init(&s, buf, 800 + 64); |
1012 | ||
1013 | seq_buf_printf(&s, "Experiment #%d (Format: <THREAD-NUM>:<Total loop time in ns>)", | |
1014 | exp_idx); | |
653ed64b | 1015 | |
dbf28efd | 1016 | for (i = 0; i < n && !torture_must_stop(); i++) { |
653ed64b | 1017 | rt = &(reader_tasks[i]); |
653ed64b JFG |
1018 | |
1019 | if (i % 5 == 0) | |
4e39bb49 CJ |
1020 | seq_buf_putc(&s, '\n'); |
1021 | ||
1022 | if (seq_buf_used(&s) >= 800) { | |
1023 | pr_alert("%s", seq_buf_str(&s)); | |
1024 | seq_buf_clear(&s); | |
9880eb87 | 1025 | } |
4e39bb49 CJ |
1026 | |
1027 | seq_buf_printf(&s, "%d: %llu\t", i, rt->last_duration_ns); | |
653ed64b JFG |
1028 | |
1029 | sum += rt->last_duration_ns; | |
1030 | } | |
4e39bb49 | 1031 | pr_alert("%s\n", seq_buf_str(&s)); |
653ed64b | 1032 | |
2e90de76 | 1033 | kfree(buf); |
653ed64b JFG |
1034 | return sum; |
1035 | } | |
1036 | ||
1037 | // The main_func is the main orchestrator, it performs a bunch of | |
1038 | // experiments. For every experiment, it orders all the readers | |
1039 | // involved to start and waits for them to finish the experiment. It | |
1040 | // then reads their timestamps and starts the next experiment. Each | |
1041 | // experiment progresses from 1 concurrent reader to N of them at which | |
1042 | // point all the timestamps are printed. | |
1043 | static int main_func(void *arg) | |
1044 | { | |
1045 | int exp, r; | |
1046 | char buf1[64]; | |
f518f154 | 1047 | char *buf; |
dbf28efd | 1048 | u64 *result_avg; |
653ed64b JFG |
1049 | |
1050 | set_cpus_allowed_ptr(current, cpumask_of(nreaders % nr_cpu_ids)); | |
1051 | set_user_nice(current, MAX_NICE); | |
1052 | ||
1fbeb3a8 | 1053 | VERBOSE_SCALEOUT("main_func task started"); |
dbf28efd | 1054 | result_avg = kzalloc(nruns * sizeof(*result_avg), GFP_KERNEL); |
9880eb87 | 1055 | buf = kzalloc(800 + 64, GFP_KERNEL); |
f518f154 | 1056 | if (!result_avg || !buf) { |
4feeb9d5 | 1057 | SCALEOUT_ERRSTRING("out of memory"); |
c30c8763 | 1058 | goto oom_exit; |
f518f154 | 1059 | } |
777a54c9 PM |
1060 | if (holdoff) |
1061 | schedule_timeout_interruptible(holdoff * HZ); | |
653ed64b | 1062 | |
96af8669 PM |
1063 | // Wait for all threads to start. |
1064 | atomic_inc(&n_init); | |
1065 | while (atomic_read(&n_init) < nreaders + 1) | |
1066 | schedule_timeout_uninterruptible(1); | |
1067 | ||
653ed64b | 1068 | // Start exp readers up per experiment |
ff9ba8db | 1069 | rcu_scale_warm_cool(); |
dbf28efd | 1070 | for (exp = 0; exp < nruns && !torture_must_stop(); exp++) { |
653ed64b JFG |
1071 | if (torture_must_stop()) |
1072 | goto end; | |
1073 | ||
dbf28efd PM |
1074 | reset_readers(); |
1075 | atomic_set(&nreaders_exp, nreaders); | |
86e0da2b | 1076 | atomic_set(&n_started, nreaders); |
2db0bda3 PM |
1077 | atomic_set(&n_warmedup, nreaders); |
1078 | atomic_set(&n_cooleddown, nreaders); | |
653ed64b JFG |
1079 | |
1080 | exp_idx = exp; | |
1081 | ||
dbf28efd | 1082 | for (r = 0; r < nreaders; r++) { |
af2789db | 1083 | smp_store_release(&reader_tasks[r].start_reader, 1); |
653ed64b JFG |
1084 | wake_up(&reader_tasks[r].wq); |
1085 | } | |
1086 | ||
1fbeb3a8 | 1087 | VERBOSE_SCALEOUT("main_func: experiment started, waiting for %d readers", |
dbf28efd | 1088 | nreaders); |
653ed64b JFG |
1089 | |
1090 | wait_event(main_wq, | |
1091 | !atomic_read(&nreaders_exp) || torture_must_stop()); | |
1092 | ||
1fbeb3a8 | 1093 | VERBOSE_SCALEOUT("main_func: experiment ended"); |
653ed64b JFG |
1094 | |
1095 | if (torture_must_stop()) | |
1096 | goto end; | |
1097 | ||
7c944d7c | 1098 | result_avg[exp] = div_u64(1000 * process_durations(nreaders), nreaders * loops); |
653ed64b | 1099 | } |
ff9ba8db | 1100 | rcu_scale_warm_cool(); |
653ed64b JFG |
1101 | |
1102 | // Print the average of all experiments | |
1fbeb3a8 | 1103 | SCALEOUT("END OF TEST. Calculating average duration per loop (nanoseconds)...\n"); |
653ed64b | 1104 | |
9880eb87 | 1105 | pr_alert("Runs\tTime(ns)\n"); |
dbf28efd | 1106 | for (exp = 0; exp < nruns; exp++) { |
7c944d7c AB |
1107 | u64 avg; |
1108 | u32 rem; | |
1109 | ||
7c944d7c AB |
1110 | avg = div_u64_rem(result_avg[exp], 1000, &rem); |
1111 | sprintf(buf1, "%d\t%llu.%03u\n", exp + 1, avg, rem); | |
653ed64b | 1112 | strcat(buf, buf1); |
9880eb87 LZ |
1113 | if (strlen(buf) >= 800) { |
1114 | pr_alert("%s", buf); | |
1115 | buf[0] = 0; | |
1116 | } | |
653ed64b JFG |
1117 | } |
1118 | ||
9880eb87 | 1119 | pr_alert("%s", buf); |
653ed64b | 1120 | |
c30c8763 | 1121 | oom_exit: |
653ed64b JFG |
1122 | // This will shutdown everything including us. |
1123 | if (shutdown) { | |
1124 | shutdown_start = 1; | |
1125 | wake_up(&shutdown_wq); | |
1126 | } | |
1127 | ||
1128 | // Wait for torture to stop us | |
1129 | while (!torture_must_stop()) | |
1130 | schedule_timeout_uninterruptible(1); | |
1131 | ||
1132 | end: | |
1133 | torture_kthread_stopping("main_func"); | |
f518f154 PM |
1134 | kfree(result_avg); |
1135 | kfree(buf); | |
653ed64b JFG |
1136 | return 0; |
1137 | } | |
1138 | ||
1139 | static void | |
8f35fefa | 1140 | ref_scale_print_module_parms(const struct ref_scale_ops *cur_ops, const char *tag) |
653ed64b | 1141 | { |
1fbeb3a8 | 1142 | pr_alert("%s" SCALE_FLAG |
d6fea1dd PM |
1143 | "--- %s: verbose=%d verbose_batched=%d shutdown=%d holdoff=%d lookup_instances=%ld loops=%ld nreaders=%d nruns=%d readdelay=%d\n", scale_type, tag, |
1144 | verbose, verbose_batched, shutdown, holdoff, lookup_instances, loops, nreaders, nruns, readdelay); | |
653ed64b JFG |
1145 | } |
1146 | ||
1147 | static void | |
1fbeb3a8 | 1148 | ref_scale_cleanup(void) |
653ed64b JFG |
1149 | { |
1150 | int i; | |
1151 | ||
1152 | if (torture_cleanup_begin()) | |
1153 | return; | |
1154 | ||
1155 | if (!cur_ops) { | |
1156 | torture_cleanup_end(); | |
1157 | return; | |
1158 | } | |
1159 | ||
1160 | if (reader_tasks) { | |
1161 | for (i = 0; i < nreaders; i++) | |
1fbeb3a8 | 1162 | torture_stop_kthread("ref_scale_reader", |
653ed64b JFG |
1163 | reader_tasks[i].task); |
1164 | } | |
1165 | kfree(reader_tasks); | |
1166 | ||
1167 | torture_stop_kthread("main_task", main_task); | |
1168 | kfree(main_task); | |
1169 | ||
1fbeb3a8 | 1170 | // Do scale-type-specific cleanup operations. |
653ed64b JFG |
1171 | if (cur_ops->cleanup != NULL) |
1172 | cur_ops->cleanup(); | |
1173 | ||
1174 | torture_cleanup_end(); | |
1175 | } | |
1176 | ||
1177 | // Shutdown kthread. Just waits to be awakened, then shuts down system. | |
1178 | static int | |
1fbeb3a8 | 1179 | ref_scale_shutdown(void *arg) |
653ed64b | 1180 | { |
6bc6e6b2 | 1181 | wait_event_idle(shutdown_wq, shutdown_start); |
653ed64b JFG |
1182 | |
1183 | smp_mb(); // Wake before output. | |
1fbeb3a8 | 1184 | ref_scale_cleanup(); |
653ed64b JFG |
1185 | kernel_power_off(); |
1186 | ||
1187 | return -EINVAL; | |
1188 | } | |
1189 | ||
1190 | static int __init | |
1fbeb3a8 | 1191 | ref_scale_init(void) |
653ed64b JFG |
1192 | { |
1193 | long i; | |
1194 | int firsterr = 0; | |
8f35fefa | 1195 | static const struct ref_scale_ops *scale_ops[] = { |
4c3fca0f | 1196 | &rcu_ops, &srcu_ops, &srcu_fast_ops, &srcu_lite_ops, RCU_TRACE_OPS RCU_TASKS_OPS |
1806b1f9 PM |
1197 | &refcnt_ops, &rwlock_ops, &rwsem_ops, &lock_ops, &lock_irq_ops, |
1198 | &acqrel_ops, &sched_clock_ops, &clock_ops, &jiffies_ops, | |
1199 | &typesafe_ref_ops, &typesafe_lock_ops, &typesafe_seqlock_ops, | |
653ed64b JFG |
1200 | }; |
1201 | ||
1fbeb3a8 | 1202 | if (!torture_init_begin(scale_type, verbose)) |
653ed64b JFG |
1203 | return -EBUSY; |
1204 | ||
1fbeb3a8 PM |
1205 | for (i = 0; i < ARRAY_SIZE(scale_ops); i++) { |
1206 | cur_ops = scale_ops[i]; | |
1207 | if (strcmp(scale_type, cur_ops->name) == 0) | |
653ed64b JFG |
1208 | break; |
1209 | } | |
1fbeb3a8 PM |
1210 | if (i == ARRAY_SIZE(scale_ops)) { |
1211 | pr_alert("rcu-scale: invalid scale type: \"%s\"\n", scale_type); | |
1212 | pr_alert("rcu-scale types:"); | |
1213 | for (i = 0; i < ARRAY_SIZE(scale_ops); i++) | |
1214 | pr_cont(" %s", scale_ops[i]->name); | |
653ed64b | 1215 | pr_cont("\n"); |
653ed64b JFG |
1216 | firsterr = -EINVAL; |
1217 | cur_ops = NULL; | |
1218 | goto unwind; | |
1219 | } | |
1220 | if (cur_ops->init) | |
3c6496c8 PM |
1221 | if (!cur_ops->init()) { |
1222 | firsterr = -EUCLEAN; | |
1223 | goto unwind; | |
1224 | } | |
653ed64b | 1225 | |
1fbeb3a8 | 1226 | ref_scale_print_module_parms(cur_ops, "Start of test"); |
653ed64b JFG |
1227 | |
1228 | // Shutdown task | |
1229 | if (shutdown) { | |
1230 | init_waitqueue_head(&shutdown_wq); | |
1fbeb3a8 | 1231 | firsterr = torture_create_kthread(ref_scale_shutdown, NULL, |
653ed64b | 1232 | shutdown_task); |
ed60ad73 | 1233 | if (torture_init_error(firsterr)) |
653ed64b JFG |
1234 | goto unwind; |
1235 | schedule_timeout_uninterruptible(1); | |
1236 | } | |
1237 | ||
8fc28783 PM |
1238 | // Reader tasks (default to ~75% of online CPUs). |
1239 | if (nreaders < 0) | |
1240 | nreaders = (num_online_cpus() >> 1) + (num_online_cpus() >> 2); | |
0c6d18d8 PM |
1241 | if (WARN_ONCE(loops <= 0, "%s: loops = %ld, adjusted to 1\n", __func__, loops)) |
1242 | loops = 1; | |
1243 | if (WARN_ONCE(nreaders <= 0, "%s: nreaders = %d, adjusted to 1\n", __func__, nreaders)) | |
1244 | nreaders = 1; | |
1245 | if (WARN_ONCE(nruns <= 0, "%s: nruns = %d, adjusted to 1\n", __func__, nruns)) | |
1246 | nruns = 1; | |
653ed64b JFG |
1247 | reader_tasks = kcalloc(nreaders, sizeof(reader_tasks[0]), |
1248 | GFP_KERNEL); | |
1249 | if (!reader_tasks) { | |
4feeb9d5 | 1250 | SCALEOUT_ERRSTRING("out of memory"); |
653ed64b JFG |
1251 | firsterr = -ENOMEM; |
1252 | goto unwind; | |
1253 | } | |
1254 | ||
f71f22b6 | 1255 | VERBOSE_SCALEOUT("Starting %d reader threads", nreaders); |
653ed64b JFG |
1256 | |
1257 | for (i = 0; i < nreaders; i++) { | |
f5063e89 | 1258 | init_waitqueue_head(&reader_tasks[i].wq); |
1fbeb3a8 | 1259 | firsterr = torture_create_kthread(ref_scale_reader, (void *)i, |
653ed64b | 1260 | reader_tasks[i].task); |
ed60ad73 | 1261 | if (torture_init_error(firsterr)) |
653ed64b | 1262 | goto unwind; |
653ed64b JFG |
1263 | } |
1264 | ||
1265 | // Main Task | |
1266 | init_waitqueue_head(&main_wq); | |
1267 | firsterr = torture_create_kthread(main_func, NULL, main_task); | |
ed60ad73 | 1268 | if (torture_init_error(firsterr)) |
653ed64b | 1269 | goto unwind; |
653ed64b JFG |
1270 | |
1271 | torture_init_end(); | |
1272 | return 0; | |
1273 | ||
1274 | unwind: | |
1275 | torture_init_end(); | |
1fbeb3a8 | 1276 | ref_scale_cleanup(); |
bc80d353 PM |
1277 | if (shutdown) { |
1278 | WARN_ON(!IS_MODULE(CONFIG_RCU_REF_SCALE_TEST)); | |
1279 | kernel_power_off(); | |
1280 | } | |
653ed64b JFG |
1281 | return firsterr; |
1282 | } | |
1283 | ||
1fbeb3a8 PM |
1284 | module_init(ref_scale_init); |
1285 | module_exit(ref_scale_cleanup); |