blob: f1345828c7c58dd58d8b2199eda934013b0e803b [file] [log] [blame]
Steven Rostedt7a8e76a2008-09-29 23:02:38 -04001#ifndef _LINUX_RING_BUFFER_H
2#define _LINUX_RING_BUFFER_H
3
4#include <linux/mm.h>
5#include <linux/seq_file.h>
6
7struct ring_buffer;
8struct ring_buffer_iter;
9
10/*
Wenji Huangc3706f02009-02-10 01:03:18 -050011 * Don't refer to this struct directly, use functions below.
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040012 */
13struct ring_buffer_event {
Lai Jiangshan334d4162009-04-24 11:27:05 +080014 u32 type_len:5, time_delta:27;
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040015 u32 array[];
16};
17
18/**
19 * enum ring_buffer_type - internal ring buffer types
20 *
Tom Zanussi2d622712009-03-22 03:30:49 -050021 * @RINGBUF_TYPE_PADDING: Left over page padding or discarded event
22 * If time_delta is 0:
23 * array is ignored
24 * size is variable depending on how much
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040025 * padding is needed
Tom Zanussi2d622712009-03-22 03:30:49 -050026 * If time_delta is non zero:
Lai Jiangshan334d4162009-04-24 11:27:05 +080027 * array[0] holds the actual length
28 * size = 4 + length (bytes)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040029 *
30 * @RINGBUF_TYPE_TIME_EXTEND: Extend the time delta
31 * array[0] = time delta (28 .. 59)
32 * size = 8 bytes
33 *
34 * @RINGBUF_TYPE_TIME_STAMP: Sync time stamp with external clock
Lai Jiangshan361b73d2008-12-08 10:58:08 +080035 * array[0] = tv_nsec
36 * array[1..2] = tv_sec
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040037 * size = 16 bytes
38 *
Lai Jiangshan334d4162009-04-24 11:27:05 +080039 * <= @RINGBUF_TYPE_DATA_TYPE_LEN_MAX:
40 * Data record
41 * If type_len is zero:
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040042 * array[0] holds the actual length
Lai Jiangshan361b73d2008-12-08 10:58:08 +080043 * array[1..(length+3)/4] holds data
Lai Jiangshan334d4162009-04-24 11:27:05 +080044 * size = 4 + length (bytes)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040045 * else
Lai Jiangshan334d4162009-04-24 11:27:05 +080046 * length = type_len << 2
Lai Jiangshan361b73d2008-12-08 10:58:08 +080047 * array[0..(length+3)/4-1] holds data
48 * size = 4 + length (bytes)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040049 */
50enum ring_buffer_type {
Lai Jiangshan334d4162009-04-24 11:27:05 +080051 RINGBUF_TYPE_DATA_TYPE_LEN_MAX = 28,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040052 RINGBUF_TYPE_PADDING,
53 RINGBUF_TYPE_TIME_EXTEND,
54 /* FIXME: RINGBUF_TYPE_TIME_STAMP not implemented */
55 RINGBUF_TYPE_TIME_STAMP,
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040056};
57
58unsigned ring_buffer_event_length(struct ring_buffer_event *event);
59void *ring_buffer_event_data(struct ring_buffer_event *event);
60
61/**
62 * ring_buffer_event_time_delta - return the delta timestamp of the event
63 * @event: the event to get the delta timestamp of
64 *
65 * The delta timestamp is the 27 bit timestamp since the last event.
66 */
67static inline unsigned
68ring_buffer_event_time_delta(struct ring_buffer_event *event)
69{
70 return event->time_delta;
71}
72
Steven Rostedtfa1b47d2009-04-02 00:09:41 -040073/*
74 * ring_buffer_event_discard can discard any event in the ring buffer.
75 * it is up to the caller to protect against a reader from
76 * consuming it or a writer from wrapping and replacing it.
77 *
78 * No external protection is needed if this is called before
79 * the event is commited. But in that case it would be better to
80 * use ring_buffer_discard_commit.
81 *
82 * Note, if an event that has not been committed is discarded
83 * with ring_buffer_event_discard, it must still be committed.
84 */
Tom Zanussi2d622712009-03-22 03:30:49 -050085void ring_buffer_event_discard(struct ring_buffer_event *event);
86
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040087/*
Steven Rostedtfa1b47d2009-04-02 00:09:41 -040088 * ring_buffer_discard_commit will remove an event that has not
89 * ben committed yet. If this is used, then ring_buffer_unlock_commit
90 * must not be called on the discarded event. This function
91 * will try to remove the event from the ring buffer completely
92 * if another event has not been written after it.
93 *
94 * Example use:
95 *
96 * if (some_condition)
97 * ring_buffer_discard_commit(buffer, event);
98 * else
99 * ring_buffer_unlock_commit(buffer, event);
100 */
101void ring_buffer_discard_commit(struct ring_buffer *buffer,
102 struct ring_buffer_event *event);
103
104/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400105 * size is in bytes for each per CPU buffer.
106 */
107struct ring_buffer *
108ring_buffer_alloc(unsigned long size, unsigned flags);
109void ring_buffer_free(struct ring_buffer *buffer);
110
111int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size);
112
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200113struct ring_buffer_event *ring_buffer_lock_reserve(struct ring_buffer *buffer,
114 unsigned long length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400115int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200116 struct ring_buffer_event *event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400117int ring_buffer_write(struct ring_buffer *buffer,
118 unsigned long length, void *data);
119
120struct ring_buffer_event *
121ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts);
122struct ring_buffer_event *
123ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts);
124
125struct ring_buffer_iter *
126ring_buffer_read_start(struct ring_buffer *buffer, int cpu);
127void ring_buffer_read_finish(struct ring_buffer_iter *iter);
128
129struct ring_buffer_event *
130ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts);
131struct ring_buffer_event *
132ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts);
133void ring_buffer_iter_reset(struct ring_buffer_iter *iter);
134int ring_buffer_iter_empty(struct ring_buffer_iter *iter);
135
136unsigned long ring_buffer_size(struct ring_buffer *buffer);
137
138void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu);
139void ring_buffer_reset(struct ring_buffer *buffer);
140
141int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
142 struct ring_buffer *buffer_b, int cpu);
143
144int ring_buffer_empty(struct ring_buffer *buffer);
145int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu);
146
147void ring_buffer_record_disable(struct ring_buffer *buffer);
148void ring_buffer_record_enable(struct ring_buffer *buffer);
149void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu);
150void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu);
151
152unsigned long ring_buffer_entries(struct ring_buffer *buffer);
153unsigned long ring_buffer_overruns(struct ring_buffer *buffer);
Robert Richtere09373f2008-11-26 14:04:19 +0100154unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu);
155unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu);
Steven Rostedtf0d2c682009-04-29 13:43:37 -0400156unsigned long ring_buffer_commit_overrun_cpu(struct ring_buffer *buffer, int cpu);
157unsigned long ring_buffer_nmi_dropped_cpu(struct ring_buffer *buffer, int cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400158
Steven Rostedt37886f62009-03-17 17:22:06 -0400159u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu);
160void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
161 int cpu, u64 *ts);
162void ring_buffer_set_clock(struct ring_buffer *buffer,
163 u64 (*clock)(void));
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400164
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500165size_t ring_buffer_page_len(void *page);
166
167
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500168void *ring_buffer_alloc_read_page(struct ring_buffer *buffer);
169void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data);
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500170int ring_buffer_read_page(struct ring_buffer *buffer, void **data_page,
171 size_t len, int cpu, int full);
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500172
Steven Rostedtd1b182a2009-04-15 16:53:47 -0400173struct trace_seq;
174
175int ring_buffer_print_entry_header(struct trace_seq *s);
176int ring_buffer_print_page_header(struct trace_seq *s);
177
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400178enum ring_buffer_flags {
179 RB_FL_OVERWRITE = 1 << 0,
180};
181
182#endif /* _LINUX_RING_BUFFER_H */