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 read_events;
55 static enum event_status read_event(int cpu)
57 struct ring_buffer_event *event;
61 event = ring_buffer_consume(buffer, cpu, &ts);
65 entry = ring_buffer_event_data(event);
75 static enum event_status read_page(int cpu)
77 struct ring_buffer_event *event;
78 struct rb_page *rpage;
86 bpage = ring_buffer_alloc_read_page(buffer);
90 ret = ring_buffer_read_page(buffer, &bpage, PAGE_SIZE, cpu, 1);
93 commit = local_read(&rpage->commit);
94 for (i = 0; i < commit && !kill_test; i += inc) {
96 if (i >= (PAGE_SIZE - offsetof(struct rb_page, data))) {
102 event = (void *)&rpage->data[i];
103 switch (event->type_len) {
104 case RINGBUF_TYPE_PADDING:
105 /* failed writes may be discarded events */
106 if (!event->time_delta)
108 inc = event->array[0] + 4;
110 case RINGBUF_TYPE_TIME_EXTEND:
114 entry = ring_buffer_event_data(event);
120 if (!event->array[0]) {
124 inc = event->array[0] + 4;
127 entry = ring_buffer_event_data(event);
133 inc = ((event->type_len + 1) * 4);
144 ring_buffer_free_read_page(buffer, bpage);
147 return EVENT_DROPPED;
151 static void ring_buffer_consumer(void)
153 /* toggle between reading pages and events */
157 while (!reader_finish && !kill_test) {
164 for_each_online_cpu(cpu) {
165 enum event_status stat;
168 stat = read_event(cpu);
170 stat = read_page(cpu);
174 if (stat == EVENT_FOUND)
177 } while (found && !kill_test);
179 set_current_state(TASK_INTERRUPTIBLE);
184 __set_current_state(TASK_RUNNING);
187 complete(&read_done);
190 static void ring_buffer_producer(void)
192 struct timeval start_tv;
193 struct timeval end_tv;
194 unsigned long long time;
195 unsigned long long entries;
196 unsigned long long overruns;
197 unsigned long missed = 0;
198 unsigned long hit = 0;
203 * Hammer the buffer for 10 secs (this may
204 * make the system stall)
206 trace_printk("Starting ring buffer hammer\n");
207 do_gettimeofday(&start_tv);
209 struct ring_buffer_event *event;
212 event = ring_buffer_lock_reserve(buffer, 10);
217 entry = ring_buffer_event_data(event);
218 *entry = smp_processor_id();
219 ring_buffer_unlock_commit(buffer, event);
221 do_gettimeofday(&end_tv);
224 if (consumer && !(cnt % wakeup_interval))
225 wake_up_process(consumer);
227 #ifndef CONFIG_PREEMPT
229 * If we are a non preempt kernel, the 10 second run will
230 * stop everything while it runs. Instead, we will call
231 * cond_resched and also add any time that was lost by a
234 * Do a cond resched at the same frequency we would wake up
237 if (cnt % wakeup_interval)
241 } while (end_tv.tv_sec < (start_tv.tv_sec + RUN_TIME) && !kill_test);
242 trace_printk("End ring buffer hammer\n");
245 /* Init both completions here to avoid races */
246 init_completion(&read_start);
247 init_completion(&read_done);
248 /* the completions must be visible before the finish var */
251 /* finish var visible before waking up the consumer */
253 wake_up_process(consumer);
254 wait_for_completion(&read_done);
257 time = end_tv.tv_sec - start_tv.tv_sec;
258 time *= USEC_PER_SEC;
259 time += (long long)((long)end_tv.tv_usec - (long)start_tv.tv_usec);
261 entries = ring_buffer_entries(buffer);
262 overruns = ring_buffer_overruns(buffer);
265 trace_printk("ERROR!\n");
266 trace_printk("Time: %lld (usecs)\n", time);
267 trace_printk("Overruns: %lld\n", overruns);
269 trace_printk("Read: (reader disabled)\n");
271 trace_printk("Read: %ld (by %s)\n", read,
272 read_events ? "events" : "pages");
273 trace_printk("Entries: %lld\n", entries);
274 trace_printk("Total: %lld\n", entries + overruns + read);
275 trace_printk("Missed: %ld\n", missed);
276 trace_printk("Hit: %ld\n", hit);
278 /* Convert time from usecs to millisecs */
279 do_div(time, USEC_PER_MSEC);
283 trace_printk("TIME IS ZERO??\n");
285 trace_printk("Entries per millisec: %ld\n", hit);
288 /* Calculate the average time in nanosecs */
289 avg = NSEC_PER_MSEC / hit;
290 trace_printk("%ld ns per entry\n", avg);
295 missed /= (long)time;
297 trace_printk("Total iterations per millisec: %ld\n",
300 /* it is possible that hit + missed will overflow and be zero */
301 if (!(hit + missed)) {
302 trace_printk("hit + missed overflowed and totalled zero!\n");
303 hit--; /* make it non zero */
306 /* Caculate the average time in nanosecs */
307 avg = NSEC_PER_MSEC / (hit + missed);
308 trace_printk("%ld ns per entry\n", avg);
312 static void wait_to_die(void)
314 set_current_state(TASK_INTERRUPTIBLE);
315 while (!kthread_should_stop()) {
317 set_current_state(TASK_INTERRUPTIBLE);
319 __set_current_state(TASK_RUNNING);
322 static int ring_buffer_consumer_thread(void *arg)
324 while (!kthread_should_stop() && !kill_test) {
325 complete(&read_start);
327 ring_buffer_consumer();
329 set_current_state(TASK_INTERRUPTIBLE);
330 if (kthread_should_stop() || kill_test)
334 __set_current_state(TASK_RUNNING);
336 __set_current_state(TASK_RUNNING);
344 static int ring_buffer_producer_thread(void *arg)
346 init_completion(&read_start);
348 while (!kthread_should_stop() && !kill_test) {
349 ring_buffer_reset(buffer);
353 wake_up_process(consumer);
354 wait_for_completion(&read_start);
357 ring_buffer_producer();
359 trace_printk("Sleeping for 10 secs\n");
360 set_current_state(TASK_INTERRUPTIBLE);
361 schedule_timeout(HZ * SLEEP_TIME);
362 __set_current_state(TASK_RUNNING);
371 static int __init ring_buffer_benchmark_init(void)
375 /* make a one meg buffer in overwite mode */
376 buffer = ring_buffer_alloc(1000000, RB_FL_OVERWRITE);
380 if (!disable_reader) {
381 consumer = kthread_create(ring_buffer_consumer_thread,
382 NULL, "rb_consumer");
383 ret = PTR_ERR(consumer);
384 if (IS_ERR(consumer))
388 producer = kthread_run(ring_buffer_producer_thread,
389 NULL, "rb_producer");
390 ret = PTR_ERR(producer);
392 if (IS_ERR(producer))
399 kthread_stop(consumer);
402 ring_buffer_free(buffer);
406 static void __exit ring_buffer_benchmark_exit(void)
408 kthread_stop(producer);
410 kthread_stop(consumer);
411 ring_buffer_free(buffer);
414 module_init(ring_buffer_benchmark_init);
415 module_exit(ring_buffer_benchmark_exit);
417 MODULE_AUTHOR("Steven Rostedt");
418 MODULE_DESCRIPTION("ring_buffer_benchmark");
419 MODULE_LICENSE("GPL");