Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // |
Paul E. McKenney | 8e4ec3d | 2020-06-17 11:33:54 -0700 | [diff] [blame] | 3 | // Scalability test comparing RCU vs other mechanisms |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 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> |
Paul E. McKenney | 72bb749 | 2020-06-02 08:34:41 -0700 | [diff] [blame] | 28 | #include <linux/rcupdate_trace.h> |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 29 | #include <linux/reboot.h> |
| 30 | #include <linux/sched.h> |
| 31 | #include <linux/spinlock.h> |
| 32 | #include <linux/smp.h> |
| 33 | #include <linux/stat.h> |
| 34 | #include <linux/srcu.h> |
| 35 | #include <linux/slab.h> |
| 36 | #include <linux/torture.h> |
| 37 | #include <linux/types.h> |
| 38 | |
| 39 | #include "rcu.h" |
| 40 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 41 | #define SCALE_FLAG "-ref-scale: " |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 42 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 43 | #define SCALEOUT(s, x...) \ |
| 44 | pr_alert("%s" SCALE_FLAG s, scale_type, ## x) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 45 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 46 | #define VERBOSE_SCALEOUT(s, x...) \ |
| 47 | do { if (verbose) pr_alert("%s" SCALE_FLAG s, scale_type, ## x); } while (0) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 48 | |
Paul E. McKenney | e76506f | 2020-11-15 10:24:52 -0800 | [diff] [blame] | 49 | static atomic_t verbose_batch_ctr; |
| 50 | |
| 51 | #define VERBOSE_SCALEOUT_BATCH(s, x...) \ |
| 52 | do { \ |
| 53 | if (verbose && \ |
| 54 | (verbose_batched <= 0 || \ |
Paul E. McKenney | 414c116 | 2020-11-25 10:50:35 -0800 | [diff] [blame^] | 55 | !(atomic_inc_return(&verbose_batch_ctr) % verbose_batched))) { \ |
| 56 | schedule_timeout_uninterruptible(1); \ |
Paul E. McKenney | e76506f | 2020-11-15 10:24:52 -0800 | [diff] [blame] | 57 | pr_alert("%s" SCALE_FLAG s, scale_type, ## x); \ |
Paul E. McKenney | 414c116 | 2020-11-25 10:50:35 -0800 | [diff] [blame^] | 58 | } \ |
Paul E. McKenney | e76506f | 2020-11-15 10:24:52 -0800 | [diff] [blame] | 59 | } while (0) |
| 60 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 61 | #define VERBOSE_SCALEOUT_ERRSTRING(s, x...) \ |
| 62 | do { if (verbose) pr_alert("%s" SCALE_FLAG "!!! " s, scale_type, ## x); } while (0) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 63 | |
| 64 | MODULE_LICENSE("GPL"); |
| 65 | MODULE_AUTHOR("Joel Fernandes (Google) <joel@joelfernandes.org>"); |
| 66 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 67 | static char *scale_type = "rcu"; |
| 68 | module_param(scale_type, charp, 0444); |
| 69 | MODULE_PARM_DESC(scale_type, "Type of test (rcu, srcu, refcnt, rwsem, rwlock."); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 70 | |
| 71 | torture_param(int, verbose, 0, "Enable verbose debugging printk()s"); |
Paul E. McKenney | e76506f | 2020-11-15 10:24:52 -0800 | [diff] [blame] | 72 | torture_param(int, verbose_batched, 0, "Batch verbose debugging printk()s"); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 73 | |
Paul E. McKenney | 777a54c | 2020-05-25 14:16:44 -0700 | [diff] [blame] | 74 | // Wait until there are multiple CPUs before starting test. |
Paul E. McKenney | 8e4ec3d | 2020-06-17 11:33:54 -0700 | [diff] [blame] | 75 | torture_param(int, holdoff, IS_BUILTIN(CONFIG_RCU_REF_SCALE_TEST) ? 10 : 0, |
Paul E. McKenney | 777a54c | 2020-05-25 14:16:44 -0700 | [diff] [blame] | 76 | "Holdoff time before test start (s)"); |
| 77 | // Number of loops per experiment, all readers execute operations concurrently. |
Paul E. McKenney | 4dd72a3 | 2020-05-29 13:11:26 -0700 | [diff] [blame] | 78 | torture_param(long, loops, 10000, "Number of loops per experiment."); |
Paul E. McKenney | 8fc2878 | 2020-05-25 15:48:38 -0700 | [diff] [blame] | 79 | // Number of readers, with -1 defaulting to about 75% of the CPUs. |
| 80 | torture_param(int, nreaders, -1, "Number of readers, -1 for 75% of CPUs."); |
| 81 | // Number of runs. |
| 82 | torture_param(int, nruns, 30, "Number of experiments to run."); |
Paul E. McKenney | 918b351 | 2020-05-31 18:14:57 -0700 | [diff] [blame] | 83 | // Reader delay in nanoseconds, 0 for no delay. |
| 84 | torture_param(int, readdelay, 0, "Read-side delay in nanoseconds."); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 85 | |
| 86 | #ifdef MODULE |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 87 | # define REFSCALE_SHUTDOWN 0 |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 88 | #else |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 89 | # define REFSCALE_SHUTDOWN 1 |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 90 | #endif |
| 91 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 92 | torture_param(bool, shutdown, REFSCALE_SHUTDOWN, |
| 93 | "Shutdown at end of scalability tests."); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 94 | |
| 95 | struct reader_task { |
| 96 | struct task_struct *task; |
Paul E. McKenney | af2789d | 2020-05-26 11:22:03 -0700 | [diff] [blame] | 97 | int start_reader; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 98 | wait_queue_head_t wq; |
| 99 | u64 last_duration_ns; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | static struct task_struct *shutdown_task; |
| 103 | static wait_queue_head_t shutdown_wq; |
| 104 | |
| 105 | static struct task_struct *main_task; |
| 106 | static wait_queue_head_t main_wq; |
| 107 | static int shutdown_start; |
| 108 | |
| 109 | static struct reader_task *reader_tasks; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 110 | |
| 111 | // Number of readers that are part of the current experiment. |
| 112 | static atomic_t nreaders_exp; |
| 113 | |
| 114 | // Use to wait for all threads to start. |
| 115 | static atomic_t n_init; |
Paul E. McKenney | 86e0da2 | 2020-05-26 11:40:52 -0700 | [diff] [blame] | 116 | static atomic_t n_started; |
Paul E. McKenney | 2db0bda | 2020-05-26 12:34:57 -0700 | [diff] [blame] | 117 | static atomic_t n_warmedup; |
| 118 | static atomic_t n_cooleddown; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 119 | |
| 120 | // Track which experiment is currently running. |
| 121 | static int exp_idx; |
| 122 | |
| 123 | // Operations vector for selecting different types of tests. |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 124 | struct ref_scale_ops { |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 125 | void (*init)(void); |
| 126 | void (*cleanup)(void); |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 127 | void (*readsection)(const int nloops); |
Paul E. McKenney | 918b351 | 2020-05-31 18:14:57 -0700 | [diff] [blame] | 128 | void (*delaysection)(const int nloops, const int udl, const int ndl); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 129 | const char *name; |
| 130 | }; |
| 131 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 132 | static struct ref_scale_ops *cur_ops; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 133 | |
Paul E. McKenney | 918b351 | 2020-05-31 18:14:57 -0700 | [diff] [blame] | 134 | static void un_delay(const int udl, const int ndl) |
| 135 | { |
| 136 | if (udl) |
| 137 | udelay(udl); |
| 138 | if (ndl) |
| 139 | ndelay(ndl); |
| 140 | } |
| 141 | |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 142 | static void ref_rcu_read_section(const 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; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 145 | |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 146 | for (i = nloops; i >= 0; i--) { |
| 147 | rcu_read_lock(); |
| 148 | rcu_read_unlock(); |
| 149 | } |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 150 | } |
| 151 | |
Paul E. McKenney | 918b351 | 2020-05-31 18:14:57 -0700 | [diff] [blame] | 152 | static void ref_rcu_delay_section(const int nloops, const int udl, const int ndl) |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 153 | { |
| 154 | int i; |
| 155 | |
| 156 | for (i = nloops; i >= 0; i--) { |
| 157 | rcu_read_lock(); |
Paul E. McKenney | 918b351 | 2020-05-31 18:14:57 -0700 | [diff] [blame] | 158 | un_delay(udl, ndl); |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 159 | rcu_read_unlock(); |
| 160 | } |
| 161 | } |
| 162 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 163 | static void rcu_sync_scale_init(void) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 164 | { |
| 165 | } |
| 166 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 167 | static struct ref_scale_ops rcu_ops = { |
| 168 | .init = rcu_sync_scale_init, |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 169 | .readsection = ref_rcu_read_section, |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 170 | .delaysection = ref_rcu_delay_section, |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 171 | .name = "rcu" |
| 172 | }; |
| 173 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 174 | // Definitions for SRCU ref scale testing. |
| 175 | DEFINE_STATIC_SRCU(srcu_refctl_scale); |
| 176 | static struct srcu_struct *srcu_ctlp = &srcu_refctl_scale; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 177 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 178 | static void srcu_ref_scale_read_section(const int nloops) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 179 | { |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 180 | int i; |
| 181 | int idx; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 182 | |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 183 | for (i = nloops; i >= 0; i--) { |
| 184 | idx = srcu_read_lock(srcu_ctlp); |
| 185 | srcu_read_unlock(srcu_ctlp, idx); |
| 186 | } |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 187 | } |
| 188 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 189 | static void srcu_ref_scale_delay_section(const int nloops, const int udl, const int ndl) |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 190 | { |
| 191 | int i; |
| 192 | int idx; |
| 193 | |
| 194 | for (i = nloops; i >= 0; i--) { |
| 195 | idx = srcu_read_lock(srcu_ctlp); |
Paul E. McKenney | 918b351 | 2020-05-31 18:14:57 -0700 | [diff] [blame] | 196 | un_delay(udl, ndl); |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 197 | srcu_read_unlock(srcu_ctlp, idx); |
| 198 | } |
| 199 | } |
| 200 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 201 | static struct ref_scale_ops srcu_ops = { |
| 202 | .init = rcu_sync_scale_init, |
| 203 | .readsection = srcu_ref_scale_read_section, |
| 204 | .delaysection = srcu_ref_scale_delay_section, |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 205 | .name = "srcu" |
| 206 | }; |
| 207 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 208 | // Definitions for RCU Tasks ref scale testing: Empty read markers. |
Paul E. McKenney | e13ef44 | 2020-06-03 11:56:34 -0700 | [diff] [blame] | 209 | // These definitions also work for RCU Rude readers. |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 210 | static void rcu_tasks_ref_scale_read_section(const int nloops) |
Paul E. McKenney | e13ef44 | 2020-06-03 11:56:34 -0700 | [diff] [blame] | 211 | { |
| 212 | int i; |
| 213 | |
| 214 | for (i = nloops; i >= 0; i--) |
| 215 | continue; |
| 216 | } |
| 217 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 218 | static void rcu_tasks_ref_scale_delay_section(const int nloops, const int udl, const int ndl) |
Paul E. McKenney | e13ef44 | 2020-06-03 11:56:34 -0700 | [diff] [blame] | 219 | { |
| 220 | int i; |
| 221 | |
| 222 | for (i = nloops; i >= 0; i--) |
| 223 | un_delay(udl, ndl); |
| 224 | } |
| 225 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 226 | static struct ref_scale_ops rcu_tasks_ops = { |
| 227 | .init = rcu_sync_scale_init, |
| 228 | .readsection = rcu_tasks_ref_scale_read_section, |
| 229 | .delaysection = rcu_tasks_ref_scale_delay_section, |
Paul E. McKenney | e13ef44 | 2020-06-03 11:56:34 -0700 | [diff] [blame] | 230 | .name = "rcu-tasks" |
| 231 | }; |
| 232 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 233 | // Definitions for RCU Tasks Trace ref scale testing. |
| 234 | static void rcu_trace_ref_scale_read_section(const int nloops) |
Paul E. McKenney | 72bb749 | 2020-06-02 08:34:41 -0700 | [diff] [blame] | 235 | { |
| 236 | int i; |
| 237 | |
| 238 | for (i = nloops; i >= 0; i--) { |
| 239 | rcu_read_lock_trace(); |
| 240 | rcu_read_unlock_trace(); |
| 241 | } |
| 242 | } |
| 243 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 244 | static void rcu_trace_ref_scale_delay_section(const int nloops, const int udl, const int ndl) |
Paul E. McKenney | 72bb749 | 2020-06-02 08:34:41 -0700 | [diff] [blame] | 245 | { |
| 246 | int i; |
| 247 | |
| 248 | for (i = nloops; i >= 0; i--) { |
| 249 | rcu_read_lock_trace(); |
| 250 | un_delay(udl, ndl); |
| 251 | rcu_read_unlock_trace(); |
| 252 | } |
| 253 | } |
| 254 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 255 | static struct ref_scale_ops rcu_trace_ops = { |
| 256 | .init = rcu_sync_scale_init, |
| 257 | .readsection = rcu_trace_ref_scale_read_section, |
| 258 | .delaysection = rcu_trace_ref_scale_delay_section, |
Paul E. McKenney | 72bb749 | 2020-06-02 08:34:41 -0700 | [diff] [blame] | 259 | .name = "rcu-trace" |
| 260 | }; |
| 261 | |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 262 | // Definitions for reference count |
| 263 | static atomic_t refcnt; |
| 264 | |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 265 | static void ref_refcnt_section(const int nloops) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 266 | { |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 267 | int i; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 268 | |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 269 | for (i = nloops; i >= 0; i--) { |
| 270 | atomic_inc(&refcnt); |
| 271 | atomic_dec(&refcnt); |
| 272 | } |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 273 | } |
| 274 | |
Paul E. McKenney | 918b351 | 2020-05-31 18:14:57 -0700 | [diff] [blame] | 275 | static void ref_refcnt_delay_section(const int nloops, const int udl, const int ndl) |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 276 | { |
| 277 | int i; |
| 278 | |
| 279 | for (i = nloops; i >= 0; i--) { |
| 280 | atomic_inc(&refcnt); |
Paul E. McKenney | 918b351 | 2020-05-31 18:14:57 -0700 | [diff] [blame] | 281 | un_delay(udl, ndl); |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 282 | atomic_dec(&refcnt); |
| 283 | } |
| 284 | } |
| 285 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 286 | static struct ref_scale_ops refcnt_ops = { |
| 287 | .init = rcu_sync_scale_init, |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 288 | .readsection = ref_refcnt_section, |
| 289 | .delaysection = ref_refcnt_delay_section, |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 290 | .name = "refcnt" |
| 291 | }; |
| 292 | |
| 293 | // Definitions for rwlock |
| 294 | static rwlock_t test_rwlock; |
| 295 | |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 296 | static void ref_rwlock_init(void) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 297 | { |
| 298 | rwlock_init(&test_rwlock); |
| 299 | } |
| 300 | |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 301 | static void ref_rwlock_section(const int nloops) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 302 | { |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 303 | int i; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 304 | |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 305 | for (i = nloops; i >= 0; i--) { |
| 306 | read_lock(&test_rwlock); |
| 307 | read_unlock(&test_rwlock); |
| 308 | } |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 309 | } |
| 310 | |
Paul E. McKenney | 918b351 | 2020-05-31 18:14:57 -0700 | [diff] [blame] | 311 | static void ref_rwlock_delay_section(const int nloops, const int udl, const int ndl) |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 312 | { |
| 313 | int i; |
| 314 | |
| 315 | for (i = nloops; i >= 0; i--) { |
| 316 | read_lock(&test_rwlock); |
Paul E. McKenney | 918b351 | 2020-05-31 18:14:57 -0700 | [diff] [blame] | 317 | un_delay(udl, ndl); |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 318 | read_unlock(&test_rwlock); |
| 319 | } |
| 320 | } |
| 321 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 322 | static struct ref_scale_ops rwlock_ops = { |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 323 | .init = ref_rwlock_init, |
| 324 | .readsection = ref_rwlock_section, |
| 325 | .delaysection = ref_rwlock_delay_section, |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 326 | .name = "rwlock" |
| 327 | }; |
| 328 | |
| 329 | // Definitions for rwsem |
| 330 | static struct rw_semaphore test_rwsem; |
| 331 | |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 332 | static void ref_rwsem_init(void) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 333 | { |
| 334 | init_rwsem(&test_rwsem); |
| 335 | } |
| 336 | |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 337 | static void ref_rwsem_section(const int nloops) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 338 | { |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 339 | int i; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 340 | |
Paul E. McKenney | 75dd8ef | 2020-05-25 14:59:06 -0700 | [diff] [blame] | 341 | for (i = nloops; i >= 0; i--) { |
| 342 | down_read(&test_rwsem); |
| 343 | up_read(&test_rwsem); |
| 344 | } |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 345 | } |
| 346 | |
Paul E. McKenney | 918b351 | 2020-05-31 18:14:57 -0700 | [diff] [blame] | 347 | static void ref_rwsem_delay_section(const int nloops, const int udl, const int ndl) |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 348 | { |
| 349 | int i; |
| 350 | |
| 351 | for (i = nloops; i >= 0; i--) { |
| 352 | down_read(&test_rwsem); |
Paul E. McKenney | 918b351 | 2020-05-31 18:14:57 -0700 | [diff] [blame] | 353 | un_delay(udl, ndl); |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 354 | up_read(&test_rwsem); |
| 355 | } |
| 356 | } |
| 357 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 358 | static struct ref_scale_ops rwsem_ops = { |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 359 | .init = ref_rwsem_init, |
| 360 | .readsection = ref_rwsem_section, |
| 361 | .delaysection = ref_rwsem_delay_section, |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 362 | .name = "rwsem" |
| 363 | }; |
| 364 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 365 | static void rcu_scale_one_reader(void) |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 366 | { |
| 367 | if (readdelay <= 0) |
| 368 | cur_ops->readsection(loops); |
| 369 | else |
Paul E. McKenney | 918b351 | 2020-05-31 18:14:57 -0700 | [diff] [blame] | 370 | cur_ops->delaysection(loops, readdelay / 1000, readdelay % 1000); |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 373 | // Reader kthread. Repeatedly does empty RCU read-side |
| 374 | // critical section, minimizing update-side interference. |
| 375 | static int |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 376 | ref_scale_reader(void *arg) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 377 | { |
| 378 | unsigned long flags; |
| 379 | long me = (long)arg; |
| 380 | struct reader_task *rt = &(reader_tasks[me]); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 381 | u64 start; |
| 382 | s64 duration; |
| 383 | |
Paul E. McKenney | e76506f | 2020-11-15 10:24:52 -0800 | [diff] [blame] | 384 | VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: task started", me); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 385 | set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids)); |
| 386 | set_user_nice(current, MAX_NICE); |
| 387 | atomic_inc(&n_init); |
Paul E. McKenney | 777a54c | 2020-05-25 14:16:44 -0700 | [diff] [blame] | 388 | if (holdoff) |
| 389 | schedule_timeout_interruptible(holdoff * HZ); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 390 | repeat: |
Paul E. McKenney | e76506f | 2020-11-15 10:24:52 -0800 | [diff] [blame] | 391 | VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: waiting to start next experiment on cpu %d", me, smp_processor_id()); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 392 | |
| 393 | // Wait for signal that this reader can start. |
Paul E. McKenney | af2789d | 2020-05-26 11:22:03 -0700 | [diff] [blame] | 394 | 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] | 395 | torture_must_stop()); |
| 396 | |
| 397 | if (torture_must_stop()) |
| 398 | goto end; |
| 399 | |
| 400 | // Make sure that the CPU is affinitized appropriately during testing. |
| 401 | WARN_ON_ONCE(smp_processor_id() != me); |
| 402 | |
Paul E. McKenney | af2789d | 2020-05-26 11:22:03 -0700 | [diff] [blame] | 403 | WRITE_ONCE(rt->start_reader, 0); |
Paul E. McKenney | 86e0da2 | 2020-05-26 11:40:52 -0700 | [diff] [blame] | 404 | if (!atomic_dec_return(&n_started)) |
| 405 | while (atomic_read_acquire(&n_started)) |
| 406 | cpu_relax(); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 407 | |
Paul E. McKenney | e76506f | 2020-11-15 10:24:52 -0800 | [diff] [blame] | 408 | VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: experiment %d started", me, exp_idx); |
Paul E. McKenney | b864f89 | 2020-05-26 10:57:34 -0700 | [diff] [blame] | 409 | |
Paul E. McKenney | 2db0bda | 2020-05-26 12:34:57 -0700 | [diff] [blame] | 410 | |
| 411 | // To reduce noise, do an initial cache-warming invocation, check |
| 412 | // in, and then keep warming until everyone has checked in. |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 413 | rcu_scale_one_reader(); |
Paul E. McKenney | 2db0bda | 2020-05-26 12:34:57 -0700 | [diff] [blame] | 414 | if (!atomic_dec_return(&n_warmedup)) |
| 415 | while (atomic_read_acquire(&n_warmedup)) |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 416 | rcu_scale_one_reader(); |
Paul E. McKenney | 2db0bda | 2020-05-26 12:34:57 -0700 | [diff] [blame] | 417 | // Also keep interrupts disabled. This also has the effect |
| 418 | // of preventing entries into slow path for rcu_read_unlock(). |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 419 | local_irq_save(flags); |
| 420 | start = ktime_get_mono_fast_ns(); |
| 421 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 422 | rcu_scale_one_reader(); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 423 | |
| 424 | duration = ktime_get_mono_fast_ns() - start; |
| 425 | local_irq_restore(flags); |
| 426 | |
| 427 | rt->last_duration_ns = WARN_ON_ONCE(duration < 0) ? 0 : duration; |
Paul E. McKenney | 2db0bda | 2020-05-26 12:34:57 -0700 | [diff] [blame] | 428 | // To reduce runtime-skew noise, do maintain-load invocations until |
| 429 | // everyone is done. |
| 430 | if (!atomic_dec_return(&n_cooleddown)) |
| 431 | while (atomic_read_acquire(&n_cooleddown)) |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 432 | rcu_scale_one_reader(); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 433 | |
Paul E. McKenney | b864f89 | 2020-05-26 10:57:34 -0700 | [diff] [blame] | 434 | if (atomic_dec_and_test(&nreaders_exp)) |
| 435 | wake_up(&main_wq); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 436 | |
Paul E. McKenney | e76506f | 2020-11-15 10:24:52 -0800 | [diff] [blame] | 437 | VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: experiment %d ended, (readers remaining=%d)", |
| 438 | me, exp_idx, atomic_read(&nreaders_exp)); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 439 | |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 440 | if (!torture_must_stop()) |
| 441 | goto repeat; |
| 442 | end: |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 443 | torture_kthread_stopping("ref_scale_reader"); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 444 | return 0; |
| 445 | } |
| 446 | |
Paul E. McKenney | 2990750 | 2020-05-26 09:32:57 -0700 | [diff] [blame] | 447 | static void reset_readers(void) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 448 | { |
| 449 | int i; |
| 450 | struct reader_task *rt; |
| 451 | |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 452 | for (i = 0; i < nreaders; i++) { |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 453 | rt = &(reader_tasks[i]); |
| 454 | |
| 455 | rt->last_duration_ns = 0; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | // 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] | 460 | static u64 process_durations(int n) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 461 | { |
| 462 | int i; |
| 463 | struct reader_task *rt; |
| 464 | char buf1[64]; |
Paul E. McKenney | 2e90de7 | 2020-05-25 17:45:03 -0700 | [diff] [blame] | 465 | char *buf; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 466 | u64 sum = 0; |
| 467 | |
Paul E. McKenney | 2e90de7 | 2020-05-25 17:45:03 -0700 | [diff] [blame] | 468 | buf = kmalloc(128 + nreaders * 32, GFP_KERNEL); |
| 469 | if (!buf) |
| 470 | return 0; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 471 | buf[0] = 0; |
| 472 | sprintf(buf, "Experiment #%d (Format: <THREAD-NUM>:<Total loop time in ns>)", |
| 473 | exp_idx); |
| 474 | |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 475 | for (i = 0; i < n && !torture_must_stop(); i++) { |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 476 | rt = &(reader_tasks[i]); |
| 477 | sprintf(buf1, "%d: %llu\t", i, rt->last_duration_ns); |
| 478 | |
| 479 | if (i % 5 == 0) |
| 480 | strcat(buf, "\n"); |
| 481 | strcat(buf, buf1); |
| 482 | |
| 483 | sum += rt->last_duration_ns; |
| 484 | } |
| 485 | strcat(buf, "\n"); |
| 486 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 487 | SCALEOUT("%s\n", buf); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 488 | |
Paul E. McKenney | 2e90de7 | 2020-05-25 17:45:03 -0700 | [diff] [blame] | 489 | kfree(buf); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 490 | return sum; |
| 491 | } |
| 492 | |
| 493 | // The main_func is the main orchestrator, it performs a bunch of |
| 494 | // experiments. For every experiment, it orders all the readers |
| 495 | // involved to start and waits for them to finish the experiment. It |
| 496 | // then reads their timestamps and starts the next experiment. Each |
| 497 | // experiment progresses from 1 concurrent reader to N of them at which |
| 498 | // point all the timestamps are printed. |
| 499 | static int main_func(void *arg) |
| 500 | { |
Paul E. McKenney | f518f15 | 2020-05-25 17:32:56 -0700 | [diff] [blame] | 501 | bool errexit = false; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 502 | int exp, r; |
| 503 | char buf1[64]; |
Paul E. McKenney | f518f15 | 2020-05-25 17:32:56 -0700 | [diff] [blame] | 504 | char *buf; |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 505 | u64 *result_avg; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 506 | |
| 507 | set_cpus_allowed_ptr(current, cpumask_of(nreaders % nr_cpu_ids)); |
| 508 | set_user_nice(current, MAX_NICE); |
| 509 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 510 | VERBOSE_SCALEOUT("main_func task started"); |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 511 | result_avg = kzalloc(nruns * sizeof(*result_avg), GFP_KERNEL); |
Paul E. McKenney | f518f15 | 2020-05-25 17:32:56 -0700 | [diff] [blame] | 512 | buf = kzalloc(64 + nruns * 32, GFP_KERNEL); |
| 513 | if (!result_avg || !buf) { |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 514 | VERBOSE_SCALEOUT_ERRSTRING("out of memory"); |
Paul E. McKenney | f518f15 | 2020-05-25 17:32:56 -0700 | [diff] [blame] | 515 | errexit = true; |
| 516 | } |
Paul E. McKenney | 777a54c | 2020-05-25 14:16:44 -0700 | [diff] [blame] | 517 | if (holdoff) |
| 518 | schedule_timeout_interruptible(holdoff * HZ); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 519 | |
Paul E. McKenney | 96af866 | 2020-05-27 16:46:56 -0700 | [diff] [blame] | 520 | // Wait for all threads to start. |
| 521 | atomic_inc(&n_init); |
| 522 | while (atomic_read(&n_init) < nreaders + 1) |
| 523 | schedule_timeout_uninterruptible(1); |
| 524 | |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 525 | // Start exp readers up per experiment |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 526 | for (exp = 0; exp < nruns && !torture_must_stop(); exp++) { |
Paul E. McKenney | f518f15 | 2020-05-25 17:32:56 -0700 | [diff] [blame] | 527 | if (errexit) |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 528 | break; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 529 | if (torture_must_stop()) |
| 530 | goto end; |
| 531 | |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 532 | reset_readers(); |
| 533 | atomic_set(&nreaders_exp, nreaders); |
Paul E. McKenney | 86e0da2 | 2020-05-26 11:40:52 -0700 | [diff] [blame] | 534 | atomic_set(&n_started, nreaders); |
Paul E. McKenney | 2db0bda | 2020-05-26 12:34:57 -0700 | [diff] [blame] | 535 | atomic_set(&n_warmedup, nreaders); |
| 536 | atomic_set(&n_cooleddown, nreaders); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 537 | |
| 538 | exp_idx = exp; |
| 539 | |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 540 | for (r = 0; r < nreaders; r++) { |
Paul E. McKenney | af2789d | 2020-05-26 11:22:03 -0700 | [diff] [blame] | 541 | smp_store_release(&reader_tasks[r].start_reader, 1); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 542 | wake_up(&reader_tasks[r].wq); |
| 543 | } |
| 544 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 545 | VERBOSE_SCALEOUT("main_func: experiment started, waiting for %d readers", |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 546 | nreaders); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 547 | |
| 548 | wait_event(main_wq, |
| 549 | !atomic_read(&nreaders_exp) || torture_must_stop()); |
| 550 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 551 | VERBOSE_SCALEOUT("main_func: experiment ended"); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 552 | |
| 553 | if (torture_must_stop()) |
| 554 | goto end; |
| 555 | |
Arnd Bergmann | 7c944d7 | 2020-05-29 14:36:26 -0700 | [diff] [blame] | 556 | result_avg[exp] = div_u64(1000 * process_durations(nreaders), nreaders * loops); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | // Print the average of all experiments |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 560 | SCALEOUT("END OF TEST. Calculating average duration per loop (nanoseconds)...\n"); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 561 | |
Colin Ian King | 58db578 | 2020-07-16 15:38:56 +0100 | [diff] [blame] | 562 | if (!errexit) { |
| 563 | buf[0] = 0; |
| 564 | strcat(buf, "\n"); |
| 565 | strcat(buf, "Runs\tTime(ns)\n"); |
| 566 | } |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 567 | |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 568 | for (exp = 0; exp < nruns; exp++) { |
Arnd Bergmann | 7c944d7 | 2020-05-29 14:36:26 -0700 | [diff] [blame] | 569 | u64 avg; |
| 570 | u32 rem; |
| 571 | |
Paul E. McKenney | f518f15 | 2020-05-25 17:32:56 -0700 | [diff] [blame] | 572 | if (errexit) |
Paul E. McKenney | dbf28ef | 2020-05-25 17:22:24 -0700 | [diff] [blame] | 573 | break; |
Arnd Bergmann | 7c944d7 | 2020-05-29 14:36:26 -0700 | [diff] [blame] | 574 | avg = div_u64_rem(result_avg[exp], 1000, &rem); |
| 575 | sprintf(buf1, "%d\t%llu.%03u\n", exp + 1, avg, rem); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 576 | strcat(buf, buf1); |
| 577 | } |
| 578 | |
Paul E. McKenney | f518f15 | 2020-05-25 17:32:56 -0700 | [diff] [blame] | 579 | if (!errexit) |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 580 | SCALEOUT("%s", buf); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 581 | |
| 582 | // This will shutdown everything including us. |
| 583 | if (shutdown) { |
| 584 | shutdown_start = 1; |
| 585 | wake_up(&shutdown_wq); |
| 586 | } |
| 587 | |
| 588 | // Wait for torture to stop us |
| 589 | while (!torture_must_stop()) |
| 590 | schedule_timeout_uninterruptible(1); |
| 591 | |
| 592 | end: |
| 593 | torture_kthread_stopping("main_func"); |
Paul E. McKenney | f518f15 | 2020-05-25 17:32:56 -0700 | [diff] [blame] | 594 | kfree(result_avg); |
| 595 | kfree(buf); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | static void |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 600 | ref_scale_print_module_parms(struct ref_scale_ops *cur_ops, const char *tag) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 601 | { |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 602 | pr_alert("%s" SCALE_FLAG |
| 603 | "--- %s: verbose=%d shutdown=%d holdoff=%d loops=%ld nreaders=%d nruns=%d readdelay=%d\n", scale_type, tag, |
Paul E. McKenney | b4d1e34 | 2020-05-28 16:37:35 -0700 | [diff] [blame] | 604 | verbose, shutdown, holdoff, loops, nreaders, nruns, readdelay); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | static void |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 608 | ref_scale_cleanup(void) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 609 | { |
| 610 | int i; |
| 611 | |
| 612 | if (torture_cleanup_begin()) |
| 613 | return; |
| 614 | |
| 615 | if (!cur_ops) { |
| 616 | torture_cleanup_end(); |
| 617 | return; |
| 618 | } |
| 619 | |
| 620 | if (reader_tasks) { |
| 621 | for (i = 0; i < nreaders; i++) |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 622 | torture_stop_kthread("ref_scale_reader", |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 623 | reader_tasks[i].task); |
| 624 | } |
| 625 | kfree(reader_tasks); |
| 626 | |
| 627 | torture_stop_kthread("main_task", main_task); |
| 628 | kfree(main_task); |
| 629 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 630 | // Do scale-type-specific cleanup operations. |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 631 | if (cur_ops->cleanup != NULL) |
| 632 | cur_ops->cleanup(); |
| 633 | |
| 634 | torture_cleanup_end(); |
| 635 | } |
| 636 | |
| 637 | // Shutdown kthread. Just waits to be awakened, then shuts down system. |
| 638 | static int |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 639 | ref_scale_shutdown(void *arg) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 640 | { |
| 641 | wait_event(shutdown_wq, shutdown_start); |
| 642 | |
| 643 | smp_mb(); // Wake before output. |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 644 | ref_scale_cleanup(); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 645 | kernel_power_off(); |
| 646 | |
| 647 | return -EINVAL; |
| 648 | } |
| 649 | |
| 650 | static int __init |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 651 | ref_scale_init(void) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 652 | { |
| 653 | long i; |
| 654 | int firsterr = 0; |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 655 | static struct ref_scale_ops *scale_ops[] = { |
Paul E. McKenney | e13ef44 | 2020-06-03 11:56:34 -0700 | [diff] [blame] | 656 | &rcu_ops, &srcu_ops, &rcu_trace_ops, &rcu_tasks_ops, |
| 657 | &refcnt_ops, &rwlock_ops, &rwsem_ops, |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 658 | }; |
| 659 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 660 | if (!torture_init_begin(scale_type, verbose)) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 661 | return -EBUSY; |
| 662 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 663 | for (i = 0; i < ARRAY_SIZE(scale_ops); i++) { |
| 664 | cur_ops = scale_ops[i]; |
| 665 | if (strcmp(scale_type, cur_ops->name) == 0) |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 666 | break; |
| 667 | } |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 668 | if (i == ARRAY_SIZE(scale_ops)) { |
| 669 | pr_alert("rcu-scale: invalid scale type: \"%s\"\n", scale_type); |
| 670 | pr_alert("rcu-scale types:"); |
| 671 | for (i = 0; i < ARRAY_SIZE(scale_ops); i++) |
| 672 | pr_cont(" %s", scale_ops[i]->name); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 673 | pr_cont("\n"); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 674 | firsterr = -EINVAL; |
| 675 | cur_ops = NULL; |
| 676 | goto unwind; |
| 677 | } |
| 678 | if (cur_ops->init) |
| 679 | cur_ops->init(); |
| 680 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 681 | ref_scale_print_module_parms(cur_ops, "Start of test"); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 682 | |
| 683 | // Shutdown task |
| 684 | if (shutdown) { |
| 685 | init_waitqueue_head(&shutdown_wq); |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 686 | firsterr = torture_create_kthread(ref_scale_shutdown, NULL, |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 687 | shutdown_task); |
| 688 | if (firsterr) |
| 689 | goto unwind; |
| 690 | schedule_timeout_uninterruptible(1); |
| 691 | } |
| 692 | |
Paul E. McKenney | 8fc2878 | 2020-05-25 15:48:38 -0700 | [diff] [blame] | 693 | // Reader tasks (default to ~75% of online CPUs). |
| 694 | if (nreaders < 0) |
| 695 | nreaders = (num_online_cpus() >> 1) + (num_online_cpus() >> 2); |
Paul E. McKenney | 0c6d18d | 2020-08-27 09:58:19 -0700 | [diff] [blame] | 696 | if (WARN_ONCE(loops <= 0, "%s: loops = %ld, adjusted to 1\n", __func__, loops)) |
| 697 | loops = 1; |
| 698 | if (WARN_ONCE(nreaders <= 0, "%s: nreaders = %d, adjusted to 1\n", __func__, nreaders)) |
| 699 | nreaders = 1; |
| 700 | if (WARN_ONCE(nruns <= 0, "%s: nruns = %d, adjusted to 1\n", __func__, nruns)) |
| 701 | nruns = 1; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 702 | reader_tasks = kcalloc(nreaders, sizeof(reader_tasks[0]), |
| 703 | GFP_KERNEL); |
| 704 | if (!reader_tasks) { |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 705 | VERBOSE_SCALEOUT_ERRSTRING("out of memory"); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 706 | firsterr = -ENOMEM; |
| 707 | goto unwind; |
| 708 | } |
| 709 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 710 | VERBOSE_SCALEOUT("Starting %d reader threads\n", nreaders); |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 711 | |
| 712 | for (i = 0; i < nreaders; i++) { |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 713 | firsterr = torture_create_kthread(ref_scale_reader, (void *)i, |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 714 | reader_tasks[i].task); |
| 715 | if (firsterr) |
| 716 | goto unwind; |
| 717 | |
| 718 | init_waitqueue_head(&(reader_tasks[i].wq)); |
| 719 | } |
| 720 | |
| 721 | // Main Task |
| 722 | init_waitqueue_head(&main_wq); |
| 723 | firsterr = torture_create_kthread(main_func, NULL, main_task); |
| 724 | if (firsterr) |
| 725 | goto unwind; |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 726 | |
| 727 | torture_init_end(); |
| 728 | return 0; |
| 729 | |
| 730 | unwind: |
| 731 | torture_init_end(); |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 732 | ref_scale_cleanup(); |
Paul E. McKenney | bc80d35 | 2020-09-17 10:37:10 -0700 | [diff] [blame] | 733 | if (shutdown) { |
| 734 | WARN_ON(!IS_MODULE(CONFIG_RCU_REF_SCALE_TEST)); |
| 735 | kernel_power_off(); |
| 736 | } |
Joel Fernandes (Google) | 653ed64 | 2020-05-25 00:36:48 -0400 | [diff] [blame] | 737 | return firsterr; |
| 738 | } |
| 739 | |
Paul E. McKenney | 1fbeb3a | 2020-06-17 11:53:53 -0700 | [diff] [blame] | 740 | module_init(ref_scale_init); |
| 741 | module_exit(ref_scale_cleanup); |