2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Ring Buffer Library Synchronization Header (API).
8 * See ring_buffer_frontend.c for more information on wait-free algorithms.
11 #ifndef _LTTNG_RING_BUFFER_FRONTEND_H
12 #define _LTTNG_RING_BUFFER_FRONTEND_H
17 #include <urcu/compiler.h>
18 #include <urcu/uatomic.h>
22 /* Internal helpers */
23 #include "frontend_internal.h"
25 /* Buffer creation/removal and setup operations */
28 * switch_timer_interval is the time interval (in us) to fill sub-buffers with
29 * padding to let readers get those sub-buffers. Used for live streaming.
31 * read_timer_interval is the time interval (in us) to wake up pending readers.
33 * buf_addr is a pointer the the beginning of the preallocated buffer contiguous
34 * address mapping. It is used only by RING_BUFFER_STATIC configuration. It can
35 * be set to NULL for other backends.
37 * private data is a memory area for configuration data. This memory is
38 * managed by lib ring buffer. priv_data_align is the alignment required
39 * for the private data area.
42 __attribute__((visibility("hidden")))
44 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
46 size_t priv_data_align
,
47 size_t priv_data_size
,
51 size_t subbuf_size
, size_t num_subbuf
,
52 unsigned int switch_timer_interval
,
53 unsigned int read_timer_interval
,
54 const int *stream_fds
, int nr_stream_fds
,
55 int64_t blocking_timeout
);
58 * channel_destroy finalizes all channel's buffers, waits for readers to
59 * release all references, and destroys the channel.
61 __attribute__((visibility("hidden")))
62 void channel_destroy(struct lttng_ust_lib_ring_buffer_channel
*chan
,
63 struct lttng_ust_shm_handle
*handle
,
67 /* Buffer read operations */
70 * Iteration on channel cpumask needs to issue a read barrier to match the write
71 * barrier in cpu hotplug. It orders the cpumask read before read of per-cpu
72 * buffer data. The per-cpu buffer is never removed by cpu hotplug; teardown is
73 * only performed at channel destruction.
75 #define for_each_channel_cpu(cpu, chan) \
76 for_each_possible_cpu(cpu)
78 __attribute__((visibility("hidden")))
79 extern struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
80 const struct lttng_ust_lib_ring_buffer_config
*config
,
81 struct lttng_ust_lib_ring_buffer_channel
*chan
, int cpu
,
82 struct lttng_ust_shm_handle
*handle
,
83 int *shm_fd
, int *wait_fd
,
85 uint64_t *memory_map_size
);
87 __attribute__((visibility("hidden")))
89 int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
90 struct lttng_ust_lib_ring_buffer_channel
*chan
,
91 struct lttng_ust_shm_handle
*handle
);
93 __attribute__((visibility("hidden")))
95 int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
96 struct lttng_ust_lib_ring_buffer_channel
*chan
,
97 struct lttng_ust_shm_handle
*handle
);
99 __attribute__((visibility("hidden")))
101 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
102 struct lttng_ust_lib_ring_buffer_channel
*chan
,
103 struct lttng_ust_shm_handle
*handle
,
106 __attribute__((visibility("hidden")))
108 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
109 struct lttng_ust_lib_ring_buffer_channel
*chan
,
110 struct lttng_ust_shm_handle
*handle
,
113 __attribute__((visibility("hidden")))
114 extern int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
115 struct lttng_ust_shm_handle
*handle
);
117 __attribute__((visibility("hidden")))
118 extern void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
119 struct lttng_ust_shm_handle
*handle
);
122 * Initialize signals for ring buffer. Should be called early e.g. by
123 * main() in the program to affect all threads.
125 __attribute__((visibility("hidden")))
126 void lib_ringbuffer_signal_init(void);
129 * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer.
131 __attribute__((visibility("hidden")))
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
);
137 __attribute__((visibility("hidden")))
138 extern int lib_ring_buffer_snapshot_sample_positions(
139 struct lttng_ust_lib_ring_buffer
*buf
,
140 unsigned long *consumed
,
141 unsigned long *produced
,
142 struct lttng_ust_shm_handle
*handle
);
144 __attribute__((visibility("hidden")))
145 extern void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
146 unsigned long consumed_new
,
147 struct lttng_ust_shm_handle
*handle
);
149 __attribute__((visibility("hidden")))
150 extern int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
151 unsigned long consumed
,
152 struct lttng_ust_shm_handle
*handle
);
153 __attribute__((visibility("hidden")))
154 extern void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
155 struct lttng_ust_shm_handle
*handle
);
158 * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers
159 * to read sub-buffers sequentially.
161 static inline int lib_ring_buffer_get_next_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
162 struct lttng_ust_shm_handle
*handle
)
166 ret
= lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
167 &buf
->prod_snapshot
, handle
);
170 ret
= lib_ring_buffer_get_subbuf(buf
, buf
->cons_snapshot
, handle
);
175 void lib_ring_buffer_put_next_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
176 struct lttng_ust_shm_handle
*handle
)
178 struct lttng_ust_lib_ring_buffer_channel
*chan
;
180 chan
= shmp(handle
, buf
->backend
.chan
);
183 lib_ring_buffer_put_subbuf(buf
, handle
);
184 lib_ring_buffer_move_consumer(buf
, subbuf_align(buf
->cons_snapshot
, chan
),
188 __attribute__((visibility("hidden")))
189 extern void channel_reset(struct lttng_ust_lib_ring_buffer_channel
*chan
);
191 __attribute__((visibility("hidden")))
192 extern void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
193 struct lttng_ust_shm_handle
*handle
);
196 unsigned long lib_ring_buffer_get_offset(const struct lttng_ust_lib_ring_buffer_config
*config
,
197 struct lttng_ust_lib_ring_buffer
*buf
)
199 return v_read(config
, &buf
->offset
);
203 unsigned long lib_ring_buffer_get_consumed(const struct lttng_ust_lib_ring_buffer_config
*config
,
204 struct lttng_ust_lib_ring_buffer
*buf
)
206 return uatomic_read(&buf
->consumed
);
210 * Must call lib_ring_buffer_is_finalized before reading counters (memory
211 * ordering enforced with respect to trace teardown).
214 int lib_ring_buffer_is_finalized(const struct lttng_ust_lib_ring_buffer_config
*config
,
215 struct lttng_ust_lib_ring_buffer
*buf
)
217 int finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
219 * Read finalized before counters.
226 int lib_ring_buffer_channel_is_finalized(const struct lttng_ust_lib_ring_buffer_channel
*chan
)
228 return chan
->finalized
;
232 int lib_ring_buffer_channel_is_disabled(const struct lttng_ust_lib_ring_buffer_channel
*chan
)
234 return uatomic_read(&chan
->record_disabled
);
238 unsigned long lib_ring_buffer_get_read_data_size(
239 const struct lttng_ust_lib_ring_buffer_config
*config
,
240 struct lttng_ust_lib_ring_buffer
*buf
,
241 struct lttng_ust_shm_handle
*handle
)
243 return subbuffer_get_read_data_size(config
, &buf
->backend
, handle
);
247 unsigned long lib_ring_buffer_get_records_count(
248 const struct lttng_ust_lib_ring_buffer_config
*config
,
249 struct lttng_ust_lib_ring_buffer
*buf
)
251 return v_read(config
, &buf
->records_count
);
255 unsigned long lib_ring_buffer_get_records_overrun(
256 const struct lttng_ust_lib_ring_buffer_config
*config
,
257 struct lttng_ust_lib_ring_buffer
*buf
)
259 return v_read(config
, &buf
->records_overrun
);
263 unsigned long lib_ring_buffer_get_records_lost_full(
264 const struct lttng_ust_lib_ring_buffer_config
*config
,
265 struct lttng_ust_lib_ring_buffer
*buf
)
267 return v_read(config
, &buf
->records_lost_full
);
271 unsigned long lib_ring_buffer_get_records_lost_wrap(
272 const struct lttng_ust_lib_ring_buffer_config
*config
,
273 struct lttng_ust_lib_ring_buffer
*buf
)
275 return v_read(config
, &buf
->records_lost_wrap
);
279 unsigned long lib_ring_buffer_get_records_lost_big(
280 const struct lttng_ust_lib_ring_buffer_config
*config
,
281 struct lttng_ust_lib_ring_buffer
*buf
)
283 return v_read(config
, &buf
->records_lost_big
);
287 unsigned long lib_ring_buffer_get_records_read(
288 const struct lttng_ust_lib_ring_buffer_config
*config
,
289 struct lttng_ust_lib_ring_buffer
*buf
)
291 return v_read(config
, &buf
->backend
.records_read
);
294 #endif /* _LTTNG_RING_BUFFER_FRONTEND_H */