Commit | Line | Data |
---|---|---|
b7cdc182 | 1 | /* SPDX-License-Identifier: (GPL-2.0-only OR LGPL-2.1-only) |
9f36eaed | 2 | * |
24591303 | 3 | * ringbuffer/frontend_internal.h |
f3bc08c5 | 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 | ||
24591303 MD |
15 | #include <ringbuffer/config.h> |
16 | #include <ringbuffer/backend_types.h> | |
17 | #include <ringbuffer/frontend_types.h> | |
a071f25d | 18 | #include <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. */ | |
23 | static inline | |
24 | unsigned 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). */ | |
31 | static inline | |
32 | unsigned 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. */ | |
38 | static inline | |
39 | unsigned 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. */ | |
45 | static inline | |
46 | unsigned 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. */ | |
52 | static inline | |
53 | unsigned 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. */ | |
59 | static inline | |
60 | unsigned 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. */ | |
67 | static inline | |
68 | unsigned 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) | |
81 | static inline | |
82 | void 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 | ||
94 | static inline | |
95 | int 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 | |
111 | static inline | |
112 | void 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 | ||
121 | static inline | |
122 | int 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 | ||
136 | extern | |
e27e4381 | 137 | int lib_ring_buffer_reserve_slow(struct lttng_kernel_ring_buffer_ctx *ctx, |
cc62f29e | 138 | void *client_ctx); |
f3bc08c5 MD |
139 | |
140 | extern | |
141 | void lib_ring_buffer_switch_slow(struct lib_ring_buffer *buf, | |
142 | enum switch_mode mode); | |
143 | ||
aece661f MD |
144 | extern |
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, | |
150 | unsigned long idx, | |
151 | u64 tsc); | |
152 | ||
5e391252 MD |
153 | extern |
154 | void lib_ring_buffer_switch_remote(struct lib_ring_buffer *buf); | |
c6f05468 MD |
155 | extern |
156 | void lib_ring_buffer_switch_remote_empty(struct lib_ring_buffer *buf); | |
c245d0d3 MD |
157 | extern |
158 | void lib_ring_buffer_clear(struct lib_ring_buffer *buf); | |
5e391252 | 159 | |
f3bc08c5 MD |
160 | /* Buffer write helpers */ |
161 | ||
162 | static inline | |
163 | void lib_ring_buffer_reserve_push_reader(struct lib_ring_buffer *buf, | |
164 | struct channel *chan, | |
165 | unsigned long offset) | |
166 | { | |
167 | unsigned long consumed_old, consumed_new; | |
168 | ||
169 | do { | |
170 | consumed_old = atomic_long_read(&buf->consumed); | |
171 | /* | |
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. | |
179 | */ | |
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); | |
184 | else | |
185 | return; | |
186 | } while (unlikely(atomic_long_cmpxchg(&buf->consumed, consumed_old, | |
187 | consumed_new) != consumed_old)); | |
188 | } | |
189 | ||
c245d0d3 MD |
190 | /* |
191 | * Move consumed position to the beginning of subbuffer in which the | |
ac4a87e5 MD |
192 | * write offset is. Should only be used on ring buffers that are not |
193 | * actively being written into, because clear_reader does not take into | |
194 | * account the commit counters when moving the consumed position, which | |
195 | * can make concurrent trace producers or consumers observe consumed | |
196 | * position further than the write offset, which breaks ring buffer | |
197 | * algorithm guarantees. | |
c245d0d3 MD |
198 | */ |
199 | static inline | |
200 | void lib_ring_buffer_clear_reader(struct lib_ring_buffer *buf, | |
201 | struct channel *chan) | |
202 | { | |
203 | const struct lib_ring_buffer_config *config = &chan->backend.config; | |
204 | unsigned long offset, consumed_old, consumed_new; | |
205 | ||
206 | do { | |
207 | offset = v_read(config, &buf->offset); | |
208 | consumed_old = atomic_long_read(&buf->consumed); | |
ac4a87e5 MD |
209 | CHAN_WARN_ON(chan, (long) (subbuf_trunc(offset, chan) |
210 | - subbuf_trunc(consumed_old, chan)) | |
211 | < 0); | |
212 | consumed_new = subbuf_trunc(offset, chan); | |
c245d0d3 MD |
213 | } while (unlikely(atomic_long_cmpxchg(&buf->consumed, consumed_old, |
214 | consumed_new) != consumed_old)); | |
215 | } | |
216 | ||
f3bc08c5 MD |
217 | static inline |
218 | int lib_ring_buffer_pending_data(const struct lib_ring_buffer_config *config, | |
219 | struct lib_ring_buffer *buf, | |
220 | struct channel *chan) | |
221 | { | |
222 | return !!subbuf_offset(v_read(config, &buf->offset), chan); | |
223 | } | |
224 | ||
225 | static inline | |
226 | unsigned long lib_ring_buffer_get_data_size(const struct lib_ring_buffer_config *config, | |
227 | struct lib_ring_buffer *buf, | |
228 | unsigned long idx) | |
229 | { | |
230 | return subbuffer_get_data_size(config, &buf->backend, idx); | |
231 | } | |
232 | ||
233 | /* | |
234 | * Check if all space reservation in a buffer have been committed. This helps | |
235 | * knowing if an execution context is nested (for per-cpu buffers only). | |
236 | * This is a very specific ftrace use-case, so we keep this as "internal" API. | |
237 | */ | |
238 | static inline | |
239 | int lib_ring_buffer_reserve_committed(const struct lib_ring_buffer_config *config, | |
240 | struct lib_ring_buffer *buf, | |
241 | struct channel *chan) | |
242 | { | |
243 | unsigned long offset, idx, commit_count; | |
244 | ||
245 | CHAN_WARN_ON(chan, config->alloc != RING_BUFFER_ALLOC_PER_CPU); | |
246 | CHAN_WARN_ON(chan, config->sync != RING_BUFFER_SYNC_PER_CPU); | |
247 | ||
248 | /* | |
249 | * Read offset and commit count in a loop so they are both read | |
250 | * atomically wrt interrupts. By deal with interrupt concurrency by | |
251 | * restarting both reads if the offset has been pushed. Note that given | |
252 | * we only have to deal with interrupt concurrency here, an interrupt | |
253 | * modifying the commit count will also modify "offset", so it is safe | |
254 | * to only check for offset modifications. | |
255 | */ | |
256 | do { | |
257 | offset = v_read(config, &buf->offset); | |
258 | idx = subbuf_index(offset, chan); | |
259 | commit_count = v_read(config, &buf->commit_hot[idx].cc); | |
260 | } while (offset != v_read(config, &buf->offset)); | |
261 | ||
262 | return ((buf_trunc(offset, chan) >> chan->backend.num_subbuf_order) | |
263 | - (commit_count & chan->commit_count_mask) == 0); | |
264 | } | |
265 | ||
635e457c MD |
266 | /* |
267 | * Receive end of subbuffer TSC as parameter. It has been read in the | |
268 | * space reservation loop of either reserve or switch, which ensures it | |
269 | * progresses monotonically with event records in the buffer. Therefore, | |
270 | * it ensures that the end timestamp of a subbuffer is <= begin | |
271 | * timestamp of the following subbuffers. | |
272 | */ | |
f3bc08c5 MD |
273 | static inline |
274 | void lib_ring_buffer_check_deliver(const struct lib_ring_buffer_config *config, | |
275 | struct lib_ring_buffer *buf, | |
276 | struct channel *chan, | |
277 | unsigned long offset, | |
278 | unsigned long commit_count, | |
635e457c MD |
279 | unsigned long idx, |
280 | u64 tsc) | |
f3bc08c5 MD |
281 | { |
282 | unsigned long old_commit_count = commit_count | |
283 | - chan->backend.subbuf_size; | |
f3bc08c5 MD |
284 | |
285 | /* Check if all commits have been done */ | |
286 | if (unlikely((buf_trunc(offset, chan) >> chan->backend.num_subbuf_order) | |
aece661f MD |
287 | - (old_commit_count & chan->commit_count_mask) == 0)) |
288 | lib_ring_buffer_check_deliver_slow(config, buf, chan, offset, | |
289 | commit_count, idx, tsc); | |
f3bc08c5 MD |
290 | } |
291 | ||
292 | /* | |
293 | * lib_ring_buffer_write_commit_counter | |
294 | * | |
295 | * For flight recording. must be called after commit. | |
296 | * This function increments the subbuffer's commit_seq counter each time the | |
297 | * commit count reaches back the reserve offset (modulo subbuffer size). It is | |
298 | * useful for crash dump. | |
299 | */ | |
300 | static inline | |
301 | void lib_ring_buffer_write_commit_counter(const struct lib_ring_buffer_config *config, | |
302 | struct lib_ring_buffer *buf, | |
303 | struct channel *chan, | |
f3bc08c5 | 304 | unsigned long buf_offset, |
8ec496cf MD |
305 | unsigned long commit_count, |
306 | struct commit_counters_hot *cc_hot) | |
f3bc08c5 | 307 | { |
7915e163 | 308 | unsigned long commit_seq_old; |
f3bc08c5 MD |
309 | |
310 | if (config->oops != RING_BUFFER_OOPS_CONSISTENCY) | |
311 | return; | |
312 | ||
f3bc08c5 MD |
313 | /* |
314 | * subbuf_offset includes commit_count_mask. We can simply | |
315 | * compare the offsets within the subbuffer without caring about | |
316 | * buffer full/empty mismatch because offset is never zero here | |
317 | * (subbuffer header and record headers have non-zero length). | |
318 | */ | |
7915e163 | 319 | if (unlikely(subbuf_offset(buf_offset - commit_count, chan))) |
f3bc08c5 MD |
320 | return; |
321 | ||
8ec496cf | 322 | commit_seq_old = v_read(config, &cc_hot->seq); |
6212b6b6 | 323 | if (likely((long) (commit_seq_old - commit_count) < 0)) |
8ec496cf | 324 | v_set(config, &cc_hot->seq, commit_count); |
f3bc08c5 MD |
325 | } |
326 | ||
327 | extern int lib_ring_buffer_create(struct lib_ring_buffer *buf, | |
328 | struct channel_backend *chanb, int cpu); | |
329 | extern void lib_ring_buffer_free(struct lib_ring_buffer *buf); | |
330 | ||
331 | /* Keep track of trap nesting inside ring buffer code */ | |
332 | DECLARE_PER_CPU(unsigned int, lib_ring_buffer_nesting); | |
333 | ||
886d51a3 | 334 | #endif /* _LIB_RING_BUFFER_FRONTEND_INTERNAL_H */ |