blob: 952595c678b37299a111e288cb8d4e711759dd14 [file] [log] [blame]
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -04001// SPDX-License-Identifier: GPL-2.0+
2//
Paul E. McKenney8e4ec3d2020-06-17 11:33:54 -07003// Scalability test comparing RCU vs other mechanisms
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -04004// 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. McKenney72bb7492020-06-02 08:34:41 -070028#include <linux/rcupdate_trace.h>
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -040029#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. McKenney1fbeb3a2020-06-17 11:53:53 -070041#define SCALE_FLAG "-ref-scale: "
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -040042
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -070043#define SCALEOUT(s, x...) \
44 pr_alert("%s" SCALE_FLAG s, scale_type, ## x)
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -040045
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -070046#define VERBOSE_SCALEOUT(s, x...) \
47 do { if (verbose) pr_alert("%s" SCALE_FLAG s, scale_type, ## x); } while (0)
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -040048
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -070049#define VERBOSE_SCALEOUT_ERRSTRING(s, x...) \
50 do { if (verbose) pr_alert("%s" SCALE_FLAG "!!! " s, scale_type, ## x); } while (0)
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -040051
52MODULE_LICENSE("GPL");
53MODULE_AUTHOR("Joel Fernandes (Google) <joel@joelfernandes.org>");
54
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -070055static char *scale_type = "rcu";
56module_param(scale_type, charp, 0444);
57MODULE_PARM_DESC(scale_type, "Type of test (rcu, srcu, refcnt, rwsem, rwlock.");
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -040058
59torture_param(int, verbose, 0, "Enable verbose debugging printk()s");
60
Paul E. McKenney777a54c2020-05-25 14:16:44 -070061// Wait until there are multiple CPUs before starting test.
Paul E. McKenney8e4ec3d2020-06-17 11:33:54 -070062torture_param(int, holdoff, IS_BUILTIN(CONFIG_RCU_REF_SCALE_TEST) ? 10 : 0,
Paul E. McKenney777a54c2020-05-25 14:16:44 -070063 "Holdoff time before test start (s)");
64// Number of loops per experiment, all readers execute operations concurrently.
Paul E. McKenney4dd72a32020-05-29 13:11:26 -070065torture_param(long, loops, 10000, "Number of loops per experiment.");
Paul E. McKenney8fc28782020-05-25 15:48:38 -070066// Number of readers, with -1 defaulting to about 75% of the CPUs.
67torture_param(int, nreaders, -1, "Number of readers, -1 for 75% of CPUs.");
68// Number of runs.
69torture_param(int, nruns, 30, "Number of experiments to run.");
Paul E. McKenney918b3512020-05-31 18:14:57 -070070// Reader delay in nanoseconds, 0 for no delay.
71torture_param(int, readdelay, 0, "Read-side delay in nanoseconds.");
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -040072
73#ifdef MODULE
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -070074# define REFSCALE_SHUTDOWN 0
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -040075#else
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -070076# define REFSCALE_SHUTDOWN 1
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -040077#endif
78
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -070079torture_param(bool, shutdown, REFSCALE_SHUTDOWN,
80 "Shutdown at end of scalability tests.");
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -040081
82struct reader_task {
83 struct task_struct *task;
Paul E. McKenneyaf2789d2020-05-26 11:22:03 -070084 int start_reader;
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -040085 wait_queue_head_t wq;
86 u64 last_duration_ns;
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -040087};
88
89static struct task_struct *shutdown_task;
90static wait_queue_head_t shutdown_wq;
91
92static struct task_struct *main_task;
93static wait_queue_head_t main_wq;
94static int shutdown_start;
95
96static struct reader_task *reader_tasks;
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -040097
98// Number of readers that are part of the current experiment.
99static atomic_t nreaders_exp;
100
101// Use to wait for all threads to start.
102static atomic_t n_init;
Paul E. McKenney86e0da22020-05-26 11:40:52 -0700103static atomic_t n_started;
Paul E. McKenney2db0bda2020-05-26 12:34:57 -0700104static atomic_t n_warmedup;
105static atomic_t n_cooleddown;
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400106
107// Track which experiment is currently running.
108static int exp_idx;
109
110// Operations vector for selecting different types of tests.
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700111struct ref_scale_ops {
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400112 void (*init)(void);
113 void (*cleanup)(void);
Paul E. McKenney75dd8ef2020-05-25 14:59:06 -0700114 void (*readsection)(const int nloops);
Paul E. McKenney918b3512020-05-31 18:14:57 -0700115 void (*delaysection)(const int nloops, const int udl, const int ndl);
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400116 const char *name;
117};
118
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700119static struct ref_scale_ops *cur_ops;
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400120
Paul E. McKenney918b3512020-05-31 18:14:57 -0700121static void un_delay(const int udl, const int ndl)
122{
123 if (udl)
124 udelay(udl);
125 if (ndl)
126 ndelay(ndl);
127}
128
Paul E. McKenney75dd8ef2020-05-25 14:59:06 -0700129static void ref_rcu_read_section(const int nloops)
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400130{
Paul E. McKenney75dd8ef2020-05-25 14:59:06 -0700131 int i;
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400132
Paul E. McKenney75dd8ef2020-05-25 14:59:06 -0700133 for (i = nloops; i >= 0; i--) {
134 rcu_read_lock();
135 rcu_read_unlock();
136 }
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400137}
138
Paul E. McKenney918b3512020-05-31 18:14:57 -0700139static void ref_rcu_delay_section(const int nloops, const int udl, const int ndl)
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700140{
141 int i;
142
143 for (i = nloops; i >= 0; i--) {
144 rcu_read_lock();
Paul E. McKenney918b3512020-05-31 18:14:57 -0700145 un_delay(udl, ndl);
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700146 rcu_read_unlock();
147 }
148}
149
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700150static void rcu_sync_scale_init(void)
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400151{
152}
153
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700154static struct ref_scale_ops rcu_ops = {
155 .init = rcu_sync_scale_init,
Paul E. McKenney75dd8ef2020-05-25 14:59:06 -0700156 .readsection = ref_rcu_read_section,
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700157 .delaysection = ref_rcu_delay_section,
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400158 .name = "rcu"
159};
160
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700161// Definitions for SRCU ref scale testing.
162DEFINE_STATIC_SRCU(srcu_refctl_scale);
163static struct srcu_struct *srcu_ctlp = &srcu_refctl_scale;
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400164
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700165static void srcu_ref_scale_read_section(const int nloops)
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400166{
Paul E. McKenney75dd8ef2020-05-25 14:59:06 -0700167 int i;
168 int idx;
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400169
Paul E. McKenney75dd8ef2020-05-25 14:59:06 -0700170 for (i = nloops; i >= 0; i--) {
171 idx = srcu_read_lock(srcu_ctlp);
172 srcu_read_unlock(srcu_ctlp, idx);
173 }
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400174}
175
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700176static void srcu_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700177{
178 int i;
179 int idx;
180
181 for (i = nloops; i >= 0; i--) {
182 idx = srcu_read_lock(srcu_ctlp);
Paul E. McKenney918b3512020-05-31 18:14:57 -0700183 un_delay(udl, ndl);
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700184 srcu_read_unlock(srcu_ctlp, idx);
185 }
186}
187
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700188static struct ref_scale_ops srcu_ops = {
189 .init = rcu_sync_scale_init,
190 .readsection = srcu_ref_scale_read_section,
191 .delaysection = srcu_ref_scale_delay_section,
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400192 .name = "srcu"
193};
194
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700195// Definitions for RCU Tasks ref scale testing: Empty read markers.
Paul E. McKenneye13ef442020-06-03 11:56:34 -0700196// These definitions also work for RCU Rude readers.
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700197static void rcu_tasks_ref_scale_read_section(const int nloops)
Paul E. McKenneye13ef442020-06-03 11:56:34 -0700198{
199 int i;
200
201 for (i = nloops; i >= 0; i--)
202 continue;
203}
204
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700205static void rcu_tasks_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
Paul E. McKenneye13ef442020-06-03 11:56:34 -0700206{
207 int i;
208
209 for (i = nloops; i >= 0; i--)
210 un_delay(udl, ndl);
211}
212
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700213static struct ref_scale_ops rcu_tasks_ops = {
214 .init = rcu_sync_scale_init,
215 .readsection = rcu_tasks_ref_scale_read_section,
216 .delaysection = rcu_tasks_ref_scale_delay_section,
Paul E. McKenneye13ef442020-06-03 11:56:34 -0700217 .name = "rcu-tasks"
218};
219
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700220// Definitions for RCU Tasks Trace ref scale testing.
221static void rcu_trace_ref_scale_read_section(const int nloops)
Paul E. McKenney72bb7492020-06-02 08:34:41 -0700222{
223 int i;
224
225 for (i = nloops; i >= 0; i--) {
226 rcu_read_lock_trace();
227 rcu_read_unlock_trace();
228 }
229}
230
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700231static void rcu_trace_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
Paul E. McKenney72bb7492020-06-02 08:34:41 -0700232{
233 int i;
234
235 for (i = nloops; i >= 0; i--) {
236 rcu_read_lock_trace();
237 un_delay(udl, ndl);
238 rcu_read_unlock_trace();
239 }
240}
241
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700242static struct ref_scale_ops rcu_trace_ops = {
243 .init = rcu_sync_scale_init,
244 .readsection = rcu_trace_ref_scale_read_section,
245 .delaysection = rcu_trace_ref_scale_delay_section,
Paul E. McKenney72bb7492020-06-02 08:34:41 -0700246 .name = "rcu-trace"
247};
248
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400249// Definitions for reference count
250static atomic_t refcnt;
251
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700252static void ref_refcnt_section(const int nloops)
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400253{
Paul E. McKenney75dd8ef2020-05-25 14:59:06 -0700254 int i;
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400255
Paul E. McKenney75dd8ef2020-05-25 14:59:06 -0700256 for (i = nloops; i >= 0; i--) {
257 atomic_inc(&refcnt);
258 atomic_dec(&refcnt);
259 }
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400260}
261
Paul E. McKenney918b3512020-05-31 18:14:57 -0700262static void ref_refcnt_delay_section(const int nloops, const int udl, const int ndl)
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700263{
264 int i;
265
266 for (i = nloops; i >= 0; i--) {
267 atomic_inc(&refcnt);
Paul E. McKenney918b3512020-05-31 18:14:57 -0700268 un_delay(udl, ndl);
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700269 atomic_dec(&refcnt);
270 }
271}
272
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700273static struct ref_scale_ops refcnt_ops = {
274 .init = rcu_sync_scale_init,
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700275 .readsection = ref_refcnt_section,
276 .delaysection = ref_refcnt_delay_section,
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400277 .name = "refcnt"
278};
279
280// Definitions for rwlock
281static rwlock_t test_rwlock;
282
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700283static void ref_rwlock_init(void)
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400284{
285 rwlock_init(&test_rwlock);
286}
287
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700288static void ref_rwlock_section(const int nloops)
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400289{
Paul E. McKenney75dd8ef2020-05-25 14:59:06 -0700290 int i;
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400291
Paul E. McKenney75dd8ef2020-05-25 14:59:06 -0700292 for (i = nloops; i >= 0; i--) {
293 read_lock(&test_rwlock);
294 read_unlock(&test_rwlock);
295 }
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400296}
297
Paul E. McKenney918b3512020-05-31 18:14:57 -0700298static void ref_rwlock_delay_section(const int nloops, const int udl, const int ndl)
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700299{
300 int i;
301
302 for (i = nloops; i >= 0; i--) {
303 read_lock(&test_rwlock);
Paul E. McKenney918b3512020-05-31 18:14:57 -0700304 un_delay(udl, ndl);
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700305 read_unlock(&test_rwlock);
306 }
307}
308
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700309static struct ref_scale_ops rwlock_ops = {
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700310 .init = ref_rwlock_init,
311 .readsection = ref_rwlock_section,
312 .delaysection = ref_rwlock_delay_section,
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400313 .name = "rwlock"
314};
315
316// Definitions for rwsem
317static struct rw_semaphore test_rwsem;
318
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700319static void ref_rwsem_init(void)
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400320{
321 init_rwsem(&test_rwsem);
322}
323
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700324static void ref_rwsem_section(const int nloops)
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400325{
Paul E. McKenney75dd8ef2020-05-25 14:59:06 -0700326 int i;
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400327
Paul E. McKenney75dd8ef2020-05-25 14:59:06 -0700328 for (i = nloops; i >= 0; i--) {
329 down_read(&test_rwsem);
330 up_read(&test_rwsem);
331 }
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400332}
333
Paul E. McKenney918b3512020-05-31 18:14:57 -0700334static void ref_rwsem_delay_section(const int nloops, const int udl, const int ndl)
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700335{
336 int i;
337
338 for (i = nloops; i >= 0; i--) {
339 down_read(&test_rwsem);
Paul E. McKenney918b3512020-05-31 18:14:57 -0700340 un_delay(udl, ndl);
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700341 up_read(&test_rwsem);
342 }
343}
344
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700345static struct ref_scale_ops rwsem_ops = {
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700346 .init = ref_rwsem_init,
347 .readsection = ref_rwsem_section,
348 .delaysection = ref_rwsem_delay_section,
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400349 .name = "rwsem"
350};
351
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700352static void rcu_scale_one_reader(void)
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700353{
354 if (readdelay <= 0)
355 cur_ops->readsection(loops);
356 else
Paul E. McKenney918b3512020-05-31 18:14:57 -0700357 cur_ops->delaysection(loops, readdelay / 1000, readdelay % 1000);
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700358}
359
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400360// Reader kthread. Repeatedly does empty RCU read-side
361// critical section, minimizing update-side interference.
362static int
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700363ref_scale_reader(void *arg)
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400364{
365 unsigned long flags;
366 long me = (long)arg;
367 struct reader_task *rt = &(reader_tasks[me]);
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400368 u64 start;
369 s64 duration;
370
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700371 VERBOSE_SCALEOUT("ref_scale_reader %ld: task started", me);
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400372 set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids));
373 set_user_nice(current, MAX_NICE);
374 atomic_inc(&n_init);
Paul E. McKenney777a54c2020-05-25 14:16:44 -0700375 if (holdoff)
376 schedule_timeout_interruptible(holdoff * HZ);
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400377repeat:
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700378 VERBOSE_SCALEOUT("ref_scale_reader %ld: waiting to start next experiment on cpu %d", me, smp_processor_id());
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400379
380 // Wait for signal that this reader can start.
Paul E. McKenneyaf2789d2020-05-26 11:22:03 -0700381 wait_event(rt->wq, (atomic_read(&nreaders_exp) && smp_load_acquire(&rt->start_reader)) ||
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400382 torture_must_stop());
383
384 if (torture_must_stop())
385 goto end;
386
387 // Make sure that the CPU is affinitized appropriately during testing.
388 WARN_ON_ONCE(smp_processor_id() != me);
389
Paul E. McKenneyaf2789d2020-05-26 11:22:03 -0700390 WRITE_ONCE(rt->start_reader, 0);
Paul E. McKenney86e0da22020-05-26 11:40:52 -0700391 if (!atomic_dec_return(&n_started))
392 while (atomic_read_acquire(&n_started))
393 cpu_relax();
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400394
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700395 VERBOSE_SCALEOUT("ref_scale_reader %ld: experiment %d started", me, exp_idx);
Paul E. McKenneyb864f892020-05-26 10:57:34 -0700396
Paul E. McKenney2db0bda2020-05-26 12:34:57 -0700397
398 // To reduce noise, do an initial cache-warming invocation, check
399 // in, and then keep warming until everyone has checked in.
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700400 rcu_scale_one_reader();
Paul E. McKenney2db0bda2020-05-26 12:34:57 -0700401 if (!atomic_dec_return(&n_warmedup))
402 while (atomic_read_acquire(&n_warmedup))
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700403 rcu_scale_one_reader();
Paul E. McKenney2db0bda2020-05-26 12:34:57 -0700404 // Also keep interrupts disabled. This also has the effect
405 // of preventing entries into slow path for rcu_read_unlock().
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400406 local_irq_save(flags);
407 start = ktime_get_mono_fast_ns();
408
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700409 rcu_scale_one_reader();
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400410
411 duration = ktime_get_mono_fast_ns() - start;
412 local_irq_restore(flags);
413
414 rt->last_duration_ns = WARN_ON_ONCE(duration < 0) ? 0 : duration;
Paul E. McKenney2db0bda2020-05-26 12:34:57 -0700415 // To reduce runtime-skew noise, do maintain-load invocations until
416 // everyone is done.
417 if (!atomic_dec_return(&n_cooleddown))
418 while (atomic_read_acquire(&n_cooleddown))
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700419 rcu_scale_one_reader();
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400420
Paul E. McKenneyb864f892020-05-26 10:57:34 -0700421 if (atomic_dec_and_test(&nreaders_exp))
422 wake_up(&main_wq);
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400423
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700424 VERBOSE_SCALEOUT("ref_scale_reader %ld: experiment %d ended, (readers remaining=%d)",
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400425 me, exp_idx, atomic_read(&nreaders_exp));
426
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400427 if (!torture_must_stop())
428 goto repeat;
429end:
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700430 torture_kthread_stopping("ref_scale_reader");
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400431 return 0;
432}
433
Paul E. McKenney29907502020-05-26 09:32:57 -0700434static void reset_readers(void)
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400435{
436 int i;
437 struct reader_task *rt;
438
Paul E. McKenneydbf28ef2020-05-25 17:22:24 -0700439 for (i = 0; i < nreaders; i++) {
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400440 rt = &(reader_tasks[i]);
441
442 rt->last_duration_ns = 0;
443 }
444}
445
446// Print the results of each reader and return the sum of all their durations.
Paul E. McKenney29907502020-05-26 09:32:57 -0700447static u64 process_durations(int n)
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400448{
449 int i;
450 struct reader_task *rt;
451 char buf1[64];
Paul E. McKenney2e90de72020-05-25 17:45:03 -0700452 char *buf;
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400453 u64 sum = 0;
454
Paul E. McKenney2e90de72020-05-25 17:45:03 -0700455 buf = kmalloc(128 + nreaders * 32, GFP_KERNEL);
456 if (!buf)
457 return 0;
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400458 buf[0] = 0;
459 sprintf(buf, "Experiment #%d (Format: <THREAD-NUM>:<Total loop time in ns>)",
460 exp_idx);
461
Paul E. McKenneydbf28ef2020-05-25 17:22:24 -0700462 for (i = 0; i < n && !torture_must_stop(); i++) {
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400463 rt = &(reader_tasks[i]);
464 sprintf(buf1, "%d: %llu\t", i, rt->last_duration_ns);
465
466 if (i % 5 == 0)
467 strcat(buf, "\n");
468 strcat(buf, buf1);
469
470 sum += rt->last_duration_ns;
471 }
472 strcat(buf, "\n");
473
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700474 SCALEOUT("%s\n", buf);
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400475
Paul E. McKenney2e90de72020-05-25 17:45:03 -0700476 kfree(buf);
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400477 return sum;
478}
479
480// The main_func is the main orchestrator, it performs a bunch of
481// experiments. For every experiment, it orders all the readers
482// involved to start and waits for them to finish the experiment. It
483// then reads their timestamps and starts the next experiment. Each
484// experiment progresses from 1 concurrent reader to N of them at which
485// point all the timestamps are printed.
486static int main_func(void *arg)
487{
Paul E. McKenneyf518f152020-05-25 17:32:56 -0700488 bool errexit = false;
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400489 int exp, r;
490 char buf1[64];
Paul E. McKenneyf518f152020-05-25 17:32:56 -0700491 char *buf;
Paul E. McKenneydbf28ef2020-05-25 17:22:24 -0700492 u64 *result_avg;
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400493
494 set_cpus_allowed_ptr(current, cpumask_of(nreaders % nr_cpu_ids));
495 set_user_nice(current, MAX_NICE);
496
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700497 VERBOSE_SCALEOUT("main_func task started");
Paul E. McKenneydbf28ef2020-05-25 17:22:24 -0700498 result_avg = kzalloc(nruns * sizeof(*result_avg), GFP_KERNEL);
Paul E. McKenneyf518f152020-05-25 17:32:56 -0700499 buf = kzalloc(64 + nruns * 32, GFP_KERNEL);
500 if (!result_avg || !buf) {
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700501 VERBOSE_SCALEOUT_ERRSTRING("out of memory");
Paul E. McKenneyf518f152020-05-25 17:32:56 -0700502 errexit = true;
503 }
Paul E. McKenney777a54c2020-05-25 14:16:44 -0700504 if (holdoff)
505 schedule_timeout_interruptible(holdoff * HZ);
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400506
Paul E. McKenney96af8662020-05-27 16:46:56 -0700507 // Wait for all threads to start.
508 atomic_inc(&n_init);
509 while (atomic_read(&n_init) < nreaders + 1)
510 schedule_timeout_uninterruptible(1);
511
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400512 // Start exp readers up per experiment
Paul E. McKenneydbf28ef2020-05-25 17:22:24 -0700513 for (exp = 0; exp < nruns && !torture_must_stop(); exp++) {
Paul E. McKenneyf518f152020-05-25 17:32:56 -0700514 if (errexit)
Paul E. McKenneydbf28ef2020-05-25 17:22:24 -0700515 break;
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400516 if (torture_must_stop())
517 goto end;
518
Paul E. McKenneydbf28ef2020-05-25 17:22:24 -0700519 reset_readers();
520 atomic_set(&nreaders_exp, nreaders);
Paul E. McKenney86e0da22020-05-26 11:40:52 -0700521 atomic_set(&n_started, nreaders);
Paul E. McKenney2db0bda2020-05-26 12:34:57 -0700522 atomic_set(&n_warmedup, nreaders);
523 atomic_set(&n_cooleddown, nreaders);
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400524
525 exp_idx = exp;
526
Paul E. McKenneydbf28ef2020-05-25 17:22:24 -0700527 for (r = 0; r < nreaders; r++) {
Paul E. McKenneyaf2789d2020-05-26 11:22:03 -0700528 smp_store_release(&reader_tasks[r].start_reader, 1);
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400529 wake_up(&reader_tasks[r].wq);
530 }
531
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700532 VERBOSE_SCALEOUT("main_func: experiment started, waiting for %d readers",
Paul E. McKenneydbf28ef2020-05-25 17:22:24 -0700533 nreaders);
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400534
535 wait_event(main_wq,
536 !atomic_read(&nreaders_exp) || torture_must_stop());
537
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700538 VERBOSE_SCALEOUT("main_func: experiment ended");
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400539
540 if (torture_must_stop())
541 goto end;
542
Arnd Bergmann7c944d72020-05-29 14:36:26 -0700543 result_avg[exp] = div_u64(1000 * process_durations(nreaders), nreaders * loops);
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400544 }
545
546 // Print the average of all experiments
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700547 SCALEOUT("END OF TEST. Calculating average duration per loop (nanoseconds)...\n");
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400548
Colin Ian King58db5782020-07-16 15:38:56 +0100549 if (!errexit) {
550 buf[0] = 0;
551 strcat(buf, "\n");
552 strcat(buf, "Runs\tTime(ns)\n");
553 }
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400554
Paul E. McKenneydbf28ef2020-05-25 17:22:24 -0700555 for (exp = 0; exp < nruns; exp++) {
Arnd Bergmann7c944d72020-05-29 14:36:26 -0700556 u64 avg;
557 u32 rem;
558
Paul E. McKenneyf518f152020-05-25 17:32:56 -0700559 if (errexit)
Paul E. McKenneydbf28ef2020-05-25 17:22:24 -0700560 break;
Arnd Bergmann7c944d72020-05-29 14:36:26 -0700561 avg = div_u64_rem(result_avg[exp], 1000, &rem);
562 sprintf(buf1, "%d\t%llu.%03u\n", exp + 1, avg, rem);
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400563 strcat(buf, buf1);
564 }
565
Paul E. McKenneyf518f152020-05-25 17:32:56 -0700566 if (!errexit)
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700567 SCALEOUT("%s", buf);
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400568
569 // This will shutdown everything including us.
570 if (shutdown) {
571 shutdown_start = 1;
572 wake_up(&shutdown_wq);
573 }
574
575 // Wait for torture to stop us
576 while (!torture_must_stop())
577 schedule_timeout_uninterruptible(1);
578
579end:
580 torture_kthread_stopping("main_func");
Paul E. McKenneyf518f152020-05-25 17:32:56 -0700581 kfree(result_avg);
582 kfree(buf);
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400583 return 0;
584}
585
586static void
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700587ref_scale_print_module_parms(struct ref_scale_ops *cur_ops, const char *tag)
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400588{
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700589 pr_alert("%s" SCALE_FLAG
590 "--- %s: verbose=%d shutdown=%d holdoff=%d loops=%ld nreaders=%d nruns=%d readdelay=%d\n", scale_type, tag,
Paul E. McKenneyb4d1e342020-05-28 16:37:35 -0700591 verbose, shutdown, holdoff, loops, nreaders, nruns, readdelay);
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400592}
593
594static void
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700595ref_scale_cleanup(void)
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400596{
597 int i;
598
599 if (torture_cleanup_begin())
600 return;
601
602 if (!cur_ops) {
603 torture_cleanup_end();
604 return;
605 }
606
607 if (reader_tasks) {
608 for (i = 0; i < nreaders; i++)
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700609 torture_stop_kthread("ref_scale_reader",
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400610 reader_tasks[i].task);
611 }
612 kfree(reader_tasks);
613
614 torture_stop_kthread("main_task", main_task);
615 kfree(main_task);
616
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700617 // Do scale-type-specific cleanup operations.
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400618 if (cur_ops->cleanup != NULL)
619 cur_ops->cleanup();
620
621 torture_cleanup_end();
622}
623
624// Shutdown kthread. Just waits to be awakened, then shuts down system.
625static int
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700626ref_scale_shutdown(void *arg)
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400627{
628 wait_event(shutdown_wq, shutdown_start);
629
630 smp_mb(); // Wake before output.
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700631 ref_scale_cleanup();
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400632 kernel_power_off();
633
634 return -EINVAL;
635}
636
637static int __init
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700638ref_scale_init(void)
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400639{
640 long i;
641 int firsterr = 0;
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700642 static struct ref_scale_ops *scale_ops[] = {
Paul E. McKenneye13ef442020-06-03 11:56:34 -0700643 &rcu_ops, &srcu_ops, &rcu_trace_ops, &rcu_tasks_ops,
644 &refcnt_ops, &rwlock_ops, &rwsem_ops,
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400645 };
646
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700647 if (!torture_init_begin(scale_type, verbose))
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400648 return -EBUSY;
649
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700650 for (i = 0; i < ARRAY_SIZE(scale_ops); i++) {
651 cur_ops = scale_ops[i];
652 if (strcmp(scale_type, cur_ops->name) == 0)
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400653 break;
654 }
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700655 if (i == ARRAY_SIZE(scale_ops)) {
656 pr_alert("rcu-scale: invalid scale type: \"%s\"\n", scale_type);
657 pr_alert("rcu-scale types:");
658 for (i = 0; i < ARRAY_SIZE(scale_ops); i++)
659 pr_cont(" %s", scale_ops[i]->name);
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400660 pr_cont("\n");
Paul E. McKenney8e4ec3d2020-06-17 11:33:54 -0700661 WARN_ON(!IS_MODULE(CONFIG_RCU_REF_SCALE_TEST));
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400662 firsterr = -EINVAL;
663 cur_ops = NULL;
664 goto unwind;
665 }
666 if (cur_ops->init)
667 cur_ops->init();
668
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700669 ref_scale_print_module_parms(cur_ops, "Start of test");
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400670
671 // Shutdown task
672 if (shutdown) {
673 init_waitqueue_head(&shutdown_wq);
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700674 firsterr = torture_create_kthread(ref_scale_shutdown, NULL,
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400675 shutdown_task);
676 if (firsterr)
677 goto unwind;
678 schedule_timeout_uninterruptible(1);
679 }
680
Paul E. McKenney8fc28782020-05-25 15:48:38 -0700681 // Reader tasks (default to ~75% of online CPUs).
682 if (nreaders < 0)
683 nreaders = (num_online_cpus() >> 1) + (num_online_cpus() >> 2);
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400684 reader_tasks = kcalloc(nreaders, sizeof(reader_tasks[0]),
685 GFP_KERNEL);
686 if (!reader_tasks) {
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700687 VERBOSE_SCALEOUT_ERRSTRING("out of memory");
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400688 firsterr = -ENOMEM;
689 goto unwind;
690 }
691
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700692 VERBOSE_SCALEOUT("Starting %d reader threads\n", nreaders);
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400693
694 for (i = 0; i < nreaders; i++) {
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700695 firsterr = torture_create_kthread(ref_scale_reader, (void *)i,
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400696 reader_tasks[i].task);
697 if (firsterr)
698 goto unwind;
699
700 init_waitqueue_head(&(reader_tasks[i].wq));
701 }
702
703 // Main Task
704 init_waitqueue_head(&main_wq);
705 firsterr = torture_create_kthread(main_func, NULL, main_task);
706 if (firsterr)
707 goto unwind;
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400708
709 torture_init_end();
710 return 0;
711
712unwind:
713 torture_init_end();
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700714 ref_scale_cleanup();
Joel Fernandes (Google)653ed642020-05-25 00:36:48 -0400715 return firsterr;
716}
717
Paul E. McKenney1fbeb3a2020-06-17 11:53:53 -0700718module_init(ref_scale_init);
719module_exit(ref_scale_cleanup);