blob: f0aa486d131c6f41120a8a39731f5df31deb2eee [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 {
14 u32 type:2, len:3, time_delta:27;
15 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:
27 * everything else same as RINGBUF_TYPE_DATA
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040028 *
29 * @RINGBUF_TYPE_TIME_EXTEND: Extend the time delta
30 * array[0] = time delta (28 .. 59)
31 * size = 8 bytes
32 *
33 * @RINGBUF_TYPE_TIME_STAMP: Sync time stamp with external clock
Lai Jiangshan361b73d2008-12-08 10:58:08 +080034 * array[0] = tv_nsec
35 * array[1..2] = tv_sec
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040036 * size = 16 bytes
37 *
38 * @RINGBUF_TYPE_DATA: Data record
39 * If len is zero:
40 * array[0] holds the actual length
Lai Jiangshan361b73d2008-12-08 10:58:08 +080041 * array[1..(length+3)/4] holds data
42 * size = 4 + 4 + length (bytes)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040043 * else
44 * length = len << 2
Lai Jiangshan361b73d2008-12-08 10:58:08 +080045 * array[0..(length+3)/4-1] holds data
46 * size = 4 + length (bytes)
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040047 */
48enum ring_buffer_type {
49 RINGBUF_TYPE_PADDING,
50 RINGBUF_TYPE_TIME_EXTEND,
51 /* FIXME: RINGBUF_TYPE_TIME_STAMP not implemented */
52 RINGBUF_TYPE_TIME_STAMP,
53 RINGBUF_TYPE_DATA,
54};
55
56unsigned ring_buffer_event_length(struct ring_buffer_event *event);
57void *ring_buffer_event_data(struct ring_buffer_event *event);
58
59/**
60 * ring_buffer_event_time_delta - return the delta timestamp of the event
61 * @event: the event to get the delta timestamp of
62 *
63 * The delta timestamp is the 27 bit timestamp since the last event.
64 */
65static inline unsigned
66ring_buffer_event_time_delta(struct ring_buffer_event *event)
67{
68 return event->time_delta;
69}
70
Steven Rostedtfa1b47d2009-04-02 00:09:41 -040071/*
72 * ring_buffer_event_discard can discard any event in the ring buffer.
73 * it is up to the caller to protect against a reader from
74 * consuming it or a writer from wrapping and replacing it.
75 *
76 * No external protection is needed if this is called before
77 * the event is commited. But in that case it would be better to
78 * use ring_buffer_discard_commit.
79 *
80 * Note, if an event that has not been committed is discarded
81 * with ring_buffer_event_discard, it must still be committed.
82 */
Tom Zanussi2d622712009-03-22 03:30:49 -050083void ring_buffer_event_discard(struct ring_buffer_event *event);
84
Steven Rostedt7a8e76a2008-09-29 23:02:38 -040085/*
Steven Rostedtfa1b47d2009-04-02 00:09:41 -040086 * ring_buffer_discard_commit will remove an event that has not
87 * ben committed yet. If this is used, then ring_buffer_unlock_commit
88 * must not be called on the discarded event. This function
89 * will try to remove the event from the ring buffer completely
90 * if another event has not been written after it.
91 *
92 * Example use:
93 *
94 * if (some_condition)
95 * ring_buffer_discard_commit(buffer, event);
96 * else
97 * ring_buffer_unlock_commit(buffer, event);
98 */
99void ring_buffer_discard_commit(struct ring_buffer *buffer,
100 struct ring_buffer_event *event);
101
102/*
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400103 * size is in bytes for each per CPU buffer.
104 */
105struct ring_buffer *
106ring_buffer_alloc(unsigned long size, unsigned flags);
107void ring_buffer_free(struct ring_buffer *buffer);
108
109int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size);
110
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200111struct ring_buffer_event *ring_buffer_lock_reserve(struct ring_buffer *buffer,
112 unsigned long length);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400113int ring_buffer_unlock_commit(struct ring_buffer *buffer,
Arnaldo Carvalho de Melo0a987752009-02-05 16:12:56 -0200114 struct ring_buffer_event *event);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400115int ring_buffer_write(struct ring_buffer *buffer,
116 unsigned long length, void *data);
117
118struct ring_buffer_event *
119ring_buffer_peek(struct ring_buffer *buffer, int cpu, u64 *ts);
120struct ring_buffer_event *
121ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts);
122
123struct ring_buffer_iter *
124ring_buffer_read_start(struct ring_buffer *buffer, int cpu);
125void ring_buffer_read_finish(struct ring_buffer_iter *iter);
126
127struct ring_buffer_event *
128ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts);
129struct ring_buffer_event *
130ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts);
131void ring_buffer_iter_reset(struct ring_buffer_iter *iter);
132int ring_buffer_iter_empty(struct ring_buffer_iter *iter);
133
134unsigned long ring_buffer_size(struct ring_buffer *buffer);
135
136void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu);
137void ring_buffer_reset(struct ring_buffer *buffer);
138
139int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
140 struct ring_buffer *buffer_b, int cpu);
141
142int ring_buffer_empty(struct ring_buffer *buffer);
143int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu);
144
145void ring_buffer_record_disable(struct ring_buffer *buffer);
146void ring_buffer_record_enable(struct ring_buffer *buffer);
147void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu);
148void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu);
149
150unsigned long ring_buffer_entries(struct ring_buffer *buffer);
151unsigned long ring_buffer_overruns(struct ring_buffer *buffer);
Robert Richtere09373f2008-11-26 14:04:19 +0100152unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu);
153unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu);
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400154
Steven Rostedt37886f62009-03-17 17:22:06 -0400155u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu);
156void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
157 int cpu, u64 *ts);
158void ring_buffer_set_clock(struct ring_buffer *buffer,
159 u64 (*clock)(void));
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400160
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500161size_t ring_buffer_page_len(void *page);
162
163
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500164void *ring_buffer_alloc_read_page(struct ring_buffer *buffer);
165void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data);
Steven Rostedtef7a4a12009-03-03 00:27:49 -0500166int ring_buffer_read_page(struct ring_buffer *buffer, void **data_page,
167 size_t len, int cpu, int full);
Steven Rostedt8789a9e2008-12-02 15:34:07 -0500168
Steven Rostedt7a8e76a2008-09-29 23:02:38 -0400169enum ring_buffer_flags {
170 RB_FL_OVERWRITE = 1 << 0,
171};
172
173#endif /* _LINUX_RING_BUFFER_H */