ring_buffer.h 6.4 KB
Newer Older
S
Steven Rostedt 已提交
1 2 3
#ifndef _LINUX_RING_BUFFER_H
#define _LINUX_RING_BUFFER_H

4
#include <linux/kmemcheck.h>
S
Steven Rostedt 已提交
5 6 7 8 9 10 11
#include <linux/mm.h>
#include <linux/seq_file.h>

struct ring_buffer;
struct ring_buffer_iter;

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

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

/**
 * enum ring_buffer_type - internal ring buffer types
 *
25 26 27 28
 * @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 已提交
29
 *				  padding is needed
30
 *				 If time_delta is non zero:
31 32
 *				  array[0] holds the actual length
 *				  size = 4 + length (bytes)
S
Steven Rostedt 已提交
33 34 35 36 37 38
 *
 * @RINGBUF_TYPE_TIME_EXTEND:	Extend the time delta
 *				 array[0] = time delta (28 .. 59)
 *				 size = 8 bytes
 *
 * @RINGBUF_TYPE_TIME_STAMP:	Sync time stamp with external clock
L
Lai Jiangshan 已提交
39 40
 *				 array[0]    = tv_nsec
 *				 array[1..2] = tv_sec
S
Steven Rostedt 已提交
41 42
 *				 size = 16 bytes
 *
43 44 45
 * <= @RINGBUF_TYPE_DATA_TYPE_LEN_MAX:
 *				Data record
 *				 If type_len is zero:
S
Steven Rostedt 已提交
46
 *				  array[0] holds the actual length
L
Lai Jiangshan 已提交
47
 *				  array[1..(length+3)/4] holds data
48
 *				  size = 4 + length (bytes)
S
Steven Rostedt 已提交
49
 *				 else
50
 *				  length = type_len << 2
L
Lai Jiangshan 已提交
51 52
 *				  array[0..(length+3)/4-1] holds data
 *				  size = 4 + length (bytes)
S
Steven Rostedt 已提交
53 54
 */
enum ring_buffer_type {
55
	RINGBUF_TYPE_DATA_TYPE_LEN_MAX = 28,
S
Steven Rostedt 已提交
56 57 58 59 60 61 62 63 64
	RINGBUF_TYPE_PADDING,
	RINGBUF_TYPE_TIME_EXTEND,
	/* FIXME: RINGBUF_TYPE_TIME_STAMP not implemented */
	RINGBUF_TYPE_TIME_STAMP,
};

unsigned ring_buffer_event_length(struct ring_buffer_event *event);
void *ring_buffer_event_data(struct ring_buffer_event *event);

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
/*
 * ring_buffer_discard_commit will remove an event that has not
 *   ben committed yet. If this is used, then ring_buffer_unlock_commit
 *   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);
 */
void ring_buffer_discard_commit(struct ring_buffer *buffer,
				struct ring_buffer_event *event);

S
Steven Rostedt 已提交
82 83 84 85
/*
 * size is in bytes for each per CPU buffer.
 */
struct ring_buffer *
86 87 88 89 90 91 92 93 94 95 96 97 98
__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);	\
})

99 100
#define RING_BUFFER_ALL_CPUS -1

S
Steven Rostedt 已提交
101 102
void ring_buffer_free(struct ring_buffer *buffer);

103
int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size, int cpu);
S
Steven Rostedt 已提交
104

105 106
void ring_buffer_change_overwrite(struct ring_buffer *buffer, int val);

107 108
struct ring_buffer_event *ring_buffer_lock_reserve(struct ring_buffer *buffer,
						   unsigned long length);
S
Steven Rostedt 已提交
109
int ring_buffer_unlock_commit(struct ring_buffer *buffer,
110
			      struct ring_buffer_event *event);
S
Steven Rostedt 已提交
111 112 113 114
int ring_buffer_write(struct ring_buffer *buffer,
		      unsigned long length, void *data);

struct ring_buffer_event *
115 116
ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts,
		 unsigned long *lost_events);
S
Steven Rostedt 已提交
117
struct ring_buffer_event *
118 119
ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts,
		    unsigned long *lost_events);
S
Steven Rostedt 已提交
120 121

struct ring_buffer_iter *
122 123 124
ring_buffer_read_prepare(struct ring_buffer *buffer, int cpu);
void ring_buffer_read_prepare_sync(void);
void ring_buffer_read_start(struct ring_buffer_iter *iter);
S
Steven Rostedt 已提交
125 126 127 128 129 130 131 132 133
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);

134
unsigned long ring_buffer_size(struct ring_buffer *buffer, int cpu);
S
Steven Rostedt 已提交
135 136 137 138

void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu);
void ring_buffer_reset(struct ring_buffer *buffer);

139
#ifdef CONFIG_RING_BUFFER_ALLOW_SWAP
S
Steven Rostedt 已提交
140 141
int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
			 struct ring_buffer *buffer_b, int cpu);
142 143 144 145 146 147 148 149
#else
static inline int
ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
		     struct ring_buffer *buffer_b, int cpu)
{
	return -ENODEV;
}
#endif
S
Steven Rostedt 已提交
150 151 152 153 154 155

int ring_buffer_empty(struct ring_buffer *buffer);
int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu);

void ring_buffer_record_disable(struct ring_buffer *buffer);
void ring_buffer_record_enable(struct ring_buffer *buffer);
156 157 158
void ring_buffer_record_off(struct ring_buffer *buffer);
void ring_buffer_record_on(struct ring_buffer *buffer);
int ring_buffer_record_is_on(struct ring_buffer *buffer);
S
Steven Rostedt 已提交
159 160 161
void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu);
void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu);

162
u64 ring_buffer_oldest_event_ts(struct ring_buffer *buffer, int cpu);
163
unsigned long ring_buffer_bytes_cpu(struct ring_buffer *buffer, int cpu);
S
Steven Rostedt 已提交
164 165
unsigned long ring_buffer_entries(struct ring_buffer *buffer);
unsigned long ring_buffer_overruns(struct ring_buffer *buffer);
166 167
unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu);
unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu);
168
unsigned long ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu);
169
unsigned long ring_buffer_dropped_events_cpu(struct ring_buffer *buffer, int cpu);
170
unsigned long ring_buffer_read_events_cpu(struct ring_buffer *buffer, int cpu);
S
Steven Rostedt 已提交
171

172 173 174 175 176
u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu);
void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
				      int cpu, u64 *ts);
void ring_buffer_set_clock(struct ring_buffer *buffer,
			   u64 (*clock)(void));
S
Steven Rostedt 已提交
177

178 179 180
size_t ring_buffer_page_len(void *page);


181
void *ring_buffer_alloc_read_page(struct ring_buffer *buffer, int cpu);
S
Steven Rostedt 已提交
182
void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data);
183 184
int ring_buffer_read_page(struct ring_buffer *buffer, void **data_page,
			  size_t len, int cpu, int full);
S
Steven Rostedt 已提交
185

186 187 188 189 190
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 已提交
191 192 193 194 195
enum ring_buffer_flags {
	RB_FL_OVERWRITE		= 1 << 0,
};

#endif /* _LINUX_RING_BUFFER_H */