Cleanup: apply `include-what-you-use` guideline for `size_t`
[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 30 */
b4051ad8 31#include <stddef.h>
852c2936 32
14641deb
MD
33#include <urcu/compiler.h>
34#include <urcu/uatomic.h>
35
a6352fd4 36#include "smp.h"
852c2936 37/* Internal helpers */
4931a13e 38#include "frontend_internal.h"
852c2936
MD
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.
a3f61e7f
MD
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.
852c2936
MD
56 */
57
58extern
4cfec15c 59struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config,
a3f61e7f
MD
60 const char *name,
61 void **priv_data,
62 size_t priv_data_align,
63 size_t priv_data_size,
d028eddb 64 void *priv_data_init,
431d5cf0
MD
65 void *buf_addr,
66 size_t subbuf_size, size_t num_subbuf,
67 unsigned int switch_timer_interval,
a9ff648c 68 unsigned int read_timer_interval,
b2c5f61a
MD
69 const int *stream_fds, int nr_stream_fds,
70 int64_t blocking_timeout);
852c2936
MD
71
72/*
a3f61e7f
MD
73 * channel_destroy finalizes all channel's buffers, waits for readers to
74 * release all references, and destroys the channel.
852c2936
MD
75 */
76extern
a3f61e7f 77void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle,
74d81a6c 78 int consumer);
852c2936
MD
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) \
a6352fd4 90 for_each_possible_cpu(cpu)
852c2936 91
4cfec15c
MD
92extern struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer(
93 const struct lttng_ust_lib_ring_buffer_config *config,
1d498196 94 struct channel *chan, int cpu,
38fae1d3 95 struct lttng_ust_shm_handle *handle,
74d81a6c
MD
96 int *shm_fd, int *wait_fd,
97 int *wakeup_fd,
98 uint64_t *memory_map_size);
99extern
ff0f5728
MD
100int 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);
103extern
104int 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);
107extern
108int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config,
74d81a6c
MD
109 struct channel *chan,
110 struct lttng_ust_shm_handle *handle,
111 int cpu);
112extern
ff0f5728 113int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config,
74d81a6c
MD
114 struct channel *chan,
115 struct lttng_ust_shm_handle *handle,
116 int cpu);
117
4cfec15c 118extern int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf,
74d81a6c 119 struct lttng_ust_shm_handle *handle);
4cfec15c 120extern void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf,
74d81a6c 121 struct lttng_ust_shm_handle *handle);
852c2936 122
03d2d293
MD
123/*
124 * Initialize signals for ring buffer. Should be called early e.g. by
125 * main() in the program to affect all threads.
126 */
127void lib_ringbuffer_signal_init(void);
128
852c2936
MD
129/*
130 * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer.
131 */
4cfec15c 132extern int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf,
852c2936 133 unsigned long *consumed,
1d498196 134 unsigned long *produced,
38fae1d3 135 struct lttng_ust_shm_handle *handle);
f45930b7
JG
136extern 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);
4cfec15c 141extern void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf,
1d498196 142 unsigned long consumed_new,
38fae1d3 143 struct lttng_ust_shm_handle *handle);
852c2936 144
4cfec15c 145extern int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf,
1d498196 146 unsigned long consumed,
38fae1d3 147 struct lttng_ust_shm_handle *handle);
4cfec15c 148extern void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 149 struct lttng_ust_shm_handle *handle);
852c2936
MD
150
151/*
152 * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers
153 * to read sub-buffers sequentially.
154 */
4cfec15c 155static inline int lib_ring_buffer_get_next_subbuf(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 156 struct lttng_ust_shm_handle *handle)
852c2936
MD
157{
158 int ret;
159
160 ret = lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
1d498196 161 &buf->prod_snapshot, handle);
852c2936
MD
162 if (ret)
163 return ret;
1d498196 164 ret = lib_ring_buffer_get_subbuf(buf, buf->cons_snapshot, handle);
852c2936
MD
165 return ret;
166}
167
1d498196 168static inline
4cfec15c 169void lib_ring_buffer_put_next_subbuf(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 170 struct lttng_ust_shm_handle *handle)
852c2936 171{
15500a1b
MD
172 struct channel *chan;
173
174 chan = shmp(handle, buf->backend.chan);
175 if (!chan)
176 return;
1d498196 177 lib_ring_buffer_put_subbuf(buf, handle);
15500a1b
MD
178 lib_ring_buffer_move_consumer(buf, subbuf_align(buf->cons_snapshot, chan),
179 handle);
852c2936
MD
180}
181
182extern void channel_reset(struct channel *chan);
4cfec15c 183extern void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 184 struct lttng_ust_shm_handle *handle);
852c2936
MD
185
186static inline
4cfec15c
MD
187unsigned long lib_ring_buffer_get_offset(const struct lttng_ust_lib_ring_buffer_config *config,
188 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
189{
190 return v_read(config, &buf->offset);
191}
192
193static inline
4cfec15c
MD
194unsigned long lib_ring_buffer_get_consumed(const struct lttng_ust_lib_ring_buffer_config *config,
195 struct lttng_ust_lib_ring_buffer *buf)
852c2936 196{
14641deb 197 return uatomic_read(&buf->consumed);
852c2936
MD
198}
199
200/*
201 * Must call lib_ring_buffer_is_finalized before reading counters (memory
202 * ordering enforced with respect to trace teardown).
203 */
204static inline
4cfec15c
MD
205int lib_ring_buffer_is_finalized(const struct lttng_ust_lib_ring_buffer_config *config,
206 struct lttng_ust_lib_ring_buffer *buf)
852c2936 207{
14641deb 208 int finalized = CMM_ACCESS_ONCE(buf->finalized);
852c2936
MD
209 /*
210 * Read finalized before counters.
211 */
14641deb 212 cmm_smp_rmb();
852c2936
MD
213 return finalized;
214}
215
216static inline
217int lib_ring_buffer_channel_is_finalized(const struct channel *chan)
218{
219 return chan->finalized;
220}
221
222static inline
223int lib_ring_buffer_channel_is_disabled(const struct channel *chan)
224{
14641deb 225 return uatomic_read(&chan->record_disabled);
852c2936
MD
226}
227
228static inline
229unsigned long lib_ring_buffer_get_read_data_size(
4cfec15c
MD
230 const struct lttng_ust_lib_ring_buffer_config *config,
231 struct lttng_ust_lib_ring_buffer *buf,
38fae1d3 232 struct lttng_ust_shm_handle *handle)
852c2936 233{
1d498196 234 return subbuffer_get_read_data_size(config, &buf->backend, handle);
852c2936
MD
235}
236
237static inline
238unsigned long lib_ring_buffer_get_records_count(
4cfec15c
MD
239 const struct lttng_ust_lib_ring_buffer_config *config,
240 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
241{
242 return v_read(config, &buf->records_count);
243}
244
245static inline
246unsigned long lib_ring_buffer_get_records_overrun(
4cfec15c
MD
247 const struct lttng_ust_lib_ring_buffer_config *config,
248 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
249{
250 return v_read(config, &buf->records_overrun);
251}
252
253static inline
254unsigned long lib_ring_buffer_get_records_lost_full(
4cfec15c
MD
255 const struct lttng_ust_lib_ring_buffer_config *config,
256 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
257{
258 return v_read(config, &buf->records_lost_full);
259}
260
261static inline
262unsigned long lib_ring_buffer_get_records_lost_wrap(
4cfec15c
MD
263 const struct lttng_ust_lib_ring_buffer_config *config,
264 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
265{
266 return v_read(config, &buf->records_lost_wrap);
267}
268
269static inline
270unsigned long lib_ring_buffer_get_records_lost_big(
4cfec15c
MD
271 const struct lttng_ust_lib_ring_buffer_config *config,
272 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
273{
274 return v_read(config, &buf->records_lost_big);
275}
276
277static inline
278unsigned long lib_ring_buffer_get_records_read(
4cfec15c
MD
279 const struct lttng_ust_lib_ring_buffer_config *config,
280 struct lttng_ust_lib_ring_buffer *buf)
852c2936
MD
281{
282 return v_read(config, &buf->backend.records_read);
283}
284
e92f3e28 285#endif /* _LTTNG_RING_BUFFER_FRONTEND_H */
This page took 0.041301 seconds and 4 git commands to generate.