2 * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca>
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
16 #include <common/common.h>
17 #include <common/consumer/consumer-timer.h>
18 #include <common/consumer/consumer.h>
19 #include <common/consumer/metadata-bucket.h>
20 #include <common/index/index.h>
21 #include <common/kernel-consumer/kernel-consumer.h>
22 #include <common/macros.h>
23 #include <common/relayd/relayd.h>
24 #include <common/ust-consumer/ust-consumer.h>
25 #include <common/utils.h>
27 #include "consumer-stream.h"
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
=
35 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
36 struct lttng_consumer_stream
*stream
=
37 caa_container_of(node
, struct lttng_consumer_stream
, node
);
39 pthread_mutex_destroy(&stream
->lock
);
43 static void consumer_stream_data_lock_all(struct lttng_consumer_stream
*stream
)
45 pthread_mutex_lock(&stream
->chan
->lock
);
46 pthread_mutex_lock(&stream
->lock
);
49 static void consumer_stream_data_unlock_all(struct lttng_consumer_stream
*stream
)
51 pthread_mutex_unlock(&stream
->lock
);
52 pthread_mutex_unlock(&stream
->chan
->lock
);
55 static void consumer_stream_data_assert_locked_all(struct lttng_consumer_stream
*stream
)
57 ASSERT_LOCKED(stream
->lock
);
58 ASSERT_LOCKED(stream
->chan
->lock
);
61 static void consumer_stream_metadata_lock_all(struct lttng_consumer_stream
*stream
)
63 consumer_stream_data_lock_all(stream
);
64 pthread_mutex_lock(&stream
->metadata_rdv_lock
);
67 static void consumer_stream_metadata_unlock_all(struct lttng_consumer_stream
*stream
)
69 pthread_mutex_unlock(&stream
->metadata_rdv_lock
);
70 consumer_stream_data_unlock_all(stream
);
73 static void consumer_stream_metadata_assert_locked_all(struct lttng_consumer_stream
*stream
)
75 ASSERT_LOCKED(stream
->metadata_rdv_lock
);
76 consumer_stream_data_assert_locked_all(stream
);
79 /* Only used for data streams. */
80 static int consumer_stream_update_stats(struct lttng_consumer_stream
*stream
,
81 const struct stream_subbuffer
*subbuf
)
84 uint64_t sequence_number
;
85 const uint64_t discarded_events
= subbuf
->info
.data
.events_discarded
;
87 if (!subbuf
->info
.data
.sequence_number
.is_set
) {
88 /* Command not supported by the tracer. */
89 sequence_number
= -1ULL;
90 stream
->sequence_number_unavailable
= true;
92 sequence_number
= subbuf
->info
.data
.sequence_number
.value
;
96 * Start the sequence when we extract the first packet in case we don't
97 * start at 0 (for example if a consumer is not connected to the
98 * session immediately after the beginning).
100 if (stream
->last_sequence_number
== -1ULL) {
101 stream
->last_sequence_number
= sequence_number
;
102 } else if (sequence_number
> stream
->last_sequence_number
) {
103 stream
->chan
->lost_packets
+= sequence_number
-
104 stream
->last_sequence_number
- 1;
106 /* seq <= last_sequence_number */
107 ERR("Sequence number inconsistent : prev = %" PRIu64
108 ", current = %" PRIu64
,
109 stream
->last_sequence_number
, sequence_number
);
113 stream
->last_sequence_number
= sequence_number
;
115 if (discarded_events
< stream
->last_discarded_events
) {
117 * Overflow has occurred. We assume only one wrap-around
120 stream
->chan
->discarded_events
+=
121 (1ULL << (CAA_BITS_PER_LONG
- 1)) -
122 stream
->last_discarded_events
+
125 stream
->chan
->discarded_events
+= discarded_events
-
126 stream
->last_discarded_events
;
128 stream
->last_discarded_events
= discarded_events
;
136 void ctf_packet_index_populate(struct ctf_packet_index
*index
,
137 off_t offset
, const struct stream_subbuffer
*subbuffer
)
139 *index
= (typeof(*index
)){
140 .offset
= htobe64(offset
),
141 .packet_size
= htobe64(subbuffer
->info
.data
.packet_size
),
142 .content_size
= htobe64(subbuffer
->info
.data
.content_size
),
143 .timestamp_begin
= htobe64(
144 subbuffer
->info
.data
.timestamp_begin
),
145 .timestamp_end
= htobe64(
146 subbuffer
->info
.data
.timestamp_end
),
147 .events_discarded
= htobe64(
148 subbuffer
->info
.data
.events_discarded
),
149 .stream_id
= htobe64(subbuffer
->info
.data
.stream_id
),
150 .stream_instance_id
= htobe64(
151 subbuffer
->info
.data
.stream_instance_id
.is_set
?
152 subbuffer
->info
.data
.stream_instance_id
.value
: -1ULL),
153 .packet_seq_num
= htobe64(
154 subbuffer
->info
.data
.sequence_number
.is_set
?
155 subbuffer
->info
.data
.sequence_number
.value
: -1ULL),
159 static ssize_t
consumer_stream_consume_mmap(
160 struct lttng_consumer_local_data
*ctx
,
161 struct lttng_consumer_stream
*stream
,
162 const struct stream_subbuffer
*subbuffer
)
164 const unsigned long padding_size
=
165 subbuffer
->info
.data
.padded_subbuf_size
-
166 subbuffer
->info
.data
.subbuf_size
;
167 const ssize_t written_bytes
= lttng_consumer_on_read_subbuffer_mmap(
168 stream
, &subbuffer
->buffer
.buffer
, padding_size
);
170 if (stream
->net_seq_idx
== -1ULL) {
172 * When writing on disk, check that only the subbuffer (no
173 * padding) was written to disk.
175 if (written_bytes
!= subbuffer
->info
.data
.padded_subbuf_size
) {
176 DBG("Failed to write the entire padded subbuffer on disk (written_bytes: %zd, padded subbuffer size %lu)",
178 subbuffer
->info
.data
.padded_subbuf_size
);
182 * When streaming over the network, check that the entire
183 * subbuffer including padding was successfully written.
185 if (written_bytes
!= subbuffer
->info
.data
.subbuf_size
) {
186 DBG("Failed to write only the subbuffer over the network (written_bytes: %zd, subbuffer size %lu)",
188 subbuffer
->info
.data
.subbuf_size
);
193 * If `lttng_consumer_on_read_subbuffer_mmap()` returned an error, pass
194 * it along to the caller, else return zero.
196 if (written_bytes
< 0) {
197 ERR("Error reading mmap subbuffer: %zd", written_bytes
);
200 return written_bytes
;
203 static ssize_t
consumer_stream_consume_splice(
204 struct lttng_consumer_local_data
*ctx
,
205 struct lttng_consumer_stream
*stream
,
206 const struct stream_subbuffer
*subbuffer
)
208 const ssize_t written_bytes
= lttng_consumer_on_read_subbuffer_splice(
209 ctx
, stream
, subbuffer
->info
.data
.padded_subbuf_size
, 0);
211 if (written_bytes
!= subbuffer
->info
.data
.padded_subbuf_size
) {
212 DBG("Failed to write the entire padded subbuffer (written_bytes: %zd, padded subbuffer size %lu)",
214 subbuffer
->info
.data
.padded_subbuf_size
);
218 * If `lttng_consumer_on_read_subbuffer_splice()` returned an error,
219 * pass it along to the caller, else return zero.
221 if (written_bytes
< 0) {
222 ERR("Error reading splice subbuffer: %zd", written_bytes
);
225 return written_bytes
;
228 static int consumer_stream_send_index(
229 struct lttng_consumer_stream
*stream
,
230 const struct stream_subbuffer
*subbuffer
,
231 struct lttng_consumer_local_data
*ctx
)
233 off_t packet_offset
= 0;
234 struct ctf_packet_index index
= {};
237 * This is called after consuming the sub-buffer; substract the
238 * effect this sub-buffer from the offset.
240 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
241 packet_offset
= stream
->out_fd_offset
-
242 subbuffer
->info
.data
.padded_subbuf_size
;
245 ctf_packet_index_populate(&index
, packet_offset
, subbuffer
);
246 return consumer_stream_write_index(stream
, &index
);
250 * Actually do the metadata sync using the given metadata stream.
252 * Return 0 on success else a negative value. ENODATA can be returned also
253 * indicating that there is no metadata available for that stream.
255 static int do_sync_metadata(struct lttng_consumer_stream
*metadata
,
256 struct lttng_consumer_local_data
*ctx
)
259 enum sync_metadata_status status
;
262 assert(metadata
->metadata_flag
);
266 * In UST, since we have to write the metadata from the cache packet
267 * by packet, we might need to start this procedure multiple times
268 * until all the metadata from the cache has been extracted.
273 * - Lock the metadata stream
274 * - Check if metadata stream node was deleted before locking.
275 * - if yes, release and return success
276 * - Check if new metadata is ready (flush + snapshot pos)
277 * - If nothing : release and return.
278 * - Lock the metadata_rdv_lock
279 * - Unlock the metadata stream
280 * - cond_wait on metadata_rdv to wait the wakeup from the
282 * - Unlock the metadata_rdv_lock
284 pthread_mutex_lock(&metadata
->lock
);
287 * There is a possibility that we were able to acquire a reference on the
288 * stream from the RCU hash table but between then and now, the node might
289 * have been deleted just before the lock is acquired. Thus, after locking,
290 * we make sure the metadata node has not been deleted which means that the
291 * buffers are closed.
293 * In that case, there is no need to sync the metadata hence returning a
294 * success return code.
296 ret
= cds_lfht_is_node_deleted(&metadata
->node
.node
);
299 goto end_unlock_mutex
;
303 case LTTNG_CONSUMER_KERNEL
:
305 * Empty the metadata cache and flush the current stream.
307 status
= lttng_kconsumer_sync_metadata(metadata
);
309 case LTTNG_CONSUMER32_UST
:
310 case LTTNG_CONSUMER64_UST
:
312 * Ask the sessiond if we have new metadata waiting and update the
313 * consumer metadata cache.
315 status
= lttng_ustconsumer_sync_metadata(ctx
, metadata
);
322 case SYNC_METADATA_STATUS_NEW_DATA
:
324 case SYNC_METADATA_STATUS_NO_DATA
:
326 goto end_unlock_mutex
;
327 case SYNC_METADATA_STATUS_ERROR
:
329 goto end_unlock_mutex
;
335 * At this point, new metadata have been flushed, so we wait on the
336 * rendez-vous point for the metadata thread to wake us up when it
337 * finishes consuming the metadata and continue execution.
340 pthread_mutex_lock(&metadata
->metadata_rdv_lock
);
343 * Release metadata stream lock so the metadata thread can process it.
345 pthread_mutex_unlock(&metadata
->lock
);
348 * Wait on the rendez-vous point. Once woken up, it means the metadata was
349 * consumed and thus synchronization is achieved.
351 pthread_cond_wait(&metadata
->metadata_rdv
, &metadata
->metadata_rdv_lock
);
352 pthread_mutex_unlock(&metadata
->metadata_rdv_lock
);
353 } while (status
== SYNC_METADATA_STATUS_NEW_DATA
);
359 pthread_mutex_unlock(&metadata
->lock
);
364 * Synchronize the metadata using a given session ID. A successful acquisition
365 * of a metadata stream will trigger a request to the session daemon and a
366 * snapshot so the metadata thread can consume it.
368 * This function call is a rendez-vous point between the metadata thread and
371 * Return 0 on success or else a negative value.
373 int consumer_stream_sync_metadata(struct lttng_consumer_local_data
*ctx
,
377 struct lttng_consumer_stream
*stream
= NULL
;
378 struct lttng_ht_iter iter
;
383 /* Ease our life a bit. */
384 ht
= consumer_data
.stream_list_ht
;
388 /* Search the metadata associated with the session id of the given stream. */
390 cds_lfht_for_each_entry_duplicate(ht
->ht
,
391 ht
->hash_fct(&session_id
, lttng_ht_seed
), ht
->match_fct
,
392 &session_id
, &iter
.iter
, stream
, node_session_id
.node
) {
393 if (!stream
->metadata_flag
) {
397 ret
= do_sync_metadata(stream
, ctx
);
404 * Force return code to 0 (success) since ret might be ENODATA for instance
405 * which is not an error but rather that we should come back.
414 static int consumer_stream_sync_metadata_index(
415 struct lttng_consumer_stream
*stream
,
416 const struct stream_subbuffer
*subbuffer
,
417 struct lttng_consumer_local_data
*ctx
)
421 /* Block until all the metadata is sent. */
422 pthread_mutex_lock(&stream
->metadata_timer_lock
);
423 assert(!stream
->missed_metadata_flush
);
424 stream
->waiting_on_metadata
= true;
425 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
427 ret
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
429 pthread_mutex_lock(&stream
->metadata_timer_lock
);
430 stream
->waiting_on_metadata
= false;
431 if (stream
->missed_metadata_flush
) {
432 stream
->missed_metadata_flush
= false;
433 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
434 (void) stream
->read_subbuffer_ops
.send_live_beacon(stream
);
436 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
442 ret
= consumer_stream_send_index(stream
, subbuffer
, ctx
);
448 * Check if the local version of the metadata stream matches with the version
449 * of the metadata stream in the kernel. If it was updated, set the reset flag
453 int metadata_stream_check_version(struct lttng_consumer_stream
*stream
,
454 const struct stream_subbuffer
*subbuffer
)
456 if (stream
->metadata_version
== subbuffer
->info
.metadata
.version
) {
460 DBG("New metadata version detected");
461 consumer_stream_metadata_set_version(stream
,
462 subbuffer
->info
.metadata
.version
);
464 if (stream
->read_subbuffer_ops
.reset_metadata
) {
465 stream
->read_subbuffer_ops
.reset_metadata(stream
);
472 struct lttng_consumer_stream
*consumer_stream_create(
473 struct lttng_consumer_channel
*channel
,
474 uint64_t channel_key
,
476 const char *channel_name
,
479 struct lttng_trace_chunk
*trace_chunk
,
482 enum consumer_channel_type type
,
483 unsigned int monitor
)
486 struct lttng_consumer_stream
*stream
;
488 stream
= zmalloc(sizeof(*stream
));
489 if (stream
== NULL
) {
490 PERROR("malloc struct lttng_consumer_stream");
497 if (trace_chunk
&& !lttng_trace_chunk_get(trace_chunk
)) {
498 ERR("Failed to acquire trace chunk reference during the creation of a stream");
503 stream
->chan
= channel
;
504 stream
->key
= stream_key
;
505 stream
->trace_chunk
= trace_chunk
;
507 stream
->out_fd_offset
= 0;
508 stream
->output_written
= 0;
509 stream
->net_seq_idx
= relayd_id
;
510 stream
->session_id
= session_id
;
511 stream
->monitor
= monitor
;
512 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
513 stream
->index_file
= NULL
;
514 stream
->last_sequence_number
= -1ULL;
515 stream
->rotate_position
= -1ULL;
516 /* Buffer is created with an open packet. */
517 stream
->opened_packet_in_current_trace_chunk
= true;
518 pthread_mutex_init(&stream
->lock
, NULL
);
519 pthread_mutex_init(&stream
->metadata_timer_lock
, NULL
);
521 /* If channel is the metadata, flag this stream as metadata. */
522 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
523 stream
->metadata_flag
= 1;
524 /* Metadata is flat out. */
525 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
526 /* Live rendez-vous point. */
527 pthread_cond_init(&stream
->metadata_rdv
, NULL
);
528 pthread_mutex_init(&stream
->metadata_rdv_lock
, NULL
);
530 /* Format stream name to <channel_name>_<cpu_number> */
531 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
534 PERROR("snprintf stream name");
539 switch (channel
->output
) {
540 case CONSUMER_CHANNEL_SPLICE
:
541 stream
->output
= LTTNG_EVENT_SPLICE
;
542 ret
= utils_create_pipe(stream
->splice_pipe
);
547 case CONSUMER_CHANNEL_MMAP
:
548 stream
->output
= LTTNG_EVENT_MMAP
;
554 /* Key is always the wait_fd for streams. */
555 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
557 /* Init node per channel id key */
558 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
560 /* Init session id node with the stream session id */
561 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
563 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
564 " relayd_id %" PRIu64
", session_id %" PRIu64
,
565 stream
->name
, stream
->key
, channel_key
,
566 stream
->net_seq_idx
, stream
->session_id
);
570 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
571 stream
->read_subbuffer_ops
.lock
=
572 consumer_stream_metadata_lock_all
;
573 stream
->read_subbuffer_ops
.unlock
=
574 consumer_stream_metadata_unlock_all
;
575 stream
->read_subbuffer_ops
.assert_locked
=
576 consumer_stream_metadata_assert_locked_all
;
577 stream
->read_subbuffer_ops
.pre_consume_subbuffer
=
578 metadata_stream_check_version
;
580 stream
->read_subbuffer_ops
.lock
= consumer_stream_data_lock_all
;
581 stream
->read_subbuffer_ops
.unlock
=
582 consumer_stream_data_unlock_all
;
583 stream
->read_subbuffer_ops
.assert_locked
=
584 consumer_stream_data_assert_locked_all
;
585 stream
->read_subbuffer_ops
.pre_consume_subbuffer
=
586 consumer_stream_update_stats
;
587 if (channel
->is_live
) {
588 stream
->read_subbuffer_ops
.post_consume
=
589 consumer_stream_sync_metadata_index
;
591 stream
->read_subbuffer_ops
.post_consume
=
592 consumer_stream_send_index
;
596 if (channel
->output
== CONSUMER_CHANNEL_MMAP
) {
597 stream
->read_subbuffer_ops
.consume_subbuffer
=
598 consumer_stream_consume_mmap
;
600 stream
->read_subbuffer_ops
.consume_subbuffer
=
601 consumer_stream_consume_splice
;
608 lttng_trace_chunk_put(stream
->trace_chunk
);
618 * Close stream on the relayd side. This call can destroy a relayd if the
619 * conditions are met.
621 * A RCU read side lock MUST be acquired if the relayd object was looked up in
622 * a hash table before calling this.
624 void consumer_stream_relayd_close(struct lttng_consumer_stream
*stream
,
625 struct consumer_relayd_sock_pair
*relayd
)
632 if (stream
->sent_to_relayd
) {
633 uatomic_dec(&relayd
->refcount
);
634 assert(uatomic_read(&relayd
->refcount
) >= 0);
637 /* Closing streams requires to lock the control socket. */
638 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
639 ret
= relayd_send_close_stream(&relayd
->control_sock
,
640 stream
->relayd_stream_id
,
641 stream
->next_net_seq_num
- 1);
642 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
644 ERR("Relayd send close stream failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
645 lttng_consumer_cleanup_relayd(relayd
);
648 /* Both conditions are met, we destroy the relayd. */
649 if (uatomic_read(&relayd
->refcount
) == 0 &&
650 uatomic_read(&relayd
->destroy_flag
)) {
651 consumer_destroy_relayd(relayd
);
653 stream
->net_seq_idx
= (uint64_t) -1ULL;
654 stream
->sent_to_relayd
= 0;
658 * Close stream's file descriptors and, if needed, close stream also on the
661 * The consumer data lock MUST be acquired.
662 * The stream lock MUST be acquired.
664 void consumer_stream_close(struct lttng_consumer_stream
*stream
)
667 struct consumer_relayd_sock_pair
*relayd
;
671 switch (consumer_data
.type
) {
672 case LTTNG_CONSUMER_KERNEL
:
673 if (stream
->mmap_base
!= NULL
) {
674 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
680 if (stream
->wait_fd
>= 0) {
681 ret
= close(stream
->wait_fd
);
685 stream
->wait_fd
= -1;
687 if (stream
->chan
->output
== CONSUMER_CHANNEL_SPLICE
) {
688 utils_close_pipe(stream
->splice_pipe
);
691 case LTTNG_CONSUMER32_UST
:
692 case LTTNG_CONSUMER64_UST
:
695 * Special case for the metadata since the wait fd is an internal pipe
696 * polled in the metadata thread.
698 if (stream
->metadata_flag
&& stream
->chan
->monitor
) {
699 int rpipe
= stream
->ust_metadata_poll_pipe
[0];
702 * This will stop the channel timer if one and close the write side
703 * of the metadata poll pipe.
705 lttng_ustconsumer_close_metadata(stream
->chan
);
709 PERROR("closing metadata pipe read side");
711 stream
->ust_metadata_poll_pipe
[0] = -1;
717 ERR("Unknown consumer_data type");
721 /* Close output fd. Could be a socket or local file at this point. */
722 if (stream
->out_fd
>= 0) {
723 ret
= close(stream
->out_fd
);
730 if (stream
->index_file
) {
731 lttng_index_file_put(stream
->index_file
);
732 stream
->index_file
= NULL
;
735 lttng_trace_chunk_put(stream
->trace_chunk
);
736 stream
->trace_chunk
= NULL
;
738 /* Check and cleanup relayd if needed. */
740 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
741 if (relayd
!= NULL
) {
742 consumer_stream_relayd_close(stream
, relayd
);
748 * Delete the stream from all possible hash tables.
750 * The consumer data lock MUST be acquired.
751 * The stream lock MUST be acquired.
753 void consumer_stream_delete(struct lttng_consumer_stream
*stream
,
757 struct lttng_ht_iter iter
;
760 /* Should NEVER be called not in monitor mode. */
761 assert(stream
->chan
->monitor
);
766 iter
.iter
.node
= &stream
->node
.node
;
767 ret
= lttng_ht_del(ht
, &iter
);
771 /* Delete from stream per channel ID hash table. */
772 iter
.iter
.node
= &stream
->node_channel_id
.node
;
774 * The returned value is of no importance. Even if the node is NOT in the
775 * hash table, we continue since we may have been called by a code path
776 * that did not add the stream to a (all) hash table. Same goes for the
777 * next call ht del call.
779 (void) lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
781 /* Delete from the global stream list. */
782 iter
.iter
.node
= &stream
->node_session_id
.node
;
783 /* See the previous ht del on why we ignore the returned value. */
784 (void) lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
788 if (!stream
->metadata_flag
) {
789 /* Decrement the stream count of the global consumer data. */
790 assert(consumer_data
.stream_count
> 0);
791 consumer_data
.stream_count
--;
796 * Free the given stream within a RCU call.
798 void consumer_stream_free(struct lttng_consumer_stream
*stream
)
802 metadata_bucket_destroy(stream
->metadata_bucket
);
803 call_rcu(&stream
->node
.head
, free_stream_rcu
);
807 * Destroy the stream's buffers of the tracer.
809 void consumer_stream_destroy_buffers(struct lttng_consumer_stream
*stream
)
813 switch (consumer_data
.type
) {
814 case LTTNG_CONSUMER_KERNEL
:
816 case LTTNG_CONSUMER32_UST
:
817 case LTTNG_CONSUMER64_UST
:
818 lttng_ustconsumer_del_stream(stream
);
821 ERR("Unknown consumer_data type");
827 * Destroy and close a already created stream.
829 static void destroy_close_stream(struct lttng_consumer_stream
*stream
)
833 DBG("Consumer stream destroy monitored key: %" PRIu64
, stream
->key
);
835 /* Destroy tracer buffers of the stream. */
836 consumer_stream_destroy_buffers(stream
);
837 /* Close down everything including the relayd if one. */
838 consumer_stream_close(stream
);
842 * Decrement the stream's channel refcount and if down to 0, return the channel
843 * pointer so it can be destroyed by the caller or NULL if not.
845 static struct lttng_consumer_channel
*unref_channel(
846 struct lttng_consumer_stream
*stream
)
848 struct lttng_consumer_channel
*free_chan
= NULL
;
851 assert(stream
->chan
);
853 /* Update refcount of channel and see if we need to destroy it. */
854 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
855 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
856 free_chan
= stream
->chan
;
863 * Destroy a stream completely. This will delete, close and free the stream.
864 * Once return, the stream is NO longer usable. Its channel may get destroyed
865 * if conditions are met for a monitored stream.
867 * This MUST be called WITHOUT the consumer data and stream lock acquired if
868 * the stream is in _monitor_ mode else it does not matter.
870 void consumer_stream_destroy(struct lttng_consumer_stream
*stream
,
875 /* Stream is in monitor mode. */
876 if (stream
->monitor
) {
877 struct lttng_consumer_channel
*free_chan
= NULL
;
880 * This means that the stream was successfully removed from the streams
881 * list of the channel and sent to the right thread managing this
882 * stream thus being globally visible.
884 if (stream
->globally_visible
) {
885 pthread_mutex_lock(&consumer_data
.lock
);
886 pthread_mutex_lock(&stream
->chan
->lock
);
887 pthread_mutex_lock(&stream
->lock
);
888 /* Remove every reference of the stream in the consumer. */
889 consumer_stream_delete(stream
, ht
);
891 destroy_close_stream(stream
);
893 /* Update channel's refcount of the stream. */
894 free_chan
= unref_channel(stream
);
896 /* Indicates that the consumer data state MUST be updated after this. */
897 consumer_data
.need_update
= 1;
899 pthread_mutex_unlock(&stream
->lock
);
900 pthread_mutex_unlock(&stream
->chan
->lock
);
901 pthread_mutex_unlock(&consumer_data
.lock
);
904 * If the stream is not visible globally, this needs to be done
905 * outside of the consumer data lock section.
907 free_chan
= unref_channel(stream
);
911 consumer_del_channel(free_chan
);
914 destroy_close_stream(stream
);
917 /* Free stream within a RCU call. */
918 lttng_trace_chunk_put(stream
->trace_chunk
);
919 stream
->trace_chunk
= NULL
;
920 consumer_stream_free(stream
);
924 * Write index of a specific stream either on the relayd or local disk.
926 * Return 0 on success or else a negative value.
928 int consumer_stream_write_index(struct lttng_consumer_stream
*stream
,
929 struct ctf_packet_index
*element
)
937 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
938 struct consumer_relayd_sock_pair
*relayd
;
939 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
941 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
942 ret
= relayd_send_index(&relayd
->control_sock
, element
,
943 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
946 * Communication error with lttng-relayd,
947 * perform cleanup now
949 ERR("Relayd send index failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
950 lttng_consumer_cleanup_relayd(relayd
);
953 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
955 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't write index.",
956 stream
->key
, stream
->net_seq_idx
);
960 if (lttng_index_file_write(stream
->index_file
, element
)) {
975 int consumer_stream_create_output_files(struct lttng_consumer_stream
*stream
,
979 enum lttng_trace_chunk_status chunk_status
;
980 const int flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
981 const mode_t mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
982 char stream_path
[LTTNG_PATH_MAX
];
984 ASSERT_LOCKED(stream
->lock
);
985 assert(stream
->trace_chunk
);
987 ret
= utils_stream_file_path(stream
->chan
->pathname
, stream
->name
,
988 stream
->chan
->tracefile_size
,
989 stream
->tracefile_count_current
, NULL
,
990 stream_path
, sizeof(stream_path
));
995 if (stream
->out_fd
>= 0) {
996 ret
= close(stream
->out_fd
);
998 PERROR("Failed to close stream file \"%s\"",
1002 stream
->out_fd
= -1;
1005 DBG("Opening stream output file \"%s\"", stream_path
);
1006 chunk_status
= lttng_trace_chunk_open_file(stream
->trace_chunk
, stream_path
,
1007 flags
, mode
, &stream
->out_fd
, false);
1008 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1009 ERR("Failed to open stream file \"%s\"", stream
->name
);
1014 if (!stream
->metadata_flag
&& (create_index
|| stream
->index_file
)) {
1015 if (stream
->index_file
) {
1016 lttng_index_file_put(stream
->index_file
);
1018 chunk_status
= lttng_index_file_create_from_trace_chunk(
1019 stream
->trace_chunk
,
1020 stream
->chan
->pathname
,
1022 stream
->chan
->tracefile_size
,
1023 stream
->tracefile_count_current
,
1024 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
,
1025 false, &stream
->index_file
);
1026 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1032 /* Reset current size because we just perform a rotation. */
1033 stream
->tracefile_size_current
= 0;
1034 stream
->out_fd_offset
= 0;
1039 int consumer_stream_rotate_output_files(struct lttng_consumer_stream
*stream
)
1043 stream
->tracefile_count_current
++;
1044 if (stream
->chan
->tracefile_count
> 0) {
1045 stream
->tracefile_count_current
%=
1046 stream
->chan
->tracefile_count
;
1049 DBG("Rotating output files of stream \"%s\"", stream
->name
);
1050 ret
= consumer_stream_create_output_files(stream
, true);
1059 bool consumer_stream_is_deleted(struct lttng_consumer_stream
*stream
)
1062 * This function does not take a const stream since
1063 * cds_lfht_is_node_deleted was not const before liburcu 0.12.
1066 return cds_lfht_is_node_deleted(&stream
->node
.node
);
1069 static ssize_t
metadata_bucket_flush(
1070 const struct stream_subbuffer
*buffer
, void *data
)
1073 struct lttng_consumer_stream
*stream
= data
;
1075 ret
= consumer_stream_consume_mmap(NULL
, stream
, buffer
);
1083 static ssize_t
metadata_bucket_consume(
1084 struct lttng_consumer_local_data
*unused
,
1085 struct lttng_consumer_stream
*stream
,
1086 const struct stream_subbuffer
*subbuffer
)
1089 enum metadata_bucket_status status
;
1091 status
= metadata_bucket_fill(stream
->metadata_bucket
, subbuffer
);
1093 case METADATA_BUCKET_STATUS_OK
:
1094 /* Return consumed size. */
1095 ret
= subbuffer
->buffer
.buffer
.size
;
1104 int consumer_stream_enable_metadata_bucketization(
1105 struct lttng_consumer_stream
*stream
)
1109 assert(stream
->metadata_flag
);
1110 assert(!stream
->metadata_bucket
);
1111 assert(stream
->chan
->output
== CONSUMER_CHANNEL_MMAP
);
1113 stream
->metadata_bucket
= metadata_bucket_create(
1114 metadata_bucket_flush
, stream
);
1115 if (!stream
->metadata_bucket
) {
1120 stream
->read_subbuffer_ops
.consume_subbuffer
= metadata_bucket_consume
;
1125 void consumer_stream_metadata_set_version(
1126 struct lttng_consumer_stream
*stream
, uint64_t new_version
)
1128 assert(new_version
> stream
->metadata_version
);
1129 stream
->metadata_version
= new_version
;
1130 stream
->reset_metadata_flag
= 1;
1132 if (stream
->metadata_bucket
) {
1133 metadata_bucket_reset(stream
->metadata_bucket
);