ring_buffer.h 7.3 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
S
Steven Rostedt 已提交
2 3 4 5 6
#ifndef _LINUX_RING_BUFFER_H
#define _LINUX_RING_BUFFER_H

#include <linux/mm.h>
#include <linux/seq_file.h>
7
#include <linux/poll.h>
S
Steven Rostedt 已提交
8

9
struct trace_buffer;
S
Steven Rostedt 已提交
10 11 12
struct ring_buffer_iter;

/*
W
Wenji Huang 已提交
13
 * Don't refer to this struct directly, use functions below.
S
Steven Rostedt 已提交
14 15
 */
struct ring_buffer_event {
16
	u32		type_len:5, time_delta:27;
17

S
Steven Rostedt 已提交
18 19 20 21 22 23
	u32		array[];
};

/**
 * enum ring_buffer_type - internal ring buffer types
 *
24 25 26 27
 * @RINGBUF_TYPE_PADDING:	Left over page padding or discarded event
 *				 If time_delta is 0:
 *				  array is ignored
 *				  size is variable depending on how much
S
Steven Rostedt 已提交
28
 *				  padding is needed
29
 *				 If time_delta is non zero:
30 31
 *				  array[0] holds the actual length
 *				  size = 4 + length (bytes)
S
Steven Rostedt 已提交
32 33 34 35 36
 *
 * @RINGBUF_TYPE_TIME_EXTEND:	Extend the time delta
 *				 array[0] = time delta (28 .. 59)
 *				 size = 8 bytes
 *
37 38 39 40 41 42
 * @RINGBUF_TYPE_TIME_STAMP:	Absolute timestamp
 *				 Same format as TIME_EXTEND except that the
 *				 value is an absolute timestamp, not a delta
 *				 event.time_delta contains bottom 27 bits
 *				 array[0] = top (28 .. 59) bits
 *				 size = 8 bytes
S
Steven Rostedt 已提交
43
 *
44 45 46
 * <= @RINGBUF_TYPE_DATA_TYPE_LEN_MAX:
 *				Data record
 *				 If type_len is zero:
S
Steven Rostedt 已提交
47
 *				  array[0] holds the actual length
L
Lai Jiangshan 已提交
48
 *				  array[1..(length+3)/4] holds data
49
 *				  size = 4 + length (bytes)
S
Steven Rostedt 已提交
50
 *				 else
51
 *				  length = type_len << 2
L
Lai Jiangshan 已提交
52 53
 *				  array[0..(length+3)/4-1] holds data
 *				  size = 4 + length (bytes)
S
Steven Rostedt 已提交
54 55
 */
enum ring_buffer_type {
56
	RINGBUF_TYPE_DATA_TYPE_LEN_MAX = 28,
S
Steven Rostedt 已提交
57 58 59 60 61 62 63
	RINGBUF_TYPE_PADDING,
	RINGBUF_TYPE_TIME_EXTEND,
	RINGBUF_TYPE_TIME_STAMP,
};

unsigned ring_buffer_event_length(struct ring_buffer_event *event);
void *ring_buffer_event_data(struct ring_buffer_event *event);
64
u64 ring_buffer_event_time_stamp(struct ring_buffer_event *event);
S
Steven Rostedt 已提交
65

66 67
/*
 * ring_buffer_discard_commit will remove an event that has not
68
 *   been committed yet. If this is used, then ring_buffer_unlock_commit
69 70 71 72 73 74 75 76 77 78 79
 *   must not be called on the discarded event. This function
 *   will try to remove the event from the ring buffer completely
 *   if another event has not been written after it.
 *
 * Example use:
 *
 *  if (some_condition)
 *    ring_buffer_discard_commit(buffer, event);
 *  else
 *    ring_buffer_unlock_commit(buffer, event);
 */
80
void ring_buffer_discard_commit(struct trace_buffer *buffer,
81 82
				struct ring_buffer_event *event);

S
Steven Rostedt 已提交
83 84 85
/*
 * size is in bytes for each per CPU buffer.
 */
86
struct trace_buffer *
87 88 89 90 91 92 93 94 95 96 97 98 99
__ring_buffer_alloc(unsigned long size, unsigned flags, struct lock_class_key *key);

/*
 * Because the ring buffer is generic, if other users of the ring buffer get
 * traced by ftrace, it can produce lockdep warnings. We need to keep each
 * ring buffer's lock class separate.
 */
#define ring_buffer_alloc(size, flags)			\
({							\
	static struct lock_class_key __key;		\
	__ring_buffer_alloc((size), (flags), &__key);	\
})

100 101
int ring_buffer_wait(struct trace_buffer *buffer, int cpu, int full);
__poll_t ring_buffer_poll_wait(struct trace_buffer *buffer, int cpu,
102 103 104
			  struct file *filp, poll_table *poll_table);


105 106
#define RING_BUFFER_ALL_CPUS -1

107
void ring_buffer_free(struct trace_buffer *buffer);
S
Steven Rostedt 已提交
108

109
int ring_buffer_resize(struct trace_buffer *buffer, unsigned long size, int cpu);
S
Steven Rostedt 已提交
110

111
void ring_buffer_change_overwrite(struct trace_buffer *buffer, int val);
112

113
struct ring_buffer_event *ring_buffer_lock_reserve(struct trace_buffer *buffer,
114
						   unsigned long length);
115
int ring_buffer_unlock_commit(struct trace_buffer *buffer,
116
			      struct ring_buffer_event *event);
