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/ktime.h>
11 #include <asm/local.h>
19 /* run time and sleep time in seconds */
20 #define RUN_TIME 10ULL
23 /* number of events for writer to wake up the reader */
24 static int wakeup_interval
= 100;
26 static int reader_finish
;
27 static DECLARE_COMPLETION(read_start
);
28 static DECLARE_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 unsigned int disable_reader
;
36 module_param(disable_reader
, uint
, 0644);
37 MODULE_PARM_DESC(disable_reader
, "only run producer");
39 static unsigned 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
= MAX_NICE
;
44 static int consumer_nice
= MAX_NICE
;
46 static int producer_fifo
= -1;
47 static int consumer_fifo
= -1;
49 module_param(producer_nice
, int, 0644);
50 MODULE_PARM_DESC(producer_nice
, "nice prio for producer");
52 module_param(consumer_nice
, int, 0644);
53 MODULE_PARM_DESC(consumer_nice
, "nice prio for consumer");
55 module_param(producer_fifo
, int, 0644);
56 MODULE_PARM_DESC(producer_fifo
, "fifo prio for producer");
58 module_param(consumer_fifo
, int, 0644);
59 MODULE_PARM_DESC(consumer_fifo
, "fifo prio for consumer");
61 static int read_events
;
63 static int test_error
;
65 #define TEST_ERROR() \
78 static bool break_test(void)
80 return test_error
|| kthread_should_stop();
83 static enum event_status
read_event(int cpu
)
85 struct ring_buffer_event
*event
;
89 event
= ring_buffer_consume(buffer
, cpu
, &ts
, NULL
);
93 entry
= ring_buffer_event_data(event
);
103 static enum event_status
read_page(int cpu
)
105 struct ring_buffer_event
*event
;
106 struct rb_page
*rpage
;
107 unsigned long commit
;
114 bpage
= ring_buffer_alloc_read_page(buffer
, cpu
);
116 return EVENT_DROPPED
;
118 ret
= ring_buffer_read_page(buffer
, &bpage
, PAGE_SIZE
, cpu
, 1);
121 /* The commit may have missed event flags set, clear them */
122 commit
= local_read(&rpage
->commit
) & 0xfffff;
123 for (i
= 0; i
< commit
&& !test_error
; i
+= inc
) {
125 if (i
>= (PAGE_SIZE
- offsetof(struct rb_page
, data
))) {
131 event
= (void *)&rpage
->data
[i
];
132 switch (event
->type_len
) {
133 case RINGBUF_TYPE_PADDING
:
134 /* failed writes may be discarded events */
135 if (!event
->time_delta
)
137 inc
= event
->array
[0] + 4;
139 case RINGBUF_TYPE_TIME_EXTEND
:
143 entry
= ring_buffer_event_data(event
);
149 if (!event
->array
[0]) {
153 inc
= event
->array
[0] + 4;
156 entry
= ring_buffer_event_data(event
);
162 inc
= ((event
->type_len
+ 1) * 4);
173 ring_buffer_free_read_page(buffer
, bpage
);
176 return EVENT_DROPPED
;
180 static void ring_buffer_consumer(void)
182 /* toggle between reading pages and events */
187 * Continue running until the producer specifically asks to stop
188 * and is ready for the completion.
190 while (!READ_ONCE(reader_finish
)) {
193 while (found
&& !test_error
) {
197 for_each_online_cpu(cpu
) {
198 enum event_status stat
;
201 stat
= read_event(cpu
);
203 stat
= read_page(cpu
);
208 if (stat
== EVENT_FOUND
)
214 /* Wait till the producer wakes us up when there is more data
215 * available or when the producer wants us to finish reading.
217 set_current_state(TASK_INTERRUPTIBLE
);
223 __set_current_state(TASK_RUNNING
);
225 complete(&read_done
);
228 static void ring_buffer_producer(void)
230 ktime_t start_time
, end_time
, timeout
;
231 unsigned long long time
;
232 unsigned long long entries
;
233 unsigned long long overruns
;
234 unsigned long missed
= 0;
235 unsigned long hit
= 0;
240 * Hammer the buffer for 10 secs (this may
241 * make the system stall)
243 trace_printk("Starting ring buffer hammer\n");
244 start_time
= ktime_get();
245 timeout
= ktime_add_ns(start_time
, RUN_TIME
* NSEC_PER_SEC
);
247 struct ring_buffer_event
*event
;
251 for (i
= 0; i
< write_iteration
; i
++) {
252 event
= ring_buffer_lock_reserve(buffer
, 10);
257 entry
= ring_buffer_event_data(event
);
258 *entry
= smp_processor_id();
259 ring_buffer_unlock_commit(buffer
, event
);
262 end_time
= ktime_get();
265 if (consumer
&& !(cnt
% wakeup_interval
))
266 wake_up_process(consumer
);
268 #ifndef CONFIG_PREEMPT
270 * If we are a non preempt kernel, the 10 second run will
271 * stop everything while it runs. Instead, we will call
272 * cond_resched and also add any time that was lost by a
275 * Do a cond resched at the same frequency we would wake up
278 if (cnt
% wakeup_interval
)
281 } while (ktime_before(end_time
, timeout
) && !break_test());
282 trace_printk("End ring buffer hammer\n");
285 /* Init both completions here to avoid races */
286 init_completion(&read_start
);
287 init_completion(&read_done
);
288 /* the completions must be visible before the finish var */
291 wake_up_process(consumer
);
292 wait_for_completion(&read_done
);
295 time
= ktime_us_delta(end_time
, start_time
);
297 entries
= ring_buffer_entries(buffer
);
298 overruns
= ring_buffer_overruns(buffer
);
301 trace_printk("ERROR!\n");
303 if (!disable_reader
) {
304 if (consumer_fifo
< 0)
305 trace_printk("Running Consumer at nice: %d\n",
308 trace_printk("Running Consumer at SCHED_FIFO %d\n",
311 if (producer_fifo
< 0)
312 trace_printk("Running Producer at nice: %d\n",
315 trace_printk("Running Producer at SCHED_FIFO %d\n",
318 /* Let the user know that the test is running at low priority */
319 if (producer_fifo
< 0 && consumer_fifo
< 0 &&
320 producer_nice
== MAX_NICE
&& consumer_nice
== MAX_NICE
)
321 trace_printk("WARNING!!! This test is running at lowest priority.\n");
323 trace_printk("Time: %lld (usecs)\n", time
);
324 trace_printk("Overruns: %lld\n", overruns
);
326 trace_printk("Read: (reader disabled)\n");
328 trace_printk("Read: %ld (by %s)\n", read
,
329 read_events
? "events" : "pages");
330 trace_printk("Entries: %lld\n", entries
);
331 trace_printk("Total: %lld\n", entries
+ overruns
+ read
);
332 trace_printk("Missed: %ld\n", missed
);
333 trace_printk("Hit: %ld\n", hit
);
335 /* Convert time from usecs to millisecs */
336 do_div(time
, USEC_PER_MSEC
);
340 trace_printk("TIME IS ZERO??\n");
342 trace_printk("Entries per millisec: %ld\n", hit
);
345 /* Calculate the average time in nanosecs */
346 avg
= NSEC_PER_MSEC
/ hit
;
347 trace_printk("%ld ns per entry\n", avg
);
352 missed
/= (long)time
;
354 trace_printk("Total iterations per millisec: %ld\n",
357 /* it is possible that hit + missed will overflow and be zero */
358 if (!(hit
+ missed
)) {
359 trace_printk("hit + missed overflowed and totalled zero!\n");
360 hit
--; /* make it non zero */
363 /* Caculate the average time in nanosecs */
364 avg
= NSEC_PER_MSEC
/ (hit
+ missed
);
365 trace_printk("%ld ns per entry\n", avg
);
369 static void wait_to_die(void)
371 set_current_state(TASK_INTERRUPTIBLE
);
372 while (!kthread_should_stop()) {
374 set_current_state(TASK_INTERRUPTIBLE
);
376 __set_current_state(TASK_RUNNING
);
379 static int ring_buffer_consumer_thread(void *arg
)
381 while (!break_test()) {
382 complete(&read_start
);
384 ring_buffer_consumer();
386 set_current_state(TASK_INTERRUPTIBLE
);
391 __set_current_state(TASK_RUNNING
);
393 if (!kthread_should_stop())
399 static int ring_buffer_producer_thread(void *arg
)
401 while (!break_test()) {
402 ring_buffer_reset(buffer
);
405 wake_up_process(consumer
);
406 wait_for_completion(&read_start
);
409 ring_buffer_producer();
413 trace_printk("Sleeping for 10 secs\n");
414 set_current_state(TASK_INTERRUPTIBLE
);
417 schedule_timeout(HZ
* SLEEP_TIME
);
421 __set_current_state(TASK_RUNNING
);
422 if (!kthread_should_stop())
428 static int __init
ring_buffer_benchmark_init(void)
432 /* make a one meg buffer in overwite mode */
433 buffer
= ring_buffer_alloc(1000000, RB_FL_OVERWRITE
);
437 if (!disable_reader
) {
438 consumer
= kthread_create(ring_buffer_consumer_thread
,
439 NULL
, "rb_consumer");
440 ret
= PTR_ERR(consumer
);
441 if (IS_ERR(consumer
))
445 producer
= kthread_run(ring_buffer_producer_thread
,
446 NULL
, "rb_producer");
447 ret
= PTR_ERR(producer
);
449 if (IS_ERR(producer
))
453 * Run them as low-prio background tasks by default:
455 if (!disable_reader
) {
456 if (consumer_fifo
>= 0) {
457 struct sched_param param
= {
458 .sched_priority
= consumer_fifo
460 sched_setscheduler(consumer
, SCHED_FIFO
, ¶m
);
462 set_user_nice(consumer
, consumer_nice
);
465 if (producer_fifo
>= 0) {
466 struct sched_param param
= {
467 .sched_priority
= producer_fifo
469 sched_setscheduler(producer
, SCHED_FIFO
, ¶m
);
471 set_user_nice(producer
, producer_nice
);
477 kthread_stop(consumer
);
480 ring_buffer_free(buffer
);
484 static void __exit
ring_buffer_benchmark_exit(void)
486 kthread_stop(producer
);
488 kthread_stop(consumer
);
489 ring_buffer_free(buffer
);
492 module_init(ring_buffer_benchmark_init
);
493 module_exit(ring_buffer_benchmark_exit
);
495 MODULE_AUTHOR("Steven Rostedt");
496 MODULE_DESCRIPTION("ring_buffer_benchmark");
497 MODULE_LICENSE("GPL");