Commit | Line | Data |
---|---|---|
852c2936 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
852c2936 | 3 | * |
e92f3e28 MD |
4 | * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
5 | * | |
c0c0989a | 6 | * Ring Buffer Library Synchronization Header (API). |
852c2936 MD |
7 | * |
8 | * See ring_buffer_frontend.c for more information on wait-free algorithms. | |
852c2936 | 9 | */ |
c0c0989a MJ |
10 | |
11 | #ifndef _LTTNG_RING_BUFFER_FRONTEND_H | |
12 | #define _LTTNG_RING_BUFFER_FRONTEND_H | |
13 | ||
b4051ad8 | 14 | #include <stddef.h> |
fb31eb73 | 15 | #include <stdint.h> |
852c2936 | 16 | |
14641deb MD |
17 | #include <urcu/compiler.h> |
18 | #include <urcu/uatomic.h> | |
19 | ||
74cc1f59 | 20 | #include "common/smp.h" |
071dec43 | 21 | |
852c2936 | 22 | /* Internal helpers */ |
4931a13e | 23 | #include "frontend_internal.h" |
852c2936 MD |
24 | |
25 | /* Buffer creation/removal and setup operations */ | |
26 | ||
27 | /* | |
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. | |
30 | * | |
31 | * read_timer_interval is the time interval (in us) to wake up pending readers. | |
32 | * | |
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. | |
a3f61e7f | 36 | * |
f0fde1c3 MD |
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. | |
852c2936 MD |
40 | */ |
41 | ||
42 | extern | |
b5457df5 | 43 | struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_ring_buffer_config *config, |
a3f61e7f | 44 | const char *name, |
a3f61e7f MD |
45 | size_t priv_data_align, |
46 | size_t priv_data_size, | |
d028eddb | 47 | void *priv_data_init, |
f0fde1c3 | 48 | void *priv, |
431d5cf0 MD |
49 | void *buf_addr, |
50 | size_t subbuf_size, size_t num_subbuf, | |
51 | unsigned int switch_timer_interval, | |
a9ff648c | 52 | unsigned int read_timer_interval, |
b2c5f61a | 53 | const int *stream_fds, int nr_stream_fds, |
1d18d519 MJ |
54 | int64_t blocking_timeout) |
55 | __attribute__((visibility("hidden"))); | |
852c2936 MD |
56 | |
57 | /* | |
a3f61e7f MD |
58 | * channel_destroy finalizes all channel's buffers, waits for readers to |
59 | * release all references, and destroys the channel. | |
852c2936 | 60 | */ |
b5457df5 | 61 | void channel_destroy(struct lttng_ust_ring_buffer_channel *chan, |
5198080d | 62 | struct lttng_ust_shm_handle *handle, |
1d18d519 MJ |
63 | int consumer) |
64 | __attribute__((visibility("hidden"))); | |
852c2936 MD |
65 | |
66 | ||
67 | /* Buffer read operations */ | |
68 | ||
69 | /* | |
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. | |
74 | */ | |
75 | #define for_each_channel_cpu(cpu, chan) \ | |
a6352fd4 | 76 | for_each_possible_cpu(cpu) |
852c2936 | 77 | |
b5457df5 MD |
78 | extern struct lttng_ust_ring_buffer *channel_get_ring_buffer( |
79 | const struct lttng_ust_ring_buffer_config *config, | |
80 | struct lttng_ust_ring_buffer_channel *chan, int cpu, | |
38fae1d3 | 81 | struct lttng_ust_shm_handle *handle, |
74d81a6c MD |
82 | int *shm_fd, int *wait_fd, |
83 | int *wakeup_fd, | |
63b3205f MD |
84 | uint64_t *memory_map_size, |
85 | void **memory_map_addr) | |
1d18d519 | 86 | __attribute__((visibility("hidden"))); |
ddabe860 | 87 | |
74d81a6c | 88 | extern |
b5457df5 MD |
89 | int ring_buffer_channel_close_wait_fd(const struct lttng_ust_ring_buffer_config *config, |
90 | struct lttng_ust_ring_buffer_channel *chan, | |
1d18d519 MJ |
91 | struct lttng_ust_shm_handle *handle) |
92 | __attribute__((visibility("hidden"))); | |
ddabe860 | 93 | |
ff0f5728 | 94 | extern |
b5457df5 MD |
95 | int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_ring_buffer_config *config, |
96 | struct lttng_ust_ring_buffer_channel *chan, | |
1d18d519 MJ |
97 | struct lttng_ust_shm_handle *handle) |
98 | __attribute__((visibility("hidden"))); | |
ddabe860 | 99 | |
ff0f5728 | 100 | extern |
b5457df5 MD |
101 | int ring_buffer_stream_close_wait_fd(const struct lttng_ust_ring_buffer_config *config, |
102 | struct lttng_ust_ring_buffer_channel *chan, | |
74d81a6c | 103 | struct lttng_ust_shm_handle *handle, |
1d18d519 MJ |
104 | int cpu) |
105 | __attribute__((visibility("hidden"))); | |
ddabe860 | 106 | |
74d81a6c | 107 | extern |
b5457df5 MD |
108 | int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_ring_buffer_config *config, |
109 | struct lttng_ust_ring_buffer_channel *chan, | |
74d81a6c | 110 | struct lttng_ust_shm_handle *handle, |
1d18d519 MJ |
111 | int cpu) |
112 | __attribute__((visibility("hidden"))); | |
74d81a6c | 113 | |
b5457df5 | 114 | extern int lib_ring_buffer_open_read(struct lttng_ust_ring_buffer *buf, |
1d18d519 MJ |
115 | struct lttng_ust_shm_handle *handle) |
116 | __attribute__((visibility("hidden"))); | |
ddabe860 | 117 | |
b5457df5 | 118 | extern void lib_ring_buffer_release_read(struct lttng_ust_ring_buffer *buf, |
1d18d519 MJ |
119 | struct lttng_ust_shm_handle *handle) |
120 | __attribute__((visibility("hidden"))); | |
852c2936 | 121 | |
03d2d293 MD |
122 | /* |
123 | * Initialize signals for ring buffer. Should be called early e.g. by | |
124 | * main() in the program to affect all threads. | |
125 | */ | |
1d18d519 MJ |
126 | void lib_ringbuffer_signal_init(void) |
127 | __attribute__((visibility("hidden"))); | |
03d2d293 | 128 | |
852c2936 MD |
129 | /* |
130 | * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer. | |
131 | */ | |
b5457df5 | 132 | extern int lib_ring_buffer_snapshot(struct lttng_ust_ring_buffer *buf, |
852c2936 | 133 | unsigned long *consumed, |
1d498196 | 134 | unsigned long *produced, |
1d18d519 MJ |
135 | struct lttng_ust_shm_handle *handle) |
136 | __attribute__((visibility("hidden"))); | |
ddabe860 | 137 | |
f45930b7 | 138 | extern int lib_ring_buffer_snapshot_sample_positions( |
b5457df5 | 139 | struct lttng_ust_ring_buffer *buf, |
f45930b7 JG |
140 | unsigned long *consumed, |
141 | unsigned long *produced, | |
1d18d519 MJ |
142 | struct lttng_ust_shm_handle *handle) |
143 | __attribute__((visibility("hidden"))); | |
ddabe860 | 144 | |
b5457df5 | 145 | extern void lib_ring_buffer_move_consumer(struct lttng_ust_ring_buffer *buf, |
1d498196 | 146 | unsigned long consumed_new, |
1d18d519 MJ |
147 | struct lttng_ust_shm_handle *handle) |
148 | __attribute__((visibility("hidden"))); | |
852c2936 | 149 | |
b5457df5 | 150 | extern int lib_ring_buffer_get_subbuf(struct lttng_ust_ring_buffer *buf, |
1d498196 | 151 | unsigned long consumed, |
1d18d519 MJ |
152 | struct lttng_ust_shm_handle *handle) |
153 | __attribute__((visibility("hidden"))); | |
154 | ||
b5457df5 | 155 | extern void lib_ring_buffer_put_subbuf(struct lttng_ust_ring_buffer *buf, |
1d18d519 MJ |
156 | struct lttng_ust_shm_handle *handle) |
157 | __attribute__((visibility("hidden"))); | |
852c2936 MD |
158 | |
159 | /* | |
160 | * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers | |
161 | * to read sub-buffers sequentially. | |
162 | */ | |
b5457df5 | 163 | static inline int lib_ring_buffer_get_next_subbuf(struct lttng_ust_ring_buffer *buf, |
38fae1d3 | 164 | struct lttng_ust_shm_handle *handle) |
852c2936 MD |
165 | { |
166 | int ret; | |
167 | ||
168 | ret = lib_ring_buffer_snapshot(buf, &buf->cons_snapshot, | |
1d498196 | 169 | &buf->prod_snapshot, handle); |
852c2936 MD |
170 | if (ret) |
171 | return ret; | |
1d498196 | 172 | ret = lib_ring_buffer_get_subbuf(buf, buf->cons_snapshot, handle); |
852c2936 MD |
173 | return ret; |
174 | } | |
175 | ||
1d498196 | 176 | static inline |
b5457df5 | 177 | void lib_ring_buffer_put_next_subbuf(struct lttng_ust_ring_buffer *buf, |
38fae1d3 | 178 | struct lttng_ust_shm_handle *handle) |
852c2936 | 179 | { |
b5457df5 | 180 | struct lttng_ust_ring_buffer_channel *chan; |
15500a1b MD |
181 | |
182 | chan = shmp(handle, buf->backend.chan); | |
183 | if (!chan) | |
184 | return; | |
1d498196 | 185 | lib_ring_buffer_put_subbuf(buf, handle); |
15500a1b MD |
186 | lib_ring_buffer_move_consumer(buf, subbuf_align(buf->cons_snapshot, chan), |
187 | handle); | |
852c2936 MD |
188 | } |
189 | ||
b5457df5 | 190 | extern void channel_reset(struct lttng_ust_ring_buffer_channel *chan) |
1d18d519 | 191 | __attribute__((visibility("hidden"))); |
ddabe860 | 192 | |
b5457df5 | 193 | extern void lib_ring_buffer_reset(struct lttng_ust_ring_buffer *buf, |
1d18d519 MJ |
194 | struct lttng_ust_shm_handle *handle) |
195 | __attribute__((visibility("hidden"))); | |
852c2936 MD |
196 | |
197 | static inline | |
b5457df5 MD |
198 | unsigned long lib_ring_buffer_get_offset(const struct lttng_ust_ring_buffer_config *config, |
199 | struct lttng_ust_ring_buffer *buf) | |
852c2936 MD |
200 | { |
201 | return v_read(config, &buf->offset); | |
202 | } | |
203 | ||
204 | static inline | |
2208d8b5 | 205 | unsigned long lib_ring_buffer_get_consumed( |
b5457df5 MD |
206 | const struct lttng_ust_ring_buffer_config *config __attribute__((unused)), |
207 | struct lttng_ust_ring_buffer *buf) | |
852c2936 | 208 | { |
14641deb | 209 | return uatomic_read(&buf->consumed); |
852c2936 MD |
210 | } |
211 | ||
212 | /* | |
213 | * Must call lib_ring_buffer_is_finalized before reading counters (memory | |
214 | * ordering enforced with respect to trace teardown). | |
215 | */ | |
216 | static inline | |
2208d8b5 | 217 | int lib_ring_buffer_is_finalized( |
b5457df5 MD |
218 | const struct lttng_ust_ring_buffer_config *config __attribute__((unused)), |
219 | struct lttng_ust_ring_buffer *buf) | |
852c2936 | 220 | { |
14641deb | 221 | int finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
222 | /* |
223 | * Read finalized before counters. | |
224 | */ | |
14641deb | 225 | cmm_smp_rmb(); |
852c2936 MD |
226 | return finalized; |
227 | } | |
228 | ||
229 | static inline | |
b5457df5 | 230 | int lib_ring_buffer_channel_is_finalized(const struct lttng_ust_ring_buffer_channel *chan) |
852c2936 MD |
231 | { |
232 | return chan->finalized; | |
233 | } | |
234 | ||
235 | static inline | |
b5457df5 | 236 | int lib_ring_buffer_channel_is_disabled(const struct lttng_ust_ring_buffer_channel *chan) |
852c2936 | 237 | { |
14641deb | 238 | return uatomic_read(&chan->record_disabled); |
852c2936 MD |
239 | } |
240 | ||
241 | static inline | |
242 | unsigned long lib_ring_buffer_get_read_data_size( | |
b5457df5 MD |
243 | const struct lttng_ust_ring_buffer_config *config, |
244 | struct lttng_ust_ring_buffer *buf, | |
38fae1d3 | 245 | struct lttng_ust_shm_handle *handle) |
852c2936 | 246 | { |
1d498196 | 247 | return subbuffer_get_read_data_size(config, &buf->backend, handle); |
852c2936 MD |
248 | } |
249 | ||
250 | static inline | |
251 | unsigned long lib_ring_buffer_get_records_count( | |
b5457df5 MD |
252 | const struct lttng_ust_ring_buffer_config *config, |
253 | struct lttng_ust_ring_buffer *buf) | |
852c2936 MD |
254 | { |
255 | return v_read(config, &buf->records_count); | |
256 | } | |
257 | ||
258 | static inline | |
259 | unsigned long lib_ring_buffer_get_records_overrun( | |
b5457df5 MD |
260 | const struct lttng_ust_ring_buffer_config *config, |
261 | struct lttng_ust_ring_buffer *buf) | |
852c2936 MD |
262 | { |
263 | return v_read(config, &buf->records_overrun); | |
264 | } | |
265 | ||
266 | static inline | |
267 | unsigned long lib_ring_buffer_get_records_lost_full( | |
f27b0770 MD |
268 | const struct lttng_ust_ring_buffer_config *config __attribute__((unused)), |
269 | const struct lttng_ust_ring_buffer_ctx *ctx) | |
852c2936 | 270 | { |
f27b0770 | 271 | return ctx->priv->records_lost_full; |
852c2936 MD |
272 | } |
273 | ||
274 | static inline | |
275 | unsigned long lib_ring_buffer_get_records_lost_wrap( | |
f27b0770 MD |
276 | const struct lttng_ust_ring_buffer_config *config __attribute__((unused)), |
277 | const struct lttng_ust_ring_buffer_ctx *ctx) | |
852c2936 | 278 | { |
f27b0770 | 279 | return ctx->priv->records_lost_wrap; |
852c2936 MD |
280 | } |
281 | ||
282 | static inline | |
283 | unsigned long lib_ring_buffer_get_records_lost_big( | |
f27b0770 MD |
284 | const struct lttng_ust_ring_buffer_config *config __attribute__((unused)), |
285 | const struct lttng_ust_ring_buffer_ctx *ctx) | |
852c2936 | 286 | { |
f27b0770 | 287 | return ctx->priv->records_lost_big; |
852c2936 MD |
288 | } |
289 | ||
290 | static inline | |
291 | unsigned long lib_ring_buffer_get_records_read( | |
b5457df5 MD |
292 | const struct lttng_ust_ring_buffer_config *config, |
293 | struct lttng_ust_ring_buffer *buf) | |
852c2936 MD |
294 | { |
295 | return v_read(config, &buf->backend.records_read); | |
296 | } | |
297 | ||
e92f3e28 | 298 | #endif /* _LTTNG_RING_BUFFER_FRONTEND_H */ |