1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_RING_BUFFER_H 3 #define _LINUX_RING_BUFFER_H 4 5 #include <linux/mm.h> 6 #include <linux/seq_file.h> 7 #include <linux/poll.h> 8 9 struct ring_buffer; 10 struct ring_buffer_iter; 11 12 /* 13 * Don't refer to this struct directly, use functions below. 14 */ 15 struct ring_buffer_event { 16 u32 type_len:5, time_delta:27; 17 18 u32 array[]; 19 }; 20 21 /** 22 * enum ring_buffer_type - internal ring buffer types 23 * 24 * @RINGBUF_TYPE_PADDING: Left over page padding or discarded event 25 * If time_delta is 0: 26 * array is ignored 27 * size is variable depending on how much 28 * padding is needed 29 * If time_delta is non zero: 30 * array[0] holds the actual length 31 * size = 4 + length (bytes) 32 * 33 * @RINGBUF_TYPE_TIME_EXTEND: Extend the time delta 34 * array[0] = time delta (28 .. 59) 35 * size = 8 bytes 36 * 37 * @RINGBUF_TYPE_TIME_STAMP: Sync time stamp with external clock 38 * array[0] = tv_nsec 39 * array[1..2] = tv_sec 40 * size = 16 bytes 41 * 42 * <= @RINGBUF_TYPE_DATA_TYPE_LEN_MAX: 43 * Data record 44 * If type_len is zero: 45 * array[0] holds the actual length 46 * array[1..(length+3)/4] holds data 47 * size = 4 + length (bytes) 48 * else 49 * length = type_len << 2 50 * array[0..(length+3)/4-1] holds data 51 * size = 4 + length (bytes) 52 */ 53 enum ring_buffer_type { 54 RINGBUF_TYPE_DATA_TYPE_LEN_MAX = 28, 55 RINGBUF_TYPE_PADDING, 56 RINGBUF_TYPE_TIME_EXTEND, 57 /* FIXME: RINGBUF_TYPE_TIME_STAMP not implemented */ 58 RINGBUF_TYPE_TIME_STAMP, 59 }; 60 61 unsigned ring_buffer_event_length(struct ring_buffer_event *event); 62 void *ring_buffer_event_data(struct ring_buffer_event *event); 63 64 /* 65 * ring_buffer_discard_commit will remove an event that has not 66 * ben committed yet. If this is used, then ring_buffer_unlock_commit 67 * must not be called on the discarded event. This function 68 * will try to remove the event from the ring buffer completely 69 * if another event has not been written after it. 70 * 71 * Example use: 72 * 73 * if (some_condition) 74 * ring_buffer_discard_commit(buffer, event); 75 * else 76 * ring_buffer_unlock_commit(buffer, event); 77 */ 78 void ring_buffer_discard_commit(struct ring_buffer *buffer, 79 struct ring_buffer_event *event); 80 81 /* 82 * size is in bytes for each per CPU buffer. 83 */ 84 struct ring_buffer * 85 __ring_buffer_alloc(unsigned long size, unsigned flags, struct lock_class_key *key); 86 87 /* 88 * Because the ring buffer is generic, if other users of the ring buffer get 89 * traced by ftrace, it can produce lockdep warnings. We need to keep each 90 * ring buffer's lock class separate. 91 */ 92 #define ring_buffer_alloc(size, flags) \ 93 ({ \ 94 static struct lock_class_key __key; \ 95 __ring_buffer_alloc((size), (flags), &__key); \ 96 }) 97 98 int ring_buffer_wait(struct ring_buffer *buffer, int cpu, bool full); 99 __poll_t ring_buffer_poll_wait(struct ring_buffer *buffer, int cpu, 100 struct file *filp, poll_table *poll_table); 101 102 103 #define RING_BUFFER_ALL_CPUS -1 104 105 void ring_buffer_free(struct ring_buffer *buffer); 106 107 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size, int cpu); 108 109 void ring_buffer_change_overwrite(struct ring_buffer *buffer, int val); 110 111 struct ring_buffer_event *ring_buffer_lock_reserve(struct ring_buffer *buffer, 112 unsigned long length); 113 int ring_buffer_unlock_commit(struct ring_buffer *buffer, 114 struct ring_buffer_event *event); 115 int ring_buffer_write(struct ring_buffer *buffer, 116 unsigned long length, void *data); 117 118 struct ring_buffer_event * 119 ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts, 120 unsigned long *lost_events); 121 struct ring_buffer_event * 122 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts, 123 unsigned long *lost_events); 124 125 struct ring_buffer_iter * 126 ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu); 127 void ring_buffer_read_prepare_sync(void); 128 void ring_buffer_read_start(struct ring_buffer_iter *iter); 129 void ring_buffer_read_finish(struct ring_buffer_iter *iter); 130 131 struct ring_buffer_event * 132 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts); 133 struct ring_buffer_event * 134 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts); 135 void ring_buffer_iter_reset(struct ring_buffer_iter *iter); 136 int ring_buffer_iter_empty(struct ring_buffer_iter *iter); 137 138 unsigned long ring_buffer_size(struct ring_buffer *buffer, int cpu); 139 140 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu); 141 void ring_buffer_reset(struct ring_buffer *buffer); 142 143 #ifdef CONFIG_RING_BUFFER_ALLOW_SWAP 144 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a, 145 struct ring_buffer *buffer_b, int cpu); 146 #else 147 static inline int 148 ring_buffer_swap_cpu(struct ring_buffer *buffer_a, 149 struct ring_buffer *buffer_b, int cpu) 150 { 151 return -ENODEV; 152 } 153 #endif 154 155 bool ring_buffer_empty(struct ring_buffer *buffer); 156 bool ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu); 157 158 void ring_buffer_record_disable(struct ring_buffer *buffer); 159 void ring_buffer_record_enable(struct ring_buffer *buffer); 160 void ring_buffer_record_off(struct ring_buffer *buffer); 161 void ring_buffer_record_on(struct ring_buffer *buffer); 162 int ring_buffer_record_is_on(struct ring_buffer *buffer); 163 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu); 164 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu); 165 166 u64 ring_buffer_oldest_event_ts(struct ring_buffer *buffer, int cpu); 167 unsigned long ring_buffer_bytes_cpu(struct ring_buffer *buffer, int cpu); 168 unsigned long ring_buffer_entries(struct ring_buffer *buffer); 169 unsigned long ring_buffer_overruns(struct ring_buffer *buffer); 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); 172 unsigned long ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu); 173 unsigned long ring_buffer_dropped_events_cpu(struct ring_buffer *buffer, int cpu); 174 unsigned long ring_buffer_read_events_cpu(struct ring_buffer *buffer, int cpu); 175 176 u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu); 177 void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer, 178 int cpu, u64 *ts); 179 void ring_buffer_set_clock(struct ring_buffer *buffer, 180 u64 (*clock)(void)); 181 182 size_t ring_buffer_page_len(void *page); 183 184 185 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer, int cpu); 186 void ring_buffer_free_read_page(struct ring_buffer *buffer, int cpu, void *data); 187 int ring_buffer_read_page(struct ring_buffer *buffer, void **data_page, 188 size_t len, int cpu, int full); 189 190 struct trace_seq; 191 192 int ring_buffer_print_entry_header(struct trace_seq *s); 193 int ring_buffer_print_page_header(struct trace_seq *s); 194 195 enum ring_buffer_flags { 196 RB_FL_OVERWRITE = 1 << 0, 197 }; 198 199 #ifdef CONFIG_RING_BUFFER 200 int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node); 201 #else 202 #define trace_rb_cpu_prepare NULL 203 #endif 204 205 #endif /* _LINUX_RING_BUFFER_H */ 206