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