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>
18 /* run time and sleep time in seconds */
22 /* number of events for writer to wake up the reader */
23 static int wakeup_interval
= 100;
25 static int reader_finish
;
26 static struct completion read_start
;
27 static struct completion read_done
;
29 static struct ring_buffer
*buffer
;
30 static struct task_struct
*producer
;
31 static struct task_struct
*consumer
;
32 static unsigned long read
;
34 static int disable_reader
;
35 module_param(disable_reader
, uint
, 0644);
36 MODULE_PARM_DESC(disable_reader
, "only run producer");
38 static int write_iteration
= 50;
39 module_param(write_iteration
, uint
, 0644);
40 MODULE_PARM_DESC(write_iteration
, "# of writes between timestamp readings");
42 static int producer_nice
= 19;
43 static int consumer_nice
= 19;
45 static int producer_fifo
= -1;
46 static int consumer_fifo
= -1;
48 module_param(producer_nice
, uint
, 0644);
49 MODULE_PARM_DESC(producer_nice
, "nice prio for producer");
51 module_param(consumer_nice
, uint
, 0644);
52 MODULE_PARM_DESC(consumer_nice
, "nice prio for consumer");
54 module_param(producer_fifo
, uint
, 0644);
55 MODULE_PARM_DESC(producer_fifo
, "fifo prio for producer");
57 module_param(consumer_fifo
, uint
, 0644);
58 MODULE_PARM_DESC(consumer_fifo
, "fifo prio for consumer");
60 static int read_events
;
77 static enum event_status
read_event(int cpu
)
79 struct ring_buffer_event
*event
;
83 event
= ring_buffer_consume(buffer
, cpu
, &ts
);
87 entry
= ring_buffer_event_data(event
);
97 static enum event_status
read_page(int cpu
)
99 struct ring_buffer_event
*event
;
100 struct rb_page
*rpage
;
101 unsigned long commit
;
108 bpage
= ring_buffer_alloc_read_page(buffer
);
110 return EVENT_DROPPED
;
112 ret
= ring_buffer_read_page(buffer
, &bpage
, PAGE_SIZE
, cpu
, 1);
115 commit
= local_read(&rpage
->commit
);
116 for (i
= 0; i
< commit
&& !kill_test
; i
+= inc
) {
118 if (i
>= (PAGE_SIZE
- offsetof(struct rb_page
, data
))) {
124 event
= (void *)&rpage
->data
[i
];
125 switch (event
->type_len
) {
126 case RINGBUF_TYPE_PADDING
:
127 /* failed writes may be discarded events */
128 if (!event
->time_delta
)
130 inc
= event
->array
[0] + 4;
132 case RINGBUF_TYPE_TIME_EXTEND
:
136 entry
= ring_buffer_event_data(event
);
142 if (!event
->array
[0]) {
146 inc
= event
->array
[0] + 4;
149 entry
= ring_buffer_event_data(event
);
155 inc
= ((event
->type_len
+ 1) * 4);
166 ring_buffer_free_read_page(buffer
, bpage
);
169 return EVENT_DROPPED
;
173 static void ring_buffer_consumer(void)
175 /* toggle between reading pages and events */
179 while (!reader_finish
&& !kill_test
) {
186 for_each_online_cpu(cpu
) {
187 enum event_status stat
;
190 stat
= read_event(cpu
);
192 stat
= read_page(cpu
);
196 if (stat
== EVENT_FOUND
)
199 } while (found
&& !kill_test
);
201 set_current_state(TASK_INTERRUPTIBLE
);
206 __set_current_state(TASK_RUNNING
);
209 complete(&read_done
);
212 static void ring_buffer_producer(void)
214 struct timeval start_tv
;
215 struct timeval end_tv
;
216 unsigned long long time
;
217 unsigned long long entries
;
218 unsigned long long overruns
;
219 unsigned long missed
= 0;
220 unsigned long hit
= 0;
225 * Hammer the buffer for 10 secs (this may
226 * make the system stall)
228 trace_printk("Starting ring buffer hammer\n");
229 do_gettimeofday(&start_tv
);
231 struct ring_buffer_event
*event
;
235 for (i
= 0; i
< write_iteration
; i
++) {
236 event
= ring_buffer_lock_reserve(buffer
, 10);
241 entry
= ring_buffer_event_data(event
);
242 *entry
= smp_processor_id();
243 ring_buffer_unlock_commit(buffer
, event
);
246 do_gettimeofday(&end_tv
);
249 if (consumer
&& !(cnt
% wakeup_interval
))
250 wake_up_process(consumer
);
252 #ifndef CONFIG_PREEMPT
254 * If we are a non preempt kernel, the 10 second run will
255 * stop everything while it runs. Instead, we will call
256 * cond_resched and also add any time that was lost by a
259 * Do a cond resched at the same frequency we would wake up
262 if (cnt
% wakeup_interval
)
266 } while (end_tv
.tv_sec
< (start_tv
.tv_sec
+ RUN_TIME
) && !kill_test
);
267 trace_printk("End ring buffer hammer\n");
270 /* Init both completions here to avoid races */
271 init_completion(&read_start
);
272 init_completion(&read_done
);
273 /* the completions must be visible before the finish var */
276 /* finish var visible before waking up the consumer */
278 wake_up_process(consumer
);
279 wait_for_completion(&read_done
);
282 time
= end_tv
.tv_sec
- start_tv
.tv_sec
;
283 time
*= USEC_PER_SEC
;
284 time
+= (long long)((long)end_tv
.tv_usec
- (long)start_tv
.tv_usec
);
286 entries
= ring_buffer_entries(buffer
);
287 overruns
= ring_buffer_overruns(buffer
);
290 trace_printk("ERROR!\n");
292 if (!disable_reader
) {
293 if (consumer_fifo
< 0)
294 trace_printk("Running Consumer at nice: %d\n",
297 trace_printk("Running Consumer at SCHED_FIFO %d\n",
300 if (producer_fifo
< 0)
301 trace_printk("Running Producer at nice: %d\n",
304 trace_printk("Running Producer at SCHED_FIFO %d\n",
307 /* Let the user know that the test is running at low priority */
308 if (producer_fifo
< 0 && consumer_fifo
< 0 &&
309 producer_nice
== 19 && consumer_nice
== 19)
310 trace_printk("WARNING!!! This test is running at lowest priority.\n");
312 trace_printk("Time: %lld (usecs)\n", time
);
313 trace_printk("Overruns: %lld\n", overruns
);
315 trace_printk("Read: (reader disabled)\n");
317 trace_printk("Read: %ld (by %s)\n", read
,
318 read_events
? "events" : "pages");
319 trace_printk("Entries: %lld\n", entries
);
320 trace_printk("Total: %lld\n", entries
+ overruns
+ read
);
321 trace_printk("Missed: %ld\n", missed
);
322 trace_printk("Hit: %ld\n", hit
);
324 /* Convert time from usecs to millisecs */
325 do_div(time
, USEC_PER_MSEC
);
329 trace_printk("TIME IS ZERO??\n");
331 trace_printk("Entries per millisec: %ld\n", hit
);
334 /* Calculate the average time in nanosecs */
335 avg
= NSEC_PER_MSEC
/ hit
;
336 trace_printk("%ld ns per entry\n", avg
);
341 missed
/= (long)time
;
343 trace_printk("Total iterations per millisec: %ld\n",
346 /* it is possible that hit + missed will overflow and be zero */
347 if (!(hit
+ missed
)) {
348 trace_printk("hit + missed overflowed and totalled zero!\n");
349 hit
--; /* make it non zero */
352 /* Caculate the average time in nanosecs */
353 avg
= NSEC_PER_MSEC
/ (hit
+ missed
);
354 trace_printk("%ld ns per entry\n", avg
);
358 static void wait_to_die(void)
360 set_current_state(TASK_INTERRUPTIBLE
);
361 while (!kthread_should_stop()) {
363 set_current_state(TASK_INTERRUPTIBLE
);
365 __set_current_state(TASK_RUNNING
);
368 static int ring_buffer_consumer_thread(void *arg
)
370 while (!kthread_should_stop() && !kill_test
) {
371 complete(&read_start
);
373 ring_buffer_consumer();
375 set_current_state(TASK_INTERRUPTIBLE
);
376 if (kthread_should_stop() || kill_test
)
380 __set_current_state(TASK_RUNNING
);
382 __set_current_state(TASK_RUNNING
);
390 static int ring_buffer_producer_thread(void *arg
)
392 init_completion(&read_start
);
394 while (!kthread_should_stop() && !kill_test
) {
395 ring_buffer_reset(buffer
);
399 wake_up_process(consumer
);
400 wait_for_completion(&read_start
);
403 ring_buffer_producer();
405 trace_printk("Sleeping for 10 secs\n");
406 set_current_state(TASK_INTERRUPTIBLE
);
407 schedule_timeout(HZ
* SLEEP_TIME
);
408 __set_current_state(TASK_RUNNING
);
417 static int __init
ring_buffer_benchmark_init(void)
421 /* make a one meg buffer in overwite mode */
422 buffer
= ring_buffer_alloc(1000000, RB_FL_OVERWRITE
);
426 if (!disable_reader
) {
427 consumer
= kthread_create(ring_buffer_consumer_thread
,
428 NULL
, "rb_consumer");
429 ret
= PTR_ERR(consumer
);
430 if (IS_ERR(consumer
))
434 producer
= kthread_run(ring_buffer_producer_thread
,
435 NULL
, "rb_producer");
436 ret
= PTR_ERR(producer
);
438 if (IS_ERR(producer
))
442 * Run them as low-prio background tasks by default:
444 if (!disable_reader
) {
445 if (consumer_fifo
>= 0) {
446 struct sched_param param
= {
447 .sched_priority
= consumer_fifo
449 sched_setscheduler(consumer
, SCHED_FIFO
, ¶m
);
451 set_user_nice(consumer
, consumer_nice
);
454 if (producer_fifo
>= 0) {
455 struct sched_param param
= {
456 .sched_priority
= consumer_fifo
458 sched_setscheduler(producer
, SCHED_FIFO
, ¶m
);
460 set_user_nice(producer
, producer_nice
);
466 kthread_stop(consumer
);
469 ring_buffer_free(buffer
);
473 static void __exit
ring_buffer_benchmark_exit(void)
475 kthread_stop(producer
);
477 kthread_stop(consumer
);
478 ring_buffer_free(buffer
);
481 module_init(ring_buffer_benchmark_init
);
482 module_exit(ring_buffer_benchmark_exit
);
484 MODULE_AUTHOR("Steven Rostedt");
485 MODULE_DESCRIPTION("ring_buffer_benchmark");
486 MODULE_LICENSE("GPL");