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 <uapi/linux/sched/types.h>
10 #include <linux/module.h>
11 #include <linux/ktime.h>
12 #include <asm/local.h>
20 /* run time and sleep time in seconds */
21 #define RUN_TIME 10ULL
24 /* number of events for writer to wake up the reader */
25 static int wakeup_interval
= 100;
27 static int reader_finish
;
28 static DECLARE_COMPLETION(read_start
);
29 static DECLARE_COMPLETION(read_done
);
31 static struct ring_buffer
*buffer
;
32 static struct task_struct
*producer
;
33 static struct task_struct
*consumer
;
34 static unsigned long read
;
36 static unsigned int disable_reader
;
37 module_param(disable_reader
, uint
, 0644);
38 MODULE_PARM_DESC(disable_reader
, "only run producer");
40 static unsigned int write_iteration
= 50;
41 module_param(write_iteration
, uint
, 0644);
42 MODULE_PARM_DESC(write_iteration
, "# of writes between timestamp readings");
44 static int producer_nice
= MAX_NICE
;
45 static int consumer_nice
= MAX_NICE
;
47 static int producer_fifo
= -1;
48 static int consumer_fifo
= -1;
50 module_param(producer_nice
, int, 0644);
51 MODULE_PARM_DESC(producer_nice
, "nice prio for producer");
53 module_param(consumer_nice
, int, 0644);
54 MODULE_PARM_DESC(consumer_nice
, "nice prio for consumer");
56 module_param(producer_fifo
, int, 0644);
57 MODULE_PARM_DESC(producer_fifo
, "fifo prio for producer");
59 module_param(consumer_fifo
, int, 0644);
60 MODULE_PARM_DESC(consumer_fifo
, "fifo prio for consumer");
62 static int read_events
;
64 static int test_error
;
66 #define TEST_ERROR() \
79 static bool break_test(void)
81 return test_error
|| kthread_should_stop();
84 static enum event_status
read_event(int cpu
)
86 struct ring_buffer_event
*event
;
90 event
= ring_buffer_consume(buffer
, cpu
, &ts
, NULL
);
94 entry
= ring_buffer_event_data(event
);
104 static enum event_status
read_page(int cpu
)
106 struct ring_buffer_event
*event
;
107 struct rb_page
*rpage
;
108 unsigned long commit
;
115 bpage
= ring_buffer_alloc_read_page(buffer
, cpu
);
117 return EVENT_DROPPED
;
119 ret
= ring_buffer_read_page(buffer
, &bpage
, PAGE_SIZE
, cpu
, 1);
122 /* The commit may have missed event flags set, clear them */
123 commit
= local_read(&rpage
->commit
) & 0xfffff;
124 for (i
= 0; i
< commit
&& !test_error
; i
+= inc
) {
126 if (i
>= (PAGE_SIZE
- offsetof(struct rb_page
, data
))) {
132 event
= (void *)&rpage
->data
[i
];
133 switch (event
->type_len
) {
134 case RINGBUF_TYPE_PADDING
:
135 /* failed writes may be discarded events */
136 if (!event
->time_delta
)
138 inc
= event
->array
[0] + 4;
140 case RINGBUF_TYPE_TIME_EXTEND
:
144 entry
= ring_buffer_event_data(event
);
150 if (!event
->array
[0]) {
154 inc
= event
->array
[0] + 4;
157 entry
= ring_buffer_event_data(event
);
163 inc
= ((event
->type_len
+ 1) * 4);
174 ring_buffer_free_read_page(buffer
, cpu
, bpage
);
177 return EVENT_DROPPED
;
181 static void ring_buffer_consumer(void)
183 /* toggle between reading pages and events */
188 * Continue running until the producer specifically asks to stop
189 * and is ready for the completion.
191 while (!READ_ONCE(reader_finish
)) {
194 while (found
&& !test_error
) {
198 for_each_online_cpu(cpu
) {
199 enum event_status stat
;
202 stat
= read_event(cpu
);
204 stat
= read_page(cpu
);
209 if (stat
== EVENT_FOUND
)
215 /* Wait till the producer wakes us up when there is more data
216 * available or when the producer wants us to finish reading.
218 set_current_state(TASK_INTERRUPTIBLE
);
224 __set_current_state(TASK_RUNNING
);
226 complete(&read_done
);
229 static void ring_buffer_producer(void)
231 ktime_t start_time
, end_time
, timeout
;
232 unsigned long long time
;
233 unsigned long long entries
;
234 unsigned long long overruns
;
235 unsigned long missed
= 0;
236 unsigned long hit
= 0;
241 * Hammer the buffer for 10 secs (this may
242 * make the system stall)
244 trace_printk("Starting ring buffer hammer\n");
245 start_time
= ktime_get();
246 timeout
= ktime_add_ns(start_time
, RUN_TIME
* NSEC_PER_SEC
);
248 struct ring_buffer_event
*event
;
252 for (i
= 0; i
< write_iteration
; i
++) {
253 event
= ring_buffer_lock_reserve(buffer
, 10);
258 entry
= ring_buffer_event_data(event
);
259 *entry
= smp_processor_id();
260 ring_buffer_unlock_commit(buffer
, event
);
263 end_time
= ktime_get();
266 if (consumer
&& !(cnt
% wakeup_interval
))
267 wake_up_process(consumer
);
269 #ifndef CONFIG_PREEMPT
271 * If we are a non preempt kernel, the 10 second run will
272 * stop everything while it runs. Instead, we will call
273 * cond_resched and also add any time that was lost by a
276 * Do a cond resched at the same frequency we would wake up
279 if (cnt
% wakeup_interval
)
282 } while (ktime_before(end_time
, timeout
) && !break_test());
283 trace_printk("End ring buffer hammer\n");
286 /* Init both completions here to avoid races */
287 init_completion(&read_start
);
288 init_completion(&read_done
);
289 /* the completions must be visible before the finish var */
292 wake_up_process(consumer
);
293 wait_for_completion(&read_done
);
296 time
= ktime_us_delta(end_time
, start_time
);
298 entries
= ring_buffer_entries(buffer
);
299 overruns
= ring_buffer_overruns(buffer
);
302 trace_printk("ERROR!\n");
304 if (!disable_reader
) {
305 if (consumer_fifo
< 0)
306 trace_printk("Running Consumer at nice: %d\n",
309 trace_printk("Running Consumer at SCHED_FIFO %d\n",
312 if (producer_fifo
< 0)
313 trace_printk("Running Producer at nice: %d\n",
316 trace_printk("Running Producer at SCHED_FIFO %d\n",
319 /* Let the user know that the test is running at low priority */
320 if (producer_fifo
< 0 && consumer_fifo
< 0 &&
321 producer_nice
== MAX_NICE
&& consumer_nice
== MAX_NICE
)
322 trace_printk("WARNING!!! This test is running at lowest priority.\n");
324 trace_printk("Time: %lld (usecs)\n", time
);
325 trace_printk("Overruns: %lld\n", overruns
);
327 trace_printk("Read: (reader disabled)\n");
329 trace_printk("Read: %ld (by %s)\n", read
,
330 read_events
? "events" : "pages");
331 trace_printk("Entries: %lld\n", entries
);
332 trace_printk("Total: %lld\n", entries
+ overruns
+ read
);
333 trace_printk("Missed: %ld\n", missed
);
334 trace_printk("Hit: %ld\n", hit
);
336 /* Convert time from usecs to millisecs */
337 do_div(time
, USEC_PER_MSEC
);
341 trace_printk("TIME IS ZERO??\n");
343 trace_printk("Entries per millisec: %ld\n", hit
);
346 /* Calculate the average time in nanosecs */
347 avg
= NSEC_PER_MSEC
/ hit
;
348 trace_printk("%ld ns per entry\n", avg
);
353 missed
/= (long)time
;
355 trace_printk("Total iterations per millisec: %ld\n",
358 /* it is possible that hit + missed will overflow and be zero */
359 if (!(hit
+ missed
)) {
360 trace_printk("hit + missed overflowed and totalled zero!\n");
361 hit
--; /* make it non zero */
364 /* Caculate the average time in nanosecs */
365 avg
= NSEC_PER_MSEC
/ (hit
+ missed
);
366 trace_printk("%ld ns per entry\n", avg
);
370 static void wait_to_die(void)
372 set_current_state(TASK_INTERRUPTIBLE
);
373 while (!kthread_should_stop()) {
375 set_current_state(TASK_INTERRUPTIBLE
);
377 __set_current_state(TASK_RUNNING
);
380 static int ring_buffer_consumer_thread(void *arg
)
382 while (!break_test()) {
383 complete(&read_start
);
385 ring_buffer_consumer();
387 set_current_state(TASK_INTERRUPTIBLE
);
392 __set_current_state(TASK_RUNNING
);
394 if (!kthread_should_stop())
400 static int ring_buffer_producer_thread(void *arg
)
402 while (!break_test()) {
403 ring_buffer_reset(buffer
);
406 wake_up_process(consumer
);
407 wait_for_completion(&read_start
);
410 ring_buffer_producer();
414 trace_printk("Sleeping for 10 secs\n");
415 set_current_state(TASK_INTERRUPTIBLE
);
418 schedule_timeout(HZ
* SLEEP_TIME
);
422 __set_current_state(TASK_RUNNING
);
423 if (!kthread_should_stop())
429 static int __init
ring_buffer_benchmark_init(void)
433 /* make a one meg buffer in overwite mode */
434 buffer
= ring_buffer_alloc(1000000, RB_FL_OVERWRITE
);
438 if (!disable_reader
) {
439 consumer
= kthread_create(ring_buffer_consumer_thread
,
440 NULL
, "rb_consumer");
441 ret
= PTR_ERR(consumer
);
442 if (IS_ERR(consumer
))
446 producer
= kthread_run(ring_buffer_producer_thread
,
447 NULL
, "rb_producer");
448 ret
= PTR_ERR(producer
);
450 if (IS_ERR(producer
))
454 * Run them as low-prio background tasks by default:
456 if (!disable_reader
) {
457 if (consumer_fifo
>= 0) {
458 struct sched_param param
= {
459 .sched_priority
= consumer_fifo
461 sched_setscheduler(consumer
, SCHED_FIFO
, ¶m
);
463 set_user_nice(consumer
, consumer_nice
);
466 if (producer_fifo
>= 0) {
467 struct sched_param param
= {
468 .sched_priority
= producer_fifo
470 sched_setscheduler(producer
, SCHED_FIFO
, ¶m
);
472 set_user_nice(producer
, producer_nice
);
478 kthread_stop(consumer
);
481 ring_buffer_free(buffer
);
485 static void __exit
ring_buffer_benchmark_exit(void)
487 kthread_stop(producer
);
489 kthread_stop(consumer
);
490 ring_buffer_free(buffer
);
493 module_init(ring_buffer_benchmark_init
);
494 module_exit(ring_buffer_benchmark_exit
);
496 MODULE_AUTHOR("Steven Rostedt");
497 MODULE_DESCRIPTION("ring_buffer_benchmark");
498 MODULE_LICENSE("GPL");