Commit | Line | Data |
---|---|---|
bcea3f96 | 1 | // SPDX-License-Identifier: GPL-2.0 |
5092dbc9 SR |
2 | /* |
3 | * ring buffer tester and benchmark | |
4 | * | |
5 | * Copyright (C) 2009 Steven Rostedt <srostedt@redhat.com> | |
6 | */ | |
7 | #include <linux/ring_buffer.h> | |
8 | #include <linux/completion.h> | |
9 | #include <linux/kthread.h> | |
ae7e81c0 | 10 | #include <uapi/linux/sched/types.h> |
5092dbc9 | 11 | #include <linux/module.h> |
da194930 | 12 | #include <linux/ktime.h> |
79615760 | 13 | #include <asm/local.h> |
5092dbc9 SR |
14 | |
15 | struct rb_page { | |
16 | u64 ts; | |
17 | local_t commit; | |
18 | char data[4080]; | |
19 | }; | |
20 | ||
21 | /* run time and sleep time in seconds */ | |
da194930 | 22 | #define RUN_TIME 10ULL |
5092dbc9 SR |
23 | #define SLEEP_TIME 10 |
24 | ||
25 | /* number of events for writer to wake up the reader */ | |
26 | static int wakeup_interval = 100; | |
27 | ||
28 | static int reader_finish; | |
8b46ff69 PM |
29 | static DECLARE_COMPLETION(read_start); |
30 | static DECLARE_COMPLETION(read_done); | |
5092dbc9 | 31 | |
13292494 | 32 | static struct trace_buffer *buffer; |
5092dbc9 SR |
33 | static struct task_struct *producer; |
34 | static struct task_struct *consumer; | |
35 | static unsigned long read; | |
36 | ||
33d657d1 | 37 | static unsigned int disable_reader; |
5092dbc9 SR |
38 | module_param(disable_reader, uint, 0644); |
39 | MODULE_PARM_DESC(disable_reader, "only run producer"); | |
40 | ||
33d657d1 | 41 | static unsigned int write_iteration = 50; |
a6f0eb6a SR |
42 | module_param(write_iteration, uint, 0644); |
43 | MODULE_PARM_DESC(write_iteration, "# of writes between timestamp readings"); | |
44 | ||
2b3942e4 DY |
45 | static int producer_nice = MAX_NICE; |
46 | static int consumer_nice = MAX_NICE; | |
7ac07434 | 47 | |
4fd5750a PZ |
48 | static int producer_fifo; |
49 | static int consumer_fifo; | |
7ac07434 | 50 | |
7364e865 | 51 | module_param(producer_nice, int, 0644); |
7ac07434 SR |
52 | MODULE_PARM_DESC(producer_nice, "nice prio for producer"); |
53 | ||
7364e865 | 54 | module_param(consumer_nice, int, 0644); |
7ac07434 SR |
55 | MODULE_PARM_DESC(consumer_nice, "nice prio for consumer"); |
56 | ||
7364e865 | 57 | module_param(producer_fifo, int, 0644); |
4fd5750a | 58 | MODULE_PARM_DESC(producer_fifo, "use fifo for producer: 0 - disabled, 1 - low prio, 2 - fifo"); |
7ac07434 | 59 | |
7364e865 | 60 | module_param(consumer_fifo, int, 0644); |
4fd5750a | 61 | MODULE_PARM_DESC(consumer_fifo, "use fifo for consumer: 0 - disabled, 1 - low prio, 2 - fifo"); |
7ac07434 | 62 | |
5092dbc9 SR |
63 | static int read_events; |
64 | ||
f47cb66d | 65 | static int test_error; |
5092dbc9 | 66 | |
f47cb66d | 67 | #define TEST_ERROR() \ |
5092dbc9 | 68 | do { \ |
f47cb66d PM |
69 | if (!test_error) { \ |
70 | test_error = 1; \ | |
5092dbc9 SR |
71 | WARN_ON(1); \ |
72 | } \ | |
73 | } while (0) | |
74 | ||
75 | enum event_status { | |
76 | EVENT_FOUND, | |
77 | EVENT_DROPPED, | |
78 | }; | |
79 | ||
f47cb66d PM |
80 | static bool break_test(void) |
81 | { | |
82 | return test_error || kthread_should_stop(); | |
83 | } | |
84 | ||
5092dbc9 SR |
85 | static enum event_status read_event(int cpu) |
86 | { | |
87 | struct ring_buffer_event *event; | |
88 | int *entry; | |
89 | u64 ts; | |
90 | ||
66a8cb95 | 91 | event = ring_buffer_consume(buffer, cpu, &ts, NULL); |
5092dbc9 SR |
92 | if (!event) |
93 | return EVENT_DROPPED; | |
94 | ||
95 | entry = ring_buffer_event_data(event); | |
96 | if (*entry != cpu) { | |
f47cb66d | 97 | TEST_ERROR(); |
5092dbc9 SR |
98 | return EVENT_DROPPED; |
99 | } | |
100 | ||
101 | read++; | |
102 | return EVENT_FOUND; | |
103 | } | |
104 | ||
105 | static enum event_status read_page(int cpu) | |
106 | { | |
107 | struct ring_buffer_event *event; | |
108 | struct rb_page *rpage; | |
109 | unsigned long commit; | |
110 | void *bpage; | |
111 | int *entry; | |
112 | int ret; | |
113 | int inc; | |
114 | int i; | |
115 | ||
7ea59064 | 116 | bpage = ring_buffer_alloc_read_page(buffer, cpu); |
a7e52ad7 | 117 | if (IS_ERR(bpage)) |
00c81a58 SR |
118 | return EVENT_DROPPED; |
119 | ||
5092dbc9 SR |
120 | ret = ring_buffer_read_page(buffer, &bpage, PAGE_SIZE, cpu, 1); |
121 | if (ret >= 0) { | |
122 | rpage = bpage; | |
a838b2e6 SR |
123 | /* The commit may have missed event flags set, clear them */ |
124 | commit = local_read(&rpage->commit) & 0xfffff; | |
f47cb66d | 125 | for (i = 0; i < commit && !test_error ; i += inc) { |
5092dbc9 SR |
126 | |
127 | if (i >= (PAGE_SIZE - offsetof(struct rb_page, data))) { | |
f47cb66d | 128 | TEST_ERROR(); |
5092dbc9 SR |
129 | break; |
130 | } | |
131 | ||
132 | inc = -1; | |
133 | event = (void *)&rpage->data[i]; | |
134 | switch (event->type_len) { | |
135 | case RINGBUF_TYPE_PADDING: | |
9086c7b9 SR |
136 | /* failed writes may be discarded events */ |
137 | if (!event->time_delta) | |
f47cb66d | 138 | TEST_ERROR(); |
9086c7b9 | 139 | inc = event->array[0] + 4; |
5092dbc9 SR |
140 | break; |
141 | case RINGBUF_TYPE_TIME_EXTEND: | |
142 | inc = 8; | |
143 | break; | |
144 | case 0: | |
145 | entry = ring_buffer_event_data(event); | |
146 | if (*entry != cpu) { | |
f47cb66d | 147 | TEST_ERROR(); |
5092dbc9 SR |
148 | break; |
149 | } | |
150 | read++; | |
151 | if (!event->array[0]) { | |
f47cb66d | 152 | TEST_ERROR(); |
5092dbc9 SR |
153 | break; |
154 | } | |
9086c7b9 | 155 | inc = event->array[0] + 4; |
5092dbc9 SR |
156 | break; |
157 | default: | |
158 | entry = ring_buffer_event_data(event); | |
159 | if (*entry != cpu) { | |
f47cb66d | 160 | TEST_ERROR(); |
5092dbc9 SR |
161 | break; |
162 | } | |
163 | read++; | |
164 | inc = ((event->type_len + 1) * 4); | |
165 | } | |
f47cb66d | 166 | if (test_error) |
5092dbc9 SR |
167 | break; |
168 | ||
169 | if (inc <= 0) { | |
f47cb66d | 170 | TEST_ERROR(); |
5092dbc9 SR |
171 | break; |
172 | } | |
173 | } | |
174 | } | |
73a757e6 | 175 | ring_buffer_free_read_page(buffer, cpu, bpage); |
5092dbc9 SR |
176 | |
177 | if (ret < 0) | |
178 | return EVENT_DROPPED; | |
179 | return EVENT_FOUND; | |
180 | } | |
181 | ||
182 | static void ring_buffer_consumer(void) | |
183 | { | |
184 | /* toggle between reading pages and events */ | |
185 | read_events ^= 1; | |
186 | ||
187 | read = 0; | |
8b46ff69 PM |
188 | /* |
189 | * Continue running until the producer specifically asks to stop | |
190 | * and is ready for the completion. | |
191 | */ | |
192 | while (!READ_ONCE(reader_finish)) { | |
193 | int found = 1; | |
5092dbc9 | 194 | |
f47cb66d | 195 | while (found && !test_error) { |
5092dbc9 SR |
196 | int cpu; |
197 | ||
198 | found = 0; | |
199 | for_each_online_cpu(cpu) { | |
200 | enum event_status stat; | |
201 | ||
202 | if (read_events) | |
203 | stat = read_event(cpu); | |
204 | else | |
205 | stat = read_page(cpu); | |
206 | ||
f47cb66d | 207 | if (test_error) |
5092dbc9 | 208 | break; |
8b46ff69 | 209 | |
5092dbc9 SR |
210 | if (stat == EVENT_FOUND) |
211 | found = 1; | |
8b46ff69 | 212 | |
5092dbc9 | 213 | } |
8b46ff69 | 214 | } |
5092dbc9 | 215 | |
8b46ff69 PM |
216 | /* Wait till the producer wakes us up when there is more data |
217 | * available or when the producer wants us to finish reading. | |
218 | */ | |
5092dbc9 SR |
219 | set_current_state(TASK_INTERRUPTIBLE); |
220 | if (reader_finish) | |
221 | break; | |
222 | ||
223 | schedule(); | |
5092dbc9 | 224 | } |
8b46ff69 | 225 | __set_current_state(TASK_RUNNING); |
5092dbc9 SR |
226 | reader_finish = 0; |
227 | complete(&read_done); | |
228 | } | |
229 | ||
230 | static void ring_buffer_producer(void) | |
231 | { | |
da194930 | 232 | ktime_t start_time, end_time, timeout; |
5092dbc9 SR |
233 | unsigned long long time; |
234 | unsigned long long entries; | |
235 | unsigned long long overruns; | |
236 | unsigned long missed = 0; | |
237 | unsigned long hit = 0; | |
238 | unsigned long avg; | |
239 | int cnt = 0; | |
240 | ||
241 | /* | |
242 | * Hammer the buffer for 10 secs (this may | |
243 | * make the system stall) | |
244 | */ | |
4b221f03 | 245 | trace_printk("Starting ring buffer hammer\n"); |
da194930 TR |
246 | start_time = ktime_get(); |
247 | timeout = ktime_add_ns(start_time, RUN_TIME * NSEC_PER_SEC); | |
5092dbc9 SR |
248 | do { |
249 | struct ring_buffer_event *event; | |
250 | int *entry; | |
a6f0eb6a SR |
251 | int i; |
252 | ||
253 | for (i = 0; i < write_iteration; i++) { | |
254 | event = ring_buffer_lock_reserve(buffer, 10); | |
255 | if (!event) { | |
256 | missed++; | |
257 | } else { | |
258 | hit++; | |
259 | entry = ring_buffer_event_data(event); | |
260 | *entry = smp_processor_id(); | |
04aabc32 | 261 | ring_buffer_unlock_commit(buffer); |
a6f0eb6a | 262 | } |
5092dbc9 | 263 | } |
da194930 | 264 | end_time = ktime_get(); |
5092dbc9 | 265 | |
0574ea42 SR |
266 | cnt++; |
267 | if (consumer && !(cnt % wakeup_interval)) | |
5092dbc9 SR |
268 | wake_up_process(consumer); |
269 | ||
30c93704 | 270 | #ifndef CONFIG_PREEMPTION |
29c8000e | 271 | /* |
a82a4804 | 272 | * If we are a non preempt kernel, the 10 seconds run will |
29c8000e SR |
273 | * stop everything while it runs. Instead, we will call |
274 | * cond_resched and also add any time that was lost by a | |
a82a4804 | 275 | * reschedule. |
0574ea42 SR |
276 | * |
277 | * Do a cond resched at the same frequency we would wake up | |
278 | * the reader. | |
29c8000e | 279 | */ |
0574ea42 SR |
280 | if (cnt % wakeup_interval) |
281 | cond_resched(); | |
282 | #endif | |
f47cb66d | 283 | } while (ktime_before(end_time, timeout) && !break_test()); |
4b221f03 | 284 | trace_printk("End ring buffer hammer\n"); |
5092dbc9 SR |
285 | |
286 | if (consumer) { | |
287 | /* Init both completions here to avoid races */ | |
288 | init_completion(&read_start); | |
289 | init_completion(&read_done); | |
290 | /* the completions must be visible before the finish var */ | |
291 | smp_wmb(); | |
292 | reader_finish = 1; | |
5092dbc9 SR |
293 | wake_up_process(consumer); |
294 | wait_for_completion(&read_done); | |
295 | } | |
296 | ||
da194930 | 297 | time = ktime_us_delta(end_time, start_time); |
5092dbc9 SR |
298 | |
299 | entries = ring_buffer_entries(buffer); | |
300 | overruns = ring_buffer_overruns(buffer); | |
301 | ||
f47cb66d | 302 | if (test_error) |
4b221f03 | 303 | trace_printk("ERROR!\n"); |
7ac07434 SR |
304 | |
305 | if (!disable_reader) { | |
4fd5750a PZ |
306 | if (consumer_fifo) |
307 | trace_printk("Running Consumer at SCHED_FIFO %s\n", | |
308 | consumer_fifo == 1 ? "low" : "high"); | |
309 | else | |
7ac07434 SR |
310 | trace_printk("Running Consumer at nice: %d\n", |
311 | consumer_nice); | |
7ac07434 | 312 | } |
4fd5750a PZ |
313 | if (producer_fifo) |
314 | trace_printk("Running Producer at SCHED_FIFO %s\n", | |
315 | producer_fifo == 1 ? "low" : "high"); | |
316 | else | |
7ac07434 SR |
317 | trace_printk("Running Producer at nice: %d\n", |
318 | producer_nice); | |
7ac07434 SR |
319 | |
320 | /* Let the user know that the test is running at low priority */ | |
4fd5750a | 321 | if (!producer_fifo && !consumer_fifo && |
2b3942e4 | 322 | producer_nice == MAX_NICE && consumer_nice == MAX_NICE) |
7ac07434 SR |
323 | trace_printk("WARNING!!! This test is running at lowest priority.\n"); |
324 | ||
4b221f03 SR |
325 | trace_printk("Time: %lld (usecs)\n", time); |
326 | trace_printk("Overruns: %lld\n", overruns); | |
5092dbc9 | 327 | if (disable_reader) |
4b221f03 | 328 | trace_printk("Read: (reader disabled)\n"); |
5092dbc9 | 329 | else |
4b221f03 | 330 | trace_printk("Read: %ld (by %s)\n", read, |
5092dbc9 | 331 | read_events ? "events" : "pages"); |
4b221f03 SR |
332 | trace_printk("Entries: %lld\n", entries); |
333 | trace_printk("Total: %lld\n", entries + overruns + read); | |
334 | trace_printk("Missed: %ld\n", missed); | |
335 | trace_printk("Hit: %ld\n", hit); | |
5092dbc9 | 336 | |
5a772b2b SR |
337 | /* Convert time from usecs to millisecs */ |
338 | do_div(time, USEC_PER_MSEC); | |
5092dbc9 SR |
339 | if (time) |
340 | hit /= (long)time; | |
341 | else | |
4b221f03 | 342 | trace_printk("TIME IS ZERO??\n"); |
5092dbc9 | 343 | |
4b221f03 | 344 | trace_printk("Entries per millisec: %ld\n", hit); |
5092dbc9 SR |
345 | |
346 | if (hit) { | |
5a772b2b SR |
347 | /* Calculate the average time in nanosecs */ |
348 | avg = NSEC_PER_MSEC / hit; | |
4b221f03 | 349 | trace_printk("%ld ns per entry\n", avg); |
5092dbc9 | 350 | } |
7da3046d | 351 | |
7da3046d SR |
352 | if (missed) { |
353 | if (time) | |
354 | missed /= (long)time; | |
355 | ||
4b221f03 SR |
356 | trace_printk("Total iterations per millisec: %ld\n", |
357 | hit + missed); | |
7da3046d | 358 | |
d988ff94 SR |
359 | /* it is possible that hit + missed will overflow and be zero */ |
360 | if (!(hit + missed)) { | |
4b221f03 | 361 | trace_printk("hit + missed overflowed and totalled zero!\n"); |
d988ff94 SR |
362 | hit--; /* make it non zero */ |
363 | } | |
364 | ||
5c173bed | 365 | /* Calculate the average time in nanosecs */ |
5a772b2b | 366 | avg = NSEC_PER_MSEC / (hit + missed); |
4b221f03 | 367 | trace_printk("%ld ns per entry\n", avg); |
7da3046d | 368 | } |
5092dbc9 SR |
369 | } |
370 | ||
371 | static void wait_to_die(void) | |
372 | { | |
373 | set_current_state(TASK_INTERRUPTIBLE); | |
374 | while (!kthread_should_stop()) { | |
375 | schedule(); | |
376 | set_current_state(TASK_INTERRUPTIBLE); | |
377 | } | |
378 | __set_current_state(TASK_RUNNING); | |
379 | } | |
380 | ||
381 | static int ring_buffer_consumer_thread(void *arg) | |
382 | { | |
f47cb66d | 383 | while (!break_test()) { |
5092dbc9 SR |
384 | complete(&read_start); |
385 | ||
386 | ring_buffer_consumer(); | |
387 | ||
388 | set_current_state(TASK_INTERRUPTIBLE); | |
f47cb66d | 389 | if (break_test()) |
5092dbc9 | 390 | break; |
5092dbc9 | 391 | schedule(); |
5092dbc9 SR |
392 | } |
393 | __set_current_state(TASK_RUNNING); | |
394 | ||
b44754d8 | 395 | if (!kthread_should_stop()) |
5092dbc9 SR |
396 | wait_to_die(); |
397 | ||
398 | return 0; | |
399 | } | |
400 | ||
401 | static int ring_buffer_producer_thread(void *arg) | |
402 | { | |
f47cb66d | 403 | while (!break_test()) { |
5092dbc9 SR |
404 | ring_buffer_reset(buffer); |
405 | ||
406 | if (consumer) { | |
5092dbc9 SR |
407 | wake_up_process(consumer); |
408 | wait_for_completion(&read_start); | |
409 | } | |
410 | ||
411 | ring_buffer_producer(); | |
f47cb66d | 412 | if (break_test()) |
b44754d8 | 413 | goto out_kill; |
5092dbc9 | 414 | |
4b221f03 | 415 | trace_printk("Sleeping for 10 secs\n"); |
5092dbc9 | 416 | set_current_state(TASK_INTERRUPTIBLE); |
f47cb66d PM |
417 | if (break_test()) |
418 | goto out_kill; | |
5092dbc9 | 419 | schedule_timeout(HZ * SLEEP_TIME); |
5092dbc9 SR |
420 | } |
421 | ||
b44754d8 | 422 | out_kill: |
f47cb66d | 423 | __set_current_state(TASK_RUNNING); |
b44754d8 | 424 | if (!kthread_should_stop()) |
5092dbc9 SR |
425 | wait_to_die(); |
426 | ||
427 | return 0; | |
428 | } | |
429 | ||
430 | static int __init ring_buffer_benchmark_init(void) | |
431 | { | |
432 | int ret; | |
433 | ||
434 | /* make a one meg buffer in overwite mode */ | |
435 | buffer = ring_buffer_alloc(1000000, RB_FL_OVERWRITE); | |
436 | if (!buffer) | |
437 | return -ENOMEM; | |
438 | ||
439 | if (!disable_reader) { | |
440 | consumer = kthread_create(ring_buffer_consumer_thread, | |
441 | NULL, "rb_consumer"); | |
442 | ret = PTR_ERR(consumer); | |
443 | if (IS_ERR(consumer)) | |
444 | goto out_fail; | |
445 | } | |
446 | ||
447 | producer = kthread_run(ring_buffer_producer_thread, | |
448 | NULL, "rb_producer"); | |
449 | ret = PTR_ERR(producer); | |
450 | ||
451 | if (IS_ERR(producer)) | |
452 | goto out_kill; | |
453 | ||
98e4833b IM |
454 | /* |
455 | * Run them as low-prio background tasks by default: | |
456 | */ | |
7ac07434 | 457 | if (!disable_reader) { |
4fd5750a PZ |
458 | if (consumer_fifo >= 2) |
459 | sched_set_fifo(consumer); | |
460 | else if (consumer_fifo == 1) | |
461 | sched_set_fifo_low(consumer); | |
462 | else | |
7ac07434 SR |
463 | set_user_nice(consumer, consumer_nice); |
464 | } | |
465 | ||
4fd5750a PZ |
466 | if (producer_fifo >= 2) |
467 | sched_set_fifo(producer); | |
468 | else if (producer_fifo == 1) | |
469 | sched_set_fifo_low(producer); | |
470 | else | |
7ac07434 | 471 | set_user_nice(producer, producer_nice); |
98e4833b | 472 | |
5092dbc9 SR |
473 | return 0; |
474 | ||
475 | out_kill: | |
476 | if (consumer) | |
477 | kthread_stop(consumer); | |
478 | ||
479 | out_fail: | |
480 | ring_buffer_free(buffer); | |
481 | return ret; | |
482 | } | |
483 | ||
484 | static void __exit ring_buffer_benchmark_exit(void) | |
485 | { | |
486 | kthread_stop(producer); | |
487 | if (consumer) | |
488 | kthread_stop(consumer); | |
489 | ring_buffer_free(buffer); | |
490 | } | |
491 | ||
492 | module_init(ring_buffer_benchmark_init); | |
493 | module_exit(ring_buffer_benchmark_exit); | |
494 | ||
495 | MODULE_AUTHOR("Steven Rostedt"); | |
496 | MODULE_DESCRIPTION("ring_buffer_benchmark"); | |
497 | MODULE_LICENSE("GPL"); |