Version 2.2.3
[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
32 #include <urcu/compiler.h>
33 #include <urcu/uatomic.h>
34
35 #include "smp.h"
36 /* Internal helpers */
37 #include "frontend_internal.h"
38
39 /* Buffer creation/removal and setup operations */
40
41 /*
42 * switch_timer_interval is the time interval (in us) to fill sub-buffers with
43 * padding to let readers get those sub-buffers. Used for live streaming.
44 *
45 * read_timer_interval is the time interval (in us) to wake up pending readers.
46 *
47 * buf_addr is a pointer the the beginning of the preallocated buffer contiguous
48 * address mapping. It is used only by RING_BUFFER_STATIC configuration. It can
49 * be set to NULL for other backends.
50 *
51 * priv_data (output) is set to a pointer into a "priv_data_len"-sized
52 * memory area for client-specific data. This memory is managed by lib
53 * ring buffer. priv_data_align is the alignment required for the
54 * private data area.
55 */
56
57 extern
58 struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config,
59 const char *name,
60 void **priv_data,
61 size_t priv_data_align,
62 size_t priv_data_size,
63 void *priv_data_init,
64 void *buf_addr,
65 size_t subbuf_size, size_t num_subbuf,
66 unsigned int switch_timer_interval,
67 unsigned int read_timer_interval);
68
69 /*
70 * channel_destroy finalizes all channel's buffers, waits for readers to
71 * release all references, and destroys the channel.
72 */
73 extern
74 void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle,
75 int consumer);
76
77
78 /* Buffer read operations */
79
80 /*
81 * Iteration on channel cpumask needs to issue a read barrier to match the write
82 * barrier in cpu hotplug. It orders the cpumask read before read of per-cpu
83 * buffer data. The per-cpu buffer is never removed by cpu hotplug; teardown is
84 * only performed at channel destruction.
85 */
86 #define for_each_channel_cpu(cpu, chan) \
87 for_each_possible_cpu(cpu)
88
89 extern struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer(
90 const struct lttng_ust_lib_ring_buffer_config *config,
91 struct channel *chan, int cpu,
92 struct lttng_ust_shm_handle *handle,
93 int *shm_fd, int *wait_fd,
94 int *wakeup_fd,
95 uint64_t *memory_map_size);
96 extern
97 int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config,
98 struct channel *chan,
99 struct lttng_ust_shm_handle *handle);
100 extern
101 int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config,
102 struct channel *chan,
103 struct lttng_ust_shm_handle *handle);
104 extern
105 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config,
106 struct channel *chan,
107 struct lttng_ust_shm_handle *handle,
108 int cpu);
109 extern
110 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config,
111 struct channel *chan,
112 struct lttng_ust_shm_handle *handle,
113 int cpu);
114
115 extern int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf,
116 struct lttng_ust_shm_handle *handle);
117 extern void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf,
118 struct lttng_ust_shm_handle *handle);
119
120 /*
121 * Initialize signals for ring buffer. Should be called early e.g. by
122 * main() in the program to affect all threads.
123 */
124 void lib_ringbuffer_signal_init(void);
125
126 /*
127 * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer.
128 */
129 extern int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf,
130 unsigned long *consumed,
131 unsigned long *produced,
132 struct lttng_ust_shm_handle *handle);
133 extern void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf,
134 unsigned long consumed_new,
135 struct lttng_ust_shm_handle *handle);
136
137 extern int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf,
138 unsigned long consumed,
139 struct lttng_ust_shm_handle *handle);
140 extern void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf,
141 struct lttng_ust_shm_handle *handle);
142
143 /*
144 * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers
145 * to read sub-buffers sequentially.
146 */
147 static inline int lib_ring_buffer_get_next_subbuf(struct lttng_ust_lib_ring_buffer *buf,
148 struct lttng_ust_shm_handle *handle)
149 {
150 int ret;
151
152 ret = lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
153 &buf->prod_snapshot, handle);
154 if (ret)
155 return ret;
156 ret = lib_ring_buffer_get_subbuf(buf, buf->cons_snapshot, handle);
157 return ret;
158 }
159
160 static inline
161 void lib_ring_buffer_put_next_subbuf(struct lttng_ust_lib_ring_buffer *buf,
162 struct lttng_ust_shm_handle *handle)
163 {
164 lib_ring_buffer_put_subbuf(buf, handle);
165 lib_ring_buffer_move_consumer(buf, subbuf_align(buf->cons_snapshot,
166 shmp(handle, buf->backend.chan)), handle);
167 }
168
169 extern void channel_reset(struct channel *chan);
170 extern void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf,
171 struct lttng_ust_shm_handle *handle);
172
173 static inline
174 unsigned long lib_ring_buffer_get_offset(const struct lttng_ust_lib_ring_buffer_config *config,
175 struct lttng_ust_lib_ring_buffer *buf)
176 {
177 return v_read(config, &buf->offset);
178 }
179
180 static inline
181 unsigned long lib_ring_buffer_get_consumed(const struct lttng_ust_lib_ring_buffer_config *config,
182 struct lttng_ust_lib_ring_buffer *buf)
183 {
184 return uatomic_read(&buf->consumed);
185 }
186
187 /*
188 * Must call lib_ring_buffer_is_finalized before reading counters (memory
189 * ordering enforced with respect to trace teardown).
190 */
191 static inline
192 int lib_ring_buffer_is_finalized(const struct lttng_ust_lib_ring_buffer_config *config,
193 struct lttng_ust_lib_ring_buffer *buf)
194 {
195 int finalized = CMM_ACCESS_ONCE(buf->finalized);
196 /*
197 * Read finalized before counters.
198 */
199 cmm_smp_rmb();
200 return finalized;
201 }
202
203 static inline
204 int lib_ring_buffer_channel_is_finalized(const struct channel *chan)
205 {
206 return chan->finalized;
207 }
208
209 static inline
210 int lib_ring_buffer_channel_is_disabled(const struct channel *chan)
211 {
212 return uatomic_read(&chan->record_disabled);
213 }
214
215 static inline
216 unsigned long lib_ring_buffer_get_read_data_size(
217 const struct lttng_ust_lib_ring_buffer_config *config,
218 struct lttng_ust_lib_ring_buffer *buf,
219 struct lttng_ust_shm_handle *handle)
220 {
221 return subbuffer_get_read_data_size(config, &buf->backend, handle);
222 }
223
224 static inline
225 unsigned long lib_ring_buffer_get_records_count(
226 const struct lttng_ust_lib_ring_buffer_config *config,
227 struct lttng_ust_lib_ring_buffer *buf)
228 {
229 return v_read(config, &buf->records_count);
230 }
231
232 static inline
233 unsigned long lib_ring_buffer_get_records_overrun(
234 const struct lttng_ust_lib_ring_buffer_config *config,
235 struct lttng_ust_lib_ring_buffer *buf)
236 {
237 return v_read(config, &buf->records_overrun);
238 }
239
240 static inline
241 unsigned long lib_ring_buffer_get_records_lost_full(
242 const struct lttng_ust_lib_ring_buffer_config *config,
243 struct lttng_ust_lib_ring_buffer *buf)
244 {
245 return v_read(config, &buf->records_lost_full);
246 }
247
248 static inline
249 unsigned long lib_ring_buffer_get_records_lost_wrap(
250 const struct lttng_ust_lib_ring_buffer_config *config,
251 struct lttng_ust_lib_ring_buffer *buf)
252 {
253 return v_read(config, &buf->records_lost_wrap);
254 }
255
256 static inline
257 unsigned long lib_ring_buffer_get_records_lost_big(
258 const struct lttng_ust_lib_ring_buffer_config *config,
259 struct lttng_ust_lib_ring_buffer *buf)
260 {
261 return v_read(config, &buf->records_lost_big);
262 }
263
264 static inline
265 unsigned long lib_ring_buffer_get_records_read(
266 const struct lttng_ust_lib_ring_buffer_config *config,
267 struct lttng_ust_lib_ring_buffer *buf)
268 {
269 return v_read(config, &buf->backend.records_read);
270 }
271
272 #endif /* _LTTNG_RING_BUFFER_FRONTEND_H */
This page took 0.037351 seconds and 4 git commands to generate.