1 #ifndef _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H
2 #define _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H
5 * libringbuffer/backend_internal.h
7 * Ring buffer backend (internal helpers).
9 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; only
14 * version 2.1 of the License.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <urcu/compiler.h>
29 #include <lttng/ringbuffer-config.h>
30 #include "backend_types.h"
31 #include "frontend_types.h"
34 /* Ring buffer backend API presented to the frontend */
36 /* Ring buffer and channel backend create/free */
38 int lib_ring_buffer_backend_create(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
39 struct channel_backend
*chan
, int cpu
,
40 struct lttng_ust_shm_handle
*handle
,
41 struct shm_object
*shmobj
);
42 void channel_backend_unregister_notifiers(struct channel_backend
*chanb
);
43 void lib_ring_buffer_backend_free(struct lttng_ust_lib_ring_buffer_backend
*bufb
);
44 int channel_backend_init(struct channel_backend
*chanb
,
46 const struct lttng_ust_lib_ring_buffer_config
*config
,
48 size_t num_subbuf
, struct lttng_ust_shm_handle
*handle
,
49 const int *stream_fds
);
50 void channel_backend_free(struct channel_backend
*chanb
,
51 struct lttng_ust_shm_handle
*handle
);
53 void lib_ring_buffer_backend_reset(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
54 struct lttng_ust_shm_handle
*handle
);
55 void channel_backend_reset(struct channel_backend
*chanb
);
57 int lib_ring_buffer_backend_init(void);
58 void lib_ring_buffer_backend_exit(void);
60 extern void _lib_ring_buffer_write(struct lttng_ust_lib_ring_buffer_backend
*bufb
,
61 size_t offset
, const void *src
, size_t len
,
65 * Subbuffer ID bits for overwrite mode. Need to fit within a single word to be
66 * exchanged atomically.
68 * Top half word, except lowest bit, belongs to "offset", which is used to keep
69 * to count the produced buffers. For overwrite mode, this provides the
70 * consumer with the capacity to read subbuffers in order, handling the
71 * situation where producers would write up to 2^15 buffers (or 2^31 for 64-bit
72 * systems) concurrently with a single execution of get_subbuf (between offset
73 * sampling and subbuffer ID exchange).
76 #define HALF_ULONG_BITS (CAA_BITS_PER_LONG >> 1)
78 #define SB_ID_OFFSET_SHIFT (HALF_ULONG_BITS + 1)
79 #define SB_ID_OFFSET_COUNT (1UL << SB_ID_OFFSET_SHIFT)
80 #define SB_ID_OFFSET_MASK (~(SB_ID_OFFSET_COUNT - 1))
82 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
84 #define SB_ID_NOREF_SHIFT (SB_ID_OFFSET_SHIFT - 1)
85 #define SB_ID_NOREF_COUNT (1UL << SB_ID_NOREF_SHIFT)
86 #define SB_ID_NOREF_MASK SB_ID_NOREF_COUNT
88 * In overwrite mode: lowest half of word is used for index.
89 * Limit of 2^16 subbuffers per buffer on 32-bit, 2^32 on 64-bit.
90 * In producer-consumer mode: whole word used for index.
92 #define SB_ID_INDEX_SHIFT 0
93 #define SB_ID_INDEX_COUNT (1UL << SB_ID_INDEX_SHIFT)
94 #define SB_ID_INDEX_MASK (SB_ID_NOREF_COUNT - 1)
97 * Construct the subbuffer id from offset, index and noref. Use only the index
98 * for producer-consumer mode (offset and noref are only used in overwrite
102 unsigned long subbuffer_id(const struct lttng_ust_lib_ring_buffer_config
*config
,
103 unsigned long offset
, unsigned long noref
,
106 if (config
->mode
== RING_BUFFER_OVERWRITE
)
107 return (offset
<< SB_ID_OFFSET_SHIFT
)
108 | (noref
<< SB_ID_NOREF_SHIFT
)
115 * Compare offset with the offset contained within id. Return 1 if the offset
116 * bits are identical, else 0.
119 int subbuffer_id_compare_offset(const struct lttng_ust_lib_ring_buffer_config
*config
,
120 unsigned long id
, unsigned long offset
)
122 return (id
& SB_ID_OFFSET_MASK
) == (offset
<< SB_ID_OFFSET_SHIFT
);
126 unsigned long subbuffer_id_get_index(const struct lttng_ust_lib_ring_buffer_config
*config
,
129 if (config
->mode
== RING_BUFFER_OVERWRITE
)
130 return id
& SB_ID_INDEX_MASK
;
136 unsigned long subbuffer_id_is_noref(const struct lttng_ust_lib_ring_buffer_config
*config
,
139 if (config
->mode
== RING_BUFFER_OVERWRITE
)
140 return !!(id
& SB_ID_NOREF_MASK
);
146 * Only used by reader on subbuffer ID it has exclusive access to. No volatile
150 void subbuffer_id_set_noref(const struct lttng_ust_lib_ring_buffer_config
*config
,
153 if (config
->mode
== RING_BUFFER_OVERWRITE
)
154 *id
|= SB_ID_NOREF_MASK
;
158 void subbuffer_id_set_noref_offset(const struct lttng_ust_lib_ring_buffer_config
*config
,
159 unsigned long *id
, unsigned long offset
)
163 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
165 tmp
&= ~SB_ID_OFFSET_MASK
;
166 tmp
|= offset
<< SB_ID_OFFSET_SHIFT
;
167 tmp
|= SB_ID_NOREF_MASK
;
168 /* Volatile store, read concurrently by readers. */
169 CMM_ACCESS_ONCE(*id
) = tmp
;
173 /* No volatile access, since already used locally */
175 void subbuffer_id_clear_noref(const struct lttng_ust_lib_ring_buffer_config
*config
,
178 if (config
->mode
== RING_BUFFER_OVERWRITE
)
179 *id
&= ~SB_ID_NOREF_MASK
;
183 * For overwrite mode, cap the number of subbuffers per buffer to:
184 * 2^16 on 32-bit architectures
185 * 2^32 on 64-bit architectures
186 * This is required to fit in the index part of the ID. Return 0 on success,
190 int subbuffer_id_check_index(const struct lttng_ust_lib_ring_buffer_config
*config
,
191 unsigned long num_subbuf
)
193 if (config
->mode
== RING_BUFFER_OVERWRITE
)
194 return (num_subbuf
> (1UL << HALF_ULONG_BITS
)) ? -EPERM
: 0;
200 void subbuffer_count_record(const struct lttng_ust_lib_ring_buffer_config
*config
,
201 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
202 unsigned long idx
, struct lttng_ust_shm_handle
*handle
)
204 unsigned long sb_bindex
;
206 sb_bindex
= subbuffer_id_get_index(config
, shmp_index(handle
, bufb
->buf_wsb
, idx
)->id
);
207 v_inc(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, sb_bindex
)->shmp
)->records_commit
);
211 * Reader has exclusive subbuffer access for record consumption. No need to
212 * perform the decrement atomically.
215 void subbuffer_consume_record(const struct lttng_ust_lib_ring_buffer_config
*config
,
216 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
217 struct lttng_ust_shm_handle
*handle
)
219 unsigned long sb_bindex
;
221 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
222 CHAN_WARN_ON(shmp(handle
, bufb
->chan
),
223 !v_read(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, sb_bindex
)->shmp
)->records_unread
));
224 /* Non-atomic decrement protected by exclusive subbuffer access */
225 _v_dec(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, sb_bindex
)->shmp
)->records_unread
);
226 v_inc(config
, &bufb
->records_read
);
230 unsigned long subbuffer_get_records_count(
231 const struct lttng_ust_lib_ring_buffer_config
*config
,
232 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
234 struct lttng_ust_shm_handle
*handle
)
236 unsigned long sb_bindex
;
238 sb_bindex
= subbuffer_id_get_index(config
, shmp_index(handle
, bufb
->buf_wsb
, idx
)->id
);
239 return v_read(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, sb_bindex
)->shmp
)->records_commit
);
243 * Must be executed at subbuffer delivery when the writer has _exclusive_
244 * subbuffer access. See lib_ring_buffer_check_deliver() for details.
245 * lib_ring_buffer_get_records_count() must be called to get the records
246 * count before this function, because it resets the records_commit
250 unsigned long subbuffer_count_records_overrun(
251 const struct lttng_ust_lib_ring_buffer_config
*config
,
252 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
254 struct lttng_ust_shm_handle
*handle
)
256 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*pages
;
257 unsigned long overruns
, sb_bindex
;
259 sb_bindex
= subbuffer_id_get_index(config
, shmp_index(handle
, bufb
->buf_wsb
, idx
)->id
);
260 pages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
261 overruns
= v_read(config
, &shmp(handle
, pages
->shmp
)->records_unread
);
262 v_set(config
, &shmp(handle
, pages
->shmp
)->records_unread
,
263 v_read(config
, &shmp(handle
, pages
->shmp
)->records_commit
));
264 v_set(config
, &shmp(handle
, pages
->shmp
)->records_commit
, 0);
270 void subbuffer_set_data_size(const struct lttng_ust_lib_ring_buffer_config
*config
,
271 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
273 unsigned long data_size
,
274 struct lttng_ust_shm_handle
*handle
)
276 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*pages
;
277 unsigned long sb_bindex
;
279 sb_bindex
= subbuffer_id_get_index(config
, shmp_index(handle
, bufb
->buf_wsb
, idx
)->id
);
280 pages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
281 shmp(handle
, pages
->shmp
)->data_size
= data_size
;
285 unsigned long subbuffer_get_read_data_size(
286 const struct lttng_ust_lib_ring_buffer_config
*config
,
287 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
288 struct lttng_ust_shm_handle
*handle
)
290 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*pages
;
291 unsigned long sb_bindex
;
293 sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
294 pages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
295 return shmp(handle
, pages
->shmp
)->data_size
;
299 unsigned long subbuffer_get_data_size(
300 const struct lttng_ust_lib_ring_buffer_config
*config
,
301 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
303 struct lttng_ust_shm_handle
*handle
)
305 struct lttng_ust_lib_ring_buffer_backend_pages_shmp
*pages
;
306 unsigned long sb_bindex
;
308 sb_bindex
= subbuffer_id_get_index(config
, shmp_index(handle
, bufb
->buf_wsb
, idx
)->id
);
309 pages
= shmp_index(handle
, bufb
->array
, sb_bindex
);
310 return shmp(handle
, pages
->shmp
)->data_size
;
314 * lib_ring_buffer_clear_noref - Clear the noref subbuffer flag, called by
318 void lib_ring_buffer_clear_noref(const struct lttng_ust_lib_ring_buffer_config
*config
,
319 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
321 struct lttng_ust_shm_handle
*handle
)
323 unsigned long id
, new_id
;
325 if (config
->mode
!= RING_BUFFER_OVERWRITE
)
329 * Performing a volatile access to read the sb_pages, because we want to
330 * read a coherent version of the pointer and the associated noref flag.
332 id
= CMM_ACCESS_ONCE(shmp_index(handle
, bufb
->buf_wsb
, idx
)->id
);
334 /* This check is called on the fast path for each record. */
335 if (caa_likely(!subbuffer_id_is_noref(config
, id
))) {
337 * Store after load dependency ordering the writes to
338 * the subbuffer after load and test of the noref flag
339 * matches the memory barrier implied by the cmpxchg()
340 * in update_read_sb_index().
342 return; /* Already writing to this buffer */
345 subbuffer_id_clear_noref(config
, &new_id
);
346 new_id
= uatomic_cmpxchg(&shmp_index(handle
, bufb
->buf_wsb
, idx
)->id
, id
, new_id
);
347 if (caa_likely(new_id
== id
))
354 * lib_ring_buffer_set_noref_offset - Set the noref subbuffer flag and offset,
358 void lib_ring_buffer_set_noref_offset(const struct lttng_ust_lib_ring_buffer_config
*config
,
359 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
360 unsigned long idx
, unsigned long offset
,
361 struct lttng_ust_shm_handle
*handle
)
363 if (config
->mode
!= RING_BUFFER_OVERWRITE
)
367 * Because ring_buffer_set_noref() is only called by a single thread
368 * (the one which updated the cc_sb value), there are no concurrent
369 * updates to take care of: other writers have not updated cc_sb, so
370 * they cannot set the noref flag, and concurrent readers cannot modify
371 * the pointer because the noref flag is not set yet.
372 * The smp_wmb() in ring_buffer_commit() takes care of ordering writes
373 * to the subbuffer before this set noref operation.
374 * subbuffer_set_noref() uses a volatile store to deal with concurrent
375 * readers of the noref flag.
377 CHAN_WARN_ON(shmp(handle
, bufb
->chan
),
378 subbuffer_id_is_noref(config
, shmp_index(handle
, bufb
->buf_wsb
, idx
)->id
));
380 * Memory barrier that ensures counter stores are ordered before set
384 subbuffer_id_set_noref_offset(config
, &shmp_index(handle
, bufb
->buf_wsb
, idx
)->id
, offset
);
388 * update_read_sb_index - Read-side subbuffer index update.
391 int update_read_sb_index(const struct lttng_ust_lib_ring_buffer_config
*config
,
392 struct lttng_ust_lib_ring_buffer_backend
*bufb
,
393 struct channel_backend
*chanb
,
394 unsigned long consumed_idx
,
395 unsigned long consumed_count
,
396 struct lttng_ust_shm_handle
*handle
)
398 unsigned long old_id
, new_id
;
400 if (config
->mode
== RING_BUFFER_OVERWRITE
) {
402 * Exchange the target writer subbuffer with our own unused
403 * subbuffer. No need to use CMM_ACCESS_ONCE() here to read the
404 * old_wpage, because the value read will be confirmed by the
405 * following cmpxchg().
407 old_id
= shmp_index(handle
, bufb
->buf_wsb
, consumed_idx
)->id
;
408 if (caa_unlikely(!subbuffer_id_is_noref(config
, old_id
)))
411 * Make sure the offset count we are expecting matches the one
412 * indicated by the writer.
414 if (caa_unlikely(!subbuffer_id_compare_offset(config
, old_id
,
417 CHAN_WARN_ON(shmp(handle
, bufb
->chan
),
418 !subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
419 subbuffer_id_set_noref_offset(config
, &bufb
->buf_rsb
.id
,
421 new_id
= uatomic_cmpxchg(&shmp_index(handle
, bufb
->buf_wsb
, consumed_idx
)->id
, old_id
,
423 if (caa_unlikely(old_id
!= new_id
))
425 bufb
->buf_rsb
.id
= new_id
;
427 /* No page exchange, use the writer page directly */
428 bufb
->buf_rsb
.id
= shmp_index(handle
, bufb
->buf_wsb
, consumed_idx
)->id
;
433 #ifndef inline_memcpy
434 #define inline_memcpy(dest, src, n) memcpy(dest, src, n)
438 * Use the architecture-specific memcpy implementation for constant-sized
439 * inputs, but rely on an inline memcpy for length statically unknown.
440 * The function call to memcpy is just way too expensive for a fast path.
442 #define lib_ring_buffer_do_copy(config, dest, src, len) \
444 size_t __len = (len); \
445 if (__builtin_constant_p(len)) \
446 memcpy(dest, src, __len); \
448 inline_memcpy(dest, src, __len); \
452 * write len bytes to dest with c
455 void lib_ring_buffer_do_memset(char *dest
, int c
, unsigned long len
)
459 for (i
= 0; i
< len
; i
++)
463 /* arch-agnostic implementation */
465 static inline int lttng_ust_fls(unsigned int x
)
471 if (!(x
& 0xFFFF0000U
)) {
475 if (!(x
& 0xFF000000U
)) {
479 if (!(x
& 0xF0000000U
)) {
483 if (!(x
& 0xC0000000U
)) {
487 if (!(x
& 0x80000000U
)) {
488 /* No need to bit shift on last operation */
494 static inline int get_count_order(unsigned int count
)
498 order
= lttng_ust_fls(count
) - 1;
499 if (count
& (count
- 1))
504 #endif /* _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H */