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> |
Ingo Molnar | 14131f2 | 2009-02-26 18:47:11 +0100 | [diff] [blame] | 7 | #include <linux/trace_clock.h> |
Steven Rostedt | 78d904b | 2009-02-05 18:43:07 -0500 | [diff] [blame] | 8 | #include <linux/ftrace_irq.h> |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 9 | #include <linux/spinlock.h> |
| 10 | #include <linux/debugfs.h> |
| 11 | #include <linux/uaccess.h> |
Steven Rostedt | a81bd80 | 2009-02-06 01:45:16 -0500 | [diff] [blame] | 12 | #include <linux/hardirq.h> |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/percpu.h> |
| 15 | #include <linux/mutex.h> |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 16 | #include <linux/init.h> |
| 17 | #include <linux/hash.h> |
| 18 | #include <linux/list.h> |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 19 | #include <linux/cpu.h> |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 20 | #include <linux/fs.h> |
| 21 | |
Steven Rostedt | 182e9f5 | 2008-11-03 23:15:56 -0500 | [diff] [blame] | 22 | #include "trace.h" |
| 23 | |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 24 | /* |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 25 | * The ring buffer header is special. We must manually up keep it. |
| 26 | */ |
| 27 | int ring_buffer_print_entry_header(struct trace_seq *s) |
| 28 | { |
| 29 | int ret; |
| 30 | |
| 31 | ret = trace_seq_printf(s, "\ttype : 2 bits\n"); |
| 32 | ret = trace_seq_printf(s, "\tlen : 3 bits\n"); |
| 33 | ret = trace_seq_printf(s, "\ttime_delta : 27 bits\n"); |
| 34 | ret = trace_seq_printf(s, "\tarray : 32 bits\n"); |
| 35 | ret = trace_seq_printf(s, "\n"); |
| 36 | ret = trace_seq_printf(s, "\tpadding : type == %d\n", |
| 37 | RINGBUF_TYPE_PADDING); |
| 38 | ret = trace_seq_printf(s, "\ttime_extend : type == %d\n", |
| 39 | RINGBUF_TYPE_TIME_EXTEND); |
| 40 | ret = trace_seq_printf(s, "\tdata : type == %d\n", |
| 41 | RINGBUF_TYPE_DATA); |
| 42 | |
| 43 | return ret; |
| 44 | } |
| 45 | |
| 46 | /* |
Steven Rostedt | 5cc9854 | 2009-03-12 22:24:17 -0400 | [diff] [blame] | 47 | * The ring buffer is made up of a list of pages. A separate list of pages is |
| 48 | * allocated for each CPU. A writer may only write to a buffer that is |
| 49 | * associated with the CPU it is currently executing on. A reader may read |
| 50 | * from any per cpu buffer. |
| 51 | * |
| 52 | * The reader is special. For each per cpu buffer, the reader has its own |
| 53 | * reader page. When a reader has read the entire reader page, this reader |
| 54 | * page is swapped with another page in the ring buffer. |
| 55 | * |
| 56 | * Now, as long as the writer is off the reader page, the reader can do what |
| 57 | * ever it wants with that page. The writer will never write to that page |
| 58 | * again (as long as it is out of the ring buffer). |
| 59 | * |
| 60 | * Here's some silly ASCII art. |
| 61 | * |
| 62 | * +------+ |
| 63 | * |reader| RING BUFFER |
| 64 | * |page | |
| 65 | * +------+ +---+ +---+ +---+ |
| 66 | * | |-->| |-->| | |
| 67 | * +---+ +---+ +---+ |
| 68 | * ^ | |
| 69 | * | | |
| 70 | * +---------------+ |
| 71 | * |
| 72 | * |
| 73 | * +------+ |
| 74 | * |reader| RING BUFFER |
| 75 | * |page |------------------v |
| 76 | * +------+ +---+ +---+ +---+ |
| 77 | * | |-->| |-->| | |
| 78 | * +---+ +---+ +---+ |
| 79 | * ^ | |
| 80 | * | | |
| 81 | * +---------------+ |
| 82 | * |
| 83 | * |
| 84 | * +------+ |
| 85 | * |reader| RING BUFFER |
| 86 | * |page |------------------v |
| 87 | * +------+ +---+ +---+ +---+ |
| 88 | * ^ | |-->| |-->| | |
| 89 | * | +---+ +---+ +---+ |
| 90 | * | | |
| 91 | * | | |
| 92 | * +------------------------------+ |
| 93 | * |
| 94 | * |
| 95 | * +------+ |
| 96 | * |buffer| RING BUFFER |
| 97 | * |page |------------------v |
| 98 | * +------+ +---+ +---+ +---+ |
| 99 | * ^ | | | |-->| | |
| 100 | * | New +---+ +---+ +---+ |
| 101 | * | Reader------^ | |
| 102 | * | page | |
| 103 | * +------------------------------+ |
| 104 | * |
| 105 | * |
| 106 | * After we make this swap, the reader can hand this page off to the splice |
| 107 | * code and be done with it. It can even allocate a new page if it needs to |
| 108 | * and swap that into the ring buffer. |
| 109 | * |
| 110 | * We will be using cmpxchg soon to make all this lockless. |
| 111 | * |
| 112 | */ |
| 113 | |
| 114 | /* |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 115 | * A fast way to enable or disable all ring buffers is to |
| 116 | * call tracing_on or tracing_off. Turning off the ring buffers |
| 117 | * prevents all ring buffers from being recorded to. |
| 118 | * Turning this switch on, makes it OK to write to the |
| 119 | * ring buffer, if the ring buffer is enabled itself. |
| 120 | * |
| 121 | * There's three layers that must be on in order to write |
| 122 | * to the ring buffer. |
| 123 | * |
| 124 | * 1) This global flag must be set. |
| 125 | * 2) The ring buffer must be enabled for recording. |
| 126 | * 3) The per cpu buffer must be enabled for recording. |
| 127 | * |
| 128 | * In case of an anomaly, this global flag has a bit set that |
| 129 | * will permantly disable all ring buffers. |
| 130 | */ |
| 131 | |
| 132 | /* |
| 133 | * Global flag to disable all recording to ring buffers |
| 134 | * This has two bits: ON, DISABLED |
| 135 | * |
| 136 | * ON DISABLED |
| 137 | * ---- ---------- |
| 138 | * 0 0 : ring buffers are off |
| 139 | * 1 0 : ring buffers are on |
| 140 | * X 1 : ring buffers are permanently disabled |
| 141 | */ |
| 142 | |
| 143 | enum { |
| 144 | RB_BUFFERS_ON_BIT = 0, |
| 145 | RB_BUFFERS_DISABLED_BIT = 1, |
| 146 | }; |
| 147 | |
| 148 | enum { |
| 149 | RB_BUFFERS_ON = 1 << RB_BUFFERS_ON_BIT, |
| 150 | RB_BUFFERS_DISABLED = 1 << RB_BUFFERS_DISABLED_BIT, |
| 151 | }; |
| 152 | |
Hannes Eder | 5e39841 | 2009-02-10 19:44:34 +0100 | [diff] [blame] | 153 | static unsigned long ring_buffer_flags __read_mostly = RB_BUFFERS_ON; |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 154 | |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 155 | #define BUF_PAGE_HDR_SIZE offsetof(struct buffer_data_page, data) |
| 156 | |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 157 | /** |
| 158 | * tracing_on - enable all tracing buffers |
| 159 | * |
| 160 | * This function enables all tracing buffers that may have been |
| 161 | * disabled with tracing_off. |
| 162 | */ |
| 163 | void tracing_on(void) |
| 164 | { |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 165 | set_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags); |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 166 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 167 | EXPORT_SYMBOL_GPL(tracing_on); |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 168 | |
| 169 | /** |
| 170 | * tracing_off - turn off all tracing buffers |
| 171 | * |
| 172 | * This function stops all tracing buffers from recording data. |
| 173 | * It does not disable any overhead the tracers themselves may |
| 174 | * be causing. This function simply causes all recording to |
| 175 | * the ring buffers to fail. |
| 176 | */ |
| 177 | void tracing_off(void) |
| 178 | { |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 179 | clear_bit(RB_BUFFERS_ON_BIT, &ring_buffer_flags); |
| 180 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 181 | EXPORT_SYMBOL_GPL(tracing_off); |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 182 | |
| 183 | /** |
| 184 | * tracing_off_permanent - permanently disable ring buffers |
| 185 | * |
| 186 | * This function, once called, will disable all ring buffers |
Wenji Huang | c3706f0 | 2009-02-10 01:03:18 -0500 | [diff] [blame] | 187 | * permanently. |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 188 | */ |
| 189 | void tracing_off_permanent(void) |
| 190 | { |
| 191 | set_bit(RB_BUFFERS_DISABLED_BIT, &ring_buffer_flags); |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 192 | } |
| 193 | |
Steven Rostedt | 988ae9d | 2009-02-14 19:17:02 -0500 | [diff] [blame] | 194 | /** |
| 195 | * tracing_is_on - show state of ring buffers enabled |
| 196 | */ |
| 197 | int tracing_is_on(void) |
| 198 | { |
| 199 | return ring_buffer_flags == RB_BUFFERS_ON; |
| 200 | } |
| 201 | EXPORT_SYMBOL_GPL(tracing_is_on); |
| 202 | |
Ingo Molnar | d06bbd6 | 2008-11-12 10:11:37 +0100 | [diff] [blame] | 203 | #include "trace.h" |
| 204 | |
Steven Rostedt | e3d6bf0 | 2009-03-03 13:53:07 -0500 | [diff] [blame] | 205 | #define RB_EVNT_HDR_SIZE (offsetof(struct ring_buffer_event, array)) |
Andrew Morton | 67d3472 | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 206 | #define RB_ALIGNMENT 4U |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 207 | #define RB_MAX_SMALL_DATA 28 |
| 208 | |
| 209 | enum { |
| 210 | RB_LEN_TIME_EXTEND = 8, |
| 211 | RB_LEN_TIME_STAMP = 16, |
| 212 | }; |
| 213 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 214 | static inline int rb_null_event(struct ring_buffer_event *event) |
| 215 | { |
| 216 | return event->type == RINGBUF_TYPE_PADDING && event->time_delta == 0; |
| 217 | } |
| 218 | |
| 219 | static inline int rb_discarded_event(struct ring_buffer_event *event) |
| 220 | { |
| 221 | return event->type == RINGBUF_TYPE_PADDING && event->time_delta; |
| 222 | } |
| 223 | |
| 224 | static void rb_event_set_padding(struct ring_buffer_event *event) |
| 225 | { |
| 226 | event->type = RINGBUF_TYPE_PADDING; |
| 227 | event->time_delta = 0; |
| 228 | } |
| 229 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 230 | static unsigned |
| 231 | rb_event_data_length(struct ring_buffer_event *event) |
| 232 | { |
| 233 | unsigned length; |
| 234 | |
| 235 | if (event->len) |
| 236 | length = event->len * RB_ALIGNMENT; |
| 237 | else |
| 238 | length = event->array[0]; |
| 239 | return length + RB_EVNT_HDR_SIZE; |
| 240 | } |
| 241 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 242 | /* inline for ring buffer fast paths */ |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 243 | static unsigned |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 244 | rb_event_length(struct ring_buffer_event *event) |
| 245 | { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 246 | switch (event->type) { |
| 247 | case RINGBUF_TYPE_PADDING: |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 248 | if (rb_null_event(event)) |
| 249 | /* undefined */ |
| 250 | return -1; |
| 251 | return rb_event_data_length(event); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 252 | |
| 253 | case RINGBUF_TYPE_TIME_EXTEND: |
| 254 | return RB_LEN_TIME_EXTEND; |
| 255 | |
| 256 | case RINGBUF_TYPE_TIME_STAMP: |
| 257 | return RB_LEN_TIME_STAMP; |
| 258 | |
| 259 | case RINGBUF_TYPE_DATA: |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 260 | return rb_event_data_length(event); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 261 | default: |
| 262 | BUG(); |
| 263 | } |
| 264 | /* not hit */ |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * ring_buffer_event_length - return the length of the event |
| 270 | * @event: the event to get the length of |
| 271 | */ |
| 272 | unsigned ring_buffer_event_length(struct ring_buffer_event *event) |
| 273 | { |
Robert Richter | 465634a | 2009-01-07 15:32:11 +0100 | [diff] [blame] | 274 | unsigned length = rb_event_length(event); |
| 275 | if (event->type != RINGBUF_TYPE_DATA) |
| 276 | return length; |
| 277 | length -= RB_EVNT_HDR_SIZE; |
| 278 | if (length > RB_MAX_SMALL_DATA + sizeof(event->array[0])) |
| 279 | length -= sizeof(event->array[0]); |
| 280 | return length; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 281 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 282 | EXPORT_SYMBOL_GPL(ring_buffer_event_length); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 283 | |
| 284 | /* inline for ring buffer fast paths */ |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 285 | static void * |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 286 | rb_event_data(struct ring_buffer_event *event) |
| 287 | { |
| 288 | BUG_ON(event->type != RINGBUF_TYPE_DATA); |
| 289 | /* If length is in len field, then array[0] has the data */ |
| 290 | if (event->len) |
| 291 | return (void *)&event->array[0]; |
| 292 | /* Otherwise length is in array[0] and array[1] has the data */ |
| 293 | return (void *)&event->array[1]; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * ring_buffer_event_data - return the data of the event |
| 298 | * @event: the event to get the data from |
| 299 | */ |
| 300 | void *ring_buffer_event_data(struct ring_buffer_event *event) |
| 301 | { |
| 302 | return rb_event_data(event); |
| 303 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 304 | EXPORT_SYMBOL_GPL(ring_buffer_event_data); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 305 | |
| 306 | #define for_each_buffer_cpu(buffer, cpu) \ |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 307 | for_each_cpu(cpu, buffer->cpumask) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 308 | |
| 309 | #define TS_SHIFT 27 |
| 310 | #define TS_MASK ((1ULL << TS_SHIFT) - 1) |
| 311 | #define TS_DELTA_TEST (~TS_MASK) |
| 312 | |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 313 | struct buffer_data_page { |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 314 | u64 time_stamp; /* page time stamp */ |
Wenji Huang | c3706f0 | 2009-02-10 01:03:18 -0500 | [diff] [blame] | 315 | local_t commit; /* write committed index */ |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 316 | unsigned char data[]; /* data of buffer page */ |
| 317 | }; |
| 318 | |
| 319 | struct buffer_page { |
| 320 | local_t write; /* index for next write */ |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 321 | unsigned read; /* index for next read */ |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 322 | struct list_head list; /* list of free pages */ |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 323 | struct buffer_data_page *page; /* Actual data page */ |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 324 | }; |
| 325 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 326 | static void rb_init_page(struct buffer_data_page *bpage) |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 327 | { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 328 | local_set(&bpage->commit, 0); |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 329 | } |
| 330 | |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 331 | /** |
| 332 | * ring_buffer_page_len - the size of data on the page. |
| 333 | * @page: The page to read |
| 334 | * |
| 335 | * Returns the amount of data on the page, including buffer page header. |
| 336 | */ |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 337 | size_t ring_buffer_page_len(void *page) |
| 338 | { |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 339 | return local_read(&((struct buffer_data_page *)page)->commit) |
| 340 | + BUF_PAGE_HDR_SIZE; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 341 | } |
| 342 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 343 | /* |
Steven Rostedt | ed56829 | 2008-09-29 23:02:40 -0400 | [diff] [blame] | 344 | * Also stolen from mm/slob.c. Thanks to Mathieu Desnoyers for pointing |
| 345 | * this issue out. |
| 346 | */ |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 347 | static void free_buffer_page(struct buffer_page *bpage) |
Steven Rostedt | ed56829 | 2008-09-29 23:02:40 -0400 | [diff] [blame] | 348 | { |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 349 | free_page((unsigned long)bpage->page); |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 350 | kfree(bpage); |
Steven Rostedt | ed56829 | 2008-09-29 23:02:40 -0400 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /* |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 354 | * We need to fit the time_stamp delta into 27 bits. |
| 355 | */ |
| 356 | static inline int test_time_stamp(u64 delta) |
| 357 | { |
| 358 | if (delta & TS_DELTA_TEST) |
| 359 | return 1; |
| 360 | return 0; |
| 361 | } |
| 362 | |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 363 | #define BUF_PAGE_SIZE (PAGE_SIZE - BUF_PAGE_HDR_SIZE) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 364 | |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 365 | int ring_buffer_print_page_header(struct trace_seq *s) |
| 366 | { |
| 367 | struct buffer_data_page field; |
| 368 | int ret; |
| 369 | |
| 370 | ret = trace_seq_printf(s, "\tfield: u64 timestamp;\t" |
| 371 | "offset:0;\tsize:%u;\n", |
| 372 | (unsigned int)sizeof(field.time_stamp)); |
| 373 | |
| 374 | ret = trace_seq_printf(s, "\tfield: local_t commit;\t" |
| 375 | "offset:%u;\tsize:%u;\n", |
| 376 | (unsigned int)offsetof(typeof(field), commit), |
| 377 | (unsigned int)sizeof(field.commit)); |
| 378 | |
| 379 | ret = trace_seq_printf(s, "\tfield: char data;\t" |
| 380 | "offset:%u;\tsize:%u;\n", |
| 381 | (unsigned int)offsetof(typeof(field), data), |
| 382 | (unsigned int)BUF_PAGE_SIZE); |
| 383 | |
| 384 | return ret; |
| 385 | } |
| 386 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 387 | /* |
| 388 | * head_page == tail_page && head == tail then buffer is empty. |
| 389 | */ |
| 390 | struct ring_buffer_per_cpu { |
| 391 | int cpu; |
| 392 | struct ring_buffer *buffer; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 393 | spinlock_t reader_lock; /* serialize readers */ |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 394 | raw_spinlock_t lock; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 395 | struct lock_class_key lock_key; |
| 396 | struct list_head pages; |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 397 | struct buffer_page *head_page; /* read from head */ |
| 398 | struct buffer_page *tail_page; /* write to tail */ |
Wenji Huang | c3706f0 | 2009-02-10 01:03:18 -0500 | [diff] [blame] | 399 | struct buffer_page *commit_page; /* committed pages */ |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 400 | struct buffer_page *reader_page; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 401 | unsigned long overrun; |
| 402 | unsigned long entries; |
| 403 | u64 write_stamp; |
| 404 | u64 read_stamp; |
| 405 | atomic_t record_disabled; |
| 406 | }; |
| 407 | |
| 408 | struct ring_buffer { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 409 | unsigned pages; |
| 410 | unsigned flags; |
| 411 | int cpus; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 412 | atomic_t record_disabled; |
Arnaldo Carvalho de Melo | 00f62f6 | 2009-02-09 17:04:06 -0200 | [diff] [blame] | 413 | cpumask_var_t cpumask; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 414 | |
| 415 | struct mutex mutex; |
| 416 | |
| 417 | struct ring_buffer_per_cpu **buffers; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 418 | |
Steven Rostedt | 59222ef | 2009-03-12 11:46:03 -0400 | [diff] [blame] | 419 | #ifdef CONFIG_HOTPLUG_CPU |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 420 | struct notifier_block cpu_notify; |
| 421 | #endif |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 422 | u64 (*clock)(void); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 423 | }; |
| 424 | |
| 425 | struct ring_buffer_iter { |
| 426 | struct ring_buffer_per_cpu *cpu_buffer; |
| 427 | unsigned long head; |
| 428 | struct buffer_page *head_page; |
| 429 | u64 read_stamp; |
| 430 | }; |
| 431 | |
Steven Rostedt | f536aaf | 2008-11-10 23:07:30 -0500 | [diff] [blame] | 432 | /* buffer may be either ring_buffer or ring_buffer_per_cpu */ |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 433 | #define RB_WARN_ON(buffer, cond) \ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 434 | ({ \ |
| 435 | int _____ret = unlikely(cond); \ |
| 436 | if (_____ret) { \ |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 437 | atomic_inc(&buffer->record_disabled); \ |
| 438 | WARN_ON(1); \ |
| 439 | } \ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 440 | _____ret; \ |
| 441 | }) |
Steven Rostedt | f536aaf | 2008-11-10 23:07:30 -0500 | [diff] [blame] | 442 | |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 443 | /* Up this if you want to test the TIME_EXTENTS and normalization */ |
| 444 | #define DEBUG_SHIFT 0 |
| 445 | |
| 446 | u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu) |
| 447 | { |
| 448 | u64 time; |
| 449 | |
| 450 | preempt_disable_notrace(); |
| 451 | /* shift to debug/test normalization and TIME_EXTENTS */ |
| 452 | time = buffer->clock() << DEBUG_SHIFT; |
| 453 | preempt_enable_no_resched_notrace(); |
| 454 | |
| 455 | return time; |
| 456 | } |
| 457 | EXPORT_SYMBOL_GPL(ring_buffer_time_stamp); |
| 458 | |
| 459 | void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer, |
| 460 | int cpu, u64 *ts) |
| 461 | { |
| 462 | /* Just stupid testing the normalize function and deltas */ |
| 463 | *ts >>= DEBUG_SHIFT; |
| 464 | } |
| 465 | EXPORT_SYMBOL_GPL(ring_buffer_normalize_time_stamp); |
| 466 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 467 | /** |
| 468 | * check_pages - integrity check of buffer pages |
| 469 | * @cpu_buffer: CPU buffer with pages to test |
| 470 | * |
Wenji Huang | c3706f0 | 2009-02-10 01:03:18 -0500 | [diff] [blame] | 471 | * As a safety measure we check to make sure the data pages have not |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 472 | * been corrupted. |
| 473 | */ |
| 474 | static int rb_check_pages(struct ring_buffer_per_cpu *cpu_buffer) |
| 475 | { |
| 476 | struct list_head *head = &cpu_buffer->pages; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 477 | struct buffer_page *bpage, *tmp; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 478 | |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 479 | if (RB_WARN_ON(cpu_buffer, head->next->prev != head)) |
| 480 | return -1; |
| 481 | if (RB_WARN_ON(cpu_buffer, head->prev->next != head)) |
| 482 | return -1; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 483 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 484 | list_for_each_entry_safe(bpage, tmp, head, list) { |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 485 | if (RB_WARN_ON(cpu_buffer, |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 486 | bpage->list.next->prev != &bpage->list)) |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 487 | return -1; |
| 488 | if (RB_WARN_ON(cpu_buffer, |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 489 | bpage->list.prev->next != &bpage->list)) |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 490 | return -1; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 496 | static int rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, |
| 497 | unsigned nr_pages) |
| 498 | { |
| 499 | struct list_head *head = &cpu_buffer->pages; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 500 | struct buffer_page *bpage, *tmp; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 501 | unsigned long addr; |
| 502 | LIST_HEAD(pages); |
| 503 | unsigned i; |
| 504 | |
| 505 | for (i = 0; i < nr_pages; i++) { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 506 | bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()), |
Steven Rostedt | aa1e0e3b | 2008-10-02 19:18:09 -0400 | [diff] [blame] | 507 | GFP_KERNEL, cpu_to_node(cpu_buffer->cpu)); |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 508 | if (!bpage) |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 509 | goto free_pages; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 510 | list_add(&bpage->list, &pages); |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 511 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 512 | addr = __get_free_page(GFP_KERNEL); |
| 513 | if (!addr) |
| 514 | goto free_pages; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 515 | bpage->page = (void *)addr; |
| 516 | rb_init_page(bpage->page); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | list_splice(&pages, head); |
| 520 | |
| 521 | rb_check_pages(cpu_buffer); |
| 522 | |
| 523 | return 0; |
| 524 | |
| 525 | free_pages: |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 526 | list_for_each_entry_safe(bpage, tmp, &pages, list) { |
| 527 | list_del_init(&bpage->list); |
| 528 | free_buffer_page(bpage); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 529 | } |
| 530 | return -ENOMEM; |
| 531 | } |
| 532 | |
| 533 | static struct ring_buffer_per_cpu * |
| 534 | rb_allocate_cpu_buffer(struct ring_buffer *buffer, int cpu) |
| 535 | { |
| 536 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 537 | struct buffer_page *bpage; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 538 | unsigned long addr; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 539 | int ret; |
| 540 | |
| 541 | cpu_buffer = kzalloc_node(ALIGN(sizeof(*cpu_buffer), cache_line_size()), |
| 542 | GFP_KERNEL, cpu_to_node(cpu)); |
| 543 | if (!cpu_buffer) |
| 544 | return NULL; |
| 545 | |
| 546 | cpu_buffer->cpu = cpu; |
| 547 | cpu_buffer->buffer = buffer; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 548 | spin_lock_init(&cpu_buffer->reader_lock); |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 549 | cpu_buffer->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 550 | INIT_LIST_HEAD(&cpu_buffer->pages); |
| 551 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 552 | bpage = kzalloc_node(ALIGN(sizeof(*bpage), cache_line_size()), |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 553 | GFP_KERNEL, cpu_to_node(cpu)); |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 554 | if (!bpage) |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 555 | goto fail_free_buffer; |
| 556 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 557 | cpu_buffer->reader_page = bpage; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 558 | addr = __get_free_page(GFP_KERNEL); |
| 559 | if (!addr) |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 560 | goto fail_free_reader; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 561 | bpage->page = (void *)addr; |
| 562 | rb_init_page(bpage->page); |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 563 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 564 | INIT_LIST_HEAD(&cpu_buffer->reader_page->list); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 565 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 566 | ret = rb_allocate_pages(cpu_buffer, buffer->pages); |
| 567 | if (ret < 0) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 568 | goto fail_free_reader; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 569 | |
| 570 | cpu_buffer->head_page |
| 571 | = list_entry(cpu_buffer->pages.next, struct buffer_page, list); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 572 | cpu_buffer->tail_page = cpu_buffer->commit_page = cpu_buffer->head_page; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 573 | |
| 574 | return cpu_buffer; |
| 575 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 576 | fail_free_reader: |
| 577 | free_buffer_page(cpu_buffer->reader_page); |
| 578 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 579 | fail_free_buffer: |
| 580 | kfree(cpu_buffer); |
| 581 | return NULL; |
| 582 | } |
| 583 | |
| 584 | static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer) |
| 585 | { |
| 586 | struct list_head *head = &cpu_buffer->pages; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 587 | struct buffer_page *bpage, *tmp; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 588 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 589 | free_buffer_page(cpu_buffer->reader_page); |
| 590 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 591 | list_for_each_entry_safe(bpage, tmp, head, list) { |
| 592 | list_del_init(&bpage->list); |
| 593 | free_buffer_page(bpage); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 594 | } |
| 595 | kfree(cpu_buffer); |
| 596 | } |
| 597 | |
Steven Rostedt | a7b1374 | 2008-09-29 23:02:39 -0400 | [diff] [blame] | 598 | /* |
| 599 | * Causes compile errors if the struct buffer_page gets bigger |
| 600 | * than the struct page. |
| 601 | */ |
| 602 | extern int ring_buffer_page_too_big(void); |
| 603 | |
Steven Rostedt | 59222ef | 2009-03-12 11:46:03 -0400 | [diff] [blame] | 604 | #ifdef CONFIG_HOTPLUG_CPU |
Frederic Weisbecker | 09c9e84 | 2009-03-21 04:33:36 +0100 | [diff] [blame] | 605 | static int rb_cpu_notify(struct notifier_block *self, |
| 606 | unsigned long action, void *hcpu); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 607 | #endif |
| 608 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 609 | /** |
| 610 | * ring_buffer_alloc - allocate a new ring_buffer |
Robert Richter | 68814b5 | 2008-11-24 12:24:12 +0100 | [diff] [blame] | 611 | * @size: the size in bytes per cpu that is needed. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 612 | * @flags: attributes to set for the ring buffer. |
| 613 | * |
| 614 | * Currently the only flag that is available is the RB_FL_OVERWRITE |
| 615 | * flag. This flag means that the buffer will overwrite old data |
| 616 | * when the buffer wraps. If this flag is not set, the buffer will |
| 617 | * drop data when the tail hits the head. |
| 618 | */ |
| 619 | struct ring_buffer *ring_buffer_alloc(unsigned long size, unsigned flags) |
| 620 | { |
| 621 | struct ring_buffer *buffer; |
| 622 | int bsize; |
| 623 | int cpu; |
| 624 | |
Steven Rostedt | a7b1374 | 2008-09-29 23:02:39 -0400 | [diff] [blame] | 625 | /* Paranoid! Optimizes out when all is well */ |
| 626 | if (sizeof(struct buffer_page) > sizeof(struct page)) |
| 627 | ring_buffer_page_too_big(); |
| 628 | |
| 629 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 630 | /* keep it in its own cache line */ |
| 631 | buffer = kzalloc(ALIGN(sizeof(*buffer), cache_line_size()), |
| 632 | GFP_KERNEL); |
| 633 | if (!buffer) |
| 634 | return NULL; |
| 635 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 636 | if (!alloc_cpumask_var(&buffer->cpumask, GFP_KERNEL)) |
| 637 | goto fail_free_buffer; |
| 638 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 639 | buffer->pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE); |
| 640 | buffer->flags = flags; |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 641 | buffer->clock = trace_clock_local; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 642 | |
| 643 | /* need at least two pages */ |
| 644 | if (buffer->pages == 1) |
| 645 | buffer->pages++; |
| 646 | |
Frederic Weisbecker | 3bf832c | 2009-03-19 14:47:33 +0100 | [diff] [blame] | 647 | /* |
| 648 | * In case of non-hotplug cpu, if the ring-buffer is allocated |
| 649 | * in early initcall, it will not be notified of secondary cpus. |
| 650 | * In that off case, we need to allocate for all possible cpus. |
| 651 | */ |
| 652 | #ifdef CONFIG_HOTPLUG_CPU |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 653 | get_online_cpus(); |
| 654 | cpumask_copy(buffer->cpumask, cpu_online_mask); |
Frederic Weisbecker | 3bf832c | 2009-03-19 14:47:33 +0100 | [diff] [blame] | 655 | #else |
| 656 | cpumask_copy(buffer->cpumask, cpu_possible_mask); |
| 657 | #endif |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 658 | buffer->cpus = nr_cpu_ids; |
| 659 | |
| 660 | bsize = sizeof(void *) * nr_cpu_ids; |
| 661 | buffer->buffers = kzalloc(ALIGN(bsize, cache_line_size()), |
| 662 | GFP_KERNEL); |
| 663 | if (!buffer->buffers) |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 664 | goto fail_free_cpumask; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 665 | |
| 666 | for_each_buffer_cpu(buffer, cpu) { |
| 667 | buffer->buffers[cpu] = |
| 668 | rb_allocate_cpu_buffer(buffer, cpu); |
| 669 | if (!buffer->buffers[cpu]) |
| 670 | goto fail_free_buffers; |
| 671 | } |
| 672 | |
Steven Rostedt | 59222ef | 2009-03-12 11:46:03 -0400 | [diff] [blame] | 673 | #ifdef CONFIG_HOTPLUG_CPU |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 674 | buffer->cpu_notify.notifier_call = rb_cpu_notify; |
| 675 | buffer->cpu_notify.priority = 0; |
| 676 | register_cpu_notifier(&buffer->cpu_notify); |
| 677 | #endif |
| 678 | |
| 679 | put_online_cpus(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 680 | mutex_init(&buffer->mutex); |
| 681 | |
| 682 | return buffer; |
| 683 | |
| 684 | fail_free_buffers: |
| 685 | for_each_buffer_cpu(buffer, cpu) { |
| 686 | if (buffer->buffers[cpu]) |
| 687 | rb_free_cpu_buffer(buffer->buffers[cpu]); |
| 688 | } |
| 689 | kfree(buffer->buffers); |
| 690 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 691 | fail_free_cpumask: |
| 692 | free_cpumask_var(buffer->cpumask); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 693 | put_online_cpus(); |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 694 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 695 | fail_free_buffer: |
| 696 | kfree(buffer); |
| 697 | return NULL; |
| 698 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 699 | EXPORT_SYMBOL_GPL(ring_buffer_alloc); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 700 | |
| 701 | /** |
| 702 | * ring_buffer_free - free a ring buffer. |
| 703 | * @buffer: the buffer to free. |
| 704 | */ |
| 705 | void |
| 706 | ring_buffer_free(struct ring_buffer *buffer) |
| 707 | { |
| 708 | int cpu; |
| 709 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 710 | get_online_cpus(); |
| 711 | |
Steven Rostedt | 59222ef | 2009-03-12 11:46:03 -0400 | [diff] [blame] | 712 | #ifdef CONFIG_HOTPLUG_CPU |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 713 | unregister_cpu_notifier(&buffer->cpu_notify); |
| 714 | #endif |
| 715 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 716 | for_each_buffer_cpu(buffer, cpu) |
| 717 | rb_free_cpu_buffer(buffer->buffers[cpu]); |
| 718 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 719 | put_online_cpus(); |
| 720 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 721 | free_cpumask_var(buffer->cpumask); |
| 722 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 723 | kfree(buffer); |
| 724 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 725 | EXPORT_SYMBOL_GPL(ring_buffer_free); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 726 | |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 727 | void ring_buffer_set_clock(struct ring_buffer *buffer, |
| 728 | u64 (*clock)(void)) |
| 729 | { |
| 730 | buffer->clock = clock; |
| 731 | } |
| 732 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 733 | static void rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer); |
| 734 | |
| 735 | static void |
| 736 | rb_remove_pages(struct ring_buffer_per_cpu *cpu_buffer, unsigned nr_pages) |
| 737 | { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 738 | struct buffer_page *bpage; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 739 | struct list_head *p; |
| 740 | unsigned i; |
| 741 | |
| 742 | atomic_inc(&cpu_buffer->record_disabled); |
| 743 | synchronize_sched(); |
| 744 | |
| 745 | for (i = 0; i < nr_pages; i++) { |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 746 | if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages))) |
| 747 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 748 | p = cpu_buffer->pages.next; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 749 | bpage = list_entry(p, struct buffer_page, list); |
| 750 | list_del_init(&bpage->list); |
| 751 | free_buffer_page(bpage); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 752 | } |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 753 | if (RB_WARN_ON(cpu_buffer, list_empty(&cpu_buffer->pages))) |
| 754 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 755 | |
| 756 | rb_reset_cpu(cpu_buffer); |
| 757 | |
| 758 | rb_check_pages(cpu_buffer); |
| 759 | |
| 760 | atomic_dec(&cpu_buffer->record_disabled); |
| 761 | |
| 762 | } |
| 763 | |
| 764 | static void |
| 765 | rb_insert_pages(struct ring_buffer_per_cpu *cpu_buffer, |
| 766 | struct list_head *pages, unsigned nr_pages) |
| 767 | { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 768 | struct buffer_page *bpage; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 769 | struct list_head *p; |
| 770 | unsigned i; |
| 771 | |
| 772 | atomic_inc(&cpu_buffer->record_disabled); |
| 773 | synchronize_sched(); |
| 774 | |
| 775 | for (i = 0; i < nr_pages; i++) { |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 776 | if (RB_WARN_ON(cpu_buffer, list_empty(pages))) |
| 777 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 778 | p = pages->next; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 779 | bpage = list_entry(p, struct buffer_page, list); |
| 780 | list_del_init(&bpage->list); |
| 781 | list_add_tail(&bpage->list, &cpu_buffer->pages); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 782 | } |
| 783 | rb_reset_cpu(cpu_buffer); |
| 784 | |
| 785 | rb_check_pages(cpu_buffer); |
| 786 | |
| 787 | atomic_dec(&cpu_buffer->record_disabled); |
| 788 | } |
| 789 | |
| 790 | /** |
| 791 | * ring_buffer_resize - resize the ring buffer |
| 792 | * @buffer: the buffer to resize. |
| 793 | * @size: the new size. |
| 794 | * |
| 795 | * The tracer is responsible for making sure that the buffer is |
| 796 | * not being used while changing the size. |
| 797 | * Note: We may be able to change the above requirement by using |
| 798 | * RCU synchronizations. |
| 799 | * |
| 800 | * Minimum size is 2 * BUF_PAGE_SIZE. |
| 801 | * |
| 802 | * Returns -1 on failure. |
| 803 | */ |
| 804 | int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size) |
| 805 | { |
| 806 | struct ring_buffer_per_cpu *cpu_buffer; |
| 807 | unsigned nr_pages, rm_pages, new_pages; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 808 | struct buffer_page *bpage, *tmp; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 809 | unsigned long buffer_size; |
| 810 | unsigned long addr; |
| 811 | LIST_HEAD(pages); |
| 812 | int i, cpu; |
| 813 | |
Ingo Molnar | ee51a1d | 2008-11-13 14:58:31 +0100 | [diff] [blame] | 814 | /* |
| 815 | * Always succeed at resizing a non-existent buffer: |
| 816 | */ |
| 817 | if (!buffer) |
| 818 | return size; |
| 819 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 820 | size = DIV_ROUND_UP(size, BUF_PAGE_SIZE); |
| 821 | size *= BUF_PAGE_SIZE; |
| 822 | buffer_size = buffer->pages * BUF_PAGE_SIZE; |
| 823 | |
| 824 | /* we need a minimum of two pages */ |
| 825 | if (size < BUF_PAGE_SIZE * 2) |
| 826 | size = BUF_PAGE_SIZE * 2; |
| 827 | |
| 828 | if (size == buffer_size) |
| 829 | return size; |
| 830 | |
| 831 | mutex_lock(&buffer->mutex); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 832 | get_online_cpus(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 833 | |
| 834 | nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE); |
| 835 | |
| 836 | if (size < buffer_size) { |
| 837 | |
| 838 | /* easy case, just free pages */ |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 839 | if (RB_WARN_ON(buffer, nr_pages >= buffer->pages)) |
| 840 | goto out_fail; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 841 | |
| 842 | rm_pages = buffer->pages - nr_pages; |
| 843 | |
| 844 | for_each_buffer_cpu(buffer, cpu) { |
| 845 | cpu_buffer = buffer->buffers[cpu]; |
| 846 | rb_remove_pages(cpu_buffer, rm_pages); |
| 847 | } |
| 848 | goto out; |
| 849 | } |
| 850 | |
| 851 | /* |
| 852 | * This is a bit more difficult. We only want to add pages |
| 853 | * when we can allocate enough for all CPUs. We do this |
| 854 | * by allocating all the pages and storing them on a local |
| 855 | * link list. If we succeed in our allocation, then we |
| 856 | * add these pages to the cpu_buffers. Otherwise we just free |
| 857 | * them all and return -ENOMEM; |
| 858 | */ |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 859 | if (RB_WARN_ON(buffer, nr_pages <= buffer->pages)) |
| 860 | goto out_fail; |
Steven Rostedt | f536aaf | 2008-11-10 23:07:30 -0500 | [diff] [blame] | 861 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 862 | new_pages = nr_pages - buffer->pages; |
| 863 | |
| 864 | for_each_buffer_cpu(buffer, cpu) { |
| 865 | for (i = 0; i < new_pages; i++) { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 866 | bpage = kzalloc_node(ALIGN(sizeof(*bpage), |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 867 | cache_line_size()), |
| 868 | GFP_KERNEL, cpu_to_node(cpu)); |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 869 | if (!bpage) |
Steven Rostedt | e4c2ce8 | 2008-10-01 11:14:54 -0400 | [diff] [blame] | 870 | goto free_pages; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 871 | list_add(&bpage->list, &pages); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 872 | addr = __get_free_page(GFP_KERNEL); |
| 873 | if (!addr) |
| 874 | goto free_pages; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 875 | bpage->page = (void *)addr; |
| 876 | rb_init_page(bpage->page); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | |
| 880 | for_each_buffer_cpu(buffer, cpu) { |
| 881 | cpu_buffer = buffer->buffers[cpu]; |
| 882 | rb_insert_pages(cpu_buffer, &pages, new_pages); |
| 883 | } |
| 884 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 885 | if (RB_WARN_ON(buffer, !list_empty(&pages))) |
| 886 | goto out_fail; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 887 | |
| 888 | out: |
| 889 | buffer->pages = nr_pages; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 890 | put_online_cpus(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 891 | mutex_unlock(&buffer->mutex); |
| 892 | |
| 893 | return size; |
| 894 | |
| 895 | free_pages: |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 896 | list_for_each_entry_safe(bpage, tmp, &pages, list) { |
| 897 | list_del_init(&bpage->list); |
| 898 | free_buffer_page(bpage); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 899 | } |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 900 | put_online_cpus(); |
Vegard Nossum | 641d2f6 | 2008-11-18 19:22:13 +0100 | [diff] [blame] | 901 | mutex_unlock(&buffer->mutex); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 902 | return -ENOMEM; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 903 | |
| 904 | /* |
| 905 | * Something went totally wrong, and we are too paranoid |
| 906 | * to even clean up the mess. |
| 907 | */ |
| 908 | out_fail: |
| 909 | put_online_cpus(); |
| 910 | mutex_unlock(&buffer->mutex); |
| 911 | return -1; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 912 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 913 | EXPORT_SYMBOL_GPL(ring_buffer_resize); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 914 | |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 915 | static inline void * |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 916 | __rb_data_page_index(struct buffer_data_page *bpage, unsigned index) |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 917 | { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 918 | return bpage->data + index; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 919 | } |
| 920 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 921 | static inline void *__rb_page_index(struct buffer_page *bpage, unsigned index) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 922 | { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 923 | return bpage->page->data + index; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | static inline struct ring_buffer_event * |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 927 | rb_reader_event(struct ring_buffer_per_cpu *cpu_buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 928 | { |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 929 | return __rb_page_index(cpu_buffer->reader_page, |
| 930 | cpu_buffer->reader_page->read); |
| 931 | } |
| 932 | |
| 933 | static inline struct ring_buffer_event * |
| 934 | rb_head_event(struct ring_buffer_per_cpu *cpu_buffer) |
| 935 | { |
| 936 | return __rb_page_index(cpu_buffer->head_page, |
| 937 | cpu_buffer->head_page->read); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | static inline struct ring_buffer_event * |
| 941 | rb_iter_head_event(struct ring_buffer_iter *iter) |
| 942 | { |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 943 | return __rb_page_index(iter->head_page, iter->head); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 944 | } |
| 945 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 946 | static inline unsigned rb_page_write(struct buffer_page *bpage) |
| 947 | { |
| 948 | return local_read(&bpage->write); |
| 949 | } |
| 950 | |
| 951 | static inline unsigned rb_page_commit(struct buffer_page *bpage) |
| 952 | { |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 953 | return local_read(&bpage->page->commit); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | /* Size is determined by what has been commited */ |
| 957 | static inline unsigned rb_page_size(struct buffer_page *bpage) |
| 958 | { |
| 959 | return rb_page_commit(bpage); |
| 960 | } |
| 961 | |
| 962 | static inline unsigned |
| 963 | rb_commit_index(struct ring_buffer_per_cpu *cpu_buffer) |
| 964 | { |
| 965 | return rb_page_commit(cpu_buffer->commit_page); |
| 966 | } |
| 967 | |
| 968 | static inline unsigned rb_head_size(struct ring_buffer_per_cpu *cpu_buffer) |
| 969 | { |
| 970 | return rb_page_commit(cpu_buffer->head_page); |
| 971 | } |
| 972 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 973 | /* |
| 974 | * When the tail hits the head and the buffer is in overwrite mode, |
| 975 | * the head jumps to the next page and all content on the previous |
| 976 | * page is discarded. But before doing so, we update the overrun |
| 977 | * variable of the buffer. |
| 978 | */ |
| 979 | static void rb_update_overflow(struct ring_buffer_per_cpu *cpu_buffer) |
| 980 | { |
| 981 | struct ring_buffer_event *event; |
| 982 | unsigned long head; |
| 983 | |
| 984 | for (head = 0; head < rb_head_size(cpu_buffer); |
| 985 | head += rb_event_length(event)) { |
| 986 | |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 987 | event = __rb_page_index(cpu_buffer->head_page, head); |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 988 | if (RB_WARN_ON(cpu_buffer, rb_null_event(event))) |
| 989 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 990 | /* Only count data entries */ |
| 991 | if (event->type != RINGBUF_TYPE_DATA) |
| 992 | continue; |
| 993 | cpu_buffer->overrun++; |
| 994 | cpu_buffer->entries--; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | static inline void rb_inc_page(struct ring_buffer_per_cpu *cpu_buffer, |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 999 | struct buffer_page **bpage) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1000 | { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1001 | struct list_head *p = (*bpage)->list.next; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1002 | |
| 1003 | if (p == &cpu_buffer->pages) |
| 1004 | p = p->next; |
| 1005 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 1006 | *bpage = list_entry(p, struct buffer_page, list); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1007 | } |
| 1008 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1009 | static inline unsigned |
| 1010 | rb_event_index(struct ring_buffer_event *event) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1011 | { |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1012 | unsigned long addr = (unsigned long)event; |
| 1013 | |
| 1014 | return (addr & ~PAGE_MASK) - (PAGE_SIZE - BUF_PAGE_SIZE); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1015 | } |
| 1016 | |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 1017 | static int |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1018 | rb_is_commit(struct ring_buffer_per_cpu *cpu_buffer, |
| 1019 | struct ring_buffer_event *event) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1020 | { |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1021 | unsigned long addr = (unsigned long)event; |
| 1022 | unsigned long index; |
| 1023 | |
| 1024 | index = rb_event_index(event); |
| 1025 | addr &= PAGE_MASK; |
| 1026 | |
| 1027 | return cpu_buffer->commit_page->page == (void *)addr && |
| 1028 | rb_commit_index(cpu_buffer) == index; |
| 1029 | } |
| 1030 | |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 1031 | static void |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1032 | rb_set_commit_event(struct ring_buffer_per_cpu *cpu_buffer, |
| 1033 | struct ring_buffer_event *event) |
| 1034 | { |
| 1035 | unsigned long addr = (unsigned long)event; |
| 1036 | unsigned long index; |
| 1037 | |
| 1038 | index = rb_event_index(event); |
| 1039 | addr &= PAGE_MASK; |
| 1040 | |
| 1041 | while (cpu_buffer->commit_page->page != (void *)addr) { |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1042 | if (RB_WARN_ON(cpu_buffer, |
| 1043 | cpu_buffer->commit_page == cpu_buffer->tail_page)) |
| 1044 | return; |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 1045 | cpu_buffer->commit_page->page->commit = |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1046 | cpu_buffer->commit_page->write; |
| 1047 | rb_inc_page(cpu_buffer, &cpu_buffer->commit_page); |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 1048 | cpu_buffer->write_stamp = |
| 1049 | cpu_buffer->commit_page->page->time_stamp; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | /* Now set the commit to the event's index */ |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 1053 | local_set(&cpu_buffer->commit_page->page->commit, index); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1054 | } |
| 1055 | |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 1056 | static void |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1057 | rb_set_commit_to_write(struct ring_buffer_per_cpu *cpu_buffer) |
| 1058 | { |
| 1059 | /* |
| 1060 | * We only race with interrupts and NMIs on this CPU. |
| 1061 | * If we own the commit event, then we can commit |
| 1062 | * all others that interrupted us, since the interruptions |
| 1063 | * are in stack format (they finish before they come |
| 1064 | * back to us). This allows us to do a simple loop to |
| 1065 | * assign the commit to the tail. |
| 1066 | */ |
Steven Rostedt | a8ccf1d | 2008-12-23 11:32:24 -0500 | [diff] [blame] | 1067 | again: |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1068 | while (cpu_buffer->commit_page != cpu_buffer->tail_page) { |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 1069 | cpu_buffer->commit_page->page->commit = |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1070 | cpu_buffer->commit_page->write; |
| 1071 | rb_inc_page(cpu_buffer, &cpu_buffer->commit_page); |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 1072 | cpu_buffer->write_stamp = |
| 1073 | cpu_buffer->commit_page->page->time_stamp; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1074 | /* add barrier to keep gcc from optimizing too much */ |
| 1075 | barrier(); |
| 1076 | } |
| 1077 | while (rb_commit_index(cpu_buffer) != |
| 1078 | rb_page_write(cpu_buffer->commit_page)) { |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 1079 | cpu_buffer->commit_page->page->commit = |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1080 | cpu_buffer->commit_page->write; |
| 1081 | barrier(); |
| 1082 | } |
Steven Rostedt | a8ccf1d | 2008-12-23 11:32:24 -0500 | [diff] [blame] | 1083 | |
| 1084 | /* again, keep gcc from optimizing */ |
| 1085 | barrier(); |
| 1086 | |
| 1087 | /* |
| 1088 | * If an interrupt came in just after the first while loop |
| 1089 | * and pushed the tail page forward, we will be left with |
| 1090 | * a dangling commit that will never go forward. |
| 1091 | */ |
| 1092 | if (unlikely(cpu_buffer->commit_page != cpu_buffer->tail_page)) |
| 1093 | goto again; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1094 | } |
| 1095 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1096 | 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] | 1097 | { |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 1098 | cpu_buffer->read_stamp = cpu_buffer->reader_page->page->time_stamp; |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 1099 | cpu_buffer->reader_page->read = 0; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1100 | } |
| 1101 | |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 1102 | static void rb_inc_iter(struct ring_buffer_iter *iter) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1103 | { |
| 1104 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 1105 | |
| 1106 | /* |
| 1107 | * The iterator could be on the reader page (it starts there). |
| 1108 | * But the head could have moved, since the reader was |
| 1109 | * found. Check for this case and assign the iterator |
| 1110 | * to the head page instead of next. |
| 1111 | */ |
| 1112 | if (iter->head_page == cpu_buffer->reader_page) |
| 1113 | iter->head_page = cpu_buffer->head_page; |
| 1114 | else |
| 1115 | rb_inc_page(cpu_buffer, &iter->head_page); |
| 1116 | |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 1117 | iter->read_stamp = iter->head_page->page->time_stamp; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1118 | iter->head = 0; |
| 1119 | } |
| 1120 | |
| 1121 | /** |
| 1122 | * ring_buffer_update_event - update event type and data |
| 1123 | * @event: the even to update |
| 1124 | * @type: the type of event |
| 1125 | * @length: the size of the event field in the ring buffer |
| 1126 | * |
| 1127 | * Update the type and data fields of the event. The length |
| 1128 | * is the actual size that is written to the ring buffer, |
| 1129 | * and with this, we can determine what to place into the |
| 1130 | * data field. |
| 1131 | */ |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 1132 | static void |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1133 | rb_update_event(struct ring_buffer_event *event, |
| 1134 | unsigned type, unsigned length) |
| 1135 | { |
| 1136 | event->type = type; |
| 1137 | |
| 1138 | switch (type) { |
| 1139 | |
| 1140 | case RINGBUF_TYPE_PADDING: |
| 1141 | break; |
| 1142 | |
| 1143 | case RINGBUF_TYPE_TIME_EXTEND: |
Andrew Morton | 67d3472 | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 1144 | event->len = DIV_ROUND_UP(RB_LEN_TIME_EXTEND, RB_ALIGNMENT); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1145 | break; |
| 1146 | |
| 1147 | case RINGBUF_TYPE_TIME_STAMP: |
Andrew Morton | 67d3472 | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 1148 | event->len = DIV_ROUND_UP(RB_LEN_TIME_STAMP, RB_ALIGNMENT); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1149 | break; |
| 1150 | |
| 1151 | case RINGBUF_TYPE_DATA: |
| 1152 | length -= RB_EVNT_HDR_SIZE; |
| 1153 | if (length > RB_MAX_SMALL_DATA) { |
| 1154 | event->len = 0; |
| 1155 | event->array[0] = length; |
| 1156 | } else |
Andrew Morton | 67d3472 | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 1157 | event->len = DIV_ROUND_UP(length, RB_ALIGNMENT); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1158 | break; |
| 1159 | default: |
| 1160 | BUG(); |
| 1161 | } |
| 1162 | } |
| 1163 | |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 1164 | static unsigned rb_calculate_event_length(unsigned length) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1165 | { |
| 1166 | struct ring_buffer_event event; /* Used only for sizeof array */ |
| 1167 | |
| 1168 | /* zero length can cause confusions */ |
| 1169 | if (!length) |
| 1170 | length = 1; |
| 1171 | |
| 1172 | if (length > RB_MAX_SMALL_DATA) |
| 1173 | length += sizeof(event.array[0]); |
| 1174 | |
| 1175 | length += RB_EVNT_HDR_SIZE; |
| 1176 | length = ALIGN(length, RB_ALIGNMENT); |
| 1177 | |
| 1178 | return length; |
| 1179 | } |
| 1180 | |
| 1181 | static struct ring_buffer_event * |
| 1182 | __rb_reserve_next(struct ring_buffer_per_cpu *cpu_buffer, |
| 1183 | unsigned type, unsigned long length, u64 *ts) |
| 1184 | { |
Steven Rostedt | 98db8df | 2008-12-23 11:32:25 -0500 | [diff] [blame] | 1185 | struct buffer_page *tail_page, *head_page, *reader_page, *commit_page; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1186 | unsigned long tail, write; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1187 | struct ring_buffer *buffer = cpu_buffer->buffer; |
| 1188 | struct ring_buffer_event *event; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1189 | unsigned long flags; |
Steven Rostedt | 78d904b | 2009-02-05 18:43:07 -0500 | [diff] [blame] | 1190 | bool lock_taken = false; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1191 | |
Steven Rostedt | 98db8df | 2008-12-23 11:32:25 -0500 | [diff] [blame] | 1192 | commit_page = cpu_buffer->commit_page; |
| 1193 | /* we just need to protect against interrupts */ |
| 1194 | barrier(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1195 | tail_page = cpu_buffer->tail_page; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1196 | write = local_add_return(length, &tail_page->write); |
| 1197 | tail = write - length; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1198 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1199 | /* See if we shot pass the end of this buffer page */ |
| 1200 | if (write > BUF_PAGE_SIZE) { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1201 | struct buffer_page *next_page = tail_page; |
| 1202 | |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 1203 | local_irq_save(flags); |
Steven Rostedt | 78d904b | 2009-02-05 18:43:07 -0500 | [diff] [blame] | 1204 | /* |
Steven Rostedt | a81bd80 | 2009-02-06 01:45:16 -0500 | [diff] [blame] | 1205 | * Since the write to the buffer is still not |
| 1206 | * fully lockless, we must be careful with NMIs. |
| 1207 | * The locks in the writers are taken when a write |
| 1208 | * crosses to a new page. The locks protect against |
| 1209 | * races with the readers (this will soon be fixed |
| 1210 | * with a lockless solution). |
| 1211 | * |
| 1212 | * Because we can not protect against NMIs, and we |
| 1213 | * want to keep traces reentrant, we need to manage |
| 1214 | * what happens when we are in an NMI. |
| 1215 | * |
Steven Rostedt | 78d904b | 2009-02-05 18:43:07 -0500 | [diff] [blame] | 1216 | * NMIs can happen after we take the lock. |
| 1217 | * If we are in an NMI, only take the lock |
| 1218 | * if it is not already taken. Otherwise |
| 1219 | * simply fail. |
| 1220 | */ |
Steven Rostedt | a81bd80 | 2009-02-06 01:45:16 -0500 | [diff] [blame] | 1221 | if (unlikely(in_nmi())) { |
Steven Rostedt | 78d904b | 2009-02-05 18:43:07 -0500 | [diff] [blame] | 1222 | if (!__raw_spin_trylock(&cpu_buffer->lock)) |
Steven Rostedt | 45141d4 | 2009-02-12 13:19:48 -0500 | [diff] [blame] | 1223 | goto out_reset; |
Steven Rostedt | 78d904b | 2009-02-05 18:43:07 -0500 | [diff] [blame] | 1224 | } else |
| 1225 | __raw_spin_lock(&cpu_buffer->lock); |
| 1226 | |
| 1227 | lock_taken = true; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1228 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1229 | rb_inc_page(cpu_buffer, &next_page); |
| 1230 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1231 | head_page = cpu_buffer->head_page; |
| 1232 | reader_page = cpu_buffer->reader_page; |
| 1233 | |
| 1234 | /* we grabbed the lock before incrementing */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1235 | if (RB_WARN_ON(cpu_buffer, next_page == reader_page)) |
Steven Rostedt | 45141d4 | 2009-02-12 13:19:48 -0500 | [diff] [blame] | 1236 | goto out_reset; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1237 | |
| 1238 | /* |
| 1239 | * If for some reason, we had an interrupt storm that made |
| 1240 | * it all the way around the buffer, bail, and warn |
| 1241 | * about it. |
| 1242 | */ |
Steven Rostedt | 98db8df | 2008-12-23 11:32:25 -0500 | [diff] [blame] | 1243 | if (unlikely(next_page == commit_page)) { |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1244 | WARN_ON_ONCE(1); |
Steven Rostedt | 45141d4 | 2009-02-12 13:19:48 -0500 | [diff] [blame] | 1245 | goto out_reset; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1246 | } |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1247 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1248 | if (next_page == head_page) { |
Lai Jiangshan | 6f3b344 | 2009-01-12 11:06:18 +0800 | [diff] [blame] | 1249 | if (!(buffer->flags & RB_FL_OVERWRITE)) |
Steven Rostedt | 45141d4 | 2009-02-12 13:19:48 -0500 | [diff] [blame] | 1250 | goto out_reset; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1251 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1252 | /* tail_page has not moved yet? */ |
| 1253 | if (tail_page == cpu_buffer->tail_page) { |
| 1254 | /* count overflows */ |
| 1255 | rb_update_overflow(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1256 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1257 | rb_inc_page(cpu_buffer, &head_page); |
| 1258 | cpu_buffer->head_page = head_page; |
| 1259 | cpu_buffer->head_page->read = 0; |
| 1260 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1261 | } |
| 1262 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1263 | /* |
| 1264 | * If the tail page is still the same as what we think |
| 1265 | * it is, then it is up to us to update the tail |
| 1266 | * pointer. |
| 1267 | */ |
| 1268 | if (tail_page == cpu_buffer->tail_page) { |
| 1269 | local_set(&next_page->write, 0); |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 1270 | local_set(&next_page->page->commit, 0); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1271 | cpu_buffer->tail_page = next_page; |
| 1272 | |
| 1273 | /* reread the time stamp */ |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 1274 | *ts = ring_buffer_time_stamp(buffer, cpu_buffer->cpu); |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 1275 | cpu_buffer->tail_page->page->time_stamp = *ts; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | /* |
| 1279 | * The actual tail page has moved forward. |
| 1280 | */ |
| 1281 | if (tail < BUF_PAGE_SIZE) { |
| 1282 | /* Mark the rest of the page with padding */ |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 1283 | event = __rb_page_index(tail_page, tail); |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 1284 | rb_event_set_padding(event); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1285 | } |
| 1286 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1287 | if (tail <= BUF_PAGE_SIZE) |
| 1288 | /* Set the write back to the previous setting */ |
| 1289 | local_set(&tail_page->write, tail); |
| 1290 | |
| 1291 | /* |
| 1292 | * If this was a commit entry that failed, |
| 1293 | * increment that too |
| 1294 | */ |
| 1295 | if (tail_page == cpu_buffer->commit_page && |
| 1296 | tail == rb_commit_index(cpu_buffer)) { |
| 1297 | rb_set_commit_to_write(cpu_buffer); |
| 1298 | } |
| 1299 | |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 1300 | __raw_spin_unlock(&cpu_buffer->lock); |
| 1301 | local_irq_restore(flags); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1302 | |
| 1303 | /* fail and let the caller try again */ |
| 1304 | return ERR_PTR(-EAGAIN); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1305 | } |
| 1306 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1307 | /* We reserved something on the buffer */ |
| 1308 | |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1309 | if (RB_WARN_ON(cpu_buffer, write > BUF_PAGE_SIZE)) |
| 1310 | return NULL; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1311 | |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 1312 | event = __rb_page_index(tail_page, tail); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1313 | rb_update_event(event, type, length); |
| 1314 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1315 | /* |
| 1316 | * If this is a commit and the tail is zero, then update |
| 1317 | * this page's time stamp. |
| 1318 | */ |
| 1319 | if (!tail && rb_is_commit(cpu_buffer, event)) |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 1320 | cpu_buffer->commit_page->page->time_stamp = *ts; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1321 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1322 | return event; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1323 | |
Steven Rostedt | 45141d4 | 2009-02-12 13:19:48 -0500 | [diff] [blame] | 1324 | out_reset: |
Lai Jiangshan | 6f3b344 | 2009-01-12 11:06:18 +0800 | [diff] [blame] | 1325 | /* reset write */ |
| 1326 | if (tail <= BUF_PAGE_SIZE) |
| 1327 | local_set(&tail_page->write, tail); |
| 1328 | |
Steven Rostedt | 78d904b | 2009-02-05 18:43:07 -0500 | [diff] [blame] | 1329 | if (likely(lock_taken)) |
| 1330 | __raw_spin_unlock(&cpu_buffer->lock); |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 1331 | local_irq_restore(flags); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1332 | return NULL; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | static int |
| 1336 | rb_add_time_stamp(struct ring_buffer_per_cpu *cpu_buffer, |
| 1337 | u64 *ts, u64 *delta) |
| 1338 | { |
| 1339 | struct ring_buffer_event *event; |
| 1340 | static int once; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1341 | int ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1342 | |
| 1343 | if (unlikely(*delta > (1ULL << 59) && !once++)) { |
| 1344 | printk(KERN_WARNING "Delta way too big! %llu" |
| 1345 | " ts=%llu write stamp = %llu\n", |
Stephen Rothwell | e2862c9 | 2008-10-27 17:43:28 +1100 | [diff] [blame] | 1346 | (unsigned long long)*delta, |
| 1347 | (unsigned long long)*ts, |
| 1348 | (unsigned long long)cpu_buffer->write_stamp); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1349 | WARN_ON(1); |
| 1350 | } |
| 1351 | |
| 1352 | /* |
| 1353 | * The delta is too big, we to add a |
| 1354 | * new timestamp. |
| 1355 | */ |
| 1356 | event = __rb_reserve_next(cpu_buffer, |
| 1357 | RINGBUF_TYPE_TIME_EXTEND, |
| 1358 | RB_LEN_TIME_EXTEND, |
| 1359 | ts); |
| 1360 | if (!event) |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1361 | return -EBUSY; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1362 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1363 | if (PTR_ERR(event) == -EAGAIN) |
| 1364 | return -EAGAIN; |
| 1365 | |
| 1366 | /* Only a commited time event can update the write stamp */ |
| 1367 | if (rb_is_commit(cpu_buffer, event)) { |
| 1368 | /* |
| 1369 | * If this is the first on the page, then we need to |
| 1370 | * update the page itself, and just put in a zero. |
| 1371 | */ |
| 1372 | if (rb_event_index(event)) { |
| 1373 | event->time_delta = *delta & TS_MASK; |
| 1374 | event->array[0] = *delta >> TS_SHIFT; |
| 1375 | } else { |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 1376 | cpu_buffer->commit_page->page->time_stamp = *ts; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1377 | event->time_delta = 0; |
| 1378 | event->array[0] = 0; |
| 1379 | } |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1380 | cpu_buffer->write_stamp = *ts; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1381 | /* let the caller know this was the commit */ |
| 1382 | ret = 1; |
| 1383 | } else { |
| 1384 | /* Darn, this is just wasted space */ |
| 1385 | event->time_delta = 0; |
| 1386 | event->array[0] = 0; |
| 1387 | ret = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1388 | } |
| 1389 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1390 | *delta = 0; |
| 1391 | |
| 1392 | return ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | static struct ring_buffer_event * |
| 1396 | rb_reserve_next_event(struct ring_buffer_per_cpu *cpu_buffer, |
| 1397 | unsigned type, unsigned long length) |
| 1398 | { |
| 1399 | struct ring_buffer_event *event; |
| 1400 | u64 ts, delta; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1401 | int commit = 0; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 1402 | int nr_loops = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1403 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1404 | again: |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 1405 | /* |
| 1406 | * We allow for interrupts to reenter here and do a trace. |
| 1407 | * If one does, it will cause this original code to loop |
| 1408 | * back here. Even with heavy interrupts happening, this |
| 1409 | * should only happen a few times in a row. If this happens |
| 1410 | * 1000 times in a row, there must be either an interrupt |
| 1411 | * storm or we have something buggy. |
| 1412 | * Bail! |
| 1413 | */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 1414 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 1000)) |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 1415 | return NULL; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 1416 | |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 1417 | ts = ring_buffer_time_stamp(cpu_buffer->buffer, cpu_buffer->cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1418 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1419 | /* |
| 1420 | * Only the first commit can update the timestamp. |
| 1421 | * Yes there is a race here. If an interrupt comes in |
| 1422 | * just after the conditional and it traces too, then it |
| 1423 | * will also check the deltas. More than one timestamp may |
| 1424 | * also be made. But only the entry that did the actual |
| 1425 | * commit will be something other than zero. |
| 1426 | */ |
| 1427 | if (cpu_buffer->tail_page == cpu_buffer->commit_page && |
| 1428 | rb_page_write(cpu_buffer->tail_page) == |
| 1429 | rb_commit_index(cpu_buffer)) { |
| 1430 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1431 | delta = ts - cpu_buffer->write_stamp; |
| 1432 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1433 | /* make sure this delta is calculated here */ |
| 1434 | barrier(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1435 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1436 | /* Did the write stamp get updated already? */ |
| 1437 | if (unlikely(ts < cpu_buffer->write_stamp)) |
Steven Rostedt | 4143c5c | 2008-11-10 21:46:01 -0500 | [diff] [blame] | 1438 | delta = 0; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1439 | |
| 1440 | if (test_time_stamp(delta)) { |
| 1441 | |
| 1442 | commit = rb_add_time_stamp(cpu_buffer, &ts, &delta); |
| 1443 | |
| 1444 | if (commit == -EBUSY) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1445 | return NULL; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1446 | |
| 1447 | if (commit == -EAGAIN) |
| 1448 | goto again; |
| 1449 | |
| 1450 | RB_WARN_ON(cpu_buffer, commit < 0); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1451 | } |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1452 | } else |
| 1453 | /* Non commits have zero deltas */ |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1454 | delta = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1455 | |
| 1456 | event = __rb_reserve_next(cpu_buffer, type, length, &ts); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1457 | if (PTR_ERR(event) == -EAGAIN) |
| 1458 | goto again; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1459 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1460 | if (!event) { |
| 1461 | if (unlikely(commit)) |
| 1462 | /* |
| 1463 | * Ouch! We needed a timestamp and it was commited. But |
| 1464 | * we didn't get our event reserved. |
| 1465 | */ |
| 1466 | rb_set_commit_to_write(cpu_buffer); |
| 1467 | return NULL; |
| 1468 | } |
| 1469 | |
| 1470 | /* |
| 1471 | * If the timestamp was commited, make the commit our entry |
| 1472 | * now so that we will update it when needed. |
| 1473 | */ |
| 1474 | if (commit) |
| 1475 | rb_set_commit_event(cpu_buffer, event); |
| 1476 | else if (!rb_is_commit(cpu_buffer, event)) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1477 | delta = 0; |
| 1478 | |
| 1479 | event->time_delta = delta; |
| 1480 | |
| 1481 | return event; |
| 1482 | } |
| 1483 | |
Steven Rostedt | 261842b | 2009-04-16 21:41:52 -0400 | [diff] [blame^] | 1484 | static int trace_irq_level(void) |
| 1485 | { |
| 1486 | return hardirq_count() + softirq_count() + in_nmi(); |
| 1487 | } |
| 1488 | |
| 1489 | static int trace_recursive_lock(void) |
| 1490 | { |
| 1491 | int level; |
| 1492 | |
| 1493 | level = trace_irq_level(); |
| 1494 | |
| 1495 | if (unlikely(current->trace_recursion & (1 << level))) { |
| 1496 | /* Disable all tracing before we do anything else */ |
| 1497 | tracing_off_permanent(); |
| 1498 | WARN_ON_ONCE(1); |
| 1499 | return -1; |
| 1500 | } |
| 1501 | |
| 1502 | current->trace_recursion |= 1 << level; |
| 1503 | |
| 1504 | return 0; |
| 1505 | } |
| 1506 | |
| 1507 | static void trace_recursive_unlock(void) |
| 1508 | { |
| 1509 | int level; |
| 1510 | |
| 1511 | level = trace_irq_level(); |
| 1512 | |
| 1513 | WARN_ON_ONCE(!current->trace_recursion & (1 << level)); |
| 1514 | |
| 1515 | current->trace_recursion &= ~(1 << level); |
| 1516 | } |
| 1517 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1518 | static DEFINE_PER_CPU(int, rb_need_resched); |
| 1519 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1520 | /** |
| 1521 | * ring_buffer_lock_reserve - reserve a part of the buffer |
| 1522 | * @buffer: the ring buffer to reserve from |
| 1523 | * @length: the length of the data to reserve (excluding event header) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1524 | * |
| 1525 | * Returns a reseverd event on the ring buffer to copy directly to. |
| 1526 | * The user of this interface will need to get the body to write into |
| 1527 | * and can use the ring_buffer_event_data() interface. |
| 1528 | * |
| 1529 | * The length is the length of the data needed, not the event length |
| 1530 | * which also includes the event header. |
| 1531 | * |
| 1532 | * Must be paired with ring_buffer_unlock_commit, unless NULL is returned. |
| 1533 | * If NULL is returned, then nothing has been allocated or locked. |
| 1534 | */ |
| 1535 | struct ring_buffer_event * |
Arnaldo Carvalho de Melo | 0a98775 | 2009-02-05 16:12:56 -0200 | [diff] [blame] | 1536 | ring_buffer_lock_reserve(struct ring_buffer *buffer, unsigned long length) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1537 | { |
| 1538 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1539 | struct ring_buffer_event *event; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1540 | int cpu, resched; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1541 | |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 1542 | if (ring_buffer_flags != RB_BUFFERS_ON) |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 1543 | return NULL; |
| 1544 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1545 | if (atomic_read(&buffer->record_disabled)) |
| 1546 | return NULL; |
| 1547 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1548 | /* If we are tracing schedule, we don't want to recurse */ |
Steven Rostedt | 182e9f5 | 2008-11-03 23:15:56 -0500 | [diff] [blame] | 1549 | resched = ftrace_preempt_disable(); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1550 | |
Steven Rostedt | 261842b | 2009-04-16 21:41:52 -0400 | [diff] [blame^] | 1551 | if (trace_recursive_lock()) |
| 1552 | goto out_nocheck; |
| 1553 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1554 | cpu = raw_smp_processor_id(); |
| 1555 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 1556 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1557 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1558 | |
| 1559 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1560 | |
| 1561 | if (atomic_read(&cpu_buffer->record_disabled)) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1562 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1563 | |
| 1564 | length = rb_calculate_event_length(length); |
| 1565 | if (length > BUF_PAGE_SIZE) |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1566 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1567 | |
| 1568 | event = rb_reserve_next_event(cpu_buffer, RINGBUF_TYPE_DATA, length); |
| 1569 | if (!event) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1570 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1571 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1572 | /* |
| 1573 | * Need to store resched state on this cpu. |
| 1574 | * Only the first needs to. |
| 1575 | */ |
| 1576 | |
| 1577 | if (preempt_count() == 1) |
| 1578 | per_cpu(rb_need_resched, cpu) = resched; |
| 1579 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1580 | return event; |
| 1581 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1582 | out: |
Steven Rostedt | 261842b | 2009-04-16 21:41:52 -0400 | [diff] [blame^] | 1583 | trace_recursive_unlock(); |
| 1584 | |
| 1585 | out_nocheck: |
Steven Rostedt | 182e9f5 | 2008-11-03 23:15:56 -0500 | [diff] [blame] | 1586 | ftrace_preempt_enable(resched); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1587 | return NULL; |
| 1588 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 1589 | EXPORT_SYMBOL_GPL(ring_buffer_lock_reserve); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1590 | |
| 1591 | static void rb_commit(struct ring_buffer_per_cpu *cpu_buffer, |
| 1592 | struct ring_buffer_event *event) |
| 1593 | { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1594 | cpu_buffer->entries++; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1595 | |
| 1596 | /* Only process further if we own the commit */ |
| 1597 | if (!rb_is_commit(cpu_buffer, event)) |
| 1598 | return; |
| 1599 | |
| 1600 | cpu_buffer->write_stamp += event->time_delta; |
| 1601 | |
| 1602 | rb_set_commit_to_write(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | /** |
| 1606 | * ring_buffer_unlock_commit - commit a reserved |
| 1607 | * @buffer: The buffer to commit to |
| 1608 | * @event: The event pointer to commit. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1609 | * |
| 1610 | * This commits the data to the ring buffer, and releases any locks held. |
| 1611 | * |
| 1612 | * Must be paired with ring_buffer_lock_reserve. |
| 1613 | */ |
| 1614 | int ring_buffer_unlock_commit(struct ring_buffer *buffer, |
Arnaldo Carvalho de Melo | 0a98775 | 2009-02-05 16:12:56 -0200 | [diff] [blame] | 1615 | struct ring_buffer_event *event) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1616 | { |
| 1617 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1618 | int cpu = raw_smp_processor_id(); |
| 1619 | |
| 1620 | cpu_buffer = buffer->buffers[cpu]; |
| 1621 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1622 | rb_commit(cpu_buffer, event); |
| 1623 | |
Steven Rostedt | 261842b | 2009-04-16 21:41:52 -0400 | [diff] [blame^] | 1624 | trace_recursive_unlock(); |
| 1625 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1626 | /* |
| 1627 | * Only the last preempt count needs to restore preemption. |
| 1628 | */ |
Steven Rostedt | 182e9f5 | 2008-11-03 23:15:56 -0500 | [diff] [blame] | 1629 | if (preempt_count() == 1) |
| 1630 | ftrace_preempt_enable(per_cpu(rb_need_resched, cpu)); |
| 1631 | else |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1632 | preempt_enable_no_resched_notrace(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1633 | |
| 1634 | return 0; |
| 1635 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 1636 | EXPORT_SYMBOL_GPL(ring_buffer_unlock_commit); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1637 | |
| 1638 | /** |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 1639 | * ring_buffer_event_discard - discard any event in the ring buffer |
| 1640 | * @event: the event to discard |
| 1641 | * |
| 1642 | * Sometimes a event that is in the ring buffer needs to be ignored. |
| 1643 | * This function lets the user discard an event in the ring buffer |
| 1644 | * and then that event will not be read later. |
| 1645 | * |
| 1646 | * Note, it is up to the user to be careful with this, and protect |
| 1647 | * against races. If the user discards an event that has been consumed |
| 1648 | * it is possible that it could corrupt the ring buffer. |
| 1649 | */ |
| 1650 | void ring_buffer_event_discard(struct ring_buffer_event *event) |
| 1651 | { |
| 1652 | event->type = RINGBUF_TYPE_PADDING; |
| 1653 | /* time delta must be non zero */ |
| 1654 | if (!event->time_delta) |
| 1655 | event->time_delta = 1; |
| 1656 | } |
| 1657 | EXPORT_SYMBOL_GPL(ring_buffer_event_discard); |
| 1658 | |
| 1659 | /** |
| 1660 | * ring_buffer_commit_discard - discard an event that has not been committed |
| 1661 | * @buffer: the ring buffer |
| 1662 | * @event: non committed event to discard |
| 1663 | * |
| 1664 | * This is similar to ring_buffer_event_discard but must only be |
| 1665 | * performed on an event that has not been committed yet. The difference |
| 1666 | * is that this will also try to free the event from the ring buffer |
| 1667 | * if another event has not been added behind it. |
| 1668 | * |
| 1669 | * If another event has been added behind it, it will set the event |
| 1670 | * up as discarded, and perform the commit. |
| 1671 | * |
| 1672 | * If this function is called, do not call ring_buffer_unlock_commit on |
| 1673 | * the event. |
| 1674 | */ |
| 1675 | void ring_buffer_discard_commit(struct ring_buffer *buffer, |
| 1676 | struct ring_buffer_event *event) |
| 1677 | { |
| 1678 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1679 | unsigned long new_index, old_index; |
| 1680 | struct buffer_page *bpage; |
| 1681 | unsigned long index; |
| 1682 | unsigned long addr; |
| 1683 | int cpu; |
| 1684 | |
| 1685 | /* The event is discarded regardless */ |
| 1686 | ring_buffer_event_discard(event); |
| 1687 | |
| 1688 | /* |
| 1689 | * This must only be called if the event has not been |
| 1690 | * committed yet. Thus we can assume that preemption |
| 1691 | * is still disabled. |
| 1692 | */ |
| 1693 | RB_WARN_ON(buffer, !preempt_count()); |
| 1694 | |
| 1695 | cpu = smp_processor_id(); |
| 1696 | cpu_buffer = buffer->buffers[cpu]; |
| 1697 | |
| 1698 | new_index = rb_event_index(event); |
| 1699 | old_index = new_index + rb_event_length(event); |
| 1700 | addr = (unsigned long)event; |
| 1701 | addr &= PAGE_MASK; |
| 1702 | |
| 1703 | bpage = cpu_buffer->tail_page; |
| 1704 | |
| 1705 | if (bpage == (void *)addr && rb_page_write(bpage) == old_index) { |
| 1706 | /* |
| 1707 | * This is on the tail page. It is possible that |
| 1708 | * a write could come in and move the tail page |
| 1709 | * and write to the next page. That is fine |
| 1710 | * because we just shorten what is on this page. |
| 1711 | */ |
| 1712 | index = local_cmpxchg(&bpage->write, old_index, new_index); |
| 1713 | if (index == old_index) |
| 1714 | goto out; |
| 1715 | } |
| 1716 | |
| 1717 | /* |
| 1718 | * The commit is still visible by the reader, so we |
| 1719 | * must increment entries. |
| 1720 | */ |
| 1721 | cpu_buffer->entries++; |
| 1722 | out: |
| 1723 | /* |
| 1724 | * If a write came in and pushed the tail page |
| 1725 | * we still need to update the commit pointer |
| 1726 | * if we were the commit. |
| 1727 | */ |
| 1728 | if (rb_is_commit(cpu_buffer, event)) |
| 1729 | rb_set_commit_to_write(cpu_buffer); |
| 1730 | |
| 1731 | /* |
| 1732 | * Only the last preempt count needs to restore preemption. |
| 1733 | */ |
| 1734 | if (preempt_count() == 1) |
| 1735 | ftrace_preempt_enable(per_cpu(rb_need_resched, cpu)); |
| 1736 | else |
| 1737 | preempt_enable_no_resched_notrace(); |
| 1738 | |
| 1739 | } |
| 1740 | EXPORT_SYMBOL_GPL(ring_buffer_discard_commit); |
| 1741 | |
| 1742 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1743 | * ring_buffer_write - write data to the buffer without reserving |
| 1744 | * @buffer: The ring buffer to write to. |
| 1745 | * @length: The length of the data being written (excluding the event header) |
| 1746 | * @data: The data to write to the buffer. |
| 1747 | * |
| 1748 | * This is like ring_buffer_lock_reserve and ring_buffer_unlock_commit as |
| 1749 | * one function. If you already have the data to write to the buffer, it |
| 1750 | * may be easier to simply call this function. |
| 1751 | * |
| 1752 | * Note, like ring_buffer_lock_reserve, the length is the length of the data |
| 1753 | * and not the length of the event which would hold the header. |
| 1754 | */ |
| 1755 | int ring_buffer_write(struct ring_buffer *buffer, |
| 1756 | unsigned long length, |
| 1757 | void *data) |
| 1758 | { |
| 1759 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1760 | struct ring_buffer_event *event; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1761 | unsigned long event_length; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1762 | void *body; |
| 1763 | int ret = -EBUSY; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1764 | int cpu, resched; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1765 | |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 1766 | if (ring_buffer_flags != RB_BUFFERS_ON) |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 1767 | return -EBUSY; |
| 1768 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1769 | if (atomic_read(&buffer->record_disabled)) |
| 1770 | return -EBUSY; |
| 1771 | |
Steven Rostedt | 182e9f5 | 2008-11-03 23:15:56 -0500 | [diff] [blame] | 1772 | resched = ftrace_preempt_disable(); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1773 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1774 | cpu = raw_smp_processor_id(); |
| 1775 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 1776 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1777 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1778 | |
| 1779 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1780 | |
| 1781 | if (atomic_read(&cpu_buffer->record_disabled)) |
| 1782 | goto out; |
| 1783 | |
| 1784 | event_length = rb_calculate_event_length(length); |
| 1785 | event = rb_reserve_next_event(cpu_buffer, |
| 1786 | RINGBUF_TYPE_DATA, event_length); |
| 1787 | if (!event) |
| 1788 | goto out; |
| 1789 | |
| 1790 | body = rb_event_data(event); |
| 1791 | |
| 1792 | memcpy(body, data, length); |
| 1793 | |
| 1794 | rb_commit(cpu_buffer, event); |
| 1795 | |
| 1796 | ret = 0; |
| 1797 | out: |
Steven Rostedt | 182e9f5 | 2008-11-03 23:15:56 -0500 | [diff] [blame] | 1798 | ftrace_preempt_enable(resched); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1799 | |
| 1800 | return ret; |
| 1801 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 1802 | EXPORT_SYMBOL_GPL(ring_buffer_write); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1803 | |
Andrew Morton | 34a148b | 2009-01-09 12:27:09 -0800 | [diff] [blame] | 1804 | static int rb_per_cpu_empty(struct ring_buffer_per_cpu *cpu_buffer) |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 1805 | { |
| 1806 | struct buffer_page *reader = cpu_buffer->reader_page; |
| 1807 | struct buffer_page *head = cpu_buffer->head_page; |
| 1808 | struct buffer_page *commit = cpu_buffer->commit_page; |
| 1809 | |
| 1810 | return reader->read == rb_page_commit(reader) && |
| 1811 | (commit == reader || |
| 1812 | (commit == head && |
| 1813 | head->read == rb_page_commit(commit))); |
| 1814 | } |
| 1815 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1816 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1817 | * ring_buffer_record_disable - stop all writes into the buffer |
| 1818 | * @buffer: The ring buffer to stop writes to. |
| 1819 | * |
| 1820 | * This prevents all writes to the buffer. Any attempt to write |
| 1821 | * to the buffer after this will fail and return NULL. |
| 1822 | * |
| 1823 | * The caller should call synchronize_sched() after this. |
| 1824 | */ |
| 1825 | void ring_buffer_record_disable(struct ring_buffer *buffer) |
| 1826 | { |
| 1827 | atomic_inc(&buffer->record_disabled); |
| 1828 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 1829 | EXPORT_SYMBOL_GPL(ring_buffer_record_disable); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1830 | |
| 1831 | /** |
| 1832 | * ring_buffer_record_enable - enable writes to the buffer |
| 1833 | * @buffer: The ring buffer to enable writes |
| 1834 | * |
| 1835 | * Note, multiple disables will need the same number of enables |
| 1836 | * to truely enable the writing (much like preempt_disable). |
| 1837 | */ |
| 1838 | void ring_buffer_record_enable(struct ring_buffer *buffer) |
| 1839 | { |
| 1840 | atomic_dec(&buffer->record_disabled); |
| 1841 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 1842 | EXPORT_SYMBOL_GPL(ring_buffer_record_enable); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1843 | |
| 1844 | /** |
| 1845 | * ring_buffer_record_disable_cpu - stop all writes into the cpu_buffer |
| 1846 | * @buffer: The ring buffer to stop writes to. |
| 1847 | * @cpu: The CPU buffer to stop |
| 1848 | * |
| 1849 | * This prevents all writes to the buffer. Any attempt to write |
| 1850 | * to the buffer after this will fail and return NULL. |
| 1851 | * |
| 1852 | * The caller should call synchronize_sched() after this. |
| 1853 | */ |
| 1854 | void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu) |
| 1855 | { |
| 1856 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1857 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 1858 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 1859 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1860 | |
| 1861 | cpu_buffer = buffer->buffers[cpu]; |
| 1862 | atomic_inc(&cpu_buffer->record_disabled); |
| 1863 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 1864 | EXPORT_SYMBOL_GPL(ring_buffer_record_disable_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1865 | |
| 1866 | /** |
| 1867 | * ring_buffer_record_enable_cpu - enable writes to the buffer |
| 1868 | * @buffer: The ring buffer to enable writes |
| 1869 | * @cpu: The CPU to enable. |
| 1870 | * |
| 1871 | * Note, multiple disables will need the same number of enables |
| 1872 | * to truely enable the writing (much like preempt_disable). |
| 1873 | */ |
| 1874 | void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu) |
| 1875 | { |
| 1876 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1877 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 1878 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 1879 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1880 | |
| 1881 | cpu_buffer = buffer->buffers[cpu]; |
| 1882 | atomic_dec(&cpu_buffer->record_disabled); |
| 1883 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 1884 | EXPORT_SYMBOL_GPL(ring_buffer_record_enable_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1885 | |
| 1886 | /** |
| 1887 | * ring_buffer_entries_cpu - get the number of entries in a cpu buffer |
| 1888 | * @buffer: The ring buffer |
| 1889 | * @cpu: The per CPU buffer to get the entries from. |
| 1890 | */ |
| 1891 | unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu) |
| 1892 | { |
| 1893 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 1894 | unsigned long ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1895 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 1896 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 1897 | return 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1898 | |
| 1899 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1900 | ret = cpu_buffer->entries; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1901 | |
| 1902 | return ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1903 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 1904 | EXPORT_SYMBOL_GPL(ring_buffer_entries_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1905 | |
| 1906 | /** |
| 1907 | * ring_buffer_overrun_cpu - get the number of overruns in a cpu_buffer |
| 1908 | * @buffer: The ring buffer |
| 1909 | * @cpu: The per CPU buffer to get the number of overruns from |
| 1910 | */ |
| 1911 | unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu) |
| 1912 | { |
| 1913 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 1914 | unsigned long ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1915 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 1916 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 1917 | return 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1918 | |
| 1919 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1920 | ret = cpu_buffer->overrun; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1921 | |
| 1922 | return ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1923 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 1924 | EXPORT_SYMBOL_GPL(ring_buffer_overrun_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1925 | |
| 1926 | /** |
| 1927 | * ring_buffer_entries - get the number of entries in a buffer |
| 1928 | * @buffer: The ring buffer |
| 1929 | * |
| 1930 | * Returns the total number of entries in the ring buffer |
| 1931 | * (all CPU entries) |
| 1932 | */ |
| 1933 | unsigned long ring_buffer_entries(struct ring_buffer *buffer) |
| 1934 | { |
| 1935 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1936 | unsigned long entries = 0; |
| 1937 | int cpu; |
| 1938 | |
| 1939 | /* if you care about this being correct, lock the buffer */ |
| 1940 | for_each_buffer_cpu(buffer, cpu) { |
| 1941 | cpu_buffer = buffer->buffers[cpu]; |
| 1942 | entries += cpu_buffer->entries; |
| 1943 | } |
| 1944 | |
| 1945 | return entries; |
| 1946 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 1947 | EXPORT_SYMBOL_GPL(ring_buffer_entries); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1948 | |
| 1949 | /** |
| 1950 | * ring_buffer_overrun_cpu - get the number of overruns in buffer |
| 1951 | * @buffer: The ring buffer |
| 1952 | * |
| 1953 | * Returns the total number of overruns in the ring buffer |
| 1954 | * (all CPU entries) |
| 1955 | */ |
| 1956 | unsigned long ring_buffer_overruns(struct ring_buffer *buffer) |
| 1957 | { |
| 1958 | struct ring_buffer_per_cpu *cpu_buffer; |
| 1959 | unsigned long overruns = 0; |
| 1960 | int cpu; |
| 1961 | |
| 1962 | /* if you care about this being correct, lock the buffer */ |
| 1963 | for_each_buffer_cpu(buffer, cpu) { |
| 1964 | cpu_buffer = buffer->buffers[cpu]; |
| 1965 | overruns += cpu_buffer->overrun; |
| 1966 | } |
| 1967 | |
| 1968 | return overruns; |
| 1969 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 1970 | EXPORT_SYMBOL_GPL(ring_buffer_overruns); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1971 | |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 1972 | static void rb_iter_reset(struct ring_buffer_iter *iter) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1973 | { |
| 1974 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 1975 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1976 | /* Iterator usage is expected to have record disabled */ |
| 1977 | if (list_empty(&cpu_buffer->reader_page->list)) { |
| 1978 | iter->head_page = cpu_buffer->head_page; |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 1979 | iter->head = cpu_buffer->head_page->read; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1980 | } else { |
| 1981 | iter->head_page = cpu_buffer->reader_page; |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 1982 | iter->head = cpu_buffer->reader_page->read; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 1983 | } |
| 1984 | if (iter->head) |
| 1985 | iter->read_stamp = cpu_buffer->read_stamp; |
| 1986 | else |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 1987 | iter->read_stamp = iter->head_page->page->time_stamp; |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 1988 | } |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 1989 | |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 1990 | /** |
| 1991 | * ring_buffer_iter_reset - reset an iterator |
| 1992 | * @iter: The iterator to reset |
| 1993 | * |
| 1994 | * Resets the iterator, so that it will start from the beginning |
| 1995 | * again. |
| 1996 | */ |
| 1997 | void ring_buffer_iter_reset(struct ring_buffer_iter *iter) |
| 1998 | { |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 1999 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 2000 | unsigned long flags; |
| 2001 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2002 | if (!iter) |
| 2003 | return; |
| 2004 | |
| 2005 | cpu_buffer = iter->cpu_buffer; |
| 2006 | |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 2007 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
| 2008 | rb_iter_reset(iter); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2009 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2010 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2011 | EXPORT_SYMBOL_GPL(ring_buffer_iter_reset); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2012 | |
| 2013 | /** |
| 2014 | * ring_buffer_iter_empty - check if an iterator has no more to read |
| 2015 | * @iter: The iterator to check |
| 2016 | */ |
| 2017 | int ring_buffer_iter_empty(struct ring_buffer_iter *iter) |
| 2018 | { |
| 2019 | struct ring_buffer_per_cpu *cpu_buffer; |
| 2020 | |
| 2021 | cpu_buffer = iter->cpu_buffer; |
| 2022 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2023 | return iter->head_page == cpu_buffer->commit_page && |
| 2024 | iter->head == rb_commit_index(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2025 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2026 | EXPORT_SYMBOL_GPL(ring_buffer_iter_empty); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2027 | |
| 2028 | static void |
| 2029 | rb_update_read_stamp(struct ring_buffer_per_cpu *cpu_buffer, |
| 2030 | struct ring_buffer_event *event) |
| 2031 | { |
| 2032 | u64 delta; |
| 2033 | |
| 2034 | switch (event->type) { |
| 2035 | case RINGBUF_TYPE_PADDING: |
| 2036 | return; |
| 2037 | |
| 2038 | case RINGBUF_TYPE_TIME_EXTEND: |
| 2039 | delta = event->array[0]; |
| 2040 | delta <<= TS_SHIFT; |
| 2041 | delta += event->time_delta; |
| 2042 | cpu_buffer->read_stamp += delta; |
| 2043 | return; |
| 2044 | |
| 2045 | case RINGBUF_TYPE_TIME_STAMP: |
| 2046 | /* FIXME: not implemented */ |
| 2047 | return; |
| 2048 | |
| 2049 | case RINGBUF_TYPE_DATA: |
| 2050 | cpu_buffer->read_stamp += event->time_delta; |
| 2051 | return; |
| 2052 | |
| 2053 | default: |
| 2054 | BUG(); |
| 2055 | } |
| 2056 | return; |
| 2057 | } |
| 2058 | |
| 2059 | static void |
| 2060 | rb_update_iter_read_stamp(struct ring_buffer_iter *iter, |
| 2061 | struct ring_buffer_event *event) |
| 2062 | { |
| 2063 | u64 delta; |
| 2064 | |
| 2065 | switch (event->type) { |
| 2066 | case RINGBUF_TYPE_PADDING: |
| 2067 | return; |
| 2068 | |
| 2069 | case RINGBUF_TYPE_TIME_EXTEND: |
| 2070 | delta = event->array[0]; |
| 2071 | delta <<= TS_SHIFT; |
| 2072 | delta += event->time_delta; |
| 2073 | iter->read_stamp += delta; |
| 2074 | return; |
| 2075 | |
| 2076 | case RINGBUF_TYPE_TIME_STAMP: |
| 2077 | /* FIXME: not implemented */ |
| 2078 | return; |
| 2079 | |
| 2080 | case RINGBUF_TYPE_DATA: |
| 2081 | iter->read_stamp += event->time_delta; |
| 2082 | return; |
| 2083 | |
| 2084 | default: |
| 2085 | BUG(); |
| 2086 | } |
| 2087 | return; |
| 2088 | } |
| 2089 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2090 | static struct buffer_page * |
| 2091 | rb_get_reader_page(struct ring_buffer_per_cpu *cpu_buffer) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2092 | { |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2093 | struct buffer_page *reader = NULL; |
| 2094 | unsigned long flags; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 2095 | int nr_loops = 0; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2096 | |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 2097 | local_irq_save(flags); |
| 2098 | __raw_spin_lock(&cpu_buffer->lock); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2099 | |
| 2100 | again: |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 2101 | /* |
| 2102 | * This should normally only loop twice. But because the |
| 2103 | * start of the reader inserts an empty page, it causes |
| 2104 | * a case where we will loop three times. There should be no |
| 2105 | * reason to loop four times (that I know of). |
| 2106 | */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 2107 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 3)) { |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 2108 | reader = NULL; |
| 2109 | goto out; |
| 2110 | } |
| 2111 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2112 | reader = cpu_buffer->reader_page; |
| 2113 | |
| 2114 | /* If there's more to read, return this page */ |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2115 | if (cpu_buffer->reader_page->read < rb_page_size(reader)) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2116 | goto out; |
| 2117 | |
| 2118 | /* Never should we have an index greater than the size */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 2119 | if (RB_WARN_ON(cpu_buffer, |
| 2120 | cpu_buffer->reader_page->read > rb_page_size(reader))) |
| 2121 | goto out; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2122 | |
| 2123 | /* check if we caught up to the tail */ |
| 2124 | reader = NULL; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2125 | if (cpu_buffer->commit_page == cpu_buffer->reader_page) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2126 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2127 | |
| 2128 | /* |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2129 | * Splice the empty reader page into the list around the head. |
| 2130 | * Reset the reader page to size zero. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2131 | */ |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2132 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2133 | reader = cpu_buffer->head_page; |
| 2134 | cpu_buffer->reader_page->list.next = reader->list.next; |
| 2135 | cpu_buffer->reader_page->list.prev = reader->list.prev; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2136 | |
| 2137 | local_set(&cpu_buffer->reader_page->write, 0); |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 2138 | local_set(&cpu_buffer->reader_page->page->commit, 0); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2139 | |
| 2140 | /* Make the reader page now replace the head */ |
| 2141 | reader->list.prev->next = &cpu_buffer->reader_page->list; |
| 2142 | reader->list.next->prev = &cpu_buffer->reader_page->list; |
| 2143 | |
| 2144 | /* |
| 2145 | * If the tail is on the reader, then we must set the head |
| 2146 | * to the inserted page, otherwise we set it one before. |
| 2147 | */ |
| 2148 | cpu_buffer->head_page = cpu_buffer->reader_page; |
| 2149 | |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2150 | if (cpu_buffer->commit_page != reader) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2151 | rb_inc_page(cpu_buffer, &cpu_buffer->head_page); |
| 2152 | |
| 2153 | /* Finally update the reader page to the new head */ |
| 2154 | cpu_buffer->reader_page = reader; |
| 2155 | rb_reset_reader_page(cpu_buffer); |
| 2156 | |
| 2157 | goto again; |
| 2158 | |
| 2159 | out: |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 2160 | __raw_spin_unlock(&cpu_buffer->lock); |
| 2161 | local_irq_restore(flags); |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2162 | |
| 2163 | return reader; |
| 2164 | } |
| 2165 | |
| 2166 | static void rb_advance_reader(struct ring_buffer_per_cpu *cpu_buffer) |
| 2167 | { |
| 2168 | struct ring_buffer_event *event; |
| 2169 | struct buffer_page *reader; |
| 2170 | unsigned length; |
| 2171 | |
| 2172 | reader = rb_get_reader_page(cpu_buffer); |
| 2173 | |
| 2174 | /* This function should not be called when buffer is empty */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 2175 | if (RB_WARN_ON(cpu_buffer, !reader)) |
| 2176 | return; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2177 | |
| 2178 | event = rb_reader_event(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2179 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 2180 | if (event->type == RINGBUF_TYPE_DATA || rb_discarded_event(event)) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2181 | cpu_buffer->entries--; |
| 2182 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2183 | rb_update_read_stamp(cpu_buffer, event); |
| 2184 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2185 | length = rb_event_length(event); |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 2186 | cpu_buffer->reader_page->read += length; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2187 | } |
| 2188 | |
| 2189 | static void rb_advance_iter(struct ring_buffer_iter *iter) |
| 2190 | { |
| 2191 | struct ring_buffer *buffer; |
| 2192 | struct ring_buffer_per_cpu *cpu_buffer; |
| 2193 | struct ring_buffer_event *event; |
| 2194 | unsigned length; |
| 2195 | |
| 2196 | cpu_buffer = iter->cpu_buffer; |
| 2197 | buffer = cpu_buffer->buffer; |
| 2198 | |
| 2199 | /* |
| 2200 | * Check if we are at the end of the buffer. |
| 2201 | */ |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2202 | if (iter->head >= rb_page_size(iter->head_page)) { |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 2203 | if (RB_WARN_ON(buffer, |
| 2204 | iter->head_page == cpu_buffer->commit_page)) |
| 2205 | return; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2206 | rb_inc_iter(iter); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2207 | return; |
| 2208 | } |
| 2209 | |
| 2210 | event = rb_iter_head_event(iter); |
| 2211 | |
| 2212 | length = rb_event_length(event); |
| 2213 | |
| 2214 | /* |
| 2215 | * This should not be called to advance the header if we are |
| 2216 | * at the tail of the buffer. |
| 2217 | */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 2218 | if (RB_WARN_ON(cpu_buffer, |
Steven Rostedt | f536aaf | 2008-11-10 23:07:30 -0500 | [diff] [blame] | 2219 | (iter->head_page == cpu_buffer->commit_page) && |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 2220 | (iter->head + length > rb_commit_index(cpu_buffer)))) |
| 2221 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2222 | |
| 2223 | rb_update_iter_read_stamp(iter, event); |
| 2224 | |
| 2225 | iter->head += length; |
| 2226 | |
| 2227 | /* check for end of page padding */ |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2228 | if ((iter->head >= rb_page_size(iter->head_page)) && |
| 2229 | (iter->head_page != cpu_buffer->commit_page)) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2230 | rb_advance_iter(iter); |
| 2231 | } |
| 2232 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2233 | static struct ring_buffer_event * |
| 2234 | rb_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2235 | { |
| 2236 | struct ring_buffer_per_cpu *cpu_buffer; |
| 2237 | struct ring_buffer_event *event; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2238 | struct buffer_page *reader; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 2239 | int nr_loops = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2240 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2241 | cpu_buffer = buffer->buffers[cpu]; |
| 2242 | |
| 2243 | again: |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 2244 | /* |
| 2245 | * We repeat when a timestamp is encountered. It is possible |
| 2246 | * to get multiple timestamps from an interrupt entering just |
| 2247 | * as one timestamp is about to be written. The max times |
| 2248 | * that this can happen is the number of nested interrupts we |
| 2249 | * can have. Nesting 10 deep of interrupts is clearly |
| 2250 | * an anomaly. |
| 2251 | */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 2252 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10)) |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 2253 | return NULL; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 2254 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2255 | reader = rb_get_reader_page(cpu_buffer); |
| 2256 | if (!reader) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2257 | return NULL; |
| 2258 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2259 | event = rb_reader_event(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2260 | |
| 2261 | switch (event->type) { |
| 2262 | case RINGBUF_TYPE_PADDING: |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 2263 | if (rb_null_event(event)) |
| 2264 | RB_WARN_ON(cpu_buffer, 1); |
| 2265 | /* |
| 2266 | * Because the writer could be discarding every |
| 2267 | * event it creates (which would probably be bad) |
| 2268 | * if we were to go back to "again" then we may never |
| 2269 | * catch up, and will trigger the warn on, or lock |
| 2270 | * the box. Return the padding, and we will release |
| 2271 | * the current locks, and try again. |
| 2272 | */ |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2273 | rb_advance_reader(cpu_buffer); |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 2274 | return event; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2275 | |
| 2276 | case RINGBUF_TYPE_TIME_EXTEND: |
| 2277 | /* Internal data, OK to advance */ |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2278 | rb_advance_reader(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2279 | goto again; |
| 2280 | |
| 2281 | case RINGBUF_TYPE_TIME_STAMP: |
| 2282 | /* FIXME: not implemented */ |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2283 | rb_advance_reader(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2284 | goto again; |
| 2285 | |
| 2286 | case RINGBUF_TYPE_DATA: |
| 2287 | if (ts) { |
| 2288 | *ts = cpu_buffer->read_stamp + event->time_delta; |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 2289 | ring_buffer_normalize_time_stamp(buffer, |
| 2290 | cpu_buffer->cpu, ts); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2291 | } |
| 2292 | return event; |
| 2293 | |
| 2294 | default: |
| 2295 | BUG(); |
| 2296 | } |
| 2297 | |
| 2298 | return NULL; |
| 2299 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2300 | EXPORT_SYMBOL_GPL(ring_buffer_peek); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2301 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2302 | static struct ring_buffer_event * |
| 2303 | rb_iter_peek(struct ring_buffer_iter *iter, u64 *ts) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2304 | { |
| 2305 | struct ring_buffer *buffer; |
| 2306 | struct ring_buffer_per_cpu *cpu_buffer; |
| 2307 | struct ring_buffer_event *event; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 2308 | int nr_loops = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2309 | |
| 2310 | if (ring_buffer_iter_empty(iter)) |
| 2311 | return NULL; |
| 2312 | |
| 2313 | cpu_buffer = iter->cpu_buffer; |
| 2314 | buffer = cpu_buffer->buffer; |
| 2315 | |
| 2316 | again: |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 2317 | /* |
| 2318 | * We repeat when a timestamp is encountered. It is possible |
| 2319 | * to get multiple timestamps from an interrupt entering just |
| 2320 | * as one timestamp is about to be written. The max times |
| 2321 | * that this can happen is the number of nested interrupts we |
| 2322 | * can have. Nesting 10 deep of interrupts is clearly |
| 2323 | * an anomaly. |
| 2324 | */ |
Steven Rostedt | 3e89c7bb | 2008-11-11 15:28:41 -0500 | [diff] [blame] | 2325 | if (RB_WARN_ON(cpu_buffer, ++nr_loops > 10)) |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 2326 | return NULL; |
Steven Rostedt | 818e3dd | 2008-10-31 09:58:35 -0400 | [diff] [blame] | 2327 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2328 | if (rb_per_cpu_empty(cpu_buffer)) |
| 2329 | return NULL; |
| 2330 | |
| 2331 | event = rb_iter_head_event(iter); |
| 2332 | |
| 2333 | switch (event->type) { |
| 2334 | case RINGBUF_TYPE_PADDING: |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 2335 | if (rb_null_event(event)) { |
| 2336 | rb_inc_iter(iter); |
| 2337 | goto again; |
| 2338 | } |
| 2339 | rb_advance_iter(iter); |
| 2340 | return event; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2341 | |
| 2342 | case RINGBUF_TYPE_TIME_EXTEND: |
| 2343 | /* Internal data, OK to advance */ |
| 2344 | rb_advance_iter(iter); |
| 2345 | goto again; |
| 2346 | |
| 2347 | case RINGBUF_TYPE_TIME_STAMP: |
| 2348 | /* FIXME: not implemented */ |
| 2349 | rb_advance_iter(iter); |
| 2350 | goto again; |
| 2351 | |
| 2352 | case RINGBUF_TYPE_DATA: |
| 2353 | if (ts) { |
| 2354 | *ts = iter->read_stamp + event->time_delta; |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 2355 | ring_buffer_normalize_time_stamp(buffer, |
| 2356 | cpu_buffer->cpu, ts); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2357 | } |
| 2358 | return event; |
| 2359 | |
| 2360 | default: |
| 2361 | BUG(); |
| 2362 | } |
| 2363 | |
| 2364 | return NULL; |
| 2365 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2366 | EXPORT_SYMBOL_GPL(ring_buffer_iter_peek); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2367 | |
| 2368 | /** |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2369 | * ring_buffer_peek - peek at the next event to be read |
| 2370 | * @buffer: The ring buffer to read |
| 2371 | * @cpu: The cpu to peak at |
| 2372 | * @ts: The timestamp counter of this event. |
| 2373 | * |
| 2374 | * This will return the event that will be read next, but does |
| 2375 | * not consume the data. |
| 2376 | */ |
| 2377 | struct ring_buffer_event * |
| 2378 | ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts) |
| 2379 | { |
| 2380 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 2381 | struct ring_buffer_event *event; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2382 | unsigned long flags; |
| 2383 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2384 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 2385 | return NULL; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2386 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 2387 | again: |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2388 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
| 2389 | event = rb_buffer_peek(buffer, cpu, ts); |
| 2390 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
| 2391 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 2392 | if (event && event->type == RINGBUF_TYPE_PADDING) { |
| 2393 | cpu_relax(); |
| 2394 | goto again; |
| 2395 | } |
| 2396 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2397 | return event; |
| 2398 | } |
| 2399 | |
| 2400 | /** |
| 2401 | * ring_buffer_iter_peek - peek at the next event to be read |
| 2402 | * @iter: The ring buffer iterator |
| 2403 | * @ts: The timestamp counter of this event. |
| 2404 | * |
| 2405 | * This will return the event that will be read next, but does |
| 2406 | * not increment the iterator. |
| 2407 | */ |
| 2408 | struct ring_buffer_event * |
| 2409 | ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts) |
| 2410 | { |
| 2411 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 2412 | struct ring_buffer_event *event; |
| 2413 | unsigned long flags; |
| 2414 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 2415 | again: |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2416 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
| 2417 | event = rb_iter_peek(iter, ts); |
| 2418 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
| 2419 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 2420 | if (event && event->type == RINGBUF_TYPE_PADDING) { |
| 2421 | cpu_relax(); |
| 2422 | goto again; |
| 2423 | } |
| 2424 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2425 | return event; |
| 2426 | } |
| 2427 | |
| 2428 | /** |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2429 | * ring_buffer_consume - return an event and consume it |
| 2430 | * @buffer: The ring buffer to get the next event from |
| 2431 | * |
| 2432 | * Returns the next event in the ring buffer, and that event is consumed. |
| 2433 | * Meaning, that sequential reads will keep returning a different event, |
| 2434 | * and eventually empty the ring buffer if the producer is slower. |
| 2435 | */ |
| 2436 | struct ring_buffer_event * |
| 2437 | ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts) |
| 2438 | { |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2439 | struct ring_buffer_per_cpu *cpu_buffer; |
| 2440 | struct ring_buffer_event *event = NULL; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2441 | unsigned long flags; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2442 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 2443 | again: |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2444 | /* might be called in atomic */ |
| 2445 | preempt_disable(); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2446 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2447 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 2448 | goto out; |
| 2449 | |
| 2450 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2451 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2452 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2453 | event = rb_buffer_peek(buffer, cpu, ts); |
| 2454 | if (!event) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2455 | goto out_unlock; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2456 | |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2457 | rb_advance_reader(cpu_buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2458 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2459 | out_unlock: |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2460 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
| 2461 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2462 | out: |
| 2463 | preempt_enable(); |
| 2464 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 2465 | if (event && event->type == RINGBUF_TYPE_PADDING) { |
| 2466 | cpu_relax(); |
| 2467 | goto again; |
| 2468 | } |
| 2469 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2470 | return event; |
| 2471 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2472 | EXPORT_SYMBOL_GPL(ring_buffer_consume); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2473 | |
| 2474 | /** |
| 2475 | * ring_buffer_read_start - start a non consuming read of the buffer |
| 2476 | * @buffer: The ring buffer to read from |
| 2477 | * @cpu: The cpu buffer to iterate over |
| 2478 | * |
| 2479 | * This starts up an iteration through the buffer. It also disables |
| 2480 | * the recording to the buffer until the reading is finished. |
| 2481 | * This prevents the reading from being corrupted. This is not |
| 2482 | * a consuming read, so a producer is not expected. |
| 2483 | * |
| 2484 | * Must be paired with ring_buffer_finish. |
| 2485 | */ |
| 2486 | struct ring_buffer_iter * |
| 2487 | ring_buffer_read_start(struct ring_buffer *buffer, int cpu) |
| 2488 | { |
| 2489 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 2490 | struct ring_buffer_iter *iter; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2491 | unsigned long flags; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2492 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 2493 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 2494 | return NULL; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2495 | |
| 2496 | iter = kmalloc(sizeof(*iter), GFP_KERNEL); |
| 2497 | if (!iter) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 2498 | return NULL; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2499 | |
| 2500 | cpu_buffer = buffer->buffers[cpu]; |
| 2501 | |
| 2502 | iter->cpu_buffer = cpu_buffer; |
| 2503 | |
| 2504 | atomic_inc(&cpu_buffer->record_disabled); |
| 2505 | synchronize_sched(); |
| 2506 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2507 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 2508 | __raw_spin_lock(&cpu_buffer->lock); |
Steven Rostedt | 642edba | 2008-11-12 00:01:26 -0500 | [diff] [blame] | 2509 | rb_iter_reset(iter); |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 2510 | __raw_spin_unlock(&cpu_buffer->lock); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2511 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2512 | |
| 2513 | return iter; |
| 2514 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2515 | EXPORT_SYMBOL_GPL(ring_buffer_read_start); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2516 | |
| 2517 | /** |
| 2518 | * ring_buffer_finish - finish reading the iterator of the buffer |
| 2519 | * @iter: The iterator retrieved by ring_buffer_start |
| 2520 | * |
| 2521 | * This re-enables the recording to the buffer, and frees the |
| 2522 | * iterator. |
| 2523 | */ |
| 2524 | void |
| 2525 | ring_buffer_read_finish(struct ring_buffer_iter *iter) |
| 2526 | { |
| 2527 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 2528 | |
| 2529 | atomic_dec(&cpu_buffer->record_disabled); |
| 2530 | kfree(iter); |
| 2531 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2532 | EXPORT_SYMBOL_GPL(ring_buffer_read_finish); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2533 | |
| 2534 | /** |
| 2535 | * ring_buffer_read - read the next item in the ring buffer by the iterator |
| 2536 | * @iter: The ring buffer iterator |
| 2537 | * @ts: The time stamp of the event read. |
| 2538 | * |
| 2539 | * This reads the next event in the ring buffer and increments the iterator. |
| 2540 | */ |
| 2541 | struct ring_buffer_event * |
| 2542 | ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts) |
| 2543 | { |
| 2544 | struct ring_buffer_event *event; |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2545 | struct ring_buffer_per_cpu *cpu_buffer = iter->cpu_buffer; |
| 2546 | unsigned long flags; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2547 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 2548 | again: |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2549 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
| 2550 | event = rb_iter_peek(iter, ts); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2551 | if (!event) |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2552 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2553 | |
| 2554 | rb_advance_iter(iter); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2555 | out: |
| 2556 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2557 | |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 2558 | if (event && event->type == RINGBUF_TYPE_PADDING) { |
| 2559 | cpu_relax(); |
| 2560 | goto again; |
| 2561 | } |
| 2562 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2563 | return event; |
| 2564 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2565 | EXPORT_SYMBOL_GPL(ring_buffer_read); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2566 | |
| 2567 | /** |
| 2568 | * ring_buffer_size - return the size of the ring buffer (in bytes) |
| 2569 | * @buffer: The ring buffer. |
| 2570 | */ |
| 2571 | unsigned long ring_buffer_size(struct ring_buffer *buffer) |
| 2572 | { |
| 2573 | return BUF_PAGE_SIZE * buffer->pages; |
| 2574 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2575 | EXPORT_SYMBOL_GPL(ring_buffer_size); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2576 | |
| 2577 | static void |
| 2578 | rb_reset_cpu(struct ring_buffer_per_cpu *cpu_buffer) |
| 2579 | { |
| 2580 | cpu_buffer->head_page |
| 2581 | = list_entry(cpu_buffer->pages.next, struct buffer_page, list); |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2582 | local_set(&cpu_buffer->head_page->write, 0); |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 2583 | local_set(&cpu_buffer->head_page->page->commit, 0); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2584 | |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 2585 | cpu_buffer->head_page->read = 0; |
Steven Rostedt | bf41a15 | 2008-10-04 02:00:59 -0400 | [diff] [blame] | 2586 | |
| 2587 | cpu_buffer->tail_page = cpu_buffer->head_page; |
| 2588 | cpu_buffer->commit_page = cpu_buffer->head_page; |
| 2589 | |
| 2590 | INIT_LIST_HEAD(&cpu_buffer->reader_page->list); |
| 2591 | local_set(&cpu_buffer->reader_page->write, 0); |
Steven Rostedt | abc9b56 | 2008-12-02 15:34:06 -0500 | [diff] [blame] | 2592 | local_set(&cpu_buffer->reader_page->page->commit, 0); |
Steven Rostedt | 6f807ac | 2008-10-04 02:00:58 -0400 | [diff] [blame] | 2593 | cpu_buffer->reader_page->read = 0; |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2594 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2595 | cpu_buffer->overrun = 0; |
| 2596 | cpu_buffer->entries = 0; |
Steven Rostedt | 69507c0 | 2009-01-21 18:45:57 -0500 | [diff] [blame] | 2597 | |
| 2598 | cpu_buffer->write_stamp = 0; |
| 2599 | cpu_buffer->read_stamp = 0; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2600 | } |
| 2601 | |
| 2602 | /** |
| 2603 | * ring_buffer_reset_cpu - reset a ring buffer per CPU buffer |
| 2604 | * @buffer: The ring buffer to reset a per cpu buffer of |
| 2605 | * @cpu: The CPU buffer to be reset |
| 2606 | */ |
| 2607 | void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu) |
| 2608 | { |
| 2609 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; |
| 2610 | unsigned long flags; |
| 2611 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 2612 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 2613 | return; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2614 | |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2615 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
| 2616 | |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 2617 | __raw_spin_lock(&cpu_buffer->lock); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2618 | |
| 2619 | rb_reset_cpu(cpu_buffer); |
| 2620 | |
Steven Rostedt | 3e03fb7 | 2008-11-06 00:09:43 -0500 | [diff] [blame] | 2621 | __raw_spin_unlock(&cpu_buffer->lock); |
Steven Rostedt | f83c9d0 | 2008-11-11 18:47:44 +0100 | [diff] [blame] | 2622 | |
| 2623 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2624 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2625 | EXPORT_SYMBOL_GPL(ring_buffer_reset_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2626 | |
| 2627 | /** |
| 2628 | * ring_buffer_reset - reset a ring buffer |
| 2629 | * @buffer: The ring buffer to reset all cpu buffers |
| 2630 | */ |
| 2631 | void ring_buffer_reset(struct ring_buffer *buffer) |
| 2632 | { |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2633 | int cpu; |
| 2634 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2635 | for_each_buffer_cpu(buffer, cpu) |
Steven Rostedt | d769041 | 2008-10-01 00:29:53 -0400 | [diff] [blame] | 2636 | ring_buffer_reset_cpu(buffer, cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2637 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2638 | EXPORT_SYMBOL_GPL(ring_buffer_reset); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2639 | |
| 2640 | /** |
| 2641 | * rind_buffer_empty - is the ring buffer empty? |
| 2642 | * @buffer: The ring buffer to test |
| 2643 | */ |
| 2644 | int ring_buffer_empty(struct ring_buffer *buffer) |
| 2645 | { |
| 2646 | struct ring_buffer_per_cpu *cpu_buffer; |
| 2647 | int cpu; |
| 2648 | |
| 2649 | /* yes this is racy, but if you don't like the race, lock the buffer */ |
| 2650 | for_each_buffer_cpu(buffer, cpu) { |
| 2651 | cpu_buffer = buffer->buffers[cpu]; |
| 2652 | if (!rb_per_cpu_empty(cpu_buffer)) |
| 2653 | return 0; |
| 2654 | } |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2655 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2656 | return 1; |
| 2657 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2658 | EXPORT_SYMBOL_GPL(ring_buffer_empty); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2659 | |
| 2660 | /** |
| 2661 | * ring_buffer_empty_cpu - is a cpu buffer of a ring buffer empty? |
| 2662 | * @buffer: The ring buffer |
| 2663 | * @cpu: The CPU buffer to test |
| 2664 | */ |
| 2665 | int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu) |
| 2666 | { |
| 2667 | struct ring_buffer_per_cpu *cpu_buffer; |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 2668 | int ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2669 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 2670 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
Steven Rostedt | 8aabee5 | 2009-03-12 13:13:49 -0400 | [diff] [blame] | 2671 | return 1; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2672 | |
| 2673 | cpu_buffer = buffer->buffers[cpu]; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2674 | ret = rb_per_cpu_empty(cpu_buffer); |
| 2675 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2676 | |
| 2677 | return ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2678 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2679 | EXPORT_SYMBOL_GPL(ring_buffer_empty_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2680 | |
| 2681 | /** |
| 2682 | * ring_buffer_swap_cpu - swap a CPU buffer between two ring buffers |
| 2683 | * @buffer_a: One buffer to swap with |
| 2684 | * @buffer_b: The other buffer to swap with |
| 2685 | * |
| 2686 | * This function is useful for tracers that want to take a "snapshot" |
| 2687 | * of a CPU buffer and has another back up buffer lying around. |
| 2688 | * it is expected that the tracer handles the cpu buffer not being |
| 2689 | * used at the moment. |
| 2690 | */ |
| 2691 | int ring_buffer_swap_cpu(struct ring_buffer *buffer_a, |
| 2692 | struct ring_buffer *buffer_b, int cpu) |
| 2693 | { |
| 2694 | struct ring_buffer_per_cpu *cpu_buffer_a; |
| 2695 | struct ring_buffer_per_cpu *cpu_buffer_b; |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2696 | int ret = -EINVAL; |
| 2697 | |
Rusty Russell | 9e01c1b | 2009-01-01 10:12:22 +1030 | [diff] [blame] | 2698 | if (!cpumask_test_cpu(cpu, buffer_a->cpumask) || |
| 2699 | !cpumask_test_cpu(cpu, buffer_b->cpumask)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2700 | goto out; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2701 | |
| 2702 | /* At least make sure the two buffers are somewhat the same */ |
Lai Jiangshan | 6d102bc | 2008-12-17 17:48:23 +0800 | [diff] [blame] | 2703 | if (buffer_a->pages != buffer_b->pages) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2704 | goto out; |
| 2705 | |
| 2706 | ret = -EAGAIN; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2707 | |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 2708 | if (ring_buffer_flags != RB_BUFFERS_ON) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2709 | goto out; |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 2710 | |
| 2711 | if (atomic_read(&buffer_a->record_disabled)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2712 | goto out; |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 2713 | |
| 2714 | if (atomic_read(&buffer_b->record_disabled)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2715 | goto out; |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 2716 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2717 | cpu_buffer_a = buffer_a->buffers[cpu]; |
| 2718 | cpu_buffer_b = buffer_b->buffers[cpu]; |
| 2719 | |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 2720 | if (atomic_read(&cpu_buffer_a->record_disabled)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2721 | goto out; |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 2722 | |
| 2723 | if (atomic_read(&cpu_buffer_b->record_disabled)) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2724 | goto out; |
Steven Rostedt | 97b17ef | 2009-01-21 15:24:56 -0500 | [diff] [blame] | 2725 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2726 | /* |
| 2727 | * We can't do a synchronize_sched here because this |
| 2728 | * function can be called in atomic context. |
| 2729 | * Normally this will be called from the same CPU as cpu. |
| 2730 | * If not it's up to the caller to protect this. |
| 2731 | */ |
| 2732 | atomic_inc(&cpu_buffer_a->record_disabled); |
| 2733 | atomic_inc(&cpu_buffer_b->record_disabled); |
| 2734 | |
| 2735 | buffer_a->buffers[cpu] = cpu_buffer_b; |
| 2736 | buffer_b->buffers[cpu] = cpu_buffer_a; |
| 2737 | |
| 2738 | cpu_buffer_b->buffer = buffer_a; |
| 2739 | cpu_buffer_a->buffer = buffer_b; |
| 2740 | |
| 2741 | atomic_dec(&cpu_buffer_a->record_disabled); |
| 2742 | atomic_dec(&cpu_buffer_b->record_disabled); |
| 2743 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2744 | ret = 0; |
| 2745 | out: |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2746 | return ret; |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2747 | } |
Robert Richter | c4f5018 | 2008-12-11 16:49:22 +0100 | [diff] [blame] | 2748 | EXPORT_SYMBOL_GPL(ring_buffer_swap_cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 2749 | |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2750 | static void rb_remove_entries(struct ring_buffer_per_cpu *cpu_buffer, |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 2751 | struct buffer_data_page *bpage, |
| 2752 | unsigned int offset) |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2753 | { |
| 2754 | struct ring_buffer_event *event; |
| 2755 | unsigned long head; |
| 2756 | |
| 2757 | __raw_spin_lock(&cpu_buffer->lock); |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 2758 | for (head = offset; head < local_read(&bpage->commit); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2759 | head += rb_event_length(event)) { |
| 2760 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 2761 | event = __rb_data_page_index(bpage, head); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2762 | if (RB_WARN_ON(cpu_buffer, rb_null_event(event))) |
| 2763 | return; |
| 2764 | /* Only count data entries */ |
| 2765 | if (event->type != RINGBUF_TYPE_DATA) |
| 2766 | continue; |
| 2767 | cpu_buffer->entries--; |
| 2768 | } |
| 2769 | __raw_spin_unlock(&cpu_buffer->lock); |
| 2770 | } |
| 2771 | |
| 2772 | /** |
| 2773 | * ring_buffer_alloc_read_page - allocate a page to read from buffer |
| 2774 | * @buffer: the buffer to allocate for. |
| 2775 | * |
| 2776 | * This function is used in conjunction with ring_buffer_read_page. |
| 2777 | * When reading a full page from the ring buffer, these functions |
| 2778 | * can be used to speed up the process. The calling function should |
| 2779 | * allocate a few pages first with this function. Then when it |
| 2780 | * needs to get pages from the ring buffer, it passes the result |
| 2781 | * of this function into ring_buffer_read_page, which will swap |
| 2782 | * the page that was allocated, with the read page of the buffer. |
| 2783 | * |
| 2784 | * Returns: |
| 2785 | * The page allocated, or NULL on error. |
| 2786 | */ |
| 2787 | void *ring_buffer_alloc_read_page(struct ring_buffer *buffer) |
| 2788 | { |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 2789 | struct buffer_data_page *bpage; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2790 | unsigned long addr; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2791 | |
| 2792 | addr = __get_free_page(GFP_KERNEL); |
| 2793 | if (!addr) |
| 2794 | return NULL; |
| 2795 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 2796 | bpage = (void *)addr; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2797 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2798 | rb_init_page(bpage); |
| 2799 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 2800 | return bpage; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | /** |
| 2804 | * ring_buffer_free_read_page - free an allocated read page |
| 2805 | * @buffer: the buffer the page was allocate for |
| 2806 | * @data: the page to free |
| 2807 | * |
| 2808 | * Free a page allocated from ring_buffer_alloc_read_page. |
| 2809 | */ |
| 2810 | void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data) |
| 2811 | { |
| 2812 | free_page((unsigned long)data); |
| 2813 | } |
| 2814 | |
| 2815 | /** |
| 2816 | * ring_buffer_read_page - extract a page from the ring buffer |
| 2817 | * @buffer: buffer to extract from |
| 2818 | * @data_page: the page to use allocated from ring_buffer_alloc_read_page |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2819 | * @len: amount to extract |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2820 | * @cpu: the cpu of the buffer to extract |
| 2821 | * @full: should the extraction only happen when the page is full. |
| 2822 | * |
| 2823 | * This function will pull out a page from the ring buffer and consume it. |
| 2824 | * @data_page must be the address of the variable that was returned |
| 2825 | * from ring_buffer_alloc_read_page. This is because the page might be used |
| 2826 | * to swap with a page in the ring buffer. |
| 2827 | * |
| 2828 | * for example: |
Lai Jiangshan | b85fa01 | 2009-02-09 14:21:14 +0800 | [diff] [blame] | 2829 | * rpage = ring_buffer_alloc_read_page(buffer); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2830 | * if (!rpage) |
| 2831 | * return error; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2832 | * ret = ring_buffer_read_page(buffer, &rpage, len, cpu, 0); |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 2833 | * if (ret >= 0) |
| 2834 | * process_page(rpage, ret); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2835 | * |
| 2836 | * When @full is set, the function will not return true unless |
| 2837 | * the writer is off the reader page. |
| 2838 | * |
| 2839 | * Note: it is up to the calling functions to handle sleeps and wakeups. |
| 2840 | * The ring buffer can be used anywhere in the kernel and can not |
| 2841 | * blindly call wake_up. The layer that uses the ring buffer must be |
| 2842 | * responsible for that. |
| 2843 | * |
| 2844 | * Returns: |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 2845 | * >=0 if data has been transferred, returns the offset of consumed data. |
| 2846 | * <0 if no data has been transferred. |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2847 | */ |
| 2848 | int ring_buffer_read_page(struct ring_buffer *buffer, |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2849 | void **data_page, size_t len, int cpu, int full) |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2850 | { |
| 2851 | struct ring_buffer_per_cpu *cpu_buffer = buffer->buffers[cpu]; |
| 2852 | struct ring_buffer_event *event; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 2853 | struct buffer_data_page *bpage; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2854 | struct buffer_page *reader; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2855 | unsigned long flags; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2856 | unsigned int commit; |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 2857 | unsigned int read; |
Steven Rostedt | 4f3640f | 2009-03-03 23:52:42 -0500 | [diff] [blame] | 2858 | u64 save_timestamp; |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 2859 | int ret = -1; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2860 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2861 | if (!cpumask_test_cpu(cpu, buffer->cpumask)) |
| 2862 | goto out; |
| 2863 | |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 2864 | /* |
| 2865 | * If len is not big enough to hold the page header, then |
| 2866 | * we can not copy anything. |
| 2867 | */ |
| 2868 | if (len <= BUF_PAGE_HDR_SIZE) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2869 | goto out; |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 2870 | |
| 2871 | len -= BUF_PAGE_HDR_SIZE; |
| 2872 | |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2873 | if (!data_page) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2874 | goto out; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2875 | |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 2876 | bpage = *data_page; |
| 2877 | if (!bpage) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2878 | goto out; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2879 | |
| 2880 | spin_lock_irqsave(&cpu_buffer->reader_lock, flags); |
| 2881 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2882 | reader = rb_get_reader_page(cpu_buffer); |
| 2883 | if (!reader) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2884 | goto out_unlock; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2885 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2886 | event = rb_reader_event(cpu_buffer); |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 2887 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2888 | read = reader->read; |
| 2889 | commit = rb_page_commit(reader); |
| 2890 | |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2891 | /* |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 2892 | * If this page has been partially read or |
| 2893 | * if len is not big enough to read the rest of the page or |
| 2894 | * a writer is still on the page, then |
| 2895 | * we must copy the data from the page to the buffer. |
| 2896 | * Otherwise, we can simply swap the page with the one passed in. |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2897 | */ |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 2898 | if (read || (len < (commit - read)) || |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2899 | cpu_buffer->reader_page == cpu_buffer->commit_page) { |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 2900 | struct buffer_data_page *rpage = cpu_buffer->reader_page->page; |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 2901 | unsigned int rpos = read; |
| 2902 | unsigned int pos = 0; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2903 | unsigned int size; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2904 | |
| 2905 | if (full) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2906 | goto out_unlock; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2907 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2908 | if (len > (commit - read)) |
| 2909 | len = (commit - read); |
| 2910 | |
| 2911 | size = rb_event_length(event); |
| 2912 | |
| 2913 | if (len < size) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2914 | goto out_unlock; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2915 | |
Steven Rostedt | 4f3640f | 2009-03-03 23:52:42 -0500 | [diff] [blame] | 2916 | /* save the current timestamp, since the user will need it */ |
| 2917 | save_timestamp = cpu_buffer->read_stamp; |
| 2918 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2919 | /* Need to copy one event at a time */ |
| 2920 | do { |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 2921 | memcpy(bpage->data + pos, rpage->data + rpos, size); |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2922 | |
| 2923 | len -= size; |
| 2924 | |
| 2925 | rb_advance_reader(cpu_buffer); |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 2926 | rpos = reader->read; |
| 2927 | pos += size; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2928 | |
| 2929 | event = rb_reader_event(cpu_buffer); |
| 2930 | size = rb_event_length(event); |
| 2931 | } while (len > size); |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 2932 | |
| 2933 | /* update bpage */ |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2934 | local_set(&bpage->commit, pos); |
Steven Rostedt | 4f3640f | 2009-03-03 23:52:42 -0500 | [diff] [blame] | 2935 | bpage->time_stamp = save_timestamp; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2936 | |
Steven Rostedt | 474d32b | 2009-03-03 19:51:40 -0500 | [diff] [blame] | 2937 | /* we copied everything to the beginning */ |
| 2938 | read = 0; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2939 | } else { |
| 2940 | /* swap the pages */ |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 2941 | rb_init_page(bpage); |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2942 | bpage = reader->page; |
| 2943 | reader->page = *data_page; |
| 2944 | local_set(&reader->write, 0); |
| 2945 | reader->read = 0; |
Steven Rostedt | 044fa78 | 2008-12-02 23:50:03 -0500 | [diff] [blame] | 2946 | *data_page = bpage; |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 2947 | |
| 2948 | /* update the entry counter */ |
| 2949 | rb_remove_entries(cpu_buffer, bpage, read); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2950 | } |
Lai Jiangshan | 667d241 | 2009-02-09 14:21:17 +0800 | [diff] [blame] | 2951 | ret = read; |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2952 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2953 | out_unlock: |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2954 | spin_unlock_irqrestore(&cpu_buffer->reader_lock, flags); |
| 2955 | |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 2956 | out: |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 2957 | return ret; |
| 2958 | } |
| 2959 | |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 2960 | static ssize_t |
| 2961 | rb_simple_read(struct file *filp, char __user *ubuf, |
| 2962 | size_t cnt, loff_t *ppos) |
| 2963 | { |
Hannes Eder | 5e39841 | 2009-02-10 19:44:34 +0100 | [diff] [blame] | 2964 | unsigned long *p = filp->private_data; |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 2965 | char buf[64]; |
| 2966 | int r; |
| 2967 | |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 2968 | if (test_bit(RB_BUFFERS_DISABLED_BIT, p)) |
| 2969 | r = sprintf(buf, "permanently disabled\n"); |
| 2970 | else |
| 2971 | r = sprintf(buf, "%d\n", test_bit(RB_BUFFERS_ON_BIT, p)); |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 2972 | |
| 2973 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); |
| 2974 | } |
| 2975 | |
| 2976 | static ssize_t |
| 2977 | rb_simple_write(struct file *filp, const char __user *ubuf, |
| 2978 | size_t cnt, loff_t *ppos) |
| 2979 | { |
Hannes Eder | 5e39841 | 2009-02-10 19:44:34 +0100 | [diff] [blame] | 2980 | unsigned long *p = filp->private_data; |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 2981 | char buf[64]; |
Hannes Eder | 5e39841 | 2009-02-10 19:44:34 +0100 | [diff] [blame] | 2982 | unsigned long val; |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 2983 | int ret; |
| 2984 | |
| 2985 | if (cnt >= sizeof(buf)) |
| 2986 | return -EINVAL; |
| 2987 | |
| 2988 | if (copy_from_user(&buf, ubuf, cnt)) |
| 2989 | return -EFAULT; |
| 2990 | |
| 2991 | buf[cnt] = 0; |
| 2992 | |
| 2993 | ret = strict_strtoul(buf, 10, &val); |
| 2994 | if (ret < 0) |
| 2995 | return ret; |
| 2996 | |
Steven Rostedt | 033601a | 2008-11-21 12:41:55 -0500 | [diff] [blame] | 2997 | if (val) |
| 2998 | set_bit(RB_BUFFERS_ON_BIT, p); |
| 2999 | else |
| 3000 | clear_bit(RB_BUFFERS_ON_BIT, p); |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 3001 | |
| 3002 | (*ppos)++; |
| 3003 | |
| 3004 | return cnt; |
| 3005 | } |
| 3006 | |
Steven Rostedt | 5e2336a | 2009-03-05 21:44:55 -0500 | [diff] [blame] | 3007 | static const struct file_operations rb_simple_fops = { |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 3008 | .open = tracing_open_generic, |
| 3009 | .read = rb_simple_read, |
| 3010 | .write = rb_simple_write, |
| 3011 | }; |
| 3012 | |
| 3013 | |
| 3014 | static __init int rb_init_debugfs(void) |
| 3015 | { |
| 3016 | struct dentry *d_tracer; |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 3017 | |
| 3018 | d_tracer = tracing_init_dentry(); |
| 3019 | |
Frederic Weisbecker | 5452af6 | 2009-03-27 00:25:38 +0100 | [diff] [blame] | 3020 | trace_create_file("tracing_on", 0644, d_tracer, |
| 3021 | &ring_buffer_flags, &rb_simple_fops); |
Steven Rostedt | a358324 | 2008-11-11 15:01:42 -0500 | [diff] [blame] | 3022 | |
| 3023 | return 0; |
| 3024 | } |
| 3025 | |
| 3026 | fs_initcall(rb_init_debugfs); |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3027 | |
Steven Rostedt | 59222ef | 2009-03-12 11:46:03 -0400 | [diff] [blame] | 3028 | #ifdef CONFIG_HOTPLUG_CPU |
Frederic Weisbecker | 09c9e84 | 2009-03-21 04:33:36 +0100 | [diff] [blame] | 3029 | static int rb_cpu_notify(struct notifier_block *self, |
| 3030 | unsigned long action, void *hcpu) |
Steven Rostedt | 554f786 | 2009-03-11 22:00:13 -0400 | [diff] [blame] | 3031 | { |
| 3032 | struct ring_buffer *buffer = |
| 3033 | container_of(self, struct ring_buffer, cpu_notify); |
| 3034 | long cpu = (long)hcpu; |
| 3035 | |
| 3036 | switch (action) { |
| 3037 | case CPU_UP_PREPARE: |
| 3038 | case CPU_UP_PREPARE_FROZEN: |
| 3039 | if (cpu_isset(cpu, *buffer->cpumask)) |
| 3040 | return NOTIFY_OK; |
| 3041 | |
| 3042 | buffer->buffers[cpu] = |
| 3043 | rb_allocate_cpu_buffer(buffer, cpu); |
| 3044 | if (!buffer->buffers[cpu]) { |
| 3045 | WARN(1, "failed to allocate ring buffer on CPU %ld\n", |
| 3046 | cpu); |
| 3047 | return NOTIFY_OK; |
| 3048 | } |
| 3049 | smp_wmb(); |
| 3050 | cpu_set(cpu, *buffer->cpumask); |
| 3051 | break; |
| 3052 | case CPU_DOWN_PREPARE: |
| 3053 | case CPU_DOWN_PREPARE_FROZEN: |
| 3054 | /* |
| 3055 | * Do nothing. |
| 3056 | * If we were to free the buffer, then the user would |
| 3057 | * lose any trace that was in the buffer. |
| 3058 | */ |
| 3059 | break; |
| 3060 | default: |
| 3061 | break; |
| 3062 | } |
| 3063 | return NOTIFY_OK; |
| 3064 | } |
| 3065 | #endif |