1 #ifndef _LTTNG_RING_BUFFER_FRONTEND_H
2 #define _LTTNG_RING_BUFFER_FRONTEND_H
5 * libringbuffer/frontend.h
7 * Ring Buffer Library Synchronization Header (API).
9 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
27 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
29 * See ring_buffer_frontend.c for more information on wait-free algorithms.
33 #include <urcu/compiler.h>
34 #include <urcu/uatomic.h>
37 /* Internal helpers */
38 #include "frontend_internal.h"
40 /* Buffer creation/removal and setup operations */
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.
46 * read_timer_interval is the time interval (in us) to wake up pending readers.
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.
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
59 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
62 size_t priv_data_align
,
63 size_t priv_data_size
,
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
);
73 * channel_destroy finalizes all channel's buffers, waits for readers to
74 * release all references, and destroys the channel.
77 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
81 /* Buffer read operations */
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.
89 #define for_each_channel_cpu(cpu, chan) \
90 for_each_possible_cpu(cpu)
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
,
98 uint64_t *memory_map_size
);
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
);
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
);
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
,
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
,
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
);
124 * Initialize signals for ring buffer. Should be called early e.g. by
125 * main() in the program to affect all threads.
127 void lib_ringbuffer_signal_init(void);
130 * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer.
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
);
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
);
152 * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers
153 * to read sub-buffers sequentially.
155 static inline int lib_ring_buffer_get_next_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
156 struct lttng_ust_shm_handle
*handle
)
160 ret
= lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
161 &buf
->prod_snapshot
, handle
);
164 ret
= lib_ring_buffer_get_subbuf(buf
, buf
->cons_snapshot
, handle
);
169 void lib_ring_buffer_put_next_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
170 struct lttng_ust_shm_handle
*handle
)
172 struct channel
*chan
;
174 chan
= shmp(handle
, buf
->backend
.chan
);
177 lib_ring_buffer_put_subbuf(buf
, handle
);
178 lib_ring_buffer_move_consumer(buf
, subbuf_align(buf
->cons_snapshot
, chan
),
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
);
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
)
190 return v_read(config
, &buf
->offset
);
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
)
197 return uatomic_read(&buf
->consumed
);
201 * Must call lib_ring_buffer_is_finalized before reading counters (memory
202 * ordering enforced with respect to trace teardown).
205 int lib_ring_buffer_is_finalized(const struct lttng_ust_lib_ring_buffer_config
*config
,
206 struct lttng_ust_lib_ring_buffer
*buf
)
208 int finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
210 * Read finalized before counters.
217 int lib_ring_buffer_channel_is_finalized(const struct channel
*chan
)
219 return chan
->finalized
;
223 int lib_ring_buffer_channel_is_disabled(const struct channel
*chan
)
225 return uatomic_read(&chan
->record_disabled
);
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
)
234 return subbuffer_get_read_data_size(config
, &buf
->backend
, handle
);
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
)
242 return v_read(config
, &buf
->records_count
);
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
)
250 return v_read(config
, &buf
->records_overrun
);
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
)
258 return v_read(config
, &buf
->records_lost_full
);
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
)
266 return v_read(config
, &buf
->records_lost_wrap
);
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
)
274 return v_read(config
, &buf
->records_lost_big
);
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
)
282 return v_read(config
, &buf
->backend
.records_read
);
285 #endif /* _LTTNG_RING_BUFFER_FRONTEND_H */