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