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