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 /* We don't expect any padding */
108 case RINGBUF_TYPE_TIME_EXTEND:
112 entry = ring_buffer_event_data(event);
118 if (!event->array[0]) {
122 inc = event->array[0];
125 entry = ring_buffer_event_data(event);
131 inc = ((event->type_len + 1) * 4);
142 ring_buffer_free_read_page(buffer, bpage);
145 return EVENT_DROPPED;
149 static void ring_buffer_consumer(void)
151 /* toggle between reading pages and events */
155 while (!reader_finish && !kill_test) {
162 for_each_online_cpu(cpu) {
163 enum event_status stat;
166 stat = read_event(cpu);
168 stat = read_page(cpu);
172 if (stat == EVENT_FOUND)
175 } while (found && !kill_test);
177 set_current_state(TASK_INTERRUPTIBLE);
182 __set_current_state(TASK_RUNNING);
185 complete(&read_done);
188 static void ring_buffer_producer(void)
190 struct timeval start_tv;
191 struct timeval end_tv;
192 unsigned long long time;
193 unsigned long long entries;
194 unsigned long long overruns;
195 unsigned long missed = 0;
196 unsigned long hit = 0;
201 * Hammer the buffer for 10 secs (this may
202 * make the system stall)
204 pr_info("Starting ring buffer hammer\n");
205 do_gettimeofday(&start_tv);
207 struct ring_buffer_event *event;
210 event = ring_buffer_lock_reserve(buffer, 10);
215 entry = ring_buffer_event_data(event);
216 *entry = smp_processor_id();
217 ring_buffer_unlock_commit(buffer, event);
219 do_gettimeofday(&end_tv);
222 if (consumer && !(cnt % wakeup_interval))
223 wake_up_process(consumer);
225 #ifndef CONFIG_PREEMPT
227 * If we are a non preempt kernel, the 10 second run will
228 * stop everything while it runs. Instead, we will call
229 * cond_resched and also add any time that was lost by a
232 * Do a cond resched at the same frequency we would wake up
235 if (cnt % wakeup_interval)
239 } while (end_tv.tv_sec < (start_tv.tv_sec + RUN_TIME) && !kill_test);
240 pr_info("End ring buffer hammer\n");
243 /* Init both completions here to avoid races */
244 init_completion(&read_start);
245 init_completion(&read_done);
246 /* the completions must be visible before the finish var */
249 /* finish var visible before waking up the consumer */
251 wake_up_process(consumer);
252 wait_for_completion(&read_done);
255 time = end_tv.tv_sec - start_tv.tv_sec;
256 time *= USEC_PER_SEC;
257 time += (long long)((long)end_tv.tv_usec - (long)start_tv.tv_usec);
259 entries = ring_buffer_entries(buffer);
260 overruns = ring_buffer_overruns(buffer);
264 pr_info("Time: %lld (usecs)\n", time);
265 pr_info("Overruns: %lld\n", overruns);
267 pr_info("Read: (reader disabled)\n");
269 pr_info("Read: %ld (by %s)\n", read,
270 read_events ? "events" : "pages");
271 pr_info("Entries: %lld\n", entries);
272 pr_info("Total: %lld\n", entries + overruns + read);
273 pr_info("Missed: %ld\n", missed);
274 pr_info("Hit: %ld\n", hit);
276 /* Convert time from usecs to millisecs */
277 do_div(time, USEC_PER_MSEC);
281 pr_info("TIME IS ZERO??\n");
283 pr_info("Entries per millisec: %ld\n", hit);
286 /* Calculate the average time in nanosecs */
287 avg = NSEC_PER_MSEC / hit;
288 pr_info("%ld ns per entry\n", avg);
293 missed /= (long)time;
295 pr_info("Total iterations per millisec: %ld\n", hit + missed);
297 /* it is possible that hit + missed will overflow and be zero */
298 if (!(hit + missed)) {
299 pr_info("hit + missed overflowed and totalled zero!\n");
300 hit--; /* make it non zero */
303 /* Caculate the average time in nanosecs */
304 avg = NSEC_PER_MSEC / (hit + missed);
305 pr_info("%ld ns per entry\n", avg);
309 static void wait_to_die(void)
311 set_current_state(TASK_INTERRUPTIBLE);
312 while (!kthread_should_stop()) {
314 set_current_state(TASK_INTERRUPTIBLE);
316 __set_current_state(TASK_RUNNING);
319 static int ring_buffer_consumer_thread(void *arg)
321 while (!kthread_should_stop() && !kill_test) {
322 complete(&read_start);
324 ring_buffer_consumer();
326 set_current_state(TASK_INTERRUPTIBLE);
327 if (kthread_should_stop() || kill_test)
331 __set_current_state(TASK_RUNNING);
333 __set_current_state(TASK_RUNNING);
341 static int ring_buffer_producer_thread(void *arg)
343 init_completion(&read_start);
345 while (!kthread_should_stop() && !kill_test) {
346 ring_buffer_reset(buffer);
350 wake_up_process(consumer);
351 wait_for_completion(&read_start);
354 ring_buffer_producer();
356 pr_info("Sleeping for 10 secs\n");
357 set_current_state(TASK_INTERRUPTIBLE);
358 schedule_timeout(HZ * SLEEP_TIME);
359 __set_current_state(TASK_RUNNING);
368 static int __init ring_buffer_benchmark_init(void)
372 /* make a one meg buffer in overwite mode */
373 buffer = ring_buffer_alloc(1000000, RB_FL_OVERWRITE);
377 if (!disable_reader) {
378 consumer = kthread_create(ring_buffer_consumer_thread,
379 NULL, "rb_consumer");
380 ret = PTR_ERR(consumer);
381 if (IS_ERR(consumer))
385 producer = kthread_run(ring_buffer_producer_thread,
386 NULL, "rb_producer");
387 ret = PTR_ERR(producer);
389 if (IS_ERR(producer))
396 kthread_stop(consumer);
399 ring_buffer_free(buffer);
403 static void __exit ring_buffer_benchmark_exit(void)
405 kthread_stop(producer);
407 kthread_stop(consumer);
408 ring_buffer_free(buffer);
411 module_init(ring_buffer_benchmark_init);
412 module_exit(ring_buffer_benchmark_exit);
414 MODULE_AUTHOR("Steven Rostedt");
415 MODULE_DESCRIPTION("ring_buffer_benchmark");
416 MODULE_LICENSE("GPL");