Performance: cache the backend pages pointer in context
[lttng-ust.git] / include / lttng / ringbuffer-config.h
CommitLineData
e92f3e28
MD
1#ifndef _LTTNG_RING_BUFFER_CONFIG_H
2#define _LTTNG_RING_BUFFER_CONFIG_H
a6352fd4
MD
3
4/*
e92f3e28 5 * lttng/ringbuffer-config.h
a6352fd4
MD
6 *
7 * Copyright (C) 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * Ring buffer configuration header. Note: after declaring the standard inline
10 * functions, clients should also include linux/ringbuffer/api.h.
11 *
e92f3e28
MD
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
a60d70e6 18 *
e92f3e28
MD
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
d2428e87
MD
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 * SOFTWARE.
a6352fd4
MD
29 */
30
31#include <errno.h>
4318ae1b 32#include "lttng/ust-tracer.h"
b728d87e
MD
33#include <stdint.h>
34#include <stddef.h>
35#include <urcu/arch.h>
2e707419 36#include <string.h>
4318ae1b 37#include "lttng/align.h"
a8909ba5 38#include <lttng/ust-compiler.h>
a6352fd4 39
4cfec15c 40struct lttng_ust_lib_ring_buffer;
a6352fd4 41struct channel;
4cfec15c
MD
42struct lttng_ust_lib_ring_buffer_config;
43struct lttng_ust_lib_ring_buffer_ctx;
5a821cd6 44struct lttng_ust_shm_handle;
a6352fd4
MD
45
46/*
47 * Ring buffer client callbacks. Only used by slow path, never on fast path.
48 * For the fast path, record_header_size(), ring_buffer_clock_read() should be
49 * provided as inline functions too. These may simply return 0 if not used by
50 * the client.
51 */
4cfec15c 52struct lttng_ust_lib_ring_buffer_client_cb {
a6352fd4
MD
53 /* Mandatory callbacks */
54
55 /* A static inline version is also required for fast path */
2fed87ae 56 uint64_t (*ring_buffer_clock_read) (struct channel *chan);
4cfec15c 57 size_t (*record_header_size) (const struct lttng_ust_lib_ring_buffer_config *config,
a6352fd4
MD
58 struct channel *chan, size_t offset,
59 size_t *pre_header_padding,
4cfec15c 60 struct lttng_ust_lib_ring_buffer_ctx *ctx);
a6352fd4
MD
61
62 /* Slow path only, at subbuffer switch */
63 size_t (*subbuffer_header_size) (void);
2fed87ae 64 void (*buffer_begin) (struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc,
1d498196 65 unsigned int subbuf_idx,
38fae1d3 66 struct lttng_ust_shm_handle *handle);
2fed87ae 67 void (*buffer_end) (struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc,
1d498196 68 unsigned int subbuf_idx, unsigned long data_size,
38fae1d3 69 struct lttng_ust_shm_handle *handle);
a6352fd4
MD
70
71 /* Optional callbacks (can be set to NULL) */
72
73 /* Called at buffer creation/finalize */
4cfec15c 74 int (*buffer_create) (struct lttng_ust_lib_ring_buffer *buf, void *priv,
1d498196 75 int cpu, const char *name,
38fae1d3 76 struct lttng_ust_shm_handle *handle);
a6352fd4
MD
77 /*
78 * Clients should guarantee that no new reader handle can be opened
79 * after finalize.
80 */
4cfec15c 81 void (*buffer_finalize) (struct lttng_ust_lib_ring_buffer *buf,
1d498196 82 void *priv, int cpu,
38fae1d3 83 struct lttng_ust_shm_handle *handle);
a6352fd4
MD
84
85 /*
86 * Extract header length, payload length and timestamp from event
87 * record. Used by buffer iterators. Timestamp is only used by channel
88 * iterator.
89 */
4cfec15c
MD
90 void (*record_get) (const struct lttng_ust_lib_ring_buffer_config *config,
91 struct channel *chan, struct lttng_ust_lib_ring_buffer *buf,
a6352fd4 92 size_t offset, size_t *header_len,
2fed87ae 93 size_t *payload_len, uint64_t *timestamp,
38fae1d3 94 struct lttng_ust_shm_handle *handle);
a9ff648c
MD
95 /*
96 * Offset and size of content size field in client.
97 */
98 void (*content_size_field) (const struct lttng_ust_lib_ring_buffer_config *config,
99 size_t *offset, size_t *length);
100 void (*packet_size_field) (const struct lttng_ust_lib_ring_buffer_config *config,
101 size_t *offset, size_t *length);
a6352fd4
MD
102};
103
104/*
105 * Ring buffer instance configuration.
106 *
107 * Declare as "static const" within the client object to ensure the inline fast
108 * paths can be optimized.
109 *
110 * alloc/sync pairs:
111 *
112 * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_PER_CPU :
113 * Per-cpu buffers with per-cpu synchronization. Tracing must be performed
114 * with preemption disabled (lib_ring_buffer_get_cpu() and
115 * lib_ring_buffer_put_cpu()).
116 *
117 * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_GLOBAL :
118 * Per-cpu buffer with global synchronization. Tracing can be performed with
119 * preemption enabled, statistically stays on the local buffers.
120 *
121 * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_PER_CPU :
122 * Should only be used for buffers belonging to a single thread or protected
123 * by mutual exclusion by the client. Note that periodical sub-buffer switch
124 * should be disabled in this kind of configuration.
125 *
126 * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_GLOBAL :
127 * Global shared buffer with global synchronization.
128 *
129 * wakeup:
130 *
131 * RING_BUFFER_WAKEUP_BY_TIMER uses per-cpu deferrable timers to poll the
132 * buffers and wake up readers if data is ready. Mainly useful for tracers which
133 * don't want to call into the wakeup code on the tracing path. Use in
134 * combination with "read_timer_interval" channel_create() argument.
135 *
136 * RING_BUFFER_WAKEUP_BY_WRITER directly wakes up readers when a subbuffer is
137 * ready to read. Lower latencies before the reader is woken up. Mainly suitable
138 * for drivers.
139 *
140 * RING_BUFFER_WAKEUP_NONE does not perform any wakeup whatsoever. The client
141 * has the responsibility to perform wakeups.
142 */
82b9bde8 143#define LTTNG_UST_RING_BUFFER_CONFIG_PADDING 20
46d52200
ZT
144
145enum lttng_ust_lib_ring_buffer_alloc_types {
146 RING_BUFFER_ALLOC_PER_CPU,
147 RING_BUFFER_ALLOC_GLOBAL,
148};
149
150enum lttng_ust_lib_ring_buffer_sync_types {
151 RING_BUFFER_SYNC_PER_CPU, /* Wait-free */
152 RING_BUFFER_SYNC_GLOBAL, /* Lock-free */
153};
154
155enum lttng_ust_lib_ring_buffer_mode_types {
a9ff648c
MD
156 RING_BUFFER_OVERWRITE = 0, /* Overwrite when buffer full */
157 RING_BUFFER_DISCARD = 1, /* Discard when buffer full */
46d52200
ZT
158};
159
160enum lttng_ust_lib_ring_buffer_output_types {
161 RING_BUFFER_SPLICE,
162 RING_BUFFER_MMAP,
163 RING_BUFFER_READ, /* TODO */
164 RING_BUFFER_ITERATOR,
165 RING_BUFFER_NONE,
166};
167
168enum lttng_ust_lib_ring_buffer_backend_types {
169 RING_BUFFER_PAGE,
170 RING_BUFFER_VMAP, /* TODO */
171 RING_BUFFER_STATIC, /* TODO */
172};
173
174enum lttng_ust_lib_ring_buffer_oops_types {
175 RING_BUFFER_NO_OOPS_CONSISTENCY,
176 RING_BUFFER_OOPS_CONSISTENCY,
177};
178
179enum lttng_ust_lib_ring_buffer_ipi_types {
180 RING_BUFFER_IPI_BARRIER,
181 RING_BUFFER_NO_IPI_BARRIER,
182};
183
184enum lttng_ust_lib_ring_buffer_wakeup_types {
185 RING_BUFFER_WAKEUP_BY_TIMER, /* wake up performed by timer */
186 RING_BUFFER_WAKEUP_BY_WRITER, /*
187 * writer wakes up reader,
188 * not lock-free
189 * (takes spinlock).
190 */
191};
192
4cfec15c 193struct lttng_ust_lib_ring_buffer_config {
46d52200
ZT
194 enum lttng_ust_lib_ring_buffer_alloc_types alloc;
195 enum lttng_ust_lib_ring_buffer_sync_types sync;
196 enum lttng_ust_lib_ring_buffer_mode_types mode;
197 enum lttng_ust_lib_ring_buffer_output_types output;
198 enum lttng_ust_lib_ring_buffer_backend_types backend;
199 enum lttng_ust_lib_ring_buffer_oops_types oops;
200 enum lttng_ust_lib_ring_buffer_ipi_types ipi;
201 enum lttng_ust_lib_ring_buffer_wakeup_types wakeup;
a6352fd4
MD
202 /*
203 * tsc_bits: timestamp bits saved at each record.
204 * 0 and 64 disable the timestamp compression scheme.
205 */
206 unsigned int tsc_bits;
4cfec15c 207 struct lttng_ust_lib_ring_buffer_client_cb cb;
c1fca457
MD
208 /*
209 * client_type is used by the consumer process (which is in a
210 * different address space) to lookup the appropriate client
211 * callbacks and update the cb pointers.
212 */
213 int client_type;
82b9bde8
JD
214 int _unused1;
215 const struct lttng_ust_lib_ring_buffer_client_cb *cb_ptr;
2e707419 216 char padding[LTTNG_UST_RING_BUFFER_CONFIG_PADDING];
a6352fd4
MD
217};
218
219/*
220 * ring buffer context
221 *
222 * Context passed to lib_ring_buffer_reserve(), lib_ring_buffer_commit(),
223 * lib_ring_buffer_try_discard_reserve(), lib_ring_buffer_align_ctx() and
224 * lib_ring_buffer_write().
180901e6
MD
225 *
226 * IMPORTANT: this structure is part of the ABI between the probe and
227 * UST. Fields need to be only added at the end, never reordered, never
228 * removed.
a6352fd4 229 */
96f85541 230#define LTTNG_UST_RING_BUFFER_CTX_PADDING \
53569322 231 (24 - sizeof(int) - sizeof(void *) - sizeof(void *))
4cfec15c 232struct lttng_ust_lib_ring_buffer_ctx {
a6352fd4
MD
233 /* input received by lib_ring_buffer_reserve(), saved here. */
234 struct channel *chan; /* channel */
235 void *priv; /* client private data */
38fae1d3 236 struct lttng_ust_shm_handle *handle; /* shared-memory handle */
a6352fd4
MD
237 size_t data_size; /* size of payload */
238 int largest_align; /*
239 * alignment of the largest element
240 * in the payload
241 */
242 int cpu; /* processor id */
243
244 /* output from lib_ring_buffer_reserve() */
4cfec15c 245 struct lttng_ust_lib_ring_buffer *buf; /*
a6352fd4
MD
246 * buffer corresponding to processor id
247 * for this channel
248 */
249 size_t slot_size; /* size of the reserved slot */
250 unsigned long buf_offset; /* offset following the record header */
251 unsigned long pre_offset; /*
252 * Initial offset position _before_
253 * the record is written. Positioned
254 * prior to record header alignment
255 * padding.
256 */
2fed87ae 257 uint64_t tsc; /* time-stamp counter value */
a6352fd4 258 unsigned int rflags; /* reservation flags */
82cd0cfd
MD
259 /*
260 * The field ctx_len is the length of struct
261 * lttng_ust_lib_ring_buffer_ctx as known by the user of
262 * lib_ring_buffer_ctx_init.
263 */
264 unsigned int ctx_len;
96f85541 265 void *ip; /* caller ip address */
53569322 266 void *priv2; /* 2nd priv data */
96f85541 267 char padding2[LTTNG_UST_RING_BUFFER_CTX_PADDING];
82cd0cfd
MD
268 /*
269 * This is the end of the initial fields expected by the original ABI
270 * between probes and UST. Only the fields above can be used if
271 * ctx_len is 0. Use the value of ctx_len to find out which of the
272 * following fields may be used.
273 */
a3492932 274 struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages;
a6352fd4
MD
275};
276
277/**
278 * lib_ring_buffer_ctx_init - initialize ring buffer context
279 * @ctx: ring buffer context to initialize
280 * @chan: channel
281 * @priv: client private data
282 * @data_size: size of record data payload
283 * @largest_align: largest alignment within data payload types
284 * @cpu: processor id
285 */
a8909ba5
PW
286static inline lttng_ust_notrace
287void lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx *ctx,
288 struct channel *chan, void *priv,
289 size_t data_size, int largest_align,
53569322
MD
290 int cpu, struct lttng_ust_shm_handle *handle,
291 void *priv2);
a6352fd4 292static inline
4cfec15c 293void lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx *ctx,
a6352fd4
MD
294 struct channel *chan, void *priv,
295 size_t data_size, int largest_align,
53569322
MD
296 int cpu, struct lttng_ust_shm_handle *handle,
297 void *priv2)
a6352fd4
MD
298{
299 ctx->chan = chan;
300 ctx->priv = priv;
301 ctx->data_size = data_size;
302 ctx->largest_align = largest_align;
303 ctx->cpu = cpu;
304 ctx->rflags = 0;
1d498196 305 ctx->handle = handle;
82cd0cfd 306 ctx->ctx_len = sizeof(struct lttng_ust_lib_ring_buffer_ctx);
96f85541 307 ctx->ip = 0;
53569322 308 ctx->priv2 = priv2;
96f85541 309 memset(ctx->padding2, 0, LTTNG_UST_RING_BUFFER_CTX_PADDING);
a6352fd4
MD
310}
311
312/*
313 * Reservation flags.
314 *
315 * RING_BUFFER_RFLAG_FULL_TSC
316 *
317 * This flag is passed to record_header_size() and to the primitive used to
318 * write the record header. It indicates that the full 64-bit time value is
319 * needed in the record header. If this flag is not set, the record header needs
320 * only to contain "tsc_bits" bit of time value.
321 *
322 * Reservation flags can be added by the client, starting from
323 * "(RING_BUFFER_FLAGS_END << 0)". It can be used to pass information from
324 * record_header_size() to lib_ring_buffer_write_record_header().
325 */
326#define RING_BUFFER_RFLAG_FULL_TSC (1U << 0)
327#define RING_BUFFER_RFLAG_END (1U << 1)
328
329/*
330 * We need to define RING_BUFFER_ALIGN_ATTR so it is known early at
331 * compile-time. We have to duplicate the "config->align" information and the
332 * definition here because config->align is used both in the slow and fast
333 * paths, but RING_BUFFER_ALIGN_ATTR is only available for the client code.
334 */
335#ifdef RING_BUFFER_ALIGN
336
337# define RING_BUFFER_ALIGN_ATTR /* Default arch alignment */
338
339/*
340 * Calculate the offset needed to align the type.
341 * size_of_type must be non-zero.
342 */
a8909ba5
PW
343static inline lttng_ust_notrace
344unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type);
a6352fd4
MD
345static inline
346unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type)
347{
348 return offset_align(align_drift, size_of_type);
349}
350
351#else
352
353# define RING_BUFFER_ALIGN_ATTR __attribute__((packed))
354
355/*
356 * Calculate the offset needed to align the type.
357 * size_of_type must be non-zero.
358 */
a8909ba5
PW
359static inline lttng_ust_notrace
360unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type);
a6352fd4
MD
361static inline
362unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type)
363{
364 return 0;
365}
366
367#endif
368
369/**
370 * lib_ring_buffer_align_ctx - Align context offset on "alignment"
371 * @ctx: ring buffer context.
372 */
a8909ba5
PW
373static inline lttng_ust_notrace
374void lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx *ctx,
375 size_t alignment);
a6352fd4 376static inline
4cfec15c 377void lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx *ctx,
a6352fd4
MD
378 size_t alignment)
379{
380 ctx->buf_offset += lib_ring_buffer_align(ctx->buf_offset,
381 alignment);
382}
383
384/*
385 * lib_ring_buffer_check_config() returns 0 on success.
386 * Used internally to check for valid configurations at channel creation.
387 */
a8909ba5
PW
388static inline lttng_ust_notrace
389int lib_ring_buffer_check_config(const struct lttng_ust_lib_ring_buffer_config *config,
390 unsigned int switch_timer_interval,
391 unsigned int read_timer_interval);
a6352fd4 392static inline
4cfec15c 393int lib_ring_buffer_check_config(const struct lttng_ust_lib_ring_buffer_config *config,
a6352fd4
MD
394 unsigned int switch_timer_interval,
395 unsigned int read_timer_interval)
396{
397 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL
398 && config->sync == RING_BUFFER_SYNC_PER_CPU
399 && switch_timer_interval)
400 return -EINVAL;
401 return 0;
402}
403
e92f3e28 404#endif /* _LTTNG_RING_BUFFER_CONFIG_H */
This page took 0.047595 seconds and 4 git commands to generate.