ring_buffer_benchmark.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * ring buffer tester and benchmark
  3. *
  4. * Copyright (C) 2009 Steven Rostedt <srostedt@redhat.com>
  5. */
  6. #include <linux/ring_buffer.h>
  7. #include <linux/completion.h>
  8. #include <linux/kthread.h>
  9. #include <linux/module.h>
  10. #include <linux/ktime.h>
  11. #include <asm/local.h>
  12. struct rb_page {
  13. u64 ts;
  14. local_t commit;
  15. char data[4080];
  16. };
  17. /* run time and sleep time in seconds */
  18. #define RUN_TIME 10ULL
  19. #define SLEEP_TIME 10
  20. /* number of events for writer to wake up the reader */
  21. static int wakeup_interval = 100;
  22. static int reader_finish;
  23. static DECLARE_COMPLETION(read_start);
  24. static DECLARE_COMPLETION(read_done);
  25. static struct ring_buffer *buffer;
  26. static struct task_struct *producer;
  27. static struct task_struct *consumer;
  28. static unsigned long read;
  29. static unsigned int disable_reader;
  30. module_param(disable_reader, uint, 0644);
  31. MODULE_PARM_DESC(disable_reader, "only run producer");
  32. static unsigned int write_iteration = 50;
  33. module_param(write_iteration, uint, 0644);
  34. MODULE_PARM_DESC(write_iteration, "# of writes between timestamp readings");
  35. static int producer_nice = MAX_NICE;
  36. static int consumer_nice = MAX_NICE;
  37. static int producer_fifo = -1;
  38. static int consumer_fifo = -1;
  39. module_param(producer_nice, int, 0644);
  40. MODULE_PARM_DESC(producer_nice, "nice prio for producer");
  41. module_param(consumer_nice, int, 0644);
  42. MODULE_PARM_DESC(consumer_nice, "nice prio for consumer");
  43. module_param(producer_fifo, int, 0644);
  44. MODULE_PARM_DESC(producer_fifo, "fifo prio for producer");
  45. module_param(consumer_fifo, int, 0644);
  46. MODULE_PARM_DESC(consumer_fifo, "fifo prio for consumer");
  47. static int read_events;
  48. static int test_error;
  49. #define TEST_ERROR() \
  50. do { \
  51. if (!test_error) { \
  52. test_error = 1; \
  53. WARN_ON(1); \
  54. } \
  55. } while (0)
  56. enum event_status {
  57. EVENT_FOUND,
  58. EVENT_DROPPED,
  59. };
  60. static bool break_test(void)
  61. {
  62. return test_error || kthread_should_stop();
  63. }
  64. static enum event_status read_event(int cpu)
  65. {
  66. struct ring_buffer_event *event;
  67. int *entry;
  68. u64 ts;
  69. event = ring_buffer_consume(buffer, cpu, &ts, NULL);
  70. if (!event)
  71. return EVENT_DROPPED;
  72. entry = ring_buffer_event_data(event);
  73. if (*entry != cpu) {
  74. TEST_ERROR();
  75. return EVENT_DROPPED;
  76. }
  77. read++;
  78. return EVENT_FOUND;
  79. }
  80. static enum event_status read_page(int cpu)
  81. {
  82. struct ring_buffer_event *event;
  83. struct rb_page *rpage;
  84. unsigned long commit;
  85. void *bpage;
  86. int *entry;
  87. int ret;
  88. int inc;
  89. int i;
  90. bpage = ring_buffer_alloc_read_page(buffer, cpu);
  91. if (!bpage)
  92. return EVENT_DROPPED;
  93. ret = ring_buffer_read_page(buffer, &bpage, PAGE_SIZE, cpu, 1);
  94. if (ret >= 0) {
  95. rpage = bpage;
  96. /* The commit may have missed event flags set, clear them */
  97. commit = local_read(&rpage->commit) & 0xfffff;
  98. for (i = 0; i < commit && !test_error ; i += inc) {
  99. if (i >= (PAGE_SIZE - offsetof(struct rb_page, data))) {
  100. TEST_ERROR();
  101. break;
  102. }
  103. inc = -1;
  104. event = (void *)&rpage->data[i];
  105. switch (event->type_len) {
  106. case RINGBUF_TYPE_PADDING:
  107. /* failed writes may be discarded events */
  108. if (!event->time_delta)
  109. TEST_ERROR();
  110. inc = event->array[0] + 4;
  111. break;
  112. case RINGBUF_TYPE_TIME_EXTEND:
  113. inc = 8;
  114. break;
  115. case 0:
  116. entry = ring_buffer_event_data(event);
  117. if (*entry != cpu) {
  118. TEST_ERROR();
  119. break;
  120. }
  121. read++;
  122. if (!event->array[0]) {
  123. TEST_ERROR();
  124. break;
  125. }
  126. inc = event->array[0] + 4;
  127. break;
  128. default:
  129. entry = ring_buffer_event_data(event);
  130. if (*entry != cpu) {
  131. TEST_ERROR();
  132. break;
  133. }
  134. read++;
  135. inc = ((event->type_len + 1) * 4);
  136. }
  137. if (test_error)
  138. break;
  139. if (inc <= 0) {
  140. TEST_ERROR();
  141. break;
  142. }
  143. }
  144. }
  145. ring_buffer_free_read_page(buffer, bpage);
  146. if (ret < 0)
  147. return EVENT_DROPPED;
  148. return EVENT_FOUND;
  149. }
  150. static void ring_buffer_consumer(void)
  151. {
  152. /* toggle between reading pages and events */
  153. read_events ^= 1;
  154. read = 0;
  155. /*
  156. * Continue running until the producer specifically asks to stop
  157. * and is ready for the completion.
  158. */
  159. while (!READ_ONCE(reader_finish)) {
  160. int found = 1;
  161. while (found && !test_error) {
  162. int cpu;
  163. found = 0;
  164. for_each_online_cpu(cpu) {
  165. enum event_status stat;
  166. if (read_events)
  167. stat = read_event(cpu);
  168. else
  169. stat = read_page(cpu);
  170. if (test_error)
  171. break;
  172. if (stat == EVENT_FOUND)
  173. found = 1;
  174. }
  175. }
  176. /* Wait till the producer wakes us up when there is more data
  177. * available or when the producer wants us to finish reading.
  178. */
  179. set_current_state(TASK_INTERRUPTIBLE);
  180. if (reader_finish)
  181. break;
  182. schedule();
  183. }
  184. __set_current_state(TASK_RUNNING);
  185. reader_finish = 0;
  186. complete(&read_done);
  187. }
  188. static void ring_buffer_producer(void)
  189. {
  190. ktime_t start_time, end_time, timeout;
  191. unsigned long long time;
  192. unsigned long long entries;
  193. unsigned long long overruns;
  194. unsigned long missed = 0;
  195. unsigned long hit = 0;
  196. unsigned long avg;
  197. int cnt = 0;
  198. /*
  199. * Hammer the buffer for 10 secs (this may
  200. * make the system stall)
  201. */
  202. trace_printk("Starting ring buffer hammer\n");
  203. start_time = ktime_get();
  204. timeout = ktime_add_ns(start_time, RUN_TIME * NSEC_PER_SEC);
  205. do {
  206. struct ring_buffer_event *event;
  207. int *entry;
  208. int i;
  209. for (i = 0; i < write_iteration; i++) {
  210. event = ring_buffer_lock_reserve(buffer, 10);
  211. if (!event) {
  212. missed++;
  213. } else {
  214. hit++;
  215. entry = ring_buffer_event_data(event);
  216. *entry = smp_processor_id();
  217. ring_buffer_unlock_commit(buffer, event);
  218. }
  219. }
  220. end_time = ktime_get();
  221. cnt++;
  222. if (consumer && !(cnt % wakeup_interval))
  223. wake_up_process(consumer);
  224. #ifndef CONFIG_PREEMPT
  225. /*
  226. * If we are a non preempt kernel, the 10 second run will
  227. * stop everything while it runs. Instead, we will call
  228. * cond_resched and also add any time that was lost by a
  229. * rescedule.
  230. *
  231. * Do a cond resched at the same frequency we would wake up
  232. * the reader.
  233. */
  234. if (cnt % wakeup_interval)
  235. cond_resched();
  236. #endif
  237. } while (ktime_before(end_time, timeout) && !break_test());
  238. trace_printk("End ring buffer hammer\n");
  239. if (consumer) {
  240. /* Init both completions here to avoid races */
  241. init_completion(&read_start);
  242. init_completion(&read_done);
  243. /* the completions must be visible before the finish var */
  244. smp_wmb();
  245. reader_finish = 1;
  246. wake_up_process(consumer);
  247. wait_for_completion(&read_done);
  248. }
  249. time = ktime_us_delta(end_time, start_time);
  250. entries = ring_buffer_entries(buffer);
  251. overruns = ring_buffer_overruns(buffer);
  252. if (test_error)
  253. trace_printk("ERROR!\n");
  254. if (!disable_reader) {
  255. if (consumer_fifo < 0)
  256. trace_printk("Running Consumer at nice: %d\n",
  257. consumer_nice);
  258. else
  259. trace_printk("Running Consumer at SCHED_FIFO %d\n",
  260. consumer_fifo);
  261. }
  262. if (producer_fifo < 0)
  263. trace_printk("Running Producer at nice: %d\n",
  264. producer_nice);
  265. else
  266. trace_printk("Running Producer at SCHED_FIFO %d\n",
  267. producer_fifo);
  268. /* Let the user know that the test is running at low priority */
  269. if (producer_fifo < 0 && consumer_fifo < 0 &&
  270. producer_nice == MAX_NICE && consumer_nice == MAX_NICE)
  271. trace_printk("WARNING!!! This test is running at lowest priority.\n");
  272. trace_printk("Time: %lld (usecs)\n", time);
  273. trace_printk("Overruns: %lld\n", overruns);
  274. if (disable_reader)
  275. trace_printk("Read: (reader disabled)\n");
  276. else
  277. trace_printk("Read: %ld (by %s)\n", read,
  278. read_events ? "events" : "pages");
  279. trace_printk("Entries: %lld\n", entries);
  280. trace_printk("Total: %lld\n", entries + overruns + read);
  281. trace_printk("Missed: %ld\n", missed);
  282. trace_printk("Hit: %ld\n", hit);
  283. /* Convert time from usecs to millisecs */
  284. do_div(time, USEC_PER_MSEC);
  285. if (time)
  286. hit /= (long)time;
  287. else
  288. trace_printk("TIME IS ZERO??\n");
  289. trace_printk("Entries per millisec: %ld\n", hit);
  290. if (hit) {
  291. /* Calculate the average time in nanosecs */
  292. avg = NSEC_PER_MSEC / hit;
  293. trace_printk("%ld ns per entry\n", avg);
  294. }
  295. if (missed) {
  296. if (time)
  297. missed /= (long)time;
  298. trace_printk("Total iterations per millisec: %ld\n",
  299. hit + missed);
  300. /* it is possible that hit + missed will overflow and be zero */
  301. if (!(hit + missed)) {
  302. trace_printk("hit + missed overflowed and totalled zero!\n");
  303. hit--; /* make it non zero */
  304. }
  305. /* Caculate the average time in nanosecs */
  306. avg = NSEC_PER_MSEC / (hit + missed);
  307. trace_printk("%ld ns per entry\n", avg);
  308. }
  309. }
  310. static void wait_to_die(void)
  311. {
  312. set_current_state(TASK_INTERRUPTIBLE);
  313. while (!kthread_should_stop()) {
  314. schedule();
  315. set_current_state(TASK_INTERRUPTIBLE);
  316. }
  317. __set_current_state(TASK_RUNNING);
  318. }
  319. static int ring_buffer_consumer_thread(void *arg)
  320. {
  321. while (!break_test()) {
  322. complete(&read_start);
  323. ring_buffer_consumer();
  324. set_current_state(TASK_INTERRUPTIBLE);
  325. if (break_test())
  326. break;
  327. schedule();
  328. }
  329. __set_current_state(TASK_RUNNING);
  330. if (!kthread_should_stop())
  331. wait_to_die();
  332. return 0;
  333. }
  334. static int ring_buffer_producer_thread(void *arg)
  335. {
  336. while (!break_test()) {
  337. ring_buffer_reset(buffer);
  338. if (consumer) {
  339. wake_up_process(consumer);
  340. wait_for_completion(&read_start);
  341. }
  342. ring_buffer_producer();
  343. if (break_test())
  344. goto out_kill;
  345. trace_printk("Sleeping for 10 secs\n");
  346. set_current_state(TASK_INTERRUPTIBLE);
  347. if (break_test())
  348. goto out_kill;
  349. schedule_timeout(HZ * SLEEP_TIME);
  350. }
  351. out_kill:
  352. __set_current_state(TASK_RUNNING);
  353. if (!kthread_should_stop())
  354. wait_to_die();
  355. return 0;
  356. }
  357. static int __init ring_buffer_benchmark_init(void)
  358. {
  359. int ret;
  360. /* make a one meg buffer in overwite mode */
  361. buffer = ring_buffer_alloc(1000000, RB_FL_OVERWRITE);
  362. if (!buffer)
  363. return -ENOMEM;
  364. if (!disable_reader) {
  365. consumer = kthread_create(ring_buffer_consumer_thread,
  366. NULL, "rb_consumer");
  367. ret = PTR_ERR(consumer);
  368. if (IS_ERR(consumer))
  369. goto out_fail;
  370. }
  371. producer = kthread_run(ring_buffer_producer_thread,
  372. NULL, "rb_producer");
  373. ret = PTR_ERR(producer);
  374. if (IS_ERR(producer))
  375. goto out_kill;
  376. /*
  377. * Run them as low-prio background tasks by default:
  378. */
  379. if (!disable_reader) {
  380. if (consumer_fifo >= 0) {
  381. struct sched_param param = {
  382. .sched_priority = consumer_fifo
  383. };
  384. sched_setscheduler(consumer, SCHED_FIFO, &param);
  385. } else
  386. set_user_nice(consumer, consumer_nice);
  387. }
  388. if (producer_fifo >= 0) {
  389. struct sched_param param = {
  390. .sched_priority = producer_fifo
  391. };
  392. sched_setscheduler(producer, SCHED_FIFO, &param);
  393. } else
  394. set_user_nice(producer, producer_nice);
  395. return 0;
  396. out_kill:
  397. if (consumer)
  398. kthread_stop(consumer);
  399. out_fail:
  400. ring_buffer_free(buffer);
  401. return ret;
  402. }
  403. static void __exit ring_buffer_benchmark_exit(void)
  404. {
  405. kthread_stop(producer);
  406. if (consumer)
  407. kthread_stop(consumer);
  408. ring_buffer_free(buffer);
  409. }
  410. module_init(ring_buffer_benchmark_init);
  411. module_exit(ring_buffer_benchmark_exit);
  412. MODULE_AUTHOR("Steven Rostedt");
  413. MODULE_DESCRIPTION("ring_buffer_benchmark");
  414. MODULE_LICENSE("GPL");