Commit | Line | Data |
---|---|---|
852c2936 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: (LGPL-2.1-only or GPL-2.0-only) |
852c2936 | 3 | * |
e92f3e28 MD |
4 | * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
5 | * | |
c0c0989a | 6 | * Ring Buffer Library Synchronization Header (internal helpers). |
852c2936 MD |
7 | * |
8 | * See ring_buffer_frontend.c for more information on wait-free algorithms. | |
852c2936 MD |
9 | */ |
10 | ||
c0c0989a MJ |
11 | #ifndef _LTTNG_RING_BUFFER_FRONTEND_INTERNAL_H |
12 | #define _LTTNG_RING_BUFFER_FRONTEND_INTERNAL_H | |
13 | ||
14641deb | 14 | #include <urcu/compiler.h> |
8c90a710 | 15 | #include <urcu/tls-compat.h> |
2c44f5b9 | 16 | #include <signal.h> |
fb31eb73 | 17 | #include <stdint.h> |
2c44f5b9 | 18 | #include <pthread.h> |
14641deb | 19 | |
4318ae1b | 20 | #include <lttng/ringbuffer-config.h> |
4931a13e MD |
21 | #include "backend_types.h" |
22 | #include "frontend_types.h" | |
a6352fd4 | 23 | #include "shm.h" |
852c2936 MD |
24 | |
25 | /* Buffer offset macros */ | |
26 | ||
27 | /* buf_trunc mask selects only the buffer number. */ | |
28 | static inline | |
29 | unsigned long buf_trunc(unsigned long offset, struct channel *chan) | |
30 | { | |
31 | return offset & ~(chan->backend.buf_size - 1); | |
32 | ||
33 | } | |
34 | ||
35 | /* Select the buffer number value (counter). */ | |
36 | static inline | |
37 | unsigned long buf_trunc_val(unsigned long offset, struct channel *chan) | |
38 | { | |
39 | return buf_trunc(offset, chan) >> chan->backend.buf_size_order; | |
40 | } | |
41 | ||
42 | /* buf_offset mask selects only the offset within the current buffer. */ | |
43 | static inline | |
44 | unsigned long buf_offset(unsigned long offset, struct channel *chan) | |
45 | { | |
46 | return offset & (chan->backend.buf_size - 1); | |
47 | } | |
48 | ||
49 | /* subbuf_offset mask selects the offset within the current subbuffer. */ | |
50 | static inline | |
51 | unsigned long subbuf_offset(unsigned long offset, struct channel *chan) | |
52 | { | |
53 | return offset & (chan->backend.subbuf_size - 1); | |
54 | } | |
55 | ||
56 | /* subbuf_trunc mask selects the subbuffer number. */ | |
57 | static inline | |
58 | unsigned long subbuf_trunc(unsigned long offset, struct channel *chan) | |
59 | { | |
60 | return offset & ~(chan->backend.subbuf_size - 1); | |
61 | } | |
62 | ||
63 | /* subbuf_align aligns the offset to the next subbuffer. */ | |
64 | static inline | |
65 | unsigned long subbuf_align(unsigned long offset, struct channel *chan) | |
66 | { | |
67 | return (offset + chan->backend.subbuf_size) | |
68 | & ~(chan->backend.subbuf_size - 1); | |
69 | } | |
70 | ||
71 | /* subbuf_index returns the index of the current subbuffer within the buffer. */ | |
72 | static inline | |
73 | unsigned long subbuf_index(unsigned long offset, struct channel *chan) | |
74 | { | |
75 | return buf_offset(offset, chan) >> chan->backend.subbuf_size_order; | |
76 | } | |
77 | ||
78 | /* | |
79 | * Last TSC comparison functions. Check if the current TSC overflows tsc_bits | |
80 | * bits from the last TSC read. When overflows are detected, the full 64-bit | |
81 | * timestamp counter should be written in the record header. Reads and writes | |
82 | * last_tsc atomically. | |
83 | */ | |
84 | ||
14641deb | 85 | #if (CAA_BITS_PER_LONG == 32) |
852c2936 | 86 | static inline |
4cfec15c | 87 | void save_last_tsc(const struct lttng_ust_lib_ring_buffer_config *config, |
2fed87ae | 88 | struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc) |
852c2936 MD |
89 | { |
90 | if (config->tsc_bits == 0 || config->tsc_bits == 64) | |
91 | return; | |
92 | ||
93 | /* | |
94 | * Ensure the compiler performs this update in a single instruction. | |
95 | */ | |
96 | v_set(config, &buf->last_tsc, (unsigned long)(tsc >> config->tsc_bits)); | |
97 | } | |
98 | ||
99 | static inline | |
4cfec15c | 100 | int last_tsc_overflow(const struct lttng_ust_lib_ring_buffer_config *config, |
2fed87ae | 101 | struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc) |
852c2936 MD |
102 | { |
103 | unsigned long tsc_shifted; | |
104 | ||
105 | if (config->tsc_bits == 0 || config->tsc_bits == 64) | |
106 | return 0; | |
107 | ||
108 | tsc_shifted = (unsigned long)(tsc >> config->tsc_bits); | |
b5a3dfa5 | 109 | if (caa_unlikely(tsc_shifted |
852c2936 MD |
110 | - (unsigned long)v_read(config, &buf->last_tsc))) |
111 | return 1; | |
112 | else | |
113 | return 0; | |
114 | } | |
115 | #else | |
116 | static inline | |
4cfec15c | 117 | void save_last_tsc(const struct lttng_ust_lib_ring_buffer_config *config, |
2fed87ae | 118 | struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc) |
852c2936 MD |
119 | { |
120 | if (config->tsc_bits == 0 || config->tsc_bits == 64) | |
121 | return; | |
122 | ||
123 | v_set(config, &buf->last_tsc, (unsigned long)tsc); | |
124 | } | |
125 | ||
126 | static inline | |
4cfec15c | 127 | int last_tsc_overflow(const struct lttng_ust_lib_ring_buffer_config *config, |
2fed87ae | 128 | struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc) |
852c2936 MD |
129 | { |
130 | if (config->tsc_bits == 0 || config->tsc_bits == 64) | |
131 | return 0; | |
132 | ||
b5a3dfa5 | 133 | if (caa_unlikely((tsc - v_read(config, &buf->last_tsc)) |
852c2936 MD |
134 | >> config->tsc_bits)) |
135 | return 1; | |
136 | else | |
137 | return 0; | |
138 | } | |
139 | #endif | |
140 | ||
ddabe860 | 141 | __attribute__((visibility("hidden"))) |
852c2936 | 142 | extern |
e56bb47c MD |
143 | int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx *ctx, |
144 | void *client_ctx); | |
852c2936 | 145 | |
ddabe860 | 146 | __attribute__((visibility("hidden"))) |
852c2936 | 147 | extern |
4cfec15c | 148 | void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 149 | enum switch_mode mode, |
38fae1d3 | 150 | struct lttng_ust_shm_handle *handle); |
852c2936 | 151 | |
ddabe860 | 152 | __attribute__((visibility("hidden"))) |
b07cd987 MD |
153 | void lib_ring_buffer_check_deliver_slow(const struct lttng_ust_lib_ring_buffer_config *config, |
154 | struct lttng_ust_lib_ring_buffer *buf, | |
155 | struct channel *chan, | |
156 | unsigned long offset, | |
157 | unsigned long commit_count, | |
158 | unsigned long idx, | |
159 | struct lttng_ust_shm_handle *handle, | |
160 | uint64_t tsc); | |
161 | ||
852c2936 MD |
162 | /* Buffer write helpers */ |
163 | ||
164 | static inline | |
4cfec15c | 165 | void lib_ring_buffer_reserve_push_reader(struct lttng_ust_lib_ring_buffer *buf, |
852c2936 MD |
166 | struct channel *chan, |
167 | unsigned long offset) | |
168 | { | |
169 | unsigned long consumed_old, consumed_new; | |
170 | ||
171 | do { | |
14641deb | 172 | consumed_old = uatomic_read(&buf->consumed); |
852c2936 MD |
173 | /* |
174 | * If buffer is in overwrite mode, push the reader consumed | |
175 | * count if the write position has reached it and we are not | |
176 | * at the first iteration (don't push the reader farther than | |
177 | * the writer). This operation can be done concurrently by many | |
178 | * writers in the same buffer, the writer being at the farthest | |
179 | * write position sub-buffer index in the buffer being the one | |
180 | * which will win this loop. | |
181 | */ | |
b5a3dfa5 | 182 | if (caa_unlikely(subbuf_trunc(offset, chan) |
852c2936 MD |
183 | - subbuf_trunc(consumed_old, chan) |
184 | >= chan->backend.buf_size)) | |
185 | consumed_new = subbuf_align(consumed_old, chan); | |
186 | else | |
187 | return; | |
b5a3dfa5 | 188 | } while (caa_unlikely(uatomic_cmpxchg(&buf->consumed, consumed_old, |
852c2936 MD |
189 | consumed_new) != consumed_old)); |
190 | } | |
191 | ||
4c742ffd MD |
192 | /* |
193 | * Move consumed position to the beginning of subbuffer in which the | |
194 | * write offset is. Should only be used on ring buffers that are not | |
195 | * actively being written into, because clear_reader does not take into | |
196 | * account the commit counters when moving the consumed position, which | |
197 | * can make concurrent trace producers or consumers observe consumed | |
198 | * position further than the write offset, which breaks ring buffer | |
199 | * algorithm guarantees. | |
200 | */ | |
beca55a1 MD |
201 | static inline |
202 | void lib_ring_buffer_clear_reader(struct lttng_ust_lib_ring_buffer *buf, | |
203 | struct lttng_ust_shm_handle *handle) | |
204 | { | |
205 | struct channel *chan; | |
206 | const struct lttng_ust_lib_ring_buffer_config *config; | |
207 | unsigned long offset, consumed_old, consumed_new; | |
208 | ||
209 | chan = shmp(handle, buf->backend.chan); | |
210 | if (!chan) | |
211 | return; | |
212 | config = &chan->backend.config; | |
213 | ||
214 | do { | |
215 | offset = v_read(config, &buf->offset); | |
216 | consumed_old = uatomic_read(&buf->consumed); | |
4c742ffd MD |
217 | CHAN_WARN_ON(chan, (long) (subbuf_trunc(offset, chan) |
218 | - subbuf_trunc(consumed_old, chan)) | |
219 | < 0); | |
220 | consumed_new = subbuf_trunc(offset, chan); | |
beca55a1 MD |
221 | } while (caa_unlikely(uatomic_cmpxchg(&buf->consumed, consumed_old, |
222 | consumed_new) != consumed_old)); | |
223 | } | |
224 | ||
852c2936 | 225 | static inline |
4cfec15c MD |
226 | int lib_ring_buffer_pending_data(const struct lttng_ust_lib_ring_buffer_config *config, |
227 | struct lttng_ust_lib_ring_buffer *buf, | |
852c2936 MD |
228 | struct channel *chan) |
229 | { | |
230 | return !!subbuf_offset(v_read(config, &buf->offset), chan); | |
231 | } | |
232 | ||
233 | static inline | |
4cfec15c MD |
234 | unsigned long lib_ring_buffer_get_data_size(const struct lttng_ust_lib_ring_buffer_config *config, |
235 | struct lttng_ust_lib_ring_buffer *buf, | |
1d498196 | 236 | unsigned long idx, |
38fae1d3 | 237 | struct lttng_ust_shm_handle *handle) |
852c2936 | 238 | { |
1d498196 | 239 | return subbuffer_get_data_size(config, &buf->backend, idx, handle); |
852c2936 MD |
240 | } |
241 | ||
242 | /* | |
243 | * Check if all space reservation in a buffer have been committed. This helps | |
244 | * knowing if an execution context is nested (for per-cpu buffers only). | |
245 | * This is a very specific ftrace use-case, so we keep this as "internal" API. | |
246 | */ | |
247 | static inline | |
4cfec15c MD |
248 | int lib_ring_buffer_reserve_committed(const struct lttng_ust_lib_ring_buffer_config *config, |
249 | struct lttng_ust_lib_ring_buffer *buf, | |
1d498196 | 250 | struct channel *chan, |
38fae1d3 | 251 | struct lttng_ust_shm_handle *handle) |
852c2936 MD |
252 | { |
253 | unsigned long offset, idx, commit_count; | |
730be651 | 254 | struct commit_counters_hot *cc_hot; |
852c2936 MD |
255 | |
256 | CHAN_WARN_ON(chan, config->alloc != RING_BUFFER_ALLOC_PER_CPU); | |
257 | CHAN_WARN_ON(chan, config->sync != RING_BUFFER_SYNC_PER_CPU); | |
258 | ||
259 | /* | |
260 | * Read offset and commit count in a loop so they are both read | |
261 | * atomically wrt interrupts. By deal with interrupt concurrency by | |
262 | * restarting both reads if the offset has been pushed. Note that given | |
263 | * we only have to deal with interrupt concurrency here, an interrupt | |
264 | * modifying the commit count will also modify "offset", so it is safe | |
265 | * to only check for offset modifications. | |
266 | */ | |
267 | do { | |
268 | offset = v_read(config, &buf->offset); | |
269 | idx = subbuf_index(offset, chan); | |
730be651 MD |
270 | cc_hot = shmp_index(handle, buf->commit_hot, idx); |
271 | if (caa_unlikely(!cc_hot)) | |
272 | return 0; | |
15500a1b | 273 | commit_count = v_read(config, &cc_hot->cc); |
852c2936 MD |
274 | } while (offset != v_read(config, &buf->offset)); |
275 | ||
276 | return ((buf_trunc(offset, chan) >> chan->backend.num_subbuf_order) | |
277 | - (commit_count & chan->commit_count_mask) == 0); | |
278 | } | |
279 | ||
1b7b0501 MD |
280 | /* |
281 | * Receive end of subbuffer TSC as parameter. It has been read in the | |
282 | * space reservation loop of either reserve or switch, which ensures it | |
283 | * progresses monotonically with event records in the buffer. Therefore, | |
284 | * it ensures that the end timestamp of a subbuffer is <= begin | |
285 | * timestamp of the following subbuffers. | |
286 | */ | |
852c2936 | 287 | static inline |
4cfec15c MD |
288 | void lib_ring_buffer_check_deliver(const struct lttng_ust_lib_ring_buffer_config *config, |
289 | struct lttng_ust_lib_ring_buffer *buf, | |
852c2936 MD |
290 | struct channel *chan, |
291 | unsigned long offset, | |
292 | unsigned long commit_count, | |
1d498196 | 293 | unsigned long idx, |
1b7b0501 MD |
294 | struct lttng_ust_shm_handle *handle, |
295 | uint64_t tsc) | |
852c2936 MD |
296 | { |
297 | unsigned long old_commit_count = commit_count | |
298 | - chan->backend.subbuf_size; | |
852c2936 MD |
299 | |
300 | /* Check if all commits have been done */ | |
b5a3dfa5 | 301 | if (caa_unlikely((buf_trunc(offset, chan) >> chan->backend.num_subbuf_order) |
b07cd987 MD |
302 | - (old_commit_count & chan->commit_count_mask) == 0)) |
303 | lib_ring_buffer_check_deliver_slow(config, buf, chan, offset, | |
304 | commit_count, idx, handle, tsc); | |
852c2936 MD |
305 | } |
306 | ||
307 | /* | |
308 | * lib_ring_buffer_write_commit_counter | |
309 | * | |
310 | * For flight recording. must be called after commit. | |
311 | * This function increments the subbuffer's commit_seq counter each time the | |
312 | * commit count reaches back the reserve offset (modulo subbuffer size). It is | |
313 | * useful for crash dump. | |
314 | */ | |
315 | static inline | |
4cfec15c MD |
316 | void lib_ring_buffer_write_commit_counter(const struct lttng_ust_lib_ring_buffer_config *config, |
317 | struct lttng_ust_lib_ring_buffer *buf, | |
852c2936 | 318 | struct channel *chan, |
852c2936 MD |
319 | unsigned long buf_offset, |
320 | unsigned long commit_count, | |
d2fe4771 MD |
321 | struct lttng_ust_shm_handle *handle, |
322 | struct commit_counters_hot *cc_hot) | |
852c2936 | 323 | { |
80249235 | 324 | unsigned long commit_seq_old; |
852c2936 MD |
325 | |
326 | if (config->oops != RING_BUFFER_OOPS_CONSISTENCY) | |
327 | return; | |
328 | ||
852c2936 MD |
329 | /* |
330 | * subbuf_offset includes commit_count_mask. We can simply | |
331 | * compare the offsets within the subbuffer without caring about | |
332 | * buffer full/empty mismatch because offset is never zero here | |
333 | * (subbuffer header and record headers have non-zero length). | |
334 | */ | |
80249235 | 335 | if (caa_unlikely(subbuf_offset(buf_offset - commit_count, chan))) |
852c2936 MD |
336 | return; |
337 | ||
d2fe4771 | 338 | commit_seq_old = v_read(config, &cc_hot->seq); |
16ec84c8 | 339 | if (caa_likely((long) (commit_seq_old - commit_count) < 0)) |
d2fe4771 | 340 | v_set(config, &cc_hot->seq, commit_count); |
852c2936 MD |
341 | } |
342 | ||
ddabe860 | 343 | __attribute__((visibility("hidden"))) |
4cfec15c | 344 | extern int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer *buf, |
a6352fd4 | 345 | struct channel_backend *chanb, int cpu, |
38fae1d3 | 346 | struct lttng_ust_shm_handle *handle, |
1d498196 | 347 | struct shm_object *shmobj); |
ddabe860 MJ |
348 | |
349 | __attribute__((visibility("hidden"))) | |
4cfec15c | 350 | extern void lib_ring_buffer_free(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 351 | struct lttng_ust_shm_handle *handle); |
852c2936 MD |
352 | |
353 | /* Keep track of trap nesting inside ring buffer code */ | |
ddabe860 | 354 | __attribute__((visibility("hidden"))) |
8c90a710 | 355 | extern DECLARE_URCU_TLS(unsigned int, lib_ring_buffer_nesting); |
852c2936 | 356 | |
e92f3e28 | 357 | #endif /* _LTTNG_RING_BUFFER_FRONTEND_INTERNAL_H */ |