2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
11 #include <common/align.hpp>
12 #include <common/common.hpp>
13 #include <common/compat/endian.hpp>
14 #include <common/compat/poll.hpp>
15 #include <common/consumer/consumer-metadata-cache.hpp>
16 #include <common/consumer/consumer-stream.hpp>
17 #include <common/consumer/consumer-testpoint.hpp>
18 #include <common/consumer/consumer-timer.hpp>
19 #include <common/consumer/consumer.hpp>
20 #include <common/dynamic-array.hpp>
21 #include <common/index/ctf-index.hpp>
22 #include <common/index/index.hpp>
23 #include <common/kernel-consumer/kernel-consumer.hpp>
24 #include <common/kernel-ctl/kernel-ctl.hpp>
25 #include <common/relayd/relayd.hpp>
26 #include <common/sessiond-comm/relayd.hpp>
27 #include <common/sessiond-comm/sessiond-comm.hpp>
28 #include <common/string-utils/format.hpp>
29 #include <common/time.hpp>
30 #include <common/trace-chunk-registry.hpp>
31 #include <common/trace-chunk.hpp>
32 #include <common/urcu.hpp>
33 #include <common/ust-consumer/ust-consumer.hpp>
34 #include <common/utils.hpp>
36 #include <bin/lttng-consumerd/health-consumerd.hpp>
44 #include <sys/socket.h>
45 #include <sys/types.h>
48 lttng_consumer_global_data the_consumer_data
;
50 enum consumer_channel_action
{
53 CONSUMER_CHANNEL_QUIT
,
57 struct consumer_channel_msg
{
58 enum consumer_channel_action action
;
59 struct lttng_consumer_channel
*chan
; /* add */
60 uint64_t key
; /* del */
64 * Global hash table containing respectively metadata and data streams. The
65 * stream element in this ht should only be updated by the metadata poll thread
66 * for the metadata and the data poll thread for the data.
68 struct lttng_ht
*metadata_ht
;
69 struct lttng_ht
*data_ht
;
72 /* Flag used to temporarily pause data consumption from testpoints. */
73 int data_consumption_paused
;
76 * Flag to inform the polling thread to quit when all fd hung up. Updated by
77 * the consumer_thread_receive_fds when it notices that all fds has hung up.
78 * Also updated by the signal handler (consumer_should_exit()). Read by the
83 static const char *get_consumer_domain()
85 switch (the_consumer_data
.type
) {
86 case LTTNG_CONSUMER_KERNEL
:
87 return DEFAULT_KERNEL_TRACE_DIR
;
88 case LTTNG_CONSUMER64_UST
:
90 case LTTNG_CONSUMER32_UST
:
91 return DEFAULT_UST_TRACE_DIR
;
98 * Notify a thread lttng pipe to poll back again. This usually means that some
99 * global state has changed so we just send back the thread in a poll wait
102 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
104 struct lttng_consumer_stream
*null_stream
= nullptr;
108 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
)); /* NOLINT sizeof used on a
112 static void notify_health_quit_pipe(int *pipe
)
116 ret
= lttng_write(pipe
[1], "4", 1);
118 PERROR("write consumer health quit");
122 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
123 struct lttng_consumer_channel
*chan
,
125 enum consumer_channel_action action
)
127 struct consumer_channel_msg msg
;
130 memset(&msg
, 0, sizeof(msg
));
135 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
136 if (ret
< sizeof(msg
)) {
137 PERROR("notify_channel_pipe write error");
141 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
143 notify_channel_pipe(ctx
, nullptr, key
, CONSUMER_CHANNEL_DEL
);
146 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
147 struct lttng_consumer_channel
**chan
,
149 enum consumer_channel_action
*action
)
151 struct consumer_channel_msg msg
;
154 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
155 if (ret
< sizeof(msg
)) {
159 *action
= msg
.action
;
167 * Cleanup the stream list of a channel. Those streams are not yet globally
170 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
172 struct lttng_consumer_stream
*stream
, *stmp
;
174 LTTNG_ASSERT(channel
);
176 /* Delete streams that might have been left in the stream list. */
177 cds_list_for_each_entry_safe (stream
, stmp
, &channel
->streams
.head
, send_node
) {
179 * Once a stream is added to this list, the buffers were created so we
180 * have a guarantee that this call will succeed. Setting the monitor
181 * mode to 0 so we don't lock nor try to delete the stream from the
185 consumer_stream_destroy(stream
, nullptr);
190 * Find a stream. The consumer_data.lock must be locked during this
193 static struct lttng_consumer_stream
*find_stream(uint64_t key
, struct lttng_ht
*ht
)
195 struct lttng_ht_iter iter
;
196 struct lttng_ht_node_u64
*node
;
197 struct lttng_consumer_stream
*stream
= nullptr;
201 /* -1ULL keys are lookup failures */
202 if (key
== (uint64_t) -1ULL) {
206 lttng::urcu::read_lock_guard read_lock
;
208 lttng_ht_lookup(ht
, &key
, &iter
);
209 node
= lttng_ht_iter_get_node_u64(&iter
);
210 if (node
!= nullptr) {
211 stream
= lttng::utils::container_of(node
, <tng_consumer_stream::node
);
217 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
219 struct lttng_consumer_stream
*stream
;
221 lttng::urcu::read_lock_guard read_lock
;
222 stream
= find_stream(key
, ht
);
224 stream
->key
= (uint64_t) -1ULL;
226 * We don't want the lookup to match, but we still need
227 * to iterate on this stream when iterating over the hash table. Just
228 * change the node key.
230 stream
->node
.key
= (uint64_t) -1ULL;
235 * Return a channel object for the given key.
237 * RCU read side lock MUST be acquired before calling this function and
238 * protects the channel ptr.
240 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
242 struct lttng_ht_iter iter
;
243 struct lttng_ht_node_u64
*node
;
244 struct lttng_consumer_channel
*channel
= nullptr;
246 ASSERT_RCU_READ_LOCKED();
248 /* -1ULL keys are lookup failures */
249 if (key
== (uint64_t) -1ULL) {
253 lttng_ht_lookup(the_consumer_data
.channel_ht
, &key
, &iter
);
254 node
= lttng_ht_iter_get_node_u64(&iter
);
255 if (node
!= nullptr) {
256 channel
= lttng::utils::container_of(node
, <tng_consumer_channel::node
);
263 * There is a possibility that the consumer does not have enough time between
264 * the close of the channel on the session daemon and the cleanup in here thus
265 * once we have a channel add with an existing key, we know for sure that this
266 * channel will eventually get cleaned up by all streams being closed.
268 * This function just nullifies the already existing channel key.
270 static void steal_channel_key(uint64_t key
)
272 struct lttng_consumer_channel
*channel
;
274 lttng::urcu::read_lock_guard read_lock
;
275 channel
= consumer_find_channel(key
);
277 channel
->key
= (uint64_t) -1ULL;
279 * We don't want the lookup to match, but we still need to iterate on
280 * this channel when iterating over the hash table. Just change the
283 channel
->node
.key
= (uint64_t) -1ULL;
287 static void free_channel_rcu(struct rcu_head
*head
)
289 struct lttng_ht_node_u64
*node
= lttng::utils::container_of(head
, <tng_ht_node_u64::head
);
290 struct lttng_consumer_channel
*channel
=
291 lttng::utils::container_of(node
, <tng_consumer_channel::node
);
293 switch (the_consumer_data
.type
) {
294 case LTTNG_CONSUMER_KERNEL
:
296 case LTTNG_CONSUMER32_UST
:
297 case LTTNG_CONSUMER64_UST
:
298 lttng_ustconsumer_free_channel(channel
);
301 ERR("Unknown consumer_data type");
308 * RCU protected relayd socket pair free.
310 static void free_relayd_rcu(struct rcu_head
*head
)
312 struct lttng_ht_node_u64
*node
= lttng::utils::container_of(head
, <tng_ht_node_u64::head
);
313 struct consumer_relayd_sock_pair
*relayd
=
314 lttng::utils::container_of(node
, &consumer_relayd_sock_pair::node
);
317 * Close all sockets. This is done in the call RCU since we don't want the
318 * socket fds to be reassigned thus potentially creating bad state of the
321 * We do not have to lock the control socket mutex here since at this stage
322 * there is no one referencing to this relayd object.
324 (void) relayd_close(&relayd
->control_sock
);
325 (void) relayd_close(&relayd
->data_sock
);
327 pthread_mutex_destroy(&relayd
->ctrl_sock_mutex
);
332 * Destroy and free relayd socket pair object.
334 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
337 struct lttng_ht_iter iter
;
339 if (relayd
== nullptr) {
343 DBG("Consumer destroy and close relayd socket pair");
345 iter
.iter
.node
= &relayd
->node
.node
;
346 ret
= lttng_ht_del(the_consumer_data
.relayd_ht
, &iter
);
348 /* We assume the relayd is being or is destroyed */
352 /* RCU free() call */
353 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
357 * Remove a channel from the global list protected by a mutex. This function is
358 * also responsible for freeing its data structures.
360 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
362 struct lttng_ht_iter iter
;
364 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
366 pthread_mutex_lock(&the_consumer_data
.lock
);
367 pthread_mutex_lock(&channel
->lock
);
369 /* Destroy streams that might have been left in the stream list. */
370 clean_channel_stream_list(channel
);
372 if (channel
->live_timer_enabled
== 1) {
373 consumer_timer_live_stop(channel
);
375 if (channel
->monitor_timer_enabled
== 1) {
376 consumer_timer_monitor_stop(channel
);
380 * Send a last buffer statistics sample to the session daemon
381 * to ensure it tracks the amount of data consumed by this channel.
383 sample_and_send_channel_buffer_stats(channel
);
385 switch (the_consumer_data
.type
) {
386 case LTTNG_CONSUMER_KERNEL
:
388 case LTTNG_CONSUMER32_UST
:
389 case LTTNG_CONSUMER64_UST
:
390 lttng_ustconsumer_del_channel(channel
);
393 ERR("Unknown consumer_data type");
398 lttng_trace_chunk_put(channel
->trace_chunk
);
399 channel
->trace_chunk
= nullptr;
401 if (channel
->is_published
) {
404 lttng::urcu::read_lock_guard read_lock
;
405 iter
.iter
.node
= &channel
->node
.node
;
406 ret
= lttng_ht_del(the_consumer_data
.channel_ht
, &iter
);
409 iter
.iter
.node
= &channel
->channels_by_session_id_ht_node
.node
;
410 ret
= lttng_ht_del(the_consumer_data
.channels_by_session_id_ht
, &iter
);
414 channel
->is_deleted
= true;
415 call_rcu(&channel
->node
.head
, free_channel_rcu
);
417 pthread_mutex_unlock(&channel
->lock
);
418 pthread_mutex_unlock(&the_consumer_data
.lock
);
422 * Iterate over the relayd hash table and destroy each element. Finally,
423 * destroy the whole hash table.
425 static void cleanup_relayd_ht()
427 struct lttng_ht_iter iter
;
428 struct consumer_relayd_sock_pair
*relayd
;
431 lttng::urcu::read_lock_guard read_lock
;
433 cds_lfht_for_each_entry (
434 the_consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
, node
.node
) {
435 consumer_destroy_relayd(relayd
);
439 lttng_ht_destroy(the_consumer_data
.relayd_ht
);
443 * Update the end point status of all streams having the given network sequence
444 * index (relayd index).
446 * It's atomically set without having the stream mutex locked which is fine
447 * because we handle the write/read race with a pipe wakeup for each thread.
449 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
450 enum consumer_endpoint_status status
)
452 struct lttng_ht_iter iter
;
453 struct lttng_consumer_stream
*stream
;
455 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
457 lttng::urcu::read_lock_guard read_lock
;
459 /* Let's begin with metadata */
460 cds_lfht_for_each_entry (metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
461 if (stream
->net_seq_idx
== net_seq_idx
) {
462 uatomic_set(&stream
->endpoint_status
, status
);
463 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
467 /* Follow up by the data streams */
468 cds_lfht_for_each_entry (data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
469 if (stream
->net_seq_idx
== net_seq_idx
) {
470 uatomic_set(&stream
->endpoint_status
, status
);
471 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
477 * Cleanup a relayd object by flagging every associated streams for deletion,
478 * destroying the object meaning removing it from the relayd hash table,
479 * closing the sockets and freeing the memory in a RCU call.
481 * If a local data context is available, notify the threads that the streams'
482 * state have changed.
484 void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
)
488 LTTNG_ASSERT(relayd
);
490 DBG("Cleaning up relayd object ID %" PRIu64
, relayd
->net_seq_idx
);
492 /* Save the net sequence index before destroying the object */
493 netidx
= relayd
->net_seq_idx
;
496 * Delete the relayd from the relayd hash table, close the sockets and free
497 * the object in a RCU call.
499 consumer_destroy_relayd(relayd
);
501 /* Set inactive endpoint to all streams */
502 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
505 * With a local data context, notify the threads that the streams' state
506 * have changed. The write() action on the pipe acts as an "implicit"
507 * memory barrier ordering the updates of the end point status from the
508 * read of this status which happens AFTER receiving this notify.
510 notify_thread_lttng_pipe(relayd
->ctx
->consumer_data_pipe
);
511 notify_thread_lttng_pipe(relayd
->ctx
->consumer_metadata_pipe
);
515 * Flag a relayd socket pair for destruction. Destroy it if the refcount
518 * RCU read side lock MUST be aquired before calling this function.
520 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
522 LTTNG_ASSERT(relayd
);
523 ASSERT_RCU_READ_LOCKED();
525 /* Set destroy flag for this object */
526 uatomic_set(&relayd
->destroy_flag
, 1);
528 /* Destroy the relayd if refcount is 0 */
529 if (uatomic_read(&relayd
->refcount
) == 0) {
530 consumer_destroy_relayd(relayd
);
535 * Completly destroy stream from every visiable data structure and the given
538 * One this call returns, the stream object is not longer usable nor visible.
540 void consumer_del_stream(struct lttng_consumer_stream
*stream
, struct lttng_ht
*ht
)
542 consumer_stream_destroy(stream
, ht
);
546 * XXX naming of del vs destroy is all mixed up.
548 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
550 consumer_stream_destroy(stream
, data_ht
);
553 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
555 consumer_stream_destroy(stream
, metadata_ht
);
558 void consumer_stream_update_channel_attributes(struct lttng_consumer_stream
*stream
,
559 struct lttng_consumer_channel
*channel
)
561 stream
->channel_read_only_attributes
.tracefile_size
= channel
->tracefile_size
;
565 * Add a stream to the global list protected by a mutex.
567 void consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
569 struct lttng_ht
*ht
= data_ht
;
571 LTTNG_ASSERT(stream
);
574 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
576 pthread_mutex_lock(&the_consumer_data
.lock
);
577 pthread_mutex_lock(&stream
->chan
->lock
);
578 pthread_mutex_lock(&stream
->chan
->timer_lock
);
579 pthread_mutex_lock(&stream
->lock
);
580 lttng::urcu::read_lock_guard read_lock
;
582 /* Steal stream identifier to avoid having streams with the same key */
583 steal_stream_key(stream
->key
, ht
);
585 lttng_ht_add_unique_u64(ht
, &stream
->node
);
587 lttng_ht_add_u64(the_consumer_data
.stream_per_chan_id_ht
, &stream
->node_channel_id
);
590 * Add stream to the stream_list_ht of the consumer data. No need to steal
591 * the key since the HT does not use it and we allow to add redundant keys
594 lttng_ht_add_u64(the_consumer_data
.stream_list_ht
, &stream
->node_session_id
);
597 * When nb_init_stream_left reaches 0, we don't need to trigger any action
598 * in terms of destroying the associated channel, because the action that
599 * causes the count to become 0 also causes a stream to be added. The
600 * channel deletion will thus be triggered by the following removal of this
603 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
604 /* Increment refcount before decrementing nb_init_stream_left */
606 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
609 /* Update consumer data once the node is inserted. */
610 the_consumer_data
.stream_count
++;
611 the_consumer_data
.need_update
= 1;
613 pthread_mutex_unlock(&stream
->lock
);
614 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
615 pthread_mutex_unlock(&stream
->chan
->lock
);
616 pthread_mutex_unlock(&the_consumer_data
.lock
);
620 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
621 * be acquired before calling this.
623 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
626 struct lttng_ht_node_u64
*node
;
627 struct lttng_ht_iter iter
;
629 LTTNG_ASSERT(relayd
);
630 ASSERT_RCU_READ_LOCKED();
632 lttng_ht_lookup(the_consumer_data
.relayd_ht
, &relayd
->net_seq_idx
, &iter
);
633 node
= lttng_ht_iter_get_node_u64(&iter
);
634 if (node
!= nullptr) {
637 lttng_ht_add_unique_u64(the_consumer_data
.relayd_ht
, &relayd
->node
);
644 * Allocate and return a consumer relayd socket.
646 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(uint64_t net_seq_idx
)
648 struct consumer_relayd_sock_pair
*obj
= nullptr;
650 /* net sequence index of -1 is a failure */
651 if (net_seq_idx
== (uint64_t) -1ULL) {
655 obj
= zmalloc
<consumer_relayd_sock_pair
>();
656 if (obj
== nullptr) {
657 PERROR("zmalloc relayd sock");
661 obj
->net_seq_idx
= net_seq_idx
;
663 obj
->destroy_flag
= 0;
664 obj
->control_sock
.sock
.fd
= -1;
665 obj
->data_sock
.sock
.fd
= -1;
666 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
667 pthread_mutex_init(&obj
->ctrl_sock_mutex
, nullptr);
674 * Find a relayd socket pair in the global consumer data.
676 * Return the object if found else NULL.
677 * RCU read-side lock must be held across this call and while using the
680 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
682 struct lttng_ht_iter iter
;
683 struct lttng_ht_node_u64
*node
;
684 struct consumer_relayd_sock_pair
*relayd
= nullptr;
686 ASSERT_RCU_READ_LOCKED();
688 /* Negative keys are lookup failures */
689 if (key
== (uint64_t) -1ULL) {
693 lttng_ht_lookup(the_consumer_data
.relayd_ht
, &key
, &iter
);
694 node
= lttng_ht_iter_get_node_u64(&iter
);
695 if (node
!= nullptr) {
696 relayd
= lttng::utils::container_of(node
, &consumer_relayd_sock_pair::node
);
704 * Find a relayd and send the stream
706 * Returns 0 on success, < 0 on error
708 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
, char *path
)
711 struct consumer_relayd_sock_pair
*relayd
;
713 LTTNG_ASSERT(stream
);
714 LTTNG_ASSERT(stream
->net_seq_idx
!= -1ULL);
717 /* The stream is not metadata. Get relayd reference if exists. */
718 lttng::urcu::read_lock_guard read_lock
;
719 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
720 if (relayd
!= nullptr) {
721 /* Add stream on the relayd */
722 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
723 ret
= relayd_add_stream(&relayd
->control_sock
,
725 get_consumer_domain(),
727 &stream
->relayd_stream_id
,
728 stream
->chan
->tracefile_size
,
729 stream
->chan
->tracefile_count
,
730 stream
->trace_chunk
);
731 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
733 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".",
734 relayd
->net_seq_idx
);
735 lttng_consumer_cleanup_relayd(relayd
);
739 uatomic_inc(&relayd
->refcount
);
740 stream
->sent_to_relayd
= 1;
742 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
744 stream
->net_seq_idx
);
749 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
752 stream
->net_seq_idx
);
759 * Find a relayd and send the streams sent message
761 * Returns 0 on success, < 0 on error
763 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
766 struct consumer_relayd_sock_pair
*relayd
;
768 LTTNG_ASSERT(net_seq_idx
!= -1ULL);
770 /* The stream is not metadata. Get relayd reference if exists. */
771 lttng::urcu::read_lock_guard read_lock
;
772 relayd
= consumer_find_relayd(net_seq_idx
);
773 if (relayd
!= nullptr) {
774 /* Add stream on the relayd */
775 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
776 ret
= relayd_streams_sent(&relayd
->control_sock
);
777 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
779 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64
".",
780 relayd
->net_seq_idx
);
781 lttng_consumer_cleanup_relayd(relayd
);
785 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.", net_seq_idx
);
791 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
798 * Find a relayd and close the stream
800 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
802 struct consumer_relayd_sock_pair
*relayd
;
804 /* The stream is not metadata. Get relayd reference if exists. */
805 lttng::urcu::read_lock_guard read_lock
;
806 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
808 consumer_stream_relayd_close(stream
, relayd
);
813 * Handle stream for relayd transmission if the stream applies for network
814 * streaming where the net sequence index is set.
816 * Return destination file descriptor or negative value on error.
818 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
820 unsigned long padding
,
821 struct consumer_relayd_sock_pair
*relayd
)
824 struct lttcomm_relayd_data_hdr data_hdr
;
827 LTTNG_ASSERT(stream
);
828 LTTNG_ASSERT(relayd
);
830 /* Reset data header */
831 memset(&data_hdr
, 0, sizeof(data_hdr
));
833 if (stream
->metadata_flag
) {
834 /* Caller MUST acquire the relayd control socket lock */
835 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
840 /* Metadata are always sent on the control socket. */
841 outfd
= relayd
->control_sock
.sock
.fd
;
843 /* Set header with stream information */
844 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
845 data_hdr
.data_size
= htobe32(data_size
);
846 data_hdr
.padding_size
= htobe32(padding
);
849 * Note that net_seq_num below is assigned with the *current* value of
850 * next_net_seq_num and only after that the next_net_seq_num will be
851 * increment. This is why when issuing a command on the relayd using
852 * this next value, 1 should always be substracted in order to compare
853 * the last seen sequence number on the relayd side to the last sent.
855 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
856 /* Other fields are zeroed previously */
858 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
, sizeof(data_hdr
));
863 ++stream
->next_net_seq_num
;
865 /* Set to go on data socket */
866 outfd
= relayd
->data_sock
.sock
.fd
;
874 * Write a character on the metadata poll pipe to wake the metadata thread.
875 * Returns 0 on success, -1 on error.
877 int consumer_metadata_wakeup_pipe(const struct lttng_consumer_channel
*channel
)
881 DBG("Waking up metadata poll thread (writing to pipe): channel name = '%s'", channel
->name
);
882 if (channel
->monitor
&& channel
->metadata_stream
) {
883 const char dummy
= 'c';
884 const ssize_t write_ret
=
885 lttng_write(channel
->metadata_stream
->ust_metadata_poll_pipe
[1], &dummy
, 1);
888 if (errno
== EWOULDBLOCK
) {
890 * This is fine, the metadata poll thread
891 * is having a hard time keeping-up, but
892 * it will eventually wake-up and consume
893 * the available data.
897 PERROR("Failed to write to UST metadata pipe while attempting to wake-up the metadata poll thread");
909 * Trigger a dump of the metadata content. Following/during the succesful
910 * completion of this call, the metadata poll thread will start receiving
911 * metadata packets to consume.
913 * The caller must hold the channel and stream locks.
915 static int consumer_metadata_stream_dump(struct lttng_consumer_stream
*stream
)
919 ASSERT_LOCKED(stream
->chan
->lock
);
920 ASSERT_LOCKED(stream
->lock
);
921 LTTNG_ASSERT(stream
->metadata_flag
);
922 LTTNG_ASSERT(stream
->chan
->trace_chunk
);
924 switch (the_consumer_data
.type
) {
925 case LTTNG_CONSUMER_KERNEL
:
927 * Reset the position of what has been read from the
928 * metadata cache to 0 so we can dump it again.
930 ret
= kernctl_metadata_cache_dump(stream
->wait_fd
);
932 case LTTNG_CONSUMER32_UST
:
933 case LTTNG_CONSUMER64_UST
:
935 * Reset the position pushed from the metadata cache so it
936 * will write from the beginning on the next push.
938 stream
->ust_metadata_pushed
= 0;
939 ret
= consumer_metadata_wakeup_pipe(stream
->chan
);
942 ERR("Unknown consumer_data type");
946 ERR("Failed to dump the metadata cache");
951 static int lttng_consumer_channel_set_trace_chunk(struct lttng_consumer_channel
*channel
,
952 struct lttng_trace_chunk
*new_trace_chunk
)
954 pthread_mutex_lock(&channel
->lock
);
955 if (channel
->is_deleted
) {
957 * The channel has been logically deleted and should no longer
958 * be used. It has released its reference to its current trace
959 * chunk and should not acquire a new one.
961 * Return success as there is nothing for the caller to do.
967 * The acquisition of the reference cannot fail (barring
968 * a severe internal error) since a reference to the published
969 * chunk is already held by the caller.
971 if (new_trace_chunk
) {
972 const bool acquired_reference
= lttng_trace_chunk_get(new_trace_chunk
);
974 LTTNG_ASSERT(acquired_reference
);
977 lttng_trace_chunk_put(channel
->trace_chunk
);
978 channel
->trace_chunk
= new_trace_chunk
;
980 pthread_mutex_unlock(&channel
->lock
);
985 * Allocate and return a new lttng_consumer_channel object using the given key
986 * to initialize the hash table node.
988 * On error, return NULL.
990 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
992 const uint64_t *chunk_id
,
993 const char *pathname
,
996 enum lttng_event_output output
,
997 uint64_t tracefile_size
,
998 uint64_t tracefile_count
,
999 uint64_t session_id_per_pid
,
1000 unsigned int monitor
,
1001 unsigned int live_timer_interval
,
1002 bool is_in_live_session
,
1003 const char *root_shm_path
,
1004 const char *shm_path
)
1006 struct lttng_consumer_channel
*channel
= nullptr;
1007 struct lttng_trace_chunk
*trace_chunk
= nullptr;
1010 trace_chunk
= lttng_trace_chunk_registry_find_chunk(
1011 the_consumer_data
.chunk_registry
, session_id
, *chunk_id
);
1013 ERR("Failed to find trace chunk reference during creation of channel");
1018 channel
= zmalloc
<lttng_consumer_channel
>();
1019 if (channel
== nullptr) {
1020 PERROR("malloc struct lttng_consumer_channel");
1025 channel
->refcount
= 0;
1026 channel
->session_id
= session_id
;
1027 channel
->session_id_per_pid
= session_id_per_pid
;
1028 channel
->relayd_id
= relayd_id
;
1029 channel
->tracefile_size
= tracefile_size
;
1030 channel
->tracefile_count
= tracefile_count
;
1031 channel
->monitor
= monitor
;
1032 channel
->live_timer_interval
= live_timer_interval
;
1033 channel
->is_live
= is_in_live_session
;
1034 pthread_mutex_init(&channel
->lock
, nullptr);
1035 pthread_mutex_init(&channel
->timer_lock
, nullptr);
1038 case LTTNG_EVENT_SPLICE
:
1039 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
1041 case LTTNG_EVENT_MMAP
:
1042 channel
->output
= CONSUMER_CHANNEL_MMAP
;
1052 * In monitor mode, the streams associated with the channel will be put in
1053 * a special list ONLY owned by this channel. So, the refcount is set to 1
1054 * here meaning that the channel itself has streams that are referenced.
1056 * On a channel deletion, once the channel is no longer visible, the
1057 * refcount is decremented and checked for a zero value to delete it. With
1058 * streams in no monitor mode, it will now be safe to destroy the channel.
1060 if (!channel
->monitor
) {
1061 channel
->refcount
= 1;
1064 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1065 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1067 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1068 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1070 if (root_shm_path
) {
1071 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1072 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1075 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1076 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1079 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1080 lttng_ht_node_init_u64(&channel
->channels_by_session_id_ht_node
, channel
->session_id
);
1082 channel
->wait_fd
= -1;
1083 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1086 int ret
= lttng_consumer_channel_set_trace_chunk(channel
, trace_chunk
);
1092 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1095 lttng_trace_chunk_put(trace_chunk
);
1098 consumer_del_channel(channel
);
1104 * Add a channel to the global list protected by a mutex.
1106 * Always return 0 indicating success.
1108 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1109 struct lttng_consumer_local_data
*ctx
)
1111 pthread_mutex_lock(&the_consumer_data
.lock
);
1112 pthread_mutex_lock(&channel
->lock
);
1113 pthread_mutex_lock(&channel
->timer_lock
);
1116 * This gives us a guarantee that the channel we are about to add to the
1117 * channel hash table will be unique. See this function comment on the why
1118 * we need to steel the channel key at this stage.
1120 steal_channel_key(channel
->key
);
1122 lttng::urcu::read_lock_guard read_lock
;
1123 lttng_ht_add_unique_u64(the_consumer_data
.channel_ht
, &channel
->node
);
1124 lttng_ht_add_u64(the_consumer_data
.channels_by_session_id_ht
,
1125 &channel
->channels_by_session_id_ht_node
);
1126 channel
->is_published
= true;
1128 pthread_mutex_unlock(&channel
->timer_lock
);
1129 pthread_mutex_unlock(&channel
->lock
);
1130 pthread_mutex_unlock(&the_consumer_data
.lock
);
1132 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1133 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1140 * Allocate the pollfd structure and the local view of the out fds to avoid
1141 * doing a lookup in the linked list and concurrency issues when writing is
1142 * needed. Called with consumer_data.lock held.
1144 * Returns the number of fds in the structures.
1146 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1147 struct pollfd
**pollfd
,
1148 struct lttng_consumer_stream
**local_stream
,
1149 struct lttng_ht
*ht
,
1150 int *nb_inactive_fd
)
1153 struct lttng_ht_iter iter
;
1154 struct lttng_consumer_stream
*stream
;
1158 LTTNG_ASSERT(pollfd
);
1159 LTTNG_ASSERT(local_stream
);
1161 DBG("Updating poll fd array");
1162 *nb_inactive_fd
= 0;
1165 lttng::urcu::read_lock_guard read_lock
;
1166 cds_lfht_for_each_entry (ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1168 * Only active streams with an active end point can be added to the
1169 * poll set and local stream storage of the thread.
1171 * There is a potential race here for endpoint_status to be updated
1172 * just after the check. However, this is OK since the stream(s) will
1173 * be deleted once the thread is notified that the end point state has
1174 * changed where this function will be called back again.
1176 * We track the number of inactive FDs because they still need to be
1177 * closed by the polling thread after a wakeup on the data_pipe or
1180 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1181 (*nb_inactive_fd
)++;
1185 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1186 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1187 local_stream
[i
] = stream
;
1193 * Insert the consumer_data_pipe at the end of the array and don't
1194 * increment i so nb_fd is the number of real FD.
1196 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1197 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1199 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1200 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1205 * Poll on the should_quit pipe and the command socket return -1 on
1206 * error, 1 if should exit, 0 if data is available on the command socket
1208 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1213 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1214 if (num_rdy
== -1) {
1216 * Restart interrupted system call.
1218 if (errno
== EINTR
) {
1221 PERROR("Poll error");
1224 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1225 DBG("consumer_should_quit wake up");
1232 * Set the error socket.
1234 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
, int sock
)
1236 ctx
->consumer_error_socket
= sock
;
1240 * Set the command socket path.
1242 void lttng_consumer_set_command_sock_path(struct lttng_consumer_local_data
*ctx
, char *sock
)
1244 ctx
->consumer_command_sock_path
= sock
;
1248 * Send return code to the session daemon.
1249 * If the socket is not defined, we return 0, it is not a fatal error
1251 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1253 if (ctx
->consumer_error_socket
> 0) {
1254 return lttcomm_send_unix_sock(
1255 ctx
->consumer_error_socket
, &cmd
, sizeof(enum lttcomm_sessiond_command
));
1262 * Close all the tracefiles and stream fds and MUST be called when all
1263 * instances are destroyed i.e. when all threads were joined and are ended.
1265 void lttng_consumer_cleanup()
1267 struct lttng_ht_iter iter
;
1268 struct lttng_consumer_channel
*channel
;
1269 unsigned int trace_chunks_left
;
1272 lttng::urcu::read_lock_guard read_lock
;
1274 cds_lfht_for_each_entry (
1275 the_consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
, node
.node
) {
1276 consumer_del_channel(channel
);
1280 lttng_ht_destroy(the_consumer_data
.channel_ht
);
1281 lttng_ht_destroy(the_consumer_data
.channels_by_session_id_ht
);
1283 cleanup_relayd_ht();
1285 lttng_ht_destroy(the_consumer_data
.stream_per_chan_id_ht
);
1288 * This HT contains streams that are freed by either the metadata thread or
1289 * the data thread so we do *nothing* on the hash table and simply destroy
1292 lttng_ht_destroy(the_consumer_data
.stream_list_ht
);
1295 * Trace chunks in the registry may still exist if the session
1296 * daemon has encountered an internal error and could not
1297 * tear down its sessions and/or trace chunks properly.
1299 * Release the session daemon's implicit reference to any remaining
1300 * trace chunk and print an error if any trace chunk was found. Note
1301 * that there are _no_ legitimate cases for trace chunks to be left,
1302 * it is a leak. However, it can happen following a crash of the
1303 * session daemon and not emptying the registry would cause an assertion
1307 lttng_trace_chunk_registry_put_each_chunk(the_consumer_data
.chunk_registry
);
1308 if (trace_chunks_left
) {
1309 ERR("%u trace chunks are leaked by lttng-consumerd. "
1310 "This can be caused by an internal error of the session daemon.",
1313 /* Run all callbacks freeing each chunk. */
1315 lttng_trace_chunk_registry_destroy(the_consumer_data
.chunk_registry
);
1319 * Called from signal handler.
1321 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1325 CMM_STORE_SHARED(consumer_quit
, 1);
1326 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1328 PERROR("write consumer quit");
1331 DBG("Consumer flag that it should quit");
1335 * Flush pending writes to trace output disk file.
1337 static void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
, off_t orig_offset
)
1340 int outfd
= stream
->out_fd
;
1343 * This does a blocking write-and-wait on any page that belongs to the
1344 * subbuffer prior to the one we just wrote.
1345 * Don't care about error values, as these are just hints and ways to
1346 * limit the amount of page cache used.
1348 if (orig_offset
< stream
->max_sb_size
) {
1351 lttng_sync_file_range(outfd
,
1352 orig_offset
- stream
->max_sb_size
,
1353 stream
->max_sb_size
,
1354 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
|
1355 SYNC_FILE_RANGE_WAIT_AFTER
);
1357 * Give hints to the kernel about how we access the file:
1358 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1361 * We need to call fadvise again after the file grows because the
1362 * kernel does not seem to apply fadvise to non-existing parts of the
1365 * Call fadvise _after_ having waited for the page writeback to
1366 * complete because the dirty page writeback semantic is not well
1367 * defined. So it can be expected to lead to lower throughput in
1370 ret
= posix_fadvise(
1371 outfd
, orig_offset
- stream
->max_sb_size
, stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1372 if (ret
&& ret
!= -ENOSYS
) {
1374 PERROR("posix_fadvise on fd %i", outfd
);
1379 * Initialise the necessary environnement :
1380 * - create a new context
1381 * - create the poll_pipe
1382 * - create the should_quit pipe (for signal handler)
1383 * - create the thread pipe (for splice)
1385 * Takes a function pointer as argument, this function is called when data is
1386 * available on a buffer. This function is responsible to do the
1387 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1388 * buffer configuration and then kernctl_put_next_subbuf at the end.
1390 * Returns a pointer to the new context or NULL on error.
1392 struct lttng_consumer_local_data
*
1393 lttng_consumer_create(enum lttng_consumer_type type
,
1394 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1395 struct lttng_consumer_local_data
*ctx
,
1396 bool locked_by_caller
),
1397 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1398 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1399 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1402 struct lttng_consumer_local_data
*ctx
;
1404 LTTNG_ASSERT(the_consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1405 the_consumer_data
.type
== type
);
1406 the_consumer_data
.type
= type
;
1408 ctx
= zmalloc
<lttng_consumer_local_data
>();
1409 if (ctx
== nullptr) {
1410 PERROR("allocating context");
1414 ctx
->consumer_error_socket
= -1;
1415 ctx
->consumer_metadata_socket
= -1;
1416 pthread_mutex_init(&ctx
->metadata_socket_lock
, nullptr);
1417 /* assign the callbacks */
1418 ctx
->on_buffer_ready
= buffer_ready
;
1419 ctx
->on_recv_channel
= recv_channel
;
1420 ctx
->on_recv_stream
= recv_stream
;
1421 ctx
->on_update_stream
= update_stream
;
1423 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1424 if (!ctx
->consumer_data_pipe
) {
1425 goto error_poll_pipe
;
1428 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1429 if (!ctx
->consumer_wakeup_pipe
) {
1430 goto error_wakeup_pipe
;
1433 ret
= pipe(ctx
->consumer_should_quit
);
1435 PERROR("Error creating recv pipe");
1436 goto error_quit_pipe
;
1439 ret
= pipe(ctx
->consumer_channel_pipe
);
1441 PERROR("Error creating channel pipe");
1442 goto error_channel_pipe
;
1445 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1446 if (!ctx
->consumer_metadata_pipe
) {
1447 goto error_metadata_pipe
;
1450 ctx
->channel_monitor_pipe
= -1;
1454 error_metadata_pipe
:
1455 utils_close_pipe(ctx
->consumer_channel_pipe
);
1457 utils_close_pipe(ctx
->consumer_should_quit
);
1459 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1461 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1469 * Iterate over all streams of the hashtable and free them properly.
1471 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1473 struct lttng_ht_iter iter
;
1474 struct lttng_consumer_stream
*stream
;
1476 if (ht
== nullptr) {
1481 lttng::urcu::read_lock_guard read_lock
;
1482 cds_lfht_for_each_entry (ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1484 * Ignore return value since we are currently cleaning up so any error
1487 (void) consumer_del_stream(stream
, ht
);
1491 lttng_ht_destroy(ht
);
1495 * Iterate over all streams of the metadata hashtable and free them
1498 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1500 struct lttng_ht_iter iter
;
1501 struct lttng_consumer_stream
*stream
;
1503 if (ht
== nullptr) {
1508 lttng::urcu::read_lock_guard read_lock
;
1509 cds_lfht_for_each_entry (ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1511 * Ignore return value since we are currently cleaning up so any error
1514 (void) consumer_del_metadata_stream(stream
, ht
);
1518 lttng_ht_destroy(ht
);
1522 * Close all fds associated with the instance and free the context.
1524 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1528 DBG("Consumer destroying it. Closing everything.");
1534 destroy_data_stream_ht(data_ht
);
1535 destroy_metadata_stream_ht(metadata_ht
);
1537 ret
= close(ctx
->consumer_error_socket
);
1541 ret
= close(ctx
->consumer_metadata_socket
);
1545 utils_close_pipe(ctx
->consumer_channel_pipe
);
1546 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1547 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1548 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1549 utils_close_pipe(ctx
->consumer_should_quit
);
1551 unlink(ctx
->consumer_command_sock_path
);
1556 * Write the metadata stream id on the specified file descriptor.
1559 write_relayd_metadata_id(int fd
, struct lttng_consumer_stream
*stream
, unsigned long padding
)
1562 struct lttcomm_relayd_metadata_payload hdr
;
1564 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1565 hdr
.padding_size
= htobe32(padding
);
1566 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1567 if (ret
< sizeof(hdr
)) {
1569 * This error means that the fd's end is closed so ignore the PERROR
1570 * not to clubber the error output since this can happen in a normal
1573 if (errno
!= EPIPE
) {
1574 PERROR("write metadata stream id");
1576 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1578 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1579 * handle writting the missing part so report that as an error and
1580 * don't lie to the caller.
1585 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1586 stream
->relayd_stream_id
,
1594 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1595 * core function for writing trace buffers to either the local filesystem or
1598 * It must be called with the stream and the channel lock held.
1600 * Careful review MUST be put if any changes occur!
1602 * Returns the number of bytes written
1604 ssize_t
lttng_consumer_on_read_subbuffer_mmap(struct lttng_consumer_stream
*stream
,
1605 const struct lttng_buffer_view
*buffer
,
1606 unsigned long padding
)
1609 off_t orig_offset
= stream
->out_fd_offset
;
1610 /* Default is on the disk */
1611 int outfd
= stream
->out_fd
;
1612 struct consumer_relayd_sock_pair
*relayd
= nullptr;
1613 unsigned int relayd_hang_up
= 0;
1614 const size_t subbuf_content_size
= buffer
->size
- padding
;
1617 /* RCU lock for the relayd pointer */
1618 lttng::urcu::read_lock_guard read_lock
;
1619 LTTNG_ASSERT(stream
->net_seq_idx
!= (uint64_t) -1ULL || stream
->trace_chunk
);
1621 /* Flag that the current stream if set for network streaming. */
1622 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1623 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1624 if (relayd
== nullptr) {
1630 /* Handle stream on the relayd if the output is on the network */
1632 unsigned long netlen
= subbuf_content_size
;
1635 * Lock the control socket for the complete duration of the function
1636 * since from this point on we will use the socket.
1638 if (stream
->metadata_flag
) {
1639 /* Metadata requires the control socket. */
1640 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1641 if (stream
->reset_metadata_flag
) {
1642 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1643 stream
->relayd_stream_id
,
1644 stream
->metadata_version
);
1649 stream
->reset_metadata_flag
= 0;
1651 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1654 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1659 /* Use the returned socket. */
1662 /* Write metadata stream id before payload */
1663 if (stream
->metadata_flag
) {
1664 ret
= write_relayd_metadata_id(outfd
, stream
, padding
);
1671 write_len
= subbuf_content_size
;
1673 /* No streaming; we have to write the full padding. */
1674 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1675 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1677 ERR("Reset metadata file");
1680 stream
->reset_metadata_flag
= 0;
1684 * Check if we need to change the tracefile before writing the packet.
1686 if (stream
->chan
->tracefile_size
> 0 &&
1687 (stream
->tracefile_size_current
+ buffer
->size
) >
1688 stream
->chan
->tracefile_size
) {
1689 ret
= consumer_stream_rotate_output_files(stream
);
1693 outfd
= stream
->out_fd
;
1696 stream
->tracefile_size_current
+= buffer
->size
;
1697 write_len
= buffer
->size
;
1701 * This call guarantee that len or less is returned. It's impossible to
1702 * receive a ret value that is bigger than len.
1704 ret
= lttng_write(outfd
, buffer
->data
, write_len
);
1705 DBG("Consumer mmap write() ret %zd (len %zu)", ret
, write_len
);
1706 if (ret
< 0 || ((size_t) ret
!= write_len
)) {
1708 * Report error to caller if nothing was written else at least send the
1716 /* Socket operation failed. We consider the relayd dead */
1717 if (errno
== EPIPE
) {
1719 * This is possible if the fd is closed on the other side
1720 * (outfd) or any write problem. It can be verbose a bit for a
1721 * normal execution if for instance the relayd is stopped
1722 * abruptly. This can happen so set this to a DBG statement.
1724 DBG("Consumer mmap write detected relayd hang up");
1726 /* Unhandled error, print it and stop function right now. */
1727 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret
, write_len
);
1731 stream
->output_written
+= ret
;
1733 /* This call is useless on a socket so better save a syscall. */
1735 /* This won't block, but will start writeout asynchronously */
1736 lttng_sync_file_range(
1737 outfd
, stream
->out_fd_offset
, write_len
, SYNC_FILE_RANGE_WRITE
);
1738 stream
->out_fd_offset
+= write_len
;
1739 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1744 * This is a special case that the relayd has closed its socket. Let's
1745 * cleanup the relayd object and all associated streams.
1747 if (relayd
&& relayd_hang_up
) {
1748 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1749 lttng_consumer_cleanup_relayd(relayd
);
1753 /* Unlock only if ctrl socket used */
1754 if (relayd
&& stream
->metadata_flag
) {
1755 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1762 * Splice the data from the ring buffer to the tracefile.
1764 * It must be called with the stream lock held.
1766 * Returns the number of bytes spliced.
1768 ssize_t
lttng_consumer_on_read_subbuffer_splice(struct lttng_consumer_local_data
*ctx
,
1769 struct lttng_consumer_stream
*stream
,
1771 unsigned long padding
)
1773 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1775 off_t orig_offset
= stream
->out_fd_offset
;
1776 int fd
= stream
->wait_fd
;
1777 /* Default is on the disk */
1778 int outfd
= stream
->out_fd
;
1779 struct consumer_relayd_sock_pair
*relayd
= nullptr;
1781 unsigned int relayd_hang_up
= 0;
1783 switch (the_consumer_data
.type
) {
1784 case LTTNG_CONSUMER_KERNEL
:
1786 case LTTNG_CONSUMER32_UST
:
1787 case LTTNG_CONSUMER64_UST
:
1788 /* Not supported for user space tracing */
1791 ERR("Unknown consumer_data type");
1795 /* RCU lock for the relayd pointer */
1796 lttng::urcu::read_lock_guard read_lock
;
1798 /* Flag that the current stream if set for network streaming. */
1799 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1800 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1801 if (relayd
== nullptr) {
1806 splice_pipe
= stream
->splice_pipe
;
1808 /* Write metadata stream id before payload */
1810 unsigned long total_len
= len
;
1812 if (stream
->metadata_flag
) {
1814 * Lock the control socket for the complete duration of the function
1815 * since from this point on we will use the socket.
1817 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1819 if (stream
->reset_metadata_flag
) {
1820 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1821 stream
->relayd_stream_id
,
1822 stream
->metadata_version
);
1827 stream
->reset_metadata_flag
= 0;
1829 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, padding
);
1836 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1839 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1845 /* Use the returned socket. */
1848 /* No streaming, we have to set the len with the full padding */
1851 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1852 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1854 ERR("Reset metadata file");
1857 stream
->reset_metadata_flag
= 0;
1860 * Check if we need to change the tracefile before writing the packet.
1862 if (stream
->chan
->tracefile_size
> 0 &&
1863 (stream
->tracefile_size_current
+ len
) > stream
->chan
->tracefile_size
) {
1864 ret
= consumer_stream_rotate_output_files(stream
);
1869 outfd
= stream
->out_fd
;
1872 stream
->tracefile_size_current
+= len
;
1876 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1877 (unsigned long) offset
,
1881 ret_splice
= splice(
1882 fd
, &offset
, splice_pipe
[1], nullptr, len
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1883 DBG("splice chan to pipe, ret %zd", ret_splice
);
1884 if (ret_splice
< 0) {
1887 PERROR("Error in relay splice");
1891 /* Handle stream on the relayd if the output is on the network */
1892 if (relayd
&& stream
->metadata_flag
) {
1893 size_t metadata_payload_size
=
1894 sizeof(struct lttcomm_relayd_metadata_payload
);
1896 /* Update counter to fit the spliced data */
1897 ret_splice
+= metadata_payload_size
;
1898 len
+= metadata_payload_size
;
1900 * We do this so the return value can match the len passed as
1901 * argument to this function.
1903 written
-= metadata_payload_size
;
1906 /* Splice data out */
1907 ret_splice
= splice(splice_pipe
[0],
1912 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1913 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd", outfd
, ret_splice
);
1914 if (ret_splice
< 0) {
1919 } else if (ret_splice
> len
) {
1921 * We don't expect this code path to be executed but you never know
1922 * so this is an extra protection agains a buggy splice().
1925 written
+= ret_splice
;
1926 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
, len
);
1929 /* All good, update current len and continue. */
1933 /* This call is useless on a socket so better save a syscall. */
1935 /* This won't block, but will start writeout asynchronously */
1936 lttng_sync_file_range(
1937 outfd
, stream
->out_fd_offset
, ret_splice
, SYNC_FILE_RANGE_WRITE
);
1938 stream
->out_fd_offset
+= ret_splice
;
1940 stream
->output_written
+= ret_splice
;
1941 written
+= ret_splice
;
1944 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1950 * This is a special case that the relayd has closed its socket. Let's
1951 * cleanup the relayd object and all associated streams.
1953 if (relayd
&& relayd_hang_up
) {
1954 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1955 lttng_consumer_cleanup_relayd(relayd
);
1956 /* Skip splice error so the consumer does not fail */
1961 /* send the appropriate error description to sessiond */
1964 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1967 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1970 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1975 if (relayd
&& stream
->metadata_flag
) {
1976 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1983 * Sample the snapshot positions for a specific fd
1985 * Returns 0 on success, < 0 on error
1987 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
1989 switch (the_consumer_data
.type
) {
1990 case LTTNG_CONSUMER_KERNEL
:
1991 return lttng_kconsumer_sample_snapshot_positions(stream
);
1992 case LTTNG_CONSUMER32_UST
:
1993 case LTTNG_CONSUMER64_UST
:
1994 return lttng_ustconsumer_sample_snapshot_positions(stream
);
1996 ERR("Unknown consumer_data type");
2002 * Take a snapshot for a specific fd
2004 * Returns 0 on success, < 0 on error
2006 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2008 switch (the_consumer_data
.type
) {
2009 case LTTNG_CONSUMER_KERNEL
:
2010 return lttng_kconsumer_take_snapshot(stream
);
2011 case LTTNG_CONSUMER32_UST
:
2012 case LTTNG_CONSUMER64_UST
:
2013 return lttng_ustconsumer_take_snapshot(stream
);
2015 ERR("Unknown consumer_data type");
2022 * Get the produced position
2024 * Returns 0 on success, < 0 on error
2026 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2028 switch (the_consumer_data
.type
) {
2029 case LTTNG_CONSUMER_KERNEL
:
2030 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
2031 case LTTNG_CONSUMER32_UST
:
2032 case LTTNG_CONSUMER64_UST
:
2033 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2035 ERR("Unknown consumer_data type");
2042 * Get the consumed position (free-running counter position in bytes).
2044 * Returns 0 on success, < 0 on error
2046 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
, unsigned long *pos
)
2048 switch (the_consumer_data
.type
) {
2049 case LTTNG_CONSUMER_KERNEL
:
2050 return lttng_kconsumer_get_consumed_snapshot(stream
, pos
);
2051 case LTTNG_CONSUMER32_UST
:
2052 case LTTNG_CONSUMER64_UST
:
2053 return lttng_ustconsumer_get_consumed_snapshot(stream
, pos
);
2055 ERR("Unknown consumer_data type");
2061 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2063 struct pollfd
*consumer_sockpoll
)
2065 switch (the_consumer_data
.type
) {
2066 case LTTNG_CONSUMER_KERNEL
:
2067 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2068 case LTTNG_CONSUMER32_UST
:
2069 case LTTNG_CONSUMER64_UST
:
2070 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2072 ERR("Unknown consumer_data type");
2078 static void lttng_consumer_close_all_metadata()
2080 switch (the_consumer_data
.type
) {
2081 case LTTNG_CONSUMER_KERNEL
:
2083 * The Kernel consumer has a different metadata scheme so we don't
2084 * close anything because the stream will be closed by the session
2088 case LTTNG_CONSUMER32_UST
:
2089 case LTTNG_CONSUMER64_UST
:
2091 * Close all metadata streams. The metadata hash table is passed and
2092 * this call iterates over it by closing all wakeup fd. This is safe
2093 * because at this point we are sure that the metadata producer is
2094 * either dead or blocked.
2096 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2099 ERR("Unknown consumer_data type");
2105 * Clean up a metadata stream and free its memory.
2107 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
, struct lttng_ht
*ht
)
2109 struct lttng_consumer_channel
*channel
= nullptr;
2110 bool free_channel
= false;
2112 LTTNG_ASSERT(stream
);
2114 * This call should NEVER receive regular stream. It must always be
2115 * metadata stream and this is crucial for data structure synchronization.
2117 LTTNG_ASSERT(stream
->metadata_flag
);
2119 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2121 pthread_mutex_lock(&the_consumer_data
.lock
);
2123 * Note that this assumes that a stream's channel is never changed and
2124 * that the stream's lock doesn't need to be taken to sample its
2127 channel
= stream
->chan
;
2128 pthread_mutex_lock(&channel
->lock
);
2129 pthread_mutex_lock(&stream
->lock
);
2130 if (channel
->metadata_cache
) {
2131 /* Only applicable to userspace consumers. */
2132 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
2135 /* Remove any reference to that stream. */
2136 consumer_stream_delete(stream
, ht
);
2138 /* Close down everything including the relayd if one. */
2139 consumer_stream_close_output(stream
);
2140 /* Destroy tracer buffers of the stream. */
2141 consumer_stream_destroy_buffers(stream
);
2143 /* Atomically decrement channel refcount since other threads can use it. */
2144 if (!uatomic_sub_return(&channel
->refcount
, 1) &&
2145 !uatomic_read(&channel
->nb_init_stream_left
)) {
2146 /* Go for channel deletion! */
2147 free_channel
= true;
2149 stream
->chan
= nullptr;
2152 * Nullify the stream reference so it is not used after deletion. The
2153 * channel lock MUST be acquired before being able to check for a NULL
2156 channel
->metadata_stream
= nullptr;
2158 if (channel
->metadata_cache
) {
2159 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
2161 pthread_mutex_unlock(&stream
->lock
);
2162 pthread_mutex_unlock(&channel
->lock
);
2163 pthread_mutex_unlock(&the_consumer_data
.lock
);
2166 consumer_del_channel(channel
);
2169 lttng_trace_chunk_put(stream
->trace_chunk
);
2170 stream
->trace_chunk
= nullptr;
2171 consumer_stream_free(stream
);
2175 * Action done with the metadata stream when adding it to the consumer internal
2176 * data structures to handle it.
2178 void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2180 struct lttng_ht
*ht
= metadata_ht
;
2181 struct lttng_ht_iter iter
;
2182 struct lttng_ht_node_u64
*node
;
2184 LTTNG_ASSERT(stream
);
2187 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2189 pthread_mutex_lock(&the_consumer_data
.lock
);
2190 pthread_mutex_lock(&stream
->chan
->lock
);
2191 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2192 pthread_mutex_lock(&stream
->lock
);
2195 * From here, refcounts are updated so be _careful_ when returning an error
2199 lttng::urcu::read_lock_guard read_lock
;
2202 * Lookup the stream just to make sure it does not exist in our internal
2203 * state. This should NEVER happen.
2205 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2206 node
= lttng_ht_iter_get_node_u64(&iter
);
2207 LTTNG_ASSERT(!node
);
2210 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2211 * in terms of destroying the associated channel, because the action that
2212 * causes the count to become 0 also causes a stream to be added. The
2213 * channel deletion will thus be triggered by the following removal of this
2216 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2217 /* Increment refcount before decrementing nb_init_stream_left */
2219 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2222 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2224 lttng_ht_add_u64(the_consumer_data
.stream_per_chan_id_ht
, &stream
->node_channel_id
);
2227 * Add stream to the stream_list_ht of the consumer data. No need to steal
2228 * the key since the HT does not use it and we allow to add redundant keys
2231 lttng_ht_add_u64(the_consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2233 pthread_mutex_unlock(&stream
->lock
);
2234 pthread_mutex_unlock(&stream
->chan
->lock
);
2235 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2236 pthread_mutex_unlock(&the_consumer_data
.lock
);
2240 * Delete data stream that are flagged for deletion (endpoint_status).
2242 static void validate_endpoint_status_data_stream()
2244 struct lttng_ht_iter iter
;
2245 struct lttng_consumer_stream
*stream
;
2247 DBG("Consumer delete flagged data stream");
2250 lttng::urcu::read_lock_guard read_lock
;
2252 cds_lfht_for_each_entry (data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2253 /* Validate delete flag of the stream */
2254 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2257 /* Delete it right now */
2258 consumer_del_stream(stream
, data_ht
);
2264 * Delete metadata stream that are flagged for deletion (endpoint_status).
2266 static void validate_endpoint_status_metadata_stream(struct lttng_poll_event
*pollset
)
2268 struct lttng_ht_iter iter
;
2269 struct lttng_consumer_stream
*stream
;
2271 DBG("Consumer delete flagged metadata stream");
2273 LTTNG_ASSERT(pollset
);
2276 lttng::urcu::read_lock_guard read_lock
;
2277 cds_lfht_for_each_entry (metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2278 /* Validate delete flag of the stream */
2279 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2283 * Remove from pollset so the metadata thread can continue without
2284 * blocking on a deleted stream.
2286 lttng_poll_del(pollset
, stream
->wait_fd
);
2288 /* Delete it right now */
2289 consumer_del_metadata_stream(stream
, metadata_ht
);
2295 * Thread polls on metadata file descriptor and write them on disk or on the
2298 void *consumer_thread_metadata_poll(void *data
)
2300 int ret
, i
, pollfd
, err
= -1;
2301 uint32_t revents
, nb_fd
;
2302 struct lttng_consumer_stream
*stream
= nullptr;
2303 struct lttng_ht_iter iter
;
2304 struct lttng_ht_node_u64
*node
;
2305 struct lttng_poll_event events
;
2306 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
2309 rcu_register_thread();
2311 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2313 if (testpoint(consumerd_thread_metadata
)) {
2314 goto error_testpoint
;
2317 health_code_update();
2319 DBG("Thread metadata poll started");
2321 /* Size is set to 1 for the consumer_metadata pipe */
2322 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2324 ERR("Poll set creation failed");
2328 ret
= lttng_poll_add(&events
, lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2334 DBG("Metadata main loop started");
2338 health_code_update();
2339 health_poll_entry();
2340 DBG("Metadata poll wait");
2341 ret
= lttng_poll_wait(&events
, -1);
2342 DBG("Metadata poll return from wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2344 DBG("Metadata event caught in thread");
2346 if (errno
== EINTR
) {
2347 ERR("Poll EINTR caught");
2350 if (LTTNG_POLL_GETNB(&events
) == 0) {
2351 err
= 0; /* All is OK */
2358 /* From here, the event is a metadata wait fd */
2359 for (i
= 0; i
< nb_fd
; i
++) {
2360 health_code_update();
2362 revents
= LTTNG_POLL_GETEV(&events
, i
);
2363 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2365 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2366 if (revents
& LPOLLIN
) {
2369 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2371 sizeof(stream
)); /* NOLINT sizeof
2374 if (pipe_len
< sizeof(stream
)) { /* NOLINT sizeof used on a
2377 PERROR("read metadata stream");
2380 * Remove the pipe from the poll set and continue
2381 * the loop since their might be data to consume.
2385 lttng_pipe_get_readfd(
2386 ctx
->consumer_metadata_pipe
));
2387 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2391 /* A NULL stream means that the state has changed. */
2392 if (stream
== nullptr) {
2393 /* Check for deleted streams. */
2394 validate_endpoint_status_metadata_stream(&events
);
2398 DBG("Adding metadata stream %d to poll set",
2401 /* Add metadata stream to the global poll events list */
2403 &events
, stream
->wait_fd
, LPOLLIN
| LPOLLPRI
);
2404 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2405 DBG("Metadata thread pipe hung up");
2407 * Remove the pipe from the poll set and continue the loop
2408 * since their might be data to consume.
2412 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2413 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2416 ERR("Unexpected poll events %u for sock %d",
2422 /* Handle other stream */
2426 lttng::urcu::read_lock_guard read_lock
;
2428 uint64_t tmp_id
= (uint64_t) pollfd
;
2430 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2432 node
= lttng_ht_iter_get_node_u64(&iter
);
2435 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
2437 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2438 /* Get the data out of the metadata file descriptor */
2439 DBG("Metadata available on fd %d", pollfd
);
2440 LTTNG_ASSERT(stream
->wait_fd
== pollfd
);
2443 health_code_update();
2445 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2447 * We don't check the return value here since if we get
2448 * a negative len, it means an error occurred thus we
2449 * simply remove it from the poll set and free the
2454 /* It's ok to have an unavailable sub-buffer */
2455 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2456 /* Clean up stream from consumer and free it. */
2457 lttng_poll_del(&events
, stream
->wait_fd
);
2458 consumer_del_metadata_stream(stream
, metadata_ht
);
2460 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2461 DBG("Metadata fd %d is hup|err.", pollfd
);
2462 if (!stream
->hangup_flush_done
&&
2463 (the_consumer_data
.type
== LTTNG_CONSUMER32_UST
||
2464 the_consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2465 DBG("Attempting to flush and consume the UST buffers");
2466 lttng_ustconsumer_on_stream_hangup(stream
);
2468 /* We just flushed the stream now read it. */
2470 health_code_update();
2472 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2474 * We don't check the return value here since if we
2475 * get a negative len, it means an error occurred
2476 * thus we simply remove it from the poll set and
2482 lttng_poll_del(&events
, stream
->wait_fd
);
2484 * This call update the channel states, closes file descriptors
2485 * and securely free the stream.
2487 consumer_del_metadata_stream(stream
, metadata_ht
);
2489 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2492 /* Release RCU lock for the stream looked up */
2499 DBG("Metadata poll thread exiting");
2501 lttng_poll_clean(&events
);
2506 ERR("Health error occurred in %s", __func__
);
2508 health_unregister(health_consumerd
);
2509 rcu_unregister_thread();
2514 * This thread polls the fds in the set to consume the data and write
2515 * it to tracefile if necessary.
2517 void *consumer_thread_data_poll(void *data
)
2519 int num_rdy
, high_prio
, ret
, i
, err
= -1;
2520 struct pollfd
*pollfd
= nullptr;
2521 /* local view of the streams */
2522 struct lttng_consumer_stream
**local_stream
= nullptr, *new_stream
= nullptr;
2523 /* local view of consumer_data.fds_count */
2525 /* 2 for the consumer_data_pipe and wake up pipe */
2526 const int nb_pipes_fd
= 2;
2527 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2528 int nb_inactive_fd
= 0;
2529 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
2532 rcu_register_thread();
2534 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2536 if (testpoint(consumerd_thread_data
)) {
2537 goto error_testpoint
;
2540 health_code_update();
2542 local_stream
= zmalloc
<lttng_consumer_stream
*>();
2543 if (local_stream
== nullptr) {
2544 PERROR("local_stream malloc");
2549 health_code_update();
2554 * the fds set has been updated, we need to update our
2555 * local array as well
2557 pthread_mutex_lock(&the_consumer_data
.lock
);
2558 if (the_consumer_data
.need_update
) {
2563 local_stream
= nullptr;
2565 /* Allocate for all fds */
2567 calloc
<struct pollfd
>(the_consumer_data
.stream_count
+ nb_pipes_fd
);
2568 if (pollfd
== nullptr) {
2569 PERROR("pollfd malloc");
2570 pthread_mutex_unlock(&the_consumer_data
.lock
);
2574 local_stream
= calloc
<lttng_consumer_stream
*>(
2575 the_consumer_data
.stream_count
+ nb_pipes_fd
);
2576 if (local_stream
== nullptr) {
2577 PERROR("local_stream malloc");
2578 pthread_mutex_unlock(&the_consumer_data
.lock
);
2581 ret
= update_poll_array(
2582 ctx
, &pollfd
, local_stream
, data_ht
, &nb_inactive_fd
);
2584 ERR("Error in allocating pollfd or local_outfds");
2585 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2586 pthread_mutex_unlock(&the_consumer_data
.lock
);
2590 the_consumer_data
.need_update
= 0;
2592 pthread_mutex_unlock(&the_consumer_data
.lock
);
2594 /* No FDs and consumer_quit, consumer_cleanup the thread */
2595 if (nb_fd
== 0 && nb_inactive_fd
== 0 && CMM_LOAD_SHARED(consumer_quit
) == 1) {
2596 err
= 0; /* All is OK */
2599 /* poll on the array of fds */
2601 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2602 if (testpoint(consumerd_thread_data_poll
)) {
2605 health_poll_entry();
2606 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2608 DBG("poll num_rdy : %d", num_rdy
);
2609 if (num_rdy
== -1) {
2611 * Restart interrupted system call.
2613 if (errno
== EINTR
) {
2616 PERROR("Poll error");
2617 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2619 } else if (num_rdy
== 0) {
2620 DBG("Polling thread timed out");
2624 if (caa_unlikely(data_consumption_paused
)) {
2625 DBG("Data consumption paused, sleeping...");
2631 * If the consumer_data_pipe triggered poll go directly to the
2632 * beginning of the loop to update the array. We want to prioritize
2633 * array update over low-priority reads.
2635 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2636 ssize_t pipe_readlen
;
2638 DBG("consumer_data_pipe wake up");
2639 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2641 sizeof(new_stream
)); /* NOLINT sizeof used on
2643 if (pipe_readlen
< sizeof(new_stream
)) { /* NOLINT sizeof used on a pointer.
2645 PERROR("Consumer data pipe");
2646 /* Continue so we can at least handle the current stream(s). */
2651 * If the stream is NULL, just ignore it. It's also possible that
2652 * the sessiond poll thread changed the consumer_quit state and is
2653 * waking us up to test it.
2655 if (new_stream
== nullptr) {
2656 validate_endpoint_status_data_stream();
2660 /* Continue to update the local streams and handle prio ones */
2664 /* Handle wakeup pipe. */
2665 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2667 ssize_t pipe_readlen
;
2670 lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
, sizeof(dummy
));
2671 if (pipe_readlen
< 0) {
2672 PERROR("Consumer data wakeup pipe");
2674 /* We've been awakened to handle stream(s). */
2675 ctx
->has_wakeup
= 0;
2678 /* Take care of high priority channels first. */
2679 for (i
= 0; i
< nb_fd
; i
++) {
2680 health_code_update();
2682 if (local_stream
[i
] == nullptr) {
2685 if (pollfd
[i
].revents
& POLLPRI
) {
2686 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2688 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2689 /* it's ok to have an unavailable sub-buffer */
2690 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2691 /* Clean the stream and free it. */
2692 consumer_del_stream(local_stream
[i
], data_ht
);
2693 local_stream
[i
] = nullptr;
2694 } else if (len
> 0) {
2695 local_stream
[i
]->has_data_left_to_be_read_before_teardown
=
2702 * If we read high prio channel in this loop, try again
2703 * for more high prio data.
2709 /* Take care of low priority channels. */
2710 for (i
= 0; i
< nb_fd
; i
++) {
2711 health_code_update();
2713 if (local_stream
[i
] == nullptr) {
2716 if ((pollfd
[i
].revents
& POLLIN
) || local_stream
[i
]->hangup_flush_done
||
2717 local_stream
[i
]->has_data
) {
2718 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2719 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2720 /* it's ok to have an unavailable sub-buffer */
2721 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2722 /* Clean the stream and free it. */
2723 consumer_del_stream(local_stream
[i
], data_ht
);
2724 local_stream
[i
] = nullptr;
2725 } else if (len
> 0) {
2726 local_stream
[i
]->has_data_left_to_be_read_before_teardown
=
2732 /* Handle hangup and errors */
2733 for (i
= 0; i
< nb_fd
; i
++) {
2734 health_code_update();
2736 if (local_stream
[i
] == nullptr) {
2739 if (!local_stream
[i
]->hangup_flush_done
&&
2740 (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
)) &&
2741 (the_consumer_data
.type
== LTTNG_CONSUMER32_UST
||
2742 the_consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2743 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2745 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2746 /* Attempt read again, for the data we just flushed. */
2747 local_stream
[i
]->has_data_left_to_be_read_before_teardown
= 1;
2750 * When a stream's pipe dies (hup/err/nval), an "inactive producer" flush is
2751 * performed. This type of flush ensures that a new packet is produced no
2752 * matter the consumed/produced positions are.
2754 * This, in turn, causes the next pass to see that data available for the
2755 * stream. When we come back here, we can be assured that all available
2756 * data has been consumed and we can finally destroy the stream.
2758 * If the poll flag is HUP/ERR/NVAL and we have
2759 * read no data in this pass, we can remove the
2760 * stream from its hash table.
2762 if ((pollfd
[i
].revents
& POLLHUP
)) {
2763 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2764 if (!local_stream
[i
]->has_data_left_to_be_read_before_teardown
) {
2765 consumer_del_stream(local_stream
[i
], data_ht
);
2766 local_stream
[i
] = nullptr;
2768 } else if (pollfd
[i
].revents
& POLLERR
) {
2769 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2770 if (!local_stream
[i
]->has_data_left_to_be_read_before_teardown
) {
2771 consumer_del_stream(local_stream
[i
], data_ht
);
2772 local_stream
[i
] = nullptr;
2774 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2775 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2776 if (!local_stream
[i
]->has_data_left_to_be_read_before_teardown
) {
2777 consumer_del_stream(local_stream
[i
], data_ht
);
2778 local_stream
[i
] = nullptr;
2781 if (local_stream
[i
] != nullptr) {
2782 local_stream
[i
]->has_data_left_to_be_read_before_teardown
= 0;
2789 DBG("polling thread exiting");
2794 * Close the write side of the pipe so epoll_wait() in
2795 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2796 * read side of the pipe. If we close them both, epoll_wait strangely does
2797 * not return and could create a endless wait period if the pipe is the
2798 * only tracked fd in the poll set. The thread will take care of closing
2801 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2806 ERR("Health error occurred in %s", __func__
);
2808 health_unregister(health_consumerd
);
2810 rcu_unregister_thread();
2815 * Close wake-up end of each stream belonging to the channel. This will
2816 * allow the poll() on the stream read-side to detect when the
2817 * write-side (application) finally closes them.
2819 static void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2821 struct lttng_ht
*ht
;
2822 struct lttng_consumer_stream
*stream
;
2823 struct lttng_ht_iter iter
;
2825 ht
= the_consumer_data
.stream_per_chan_id_ht
;
2827 lttng::urcu::read_lock_guard read_lock
;
2828 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2829 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2834 node_channel_id
.node
)
2837 * Protect against teardown with mutex.
2839 pthread_mutex_lock(&stream
->lock
);
2840 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2843 switch (the_consumer_data
.type
) {
2844 case LTTNG_CONSUMER_KERNEL
:
2846 case LTTNG_CONSUMER32_UST
:
2847 case LTTNG_CONSUMER64_UST
:
2848 if (stream
->metadata_flag
) {
2849 /* Safe and protected by the stream lock. */
2850 lttng_ustconsumer_close_metadata(stream
->chan
);
2853 * Note: a mutex is taken internally within
2854 * liblttng-ust-ctl to protect timer wakeup_fd
2855 * use from concurrent close.
2857 lttng_ustconsumer_close_stream_wakeup(stream
);
2861 ERR("Unknown consumer_data type");
2865 pthread_mutex_unlock(&stream
->lock
);
2869 static void destroy_channel_ht(struct lttng_ht
*ht
)
2871 struct lttng_ht_iter iter
;
2872 struct lttng_consumer_channel
*channel
;
2875 if (ht
== nullptr) {
2880 lttng::urcu::read_lock_guard read_lock
;
2882 cds_lfht_for_each_entry (ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2883 ret
= lttng_ht_del(ht
, &iter
);
2884 LTTNG_ASSERT(ret
!= 0);
2888 lttng_ht_destroy(ht
);
2892 * This thread polls the channel fds to detect when they are being
2893 * closed. It closes all related streams if the channel is detected as
2894 * closed. It is currently only used as a shim layer for UST because the
2895 * consumerd needs to keep the per-stream wakeup end of pipes open for
2898 void *consumer_thread_channel_poll(void *data
)
2900 int ret
, i
, pollfd
, err
= -1;
2901 uint32_t revents
, nb_fd
;
2902 struct lttng_consumer_channel
*chan
= nullptr;
2903 struct lttng_ht_iter iter
;
2904 struct lttng_ht_node_u64
*node
;
2905 struct lttng_poll_event events
;
2906 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
2907 struct lttng_ht
*channel_ht
;
2909 rcu_register_thread();
2911 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2913 if (testpoint(consumerd_thread_channel
)) {
2914 goto error_testpoint
;
2917 health_code_update();
2919 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2921 /* ENOMEM at this point. Better to bail out. */
2925 DBG("Thread channel poll started");
2927 /* Size is set to 1 for the consumer_channel pipe */
2928 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2930 ERR("Poll set creation failed");
2934 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2940 DBG("Channel main loop started");
2944 health_code_update();
2945 DBG("Channel poll wait");
2946 health_poll_entry();
2947 ret
= lttng_poll_wait(&events
, -1);
2948 DBG("Channel poll return from wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2950 DBG("Channel event caught in thread");
2952 if (errno
== EINTR
) {
2953 ERR("Poll EINTR caught");
2956 if (LTTNG_POLL_GETNB(&events
) == 0) {
2957 err
= 0; /* All is OK */
2964 /* From here, the event is a channel wait fd */
2965 for (i
= 0; i
< nb_fd
; i
++) {
2966 health_code_update();
2968 revents
= LTTNG_POLL_GETEV(&events
, i
);
2969 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2971 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2972 if (revents
& LPOLLIN
) {
2973 enum consumer_channel_action action
;
2976 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2979 ERR("Error reading channel pipe");
2981 lttng_poll_del(&events
,
2982 ctx
->consumer_channel_pipe
[0]);
2987 case CONSUMER_CHANNEL_ADD
:
2989 DBG("Adding channel %d to poll set", chan
->wait_fd
);
2991 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2993 lttng::urcu::read_lock_guard read_lock
;
2994 lttng_ht_add_unique_u64(channel_ht
,
2995 &chan
->wait_fd_node
);
2996 /* Add channel to the global poll events list */
2997 // FIXME: Empty flag on a pipe pollset, this might
2999 lttng_poll_add(&events
, chan
->wait_fd
, 0);
3002 case CONSUMER_CHANNEL_DEL
:
3005 * This command should never be called if the
3006 * channel has streams monitored by either the data
3007 * or metadata thread. The consumer only notify this
3008 * thread with a channel del. command if it receives
3009 * a destroy channel command from the session daemon
3010 * that send it if a command prior to the
3011 * GET_CHANNEL failed.
3014 lttng::urcu::read_lock_guard read_lock
;
3015 chan
= consumer_find_channel(key
);
3017 ERR("UST consumer get channel key %" PRIu64
3018 " not found for del channel",
3022 lttng_poll_del(&events
, chan
->wait_fd
);
3023 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
3024 ret
= lttng_ht_del(channel_ht
, &iter
);
3025 LTTNG_ASSERT(ret
== 0);
3027 switch (the_consumer_data
.type
) {
3028 case LTTNG_CONSUMER_KERNEL
:
3030 case LTTNG_CONSUMER32_UST
:
3031 case LTTNG_CONSUMER64_UST
:
3032 health_code_update();
3033 /* Destroy streams that might have been left
3034 * in the stream list. */
3035 clean_channel_stream_list(chan
);
3038 ERR("Unknown consumer_data type");
3043 * Release our own refcount. Force channel deletion
3044 * even if streams were not initialized.
3046 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3047 consumer_del_channel(chan
);
3051 case CONSUMER_CHANNEL_QUIT
:
3053 * Remove the pipe from the poll set and continue
3054 * the loop since their might be data to consume.
3056 lttng_poll_del(&events
,
3057 ctx
->consumer_channel_pipe
[0]);
3060 ERR("Unknown action");
3063 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3064 DBG("Channel thread pipe hung up");
3066 * Remove the pipe from the poll set and continue the loop
3067 * since their might be data to consume.
3069 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3072 ERR("Unexpected poll events %u for sock %d",
3078 /* Handle other stream */
3082 lttng::urcu::read_lock_guard read_lock
;
3084 uint64_t tmp_id
= (uint64_t) pollfd
;
3086 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3088 node
= lttng_ht_iter_get_node_u64(&iter
);
3091 chan
= caa_container_of(node
, struct lttng_consumer_channel
, wait_fd_node
);
3093 /* Check for error event */
3094 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3095 DBG("Channel fd %d is hup|err.", pollfd
);
3097 lttng_poll_del(&events
, chan
->wait_fd
);
3098 ret
= lttng_ht_del(channel_ht
, &iter
);
3099 LTTNG_ASSERT(ret
== 0);
3102 * This will close the wait fd for each stream associated to
3103 * this channel AND monitored by the data/metadata thread thus
3104 * will be clean by the right thread.
3106 consumer_close_channel_streams(chan
);
3108 /* Release our own refcount */
3109 if (!uatomic_sub_return(&chan
->refcount
, 1) &&
3110 !uatomic_read(&chan
->nb_init_stream_left
)) {
3111 consumer_del_channel(chan
);
3114 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3118 /* Release RCU lock for the channel looked up */
3125 lttng_poll_clean(&events
);
3127 destroy_channel_ht(channel_ht
);
3130 DBG("Channel poll thread exiting");
3133 ERR("Health error occurred in %s", __func__
);
3135 health_unregister(health_consumerd
);
3136 rcu_unregister_thread();
3140 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3141 struct pollfd
*sockpoll
,
3147 LTTNG_ASSERT(sockpoll
);
3149 ret
= lttng_consumer_poll_socket(sockpoll
);
3153 DBG("Metadata connection on client_socket");
3155 /* Blocking call, waiting for transmission */
3156 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3157 if (ctx
->consumer_metadata_socket
< 0) {
3158 WARN("On accept metadata");
3169 * This thread listens on the consumerd socket and receives the file
3170 * descriptors from the session daemon.
3172 void *consumer_thread_sessiond_poll(void *data
)
3174 int sock
= -1, client_socket
, ret
, err
= -1;
3176 * structure to poll for incoming data on communication socket avoids
3177 * making blocking sockets.
3179 struct pollfd consumer_sockpoll
[2];
3180 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
3182 rcu_register_thread();
3184 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3186 if (testpoint(consumerd_thread_sessiond
)) {
3187 goto error_testpoint
;
3190 health_code_update();
3192 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3193 unlink(ctx
->consumer_command_sock_path
);
3194 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3195 if (client_socket
< 0) {
3196 ERR("Cannot create command socket");
3200 ret
= lttcomm_listen_unix_sock(client_socket
);
3205 DBG("Sending ready command to lttng-sessiond");
3206 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3207 /* return < 0 on error, but == 0 is not fatal */
3209 ERR("Error sending ready command to lttng-sessiond");
3213 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3214 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3215 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3216 consumer_sockpoll
[1].fd
= client_socket
;
3217 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3219 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3227 DBG("Connection on client_socket");
3229 /* Blocking call, waiting for transmission */
3230 sock
= lttcomm_accept_unix_sock(client_socket
);
3237 * Setup metadata socket which is the second socket connection on the
3238 * command unix socket.
3240 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3249 /* This socket is not useful anymore. */
3250 ret
= close(client_socket
);
3252 PERROR("close client_socket");
3256 /* update the polling structure to poll on the established socket */
3257 consumer_sockpoll
[1].fd
= sock
;
3258 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3261 health_code_update();
3263 health_poll_entry();
3264 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3273 DBG("Incoming command on sock");
3274 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3277 * This could simply be a session daemon quitting. Don't output
3280 DBG("Communication interrupted on command socket");
3284 if (CMM_LOAD_SHARED(consumer_quit
)) {
3285 DBG("consumer_thread_receive_fds received quit from signal");
3286 err
= 0; /* All is OK */
3289 DBG("Received command on sock");
3295 DBG("Consumer thread sessiond poll exiting");
3298 * Close metadata streams since the producer is the session daemon which
3301 * NOTE: for now, this only applies to the UST tracer.
3303 lttng_consumer_close_all_metadata();
3306 * when all fds have hung up, the polling thread
3309 CMM_STORE_SHARED(consumer_quit
, 1);
3312 * Notify the data poll thread to poll back again and test the
3313 * consumer_quit state that we just set so to quit gracefully.
3315 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3317 notify_channel_pipe(ctx
, nullptr, -1, CONSUMER_CHANNEL_QUIT
);
3319 notify_health_quit_pipe(health_quit_pipe
);
3321 /* Cleaning up possibly open sockets. */
3325 PERROR("close sock sessiond poll");
3328 if (client_socket
>= 0) {
3329 ret
= close(client_socket
);
3331 PERROR("close client_socket sessiond poll");
3338 ERR("Health error occurred in %s", __func__
);
3340 health_unregister(health_consumerd
);
3342 rcu_unregister_thread();
3346 static int post_consume(struct lttng_consumer_stream
*stream
,
3347 const struct stream_subbuffer
*subbuffer
,
3348 struct lttng_consumer_local_data
*ctx
)
3352 const size_t count
=
3353 lttng_dynamic_array_get_count(&stream
->read_subbuffer_ops
.post_consume_cbs
);
3355 for (i
= 0; i
< count
; i
++) {
3356 const post_consume_cb op
= *(post_consume_cb
*) lttng_dynamic_array_get_element(
3357 &stream
->read_subbuffer_ops
.post_consume_cbs
, i
);
3359 ret
= op(stream
, subbuffer
, ctx
);
3368 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3369 struct lttng_consumer_local_data
*ctx
,
3370 bool locked_by_caller
)
3372 ssize_t ret
, written_bytes
= 0;
3374 struct stream_subbuffer subbuffer
= {};
3375 enum get_next_subbuffer_status get_next_status
;
3377 if (!locked_by_caller
) {
3378 stream
->read_subbuffer_ops
.lock(stream
);
3380 stream
->read_subbuffer_ops
.assert_locked(stream
);
3383 if (stream
->read_subbuffer_ops
.on_wake_up
) {
3384 ret
= stream
->read_subbuffer_ops
.on_wake_up(stream
);
3391 * If the stream was flagged to be ready for rotation before we extract
3392 * the next packet, rotate it now.
3394 if (stream
->rotate_ready
) {
3395 DBG("Rotate stream before consuming data");
3396 ret
= lttng_consumer_rotate_stream(stream
);
3398 ERR("Stream rotation error before consuming data");
3403 get_next_status
= stream
->read_subbuffer_ops
.get_next_subbuffer(stream
, &subbuffer
);
3404 switch (get_next_status
) {
3405 case GET_NEXT_SUBBUFFER_STATUS_OK
:
3407 case GET_NEXT_SUBBUFFER_STATUS_NO_DATA
:
3411 case GET_NEXT_SUBBUFFER_STATUS_ERROR
:
3418 ret
= stream
->read_subbuffer_ops
.pre_consume_subbuffer(stream
, &subbuffer
);
3420 goto error_put_subbuf
;
3423 written_bytes
= stream
->read_subbuffer_ops
.consume_subbuffer(ctx
, stream
, &subbuffer
);
3424 if (written_bytes
<= 0) {
3425 ERR("Error consuming subbuffer: (%zd)", written_bytes
);
3426 ret
= (int) written_bytes
;
3427 goto error_put_subbuf
;
3430 ret
= stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3435 ret
= post_consume(stream
, &subbuffer
, ctx
);
3441 * After extracting the packet, we check if the stream is now ready to
3442 * be rotated and perform the action immediately.
3444 * Don't overwrite `ret` as callers expect the number of bytes
3445 * consumed to be returned on success.
3447 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
3448 if (rotation_ret
== 1) {
3449 rotation_ret
= lttng_consumer_rotate_stream(stream
);
3450 if (rotation_ret
< 0) {
3452 ERR("Stream rotation error after consuming data");
3456 } else if (rotation_ret
< 0) {
3458 ERR("Failed to check if stream was ready to rotate after consuming data");
3463 if (stream
->read_subbuffer_ops
.on_sleep
) {
3464 stream
->read_subbuffer_ops
.on_sleep(stream
, ctx
);
3467 ret
= written_bytes
;
3469 if (!locked_by_caller
) {
3470 stream
->read_subbuffer_ops
.unlock(stream
);
3475 (void) stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3479 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3481 switch (the_consumer_data
.type
) {
3482 case LTTNG_CONSUMER_KERNEL
:
3483 return lttng_kconsumer_on_recv_stream(stream
);
3484 case LTTNG_CONSUMER32_UST
:
3485 case LTTNG_CONSUMER64_UST
:
3486 return lttng_ustconsumer_on_recv_stream(stream
);
3488 ERR("Unknown consumer_data type");
3495 * Allocate and set consumer data hash tables.
3497 int lttng_consumer_init()
3499 the_consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3500 if (!the_consumer_data
.channel_ht
) {
3504 the_consumer_data
.channels_by_session_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3505 if (!the_consumer_data
.channels_by_session_id_ht
) {
3509 the_consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3510 if (!the_consumer_data
.relayd_ht
) {
3514 the_consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3515 if (!the_consumer_data
.stream_list_ht
) {
3519 the_consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3520 if (!the_consumer_data
.stream_per_chan_id_ht
) {
3524 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3529 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3534 the_consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3535 if (!the_consumer_data
.chunk_registry
) {
3546 * Process the ADD_RELAYD command receive by a consumer.
3548 * This will create a relayd socket pair and add it to the relayd hash table.
3549 * The caller MUST acquire a RCU read side lock before calling it.
3551 void consumer_add_relayd_socket(uint64_t net_seq_idx
,
3553 struct lttng_consumer_local_data
*ctx
,
3555 struct pollfd
*consumer_sockpoll
,
3556 uint64_t sessiond_id
,
3557 uint64_t relayd_session_id
,
3558 uint32_t relayd_version_major
,
3559 uint32_t relayd_version_minor
,
3560 enum lttcomm_sock_proto relayd_socket_protocol
)
3562 int fd
= -1, ret
= -1, relayd_created
= 0;
3563 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3564 struct consumer_relayd_sock_pair
*relayd
= nullptr;
3567 LTTNG_ASSERT(sock
>= 0);
3568 ASSERT_RCU_READ_LOCKED();
3570 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3572 /* Get relayd reference if exists. */
3573 relayd
= consumer_find_relayd(net_seq_idx
);
3574 if (relayd
== nullptr) {
3575 LTTNG_ASSERT(sock_type
== LTTNG_STREAM_CONTROL
);
3576 /* Not found. Allocate one. */
3577 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3578 if (relayd
== nullptr) {
3579 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3582 relayd
->sessiond_session_id
= sessiond_id
;
3587 * This code path MUST continue to the consumer send status message to
3588 * we can notify the session daemon and continue our work without
3589 * killing everything.
3593 * relayd key should never be found for control socket.
3595 LTTNG_ASSERT(sock_type
!= LTTNG_STREAM_CONTROL
);
3598 /* First send a status message before receiving the fds. */
3599 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3601 /* Somehow, the session daemon is not responding anymore. */
3602 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3603 goto error_nosignal
;
3606 /* Poll on consumer socket. */
3607 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3609 /* Needing to exit in the middle of a command: error. */
3610 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3611 goto error_nosignal
;
3614 /* Get relayd socket from session daemon */
3615 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3616 if (ret
!= sizeof(fd
)) {
3617 fd
= -1; /* Just in case it gets set with an invalid value. */
3620 * Failing to receive FDs might indicate a major problem such as
3621 * reaching a fd limit during the receive where the kernel returns a
3622 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3623 * don't take any chances and stop everything.
3625 * XXX: Feature request #558 will fix that and avoid this possible
3626 * issue when reaching the fd limit.
3628 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3629 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3633 /* Copy socket information and received FD */
3634 switch (sock_type
) {
3635 case LTTNG_STREAM_CONTROL
:
3636 /* Copy received lttcomm socket */
3637 ret
= lttcomm_populate_sock_from_open_socket(
3638 &relayd
->control_sock
.sock
, fd
, relayd_socket_protocol
);
3640 /* Assign version values. */
3641 relayd
->control_sock
.major
= relayd_version_major
;
3642 relayd
->control_sock
.minor
= relayd_version_minor
;
3644 relayd
->relayd_session_id
= relayd_session_id
;
3647 case LTTNG_STREAM_DATA
:
3648 /* Copy received lttcomm socket */
3649 ret
= lttcomm_populate_sock_from_open_socket(
3650 &relayd
->data_sock
.sock
, fd
, relayd_socket_protocol
);
3651 /* Assign version values. */
3652 relayd
->data_sock
.major
= relayd_version_major
;
3653 relayd
->data_sock
.minor
= relayd_version_minor
;
3656 ERR("Unknown relayd socket type (%d)", sock_type
);
3657 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3662 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3666 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3667 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3668 relayd
->net_seq_idx
,
3671 * We gave the ownership of the fd to the relayd structure. Set the
3672 * fd to -1 so we don't call close() on it in the error path below.
3676 /* We successfully added the socket. Send status back. */
3677 ret
= consumer_send_status_msg(sock
, ret_code
);
3679 /* Somehow, the session daemon is not responding anymore. */
3680 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3681 goto error_nosignal
;
3685 * Add relayd socket pair to consumer data hashtable. If object already
3686 * exists or on error, the function gracefully returns.
3695 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3696 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3700 /* Close received socket if valid. */
3703 PERROR("close received socket");
3707 if (relayd_created
) {
3713 * Search for a relayd associated to the session id and return the reference.
3715 * A rcu read side lock MUST be acquire before calling this function and locked
3716 * until the relayd object is no longer necessary.
3718 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3720 struct lttng_ht_iter iter
;
3721 struct consumer_relayd_sock_pair
*relayd
= nullptr;
3723 ASSERT_RCU_READ_LOCKED();
3725 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3726 cds_lfht_for_each_entry (the_consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
, node
.node
) {
3728 * Check by sessiond id which is unique here where the relayd session
3729 * id might not be when having multiple relayd.
3731 if (relayd
->sessiond_session_id
== id
) {
3732 /* Found the relayd. There can be only one per id. */
3744 * Check if for a given session id there is still data needed to be extract
3747 * Return 1 if data is pending or else 0 meaning ready to be read.
3749 int consumer_data_pending(uint64_t id
)
3752 struct lttng_ht_iter iter
;
3753 struct lttng_ht
*ht
;
3754 struct lttng_consumer_stream
*stream
;
3755 struct consumer_relayd_sock_pair
*relayd
= nullptr;
3756 int (*data_pending
)(struct lttng_consumer_stream
*);
3758 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3760 lttng::urcu::read_lock_guard read_lock
;
3761 pthread_mutex_lock(&the_consumer_data
.lock
);
3763 switch (the_consumer_data
.type
) {
3764 case LTTNG_CONSUMER_KERNEL
:
3765 data_pending
= lttng_kconsumer_data_pending
;
3767 case LTTNG_CONSUMER32_UST
:
3768 case LTTNG_CONSUMER64_UST
:
3769 data_pending
= lttng_ustconsumer_data_pending
;
3772 ERR("Unknown consumer data type");
3776 /* Ease our life a bit */
3777 ht
= the_consumer_data
.stream_list_ht
;
3779 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3780 ht
->hash_fct(&id
, lttng_ht_seed
),
3785 node_session_id
.node
)
3787 pthread_mutex_lock(&stream
->lock
);
3790 * A removed node from the hash table indicates that the stream has
3791 * been deleted thus having a guarantee that the buffers are closed
3792 * on the consumer side. However, data can still be transmitted
3793 * over the network so don't skip the relayd check.
3795 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3797 /* Check the stream if there is data in the buffers. */
3798 ret
= data_pending(stream
);
3800 pthread_mutex_unlock(&stream
->lock
);
3805 pthread_mutex_unlock(&stream
->lock
);
3808 relayd
= find_relayd_by_session_id(id
);
3810 unsigned int is_data_inflight
= 0;
3812 /* Send init command for data pending. */
3813 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3814 ret
= relayd_begin_data_pending(&relayd
->control_sock
, relayd
->relayd_session_id
);
3816 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3817 /* Communication error thus the relayd so no data pending. */
3818 goto data_not_pending
;
3821 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3822 ht
->hash_fct(&id
, lttng_ht_seed
),
3827 node_session_id
.node
)
3829 if (stream
->metadata_flag
) {
3830 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3831 stream
->relayd_stream_id
);
3833 ret
= relayd_data_pending(&relayd
->control_sock
,
3834 stream
->relayd_stream_id
,
3835 stream
->next_net_seq_num
- 1);
3839 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3841 } else if (ret
< 0) {
3842 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".",
3843 relayd
->net_seq_idx
);
3844 lttng_consumer_cleanup_relayd(relayd
);
3845 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3846 goto data_not_pending
;
3850 /* Send end command for data pending. */
3851 ret
= relayd_end_data_pending(
3852 &relayd
->control_sock
, relayd
->relayd_session_id
, &is_data_inflight
);
3853 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3855 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".",
3856 relayd
->net_seq_idx
);
3857 lttng_consumer_cleanup_relayd(relayd
);
3858 goto data_not_pending
;
3860 if (is_data_inflight
) {
3866 * Finding _no_ node in the hash table and no inflight data means that the
3867 * stream(s) have been removed thus data is guaranteed to be available for
3868 * analysis from the trace files.
3872 /* Data is available to be read by a viewer. */
3873 pthread_mutex_unlock(&the_consumer_data
.lock
);
3877 /* Data is still being extracted from buffers. */
3878 pthread_mutex_unlock(&the_consumer_data
.lock
);
3883 * Send a ret code status message to the sessiond daemon.
3885 * Return the sendmsg() return value.
3887 int consumer_send_status_msg(int sock
, int ret_code
)
3889 struct lttcomm_consumer_status_msg msg
;
3891 memset(&msg
, 0, sizeof(msg
));
3892 msg
.ret_code
= (lttcomm_return_code
) ret_code
;
3894 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3898 * Send a channel status message to the sessiond daemon.
3900 * Return the sendmsg() return value.
3902 int consumer_send_status_channel(int sock
, struct lttng_consumer_channel
*channel
)
3904 struct lttcomm_consumer_status_channel msg
;
3906 LTTNG_ASSERT(sock
>= 0);
3908 memset(&msg
, 0, sizeof(msg
));
3910 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3912 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3913 msg
.key
= channel
->key
;
3914 msg
.stream_count
= channel
->streams
.count
;
3917 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3920 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3921 unsigned long produced_pos
,
3922 uint64_t nb_packets_per_stream
,
3923 uint64_t max_sb_size
)
3925 unsigned long start_pos
;
3927 if (!nb_packets_per_stream
) {
3928 return consumed_pos
; /* Grab everything */
3930 start_pos
= produced_pos
- lttng_offset_align_floor(produced_pos
, max_sb_size
);
3931 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3932 if ((long) (start_pos
- consumed_pos
) < 0) {
3933 return consumed_pos
; /* Grab everything */
3938 /* Stream lock must be held by the caller. */
3939 static int sample_stream_positions(struct lttng_consumer_stream
*stream
,
3940 unsigned long *produced
,
3941 unsigned long *consumed
)
3945 ASSERT_LOCKED(stream
->lock
);
3947 ret
= lttng_consumer_sample_snapshot_positions(stream
);
3949 ERR("Failed to sample snapshot positions");
3953 ret
= lttng_consumer_get_produced_snapshot(stream
, produced
);
3955 ERR("Failed to sample produced position");
3959 ret
= lttng_consumer_get_consumed_snapshot(stream
, consumed
);
3961 ERR("Failed to sample consumed position");
3970 * Sample the rotate position for all the streams of a channel. If a stream
3971 * is already at the rotate position (produced == consumed), we flag it as
3972 * ready for rotation. The rotation of ready streams occurs after we have
3973 * replied to the session daemon that we have finished sampling the positions.
3974 * Must be called with RCU read-side lock held to ensure existence of channel.
3976 * Returns 0 on success, < 0 on error
3978 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
3983 struct lttng_consumer_stream
*stream
;
3984 struct lttng_ht_iter iter
;
3985 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
3986 struct lttng_dynamic_array stream_rotation_positions
;
3987 uint64_t next_chunk_id
, stream_count
= 0;
3988 enum lttng_trace_chunk_status chunk_status
;
3989 const bool is_local_trace
= relayd_id
== -1ULL;
3990 struct consumer_relayd_sock_pair
*relayd
= nullptr;
3991 bool rotating_to_new_chunk
= true;
3992 /* Array of `struct lttng_consumer_stream *` */
3993 struct lttng_dynamic_pointer_array streams_packet_to_open
;
3996 ASSERT_RCU_READ_LOCKED();
3998 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
4000 lttng_dynamic_array_init(&stream_rotation_positions
,
4001 sizeof(struct relayd_stream_rotation_position
),
4003 lttng_dynamic_pointer_array_init(&streams_packet_to_open
, nullptr);
4005 lttng::urcu::read_lock_guard read_lock
;
4007 pthread_mutex_lock(&channel
->lock
);
4008 LTTNG_ASSERT(channel
->trace_chunk
);
4009 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
, &next_chunk_id
);
4010 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4012 goto end_unlock_channel
;
4015 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4016 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4021 node_channel_id
.node
)
4023 unsigned long produced_pos
= 0, consumed_pos
= 0;
4025 health_code_update();
4028 * Lock stream because we are about to change its state.
4030 pthread_mutex_lock(&stream
->lock
);
4032 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
4033 rotating_to_new_chunk
= false;
4037 * Do not flush a packet when rotating from a NULL trace
4038 * chunk. The stream has no means to output data, and the prior
4039 * rotation which rotated to NULL performed that side-effect
4040 * already. No new data can be produced when a stream has no
4041 * associated trace chunk (e.g. a stop followed by a rotate).
4043 if (stream
->trace_chunk
) {
4046 if (stream
->metadata_flag
) {
4048 * Don't produce an empty metadata packet,
4049 * simply close the current one.
4051 * Metadata is regenerated on every trace chunk
4052 * switch; there is no concern that no data was
4055 flush_active
= true;
4058 * Only flush an empty packet if the "packet
4059 * open" could not be performed on transition
4060 * to a new trace chunk and no packets were
4061 * consumed within the chunk's lifetime.
4063 if (stream
->opened_packet_in_current_trace_chunk
) {
4064 flush_active
= true;
4067 * Stream could have been full at the
4068 * time of rotation, but then have had
4069 * no activity at all.
4071 * It is important to flush a packet
4072 * to prevent 0-length files from being
4073 * produced as most viewers choke on
4076 * Unfortunately viewers will not be
4077 * able to know that tracing was active
4078 * for this stream during this trace
4081 ret
= sample_stream_positions(
4082 stream
, &produced_pos
, &consumed_pos
);
4084 goto end_unlock_stream
;
4088 * Don't flush an empty packet if data
4089 * was produced; it will be consumed
4090 * before the rotation completes.
4092 flush_active
= produced_pos
!= consumed_pos
;
4093 if (!flush_active
) {
4094 const char *trace_chunk_name
;
4095 uint64_t trace_chunk_id
;
4097 chunk_status
= lttng_trace_chunk_get_name(
4098 stream
->trace_chunk
,
4101 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NONE
) {
4102 trace_chunk_name
= "none";
4106 * Consumer trace chunks are
4109 chunk_status
= lttng_trace_chunk_get_id(
4110 stream
->trace_chunk
, &trace_chunk_id
);
4111 LTTNG_ASSERT(chunk_status
==
4112 LTTNG_TRACE_CHUNK_STATUS_OK
);
4114 DBG("Unable to open packet for stream during trace chunk's lifetime. "
4115 "Flushing an empty packet to prevent an empty file from being created: "
4116 "stream id = %" PRIu64
4117 ", trace chunk name = `%s`, trace chunk id = %" PRIu64
,
4126 * Close the current packet before sampling the
4127 * ring buffer positions.
4129 ret
= consumer_stream_flush_buffer(stream
, flush_active
);
4131 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4133 goto end_unlock_stream
;
4137 ret
= lttng_consumer_take_snapshot(stream
);
4138 if (ret
< 0 && ret
!= -ENODATA
&& ret
!= -EAGAIN
) {
4139 ERR("Failed to sample snapshot position during channel rotation");
4140 goto end_unlock_stream
;
4143 ret
= lttng_consumer_get_produced_snapshot(stream
, &produced_pos
);
4145 ERR("Failed to sample produced position during channel rotation");
4146 goto end_unlock_stream
;
4149 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos
);
4151 ERR("Failed to sample consumed position during channel rotation");
4152 goto end_unlock_stream
;
4156 * Align produced position on the start-of-packet boundary of the first
4157 * packet going into the next trace chunk.
4159 produced_pos
= lttng_align_floor(produced_pos
, stream
->max_sb_size
);
4160 if (consumed_pos
== produced_pos
) {
4161 DBG("Set rotate ready for stream %" PRIu64
" produced = %lu consumed = %lu",
4165 stream
->rotate_ready
= true;
4167 DBG("Different consumed and produced positions "
4168 "for stream %" PRIu64
" produced = %lu consumed = %lu",
4174 * The rotation position is based on the packet_seq_num of the
4175 * packet following the last packet that was consumed for this
4176 * stream, incremented by the offset between produced and
4177 * consumed positions. This rotation position is a lower bound
4178 * (inclusive) at which the next trace chunk starts. Since it
4179 * is a lower bound, it is OK if the packet_seq_num does not
4180 * correspond exactly to the same packet identified by the
4181 * consumed_pos, which can happen in overwrite mode.
4183 if (stream
->sequence_number_unavailable
) {
4185 * Rotation should never be performed on a session which
4186 * interacts with a pre-2.8 lttng-modules, which does
4187 * not implement packet sequence number.
4189 ERR("Failure to rotate stream %" PRIu64
": sequence number unavailable",
4192 goto end_unlock_stream
;
4194 stream
->rotate_position
= stream
->last_sequence_number
+ 1 +
4195 ((produced_pos
- consumed_pos
) / stream
->max_sb_size
);
4196 DBG("Set rotation position for stream %" PRIu64
" at position %" PRIu64
,
4198 stream
->rotate_position
);
4200 if (!is_local_trace
) {
4202 * The relay daemon control protocol expects a rotation
4203 * position as "the sequence number of the first packet
4204 * _after_ the current trace chunk".
4206 const struct relayd_stream_rotation_position position
= {
4207 .stream_id
= stream
->relayd_stream_id
,
4208 .rotate_at_seq_num
= stream
->rotate_position
,
4211 ret
= lttng_dynamic_array_add_element(&stream_rotation_positions
,
4214 ERR("Failed to allocate stream rotation position");
4215 goto end_unlock_stream
;
4220 stream
->opened_packet_in_current_trace_chunk
= false;
4222 if (rotating_to_new_chunk
&& !stream
->metadata_flag
) {
4224 * Attempt to flush an empty packet as close to the
4225 * rotation point as possible. In the event where a
4226 * stream remains inactive after the rotation point,
4227 * this ensures that the new trace chunk has a
4228 * beginning timestamp set at the begining of the
4229 * trace chunk instead of only creating an empty
4230 * packet when the trace chunk is stopped.
4232 * This indicates to the viewers that the stream
4233 * was being recorded, but more importantly it
4234 * allows viewers to determine a useable trace
4237 * This presents a problem in the case where the
4238 * ring-buffer is completely full.
4240 * Consider the following scenario:
4241 * - The consumption of data is slow (slow network,
4243 * - The ring buffer is full,
4244 * - A rotation is initiated,
4245 * - The flush below does nothing (no space left to
4246 * open a new packet),
4247 * - The other streams rotate very soon, and new
4248 * data is produced in the new chunk,
4249 * - This stream completes its rotation long after the
4250 * rotation was initiated
4251 * - The session is stopped before any event can be
4252 * produced in this stream's buffers.
4254 * The resulting trace chunk will have a single packet
4255 * temporaly at the end of the trace chunk for this
4256 * stream making the stream intersection more narrow
4257 * than it should be.
4259 * To work-around this, an empty flush is performed
4260 * after the first consumption of a packet during a
4261 * rotation if open_packet fails. The idea is that
4262 * consuming a packet frees enough space to switch
4263 * packets in this scenario and allows the tracer to
4264 * "stamp" the beginning of the new trace chunk at the
4265 * earliest possible point.
4267 * The packet open is performed after the channel
4268 * rotation to ensure that no attempt to open a packet
4269 * is performed in a stream that has no active trace
4272 ret
= lttng_dynamic_pointer_array_add_pointer(&streams_packet_to_open
,
4275 PERROR("Failed to add a stream pointer to array of streams in which to open a packet");
4277 goto end_unlock_stream
;
4281 pthread_mutex_unlock(&stream
->lock
);
4285 if (!is_local_trace
) {
4286 relayd
= consumer_find_relayd(relayd_id
);
4288 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4290 goto end_unlock_channel
;
4293 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4294 ret
= relayd_rotate_streams(&relayd
->control_sock
,
4296 rotating_to_new_chunk
? &next_chunk_id
: nullptr,
4297 (const struct relayd_stream_rotation_position
*)
4298 stream_rotation_positions
.buffer
.data
);
4299 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4301 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4302 relayd
->net_seq_idx
);
4303 lttng_consumer_cleanup_relayd(relayd
);
4304 goto end_unlock_channel
;
4308 for (stream_idx
= 0;
4309 stream_idx
< lttng_dynamic_pointer_array_get_count(&streams_packet_to_open
);
4311 enum consumer_stream_open_packet_status status
;
4313 stream
= (lttng_consumer_stream
*) lttng_dynamic_pointer_array_get_pointer(
4314 &streams_packet_to_open
, stream_idx
);
4316 pthread_mutex_lock(&stream
->lock
);
4317 status
= consumer_stream_open_packet(stream
);
4318 pthread_mutex_unlock(&stream
->lock
);
4320 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
4321 DBG("Opened a packet after a rotation: stream id = %" PRIu64
4322 ", channel name = %s, session id = %" PRIu64
,
4325 stream
->chan
->session_id
);
4327 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
4329 * Can't open a packet as there is no space left
4330 * in the buffer. A new packet will be opened
4331 * once one has been consumed.
4333 DBG("No space left to open a packet after a rotation: stream id = %" PRIu64
4334 ", channel name = %s, session id = %" PRIu64
,
4337 stream
->chan
->session_id
);
4339 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
4340 /* Logged by callee. */
4342 goto end_unlock_channel
;
4348 pthread_mutex_unlock(&channel
->lock
);
4353 pthread_mutex_unlock(&stream
->lock
);
4355 pthread_mutex_unlock(&channel
->lock
);
4357 lttng_dynamic_array_reset(&stream_rotation_positions
);
4358 lttng_dynamic_pointer_array_reset(&streams_packet_to_open
);
4362 static int consumer_clear_buffer(struct lttng_consumer_stream
*stream
)
4365 unsigned long consumed_pos_before
, consumed_pos_after
;
4367 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4369 ERR("Taking snapshot positions");
4373 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_before
);
4375 ERR("Consumed snapshot position");
4379 switch (the_consumer_data
.type
) {
4380 case LTTNG_CONSUMER_KERNEL
:
4381 ret
= kernctl_buffer_clear(stream
->wait_fd
);
4383 ERR("Failed to clear kernel stream (ret = %d)", ret
);
4387 case LTTNG_CONSUMER32_UST
:
4388 case LTTNG_CONSUMER64_UST
:
4389 ret
= lttng_ustconsumer_clear_buffer(stream
);
4391 ERR("Failed to clear ust stream (ret = %d)", ret
);
4396 ERR("Unknown consumer_data type");
4400 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4402 ERR("Taking snapshot positions");
4405 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_after
);
4407 ERR("Consumed snapshot position");
4410 DBG("clear: before: %lu after: %lu", consumed_pos_before
, consumed_pos_after
);
4415 static int consumer_clear_stream(struct lttng_consumer_stream
*stream
)
4419 ret
= consumer_stream_flush_buffer(stream
, true);
4421 ERR("Failed to flush stream %" PRIu64
" during channel clear", stream
->key
);
4422 ret
= LTTCOMM_CONSUMERD_FATAL
;
4426 ret
= consumer_clear_buffer(stream
);
4428 ERR("Failed to clear stream %" PRIu64
" during channel clear", stream
->key
);
4429 ret
= LTTCOMM_CONSUMERD_FATAL
;
4433 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4438 static int consumer_clear_unmonitored_channel(struct lttng_consumer_channel
*channel
)
4441 struct lttng_consumer_stream
*stream
;
4443 lttng::urcu::read_lock_guard read_lock
;
4444 pthread_mutex_lock(&channel
->lock
);
4445 cds_list_for_each_entry (stream
, &channel
->streams
.head
, send_node
) {
4446 health_code_update();
4447 pthread_mutex_lock(&stream
->lock
);
4448 ret
= consumer_clear_stream(stream
);
4452 pthread_mutex_unlock(&stream
->lock
);
4454 pthread_mutex_unlock(&channel
->lock
);
4458 pthread_mutex_unlock(&stream
->lock
);
4459 pthread_mutex_unlock(&channel
->lock
);
4464 * Check if a stream is ready to be rotated after extracting it.
4466 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4467 * error. Stream lock must be held.
4469 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4471 DBG("Check is rotate ready for stream %" PRIu64
" ready %u rotate_position %" PRIu64
4472 " last_sequence_number %" PRIu64
,
4474 stream
->rotate_ready
,
4475 stream
->rotate_position
,
4476 stream
->last_sequence_number
);
4477 if (stream
->rotate_ready
) {
4482 * If packet seq num is unavailable, it means we are interacting
4483 * with a pre-2.8 lttng-modules which does not implement the
4484 * sequence number. Rotation should never be used by sessiond in this
4487 if (stream
->sequence_number_unavailable
) {
4488 ERR("Internal error: rotation used on stream %" PRIu64
4489 " with unavailable sequence number",
4494 if (stream
->rotate_position
== -1ULL || stream
->last_sequence_number
== -1ULL) {
4499 * Rotate position not reached yet. The stream rotate position is
4500 * the position of the next packet belonging to the next trace chunk,
4501 * but consumerd considers rotation ready when reaching the last
4502 * packet of the current chunk, hence the "rotate_position - 1".
4505 DBG("Check is rotate ready for stream %" PRIu64
" last_sequence_number %" PRIu64
4506 " rotate_position %" PRIu64
,
4508 stream
->last_sequence_number
,
4509 stream
->rotate_position
);
4510 if (stream
->last_sequence_number
>= stream
->rotate_position
- 1) {
4518 * Reset the state for a stream after a rotation occurred.
4520 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4522 DBG("lttng_consumer_reset_stream_rotate_state for stream %" PRIu64
, stream
->key
);
4523 stream
->rotate_position
= -1ULL;
4524 stream
->rotate_ready
= false;
4528 * Perform the rotation a local stream file.
4530 static int rotate_local_stream(struct lttng_consumer_stream
*stream
)
4534 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4537 stream
->tracefile_size_current
= 0;
4538 stream
->tracefile_count_current
= 0;
4540 if (stream
->out_fd
>= 0) {
4541 ret
= close(stream
->out_fd
);
4543 PERROR("Failed to close stream out_fd of channel \"%s\"",
4544 stream
->chan
->name
);
4546 stream
->out_fd
= -1;
4549 if (stream
->index_file
) {
4550 lttng_index_file_put(stream
->index_file
);
4551 stream
->index_file
= nullptr;
4554 if (!stream
->trace_chunk
) {
4558 ret
= consumer_stream_create_output_files(stream
, true);
4564 * Performs the stream rotation for the rotate session feature if needed.
4565 * It must be called with the channel and stream locks held.
4567 * Return 0 on success, a negative number of error.
4569 int lttng_consumer_rotate_stream(struct lttng_consumer_stream
*stream
)
4573 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4576 * Update the stream's 'current' chunk to the session's (channel)
4577 * now-current chunk.
4579 lttng_trace_chunk_put(stream
->trace_chunk
);
4580 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4582 * A channel can be rotated and not have a "next" chunk
4583 * to transition to. In that case, the channel's "current chunk"
4584 * has not been closed yet, but it has not been updated to
4585 * a "next" trace chunk either. Hence, the stream, like its
4586 * parent channel, becomes part of no chunk and can't output
4587 * anything until a new trace chunk is created.
4589 stream
->trace_chunk
= nullptr;
4590 } else if (stream
->chan
->trace_chunk
&& !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4591 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4596 * Update the stream's trace chunk to its parent channel's
4597 * current trace chunk.
4599 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4602 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4603 ret
= rotate_local_stream(stream
);
4605 ERR("Failed to rotate stream, ret = %i", ret
);
4610 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4612 * If the stream has transitioned to a new trace
4613 * chunk, the metadata should be re-dumped to the
4616 * However, it is possible for a stream to transition to
4617 * a "no-chunk" state. This can happen if a rotation
4618 * occurs on an inactive session. In such cases, the metadata
4619 * regeneration will happen when the next trace chunk is
4622 ret
= consumer_metadata_stream_dump(stream
);
4627 lttng_consumer_reset_stream_rotate_state(stream
);
4636 * Rotate all the ready streams now.
4638 * This is especially important for low throughput streams that have already
4639 * been consumed, we cannot wait for their next packet to perform the
4641 * Need to be called with RCU read-side lock held to ensure existence of
4644 * Returns 0 on success, < 0 on error
4646 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
, uint64_t key
)
4649 struct lttng_consumer_stream
*stream
;
4650 struct lttng_ht_iter iter
;
4651 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
4653 ASSERT_RCU_READ_LOCKED();
4655 lttng::urcu::read_lock_guard read_lock
;
4657 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4659 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4660 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4665 node_channel_id
.node
)
4667 health_code_update();
4669 pthread_mutex_lock(&stream
->chan
->lock
);
4670 pthread_mutex_lock(&stream
->lock
);
4672 if (!stream
->rotate_ready
) {
4673 pthread_mutex_unlock(&stream
->lock
);
4674 pthread_mutex_unlock(&stream
->chan
->lock
);
4677 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4679 ret
= lttng_consumer_rotate_stream(stream
);
4680 pthread_mutex_unlock(&stream
->lock
);
4681 pthread_mutex_unlock(&stream
->chan
->lock
);
4693 enum lttcomm_return_code
lttng_consumer_init_command(struct lttng_consumer_local_data
*ctx
,
4694 const lttng_uuid
& sessiond_uuid
)
4696 enum lttcomm_return_code ret
;
4697 char uuid_str
[LTTNG_UUID_STR_LEN
];
4699 if (ctx
->sessiond_uuid
.is_set
) {
4700 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4704 ctx
->sessiond_uuid
.is_set
= true;
4705 ctx
->sessiond_uuid
.value
= sessiond_uuid
;
4706 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4707 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4708 DBG("Received session daemon UUID: %s", uuid_str
);
4713 enum lttcomm_return_code
4714 lttng_consumer_create_trace_chunk(const uint64_t *relayd_id
,
4715 uint64_t session_id
,
4717 time_t chunk_creation_timestamp
,
4718 const char *chunk_override_name
,
4719 const struct lttng_credentials
*credentials
,
4720 struct lttng_directory_handle
*chunk_directory_handle
)
4723 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4724 struct lttng_trace_chunk
*created_chunk
= nullptr, *published_chunk
= nullptr;
4725 enum lttng_trace_chunk_status chunk_status
;
4726 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4727 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4728 const char *relayd_id_str
= "(none)";
4729 const char *creation_timestamp_str
;
4730 struct lttng_ht_iter iter
;
4731 struct lttng_consumer_channel
*channel
;
4734 /* Only used for logging purposes. */
4735 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
), "%" PRIu64
, *relayd_id
);
4736 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4737 relayd_id_str
= relayd_id_buffer
;
4739 relayd_id_str
= "(formatting error)";
4743 /* Local protocol error. */
4744 LTTNG_ASSERT(chunk_creation_timestamp
);
4745 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4746 creation_timestamp_buffer
,
4747 sizeof(creation_timestamp_buffer
));
4748 creation_timestamp_str
= !ret
? creation_timestamp_buffer
: "(formatting error)";
4750 DBG("Consumer create trace chunk command: relay_id = %s"
4751 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
", chunk_override_name = %s"
4752 ", chunk_creation_timestamp = %s",
4756 chunk_override_name
?: "(none)",
4757 creation_timestamp_str
);
4760 * The trace chunk registry, as used by the consumer daemon, implicitly
4761 * owns the trace chunks. This is only needed in the consumer since
4762 * the consumer has no notion of a session beyond session IDs being
4763 * used to identify other objects.
4765 * The lttng_trace_chunk_registry_publish() call below provides a
4766 * reference which is not released; it implicitly becomes the session
4767 * daemon's reference to the chunk in the consumer daemon.
4769 * The lifetime of trace chunks in the consumer daemon is managed by
4770 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4771 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4773 created_chunk
= lttng_trace_chunk_create(chunk_id
, chunk_creation_timestamp
, nullptr);
4774 if (!created_chunk
) {
4775 ERR("Failed to create trace chunk");
4776 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4780 if (chunk_override_name
) {
4781 chunk_status
= lttng_trace_chunk_override_name(created_chunk
, chunk_override_name
);
4782 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4783 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4788 if (chunk_directory_handle
) {
4789 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
, credentials
);
4790 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4791 ERR("Failed to set trace chunk credentials");
4792 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4796 * The consumer daemon has no ownership of the chunk output
4799 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
, chunk_directory_handle
);
4800 chunk_directory_handle
= nullptr;
4801 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4802 ERR("Failed to set trace chunk's directory handle");
4803 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4808 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4809 the_consumer_data
.chunk_registry
, session_id
, created_chunk
);
4810 lttng_trace_chunk_put(created_chunk
);
4811 created_chunk
= nullptr;
4812 if (!published_chunk
) {
4813 ERR("Failed to publish trace chunk");
4814 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4819 lttng::urcu::read_lock_guard read_lock
;
4820 cds_lfht_for_each_entry_duplicate(
4821 the_consumer_data
.channels_by_session_id_ht
->ht
,
4822 the_consumer_data
.channels_by_session_id_ht
->hash_fct(&session_id
,
4824 the_consumer_data
.channels_by_session_id_ht
->match_fct
,
4828 channels_by_session_id_ht_node
.node
)
4830 ret
= lttng_consumer_channel_set_trace_chunk(channel
, published_chunk
);
4833 * Roll-back the creation of this chunk.
4835 * This is important since the session daemon will
4836 * assume that the creation of this chunk failed and
4837 * will never ask for it to be closed, resulting
4838 * in a leak and an inconsistent state for some
4841 enum lttcomm_return_code close_ret
;
4842 char path
[LTTNG_PATH_MAX
];
4844 DBG("Failed to set new trace chunk on existing channels, rolling back");
4846 lttng_consumer_close_trace_chunk(relayd_id
,
4849 chunk_creation_timestamp
,
4852 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4853 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
4854 ", chunk_id = %" PRIu64
,
4859 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4866 struct consumer_relayd_sock_pair
*relayd
;
4868 relayd
= consumer_find_relayd(*relayd_id
);
4870 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4871 ret
= relayd_create_trace_chunk(&relayd
->control_sock
, published_chunk
);
4872 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4874 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4877 if (!relayd
|| ret
) {
4878 enum lttcomm_return_code close_ret
;
4879 char path
[LTTNG_PATH_MAX
];
4881 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4884 chunk_creation_timestamp
,
4887 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4888 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
4889 ", chunk_id = %" PRIu64
,
4894 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4900 /* Release the reference returned by the "publish" operation. */
4901 lttng_trace_chunk_put(published_chunk
);
4902 lttng_trace_chunk_put(created_chunk
);
4906 enum lttcomm_return_code
4907 lttng_consumer_close_trace_chunk(const uint64_t *relayd_id
,
4908 uint64_t session_id
,
4910 time_t chunk_close_timestamp
,
4911 const enum lttng_trace_chunk_command_type
*close_command
,
4914 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4915 struct lttng_trace_chunk
*chunk
;
4916 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4917 const char *relayd_id_str
= "(none)";
4918 const char *close_command_name
= "none";
4919 struct lttng_ht_iter iter
;
4920 struct lttng_consumer_channel
*channel
;
4921 enum lttng_trace_chunk_status chunk_status
;
4926 /* Only used for logging purposes. */
4927 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
), "%" PRIu64
, *relayd_id
);
4928 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4929 relayd_id_str
= relayd_id_buffer
;
4931 relayd_id_str
= "(formatting error)";
4934 if (close_command
) {
4935 close_command_name
= lttng_trace_chunk_command_type_get_name(*close_command
);
4938 DBG("Consumer close trace chunk command: relayd_id = %s"
4939 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
", close command = %s",
4943 close_command_name
);
4945 chunk
= lttng_trace_chunk_registry_find_chunk(
4946 the_consumer_data
.chunk_registry
, session_id
, chunk_id
);
4948 ERR("Failed to find chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4951 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4955 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
, chunk_close_timestamp
);
4956 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4957 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4961 if (close_command
) {
4962 chunk_status
= lttng_trace_chunk_set_close_command(chunk
, *close_command
);
4963 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4964 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4970 * chunk is now invalid to access as we no longer hold a reference to
4971 * it; it is only kept around to compare it (by address) to the
4972 * current chunk found in the session's channels.
4975 lttng::urcu::read_lock_guard read_lock
;
4976 cds_lfht_for_each_entry (
4977 the_consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
, node
.node
) {
4981 * Only change the channel's chunk to NULL if it still
4982 * references the chunk being closed. The channel may
4983 * reference a newer channel in the case of a session
4984 * rotation. When a session rotation occurs, the "next"
4985 * chunk is created before the "current" chunk is closed.
4987 if (channel
->trace_chunk
!= chunk
) {
4990 ret
= lttng_consumer_channel_set_trace_chunk(channel
, nullptr);
4993 * Attempt to close the chunk on as many channels as
4996 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5002 struct consumer_relayd_sock_pair
*relayd
;
5004 relayd
= consumer_find_relayd(*relayd_id
);
5006 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5007 ret
= relayd_close_trace_chunk(&relayd
->control_sock
, chunk
, path
);
5008 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5010 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
5013 if (!relayd
|| ret
) {
5014 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5021 * Release the reference returned by the "find" operation and
5022 * the session daemon's implicit reference to the chunk.
5024 lttng_trace_chunk_put(chunk
);
5025 lttng_trace_chunk_put(chunk
);
5030 enum lttcomm_return_code
5031 lttng_consumer_trace_chunk_exists(const uint64_t *relayd_id
, uint64_t session_id
, uint64_t chunk_id
)
5034 enum lttcomm_return_code ret_code
;
5035 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
5036 const char *relayd_id_str
= "(none)";
5037 const bool is_local_trace
= !relayd_id
;
5038 struct consumer_relayd_sock_pair
*relayd
= nullptr;
5039 bool chunk_exists_local
, chunk_exists_remote
;
5040 lttng::urcu::read_lock_guard read_lock
;
5043 /* Only used for logging purposes. */
5044 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
), "%" PRIu64
, *relayd_id
);
5045 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
5046 relayd_id_str
= relayd_id_buffer
;
5048 relayd_id_str
= "(formatting error)";
5052 DBG("Consumer trace chunk exists command: relayd_id = %s"
5053 ", chunk_id = %" PRIu64
,
5056 ret
= lttng_trace_chunk_registry_chunk_exists(
5057 the_consumer_data
.chunk_registry
, session_id
, chunk_id
, &chunk_exists_local
);
5059 /* Internal error. */
5060 ERR("Failed to query the existence of a trace chunk");
5061 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
5064 DBG("Trace chunk %s locally", chunk_exists_local
? "exists" : "does not exist");
5065 if (chunk_exists_local
) {
5066 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
5068 } else if (is_local_trace
) {
5069 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5073 relayd
= consumer_find_relayd(*relayd_id
);
5075 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
5076 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5077 goto end_rcu_unlock
;
5079 DBG("Looking up existence of trace chunk on relay daemon");
5080 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5081 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
, &chunk_exists_remote
);
5082 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5084 ERR("Failed to look-up the existence of trace chunk on relay daemon");
5085 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
5086 goto end_rcu_unlock
;
5089 ret_code
= chunk_exists_remote
? LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
5090 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5091 DBG("Trace chunk %s on relay daemon", chunk_exists_remote
? "exists" : "does not exist");
5098 static int consumer_clear_monitored_channel(struct lttng_consumer_channel
*channel
)
5100 struct lttng_ht
*ht
;
5101 struct lttng_consumer_stream
*stream
;
5102 struct lttng_ht_iter iter
;
5105 ht
= the_consumer_data
.stream_per_chan_id_ht
;
5107 lttng::urcu::read_lock_guard read_lock
;
5108 cds_lfht_for_each_entry_duplicate(ht
->ht
,
5109 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
5114 node_channel_id
.node
)
5117 * Protect against teardown with mutex.
5119 pthread_mutex_lock(&stream
->lock
);
5120 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5123 ret
= consumer_clear_stream(stream
);
5128 pthread_mutex_unlock(&stream
->lock
);
5130 return LTTCOMM_CONSUMERD_SUCCESS
;
5133 pthread_mutex_unlock(&stream
->lock
);
5137 int lttng_consumer_clear_channel(struct lttng_consumer_channel
*channel
)
5141 DBG("Consumer clear channel %" PRIu64
, channel
->key
);
5143 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
5145 * Nothing to do for the metadata channel/stream.
5146 * Snapshot mechanism already take care of the metadata
5147 * handling/generation, and monitored channels only need to
5148 * have their data stream cleared..
5150 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5154 if (!channel
->monitor
) {
5155 ret
= consumer_clear_unmonitored_channel(channel
);
5157 ret
= consumer_clear_monitored_channel(channel
);
5163 enum lttcomm_return_code
lttng_consumer_open_channel_packets(struct lttng_consumer_channel
*channel
)
5165 struct lttng_consumer_stream
*stream
;
5166 enum lttcomm_return_code ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5168 if (channel
->metadata_stream
) {
5169 ERR("Open channel packets command attempted on a metadata channel");
5170 ret
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5175 lttng::urcu::read_lock_guard read_lock
;
5176 cds_list_for_each_entry (stream
, &channel
->streams
.head
, send_node
) {
5177 enum consumer_stream_open_packet_status status
;
5179 pthread_mutex_lock(&stream
->lock
);
5180 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5184 status
= consumer_stream_open_packet(stream
);
5186 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
5187 DBG("Opened a packet in \"open channel packets\" command: stream id = %" PRIu64
5188 ", channel name = %s, session id = %" PRIu64
,
5191 stream
->chan
->session_id
);
5192 stream
->opened_packet_in_current_trace_chunk
= true;
5194 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
5195 DBG("No space left to open a packet in \"open channel packets\" command: stream id = %" PRIu64
5196 ", channel name = %s, session id = %" PRIu64
,
5199 stream
->chan
->session_id
);
5201 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
5203 * Only unexpected internal errors can lead to this
5204 * failing. Report an unknown error.
5206 ERR("Failed to flush empty buffer in \"open channel packets\" command: stream id = %" PRIu64
5207 ", channel id = %" PRIu64
", channel name = %s"
5208 ", session id = %" PRIu64
,
5212 channel
->session_id
);
5213 ret
= LTTCOMM_CONSUMERD_UNKNOWN_ERROR
;
5220 pthread_mutex_unlock(&stream
->lock
);
5228 pthread_mutex_unlock(&stream
->lock
);
5229 goto end_rcu_unlock
;
5232 void lttng_consumer_sigbus_handle(void *addr
)
5234 lttng_ustconsumer_sigbus_handle(addr
);