Cleanup: fix comment
[lttng-ust.git] / libringbuffer / backend_internal.h
CommitLineData
e92f3e28
MD
1#ifndef _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H
2#define _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H
852c2936
MD
3
4/*
e92f3e28 5 * libringbuffer/backend_internal.h
852c2936
MD
6 *
7 * Ring buffer backend (internal helpers).
8 *
e92f3e28
MD
9 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
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.
15 *
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.
20 *
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
852c2936
MD
24 */
25
14641deb
MD
26#include <unistd.h>
27#include <urcu/compiler.h>
28
4318ae1b 29#include <lttng/ringbuffer-config.h>
4931a13e
MD
30#include "backend_types.h"
31#include "frontend_types.h"
a6352fd4 32#include "shm.h"
852c2936
MD
33
34/* Ring buffer backend API presented to the frontend */
35
36/* Ring buffer and channel backend create/free */
37
4cfec15c 38int lib_ring_buffer_backend_create(struct lttng_ust_lib_ring_buffer_backend *bufb,
a6352fd4 39 struct channel_backend *chan, int cpu,
38fae1d3 40 struct lttng_ust_shm_handle *handle,
1d498196 41 struct shm_object *shmobj);
852c2936 42void channel_backend_unregister_notifiers(struct channel_backend *chanb);
4cfec15c 43void lib_ring_buffer_backend_free(struct lttng_ust_lib_ring_buffer_backend *bufb);
852c2936
MD
44int channel_backend_init(struct channel_backend *chanb,
45 const char *name,
4cfec15c 46 const struct lttng_ust_lib_ring_buffer_config *config,
a3f61e7f 47 size_t subbuf_size,
38fae1d3 48 size_t num_subbuf, struct lttng_ust_shm_handle *handle);
1d498196 49void channel_backend_free(struct channel_backend *chanb,
38fae1d3 50 struct lttng_ust_shm_handle *handle);
852c2936 51
4cfec15c 52void lib_ring_buffer_backend_reset(struct lttng_ust_lib_ring_buffer_backend *bufb,
38fae1d3 53 struct lttng_ust_shm_handle *handle);
852c2936
MD
54void channel_backend_reset(struct channel_backend *chanb);
55
56int lib_ring_buffer_backend_init(void);
57void lib_ring_buffer_backend_exit(void);
58
4cfec15c 59extern 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 */
100static inline
4cfec15c 101unsigned 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 */
117static inline
4cfec15c 118int 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
124static inline
4cfec15c 125unsigned 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
134static inline
4cfec15c 135unsigned 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 */
148static inline
4cfec15c 149void 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
156static inline
4cfec15c 157void 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 */
173static inline
4cfec15c 174void 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 */
188static inline
4cfec15c 189int 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
198static inline
4cfec15c
MD
199void subbuffer_count_record(const struct lttng_ust_lib_ring_buffer_config *config,
200 struct lttng_ust_lib_ring_buffer_backend *bufb,
38fae1d3 201 unsigned long idx, struct lttng_ust_shm_handle *handle)
852c2936
MD
202{
203 unsigned long sb_bindex;
204
4746ae29
MD
205 sb_bindex = subbuffer_id_get_index(config, shmp_index(handle, bufb->buf_wsb, idx)->id);
206 v_inc(config, &shmp(handle, shmp_index(handle, bufb->array, sb_bindex)->shmp)->records_commit);
852c2936
MD
207}
208
209/*
210 * Reader has exclusive subbuffer access for record consumption. No need to
211 * perform the decrement atomically.
212 */
213static inline
4cfec15c
MD
214void subbuffer_consume_record(const struct lttng_ust_lib_ring_buffer_config *config,
215 struct lttng_ust_lib_ring_buffer_backend *bufb,
38fae1d3 216 struct lttng_ust_shm_handle *handle)
852c2936
MD
217{
218 unsigned long sb_bindex;
219
220 sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
1d498196 221 CHAN_WARN_ON(shmp(handle, bufb->chan),
4746ae29 222 !v_read(config, &shmp(handle, shmp_index(handle, bufb->array, sb_bindex)->shmp)->records_unread));
852c2936 223 /* Non-atomic decrement protected by exclusive subbuffer access */
4746ae29 224 _v_dec(config, &shmp(handle, shmp_index(handle, bufb->array, sb_bindex)->shmp)->records_unread);
852c2936
MD
225 v_inc(config, &bufb->records_read);
226}
227
228static inline
229unsigned long subbuffer_get_records_count(
4cfec15c
MD
230 const struct lttng_ust_lib_ring_buffer_config *config,
231 struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 232 unsigned long idx,
38fae1d3 233 struct lttng_ust_shm_handle *handle)
852c2936
MD
234{
235 unsigned long sb_bindex;
236
4746ae29
MD
237 sb_bindex = subbuffer_id_get_index(config, shmp_index(handle, bufb->buf_wsb, idx)->id);
238 return v_read(config, &shmp(handle, shmp_index(handle, bufb->array, sb_bindex)->shmp)->records_commit);
852c2936
MD
239}
240
241/*
242 * Must be executed at subbuffer delivery when the writer has _exclusive_
3d1aec25
MD
243 * subbuffer access. See lib_ring_buffer_check_deliver() for details.
244 * lib_ring_buffer_get_records_count() must be called to get the records
245 * count before this function, because it resets the records_commit
246 * count.
852c2936
MD
247 */
248static inline
249unsigned long subbuffer_count_records_overrun(
4cfec15c
MD
250 const struct lttng_ust_lib_ring_buffer_config *config,
251 struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 252 unsigned long idx,
38fae1d3 253 struct lttng_ust_shm_handle *handle)
852c2936 254{
4cfec15c 255 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages;
852c2936
MD
256 unsigned long overruns, sb_bindex;
257
4746ae29
MD
258 sb_bindex = subbuffer_id_get_index(config, shmp_index(handle, bufb->buf_wsb, idx)->id);
259 pages = shmp_index(handle, bufb->array, sb_bindex);
1d498196
MD
260 overruns = v_read(config, &shmp(handle, pages->shmp)->records_unread);
261 v_set(config, &shmp(handle, pages->shmp)->records_unread,
262 v_read(config, &shmp(handle, pages->shmp)->records_commit));
263 v_set(config, &shmp(handle, pages->shmp)->records_commit, 0);
852c2936
MD
264
265 return overruns;
266}
267
268static inline
4cfec15c
MD
269void subbuffer_set_data_size(const struct lttng_ust_lib_ring_buffer_config *config,
270 struct lttng_ust_lib_ring_buffer_backend *bufb,
852c2936 271 unsigned long idx,
1d498196 272 unsigned long data_size,
38fae1d3 273 struct lttng_ust_shm_handle *handle)
852c2936 274{
4cfec15c 275 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages;
852c2936
MD
276 unsigned long sb_bindex;
277
4746ae29
MD
278 sb_bindex = subbuffer_id_get_index(config, shmp_index(handle, bufb->buf_wsb, idx)->id);
279 pages = shmp_index(handle, bufb->array, sb_bindex);
1d498196 280 shmp(handle, pages->shmp)->data_size = data_size;
852c2936
MD
281}
282
283static inline
284unsigned long subbuffer_get_read_data_size(
4cfec15c
MD
285 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 288{
4cfec15c 289 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages;
852c2936
MD
290 unsigned long sb_bindex;
291
292 sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
4746ae29 293 pages = shmp_index(handle, bufb->array, sb_bindex);
1d498196 294 return shmp(handle, pages->shmp)->data_size;
852c2936
MD
295}
296
297static inline
298unsigned long subbuffer_get_data_size(
4cfec15c
MD
299 const struct lttng_ust_lib_ring_buffer_config *config,
300 struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 301 unsigned long idx,
38fae1d3 302 struct lttng_ust_shm_handle *handle)
852c2936 303{
4cfec15c 304 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages;
852c2936
MD
305 unsigned long sb_bindex;
306
4746ae29
MD
307 sb_bindex = subbuffer_id_get_index(config, shmp_index(handle, bufb->buf_wsb, idx)->id);
308 pages = shmp_index(handle, bufb->array, sb_bindex);
1d498196 309 return shmp(handle, pages->shmp)->data_size;
852c2936
MD
310}
311
312/**
313 * lib_ring_buffer_clear_noref - Clear the noref subbuffer flag, called by
314 * writer.
315 */
316static inline
4cfec15c
MD
317void lib_ring_buffer_clear_noref(const struct lttng_ust_lib_ring_buffer_config *config,
318 struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 319 unsigned long idx,
38fae1d3 320 struct lttng_ust_shm_handle *handle)
852c2936
MD
321{
322 unsigned long id, new_id;
323
324 if (config->mode != RING_BUFFER_OVERWRITE)
325 return;
326
327 /*
328 * Performing a volatile access to read the sb_pages, because we want to
329 * read a coherent version of the pointer and the associated noref flag.
330 */
4746ae29 331 id = CMM_ACCESS_ONCE(shmp_index(handle, bufb->buf_wsb, idx)->id);
852c2936
MD
332 for (;;) {
333 /* This check is called on the fast path for each record. */
b5a3dfa5 334 if (caa_likely(!subbuffer_id_is_noref(config, id))) {
852c2936
MD
335 /*
336 * Store after load dependency ordering the writes to
337 * the subbuffer after load and test of the noref flag
338 * matches the memory barrier implied by the cmpxchg()
339 * in update_read_sb_index().
340 */
341 return; /* Already writing to this buffer */
342 }
343 new_id = id;
344 subbuffer_id_clear_noref(config, &new_id);
4746ae29 345 new_id = uatomic_cmpxchg(&shmp_index(handle, bufb->buf_wsb, idx)->id, id, new_id);
b5a3dfa5 346 if (caa_likely(new_id == id))
852c2936
MD
347 break;
348 id = new_id;
349 }
350}
351
352/**
353 * lib_ring_buffer_set_noref_offset - Set the noref subbuffer flag and offset,
354 * called by writer.
355 */
356static inline
4cfec15c
MD
357void lib_ring_buffer_set_noref_offset(const struct lttng_ust_lib_ring_buffer_config *config,
358 struct lttng_ust_lib_ring_buffer_backend *bufb,
1d498196 359 unsigned long idx, unsigned long offset,
38fae1d3 360 struct lttng_ust_shm_handle *handle)
852c2936
MD
361{
362 if (config->mode != RING_BUFFER_OVERWRITE)
363 return;
364
365 /*
366 * Because ring_buffer_set_noref() is only called by a single thread
367 * (the one which updated the cc_sb value), there are no concurrent
368 * updates to take care of: other writers have not updated cc_sb, so
369 * they cannot set the noref flag, and concurrent readers cannot modify
370 * the pointer because the noref flag is not set yet.
371 * The smp_wmb() in ring_buffer_commit() takes care of ordering writes
372 * to the subbuffer before this set noref operation.
373 * subbuffer_set_noref() uses a volatile store to deal with concurrent
374 * readers of the noref flag.
375 */
1d498196 376 CHAN_WARN_ON(shmp(handle, bufb->chan),
4746ae29 377 subbuffer_id_is_noref(config, shmp_index(handle, bufb->buf_wsb, idx)->id));
852c2936
MD
378 /*
379 * Memory barrier that ensures counter stores are ordered before set
380 * noref and offset.
381 */
14641deb 382 cmm_smp_mb();
4746ae29 383 subbuffer_id_set_noref_offset(config, &shmp_index(handle, bufb->buf_wsb, idx)->id, offset);
852c2936
MD
384}
385
386/**
387 * update_read_sb_index - Read-side subbuffer index update.
388 */
389static inline
4cfec15c
MD
390int update_read_sb_index(const struct lttng_ust_lib_ring_buffer_config *config,
391 struct lttng_ust_lib_ring_buffer_backend *bufb,
852c2936
MD
392 struct channel_backend *chanb,
393 unsigned long consumed_idx,
1d498196 394 unsigned long consumed_count,
38fae1d3 395 struct lttng_ust_shm_handle *handle)
852c2936
MD
396{
397 unsigned long old_id, new_id;
398
399 if (config->mode == RING_BUFFER_OVERWRITE) {
400 /*
401 * Exchange the target writer subbuffer with our own unused
14641deb 402 * subbuffer. No need to use CMM_ACCESS_ONCE() here to read the
852c2936
MD
403 * old_wpage, because the value read will be confirmed by the
404 * following cmpxchg().
405 */
4746ae29 406 old_id = shmp_index(handle, bufb->buf_wsb, consumed_idx)->id;
b5a3dfa5 407 if (caa_unlikely(!subbuffer_id_is_noref(config, old_id)))
852c2936
MD
408 return -EAGAIN;
409 /*
410 * Make sure the offset count we are expecting matches the one
411 * indicated by the writer.
412 */
b5a3dfa5 413 if (caa_unlikely(!subbuffer_id_compare_offset(config, old_id,
852c2936
MD
414 consumed_count)))
415 return -EAGAIN;
1d498196 416 CHAN_WARN_ON(shmp(handle, bufb->chan),
852c2936
MD
417 !subbuffer_id_is_noref(config, bufb->buf_rsb.id));
418 subbuffer_id_set_noref_offset(config, &bufb->buf_rsb.id,
419 consumed_count);
4746ae29 420 new_id = uatomic_cmpxchg(&shmp_index(handle, bufb->buf_wsb, consumed_idx)->id, old_id,
852c2936 421 bufb->buf_rsb.id);
b5a3dfa5 422 if (caa_unlikely(old_id != new_id))
852c2936
MD
423 return -EAGAIN;
424 bufb->buf_rsb.id = new_id;
425 } else {
426 /* No page exchange, use the writer page directly */
4746ae29 427 bufb->buf_rsb.id = shmp_index(handle, bufb->buf_wsb, consumed_idx)->id;
852c2936
MD
428 }
429 return 0;
430}
431
0d4aa2df
MD
432#ifndef inline_memcpy
433#define inline_memcpy(dest, src, n) memcpy(dest, src, n)
434#endif
435
852c2936
MD
436/*
437 * Use the architecture-specific memcpy implementation for constant-sized
438 * inputs, but rely on an inline memcpy for length statically unknown.
439 * The function call to memcpy is just way too expensive for a fast path.
440 */
441#define lib_ring_buffer_do_copy(config, dest, src, len) \
442do { \
443 size_t __len = (len); \
444 if (__builtin_constant_p(len)) \
445 memcpy(dest, src, __len); \
446 else \
447 inline_memcpy(dest, src, __len); \
448} while (0)
449
b728d87e
MD
450/* arch-agnostic implementation */
451
bfd26582 452static inline int lttng_ust_fls(unsigned int x)
b728d87e
MD
453{
454 int r = 32;
455
456 if (!x)
457 return 0;
458 if (!(x & 0xFFFF0000U)) {
459 x <<= 16;
460 r -= 16;
461 }
462 if (!(x & 0xFF000000U)) {
463 x <<= 8;
464 r -= 8;
465 }
466 if (!(x & 0xF0000000U)) {
467 x <<= 4;
468 r -= 4;
469 }
470 if (!(x & 0xC0000000U)) {
471 x <<= 2;
472 r -= 2;
473 }
474 if (!(x & 0x80000000U)) {
475 x <<= 1;
476 r -= 1;
477 }
478 return r;
479}
480
481static inline int get_count_order(unsigned int count)
482{
483 int order;
484
bfd26582 485 order = lttng_ust_fls(count) - 1;
b728d87e
MD
486 if (count & (count - 1))
487 order++;
488 return order;
489}
490
e92f3e28 491#endif /* _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H */
This page took 0.047634 seconds and 4 git commands to generate.