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