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 |
a3f61e7f | 64 | void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle, |
74d81a6c | 65 | int consumer); |
852c2936 MD |
66 | |
67 | ||
68 | /* Buffer read operations */ | |
69 | ||
70 | /* | |
71 | * Iteration on channel cpumask needs to issue a read barrier to match the write | |
72 | * barrier in cpu hotplug. It orders the cpumask read before read of per-cpu | |
73 | * buffer data. The per-cpu buffer is never removed by cpu hotplug; teardown is | |
74 | * only performed at channel destruction. | |
75 | */ | |
76 | #define for_each_channel_cpu(cpu, chan) \ | |
a6352fd4 | 77 | for_each_possible_cpu(cpu) |
852c2936 | 78 | |
ddabe860 | 79 | __attribute__((visibility("hidden"))) |
4cfec15c MD |
80 | extern struct lttng_ust_lib_ring_buffer *channel_get_ring_buffer( |
81 | const struct lttng_ust_lib_ring_buffer_config *config, | |
1d498196 | 82 | struct channel *chan, int cpu, |
38fae1d3 | 83 | struct lttng_ust_shm_handle *handle, |
74d81a6c MD |
84 | int *shm_fd, int *wait_fd, |
85 | int *wakeup_fd, | |
86 | uint64_t *memory_map_size); | |
ddabe860 MJ |
87 | |
88 | __attribute__((visibility("hidden"))) | |
74d81a6c | 89 | extern |
ff0f5728 MD |
90 | int ring_buffer_channel_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config, |
91 | struct channel *chan, | |
92 | struct lttng_ust_shm_handle *handle); | |
ddabe860 MJ |
93 | |
94 | __attribute__((visibility("hidden"))) | |
ff0f5728 MD |
95 | extern |
96 | int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config, | |
97 | struct channel *chan, | |
98 | struct lttng_ust_shm_handle *handle); | |
ddabe860 MJ |
99 | |
100 | __attribute__((visibility("hidden"))) | |
ff0f5728 MD |
101 | extern |
102 | int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config, | |
74d81a6c MD |
103 | struct channel *chan, |
104 | struct lttng_ust_shm_handle *handle, | |
105 | int cpu); | |
ddabe860 MJ |
106 | |
107 | __attribute__((visibility("hidden"))) | |
74d81a6c | 108 | extern |
ff0f5728 | 109 | int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config, |
74d81a6c MD |
110 | struct channel *chan, |
111 | struct lttng_ust_shm_handle *handle, | |
112 | int cpu); | |
113 | ||
ddabe860 | 114 | __attribute__((visibility("hidden"))) |
4cfec15c | 115 | extern int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf, |
74d81a6c | 116 | struct lttng_ust_shm_handle *handle); |
ddabe860 MJ |
117 | |
118 | __attribute__((visibility("hidden"))) | |
4cfec15c | 119 | extern void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf, |
74d81a6c | 120 | struct lttng_ust_shm_handle *handle); |
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 | */ | |
ddabe860 | 126 | __attribute__((visibility("hidden"))) |
03d2d293 MD |
127 | void lib_ringbuffer_signal_init(void); |
128 | ||
852c2936 MD |
129 | /* |
130 | * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer. | |
131 | */ | |
ddabe860 | 132 | __attribute__((visibility("hidden"))) |
4cfec15c | 133 | extern int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 | 134 | unsigned long *consumed, |
1d498196 | 135 | unsigned long *produced, |
38fae1d3 | 136 | struct lttng_ust_shm_handle *handle); |
ddabe860 MJ |
137 | |
138 | __attribute__((visibility("hidden"))) | |
f45930b7 JG |
139 | extern int lib_ring_buffer_snapshot_sample_positions( |
140 | struct lttng_ust_lib_ring_buffer *buf, | |
141 | unsigned long *consumed, | |
142 | unsigned long *produced, | |
143 | struct lttng_ust_shm_handle *handle); | |
ddabe860 MJ |
144 | |
145 | __attribute__((visibility("hidden"))) | |
4cfec15c | 146 | extern void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 147 | unsigned long consumed_new, |
38fae1d3 | 148 | struct lttng_ust_shm_handle *handle); |
852c2936 | 149 | |
ddabe860 | 150 | __attribute__((visibility("hidden"))) |
4cfec15c | 151 | extern int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 152 | unsigned long consumed, |
38fae1d3 | 153 | struct lttng_ust_shm_handle *handle); |
ddabe860 | 154 | __attribute__((visibility("hidden"))) |
4cfec15c | 155 | extern void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 156 | struct lttng_ust_shm_handle *handle); |
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 | */ | |
4cfec15c | 162 | static inline int lib_ring_buffer_get_next_subbuf(struct lttng_ust_lib_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 |
4cfec15c | 176 | void lib_ring_buffer_put_next_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 177 | struct lttng_ust_shm_handle *handle) |
852c2936 | 178 | { |
15500a1b MD |
179 | struct channel *chan; |
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 | ||
ddabe860 | 189 | __attribute__((visibility("hidden"))) |
852c2936 | 190 | extern void channel_reset(struct channel *chan); |
ddabe860 MJ |
191 | |
192 | __attribute__((visibility("hidden"))) | |
4cfec15c | 193 | extern void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 194 | struct lttng_ust_shm_handle *handle); |
852c2936 MD |
195 | |
196 | static inline | |
4cfec15c MD |
197 | unsigned long lib_ring_buffer_get_offset(const struct lttng_ust_lib_ring_buffer_config *config, |
198 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
199 | { |
200 | return v_read(config, &buf->offset); | |
201 | } | |
202 | ||
203 | static inline | |
4cfec15c MD |
204 | unsigned long lib_ring_buffer_get_consumed(const struct lttng_ust_lib_ring_buffer_config *config, |
205 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 | 206 | { |
14641deb | 207 | return uatomic_read(&buf->consumed); |
852c2936 MD |
208 | } |
209 | ||
210 | /* | |
211 | * Must call lib_ring_buffer_is_finalized before reading counters (memory | |
212 | * ordering enforced with respect to trace teardown). | |
213 | */ | |
214 | static inline | |
4cfec15c MD |
215 | int lib_ring_buffer_is_finalized(const struct lttng_ust_lib_ring_buffer_config *config, |
216 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 | 217 | { |
14641deb | 218 | int finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
219 | /* |
220 | * Read finalized before counters. | |
221 | */ | |
14641deb | 222 | cmm_smp_rmb(); |
852c2936 MD |
223 | return finalized; |
224 | } | |
225 | ||
226 | static inline | |
227 | int lib_ring_buffer_channel_is_finalized(const struct channel *chan) | |
228 | { | |
229 | return chan->finalized; | |
230 | } | |
231 | ||
232 | static inline | |
233 | int lib_ring_buffer_channel_is_disabled(const struct channel *chan) | |
234 | { | |
14641deb | 235 | return uatomic_read(&chan->record_disabled); |
852c2936 MD |
236 | } |
237 | ||
238 | static inline | |
239 | unsigned long lib_ring_buffer_get_read_data_size( | |
4cfec15c MD |
240 | const struct lttng_ust_lib_ring_buffer_config *config, |
241 | struct lttng_ust_lib_ring_buffer *buf, | |
38fae1d3 | 242 | struct lttng_ust_shm_handle *handle) |
852c2936 | 243 | { |
1d498196 | 244 | return subbuffer_get_read_data_size(config, &buf->backend, handle); |
852c2936 MD |
245 | } |
246 | ||
247 | static inline | |
248 | unsigned long lib_ring_buffer_get_records_count( | |
4cfec15c MD |
249 | const struct lttng_ust_lib_ring_buffer_config *config, |
250 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
251 | { |
252 | return v_read(config, &buf->records_count); | |
253 | } | |
254 | ||
255 | static inline | |
256 | unsigned long lib_ring_buffer_get_records_overrun( | |
4cfec15c MD |
257 | const struct lttng_ust_lib_ring_buffer_config *config, |
258 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
259 | { |
260 | return v_read(config, &buf->records_overrun); | |
261 | } | |
262 | ||
263 | static inline | |
264 | unsigned long lib_ring_buffer_get_records_lost_full( | |
4cfec15c MD |
265 | const struct lttng_ust_lib_ring_buffer_config *config, |
266 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
267 | { |
268 | return v_read(config, &buf->records_lost_full); | |
269 | } | |
270 | ||
271 | static inline | |
272 | unsigned long lib_ring_buffer_get_records_lost_wrap( | |
4cfec15c MD |
273 | const struct lttng_ust_lib_ring_buffer_config *config, |
274 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
275 | { |
276 | return v_read(config, &buf->records_lost_wrap); | |
277 | } | |
278 | ||
279 | static inline | |
280 | unsigned long lib_ring_buffer_get_records_lost_big( | |
4cfec15c MD |
281 | const struct lttng_ust_lib_ring_buffer_config *config, |
282 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
283 | { |
284 | return v_read(config, &buf->records_lost_big); | |
285 | } | |
286 | ||
287 | static inline | |
288 | unsigned long lib_ring_buffer_get_records_read( | |
4cfec15c MD |
289 | const struct lttng_ust_lib_ring_buffer_config *config, |
290 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
291 | { |
292 | return v_read(config, &buf->backend.records_read); | |
293 | } | |
294 | ||
e92f3e28 | 295 | #endif /* _LTTNG_RING_BUFFER_FRONTEND_H */ |