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 | 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 | ||
ddabe860 | 42 | __attribute__((visibility("hidden"))) |
852c2936 | 43 | extern |
4cfec15c | 44 | struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config, |
a3f61e7f | 45 | const char *name, |
a3f61e7f MD |
46 | size_t priv_data_align, |
47 | size_t priv_data_size, | |
d028eddb | 48 | void *priv_data_init, |
f0fde1c3 | 49 | void *priv, |
431d5cf0 MD |
50 | void *buf_addr, |
51 | size_t subbuf_size, size_t num_subbuf, | |
52 | unsigned int switch_timer_interval, | |
a9ff648c | 53 | unsigned int read_timer_interval, |
b2c5f61a MD |
54 | const int *stream_fds, int nr_stream_fds, |
55 | int64_t blocking_timeout); | |
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 | */ |
ddabe860 | 61 | __attribute__((visibility("hidden"))) |
5198080d MJ |
62 | void channel_destroy(struct lttng_ust_lib_ring_buffer_channel *chan, |
63 | struct lttng_ust_shm_handle *handle, | |
74d81a6c | 64 | int consumer); |
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 | |
ddabe860 | 78 | __attribute__((visibility("hidden"))) |
4cfec15c MD |
79 | extern struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer( |
80 | const struct lttng_ust_lib_ring_buffer_config *config, | |
5198080d | 81 | struct lttng_ust_lib_ring_buffer_channel *chan, int cpu, |
38fae1d3 | 82 | struct lttng_ust_shm_handle *handle, |
74d81a6c MD |
83 | int *shm_fd, int *wait_fd, |
84 | int *wakeup_fd, | |
85 | uint64_t *memory_map_size); | |
ddabe860 MJ |
86 | |
87 | __attribute__((visibility("hidden"))) | |
74d81a6c | 88 | extern |
ff0f5728 | 89 | int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config, |
5198080d | 90 | struct lttng_ust_lib_ring_buffer_channel *chan, |
ff0f5728 | 91 | struct lttng_ust_shm_handle *handle); |
ddabe860 MJ |
92 | |
93 | __attribute__((visibility("hidden"))) | |
ff0f5728 MD |
94 | extern |
95 | int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config, | |
5198080d | 96 | struct lttng_ust_lib_ring_buffer_channel *chan, |
ff0f5728 | 97 | struct lttng_ust_shm_handle *handle); |
ddabe860 MJ |
98 | |
99 | __attribute__((visibility("hidden"))) | |
ff0f5728 MD |
100 | extern |
101 | int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config, | |
5198080d | 102 | struct lttng_ust_lib_ring_buffer_channel *chan, |
74d81a6c MD |
103 | struct lttng_ust_shm_handle *handle, |
104 | int cpu); | |
ddabe860 MJ |
105 | |
106 | __attribute__((visibility("hidden"))) | |
74d81a6c | 107 | extern |
ff0f5728 | 108 | int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config, |
5198080d | 109 | struct lttng_ust_lib_ring_buffer_channel *chan, |
74d81a6c MD |
110 | struct lttng_ust_shm_handle *handle, |
111 | int cpu); | |
112 | ||
ddabe860 | 113 | __attribute__((visibility("hidden"))) |
4cfec15c | 114 | extern int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf, |
74d81a6c | 115 | struct lttng_ust_shm_handle *handle); |
ddabe860 MJ |
116 | |
117 | __attribute__((visibility("hidden"))) | |
4cfec15c | 118 | extern void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf, |
74d81a6c | 119 | struct lttng_ust_shm_handle *handle); |
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 | */ | |
ddabe860 | 125 | __attribute__((visibility("hidden"))) |
03d2d293 MD |
126 | void lib_ringbuffer_signal_init(void); |
127 | ||
852c2936 MD |
128 | /* |
129 | * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer. | |
130 | */ | |
ddabe860 | 131 | __attribute__((visibility("hidden"))) |
4cfec15c | 132 | extern int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 | 133 | unsigned long *consumed, |
1d498196 | 134 | unsigned long *produced, |
38fae1d3 | 135 | struct lttng_ust_shm_handle *handle); |
ddabe860 MJ |
136 | |
137 | __attribute__((visibility("hidden"))) | |
f45930b7 JG |
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); | |
ddabe860 MJ |
143 | |
144 | __attribute__((visibility("hidden"))) | |
4cfec15c | 145 | extern void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 146 | unsigned long consumed_new, |
38fae1d3 | 147 | struct lttng_ust_shm_handle *handle); |
852c2936 | 148 | |
ddabe860 | 149 | __attribute__((visibility("hidden"))) |
4cfec15c | 150 | extern int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 151 | unsigned long consumed, |
38fae1d3 | 152 | struct lttng_ust_shm_handle *handle); |
ddabe860 | 153 | __attribute__((visibility("hidden"))) |
4cfec15c | 154 | extern void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 155 | struct lttng_ust_shm_handle *handle); |
852c2936 MD |
156 | |
157 | /* | |
158 | * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers | |
159 | * to read sub-buffers sequentially. | |
160 | */ | |
4cfec15c | 161 | static inline int lib_ring_buffer_get_next_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 162 | struct lttng_ust_shm_handle *handle) |
852c2936 MD |
163 | { |
164 | int ret; | |
165 | ||
166 | ret = lib_ring_buffer_snapshot(buf, &buf->cons_snapshot, | |
1d498196 | 167 | &buf->prod_snapshot, handle); |
852c2936 MD |
168 | if (ret) |
169 | return ret; | |
1d498196 | 170 | ret = lib_ring_buffer_get_subbuf(buf, buf->cons_snapshot, handle); |
852c2936 MD |
171 | return ret; |
172 | } | |
173 | ||
1d498196 | 174 | static inline |
4cfec15c | 175 | void lib_ring_buffer_put_next_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 176 | struct lttng_ust_shm_handle *handle) |
852c2936 | 177 | { |
5198080d | 178 | struct lttng_ust_lib_ring_buffer_channel *chan; |
15500a1b MD |
179 | |
180 | chan = shmp(handle, buf->backend.chan); | |
181 | if (!chan) | |
182 | return; | |
1d498196 | 183 | lib_ring_buffer_put_subbuf(buf, handle); |
15500a1b MD |
184 | lib_ring_buffer_move_consumer(buf, subbuf_align(buf->cons_snapshot, chan), |
185 | handle); | |
852c2936 MD |
186 | } |
187 | ||
ddabe860 | 188 | __attribute__((visibility("hidden"))) |
5198080d | 189 | extern void channel_reset(struct lttng_ust_lib_ring_buffer_channel *chan); |
ddabe860 MJ |
190 | |
191 | __attribute__((visibility("hidden"))) | |
4cfec15c | 192 | extern void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 193 | struct lttng_ust_shm_handle *handle); |
852c2936 MD |
194 | |
195 | static inline | |
4cfec15c MD |
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) | |
852c2936 MD |
198 | { |
199 | return v_read(config, &buf->offset); | |
200 | } | |
201 | ||
202 | static inline | |
4cfec15c MD |
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) | |
852c2936 | 205 | { |
14641deb | 206 | return uatomic_read(&buf->consumed); |
852c2936 MD |
207 | } |
208 | ||
209 | /* | |
210 | * Must call lib_ring_buffer_is_finalized before reading counters (memory | |
211 | * ordering enforced with respect to trace teardown). | |
212 | */ | |
213 | static inline | |
4cfec15c MD |
214 | int lib_ring_buffer_is_finalized(const struct lttng_ust_lib_ring_buffer_config *config, |
215 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 | 216 | { |
14641deb | 217 | int finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
218 | /* |
219 | * Read finalized before counters. | |
220 | */ | |
14641deb | 221 | cmm_smp_rmb(); |
852c2936 MD |
222 | return finalized; |
223 | } | |
224 | ||
225 | static inline | |
5198080d | 226 | int lib_ring_buffer_channel_is_finalized(const struct lttng_ust_lib_ring_buffer_channel *chan) |
852c2936 MD |
227 | { |
228 | return chan->finalized; | |
229 | } | |
230 | ||
231 | static inline | |
5198080d | 232 | int lib_ring_buffer_channel_is_disabled(const struct lttng_ust_lib_ring_buffer_channel *chan) |
852c2936 | 233 | { |
14641deb | 234 | return uatomic_read(&chan->record_disabled); |
852c2936 MD |
235 | } |
236 | ||
237 | static inline | |
238 | unsigned long lib_ring_buffer_get_read_data_size( | |
4cfec15c MD |
239 | const struct lttng_ust_lib_ring_buffer_config *config, |
240 | struct lttng_ust_lib_ring_buffer *buf, | |
38fae1d3 | 241 | struct lttng_ust_shm_handle *handle) |
852c2936 | 242 | { |
1d498196 | 243 | return subbuffer_get_read_data_size(config, &buf->backend, handle); |
852c2936 MD |
244 | } |
245 | ||
246 | static inline | |
247 | unsigned long lib_ring_buffer_get_records_count( | |
4cfec15c MD |
248 | const struct lttng_ust_lib_ring_buffer_config *config, |
249 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
250 | { |
251 | return v_read(config, &buf->records_count); | |
252 | } | |
253 | ||
254 | static inline | |
255 | unsigned long lib_ring_buffer_get_records_overrun( | |
4cfec15c MD |
256 | const struct lttng_ust_lib_ring_buffer_config *config, |
257 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
258 | { |
259 | return v_read(config, &buf->records_overrun); | |
260 | } | |
261 | ||
262 | static inline | |
263 | unsigned long lib_ring_buffer_get_records_lost_full( | |
4cfec15c MD |
264 | const struct lttng_ust_lib_ring_buffer_config *config, |
265 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
266 | { |
267 | return v_read(config, &buf->records_lost_full); | |
268 | } | |
269 | ||
270 | static inline | |
271 | unsigned long lib_ring_buffer_get_records_lost_wrap( | |
4cfec15c MD |
272 | const struct lttng_ust_lib_ring_buffer_config *config, |
273 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
274 | { |
275 | return v_read(config, &buf->records_lost_wrap); | |
276 | } | |
277 | ||
278 | static inline | |
279 | unsigned long lib_ring_buffer_get_records_lost_big( | |
4cfec15c MD |
280 | const struct lttng_ust_lib_ring_buffer_config *config, |
281 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
282 | { |
283 | return v_read(config, &buf->records_lost_big); | |
284 | } | |
285 | ||
286 | static inline | |
287 | unsigned long lib_ring_buffer_get_records_read( | |
4cfec15c MD |
288 | const struct lttng_ust_lib_ring_buffer_config *config, |
289 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
290 | { |
291 | return v_read(config, &buf->backend.records_read); | |
292 | } | |
293 | ||
e92f3e28 | 294 | #endif /* _LTTNG_RING_BUFFER_FRONTEND_H */ |