ring-buffer: add ring_buffer_discard_commit
[safe/jmp/linux-2.6] / include / linux / ring_buffer.h
1 #ifndef _LINUX_RING_BUFFER_H
2 #define _LINUX_RING_BUFFER_H
3
4 #include <linux/mm.h>
5 #include <linux/seq_file.h>
6
7 struct ring_buffer;
8 struct ring_buffer_iter;
9
10 /*
11  * Don't refer to this struct directly, use functions below.
12  */
13 struct 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  *
21  * @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
25  *                                padding is needed
26  *                               If time_delta is non zero:
27  *                                everything else same as RINGBUF_TYPE_DATA
28  *
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
34  *                               array[0]    = tv_nsec
35  *                               array[1..2] = tv_sec
36  *                               size = 16 bytes
37  *
38  * @RINGBUF_TYPE_DATA:          Data record
39  *                               If len is zero:
40  *                                array[0] holds the actual length
41  *                                array[1..(length+3)/4] holds data
42  *                                size = 4 + 4 + length (bytes)
43  *                               else
44  *                                length = len << 2
45  *                                array[0..(length+3)/4-1] holds data
46  *                                size = 4 + length (bytes)
47  */
48 enum 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
56 unsigned ring_buffer_event_length(struct ring_buffer_event *event);
57 void *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  */
65 static inline unsigned
66 ring_buffer_event_time_delta(struct ring_buffer_event *event)
67 {
68         return event->time_delta;
69 }
70
71 /*
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  */
83 void ring_buffer_event_discard(struct ring_buffer_event *event);
84
85 /*
86  * 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  */
99 void ring_buffer_discard_commit(struct ring_buffer *buffer,
100                                 struct ring_buffer_event *event);
101
102 /*
103  * size is in bytes for each per CPU buffer.
104  */
105 struct ring_buffer *
106 ring_buffer_alloc(unsigned long size, unsigned flags);
107 void ring_buffer_free(struct ring_buffer *buffer);
108
109 int ring_buffer_resize(struct ring_buffer *buffer, unsigned long size);
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 struct ring_buffer_event *
121 ring_buffer_consume(struct ring_buffer *buffer, int cpu, u64 *ts);
122
123 struct ring_buffer_iter *
124 ring_buffer_read_start(struct ring_buffer *buffer, int cpu);
125 void ring_buffer_read_finish(struct ring_buffer_iter *iter);
126
127 struct ring_buffer_event *
128 ring_buffer_iter_peek(struct ring_buffer_iter *iter, u64 *ts);
129 struct ring_buffer_event *
130 ring_buffer_read(struct ring_buffer_iter *iter, u64 *ts);
131 void ring_buffer_iter_reset(struct ring_buffer_iter *iter);
132 int ring_buffer_iter_empty(struct ring_buffer_iter *iter);
133
134 unsigned long ring_buffer_size(struct ring_buffer *buffer);
135
136 void ring_buffer_reset_cpu(struct ring_buffer *buffer, int cpu);
137 void ring_buffer_reset(struct ring_buffer *buffer);
138
139 int ring_buffer_swap_cpu(struct ring_buffer *buffer_a,
140                          struct ring_buffer *buffer_b, int cpu);
141
142 int ring_buffer_empty(struct ring_buffer *buffer);
143 int ring_buffer_empty_cpu(struct ring_buffer *buffer, int cpu);
144
145 void ring_buffer_record_disable(struct ring_buffer *buffer);
146 void ring_buffer_record_enable(struct ring_buffer *buffer);
147 void ring_buffer_record_disable_cpu(struct ring_buffer *buffer, int cpu);
148 void ring_buffer_record_enable_cpu(struct ring_buffer *buffer, int cpu);
149
150 unsigned long ring_buffer_entries(struct ring_buffer *buffer);
151 unsigned long ring_buffer_overruns(struct ring_buffer *buffer);
152 unsigned long ring_buffer_entries_cpu(struct ring_buffer *buffer, int cpu);
153 unsigned long ring_buffer_overrun_cpu(struct ring_buffer *buffer, int cpu);
154
155 u64 ring_buffer_time_stamp(struct ring_buffer *buffer, int cpu);
156 void ring_buffer_normalize_time_stamp(struct ring_buffer *buffer,
157                                       int cpu, u64 *ts);
158 void ring_buffer_set_clock(struct ring_buffer *buffer,
159                            u64 (*clock)(void));
160
161 size_t ring_buffer_page_len(void *page);
162
163
164 void *ring_buffer_alloc_read_page(struct ring_buffer *buffer);
165 void ring_buffer_free_read_page(struct ring_buffer *buffer, void *data);
166 int ring_buffer_read_page(struct ring_buffer *buffer, void **data_page,
167                           size_t len, int cpu, int full);
168
169 enum ring_buffer_flags {
170         RB_FL_OVERWRITE         = 1 << 0,
171 };
172
173 #endif /* _LINUX_RING_BUFFER_H */