Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 1 | #ifndef _LINUX_RING_BUFFER_H |
| 2 | #define _LINUX_RING_BUFFER_H |
| 3 | |
Vegard Nossum | 1744a21 | 2009-02-28 08:29:44 +0100 | [diff] [blame] | 4 | #include <linux/kmemcheck.h> |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 5 | #include <linux/mm.h> |
| 6 | #include <linux/seq_file.h> |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 7 | #include <linux/poll.h> |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 8 | |
| 9 | struct ring_buffer; |
| 10 | struct ring_buffer_iter; |
| 11 | |
| 12 | /* |
Wenji Huang | c3706f0 | 2009-02-10 01:03:18 -0500 | [diff] [blame] | 13 | * Don't refer to this struct directly, use functions below. |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 14 | */ |
| 15 | struct ring_buffer_event { |
Vegard Nossum | 1744a21 | 2009-02-28 08:29:44 +0100 | [diff] [blame] | 16 | kmemcheck_bitfield_begin(bitfield); |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 17 | u32 type_len:5, time_delta:27; |
Vegard Nossum | 1744a21 | 2009-02-28 08:29:44 +0100 | [diff] [blame] | 18 | kmemcheck_bitfield_end(bitfield); |
| 19 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 20 | u32 array[]; |
| 21 | }; |
| 22 | |
| 23 | /** |
| 24 | * enum ring_buffer_type - internal ring buffer types |
| 25 | * |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 26 | * @RINGBUF_TYPE_PADDING: Left over page padding or discarded event |
| 27 | * If time_delta is 0: |
| 28 | * array is ignored |
| 29 | * size is variable depending on how much |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 30 | * padding is needed |
Tom Zanussi | 2d62271 | 2009-03-22 03:30:49 -0500 | [diff] [blame] | 31 | * If time_delta is non zero: |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 32 | * array[0] holds the actual length |
| 33 | * size = 4 + length (bytes) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 34 | * |
| 35 | * @RINGBUF_TYPE_TIME_EXTEND: Extend the time delta |
| 36 | * array[0] = time delta (28 .. 59) |
| 37 | * size = 8 bytes |
| 38 | * |
| 39 | * @RINGBUF_TYPE_TIME_STAMP: Sync time stamp with external clock |
Lai Jiangshan | 361b73d | 2008-12-08 10:58:08 +0800 | [diff] [blame] | 40 | * array[0] = tv_nsec |
| 41 | * array[1..2] = tv_sec |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 42 | * size = 16 bytes |
| 43 | * |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 44 | * <= @RINGBUF_TYPE_DATA_TYPE_LEN_MAX: |
| 45 | * Data record |
| 46 | * If type_len is zero: |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 47 | * array[0] holds the actual length |
Lai Jiangshan | 361b73d | 2008-12-08 10:58:08 +0800 | [diff] [blame] | 48 | * array[1..(length+3)/4] holds data |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 49 | * size = 4 + length (bytes) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 50 | * else |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 51 | * length = type_len << 2 |
Lai Jiangshan | 361b73d | 2008-12-08 10:58:08 +0800 | [diff] [blame] | 52 | * array[0..(length+3)/4-1] holds data |
| 53 | * size = 4 + length (bytes) |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 54 | */ |
| 55 | enum ring_buffer_type { |
Lai Jiangshan | 334d416 | 2009-04-24 11:27:05 +0800 | [diff] [blame] | 56 | RINGBUF_TYPE_DATA_TYPE_LEN_MAX = 28, |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 57 | RINGBUF_TYPE_PADDING, |
| 58 | RINGBUF_TYPE_TIME_EXTEND, |
| 59 | /* FIXME: RINGBUF_TYPE_TIME_STAMP not implemented */ |
| 60 | RINGBUF_TYPE_TIME_STAMP, |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | unsigned ring_buffer_event_length(struct ring_buffer_event *event); |
| 64 | void *ring_buffer_event_data(struct ring_buffer_event *event); |
| 65 | |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 66 | /* |
Steven Rostedt | fa1b47d | 2009-04-02 00:09:41 -0400 | [diff] [blame] | 67 | * ring_buffer_discard_commit will remove an event that has not |
| 68 | * ben committed yet. If this is used, then ring_buffer_unlock_commit |
| 69 | * must not be called on the discarded event. This function |
| 70 | * will try to remove the event from the ring buffer completely |
| 71 | * if another event has not been written after it. |
| 72 | * |
| 73 | * Example use: |
| 74 | * |
| 75 | * if (some_condition) |
| 76 | * ring_buffer_discard_commit(buffer, event); |
| 77 | * else |
| 78 | * ring_buffer_unlock_commit(buffer, event); |
| 79 | */ |
| 80 | void ring_buffer_discard_commit(struct ring_buffer *buffer, |
| 81 | struct ring_buffer_event *event); |
| 82 | |
| 83 | /* |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 84 | * size is in bytes for each per CPU buffer. |
| 85 | */ |
| 86 | struct ring_buffer * |
Peter Zijlstra | 1f8a6a1 | 2009-06-08 18:18:39 +0200 | [diff] [blame] | 87 | __ring_buffer_alloc(unsigned long size, unsigned flags, struct lock_class_key *key); |
| 88 | |
| 89 | /* |
| 90 | * Because the ring buffer is generic, if other users of the ring buffer get |
| 91 | * traced by ftrace, it can produce lockdep warnings. We need to keep each |
| 92 | * ring buffer's lock class separate. |
| 93 | */ |
| 94 | #define ring_buffer_alloc(size, flags) \ |
| 95 | ({ \ |
| 96 | static struct lock_class_key __key; \ |
| 97 | __ring_buffer_alloc((size), (flags), &__key); \ |
| 98 | }) |
| 99 | |
Rabin Vincent | e30f53a | 2014-11-10 19:46:34 +0100 | [diff] [blame] | 100 | int ring_buffer_wait(struct ring_buffer *buffer, int cpu, bool full); |
Steven Rostedt (Red Hat) | 1569345 | 2013-02-28 19:59:17 -0500 | [diff] [blame] | 101 | int ring_buffer_poll_wait(struct ring_buffer *buffer, int cpu, |
| 102 | struct file *filp, poll_table *poll_table); |
| 103 | |
| 104 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 105 | #define RING_BUFFER_ALL_CPUS -1 |
| 106 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 107 | void ring_buffer_free(struct ring_buffer *buffer); |
| 108 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 109 | int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size, int cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 110 | |
David Sharp | 750912f | 2010-12-08 13:46:47 -0800 | [diff] [blame] | 111 | void ring_buffer_change_overwrite(struct ring_buffer *buffer, int val); |
| 112 | |
Arnaldo Carvalho de Melo | 0a98775 | 2009-02-05 16:12:56 -0200 | [diff] [blame] | 113 | struct ring_buffer_event *ring_buffer_lock_reserve(struct ring_buffer *buffer, |
| 114 | unsigned long length); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 115 | int ring_buffer_unlock_commit(struct ring_buffer *buffer, |
Arnaldo Carvalho de Melo | 0a98775 | 2009-02-05 16:12:56 -0200 | [diff] [blame] | 116 | struct ring_buffer_event *event); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 117 | int ring_buffer_write(struct ring_buffer *buffer, |
| 118 | unsigned long length, void *data); |
| 119 | |
| 120 | struct ring_buffer_event * |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 121 | ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts, |
| 122 | unsigned long *lost_events); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 123 | struct ring_buffer_event * |
Steven Rostedt | 66a8cb9 | 2010-03-31 13:21:56 -0400 | [diff] [blame] | 124 | ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts, |
| 125 | unsigned long *lost_events); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 126 | |
| 127 | struct ring_buffer_iter * |
David Miller | 72c9ddf | 2010-04-20 15:47:11 -0700 | [diff] [blame] | 128 | ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu); |
| 129 | void ring_buffer_read_prepare_sync(void); |
| 130 | void ring_buffer_read_start(struct ring_buffer_iter *iter); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 131 | void ring_buffer_read_finish(struct ring_buffer_iter *iter); |
| 132 | |
| 133 | struct ring_buffer_event * |
| 134 | ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts); |
| 135 | struct ring_buffer_event * |
| 136 | ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts); |
| 137 | void ring_buffer_iter_reset(struct ring_buffer_iter *iter); |
| 138 | int ring_buffer_iter_empty(struct ring_buffer_iter *iter); |
| 139 | |
Vaibhav Nagarnaik | 438ced1 | 2012-02-02 12:00:41 -0800 | [diff] [blame] | 140 | unsigned long ring_buffer_size(struct ring_buffer *buffer, int cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 141 | |
| 142 | void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu); |
| 143 | void ring_buffer_reset(struct ring_buffer *buffer); |
| 144 | |
Steven Rostedt | 85bac32 | 2009-09-04 14:24:40 -0400 | [diff] [blame] | 145 | #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 146 | int ring_buffer_swap_cpu(struct ring_buffer *buffer_a, |
| 147 | struct ring_buffer *buffer_b, int cpu); |
Steven Rostedt | 85bac32 | 2009-09-04 14:24:40 -0400 | [diff] [blame] | 148 | #else |
| 149 | static inline int |
| 150 | ring_buffer_swap_cpu(struct ring_buffer *buffer_a, |
| 151 | struct ring_buffer *buffer_b, int cpu) |
| 152 | { |
| 153 | return -ENODEV; |
| 154 | } |
| 155 | #endif |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 156 | |
| 157 | int ring_buffer_empty(struct ring_buffer *buffer); |
| 158 | int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu); |
| 159 | |
| 160 | void ring_buffer_record_disable(struct ring_buffer *buffer); |
| 161 | void ring_buffer_record_enable(struct ring_buffer *buffer); |
Steven Rostedt | 499e547 | 2012-02-22 15:50:28 -0500 | [diff] [blame] | 162 | void ring_buffer_record_off(struct ring_buffer *buffer); |
| 163 | void ring_buffer_record_on(struct ring_buffer *buffer); |
| 164 | int ring_buffer_record_is_on(struct ring_buffer *buffer); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 165 | void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu); |
| 166 | void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu); |
| 167 | |
Yoshihiro YUNOMAE | 50ecf2c | 2012-10-11 16:27:54 -0700 | [diff] [blame] | 168 | u64 ring_buffer_oldest_event_ts(struct ring_buffer *buffer, int cpu); |
Vaibhav Nagarnaik | c64e148 | 2011-08-16 14:46:16 -0700 | [diff] [blame] | 169 | unsigned long ring_buffer_bytes_cpu(struct ring_buffer *buffer, int cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 170 | unsigned long ring_buffer_entries(struct ring_buffer *buffer); |
| 171 | unsigned long ring_buffer_overruns(struct ring_buffer *buffer); |
Robert Richter | e09373f | 2008-11-26 14:04:19 +0100 | [diff] [blame] | 172 | unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu); |
| 173 | unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu); |
Steven Rostedt | f0d2c68 | 2009-04-29 13:43:37 -0400 | [diff] [blame] | 174 | unsigned long ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu); |
Slava Pestov | 884bfe8 | 2011-07-15 14:23:58 -0700 | [diff] [blame] | 175 | unsigned long ring_buffer_dropped_events_cpu(struct ring_buffer *buffer, int cpu); |
Steven Rostedt (Red Hat) | ad96470 | 2013-01-29 17:45:49 -0500 | [diff] [blame] | 176 | unsigned long ring_buffer_read_events_cpu(struct ring_buffer *buffer, int cpu); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 177 | |
Steven Rostedt | 37886f6 | 2009-03-17 17:22:06 -0400 | [diff] [blame] | 178 | u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu); |
| 179 | void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer, |
| 180 | int cpu, u64 *ts); |
| 181 | void ring_buffer_set_clock(struct ring_buffer *buffer, |
| 182 | u64 (*clock)(void)); |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 183 | |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 184 | size_t ring_buffer_page_len(void *page); |
| 185 | |
| 186 | |
Vaibhav Nagarnaik | 7ea5906 | 2011-05-03 17:56:42 -0700 | [diff] [blame] | 187 | void *ring_buffer_alloc_read_page(struct ring_buffer *buffer, int cpu); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 188 | void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data); |
Steven Rostedt | ef7a4a1 | 2009-03-03 00:27:49 -0500 | [diff] [blame] | 189 | int ring_buffer_read_page(struct ring_buffer *buffer, void **data_page, |
| 190 | size_t len, int cpu, int full); |
Steven Rostedt | 8789a9e | 2008-12-02 15:34:07 -0500 | [diff] [blame] | 191 | |
Steven Rostedt | d1b182a | 2009-04-15 16:53:47 -0400 | [diff] [blame] | 192 | struct trace_seq; |
| 193 | |
| 194 | int ring_buffer_print_entry_header(struct trace_seq *s); |
| 195 | int ring_buffer_print_page_header(struct trace_seq *s); |
| 196 | |
Steven Rostedt | 7a8e76a | 2008-09-29 23:02:38 -0400 | [diff] [blame] | 197 | enum ring_buffer_flags { |
| 198 | RB_FL_OVERWRITE = 1 << 0, |
| 199 | }; |
| 200 | |
| 201 | #endif /* _LINUX_RING_BUFFER_H */ |