2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
11 #include <common/align.hpp>
12 #include <common/common.hpp>
13 #include <common/compat/endian.hpp>
14 #include <common/compat/poll.hpp>
15 #include <common/consumer/consumer-metadata-cache.hpp>
16 #include <common/consumer/consumer-stream.hpp>
17 #include <common/consumer/consumer-testpoint.hpp>
18 #include <common/consumer/consumer-timer.hpp>
19 #include <common/consumer/consumer.hpp>
20 #include <common/dynamic-array.hpp>
21 #include <common/index/ctf-index.hpp>
22 #include <common/index/index.hpp>
23 #include <common/kernel-consumer/kernel-consumer.hpp>
24 #include <common/kernel-ctl/kernel-ctl.hpp>
25 #include <common/relayd/relayd.hpp>
26 #include <common/sessiond-comm/relayd.hpp>
27 #include <common/sessiond-comm/sessiond-comm.hpp>
28 #include <common/string-utils/format.hpp>
29 #include <common/time.hpp>
30 #include <common/trace-chunk-registry.hpp>
31 #include <common/trace-chunk.hpp>
32 #include <common/ust-consumer/ust-consumer.hpp>
33 #include <common/utils.hpp>
35 #include <bin/lttng-consumerd/health-consumerd.hpp>
43 #include <sys/socket.h>
44 #include <sys/types.h>
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()
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
= nullptr;
107 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
)); /* NOLINT sizeof used on a
111 static void notify_health_quit_pipe(int *pipe
)
115 ret
= lttng_write(pipe
[1], "4", 1);
117 PERROR("write consumer health quit");
121 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
122 struct lttng_consumer_channel
*chan
,
124 enum consumer_channel_action action
)
126 struct consumer_channel_msg msg
;
129 memset(&msg
, 0, sizeof(msg
));
134 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
135 if (ret
< sizeof(msg
)) {
136 PERROR("notify_channel_pipe write error");
140 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
142 notify_channel_pipe(ctx
, nullptr, 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
, send_node
) {
178 * Once a stream is added to this list, the buffers were created so we
179 * have a guarantee that this call will succeed. Setting the monitor
180 * mode to 0 so we don't lock nor try to delete the stream from the
184 consumer_stream_destroy(stream
, nullptr);
189 * Find a stream. The consumer_data.lock must be locked during this
192 static struct lttng_consumer_stream
*find_stream(uint64_t key
, struct lttng_ht
*ht
)
194 struct lttng_ht_iter iter
;
195 struct lttng_ht_node_u64
*node
;
196 struct lttng_consumer_stream
*stream
= nullptr;
200 /* -1ULL keys are lookup failures */
201 if (key
== (uint64_t) -1ULL) {
207 lttng_ht_lookup(ht
, &key
, &iter
);
208 node
= lttng_ht_iter_get_node_u64(&iter
);
209 if (node
!= nullptr) {
210 stream
= lttng::utils::container_of(node
, <tng_consumer_stream::node
);
218 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
220 struct lttng_consumer_stream
*stream
;
223 stream
= find_stream(key
, ht
);
225 stream
->key
= (uint64_t) -1ULL;
227 * We don't want the lookup to match, but we still need
228 * to iterate on this stream when iterating over the hash table. Just
229 * change the node key.
231 stream
->node
.key
= (uint64_t) -1ULL;
237 * Return a channel object for the given key.
239 * RCU read side lock MUST be acquired before calling this function and
240 * protects the channel ptr.
242 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
244 struct lttng_ht_iter iter
;
245 struct lttng_ht_node_u64
*node
;
246 struct lttng_consumer_channel
*channel
= nullptr;
248 ASSERT_RCU_READ_LOCKED();
250 /* -1ULL keys are lookup failures */
251 if (key
== (uint64_t) -1ULL) {
255 lttng_ht_lookup(the_consumer_data
.channel_ht
, &key
, &iter
);
256 node
= lttng_ht_iter_get_node_u64(&iter
);
257 if (node
!= nullptr) {
258 channel
= lttng::utils::container_of(node
, <tng_consumer_channel::node
);
265 * There is a possibility that the consumer does not have enough time between
266 * the close of the channel on the session daemon and the cleanup in here thus
267 * once we have a channel add with an existing key, we know for sure that this
268 * channel will eventually get cleaned up by all streams being closed.
270 * This function just nullifies the already existing channel key.
272 static void steal_channel_key(uint64_t key
)
274 struct lttng_consumer_channel
*channel
;
277 channel
= consumer_find_channel(key
);
279 channel
->key
= (uint64_t) -1ULL;
281 * We don't want the lookup to match, but we still need to iterate on
282 * this channel when iterating over the hash table. Just change the
285 channel
->node
.key
= (uint64_t) -1ULL;
290 static void free_channel_rcu(struct rcu_head
*head
)
292 struct lttng_ht_node_u64
*node
= lttng::utils::container_of(head
, <tng_ht_node_u64::head
);
293 struct lttng_consumer_channel
*channel
=
294 lttng::utils::container_of(node
, <tng_consumer_channel::node
);
296 switch (the_consumer_data
.type
) {
297 case LTTNG_CONSUMER_KERNEL
:
299 case LTTNG_CONSUMER32_UST
:
300 case LTTNG_CONSUMER64_UST
:
301 lttng_ustconsumer_free_channel(channel
);
304 ERR("Unknown consumer_data type");
311 * RCU protected relayd socket pair free.
313 static void free_relayd_rcu(struct rcu_head
*head
)
315 struct lttng_ht_node_u64
*node
= lttng::utils::container_of(head
, <tng_ht_node_u64::head
);
316 struct consumer_relayd_sock_pair
*relayd
=
317 lttng::utils::container_of(node
, &consumer_relayd_sock_pair::node
);
320 * Close all sockets. This is done in the call RCU since we don't want the
321 * socket fds to be reassigned thus potentially creating bad state of the
324 * We do not have to lock the control socket mutex here since at this stage
325 * there is no one referencing to this relayd object.
327 (void) relayd_close(&relayd
->control_sock
);
328 (void) relayd_close(&relayd
->data_sock
);
330 pthread_mutex_destroy(&relayd
->ctrl_sock_mutex
);
335 * Destroy and free relayd socket pair object.
337 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
340 struct lttng_ht_iter iter
;
342 if (relayd
== nullptr) {
346 DBG("Consumer destroy and close relayd socket pair");
348 iter
.iter
.node
= &relayd
->node
.node
;
349 ret
= lttng_ht_del(the_consumer_data
.relayd_ht
, &iter
);
351 /* We assume the relayd is being or is destroyed */
355 /* RCU free() call */
356 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
360 * Remove a channel from the global list protected by a mutex. This function is
361 * also responsible for freeing its data structures.
363 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
365 struct lttng_ht_iter iter
;
367 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
369 pthread_mutex_lock(&the_consumer_data
.lock
);
370 pthread_mutex_lock(&channel
->lock
);
372 /* Destroy streams that might have been left in the stream list. */
373 clean_channel_stream_list(channel
);
375 if (channel
->live_timer_enabled
== 1) {
376 consumer_timer_live_stop(channel
);
378 if (channel
->monitor_timer_enabled
== 1) {
379 consumer_timer_monitor_stop(channel
);
383 * Send a last buffer statistics sample to the session daemon
384 * to ensure it tracks the amount of data consumed by this channel.
386 sample_and_send_channel_buffer_stats(channel
);
388 switch (the_consumer_data
.type
) {
389 case LTTNG_CONSUMER_KERNEL
:
391 case LTTNG_CONSUMER32_UST
:
392 case LTTNG_CONSUMER64_UST
:
393 lttng_ustconsumer_del_channel(channel
);
396 ERR("Unknown consumer_data type");
401 lttng_trace_chunk_put(channel
->trace_chunk
);
402 channel
->trace_chunk
= nullptr;
404 if (channel
->is_published
) {
408 iter
.iter
.node
= &channel
->node
.node
;
409 ret
= lttng_ht_del(the_consumer_data
.channel_ht
, &iter
);
412 iter
.iter
.node
= &channel
->channels_by_session_id_ht_node
.node
;
413 ret
= lttng_ht_del(the_consumer_data
.channels_by_session_id_ht
, &iter
);
418 channel
->is_deleted
= true;
419 call_rcu(&channel
->node
.head
, free_channel_rcu
);
421 pthread_mutex_unlock(&channel
->lock
);
422 pthread_mutex_unlock(&the_consumer_data
.lock
);
426 * Iterate over the relayd hash table and destroy each element. Finally,
427 * destroy the whole hash table.
429 static void cleanup_relayd_ht()
431 struct lttng_ht_iter iter
;
432 struct consumer_relayd_sock_pair
*relayd
;
436 cds_lfht_for_each_entry (the_consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
, node
.node
) {
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
, struct lttng_ht
*ht
)
546 consumer_stream_destroy(stream
, ht
);
550 * XXX naming of del vs destroy is all mixed up.
552 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
554 consumer_stream_destroy(stream
, data_ht
);
557 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
559 consumer_stream_destroy(stream
, metadata_ht
);
562 void consumer_stream_update_channel_attributes(struct lttng_consumer_stream
*stream
,
563 struct lttng_consumer_channel
*channel
)
565 stream
->channel_read_only_attributes
.tracefile_size
= channel
->tracefile_size
;
569 * Add a stream to the global list protected by a mutex.
571 void consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
573 struct lttng_ht
*ht
= data_ht
;
575 LTTNG_ASSERT(stream
);
578 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
580 pthread_mutex_lock(&the_consumer_data
.lock
);
581 pthread_mutex_lock(&stream
->chan
->lock
);
582 pthread_mutex_lock(&stream
->chan
->timer_lock
);
583 pthread_mutex_lock(&stream
->lock
);
586 /* Steal stream identifier to avoid having streams with the same key */
587 steal_stream_key(stream
->key
, ht
);
589 lttng_ht_add_unique_u64(ht
, &stream
->node
);
591 lttng_ht_add_u64(the_consumer_data
.stream_per_chan_id_ht
, &stream
->node_channel_id
);
594 * Add stream to the stream_list_ht of the consumer data. No need to steal
595 * the key since the HT does not use it and we allow to add redundant keys
598 lttng_ht_add_u64(the_consumer_data
.stream_list_ht
, &stream
->node_session_id
);
601 * When nb_init_stream_left reaches 0, we don't need to trigger any action
602 * in terms of destroying the associated channel, because the action that
603 * causes the count to become 0 also causes a stream to be added. The
604 * channel deletion will thus be triggered by the following removal of this
607 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
608 /* Increment refcount before decrementing nb_init_stream_left */
610 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
613 /* Update consumer data once the node is inserted. */
614 the_consumer_data
.stream_count
++;
615 the_consumer_data
.need_update
= 1;
618 pthread_mutex_unlock(&stream
->lock
);
619 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
620 pthread_mutex_unlock(&stream
->chan
->lock
);
621 pthread_mutex_unlock(&the_consumer_data
.lock
);
625 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
626 * be acquired before calling this.
628 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
631 struct lttng_ht_node_u64
*node
;
632 struct lttng_ht_iter iter
;
634 LTTNG_ASSERT(relayd
);
635 ASSERT_RCU_READ_LOCKED();
637 lttng_ht_lookup(the_consumer_data
.relayd_ht
, &relayd
->net_seq_idx
, &iter
);
638 node
= lttng_ht_iter_get_node_u64(&iter
);
639 if (node
!= nullptr) {
642 lttng_ht_add_unique_u64(the_consumer_data
.relayd_ht
, &relayd
->node
);
649 * Allocate and return a consumer relayd socket.
651 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(uint64_t net_seq_idx
)
653 struct consumer_relayd_sock_pair
*obj
= nullptr;
655 /* net sequence index of -1 is a failure */
656 if (net_seq_idx
== (uint64_t) -1ULL) {
660 obj
= zmalloc
<consumer_relayd_sock_pair
>();
661 if (obj
== nullptr) {
662 PERROR("zmalloc relayd sock");
666 obj
->net_seq_idx
= net_seq_idx
;
668 obj
->destroy_flag
= 0;
669 obj
->control_sock
.sock
.fd
= -1;
670 obj
->data_sock
.sock
.fd
= -1;
671 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
672 pthread_mutex_init(&obj
->ctrl_sock_mutex
, nullptr);
679 * Find a relayd socket pair in the global consumer data.
681 * Return the object if found else NULL.
682 * RCU read-side lock must be held across this call and while using the
685 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
687 struct lttng_ht_iter iter
;
688 struct lttng_ht_node_u64
*node
;
689 struct consumer_relayd_sock_pair
*relayd
= nullptr;
691 ASSERT_RCU_READ_LOCKED();
693 /* Negative keys are lookup failures */
694 if (key
== (uint64_t) -1ULL) {
698 lttng_ht_lookup(the_consumer_data
.relayd_ht
, &key
, &iter
);
699 node
= lttng_ht_iter_get_node_u64(&iter
);
700 if (node
!= nullptr) {
701 relayd
= lttng::utils::container_of(node
, &consumer_relayd_sock_pair::node
);
709 * Find a relayd and send the stream
711 * Returns 0 on success, < 0 on error
713 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
, char *path
)
716 struct consumer_relayd_sock_pair
*relayd
;
718 LTTNG_ASSERT(stream
);
719 LTTNG_ASSERT(stream
->net_seq_idx
!= -1ULL);
722 /* The stream is not metadata. Get relayd reference if exists. */
724 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
725 if (relayd
!= nullptr) {
726 /* Add stream on the relayd */
727 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
728 ret
= relayd_add_stream(&relayd
->control_sock
,
730 get_consumer_domain(),
732 &stream
->relayd_stream_id
,
733 stream
->chan
->tracefile_size
,
734 stream
->chan
->tracefile_count
,
735 stream
->trace_chunk
);
736 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
738 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".",
739 relayd
->net_seq_idx
);
740 lttng_consumer_cleanup_relayd(relayd
);
744 uatomic_inc(&relayd
->refcount
);
745 stream
->sent_to_relayd
= 1;
747 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
749 stream
->net_seq_idx
);
754 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
757 stream
->net_seq_idx
);
765 * Find a relayd and send the streams sent message
767 * Returns 0 on success, < 0 on error
769 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
772 struct consumer_relayd_sock_pair
*relayd
;
774 LTTNG_ASSERT(net_seq_idx
!= -1ULL);
776 /* The stream is not metadata. Get relayd reference if exists. */
778 relayd
= consumer_find_relayd(net_seq_idx
);
779 if (relayd
!= nullptr) {
780 /* Add stream on the relayd */
781 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
782 ret
= relayd_streams_sent(&relayd
->control_sock
);
783 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
785 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64
".",
786 relayd
->net_seq_idx
);
787 lttng_consumer_cleanup_relayd(relayd
);
791 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.", net_seq_idx
);
797 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
805 * Find a relayd and close the stream
807 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
809 struct consumer_relayd_sock_pair
*relayd
;
811 /* The stream is not metadata. Get relayd reference if exists. */
813 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
815 consumer_stream_relayd_close(stream
, relayd
);
821 * Handle stream for relayd transmission if the stream applies for network
822 * streaming where the net sequence index is set.
824 * Return destination file descriptor or negative value on error.
826 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
828 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
, sizeof(data_hdr
));
871 ++stream
->next_net_seq_num
;
873 /* Set to go on data socket */
874 outfd
= relayd
->data_sock
.sock
.fd
;
882 * Write a character on the metadata poll pipe to wake the metadata thread.
883 * Returns 0 on success, -1 on error.
885 int consumer_metadata_wakeup_pipe(const struct lttng_consumer_channel
*channel
)
889 DBG("Waking up metadata poll thread (writing to pipe): channel name = '%s'", channel
->name
);
890 if (channel
->monitor
&& channel
->metadata_stream
) {
891 const char dummy
= 'c';
892 const ssize_t write_ret
=
893 lttng_write(channel
->metadata_stream
->ust_metadata_poll_pipe
[1], &dummy
, 1);
896 if (errno
== EWOULDBLOCK
) {
898 * This is fine, the metadata poll thread
899 * is having a hard time keeping-up, but
900 * it will eventually wake-up and consume
901 * the available data.
905 PERROR("Failed to write to UST metadata pipe while attempting to wake-up the metadata poll thread");
917 * Trigger a dump of the metadata content. Following/during the succesful
918 * completion of this call, the metadata poll thread will start receiving
919 * metadata packets to consume.
921 * The caller must hold the channel and stream locks.
923 static int consumer_metadata_stream_dump(struct lttng_consumer_stream
*stream
)
927 ASSERT_LOCKED(stream
->chan
->lock
);
928 ASSERT_LOCKED(stream
->lock
);
929 LTTNG_ASSERT(stream
->metadata_flag
);
930 LTTNG_ASSERT(stream
->chan
->trace_chunk
);
932 switch (the_consumer_data
.type
) {
933 case LTTNG_CONSUMER_KERNEL
:
935 * Reset the position of what has been read from the
936 * metadata cache to 0 so we can dump it again.
938 ret
= kernctl_metadata_cache_dump(stream
->wait_fd
);
940 case LTTNG_CONSUMER32_UST
:
941 case LTTNG_CONSUMER64_UST
:
943 * Reset the position pushed from the metadata cache so it
944 * will write from the beginning on the next push.
946 stream
->ust_metadata_pushed
= 0;
947 ret
= consumer_metadata_wakeup_pipe(stream
->chan
);
950 ERR("Unknown consumer_data type");
954 ERR("Failed to dump the metadata cache");
959 static int lttng_consumer_channel_set_trace_chunk(struct lttng_consumer_channel
*channel
,
960 struct lttng_trace_chunk
*new_trace_chunk
)
962 pthread_mutex_lock(&channel
->lock
);
963 if (channel
->is_deleted
) {
965 * The channel has been logically deleted and should no longer
966 * be used. It has released its reference to its current trace
967 * chunk and should not acquire a new one.
969 * Return success as there is nothing for the caller to do.
975 * The acquisition of the reference cannot fail (barring
976 * a severe internal error) since a reference to the published
977 * chunk is already held by the caller.
979 if (new_trace_chunk
) {
980 const bool acquired_reference
= lttng_trace_chunk_get(new_trace_chunk
);
982 LTTNG_ASSERT(acquired_reference
);
985 lttng_trace_chunk_put(channel
->trace_chunk
);
986 channel
->trace_chunk
= new_trace_chunk
;
988 pthread_mutex_unlock(&channel
->lock
);
993 * Allocate and return a new lttng_consumer_channel object using the given key
994 * to initialize the hash table node.
996 * On error, return NULL.
998 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
1000 const uint64_t *chunk_id
,
1001 const char *pathname
,
1004 enum lttng_event_output output
,
1005 uint64_t tracefile_size
,
1006 uint64_t tracefile_count
,
1007 uint64_t session_id_per_pid
,
1008 unsigned int monitor
,
1009 unsigned int live_timer_interval
,
1010 bool is_in_live_session
,
1011 const char *root_shm_path
,
1012 const char *shm_path
)
1014 struct lttng_consumer_channel
*channel
= nullptr;
1015 struct lttng_trace_chunk
*trace_chunk
= nullptr;
1018 trace_chunk
= lttng_trace_chunk_registry_find_chunk(
1019 the_consumer_data
.chunk_registry
, session_id
, *chunk_id
);
1021 ERR("Failed to find trace chunk reference during creation of channel");
1026 channel
= zmalloc
<lttng_consumer_channel
>();
1027 if (channel
== nullptr) {
1028 PERROR("malloc struct lttng_consumer_channel");
1033 channel
->refcount
= 0;
1034 channel
->session_id
= session_id
;
1035 channel
->session_id_per_pid
= session_id_per_pid
;
1036 channel
->relayd_id
= relayd_id
;
1037 channel
->tracefile_size
= tracefile_size
;
1038 channel
->tracefile_count
= tracefile_count
;
1039 channel
->monitor
= monitor
;
1040 channel
->live_timer_interval
= live_timer_interval
;
1041 channel
->is_live
= is_in_live_session
;
1042 pthread_mutex_init(&channel
->lock
, nullptr);
1043 pthread_mutex_init(&channel
->timer_lock
, nullptr);
1046 case LTTNG_EVENT_SPLICE
:
1047 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
1049 case LTTNG_EVENT_MMAP
:
1050 channel
->output
= CONSUMER_CHANNEL_MMAP
;
1060 * In monitor mode, the streams associated with the channel will be put in
1061 * a special list ONLY owned by this channel. So, the refcount is set to 1
1062 * here meaning that the channel itself has streams that are referenced.
1064 * On a channel deletion, once the channel is no longer visible, the
1065 * refcount is decremented and checked for a zero value to delete it. With
1066 * streams in no monitor mode, it will now be safe to destroy the channel.
1068 if (!channel
->monitor
) {
1069 channel
->refcount
= 1;
1072 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1073 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1075 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1076 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1078 if (root_shm_path
) {
1079 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1080 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1083 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1084 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1087 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1088 lttng_ht_node_init_u64(&channel
->channels_by_session_id_ht_node
, channel
->session_id
);
1090 channel
->wait_fd
= -1;
1091 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1094 int ret
= lttng_consumer_channel_set_trace_chunk(channel
, trace_chunk
);
1100 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1103 lttng_trace_chunk_put(trace_chunk
);
1106 consumer_del_channel(channel
);
1112 * Add a channel to the global list protected by a mutex.
1114 * Always return 0 indicating success.
1116 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1117 struct lttng_consumer_local_data
*ctx
)
1119 pthread_mutex_lock(&the_consumer_data
.lock
);
1120 pthread_mutex_lock(&channel
->lock
);
1121 pthread_mutex_lock(&channel
->timer_lock
);
1124 * This gives us a guarantee that the channel we are about to add to the
1125 * channel hash table will be unique. See this function comment on the why
1126 * we need to steel the channel key at this stage.
1128 steal_channel_key(channel
->key
);
1131 lttng_ht_add_unique_u64(the_consumer_data
.channel_ht
, &channel
->node
);
1132 lttng_ht_add_u64(the_consumer_data
.channels_by_session_id_ht
,
1133 &channel
->channels_by_session_id_ht_node
);
1135 channel
->is_published
= true;
1137 pthread_mutex_unlock(&channel
->timer_lock
);
1138 pthread_mutex_unlock(&channel
->lock
);
1139 pthread_mutex_unlock(&the_consumer_data
.lock
);
1141 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1142 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1149 * Allocate the pollfd structure and the local view of the out fds to avoid
1150 * doing a lookup in the linked list and concurrency issues when writing is
1151 * needed. Called with consumer_data.lock held.
1153 * Returns the number of fds in the structures.
1155 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1156 struct pollfd
**pollfd
,
1157 struct lttng_consumer_stream
**local_stream
,
1158 struct lttng_ht
*ht
,
1159 int *nb_inactive_fd
)
1162 struct lttng_ht_iter iter
;
1163 struct lttng_consumer_stream
*stream
;
1167 LTTNG_ASSERT(pollfd
);
1168 LTTNG_ASSERT(local_stream
);
1170 DBG("Updating poll fd array");
1171 *nb_inactive_fd
= 0;
1173 cds_lfht_for_each_entry (ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1175 * Only active streams with an active end point can be added to the
1176 * poll set and local stream storage of the thread.
1178 * There is a potential race here for endpoint_status to be updated
1179 * just after the check. However, this is OK since the stream(s) will
1180 * be deleted once the thread is notified that the end point state has
1181 * changed where this function will be called back again.
1183 * We track the number of inactive FDs because they still need to be
1184 * closed by the polling thread after a wakeup on the data_pipe or
1187 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1188 (*nb_inactive_fd
)++;
1192 * This clobbers way too much the debug output. Uncomment that if you
1193 * need it for debugging purposes.
1195 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1196 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1197 local_stream
[i
] = stream
;
1203 * Insert the consumer_data_pipe at the end of the array and don't
1204 * increment i so nb_fd is the number of real FD.
1206 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1207 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1209 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1210 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1215 * Poll on the should_quit pipe and the command socket return -1 on
1216 * error, 1 if should exit, 0 if data is available on the command socket
1218 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1223 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1224 if (num_rdy
== -1) {
1226 * Restart interrupted system call.
1228 if (errno
== EINTR
) {
1231 PERROR("Poll error");
1234 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1235 DBG("consumer_should_quit wake up");
1242 * Set the error socket.
1244 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
, int sock
)
1246 ctx
->consumer_error_socket
= sock
;
1250 * Set the command socket path.
1252 void lttng_consumer_set_command_sock_path(struct lttng_consumer_local_data
*ctx
, char *sock
)
1254 ctx
->consumer_command_sock_path
= sock
;
1258 * Send return code to the session daemon.
1259 * If the socket is not defined, we return 0, it is not a fatal error
1261 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1263 if (ctx
->consumer_error_socket
> 0) {
1264 return lttcomm_send_unix_sock(
1265 ctx
->consumer_error_socket
, &cmd
, sizeof(enum lttcomm_sessiond_command
));
1272 * Close all the tracefiles and stream fds and MUST be called when all
1273 * instances are destroyed i.e. when all threads were joined and are ended.
1275 void lttng_consumer_cleanup()
1277 struct lttng_ht_iter iter
;
1278 struct lttng_consumer_channel
*channel
;
1279 unsigned int trace_chunks_left
;
1283 cds_lfht_for_each_entry (the_consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
, node
.node
) {
1284 consumer_del_channel(channel
);
1289 lttng_ht_destroy(the_consumer_data
.channel_ht
);
1290 lttng_ht_destroy(the_consumer_data
.channels_by_session_id_ht
);
1292 cleanup_relayd_ht();
1294 lttng_ht_destroy(the_consumer_data
.stream_per_chan_id_ht
);
1297 * This HT contains streams that are freed by either the metadata thread or
1298 * the data thread so we do *nothing* on the hash table and simply destroy
1301 lttng_ht_destroy(the_consumer_data
.stream_list_ht
);
1304 * Trace chunks in the registry may still exist if the session
1305 * daemon has encountered an internal error and could not
1306 * tear down its sessions and/or trace chunks properly.
1308 * Release the session daemon's implicit reference to any remaining
1309 * trace chunk and print an error if any trace chunk was found. Note
1310 * that there are _no_ legitimate cases for trace chunks to be left,
1311 * it is a leak. However, it can happen following a crash of the
1312 * session daemon and not emptying the registry would cause an assertion
1316 lttng_trace_chunk_registry_put_each_chunk(the_consumer_data
.chunk_registry
);
1317 if (trace_chunks_left
) {
1318 ERR("%u trace chunks are leaked by lttng-consumerd. "
1319 "This can be caused by an internal error of the session daemon.",
1322 /* Run all callbacks freeing each chunk. */
1324 lttng_trace_chunk_registry_destroy(the_consumer_data
.chunk_registry
);
1328 * Called from signal handler.
1330 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1334 CMM_STORE_SHARED(consumer_quit
, 1);
1335 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1337 PERROR("write consumer quit");
1340 DBG("Consumer flag that it should quit");
1344 * Flush pending writes to trace output disk file.
1346 static void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
, off_t orig_offset
)
1349 int outfd
= stream
->out_fd
;
1352 * This does a blocking write-and-wait on any page that belongs to the
1353 * subbuffer prior to the one we just wrote.
1354 * Don't care about error values, as these are just hints and ways to
1355 * limit the amount of page cache used.
1357 if (orig_offset
< stream
->max_sb_size
) {
1360 lttng_sync_file_range(outfd
,
1361 orig_offset
- stream
->max_sb_size
,
1362 stream
->max_sb_size
,
1363 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
|
1364 SYNC_FILE_RANGE_WAIT_AFTER
);
1366 * Give hints to the kernel about how we access the file:
1367 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1370 * We need to call fadvise again after the file grows because the
1371 * kernel does not seem to apply fadvise to non-existing parts of the
1374 * Call fadvise _after_ having waited for the page writeback to
1375 * complete because the dirty page writeback semantic is not well
1376 * defined. So it can be expected to lead to lower throughput in
1379 ret
= posix_fadvise(
1380 outfd
, orig_offset
- stream
->max_sb_size
, stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1381 if (ret
&& ret
!= -ENOSYS
) {
1383 PERROR("posix_fadvise on fd %i", outfd
);
1388 * Initialise the necessary environnement :
1389 * - create a new context
1390 * - create the poll_pipe
1391 * - create the should_quit pipe (for signal handler)
1392 * - create the thread pipe (for splice)
1394 * Takes a function pointer as argument, this function is called when data is
1395 * available on a buffer. This function is responsible to do the
1396 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1397 * buffer configuration and then kernctl_put_next_subbuf at the end.
1399 * Returns a pointer to the new context or NULL on error.
1401 struct lttng_consumer_local_data
*
1402 lttng_consumer_create(enum lttng_consumer_type type
,
1403 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1404 struct lttng_consumer_local_data
*ctx
,
1405 bool locked_by_caller
),
1406 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1407 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1408 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1411 struct lttng_consumer_local_data
*ctx
;
1413 LTTNG_ASSERT(the_consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1414 the_consumer_data
.type
== type
);
1415 the_consumer_data
.type
= type
;
1417 ctx
= zmalloc
<lttng_consumer_local_data
>();
1418 if (ctx
== nullptr) {
1419 PERROR("allocating context");
1423 ctx
->consumer_error_socket
= -1;
1424 ctx
->consumer_metadata_socket
= -1;
1425 pthread_mutex_init(&ctx
->metadata_socket_lock
, nullptr);
1426 /* assign the callbacks */
1427 ctx
->on_buffer_ready
= buffer_ready
;
1428 ctx
->on_recv_channel
= recv_channel
;
1429 ctx
->on_recv_stream
= recv_stream
;
1430 ctx
->on_update_stream
= update_stream
;
1432 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1433 if (!ctx
->consumer_data_pipe
) {
1434 goto error_poll_pipe
;
1437 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1438 if (!ctx
->consumer_wakeup_pipe
) {
1439 goto error_wakeup_pipe
;
1442 ret
= pipe(ctx
->consumer_should_quit
);
1444 PERROR("Error creating recv pipe");
1445 goto error_quit_pipe
;
1448 ret
= pipe(ctx
->consumer_channel_pipe
);
1450 PERROR("Error creating channel pipe");
1451 goto error_channel_pipe
;
1454 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1455 if (!ctx
->consumer_metadata_pipe
) {
1456 goto error_metadata_pipe
;
1459 ctx
->channel_monitor_pipe
= -1;
1463 error_metadata_pipe
:
1464 utils_close_pipe(ctx
->consumer_channel_pipe
);
1466 utils_close_pipe(ctx
->consumer_should_quit
);
1468 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1470 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1478 * Iterate over all streams of the hashtable and free them properly.
1480 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1482 struct lttng_ht_iter iter
;
1483 struct lttng_consumer_stream
*stream
;
1485 if (ht
== nullptr) {
1490 cds_lfht_for_each_entry (ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1492 * Ignore return value since we are currently cleaning up so any error
1495 (void) consumer_del_stream(stream
, ht
);
1499 lttng_ht_destroy(ht
);
1503 * Iterate over all streams of the metadata hashtable and free them
1506 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1508 struct lttng_ht_iter iter
;
1509 struct lttng_consumer_stream
*stream
;
1511 if (ht
== nullptr) {
1516 cds_lfht_for_each_entry (ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1518 * Ignore return value since we are currently cleaning up so any error
1521 (void) consumer_del_metadata_stream(stream
, ht
);
1525 lttng_ht_destroy(ht
);
1529 * Close all fds associated with the instance and free the context.
1531 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1535 DBG("Consumer destroying it. Closing everything.");
1541 destroy_data_stream_ht(data_ht
);
1542 destroy_metadata_stream_ht(metadata_ht
);
1544 ret
= close(ctx
->consumer_error_socket
);
1548 ret
= close(ctx
->consumer_metadata_socket
);
1552 utils_close_pipe(ctx
->consumer_channel_pipe
);
1553 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1554 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1555 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1556 utils_close_pipe(ctx
->consumer_should_quit
);
1558 unlink(ctx
->consumer_command_sock_path
);
1563 * Write the metadata stream id on the specified file descriptor.
1566 write_relayd_metadata_id(int fd
, struct lttng_consumer_stream
*stream
, unsigned long padding
)
1569 struct lttcomm_relayd_metadata_payload hdr
;
1571 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1572 hdr
.padding_size
= htobe32(padding
);
1573 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1574 if (ret
< sizeof(hdr
)) {
1576 * This error means that the fd's end is closed so ignore the PERROR
1577 * not to clubber the error output since this can happen in a normal
1580 if (errno
!= EPIPE
) {
1581 PERROR("write metadata stream id");
1583 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1585 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1586 * handle writting the missing part so report that as an error and
1587 * don't lie to the caller.
1592 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1593 stream
->relayd_stream_id
,
1601 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1602 * core function for writing trace buffers to either the local filesystem or
1605 * It must be called with the stream and the channel lock held.
1607 * Careful review MUST be put if any changes occur!
1609 * Returns the number of bytes written
1611 ssize_t
lttng_consumer_on_read_subbuffer_mmap(struct lttng_consumer_stream
*stream
,
1612 const struct lttng_buffer_view
*buffer
,
1613 unsigned long padding
)
1616 off_t orig_offset
= stream
->out_fd_offset
;
1617 /* Default is on the disk */
1618 int outfd
= stream
->out_fd
;
1619 struct consumer_relayd_sock_pair
*relayd
= nullptr;
1620 unsigned int relayd_hang_up
= 0;
1621 const size_t subbuf_content_size
= buffer
->size
- padding
;
1624 /* RCU lock for the relayd pointer */
1626 LTTNG_ASSERT(stream
->net_seq_idx
!= (uint64_t) -1ULL || stream
->trace_chunk
);
1628 /* Flag that the current stream if set for network streaming. */
1629 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1630 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1631 if (relayd
== nullptr) {
1637 /* Handle stream on the relayd if the output is on the network */
1639 unsigned long netlen
= subbuf_content_size
;
1642 * Lock the control socket for the complete duration of the function
1643 * since from this point on we will use the socket.
1645 if (stream
->metadata_flag
) {
1646 /* Metadata requires the control socket. */
1647 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1648 if (stream
->reset_metadata_flag
) {
1649 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1650 stream
->relayd_stream_id
,
1651 stream
->metadata_version
);
1656 stream
->reset_metadata_flag
= 0;
1658 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1661 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1666 /* Use the returned socket. */
1669 /* Write metadata stream id before payload */
1670 if (stream
->metadata_flag
) {
1671 ret
= write_relayd_metadata_id(outfd
, stream
, padding
);
1678 write_len
= subbuf_content_size
;
1680 /* No streaming; we have to write the full padding. */
1681 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1682 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1684 ERR("Reset metadata file");
1687 stream
->reset_metadata_flag
= 0;
1691 * Check if we need to change the tracefile before writing the packet.
1693 if (stream
->chan
->tracefile_size
> 0 &&
1694 (stream
->tracefile_size_current
+ buffer
->size
) >
1695 stream
->chan
->tracefile_size
) {
1696 ret
= consumer_stream_rotate_output_files(stream
);
1700 outfd
= stream
->out_fd
;
1703 stream
->tracefile_size_current
+= buffer
->size
;
1704 write_len
= buffer
->size
;
1708 * This call guarantee that len or less is returned. It's impossible to
1709 * receive a ret value that is bigger than len.
1711 ret
= lttng_write(outfd
, buffer
->data
, write_len
);
1712 DBG("Consumer mmap write() ret %zd (len %zu)", ret
, write_len
);
1713 if (ret
< 0 || ((size_t) ret
!= write_len
)) {
1715 * Report error to caller if nothing was written else at least send the
1723 /* Socket operation failed. We consider the relayd dead */
1724 if (errno
== EPIPE
) {
1726 * This is possible if the fd is closed on the other side
1727 * (outfd) or any write problem. It can be verbose a bit for a
1728 * normal execution if for instance the relayd is stopped
1729 * abruptly. This can happen so set this to a DBG statement.
1731 DBG("Consumer mmap write detected relayd hang up");
1733 /* Unhandled error, print it and stop function right now. */
1734 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret
, write_len
);
1738 stream
->output_written
+= ret
;
1740 /* This call is useless on a socket so better save a syscall. */
1742 /* This won't block, but will start writeout asynchronously */
1743 lttng_sync_file_range(
1744 outfd
, stream
->out_fd_offset
, write_len
, SYNC_FILE_RANGE_WRITE
);
1745 stream
->out_fd_offset
+= write_len
;
1746 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1751 * This is a special case that the relayd has closed its socket. Let's
1752 * cleanup the relayd object and all associated streams.
1754 if (relayd
&& relayd_hang_up
) {
1755 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1756 lttng_consumer_cleanup_relayd(relayd
);
1760 /* Unlock only if ctrl socket used */
1761 if (relayd
&& stream
->metadata_flag
) {
1762 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1770 * Splice the data from the ring buffer to the tracefile.
1772 * It must be called with the stream lock held.
1774 * Returns the number of bytes spliced.
1776 ssize_t
lttng_consumer_on_read_subbuffer_splice(struct lttng_consumer_local_data
*ctx
,
1777 struct lttng_consumer_stream
*stream
,
1779 unsigned long padding
)
1781 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1783 off_t orig_offset
= stream
->out_fd_offset
;
1784 int fd
= stream
->wait_fd
;
1785 /* Default is on the disk */
1786 int outfd
= stream
->out_fd
;
1787 struct consumer_relayd_sock_pair
*relayd
= nullptr;
1789 unsigned int relayd_hang_up
= 0;
1791 switch (the_consumer_data
.type
) {
1792 case LTTNG_CONSUMER_KERNEL
:
1794 case LTTNG_CONSUMER32_UST
:
1795 case LTTNG_CONSUMER64_UST
:
1796 /* Not supported for user space tracing */
1799 ERR("Unknown consumer_data type");
1803 /* RCU lock for the relayd pointer */
1806 /* Flag that the current stream if set for network streaming. */
1807 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1808 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1809 if (relayd
== nullptr) {
1814 splice_pipe
= stream
->splice_pipe
;
1816 /* Write metadata stream id before payload */
1818 unsigned long total_len
= len
;
1820 if (stream
->metadata_flag
) {
1822 * Lock the control socket for the complete duration of the function
1823 * since from this point on we will use the socket.
1825 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1827 if (stream
->reset_metadata_flag
) {
1828 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1829 stream
->relayd_stream_id
,
1830 stream
->metadata_version
);
1835 stream
->reset_metadata_flag
= 0;
1837 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, padding
);
1844 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1847 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1853 /* Use the returned socket. */
1856 /* No streaming, we have to set the len with the full padding */
1859 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1860 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1862 ERR("Reset metadata file");
1865 stream
->reset_metadata_flag
= 0;
1868 * Check if we need to change the tracefile before writing the packet.
1870 if (stream
->chan
->tracefile_size
> 0 &&
1871 (stream
->tracefile_size_current
+ len
) > stream
->chan
->tracefile_size
) {
1872 ret
= consumer_stream_rotate_output_files(stream
);
1877 outfd
= stream
->out_fd
;
1880 stream
->tracefile_size_current
+= len
;
1884 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1885 (unsigned long) offset
,
1889 ret_splice
= splice(
1890 fd
, &offset
, splice_pipe
[1], nullptr, len
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1891 DBG("splice chan to pipe, ret %zd", ret_splice
);
1892 if (ret_splice
< 0) {
1895 PERROR("Error in relay splice");
1899 /* Handle stream on the relayd if the output is on the network */
1900 if (relayd
&& stream
->metadata_flag
) {
1901 size_t metadata_payload_size
=
1902 sizeof(struct lttcomm_relayd_metadata_payload
);
1904 /* Update counter to fit the spliced data */
1905 ret_splice
+= metadata_payload_size
;
1906 len
+= metadata_payload_size
;
1908 * We do this so the return value can match the len passed as
1909 * argument to this function.
1911 written
-= metadata_payload_size
;
1914 /* Splice data out */
1915 ret_splice
= splice(splice_pipe
[0],
1920 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1921 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd", outfd
, ret_splice
);
1922 if (ret_splice
< 0) {
1927 } else if (ret_splice
> len
) {
1929 * We don't expect this code path to be executed but you never know
1930 * so this is an extra protection agains a buggy splice().
1933 written
+= ret_splice
;
1934 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
, len
);
1937 /* All good, update current len and continue. */
1941 /* This call is useless on a socket so better save a syscall. */
1943 /* This won't block, but will start writeout asynchronously */
1944 lttng_sync_file_range(
1945 outfd
, stream
->out_fd_offset
, ret_splice
, SYNC_FILE_RANGE_WRITE
);
1946 stream
->out_fd_offset
+= ret_splice
;
1948 stream
->output_written
+= ret_splice
;
1949 written
+= ret_splice
;
1952 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1958 * This is a special case that the relayd has closed its socket. Let's
1959 * cleanup the relayd object and all associated streams.
1961 if (relayd
&& relayd_hang_up
) {
1962 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1963 lttng_consumer_cleanup_relayd(relayd
);
1964 /* Skip splice error so the consumer does not fail */
1969 /* send the appropriate error description to sessiond */
1972 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1975 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1978 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1983 if (relayd
&& stream
->metadata_flag
) {
1984 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1992 * Sample the snapshot positions for a specific fd
1994 * Returns 0 on success, < 0 on error
1996 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
1998 switch (the_consumer_data
.type
) {
1999 case LTTNG_CONSUMER_KERNEL
:
2000 return lttng_kconsumer_sample_snapshot_positions(stream
);
2001 case LTTNG_CONSUMER32_UST
:
2002 case LTTNG_CONSUMER64_UST
:
2003 return lttng_ustconsumer_sample_snapshot_positions(stream
);
2005 ERR("Unknown consumer_data type");
2011 * Take a snapshot for a specific fd
2013 * Returns 0 on success, < 0 on error
2015 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2017 switch (the_consumer_data
.type
) {
2018 case LTTNG_CONSUMER_KERNEL
:
2019 return lttng_kconsumer_take_snapshot(stream
);
2020 case LTTNG_CONSUMER32_UST
:
2021 case LTTNG_CONSUMER64_UST
:
2022 return lttng_ustconsumer_take_snapshot(stream
);
2024 ERR("Unknown consumer_data type");
2031 * Get the produced position
2033 * Returns 0 on success, < 0 on error
2035 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2037 switch (the_consumer_data
.type
) {
2038 case LTTNG_CONSUMER_KERNEL
:
2039 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
2040 case LTTNG_CONSUMER32_UST
:
2041 case LTTNG_CONSUMER64_UST
:
2042 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2044 ERR("Unknown consumer_data type");
2051 * Get the consumed position (free-running counter position in bytes).
2053 * Returns 0 on success, < 0 on error
2055 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2057 switch (the_consumer_data
.type
) {
2058 case LTTNG_CONSUMER_KERNEL
:
2059 return lttng_kconsumer_get_consumed_snapshot(stream
, pos
);
2060 case LTTNG_CONSUMER32_UST
:
2061 case LTTNG_CONSUMER64_UST
:
2062 return lttng_ustconsumer_get_consumed_snapshot(stream
, pos
);
2064 ERR("Unknown consumer_data type");
2070 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2072 struct pollfd
*consumer_sockpoll
)
2074 switch (the_consumer_data
.type
) {
2075 case LTTNG_CONSUMER_KERNEL
:
2076 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2077 case LTTNG_CONSUMER32_UST
:
2078 case LTTNG_CONSUMER64_UST
:
2079 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2081 ERR("Unknown consumer_data type");
2087 static void lttng_consumer_close_all_metadata()
2089 switch (the_consumer_data
.type
) {
2090 case LTTNG_CONSUMER_KERNEL
:
2092 * The Kernel consumer has a different metadata scheme so we don't
2093 * close anything because the stream will be closed by the session
2097 case LTTNG_CONSUMER32_UST
:
2098 case LTTNG_CONSUMER64_UST
:
2100 * Close all metadata streams. The metadata hash table is passed and
2101 * this call iterates over it by closing all wakeup fd. This is safe
2102 * because at this point we are sure that the metadata producer is
2103 * either dead or blocked.
2105 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2108 ERR("Unknown consumer_data type");
2114 * Clean up a metadata stream and free its memory.
2116 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
, struct lttng_ht
*ht
)
2118 struct lttng_consumer_channel
*channel
= nullptr;
2119 bool free_channel
= false;
2121 LTTNG_ASSERT(stream
);
2123 * This call should NEVER receive regular stream. It must always be
2124 * metadata stream and this is crucial for data structure synchronization.
2126 LTTNG_ASSERT(stream
->metadata_flag
);
2128 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2130 pthread_mutex_lock(&the_consumer_data
.lock
);
2132 * Note that this assumes that a stream's channel is never changed and
2133 * that the stream's lock doesn't need to be taken to sample its
2136 channel
= stream
->chan
;
2137 pthread_mutex_lock(&channel
->lock
);
2138 pthread_mutex_lock(&stream
->lock
);
2139 if (channel
->metadata_cache
) {
2140 /* Only applicable to userspace consumers. */
2141 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
2144 /* Remove any reference to that stream. */
2145 consumer_stream_delete(stream
, ht
);
2147 /* Close down everything including the relayd if one. */
2148 consumer_stream_close_output(stream
);
2149 /* Destroy tracer buffers of the stream. */
2150 consumer_stream_destroy_buffers(stream
);
2152 /* Atomically decrement channel refcount since other threads can use it. */
2153 if (!uatomic_sub_return(&channel
->refcount
, 1) &&
2154 !uatomic_read(&channel
->nb_init_stream_left
)) {
2155 /* Go for channel deletion! */
2156 free_channel
= true;
2158 stream
->chan
= nullptr;
2161 * Nullify the stream reference so it is not used after deletion. The
2162 * channel lock MUST be acquired before being able to check for a NULL
2165 channel
->metadata_stream
= nullptr;
2167 if (channel
->metadata_cache
) {
2168 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
2170 pthread_mutex_unlock(&stream
->lock
);
2171 pthread_mutex_unlock(&channel
->lock
);
2172 pthread_mutex_unlock(&the_consumer_data
.lock
);
2175 consumer_del_channel(channel
);
2178 lttng_trace_chunk_put(stream
->trace_chunk
);
2179 stream
->trace_chunk
= nullptr;
2180 consumer_stream_free(stream
);
2184 * Action done with the metadata stream when adding it to the consumer internal
2185 * data structures to handle it.
2187 void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2189 struct lttng_ht
*ht
= metadata_ht
;
2190 struct lttng_ht_iter iter
;
2191 struct lttng_ht_node_u64
*node
;
2193 LTTNG_ASSERT(stream
);
2196 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2198 pthread_mutex_lock(&the_consumer_data
.lock
);
2199 pthread_mutex_lock(&stream
->chan
->lock
);
2200 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2201 pthread_mutex_lock(&stream
->lock
);
2204 * From here, refcounts are updated so be _careful_ when returning an error
2211 * Lookup the stream just to make sure it does not exist in our internal
2212 * state. This should NEVER happen.
2214 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2215 node
= lttng_ht_iter_get_node_u64(&iter
);
2216 LTTNG_ASSERT(!node
);
2219 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2220 * in terms of destroying the associated channel, because the action that
2221 * causes the count to become 0 also causes a stream to be added. The
2222 * channel deletion will thus be triggered by the following removal of this
2225 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2226 /* Increment refcount before decrementing nb_init_stream_left */
2228 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2231 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2233 lttng_ht_add_u64(the_consumer_data
.stream_per_chan_id_ht
, &stream
->node_channel_id
);
2236 * Add stream to the stream_list_ht of the consumer data. No need to steal
2237 * the key since the HT does not use it and we allow to add redundant keys
2240 lttng_ht_add_u64(the_consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2244 pthread_mutex_unlock(&stream
->lock
);
2245 pthread_mutex_unlock(&stream
->chan
->lock
);
2246 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2247 pthread_mutex_unlock(&the_consumer_data
.lock
);
2251 * Delete data stream that are flagged for deletion (endpoint_status).
2253 static void validate_endpoint_status_data_stream()
2255 struct lttng_ht_iter iter
;
2256 struct lttng_consumer_stream
*stream
;
2258 DBG("Consumer delete flagged data stream");
2261 cds_lfht_for_each_entry (data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2262 /* Validate delete flag of the stream */
2263 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2266 /* Delete it right now */
2267 consumer_del_stream(stream
, data_ht
);
2273 * Delete metadata stream that are flagged for deletion (endpoint_status).
2275 static void validate_endpoint_status_metadata_stream(struct lttng_poll_event
*pollset
)
2277 struct lttng_ht_iter iter
;
2278 struct lttng_consumer_stream
*stream
;
2280 DBG("Consumer delete flagged metadata stream");
2282 LTTNG_ASSERT(pollset
);
2285 cds_lfht_for_each_entry (metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2286 /* Validate delete flag of the stream */
2287 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2291 * Remove from pollset so the metadata thread can continue without
2292 * blocking on a deleted stream.
2294 lttng_poll_del(pollset
, stream
->wait_fd
);
2296 /* Delete it right now */
2297 consumer_del_metadata_stream(stream
, metadata_ht
);
2303 * Thread polls on metadata file descriptor and write them on disk or on the
2306 void *consumer_thread_metadata_poll(void *data
)
2308 int ret
, i
, pollfd
, err
= -1;
2309 uint32_t revents
, nb_fd
;
2310 struct lttng_consumer_stream
*stream
= nullptr;
2311 struct lttng_ht_iter iter
;
2312 struct lttng_ht_node_u64
*node
;
2313 struct lttng_poll_event events
;
2314 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
2317 rcu_register_thread();
2319 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2321 if (testpoint(consumerd_thread_metadata
)) {
2322 goto error_testpoint
;
2325 health_code_update();
2327 DBG("Thread metadata poll started");
2329 /* Size is set to 1 for the consumer_metadata pipe */
2330 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2332 ERR("Poll set creation failed");
2336 ret
= lttng_poll_add(&events
, lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2342 DBG("Metadata main loop started");
2346 health_code_update();
2347 health_poll_entry();
2348 DBG("Metadata poll wait");
2349 ret
= lttng_poll_wait(&events
, -1);
2350 DBG("Metadata poll return from wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2352 DBG("Metadata event caught in thread");
2354 if (errno
== EINTR
) {
2355 ERR("Poll EINTR caught");
2358 if (LTTNG_POLL_GETNB(&events
) == 0) {
2359 err
= 0; /* All is OK */
2366 /* From here, the event is a metadata wait fd */
2367 for (i
= 0; i
< nb_fd
; i
++) {
2368 health_code_update();
2370 revents
= LTTNG_POLL_GETEV(&events
, i
);
2371 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2373 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2374 if (revents
& LPOLLIN
) {
2377 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2379 sizeof(stream
)); /* NOLINT sizeof
2382 if (pipe_len
< sizeof(stream
)) { /* NOLINT sizeof used on a
2385 PERROR("read metadata stream");
2388 * Remove the pipe from the poll set and continue
2389 * the loop since their might be data to consume.
2393 lttng_pipe_get_readfd(
2394 ctx
->consumer_metadata_pipe
));
2395 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2399 /* A NULL stream means that the state has changed. */
2400 if (stream
== nullptr) {
2401 /* Check for deleted streams. */
2402 validate_endpoint_status_metadata_stream(&events
);
2406 DBG("Adding metadata stream %d to poll set",
2409 /* Add metadata stream to the global poll events list */
2411 &events
, stream
->wait_fd
, LPOLLIN
| LPOLLPRI
);
2412 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2413 DBG("Metadata thread pipe hung up");
2415 * Remove the pipe from the poll set and continue the loop
2416 * since their might be data to consume.
2420 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2421 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2424 ERR("Unexpected poll events %u for sock %d",
2430 /* Handle other stream */
2436 uint64_t tmp_id
= (uint64_t) pollfd
;
2438 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2440 node
= lttng_ht_iter_get_node_u64(&iter
);
2443 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
2445 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2446 /* Get the data out of the metadata file descriptor */
2447 DBG("Metadata available on fd %d", pollfd
);
2448 LTTNG_ASSERT(stream
->wait_fd
== pollfd
);
2451 health_code_update();
2453 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2455 * We don't check the return value here since if we get
2456 * a negative len, it means an error occurred thus we
2457 * simply remove it from the poll set and free the
2462 /* It's ok to have an unavailable sub-buffer */
2463 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2464 /* Clean up stream from consumer and free it. */
2465 lttng_poll_del(&events
, stream
->wait_fd
);
2466 consumer_del_metadata_stream(stream
, metadata_ht
);
2468 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2469 DBG("Metadata fd %d is hup|err.", pollfd
);
2470 if (!stream
->hangup_flush_done
&&
2471 (the_consumer_data
.type
== LTTNG_CONSUMER32_UST
||
2472 the_consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2473 DBG("Attempting to flush and consume the UST buffers");
2474 lttng_ustconsumer_on_stream_hangup(stream
);
2476 /* We just flushed the stream now read it. */
2478 health_code_update();
2480 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2482 * We don't check the return value here since if we
2483 * get a negative len, it means an error occurred
2484 * thus we simply remove it from the poll set and
2490 lttng_poll_del(&events
, stream
->wait_fd
);
2492 * This call update the channel states, closes file descriptors
2493 * and securely free the stream.
2495 consumer_del_metadata_stream(stream
, metadata_ht
);
2497 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2501 /* Release RCU lock for the stream looked up */
2509 DBG("Metadata poll thread exiting");
2511 lttng_poll_clean(&events
);
2516 ERR("Health error occurred in %s", __func__
);
2518 health_unregister(health_consumerd
);
2519 rcu_unregister_thread();
2524 * This thread polls the fds in the set to consume the data and write
2525 * it to tracefile if necessary.
2527 void *consumer_thread_data_poll(void *data
)
2529 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2530 struct pollfd
*pollfd
= nullptr;
2531 /* local view of the streams */
2532 struct lttng_consumer_stream
**local_stream
= nullptr, *new_stream
= nullptr;
2533 /* local view of consumer_data.fds_count */
2535 /* 2 for the consumer_data_pipe and wake up pipe */
2536 const int nb_pipes_fd
= 2;
2537 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2538 int nb_inactive_fd
= 0;
2539 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
2542 rcu_register_thread();
2544 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2546 if (testpoint(consumerd_thread_data
)) {
2547 goto error_testpoint
;
2550 health_code_update();
2552 local_stream
= zmalloc
<lttng_consumer_stream
*>();
2553 if (local_stream
== nullptr) {
2554 PERROR("local_stream malloc");
2559 health_code_update();
2565 * the fds set has been updated, we need to update our
2566 * local array as well
2568 pthread_mutex_lock(&the_consumer_data
.lock
);
2569 if (the_consumer_data
.need_update
) {
2574 local_stream
= nullptr;
2576 /* Allocate for all fds */
2578 calloc
<struct pollfd
>(the_consumer_data
.stream_count
+ nb_pipes_fd
);
2579 if (pollfd
== nullptr) {
2580 PERROR("pollfd malloc");
2581 pthread_mutex_unlock(&the_consumer_data
.lock
);
2585 local_stream
= calloc
<lttng_consumer_stream
*>(
2586 the_consumer_data
.stream_count
+ nb_pipes_fd
);
2587 if (local_stream
== nullptr) {
2588 PERROR("local_stream malloc");
2589 pthread_mutex_unlock(&the_consumer_data
.lock
);
2592 ret
= update_poll_array(
2593 ctx
, &pollfd
, local_stream
, data_ht
, &nb_inactive_fd
);
2595 ERR("Error in allocating pollfd or local_outfds");
2596 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2597 pthread_mutex_unlock(&the_consumer_data
.lock
);
2601 the_consumer_data
.need_update
= 0;
2603 pthread_mutex_unlock(&the_consumer_data
.lock
);
2605 /* No FDs and consumer_quit, consumer_cleanup the thread */
2606 if (nb_fd
== 0 && nb_inactive_fd
== 0 && CMM_LOAD_SHARED(consumer_quit
) == 1) {
2607 err
= 0; /* All is OK */
2610 /* poll on the array of fds */
2612 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2613 if (testpoint(consumerd_thread_data_poll
)) {
2616 health_poll_entry();
2617 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2619 DBG("poll num_rdy : %d", num_rdy
);
2620 if (num_rdy
== -1) {
2622 * Restart interrupted system call.
2624 if (errno
== EINTR
) {
2627 PERROR("Poll error");
2628 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2630 } else if (num_rdy
== 0) {
2631 DBG("Polling thread timed out");
2635 if (caa_unlikely(data_consumption_paused
)) {
2636 DBG("Data consumption paused, sleeping...");
2642 * If the consumer_data_pipe triggered poll go directly to the
2643 * beginning of the loop to update the array. We want to prioritize
2644 * array update over low-priority reads.
2646 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2647 ssize_t pipe_readlen
;
2649 DBG("consumer_data_pipe wake up");
2650 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2652 sizeof(new_stream
)); /* NOLINT sizeof used on
2654 if (pipe_readlen
< sizeof(new_stream
)) { /* NOLINT sizeof used on a pointer.
2656 PERROR("Consumer data pipe");
2657 /* Continue so we can at least handle the current stream(s). */
2662 * If the stream is NULL, just ignore it. It's also possible that
2663 * the sessiond poll thread changed the consumer_quit state and is
2664 * waking us up to test it.
2666 if (new_stream
== nullptr) {
2667 validate_endpoint_status_data_stream();
2671 /* Continue to update the local streams and handle prio ones */
2675 /* Handle wakeup pipe. */
2676 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2678 ssize_t pipe_readlen
;
2681 lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
, sizeof(dummy
));
2682 if (pipe_readlen
< 0) {
2683 PERROR("Consumer data wakeup pipe");
2685 /* We've been awakened to handle stream(s). */
2686 ctx
->has_wakeup
= 0;
2689 /* Take care of high priority channels first. */
2690 for (i
= 0; i
< nb_fd
; i
++) {
2691 health_code_update();
2693 if (local_stream
[i
] == nullptr) {
2696 if (pollfd
[i
].revents
& POLLPRI
) {
2697 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2699 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2700 /* it's ok to have an unavailable sub-buffer */
2701 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2702 /* Clean the stream and free it. */
2703 consumer_del_stream(local_stream
[i
], data_ht
);
2704 local_stream
[i
] = nullptr;
2705 } else if (len
> 0) {
2706 local_stream
[i
]->has_data_left_to_be_read_before_teardown
=
2713 * If we read high prio channel in this loop, try again
2714 * for more high prio data.
2720 /* Take care of low priority channels. */
2721 for (i
= 0; i
< nb_fd
; i
++) {
2722 health_code_update();
2724 if (local_stream
[i
] == nullptr) {
2727 if ((pollfd
[i
].revents
& POLLIN
) || local_stream
[i
]->hangup_flush_done
||
2728 local_stream
[i
]->has_data
) {
2729 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2730 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2731 /* it's ok to have an unavailable sub-buffer */
2732 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2733 /* Clean the stream and free it. */
2734 consumer_del_stream(local_stream
[i
], data_ht
);
2735 local_stream
[i
] = nullptr;
2736 } else if (len
> 0) {
2737 local_stream
[i
]->has_data_left_to_be_read_before_teardown
=
2743 /* Handle hangup and errors */
2744 for (i
= 0; i
< nb_fd
; i
++) {
2745 health_code_update();
2747 if (local_stream
[i
] == nullptr) {
2750 if (!local_stream
[i
]->hangup_flush_done
&&
2751 (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
)) &&
2752 (the_consumer_data
.type
== LTTNG_CONSUMER32_UST
||
2753 the_consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2754 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2756 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2757 /* Attempt read again, for the data we just flushed. */
2758 local_stream
[i
]->has_data_left_to_be_read_before_teardown
= 1;
2761 * When a stream's pipe dies (hup/err/nval), an "inactive producer" flush is
2762 * performed. This type of flush ensures that a new packet is produced no
2763 * matter the consumed/produced positions are.
2765 * This, in turn, causes the next pass to see that data available for the
2766 * stream. When we come back here, we can be assured that all available
2767 * data has been consumed and we can finally destroy the stream.
2769 * If the poll flag is HUP/ERR/NVAL and we have
2770 * read no data in this pass, we can remove the
2771 * stream from its hash table.
2773 if ((pollfd
[i
].revents
& POLLHUP
)) {
2774 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2775 if (!local_stream
[i
]->has_data_left_to_be_read_before_teardown
) {
2776 consumer_del_stream(local_stream
[i
], data_ht
);
2777 local_stream
[i
] = nullptr;
2780 } else if (pollfd
[i
].revents
& POLLERR
) {
2781 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2782 if (!local_stream
[i
]->has_data_left_to_be_read_before_teardown
) {
2783 consumer_del_stream(local_stream
[i
], data_ht
);
2784 local_stream
[i
] = nullptr;
2787 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2788 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2789 if (!local_stream
[i
]->has_data_left_to_be_read_before_teardown
) {
2790 consumer_del_stream(local_stream
[i
], data_ht
);
2791 local_stream
[i
] = nullptr;
2795 if (local_stream
[i
] != nullptr) {
2796 local_stream
[i
]->has_data_left_to_be_read_before_teardown
= 0;
2803 DBG("polling thread exiting");
2808 * Close the write side of the pipe so epoll_wait() in
2809 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2810 * read side of the pipe. If we close them both, epoll_wait strangely does
2811 * not return and could create a endless wait period if the pipe is the
2812 * only tracked fd in the poll set. The thread will take care of closing
2815 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2820 ERR("Health error occurred in %s", __func__
);
2822 health_unregister(health_consumerd
);
2824 rcu_unregister_thread();
2829 * Close wake-up end of each stream belonging to the channel. This will
2830 * allow the poll() on the stream read-side to detect when the
2831 * write-side (application) finally closes them.
2833 static void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2835 struct lttng_ht
*ht
;
2836 struct lttng_consumer_stream
*stream
;
2837 struct lttng_ht_iter iter
;
2839 ht
= the_consumer_data
.stream_per_chan_id_ht
;
2842 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2843 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2848 node_channel_id
.node
)
2851 * Protect against teardown with mutex.
2853 pthread_mutex_lock(&stream
->lock
);
2854 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2857 switch (the_consumer_data
.type
) {
2858 case LTTNG_CONSUMER_KERNEL
:
2860 case LTTNG_CONSUMER32_UST
:
2861 case LTTNG_CONSUMER64_UST
:
2862 if (stream
->metadata_flag
) {
2863 /* Safe and protected by the stream lock. */
2864 lttng_ustconsumer_close_metadata(stream
->chan
);
2867 * Note: a mutex is taken internally within
2868 * liblttng-ust-ctl to protect timer wakeup_fd
2869 * use from concurrent close.
2871 lttng_ustconsumer_close_stream_wakeup(stream
);
2875 ERR("Unknown consumer_data type");
2879 pthread_mutex_unlock(&stream
->lock
);
2884 static void destroy_channel_ht(struct lttng_ht
*ht
)
2886 struct lttng_ht_iter iter
;
2887 struct lttng_consumer_channel
*channel
;
2890 if (ht
== nullptr) {
2895 cds_lfht_for_each_entry (ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2896 ret
= lttng_ht_del(ht
, &iter
);
2897 LTTNG_ASSERT(ret
!= 0);
2901 lttng_ht_destroy(ht
);
2905 * This thread polls the channel fds to detect when they are being
2906 * closed. It closes all related streams if the channel is detected as
2907 * closed. It is currently only used as a shim layer for UST because the
2908 * consumerd needs to keep the per-stream wakeup end of pipes open for
2911 void *consumer_thread_channel_poll(void *data
)
2913 int ret
, i
, pollfd
, err
= -1;
2914 uint32_t revents
, nb_fd
;
2915 struct lttng_consumer_channel
*chan
= nullptr;
2916 struct lttng_ht_iter iter
;
2917 struct lttng_ht_node_u64
*node
;
2918 struct lttng_poll_event events
;
2919 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
2920 struct lttng_ht
*channel_ht
;
2922 rcu_register_thread();
2924 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2926 if (testpoint(consumerd_thread_channel
)) {
2927 goto error_testpoint
;
2930 health_code_update();
2932 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2934 /* ENOMEM at this point. Better to bail out. */
2938 DBG("Thread channel poll started");
2940 /* Size is set to 1 for the consumer_channel pipe */
2941 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2943 ERR("Poll set creation failed");
2947 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2953 DBG("Channel main loop started");
2957 health_code_update();
2958 DBG("Channel poll wait");
2959 health_poll_entry();
2960 ret
= lttng_poll_wait(&events
, -1);
2961 DBG("Channel poll return from wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2963 DBG("Channel event caught in thread");
2965 if (errno
== EINTR
) {
2966 ERR("Poll EINTR caught");
2969 if (LTTNG_POLL_GETNB(&events
) == 0) {
2970 err
= 0; /* All is OK */
2977 /* From here, the event is a channel wait fd */
2978 for (i
= 0; i
< nb_fd
; i
++) {
2979 health_code_update();
2981 revents
= LTTNG_POLL_GETEV(&events
, i
);
2982 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2984 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2985 if (revents
& LPOLLIN
) {
2986 enum consumer_channel_action action
;
2989 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2992 ERR("Error reading channel pipe");
2994 lttng_poll_del(&events
,
2995 ctx
->consumer_channel_pipe
[0]);
3000 case CONSUMER_CHANNEL_ADD
:
3001 DBG("Adding channel %d to poll set", chan
->wait_fd
);
3003 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
3006 lttng_ht_add_unique_u64(channel_ht
,
3007 &chan
->wait_fd_node
);
3009 /* Add channel to the global poll events list */
3010 // FIXME: Empty flag on a pipe pollset, this might
3012 lttng_poll_add(&events
, chan
->wait_fd
, 0);
3014 case CONSUMER_CHANNEL_DEL
:
3017 * This command should never be called if the
3018 * channel has streams monitored by either the data
3019 * or metadata thread. The consumer only notify this
3020 * thread with a channel del. command if it receives
3021 * a destroy channel command from the session daemon
3022 * that send it if a command prior to the
3023 * GET_CHANNEL failed.
3027 chan
= consumer_find_channel(key
);
3030 ERR("UST consumer get channel key %" PRIu64
3031 " not found for del channel",
3035 lttng_poll_del(&events
, chan
->wait_fd
);
3036 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
3037 ret
= lttng_ht_del(channel_ht
, &iter
);
3038 LTTNG_ASSERT(ret
== 0);
3040 switch (the_consumer_data
.type
) {
3041 case LTTNG_CONSUMER_KERNEL
:
3043 case LTTNG_CONSUMER32_UST
:
3044 case LTTNG_CONSUMER64_UST
:
3045 health_code_update();
3046 /* Destroy streams that might have been left
3047 * in the stream list. */
3048 clean_channel_stream_list(chan
);
3051 ERR("Unknown consumer_data type");
3056 * Release our own refcount. Force channel deletion
3057 * even if streams were not initialized.
3059 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3060 consumer_del_channel(chan
);
3065 case CONSUMER_CHANNEL_QUIT
:
3067 * Remove the pipe from the poll set and continue
3068 * the loop since their might be data to consume.
3070 lttng_poll_del(&events
,
3071 ctx
->consumer_channel_pipe
[0]);
3074 ERR("Unknown action");
3077 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3078 DBG("Channel thread pipe hung up");
3080 * Remove the pipe from the poll set and continue the loop
3081 * since their might be data to consume.
3083 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3086 ERR("Unexpected poll events %u for sock %d",
3092 /* Handle other stream */
3098 uint64_t tmp_id
= (uint64_t) pollfd
;
3100 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3102 node
= lttng_ht_iter_get_node_u64(&iter
);
3105 chan
= caa_container_of(node
, struct lttng_consumer_channel
, wait_fd_node
);
3107 /* Check for error event */
3108 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3109 DBG("Channel fd %d is hup|err.", pollfd
);
3111 lttng_poll_del(&events
, chan
->wait_fd
);
3112 ret
= lttng_ht_del(channel_ht
, &iter
);
3113 LTTNG_ASSERT(ret
== 0);
3116 * This will close the wait fd for each stream associated to
3117 * this channel AND monitored by the data/metadata thread thus
3118 * will be clean by the right thread.
3120 consumer_close_channel_streams(chan
);
3122 /* Release our own refcount */
3123 if (!uatomic_sub_return(&chan
->refcount
, 1) &&
3124 !uatomic_read(&chan
->nb_init_stream_left
)) {
3125 consumer_del_channel(chan
);
3128 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3133 /* Release RCU lock for the channel looked up */
3141 lttng_poll_clean(&events
);
3143 destroy_channel_ht(channel_ht
);
3146 DBG("Channel poll thread exiting");
3149 ERR("Health error occurred in %s", __func__
);
3151 health_unregister(health_consumerd
);
3152 rcu_unregister_thread();
3156 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3157 struct pollfd
*sockpoll
,
3163 LTTNG_ASSERT(sockpoll
);
3165 ret
= lttng_consumer_poll_socket(sockpoll
);
3169 DBG("Metadata connection on client_socket");
3171 /* Blocking call, waiting for transmission */
3172 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3173 if (ctx
->consumer_metadata_socket
< 0) {
3174 WARN("On accept metadata");
3185 * This thread listens on the consumerd socket and receives the file
3186 * descriptors from the session daemon.
3188 void *consumer_thread_sessiond_poll(void *data
)
3190 int sock
= -1, client_socket
, ret
, err
= -1;
3192 * structure to poll for incoming data on communication socket avoids
3193 * making blocking sockets.
3195 struct pollfd consumer_sockpoll
[2];
3196 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
3198 rcu_register_thread();
3200 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3202 if (testpoint(consumerd_thread_sessiond
)) {
3203 goto error_testpoint
;
3206 health_code_update();
3208 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3209 unlink(ctx
->consumer_command_sock_path
);
3210 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3211 if (client_socket
< 0) {
3212 ERR("Cannot create command socket");
3216 ret
= lttcomm_listen_unix_sock(client_socket
);
3221 DBG("Sending ready command to lttng-sessiond");
3222 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3223 /* return < 0 on error, but == 0 is not fatal */
3225 ERR("Error sending ready command to lttng-sessiond");
3229 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3230 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3231 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3232 consumer_sockpoll
[1].fd
= client_socket
;
3233 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3235 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3243 DBG("Connection on client_socket");
3245 /* Blocking call, waiting for transmission */
3246 sock
= lttcomm_accept_unix_sock(client_socket
);
3253 * Setup metadata socket which is the second socket connection on the
3254 * command unix socket.
3256 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3265 /* This socket is not useful anymore. */
3266 ret
= close(client_socket
);
3268 PERROR("close client_socket");
3272 /* update the polling structure to poll on the established socket */
3273 consumer_sockpoll
[1].fd
= sock
;
3274 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3277 health_code_update();
3279 health_poll_entry();
3280 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3289 DBG("Incoming command on sock");
3290 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3293 * This could simply be a session daemon quitting. Don't output
3296 DBG("Communication interrupted on command socket");
3300 if (CMM_LOAD_SHARED(consumer_quit
)) {
3301 DBG("consumer_thread_receive_fds received quit from signal");
3302 err
= 0; /* All is OK */
3305 DBG("Received command on sock");
3311 DBG("Consumer thread sessiond poll exiting");
3314 * Close metadata streams since the producer is the session daemon which
3317 * NOTE: for now, this only applies to the UST tracer.
3319 lttng_consumer_close_all_metadata();
3322 * when all fds have hung up, the polling thread
3325 CMM_STORE_SHARED(consumer_quit
, 1);
3328 * Notify the data poll thread to poll back again and test the
3329 * consumer_quit state that we just set so to quit gracefully.
3331 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3333 notify_channel_pipe(ctx
, nullptr, -1, CONSUMER_CHANNEL_QUIT
);
3335 notify_health_quit_pipe(health_quit_pipe
);
3337 /* Cleaning up possibly open sockets. */
3341 PERROR("close sock sessiond poll");
3344 if (client_socket
>= 0) {
3345 ret
= close(client_socket
);
3347 PERROR("close client_socket sessiond poll");
3354 ERR("Health error occurred in %s", __func__
);
3356 health_unregister(health_consumerd
);
3358 rcu_unregister_thread();
3362 static int post_consume(struct lttng_consumer_stream
*stream
,
3363 const struct stream_subbuffer
*subbuffer
,
3364 struct lttng_consumer_local_data
*ctx
)
3368 const size_t count
=
3369 lttng_dynamic_array_get_count(&stream
->read_subbuffer_ops
.post_consume_cbs
);
3371 for (i
= 0; i
< count
; i
++) {
3372 const post_consume_cb op
= *(post_consume_cb
*) lttng_dynamic_array_get_element(
3373 &stream
->read_subbuffer_ops
.post_consume_cbs
, i
);
3375 ret
= op(stream
, subbuffer
, ctx
);
3384 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3385 struct lttng_consumer_local_data
*ctx
,
3386 bool locked_by_caller
)
3388 ssize_t ret
, written_bytes
= 0;
3390 struct stream_subbuffer subbuffer
= {};
3391 enum get_next_subbuffer_status get_next_status
;
3393 if (!locked_by_caller
) {
3394 stream
->read_subbuffer_ops
.lock(stream
);
3396 stream
->read_subbuffer_ops
.assert_locked(stream
);
3399 if (stream
->read_subbuffer_ops
.on_wake_up
) {
3400 ret
= stream
->read_subbuffer_ops
.on_wake_up(stream
);
3407 * If the stream was flagged to be ready for rotation before we extract
3408 * the next packet, rotate it now.
3410 if (stream
->rotate_ready
) {
3411 DBG("Rotate stream before consuming data");
3412 ret
= lttng_consumer_rotate_stream(stream
);
3414 ERR("Stream rotation error before consuming data");
3419 get_next_status
= stream
->read_subbuffer_ops
.get_next_subbuffer(stream
, &subbuffer
);
3420 switch (get_next_status
) {
3421 case GET_NEXT_SUBBUFFER_STATUS_OK
:
3423 case GET_NEXT_SUBBUFFER_STATUS_NO_DATA
:
3427 case GET_NEXT_SUBBUFFER_STATUS_ERROR
:
3434 ret
= stream
->read_subbuffer_ops
.pre_consume_subbuffer(stream
, &subbuffer
);
3436 goto error_put_subbuf
;
3439 written_bytes
= stream
->read_subbuffer_ops
.consume_subbuffer(ctx
, stream
, &subbuffer
);
3440 if (written_bytes
<= 0) {
3441 ERR("Error consuming subbuffer: (%zd)", written_bytes
);
3442 ret
= (int) written_bytes
;
3443 goto error_put_subbuf
;
3446 ret
= stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3451 ret
= post_consume(stream
, &subbuffer
, ctx
);
3457 * After extracting the packet, we check if the stream is now ready to
3458 * be rotated and perform the action immediately.
3460 * Don't overwrite `ret` as callers expect the number of bytes
3461 * consumed to be returned on success.
3463 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
3464 if (rotation_ret
== 1) {
3465 rotation_ret
= lttng_consumer_rotate_stream(stream
);
3466 if (rotation_ret
< 0) {
3468 ERR("Stream rotation error after consuming data");
3472 } else if (rotation_ret
< 0) {
3474 ERR("Failed to check if stream was ready to rotate after consuming data");
3479 if (stream
->read_subbuffer_ops
.on_sleep
) {
3480 stream
->read_subbuffer_ops
.on_sleep(stream
, ctx
);
3483 ret
= written_bytes
;
3485 if (!locked_by_caller
) {
3486 stream
->read_subbuffer_ops
.unlock(stream
);
3491 (void) stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3495 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3497 switch (the_consumer_data
.type
) {
3498 case LTTNG_CONSUMER_KERNEL
:
3499 return lttng_kconsumer_on_recv_stream(stream
);
3500 case LTTNG_CONSUMER32_UST
:
3501 case LTTNG_CONSUMER64_UST
:
3502 return lttng_ustconsumer_on_recv_stream(stream
);
3504 ERR("Unknown consumer_data type");
3511 * Allocate and set consumer data hash tables.
3513 int lttng_consumer_init()
3515 the_consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3516 if (!the_consumer_data
.channel_ht
) {
3520 the_consumer_data
.channels_by_session_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3521 if (!the_consumer_data
.channels_by_session_id_ht
) {
3525 the_consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3526 if (!the_consumer_data
.relayd_ht
) {
3530 the_consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3531 if (!the_consumer_data
.stream_list_ht
) {
3535 the_consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3536 if (!the_consumer_data
.stream_per_chan_id_ht
) {
3540 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3545 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3550 the_consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3551 if (!the_consumer_data
.chunk_registry
) {
3562 * Process the ADD_RELAYD command receive by a consumer.
3564 * This will create a relayd socket pair and add it to the relayd hash table.
3565 * The caller MUST acquire a RCU read side lock before calling it.
3567 void consumer_add_relayd_socket(uint64_t net_seq_idx
,
3569 struct lttng_consumer_local_data
*ctx
,
3571 struct pollfd
*consumer_sockpoll
,
3572 uint64_t sessiond_id
,
3573 uint64_t relayd_session_id
,
3574 uint32_t relayd_version_major
,
3575 uint32_t relayd_version_minor
,
3576 enum lttcomm_sock_proto relayd_socket_protocol
)
3578 int fd
= -1, ret
= -1, relayd_created
= 0;
3579 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3580 struct consumer_relayd_sock_pair
*relayd
= nullptr;
3583 LTTNG_ASSERT(sock
>= 0);
3584 ASSERT_RCU_READ_LOCKED();
3586 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3588 /* Get relayd reference if exists. */
3589 relayd
= consumer_find_relayd(net_seq_idx
);
3590 if (relayd
== nullptr) {
3591 LTTNG_ASSERT(sock_type
== LTTNG_STREAM_CONTROL
);
3592 /* Not found. Allocate one. */
3593 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3594 if (relayd
== nullptr) {
3595 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3598 relayd
->sessiond_session_id
= sessiond_id
;
3603 * This code path MUST continue to the consumer send status message to
3604 * we can notify the session daemon and continue our work without
3605 * killing everything.
3609 * relayd key should never be found for control socket.
3611 LTTNG_ASSERT(sock_type
!= LTTNG_STREAM_CONTROL
);
3614 /* First send a status message before receiving the fds. */
3615 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3617 /* Somehow, the session daemon is not responding anymore. */
3618 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3619 goto error_nosignal
;
3622 /* Poll on consumer socket. */
3623 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3625 /* Needing to exit in the middle of a command: error. */
3626 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3627 goto error_nosignal
;
3630 /* Get relayd socket from session daemon */
3631 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3632 if (ret
!= sizeof(fd
)) {
3633 fd
= -1; /* Just in case it gets set with an invalid value. */
3636 * Failing to receive FDs might indicate a major problem such as
3637 * reaching a fd limit during the receive where the kernel returns a
3638 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3639 * don't take any chances and stop everything.
3641 * XXX: Feature request #558 will fix that and avoid this possible
3642 * issue when reaching the fd limit.
3644 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3645 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3649 /* Copy socket information and received FD */
3650 switch (sock_type
) {
3651 case LTTNG_STREAM_CONTROL
:
3652 /* Copy received lttcomm socket */
3653 ret
= lttcomm_populate_sock_from_open_socket(
3654 &relayd
->control_sock
.sock
, fd
, relayd_socket_protocol
);
3656 /* Assign version values. */
3657 relayd
->control_sock
.major
= relayd_version_major
;
3658 relayd
->control_sock
.minor
= relayd_version_minor
;
3660 relayd
->relayd_session_id
= relayd_session_id
;
3663 case LTTNG_STREAM_DATA
:
3664 /* Copy received lttcomm socket */
3665 ret
= lttcomm_populate_sock_from_open_socket(
3666 &relayd
->data_sock
.sock
, fd
, relayd_socket_protocol
);
3667 /* Assign version values. */
3668 relayd
->data_sock
.major
= relayd_version_major
;
3669 relayd
->data_sock
.minor
= relayd_version_minor
;
3672 ERR("Unknown relayd socket type (%d)", sock_type
);
3673 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3678 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3682 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3683 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3684 relayd
->net_seq_idx
,
3687 * We gave the ownership of the fd to the relayd structure. Set the
3688 * fd to -1 so we don't call close() on it in the error path below.
3692 /* We successfully added the socket. Send status back. */
3693 ret
= consumer_send_status_msg(sock
, ret_code
);
3695 /* Somehow, the session daemon is not responding anymore. */
3696 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3697 goto error_nosignal
;
3701 * Add relayd socket pair to consumer data hashtable. If object already
3702 * exists or on error, the function gracefully returns.
3711 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3712 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3716 /* Close received socket if valid. */
3719 PERROR("close received socket");
3723 if (relayd_created
) {
3729 * Search for a relayd associated to the session id and return the reference.
3731 * A rcu read side lock MUST be acquire before calling this function and locked
3732 * until the relayd object is no longer necessary.
3734 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3736 struct lttng_ht_iter iter
;
3737 struct consumer_relayd_sock_pair
*relayd
= nullptr;
3739 ASSERT_RCU_READ_LOCKED();
3741 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3742 cds_lfht_for_each_entry (the_consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
, node
.node
) {
3744 * Check by sessiond id which is unique here where the relayd session
3745 * id might not be when having multiple relayd.
3747 if (relayd
->sessiond_session_id
== id
) {
3748 /* Found the relayd. There can be only one per id. */
3760 * Check if for a given session id there is still data needed to be extract
3763 * Return 1 if data is pending or else 0 meaning ready to be read.
3765 int consumer_data_pending(uint64_t id
)
3768 struct lttng_ht_iter iter
;
3769 struct lttng_ht
*ht
;
3770 struct lttng_consumer_stream
*stream
;
3771 struct consumer_relayd_sock_pair
*relayd
= nullptr;
3772 int (*data_pending
)(struct lttng_consumer_stream
*);
3774 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3777 pthread_mutex_lock(&the_consumer_data
.lock
);
3779 switch (the_consumer_data
.type
) {
3780 case LTTNG_CONSUMER_KERNEL
:
3781 data_pending
= lttng_kconsumer_data_pending
;
3783 case LTTNG_CONSUMER32_UST
:
3784 case LTTNG_CONSUMER64_UST
:
3785 data_pending
= lttng_ustconsumer_data_pending
;
3788 ERR("Unknown consumer data type");
3792 /* Ease our life a bit */
3793 ht
= the_consumer_data
.stream_list_ht
;
3795 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3796 ht
->hash_fct(&id
, lttng_ht_seed
),
3801 node_session_id
.node
)
3803 pthread_mutex_lock(&stream
->lock
);
3806 * A removed node from the hash table indicates that the stream has
3807 * been deleted thus having a guarantee that the buffers are closed
3808 * on the consumer side. However, data can still be transmitted
3809 * over the network so don't skip the relayd check.
3811 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3813 /* Check the stream if there is data in the buffers. */
3814 ret
= data_pending(stream
);
3816 pthread_mutex_unlock(&stream
->lock
);
3821 pthread_mutex_unlock(&stream
->lock
);
3824 relayd
= find_relayd_by_session_id(id
);
3826 unsigned int is_data_inflight
= 0;
3828 /* Send init command for data pending. */
3829 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3830 ret
= relayd_begin_data_pending(&relayd
->control_sock
, relayd
->relayd_session_id
);
3832 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3833 /* Communication error thus the relayd so no data pending. */
3834 goto data_not_pending
;
3837 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3838 ht
->hash_fct(&id
, lttng_ht_seed
),
3843 node_session_id
.node
)
3845 if (stream
->metadata_flag
) {
3846 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3847 stream
->relayd_stream_id
);
3849 ret
= relayd_data_pending(&relayd
->control_sock
,
3850 stream
->relayd_stream_id
,
3851 stream
->next_net_seq_num
- 1);
3855 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3857 } else if (ret
< 0) {
3858 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".",
3859 relayd
->net_seq_idx
);
3860 lttng_consumer_cleanup_relayd(relayd
);
3861 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3862 goto data_not_pending
;
3866 /* Send end command for data pending. */
3867 ret
= relayd_end_data_pending(
3868 &relayd
->control_sock
, relayd
->relayd_session_id
, &is_data_inflight
);
3869 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3871 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".",
3872 relayd
->net_seq_idx
);
3873 lttng_consumer_cleanup_relayd(relayd
);
3874 goto data_not_pending
;
3876 if (is_data_inflight
) {
3882 * Finding _no_ node in the hash table and no inflight data means that the
3883 * stream(s) have been removed thus data is guaranteed to be available for
3884 * analysis from the trace files.
3888 /* Data is available to be read by a viewer. */
3889 pthread_mutex_unlock(&the_consumer_data
.lock
);
3894 /* Data is still being extracted from buffers. */
3895 pthread_mutex_unlock(&the_consumer_data
.lock
);
3901 * Send a ret code status message to the sessiond daemon.
3903 * Return the sendmsg() return value.
3905 int consumer_send_status_msg(int sock
, int ret_code
)
3907 struct lttcomm_consumer_status_msg msg
;
3909 memset(&msg
, 0, sizeof(msg
));
3910 msg
.ret_code
= (lttcomm_return_code
) ret_code
;
3912 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3916 * Send a channel status message to the sessiond daemon.
3918 * Return the sendmsg() return value.
3920 int consumer_send_status_channel(int sock
, struct lttng_consumer_channel
*channel
)
3922 struct lttcomm_consumer_status_channel msg
;
3924 LTTNG_ASSERT(sock
>= 0);
3926 memset(&msg
, 0, sizeof(msg
));
3928 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3930 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3931 msg
.key
= channel
->key
;
3932 msg
.stream_count
= channel
->streams
.count
;
3935 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3938 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3939 unsigned long produced_pos
,
3940 uint64_t nb_packets_per_stream
,
3941 uint64_t max_sb_size
)
3943 unsigned long start_pos
;
3945 if (!nb_packets_per_stream
) {
3946 return consumed_pos
; /* Grab everything */
3948 start_pos
= produced_pos
- lttng_offset_align_floor(produced_pos
, max_sb_size
);
3949 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3950 if ((long) (start_pos
- consumed_pos
) < 0) {
3951 return consumed_pos
; /* Grab everything */
3956 /* Stream lock must be held by the caller. */
3957 static int sample_stream_positions(struct lttng_consumer_stream
*stream
,
3958 unsigned long *produced
,
3959 unsigned long *consumed
)
3963 ASSERT_LOCKED(stream
->lock
);
3965 ret
= lttng_consumer_sample_snapshot_positions(stream
);
3967 ERR("Failed to sample snapshot positions");
3971 ret
= lttng_consumer_get_produced_snapshot(stream
, produced
);
3973 ERR("Failed to sample produced position");
3977 ret
= lttng_consumer_get_consumed_snapshot(stream
, consumed
);
3979 ERR("Failed to sample consumed position");
3988 * Sample the rotate position for all the streams of a channel. If a stream
3989 * is already at the rotate position (produced == consumed), we flag it as
3990 * ready for rotation. The rotation of ready streams occurs after we have
3991 * replied to the session daemon that we have finished sampling the positions.
3992 * Must be called with RCU read-side lock held to ensure existence of channel.
3994 * Returns 0 on success, < 0 on error
3996 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
4001 struct lttng_consumer_stream
*stream
;
4002 struct lttng_ht_iter iter
;
4003 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
4004 struct lttng_dynamic_array stream_rotation_positions
;
4005 uint64_t next_chunk_id
, stream_count
= 0;
4006 enum lttng_trace_chunk_status chunk_status
;
4007 const bool is_local_trace
= relayd_id
== -1ULL;
4008 struct consumer_relayd_sock_pair
*relayd
= nullptr;
4009 bool rotating_to_new_chunk
= true;
4010 /* Array of `struct lttng_consumer_stream *` */
4011 struct lttng_dynamic_pointer_array streams_packet_to_open
;
4014 ASSERT_RCU_READ_LOCKED();
4016 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
4018 lttng_dynamic_array_init(&stream_rotation_positions
,
4019 sizeof(struct relayd_stream_rotation_position
),
4021 lttng_dynamic_pointer_array_init(&streams_packet_to_open
, nullptr);
4025 pthread_mutex_lock(&channel
->lock
);
4026 LTTNG_ASSERT(channel
->trace_chunk
);
4027 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
, &next_chunk_id
);
4028 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4030 goto end_unlock_channel
;
4033 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4034 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4039 node_channel_id
.node
)
4041 unsigned long produced_pos
= 0, consumed_pos
= 0;
4043 health_code_update();
4046 * Lock stream because we are about to change its state.
4048 pthread_mutex_lock(&stream
->lock
);
4050 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
4051 rotating_to_new_chunk
= false;
4055 * Do not flush a packet when rotating from a NULL trace
4056 * chunk. The stream has no means to output data, and the prior
4057 * rotation which rotated to NULL performed that side-effect
4058 * already. No new data can be produced when a stream has no
4059 * associated trace chunk (e.g. a stop followed by a rotate).
4061 if (stream
->trace_chunk
) {
4064 if (stream
->metadata_flag
) {
4066 * Don't produce an empty metadata packet,
4067 * simply close the current one.
4069 * Metadata is regenerated on every trace chunk
4070 * switch; there is no concern that no data was
4073 flush_active
= true;
4076 * Only flush an empty packet if the "packet
4077 * open" could not be performed on transition
4078 * to a new trace chunk and no packets were
4079 * consumed within the chunk's lifetime.
4081 if (stream
->opened_packet_in_current_trace_chunk
) {
4082 flush_active
= true;
4085 * Stream could have been full at the
4086 * time of rotation, but then have had
4087 * no activity at all.
4089 * It is important to flush a packet
4090 * to prevent 0-length files from being
4091 * produced as most viewers choke on
4094 * Unfortunately viewers will not be
4095 * able to know that tracing was active
4096 * for this stream during this trace
4099 ret
= sample_stream_positions(
4100 stream
, &produced_pos
, &consumed_pos
);
4102 goto end_unlock_stream
;
4106 * Don't flush an empty packet if data
4107 * was produced; it will be consumed
4108 * before the rotation completes.
4110 flush_active
= produced_pos
!= consumed_pos
;
4111 if (!flush_active
) {
4112 const char *trace_chunk_name
;
4113 uint64_t trace_chunk_id
;
4115 chunk_status
= lttng_trace_chunk_get_name(
4116 stream
->trace_chunk
,
4119 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NONE
) {
4120 trace_chunk_name
= "none";
4124 * Consumer trace chunks are
4127 chunk_status
= lttng_trace_chunk_get_id(
4128 stream
->trace_chunk
, &trace_chunk_id
);
4129 LTTNG_ASSERT(chunk_status
==
4130 LTTNG_TRACE_CHUNK_STATUS_OK
);
4132 DBG("Unable to open packet for stream during trace chunk's lifetime. "
4133 "Flushing an empty packet to prevent an empty file from being created: "
4134 "stream id = %" PRIu64
4135 ", trace chunk name = `%s`, trace chunk id = %" PRIu64
,
4144 * Close the current packet before sampling the
4145 * ring buffer positions.
4147 ret
= consumer_stream_flush_buffer(stream
, flush_active
);
4149 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4151 goto end_unlock_stream
;
4155 ret
= lttng_consumer_take_snapshot(stream
);
4156 if (ret
< 0 && ret
!= -ENODATA
&& ret
!= -EAGAIN
) {
4157 ERR("Failed to sample snapshot position during channel rotation");
4158 goto end_unlock_stream
;
4161 ret
= lttng_consumer_get_produced_snapshot(stream
, &produced_pos
);
4163 ERR("Failed to sample produced position during channel rotation");
4164 goto end_unlock_stream
;
4167 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos
);
4169 ERR("Failed to sample consumed position during channel rotation");
4170 goto end_unlock_stream
;
4174 * Align produced position on the start-of-packet boundary of the first
4175 * packet going into the next trace chunk.
4177 produced_pos
= lttng_align_floor(produced_pos
, stream
->max_sb_size
);
4178 if (consumed_pos
== produced_pos
) {
4179 DBG("Set rotate ready for stream %" PRIu64
" produced = %lu consumed = %lu",
4183 stream
->rotate_ready
= true;
4185 DBG("Different consumed and produced positions "
4186 "for stream %" PRIu64
" produced = %lu consumed = %lu",
4192 * The rotation position is based on the packet_seq_num of the
4193 * packet following the last packet that was consumed for this
4194 * stream, incremented by the offset between produced and
4195 * consumed positions. This rotation position is a lower bound
4196 * (inclusive) at which the next trace chunk starts. Since it
4197 * is a lower bound, it is OK if the packet_seq_num does not
4198 * correspond exactly to the same packet identified by the
4199 * consumed_pos, which can happen in overwrite mode.
4201 if (stream
->sequence_number_unavailable
) {
4203 * Rotation should never be performed on a session which
4204 * interacts with a pre-2.8 lttng-modules, which does
4205 * not implement packet sequence number.
4207 ERR("Failure to rotate stream %" PRIu64
": sequence number unavailable",
4210 goto end_unlock_stream
;
4212 stream
->rotate_position
= stream
->last_sequence_number
+ 1 +
4213 ((produced_pos
- consumed_pos
) / stream
->max_sb_size
);
4214 DBG("Set rotation position for stream %" PRIu64
" at position %" PRIu64
,
4216 stream
->rotate_position
);
4218 if (!is_local_trace
) {
4220 * The relay daemon control protocol expects a rotation
4221 * position as "the sequence number of the first packet
4222 * _after_ the current trace chunk".
4224 const struct relayd_stream_rotation_position position
= {
4225 .stream_id
= stream
->relayd_stream_id
,
4226 .rotate_at_seq_num
= stream
->rotate_position
,
4229 ret
= lttng_dynamic_array_add_element(&stream_rotation_positions
,
4232 ERR("Failed to allocate stream rotation position");
4233 goto end_unlock_stream
;
4238 stream
->opened_packet_in_current_trace_chunk
= false;
4240 if (rotating_to_new_chunk
&& !stream
->metadata_flag
) {
4242 * Attempt to flush an empty packet as close to the
4243 * rotation point as possible. In the event where a
4244 * stream remains inactive after the rotation point,
4245 * this ensures that the new trace chunk has a
4246 * beginning timestamp set at the begining of the
4247 * trace chunk instead of only creating an empty
4248 * packet when the trace chunk is stopped.
4250 * This indicates to the viewers that the stream
4251 * was being recorded, but more importantly it
4252 * allows viewers to determine a useable trace
4255 * This presents a problem in the case where the
4256 * ring-buffer is completely full.
4258 * Consider the following scenario:
4259 * - The consumption of data is slow (slow network,
4261 * - The ring buffer is full,
4262 * - A rotation is initiated,
4263 * - The flush below does nothing (no space left to
4264 * open a new packet),
4265 * - The other streams rotate very soon, and new
4266 * data is produced in the new chunk,
4267 * - This stream completes its rotation long after the
4268 * rotation was initiated
4269 * - The session is stopped before any event can be
4270 * produced in this stream's buffers.
4272 * The resulting trace chunk will have a single packet
4273 * temporaly at the end of the trace chunk for this
4274 * stream making the stream intersection more narrow
4275 * than it should be.
4277 * To work-around this, an empty flush is performed
4278 * after the first consumption of a packet during a
4279 * rotation if open_packet fails. The idea is that
4280 * consuming a packet frees enough space to switch
4281 * packets in this scenario and allows the tracer to
4282 * "stamp" the beginning of the new trace chunk at the
4283 * earliest possible point.
4285 * The packet open is performed after the channel
4286 * rotation to ensure that no attempt to open a packet
4287 * is performed in a stream that has no active trace
4290 ret
= lttng_dynamic_pointer_array_add_pointer(&streams_packet_to_open
,
4293 PERROR("Failed to add a stream pointer to array of streams in which to open a packet");
4295 goto end_unlock_stream
;
4299 pthread_mutex_unlock(&stream
->lock
);
4303 if (!is_local_trace
) {
4304 relayd
= consumer_find_relayd(relayd_id
);
4306 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4308 goto end_unlock_channel
;
4311 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4312 ret
= relayd_rotate_streams(&relayd
->control_sock
,
4314 rotating_to_new_chunk
? &next_chunk_id
: nullptr,
4315 (const struct relayd_stream_rotation_position
*)
4316 stream_rotation_positions
.buffer
.data
);
4317 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4319 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4320 relayd
->net_seq_idx
);
4321 lttng_consumer_cleanup_relayd(relayd
);
4322 goto end_unlock_channel
;
4326 for (stream_idx
= 0;
4327 stream_idx
< lttng_dynamic_pointer_array_get_count(&streams_packet_to_open
);
4329 enum consumer_stream_open_packet_status status
;
4331 stream
= (lttng_consumer_stream
*) lttng_dynamic_pointer_array_get_pointer(
4332 &streams_packet_to_open
, stream_idx
);
4334 pthread_mutex_lock(&stream
->lock
);
4335 status
= consumer_stream_open_packet(stream
);
4336 pthread_mutex_unlock(&stream
->lock
);
4338 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
4339 DBG("Opened a packet after a rotation: stream id = %" PRIu64
4340 ", channel name = %s, session id = %" PRIu64
,
4343 stream
->chan
->session_id
);
4345 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
4347 * Can't open a packet as there is no space left
4348 * in the buffer. A new packet will be opened
4349 * once one has been consumed.
4351 DBG("No space left to open a packet after a rotation: stream id = %" PRIu64
4352 ", channel name = %s, session id = %" PRIu64
,
4355 stream
->chan
->session_id
);
4357 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
4358 /* Logged by callee. */
4360 goto end_unlock_channel
;
4366 pthread_mutex_unlock(&channel
->lock
);
4371 pthread_mutex_unlock(&stream
->lock
);
4373 pthread_mutex_unlock(&channel
->lock
);
4376 lttng_dynamic_array_reset(&stream_rotation_positions
);
4377 lttng_dynamic_pointer_array_reset(&streams_packet_to_open
);
4381 static int consumer_clear_buffer(struct lttng_consumer_stream
*stream
)
4384 unsigned long consumed_pos_before
, consumed_pos_after
;
4386 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4388 ERR("Taking snapshot positions");
4392 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_before
);
4394 ERR("Consumed snapshot position");
4398 switch (the_consumer_data
.type
) {
4399 case LTTNG_CONSUMER_KERNEL
:
4400 ret
= kernctl_buffer_clear(stream
->wait_fd
);
4402 ERR("Failed to clear kernel stream (ret = %d)", ret
);
4406 case LTTNG_CONSUMER32_UST
:
4407 case LTTNG_CONSUMER64_UST
:
4408 ret
= lttng_ustconsumer_clear_buffer(stream
);
4410 ERR("Failed to clear ust stream (ret = %d)", ret
);
4415 ERR("Unknown consumer_data type");
4419 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4421 ERR("Taking snapshot positions");
4424 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_after
);
4426 ERR("Consumed snapshot position");
4429 DBG("clear: before: %lu after: %lu", consumed_pos_before
, consumed_pos_after
);
4434 static int consumer_clear_stream(struct lttng_consumer_stream
*stream
)
4438 ret
= consumer_stream_flush_buffer(stream
, true);
4440 ERR("Failed to flush stream %" PRIu64
" during channel clear", stream
->key
);
4441 ret
= LTTCOMM_CONSUMERD_FATAL
;
4445 ret
= consumer_clear_buffer(stream
);
4447 ERR("Failed to clear stream %" PRIu64
" during channel clear", stream
->key
);
4448 ret
= LTTCOMM_CONSUMERD_FATAL
;
4452 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4457 static int consumer_clear_unmonitored_channel(struct lttng_consumer_channel
*channel
)
4460 struct lttng_consumer_stream
*stream
;
4463 pthread_mutex_lock(&channel
->lock
);
4464 cds_list_for_each_entry (stream
, &channel
->streams
.head
, send_node
) {
4465 health_code_update();
4466 pthread_mutex_lock(&stream
->lock
);
4467 ret
= consumer_clear_stream(stream
);
4471 pthread_mutex_unlock(&stream
->lock
);
4473 pthread_mutex_unlock(&channel
->lock
);
4478 pthread_mutex_unlock(&stream
->lock
);
4479 pthread_mutex_unlock(&channel
->lock
);
4485 * Check if a stream is ready to be rotated after extracting it.
4487 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4488 * error. Stream lock must be held.
4490 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4492 DBG("Check is rotate ready for stream %" PRIu64
" ready %u rotate_position %" PRIu64
4493 " last_sequence_number %" PRIu64
,
4495 stream
->rotate_ready
,
4496 stream
->rotate_position
,
4497 stream
->last_sequence_number
);
4498 if (stream
->rotate_ready
) {
4503 * If packet seq num is unavailable, it means we are interacting
4504 * with a pre-2.8 lttng-modules which does not implement the
4505 * sequence number. Rotation should never be used by sessiond in this
4508 if (stream
->sequence_number_unavailable
) {
4509 ERR("Internal error: rotation used on stream %" PRIu64
4510 " with unavailable sequence number",
4515 if (stream
->rotate_position
== -1ULL || stream
->last_sequence_number
== -1ULL) {
4520 * Rotate position not reached yet. The stream rotate position is
4521 * the position of the next packet belonging to the next trace chunk,
4522 * but consumerd considers rotation ready when reaching the last
4523 * packet of the current chunk, hence the "rotate_position - 1".
4526 DBG("Check is rotate ready for stream %" PRIu64
" last_sequence_number %" PRIu64
4527 " rotate_position %" PRIu64
,
4529 stream
->last_sequence_number
,
4530 stream
->rotate_position
);
4531 if (stream
->last_sequence_number
>= stream
->rotate_position
- 1) {
4539 * Reset the state for a stream after a rotation occurred.
4541 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4543 DBG("lttng_consumer_reset_stream_rotate_state for stream %" PRIu64
, stream
->key
);
4544 stream
->rotate_position
= -1ULL;
4545 stream
->rotate_ready
= false;
4549 * Perform the rotation a local stream file.
4551 static int rotate_local_stream(struct lttng_consumer_stream
*stream
)
4555 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4558 stream
->tracefile_size_current
= 0;
4559 stream
->tracefile_count_current
= 0;
4561 if (stream
->out_fd
>= 0) {
4562 ret
= close(stream
->out_fd
);
4564 PERROR("Failed to close stream out_fd of channel \"%s\"",
4565 stream
->chan
->name
);
4567 stream
->out_fd
= -1;
4570 if (stream
->index_file
) {
4571 lttng_index_file_put(stream
->index_file
);
4572 stream
->index_file
= nullptr;
4575 if (!stream
->trace_chunk
) {
4579 ret
= consumer_stream_create_output_files(stream
, true);
4585 * Performs the stream rotation for the rotate session feature if needed.
4586 * It must be called with the channel and stream locks held.
4588 * Return 0 on success, a negative number of error.
4590 int lttng_consumer_rotate_stream(struct lttng_consumer_stream
*stream
)
4594 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4597 * Update the stream's 'current' chunk to the session's (channel)
4598 * now-current chunk.
4600 lttng_trace_chunk_put(stream
->trace_chunk
);
4601 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4603 * A channel can be rotated and not have a "next" chunk
4604 * to transition to. In that case, the channel's "current chunk"
4605 * has not been closed yet, but it has not been updated to
4606 * a "next" trace chunk either. Hence, the stream, like its
4607 * parent channel, becomes part of no chunk and can't output
4608 * anything until a new trace chunk is created.
4610 stream
->trace_chunk
= nullptr;
4611 } else if (stream
->chan
->trace_chunk
&& !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4612 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4617 * Update the stream's trace chunk to its parent channel's
4618 * current trace chunk.
4620 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4623 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4624 ret
= rotate_local_stream(stream
);
4626 ERR("Failed to rotate stream, ret = %i", ret
);
4631 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4633 * If the stream has transitioned to a new trace
4634 * chunk, the metadata should be re-dumped to the
4637 * However, it is possible for a stream to transition to
4638 * a "no-chunk" state. This can happen if a rotation
4639 * occurs on an inactive session. In such cases, the metadata
4640 * regeneration will happen when the next trace chunk is
4643 ret
= consumer_metadata_stream_dump(stream
);
4648 lttng_consumer_reset_stream_rotate_state(stream
);
4657 * Rotate all the ready streams now.
4659 * This is especially important for low throughput streams that have already
4660 * been consumed, we cannot wait for their next packet to perform the
4662 * Need to be called with RCU read-side lock held to ensure existence of
4665 * Returns 0 on success, < 0 on error
4667 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
, uint64_t key
)
4670 struct lttng_consumer_stream
*stream
;
4671 struct lttng_ht_iter iter
;
4672 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
4674 ASSERT_RCU_READ_LOCKED();
4678 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4680 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4681 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4686 node_channel_id
.node
)
4688 health_code_update();
4690 pthread_mutex_lock(&stream
->chan
->lock
);
4691 pthread_mutex_lock(&stream
->lock
);
4693 if (!stream
->rotate_ready
) {
4694 pthread_mutex_unlock(&stream
->lock
);
4695 pthread_mutex_unlock(&stream
->chan
->lock
);
4698 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4700 ret
= lttng_consumer_rotate_stream(stream
);
4701 pthread_mutex_unlock(&stream
->lock
);
4702 pthread_mutex_unlock(&stream
->chan
->lock
);
4715 enum lttcomm_return_code
lttng_consumer_init_command(struct lttng_consumer_local_data
*ctx
,
4716 const lttng_uuid
& sessiond_uuid
)
4718 enum lttcomm_return_code ret
;
4719 char uuid_str
[LTTNG_UUID_STR_LEN
];
4721 if (ctx
->sessiond_uuid
.is_set
) {
4722 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4726 ctx
->sessiond_uuid
.is_set
= true;
4727 ctx
->sessiond_uuid
.value
= sessiond_uuid
;
4728 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4729 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4730 DBG("Received session daemon UUID: %s", uuid_str
);
4735 enum lttcomm_return_code
4736 lttng_consumer_create_trace_chunk(const uint64_t *relayd_id
,
4737 uint64_t session_id
,
4739 time_t chunk_creation_timestamp
,
4740 const char *chunk_override_name
,
4741 const struct lttng_credentials
*credentials
,
4742 struct lttng_directory_handle
*chunk_directory_handle
)
4745 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4746 struct lttng_trace_chunk
*created_chunk
= nullptr, *published_chunk
= nullptr;
4747 enum lttng_trace_chunk_status chunk_status
;
4748 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4749 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4750 const char *relayd_id_str
= "(none)";
4751 const char *creation_timestamp_str
;
4752 struct lttng_ht_iter iter
;
4753 struct lttng_consumer_channel
*channel
;
4756 /* Only used for logging purposes. */
4757 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
), "%" PRIu64
, *relayd_id
);
4758 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4759 relayd_id_str
= relayd_id_buffer
;
4761 relayd_id_str
= "(formatting error)";
4765 /* Local protocol error. */
4766 LTTNG_ASSERT(chunk_creation_timestamp
);
4767 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4768 creation_timestamp_buffer
,
4769 sizeof(creation_timestamp_buffer
));
4770 creation_timestamp_str
= !ret
? creation_timestamp_buffer
: "(formatting error)";
4772 DBG("Consumer create trace chunk command: relay_id = %s"
4773 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
", chunk_override_name = %s"
4774 ", chunk_creation_timestamp = %s",
4778 chunk_override_name
?: "(none)",
4779 creation_timestamp_str
);
4782 * The trace chunk registry, as used by the consumer daemon, implicitly
4783 * owns the trace chunks. This is only needed in the consumer since
4784 * the consumer has no notion of a session beyond session IDs being
4785 * used to identify other objects.
4787 * The lttng_trace_chunk_registry_publish() call below provides a
4788 * reference which is not released; it implicitly becomes the session
4789 * daemon's reference to the chunk in the consumer daemon.
4791 * The lifetime of trace chunks in the consumer daemon is managed by
4792 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4793 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4795 created_chunk
= lttng_trace_chunk_create(chunk_id
, chunk_creation_timestamp
, nullptr);
4796 if (!created_chunk
) {
4797 ERR("Failed to create trace chunk");
4798 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4802 if (chunk_override_name
) {
4803 chunk_status
= lttng_trace_chunk_override_name(created_chunk
, chunk_override_name
);
4804 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4805 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4810 if (chunk_directory_handle
) {
4811 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
, credentials
);
4812 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4813 ERR("Failed to set trace chunk credentials");
4814 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4818 * The consumer daemon has no ownership of the chunk output
4821 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
, chunk_directory_handle
);
4822 chunk_directory_handle
= nullptr;
4823 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4824 ERR("Failed to set trace chunk's directory handle");
4825 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4830 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4831 the_consumer_data
.chunk_registry
, session_id
, created_chunk
);
4832 lttng_trace_chunk_put(created_chunk
);
4833 created_chunk
= nullptr;
4834 if (!published_chunk
) {
4835 ERR("Failed to publish trace chunk");
4836 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4841 cds_lfht_for_each_entry_duplicate(
4842 the_consumer_data
.channels_by_session_id_ht
->ht
,
4843 the_consumer_data
.channels_by_session_id_ht
->hash_fct(&session_id
, lttng_ht_seed
),
4844 the_consumer_data
.channels_by_session_id_ht
->match_fct
,
4848 channels_by_session_id_ht_node
.node
)
4850 ret
= lttng_consumer_channel_set_trace_chunk(channel
, published_chunk
);
4853 * Roll-back the creation of this chunk.
4855 * This is important since the session daemon will
4856 * assume that the creation of this chunk failed and
4857 * will never ask for it to be closed, resulting
4858 * in a leak and an inconsistent state for some
4861 enum lttcomm_return_code close_ret
;
4862 char path
[LTTNG_PATH_MAX
];
4864 DBG("Failed to set new trace chunk on existing channels, rolling back");
4865 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4868 chunk_creation_timestamp
,
4871 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4872 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
4873 ", chunk_id = %" PRIu64
,
4878 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4884 struct consumer_relayd_sock_pair
*relayd
;
4886 relayd
= consumer_find_relayd(*relayd_id
);
4888 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4889 ret
= relayd_create_trace_chunk(&relayd
->control_sock
, published_chunk
);
4890 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4892 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4895 if (!relayd
|| ret
) {
4896 enum lttcomm_return_code close_ret
;
4897 char path
[LTTNG_PATH_MAX
];
4899 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4902 chunk_creation_timestamp
,
4905 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4906 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
4907 ", chunk_id = %" PRIu64
,
4912 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4919 /* Release the reference returned by the "publish" operation. */
4920 lttng_trace_chunk_put(published_chunk
);
4921 lttng_trace_chunk_put(created_chunk
);
4925 enum lttcomm_return_code
4926 lttng_consumer_close_trace_chunk(const uint64_t *relayd_id
,
4927 uint64_t session_id
,
4929 time_t chunk_close_timestamp
,
4930 const enum lttng_trace_chunk_command_type
*close_command
,
4933 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4934 struct lttng_trace_chunk
*chunk
;
4935 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4936 const char *relayd_id_str
= "(none)";
4937 const char *close_command_name
= "none";
4938 struct lttng_ht_iter iter
;
4939 struct lttng_consumer_channel
*channel
;
4940 enum lttng_trace_chunk_status chunk_status
;
4945 /* Only used for logging purposes. */
4946 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
), "%" PRIu64
, *relayd_id
);
4947 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4948 relayd_id_str
= relayd_id_buffer
;
4950 relayd_id_str
= "(formatting error)";
4953 if (close_command
) {
4954 close_command_name
= lttng_trace_chunk_command_type_get_name(*close_command
);
4957 DBG("Consumer close trace chunk command: relayd_id = %s"
4958 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
", close command = %s",
4962 close_command_name
);
4964 chunk
= lttng_trace_chunk_registry_find_chunk(
4965 the_consumer_data
.chunk_registry
, session_id
, chunk_id
);
4967 ERR("Failed to find chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4970 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4974 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
, chunk_close_timestamp
);
4975 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4976 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4980 if (close_command
) {
4981 chunk_status
= lttng_trace_chunk_set_close_command(chunk
, *close_command
);
4982 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4983 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4989 * chunk is now invalid to access as we no longer hold a reference to
4990 * it; it is only kept around to compare it (by address) to the
4991 * current chunk found in the session's channels.
4994 cds_lfht_for_each_entry (the_consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
, node
.node
) {
4998 * Only change the channel's chunk to NULL if it still
4999 * references the chunk being closed. The channel may
5000 * reference a newer channel in the case of a session
5001 * rotation. When a session rotation occurs, the "next"
5002 * chunk is created before the "current" chunk is closed.
5004 if (channel
->trace_chunk
!= chunk
) {
5007 ret
= lttng_consumer_channel_set_trace_chunk(channel
, nullptr);
5010 * Attempt to close the chunk on as many channels as
5013 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5019 struct consumer_relayd_sock_pair
*relayd
;
5021 relayd
= consumer_find_relayd(*relayd_id
);
5023 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5024 ret
= relayd_close_trace_chunk(&relayd
->control_sock
, chunk
, path
);
5025 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5027 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
5030 if (!relayd
|| ret
) {
5031 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5039 * Release the reference returned by the "find" operation and
5040 * the session daemon's implicit reference to the chunk.
5042 lttng_trace_chunk_put(chunk
);
5043 lttng_trace_chunk_put(chunk
);
5048 enum lttcomm_return_code
5049 lttng_consumer_trace_chunk_exists(const uint64_t *relayd_id
, uint64_t session_id
, uint64_t chunk_id
)
5052 enum lttcomm_return_code ret_code
;
5053 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
5054 const char *relayd_id_str
= "(none)";
5055 const bool is_local_trace
= !relayd_id
;
5056 struct consumer_relayd_sock_pair
*relayd
= nullptr;
5057 bool chunk_exists_local
, chunk_exists_remote
;
5060 /* Only used for logging purposes. */
5061 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
), "%" PRIu64
, *relayd_id
);
5062 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
5063 relayd_id_str
= relayd_id_buffer
;
5065 relayd_id_str
= "(formatting error)";
5069 DBG("Consumer trace chunk exists command: relayd_id = %s"
5070 ", chunk_id = %" PRIu64
,
5073 ret
= lttng_trace_chunk_registry_chunk_exists(
5074 the_consumer_data
.chunk_registry
, session_id
, chunk_id
, &chunk_exists_local
);
5076 /* Internal error. */
5077 ERR("Failed to query the existence of a trace chunk");
5078 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
5081 DBG("Trace chunk %s locally", chunk_exists_local
? "exists" : "does not exist");
5082 if (chunk_exists_local
) {
5083 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
5085 } else if (is_local_trace
) {
5086 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5091 relayd
= consumer_find_relayd(*relayd_id
);
5093 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
5094 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5095 goto end_rcu_unlock
;
5097 DBG("Looking up existence of trace chunk on relay daemon");
5098 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5099 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
, &chunk_exists_remote
);
5100 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5102 ERR("Failed to look-up the existence of trace chunk on relay daemon");
5103 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
5104 goto end_rcu_unlock
;
5107 ret_code
= chunk_exists_remote
? LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
5108 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5109 DBG("Trace chunk %s on relay daemon", chunk_exists_remote
? "exists" : "does not exist");
5117 static int consumer_clear_monitored_channel(struct lttng_consumer_channel
*channel
)
5119 struct lttng_ht
*ht
;
5120 struct lttng_consumer_stream
*stream
;
5121 struct lttng_ht_iter iter
;
5124 ht
= the_consumer_data
.stream_per_chan_id_ht
;
5127 cds_lfht_for_each_entry_duplicate(ht
->ht
,
5128 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
5133 node_channel_id
.node
)
5136 * Protect against teardown with mutex.
5138 pthread_mutex_lock(&stream
->lock
);
5139 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5142 ret
= consumer_clear_stream(stream
);
5147 pthread_mutex_unlock(&stream
->lock
);
5150 return LTTCOMM_CONSUMERD_SUCCESS
;
5153 pthread_mutex_unlock(&stream
->lock
);
5158 int lttng_consumer_clear_channel(struct lttng_consumer_channel
*channel
)
5162 DBG("Consumer clear channel %" PRIu64
, channel
->key
);
5164 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
5166 * Nothing to do for the metadata channel/stream.
5167 * Snapshot mechanism already take care of the metadata
5168 * handling/generation, and monitored channels only need to
5169 * have their data stream cleared..
5171 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5175 if (!channel
->monitor
) {
5176 ret
= consumer_clear_unmonitored_channel(channel
);
5178 ret
= consumer_clear_monitored_channel(channel
);
5184 enum lttcomm_return_code
lttng_consumer_open_channel_packets(struct lttng_consumer_channel
*channel
)
5186 struct lttng_consumer_stream
*stream
;
5187 enum lttcomm_return_code ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5189 if (channel
->metadata_stream
) {
5190 ERR("Open channel packets command attempted on a metadata channel");
5191 ret
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5196 cds_list_for_each_entry (stream
, &channel
->streams
.head
, send_node
) {
5197 enum consumer_stream_open_packet_status status
;
5199 pthread_mutex_lock(&stream
->lock
);
5200 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5204 status
= consumer_stream_open_packet(stream
);
5206 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
5207 DBG("Opened a packet in \"open channel packets\" command: stream id = %" PRIu64
5208 ", channel name = %s, session id = %" PRIu64
,
5211 stream
->chan
->session_id
);
5212 stream
->opened_packet_in_current_trace_chunk
= true;
5214 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
5215 DBG("No space left to open a packet in \"open channel packets\" command: stream id = %" PRIu64
5216 ", channel name = %s, session id = %" PRIu64
,
5219 stream
->chan
->session_id
);
5221 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
5223 * Only unexpected internal errors can lead to this
5224 * failing. Report an unknown error.
5226 ERR("Failed to flush empty buffer in \"open channel packets\" command: stream id = %" PRIu64
5227 ", channel id = %" PRIu64
", channel name = %s"
5228 ", session id = %" PRIu64
,
5232 channel
->session_id
);
5233 ret
= LTTCOMM_CONSUMERD_UNKNOWN_ERROR
;
5240 pthread_mutex_unlock(&stream
->lock
);
5249 pthread_mutex_unlock(&stream
->lock
);
5250 goto end_rcu_unlock
;
5253 void lttng_consumer_sigbus_handle(void *addr
)
5255 lttng_ustconsumer_sigbus_handle(addr
);