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
18 #include <sys/socket.h>
19 #include <sys/types.h>
22 #include <bin/lttng-consumerd/health-consumerd.hpp>
23 #include <common/align.hpp>
24 #include <common/common.hpp>
25 #include <common/compat/endian.hpp>
26 #include <common/compat/poll.hpp>
27 #include <common/consumer/consumer-metadata-cache.hpp>
28 #include <common/consumer/consumer-stream.hpp>
29 #include <common/consumer/consumer-testpoint.hpp>
30 #include <common/consumer/consumer-timer.hpp>
31 #include <common/consumer/consumer.hpp>
32 #include <common/dynamic-array.hpp>
33 #include <common/index/ctf-index.hpp>
34 #include <common/index/index.hpp>
35 #include <common/kernel-consumer/kernel-consumer.hpp>
36 #include <common/kernel-ctl/kernel-ctl.hpp>
37 #include <common/relayd/relayd.hpp>
38 #include <common/sessiond-comm/relayd.hpp>
39 #include <common/sessiond-comm/sessiond-comm.hpp>
40 #include <common/string-utils/format.hpp>
41 #include <common/time.hpp>
42 #include <common/trace-chunk-registry.hpp>
43 #include <common/trace-chunk.hpp>
44 #include <common/ust-consumer/ust-consumer.hpp>
45 #include <common/utils.hpp>
47 lttng_consumer_global_data the_consumer_data
;
49 enum consumer_channel_action
{
52 CONSUMER_CHANNEL_QUIT
,
56 struct consumer_channel_msg
{
57 enum consumer_channel_action action
;
58 struct lttng_consumer_channel
*chan
; /* add */
59 uint64_t key
; /* del */
63 * Global hash table containing respectively metadata and data streams. The
64 * stream element in this ht should only be updated by the metadata poll thread
65 * for the metadata and the data poll thread for the data.
67 struct lttng_ht
*metadata_ht
;
68 struct lttng_ht
*data_ht
;
71 /* Flag used to temporarily pause data consumption from testpoints. */
72 int data_consumption_paused
;
75 * Flag to inform the polling thread to quit when all fd hung up. Updated by
76 * the consumer_thread_receive_fds when it notices that all fds has hung up.
77 * Also updated by the signal handler (consumer_should_exit()). Read by the
82 static const char *get_consumer_domain(void)
84 switch (the_consumer_data
.type
) {
85 case LTTNG_CONSUMER_KERNEL
:
86 return DEFAULT_KERNEL_TRACE_DIR
;
87 case LTTNG_CONSUMER64_UST
:
89 case LTTNG_CONSUMER32_UST
:
90 return DEFAULT_UST_TRACE_DIR
;
97 * Notify a thread lttng pipe to poll back again. This usually means that some
98 * global state has changed so we just send back the thread in a poll wait
101 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
103 struct lttng_consumer_stream
*null_stream
= NULL
;
107 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
110 static void notify_health_quit_pipe(int *pipe
)
114 ret
= lttng_write(pipe
[1], "4", 1);
116 PERROR("write consumer health quit");
120 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
121 struct lttng_consumer_channel
*chan
,
123 enum consumer_channel_action action
)
125 struct consumer_channel_msg msg
;
128 memset(&msg
, 0, sizeof(msg
));
133 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
134 if (ret
< sizeof(msg
)) {
135 PERROR("notify_channel_pipe write error");
139 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
142 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
145 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
146 struct lttng_consumer_channel
**chan
,
148 enum consumer_channel_action
*action
)
150 struct consumer_channel_msg msg
;
153 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
154 if (ret
< sizeof(msg
)) {
158 *action
= msg
.action
;
166 * Cleanup the stream list of a channel. Those streams are not yet globally
169 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
171 struct lttng_consumer_stream
*stream
, *stmp
;
173 LTTNG_ASSERT(channel
);
175 /* Delete streams that might have been left in the stream list. */
176 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
179 * Once a stream is added to this list, the buffers were created so we
180 * have a guarantee that this call will succeed. Setting the monitor
181 * mode to 0 so we don't lock nor try to delete the stream from the
185 consumer_stream_destroy(stream
, NULL
);
190 * Find a stream. The consumer_data.lock must be locked during this
193 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
196 struct lttng_ht_iter iter
;
197 struct lttng_ht_node_u64
*node
;
198 struct lttng_consumer_stream
*stream
= NULL
;
202 /* -1ULL keys are lookup failures */
203 if (key
== (uint64_t) -1ULL) {
209 lttng_ht_lookup(ht
, &key
, &iter
);
210 node
= lttng_ht_iter_get_node_u64(&iter
);
212 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
220 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
222 struct lttng_consumer_stream
*stream
;
225 stream
= find_stream(key
, ht
);
227 stream
->key
= (uint64_t) -1ULL;
229 * We don't want the lookup to match, but we still need
230 * to iterate on this stream when iterating over the hash table. Just
231 * change the node key.
233 stream
->node
.key
= (uint64_t) -1ULL;
239 * Return a channel object for the given key.
241 * RCU read side lock MUST be acquired before calling this function and
242 * protects the channel ptr.
244 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
246 struct lttng_ht_iter iter
;
247 struct lttng_ht_node_u64
*node
;
248 struct lttng_consumer_channel
*channel
= NULL
;
250 ASSERT_RCU_READ_LOCKED();
252 /* -1ULL keys are lookup failures */
253 if (key
== (uint64_t) -1ULL) {
257 lttng_ht_lookup(the_consumer_data
.channel_ht
, &key
, &iter
);
258 node
= lttng_ht_iter_get_node_u64(&iter
);
260 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
267 * There is a possibility that the consumer does not have enough time between
268 * the close of the channel on the session daemon and the cleanup in here thus
269 * once we have a channel add with an existing key, we know for sure that this
270 * channel will eventually get cleaned up by all streams being closed.
272 * This function just nullifies the already existing channel key.
274 static void steal_channel_key(uint64_t key
)
276 struct lttng_consumer_channel
*channel
;
279 channel
= consumer_find_channel(key
);
281 channel
->key
= (uint64_t) -1ULL;
283 * We don't want the lookup to match, but we still need to iterate on
284 * this channel when iterating over the hash table. Just change the
287 channel
->node
.key
= (uint64_t) -1ULL;
292 static void free_channel_rcu(struct rcu_head
*head
)
294 struct lttng_ht_node_u64
*node
=
295 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
296 struct lttng_consumer_channel
*channel
=
297 caa_container_of(node
, struct lttng_consumer_channel
, node
);
299 switch (the_consumer_data
.type
) {
300 case LTTNG_CONSUMER_KERNEL
:
302 case LTTNG_CONSUMER32_UST
:
303 case LTTNG_CONSUMER64_UST
:
304 lttng_ustconsumer_free_channel(channel
);
307 ERR("Unknown consumer_data type");
314 * RCU protected relayd socket pair free.
316 static void free_relayd_rcu(struct rcu_head
*head
)
318 struct lttng_ht_node_u64
*node
=
319 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
320 struct consumer_relayd_sock_pair
*relayd
=
321 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
324 * Close all sockets. This is done in the call RCU since we don't want the
325 * socket fds to be reassigned thus potentially creating bad state of the
328 * We do not have to lock the control socket mutex here since at this stage
329 * there is no one referencing to this relayd object.
331 (void) relayd_close(&relayd
->control_sock
);
332 (void) relayd_close(&relayd
->data_sock
);
334 pthread_mutex_destroy(&relayd
->ctrl_sock_mutex
);
339 * Destroy and free relayd socket pair object.
341 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
344 struct lttng_ht_iter iter
;
346 if (relayd
== NULL
) {
350 DBG("Consumer destroy and close relayd socket pair");
352 iter
.iter
.node
= &relayd
->node
.node
;
353 ret
= lttng_ht_del(the_consumer_data
.relayd_ht
, &iter
);
355 /* We assume the relayd is being or is destroyed */
359 /* RCU free() call */
360 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
364 * Remove a channel from the global list protected by a mutex. This function is
365 * also responsible for freeing its data structures.
367 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
369 struct lttng_ht_iter iter
;
371 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
373 pthread_mutex_lock(&the_consumer_data
.lock
);
374 pthread_mutex_lock(&channel
->lock
);
376 /* Destroy streams that might have been left in the stream list. */
377 clean_channel_stream_list(channel
);
379 if (channel
->live_timer_enabled
== 1) {
380 consumer_timer_live_stop(channel
);
382 if (channel
->monitor_timer_enabled
== 1) {
383 consumer_timer_monitor_stop(channel
);
386 switch (the_consumer_data
.type
) {
387 case LTTNG_CONSUMER_KERNEL
:
389 case LTTNG_CONSUMER32_UST
:
390 case LTTNG_CONSUMER64_UST
:
391 lttng_ustconsumer_del_channel(channel
);
394 ERR("Unknown consumer_data type");
399 lttng_trace_chunk_put(channel
->trace_chunk
);
400 channel
->trace_chunk
= NULL
;
402 if (channel
->is_published
) {
406 iter
.iter
.node
= &channel
->node
.node
;
407 ret
= lttng_ht_del(the_consumer_data
.channel_ht
, &iter
);
410 iter
.iter
.node
= &channel
->channels_by_session_id_ht_node
.node
;
411 ret
= lttng_ht_del(the_consumer_data
.channels_by_session_id_ht
,
417 channel
->is_deleted
= true;
418 call_rcu(&channel
->node
.head
, free_channel_rcu
);
420 pthread_mutex_unlock(&channel
->lock
);
421 pthread_mutex_unlock(&the_consumer_data
.lock
);
425 * Iterate over the relayd hash table and destroy each element. Finally,
426 * destroy the whole hash table.
428 static void cleanup_relayd_ht(void)
430 struct lttng_ht_iter iter
;
431 struct consumer_relayd_sock_pair
*relayd
;
435 cds_lfht_for_each_entry(the_consumer_data
.relayd_ht
->ht
, &iter
.iter
,
437 consumer_destroy_relayd(relayd
);
442 lttng_ht_destroy(the_consumer_data
.relayd_ht
);
446 * Update the end point status of all streams having the given network sequence
447 * index (relayd index).
449 * It's atomically set without having the stream mutex locked which is fine
450 * because we handle the write/read race with a pipe wakeup for each thread.
452 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
453 enum consumer_endpoint_status status
)
455 struct lttng_ht_iter iter
;
456 struct lttng_consumer_stream
*stream
;
458 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
462 /* Let's begin with metadata */
463 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
464 if (stream
->net_seq_idx
== net_seq_idx
) {
465 uatomic_set(&stream
->endpoint_status
, status
);
466 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
470 /* Follow up by the data streams */
471 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
472 if (stream
->net_seq_idx
== net_seq_idx
) {
473 uatomic_set(&stream
->endpoint_status
, status
);
474 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
481 * Cleanup a relayd object by flagging every associated streams for deletion,
482 * destroying the object meaning removing it from the relayd hash table,
483 * closing the sockets and freeing the memory in a RCU call.
485 * If a local data context is available, notify the threads that the streams'
486 * state have changed.
488 void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
)
492 LTTNG_ASSERT(relayd
);
494 DBG("Cleaning up relayd object ID %" PRIu64
, relayd
->net_seq_idx
);
496 /* Save the net sequence index before destroying the object */
497 netidx
= relayd
->net_seq_idx
;
500 * Delete the relayd from the relayd hash table, close the sockets and free
501 * the object in a RCU call.
503 consumer_destroy_relayd(relayd
);
505 /* Set inactive endpoint to all streams */
506 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
509 * With a local data context, notify the threads that the streams' state
510 * have changed. The write() action on the pipe acts as an "implicit"
511 * memory barrier ordering the updates of the end point status from the
512 * read of this status which happens AFTER receiving this notify.
514 notify_thread_lttng_pipe(relayd
->ctx
->consumer_data_pipe
);
515 notify_thread_lttng_pipe(relayd
->ctx
->consumer_metadata_pipe
);
519 * Flag a relayd socket pair for destruction. Destroy it if the refcount
522 * RCU read side lock MUST be aquired before calling this function.
524 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
526 LTTNG_ASSERT(relayd
);
527 ASSERT_RCU_READ_LOCKED();
529 /* Set destroy flag for this object */
530 uatomic_set(&relayd
->destroy_flag
, 1);
532 /* Destroy the relayd if refcount is 0 */
533 if (uatomic_read(&relayd
->refcount
) == 0) {
534 consumer_destroy_relayd(relayd
);
539 * Completly destroy stream from every visiable data structure and the given
542 * One this call returns, the stream object is not longer usable nor visible.
544 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
547 consumer_stream_destroy(stream
, ht
);
551 * XXX naming of del vs destroy is all mixed up.
553 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
555 consumer_stream_destroy(stream
, data_ht
);
558 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
560 consumer_stream_destroy(stream
, metadata_ht
);
563 void consumer_stream_update_channel_attributes(
564 struct lttng_consumer_stream
*stream
,
565 struct lttng_consumer_channel
*channel
)
567 stream
->channel_read_only_attributes
.tracefile_size
=
568 channel
->tracefile_size
;
572 * Add a stream to the global list protected by a mutex.
574 void consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
576 struct lttng_ht
*ht
= data_ht
;
578 LTTNG_ASSERT(stream
);
581 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
583 pthread_mutex_lock(&the_consumer_data
.lock
);
584 pthread_mutex_lock(&stream
->chan
->lock
);
585 pthread_mutex_lock(&stream
->chan
->timer_lock
);
586 pthread_mutex_lock(&stream
->lock
);
589 /* Steal stream identifier to avoid having streams with the same key */
590 steal_stream_key(stream
->key
, ht
);
592 lttng_ht_add_unique_u64(ht
, &stream
->node
);
594 lttng_ht_add_u64(the_consumer_data
.stream_per_chan_id_ht
,
595 &stream
->node_channel_id
);
598 * Add stream to the stream_list_ht of the consumer data. No need to steal
599 * the key since the HT does not use it and we allow to add redundant keys
602 lttng_ht_add_u64(the_consumer_data
.stream_list_ht
,
603 &stream
->node_session_id
);
606 * When nb_init_stream_left reaches 0, we don't need to trigger any action
607 * in terms of destroying the associated channel, because the action that
608 * causes the count to become 0 also causes a stream to be added. The
609 * channel deletion will thus be triggered by the following removal of this
612 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
613 /* Increment refcount before decrementing nb_init_stream_left */
615 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
618 /* Update consumer data once the node is inserted. */
619 the_consumer_data
.stream_count
++;
620 the_consumer_data
.need_update
= 1;
623 pthread_mutex_unlock(&stream
->lock
);
624 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
625 pthread_mutex_unlock(&stream
->chan
->lock
);
626 pthread_mutex_unlock(&the_consumer_data
.lock
);
630 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
631 * be acquired before calling this.
633 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
636 struct lttng_ht_node_u64
*node
;
637 struct lttng_ht_iter iter
;
639 LTTNG_ASSERT(relayd
);
640 ASSERT_RCU_READ_LOCKED();
642 lttng_ht_lookup(the_consumer_data
.relayd_ht
, &relayd
->net_seq_idx
,
644 node
= lttng_ht_iter_get_node_u64(&iter
);
648 lttng_ht_add_unique_u64(the_consumer_data
.relayd_ht
, &relayd
->node
);
655 * Allocate and return a consumer relayd socket.
657 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
658 uint64_t net_seq_idx
)
660 struct consumer_relayd_sock_pair
*obj
= NULL
;
662 /* net sequence index of -1 is a failure */
663 if (net_seq_idx
== (uint64_t) -1ULL) {
667 obj
= zmalloc
<consumer_relayd_sock_pair
>();
669 PERROR("zmalloc relayd sock");
673 obj
->net_seq_idx
= net_seq_idx
;
675 obj
->destroy_flag
= 0;
676 obj
->control_sock
.sock
.fd
= -1;
677 obj
->data_sock
.sock
.fd
= -1;
678 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
679 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
686 * Find a relayd socket pair in the global consumer data.
688 * Return the object if found else NULL.
689 * RCU read-side lock must be held across this call and while using the
692 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
694 struct lttng_ht_iter iter
;
695 struct lttng_ht_node_u64
*node
;
696 struct consumer_relayd_sock_pair
*relayd
= NULL
;
698 ASSERT_RCU_READ_LOCKED();
700 /* Negative keys are lookup failures */
701 if (key
== (uint64_t) -1ULL) {
705 lttng_ht_lookup(the_consumer_data
.relayd_ht
, &key
, &iter
);
706 node
= lttng_ht_iter_get_node_u64(&iter
);
708 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
716 * Find a relayd and send the stream
718 * Returns 0 on success, < 0 on error
720 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
724 struct consumer_relayd_sock_pair
*relayd
;
726 LTTNG_ASSERT(stream
);
727 LTTNG_ASSERT(stream
->net_seq_idx
!= -1ULL);
730 /* The stream is not metadata. Get relayd reference if exists. */
732 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
733 if (relayd
!= NULL
) {
734 /* Add stream on the relayd */
735 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
736 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
737 get_consumer_domain(), path
, &stream
->relayd_stream_id
,
738 stream
->chan
->tracefile_size
,
739 stream
->chan
->tracefile_count
,
740 stream
->trace_chunk
);
741 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
743 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
744 lttng_consumer_cleanup_relayd(relayd
);
748 uatomic_inc(&relayd
->refcount
);
749 stream
->sent_to_relayd
= 1;
751 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
752 stream
->key
, stream
->net_seq_idx
);
757 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
758 stream
->name
, stream
->key
, stream
->net_seq_idx
);
766 * Find a relayd and send the streams sent message
768 * Returns 0 on success, < 0 on error
770 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
773 struct consumer_relayd_sock_pair
*relayd
;
775 LTTNG_ASSERT(net_seq_idx
!= -1ULL);
777 /* The stream is not metadata. Get relayd reference if exists. */
779 relayd
= consumer_find_relayd(net_seq_idx
);
780 if (relayd
!= NULL
) {
781 /* Add stream on the relayd */
782 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
783 ret
= relayd_streams_sent(&relayd
->control_sock
);
784 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
786 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
787 lttng_consumer_cleanup_relayd(relayd
);
791 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
798 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
806 * Find a relayd and close the stream
808 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
810 struct consumer_relayd_sock_pair
*relayd
;
812 /* The stream is not metadata. Get relayd reference if exists. */
814 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
816 consumer_stream_relayd_close(stream
, relayd
);
822 * Handle stream for relayd transmission if the stream applies for network
823 * streaming where the net sequence index is set.
825 * Return destination file descriptor or negative value on error.
827 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
828 size_t data_size
, unsigned long padding
,
829 struct consumer_relayd_sock_pair
*relayd
)
832 struct lttcomm_relayd_data_hdr data_hdr
;
835 LTTNG_ASSERT(stream
);
836 LTTNG_ASSERT(relayd
);
838 /* Reset data header */
839 memset(&data_hdr
, 0, sizeof(data_hdr
));
841 if (stream
->metadata_flag
) {
842 /* Caller MUST acquire the relayd control socket lock */
843 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
848 /* Metadata are always sent on the control socket. */
849 outfd
= relayd
->control_sock
.sock
.fd
;
851 /* Set header with stream information */
852 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
853 data_hdr
.data_size
= htobe32(data_size
);
854 data_hdr
.padding_size
= htobe32(padding
);
857 * Note that net_seq_num below is assigned with the *current* value of
858 * next_net_seq_num and only after that the next_net_seq_num will be
859 * increment. This is why when issuing a command on the relayd using
860 * this next value, 1 should always be substracted in order to compare
861 * the last seen sequence number on the relayd side to the last sent.
863 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
864 /* Other fields are zeroed previously */
866 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
872 ++stream
->next_net_seq_num
;
874 /* Set to go on data socket */
875 outfd
= relayd
->data_sock
.sock
.fd
;
883 * Write a character on the metadata poll pipe to wake the metadata thread.
884 * Returns 0 on success, -1 on error.
886 int consumer_metadata_wakeup_pipe(const struct lttng_consumer_channel
*channel
)
890 DBG("Waking up metadata poll thread (writing to pipe): channel name = '%s'",
892 if (channel
->monitor
&& channel
->metadata_stream
) {
893 const char dummy
= 'c';
894 const ssize_t write_ret
= lttng_write(
895 channel
->metadata_stream
->ust_metadata_poll_pipe
[1],
899 if (errno
== EWOULDBLOCK
) {
901 * This is fine, the metadata poll thread
902 * is having a hard time keeping-up, but
903 * it will eventually wake-up and consume
904 * the available data.
908 PERROR("Failed to write to UST metadata pipe while attempting to wake-up the metadata poll thread");
920 * Trigger a dump of the metadata content. Following/during the succesful
921 * completion of this call, the metadata poll thread will start receiving
922 * metadata packets to consume.
924 * The caller must hold the channel and stream locks.
927 int consumer_metadata_stream_dump(struct lttng_consumer_stream
*stream
)
931 ASSERT_LOCKED(stream
->chan
->lock
);
932 ASSERT_LOCKED(stream
->lock
);
933 LTTNG_ASSERT(stream
->metadata_flag
);
934 LTTNG_ASSERT(stream
->chan
->trace_chunk
);
936 switch (the_consumer_data
.type
) {
937 case LTTNG_CONSUMER_KERNEL
:
939 * Reset the position of what has been read from the
940 * metadata cache to 0 so we can dump it again.
942 ret
= kernctl_metadata_cache_dump(stream
->wait_fd
);
944 case LTTNG_CONSUMER32_UST
:
945 case LTTNG_CONSUMER64_UST
:
947 * Reset the position pushed from the metadata cache so it
948 * will write from the beginning on the next push.
950 stream
->ust_metadata_pushed
= 0;
951 ret
= consumer_metadata_wakeup_pipe(stream
->chan
);
954 ERR("Unknown consumer_data type");
958 ERR("Failed to dump the metadata cache");
964 int lttng_consumer_channel_set_trace_chunk(
965 struct lttng_consumer_channel
*channel
,
966 struct lttng_trace_chunk
*new_trace_chunk
)
968 pthread_mutex_lock(&channel
->lock
);
969 if (channel
->is_deleted
) {
971 * The channel has been logically deleted and should no longer
972 * be used. It has released its reference to its current trace
973 * chunk and should not acquire a new one.
975 * Return success as there is nothing for the caller to do.
981 * The acquisition of the reference cannot fail (barring
982 * a severe internal error) since a reference to the published
983 * chunk is already held by the caller.
985 if (new_trace_chunk
) {
986 const bool acquired_reference
= lttng_trace_chunk_get(
989 LTTNG_ASSERT(acquired_reference
);
992 lttng_trace_chunk_put(channel
->trace_chunk
);
993 channel
->trace_chunk
= new_trace_chunk
;
995 pthread_mutex_unlock(&channel
->lock
);
1000 * Allocate and return a new lttng_consumer_channel object using the given key
1001 * to initialize the hash table node.
1003 * On error, return NULL.
1005 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
1006 uint64_t session_id
,
1007 const uint64_t *chunk_id
,
1008 const char *pathname
,
1011 enum lttng_event_output output
,
1012 uint64_t tracefile_size
,
1013 uint64_t tracefile_count
,
1014 uint64_t session_id_per_pid
,
1015 unsigned int monitor
,
1016 unsigned int live_timer_interval
,
1017 bool is_in_live_session
,
1018 const char *root_shm_path
,
1019 const char *shm_path
)
1021 struct lttng_consumer_channel
*channel
= NULL
;
1022 struct lttng_trace_chunk
*trace_chunk
= NULL
;
1025 trace_chunk
= lttng_trace_chunk_registry_find_chunk(
1026 the_consumer_data
.chunk_registry
, session_id
,
1029 ERR("Failed to find trace chunk reference during creation of channel");
1034 channel
= zmalloc
<lttng_consumer_channel
>();
1035 if (channel
== NULL
) {
1036 PERROR("malloc struct lttng_consumer_channel");
1041 channel
->refcount
= 0;
1042 channel
->session_id
= session_id
;
1043 channel
->session_id_per_pid
= session_id_per_pid
;
1044 channel
->relayd_id
= relayd_id
;
1045 channel
->tracefile_size
= tracefile_size
;
1046 channel
->tracefile_count
= tracefile_count
;
1047 channel
->monitor
= monitor
;
1048 channel
->live_timer_interval
= live_timer_interval
;
1049 channel
->is_live
= is_in_live_session
;
1050 pthread_mutex_init(&channel
->lock
, NULL
);
1051 pthread_mutex_init(&channel
->timer_lock
, NULL
);
1054 case LTTNG_EVENT_SPLICE
:
1055 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
1057 case LTTNG_EVENT_MMAP
:
1058 channel
->output
= CONSUMER_CHANNEL_MMAP
;
1068 * In monitor mode, the streams associated with the channel will be put in
1069 * a special list ONLY owned by this channel. So, the refcount is set to 1
1070 * here meaning that the channel itself has streams that are referenced.
1072 * On a channel deletion, once the channel is no longer visible, the
1073 * refcount is decremented and checked for a zero value to delete it. With
1074 * streams in no monitor mode, it will now be safe to destroy the channel.
1076 if (!channel
->monitor
) {
1077 channel
->refcount
= 1;
1080 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1081 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1083 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1084 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1086 if (root_shm_path
) {
1087 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1088 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1091 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1092 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1095 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1096 lttng_ht_node_init_u64(&channel
->channels_by_session_id_ht_node
,
1097 channel
->session_id
);
1099 channel
->wait_fd
= -1;
1100 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1103 int ret
= lttng_consumer_channel_set_trace_chunk(channel
,
1110 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1113 lttng_trace_chunk_put(trace_chunk
);
1116 consumer_del_channel(channel
);
1122 * Add a channel to the global list protected by a mutex.
1124 * Always return 0 indicating success.
1126 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1127 struct lttng_consumer_local_data
*ctx
)
1129 pthread_mutex_lock(&the_consumer_data
.lock
);
1130 pthread_mutex_lock(&channel
->lock
);
1131 pthread_mutex_lock(&channel
->timer_lock
);
1134 * This gives us a guarantee that the channel we are about to add to the
1135 * channel hash table will be unique. See this function comment on the why
1136 * we need to steel the channel key at this stage.
1138 steal_channel_key(channel
->key
);
1141 lttng_ht_add_unique_u64(the_consumer_data
.channel_ht
, &channel
->node
);
1142 lttng_ht_add_u64(the_consumer_data
.channels_by_session_id_ht
,
1143 &channel
->channels_by_session_id_ht_node
);
1145 channel
->is_published
= true;
1147 pthread_mutex_unlock(&channel
->timer_lock
);
1148 pthread_mutex_unlock(&channel
->lock
);
1149 pthread_mutex_unlock(&the_consumer_data
.lock
);
1151 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1152 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1159 * Allocate the pollfd structure and the local view of the out fds to avoid
1160 * doing a lookup in the linked list and concurrency issues when writing is
1161 * needed. Called with consumer_data.lock held.
1163 * Returns the number of fds in the structures.
1165 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1166 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1167 struct lttng_ht
*ht
, int *nb_inactive_fd
)
1170 struct lttng_ht_iter iter
;
1171 struct lttng_consumer_stream
*stream
;
1175 LTTNG_ASSERT(pollfd
);
1176 LTTNG_ASSERT(local_stream
);
1178 DBG("Updating poll fd array");
1179 *nb_inactive_fd
= 0;
1181 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1183 * Only active streams with an active end point can be added to the
1184 * poll set and local stream storage of the thread.
1186 * There is a potential race here for endpoint_status to be updated
1187 * just after the check. However, this is OK since the stream(s) will
1188 * be deleted once the thread is notified that the end point state has
1189 * changed where this function will be called back again.
1191 * We track the number of inactive FDs because they still need to be
1192 * closed by the polling thread after a wakeup on the data_pipe or
1195 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1196 (*nb_inactive_fd
)++;
1200 * This clobbers way too much the debug output. Uncomment that if you
1201 * need it for debugging purposes.
1203 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1204 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1205 local_stream
[i
] = stream
;
1211 * Insert the consumer_data_pipe at the end of the array and don't
1212 * increment i so nb_fd is the number of real FD.
1214 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1215 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1217 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1218 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1223 * Poll on the should_quit pipe and the command socket return -1 on
1224 * error, 1 if should exit, 0 if data is available on the command socket
1226 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1231 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1232 if (num_rdy
== -1) {
1234 * Restart interrupted system call.
1236 if (errno
== EINTR
) {
1239 PERROR("Poll error");
1242 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1243 DBG("consumer_should_quit wake up");
1250 * Set the error socket.
1252 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1255 ctx
->consumer_error_socket
= sock
;
1259 * Set the command socket path.
1261 void lttng_consumer_set_command_sock_path(
1262 struct lttng_consumer_local_data
*ctx
, char *sock
)
1264 ctx
->consumer_command_sock_path
= sock
;
1268 * Send return code to the session daemon.
1269 * If the socket is not defined, we return 0, it is not a fatal error
1271 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1273 if (ctx
->consumer_error_socket
> 0) {
1274 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1275 sizeof(enum lttcomm_sessiond_command
));
1282 * Close all the tracefiles and stream fds and MUST be called when all
1283 * instances are destroyed i.e. when all threads were joined and are ended.
1285 void lttng_consumer_cleanup(void)
1287 struct lttng_ht_iter iter
;
1288 struct lttng_consumer_channel
*channel
;
1289 unsigned int trace_chunks_left
;
1293 cds_lfht_for_each_entry(the_consumer_data
.channel_ht
->ht
, &iter
.iter
,
1294 channel
, node
.node
) {
1295 consumer_del_channel(channel
);
1300 lttng_ht_destroy(the_consumer_data
.channel_ht
);
1301 lttng_ht_destroy(the_consumer_data
.channels_by_session_id_ht
);
1303 cleanup_relayd_ht();
1305 lttng_ht_destroy(the_consumer_data
.stream_per_chan_id_ht
);
1308 * This HT contains streams that are freed by either the metadata thread or
1309 * the data thread so we do *nothing* on the hash table and simply destroy
1312 lttng_ht_destroy(the_consumer_data
.stream_list_ht
);
1315 * Trace chunks in the registry may still exist if the session
1316 * daemon has encountered an internal error and could not
1317 * tear down its sessions and/or trace chunks properly.
1319 * Release the session daemon's implicit reference to any remaining
1320 * trace chunk and print an error if any trace chunk was found. Note
1321 * that there are _no_ legitimate cases for trace chunks to be left,
1322 * it is a leak. However, it can happen following a crash of the
1323 * session daemon and not emptying the registry would cause an assertion
1326 trace_chunks_left
= lttng_trace_chunk_registry_put_each_chunk(
1327 the_consumer_data
.chunk_registry
);
1328 if (trace_chunks_left
) {
1329 ERR("%u trace chunks are leaked by lttng-consumerd. "
1330 "This can be caused by an internal error of the session daemon.",
1333 /* Run all callbacks freeing each chunk. */
1335 lttng_trace_chunk_registry_destroy(the_consumer_data
.chunk_registry
);
1339 * Called from signal handler.
1341 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1345 CMM_STORE_SHARED(consumer_quit
, 1);
1346 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1348 PERROR("write consumer quit");
1351 DBG("Consumer flag that it should quit");
1356 * Flush pending writes to trace output disk file.
1359 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1363 int outfd
= stream
->out_fd
;
1366 * This does a blocking write-and-wait on any page that belongs to the
1367 * subbuffer prior to the one we just wrote.
1368 * Don't care about error values, as these are just hints and ways to
1369 * limit the amount of page cache used.
1371 if (orig_offset
< stream
->max_sb_size
) {
1374 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1375 stream
->max_sb_size
,
1376 SYNC_FILE_RANGE_WAIT_BEFORE
1377 | SYNC_FILE_RANGE_WRITE
1378 | SYNC_FILE_RANGE_WAIT_AFTER
);
1380 * Give hints to the kernel about how we access the file:
1381 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1384 * We need to call fadvise again after the file grows because the
1385 * kernel does not seem to apply fadvise to non-existing parts of the
1388 * Call fadvise _after_ having waited for the page writeback to
1389 * complete because the dirty page writeback semantic is not well
1390 * defined. So it can be expected to lead to lower throughput in
1393 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1394 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1395 if (ret
&& ret
!= -ENOSYS
) {
1397 PERROR("posix_fadvise on fd %i", outfd
);
1402 * Initialise the necessary environnement :
1403 * - create a new context
1404 * - create the poll_pipe
1405 * - create the should_quit pipe (for signal handler)
1406 * - create the thread pipe (for splice)
1408 * Takes a function pointer as argument, this function is called when data is
1409 * available on a buffer. This function is responsible to do the
1410 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1411 * buffer configuration and then kernctl_put_next_subbuf at the end.
1413 * Returns a pointer to the new context or NULL on error.
1415 struct lttng_consumer_local_data
*lttng_consumer_create(
1416 enum lttng_consumer_type type
,
1417 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1418 struct lttng_consumer_local_data
*ctx
, bool locked_by_caller
),
1419 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1420 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1421 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1424 struct lttng_consumer_local_data
*ctx
;
1426 LTTNG_ASSERT(the_consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1427 the_consumer_data
.type
== type
);
1428 the_consumer_data
.type
= type
;
1430 ctx
= zmalloc
<lttng_consumer_local_data
>();
1432 PERROR("allocating context");
1436 ctx
->consumer_error_socket
= -1;
1437 ctx
->consumer_metadata_socket
= -1;
1438 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1439 /* assign the callbacks */
1440 ctx
->on_buffer_ready
= buffer_ready
;
1441 ctx
->on_recv_channel
= recv_channel
;
1442 ctx
->on_recv_stream
= recv_stream
;
1443 ctx
->on_update_stream
= update_stream
;
1445 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1446 if (!ctx
->consumer_data_pipe
) {
1447 goto error_poll_pipe
;
1450 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1451 if (!ctx
->consumer_wakeup_pipe
) {
1452 goto error_wakeup_pipe
;
1455 ret
= pipe(ctx
->consumer_should_quit
);
1457 PERROR("Error creating recv pipe");
1458 goto error_quit_pipe
;
1461 ret
= pipe(ctx
->consumer_channel_pipe
);
1463 PERROR("Error creating channel pipe");
1464 goto error_channel_pipe
;
1467 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1468 if (!ctx
->consumer_metadata_pipe
) {
1469 goto error_metadata_pipe
;
1472 ctx
->channel_monitor_pipe
= -1;
1476 error_metadata_pipe
:
1477 utils_close_pipe(ctx
->consumer_channel_pipe
);
1479 utils_close_pipe(ctx
->consumer_should_quit
);
1481 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1483 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1491 * Iterate over all streams of the hashtable and free them properly.
1493 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1495 struct lttng_ht_iter iter
;
1496 struct lttng_consumer_stream
*stream
;
1503 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1505 * Ignore return value since we are currently cleaning up so any error
1508 (void) consumer_del_stream(stream
, ht
);
1512 lttng_ht_destroy(ht
);
1516 * Iterate over all streams of the metadata hashtable and free them
1519 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1521 struct lttng_ht_iter iter
;
1522 struct lttng_consumer_stream
*stream
;
1529 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1531 * Ignore return value since we are currently cleaning up so any error
1534 (void) consumer_del_metadata_stream(stream
, ht
);
1538 lttng_ht_destroy(ht
);
1542 * Close all fds associated with the instance and free the context.
1544 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1548 DBG("Consumer destroying it. Closing everything.");
1554 destroy_data_stream_ht(data_ht
);
1555 destroy_metadata_stream_ht(metadata_ht
);
1557 ret
= close(ctx
->consumer_error_socket
);
1561 ret
= close(ctx
->consumer_metadata_socket
);
1565 utils_close_pipe(ctx
->consumer_channel_pipe
);
1566 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1567 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1568 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1569 utils_close_pipe(ctx
->consumer_should_quit
);
1571 unlink(ctx
->consumer_command_sock_path
);
1576 * Write the metadata stream id on the specified file descriptor.
1578 static int write_relayd_metadata_id(int fd
,
1579 struct lttng_consumer_stream
*stream
,
1580 unsigned long padding
)
1583 struct lttcomm_relayd_metadata_payload hdr
;
1585 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1586 hdr
.padding_size
= htobe32(padding
);
1587 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1588 if (ret
< sizeof(hdr
)) {
1590 * This error means that the fd's end is closed so ignore the PERROR
1591 * not to clubber the error output since this can happen in a normal
1594 if (errno
!= EPIPE
) {
1595 PERROR("write metadata stream id");
1597 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1599 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1600 * handle writting the missing part so report that as an error and
1601 * don't lie to the caller.
1606 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1607 stream
->relayd_stream_id
, padding
);
1614 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1615 * core function for writing trace buffers to either the local filesystem or
1618 * It must be called with the stream and the channel lock held.
1620 * Careful review MUST be put if any changes occur!
1622 * Returns the number of bytes written
1624 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1625 struct lttng_consumer_stream
*stream
,
1626 const struct lttng_buffer_view
*buffer
,
1627 unsigned long padding
)
1630 off_t orig_offset
= stream
->out_fd_offset
;
1631 /* Default is on the disk */
1632 int outfd
= stream
->out_fd
;
1633 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1634 unsigned int relayd_hang_up
= 0;
1635 const size_t subbuf_content_size
= buffer
->size
- padding
;
1638 /* RCU lock for the relayd pointer */
1640 LTTNG_ASSERT(stream
->net_seq_idx
!= (uint64_t) -1ULL ||
1641 stream
->trace_chunk
);
1643 /* Flag that the current stream if set for network streaming. */
1644 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1645 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1646 if (relayd
== NULL
) {
1652 /* Handle stream on the relayd if the output is on the network */
1654 unsigned long netlen
= subbuf_content_size
;
1657 * Lock the control socket for the complete duration of the function
1658 * since from this point on we will use the socket.
1660 if (stream
->metadata_flag
) {
1661 /* Metadata requires the control socket. */
1662 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1663 if (stream
->reset_metadata_flag
) {
1664 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1665 stream
->relayd_stream_id
,
1666 stream
->metadata_version
);
1671 stream
->reset_metadata_flag
= 0;
1673 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1676 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1681 /* Use the returned socket. */
1684 /* Write metadata stream id before payload */
1685 if (stream
->metadata_flag
) {
1686 ret
= write_relayd_metadata_id(outfd
, stream
, padding
);
1693 write_len
= subbuf_content_size
;
1695 /* No streaming; we have to write the full padding. */
1696 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1697 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1699 ERR("Reset metadata file");
1702 stream
->reset_metadata_flag
= 0;
1706 * Check if we need to change the tracefile before writing the packet.
1708 if (stream
->chan
->tracefile_size
> 0 &&
1709 (stream
->tracefile_size_current
+ buffer
->size
) >
1710 stream
->chan
->tracefile_size
) {
1711 ret
= consumer_stream_rotate_output_files(stream
);
1715 outfd
= stream
->out_fd
;
1718 stream
->tracefile_size_current
+= buffer
->size
;
1719 write_len
= buffer
->size
;
1723 * This call guarantee that len or less is returned. It's impossible to
1724 * receive a ret value that is bigger than len.
1726 ret
= lttng_write(outfd
, buffer
->data
, write_len
);
1727 DBG("Consumer mmap write() ret %zd (len %zu)", ret
, write_len
);
1728 if (ret
< 0 || ((size_t) ret
!= write_len
)) {
1730 * Report error to caller if nothing was written else at least send the
1738 /* Socket operation failed. We consider the relayd dead */
1739 if (errno
== EPIPE
) {
1741 * This is possible if the fd is closed on the other side
1742 * (outfd) or any write problem. It can be verbose a bit for a
1743 * normal execution if for instance the relayd is stopped
1744 * abruptly. This can happen so set this to a DBG statement.
1746 DBG("Consumer mmap write detected relayd hang up");
1748 /* Unhandled error, print it and stop function right now. */
1749 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret
,
1754 stream
->output_written
+= ret
;
1756 /* This call is useless on a socket so better save a syscall. */
1758 /* This won't block, but will start writeout asynchronously */
1759 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, write_len
,
1760 SYNC_FILE_RANGE_WRITE
);
1761 stream
->out_fd_offset
+= write_len
;
1762 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1767 * This is a special case that the relayd has closed its socket. Let's
1768 * cleanup the relayd object and all associated streams.
1770 if (relayd
&& relayd_hang_up
) {
1771 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1772 lttng_consumer_cleanup_relayd(relayd
);
1776 /* Unlock only if ctrl socket used */
1777 if (relayd
&& stream
->metadata_flag
) {
1778 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1786 * Splice the data from the ring buffer to the tracefile.
1788 * It must be called with the stream lock held.
1790 * Returns the number of bytes spliced.
1792 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1793 struct lttng_consumer_local_data
*ctx
,
1794 struct lttng_consumer_stream
*stream
, unsigned long len
,
1795 unsigned long padding
)
1797 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1799 off_t orig_offset
= stream
->out_fd_offset
;
1800 int fd
= stream
->wait_fd
;
1801 /* Default is on the disk */
1802 int outfd
= stream
->out_fd
;
1803 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1805 unsigned int relayd_hang_up
= 0;
1807 switch (the_consumer_data
.type
) {
1808 case LTTNG_CONSUMER_KERNEL
:
1810 case LTTNG_CONSUMER32_UST
:
1811 case LTTNG_CONSUMER64_UST
:
1812 /* Not supported for user space tracing */
1815 ERR("Unknown consumer_data type");
1819 /* RCU lock for the relayd pointer */
1822 /* Flag that the current stream if set for network streaming. */
1823 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1824 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1825 if (relayd
== NULL
) {
1830 splice_pipe
= stream
->splice_pipe
;
1832 /* Write metadata stream id before payload */
1834 unsigned long total_len
= len
;
1836 if (stream
->metadata_flag
) {
1838 * Lock the control socket for the complete duration of the function
1839 * since from this point on we will use the socket.
1841 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1843 if (stream
->reset_metadata_flag
) {
1844 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1845 stream
->relayd_stream_id
,
1846 stream
->metadata_version
);
1851 stream
->reset_metadata_flag
= 0;
1853 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
,
1861 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1864 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1870 /* Use the returned socket. */
1873 /* No streaming, we have to set the len with the full padding */
1876 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1877 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1879 ERR("Reset metadata file");
1882 stream
->reset_metadata_flag
= 0;
1885 * Check if we need to change the tracefile before writing the packet.
1887 if (stream
->chan
->tracefile_size
> 0 &&
1888 (stream
->tracefile_size_current
+ len
) >
1889 stream
->chan
->tracefile_size
) {
1890 ret
= consumer_stream_rotate_output_files(stream
);
1895 outfd
= stream
->out_fd
;
1898 stream
->tracefile_size_current
+= len
;
1902 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1903 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1904 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1905 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1906 DBG("splice chan to pipe, ret %zd", ret_splice
);
1907 if (ret_splice
< 0) {
1910 PERROR("Error in relay splice");
1914 /* Handle stream on the relayd if the output is on the network */
1915 if (relayd
&& stream
->metadata_flag
) {
1916 size_t metadata_payload_size
=
1917 sizeof(struct lttcomm_relayd_metadata_payload
);
1919 /* Update counter to fit the spliced data */
1920 ret_splice
+= metadata_payload_size
;
1921 len
+= metadata_payload_size
;
1923 * We do this so the return value can match the len passed as
1924 * argument to this function.
1926 written
-= metadata_payload_size
;
1929 /* Splice data out */
1930 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1931 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1932 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1934 if (ret_splice
< 0) {
1939 } else if (ret_splice
> len
) {
1941 * We don't expect this code path to be executed but you never know
1942 * so this is an extra protection agains a buggy splice().
1945 written
+= ret_splice
;
1946 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1950 /* All good, update current len and continue. */
1954 /* This call is useless on a socket so better save a syscall. */
1956 /* This won't block, but will start writeout asynchronously */
1957 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1958 SYNC_FILE_RANGE_WRITE
);
1959 stream
->out_fd_offset
+= ret_splice
;
1961 stream
->output_written
+= ret_splice
;
1962 written
+= ret_splice
;
1965 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1971 * This is a special case that the relayd has closed its socket. Let's
1972 * cleanup the relayd object and all associated streams.
1974 if (relayd
&& relayd_hang_up
) {
1975 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1976 lttng_consumer_cleanup_relayd(relayd
);
1977 /* Skip splice error so the consumer does not fail */
1982 /* send the appropriate error description to sessiond */
1985 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1988 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1991 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1996 if (relayd
&& stream
->metadata_flag
) {
1997 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2005 * Sample the snapshot positions for a specific fd
2007 * Returns 0 on success, < 0 on error
2009 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
2011 switch (the_consumer_data
.type
) {
2012 case LTTNG_CONSUMER_KERNEL
:
2013 return lttng_kconsumer_sample_snapshot_positions(stream
);
2014 case LTTNG_CONSUMER32_UST
:
2015 case LTTNG_CONSUMER64_UST
:
2016 return lttng_ustconsumer_sample_snapshot_positions(stream
);
2018 ERR("Unknown consumer_data type");
2024 * Take a snapshot for a specific fd
2026 * Returns 0 on success, < 0 on error
2028 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2030 switch (the_consumer_data
.type
) {
2031 case LTTNG_CONSUMER_KERNEL
:
2032 return lttng_kconsumer_take_snapshot(stream
);
2033 case LTTNG_CONSUMER32_UST
:
2034 case LTTNG_CONSUMER64_UST
:
2035 return lttng_ustconsumer_take_snapshot(stream
);
2037 ERR("Unknown consumer_data type");
2044 * Get the produced position
2046 * Returns 0 on success, < 0 on error
2048 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
2051 switch (the_consumer_data
.type
) {
2052 case LTTNG_CONSUMER_KERNEL
:
2053 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
2054 case LTTNG_CONSUMER32_UST
:
2055 case LTTNG_CONSUMER64_UST
:
2056 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2058 ERR("Unknown consumer_data type");
2065 * Get the consumed position (free-running counter position in bytes).
2067 * Returns 0 on success, < 0 on error
2069 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
2072 switch (the_consumer_data
.type
) {
2073 case LTTNG_CONSUMER_KERNEL
:
2074 return lttng_kconsumer_get_consumed_snapshot(stream
, pos
);
2075 case LTTNG_CONSUMER32_UST
:
2076 case LTTNG_CONSUMER64_UST
:
2077 return lttng_ustconsumer_get_consumed_snapshot(stream
, pos
);
2079 ERR("Unknown consumer_data type");
2085 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2086 int sock
, struct pollfd
*consumer_sockpoll
)
2088 switch (the_consumer_data
.type
) {
2089 case LTTNG_CONSUMER_KERNEL
:
2090 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2091 case LTTNG_CONSUMER32_UST
:
2092 case LTTNG_CONSUMER64_UST
:
2093 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2095 ERR("Unknown consumer_data type");
2102 void lttng_consumer_close_all_metadata(void)
2104 switch (the_consumer_data
.type
) {
2105 case LTTNG_CONSUMER_KERNEL
:
2107 * The Kernel consumer has a different metadata scheme so we don't
2108 * close anything because the stream will be closed by the session
2112 case LTTNG_CONSUMER32_UST
:
2113 case LTTNG_CONSUMER64_UST
:
2115 * Close all metadata streams. The metadata hash table is passed and
2116 * this call iterates over it by closing all wakeup fd. This is safe
2117 * because at this point we are sure that the metadata producer is
2118 * either dead or blocked.
2120 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2123 ERR("Unknown consumer_data type");
2129 * Clean up a metadata stream and free its memory.
2131 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2132 struct lttng_ht
*ht
)
2134 struct lttng_consumer_channel
*channel
= NULL
;
2135 bool free_channel
= false;
2137 LTTNG_ASSERT(stream
);
2139 * This call should NEVER receive regular stream. It must always be
2140 * metadata stream and this is crucial for data structure synchronization.
2142 LTTNG_ASSERT(stream
->metadata_flag
);
2144 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2146 pthread_mutex_lock(&the_consumer_data
.lock
);
2148 * Note that this assumes that a stream's channel is never changed and
2149 * that the stream's lock doesn't need to be taken to sample its
2152 channel
= stream
->chan
;
2153 pthread_mutex_lock(&channel
->lock
);
2154 pthread_mutex_lock(&stream
->lock
);
2155 if (channel
->metadata_cache
) {
2156 /* Only applicable to userspace consumers. */
2157 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
2160 /* Remove any reference to that stream. */
2161 consumer_stream_delete(stream
, ht
);
2163 /* Close down everything including the relayd if one. */
2164 consumer_stream_close(stream
);
2165 /* Destroy tracer buffers of the stream. */
2166 consumer_stream_destroy_buffers(stream
);
2168 /* Atomically decrement channel refcount since other threads can use it. */
2169 if (!uatomic_sub_return(&channel
->refcount
, 1)
2170 && !uatomic_read(&channel
->nb_init_stream_left
)) {
2171 /* Go for channel deletion! */
2172 free_channel
= true;
2174 stream
->chan
= NULL
;
2177 * Nullify the stream reference so it is not used after deletion. The
2178 * channel lock MUST be acquired before being able to check for a NULL
2181 channel
->metadata_stream
= NULL
;
2183 if (channel
->metadata_cache
) {
2184 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
2186 pthread_mutex_unlock(&stream
->lock
);
2187 pthread_mutex_unlock(&channel
->lock
);
2188 pthread_mutex_unlock(&the_consumer_data
.lock
);
2191 consumer_del_channel(channel
);
2194 lttng_trace_chunk_put(stream
->trace_chunk
);
2195 stream
->trace_chunk
= NULL
;
2196 consumer_stream_free(stream
);
2200 * Action done with the metadata stream when adding it to the consumer internal
2201 * data structures to handle it.
2203 void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2205 struct lttng_ht
*ht
= metadata_ht
;
2206 struct lttng_ht_iter iter
;
2207 struct lttng_ht_node_u64
*node
;
2209 LTTNG_ASSERT(stream
);
2212 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2214 pthread_mutex_lock(&the_consumer_data
.lock
);
2215 pthread_mutex_lock(&stream
->chan
->lock
);
2216 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2217 pthread_mutex_lock(&stream
->lock
);
2220 * From here, refcounts are updated so be _careful_ when returning an error
2227 * Lookup the stream just to make sure it does not exist in our internal
2228 * state. This should NEVER happen.
2230 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2231 node
= lttng_ht_iter_get_node_u64(&iter
);
2232 LTTNG_ASSERT(!node
);
2235 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2236 * in terms of destroying the associated channel, because the action that
2237 * causes the count to become 0 also causes a stream to be added. The
2238 * channel deletion will thus be triggered by the following removal of this
2241 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2242 /* Increment refcount before decrementing nb_init_stream_left */
2244 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2247 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2249 lttng_ht_add_u64(the_consumer_data
.stream_per_chan_id_ht
,
2250 &stream
->node_channel_id
);
2253 * Add stream to the stream_list_ht of the consumer data. No need to steal
2254 * the key since the HT does not use it and we allow to add redundant keys
2257 lttng_ht_add_u64(the_consumer_data
.stream_list_ht
,
2258 &stream
->node_session_id
);
2262 pthread_mutex_unlock(&stream
->lock
);
2263 pthread_mutex_unlock(&stream
->chan
->lock
);
2264 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2265 pthread_mutex_unlock(&the_consumer_data
.lock
);
2269 * Delete data stream that are flagged for deletion (endpoint_status).
2271 static void validate_endpoint_status_data_stream(void)
2273 struct lttng_ht_iter iter
;
2274 struct lttng_consumer_stream
*stream
;
2276 DBG("Consumer delete flagged data stream");
2279 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2280 /* Validate delete flag of the stream */
2281 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2284 /* Delete it right now */
2285 consumer_del_stream(stream
, data_ht
);
2291 * Delete metadata stream that are flagged for deletion (endpoint_status).
2293 static void validate_endpoint_status_metadata_stream(
2294 struct lttng_poll_event
*pollset
)
2296 struct lttng_ht_iter iter
;
2297 struct lttng_consumer_stream
*stream
;
2299 DBG("Consumer delete flagged metadata stream");
2301 LTTNG_ASSERT(pollset
);
2304 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2305 /* Validate delete flag of the stream */
2306 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2310 * Remove from pollset so the metadata thread can continue without
2311 * blocking on a deleted stream.
2313 lttng_poll_del(pollset
, stream
->wait_fd
);
2315 /* Delete it right now */
2316 consumer_del_metadata_stream(stream
, metadata_ht
);
2322 * Thread polls on metadata file descriptor and write them on disk or on the
2325 void *consumer_thread_metadata_poll(void *data
)
2327 int ret
, i
, pollfd
, err
= -1;
2328 uint32_t revents
, nb_fd
;
2329 struct lttng_consumer_stream
*stream
= NULL
;
2330 struct lttng_ht_iter iter
;
2331 struct lttng_ht_node_u64
*node
;
2332 struct lttng_poll_event events
;
2333 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
2336 rcu_register_thread();
2338 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2340 if (testpoint(consumerd_thread_metadata
)) {
2341 goto error_testpoint
;
2344 health_code_update();
2346 DBG("Thread metadata poll started");
2348 /* Size is set to 1 for the consumer_metadata pipe */
2349 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2351 ERR("Poll set creation failed");
2355 ret
= lttng_poll_add(&events
,
2356 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2362 DBG("Metadata main loop started");
2366 health_code_update();
2367 health_poll_entry();
2368 DBG("Metadata poll wait");
2369 ret
= lttng_poll_wait(&events
, -1);
2370 DBG("Metadata poll return from wait with %d fd(s)",
2371 LTTNG_POLL_GETNB(&events
));
2373 DBG("Metadata event caught in thread");
2375 if (errno
== EINTR
) {
2376 ERR("Poll EINTR caught");
2379 if (LTTNG_POLL_GETNB(&events
) == 0) {
2380 err
= 0; /* All is OK */
2387 /* From here, the event is a metadata wait fd */
2388 for (i
= 0; i
< nb_fd
; i
++) {
2389 health_code_update();
2391 revents
= LTTNG_POLL_GETEV(&events
, i
);
2392 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2394 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2395 if (revents
& LPOLLIN
) {
2398 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2399 &stream
, sizeof(stream
));
2400 if (pipe_len
< sizeof(stream
)) {
2402 PERROR("read metadata stream");
2405 * Remove the pipe from the poll set and continue the loop
2406 * since their might be data to consume.
2408 lttng_poll_del(&events
,
2409 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2410 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2414 /* A NULL stream means that the state has changed. */
2415 if (stream
== NULL
) {
2416 /* Check for deleted streams. */
2417 validate_endpoint_status_metadata_stream(&events
);
2421 DBG("Adding metadata stream %d to poll set",
2424 /* Add metadata stream to the global poll events list */
2425 lttng_poll_add(&events
, stream
->wait_fd
,
2426 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2427 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2428 DBG("Metadata thread pipe hung up");
2430 * Remove the pipe from the poll set and continue the loop
2431 * since their might be data to consume.
2433 lttng_poll_del(&events
,
2434 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2435 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2438 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2442 /* Handle other stream */
2448 uint64_t tmp_id
= (uint64_t) pollfd
;
2450 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2452 node
= lttng_ht_iter_get_node_u64(&iter
);
2455 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2458 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2459 /* Get the data out of the metadata file descriptor */
2460 DBG("Metadata available on fd %d", pollfd
);
2461 LTTNG_ASSERT(stream
->wait_fd
== pollfd
);
2464 health_code_update();
2466 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2468 * We don't check the return value here since if we get
2469 * a negative len, it means an error occurred thus we
2470 * simply remove it from the poll set and free the
2475 /* It's ok to have an unavailable sub-buffer */
2476 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2477 /* Clean up stream from consumer and free it. */
2478 lttng_poll_del(&events
, stream
->wait_fd
);
2479 consumer_del_metadata_stream(stream
, metadata_ht
);
2481 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2482 DBG("Metadata fd %d is hup|err.", pollfd
);
2483 if (!stream
->hangup_flush_done
&&
2484 (the_consumer_data
.type
== LTTNG_CONSUMER32_UST
||
2485 the_consumer_data
.type
==
2486 LTTNG_CONSUMER64_UST
)) {
2487 DBG("Attempting to flush and consume the UST buffers");
2488 lttng_ustconsumer_on_stream_hangup(stream
);
2490 /* We just flushed the stream now read it. */
2492 health_code_update();
2494 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2496 * We don't check the return value here since if we get
2497 * a negative len, it means an error occurred thus we
2498 * simply remove it from the poll set and free the
2504 lttng_poll_del(&events
, stream
->wait_fd
);
2506 * This call update the channel states, closes file descriptors
2507 * and securely free the stream.
2509 consumer_del_metadata_stream(stream
, metadata_ht
);
2511 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2515 /* Release RCU lock for the stream looked up */
2523 DBG("Metadata poll thread exiting");
2525 lttng_poll_clean(&events
);
2530 ERR("Health error occurred in %s", __func__
);
2532 health_unregister(health_consumerd
);
2533 rcu_unregister_thread();
2538 * This thread polls the fds in the set to consume the data and write
2539 * it to tracefile if necessary.
2541 void *consumer_thread_data_poll(void *data
)
2543 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2544 struct pollfd
*pollfd
= NULL
;
2545 /* local view of the streams */
2546 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2547 /* local view of consumer_data.fds_count */
2549 /* 2 for the consumer_data_pipe and wake up pipe */
2550 const int nb_pipes_fd
= 2;
2551 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2552 int nb_inactive_fd
= 0;
2553 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
2556 rcu_register_thread();
2558 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2560 if (testpoint(consumerd_thread_data
)) {
2561 goto error_testpoint
;
2564 health_code_update();
2566 local_stream
= zmalloc
<lttng_consumer_stream
*>();
2567 if (local_stream
== NULL
) {
2568 PERROR("local_stream malloc");
2573 health_code_update();
2579 * the fds set has been updated, we need to update our
2580 * local array as well
2582 pthread_mutex_lock(&the_consumer_data
.lock
);
2583 if (the_consumer_data
.need_update
) {
2588 local_stream
= NULL
;
2590 /* Allocate for all fds */
2591 pollfd
= calloc
<struct pollfd
>(the_consumer_data
.stream_count
+ nb_pipes_fd
);
2592 if (pollfd
== NULL
) {
2593 PERROR("pollfd malloc");
2594 pthread_mutex_unlock(&the_consumer_data
.lock
);
2598 local_stream
= calloc
<lttng_consumer_stream
*>(the_consumer_data
.stream_count
+ nb_pipes_fd
);
2599 if (local_stream
== NULL
) {
2600 PERROR("local_stream malloc");
2601 pthread_mutex_unlock(&the_consumer_data
.lock
);
2604 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2605 data_ht
, &nb_inactive_fd
);
2607 ERR("Error in allocating pollfd or local_outfds");
2608 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2609 pthread_mutex_unlock(&the_consumer_data
.lock
);
2613 the_consumer_data
.need_update
= 0;
2615 pthread_mutex_unlock(&the_consumer_data
.lock
);
2617 /* No FDs and consumer_quit, consumer_cleanup the thread */
2618 if (nb_fd
== 0 && nb_inactive_fd
== 0 &&
2619 CMM_LOAD_SHARED(consumer_quit
) == 1) {
2620 err
= 0; /* All is OK */
2623 /* poll on the array of fds */
2625 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2626 if (testpoint(consumerd_thread_data_poll
)) {
2629 health_poll_entry();
2630 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2632 DBG("poll num_rdy : %d", num_rdy
);
2633 if (num_rdy
== -1) {
2635 * Restart interrupted system call.
2637 if (errno
== EINTR
) {
2640 PERROR("Poll error");
2641 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2643 } else if (num_rdy
== 0) {
2644 DBG("Polling thread timed out");
2648 if (caa_unlikely(data_consumption_paused
)) {
2649 DBG("Data consumption paused, sleeping...");
2655 * If the consumer_data_pipe triggered poll go directly to the
2656 * beginning of the loop to update the array. We want to prioritize
2657 * array update over low-priority reads.
2659 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2660 ssize_t pipe_readlen
;
2662 DBG("consumer_data_pipe wake up");
2663 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2664 &new_stream
, sizeof(new_stream
));
2665 if (pipe_readlen
< sizeof(new_stream
)) {
2666 PERROR("Consumer data pipe");
2667 /* Continue so we can at least handle the current stream(s). */
2672 * If the stream is NULL, just ignore it. It's also possible that
2673 * the sessiond poll thread changed the consumer_quit state and is
2674 * waking us up to test it.
2676 if (new_stream
== NULL
) {
2677 validate_endpoint_status_data_stream();
2681 /* Continue to update the local streams and handle prio ones */
2685 /* Handle wakeup pipe. */
2686 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2688 ssize_t pipe_readlen
;
2690 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2692 if (pipe_readlen
< 0) {
2693 PERROR("Consumer data wakeup pipe");
2695 /* We've been awakened to handle stream(s). */
2696 ctx
->has_wakeup
= 0;
2699 /* Take care of high priority channels first. */
2700 for (i
= 0; i
< nb_fd
; i
++) {
2701 health_code_update();
2703 if (local_stream
[i
] == NULL
) {
2706 if (pollfd
[i
].revents
& POLLPRI
) {
2707 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2709 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2710 /* it's ok to have an unavailable sub-buffer */
2711 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2712 /* Clean the stream and free it. */
2713 consumer_del_stream(local_stream
[i
], data_ht
);
2714 local_stream
[i
] = NULL
;
2715 } else if (len
> 0) {
2716 local_stream
[i
]->data_read
= 1;
2722 * If we read high prio channel in this loop, try again
2723 * for more high prio data.
2729 /* Take care of low priority channels. */
2730 for (i
= 0; i
< nb_fd
; i
++) {
2731 health_code_update();
2733 if (local_stream
[i
] == NULL
) {
2736 if ((pollfd
[i
].revents
& POLLIN
) ||
2737 local_stream
[i
]->hangup_flush_done
||
2738 local_stream
[i
]->has_data
) {
2739 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2740 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2741 /* it's ok to have an unavailable sub-buffer */
2742 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2743 /* Clean the stream and free it. */
2744 consumer_del_stream(local_stream
[i
], data_ht
);
2745 local_stream
[i
] = NULL
;
2746 } else if (len
> 0) {
2747 local_stream
[i
]->data_read
= 1;
2752 /* Handle hangup and errors */
2753 for (i
= 0; i
< nb_fd
; i
++) {
2754 health_code_update();
2756 if (local_stream
[i
] == NULL
) {
2759 if (!local_stream
[i
]->hangup_flush_done
2760 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2761 && (the_consumer_data
.type
== LTTNG_CONSUMER32_UST
2762 || the_consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2763 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2765 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2766 /* Attempt read again, for the data we just flushed. */
2767 local_stream
[i
]->data_read
= 1;
2770 * If the poll flag is HUP/ERR/NVAL and we have
2771 * read no data in this pass, we can remove the
2772 * stream from its hash table.
2774 if ((pollfd
[i
].revents
& POLLHUP
)) {
2775 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2776 if (!local_stream
[i
]->data_read
) {
2777 consumer_del_stream(local_stream
[i
], data_ht
);
2778 local_stream
[i
] = NULL
;
2781 } else if (pollfd
[i
].revents
& POLLERR
) {
2782 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2783 if (!local_stream
[i
]->data_read
) {
2784 consumer_del_stream(local_stream
[i
], data_ht
);
2785 local_stream
[i
] = NULL
;
2788 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2789 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2790 if (!local_stream
[i
]->data_read
) {
2791 consumer_del_stream(local_stream
[i
], data_ht
);
2792 local_stream
[i
] = NULL
;
2796 if (local_stream
[i
] != NULL
) {
2797 local_stream
[i
]->data_read
= 0;
2804 DBG("polling thread exiting");
2809 * Close the write side of the pipe so epoll_wait() in
2810 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2811 * read side of the pipe. If we close them both, epoll_wait strangely does
2812 * not return and could create a endless wait period if the pipe is the
2813 * only tracked fd in the poll set. The thread will take care of closing
2816 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2821 ERR("Health error occurred in %s", __func__
);
2823 health_unregister(health_consumerd
);
2825 rcu_unregister_thread();
2830 * Close wake-up end of each stream belonging to the channel. This will
2831 * allow the poll() on the stream read-side to detect when the
2832 * write-side (application) finally closes them.
2835 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2837 struct lttng_ht
*ht
;
2838 struct lttng_consumer_stream
*stream
;
2839 struct lttng_ht_iter iter
;
2841 ht
= the_consumer_data
.stream_per_chan_id_ht
;
2844 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2845 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2846 ht
->match_fct
, &channel
->key
,
2847 &iter
.iter
, stream
, node_channel_id
.node
) {
2849 * Protect against teardown with mutex.
2851 pthread_mutex_lock(&stream
->lock
);
2852 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2855 switch (the_consumer_data
.type
) {
2856 case LTTNG_CONSUMER_KERNEL
:
2858 case LTTNG_CONSUMER32_UST
:
2859 case LTTNG_CONSUMER64_UST
:
2860 if (stream
->metadata_flag
) {
2861 /* Safe and protected by the stream lock. */
2862 lttng_ustconsumer_close_metadata(stream
->chan
);
2865 * Note: a mutex is taken internally within
2866 * liblttng-ust-ctl to protect timer wakeup_fd
2867 * use from concurrent close.
2869 lttng_ustconsumer_close_stream_wakeup(stream
);
2873 ERR("Unknown consumer_data type");
2877 pthread_mutex_unlock(&stream
->lock
);
2882 static void destroy_channel_ht(struct lttng_ht
*ht
)
2884 struct lttng_ht_iter iter
;
2885 struct lttng_consumer_channel
*channel
;
2893 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2894 ret
= lttng_ht_del(ht
, &iter
);
2895 LTTNG_ASSERT(ret
!= 0);
2899 lttng_ht_destroy(ht
);
2903 * This thread polls the channel fds to detect when they are being
2904 * closed. It closes all related streams if the channel is detected as
2905 * closed. It is currently only used as a shim layer for UST because the
2906 * consumerd needs to keep the per-stream wakeup end of pipes open for
2909 void *consumer_thread_channel_poll(void *data
)
2911 int ret
, i
, pollfd
, err
= -1;
2912 uint32_t revents
, nb_fd
;
2913 struct lttng_consumer_channel
*chan
= NULL
;
2914 struct lttng_ht_iter iter
;
2915 struct lttng_ht_node_u64
*node
;
2916 struct lttng_poll_event events
;
2917 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
2918 struct lttng_ht
*channel_ht
;
2920 rcu_register_thread();
2922 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2924 if (testpoint(consumerd_thread_channel
)) {
2925 goto error_testpoint
;
2928 health_code_update();
2930 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2932 /* ENOMEM at this point. Better to bail out. */
2936 DBG("Thread channel poll started");
2938 /* Size is set to 1 for the consumer_channel pipe */
2939 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2941 ERR("Poll set creation failed");
2945 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2951 DBG("Channel main loop started");
2955 health_code_update();
2956 DBG("Channel poll wait");
2957 health_poll_entry();
2958 ret
= lttng_poll_wait(&events
, -1);
2959 DBG("Channel poll return from wait with %d fd(s)",
2960 LTTNG_POLL_GETNB(&events
));
2962 DBG("Channel event caught in thread");
2964 if (errno
== EINTR
) {
2965 ERR("Poll EINTR caught");
2968 if (LTTNG_POLL_GETNB(&events
) == 0) {
2969 err
= 0; /* All is OK */
2976 /* From here, the event is a channel wait fd */
2977 for (i
= 0; i
< nb_fd
; i
++) {
2978 health_code_update();
2980 revents
= LTTNG_POLL_GETEV(&events
, i
);
2981 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2983 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2984 if (revents
& LPOLLIN
) {
2985 enum consumer_channel_action action
;
2988 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2991 ERR("Error reading channel pipe");
2993 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2998 case CONSUMER_CHANNEL_ADD
:
2999 DBG("Adding channel %d to poll set",
3002 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
3005 lttng_ht_add_unique_u64(channel_ht
,
3006 &chan
->wait_fd_node
);
3008 /* Add channel to the global poll events list */
3009 lttng_poll_add(&events
, chan
->wait_fd
,
3010 LPOLLERR
| LPOLLHUP
);
3012 case CONSUMER_CHANNEL_DEL
:
3015 * This command should never be called if the channel
3016 * has streams monitored by either the data or metadata
3017 * thread. The consumer only notify this thread with a
3018 * channel del. command if it receives a destroy
3019 * channel command from the session daemon that send it
3020 * if a command prior to the GET_CHANNEL failed.
3024 chan
= consumer_find_channel(key
);
3027 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
3030 lttng_poll_del(&events
, chan
->wait_fd
);
3031 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
3032 ret
= lttng_ht_del(channel_ht
, &iter
);
3033 LTTNG_ASSERT(ret
== 0);
3035 switch (the_consumer_data
.type
) {
3036 case LTTNG_CONSUMER_KERNEL
:
3038 case LTTNG_CONSUMER32_UST
:
3039 case LTTNG_CONSUMER64_UST
:
3040 health_code_update();
3041 /* Destroy streams that might have been left in the stream list. */
3042 clean_channel_stream_list(chan
);
3045 ERR("Unknown consumer_data type");
3050 * Release our own refcount. Force channel deletion even if
3051 * streams were not initialized.
3053 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3054 consumer_del_channel(chan
);
3059 case CONSUMER_CHANNEL_QUIT
:
3061 * Remove the pipe from the poll set and continue the loop
3062 * since their might be data to consume.
3064 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3067 ERR("Unknown action");
3070 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3071 DBG("Channel thread pipe hung up");
3073 * Remove the pipe from the poll set and continue the loop
3074 * since their might be data to consume.
3076 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3079 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3083 /* Handle other stream */
3089 uint64_t tmp_id
= (uint64_t) pollfd
;
3091 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3093 node
= lttng_ht_iter_get_node_u64(&iter
);
3096 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
3099 /* Check for error event */
3100 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3101 DBG("Channel fd %d is hup|err.", pollfd
);
3103 lttng_poll_del(&events
, chan
->wait_fd
);
3104 ret
= lttng_ht_del(channel_ht
, &iter
);
3105 LTTNG_ASSERT(ret
== 0);
3108 * This will close the wait fd for each stream associated to
3109 * this channel AND monitored by the data/metadata thread thus
3110 * will be clean by the right thread.
3112 consumer_close_channel_streams(chan
);
3114 /* Release our own refcount */
3115 if (!uatomic_sub_return(&chan
->refcount
, 1)
3116 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3117 consumer_del_channel(chan
);
3120 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3125 /* Release RCU lock for the channel looked up */
3133 lttng_poll_clean(&events
);
3135 destroy_channel_ht(channel_ht
);
3138 DBG("Channel poll thread exiting");
3141 ERR("Health error occurred in %s", __func__
);
3143 health_unregister(health_consumerd
);
3144 rcu_unregister_thread();
3148 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3149 struct pollfd
*sockpoll
, int client_socket
)
3154 LTTNG_ASSERT(sockpoll
);
3156 ret
= lttng_consumer_poll_socket(sockpoll
);
3160 DBG("Metadata connection on client_socket");
3162 /* Blocking call, waiting for transmission */
3163 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3164 if (ctx
->consumer_metadata_socket
< 0) {
3165 WARN("On accept metadata");
3176 * This thread listens on the consumerd socket and receives the file
3177 * descriptors from the session daemon.
3179 void *consumer_thread_sessiond_poll(void *data
)
3181 int sock
= -1, client_socket
, ret
, err
= -1;
3183 * structure to poll for incoming data on communication socket avoids
3184 * making blocking sockets.
3186 struct pollfd consumer_sockpoll
[2];
3187 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
3189 rcu_register_thread();
3191 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3193 if (testpoint(consumerd_thread_sessiond
)) {
3194 goto error_testpoint
;
3197 health_code_update();
3199 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3200 unlink(ctx
->consumer_command_sock_path
);
3201 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3202 if (client_socket
< 0) {
3203 ERR("Cannot create command socket");
3207 ret
= lttcomm_listen_unix_sock(client_socket
);
3212 DBG("Sending ready command to lttng-sessiond");
3213 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3214 /* return < 0 on error, but == 0 is not fatal */
3216 ERR("Error sending ready command to lttng-sessiond");
3220 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3221 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3222 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3223 consumer_sockpoll
[1].fd
= client_socket
;
3224 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3226 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3234 DBG("Connection on client_socket");
3236 /* Blocking call, waiting for transmission */
3237 sock
= lttcomm_accept_unix_sock(client_socket
);
3244 * Setup metadata socket which is the second socket connection on the
3245 * command unix socket.
3247 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3256 /* This socket is not useful anymore. */
3257 ret
= close(client_socket
);
3259 PERROR("close client_socket");
3263 /* update the polling structure to poll on the established socket */
3264 consumer_sockpoll
[1].fd
= sock
;
3265 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3268 health_code_update();
3270 health_poll_entry();
3271 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3280 DBG("Incoming command on sock");
3281 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3284 * This could simply be a session daemon quitting. Don't output
3287 DBG("Communication interrupted on command socket");
3291 if (CMM_LOAD_SHARED(consumer_quit
)) {
3292 DBG("consumer_thread_receive_fds received quit from signal");
3293 err
= 0; /* All is OK */
3296 DBG("Received command on sock");
3302 DBG("Consumer thread sessiond poll exiting");
3305 * Close metadata streams since the producer is the session daemon which
3308 * NOTE: for now, this only applies to the UST tracer.
3310 lttng_consumer_close_all_metadata();
3313 * when all fds have hung up, the polling thread
3316 CMM_STORE_SHARED(consumer_quit
, 1);
3319 * Notify the data poll thread to poll back again and test the
3320 * consumer_quit state that we just set so to quit gracefully.
3322 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3324 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3326 notify_health_quit_pipe(health_quit_pipe
);
3328 /* Cleaning up possibly open sockets. */
3332 PERROR("close sock sessiond poll");
3335 if (client_socket
>= 0) {
3336 ret
= close(client_socket
);
3338 PERROR("close client_socket sessiond poll");
3345 ERR("Health error occurred in %s", __func__
);
3347 health_unregister(health_consumerd
);
3349 rcu_unregister_thread();
3353 static int post_consume(struct lttng_consumer_stream
*stream
,
3354 const struct stream_subbuffer
*subbuffer
,
3355 struct lttng_consumer_local_data
*ctx
)
3359 const size_t count
= lttng_dynamic_array_get_count(
3360 &stream
->read_subbuffer_ops
.post_consume_cbs
);
3362 for (i
= 0; i
< count
; i
++) {
3363 const post_consume_cb op
= *(post_consume_cb
*) lttng_dynamic_array_get_element(
3364 &stream
->read_subbuffer_ops
.post_consume_cbs
,
3367 ret
= op(stream
, subbuffer
, ctx
);
3376 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3377 struct lttng_consumer_local_data
*ctx
,
3378 bool locked_by_caller
)
3380 ssize_t ret
, written_bytes
= 0;
3382 struct stream_subbuffer subbuffer
= {};
3383 enum get_next_subbuffer_status get_next_status
;
3385 if (!locked_by_caller
) {
3386 stream
->read_subbuffer_ops
.lock(stream
);
3388 stream
->read_subbuffer_ops
.assert_locked(stream
);
3391 if (stream
->read_subbuffer_ops
.on_wake_up
) {
3392 ret
= stream
->read_subbuffer_ops
.on_wake_up(stream
);
3399 * If the stream was flagged to be ready for rotation before we extract
3400 * the next packet, rotate it now.
3402 if (stream
->rotate_ready
) {
3403 DBG("Rotate stream before consuming data");
3404 ret
= lttng_consumer_rotate_stream(stream
);
3406 ERR("Stream rotation error before consuming data");
3411 get_next_status
= stream
->read_subbuffer_ops
.get_next_subbuffer(
3412 stream
, &subbuffer
);
3413 switch (get_next_status
) {
3414 case GET_NEXT_SUBBUFFER_STATUS_OK
:
3416 case GET_NEXT_SUBBUFFER_STATUS_NO_DATA
:
3420 case GET_NEXT_SUBBUFFER_STATUS_ERROR
:
3427 ret
= stream
->read_subbuffer_ops
.pre_consume_subbuffer(
3428 stream
, &subbuffer
);
3430 goto error_put_subbuf
;
3433 written_bytes
= stream
->read_subbuffer_ops
.consume_subbuffer(
3434 ctx
, stream
, &subbuffer
);
3435 if (written_bytes
<= 0) {
3436 ERR("Error consuming subbuffer: (%zd)", written_bytes
);
3437 ret
= (int) written_bytes
;
3438 goto error_put_subbuf
;
3441 ret
= stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3446 ret
= post_consume(stream
, &subbuffer
, ctx
);
3452 * After extracting the packet, we check if the stream is now ready to
3453 * be rotated and perform the action immediately.
3455 * Don't overwrite `ret` as callers expect the number of bytes
3456 * consumed to be returned on success.
3458 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
3459 if (rotation_ret
== 1) {
3460 rotation_ret
= lttng_consumer_rotate_stream(stream
);
3461 if (rotation_ret
< 0) {
3463 ERR("Stream rotation error after consuming data");
3467 } else if (rotation_ret
< 0) {
3469 ERR("Failed to check if stream was ready to rotate after consuming data");
3474 if (stream
->read_subbuffer_ops
.on_sleep
) {
3475 stream
->read_subbuffer_ops
.on_sleep(stream
, ctx
);
3478 ret
= written_bytes
;
3480 if (!locked_by_caller
) {
3481 stream
->read_subbuffer_ops
.unlock(stream
);
3486 (void) stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3490 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3492 switch (the_consumer_data
.type
) {
3493 case LTTNG_CONSUMER_KERNEL
:
3494 return lttng_kconsumer_on_recv_stream(stream
);
3495 case LTTNG_CONSUMER32_UST
:
3496 case LTTNG_CONSUMER64_UST
:
3497 return lttng_ustconsumer_on_recv_stream(stream
);
3499 ERR("Unknown consumer_data type");
3506 * Allocate and set consumer data hash tables.
3508 int lttng_consumer_init(void)
3510 the_consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3511 if (!the_consumer_data
.channel_ht
) {
3515 the_consumer_data
.channels_by_session_id_ht
=
3516 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3517 if (!the_consumer_data
.channels_by_session_id_ht
) {
3521 the_consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3522 if (!the_consumer_data
.relayd_ht
) {
3526 the_consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3527 if (!the_consumer_data
.stream_list_ht
) {
3531 the_consumer_data
.stream_per_chan_id_ht
=
3532 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3533 if (!the_consumer_data
.stream_per_chan_id_ht
) {
3537 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3542 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3547 the_consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3548 if (!the_consumer_data
.chunk_registry
) {
3559 * Process the ADD_RELAYD command receive by a consumer.
3561 * This will create a relayd socket pair and add it to the relayd hash table.
3562 * The caller MUST acquire a RCU read side lock before calling it.
3564 void consumer_add_relayd_socket(uint64_t net_seq_idx
,
3566 struct lttng_consumer_local_data
*ctx
,
3568 struct pollfd
*consumer_sockpoll
,
3569 uint64_t sessiond_id
,
3570 uint64_t relayd_session_id
,
3571 uint32_t relayd_version_major
,
3572 uint32_t relayd_version_minor
,
3573 enum lttcomm_sock_proto relayd_socket_protocol
)
3575 int fd
= -1, ret
= -1, relayd_created
= 0;
3576 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3577 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3580 LTTNG_ASSERT(sock
>= 0);
3581 ASSERT_RCU_READ_LOCKED();
3583 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3585 /* Get relayd reference if exists. */
3586 relayd
= consumer_find_relayd(net_seq_idx
);
3587 if (relayd
== NULL
) {
3588 LTTNG_ASSERT(sock_type
== LTTNG_STREAM_CONTROL
);
3589 /* Not found. Allocate one. */
3590 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3591 if (relayd
== NULL
) {
3592 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3595 relayd
->sessiond_session_id
= sessiond_id
;
3600 * This code path MUST continue to the consumer send status message to
3601 * we can notify the session daemon and continue our work without
3602 * killing everything.
3606 * relayd key should never be found for control socket.
3608 LTTNG_ASSERT(sock_type
!= LTTNG_STREAM_CONTROL
);
3611 /* First send a status message before receiving the fds. */
3612 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3614 /* Somehow, the session daemon is not responding anymore. */
3615 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3616 goto error_nosignal
;
3619 /* Poll on consumer socket. */
3620 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3622 /* Needing to exit in the middle of a command: error. */
3623 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3624 goto error_nosignal
;
3627 /* Get relayd socket from session daemon */
3628 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3629 if (ret
!= sizeof(fd
)) {
3630 fd
= -1; /* Just in case it gets set with an invalid value. */
3633 * Failing to receive FDs might indicate a major problem such as
3634 * reaching a fd limit during the receive where the kernel returns a
3635 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3636 * don't take any chances and stop everything.
3638 * XXX: Feature request #558 will fix that and avoid this possible
3639 * issue when reaching the fd limit.
3641 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3642 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3646 /* Copy socket information and received FD */
3647 switch (sock_type
) {
3648 case LTTNG_STREAM_CONTROL
:
3649 /* Copy received lttcomm socket */
3650 ret
= lttcomm_populate_sock_from_open_socket(
3651 &relayd
->control_sock
.sock
, fd
,
3652 relayd_socket_protocol
);
3654 /* Assign version values. */
3655 relayd
->control_sock
.major
= relayd_version_major
;
3656 relayd
->control_sock
.minor
= relayd_version_minor
;
3658 relayd
->relayd_session_id
= relayd_session_id
;
3661 case LTTNG_STREAM_DATA
:
3662 /* Copy received lttcomm socket */
3663 ret
= lttcomm_populate_sock_from_open_socket(
3664 &relayd
->data_sock
.sock
, fd
,
3665 relayd_socket_protocol
);
3666 /* Assign version values. */
3667 relayd
->data_sock
.major
= relayd_version_major
;
3668 relayd
->data_sock
.minor
= relayd_version_minor
;
3671 ERR("Unknown relayd socket type (%d)", sock_type
);
3672 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3677 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3681 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3682 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3683 relayd
->net_seq_idx
, fd
);
3685 * We gave the ownership of the fd to the relayd structure. Set the
3686 * fd to -1 so we don't call close() on it in the error path below.
3690 /* We successfully added the socket. Send status back. */
3691 ret
= consumer_send_status_msg(sock
, ret_code
);
3693 /* Somehow, the session daemon is not responding anymore. */
3694 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3695 goto error_nosignal
;
3699 * Add relayd socket pair to consumer data hashtable. If object already
3700 * exists or on error, the function gracefully returns.
3709 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3710 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3714 /* Close received socket if valid. */
3717 PERROR("close received socket");
3721 if (relayd_created
) {
3727 * Search for a relayd associated to the session id and return the reference.
3729 * A rcu read side lock MUST be acquire before calling this function and locked
3730 * until the relayd object is no longer necessary.
3732 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3734 struct lttng_ht_iter iter
;
3735 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3737 ASSERT_RCU_READ_LOCKED();
3739 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3740 cds_lfht_for_each_entry(the_consumer_data
.relayd_ht
->ht
, &iter
.iter
,
3741 relayd
, node
.node
) {
3743 * Check by sessiond id which is unique here where the relayd session
3744 * id might not be when having multiple relayd.
3746 if (relayd
->sessiond_session_id
== id
) {
3747 /* Found the relayd. There can be only one per id. */
3759 * Check if for a given session id there is still data needed to be extract
3762 * Return 1 if data is pending or else 0 meaning ready to be read.
3764 int consumer_data_pending(uint64_t id
)
3767 struct lttng_ht_iter iter
;
3768 struct lttng_ht
*ht
;
3769 struct lttng_consumer_stream
*stream
;
3770 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3771 int (*data_pending
)(struct lttng_consumer_stream
*);
3773 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3776 pthread_mutex_lock(&the_consumer_data
.lock
);
3778 switch (the_consumer_data
.type
) {
3779 case LTTNG_CONSUMER_KERNEL
:
3780 data_pending
= lttng_kconsumer_data_pending
;
3782 case LTTNG_CONSUMER32_UST
:
3783 case LTTNG_CONSUMER64_UST
:
3784 data_pending
= lttng_ustconsumer_data_pending
;
3787 ERR("Unknown consumer data type");
3791 /* Ease our life a bit */
3792 ht
= the_consumer_data
.stream_list_ht
;
3794 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3795 ht
->hash_fct(&id
, lttng_ht_seed
),
3797 &iter
.iter
, stream
, node_session_id
.node
) {
3798 pthread_mutex_lock(&stream
->lock
);
3801 * A removed node from the hash table indicates that the stream has
3802 * been deleted thus having a guarantee that the buffers are closed
3803 * on the consumer side. However, data can still be transmitted
3804 * over the network so don't skip the relayd check.
3806 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3808 /* Check the stream if there is data in the buffers. */
3809 ret
= data_pending(stream
);
3811 pthread_mutex_unlock(&stream
->lock
);
3816 pthread_mutex_unlock(&stream
->lock
);
3819 relayd
= find_relayd_by_session_id(id
);
3821 unsigned int is_data_inflight
= 0;
3823 /* Send init command for data pending. */
3824 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3825 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3826 relayd
->relayd_session_id
);
3828 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3829 /* Communication error thus the relayd so no data pending. */
3830 goto data_not_pending
;
3833 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3834 ht
->hash_fct(&id
, lttng_ht_seed
),
3836 &iter
.iter
, stream
, node_session_id
.node
) {
3837 if (stream
->metadata_flag
) {
3838 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3839 stream
->relayd_stream_id
);
3841 ret
= relayd_data_pending(&relayd
->control_sock
,
3842 stream
->relayd_stream_id
,
3843 stream
->next_net_seq_num
- 1);
3847 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3849 } else if (ret
< 0) {
3850 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3851 lttng_consumer_cleanup_relayd(relayd
);
3852 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3853 goto data_not_pending
;
3857 /* Send end command for data pending. */
3858 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3859 relayd
->relayd_session_id
, &is_data_inflight
);
3860 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3862 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3863 lttng_consumer_cleanup_relayd(relayd
);
3864 goto data_not_pending
;
3866 if (is_data_inflight
) {
3872 * Finding _no_ node in the hash table and no inflight data means that the
3873 * stream(s) have been removed thus data is guaranteed to be available for
3874 * analysis from the trace files.
3878 /* Data is available to be read by a viewer. */
3879 pthread_mutex_unlock(&the_consumer_data
.lock
);
3884 /* Data is still being extracted from buffers. */
3885 pthread_mutex_unlock(&the_consumer_data
.lock
);
3891 * Send a ret code status message to the sessiond daemon.
3893 * Return the sendmsg() return value.
3895 int consumer_send_status_msg(int sock
, int ret_code
)
3897 struct lttcomm_consumer_status_msg msg
;
3899 memset(&msg
, 0, sizeof(msg
));
3900 msg
.ret_code
= (lttcomm_return_code
) ret_code
;
3902 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3906 * Send a channel status message to the sessiond daemon.
3908 * Return the sendmsg() return value.
3910 int consumer_send_status_channel(int sock
,
3911 struct lttng_consumer_channel
*channel
)
3913 struct lttcomm_consumer_status_channel msg
;
3915 LTTNG_ASSERT(sock
>= 0);
3917 memset(&msg
, 0, sizeof(msg
));
3919 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3921 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3922 msg
.key
= channel
->key
;
3923 msg
.stream_count
= channel
->streams
.count
;
3926 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3929 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3930 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3931 uint64_t max_sb_size
)
3933 unsigned long start_pos
;
3935 if (!nb_packets_per_stream
) {
3936 return consumed_pos
; /* Grab everything */
3938 start_pos
= produced_pos
- lttng_offset_align_floor(produced_pos
, max_sb_size
);
3939 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3940 if ((long) (start_pos
- consumed_pos
) < 0) {
3941 return consumed_pos
; /* Grab everything */
3946 /* Stream lock must be held by the caller. */
3947 static int sample_stream_positions(struct lttng_consumer_stream
*stream
,
3948 unsigned long *produced
, unsigned long *consumed
)
3952 ASSERT_LOCKED(stream
->lock
);
3954 ret
= lttng_consumer_sample_snapshot_positions(stream
);
3956 ERR("Failed to sample snapshot positions");
3960 ret
= lttng_consumer_get_produced_snapshot(stream
, produced
);
3962 ERR("Failed to sample produced position");
3966 ret
= lttng_consumer_get_consumed_snapshot(stream
, consumed
);
3968 ERR("Failed to sample consumed position");
3977 * Sample the rotate position for all the streams of a channel. If a stream
3978 * is already at the rotate position (produced == consumed), we flag it as
3979 * ready for rotation. The rotation of ready streams occurs after we have
3980 * replied to the session daemon that we have finished sampling the positions.
3981 * Must be called with RCU read-side lock held to ensure existence of channel.
3983 * Returns 0 on success, < 0 on error
3985 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
3986 uint64_t key
, uint64_t relayd_id
)
3989 struct lttng_consumer_stream
*stream
;
3990 struct lttng_ht_iter iter
;
3991 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
3992 struct lttng_dynamic_array stream_rotation_positions
;
3993 uint64_t next_chunk_id
, stream_count
= 0;
3994 enum lttng_trace_chunk_status chunk_status
;
3995 const bool is_local_trace
= relayd_id
== -1ULL;
3996 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3997 bool rotating_to_new_chunk
= true;
3998 /* Array of `struct lttng_consumer_stream *` */
3999 struct lttng_dynamic_pointer_array streams_packet_to_open
;
4002 ASSERT_RCU_READ_LOCKED();
4004 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
4006 lttng_dynamic_array_init(&stream_rotation_positions
,
4007 sizeof(struct relayd_stream_rotation_position
), NULL
);
4008 lttng_dynamic_pointer_array_init(&streams_packet_to_open
, NULL
);
4012 pthread_mutex_lock(&channel
->lock
);
4013 LTTNG_ASSERT(channel
->trace_chunk
);
4014 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
,
4016 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4018 goto end_unlock_channel
;
4021 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4022 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4023 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4024 stream
, node_channel_id
.node
) {
4025 unsigned long produced_pos
= 0, consumed_pos
= 0;
4027 health_code_update();
4030 * Lock stream because we are about to change its state.
4032 pthread_mutex_lock(&stream
->lock
);
4034 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
4035 rotating_to_new_chunk
= false;
4039 * Do not flush a packet when rotating from a NULL trace
4040 * chunk. The stream has no means to output data, and the prior
4041 * rotation which rotated to NULL performed that side-effect
4042 * already. No new data can be produced when a stream has no
4043 * associated trace chunk (e.g. a stop followed by a rotate).
4045 if (stream
->trace_chunk
) {
4048 if (stream
->metadata_flag
) {
4050 * Don't produce an empty metadata packet,
4051 * simply close the current one.
4053 * Metadata is regenerated on every trace chunk
4054 * switch; there is no concern that no data was
4057 flush_active
= true;
4060 * Only flush an empty packet if the "packet
4061 * open" could not be performed on transition
4062 * to a new trace chunk and no packets were
4063 * consumed within the chunk's lifetime.
4065 if (stream
->opened_packet_in_current_trace_chunk
) {
4066 flush_active
= true;
4069 * Stream could have been full at the
4070 * time of rotation, but then have had
4071 * no activity at all.
4073 * It is important to flush a packet
4074 * to prevent 0-length files from being
4075 * produced as most viewers choke on
4078 * Unfortunately viewers will not be
4079 * able to know that tracing was active
4080 * for this stream during this trace
4083 ret
= sample_stream_positions(stream
, &produced_pos
, &consumed_pos
);
4085 goto end_unlock_stream
;
4089 * Don't flush an empty packet if data
4090 * was produced; it will be consumed
4091 * before the rotation completes.
4093 flush_active
= produced_pos
!= consumed_pos
;
4094 if (!flush_active
) {
4095 const char *trace_chunk_name
;
4096 uint64_t trace_chunk_id
;
4098 chunk_status
= lttng_trace_chunk_get_name(
4099 stream
->trace_chunk
,
4102 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NONE
) {
4103 trace_chunk_name
= "none";
4107 * Consumer trace chunks are
4110 chunk_status
= lttng_trace_chunk_get_id(
4111 stream
->trace_chunk
,
4113 LTTNG_ASSERT(chunk_status
==
4114 LTTNG_TRACE_CHUNK_STATUS_OK
);
4116 DBG("Unable to open packet for stream during trace chunk's lifetime. "
4117 "Flushing an empty packet to prevent an empty file from being created: "
4118 "stream id = %" PRIu64
", trace chunk name = `%s`, trace chunk id = %" PRIu64
,
4119 stream
->key
, trace_chunk_name
, trace_chunk_id
);
4125 * Close the current packet before sampling the
4126 * ring buffer positions.
4128 ret
= consumer_stream_flush_buffer(stream
, flush_active
);
4130 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4132 goto end_unlock_stream
;
4136 ret
= lttng_consumer_take_snapshot(stream
);
4137 if (ret
< 0 && ret
!= -ENODATA
&& ret
!= -EAGAIN
) {
4138 ERR("Failed to sample snapshot position during channel rotation");
4139 goto end_unlock_stream
;
4142 ret
= lttng_consumer_get_produced_snapshot(stream
,
4145 ERR("Failed to sample produced position during channel rotation");
4146 goto end_unlock_stream
;
4149 ret
= lttng_consumer_get_consumed_snapshot(stream
,
4152 ERR("Failed to sample consumed position during channel rotation");
4153 goto end_unlock_stream
;
4157 * Align produced position on the start-of-packet boundary of the first
4158 * packet going into the next trace chunk.
4160 produced_pos
= lttng_align_floor(produced_pos
, stream
->max_sb_size
);
4161 if (consumed_pos
== produced_pos
) {
4162 DBG("Set rotate ready for stream %" PRIu64
" produced = %lu consumed = %lu",
4163 stream
->key
, produced_pos
, consumed_pos
);
4164 stream
->rotate_ready
= true;
4166 DBG("Different consumed and produced positions "
4167 "for stream %" PRIu64
" produced = %lu consumed = %lu",
4168 stream
->key
, produced_pos
, consumed_pos
);
4171 * The rotation position is based on the packet_seq_num of the
4172 * packet following the last packet that was consumed for this
4173 * stream, incremented by the offset between produced and
4174 * consumed positions. This rotation position is a lower bound
4175 * (inclusive) at which the next trace chunk starts. Since it
4176 * is a lower bound, it is OK if the packet_seq_num does not
4177 * correspond exactly to the same packet identified by the
4178 * consumed_pos, which can happen in overwrite mode.
4180 if (stream
->sequence_number_unavailable
) {
4182 * Rotation should never be performed on a session which
4183 * interacts with a pre-2.8 lttng-modules, which does
4184 * not implement packet sequence number.
4186 ERR("Failure to rotate stream %" PRIu64
": sequence number unavailable",
4189 goto end_unlock_stream
;
4191 stream
->rotate_position
= stream
->last_sequence_number
+ 1 +
4192 ((produced_pos
- consumed_pos
) / stream
->max_sb_size
);
4193 DBG("Set rotation position for stream %" PRIu64
" at position %" PRIu64
,
4194 stream
->key
, stream
->rotate_position
);
4196 if (!is_local_trace
) {
4198 * The relay daemon control protocol expects a rotation
4199 * position as "the sequence number of the first packet
4200 * _after_ the current trace chunk".
4202 const struct relayd_stream_rotation_position position
= {
4203 .stream_id
= stream
->relayd_stream_id
,
4204 .rotate_at_seq_num
= stream
->rotate_position
,
4207 ret
= lttng_dynamic_array_add_element(
4208 &stream_rotation_positions
,
4211 ERR("Failed to allocate stream rotation position");
4212 goto end_unlock_stream
;
4217 stream
->opened_packet_in_current_trace_chunk
= false;
4219 if (rotating_to_new_chunk
&& !stream
->metadata_flag
) {
4221 * Attempt to flush an empty packet as close to the
4222 * rotation point as possible. In the event where a
4223 * stream remains inactive after the rotation point,
4224 * this ensures that the new trace chunk has a
4225 * beginning timestamp set at the begining of the
4226 * trace chunk instead of only creating an empty
4227 * packet when the trace chunk is stopped.
4229 * This indicates to the viewers that the stream
4230 * was being recorded, but more importantly it
4231 * allows viewers to determine a useable trace
4234 * This presents a problem in the case where the
4235 * ring-buffer is completely full.
4237 * Consider the following scenario:
4238 * - The consumption of data is slow (slow network,
4240 * - The ring buffer is full,
4241 * - A rotation is initiated,
4242 * - The flush below does nothing (no space left to
4243 * open a new packet),
4244 * - The other streams rotate very soon, and new
4245 * data is produced in the new chunk,
4246 * - This stream completes its rotation long after the
4247 * rotation was initiated
4248 * - The session is stopped before any event can be
4249 * produced in this stream's buffers.
4251 * The resulting trace chunk will have a single packet
4252 * temporaly at the end of the trace chunk for this
4253 * stream making the stream intersection more narrow
4254 * than it should be.
4256 * To work-around this, an empty flush is performed
4257 * after the first consumption of a packet during a
4258 * rotation if open_packet fails. The idea is that
4259 * consuming a packet frees enough space to switch
4260 * packets in this scenario and allows the tracer to
4261 * "stamp" the beginning of the new trace chunk at the
4262 * earliest possible point.
4264 * The packet open is performed after the channel
4265 * rotation to ensure that no attempt to open a packet
4266 * is performed in a stream that has no active trace
4269 ret
= lttng_dynamic_pointer_array_add_pointer(
4270 &streams_packet_to_open
, stream
);
4272 PERROR("Failed to add a stream pointer to array of streams in which to open a packet");
4274 goto end_unlock_stream
;
4278 pthread_mutex_unlock(&stream
->lock
);
4282 if (!is_local_trace
) {
4283 relayd
= consumer_find_relayd(relayd_id
);
4285 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4287 goto end_unlock_channel
;
4290 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4291 ret
= relayd_rotate_streams(&relayd
->control_sock
, stream_count
,
4292 rotating_to_new_chunk
? &next_chunk_id
: NULL
,
4293 (const struct relayd_stream_rotation_position
*)
4294 stream_rotation_positions
.buffer
4296 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4298 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4299 relayd
->net_seq_idx
);
4300 lttng_consumer_cleanup_relayd(relayd
);
4301 goto end_unlock_channel
;
4305 for (stream_idx
= 0;
4306 stream_idx
< lttng_dynamic_pointer_array_get_count(
4307 &streams_packet_to_open
);
4309 enum consumer_stream_open_packet_status status
;
4311 stream
= (lttng_consumer_stream
*) lttng_dynamic_pointer_array_get_pointer(
4312 &streams_packet_to_open
, stream_idx
);
4314 pthread_mutex_lock(&stream
->lock
);
4315 status
= consumer_stream_open_packet(stream
);
4316 pthread_mutex_unlock(&stream
->lock
);
4318 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
4319 DBG("Opened a packet after a rotation: stream id = %" PRIu64
4320 ", channel name = %s, session id = %" PRIu64
,
4321 stream
->key
, stream
->chan
->name
,
4322 stream
->chan
->session_id
);
4324 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
4326 * Can't open a packet as there is no space left
4327 * in the buffer. A new packet will be opened
4328 * once one has been consumed.
4330 DBG("No space left to open a packet after a rotation: stream id = %" PRIu64
4331 ", channel name = %s, session id = %" PRIu64
,
4332 stream
->key
, stream
->chan
->name
,
4333 stream
->chan
->session_id
);
4335 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
4336 /* Logged by callee. */
4338 goto end_unlock_channel
;
4344 pthread_mutex_unlock(&channel
->lock
);
4349 pthread_mutex_unlock(&stream
->lock
);
4351 pthread_mutex_unlock(&channel
->lock
);
4354 lttng_dynamic_array_reset(&stream_rotation_positions
);
4355 lttng_dynamic_pointer_array_reset(&streams_packet_to_open
);
4360 int consumer_clear_buffer(struct lttng_consumer_stream
*stream
)
4363 unsigned long consumed_pos_before
, consumed_pos_after
;
4365 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4367 ERR("Taking snapshot positions");
4371 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_before
);
4373 ERR("Consumed snapshot position");
4377 switch (the_consumer_data
.type
) {
4378 case LTTNG_CONSUMER_KERNEL
:
4379 ret
= kernctl_buffer_clear(stream
->wait_fd
);
4381 ERR("Failed to clear kernel stream (ret = %d)", ret
);
4385 case LTTNG_CONSUMER32_UST
:
4386 case LTTNG_CONSUMER64_UST
:
4387 ret
= lttng_ustconsumer_clear_buffer(stream
);
4389 ERR("Failed to clear ust stream (ret = %d)", ret
);
4394 ERR("Unknown consumer_data type");
4398 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4400 ERR("Taking snapshot positions");
4403 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_after
);
4405 ERR("Consumed snapshot position");
4408 DBG("clear: before: %lu after: %lu", consumed_pos_before
, consumed_pos_after
);
4414 int consumer_clear_stream(struct lttng_consumer_stream
*stream
)
4418 ret
= consumer_stream_flush_buffer(stream
, 1);
4420 ERR("Failed to flush stream %" PRIu64
" during channel clear",
4422 ret
= LTTCOMM_CONSUMERD_FATAL
;
4426 ret
= consumer_clear_buffer(stream
);
4428 ERR("Failed to clear stream %" PRIu64
" during channel clear",
4430 ret
= LTTCOMM_CONSUMERD_FATAL
;
4434 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4440 int consumer_clear_unmonitored_channel(struct lttng_consumer_channel
*channel
)
4443 struct lttng_consumer_stream
*stream
;
4446 pthread_mutex_lock(&channel
->lock
);
4447 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
4448 health_code_update();
4449 pthread_mutex_lock(&stream
->lock
);
4450 ret
= consumer_clear_stream(stream
);
4454 pthread_mutex_unlock(&stream
->lock
);
4456 pthread_mutex_unlock(&channel
->lock
);
4461 pthread_mutex_unlock(&stream
->lock
);
4462 pthread_mutex_unlock(&channel
->lock
);
4468 * Check if a stream is ready to be rotated after extracting it.
4470 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4471 * error. Stream lock must be held.
4473 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4475 DBG("Check is rotate ready for stream %" PRIu64
4476 " ready %u rotate_position %" PRIu64
4477 " last_sequence_number %" PRIu64
,
4478 stream
->key
, stream
->rotate_ready
,
4479 stream
->rotate_position
, stream
->last_sequence_number
);
4480 if (stream
->rotate_ready
) {
4485 * If packet seq num is unavailable, it means we are interacting
4486 * with a pre-2.8 lttng-modules which does not implement the
4487 * sequence number. Rotation should never be used by sessiond in this
4490 if (stream
->sequence_number_unavailable
) {
4491 ERR("Internal error: rotation used on stream %" PRIu64
4492 " with unavailable sequence number",
4497 if (stream
->rotate_position
== -1ULL ||
4498 stream
->last_sequence_number
== -1ULL) {
4503 * Rotate position not reached yet. The stream rotate position is
4504 * the position of the next packet belonging to the next trace chunk,
4505 * but consumerd considers rotation ready when reaching the last
4506 * packet of the current chunk, hence the "rotate_position - 1".
4509 DBG("Check is rotate ready for stream %" PRIu64
4510 " last_sequence_number %" PRIu64
4511 " rotate_position %" PRIu64
,
4512 stream
->key
, stream
->last_sequence_number
,
4513 stream
->rotate_position
);
4514 if (stream
->last_sequence_number
>= stream
->rotate_position
- 1) {
4522 * Reset the state for a stream after a rotation occurred.
4524 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4526 DBG("lttng_consumer_reset_stream_rotate_state for stream %" PRIu64
,
4528 stream
->rotate_position
= -1ULL;
4529 stream
->rotate_ready
= false;
4533 * Perform the rotation a local stream file.
4536 int rotate_local_stream(struct lttng_consumer_stream
*stream
)
4540 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4543 stream
->tracefile_size_current
= 0;
4544 stream
->tracefile_count_current
= 0;
4546 if (stream
->out_fd
>= 0) {
4547 ret
= close(stream
->out_fd
);
4549 PERROR("Failed to close stream out_fd of channel \"%s\"",
4550 stream
->chan
->name
);
4552 stream
->out_fd
= -1;
4555 if (stream
->index_file
) {
4556 lttng_index_file_put(stream
->index_file
);
4557 stream
->index_file
= NULL
;
4560 if (!stream
->trace_chunk
) {
4564 ret
= consumer_stream_create_output_files(stream
, true);
4570 * Performs the stream rotation for the rotate session feature if needed.
4571 * It must be called with the channel and stream locks held.
4573 * Return 0 on success, a negative number of error.
4575 int lttng_consumer_rotate_stream(struct lttng_consumer_stream
*stream
)
4579 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4582 * Update the stream's 'current' chunk to the session's (channel)
4583 * now-current chunk.
4585 lttng_trace_chunk_put(stream
->trace_chunk
);
4586 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4588 * A channel can be rotated and not have a "next" chunk
4589 * to transition to. In that case, the channel's "current chunk"
4590 * has not been closed yet, but it has not been updated to
4591 * a "next" trace chunk either. Hence, the stream, like its
4592 * parent channel, becomes part of no chunk and can't output
4593 * anything until a new trace chunk is created.
4595 stream
->trace_chunk
= NULL
;
4596 } else if (stream
->chan
->trace_chunk
&&
4597 !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4598 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4603 * Update the stream's trace chunk to its parent channel's
4604 * current trace chunk.
4606 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4609 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4610 ret
= rotate_local_stream(stream
);
4612 ERR("Failed to rotate stream, ret = %i", ret
);
4617 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4619 * If the stream has transitioned to a new trace
4620 * chunk, the metadata should be re-dumped to the
4623 * However, it is possible for a stream to transition to
4624 * a "no-chunk" state. This can happen if a rotation
4625 * occurs on an inactive session. In such cases, the metadata
4626 * regeneration will happen when the next trace chunk is
4629 ret
= consumer_metadata_stream_dump(stream
);
4634 lttng_consumer_reset_stream_rotate_state(stream
);
4643 * Rotate all the ready streams now.
4645 * This is especially important for low throughput streams that have already
4646 * been consumed, we cannot wait for their next packet to perform the
4648 * Need to be called with RCU read-side lock held to ensure existence of
4651 * Returns 0 on success, < 0 on error
4653 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
,
4657 struct lttng_consumer_stream
*stream
;
4658 struct lttng_ht_iter iter
;
4659 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
4661 ASSERT_RCU_READ_LOCKED();
4665 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4667 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4668 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4669 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4670 stream
, node_channel_id
.node
) {
4671 health_code_update();
4673 pthread_mutex_lock(&stream
->chan
->lock
);
4674 pthread_mutex_lock(&stream
->lock
);
4676 if (!stream
->rotate_ready
) {
4677 pthread_mutex_unlock(&stream
->lock
);
4678 pthread_mutex_unlock(&stream
->chan
->lock
);
4681 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4683 ret
= lttng_consumer_rotate_stream(stream
);
4684 pthread_mutex_unlock(&stream
->lock
);
4685 pthread_mutex_unlock(&stream
->chan
->lock
);
4698 enum lttcomm_return_code
lttng_consumer_init_command(
4699 struct lttng_consumer_local_data
*ctx
,
4700 const lttng_uuid sessiond_uuid
)
4702 enum lttcomm_return_code ret
;
4703 char uuid_str
[LTTNG_UUID_STR_LEN
];
4705 if (ctx
->sessiond_uuid
.is_set
) {
4706 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4710 ctx
->sessiond_uuid
.is_set
= true;
4711 memcpy(ctx
->sessiond_uuid
.value
, sessiond_uuid
, sizeof(lttng_uuid
));
4712 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4713 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4714 DBG("Received session daemon UUID: %s", uuid_str
);
4719 enum lttcomm_return_code
lttng_consumer_create_trace_chunk(
4720 const uint64_t *relayd_id
, uint64_t session_id
,
4722 time_t chunk_creation_timestamp
,
4723 const char *chunk_override_name
,
4724 const struct lttng_credentials
*credentials
,
4725 struct lttng_directory_handle
*chunk_directory_handle
)
4728 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4729 struct lttng_trace_chunk
*created_chunk
= NULL
, *published_chunk
= NULL
;
4730 enum lttng_trace_chunk_status chunk_status
;
4731 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4732 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4733 const char *relayd_id_str
= "(none)";
4734 const char *creation_timestamp_str
;
4735 struct lttng_ht_iter iter
;
4736 struct lttng_consumer_channel
*channel
;
4739 /* Only used for logging purposes. */
4740 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4741 "%" PRIu64
, *relayd_id
);
4742 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4743 relayd_id_str
= relayd_id_buffer
;
4745 relayd_id_str
= "(formatting error)";
4749 /* Local protocol error. */
4750 LTTNG_ASSERT(chunk_creation_timestamp
);
4751 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4752 creation_timestamp_buffer
,
4753 sizeof(creation_timestamp_buffer
));
4754 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
4755 "(formatting error)";
4757 DBG("Consumer create trace chunk command: relay_id = %s"
4758 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4759 ", chunk_override_name = %s"
4760 ", chunk_creation_timestamp = %s",
4761 relayd_id_str
, session_id
, chunk_id
,
4762 chunk_override_name
? : "(none)",
4763 creation_timestamp_str
);
4766 * The trace chunk registry, as used by the consumer daemon, implicitly
4767 * owns the trace chunks. This is only needed in the consumer since
4768 * the consumer has no notion of a session beyond session IDs being
4769 * used to identify other objects.
4771 * The lttng_trace_chunk_registry_publish() call below provides a
4772 * reference which is not released; it implicitly becomes the session
4773 * daemon's reference to the chunk in the consumer daemon.
4775 * The lifetime of trace chunks in the consumer daemon is managed by
4776 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4777 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4779 created_chunk
= lttng_trace_chunk_create(chunk_id
,
4780 chunk_creation_timestamp
, NULL
);
4781 if (!created_chunk
) {
4782 ERR("Failed to create trace chunk");
4783 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4787 if (chunk_override_name
) {
4788 chunk_status
= lttng_trace_chunk_override_name(created_chunk
,
4789 chunk_override_name
);
4790 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4791 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4796 if (chunk_directory_handle
) {
4797 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
,
4799 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4800 ERR("Failed to set trace chunk credentials");
4801 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4805 * The consumer daemon has no ownership of the chunk output
4808 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
,
4809 chunk_directory_handle
);
4810 chunk_directory_handle
= NULL
;
4811 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4812 ERR("Failed to set trace chunk's directory handle");
4813 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4818 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4819 the_consumer_data
.chunk_registry
, session_id
,
4821 lttng_trace_chunk_put(created_chunk
);
4822 created_chunk
= NULL
;
4823 if (!published_chunk
) {
4824 ERR("Failed to publish trace chunk");
4825 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4830 cds_lfht_for_each_entry_duplicate(
4831 the_consumer_data
.channels_by_session_id_ht
->ht
,
4832 the_consumer_data
.channels_by_session_id_ht
->hash_fct(
4833 &session_id
, lttng_ht_seed
),
4834 the_consumer_data
.channels_by_session_id_ht
->match_fct
,
4835 &session_id
, &iter
.iter
, channel
,
4836 channels_by_session_id_ht_node
.node
) {
4837 ret
= lttng_consumer_channel_set_trace_chunk(channel
,
4841 * Roll-back the creation of this chunk.
4843 * This is important since the session daemon will
4844 * assume that the creation of this chunk failed and
4845 * will never ask for it to be closed, resulting
4846 * in a leak and an inconsistent state for some
4849 enum lttcomm_return_code close_ret
;
4850 char path
[LTTNG_PATH_MAX
];
4852 DBG("Failed to set new trace chunk on existing channels, rolling back");
4853 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4854 session_id
, chunk_id
,
4855 chunk_creation_timestamp
, NULL
,
4857 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4858 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4859 session_id
, chunk_id
);
4862 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4868 struct consumer_relayd_sock_pair
*relayd
;
4870 relayd
= consumer_find_relayd(*relayd_id
);
4872 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4873 ret
= relayd_create_trace_chunk(
4874 &relayd
->control_sock
, published_chunk
);
4875 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4877 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4880 if (!relayd
|| ret
) {
4881 enum lttcomm_return_code close_ret
;
4882 char path
[LTTNG_PATH_MAX
];
4884 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4887 chunk_creation_timestamp
,
4889 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4890 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4895 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4902 /* Release the reference returned by the "publish" operation. */
4903 lttng_trace_chunk_put(published_chunk
);
4904 lttng_trace_chunk_put(created_chunk
);
4908 enum lttcomm_return_code
lttng_consumer_close_trace_chunk(
4909 const uint64_t *relayd_id
, uint64_t session_id
,
4910 uint64_t chunk_id
, time_t chunk_close_timestamp
,
4911 const enum lttng_trace_chunk_command_type
*close_command
,
4914 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4915 struct lttng_trace_chunk
*chunk
;
4916 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4917 const char *relayd_id_str
= "(none)";
4918 const char *close_command_name
= "none";
4919 struct lttng_ht_iter iter
;
4920 struct lttng_consumer_channel
*channel
;
4921 enum lttng_trace_chunk_status chunk_status
;
4926 /* Only used for logging purposes. */
4927 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4928 "%" PRIu64
, *relayd_id
);
4929 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4930 relayd_id_str
= relayd_id_buffer
;
4932 relayd_id_str
= "(formatting error)";
4935 if (close_command
) {
4936 close_command_name
= lttng_trace_chunk_command_type_get_name(
4940 DBG("Consumer close trace chunk command: relayd_id = %s"
4941 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4942 ", close command = %s",
4943 relayd_id_str
, session_id
, chunk_id
,
4944 close_command_name
);
4946 chunk
= lttng_trace_chunk_registry_find_chunk(
4947 the_consumer_data
.chunk_registry
, session_id
, chunk_id
);
4949 ERR("Failed to find chunk: session_id = %" PRIu64
4950 ", chunk_id = %" PRIu64
,
4951 session_id
, chunk_id
);
4952 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4956 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
,
4957 chunk_close_timestamp
);
4958 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4959 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4963 if (close_command
) {
4964 chunk_status
= lttng_trace_chunk_set_close_command(
4965 chunk
, *close_command
);
4966 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4967 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4973 * chunk is now invalid to access as we no longer hold a reference to
4974 * it; it is only kept around to compare it (by address) to the
4975 * current chunk found in the session's channels.
4978 cds_lfht_for_each_entry(the_consumer_data
.channel_ht
->ht
, &iter
.iter
,
4979 channel
, node
.node
) {
4983 * Only change the channel's chunk to NULL if it still
4984 * references the chunk being closed. The channel may
4985 * reference a newer channel in the case of a session
4986 * rotation. When a session rotation occurs, the "next"
4987 * chunk is created before the "current" chunk is closed.
4989 if (channel
->trace_chunk
!= chunk
) {
4992 ret
= lttng_consumer_channel_set_trace_chunk(channel
, NULL
);
4995 * Attempt to close the chunk on as many channels as
4998 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5004 struct consumer_relayd_sock_pair
*relayd
;
5006 relayd
= consumer_find_relayd(*relayd_id
);
5008 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5009 ret
= relayd_close_trace_chunk(
5010 &relayd
->control_sock
, chunk
,
5012 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5014 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
,
5018 if (!relayd
|| ret
) {
5019 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5027 * Release the reference returned by the "find" operation and
5028 * the session daemon's implicit reference to the chunk.
5030 lttng_trace_chunk_put(chunk
);
5031 lttng_trace_chunk_put(chunk
);
5036 enum lttcomm_return_code
lttng_consumer_trace_chunk_exists(
5037 const uint64_t *relayd_id
, uint64_t session_id
,
5041 enum lttcomm_return_code ret_code
;
5042 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
5043 const char *relayd_id_str
= "(none)";
5044 const bool is_local_trace
= !relayd_id
;
5045 struct consumer_relayd_sock_pair
*relayd
= NULL
;
5046 bool chunk_exists_local
, chunk_exists_remote
;
5049 /* Only used for logging purposes. */
5050 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
5051 "%" PRIu64
, *relayd_id
);
5052 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
5053 relayd_id_str
= relayd_id_buffer
;
5055 relayd_id_str
= "(formatting error)";
5059 DBG("Consumer trace chunk exists command: relayd_id = %s"
5060 ", chunk_id = %" PRIu64
, relayd_id_str
,
5062 ret
= lttng_trace_chunk_registry_chunk_exists(
5063 the_consumer_data
.chunk_registry
, session_id
, chunk_id
,
5064 &chunk_exists_local
);
5066 /* Internal error. */
5067 ERR("Failed to query the existence of a trace chunk");
5068 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
5071 DBG("Trace chunk %s locally",
5072 chunk_exists_local
? "exists" : "does not exist");
5073 if (chunk_exists_local
) {
5074 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
5076 } else if (is_local_trace
) {
5077 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5082 relayd
= consumer_find_relayd(*relayd_id
);
5084 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
5085 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5086 goto end_rcu_unlock
;
5088 DBG("Looking up existence of trace chunk on relay daemon");
5089 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5090 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
,
5091 &chunk_exists_remote
);
5092 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5094 ERR("Failed to look-up the existence of trace chunk on relay daemon");
5095 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
5096 goto end_rcu_unlock
;
5099 ret_code
= chunk_exists_remote
?
5100 LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
5101 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5102 DBG("Trace chunk %s on relay daemon",
5103 chunk_exists_remote
? "exists" : "does not exist");
5112 int consumer_clear_monitored_channel(struct lttng_consumer_channel
*channel
)
5114 struct lttng_ht
*ht
;
5115 struct lttng_consumer_stream
*stream
;
5116 struct lttng_ht_iter iter
;
5119 ht
= the_consumer_data
.stream_per_chan_id_ht
;
5122 cds_lfht_for_each_entry_duplicate(ht
->ht
,
5123 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
5124 ht
->match_fct
, &channel
->key
,
5125 &iter
.iter
, stream
, node_channel_id
.node
) {
5127 * Protect against teardown with mutex.
5129 pthread_mutex_lock(&stream
->lock
);
5130 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5133 ret
= consumer_clear_stream(stream
);
5138 pthread_mutex_unlock(&stream
->lock
);
5141 return LTTCOMM_CONSUMERD_SUCCESS
;
5144 pthread_mutex_unlock(&stream
->lock
);
5149 int lttng_consumer_clear_channel(struct lttng_consumer_channel
*channel
)
5153 DBG("Consumer clear channel %" PRIu64
, channel
->key
);
5155 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
5157 * Nothing to do for the metadata channel/stream.
5158 * Snapshot mechanism already take care of the metadata
5159 * handling/generation, and monitored channels only need to
5160 * have their data stream cleared..
5162 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5166 if (!channel
->monitor
) {
5167 ret
= consumer_clear_unmonitored_channel(channel
);
5169 ret
= consumer_clear_monitored_channel(channel
);
5175 enum lttcomm_return_code
lttng_consumer_open_channel_packets(
5176 struct lttng_consumer_channel
*channel
)
5178 struct lttng_consumer_stream
*stream
;
5179 enum lttcomm_return_code ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5181 if (channel
->metadata_stream
) {
5182 ERR("Open channel packets command attempted on a metadata channel");
5183 ret
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5188 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
5189 enum consumer_stream_open_packet_status status
;
5191 pthread_mutex_lock(&stream
->lock
);
5192 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5196 status
= consumer_stream_open_packet(stream
);
5198 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
5199 DBG("Opened a packet in \"open channel packets\" command: stream id = %" PRIu64
5200 ", channel name = %s, session id = %" PRIu64
,
5201 stream
->key
, stream
->chan
->name
,
5202 stream
->chan
->session_id
);
5203 stream
->opened_packet_in_current_trace_chunk
= true;
5205 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
5206 DBG("No space left to open a packet in \"open channel packets\" command: stream id = %" PRIu64
5207 ", channel name = %s, session id = %" PRIu64
,
5208 stream
->key
, stream
->chan
->name
,
5209 stream
->chan
->session_id
);
5211 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
5213 * Only unexpected internal errors can lead to this
5214 * failing. Report an unknown error.
5216 ERR("Failed to flush empty buffer in \"open channel packets\" command: stream id = %" PRIu64
5217 ", channel id = %" PRIu64
5218 ", channel name = %s"
5219 ", session id = %" PRIu64
,
5220 stream
->key
, channel
->key
,
5221 channel
->name
, channel
->session_id
);
5222 ret
= LTTCOMM_CONSUMERD_UNKNOWN_ERROR
;
5229 pthread_mutex_unlock(&stream
->lock
);
5238 pthread_mutex_unlock(&stream
->lock
);
5239 goto end_rcu_unlock
;
5242 void lttng_consumer_sigbus_handle(void *addr
)
5244 lttng_ustconsumer_sigbus_handle(addr
);