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.
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 lib_ring_buffer_config
*config
)
46 nesting
= ++lib_ring_buffer_nesting
; /* TLS */
49 if (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 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 lib_ring_buffer_config
*config
,
77 struct 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 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 (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 (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 (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 lib_ring_buffer_config
*config
,
146 struct lib_ring_buffer_ctx
*ctx
)
148 struct channel
*chan
= ctx
->chan
;
149 struct lib_ring_buffer
*buf
;
150 unsigned long o_begin
, o_end
, o_old
;
151 size_t before_hdr_pad
= 0;
153 if (uatomic_read(&chan
->record_disabled
))
156 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
157 buf
= &shmp(chan
->backend
.buf
)[ctx
->cpu
];
159 buf
= shmp(chan
->backend
.buf
);
160 if (uatomic_read(&buf
->record_disabled
))
165 * Perform retryable operations.
167 if (unlikely(lib_ring_buffer_try_reserve(config
, ctx
, &o_begin
,
168 &o_end
, &o_old
, &before_hdr_pad
)))
171 if (unlikely(v_cmpxchg(config
, &ctx
->buf
->offset
, o_old
, o_end
)
176 * Atomically update last_tsc. This update races against concurrent
177 * atomic updates, but the race will always cause supplementary full TSC
178 * record headers, never the opposite (missing a full TSC record header
179 * when it would be needed).
181 save_last_tsc(config
, ctx
->buf
, ctx
->tsc
);
184 * Push the reader if necessary
186 lib_ring_buffer_reserve_push_reader(ctx
->buf
, chan
, o_end
- 1);
189 * Clear noref flag for this subbuffer.
191 lib_ring_buffer_clear_noref(config
, &ctx
->buf
->backend
,
192 subbuf_index(o_end
- 1, chan
));
194 ctx
->pre_offset
= o_begin
;
195 ctx
->buf_offset
= o_begin
+ before_hdr_pad
;
198 return lib_ring_buffer_reserve_slow(ctx
);
202 * lib_ring_buffer_switch - Perform a sub-buffer switch for a per-cpu buffer.
203 * @config: ring buffer instance configuration.
205 * @mode: buffer switch mode (SWITCH_ACTIVE or SWITCH_FLUSH)
207 * This operation is completely reentrant : can be called while tracing is
208 * active with absolutely no lock held.
210 * Note, however, that as a v_cmpxchg is used for some atomic operations and
211 * requires to be executed locally for per-CPU buffers, this function must be
212 * called from the CPU which owns the buffer for a ACTIVE flush, with preemption
213 * disabled, for RING_BUFFER_SYNC_PER_CPU configuration.
216 void lib_ring_buffer_switch(const struct lib_ring_buffer_config
*config
,
217 struct lib_ring_buffer
*buf
, enum switch_mode mode
)
219 lib_ring_buffer_switch_slow(buf
, mode
);
222 /* See ring_buffer_frontend_api.h for lib_ring_buffer_reserve(). */
225 * lib_ring_buffer_commit - Commit an record.
226 * @config: ring buffer instance configuration.
227 * @ctx: ring buffer context. (input arguments only)
229 * Atomic unordered slot commit. Increments the commit count in the
230 * specified sub-buffer, and delivers it if necessary.
233 void lib_ring_buffer_commit(const struct lib_ring_buffer_config
*config
,
234 const struct lib_ring_buffer_ctx
*ctx
)
236 struct channel
*chan
= ctx
->chan
;
237 struct lib_ring_buffer
*buf
= ctx
->buf
;
238 unsigned long offset_end
= ctx
->buf_offset
;
239 unsigned long endidx
= subbuf_index(offset_end
- 1, chan
);
240 unsigned long commit_count
;
243 * Must count record before incrementing the commit count.
245 subbuffer_count_record(config
, &buf
->backend
, endidx
);
248 * Order all writes to buffer before the commit count update that will
249 * determine that the subbuffer is full.
253 v_add(config
, ctx
->slot_size
, &shmp(buf
->commit_hot
)[endidx
].cc
);
256 * commit count read can race with concurrent OOO commit count updates.
257 * This is only needed for lib_ring_buffer_check_deliver (for
258 * non-polling delivery only) and for
259 * lib_ring_buffer_write_commit_counter. The race can only cause the
260 * counter to be read with the same value more than once, which could
262 * - Multiple delivery for the same sub-buffer (which is handled
263 * gracefully by the reader code) if the value is for a full
264 * sub-buffer. It's important that we can never miss a sub-buffer
265 * delivery. Re-reading the value after the v_add ensures this.
266 * - Reading a commit_count with a higher value that what was actually
267 * added to it for the lib_ring_buffer_write_commit_counter call
268 * (again caused by a concurrent committer). It does not matter,
269 * because this function is interested in the fact that the commit
270 * count reaches back the reserve offset for a specific sub-buffer,
271 * which is completely independent of the order.
273 commit_count
= v_read(config
, &shmp(buf
->commit_hot
)[endidx
].cc
);
275 lib_ring_buffer_check_deliver(config
, buf
, chan
, offset_end
- 1,
276 commit_count
, endidx
);
278 * Update used size at each commit. It's needed only for extracting
279 * ring_buffer buffers from vmcore, after crash.
281 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
282 ctx
->buf_offset
, commit_count
,
287 * lib_ring_buffer_try_discard_reserve - Try discarding a record.
288 * @config: ring buffer instance configuration.
289 * @ctx: ring buffer context. (input arguments only)
291 * Only succeeds if no other record has been written after the record to
292 * discard. If discard fails, the record must be committed to the buffer.
294 * Returns 0 upon success, -EPERM if the record cannot be discarded.
297 int lib_ring_buffer_try_discard_reserve(const struct lib_ring_buffer_config
*config
,
298 const struct lib_ring_buffer_ctx
*ctx
)
300 struct lib_ring_buffer
*buf
= ctx
->buf
;
301 unsigned long end_offset
= ctx
->pre_offset
+ ctx
->slot_size
;
304 * We need to ensure that if the cmpxchg succeeds and discards the
305 * record, the next record will record a full TSC, because it cannot
306 * rely on the last_tsc associated with the discarded record to detect
307 * overflows. The only way to ensure this is to set the last_tsc to 0
308 * (assuming no 64-bit TSC overflow), which forces to write a 64-bit
309 * timestamp in the next record.
311 * Note: if discard fails, we must leave the TSC in the record header.
312 * It is needed to keep track of TSC overflows for the following
315 save_last_tsc(config
, buf
, 0ULL);
317 if (likely(v_cmpxchg(config
, &buf
->offset
, end_offset
, ctx
->pre_offset
)
325 void channel_record_disable(const struct lib_ring_buffer_config
*config
,
326 struct channel
*chan
)
328 uatomic_inc(&chan
->record_disabled
);
332 void channel_record_enable(const struct lib_ring_buffer_config
*config
,
333 struct channel
*chan
)
335 uatomic_dec(&chan
->record_disabled
);
339 void lib_ring_buffer_record_disable(const struct lib_ring_buffer_config
*config
,
340 struct lib_ring_buffer
*buf
)
342 uatomic_inc(&buf
->record_disabled
);
346 void lib_ring_buffer_record_enable(const struct lib_ring_buffer_config
*config
,
347 struct lib_ring_buffer
*buf
)
349 uatomic_dec(&buf
->record_disabled
);
352 #endif /* _LINUX_RING_BUFFER_FRONTEND_API_H */