Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Generic ring buffer |
| 3 | * |
| 4 | * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com> |
| 5 | */ |
| 6 | #include <linux/ring_buffer.h> |
| 7 | #include <linux/spinlock.h> |
| 8 | #include <linux/debugfs.h> |
| 9 | #include <linux/uaccess.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/percpu.h> |
| 12 | #include <linux/mutex.h> |
| 13 | #include <linux/sched.h> /* used for sched_clock() (for now) */ |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/hash.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/fs.h> |
| 18 | |
Steven Rostedt | 182e9f5 | 2008-11-03 23:15:56 -0500 | [diff] [blame] | 19 | #include "trace.h" |
| 20 | |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 21 | /* Global flag to disable all recording to ring buffers */ |
| 22 | static int ring_buffers_off __read_mostly; |
| 23 | |
| 24 | /** |
| 25 | * tracing_on - enable all tracing buffers |
| 26 | * |
| 27 | * This function enables all tracing buffers that may have been |
| 28 | * disabled with tracing_off. |
| 29 | */ |
| 30 | void tracing_on(void) |
| 31 | { |
| 32 | ring_buffers_off = 0; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * tracing_off - turn off all tracing buffers |
| 37 | * |
| 38 | * This function stops all tracing buffers from recording data. |
| 39 | * It does not disable any overhead the tracers themselves may |
| 40 | * be causing. This function simply causes all recording to |
| 41 | * the ring buffers to fail. |
| 42 | */ |
| 43 | void tracing_off(void) |
| 44 | { |
| 45 | ring_buffers_off = 1; |
| 46 | } |
| 47 | |
Ingo Molnar | d06bbd6 | 2008-11-12 10:11:37 +0100 | [diff] [blame] | 48 | #include "trace.h" |
| 49 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 50 | /* Up this if you want to test the TIME_EXTENTS and normalization */ |
| 51 | #define DEBUG_SHIFT 0 |
| 52 | |
| 53 | /* FIXME!!! */ |
| 54 | u64 ring_buffer_time_stamp(int cpu) |
| 55 | { |
| 56 | /* shift to debug/test normalization and TIME_EXTENTS */ |
| 57 | return sched_clock() << DEBUG_SHIFT; |
| 58 | } |
| 59 | |
| 60 | void ring_buffer_normalize_time_stamp(int cpu, u64 *ts) |
| 61 | { |
| 62 | /* Just stupid testing the normalize function and deltas */ |
| 63 | *ts >>= DEBUG_SHIFT; |
| 64 | } |
| 65 | |
| 66 | #define RB_EVNT_HDR_SIZE (sizeof(struct ring_buffer_event)) |
| 67 | #define RB_ALIGNMENT_SHIFT 2 |
| 68 | #define RB_ALIGNMENT (1 << RB_ALIGNMENT_SHIFT) |
| 69 | #define RB_MAX_SMALL_DATA 28 |
| 70 | |
| 71 | enum { |
| 72 | RB_LEN_TIME_EXTEND = 8, |
| 73 | RB_LEN_TIME_STAMP = 16, |
| 74 | }; |
| 75 | |
| 76 | /* inline for ring buffer fast paths */ |
| 77 | static inline unsigned |
| 78 | rb_event_length(struct ring_buffer_event *event) |
| 79 | { |
| 80 | unsigned length; |
| 81 | |
| 82 | switch (event->type) { |
| 83 | case RINGBUF_TYPE_PADDING: |
| 84 | /* undefined */ |
| 85 | return -1; |
| 86 | |
| 87 | case RINGBUF_TYPE_TIME_EXTEND: |
| 88 | return RB_LEN_TIME_EXTEND; |
| 89 | |
| 90 | case RINGBUF_TYPE_TIME_STAMP: |
| 91 | return RB_LEN_TIME_STAMP; |
| 92 | |
| 93 | case RINGBUF_TYPE_DATA: |
| 94 | if (event->len) |
| 95 | length = event->len << RB_ALIGNMENT_SHIFT; |
| 96 | else |
| 97 | length = event->array[0]; |
| 98 | return length + RB_EVNT_HDR_SIZE; |
| 99 | default: |
| 100 | BUG(); |
| 101 | } |
| 102 | /* not hit */ |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * ring_buffer_event_length - return the length of the event |
| 108 | * @event: the event to get the length of |
| 109 | */ |
| 110 | unsigned ring_buffer_event_length(struct ring_buffer_event *event) |
| 111 | { |
| 112 | return rb_event_length(event); |
| 113 | } |
| 114 | |
| 115 | /* inline for ring buffer fast paths */ |
| 116 | static inline void * |
| 117 | rb_event_data(struct ring_buffer_event *event) |
| 118 | { |
| 119 | BUG_ON(event->type != RINGBUF_TYPE_DATA); |
| 120 | /* If length is in len field, then array[0] has the data */ |
| 121 | if (event->len) |
| 122 | return (void *)&event->array[0]; |
| 123 | /* Otherwise length is in array[0] and array[1] has the data */ |
| 124 | return (void *)&event->array[1]; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * ring_buffer_event_data - return the data of the event |
| 129 | * @event: the event to get the data from |
| 130 | */ |
| 131 | void *ring_buffer_event_data(struct ring_buffer_event *event) |
| 132 | { |
| 133 | return rb_event_data(event); |
| 134 | } |
| 135 | |
| 136 | #define for_each_buffer_cpu(buffer, cpu) \ |
| 137 | for_each_cpu_mask(cpu, buffer->cpumask) |
| 138 | |
| 139 | #define TS_SHIFT 27 |
| 140 | #define TS_MASK ((1ULL << TS_SHIFT) - 1) |
| 141 | #define TS_DELTA_TEST (~TS_MASK) |
| 142 | |
| 143 | /* |
| 144 | * This hack stolen from mm/slob.c. |
| 145 | * We can store per page timing information in the page frame of the page. |
| 146 | * Thanks to Peter Zijlstra for suggesting this idea. |
| 147 | */ |
| 148 | struct buffer_page { |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 149 | u64 time_stamp; /* page time stamp */ |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 150 | local_t write; /* index for next write */ |
| 151 | local_t commit; /* write commited index */ |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 152 | unsigned read; /* index for next read */ |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 153 | struct list_head list; /* list of free pages */ |
| 154 | void *page; /* Actual data page */ |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | /* |
Steven Rostedt | ed56829 | 2008-09-29 23:02:40 -0400 | [diff] [blame] | 158 | * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing |
| 159 | * this issue out. |
| 160 | */ |
| 161 | static inline void free_buffer_page(struct buffer_page *bpage) |
| 162 | { |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 163 | if (bpage->page) |
Steven Rostedt | 6ae2a07 | 2008-10-13 10:22:06 -0400 | [diff] [blame] | 164 | free_page((unsigned long)bpage->page); |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 165 | kfree(bpage); |
Steven Rostedt | ed56829 | 2008-09-29 23:02:40 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 169 | * We need to fit the time_stamp delta into 27 bits. |
| 170 | */ |
| 171 | static inline int test_time_stamp(u64 delta) |
| 172 | { |
| 173 | if (delta & TS_DELTA_TEST) |
| 174 | return 1; |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | #define BUF_PAGE_SIZE PAGE_SIZE |
| 179 | |
| 180 | /* |
| 181 | * head_page == tail_page && head == tail then buffer is empty. |
| 182 | */ |
| 183 | struct ring_buffer_per_cpu { |
| 184 | int cpu; |
| 185 | struct ring_buffer *buffer; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 186 | spinlock_t reader_lock; /* serialize readers */ |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 187 | raw_spinlock_t lock; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 188 | struct lock_class_key lock_key; |
| 189 | struct list_head pages; |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 190 | struct buffer_page *head_page; /* read from head */ |
| 191 | struct buffer_page *tail_page; /* write to tail */ |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 192 | struct buffer_page *commit_page; /* commited pages */ |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 193 | struct buffer_page *reader_page; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 194 | unsigned long overrun; |
| 195 | unsigned long entries; |
| 196 | u64 write_stamp; |
| 197 | u64 read_stamp; |
| 198 | atomic_t record_disabled; |
| 199 | }; |
| 200 | |
| 201 | struct ring_buffer { |
| 202 | unsigned long size; |
| 203 | unsigned pages; |
| 204 | unsigned flags; |
| 205 | int cpus; |
| 206 | cpumask_t cpumask; |
| 207 | atomic_t record_disabled; |
| 208 | |
| 209 | struct mutex mutex; |
| 210 | |
| 211 | struct ring_buffer_per_cpu **buffers; |
| 212 | }; |
| 213 | |
| 214 | struct ring_buffer_iter { |
| 215 | struct ring_buffer_per_cpu *cpu_buffer; |
| 216 | unsigned long head; |
| 217 | struct buffer_page *head_page; |
| 218 | u64 read_stamp; |
| 219 | }; |
| 220 | |
Steven Rostedt | f536aaf | 2008-11-10 23:07:30 -0500 | [diff] [blame] | 221 | /* buffer may be either ring_buffer or ring_buffer_per_cpu */ |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 222 | #define RB_WARN_ON(buffer, cond) \ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 223 | ({ \ |
| 224 | int _____ret = unlikely(cond); \ |
| 225 | if (_____ret) { \ |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 226 | atomic_inc(&buffer->record_disabled); \ |
| 227 | WARN_ON(1); \ |
| 228 | } \ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 229 | _____ret; \ |
| 230 | }) |
Steven Rostedt | f536aaf | 2008-11-10 23:07:30 -0500 | [diff] [blame] | 231 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 232 | /** |
| 233 | * check_pages - integrity check of buffer pages |
| 234 | * @cpu_buffer: CPU buffer with pages to test |
| 235 | * |
| 236 | * As a safty measure we check to make sure the data pages have not |
| 237 | * been corrupted. |
| 238 | */ |
| 239 | static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer) |
| 240 | { |
| 241 | struct list_head *head = &cpu_buffer->pages; |
| 242 | struct buffer_page *page, *tmp; |
| 243 | |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 244 | if (RB_WARN_ON(cpu_buffer, head->next->prev != head)) |
| 245 | return -1; |
| 246 | if (RB_WARN_ON(cpu_buffer, head->prev->next != head)) |
| 247 | return -1; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 248 | |
| 249 | list_for_each_entry_safe(page, tmp, head, list) { |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 250 | if (RB_WARN_ON(cpu_buffer, |
| 251 | page->list.next->prev != &page->list)) |
| 252 | return -1; |
| 253 | if (RB_WARN_ON(cpu_buffer, |
| 254 | page->list.prev->next != &page->list)) |
| 255 | return -1; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 261 | static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, |
| 262 | unsigned nr_pages) |
| 263 | { |
| 264 | struct list_head *head = &cpu_buffer->pages; |
| 265 | struct buffer_page *page, *tmp; |
| 266 | unsigned long addr; |
| 267 | LIST_HEAD(pages); |
| 268 | unsigned i; |
| 269 | |
| 270 | for (i = 0; i < nr_pages; i++) { |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 271 | page = kzalloc_node(ALIGN(sizeof(*page), cache_line_size()), |
Steven Rostedt | aa1e0e3b | 2008-10-02 19:18:09 -0400 | [diff] [blame] | 272 | GFP_KERNEL, cpu_to_node(cpu_buffer->cpu)); |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 273 | if (!page) |
| 274 | goto free_pages; |
| 275 | list_add(&page->list, &pages); |
| 276 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 277 | addr = __get_free_page(GFP_KERNEL); |
| 278 | if (!addr) |
| 279 | goto free_pages; |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 280 | page->page = (void *)addr; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | list_splice(&pages, head); |
| 284 | |
| 285 | rb_check_pages(cpu_buffer); |
| 286 | |
| 287 | return 0; |
| 288 | |
| 289 | free_pages: |
| 290 | list_for_each_entry_safe(page, tmp, &pages, list) { |
| 291 | list_del_init(&page->list); |
Steven Rostedt | ed56829 | 2008-09-29 23:02:40 -0400 | [diff] [blame] | 292 | free_buffer_page(page); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 293 | } |
| 294 | return -ENOMEM; |
| 295 | } |
| 296 | |
| 297 | static struct ring_buffer_per_cpu * |
| 298 | rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu) |
| 299 | { |
| 300 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 301 | struct buffer_page *page; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 302 | unsigned long addr; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 303 | int ret; |
| 304 | |
| 305 | cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()), |
| 306 | GFP_KERNEL, cpu_to_node(cpu)); |
| 307 | if (!cpu_buffer) |
| 308 | return NULL; |
| 309 | |
| 310 | cpu_buffer->cpu = cpu; |
| 311 | cpu_buffer->buffer = buffer; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 312 | spin_lock_init(&cpu_buffer->reader_lock); |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 313 | cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 314 | INIT_LIST_HEAD(&cpu_buffer->pages); |
| 315 | |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 316 | page = kzalloc_node(ALIGN(sizeof(*page), cache_line_size()), |
| 317 | GFP_KERNEL, cpu_to_node(cpu)); |
| 318 | if (!page) |
| 319 | goto fail_free_buffer; |
| 320 | |
| 321 | cpu_buffer->reader_page = page; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 322 | addr = __get_free_page(GFP_KERNEL); |
| 323 | if (!addr) |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 324 | goto fail_free_reader; |
| 325 | page->page = (void *)addr; |
| 326 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 327 | INIT_LIST_HEAD(&cpu_buffer->reader_page->list); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 328 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 329 | ret = rb_allocate_pages(cpu_buffer, buffer->pages); |
| 330 | if (ret < 0) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 331 | goto fail_free_reader; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 332 | |
| 333 | cpu_buffer->head_page |
| 334 | = list_entry(cpu_buffer->pages.next, struct buffer_page, list); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 335 | cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 336 | |
| 337 | return cpu_buffer; |
| 338 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 339 | fail_free_reader: |
| 340 | free_buffer_page(cpu_buffer->reader_page); |
| 341 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 342 | fail_free_buffer: |
| 343 | kfree(cpu_buffer); |
| 344 | return NULL; |
| 345 | } |
| 346 | |
| 347 | static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer) |
| 348 | { |
| 349 | struct list_head *head = &cpu_buffer->pages; |
| 350 | struct buffer_page *page, *tmp; |
| 351 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 352 | list_del_init(&cpu_buffer->reader_page->list); |
| 353 | free_buffer_page(cpu_buffer->reader_page); |
| 354 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 355 | list_for_each_entry_safe(page, tmp, head, list) { |
| 356 | list_del_init(&page->list); |
Steven Rostedt | ed56829 | 2008-09-29 23:02:40 -0400 | [diff] [blame] | 357 | free_buffer_page(page); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 358 | } |
| 359 | kfree(cpu_buffer); |
| 360 | } |
| 361 | |
Steven Rostedt | a7b1374 | 2008-09-29 23:02:39 -0400 | [diff] [blame] | 362 | /* |
| 363 | * Causes compile errors if the struct buffer_page gets bigger |
| 364 | * than the struct page. |
| 365 | */ |
| 366 | extern int ring_buffer_page_too_big(void); |
| 367 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 368 | /** |
| 369 | * ring_buffer_alloc - allocate a new ring_buffer |
| 370 | * @size: the size in bytes that is needed. |
| 371 | * @flags: attributes to set for the ring buffer. |
| 372 | * |
| 373 | * Currently the only flag that is available is the RB_FL_OVERWRITE |
| 374 | * flag. This flag means that the buffer will overwrite old data |
| 375 | * when the buffer wraps. If this flag is not set, the buffer will |
| 376 | * drop data when the tail hits the head. |
| 377 | */ |
| 378 | struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags) |
| 379 | { |
| 380 | struct ring_buffer *buffer; |
| 381 | int bsize; |
| 382 | int cpu; |
| 383 | |
Steven Rostedt | a7b1374 | 2008-09-29 23:02:39 -0400 | [diff] [blame] | 384 | /* Paranoid! Optimizes out when all is well */ |
| 385 | if (sizeof(struct buffer_page) > sizeof(struct page)) |
| 386 | ring_buffer_page_too_big(); |
| 387 | |
| 388 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 389 | /* keep it in its own cache line */ |
| 390 | buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()), |
| 391 | GFP_KERNEL); |
| 392 | if (!buffer) |
| 393 | return NULL; |
| 394 | |
| 395 | buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE); |
| 396 | buffer->flags = flags; |
| 397 | |
| 398 | /* need at least two pages */ |
| 399 | if (buffer->pages == 1) |
| 400 | buffer->pages++; |
| 401 | |
| 402 | buffer->cpumask = cpu_possible_map; |
| 403 | buffer->cpus = nr_cpu_ids; |
| 404 | |
| 405 | bsize = sizeof(void *) * nr_cpu_ids; |
| 406 | buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()), |
| 407 | GFP_KERNEL); |
| 408 | if (!buffer->buffers) |
| 409 | goto fail_free_buffer; |
| 410 | |
| 411 | for_each_buffer_cpu(buffer, cpu) { |
| 412 | buffer->buffers[cpu] = |
| 413 | rb_allocate_cpu_buffer(buffer, cpu); |
| 414 | if (!buffer->buffers[cpu]) |
| 415 | goto fail_free_buffers; |
| 416 | } |
| 417 | |
| 418 | mutex_init(&buffer->mutex); |
| 419 | |
| 420 | return buffer; |
| 421 | |
| 422 | fail_free_buffers: |
| 423 | for_each_buffer_cpu(buffer, cpu) { |
| 424 | if (buffer->buffers[cpu]) |
| 425 | rb_free_cpu_buffer(buffer->buffers[cpu]); |
| 426 | } |
| 427 | kfree(buffer->buffers); |
| 428 | |
| 429 | fail_free_buffer: |
| 430 | kfree(buffer); |
| 431 | return NULL; |
| 432 | } |
| 433 | |
| 434 | /** |
| 435 | * ring_buffer_free - free a ring buffer. |
| 436 | * @buffer: the buffer to free. |
| 437 | */ |
| 438 | void |
| 439 | ring_buffer_free(struct ring_buffer *buffer) |
| 440 | { |
| 441 | int cpu; |
| 442 | |
| 443 | for_each_buffer_cpu(buffer, cpu) |
| 444 | rb_free_cpu_buffer(buffer->buffers[cpu]); |
| 445 | |
| 446 | kfree(buffer); |
| 447 | } |
| 448 | |
| 449 | static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer); |
| 450 | |
| 451 | static void |
| 452 | rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages) |
| 453 | { |
| 454 | struct buffer_page *page; |
| 455 | struct list_head *p; |
| 456 | unsigned i; |
| 457 | |
| 458 | atomic_inc(&cpu_buffer->record_disabled); |
| 459 | synchronize_sched(); |
| 460 | |
| 461 | for (i = 0; i < nr_pages; i++) { |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 462 | if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages))) |
| 463 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 464 | p = cpu_buffer->pages.next; |
| 465 | page = list_entry(p, struct buffer_page, list); |
| 466 | list_del_init(&page->list); |
Steven Rostedt | ed56829 | 2008-09-29 23:02:40 -0400 | [diff] [blame] | 467 | free_buffer_page(page); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 468 | } |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 469 | if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages))) |
| 470 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 471 | |
| 472 | rb_reset_cpu(cpu_buffer); |
| 473 | |
| 474 | rb_check_pages(cpu_buffer); |
| 475 | |
| 476 | atomic_dec(&cpu_buffer->record_disabled); |
| 477 | |
| 478 | } |
| 479 | |
| 480 | static void |
| 481 | rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer, |
| 482 | struct list_head *pages, unsigned nr_pages) |
| 483 | { |
| 484 | struct buffer_page *page; |
| 485 | struct list_head *p; |
| 486 | unsigned i; |
| 487 | |
| 488 | atomic_inc(&cpu_buffer->record_disabled); |
| 489 | synchronize_sched(); |
| 490 | |
| 491 | for (i = 0; i < nr_pages; i++) { |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 492 | if (RB_WARN_ON(cpu_buffer, list_empty(pages))) |
| 493 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 494 | p = pages->next; |
| 495 | page = list_entry(p, struct buffer_page, list); |
| 496 | list_del_init(&page->list); |
| 497 | list_add_tail(&page->list, &cpu_buffer->pages); |
| 498 | } |
| 499 | rb_reset_cpu(cpu_buffer); |
| 500 | |
| 501 | rb_check_pages(cpu_buffer); |
| 502 | |
| 503 | atomic_dec(&cpu_buffer->record_disabled); |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * ring_buffer_resize - resize the ring buffer |
| 508 | * @buffer: the buffer to resize. |
| 509 | * @size: the new size. |
| 510 | * |
| 511 | * The tracer is responsible for making sure that the buffer is |
| 512 | * not being used while changing the size. |
| 513 | * Note: We may be able to change the above requirement by using |
| 514 | * RCU synchronizations. |
| 515 | * |
| 516 | * Minimum size is 2 * BUF_PAGE_SIZE. |
| 517 | * |
| 518 | * Returns -1 on failure. |
| 519 | */ |
| 520 | int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size) |
| 521 | { |
| 522 | struct ring_buffer_per_cpu *cpu_buffer; |
| 523 | unsigned nr_pages, rm_pages, new_pages; |
| 524 | struct buffer_page *page, *tmp; |
| 525 | unsigned long buffer_size; |
| 526 | unsigned long addr; |
| 527 | LIST_HEAD(pages); |
| 528 | int i, cpu; |
| 529 | |
| 530 | size = DIV_ROUND_UP(size, BUF_PAGE_SIZE); |
| 531 | size *= BUF_PAGE_SIZE; |
| 532 | buffer_size = buffer->pages * BUF_PAGE_SIZE; |
| 533 | |
| 534 | /* we need a minimum of two pages */ |
| 535 | if (size < BUF_PAGE_SIZE * 2) |
| 536 | size = BUF_PAGE_SIZE * 2; |
| 537 | |
| 538 | if (size == buffer_size) |
| 539 | return size; |
| 540 | |
| 541 | mutex_lock(&buffer->mutex); |
| 542 | |
| 543 | nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE); |
| 544 | |
| 545 | if (size < buffer_size) { |
| 546 | |
| 547 | /* easy case, just free pages */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 548 | if (RB_WARN_ON(buffer, nr_pages >= buffer->pages)) { |
| 549 | mutex_unlock(&buffer->mutex); |
| 550 | return -1; |
| 551 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 552 | |
| 553 | rm_pages = buffer->pages - nr_pages; |
| 554 | |
| 555 | for_each_buffer_cpu(buffer, cpu) { |
| 556 | cpu_buffer = buffer->buffers[cpu]; |
| 557 | rb_remove_pages(cpu_buffer, rm_pages); |
| 558 | } |
| 559 | goto out; |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * This is a bit more difficult. We only want to add pages |
| 564 | * when we can allocate enough for all CPUs. We do this |
| 565 | * by allocating all the pages and storing them on a local |
| 566 | * link list. If we succeed in our allocation, then we |
| 567 | * add these pages to the cpu_buffers. Otherwise we just free |
| 568 | * them all and return -ENOMEM; |
| 569 | */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 570 | if (RB_WARN_ON(buffer, nr_pages <= buffer->pages)) { |
| 571 | mutex_unlock(&buffer->mutex); |
| 572 | return -1; |
| 573 | } |
Steven Rostedt | f536aaf | 2008-11-10 23:07:30 -0500 | [diff] [blame] | 574 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 575 | new_pages = nr_pages - buffer->pages; |
| 576 | |
| 577 | for_each_buffer_cpu(buffer, cpu) { |
| 578 | for (i = 0; i < new_pages; i++) { |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 579 | page = kzalloc_node(ALIGN(sizeof(*page), |
| 580 | cache_line_size()), |
| 581 | GFP_KERNEL, cpu_to_node(cpu)); |
| 582 | if (!page) |
| 583 | goto free_pages; |
| 584 | list_add(&page->list, &pages); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 585 | addr = __get_free_page(GFP_KERNEL); |
| 586 | if (!addr) |
| 587 | goto free_pages; |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 588 | page->page = (void *)addr; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
| 592 | for_each_buffer_cpu(buffer, cpu) { |
| 593 | cpu_buffer = buffer->buffers[cpu]; |
| 594 | rb_insert_pages(cpu_buffer, &pages, new_pages); |
| 595 | } |
| 596 | |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 597 | if (RB_WARN_ON(buffer, !list_empty(&pages))) { |
| 598 | mutex_unlock(&buffer->mutex); |
| 599 | return -1; |
| 600 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 601 | |
| 602 | out: |
| 603 | buffer->pages = nr_pages; |
| 604 | mutex_unlock(&buffer->mutex); |
| 605 | |
| 606 | return size; |
| 607 | |
| 608 | free_pages: |
| 609 | list_for_each_entry_safe(page, tmp, &pages, list) { |
| 610 | list_del_init(&page->list); |
Steven Rostedt | ed56829 | 2008-09-29 23:02:40 -0400 | [diff] [blame] | 611 | free_buffer_page(page); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 612 | } |
| 613 | return -ENOMEM; |
| 614 | } |
| 615 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 616 | static inline int rb_null_event(struct ring_buffer_event *event) |
| 617 | { |
| 618 | return event->type == RINGBUF_TYPE_PADDING; |
| 619 | } |
| 620 | |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 621 | static inline void *__rb_page_index(struct buffer_page *page, unsigned index) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 622 | { |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 623 | return page->page + index; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | static inline struct ring_buffer_event * |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 627 | rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 628 | { |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 629 | return __rb_page_index(cpu_buffer->reader_page, |
| 630 | cpu_buffer->reader_page->read); |
| 631 | } |
| 632 | |
| 633 | static inline struct ring_buffer_event * |
| 634 | rb_head_event(struct ring_buffer_per_cpu *cpu_buffer) |
| 635 | { |
| 636 | return __rb_page_index(cpu_buffer->head_page, |
| 637 | cpu_buffer->head_page->read); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | static inline struct ring_buffer_event * |
| 641 | rb_iter_head_event(struct ring_buffer_iter *iter) |
| 642 | { |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 643 | return __rb_page_index(iter->head_page, iter->head); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 644 | } |
| 645 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 646 | static inline unsigned rb_page_write(struct buffer_page *bpage) |
| 647 | { |
| 648 | return local_read(&bpage->write); |
| 649 | } |
| 650 | |
| 651 | static inline unsigned rb_page_commit(struct buffer_page *bpage) |
| 652 | { |
| 653 | return local_read(&bpage->commit); |
| 654 | } |
| 655 | |
| 656 | /* Size is determined by what has been commited */ |
| 657 | static inline unsigned rb_page_size(struct buffer_page *bpage) |
| 658 | { |
| 659 | return rb_page_commit(bpage); |
| 660 | } |
| 661 | |
| 662 | static inline unsigned |
| 663 | rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer) |
| 664 | { |
| 665 | return rb_page_commit(cpu_buffer->commit_page); |
| 666 | } |
| 667 | |
| 668 | static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer) |
| 669 | { |
| 670 | return rb_page_commit(cpu_buffer->head_page); |
| 671 | } |
| 672 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 673 | /* |
| 674 | * When the tail hits the head and the buffer is in overwrite mode, |
| 675 | * the head jumps to the next page and all content on the previous |
| 676 | * page is discarded. But before doing so, we update the overrun |
| 677 | * variable of the buffer. |
| 678 | */ |
| 679 | static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer) |
| 680 | { |
| 681 | struct ring_buffer_event *event; |
| 682 | unsigned long head; |
| 683 | |
| 684 | for (head = 0; head < rb_head_size(cpu_buffer); |
| 685 | head += rb_event_length(event)) { |
| 686 | |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 687 | event = __rb_page_index(cpu_buffer->head_page, head); |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 688 | if (RB_WARN_ON(cpu_buffer, rb_null_event(event))) |
| 689 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 690 | /* Only count data entries */ |
| 691 | if (event->type != RINGBUF_TYPE_DATA) |
| 692 | continue; |
| 693 | cpu_buffer->overrun++; |
| 694 | cpu_buffer->entries--; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer, |
| 699 | struct buffer_page **page) |
| 700 | { |
| 701 | struct list_head *p = (*page)->list.next; |
| 702 | |
| 703 | if (p == &cpu_buffer->pages) |
| 704 | p = p->next; |
| 705 | |
| 706 | *page = list_entry(p, struct buffer_page, list); |
| 707 | } |
| 708 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 709 | static inline unsigned |
| 710 | rb_event_index(struct ring_buffer_event *event) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 711 | { |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 712 | unsigned long addr = (unsigned long)event; |
| 713 | |
| 714 | return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 715 | } |
| 716 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 717 | static inline int |
| 718 | rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer, |
| 719 | struct ring_buffer_event *event) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 720 | { |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 721 | unsigned long addr = (unsigned long)event; |
| 722 | unsigned long index; |
| 723 | |
| 724 | index = rb_event_index(event); |
| 725 | addr &= PAGE_MASK; |
| 726 | |
| 727 | return cpu_buffer->commit_page->page == (void *)addr && |
| 728 | rb_commit_index(cpu_buffer) == index; |
| 729 | } |
| 730 | |
| 731 | static inline void |
| 732 | rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer, |
| 733 | struct ring_buffer_event *event) |
| 734 | { |
| 735 | unsigned long addr = (unsigned long)event; |
| 736 | unsigned long index; |
| 737 | |
| 738 | index = rb_event_index(event); |
| 739 | addr &= PAGE_MASK; |
| 740 | |
| 741 | while (cpu_buffer->commit_page->page != (void *)addr) { |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 742 | if (RB_WARN_ON(cpu_buffer, |
| 743 | cpu_buffer->commit_page == cpu_buffer->tail_page)) |
| 744 | return; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 745 | cpu_buffer->commit_page->commit = |
| 746 | cpu_buffer->commit_page->write; |
| 747 | rb_inc_page(cpu_buffer, &cpu_buffer->commit_page); |
| 748 | cpu_buffer->write_stamp = cpu_buffer->commit_page->time_stamp; |
| 749 | } |
| 750 | |
| 751 | /* Now set the commit to the event's index */ |
| 752 | local_set(&cpu_buffer->commit_page->commit, index); |
| 753 | } |
| 754 | |
| 755 | static inline void |
| 756 | rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer) |
| 757 | { |
| 758 | /* |
| 759 | * We only race with interrupts and NMIs on this CPU. |
| 760 | * If we own the commit event, then we can commit |
| 761 | * all others that interrupted us, since the interruptions |
| 762 | * are in stack format (they finish before they come |
| 763 | * back to us). This allows us to do a simple loop to |
| 764 | * assign the commit to the tail. |
| 765 | */ |
| 766 | while (cpu_buffer->commit_page != cpu_buffer->tail_page) { |
| 767 | cpu_buffer->commit_page->commit = |
| 768 | cpu_buffer->commit_page->write; |
| 769 | rb_inc_page(cpu_buffer, &cpu_buffer->commit_page); |
| 770 | cpu_buffer->write_stamp = cpu_buffer->commit_page->time_stamp; |
| 771 | /* add barrier to keep gcc from optimizing too much */ |
| 772 | barrier(); |
| 773 | } |
| 774 | while (rb_commit_index(cpu_buffer) != |
| 775 | rb_page_write(cpu_buffer->commit_page)) { |
| 776 | cpu_buffer->commit_page->commit = |
| 777 | cpu_buffer->commit_page->write; |
| 778 | barrier(); |
| 779 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 780 | } |
| 781 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 782 | static void rb_reset_reader_page(struct ring_buffer_per_cpu *cpu_buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 783 | { |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 784 | cpu_buffer->read_stamp = cpu_buffer->reader_page->time_stamp; |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 785 | cpu_buffer->reader_page->read = 0; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | static inline void rb_inc_iter(struct ring_buffer_iter *iter) |
| 789 | { |
| 790 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 791 | |
| 792 | /* |
| 793 | * The iterator could be on the reader page (it starts there). |
| 794 | * But the head could have moved, since the reader was |
| 795 | * found. Check for this case and assign the iterator |
| 796 | * to the head page instead of next. |
| 797 | */ |
| 798 | if (iter->head_page == cpu_buffer->reader_page) |
| 799 | iter->head_page = cpu_buffer->head_page; |
| 800 | else |
| 801 | rb_inc_page(cpu_buffer, &iter->head_page); |
| 802 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 803 | iter->read_stamp = iter->head_page->time_stamp; |
| 804 | iter->head = 0; |
| 805 | } |
| 806 | |
| 807 | /** |
| 808 | * ring_buffer_update_event - update event type and data |
| 809 | * @event: the even to update |
| 810 | * @type: the type of event |
| 811 | * @length: the size of the event field in the ring buffer |
| 812 | * |
| 813 | * Update the type and data fields of the event. The length |
| 814 | * is the actual size that is written to the ring buffer, |
| 815 | * and with this, we can determine what to place into the |
| 816 | * data field. |
| 817 | */ |
| 818 | static inline void |
| 819 | rb_update_event(struct ring_buffer_event *event, |
| 820 | unsigned type, unsigned length) |
| 821 | { |
| 822 | event->type = type; |
| 823 | |
| 824 | switch (type) { |
| 825 | |
| 826 | case RINGBUF_TYPE_PADDING: |
| 827 | break; |
| 828 | |
| 829 | case RINGBUF_TYPE_TIME_EXTEND: |
| 830 | event->len = |
| 831 | (RB_LEN_TIME_EXTEND + (RB_ALIGNMENT-1)) |
| 832 | >> RB_ALIGNMENT_SHIFT; |
| 833 | break; |
| 834 | |
| 835 | case RINGBUF_TYPE_TIME_STAMP: |
| 836 | event->len = |
| 837 | (RB_LEN_TIME_STAMP + (RB_ALIGNMENT-1)) |
| 838 | >> RB_ALIGNMENT_SHIFT; |
| 839 | break; |
| 840 | |
| 841 | case RINGBUF_TYPE_DATA: |
| 842 | length -= RB_EVNT_HDR_SIZE; |
| 843 | if (length > RB_MAX_SMALL_DATA) { |
| 844 | event->len = 0; |
| 845 | event->array[0] = length; |
| 846 | } else |
| 847 | event->len = |
| 848 | (length + (RB_ALIGNMENT-1)) |
| 849 | >> RB_ALIGNMENT_SHIFT; |
| 850 | break; |
| 851 | default: |
| 852 | BUG(); |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | static inline unsigned rb_calculate_event_length(unsigned length) |
| 857 | { |
| 858 | struct ring_buffer_event event; /* Used only for sizeof array */ |
| 859 | |
| 860 | /* zero length can cause confusions */ |
| 861 | if (!length) |
| 862 | length = 1; |
| 863 | |
| 864 | if (length > RB_MAX_SMALL_DATA) |
| 865 | length += sizeof(event.array[0]); |
| 866 | |
| 867 | length += RB_EVNT_HDR_SIZE; |
| 868 | length = ALIGN(length, RB_ALIGNMENT); |
| 869 | |
| 870 | return length; |
| 871 | } |
| 872 | |
| 873 | static struct ring_buffer_event * |
| 874 | __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer, |
| 875 | unsigned type, unsigned long length, u64 *ts) |
| 876 | { |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 877 | struct buffer_page *tail_page, *head_page, *reader_page; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 878 | unsigned long tail, write; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 879 | struct ring_buffer *buffer = cpu_buffer->buffer; |
| 880 | struct ring_buffer_event *event; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 881 | unsigned long flags; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 882 | |
| 883 | tail_page = cpu_buffer->tail_page; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 884 | write = local_add_return(length, &tail_page->write); |
| 885 | tail = write - length; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 886 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 887 | /* See if we shot pass the end of this buffer page */ |
| 888 | if (write > BUF_PAGE_SIZE) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 889 | struct buffer_page *next_page = tail_page; |
| 890 | |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 891 | local_irq_save(flags); |
| 892 | __raw_spin_lock(&cpu_buffer->lock); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 893 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 894 | rb_inc_page(cpu_buffer, &next_page); |
| 895 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 896 | head_page = cpu_buffer->head_page; |
| 897 | reader_page = cpu_buffer->reader_page; |
| 898 | |
| 899 | /* we grabbed the lock before incrementing */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 900 | if (RB_WARN_ON(cpu_buffer, next_page == reader_page)) |
| 901 | goto out_unlock; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 902 | |
| 903 | /* |
| 904 | * If for some reason, we had an interrupt storm that made |
| 905 | * it all the way around the buffer, bail, and warn |
| 906 | * about it. |
| 907 | */ |
| 908 | if (unlikely(next_page == cpu_buffer->commit_page)) { |
| 909 | WARN_ON_ONCE(1); |
| 910 | goto out_unlock; |
| 911 | } |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 912 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 913 | if (next_page == head_page) { |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 914 | if (!(buffer->flags & RB_FL_OVERWRITE)) { |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 915 | /* reset write */ |
| 916 | if (tail <= BUF_PAGE_SIZE) |
| 917 | local_set(&tail_page->write, tail); |
| 918 | goto out_unlock; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 919 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 920 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 921 | /* tail_page has not moved yet? */ |
| 922 | if (tail_page == cpu_buffer->tail_page) { |
| 923 | /* count overflows */ |
| 924 | rb_update_overflow(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 925 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 926 | rb_inc_page(cpu_buffer, &head_page); |
| 927 | cpu_buffer->head_page = head_page; |
| 928 | cpu_buffer->head_page->read = 0; |
| 929 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 930 | } |
| 931 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 932 | /* |
| 933 | * If the tail page is still the same as what we think |
| 934 | * it is, then it is up to us to update the tail |
| 935 | * pointer. |
| 936 | */ |
| 937 | if (tail_page == cpu_buffer->tail_page) { |
| 938 | local_set(&next_page->write, 0); |
| 939 | local_set(&next_page->commit, 0); |
| 940 | cpu_buffer->tail_page = next_page; |
| 941 | |
| 942 | /* reread the time stamp */ |
| 943 | *ts = ring_buffer_time_stamp(cpu_buffer->cpu); |
| 944 | cpu_buffer->tail_page->time_stamp = *ts; |
| 945 | } |
| 946 | |
| 947 | /* |
| 948 | * The actual tail page has moved forward. |
| 949 | */ |
| 950 | if (tail < BUF_PAGE_SIZE) { |
| 951 | /* Mark the rest of the page with padding */ |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 952 | event = __rb_page_index(tail_page, tail); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 953 | event->type = RINGBUF_TYPE_PADDING; |
| 954 | } |
| 955 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 956 | if (tail <= BUF_PAGE_SIZE) |
| 957 | /* Set the write back to the previous setting */ |
| 958 | local_set(&tail_page->write, tail); |
| 959 | |
| 960 | /* |
| 961 | * If this was a commit entry that failed, |
| 962 | * increment that too |
| 963 | */ |
| 964 | if (tail_page == cpu_buffer->commit_page && |
| 965 | tail == rb_commit_index(cpu_buffer)) { |
| 966 | rb_set_commit_to_write(cpu_buffer); |
| 967 | } |
| 968 | |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 969 | __raw_spin_unlock(&cpu_buffer->lock); |
| 970 | local_irq_restore(flags); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 971 | |
| 972 | /* fail and let the caller try again */ |
| 973 | return ERR_PTR(-EAGAIN); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 974 | } |
| 975 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 976 | /* We reserved something on the buffer */ |
| 977 | |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 978 | if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE)) |
| 979 | return NULL; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 980 | |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 981 | event = __rb_page_index(tail_page, tail); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 982 | rb_update_event(event, type, length); |
| 983 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 984 | /* |
| 985 | * If this is a commit and the tail is zero, then update |
| 986 | * this page's time stamp. |
| 987 | */ |
| 988 | if (!tail && rb_is_commit(cpu_buffer, event)) |
| 989 | cpu_buffer->commit_page->time_stamp = *ts; |
| 990 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 991 | return event; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 992 | |
| 993 | out_unlock: |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 994 | __raw_spin_unlock(&cpu_buffer->lock); |
| 995 | local_irq_restore(flags); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 996 | return NULL; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | static int |
| 1000 | rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer, |
| 1001 | u64 *ts, u64 *delta) |
| 1002 | { |
| 1003 | struct ring_buffer_event *event; |
| 1004 | static int once; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1005 | int ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1006 | |
| 1007 | if (unlikely(*delta > (1ULL << 59) && !once++)) { |
| 1008 | printk(KERN_WARNING "Delta way too big! %llu" |
| 1009 | " ts=%llu write stamp = %llu\n", |
Stephen Rothwell | e2862c9 | 2008-10-27 17:43:28 +1100 | [diff] [blame] | 1010 | (unsigned long long)*delta, |
| 1011 | (unsigned long long)*ts, |
| 1012 | (unsigned long long)cpu_buffer->write_stamp); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1013 | WARN_ON(1); |
| 1014 | } |
| 1015 | |
| 1016 | /* |
| 1017 | * The delta is too big, we to add a |
| 1018 | * new timestamp. |
| 1019 | */ |
| 1020 | event = __rb_reserve_next(cpu_buffer, |
| 1021 | RINGBUF_TYPE_TIME_EXTEND, |
| 1022 | RB_LEN_TIME_EXTEND, |
| 1023 | ts); |
| 1024 | if (!event) |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1025 | return -EBUSY; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1026 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1027 | if (PTR_ERR(event) == -EAGAIN) |
| 1028 | return -EAGAIN; |
| 1029 | |
| 1030 | /* Only a commited time event can update the write stamp */ |
| 1031 | if (rb_is_commit(cpu_buffer, event)) { |
| 1032 | /* |
| 1033 | * If this is the first on the page, then we need to |
| 1034 | * update the page itself, and just put in a zero. |
| 1035 | */ |
| 1036 | if (rb_event_index(event)) { |
| 1037 | event->time_delta = *delta & TS_MASK; |
| 1038 | event->array[0] = *delta >> TS_SHIFT; |
| 1039 | } else { |
| 1040 | cpu_buffer->commit_page->time_stamp = *ts; |
| 1041 | event->time_delta = 0; |
| 1042 | event->array[0] = 0; |
| 1043 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1044 | cpu_buffer->write_stamp = *ts; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1045 | /* let the caller know this was the commit */ |
| 1046 | ret = 1; |
| 1047 | } else { |
| 1048 | /* Darn, this is just wasted space */ |
| 1049 | event->time_delta = 0; |
| 1050 | event->array[0] = 0; |
| 1051 | ret = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1052 | } |
| 1053 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1054 | *delta = 0; |
| 1055 | |
| 1056 | return ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | static struct ring_buffer_event * |
| 1060 | rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer, |
| 1061 | unsigned type, unsigned long length) |
| 1062 | { |
| 1063 | struct ring_buffer_event *event; |
| 1064 | u64 ts, delta; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1065 | int commit = 0; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 1066 | int nr_loops = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1067 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1068 | again: |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 1069 | /* |
| 1070 | * We allow for interrupts to reenter here and do a trace. |
| 1071 | * If one does, it will cause this original code to loop |
| 1072 | * back here. Even with heavy interrupts happening, this |
| 1073 | * should only happen a few times in a row. If this happens |
| 1074 | * 1000 times in a row, there must be either an interrupt |
| 1075 | * storm or we have something buggy. |
| 1076 | * Bail! |
| 1077 | */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1078 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000)) |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 1079 | return NULL; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 1080 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1081 | ts = ring_buffer_time_stamp(cpu_buffer->cpu); |
| 1082 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1083 | /* |
| 1084 | * Only the first commit can update the timestamp. |
| 1085 | * Yes there is a race here. If an interrupt comes in |
| 1086 | * just after the conditional and it traces too, then it |
| 1087 | * will also check the deltas. More than one timestamp may |
| 1088 | * also be made. But only the entry that did the actual |
| 1089 | * commit will be something other than zero. |
| 1090 | */ |
| 1091 | if (cpu_buffer->tail_page == cpu_buffer->commit_page && |
| 1092 | rb_page_write(cpu_buffer->tail_page) == |
| 1093 | rb_commit_index(cpu_buffer)) { |
| 1094 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1095 | delta = ts - cpu_buffer->write_stamp; |
| 1096 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1097 | /* make sure this delta is calculated here */ |
| 1098 | barrier(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1099 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1100 | /* Did the write stamp get updated already? */ |
| 1101 | if (unlikely(ts < cpu_buffer->write_stamp)) |
Steven Rostedt | 4143c5c | 2008-11-10 21:46:01 -0500 | [diff] [blame] | 1102 | delta = 0; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1103 | |
| 1104 | if (test_time_stamp(delta)) { |
| 1105 | |
| 1106 | commit = rb_add_time_stamp(cpu_buffer, &ts, &delta); |
| 1107 | |
| 1108 | if (commit == -EBUSY) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1109 | return NULL; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1110 | |
| 1111 | if (commit == -EAGAIN) |
| 1112 | goto again; |
| 1113 | |
| 1114 | RB_WARN_ON(cpu_buffer, commit < 0); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1115 | } |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1116 | } else |
| 1117 | /* Non commits have zero deltas */ |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1118 | delta = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1119 | |
| 1120 | event = __rb_reserve_next(cpu_buffer, type, length, &ts); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1121 | if (PTR_ERR(event) == -EAGAIN) |
| 1122 | goto again; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1123 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1124 | if (!event) { |
| 1125 | if (unlikely(commit)) |
| 1126 | /* |
| 1127 | * Ouch! We needed a timestamp and it was commited. But |
| 1128 | * we didn't get our event reserved. |
| 1129 | */ |
| 1130 | rb_set_commit_to_write(cpu_buffer); |
| 1131 | return NULL; |
| 1132 | } |
| 1133 | |
| 1134 | /* |
| 1135 | * If the timestamp was commited, make the commit our entry |
| 1136 | * now so that we will update it when needed. |
| 1137 | */ |
| 1138 | if (commit) |
| 1139 | rb_set_commit_event(cpu_buffer, event); |
| 1140 | else if (!rb_is_commit(cpu_buffer, event)) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1141 | delta = 0; |
| 1142 | |
| 1143 | event->time_delta = delta; |
| 1144 | |
| 1145 | return event; |
| 1146 | } |
| 1147 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1148 | static DEFINE_PER_CPU(int, rb_need_resched); |
| 1149 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1150 | /** |
| 1151 | * ring_buffer_lock_reserve - reserve a part of the buffer |
| 1152 | * @buffer: the ring buffer to reserve from |
| 1153 | * @length: the length of the data to reserve (excluding event header) |
| 1154 | * @flags: a pointer to save the interrupt flags |
| 1155 | * |
| 1156 | * Returns a reseverd event on the ring buffer to copy directly to. |
| 1157 | * The user of this interface will need to get the body to write into |
| 1158 | * and can use the ring_buffer_event_data() interface. |
| 1159 | * |
| 1160 | * The length is the length of the data needed, not the event length |
| 1161 | * which also includes the event header. |
| 1162 | * |
| 1163 | * Must be paired with ring_buffer_unlock_commit, unless NULL is returned. |
| 1164 | * If NULL is returned, then nothing has been allocated or locked. |
| 1165 | */ |
| 1166 | struct ring_buffer_event * |
| 1167 | ring_buffer_lock_reserve(struct ring_buffer *buffer, |
| 1168 | unsigned long length, |
| 1169 | unsigned long *flags) |
| 1170 | { |
| 1171 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1172 | struct ring_buffer_event *event; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1173 | int cpu, resched; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1174 | |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 1175 | if (ring_buffers_off) |
| 1176 | return NULL; |
| 1177 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1178 | if (atomic_read(&buffer->record_disabled)) |
| 1179 | return NULL; |
| 1180 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1181 | /* If we are tracing schedule, we don't want to recurse */ |
Steven Rostedt | 182e9f5 | 2008-11-03 23:15:56 -0500 | [diff] [blame] | 1182 | resched = ftrace_preempt_disable(); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1183 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1184 | cpu = raw_smp_processor_id(); |
| 1185 | |
| 1186 | if (!cpu_isset(cpu, buffer->cpumask)) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1187 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1188 | |
| 1189 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1190 | |
| 1191 | if (atomic_read(&cpu_buffer->record_disabled)) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1192 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1193 | |
| 1194 | length = rb_calculate_event_length(length); |
| 1195 | if (length > BUF_PAGE_SIZE) |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1196 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1197 | |
| 1198 | event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length); |
| 1199 | if (!event) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1200 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1201 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1202 | /* |
| 1203 | * Need to store resched state on this cpu. |
| 1204 | * Only the first needs to. |
| 1205 | */ |
| 1206 | |
| 1207 | if (preempt_count() == 1) |
| 1208 | per_cpu(rb_need_resched, cpu) = resched; |
| 1209 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1210 | return event; |
| 1211 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1212 | out: |
Steven Rostedt | 182e9f5 | 2008-11-03 23:15:56 -0500 | [diff] [blame] | 1213 | ftrace_preempt_enable(resched); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1214 | return NULL; |
| 1215 | } |
| 1216 | |
| 1217 | static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer, |
| 1218 | struct ring_buffer_event *event) |
| 1219 | { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1220 | cpu_buffer->entries++; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1221 | |
| 1222 | /* Only process further if we own the commit */ |
| 1223 | if (!rb_is_commit(cpu_buffer, event)) |
| 1224 | return; |
| 1225 | |
| 1226 | cpu_buffer->write_stamp += event->time_delta; |
| 1227 | |
| 1228 | rb_set_commit_to_write(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | /** |
| 1232 | * ring_buffer_unlock_commit - commit a reserved |
| 1233 | * @buffer: The buffer to commit to |
| 1234 | * @event: The event pointer to commit. |
| 1235 | * @flags: the interrupt flags received from ring_buffer_lock_reserve. |
| 1236 | * |
| 1237 | * This commits the data to the ring buffer, and releases any locks held. |
| 1238 | * |
| 1239 | * Must be paired with ring_buffer_lock_reserve. |
| 1240 | */ |
| 1241 | int ring_buffer_unlock_commit(struct ring_buffer *buffer, |
| 1242 | struct ring_buffer_event *event, |
| 1243 | unsigned long flags) |
| 1244 | { |
| 1245 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1246 | int cpu = raw_smp_processor_id(); |
| 1247 | |
| 1248 | cpu_buffer = buffer->buffers[cpu]; |
| 1249 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1250 | rb_commit(cpu_buffer, event); |
| 1251 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1252 | /* |
| 1253 | * Only the last preempt count needs to restore preemption. |
| 1254 | */ |
Steven Rostedt | 182e9f5 | 2008-11-03 23:15:56 -0500 | [diff] [blame] | 1255 | if (preempt_count() == 1) |
| 1256 | ftrace_preempt_enable(per_cpu(rb_need_resched, cpu)); |
| 1257 | else |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1258 | preempt_enable_no_resched_notrace(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1259 | |
| 1260 | return 0; |
| 1261 | } |
| 1262 | |
| 1263 | /** |
| 1264 | * ring_buffer_write - write data to the buffer without reserving |
| 1265 | * @buffer: The ring buffer to write to. |
| 1266 | * @length: The length of the data being written (excluding the event header) |
| 1267 | * @data: The data to write to the buffer. |
| 1268 | * |
| 1269 | * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as |
| 1270 | * one function. If you already have the data to write to the buffer, it |
| 1271 | * may be easier to simply call this function. |
| 1272 | * |
| 1273 | * Note, like ring_buffer_lock_reserve, the length is the length of the data |
| 1274 | * and not the length of the event which would hold the header. |
| 1275 | */ |
| 1276 | int ring_buffer_write(struct ring_buffer *buffer, |
| 1277 | unsigned long length, |
| 1278 | void *data) |
| 1279 | { |
| 1280 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1281 | struct ring_buffer_event *event; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1282 | unsigned long event_length; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1283 | void *body; |
| 1284 | int ret = -EBUSY; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1285 | int cpu, resched; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1286 | |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 1287 | if (ring_buffers_off) |
| 1288 | return -EBUSY; |
| 1289 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1290 | if (atomic_read(&buffer->record_disabled)) |
| 1291 | return -EBUSY; |
| 1292 | |
Steven Rostedt | 182e9f5 | 2008-11-03 23:15:56 -0500 | [diff] [blame] | 1293 | resched = ftrace_preempt_disable(); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1294 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1295 | cpu = raw_smp_processor_id(); |
| 1296 | |
| 1297 | if (!cpu_isset(cpu, buffer->cpumask)) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1298 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1299 | |
| 1300 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1301 | |
| 1302 | if (atomic_read(&cpu_buffer->record_disabled)) |
| 1303 | goto out; |
| 1304 | |
| 1305 | event_length = rb_calculate_event_length(length); |
| 1306 | event = rb_reserve_next_event(cpu_buffer, |
| 1307 | RINGBUF_TYPE_DATA, event_length); |
| 1308 | if (!event) |
| 1309 | goto out; |
| 1310 | |
| 1311 | body = rb_event_data(event); |
| 1312 | |
| 1313 | memcpy(body, data, length); |
| 1314 | |
| 1315 | rb_commit(cpu_buffer, event); |
| 1316 | |
| 1317 | ret = 0; |
| 1318 | out: |
Steven Rostedt | 182e9f5 | 2008-11-03 23:15:56 -0500 | [diff] [blame] | 1319 | ftrace_preempt_enable(resched); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1320 | |
| 1321 | return ret; |
| 1322 | } |
| 1323 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1324 | static inline int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer) |
| 1325 | { |
| 1326 | struct buffer_page *reader = cpu_buffer->reader_page; |
| 1327 | struct buffer_page *head = cpu_buffer->head_page; |
| 1328 | struct buffer_page *commit = cpu_buffer->commit_page; |
| 1329 | |
| 1330 | return reader->read == rb_page_commit(reader) && |
| 1331 | (commit == reader || |
| 1332 | (commit == head && |
| 1333 | head->read == rb_page_commit(commit))); |
| 1334 | } |
| 1335 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1336 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1337 | * ring_buffer_record_disable - stop all writes into the buffer |
| 1338 | * @buffer: The ring buffer to stop writes to. |
| 1339 | * |
| 1340 | * This prevents all writes to the buffer. Any attempt to write |
| 1341 | * to the buffer after this will fail and return NULL. |
| 1342 | * |
| 1343 | * The caller should call synchronize_sched() after this. |
| 1344 | */ |
| 1345 | void ring_buffer_record_disable(struct ring_buffer *buffer) |
| 1346 | { |
| 1347 | atomic_inc(&buffer->record_disabled); |
| 1348 | } |
| 1349 | |
| 1350 | /** |
| 1351 | * ring_buffer_record_enable - enable writes to the buffer |
| 1352 | * @buffer: The ring buffer to enable writes |
| 1353 | * |
| 1354 | * Note, multiple disables will need the same number of enables |
| 1355 | * to truely enable the writing (much like preempt_disable). |
| 1356 | */ |
| 1357 | void ring_buffer_record_enable(struct ring_buffer *buffer) |
| 1358 | { |
| 1359 | atomic_dec(&buffer->record_disabled); |
| 1360 | } |
| 1361 | |
| 1362 | /** |
| 1363 | * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer |
| 1364 | * @buffer: The ring buffer to stop writes to. |
| 1365 | * @cpu: The CPU buffer to stop |
| 1366 | * |
| 1367 | * This prevents all writes to the buffer. Any attempt to write |
| 1368 | * to the buffer after this will fail and return NULL. |
| 1369 | * |
| 1370 | * The caller should call synchronize_sched() after this. |
| 1371 | */ |
| 1372 | void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu) |
| 1373 | { |
| 1374 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1375 | |
| 1376 | if (!cpu_isset(cpu, buffer->cpumask)) |
| 1377 | return; |
| 1378 | |
| 1379 | cpu_buffer = buffer->buffers[cpu]; |
| 1380 | atomic_inc(&cpu_buffer->record_disabled); |
| 1381 | } |
| 1382 | |
| 1383 | /** |
| 1384 | * ring_buffer_record_enable_cpu - enable writes to the buffer |
| 1385 | * @buffer: The ring buffer to enable writes |
| 1386 | * @cpu: The CPU to enable. |
| 1387 | * |
| 1388 | * Note, multiple disables will need the same number of enables |
| 1389 | * to truely enable the writing (much like preempt_disable). |
| 1390 | */ |
| 1391 | void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu) |
| 1392 | { |
| 1393 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1394 | |
| 1395 | if (!cpu_isset(cpu, buffer->cpumask)) |
| 1396 | return; |
| 1397 | |
| 1398 | cpu_buffer = buffer->buffers[cpu]; |
| 1399 | atomic_dec(&cpu_buffer->record_disabled); |
| 1400 | } |
| 1401 | |
| 1402 | /** |
| 1403 | * ring_buffer_entries_cpu - get the number of entries in a cpu buffer |
| 1404 | * @buffer: The ring buffer |
| 1405 | * @cpu: The per CPU buffer to get the entries from. |
| 1406 | */ |
| 1407 | unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu) |
| 1408 | { |
| 1409 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1410 | |
| 1411 | if (!cpu_isset(cpu, buffer->cpumask)) |
| 1412 | return 0; |
| 1413 | |
| 1414 | cpu_buffer = buffer->buffers[cpu]; |
| 1415 | return cpu_buffer->entries; |
| 1416 | } |
| 1417 | |
| 1418 | /** |
| 1419 | * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer |
| 1420 | * @buffer: The ring buffer |
| 1421 | * @cpu: The per CPU buffer to get the number of overruns from |
| 1422 | */ |
| 1423 | unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu) |
| 1424 | { |
| 1425 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1426 | |
| 1427 | if (!cpu_isset(cpu, buffer->cpumask)) |
| 1428 | return 0; |
| 1429 | |
| 1430 | cpu_buffer = buffer->buffers[cpu]; |
| 1431 | return cpu_buffer->overrun; |
| 1432 | } |
| 1433 | |
| 1434 | /** |
| 1435 | * ring_buffer_entries - get the number of entries in a buffer |
| 1436 | * @buffer: The ring buffer |
| 1437 | * |
| 1438 | * Returns the total number of entries in the ring buffer |
| 1439 | * (all CPU entries) |
| 1440 | */ |
| 1441 | unsigned long ring_buffer_entries(struct ring_buffer *buffer) |
| 1442 | { |
| 1443 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1444 | unsigned long entries = 0; |
| 1445 | int cpu; |
| 1446 | |
| 1447 | /* if you care about this being correct, lock the buffer */ |
| 1448 | for_each_buffer_cpu(buffer, cpu) { |
| 1449 | cpu_buffer = buffer->buffers[cpu]; |
| 1450 | entries += cpu_buffer->entries; |
| 1451 | } |
| 1452 | |
| 1453 | return entries; |
| 1454 | } |
| 1455 | |
| 1456 | /** |
| 1457 | * ring_buffer_overrun_cpu - get the number of overruns in buffer |
| 1458 | * @buffer: The ring buffer |
| 1459 | * |
| 1460 | * Returns the total number of overruns in the ring buffer |
| 1461 | * (all CPU entries) |
| 1462 | */ |
| 1463 | unsigned long ring_buffer_overruns(struct ring_buffer *buffer) |
| 1464 | { |
| 1465 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1466 | unsigned long overruns = 0; |
| 1467 | int cpu; |
| 1468 | |
| 1469 | /* if you care about this being correct, lock the buffer */ |
| 1470 | for_each_buffer_cpu(buffer, cpu) { |
| 1471 | cpu_buffer = buffer->buffers[cpu]; |
| 1472 | overruns += cpu_buffer->overrun; |
| 1473 | } |
| 1474 | |
| 1475 | return overruns; |
| 1476 | } |
| 1477 | |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame^] | 1478 | static void rb_iter_reset(struct ring_buffer_iter *iter) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1479 | { |
| 1480 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 1481 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1482 | /* Iterator usage is expected to have record disabled */ |
| 1483 | if (list_empty(&cpu_buffer->reader_page->list)) { |
| 1484 | iter->head_page = cpu_buffer->head_page; |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 1485 | iter->head = cpu_buffer->head_page->read; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1486 | } else { |
| 1487 | iter->head_page = cpu_buffer->reader_page; |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 1488 | iter->head = cpu_buffer->reader_page->read; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1489 | } |
| 1490 | if (iter->head) |
| 1491 | iter->read_stamp = cpu_buffer->read_stamp; |
| 1492 | else |
| 1493 | iter->read_stamp = iter->head_page->time_stamp; |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame^] | 1494 | } |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 1495 | |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame^] | 1496 | /** |
| 1497 | * ring_buffer_iter_reset - reset an iterator |
| 1498 | * @iter: The iterator to reset |
| 1499 | * |
| 1500 | * Resets the iterator, so that it will start from the beginning |
| 1501 | * again. |
| 1502 | */ |
| 1503 | void ring_buffer_iter_reset(struct ring_buffer_iter *iter) |
| 1504 | { |
| 1505 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 1506 | unsigned long flags; |
| 1507 | |
| 1508 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
| 1509 | rb_iter_reset(iter); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 1510 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | /** |
| 1514 | * ring_buffer_iter_empty - check if an iterator has no more to read |
| 1515 | * @iter: The iterator to check |
| 1516 | */ |
| 1517 | int ring_buffer_iter_empty(struct ring_buffer_iter *iter) |
| 1518 | { |
| 1519 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1520 | |
| 1521 | cpu_buffer = iter->cpu_buffer; |
| 1522 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1523 | return iter->head_page == cpu_buffer->commit_page && |
| 1524 | iter->head == rb_commit_index(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | static void |
| 1528 | rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer, |
| 1529 | struct ring_buffer_event *event) |
| 1530 | { |
| 1531 | u64 delta; |
| 1532 | |
| 1533 | switch (event->type) { |
| 1534 | case RINGBUF_TYPE_PADDING: |
| 1535 | return; |
| 1536 | |
| 1537 | case RINGBUF_TYPE_TIME_EXTEND: |
| 1538 | delta = event->array[0]; |
| 1539 | delta <<= TS_SHIFT; |
| 1540 | delta += event->time_delta; |
| 1541 | cpu_buffer->read_stamp += delta; |
| 1542 | return; |
| 1543 | |
| 1544 | case RINGBUF_TYPE_TIME_STAMP: |
| 1545 | /* FIXME: not implemented */ |
| 1546 | return; |
| 1547 | |
| 1548 | case RINGBUF_TYPE_DATA: |
| 1549 | cpu_buffer->read_stamp += event->time_delta; |
| 1550 | return; |
| 1551 | |
| 1552 | default: |
| 1553 | BUG(); |
| 1554 | } |
| 1555 | return; |
| 1556 | } |
| 1557 | |
| 1558 | static void |
| 1559 | rb_update_iter_read_stamp(struct ring_buffer_iter *iter, |
| 1560 | struct ring_buffer_event *event) |
| 1561 | { |
| 1562 | u64 delta; |
| 1563 | |
| 1564 | switch (event->type) { |
| 1565 | case RINGBUF_TYPE_PADDING: |
| 1566 | return; |
| 1567 | |
| 1568 | case RINGBUF_TYPE_TIME_EXTEND: |
| 1569 | delta = event->array[0]; |
| 1570 | delta <<= TS_SHIFT; |
| 1571 | delta += event->time_delta; |
| 1572 | iter->read_stamp += delta; |
| 1573 | return; |
| 1574 | |
| 1575 | case RINGBUF_TYPE_TIME_STAMP: |
| 1576 | /* FIXME: not implemented */ |
| 1577 | return; |
| 1578 | |
| 1579 | case RINGBUF_TYPE_DATA: |
| 1580 | iter->read_stamp += event->time_delta; |
| 1581 | return; |
| 1582 | |
| 1583 | default: |
| 1584 | BUG(); |
| 1585 | } |
| 1586 | return; |
| 1587 | } |
| 1588 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1589 | static struct buffer_page * |
| 1590 | rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1591 | { |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1592 | struct buffer_page *reader = NULL; |
| 1593 | unsigned long flags; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 1594 | int nr_loops = 0; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1595 | |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 1596 | local_irq_save(flags); |
| 1597 | __raw_spin_lock(&cpu_buffer->lock); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1598 | |
| 1599 | again: |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 1600 | /* |
| 1601 | * This should normally only loop twice. But because the |
| 1602 | * start of the reader inserts an empty page, it causes |
| 1603 | * a case where we will loop three times. There should be no |
| 1604 | * reason to loop four times (that I know of). |
| 1605 | */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1606 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) { |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 1607 | reader = NULL; |
| 1608 | goto out; |
| 1609 | } |
| 1610 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1611 | reader = cpu_buffer->reader_page; |
| 1612 | |
| 1613 | /* If there's more to read, return this page */ |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1614 | if (cpu_buffer->reader_page->read < rb_page_size(reader)) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1615 | goto out; |
| 1616 | |
| 1617 | /* Never should we have an index greater than the size */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1618 | if (RB_WARN_ON(cpu_buffer, |
| 1619 | cpu_buffer->reader_page->read > rb_page_size(reader))) |
| 1620 | goto out; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1621 | |
| 1622 | /* check if we caught up to the tail */ |
| 1623 | reader = NULL; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1624 | if (cpu_buffer->commit_page == cpu_buffer->reader_page) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1625 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1626 | |
| 1627 | /* |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1628 | * Splice the empty reader page into the list around the head. |
| 1629 | * Reset the reader page to size zero. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1630 | */ |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1631 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1632 | reader = cpu_buffer->head_page; |
| 1633 | cpu_buffer->reader_page->list.next = reader->list.next; |
| 1634 | cpu_buffer->reader_page->list.prev = reader->list.prev; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1635 | |
| 1636 | local_set(&cpu_buffer->reader_page->write, 0); |
| 1637 | local_set(&cpu_buffer->reader_page->commit, 0); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1638 | |
| 1639 | /* Make the reader page now replace the head */ |
| 1640 | reader->list.prev->next = &cpu_buffer->reader_page->list; |
| 1641 | reader->list.next->prev = &cpu_buffer->reader_page->list; |
| 1642 | |
| 1643 | /* |
| 1644 | * If the tail is on the reader, then we must set the head |
| 1645 | * to the inserted page, otherwise we set it one before. |
| 1646 | */ |
| 1647 | cpu_buffer->head_page = cpu_buffer->reader_page; |
| 1648 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1649 | if (cpu_buffer->commit_page != reader) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1650 | rb_inc_page(cpu_buffer, &cpu_buffer->head_page); |
| 1651 | |
| 1652 | /* Finally update the reader page to the new head */ |
| 1653 | cpu_buffer->reader_page = reader; |
| 1654 | rb_reset_reader_page(cpu_buffer); |
| 1655 | |
| 1656 | goto again; |
| 1657 | |
| 1658 | out: |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 1659 | __raw_spin_unlock(&cpu_buffer->lock); |
| 1660 | local_irq_restore(flags); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1661 | |
| 1662 | return reader; |
| 1663 | } |
| 1664 | |
| 1665 | static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer) |
| 1666 | { |
| 1667 | struct ring_buffer_event *event; |
| 1668 | struct buffer_page *reader; |
| 1669 | unsigned length; |
| 1670 | |
| 1671 | reader = rb_get_reader_page(cpu_buffer); |
| 1672 | |
| 1673 | /* This function should not be called when buffer is empty */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1674 | if (RB_WARN_ON(cpu_buffer, !reader)) |
| 1675 | return; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1676 | |
| 1677 | event = rb_reader_event(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1678 | |
| 1679 | if (event->type == RINGBUF_TYPE_DATA) |
| 1680 | cpu_buffer->entries--; |
| 1681 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1682 | rb_update_read_stamp(cpu_buffer, event); |
| 1683 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1684 | length = rb_event_length(event); |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 1685 | cpu_buffer->reader_page->read += length; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | static void rb_advance_iter(struct ring_buffer_iter *iter) |
| 1689 | { |
| 1690 | struct ring_buffer *buffer; |
| 1691 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1692 | struct ring_buffer_event *event; |
| 1693 | unsigned length; |
| 1694 | |
| 1695 | cpu_buffer = iter->cpu_buffer; |
| 1696 | buffer = cpu_buffer->buffer; |
| 1697 | |
| 1698 | /* |
| 1699 | * Check if we are at the end of the buffer. |
| 1700 | */ |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1701 | if (iter->head >= rb_page_size(iter->head_page)) { |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1702 | if (RB_WARN_ON(buffer, |
| 1703 | iter->head_page == cpu_buffer->commit_page)) |
| 1704 | return; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1705 | rb_inc_iter(iter); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1706 | return; |
| 1707 | } |
| 1708 | |
| 1709 | event = rb_iter_head_event(iter); |
| 1710 | |
| 1711 | length = rb_event_length(event); |
| 1712 | |
| 1713 | /* |
| 1714 | * This should not be called to advance the header if we are |
| 1715 | * at the tail of the buffer. |
| 1716 | */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1717 | if (RB_WARN_ON(cpu_buffer, |
Steven Rostedt | f536aaf | 2008-11-10 23:07:30 -0500 | [diff] [blame] | 1718 | (iter->head_page == cpu_buffer->commit_page) && |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1719 | (iter->head + length > rb_commit_index(cpu_buffer)))) |
| 1720 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1721 | |
| 1722 | rb_update_iter_read_stamp(iter, event); |
| 1723 | |
| 1724 | iter->head += length; |
| 1725 | |
| 1726 | /* check for end of page padding */ |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1727 | if ((iter->head >= rb_page_size(iter->head_page)) && |
| 1728 | (iter->head_page != cpu_buffer->commit_page)) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1729 | rb_advance_iter(iter); |
| 1730 | } |
| 1731 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 1732 | static struct ring_buffer_event * |
| 1733 | rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1734 | { |
| 1735 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1736 | struct ring_buffer_event *event; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1737 | struct buffer_page *reader; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 1738 | int nr_loops = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1739 | |
| 1740 | if (!cpu_isset(cpu, buffer->cpumask)) |
| 1741 | return NULL; |
| 1742 | |
| 1743 | cpu_buffer = buffer->buffers[cpu]; |
| 1744 | |
| 1745 | again: |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 1746 | /* |
| 1747 | * We repeat when a timestamp is encountered. It is possible |
| 1748 | * to get multiple timestamps from an interrupt entering just |
| 1749 | * as one timestamp is about to be written. The max times |
| 1750 | * that this can happen is the number of nested interrupts we |
| 1751 | * can have. Nesting 10 deep of interrupts is clearly |
| 1752 | * an anomaly. |
| 1753 | */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1754 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10)) |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 1755 | return NULL; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 1756 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1757 | reader = rb_get_reader_page(cpu_buffer); |
| 1758 | if (!reader) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1759 | return NULL; |
| 1760 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1761 | event = rb_reader_event(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1762 | |
| 1763 | switch (event->type) { |
| 1764 | case RINGBUF_TYPE_PADDING: |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1765 | RB_WARN_ON(cpu_buffer, 1); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1766 | rb_advance_reader(cpu_buffer); |
| 1767 | return NULL; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1768 | |
| 1769 | case RINGBUF_TYPE_TIME_EXTEND: |
| 1770 | /* Internal data, OK to advance */ |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1771 | rb_advance_reader(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1772 | goto again; |
| 1773 | |
| 1774 | case RINGBUF_TYPE_TIME_STAMP: |
| 1775 | /* FIXME: not implemented */ |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1776 | rb_advance_reader(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1777 | goto again; |
| 1778 | |
| 1779 | case RINGBUF_TYPE_DATA: |
| 1780 | if (ts) { |
| 1781 | *ts = cpu_buffer->read_stamp + event->time_delta; |
| 1782 | ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts); |
| 1783 | } |
| 1784 | return event; |
| 1785 | |
| 1786 | default: |
| 1787 | BUG(); |
| 1788 | } |
| 1789 | |
| 1790 | return NULL; |
| 1791 | } |
| 1792 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 1793 | static struct ring_buffer_event * |
| 1794 | rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1795 | { |
| 1796 | struct ring_buffer *buffer; |
| 1797 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1798 | struct ring_buffer_event *event; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 1799 | int nr_loops = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1800 | |
| 1801 | if (ring_buffer_iter_empty(iter)) |
| 1802 | return NULL; |
| 1803 | |
| 1804 | cpu_buffer = iter->cpu_buffer; |
| 1805 | buffer = cpu_buffer->buffer; |
| 1806 | |
| 1807 | again: |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 1808 | /* |
| 1809 | * We repeat when a timestamp is encountered. It is possible |
| 1810 | * to get multiple timestamps from an interrupt entering just |
| 1811 | * as one timestamp is about to be written. The max times |
| 1812 | * that this can happen is the number of nested interrupts we |
| 1813 | * can have. Nesting 10 deep of interrupts is clearly |
| 1814 | * an anomaly. |
| 1815 | */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1816 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10)) |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 1817 | return NULL; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 1818 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1819 | if (rb_per_cpu_empty(cpu_buffer)) |
| 1820 | return NULL; |
| 1821 | |
| 1822 | event = rb_iter_head_event(iter); |
| 1823 | |
| 1824 | switch (event->type) { |
| 1825 | case RINGBUF_TYPE_PADDING: |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1826 | rb_inc_iter(iter); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1827 | goto again; |
| 1828 | |
| 1829 | case RINGBUF_TYPE_TIME_EXTEND: |
| 1830 | /* Internal data, OK to advance */ |
| 1831 | rb_advance_iter(iter); |
| 1832 | goto again; |
| 1833 | |
| 1834 | case RINGBUF_TYPE_TIME_STAMP: |
| 1835 | /* FIXME: not implemented */ |
| 1836 | rb_advance_iter(iter); |
| 1837 | goto again; |
| 1838 | |
| 1839 | case RINGBUF_TYPE_DATA: |
| 1840 | if (ts) { |
| 1841 | *ts = iter->read_stamp + event->time_delta; |
| 1842 | ring_buffer_normalize_time_stamp(cpu_buffer->cpu, ts); |
| 1843 | } |
| 1844 | return event; |
| 1845 | |
| 1846 | default: |
| 1847 | BUG(); |
| 1848 | } |
| 1849 | |
| 1850 | return NULL; |
| 1851 | } |
| 1852 | |
| 1853 | /** |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 1854 | * ring_buffer_peek - peek at the next event to be read |
| 1855 | * @buffer: The ring buffer to read |
| 1856 | * @cpu: The cpu to peak at |
| 1857 | * @ts: The timestamp counter of this event. |
| 1858 | * |
| 1859 | * This will return the event that will be read next, but does |
| 1860 | * not consume the data. |
| 1861 | */ |
| 1862 | struct ring_buffer_event * |
| 1863 | ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts) |
| 1864 | { |
| 1865 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; |
| 1866 | struct ring_buffer_event *event; |
| 1867 | unsigned long flags; |
| 1868 | |
| 1869 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
| 1870 | event = rb_buffer_peek(buffer, cpu, ts); |
| 1871 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
| 1872 | |
| 1873 | return event; |
| 1874 | } |
| 1875 | |
| 1876 | /** |
| 1877 | * ring_buffer_iter_peek - peek at the next event to be read |
| 1878 | * @iter: The ring buffer iterator |
| 1879 | * @ts: The timestamp counter of this event. |
| 1880 | * |
| 1881 | * This will return the event that will be read next, but does |
| 1882 | * not increment the iterator. |
| 1883 | */ |
| 1884 | struct ring_buffer_event * |
| 1885 | ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts) |
| 1886 | { |
| 1887 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 1888 | struct ring_buffer_event *event; |
| 1889 | unsigned long flags; |
| 1890 | |
| 1891 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
| 1892 | event = rb_iter_peek(iter, ts); |
| 1893 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
| 1894 | |
| 1895 | return event; |
| 1896 | } |
| 1897 | |
| 1898 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1899 | * ring_buffer_consume - return an event and consume it |
| 1900 | * @buffer: The ring buffer to get the next event from |
| 1901 | * |
| 1902 | * Returns the next event in the ring buffer, and that event is consumed. |
| 1903 | * Meaning, that sequential reads will keep returning a different event, |
| 1904 | * and eventually empty the ring buffer if the producer is slower. |
| 1905 | */ |
| 1906 | struct ring_buffer_event * |
| 1907 | ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts) |
| 1908 | { |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 1909 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1910 | struct ring_buffer_event *event; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 1911 | unsigned long flags; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1912 | |
| 1913 | if (!cpu_isset(cpu, buffer->cpumask)) |
| 1914 | return NULL; |
| 1915 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 1916 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1917 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 1918 | event = rb_buffer_peek(buffer, cpu, ts); |
| 1919 | if (!event) |
| 1920 | goto out; |
| 1921 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1922 | rb_advance_reader(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1923 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 1924 | out: |
| 1925 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
| 1926 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1927 | return event; |
| 1928 | } |
| 1929 | |
| 1930 | /** |
| 1931 | * ring_buffer_read_start - start a non consuming read of the buffer |
| 1932 | * @buffer: The ring buffer to read from |
| 1933 | * @cpu: The cpu buffer to iterate over |
| 1934 | * |
| 1935 | * This starts up an iteration through the buffer. It also disables |
| 1936 | * the recording to the buffer until the reading is finished. |
| 1937 | * This prevents the reading from being corrupted. This is not |
| 1938 | * a consuming read, so a producer is not expected. |
| 1939 | * |
| 1940 | * Must be paired with ring_buffer_finish. |
| 1941 | */ |
| 1942 | struct ring_buffer_iter * |
| 1943 | ring_buffer_read_start(struct ring_buffer *buffer, int cpu) |
| 1944 | { |
| 1945 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1946 | struct ring_buffer_iter *iter; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1947 | unsigned long flags; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1948 | |
| 1949 | if (!cpu_isset(cpu, buffer->cpumask)) |
| 1950 | return NULL; |
| 1951 | |
| 1952 | iter = kmalloc(sizeof(*iter), GFP_KERNEL); |
| 1953 | if (!iter) |
| 1954 | return NULL; |
| 1955 | |
| 1956 | cpu_buffer = buffer->buffers[cpu]; |
| 1957 | |
| 1958 | iter->cpu_buffer = cpu_buffer; |
| 1959 | |
| 1960 | atomic_inc(&cpu_buffer->record_disabled); |
| 1961 | synchronize_sched(); |
| 1962 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 1963 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 1964 | __raw_spin_lock(&cpu_buffer->lock); |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame^] | 1965 | rb_iter_reset(iter); |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 1966 | __raw_spin_unlock(&cpu_buffer->lock); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 1967 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1968 | |
| 1969 | return iter; |
| 1970 | } |
| 1971 | |
| 1972 | /** |
| 1973 | * ring_buffer_finish - finish reading the iterator of the buffer |
| 1974 | * @iter: The iterator retrieved by ring_buffer_start |
| 1975 | * |
| 1976 | * This re-enables the recording to the buffer, and frees the |
| 1977 | * iterator. |
| 1978 | */ |
| 1979 | void |
| 1980 | ring_buffer_read_finish(struct ring_buffer_iter *iter) |
| 1981 | { |
| 1982 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 1983 | |
| 1984 | atomic_dec(&cpu_buffer->record_disabled); |
| 1985 | kfree(iter); |
| 1986 | } |
| 1987 | |
| 1988 | /** |
| 1989 | * ring_buffer_read - read the next item in the ring buffer by the iterator |
| 1990 | * @iter: The ring buffer iterator |
| 1991 | * @ts: The time stamp of the event read. |
| 1992 | * |
| 1993 | * This reads the next event in the ring buffer and increments the iterator. |
| 1994 | */ |
| 1995 | struct ring_buffer_event * |
| 1996 | ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts) |
| 1997 | { |
| 1998 | struct ring_buffer_event *event; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 1999 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 2000 | unsigned long flags; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2001 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2002 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
| 2003 | event = rb_iter_peek(iter, ts); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2004 | if (!event) |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2005 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2006 | |
| 2007 | rb_advance_iter(iter); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2008 | out: |
| 2009 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2010 | |
| 2011 | return event; |
| 2012 | } |
| 2013 | |
| 2014 | /** |
| 2015 | * ring_buffer_size - return the size of the ring buffer (in bytes) |
| 2016 | * @buffer: The ring buffer. |
| 2017 | */ |
| 2018 | unsigned long ring_buffer_size(struct ring_buffer *buffer) |
| 2019 | { |
| 2020 | return BUF_PAGE_SIZE * buffer->pages; |
| 2021 | } |
| 2022 | |
| 2023 | static void |
| 2024 | rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer) |
| 2025 | { |
| 2026 | cpu_buffer->head_page |
| 2027 | = list_entry(cpu_buffer->pages.next, struct buffer_page, list); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2028 | local_set(&cpu_buffer->head_page->write, 0); |
| 2029 | local_set(&cpu_buffer->head_page->commit, 0); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2030 | |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 2031 | cpu_buffer->head_page->read = 0; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2032 | |
| 2033 | cpu_buffer->tail_page = cpu_buffer->head_page; |
| 2034 | cpu_buffer->commit_page = cpu_buffer->head_page; |
| 2035 | |
| 2036 | INIT_LIST_HEAD(&cpu_buffer->reader_page->list); |
| 2037 | local_set(&cpu_buffer->reader_page->write, 0); |
| 2038 | local_set(&cpu_buffer->reader_page->commit, 0); |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 2039 | cpu_buffer->reader_page->read = 0; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2040 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2041 | cpu_buffer->overrun = 0; |
| 2042 | cpu_buffer->entries = 0; |
| 2043 | } |
| 2044 | |
| 2045 | /** |
| 2046 | * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer |
| 2047 | * @buffer: The ring buffer to reset a per cpu buffer of |
| 2048 | * @cpu: The CPU buffer to be reset |
| 2049 | */ |
| 2050 | void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu) |
| 2051 | { |
| 2052 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; |
| 2053 | unsigned long flags; |
| 2054 | |
| 2055 | if (!cpu_isset(cpu, buffer->cpumask)) |
| 2056 | return; |
| 2057 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2058 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
| 2059 | |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 2060 | __raw_spin_lock(&cpu_buffer->lock); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2061 | |
| 2062 | rb_reset_cpu(cpu_buffer); |
| 2063 | |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 2064 | __raw_spin_unlock(&cpu_buffer->lock); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2065 | |
| 2066 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | /** |
| 2070 | * ring_buffer_reset - reset a ring buffer |
| 2071 | * @buffer: The ring buffer to reset all cpu buffers |
| 2072 | */ |
| 2073 | void ring_buffer_reset(struct ring_buffer *buffer) |
| 2074 | { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2075 | int cpu; |
| 2076 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2077 | for_each_buffer_cpu(buffer, cpu) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2078 | ring_buffer_reset_cpu(buffer, cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | /** |
| 2082 | * rind_buffer_empty - is the ring buffer empty? |
| 2083 | * @buffer: The ring buffer to test |
| 2084 | */ |
| 2085 | int ring_buffer_empty(struct ring_buffer *buffer) |
| 2086 | { |
| 2087 | struct ring_buffer_per_cpu *cpu_buffer; |
| 2088 | int cpu; |
| 2089 | |
| 2090 | /* yes this is racy, but if you don't like the race, lock the buffer */ |
| 2091 | for_each_buffer_cpu(buffer, cpu) { |
| 2092 | cpu_buffer = buffer->buffers[cpu]; |
| 2093 | if (!rb_per_cpu_empty(cpu_buffer)) |
| 2094 | return 0; |
| 2095 | } |
| 2096 | return 1; |
| 2097 | } |
| 2098 | |
| 2099 | /** |
| 2100 | * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty? |
| 2101 | * @buffer: The ring buffer |
| 2102 | * @cpu: The CPU buffer to test |
| 2103 | */ |
| 2104 | int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu) |
| 2105 | { |
| 2106 | struct ring_buffer_per_cpu *cpu_buffer; |
| 2107 | |
| 2108 | if (!cpu_isset(cpu, buffer->cpumask)) |
| 2109 | return 1; |
| 2110 | |
| 2111 | cpu_buffer = buffer->buffers[cpu]; |
| 2112 | return rb_per_cpu_empty(cpu_buffer); |
| 2113 | } |
| 2114 | |
| 2115 | /** |
| 2116 | * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers |
| 2117 | * @buffer_a: One buffer to swap with |
| 2118 | * @buffer_b: The other buffer to swap with |
| 2119 | * |
| 2120 | * This function is useful for tracers that want to take a "snapshot" |
| 2121 | * of a CPU buffer and has another back up buffer lying around. |
| 2122 | * it is expected that the tracer handles the cpu buffer not being |
| 2123 | * used at the moment. |
| 2124 | */ |
| 2125 | int ring_buffer_swap_cpu(struct ring_buffer *buffer_a, |
| 2126 | struct ring_buffer *buffer_b, int cpu) |
| 2127 | { |
| 2128 | struct ring_buffer_per_cpu *cpu_buffer_a; |
| 2129 | struct ring_buffer_per_cpu *cpu_buffer_b; |
| 2130 | |
| 2131 | if (!cpu_isset(cpu, buffer_a->cpumask) || |
| 2132 | !cpu_isset(cpu, buffer_b->cpumask)) |
| 2133 | return -EINVAL; |
| 2134 | |
| 2135 | /* At least make sure the two buffers are somewhat the same */ |
| 2136 | if (buffer_a->size != buffer_b->size || |
| 2137 | buffer_a->pages != buffer_b->pages) |
| 2138 | return -EINVAL; |
| 2139 | |
| 2140 | cpu_buffer_a = buffer_a->buffers[cpu]; |
| 2141 | cpu_buffer_b = buffer_b->buffers[cpu]; |
| 2142 | |
| 2143 | /* |
| 2144 | * We can't do a synchronize_sched here because this |
| 2145 | * function can be called in atomic context. |
| 2146 | * Normally this will be called from the same CPU as cpu. |
| 2147 | * If not it's up to the caller to protect this. |
| 2148 | */ |
| 2149 | atomic_inc(&cpu_buffer_a->record_disabled); |
| 2150 | atomic_inc(&cpu_buffer_b->record_disabled); |
| 2151 | |
| 2152 | buffer_a->buffers[cpu] = cpu_buffer_b; |
| 2153 | buffer_b->buffers[cpu] = cpu_buffer_a; |
| 2154 | |
| 2155 | cpu_buffer_b->buffer = buffer_a; |
| 2156 | cpu_buffer_a->buffer = buffer_b; |
| 2157 | |
| 2158 | atomic_dec(&cpu_buffer_a->record_disabled); |
| 2159 | atomic_dec(&cpu_buffer_b->record_disabled); |
| 2160 | |
| 2161 | return 0; |
| 2162 | } |
| 2163 | |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 2164 | static ssize_t |
| 2165 | rb_simple_read(struct file *filp, char __user *ubuf, |
| 2166 | size_t cnt, loff_t *ppos) |
| 2167 | { |
| 2168 | int *p = filp->private_data; |
| 2169 | char buf[64]; |
| 2170 | int r; |
| 2171 | |
| 2172 | /* !ring_buffers_off == tracing_on */ |
| 2173 | r = sprintf(buf, "%d\n", !*p); |
| 2174 | |
| 2175 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 2176 | } |
| 2177 | |
| 2178 | static ssize_t |
| 2179 | rb_simple_write(struct file *filp, const char __user *ubuf, |
| 2180 | size_t cnt, loff_t *ppos) |
| 2181 | { |
| 2182 | int *p = filp->private_data; |
| 2183 | char buf[64]; |
| 2184 | long val; |
| 2185 | int ret; |
| 2186 | |
| 2187 | if (cnt >= sizeof(buf)) |
| 2188 | return -EINVAL; |
| 2189 | |
| 2190 | if (copy_from_user(&buf, ubuf, cnt)) |
| 2191 | return -EFAULT; |
| 2192 | |
| 2193 | buf[cnt] = 0; |
| 2194 | |
| 2195 | ret = strict_strtoul(buf, 10, &val); |
| 2196 | if (ret < 0) |
| 2197 | return ret; |
| 2198 | |
| 2199 | /* !ring_buffers_off == tracing_on */ |
| 2200 | *p = !val; |
| 2201 | |
| 2202 | (*ppos)++; |
| 2203 | |
| 2204 | return cnt; |
| 2205 | } |
| 2206 | |
| 2207 | static struct file_operations rb_simple_fops = { |
| 2208 | .open = tracing_open_generic, |
| 2209 | .read = rb_simple_read, |
| 2210 | .write = rb_simple_write, |
| 2211 | }; |
| 2212 | |
| 2213 | |
| 2214 | static __init int rb_init_debugfs(void) |
| 2215 | { |
| 2216 | struct dentry *d_tracer; |
| 2217 | struct dentry *entry; |
| 2218 | |
| 2219 | d_tracer = tracing_init_dentry(); |
| 2220 | |
| 2221 | entry = debugfs_create_file("tracing_on", 0644, d_tracer, |
| 2222 | &ring_buffers_off, &rb_simple_fops); |
| 2223 | if (!entry) |
| 2224 | pr_warning("Could not create debugfs 'tracing_on' entry\n"); |
| 2225 | |
| 2226 | return 0; |
| 2227 | } |
| 2228 | |
| 2229 | fs_initcall(rb_init_debugfs); |