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 | ||
a6352fd4 | 20 | #include "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 MD |
36 | * |
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 | |
40 | * private data area. | |
852c2936 MD |
41 | */ |
42 | ||
ddabe860 | 43 | __attribute__((visibility("hidden"))) |
852c2936 | 44 | extern |
4cfec15c | 45 | struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config, |
a3f61e7f MD |
46 | const char *name, |
47 | void **priv_data, | |
48 | size_t priv_data_align, | |
49 | size_t priv_data_size, | |
d028eddb | 50 | void *priv_data_init, |
431d5cf0 MD |
51 | void *buf_addr, |
52 | size_t subbuf_size, size_t num_subbuf, | |
53 | unsigned int switch_timer_interval, | |
a9ff648c | 54 | unsigned int read_timer_interval, |
b2c5f61a MD |
55 | const int *stream_fds, int nr_stream_fds, |
56 | int64_t blocking_timeout); | |
852c2936 MD |
57 | |
58 | /* | |
a3f61e7f MD |
59 | * channel_destroy finalizes all channel's buffers, waits for readers to |
60 | * release all references, and destroys the channel. | |
852c2936 | 61 | */ |
ddabe860 | 62 | __attribute__((visibility("hidden"))) |
852c2936 | 63 | extern |
5198080d MJ |
64 | void channel_destroy(struct lttng_ust_lib_ring_buffer_channel *chan, |
65 | struct lttng_ust_shm_handle *handle, | |
74d81a6c | 66 | int consumer); |
852c2936 MD |
67 | |
68 | ||
69 | /* Buffer read operations */ | |
70 | ||
71 | /* | |
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. | |
76 | */ | |
77 | #define for_each_channel_cpu(cpu, chan) \ | |
a6352fd4 | 78 | for_each_possible_cpu(cpu) |
852c2936 | 79 | |
ddabe860 | 80 | __attribute__((visibility("hidden"))) |
4cfec15c MD |
81 | extern struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer( |
82 | const struct lttng_ust_lib_ring_buffer_config *config, | |
5198080d | 83 | struct lttng_ust_lib_ring_buffer_channel *chan, int cpu, |
38fae1d3 | 84 | struct lttng_ust_shm_handle *handle, |
74d81a6c MD |
85 | int *shm_fd, int *wait_fd, |
86 | int *wakeup_fd, | |
87 | uint64_t *memory_map_size); | |
ddabe860 MJ |
88 | |
89 | __attribute__((visibility("hidden"))) | |
74d81a6c | 90 | extern |
ff0f5728 | 91 | int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config, |
5198080d | 92 | struct lttng_ust_lib_ring_buffer_channel *chan, |
ff0f5728 | 93 | struct lttng_ust_shm_handle *handle); |
ddabe860 MJ |
94 | |
95 | __attribute__((visibility("hidden"))) | |
ff0f5728 MD |
96 | extern |
97 | int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config, | |
5198080d | 98 | struct lttng_ust_lib_ring_buffer_channel *chan, |
ff0f5728 | 99 | struct lttng_ust_shm_handle *handle); |
ddabe860 MJ |
100 | |
101 | __attribute__((visibility("hidden"))) | |
ff0f5728 MD |
102 | extern |
103 | int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config, | |
5198080d | 104 | struct lttng_ust_lib_ring_buffer_channel *chan, |
74d81a6c MD |
105 | struct lttng_ust_shm_handle *handle, |
106 | int cpu); | |
ddabe860 MJ |
107 | |
108 | __attribute__((visibility("hidden"))) | |
74d81a6c | 109 | extern |
ff0f5728 | 110 | int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config, |
5198080d | 111 | struct lttng_ust_lib_ring_buffer_channel *chan, |
74d81a6c MD |
112 | struct lttng_ust_shm_handle *handle, |
113 | int cpu); | |
114 | ||
ddabe860 | 115 | __attribute__((visibility("hidden"))) |
4cfec15c | 116 | extern int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf, |
74d81a6c | 117 | struct lttng_ust_shm_handle *handle); |
ddabe860 MJ |
118 | |
119 | __attribute__((visibility("hidden"))) | |
4cfec15c | 120 | extern void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf, |
74d81a6c | 121 | struct lttng_ust_shm_handle *handle); |
852c2936 | 122 | |
03d2d293 MD |
123 | /* |
124 | * Initialize signals for ring buffer. Should be called early e.g. by | |
125 | * main() in the program to affect all threads. | |
126 | */ | |
ddabe860 | 127 | __attribute__((visibility("hidden"))) |
03d2d293 MD |
128 | void lib_ringbuffer_signal_init(void); |
129 | ||
852c2936 MD |
130 | /* |
131 | * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer. | |
132 | */ | |
ddabe860 | 133 | __attribute__((visibility("hidden"))) |
4cfec15c | 134 | extern int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 | 135 | unsigned long *consumed, |
1d498196 | 136 | unsigned long *produced, |
38fae1d3 | 137 | struct lttng_ust_shm_handle *handle); |
ddabe860 MJ |
138 | |
139 | __attribute__((visibility("hidden"))) | |
f45930b7 JG |
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); | |
ddabe860 MJ |
145 | |
146 | __attribute__((visibility("hidden"))) | |
4cfec15c | 147 | extern void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 148 | unsigned long consumed_new, |
38fae1d3 | 149 | struct lttng_ust_shm_handle *handle); |
852c2936 | 150 | |
ddabe860 | 151 | __attribute__((visibility("hidden"))) |
4cfec15c | 152 | extern int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 153 | unsigned long consumed, |
38fae1d3 | 154 | struct lttng_ust_shm_handle *handle); |
ddabe860 | 155 | __attribute__((visibility("hidden"))) |
4cfec15c | 156 | extern void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 157 | struct lttng_ust_shm_handle *handle); |
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 | */ | |
4cfec15c | 163 | static inline int lib_ring_buffer_get_next_subbuf(struct lttng_ust_lib_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 |
4cfec15c | 177 | void lib_ring_buffer_put_next_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 178 | struct lttng_ust_shm_handle *handle) |
852c2936 | 179 | { |
5198080d | 180 | struct lttng_ust_lib_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 | ||
ddabe860 | 190 | __attribute__((visibility("hidden"))) |
5198080d | 191 | extern void channel_reset(struct lttng_ust_lib_ring_buffer_channel *chan); |
ddabe860 MJ |
192 | |
193 | __attribute__((visibility("hidden"))) | |
4cfec15c | 194 | extern void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 195 | struct lttng_ust_shm_handle *handle); |
852c2936 MD |
196 | |
197 | static inline | |
4cfec15c MD |
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) | |
852c2936 MD |
200 | { |
201 | return v_read(config, &buf->offset); | |
202 | } | |
203 | ||
204 | static inline | |
4cfec15c MD |
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) | |
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 | |
4cfec15c MD |
216 | int lib_ring_buffer_is_finalized(const struct lttng_ust_lib_ring_buffer_config *config, |
217 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 | 218 | { |
14641deb | 219 | int finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
220 | /* |
221 | * Read finalized before counters. | |
222 | */ | |
14641deb | 223 | cmm_smp_rmb(); |
852c2936 MD |
224 | return finalized; |
225 | } | |
226 | ||
227 | static inline | |
5198080d | 228 | int lib_ring_buffer_channel_is_finalized(const struct lttng_ust_lib_ring_buffer_channel *chan) |
852c2936 MD |
229 | { |
230 | return chan->finalized; | |
231 | } | |
232 | ||
233 | static inline | |
5198080d | 234 | int lib_ring_buffer_channel_is_disabled(const struct lttng_ust_lib_ring_buffer_channel *chan) |
852c2936 | 235 | { |
14641deb | 236 | return uatomic_read(&chan->record_disabled); |
852c2936 MD |
237 | } |
238 | ||
239 | static inline | |
240 | unsigned long lib_ring_buffer_get_read_data_size( | |
4cfec15c MD |
241 | const struct lttng_ust_lib_ring_buffer_config *config, |
242 | struct lttng_ust_lib_ring_buffer *buf, | |
38fae1d3 | 243 | struct lttng_ust_shm_handle *handle) |
852c2936 | 244 | { |
1d498196 | 245 | return subbuffer_get_read_data_size(config, &buf->backend, handle); |
852c2936 MD |
246 | } |
247 | ||
248 | static inline | |
249 | unsigned long lib_ring_buffer_get_records_count( | |
4cfec15c MD |
250 | const struct lttng_ust_lib_ring_buffer_config *config, |
251 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
252 | { |
253 | return v_read(config, &buf->records_count); | |
254 | } | |
255 | ||
256 | static inline | |
257 | unsigned long lib_ring_buffer_get_records_overrun( | |
4cfec15c MD |
258 | const struct lttng_ust_lib_ring_buffer_config *config, |
259 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
260 | { |
261 | return v_read(config, &buf->records_overrun); | |
262 | } | |
263 | ||
264 | static inline | |
265 | unsigned long lib_ring_buffer_get_records_lost_full( | |
4cfec15c MD |
266 | const struct lttng_ust_lib_ring_buffer_config *config, |
267 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
268 | { |
269 | return v_read(config, &buf->records_lost_full); | |
270 | } | |
271 | ||
272 | static inline | |
273 | unsigned long lib_ring_buffer_get_records_lost_wrap( | |
4cfec15c MD |
274 | const struct lttng_ust_lib_ring_buffer_config *config, |
275 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
276 | { |
277 | return v_read(config, &buf->records_lost_wrap); | |
278 | } | |
279 | ||
280 | static inline | |
281 | unsigned long lib_ring_buffer_get_records_lost_big( | |
4cfec15c MD |
282 | const struct lttng_ust_lib_ring_buffer_config *config, |
283 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
284 | { |
285 | return v_read(config, &buf->records_lost_big); | |
286 | } | |
287 | ||
288 | static inline | |
289 | unsigned long lib_ring_buffer_get_records_read( | |
4cfec15c MD |
290 | const struct lttng_ust_lib_ring_buffer_config *config, |
291 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
292 | { |
293 | return v_read(config, &buf->backend.records_read); | |
294 | } | |
295 | ||
e92f3e28 | 296 | #endif /* _LTTNG_RING_BUFFER_FRONTEND_H */ |