Commit | Line | Data |
---|---|---|
852c2936 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
852c2936 | 3 | * |
e92f3e28 MD |
4 | * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
5 | * | |
c0c0989a | 6 | * Ring buffer backend (internal helpers). |
852c2936 MD |
7 | */ |
8 | ||
c0c0989a MJ |
9 | #ifndef _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H |
10 | #define _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H | |
11 | ||
b4051ad8 | 12 | #include <stddef.h> |
fb31eb73 | 13 | #include <stdint.h> |
14641deb MD |
14 | #include <unistd.h> |
15 | #include <urcu/compiler.h> | |
16 | ||
4318ae1b | 17 | #include <lttng/ringbuffer-config.h> |
4931a13e MD |
18 | #include "backend_types.h" |
19 | #include "frontend_types.h" | |
a6352fd4 | 20 | #include "shm.h" |
071dec43 | 21 | #include "ust-helper.h" |
852c2936 MD |
22 | |
23 | /* Ring buffer backend API presented to the frontend */ | |
24 | ||
25 | /* Ring buffer and channel backend create/free */ | |
26 | ||
071dec43 | 27 | LTTNG_HIDDEN |
4cfec15c | 28 | int lib_ring_buffer_backend_create(struct lttng_ust_lib_ring_buffer_backend *bufb, |
a6352fd4 | 29 | struct channel_backend *chan, int cpu, |
38fae1d3 | 30 | struct lttng_ust_shm_handle *handle, |
1d498196 | 31 | struct shm_object *shmobj); |
071dec43 | 32 | LTTNG_HIDDEN |
852c2936 | 33 | void channel_backend_unregister_notifiers(struct channel_backend *chanb); |
071dec43 | 34 | LTTNG_HIDDEN |
4cfec15c | 35 | void lib_ring_buffer_backend_free(struct lttng_ust_lib_ring_buffer_backend *bufb); |
071dec43 | 36 | LTTNG_HIDDEN |
852c2936 MD |
37 | int channel_backend_init(struct channel_backend *chanb, |
38 | const char *name, | |
4cfec15c | 39 | const struct lttng_ust_lib_ring_buffer_config *config, |
a3f61e7f | 40 | size_t subbuf_size, |
a9ff648c | 41 | size_t num_subbuf, struct lttng_ust_shm_handle *handle, |
5ea386c3 | 42 | const int *stream_fds); |
071dec43 | 43 | LTTNG_HIDDEN |
1d498196 | 44 | void channel_backend_free(struct channel_backend *chanb, |
38fae1d3 | 45 | struct lttng_ust_shm_handle *handle); |
852c2936 | 46 | |
071dec43 | 47 | LTTNG_HIDDEN |
4cfec15c | 48 | void lib_ring_buffer_backend_reset(struct lttng_ust_lib_ring_buffer_backend *bufb, |
38fae1d3 | 49 | struct lttng_ust_shm_handle *handle); |
071dec43 | 50 | LTTNG_HIDDEN |
852c2936 MD |
51 | void channel_backend_reset(struct channel_backend *chanb); |
52 | ||
071dec43 | 53 | LTTNG_HIDDEN |
852c2936 | 54 | int lib_ring_buffer_backend_init(void); |
071dec43 | 55 | LTTNG_HIDDEN |
852c2936 MD |
56 | void lib_ring_buffer_backend_exit(void); |
57 | ||
071dec43 | 58 | LTTNG_HIDDEN |
4cfec15c | 59 | extern void _lib_ring_buffer_write(struct lttng_ust_lib_ring_buffer_backend *bufb, |
852c2936 MD |
60 | size_t offset, const void *src, size_t len, |
61 | ssize_t pagecpy); | |
62 | ||
63 | /* | |
64 | * Subbuffer ID bits for overwrite mode. Need to fit within a single word to be | |
65 | * exchanged atomically. | |
66 | * | |
67 | * Top half word, except lowest bit, belongs to "offset", which is used to keep | |
68 | * to count the produced buffers. For overwrite mode, this provides the | |
69 | * consumer with the capacity to read subbuffers in order, handling the | |
70 | * situation where producers would write up to 2^15 buffers (or 2^31 for 64-bit | |
71 | * systems) concurrently with a single execution of get_subbuf (between offset | |
72 | * sampling and subbuffer ID exchange). | |
73 | */ | |
74 | ||
14641deb | 75 | #define HALF_ULONG_BITS (CAA_BITS_PER_LONG >> 1) |
852c2936 MD |
76 | |
77 | #define SB_ID_OFFSET_SHIFT (HALF_ULONG_BITS + 1) | |
78 | #define SB_ID_OFFSET_COUNT (1UL << SB_ID_OFFSET_SHIFT) | |
79 | #define SB_ID_OFFSET_MASK (~(SB_ID_OFFSET_COUNT - 1)) | |
80 | /* | |
81 | * Lowest bit of top word half belongs to noref. Used only for overwrite mode. | |
82 | */ | |
83 | #define SB_ID_NOREF_SHIFT (SB_ID_OFFSET_SHIFT - 1) | |
84 | #define SB_ID_NOREF_COUNT (1UL << SB_ID_NOREF_SHIFT) | |
85 | #define SB_ID_NOREF_MASK SB_ID_NOREF_COUNT | |
86 | /* | |
87 | * In overwrite mode: lowest half of word is used for index. | |
88 | * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit. | |
89 | * In producer-consumer mode: whole word used for index. | |
90 | */ | |
91 | #define SB_ID_INDEX_SHIFT 0 | |
92 | #define SB_ID_INDEX_COUNT (1UL << SB_ID_INDEX_SHIFT) | |
93 | #define SB_ID_INDEX_MASK (SB_ID_NOREF_COUNT - 1) | |
94 | ||
95 | /* | |
96 | * Construct the subbuffer id from offset, index and noref. Use only the index | |
97 | * for producer-consumer mode (offset and noref are only used in overwrite | |
98 | * mode). | |
99 | */ | |
100 | static inline | |
4cfec15c | 101 | unsigned long subbuffer_id(const struct lttng_ust_lib_ring_buffer_config *config, |
852c2936 MD |
102 | unsigned long offset, unsigned long noref, |
103 | unsigned long index) | |
104 | { | |
105 | if (config->mode == RING_BUFFER_OVERWRITE) | |
106 | return (offset << SB_ID_OFFSET_SHIFT) | |
107 | | (noref << SB_ID_NOREF_SHIFT) | |
108 | | index; | |
109 | else | |
110 | return index; | |
111 | } | |
112 | ||
113 | /* | |
114 | * Compare offset with the offset contained within id. Return 1 if the offset | |
115 | * bits are identical, else 0. | |
116 | */ | |
117 | static inline | |
4cfec15c | 118 | int subbuffer_id_compare_offset(const struct lttng_ust_lib_ring_buffer_config *config, |
852c2936 MD |
119 | unsigned long id, unsigned long offset) |
120 | { | |
121 | return (id & SB_ID_OFFSET_MASK) == (offset << SB_ID_OFFSET_SHIFT); | |
122 | } | |
123 | ||
124 | static inline | |
4cfec15c | 125 | unsigned long subbuffer_id_get_index(const struct lttng_ust_lib_ring_buffer_config *config, |
852c2936 MD |
126 | unsigned long id) |
127 | { | |
128 | if (config->mode == RING_BUFFER_OVERWRITE) | |
129 | return id & SB_ID_INDEX_MASK; | |
130 | else | |
131 | return id; | |
132 | } | |
133 | ||
134 | static inline | |
4cfec15c | 135 | unsigned long subbuffer_id_is_noref(const struct lttng_ust_lib_ring_buffer_config *config, |
852c2936 MD |
136 | unsigned long id) |
137 | { | |
138 | if (config->mode == RING_BUFFER_OVERWRITE) | |
139 | return !!(id & SB_ID_NOREF_MASK); | |
140 | else | |
141 | return 1; | |
142 | } | |
143 | ||
144 | /* | |
145 | * Only used by reader on subbuffer ID it has exclusive access to. No volatile | |
146 | * needed. | |
147 | */ | |
148 | static inline | |
4cfec15c | 149 | void subbuffer_id_set_noref(const struct lttng_ust_lib_ring_buffer_config *config, |
852c2936 MD |
150 | unsigned long *id) |
151 | { | |
152 | if (config->mode == RING_BUFFER_OVERWRITE) | |
153 | *id |= SB_ID_NOREF_MASK; | |
154 | } | |
155 | ||
156 | static inline | |
4cfec15c | 157 | void subbuffer_id_set_noref_offset(const struct lttng_ust_lib_ring_buffer_config *config, |
852c2936 MD |
158 | unsigned long *id, unsigned long offset) |
159 | { | |
160 | unsigned long tmp; | |
161 | ||
162 | if (config->mode == RING_BUFFER_OVERWRITE) { | |
163 | tmp = *id; | |
164 | tmp &= ~SB_ID_OFFSET_MASK; | |
165 | tmp |= offset << SB_ID_OFFSET_SHIFT; | |
166 | tmp |= SB_ID_NOREF_MASK; | |
167 | /* Volatile store, read concurrently by readers. */ | |
14641deb | 168 | CMM_ACCESS_ONCE(*id) = tmp; |
852c2936 MD |
169 | } |
170 | } | |
171 | ||
172 | /* No volatile access, since already used locally */ | |
173 | static inline | |
4cfec15c | 174 | void subbuffer_id_clear_noref(const struct lttng_ust_lib_ring_buffer_config *config, |
852c2936 MD |
175 | unsigned long *id) |
176 | { | |
177 | if (config->mode == RING_BUFFER_OVERWRITE) | |
178 | *id &= ~SB_ID_NOREF_MASK; | |
179 | } | |
180 | ||
181 | /* | |
182 | * For overwrite mode, cap the number of subbuffers per buffer to: | |
183 | * 2^16 on 32-bit architectures | |
184 | * 2^32 on 64-bit architectures | |
185 | * This is required to fit in the index part of the ID. Return 0 on success, | |
186 | * -EPERM on failure. | |
187 | */ | |
188 | static inline | |
4cfec15c | 189 | int subbuffer_id_check_index(const struct lttng_ust_lib_ring_buffer_config *config, |
852c2936 MD |
190 | unsigned long num_subbuf) |
191 | { | |
192 | if (config->mode == RING_BUFFER_OVERWRITE) | |
193 | return (num_subbuf > (1UL << HALF_ULONG_BITS)) ? -EPERM : 0; | |
194 | else | |
195 | return 0; | |
196 | } | |
197 | ||
a3492932 MD |
198 | static inline |
199 | int lib_ring_buffer_backend_get_pages(const struct lttng_ust_lib_ring_buffer_config *config, | |
200 | struct lttng_ust_lib_ring_buffer_ctx *ctx, | |
201 | struct lttng_ust_lib_ring_buffer_backend_pages **backend_pages) | |
202 | { | |
203 | struct lttng_ust_lib_ring_buffer_backend *bufb = &ctx->buf->backend; | |
204 | struct channel_backend *chanb = &ctx->chan->backend; | |
205 | struct lttng_ust_shm_handle *handle = ctx->handle; | |
206 | size_t sbidx; | |
207 | size_t offset = ctx->buf_offset; | |
208 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; | |
209 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; | |
210 | unsigned long sb_bindex, id; | |
211 | struct lttng_ust_lib_ring_buffer_backend_pages *_backend_pages; | |
212 | ||
213 | offset &= chanb->buf_size - 1; | |
214 | sbidx = offset >> chanb->subbuf_size_order; | |
215 | wsb = shmp_index(handle, bufb->buf_wsb, sbidx); | |
216 | if (caa_unlikely(!wsb)) | |
217 | return -1; | |
218 | id = wsb->id; | |
219 | sb_bindex = subbuffer_id_get_index(config, id); | |
220 | rpages = shmp_index(handle, bufb->array, sb_bindex); | |
221 | if (caa_unlikely(!rpages)) | |
222 | return -1; | |
223 | CHAN_WARN_ON(ctx->chan, | |
224 | config->mode == RING_BUFFER_OVERWRITE | |
225 | && subbuffer_id_is_noref(config, id)); | |
226 | _backend_pages = shmp(handle, rpages->shmp); | |
227 | if (caa_unlikely(!_backend_pages)) | |
228 | return -1; | |
229 | *backend_pages = _backend_pages; | |
230 | return 0; | |
231 | } | |
232 | ||
233 | /* Get backend pages from cache. */ | |
234 | static inline | |
235 | struct lttng_ust_lib_ring_buffer_backend_pages * | |
236 | lib_ring_buffer_get_backend_pages_from_ctx(const struct lttng_ust_lib_ring_buffer_config *config, | |
237 | struct lttng_ust_lib_ring_buffer_ctx *ctx) | |
238 | { | |
a3492932 MD |
239 | return ctx->backend_pages; |
240 | } | |
241 | ||
9c995331 MD |
242 | /* |
243 | * The ring buffer can count events recorded and overwritten per buffer, | |
244 | * but it is disabled by default due to its performance overhead. | |
245 | */ | |
246 | #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS | |
852c2936 | 247 | static inline |
4cfec15c | 248 | void subbuffer_count_record(const struct lttng_ust_lib_ring_buffer_config *config, |
15500a1b | 249 | const struct lttng_ust_lib_ring_buffer_ctx *ctx, |
4cfec15c | 250 | struct lttng_ust_lib_ring_buffer_backend *bufb, |
38fae1d3 | 251 | unsigned long idx, struct lttng_ust_shm_handle *handle) |
852c2936 | 252 | { |
15500a1b | 253 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; |
852c2936 | 254 | |
15500a1b MD |
255 | backend_pages = lib_ring_buffer_get_backend_pages_from_ctx(config, ctx); |
256 | if (caa_unlikely(!backend_pages)) { | |
257 | if (lib_ring_buffer_backend_get_pages(config, ctx, &backend_pages)) | |
258 | return; | |
259 | } | |
260 | v_inc(config, &backend_pages->records_commit); | |
852c2936 | 261 | } |
9c995331 MD |
262 | #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */ |
263 | static inline | |
264 | void subbuffer_count_record(const struct lttng_ust_lib_ring_buffer_config *config, | |
15500a1b | 265 | const struct lttng_ust_lib_ring_buffer_ctx *ctx, |
9c995331 MD |
266 | struct lttng_ust_lib_ring_buffer_backend *bufb, |
267 | unsigned long idx, struct lttng_ust_shm_handle *handle) | |
268 | { | |
269 | } | |
270 | #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */ | |
852c2936 MD |
271 | |
272 | /* | |
273 | * Reader has exclusive subbuffer access for record consumption. No need to | |
274 | * perform the decrement atomically. | |
275 | */ | |
276 | static inline | |
4cfec15c MD |
277 | void subbuffer_consume_record(const struct lttng_ust_lib_ring_buffer_config *config, |
278 | struct lttng_ust_lib_ring_buffer_backend *bufb, | |
38fae1d3 | 279 | struct lttng_ust_shm_handle *handle) |
852c2936 MD |
280 | { |
281 | unsigned long sb_bindex; | |
15500a1b MD |
282 | struct channel *chan; |
283 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages_shmp; | |
284 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; | |
852c2936 MD |
285 | |
286 | sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id); | |
15500a1b MD |
287 | chan = shmp(handle, bufb->chan); |
288 | if (!chan) | |
289 | return; | |
290 | pages_shmp = shmp_index(handle, bufb->array, sb_bindex); | |
291 | if (!pages_shmp) | |
292 | return; | |
293 | backend_pages = shmp(handle, pages_shmp->shmp); | |
294 | if (!backend_pages) | |
295 | return; | |
296 | CHAN_WARN_ON(chan, !v_read(config, &backend_pages->records_unread)); | |
852c2936 | 297 | /* Non-atomic decrement protected by exclusive subbuffer access */ |
15500a1b | 298 | _v_dec(config, &backend_pages->records_unread); |
852c2936 MD |
299 | v_inc(config, &bufb->records_read); |
300 | } | |
301 | ||
302 | static inline | |
303 | unsigned long subbuffer_get_records_count( | |
4cfec15c MD |
304 | const struct lttng_ust_lib_ring_buffer_config *config, |
305 | struct lttng_ust_lib_ring_buffer_backend *bufb, | |
1d498196 | 306 | unsigned long idx, |
38fae1d3 | 307 | struct lttng_ust_shm_handle *handle) |
852c2936 MD |
308 | { |
309 | unsigned long sb_bindex; | |
15500a1b MD |
310 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
311 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; | |
312 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; | |
852c2936 | 313 | |
15500a1b MD |
314 | wsb = shmp_index(handle, bufb->buf_wsb, idx); |
315 | if (!wsb) | |
316 | return 0; | |
317 | sb_bindex = subbuffer_id_get_index(config, wsb->id); | |
318 | rpages = shmp_index(handle, bufb->array, sb_bindex); | |
319 | if (!rpages) | |
320 | return 0; | |
321 | backend_pages = shmp(handle, rpages->shmp); | |
322 | if (!backend_pages) | |
323 | return 0; | |
324 | return v_read(config, &backend_pages->records_commit); | |
852c2936 MD |
325 | } |
326 | ||
327 | /* | |
328 | * Must be executed at subbuffer delivery when the writer has _exclusive_ | |
3d1aec25 MD |
329 | * subbuffer access. See lib_ring_buffer_check_deliver() for details. |
330 | * lib_ring_buffer_get_records_count() must be called to get the records | |
331 | * count before this function, because it resets the records_commit | |
332 | * count. | |
852c2936 MD |
333 | */ |
334 | static inline | |
335 | unsigned long subbuffer_count_records_overrun( | |
4cfec15c MD |
336 | const struct lttng_ust_lib_ring_buffer_config *config, |
337 | struct lttng_ust_lib_ring_buffer_backend *bufb, | |
1d498196 | 338 | unsigned long idx, |
38fae1d3 | 339 | struct lttng_ust_shm_handle *handle) |
852c2936 | 340 | { |
852c2936 | 341 | unsigned long overruns, sb_bindex; |
15500a1b MD |
342 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
343 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; | |
344 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; | |
852c2936 | 345 | |
15500a1b MD |
346 | wsb = shmp_index(handle, bufb->buf_wsb, idx); |
347 | if (!wsb) | |
348 | return 0; | |
349 | sb_bindex = subbuffer_id_get_index(config, wsb->id); | |
350 | rpages = shmp_index(handle, bufb->array, sb_bindex); | |
351 | if (!rpages) | |
352 | return 0; | |
353 | backend_pages = shmp(handle, rpages->shmp); | |
354 | if (!backend_pages) | |
355 | return 0; | |
356 | overruns = v_read(config, &backend_pages->records_unread); | |
357 | v_set(config, &backend_pages->records_unread, | |
358 | v_read(config, &backend_pages->records_commit)); | |
359 | v_set(config, &backend_pages->records_commit, 0); | |
852c2936 MD |
360 | |
361 | return overruns; | |
362 | } | |
363 | ||
364 | static inline | |
4cfec15c MD |
365 | void subbuffer_set_data_size(const struct lttng_ust_lib_ring_buffer_config *config, |
366 | struct lttng_ust_lib_ring_buffer_backend *bufb, | |
852c2936 | 367 | unsigned long idx, |
1d498196 | 368 | unsigned long data_size, |
38fae1d3 | 369 | struct lttng_ust_shm_handle *handle) |
852c2936 | 370 | { |
852c2936 | 371 | unsigned long sb_bindex; |
15500a1b MD |
372 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
373 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; | |
374 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; | |
852c2936 | 375 | |
15500a1b MD |
376 | wsb = shmp_index(handle, bufb->buf_wsb, idx); |
377 | if (!wsb) | |
378 | return; | |
379 | sb_bindex = subbuffer_id_get_index(config, wsb->id); | |
380 | rpages = shmp_index(handle, bufb->array, sb_bindex); | |
381 | if (!rpages) | |
382 | return; | |
383 | backend_pages = shmp(handle, rpages->shmp); | |
384 | if (!backend_pages) | |
385 | return; | |
386 | backend_pages->data_size = data_size; | |
852c2936 MD |
387 | } |
388 | ||
389 | static inline | |
390 | unsigned long subbuffer_get_read_data_size( | |
4cfec15c MD |
391 | const struct lttng_ust_lib_ring_buffer_config *config, |
392 | struct lttng_ust_lib_ring_buffer_backend *bufb, | |
38fae1d3 | 393 | struct lttng_ust_shm_handle *handle) |
852c2936 | 394 | { |
852c2936 | 395 | unsigned long sb_bindex; |
15500a1b MD |
396 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages_shmp; |
397 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; | |
852c2936 MD |
398 | |
399 | sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id); | |
15500a1b MD |
400 | pages_shmp = shmp_index(handle, bufb->array, sb_bindex); |
401 | if (!pages_shmp) | |
402 | return 0; | |
403 | backend_pages = shmp(handle, pages_shmp->shmp); | |
404 | if (!backend_pages) | |
405 | return 0; | |
406 | return backend_pages->data_size; | |
852c2936 MD |
407 | } |
408 | ||
409 | static inline | |
410 | unsigned long subbuffer_get_data_size( | |
4cfec15c MD |
411 | const struct lttng_ust_lib_ring_buffer_config *config, |
412 | struct lttng_ust_lib_ring_buffer_backend *bufb, | |
1d498196 | 413 | unsigned long idx, |
38fae1d3 | 414 | struct lttng_ust_shm_handle *handle) |
852c2936 | 415 | { |
852c2936 | 416 | unsigned long sb_bindex; |
15500a1b MD |
417 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
418 | struct lttng_ust_lib_ring_buffer_backend_pages_shmp *rpages; | |
419 | struct lttng_ust_lib_ring_buffer_backend_pages *backend_pages; | |
852c2936 | 420 | |
15500a1b MD |
421 | wsb = shmp_index(handle, bufb->buf_wsb, idx); |
422 | if (!wsb) | |
423 | return 0; | |
424 | sb_bindex = subbuffer_id_get_index(config, wsb->id); | |
425 | rpages = shmp_index(handle, bufb->array, sb_bindex); | |
426 | if (!rpages) | |
427 | return 0; | |
428 | backend_pages = shmp(handle, rpages->shmp); | |
429 | if (!backend_pages) | |
430 | return 0; | |
431 | return backend_pages->data_size; | |
852c2936 MD |
432 | } |
433 | ||
1ff31389 JD |
434 | static inline |
435 | void subbuffer_inc_packet_count(const struct lttng_ust_lib_ring_buffer_config *config, | |
436 | struct lttng_ust_lib_ring_buffer_backend *bufb, | |
437 | unsigned long idx, struct lttng_ust_shm_handle *handle) | |
438 | { | |
15500a1b MD |
439 | struct lttng_ust_lib_ring_buffer_backend_counts *counts; |
440 | ||
441 | counts = shmp_index(handle, bufb->buf_cnt, idx); | |
442 | if (!counts) | |
443 | return; | |
444 | counts->seq_cnt++; | |
1ff31389 JD |
445 | } |
446 | ||
852c2936 MD |
447 | /** |
448 | * lib_ring_buffer_clear_noref - Clear the noref subbuffer flag, called by | |
449 | * writer. | |
450 | */ | |
451 | static inline | |
4cfec15c MD |
452 | void lib_ring_buffer_clear_noref(const struct lttng_ust_lib_ring_buffer_config *config, |
453 | struct lttng_ust_lib_ring_buffer_backend *bufb, | |
1d498196 | 454 | unsigned long idx, |
38fae1d3 | 455 | struct lttng_ust_shm_handle *handle) |
852c2936 MD |
456 | { |
457 | unsigned long id, new_id; | |
15500a1b | 458 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
852c2936 MD |
459 | |
460 | if (config->mode != RING_BUFFER_OVERWRITE) | |
461 | return; | |
462 | ||
463 | /* | |
464 | * Performing a volatile access to read the sb_pages, because we want to | |
465 | * read a coherent version of the pointer and the associated noref flag. | |
466 | */ | |
15500a1b MD |
467 | wsb = shmp_index(handle, bufb->buf_wsb, idx); |
468 | if (!wsb) | |
469 | return; | |
470 | id = CMM_ACCESS_ONCE(wsb->id); | |
852c2936 MD |
471 | for (;;) { |
472 | /* This check is called on the fast path for each record. */ | |
b5a3dfa5 | 473 | if (caa_likely(!subbuffer_id_is_noref(config, id))) { |
852c2936 MD |
474 | /* |
475 | * Store after load dependency ordering the writes to | |
476 | * the subbuffer after load and test of the noref flag | |
477 | * matches the memory barrier implied by the cmpxchg() | |
478 | * in update_read_sb_index(). | |
479 | */ | |
480 | return; /* Already writing to this buffer */ | |
481 | } | |
482 | new_id = id; | |
483 | subbuffer_id_clear_noref(config, &new_id); | |
15500a1b | 484 | new_id = uatomic_cmpxchg(&wsb->id, id, new_id); |
b5a3dfa5 | 485 | if (caa_likely(new_id == id)) |
852c2936 MD |
486 | break; |
487 | id = new_id; | |
488 | } | |
489 | } | |
490 | ||
491 | /** | |
492 | * lib_ring_buffer_set_noref_offset - Set the noref subbuffer flag and offset, | |
493 | * called by writer. | |
494 | */ | |
495 | static inline | |
4cfec15c MD |
496 | void lib_ring_buffer_set_noref_offset(const struct lttng_ust_lib_ring_buffer_config *config, |
497 | struct lttng_ust_lib_ring_buffer_backend *bufb, | |
1d498196 | 498 | unsigned long idx, unsigned long offset, |
38fae1d3 | 499 | struct lttng_ust_shm_handle *handle) |
852c2936 | 500 | { |
15500a1b MD |
501 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
502 | struct channel *chan; | |
503 | ||
852c2936 MD |
504 | if (config->mode != RING_BUFFER_OVERWRITE) |
505 | return; | |
506 | ||
15500a1b MD |
507 | wsb = shmp_index(handle, bufb->buf_wsb, idx); |
508 | if (!wsb) | |
509 | return; | |
852c2936 MD |
510 | /* |
511 | * Because ring_buffer_set_noref() is only called by a single thread | |
512 | * (the one which updated the cc_sb value), there are no concurrent | |
513 | * updates to take care of: other writers have not updated cc_sb, so | |
514 | * they cannot set the noref flag, and concurrent readers cannot modify | |
515 | * the pointer because the noref flag is not set yet. | |
516 | * The smp_wmb() in ring_buffer_commit() takes care of ordering writes | |
517 | * to the subbuffer before this set noref operation. | |
518 | * subbuffer_set_noref() uses a volatile store to deal with concurrent | |
519 | * readers of the noref flag. | |
520 | */ | |
15500a1b MD |
521 | chan = shmp(handle, bufb->chan); |
522 | if (!chan) | |
523 | return; | |
524 | CHAN_WARN_ON(chan, subbuffer_id_is_noref(config, wsb->id)); | |
852c2936 MD |
525 | /* |
526 | * Memory barrier that ensures counter stores are ordered before set | |
527 | * noref and offset. | |
528 | */ | |
14641deb | 529 | cmm_smp_mb(); |
15500a1b | 530 | subbuffer_id_set_noref_offset(config, &wsb->id, offset); |
852c2936 MD |
531 | } |
532 | ||
533 | /** | |
534 | * update_read_sb_index - Read-side subbuffer index update. | |
535 | */ | |
536 | static inline | |
4cfec15c MD |
537 | int update_read_sb_index(const struct lttng_ust_lib_ring_buffer_config *config, |
538 | struct lttng_ust_lib_ring_buffer_backend *bufb, | |
852c2936 MD |
539 | struct channel_backend *chanb, |
540 | unsigned long consumed_idx, | |
1d498196 | 541 | unsigned long consumed_count, |
38fae1d3 | 542 | struct lttng_ust_shm_handle *handle) |
852c2936 | 543 | { |
15500a1b | 544 | struct lttng_ust_lib_ring_buffer_backend_subbuffer *wsb; |
852c2936 MD |
545 | unsigned long old_id, new_id; |
546 | ||
15500a1b MD |
547 | wsb = shmp_index(handle, bufb->buf_wsb, consumed_idx); |
548 | if (caa_unlikely(!wsb)) | |
549 | return -EPERM; | |
550 | ||
852c2936 | 551 | if (config->mode == RING_BUFFER_OVERWRITE) { |
15500a1b MD |
552 | struct channel *chan; |
553 | ||
852c2936 MD |
554 | /* |
555 | * Exchange the target writer subbuffer with our own unused | |
14641deb | 556 | * subbuffer. No need to use CMM_ACCESS_ONCE() here to read the |
852c2936 MD |
557 | * old_wpage, because the value read will be confirmed by the |
558 | * following cmpxchg(). | |
559 | */ | |
15500a1b | 560 | old_id = wsb->id; |
b5a3dfa5 | 561 | if (caa_unlikely(!subbuffer_id_is_noref(config, old_id))) |
852c2936 MD |
562 | return -EAGAIN; |
563 | /* | |
564 | * Make sure the offset count we are expecting matches the one | |
565 | * indicated by the writer. | |
566 | */ | |
b5a3dfa5 | 567 | if (caa_unlikely(!subbuffer_id_compare_offset(config, old_id, |
852c2936 MD |
568 | consumed_count))) |
569 | return -EAGAIN; | |
15500a1b MD |
570 | chan = shmp(handle, bufb->chan); |
571 | if (caa_unlikely(!chan)) | |
572 | return -EPERM; | |
573 | CHAN_WARN_ON(chan, !subbuffer_id_is_noref(config, bufb->buf_rsb.id)); | |
852c2936 MD |
574 | subbuffer_id_set_noref_offset(config, &bufb->buf_rsb.id, |
575 | consumed_count); | |
15500a1b | 576 | new_id = uatomic_cmpxchg(&wsb->id, old_id, bufb->buf_rsb.id); |
b5a3dfa5 | 577 | if (caa_unlikely(old_id != new_id)) |
852c2936 MD |
578 | return -EAGAIN; |
579 | bufb->buf_rsb.id = new_id; | |
580 | } else { | |
581 | /* No page exchange, use the writer page directly */ | |
15500a1b | 582 | bufb->buf_rsb.id = wsb->id; |
852c2936 MD |
583 | } |
584 | return 0; | |
585 | } | |
586 | ||
0d4aa2df MD |
587 | #ifndef inline_memcpy |
588 | #define inline_memcpy(dest, src, n) memcpy(dest, src, n) | |
589 | #endif | |
590 | ||
51b8f2fa MD |
591 | static inline __attribute__((always_inline)) |
592 | void lttng_inline_memcpy(void *dest, const void *src, | |
593 | unsigned long len) | |
594 | { | |
595 | switch (len) { | |
596 | case 1: | |
597 | *(uint8_t *) dest = *(const uint8_t *) src; | |
598 | break; | |
599 | case 2: | |
600 | *(uint16_t *) dest = *(const uint16_t *) src; | |
601 | break; | |
602 | case 4: | |
603 | *(uint32_t *) dest = *(const uint32_t *) src; | |
604 | break; | |
605 | case 8: | |
606 | *(uint64_t *) dest = *(const uint64_t *) src; | |
607 | break; | |
608 | default: | |
609 | inline_memcpy(dest, src, len); | |
610 | } | |
611 | } | |
612 | ||
852c2936 MD |
613 | /* |
614 | * Use the architecture-specific memcpy implementation for constant-sized | |
615 | * inputs, but rely on an inline memcpy for length statically unknown. | |
616 | * The function call to memcpy is just way too expensive for a fast path. | |
617 | */ | |
618 | #define lib_ring_buffer_do_copy(config, dest, src, len) \ | |
619 | do { \ | |
620 | size_t __len = (len); \ | |
621 | if (__builtin_constant_p(len)) \ | |
622 | memcpy(dest, src, __len); \ | |
623 | else \ | |
51b8f2fa | 624 | lttng_inline_memcpy(dest, src, __len); \ |
852c2936 MD |
625 | } while (0) |
626 | ||
a44c74d9 MD |
627 | /* |
628 | * write len bytes to dest with c | |
629 | */ | |
630 | static inline | |
631 | void lib_ring_buffer_do_memset(char *dest, int c, unsigned long len) | |
632 | { | |
633 | unsigned long i; | |
634 | ||
635 | for (i = 0; i < len; i++) | |
636 | dest[i] = c; | |
637 | } | |
638 | ||
b728d87e MD |
639 | /* arch-agnostic implementation */ |
640 | ||
bfd26582 | 641 | static inline int lttng_ust_fls(unsigned int x) |
b728d87e MD |
642 | { |
643 | int r = 32; | |
644 | ||
645 | if (!x) | |
646 | return 0; | |
647 | if (!(x & 0xFFFF0000U)) { | |
648 | x <<= 16; | |
649 | r -= 16; | |
650 | } | |
651 | if (!(x & 0xFF000000U)) { | |
652 | x <<= 8; | |
653 | r -= 8; | |
654 | } | |
655 | if (!(x & 0xF0000000U)) { | |
656 | x <<= 4; | |
657 | r -= 4; | |
658 | } | |
659 | if (!(x & 0xC0000000U)) { | |
660 | x <<= 2; | |
661 | r -= 2; | |
662 | } | |
663 | if (!(x & 0x80000000U)) { | |
e2bd33a5 | 664 | /* No need to bit shift on last operation */ |
b728d87e MD |
665 | r -= 1; |
666 | } | |
667 | return r; | |
668 | } | |
669 | ||
670 | static inline int get_count_order(unsigned int count) | |
671 | { | |
672 | int order; | |
673 | ||
bfd26582 | 674 | order = lttng_ust_fls(count) - 1; |
b728d87e MD |
675 | if (count & (count - 1)) |
676 | order++; | |
677 | return order; | |
678 | } | |
679 | ||
e92f3e28 | 680 | #endif /* _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H */ |