2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
11 #include "consumer-stream.hpp"
13 #include <common/common.hpp>
14 #include <common/consumer/consumer-timer.hpp>
15 #include <common/consumer/consumer.hpp>
16 #include <common/consumer/metadata-bucket.hpp>
17 #include <common/index/index.hpp>
18 #include <common/kernel-consumer/kernel-consumer.hpp>
19 #include <common/kernel-ctl/kernel-ctl.hpp>
20 #include <common/macros.hpp>
21 #include <common/relayd/relayd.hpp>
22 #include <common/ust-consumer/ust-consumer.hpp>
23 #include <common/utils.hpp>
30 * RCU call to free stream. MUST only be used with call_rcu().
32 static void free_stream_rcu(struct rcu_head
*head
)
34 struct lttng_ht_node_u64
*node
= lttng::utils::container_of(head
, <tng_ht_node_u64::head
);
35 struct lttng_consumer_stream
*stream
=
36 lttng::utils::container_of(node
, <tng_consumer_stream::node
);
38 pthread_mutex_destroy(&stream
->lock
);
42 static void consumer_stream_data_lock_all(struct lttng_consumer_stream
*stream
)
44 pthread_mutex_lock(&stream
->chan
->lock
);
45 pthread_mutex_lock(&stream
->lock
);
48 static void consumer_stream_data_unlock_all(struct lttng_consumer_stream
*stream
)
50 pthread_mutex_unlock(&stream
->lock
);
51 pthread_mutex_unlock(&stream
->chan
->lock
);
54 static void consumer_stream_data_assert_locked_all(struct lttng_consumer_stream
*stream
)
56 ASSERT_LOCKED(stream
->lock
);
57 ASSERT_LOCKED(stream
->chan
->lock
);
60 static void consumer_stream_metadata_lock_all(struct lttng_consumer_stream
*stream
)
62 consumer_stream_data_lock_all(stream
);
63 pthread_mutex_lock(&stream
->metadata_rdv_lock
);
66 static void consumer_stream_metadata_unlock_all(struct lttng_consumer_stream
*stream
)
68 pthread_mutex_unlock(&stream
->metadata_rdv_lock
);
69 consumer_stream_data_unlock_all(stream
);
72 static void consumer_stream_metadata_assert_locked_all(struct lttng_consumer_stream
*stream
)
74 ASSERT_LOCKED(stream
->metadata_rdv_lock
);
75 consumer_stream_data_assert_locked_all(stream
);
78 /* Only used for data streams. */
79 static int consumer_stream_update_stats(struct lttng_consumer_stream
*stream
,
80 const struct stream_subbuffer
*subbuf
)
83 uint64_t sequence_number
;
84 const uint64_t discarded_events
= subbuf
->info
.data
.events_discarded
;
86 if (!subbuf
->info
.data
.sequence_number
.is_set
) {
87 /* Command not supported by the tracer. */
88 sequence_number
= -1ULL;
89 stream
->sequence_number_unavailable
= true;
91 sequence_number
= subbuf
->info
.data
.sequence_number
.value
;
95 * Start the sequence when we extract the first packet in case we don't
96 * start at 0 (for example if a consumer is not connected to the
97 * session immediately after the beginning).
99 if (stream
->last_sequence_number
== -1ULL) {
100 stream
->last_sequence_number
= sequence_number
;
101 } else if (sequence_number
> stream
->last_sequence_number
) {
102 stream
->chan
->lost_packets
+= sequence_number
- stream
->last_sequence_number
- 1;
104 /* seq <= last_sequence_number */
105 ERR("Sequence number inconsistent : prev = %" PRIu64
", current = %" PRIu64
,
106 stream
->last_sequence_number
,
111 stream
->last_sequence_number
= sequence_number
;
113 if (discarded_events
< stream
->last_discarded_events
) {
115 * Overflow has occurred. We assume only one wrap-around
118 stream
->chan
->discarded_events
+= (1ULL << (CAA_BITS_PER_LONG
- 1)) -
119 stream
->last_discarded_events
+ discarded_events
;
121 stream
->chan
->discarded_events
+= discarded_events
- stream
->last_discarded_events
;
123 stream
->last_discarded_events
= discarded_events
;
130 static void ctf_packet_index_populate(struct ctf_packet_index
*index
,
132 const struct stream_subbuffer
*subbuffer
)
134 *index
= (typeof(*index
)){
135 .offset
= htobe64(offset
),
136 .packet_size
= htobe64(subbuffer
->info
.data
.packet_size
),
137 .content_size
= htobe64(subbuffer
->info
.data
.content_size
),
138 .timestamp_begin
= htobe64(subbuffer
->info
.data
.timestamp_begin
),
139 .timestamp_end
= htobe64(subbuffer
->info
.data
.timestamp_end
),
140 .events_discarded
= htobe64(subbuffer
->info
.data
.events_discarded
),
141 .stream_id
= htobe64(subbuffer
->info
.data
.stream_id
),
142 .stream_instance_id
=
143 htobe64(subbuffer
->info
.data
.stream_instance_id
.is_set
?
144 subbuffer
->info
.data
.stream_instance_id
.value
:
146 .packet_seq_num
= htobe64(subbuffer
->info
.data
.sequence_number
.is_set
?
147 subbuffer
->info
.data
.sequence_number
.value
:
152 static ssize_t
consumer_stream_consume_mmap(struct lttng_consumer_local_data
*ctx
153 __attribute__((unused
)),
154 struct lttng_consumer_stream
*stream
,
155 const struct stream_subbuffer
*subbuffer
)
157 const unsigned long padding_size
=
158 subbuffer
->info
.data
.padded_subbuf_size
- subbuffer
->info
.data
.subbuf_size
;
159 const ssize_t written_bytes
= lttng_consumer_on_read_subbuffer_mmap(
160 stream
, &subbuffer
->buffer
.buffer
, padding_size
);
162 if (stream
->net_seq_idx
== -1ULL) {
164 * When writing on disk, check that only the subbuffer (no
165 * padding) was written to disk.
167 if (written_bytes
!= subbuffer
->info
.data
.padded_subbuf_size
) {
168 DBG("Failed to write the entire padded subbuffer on disk (written_bytes: %zd, padded subbuffer size %lu)",
170 subbuffer
->info
.data
.padded_subbuf_size
);
174 * When streaming over the network, check that the entire
175 * subbuffer including padding was successfully written.
177 if (written_bytes
!= subbuffer
->info
.data
.subbuf_size
) {
178 DBG("Failed to write only the subbuffer over the network (written_bytes: %zd, subbuffer size %lu)",
180 subbuffer
->info
.data
.subbuf_size
);
185 * If `lttng_consumer_on_read_subbuffer_mmap()` returned an error, pass
186 * it along to the caller, else return zero.
188 if (written_bytes
< 0) {
189 ERR("Error reading mmap subbuffer: %zd", written_bytes
);
192 return written_bytes
;
195 static ssize_t
consumer_stream_consume_splice(struct lttng_consumer_local_data
*ctx
,
196 struct lttng_consumer_stream
*stream
,
197 const struct stream_subbuffer
*subbuffer
)
199 const ssize_t written_bytes
= lttng_consumer_on_read_subbuffer_splice(
200 ctx
, stream
, subbuffer
->info
.data
.padded_subbuf_size
, 0);
202 if (written_bytes
!= subbuffer
->info
.data
.padded_subbuf_size
) {
203 DBG("Failed to write the entire padded subbuffer (written_bytes: %zd, padded subbuffer size %lu)",
205 subbuffer
->info
.data
.padded_subbuf_size
);
209 * If `lttng_consumer_on_read_subbuffer_splice()` returned an error,
210 * pass it along to the caller, else return zero.
212 if (written_bytes
< 0) {
213 ERR("Error reading splice subbuffer: %zd", written_bytes
);
216 return written_bytes
;
219 static int consumer_stream_send_index(struct lttng_consumer_stream
*stream
,
220 const struct stream_subbuffer
*subbuffer
,
221 struct lttng_consumer_local_data
*ctx
__attribute__((unused
)))
223 off_t packet_offset
= 0;
224 struct ctf_packet_index index
= {};
227 * This is called after consuming the sub-buffer; substract the
228 * effect this sub-buffer from the offset.
230 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
231 packet_offset
= stream
->out_fd_offset
- subbuffer
->info
.data
.padded_subbuf_size
;
234 ctf_packet_index_populate(&index
, packet_offset
, subbuffer
);
235 return consumer_stream_write_index(stream
, &index
);
239 * Actually do the metadata sync using the given metadata stream.
241 * Return 0 on success else a negative value. ENODATA can be returned also
242 * indicating that there is no metadata available for that stream.
244 static int do_sync_metadata(struct lttng_consumer_stream
*metadata
,
245 struct lttng_consumer_local_data
*ctx
)
248 enum sync_metadata_status status
;
250 LTTNG_ASSERT(metadata
);
251 LTTNG_ASSERT(metadata
->metadata_flag
);
255 * In UST, since we have to write the metadata from the cache packet
256 * by packet, we might need to start this procedure multiple times
257 * until all the metadata from the cache has been extracted.
262 * - Lock the metadata stream
263 * - Check if metadata stream node was deleted before locking.
264 * - if yes, release and return success
265 * - Check if new metadata is ready (flush + snapshot pos)
266 * - If nothing : release and return.
267 * - Lock the metadata_rdv_lock
268 * - Unlock the metadata stream
269 * - cond_wait on metadata_rdv to wait the wakeup from the
271 * - Unlock the metadata_rdv_lock
273 pthread_mutex_lock(&metadata
->lock
);
276 * There is a possibility that we were able to acquire a reference on the
277 * stream from the RCU hash table but between then and now, the node might
278 * have been deleted just before the lock is acquired. Thus, after locking,
279 * we make sure the metadata node has not been deleted which means that the
280 * buffers are closed.
282 * In that case, there is no need to sync the metadata hence returning a
283 * success return code.
285 ret
= cds_lfht_is_node_deleted(&metadata
->node
.node
);
288 goto end_unlock_mutex
;
292 case LTTNG_CONSUMER_KERNEL
:
294 * Empty the metadata cache and flush the current stream.
296 status
= lttng_kconsumer_sync_metadata(metadata
);
298 case LTTNG_CONSUMER32_UST
:
299 case LTTNG_CONSUMER64_UST
:
301 * Ask the sessiond if we have new metadata waiting and update the
302 * consumer metadata cache.
304 status
= lttng_ustconsumer_sync_metadata(ctx
, metadata
);
311 case SYNC_METADATA_STATUS_NEW_DATA
:
313 case SYNC_METADATA_STATUS_NO_DATA
:
315 goto end_unlock_mutex
;
316 case SYNC_METADATA_STATUS_ERROR
:
318 goto end_unlock_mutex
;
324 * At this point, new metadata have been flushed, so we wait on the
325 * rendez-vous point for the metadata thread to wake us up when it
326 * finishes consuming the metadata and continue execution.
329 pthread_mutex_lock(&metadata
->metadata_rdv_lock
);
332 * Release metadata stream lock so the metadata thread can process it.
334 pthread_mutex_unlock(&metadata
->lock
);
337 * Wait on the rendez-vous point. Once woken up, it means the metadata was
338 * consumed and thus synchronization is achieved.
340 pthread_cond_wait(&metadata
->metadata_rdv
, &metadata
->metadata_rdv_lock
);
341 pthread_mutex_unlock(&metadata
->metadata_rdv_lock
);
342 } while (status
== SYNC_METADATA_STATUS_NEW_DATA
);
348 pthread_mutex_unlock(&metadata
->lock
);
353 * Synchronize the metadata using a given session ID. A successful acquisition
354 * of a metadata stream will trigger a request to the session daemon and a
355 * snapshot so the metadata thread can consume it.
357 * This function call is a rendez-vous point between the metadata thread and
360 * Return 0 on success or else a negative value.
362 int consumer_stream_sync_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t session_id
)
365 struct lttng_consumer_stream
*stream
= nullptr;
366 struct lttng_ht_iter iter
;
371 /* Ease our life a bit. */
372 ht
= the_consumer_data
.stream_list_ht
;
376 /* Search the metadata associated with the session id of the given stream. */
378 cds_lfht_for_each_entry_duplicate(ht
->ht
,
379 ht
->hash_fct(&session_id
, lttng_ht_seed
),
384 node_session_id
.node
)
386 if (!stream
->metadata_flag
) {
390 ret
= do_sync_metadata(stream
, ctx
);
397 * Force return code to 0 (success) since ret might be ENODATA for instance
398 * which is not an error but rather that we should come back.
407 static int consumer_stream_sync_metadata_index(struct lttng_consumer_stream
*stream
,
408 const struct stream_subbuffer
*subbuffer
,
409 struct lttng_consumer_local_data
*ctx
)
411 bool missed_metadata_flush
;
414 /* Block until all the metadata is sent. */
415 pthread_mutex_lock(&stream
->metadata_timer_lock
);
416 LTTNG_ASSERT(!stream
->missed_metadata_flush
);
417 stream
->waiting_on_metadata
= true;
418 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
420 ret
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
422 pthread_mutex_lock(&stream
->metadata_timer_lock
);
423 stream
->waiting_on_metadata
= false;
424 missed_metadata_flush
= stream
->missed_metadata_flush
;
425 if (missed_metadata_flush
) {
426 stream
->missed_metadata_flush
= false;
428 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
433 ret
= consumer_stream_send_index(stream
, subbuffer
, ctx
);
435 * Send the live inactivity beacon to handle the situation where
436 * the live timer is prevented from sampling this stream
437 * because the stream lock was being held while this stream is
438 * waiting on metadata. This ensures live viewer progress in the
439 * unlikely scenario where a live timer would be prevented from
440 * locking a stream lock repeatedly due to a steady flow of
441 * incoming metadata, for a stream which is mostly inactive.
443 * It is important to send the inactivity beacon packet to
444 * relayd _after_ sending the index associated with the data
445 * that was just sent, otherwise this can cause live viewers to
446 * observe timestamps going backwards between an inactivity
447 * beacon and a following trace packet.
449 if (missed_metadata_flush
) {
450 (void) stream
->read_subbuffer_ops
.send_live_beacon(stream
);
457 * Check if the local version of the metadata stream matches with the version
458 * of the metadata stream in the kernel. If it was updated, set the reset flag
461 static int metadata_stream_check_version(struct lttng_consumer_stream
*stream
,
462 const struct stream_subbuffer
*subbuffer
)
464 if (stream
->metadata_version
== subbuffer
->info
.metadata
.version
) {
468 DBG("New metadata version detected");
469 consumer_stream_metadata_set_version(stream
, subbuffer
->info
.metadata
.version
);
471 if (stream
->read_subbuffer_ops
.reset_metadata
) {
472 stream
->read_subbuffer_ops
.reset_metadata(stream
);
479 static bool stream_is_rotating_to_null_chunk(const struct lttng_consumer_stream
*stream
)
481 bool rotating_to_null_chunk
= false;
483 if (stream
->rotate_position
== -1ULL) {
484 /* No rotation ongoing. */
488 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
|| !stream
->chan
->trace_chunk
) {
489 rotating_to_null_chunk
= true;
492 return rotating_to_null_chunk
;
495 enum consumer_stream_open_packet_status
496 consumer_stream_open_packet(struct lttng_consumer_stream
*stream
)
499 enum consumer_stream_open_packet_status status
;
500 unsigned long produced_pos_before
, produced_pos_after
;
502 ret
= lttng_consumer_sample_snapshot_positions(stream
);
504 ERR("Failed to snapshot positions before post-rotation empty packet flush: stream id = %" PRIu64
505 ", channel name = %s, session id = %" PRIu64
,
508 stream
->chan
->session_id
);
509 status
= CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
;
513 ret
= lttng_consumer_get_produced_snapshot(stream
, &produced_pos_before
);
515 ERR("Failed to read produced position before post-rotation empty packet flush: stream id = %" PRIu64
516 ", channel name = %s, session id = %" PRIu64
,
519 stream
->chan
->session_id
);
520 status
= CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
;
524 ret
= consumer_stream_flush_buffer(stream
, false);
526 ERR("Failed to flush an empty packet at rotation point: stream id = %" PRIu64
527 ", channel name = %s, session id = %" PRIu64
,
530 stream
->chan
->session_id
);
531 status
= CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
;
535 ret
= lttng_consumer_sample_snapshot_positions(stream
);
537 ERR("Failed to snapshot positions after post-rotation empty packet flush: stream id = %" PRIu64
538 ", channel name = %s, session id = %" PRIu64
,
541 stream
->chan
->session_id
);
542 status
= CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
;
546 ret
= lttng_consumer_get_produced_snapshot(stream
, &produced_pos_after
);
548 ERR("Failed to read produced position after post-rotation empty packet flush: stream id = %" PRIu64
549 ", channel name = %s, session id = %" PRIu64
,
552 stream
->chan
->session_id
);
553 status
= CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
;
558 * Determine if the flush had an effect by comparing the produced
559 * positons before and after the flush.
561 status
= produced_pos_before
!= produced_pos_after
?
562 CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
563 CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
;
564 if (status
== CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
) {
565 stream
->opened_packet_in_current_trace_chunk
= true;
573 * An attempt to open a new packet is performed after a rotation completes to
574 * get a begin timestamp as close as possible to the rotation point.
576 * However, that initial attempt at opening a packet can fail due to a full
577 * ring-buffer. In that case, a second attempt is performed after consuming
578 * a packet since that will have freed enough space in the ring-buffer.
580 static int post_consume_open_new_packet(struct lttng_consumer_stream
*stream
,
581 const struct stream_subbuffer
*subbuffer
582 __attribute__((unused
)),
583 struct lttng_consumer_local_data
*ctx
584 __attribute__((unused
)))
588 if (!stream
->opened_packet_in_current_trace_chunk
&& stream
->trace_chunk
&&
589 !stream_is_rotating_to_null_chunk(stream
)) {
590 const enum consumer_stream_open_packet_status status
=
591 consumer_stream_open_packet(stream
);
594 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
595 DBG("Opened a packet after consuming a packet rotation: stream id = %" PRIu64
596 ", channel name = %s, session id = %" PRIu64
,
599 stream
->chan
->session_id
);
600 stream
->opened_packet_in_current_trace_chunk
= true;
602 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
604 * Can't open a packet as there is no space left.
605 * This means that new events were produced, resulting
606 * in a packet being opened, which is what we want
609 DBG("No space left to open a packet after consuming a packet: stream id = %" PRIu64
610 ", channel name = %s, session id = %" PRIu64
,
613 stream
->chan
->session_id
);
614 stream
->opened_packet_in_current_trace_chunk
= true;
616 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
617 /* Logged by callee. */
624 stream
->opened_packet_in_current_trace_chunk
= true;
631 struct lttng_consumer_stream
*consumer_stream_create(struct lttng_consumer_channel
*channel
,
632 uint64_t channel_key
,
634 const char *channel_name
,
637 struct lttng_trace_chunk
*trace_chunk
,
640 enum consumer_channel_type type
,
641 unsigned int monitor
)
644 struct lttng_consumer_stream
*stream
;
646 stream
= zmalloc
<lttng_consumer_stream
>();
647 if (stream
== nullptr) {
648 PERROR("malloc struct lttng_consumer_stream");
655 if (trace_chunk
&& !lttng_trace_chunk_get(trace_chunk
)) {
656 ERR("Failed to acquire trace chunk reference during the creation of a stream");
661 stream
->send_node
= CDS_LIST_HEAD_INIT(stream
->send_node
);
662 stream
->chan
= channel
;
663 stream
->key
= stream_key
;
664 stream
->trace_chunk
= trace_chunk
;
666 stream
->out_fd_offset
= 0;
667 stream
->output_written
= 0;
668 stream
->net_seq_idx
= relayd_id
;
669 stream
->session_id
= session_id
;
670 stream
->monitor
= monitor
;
671 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
672 stream
->index_file
= nullptr;
673 stream
->last_sequence_number
= -1ULL;
674 stream
->rotate_position
= -1ULL;
675 /* Buffer is created with an open packet. */
676 stream
->opened_packet_in_current_trace_chunk
= true;
677 pthread_mutex_init(&stream
->lock
, nullptr);
678 pthread_mutex_init(&stream
->metadata_timer_lock
, nullptr);
680 /* If channel is the metadata, flag this stream as metadata. */
681 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
682 stream
->metadata_flag
= 1;
683 /* Metadata is flat out. */
684 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
685 /* Live rendez-vous point. */
686 pthread_cond_init(&stream
->metadata_rdv
, nullptr);
687 pthread_mutex_init(&stream
->metadata_rdv_lock
, nullptr);
689 /* Format stream name to <channel_name>_<cpu_number> */
690 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d", channel_name
, cpu
);
692 PERROR("snprintf stream name");
697 switch (channel
->output
) {
698 case CONSUMER_CHANNEL_SPLICE
:
699 stream
->output
= LTTNG_EVENT_SPLICE
;
700 ret
= utils_create_pipe(stream
->splice_pipe
);
705 case CONSUMER_CHANNEL_MMAP
:
706 stream
->output
= LTTNG_EVENT_MMAP
;
712 /* Key is always the wait_fd for streams. */
713 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
715 /* Init node per channel id key */
716 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
718 /* Init session id node with the stream session id */
719 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
721 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
" relayd_id %" PRIu64
722 ", session_id %" PRIu64
,
731 lttng_dynamic_array_init(
732 &stream
->read_subbuffer_ops
.post_consume_cbs
, sizeof(post_consume_cb
), nullptr);
734 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
735 stream
->read_subbuffer_ops
.lock
= consumer_stream_metadata_lock_all
;
736 stream
->read_subbuffer_ops
.unlock
= consumer_stream_metadata_unlock_all
;
737 stream
->read_subbuffer_ops
.assert_locked
=
738 consumer_stream_metadata_assert_locked_all
;
739 stream
->read_subbuffer_ops
.pre_consume_subbuffer
= metadata_stream_check_version
;
741 const post_consume_cb post_consume_index_op
= channel
->is_live
?
742 consumer_stream_sync_metadata_index
:
743 consumer_stream_send_index
;
744 const post_consume_cb post_consume_open_new_packet_
= post_consume_open_new_packet
;
746 ret
= lttng_dynamic_array_add_element(&stream
->read_subbuffer_ops
.post_consume_cbs
,
747 &post_consume_index_op
);
749 PERROR("Failed to add `send index` callback to stream's post consumption callbacks");
753 ret
= lttng_dynamic_array_add_element(&stream
->read_subbuffer_ops
.post_consume_cbs
,
754 &post_consume_open_new_packet_
);
756 PERROR("Failed to add `open new packet` callback to stream's post consumption callbacks");
760 stream
->read_subbuffer_ops
.lock
= consumer_stream_data_lock_all
;
761 stream
->read_subbuffer_ops
.unlock
= consumer_stream_data_unlock_all
;
762 stream
->read_subbuffer_ops
.assert_locked
= consumer_stream_data_assert_locked_all
;
763 stream
->read_subbuffer_ops
.pre_consume_subbuffer
= consumer_stream_update_stats
;
766 if (channel
->output
== CONSUMER_CHANNEL_MMAP
) {
767 stream
->read_subbuffer_ops
.consume_subbuffer
= consumer_stream_consume_mmap
;
769 stream
->read_subbuffer_ops
.consume_subbuffer
= consumer_stream_consume_splice
;
776 lttng_trace_chunk_put(stream
->trace_chunk
);
777 lttng_dynamic_array_reset(&stream
->read_subbuffer_ops
.post_consume_cbs
);
787 * Close stream on the relayd side. This call can destroy a relayd if the
788 * conditions are met.
790 * A RCU read side lock MUST be acquired if the relayd object was looked up in
791 * a hash table before calling this.
793 void consumer_stream_relayd_close(struct lttng_consumer_stream
*stream
,
794 struct consumer_relayd_sock_pair
*relayd
)
798 LTTNG_ASSERT(stream
);
799 LTTNG_ASSERT(relayd
);
801 if (stream
->sent_to_relayd
) {
802 uatomic_dec(&relayd
->refcount
);
803 LTTNG_ASSERT(uatomic_read(&relayd
->refcount
) >= 0);
806 /* Closing streams requires to lock the control socket. */
807 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
808 ret
= relayd_send_close_stream(
809 &relayd
->control_sock
, stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
810 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
812 ERR("Relayd send close stream failed. Cleaning up relayd %" PRIu64
".",
813 relayd
->net_seq_idx
);
814 lttng_consumer_cleanup_relayd(relayd
);
817 /* Both conditions are met, we destroy the relayd. */
818 if (uatomic_read(&relayd
->refcount
) == 0 && uatomic_read(&relayd
->destroy_flag
)) {
819 consumer_destroy_relayd(relayd
);
821 stream
->net_seq_idx
= (uint64_t) -1ULL;
822 stream
->sent_to_relayd
= 0;
826 * Close stream's file descriptors and, if needed, close stream also on the
829 * The consumer data lock MUST be acquired.
830 * The stream lock MUST be acquired.
832 void consumer_stream_close_output(struct lttng_consumer_stream
*stream
)
834 struct consumer_relayd_sock_pair
*relayd
;
836 LTTNG_ASSERT(stream
);
838 /* Close output fd. Could be a socket or local file at this point. */
839 if (stream
->out_fd
>= 0) {
840 const auto ret
= close(stream
->out_fd
);
842 PERROR("Failed to close stream output file descriptor");
848 if (stream
->index_file
) {
849 lttng_index_file_put(stream
->index_file
);
850 stream
->index_file
= nullptr;
853 lttng_trace_chunk_put(stream
->trace_chunk
);
854 stream
->trace_chunk
= nullptr;
856 /* Check and cleanup relayd if needed. */
858 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
859 if (relayd
!= nullptr) {
860 consumer_stream_relayd_close(stream
, relayd
);
861 stream
->net_seq_idx
= -1ULL;
868 * Delete the stream from all possible hash tables.
870 * The consumer data lock MUST be acquired.
871 * The stream lock MUST be acquired.
873 void consumer_stream_delete(struct lttng_consumer_stream
*stream
, struct lttng_ht
*ht
)
876 struct lttng_ht_iter iter
;
878 LTTNG_ASSERT(stream
);
879 /* Should NEVER be called not in monitor mode. */
880 LTTNG_ASSERT(stream
->chan
->monitor
);
885 iter
.iter
.node
= &stream
->node
.node
;
886 ret
= lttng_ht_del(ht
, &iter
);
890 /* Delete from stream per channel ID hash table. */
891 iter
.iter
.node
= &stream
->node_channel_id
.node
;
893 * The returned value is of no importance. Even if the node is NOT in the
894 * hash table, we continue since we may have been called by a code path
895 * that did not add the stream to a (all) hash table. Same goes for the
896 * next call ht del call.
898 (void) lttng_ht_del(the_consumer_data
.stream_per_chan_id_ht
, &iter
);
900 /* Delete from the global stream list. */
901 iter
.iter
.node
= &stream
->node_session_id
.node
;
902 /* See the previous ht del on why we ignore the returned value. */
903 (void) lttng_ht_del(the_consumer_data
.stream_list_ht
, &iter
);
907 if (!stream
->metadata_flag
) {
908 /* Decrement the stream count of the global consumer data. */
909 LTTNG_ASSERT(the_consumer_data
.stream_count
> 0);
910 the_consumer_data
.stream_count
--;
915 * Free the given stream within a RCU call.
917 void consumer_stream_free(struct lttng_consumer_stream
*stream
)
919 LTTNG_ASSERT(stream
);
921 metadata_bucket_destroy(stream
->metadata_bucket
);
922 call_rcu(&stream
->node
.head
, free_stream_rcu
);
926 * Destroy the stream's buffers of the tracer.
928 void consumer_stream_destroy_buffers(struct lttng_consumer_stream
*stream
)
930 LTTNG_ASSERT(stream
);
932 switch (the_consumer_data
.type
) {
933 case LTTNG_CONSUMER_KERNEL
:
934 if (stream
->mmap_base
!= nullptr) {
935 const auto ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
942 if (stream
->wait_fd
>= 0) {
943 const auto ret
= close(stream
->wait_fd
);
949 stream
->wait_fd
= -1;
952 if (stream
->chan
->output
== CONSUMER_CHANNEL_SPLICE
) {
953 utils_close_pipe(stream
->splice_pipe
);
957 case LTTNG_CONSUMER32_UST
:
958 case LTTNG_CONSUMER64_UST
:
960 * Special case for the metadata since the wait fd is an internal pipe
961 * polled in the metadata thread.
963 if (stream
->metadata_flag
&& stream
->chan
->monitor
) {
964 const auto rpipe
= stream
->ust_metadata_poll_pipe
[0];
967 * This will stop the channel timer if one and close the write side
968 * of the metadata poll pipe.
970 lttng_ustconsumer_close_metadata(stream
->chan
);
972 const auto ret
= close(rpipe
);
975 PERROR("closing metadata pipe read side");
978 stream
->ust_metadata_poll_pipe
[0] = -1;
982 lttng_ustconsumer_del_stream(stream
);
985 ERR("Unknown consumer_data type");
991 * Destroy and close a already created stream.
993 static void destroy_close_stream(struct lttng_consumer_stream
*stream
)
995 LTTNG_ASSERT(stream
);
997 DBG("Consumer stream destroy monitored key: %" PRIu64
, stream
->key
);
999 /* Destroy tracer buffers of the stream. */
1000 consumer_stream_destroy_buffers(stream
);
1001 /* Close down everything including the relayd if one. */
1002 consumer_stream_close_output(stream
);
1006 * Decrement the stream's channel refcount and if down to 0, return the channel
1007 * pointer so it can be destroyed by the caller or NULL if not.
1009 static struct lttng_consumer_channel
*unref_channel(struct lttng_consumer_stream
*stream
)
1011 struct lttng_consumer_channel
*free_chan
= nullptr;
1013 LTTNG_ASSERT(stream
);
1014 LTTNG_ASSERT(stream
->chan
);
1016 /* Update refcount of channel and see if we need to destroy it. */
1017 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1) &&
1018 !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
1019 free_chan
= stream
->chan
;
1026 * Destroy a stream completely. This will delete, close and free the stream.
1027 * Once return, the stream is NO longer usable. Its channel may get destroyed
1028 * if conditions are met for a monitored stream.
1030 * This MUST be called WITHOUT the consumer data and stream lock acquired if
1031 * the stream is in _monitor_ mode else it does not matter.
1033 void consumer_stream_destroy(struct lttng_consumer_stream
*stream
, struct lttng_ht
*ht
)
1035 LTTNG_ASSERT(stream
);
1037 cds_list_del_init(&stream
->send_node
);
1039 /* Stream is in monitor mode. */
1040 if (stream
->monitor
) {
1041 struct lttng_consumer_channel
*free_chan
= nullptr;
1044 * This means that the stream was successfully removed from the streams
1045 * list of the channel and sent to the right thread managing this
1046 * stream thus being globally visible.
1048 if (stream
->globally_visible
) {
1049 pthread_mutex_lock(&the_consumer_data
.lock
);
1050 pthread_mutex_lock(&stream
->chan
->lock
);
1052 pthread_mutex_lock(&stream
->lock
);
1053 /* Remove every reference of the stream in the consumer. */
1054 consumer_stream_delete(stream
, ht
);
1056 destroy_close_stream(stream
);
1058 /* Update channel's refcount of the stream. */
1059 free_chan
= unref_channel(stream
);
1061 /* Indicates that the consumer data state MUST be updated after this. */
1062 the_consumer_data
.need_update
= 1;
1064 pthread_mutex_unlock(&stream
->lock
);
1065 pthread_mutex_unlock(&stream
->chan
->lock
);
1066 pthread_mutex_unlock(&the_consumer_data
.lock
);
1069 * If the stream is not visible globally, this needs to be done
1070 * outside of the consumer data lock section.
1072 destroy_close_stream(stream
);
1073 free_chan
= unref_channel(stream
);
1077 consumer_del_channel(free_chan
);
1080 destroy_close_stream(stream
);
1083 /* Free stream within a RCU call. */
1084 lttng_trace_chunk_put(stream
->trace_chunk
);
1085 stream
->trace_chunk
= nullptr;
1086 lttng_dynamic_array_reset(&stream
->read_subbuffer_ops
.post_consume_cbs
);
1087 consumer_stream_free(stream
);
1091 * Write index of a specific stream either on the relayd or local disk.
1093 * Return 0 on success or else a negative value.
1095 int consumer_stream_write_index(struct lttng_consumer_stream
*stream
,
1096 struct ctf_packet_index
*element
)
1100 LTTNG_ASSERT(stream
);
1101 LTTNG_ASSERT(element
);
1104 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1105 struct consumer_relayd_sock_pair
*relayd
;
1106 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1108 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1109 ret
= relayd_send_index(&relayd
->control_sock
,
1111 stream
->relayd_stream_id
,
1112 stream
->next_net_seq_num
- 1);
1115 * Communication error with lttng-relayd,
1116 * perform cleanup now
1118 ERR("Relayd send index failed. Cleaning up relayd %" PRIu64
".",
1119 relayd
->net_seq_idx
);
1120 lttng_consumer_cleanup_relayd(relayd
);
1123 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1125 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't write index.",
1127 stream
->net_seq_idx
);
1131 if (lttng_index_file_write(stream
->index_file
, element
)) {
1146 int consumer_stream_create_output_files(struct lttng_consumer_stream
*stream
, bool create_index
)
1149 enum lttng_trace_chunk_status chunk_status
;
1150 const int flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
1151 const mode_t mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
1152 char stream_path
[LTTNG_PATH_MAX
];
1154 ASSERT_LOCKED(stream
->lock
);
1155 LTTNG_ASSERT(stream
->trace_chunk
);
1157 ret
= utils_stream_file_path(stream
->chan
->pathname
,
1159 stream
->chan
->tracefile_size
,
1160 stream
->tracefile_count_current
,
1163 sizeof(stream_path
));
1168 if (stream
->out_fd
>= 0) {
1169 ret
= close(stream
->out_fd
);
1171 PERROR("Failed to close stream file \"%s\"", stream
->name
);
1174 stream
->out_fd
= -1;
1177 DBG("Opening stream output file \"%s\"", stream_path
);
1178 chunk_status
= lttng_trace_chunk_open_file(
1179 stream
->trace_chunk
, stream_path
, flags
, mode
, &stream
->out_fd
, false);
1180 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1181 ERR("Failed to open stream file \"%s\"", stream
->name
);
1186 if (!stream
->metadata_flag
&& (create_index
|| stream
->index_file
)) {
1187 if (stream
->index_file
) {
1188 lttng_index_file_put(stream
->index_file
);
1191 lttng_index_file_create_from_trace_chunk(stream
->trace_chunk
,
1192 stream
->chan
->pathname
,
1194 stream
->chan
->tracefile_size
,
1195 stream
->tracefile_count_current
,
1199 &stream
->index_file
);
1200 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1206 /* Reset current size because we just perform a rotation. */
1207 stream
->tracefile_size_current
= 0;
1208 stream
->out_fd_offset
= 0;
1213 int consumer_stream_rotate_output_files(struct lttng_consumer_stream
*stream
)
1217 stream
->tracefile_count_current
++;
1218 if (stream
->chan
->tracefile_count
> 0) {
1219 stream
->tracefile_count_current
%= stream
->chan
->tracefile_count
;
1222 DBG("Rotating output files of stream \"%s\"", stream
->name
);
1223 ret
= consumer_stream_create_output_files(stream
, true);
1232 bool consumer_stream_is_deleted(struct lttng_consumer_stream
*stream
)
1235 * This function does not take a const stream since
1236 * cds_lfht_is_node_deleted was not const before liburcu 0.12.
1238 LTTNG_ASSERT(stream
);
1239 return cds_lfht_is_node_deleted(&stream
->node
.node
);
1242 static ssize_t
metadata_bucket_flush(const struct stream_subbuffer
*buffer
, void *data
)
1245 struct lttng_consumer_stream
*stream
= (lttng_consumer_stream
*) data
;
1247 ret
= consumer_stream_consume_mmap(nullptr, stream
, buffer
);
1255 static ssize_t
metadata_bucket_consume(struct lttng_consumer_local_data
*unused
1256 __attribute__((unused
)),
1257 struct lttng_consumer_stream
*stream
,
1258 const struct stream_subbuffer
*subbuffer
)
1261 enum metadata_bucket_status status
;
1263 status
= metadata_bucket_fill(stream
->metadata_bucket
, subbuffer
);
1265 case METADATA_BUCKET_STATUS_OK
:
1266 /* Return consumed size. */
1267 ret
= subbuffer
->buffer
.buffer
.size
;
1276 int consumer_stream_enable_metadata_bucketization(struct lttng_consumer_stream
*stream
)
1280 LTTNG_ASSERT(stream
->metadata_flag
);
1281 LTTNG_ASSERT(!stream
->metadata_bucket
);
1282 LTTNG_ASSERT(stream
->chan
->output
== CONSUMER_CHANNEL_MMAP
);
1284 stream
->metadata_bucket
= metadata_bucket_create(metadata_bucket_flush
, stream
);
1285 if (!stream
->metadata_bucket
) {
1290 stream
->read_subbuffer_ops
.consume_subbuffer
= metadata_bucket_consume
;
1295 void consumer_stream_metadata_set_version(struct lttng_consumer_stream
*stream
,
1296 uint64_t new_version
)
1298 LTTNG_ASSERT(new_version
> stream
->metadata_version
);
1299 stream
->metadata_version
= new_version
;
1300 stream
->reset_metadata_flag
= 1;
1302 if (stream
->metadata_bucket
) {
1303 metadata_bucket_reset(stream
->metadata_bucket
);
1307 int consumer_stream_flush_buffer(struct lttng_consumer_stream
*stream
, bool producer_active
)
1311 switch (the_consumer_data
.type
) {
1312 case LTTNG_CONSUMER_KERNEL
:
1313 if (producer_active
) {
1314 ret
= kernctl_buffer_flush(stream
->wait_fd
);
1316 ERR("Failed to flush kernel stream");
1320 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
1323 * Doing a buffer flush which does not take into
1324 * account empty packets. This is not perfect,
1325 * but required as a fall-back when
1326 * "flush_empty" is not implemented by
1329 ret
= kernctl_buffer_flush(stream
->wait_fd
);
1331 ERR("Failed to flush kernel stream");
1337 case LTTNG_CONSUMER32_UST
:
1338 case LTTNG_CONSUMER64_UST
:
1339 ret
= lttng_ustconsumer_flush_buffer(stream
, (int) producer_active
);
1342 ERR("Unknown consumer_data type");