Performance: disable event counting by default
[lttng-ust.git] / libringbuffer / backend_internal.h
1 #ifndef _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H
2 #define _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H
3
4 /*
5 * libringbuffer/backend_internal.h
6 *
7 * Ring buffer backend (internal helpers).
8 *
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
24 */
25
26 #include <unistd.h>
27 #include <urcu/compiler.h>
28
29 #include <lttng/ringbuffer-config.h>
30 #include "backend_types.h"
31 #include "frontend_types.h"
32 #include "shm.h"
33
34 /* Ring buffer backend API presented to the frontend */
35
36 /* Ring buffer and channel backend create/free */
37
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,
45 const char *name,
46 const struct lttng_ust_lib_ring_buffer_config *config,
47 size_t subbuf_size,
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);
52
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);
56
57 int lib_ring_buffer_backend_init(void);
58 void lib_ring_buffer_backend_exit(void);
59
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,
62 ssize_t pagecpy);
63
64 /*
65 * Subbuffer ID bits for overwrite mode. Need to fit within a single word to be
66 * exchanged atomically.
67 *
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).
74 */
75
76 #define HALF_ULONG_BITS (CAA_BITS_PER_LONG >> 1)
77
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))
81 /*
82 * Lowest bit of top word half belongs to noref. Used only for overwrite mode.
83 */
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
87 /*
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.
91 */
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)
95
96 /*
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
99 * mode).
100 */
101 static inline
102 unsigned long subbuffer_id(const struct lttng_ust_lib_ring_buffer_config *config,
103 unsigned long offset, unsigned long noref,
104 unsigned long index)
105 {
106 if (config->mode == RING_BUFFER_OVERWRITE)
107 return (offset << SB_ID_OFFSET_SHIFT)
108 | (noref << SB_ID_NOREF_SHIFT)
109 | index;
110 else
111 return index;
112 }
113
114 /*
115 * Compare offset with the offset contained within id. Return 1 if the offset
116 * bits are identical, else 0.
117 */
118 static inline
119 int subbuffer_id_compare_offset(const struct lttng_ust_lib_ring_buffer_config *config,
120 unsigned long id, unsigned long offset)
121 {
122 return (id & SB_ID_OFFSET_MASK) == (offset << SB_ID_OFFSET_SHIFT);
123 }
124
125 static inline
126 unsigned long subbuffer_id_get_index(const struct lttng_ust_lib_ring_buffer_config *config,
127 unsigned long id)
128 {
129 if (config->mode == RING_BUFFER_OVERWRITE)
130 return id & SB_ID_INDEX_MASK;
131 else
132 return id;
133 }
134
135 static inline
136 unsigned long subbuffer_id_is_noref(const struct lttng_ust_lib_ring_buffer_config *config,
137 unsigned long id)
138 {
139 if (config->mode == RING_BUFFER_OVERWRITE)
140 return !!(id & SB_ID_NOREF_MASK);
141 else
142 return 1;
143 }
144
145 /*
146 * Only used by reader on subbuffer ID it has exclusive access to. No volatile
147 * needed.
148 */
149 static inline
150 void subbuffer_id_set_noref(const struct lttng_ust_lib_ring_buffer_config *config,
151 unsigned long *id)
152 {
153 if (config->mode == RING_BUFFER_OVERWRITE)
154 *id |= SB_ID_NOREF_MASK;
155 }
156
157 static inline
158 void subbuffer_id_set_noref_offset(const struct lttng_ust_lib_ring_buffer_config *config,
159 unsigned long *id, unsigned long offset)
160 {
161 unsigned long tmp;
162
163 if (config->mode == RING_BUFFER_OVERWRITE) {
164 tmp = *id;
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;
170 }
171 }
172
173 /* No volatile access, since already used locally */
174 static inline
175 void subbuffer_id_clear_noref(const struct lttng_ust_lib_ring_buffer_config *config,
176 unsigned long *id)
177 {
178 if (config->mode == RING_BUFFER_OVERWRITE)
179 *id &= ~SB_ID_NOREF_MASK;
180 }
181
182 /*
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,
187 * -EPERM on failure.
188 */
189 static inline
190 int subbuffer_id_check_index(const struct lttng_ust_lib_ring_buffer_config *config,
191 unsigned long num_subbuf)
192 {
193 if (config->mode == RING_BUFFER_OVERWRITE)
194 return (num_subbuf > (1UL << HALF_ULONG_BITS)) ? -EPERM : 0;
195 else
196 return 0;
197 }
198
199 /*
200 * The ring buffer can count events recorded and overwritten per buffer,
201 * but it is disabled by default due to its performance overhead.
202 */
203 #ifdef LTTNG_RING_BUFFER_COUNT_EVENTS
204 static inline
205 void subbuffer_count_record(const struct lttng_ust_lib_ring_buffer_config *config,
206 struct lttng_ust_lib_ring_buffer_backend *bufb,
207 unsigned long idx, struct lttng_ust_shm_handle *handle)
208 {
209 unsigned long sb_bindex;
210
211 sb_bindex = subbuffer_id_get_index(config, shmp_index(handle, bufb->buf_wsb, idx)->id);
212 v_inc(config, &shmp(handle, shmp_index(handle, bufb->array, sb_bindex)->shmp)->records_commit);
213 }
214 #else /* LTTNG_RING_BUFFER_COUNT_EVENTS */
215 static inline
216 void subbuffer_count_record(const struct lttng_ust_lib_ring_buffer_config *config,
217 struct lttng_ust_lib_ring_buffer_backend *bufb,
218 unsigned long idx, struct lttng_ust_shm_handle *handle)
219 {
220 }
221 #endif /* #else LTTNG_RING_BUFFER_COUNT_EVENTS */
222
223 /*
224 * Reader has exclusive subbuffer access for record consumption. No need to
225 * perform the decrement atomically.
226 */
227 static inline
228 void subbuffer_consume_record(const struct lttng_ust_lib_ring_buffer_config *config,
229 struct lttng_ust_lib_ring_buffer_backend *bufb,
230 struct lttng_ust_shm_handle *handle)
231 {
232 unsigned long sb_bindex;
233
234 sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
235 CHAN_WARN_ON(shmp(handle, bufb->chan),
236 !v_read(config, &shmp(handle, shmp_index(handle, bufb->array, sb_bindex)->shmp)->records_unread));
237 /* Non-atomic decrement protected by exclusive subbuffer access */
238 _v_dec(config, &shmp(handle, shmp_index(handle, bufb->array, sb_bindex)->shmp)->records_unread);
239 v_inc(config, &bufb->records_read);
240 }
241
242 static inline
243 unsigned long subbuffer_get_records_count(
244 const struct lttng_ust_lib_ring_buffer_config *config,
245 struct lttng_ust_lib_ring_buffer_backend *bufb,
246 unsigned long idx,
247 struct lttng_ust_shm_handle *handle)
248 {
249 unsigned long sb_bindex;
250
251 sb_bindex = subbuffer_id_get_index(config, shmp_index(handle, bufb->buf_wsb, idx)->id);
252 return v_read(config, &shmp(handle, shmp_index(handle, bufb->array, sb_bindex)->shmp)->records_commit);
253 }
254
255 /*
256 * Must be executed at subbuffer delivery when the writer has _exclusive_
257 * subbuffer access. See lib_ring_buffer_check_deliver() for details.
258 * lib_ring_buffer_get_records_count() must be called to get the records
259 * count before this function, because it resets the records_commit
260 * count.
261 */
262 static inline
263 unsigned long subbuffer_count_records_overrun(
264 const struct lttng_ust_lib_ring_buffer_config *config,
265 struct lttng_ust_lib_ring_buffer_backend *bufb,
266 unsigned long idx,
267 struct lttng_ust_shm_handle *handle)
268 {
269 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages;
270 unsigned long overruns, sb_bindex;
271
272 sb_bindex = subbuffer_id_get_index(config, shmp_index(handle, bufb->buf_wsb, idx)->id);
273 pages = shmp_index(handle, bufb->array, sb_bindex);
274 overruns = v_read(config, &shmp(handle, pages->shmp)->records_unread);
275 v_set(config, &shmp(handle, pages->shmp)->records_unread,
276 v_read(config, &shmp(handle, pages->shmp)->records_commit));
277 v_set(config, &shmp(handle, pages->shmp)->records_commit, 0);
278
279 return overruns;
280 }
281
282 static inline
283 void subbuffer_set_data_size(const struct lttng_ust_lib_ring_buffer_config *config,
284 struct lttng_ust_lib_ring_buffer_backend *bufb,
285 unsigned long idx,
286 unsigned long data_size,
287 struct lttng_ust_shm_handle *handle)
288 {
289 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages;
290 unsigned long sb_bindex;
291
292 sb_bindex = subbuffer_id_get_index(config, shmp_index(handle, bufb->buf_wsb, idx)->id);
293 pages = shmp_index(handle, bufb->array, sb_bindex);
294 shmp(handle, pages->shmp)->data_size = data_size;
295 }
296
297 static inline
298 unsigned long subbuffer_get_read_data_size(
299 const struct lttng_ust_lib_ring_buffer_config *config,
300 struct lttng_ust_lib_ring_buffer_backend *bufb,
301 struct lttng_ust_shm_handle *handle)
302 {
303 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages;
304 unsigned long sb_bindex;
305
306 sb_bindex = subbuffer_id_get_index(config, bufb->buf_rsb.id);
307 pages = shmp_index(handle, bufb->array, sb_bindex);
308 return shmp(handle, pages->shmp)->data_size;
309 }
310
311 static inline
312 unsigned long subbuffer_get_data_size(
313 const struct lttng_ust_lib_ring_buffer_config *config,
314 struct lttng_ust_lib_ring_buffer_backend *bufb,
315 unsigned long idx,
316 struct lttng_ust_shm_handle *handle)
317 {
318 struct lttng_ust_lib_ring_buffer_backend_pages_shmp *pages;
319 unsigned long sb_bindex;
320
321 sb_bindex = subbuffer_id_get_index(config, shmp_index(handle, bufb->buf_wsb, idx)->id);
322 pages = shmp_index(handle, bufb->array, sb_bindex);
323 return shmp(handle, pages->shmp)->data_size;
324 }
325
326 static inline
327 void subbuffer_inc_packet_count(const struct lttng_ust_lib_ring_buffer_config *config,
328 struct lttng_ust_lib_ring_buffer_backend *bufb,
329 unsigned long idx, struct lttng_ust_shm_handle *handle)
330 {
331 shmp_index(handle, bufb->buf_cnt, idx)->seq_cnt++;
332 }
333
334 /**
335 * lib_ring_buffer_clear_noref - Clear the noref subbuffer flag, called by
336 * writer.
337 */
338 static inline
339 void lib_ring_buffer_clear_noref(const struct lttng_ust_lib_ring_buffer_config *config,
340 struct lttng_ust_lib_ring_buffer_backend *bufb,
341 unsigned long idx,
342 struct lttng_ust_shm_handle *handle)
343 {
344 unsigned long id, new_id;
345
346 if (config->mode != RING_BUFFER_OVERWRITE)
347 return;
348
349 /*
350 * Performing a volatile access to read the sb_pages, because we want to
351 * read a coherent version of the pointer and the associated noref flag.
352 */
353 id = CMM_ACCESS_ONCE(shmp_index(handle, bufb->buf_wsb, idx)->id);
354 for (;;) {
355 /* This check is called on the fast path for each record. */
356 if (caa_likely(!subbuffer_id_is_noref(config, id))) {
357 /*
358 * Store after load dependency ordering the writes to
359 * the subbuffer after load and test of the noref flag
360 * matches the memory barrier implied by the cmpxchg()
361 * in update_read_sb_index().
362 */
363 return; /* Already writing to this buffer */
364 }
365 new_id = id;
366 subbuffer_id_clear_noref(config, &new_id);
367 new_id = uatomic_cmpxchg(&shmp_index(handle, bufb->buf_wsb, idx)->id, id, new_id);
368 if (caa_likely(new_id == id))
369 break;
370 id = new_id;
371 }
372 }
373
374 /**
375 * lib_ring_buffer_set_noref_offset - Set the noref subbuffer flag and offset,
376 * called by writer.
377 */
378 static inline
379 void lib_ring_buffer_set_noref_offset(const struct lttng_ust_lib_ring_buffer_config *config,
380 struct lttng_ust_lib_ring_buffer_backend *bufb,
381 unsigned long idx, unsigned long offset,
382 struct lttng_ust_shm_handle *handle)
383 {
384 if (config->mode != RING_BUFFER_OVERWRITE)
385 return;
386
387 /*
388 * Because ring_buffer_set_noref() is only called by a single thread
389 * (the one which updated the cc_sb value), there are no concurrent
390 * updates to take care of: other writers have not updated cc_sb, so
391 * they cannot set the noref flag, and concurrent readers cannot modify
392 * the pointer because the noref flag is not set yet.
393 * The smp_wmb() in ring_buffer_commit() takes care of ordering writes
394 * to the subbuffer before this set noref operation.
395 * subbuffer_set_noref() uses a volatile store to deal with concurrent
396 * readers of the noref flag.
397 */
398 CHAN_WARN_ON(shmp(handle, bufb->chan),
399 subbuffer_id_is_noref(config, shmp_index(handle, bufb->buf_wsb, idx)->id));
400 /*
401 * Memory barrier that ensures counter stores are ordered before set
402 * noref and offset.
403 */
404 cmm_smp_mb();
405 subbuffer_id_set_noref_offset(config, &shmp_index(handle, bufb->buf_wsb, idx)->id, offset);
406 }
407
408 /**
409 * update_read_sb_index - Read-side subbuffer index update.
410 */
411 static inline
412 int update_read_sb_index(const struct lttng_ust_lib_ring_buffer_config *config,
413 struct lttng_ust_lib_ring_buffer_backend *bufb,
414 struct channel_backend *chanb,
415 unsigned long consumed_idx,
416 unsigned long consumed_count,
417 struct lttng_ust_shm_handle *handle)
418 {
419 unsigned long old_id, new_id;
420
421 if (config->mode == RING_BUFFER_OVERWRITE) {
422 /*
423 * Exchange the target writer subbuffer with our own unused
424 * subbuffer. No need to use CMM_ACCESS_ONCE() here to read the
425 * old_wpage, because the value read will be confirmed by the
426 * following cmpxchg().
427 */
428 old_id = shmp_index(handle, bufb->buf_wsb, consumed_idx)->id;
429 if (caa_unlikely(!subbuffer_id_is_noref(config, old_id)))
430 return -EAGAIN;
431 /*
432 * Make sure the offset count we are expecting matches the one
433 * indicated by the writer.
434 */
435 if (caa_unlikely(!subbuffer_id_compare_offset(config, old_id,
436 consumed_count)))
437 return -EAGAIN;
438 CHAN_WARN_ON(shmp(handle, bufb->chan),
439 !subbuffer_id_is_noref(config, bufb->buf_rsb.id));
440 subbuffer_id_set_noref_offset(config, &bufb->buf_rsb.id,
441 consumed_count);
442 new_id = uatomic_cmpxchg(&shmp_index(handle, bufb->buf_wsb, consumed_idx)->id, old_id,
443 bufb->buf_rsb.id);
444 if (caa_unlikely(old_id != new_id))
445 return -EAGAIN;
446 bufb->buf_rsb.id = new_id;
447 } else {
448 /* No page exchange, use the writer page directly */
449 bufb->buf_rsb.id = shmp_index(handle, bufb->buf_wsb, consumed_idx)->id;
450 }
451 return 0;
452 }
453
454 #ifndef inline_memcpy
455 #define inline_memcpy(dest, src, n) memcpy(dest, src, n)
456 #endif
457
458 /*
459 * Use the architecture-specific memcpy implementation for constant-sized
460 * inputs, but rely on an inline memcpy for length statically unknown.
461 * The function call to memcpy is just way too expensive for a fast path.
462 */
463 #define lib_ring_buffer_do_copy(config, dest, src, len) \
464 do { \
465 size_t __len = (len); \
466 if (__builtin_constant_p(len)) \
467 memcpy(dest, src, __len); \
468 else \
469 inline_memcpy(dest, src, __len); \
470 } while (0)
471
472 /*
473 * write len bytes to dest with c
474 */
475 static inline
476 void lib_ring_buffer_do_memset(char *dest, int c, unsigned long len)
477 {
478 unsigned long i;
479
480 for (i = 0; i < len; i++)
481 dest[i] = c;
482 }
483
484 /* arch-agnostic implementation */
485
486 static inline int lttng_ust_fls(unsigned int x)
487 {
488 int r = 32;
489
490 if (!x)
491 return 0;
492 if (!(x & 0xFFFF0000U)) {
493 x <<= 16;
494 r -= 16;
495 }
496 if (!(x & 0xFF000000U)) {
497 x <<= 8;
498 r -= 8;
499 }
500 if (!(x & 0xF0000000U)) {
501 x <<= 4;
502 r -= 4;
503 }
504 if (!(x & 0xC0000000U)) {
505 x <<= 2;
506 r -= 2;
507 }
508 if (!(x & 0x80000000U)) {
509 /* No need to bit shift on last operation */
510 r -= 1;
511 }
512 return r;
513 }
514
515 static inline int get_count_order(unsigned int count)
516 {
517 int order;
518
519 order = lttng_ust_fls(count) - 1;
520 if (count & (count - 1))
521 order++;
522 return order;
523 }
524
525 #endif /* _LTTNG_RING_BUFFER_BACKEND_INTERNAL_H */
This page took 0.039419 seconds and 4 git commands to generate.