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