Version 2.9.7
[lttng-ust.git] / libringbuffer / frontend.h
CommitLineData
e92f3e28
MD
1#ifndef _LTTNG_RING_BUFFER_FRONTEND_H
2#define _LTTNG_RING_BUFFER_FRONTEND_H
852c2936
MD
3
4/*
e92f3e28 5 * libringbuffer/frontend.h
852c2936
MD
6 *
7 * Ring Buffer Library Synchronization Header (API).
8 *
e92f3e28
MD
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 *
852c2936
MD
26 * Author:
27 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
28 *
29 * See ring_buffer_frontend.c for more information on wait-free algorithms.
852c2936
MD
30 */
31
14641deb
MD
32#include <urcu/compiler.h>
33#include <urcu/uatomic.h>
34
a6352fd4 35#include "smp.h"
852c2936 36/* Internal helpers */
4931a13e 37#include "frontend_internal.h"
852c2936
MD
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.
a3f61e7f
MD
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.
852c2936
MD
55 */
56
57extern
4cfec15c 58struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config,
a3f61e7f
MD
59 const char *name,
60 void **priv_data,
61 size_t priv_data_align,
62 size_t priv_data_size,
d028eddb 63 void *priv_data_init,
431d5cf0
MD
64 void *buf_addr,
65 size_t subbuf_size, size_t num_subbuf,
66 unsigned int switch_timer_interval,
a9ff648c 67 unsigned int read_timer_interval,
5ea386c3 68 const int *stream_fds, int nr_stream_fds);
852c2936
MD
69
70/*
a3f61e7f
MD
71 * channel_destroy finalizes all channel's buffers, waits for readers to
72 * release all references, and destroys the channel.
852c2936
MD
73 */
74extern
a3f61e7f 75void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle,
74d81a6c 76 int consumer);
852c2936
MD
77
78
79/* Buffer read operations */
80
81/*
82 * Iteration on channel cpumask needs to issue a read barrier to match the write
83 * barrier in cpu hotplug. It orders the cpumask read before read of per-cpu
84 * buffer data. The per-cpu buffer is never removed by cpu hotplug; teardown is
85 * only performed at channel destruction.
86 */
87#define for_each_channel_cpu(cpu, chan) \
a6352fd4 88 for_each_possible_cpu(cpu)
852c2936 89
4cfec15c
MD
90extern struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer(
91 const struct lttng_ust_lib_ring_buffer_config *config,
1d498196 92 struct channel *chan, int cpu,
38fae1d3 93 struct lttng_ust_shm_handle *handle,
74d81a6c
MD
94 int *shm_fd, int *wait_fd,
95 int *wakeup_fd,
96 uint64_t *memory_map_size);
97extern
ff0f5728
MD
98int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config,
99 struct channel *chan,
100 struct lttng_ust_shm_handle *handle);
101extern
102int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config,
103 struct channel *chan,
104 struct lttng_ust_shm_handle *handle);
105extern
106int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config,
74d81a6c
MD
107 struct channel *chan,
108 struct lttng_ust_shm_handle *handle,
109 int cpu);
110extern
ff0f5728 111int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config,
74d81a6c
MD
112 struct channel *chan,
113 struct lttng_ust_shm_handle *handle,
114 int cpu);
115
4cfec15c 116extern int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf,
74d81a6c 117 struct lttng_ust_shm_handle *handle);
4cfec15c 118extern void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf,
74d81a6c 119 struct lttng_ust_shm_handle *handle);
852c2936 120
03d2d293
MD
121/*
122 * Initialize signals for ring buffer. Should be called early e.g. by
123 * main() in the program to affect all threads.
124 */
125void lib_ringbuffer_signal_init(void);
126
852c2936
MD
127/*
128 * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer.
129 */
4cfec15c 130extern int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf,
852c2936 131 unsigned long *consumed,
1d498196 132 unsigned long *produced,
38fae1d3 133 struct lttng_ust_shm_handle *handle);
4cfec15c 134extern void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf,
1d498196 135 unsigned long consumed_new,
38fae1d3 136 struct lttng_ust_shm_handle *handle);
852c2936 137
4cfec15c 138extern int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf,
1d498196 139 unsigned long consumed,
38fae1d3 140 struct lttng_ust_shm_handle *handle);
4cfec15c 141extern void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 142 struct lttng_ust_shm_handle *handle);
852c2936
MD
143
144/*
145 * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers
146 * to read sub-buffers sequentially.
147 */
4cfec15c 148static inline int lib_ring_buffer_get_next_subbuf(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 149 struct lttng_ust_shm_handle *handle)
852c2936
MD
150{
151 int ret;
152
153 ret = lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
1d498196 154 &buf->prod_snapshot, handle);
852c2936
MD
155 if (ret)
156 return ret;
1d498196 157 ret = lib_ring_buffer_get_subbuf(buf, buf->cons_snapshot, handle);
852c2936
MD
158 return ret;
159}
160
1d498196 161static inline
4cfec15c 162void lib_ring_buffer_put_next_subbuf(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 163 struct lttng_ust_shm_handle *handle)
852c2936 164{
15500a1b
MD
165 struct channel *chan;
166
167 chan = shmp(handle, buf->backend.chan);
168 if (!chan)
169 return;
1d498196 170 lib_ring_buffer_put_subbuf(buf, handle);
15500a1b
MD
171 lib_ring_buffer_move_consumer(buf, subbuf_align(buf->cons_snapshot, chan),
172 handle);
852c2936
MD
173}
174
175extern void channel_reset(struct channel *chan);
4cfec15c 176extern void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 177 struct lttng_ust_shm_handle *handle);
852c2936
MD
178
179static inline
4cfec15c
MD
180unsigned long lib_ring_buffer_get_offset(const struct lttng_ust_lib_ring_buffer_config *config,
181 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
182{
183 return v_read(config, &buf->offset);
184}
185
186static inline
4cfec15c
MD
187unsigned long lib_ring_buffer_get_consumed(const struct lttng_ust_lib_ring_buffer_config *config,
188 struct lttng_ust_lib_ring_buffer *buf)
852c2936 189{
14641deb 190 return uatomic_read(&buf->consumed);
852c2936
MD
191}
192
193/*
194 * Must call lib_ring_buffer_is_finalized before reading counters (memory
195 * ordering enforced with respect to trace teardown).
196 */
197static inline
4cfec15c
MD
198int lib_ring_buffer_is_finalized(const struct lttng_ust_lib_ring_buffer_config *config,
199 struct lttng_ust_lib_ring_buffer *buf)
852c2936 200{
14641deb 201 int finalized = CMM_ACCESS_ONCE(buf->finalized);
852c2936
MD
202 /*
203 * Read finalized before counters.
204 */
14641deb 205 cmm_smp_rmb();
852c2936
MD
206 return finalized;
207}
208
209static inline
210int lib_ring_buffer_channel_is_finalized(const struct channel *chan)
211{
212 return chan->finalized;
213}
214
215static inline
216int lib_ring_buffer_channel_is_disabled(const struct channel *chan)
217{
14641deb 218 return uatomic_read(&chan->record_disabled);
852c2936
MD
219}
220
221static inline
222unsigned long lib_ring_buffer_get_read_data_size(
4cfec15c
MD
223 const struct lttng_ust_lib_ring_buffer_config *config,
224 struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 225 struct lttng_ust_shm_handle *handle)
852c2936 226{
1d498196 227 return subbuffer_get_read_data_size(config, &buf->backend, handle);
852c2936
MD
228}
229
230static inline
231unsigned long lib_ring_buffer_get_records_count(
4cfec15c
MD
232 const struct lttng_ust_lib_ring_buffer_config *config,
233 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
234{
235 return v_read(config, &buf->records_count);
236}
237
238static inline
239unsigned long lib_ring_buffer_get_records_overrun(
4cfec15c
MD
240 const struct lttng_ust_lib_ring_buffer_config *config,
241 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
242{
243 return v_read(config, &buf->records_overrun);
244}
245
246static inline
247unsigned long lib_ring_buffer_get_records_lost_full(
4cfec15c
MD
248 const struct lttng_ust_lib_ring_buffer_config *config,
249 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
250{
251 return v_read(config, &buf->records_lost_full);
252}
253
254static inline
255unsigned long lib_ring_buffer_get_records_lost_wrap(
4cfec15c
MD
256 const struct lttng_ust_lib_ring_buffer_config *config,
257 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
258{
259 return v_read(config, &buf->records_lost_wrap);
260}
261
262static inline
263unsigned long lib_ring_buffer_get_records_lost_big(
4cfec15c
MD
264 const struct lttng_ust_lib_ring_buffer_config *config,
265 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
266{
267 return v_read(config, &buf->records_lost_big);
268}
269
270static inline
271unsigned long lib_ring_buffer_get_records_read(
4cfec15c
MD
272 const struct lttng_ust_lib_ring_buffer_config *config,
273 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
274{
275 return v_read(config, &buf->backend.records_read);
276}
277
e92f3e28 278#endif /* _LTTNG_RING_BUFFER_FRONTEND_H */
This page took 0.041738 seconds and 4 git commands to generate.