Move lttng_ust_object_data et lttng_ust_channel_attr to -abi.h
[lttng-ust.git] / libringbuffer / frontend_api.h
CommitLineData
852c2936
MD
1#ifndef _LINUX_RING_BUFFER_FRONTEND_API_H
2#define _LINUX_RING_BUFFER_FRONTEND_API_H
3
4/*
5 * linux/ringbuffer/frontend_api.h
6 *
7 * (C) Copyright 2005-2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * Ring Buffer Library Synchronization Header (buffer write API).
10 *
11 * Author:
12 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 *
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.
16 *
17 * Dual LGPL v2.1/GPL v2 license.
18 */
19
4931a13e 20#include "frontend.h"
9f3fdbc6
MD
21#include "ust/core.h"
22#include <urcu-bp.h>
23#include <urcu/compiler.h>
852c2936
MD
24
25/**
26 * lib_ring_buffer_get_cpu - Precedes ring buffer reserve/commit.
27 *
9f3fdbc6
MD
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).
852c2936
MD
32 *
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
37 * section.
38 */
39static inline
40int lib_ring_buffer_get_cpu(const struct lib_ring_buffer_config *config)
41{
42 int cpu, nesting;
43
9f3fdbc6
MD
44 rcu_read_lock();
45 cpu = ust_get_cpu();
b9b858b8 46 nesting = ++lib_ring_buffer_nesting; /* TLS */
9f3fdbc6 47 cmm_barrier();
852c2936
MD
48
49 if (unlikely(nesting > 4)) {
50 WARN_ON_ONCE(1);
b9b858b8 51 lib_ring_buffer_nesting--; /* TLS */
9f3fdbc6 52 rcu_read_unlock();
852c2936
MD
53 return -EPERM;
54 } else
55 return cpu;
56}
57
58/**
59 * lib_ring_buffer_put_cpu - Follows ring buffer reserve/commit.
60 */
61static inline
62void lib_ring_buffer_put_cpu(const struct lib_ring_buffer_config *config)
63{
9f3fdbc6 64 cmm_barrier();
b9b858b8 65 lib_ring_buffer_nesting--; /* TLS */
9f3fdbc6 66 rcu_read_unlock();
852c2936
MD
67}
68
69/*
70 * lib_ring_buffer_try_reserve is called by lib_ring_buffer_reserve(). It is not
71 * part of the API per se.
72 *
73 * returns 0 if reserve ok, or 1 if the slow path must be taken.
74 */
75static inline
76int 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)
80{
81 struct channel *chan = ctx->chan;
82 struct lib_ring_buffer *buf = ctx->buf;
83 *o_begin = v_read(config, &buf->offset);
84 *o_old = *o_begin;
85
86 ctx->tsc = lib_ring_buffer_clock_read(chan);
87 if ((int64_t) ctx->tsc == -EIO)
88 return 1;
89
90 /*
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
93 * the commit counter.
94 */
9f3fdbc6 95 //prefetch(&buf->commit_hot[subbuf_index(*o_begin, chan)]);
852c2936
MD
96
97 if (last_tsc_overflow(config, buf, ctx->tsc))
98 ctx->rflags |= RING_BUFFER_RFLAG_FULL_TSC;
99
100 if (unlikely(subbuf_offset(*o_begin, chan) == 0))
101 return 1;
102
103 ctx->slot_size = record_header_size(config, chan, *o_begin,
104 before_hdr_pad, ctx);
105 ctx->slot_size +=
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))
110 return 1;
111
112 /*
113 * Record fits in the current buffer and we are not on a switch
114 * boundary. It's safe to write.
115 */
116 *o_end = *o_begin + ctx->slot_size;
117
118 if (unlikely((subbuf_offset(*o_end, chan)) == 0))
119 /*
120 * The offset_end will fall at the very beginning of the next
121 * subbuffer.
122 */
123 return 1;
124
125 return 0;
126}
127
128/**
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.
132 *
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".
135 *
136 * Return :
137 * 0 on success.
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.
142 */
143
144static inline
145int lib_ring_buffer_reserve(const struct lib_ring_buffer_config *config,
146 struct lib_ring_buffer_ctx *ctx)
147{
148 struct channel *chan = ctx->chan;
1d498196 149 struct shm_handle *handle = ctx->handle;
852c2936
MD
150 struct lib_ring_buffer *buf;
151 unsigned long o_begin, o_end, o_old;
152 size_t before_hdr_pad = 0;
153
9f3fdbc6 154 if (uatomic_read(&chan->record_disabled))
852c2936
MD
155 return -EAGAIN;
156
157 if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
1d498196 158 buf = shmp(handle, chan->backend.buf[ctx->cpu].shmp);
852c2936 159 else
1d498196 160 buf = shmp(handle, chan->backend.buf[0].shmp);
9f3fdbc6 161 if (uatomic_read(&buf->record_disabled))
852c2936
MD
162 return -EAGAIN;
163 ctx->buf = buf;
164
165 /*
166 * Perform retryable operations.
167 */
168 if (unlikely(lib_ring_buffer_try_reserve(config, ctx, &o_begin,
169 &o_end, &o_old, &before_hdr_pad)))
170 goto slow_path;
171
172 if (unlikely(v_cmpxchg(config, &ctx->buf->offset, o_old, o_end)
173 != o_old))
174 goto slow_path;
175
176 /*
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).
181 */
182 save_last_tsc(config, ctx->buf, ctx->tsc);
183
184 /*
185 * Push the reader if necessary
186 */
187 lib_ring_buffer_reserve_push_reader(ctx->buf, chan, o_end - 1);
188
189 /*
190 * Clear noref flag for this subbuffer.
191 */
192 lib_ring_buffer_clear_noref(config, &ctx->buf->backend,
1d498196 193 subbuf_index(o_end - 1, chan), handle);
852c2936
MD
194
195 ctx->pre_offset = o_begin;
196 ctx->buf_offset = o_begin + before_hdr_pad;
197 return 0;
198slow_path:
199 return lib_ring_buffer_reserve_slow(ctx);
200}
201
202/**
203 * lib_ring_buffer_switch - Perform a sub-buffer switch for a per-cpu buffer.
204 * @config: ring buffer instance configuration.
205 * @buf: buffer
206 * @mode: buffer switch mode (SWITCH_ACTIVE or SWITCH_FLUSH)
207 *
208 * This operation is completely reentrant : can be called while tracing is
209 * active with absolutely no lock held.
210 *
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.
215 */
216static inline
217void lib_ring_buffer_switch(const struct lib_ring_buffer_config *config,
1d498196
MD
218 struct lib_ring_buffer *buf, enum switch_mode mode,
219 struct shm_handle *handle)
852c2936 220{
1d498196 221 lib_ring_buffer_switch_slow(buf, mode, handle);
852c2936
MD
222}
223
224/* See ring_buffer_frontend_api.h for lib_ring_buffer_reserve(). */
225
226/**
227 * lib_ring_buffer_commit - Commit an record.
228 * @config: ring buffer instance configuration.
229 * @ctx: ring buffer context. (input arguments only)
230 *
231 * Atomic unordered slot commit. Increments the commit count in the
232 * specified sub-buffer, and delivers it if necessary.
233 */
234static inline
235void lib_ring_buffer_commit(const struct lib_ring_buffer_config *config,
236 const struct lib_ring_buffer_ctx *ctx)
237{
238 struct channel *chan = ctx->chan;
1d498196 239 struct shm_handle *handle = ctx->handle;
852c2936
MD
240 struct 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;
244
245 /*
246 * Must count record before incrementing the commit count.
247 */
1d498196 248 subbuffer_count_record(config, &buf->backend, endidx, handle);
852c2936
MD
249
250 /*
251 * Order all writes to buffer before the commit count update that will
252 * determine that the subbuffer is full.
253 */
9f3fdbc6 254 cmm_smp_wmb();
852c2936 255
4746ae29 256 v_add(config, ctx->slot_size, &shmp_index(handle, buf->commit_hot, endidx)->cc);
852c2936
MD
257
258 /*
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
264 * cause :
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.
275 */
4746ae29 276 commit_count = v_read(config, &shmp_index(handle, buf->commit_hot, endidx)->cc);
852c2936
MD
277
278 lib_ring_buffer_check_deliver(config, buf, chan, offset_end - 1,
1d498196 279 commit_count, endidx, handle);
852c2936
MD
280 /*
281 * Update used size at each commit. It's needed only for extracting
282 * ring_buffer buffers from vmcore, after crash.
283 */
284 lib_ring_buffer_write_commit_counter(config, buf, chan, endidx,
285 ctx->buf_offset, commit_count,
1d498196 286 ctx->slot_size, handle);
852c2936
MD
287}
288
289/**
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)
293 *
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.
296 *
297 * Returns 0 upon success, -EPERM if the record cannot be discarded.
298 */
299static inline
300int lib_ring_buffer_try_discard_reserve(const struct lib_ring_buffer_config *config,
301 const struct lib_ring_buffer_ctx *ctx)
302{
303 struct lib_ring_buffer *buf = ctx->buf;
304 unsigned long end_offset = ctx->pre_offset + ctx->slot_size;
305
306 /*
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.
313 *
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
316 * records.
317 */
318 save_last_tsc(config, buf, 0ULL);
319
320 if (likely(v_cmpxchg(config, &buf->offset, end_offset, ctx->pre_offset)
321 != end_offset))
322 return -EPERM;
323 else
324 return 0;
325}
326
327static inline
328void channel_record_disable(const struct lib_ring_buffer_config *config,
329 struct channel *chan)
330{
9f3fdbc6 331 uatomic_inc(&chan->record_disabled);
852c2936
MD
332}
333
334static inline
335void channel_record_enable(const struct lib_ring_buffer_config *config,
336 struct channel *chan)
337{
9f3fdbc6 338 uatomic_dec(&chan->record_disabled);
852c2936
MD
339}
340
341static inline
342void lib_ring_buffer_record_disable(const struct lib_ring_buffer_config *config,
343 struct lib_ring_buffer *buf)
344{
9f3fdbc6 345 uatomic_inc(&buf->record_disabled);
852c2936
MD
346}
347
348static inline
349void lib_ring_buffer_record_enable(const struct lib_ring_buffer_config *config,
350 struct lib_ring_buffer *buf)
351{
9f3fdbc6 352 uatomic_dec(&buf->record_disabled);
852c2936
MD
353}
354
355#endif /* _LINUX_RING_BUFFER_FRONTEND_API_H */
This page took 0.036686 seconds and 4 git commands to generate.