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