016fc530685cd572602fef5cb67d00dbd01fc743
[lttng-ust.git] / libringbuffer / frontend.h
1 #ifndef _LTTNG_RING_BUFFER_FRONTEND_H
2 #define _LTTNG_RING_BUFFER_FRONTEND_H
3
4 /*
5 * libringbuffer/frontend.h
6 *
7 * Ring Buffer Library Synchronization Header (API).
8 *
9 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; only
14 * version 2.1 of the License.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 *
26 * Author:
27 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
28 *
29 * See ring_buffer_frontend.c for more information on wait-free algorithms.
30 */
31 #include <stddef.h>
32
33 #include <urcu/compiler.h>
34 #include <urcu/uatomic.h>
35
36 #include "smp.h"
37 /* Internal helpers */
38 #include "frontend_internal.h"
39
40 /* Buffer creation/removal and setup operations */
41
42 /*
43 * switch_timer_interval is the time interval (in us) to fill sub-buffers with
44 * padding to let readers get those sub-buffers. Used for live streaming.
45 *
46 * read_timer_interval is the time interval (in us) to wake up pending readers.
47 *
48 * buf_addr is a pointer the the beginning of the preallocated buffer contiguous
49 * address mapping. It is used only by RING_BUFFER_STATIC configuration. It can
50 * be set to NULL for other backends.
51 *
52 * priv_data (output) is set to a pointer into a "priv_data_len"-sized
53 * memory area for client-specific data. This memory is managed by lib
54 * ring buffer. priv_data_align is the alignment required for the
55 * private data area.
56 */
57
58 extern
59 struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config,
60 const char *name,
61 void **priv_data,
62 size_t priv_data_align,
63 size_t priv_data_size,
64 void *priv_data_init,
65 void *buf_addr,
66 size_t subbuf_size, size_t num_subbuf,
67 unsigned int switch_timer_interval,
68 unsigned int read_timer_interval,
69 const int *stream_fds, int nr_stream_fds,
70 int64_t blocking_timeout);
71
72 /*
73 * channel_destroy finalizes all channel's buffers, waits for readers to
74 * release all references, and destroys the channel.
75 */
76 extern
77 void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle,
78 int consumer);
79
80
81 /* Buffer read operations */
82
83 /*
84 * Iteration on channel cpumask needs to issue a read barrier to match the write
85 * barrier in cpu hotplug. It orders the cpumask read before read of per-cpu
86 * buffer data. The per-cpu buffer is never removed by cpu hotplug; teardown is
87 * only performed at channel destruction.
88 */
89 #define for_each_channel_cpu(cpu, chan) \
90 for_each_possible_cpu(cpu)
91
92 extern struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer(
93 const struct lttng_ust_lib_ring_buffer_config *config,
94 struct channel *chan, int cpu,
95 struct lttng_ust_shm_handle *handle,
96 int *shm_fd, int *wait_fd,
97 int *wakeup_fd,
98 uint64_t *memory_map_size);
99 extern
100 int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config,
101 struct channel *chan,
102 struct lttng_ust_shm_handle *handle);
103 extern
104 int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config,
105 struct channel *chan,
106 struct lttng_ust_shm_handle *handle);
107 extern
108 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config,
109 struct channel *chan,
110 struct lttng_ust_shm_handle *handle,
111 int cpu);
112 extern
113 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config,
114 struct channel *chan,
115 struct lttng_ust_shm_handle *handle,
116 int cpu);
117
118 extern int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf,
119 struct lttng_ust_shm_handle *handle);
120 extern void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf,
121 struct lttng_ust_shm_handle *handle);
122
123 /*
124 * Initialize signals for ring buffer. Should be called early e.g. by
125 * main() in the program to affect all threads.
126 */
127 void lib_ringbuffer_signal_init(void);
128
129 /*
130 * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer.
131 */
132 extern int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf,
133 unsigned long *consumed,
134 unsigned long *produced,
135 struct lttng_ust_shm_handle *handle);
136 extern int lib_ring_buffer_snapshot_sample_positions(
137 struct lttng_ust_lib_ring_buffer *buf,
138 unsigned long *consumed,
139 unsigned long *produced,
140 struct lttng_ust_shm_handle *handle);
141 extern void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf,
142 unsigned long consumed_new,
143 struct lttng_ust_shm_handle *handle);
144
145 extern int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf,
146 unsigned long consumed,
147 struct lttng_ust_shm_handle *handle);
148 extern void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf,
149 struct lttng_ust_shm_handle *handle);
150
151 /*
152 * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers
153 * to read sub-buffers sequentially.
154 */
155 static inline int lib_ring_buffer_get_next_subbuf(struct lttng_ust_lib_ring_buffer *buf,
156 struct lttng_ust_shm_handle *handle)
157 {
158 int ret;
159
160 ret = lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
161 &buf->prod_snapshot, handle);
162 if (ret)
163 return ret;
164 ret = lib_ring_buffer_get_subbuf(buf, buf->cons_snapshot, handle);
165 return ret;
166 }
167
168 static inline
169 void lib_ring_buffer_put_next_subbuf(struct lttng_ust_lib_ring_buffer *buf,
170 struct lttng_ust_shm_handle *handle)
171 {
172 struct channel *chan;
173
174 chan = shmp(handle, buf->backend.chan);
175 if (!chan)
176 return;
177 lib_ring_buffer_put_subbuf(buf, handle);
178 lib_ring_buffer_move_consumer(buf, subbuf_align(buf->cons_snapshot, chan),
179 handle);
180 }
181
182 extern void channel_reset(struct channel *chan);
183 extern void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf,
184 struct lttng_ust_shm_handle *handle);
185
186 static inline
187 unsigned long lib_ring_buffer_get_offset(const struct lttng_ust_lib_ring_buffer_config *config,
188 struct lttng_ust_lib_ring_buffer *buf)
189 {
190 return v_read(config, &buf->offset);
191 }
192
193 static inline
194 unsigned long lib_ring_buffer_get_consumed(const struct lttng_ust_lib_ring_buffer_config *config,
195 struct lttng_ust_lib_ring_buffer *buf)
196 {
197 return uatomic_read(&buf->consumed);
198 }
199
200 /*
201 * Must call lib_ring_buffer_is_finalized before reading counters (memory
202 * ordering enforced with respect to trace teardown).
203 */
204 static inline
205 int lib_ring_buffer_is_finalized(const struct lttng_ust_lib_ring_buffer_config *config,
206 struct lttng_ust_lib_ring_buffer *buf)
207 {
208 int finalized = CMM_ACCESS_ONCE(buf->finalized);
209 /*
210 * Read finalized before counters.
211 */
212 cmm_smp_rmb();
213 return finalized;
214 }
215
216 static inline
217 int lib_ring_buffer_channel_is_finalized(const struct channel *chan)
218 {
219 return chan->finalized;
220 }
221
222 static inline
223 int lib_ring_buffer_channel_is_disabled(const struct channel *chan)
224 {
225 return uatomic_read(&chan->record_disabled);
226 }
227
228 static inline
229 unsigned long lib_ring_buffer_get_read_data_size(
230 const struct lttng_ust_lib_ring_buffer_config *config,
231 struct lttng_ust_lib_ring_buffer *buf,
232 struct lttng_ust_shm_handle *handle)
233 {
234 return subbuffer_get_read_data_size(config, &buf->backend, handle);
235 }
236
237 static inline
238 unsigned long lib_ring_buffer_get_records_count(
239 const struct lttng_ust_lib_ring_buffer_config *config,
240 struct lttng_ust_lib_ring_buffer *buf)
241 {
242 return v_read(config, &buf->records_count);
243 }
244
245 static inline
246 unsigned long lib_ring_buffer_get_records_overrun(
247 const struct lttng_ust_lib_ring_buffer_config *config,
248 struct lttng_ust_lib_ring_buffer *buf)
249 {
250 return v_read(config, &buf->records_overrun);
251 }
252
253 static inline
254 unsigned long lib_ring_buffer_get_records_lost_full(
255 const struct lttng_ust_lib_ring_buffer_config *config,
256 struct lttng_ust_lib_ring_buffer *buf)
257 {
258 return v_read(config, &buf->records_lost_full);
259 }
260
261 static inline
262 unsigned long lib_ring_buffer_get_records_lost_wrap(
263 const struct lttng_ust_lib_ring_buffer_config *config,
264 struct lttng_ust_lib_ring_buffer *buf)
265 {
266 return v_read(config, &buf->records_lost_wrap);
267 }
268
269 static inline
270 unsigned long lib_ring_buffer_get_records_lost_big(
271 const struct lttng_ust_lib_ring_buffer_config *config,
272 struct lttng_ust_lib_ring_buffer *buf)
273 {
274 return v_read(config, &buf->records_lost_big);
275 }
276
277 static inline
278 unsigned long lib_ring_buffer_get_records_read(
279 const struct lttng_ust_lib_ring_buffer_config *config,
280 struct lttng_ust_lib_ring_buffer *buf)
281 {
282 return v_read(config, &buf->backend.records_read);
283 }
284
285 #endif /* _LTTNG_RING_BUFFER_FRONTEND_H */
This page took 0.034421 seconds and 3 git commands to generate.