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
, NULL
);
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
, cpu
);
111 return EVENT_DROPPED
;
113 ret
= ring_buffer_read_page(buffer
, &bpage
, PAGE_SIZE
, cpu
, 1);
116 /* The commit may have missed event flags set, clear them */
117 commit
= local_read(&rpage
->commit
) & 0xfffff;
118 for (i
= 0; i
< commit
&& !kill_test
; i
+= inc
) {
120 if (i
>= (PAGE_SIZE
- offsetof(struct rb_page
, data
))) {
126 event
= (void *)&rpage
->data
[i
];
127 switch (event
->type_len
) {
128 case RINGBUF_TYPE_PADDING
:
129 /* failed writes may be discarded events */
130 if (!event
->time_delta
)
132 inc
= event
->array
[0] + 4;
134 case RINGBUF_TYPE_TIME_EXTEND
:
138 entry
= ring_buffer_event_data(event
);
144 if (!event
->array
[0]) {
148 inc
= event
->array
[0] + 4;
151 entry
= ring_buffer_event_data(event
);
157 inc
= ((event
->type_len
+ 1) * 4);
168 ring_buffer_free_read_page(buffer
, bpage
);
171 return EVENT_DROPPED
;
175 static void ring_buffer_consumer(void)
177 /* toggle between reading pages and events */
181 while (!reader_finish
&& !kill_test
) {
188 for_each_online_cpu(cpu
) {
189 enum event_status stat
;
192 stat
= read_event(cpu
);
194 stat
= read_page(cpu
);
198 if (stat
== EVENT_FOUND
)
201 } while (found
&& !kill_test
);
203 set_current_state(TASK_INTERRUPTIBLE
);
208 __set_current_state(TASK_RUNNING
);
211 complete(&read_done
);
214 static void ring_buffer_producer(void)
216 struct timeval start_tv
;
217 struct timeval end_tv
;
218 unsigned long long time
;
219 unsigned long long entries
;
220 unsigned long long overruns
;
221 unsigned long missed
= 0;
222 unsigned long hit
= 0;
227 * Hammer the buffer for 10 secs (this may
228 * make the system stall)
230 trace_printk("Starting ring buffer hammer\n");
231 do_gettimeofday(&start_tv
);
233 struct ring_buffer_event
*event
;
237 for (i
= 0; i
< write_iteration
; i
++) {
238 event
= ring_buffer_lock_reserve(buffer
, 10);
243 entry
= ring_buffer_event_data(event
);
244 *entry
= smp_processor_id();
245 ring_buffer_unlock_commit(buffer
, event
);
248 do_gettimeofday(&end_tv
);
251 if (consumer
&& !(cnt
% wakeup_interval
))
252 wake_up_process(consumer
);
254 #ifndef CONFIG_PREEMPT
256 * If we are a non preempt kernel, the 10 second run will
257 * stop everything while it runs. Instead, we will call
258 * cond_resched and also add any time that was lost by a
261 * Do a cond resched at the same frequency we would wake up
264 if (cnt
% wakeup_interval
)
268 } while (end_tv
.tv_sec
< (start_tv
.tv_sec
+ RUN_TIME
) && !kill_test
);
269 trace_printk("End ring buffer hammer\n");
272 /* Init both completions here to avoid races */
273 init_completion(&read_start
);
274 init_completion(&read_done
);
275 /* the completions must be visible before the finish var */
278 /* finish var visible before waking up the consumer */
280 wake_up_process(consumer
);
281 wait_for_completion(&read_done
);
284 time
= end_tv
.tv_sec
- start_tv
.tv_sec
;
285 time
*= USEC_PER_SEC
;
286 time
+= (long long)((long)end_tv
.tv_usec
- (long)start_tv
.tv_usec
);
288 entries
= ring_buffer_entries(buffer
);
289 overruns
= ring_buffer_overruns(buffer
);
292 trace_printk("ERROR!\n");
294 if (!disable_reader
) {
295 if (consumer_fifo
< 0)
296 trace_printk("Running Consumer at nice: %d\n",
299 trace_printk("Running Consumer at SCHED_FIFO %d\n",
302 if (producer_fifo
< 0)
303 trace_printk("Running Producer at nice: %d\n",
306 trace_printk("Running Producer at SCHED_FIFO %d\n",
309 /* Let the user know that the test is running at low priority */
310 if (producer_fifo
< 0 && consumer_fifo
< 0 &&
311 producer_nice
== 19 && consumer_nice
== 19)
312 trace_printk("WARNING!!! This test is running at lowest priority.\n");
314 trace_printk("Time: %lld (usecs)\n", time
);
315 trace_printk("Overruns: %lld\n", overruns
);
317 trace_printk("Read: (reader disabled)\n");
319 trace_printk("Read: %ld (by %s)\n", read
,
320 read_events
? "events" : "pages");
321 trace_printk("Entries: %lld\n", entries
);
322 trace_printk("Total: %lld\n", entries
+ overruns
+ read
);
323 trace_printk("Missed: %ld\n", missed
);
324 trace_printk("Hit: %ld\n", hit
);
326 /* Convert time from usecs to millisecs */
327 do_div(time
, USEC_PER_MSEC
);
331 trace_printk("TIME IS ZERO??\n");
333 trace_printk("Entries per millisec: %ld\n", hit
);
336 /* Calculate the average time in nanosecs */
337 avg
= NSEC_PER_MSEC
/ hit
;
338 trace_printk("%ld ns per entry\n", avg
);
343 missed
/= (long)time
;
345 trace_printk("Total iterations per millisec: %ld\n",
348 /* it is possible that hit + missed will overflow and be zero */
349 if (!(hit
+ missed
)) {
350 trace_printk("hit + missed overflowed and totalled zero!\n");
351 hit
--; /* make it non zero */
354 /* Caculate the average time in nanosecs */
355 avg
= NSEC_PER_MSEC
/ (hit
+ missed
);
356 trace_printk("%ld ns per entry\n", avg
);
360 static void wait_to_die(void)
362 set_current_state(TASK_INTERRUPTIBLE
);
363 while (!kthread_should_stop()) {
365 set_current_state(TASK_INTERRUPTIBLE
);
367 __set_current_state(TASK_RUNNING
);
370 static int ring_buffer_consumer_thread(void *arg
)
372 while (!kthread_should_stop() && !kill_test
) {
373 complete(&read_start
);
375 ring_buffer_consumer();
377 set_current_state(TASK_INTERRUPTIBLE
);
378 if (kthread_should_stop() || kill_test
)
382 __set_current_state(TASK_RUNNING
);
384 __set_current_state(TASK_RUNNING
);
392 static int ring_buffer_producer_thread(void *arg
)
394 init_completion(&read_start
);
396 while (!kthread_should_stop() && !kill_test
) {
397 ring_buffer_reset(buffer
);
401 wake_up_process(consumer
);
402 wait_for_completion(&read_start
);
405 ring_buffer_producer();
407 trace_printk("Sleeping for 10 secs\n");
408 set_current_state(TASK_INTERRUPTIBLE
);
409 schedule_timeout(HZ
* SLEEP_TIME
);
410 __set_current_state(TASK_RUNNING
);
419 static int __init
ring_buffer_benchmark_init(void)
423 /* make a one meg buffer in overwite mode */
424 buffer
= ring_buffer_alloc(1000000, RB_FL_OVERWRITE
);
428 if (!disable_reader
) {
429 consumer
= kthread_create(ring_buffer_consumer_thread
,
430 NULL
, "rb_consumer");
431 ret
= PTR_ERR(consumer
);
432 if (IS_ERR(consumer
))
436 producer
= kthread_run(ring_buffer_producer_thread
,
437 NULL
, "rb_producer");
438 ret
= PTR_ERR(producer
);
440 if (IS_ERR(producer
))
444 * Run them as low-prio background tasks by default:
446 if (!disable_reader
) {
447 if (consumer_fifo
>= 0) {
448 struct sched_param param
= {
449 .sched_priority
= consumer_fifo
451 sched_setscheduler(consumer
, SCHED_FIFO
, ¶m
);
453 set_user_nice(consumer
, consumer_nice
);
456 if (producer_fifo
>= 0) {
457 struct sched_param param
= {
458 .sched_priority
= consumer_fifo
460 sched_setscheduler(producer
, SCHED_FIFO
, ¶m
);
462 set_user_nice(producer
, producer_nice
);
468 kthread_stop(consumer
);
471 ring_buffer_free(buffer
);
475 static void __exit
ring_buffer_benchmark_exit(void)
477 kthread_stop(producer
);
479 kthread_stop(consumer
);
480 ring_buffer_free(buffer
);
483 module_init(ring_buffer_benchmark_init
);
484 module_exit(ring_buffer_benchmark_exit
);
486 MODULE_AUTHOR("Steven Rostedt");
487 MODULE_DESCRIPTION("ring_buffer_benchmark");
488 MODULE_LICENSE("GPL");