2 * ring_buffer_frontend.c
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; only
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Ring buffer wait-free buffer synchronization. Producer-consumer and flight
22 * recorder (overwrite) modes. See thesis:
24 * Desnoyers, Mathieu (2009), "Low-Impact Operating System Tracing", Ph.D.
25 * dissertation, Ecole Polytechnique de Montreal.
26 * http://www.lttng.org/pub/thesis/desnoyers-dissertation-2009-12.pdf
28 * - Algorithm presentation in Chapter 5:
29 * "Lockless Multi-Core High-Throughput Buffering".
30 * - Algorithm formal verification in Section 8.6:
31 * "Formal verification of LTTng"
34 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
36 * Inspired from LTT and RelayFS:
37 * Karim Yaghmour <karim@opersys.com>
38 * Tom Zanussi <zanussi@us.ibm.com>
39 * Bob Wisniewski <bob@watson.ibm.com>
41 * Bob Wisniewski <bob@watson.ibm.com>
43 * Buffer reader semantic :
46 * while buffer is not finalized and empty
48 * - if return value != 0, continue
49 * - splice one subbuffer worth of data to a pipe
50 * - splice the data from pipe to disk/network
55 #include <sys/types.h>
59 #include <urcu/compiler.h>
64 #include <lttng/ringbuffer-config.h>
70 #include "../liblttng-ust/compat.h" /* For ENODATA */
73 #define max(a, b) ((a) > (b) ? (a) : (b))
76 /* Print DBG() messages about events lost only every 1048576 hits */
77 #define DBG_PRINT_NR_LOST (1UL << 20)
80 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
81 * close(2) to close the fd returned by shm_open.
82 * shm_unlink releases the shared memory object name.
83 * ftruncate(2) sets the size of the memory object.
84 * mmap/munmap maps the shared memory obj to a virtual address in the
85 * calling proceess (should be done both in libust and consumer).
86 * See shm_overview(7) for details.
87 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
90 * Since we don't need to access the object using its name, we can
91 * immediately shm_unlink(3) it, and only keep the handle with its file
96 * Internal structure representing offsets to use at a sub-buffer switch.
98 struct switch_offsets
{
99 unsigned long begin
, end
, old
;
100 size_t pre_header_padding
, size
;
101 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
105 __thread
unsigned int lib_ring_buffer_nesting
;
108 * TODO: this is unused. Errors are saved within the ring buffer.
109 * Eventually, allow consumerd to print these errors.
112 void lib_ring_buffer_print_errors(struct channel
*chan
,
113 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
114 struct lttng_ust_shm_handle
*handle
)
115 __attribute__((unused
));
118 * lib_ring_buffer_reset - Reset ring buffer to initial values.
121 * Effectively empty the ring buffer. Should be called when the buffer is not
122 * used for writing. The ring buffer can be opened for reading, but the reader
123 * should not be using the iterator concurrently with reset. The previous
124 * current iterator record is reset.
126 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
127 struct lttng_ust_shm_handle
*handle
)
129 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
130 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
134 * Reset iterator first. It will put the subbuffer if it currently holds
137 v_set(config
, &buf
->offset
, 0);
138 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
139 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->cc
, 0);
140 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->seq
, 0);
141 v_set(config
, &shmp_index(handle
, buf
->commit_cold
, i
)->cc_sb
, 0);
143 uatomic_set(&buf
->consumed
, 0);
144 uatomic_set(&buf
->record_disabled
, 0);
145 v_set(config
, &buf
->last_tsc
, 0);
146 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
147 /* Don't reset number of active readers */
148 v_set(config
, &buf
->records_lost_full
, 0);
149 v_set(config
, &buf
->records_lost_wrap
, 0);
150 v_set(config
, &buf
->records_lost_big
, 0);
151 v_set(config
, &buf
->records_count
, 0);
152 v_set(config
, &buf
->records_overrun
, 0);
157 * channel_reset - Reset channel to initial values.
160 * Effectively empty the channel. Should be called when the channel is not used
161 * for writing. The channel can be opened for reading, but the reader should not
162 * be using the iterator concurrently with reset. The previous current iterator
165 void channel_reset(struct channel
*chan
)
168 * Reset iterators first. Will put the subbuffer if held for reading.
170 uatomic_set(&chan
->record_disabled
, 0);
171 /* Don't reset commit_count_mask, still valid */
172 channel_backend_reset(&chan
->backend
);
173 /* Don't reset switch/read timer interval */
174 /* Don't reset notifiers and notifier enable bits */
175 /* Don't reset reader reference count */
179 * Must be called under cpu hotplug protection.
181 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
182 struct channel_backend
*chanb
, int cpu
,
183 struct lttng_ust_shm_handle
*handle
,
184 struct shm_object
*shmobj
)
186 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
187 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
188 void *priv
= channel_get_private(chan
);
189 size_t subbuf_header_size
;
193 /* Test for cpu hotplug */
194 if (buf
->backend
.allocated
)
197 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
198 cpu
, handle
, shmobj
);
202 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
203 set_shmp(buf
->commit_hot
,
205 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
206 if (!shmp(handle
, buf
->commit_hot
)) {
211 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
212 set_shmp(buf
->commit_cold
,
214 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
215 if (!shmp(handle
, buf
->commit_cold
)) {
221 * Write the subbuffer header for first subbuffer so we know the total
222 * duration of data gathering.
224 subbuf_header_size
= config
->cb
.subbuffer_header_size();
225 v_set(config
, &buf
->offset
, subbuf_header_size
);
226 subbuffer_id_clear_noref(config
, &shmp_index(handle
, buf
->backend
.buf_wsb
, 0)->id
);
227 tsc
= config
->cb
.ring_buffer_clock_read(shmp(handle
, buf
->backend
.chan
));
228 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
229 v_add(config
, subbuf_header_size
, &shmp_index(handle
, buf
->commit_hot
, 0)->cc
);
231 if (config
->cb
.buffer_create
) {
232 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
236 buf
->backend
.allocated
= 1;
241 /* commit_cold will be freed by shm teardown */
243 /* commit_hot will be freed by shm teardown */
249 static void switch_buffer_timer(unsigned long data
)
251 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
252 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
253 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
256 * Only flush buffers periodically if readers are active.
258 if (uatomic_read(&buf
->active_readers
) || uatomic_read(&buf
->active_shadow_readers
))
259 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
, handle
);
262 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
263 // mod_timer_pinned(&buf->switch_timer,
264 // jiffies + chan->switch_timer_interval);
266 // mod_timer(&buf->switch_timer,
267 // jiffies + chan->switch_timer_interval);
271 static void lib_ring_buffer_start_switch_timer(struct lttng_ust_lib_ring_buffer
*buf
,
272 struct lttng_ust_shm_handle
*handle
)
274 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
275 //const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
277 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
280 //init_timer(&buf->switch_timer);
281 //buf->switch_timer.function = switch_buffer_timer;
282 //buf->switch_timer.expires = jiffies + chan->switch_timer_interval;
283 //buf->switch_timer.data = (unsigned long)buf;
284 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
285 // add_timer_on(&buf->switch_timer, buf->backend.cpu);
287 // add_timer(&buf->switch_timer);
288 buf
->switch_timer_enabled
= 1;
291 static void lib_ring_buffer_stop_switch_timer(struct lttng_ust_lib_ring_buffer
*buf
,
292 struct lttng_ust_shm_handle
*handle
)
294 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
296 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
300 //del_timer_sync(&buf->switch_timer);
301 buf
->switch_timer_enabled
= 0;
306 * Polling timer to check the channels for data.
308 static void read_buffer_timer(unsigned long data
)
310 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
311 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
312 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
314 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
316 if (uatomic_read(&buf
->active_readers
) || uatomic_read(&buf
->active_shadow_readers
))
317 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
319 //wake_up_interruptible(&buf->read_wait);
320 //wake_up_interruptible(&chan->read_wait);
324 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
325 // mod_timer_pinned(&buf->read_timer,
326 // jiffies + chan->read_timer_interval);
328 // mod_timer(&buf->read_timer,
329 // jiffies + chan->read_timer_interval);
333 static void lib_ring_buffer_start_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
334 struct lttng_ust_shm_handle
*handle
)
336 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
337 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
339 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
340 || !chan
->read_timer_interval
341 || buf
->read_timer_enabled
)
345 //init_timer(&buf->read_timer);
346 //buf->read_timer.function = read_buffer_timer;
347 //buf->read_timer.expires = jiffies + chan->read_timer_interval;
348 //buf->read_timer.data = (unsigned long)buf;
350 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
351 // add_timer_on(&buf->read_timer, buf->backend.cpu);
353 // add_timer(&buf->read_timer);
354 buf
->read_timer_enabled
= 1;
357 static void lib_ring_buffer_stop_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
358 struct lttng_ust_shm_handle
*handle
)
360 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
361 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
363 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
364 || !chan
->read_timer_interval
365 || !buf
->read_timer_enabled
)
369 //del_timer_sync(&buf->read_timer);
371 * do one more check to catch data that has been written in the last
374 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
376 //wake_up_interruptible(&buf->read_wait);
377 //wake_up_interruptible(&chan->read_wait);
379 buf
->read_timer_enabled
= 0;
382 static void channel_unregister_notifiers(struct channel
*chan
,
383 struct lttng_ust_shm_handle
*handle
)
385 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
388 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
389 for_each_possible_cpu(cpu
) {
390 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
392 lib_ring_buffer_stop_switch_timer(buf
, handle
);
393 lib_ring_buffer_stop_read_timer(buf
, handle
);
396 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
398 lib_ring_buffer_stop_switch_timer(buf
, handle
);
399 lib_ring_buffer_stop_read_timer(buf
, handle
);
401 //channel_backend_unregister_notifiers(&chan->backend);
404 static void channel_free(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
408 channel_backend_free(&chan
->backend
, handle
);
409 /* chan is freed by shm teardown */
410 shm_object_table_destroy(handle
->table
);
415 * channel_create - Create channel.
416 * @config: ring buffer instance configuration
417 * @name: name of the channel
418 * @priv_data: ring buffer client private data area pointer (output)
419 * @priv_data_size: length, in bytes, of the private data area.
420 * @priv_data_init: initialization data for private data.
421 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
422 * address mapping. It is used only by RING_BUFFER_STATIC
423 * configuration. It can be set to NULL for other backends.
424 * @subbuf_size: subbuffer size
425 * @num_subbuf: number of subbuffers
426 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
427 * padding to let readers get those sub-buffers.
428 * Used for live streaming.
429 * @read_timer_interval: Time interval (in us) to wake up pending readers.
432 * Returns NULL on failure.
434 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
437 size_t priv_data_align
,
438 size_t priv_data_size
,
439 void *priv_data_init
,
440 void *buf_addr
, size_t subbuf_size
,
441 size_t num_subbuf
, unsigned int switch_timer_interval
,
442 unsigned int read_timer_interval
,
443 int **shm_fd
, int **wait_fd
, uint64_t **memory_map_size
)
446 size_t shmsize
, chansize
;
447 struct channel
*chan
;
448 struct lttng_ust_shm_handle
*handle
;
449 struct shm_object
*shmobj
;
452 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
453 read_timer_interval
))
456 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
460 /* Allocate table for channel + per-cpu buffers */
461 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
463 goto error_table_alloc
;
465 /* Calculate the shm allocation layout */
466 shmsize
= sizeof(struct channel
);
467 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
468 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
469 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * num_possible_cpus();
471 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
);
473 shmsize
+= offset_align(shmsize
, priv_data_align
);
474 shmsize
+= priv_data_size
;
476 shmobj
= shm_object_table_append(handle
->table
, shmsize
);
479 /* struct channel is at object 0, offset 0 (hardcoded) */
480 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
481 assert(handle
->chan
._ref
.index
== 0);
482 assert(handle
->chan
._ref
.offset
== 0);
483 chan
= shmp(handle
, handle
->chan
);
487 /* space for private data */
488 if (priv_data_size
) {
489 DECLARE_SHMP(void, priv_data_alloc
);
491 align_shm(shmobj
, priv_data_align
);
492 chan
->priv_data_offset
= shmobj
->allocated_len
;
493 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
494 if (!shmp(handle
, priv_data_alloc
))
496 *priv_data
= channel_get_private(chan
);
497 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
499 chan
->priv_data_offset
= -1;
503 ret
= channel_backend_init(&chan
->backend
, name
, config
,
504 subbuf_size
, num_subbuf
, handle
);
506 goto error_backend_init
;
508 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
510 //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval);
511 //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval);
513 //init_waitqueue_head(&chan->read_wait);
514 //init_waitqueue_head(&chan->hp_wait);
516 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
518 * In case of non-hotplug cpu, if the ring-buffer is allocated
519 * in early initcall, it will not be notified of secondary cpus.
520 * In that off case, we need to allocate for all possible cpus.
522 for_each_possible_cpu(cpu
) {
523 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
524 lib_ring_buffer_start_switch_timer(buf
, handle
);
525 lib_ring_buffer_start_read_timer(buf
, handle
);
528 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
530 lib_ring_buffer_start_switch_timer(buf
, handle
);
531 lib_ring_buffer_start_read_timer(buf
, handle
);
533 ref
= &handle
->chan
._ref
;
534 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
, memory_map_size
);
539 shm_object_table_destroy(handle
->table
);
545 struct lttng_ust_shm_handle
*channel_handle_create(int shm_fd
, int wait_fd
,
546 uint64_t memory_map_size
)
548 struct lttng_ust_shm_handle
*handle
;
549 struct shm_object
*object
;
551 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
555 /* Allocate table for channel + per-cpu buffers */
556 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
558 goto error_table_alloc
;
559 /* Add channel object */
560 object
= shm_object_table_append_shadow(handle
->table
,
561 shm_fd
, wait_fd
, memory_map_size
);
563 goto error_table_object
;
564 /* struct channel is at object 0, offset 0 (hardcoded) */
565 handle
->chan
._ref
.index
= 0;
566 handle
->chan
._ref
.offset
= 0;
570 shm_object_table_destroy(handle
->table
);
576 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
577 int shm_fd
, int wait_fd
, uint64_t memory_map_size
)
579 struct shm_object
*object
;
581 /* Add stream object */
582 object
= shm_object_table_append_shadow(handle
->table
,
583 shm_fd
, wait_fd
, memory_map_size
);
590 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
593 channel_free(chan
, handle
, shadow
);
597 * channel_destroy - Finalize, wait for q.s. and destroy channel.
598 * @chan: channel to destroy
601 * Call "destroy" callback, finalize channels, decrement the channel
602 * reference count. Note that when readers have completed data
603 * consumption of finalized channels, get_subbuf() will return -ENODATA.
604 * They should release their handle at that point.
606 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
610 channel_release(chan
, handle
, shadow
);
614 channel_unregister_notifiers(chan
, handle
);
617 * Note: the consumer takes care of finalizing and switching the
622 * sessiond/consumer are keeping a reference on the shm file
623 * descriptor directly. No need to refcount.
625 channel_release(chan
, handle
, shadow
);
629 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
630 const struct lttng_ust_lib_ring_buffer_config
*config
,
631 struct channel
*chan
, int cpu
,
632 struct lttng_ust_shm_handle
*handle
,
633 int **shm_fd
, int **wait_fd
,
634 uint64_t **memory_map_size
)
638 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
639 ref
= &chan
->backend
.buf
[0].shmp
._ref
;
640 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
,
642 return shmp(handle
, chan
->backend
.buf
[0].shmp
);
644 if (cpu
>= num_possible_cpus())
646 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
647 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
,
649 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
653 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
654 struct lttng_ust_shm_handle
*handle
,
658 if (uatomic_cmpxchg(&buf
->active_shadow_readers
, 0, 1) != 0)
663 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
669 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
670 struct lttng_ust_shm_handle
*handle
,
673 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
676 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_shadow_readers
) != 1);
678 uatomic_dec(&buf
->active_shadow_readers
);
681 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
683 uatomic_dec(&buf
->active_readers
);
687 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
689 * @consumed: consumed count indicating the position where to read
690 * @produced: produced count, indicates position when to stop reading
692 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
693 * data to read at consumed position, or 0 if the get operation succeeds.
696 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
697 unsigned long *consumed
, unsigned long *produced
,
698 struct lttng_ust_shm_handle
*handle
)
700 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
701 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
702 unsigned long consumed_cur
, write_offset
;
705 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
707 * Read finalized before counters.
710 consumed_cur
= uatomic_read(&buf
->consumed
);
712 * No need to issue a memory barrier between consumed count read and
713 * write offset read, because consumed count can only change
714 * concurrently in overwrite mode, and we keep a sequence counter
715 * identifier derived from the write offset to check we are getting
716 * the same sub-buffer we are expecting (the sub-buffers are atomically
717 * "tagged" upon writes, tags are checked upon read).
719 write_offset
= v_read(config
, &buf
->offset
);
722 * Check that we are not about to read the same subbuffer in
723 * which the writer head is.
725 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
729 *consumed
= consumed_cur
;
730 *produced
= subbuf_trunc(write_offset
, chan
);
736 * The memory barriers __wait_event()/wake_up_interruptible() take care
737 * of "raw_spin_is_locked" memory ordering.
746 * lib_ring_buffer_put_snapshot - move consumed counter forward
748 * @consumed_new: new consumed count value
750 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
751 unsigned long consumed_new
,
752 struct lttng_ust_shm_handle
*handle
)
754 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
755 struct channel
*chan
= shmp(handle
, bufb
->chan
);
756 unsigned long consumed
;
758 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1
759 && uatomic_read(&buf
->active_shadow_readers
) != 1);
762 * Only push the consumed value forward.
763 * If the consumed cmpxchg fails, this is because we have been pushed by
764 * the writer in flight recorder mode.
766 consumed
= uatomic_read(&buf
->consumed
);
767 while ((long) consumed
- (long) consumed_new
< 0)
768 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
773 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
775 * @consumed: consumed count indicating the position where to read
777 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
778 * data to read at consumed position, or 0 if the get operation succeeds.
780 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
781 unsigned long consumed
,
782 struct lttng_ust_shm_handle
*handle
)
784 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
785 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
786 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
791 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
793 * Read finalized before counters.
796 consumed_cur
= uatomic_read(&buf
->consumed
);
797 consumed_idx
= subbuf_index(consumed
, chan
);
798 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, consumed_idx
)->cc_sb
);
800 * Make sure we read the commit count before reading the buffer
801 * data and the write offset. Correct consumed offset ordering
802 * wrt commit count is insured by the use of cmpxchg to update
803 * the consumed offset.
806 * Local rmb to match the remote wmb to read the commit count
807 * before the buffer data and the write offset.
811 write_offset
= v_read(config
, &buf
->offset
);
814 * Check that the buffer we are getting is after or at consumed_cur
817 if ((long) subbuf_trunc(consumed
, chan
)
818 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
822 * Check that the subbuffer we are trying to consume has been
823 * already fully committed.
825 if (((commit_count
- chan
->backend
.subbuf_size
)
826 & chan
->commit_count_mask
)
827 - (buf_trunc(consumed_cur
, chan
)
828 >> chan
->backend
.num_subbuf_order
)
833 * Check that we are not about to read the same subbuffer in
834 * which the writer head is.
836 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
841 * Failure to get the subbuffer causes a busy-loop retry without going
842 * to a wait queue. These are caused by short-lived race windows where
843 * the writer is getting access to a subbuffer we were trying to get
844 * access to. Also checks that the "consumed" buffer count we are
845 * looking for matches the one contained in the subbuffer id.
847 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
848 consumed_idx
, buf_trunc_val(consumed
, chan
),
852 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
854 buf
->get_subbuf_consumed
= consumed
;
861 * The memory barriers __wait_event()/wake_up_interruptible() take care
862 * of "raw_spin_is_locked" memory ordering.
871 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
874 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
875 struct lttng_ust_shm_handle
*handle
)
877 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
878 struct channel
*chan
= shmp(handle
, bufb
->chan
);
879 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
880 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
882 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1
883 && uatomic_read(&buf
->active_shadow_readers
) != 1);
885 if (!buf
->get_subbuf
) {
887 * Reader puts a subbuffer it did not get.
889 CHAN_WARN_ON(chan
, 1);
892 consumed
= buf
->get_subbuf_consumed
;
896 * Clear the records_unread counter. (overruns counter)
897 * Can still be non-zero if a file reader simply grabbed the data
898 * without using iterators.
899 * Can be below zero if an iterator is used on a snapshot more than
902 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
903 v_add(config
, v_read(config
,
904 &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
),
905 &bufb
->records_read
);
906 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
, 0);
907 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
908 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
909 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
912 * Exchange the reader subbuffer with the one we put in its place in the
913 * writer subbuffer table. Expect the original consumed count. If
914 * update_read_sb_index fails, this is because the writer updated the
915 * subbuffer concurrently. We should therefore keep the subbuffer we
916 * currently have: it has become invalid to try reading this sub-buffer
917 * consumed count value anyway.
919 consumed_idx
= subbuf_index(consumed
, chan
);
920 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
921 consumed_idx
, buf_trunc_val(consumed
, chan
),
924 * update_read_sb_index return value ignored. Don't exchange sub-buffer
925 * if the writer concurrently updated it.
930 * cons_offset is an iterator on all subbuffer offsets between the reader
931 * position and the writer position. (inclusive)
934 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
935 struct channel
*chan
,
936 unsigned long cons_offset
,
938 struct lttng_ust_shm_handle
*handle
)
940 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
941 unsigned long cons_idx
, commit_count
, commit_count_sb
;
943 cons_idx
= subbuf_index(cons_offset
, chan
);
944 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, cons_idx
)->cc
);
945 commit_count_sb
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, cons_idx
)->cc_sb
);
947 if (subbuf_offset(commit_count
, chan
) != 0)
948 DBG("ring buffer %s, cpu %d: "
949 "commit count in subbuffer %lu,\n"
950 "expecting multiples of %lu bytes\n"
951 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
952 chan
->backend
.name
, cpu
, cons_idx
,
953 chan
->backend
.subbuf_size
,
954 commit_count
, commit_count_sb
);
956 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
957 chan
->backend
.name
, cpu
, commit_count
);
961 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
962 struct channel
*chan
,
964 struct lttng_ust_shm_handle
*handle
)
966 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
967 unsigned long write_offset
, cons_offset
;
970 * No need to order commit_count, write_offset and cons_offset reads
971 * because we execute at teardown when no more writer nor reader
972 * references are left.
974 write_offset
= v_read(config
, &buf
->offset
);
975 cons_offset
= uatomic_read(&buf
->consumed
);
976 if (write_offset
!= cons_offset
)
977 DBG("ring buffer %s, cpu %d: "
978 "non-consumed data\n"
979 " [ %lu bytes written, %lu bytes read ]\n",
980 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
982 for (cons_offset
= uatomic_read(&buf
->consumed
);
983 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
986 cons_offset
= subbuf_align(cons_offset
, chan
))
987 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
992 void lib_ring_buffer_print_errors(struct channel
*chan
,
993 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
994 struct lttng_ust_shm_handle
*handle
)
996 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
997 void *priv
= channel_get_private(chan
);
999 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1000 DBG("ring buffer %s: %lu records written, "
1001 "%lu records overrun\n",
1003 v_read(config
, &buf
->records_count
),
1004 v_read(config
, &buf
->records_overrun
));
1006 DBG("ring buffer %s, cpu %d: %lu records written, "
1007 "%lu records overrun\n",
1008 chan
->backend
.name
, cpu
,
1009 v_read(config
, &buf
->records_count
),
1010 v_read(config
, &buf
->records_overrun
));
1012 if (v_read(config
, &buf
->records_lost_full
)
1013 || v_read(config
, &buf
->records_lost_wrap
)
1014 || v_read(config
, &buf
->records_lost_big
))
1015 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1016 " [ %lu buffer full, %lu nest buffer wrap-around, "
1017 "%lu event too big ]\n",
1018 chan
->backend
.name
, cpu
,
1019 v_read(config
, &buf
->records_lost_full
),
1020 v_read(config
, &buf
->records_lost_wrap
),
1021 v_read(config
, &buf
->records_lost_big
));
1023 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1027 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1029 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1032 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1033 struct channel
*chan
,
1034 struct switch_offsets
*offsets
,
1036 struct lttng_ust_shm_handle
*handle
)
1038 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1039 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1040 unsigned long commit_count
;
1042 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1045 * Order all writes to buffer before the commit count update that will
1046 * determine that the subbuffer is full.
1049 v_add(config
, config
->cb
.subbuffer_header_size(),
1050 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1051 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1052 /* Check if the written buffer has to be delivered */
1053 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1054 commit_count
, oldidx
, handle
);
1055 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1056 offsets
->old
, commit_count
,
1057 config
->cb
.subbuffer_header_size(),
1062 * lib_ring_buffer_switch_old_end: switch old subbuffer
1064 * Note : offset_old should never be 0 here. It is ok, because we never perform
1065 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1066 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1070 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1071 struct channel
*chan
,
1072 struct switch_offsets
*offsets
,
1074 struct lttng_ust_shm_handle
*handle
)
1076 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1077 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1078 unsigned long commit_count
, padding_size
, data_size
;
1080 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1081 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1082 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1086 * Order all writes to buffer before the commit count update that will
1087 * determine that the subbuffer is full.
1090 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1091 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1092 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1093 commit_count
, oldidx
, handle
);
1094 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1095 offsets
->old
, commit_count
,
1096 padding_size
, handle
);
1100 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1102 * This code can be executed unordered : writers may already have written to the
1103 * sub-buffer before this code gets executed, caution. The commit makes sure
1104 * that this code is executed before the deliver of this sub-buffer.
1107 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1108 struct channel
*chan
,
1109 struct switch_offsets
*offsets
,
1111 struct lttng_ust_shm_handle
*handle
)
1113 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1114 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1115 unsigned long commit_count
;
1117 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1120 * Order all writes to buffer before the commit count update that will
1121 * determine that the subbuffer is full.
1124 v_add(config
, config
->cb
.subbuffer_header_size(),
1125 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1126 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1127 /* Check if the written buffer has to be delivered */
1128 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1129 commit_count
, beginidx
, handle
);
1130 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1131 offsets
->begin
, commit_count
,
1132 config
->cb
.subbuffer_header_size(),
1137 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1139 * The only remaining threads could be the ones with pending commits. They will
1140 * have to do the deliver themselves.
1143 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1144 struct channel
*chan
,
1145 struct switch_offsets
*offsets
,
1147 struct lttng_ust_shm_handle
*handle
)
1149 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1150 unsigned long endidx
= subbuf_index(offsets
->end
- 1, chan
);
1151 unsigned long commit_count
, padding_size
, data_size
;
1153 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1154 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1155 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1159 * Order all writes to buffer before the commit count update that will
1160 * determine that the subbuffer is full.
1163 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1164 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1165 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->end
- 1,
1166 commit_count
, endidx
, handle
);
1167 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
1168 offsets
->end
, commit_count
,
1169 padding_size
, handle
);
1175 * !0 if execution must be aborted.
1178 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1179 struct lttng_ust_lib_ring_buffer
*buf
,
1180 struct channel
*chan
,
1181 struct switch_offsets
*offsets
,
1184 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1187 offsets
->begin
= v_read(config
, &buf
->offset
);
1188 offsets
->old
= offsets
->begin
;
1189 offsets
->switch_old_start
= 0;
1190 off
= subbuf_offset(offsets
->begin
, chan
);
1192 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1195 * Ensure we flush the header of an empty subbuffer when doing the
1196 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1197 * total data gathering duration even if there were no records saved
1198 * after the last buffer switch.
1199 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1200 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1201 * subbuffer header as appropriate.
1202 * The next record that reserves space will be responsible for
1203 * populating the following subbuffer header. We choose not to populate
1204 * the next subbuffer header here because we want to be able to use
1205 * SWITCH_ACTIVE for periodical buffer flush, which must
1206 * guarantee that all the buffer content (records and header
1207 * timestamps) are visible to the reader. This is required for
1208 * quiescence guarantees for the fusion merge.
1210 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1211 if (caa_unlikely(off
== 0)) {
1213 * The client does not save any header information.
1214 * Don't switch empty subbuffer on finalize, because it
1215 * is invalid to deliver a completely empty subbuffer.
1217 if (!config
->cb
.subbuffer_header_size())
1220 * Need to write the subbuffer start header on finalize.
1222 offsets
->switch_old_start
= 1;
1224 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1226 return -1; /* we do not have to switch : buffer is empty */
1227 /* Note: old points to the next subbuf at offset 0 */
1228 offsets
->end
= offsets
->begin
;
1233 * Force a sub-buffer switch. This operation is completely reentrant : can be
1234 * called while tracing is active with absolutely no lock held.
1236 * Note, however, that as a v_cmpxchg is used for some atomic
1237 * operations, this function must be called from the CPU which owns the buffer
1238 * for a ACTIVE flush.
1240 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1241 struct lttng_ust_shm_handle
*handle
)
1243 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1244 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1245 struct switch_offsets offsets
;
1246 unsigned long oldidx
;
1252 * Perform retryable operations.
1255 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1257 return; /* Switch not needed */
1258 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1262 * Atomically update last_tsc. This update races against concurrent
1263 * atomic updates, but the race will always cause supplementary full TSC
1264 * records, never the opposite (missing a full TSC record when it would
1267 save_last_tsc(config
, buf
, tsc
);
1270 * Push the reader if necessary
1272 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1274 oldidx
= subbuf_index(offsets
.old
, chan
);
1275 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1278 * May need to populate header start on SWITCH_FLUSH.
1280 if (offsets
.switch_old_start
) {
1281 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1282 offsets
.old
+= config
->cb
.subbuffer_header_size();
1286 * Switch old subbuffer.
1288 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1294 * -ENOSPC if event size is too large for packet.
1295 * -ENOBUFS if there is currently not enough space in buffer for the event.
1296 * -EIO if data cannot be written into the buffer for any other reason.
1299 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
1300 struct channel
*chan
,
1301 struct switch_offsets
*offsets
,
1302 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1304 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1305 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1306 unsigned long reserve_commit_diff
;
1308 offsets
->begin
= v_read(config
, &buf
->offset
);
1309 offsets
->old
= offsets
->begin
;
1310 offsets
->switch_new_start
= 0;
1311 offsets
->switch_new_end
= 0;
1312 offsets
->switch_old_end
= 0;
1313 offsets
->pre_header_padding
= 0;
1315 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1316 if ((int64_t) ctx
->tsc
== -EIO
)
1319 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1320 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1322 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1323 offsets
->switch_new_start
= 1; /* For offsets->begin */
1325 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1327 &offsets
->pre_header_padding
,
1330 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1333 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
1334 offsets
->size
> chan
->backend
.subbuf_size
)) {
1335 offsets
->switch_old_end
= 1; /* For offsets->old */
1336 offsets
->switch_new_start
= 1; /* For offsets->begin */
1339 if (caa_unlikely(offsets
->switch_new_start
)) {
1340 unsigned long sb_index
;
1343 * We are typically not filling the previous buffer completely.
1345 if (caa_likely(offsets
->switch_old_end
))
1346 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1347 offsets
->begin
= offsets
->begin
1348 + config
->cb
.subbuffer_header_size();
1349 /* Test new buffer integrity */
1350 sb_index
= subbuf_index(offsets
->begin
, chan
);
1351 reserve_commit_diff
=
1352 (buf_trunc(offsets
->begin
, chan
)
1353 >> chan
->backend
.num_subbuf_order
)
1354 - ((unsigned long) v_read(config
,
1355 &shmp_index(handle
, buf
->commit_cold
, sb_index
)->cc_sb
)
1356 & chan
->commit_count_mask
);
1357 if (caa_likely(reserve_commit_diff
== 0)) {
1358 /* Next subbuffer not being written to. */
1359 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1360 subbuf_trunc(offsets
->begin
, chan
)
1361 - subbuf_trunc((unsigned long)
1362 uatomic_read(&buf
->consumed
), chan
)
1363 >= chan
->backend
.buf_size
)) {
1364 unsigned long nr_lost
;
1367 * We do not overwrite non consumed buffers
1368 * and we are full : record is lost.
1370 nr_lost
= v_read(config
, &buf
->records_lost_full
);
1371 v_inc(config
, &buf
->records_lost_full
);
1372 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1373 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
1374 nr_lost
+ 1, chan
->backend
.name
,
1380 * Next subbuffer not being written to, and we
1381 * are either in overwrite mode or the buffer is
1382 * not full. It's safe to write in this new
1387 unsigned long nr_lost
;
1390 * Next subbuffer reserve offset does not match the
1391 * commit offset. Drop record in producer-consumer and
1392 * overwrite mode. Caused by either a writer OOPS or too
1393 * many nested writes over a reserve/commit pair.
1395 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
1396 v_inc(config
, &buf
->records_lost_wrap
);
1397 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1398 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
1399 nr_lost
+ 1, chan
->backend
.name
,
1405 config
->cb
.record_header_size(config
, chan
,
1407 &offsets
->pre_header_padding
,
1410 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1413 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
1414 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1415 unsigned long nr_lost
;
1418 * Record too big for subbuffers, report error, don't
1419 * complete the sub-buffer switch.
1421 nr_lost
= v_read(config
, &buf
->records_lost_big
);
1422 v_inc(config
, &buf
->records_lost_big
);
1423 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1424 DBG("%lu or more records lost in (%s:%d) record size "
1425 " of %zu bytes is too large for buffer\n",
1426 nr_lost
+ 1, chan
->backend
.name
,
1427 buf
->backend
.cpu
, offsets
->size
);
1432 * We just made a successful buffer switch and the
1433 * record fits in the new subbuffer. Let's write.
1438 * Record fits in the current buffer and we are not on a switch
1439 * boundary. It's safe to write.
1442 offsets
->end
= offsets
->begin
+ offsets
->size
;
1444 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1446 * The offset_end will fall at the very beginning of the next
1449 offsets
->switch_new_end
= 1; /* For offsets->begin */
1455 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1456 * @ctx: ring buffer context.
1458 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1459 * -EIO for other errors, else returns 0.
1460 * It will take care of sub-buffer switching.
1462 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1464 struct channel
*chan
= ctx
->chan
;
1465 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1466 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1467 struct lttng_ust_lib_ring_buffer
*buf
;
1468 struct switch_offsets offsets
;
1471 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1472 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1474 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
1480 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1482 if (caa_unlikely(ret
))
1484 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1489 * Atomically update last_tsc. This update races against concurrent
1490 * atomic updates, but the race will always cause supplementary full TSC
1491 * records, never the opposite (missing a full TSC record when it would
1494 save_last_tsc(config
, buf
, ctx
->tsc
);
1497 * Push the reader if necessary
1499 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1502 * Clear noref flag for this subbuffer.
1504 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1505 subbuf_index(offsets
.end
- 1, chan
),
1509 * Switch old subbuffer if needed.
1511 if (caa_unlikely(offsets
.switch_old_end
)) {
1512 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1513 subbuf_index(offsets
.old
- 1, chan
),
1515 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1519 * Populate new subbuffer.
1521 if (caa_unlikely(offsets
.switch_new_start
))
1522 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1524 if (caa_unlikely(offsets
.switch_new_end
))
1525 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1527 ctx
->slot_size
= offsets
.size
;
1528 ctx
->pre_offset
= offsets
.begin
;
1529 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1534 * Force a read (imply TLS fixup for dlopen) of TLS variables.
1536 void lttng_fixup_ringbuffer_tls(void)
1538 asm volatile ("" : : "m" (lib_ring_buffer_nesting
));