Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // |
| 3 | // Performance test comparing RCU vs other mechanisms |
| 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> |
| 28 | #include <linux/reboot.h> |
| 29 | #include <linux/sched.h> |
| 30 | #include <linux/spinlock.h> |
| 31 | #include <linux/smp.h> |
| 32 | #include <linux/stat.h> |
| 33 | #include <linux/srcu.h> |
| 34 | #include <linux/slab.h> |
| 35 | #include <linux/torture.h> |
| 36 | #include <linux/types.h> |
| 37 | |
| 38 | #include "rcu.h" |
| 39 | |
| 40 | #define PERF_FLAG "-ref-perf: " |
| 41 | |
| 42 | #define PERFOUT(s, x...) \ |
| 43 | pr_alert("%s" PERF_FLAG s, perf_type, ## x) |
| 44 | |
| 45 | #define VERBOSE_PERFOUT(s, x...) \ |
| 46 | do { if (verbose) pr_alert("%s" PERF_FLAG s, perf_type, ## x); } while (0) |
| 47 | |
| 48 | #define VERBOSE_PERFOUT_ERRSTRING(s, x...) \ |
| 49 | do { if (verbose) pr_alert("%s" PERF_FLAG "!!! " s, perf_type, ## x); } while (0) |
| 50 | |
| 51 | MODULE_LICENSE("GPL"); |
| 52 | MODULE_AUTHOR("Joel Fernandes (Google) <joel@joelfernandes.org>"); |
| 53 | |
| 54 | static char *perf_type = "rcu"; |
| 55 | module_param(perf_type, charp, 0444); |
| 56 | MODULE_PARM_DESC(perf_type, "Type of test (rcu, srcu, refcnt, rwsem, rwlock."); |
| 57 | |
| 58 | torture_param(int, verbose, 0, "Enable verbose debugging printk()s"); |
| 59 | |
Paul E. McKenney | 777a54c | 2020-05-25 14:16:44 -0700 | [diff] [blame] | 60 | // Wait until there are multiple CPUs before starting test. |
| 61 | torture_param(int, holdoff, IS_BUILTIN(CONFIG_RCU_REF_PERF_TEST) ? 10 : 0, |
| 62 | "Holdoff time before test start (s)"); |
| 63 | // Number of loops per experiment, all readers execute operations concurrently. |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 64 | torture_param(long, loops, 10000000, "Number of loops per experiment."); |
Paul E. McKenney | 8fc2878 | 2020-05-25 15:48:38 -0700 | [diff] [blame] | 65 | // Number of readers, with -1 defaulting to about 75% of the CPUs. |
| 66 | torture_param(int, nreaders, -1, "Number of readers, -1 for 75% of CPUs."); |
| 67 | // Number of runs. |
| 68 | torture_param(int, nruns, 30, "Number of experiments to run."); |
| 69 | // Reader delay in nanoseconds, 0 for no delay. |
| 70 | torture_param(int, readdelay, 0, "Read-side delay in nanoseconds."); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 71 | |
| 72 | #ifdef MODULE |
| 73 | # define REFPERF_SHUTDOWN 0 |
| 74 | #else |
| 75 | # define REFPERF_SHUTDOWN 1 |
| 76 | #endif |
| 77 | |
| 78 | torture_param(bool, shutdown, REFPERF_SHUTDOWN, |
| 79 | "Shutdown at end of performance tests."); |
| 80 | |
| 81 | struct reader_task { |
| 82 | struct task_struct *task; |
Paul E. McKenney | af2789d | 2020-05-26 11:22:03 -0700 | [diff] [blame] | 83 | int start_reader; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 84 | wait_queue_head_t wq; |
| 85 | u64 last_duration_ns; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | static struct task_struct *shutdown_task; |
| 89 | static wait_queue_head_t shutdown_wq; |
| 90 | |
| 91 | static struct task_struct *main_task; |
| 92 | static wait_queue_head_t main_wq; |
| 93 | static int shutdown_start; |
| 94 | |
| 95 | static struct reader_task *reader_tasks; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 96 | |
| 97 | // Number of readers that are part of the current experiment. |
| 98 | static atomic_t nreaders_exp; |
| 99 | |
| 100 | // Use to wait for all threads to start. |
| 101 | static atomic_t n_init; |
Paul E. McKenney | 86e0da2 | 2020-05-26 11:40:52 -0700 | [diff] [blame^] | 102 | static atomic_t n_started; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 103 | |
| 104 | // Track which experiment is currently running. |
| 105 | static int exp_idx; |
| 106 | |
| 107 | // Operations vector for selecting different types of tests. |
| 108 | struct ref_perf_ops { |
| 109 | void (*init)(void); |
| 110 | void (*cleanup)(void); |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 111 | void (*readsection)(const int nloops); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 112 | const char *name; |
| 113 | }; |
| 114 | |
| 115 | static struct ref_perf_ops *cur_ops; |
| 116 | |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 117 | static void ref_rcu_read_section(const int nloops) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 118 | { |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 119 | int i; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 120 | |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 121 | for (i = nloops; i >= 0; i--) { |
| 122 | rcu_read_lock(); |
| 123 | rcu_read_unlock(); |
| 124 | } |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static void rcu_sync_perf_init(void) |
| 128 | { |
| 129 | } |
| 130 | |
| 131 | static struct ref_perf_ops rcu_ops = { |
| 132 | .init = rcu_sync_perf_init, |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 133 | .readsection = ref_rcu_read_section, |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 134 | .name = "rcu" |
| 135 | }; |
| 136 | |
| 137 | |
| 138 | // Definitions for SRCU ref perf testing. |
| 139 | DEFINE_STATIC_SRCU(srcu_refctl_perf); |
| 140 | static struct srcu_struct *srcu_ctlp = &srcu_refctl_perf; |
| 141 | |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 142 | static void srcu_ref_perf_read_section(int nloops) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 143 | { |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 144 | int i; |
| 145 | int idx; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 146 | |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 147 | for (i = nloops; i >= 0; i--) { |
| 148 | idx = srcu_read_lock(srcu_ctlp); |
| 149 | srcu_read_unlock(srcu_ctlp, idx); |
| 150 | } |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static struct ref_perf_ops srcu_ops = { |
| 154 | .init = rcu_sync_perf_init, |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 155 | .readsection = srcu_ref_perf_read_section, |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 156 | .name = "srcu" |
| 157 | }; |
| 158 | |
| 159 | // Definitions for reference count |
| 160 | static atomic_t refcnt; |
| 161 | |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 162 | static void ref_perf_refcnt_section(const int nloops) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 163 | { |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 164 | int i; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 165 | |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 166 | for (i = nloops; i >= 0; i--) { |
| 167 | atomic_inc(&refcnt); |
| 168 | atomic_dec(&refcnt); |
| 169 | } |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static struct ref_perf_ops refcnt_ops = { |
| 173 | .init = rcu_sync_perf_init, |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 174 | .readsection = ref_perf_refcnt_section, |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 175 | .name = "refcnt" |
| 176 | }; |
| 177 | |
| 178 | // Definitions for rwlock |
| 179 | static rwlock_t test_rwlock; |
| 180 | |
| 181 | static void ref_perf_rwlock_init(void) |
| 182 | { |
| 183 | rwlock_init(&test_rwlock); |
| 184 | } |
| 185 | |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 186 | static void ref_perf_rwlock_section(const int nloops) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 187 | { |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 188 | int i; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 189 | |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 190 | for (i = nloops; i >= 0; i--) { |
| 191 | read_lock(&test_rwlock); |
| 192 | read_unlock(&test_rwlock); |
| 193 | } |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static struct ref_perf_ops rwlock_ops = { |
| 197 | .init = ref_perf_rwlock_init, |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 198 | .readsection = ref_perf_rwlock_section, |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 199 | .name = "rwlock" |
| 200 | }; |
| 201 | |
| 202 | // Definitions for rwsem |
| 203 | static struct rw_semaphore test_rwsem; |
| 204 | |
| 205 | static void ref_perf_rwsem_init(void) |
| 206 | { |
| 207 | init_rwsem(&test_rwsem); |
| 208 | } |
| 209 | |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 210 | static void ref_perf_rwsem_section(const int nloops) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 211 | { |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 212 | int i; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 213 | |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 214 | for (i = nloops; i >= 0; i--) { |
| 215 | down_read(&test_rwsem); |
| 216 | up_read(&test_rwsem); |
| 217 | } |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | static struct ref_perf_ops rwsem_ops = { |
| 221 | .init = ref_perf_rwsem_init, |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 222 | .readsection = ref_perf_rwsem_section, |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 223 | .name = "rwsem" |
| 224 | }; |
| 225 | |
| 226 | // Reader kthread. Repeatedly does empty RCU read-side |
| 227 | // critical section, minimizing update-side interference. |
| 228 | static int |
| 229 | ref_perf_reader(void *arg) |
| 230 | { |
| 231 | unsigned long flags; |
| 232 | long me = (long)arg; |
| 233 | struct reader_task *rt = &(reader_tasks[me]); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 234 | u64 start; |
| 235 | s64 duration; |
| 236 | |
| 237 | VERBOSE_PERFOUT("ref_perf_reader %ld: task started", me); |
| 238 | set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids)); |
| 239 | set_user_nice(current, MAX_NICE); |
| 240 | atomic_inc(&n_init); |
Paul E. McKenney | 777a54c | 2020-05-25 14:16:44 -0700 | [diff] [blame] | 241 | if (holdoff) |
| 242 | schedule_timeout_interruptible(holdoff * HZ); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 243 | repeat: |
| 244 | VERBOSE_PERFOUT("ref_perf_reader %ld: waiting to start next experiment on cpu %d", me, smp_processor_id()); |
| 245 | |
| 246 | // Wait for signal that this reader can start. |
Paul E. McKenney | af2789d | 2020-05-26 11:22:03 -0700 | [diff] [blame] | 247 | wait_event(rt->wq, (atomic_read(&nreaders_exp) && smp_load_acquire(&rt->start_reader)) || |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 248 | torture_must_stop()); |
| 249 | |
| 250 | if (torture_must_stop()) |
| 251 | goto end; |
| 252 | |
| 253 | // Make sure that the CPU is affinitized appropriately during testing. |
| 254 | WARN_ON_ONCE(smp_processor_id() != me); |
| 255 | |
Paul E. McKenney | af2789d | 2020-05-26 11:22:03 -0700 | [diff] [blame] | 256 | WRITE_ONCE(rt->start_reader, 0); |
Paul E. McKenney | 86e0da2 | 2020-05-26 11:40:52 -0700 | [diff] [blame^] | 257 | if (!atomic_dec_return(&n_started)) |
| 258 | while (atomic_read_acquire(&n_started)) |
| 259 | cpu_relax(); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 260 | |
Paul E. McKenney | b864f89 | 2020-05-26 10:57:34 -0700 | [diff] [blame] | 261 | VERBOSE_PERFOUT("ref_perf_reader %ld: experiment %d started", me, exp_idx); |
| 262 | |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 263 | // To prevent noise, keep interrupts disabled. This also has the |
| 264 | // effect of preventing entries into slow path for rcu_read_unlock(). |
| 265 | local_irq_save(flags); |
| 266 | start = ktime_get_mono_fast_ns(); |
| 267 | |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 268 | cur_ops->readsection(loops); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 269 | |
| 270 | duration = ktime_get_mono_fast_ns() - start; |
| 271 | local_irq_restore(flags); |
| 272 | |
| 273 | rt->last_duration_ns = WARN_ON_ONCE(duration < 0) ? 0 : duration; |
| 274 | |
Paul E. McKenney | b864f89 | 2020-05-26 10:57:34 -0700 | [diff] [blame] | 275 | if (atomic_dec_and_test(&nreaders_exp)) |
| 276 | wake_up(&main_wq); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 277 | |
| 278 | VERBOSE_PERFOUT("ref_perf_reader %ld: experiment %d ended, (readers remaining=%d)", |
| 279 | me, exp_idx, atomic_read(&nreaders_exp)); |
| 280 | |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 281 | if (!torture_must_stop()) |
| 282 | goto repeat; |
| 283 | end: |
| 284 | torture_kthread_stopping("ref_perf_reader"); |
| 285 | return 0; |
| 286 | } |
| 287 | |
Paul E. McKenney | 2990750 | 2020-05-26 09:32:57 -0700 | [diff] [blame] | 288 | static void reset_readers(void) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 289 | { |
| 290 | int i; |
| 291 | struct reader_task *rt; |
| 292 | |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 293 | for (i = 0; i < nreaders; i++) { |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 294 | rt = &(reader_tasks[i]); |
| 295 | |
| 296 | rt->last_duration_ns = 0; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // Print the results of each reader and return the sum of all their durations. |
Paul E. McKenney | 2990750 | 2020-05-26 09:32:57 -0700 | [diff] [blame] | 301 | static u64 process_durations(int n) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 302 | { |
| 303 | int i; |
| 304 | struct reader_task *rt; |
| 305 | char buf1[64]; |
Paul E. McKenney | 2e90de7 | 2020-05-25 17:45:03 -0700 | [diff] [blame] | 306 | char *buf; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 307 | u64 sum = 0; |
| 308 | |
Paul E. McKenney | 2e90de7 | 2020-05-25 17:45:03 -0700 | [diff] [blame] | 309 | buf = kmalloc(128 + nreaders * 32, GFP_KERNEL); |
| 310 | if (!buf) |
| 311 | return 0; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 312 | buf[0] = 0; |
| 313 | sprintf(buf, "Experiment #%d (Format: <THREAD-NUM>:<Total loop time in ns>)", |
| 314 | exp_idx); |
| 315 | |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 316 | for (i = 0; i < n && !torture_must_stop(); i++) { |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 317 | rt = &(reader_tasks[i]); |
| 318 | sprintf(buf1, "%d: %llu\t", i, rt->last_duration_ns); |
| 319 | |
| 320 | if (i % 5 == 0) |
| 321 | strcat(buf, "\n"); |
| 322 | strcat(buf, buf1); |
| 323 | |
| 324 | sum += rt->last_duration_ns; |
| 325 | } |
| 326 | strcat(buf, "\n"); |
| 327 | |
| 328 | PERFOUT("%s\n", buf); |
| 329 | |
Paul E. McKenney | 2e90de7 | 2020-05-25 17:45:03 -0700 | [diff] [blame] | 330 | kfree(buf); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 331 | return sum; |
| 332 | } |
| 333 | |
| 334 | // The main_func is the main orchestrator, it performs a bunch of |
| 335 | // experiments. For every experiment, it orders all the readers |
| 336 | // involved to start and waits for them to finish the experiment. It |
| 337 | // then reads their timestamps and starts the next experiment. Each |
| 338 | // experiment progresses from 1 concurrent reader to N of them at which |
| 339 | // point all the timestamps are printed. |
| 340 | static int main_func(void *arg) |
| 341 | { |
Paul E. McKenney | f518f15 | 2020-05-25 17:32:56 -0700 | [diff] [blame] | 342 | bool errexit = false; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 343 | int exp, r; |
| 344 | char buf1[64]; |
Paul E. McKenney | f518f15 | 2020-05-25 17:32:56 -0700 | [diff] [blame] | 345 | char *buf; |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 346 | u64 *result_avg; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 347 | |
| 348 | set_cpus_allowed_ptr(current, cpumask_of(nreaders % nr_cpu_ids)); |
| 349 | set_user_nice(current, MAX_NICE); |
| 350 | |
| 351 | VERBOSE_PERFOUT("main_func task started"); |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 352 | result_avg = kzalloc(nruns * sizeof(*result_avg), GFP_KERNEL); |
Paul E. McKenney | f518f15 | 2020-05-25 17:32:56 -0700 | [diff] [blame] | 353 | buf = kzalloc(64 + nruns * 32, GFP_KERNEL); |
| 354 | if (!result_avg || !buf) { |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 355 | VERBOSE_PERFOUT_ERRSTRING("out of memory"); |
Paul E. McKenney | f518f15 | 2020-05-25 17:32:56 -0700 | [diff] [blame] | 356 | errexit = true; |
| 357 | } |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 358 | atomic_inc(&n_init); |
| 359 | |
| 360 | // Wait for all threads to start. |
| 361 | wait_event(main_wq, atomic_read(&n_init) == (nreaders + 1)); |
Paul E. McKenney | 777a54c | 2020-05-25 14:16:44 -0700 | [diff] [blame] | 362 | if (holdoff) |
| 363 | schedule_timeout_interruptible(holdoff * HZ); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 364 | |
| 365 | // Start exp readers up per experiment |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 366 | for (exp = 0; exp < nruns && !torture_must_stop(); exp++) { |
Paul E. McKenney | f518f15 | 2020-05-25 17:32:56 -0700 | [diff] [blame] | 367 | if (errexit) |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 368 | break; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 369 | if (torture_must_stop()) |
| 370 | goto end; |
| 371 | |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 372 | reset_readers(); |
| 373 | atomic_set(&nreaders_exp, nreaders); |
Paul E. McKenney | 86e0da2 | 2020-05-26 11:40:52 -0700 | [diff] [blame^] | 374 | atomic_set(&n_started, nreaders); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 375 | |
| 376 | exp_idx = exp; |
| 377 | |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 378 | for (r = 0; r < nreaders; r++) { |
Paul E. McKenney | af2789d | 2020-05-26 11:22:03 -0700 | [diff] [blame] | 379 | smp_store_release(&reader_tasks[r].start_reader, 1); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 380 | wake_up(&reader_tasks[r].wq); |
| 381 | } |
| 382 | |
| 383 | VERBOSE_PERFOUT("main_func: experiment started, waiting for %d readers", |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 384 | nreaders); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 385 | |
| 386 | wait_event(main_wq, |
| 387 | !atomic_read(&nreaders_exp) || torture_must_stop()); |
| 388 | |
| 389 | VERBOSE_PERFOUT("main_func: experiment ended"); |
| 390 | |
| 391 | if (torture_must_stop()) |
| 392 | goto end; |
| 393 | |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 394 | result_avg[exp] = 1000 * process_durations(nreaders) / (nreaders * loops); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | // Print the average of all experiments |
| 398 | PERFOUT("END OF TEST. Calculating average duration per loop (nanoseconds)...\n"); |
| 399 | |
| 400 | buf[0] = 0; |
| 401 | strcat(buf, "\n"); |
| 402 | strcat(buf, "Threads\tTime(ns)\n"); |
| 403 | |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 404 | for (exp = 0; exp < nruns; exp++) { |
Paul E. McKenney | f518f15 | 2020-05-25 17:32:56 -0700 | [diff] [blame] | 405 | if (errexit) |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 406 | break; |
| 407 | sprintf(buf1, "%d\t%llu.%03d\n", exp + 1, result_avg[exp] / 1000, (int)(result_avg[exp] % 1000)); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 408 | strcat(buf, buf1); |
| 409 | } |
| 410 | |
Paul E. McKenney | f518f15 | 2020-05-25 17:32:56 -0700 | [diff] [blame] | 411 | if (!errexit) |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 412 | PERFOUT("%s", buf); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 413 | |
| 414 | // This will shutdown everything including us. |
| 415 | if (shutdown) { |
| 416 | shutdown_start = 1; |
| 417 | wake_up(&shutdown_wq); |
| 418 | } |
| 419 | |
| 420 | // Wait for torture to stop us |
| 421 | while (!torture_must_stop()) |
| 422 | schedule_timeout_uninterruptible(1); |
| 423 | |
| 424 | end: |
| 425 | torture_kthread_stopping("main_func"); |
Paul E. McKenney | f518f15 | 2020-05-25 17:32:56 -0700 | [diff] [blame] | 426 | kfree(result_avg); |
| 427 | kfree(buf); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | static void |
| 432 | ref_perf_print_module_parms(struct ref_perf_ops *cur_ops, const char *tag) |
| 433 | { |
| 434 | pr_alert("%s" PERF_FLAG |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 435 | "--- %s: verbose=%d shutdown=%d holdoff=%d loops=%ld nreaders=%d nruns=%d\n", perf_type, tag, |
| 436 | verbose, shutdown, holdoff, loops, nreaders, nruns); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | static void |
| 440 | ref_perf_cleanup(void) |
| 441 | { |
| 442 | int i; |
| 443 | |
| 444 | if (torture_cleanup_begin()) |
| 445 | return; |
| 446 | |
| 447 | if (!cur_ops) { |
| 448 | torture_cleanup_end(); |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | if (reader_tasks) { |
| 453 | for (i = 0; i < nreaders; i++) |
| 454 | torture_stop_kthread("ref_perf_reader", |
| 455 | reader_tasks[i].task); |
| 456 | } |
| 457 | kfree(reader_tasks); |
| 458 | |
| 459 | torture_stop_kthread("main_task", main_task); |
| 460 | kfree(main_task); |
| 461 | |
| 462 | // Do perf-type-specific cleanup operations. |
| 463 | if (cur_ops->cleanup != NULL) |
| 464 | cur_ops->cleanup(); |
| 465 | |
| 466 | torture_cleanup_end(); |
| 467 | } |
| 468 | |
| 469 | // Shutdown kthread. Just waits to be awakened, then shuts down system. |
| 470 | static int |
| 471 | ref_perf_shutdown(void *arg) |
| 472 | { |
| 473 | wait_event(shutdown_wq, shutdown_start); |
| 474 | |
| 475 | smp_mb(); // Wake before output. |
| 476 | ref_perf_cleanup(); |
| 477 | kernel_power_off(); |
| 478 | |
| 479 | return -EINVAL; |
| 480 | } |
| 481 | |
| 482 | static int __init |
| 483 | ref_perf_init(void) |
| 484 | { |
| 485 | long i; |
| 486 | int firsterr = 0; |
| 487 | static struct ref_perf_ops *perf_ops[] = { |
| 488 | &rcu_ops, &srcu_ops, &refcnt_ops, &rwlock_ops, &rwsem_ops, |
| 489 | }; |
| 490 | |
| 491 | if (!torture_init_begin(perf_type, verbose)) |
| 492 | return -EBUSY; |
| 493 | |
| 494 | for (i = 0; i < ARRAY_SIZE(perf_ops); i++) { |
| 495 | cur_ops = perf_ops[i]; |
| 496 | if (strcmp(perf_type, cur_ops->name) == 0) |
| 497 | break; |
| 498 | } |
| 499 | if (i == ARRAY_SIZE(perf_ops)) { |
| 500 | pr_alert("rcu-perf: invalid perf type: \"%s\"\n", perf_type); |
| 501 | pr_alert("rcu-perf types:"); |
| 502 | for (i = 0; i < ARRAY_SIZE(perf_ops); i++) |
| 503 | pr_cont(" %s", perf_ops[i]->name); |
| 504 | pr_cont("\n"); |
| 505 | WARN_ON(!IS_MODULE(CONFIG_RCU_REF_PERF_TEST)); |
| 506 | firsterr = -EINVAL; |
| 507 | cur_ops = NULL; |
| 508 | goto unwind; |
| 509 | } |
| 510 | if (cur_ops->init) |
| 511 | cur_ops->init(); |
| 512 | |
| 513 | ref_perf_print_module_parms(cur_ops, "Start of test"); |
| 514 | |
| 515 | // Shutdown task |
| 516 | if (shutdown) { |
| 517 | init_waitqueue_head(&shutdown_wq); |
| 518 | firsterr = torture_create_kthread(ref_perf_shutdown, NULL, |
| 519 | shutdown_task); |
| 520 | if (firsterr) |
| 521 | goto unwind; |
| 522 | schedule_timeout_uninterruptible(1); |
| 523 | } |
| 524 | |
Paul E. McKenney | 8fc2878 | 2020-05-25 15:48:38 -0700 | [diff] [blame] | 525 | // Reader tasks (default to ~75% of online CPUs). |
| 526 | if (nreaders < 0) |
| 527 | nreaders = (num_online_cpus() >> 1) + (num_online_cpus() >> 2); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 528 | reader_tasks = kcalloc(nreaders, sizeof(reader_tasks[0]), |
| 529 | GFP_KERNEL); |
| 530 | if (!reader_tasks) { |
| 531 | VERBOSE_PERFOUT_ERRSTRING("out of memory"); |
| 532 | firsterr = -ENOMEM; |
| 533 | goto unwind; |
| 534 | } |
| 535 | |
| 536 | VERBOSE_PERFOUT("Starting %d reader threads\n", nreaders); |
| 537 | |
| 538 | for (i = 0; i < nreaders; i++) { |
| 539 | firsterr = torture_create_kthread(ref_perf_reader, (void *)i, |
| 540 | reader_tasks[i].task); |
| 541 | if (firsterr) |
| 542 | goto unwind; |
| 543 | |
| 544 | init_waitqueue_head(&(reader_tasks[i].wq)); |
| 545 | } |
| 546 | |
| 547 | // Main Task |
| 548 | init_waitqueue_head(&main_wq); |
| 549 | firsterr = torture_create_kthread(main_func, NULL, main_task); |
| 550 | if (firsterr) |
| 551 | goto unwind; |
| 552 | schedule_timeout_uninterruptible(1); |
| 553 | |
| 554 | |
| 555 | // Wait until all threads start |
| 556 | while (atomic_read(&n_init) < nreaders + 1) |
| 557 | schedule_timeout_uninterruptible(1); |
| 558 | |
| 559 | wake_up(&main_wq); |
| 560 | |
| 561 | torture_init_end(); |
| 562 | return 0; |
| 563 | |
| 564 | unwind: |
| 565 | torture_init_end(); |
| 566 | ref_perf_cleanup(); |
| 567 | return firsterr; |
| 568 | } |
| 569 | |
| 570 | module_init(ref_perf_init); |
| 571 | module_exit(ref_perf_cleanup); |