1 #ifndef _LINUX_RING_BUFFER_FRONTEND_API_H
2 #define _LINUX_RING_BUFFER_FRONTEND_API_H
5 * linux/ringbuffer/frontend_api.h
7 * (C) Copyright 2005-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Ring Buffer Library Synchronization Header (buffer write API).
12 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
14 * See ring_buffer_frontend.c for more information on wait-free algorithms.
15 * See linux/ringbuffer/frontend.h for channel allocation and read-side API.
17 * Dual LGPL v2.1/GPL v2 license.
21 #include "lttng/core.h"
23 #include <urcu/compiler.h>
26 * lib_ring_buffer_get_cpu - Precedes ring buffer reserve/commit.
28 * Grabs RCU read-side lock and keeps a ring buffer nesting count as
29 * supplementary safety net to ensure tracer client code will never
30 * trigger an endless recursion. Returns the processor ID on success,
31 * -EPERM on failure (nesting count too high).
33 * asm volatile and "memory" clobber prevent the compiler from moving
34 * instructions out of the ring buffer nesting count. This is required to ensure
35 * that probe side-effects which can cause recursion (e.g. unforeseen traps,
36 * divisions by 0, ...) are triggered within the incremented nesting count
40 int lib_ring_buffer_get_cpu(const struct lttng_ust_lib_ring_buffer_config
*config
)
45 cpu
= lttng_ust_get_cpu();
46 nesting
= ++lib_ring_buffer_nesting
; /* TLS */
49 if (caa_unlikely(nesting
> 4)) {
51 lib_ring_buffer_nesting
--; /* TLS */
59 * lib_ring_buffer_put_cpu - Follows ring buffer reserve/commit.
62 void lib_ring_buffer_put_cpu(const struct lttng_ust_lib_ring_buffer_config
*config
)
65 lib_ring_buffer_nesting
--; /* TLS */
70 * lib_ring_buffer_try_reserve is called by lib_ring_buffer_reserve(). It is not
71 * part of the API per se.
73 * returns 0 if reserve ok, or 1 if the slow path must be taken.
76 int lib_ring_buffer_try_reserve(const struct lttng_ust_lib_ring_buffer_config
*config
,
77 struct lttng_ust_lib_ring_buffer_ctx
*ctx
,
78 unsigned long *o_begin
, unsigned long *o_end
,
79 unsigned long *o_old
, size_t *before_hdr_pad
)
81 struct channel
*chan
= ctx
->chan
;
82 struct lttng_ust_lib_ring_buffer
*buf
= ctx
->buf
;
83 *o_begin
= v_read(config
, &buf
->offset
);
86 ctx
->tsc
= lib_ring_buffer_clock_read(chan
);
87 if ((int64_t) ctx
->tsc
== -EIO
)
91 * Prefetch cacheline for read because we have to read the previous
92 * commit counter to increment it and commit seq value to compare it to
95 //prefetch(&buf->commit_hot[subbuf_index(*o_begin, chan)]);
97 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
98 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
100 if (caa_unlikely(subbuf_offset(*o_begin
, chan
) == 0))
103 ctx
->slot_size
= record_header_size(config
, chan
, *o_begin
,
104 before_hdr_pad
, ctx
);
106 lib_ring_buffer_align(*o_begin
+ ctx
->slot_size
,
107 ctx
->largest_align
) + ctx
->data_size
;
108 if (caa_unlikely((subbuf_offset(*o_begin
, chan
) + ctx
->slot_size
)
109 > chan
->backend
.subbuf_size
))
113 * Record fits in the current buffer and we are not on a switch
114 * boundary. It's safe to write.
116 *o_end
= *o_begin
+ ctx
->slot_size
;
118 if (caa_unlikely((subbuf_offset(*o_end
, chan
)) == 0))
120 * The offset_end will fall at the very beginning of the next
129 * lib_ring_buffer_reserve - Reserve space in a ring buffer.
130 * @config: ring buffer instance configuration.
131 * @ctx: ring buffer context. (input and output) Must be already initialized.
133 * Atomic wait-free slot reservation. The reserved space starts at the context
134 * "pre_offset". Its length is "slot_size". The associated time-stamp is "tsc".
138 * -EAGAIN if channel is disabled.
139 * -ENOSPC if event size is too large for packet.
140 * -ENOBUFS if there is currently not enough space in buffer for the event.
141 * -EIO if data cannot be written into the buffer for any other reason.
145 int lib_ring_buffer_reserve(const struct lttng_ust_lib_ring_buffer_config
*config
,
146 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
148 struct channel
*chan
= ctx
->chan
;
149 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
150 struct lttng_ust_lib_ring_buffer
*buf
;
151 unsigned long o_begin
, o_end
, o_old
;
152 size_t before_hdr_pad
= 0;
154 if (uatomic_read(&chan
->record_disabled
))
157 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
158 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
160 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
161 if (uatomic_read(&buf
->record_disabled
))
166 * Perform retryable operations.
168 if (caa_unlikely(lib_ring_buffer_try_reserve(config
, ctx
, &o_begin
,
169 &o_end
, &o_old
, &before_hdr_pad
)))
172 if (caa_unlikely(v_cmpxchg(config
, &ctx
->buf
->offset
, o_old
, o_end
)
177 * Atomically update last_tsc. This update races against concurrent
178 * atomic updates, but the race will always cause supplementary full TSC
179 * record headers, never the opposite (missing a full TSC record header
180 * when it would be needed).
182 save_last_tsc(config
, ctx
->buf
, ctx
->tsc
);
185 * Push the reader if necessary
187 lib_ring_buffer_reserve_push_reader(ctx
->buf
, chan
, o_end
- 1);
190 * Clear noref flag for this subbuffer.
192 lib_ring_buffer_clear_noref(config
, &ctx
->buf
->backend
,
193 subbuf_index(o_end
- 1, chan
), handle
);
195 ctx
->pre_offset
= o_begin
;
196 ctx
->buf_offset
= o_begin
+ before_hdr_pad
;
199 return lib_ring_buffer_reserve_slow(ctx
);
203 * lib_ring_buffer_switch - Perform a sub-buffer switch for a per-cpu buffer.
204 * @config: ring buffer instance configuration.
206 * @mode: buffer switch mode (SWITCH_ACTIVE or SWITCH_FLUSH)
208 * This operation is completely reentrant : can be called while tracing is
209 * active with absolutely no lock held.
211 * Note, however, that as a v_cmpxchg is used for some atomic operations and
212 * requires to be executed locally for per-CPU buffers, this function must be
213 * called from the CPU which owns the buffer for a ACTIVE flush, with preemption
214 * disabled, for RING_BUFFER_SYNC_PER_CPU configuration.
217 void lib_ring_buffer_switch(const struct lttng_ust_lib_ring_buffer_config
*config
,
218 struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
219 struct lttng_ust_shm_handle
*handle
)
221 lib_ring_buffer_switch_slow(buf
, mode
, handle
);
224 /* See ring_buffer_frontend_api.h for lib_ring_buffer_reserve(). */
227 * lib_ring_buffer_commit - Commit an record.
228 * @config: ring buffer instance configuration.
229 * @ctx: ring buffer context. (input arguments only)
231 * Atomic unordered slot commit. Increments the commit count in the
232 * specified sub-buffer, and delivers it if necessary.
235 void lib_ring_buffer_commit(const struct lttng_ust_lib_ring_buffer_config
*config
,
236 const struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
238 struct channel
*chan
= ctx
->chan
;
239 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
240 struct lttng_ust_lib_ring_buffer
*buf
= ctx
->buf
;
241 unsigned long offset_end
= ctx
->buf_offset
;
242 unsigned long endidx
= subbuf_index(offset_end
- 1, chan
);
243 unsigned long commit_count
;
246 * Must count record before incrementing the commit count.
248 subbuffer_count_record(config
, &buf
->backend
, endidx
, handle
);
251 * Order all writes to buffer before the commit count update that will
252 * determine that the subbuffer is full.
256 v_add(config
, ctx
->slot_size
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
259 * commit count read can race with concurrent OOO commit count updates.
260 * This is only needed for lib_ring_buffer_check_deliver (for
261 * non-polling delivery only) and for
262 * lib_ring_buffer_write_commit_counter. The race can only cause the
263 * counter to be read with the same value more than once, which could
265 * - Multiple delivery for the same sub-buffer (which is handled
266 * gracefully by the reader code) if the value is for a full
267 * sub-buffer. It's important that we can never miss a sub-buffer
268 * delivery. Re-reading the value after the v_add ensures this.
269 * - Reading a commit_count with a higher value that what was actually
270 * added to it for the lib_ring_buffer_write_commit_counter call
271 * (again caused by a concurrent committer). It does not matter,
272 * because this function is interested in the fact that the commit
273 * count reaches back the reserve offset for a specific sub-buffer,
274 * which is completely independent of the order.
276 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
278 lib_ring_buffer_check_deliver(config
, buf
, chan
, offset_end
- 1,
279 commit_count
, endidx
, handle
);
281 * Update used size at each commit. It's needed only for extracting
282 * ring_buffer buffers from vmcore, after crash.
284 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
285 ctx
->buf_offset
, commit_count
,
286 ctx
->slot_size
, handle
);
290 * lib_ring_buffer_try_discard_reserve - Try discarding a record.
291 * @config: ring buffer instance configuration.
292 * @ctx: ring buffer context. (input arguments only)
294 * Only succeeds if no other record has been written after the record to
295 * discard. If discard fails, the record must be committed to the buffer.
297 * Returns 0 upon success, -EPERM if the record cannot be discarded.
300 int lib_ring_buffer_try_discard_reserve(const struct lttng_ust_lib_ring_buffer_config
*config
,
301 const struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
303 struct lttng_ust_lib_ring_buffer
*buf
= ctx
->buf
;
304 unsigned long end_offset
= ctx
->pre_offset
+ ctx
->slot_size
;
307 * We need to ensure that if the cmpxchg succeeds and discards the
308 * record, the next record will record a full TSC, because it cannot
309 * rely on the last_tsc associated with the discarded record to detect
310 * overflows. The only way to ensure this is to set the last_tsc to 0
311 * (assuming no 64-bit TSC overflow), which forces to write a 64-bit
312 * timestamp in the next record.
314 * Note: if discard fails, we must leave the TSC in the record header.
315 * It is needed to keep track of TSC overflows for the following
318 save_last_tsc(config
, buf
, 0ULL);
320 if (caa_likely(v_cmpxchg(config
, &buf
->offset
, end_offset
, ctx
->pre_offset
)
328 void channel_record_disable(const struct lttng_ust_lib_ring_buffer_config
*config
,
329 struct channel
*chan
)
331 uatomic_inc(&chan
->record_disabled
);
335 void channel_record_enable(const struct lttng_ust_lib_ring_buffer_config
*config
,
336 struct channel
*chan
)
338 uatomic_dec(&chan
->record_disabled
);
342 void lib_ring_buffer_record_disable(const struct lttng_ust_lib_ring_buffer_config
*config
,
343 struct lttng_ust_lib_ring_buffer
*buf
)
345 uatomic_inc(&buf
->record_disabled
);
349 void lib_ring_buffer_record_enable(const struct lttng_ust_lib_ring_buffer_config
*config
,
350 struct lttng_ust_lib_ring_buffer
*buf
)
352 uatomic_dec(&buf
->record_disabled
);
355 #endif /* _LINUX_RING_BUFFER_FRONTEND_API_H */