Commit | Line | Data |
---|---|---|
a6352fd4 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: MIT |
a6352fd4 | 3 | * |
0466ac28 | 4 | * Copyright (C) 2010-2021 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
a6352fd4 MD |
5 | * |
6 | * Ring buffer configuration header. Note: after declaring the standard inline | |
7 | * functions, clients should also include linux/ringbuffer/api.h. | |
a6352fd4 MD |
8 | */ |
9 | ||
c0c0989a MJ |
10 | #ifndef _LTTNG_RING_BUFFER_CONFIG_H |
11 | #define _LTTNG_RING_BUFFER_CONFIG_H | |
12 | ||
a6352fd4 | 13 | #include <errno.h> |
b728d87e MD |
14 | #include <stdint.h> |
15 | #include <stddef.h> | |
16 | #include <urcu/arch.h> | |
2e707419 | 17 | #include <string.h> |
3d3a2bb8 | 18 | |
eae3c729 | 19 | #include <lttng/ust-utils.h> |
a8909ba5 | 20 | #include <lttng/ust-compiler.h> |
3d3a2bb8 | 21 | #include <lttng/ust-tracer.h> |
a6352fd4 | 22 | |
b5457df5 MD |
23 | struct lttng_ust_ring_buffer; |
24 | struct lttng_ust_ring_buffer_channel; | |
25 | struct lttng_ust_ring_buffer_config; | |
26 | struct lttng_ust_ring_buffer_ctx_private; | |
5a821cd6 | 27 | struct lttng_ust_shm_handle; |
a6352fd4 MD |
28 | |
29 | /* | |
30 | * Ring buffer client callbacks. Only used by slow path, never on fast path. | |
31 | * For the fast path, record_header_size(), ring_buffer_clock_read() should be | |
32 | * provided as inline functions too. These may simply return 0 if not used by | |
33 | * the client. | |
34 | */ | |
b5457df5 | 35 | struct lttng_ust_ring_buffer_client_cb { |
a6352fd4 MD |
36 | /* Mandatory callbacks */ |
37 | ||
38 | /* A static inline version is also required for fast path */ | |
b5457df5 MD |
39 | uint64_t (*ring_buffer_clock_read) (struct lttng_ust_ring_buffer_channel *chan); |
40 | size_t (*record_header_size) (const struct lttng_ust_ring_buffer_config *config, | |
41 | struct lttng_ust_ring_buffer_channel *chan, | |
5198080d | 42 | size_t offset, |
a6352fd4 | 43 | size_t *pre_header_padding, |
b5457df5 | 44 | struct lttng_ust_ring_buffer_ctx *ctx, |
e56bb47c | 45 | void *client_ctx); |
a6352fd4 MD |
46 | |
47 | /* Slow path only, at subbuffer switch */ | |
48 | size_t (*subbuffer_header_size) (void); | |
b5457df5 | 49 | void (*buffer_begin) (struct lttng_ust_ring_buffer *buf, uint64_t tsc, |
1d498196 | 50 | unsigned int subbuf_idx, |
38fae1d3 | 51 | struct lttng_ust_shm_handle *handle); |
b5457df5 | 52 | void (*buffer_end) (struct lttng_ust_ring_buffer *buf, uint64_t tsc, |
1d498196 | 53 | unsigned int subbuf_idx, unsigned long data_size, |
38fae1d3 | 54 | struct lttng_ust_shm_handle *handle); |
a6352fd4 MD |
55 | |
56 | /* Optional callbacks (can be set to NULL) */ | |
57 | ||
58 | /* Called at buffer creation/finalize */ | |
b5457df5 | 59 | int (*buffer_create) (struct lttng_ust_ring_buffer *buf, void *priv, |
1d498196 | 60 | int cpu, const char *name, |
38fae1d3 | 61 | struct lttng_ust_shm_handle *handle); |
a6352fd4 MD |
62 | /* |
63 | * Clients should guarantee that no new reader handle can be opened | |
64 | * after finalize. | |
65 | */ | |
b5457df5 | 66 | void (*buffer_finalize) (struct lttng_ust_ring_buffer *buf, |
1d498196 | 67 | void *priv, int cpu, |
38fae1d3 | 68 | struct lttng_ust_shm_handle *handle); |
a6352fd4 MD |
69 | |
70 | /* | |
71 | * Extract header length, payload length and timestamp from event | |
72 | * record. Used by buffer iterators. Timestamp is only used by channel | |
73 | * iterator. | |
74 | */ | |
b5457df5 MD |
75 | void (*record_get) (const struct lttng_ust_ring_buffer_config *config, |
76 | struct lttng_ust_ring_buffer_channel *chan, | |
77 | struct lttng_ust_ring_buffer *buf, | |
a6352fd4 | 78 | size_t offset, size_t *header_len, |
2fed87ae | 79 | size_t *payload_len, uint64_t *timestamp, |
38fae1d3 | 80 | struct lttng_ust_shm_handle *handle); |
a9ff648c MD |
81 | /* |
82 | * Offset and size of content size field in client. | |
83 | */ | |
b5457df5 | 84 | void (*content_size_field) (const struct lttng_ust_ring_buffer_config *config, |
a9ff648c | 85 | size_t *offset, size_t *length); |
b5457df5 | 86 | void (*packet_size_field) (const struct lttng_ust_ring_buffer_config *config, |
a9ff648c | 87 | size_t *offset, size_t *length); |
a6352fd4 MD |
88 | }; |
89 | ||
90 | /* | |
91 | * Ring buffer instance configuration. | |
92 | * | |
93 | * Declare as "static const" within the client object to ensure the inline fast | |
94 | * paths can be optimized. | |
95 | * | |
96 | * alloc/sync pairs: | |
97 | * | |
98 | * RING_BUFFER_ALLOC_PER_CPU and RING_BUFFER_SYNC_PER_CPU : | |
7489fcb4 | 99 | * Per-cpu buffers with per-cpu synchronization. |
a6352fd4 MD |
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 | */ | |
82b9bde8 | 127 | #define LTTNG_UST_RING_BUFFER_CONFIG_PADDING 20 |
46d52200 | 128 | |
b5457df5 | 129 | enum lttng_ust_ring_buffer_alloc_types { |
46d52200 ZT |
130 | RING_BUFFER_ALLOC_PER_CPU, |
131 | RING_BUFFER_ALLOC_GLOBAL, | |
132 | }; | |
133 | ||
b5457df5 | 134 | enum lttng_ust_ring_buffer_sync_types { |
46d52200 ZT |
135 | RING_BUFFER_SYNC_PER_CPU, /* Wait-free */ |
136 | RING_BUFFER_SYNC_GLOBAL, /* Lock-free */ | |
137 | }; | |
138 | ||
b5457df5 | 139 | enum lttng_ust_ring_buffer_mode_types { |
a9ff648c MD |
140 | RING_BUFFER_OVERWRITE = 0, /* Overwrite when buffer full */ |
141 | RING_BUFFER_DISCARD = 1, /* Discard when buffer full */ | |
46d52200 ZT |
142 | }; |
143 | ||
b5457df5 | 144 | enum lttng_ust_ring_buffer_output_types { |
46d52200 ZT |
145 | RING_BUFFER_SPLICE, |
146 | RING_BUFFER_MMAP, | |
147 | RING_BUFFER_READ, /* TODO */ | |
148 | RING_BUFFER_ITERATOR, | |
149 | RING_BUFFER_NONE, | |
150 | }; | |
151 | ||
b5457df5 | 152 | enum lttng_ust_ring_buffer_backend_types { |
46d52200 ZT |
153 | RING_BUFFER_PAGE, |
154 | RING_BUFFER_VMAP, /* TODO */ | |
155 | RING_BUFFER_STATIC, /* TODO */ | |
156 | }; | |
157 | ||
b5457df5 | 158 | enum lttng_ust_ring_buffer_oops_types { |
46d52200 ZT |
159 | RING_BUFFER_NO_OOPS_CONSISTENCY, |
160 | RING_BUFFER_OOPS_CONSISTENCY, | |
161 | }; | |
162 | ||
b5457df5 | 163 | enum lttng_ust_ring_buffer_ipi_types { |
46d52200 ZT |
164 | RING_BUFFER_IPI_BARRIER, |
165 | RING_BUFFER_NO_IPI_BARRIER, | |
166 | }; | |
167 | ||
b5457df5 | 168 | enum lttng_ust_ring_buffer_wakeup_types { |
46d52200 ZT |
169 | RING_BUFFER_WAKEUP_BY_TIMER, /* wake up performed by timer */ |
170 | RING_BUFFER_WAKEUP_BY_WRITER, /* | |
171 | * writer wakes up reader, | |
172 | * not lock-free | |
173 | * (takes spinlock). | |
174 | */ | |
175 | }; | |
176 | ||
b5457df5 MD |
177 | struct lttng_ust_ring_buffer_config { |
178 | enum lttng_ust_ring_buffer_alloc_types alloc; | |
179 | enum lttng_ust_ring_buffer_sync_types sync; | |
180 | enum lttng_ust_ring_buffer_mode_types mode; | |
181 | enum lttng_ust_ring_buffer_output_types output; | |
182 | enum lttng_ust_ring_buffer_backend_types backend; | |
183 | enum lttng_ust_ring_buffer_oops_types oops; | |
184 | enum lttng_ust_ring_buffer_ipi_types ipi; | |
185 | enum lttng_ust_ring_buffer_wakeup_types wakeup; | |
a6352fd4 MD |
186 | /* |
187 | * tsc_bits: timestamp bits saved at each record. | |
188 | * 0 and 64 disable the timestamp compression scheme. | |
189 | */ | |
190 | unsigned int tsc_bits; | |
b5457df5 | 191 | struct lttng_ust_ring_buffer_client_cb cb; |
c1fca457 MD |
192 | /* |
193 | * client_type is used by the consumer process (which is in a | |
194 | * different address space) to lookup the appropriate client | |
195 | * callbacks and update the cb pointers. | |
196 | */ | |
197 | int client_type; | |
82b9bde8 | 198 | int _unused1; |
b5457df5 | 199 | const struct lttng_ust_ring_buffer_client_cb *cb_ptr; |
2e707419 | 200 | char padding[LTTNG_UST_RING_BUFFER_CONFIG_PADDING]; |
a6352fd4 MD |
201 | }; |
202 | ||
a6352fd4 MD |
203 | /* |
204 | * Reservation flags. | |
205 | * | |
206 | * RING_BUFFER_RFLAG_FULL_TSC | |
207 | * | |
208 | * This flag is passed to record_header_size() and to the primitive used to | |
209 | * write the record header. It indicates that the full 64-bit time value is | |
210 | * needed in the record header. If this flag is not set, the record header needs | |
211 | * only to contain "tsc_bits" bit of time value. | |
212 | * | |
213 | * Reservation flags can be added by the client, starting from | |
214 | * "(RING_BUFFER_FLAGS_END << 0)". It can be used to pass information from | |
215 | * record_header_size() to lib_ring_buffer_write_record_header(). | |
216 | */ | |
217 | #define RING_BUFFER_RFLAG_FULL_TSC (1U << 0) | |
218 | #define RING_BUFFER_RFLAG_END (1U << 1) | |
219 | ||
a6352fd4 MD |
220 | /* |
221 | * lib_ring_buffer_check_config() returns 0 on success. | |
222 | * Used internally to check for valid configurations at channel creation. | |
223 | */ | |
106ff4da | 224 | static inline |
b5457df5 | 225 | int lib_ring_buffer_check_config(const struct lttng_ust_ring_buffer_config *config, |
a8909ba5 | 226 | unsigned int switch_timer_interval, |
106ff4da MJ |
227 | unsigned int read_timer_interval) |
228 | lttng_ust_notrace; | |
2208d8b5 | 229 | |
a6352fd4 | 230 | static inline |
b5457df5 | 231 | int lib_ring_buffer_check_config(const struct lttng_ust_ring_buffer_config *config, |
a6352fd4 | 232 | unsigned int switch_timer_interval, |
2208d8b5 | 233 | unsigned int read_timer_interval __attribute__((unused))) |
a6352fd4 MD |
234 | { |
235 | if (config->alloc == RING_BUFFER_ALLOC_GLOBAL | |
236 | && config->sync == RING_BUFFER_SYNC_PER_CPU | |
237 | && switch_timer_interval) | |
238 | return -EINVAL; | |
239 | return 0; | |
240 | } | |
241 | ||
e92f3e28 | 242 | #endif /* _LTTNG_RING_BUFFER_CONFIG_H */ |