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