Cleanup: move to kernel style SPDX license identifiers
[lttng-modules.git] / lib / ringbuffer / frontend_internal.h
CommitLineData
9f36eaed
MJ
1/* SPDX-License-Identifier: (GPL-2.0 OR LGPL-2.1)
2 *
f3bc08c5
MD
3 * linux/ringbuffer/frontend_internal.h
4 *
f3bc08c5
MD
5 * Ring Buffer Library Synchronization Header (internal helpers).
6 *
886d51a3
MD
7 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
f3bc08c5 9 * See ring_buffer_frontend.c for more information on wait-free algorithms.
f3bc08c5
MD
10 */
11
9f36eaed
MJ
12#ifndef _LIB_RING_BUFFER_FRONTEND_INTERNAL_H
13#define _LIB_RING_BUFFER_FRONTEND_INTERNAL_H
14
5671a661
MD
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 */
f3bc08c5
MD
19
20/* Buffer offset macros */
21
22/* buf_trunc mask selects only the buffer number. */
23static inline
24unsigned long buf_trunc(unsigned long offset, struct channel *chan)
25{
26 return offset & ~(chan->backend.buf_size - 1);
27
28}
29
30/* Select the buffer number value (counter). */
31static inline
32unsigned long buf_trunc_val(unsigned long offset, struct channel *chan)
33{
34 return buf_trunc(offset, chan) >> chan->backend.buf_size_order;
35}
36
37/* buf_offset mask selects only the offset within the current buffer. */
38static inline
39unsigned long buf_offset(unsigned long offset, struct channel *chan)
40{
41 return offset & (chan->backend.buf_size - 1);
42}
43
44/* subbuf_offset mask selects the offset within the current subbuffer. */
45static inline
46unsigned long subbuf_offset(unsigned long offset, struct channel *chan)
47{
48 return offset & (chan->backend.subbuf_size - 1);
49}
50
51/* subbuf_trunc mask selects the subbuffer number. */
52static inline
53unsigned long subbuf_trunc(unsigned long offset, struct channel *chan)
54{
55 return offset & ~(chan->backend.subbuf_size - 1);
56}
57
58/* subbuf_align aligns the offset to the next subbuffer. */
59static inline
60unsigned long subbuf_align(unsigned long offset, struct channel *chan)
61{
62 return (offset + chan->backend.subbuf_size)
63 & ~(chan->backend.subbuf_size - 1);
64}
65
66/* subbuf_index returns the index of the current subbuffer within the buffer. */
67static inline
68unsigned long subbuf_index(unsigned long offset, struct channel *chan)
69{
70 return buf_offset(offset, chan) >> chan->backend.subbuf_size_order;
71}
72
73/*
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.
78 */
79
80#if (BITS_PER_LONG == 32)
81static inline
82void save_last_tsc(const struct lib_ring_buffer_config *config,
83 struct lib_ring_buffer *buf, u64 tsc)
84{
85 if (config->tsc_bits == 0 || config->tsc_bits == 64)
86 return;
87
88 /*
89 * Ensure the compiler performs this update in a single instruction.
90 */
91 v_set(config, &buf->last_tsc, (unsigned long)(tsc >> config->tsc_bits));
92}
93
94static inline
95int last_tsc_overflow(const struct lib_ring_buffer_config *config,
96 struct lib_ring_buffer *buf, u64 tsc)
97{
98 unsigned long tsc_shifted;
99
100 if (config->tsc_bits == 0 || config->tsc_bits == 64)
101 return 0;
102
103 tsc_shifted = (unsigned long)(tsc >> config->tsc_bits);
104 if (unlikely(tsc_shifted
105 - (unsigned long)v_read(config, &buf->last_tsc)))
106 return 1;
107 else
108 return 0;
109}
110#else
111static inline
112void save_last_tsc(const struct lib_ring_buffer_config *config,
113 struct lib_ring_buffer *buf, u64 tsc)
114{
115 if (config->tsc_bits == 0 || config->tsc_bits == 64)
116 return;
117
118 v_set(config, &buf->last_tsc, (unsigned long)tsc);
119}
120
121static inline
122int last_tsc_overflow(const struct lib_ring_buffer_config *config,
123 struct lib_ring_buffer *buf, u64 tsc)
124{
125 if (config->tsc_bits == 0 || config->tsc_bits == 64)
126 return 0;
127
128 if (unlikely((tsc - v_read(config, &buf->last_tsc))
129 >> config->tsc_bits))
130 return 1;
131 else
132 return 0;
133}
134#endif
135
136extern
cc62f29e
MD
137int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx *ctx,
138 void *client_ctx);
f3bc08c5
MD
139
140extern
141void lib_ring_buffer_switch_slow(struct lib_ring_buffer *buf,
142 enum switch_mode mode);
143
aece661f
MD
144extern
145void 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,
150 unsigned long idx,
151 u64 tsc);
152
5e391252
MD
153extern
154void lib_ring_buffer_switch_remote(struct lib_ring_buffer *buf);
c6f05468
MD
155extern
156void lib_ring_buffer_switch_remote_empty(struct lib_ring_buffer *buf);
5e391252 157
f3bc08c5
MD
158/* Buffer write helpers */
159
160static inline
161void lib_ring_buffer_reserve_push_reader(struct lib_ring_buffer *buf,
162 struct channel *chan,
163 unsigned long offset)
164{
165 unsigned long consumed_old, consumed_new;
166
167 do {
168 consumed_old = atomic_long_read(&buf->consumed);
169 /*
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.
177 */
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);
182 else
183 return;
184 } while (unlikely(atomic_long_cmpxchg(&buf->consumed, consumed_old,
185 consumed_new) != consumed_old));
186}
187
f3bc08c5
MD
188static inline
189int lib_ring_buffer_pending_data(const struct lib_ring_buffer_config *config,
190 struct lib_ring_buffer *buf,
191 struct channel *chan)
192{
193 return !!subbuf_offset(v_read(config, &buf->offset), chan);
194}
195
196static inline
197unsigned long lib_ring_buffer_get_data_size(const struct lib_ring_buffer_config *config,
198 struct lib_ring_buffer *buf,
199 unsigned long idx)
200{
201 return subbuffer_get_data_size(config, &buf->backend, idx);
202}
203
204/*
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.
208 */
209static inline
210int lib_ring_buffer_reserve_committed(const struct lib_ring_buffer_config *config,
211 struct lib_ring_buffer *buf,
212 struct channel *chan)
213{
214 unsigned long offset, idx, commit_count;
215
216 CHAN_WARN_ON(chan, config->alloc != RING_BUFFER_ALLOC_PER_CPU);
217 CHAN_WARN_ON(chan, config->sync != RING_BUFFER_SYNC_PER_CPU);
218
219 /*
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.
226 */
227 do {
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));
232
233 return ((buf_trunc(offset, chan) >> chan->backend.num_subbuf_order)
234 - (commit_count & chan->commit_count_mask) == 0);
235}
236
635e457c
MD
237/*
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.
243 */
f3bc08c5
MD
244static inline
245void 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,
635e457c
MD
250 unsigned long idx,
251 u64 tsc)
f3bc08c5
MD
252{
253 unsigned long old_commit_count = commit_count
254 - chan->backend.subbuf_size;
f3bc08c5
MD
255
256 /* Check if all commits have been done */
257 if (unlikely((buf_trunc(offset, chan) >> chan->backend.num_subbuf_order)
aece661f
MD
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);
f3bc08c5
MD
261}
262
263/*
264 * lib_ring_buffer_write_commit_counter
265 *
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.
270 */
271static inline
272void lib_ring_buffer_write_commit_counter(const struct lib_ring_buffer_config *config,
273 struct lib_ring_buffer *buf,
274 struct channel *chan,
f3bc08c5 275 unsigned long buf_offset,
8ec496cf
MD
276 unsigned long commit_count,
277 struct commit_counters_hot *cc_hot)
f3bc08c5 278{
7915e163 279 unsigned long commit_seq_old;
f3bc08c5
MD
280
281 if (config->oops != RING_BUFFER_OOPS_CONSISTENCY)
282 return;
283
f3bc08c5
MD
284 /*
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).
289 */
7915e163 290 if (unlikely(subbuf_offset(buf_offset - commit_count, chan)))
f3bc08c5
MD
291 return;
292
8ec496cf 293 commit_seq_old = v_read(config, &cc_hot->seq);
6212b6b6 294 if (likely((long) (commit_seq_old - commit_count) < 0))
8ec496cf 295 v_set(config, &cc_hot->seq, commit_count);
f3bc08c5
MD
296}
297
298extern int lib_ring_buffer_create(struct lib_ring_buffer *buf,
299 struct channel_backend *chanb, int cpu);
300extern void lib_ring_buffer_free(struct lib_ring_buffer *buf);
301
302/* Keep track of trap nesting inside ring buffer code */
303DECLARE_PER_CPU(unsigned int, lib_ring_buffer_nesting);
304
886d51a3 305#endif /* _LIB_RING_BUFFER_FRONTEND_INTERNAL_H */
This page took 0.045578 seconds and 4 git commands to generate.