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 void lib_ring_buffer_clear(struct lib_ring_buffer
*buf
);
160 /* Buffer write helpers */
163 void lib_ring_buffer_reserve_push_reader(struct lib_ring_buffer
*buf
,
164 struct channel
*chan
,
165 unsigned long offset
)
167 unsigned long consumed_old
, consumed_new
;
170 consumed_old
= atomic_long_read(&buf
->consumed
);
172 * If buffer is in overwrite mode, push the reader consumed
173 * count if the write position has reached it and we are not
174 * at the first iteration (don't push the reader farther than
175 * the writer). This operation can be done concurrently by many
176 * writers in the same buffer, the writer being at the farthest
177 * write position sub-buffer index in the buffer being the one
178 * which will win this loop.
180 if (unlikely(subbuf_trunc(offset
, chan
)
181 - subbuf_trunc(consumed_old
, chan
)
182 >= chan
->backend
.buf_size
))
183 consumed_new
= subbuf_align(consumed_old
, chan
);
186 } while (unlikely(atomic_long_cmpxchg(&buf
->consumed
, consumed_old
,
187 consumed_new
) != consumed_old
));
191 * Move consumed position to the beginning of subbuffer in which the
195 void lib_ring_buffer_clear_reader(struct lib_ring_buffer
*buf
,
196 struct channel
*chan
)
198 const struct lib_ring_buffer_config
*config
= &chan
->backend
.config
;
199 unsigned long offset
, consumed_old
, consumed_new
;
202 offset
= v_read(config
, &buf
->offset
);
203 consumed_old
= atomic_long_read(&buf
->consumed
);
204 if (unlikely(subbuf_trunc(offset
, chan
)
205 - subbuf_trunc(consumed_old
, chan
)
207 consumed_new
= subbuf_trunc(offset
, chan
);
210 } while (unlikely(atomic_long_cmpxchg(&buf
->consumed
, consumed_old
,
211 consumed_new
) != consumed_old
));
215 int lib_ring_buffer_pending_data(const struct lib_ring_buffer_config
*config
,
216 struct lib_ring_buffer
*buf
,
217 struct channel
*chan
)
219 return !!subbuf_offset(v_read(config
, &buf
->offset
), chan
);
223 unsigned long lib_ring_buffer_get_data_size(const struct lib_ring_buffer_config
*config
,
224 struct lib_ring_buffer
*buf
,
227 return subbuffer_get_data_size(config
, &buf
->backend
, idx
);
231 * Check if all space reservation in a buffer have been committed. This helps
232 * knowing if an execution context is nested (for per-cpu buffers only).
233 * This is a very specific ftrace use-case, so we keep this as "internal" API.
236 int lib_ring_buffer_reserve_committed(const struct lib_ring_buffer_config
*config
,
237 struct lib_ring_buffer
*buf
,
238 struct channel
*chan
)
240 unsigned long offset
, idx
, commit_count
;
242 CHAN_WARN_ON(chan
, config
->alloc
!= RING_BUFFER_ALLOC_PER_CPU
);
243 CHAN_WARN_ON(chan
, config
->sync
!= RING_BUFFER_SYNC_PER_CPU
);
246 * Read offset and commit count in a loop so they are both read
247 * atomically wrt interrupts. By deal with interrupt concurrency by
248 * restarting both reads if the offset has been pushed. Note that given
249 * we only have to deal with interrupt concurrency here, an interrupt
250 * modifying the commit count will also modify "offset", so it is safe
251 * to only check for offset modifications.
254 offset
= v_read(config
, &buf
->offset
);
255 idx
= subbuf_index(offset
, chan
);
256 commit_count
= v_read(config
, &buf
->commit_hot
[idx
].cc
);
257 } while (offset
!= v_read(config
, &buf
->offset
));
259 return ((buf_trunc(offset
, chan
) >> chan
->backend
.num_subbuf_order
)
260 - (commit_count
& chan
->commit_count_mask
) == 0);
264 * Receive end of subbuffer TSC as parameter. It has been read in the
265 * space reservation loop of either reserve or switch, which ensures it
266 * progresses monotonically with event records in the buffer. Therefore,
267 * it ensures that the end timestamp of a subbuffer is <= begin
268 * timestamp of the following subbuffers.
271 void lib_ring_buffer_check_deliver(const struct lib_ring_buffer_config
*config
,
272 struct lib_ring_buffer
*buf
,
273 struct channel
*chan
,
274 unsigned long offset
,
275 unsigned long commit_count
,
279 unsigned long old_commit_count
= commit_count
280 - chan
->backend
.subbuf_size
;
282 /* Check if all commits have been done */
283 if (unlikely((buf_trunc(offset
, chan
) >> chan
->backend
.num_subbuf_order
)
284 - (old_commit_count
& chan
->commit_count_mask
) == 0))
285 lib_ring_buffer_check_deliver_slow(config
, buf
, chan
, offset
,
286 commit_count
, idx
, tsc
);
290 * lib_ring_buffer_write_commit_counter
292 * For flight recording. must be called after commit.
293 * This function increments the subbuffer's commit_seq counter each time the
294 * commit count reaches back the reserve offset (modulo subbuffer size). It is
295 * useful for crash dump.
298 void lib_ring_buffer_write_commit_counter(const struct lib_ring_buffer_config
*config
,
299 struct lib_ring_buffer
*buf
,
300 struct channel
*chan
,
301 unsigned long buf_offset
,
302 unsigned long commit_count
,
303 struct commit_counters_hot
*cc_hot
)
305 unsigned long commit_seq_old
;
307 if (config
->oops
!= RING_BUFFER_OOPS_CONSISTENCY
)
311 * subbuf_offset includes commit_count_mask. We can simply
312 * compare the offsets within the subbuffer without caring about
313 * buffer full/empty mismatch because offset is never zero here
314 * (subbuffer header and record headers have non-zero length).
316 if (unlikely(subbuf_offset(buf_offset
- commit_count
, chan
)))
319 commit_seq_old
= v_read(config
, &cc_hot
->seq
);
320 if (likely((long) (commit_seq_old
- commit_count
) < 0))
321 v_set(config
, &cc_hot
->seq
, commit_count
);
324 extern int lib_ring_buffer_create(struct lib_ring_buffer
*buf
,
325 struct channel_backend
*chanb
, int cpu
);
326 extern void lib_ring_buffer_free(struct lib_ring_buffer
*buf
);
328 /* Keep track of trap nesting inside ring buffer code */
329 DECLARE_PER_CPU(unsigned int, lib_ring_buffer_nesting
);
331 #endif /* _LIB_RING_BUFFER_FRONTEND_INTERNAL_H */