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
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
)
419 bool missed_metadata_flush
;
422 /* Block until all the metadata is sent. */
423 pthread_mutex_lock(&stream
->metadata_timer_lock
);
424 assert(!stream
->missed_metadata_flush
);
425 stream
->waiting_on_metadata
= true;
426 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
428 ret
= consumer_stream_sync_metadata(ctx
, stream
->session_id
);
430 pthread_mutex_lock(&stream
->metadata_timer_lock
);
431 stream
->waiting_on_metadata
= false;
432 missed_metadata_flush
= stream
->missed_metadata_flush
;
433 if (missed_metadata_flush
) {
434 stream
->missed_metadata_flush
= false;
436 pthread_mutex_unlock(&stream
->metadata_timer_lock
);
441 ret
= consumer_stream_send_index(stream
, subbuffer
, ctx
);
443 * Send the live inactivity beacon to handle the situation where
444 * the live timer is prevented from sampling this stream
445 * because the stream lock was being held while this stream is
446 * waiting on metadata. This ensures live viewer progress in the
447 * unlikely scenario where a live timer would be prevented from
448 * locking a stream lock repeatedly due to a steady flow of
449 * incoming metadata, for a stream which is mostly inactive.
451 * It is important to send the inactivity beacon packet to
452 * relayd _after_ sending the index associated with the data
453 * that was just sent, otherwise this can cause live viewers to
454 * observe timestamps going backwards between an inactivity
455 * beacon and a following trace packet.
457 if (missed_metadata_flush
) {
458 (void) stream
->read_subbuffer_ops
.send_live_beacon(stream
);
465 * Check if the local version of the metadata stream matches with the version
466 * of the metadata stream in the kernel. If it was updated, set the reset flag
470 int metadata_stream_check_version(struct lttng_consumer_stream
*stream
,
471 const struct stream_subbuffer
*subbuffer
)
473 if (stream
->metadata_version
== subbuffer
->info
.metadata
.version
) {
477 DBG("New metadata version detected");
478 consumer_stream_metadata_set_version(stream
,
479 subbuffer
->info
.metadata
.version
);
481 if (stream
->read_subbuffer_ops
.reset_metadata
) {
482 stream
->read_subbuffer_ops
.reset_metadata(stream
);
489 struct lttng_consumer_stream
*consumer_stream_create(
490 struct lttng_consumer_channel
*channel
,
491 uint64_t channel_key
,
493 const char *channel_name
,
496 struct lttng_trace_chunk
*trace_chunk
,
499 enum consumer_channel_type type
,
500 unsigned int monitor
)
503 struct lttng_consumer_stream
*stream
;
505 stream
= zmalloc(sizeof(*stream
));
506 if (stream
== NULL
) {
507 PERROR("malloc struct lttng_consumer_stream");
514 if (trace_chunk
&& !lttng_trace_chunk_get(trace_chunk
)) {
515 ERR("Failed to acquire trace chunk reference during the creation of a stream");
520 stream
->chan
= channel
;
521 stream
->key
= stream_key
;
522 stream
->trace_chunk
= trace_chunk
;
524 stream
->out_fd_offset
= 0;
525 stream
->output_written
= 0;
526 stream
->net_seq_idx
= relayd_id
;
527 stream
->session_id
= session_id
;
528 stream
->monitor
= monitor
;
529 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
530 stream
->index_file
= NULL
;
531 stream
->last_sequence_number
= -1ULL;
532 stream
->rotate_position
= -1ULL;
533 /* Buffer is created with an open packet. */
534 stream
->opened_packet_in_current_trace_chunk
= true;
535 pthread_mutex_init(&stream
->lock
, NULL
);
536 pthread_mutex_init(&stream
->metadata_timer_lock
, NULL
);
538 /* If channel is the metadata, flag this stream as metadata. */
539 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
540 stream
->metadata_flag
= 1;
541 /* Metadata is flat out. */
542 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
543 /* Live rendez-vous point. */
544 pthread_cond_init(&stream
->metadata_rdv
, NULL
);
545 pthread_mutex_init(&stream
->metadata_rdv_lock
, NULL
);
547 /* Format stream name to <channel_name>_<cpu_number> */
548 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
551 PERROR("snprintf stream name");
556 switch (channel
->output
) {
557 case CONSUMER_CHANNEL_SPLICE
:
558 stream
->output
= LTTNG_EVENT_SPLICE
;
559 ret
= utils_create_pipe(stream
->splice_pipe
);
564 case CONSUMER_CHANNEL_MMAP
:
565 stream
->output
= LTTNG_EVENT_MMAP
;
571 /* Key is always the wait_fd for streams. */
572 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
574 /* Init node per channel id key */
575 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
577 /* Init session id node with the stream session id */
578 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
580 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
581 " relayd_id %" PRIu64
", session_id %" PRIu64
,
582 stream
->name
, stream
->key
, channel_key
,
583 stream
->net_seq_idx
, stream
->session_id
);
587 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
588 stream
->read_subbuffer_ops
.lock
=
589 consumer_stream_metadata_lock_all
;
590 stream
->read_subbuffer_ops
.unlock
=
591 consumer_stream_metadata_unlock_all
;
592 stream
->read_subbuffer_ops
.assert_locked
=
593 consumer_stream_metadata_assert_locked_all
;
594 stream
->read_subbuffer_ops
.pre_consume_subbuffer
=
595 metadata_stream_check_version
;
597 stream
->read_subbuffer_ops
.lock
= consumer_stream_data_lock_all
;
598 stream
->read_subbuffer_ops
.unlock
=
599 consumer_stream_data_unlock_all
;
600 stream
->read_subbuffer_ops
.assert_locked
=
601 consumer_stream_data_assert_locked_all
;
602 stream
->read_subbuffer_ops
.pre_consume_subbuffer
=
603 consumer_stream_update_stats
;
604 if (channel
->is_live
) {
605 stream
->read_subbuffer_ops
.post_consume
=
606 consumer_stream_sync_metadata_index
;
608 stream
->read_subbuffer_ops
.post_consume
=
609 consumer_stream_send_index
;
613 if (channel
->output
== CONSUMER_CHANNEL_MMAP
) {
614 stream
->read_subbuffer_ops
.consume_subbuffer
=
615 consumer_stream_consume_mmap
;
617 stream
->read_subbuffer_ops
.consume_subbuffer
=
618 consumer_stream_consume_splice
;
625 lttng_trace_chunk_put(stream
->trace_chunk
);
635 * Close stream on the relayd side. This call can destroy a relayd if the
636 * conditions are met.
638 * A RCU read side lock MUST be acquired if the relayd object was looked up in
639 * a hash table before calling this.
641 void consumer_stream_relayd_close(struct lttng_consumer_stream
*stream
,
642 struct consumer_relayd_sock_pair
*relayd
)
649 if (stream
->sent_to_relayd
) {
650 uatomic_dec(&relayd
->refcount
);
651 assert(uatomic_read(&relayd
->refcount
) >= 0);
654 /* Closing streams requires to lock the control socket. */
655 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
656 ret
= relayd_send_close_stream(&relayd
->control_sock
,
657 stream
->relayd_stream_id
,
658 stream
->next_net_seq_num
- 1);
659 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
661 ERR("Relayd send close stream failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
662 lttng_consumer_cleanup_relayd(relayd
);
665 /* Both conditions are met, we destroy the relayd. */
666 if (uatomic_read(&relayd
->refcount
) == 0 &&
667 uatomic_read(&relayd
->destroy_flag
)) {
668 consumer_destroy_relayd(relayd
);
670 stream
->net_seq_idx
= (uint64_t) -1ULL;
671 stream
->sent_to_relayd
= 0;
675 * Close stream's file descriptors and, if needed, close stream also on the
678 * The consumer data lock MUST be acquired.
679 * The stream lock MUST be acquired.
681 void consumer_stream_close(struct lttng_consumer_stream
*stream
)
684 struct consumer_relayd_sock_pair
*relayd
;
688 switch (consumer_data
.type
) {
689 case LTTNG_CONSUMER_KERNEL
:
690 if (stream
->mmap_base
!= NULL
) {
691 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
697 if (stream
->wait_fd
>= 0) {
698 ret
= close(stream
->wait_fd
);
702 stream
->wait_fd
= -1;
704 if (stream
->chan
->output
== CONSUMER_CHANNEL_SPLICE
) {
705 utils_close_pipe(stream
->splice_pipe
);
708 case LTTNG_CONSUMER32_UST
:
709 case LTTNG_CONSUMER64_UST
:
712 * Special case for the metadata since the wait fd is an internal pipe
713 * polled in the metadata thread.
715 if (stream
->metadata_flag
&& stream
->chan
->monitor
) {
716 int rpipe
= stream
->ust_metadata_poll_pipe
[0];
719 * This will stop the channel timer if one and close the write side
720 * of the metadata poll pipe.
722 lttng_ustconsumer_close_metadata(stream
->chan
);
726 PERROR("closing metadata pipe read side");
728 stream
->ust_metadata_poll_pipe
[0] = -1;
734 ERR("Unknown consumer_data type");
738 /* Close output fd. Could be a socket or local file at this point. */
739 if (stream
->out_fd
>= 0) {
740 ret
= close(stream
->out_fd
);
747 if (stream
->index_file
) {
748 lttng_index_file_put(stream
->index_file
);
749 stream
->index_file
= NULL
;
752 lttng_trace_chunk_put(stream
->trace_chunk
);
753 stream
->trace_chunk
= NULL
;
755 /* Check and cleanup relayd if needed. */
757 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
758 if (relayd
!= NULL
) {
759 consumer_stream_relayd_close(stream
, relayd
);
765 * Delete the stream from all possible hash tables.
767 * The consumer data lock MUST be acquired.
768 * The stream lock MUST be acquired.
770 void consumer_stream_delete(struct lttng_consumer_stream
*stream
,
774 struct lttng_ht_iter iter
;
777 /* Should NEVER be called not in monitor mode. */
778 assert(stream
->chan
->monitor
);
783 iter
.iter
.node
= &stream
->node
.node
;
784 ret
= lttng_ht_del(ht
, &iter
);
788 /* Delete from stream per channel ID hash table. */
789 iter
.iter
.node
= &stream
->node_channel_id
.node
;
791 * The returned value is of no importance. Even if the node is NOT in the
792 * hash table, we continue since we may have been called by a code path
793 * that did not add the stream to a (all) hash table. Same goes for the
794 * next call ht del call.
796 (void) lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
798 /* Delete from the global stream list. */
799 iter
.iter
.node
= &stream
->node_session_id
.node
;
800 /* See the previous ht del on why we ignore the returned value. */
801 (void) lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
805 if (!stream
->metadata_flag
) {
806 /* Decrement the stream count of the global consumer data. */
807 assert(consumer_data
.stream_count
> 0);
808 consumer_data
.stream_count
--;
813 * Free the given stream within a RCU call.
815 void consumer_stream_free(struct lttng_consumer_stream
*stream
)
819 metadata_bucket_destroy(stream
->metadata_bucket
);
820 call_rcu(&stream
->node
.head
, free_stream_rcu
);
824 * Destroy the stream's buffers of the tracer.
826 void consumer_stream_destroy_buffers(struct lttng_consumer_stream
*stream
)
830 switch (consumer_data
.type
) {
831 case LTTNG_CONSUMER_KERNEL
:
833 case LTTNG_CONSUMER32_UST
:
834 case LTTNG_CONSUMER64_UST
:
835 lttng_ustconsumer_del_stream(stream
);
838 ERR("Unknown consumer_data type");
844 * Destroy and close a already created stream.
846 static void destroy_close_stream(struct lttng_consumer_stream
*stream
)
850 DBG("Consumer stream destroy monitored key: %" PRIu64
, stream
->key
);
852 /* Destroy tracer buffers of the stream. */
853 consumer_stream_destroy_buffers(stream
);
854 /* Close down everything including the relayd if one. */
855 consumer_stream_close(stream
);
859 * Decrement the stream's channel refcount and if down to 0, return the channel
860 * pointer so it can be destroyed by the caller or NULL if not.
862 static struct lttng_consumer_channel
*unref_channel(
863 struct lttng_consumer_stream
*stream
)
865 struct lttng_consumer_channel
*free_chan
= NULL
;
868 assert(stream
->chan
);
870 /* Update refcount of channel and see if we need to destroy it. */
871 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
872 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
873 free_chan
= stream
->chan
;
880 * Destroy a stream completely. This will delete, close and free the stream.
881 * Once return, the stream is NO longer usable. Its channel may get destroyed
882 * if conditions are met for a monitored stream.
884 * This MUST be called WITHOUT the consumer data and stream lock acquired if
885 * the stream is in _monitor_ mode else it does not matter.
887 void consumer_stream_destroy(struct lttng_consumer_stream
*stream
,
892 /* Stream is in monitor mode. */
893 if (stream
->monitor
) {
894 struct lttng_consumer_channel
*free_chan
= NULL
;
897 * This means that the stream was successfully removed from the streams
898 * list of the channel and sent to the right thread managing this
899 * stream thus being globally visible.
901 if (stream
->globally_visible
) {
902 pthread_mutex_lock(&consumer_data
.lock
);
903 pthread_mutex_lock(&stream
->chan
->lock
);
904 pthread_mutex_lock(&stream
->lock
);
905 /* Remove every reference of the stream in the consumer. */
906 consumer_stream_delete(stream
, ht
);
908 destroy_close_stream(stream
);
910 /* Update channel's refcount of the stream. */
911 free_chan
= unref_channel(stream
);
913 /* Indicates that the consumer data state MUST be updated after this. */
914 consumer_data
.need_update
= 1;
916 pthread_mutex_unlock(&stream
->lock
);
917 pthread_mutex_unlock(&stream
->chan
->lock
);
918 pthread_mutex_unlock(&consumer_data
.lock
);
921 * If the stream is not visible globally, this needs to be done
922 * outside of the consumer data lock section.
924 destroy_close_stream(stream
);
925 free_chan
= unref_channel(stream
);
929 consumer_del_channel(free_chan
);
932 destroy_close_stream(stream
);
935 /* Free stream within a RCU call. */
936 lttng_trace_chunk_put(stream
->trace_chunk
);
937 stream
->trace_chunk
= NULL
;
938 consumer_stream_free(stream
);
942 * Write index of a specific stream either on the relayd or local disk.
944 * Return 0 on success or else a negative value.
946 int consumer_stream_write_index(struct lttng_consumer_stream
*stream
,
947 struct ctf_packet_index
*element
)
955 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
956 struct consumer_relayd_sock_pair
*relayd
;
957 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
959 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
960 ret
= relayd_send_index(&relayd
->control_sock
, element
,
961 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
964 * Communication error with lttng-relayd,
965 * perform cleanup now
967 ERR("Relayd send index failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
968 lttng_consumer_cleanup_relayd(relayd
);
971 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
973 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't write index.",
974 stream
->key
, stream
->net_seq_idx
);
978 if (lttng_index_file_write(stream
->index_file
, element
)) {
993 int consumer_stream_create_output_files(struct lttng_consumer_stream
*stream
,
997 enum lttng_trace_chunk_status chunk_status
;
998 const int flags
= O_WRONLY
| O_CREAT
| O_TRUNC
;
999 const mode_t mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
1000 char stream_path
[LTTNG_PATH_MAX
];
1002 ASSERT_LOCKED(stream
->lock
);
1003 assert(stream
->trace_chunk
);
1005 ret
= utils_stream_file_path(stream
->chan
->pathname
, stream
->name
,
1006 stream
->chan
->tracefile_size
,
1007 stream
->tracefile_count_current
, NULL
,
1008 stream_path
, sizeof(stream_path
));
1013 if (stream
->out_fd
>= 0) {
1014 ret
= close(stream
->out_fd
);
1016 PERROR("Failed to close stream file \"%s\"",
1020 stream
->out_fd
= -1;
1023 DBG("Opening stream output file \"%s\"", stream_path
);
1024 chunk_status
= lttng_trace_chunk_open_file(stream
->trace_chunk
, stream_path
,
1025 flags
, mode
, &stream
->out_fd
, false);
1026 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1027 ERR("Failed to open stream file \"%s\"", stream
->name
);
1032 if (!stream
->metadata_flag
&& (create_index
|| stream
->index_file
)) {
1033 if (stream
->index_file
) {
1034 lttng_index_file_put(stream
->index_file
);
1036 chunk_status
= lttng_index_file_create_from_trace_chunk(
1037 stream
->trace_chunk
,
1038 stream
->chan
->pathname
,
1040 stream
->chan
->tracefile_size
,
1041 stream
->tracefile_count_current
,
1042 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
,
1043 false, &stream
->index_file
);
1044 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1050 /* Reset current size because we just perform a rotation. */
1051 stream
->tracefile_size_current
= 0;
1052 stream
->out_fd_offset
= 0;
1057 int consumer_stream_rotate_output_files(struct lttng_consumer_stream
*stream
)
1061 stream
->tracefile_count_current
++;
1062 if (stream
->chan
->tracefile_count
> 0) {
1063 stream
->tracefile_count_current
%=
1064 stream
->chan
->tracefile_count
;
1067 DBG("Rotating output files of stream \"%s\"", stream
->name
);
1068 ret
= consumer_stream_create_output_files(stream
, true);
1077 bool consumer_stream_is_deleted(struct lttng_consumer_stream
*stream
)
1080 * This function does not take a const stream since
1081 * cds_lfht_is_node_deleted was not const before liburcu 0.12.
1084 return cds_lfht_is_node_deleted(&stream
->node
.node
);
1087 static ssize_t
metadata_bucket_flush(
1088 const struct stream_subbuffer
*buffer
, void *data
)
1091 struct lttng_consumer_stream
*stream
= data
;
1093 ret
= consumer_stream_consume_mmap(NULL
, stream
, buffer
);
1101 static ssize_t
metadata_bucket_consume(
1102 struct lttng_consumer_local_data
*unused
,
1103 struct lttng_consumer_stream
*stream
,
1104 const struct stream_subbuffer
*subbuffer
)
1107 enum metadata_bucket_status status
;
1109 status
= metadata_bucket_fill(stream
->metadata_bucket
, subbuffer
);
1111 case METADATA_BUCKET_STATUS_OK
:
1112 /* Return consumed size. */
1113 ret
= subbuffer
->buffer
.buffer
.size
;
1122 int consumer_stream_enable_metadata_bucketization(
1123 struct lttng_consumer_stream
*stream
)
1127 assert(stream
->metadata_flag
);
1128 assert(!stream
->metadata_bucket
);
1129 assert(stream
->chan
->output
== CONSUMER_CHANNEL_MMAP
);
1131 stream
->metadata_bucket
= metadata_bucket_create(
1132 metadata_bucket_flush
, stream
);
1133 if (!stream
->metadata_bucket
) {
1138 stream
->read_subbuffer_ops
.consume_subbuffer
= metadata_bucket_consume
;
1143 void consumer_stream_metadata_set_version(
1144 struct lttng_consumer_stream
*stream
, uint64_t new_version
)
1146 assert(new_version
> stream
->metadata_version
);
1147 stream
->metadata_version
= new_version
;
1148 stream
->reset_metadata_flag
= 1;
1150 if (stream
->metadata_bucket
) {
1151 metadata_bucket_reset(stream
->metadata_bucket
);