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>
61 #include <urcu/tls-compat.h>
65 #include <lttng/ringbuffer-config.h>
71 #include "../liblttng-ust/compat.h" /* For ENODATA */
74 #define max(a, b) ((a) > (b) ? (a) : (b))
77 /* Print DBG() messages about events lost only every 1048576 hits */
78 #define DBG_PRINT_NR_LOST (1UL << 20)
81 * Use POSIX SHM: shm_open(3) and shm_unlink(3).
82 * close(2) to close the fd returned by shm_open.
83 * shm_unlink releases the shared memory object name.
84 * ftruncate(2) sets the size of the memory object.
85 * mmap/munmap maps the shared memory obj to a virtual address in the
86 * calling proceess (should be done both in libust and consumer).
87 * See shm_overview(7) for details.
88 * Pass file descriptor returned by shm_open(3) to ltt-sessiond through
91 * Since we don't need to access the object using its name, we can
92 * immediately shm_unlink(3) it, and only keep the handle with its file
97 * Internal structure representing offsets to use at a sub-buffer switch.
99 struct switch_offsets
{
100 unsigned long begin
, end
, old
;
101 size_t pre_header_padding
, size
;
102 unsigned int switch_new_start
:1, switch_new_end
:1, switch_old_start
:1,
106 DEFINE_URCU_TLS(unsigned int, lib_ring_buffer_nesting
);
109 void lib_ring_buffer_print_errors(struct channel
*chan
,
110 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
111 struct lttng_ust_shm_handle
*handle
);
114 * lib_ring_buffer_reset - Reset ring buffer to initial values.
117 * Effectively empty the ring buffer. Should be called when the buffer is not
118 * used for writing. The ring buffer can be opened for reading, but the reader
119 * should not be using the iterator concurrently with reset. The previous
120 * current iterator record is reset.
122 void lib_ring_buffer_reset(struct lttng_ust_lib_ring_buffer
*buf
,
123 struct lttng_ust_shm_handle
*handle
)
125 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
126 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
130 * Reset iterator first. It will put the subbuffer if it currently holds
133 v_set(config
, &buf
->offset
, 0);
134 for (i
= 0; i
< chan
->backend
.num_subbuf
; i
++) {
135 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->cc
, 0);
136 v_set(config
, &shmp_index(handle
, buf
->commit_hot
, i
)->seq
, 0);
137 v_set(config
, &shmp_index(handle
, buf
->commit_cold
, i
)->cc_sb
, 0);
139 uatomic_set(&buf
->consumed
, 0);
140 uatomic_set(&buf
->record_disabled
, 0);
141 v_set(config
, &buf
->last_tsc
, 0);
142 lib_ring_buffer_backend_reset(&buf
->backend
, handle
);
143 /* Don't reset number of active readers */
144 v_set(config
, &buf
->records_lost_full
, 0);
145 v_set(config
, &buf
->records_lost_wrap
, 0);
146 v_set(config
, &buf
->records_lost_big
, 0);
147 v_set(config
, &buf
->records_count
, 0);
148 v_set(config
, &buf
->records_overrun
, 0);
153 * channel_reset - Reset channel to initial values.
156 * Effectively empty the channel. Should be called when the channel is not used
157 * for writing. The channel can be opened for reading, but the reader should not
158 * be using the iterator concurrently with reset. The previous current iterator
161 void channel_reset(struct channel
*chan
)
164 * Reset iterators first. Will put the subbuffer if held for reading.
166 uatomic_set(&chan
->record_disabled
, 0);
167 /* Don't reset commit_count_mask, still valid */
168 channel_backend_reset(&chan
->backend
);
169 /* Don't reset switch/read timer interval */
170 /* Don't reset notifiers and notifier enable bits */
171 /* Don't reset reader reference count */
175 * Must be called under cpu hotplug protection.
177 int lib_ring_buffer_create(struct lttng_ust_lib_ring_buffer
*buf
,
178 struct channel_backend
*chanb
, int cpu
,
179 struct lttng_ust_shm_handle
*handle
,
180 struct shm_object
*shmobj
)
182 const struct lttng_ust_lib_ring_buffer_config
*config
= &chanb
->config
;
183 struct channel
*chan
= caa_container_of(chanb
, struct channel
, backend
);
184 void *priv
= channel_get_private(chan
);
185 size_t subbuf_header_size
;
189 /* Test for cpu hotplug */
190 if (buf
->backend
.allocated
)
193 ret
= lib_ring_buffer_backend_create(&buf
->backend
, &chan
->backend
,
194 cpu
, handle
, shmobj
);
198 align_shm(shmobj
, __alignof__(struct commit_counters_hot
));
199 set_shmp(buf
->commit_hot
,
201 sizeof(struct commit_counters_hot
) * chan
->backend
.num_subbuf
));
202 if (!shmp(handle
, buf
->commit_hot
)) {
207 align_shm(shmobj
, __alignof__(struct commit_counters_cold
));
208 set_shmp(buf
->commit_cold
,
210 sizeof(struct commit_counters_cold
) * chan
->backend
.num_subbuf
));
211 if (!shmp(handle
, buf
->commit_cold
)) {
217 * Write the subbuffer header for first subbuffer so we know the total
218 * duration of data gathering.
220 subbuf_header_size
= config
->cb
.subbuffer_header_size();
221 v_set(config
, &buf
->offset
, subbuf_header_size
);
222 subbuffer_id_clear_noref(config
, &shmp_index(handle
, buf
->backend
.buf_wsb
, 0)->id
);
223 tsc
= config
->cb
.ring_buffer_clock_read(shmp(handle
, buf
->backend
.chan
));
224 config
->cb
.buffer_begin(buf
, tsc
, 0, handle
);
225 v_add(config
, subbuf_header_size
, &shmp_index(handle
, buf
->commit_hot
, 0)->cc
);
227 if (config
->cb
.buffer_create
) {
228 ret
= config
->cb
.buffer_create(buf
, priv
, cpu
, chanb
->name
, handle
);
232 buf
->backend
.allocated
= 1;
237 /* commit_cold will be freed by shm teardown */
239 /* commit_hot will be freed by shm teardown */
245 static void switch_buffer_timer(unsigned long data
)
247 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
248 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
249 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
252 * Only flush buffers periodically if readers are active.
254 if (uatomic_read(&buf
->active_readers
))
255 lib_ring_buffer_switch_slow(buf
, SWITCH_ACTIVE
, handle
);
258 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
259 // mod_timer_pinned(&buf->switch_timer,
260 // jiffies + chan->switch_timer_interval);
262 // mod_timer(&buf->switch_timer,
263 // jiffies + chan->switch_timer_interval);
267 static void lib_ring_buffer_start_switch_timer(struct lttng_ust_lib_ring_buffer
*buf
,
268 struct lttng_ust_shm_handle
*handle
)
270 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
271 //const struct lttng_ust_lib_ring_buffer_config *config = &chan->backend.config;
273 if (!chan
->switch_timer_interval
|| buf
->switch_timer_enabled
)
276 //init_timer(&buf->switch_timer);
277 //buf->switch_timer.function = switch_buffer_timer;
278 //buf->switch_timer.expires = jiffies + chan->switch_timer_interval;
279 //buf->switch_timer.data = (unsigned long)buf;
280 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
281 // add_timer_on(&buf->switch_timer, buf->backend.cpu);
283 // add_timer(&buf->switch_timer);
284 buf
->switch_timer_enabled
= 1;
287 static void lib_ring_buffer_stop_switch_timer(struct lttng_ust_lib_ring_buffer
*buf
,
288 struct lttng_ust_shm_handle
*handle
)
290 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
292 if (!chan
->switch_timer_interval
|| !buf
->switch_timer_enabled
)
296 //del_timer_sync(&buf->switch_timer);
297 buf
->switch_timer_enabled
= 0;
302 * Polling timer to check the channels for data.
304 static void read_buffer_timer(unsigned long data
)
306 struct lttng_ust_lib_ring_buffer
*buf
= (struct lttng_ust_lib_ring_buffer
*)data
;
307 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
308 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
310 CHAN_WARN_ON(chan
, !buf
->backend
.allocated
);
312 if (uatomic_read(&buf
->active_readers
))
313 && lib_ring_buffer_poll_deliver(config
, buf
, chan
)) {
315 //wake_up_interruptible(&buf->read_wait);
316 //wake_up_interruptible(&chan->read_wait);
320 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
321 // mod_timer_pinned(&buf->read_timer,
322 // jiffies + chan->read_timer_interval);
324 // mod_timer(&buf->read_timer,
325 // jiffies + chan->read_timer_interval);
329 static void lib_ring_buffer_start_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
330 struct lttng_ust_shm_handle
*handle
)
332 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
333 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
335 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
336 || !chan
->read_timer_interval
337 || buf
->read_timer_enabled
)
341 //init_timer(&buf->read_timer);
342 //buf->read_timer.function = read_buffer_timer;
343 //buf->read_timer.expires = jiffies + chan->read_timer_interval;
344 //buf->read_timer.data = (unsigned long)buf;
346 //if (config->alloc == RING_BUFFER_ALLOC_PER_CPU)
347 // add_timer_on(&buf->read_timer, buf->backend.cpu);
349 // add_timer(&buf->read_timer);
350 buf
->read_timer_enabled
= 1;
353 static void lib_ring_buffer_stop_read_timer(struct lttng_ust_lib_ring_buffer
*buf
,
354 struct lttng_ust_shm_handle
*handle
)
356 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
357 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
359 if (config
->wakeup
!= RING_BUFFER_WAKEUP_BY_TIMER
360 || !chan
->read_timer_interval
361 || !buf
->read_timer_enabled
)
365 //del_timer_sync(&buf->read_timer);
367 * do one more check to catch data that has been written in the last
370 if (lib_ring_buffer_poll_deliver(config
, buf
, chan
, handle
)) {
372 //wake_up_interruptible(&buf->read_wait);
373 //wake_up_interruptible(&chan->read_wait);
375 buf
->read_timer_enabled
= 0;
378 static void channel_unregister_notifiers(struct channel
*chan
,
379 struct lttng_ust_shm_handle
*handle
)
381 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
384 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
385 for_each_possible_cpu(cpu
) {
386 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
388 lib_ring_buffer_stop_switch_timer(buf
, handle
);
389 lib_ring_buffer_stop_read_timer(buf
, handle
);
392 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
394 lib_ring_buffer_stop_switch_timer(buf
, handle
);
395 lib_ring_buffer_stop_read_timer(buf
, handle
);
397 //channel_backend_unregister_notifiers(&chan->backend);
400 static void channel_print_errors(struct channel
*chan
,
401 struct lttng_ust_shm_handle
*handle
)
403 const struct lttng_ust_lib_ring_buffer_config
*config
=
404 &chan
->backend
.config
;
407 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
408 for_each_possible_cpu(cpu
) {
409 struct lttng_ust_lib_ring_buffer
*buf
=
410 shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
411 lib_ring_buffer_print_errors(chan
, buf
, cpu
, handle
);
414 struct lttng_ust_lib_ring_buffer
*buf
=
415 shmp(handle
, chan
->backend
.buf
[0].shmp
);
417 lib_ring_buffer_print_errors(chan
, buf
, -1, handle
);
421 static void channel_free(struct channel
*chan
,
422 struct lttng_ust_shm_handle
*handle
)
424 channel_print_errors(chan
, handle
);
425 channel_backend_free(&chan
->backend
, handle
);
426 /* chan is freed by shm teardown */
427 shm_object_table_destroy(handle
->table
);
432 * channel_create - Create channel.
433 * @config: ring buffer instance configuration
434 * @name: name of the channel
435 * @priv_data: ring buffer client private data area pointer (output)
436 * @priv_data_size: length, in bytes, of the private data area.
437 * @priv_data_init: initialization data for private data.
438 * @buf_addr: pointer the the beginning of the preallocated buffer contiguous
439 * address mapping. It is used only by RING_BUFFER_STATIC
440 * configuration. It can be set to NULL for other backends.
441 * @subbuf_size: subbuffer size
442 * @num_subbuf: number of subbuffers
443 * @switch_timer_interval: Time interval (in us) to fill sub-buffers with
444 * padding to let readers get those sub-buffers.
445 * Used for live streaming.
446 * @read_timer_interval: Time interval (in us) to wake up pending readers.
449 * Returns NULL on failure.
451 struct lttng_ust_shm_handle
*channel_create(const struct lttng_ust_lib_ring_buffer_config
*config
,
454 size_t priv_data_align
,
455 size_t priv_data_size
,
456 void *priv_data_init
,
457 void *buf_addr
, size_t subbuf_size
,
458 size_t num_subbuf
, unsigned int switch_timer_interval
,
459 unsigned int read_timer_interval
)
462 size_t shmsize
, chansize
;
463 struct channel
*chan
;
464 struct lttng_ust_shm_handle
*handle
;
465 struct shm_object
*shmobj
;
466 unsigned int nr_streams
;
468 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
469 nr_streams
= num_possible_cpus();
473 if (lib_ring_buffer_check_config(config
, switch_timer_interval
,
474 read_timer_interval
))
477 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
481 /* Allocate table for channel + per-cpu buffers */
482 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
484 goto error_table_alloc
;
486 /* Calculate the shm allocation layout */
487 shmsize
= sizeof(struct channel
);
488 shmsize
+= offset_align(shmsize
, __alignof__(struct lttng_ust_lib_ring_buffer_shmp
));
489 shmsize
+= sizeof(struct lttng_ust_lib_ring_buffer_shmp
) * nr_streams
;
492 shmsize
+= offset_align(shmsize
, priv_data_align
);
493 shmsize
+= priv_data_size
;
495 /* Allocate normal memory for channel (not shared) */
496 shmobj
= shm_object_table_alloc(handle
->table
, shmsize
, SHM_OBJECT_MEM
);
499 /* struct channel is at object 0, offset 0 (hardcoded) */
500 set_shmp(handle
->chan
, zalloc_shm(shmobj
, chansize
));
501 assert(handle
->chan
._ref
.index
== 0);
502 assert(handle
->chan
._ref
.offset
== 0);
503 chan
= shmp(handle
, handle
->chan
);
506 chan
->nr_streams
= nr_streams
;
508 /* space for private data */
509 if (priv_data_size
) {
510 DECLARE_SHMP(void, priv_data_alloc
);
512 align_shm(shmobj
, priv_data_align
);
513 chan
->priv_data_offset
= shmobj
->allocated_len
;
514 set_shmp(priv_data_alloc
, zalloc_shm(shmobj
, priv_data_size
));
515 if (!shmp(handle
, priv_data_alloc
))
517 *priv_data
= channel_get_private(chan
);
518 memcpy(*priv_data
, priv_data_init
, priv_data_size
);
520 chan
->priv_data_offset
= -1;
525 ret
= channel_backend_init(&chan
->backend
, name
, config
,
526 subbuf_size
, num_subbuf
, handle
);
528 goto error_backend_init
;
530 chan
->commit_count_mask
= (~0UL >> chan
->backend
.num_subbuf_order
);
532 //chan->switch_timer_interval = usecs_to_jiffies(switch_timer_interval);
533 //chan->read_timer_interval = usecs_to_jiffies(read_timer_interval);
535 //init_waitqueue_head(&chan->read_wait);
536 //init_waitqueue_head(&chan->hp_wait);
538 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
) {
540 * In case of non-hotplug cpu, if the ring-buffer is allocated
541 * in early initcall, it will not be notified of secondary cpus.
542 * In that off case, we need to allocate for all possible cpus.
544 for_each_possible_cpu(cpu
) {
545 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
546 lib_ring_buffer_start_switch_timer(buf
, handle
);
547 lib_ring_buffer_start_read_timer(buf
, handle
);
550 struct lttng_ust_lib_ring_buffer
*buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
552 lib_ring_buffer_start_switch_timer(buf
, handle
);
553 lib_ring_buffer_start_read_timer(buf
, handle
);
559 shm_object_table_destroy(handle
->table
);
565 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
566 uint64_t memory_map_size
)
568 struct lttng_ust_shm_handle
*handle
;
569 struct shm_object
*object
;
571 handle
= zmalloc(sizeof(struct lttng_ust_shm_handle
));
575 /* Allocate table for channel + per-cpu buffers */
576 handle
->table
= shm_object_table_create(1 + num_possible_cpus());
578 goto error_table_alloc
;
579 /* Add channel object */
580 object
= shm_object_table_append_mem(handle
->table
, data
,
583 goto error_table_object
;
584 /* struct channel is at object 0, offset 0 (hardcoded) */
585 handle
->chan
._ref
.index
= 0;
586 handle
->chan
._ref
.offset
= 0;
590 shm_object_table_destroy(handle
->table
);
596 int channel_handle_add_stream(struct lttng_ust_shm_handle
*handle
,
597 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
598 uint64_t memory_map_size
)
600 struct shm_object
*object
;
602 /* Add stream object */
603 object
= shm_object_table_append_shm(handle
->table
,
604 shm_fd
, wakeup_fd
, stream_nr
,
611 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
)
613 assert(handle
->table
);
614 return handle
->table
->allocated_len
- 1;
618 void channel_release(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
)
620 channel_free(chan
, handle
);
624 * channel_destroy - Finalize, wait for q.s. and destroy channel.
625 * @chan: channel to destroy
628 * Call "destroy" callback, finalize channels, decrement the channel
629 * reference count. Note that when readers have completed data
630 * consumption of finalized channels, get_subbuf() will return -ENODATA.
631 * They should release their handle at that point.
633 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
638 * Note: the consumer takes care of finalizing and
639 * switching the buffers.
641 channel_unregister_notifiers(chan
, handle
);
645 * sessiond/consumer are keeping a reference on the shm file
646 * descriptor directly. No need to refcount.
648 channel_release(chan
, handle
);
652 struct lttng_ust_lib_ring_buffer
*channel_get_ring_buffer(
653 const struct lttng_ust_lib_ring_buffer_config
*config
,
654 struct channel
*chan
, int cpu
,
655 struct lttng_ust_shm_handle
*handle
,
656 int *shm_fd
, int *wait_fd
,
658 uint64_t *memory_map_size
)
662 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
665 if (cpu
>= num_possible_cpus())
668 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
669 *shm_fd
= shm_get_shm_fd(handle
, ref
);
670 *wait_fd
= shm_get_wait_fd(handle
, ref
);
671 *wakeup_fd
= shm_get_wakeup_fd(handle
, ref
);
672 if (shm_get_shm_size(handle
, ref
, memory_map_size
))
674 return shmp(handle
, chan
->backend
.buf
[cpu
].shmp
);
677 int ring_buffer_close_wait_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
678 struct channel
*chan
,
679 struct lttng_ust_shm_handle
*handle
,
684 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
687 if (cpu
>= num_possible_cpus())
690 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
691 return shm_close_wait_fd(handle
, ref
);
694 int ring_buffer_close_wakeup_fd(const struct lttng_ust_lib_ring_buffer_config
*config
,
695 struct channel
*chan
,
696 struct lttng_ust_shm_handle
*handle
,
701 if (config
->alloc
== RING_BUFFER_ALLOC_GLOBAL
) {
704 if (cpu
>= num_possible_cpus())
707 ref
= &chan
->backend
.buf
[cpu
].shmp
._ref
;
708 return shm_close_wakeup_fd(handle
, ref
);
711 int lib_ring_buffer_open_read(struct lttng_ust_lib_ring_buffer
*buf
,
712 struct lttng_ust_shm_handle
*handle
)
714 if (uatomic_cmpxchg(&buf
->active_readers
, 0, 1) != 0)
720 void lib_ring_buffer_release_read(struct lttng_ust_lib_ring_buffer
*buf
,
721 struct lttng_ust_shm_handle
*handle
)
723 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
725 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
727 uatomic_dec(&buf
->active_readers
);
731 * lib_ring_buffer_snapshot - save subbuffer position snapshot (for read)
733 * @consumed: consumed count indicating the position where to read
734 * @produced: produced count, indicates position when to stop reading
736 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
737 * data to read at consumed position, or 0 if the get operation succeeds.
740 int lib_ring_buffer_snapshot(struct lttng_ust_lib_ring_buffer
*buf
,
741 unsigned long *consumed
, unsigned long *produced
,
742 struct lttng_ust_shm_handle
*handle
)
744 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
745 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
746 unsigned long consumed_cur
, write_offset
;
749 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
751 * Read finalized before counters.
754 consumed_cur
= uatomic_read(&buf
->consumed
);
756 * No need to issue a memory barrier between consumed count read and
757 * write offset read, because consumed count can only change
758 * concurrently in overwrite mode, and we keep a sequence counter
759 * identifier derived from the write offset to check we are getting
760 * the same sub-buffer we are expecting (the sub-buffers are atomically
761 * "tagged" upon writes, tags are checked upon read).
763 write_offset
= v_read(config
, &buf
->offset
);
766 * Check that we are not about to read the same subbuffer in
767 * which the writer head is.
769 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
773 *consumed
= consumed_cur
;
774 *produced
= subbuf_trunc(write_offset
, chan
);
780 * The memory barriers __wait_event()/wake_up_interruptible() take care
781 * of "raw_spin_is_locked" memory ordering.
790 * lib_ring_buffer_put_snapshot - move consumed counter forward
792 * @consumed_new: new consumed count value
794 void lib_ring_buffer_move_consumer(struct lttng_ust_lib_ring_buffer
*buf
,
795 unsigned long consumed_new
,
796 struct lttng_ust_shm_handle
*handle
)
798 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
799 struct channel
*chan
= shmp(handle
, bufb
->chan
);
800 unsigned long consumed
;
802 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
805 * Only push the consumed value forward.
806 * If the consumed cmpxchg fails, this is because we have been pushed by
807 * the writer in flight recorder mode.
809 consumed
= uatomic_read(&buf
->consumed
);
810 while ((long) consumed
- (long) consumed_new
< 0)
811 consumed
= uatomic_cmpxchg(&buf
->consumed
, consumed
,
816 * lib_ring_buffer_get_subbuf - get exclusive access to subbuffer for reading
818 * @consumed: consumed count indicating the position where to read
820 * Returns -ENODATA if buffer is finalized, -EAGAIN if there is currently no
821 * data to read at consumed position, or 0 if the get operation succeeds.
823 int lib_ring_buffer_get_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
824 unsigned long consumed
,
825 struct lttng_ust_shm_handle
*handle
)
827 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
828 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
829 unsigned long consumed_cur
, consumed_idx
, commit_count
, write_offset
;
834 finalized
= CMM_ACCESS_ONCE(buf
->finalized
);
836 * Read finalized before counters.
839 consumed_cur
= uatomic_read(&buf
->consumed
);
840 consumed_idx
= subbuf_index(consumed
, chan
);
841 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, consumed_idx
)->cc_sb
);
843 * Make sure we read the commit count before reading the buffer
844 * data and the write offset. Correct consumed offset ordering
845 * wrt commit count is insured by the use of cmpxchg to update
846 * the consumed offset.
849 * Local rmb to match the remote wmb to read the commit count
850 * before the buffer data and the write offset.
854 write_offset
= v_read(config
, &buf
->offset
);
857 * Check that the buffer we are getting is after or at consumed_cur
860 if ((long) subbuf_trunc(consumed
, chan
)
861 - (long) subbuf_trunc(consumed_cur
, chan
) < 0)
865 * Check that the subbuffer we are trying to consume has been
866 * already fully committed.
868 if (((commit_count
- chan
->backend
.subbuf_size
)
869 & chan
->commit_count_mask
)
870 - (buf_trunc(consumed_cur
, chan
)
871 >> chan
->backend
.num_subbuf_order
)
876 * Check that we are not about to read the same subbuffer in
877 * which the writer head is.
879 if (subbuf_trunc(write_offset
, chan
) - subbuf_trunc(consumed_cur
, chan
)
884 * Failure to get the subbuffer causes a busy-loop retry without going
885 * to a wait queue. These are caused by short-lived race windows where
886 * the writer is getting access to a subbuffer we were trying to get
887 * access to. Also checks that the "consumed" buffer count we are
888 * looking for matches the one contained in the subbuffer id.
890 ret
= update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
891 consumed_idx
, buf_trunc_val(consumed
, chan
),
895 subbuffer_id_clear_noref(config
, &buf
->backend
.buf_rsb
.id
);
897 buf
->get_subbuf_consumed
= consumed
;
904 * The memory barriers __wait_event()/wake_up_interruptible() take care
905 * of "raw_spin_is_locked" memory ordering.
914 * lib_ring_buffer_put_subbuf - release exclusive subbuffer access
917 void lib_ring_buffer_put_subbuf(struct lttng_ust_lib_ring_buffer
*buf
,
918 struct lttng_ust_shm_handle
*handle
)
920 struct lttng_ust_lib_ring_buffer_backend
*bufb
= &buf
->backend
;
921 struct channel
*chan
= shmp(handle
, bufb
->chan
);
922 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
923 unsigned long read_sb_bindex
, consumed_idx
, consumed
;
925 CHAN_WARN_ON(chan
, uatomic_read(&buf
->active_readers
) != 1);
927 if (!buf
->get_subbuf
) {
929 * Reader puts a subbuffer it did not get.
931 CHAN_WARN_ON(chan
, 1);
934 consumed
= buf
->get_subbuf_consumed
;
938 * Clear the records_unread counter. (overruns counter)
939 * Can still be non-zero if a file reader simply grabbed the data
940 * without using iterators.
941 * Can be below zero if an iterator is used on a snapshot more than
944 read_sb_bindex
= subbuffer_id_get_index(config
, bufb
->buf_rsb
.id
);
945 v_add(config
, v_read(config
,
946 &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
),
947 &bufb
->records_read
);
948 v_set(config
, &shmp(handle
, shmp_index(handle
, bufb
->array
, read_sb_bindex
)->shmp
)->records_unread
, 0);
949 CHAN_WARN_ON(chan
, config
->mode
== RING_BUFFER_OVERWRITE
950 && subbuffer_id_is_noref(config
, bufb
->buf_rsb
.id
));
951 subbuffer_id_set_noref(config
, &bufb
->buf_rsb
.id
);
954 * Exchange the reader subbuffer with the one we put in its place in the
955 * writer subbuffer table. Expect the original consumed count. If
956 * update_read_sb_index fails, this is because the writer updated the
957 * subbuffer concurrently. We should therefore keep the subbuffer we
958 * currently have: it has become invalid to try reading this sub-buffer
959 * consumed count value anyway.
961 consumed_idx
= subbuf_index(consumed
, chan
);
962 update_read_sb_index(config
, &buf
->backend
, &chan
->backend
,
963 consumed_idx
, buf_trunc_val(consumed
, chan
),
966 * update_read_sb_index return value ignored. Don't exchange sub-buffer
967 * if the writer concurrently updated it.
972 * cons_offset is an iterator on all subbuffer offsets between the reader
973 * position and the writer position. (inclusive)
976 void lib_ring_buffer_print_subbuffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
977 struct channel
*chan
,
978 unsigned long cons_offset
,
980 struct lttng_ust_shm_handle
*handle
)
982 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
983 unsigned long cons_idx
, commit_count
, commit_count_sb
;
985 cons_idx
= subbuf_index(cons_offset
, chan
);
986 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, cons_idx
)->cc
);
987 commit_count_sb
= v_read(config
, &shmp_index(handle
, buf
->commit_cold
, cons_idx
)->cc_sb
);
989 if (subbuf_offset(commit_count
, chan
) != 0)
990 DBG("ring buffer %s, cpu %d: "
991 "commit count in subbuffer %lu,\n"
992 "expecting multiples of %lu bytes\n"
993 " [ %lu bytes committed, %lu bytes reader-visible ]\n",
994 chan
->backend
.name
, cpu
, cons_idx
,
995 chan
->backend
.subbuf_size
,
996 commit_count
, commit_count_sb
);
998 DBG("ring buffer: %s, cpu %d: %lu bytes committed\n",
999 chan
->backend
.name
, cpu
, commit_count
);
1003 void lib_ring_buffer_print_buffer_errors(struct lttng_ust_lib_ring_buffer
*buf
,
1004 struct channel
*chan
,
1005 void *priv
, int cpu
,
1006 struct lttng_ust_shm_handle
*handle
)
1008 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1009 unsigned long write_offset
, cons_offset
;
1012 * No need to order commit_count, write_offset and cons_offset reads
1013 * because we execute at teardown when no more writer nor reader
1014 * references are left.
1016 write_offset
= v_read(config
, &buf
->offset
);
1017 cons_offset
= uatomic_read(&buf
->consumed
);
1018 if (write_offset
!= cons_offset
)
1019 DBG("ring buffer %s, cpu %d: "
1020 "non-consumed data\n"
1021 " [ %lu bytes written, %lu bytes read ]\n",
1022 chan
->backend
.name
, cpu
, write_offset
, cons_offset
);
1024 for (cons_offset
= uatomic_read(&buf
->consumed
);
1025 (long) (subbuf_trunc((unsigned long) v_read(config
, &buf
->offset
),
1028 cons_offset
= subbuf_align(cons_offset
, chan
))
1029 lib_ring_buffer_print_subbuffer_errors(buf
, chan
, cons_offset
,
1034 void lib_ring_buffer_print_errors(struct channel
*chan
,
1035 struct lttng_ust_lib_ring_buffer
*buf
, int cpu
,
1036 struct lttng_ust_shm_handle
*handle
)
1038 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1039 void *priv
= channel_get_private(chan
);
1041 if (!strcmp(chan
->backend
.name
, "relay-metadata-mmap")) {
1042 DBG("ring buffer %s: %lu records written, "
1043 "%lu records overrun\n",
1045 v_read(config
, &buf
->records_count
),
1046 v_read(config
, &buf
->records_overrun
));
1048 DBG("ring buffer %s, cpu %d: %lu records written, "
1049 "%lu records overrun\n",
1050 chan
->backend
.name
, cpu
,
1051 v_read(config
, &buf
->records_count
),
1052 v_read(config
, &buf
->records_overrun
));
1054 if (v_read(config
, &buf
->records_lost_full
)
1055 || v_read(config
, &buf
->records_lost_wrap
)
1056 || v_read(config
, &buf
->records_lost_big
))
1057 DBG("ring buffer %s, cpu %d: records were lost. Caused by:\n"
1058 " [ %lu buffer full, %lu nest buffer wrap-around, "
1059 "%lu event too big ]\n",
1060 chan
->backend
.name
, cpu
,
1061 v_read(config
, &buf
->records_lost_full
),
1062 v_read(config
, &buf
->records_lost_wrap
),
1063 v_read(config
, &buf
->records_lost_big
));
1065 lib_ring_buffer_print_buffer_errors(buf
, chan
, priv
, cpu
, handle
);
1069 * lib_ring_buffer_switch_old_start: Populate old subbuffer header.
1071 * Only executed when the buffer is finalized, in SWITCH_FLUSH.
1074 void lib_ring_buffer_switch_old_start(struct lttng_ust_lib_ring_buffer
*buf
,
1075 struct channel
*chan
,
1076 struct switch_offsets
*offsets
,
1078 struct lttng_ust_shm_handle
*handle
)
1080 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1081 unsigned long oldidx
= subbuf_index(offsets
->old
, chan
);
1082 unsigned long commit_count
;
1084 config
->cb
.buffer_begin(buf
, tsc
, oldidx
, handle
);
1087 * Order all writes to buffer before the commit count update that will
1088 * determine that the subbuffer is full.
1091 v_add(config
, config
->cb
.subbuffer_header_size(),
1092 &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1093 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1094 /* Check if the written buffer has to be delivered */
1095 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
,
1096 commit_count
, oldidx
, handle
);
1097 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1098 offsets
->old
, commit_count
,
1099 config
->cb
.subbuffer_header_size(),
1104 * lib_ring_buffer_switch_old_end: switch old subbuffer
1106 * Note : offset_old should never be 0 here. It is ok, because we never perform
1107 * buffer switch on an empty subbuffer in SWITCH_ACTIVE mode. The caller
1108 * increments the offset_old value when doing a SWITCH_FLUSH on an empty
1112 void lib_ring_buffer_switch_old_end(struct lttng_ust_lib_ring_buffer
*buf
,
1113 struct channel
*chan
,
1114 struct switch_offsets
*offsets
,
1116 struct lttng_ust_shm_handle
*handle
)
1118 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1119 unsigned long oldidx
= subbuf_index(offsets
->old
- 1, chan
);
1120 unsigned long commit_count
, padding_size
, data_size
;
1122 data_size
= subbuf_offset(offsets
->old
- 1, chan
) + 1;
1123 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1124 subbuffer_set_data_size(config
, &buf
->backend
, oldidx
, data_size
,
1128 * Order all writes to buffer before the commit count update that will
1129 * determine that the subbuffer is full.
1132 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1133 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, oldidx
)->cc
);
1134 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->old
- 1,
1135 commit_count
, oldidx
, handle
);
1136 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, oldidx
,
1137 offsets
->old
, commit_count
,
1138 padding_size
, handle
);
1142 * lib_ring_buffer_switch_new_start: Populate new subbuffer.
1144 * This code can be executed unordered : writers may already have written to the
1145 * sub-buffer before this code gets executed, caution. The commit makes sure
1146 * that this code is executed before the deliver of this sub-buffer.
1149 void lib_ring_buffer_switch_new_start(struct lttng_ust_lib_ring_buffer
*buf
,
1150 struct channel
*chan
,
1151 struct switch_offsets
*offsets
,
1153 struct lttng_ust_shm_handle
*handle
)
1155 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1156 unsigned long beginidx
= subbuf_index(offsets
->begin
, chan
);
1157 unsigned long commit_count
;
1159 config
->cb
.buffer_begin(buf
, tsc
, beginidx
, handle
);
1162 * Order all writes to buffer before the commit count update that will
1163 * determine that the subbuffer is full.
1166 v_add(config
, config
->cb
.subbuffer_header_size(),
1167 &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1168 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, beginidx
)->cc
);
1169 /* Check if the written buffer has to be delivered */
1170 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->begin
,
1171 commit_count
, beginidx
, handle
);
1172 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, beginidx
,
1173 offsets
->begin
, commit_count
,
1174 config
->cb
.subbuffer_header_size(),
1179 * lib_ring_buffer_switch_new_end: finish switching current subbuffer
1181 * The only remaining threads could be the ones with pending commits. They will
1182 * have to do the deliver themselves.
1185 void lib_ring_buffer_switch_new_end(struct lttng_ust_lib_ring_buffer
*buf
,
1186 struct channel
*chan
,
1187 struct switch_offsets
*offsets
,
1189 struct lttng_ust_shm_handle
*handle
)
1191 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1192 unsigned long endidx
= subbuf_index(offsets
->end
- 1, chan
);
1193 unsigned long commit_count
, padding_size
, data_size
;
1195 data_size
= subbuf_offset(offsets
->end
- 1, chan
) + 1;
1196 padding_size
= chan
->backend
.subbuf_size
- data_size
;
1197 subbuffer_set_data_size(config
, &buf
->backend
, endidx
, data_size
,
1201 * Order all writes to buffer before the commit count update that will
1202 * determine that the subbuffer is full.
1205 v_add(config
, padding_size
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1206 commit_count
= v_read(config
, &shmp_index(handle
, buf
->commit_hot
, endidx
)->cc
);
1207 lib_ring_buffer_check_deliver(config
, buf
, chan
, offsets
->end
- 1,
1208 commit_count
, endidx
, handle
);
1209 lib_ring_buffer_write_commit_counter(config
, buf
, chan
, endidx
,
1210 offsets
->end
, commit_count
,
1211 padding_size
, handle
);
1217 * !0 if execution must be aborted.
1220 int lib_ring_buffer_try_switch_slow(enum switch_mode mode
,
1221 struct lttng_ust_lib_ring_buffer
*buf
,
1222 struct channel
*chan
,
1223 struct switch_offsets
*offsets
,
1226 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1229 offsets
->begin
= v_read(config
, &buf
->offset
);
1230 offsets
->old
= offsets
->begin
;
1231 offsets
->switch_old_start
= 0;
1232 off
= subbuf_offset(offsets
->begin
, chan
);
1234 *tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1237 * Ensure we flush the header of an empty subbuffer when doing the
1238 * finalize (SWITCH_FLUSH). This ensures that we end up knowing the
1239 * total data gathering duration even if there were no records saved
1240 * after the last buffer switch.
1241 * In SWITCH_ACTIVE mode, switch the buffer when it contains events.
1242 * SWITCH_ACTIVE only flushes the current subbuffer, dealing with end of
1243 * subbuffer header as appropriate.
1244 * The next record that reserves space will be responsible for
1245 * populating the following subbuffer header. We choose not to populate
1246 * the next subbuffer header here because we want to be able to use
1247 * SWITCH_ACTIVE for periodical buffer flush, which must
1248 * guarantee that all the buffer content (records and header
1249 * timestamps) are visible to the reader. This is required for
1250 * quiescence guarantees for the fusion merge.
1252 if (mode
== SWITCH_FLUSH
|| off
> 0) {
1253 if (caa_unlikely(off
== 0)) {
1255 * A final flush that encounters an empty
1256 * sub-buffer cannot switch buffer if a
1257 * reader is located within this sub-buffer.
1258 * Anyway, the purpose of final flushing of a
1259 * sub-buffer at offset 0 is to handle the case
1260 * of entirely empty stream.
1262 if (caa_unlikely(subbuf_trunc(offsets
->begin
, chan
)
1263 - subbuf_trunc((unsigned long)
1264 uatomic_read(&buf
->consumed
), chan
)
1265 >= chan
->backend
.buf_size
))
1268 * The client does not save any header information.
1269 * Don't switch empty subbuffer on finalize, because it
1270 * is invalid to deliver a completely empty subbuffer.
1272 if (!config
->cb
.subbuffer_header_size())
1275 * Need to write the subbuffer start header on finalize.
1277 offsets
->switch_old_start
= 1;
1279 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1281 return -1; /* we do not have to switch : buffer is empty */
1282 /* Note: old points to the next subbuf at offset 0 */
1283 offsets
->end
= offsets
->begin
;
1288 * Force a sub-buffer switch. This operation is completely reentrant : can be
1289 * called while tracing is active with absolutely no lock held.
1291 * Note, however, that as a v_cmpxchg is used for some atomic
1292 * operations, this function must be called from the CPU which owns the buffer
1293 * for a ACTIVE flush.
1295 void lib_ring_buffer_switch_slow(struct lttng_ust_lib_ring_buffer
*buf
, enum switch_mode mode
,
1296 struct lttng_ust_shm_handle
*handle
)
1298 struct channel
*chan
= shmp(handle
, buf
->backend
.chan
);
1299 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1300 struct switch_offsets offsets
;
1301 unsigned long oldidx
;
1307 * Perform retryable operations.
1310 if (lib_ring_buffer_try_switch_slow(mode
, buf
, chan
, &offsets
,
1312 return; /* Switch not needed */
1313 } while (v_cmpxchg(config
, &buf
->offset
, offsets
.old
, offsets
.end
)
1317 * Atomically update last_tsc. This update races against concurrent
1318 * atomic updates, but the race will always cause supplementary full TSC
1319 * records, never the opposite (missing a full TSC record when it would
1322 save_last_tsc(config
, buf
, tsc
);
1325 * Push the reader if necessary
1327 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.old
);
1329 oldidx
= subbuf_index(offsets
.old
, chan
);
1330 lib_ring_buffer_clear_noref(config
, &buf
->backend
, oldidx
, handle
);
1333 * May need to populate header start on SWITCH_FLUSH.
1335 if (offsets
.switch_old_start
) {
1336 lib_ring_buffer_switch_old_start(buf
, chan
, &offsets
, tsc
, handle
);
1337 offsets
.old
+= config
->cb
.subbuffer_header_size();
1341 * Switch old subbuffer.
1343 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, tsc
, handle
);
1349 * -ENOSPC if event size is too large for packet.
1350 * -ENOBUFS if there is currently not enough space in buffer for the event.
1351 * -EIO if data cannot be written into the buffer for any other reason.
1354 int lib_ring_buffer_try_reserve_slow(struct lttng_ust_lib_ring_buffer
*buf
,
1355 struct channel
*chan
,
1356 struct switch_offsets
*offsets
,
1357 struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1359 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1360 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1361 unsigned long reserve_commit_diff
;
1363 offsets
->begin
= v_read(config
, &buf
->offset
);
1364 offsets
->old
= offsets
->begin
;
1365 offsets
->switch_new_start
= 0;
1366 offsets
->switch_new_end
= 0;
1367 offsets
->switch_old_end
= 0;
1368 offsets
->pre_header_padding
= 0;
1370 ctx
->tsc
= config
->cb
.ring_buffer_clock_read(chan
);
1371 if ((int64_t) ctx
->tsc
== -EIO
)
1374 if (last_tsc_overflow(config
, buf
, ctx
->tsc
))
1375 ctx
->rflags
|= RING_BUFFER_RFLAG_FULL_TSC
;
1377 if (caa_unlikely(subbuf_offset(offsets
->begin
, ctx
->chan
) == 0)) {
1378 offsets
->switch_new_start
= 1; /* For offsets->begin */
1380 offsets
->size
= config
->cb
.record_header_size(config
, chan
,
1382 &offsets
->pre_header_padding
,
1385 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1388 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
) +
1389 offsets
->size
> chan
->backend
.subbuf_size
)) {
1390 offsets
->switch_old_end
= 1; /* For offsets->old */
1391 offsets
->switch_new_start
= 1; /* For offsets->begin */
1394 if (caa_unlikely(offsets
->switch_new_start
)) {
1395 unsigned long sb_index
;
1398 * We are typically not filling the previous buffer completely.
1400 if (caa_likely(offsets
->switch_old_end
))
1401 offsets
->begin
= subbuf_align(offsets
->begin
, chan
);
1402 offsets
->begin
= offsets
->begin
1403 + config
->cb
.subbuffer_header_size();
1404 /* Test new buffer integrity */
1405 sb_index
= subbuf_index(offsets
->begin
, chan
);
1406 reserve_commit_diff
=
1407 (buf_trunc(offsets
->begin
, chan
)
1408 >> chan
->backend
.num_subbuf_order
)
1409 - ((unsigned long) v_read(config
,
1410 &shmp_index(handle
, buf
->commit_cold
, sb_index
)->cc_sb
)
1411 & chan
->commit_count_mask
);
1412 if (caa_likely(reserve_commit_diff
== 0)) {
1413 /* Next subbuffer not being written to. */
1414 if (caa_unlikely(config
->mode
!= RING_BUFFER_OVERWRITE
&&
1415 subbuf_trunc(offsets
->begin
, chan
)
1416 - subbuf_trunc((unsigned long)
1417 uatomic_read(&buf
->consumed
), chan
)
1418 >= chan
->backend
.buf_size
)) {
1419 unsigned long nr_lost
;
1422 * We do not overwrite non consumed buffers
1423 * and we are full : record is lost.
1425 nr_lost
= v_read(config
, &buf
->records_lost_full
);
1426 v_inc(config
, &buf
->records_lost_full
);
1427 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1428 DBG("%lu or more records lost in (%s:%d) (buffer full)\n",
1429 nr_lost
+ 1, chan
->backend
.name
,
1435 * Next subbuffer not being written to, and we
1436 * are either in overwrite mode or the buffer is
1437 * not full. It's safe to write in this new
1442 unsigned long nr_lost
;
1445 * Next subbuffer reserve offset does not match the
1446 * commit offset. Drop record in producer-consumer and
1447 * overwrite mode. Caused by either a writer OOPS or too
1448 * many nested writes over a reserve/commit pair.
1450 nr_lost
= v_read(config
, &buf
->records_lost_wrap
);
1451 v_inc(config
, &buf
->records_lost_wrap
);
1452 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1453 DBG("%lu or more records lost in (%s:%d) (wrap-around)\n",
1454 nr_lost
+ 1, chan
->backend
.name
,
1460 config
->cb
.record_header_size(config
, chan
,
1462 &offsets
->pre_header_padding
,
1465 lib_ring_buffer_align(offsets
->begin
+ offsets
->size
,
1468 if (caa_unlikely(subbuf_offset(offsets
->begin
, chan
)
1469 + offsets
->size
> chan
->backend
.subbuf_size
)) {
1470 unsigned long nr_lost
;
1473 * Record too big for subbuffers, report error, don't
1474 * complete the sub-buffer switch.
1476 nr_lost
= v_read(config
, &buf
->records_lost_big
);
1477 v_inc(config
, &buf
->records_lost_big
);
1478 if ((nr_lost
& (DBG_PRINT_NR_LOST
- 1)) == 0) {
1479 DBG("%lu or more records lost in (%s:%d) record size "
1480 " of %zu bytes is too large for buffer\n",
1481 nr_lost
+ 1, chan
->backend
.name
,
1482 buf
->backend
.cpu
, offsets
->size
);
1487 * We just made a successful buffer switch and the
1488 * record fits in the new subbuffer. Let's write.
1493 * Record fits in the current buffer and we are not on a switch
1494 * boundary. It's safe to write.
1497 offsets
->end
= offsets
->begin
+ offsets
->size
;
1499 if (caa_unlikely(subbuf_offset(offsets
->end
, chan
) == 0)) {
1501 * The offset_end will fall at the very beginning of the next
1504 offsets
->switch_new_end
= 1; /* For offsets->begin */
1510 * lib_ring_buffer_reserve_slow - Atomic slot reservation in a buffer.
1511 * @ctx: ring buffer context.
1513 * Return : -NOBUFS if not enough space, -ENOSPC if event size too large,
1514 * -EIO for other errors, else returns 0.
1515 * It will take care of sub-buffer switching.
1517 int lib_ring_buffer_reserve_slow(struct lttng_ust_lib_ring_buffer_ctx
*ctx
)
1519 struct channel
*chan
= ctx
->chan
;
1520 struct lttng_ust_shm_handle
*handle
= ctx
->handle
;
1521 const struct lttng_ust_lib_ring_buffer_config
*config
= &chan
->backend
.config
;
1522 struct lttng_ust_lib_ring_buffer
*buf
;
1523 struct switch_offsets offsets
;
1526 if (config
->alloc
== RING_BUFFER_ALLOC_PER_CPU
)
1527 buf
= shmp(handle
, chan
->backend
.buf
[ctx
->cpu
].shmp
);
1529 buf
= shmp(handle
, chan
->backend
.buf
[0].shmp
);
1535 ret
= lib_ring_buffer_try_reserve_slow(buf
, chan
, &offsets
,
1537 if (caa_unlikely(ret
))
1539 } while (caa_unlikely(v_cmpxchg(config
, &buf
->offset
, offsets
.old
,
1544 * Atomically update last_tsc. This update races against concurrent
1545 * atomic updates, but the race will always cause supplementary full TSC
1546 * records, never the opposite (missing a full TSC record when it would
1549 save_last_tsc(config
, buf
, ctx
->tsc
);
1552 * Push the reader if necessary
1554 lib_ring_buffer_reserve_push_reader(buf
, chan
, offsets
.end
- 1);
1557 * Clear noref flag for this subbuffer.
1559 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1560 subbuf_index(offsets
.end
- 1, chan
),
1564 * Switch old subbuffer if needed.
1566 if (caa_unlikely(offsets
.switch_old_end
)) {
1567 lib_ring_buffer_clear_noref(config
, &buf
->backend
,
1568 subbuf_index(offsets
.old
- 1, chan
),
1570 lib_ring_buffer_switch_old_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1574 * Populate new subbuffer.
1576 if (caa_unlikely(offsets
.switch_new_start
))
1577 lib_ring_buffer_switch_new_start(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1579 if (caa_unlikely(offsets
.switch_new_end
))
1580 lib_ring_buffer_switch_new_end(buf
, chan
, &offsets
, ctx
->tsc
, handle
);
1582 ctx
->slot_size
= offsets
.size
;
1583 ctx
->pre_offset
= offsets
.begin
;
1584 ctx
->buf_offset
= offsets
.begin
+ offsets
.pre_header_padding
;
1589 * Force a read (imply TLS fixup for dlopen) of TLS variables.
1591 void lttng_fixup_ringbuffer_tls(void)
1593 asm volatile ("" : : "m" (URCU_TLS(lib_ring_buffer_nesting
)));