Commit | Line | Data |
---|---|---|
852c2936 MD |
1 | #ifndef _LINUX_RING_BUFFER_FRONTEND_H |
2 | #define _LINUX_RING_BUFFER_FRONTEND_H | |
3 | ||
4 | /* | |
5 | * linux/ringbuffer/frontend.h | |
6 | * | |
7 | * (C) Copyright 2005-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
8 | * | |
9 | * Ring Buffer Library Synchronization Header (API). | |
10 | * | |
11 | * Author: | |
12 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
13 | * | |
14 | * See ring_buffer_frontend.c for more information on wait-free algorithms. | |
15 | * | |
16 | * Dual LGPL v2.1/GPL v2 license. | |
17 | */ | |
18 | ||
14641deb MD |
19 | #include <urcu/compiler.h> |
20 | #include <urcu/uatomic.h> | |
21 | ||
a6352fd4 | 22 | #include "smp.h" |
852c2936 | 23 | /* Internal helpers */ |
4931a13e | 24 | #include "frontend_internal.h" |
852c2936 MD |
25 | |
26 | /* Buffer creation/removal and setup operations */ | |
27 | ||
28 | /* | |
29 | * switch_timer_interval is the time interval (in us) to fill sub-buffers with | |
30 | * padding to let readers get those sub-buffers. Used for live streaming. | |
31 | * | |
32 | * read_timer_interval is the time interval (in us) to wake up pending readers. | |
33 | * | |
34 | * buf_addr is a pointer the the beginning of the preallocated buffer contiguous | |
35 | * address mapping. It is used only by RING_BUFFER_STATIC configuration. It can | |
36 | * be set to NULL for other backends. | |
37 | */ | |
38 | ||
39 | extern | |
431d5cf0 MD |
40 | struct shm_handle *channel_create(const struct lib_ring_buffer_config *config, |
41 | const char *name, void *priv, | |
42 | void *buf_addr, | |
43 | size_t subbuf_size, size_t num_subbuf, | |
44 | unsigned int switch_timer_interval, | |
193183fb MD |
45 | unsigned int read_timer_interval, |
46 | int *shm_fd, int *wait_fd, | |
47 | uint64_t *memory_map_size); | |
48 | ||
49 | /* channel_handle_create - for consumer. */ | |
50 | extern | |
51 | struct shm_handle *channel_handle_create(int shm_fd, int wait_fd, | |
52 | uint64_t memory_map_size); | |
53 | ||
54 | /* channel_handle_add_stream - for consumer. */ | |
55 | extern | |
56 | int channel_handle_add_stream(struct shm_handle *handle, | |
57 | int shm_fd, int wait_fd, uint64_t memory_map_size); | |
852c2936 MD |
58 | |
59 | /* | |
60 | * channel_destroy returns the private data pointer. It finalizes all channel's | |
61 | * buffers, waits for readers to release all references, and destroys the | |
62 | * channel. | |
63 | */ | |
64 | extern | |
1d498196 | 65 | void *channel_destroy(struct channel *chan, struct shm_handle *handle); |
852c2936 MD |
66 | |
67 | ||
68 | /* Buffer read operations */ | |
69 | ||
70 | /* | |
71 | * Iteration on channel cpumask needs to issue a read barrier to match the write | |
72 | * barrier in cpu hotplug. It orders the cpumask read before read of per-cpu | |
73 | * buffer data. The per-cpu buffer is never removed by cpu hotplug; teardown is | |
74 | * only performed at channel destruction. | |
75 | */ | |
76 | #define for_each_channel_cpu(cpu, chan) \ | |
a6352fd4 | 77 | for_each_possible_cpu(cpu) |
852c2936 MD |
78 | |
79 | extern struct lib_ring_buffer *channel_get_ring_buffer( | |
80 | const struct lib_ring_buffer_config *config, | |
1d498196 | 81 | struct channel *chan, int cpu, |
381c0f1e MD |
82 | struct shm_handle *handle, |
83 | int *shm_fd, int *wait_fd, | |
84 | uint64_t *memory_map_size); | |
1d498196 MD |
85 | extern int lib_ring_buffer_open_read(struct lib_ring_buffer *buf, |
86 | struct shm_handle *handle); | |
87 | extern void lib_ring_buffer_release_read(struct lib_ring_buffer *buf, | |
88 | struct shm_handle *handle); | |
852c2936 MD |
89 | |
90 | /* | |
91 | * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer. | |
92 | */ | |
93 | extern int lib_ring_buffer_snapshot(struct lib_ring_buffer *buf, | |
94 | unsigned long *consumed, | |
1d498196 MD |
95 | unsigned long *produced, |
96 | struct shm_handle *handle); | |
852c2936 | 97 | extern void lib_ring_buffer_move_consumer(struct lib_ring_buffer *buf, |
1d498196 MD |
98 | unsigned long consumed_new, |
99 | struct shm_handle *handle); | |
852c2936 MD |
100 | |
101 | extern int lib_ring_buffer_get_subbuf(struct lib_ring_buffer *buf, | |
1d498196 MD |
102 | unsigned long consumed, |
103 | struct shm_handle *handle); | |
104 | extern void lib_ring_buffer_put_subbuf(struct lib_ring_buffer *buf, | |
105 | struct shm_handle *handle); | |
852c2936 MD |
106 | |
107 | /* | |
108 | * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers | |
109 | * to read sub-buffers sequentially. | |
110 | */ | |
1d498196 MD |
111 | static inline int lib_ring_buffer_get_next_subbuf(struct lib_ring_buffer *buf, |
112 | struct shm_handle *handle) | |
852c2936 MD |
113 | { |
114 | int ret; | |
115 | ||
116 | ret = lib_ring_buffer_snapshot(buf, &buf->cons_snapshot, | |
1d498196 | 117 | &buf->prod_snapshot, handle); |
852c2936 MD |
118 | if (ret) |
119 | return ret; | |
1d498196 | 120 | ret = lib_ring_buffer_get_subbuf(buf, buf->cons_snapshot, handle); |
852c2936 MD |
121 | return ret; |
122 | } | |
123 | ||
1d498196 MD |
124 | static inline |
125 | void lib_ring_buffer_put_next_subbuf(struct lib_ring_buffer *buf, | |
126 | struct shm_handle *handle) | |
852c2936 | 127 | { |
1d498196 | 128 | lib_ring_buffer_put_subbuf(buf, handle); |
852c2936 | 129 | lib_ring_buffer_move_consumer(buf, subbuf_align(buf->cons_snapshot, |
1d498196 | 130 | shmp(handle, buf->backend.chan)), handle); |
852c2936 MD |
131 | } |
132 | ||
133 | extern void channel_reset(struct channel *chan); | |
1d498196 MD |
134 | extern void lib_ring_buffer_reset(struct lib_ring_buffer *buf, |
135 | struct shm_handle *handle); | |
852c2936 MD |
136 | |
137 | static inline | |
138 | unsigned long lib_ring_buffer_get_offset(const struct lib_ring_buffer_config *config, | |
139 | struct lib_ring_buffer *buf) | |
140 | { | |
141 | return v_read(config, &buf->offset); | |
142 | } | |
143 | ||
144 | static inline | |
145 | unsigned long lib_ring_buffer_get_consumed(const struct lib_ring_buffer_config *config, | |
146 | struct lib_ring_buffer *buf) | |
147 | { | |
14641deb | 148 | return uatomic_read(&buf->consumed); |
852c2936 MD |
149 | } |
150 | ||
151 | /* | |
152 | * Must call lib_ring_buffer_is_finalized before reading counters (memory | |
153 | * ordering enforced with respect to trace teardown). | |
154 | */ | |
155 | static inline | |
156 | int lib_ring_buffer_is_finalized(const struct lib_ring_buffer_config *config, | |
157 | struct lib_ring_buffer *buf) | |
158 | { | |
14641deb | 159 | int finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
160 | /* |
161 | * Read finalized before counters. | |
162 | */ | |
14641deb | 163 | cmm_smp_rmb(); |
852c2936 MD |
164 | return finalized; |
165 | } | |
166 | ||
167 | static inline | |
168 | int lib_ring_buffer_channel_is_finalized(const struct channel *chan) | |
169 | { | |
170 | return chan->finalized; | |
171 | } | |
172 | ||
173 | static inline | |
174 | int lib_ring_buffer_channel_is_disabled(const struct channel *chan) | |
175 | { | |
14641deb | 176 | return uatomic_read(&chan->record_disabled); |
852c2936 MD |
177 | } |
178 | ||
179 | static inline | |
180 | unsigned long lib_ring_buffer_get_read_data_size( | |
181 | const struct lib_ring_buffer_config *config, | |
1d498196 MD |
182 | struct lib_ring_buffer *buf, |
183 | struct shm_handle *handle) | |
852c2936 | 184 | { |
1d498196 | 185 | return subbuffer_get_read_data_size(config, &buf->backend, handle); |
852c2936 MD |
186 | } |
187 | ||
188 | static inline | |
189 | unsigned long lib_ring_buffer_get_records_count( | |
190 | const struct lib_ring_buffer_config *config, | |
191 | struct lib_ring_buffer *buf) | |
192 | { | |
193 | return v_read(config, &buf->records_count); | |
194 | } | |
195 | ||
196 | static inline | |
197 | unsigned long lib_ring_buffer_get_records_overrun( | |
198 | const struct lib_ring_buffer_config *config, | |
199 | struct lib_ring_buffer *buf) | |
200 | { | |
201 | return v_read(config, &buf->records_overrun); | |
202 | } | |
203 | ||
204 | static inline | |
205 | unsigned long lib_ring_buffer_get_records_lost_full( | |
206 | const struct lib_ring_buffer_config *config, | |
207 | struct lib_ring_buffer *buf) | |
208 | { | |
209 | return v_read(config, &buf->records_lost_full); | |
210 | } | |
211 | ||
212 | static inline | |
213 | unsigned long lib_ring_buffer_get_records_lost_wrap( | |
214 | const struct lib_ring_buffer_config *config, | |
215 | struct lib_ring_buffer *buf) | |
216 | { | |
217 | return v_read(config, &buf->records_lost_wrap); | |
218 | } | |
219 | ||
220 | static inline | |
221 | unsigned long lib_ring_buffer_get_records_lost_big( | |
222 | const struct lib_ring_buffer_config *config, | |
223 | struct lib_ring_buffer *buf) | |
224 | { | |
225 | return v_read(config, &buf->records_lost_big); | |
226 | } | |
227 | ||
228 | static inline | |
229 | unsigned long lib_ring_buffer_get_records_read( | |
230 | const struct lib_ring_buffer_config *config, | |
231 | struct lib_ring_buffer *buf) | |
232 | { | |
233 | return v_read(config, &buf->backend.records_read); | |
234 | } | |
235 | ||
236 | #endif /* _LINUX_RING_BUFFER_FRONTEND_H */ |