117
int ring_buffer_write(struct trace_buffer *buffer,
S
Steven Rostedt 已提交
118 119
		      unsigned long length, void *data);

120 121
void ring_buffer_nest_start(struct trace_buffer *buffer);
void ring_buffer_nest_end(struct trace_buffer *buffer);
122

S
Steven Rostedt 已提交
123
struct ring_buffer_event *
124
ring_buffer_peek(struct trace_buffer *buffer, int cpu, u64 *ts,
125
		 unsigned long *lost_events);
S
Steven Rostedt 已提交
126
struct ring_buffer_event *
127
ring_buffer_consume(struct trace_buffer *buffer, int cpu, u64 *ts,
128
		    unsigned long *lost_events);
S
Steven Rostedt 已提交
129 130

struct ring_buffer_iter *
131
ring_buffer_read_prepare(struct trace_buffer *buffer, int cpu, gfp_t flags);
132 133
void ring_buffer_read_prepare_sync(void);
void ring_buffer_read_start(struct ring_buffer_iter *iter);
S
Steven Rostedt 已提交
134 135 136 137 138 139 140 141 142
void ring_buffer_read_finish(struct ring_buffer_iter *iter);

struct ring_buffer_event *
ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts);
struct ring_buffer_event *
ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts);
void ring_buffer_iter_reset(struct ring_buffer_iter *iter);
int ring_buffer_iter_empty(struct ring_buffer_iter *iter);

143
unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu);
S
Steven Rostedt 已提交
144

145 146
void ring_buffer_reset_cpu(struct trace_buffer *buffer, int cpu);
void ring_buffer_reset(struct trace_buffer *buffer);
S
Steven Rostedt 已提交
147

148
#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
149 150
int ring_buffer_swap_cpu(struct trace_buffer *buffer_a,
			 struct trace_buffer *buffer_b, int cpu);
151 152
#else
static inline int
153 154
ring_buffer_swap_cpu(struct trace_buffer *buffer_a,
		     struct trace_buffer *buffer_b, int cpu)
155 156 157 158
{
	return -ENODEV;
}
#endif
S
Steven Rostedt 已提交
159

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
bool ring_buffer_empty(struct trace_buffer *buffer);
bool ring_buffer_empty_cpu(struct trace_buffer *buffer, int cpu);

void ring_buffer_record_disable(struct trace_buffer *buffer);
void ring_buffer_record_enable(struct trace_buffer *buffer);
void ring_buffer_record_off(struct trace_buffer *buffer);
void ring_buffer_record_on(struct trace_buffer *buffer);
bool ring_buffer_record_is_on(struct trace_buffer *buffer);
bool ring_buffer_record_is_set_on(struct trace_buffer *buffer);
void ring_buffer_record_disable_cpu(struct trace_buffer *buffer, int cpu);
void ring_buffer_record_enable_cpu(struct trace_buffer *buffer, int cpu);

u64 ring_buffer_oldest_event_ts(struct trace_buffer *buffer, int cpu);
unsigned long ring_buffer_bytes_cpu(struct trace_buffer *buffer, int cpu);
unsigned long ring_buffer_entries(struct trace_buffer *buffer);
unsigned long ring_buffer_overruns(struct trace_buffer *buffer);
unsigned long ring_buffer_entries_cpu(struct trace_buffer *buffer, int cpu);
unsigned long ring_buffer_overrun_cpu(struct trace_buffer *buffer, int cpu);
unsigned long ring_buffer_commit_overrun_cpu(struct trace_buffer *buffer, int cpu);
unsigned long ring_buffer_dropped_events_cpu(struct trace_buffer *buffer, int cpu);
unsigned long ring_buffer_read_events_cpu(struct trace_buffer *buffer, int cpu);

u64 ring_buffer_time_stamp(struct trace_buffer *buffer, int cpu);
void ring_buffer_normalize_time_stamp(struct trace_buffer *buffer,
184
				      int cpu, u64 *ts);
185
void ring_buffer_set_clock(struct trace_buffer *buffer,
186
			   u64 (*clock)(void));
187 188
void ring_buffer_set_time_stamp_abs(struct trace_buffer *buffer, bool abs);
bool ring_buffer_time_stamp_abs(struct trace_buffer *buffer);
S
Steven Rostedt 已提交
189

190 191
size_t ring_buffer_nr_pages(struct trace_buffer *buffer, int cpu);
size_t ring_buffer_nr_dirty_pages(struct trace_buffer *buffer, int cpu);
192

193 194 195
void *ring_buffer_alloc_read_page(struct trace_buffer *buffer, int cpu);
void ring_buffer_free_read_page(struct trace_buffer *buffer, int cpu, void *data);
int ring_buffer_read_page(struct trace_buffer *buffer, void **data_page,
196
			  size_t len, int cpu, int full);
S
Steven Rostedt 已提交
197

198 199 200 201 202
struct trace_seq;

int ring_buffer_print_entry_header(struct trace_seq *s);
int ring_buffer_print_page_header(struct trace_seq *s);

S
Steven Rostedt 已提交
203 204 205 206
enum ring_buffer_flags {
	RB_FL_OVERWRITE		= 1 << 0,
};

207 208 209 210 211 212
#ifdef CONFIG_RING_BUFFER
int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node);
#else
#define trace_rb_cpu_prepare	NULL
#endif

S
Steven Rostedt 已提交
213
#endif /* _LINUX_RING_BUFFER_H */