Implement ring buffer clear
[lttng-modules.git] / lib / ringbuffer / frontend_internal.h
1 /* SPDX-License-Identifier: (GPL-2.0 OR LGPL-2.1)
2 *
3 * linux/ringbuffer/frontend_internal.h
4 *
5 * Ring Buffer Library Synchronization Header (internal helpers).
6 *
7 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * See ring_buffer_frontend.c for more information on wait-free algorithms.
10 */
11
12 #ifndef _LIB_RING_BUFFER_FRONTEND_INTERNAL_H
13 #define _LIB_RING_BUFFER_FRONTEND_INTERNAL_H
14
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 */
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
137 int lib_ring_buffer_reserve_slow(struct lib_ring_buffer_ctx *ctx,
138 void *client_ctx);
139
140 extern
141 void lib_ring_buffer_switch_slow(struct lib_ring_buffer *buf,
142 enum switch_mode mode);
143
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
153 extern
154 void lib_ring_buffer_switch_remote(struct lib_ring_buffer *buf);
155 extern
156 void lib_ring_buffer_switch_remote_empty(struct lib_ring_buffer *buf);
157 extern
158 void lib_ring_buffer_clear(struct lib_ring_buffer *buf);
159
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
190 /*
191 * Move consumed position to the beginning of subbuffer in which the
192 * write offset is.
193 */
194 static inline
195 void lib_ring_buffer_clear_reader(struct lib_ring_buffer *buf,
196 struct channel *chan)
197 {
198 const struct lib_ring_buffer_config *config = &chan->backend.config;
199 unsigned long offset, consumed_old, consumed_new;
200
201 do {
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)
206 > 0))
207 consumed_new = subbuf_trunc(offset, chan);
208 else
209 return;
210 } while (unlikely(atomic_long_cmpxchg(&buf->consumed, consumed_old,
211 consumed_new) != consumed_old));
212 }
213
214 static inline
215 int lib_ring_buffer_pending_data(const struct lib_ring_buffer_config *config,
216 struct lib_ring_buffer *buf,
217 struct channel *chan)
218 {
219 return !!subbuf_offset(v_read(config, &buf->offset), chan);
220 }
221
222 static inline
223 unsigned long lib_ring_buffer_get_data_size(const struct lib_ring_buffer_config *config,
224 struct lib_ring_buffer *buf,
225 unsigned long idx)
226 {
227 return subbuffer_get_data_size(config, &buf->backend, idx);
228 }
229
230 /*
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.
234 */
235 static inline
236 int lib_ring_buffer_reserve_committed(const struct lib_ring_buffer_config *config,
237 struct lib_ring_buffer *buf,
238 struct channel *chan)
239 {
240 unsigned long offset, idx, commit_count;
241
242 CHAN_WARN_ON(chan, config->alloc != RING_BUFFER_ALLOC_PER_CPU);
243 CHAN_WARN_ON(chan, config->sync != RING_BUFFER_SYNC_PER_CPU);
244
245 /*
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.
252 */
253 do {
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));
258
259 return ((buf_trunc(offset, chan) >> chan->backend.num_subbuf_order)
260 - (commit_count & chan->commit_count_mask) == 0);
261 }
262
263 /*
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.
269 */
270 static inline
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,
276 unsigned long idx,
277 u64 tsc)
278 {
279 unsigned long old_commit_count = commit_count
280 - chan->backend.subbuf_size;
281
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);
287 }
288
289 /*
290 * lib_ring_buffer_write_commit_counter
291 *
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.
296 */
297 static inline
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)
304 {
305 unsigned long commit_seq_old;
306
307 if (config->oops != RING_BUFFER_OOPS_CONSISTENCY)
308 return;
309
310 /*
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).
315 */
316 if (unlikely(subbuf_offset(buf_offset - commit_count, chan)))
317 return;
318
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);
322 }
323
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);
327
328 /* Keep track of trap nesting inside ring buffer code */
329 DECLARE_PER_CPU(unsigned int, lib_ring_buffer_nesting);
330
331 #endif /* _LIB_RING_BUFFER_FRONTEND_INTERNAL_H */
This page took 0.035637 seconds and 4 git commands to generate.