Steven Rostedt | 5092dbc | 2009-05-05 22:47:18 -0400 | [diff] [blame] | 1 | /* |
| 2 | * ring buffer tester and benchmark |
| 3 | * |
| 4 | * Copyright (C) 2009 Steven Rostedt <srostedt@redhat.com> |
| 5 | */ |
| 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> |
| 11 | |
| 12 | struct rb_page { |
| 13 | u64 ts; |
| 14 | local_t commit; |
| 15 | char data[4080]; |
| 16 | }; |
| 17 | |
| 18 | /* run time and sleep time in seconds */ |
| 19 | #define RUN_TIME 10 |
| 20 | #define SLEEP_TIME 10 |
| 21 | |
| 22 | /* number of events for writer to wake up the reader */ |
| 23 | static int wakeup_interval = 100; |
| 24 | |
| 25 | static int reader_finish; |
| 26 | static struct completion read_start; |
| 27 | static struct completion read_done; |
| 28 | |
| 29 | static struct ring_buffer *buffer; |
| 30 | static struct task_struct *producer; |
| 31 | static struct task_struct *consumer; |
| 32 | static unsigned long read; |
| 33 | |
| 34 | static int disable_reader; |
| 35 | module_param(disable_reader, uint, 0644); |
| 36 | MODULE_PARM_DESC(disable_reader, "only run producer"); |
| 37 | |
| 38 | static int read_events; |
| 39 | |
| 40 | static int kill_test; |
| 41 | |
| 42 | #define KILL_TEST() \ |
| 43 | do { \ |
| 44 | if (!kill_test) { \ |
| 45 | kill_test = 1; \ |
| 46 | WARN_ON(1); \ |
| 47 | } \ |
| 48 | } while (0) |
| 49 | |
| 50 | enum event_status { |
| 51 | EVENT_FOUND, |
| 52 | EVENT_DROPPED, |
| 53 | }; |
| 54 | |
| 55 | static enum event_status read_event(int cpu) |
| 56 | { |
| 57 | struct ring_buffer_event *event; |
| 58 | int *entry; |
| 59 | u64 ts; |
| 60 | |
| 61 | event = ring_buffer_consume(buffer, cpu, &ts); |
| 62 | if (!event) |
| 63 | return EVENT_DROPPED; |
| 64 | |
| 65 | entry = ring_buffer_event_data(event); |
| 66 | if (*entry != cpu) { |
| 67 | KILL_TEST(); |
| 68 | return EVENT_DROPPED; |
| 69 | } |
| 70 | |
| 71 | read++; |
| 72 | return EVENT_FOUND; |
| 73 | } |
| 74 | |
| 75 | static enum event_status read_page(int cpu) |
| 76 | { |
| 77 | struct ring_buffer_event *event; |
| 78 | struct rb_page *rpage; |
| 79 | unsigned long commit; |
| 80 | void *bpage; |
| 81 | int *entry; |
| 82 | int ret; |
| 83 | int inc; |
| 84 | int i; |
| 85 | |
| 86 | bpage = ring_buffer_alloc_read_page(buffer); |
Steven Rostedt | 00c81a5 | 2009-05-06 12:40:51 -0400 | [diff] [blame^] | 87 | if (!bpage) |
| 88 | return EVENT_DROPPED; |
| 89 | |
Steven Rostedt | 5092dbc | 2009-05-05 22:47:18 -0400 | [diff] [blame] | 90 | ret = ring_buffer_read_page(buffer, &bpage, PAGE_SIZE, cpu, 1); |
| 91 | if (ret >= 0) { |
| 92 | rpage = bpage; |
| 93 | commit = local_read(&rpage->commit); |
| 94 | for (i = 0; i < commit && !kill_test; i += inc) { |
| 95 | |
| 96 | if (i >= (PAGE_SIZE - offsetof(struct rb_page, data))) { |
| 97 | KILL_TEST(); |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | inc = -1; |
| 102 | event = (void *)&rpage->data[i]; |
| 103 | switch (event->type_len) { |
| 104 | case RINGBUF_TYPE_PADDING: |
| 105 | /* We don't expect any padding */ |
| 106 | KILL_TEST(); |
| 107 | break; |
| 108 | case RINGBUF_TYPE_TIME_EXTEND: |
| 109 | inc = 8; |
| 110 | break; |
| 111 | case 0: |
| 112 | entry = ring_buffer_event_data(event); |
| 113 | if (*entry != cpu) { |
| 114 | KILL_TEST(); |
| 115 | break; |
| 116 | } |
| 117 | read++; |
| 118 | if (!event->array[0]) { |
| 119 | KILL_TEST(); |
| 120 | break; |
| 121 | } |
| 122 | inc = event->array[0]; |
| 123 | break; |
| 124 | default: |
| 125 | entry = ring_buffer_event_data(event); |
| 126 | if (*entry != cpu) { |
| 127 | KILL_TEST(); |
| 128 | break; |
| 129 | } |
| 130 | read++; |
| 131 | inc = ((event->type_len + 1) * 4); |
| 132 | } |
| 133 | if (kill_test) |
| 134 | break; |
| 135 | |
| 136 | if (inc <= 0) { |
| 137 | KILL_TEST(); |
| 138 | break; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | ring_buffer_free_read_page(buffer, bpage); |
| 143 | |
| 144 | if (ret < 0) |
| 145 | return EVENT_DROPPED; |
| 146 | return EVENT_FOUND; |
| 147 | } |
| 148 | |
| 149 | static void ring_buffer_consumer(void) |
| 150 | { |
| 151 | /* toggle between reading pages and events */ |
| 152 | read_events ^= 1; |
| 153 | |
| 154 | read = 0; |
| 155 | while (!reader_finish && !kill_test) { |
| 156 | int found; |
| 157 | |
| 158 | do { |
| 159 | int cpu; |
| 160 | |
| 161 | found = 0; |
| 162 | for_each_online_cpu(cpu) { |
| 163 | enum event_status stat; |
| 164 | |
| 165 | if (read_events) |
| 166 | stat = read_event(cpu); |
| 167 | else |
| 168 | stat = read_page(cpu); |
| 169 | |
| 170 | if (kill_test) |
| 171 | break; |
| 172 | if (stat == EVENT_FOUND) |
| 173 | found = 1; |
| 174 | } |
| 175 | } while (found && !kill_test); |
| 176 | |
| 177 | set_current_state(TASK_INTERRUPTIBLE); |
| 178 | if (reader_finish) |
| 179 | break; |
| 180 | |
| 181 | schedule(); |
| 182 | __set_current_state(TASK_RUNNING); |
| 183 | } |
| 184 | reader_finish = 0; |
| 185 | complete(&read_done); |
| 186 | } |
| 187 | |
| 188 | static void ring_buffer_producer(void) |
| 189 | { |
| 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; |
| 197 | unsigned long avg; |
| 198 | int cnt = 0; |
| 199 | |
| 200 | /* |
| 201 | * Hammer the buffer for 10 secs (this may |
| 202 | * make the system stall) |
| 203 | */ |
| 204 | pr_info("Starting ring buffer hammer\n"); |
| 205 | do_gettimeofday(&start_tv); |
| 206 | do { |
| 207 | struct ring_buffer_event *event; |
| 208 | int *entry; |
| 209 | |
| 210 | event = ring_buffer_lock_reserve(buffer, 10); |
| 211 | if (!event) { |
| 212 | missed++; |
| 213 | } else { |
| 214 | hit++; |
| 215 | entry = ring_buffer_event_data(event); |
| 216 | *entry = smp_processor_id(); |
| 217 | ring_buffer_unlock_commit(buffer, event); |
| 218 | } |
| 219 | do_gettimeofday(&end_tv); |
| 220 | |
| 221 | if (consumer && !(++cnt % wakeup_interval)) |
| 222 | wake_up_process(consumer); |
| 223 | |
| 224 | } while (end_tv.tv_sec < (start_tv.tv_sec + RUN_TIME) && !kill_test); |
| 225 | pr_info("End ring buffer hammer\n"); |
| 226 | |
| 227 | if (consumer) { |
| 228 | /* Init both completions here to avoid races */ |
| 229 | init_completion(&read_start); |
| 230 | init_completion(&read_done); |
| 231 | /* the completions must be visible before the finish var */ |
| 232 | smp_wmb(); |
| 233 | reader_finish = 1; |
| 234 | /* finish var visible before waking up the consumer */ |
| 235 | smp_wmb(); |
| 236 | wake_up_process(consumer); |
| 237 | wait_for_completion(&read_done); |
| 238 | } |
| 239 | |
| 240 | time = end_tv.tv_sec - start_tv.tv_sec; |
| 241 | time *= 1000000; |
| 242 | time += (long long)((long)end_tv.tv_usec - (long)start_tv.tv_usec); |
| 243 | |
| 244 | entries = ring_buffer_entries(buffer); |
| 245 | overruns = ring_buffer_overruns(buffer); |
| 246 | |
| 247 | if (kill_test) |
| 248 | pr_info("ERROR!\n"); |
| 249 | pr_info("Time: %lld (usecs)\n", time); |
| 250 | pr_info("Overruns: %lld\n", overruns); |
| 251 | if (disable_reader) |
| 252 | pr_info("Read: (reader disabled)\n"); |
| 253 | else |
| 254 | pr_info("Read: %ld (by %s)\n", read, |
| 255 | read_events ? "events" : "pages"); |
| 256 | pr_info("Entries: %lld\n", entries); |
| 257 | pr_info("Total: %lld\n", entries + overruns + read); |
| 258 | pr_info("Missed: %ld\n", missed); |
| 259 | pr_info("Hit: %ld\n", hit); |
| 260 | |
| 261 | do_div(time, 1000); |
| 262 | if (time) |
| 263 | hit /= (long)time; |
| 264 | else |
| 265 | pr_info("TIME IS ZERO??\n"); |
| 266 | |
| 267 | pr_info("Entries per millisec: %ld\n", hit); |
| 268 | |
| 269 | if (hit) { |
| 270 | avg = 1000000 / hit; |
| 271 | pr_info("%ld ns per entry\n", avg); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | static void wait_to_die(void) |
| 276 | { |
| 277 | set_current_state(TASK_INTERRUPTIBLE); |
| 278 | while (!kthread_should_stop()) { |
| 279 | schedule(); |
| 280 | set_current_state(TASK_INTERRUPTIBLE); |
| 281 | } |
| 282 | __set_current_state(TASK_RUNNING); |
| 283 | } |
| 284 | |
| 285 | static int ring_buffer_consumer_thread(void *arg) |
| 286 | { |
| 287 | while (!kthread_should_stop() && !kill_test) { |
| 288 | complete(&read_start); |
| 289 | |
| 290 | ring_buffer_consumer(); |
| 291 | |
| 292 | set_current_state(TASK_INTERRUPTIBLE); |
| 293 | if (kthread_should_stop() || kill_test) |
| 294 | break; |
| 295 | |
| 296 | schedule(); |
| 297 | __set_current_state(TASK_RUNNING); |
| 298 | } |
| 299 | __set_current_state(TASK_RUNNING); |
| 300 | |
| 301 | if (kill_test) |
| 302 | wait_to_die(); |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static int ring_buffer_producer_thread(void *arg) |
| 308 | { |
| 309 | init_completion(&read_start); |
| 310 | |
| 311 | while (!kthread_should_stop() && !kill_test) { |
| 312 | ring_buffer_reset(buffer); |
| 313 | |
| 314 | if (consumer) { |
| 315 | smp_wmb(); |
| 316 | wake_up_process(consumer); |
| 317 | wait_for_completion(&read_start); |
| 318 | } |
| 319 | |
| 320 | ring_buffer_producer(); |
| 321 | |
| 322 | pr_info("Sleeping for 10 secs\n"); |
| 323 | set_current_state(TASK_INTERRUPTIBLE); |
| 324 | schedule_timeout(HZ * SLEEP_TIME); |
| 325 | __set_current_state(TASK_RUNNING); |
| 326 | } |
| 327 | |
| 328 | if (kill_test) |
| 329 | wait_to_die(); |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static int __init ring_buffer_benchmark_init(void) |
| 335 | { |
| 336 | int ret; |
| 337 | |
| 338 | /* make a one meg buffer in overwite mode */ |
| 339 | buffer = ring_buffer_alloc(1000000, RB_FL_OVERWRITE); |
| 340 | if (!buffer) |
| 341 | return -ENOMEM; |
| 342 | |
| 343 | if (!disable_reader) { |
| 344 | consumer = kthread_create(ring_buffer_consumer_thread, |
| 345 | NULL, "rb_consumer"); |
| 346 | ret = PTR_ERR(consumer); |
| 347 | if (IS_ERR(consumer)) |
| 348 | goto out_fail; |
| 349 | } |
| 350 | |
| 351 | producer = kthread_run(ring_buffer_producer_thread, |
| 352 | NULL, "rb_producer"); |
| 353 | ret = PTR_ERR(producer); |
| 354 | |
| 355 | if (IS_ERR(producer)) |
| 356 | goto out_kill; |
| 357 | |
| 358 | return 0; |
| 359 | |
| 360 | out_kill: |
| 361 | if (consumer) |
| 362 | kthread_stop(consumer); |
| 363 | |
| 364 | out_fail: |
| 365 | ring_buffer_free(buffer); |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | static void __exit ring_buffer_benchmark_exit(void) |
| 370 | { |
| 371 | kthread_stop(producer); |
| 372 | if (consumer) |
| 373 | kthread_stop(consumer); |
| 374 | ring_buffer_free(buffer); |
| 375 | } |
| 376 | |
| 377 | module_init(ring_buffer_benchmark_init); |
| 378 | module_exit(ring_buffer_benchmark_exit); |
| 379 | |
| 380 | MODULE_AUTHOR("Steven Rostedt"); |
| 381 | MODULE_DESCRIPTION("ring_buffer_benchmark"); |
| 382 | MODULE_LICENSE("GPL"); |