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, | |
1d18d519 MJ |
84 | uint64_t *memory_map_size) |
85 | __attribute__((visibility("hidden"))); | |
ddabe860 | 86 | |
74d81a6c | 87 | extern |
b5457df5 MD |
88 | int ring_buffer_channel_close_wait_fd(const struct lttng_ust_ring_buffer_config *config, |
89 | struct lttng_ust_ring_buffer_channel *chan, | |
1d18d519 MJ |
90 | struct lttng_ust_shm_handle *handle) |
91 | __attribute__((visibility("hidden"))); | |
ddabe860 | 92 | |
ff0f5728 | 93 | extern |
b5457df5 MD |
94 | int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_ring_buffer_config *config, |
95 | struct lttng_ust_ring_buffer_channel *chan, | |
1d18d519 MJ |
96 | struct lttng_ust_shm_handle *handle) |
97 | __attribute__((visibility("hidden"))); | |
ddabe860 | 98 | |
ff0f5728 | 99 | extern |
b5457df5 MD |
100 | int ring_buffer_stream_close_wait_fd(const struct lttng_ust_ring_buffer_config *config, |
101 | struct lttng_ust_ring_buffer_channel *chan, | |
74d81a6c | 102 | struct lttng_ust_shm_handle *handle, |
1d18d519 MJ |
103 | int cpu) |
104 | __attribute__((visibility("hidden"))); | |
ddabe860 | 105 | |
74d81a6c | 106 | extern |
b5457df5 MD |
107 | int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_ring_buffer_config *config, |
108 | struct lttng_ust_ring_buffer_channel *chan, | |
74d81a6c | 109 | struct lttng_ust_shm_handle *handle, |
1d18d519 MJ |
110 | int cpu) |
111 | __attribute__((visibility("hidden"))); | |
74d81a6c | 112 | |
b5457df5 | 113 | extern int lib_ring_buffer_open_read(struct lttng_ust_ring_buffer *buf, |
1d18d519 MJ |
114 | struct lttng_ust_shm_handle *handle) |
115 | __attribute__((visibility("hidden"))); | |
ddabe860 | 116 | |
b5457df5 | 117 | extern void lib_ring_buffer_release_read(struct lttng_ust_ring_buffer *buf, |
1d18d519 MJ |
118 | struct lttng_ust_shm_handle *handle) |
119 | __attribute__((visibility("hidden"))); | |
852c2936 | 120 | |
03d2d293 MD |
121 | /* |
122 | * Initialize signals for ring buffer. Should be called early e.g. by | |
123 | * main() in the program to affect all threads. | |
124 | */ | |
1d18d519 MJ |
125 | void lib_ringbuffer_signal_init(void) |
126 | __attribute__((visibility("hidden"))); | |
03d2d293 | 127 | |
852c2936 MD |
128 | /* |
129 | * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer. | |
130 | */ | |
b5457df5 | 131 | extern int lib_ring_buffer_snapshot(struct lttng_ust_ring_buffer *buf, |
852c2936 | 132 | unsigned long *consumed, |
1d498196 | 133 | unsigned long *produced, |
1d18d519 MJ |
134 | struct lttng_ust_shm_handle *handle) |
135 | __attribute__((visibility("hidden"))); | |
ddabe860 | 136 | |
f45930b7 | 137 | extern int lib_ring_buffer_snapshot_sample_positions( |
b5457df5 | 138 | struct lttng_ust_ring_buffer *buf, |
f45930b7 JG |
139 | unsigned long *consumed, |
140 | unsigned long *produced, | |
1d18d519 MJ |
141 | struct lttng_ust_shm_handle *handle) |
142 | __attribute__((visibility("hidden"))); | |
ddabe860 | 143 | |
b5457df5 | 144 | extern void lib_ring_buffer_move_consumer(struct lttng_ust_ring_buffer *buf, |
1d498196 | 145 | unsigned long consumed_new, |
1d18d519 MJ |
146 | struct lttng_ust_shm_handle *handle) |
147 | __attribute__((visibility("hidden"))); | |
852c2936 | 148 | |
b5457df5 | 149 | extern int lib_ring_buffer_get_subbuf(struct lttng_ust_ring_buffer *buf, |
1d498196 | 150 | unsigned long consumed, |
1d18d519 MJ |
151 | struct lttng_ust_shm_handle *handle) |
152 | __attribute__((visibility("hidden"))); | |
153 | ||
b5457df5 | 154 | extern void lib_ring_buffer_put_subbuf(struct lttng_ust_ring_buffer *buf, |
1d18d519 MJ |
155 | struct lttng_ust_shm_handle *handle) |
156 | __attribute__((visibility("hidden"))); | |
852c2936 MD |
157 | |
158 | /* | |
159 | * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers | |
160 | * to read sub-buffers sequentially. | |
161 | */ | |
b5457df5 | 162 | static inline int lib_ring_buffer_get_next_subbuf(struct lttng_ust_ring_buffer *buf, |
38fae1d3 | 163 | struct lttng_ust_shm_handle *handle) |
852c2936 MD |
164 | { |
165 | int ret; | |
166 | ||
167 | ret = lib_ring_buffer_snapshot(buf, &buf->cons_snapshot, | |
1d498196 | 168 | &buf->prod_snapshot, handle); |
852c2936 MD |
169 | if (ret) |
170 | return ret; | |
1d498196 | 171 | ret = lib_ring_buffer_get_subbuf(buf, buf->cons_snapshot, handle); |
852c2936 MD |
172 | return ret; |
173 | } | |
174 | ||
1d498196 | 175 | static inline |
b5457df5 | 176 | void lib_ring_buffer_put_next_subbuf(struct lttng_ust_ring_buffer *buf, |
38fae1d3 | 177 | struct lttng_ust_shm_handle *handle) |
852c2936 | 178 | { |
b5457df5 | 179 | struct lttng_ust_ring_buffer_channel *chan; |
15500a1b MD |
180 | |
181 | chan = shmp(handle, buf->backend.chan); | |
182 | if (!chan) | |
183 | return; | |
1d498196 | 184 | lib_ring_buffer_put_subbuf(buf, handle); |
15500a1b MD |
185 | lib_ring_buffer_move_consumer(buf, subbuf_align(buf->cons_snapshot, chan), |
186 | handle); | |
852c2936 MD |
187 | } |
188 | ||
b5457df5 | 189 | extern void channel_reset(struct lttng_ust_ring_buffer_channel *chan) |
1d18d519 | 190 | __attribute__((visibility("hidden"))); |
ddabe860 | 191 | |
b5457df5 | 192 | extern void lib_ring_buffer_reset(struct lttng_ust_ring_buffer *buf, |
1d18d519 MJ |
193 | struct lttng_ust_shm_handle *handle) |
194 | __attribute__((visibility("hidden"))); | |
852c2936 MD |
195 | |
196 | static inline | |
b5457df5 MD |
197 | unsigned long lib_ring_buffer_get_offset(const struct lttng_ust_ring_buffer_config *config, |
198 | struct lttng_ust_ring_buffer *buf) | |
852c2936 MD |
199 | { |
200 | return v_read(config, &buf->offset); | |
201 | } | |
202 | ||
203 | static inline | |
2208d8b5 | 204 | unsigned long lib_ring_buffer_get_consumed( |
b5457df5 MD |
205 | const struct lttng_ust_ring_buffer_config *config __attribute__((unused)), |
206 | struct lttng_ust_ring_buffer *buf) | |
852c2936 | 207 | { |
14641deb | 208 | return uatomic_read(&buf->consumed); |
852c2936 MD |
209 | } |
210 | ||
211 | /* | |
212 | * Must call lib_ring_buffer_is_finalized before reading counters (memory | |
213 | * ordering enforced with respect to trace teardown). | |
214 | */ | |
215 | static inline | |
2208d8b5 | 216 | int lib_ring_buffer_is_finalized( |
b5457df5 MD |
217 | const struct lttng_ust_ring_buffer_config *config __attribute__((unused)), |
218 | struct lttng_ust_ring_buffer *buf) | |
852c2936 | 219 | { |
14641deb | 220 | int finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
221 | /* |
222 | * Read finalized before counters. | |
223 | */ | |
14641deb | 224 | cmm_smp_rmb(); |
852c2936 MD |
225 | return finalized; |
226 | } | |
227 | ||
228 | static inline | |
b5457df5 | 229 | int lib_ring_buffer_channel_is_finalized(const struct lttng_ust_ring_buffer_channel *chan) |
852c2936 MD |
230 | { |
231 | return chan->finalized; | |
232 | } | |
233 | ||
234 | static inline | |
b5457df5 | 235 | int lib_ring_buffer_channel_is_disabled(const struct lttng_ust_ring_buffer_channel *chan) |
852c2936 | 236 | { |
14641deb | 237 | return uatomic_read(&chan->record_disabled); |
852c2936 MD |
238 | } |
239 | ||
240 | static inline | |
241 | unsigned long lib_ring_buffer_get_read_data_size( | |
b5457df5 MD |
242 | const struct lttng_ust_ring_buffer_config *config, |
243 | struct lttng_ust_ring_buffer *buf, | |
38fae1d3 | 244 | struct lttng_ust_shm_handle *handle) |
852c2936 | 245 | { |
1d498196 | 246 | return subbuffer_get_read_data_size(config, &buf->backend, handle); |
852c2936 MD |
247 | } |
248 | ||
249 | static inline | |
250 | unsigned long lib_ring_buffer_get_records_count( | |
b5457df5 MD |
251 | const struct lttng_ust_ring_buffer_config *config, |
252 | struct lttng_ust_ring_buffer *buf) | |
852c2936 MD |
253 | { |
254 | return v_read(config, &buf->records_count); | |
255 | } | |
256 | ||
257 | static inline | |
258 | unsigned long lib_ring_buffer_get_records_overrun( | |
b5457df5 MD |
259 | const struct lttng_ust_ring_buffer_config *config, |
260 | struct lttng_ust_ring_buffer *buf) | |
852c2936 MD |
261 | { |
262 | return v_read(config, &buf->records_overrun); | |
263 | } | |
264 | ||
265 | static inline | |
266 | unsigned long lib_ring_buffer_get_records_lost_full( | |
b5457df5 MD |
267 | const struct lttng_ust_ring_buffer_config *config, |
268 | struct lttng_ust_ring_buffer *buf) | |
852c2936 MD |
269 | { |
270 | return v_read(config, &buf->records_lost_full); | |
271 | } | |
272 | ||
273 | static inline | |
274 | unsigned long lib_ring_buffer_get_records_lost_wrap( | |
b5457df5 MD |
275 | const struct lttng_ust_ring_buffer_config *config, |
276 | struct lttng_ust_ring_buffer *buf) | |
852c2936 MD |
277 | { |
278 | return v_read(config, &buf->records_lost_wrap); | |
279 | } | |
280 | ||
281 | static inline | |
282 | unsigned long lib_ring_buffer_get_records_lost_big( | |
b5457df5 MD |
283 | const struct lttng_ust_ring_buffer_config *config, |
284 | struct lttng_ust_ring_buffer *buf) | |
852c2936 MD |
285 | { |
286 | return v_read(config, &buf->records_lost_big); | |
287 | } | |
288 | ||
289 | static inline | |
290 | unsigned long lib_ring_buffer_get_records_read( | |
b5457df5 MD |
291 | const struct lttng_ust_ring_buffer_config *config, |
292 | struct lttng_ust_ring_buffer *buf) | |
852c2936 MD |
293 | { |
294 | return v_read(config, &buf->backend.records_read); | |
295 | } | |
296 | ||
e92f3e28 | 297 | #endif /* _LTTNG_RING_BUFFER_FRONTEND_H */ |