2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
11 #include <common/align.hpp>
12 #include <common/common.hpp>
13 #include <common/compat/endian.hpp>
14 #include <common/compat/poll.hpp>
15 #include <common/consumer/consumer-metadata-cache.hpp>
16 #include <common/consumer/consumer-stream.hpp>
17 #include <common/consumer/consumer-testpoint.hpp>
18 #include <common/consumer/consumer-timer.hpp>
19 #include <common/consumer/consumer.hpp>
20 #include <common/dynamic-array.hpp>
21 #include <common/index/ctf-index.hpp>
22 #include <common/index/index.hpp>
23 #include <common/io-hint.hpp>
24 #include <common/kernel-consumer/kernel-consumer.hpp>
25 #include <common/kernel-ctl/kernel-ctl.hpp>
26 #include <common/relayd/relayd.hpp>
27 #include <common/sessiond-comm/relayd.hpp>
28 #include <common/sessiond-comm/sessiond-comm.hpp>
29 #include <common/string-utils/format.hpp>
30 #include <common/time.hpp>
31 #include <common/trace-chunk-registry.hpp>
32 #include <common/trace-chunk.hpp>
33 #include <common/urcu.hpp>
34 #include <common/ust-consumer/ust-consumer.hpp>
35 #include <common/utils.hpp>
37 #include <bin/lttng-consumerd/health-consumerd.hpp>
46 #include <sys/socket.h>
47 #include <sys/types.h>
48 #include <type_traits>
51 lttng_consumer_global_data the_consumer_data
;
53 enum consumer_channel_action
{
56 CONSUMER_CHANNEL_QUIT
,
60 struct consumer_channel_msg
{
61 enum consumer_channel_action action
;
62 struct lttng_consumer_channel
*chan
; /* add */
63 uint64_t key
; /* del */
67 * Global hash table containing respectively metadata and data streams. The
68 * stream element in this ht should only be updated by the metadata poll thread
69 * for the metadata and the data poll thread for the data.
71 struct lttng_ht
*metadata_ht
;
72 struct lttng_ht
*data_ht
;
75 /* Flag used to temporarily pause data consumption from testpoints. */
76 int data_consumption_paused
;
79 * Flag to inform the polling thread to quit when all fd hung up. Updated by
80 * the consumer_thread_receive_fds when it notices that all fds has hung up.
81 * Also updated by the signal handler (consumer_should_exit()). Read by the
86 static const char *get_consumer_domain()
88 switch (the_consumer_data
.type
) {
89 case LTTNG_CONSUMER_KERNEL
:
90 return DEFAULT_KERNEL_TRACE_DIR
;
91 case LTTNG_CONSUMER64_UST
:
93 case LTTNG_CONSUMER32_UST
:
94 return DEFAULT_UST_TRACE_DIR
;
101 * Notify a thread lttng pipe to poll back again. This usually means that some
102 * global state has changed so we just send back the thread in a poll wait
105 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
107 struct lttng_consumer_stream
*null_stream
= nullptr;
111 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
)); /* NOLINT sizeof used on a
115 static void notify_health_quit_pipe(int *pipe
)
119 ret
= lttng_write(pipe
[1], "4", 1);
121 PERROR("write consumer health quit");
125 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
126 struct lttng_consumer_channel
*chan
,
128 enum consumer_channel_action action
)
130 struct consumer_channel_msg msg
;
133 memset(&msg
, 0, sizeof(msg
));
138 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
139 if (ret
< sizeof(msg
)) {
140 PERROR("notify_channel_pipe write error");
144 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
146 notify_channel_pipe(ctx
, nullptr, key
, CONSUMER_CHANNEL_DEL
);
149 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
150 struct lttng_consumer_channel
**chan
,
152 enum consumer_channel_action
*action
)
154 struct consumer_channel_msg msg
;
157 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
158 if (ret
< sizeof(msg
)) {
162 *action
= msg
.action
;
170 * Cleanup the stream list of a channel. Those streams are not yet globally
173 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
175 struct lttng_consumer_stream
*stream
, *stmp
;
177 LTTNG_ASSERT(channel
);
179 /* Delete streams that might have been left in the stream list. */
180 cds_list_for_each_entry_safe (stream
, stmp
, &channel
->streams
.head
, send_node
) {
181 consumer_stream_destroy(stream
, nullptr);
186 * Find a stream. The consumer_data.lock must be locked during this
189 static struct lttng_consumer_stream
*find_stream(uint64_t key
, struct lttng_ht
*ht
)
191 struct lttng_ht_iter iter
;
192 struct lttng_ht_node_u64
*node
;
193 struct lttng_consumer_stream
*stream
= nullptr;
197 /* -1ULL keys are lookup failures */
198 if (key
== (uint64_t) -1ULL) {
202 const lttng::urcu::read_lock_guard read_lock
;
204 lttng_ht_lookup(ht
, &key
, &iter
);
205 node
= lttng_ht_iter_get_node
<lttng_ht_node_u64
>(&iter
);
206 if (node
!= nullptr) {
207 stream
= lttng::utils::container_of(node
, <tng_consumer_stream::node
);
213 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
215 struct lttng_consumer_stream
*stream
;
217 const lttng::urcu::read_lock_guard read_lock
;
218 stream
= find_stream(key
, ht
);
220 stream
->key
= (uint64_t) -1ULL;
222 * We don't want the lookup to match, but we still need
223 * to iterate on this stream when iterating over the hash table. Just
224 * change the node key.
226 stream
->node
.key
= (uint64_t) -1ULL;
231 * Return a channel object for the given key.
233 * RCU read side lock MUST be acquired before calling this function and
234 * protects the channel ptr.
236 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
238 struct lttng_ht_iter iter
;
239 struct lttng_ht_node_u64
*node
;
240 struct lttng_consumer_channel
*channel
= nullptr;
242 ASSERT_RCU_READ_LOCKED();
244 /* -1ULL keys are lookup failures */
245 if (key
== (uint64_t) -1ULL) {
249 lttng_ht_lookup(the_consumer_data
.channel_ht
, &key
, &iter
);
250 node
= lttng_ht_iter_get_node
<lttng_ht_node_u64
>(&iter
);
251 if (node
!= nullptr) {
252 channel
= lttng::utils::container_of(node
, <tng_consumer_channel::node
);
259 * There is a possibility that the consumer does not have enough time between
260 * the close of the channel on the session daemon and the cleanup in here thus
261 * once we have a channel add with an existing key, we know for sure that this
262 * channel will eventually get cleaned up by all streams being closed.
264 * This function just nullifies the already existing channel key.
266 static void steal_channel_key(uint64_t key
)
268 struct lttng_consumer_channel
*channel
;
270 const lttng::urcu::read_lock_guard read_lock
;
271 channel
= consumer_find_channel(key
);
273 channel
->key
= (uint64_t) -1ULL;
275 * We don't want the lookup to match, but we still need to iterate on
276 * this channel when iterating over the hash table. Just change the
279 channel
->node
.key
= (uint64_t) -1ULL;
283 static void free_channel_rcu(struct rcu_head
*head
)
285 struct lttng_ht_node_u64
*node
= lttng::utils::container_of(head
, <tng_ht_node_u64::head
);
286 struct lttng_consumer_channel
*channel
=
287 lttng::utils::container_of(node
, <tng_consumer_channel::node
);
289 switch (the_consumer_data
.type
) {
290 case LTTNG_CONSUMER_KERNEL
:
292 case LTTNG_CONSUMER32_UST
:
293 case LTTNG_CONSUMER64_UST
:
294 lttng_ustconsumer_free_channel(channel
);
297 ERR("Unknown consumer_data type");
305 * RCU protected relayd socket pair free.
307 static void free_relayd_rcu(struct rcu_head
*head
)
309 struct lttng_ht_node_u64
*node
= lttng::utils::container_of(head
, <tng_ht_node_u64::head
);
310 struct consumer_relayd_sock_pair
*relayd
=
311 lttng::utils::container_of(node
, &consumer_relayd_sock_pair::node
);
314 * Close all sockets. This is done in the call RCU since we don't want the
315 * socket fds to be reassigned thus potentially creating bad state of the
318 * We do not have to lock the control socket mutex here since at this stage
319 * there is no one referencing to this relayd object.
321 (void) relayd_close(&relayd
->control_sock
);
322 (void) relayd_close(&relayd
->data_sock
);
324 pthread_mutex_destroy(&relayd
->ctrl_sock_mutex
);
329 * Destroy and free relayd socket pair object.
331 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
334 struct lttng_ht_iter iter
;
336 if (relayd
== nullptr) {
340 DBG("Consumer destroy and close relayd socket pair");
342 iter
.iter
.node
= &relayd
->node
.node
;
343 ret
= lttng_ht_del(the_consumer_data
.relayd_ht
, &iter
);
345 /* We assume the relayd is being or is destroyed */
349 /* RCU free() call */
350 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
354 * Remove a channel from the global list protected by a mutex. This function is
355 * also responsible for freeing its data structures.
357 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
359 struct lttng_ht_iter iter
;
361 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
363 pthread_mutex_lock(&the_consumer_data
.lock
);
364 pthread_mutex_lock(&channel
->lock
);
366 /* Destroy streams that might have been left in the stream list. */
367 clean_channel_stream_list(channel
);
369 if (channel
->live_timer_enabled
== 1) {
370 consumer_timer_live_stop(channel
);
372 if (channel
->monitor_timer_enabled
== 1) {
373 consumer_timer_monitor_stop(channel
);
377 * Send a last buffer statistics sample to the session daemon
378 * to ensure it tracks the amount of data consumed by this channel.
380 sample_and_send_channel_buffer_stats(channel
);
382 switch (the_consumer_data
.type
) {
383 case LTTNG_CONSUMER_KERNEL
:
385 case LTTNG_CONSUMER32_UST
:
386 case LTTNG_CONSUMER64_UST
:
387 lttng_ustconsumer_del_channel(channel
);
390 ERR("Unknown consumer_data type");
395 lttng_trace_chunk_put(channel
->trace_chunk
);
396 channel
->trace_chunk
= nullptr;
398 if (channel
->is_published
) {
401 const lttng::urcu::read_lock_guard read_lock
;
402 iter
.iter
.node
= &channel
->node
.node
;
403 ret
= lttng_ht_del(the_consumer_data
.channel_ht
, &iter
);
406 iter
.iter
.node
= &channel
->channels_by_session_id_ht_node
.node
;
407 ret
= lttng_ht_del(the_consumer_data
.channels_by_session_id_ht
, &iter
);
411 channel
->is_deleted
= true;
412 call_rcu(&channel
->node
.head
, free_channel_rcu
);
414 pthread_mutex_unlock(&channel
->lock
);
415 pthread_mutex_unlock(&the_consumer_data
.lock
);
419 * Iterate over the relayd hash table and destroy each element. Finally,
420 * destroy the whole hash table.
422 static void cleanup_relayd_ht()
424 struct lttng_ht_iter iter
;
425 struct consumer_relayd_sock_pair
*relayd
;
428 const lttng::urcu::read_lock_guard read_lock
;
430 cds_lfht_for_each_entry (
431 the_consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
, node
.node
) {
432 consumer_destroy_relayd(relayd
);
436 lttng_ht_destroy(the_consumer_data
.relayd_ht
);
440 * Update the end point status of all streams having the given network sequence
441 * index (relayd index).
443 * It's atomically set without having the stream mutex locked which is fine
444 * because we handle the write/read race with a pipe wakeup for each thread.
446 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
447 enum consumer_endpoint_status status
)
449 struct lttng_ht_iter iter
;
450 struct lttng_consumer_stream
*stream
;
452 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
454 const lttng::urcu::read_lock_guard read_lock
;
456 /* Let's begin with metadata */
457 cds_lfht_for_each_entry (metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
458 if (stream
->net_seq_idx
== net_seq_idx
) {
459 uatomic_set(&stream
->endpoint_status
, status
);
460 stream
->chan
->metadata_pushed_wait_queue
.wake_all();
462 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
466 /* Follow up by the data streams */
467 cds_lfht_for_each_entry (data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
468 if (stream
->net_seq_idx
== net_seq_idx
) {
469 uatomic_set(&stream
->endpoint_status
, status
);
470 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
476 * Cleanup a relayd object by flagging every associated streams for deletion,
477 * destroying the object meaning removing it from the relayd hash table,
478 * closing the sockets and freeing the memory in a RCU call.
480 * If a local data context is available, notify the threads that the streams'
481 * state have changed.
483 void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
)
487 LTTNG_ASSERT(relayd
);
489 DBG("Cleaning up relayd object ID %" PRIu64
, relayd
->net_seq_idx
);
491 /* Save the net sequence index before destroying the object */
492 netidx
= relayd
->net_seq_idx
;
495 * Delete the relayd from the relayd hash table, close the sockets and free
496 * the object in a RCU call.
498 consumer_destroy_relayd(relayd
);
500 /* Set inactive endpoint to all streams */
501 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
504 * With a local data context, notify the threads that the streams' state
505 * have changed. The write() action on the pipe acts as an "implicit"
506 * memory barrier ordering the updates of the end point status from the
507 * read of this status which happens AFTER receiving this notify.
509 notify_thread_lttng_pipe(relayd
->ctx
->consumer_data_pipe
);
510 notify_thread_lttng_pipe(relayd
->ctx
->consumer_metadata_pipe
);
514 * Flag a relayd socket pair for destruction. Destroy it if the refcount
517 * RCU read side lock MUST be aquired before calling this function.
519 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
521 LTTNG_ASSERT(relayd
);
522 ASSERT_RCU_READ_LOCKED();
524 /* Set destroy flag for this object */
525 uatomic_set(&relayd
->destroy_flag
, 1);
527 /* Destroy the relayd if refcount is 0 */
528 if (uatomic_read(&relayd
->refcount
) == 0) {
529 consumer_destroy_relayd(relayd
);
534 * Completly destroy stream from every visiable data structure and the given
537 * One this call returns, the stream object is not longer usable nor visible.
539 void consumer_del_stream(struct lttng_consumer_stream
*stream
, struct lttng_ht
*ht
)
541 consumer_stream_destroy(stream
, ht
);
545 * XXX naming of del vs destroy is all mixed up.
547 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
549 consumer_stream_destroy(stream
, data_ht
);
552 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
554 consumer_stream_destroy(stream
, metadata_ht
);
557 void consumer_stream_update_channel_attributes(struct lttng_consumer_stream
*stream
,
558 struct lttng_consumer_channel
*channel
)
560 stream
->channel_read_only_attributes
.tracefile_size
= channel
->tracefile_size
;
564 * Add a stream to the global list protected by a mutex.
566 void consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
568 struct lttng_ht
*ht
= data_ht
;
570 LTTNG_ASSERT(stream
);
573 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
575 pthread_mutex_lock(&the_consumer_data
.lock
);
576 pthread_mutex_lock(&stream
->chan
->lock
);
577 pthread_mutex_lock(&stream
->chan
->timer_lock
);
578 pthread_mutex_lock(&stream
->lock
);
579 const lttng::urcu::read_lock_guard read_lock
;
581 /* Steal stream identifier to avoid having streams with the same key */
582 steal_stream_key(stream
->key
, ht
);
584 lttng_ht_add_unique_u64(ht
, &stream
->node
);
586 lttng_ht_add_u64(the_consumer_data
.stream_per_chan_id_ht
, &stream
->node_channel_id
);
589 * Add stream to the stream_list_ht of the consumer data. No need to steal
590 * the key since the HT does not use it and we allow to add redundant keys
593 lttng_ht_add_u64(the_consumer_data
.stream_list_ht
, &stream
->node_session_id
);
596 * When nb_init_stream_left reaches 0, we don't need to trigger any action
597 * in terms of destroying the associated channel, because the action that
598 * causes the count to become 0 also causes a stream to be added. The
599 * channel deletion will thus be triggered by the following removal of this
602 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
603 /* Increment refcount before decrementing nb_init_stream_left */
605 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
608 /* Update consumer data once the node is inserted. */
609 the_consumer_data
.stream_count
++;
610 the_consumer_data
.need_update
= 1;
612 pthread_mutex_unlock(&stream
->lock
);
613 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
614 pthread_mutex_unlock(&stream
->chan
->lock
);
615 pthread_mutex_unlock(&the_consumer_data
.lock
);
619 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
620 * be acquired before calling this.
622 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
625 struct lttng_ht_node_u64
*node
;
626 struct lttng_ht_iter iter
;
628 LTTNG_ASSERT(relayd
);
629 ASSERT_RCU_READ_LOCKED();
631 lttng_ht_lookup(the_consumer_data
.relayd_ht
, &relayd
->net_seq_idx
, &iter
);
632 node
= lttng_ht_iter_get_node
<lttng_ht_node_u64
>(&iter
);
633 if (node
!= nullptr) {
636 lttng_ht_add_unique_u64(the_consumer_data
.relayd_ht
, &relayd
->node
);
643 * Allocate and return a consumer relayd socket.
645 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(uint64_t net_seq_idx
)
647 struct consumer_relayd_sock_pair
*obj
= nullptr;
649 /* net sequence index of -1 is a failure */
650 if (net_seq_idx
== (uint64_t) -1ULL) {
654 obj
= zmalloc
<consumer_relayd_sock_pair
>();
655 if (obj
== nullptr) {
656 PERROR("zmalloc relayd sock");
660 obj
->net_seq_idx
= net_seq_idx
;
662 obj
->destroy_flag
= 0;
663 obj
->control_sock
.sock
.fd
= -1;
664 obj
->data_sock
.sock
.fd
= -1;
665 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
666 pthread_mutex_init(&obj
->ctrl_sock_mutex
, nullptr);
673 * Find a relayd socket pair in the global consumer data.
675 * Return the object if found else NULL.
676 * RCU read-side lock must be held across this call and while using the
679 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
681 struct lttng_ht_iter iter
;
682 struct lttng_ht_node_u64
*node
;
683 struct consumer_relayd_sock_pair
*relayd
= nullptr;
685 ASSERT_RCU_READ_LOCKED();
687 /* Negative keys are lookup failures */
688 if (key
== (uint64_t) -1ULL) {
692 lttng_ht_lookup(the_consumer_data
.relayd_ht
, &key
, &iter
);
693 node
= lttng_ht_iter_get_node
<lttng_ht_node_u64
>(&iter
);
694 if (node
!= nullptr) {
695 relayd
= lttng::utils::container_of(node
, &consumer_relayd_sock_pair::node
);
703 * Find a relayd and send the stream
705 * Returns 0 on success, < 0 on error
707 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
, char *path
)
710 struct consumer_relayd_sock_pair
*relayd
;
712 LTTNG_ASSERT(stream
);
713 LTTNG_ASSERT(stream
->net_seq_idx
!= -1ULL);
716 /* The stream is not metadata. Get relayd reference if exists. */
717 const lttng::urcu::read_lock_guard read_lock
;
718 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
719 if (relayd
!= nullptr) {
720 /* Add stream on the relayd */
721 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
722 ret
= relayd_add_stream(&relayd
->control_sock
,
724 get_consumer_domain(),
726 &stream
->relayd_stream_id
,
727 stream
->chan
->tracefile_size
,
728 stream
->chan
->tracefile_count
,
729 stream
->trace_chunk
);
730 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
732 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".",
733 relayd
->net_seq_idx
);
734 lttng_consumer_cleanup_relayd(relayd
);
738 uatomic_inc(&relayd
->refcount
);
739 stream
->sent_to_relayd
= 1;
741 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
743 stream
->net_seq_idx
);
748 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
751 stream
->net_seq_idx
);
758 * Find a relayd and send the streams sent message
760 * Returns 0 on success, < 0 on error
762 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
765 struct consumer_relayd_sock_pair
*relayd
;
767 LTTNG_ASSERT(net_seq_idx
!= -1ULL);
769 /* The stream is not metadata. Get relayd reference if exists. */
770 const lttng::urcu::read_lock_guard read_lock
;
771 relayd
= consumer_find_relayd(net_seq_idx
);
772 if (relayd
!= nullptr) {
773 /* Add stream on the relayd */
774 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
775 ret
= relayd_streams_sent(&relayd
->control_sock
);
776 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
778 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64
".",
779 relayd
->net_seq_idx
);
780 lttng_consumer_cleanup_relayd(relayd
);
784 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.", net_seq_idx
);
790 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
797 * Find a relayd and close the stream
799 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
801 struct consumer_relayd_sock_pair
*relayd
;
803 /* The stream is not metadata. Get relayd reference if exists. */
804 const lttng::urcu::read_lock_guard read_lock
;
805 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
807 consumer_stream_relayd_close(stream
, relayd
);
812 * Handle stream for relayd transmission if the stream applies for network
813 * streaming where the net sequence index is set.
815 * Return destination file descriptor or negative value on error.
817 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
819 unsigned long padding
,
820 struct consumer_relayd_sock_pair
*relayd
)
823 struct lttcomm_relayd_data_hdr data_hdr
;
826 LTTNG_ASSERT(stream
);
827 LTTNG_ASSERT(relayd
);
829 /* Reset data header */
830 memset(&data_hdr
, 0, sizeof(data_hdr
));
832 if (stream
->metadata_flag
) {
833 /* Caller MUST acquire the relayd control socket lock */
834 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
839 /* Metadata are always sent on the control socket. */
840 outfd
= relayd
->control_sock
.sock
.fd
;
842 /* Set header with stream information */
843 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
844 data_hdr
.data_size
= htobe32(data_size
);
845 data_hdr
.padding_size
= htobe32(padding
);
848 * Note that net_seq_num below is assigned with the *current* value of
849 * next_net_seq_num and only after that the next_net_seq_num will be
850 * increment. This is why when issuing a command on the relayd using
851 * this next value, 1 should always be substracted in order to compare
852 * the last seen sequence number on the relayd side to the last sent.
854 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
855 /* Other fields are zeroed previously */
857 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
, sizeof(data_hdr
));
862 ++stream
->next_net_seq_num
;
864 /* Set to go on data socket */
865 outfd
= relayd
->data_sock
.sock
.fd
;
873 * Write a character on the metadata poll pipe to wake the metadata thread.
874 * Returns 0 on success, -1 on error.
876 int consumer_metadata_wakeup_pipe(const struct lttng_consumer_channel
*channel
)
880 DBG("Waking up metadata poll thread (writing to pipe): channel name = '%s'", channel
->name
);
881 if (channel
->monitor
&& channel
->metadata_stream
) {
882 const char dummy
= 'c';
883 const ssize_t write_ret
=
884 lttng_write(channel
->metadata_stream
->ust_metadata_poll_pipe
[1], &dummy
, 1);
887 if (errno
== EWOULDBLOCK
) {
889 * This is fine, the metadata poll thread
890 * is having a hard time keeping-up, but
891 * it will eventually wake-up and consume
892 * the available data.
896 PERROR("Failed to write to UST metadata pipe while attempting to wake-up the metadata poll thread");
908 * Trigger a dump of the metadata content. Following/during the succesful
909 * completion of this call, the metadata poll thread will start receiving
910 * metadata packets to consume.
912 * The caller must hold the channel and stream locks.
914 static int consumer_metadata_stream_dump(struct lttng_consumer_stream
*stream
)
918 ASSERT_LOCKED(stream
->chan
->lock
);
919 ASSERT_LOCKED(stream
->lock
);
920 LTTNG_ASSERT(stream
->metadata_flag
);
921 LTTNG_ASSERT(stream
->chan
->trace_chunk
);
923 switch (the_consumer_data
.type
) {
924 case LTTNG_CONSUMER_KERNEL
:
926 * Reset the position of what has been read from the
927 * metadata cache to 0 so we can dump it again.
929 ret
= kernctl_metadata_cache_dump(stream
->wait_fd
);
931 case LTTNG_CONSUMER32_UST
:
932 case LTTNG_CONSUMER64_UST
:
934 * Reset the position pushed from the metadata cache so it
935 * will write from the beginning on the next push.
937 stream
->ust_metadata_pushed
= 0;
938 ret
= consumer_metadata_wakeup_pipe(stream
->chan
);
941 ERR("Unknown consumer_data type");
945 ERR("Failed to dump the metadata cache");
950 static int lttng_consumer_channel_set_trace_chunk(struct lttng_consumer_channel
*channel
,
951 struct lttng_trace_chunk
*new_trace_chunk
)
953 pthread_mutex_lock(&channel
->lock
);
954 if (channel
->is_deleted
) {
956 * The channel has been logically deleted and should no longer
957 * be used. It has released its reference to its current trace
958 * chunk and should not acquire a new one.
960 * Return success as there is nothing for the caller to do.
966 * The acquisition of the reference cannot fail (barring
967 * a severe internal error) since a reference to the published
968 * chunk is already held by the caller.
970 if (new_trace_chunk
) {
971 const bool acquired_reference
= lttng_trace_chunk_get(new_trace_chunk
);
973 LTTNG_ASSERT(acquired_reference
);
976 lttng_trace_chunk_put(channel
->trace_chunk
);
977 channel
->trace_chunk
= new_trace_chunk
;
979 pthread_mutex_unlock(&channel
->lock
);
984 * Allocate and return a new lttng_consumer_channel object using the given key
985 * to initialize the hash table node.
987 * On error, return NULL.
989 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
991 const uint64_t *chunk_id
,
992 const char *pathname
,
995 enum lttng_event_output output
,
996 uint64_t tracefile_size
,
997 uint64_t tracefile_count
,
998 uint64_t session_id_per_pid
,
999 unsigned int monitor
,
1000 unsigned int live_timer_interval
,
1001 bool is_in_live_session
,
1002 const char *root_shm_path
,
1003 const char *shm_path
)
1005 struct lttng_consumer_channel
*channel
= nullptr;
1006 struct lttng_trace_chunk
*trace_chunk
= nullptr;
1009 trace_chunk
= lttng_trace_chunk_registry_find_chunk(
1010 the_consumer_data
.chunk_registry
, session_id
, *chunk_id
);
1012 ERR("Failed to find trace chunk reference during creation of channel");
1018 channel
= new lttng_consumer_channel
;
1019 } catch (const std::bad_alloc
& e
) {
1020 ERR("Failed to allocate lttng_consumer_channel: %s", e
.what());
1026 channel
->refcount
= 0;
1027 channel
->session_id
= session_id
;
1028 channel
->session_id_per_pid
= session_id_per_pid
;
1029 channel
->relayd_id
= relayd_id
;
1030 channel
->tracefile_size
= tracefile_size
;
1031 channel
->tracefile_count
= tracefile_count
;
1032 channel
->monitor
= monitor
;
1033 channel
->live_timer_interval
= live_timer_interval
;
1034 channel
->is_live
= is_in_live_session
;
1035 pthread_mutex_init(&channel
->lock
, nullptr);
1036 pthread_mutex_init(&channel
->timer_lock
, nullptr);
1039 case LTTNG_EVENT_SPLICE
:
1040 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
1042 case LTTNG_EVENT_MMAP
:
1043 channel
->output
= CONSUMER_CHANNEL_MMAP
;
1053 * In monitor mode, the streams associated with the channel will be put in
1054 * a special list ONLY owned by this channel. So, the refcount is set to 1
1055 * here meaning that the channel itself has streams that are referenced.
1057 * On a channel deletion, once the channel is no longer visible, the
1058 * refcount is decremented and checked for a zero value to delete it. With
1059 * streams in no monitor mode, it will now be safe to destroy the channel.
1061 if (!channel
->monitor
) {
1062 channel
->refcount
= 1;
1065 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1066 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1068 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1069 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1071 if (root_shm_path
) {
1072 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1073 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1076 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1077 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1080 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1081 lttng_ht_node_init_u64(&channel
->channels_by_session_id_ht_node
, channel
->session_id
);
1083 channel
->wait_fd
= -1;
1084 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1087 const int ret
= lttng_consumer_channel_set_trace_chunk(channel
, trace_chunk
);
1093 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1096 lttng_trace_chunk_put(trace_chunk
);
1099 consumer_del_channel(channel
);
1105 * Add a channel to the global list protected by a mutex.
1107 * Always return 0 indicating success.
1109 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1110 struct lttng_consumer_local_data
*ctx
)
1112 pthread_mutex_lock(&the_consumer_data
.lock
);
1113 pthread_mutex_lock(&channel
->lock
);
1114 pthread_mutex_lock(&channel
->timer_lock
);
1117 * This gives us a guarantee that the channel we are about to add to the
1118 * channel hash table will be unique. See this function comment on the why
1119 * we need to steel the channel key at this stage.
1121 steal_channel_key(channel
->key
);
1123 const lttng::urcu::read_lock_guard read_lock
;
1124 lttng_ht_add_unique_u64(the_consumer_data
.channel_ht
, &channel
->node
);
1125 lttng_ht_add_u64(the_consumer_data
.channels_by_session_id_ht
,
1126 &channel
->channels_by_session_id_ht_node
);
1127 channel
->is_published
= true;
1129 pthread_mutex_unlock(&channel
->timer_lock
);
1130 pthread_mutex_unlock(&channel
->lock
);
1131 pthread_mutex_unlock(&the_consumer_data
.lock
);
1133 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1134 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1141 * Allocate the pollfd structure and the local view of the out fds to avoid
1142 * doing a lookup in the linked list and concurrency issues when writing is
1143 * needed. Called with consumer_data.lock held.
1145 * Returns the number of fds in the structures.
1147 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1148 struct pollfd
**pollfd
,
1149 struct lttng_consumer_stream
**local_stream
,
1150 struct lttng_ht
*ht
,
1151 int *nb_inactive_fd
)
1154 struct lttng_ht_iter iter
;
1155 struct lttng_consumer_stream
*stream
;
1159 LTTNG_ASSERT(pollfd
);
1160 LTTNG_ASSERT(local_stream
);
1162 DBG("Updating poll fd array");
1163 *nb_inactive_fd
= 0;
1166 const lttng::urcu::read_lock_guard read_lock
;
1167 cds_lfht_for_each_entry (ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1169 * Only active streams with an active end point can be added to the
1170 * poll set and local stream storage of the thread.
1172 * There is a potential race here for endpoint_status to be updated
1173 * just after the check. However, this is OK since the stream(s) will
1174 * be deleted once the thread is notified that the end point state has
1175 * changed where this function will be called back again.
1177 * We track the number of inactive FDs because they still need to be
1178 * closed by the polling thread after a wakeup on the data_pipe or
1181 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1182 (*nb_inactive_fd
)++;
1186 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1187 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1188 local_stream
[i
] = stream
;
1194 * Insert the consumer_data_pipe at the end of the array and don't
1195 * increment i so nb_fd is the number of real FD.
1197 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1198 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1200 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1201 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1206 * Poll on the should_quit pipe and the command socket return -1 on
1207 * error, 1 if should exit, 0 if data is available on the command socket
1209 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1214 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1215 if (num_rdy
== -1) {
1217 * Restart interrupted system call.
1219 if (errno
== EINTR
) {
1222 PERROR("Poll error");
1225 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1226 DBG("consumer_should_quit wake up");
1233 * Set the error socket.
1235 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
, int sock
)
1237 ctx
->consumer_error_socket
= sock
;
1241 * Set the command socket path.
1243 void lttng_consumer_set_command_sock_path(struct lttng_consumer_local_data
*ctx
, char *sock
)
1245 ctx
->consumer_command_sock_path
= sock
;
1249 * Send return code to the session daemon.
1250 * If the socket is not defined, we return 0, it is not a fatal error
1252 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
,
1253 enum lttcomm_return_code error_code
)
1255 if (ctx
->consumer_error_socket
> 0) {
1256 const std::int32_t comm_code
= std::int32_t(error_code
);
1259 sizeof(comm_code
) >= sizeof(std::underlying_type
<lttcomm_return_code
>),
1260 "Fixed-size communication type too small to accomodate lttcomm_return_code");
1261 return lttcomm_send_unix_sock(
1262 ctx
->consumer_error_socket
, &comm_code
, sizeof(comm_code
));
1269 * Close all the tracefiles and stream fds and MUST be called when all
1270 * instances are destroyed i.e. when all threads were joined and are ended.
1272 void lttng_consumer_cleanup()
1274 struct lttng_ht_iter iter
;
1275 struct lttng_consumer_channel
*channel
;
1276 unsigned int trace_chunks_left
;
1279 const lttng::urcu::read_lock_guard read_lock
;
1281 cds_lfht_for_each_entry (
1282 the_consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
, node
.node
) {
1283 consumer_del_channel(channel
);
1287 lttng_ht_destroy(the_consumer_data
.channel_ht
);
1288 lttng_ht_destroy(the_consumer_data
.channels_by_session_id_ht
);
1290 cleanup_relayd_ht();
1292 lttng_ht_destroy(the_consumer_data
.stream_per_chan_id_ht
);
1295 * This HT contains streams that are freed by either the metadata thread or
1296 * the data thread so we do *nothing* on the hash table and simply destroy
1299 lttng_ht_destroy(the_consumer_data
.stream_list_ht
);
1302 * Trace chunks in the registry may still exist if the session
1303 * daemon has encountered an internal error and could not
1304 * tear down its sessions and/or trace chunks properly.
1306 * Release the session daemon's implicit reference to any remaining
1307 * trace chunk and print an error if any trace chunk was found. Note
1308 * that there are _no_ legitimate cases for trace chunks to be left,
1309 * it is a leak. However, it can happen following a crash of the
1310 * session daemon and not emptying the registry would cause an assertion
1314 lttng_trace_chunk_registry_put_each_chunk(the_consumer_data
.chunk_registry
);
1315 if (trace_chunks_left
) {
1316 ERR("%u trace chunks are leaked by lttng-consumerd. "
1317 "This can be caused by an internal error of the session daemon.",
1320 /* Run all callbacks freeing each chunk. */
1322 lttng_trace_chunk_registry_destroy(the_consumer_data
.chunk_registry
);
1326 * Called from signal handler.
1328 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1332 CMM_STORE_SHARED(consumer_quit
, 1);
1333 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1335 PERROR("write consumer quit");
1338 DBG("Consumer flag that it should quit");
1342 * Flush pending writes to trace output disk file.
1344 static void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
, off_t orig_offset
)
1346 const int outfd
= stream
->out_fd
;
1349 * This does a blocking write-and-wait on any page that belongs to the
1350 * subbuffer prior to the one we just wrote.
1351 * Don't care about error values, as these are just hints and ways to
1352 * limit the amount of page cache used.
1354 if (orig_offset
< stream
->max_sb_size
) {
1357 lttng::io::hint_flush_range_dont_need_sync(
1358 outfd
, orig_offset
- stream
->max_sb_size
, stream
->max_sb_size
);
1362 * Initialise the necessary environnement :
1363 * - create a new context
1364 * - create the poll_pipe
1365 * - create the should_quit pipe (for signal handler)
1366 * - create the thread pipe (for splice)
1368 * Takes a function pointer as argument, this function is called when data is
1369 * available on a buffer. This function is responsible to do the
1370 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1371 * buffer configuration and then kernctl_put_next_subbuf at the end.
1373 * Returns a pointer to the new context or NULL on error.
1375 struct lttng_consumer_local_data
*
1376 lttng_consumer_create(enum lttng_consumer_type type
,
1377 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1378 struct lttng_consumer_local_data
*ctx
,
1379 bool locked_by_caller
),
1380 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1381 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1382 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1385 struct lttng_consumer_local_data
*ctx
;
1387 LTTNG_ASSERT(the_consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1388 the_consumer_data
.type
== type
);
1389 the_consumer_data
.type
= type
;
1391 ctx
= zmalloc
<lttng_consumer_local_data
>();
1392 if (ctx
== nullptr) {
1393 PERROR("allocating context");
1397 ctx
->consumer_error_socket
= -1;
1398 ctx
->consumer_metadata_socket
= -1;
1399 pthread_mutex_init(&ctx
->metadata_socket_lock
, nullptr);
1400 /* assign the callbacks */
1401 ctx
->on_buffer_ready
= buffer_ready
;
1402 ctx
->on_recv_channel
= recv_channel
;
1403 ctx
->on_recv_stream
= recv_stream
;
1404 ctx
->on_update_stream
= update_stream
;
1406 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1407 if (!ctx
->consumer_data_pipe
) {
1408 goto error_poll_pipe
;
1411 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1412 if (!ctx
->consumer_wakeup_pipe
) {
1413 goto error_wakeup_pipe
;
1416 ret
= pipe(ctx
->consumer_should_quit
);
1418 PERROR("Error creating recv pipe");
1419 goto error_quit_pipe
;
1422 ret
= pipe(ctx
->consumer_channel_pipe
);
1424 PERROR("Error creating channel pipe");
1425 goto error_channel_pipe
;
1428 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1429 if (!ctx
->consumer_metadata_pipe
) {
1430 goto error_metadata_pipe
;
1433 ctx
->channel_monitor_pipe
= -1;
1437 error_metadata_pipe
:
1438 utils_close_pipe(ctx
->consumer_channel_pipe
);
1440 utils_close_pipe(ctx
->consumer_should_quit
);
1442 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1444 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1452 * Iterate over all streams of the hashtable and free them properly.
1454 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1456 struct lttng_ht_iter iter
;
1457 struct lttng_consumer_stream
*stream
;
1459 if (ht
== nullptr) {
1464 const lttng::urcu::read_lock_guard read_lock
;
1465 cds_lfht_for_each_entry (ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1467 * Ignore return value since we are currently cleaning up so any error
1470 (void) consumer_del_stream(stream
, ht
);
1474 lttng_ht_destroy(ht
);
1478 * Iterate over all streams of the metadata hashtable and free them
1481 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1483 struct lttng_ht_iter iter
;
1484 struct lttng_consumer_stream
*stream
;
1486 if (ht
== nullptr) {
1491 const lttng::urcu::read_lock_guard read_lock
;
1492 cds_lfht_for_each_entry (ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1494 * Ignore return value since we are currently cleaning up so any error
1497 (void) consumer_del_metadata_stream(stream
, ht
);
1501 lttng_ht_destroy(ht
);
1505 * Close all fds associated with the instance and free the context.
1507 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1511 DBG("Consumer destroying it. Closing everything.");
1517 destroy_data_stream_ht(data_ht
);
1518 destroy_metadata_stream_ht(metadata_ht
);
1520 ret
= close(ctx
->consumer_error_socket
);
1524 ret
= close(ctx
->consumer_metadata_socket
);
1528 utils_close_pipe(ctx
->consumer_channel_pipe
);
1529 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1530 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1531 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1532 utils_close_pipe(ctx
->consumer_should_quit
);
1534 unlink(ctx
->consumer_command_sock_path
);
1539 * Write the metadata stream id on the specified file descriptor.
1542 write_relayd_metadata_id(int fd
, struct lttng_consumer_stream
*stream
, unsigned long padding
)
1545 struct lttcomm_relayd_metadata_payload hdr
;
1547 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1548 hdr
.padding_size
= htobe32(padding
);
1549 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1550 if (ret
< sizeof(hdr
)) {
1552 * This error means that the fd's end is closed so ignore the PERROR
1553 * not to clubber the error output since this can happen in a normal
1556 if (errno
!= EPIPE
) {
1557 PERROR("write metadata stream id");
1559 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1561 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1562 * handle writting the missing part so report that as an error and
1563 * don't lie to the caller.
1568 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1569 stream
->relayd_stream_id
,
1577 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1578 * core function for writing trace buffers to either the local filesystem or
1581 * It must be called with the stream and the channel lock held.
1583 * Careful review MUST be put if any changes occur!
1585 * Returns the number of bytes written
1587 ssize_t
lttng_consumer_on_read_subbuffer_mmap(struct lttng_consumer_stream
*stream
,
1588 const struct lttng_buffer_view
*buffer
,
1589 unsigned long padding
)
1592 off_t orig_offset
= stream
->out_fd_offset
;
1593 /* Default is on the disk */
1594 int outfd
= stream
->out_fd
;
1595 struct consumer_relayd_sock_pair
*relayd
= nullptr;
1596 unsigned int relayd_hang_up
= 0;
1597 const size_t subbuf_content_size
= buffer
->size
- padding
;
1600 /* RCU lock for the relayd pointer */
1601 const lttng::urcu::read_lock_guard read_lock
;
1602 LTTNG_ASSERT(stream
->net_seq_idx
!= (uint64_t) -1ULL || stream
->trace_chunk
);
1604 /* Flag that the current stream if set for network streaming. */
1605 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1606 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1607 if (relayd
== nullptr) {
1613 /* Handle stream on the relayd if the output is on the network */
1615 unsigned long netlen
= subbuf_content_size
;
1618 * Lock the control socket for the complete duration of the function
1619 * since from this point on we will use the socket.
1621 if (stream
->metadata_flag
) {
1622 /* Metadata requires the control socket. */
1623 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1624 if (stream
->reset_metadata_flag
) {
1625 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1626 stream
->relayd_stream_id
,
1627 stream
->metadata_version
);
1632 stream
->reset_metadata_flag
= 0;
1634 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1637 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1642 /* Use the returned socket. */
1645 /* Write metadata stream id before payload */
1646 if (stream
->metadata_flag
) {
1647 ret
= write_relayd_metadata_id(outfd
, stream
, padding
);
1654 write_len
= subbuf_content_size
;
1656 /* No streaming; we have to write the full padding. */
1657 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1658 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1660 ERR("Reset metadata file");
1663 stream
->reset_metadata_flag
= 0;
1667 * Check if we need to change the tracefile before writing the packet.
1669 if (stream
->chan
->tracefile_size
> 0 &&
1670 (stream
->tracefile_size_current
+ buffer
->size
) >
1671 stream
->chan
->tracefile_size
) {
1672 ret
= consumer_stream_rotate_output_files(stream
);
1676 outfd
= stream
->out_fd
;
1679 stream
->tracefile_size_current
+= buffer
->size
;
1680 write_len
= buffer
->size
;
1684 * This call guarantee that len or less is returned. It's impossible to
1685 * receive a ret value that is bigger than len.
1687 ret
= lttng_write(outfd
, buffer
->data
, write_len
);
1688 DBG("Consumer mmap write() ret %zd (len %zu)", ret
, write_len
);
1689 if (ret
< 0 || ((size_t) ret
!= write_len
)) {
1691 * Report error to caller if nothing was written else at least send the
1699 /* Socket operation failed. We consider the relayd dead */
1700 if (errno
== EPIPE
) {
1702 * This is possible if the fd is closed on the other side
1703 * (outfd) or any write problem. It can be verbose a bit for a
1704 * normal execution if for instance the relayd is stopped
1705 * abruptly. This can happen so set this to a DBG statement.
1707 DBG("Consumer mmap write detected relayd hang up");
1709 /* Unhandled error, print it and stop function right now. */
1710 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret
, write_len
);
1714 stream
->output_written
+= ret
;
1716 /* This call is useless on a socket so better save a syscall. */
1718 /* This won't block, but will start writeout asynchronously */
1719 lttng::io::hint_flush_range_async(outfd
, stream
->out_fd_offset
, write_len
);
1720 stream
->out_fd_offset
+= write_len
;
1721 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1726 * This is a special case that the relayd has closed its socket. Let's
1727 * cleanup the relayd object and all associated streams.
1729 if (relayd
&& relayd_hang_up
) {
1730 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1731 lttng_consumer_cleanup_relayd(relayd
);
1735 /* Unlock only if ctrl socket used */
1736 if (relayd
&& stream
->metadata_flag
) {
1737 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1744 * Splice the data from the ring buffer to the tracefile.
1746 * It must be called with the stream lock held.
1748 * Returns the number of bytes spliced.
1750 ssize_t
lttng_consumer_on_read_subbuffer_splice(struct lttng_consumer_local_data
*ctx
,
1751 struct lttng_consumer_stream
*stream
,
1753 unsigned long padding
)
1755 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1757 off_t orig_offset
= stream
->out_fd_offset
;
1758 const int fd
= stream
->wait_fd
;
1759 /* Default is on the disk */
1760 int outfd
= stream
->out_fd
;
1761 struct consumer_relayd_sock_pair
*relayd
= nullptr;
1763 unsigned int relayd_hang_up
= 0;
1765 switch (the_consumer_data
.type
) {
1766 case LTTNG_CONSUMER_KERNEL
:
1768 case LTTNG_CONSUMER32_UST
:
1769 case LTTNG_CONSUMER64_UST
:
1770 /* Not supported for user space tracing */
1773 ERR("Unknown consumer_data type");
1777 /* RCU lock for the relayd pointer */
1778 const lttng::urcu::read_lock_guard read_lock
;
1780 /* Flag that the current stream if set for network streaming. */
1781 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1782 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1783 if (relayd
== nullptr) {
1788 splice_pipe
= stream
->splice_pipe
;
1790 /* Write metadata stream id before payload */
1792 unsigned long total_len
= len
;
1794 if (stream
->metadata_flag
) {
1796 * Lock the control socket for the complete duration of the function
1797 * since from this point on we will use the socket.
1799 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1801 if (stream
->reset_metadata_flag
) {
1802 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1803 stream
->relayd_stream_id
,
1804 stream
->metadata_version
);
1809 stream
->reset_metadata_flag
= 0;
1811 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, padding
);
1818 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1821 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1827 /* Use the returned socket. */
1830 /* No streaming, we have to set the len with the full padding */
1833 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1834 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1836 ERR("Reset metadata file");
1839 stream
->reset_metadata_flag
= 0;
1842 * Check if we need to change the tracefile before writing the packet.
1844 if (stream
->chan
->tracefile_size
> 0 &&
1845 (stream
->tracefile_size_current
+ len
) > stream
->chan
->tracefile_size
) {
1846 ret
= consumer_stream_rotate_output_files(stream
);
1851 outfd
= stream
->out_fd
;
1854 stream
->tracefile_size_current
+= len
;
1858 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1859 (unsigned long) offset
,
1863 ret_splice
= splice(
1864 fd
, &offset
, splice_pipe
[1], nullptr, len
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1865 DBG("splice chan to pipe, ret %zd", ret_splice
);
1866 if (ret_splice
< 0) {
1869 PERROR("Error in relay splice");
1873 /* Handle stream on the relayd if the output is on the network */
1874 if (relayd
&& stream
->metadata_flag
) {
1875 const size_t metadata_payload_size
=
1876 sizeof(struct lttcomm_relayd_metadata_payload
);
1878 /* Update counter to fit the spliced data */
1879 ret_splice
+= metadata_payload_size
;
1880 len
+= metadata_payload_size
;
1882 * We do this so the return value can match the len passed as
1883 * argument to this function.
1885 written
-= metadata_payload_size
;
1888 /* Splice data out */
1889 ret_splice
= splice(splice_pipe
[0],
1894 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1895 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd", outfd
, ret_splice
);
1896 if (ret_splice
< 0) {
1901 } else if (ret_splice
> len
) {
1903 * We don't expect this code path to be executed but you never know
1904 * so this is an extra protection agains a buggy splice().
1907 written
+= ret_splice
;
1908 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
, len
);
1911 /* All good, update current len and continue. */
1915 /* This call is useless on a socket so better save a syscall. */
1917 /* This won't block, but will start writeout asynchronously */
1918 lttng::io::hint_flush_range_async(outfd
, stream
->out_fd_offset
, ret_splice
);
1919 stream
->out_fd_offset
+= ret_splice
;
1921 stream
->output_written
+= ret_splice
;
1922 written
+= ret_splice
;
1925 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1931 * This is a special case that the relayd has closed its socket. Let's
1932 * cleanup the relayd object and all associated streams.
1934 if (relayd
&& relayd_hang_up
) {
1935 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1936 lttng_consumer_cleanup_relayd(relayd
);
1937 /* Skip splice error so the consumer does not fail */
1942 /* send the appropriate error description to sessiond */
1945 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1948 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1951 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1956 if (relayd
&& stream
->metadata_flag
) {
1957 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1964 * Sample the snapshot positions for a specific fd
1966 * Returns 0 on success, < 0 on error
1968 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
1970 switch (the_consumer_data
.type
) {
1971 case LTTNG_CONSUMER_KERNEL
:
1972 return lttng_kconsumer_sample_snapshot_positions(stream
);
1973 case LTTNG_CONSUMER32_UST
:
1974 case LTTNG_CONSUMER64_UST
:
1975 return lttng_ustconsumer_sample_snapshot_positions(stream
);
1977 ERR("Unknown consumer_data type");
1983 * Take a snapshot for a specific fd
1985 * Returns 0 on success, < 0 on error
1987 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1989 switch (the_consumer_data
.type
) {
1990 case LTTNG_CONSUMER_KERNEL
:
1991 return lttng_kconsumer_take_snapshot(stream
);
1992 case LTTNG_CONSUMER32_UST
:
1993 case LTTNG_CONSUMER64_UST
:
1994 return lttng_ustconsumer_take_snapshot(stream
);
1996 ERR("Unknown consumer_data type");
2003 * Get the produced position
2005 * Returns 0 on success, < 0 on error
2007 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2009 switch (the_consumer_data
.type
) {
2010 case LTTNG_CONSUMER_KERNEL
:
2011 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
2012 case LTTNG_CONSUMER32_UST
:
2013 case LTTNG_CONSUMER64_UST
:
2014 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2016 ERR("Unknown consumer_data type");
2023 * Get the consumed position (free-running counter position in bytes).
2025 * Returns 0 on success, < 0 on error
2027 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2029 switch (the_consumer_data
.type
) {
2030 case LTTNG_CONSUMER_KERNEL
:
2031 return lttng_kconsumer_get_consumed_snapshot(stream
, pos
);
2032 case LTTNG_CONSUMER32_UST
:
2033 case LTTNG_CONSUMER64_UST
:
2034 return lttng_ustconsumer_get_consumed_snapshot(stream
, pos
);
2036 ERR("Unknown consumer_data type");
2042 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2044 struct pollfd
*consumer_sockpoll
)
2046 switch (the_consumer_data
.type
) {
2047 case LTTNG_CONSUMER_KERNEL
:
2048 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2049 case LTTNG_CONSUMER32_UST
:
2050 case LTTNG_CONSUMER64_UST
:
2051 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2053 ERR("Unknown consumer_data type");
2059 static void lttng_consumer_close_all_metadata()
2061 switch (the_consumer_data
.type
) {
2062 case LTTNG_CONSUMER_KERNEL
:
2064 * The Kernel consumer has a different metadata scheme so we don't
2065 * close anything because the stream will be closed by the session
2069 case LTTNG_CONSUMER32_UST
:
2070 case LTTNG_CONSUMER64_UST
:
2072 * Close all metadata streams. The metadata hash table is passed and
2073 * this call iterates over it by closing all wakeup fd. This is safe
2074 * because at this point we are sure that the metadata producer is
2075 * either dead or blocked.
2077 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2080 ERR("Unknown consumer_data type");
2086 * Clean up a metadata stream and free its memory.
2088 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
, struct lttng_ht
*ht
)
2090 struct lttng_consumer_channel
*channel
= nullptr;
2091 bool free_channel
= false;
2093 LTTNG_ASSERT(stream
);
2095 * This call should NEVER receive regular stream. It must always be
2096 * metadata stream and this is crucial for data structure synchronization.
2098 LTTNG_ASSERT(stream
->metadata_flag
);
2100 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2102 pthread_mutex_lock(&the_consumer_data
.lock
);
2104 * Note that this assumes that a stream's channel is never changed and
2105 * that the stream's lock doesn't need to be taken to sample its
2108 channel
= stream
->chan
;
2109 pthread_mutex_lock(&channel
->lock
);
2110 pthread_mutex_lock(&stream
->lock
);
2111 if (channel
->metadata_cache
) {
2112 /* Only applicable to userspace consumers. */
2113 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
2116 /* Remove any reference to that stream. */
2117 consumer_stream_delete(stream
, ht
);
2119 /* Close down everything including the relayd if one. */
2120 consumer_stream_close_output(stream
);
2121 /* Destroy tracer buffers of the stream. */
2122 consumer_stream_destroy_buffers(stream
);
2124 /* Atomically decrement channel refcount since other threads can use it. */
2125 if (!uatomic_sub_return(&channel
->refcount
, 1) &&
2126 !uatomic_read(&channel
->nb_init_stream_left
)) {
2127 /* Go for channel deletion! */
2128 free_channel
= true;
2130 stream
->chan
= nullptr;
2133 * Nullify the stream reference so it is not used after deletion. The
2134 * channel lock MUST be acquired before being able to check for a NULL
2137 channel
->metadata_stream
= nullptr;
2138 channel
->metadata_pushed_wait_queue
.wake_all();
2140 if (channel
->metadata_cache
) {
2141 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
2143 pthread_mutex_unlock(&stream
->lock
);
2144 pthread_mutex_unlock(&channel
->lock
);
2145 pthread_mutex_unlock(&the_consumer_data
.lock
);
2148 consumer_del_channel(channel
);
2151 lttng_trace_chunk_put(stream
->trace_chunk
);
2152 stream
->trace_chunk
= nullptr;
2153 consumer_stream_free(stream
);
2157 * Action done with the metadata stream when adding it to the consumer internal
2158 * data structures to handle it.
2160 void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2162 struct lttng_ht
*ht
= metadata_ht
;
2163 struct lttng_ht_iter iter
;
2164 struct lttng_ht_node_u64
*node
;
2166 LTTNG_ASSERT(stream
);
2169 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2171 pthread_mutex_lock(&the_consumer_data
.lock
);
2172 pthread_mutex_lock(&stream
->chan
->lock
);
2173 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2174 pthread_mutex_lock(&stream
->lock
);
2177 * From here, refcounts are updated so be _careful_ when returning an error
2181 const lttng::urcu::read_lock_guard read_lock
;
2184 * Lookup the stream just to make sure it does not exist in our internal
2185 * state. This should NEVER happen.
2187 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2188 node
= lttng_ht_iter_get_node
<lttng_ht_node_u64
>(&iter
);
2189 LTTNG_ASSERT(!node
);
2192 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2193 * in terms of destroying the associated channel, because the action that
2194 * causes the count to become 0 also causes a stream to be added. The
2195 * channel deletion will thus be triggered by the following removal of this
2198 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2199 /* Increment refcount before decrementing nb_init_stream_left */
2201 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2204 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2206 lttng_ht_add_u64(the_consumer_data
.stream_per_chan_id_ht
, &stream
->node_channel_id
);
2209 * Add stream to the stream_list_ht of the consumer data. No need to steal
2210 * the key since the HT does not use it and we allow to add redundant keys
2213 lttng_ht_add_u64(the_consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2215 pthread_mutex_unlock(&stream
->lock
);
2216 pthread_mutex_unlock(&stream
->chan
->lock
);
2217 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2218 pthread_mutex_unlock(&the_consumer_data
.lock
);
2222 * Delete data stream that are flagged for deletion (endpoint_status).
2224 static void validate_endpoint_status_data_stream()
2226 struct lttng_ht_iter iter
;
2227 struct lttng_consumer_stream
*stream
;
2229 DBG("Consumer delete flagged data stream");
2232 const lttng::urcu::read_lock_guard read_lock
;
2234 cds_lfht_for_each_entry (data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2235 /* Validate delete flag of the stream */
2236 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2239 /* Delete it right now */
2240 consumer_del_stream(stream
, data_ht
);
2246 * Delete metadata stream that are flagged for deletion (endpoint_status).
2248 static void validate_endpoint_status_metadata_stream(struct lttng_poll_event
*pollset
)
2250 struct lttng_ht_iter iter
;
2251 struct lttng_consumer_stream
*stream
;
2253 DBG("Consumer delete flagged metadata stream");
2255 LTTNG_ASSERT(pollset
);
2258 const lttng::urcu::read_lock_guard read_lock
;
2259 cds_lfht_for_each_entry (metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2260 /* Validate delete flag of the stream */
2261 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2265 * Remove from pollset so the metadata thread can continue without
2266 * blocking on a deleted stream.
2268 lttng_poll_del(pollset
, stream
->wait_fd
);
2270 /* Delete it right now */
2271 consumer_del_metadata_stream(stream
, metadata_ht
);
2277 * Thread polls on metadata file descriptor and write them on disk or on the
2280 void *consumer_thread_metadata_poll(void *data
)
2282 int ret
, i
, pollfd
, err
= -1;
2283 uint32_t revents
, nb_fd
;
2284 struct lttng_consumer_stream
*stream
= nullptr;
2285 struct lttng_ht_iter iter
;
2286 struct lttng_ht_node_u64
*node
;
2287 struct lttng_poll_event events
;
2288 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
2291 rcu_register_thread();
2293 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2295 if (testpoint(consumerd_thread_metadata
)) {
2296 goto error_testpoint
;
2299 health_code_update();
2301 DBG("Thread metadata poll started");
2303 /* Size is set to 1 for the consumer_metadata pipe */
2304 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2306 ERR("Poll set creation failed");
2310 ret
= lttng_poll_add(&events
, lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2316 DBG("Metadata main loop started");
2320 health_code_update();
2321 health_poll_entry();
2322 DBG("Metadata poll wait");
2323 ret
= lttng_poll_wait(&events
, -1);
2324 DBG("Metadata poll return from wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2326 DBG("Metadata event caught in thread");
2328 if (errno
== EINTR
) {
2329 ERR("Poll EINTR caught");
2332 if (LTTNG_POLL_GETNB(&events
) == 0) {
2333 err
= 0; /* All is OK */
2340 /* From here, the event is a metadata wait fd */
2341 for (i
= 0; i
< nb_fd
; i
++) {
2342 health_code_update();
2344 revents
= LTTNG_POLL_GETEV(&events
, i
);
2345 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2347 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2348 if (revents
& LPOLLIN
) {
2351 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2353 sizeof(stream
)); /* NOLINT sizeof
2356 if (pipe_len
< sizeof(stream
)) { /* NOLINT sizeof used on a
2359 PERROR("read metadata stream");
2362 * Remove the pipe from the poll set and continue
2363 * the loop since their might be data to consume.
2367 lttng_pipe_get_readfd(
2368 ctx
->consumer_metadata_pipe
));
2369 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2373 /* A NULL stream means that the state has changed. */
2374 if (stream
== nullptr) {
2375 /* Check for deleted streams. */
2376 validate_endpoint_status_metadata_stream(&events
);
2380 DBG("Adding metadata stream %d to poll set",
2383 /* Add metadata stream to the global poll events list */
2385 &events
, stream
->wait_fd
, LPOLLIN
| LPOLLPRI
);
2386 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2387 DBG("Metadata thread pipe hung up");
2389 * Remove the pipe from the poll set and continue the loop
2390 * since their might be data to consume.
2394 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2395 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2398 ERR("Unexpected poll events %u for sock %d",
2404 /* Handle other stream */
2408 const lttng::urcu::read_lock_guard read_lock
;
2410 uint64_t tmp_id
= (uint64_t) pollfd
;
2412 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2414 node
= lttng_ht_iter_get_node
<lttng_ht_node_u64
>(&iter
);
2417 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
2419 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2420 /* Get the data out of the metadata file descriptor */
2421 DBG("Metadata available on fd %d", pollfd
);
2422 LTTNG_ASSERT(stream
->wait_fd
== pollfd
);
2425 health_code_update();
2427 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2429 * We don't check the return value here since if we get
2430 * a negative len, it means an error occurred thus we
2431 * simply remove it from the poll set and free the
2436 /* It's ok to have an unavailable sub-buffer */
2437 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2438 /* Clean up stream from consumer and free it. */
2439 lttng_poll_del(&events
, stream
->wait_fd
);
2440 consumer_del_metadata_stream(stream
, metadata_ht
);
2442 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2443 DBG("Metadata fd %d is hup|err.", pollfd
);
2444 if (!stream
->hangup_flush_done
&&
2445 (the_consumer_data
.type
== LTTNG_CONSUMER32_UST
||
2446 the_consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2447 DBG("Attempting to flush and consume the UST buffers");
2448 lttng_ustconsumer_on_stream_hangup(stream
);
2450 /* We just flushed the stream now read it. */
2452 health_code_update();
2454 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2456 * We don't check the return value here since if we
2457 * get a negative len, it means an error occurred
2458 * thus we simply remove it from the poll set and
2464 lttng_poll_del(&events
, stream
->wait_fd
);
2466 * This call update the channel states, closes file descriptors
2467 * and securely free the stream.
2469 consumer_del_metadata_stream(stream
, metadata_ht
);
2471 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2474 /* Release RCU lock for the stream looked up */
2481 DBG("Metadata poll thread exiting");
2483 lttng_poll_clean(&events
);
2488 ERR("Health error occurred in %s", __func__
);
2490 health_unregister(health_consumerd
);
2491 rcu_unregister_thread();
2496 * This thread polls the fds in the set to consume the data and write
2497 * it to tracefile if necessary.
2499 void *consumer_thread_data_poll(void *data
)
2501 int num_rdy
, high_prio
, ret
, i
, err
= -1;
2502 struct pollfd
*pollfd
= nullptr;
2503 /* local view of the streams */
2504 struct lttng_consumer_stream
**local_stream
= nullptr, *new_stream
= nullptr;
2505 /* local view of consumer_data.fds_count */
2507 /* 2 for the consumer_data_pipe and wake up pipe */
2508 const int nb_pipes_fd
= 2;
2509 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2510 int nb_inactive_fd
= 0;
2511 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
2514 rcu_register_thread();
2516 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2518 if (testpoint(consumerd_thread_data
)) {
2519 goto error_testpoint
;
2522 health_code_update();
2524 local_stream
= zmalloc
<lttng_consumer_stream
*>();
2525 if (local_stream
== nullptr) {
2526 PERROR("local_stream malloc");
2531 health_code_update();
2536 * the fds set has been updated, we need to update our
2537 * local array as well
2539 pthread_mutex_lock(&the_consumer_data
.lock
);
2540 if (the_consumer_data
.need_update
) {
2545 local_stream
= nullptr;
2547 /* Allocate for all fds */
2549 calloc
<struct pollfd
>(the_consumer_data
.stream_count
+ nb_pipes_fd
);
2550 if (pollfd
== nullptr) {
2551 PERROR("pollfd malloc");
2552 pthread_mutex_unlock(&the_consumer_data
.lock
);
2556 local_stream
= calloc
<lttng_consumer_stream
*>(
2557 the_consumer_data
.stream_count
+ nb_pipes_fd
);
2558 if (local_stream
== nullptr) {
2559 PERROR("local_stream malloc");
2560 pthread_mutex_unlock(&the_consumer_data
.lock
);
2563 ret
= update_poll_array(
2564 ctx
, &pollfd
, local_stream
, data_ht
, &nb_inactive_fd
);
2566 ERR("Error in allocating pollfd or local_outfds");
2567 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2568 pthread_mutex_unlock(&the_consumer_data
.lock
);
2572 the_consumer_data
.need_update
= 0;
2574 pthread_mutex_unlock(&the_consumer_data
.lock
);
2576 /* No FDs and consumer_quit, consumer_cleanup the thread */
2577 if (nb_fd
== 0 && nb_inactive_fd
== 0 && CMM_LOAD_SHARED(consumer_quit
) == 1) {
2578 err
= 0; /* All is OK */
2581 /* poll on the array of fds */
2583 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2584 if (testpoint(consumerd_thread_data_poll
)) {
2587 health_poll_entry();
2588 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2590 DBG("poll num_rdy : %d", num_rdy
);
2591 if (num_rdy
== -1) {
2593 * Restart interrupted system call.
2595 if (errno
== EINTR
) {
2598 PERROR("Poll error");
2599 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2601 } else if (num_rdy
== 0) {
2602 DBG("Polling thread timed out");
2606 if (caa_unlikely(data_consumption_paused
)) {
2607 DBG("Data consumption paused, sleeping...");
2613 * If the consumer_data_pipe triggered poll go directly to the
2614 * beginning of the loop to update the array. We want to prioritize
2615 * array update over low-priority reads.
2617 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2618 ssize_t pipe_readlen
;
2620 DBG("consumer_data_pipe wake up");
2621 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2623 sizeof(new_stream
)); /* NOLINT sizeof used on
2625 if (pipe_readlen
< sizeof(new_stream
)) { /* NOLINT sizeof used on a pointer.
2627 PERROR("Consumer data pipe");
2628 /* Continue so we can at least handle the current stream(s). */
2633 * If the stream is NULL, just ignore it. It's also possible that
2634 * the sessiond poll thread changed the consumer_quit state and is
2635 * waking us up to test it.
2637 if (new_stream
== nullptr) {
2638 validate_endpoint_status_data_stream();
2642 /* Continue to update the local streams and handle prio ones */
2646 /* Handle wakeup pipe. */
2647 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2649 ssize_t pipe_readlen
;
2652 lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
, sizeof(dummy
));
2653 if (pipe_readlen
< 0) {
2654 PERROR("Consumer data wakeup pipe");
2656 /* We've been awakened to handle stream(s). */
2657 ctx
->has_wakeup
= 0;
2660 /* Take care of high priority channels first. */
2661 for (i
= 0; i
< nb_fd
; i
++) {
2662 health_code_update();
2664 if (local_stream
[i
] == nullptr) {
2667 if (pollfd
[i
].revents
& POLLPRI
) {
2668 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2670 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2671 /* it's ok to have an unavailable sub-buffer */
2672 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2673 /* Clean the stream and free it. */
2674 consumer_del_stream(local_stream
[i
], data_ht
);
2675 local_stream
[i
] = nullptr;
2676 } else if (len
> 0) {
2677 local_stream
[i
]->has_data_left_to_be_read_before_teardown
=
2684 * If we read high prio channel in this loop, try again
2685 * for more high prio data.
2691 /* Take care of low priority channels. */
2692 for (i
= 0; i
< nb_fd
; i
++) {
2693 health_code_update();
2695 if (local_stream
[i
] == nullptr) {
2698 if ((pollfd
[i
].revents
& POLLIN
) || local_stream
[i
]->hangup_flush_done
||
2699 local_stream
[i
]->has_data
) {
2700 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2701 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2702 /* it's ok to have an unavailable sub-buffer */
2703 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2704 /* Clean the stream and free it. */
2705 consumer_del_stream(local_stream
[i
], data_ht
);
2706 local_stream
[i
] = nullptr;
2707 } else if (len
> 0) {
2708 local_stream
[i
]->has_data_left_to_be_read_before_teardown
=
2714 /* Handle hangup and errors */
2715 for (i
= 0; i
< nb_fd
; i
++) {
2716 health_code_update();
2718 if (local_stream
[i
] == nullptr) {
2721 if (!local_stream
[i
]->hangup_flush_done
&&
2722 (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
)) &&
2723 (the_consumer_data
.type
== LTTNG_CONSUMER32_UST
||
2724 the_consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2725 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2727 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2728 /* Attempt read again, for the data we just flushed. */
2729 local_stream
[i
]->has_data_left_to_be_read_before_teardown
= 1;
2732 * When a stream's pipe dies (hup/err/nval), an "inactive producer" flush is
2733 * performed. This type of flush ensures that a new packet is produced no
2734 * matter the consumed/produced positions are.
2736 * This, in turn, causes the next pass to see that data available for the
2737 * stream. When we come back here, we can be assured that all available
2738 * data has been consumed and we can finally destroy the stream.
2740 * If the poll flag is HUP/ERR/NVAL and we have
2741 * read no data in this pass, we can remove the
2742 * stream from its hash table.
2744 if ((pollfd
[i
].revents
& POLLHUP
)) {
2745 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2746 if (!local_stream
[i
]->has_data_left_to_be_read_before_teardown
) {
2747 consumer_del_stream(local_stream
[i
], data_ht
);
2748 local_stream
[i
] = nullptr;
2750 } else if (pollfd
[i
].revents
& POLLERR
) {
2751 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2752 if (!local_stream
[i
]->has_data_left_to_be_read_before_teardown
) {
2753 consumer_del_stream(local_stream
[i
], data_ht
);
2754 local_stream
[i
] = nullptr;
2756 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2757 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2758 if (!local_stream
[i
]->has_data_left_to_be_read_before_teardown
) {
2759 consumer_del_stream(local_stream
[i
], data_ht
);
2760 local_stream
[i
] = nullptr;
2763 if (local_stream
[i
] != nullptr) {
2764 local_stream
[i
]->has_data_left_to_be_read_before_teardown
= 0;
2771 DBG("polling thread exiting");
2776 * Close the write side of the pipe so epoll_wait() in
2777 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2778 * read side of the pipe. If we close them both, epoll_wait strangely does
2779 * not return and could create a endless wait period if the pipe is the
2780 * only tracked fd in the poll set. The thread will take care of closing
2783 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2788 ERR("Health error occurred in %s", __func__
);
2790 health_unregister(health_consumerd
);
2792 rcu_unregister_thread();
2797 * Close wake-up end of each stream belonging to the channel. This will
2798 * allow the poll() on the stream read-side to detect when the
2799 * write-side (application) finally closes them.
2801 static void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2803 struct lttng_ht
*ht
;
2804 struct lttng_consumer_stream
*stream
;
2805 struct lttng_ht_iter iter
;
2807 ht
= the_consumer_data
.stream_per_chan_id_ht
;
2809 const lttng::urcu::read_lock_guard read_lock
;
2810 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2811 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2816 node_channel_id
.node
)
2819 * Protect against teardown with mutex.
2821 pthread_mutex_lock(&stream
->lock
);
2822 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2825 switch (the_consumer_data
.type
) {
2826 case LTTNG_CONSUMER_KERNEL
:
2828 case LTTNG_CONSUMER32_UST
:
2829 case LTTNG_CONSUMER64_UST
:
2830 if (stream
->metadata_flag
) {
2831 /* Safe and protected by the stream lock. */
2832 lttng_ustconsumer_close_metadata(stream
->chan
);
2835 * Note: a mutex is taken internally within
2836 * liblttng-ust-ctl to protect timer wakeup_fd
2837 * use from concurrent close.
2839 lttng_ustconsumer_close_stream_wakeup(stream
);
2843 ERR("Unknown consumer_data type");
2847 pthread_mutex_unlock(&stream
->lock
);
2851 static void destroy_channel_ht(struct lttng_ht
*ht
)
2853 struct lttng_ht_iter iter
;
2854 struct lttng_consumer_channel
*channel
;
2857 if (ht
== nullptr) {
2862 const lttng::urcu::read_lock_guard read_lock
;
2864 cds_lfht_for_each_entry (ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2865 ret
= lttng_ht_del(ht
, &iter
);
2866 LTTNG_ASSERT(ret
!= 0);
2870 lttng_ht_destroy(ht
);
2874 * This thread polls the channel fds to detect when they are being
2875 * closed. It closes all related streams if the channel is detected as
2876 * closed. It is currently only used as a shim layer for UST because the
2877 * consumerd needs to keep the per-stream wakeup end of pipes open for
2880 void *consumer_thread_channel_poll(void *data
)
2882 int ret
, i
, pollfd
, err
= -1;
2883 uint32_t revents
, nb_fd
;
2884 struct lttng_consumer_channel
*chan
= nullptr;
2885 struct lttng_ht_iter iter
;
2886 struct lttng_ht_node_u64
*node
;
2887 struct lttng_poll_event events
;
2888 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
2889 struct lttng_ht
*channel_ht
;
2891 rcu_register_thread();
2893 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2895 if (testpoint(consumerd_thread_channel
)) {
2896 goto error_testpoint
;
2899 health_code_update();
2901 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2903 /* ENOMEM at this point. Better to bail out. */
2907 DBG("Thread channel poll started");
2909 /* Size is set to 1 for the consumer_channel pipe */
2910 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2912 ERR("Poll set creation failed");
2916 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2922 DBG("Channel main loop started");
2926 health_code_update();
2927 DBG("Channel poll wait");
2928 health_poll_entry();
2929 ret
= lttng_poll_wait(&events
, -1);
2930 DBG("Channel poll return from wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2932 DBG("Channel event caught in thread");
2934 if (errno
== EINTR
) {
2935 ERR("Poll EINTR caught");
2938 if (LTTNG_POLL_GETNB(&events
) == 0) {
2939 err
= 0; /* All is OK */
2946 /* From here, the event is a channel wait fd */
2947 for (i
= 0; i
< nb_fd
; i
++) {
2948 health_code_update();
2950 revents
= LTTNG_POLL_GETEV(&events
, i
);
2951 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2953 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2954 if (revents
& LPOLLIN
) {
2955 enum consumer_channel_action action
;
2958 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2961 ERR("Error reading channel pipe");
2963 lttng_poll_del(&events
,
2964 ctx
->consumer_channel_pipe
[0]);
2969 case CONSUMER_CHANNEL_ADD
:
2971 DBG("Adding channel %d to poll set", chan
->wait_fd
);
2973 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2975 const lttng::urcu::read_lock_guard read_lock
;
2976 lttng_ht_add_unique_u64(channel_ht
,
2977 &chan
->wait_fd_node
);
2978 /* Add channel to the global poll events list */
2979 // FIXME: Empty flag on a pipe pollset, this might
2981 lttng_poll_add(&events
, chan
->wait_fd
, 0);
2984 case CONSUMER_CHANNEL_DEL
:
2987 * This command should never be called if the
2988 * channel has streams monitored by either the data
2989 * or metadata thread. The consumer only notify this
2990 * thread with a channel del. command if it receives
2991 * a destroy channel command from the session daemon
2992 * that send it if a command prior to the
2993 * GET_CHANNEL failed.
2996 const lttng::urcu::read_lock_guard read_lock
;
2997 chan
= consumer_find_channel(key
);
2999 ERR("UST consumer get channel key %" PRIu64
3000 " not found for del channel",
3004 lttng_poll_del(&events
, chan
->wait_fd
);
3005 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
3006 ret
= lttng_ht_del(channel_ht
, &iter
);
3007 LTTNG_ASSERT(ret
== 0);
3009 switch (the_consumer_data
.type
) {
3010 case LTTNG_CONSUMER_KERNEL
:
3012 case LTTNG_CONSUMER32_UST
:
3013 case LTTNG_CONSUMER64_UST
:
3014 health_code_update();
3015 /* Destroy streams that might have been left
3016 * in the stream list. */
3017 clean_channel_stream_list(chan
);
3020 ERR("Unknown consumer_data type");
3025 * Release our own refcount. Force channel deletion
3026 * even if streams were not initialized.
3028 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3029 consumer_del_channel(chan
);
3033 case CONSUMER_CHANNEL_QUIT
:
3035 * Remove the pipe from the poll set and continue
3036 * the loop since their might be data to consume.
3038 lttng_poll_del(&events
,
3039 ctx
->consumer_channel_pipe
[0]);
3042 ERR("Unknown action");
3045 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3046 DBG("Channel thread pipe hung up");
3048 * Remove the pipe from the poll set and continue the loop
3049 * since their might be data to consume.
3051 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3054 ERR("Unexpected poll events %u for sock %d",
3060 /* Handle other stream */
3064 const lttng::urcu::read_lock_guard read_lock
;
3066 uint64_t tmp_id
= (uint64_t) pollfd
;
3068 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3070 node
= lttng_ht_iter_get_node
<lttng_ht_node_u64
>(&iter
);
3073 chan
= caa_container_of(node
, struct lttng_consumer_channel
, wait_fd_node
);
3075 /* Check for error event */
3076 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3077 DBG("Channel fd %d is hup|err.", pollfd
);
3079 lttng_poll_del(&events
, chan
->wait_fd
);
3080 ret
= lttng_ht_del(channel_ht
, &iter
);
3081 LTTNG_ASSERT(ret
== 0);
3084 * This will close the wait fd for each stream associated to
3085 * this channel AND monitored by the data/metadata thread thus
3086 * will be clean by the right thread.
3088 consumer_close_channel_streams(chan
);
3090 /* Release our own refcount */
3091 if (!uatomic_sub_return(&chan
->refcount
, 1) &&
3092 !uatomic_read(&chan
->nb_init_stream_left
)) {
3093 consumer_del_channel(chan
);
3096 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3100 /* Release RCU lock for the channel looked up */
3107 lttng_poll_clean(&events
);
3109 destroy_channel_ht(channel_ht
);
3112 DBG("Channel poll thread exiting");
3115 ERR("Health error occurred in %s", __func__
);
3117 health_unregister(health_consumerd
);
3118 rcu_unregister_thread();
3122 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3123 struct pollfd
*sockpoll
,
3129 LTTNG_ASSERT(sockpoll
);
3131 ret
= lttng_consumer_poll_socket(sockpoll
);
3135 DBG("Metadata connection on client_socket");
3137 /* Blocking call, waiting for transmission */
3138 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3139 if (ctx
->consumer_metadata_socket
< 0) {
3140 WARN("On accept metadata");
3151 * This thread listens on the consumerd socket and receives the file
3152 * descriptors from the session daemon.
3154 void *consumer_thread_sessiond_poll(void *data
)
3156 int sock
= -1, client_socket
, ret
, err
= -1;
3158 * structure to poll for incoming data on communication socket avoids
3159 * making blocking sockets.
3161 struct pollfd consumer_sockpoll
[2];
3162 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
3164 rcu_register_thread();
3166 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3168 if (testpoint(consumerd_thread_sessiond
)) {
3169 goto error_testpoint
;
3172 health_code_update();
3174 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3175 unlink(ctx
->consumer_command_sock_path
);
3176 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3177 if (client_socket
< 0) {
3178 ERR("Cannot create command socket");
3182 ret
= lttcomm_listen_unix_sock(client_socket
);
3187 DBG("Sending ready command to lttng-sessiond");
3188 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3189 /* return < 0 on error, but == 0 is not fatal */
3191 ERR("Error sending ready command to lttng-sessiond");
3195 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3196 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3197 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3198 consumer_sockpoll
[1].fd
= client_socket
;
3199 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3201 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3209 DBG("Connection on client_socket");
3211 /* Blocking call, waiting for transmission */
3212 sock
= lttcomm_accept_unix_sock(client_socket
);
3219 * Setup metadata socket which is the second socket connection on the
3220 * command unix socket.
3222 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3231 /* This socket is not useful anymore. */
3232 ret
= close(client_socket
);
3234 PERROR("close client_socket");
3238 /* update the polling structure to poll on the established socket */
3239 consumer_sockpoll
[1].fd
= sock
;
3240 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3243 health_code_update();
3245 health_poll_entry();
3246 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3255 DBG("Incoming command on sock");
3256 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3259 * This could simply be a session daemon quitting. Don't output
3262 DBG("Communication interrupted on command socket");
3266 if (CMM_LOAD_SHARED(consumer_quit
)) {
3267 DBG("consumer_thread_receive_fds received quit from signal");
3268 err
= 0; /* All is OK */
3271 DBG("Received command on sock");
3277 DBG("Consumer thread sessiond poll exiting");
3280 * Close metadata streams since the producer is the session daemon which
3283 * NOTE: for now, this only applies to the UST tracer.
3285 lttng_consumer_close_all_metadata();
3288 * when all fds have hung up, the polling thread
3291 CMM_STORE_SHARED(consumer_quit
, 1);
3294 * Notify the data poll thread to poll back again and test the
3295 * consumer_quit state that we just set so to quit gracefully.
3297 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3299 notify_channel_pipe(ctx
, nullptr, -1, CONSUMER_CHANNEL_QUIT
);
3301 notify_health_quit_pipe(health_quit_pipe
);
3303 /* Cleaning up possibly open sockets. */
3307 PERROR("close sock sessiond poll");
3310 if (client_socket
>= 0) {
3311 ret
= close(client_socket
);
3313 PERROR("close client_socket sessiond poll");
3320 ERR("Health error occurred in %s", __func__
);
3322 health_unregister(health_consumerd
);
3324 rcu_unregister_thread();
3328 static int post_consume(struct lttng_consumer_stream
*stream
,
3329 const struct stream_subbuffer
*subbuffer
,
3330 struct lttng_consumer_local_data
*ctx
)
3334 const size_t count
=
3335 lttng_dynamic_array_get_count(&stream
->read_subbuffer_ops
.post_consume_cbs
);
3337 for (i
= 0; i
< count
; i
++) {
3338 const post_consume_cb op
= *(post_consume_cb
*) lttng_dynamic_array_get_element(
3339 &stream
->read_subbuffer_ops
.post_consume_cbs
, i
);
3341 ret
= op(stream
, subbuffer
, ctx
);
3350 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3351 struct lttng_consumer_local_data
*ctx
,
3352 bool locked_by_caller
)
3354 ssize_t ret
, written_bytes
= 0;
3356 struct stream_subbuffer subbuffer
= {};
3357 enum get_next_subbuffer_status get_next_status
;
3359 if (!locked_by_caller
) {
3360 stream
->read_subbuffer_ops
.lock(stream
);
3362 stream
->read_subbuffer_ops
.assert_locked(stream
);
3365 if (stream
->read_subbuffer_ops
.on_wake_up
) {
3366 ret
= stream
->read_subbuffer_ops
.on_wake_up(stream
);
3373 * If the stream was flagged to be ready for rotation before we extract
3374 * the next packet, rotate it now.
3376 if (stream
->rotate_ready
) {
3377 DBG("Rotate stream before consuming data");
3378 ret
= lttng_consumer_rotate_stream(stream
);
3380 ERR("Stream rotation error before consuming data");
3385 get_next_status
= stream
->read_subbuffer_ops
.get_next_subbuffer(stream
, &subbuffer
);
3386 switch (get_next_status
) {
3387 case GET_NEXT_SUBBUFFER_STATUS_OK
:
3389 case GET_NEXT_SUBBUFFER_STATUS_NO_DATA
:
3393 case GET_NEXT_SUBBUFFER_STATUS_ERROR
:
3400 ret
= stream
->read_subbuffer_ops
.pre_consume_subbuffer(stream
, &subbuffer
);
3402 goto error_put_subbuf
;
3405 written_bytes
= stream
->read_subbuffer_ops
.consume_subbuffer(ctx
, stream
, &subbuffer
);
3406 if (written_bytes
<= 0) {
3407 ERR("Error consuming subbuffer: (%zd)", written_bytes
);
3408 ret
= (int) written_bytes
;
3409 goto error_put_subbuf
;
3412 ret
= stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3417 ret
= post_consume(stream
, &subbuffer
, ctx
);
3423 * After extracting the packet, we check if the stream is now ready to
3424 * be rotated and perform the action immediately.
3426 * Don't overwrite `ret` as callers expect the number of bytes
3427 * consumed to be returned on success.
3429 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
3430 if (rotation_ret
== 1) {
3431 rotation_ret
= lttng_consumer_rotate_stream(stream
);
3432 if (rotation_ret
< 0) {
3434 ERR("Stream rotation error after consuming data");
3438 } else if (rotation_ret
< 0) {
3440 ERR("Failed to check if stream was ready to rotate after consuming data");
3445 if (stream
->read_subbuffer_ops
.on_sleep
) {
3446 stream
->read_subbuffer_ops
.on_sleep(stream
, ctx
);
3449 ret
= written_bytes
;
3451 if (!locked_by_caller
) {
3452 stream
->read_subbuffer_ops
.unlock(stream
);
3457 (void) stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3461 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3463 switch (the_consumer_data
.type
) {
3464 case LTTNG_CONSUMER_KERNEL
:
3465 return lttng_kconsumer_on_recv_stream(stream
);
3466 case LTTNG_CONSUMER32_UST
:
3467 case LTTNG_CONSUMER64_UST
:
3468 return lttng_ustconsumer_on_recv_stream(stream
);
3470 ERR("Unknown consumer_data type");
3477 * Allocate and set consumer data hash tables.
3479 int lttng_consumer_init()
3481 the_consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3482 if (!the_consumer_data
.channel_ht
) {
3486 the_consumer_data
.channels_by_session_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3487 if (!the_consumer_data
.channels_by_session_id_ht
) {
3491 the_consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3492 if (!the_consumer_data
.relayd_ht
) {
3496 the_consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3497 if (!the_consumer_data
.stream_list_ht
) {
3501 the_consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3502 if (!the_consumer_data
.stream_per_chan_id_ht
) {
3506 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3511 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3516 the_consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3517 if (!the_consumer_data
.chunk_registry
) {
3528 * Process the ADD_RELAYD command receive by a consumer.
3530 * This will create a relayd socket pair and add it to the relayd hash table.
3531 * The caller MUST acquire a RCU read side lock before calling it.
3533 void consumer_add_relayd_socket(uint64_t net_seq_idx
,
3535 struct lttng_consumer_local_data
*ctx
,
3537 struct pollfd
*consumer_sockpoll
,
3538 uint64_t sessiond_id
,
3539 uint64_t relayd_session_id
,
3540 uint32_t relayd_version_major
,
3541 uint32_t relayd_version_minor
,
3542 enum lttcomm_sock_proto relayd_socket_protocol
)
3544 int fd
= -1, ret
= -1, relayd_created
= 0;
3545 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3546 struct consumer_relayd_sock_pair
*relayd
= nullptr;
3549 LTTNG_ASSERT(sock
>= 0);
3550 ASSERT_RCU_READ_LOCKED();
3552 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3554 /* Get relayd reference if exists. */
3555 relayd
= consumer_find_relayd(net_seq_idx
);
3556 if (relayd
== nullptr) {
3557 LTTNG_ASSERT(sock_type
== LTTNG_STREAM_CONTROL
);
3558 /* Not found. Allocate one. */
3559 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3560 if (relayd
== nullptr) {
3561 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3564 relayd
->sessiond_session_id
= sessiond_id
;
3569 * This code path MUST continue to the consumer send status message to
3570 * we can notify the session daemon and continue our work without
3571 * killing everything.
3575 * relayd key should never be found for control socket.
3577 LTTNG_ASSERT(sock_type
!= LTTNG_STREAM_CONTROL
);
3580 /* First send a status message before receiving the fds. */
3581 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3583 /* Somehow, the session daemon is not responding anymore. */
3584 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3585 goto error_nosignal
;
3588 /* Poll on consumer socket. */
3589 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3591 /* Needing to exit in the middle of a command: error. */
3592 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3593 goto error_nosignal
;
3596 /* Get relayd socket from session daemon */
3597 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3598 if (ret
!= sizeof(fd
)) {
3599 fd
= -1; /* Just in case it gets set with an invalid value. */
3602 * Failing to receive FDs might indicate a major problem such as
3603 * reaching a fd limit during the receive where the kernel returns a
3604 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3605 * don't take any chances and stop everything.
3607 * XXX: Feature request #558 will fix that and avoid this possible
3608 * issue when reaching the fd limit.
3610 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3611 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3615 /* Copy socket information and received FD */
3616 switch (sock_type
) {
3617 case LTTNG_STREAM_CONTROL
:
3618 /* Copy received lttcomm socket */
3619 ret
= lttcomm_populate_sock_from_open_socket(
3620 &relayd
->control_sock
.sock
, fd
, relayd_socket_protocol
);
3622 /* Assign version values. */
3623 relayd
->control_sock
.major
= relayd_version_major
;
3624 relayd
->control_sock
.minor
= relayd_version_minor
;
3626 relayd
->relayd_session_id
= relayd_session_id
;
3629 case LTTNG_STREAM_DATA
:
3630 /* Copy received lttcomm socket */
3631 ret
= lttcomm_populate_sock_from_open_socket(
3632 &relayd
->data_sock
.sock
, fd
, relayd_socket_protocol
);
3633 /* Assign version values. */
3634 relayd
->data_sock
.major
= relayd_version_major
;
3635 relayd
->data_sock
.minor
= relayd_version_minor
;
3638 ERR("Unknown relayd socket type (%d)", sock_type
);
3639 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3644 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3648 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3649 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3650 relayd
->net_seq_idx
,
3653 * We gave the ownership of the fd to the relayd structure. Set the
3654 * fd to -1 so we don't call close() on it in the error path below.
3658 /* We successfully added the socket. Send status back. */
3659 ret
= consumer_send_status_msg(sock
, ret_code
);
3661 /* Somehow, the session daemon is not responding anymore. */
3662 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3663 goto error_nosignal
;
3667 * Add relayd socket pair to consumer data hashtable. If object already
3668 * exists or on error, the function gracefully returns.
3677 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3678 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3682 /* Close received socket if valid. */
3685 PERROR("close received socket");
3689 if (relayd_created
) {
3695 * Search for a relayd associated to the session id and return the reference.
3697 * A rcu read side lock MUST be acquire before calling this function and locked
3698 * until the relayd object is no longer necessary.
3700 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3702 struct lttng_ht_iter iter
;
3703 struct consumer_relayd_sock_pair
*relayd
= nullptr;
3705 ASSERT_RCU_READ_LOCKED();
3707 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3708 cds_lfht_for_each_entry (the_consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
, node
.node
) {
3710 * Check by sessiond id which is unique here where the relayd session
3711 * id might not be when having multiple relayd.
3713 if (relayd
->sessiond_session_id
== id
) {
3714 /* Found the relayd. There can be only one per id. */
3726 * Check if for a given session id there is still data needed to be extract
3729 * Return 1 if data is pending or else 0 meaning ready to be read.
3731 int consumer_data_pending(uint64_t id
)
3734 struct lttng_ht_iter iter
;
3735 struct lttng_ht
*ht
;
3736 struct lttng_consumer_stream
*stream
;
3737 struct consumer_relayd_sock_pair
*relayd
= nullptr;
3738 int (*data_pending
)(struct lttng_consumer_stream
*);
3740 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3742 const lttng::urcu::read_lock_guard read_lock
;
3743 pthread_mutex_lock(&the_consumer_data
.lock
);
3745 switch (the_consumer_data
.type
) {
3746 case LTTNG_CONSUMER_KERNEL
:
3747 data_pending
= lttng_kconsumer_data_pending
;
3749 case LTTNG_CONSUMER32_UST
:
3750 case LTTNG_CONSUMER64_UST
:
3751 data_pending
= lttng_ustconsumer_data_pending
;
3754 ERR("Unknown consumer data type");
3758 /* Ease our life a bit */
3759 ht
= the_consumer_data
.stream_list_ht
;
3761 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3762 ht
->hash_fct(&id
, lttng_ht_seed
),
3767 node_session_id
.node
)
3769 pthread_mutex_lock(&stream
->lock
);
3772 * A removed node from the hash table indicates that the stream has
3773 * been deleted thus having a guarantee that the buffers are closed
3774 * on the consumer side. However, data can still be transmitted
3775 * over the network so don't skip the relayd check.
3777 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3779 /* Check the stream if there is data in the buffers. */
3780 ret
= data_pending(stream
);
3782 pthread_mutex_unlock(&stream
->lock
);
3787 pthread_mutex_unlock(&stream
->lock
);
3790 relayd
= find_relayd_by_session_id(id
);
3792 unsigned int is_data_inflight
= 0;
3794 /* Send init command for data pending. */
3795 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3796 ret
= relayd_begin_data_pending(&relayd
->control_sock
, relayd
->relayd_session_id
);
3798 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3799 /* Communication error thus the relayd so no data pending. */
3800 goto data_not_pending
;
3803 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3804 ht
->hash_fct(&id
, lttng_ht_seed
),
3809 node_session_id
.node
)
3811 if (stream
->metadata_flag
) {
3812 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3813 stream
->relayd_stream_id
);
3815 ret
= relayd_data_pending(&relayd
->control_sock
,
3816 stream
->relayd_stream_id
,
3817 stream
->next_net_seq_num
- 1);
3821 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3823 } else if (ret
< 0) {
3824 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".",
3825 relayd
->net_seq_idx
);
3826 lttng_consumer_cleanup_relayd(relayd
);
3827 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3828 goto data_not_pending
;
3832 /* Send end command for data pending. */
3833 ret
= relayd_end_data_pending(
3834 &relayd
->control_sock
, relayd
->relayd_session_id
, &is_data_inflight
);
3835 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3837 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".",
3838 relayd
->net_seq_idx
);
3839 lttng_consumer_cleanup_relayd(relayd
);
3840 goto data_not_pending
;
3842 if (is_data_inflight
) {
3848 * Finding _no_ node in the hash table and no inflight data means that the
3849 * stream(s) have been removed thus data is guaranteed to be available for
3850 * analysis from the trace files.
3854 /* Data is available to be read by a viewer. */
3855 pthread_mutex_unlock(&the_consumer_data
.lock
);
3859 /* Data is still being extracted from buffers. */
3860 pthread_mutex_unlock(&the_consumer_data
.lock
);
3865 * Send a ret code status message to the sessiond daemon.
3867 * Return the sendmsg() return value.
3869 int consumer_send_status_msg(int sock
, int ret_code
)
3871 struct lttcomm_consumer_status_msg msg
;
3873 memset(&msg
, 0, sizeof(msg
));
3874 msg
.ret_code
= (lttcomm_return_code
) ret_code
;
3876 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3880 * Send a channel status message to the sessiond daemon.
3882 * Return the sendmsg() return value.
3884 int consumer_send_status_channel(int sock
, struct lttng_consumer_channel
*channel
)
3886 struct lttcomm_consumer_status_channel msg
;
3888 LTTNG_ASSERT(sock
>= 0);
3890 memset(&msg
, 0, sizeof(msg
));
3892 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3894 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3895 msg
.key
= channel
->key
;
3896 msg
.stream_count
= channel
->streams
.count
;
3899 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3902 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3903 unsigned long produced_pos
,
3904 uint64_t nb_packets_per_stream
,
3905 uint64_t max_sb_size
)
3907 unsigned long start_pos
;
3909 if (!nb_packets_per_stream
) {
3910 return consumed_pos
; /* Grab everything */
3912 start_pos
= produced_pos
- lttng_offset_align_floor(produced_pos
, max_sb_size
);
3913 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3914 if ((long) (start_pos
- consumed_pos
) < 0) {
3915 return consumed_pos
; /* Grab everything */
3920 /* Stream lock must be held by the caller. */
3921 static int sample_stream_positions(struct lttng_consumer_stream
*stream
,
3922 unsigned long *produced
,
3923 unsigned long *consumed
)
3927 ASSERT_LOCKED(stream
->lock
);
3929 ret
= lttng_consumer_sample_snapshot_positions(stream
);
3931 ERR("Failed to sample snapshot positions");
3935 ret
= lttng_consumer_get_produced_snapshot(stream
, produced
);
3937 ERR("Failed to sample produced position");
3941 ret
= lttng_consumer_get_consumed_snapshot(stream
, consumed
);
3943 ERR("Failed to sample consumed position");
3952 * Sample the rotate position for all the streams of a channel. If a stream
3953 * is already at the rotate position (produced == consumed), we flag it as
3954 * ready for rotation. The rotation of ready streams occurs after we have
3955 * replied to the session daemon that we have finished sampling the positions.
3956 * Must be called with RCU read-side lock held to ensure existence of channel.
3958 * Returns 0 on success, < 0 on error
3960 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
3965 struct lttng_consumer_stream
*stream
;
3966 struct lttng_ht_iter iter
;
3967 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
3968 struct lttng_dynamic_array stream_rotation_positions
;
3969 uint64_t next_chunk_id
, stream_count
= 0;
3970 enum lttng_trace_chunk_status chunk_status
;
3971 const bool is_local_trace
= relayd_id
== -1ULL;
3972 struct consumer_relayd_sock_pair
*relayd
= nullptr;
3973 bool rotating_to_new_chunk
= true;
3974 /* Array of `struct lttng_consumer_stream *` */
3975 struct lttng_dynamic_pointer_array streams_packet_to_open
;
3978 ASSERT_RCU_READ_LOCKED();
3980 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
3982 lttng_dynamic_array_init(&stream_rotation_positions
,
3983 sizeof(struct relayd_stream_rotation_position
),
3985 lttng_dynamic_pointer_array_init(&streams_packet_to_open
, nullptr);
3987 const lttng::urcu::read_lock_guard read_lock
;
3989 pthread_mutex_lock(&channel
->lock
);
3990 LTTNG_ASSERT(channel
->trace_chunk
);
3991 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
, &next_chunk_id
);
3992 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
3994 goto end_unlock_channel
;
3997 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3998 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4003 node_channel_id
.node
)
4005 unsigned long produced_pos
= 0, consumed_pos
= 0;
4007 health_code_update();
4010 * Lock stream because we are about to change its state.
4012 pthread_mutex_lock(&stream
->lock
);
4014 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
4015 rotating_to_new_chunk
= false;
4019 * Do not flush a packet when rotating from a NULL trace
4020 * chunk. The stream has no means to output data, and the prior
4021 * rotation which rotated to NULL performed that side-effect
4022 * already. No new data can be produced when a stream has no
4023 * associated trace chunk (e.g. a stop followed by a rotate).
4025 if (stream
->trace_chunk
) {
4028 if (stream
->metadata_flag
) {
4030 * Don't produce an empty metadata packet,
4031 * simply close the current one.
4033 * Metadata is regenerated on every trace chunk
4034 * switch; there is no concern that no data was
4037 flush_active
= true;
4040 * Only flush an empty packet if the "packet
4041 * open" could not be performed on transition
4042 * to a new trace chunk and no packets were
4043 * consumed within the chunk's lifetime.
4045 if (stream
->opened_packet_in_current_trace_chunk
) {
4046 flush_active
= true;
4049 * Stream could have been full at the
4050 * time of rotation, but then have had
4051 * no activity at all.
4053 * It is important to flush a packet
4054 * to prevent 0-length files from being
4055 * produced as most viewers choke on
4058 * Unfortunately viewers will not be
4059 * able to know that tracing was active
4060 * for this stream during this trace
4063 ret
= sample_stream_positions(
4064 stream
, &produced_pos
, &consumed_pos
);
4066 goto end_unlock_stream
;
4070 * Don't flush an empty packet if data
4071 * was produced; it will be consumed
4072 * before the rotation completes.
4074 flush_active
= produced_pos
!= consumed_pos
;
4075 if (!flush_active
) {
4076 const char *trace_chunk_name
;
4077 uint64_t trace_chunk_id
;
4079 chunk_status
= lttng_trace_chunk_get_name(
4080 stream
->trace_chunk
,
4083 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NONE
) {
4084 trace_chunk_name
= "none";
4088 * Consumer trace chunks are
4091 chunk_status
= lttng_trace_chunk_get_id(
4092 stream
->trace_chunk
, &trace_chunk_id
);
4093 LTTNG_ASSERT(chunk_status
==
4094 LTTNG_TRACE_CHUNK_STATUS_OK
);
4096 DBG("Unable to open packet for stream during trace chunk's lifetime. "
4097 "Flushing an empty packet to prevent an empty file from being created: "
4098 "stream id = %" PRIu64
4099 ", trace chunk name = `%s`, trace chunk id = %" PRIu64
,
4108 * Close the current packet before sampling the
4109 * ring buffer positions.
4111 ret
= consumer_stream_flush_buffer(stream
, flush_active
);
4113 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4115 goto end_unlock_stream
;
4119 ret
= lttng_consumer_take_snapshot(stream
);
4120 if (ret
< 0 && ret
!= -ENODATA
&& ret
!= -EAGAIN
) {
4121 ERR("Failed to sample snapshot position during channel rotation");
4122 goto end_unlock_stream
;
4125 ret
= lttng_consumer_get_produced_snapshot(stream
, &produced_pos
);
4127 ERR("Failed to sample produced position during channel rotation");
4128 goto end_unlock_stream
;
4131 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos
);
4133 ERR("Failed to sample consumed position during channel rotation");
4134 goto end_unlock_stream
;
4138 * Align produced position on the start-of-packet boundary of the first
4139 * packet going into the next trace chunk.
4141 produced_pos
= lttng_align_floor(produced_pos
, stream
->max_sb_size
);
4142 if (consumed_pos
== produced_pos
) {
4143 DBG("Set rotate ready for stream %" PRIu64
" produced = %lu consumed = %lu",
4147 stream
->rotate_ready
= true;
4149 DBG("Different consumed and produced positions "
4150 "for stream %" PRIu64
" produced = %lu consumed = %lu",
4156 * The rotation position is based on the packet_seq_num of the
4157 * packet following the last packet that was consumed for this
4158 * stream, incremented by the offset between produced and
4159 * consumed positions. This rotation position is a lower bound
4160 * (inclusive) at which the next trace chunk starts. Since it
4161 * is a lower bound, it is OK if the packet_seq_num does not
4162 * correspond exactly to the same packet identified by the
4163 * consumed_pos, which can happen in overwrite mode.
4165 if (stream
->sequence_number_unavailable
) {
4167 * Rotation should never be performed on a session which
4168 * interacts with a pre-2.8 lttng-modules, which does
4169 * not implement packet sequence number.
4171 ERR("Failure to rotate stream %" PRIu64
": sequence number unavailable",
4174 goto end_unlock_stream
;
4176 stream
->rotate_position
= stream
->last_sequence_number
+ 1 +
4177 ((produced_pos
- consumed_pos
) / stream
->max_sb_size
);
4178 DBG("Set rotation position for stream %" PRIu64
" at position %" PRIu64
,
4180 stream
->rotate_position
);
4182 if (!is_local_trace
) {
4184 * The relay daemon control protocol expects a rotation
4185 * position as "the sequence number of the first packet
4186 * _after_ the current trace chunk".
4188 const struct relayd_stream_rotation_position position
= {
4189 .stream_id
= stream
->relayd_stream_id
,
4190 .rotate_at_seq_num
= stream
->rotate_position
,
4193 ret
= lttng_dynamic_array_add_element(&stream_rotation_positions
,
4196 ERR("Failed to allocate stream rotation position");
4197 goto end_unlock_stream
;
4202 stream
->opened_packet_in_current_trace_chunk
= false;
4204 if (rotating_to_new_chunk
&& !stream
->metadata_flag
) {
4206 * Attempt to flush an empty packet as close to the
4207 * rotation point as possible. In the event where a
4208 * stream remains inactive after the rotation point,
4209 * this ensures that the new trace chunk has a
4210 * beginning timestamp set at the begining of the
4211 * trace chunk instead of only creating an empty
4212 * packet when the trace chunk is stopped.
4214 * This indicates to the viewers that the stream
4215 * was being recorded, but more importantly it
4216 * allows viewers to determine a useable trace
4219 * This presents a problem in the case where the
4220 * ring-buffer is completely full.
4222 * Consider the following scenario:
4223 * - The consumption of data is slow (slow network,
4225 * - The ring buffer is full,
4226 * - A rotation is initiated,
4227 * - The flush below does nothing (no space left to
4228 * open a new packet),
4229 * - The other streams rotate very soon, and new
4230 * data is produced in the new chunk,
4231 * - This stream completes its rotation long after the
4232 * rotation was initiated
4233 * - The session is stopped before any event can be
4234 * produced in this stream's buffers.
4236 * The resulting trace chunk will have a single packet
4237 * temporaly at the end of the trace chunk for this
4238 * stream making the stream intersection more narrow
4239 * than it should be.
4241 * To work-around this, an empty flush is performed
4242 * after the first consumption of a packet during a
4243 * rotation if open_packet fails. The idea is that
4244 * consuming a packet frees enough space to switch
4245 * packets in this scenario and allows the tracer to
4246 * "stamp" the beginning of the new trace chunk at the
4247 * earliest possible point.
4249 * The packet open is performed after the channel
4250 * rotation to ensure that no attempt to open a packet
4251 * is performed in a stream that has no active trace
4254 ret
= lttng_dynamic_pointer_array_add_pointer(&streams_packet_to_open
,
4257 PERROR("Failed to add a stream pointer to array of streams in which to open a packet");
4259 goto end_unlock_stream
;
4263 pthread_mutex_unlock(&stream
->lock
);
4267 if (!is_local_trace
) {
4268 relayd
= consumer_find_relayd(relayd_id
);
4270 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4272 goto end_unlock_channel
;
4275 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4276 ret
= relayd_rotate_streams(&relayd
->control_sock
,
4278 rotating_to_new_chunk
? &next_chunk_id
: nullptr,
4279 (const struct relayd_stream_rotation_position
*)
4280 stream_rotation_positions
.buffer
.data
);
4281 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4283 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4284 relayd
->net_seq_idx
);
4285 lttng_consumer_cleanup_relayd(relayd
);
4286 goto end_unlock_channel
;
4290 for (stream_idx
= 0;
4291 stream_idx
< lttng_dynamic_pointer_array_get_count(&streams_packet_to_open
);
4293 enum consumer_stream_open_packet_status status
;
4295 stream
= (lttng_consumer_stream
*) lttng_dynamic_pointer_array_get_pointer(
4296 &streams_packet_to_open
, stream_idx
);
4298 pthread_mutex_lock(&stream
->lock
);
4299 status
= consumer_stream_open_packet(stream
);
4300 pthread_mutex_unlock(&stream
->lock
);
4302 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
4303 DBG("Opened a packet after a rotation: stream id = %" PRIu64
4304 ", channel name = %s, session id = %" PRIu64
,
4307 stream
->chan
->session_id
);
4309 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
4311 * Can't open a packet as there is no space left
4312 * in the buffer. A new packet will be opened
4313 * once one has been consumed.
4315 DBG("No space left to open a packet after a rotation: stream id = %" PRIu64
4316 ", channel name = %s, session id = %" PRIu64
,
4319 stream
->chan
->session_id
);
4321 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
4322 /* Logged by callee. */
4324 goto end_unlock_channel
;
4330 pthread_mutex_unlock(&channel
->lock
);
4335 pthread_mutex_unlock(&stream
->lock
);
4337 pthread_mutex_unlock(&channel
->lock
);
4339 lttng_dynamic_array_reset(&stream_rotation_positions
);
4340 lttng_dynamic_pointer_array_reset(&streams_packet_to_open
);
4344 static int consumer_clear_buffer(struct lttng_consumer_stream
*stream
)
4347 unsigned long consumed_pos_before
, consumed_pos_after
;
4349 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4351 ERR("Taking snapshot positions");
4355 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_before
);
4357 ERR("Consumed snapshot position");
4361 switch (the_consumer_data
.type
) {
4362 case LTTNG_CONSUMER_KERNEL
:
4363 ret
= kernctl_buffer_clear(stream
->wait_fd
);
4365 ERR("Failed to clear kernel stream (ret = %d)", ret
);
4369 case LTTNG_CONSUMER32_UST
:
4370 case LTTNG_CONSUMER64_UST
:
4371 ret
= lttng_ustconsumer_clear_buffer(stream
);
4373 ERR("Failed to clear ust stream (ret = %d)", ret
);
4378 ERR("Unknown consumer_data type");
4382 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4384 ERR("Taking snapshot positions");
4387 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_after
);
4389 ERR("Consumed snapshot position");
4392 DBG("clear: before: %lu after: %lu", consumed_pos_before
, consumed_pos_after
);
4397 static int consumer_clear_stream(struct lttng_consumer_stream
*stream
)
4401 ret
= consumer_stream_flush_buffer(stream
, true);
4403 ERR("Failed to flush stream %" PRIu64
" during channel clear", stream
->key
);
4404 ret
= LTTCOMM_CONSUMERD_FATAL
;
4408 ret
= consumer_clear_buffer(stream
);
4410 ERR("Failed to clear stream %" PRIu64
" during channel clear", stream
->key
);
4411 ret
= LTTCOMM_CONSUMERD_FATAL
;
4415 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4420 static int consumer_clear_unmonitored_channel(struct lttng_consumer_channel
*channel
)
4423 struct lttng_consumer_stream
*stream
;
4425 const lttng::urcu::read_lock_guard read_lock
;
4426 pthread_mutex_lock(&channel
->lock
);
4427 cds_list_for_each_entry (stream
, &channel
->streams
.head
, send_node
) {
4428 health_code_update();
4429 pthread_mutex_lock(&stream
->lock
);
4430 ret
= consumer_clear_stream(stream
);
4434 pthread_mutex_unlock(&stream
->lock
);
4436 pthread_mutex_unlock(&channel
->lock
);
4440 pthread_mutex_unlock(&stream
->lock
);
4441 pthread_mutex_unlock(&channel
->lock
);
4446 * Check if a stream is ready to be rotated after extracting it.
4448 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4449 * error. Stream lock must be held.
4451 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4453 DBG("Check is rotate ready for stream %" PRIu64
" ready %u rotate_position %" PRIu64
4454 " last_sequence_number %" PRIu64
,
4456 stream
->rotate_ready
,
4457 stream
->rotate_position
,
4458 stream
->last_sequence_number
);
4459 if (stream
->rotate_ready
) {
4464 * If packet seq num is unavailable, it means we are interacting
4465 * with a pre-2.8 lttng-modules which does not implement the
4466 * sequence number. Rotation should never be used by sessiond in this
4469 if (stream
->sequence_number_unavailable
) {
4470 ERR("Internal error: rotation used on stream %" PRIu64
4471 " with unavailable sequence number",
4476 if (stream
->rotate_position
== -1ULL || stream
->last_sequence_number
== -1ULL) {
4481 * Rotate position not reached yet. The stream rotate position is
4482 * the position of the next packet belonging to the next trace chunk,
4483 * but consumerd considers rotation ready when reaching the last
4484 * packet of the current chunk, hence the "rotate_position - 1".
4487 DBG("Check is rotate ready for stream %" PRIu64
" last_sequence_number %" PRIu64
4488 " rotate_position %" PRIu64
,
4490 stream
->last_sequence_number
,
4491 stream
->rotate_position
);
4492 if (stream
->last_sequence_number
>= stream
->rotate_position
- 1) {
4500 * Reset the state for a stream after a rotation occurred.
4502 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4504 DBG("lttng_consumer_reset_stream_rotate_state for stream %" PRIu64
, stream
->key
);
4505 stream
->rotate_position
= -1ULL;
4506 stream
->rotate_ready
= false;
4510 * Perform the rotation a local stream file.
4512 static int rotate_local_stream(struct lttng_consumer_stream
*stream
)
4516 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4519 stream
->tracefile_size_current
= 0;
4520 stream
->tracefile_count_current
= 0;
4522 if (stream
->out_fd
>= 0) {
4523 ret
= close(stream
->out_fd
);
4525 PERROR("Failed to close stream out_fd of channel \"%s\"",
4526 stream
->chan
->name
);
4528 stream
->out_fd
= -1;
4531 if (stream
->index_file
) {
4532 lttng_index_file_put(stream
->index_file
);
4533 stream
->index_file
= nullptr;
4536 if (!stream
->trace_chunk
) {
4540 ret
= consumer_stream_create_output_files(stream
, true);
4546 * Performs the stream rotation for the rotate session feature if needed.
4547 * It must be called with the channel and stream locks held.
4549 * Return 0 on success, a negative number of error.
4551 int lttng_consumer_rotate_stream(struct lttng_consumer_stream
*stream
)
4555 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4558 * Update the stream's 'current' chunk to the session's (channel)
4559 * now-current chunk.
4561 lttng_trace_chunk_put(stream
->trace_chunk
);
4562 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4564 * A channel can be rotated and not have a "next" chunk
4565 * to transition to. In that case, the channel's "current chunk"
4566 * has not been closed yet, but it has not been updated to
4567 * a "next" trace chunk either. Hence, the stream, like its
4568 * parent channel, becomes part of no chunk and can't output
4569 * anything until a new trace chunk is created.
4571 stream
->trace_chunk
= nullptr;
4572 } else if (stream
->chan
->trace_chunk
&& !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4573 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4578 * Update the stream's trace chunk to its parent channel's
4579 * current trace chunk.
4581 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4584 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4585 ret
= rotate_local_stream(stream
);
4587 ERR("Failed to rotate stream, ret = %i", ret
);
4592 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4594 * If the stream has transitioned to a new trace
4595 * chunk, the metadata should be re-dumped to the
4598 * However, it is possible for a stream to transition to
4599 * a "no-chunk" state. This can happen if a rotation
4600 * occurs on an inactive session. In such cases, the metadata
4601 * regeneration will happen when the next trace chunk is
4604 ret
= consumer_metadata_stream_dump(stream
);
4609 lttng_consumer_reset_stream_rotate_state(stream
);
4618 * Rotate all the ready streams now.
4620 * This is especially important for low throughput streams that have already
4621 * been consumed, we cannot wait for their next packet to perform the
4623 * Need to be called with RCU read-side lock held to ensure existence of
4626 * Returns 0 on success, < 0 on error
4628 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
, uint64_t key
)
4631 struct lttng_consumer_stream
*stream
;
4632 struct lttng_ht_iter iter
;
4633 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
4635 ASSERT_RCU_READ_LOCKED();
4637 const lttng::urcu::read_lock_guard read_lock
;
4639 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4641 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4642 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4647 node_channel_id
.node
)
4649 health_code_update();
4651 pthread_mutex_lock(&stream
->chan
->lock
);
4652 pthread_mutex_lock(&stream
->lock
);
4654 if (!stream
->rotate_ready
) {
4655 pthread_mutex_unlock(&stream
->lock
);
4656 pthread_mutex_unlock(&stream
->chan
->lock
);
4659 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4661 ret
= lttng_consumer_rotate_stream(stream
);
4662 pthread_mutex_unlock(&stream
->lock
);
4663 pthread_mutex_unlock(&stream
->chan
->lock
);
4675 enum lttcomm_return_code
lttng_consumer_init_command(struct lttng_consumer_local_data
*ctx
,
4676 const lttng_uuid
& sessiond_uuid
)
4678 enum lttcomm_return_code ret
;
4679 char uuid_str
[LTTNG_UUID_STR_LEN
];
4681 if (ctx
->sessiond_uuid
.is_set
) {
4682 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4686 ctx
->sessiond_uuid
.is_set
= true;
4687 ctx
->sessiond_uuid
.value
= sessiond_uuid
;
4688 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4689 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4690 DBG("Received session daemon UUID: %s", uuid_str
);
4695 enum lttcomm_return_code
4696 lttng_consumer_create_trace_chunk(const uint64_t *relayd_id
,
4697 uint64_t session_id
,
4699 time_t chunk_creation_timestamp
,
4700 const char *chunk_override_name
,
4701 const struct lttng_credentials
*credentials
,
4702 struct lttng_directory_handle
*chunk_directory_handle
)
4705 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4706 struct lttng_trace_chunk
*created_chunk
= nullptr, *published_chunk
= nullptr;
4707 enum lttng_trace_chunk_status chunk_status
;
4708 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4709 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4710 const char *relayd_id_str
= "(none)";
4711 const char *creation_timestamp_str
;
4712 struct lttng_ht_iter iter
;
4713 struct lttng_consumer_channel
*channel
;
4716 /* Only used for logging purposes. */
4717 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
), "%" PRIu64
, *relayd_id
);
4718 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4719 relayd_id_str
= relayd_id_buffer
;
4721 relayd_id_str
= "(formatting error)";
4725 /* Local protocol error. */
4726 LTTNG_ASSERT(chunk_creation_timestamp
);
4727 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4728 creation_timestamp_buffer
,
4729 sizeof(creation_timestamp_buffer
));
4730 creation_timestamp_str
= !ret
? creation_timestamp_buffer
: "(formatting error)";
4732 DBG("Consumer create trace chunk command: relay_id = %s"
4733 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
", chunk_override_name = %s"
4734 ", chunk_creation_timestamp = %s",
4738 chunk_override_name
?: "(none)",
4739 creation_timestamp_str
);
4742 * The trace chunk registry, as used by the consumer daemon, implicitly
4743 * owns the trace chunks. This is only needed in the consumer since
4744 * the consumer has no notion of a session beyond session IDs being
4745 * used to identify other objects.
4747 * The lttng_trace_chunk_registry_publish() call below provides a
4748 * reference which is not released; it implicitly becomes the session
4749 * daemon's reference to the chunk in the consumer daemon.
4751 * The lifetime of trace chunks in the consumer daemon is managed by
4752 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4753 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4755 created_chunk
= lttng_trace_chunk_create(chunk_id
, chunk_creation_timestamp
, nullptr);
4756 if (!created_chunk
) {
4757 ERR("Failed to create trace chunk");
4758 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4762 if (chunk_override_name
) {
4763 chunk_status
= lttng_trace_chunk_override_name(created_chunk
, chunk_override_name
);
4764 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4765 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4770 if (chunk_directory_handle
) {
4771 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
, credentials
);
4772 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4773 ERR("Failed to set trace chunk credentials");
4774 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4778 * The consumer daemon has no ownership of the chunk output
4781 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
, chunk_directory_handle
);
4782 chunk_directory_handle
= nullptr;
4783 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4784 ERR("Failed to set trace chunk's directory handle");
4785 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4790 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4791 the_consumer_data
.chunk_registry
, session_id
, created_chunk
);
4792 lttng_trace_chunk_put(created_chunk
);
4793 created_chunk
= nullptr;
4794 if (!published_chunk
) {
4795 ERR("Failed to publish trace chunk");
4796 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4801 const lttng::urcu::read_lock_guard read_lock
;
4802 cds_lfht_for_each_entry_duplicate(
4803 the_consumer_data
.channels_by_session_id_ht
->ht
,
4804 the_consumer_data
.channels_by_session_id_ht
->hash_fct(&session_id
,
4806 the_consumer_data
.channels_by_session_id_ht
->match_fct
,
4810 channels_by_session_id_ht_node
.node
)
4812 ret
= lttng_consumer_channel_set_trace_chunk(channel
, published_chunk
);
4815 * Roll-back the creation of this chunk.
4817 * This is important since the session daemon will
4818 * assume that the creation of this chunk failed and
4819 * will never ask for it to be closed, resulting
4820 * in a leak and an inconsistent state for some
4823 enum lttcomm_return_code close_ret
;
4824 char path
[LTTNG_PATH_MAX
];
4826 DBG("Failed to set new trace chunk on existing channels, rolling back");
4828 lttng_consumer_close_trace_chunk(relayd_id
,
4831 chunk_creation_timestamp
,
4834 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4835 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
4836 ", chunk_id = %" PRIu64
,
4841 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4848 struct consumer_relayd_sock_pair
*relayd
;
4850 relayd
= consumer_find_relayd(*relayd_id
);
4852 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4853 ret
= relayd_create_trace_chunk(&relayd
->control_sock
, published_chunk
);
4854 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4856 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4859 if (!relayd
|| ret
) {
4860 enum lttcomm_return_code close_ret
;
4861 char path
[LTTNG_PATH_MAX
];
4863 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4866 chunk_creation_timestamp
,
4869 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4870 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
4871 ", chunk_id = %" PRIu64
,
4876 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4882 /* Release the reference returned by the "publish" operation. */
4883 lttng_trace_chunk_put(published_chunk
);
4884 lttng_trace_chunk_put(created_chunk
);
4888 enum lttcomm_return_code
4889 lttng_consumer_close_trace_chunk(const uint64_t *relayd_id
,
4890 uint64_t session_id
,
4892 time_t chunk_close_timestamp
,
4893 const enum lttng_trace_chunk_command_type
*close_command
,
4896 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4897 struct lttng_trace_chunk
*chunk
;
4898 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4899 const char *relayd_id_str
= "(none)";
4900 const char *close_command_name
= "none";
4901 struct lttng_ht_iter iter
;
4902 struct lttng_consumer_channel
*channel
;
4903 enum lttng_trace_chunk_status chunk_status
;
4908 /* Only used for logging purposes. */
4909 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
), "%" PRIu64
, *relayd_id
);
4910 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4911 relayd_id_str
= relayd_id_buffer
;
4913 relayd_id_str
= "(formatting error)";
4916 if (close_command
) {
4917 close_command_name
= lttng_trace_chunk_command_type_get_name(*close_command
);
4920 DBG("Consumer close trace chunk command: relayd_id = %s"
4921 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
", close command = %s",
4925 close_command_name
);
4927 chunk
= lttng_trace_chunk_registry_find_chunk(
4928 the_consumer_data
.chunk_registry
, session_id
, chunk_id
);
4930 ERR("Failed to find chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4933 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4937 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
, chunk_close_timestamp
);
4938 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4939 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4943 if (close_command
) {
4944 chunk_status
= lttng_trace_chunk_set_close_command(chunk
, *close_command
);
4945 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4946 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4952 * chunk is now invalid to access as we no longer hold a reference to
4953 * it; it is only kept around to compare it (by address) to the
4954 * current chunk found in the session's channels.
4957 const lttng::urcu::read_lock_guard read_lock
;
4958 cds_lfht_for_each_entry (
4959 the_consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
, node
.node
) {
4963 * Only change the channel's chunk to NULL if it still
4964 * references the chunk being closed. The channel may
4965 * reference a newer channel in the case of a session
4966 * rotation. When a session rotation occurs, the "next"
4967 * chunk is created before the "current" chunk is closed.
4969 if (channel
->trace_chunk
!= chunk
) {
4972 ret
= lttng_consumer_channel_set_trace_chunk(channel
, nullptr);
4975 * Attempt to close the chunk on as many channels as
4978 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4984 struct consumer_relayd_sock_pair
*relayd
;
4986 relayd
= consumer_find_relayd(*relayd_id
);
4988 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4989 ret
= relayd_close_trace_chunk(&relayd
->control_sock
, chunk
, path
);
4990 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4992 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4995 if (!relayd
|| ret
) {
4996 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5003 * Release the reference returned by the "find" operation and
5004 * the session daemon's implicit reference to the chunk.
5006 lttng_trace_chunk_put(chunk
);
5007 lttng_trace_chunk_put(chunk
);
5012 enum lttcomm_return_code
5013 lttng_consumer_trace_chunk_exists(const uint64_t *relayd_id
, uint64_t session_id
, uint64_t chunk_id
)
5016 enum lttcomm_return_code ret_code
;
5017 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
5018 const char *relayd_id_str
= "(none)";
5019 const bool is_local_trace
= !relayd_id
;
5020 struct consumer_relayd_sock_pair
*relayd
= nullptr;
5021 bool chunk_exists_local
, chunk_exists_remote
;
5022 const lttng::urcu::read_lock_guard read_lock
;
5025 /* Only used for logging purposes. */
5026 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
), "%" PRIu64
, *relayd_id
);
5027 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
5028 relayd_id_str
= relayd_id_buffer
;
5030 relayd_id_str
= "(formatting error)";
5034 DBG("Consumer trace chunk exists command: relayd_id = %s"
5035 ", chunk_id = %" PRIu64
,
5038 ret
= lttng_trace_chunk_registry_chunk_exists(
5039 the_consumer_data
.chunk_registry
, session_id
, chunk_id
, &chunk_exists_local
);
5041 /* Internal error. */
5042 ERR("Failed to query the existence of a trace chunk");
5043 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
5046 DBG("Trace chunk %s locally", chunk_exists_local
? "exists" : "does not exist");
5047 if (chunk_exists_local
) {
5048 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
5050 } else if (is_local_trace
) {
5051 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5055 relayd
= consumer_find_relayd(*relayd_id
);
5057 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
5058 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5059 goto end_rcu_unlock
;
5061 DBG("Looking up existence of trace chunk on relay daemon");
5062 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5063 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
, &chunk_exists_remote
);
5064 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5066 ERR("Failed to look-up the existence of trace chunk on relay daemon");
5067 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
5068 goto end_rcu_unlock
;
5071 ret_code
= chunk_exists_remote
? LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
5072 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5073 DBG("Trace chunk %s on relay daemon", chunk_exists_remote
? "exists" : "does not exist");
5080 static int consumer_clear_monitored_channel(struct lttng_consumer_channel
*channel
)
5082 struct lttng_ht
*ht
;
5083 struct lttng_consumer_stream
*stream
;
5084 struct lttng_ht_iter iter
;
5087 ht
= the_consumer_data
.stream_per_chan_id_ht
;
5089 const lttng::urcu::read_lock_guard read_lock
;
5090 cds_lfht_for_each_entry_duplicate(ht
->ht
,
5091 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
5096 node_channel_id
.node
)
5099 * Protect against teardown with mutex.
5101 pthread_mutex_lock(&stream
->lock
);
5102 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5105 ret
= consumer_clear_stream(stream
);
5110 pthread_mutex_unlock(&stream
->lock
);
5112 return LTTCOMM_CONSUMERD_SUCCESS
;
5115 pthread_mutex_unlock(&stream
->lock
);
5119 int lttng_consumer_clear_channel(struct lttng_consumer_channel
*channel
)
5123 DBG("Consumer clear channel %" PRIu64
, channel
->key
);
5125 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
5127 * Nothing to do for the metadata channel/stream.
5128 * Snapshot mechanism already take care of the metadata
5129 * handling/generation, and monitored channels only need to
5130 * have their data stream cleared..
5132 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5136 if (!channel
->monitor
) {
5137 ret
= consumer_clear_unmonitored_channel(channel
);
5139 ret
= consumer_clear_monitored_channel(channel
);
5145 enum lttcomm_return_code
lttng_consumer_open_channel_packets(struct lttng_consumer_channel
*channel
)
5147 struct lttng_consumer_stream
*stream
;
5148 enum lttcomm_return_code ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5150 if (channel
->metadata_stream
) {
5151 ERR("Open channel packets command attempted on a metadata channel");
5152 ret
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5157 const lttng::urcu::read_lock_guard read_lock
;
5158 cds_list_for_each_entry (stream
, &channel
->streams
.head
, send_node
) {
5159 enum consumer_stream_open_packet_status status
;
5161 pthread_mutex_lock(&stream
->lock
);
5162 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5166 status
= consumer_stream_open_packet(stream
);
5168 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
5169 DBG("Opened a packet in \"open channel packets\" command: stream id = %" PRIu64
5170 ", channel name = %s, session id = %" PRIu64
,
5173 stream
->chan
->session_id
);
5174 stream
->opened_packet_in_current_trace_chunk
= true;
5176 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
5177 DBG("No space left to open a packet in \"open channel packets\" command: stream id = %" PRIu64
5178 ", channel name = %s, session id = %" PRIu64
,
5181 stream
->chan
->session_id
);
5183 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
5185 * Only unexpected internal errors can lead to this
5186 * failing. Report an unknown error.
5188 ERR("Failed to flush empty buffer in \"open channel packets\" command: stream id = %" PRIu64
5189 ", channel id = %" PRIu64
", channel name = %s"
5190 ", session id = %" PRIu64
,
5194 channel
->session_id
);
5195 ret
= LTTCOMM_CONSUMERD_UNKNOWN_ERROR
;
5202 pthread_mutex_unlock(&stream
->lock
);
5210 pthread_mutex_unlock(&stream
->lock
);
5211 goto end_rcu_unlock
;
5214 void lttng_consumer_sigbus_handle(void *addr
)
5216 lttng_ustconsumer_sigbus_handle(addr
);