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 * priv_data (output) is set to a pointer into a "priv_data_len"-sized
38 * memory area for client-specific data. This memory is managed by lib
39 * ring buffer. priv_data_align is the alignment required for the
43 __attribute__((visibility("hidden")))
45 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
48 size_t priv_data_align
,
49 size_t priv_data_size
,
52 size_t subbuf_size
, size_t num_subbuf
,
53 unsigned int switch_timer_interval
,
54 unsigned int read_timer_interval
,
55 const int *stream_fds
, int nr_stream_fds
,
56 int64_t blocking_timeout
);
59 * channel_destroy finalizes all channel's buffers, waits for readers to
60 * release all references, and destroys the channel.
62 __attribute__((visibility("hidden")))
64 void channel_destroy(struct lttng_ust_lib_ring_buffer_channel
*chan
,
65 struct lttng_ust_shm_handle
*handle
,
69 /* Buffer read operations */
72 * Iteration on channel cpumask needs to issue a read barrier to match the write
73 * barrier in cpu hotplug. It orders the cpumask read before read of per-cpu
74 * buffer data. The per-cpu buffer is never removed by cpu hotplug; teardown is
75 * only performed at channel destruction.
77 #define for_each_channel_cpu(cpu, chan) \
78 for_each_possible_cpu(cpu)
80 __attribute__((visibility("hidden")))
81 extern struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
82 const struct lttng_ust_lib_ring_buffer_config
*config
,
83 struct lttng_ust_lib_ring_buffer_channel
*chan
, int cpu
,
84 struct lttng_ust_shm_handle
*handle
,
85 int *shm_fd
, int *wait_fd
,
87 uint64_t *memory_map_size
);
89 __attribute__((visibility("hidden")))
91 int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
92 struct lttng_ust_lib_ring_buffer_channel
*chan
,
93 struct lttng_ust_shm_handle
*handle
);
95 __attribute__((visibility("hidden")))
97 int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
98 struct lttng_ust_lib_ring_buffer_channel
*chan
,
99 struct lttng_ust_shm_handle
*handle
);
101 __attribute__((visibility("hidden")))
103 int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
104 struct lttng_ust_lib_ring_buffer_channel
*chan
,
105 struct lttng_ust_shm_handle
*handle
,
108 __attribute__((visibility("hidden")))
110 int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
111 struct lttng_ust_lib_ring_buffer_channel
*chan
,
112 struct lttng_ust_shm_handle
*handle
,
115 __attribute__((visibility("hidden")))
116 extern int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
117 struct lttng_ust_shm_handle
*handle
);
119 __attribute__((visibility("hidden")))
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 __attribute__((visibility("hidden")))
128 void lib_ringbuffer_signal_init(void);
131 * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer.
133 __attribute__((visibility("hidden")))
134 extern int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
135 unsigned long *consumed
,
136 unsigned long *produced
,
137 struct lttng_ust_shm_handle
*handle
);
139 __attribute__((visibility("hidden")))
140 extern int lib_ring_buffer_snapshot_sample_positions(
141 struct lttng_ust_lib_ring_buffer
*buf
,
142 unsigned long *consumed
,
143 unsigned long *produced
,
144 struct lttng_ust_shm_handle
*handle
);
146 __attribute__((visibility("hidden")))
147 extern void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
148 unsigned long consumed_new
,
149 struct lttng_ust_shm_handle
*handle
);
151 __attribute__((visibility("hidden")))
152 extern int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
153 unsigned long consumed
,
154 struct lttng_ust_shm_handle
*handle
);
155 __attribute__((visibility("hidden")))
156 extern void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
157 struct lttng_ust_shm_handle
*handle
);
160 * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers
161 * to read sub-buffers sequentially.
163 static inline int lib_ring_buffer_get_next_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
164 struct lttng_ust_shm_handle
*handle
)
168 ret
= lib_ring_buffer_snapshot(buf
, &buf
->cons_snapshot
,
169 &buf
->prod_snapshot
, handle
);
172 ret
= lib_ring_buffer_get_subbuf(buf
, buf
->cons_snapshot
, handle
);
177 void lib_ring_buffer_put_next_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
178 struct lttng_ust_shm_handle
*handle
)
180 struct lttng_ust_lib_ring_buffer_channel
*chan
;
182 chan
= shmp(handle
, buf
->backend
.chan
);
185 lib_ring_buffer_put_subbuf(buf
, handle
);
186 lib_ring_buffer_move_consumer(buf
, subbuf_align(buf
->cons_snapshot
, chan
),
190 __attribute__((visibility("hidden")))
191 extern void channel_reset(struct lttng_ust_lib_ring_buffer_channel
*chan
);
193 __attribute__((visibility("hidden")))
194 extern void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
195 struct lttng_ust_shm_handle
*handle
);
198 unsigned long lib_ring_buffer_get_offset(const struct lttng_ust_lib_ring_buffer_config
*config
,
199 struct lttng_ust_lib_ring_buffer
*buf
)
201 return v_read(config
, &buf
->offset
);
205 unsigned long lib_ring_buffer_get_consumed(const struct lttng_ust_lib_ring_buffer_config
*config
,
206 struct lttng_ust_lib_ring_buffer
*buf
)
208 return uatomic_read(&buf
->consumed
);
212 * Must call lib_ring_buffer_is_finalized before reading counters (memory
213 * ordering enforced with respect to trace teardown).
216 int lib_ring_buffer_is_finalized(const struct lttng_ust_lib_ring_buffer_config
*config
,
217 struct lttng_ust_lib_ring_buffer
*buf
)
219 int finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
221 * Read finalized before counters.
228 int lib_ring_buffer_channel_is_finalized(const struct lttng_ust_lib_ring_buffer_channel
*chan
)
230 return chan
->finalized
;
234 int lib_ring_buffer_channel_is_disabled(const struct lttng_ust_lib_ring_buffer_channel
*chan
)
236 return uatomic_read(&chan
->record_disabled
);
240 unsigned long lib_ring_buffer_get_read_data_size(
241 const struct lttng_ust_lib_ring_buffer_config
*config
,
242 struct lttng_ust_lib_ring_buffer
*buf
,
243 struct lttng_ust_shm_handle
*handle
)
245 return subbuffer_get_read_data_size(config
, &buf
->backend
, handle
);
249 unsigned long lib_ring_buffer_get_records_count(
250 const struct lttng_ust_lib_ring_buffer_config
*config
,
251 struct lttng_ust_lib_ring_buffer
*buf
)
253 return v_read(config
, &buf
->records_count
);
257 unsigned long lib_ring_buffer_get_records_overrun(
258 const struct lttng_ust_lib_ring_buffer_config
*config
,
259 struct lttng_ust_lib_ring_buffer
*buf
)
261 return v_read(config
, &buf
->records_overrun
);
265 unsigned long lib_ring_buffer_get_records_lost_full(
266 const struct lttng_ust_lib_ring_buffer_config
*config
,
267 struct lttng_ust_lib_ring_buffer
*buf
)
269 return v_read(config
, &buf
->records_lost_full
);
273 unsigned long lib_ring_buffer_get_records_lost_wrap(
274 const struct lttng_ust_lib_ring_buffer_config
*config
,
275 struct lttng_ust_lib_ring_buffer
*buf
)
277 return v_read(config
, &buf
->records_lost_wrap
);
281 unsigned long lib_ring_buffer_get_records_lost_big(
282 const struct lttng_ust_lib_ring_buffer_config
*config
,
283 struct lttng_ust_lib_ring_buffer
*buf
)
285 return v_read(config
, &buf
->records_lost_big
);
289 unsigned long lib_ring_buffer_get_records_read(
290 const struct lttng_ust_lib_ring_buffer_config
*config
,
291 struct lttng_ust_lib_ring_buffer
*buf
)
293 return v_read(config
, &buf
->backend
.records_read
);
296 #endif /* _LTTNG_RING_BUFFER_FRONTEND_H */