1 // SPDX-License-Identifier: GPL-2.0+
3 // Scalability test comparing RCU vs other mechanisms
4 // for acquiring references on objects.
6 // Copyright (C) Google, 2020.
8 // Author: Joel Fernandes <joel@joelfernandes.org>
10 #define pr_fmt(fmt) fmt
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>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/notifier.h>
26 #include <linux/percpu.h>
27 #include <linux/rcupdate.h>
28 #include <linux/rcupdate_trace.h>
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>
41 #define SCALE_FLAG "-ref-scale: "
43 #define SCALEOUT(s, x...) \
44 pr_alert("%s" SCALE_FLAG s, scale_type, ## x)
46 #define VERBOSE_SCALEOUT(s, x...) \
49 pr_alert("%s" SCALE_FLAG s "\n", scale_type, ## x); \
52 static atomic_t verbose_batch_ctr;
54 #define VERBOSE_SCALEOUT_BATCH(s, x...) \
57 (verbose_batched <= 0 || \
58 !(atomic_inc_return(&verbose_batch_ctr) % verbose_batched))) { \
59 schedule_timeout_uninterruptible(1); \
60 pr_alert("%s" SCALE_FLAG s "\n", scale_type, ## x); \
64 #define SCALEOUT_ERRSTRING(s, x...) pr_alert("%s" SCALE_FLAG "!!! " s "\n", scale_type, ## x)
66 MODULE_LICENSE("GPL");
67 MODULE_AUTHOR("Joel Fernandes (Google) <joel@joelfernandes.org>");
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.");
73 torture_param(int, verbose, 0, "Enable verbose debugging printk()s");
74 torture_param(int, verbose_batched, 0, "Batch verbose debugging printk()s");
76 // Wait until there are multiple CPUs before starting test.
77 torture_param(int, holdoff, IS_BUILTIN(CONFIG_RCU_REF_SCALE_TEST) ? 10 : 0,
78 "Holdoff time before test start (s)");
79 // Number of typesafe_lookup structures, that is, the degree of concurrency.
80 torture_param(long, lookup_instances, 0, "Number of typesafe_lookup structures.");
81 // Number of loops per experiment, all readers execute operations concurrently.
82 torture_param(long, loops, 10000, "Number of loops per experiment.");
83 // Number of readers, with -1 defaulting to about 75% of the CPUs.
84 torture_param(int, nreaders, -1, "Number of readers, -1 for 75% of CPUs.");
86 torture_param(int, nruns, 30, "Number of experiments to run.");
87 // Reader delay in nanoseconds, 0 for no delay.
88 torture_param(int, readdelay, 0, "Read-side delay in nanoseconds.");
91 # define REFSCALE_SHUTDOWN 0
93 # define REFSCALE_SHUTDOWN 1
96 torture_param(bool, shutdown, REFSCALE_SHUTDOWN,
97 "Shutdown at end of scalability tests.");
100 struct task_struct *task;
102 wait_queue_head_t wq;
103 u64 last_duration_ns;
106 static struct task_struct *shutdown_task;
107 static wait_queue_head_t shutdown_wq;
109 static struct task_struct *main_task;
110 static wait_queue_head_t main_wq;
111 static int shutdown_start;
113 static struct reader_task *reader_tasks;
115 // Number of readers that are part of the current experiment.
116 static atomic_t nreaders_exp;
118 // Use to wait for all threads to start.
119 static atomic_t n_init;
120 static atomic_t n_started;
121 static atomic_t n_warmedup;
122 static atomic_t n_cooleddown;
124 // Track which experiment is currently running.
127 // Operations vector for selecting different types of tests.
128 struct ref_scale_ops {
130 void (*cleanup)(void);
131 void (*readsection)(const int nloops);
132 void (*delaysection)(const int nloops, const int udl, const int ndl);
136 static struct ref_scale_ops *cur_ops;
138 static void un_delay(const int udl, const int ndl)
146 static void ref_rcu_read_section(const int nloops)
150 for (i = nloops; i >= 0; i--) {
156 static void ref_rcu_delay_section(const int nloops, const int udl, const int ndl)
160 for (i = nloops; i >= 0; i--) {
167 static bool rcu_sync_scale_init(void)
172 static struct ref_scale_ops rcu_ops = {
173 .init = rcu_sync_scale_init,
174 .readsection = ref_rcu_read_section,
175 .delaysection = ref_rcu_delay_section,
179 // Definitions for SRCU ref scale testing.
180 DEFINE_STATIC_SRCU(srcu_refctl_scale);
181 static struct srcu_struct *srcu_ctlp = &srcu_refctl_scale;
183 static void srcu_ref_scale_read_section(const int nloops)
188 for (i = nloops; i >= 0; i--) {
189 idx = srcu_read_lock(srcu_ctlp);
190 srcu_read_unlock(srcu_ctlp, idx);
194 static void srcu_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
199 for (i = nloops; i >= 0; i--) {
200 idx = srcu_read_lock(srcu_ctlp);
202 srcu_read_unlock(srcu_ctlp, idx);
206 static struct ref_scale_ops srcu_ops = {
207 .init = rcu_sync_scale_init,
208 .readsection = srcu_ref_scale_read_section,
209 .delaysection = srcu_ref_scale_delay_section,
213 #ifdef CONFIG_TASKS_RCU
215 // Definitions for RCU Tasks ref scale testing: Empty read markers.
216 // These definitions also work for RCU Rude readers.
217 static void rcu_tasks_ref_scale_read_section(const int nloops)
221 for (i = nloops; i >= 0; i--)
225 static void rcu_tasks_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
229 for (i = nloops; i >= 0; i--)
233 static struct ref_scale_ops rcu_tasks_ops = {
234 .init = rcu_sync_scale_init,
235 .readsection = rcu_tasks_ref_scale_read_section,
236 .delaysection = rcu_tasks_ref_scale_delay_section,
240 #define RCU_TASKS_OPS &rcu_tasks_ops,
242 #else // #ifdef CONFIG_TASKS_RCU
244 #define RCU_TASKS_OPS
246 #endif // #else // #ifdef CONFIG_TASKS_RCU
248 #ifdef CONFIG_TASKS_TRACE_RCU
250 // Definitions for RCU Tasks Trace ref scale testing.
251 static void rcu_trace_ref_scale_read_section(const int nloops)
255 for (i = nloops; i >= 0; i--) {
256 rcu_read_lock_trace();
257 rcu_read_unlock_trace();
261 static void rcu_trace_ref_scale_delay_section(const int nloops, const int udl, const int ndl)
265 for (i = nloops; i >= 0; i--) {
266 rcu_read_lock_trace();
268 rcu_read_unlock_trace();
272 static struct ref_scale_ops rcu_trace_ops = {
273 .init = rcu_sync_scale_init,
274 .readsection = rcu_trace_ref_scale_read_section,
275 .delaysection = rcu_trace_ref_scale_delay_section,
279 #define RCU_TRACE_OPS &rcu_trace_ops,
281 #else // #ifdef CONFIG_TASKS_TRACE_RCU
283 #define RCU_TRACE_OPS
285 #endif // #else // #ifdef CONFIG_TASKS_TRACE_RCU
287 // Definitions for reference count
288 static atomic_t refcnt;
290 static void ref_refcnt_section(const int nloops)
294 for (i = nloops; i >= 0; i--) {
300 static void ref_refcnt_delay_section(const int nloops, const int udl, const int ndl)
304 for (i = nloops; i >= 0; i--) {
311 static struct ref_scale_ops refcnt_ops = {
312 .init = rcu_sync_scale_init,
313 .readsection = ref_refcnt_section,
314 .delaysection = ref_refcnt_delay_section,
318 // Definitions for rwlock
319 static rwlock_t test_rwlock;
321 static bool ref_rwlock_init(void)
323 rwlock_init(&test_rwlock);
327 static void ref_rwlock_section(const int nloops)
331 for (i = nloops; i >= 0; i--) {
332 read_lock(&test_rwlock);
333 read_unlock(&test_rwlock);
337 static void ref_rwlock_delay_section(const int nloops, const int udl, const int ndl)
341 for (i = nloops; i >= 0; i--) {
342 read_lock(&test_rwlock);
344 read_unlock(&test_rwlock);
348 static struct ref_scale_ops rwlock_ops = {
349 .init = ref_rwlock_init,
350 .readsection = ref_rwlock_section,
351 .delaysection = ref_rwlock_delay_section,
355 // Definitions for rwsem
356 static struct rw_semaphore test_rwsem;
358 static bool ref_rwsem_init(void)
360 init_rwsem(&test_rwsem);
364 static void ref_rwsem_section(const int nloops)
368 for (i = nloops; i >= 0; i--) {
369 down_read(&test_rwsem);
370 up_read(&test_rwsem);
374 static void ref_rwsem_delay_section(const int nloops, const int udl, const int ndl)
378 for (i = nloops; i >= 0; i--) {
379 down_read(&test_rwsem);
381 up_read(&test_rwsem);
385 static struct ref_scale_ops rwsem_ops = {
386 .init = ref_rwsem_init,
387 .readsection = ref_rwsem_section,
388 .delaysection = ref_rwsem_delay_section,
392 // Definitions for global spinlock
393 static DEFINE_RAW_SPINLOCK(test_lock);
395 static void ref_lock_section(const int nloops)
400 for (i = nloops; i >= 0; i--) {
401 raw_spin_lock(&test_lock);
402 raw_spin_unlock(&test_lock);
407 static void ref_lock_delay_section(const int nloops, const int udl, const int ndl)
412 for (i = nloops; i >= 0; i--) {
413 raw_spin_lock(&test_lock);
415 raw_spin_unlock(&test_lock);
420 static struct ref_scale_ops lock_ops = {
421 .readsection = ref_lock_section,
422 .delaysection = ref_lock_delay_section,
426 // Definitions for global irq-save spinlock
428 static void ref_lock_irq_section(const int nloops)
434 for (i = nloops; i >= 0; i--) {
435 raw_spin_lock_irqsave(&test_lock, flags);
436 raw_spin_unlock_irqrestore(&test_lock, flags);
441 static void ref_lock_irq_delay_section(const int nloops, const int udl, const int ndl)
447 for (i = nloops; i >= 0; i--) {
448 raw_spin_lock_irqsave(&test_lock, flags);
450 raw_spin_unlock_irqrestore(&test_lock, flags);
455 static struct ref_scale_ops lock_irq_ops = {
456 .readsection = ref_lock_irq_section,
457 .delaysection = ref_lock_irq_delay_section,
461 // Definitions acquire-release.
462 static DEFINE_PER_CPU(unsigned long, test_acqrel);
464 static void ref_acqrel_section(const int nloops)
470 for (i = nloops; i >= 0; i--) {
471 x = smp_load_acquire(this_cpu_ptr(&test_acqrel));
472 smp_store_release(this_cpu_ptr(&test_acqrel), x + 1);
477 static void ref_acqrel_delay_section(const int nloops, const int udl, const int ndl)
483 for (i = nloops; i >= 0; i--) {
484 x = smp_load_acquire(this_cpu_ptr(&test_acqrel));
486 smp_store_release(this_cpu_ptr(&test_acqrel), x + 1);
491 static struct ref_scale_ops acqrel_ops = {
492 .readsection = ref_acqrel_section,
493 .delaysection = ref_acqrel_delay_section,
497 static volatile u64 stopopts;
499 static void ref_clock_section(const int nloops)
505 for (i = nloops; i >= 0; i--)
506 x += ktime_get_real_fast_ns();
511 static void ref_clock_delay_section(const int nloops, const int udl, const int ndl)
517 for (i = nloops; i >= 0; i--) {
518 x += ktime_get_real_fast_ns();
525 static struct ref_scale_ops clock_ops = {
526 .readsection = ref_clock_section,
527 .delaysection = ref_clock_delay_section,
531 static void ref_jiffies_section(const int nloops)
537 for (i = nloops; i >= 0; i--)
543 static void ref_jiffies_delay_section(const int nloops, const int udl, const int ndl)
549 for (i = nloops; i >= 0; i--) {
557 static struct ref_scale_ops jiffies_ops = {
558 .readsection = ref_jiffies_section,
559 .delaysection = ref_jiffies_delay_section,
563 ////////////////////////////////////////////////////////////////////////
565 // Methods leveraging SLAB_TYPESAFE_BY_RCU.
568 // Item to look up in a typesafe manner. Array of pointers to these.
569 struct refscale_typesafe {
570 atomic_t rts_refctr; // Used by all flavors
572 seqlock_t rts_seqlock;
577 static struct kmem_cache *typesafe_kmem_cachep;
578 static struct refscale_typesafe **rtsarray;
579 static long rtsarray_size;
580 static DEFINE_TORTURE_RANDOM_PERCPU(refscale_rand);
581 static bool (*rts_acquire)(struct refscale_typesafe *rtsp, unsigned int *start);
582 static bool (*rts_release)(struct refscale_typesafe *rtsp, unsigned int start);
584 // Conditionally acquire an explicit in-structure reference count.
585 static bool typesafe_ref_acquire(struct refscale_typesafe *rtsp, unsigned int *start)
587 return atomic_inc_not_zero(&rtsp->rts_refctr);
590 // Unconditionally release an explicit in-structure reference count.
591 static bool typesafe_ref_release(struct refscale_typesafe *rtsp, unsigned int start)
593 if (!atomic_dec_return(&rtsp->rts_refctr)) {
594 WRITE_ONCE(rtsp->a, rtsp->a + 1);
595 kmem_cache_free(typesafe_kmem_cachep, rtsp);
600 // Unconditionally acquire an explicit in-structure spinlock.
601 static bool typesafe_lock_acquire(struct refscale_typesafe *rtsp, unsigned int *start)
603 spin_lock(&rtsp->rts_lock);
607 // Unconditionally release an explicit in-structure spinlock.
608 static bool typesafe_lock_release(struct refscale_typesafe *rtsp, unsigned int start)
610 spin_unlock(&rtsp->rts_lock);
614 // Unconditionally acquire an explicit in-structure sequence lock.
615 static bool typesafe_seqlock_acquire(struct refscale_typesafe *rtsp, unsigned int *start)
617 *start = read_seqbegin(&rtsp->rts_seqlock);
621 // Conditionally release an explicit in-structure sequence lock. Return
622 // true if this release was successful, that is, if no retry is required.
623 static bool typesafe_seqlock_release(struct refscale_typesafe *rtsp, unsigned int start)
625 return !read_seqretry(&rtsp->rts_seqlock, start);
628 // Do a read-side critical section with the specified delay in
629 // microseconds and nanoseconds inserted so as to increase probability
631 static void typesafe_delay_section(const int nloops, const int udl, const int ndl)
637 struct refscale_typesafe *rtsp;
640 for (i = nloops; i >= 0; i--) {
642 idx = torture_random(this_cpu_ptr(&refscale_rand)) % rtsarray_size;
646 rtsp = rcu_dereference(rtsarray[idx]);
647 a = READ_ONCE(rtsp->a);
648 if (!rts_acquire(rtsp, &start)) {
652 if (a != READ_ONCE(rtsp->a)) {
653 (void)rts_release(rtsp, start);
658 // Remember, seqlock read-side release can fail.
659 if (!rts_release(rtsp, start)) {
663 b = READ_ONCE(rtsp->a);
664 WARN_ONCE(a != b, "Re-read of ->a changed from %u to %u.\n", a, b);
667 WARN_ON_ONCE(a * a != b);
671 // Because the acquisition and release methods are expensive, there
672 // is no point in optimizing away the un_delay() function's two checks.
673 // Thus simply define typesafe_read_section() as a simple wrapper around
674 // typesafe_delay_section().
675 static void typesafe_read_section(const int nloops)
677 typesafe_delay_section(nloops, 0, 0);
680 // Allocate and initialize one refscale_typesafe structure.
681 static struct refscale_typesafe *typesafe_alloc_one(void)
683 struct refscale_typesafe *rtsp;
685 rtsp = kmem_cache_alloc(typesafe_kmem_cachep, GFP_KERNEL);
688 atomic_set(&rtsp->rts_refctr, 1);
689 WRITE_ONCE(rtsp->a, rtsp->a + 1);
690 WRITE_ONCE(rtsp->b, rtsp->a * rtsp->a);
694 // Slab-allocator constructor for refscale_typesafe structures created
695 // out of a new slab of system memory.
696 static void refscale_typesafe_ctor(void *rtsp_in)
698 struct refscale_typesafe *rtsp = rtsp_in;
700 spin_lock_init(&rtsp->rts_lock);
701 seqlock_init(&rtsp->rts_seqlock);
703 rtsp->a = torture_random(this_cpu_ptr(&refscale_rand));
707 static struct ref_scale_ops typesafe_ref_ops;
708 static struct ref_scale_ops typesafe_lock_ops;
709 static struct ref_scale_ops typesafe_seqlock_ops;
711 // Initialize for a typesafe test.
712 static bool typesafe_init(void)
715 long si = lookup_instances;
717 typesafe_kmem_cachep = kmem_cache_create("refscale_typesafe",
718 sizeof(struct refscale_typesafe), sizeof(void *),
719 SLAB_TYPESAFE_BY_RCU, refscale_typesafe_ctor);
720 if (!typesafe_kmem_cachep)
723 si = -si * nr_cpu_ids;
727 rtsarray = kcalloc(si, sizeof(*rtsarray), GFP_KERNEL);
730 for (idx = 0; idx < rtsarray_size; idx++) {
731 rtsarray[idx] = typesafe_alloc_one();
735 if (cur_ops == &typesafe_ref_ops) {
736 rts_acquire = typesafe_ref_acquire;
737 rts_release = typesafe_ref_release;
738 } else if (cur_ops == &typesafe_lock_ops) {
739 rts_acquire = typesafe_lock_acquire;
740 rts_release = typesafe_lock_release;
741 } else if (cur_ops == &typesafe_seqlock_ops) {
742 rts_acquire = typesafe_seqlock_acquire;
743 rts_release = typesafe_seqlock_release;
751 // Clean up after a typesafe test.
752 static void typesafe_cleanup(void)
757 for (idx = 0; idx < rtsarray_size; idx++)
758 kmem_cache_free(typesafe_kmem_cachep, rtsarray[idx]);
763 kmem_cache_destroy(typesafe_kmem_cachep);
764 typesafe_kmem_cachep = NULL;
769 // The typesafe_init() function distinguishes these structures by address.
770 static struct ref_scale_ops typesafe_ref_ops = {
771 .init = typesafe_init,
772 .cleanup = typesafe_cleanup,
773 .readsection = typesafe_read_section,
774 .delaysection = typesafe_delay_section,
775 .name = "typesafe_ref"
778 static struct ref_scale_ops typesafe_lock_ops = {
779 .init = typesafe_init,
780 .cleanup = typesafe_cleanup,
781 .readsection = typesafe_read_section,
782 .delaysection = typesafe_delay_section,
783 .name = "typesafe_lock"
786 static struct ref_scale_ops typesafe_seqlock_ops = {
787 .init = typesafe_init,
788 .cleanup = typesafe_cleanup,
789 .readsection = typesafe_read_section,
790 .delaysection = typesafe_delay_section,
791 .name = "typesafe_seqlock"
794 static void rcu_scale_one_reader(void)
797 cur_ops->readsection(loops);
799 cur_ops->delaysection(loops, readdelay / 1000, readdelay % 1000);
802 // Reader kthread. Repeatedly does empty RCU read-side
803 // critical section, minimizing update-side interference.
805 ref_scale_reader(void *arg)
809 struct reader_task *rt = &(reader_tasks[me]);
813 VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: task started", me);
814 WARN_ON_ONCE(set_cpus_allowed_ptr(current, cpumask_of(me % nr_cpu_ids)));
815 set_user_nice(current, MAX_NICE);
818 schedule_timeout_interruptible(holdoff * HZ);
820 VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: waiting to start next experiment on cpu %d", me, raw_smp_processor_id());
822 // Wait for signal that this reader can start.
823 wait_event(rt->wq, (atomic_read(&nreaders_exp) && smp_load_acquire(&rt->start_reader)) ||
824 torture_must_stop());
826 if (torture_must_stop())
829 // Make sure that the CPU is affinitized appropriately during testing.
830 WARN_ON_ONCE(raw_smp_processor_id() != me);
832 WRITE_ONCE(rt->start_reader, 0);
833 if (!atomic_dec_return(&n_started))
834 while (atomic_read_acquire(&n_started))
837 VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: experiment %d started", me, exp_idx);
840 // To reduce noise, do an initial cache-warming invocation, check
841 // in, and then keep warming until everyone has checked in.
842 rcu_scale_one_reader();
843 if (!atomic_dec_return(&n_warmedup))
844 while (atomic_read_acquire(&n_warmedup))
845 rcu_scale_one_reader();
846 // Also keep interrupts disabled. This also has the effect
847 // of preventing entries into slow path for rcu_read_unlock().
848 local_irq_save(flags);
849 start = ktime_get_mono_fast_ns();
851 rcu_scale_one_reader();
853 duration = ktime_get_mono_fast_ns() - start;
854 local_irq_restore(flags);
856 rt->last_duration_ns = WARN_ON_ONCE(duration < 0) ? 0 : duration;
857 // To reduce runtime-skew noise, do maintain-load invocations until
859 if (!atomic_dec_return(&n_cooleddown))
860 while (atomic_read_acquire(&n_cooleddown))
861 rcu_scale_one_reader();
863 if (atomic_dec_and_test(&nreaders_exp))
866 VERBOSE_SCALEOUT_BATCH("ref_scale_reader %ld: experiment %d ended, (readers remaining=%d)",
867 me, exp_idx, atomic_read(&nreaders_exp));
869 if (!torture_must_stop())
872 torture_kthread_stopping("ref_scale_reader");
876 static void reset_readers(void)
879 struct reader_task *rt;
881 for (i = 0; i < nreaders; i++) {
882 rt = &(reader_tasks[i]);
884 rt->last_duration_ns = 0;
888 // Print the results of each reader and return the sum of all their durations.
889 static u64 process_durations(int n)
892 struct reader_task *rt;
897 buf = kmalloc(800 + 64, GFP_KERNEL);
901 sprintf(buf, "Experiment #%d (Format: <THREAD-NUM>:<Total loop time in ns>)",
904 for (i = 0; i < n && !torture_must_stop(); i++) {
905 rt = &(reader_tasks[i]);
906 sprintf(buf1, "%d: %llu\t", i, rt->last_duration_ns);
910 if (strlen(buf) >= 800) {
916 sum += rt->last_duration_ns;
918 pr_alert("%s\n", buf);
924 // The main_func is the main orchestrator, it performs a bunch of
925 // experiments. For every experiment, it orders all the readers
926 // involved to start and waits for them to finish the experiment. It
927 // then reads their timestamps and starts the next experiment. Each
928 // experiment progresses from 1 concurrent reader to N of them at which
929 // point all the timestamps are printed.
930 static int main_func(void *arg)
937 set_cpus_allowed_ptr(current, cpumask_of(nreaders % nr_cpu_ids));
938 set_user_nice(current, MAX_NICE);
940 VERBOSE_SCALEOUT("main_func task started");
941 result_avg = kzalloc(nruns * sizeof(*result_avg), GFP_KERNEL);
942 buf = kzalloc(800 + 64, GFP_KERNEL);
943 if (!result_avg || !buf) {
944 SCALEOUT_ERRSTRING("out of memory");
948 schedule_timeout_interruptible(holdoff * HZ);
950 // Wait for all threads to start.
952 while (atomic_read(&n_init) < nreaders + 1)
953 schedule_timeout_uninterruptible(1);
955 // Start exp readers up per experiment
956 for (exp = 0; exp < nruns && !torture_must_stop(); exp++) {
957 if (torture_must_stop())
961 atomic_set(&nreaders_exp, nreaders);
962 atomic_set(&n_started, nreaders);
963 atomic_set(&n_warmedup, nreaders);
964 atomic_set(&n_cooleddown, nreaders);
968 for (r = 0; r < nreaders; r++) {
969 smp_store_release(&reader_tasks[r].start_reader, 1);
970 wake_up(&reader_tasks[r].wq);
973 VERBOSE_SCALEOUT("main_func: experiment started, waiting for %d readers",
977 !atomic_read(&nreaders_exp) || torture_must_stop());
979 VERBOSE_SCALEOUT("main_func: experiment ended");
981 if (torture_must_stop())
984 result_avg[exp] = div_u64(1000 * process_durations(nreaders), nreaders * loops);
987 // Print the average of all experiments
988 SCALEOUT("END OF TEST. Calculating average duration per loop (nanoseconds)...\n");
990 pr_alert("Runs\tTime(ns)\n");
991 for (exp = 0; exp < nruns; exp++) {
995 avg = div_u64_rem(result_avg[exp], 1000, &rem);
996 sprintf(buf1, "%d\t%llu.%03u\n", exp + 1, avg, rem);
998 if (strlen(buf) >= 800) {
1004 pr_alert("%s", buf);
1007 // This will shutdown everything including us.
1010 wake_up(&shutdown_wq);
1013 // Wait for torture to stop us
1014 while (!torture_must_stop())
1015 schedule_timeout_uninterruptible(1);
1018 torture_kthread_stopping("main_func");
1025 ref_scale_print_module_parms(struct ref_scale_ops *cur_ops, const char *tag)
1027 pr_alert("%s" SCALE_FLAG
1028 "--- %s: verbose=%d shutdown=%d holdoff=%d loops=%ld nreaders=%d nruns=%d readdelay=%d\n", scale_type, tag,
1029 verbose, shutdown, holdoff, loops, nreaders, nruns, readdelay);
1033 ref_scale_cleanup(void)
1037 if (torture_cleanup_begin())
1041 torture_cleanup_end();
1046 for (i = 0; i < nreaders; i++)
1047 torture_stop_kthread("ref_scale_reader",
1048 reader_tasks[i].task);
1050 kfree(reader_tasks);
1052 torture_stop_kthread("main_task", main_task);
1055 // Do scale-type-specific cleanup operations.
1056 if (cur_ops->cleanup != NULL)
1059 torture_cleanup_end();
1062 // Shutdown kthread. Just waits to be awakened, then shuts down system.
1064 ref_scale_shutdown(void *arg)
1066 wait_event_idle(shutdown_wq, shutdown_start);
1068 smp_mb(); // Wake before output.
1069 ref_scale_cleanup();
1076 ref_scale_init(void)
1080 static struct ref_scale_ops *scale_ops[] = {
1081 &rcu_ops, &srcu_ops, RCU_TRACE_OPS RCU_TASKS_OPS &refcnt_ops, &rwlock_ops,
1082 &rwsem_ops, &lock_ops, &lock_irq_ops, &acqrel_ops, &clock_ops, &jiffies_ops,
1083 &typesafe_ref_ops, &typesafe_lock_ops, &typesafe_seqlock_ops,
1086 if (!torture_init_begin(scale_type, verbose))
1089 for (i = 0; i < ARRAY_SIZE(scale_ops); i++) {
1090 cur_ops = scale_ops[i];
1091 if (strcmp(scale_type, cur_ops->name) == 0)
1094 if (i == ARRAY_SIZE(scale_ops)) {
1095 pr_alert("rcu-scale: invalid scale type: \"%s\"\n", scale_type);
1096 pr_alert("rcu-scale types:");
1097 for (i = 0; i < ARRAY_SIZE(scale_ops); i++)
1098 pr_cont(" %s", scale_ops[i]->name);
1105 if (!cur_ops->init()) {
1106 firsterr = -EUCLEAN;
1110 ref_scale_print_module_parms(cur_ops, "Start of test");
1114 init_waitqueue_head(&shutdown_wq);
1115 firsterr = torture_create_kthread(ref_scale_shutdown, NULL,
1117 if (torture_init_error(firsterr))
1119 schedule_timeout_uninterruptible(1);
1122 // Reader tasks (default to ~75% of online CPUs).
1124 nreaders = (num_online_cpus() >> 1) + (num_online_cpus() >> 2);
1125 if (WARN_ONCE(loops <= 0, "%s: loops = %ld, adjusted to 1\n", __func__, loops))
1127 if (WARN_ONCE(nreaders <= 0, "%s: nreaders = %d, adjusted to 1\n", __func__, nreaders))
1129 if (WARN_ONCE(nruns <= 0, "%s: nruns = %d, adjusted to 1\n", __func__, nruns))
1131 reader_tasks = kcalloc(nreaders, sizeof(reader_tasks[0]),
1133 if (!reader_tasks) {
1134 SCALEOUT_ERRSTRING("out of memory");
1139 VERBOSE_SCALEOUT("Starting %d reader threads", nreaders);
1141 for (i = 0; i < nreaders; i++) {
1142 init_waitqueue_head(&reader_tasks[i].wq);
1143 firsterr = torture_create_kthread(ref_scale_reader, (void *)i,
1144 reader_tasks[i].task);
1145 if (torture_init_error(firsterr))
1150 init_waitqueue_head(&main_wq);
1151 firsterr = torture_create_kthread(main_func, NULL, main_task);
1152 if (torture_init_error(firsterr))
1160 ref_scale_cleanup();
1162 WARN_ON(!IS_MODULE(CONFIG_RCU_REF_SCALE_TEST));
1168 module_init(ref_scale_init);
1169 module_exit(ref_scale_cleanup);