2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
11 #include <common/align.hpp>
12 #include <common/common.hpp>
13 #include <common/compat/endian.hpp>
14 #include <common/compat/poll.hpp>
15 #include <common/consumer/consumer-metadata-cache.hpp>
16 #include <common/consumer/consumer-stream.hpp>
17 #include <common/consumer/consumer-testpoint.hpp>
18 #include <common/consumer/consumer-timer.hpp>
19 #include <common/consumer/consumer.hpp>
20 #include <common/dynamic-array.hpp>
21 #include <common/index/ctf-index.hpp>
22 #include <common/index/index.hpp>
23 #include <common/io-hint.hpp>
24 #include <common/kernel-consumer/kernel-consumer.hpp>
25 #include <common/kernel-ctl/kernel-ctl.hpp>
26 #include <common/relayd/relayd.hpp>
27 #include <common/sessiond-comm/relayd.hpp>
28 #include <common/sessiond-comm/sessiond-comm.hpp>
29 #include <common/string-utils/format.hpp>
30 #include <common/time.hpp>
31 #include <common/trace-chunk-registry.hpp>
32 #include <common/trace-chunk.hpp>
33 #include <common/urcu.hpp>
34 #include <common/ust-consumer/ust-consumer.hpp>
35 #include <common/utils.hpp>
37 #include <bin/lttng-consumerd/health-consumerd.hpp>
46 #include <sys/socket.h>
47 #include <sys/types.h>
50 lttng_consumer_global_data the_consumer_data
;
52 enum consumer_channel_action
{
55 CONSUMER_CHANNEL_QUIT
,
59 struct consumer_channel_msg
{
60 enum consumer_channel_action action
;
61 struct lttng_consumer_channel
*chan
; /* add */
62 uint64_t key
; /* del */
66 * Global hash table containing respectively metadata and data streams. The
67 * stream element in this ht should only be updated by the metadata poll thread
68 * for the metadata and the data poll thread for the data.
70 struct lttng_ht
*metadata_ht
;
71 struct lttng_ht
*data_ht
;
74 /* Flag used to temporarily pause data consumption from testpoints. */
75 int data_consumption_paused
;
78 * Flag to inform the polling thread to quit when all fd hung up. Updated by
79 * the consumer_thread_receive_fds when it notices that all fds has hung up.
80 * Also updated by the signal handler (consumer_should_exit()). Read by the
85 static const char *get_consumer_domain()
87 switch (the_consumer_data
.type
) {
88 case LTTNG_CONSUMER_KERNEL
:
89 return DEFAULT_KERNEL_TRACE_DIR
;
90 case LTTNG_CONSUMER64_UST
:
92 case LTTNG_CONSUMER32_UST
:
93 return DEFAULT_UST_TRACE_DIR
;
100 * Notify a thread lttng pipe to poll back again. This usually means that some
101 * global state has changed so we just send back the thread in a poll wait
104 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
106 struct lttng_consumer_stream
*null_stream
= nullptr;
110 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
)); /* NOLINT sizeof used on a
114 static void notify_health_quit_pipe(int *pipe
)
118 ret
= lttng_write(pipe
[1], "4", 1);
120 PERROR("write consumer health quit");
124 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
125 struct lttng_consumer_channel
*chan
,
127 enum consumer_channel_action action
)
129 struct consumer_channel_msg msg
;
132 memset(&msg
, 0, sizeof(msg
));
137 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
138 if (ret
< sizeof(msg
)) {
139 PERROR("notify_channel_pipe write error");
143 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
145 notify_channel_pipe(ctx
, nullptr, key
, CONSUMER_CHANNEL_DEL
);
148 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
149 struct lttng_consumer_channel
**chan
,
151 enum consumer_channel_action
*action
)
153 struct consumer_channel_msg msg
;
156 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
157 if (ret
< sizeof(msg
)) {
161 *action
= msg
.action
;
169 * Cleanup the stream list of a channel. Those streams are not yet globally
172 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
174 struct lttng_consumer_stream
*stream
, *stmp
;
176 LTTNG_ASSERT(channel
);
178 /* Delete streams that might have been left in the stream list. */
179 cds_list_for_each_entry_safe (stream
, stmp
, &channel
->streams
.head
, send_node
) {
181 * Once a stream is added to this list, the buffers were created so we
182 * have a guarantee that this call will succeed. Setting the monitor
183 * mode to 0 so we don't lock nor try to delete the stream from the
187 consumer_stream_destroy(stream
, nullptr);
192 * Find a stream. The consumer_data.lock must be locked during this
195 static struct lttng_consumer_stream
*find_stream(uint64_t key
, struct lttng_ht
*ht
)
197 struct lttng_ht_iter iter
;
198 struct lttng_ht_node_u64
*node
;
199 struct lttng_consumer_stream
*stream
= nullptr;
203 /* -1ULL keys are lookup failures */
204 if (key
== (uint64_t) -1ULL) {
208 lttng::urcu::read_lock_guard read_lock
;
210 lttng_ht_lookup(ht
, &key
, &iter
);
211 node
= lttng_ht_iter_get_node_u64(&iter
);
212 if (node
!= nullptr) {
213 stream
= lttng::utils::container_of(node
, <tng_consumer_stream::node
);
219 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
221 struct lttng_consumer_stream
*stream
;
223 lttng::urcu::read_lock_guard read_lock
;
224 stream
= find_stream(key
, ht
);
226 stream
->key
= (uint64_t) -1ULL;
228 * We don't want the lookup to match, but we still need
229 * to iterate on this stream when iterating over the hash table. Just
230 * change the node key.
232 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
;
276 lttng::urcu::read_lock_guard read_lock
;
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;
289 static void free_channel_rcu(struct rcu_head
*head
)
291 struct lttng_ht_node_u64
*node
= lttng::utils::container_of(head
, <tng_ht_node_u64::head
);
292 struct lttng_consumer_channel
*channel
=
293 lttng::utils::container_of(node
, <tng_consumer_channel::node
);
295 switch (the_consumer_data
.type
) {
296 case LTTNG_CONSUMER_KERNEL
:
298 case LTTNG_CONSUMER32_UST
:
299 case LTTNG_CONSUMER64_UST
:
300 lttng_ustconsumer_free_channel(channel
);
303 ERR("Unknown consumer_data type");
310 * RCU protected relayd socket pair free.
312 static void free_relayd_rcu(struct rcu_head
*head
)
314 struct lttng_ht_node_u64
*node
= lttng::utils::container_of(head
, <tng_ht_node_u64::head
);
315 struct consumer_relayd_sock_pair
*relayd
=
316 lttng::utils::container_of(node
, &consumer_relayd_sock_pair::node
);
319 * Close all sockets. This is done in the call RCU since we don't want the
320 * socket fds to be reassigned thus potentially creating bad state of the
323 * We do not have to lock the control socket mutex here since at this stage
324 * there is no one referencing to this relayd object.
326 (void) relayd_close(&relayd
->control_sock
);
327 (void) relayd_close(&relayd
->data_sock
);
329 pthread_mutex_destroy(&relayd
->ctrl_sock_mutex
);
334 * Destroy and free relayd socket pair object.
336 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
339 struct lttng_ht_iter iter
;
341 if (relayd
== nullptr) {
345 DBG("Consumer destroy and close relayd socket pair");
347 iter
.iter
.node
= &relayd
->node
.node
;
348 ret
= lttng_ht_del(the_consumer_data
.relayd_ht
, &iter
);
350 /* We assume the relayd is being or is destroyed */
354 /* RCU free() call */
355 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
359 * Remove a channel from the global list protected by a mutex. This function is
360 * also responsible for freeing its data structures.
362 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
364 struct lttng_ht_iter iter
;
366 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
368 pthread_mutex_lock(&the_consumer_data
.lock
);
369 pthread_mutex_lock(&channel
->lock
);
371 /* Destroy streams that might have been left in the stream list. */
372 clean_channel_stream_list(channel
);
374 if (channel
->live_timer_enabled
== 1) {
375 consumer_timer_live_stop(channel
);
377 if (channel
->monitor_timer_enabled
== 1) {
378 consumer_timer_monitor_stop(channel
);
382 * Send a last buffer statistics sample to the session daemon
383 * to ensure it tracks the amount of data consumed by this channel.
385 sample_and_send_channel_buffer_stats(channel
);
387 switch (the_consumer_data
.type
) {
388 case LTTNG_CONSUMER_KERNEL
:
390 case LTTNG_CONSUMER32_UST
:
391 case LTTNG_CONSUMER64_UST
:
392 lttng_ustconsumer_del_channel(channel
);
395 ERR("Unknown consumer_data type");
400 lttng_trace_chunk_put(channel
->trace_chunk
);
401 channel
->trace_chunk
= nullptr;
403 if (channel
->is_published
) {
406 lttng::urcu::read_lock_guard read_lock
;
407 iter
.iter
.node
= &channel
->node
.node
;
408 ret
= lttng_ht_del(the_consumer_data
.channel_ht
, &iter
);
411 iter
.iter
.node
= &channel
->channels_by_session_id_ht_node
.node
;
412 ret
= lttng_ht_del(the_consumer_data
.channels_by_session_id_ht
, &iter
);
416 channel
->is_deleted
= true;
417 call_rcu(&channel
->node
.head
, free_channel_rcu
);
419 pthread_mutex_unlock(&channel
->lock
);
420 pthread_mutex_unlock(&the_consumer_data
.lock
);
424 * Iterate over the relayd hash table and destroy each element. Finally,
425 * destroy the whole hash table.
427 static void cleanup_relayd_ht()
429 struct lttng_ht_iter iter
;
430 struct consumer_relayd_sock_pair
*relayd
;
433 lttng::urcu::read_lock_guard read_lock
;
435 cds_lfht_for_each_entry (
436 the_consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
, node
.node
) {
437 consumer_destroy_relayd(relayd
);
441 lttng_ht_destroy(the_consumer_data
.relayd_ht
);
445 * Update the end point status of all streams having the given network sequence
446 * index (relayd index).
448 * It's atomically set without having the stream mutex locked which is fine
449 * because we handle the write/read race with a pipe wakeup for each thread.
451 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
452 enum consumer_endpoint_status status
)
454 struct lttng_ht_iter iter
;
455 struct lttng_consumer_stream
*stream
;
457 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
459 lttng::urcu::read_lock_guard read_lock
;
461 /* Let's begin with metadata */
462 cds_lfht_for_each_entry (metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
463 if (stream
->net_seq_idx
== net_seq_idx
) {
464 uatomic_set(&stream
->endpoint_status
, status
);
465 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
469 /* Follow up by the data streams */
470 cds_lfht_for_each_entry (data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
471 if (stream
->net_seq_idx
== net_seq_idx
) {
472 uatomic_set(&stream
->endpoint_status
, status
);
473 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
479 * Cleanup a relayd object by flagging every associated streams for deletion,
480 * destroying the object meaning removing it from the relayd hash table,
481 * closing the sockets and freeing the memory in a RCU call.
483 * If a local data context is available, notify the threads that the streams'
484 * state have changed.
486 void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
)
490 LTTNG_ASSERT(relayd
);
492 DBG("Cleaning up relayd object ID %" PRIu64
, relayd
->net_seq_idx
);
494 /* Save the net sequence index before destroying the object */
495 netidx
= relayd
->net_seq_idx
;
498 * Delete the relayd from the relayd hash table, close the sockets and free
499 * the object in a RCU call.
501 consumer_destroy_relayd(relayd
);
503 /* Set inactive endpoint to all streams */
504 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
507 * With a local data context, notify the threads that the streams' state
508 * have changed. The write() action on the pipe acts as an "implicit"
509 * memory barrier ordering the updates of the end point status from the
510 * read of this status which happens AFTER receiving this notify.
512 notify_thread_lttng_pipe(relayd
->ctx
->consumer_data_pipe
);
513 notify_thread_lttng_pipe(relayd
->ctx
->consumer_metadata_pipe
);
517 * Flag a relayd socket pair for destruction. Destroy it if the refcount
520 * RCU read side lock MUST be aquired before calling this function.
522 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
524 LTTNG_ASSERT(relayd
);
525 ASSERT_RCU_READ_LOCKED();
527 /* Set destroy flag for this object */
528 uatomic_set(&relayd
->destroy_flag
, 1);
530 /* Destroy the relayd if refcount is 0 */
531 if (uatomic_read(&relayd
->refcount
) == 0) {
532 consumer_destroy_relayd(relayd
);
537 * Completly destroy stream from every visiable data structure and the given
540 * One this call returns, the stream object is not longer usable nor visible.
542 void consumer_del_stream(struct lttng_consumer_stream
*stream
, struct lttng_ht
*ht
)
544 consumer_stream_destroy(stream
, ht
);
548 * XXX naming of del vs destroy is all mixed up.
550 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
552 consumer_stream_destroy(stream
, data_ht
);
555 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
557 consumer_stream_destroy(stream
, metadata_ht
);
560 void consumer_stream_update_channel_attributes(struct lttng_consumer_stream
*stream
,
561 struct lttng_consumer_channel
*channel
)
563 stream
->channel_read_only_attributes
.tracefile_size
= channel
->tracefile_size
;
567 * Add a stream to the global list protected by a mutex.
569 void consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
571 struct lttng_ht
*ht
= data_ht
;
573 LTTNG_ASSERT(stream
);
576 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
578 pthread_mutex_lock(&the_consumer_data
.lock
);
579 pthread_mutex_lock(&stream
->chan
->lock
);
580 pthread_mutex_lock(&stream
->chan
->timer_lock
);
581 pthread_mutex_lock(&stream
->lock
);
582 lttng::urcu::read_lock_guard read_lock
;
584 /* Steal stream identifier to avoid having streams with the same key */
585 steal_stream_key(stream
->key
, ht
);
587 lttng_ht_add_unique_u64(ht
, &stream
->node
);
589 lttng_ht_add_u64(the_consumer_data
.stream_per_chan_id_ht
, &stream
->node_channel_id
);
592 * Add stream to the stream_list_ht of the consumer data. No need to steal
593 * the key since the HT does not use it and we allow to add redundant keys
596 lttng_ht_add_u64(the_consumer_data
.stream_list_ht
, &stream
->node_session_id
);
599 * When nb_init_stream_left reaches 0, we don't need to trigger any action
600 * in terms of destroying the associated channel, because the action that
601 * causes the count to become 0 also causes a stream to be added. The
602 * channel deletion will thus be triggered by the following removal of this
605 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
606 /* Increment refcount before decrementing nb_init_stream_left */
608 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
611 /* Update consumer data once the node is inserted. */
612 the_consumer_data
.stream_count
++;
613 the_consumer_data
.need_update
= 1;
615 pthread_mutex_unlock(&stream
->lock
);
616 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
617 pthread_mutex_unlock(&stream
->chan
->lock
);
618 pthread_mutex_unlock(&the_consumer_data
.lock
);
622 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
623 * be acquired before calling this.
625 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
628 struct lttng_ht_node_u64
*node
;
629 struct lttng_ht_iter iter
;
631 LTTNG_ASSERT(relayd
);
632 ASSERT_RCU_READ_LOCKED();
634 lttng_ht_lookup(the_consumer_data
.relayd_ht
, &relayd
->net_seq_idx
, &iter
);
635 node
= lttng_ht_iter_get_node_u64(&iter
);
636 if (node
!= nullptr) {
639 lttng_ht_add_unique_u64(the_consumer_data
.relayd_ht
, &relayd
->node
);
646 * Allocate and return a consumer relayd socket.
648 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(uint64_t net_seq_idx
)
650 struct consumer_relayd_sock_pair
*obj
= nullptr;
652 /* net sequence index of -1 is a failure */
653 if (net_seq_idx
== (uint64_t) -1ULL) {
657 obj
= zmalloc
<consumer_relayd_sock_pair
>();
658 if (obj
== nullptr) {
659 PERROR("zmalloc relayd sock");
663 obj
->net_seq_idx
= net_seq_idx
;
665 obj
->destroy_flag
= 0;
666 obj
->control_sock
.sock
.fd
= -1;
667 obj
->data_sock
.sock
.fd
= -1;
668 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
669 pthread_mutex_init(&obj
->ctrl_sock_mutex
, nullptr);
676 * Find a relayd socket pair in the global consumer data.
678 * Return the object if found else NULL.
679 * RCU read-side lock must be held across this call and while using the
682 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
684 struct lttng_ht_iter iter
;
685 struct lttng_ht_node_u64
*node
;
686 struct consumer_relayd_sock_pair
*relayd
= nullptr;
688 ASSERT_RCU_READ_LOCKED();
690 /* Negative keys are lookup failures */
691 if (key
== (uint64_t) -1ULL) {
695 lttng_ht_lookup(the_consumer_data
.relayd_ht
, &key
, &iter
);
696 node
= lttng_ht_iter_get_node_u64(&iter
);
697 if (node
!= nullptr) {
698 relayd
= lttng::utils::container_of(node
, &consumer_relayd_sock_pair::node
);
706 * Find a relayd and send the stream
708 * Returns 0 on success, < 0 on error
710 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
, char *path
)
713 struct consumer_relayd_sock_pair
*relayd
;
715 LTTNG_ASSERT(stream
);
716 LTTNG_ASSERT(stream
->net_seq_idx
!= -1ULL);
719 /* The stream is not metadata. Get relayd reference if exists. */
720 lttng::urcu::read_lock_guard read_lock
;
721 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
722 if (relayd
!= nullptr) {
723 /* Add stream on the relayd */
724 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
725 ret
= relayd_add_stream(&relayd
->control_sock
,
727 get_consumer_domain(),
729 &stream
->relayd_stream_id
,
730 stream
->chan
->tracefile_size
,
731 stream
->chan
->tracefile_count
,
732 stream
->trace_chunk
);
733 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
735 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".",
736 relayd
->net_seq_idx
);
737 lttng_consumer_cleanup_relayd(relayd
);
741 uatomic_inc(&relayd
->refcount
);
742 stream
->sent_to_relayd
= 1;
744 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
746 stream
->net_seq_idx
);
751 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
754 stream
->net_seq_idx
);
761 * Find a relayd and send the streams sent message
763 * Returns 0 on success, < 0 on error
765 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
768 struct consumer_relayd_sock_pair
*relayd
;
770 LTTNG_ASSERT(net_seq_idx
!= -1ULL);
772 /* The stream is not metadata. Get relayd reference if exists. */
773 lttng::urcu::read_lock_guard read_lock
;
774 relayd
= consumer_find_relayd(net_seq_idx
);
775 if (relayd
!= nullptr) {
776 /* Add stream on the relayd */
777 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
778 ret
= relayd_streams_sent(&relayd
->control_sock
);
779 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
781 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64
".",
782 relayd
->net_seq_idx
);
783 lttng_consumer_cleanup_relayd(relayd
);
787 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.", net_seq_idx
);
793 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
800 * Find a relayd and close the stream
802 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
804 struct consumer_relayd_sock_pair
*relayd
;
806 /* The stream is not metadata. Get relayd reference if exists. */
807 lttng::urcu::read_lock_guard read_lock
;
808 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
810 consumer_stream_relayd_close(stream
, relayd
);
815 * Handle stream for relayd transmission if the stream applies for network
816 * streaming where the net sequence index is set.
818 * Return destination file descriptor or negative value on error.
820 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
822 unsigned long padding
,
823 struct consumer_relayd_sock_pair
*relayd
)
826 struct lttcomm_relayd_data_hdr data_hdr
;
829 LTTNG_ASSERT(stream
);
830 LTTNG_ASSERT(relayd
);
832 /* Reset data header */
833 memset(&data_hdr
, 0, sizeof(data_hdr
));
835 if (stream
->metadata_flag
) {
836 /* Caller MUST acquire the relayd control socket lock */
837 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
842 /* Metadata are always sent on the control socket. */
843 outfd
= relayd
->control_sock
.sock
.fd
;
845 /* Set header with stream information */
846 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
847 data_hdr
.data_size
= htobe32(data_size
);
848 data_hdr
.padding_size
= htobe32(padding
);
851 * Note that net_seq_num below is assigned with the *current* value of
852 * next_net_seq_num and only after that the next_net_seq_num will be
853 * increment. This is why when issuing a command on the relayd using
854 * this next value, 1 should always be substracted in order to compare
855 * the last seen sequence number on the relayd side to the last sent.
857 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
858 /* Other fields are zeroed previously */
860 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
, sizeof(data_hdr
));
865 ++stream
->next_net_seq_num
;
867 /* Set to go on data socket */
868 outfd
= relayd
->data_sock
.sock
.fd
;
876 * Write a character on the metadata poll pipe to wake the metadata thread.
877 * Returns 0 on success, -1 on error.
879 int consumer_metadata_wakeup_pipe(const struct lttng_consumer_channel
*channel
)
883 DBG("Waking up metadata poll thread (writing to pipe): channel name = '%s'", channel
->name
);
884 if (channel
->monitor
&& channel
->metadata_stream
) {
885 const char dummy
= 'c';
886 const ssize_t write_ret
=
887 lttng_write(channel
->metadata_stream
->ust_metadata_poll_pipe
[1], &dummy
, 1);
890 if (errno
== EWOULDBLOCK
) {
892 * This is fine, the metadata poll thread
893 * is having a hard time keeping-up, but
894 * it will eventually wake-up and consume
895 * the available data.
899 PERROR("Failed to write to UST metadata pipe while attempting to wake-up the metadata poll thread");
911 * Trigger a dump of the metadata content. Following/during the succesful
912 * completion of this call, the metadata poll thread will start receiving
913 * metadata packets to consume.
915 * The caller must hold the channel and stream locks.
917 static int consumer_metadata_stream_dump(struct lttng_consumer_stream
*stream
)
921 ASSERT_LOCKED(stream
->chan
->lock
);
922 ASSERT_LOCKED(stream
->lock
);
923 LTTNG_ASSERT(stream
->metadata_flag
);
924 LTTNG_ASSERT(stream
->chan
->trace_chunk
);
926 switch (the_consumer_data
.type
) {
927 case LTTNG_CONSUMER_KERNEL
:
929 * Reset the position of what has been read from the
930 * metadata cache to 0 so we can dump it again.
932 ret
= kernctl_metadata_cache_dump(stream
->wait_fd
);
934 case LTTNG_CONSUMER32_UST
:
935 case LTTNG_CONSUMER64_UST
:
937 * Reset the position pushed from the metadata cache so it
938 * will write from the beginning on the next push.
940 stream
->ust_metadata_pushed
= 0;
941 ret
= consumer_metadata_wakeup_pipe(stream
->chan
);
944 ERR("Unknown consumer_data type");
948 ERR("Failed to dump the metadata cache");
953 static int lttng_consumer_channel_set_trace_chunk(struct lttng_consumer_channel
*channel
,
954 struct lttng_trace_chunk
*new_trace_chunk
)
956 pthread_mutex_lock(&channel
->lock
);
957 if (channel
->is_deleted
) {
959 * The channel has been logically deleted and should no longer
960 * be used. It has released its reference to its current trace
961 * chunk and should not acquire a new one.
963 * Return success as there is nothing for the caller to do.
969 * The acquisition of the reference cannot fail (barring
970 * a severe internal error) since a reference to the published
971 * chunk is already held by the caller.
973 if (new_trace_chunk
) {
974 const bool acquired_reference
= lttng_trace_chunk_get(new_trace_chunk
);
976 LTTNG_ASSERT(acquired_reference
);
979 lttng_trace_chunk_put(channel
->trace_chunk
);
980 channel
->trace_chunk
= new_trace_chunk
;
982 pthread_mutex_unlock(&channel
->lock
);
987 * Allocate and return a new lttng_consumer_channel object using the given key
988 * to initialize the hash table node.
990 * On error, return NULL.
992 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
994 const uint64_t *chunk_id
,
995 const char *pathname
,
998 enum lttng_event_output output
,
999 uint64_t tracefile_size
,
1000 uint64_t tracefile_count
,
1001 uint64_t session_id_per_pid
,
1002 unsigned int monitor
,
1003 unsigned int live_timer_interval
,
1004 bool is_in_live_session
,
1005 const char *root_shm_path
,
1006 const char *shm_path
)
1008 struct lttng_consumer_channel
*channel
= nullptr;
1009 struct lttng_trace_chunk
*trace_chunk
= nullptr;
1012 trace_chunk
= lttng_trace_chunk_registry_find_chunk(
1013 the_consumer_data
.chunk_registry
, session_id
, *chunk_id
);
1015 ERR("Failed to find trace chunk reference during creation of channel");
1020 channel
= zmalloc
<lttng_consumer_channel
>();
1021 if (channel
== nullptr) {
1022 PERROR("malloc struct lttng_consumer_channel");
1027 channel
->refcount
= 0;
1028 channel
->session_id
= session_id
;
1029 channel
->session_id_per_pid
= session_id_per_pid
;
1030 channel
->relayd_id
= relayd_id
;
1031 channel
->tracefile_size
= tracefile_size
;
1032 channel
->tracefile_count
= tracefile_count
;
1033 channel
->monitor
= monitor
;
1034 channel
->live_timer_interval
= live_timer_interval
;
1035 channel
->is_live
= is_in_live_session
;
1036 pthread_mutex_init(&channel
->lock
, nullptr);
1037 pthread_mutex_init(&channel
->timer_lock
, nullptr);
1040 case LTTNG_EVENT_SPLICE
:
1041 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
1043 case LTTNG_EVENT_MMAP
:
1044 channel
->output
= CONSUMER_CHANNEL_MMAP
;
1054 * In monitor mode, the streams associated with the channel will be put in
1055 * a special list ONLY owned by this channel. So, the refcount is set to 1
1056 * here meaning that the channel itself has streams that are referenced.
1058 * On a channel deletion, once the channel is no longer visible, the
1059 * refcount is decremented and checked for a zero value to delete it. With
1060 * streams in no monitor mode, it will now be safe to destroy the channel.
1062 if (!channel
->monitor
) {
1063 channel
->refcount
= 1;
1066 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1067 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1069 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1070 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1072 if (root_shm_path
) {
1073 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1074 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1077 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1078 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1081 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1082 lttng_ht_node_init_u64(&channel
->channels_by_session_id_ht_node
, channel
->session_id
);
1084 channel
->wait_fd
= -1;
1085 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1088 int ret
= lttng_consumer_channel_set_trace_chunk(channel
, trace_chunk
);
1094 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1097 lttng_trace_chunk_put(trace_chunk
);
1100 consumer_del_channel(channel
);
1106 * Add a channel to the global list protected by a mutex.
1108 * Always return 0 indicating success.
1110 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1111 struct lttng_consumer_local_data
*ctx
)
1113 pthread_mutex_lock(&the_consumer_data
.lock
);
1114 pthread_mutex_lock(&channel
->lock
);
1115 pthread_mutex_lock(&channel
->timer_lock
);
1118 * This gives us a guarantee that the channel we are about to add to the
1119 * channel hash table will be unique. See this function comment on the why
1120 * we need to steel the channel key at this stage.
1122 steal_channel_key(channel
->key
);
1124 lttng::urcu::read_lock_guard read_lock
;
1125 lttng_ht_add_unique_u64(the_consumer_data
.channel_ht
, &channel
->node
);
1126 lttng_ht_add_u64(the_consumer_data
.channels_by_session_id_ht
,
1127 &channel
->channels_by_session_id_ht_node
);
1128 channel
->is_published
= true;
1130 pthread_mutex_unlock(&channel
->timer_lock
);
1131 pthread_mutex_unlock(&channel
->lock
);
1132 pthread_mutex_unlock(&the_consumer_data
.lock
);
1134 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1135 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1142 * Allocate the pollfd structure and the local view of the out fds to avoid
1143 * doing a lookup in the linked list and concurrency issues when writing is
1144 * needed. Called with consumer_data.lock held.
1146 * Returns the number of fds in the structures.
1148 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1149 struct pollfd
**pollfd
,
1150 struct lttng_consumer_stream
**local_stream
,
1151 struct lttng_ht
*ht
,
1152 int *nb_inactive_fd
)
1155 struct lttng_ht_iter iter
;
1156 struct lttng_consumer_stream
*stream
;
1160 LTTNG_ASSERT(pollfd
);
1161 LTTNG_ASSERT(local_stream
);
1163 DBG("Updating poll fd array");
1164 *nb_inactive_fd
= 0;
1167 lttng::urcu::read_lock_guard read_lock
;
1168 cds_lfht_for_each_entry (ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1170 * Only active streams with an active end point can be added to the
1171 * poll set and local stream storage of the thread.
1173 * There is a potential race here for endpoint_status to be updated
1174 * just after the check. However, this is OK since the stream(s) will
1175 * be deleted once the thread is notified that the end point state has
1176 * changed where this function will be called back again.
1178 * We track the number of inactive FDs because they still need to be
1179 * closed by the polling thread after a wakeup on the data_pipe or
1182 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1183 (*nb_inactive_fd
)++;
1187 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1188 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1189 local_stream
[i
] = stream
;
1195 * Insert the consumer_data_pipe at the end of the array and don't
1196 * increment i so nb_fd is the number of real FD.
1198 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1199 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1201 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1202 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1207 * Poll on the should_quit pipe and the command socket return -1 on
1208 * error, 1 if should exit, 0 if data is available on the command socket
1210 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1215 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1216 if (num_rdy
== -1) {
1218 * Restart interrupted system call.
1220 if (errno
== EINTR
) {
1223 PERROR("Poll error");
1226 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1227 DBG("consumer_should_quit wake up");
1234 * Set the error socket.
1236 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
, int sock
)
1238 ctx
->consumer_error_socket
= sock
;
1242 * Set the command socket path.
1244 void lttng_consumer_set_command_sock_path(struct lttng_consumer_local_data
*ctx
, char *sock
)
1246 ctx
->consumer_command_sock_path
= sock
;
1250 * Send return code to the session daemon.
1251 * If the socket is not defined, we return 0, it is not a fatal error
1253 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1255 if (ctx
->consumer_error_socket
> 0) {
1256 return lttcomm_send_unix_sock(
1257 ctx
->consumer_error_socket
, &cmd
, sizeof(enum lttcomm_sessiond_command
));
1264 * Close all the tracefiles and stream fds and MUST be called when all
1265 * instances are destroyed i.e. when all threads were joined and are ended.
1267 void lttng_consumer_cleanup()
1269 struct lttng_ht_iter iter
;
1270 struct lttng_consumer_channel
*channel
;
1271 unsigned int trace_chunks_left
;
1274 lttng::urcu::read_lock_guard read_lock
;
1276 cds_lfht_for_each_entry (
1277 the_consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
, node
.node
) {
1278 consumer_del_channel(channel
);
1282 lttng_ht_destroy(the_consumer_data
.channel_ht
);
1283 lttng_ht_destroy(the_consumer_data
.channels_by_session_id_ht
);
1285 cleanup_relayd_ht();
1287 lttng_ht_destroy(the_consumer_data
.stream_per_chan_id_ht
);
1290 * This HT contains streams that are freed by either the metadata thread or
1291 * the data thread so we do *nothing* on the hash table and simply destroy
1294 lttng_ht_destroy(the_consumer_data
.stream_list_ht
);
1297 * Trace chunks in the registry may still exist if the session
1298 * daemon has encountered an internal error and could not
1299 * tear down its sessions and/or trace chunks properly.
1301 * Release the session daemon's implicit reference to any remaining
1302 * trace chunk and print an error if any trace chunk was found. Note
1303 * that there are _no_ legitimate cases for trace chunks to be left,
1304 * it is a leak. However, it can happen following a crash of the
1305 * session daemon and not emptying the registry would cause an assertion
1309 lttng_trace_chunk_registry_put_each_chunk(the_consumer_data
.chunk_registry
);
1310 if (trace_chunks_left
) {
1311 ERR("%u trace chunks are leaked by lttng-consumerd. "
1312 "This can be caused by an internal error of the session daemon.",
1315 /* Run all callbacks freeing each chunk. */
1317 lttng_trace_chunk_registry_destroy(the_consumer_data
.chunk_registry
);
1321 * Called from signal handler.
1323 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1327 CMM_STORE_SHARED(consumer_quit
, 1);
1328 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1330 PERROR("write consumer quit");
1333 DBG("Consumer flag that it should quit");
1337 * Flush pending writes to trace output disk file.
1339 static void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
, off_t orig_offset
)
1341 int outfd
= stream
->out_fd
;
1344 * This does a blocking write-and-wait on any page that belongs to the
1345 * subbuffer prior to the one we just wrote.
1346 * Don't care about error values, as these are just hints and ways to
1347 * limit the amount of page cache used.
1349 if (orig_offset
< stream
->max_sb_size
) {
1352 lttng::io::hint_flush_range_dont_need_sync(
1353 outfd
, orig_offset
- stream
->max_sb_size
, stream
->max_sb_size
);
1357 * Initialise the necessary environnement :
1358 * - create a new context
1359 * - create the poll_pipe
1360 * - create the should_quit pipe (for signal handler)
1361 * - create the thread pipe (for splice)
1363 * Takes a function pointer as argument, this function is called when data is
1364 * available on a buffer. This function is responsible to do the
1365 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1366 * buffer configuration and then kernctl_put_next_subbuf at the end.
1368 * Returns a pointer to the new context or NULL on error.
1370 struct lttng_consumer_local_data
*
1371 lttng_consumer_create(enum lttng_consumer_type type
,
1372 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1373 struct lttng_consumer_local_data
*ctx
,
1374 bool locked_by_caller
),
1375 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1376 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1377 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1380 struct lttng_consumer_local_data
*ctx
;
1382 LTTNG_ASSERT(the_consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1383 the_consumer_data
.type
== type
);
1384 the_consumer_data
.type
= type
;
1386 ctx
= zmalloc
<lttng_consumer_local_data
>();
1387 if (ctx
== nullptr) {
1388 PERROR("allocating context");
1392 ctx
->consumer_error_socket
= -1;
1393 ctx
->consumer_metadata_socket
= -1;
1394 pthread_mutex_init(&ctx
->metadata_socket_lock
, nullptr);
1395 /* assign the callbacks */
1396 ctx
->on_buffer_ready
= buffer_ready
;
1397 ctx
->on_recv_channel
= recv_channel
;
1398 ctx
->on_recv_stream
= recv_stream
;
1399 ctx
->on_update_stream
= update_stream
;
1401 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1402 if (!ctx
->consumer_data_pipe
) {
1403 goto error_poll_pipe
;
1406 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1407 if (!ctx
->consumer_wakeup_pipe
) {
1408 goto error_wakeup_pipe
;
1411 ret
= pipe(ctx
->consumer_should_quit
);
1413 PERROR("Error creating recv pipe");
1414 goto error_quit_pipe
;
1417 ret
= pipe(ctx
->consumer_channel_pipe
);
1419 PERROR("Error creating channel pipe");
1420 goto error_channel_pipe
;
1423 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1424 if (!ctx
->consumer_metadata_pipe
) {
1425 goto error_metadata_pipe
;
1428 ctx
->channel_monitor_pipe
= -1;
1432 error_metadata_pipe
:
1433 utils_close_pipe(ctx
->consumer_channel_pipe
);
1435 utils_close_pipe(ctx
->consumer_should_quit
);
1437 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1439 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1447 * Iterate over all streams of the hashtable and free them properly.
1449 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1451 struct lttng_ht_iter iter
;
1452 struct lttng_consumer_stream
*stream
;
1454 if (ht
== nullptr) {
1459 lttng::urcu::read_lock_guard read_lock
;
1460 cds_lfht_for_each_entry (ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1462 * Ignore return value since we are currently cleaning up so any error
1465 (void) consumer_del_stream(stream
, ht
);
1469 lttng_ht_destroy(ht
);
1473 * Iterate over all streams of the metadata hashtable and free them
1476 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1478 struct lttng_ht_iter iter
;
1479 struct lttng_consumer_stream
*stream
;
1481 if (ht
== nullptr) {
1486 lttng::urcu::read_lock_guard read_lock
;
1487 cds_lfht_for_each_entry (ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1489 * Ignore return value since we are currently cleaning up so any error
1492 (void) consumer_del_metadata_stream(stream
, ht
);
1496 lttng_ht_destroy(ht
);
1500 * Close all fds associated with the instance and free the context.
1502 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1506 DBG("Consumer destroying it. Closing everything.");
1512 destroy_data_stream_ht(data_ht
);
1513 destroy_metadata_stream_ht(metadata_ht
);
1515 ret
= close(ctx
->consumer_error_socket
);
1519 ret
= close(ctx
->consumer_metadata_socket
);
1523 utils_close_pipe(ctx
->consumer_channel_pipe
);
1524 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1525 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1526 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1527 utils_close_pipe(ctx
->consumer_should_quit
);
1529 unlink(ctx
->consumer_command_sock_path
);
1534 * Write the metadata stream id on the specified file descriptor.
1537 write_relayd_metadata_id(int fd
, struct lttng_consumer_stream
*stream
, unsigned long padding
)
1540 struct lttcomm_relayd_metadata_payload hdr
;
1542 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1543 hdr
.padding_size
= htobe32(padding
);
1544 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1545 if (ret
< sizeof(hdr
)) {
1547 * This error means that the fd's end is closed so ignore the PERROR
1548 * not to clubber the error output since this can happen in a normal
1551 if (errno
!= EPIPE
) {
1552 PERROR("write metadata stream id");
1554 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1556 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1557 * handle writting the missing part so report that as an error and
1558 * don't lie to the caller.
1563 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1564 stream
->relayd_stream_id
,
1572 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1573 * core function for writing trace buffers to either the local filesystem or
1576 * It must be called with the stream and the channel lock held.
1578 * Careful review MUST be put if any changes occur!
1580 * Returns the number of bytes written
1582 ssize_t
lttng_consumer_on_read_subbuffer_mmap(struct lttng_consumer_stream
*stream
,
1583 const struct lttng_buffer_view
*buffer
,
1584 unsigned long padding
)
1587 off_t orig_offset
= stream
->out_fd_offset
;
1588 /* Default is on the disk */
1589 int outfd
= stream
->out_fd
;
1590 struct consumer_relayd_sock_pair
*relayd
= nullptr;
1591 unsigned int relayd_hang_up
= 0;
1592 const size_t subbuf_content_size
= buffer
->size
- padding
;
1595 /* RCU lock for the relayd pointer */
1596 lttng::urcu::read_lock_guard read_lock
;
1597 LTTNG_ASSERT(stream
->net_seq_idx
!= (uint64_t) -1ULL || stream
->trace_chunk
);
1599 /* Flag that the current stream if set for network streaming. */
1600 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1601 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1602 if (relayd
== nullptr) {
1608 /* Handle stream on the relayd if the output is on the network */
1610 unsigned long netlen
= subbuf_content_size
;
1613 * Lock the control socket for the complete duration of the function
1614 * since from this point on we will use the socket.
1616 if (stream
->metadata_flag
) {
1617 /* Metadata requires the control socket. */
1618 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1619 if (stream
->reset_metadata_flag
) {
1620 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1621 stream
->relayd_stream_id
,
1622 stream
->metadata_version
);
1627 stream
->reset_metadata_flag
= 0;
1629 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1632 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1637 /* Use the returned socket. */
1640 /* Write metadata stream id before payload */
1641 if (stream
->metadata_flag
) {
1642 ret
= write_relayd_metadata_id(outfd
, stream
, padding
);
1649 write_len
= subbuf_content_size
;
1651 /* No streaming; we have to write the full padding. */
1652 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1653 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1655 ERR("Reset metadata file");
1658 stream
->reset_metadata_flag
= 0;
1662 * Check if we need to change the tracefile before writing the packet.
1664 if (stream
->chan
->tracefile_size
> 0 &&
1665 (stream
->tracefile_size_current
+ buffer
->size
) >
1666 stream
->chan
->tracefile_size
) {
1667 ret
= consumer_stream_rotate_output_files(stream
);
1671 outfd
= stream
->out_fd
;
1674 stream
->tracefile_size_current
+= buffer
->size
;
1675 write_len
= buffer
->size
;
1679 * This call guarantee that len or less is returned. It's impossible to
1680 * receive a ret value that is bigger than len.
1682 ret
= lttng_write(outfd
, buffer
->data
, write_len
);
1683 DBG("Consumer mmap write() ret %zd (len %zu)", ret
, write_len
);
1684 if (ret
< 0 || ((size_t) ret
!= write_len
)) {
1686 * Report error to caller if nothing was written else at least send the
1694 /* Socket operation failed. We consider the relayd dead */
1695 if (errno
== EPIPE
) {
1697 * This is possible if the fd is closed on the other side
1698 * (outfd) or any write problem. It can be verbose a bit for a
1699 * normal execution if for instance the relayd is stopped
1700 * abruptly. This can happen so set this to a DBG statement.
1702 DBG("Consumer mmap write detected relayd hang up");
1704 /* Unhandled error, print it and stop function right now. */
1705 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret
, write_len
);
1709 stream
->output_written
+= ret
;
1711 /* This call is useless on a socket so better save a syscall. */
1713 /* This won't block, but will start writeout asynchronously */
1714 lttng::io::hint_flush_range_async(outfd
, stream
->out_fd_offset
, write_len
);
1715 stream
->out_fd_offset
+= write_len
;
1716 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1721 * This is a special case that the relayd has closed its socket. Let's
1722 * cleanup the relayd object and all associated streams.
1724 if (relayd
&& relayd_hang_up
) {
1725 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1726 lttng_consumer_cleanup_relayd(relayd
);
1730 /* Unlock only if ctrl socket used */
1731 if (relayd
&& stream
->metadata_flag
) {
1732 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1739 * Splice the data from the ring buffer to the tracefile.
1741 * It must be called with the stream lock held.
1743 * Returns the number of bytes spliced.
1745 ssize_t
lttng_consumer_on_read_subbuffer_splice(struct lttng_consumer_local_data
*ctx
,
1746 struct lttng_consumer_stream
*stream
,
1748 unsigned long padding
)
1750 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1752 off_t orig_offset
= stream
->out_fd_offset
;
1753 int fd
= stream
->wait_fd
;
1754 /* Default is on the disk */
1755 int outfd
= stream
->out_fd
;
1756 struct consumer_relayd_sock_pair
*relayd
= nullptr;
1758 unsigned int relayd_hang_up
= 0;
1760 switch (the_consumer_data
.type
) {
1761 case LTTNG_CONSUMER_KERNEL
:
1763 case LTTNG_CONSUMER32_UST
:
1764 case LTTNG_CONSUMER64_UST
:
1765 /* Not supported for user space tracing */
1768 ERR("Unknown consumer_data type");
1772 /* RCU lock for the relayd pointer */
1773 lttng::urcu::read_lock_guard read_lock
;
1775 /* Flag that the current stream if set for network streaming. */
1776 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1777 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1778 if (relayd
== nullptr) {
1783 splice_pipe
= stream
->splice_pipe
;
1785 /* Write metadata stream id before payload */
1787 unsigned long total_len
= len
;
1789 if (stream
->metadata_flag
) {
1791 * Lock the control socket for the complete duration of the function
1792 * since from this point on we will use the socket.
1794 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1796 if (stream
->reset_metadata_flag
) {
1797 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1798 stream
->relayd_stream_id
,
1799 stream
->metadata_version
);
1804 stream
->reset_metadata_flag
= 0;
1806 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, padding
);
1813 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1816 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1822 /* Use the returned socket. */
1825 /* No streaming, we have to set the len with the full padding */
1828 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1829 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1831 ERR("Reset metadata file");
1834 stream
->reset_metadata_flag
= 0;
1837 * Check if we need to change the tracefile before writing the packet.
1839 if (stream
->chan
->tracefile_size
> 0 &&
1840 (stream
->tracefile_size_current
+ len
) > stream
->chan
->tracefile_size
) {
1841 ret
= consumer_stream_rotate_output_files(stream
);
1846 outfd
= stream
->out_fd
;
1849 stream
->tracefile_size_current
+= len
;
1853 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1854 (unsigned long) offset
,
1858 ret_splice
= splice(
1859 fd
, &offset
, splice_pipe
[1], nullptr, len
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1860 DBG("splice chan to pipe, ret %zd", ret_splice
);
1861 if (ret_splice
< 0) {
1864 PERROR("Error in relay splice");
1868 /* Handle stream on the relayd if the output is on the network */
1869 if (relayd
&& stream
->metadata_flag
) {
1870 size_t metadata_payload_size
=
1871 sizeof(struct lttcomm_relayd_metadata_payload
);
1873 /* Update counter to fit the spliced data */
1874 ret_splice
+= metadata_payload_size
;
1875 len
+= metadata_payload_size
;
1877 * We do this so the return value can match the len passed as
1878 * argument to this function.
1880 written
-= metadata_payload_size
;
1883 /* Splice data out */
1884 ret_splice
= splice(splice_pipe
[0],
1889 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1890 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd", outfd
, ret_splice
);
1891 if (ret_splice
< 0) {
1896 } else if (ret_splice
> len
) {
1898 * We don't expect this code path to be executed but you never know
1899 * so this is an extra protection agains a buggy splice().
1902 written
+= ret_splice
;
1903 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
, len
);
1906 /* All good, update current len and continue. */
1910 /* This call is useless on a socket so better save a syscall. */
1912 /* This won't block, but will start writeout asynchronously */
1913 lttng::io::hint_flush_range_async(outfd
, stream
->out_fd_offset
, ret_splice
);
1914 stream
->out_fd_offset
+= ret_splice
;
1916 stream
->output_written
+= ret_splice
;
1917 written
+= ret_splice
;
1920 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1926 * This is a special case that the relayd has closed its socket. Let's
1927 * cleanup the relayd object and all associated streams.
1929 if (relayd
&& relayd_hang_up
) {
1930 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1931 lttng_consumer_cleanup_relayd(relayd
);
1932 /* Skip splice error so the consumer does not fail */
1937 /* send the appropriate error description to sessiond */
1940 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1943 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1946 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1951 if (relayd
&& stream
->metadata_flag
) {
1952 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1959 * Sample the snapshot positions for a specific fd
1961 * Returns 0 on success, < 0 on error
1963 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
1965 switch (the_consumer_data
.type
) {
1966 case LTTNG_CONSUMER_KERNEL
:
1967 return lttng_kconsumer_sample_snapshot_positions(stream
);
1968 case LTTNG_CONSUMER32_UST
:
1969 case LTTNG_CONSUMER64_UST
:
1970 return lttng_ustconsumer_sample_snapshot_positions(stream
);
1972 ERR("Unknown consumer_data type");
1978 * Take a snapshot for a specific fd
1980 * Returns 0 on success, < 0 on error
1982 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1984 switch (the_consumer_data
.type
) {
1985 case LTTNG_CONSUMER_KERNEL
:
1986 return lttng_kconsumer_take_snapshot(stream
);
1987 case LTTNG_CONSUMER32_UST
:
1988 case LTTNG_CONSUMER64_UST
:
1989 return lttng_ustconsumer_take_snapshot(stream
);
1991 ERR("Unknown consumer_data type");
1998 * Get the produced position
2000 * Returns 0 on success, < 0 on error
2002 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2004 switch (the_consumer_data
.type
) {
2005 case LTTNG_CONSUMER_KERNEL
:
2006 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
2007 case LTTNG_CONSUMER32_UST
:
2008 case LTTNG_CONSUMER64_UST
:
2009 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2011 ERR("Unknown consumer_data type");
2018 * Get the consumed position (free-running counter position in bytes).
2020 * Returns 0 on success, < 0 on error
2022 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2024 switch (the_consumer_data
.type
) {
2025 case LTTNG_CONSUMER_KERNEL
:
2026 return lttng_kconsumer_get_consumed_snapshot(stream
, pos
);
2027 case LTTNG_CONSUMER32_UST
:
2028 case LTTNG_CONSUMER64_UST
:
2029 return lttng_ustconsumer_get_consumed_snapshot(stream
, pos
);
2031 ERR("Unknown consumer_data type");
2037 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2039 struct pollfd
*consumer_sockpoll
)
2041 switch (the_consumer_data
.type
) {
2042 case LTTNG_CONSUMER_KERNEL
:
2043 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2044 case LTTNG_CONSUMER32_UST
:
2045 case LTTNG_CONSUMER64_UST
:
2046 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2048 ERR("Unknown consumer_data type");
2054 static void lttng_consumer_close_all_metadata()
2056 switch (the_consumer_data
.type
) {
2057 case LTTNG_CONSUMER_KERNEL
:
2059 * The Kernel consumer has a different metadata scheme so we don't
2060 * close anything because the stream will be closed by the session
2064 case LTTNG_CONSUMER32_UST
:
2065 case LTTNG_CONSUMER64_UST
:
2067 * Close all metadata streams. The metadata hash table is passed and
2068 * this call iterates over it by closing all wakeup fd. This is safe
2069 * because at this point we are sure that the metadata producer is
2070 * either dead or blocked.
2072 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2075 ERR("Unknown consumer_data type");
2081 * Clean up a metadata stream and free its memory.
2083 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
, struct lttng_ht
*ht
)
2085 struct lttng_consumer_channel
*channel
= nullptr;
2086 bool free_channel
= false;
2088 LTTNG_ASSERT(stream
);
2090 * This call should NEVER receive regular stream. It must always be
2091 * metadata stream and this is crucial for data structure synchronization.
2093 LTTNG_ASSERT(stream
->metadata_flag
);
2095 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2097 pthread_mutex_lock(&the_consumer_data
.lock
);
2099 * Note that this assumes that a stream's channel is never changed and
2100 * that the stream's lock doesn't need to be taken to sample its
2103 channel
= stream
->chan
;
2104 pthread_mutex_lock(&channel
->lock
);
2105 pthread_mutex_lock(&stream
->lock
);
2106 if (channel
->metadata_cache
) {
2107 /* Only applicable to userspace consumers. */
2108 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
2111 /* Remove any reference to that stream. */
2112 consumer_stream_delete(stream
, ht
);
2114 /* Close down everything including the relayd if one. */
2115 consumer_stream_close_output(stream
);
2116 /* Destroy tracer buffers of the stream. */
2117 consumer_stream_destroy_buffers(stream
);
2119 /* Atomically decrement channel refcount since other threads can use it. */
2120 if (!uatomic_sub_return(&channel
->refcount
, 1) &&
2121 !uatomic_read(&channel
->nb_init_stream_left
)) {
2122 /* Go for channel deletion! */
2123 free_channel
= true;
2125 stream
->chan
= nullptr;
2128 * Nullify the stream reference so it is not used after deletion. The
2129 * channel lock MUST be acquired before being able to check for a NULL
2132 channel
->metadata_stream
= nullptr;
2134 if (channel
->metadata_cache
) {
2135 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
2137 pthread_mutex_unlock(&stream
->lock
);
2138 pthread_mutex_unlock(&channel
->lock
);
2139 pthread_mutex_unlock(&the_consumer_data
.lock
);
2142 consumer_del_channel(channel
);
2145 lttng_trace_chunk_put(stream
->trace_chunk
);
2146 stream
->trace_chunk
= nullptr;
2147 consumer_stream_free(stream
);
2151 * Action done with the metadata stream when adding it to the consumer internal
2152 * data structures to handle it.
2154 void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2156 struct lttng_ht
*ht
= metadata_ht
;
2157 struct lttng_ht_iter iter
;
2158 struct lttng_ht_node_u64
*node
;
2160 LTTNG_ASSERT(stream
);
2163 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2165 pthread_mutex_lock(&the_consumer_data
.lock
);
2166 pthread_mutex_lock(&stream
->chan
->lock
);
2167 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2168 pthread_mutex_lock(&stream
->lock
);
2171 * From here, refcounts are updated so be _careful_ when returning an error
2175 lttng::urcu::read_lock_guard read_lock
;
2178 * Lookup the stream just to make sure it does not exist in our internal
2179 * state. This should NEVER happen.
2181 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2182 node
= lttng_ht_iter_get_node_u64(&iter
);
2183 LTTNG_ASSERT(!node
);
2186 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2187 * in terms of destroying the associated channel, because the action that
2188 * causes the count to become 0 also causes a stream to be added. The
2189 * channel deletion will thus be triggered by the following removal of this
2192 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2193 /* Increment refcount before decrementing nb_init_stream_left */
2195 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2198 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2200 lttng_ht_add_u64(the_consumer_data
.stream_per_chan_id_ht
, &stream
->node_channel_id
);
2203 * Add stream to the stream_list_ht of the consumer data. No need to steal
2204 * the key since the HT does not use it and we allow to add redundant keys
2207 lttng_ht_add_u64(the_consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2209 pthread_mutex_unlock(&stream
->lock
);
2210 pthread_mutex_unlock(&stream
->chan
->lock
);
2211 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2212 pthread_mutex_unlock(&the_consumer_data
.lock
);
2216 * Delete data stream that are flagged for deletion (endpoint_status).
2218 static void validate_endpoint_status_data_stream()
2220 struct lttng_ht_iter iter
;
2221 struct lttng_consumer_stream
*stream
;
2223 DBG("Consumer delete flagged data stream");
2226 lttng::urcu::read_lock_guard read_lock
;
2228 cds_lfht_for_each_entry (data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2229 /* Validate delete flag of the stream */
2230 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2233 /* Delete it right now */
2234 consumer_del_stream(stream
, data_ht
);
2240 * Delete metadata stream that are flagged for deletion (endpoint_status).
2242 static void validate_endpoint_status_metadata_stream(struct lttng_poll_event
*pollset
)
2244 struct lttng_ht_iter iter
;
2245 struct lttng_consumer_stream
*stream
;
2247 DBG("Consumer delete flagged metadata stream");
2249 LTTNG_ASSERT(pollset
);
2252 lttng::urcu::read_lock_guard read_lock
;
2253 cds_lfht_for_each_entry (metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2254 /* Validate delete flag of the stream */
2255 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2259 * Remove from pollset so the metadata thread can continue without
2260 * blocking on a deleted stream.
2262 lttng_poll_del(pollset
, stream
->wait_fd
);
2264 /* Delete it right now */
2265 consumer_del_metadata_stream(stream
, metadata_ht
);
2271 * Thread polls on metadata file descriptor and write them on disk or on the
2274 void *consumer_thread_metadata_poll(void *data
)
2276 int ret
, i
, pollfd
, err
= -1;
2277 uint32_t revents
, nb_fd
;
2278 struct lttng_consumer_stream
*stream
= nullptr;
2279 struct lttng_ht_iter iter
;
2280 struct lttng_ht_node_u64
*node
;
2281 struct lttng_poll_event events
;
2282 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
2285 rcu_register_thread();
2287 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2289 if (testpoint(consumerd_thread_metadata
)) {
2290 goto error_testpoint
;
2293 health_code_update();
2295 DBG("Thread metadata poll started");
2297 /* Size is set to 1 for the consumer_metadata pipe */
2298 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2300 ERR("Poll set creation failed");
2304 ret
= lttng_poll_add(&events
, lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2310 DBG("Metadata main loop started");
2314 health_code_update();
2315 health_poll_entry();
2316 DBG("Metadata poll wait");
2317 ret
= lttng_poll_wait(&events
, -1);
2318 DBG("Metadata poll return from wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2320 DBG("Metadata event caught in thread");
2322 if (errno
== EINTR
) {
2323 ERR("Poll EINTR caught");
2326 if (LTTNG_POLL_GETNB(&events
) == 0) {
2327 err
= 0; /* All is OK */
2334 /* From here, the event is a metadata wait fd */
2335 for (i
= 0; i
< nb_fd
; i
++) {
2336 health_code_update();
2338 revents
= LTTNG_POLL_GETEV(&events
, i
);
2339 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2341 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2342 if (revents
& LPOLLIN
) {
2345 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2347 sizeof(stream
)); /* NOLINT sizeof
2350 if (pipe_len
< sizeof(stream
)) { /* NOLINT sizeof used on a
2353 PERROR("read metadata stream");
2356 * Remove the pipe from the poll set and continue
2357 * the loop since their might be data to consume.
2361 lttng_pipe_get_readfd(
2362 ctx
->consumer_metadata_pipe
));
2363 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2367 /* A NULL stream means that the state has changed. */
2368 if (stream
== nullptr) {
2369 /* Check for deleted streams. */
2370 validate_endpoint_status_metadata_stream(&events
);
2374 DBG("Adding metadata stream %d to poll set",
2377 /* Add metadata stream to the global poll events list */
2379 &events
, stream
->wait_fd
, LPOLLIN
| LPOLLPRI
);
2380 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2381 DBG("Metadata thread pipe hung up");
2383 * Remove the pipe from the poll set and continue the loop
2384 * since their might be data to consume.
2388 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2389 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2392 ERR("Unexpected poll events %u for sock %d",
2398 /* Handle other stream */
2402 lttng::urcu::read_lock_guard read_lock
;
2404 uint64_t tmp_id
= (uint64_t) pollfd
;
2406 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2408 node
= lttng_ht_iter_get_node_u64(&iter
);
2411 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
2413 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2414 /* Get the data out of the metadata file descriptor */
2415 DBG("Metadata available on fd %d", pollfd
);
2416 LTTNG_ASSERT(stream
->wait_fd
== pollfd
);
2419 health_code_update();
2421 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2423 * We don't check the return value here since if we get
2424 * a negative len, it means an error occurred thus we
2425 * simply remove it from the poll set and free the
2430 /* It's ok to have an unavailable sub-buffer */
2431 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2432 /* Clean up stream from consumer and free it. */
2433 lttng_poll_del(&events
, stream
->wait_fd
);
2434 consumer_del_metadata_stream(stream
, metadata_ht
);
2436 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2437 DBG("Metadata fd %d is hup|err.", pollfd
);
2438 if (!stream
->hangup_flush_done
&&
2439 (the_consumer_data
.type
== LTTNG_CONSUMER32_UST
||
2440 the_consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2441 DBG("Attempting to flush and consume the UST buffers");
2442 lttng_ustconsumer_on_stream_hangup(stream
);
2444 /* We just flushed the stream now read it. */
2446 health_code_update();
2448 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2450 * We don't check the return value here since if we
2451 * get a negative len, it means an error occurred
2452 * thus we simply remove it from the poll set and
2458 lttng_poll_del(&events
, stream
->wait_fd
);
2460 * This call update the channel states, closes file descriptors
2461 * and securely free the stream.
2463 consumer_del_metadata_stream(stream
, metadata_ht
);
2465 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2468 /* Release RCU lock for the stream looked up */
2475 DBG("Metadata poll thread exiting");
2477 lttng_poll_clean(&events
);
2482 ERR("Health error occurred in %s", __func__
);
2484 health_unregister(health_consumerd
);
2485 rcu_unregister_thread();
2490 * This thread polls the fds in the set to consume the data and write
2491 * it to tracefile if necessary.
2493 void *consumer_thread_data_poll(void *data
)
2495 int num_rdy
, high_prio
, ret
, i
, err
= -1;
2496 struct pollfd
*pollfd
= nullptr;
2497 /* local view of the streams */
2498 struct lttng_consumer_stream
**local_stream
= nullptr, *new_stream
= nullptr;
2499 /* local view of consumer_data.fds_count */
2501 /* 2 for the consumer_data_pipe and wake up pipe */
2502 const int nb_pipes_fd
= 2;
2503 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2504 int nb_inactive_fd
= 0;
2505 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
2508 rcu_register_thread();
2510 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2512 if (testpoint(consumerd_thread_data
)) {
2513 goto error_testpoint
;
2516 health_code_update();
2518 local_stream
= zmalloc
<lttng_consumer_stream
*>();
2519 if (local_stream
== nullptr) {
2520 PERROR("local_stream malloc");
2525 health_code_update();
2530 * the fds set has been updated, we need to update our
2531 * local array as well
2533 pthread_mutex_lock(&the_consumer_data
.lock
);
2534 if (the_consumer_data
.need_update
) {
2539 local_stream
= nullptr;
2541 /* Allocate for all fds */
2543 calloc
<struct pollfd
>(the_consumer_data
.stream_count
+ nb_pipes_fd
);
2544 if (pollfd
== nullptr) {
2545 PERROR("pollfd malloc");
2546 pthread_mutex_unlock(&the_consumer_data
.lock
);
2550 local_stream
= calloc
<lttng_consumer_stream
*>(
2551 the_consumer_data
.stream_count
+ nb_pipes_fd
);
2552 if (local_stream
== nullptr) {
2553 PERROR("local_stream malloc");
2554 pthread_mutex_unlock(&the_consumer_data
.lock
);
2557 ret
= update_poll_array(
2558 ctx
, &pollfd
, local_stream
, data_ht
, &nb_inactive_fd
);
2560 ERR("Error in allocating pollfd or local_outfds");
2561 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2562 pthread_mutex_unlock(&the_consumer_data
.lock
);
2566 the_consumer_data
.need_update
= 0;
2568 pthread_mutex_unlock(&the_consumer_data
.lock
);
2570 /* No FDs and consumer_quit, consumer_cleanup the thread */
2571 if (nb_fd
== 0 && nb_inactive_fd
== 0 && CMM_LOAD_SHARED(consumer_quit
) == 1) {
2572 err
= 0; /* All is OK */
2575 /* poll on the array of fds */
2577 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2578 if (testpoint(consumerd_thread_data_poll
)) {
2581 health_poll_entry();
2582 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2584 DBG("poll num_rdy : %d", num_rdy
);
2585 if (num_rdy
== -1) {
2587 * Restart interrupted system call.
2589 if (errno
== EINTR
) {
2592 PERROR("Poll error");
2593 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2595 } else if (num_rdy
== 0) {
2596 DBG("Polling thread timed out");
2600 if (caa_unlikely(data_consumption_paused
)) {
2601 DBG("Data consumption paused, sleeping...");
2607 * If the consumer_data_pipe triggered poll go directly to the
2608 * beginning of the loop to update the array. We want to prioritize
2609 * array update over low-priority reads.
2611 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2612 ssize_t pipe_readlen
;
2614 DBG("consumer_data_pipe wake up");
2615 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2617 sizeof(new_stream
)); /* NOLINT sizeof used on
2619 if (pipe_readlen
< sizeof(new_stream
)) { /* NOLINT sizeof used on a pointer.
2621 PERROR("Consumer data pipe");
2622 /* Continue so we can at least handle the current stream(s). */
2627 * If the stream is NULL, just ignore it. It's also possible that
2628 * the sessiond poll thread changed the consumer_quit state and is
2629 * waking us up to test it.
2631 if (new_stream
== nullptr) {
2632 validate_endpoint_status_data_stream();
2636 /* Continue to update the local streams and handle prio ones */
2640 /* Handle wakeup pipe. */
2641 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2643 ssize_t pipe_readlen
;
2646 lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
, sizeof(dummy
));
2647 if (pipe_readlen
< 0) {
2648 PERROR("Consumer data wakeup pipe");
2650 /* We've been awakened to handle stream(s). */
2651 ctx
->has_wakeup
= 0;
2654 /* Take care of high priority channels first. */
2655 for (i
= 0; i
< nb_fd
; i
++) {
2656 health_code_update();
2658 if (local_stream
[i
] == nullptr) {
2661 if (pollfd
[i
].revents
& POLLPRI
) {
2662 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2664 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2665 /* it's ok to have an unavailable sub-buffer */
2666 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2667 /* Clean the stream and free it. */
2668 consumer_del_stream(local_stream
[i
], data_ht
);
2669 local_stream
[i
] = nullptr;
2670 } else if (len
> 0) {
2671 local_stream
[i
]->has_data_left_to_be_read_before_teardown
=
2678 * If we read high prio channel in this loop, try again
2679 * for more high prio data.
2685 /* Take care of low priority channels. */
2686 for (i
= 0; i
< nb_fd
; i
++) {
2687 health_code_update();
2689 if (local_stream
[i
] == nullptr) {
2692 if ((pollfd
[i
].revents
& POLLIN
) || local_stream
[i
]->hangup_flush_done
||
2693 local_stream
[i
]->has_data
) {
2694 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2695 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2696 /* it's ok to have an unavailable sub-buffer */
2697 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2698 /* Clean the stream and free it. */
2699 consumer_del_stream(local_stream
[i
], data_ht
);
2700 local_stream
[i
] = nullptr;
2701 } else if (len
> 0) {
2702 local_stream
[i
]->has_data_left_to_be_read_before_teardown
=
2708 /* Handle hangup and errors */
2709 for (i
= 0; i
< nb_fd
; i
++) {
2710 health_code_update();
2712 if (local_stream
[i
] == nullptr) {
2715 if (!local_stream
[i
]->hangup_flush_done
&&
2716 (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
)) &&
2717 (the_consumer_data
.type
== LTTNG_CONSUMER32_UST
||
2718 the_consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2719 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2721 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2722 /* Attempt read again, for the data we just flushed. */
2723 local_stream
[i
]->has_data_left_to_be_read_before_teardown
= 1;
2726 * When a stream's pipe dies (hup/err/nval), an "inactive producer" flush is
2727 * performed. This type of flush ensures that a new packet is produced no
2728 * matter the consumed/produced positions are.
2730 * This, in turn, causes the next pass to see that data available for the
2731 * stream. When we come back here, we can be assured that all available
2732 * data has been consumed and we can finally destroy the stream.
2734 * If the poll flag is HUP/ERR/NVAL and we have
2735 * read no data in this pass, we can remove the
2736 * stream from its hash table.
2738 if ((pollfd
[i
].revents
& POLLHUP
)) {
2739 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2740 if (!local_stream
[i
]->has_data_left_to_be_read_before_teardown
) {
2741 consumer_del_stream(local_stream
[i
], data_ht
);
2742 local_stream
[i
] = nullptr;
2744 } else if (pollfd
[i
].revents
& POLLERR
) {
2745 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2746 if (!local_stream
[i
]->has_data_left_to_be_read_before_teardown
) {
2747 consumer_del_stream(local_stream
[i
], data_ht
);
2748 local_stream
[i
] = nullptr;
2750 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2751 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2752 if (!local_stream
[i
]->has_data_left_to_be_read_before_teardown
) {
2753 consumer_del_stream(local_stream
[i
], data_ht
);
2754 local_stream
[i
] = nullptr;
2757 if (local_stream
[i
] != nullptr) {
2758 local_stream
[i
]->has_data_left_to_be_read_before_teardown
= 0;
2765 DBG("polling thread exiting");
2770 * Close the write side of the pipe so epoll_wait() in
2771 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2772 * read side of the pipe. If we close them both, epoll_wait strangely does
2773 * not return and could create a endless wait period if the pipe is the
2774 * only tracked fd in the poll set. The thread will take care of closing
2777 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2782 ERR("Health error occurred in %s", __func__
);
2784 health_unregister(health_consumerd
);
2786 rcu_unregister_thread();
2791 * Close wake-up end of each stream belonging to the channel. This will
2792 * allow the poll() on the stream read-side to detect when the
2793 * write-side (application) finally closes them.
2795 static void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2797 struct lttng_ht
*ht
;
2798 struct lttng_consumer_stream
*stream
;
2799 struct lttng_ht_iter iter
;
2801 ht
= the_consumer_data
.stream_per_chan_id_ht
;
2803 lttng::urcu::read_lock_guard read_lock
;
2804 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2805 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2810 node_channel_id
.node
)
2813 * Protect against teardown with mutex.
2815 pthread_mutex_lock(&stream
->lock
);
2816 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2819 switch (the_consumer_data
.type
) {
2820 case LTTNG_CONSUMER_KERNEL
:
2822 case LTTNG_CONSUMER32_UST
:
2823 case LTTNG_CONSUMER64_UST
:
2824 if (stream
->metadata_flag
) {
2825 /* Safe and protected by the stream lock. */
2826 lttng_ustconsumer_close_metadata(stream
->chan
);
2829 * Note: a mutex is taken internally within
2830 * liblttng-ust-ctl to protect timer wakeup_fd
2831 * use from concurrent close.
2833 lttng_ustconsumer_close_stream_wakeup(stream
);
2837 ERR("Unknown consumer_data type");
2841 pthread_mutex_unlock(&stream
->lock
);
2845 static void destroy_channel_ht(struct lttng_ht
*ht
)
2847 struct lttng_ht_iter iter
;
2848 struct lttng_consumer_channel
*channel
;
2851 if (ht
== nullptr) {
2856 lttng::urcu::read_lock_guard read_lock
;
2858 cds_lfht_for_each_entry (ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2859 ret
= lttng_ht_del(ht
, &iter
);
2860 LTTNG_ASSERT(ret
!= 0);
2864 lttng_ht_destroy(ht
);
2868 * This thread polls the channel fds to detect when they are being
2869 * closed. It closes all related streams if the channel is detected as
2870 * closed. It is currently only used as a shim layer for UST because the
2871 * consumerd needs to keep the per-stream wakeup end of pipes open for
2874 void *consumer_thread_channel_poll(void *data
)
2876 int ret
, i
, pollfd
, err
= -1;
2877 uint32_t revents
, nb_fd
;
2878 struct lttng_consumer_channel
*chan
= nullptr;
2879 struct lttng_ht_iter iter
;
2880 struct lttng_ht_node_u64
*node
;
2881 struct lttng_poll_event events
;
2882 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
2883 struct lttng_ht
*channel_ht
;
2885 rcu_register_thread();
2887 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2889 if (testpoint(consumerd_thread_channel
)) {
2890 goto error_testpoint
;
2893 health_code_update();
2895 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2897 /* ENOMEM at this point. Better to bail out. */
2901 DBG("Thread channel poll started");
2903 /* Size is set to 1 for the consumer_channel pipe */
2904 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2906 ERR("Poll set creation failed");
2910 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2916 DBG("Channel main loop started");
2920 health_code_update();
2921 DBG("Channel poll wait");
2922 health_poll_entry();
2923 ret
= lttng_poll_wait(&events
, -1);
2924 DBG("Channel poll return from wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2926 DBG("Channel event caught in thread");
2928 if (errno
== EINTR
) {
2929 ERR("Poll EINTR caught");
2932 if (LTTNG_POLL_GETNB(&events
) == 0) {
2933 err
= 0; /* All is OK */
2940 /* From here, the event is a channel wait fd */
2941 for (i
= 0; i
< nb_fd
; i
++) {
2942 health_code_update();
2944 revents
= LTTNG_POLL_GETEV(&events
, i
);
2945 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2947 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2948 if (revents
& LPOLLIN
) {
2949 enum consumer_channel_action action
;
2952 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2955 ERR("Error reading channel pipe");
2957 lttng_poll_del(&events
,
2958 ctx
->consumer_channel_pipe
[0]);
2963 case CONSUMER_CHANNEL_ADD
:
2965 DBG("Adding channel %d to poll set", chan
->wait_fd
);
2967 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2969 lttng::urcu::read_lock_guard read_lock
;
2970 lttng_ht_add_unique_u64(channel_ht
,
2971 &chan
->wait_fd_node
);
2972 /* Add channel to the global poll events list */
2973 // FIXME: Empty flag on a pipe pollset, this might
2975 lttng_poll_add(&events
, chan
->wait_fd
, 0);
2978 case CONSUMER_CHANNEL_DEL
:
2981 * This command should never be called if the
2982 * channel has streams monitored by either the data
2983 * or metadata thread. The consumer only notify this
2984 * thread with a channel del. command if it receives
2985 * a destroy channel command from the session daemon
2986 * that send it if a command prior to the
2987 * GET_CHANNEL failed.
2990 lttng::urcu::read_lock_guard read_lock
;
2991 chan
= consumer_find_channel(key
);
2993 ERR("UST consumer get channel key %" PRIu64
2994 " not found for del channel",
2998 lttng_poll_del(&events
, chan
->wait_fd
);
2999 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
3000 ret
= lttng_ht_del(channel_ht
, &iter
);
3001 LTTNG_ASSERT(ret
== 0);
3003 switch (the_consumer_data
.type
) {
3004 case LTTNG_CONSUMER_KERNEL
:
3006 case LTTNG_CONSUMER32_UST
:
3007 case LTTNG_CONSUMER64_UST
:
3008 health_code_update();
3009 /* Destroy streams that might have been left
3010 * in the stream list. */
3011 clean_channel_stream_list(chan
);
3014 ERR("Unknown consumer_data type");
3019 * Release our own refcount. Force channel deletion
3020 * even if streams were not initialized.
3022 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3023 consumer_del_channel(chan
);
3027 case CONSUMER_CHANNEL_QUIT
:
3029 * Remove the pipe from the poll set and continue
3030 * the loop since their might be data to consume.
3032 lttng_poll_del(&events
,
3033 ctx
->consumer_channel_pipe
[0]);
3036 ERR("Unknown action");
3039 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3040 DBG("Channel thread pipe hung up");
3042 * Remove the pipe from the poll set and continue the loop
3043 * since their might be data to consume.
3045 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3048 ERR("Unexpected poll events %u for sock %d",
3054 /* Handle other stream */
3058 lttng::urcu::read_lock_guard read_lock
;
3060 uint64_t tmp_id
= (uint64_t) pollfd
;
3062 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3064 node
= lttng_ht_iter_get_node_u64(&iter
);
3067 chan
= caa_container_of(node
, struct lttng_consumer_channel
, wait_fd_node
);
3069 /* Check for error event */
3070 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3071 DBG("Channel fd %d is hup|err.", pollfd
);
3073 lttng_poll_del(&events
, chan
->wait_fd
);
3074 ret
= lttng_ht_del(channel_ht
, &iter
);
3075 LTTNG_ASSERT(ret
== 0);
3078 * This will close the wait fd for each stream associated to
3079 * this channel AND monitored by the data/metadata thread thus
3080 * will be clean by the right thread.
3082 consumer_close_channel_streams(chan
);
3084 /* Release our own refcount */
3085 if (!uatomic_sub_return(&chan
->refcount
, 1) &&
3086 !uatomic_read(&chan
->nb_init_stream_left
)) {
3087 consumer_del_channel(chan
);
3090 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3094 /* Release RCU lock for the channel looked up */
3101 lttng_poll_clean(&events
);
3103 destroy_channel_ht(channel_ht
);
3106 DBG("Channel poll thread exiting");
3109 ERR("Health error occurred in %s", __func__
);
3111 health_unregister(health_consumerd
);
3112 rcu_unregister_thread();
3116 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3117 struct pollfd
*sockpoll
,
3123 LTTNG_ASSERT(sockpoll
);
3125 ret
= lttng_consumer_poll_socket(sockpoll
);
3129 DBG("Metadata connection on client_socket");
3131 /* Blocking call, waiting for transmission */
3132 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3133 if (ctx
->consumer_metadata_socket
< 0) {
3134 WARN("On accept metadata");
3145 * This thread listens on the consumerd socket and receives the file
3146 * descriptors from the session daemon.
3148 void *consumer_thread_sessiond_poll(void *data
)
3150 int sock
= -1, client_socket
, ret
, err
= -1;
3152 * structure to poll for incoming data on communication socket avoids
3153 * making blocking sockets.
3155 struct pollfd consumer_sockpoll
[2];
3156 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
3158 rcu_register_thread();
3160 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3162 if (testpoint(consumerd_thread_sessiond
)) {
3163 goto error_testpoint
;
3166 health_code_update();
3168 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3169 unlink(ctx
->consumer_command_sock_path
);
3170 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3171 if (client_socket
< 0) {
3172 ERR("Cannot create command socket");
3176 ret
= lttcomm_listen_unix_sock(client_socket
);
3181 DBG("Sending ready command to lttng-sessiond");
3182 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3183 /* return < 0 on error, but == 0 is not fatal */
3185 ERR("Error sending ready command to lttng-sessiond");
3189 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3190 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3191 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3192 consumer_sockpoll
[1].fd
= client_socket
;
3193 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3195 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3203 DBG("Connection on client_socket");
3205 /* Blocking call, waiting for transmission */
3206 sock
= lttcomm_accept_unix_sock(client_socket
);
3213 * Setup metadata socket which is the second socket connection on the
3214 * command unix socket.
3216 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3225 /* This socket is not useful anymore. */
3226 ret
= close(client_socket
);
3228 PERROR("close client_socket");
3232 /* update the polling structure to poll on the established socket */
3233 consumer_sockpoll
[1].fd
= sock
;
3234 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3237 health_code_update();
3239 health_poll_entry();
3240 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3249 DBG("Incoming command on sock");
3250 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3253 * This could simply be a session daemon quitting. Don't output
3256 DBG("Communication interrupted on command socket");
3260 if (CMM_LOAD_SHARED(consumer_quit
)) {
3261 DBG("consumer_thread_receive_fds received quit from signal");
3262 err
= 0; /* All is OK */
3265 DBG("Received command on sock");
3271 DBG("Consumer thread sessiond poll exiting");
3274 * Close metadata streams since the producer is the session daemon which
3277 * NOTE: for now, this only applies to the UST tracer.
3279 lttng_consumer_close_all_metadata();
3282 * when all fds have hung up, the polling thread
3285 CMM_STORE_SHARED(consumer_quit
, 1);
3288 * Notify the data poll thread to poll back again and test the
3289 * consumer_quit state that we just set so to quit gracefully.
3291 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3293 notify_channel_pipe(ctx
, nullptr, -1, CONSUMER_CHANNEL_QUIT
);
3295 notify_health_quit_pipe(health_quit_pipe
);
3297 /* Cleaning up possibly open sockets. */
3301 PERROR("close sock sessiond poll");
3304 if (client_socket
>= 0) {
3305 ret
= close(client_socket
);
3307 PERROR("close client_socket sessiond poll");
3314 ERR("Health error occurred in %s", __func__
);
3316 health_unregister(health_consumerd
);
3318 rcu_unregister_thread();
3322 static int post_consume(struct lttng_consumer_stream
*stream
,
3323 const struct stream_subbuffer
*subbuffer
,
3324 struct lttng_consumer_local_data
*ctx
)
3328 const size_t count
=
3329 lttng_dynamic_array_get_count(&stream
->read_subbuffer_ops
.post_consume_cbs
);
3331 for (i
= 0; i
< count
; i
++) {
3332 const post_consume_cb op
= *(post_consume_cb
*) lttng_dynamic_array_get_element(
3333 &stream
->read_subbuffer_ops
.post_consume_cbs
, i
);
3335 ret
= op(stream
, subbuffer
, ctx
);
3344 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3345 struct lttng_consumer_local_data
*ctx
,
3346 bool locked_by_caller
)
3348 ssize_t ret
, written_bytes
= 0;
3350 struct stream_subbuffer subbuffer
= {};
3351 enum get_next_subbuffer_status get_next_status
;
3353 if (!locked_by_caller
) {
3354 stream
->read_subbuffer_ops
.lock(stream
);
3356 stream
->read_subbuffer_ops
.assert_locked(stream
);
3359 if (stream
->read_subbuffer_ops
.on_wake_up
) {
3360 ret
= stream
->read_subbuffer_ops
.on_wake_up(stream
);
3367 * If the stream was flagged to be ready for rotation before we extract
3368 * the next packet, rotate it now.
3370 if (stream
->rotate_ready
) {
3371 DBG("Rotate stream before consuming data");
3372 ret
= lttng_consumer_rotate_stream(stream
);
3374 ERR("Stream rotation error before consuming data");
3379 get_next_status
= stream
->read_subbuffer_ops
.get_next_subbuffer(stream
, &subbuffer
);
3380 switch (get_next_status
) {
3381 case GET_NEXT_SUBBUFFER_STATUS_OK
:
3383 case GET_NEXT_SUBBUFFER_STATUS_NO_DATA
:
3387 case GET_NEXT_SUBBUFFER_STATUS_ERROR
:
3394 ret
= stream
->read_subbuffer_ops
.pre_consume_subbuffer(stream
, &subbuffer
);
3396 goto error_put_subbuf
;
3399 written_bytes
= stream
->read_subbuffer_ops
.consume_subbuffer(ctx
, stream
, &subbuffer
);
3400 if (written_bytes
<= 0) {
3401 ERR("Error consuming subbuffer: (%zd)", written_bytes
);
3402 ret
= (int) written_bytes
;
3403 goto error_put_subbuf
;
3406 ret
= stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3411 ret
= post_consume(stream
, &subbuffer
, ctx
);
3417 * After extracting the packet, we check if the stream is now ready to
3418 * be rotated and perform the action immediately.
3420 * Don't overwrite `ret` as callers expect the number of bytes
3421 * consumed to be returned on success.
3423 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
3424 if (rotation_ret
== 1) {
3425 rotation_ret
= lttng_consumer_rotate_stream(stream
);
3426 if (rotation_ret
< 0) {
3428 ERR("Stream rotation error after consuming data");
3432 } else if (rotation_ret
< 0) {
3434 ERR("Failed to check if stream was ready to rotate after consuming data");
3439 if (stream
->read_subbuffer_ops
.on_sleep
) {
3440 stream
->read_subbuffer_ops
.on_sleep(stream
, ctx
);
3443 ret
= written_bytes
;
3445 if (!locked_by_caller
) {
3446 stream
->read_subbuffer_ops
.unlock(stream
);
3451 (void) stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3455 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3457 switch (the_consumer_data
.type
) {
3458 case LTTNG_CONSUMER_KERNEL
:
3459 return lttng_kconsumer_on_recv_stream(stream
);
3460 case LTTNG_CONSUMER32_UST
:
3461 case LTTNG_CONSUMER64_UST
:
3462 return lttng_ustconsumer_on_recv_stream(stream
);
3464 ERR("Unknown consumer_data type");
3471 * Allocate and set consumer data hash tables.
3473 int lttng_consumer_init()
3475 the_consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3476 if (!the_consumer_data
.channel_ht
) {
3480 the_consumer_data
.channels_by_session_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3481 if (!the_consumer_data
.channels_by_session_id_ht
) {
3485 the_consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3486 if (!the_consumer_data
.relayd_ht
) {
3490 the_consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3491 if (!the_consumer_data
.stream_list_ht
) {
3495 the_consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3496 if (!the_consumer_data
.stream_per_chan_id_ht
) {
3500 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3505 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3510 the_consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3511 if (!the_consumer_data
.chunk_registry
) {
3522 * Process the ADD_RELAYD command receive by a consumer.
3524 * This will create a relayd socket pair and add it to the relayd hash table.
3525 * The caller MUST acquire a RCU read side lock before calling it.
3527 void consumer_add_relayd_socket(uint64_t net_seq_idx
,
3529 struct lttng_consumer_local_data
*ctx
,
3531 struct pollfd
*consumer_sockpoll
,
3532 uint64_t sessiond_id
,
3533 uint64_t relayd_session_id
,
3534 uint32_t relayd_version_major
,
3535 uint32_t relayd_version_minor
,
3536 enum lttcomm_sock_proto relayd_socket_protocol
)
3538 int fd
= -1, ret
= -1, relayd_created
= 0;
3539 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3540 struct consumer_relayd_sock_pair
*relayd
= nullptr;
3543 LTTNG_ASSERT(sock
>= 0);
3544 ASSERT_RCU_READ_LOCKED();
3546 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3548 /* Get relayd reference if exists. */
3549 relayd
= consumer_find_relayd(net_seq_idx
);
3550 if (relayd
== nullptr) {
3551 LTTNG_ASSERT(sock_type
== LTTNG_STREAM_CONTROL
);
3552 /* Not found. Allocate one. */
3553 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3554 if (relayd
== nullptr) {
3555 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3558 relayd
->sessiond_session_id
= sessiond_id
;
3563 * This code path MUST continue to the consumer send status message to
3564 * we can notify the session daemon and continue our work without
3565 * killing everything.
3569 * relayd key should never be found for control socket.
3571 LTTNG_ASSERT(sock_type
!= LTTNG_STREAM_CONTROL
);
3574 /* First send a status message before receiving the fds. */
3575 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3577 /* Somehow, the session daemon is not responding anymore. */
3578 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3579 goto error_nosignal
;
3582 /* Poll on consumer socket. */
3583 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3585 /* Needing to exit in the middle of a command: error. */
3586 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3587 goto error_nosignal
;
3590 /* Get relayd socket from session daemon */
3591 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3592 if (ret
!= sizeof(fd
)) {
3593 fd
= -1; /* Just in case it gets set with an invalid value. */
3596 * Failing to receive FDs might indicate a major problem such as
3597 * reaching a fd limit during the receive where the kernel returns a
3598 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3599 * don't take any chances and stop everything.
3601 * XXX: Feature request #558 will fix that and avoid this possible
3602 * issue when reaching the fd limit.
3604 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3605 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3609 /* Copy socket information and received FD */
3610 switch (sock_type
) {
3611 case LTTNG_STREAM_CONTROL
:
3612 /* Copy received lttcomm socket */
3613 ret
= lttcomm_populate_sock_from_open_socket(
3614 &relayd
->control_sock
.sock
, fd
, relayd_socket_protocol
);
3616 /* Assign version values. */
3617 relayd
->control_sock
.major
= relayd_version_major
;
3618 relayd
->control_sock
.minor
= relayd_version_minor
;
3620 relayd
->relayd_session_id
= relayd_session_id
;
3623 case LTTNG_STREAM_DATA
:
3624 /* Copy received lttcomm socket */
3625 ret
= lttcomm_populate_sock_from_open_socket(
3626 &relayd
->data_sock
.sock
, fd
, relayd_socket_protocol
);
3627 /* Assign version values. */
3628 relayd
->data_sock
.major
= relayd_version_major
;
3629 relayd
->data_sock
.minor
= relayd_version_minor
;
3632 ERR("Unknown relayd socket type (%d)", sock_type
);
3633 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3638 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3642 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3643 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3644 relayd
->net_seq_idx
,
3647 * We gave the ownership of the fd to the relayd structure. Set the
3648 * fd to -1 so we don't call close() on it in the error path below.
3652 /* We successfully added the socket. Send status back. */
3653 ret
= consumer_send_status_msg(sock
, ret_code
);
3655 /* Somehow, the session daemon is not responding anymore. */
3656 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3657 goto error_nosignal
;
3661 * Add relayd socket pair to consumer data hashtable. If object already
3662 * exists or on error, the function gracefully returns.
3671 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3672 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3676 /* Close received socket if valid. */
3679 PERROR("close received socket");
3683 if (relayd_created
) {
3689 * Search for a relayd associated to the session id and return the reference.
3691 * A rcu read side lock MUST be acquire before calling this function and locked
3692 * until the relayd object is no longer necessary.
3694 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3696 struct lttng_ht_iter iter
;
3697 struct consumer_relayd_sock_pair
*relayd
= nullptr;
3699 ASSERT_RCU_READ_LOCKED();
3701 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3702 cds_lfht_for_each_entry (the_consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
, node
.node
) {
3704 * Check by sessiond id which is unique here where the relayd session
3705 * id might not be when having multiple relayd.
3707 if (relayd
->sessiond_session_id
== id
) {
3708 /* Found the relayd. There can be only one per id. */
3720 * Check if for a given session id there is still data needed to be extract
3723 * Return 1 if data is pending or else 0 meaning ready to be read.
3725 int consumer_data_pending(uint64_t id
)
3728 struct lttng_ht_iter iter
;
3729 struct lttng_ht
*ht
;
3730 struct lttng_consumer_stream
*stream
;
3731 struct consumer_relayd_sock_pair
*relayd
= nullptr;
3732 int (*data_pending
)(struct lttng_consumer_stream
*);
3734 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3736 lttng::urcu::read_lock_guard read_lock
;
3737 pthread_mutex_lock(&the_consumer_data
.lock
);
3739 switch (the_consumer_data
.type
) {
3740 case LTTNG_CONSUMER_KERNEL
:
3741 data_pending
= lttng_kconsumer_data_pending
;
3743 case LTTNG_CONSUMER32_UST
:
3744 case LTTNG_CONSUMER64_UST
:
3745 data_pending
= lttng_ustconsumer_data_pending
;
3748 ERR("Unknown consumer data type");
3752 /* Ease our life a bit */
3753 ht
= the_consumer_data
.stream_list_ht
;
3755 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3756 ht
->hash_fct(&id
, lttng_ht_seed
),
3761 node_session_id
.node
)
3763 pthread_mutex_lock(&stream
->lock
);
3766 * A removed node from the hash table indicates that the stream has
3767 * been deleted thus having a guarantee that the buffers are closed
3768 * on the consumer side. However, data can still be transmitted
3769 * over the network so don't skip the relayd check.
3771 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3773 /* Check the stream if there is data in the buffers. */
3774 ret
= data_pending(stream
);
3776 pthread_mutex_unlock(&stream
->lock
);
3781 pthread_mutex_unlock(&stream
->lock
);
3784 relayd
= find_relayd_by_session_id(id
);
3786 unsigned int is_data_inflight
= 0;
3788 /* Send init command for data pending. */
3789 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3790 ret
= relayd_begin_data_pending(&relayd
->control_sock
, relayd
->relayd_session_id
);
3792 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3793 /* Communication error thus the relayd so no data pending. */
3794 goto data_not_pending
;
3797 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3798 ht
->hash_fct(&id
, lttng_ht_seed
),
3803 node_session_id
.node
)
3805 if (stream
->metadata_flag
) {
3806 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3807 stream
->relayd_stream_id
);
3809 ret
= relayd_data_pending(&relayd
->control_sock
,
3810 stream
->relayd_stream_id
,
3811 stream
->next_net_seq_num
- 1);
3815 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3817 } else if (ret
< 0) {
3818 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".",
3819 relayd
->net_seq_idx
);
3820 lttng_consumer_cleanup_relayd(relayd
);
3821 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3822 goto data_not_pending
;
3826 /* Send end command for data pending. */
3827 ret
= relayd_end_data_pending(
3828 &relayd
->control_sock
, relayd
->relayd_session_id
, &is_data_inflight
);
3829 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3831 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".",
3832 relayd
->net_seq_idx
);
3833 lttng_consumer_cleanup_relayd(relayd
);
3834 goto data_not_pending
;
3836 if (is_data_inflight
) {
3842 * Finding _no_ node in the hash table and no inflight data means that the
3843 * stream(s) have been removed thus data is guaranteed to be available for
3844 * analysis from the trace files.
3848 /* Data is available to be read by a viewer. */
3849 pthread_mutex_unlock(&the_consumer_data
.lock
);
3853 /* Data is still being extracted from buffers. */
3854 pthread_mutex_unlock(&the_consumer_data
.lock
);
3859 * Send a ret code status message to the sessiond daemon.
3861 * Return the sendmsg() return value.
3863 int consumer_send_status_msg(int sock
, int ret_code
)
3865 struct lttcomm_consumer_status_msg msg
;
3867 memset(&msg
, 0, sizeof(msg
));
3868 msg
.ret_code
= (lttcomm_return_code
) ret_code
;
3870 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3874 * Send a channel status message to the sessiond daemon.
3876 * Return the sendmsg() return value.
3878 int consumer_send_status_channel(int sock
, struct lttng_consumer_channel
*channel
)
3880 struct lttcomm_consumer_status_channel msg
;
3882 LTTNG_ASSERT(sock
>= 0);
3884 memset(&msg
, 0, sizeof(msg
));
3886 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3888 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3889 msg
.key
= channel
->key
;
3890 msg
.stream_count
= channel
->streams
.count
;
3893 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3896 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3897 unsigned long produced_pos
,
3898 uint64_t nb_packets_per_stream
,
3899 uint64_t max_sb_size
)
3901 unsigned long start_pos
;
3903 if (!nb_packets_per_stream
) {
3904 return consumed_pos
; /* Grab everything */
3906 start_pos
= produced_pos
- lttng_offset_align_floor(produced_pos
, max_sb_size
);
3907 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3908 if ((long) (start_pos
- consumed_pos
) < 0) {
3909 return consumed_pos
; /* Grab everything */
3914 /* Stream lock must be held by the caller. */
3915 static int sample_stream_positions(struct lttng_consumer_stream
*stream
,
3916 unsigned long *produced
,
3917 unsigned long *consumed
)
3921 ASSERT_LOCKED(stream
->lock
);
3923 ret
= lttng_consumer_sample_snapshot_positions(stream
);
3925 ERR("Failed to sample snapshot positions");
3929 ret
= lttng_consumer_get_produced_snapshot(stream
, produced
);
3931 ERR("Failed to sample produced position");
3935 ret
= lttng_consumer_get_consumed_snapshot(stream
, consumed
);
3937 ERR("Failed to sample consumed position");
3946 * Sample the rotate position for all the streams of a channel. If a stream
3947 * is already at the rotate position (produced == consumed), we flag it as
3948 * ready for rotation. The rotation of ready streams occurs after we have
3949 * replied to the session daemon that we have finished sampling the positions.
3950 * Must be called with RCU read-side lock held to ensure existence of channel.
3952 * Returns 0 on success, < 0 on error
3954 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
3959 struct lttng_consumer_stream
*stream
;
3960 struct lttng_ht_iter iter
;
3961 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
3962 struct lttng_dynamic_array stream_rotation_positions
;
3963 uint64_t next_chunk_id
, stream_count
= 0;
3964 enum lttng_trace_chunk_status chunk_status
;
3965 const bool is_local_trace
= relayd_id
== -1ULL;
3966 struct consumer_relayd_sock_pair
*relayd
= nullptr;
3967 bool rotating_to_new_chunk
= true;
3968 /* Array of `struct lttng_consumer_stream *` */
3969 struct lttng_dynamic_pointer_array streams_packet_to_open
;
3972 ASSERT_RCU_READ_LOCKED();
3974 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
3976 lttng_dynamic_array_init(&stream_rotation_positions
,
3977 sizeof(struct relayd_stream_rotation_position
),
3979 lttng_dynamic_pointer_array_init(&streams_packet_to_open
, nullptr);
3981 lttng::urcu::read_lock_guard read_lock
;
3983 pthread_mutex_lock(&channel
->lock
);
3984 LTTNG_ASSERT(channel
->trace_chunk
);
3985 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
, &next_chunk_id
);
3986 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
3988 goto end_unlock_channel
;
3991 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3992 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
3997 node_channel_id
.node
)
3999 unsigned long produced_pos
= 0, consumed_pos
= 0;
4001 health_code_update();
4004 * Lock stream because we are about to change its state.
4006 pthread_mutex_lock(&stream
->lock
);
4008 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
4009 rotating_to_new_chunk
= false;
4013 * Do not flush a packet when rotating from a NULL trace
4014 * chunk. The stream has no means to output data, and the prior
4015 * rotation which rotated to NULL performed that side-effect
4016 * already. No new data can be produced when a stream has no
4017 * associated trace chunk (e.g. a stop followed by a rotate).
4019 if (stream
->trace_chunk
) {
4022 if (stream
->metadata_flag
) {
4024 * Don't produce an empty metadata packet,
4025 * simply close the current one.
4027 * Metadata is regenerated on every trace chunk
4028 * switch; there is no concern that no data was
4031 flush_active
= true;
4034 * Only flush an empty packet if the "packet
4035 * open" could not be performed on transition
4036 * to a new trace chunk and no packets were
4037 * consumed within the chunk's lifetime.
4039 if (stream
->opened_packet_in_current_trace_chunk
) {
4040 flush_active
= true;
4043 * Stream could have been full at the
4044 * time of rotation, but then have had
4045 * no activity at all.
4047 * It is important to flush a packet
4048 * to prevent 0-length files from being
4049 * produced as most viewers choke on
4052 * Unfortunately viewers will not be
4053 * able to know that tracing was active
4054 * for this stream during this trace
4057 ret
= sample_stream_positions(
4058 stream
, &produced_pos
, &consumed_pos
);
4060 goto end_unlock_stream
;
4064 * Don't flush an empty packet if data
4065 * was produced; it will be consumed
4066 * before the rotation completes.
4068 flush_active
= produced_pos
!= consumed_pos
;
4069 if (!flush_active
) {
4070 const char *trace_chunk_name
;
4071 uint64_t trace_chunk_id
;
4073 chunk_status
= lttng_trace_chunk_get_name(
4074 stream
->trace_chunk
,
4077 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NONE
) {
4078 trace_chunk_name
= "none";
4082 * Consumer trace chunks are
4085 chunk_status
= lttng_trace_chunk_get_id(
4086 stream
->trace_chunk
, &trace_chunk_id
);
4087 LTTNG_ASSERT(chunk_status
==
4088 LTTNG_TRACE_CHUNK_STATUS_OK
);
4090 DBG("Unable to open packet for stream during trace chunk's lifetime. "
4091 "Flushing an empty packet to prevent an empty file from being created: "
4092 "stream id = %" PRIu64
4093 ", trace chunk name = `%s`, trace chunk id = %" PRIu64
,
4102 * Close the current packet before sampling the
4103 * ring buffer positions.
4105 ret
= consumer_stream_flush_buffer(stream
, flush_active
);
4107 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4109 goto end_unlock_stream
;
4113 ret
= lttng_consumer_take_snapshot(stream
);
4114 if (ret
< 0 && ret
!= -ENODATA
&& ret
!= -EAGAIN
) {
4115 ERR("Failed to sample snapshot position during channel rotation");
4116 goto end_unlock_stream
;
4119 ret
= lttng_consumer_get_produced_snapshot(stream
, &produced_pos
);
4121 ERR("Failed to sample produced position during channel rotation");
4122 goto end_unlock_stream
;
4125 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos
);
4127 ERR("Failed to sample consumed position during channel rotation");
4128 goto end_unlock_stream
;
4132 * Align produced position on the start-of-packet boundary of the first
4133 * packet going into the next trace chunk.
4135 produced_pos
= lttng_align_floor(produced_pos
, stream
->max_sb_size
);
4136 if (consumed_pos
== produced_pos
) {
4137 DBG("Set rotate ready for stream %" PRIu64
" produced = %lu consumed = %lu",
4141 stream
->rotate_ready
= true;
4143 DBG("Different consumed and produced positions "
4144 "for stream %" PRIu64
" produced = %lu consumed = %lu",
4150 * The rotation position is based on the packet_seq_num of the
4151 * packet following the last packet that was consumed for this
4152 * stream, incremented by the offset between produced and
4153 * consumed positions. This rotation position is a lower bound
4154 * (inclusive) at which the next trace chunk starts. Since it
4155 * is a lower bound, it is OK if the packet_seq_num does not
4156 * correspond exactly to the same packet identified by the
4157 * consumed_pos, which can happen in overwrite mode.
4159 if (stream
->sequence_number_unavailable
) {
4161 * Rotation should never be performed on a session which
4162 * interacts with a pre-2.8 lttng-modules, which does
4163 * not implement packet sequence number.
4165 ERR("Failure to rotate stream %" PRIu64
": sequence number unavailable",
4168 goto end_unlock_stream
;
4170 stream
->rotate_position
= stream
->last_sequence_number
+ 1 +
4171 ((produced_pos
- consumed_pos
) / stream
->max_sb_size
);
4172 DBG("Set rotation position for stream %" PRIu64
" at position %" PRIu64
,
4174 stream
->rotate_position
);
4176 if (!is_local_trace
) {
4178 * The relay daemon control protocol expects a rotation
4179 * position as "the sequence number of the first packet
4180 * _after_ the current trace chunk".
4182 const struct relayd_stream_rotation_position position
= {
4183 .stream_id
= stream
->relayd_stream_id
,
4184 .rotate_at_seq_num
= stream
->rotate_position
,
4187 ret
= lttng_dynamic_array_add_element(&stream_rotation_positions
,
4190 ERR("Failed to allocate stream rotation position");
4191 goto end_unlock_stream
;
4196 stream
->opened_packet_in_current_trace_chunk
= false;
4198 if (rotating_to_new_chunk
&& !stream
->metadata_flag
) {
4200 * Attempt to flush an empty packet as close to the
4201 * rotation point as possible. In the event where a
4202 * stream remains inactive after the rotation point,
4203 * this ensures that the new trace chunk has a
4204 * beginning timestamp set at the begining of the
4205 * trace chunk instead of only creating an empty
4206 * packet when the trace chunk is stopped.
4208 * This indicates to the viewers that the stream
4209 * was being recorded, but more importantly it
4210 * allows viewers to determine a useable trace
4213 * This presents a problem in the case where the
4214 * ring-buffer is completely full.
4216 * Consider the following scenario:
4217 * - The consumption of data is slow (slow network,
4219 * - The ring buffer is full,
4220 * - A rotation is initiated,
4221 * - The flush below does nothing (no space left to
4222 * open a new packet),
4223 * - The other streams rotate very soon, and new
4224 * data is produced in the new chunk,
4225 * - This stream completes its rotation long after the
4226 * rotation was initiated
4227 * - The session is stopped before any event can be
4228 * produced in this stream's buffers.
4230 * The resulting trace chunk will have a single packet
4231 * temporaly at the end of the trace chunk for this
4232 * stream making the stream intersection more narrow
4233 * than it should be.
4235 * To work-around this, an empty flush is performed
4236 * after the first consumption of a packet during a
4237 * rotation if open_packet fails. The idea is that
4238 * consuming a packet frees enough space to switch
4239 * packets in this scenario and allows the tracer to
4240 * "stamp" the beginning of the new trace chunk at the
4241 * earliest possible point.
4243 * The packet open is performed after the channel
4244 * rotation to ensure that no attempt to open a packet
4245 * is performed in a stream that has no active trace
4248 ret
= lttng_dynamic_pointer_array_add_pointer(&streams_packet_to_open
,
4251 PERROR("Failed to add a stream pointer to array of streams in which to open a packet");
4253 goto end_unlock_stream
;
4257 pthread_mutex_unlock(&stream
->lock
);
4261 if (!is_local_trace
) {
4262 relayd
= consumer_find_relayd(relayd_id
);
4264 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4266 goto end_unlock_channel
;
4269 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4270 ret
= relayd_rotate_streams(&relayd
->control_sock
,
4272 rotating_to_new_chunk
? &next_chunk_id
: nullptr,
4273 (const struct relayd_stream_rotation_position
*)
4274 stream_rotation_positions
.buffer
.data
);
4275 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4277 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4278 relayd
->net_seq_idx
);
4279 lttng_consumer_cleanup_relayd(relayd
);
4280 goto end_unlock_channel
;
4284 for (stream_idx
= 0;
4285 stream_idx
< lttng_dynamic_pointer_array_get_count(&streams_packet_to_open
);
4287 enum consumer_stream_open_packet_status status
;
4289 stream
= (lttng_consumer_stream
*) lttng_dynamic_pointer_array_get_pointer(
4290 &streams_packet_to_open
, stream_idx
);
4292 pthread_mutex_lock(&stream
->lock
);
4293 status
= consumer_stream_open_packet(stream
);
4294 pthread_mutex_unlock(&stream
->lock
);
4296 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
4297 DBG("Opened a packet after a rotation: stream id = %" PRIu64
4298 ", channel name = %s, session id = %" PRIu64
,
4301 stream
->chan
->session_id
);
4303 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
4305 * Can't open a packet as there is no space left
4306 * in the buffer. A new packet will be opened
4307 * once one has been consumed.
4309 DBG("No space left to open a packet after a rotation: stream id = %" PRIu64
4310 ", channel name = %s, session id = %" PRIu64
,
4313 stream
->chan
->session_id
);
4315 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
4316 /* Logged by callee. */
4318 goto end_unlock_channel
;
4324 pthread_mutex_unlock(&channel
->lock
);
4329 pthread_mutex_unlock(&stream
->lock
);
4331 pthread_mutex_unlock(&channel
->lock
);
4333 lttng_dynamic_array_reset(&stream_rotation_positions
);
4334 lttng_dynamic_pointer_array_reset(&streams_packet_to_open
);
4338 static int consumer_clear_buffer(struct lttng_consumer_stream
*stream
)
4341 unsigned long consumed_pos_before
, consumed_pos_after
;
4343 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4345 ERR("Taking snapshot positions");
4349 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_before
);
4351 ERR("Consumed snapshot position");
4355 switch (the_consumer_data
.type
) {
4356 case LTTNG_CONSUMER_KERNEL
:
4357 ret
= kernctl_buffer_clear(stream
->wait_fd
);
4359 ERR("Failed to clear kernel stream (ret = %d)", ret
);
4363 case LTTNG_CONSUMER32_UST
:
4364 case LTTNG_CONSUMER64_UST
:
4365 ret
= lttng_ustconsumer_clear_buffer(stream
);
4367 ERR("Failed to clear ust stream (ret = %d)", ret
);
4372 ERR("Unknown consumer_data type");
4376 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4378 ERR("Taking snapshot positions");
4381 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_after
);
4383 ERR("Consumed snapshot position");
4386 DBG("clear: before: %lu after: %lu", consumed_pos_before
, consumed_pos_after
);
4391 static int consumer_clear_stream(struct lttng_consumer_stream
*stream
)
4395 ret
= consumer_stream_flush_buffer(stream
, true);
4397 ERR("Failed to flush stream %" PRIu64
" during channel clear", stream
->key
);
4398 ret
= LTTCOMM_CONSUMERD_FATAL
;
4402 ret
= consumer_clear_buffer(stream
);
4404 ERR("Failed to clear stream %" PRIu64
" during channel clear", stream
->key
);
4405 ret
= LTTCOMM_CONSUMERD_FATAL
;
4409 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4414 static int consumer_clear_unmonitored_channel(struct lttng_consumer_channel
*channel
)
4417 struct lttng_consumer_stream
*stream
;
4419 lttng::urcu::read_lock_guard read_lock
;
4420 pthread_mutex_lock(&channel
->lock
);
4421 cds_list_for_each_entry (stream
, &channel
->streams
.head
, send_node
) {
4422 health_code_update();
4423 pthread_mutex_lock(&stream
->lock
);
4424 ret
= consumer_clear_stream(stream
);
4428 pthread_mutex_unlock(&stream
->lock
);
4430 pthread_mutex_unlock(&channel
->lock
);
4434 pthread_mutex_unlock(&stream
->lock
);
4435 pthread_mutex_unlock(&channel
->lock
);
4440 * Check if a stream is ready to be rotated after extracting it.
4442 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4443 * error. Stream lock must be held.
4445 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4447 DBG("Check is rotate ready for stream %" PRIu64
" ready %u rotate_position %" PRIu64
4448 " last_sequence_number %" PRIu64
,
4450 stream
->rotate_ready
,
4451 stream
->rotate_position
,
4452 stream
->last_sequence_number
);
4453 if (stream
->rotate_ready
) {
4458 * If packet seq num is unavailable, it means we are interacting
4459 * with a pre-2.8 lttng-modules which does not implement the
4460 * sequence number. Rotation should never be used by sessiond in this
4463 if (stream
->sequence_number_unavailable
) {
4464 ERR("Internal error: rotation used on stream %" PRIu64
4465 " with unavailable sequence number",
4470 if (stream
->rotate_position
== -1ULL || stream
->last_sequence_number
== -1ULL) {
4475 * Rotate position not reached yet. The stream rotate position is
4476 * the position of the next packet belonging to the next trace chunk,
4477 * but consumerd considers rotation ready when reaching the last
4478 * packet of the current chunk, hence the "rotate_position - 1".
4481 DBG("Check is rotate ready for stream %" PRIu64
" last_sequence_number %" PRIu64
4482 " rotate_position %" PRIu64
,
4484 stream
->last_sequence_number
,
4485 stream
->rotate_position
);
4486 if (stream
->last_sequence_number
>= stream
->rotate_position
- 1) {
4494 * Reset the state for a stream after a rotation occurred.
4496 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4498 DBG("lttng_consumer_reset_stream_rotate_state for stream %" PRIu64
, stream
->key
);
4499 stream
->rotate_position
= -1ULL;
4500 stream
->rotate_ready
= false;
4504 * Perform the rotation a local stream file.
4506 static int rotate_local_stream(struct lttng_consumer_stream
*stream
)
4510 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4513 stream
->tracefile_size_current
= 0;
4514 stream
->tracefile_count_current
= 0;
4516 if (stream
->out_fd
>= 0) {
4517 ret
= close(stream
->out_fd
);
4519 PERROR("Failed to close stream out_fd of channel \"%s\"",
4520 stream
->chan
->name
);
4522 stream
->out_fd
= -1;
4525 if (stream
->index_file
) {
4526 lttng_index_file_put(stream
->index_file
);
4527 stream
->index_file
= nullptr;
4530 if (!stream
->trace_chunk
) {
4534 ret
= consumer_stream_create_output_files(stream
, true);
4540 * Performs the stream rotation for the rotate session feature if needed.
4541 * It must be called with the channel and stream locks held.
4543 * Return 0 on success, a negative number of error.
4545 int lttng_consumer_rotate_stream(struct lttng_consumer_stream
*stream
)
4549 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4552 * Update the stream's 'current' chunk to the session's (channel)
4553 * now-current chunk.
4555 lttng_trace_chunk_put(stream
->trace_chunk
);
4556 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4558 * A channel can be rotated and not have a "next" chunk
4559 * to transition to. In that case, the channel's "current chunk"
4560 * has not been closed yet, but it has not been updated to
4561 * a "next" trace chunk either. Hence, the stream, like its
4562 * parent channel, becomes part of no chunk and can't output
4563 * anything until a new trace chunk is created.
4565 stream
->trace_chunk
= nullptr;
4566 } else if (stream
->chan
->trace_chunk
&& !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4567 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4572 * Update the stream's trace chunk to its parent channel's
4573 * current trace chunk.
4575 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4578 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4579 ret
= rotate_local_stream(stream
);
4581 ERR("Failed to rotate stream, ret = %i", ret
);
4586 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4588 * If the stream has transitioned to a new trace
4589 * chunk, the metadata should be re-dumped to the
4592 * However, it is possible for a stream to transition to
4593 * a "no-chunk" state. This can happen if a rotation
4594 * occurs on an inactive session. In such cases, the metadata
4595 * regeneration will happen when the next trace chunk is
4598 ret
= consumer_metadata_stream_dump(stream
);
4603 lttng_consumer_reset_stream_rotate_state(stream
);
4612 * Rotate all the ready streams now.
4614 * This is especially important for low throughput streams that have already
4615 * been consumed, we cannot wait for their next packet to perform the
4617 * Need to be called with RCU read-side lock held to ensure existence of
4620 * Returns 0 on success, < 0 on error
4622 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
, uint64_t key
)
4625 struct lttng_consumer_stream
*stream
;
4626 struct lttng_ht_iter iter
;
4627 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
4629 ASSERT_RCU_READ_LOCKED();
4631 lttng::urcu::read_lock_guard read_lock
;
4633 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4635 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4636 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4641 node_channel_id
.node
)
4643 health_code_update();
4645 pthread_mutex_lock(&stream
->chan
->lock
);
4646 pthread_mutex_lock(&stream
->lock
);
4648 if (!stream
->rotate_ready
) {
4649 pthread_mutex_unlock(&stream
->lock
);
4650 pthread_mutex_unlock(&stream
->chan
->lock
);
4653 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4655 ret
= lttng_consumer_rotate_stream(stream
);
4656 pthread_mutex_unlock(&stream
->lock
);
4657 pthread_mutex_unlock(&stream
->chan
->lock
);
4669 enum lttcomm_return_code
lttng_consumer_init_command(struct lttng_consumer_local_data
*ctx
,
4670 const lttng_uuid
& sessiond_uuid
)
4672 enum lttcomm_return_code ret
;
4673 char uuid_str
[LTTNG_UUID_STR_LEN
];
4675 if (ctx
->sessiond_uuid
.is_set
) {
4676 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4680 ctx
->sessiond_uuid
.is_set
= true;
4681 ctx
->sessiond_uuid
.value
= sessiond_uuid
;
4682 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4683 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4684 DBG("Received session daemon UUID: %s", uuid_str
);
4689 enum lttcomm_return_code
4690 lttng_consumer_create_trace_chunk(const uint64_t *relayd_id
,
4691 uint64_t session_id
,
4693 time_t chunk_creation_timestamp
,
4694 const char *chunk_override_name
,
4695 const struct lttng_credentials
*credentials
,
4696 struct lttng_directory_handle
*chunk_directory_handle
)
4699 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4700 struct lttng_trace_chunk
*created_chunk
= nullptr, *published_chunk
= nullptr;
4701 enum lttng_trace_chunk_status chunk_status
;
4702 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4703 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4704 const char *relayd_id_str
= "(none)";
4705 const char *creation_timestamp_str
;
4706 struct lttng_ht_iter iter
;
4707 struct lttng_consumer_channel
*channel
;
4710 /* Only used for logging purposes. */
4711 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
), "%" PRIu64
, *relayd_id
);
4712 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4713 relayd_id_str
= relayd_id_buffer
;
4715 relayd_id_str
= "(formatting error)";
4719 /* Local protocol error. */
4720 LTTNG_ASSERT(chunk_creation_timestamp
);
4721 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4722 creation_timestamp_buffer
,
4723 sizeof(creation_timestamp_buffer
));
4724 creation_timestamp_str
= !ret
? creation_timestamp_buffer
: "(formatting error)";
4726 DBG("Consumer create trace chunk command: relay_id = %s"
4727 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
", chunk_override_name = %s"
4728 ", chunk_creation_timestamp = %s",
4732 chunk_override_name
?: "(none)",
4733 creation_timestamp_str
);
4736 * The trace chunk registry, as used by the consumer daemon, implicitly
4737 * owns the trace chunks. This is only needed in the consumer since
4738 * the consumer has no notion of a session beyond session IDs being
4739 * used to identify other objects.
4741 * The lttng_trace_chunk_registry_publish() call below provides a
4742 * reference which is not released; it implicitly becomes the session
4743 * daemon's reference to the chunk in the consumer daemon.
4745 * The lifetime of trace chunks in the consumer daemon is managed by
4746 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4747 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4749 created_chunk
= lttng_trace_chunk_create(chunk_id
, chunk_creation_timestamp
, nullptr);
4750 if (!created_chunk
) {
4751 ERR("Failed to create trace chunk");
4752 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4756 if (chunk_override_name
) {
4757 chunk_status
= lttng_trace_chunk_override_name(created_chunk
, chunk_override_name
);
4758 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4759 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4764 if (chunk_directory_handle
) {
4765 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
, credentials
);
4766 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4767 ERR("Failed to set trace chunk credentials");
4768 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4772 * The consumer daemon has no ownership of the chunk output
4775 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
, chunk_directory_handle
);
4776 chunk_directory_handle
= nullptr;
4777 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4778 ERR("Failed to set trace chunk's directory handle");
4779 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4784 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4785 the_consumer_data
.chunk_registry
, session_id
, created_chunk
);
4786 lttng_trace_chunk_put(created_chunk
);
4787 created_chunk
= nullptr;
4788 if (!published_chunk
) {
4789 ERR("Failed to publish trace chunk");
4790 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4795 lttng::urcu::read_lock_guard read_lock
;
4796 cds_lfht_for_each_entry_duplicate(
4797 the_consumer_data
.channels_by_session_id_ht
->ht
,
4798 the_consumer_data
.channels_by_session_id_ht
->hash_fct(&session_id
,
4800 the_consumer_data
.channels_by_session_id_ht
->match_fct
,
4804 channels_by_session_id_ht_node
.node
)
4806 ret
= lttng_consumer_channel_set_trace_chunk(channel
, published_chunk
);
4809 * Roll-back the creation of this chunk.
4811 * This is important since the session daemon will
4812 * assume that the creation of this chunk failed and
4813 * will never ask for it to be closed, resulting
4814 * in a leak and an inconsistent state for some
4817 enum lttcomm_return_code close_ret
;
4818 char path
[LTTNG_PATH_MAX
];
4820 DBG("Failed to set new trace chunk on existing channels, rolling back");
4822 lttng_consumer_close_trace_chunk(relayd_id
,
4825 chunk_creation_timestamp
,
4828 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4829 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
4830 ", chunk_id = %" PRIu64
,
4835 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4842 struct consumer_relayd_sock_pair
*relayd
;
4844 relayd
= consumer_find_relayd(*relayd_id
);
4846 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4847 ret
= relayd_create_trace_chunk(&relayd
->control_sock
, published_chunk
);
4848 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4850 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4853 if (!relayd
|| ret
) {
4854 enum lttcomm_return_code close_ret
;
4855 char path
[LTTNG_PATH_MAX
];
4857 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4860 chunk_creation_timestamp
,
4863 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4864 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
4865 ", chunk_id = %" PRIu64
,
4870 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4876 /* Release the reference returned by the "publish" operation. */
4877 lttng_trace_chunk_put(published_chunk
);
4878 lttng_trace_chunk_put(created_chunk
);
4882 enum lttcomm_return_code
4883 lttng_consumer_close_trace_chunk(const uint64_t *relayd_id
,
4884 uint64_t session_id
,
4886 time_t chunk_close_timestamp
,
4887 const enum lttng_trace_chunk_command_type
*close_command
,
4890 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4891 struct lttng_trace_chunk
*chunk
;
4892 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4893 const char *relayd_id_str
= "(none)";
4894 const char *close_command_name
= "none";
4895 struct lttng_ht_iter iter
;
4896 struct lttng_consumer_channel
*channel
;
4897 enum lttng_trace_chunk_status chunk_status
;
4902 /* Only used for logging purposes. */
4903 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
), "%" PRIu64
, *relayd_id
);
4904 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4905 relayd_id_str
= relayd_id_buffer
;
4907 relayd_id_str
= "(formatting error)";
4910 if (close_command
) {
4911 close_command_name
= lttng_trace_chunk_command_type_get_name(*close_command
);
4914 DBG("Consumer close trace chunk command: relayd_id = %s"
4915 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
", close command = %s",
4919 close_command_name
);
4921 chunk
= lttng_trace_chunk_registry_find_chunk(
4922 the_consumer_data
.chunk_registry
, session_id
, chunk_id
);
4924 ERR("Failed to find chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4927 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4931 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
, chunk_close_timestamp
);
4932 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4933 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4937 if (close_command
) {
4938 chunk_status
= lttng_trace_chunk_set_close_command(chunk
, *close_command
);
4939 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4940 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4946 * chunk is now invalid to access as we no longer hold a reference to
4947 * it; it is only kept around to compare it (by address) to the
4948 * current chunk found in the session's channels.
4951 lttng::urcu::read_lock_guard read_lock
;
4952 cds_lfht_for_each_entry (
4953 the_consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
, node
.node
) {
4957 * Only change the channel's chunk to NULL if it still
4958 * references the chunk being closed. The channel may
4959 * reference a newer channel in the case of a session
4960 * rotation. When a session rotation occurs, the "next"
4961 * chunk is created before the "current" chunk is closed.
4963 if (channel
->trace_chunk
!= chunk
) {
4966 ret
= lttng_consumer_channel_set_trace_chunk(channel
, nullptr);
4969 * Attempt to close the chunk on as many channels as
4972 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4978 struct consumer_relayd_sock_pair
*relayd
;
4980 relayd
= consumer_find_relayd(*relayd_id
);
4982 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4983 ret
= relayd_close_trace_chunk(&relayd
->control_sock
, chunk
, path
);
4984 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4986 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4989 if (!relayd
|| ret
) {
4990 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4997 * Release the reference returned by the "find" operation and
4998 * the session daemon's implicit reference to the chunk.
5000 lttng_trace_chunk_put(chunk
);
5001 lttng_trace_chunk_put(chunk
);
5006 enum lttcomm_return_code
5007 lttng_consumer_trace_chunk_exists(const uint64_t *relayd_id
, uint64_t session_id
, uint64_t chunk_id
)
5010 enum lttcomm_return_code ret_code
;
5011 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
5012 const char *relayd_id_str
= "(none)";
5013 const bool is_local_trace
= !relayd_id
;
5014 struct consumer_relayd_sock_pair
*relayd
= nullptr;
5015 bool chunk_exists_local
, chunk_exists_remote
;
5016 lttng::urcu::read_lock_guard read_lock
;
5019 /* Only used for logging purposes. */
5020 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
), "%" PRIu64
, *relayd_id
);
5021 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
5022 relayd_id_str
= relayd_id_buffer
;
5024 relayd_id_str
= "(formatting error)";
5028 DBG("Consumer trace chunk exists command: relayd_id = %s"
5029 ", chunk_id = %" PRIu64
,
5032 ret
= lttng_trace_chunk_registry_chunk_exists(
5033 the_consumer_data
.chunk_registry
, session_id
, chunk_id
, &chunk_exists_local
);
5035 /* Internal error. */
5036 ERR("Failed to query the existence of a trace chunk");
5037 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
5040 DBG("Trace chunk %s locally", chunk_exists_local
? "exists" : "does not exist");
5041 if (chunk_exists_local
) {
5042 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
5044 } else if (is_local_trace
) {
5045 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5049 relayd
= consumer_find_relayd(*relayd_id
);
5051 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
5052 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5053 goto end_rcu_unlock
;
5055 DBG("Looking up existence of trace chunk on relay daemon");
5056 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5057 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
, &chunk_exists_remote
);
5058 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5060 ERR("Failed to look-up the existence of trace chunk on relay daemon");
5061 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
5062 goto end_rcu_unlock
;
5065 ret_code
= chunk_exists_remote
? LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
5066 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5067 DBG("Trace chunk %s on relay daemon", chunk_exists_remote
? "exists" : "does not exist");
5074 static int consumer_clear_monitored_channel(struct lttng_consumer_channel
*channel
)
5076 struct lttng_ht
*ht
;
5077 struct lttng_consumer_stream
*stream
;
5078 struct lttng_ht_iter iter
;
5081 ht
= the_consumer_data
.stream_per_chan_id_ht
;
5083 lttng::urcu::read_lock_guard read_lock
;
5084 cds_lfht_for_each_entry_duplicate(ht
->ht
,
5085 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
5090 node_channel_id
.node
)
5093 * Protect against teardown with mutex.
5095 pthread_mutex_lock(&stream
->lock
);
5096 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5099 ret
= consumer_clear_stream(stream
);
5104 pthread_mutex_unlock(&stream
->lock
);
5106 return LTTCOMM_CONSUMERD_SUCCESS
;
5109 pthread_mutex_unlock(&stream
->lock
);
5113 int lttng_consumer_clear_channel(struct lttng_consumer_channel
*channel
)
5117 DBG("Consumer clear channel %" PRIu64
, channel
->key
);
5119 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
5121 * Nothing to do for the metadata channel/stream.
5122 * Snapshot mechanism already take care of the metadata
5123 * handling/generation, and monitored channels only need to
5124 * have their data stream cleared..
5126 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5130 if (!channel
->monitor
) {
5131 ret
= consumer_clear_unmonitored_channel(channel
);
5133 ret
= consumer_clear_monitored_channel(channel
);
5139 enum lttcomm_return_code
lttng_consumer_open_channel_packets(struct lttng_consumer_channel
*channel
)
5141 struct lttng_consumer_stream
*stream
;
5142 enum lttcomm_return_code ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5144 if (channel
->metadata_stream
) {
5145 ERR("Open channel packets command attempted on a metadata channel");
5146 ret
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5151 lttng::urcu::read_lock_guard read_lock
;
5152 cds_list_for_each_entry (stream
, &channel
->streams
.head
, send_node
) {
5153 enum consumer_stream_open_packet_status status
;
5155 pthread_mutex_lock(&stream
->lock
);
5156 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5160 status
= consumer_stream_open_packet(stream
);
5162 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
5163 DBG("Opened a packet in \"open channel packets\" command: stream id = %" PRIu64
5164 ", channel name = %s, session id = %" PRIu64
,
5167 stream
->chan
->session_id
);
5168 stream
->opened_packet_in_current_trace_chunk
= true;
5170 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
5171 DBG("No space left to open a packet in \"open channel packets\" command: stream id = %" PRIu64
5172 ", channel name = %s, session id = %" PRIu64
,
5175 stream
->chan
->session_id
);
5177 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
5179 * Only unexpected internal errors can lead to this
5180 * failing. Report an unknown error.
5182 ERR("Failed to flush empty buffer in \"open channel packets\" command: stream id = %" PRIu64
5183 ", channel id = %" PRIu64
", channel name = %s"
5184 ", session id = %" PRIu64
,
5188 channel
->session_id
);
5189 ret
= LTTCOMM_CONSUMERD_UNKNOWN_ERROR
;
5196 pthread_mutex_unlock(&stream
->lock
);
5204 pthread_mutex_unlock(&stream
->lock
);
5205 goto end_rcu_unlock
;
5208 void lttng_consumer_sigbus_handle(void *addr
)
5210 lttng_ustconsumer_sigbus_handle(addr
);