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>
72 #define max(a, b) ((a) > (b) ? (a) : (b))
75 /* Print DBG() messages about events lost only every 1048576 hits */
76 #define DBG_PRINT_NR_LOST (1UL << 20)
79 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
80 * close(2) to close the fd returned by shm_open.
81 * shm_unlink releases the shared memory object name.
82 * ftruncate(2) sets the size of the memory object.
83 * mmap/munmap maps the shared memory obj to a virtual address in the
84 * calling proceess (should be done both in libust and consumer).
85 * See shm_overview(7) for details.
86 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
89 * Since we don't need to access the object using its name, we can
90 * immediately shm_unlink(3) it, and only keep the handle with its file
95 * Internal structure representing offsets to use at a sub-buffer switch.
97 struct switch_offsets
{
98 unsigned long begin
, end
, old
;
99 size_t pre_header_padding
, size
;
100 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
104 __thread
unsigned int lib_ring_buffer_nesting
;
107 * TODO: this is unused. Errors are saved within the ring buffer.
108 * Eventually, allow consumerd to print these errors.
111 void lib_ring_buffer_print_errors(struct channel
*chan
,
112 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
113 struct lttng_ust_shm_handle
*handle
)
114 __attribute__((unused
));
117 * lib_ring_buffer_reset - Reset ring buffer to initial values.
120 * Effectively empty the ring buffer. Should be called when the buffer is not
121 * used for writing. The ring buffer can be opened for reading, but the reader
122 * should not be using the iterator concurrently with reset. The previous
123 * current iterator record is reset.
125 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
126 struct lttng_ust_shm_handle
*handle
)
128 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
129 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
133 * Reset iterator first. It will put the subbuffer if it currently holds
136 v_set(config
, &buf
->offset
, 0);
137 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
138 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->cc
, 0);
139 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->seq
, 0);
140 v_set(config
, &shmp_index(handle
, buf
->commit_cold
, i
)->cc_sb
, 0);
142 uatomic_set(&buf
->consumed
, 0);
143 uatomic_set(&buf
->record_disabled
, 0);
144 v_set(config
, &buf
->last_tsc
, 0);
145 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
146 /* Don't reset number of active readers */
147 v_set(config
, &buf
->records_lost_full
, 0);
148 v_set(config
, &buf
->records_lost_wrap
, 0);
149 v_set(config
, &buf
->records_lost_big
, 0);
150 v_set(config
, &buf
->records_count
, 0);
151 v_set(config
, &buf
->records_overrun
, 0);
156 * channel_reset - Reset channel to initial values.
159 * Effectively empty the channel. Should be called when the channel is not used
160 * for writing. The channel can be opened for reading, but the reader should not
161 * be using the iterator concurrently with reset. The previous current iterator
164 void channel_reset(struct channel
*chan
)
167 * Reset iterators first. Will put the subbuffer if held for reading.
169 uatomic_set(&chan
->record_disabled
, 0);
170 /* Don't reset commit_count_mask, still valid */
171 channel_backend_reset(&chan
->backend
);
172 /* Don't reset switch/read timer interval */
173 /* Don't reset notifiers and notifier enable bits */
174 /* Don't reset reader reference count */
178 * Must be called under cpu hotplug protection.
180 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
181 struct channel_backend
*chanb
, int cpu
,
182 struct lttng_ust_shm_handle
*handle
,
183 struct shm_object
*shmobj
)
185 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
186 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
187 void *priv
= channel_get_private(chan
);
188 size_t subbuf_header_size
;
192 /* Test for cpu hotplug */
193 if (buf
->backend
.allocated
)
196 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
197 cpu
, handle
, shmobj
);
201 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
202 set_shmp(buf
->commit_hot
,
204 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
205 if (!shmp(handle
, buf
->commit_hot
)) {
210 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
211 set_shmp(buf
->commit_cold
,
213 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
214 if (!shmp(handle
, buf
->commit_cold
)) {
220 * Write the subbuffer header for first subbuffer so we know the total
221 * duration of data gathering.
223 subbuf_header_size
= config
->cb
.subbuffer_header_size();
224 v_set(config
, &buf
->offset
, subbuf_header_size
);
225 subbuffer_id_clear_noref(config
, &shmp_index(handle
, buf
->backend
.buf_wsb
, 0)->id
);
226 tsc
= config
->cb
.ring_buffer_clock_read(shmp(handle
, buf
->backend
.chan
));
227 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
228 v_add(config
, subbuf_header_size
, &shmp_index(handle
, buf
->commit_hot
, 0)->cc
);
230 if (config
->cb
.buffer_create
) {
231 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
235 buf
->backend
.allocated
= 1;
240 /* commit_cold will be freed by shm teardown */
242 /* commit_hot will be freed by shm teardown */
248 static void switch_buffer_timer(unsigned long data
)
250 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
251 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
252 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
255 * Only flush buffers periodically if readers are active.
257 if (uatomic_read(&buf
->active_readers
) || uatomic_read(&buf
->active_shadow_readers
))
258 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
, handle
);
261 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
262 // mod_timer_pinned(&buf->switch_timer,
263 // jiffies + chan->switch_timer_interval);
265 // mod_timer(&buf->switch_timer,
266 // jiffies + chan->switch_timer_interval);
270 static void lib_ring_buffer_start_switch_timer(struct lttng_ust_lib_ring_buffer
*buf
,
271 struct lttng_ust_shm_handle
*handle
)
273 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
274 //const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
276 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
279 //init_timer(&buf->switch_timer);
280 //buf->switch_timer.function = switch_buffer_timer;
281 //buf->switch_timer.expires = jiffies + chan->switch_timer_interval;
282 //buf->switch_timer.data = (unsigned long)buf;
283 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
284 // add_timer_on(&buf->switch_timer, buf->backend.cpu);
286 // add_timer(&buf->switch_timer);
287 buf
->switch_timer_enabled
= 1;
290 static void lib_ring_buffer_stop_switch_timer(struct lttng_ust_lib_ring_buffer
*buf
,
291 struct lttng_ust_shm_handle
*handle
)
293 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
295 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
299 //del_timer_sync(&buf->switch_timer);
300 buf
->switch_timer_enabled
= 0;
305 * Polling timer to check the channels for data.
307 static void read_buffer_timer(unsigned long data
)
309 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
310 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
311 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
313 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
315 if (uatomic_read(&buf
->active_readers
) || uatomic_read(&buf
->active_shadow_readers
))
316 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
318 //wake_up_interruptible(&buf->read_wait);
319 //wake_up_interruptible(&chan->read_wait);
323 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
324 // mod_timer_pinned(&buf->read_timer,
325 // jiffies + chan->read_timer_interval);
327 // mod_timer(&buf->read_timer,
328 // jiffies + chan->read_timer_interval);
332 static void lib_ring_buffer_start_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
333 struct lttng_ust_shm_handle
*handle
)
335 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
336 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
338 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
339 || !chan
->read_timer_interval
340 || buf
->read_timer_enabled
)
344 //init_timer(&buf->read_timer);
345 //buf->read_timer.function = read_buffer_timer;
346 //buf->read_timer.expires = jiffies + chan->read_timer_interval;
347 //buf->read_timer.data = (unsigned long)buf;
349 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
350 // add_timer_on(&buf->read_timer, buf->backend.cpu);
352 // add_timer(&buf->read_timer);
353 buf
->read_timer_enabled
= 1;
356 static void lib_ring_buffer_stop_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
357 struct lttng_ust_shm_handle
*handle
)
359 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
360 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
362 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
363 || !chan
->read_timer_interval
364 || !buf
->read_timer_enabled
)
368 //del_timer_sync(&buf->read_timer);
370 * do one more check to catch data that has been written in the last
373 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
375 //wake_up_interruptible(&buf->read_wait);
376 //wake_up_interruptible(&chan->read_wait);
378 buf
->read_timer_enabled
= 0;
381 static void channel_unregister_notifiers(struct channel
*chan
,
382 struct lttng_ust_shm_handle
*handle
)
384 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
387 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
388 for_each_possible_cpu(cpu
) {
389 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
391 lib_ring_buffer_stop_switch_timer(buf
, handle
);
392 lib_ring_buffer_stop_read_timer(buf
, handle
);
395 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
397 lib_ring_buffer_stop_switch_timer(buf
, handle
);
398 lib_ring_buffer_stop_read_timer(buf
, handle
);
400 //channel_backend_unregister_notifiers(&chan->backend);
403 static void channel_free(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
407 channel_backend_free(&chan
->backend
, handle
);
408 /* chan is freed by shm teardown */
409 shm_object_table_destroy(handle
->table
);
414 * channel_create - Create channel.
415 * @config: ring buffer instance configuration
416 * @name: name of the channel
417 * @priv_data: ring buffer client private data area pointer (output)
418 * @priv_data_size: length, in bytes, of the private data area.
419 * @priv_data_init: initialization data for private data.
420 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
421 * address mapping. It is used only by RING_BUFFER_STATIC
422 * configuration. It can be set to NULL for other backends.
423 * @subbuf_size: subbuffer size
424 * @num_subbuf: number of subbuffers
425 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
426 * padding to let readers get those sub-buffers.
427 * Used for live streaming.
428 * @read_timer_interval: Time interval (in us) to wake up pending readers.
431 * Returns NULL on failure.
433 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
436 size_t priv_data_align
,
437 size_t priv_data_size
,
438 void *priv_data_init
,
439 void *buf_addr
, size_t subbuf_size
,
440 size_t num_subbuf
, unsigned int switch_timer_interval
,
441 unsigned int read_timer_interval
,
442 int **shm_fd
, char **shm_path
,
443 int **wait_fd
, char **wait_pipe_path
, 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
, shm_path
, wait_fd
, wait_pipe_path
, 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
, char **shm_path
,
634 int **wait_fd
, char **wait_pipe_path
,
635 uint64_t **memory_map_size
)
639 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
640 ref
= &chan
->backend
.buf
[0].shmp
._ref
;
641 shm_get_object_data(handle
, ref
, shm_fd
, shm_path
,
642 wait_fd
, wait_pipe_path
, memory_map_size
);
643 return shmp(handle
, chan
->backend
.buf
[0].shmp
);
645 if (cpu
>= num_possible_cpus())
647 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
648 shm_get_object_data(handle
, ref
, shm_fd
, shm_path
,
649 wait_fd
, wait_pipe_path
, memory_map_size
);
650 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
654 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
655 struct lttng_ust_shm_handle
*handle
,
659 if (uatomic_cmpxchg(&buf
->active_shadow_readers
, 0, 1) != 0)
664 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
670 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
671 struct lttng_ust_shm_handle
*handle
,
674 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
677 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_shadow_readers
) != 1);
679 uatomic_dec(&buf
->active_shadow_readers
);
682 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
684 uatomic_dec(&buf
->active_readers
);
688 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
690 * @consumed: consumed count indicating the position where to read
691 * @produced: produced count, indicates position when to stop reading
693 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
694 * data to read at consumed position, or 0 if the get operation succeeds.
697 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
698 unsigned long *consumed
, unsigned long *produced
,
699 struct lttng_ust_shm_handle
*handle
)
701 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
702 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
703 unsigned long consumed_cur
, write_offset
;
706 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
708 * Read finalized before counters.
711 consumed_cur
= uatomic_read(&buf
->consumed
);
713 * No need to issue a memory barrier between consumed count read and
714 * write offset read, because consumed count can only change
715 * concurrently in overwrite mode, and we keep a sequence counter
716 * identifier derived from the write offset to check we are getting
717 * the same sub-buffer we are expecting (the sub-buffers are atomically
718 * "tagged" upon writes, tags are checked upon read).
720 write_offset
= v_read(config
, &buf
->offset
);
723 * Check that we are not about to read the same subbuffer in
724 * which the writer head is.
726 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
730 *consumed
= consumed_cur
;
731 *produced
= subbuf_trunc(write_offset
, chan
);
737 * The memory barriers __wait_event()/wake_up_interruptible() take care
738 * of "raw_spin_is_locked" memory ordering.
747 * lib_ring_buffer_put_snapshot - move consumed counter forward
749 * @consumed_new: new consumed count value
751 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
752 unsigned long consumed_new
,
753 struct lttng_ust_shm_handle
*handle
)
755 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
756 struct channel
*chan
= shmp(handle
, bufb
->chan
);
757 unsigned long consumed
;
759 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1
760 && uatomic_read(&buf
->active_shadow_readers
) != 1);
763 * Only push the consumed value forward.
764 * If the consumed cmpxchg fails, this is because we have been pushed by
765 * the writer in flight recorder mode.
767 consumed
= uatomic_read(&buf
->consumed
);
768 while ((long) consumed
- (long) consumed_new
< 0)
769 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
774 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
776 * @consumed: consumed count indicating the position where to read
778 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
779 * data to read at consumed position, or 0 if the get operation succeeds.
781 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
782 unsigned long consumed
,
783 struct lttng_ust_shm_handle
*handle
)
785 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
786 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
787 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
792 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
794 * Read finalized before counters.
797 consumed_cur
= uatomic_read(&buf
->consumed
);
798 consumed_idx
= subbuf_index(consumed
, chan
);
799 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, consumed_idx
)->cc_sb
);
801 * Make sure we read the commit count before reading the buffer
802 * data and the write offset. Correct consumed offset ordering
803 * wrt commit count is insured by the use of cmpxchg to update
804 * the consumed offset.
807 * Local rmb to match the remote wmb to read the commit count
808 * before the buffer data and the write offset.
812 write_offset
= v_read(config
, &buf
->offset
);
815 * Check that the buffer we are getting is after or at consumed_cur
818 if ((long) subbuf_trunc(consumed
, chan
)
819 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
823 * Check that the subbuffer we are trying to consume has been
824 * already fully committed.
826 if (((commit_count
- chan
->backend
.subbuf_size
)
827 & chan
->commit_count_mask
)
828 - (buf_trunc(consumed_cur
, chan
)
829 >> chan
->backend
.num_subbuf_order
)
834 * Check that we are not about to read the same subbuffer in
835 * which the writer head is.
837 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
842 * Failure to get the subbuffer causes a busy-loop retry without going
843 * to a wait queue. These are caused by short-lived race windows where
844 * the writer is getting access to a subbuffer we were trying to get
845 * access to. Also checks that the "consumed" buffer count we are
846 * looking for matches the one contained in the subbuffer id.
848 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
849 consumed_idx
, buf_trunc_val(consumed
, chan
),
853 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
855 buf
->get_subbuf_consumed
= consumed
;
862 * The memory barriers __wait_event()/wake_up_interruptible() take care
863 * of "raw_spin_is_locked" memory ordering.
872 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
875 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
876 struct lttng_ust_shm_handle
*handle
)
878 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
879 struct channel
*chan
= shmp(handle
, bufb
->chan
);
880 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
881 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
883 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1
884 && uatomic_read(&buf
->active_shadow_readers
) != 1);
886 if (!buf
->get_subbuf
) {
888 * Reader puts a subbuffer it did not get.
890 CHAN_WARN_ON(chan
, 1);
893 consumed
= buf
->get_subbuf_consumed
;
897 * Clear the records_unread counter. (overruns counter)
898 * Can still be non-zero if a file reader simply grabbed the data
899 * without using iterators.
900 * Can be below zero if an iterator is used on a snapshot more than
903 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
904 v_add(config
, v_read(config
,
905 &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
),
906 &bufb
->records_read
);
907 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
, 0);
908 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
909 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
910 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
913 * Exchange the reader subbuffer with the one we put in its place in the
914 * writer subbuffer table. Expect the original consumed count. If
915 * update_read_sb_index fails, this is because the writer updated the
916 * subbuffer concurrently. We should therefore keep the subbuffer we
917 * currently have: it has become invalid to try reading this sub-buffer
918 * consumed count value anyway.
920 consumed_idx
= subbuf_index(consumed
, chan
);
921 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
922 consumed_idx
, buf_trunc_val(consumed
, chan
),
925 * update_read_sb_index return value ignored. Don't exchange sub-buffer
926 * if the writer concurrently updated it.
931 * cons_offset is an iterator on all subbuffer offsets between the reader
932 * position and the writer position. (inclusive)
935 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
936 struct channel
*chan
,
937 unsigned long cons_offset
,
939 struct lttng_ust_shm_handle
*handle
)
941 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
942 unsigned long cons_idx
, commit_count
, commit_count_sb
;
944 cons_idx
= subbuf_index(cons_offset
, chan
);
945 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, cons_idx
)->cc
);
946 commit_count_sb
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, cons_idx
)->cc_sb
);
948 if (subbuf_offset(commit_count
, chan
) != 0)
949 DBG("ring buffer %s, cpu %d: "
950 "commit count in subbuffer %lu,\n"
951 "expecting multiples of %lu bytes\n"
952 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
953 chan
->backend
.name
, cpu
, cons_idx
,
954 chan
->backend
.subbuf_size
,
955 commit_count
, commit_count_sb
);
957 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
958 chan
->backend
.name
, cpu
, commit_count
);
962 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
963 struct channel
*chan
,
965 struct lttng_ust_shm_handle
*handle
)
967 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
968 unsigned long write_offset
, cons_offset
;
971 * No need to order commit_count, write_offset and cons_offset reads
972 * because we execute at teardown when no more writer nor reader
973 * references are left.
975 write_offset
= v_read(config
, &buf
->offset
);
976 cons_offset
= uatomic_read(&buf
->consumed
);
977 if (write_offset
!= cons_offset
)
978 DBG("ring buffer %s, cpu %d: "
979 "non-consumed data\n"
980 " [ %lu bytes written, %lu bytes read ]\n",
981 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
983 for (cons_offset
= uatomic_read(&buf
->consumed
);
984 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
987 cons_offset
= subbuf_align(cons_offset
, chan
))
988 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
993 void lib_ring_buffer_print_errors(struct channel
*chan
,
994 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
995 struct lttng_ust_shm_handle
*handle
)
997 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
998 void *priv
= channel_get_private(chan
);
1000 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1001 DBG("ring buffer %s: %lu records written, "
1002 "%lu records overrun\n",
1004 v_read(config
, &buf
->records_count
),
1005 v_read(config
, &buf
->records_overrun
));
1007 DBG("ring buffer %s, cpu %d: %lu records written, "
1008 "%lu records overrun\n",
1009 chan
->backend
.name
, cpu
,
1010 v_read(config
, &buf
->records_count
),
1011 v_read(config
, &buf
->records_overrun
));
1013 if (v_read(config
, &buf
->records_lost_full
)
1014 || v_read(config
, &buf
->records_lost_wrap
)
1015 || v_read(config
, &buf
->records_lost_big
))
1016 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1017 " [ %lu buffer full, %lu nest buffer wrap-around, "
1018 "%lu event too big ]\n",
1019 chan
->backend
.name
, cpu
,
1020 v_read(config
, &buf
->records_lost_full
),
1021 v_read(config
, &buf
->records_lost_wrap
),
1022 v_read(config
, &buf
->records_lost_big
));
1024 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1028 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1030 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1033 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1034 struct channel
*chan
,
1035 struct switch_offsets
*offsets
,
1037 struct lttng_ust_shm_handle
*handle
)
1039 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1040 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1041 unsigned long commit_count
;
1043 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1046 * Order all writes to buffer before the commit count update that will
1047 * determine that the subbuffer is full.
1050 v_add(config
, config
->cb
.subbuffer_header_size(),
1051 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1052 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1053 /* Check if the written buffer has to be delivered */
1054 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1055 commit_count
, oldidx
, handle
);
1056 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1057 offsets
->old
, commit_count
,
1058 config
->cb
.subbuffer_header_size(),
1063 * lib_ring_buffer_switch_old_end: switch old subbuffer
1065 * Note : offset_old should never be 0 here. It is ok, because we never perform
1066 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1067 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1071 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1072 struct channel
*chan
,
1073 struct switch_offsets
*offsets
,
1075 struct lttng_ust_shm_handle
*handle
)
1077 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1078 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1079 unsigned long commit_count
, padding_size
, data_size
;
1081 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1082 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1083 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1087 * Order all writes to buffer before the commit count update that will
1088 * determine that the subbuffer is full.
1091 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1092 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1093 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1094 commit_count
, oldidx
, handle
);
1095 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1096 offsets
->old
, commit_count
,
1097 padding_size
, handle
);
1101 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1103 * This code can be executed unordered : writers may already have written to the
1104 * sub-buffer before this code gets executed, caution. The commit makes sure
1105 * that this code is executed before the deliver of this sub-buffer.
1108 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1109 struct channel
*chan
,
1110 struct switch_offsets
*offsets
,
1112 struct lttng_ust_shm_handle
*handle
)
1114 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1115 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1116 unsigned long commit_count
;
1118 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1121 * Order all writes to buffer before the commit count update that will
1122 * determine that the subbuffer is full.
1125 v_add(config
, config
->cb
.subbuffer_header_size(),
1126 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1127 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1128 /* Check if the written buffer has to be delivered */
1129 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1130 commit_count
, beginidx
, handle
);
1131 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1132 offsets
->begin
, commit_count
,
1133 config
->cb
.subbuffer_header_size(),
1138 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1140 * The only remaining threads could be the ones with pending commits. They will
1141 * have to do the deliver themselves.
1144 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1145 struct channel
*chan
,
1146 struct switch_offsets
*offsets
,
1148 struct lttng_ust_shm_handle
*handle
)
1150 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1151 unsigned long endidx
= subbuf_index(offsets
->end
- 1, chan
);
1152 unsigned long commit_count
, padding_size
, data_size
;
1154 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1155 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1156 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1160 * Order all writes to buffer before the commit count update that will
1161 * determine that the subbuffer is full.
1164 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1165 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1166 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->end
- 1,
1167 commit_count
, endidx
, handle
);
1168 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
1169 offsets
->end
, commit_count
,
1170 padding_size
, handle
);
1176 * !0 if execution must be aborted.
1179 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1180 struct lttng_ust_lib_ring_buffer
*buf
,
1181 struct channel
*chan
,
1182 struct switch_offsets
*offsets
,
1185 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1188 offsets
->begin
= v_read(config
, &buf
->offset
);
1189 offsets
->old
= offsets
->begin
;
1190 offsets
->switch_old_start
= 0;
1191 off
= subbuf_offset(offsets
->begin
, chan
);
1193 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1196 * Ensure we flush the header of an empty subbuffer when doing the
1197 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1198 * total data gathering duration even if there were no records saved
1199 * after the last buffer switch.
1200 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1201 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1202 * subbuffer header as appropriate.
1203 * The next record that reserves space will be responsible for
1204 * populating the following subbuffer header. We choose not to populate
1205 * the next subbuffer header here because we want to be able to use
1206 * SWITCH_ACTIVE for periodical buffer flush, which must
1207 * guarantee that all the buffer content (records and header
1208 * timestamps) are visible to the reader. This is required for
1209 * quiescence guarantees for the fusion merge.
1211 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1212 if (caa_unlikely(off
== 0)) {
1214 * The client does not save any header information.
1215 * Don't switch empty subbuffer on finalize, because it
1216 * is invalid to deliver a completely empty subbuffer.
1218 if (!config
->cb
.subbuffer_header_size())
1221 * Need to write the subbuffer start header on finalize.
1223 offsets
->switch_old_start
= 1;
1225 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1227 return -1; /* we do not have to switch : buffer is empty */
1228 /* Note: old points to the next subbuf at offset 0 */
1229 offsets
->end
= offsets
->begin
;
1234 * Force a sub-buffer switch. This operation is completely reentrant : can be
1235 * called while tracing is active with absolutely no lock held.
1237 * Note, however, that as a v_cmpxchg is used for some atomic
1238 * operations, this function must be called from the CPU which owns the buffer
1239 * for a ACTIVE flush.
1241 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1242 struct lttng_ust_shm_handle
*handle
)
1244 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1245 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1246 struct switch_offsets offsets
;
1247 unsigned long oldidx
;
1253 * Perform retryable operations.
1256 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1258 return; /* Switch not needed */
1259 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1263 * Atomically update last_tsc. This update races against concurrent
1264 * atomic updates, but the race will always cause supplementary full TSC
1265 * records, never the opposite (missing a full TSC record when it would
1268 save_last_tsc(config
, buf
, tsc
);
1271 * Push the reader if necessary
1273 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1275 oldidx
= subbuf_index(offsets
.old
, chan
);
1276 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1279 * May need to populate header start on SWITCH_FLUSH.
1281 if (offsets
.switch_old_start
) {
1282 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1283 offsets
.old
+= config
->cb
.subbuffer_header_size();
1287 * Switch old subbuffer.
1289 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1295 * -ENOSPC if event size is too large for packet.
1296 * -ENOBUFS if there is currently not enough space in buffer for the event.
1297 * -EIO if data cannot be written into the buffer for any other reason.
1300 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
1301 struct channel
*chan
,
1302 struct switch_offsets
*offsets
,
1303 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1305 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1306 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1307 unsigned long reserve_commit_diff
;
1309 offsets
->begin
= v_read(config
, &buf
->offset
);
1310 offsets
->old
= offsets
->begin
;
1311 offsets
->switch_new_start
= 0;
1312 offsets
->switch_new_end
= 0;
1313 offsets
->switch_old_end
= 0;
1314 offsets
->pre_header_padding
= 0;
1316 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1317 if ((int64_t) ctx
->tsc
== -EIO
)
1320 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1321 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1323 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1324 offsets
->switch_new_start
= 1; /* For offsets->begin */
1326 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1328 &offsets
->pre_header_padding
,
1331 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1334 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
1335 offsets
->size
> chan
->backend
.subbuf_size
)) {
1336 offsets
->switch_old_end
= 1; /* For offsets->old */
1337 offsets
->switch_new_start
= 1; /* For offsets->begin */
1340 if (caa_unlikely(offsets
->switch_new_start
)) {
1341 unsigned long sb_index
;
1344 * We are typically not filling the previous buffer completely.
1346 if (caa_likely(offsets
->switch_old_end
))
1347 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1348 offsets
->begin
= offsets
->begin
1349 + config
->cb
.subbuffer_header_size();
1350 /* Test new buffer integrity */
1351 sb_index
= subbuf_index(offsets
->begin
, chan
);
1352 reserve_commit_diff
=
1353 (buf_trunc(offsets
->begin
, chan
)
1354 >> chan
->backend
.num_subbuf_order
)
1355 - ((unsigned long) v_read(config
,
1356 &shmp_index(handle
, buf
->commit_cold
, sb_index
)->cc_sb
)
1357 & chan
->commit_count_mask
);
1358 if (caa_likely(reserve_commit_diff
== 0)) {
1359 /* Next subbuffer not being written to. */
1360 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1361 subbuf_trunc(offsets
->begin
, chan
)
1362 - subbuf_trunc((unsigned long)
1363 uatomic_read(&buf
->consumed
), chan
)
1364 >= chan
->backend
.buf_size
)) {
1365 unsigned long nr_lost
;
1368 * We do not overwrite non consumed buffers
1369 * and we are full : record is lost.
1371 nr_lost
= v_read(config
, &buf
->records_lost_full
);
1372 v_inc(config
, &buf
->records_lost_full
);
1373 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1374 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
1375 nr_lost
+ 1, chan
->backend
.name
,
1381 * Next subbuffer not being written to, and we
1382 * are either in overwrite mode or the buffer is
1383 * not full. It's safe to write in this new
1388 unsigned long nr_lost
;
1391 * Next subbuffer reserve offset does not match the
1392 * commit offset. Drop record in producer-consumer and
1393 * overwrite mode. Caused by either a writer OOPS or too
1394 * many nested writes over a reserve/commit pair.
1396 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
1397 v_inc(config
, &buf
->records_lost_wrap
);
1398 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1399 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
1400 nr_lost
+ 1, chan
->backend
.name
,
1406 config
->cb
.record_header_size(config
, chan
,
1408 &offsets
->pre_header_padding
,
1411 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1414 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
1415 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1416 unsigned long nr_lost
;
1419 * Record too big for subbuffers, report error, don't
1420 * complete the sub-buffer switch.
1422 nr_lost
= v_read(config
, &buf
->records_lost_big
);
1423 v_inc(config
, &buf
->records_lost_big
);
1424 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1425 DBG("%lu or more records lost in (%s:%d) record size "
1426 " of %zu bytes is too large for buffer\n",
1427 nr_lost
+ 1, chan
->backend
.name
,
1428 buf
->backend
.cpu
, offsets
->size
);
1433 * We just made a successful buffer switch and the
1434 * record fits in the new subbuffer. Let's write.
1439 * Record fits in the current buffer and we are not on a switch
1440 * boundary. It's safe to write.
1443 offsets
->end
= offsets
->begin
+ offsets
->size
;
1445 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1447 * The offset_end will fall at the very beginning of the next
1450 offsets
->switch_new_end
= 1; /* For offsets->begin */
1456 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1457 * @ctx: ring buffer context.
1459 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1460 * -EIO for other errors, else returns 0.
1461 * It will take care of sub-buffer switching.
1463 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1465 struct channel
*chan
= ctx
->chan
;
1466 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1467 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1468 struct lttng_ust_lib_ring_buffer
*buf
;
1469 struct switch_offsets offsets
;
1472 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1473 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1475 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
1481 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1483 if (caa_unlikely(ret
))
1485 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1490 * Atomically update last_tsc. This update races against concurrent
1491 * atomic updates, but the race will always cause supplementary full TSC
1492 * records, never the opposite (missing a full TSC record when it would
1495 save_last_tsc(config
, buf
, ctx
->tsc
);
1498 * Push the reader if necessary
1500 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1503 * Clear noref flag for this subbuffer.
1505 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1506 subbuf_index(offsets
.end
- 1, chan
),
1510 * Switch old subbuffer if needed.
1512 if (caa_unlikely(offsets
.switch_old_end
)) {
1513 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1514 subbuf_index(offsets
.old
- 1, chan
),
1516 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1520 * Populate new subbuffer.
1522 if (caa_unlikely(offsets
.switch_new_start
))
1523 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1525 if (caa_unlikely(offsets
.switch_new_end
))
1526 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1528 ctx
->slot_size
= offsets
.size
;
1529 ctx
->pre_offset
= offsets
.begin
;
1530 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1535 * Force a read (imply TLS fixup for dlopen) of TLS variables.
1537 void lttng_fixup_ringbuffer_tls(void)
1539 asm volatile ("" : : "m" (lib_ring_buffer_nesting
));