1 /* SPDX-License-Identifier: (GPL-2.0 OR LGPL-2.1)
3 * linux/ringbuffer/frontend_internal.h
5 * Ring Buffer Library Synchronization Header (internal helpers).
7 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * See ring_buffer_frontend.c for more information on wait-free algorithms.
12 #ifndef _LIB_RING_BUFFER_FRONTEND_INTERNAL_H
13 #define _LIB_RING_BUFFER_FRONTEND_INTERNAL_H
15 #include <wrapper/ringbuffer/config.h>
16 #include <wrapper/ringbuffer/backend_types.h>
17 #include <wrapper/ringbuffer/frontend_types.h>
18 #include <lib/prio_heap/lttng_prio_heap.h> /* For per-CPU read-side iterator */
20 /* Buffer offset macros */
22 /* buf_trunc mask selects only the buffer number. */
24 unsigned long buf_trunc(unsigned long offset
, struct channel
*chan
)
26 return offset
& ~(chan
->backend
.buf_size
- 1);
30 /* Select the buffer number value (counter). */
32 unsigned long buf_trunc_val(unsigned long offset
, struct channel
*chan
)
34 return buf_trunc(offset
, chan
) >> chan
->backend
.buf_size_order
;
37 /* buf_offset mask selects only the offset within the current buffer. */
39 unsigned long buf_offset(unsigned long offset
, struct channel
*chan
)
41 return offset
& (chan
->backend
.buf_size
- 1);
44 /* subbuf_offset mask selects the offset within the current subbuffer. */
46 unsigned long subbuf_offset(unsigned long offset
, struct channel
*chan
)
48 return offset
& (chan
->backend
.subbuf_size
- 1);
51 /* subbuf_trunc mask selects the subbuffer number. */
53 unsigned long subbuf_trunc(unsigned long offset
, struct channel
*chan
)
55 return offset
& ~(chan
->backend
.subbuf_size
- 1);
58 /* subbuf_align aligns the offset to the next subbuffer. */
60 unsigned long subbuf_align(unsigned long offset
, struct channel
*chan
)
62 return (offset
+ chan
->backend
.subbuf_size
)
63 & ~(chan
->backend
.subbuf_size
- 1);
66 /* subbuf_index returns the index of the current subbuffer within the buffer. */
68 unsigned long subbuf_index(unsigned long offset
, struct channel
*chan
)
70 return buf_offset(offset
, chan
) >> chan
->backend
.subbuf_size_order
;
74 * Last TSC comparison functions. Check if the current TSC overflows tsc_bits
75 * bits from the last TSC read. When overflows are detected, the full 64-bit
76 * timestamp counter should be written in the record header. Reads and writes
77 * last_tsc atomically.
80 #if (BITS_PER_LONG == 32)
82 void save_last_tsc(const struct lib_ring_buffer_config
*config
,
83 struct lib_ring_buffer
*buf
, u64 tsc
)
85 if (config
->tsc_bits
== 0 || config
->tsc_bits
== 64)
89 * Ensure the compiler performs this update in a single instruction.
91 v_set(config
, &buf
->last_tsc
, (unsigned long)(tsc
>> config
->tsc_bits
));
95 int last_tsc_overflow(const struct lib_ring_buffer_config
*config
,
96 struct lib_ring_buffer
*buf
, u64 tsc
)
98 unsigned long tsc_shifted
;
100 if (config
->tsc_bits
== 0 || config
->tsc_bits
== 64)
103 tsc_shifted
= (unsigned long)(tsc
>> config
->tsc_bits
);
104 if (unlikely(tsc_shifted
105 - (unsigned long)v_read(config
, &buf
->last_tsc
)))
112 void save_last_tsc(const struct lib_ring_buffer_config
*config
,
113 struct lib_ring_buffer
*buf
, u64 tsc
)
115 if (config
->tsc_bits
== 0 || config
->tsc_bits
== 64)
118 v_set(config
, &buf
->last_tsc
, (unsigned long)tsc
);
122 int last_tsc_overflow(const struct lib_ring_buffer_config
*config
,
123 struct lib_ring_buffer
*buf
, u64 tsc
)
125 if (config
->tsc_bits
== 0 || config
->tsc_bits
== 64)
128 if (unlikely((tsc
- v_read(config
, &buf
->last_tsc
))
129 >> config
->tsc_bits
))
137 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx
*ctx
,
141 void lib_ring_buffer_switch_slow(struct lib_ring_buffer
*buf
,
142 enum switch_mode mode
);
145 void lib_ring_buffer_check_deliver_slow(const struct lib_ring_buffer_config
*config
,
146 struct lib_ring_buffer
*buf
,
147 struct channel
*chan
,
148 unsigned long offset
,
149 unsigned long commit_count
,
154 void lib_ring_buffer_switch_remote(struct lib_ring_buffer
*buf
);
156 void lib_ring_buffer_switch_remote_empty(struct lib_ring_buffer
*buf
);
158 /* Buffer write helpers */
161 void lib_ring_buffer_reserve_push_reader(struct lib_ring_buffer
*buf
,
162 struct channel
*chan
,
163 unsigned long offset
)
165 unsigned long consumed_old
, consumed_new
;
168 consumed_old
= atomic_long_read(&buf
->consumed
);
170 * If buffer is in overwrite mode, push the reader consumed
171 * count if the write position has reached it and we are not
172 * at the first iteration (don't push the reader farther than
173 * the writer). This operation can be done concurrently by many
174 * writers in the same buffer, the writer being at the farthest
175 * write position sub-buffer index in the buffer being the one
176 * which will win this loop.
178 if (unlikely(subbuf_trunc(offset
, chan
)
179 - subbuf_trunc(consumed_old
, chan
)
180 >= chan
->backend
.buf_size
))
181 consumed_new
= subbuf_align(consumed_old
, chan
);
184 } while (unlikely(atomic_long_cmpxchg(&buf
->consumed
, consumed_old
,
185 consumed_new
) != consumed_old
));
189 int lib_ring_buffer_pending_data(const struct lib_ring_buffer_config
*config
,
190 struct lib_ring_buffer
*buf
,
191 struct channel
*chan
)
193 return !!subbuf_offset(v_read(config
, &buf
->offset
), chan
);
197 unsigned long lib_ring_buffer_get_data_size(const struct lib_ring_buffer_config
*config
,
198 struct lib_ring_buffer
*buf
,
201 return subbuffer_get_data_size(config
, &buf
->backend
, idx
);
205 * Check if all space reservation in a buffer have been committed. This helps
206 * knowing if an execution context is nested (for per-cpu buffers only).
207 * This is a very specific ftrace use-case, so we keep this as "internal" API.
210 int lib_ring_buffer_reserve_committed(const struct lib_ring_buffer_config
*config
,
211 struct lib_ring_buffer
*buf
,
212 struct channel
*chan
)
214 unsigned long offset
, idx
, commit_count
;
216 CHAN_WARN_ON(chan
, config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
);
217 CHAN_WARN_ON(chan
, config
->sync
!= RING_BUFFER_SYNC_PER_CPU
);
220 * Read offset and commit count in a loop so they are both read
221 * atomically wrt interrupts. By deal with interrupt concurrency by
222 * restarting both reads if the offset has been pushed. Note that given
223 * we only have to deal with interrupt concurrency here, an interrupt
224 * modifying the commit count will also modify "offset", so it is safe
225 * to only check for offset modifications.
228 offset
= v_read(config
, &buf
->offset
);
229 idx
= subbuf_index(offset
, chan
);
230 commit_count
= v_read(config
, &buf
->commit_hot
[idx
].cc
);
231 } while (offset
!= v_read(config
, &buf
->offset
));
233 return ((buf_trunc(offset
, chan
) >> chan
->backend
.num_subbuf_order
)
234 - (commit_count
& chan
->commit_count_mask
) == 0);
238 * Receive end of subbuffer TSC as parameter. It has been read in the
239 * space reservation loop of either reserve or switch, which ensures it
240 * progresses monotonically with event records in the buffer. Therefore,
241 * it ensures that the end timestamp of a subbuffer is <= begin
242 * timestamp of the following subbuffers.
245 void lib_ring_buffer_check_deliver(const struct lib_ring_buffer_config
*config
,
246 struct lib_ring_buffer
*buf
,
247 struct channel
*chan
,
248 unsigned long offset
,
249 unsigned long commit_count
,
253 unsigned long old_commit_count
= commit_count
254 - chan
->backend
.subbuf_size
;
256 /* Check if all commits have been done */
257 if (unlikely((buf_trunc(offset
, chan
) >> chan
->backend
.num_subbuf_order
)
258 - (old_commit_count
& chan
->commit_count_mask
) == 0))
259 lib_ring_buffer_check_deliver_slow(config
, buf
, chan
, offset
,
260 commit_count
, idx
, tsc
);
264 * lib_ring_buffer_write_commit_counter
266 * For flight recording. must be called after commit.
267 * This function increments the subbuffer's commit_seq counter each time the
268 * commit count reaches back the reserve offset (modulo subbuffer size). It is
269 * useful for crash dump.
272 void lib_ring_buffer_write_commit_counter(const struct lib_ring_buffer_config
*config
,
273 struct lib_ring_buffer
*buf
,
274 struct channel
*chan
,
275 unsigned long buf_offset
,
276 unsigned long commit_count
,
277 struct commit_counters_hot
*cc_hot
)
279 unsigned long commit_seq_old
;
281 if (config
->oops
!= RING_BUFFER_OOPS_CONSISTENCY
)
285 * subbuf_offset includes commit_count_mask. We can simply
286 * compare the offsets within the subbuffer without caring about
287 * buffer full/empty mismatch because offset is never zero here
288 * (subbuffer header and record headers have non-zero length).
290 if (unlikely(subbuf_offset(buf_offset
- commit_count
, chan
)))
293 commit_seq_old
= v_read(config
, &cc_hot
->seq
);
294 if (likely((long) (commit_seq_old
- commit_count
) < 0))
295 v_set(config
, &cc_hot
->seq
, commit_count
);
298 extern int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
299 struct channel_backend
*chanb
, int cpu
);
300 extern void lib_ring_buffer_free(struct lib_ring_buffer
*buf
);
302 /* Keep track of trap nesting inside ring buffer code */
303 DECLARE_PER_CPU(unsigned int, lib_ring_buffer_nesting
);
305 #endif /* _LIB_RING_BUFFER_FRONTEND_INTERNAL_H */