2 * ring buffer tester and benchmark
4 * Copyright (C) 2009 Steven Rostedt <srostedt@redhat.com>
6 #include <linux/ring_buffer.h>
7 #include <linux/completion.h>
8 #include <linux/kthread.h>
9 #include <linux/module.h>
10 #include <linux/time.h>
11 #include <asm/local.h>
19 /* run time and sleep time in seconds */
23 /* number of events for writer to wake up the reader */
24 static int wakeup_interval
= 100;
26 static int reader_finish
;
27 static struct completion read_start
;
28 static struct completion read_done
;
30 static struct ring_buffer
*buffer
;
31 static struct task_struct
*producer
;
32 static struct task_struct
*consumer
;
33 static unsigned long read
;
35 static int disable_reader
;
36 module_param(disable_reader
, uint
, 0644);
37 MODULE_PARM_DESC(disable_reader
, "only run producer");
39 static int write_iteration
= 50;
40 module_param(write_iteration
, uint
, 0644);
41 MODULE_PARM_DESC(write_iteration
, "# of writes between timestamp readings");
43 static int producer_nice
= 19;
44 static int consumer_nice
= 19;
46 static int producer_fifo
= -1;
47 static int consumer_fifo
= -1;
49 module_param(producer_nice
, uint
, 0644);
50 MODULE_PARM_DESC(producer_nice
, "nice prio for producer");
52 module_param(consumer_nice
, uint
, 0644);
53 MODULE_PARM_DESC(consumer_nice
, "nice prio for consumer");
55 module_param(producer_fifo
, uint
, 0644);
56 MODULE_PARM_DESC(producer_fifo
, "fifo prio for producer");
58 module_param(consumer_fifo
, uint
, 0644);
59 MODULE_PARM_DESC(consumer_fifo
, "fifo prio for consumer");
61 static int read_events
;
78 static enum event_status
read_event(int cpu
)
80 struct ring_buffer_event
*event
;
84 event
= ring_buffer_consume(buffer
, cpu
, &ts
);
88 entry
= ring_buffer_event_data(event
);
98 static enum event_status
read_page(int cpu
)
100 struct ring_buffer_event
*event
;
101 struct rb_page
*rpage
;
102 unsigned long commit
;
109 bpage
= ring_buffer_alloc_read_page(buffer
);
111 return EVENT_DROPPED
;
113 ret
= ring_buffer_read_page(buffer
, &bpage
, PAGE_SIZE
, cpu
, 1);
116 commit
= local_read(&rpage
->commit
);
117 for (i
= 0; i
< commit
&& !kill_test
; i
+= inc
) {
119 if (i
>= (PAGE_SIZE
- offsetof(struct rb_page
, data
))) {
125 event
= (void *)&rpage
->data
[i
];
126 switch (event
->type_len
) {
127 case RINGBUF_TYPE_PADDING
:
128 /* failed writes may be discarded events */
129 if (!event
->time_delta
)
131 inc
= event
->array
[0] + 4;
133 case RINGBUF_TYPE_TIME_EXTEND
:
137 entry
= ring_buffer_event_data(event
);
143 if (!event
->array
[0]) {
147 inc
= event
->array
[0] + 4;
150 entry
= ring_buffer_event_data(event
);
156 inc
= ((event
->type_len
+ 1) * 4);
167 ring_buffer_free_read_page(buffer
, bpage
);
170 return EVENT_DROPPED
;
174 static void ring_buffer_consumer(void)
176 /* toggle between reading pages and events */
180 while (!reader_finish
&& !kill_test
) {
187 for_each_online_cpu(cpu
) {
188 enum event_status stat
;
191 stat
= read_event(cpu
);
193 stat
= read_page(cpu
);
197 if (stat
== EVENT_FOUND
)
200 } while (found
&& !kill_test
);
202 set_current_state(TASK_INTERRUPTIBLE
);
207 __set_current_state(TASK_RUNNING
);
210 complete(&read_done
);
213 static void ring_buffer_producer(void)
215 struct timeval start_tv
;
216 struct timeval end_tv
;
217 unsigned long long time
;
218 unsigned long long entries
;
219 unsigned long long overruns
;
220 unsigned long missed
= 0;
221 unsigned long hit
= 0;
226 * Hammer the buffer for 10 secs (this may
227 * make the system stall)
229 trace_printk("Starting ring buffer hammer\n");
230 do_gettimeofday(&start_tv
);
232 struct ring_buffer_event
*event
;
236 for (i
= 0; i
< write_iteration
; i
++) {
237 event
= ring_buffer_lock_reserve(buffer
, 10);
242 entry
= ring_buffer_event_data(event
);
243 *entry
= smp_processor_id();
244 ring_buffer_unlock_commit(buffer
, event
);
247 do_gettimeofday(&end_tv
);
250 if (consumer
&& !(cnt
% wakeup_interval
))
251 wake_up_process(consumer
);
253 #ifndef CONFIG_PREEMPT
255 * If we are a non preempt kernel, the 10 second run will
256 * stop everything while it runs. Instead, we will call
257 * cond_resched and also add any time that was lost by a
260 * Do a cond resched at the same frequency we would wake up
263 if (cnt
% wakeup_interval
)
267 } while (end_tv
.tv_sec
< (start_tv
.tv_sec
+ RUN_TIME
) && !kill_test
);
268 trace_printk("End ring buffer hammer\n");
271 /* Init both completions here to avoid races */
272 init_completion(&read_start
);
273 init_completion(&read_done
);
274 /* the completions must be visible before the finish var */
277 /* finish var visible before waking up the consumer */
279 wake_up_process(consumer
);
280 wait_for_completion(&read_done
);
283 time
= end_tv
.tv_sec
- start_tv
.tv_sec
;
284 time
*= USEC_PER_SEC
;
285 time
+= (long long)((long)end_tv
.tv_usec
- (long)start_tv
.tv_usec
);
287 entries
= ring_buffer_entries(buffer
);
288 overruns
= ring_buffer_overruns(buffer
);
291 trace_printk("ERROR!\n");
293 if (!disable_reader
) {
294 if (consumer_fifo
< 0)
295 trace_printk("Running Consumer at nice: %d\n",
298 trace_printk("Running Consumer at SCHED_FIFO %d\n",
301 if (producer_fifo
< 0)
302 trace_printk("Running Producer at nice: %d\n",
305 trace_printk("Running Producer at SCHED_FIFO %d\n",
308 /* Let the user know that the test is running at low priority */
309 if (producer_fifo
< 0 && consumer_fifo
< 0 &&
310 producer_nice
== 19 && consumer_nice
== 19)
311 trace_printk("WARNING!!! This test is running at lowest priority.\n");
313 trace_printk("Time: %lld (usecs)\n", time
);
314 trace_printk("Overruns: %lld\n", overruns
);
316 trace_printk("Read: (reader disabled)\n");
318 trace_printk("Read: %ld (by %s)\n", read
,
319 read_events
? "events" : "pages");
320 trace_printk("Entries: %lld\n", entries
);
321 trace_printk("Total: %lld\n", entries
+ overruns
+ read
);
322 trace_printk("Missed: %ld\n", missed
);
323 trace_printk("Hit: %ld\n", hit
);
325 /* Convert time from usecs to millisecs */
326 do_div(time
, USEC_PER_MSEC
);
330 trace_printk("TIME IS ZERO??\n");
332 trace_printk("Entries per millisec: %ld\n", hit
);
335 /* Calculate the average time in nanosecs */
336 avg
= NSEC_PER_MSEC
/ hit
;
337 trace_printk("%ld ns per entry\n", avg
);
342 missed
/= (long)time
;
344 trace_printk("Total iterations per millisec: %ld\n",
347 /* it is possible that hit + missed will overflow and be zero */
348 if (!(hit
+ missed
)) {
349 trace_printk("hit + missed overflowed and totalled zero!\n");
350 hit
--; /* make it non zero */
353 /* Caculate the average time in nanosecs */
354 avg
= NSEC_PER_MSEC
/ (hit
+ missed
);
355 trace_printk("%ld ns per entry\n", avg
);
359 static void wait_to_die(void)
361 set_current_state(TASK_INTERRUPTIBLE
);
362 while (!kthread_should_stop()) {
364 set_current_state(TASK_INTERRUPTIBLE
);
366 __set_current_state(TASK_RUNNING
);
369 static int ring_buffer_consumer_thread(void *arg
)
371 while (!kthread_should_stop() && !kill_test
) {
372 complete(&read_start
);
374 ring_buffer_consumer();
376 set_current_state(TASK_INTERRUPTIBLE
);
377 if (kthread_should_stop() || kill_test
)
381 __set_current_state(TASK_RUNNING
);
383 __set_current_state(TASK_RUNNING
);
391 static int ring_buffer_producer_thread(void *arg
)
393 init_completion(&read_start
);
395 while (!kthread_should_stop() && !kill_test
) {
396 ring_buffer_reset(buffer
);
400 wake_up_process(consumer
);
401 wait_for_completion(&read_start
);
404 ring_buffer_producer();
406 trace_printk("Sleeping for 10 secs\n");
407 set_current_state(TASK_INTERRUPTIBLE
);
408 schedule_timeout(HZ
* SLEEP_TIME
);
409 __set_current_state(TASK_RUNNING
);
418 static int __init
ring_buffer_benchmark_init(void)
422 /* make a one meg buffer in overwite mode */
423 buffer
= ring_buffer_alloc(1000000, RB_FL_OVERWRITE
);
427 if (!disable_reader
) {
428 consumer
= kthread_create(ring_buffer_consumer_thread
,
429 NULL
, "rb_consumer");
430 ret
= PTR_ERR(consumer
);
431 if (IS_ERR(consumer
))
435 producer
= kthread_run(ring_buffer_producer_thread
,
436 NULL
, "rb_producer");
437 ret
= PTR_ERR(producer
);
439 if (IS_ERR(producer
))
443 * Run them as low-prio background tasks by default:
445 if (!disable_reader
) {
446 if (consumer_fifo
>= 0) {
447 struct sched_param param
= {
448 .sched_priority
= consumer_fifo
450 sched_setscheduler(consumer
, SCHED_FIFO
, ¶m
);
452 set_user_nice(consumer
, consumer_nice
);
455 if (producer_fifo
>= 0) {
456 struct sched_param param
= {
457 .sched_priority
= consumer_fifo
459 sched_setscheduler(producer
, SCHED_FIFO
, ¶m
);
461 set_user_nice(producer
, producer_nice
);
467 kthread_stop(consumer
);
470 ring_buffer_free(buffer
);
474 static void __exit
ring_buffer_benchmark_exit(void)
476 kthread_stop(producer
);
478 kthread_stop(consumer
);
479 ring_buffer_free(buffer
);
482 module_init(ring_buffer_benchmark_init
);
483 module_exit(ring_buffer_benchmark_exit
);
485 MODULE_AUTHOR("Steven Rostedt");
486 MODULE_DESCRIPTION("ring_buffer_benchmark");
487 MODULE_LICENSE("GPL");