refscale.c 21 KB

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