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
, int **wait_fd
, uint64_t **memory_map_size
)
445 size_t shmsize
, chansize
;
446 struct channel
*chan
;
447 struct lttng_ust_shm_handle
*handle
;
448 struct shm_object
*shmobj
;
451 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
452 read_timer_interval
))
455 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
459 /* Allocate table for channel + per-cpu buffers */
460 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
462 goto error_table_alloc
;
464 /* Calculate the shm allocation layout */
465 shmsize
= sizeof(struct channel
);
466 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
467 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
468 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * num_possible_cpus();
470 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
);
472 shmsize
+= offset_align(shmsize
, priv_data_align
);
473 shmsize
+= priv_data_size
;
475 shmobj
= shm_object_table_append(handle
->table
, shmsize
);
478 /* struct channel is at object 0, offset 0 (hardcoded) */
479 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
480 assert(handle
->chan
._ref
.index
== 0);
481 assert(handle
->chan
._ref
.offset
== 0);
482 chan
= shmp(handle
, handle
->chan
);
486 /* space for private data */
487 if (priv_data_size
) {
488 DECLARE_SHMP(void, priv_data_alloc
);
490 align_shm(shmobj
, priv_data_align
);
491 chan
->priv_data_offset
= shmobj
->allocated_len
;
492 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
493 if (!shmp(handle
, priv_data_alloc
))
495 *priv_data
= channel_get_private(chan
);
496 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
498 chan
->priv_data_offset
= -1;
502 ret
= channel_backend_init(&chan
->backend
, name
, config
,
503 subbuf_size
, num_subbuf
, handle
);
505 goto error_backend_init
;
507 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
509 //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval);
510 //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval);
512 //init_waitqueue_head(&chan->read_wait);
513 //init_waitqueue_head(&chan->hp_wait);
515 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
517 * In case of non-hotplug cpu, if the ring-buffer is allocated
518 * in early initcall, it will not be notified of secondary cpus.
519 * In that off case, we need to allocate for all possible cpus.
521 for_each_possible_cpu(cpu
) {
522 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
523 lib_ring_buffer_start_switch_timer(buf
, handle
);
524 lib_ring_buffer_start_read_timer(buf
, handle
);
527 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
529 lib_ring_buffer_start_switch_timer(buf
, handle
);
530 lib_ring_buffer_start_read_timer(buf
, handle
);
532 ref
= &handle
->chan
._ref
;
533 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
, memory_map_size
);
538 shm_object_table_destroy(handle
->table
);
544 struct lttng_ust_shm_handle
*channel_handle_create(int shm_fd
, int wait_fd
,
545 uint64_t memory_map_size
)
547 struct lttng_ust_shm_handle
*handle
;
548 struct shm_object
*object
;
550 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
554 /* Allocate table for channel + per-cpu buffers */
555 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
557 goto error_table_alloc
;
558 /* Add channel object */
559 object
= shm_object_table_append_shadow(handle
->table
,
560 shm_fd
, wait_fd
, memory_map_size
);
562 goto error_table_object
;
563 /* struct channel is at object 0, offset 0 (hardcoded) */
564 handle
->chan
._ref
.index
= 0;
565 handle
->chan
._ref
.offset
= 0;
569 shm_object_table_destroy(handle
->table
);
575 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
576 int shm_fd
, int wait_fd
, uint64_t memory_map_size
)
578 struct shm_object
*object
;
580 /* Add stream object */
581 object
= shm_object_table_append_shadow(handle
->table
,
582 shm_fd
, wait_fd
, memory_map_size
);
589 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
592 channel_free(chan
, handle
, shadow
);
596 * channel_destroy - Finalize, wait for q.s. and destroy channel.
597 * @chan: channel to destroy
600 * Call "destroy" callback, finalize channels, decrement the channel
601 * reference count. Note that when readers have completed data
602 * consumption of finalized channels, get_subbuf() will return -ENODATA.
603 * They should release their handle at that point.
605 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
609 channel_release(chan
, handle
, shadow
);
613 channel_unregister_notifiers(chan
, handle
);
616 * Note: the consumer takes care of finalizing and switching the
621 * sessiond/consumer are keeping a reference on the shm file
622 * descriptor directly. No need to refcount.
624 channel_release(chan
, handle
, shadow
);
628 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
629 const struct lttng_ust_lib_ring_buffer_config
*config
,
630 struct channel
*chan
, int cpu
,
631 struct lttng_ust_shm_handle
*handle
,
632 int **shm_fd
, int **wait_fd
,
633 uint64_t **memory_map_size
)
637 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
638 ref
= &chan
->backend
.buf
[0].shmp
._ref
;
639 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
,
641 return shmp(handle
, chan
->backend
.buf
[0].shmp
);
643 if (cpu
>= num_possible_cpus())
645 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
646 shm_get_object_data(handle
, ref
, shm_fd
, wait_fd
,
648 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
652 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
653 struct lttng_ust_shm_handle
*handle
,
657 if (uatomic_cmpxchg(&buf
->active_shadow_readers
, 0, 1) != 0)
662 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
668 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
669 struct lttng_ust_shm_handle
*handle
,
672 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
675 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_shadow_readers
) != 1);
677 uatomic_dec(&buf
->active_shadow_readers
);
680 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
682 uatomic_dec(&buf
->active_readers
);
686 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
688 * @consumed: consumed count indicating the position where to read
689 * @produced: produced count, indicates position when to stop reading
691 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
692 * data to read at consumed position, or 0 if the get operation succeeds.
695 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
696 unsigned long *consumed
, unsigned long *produced
,
697 struct lttng_ust_shm_handle
*handle
)
699 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
700 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
701 unsigned long consumed_cur
, write_offset
;
704 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
706 * Read finalized before counters.
709 consumed_cur
= uatomic_read(&buf
->consumed
);
711 * No need to issue a memory barrier between consumed count read and
712 * write offset read, because consumed count can only change
713 * concurrently in overwrite mode, and we keep a sequence counter
714 * identifier derived from the write offset to check we are getting
715 * the same sub-buffer we are expecting (the sub-buffers are atomically
716 * "tagged" upon writes, tags are checked upon read).
718 write_offset
= v_read(config
, &buf
->offset
);
721 * Check that we are not about to read the same subbuffer in
722 * which the writer head is.
724 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
728 *consumed
= consumed_cur
;
729 *produced
= subbuf_trunc(write_offset
, chan
);
735 * The memory barriers __wait_event()/wake_up_interruptible() take care
736 * of "raw_spin_is_locked" memory ordering.
745 * lib_ring_buffer_put_snapshot - move consumed counter forward
747 * @consumed_new: new consumed count value
749 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
750 unsigned long consumed_new
,
751 struct lttng_ust_shm_handle
*handle
)
753 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
754 struct channel
*chan
= shmp(handle
, bufb
->chan
);
755 unsigned long consumed
;
757 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1
758 && uatomic_read(&buf
->active_shadow_readers
) != 1);
761 * Only push the consumed value forward.
762 * If the consumed cmpxchg fails, this is because we have been pushed by
763 * the writer in flight recorder mode.
765 consumed
= uatomic_read(&buf
->consumed
);
766 while ((long) consumed
- (long) consumed_new
< 0)
767 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
772 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
774 * @consumed: consumed count indicating the position where to read
776 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
777 * data to read at consumed position, or 0 if the get operation succeeds.
779 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
780 unsigned long consumed
,
781 struct lttng_ust_shm_handle
*handle
)
783 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
784 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
785 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
790 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
792 * Read finalized before counters.
795 consumed_cur
= uatomic_read(&buf
->consumed
);
796 consumed_idx
= subbuf_index(consumed
, chan
);
797 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, consumed_idx
)->cc_sb
);
799 * Make sure we read the commit count before reading the buffer
800 * data and the write offset. Correct consumed offset ordering
801 * wrt commit count is insured by the use of cmpxchg to update
802 * the consumed offset.
805 * Local rmb to match the remote wmb to read the commit count
806 * before the buffer data and the write offset.
810 write_offset
= v_read(config
, &buf
->offset
);
813 * Check that the buffer we are getting is after or at consumed_cur
816 if ((long) subbuf_trunc(consumed
, chan
)
817 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
821 * Check that the subbuffer we are trying to consume has been
822 * already fully committed.
824 if (((commit_count
- chan
->backend
.subbuf_size
)
825 & chan
->commit_count_mask
)
826 - (buf_trunc(consumed_cur
, chan
)
827 >> chan
->backend
.num_subbuf_order
)
832 * Check that we are not about to read the same subbuffer in
833 * which the writer head is.
835 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
840 * Failure to get the subbuffer causes a busy-loop retry without going
841 * to a wait queue. These are caused by short-lived race windows where
842 * the writer is getting access to a subbuffer we were trying to get
843 * access to. Also checks that the "consumed" buffer count we are
844 * looking for matches the one contained in the subbuffer id.
846 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
847 consumed_idx
, buf_trunc_val(consumed
, chan
),
851 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
853 buf
->get_subbuf_consumed
= consumed
;
860 * The memory barriers __wait_event()/wake_up_interruptible() take care
861 * of "raw_spin_is_locked" memory ordering.
870 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
873 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
874 struct lttng_ust_shm_handle
*handle
)
876 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
877 struct channel
*chan
= shmp(handle
, bufb
->chan
);
878 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
879 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
881 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1
882 && uatomic_read(&buf
->active_shadow_readers
) != 1);
884 if (!buf
->get_subbuf
) {
886 * Reader puts a subbuffer it did not get.
888 CHAN_WARN_ON(chan
, 1);
891 consumed
= buf
->get_subbuf_consumed
;
895 * Clear the records_unread counter. (overruns counter)
896 * Can still be non-zero if a file reader simply grabbed the data
897 * without using iterators.
898 * Can be below zero if an iterator is used on a snapshot more than
901 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
902 v_add(config
, v_read(config
,
903 &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
),
904 &bufb
->records_read
);
905 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
, 0);
906 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
907 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
908 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
911 * Exchange the reader subbuffer with the one we put in its place in the
912 * writer subbuffer table. Expect the original consumed count. If
913 * update_read_sb_index fails, this is because the writer updated the
914 * subbuffer concurrently. We should therefore keep the subbuffer we
915 * currently have: it has become invalid to try reading this sub-buffer
916 * consumed count value anyway.
918 consumed_idx
= subbuf_index(consumed
, chan
);
919 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
920 consumed_idx
, buf_trunc_val(consumed
, chan
),
923 * update_read_sb_index return value ignored. Don't exchange sub-buffer
924 * if the writer concurrently updated it.
929 * cons_offset is an iterator on all subbuffer offsets between the reader
930 * position and the writer position. (inclusive)
933 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
934 struct channel
*chan
,
935 unsigned long cons_offset
,
937 struct lttng_ust_shm_handle
*handle
)
939 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
940 unsigned long cons_idx
, commit_count
, commit_count_sb
;
942 cons_idx
= subbuf_index(cons_offset
, chan
);
943 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, cons_idx
)->cc
);
944 commit_count_sb
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, cons_idx
)->cc_sb
);
946 if (subbuf_offset(commit_count
, chan
) != 0)
947 DBG("ring buffer %s, cpu %d: "
948 "commit count in subbuffer %lu,\n"
949 "expecting multiples of %lu bytes\n"
950 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
951 chan
->backend
.name
, cpu
, cons_idx
,
952 chan
->backend
.subbuf_size
,
953 commit_count
, commit_count_sb
);
955 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
956 chan
->backend
.name
, cpu
, commit_count
);
960 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
961 struct channel
*chan
,
963 struct lttng_ust_shm_handle
*handle
)
965 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
966 unsigned long write_offset
, cons_offset
;
969 * No need to order commit_count, write_offset and cons_offset reads
970 * because we execute at teardown when no more writer nor reader
971 * references are left.
973 write_offset
= v_read(config
, &buf
->offset
);
974 cons_offset
= uatomic_read(&buf
->consumed
);
975 if (write_offset
!= cons_offset
)
976 DBG("ring buffer %s, cpu %d: "
977 "non-consumed data\n"
978 " [ %lu bytes written, %lu bytes read ]\n",
979 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
981 for (cons_offset
= uatomic_read(&buf
->consumed
);
982 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
985 cons_offset
= subbuf_align(cons_offset
, chan
))
986 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
991 void lib_ring_buffer_print_errors(struct channel
*chan
,
992 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
993 struct lttng_ust_shm_handle
*handle
)
995 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
996 void *priv
= channel_get_private(chan
);
998 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
999 DBG("ring buffer %s: %lu records written, "
1000 "%lu records overrun\n",
1002 v_read(config
, &buf
->records_count
),
1003 v_read(config
, &buf
->records_overrun
));
1005 DBG("ring buffer %s, cpu %d: %lu records written, "
1006 "%lu records overrun\n",
1007 chan
->backend
.name
, cpu
,
1008 v_read(config
, &buf
->records_count
),
1009 v_read(config
, &buf
->records_overrun
));
1011 if (v_read(config
, &buf
->records_lost_full
)
1012 || v_read(config
, &buf
->records_lost_wrap
)
1013 || v_read(config
, &buf
->records_lost_big
))
1014 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1015 " [ %lu buffer full, %lu nest buffer wrap-around, "
1016 "%lu event too big ]\n",
1017 chan
->backend
.name
, cpu
,
1018 v_read(config
, &buf
->records_lost_full
),
1019 v_read(config
, &buf
->records_lost_wrap
),
1020 v_read(config
, &buf
->records_lost_big
));
1022 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1026 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1028 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1031 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1032 struct channel
*chan
,
1033 struct switch_offsets
*offsets
,
1035 struct lttng_ust_shm_handle
*handle
)
1037 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1038 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1039 unsigned long commit_count
;
1041 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1044 * Order all writes to buffer before the commit count update that will
1045 * determine that the subbuffer is full.
1048 v_add(config
, config
->cb
.subbuffer_header_size(),
1049 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1050 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1051 /* Check if the written buffer has to be delivered */
1052 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1053 commit_count
, oldidx
, handle
);
1054 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1055 offsets
->old
, commit_count
,
1056 config
->cb
.subbuffer_header_size(),
1061 * lib_ring_buffer_switch_old_end: switch old subbuffer
1063 * Note : offset_old should never be 0 here. It is ok, because we never perform
1064 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1065 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1069 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1070 struct channel
*chan
,
1071 struct switch_offsets
*offsets
,
1073 struct lttng_ust_shm_handle
*handle
)
1075 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1076 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1077 unsigned long commit_count
, padding_size
, data_size
;
1079 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1080 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1081 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1085 * Order all writes to buffer before the commit count update that will
1086 * determine that the subbuffer is full.
1089 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1090 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1091 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1092 commit_count
, oldidx
, handle
);
1093 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1094 offsets
->old
, commit_count
,
1095 padding_size
, handle
);
1099 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1101 * This code can be executed unordered : writers may already have written to the
1102 * sub-buffer before this code gets executed, caution. The commit makes sure
1103 * that this code is executed before the deliver of this sub-buffer.
1106 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1107 struct channel
*chan
,
1108 struct switch_offsets
*offsets
,
1110 struct lttng_ust_shm_handle
*handle
)
1112 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1113 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1114 unsigned long commit_count
;
1116 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1119 * Order all writes to buffer before the commit count update that will
1120 * determine that the subbuffer is full.
1123 v_add(config
, config
->cb
.subbuffer_header_size(),
1124 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1125 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1126 /* Check if the written buffer has to be delivered */
1127 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1128 commit_count
, beginidx
, handle
);
1129 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1130 offsets
->begin
, commit_count
,
1131 config
->cb
.subbuffer_header_size(),
1136 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1138 * The only remaining threads could be the ones with pending commits. They will
1139 * have to do the deliver themselves.
1142 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1143 struct channel
*chan
,
1144 struct switch_offsets
*offsets
,
1146 struct lttng_ust_shm_handle
*handle
)
1148 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1149 unsigned long endidx
= subbuf_index(offsets
->end
- 1, chan
);
1150 unsigned long commit_count
, padding_size
, data_size
;
1152 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1153 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1154 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1158 * Order all writes to buffer before the commit count update that will
1159 * determine that the subbuffer is full.
1162 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1163 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1164 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->end
- 1,
1165 commit_count
, endidx
, handle
);
1166 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
1167 offsets
->end
, commit_count
,
1168 padding_size
, handle
);
1174 * !0 if execution must be aborted.
1177 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1178 struct lttng_ust_lib_ring_buffer
*buf
,
1179 struct channel
*chan
,
1180 struct switch_offsets
*offsets
,
1183 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1186 offsets
->begin
= v_read(config
, &buf
->offset
);
1187 offsets
->old
= offsets
->begin
;
1188 offsets
->switch_old_start
= 0;
1189 off
= subbuf_offset(offsets
->begin
, chan
);
1191 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1194 * Ensure we flush the header of an empty subbuffer when doing the
1195 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1196 * total data gathering duration even if there were no records saved
1197 * after the last buffer switch.
1198 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1199 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1200 * subbuffer header as appropriate.
1201 * The next record that reserves space will be responsible for
1202 * populating the following subbuffer header. We choose not to populate
1203 * the next subbuffer header here because we want to be able to use
1204 * SWITCH_ACTIVE for periodical buffer flush, which must
1205 * guarantee that all the buffer content (records and header
1206 * timestamps) are visible to the reader. This is required for
1207 * quiescence guarantees for the fusion merge.
1209 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1210 if (caa_unlikely(off
== 0)) {
1212 * The client does not save any header information.
1213 * Don't switch empty subbuffer on finalize, because it
1214 * is invalid to deliver a completely empty subbuffer.
1216 if (!config
->cb
.subbuffer_header_size())
1219 * Need to write the subbuffer start header on finalize.
1221 offsets
->switch_old_start
= 1;
1223 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1225 return -1; /* we do not have to switch : buffer is empty */
1226 /* Note: old points to the next subbuf at offset 0 */
1227 offsets
->end
= offsets
->begin
;
1232 * Force a sub-buffer switch. This operation is completely reentrant : can be
1233 * called while tracing is active with absolutely no lock held.
1235 * Note, however, that as a v_cmpxchg is used for some atomic
1236 * operations, this function must be called from the CPU which owns the buffer
1237 * for a ACTIVE flush.
1239 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1240 struct lttng_ust_shm_handle
*handle
)
1242 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1243 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1244 struct switch_offsets offsets
;
1245 unsigned long oldidx
;
1251 * Perform retryable operations.
1254 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1256 return; /* Switch not needed */
1257 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1261 * Atomically update last_tsc. This update races against concurrent
1262 * atomic updates, but the race will always cause supplementary full TSC
1263 * records, never the opposite (missing a full TSC record when it would
1266 save_last_tsc(config
, buf
, tsc
);
1269 * Push the reader if necessary
1271 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1273 oldidx
= subbuf_index(offsets
.old
, chan
);
1274 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1277 * May need to populate header start on SWITCH_FLUSH.
1279 if (offsets
.switch_old_start
) {
1280 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1281 offsets
.old
+= config
->cb
.subbuffer_header_size();
1285 * Switch old subbuffer.
1287 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1293 * -ENOSPC if event size is too large for packet.
1294 * -ENOBUFS if there is currently not enough space in buffer for the event.
1295 * -EIO if data cannot be written into the buffer for any other reason.
1298 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
1299 struct channel
*chan
,
1300 struct switch_offsets
*offsets
,
1301 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1303 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1304 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1305 unsigned long reserve_commit_diff
;
1307 offsets
->begin
= v_read(config
, &buf
->offset
);
1308 offsets
->old
= offsets
->begin
;
1309 offsets
->switch_new_start
= 0;
1310 offsets
->switch_new_end
= 0;
1311 offsets
->switch_old_end
= 0;
1312 offsets
->pre_header_padding
= 0;
1314 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1315 if ((int64_t) ctx
->tsc
== -EIO
)
1318 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1319 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1321 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1322 offsets
->switch_new_start
= 1; /* For offsets->begin */
1324 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1326 &offsets
->pre_header_padding
,
1329 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1332 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
1333 offsets
->size
> chan
->backend
.subbuf_size
)) {
1334 offsets
->switch_old_end
= 1; /* For offsets->old */
1335 offsets
->switch_new_start
= 1; /* For offsets->begin */
1338 if (caa_unlikely(offsets
->switch_new_start
)) {
1339 unsigned long sb_index
;
1342 * We are typically not filling the previous buffer completely.
1344 if (caa_likely(offsets
->switch_old_end
))
1345 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1346 offsets
->begin
= offsets
->begin
1347 + config
->cb
.subbuffer_header_size();
1348 /* Test new buffer integrity */
1349 sb_index
= subbuf_index(offsets
->begin
, chan
);
1350 reserve_commit_diff
=
1351 (buf_trunc(offsets
->begin
, chan
)
1352 >> chan
->backend
.num_subbuf_order
)
1353 - ((unsigned long) v_read(config
,
1354 &shmp_index(handle
, buf
->commit_cold
, sb_index
)->cc_sb
)
1355 & chan
->commit_count_mask
);
1356 if (caa_likely(reserve_commit_diff
== 0)) {
1357 /* Next subbuffer not being written to. */
1358 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1359 subbuf_trunc(offsets
->begin
, chan
)
1360 - subbuf_trunc((unsigned long)
1361 uatomic_read(&buf
->consumed
), chan
)
1362 >= chan
->backend
.buf_size
)) {
1363 unsigned long nr_lost
;
1366 * We do not overwrite non consumed buffers
1367 * and we are full : record is lost.
1369 nr_lost
= v_read(config
, &buf
->records_lost_full
);
1370 v_inc(config
, &buf
->records_lost_full
);
1371 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1372 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
1373 nr_lost
+ 1, chan
->backend
.name
,
1379 * Next subbuffer not being written to, and we
1380 * are either in overwrite mode or the buffer is
1381 * not full. It's safe to write in this new
1386 unsigned long nr_lost
;
1389 * Next subbuffer reserve offset does not match the
1390 * commit offset. Drop record in producer-consumer and
1391 * overwrite mode. Caused by either a writer OOPS or too
1392 * many nested writes over a reserve/commit pair.
1394 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
1395 v_inc(config
, &buf
->records_lost_wrap
);
1396 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1397 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
1398 nr_lost
+ 1, chan
->backend
.name
,
1404 config
->cb
.record_header_size(config
, chan
,
1406 &offsets
->pre_header_padding
,
1409 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1412 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
1413 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1414 unsigned long nr_lost
;
1417 * Record too big for subbuffers, report error, don't
1418 * complete the sub-buffer switch.
1420 nr_lost
= v_read(config
, &buf
->records_lost_big
);
1421 v_inc(config
, &buf
->records_lost_big
);
1422 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1423 DBG("%lu or more records lost in (%s:%d) record size "
1424 " of %zu bytes is too large for buffer\n",
1425 nr_lost
+ 1, chan
->backend
.name
,
1426 buf
->backend
.cpu
, offsets
->size
);
1431 * We just made a successful buffer switch and the
1432 * record fits in the new subbuffer. Let's write.
1437 * Record fits in the current buffer and we are not on a switch
1438 * boundary. It's safe to write.
1441 offsets
->end
= offsets
->begin
+ offsets
->size
;
1443 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1445 * The offset_end will fall at the very beginning of the next
1448 offsets
->switch_new_end
= 1; /* For offsets->begin */
1454 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1455 * @ctx: ring buffer context.
1457 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1458 * -EIO for other errors, else returns 0.
1459 * It will take care of sub-buffer switching.
1461 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1463 struct channel
*chan
= ctx
->chan
;
1464 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1465 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1466 struct lttng_ust_lib_ring_buffer
*buf
;
1467 struct switch_offsets offsets
;
1470 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1471 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1473 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
1479 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1481 if (caa_unlikely(ret
))
1483 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1488 * Atomically update last_tsc. This update races against concurrent
1489 * atomic updates, but the race will always cause supplementary full TSC
1490 * records, never the opposite (missing a full TSC record when it would
1493 save_last_tsc(config
, buf
, ctx
->tsc
);
1496 * Push the reader if necessary
1498 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1501 * Clear noref flag for this subbuffer.
1503 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1504 subbuf_index(offsets
.end
- 1, chan
),
1508 * Switch old subbuffer if needed.
1510 if (caa_unlikely(offsets
.switch_old_end
)) {
1511 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1512 subbuf_index(offsets
.old
- 1, chan
),
1514 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1518 * Populate new subbuffer.
1520 if (caa_unlikely(offsets
.switch_new_start
))
1521 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1523 if (caa_unlikely(offsets
.switch_new_end
))
1524 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1526 ctx
->slot_size
= offsets
.size
;
1527 ctx
->pre_offset
= offsets
.begin
;
1528 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1533 * Force a read (imply TLS fixup for dlopen) of TLS variables.
1535 void lttng_fixup_ringbuffer_tls(void)
1537 asm volatile ("" : : "m" (lib_ring_buffer_nesting
));