callsite: add "ip" context
[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 * IMPORTANT: this structure is part of the ABI between the probe and
193 * UST. Fields need to be only added at the end, never reordered, never
194 * removed.
195 */
196 #define LTTNG_UST_RING_BUFFER_CTX_PADDING \
197 (24 - sizeof(int) - sizeof(void *))
198 struct lttng_ust_lib_ring_buffer_ctx {
199 /* input received by lib_ring_buffer_reserve(), saved here. */
200 struct channel *chan; /* channel */
201 void *priv; /* client private data */
202 struct lttng_ust_shm_handle *handle; /* shared-memory handle */
203 size_t data_size; /* size of payload */
204 int largest_align; /*
205 * alignment of the largest element
206 * in the payload
207 */
208 int cpu; /* processor id */
209
210 /* output from lib_ring_buffer_reserve() */
211 struct lttng_ust_lib_ring_buffer *buf; /*
212 * buffer corresponding to processor id
213 * for this channel
214 */
215 size_t slot_size; /* size of the reserved slot */
216 unsigned long buf_offset; /* offset following the record header */
217 unsigned long pre_offset; /*
218 * Initial offset position _before_
219 * the record is written. Positioned
220 * prior to record header alignment
221 * padding.
222 */
223 uint64_t tsc; /* time-stamp counter value */
224 unsigned int rflags; /* reservation flags */
225 unsigned int padding1; /* padding to realign on pointer */
226 void *ip; /* caller ip address */
227 char padding2[LTTNG_UST_RING_BUFFER_CTX_PADDING];
228 };
229
230 /**
231 * lib_ring_buffer_ctx_init - initialize ring buffer context
232 * @ctx: ring buffer context to initialize
233 * @chan: channel
234 * @priv: client private data
235 * @data_size: size of record data payload
236 * @largest_align: largest alignment within data payload types
237 * @cpu: processor id
238 */
239 static inline lttng_ust_notrace
240 void lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx *ctx,
241 struct channel *chan, void *priv,
242 size_t data_size, int largest_align,
243 int cpu, struct lttng_ust_shm_handle *handle);
244 static inline
245 void lib_ring_buffer_ctx_init(struct lttng_ust_lib_ring_buffer_ctx *ctx,
246 struct channel *chan, void *priv,
247 size_t data_size, int largest_align,
248 int cpu, struct lttng_ust_shm_handle *handle)
249 {
250 ctx->chan = chan;
251 ctx->priv = priv;
252 ctx->data_size = data_size;
253 ctx->largest_align = largest_align;
254 ctx->cpu = cpu;
255 ctx->rflags = 0;
256 ctx->handle = handle;
257 ctx->padding1 = 0;
258 ctx->ip = 0;
259 memset(ctx->padding2, 0, LTTNG_UST_RING_BUFFER_CTX_PADDING);
260 }
261
262 /*
263 * Reservation flags.
264 *
265 * RING_BUFFER_RFLAG_FULL_TSC
266 *
267 * This flag is passed to record_header_size() and to the primitive used to
268 * write the record header. It indicates that the full 64-bit time value is
269 * needed in the record header. If this flag is not set, the record header needs
270 * only to contain "tsc_bits" bit of time value.
271 *
272 * Reservation flags can be added by the client, starting from
273 * "(RING_BUFFER_FLAGS_END << 0)". It can be used to pass information from
274 * record_header_size() to lib_ring_buffer_write_record_header().
275 */
276 #define RING_BUFFER_RFLAG_FULL_TSC (1U << 0)
277 #define RING_BUFFER_RFLAG_END (1U << 1)
278
279 /*
280 * We need to define RING_BUFFER_ALIGN_ATTR so it is known early at
281 * compile-time. We have to duplicate the "config->align" information and the
282 * definition here because config->align is used both in the slow and fast
283 * paths, but RING_BUFFER_ALIGN_ATTR is only available for the client code.
284 */
285 #ifdef RING_BUFFER_ALIGN
286
287 # define RING_BUFFER_ALIGN_ATTR /* Default arch alignment */
288
289 /*
290 * Calculate the offset needed to align the type.
291 * size_of_type must be non-zero.
292 */
293 static inline lttng_ust_notrace
294 unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type);
295 static inline
296 unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type)
297 {
298 return offset_align(align_drift, size_of_type);
299 }
300
301 #else
302
303 # define RING_BUFFER_ALIGN_ATTR __attribute__((packed))
304
305 /*
306 * Calculate the offset needed to align the type.
307 * size_of_type must be non-zero.
308 */
309 static inline lttng_ust_notrace
310 unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type);
311 static inline
312 unsigned int lib_ring_buffer_align(size_t align_drift, size_t size_of_type)
313 {
314 return 0;
315 }
316
317 #endif
318
319 /**
320 * lib_ring_buffer_align_ctx - Align context offset on "alignment"
321 * @ctx: ring buffer context.
322 */
323 static inline lttng_ust_notrace
324 void lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx *ctx,
325 size_t alignment);
326 static inline
327 void lib_ring_buffer_align_ctx(struct lttng_ust_lib_ring_buffer_ctx *ctx,
328 size_t alignment)
329 {
330 ctx->buf_offset += lib_ring_buffer_align(ctx->buf_offset,
331 alignment);
332 }
333
334 /*
335 * lib_ring_buffer_check_config() returns 0 on success.
336 * Used internally to check for valid configurations at channel creation.
337 */
338 static inline lttng_ust_notrace
339 int lib_ring_buffer_check_config(const struct lttng_ust_lib_ring_buffer_config *config,
340 unsigned int switch_timer_interval,
341 unsigned int read_timer_interval);
342 static inline
343 int lib_ring_buffer_check_config(const struct lttng_ust_lib_ring_buffer_config *config,
344 unsigned int switch_timer_interval,
345 unsigned int read_timer_interval)
346 {
347 if (config->alloc == RING_BUFFER_ALLOC_GLOBAL
348 && config->sync == RING_BUFFER_SYNC_PER_CPU
349 && switch_timer_interval)
350 return -EINVAL;
351 return 0;
352 }
353
354 #endif /* _LTTNG_RING_BUFFER_CONFIG_H */
This page took 0.065153 seconds and 5 git commands to generate.