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 MJ |
21 | #include "ust-helper.h" |
22 | ||
852c2936 | 23 | /* Internal helpers */ |
4931a13e | 24 | #include "frontend_internal.h" |
852c2936 MD |
25 | |
26 | /* Buffer creation/removal and setup operations */ | |
27 | ||
28 | /* | |
29 | * switch_timer_interval is the time interval (in us) to fill sub-buffers with | |
30 | * padding to let readers get those sub-buffers. Used for live streaming. | |
31 | * | |
32 | * read_timer_interval is the time interval (in us) to wake up pending readers. | |
33 | * | |
34 | * buf_addr is a pointer the the beginning of the preallocated buffer contiguous | |
35 | * address mapping. It is used only by RING_BUFFER_STATIC configuration. It can | |
36 | * be set to NULL for other backends. | |
a3f61e7f MD |
37 | * |
38 | * priv_data (output) is set to a pointer into a "priv_data_len"-sized | |
39 | * memory area for client-specific data. This memory is managed by lib | |
40 | * ring buffer. priv_data_align is the alignment required for the | |
41 | * private data area. | |
852c2936 MD |
42 | */ |
43 | ||
071dec43 | 44 | LTTNG_HIDDEN |
852c2936 | 45 | extern |
4cfec15c | 46 | struct lttng_ust_shm_handle *channel_create(const struct lttng_ust_lib_ring_buffer_config *config, |
a3f61e7f MD |
47 | const char *name, |
48 | void **priv_data, | |
49 | size_t priv_data_align, | |
50 | size_t priv_data_size, | |
d028eddb | 51 | void *priv_data_init, |
431d5cf0 MD |
52 | void *buf_addr, |
53 | size_t subbuf_size, size_t num_subbuf, | |
54 | unsigned int switch_timer_interval, | |
a9ff648c | 55 | unsigned int read_timer_interval, |
b2c5f61a MD |
56 | const int *stream_fds, int nr_stream_fds, |
57 | int64_t blocking_timeout); | |
852c2936 MD |
58 | |
59 | /* | |
a3f61e7f MD |
60 | * channel_destroy finalizes all channel's buffers, waits for readers to |
61 | * release all references, and destroys the channel. | |
852c2936 | 62 | */ |
071dec43 | 63 | LTTNG_HIDDEN |
852c2936 | 64 | extern |
a3f61e7f | 65 | void channel_destroy(struct channel *chan, 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 | |
071dec43 | 80 | LTTNG_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, | |
1d498196 | 83 | struct 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); | |
071dec43 | 88 | LTTNG_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); | |
071dec43 | 93 | LTTNG_HIDDEN |
ff0f5728 MD |
94 | extern |
95 | int ring_buffer_channel_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config, | |
96 | struct channel *chan, | |
97 | struct lttng_ust_shm_handle *handle); | |
071dec43 | 98 | LTTNG_HIDDEN |
ff0f5728 MD |
99 | extern |
100 | int ring_buffer_stream_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config *config, | |
74d81a6c MD |
101 | struct channel *chan, |
102 | struct lttng_ust_shm_handle *handle, | |
103 | int cpu); | |
071dec43 | 104 | LTTNG_HIDDEN |
74d81a6c | 105 | extern |
ff0f5728 | 106 | int ring_buffer_stream_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config *config, |
74d81a6c MD |
107 | struct channel *chan, |
108 | struct lttng_ust_shm_handle *handle, | |
109 | int cpu); | |
110 | ||
071dec43 | 111 | LTTNG_HIDDEN |
4cfec15c | 112 | extern int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer *buf, |
74d81a6c | 113 | struct lttng_ust_shm_handle *handle); |
071dec43 | 114 | LTTNG_HIDDEN |
4cfec15c | 115 | extern void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer *buf, |
74d81a6c | 116 | struct lttng_ust_shm_handle *handle); |
852c2936 | 117 | |
03d2d293 MD |
118 | /* |
119 | * Initialize signals for ring buffer. Should be called early e.g. by | |
120 | * main() in the program to affect all threads. | |
121 | */ | |
071dec43 | 122 | LTTNG_HIDDEN |
03d2d293 MD |
123 | void lib_ringbuffer_signal_init(void); |
124 | ||
852c2936 MD |
125 | /* |
126 | * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer. | |
127 | */ | |
071dec43 | 128 | LTTNG_HIDDEN |
4cfec15c | 129 | extern int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 | 130 | unsigned long *consumed, |
1d498196 | 131 | unsigned long *produced, |
38fae1d3 | 132 | struct lttng_ust_shm_handle *handle); |
071dec43 | 133 | LTTNG_HIDDEN |
f45930b7 JG |
134 | extern int lib_ring_buffer_snapshot_sample_positions( |
135 | struct lttng_ust_lib_ring_buffer *buf, | |
136 | unsigned long *consumed, | |
137 | unsigned long *produced, | |
138 | struct lttng_ust_shm_handle *handle); | |
071dec43 | 139 | LTTNG_HIDDEN |
4cfec15c | 140 | extern void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 141 | unsigned long consumed_new, |
38fae1d3 | 142 | struct lttng_ust_shm_handle *handle); |
852c2936 | 143 | |
071dec43 | 144 | LTTNG_HIDDEN |
4cfec15c | 145 | extern int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 146 | unsigned long consumed, |
38fae1d3 | 147 | struct lttng_ust_shm_handle *handle); |
071dec43 | 148 | LTTNG_HIDDEN |
4cfec15c | 149 | extern void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 150 | struct lttng_ust_shm_handle *handle); |
852c2936 MD |
151 | |
152 | /* | |
153 | * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers | |
154 | * to read sub-buffers sequentially. | |
155 | */ | |
4cfec15c | 156 | static inline int lib_ring_buffer_get_next_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 157 | struct lttng_ust_shm_handle *handle) |
852c2936 MD |
158 | { |
159 | int ret; | |
160 | ||
161 | ret = lib_ring_buffer_snapshot(buf, &buf->cons_snapshot, | |
1d498196 | 162 | &buf->prod_snapshot, handle); |
852c2936 MD |
163 | if (ret) |
164 | return ret; | |
1d498196 | 165 | ret = lib_ring_buffer_get_subbuf(buf, buf->cons_snapshot, handle); |
852c2936 MD |
166 | return ret; |
167 | } | |
168 | ||
1d498196 | 169 | static inline |
4cfec15c | 170 | void lib_ring_buffer_put_next_subbuf(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 171 | struct lttng_ust_shm_handle *handle) |
852c2936 | 172 | { |
15500a1b MD |
173 | struct channel *chan; |
174 | ||
175 | chan = shmp(handle, buf->backend.chan); | |
176 | if (!chan) | |
177 | return; | |
1d498196 | 178 | lib_ring_buffer_put_subbuf(buf, handle); |
15500a1b MD |
179 | lib_ring_buffer_move_consumer(buf, subbuf_align(buf->cons_snapshot, chan), |
180 | handle); | |
852c2936 MD |
181 | } |
182 | ||
071dec43 | 183 | LTTNG_HIDDEN |
852c2936 | 184 | extern void channel_reset(struct channel *chan); |
071dec43 | 185 | LTTNG_HIDDEN |
4cfec15c | 186 | extern void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 187 | struct lttng_ust_shm_handle *handle); |
852c2936 MD |
188 | |
189 | static inline | |
4cfec15c MD |
190 | unsigned long lib_ring_buffer_get_offset(const struct lttng_ust_lib_ring_buffer_config *config, |
191 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
192 | { |
193 | return v_read(config, &buf->offset); | |
194 | } | |
195 | ||
196 | static inline | |
4cfec15c MD |
197 | unsigned long lib_ring_buffer_get_consumed(const struct lttng_ust_lib_ring_buffer_config *config, |
198 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 | 199 | { |
14641deb | 200 | return uatomic_read(&buf->consumed); |
852c2936 MD |
201 | } |
202 | ||
203 | /* | |
204 | * Must call lib_ring_buffer_is_finalized before reading counters (memory | |
205 | * ordering enforced with respect to trace teardown). | |
206 | */ | |
207 | static inline | |
4cfec15c MD |
208 | int lib_ring_buffer_is_finalized(const struct lttng_ust_lib_ring_buffer_config *config, |
209 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 | 210 | { |
14641deb | 211 | int finalized = CMM_ACCESS_ONCE(buf->finalized); |
852c2936 MD |
212 | /* |
213 | * Read finalized before counters. | |
214 | */ | |
14641deb | 215 | cmm_smp_rmb(); |
852c2936 MD |
216 | return finalized; |
217 | } | |
218 | ||
219 | static inline | |
220 | int lib_ring_buffer_channel_is_finalized(const struct channel *chan) | |
221 | { | |
222 | return chan->finalized; | |
223 | } | |
224 | ||
225 | static inline | |
226 | int lib_ring_buffer_channel_is_disabled(const struct channel *chan) | |
227 | { | |
14641deb | 228 | return uatomic_read(&chan->record_disabled); |
852c2936 MD |
229 | } |
230 | ||
231 | static inline | |
232 | unsigned long lib_ring_buffer_get_read_data_size( | |
4cfec15c MD |
233 | const struct lttng_ust_lib_ring_buffer_config *config, |
234 | struct lttng_ust_lib_ring_buffer *buf, | |
38fae1d3 | 235 | struct lttng_ust_shm_handle *handle) |
852c2936 | 236 | { |
1d498196 | 237 | return subbuffer_get_read_data_size(config, &buf->backend, handle); |
852c2936 MD |
238 | } |
239 | ||
240 | static inline | |
241 | unsigned long lib_ring_buffer_get_records_count( | |
4cfec15c MD |
242 | const struct lttng_ust_lib_ring_buffer_config *config, |
243 | struct lttng_ust_lib_ring_buffer *buf) | |
852c2936 MD |
244 | { |
245 | return v_read(config, &buf->records_count); | |
246 | } | |
247 | ||
248 | static inline | |
249 | unsigned long lib_ring_buffer_get_records_overrun( | |
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_overrun); | |
254 | } | |
255 | ||
256 | static inline | |
257 | unsigned long lib_ring_buffer_get_records_lost_full( | |
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_lost_full); | |
262 | } | |
263 | ||
264 | static inline | |
265 | unsigned long lib_ring_buffer_get_records_lost_wrap( | |
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_wrap); | |
270 | } | |
271 | ||
272 | static inline | |
273 | unsigned long lib_ring_buffer_get_records_lost_big( | |
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_big); | |
278 | } | |
279 | ||
280 | static inline | |
281 | unsigned long lib_ring_buffer_get_records_read( | |
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->backend.records_read); | |
286 | } | |
287 | ||
e92f3e28 | 288 | #endif /* _LTTNG_RING_BUFFER_FRONTEND_H */ |