License text standardization, add missing licenses
[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.
a6352fd4
MD
21 */
22
23#include <errno.h>
4318ae1b 24#include "lttng/ust-tracer.h"
b728d87e
MD
25#include <stdint.h>
26#include <stddef.h>
27#include <urcu/arch.h>
2e707419 28#include <string.h>
4318ae1b 29#include "lttng/align.h"
a6352fd4 30
4cfec15c 31struct lttng_ust_lib_ring_buffer;
a6352fd4 32struct channel;
4cfec15c
MD
33struct lttng_ust_lib_ring_buffer_config;
34struct lttng_ust_lib_ring_buffer_ctx;
38fae1d3 35struct lttng_ust_shm_handle *handle;
a6352fd4
MD
36
37/*
38 * Ring buffer client callbacks. Only used by slow path, never on fast path.
39 * For the fast path, record_header_size(), ring_buffer_clock_read() should be
40 * provided as inline functions too. These may simply return 0 if not used by
41 * the client.
42 */
4cfec15c 43struct lttng_ust_lib_ring_buffer_client_cb {
a6352fd4
MD
44 /* Mandatory callbacks */
45
46 /* A static inline version is also required for fast path */
2fed87ae 47 uint64_t (*ring_buffer_clock_read) (struct channel *chan);
4cfec15c 48 size_t (*record_header_size) (const struct lttng_ust_lib_ring_buffer_config *config,
a6352fd4
MD
49 struct channel *chan, size_t offset,
50 size_t *pre_header_padding,
4cfec15c 51 struct lttng_ust_lib_ring_buffer_ctx *ctx);
a6352fd4
MD
52
53 /* Slow path only, at subbuffer switch */
54 size_t (*subbuffer_header_size) (void);
2fed87ae 55 void (*buffer_begin) (struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc,
1d498196 56 unsigned int subbuf_idx,
38fae1d3 57 struct lttng_ust_shm_handle *handle);
2fed87ae 58 void (*buffer_end) (struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc,
1d498196 59 unsigned int subbuf_idx, unsigned long data_size,
38fae1d3 60 struct lttng_ust_shm_handle *handle);
a6352fd4
MD
61
62 /* Optional callbacks (can be set to NULL) */
63
64 /* Called at buffer creation/finalize */
4cfec15c 65 int (*buffer_create) (struct lttng_ust_lib_ring_buffer *buf, void *priv,
1d498196 66 int cpu, const char *name,
38fae1d3 67 struct lttng_ust_shm_handle *handle);
a6352fd4
MD
68 /*
69 * Clients should guarantee that no new reader handle can be opened
70 * after finalize.
71 */
4cfec15c 72 void (*buffer_finalize) (struct lttng_ust_lib_ring_buffer *buf,
1d498196 73 void *priv, int cpu,
38fae1d3 74 struct lttng_ust_shm_handle *handle);
a6352fd4
MD
75
76 /*
77 * Extract header length, payload length and timestamp from event
78 * record. Used by buffer iterators. Timestamp is only used by channel
79 * iterator.
80 */
4cfec15c
MD
81 void (*record_get) (const struct lttng_ust_lib_ring_buffer_config *config,
82 struct channel *chan, struct lttng_ust_lib_ring_buffer *buf,
a6352fd4 83 size_t offset, size_t *header_len,
2fed87ae 84 size_t *payload_len, uint64_t *timestamp,
38fae1d3 85 struct lttng_ust_shm_handle *handle);
a6352fd4
MD
86};
87
88/*
89 * Ring buffer instance configuration.
90 *
91 * Declare as "static const" within the client object to ensure the inline fast
92 * paths can be optimized.
93 *
94 * alloc/sync pairs:
95 *
96 * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_PER_CPU :
97 * Per-cpu buffers with per-cpu synchronization. Tracing must be performed
98 * with preemption disabled (lib_ring_buffer_get_cpu() and
99 * lib_ring_buffer_put_cpu()).
100 *
101 * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_GLOBAL :
102 * Per-cpu buffer with global synchronization. Tracing can be performed with
103 * preemption enabled, statistically stays on the local buffers.
104 *
105 * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_PER_CPU :
106 * Should only be used for buffers belonging to a single thread or protected
107 * by mutual exclusion by the client. Note that periodical sub-buffer switch
108 * should be disabled in this kind of configuration.
109 *
110 * RING_BUFFER_ALLOC_GLOBAL and RING_BUFFER_SYNC_GLOBAL :
111 * Global shared buffer with global synchronization.
112 *
113 * wakeup:
114 *
115 * RING_BUFFER_WAKEUP_BY_TIMER uses per-cpu deferrable timers to poll the
116 * buffers and wake up readers if data is ready. Mainly useful for tracers which
117 * don't want to call into the wakeup code on the tracing path. Use in
118 * combination with "read_timer_interval" channel_create() argument.
119 *
120 * RING_BUFFER_WAKEUP_BY_WRITER directly wakes up readers when a subbuffer is
121 * ready to read. Lower latencies before the reader is woken up. Mainly suitable
122 * for drivers.
123 *
124 * RING_BUFFER_WAKEUP_NONE does not perform any wakeup whatsoever. The client
125 * has the responsibility to perform wakeups.
126 */
2e707419 127#define LTTNG_UST_RING_BUFFER_CONFIG_PADDING 32
4cfec15c 128struct lttng_ust_lib_ring_buffer_config {
a6352fd4
MD
129 enum {
130 RING_BUFFER_ALLOC_PER_CPU,
131 RING_BUFFER_ALLOC_GLOBAL,
132 } alloc;
133 enum {
134 RING_BUFFER_SYNC_PER_CPU, /* Wait-free */
135 RING_BUFFER_SYNC_GLOBAL, /* Lock-free */
136 } sync;
137 enum {
138 RING_BUFFER_OVERWRITE, /* Overwrite when buffer full */
139 RING_BUFFER_DISCARD, /* Discard when buffer full */
140 } mode;
141 enum {
142 RING_BUFFER_SPLICE,
143 RING_BUFFER_MMAP,
144 RING_BUFFER_READ, /* TODO */
145 RING_BUFFER_ITERATOR,
146 RING_BUFFER_NONE,
147 } output;
148 enum {
149 RING_BUFFER_PAGE,
150 RING_BUFFER_VMAP, /* TODO */
151 RING_BUFFER_STATIC, /* TODO */
152 } backend;
153 enum {
154 RING_BUFFER_NO_OOPS_CONSISTENCY,
155 RING_BUFFER_OOPS_CONSISTENCY,
156 } oops;
157 enum {
158 RING_BUFFER_IPI_BARRIER,
159 RING_BUFFER_NO_IPI_BARRIER,
160 } ipi;
161 enum {
162 RING_BUFFER_WAKEUP_BY_TIMER, /* wake up performed by timer */
163 RING_BUFFER_WAKEUP_BY_WRITER, /*
164 * writer wakes up reader,
165 * not lock-free
166 * (takes spinlock).
167 */
168 } wakeup;
169 /*
170 * tsc_bits: timestamp bits saved at each record.
171 * 0 and 64 disable the timestamp compression scheme.
172 */
173 unsigned int tsc_bits;
4cfec15c 174 struct lttng_ust_lib_ring_buffer_client_cb cb;
c1fca457
MD
175 /*
176 * client_type is used by the consumer process (which is in a
177 * different address space) to lookup the appropriate client
178 * callbacks and update the cb pointers.
179 */
180 int client_type;
2e707419 181 char padding[LTTNG_UST_RING_BUFFER_CONFIG_PADDING];
a6352fd4
MD
182};
183
184/*
185 * ring buffer context
186 *
187 * Context passed to lib_ring_buffer_reserve(), lib_ring_buffer_commit(),
188 * lib_ring_buffer_try_discard_reserve(), lib_ring_buffer_align_ctx() and
189 * lib_ring_buffer_write().
190 */
2e707419 191#define LTTNG_UST_RING_BUFFER_CTX_PADDING 24
4cfec15c 192struct lttng_ust_lib_ring_buffer_ctx {
a6352fd4
MD
193 /* input received by lib_ring_buffer_reserve(), saved here. */
194 struct channel *chan; /* channel */
195 void *priv; /* client private data */
38fae1d3 196 struct lttng_ust_shm_handle *handle; /* shared-memory handle */
a6352fd4
MD
197 size_t data_size; /* size of payload */
198 int largest_align; /*
199 * alignment of the largest element
200 * in the payload
201 */
202 int cpu; /* processor id */
203
204 /* output from lib_ring_buffer_reserve() */
4cfec15c 205 struct lttng_ust_lib_ring_buffer *buf; /*
a6352fd4
MD
206 * buffer corresponding to processor id
207 * for this channel
208 */
209 size_t slot_size; /* size of the reserved slot */
210 unsigned long buf_offset; /* offset following the record header */
211 unsigned long pre_offset; /*
212 * Initial offset position _before_
213 * the record is written. Positioned
214 * prior to record header alignment
215 * padding.
216 */
2fed87ae 217 uint64_t tsc; /* time-stamp counter value */
a6352fd4 218 unsigned int rflags; /* reservation flags */
2e707419 219 char padding[LTTNG_UST_RING_BUFFER_CTX_PADDING];
a6352fd4
MD
220};
221
222/**
223 * lib_ring_buffer_ctx_init - initialize ring buffer context
224 * @ctx: ring buffer context to initialize
225 * @chan: channel
226 * @priv: client private data
227 * @data_size: size of record data payload
228 * @largest_align: largest alignment within data payload types
229 * @cpu: processor id
230 */
231static inline
4cfec15c 232void lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx *ctx,
a6352fd4
MD
233 struct channel *chan, void *priv,
234 size_t data_size, int largest_align,
38fae1d3 235 int cpu, struct lttng_ust_shm_handle *handle)
a6352fd4
MD
236{
237 ctx->chan = chan;
238 ctx->priv = priv;
239 ctx->data_size = data_size;
240 ctx->largest_align = largest_align;
241 ctx->cpu = cpu;
242 ctx->rflags = 0;
1d498196 243 ctx->handle = handle;
2e707419 244 memset(ctx->padding, 0, LTTNG_UST_RING_BUFFER_CTX_PADDING);
a6352fd4
MD
245}
246
247/*
248 * Reservation flags.
249 *
250 * RING_BUFFER_RFLAG_FULL_TSC
251 *
252 * This flag is passed to record_header_size() and to the primitive used to
253 * write the record header. It indicates that the full 64-bit time value is
254 * needed in the record header. If this flag is not set, the record header needs
255 * only to contain "tsc_bits" bit of time value.
256 *
257 * Reservation flags can be added by the client, starting from
258 * "(RING_BUFFER_FLAGS_END << 0)". It can be used to pass information from
259 * record_header_size() to lib_ring_buffer_write_record_header().
260 */
261#define RING_BUFFER_RFLAG_FULL_TSC (1U << 0)
262#define RING_BUFFER_RFLAG_END (1U << 1)
263
264/*
265 * We need to define RING_BUFFER_ALIGN_ATTR so it is known early at
266 * compile-time. We have to duplicate the "config->align" information and the
267 * definition here because config->align is used both in the slow and fast
268 * paths, but RING_BUFFER_ALIGN_ATTR is only available for the client code.
269 */
270#ifdef RING_BUFFER_ALIGN
271
272# define RING_BUFFER_ALIGN_ATTR /* Default arch alignment */
273
274/*
275 * Calculate the offset needed to align the type.
276 * size_of_type must be non-zero.
277 */
278static inline
279unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type)
280{
281 return offset_align(align_drift, size_of_type);
282}
283
284#else
285
286# define RING_BUFFER_ALIGN_ATTR __attribute__((packed))
287
288/*
289 * Calculate the offset needed to align the type.
290 * size_of_type must be non-zero.
291 */
292static inline
293unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type)
294{
295 return 0;
296}
297
298#endif
299
300/**
301 * lib_ring_buffer_align_ctx - Align context offset on "alignment"
302 * @ctx: ring buffer context.
303 */
304static inline
4cfec15c 305void lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx *ctx,
a6352fd4
MD
306 size_t alignment)
307{
308 ctx->buf_offset += lib_ring_buffer_align(ctx->buf_offset,
309 alignment);
310}
311
312/*
313 * lib_ring_buffer_check_config() returns 0 on success.
314 * Used internally to check for valid configurations at channel creation.
315 */
316static inline
4cfec15c 317int lib_ring_buffer_check_config(const struct lttng_ust_lib_ring_buffer_config *config,
a6352fd4
MD
318 unsigned int switch_timer_interval,
319 unsigned int read_timer_interval)
320{
321 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL
322 && config->sync == RING_BUFFER_SYNC_PER_CPU
323 && switch_timer_interval)
324 return -EINVAL;
325 return 0;
326}
327
e92f3e28 328#endif /* _LTTNG_RING_BUFFER_CONFIG_H */
This page took 0.037882 seconds and 4 git commands to generate.