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