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
, num_hup
, 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();
2555 * the fds set has been updated, we need to update our
2556 * local array as well
2558 pthread_mutex_lock(&the_consumer_data
.lock
);
2559 if (the_consumer_data
.need_update
) {
2564 local_stream
= nullptr;
2566 /* Allocate for all fds */
2568 calloc
<struct pollfd
>(the_consumer_data
.stream_count
+ nb_pipes_fd
);
2569 if (pollfd
== nullptr) {
2570 PERROR("pollfd malloc");
2571 pthread_mutex_unlock(&the_consumer_data
.lock
);
2575 local_stream
= calloc
<lttng_consumer_stream
*>(
2576 the_consumer_data
.stream_count
+ nb_pipes_fd
);
2577 if (local_stream
== nullptr) {
2578 PERROR("local_stream malloc");
2579 pthread_mutex_unlock(&the_consumer_data
.lock
);
2582 ret
= update_poll_array(
2583 ctx
, &pollfd
, local_stream
, data_ht
, &nb_inactive_fd
);
2585 ERR("Error in allocating pollfd or local_outfds");
2586 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2587 pthread_mutex_unlock(&the_consumer_data
.lock
);
2591 the_consumer_data
.need_update
= 0;
2593 pthread_mutex_unlock(&the_consumer_data
.lock
);
2595 /* No FDs and consumer_quit, consumer_cleanup the thread */
2596 if (nb_fd
== 0 && nb_inactive_fd
== 0 && CMM_LOAD_SHARED(consumer_quit
) == 1) {
2597 err
= 0; /* All is OK */
2600 /* poll on the array of fds */
2602 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2603 if (testpoint(consumerd_thread_data_poll
)) {
2606 health_poll_entry();
2607 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2609 DBG("poll num_rdy : %d", num_rdy
);
2610 if (num_rdy
== -1) {
2612 * Restart interrupted system call.
2614 if (errno
== EINTR
) {
2617 PERROR("Poll error");
2618 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2620 } else if (num_rdy
== 0) {
2621 DBG("Polling thread timed out");
2625 if (caa_unlikely(data_consumption_paused
)) {
2626 DBG("Data consumption paused, sleeping...");
2632 * If the consumer_data_pipe triggered poll go directly to the
2633 * beginning of the loop to update the array. We want to prioritize
2634 * array update over low-priority reads.
2636 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2637 ssize_t pipe_readlen
;
2639 DBG("consumer_data_pipe wake up");
2640 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2642 sizeof(new_stream
)); /* NOLINT sizeof used on
2644 if (pipe_readlen
< sizeof(new_stream
)) { /* NOLINT sizeof used on a pointer.
2646 PERROR("Consumer data pipe");
2647 /* Continue so we can at least handle the current stream(s). */
2652 * If the stream is NULL, just ignore it. It's also possible that
2653 * the sessiond poll thread changed the consumer_quit state and is
2654 * waking us up to test it.
2656 if (new_stream
== nullptr) {
2657 validate_endpoint_status_data_stream();
2661 /* Continue to update the local streams and handle prio ones */
2665 /* Handle wakeup pipe. */
2666 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2668 ssize_t pipe_readlen
;
2671 lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
, sizeof(dummy
));
2672 if (pipe_readlen
< 0) {
2673 PERROR("Consumer data wakeup pipe");
2675 /* We've been awakened to handle stream(s). */
2676 ctx
->has_wakeup
= 0;
2679 /* Take care of high priority channels first. */
2680 for (i
= 0; i
< nb_fd
; i
++) {
2681 health_code_update();
2683 if (local_stream
[i
] == nullptr) {
2686 if (pollfd
[i
].revents
& POLLPRI
) {
2687 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2689 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2690 /* it's ok to have an unavailable sub-buffer */
2691 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2692 /* Clean the stream and free it. */
2693 consumer_del_stream(local_stream
[i
], data_ht
);
2694 local_stream
[i
] = nullptr;
2695 } else if (len
> 0) {
2696 local_stream
[i
]->has_data_left_to_be_read_before_teardown
=
2703 * If we read high prio channel in this loop, try again
2704 * for more high prio data.
2710 /* Take care of low priority channels. */
2711 for (i
= 0; i
< nb_fd
; i
++) {
2712 health_code_update();
2714 if (local_stream
[i
] == nullptr) {
2717 if ((pollfd
[i
].revents
& POLLIN
) || local_stream
[i
]->hangup_flush_done
||
2718 local_stream
[i
]->has_data
) {
2719 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2720 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2721 /* it's ok to have an unavailable sub-buffer */
2722 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2723 /* Clean the stream and free it. */
2724 consumer_del_stream(local_stream
[i
], data_ht
);
2725 local_stream
[i
] = nullptr;
2726 } else if (len
> 0) {
2727 local_stream
[i
]->has_data_left_to_be_read_before_teardown
=
2733 /* Handle hangup and errors */
2734 for (i
= 0; i
< nb_fd
; i
++) {
2735 health_code_update();
2737 if (local_stream
[i
] == nullptr) {
2740 if (!local_stream
[i
]->hangup_flush_done
&&
2741 (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
)) &&
2742 (the_consumer_data
.type
== LTTNG_CONSUMER32_UST
||
2743 the_consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2744 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2746 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2747 /* Attempt read again, for the data we just flushed. */
2748 local_stream
[i
]->has_data_left_to_be_read_before_teardown
= 1;
2751 * When a stream's pipe dies (hup/err/nval), an "inactive producer" flush is
2752 * performed. This type of flush ensures that a new packet is produced no
2753 * matter the consumed/produced positions are.
2755 * This, in turn, causes the next pass to see that data available for the
2756 * stream. When we come back here, we can be assured that all available
2757 * data has been consumed and we can finally destroy the stream.
2759 * If the poll flag is HUP/ERR/NVAL and we have
2760 * read no data in this pass, we can remove the
2761 * stream from its hash table.
2763 if ((pollfd
[i
].revents
& POLLHUP
)) {
2764 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2765 if (!local_stream
[i
]->has_data_left_to_be_read_before_teardown
) {
2766 consumer_del_stream(local_stream
[i
], data_ht
);
2767 local_stream
[i
] = nullptr;
2770 } else if (pollfd
[i
].revents
& POLLERR
) {
2771 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2772 if (!local_stream
[i
]->has_data_left_to_be_read_before_teardown
) {
2773 consumer_del_stream(local_stream
[i
], data_ht
);
2774 local_stream
[i
] = nullptr;
2777 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2778 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2779 if (!local_stream
[i
]->has_data_left_to_be_read_before_teardown
) {
2780 consumer_del_stream(local_stream
[i
], data_ht
);
2781 local_stream
[i
] = nullptr;
2785 if (local_stream
[i
] != nullptr) {
2786 local_stream
[i
]->has_data_left_to_be_read_before_teardown
= 0;
2793 DBG("polling thread exiting");
2798 * Close the write side of the pipe so epoll_wait() in
2799 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2800 * read side of the pipe. If we close them both, epoll_wait strangely does
2801 * not return and could create a endless wait period if the pipe is the
2802 * only tracked fd in the poll set. The thread will take care of closing
2805 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2810 ERR("Health error occurred in %s", __func__
);
2812 health_unregister(health_consumerd
);
2814 rcu_unregister_thread();
2819 * Close wake-up end of each stream belonging to the channel. This will
2820 * allow the poll() on the stream read-side to detect when the
2821 * write-side (application) finally closes them.
2823 static void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2825 struct lttng_ht
*ht
;
2826 struct lttng_consumer_stream
*stream
;
2827 struct lttng_ht_iter iter
;
2829 ht
= the_consumer_data
.stream_per_chan_id_ht
;
2831 lttng::urcu::read_lock_guard read_lock
;
2832 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2833 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2838 node_channel_id
.node
)
2841 * Protect against teardown with mutex.
2843 pthread_mutex_lock(&stream
->lock
);
2844 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2847 switch (the_consumer_data
.type
) {
2848 case LTTNG_CONSUMER_KERNEL
:
2850 case LTTNG_CONSUMER32_UST
:
2851 case LTTNG_CONSUMER64_UST
:
2852 if (stream
->metadata_flag
) {
2853 /* Safe and protected by the stream lock. */
2854 lttng_ustconsumer_close_metadata(stream
->chan
);
2857 * Note: a mutex is taken internally within
2858 * liblttng-ust-ctl to protect timer wakeup_fd
2859 * use from concurrent close.
2861 lttng_ustconsumer_close_stream_wakeup(stream
);
2865 ERR("Unknown consumer_data type");
2869 pthread_mutex_unlock(&stream
->lock
);
2873 static void destroy_channel_ht(struct lttng_ht
*ht
)
2875 struct lttng_ht_iter iter
;
2876 struct lttng_consumer_channel
*channel
;
2879 if (ht
== nullptr) {
2884 lttng::urcu::read_lock_guard read_lock
;
2886 cds_lfht_for_each_entry (ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2887 ret
= lttng_ht_del(ht
, &iter
);
2888 LTTNG_ASSERT(ret
!= 0);
2892 lttng_ht_destroy(ht
);
2896 * This thread polls the channel fds to detect when they are being
2897 * closed. It closes all related streams if the channel is detected as
2898 * closed. It is currently only used as a shim layer for UST because the
2899 * consumerd needs to keep the per-stream wakeup end of pipes open for
2902 void *consumer_thread_channel_poll(void *data
)
2904 int ret
, i
, pollfd
, err
= -1;
2905 uint32_t revents
, nb_fd
;
2906 struct lttng_consumer_channel
*chan
= nullptr;
2907 struct lttng_ht_iter iter
;
2908 struct lttng_ht_node_u64
*node
;
2909 struct lttng_poll_event events
;
2910 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
2911 struct lttng_ht
*channel_ht
;
2913 rcu_register_thread();
2915 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2917 if (testpoint(consumerd_thread_channel
)) {
2918 goto error_testpoint
;
2921 health_code_update();
2923 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2925 /* ENOMEM at this point. Better to bail out. */
2929 DBG("Thread channel poll started");
2931 /* Size is set to 1 for the consumer_channel pipe */
2932 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2934 ERR("Poll set creation failed");
2938 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2944 DBG("Channel main loop started");
2948 health_code_update();
2949 DBG("Channel poll wait");
2950 health_poll_entry();
2951 ret
= lttng_poll_wait(&events
, -1);
2952 DBG("Channel poll return from wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2954 DBG("Channel event caught in thread");
2956 if (errno
== EINTR
) {
2957 ERR("Poll EINTR caught");
2960 if (LTTNG_POLL_GETNB(&events
) == 0) {
2961 err
= 0; /* All is OK */
2968 /* From here, the event is a channel wait fd */
2969 for (i
= 0; i
< nb_fd
; i
++) {
2970 health_code_update();
2972 revents
= LTTNG_POLL_GETEV(&events
, i
);
2973 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2975 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2976 if (revents
& LPOLLIN
) {
2977 enum consumer_channel_action action
;
2980 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2983 ERR("Error reading channel pipe");
2985 lttng_poll_del(&events
,
2986 ctx
->consumer_channel_pipe
[0]);
2991 case CONSUMER_CHANNEL_ADD
:
2993 DBG("Adding channel %d to poll set", chan
->wait_fd
);
2995 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2997 lttng::urcu::read_lock_guard read_lock
;
2998 lttng_ht_add_unique_u64(channel_ht
,
2999 &chan
->wait_fd_node
);
3000 /* Add channel to the global poll events list */
3001 // FIXME: Empty flag on a pipe pollset, this might
3003 lttng_poll_add(&events
, chan
->wait_fd
, 0);
3006 case CONSUMER_CHANNEL_DEL
:
3009 * This command should never be called if the
3010 * channel has streams monitored by either the data
3011 * or metadata thread. The consumer only notify this
3012 * thread with a channel del. command if it receives
3013 * a destroy channel command from the session daemon
3014 * that send it if a command prior to the
3015 * GET_CHANNEL failed.
3018 lttng::urcu::read_lock_guard read_lock
;
3019 chan
= consumer_find_channel(key
);
3021 ERR("UST consumer get channel key %" PRIu64
3022 " not found for del channel",
3026 lttng_poll_del(&events
, chan
->wait_fd
);
3027 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
3028 ret
= lttng_ht_del(channel_ht
, &iter
);
3029 LTTNG_ASSERT(ret
== 0);
3031 switch (the_consumer_data
.type
) {
3032 case LTTNG_CONSUMER_KERNEL
:
3034 case LTTNG_CONSUMER32_UST
:
3035 case LTTNG_CONSUMER64_UST
:
3036 health_code_update();
3037 /* Destroy streams that might have been left
3038 * in the stream list. */
3039 clean_channel_stream_list(chan
);
3042 ERR("Unknown consumer_data type");
3047 * Release our own refcount. Force channel deletion
3048 * even if streams were not initialized.
3050 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3051 consumer_del_channel(chan
);
3055 case CONSUMER_CHANNEL_QUIT
:
3057 * Remove the pipe from the poll set and continue
3058 * the loop since their might be data to consume.
3060 lttng_poll_del(&events
,
3061 ctx
->consumer_channel_pipe
[0]);
3064 ERR("Unknown action");
3067 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3068 DBG("Channel thread pipe hung up");
3070 * Remove the pipe from the poll set and continue the loop
3071 * since their might be data to consume.
3073 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3076 ERR("Unexpected poll events %u for sock %d",
3082 /* Handle other stream */
3086 lttng::urcu::read_lock_guard read_lock
;
3088 uint64_t tmp_id
= (uint64_t) pollfd
;
3090 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3092 node
= lttng_ht_iter_get_node_u64(&iter
);
3095 chan
= caa_container_of(node
, struct lttng_consumer_channel
, wait_fd_node
);
3097 /* Check for error event */
3098 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3099 DBG("Channel fd %d is hup|err.", pollfd
);
3101 lttng_poll_del(&events
, chan
->wait_fd
);
3102 ret
= lttng_ht_del(channel_ht
, &iter
);
3103 LTTNG_ASSERT(ret
== 0);
3106 * This will close the wait fd for each stream associated to
3107 * this channel AND monitored by the data/metadata thread thus
3108 * will be clean by the right thread.
3110 consumer_close_channel_streams(chan
);
3112 /* Release our own refcount */
3113 if (!uatomic_sub_return(&chan
->refcount
, 1) &&
3114 !uatomic_read(&chan
->nb_init_stream_left
)) {
3115 consumer_del_channel(chan
);
3118 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3122 /* Release RCU lock for the channel looked up */
3129 lttng_poll_clean(&events
);
3131 destroy_channel_ht(channel_ht
);
3134 DBG("Channel poll thread exiting");
3137 ERR("Health error occurred in %s", __func__
);
3139 health_unregister(health_consumerd
);
3140 rcu_unregister_thread();
3144 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3145 struct pollfd
*sockpoll
,
3151 LTTNG_ASSERT(sockpoll
);
3153 ret
= lttng_consumer_poll_socket(sockpoll
);
3157 DBG("Metadata connection on client_socket");
3159 /* Blocking call, waiting for transmission */
3160 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3161 if (ctx
->consumer_metadata_socket
< 0) {
3162 WARN("On accept metadata");
3173 * This thread listens on the consumerd socket and receives the file
3174 * descriptors from the session daemon.
3176 void *consumer_thread_sessiond_poll(void *data
)
3178 int sock
= -1, client_socket
, ret
, err
= -1;
3180 * structure to poll for incoming data on communication socket avoids
3181 * making blocking sockets.
3183 struct pollfd consumer_sockpoll
[2];
3184 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
3186 rcu_register_thread();
3188 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3190 if (testpoint(consumerd_thread_sessiond
)) {
3191 goto error_testpoint
;
3194 health_code_update();
3196 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3197 unlink(ctx
->consumer_command_sock_path
);
3198 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3199 if (client_socket
< 0) {
3200 ERR("Cannot create command socket");
3204 ret
= lttcomm_listen_unix_sock(client_socket
);
3209 DBG("Sending ready command to lttng-sessiond");
3210 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3211 /* return < 0 on error, but == 0 is not fatal */
3213 ERR("Error sending ready command to lttng-sessiond");
3217 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3218 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3219 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3220 consumer_sockpoll
[1].fd
= client_socket
;
3221 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3223 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3231 DBG("Connection on client_socket");
3233 /* Blocking call, waiting for transmission */
3234 sock
= lttcomm_accept_unix_sock(client_socket
);
3241 * Setup metadata socket which is the second socket connection on the
3242 * command unix socket.
3244 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3253 /* This socket is not useful anymore. */
3254 ret
= close(client_socket
);
3256 PERROR("close client_socket");
3260 /* update the polling structure to poll on the established socket */
3261 consumer_sockpoll
[1].fd
= sock
;
3262 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3265 health_code_update();
3267 health_poll_entry();
3268 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3277 DBG("Incoming command on sock");
3278 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3281 * This could simply be a session daemon quitting. Don't output
3284 DBG("Communication interrupted on command socket");
3288 if (CMM_LOAD_SHARED(consumer_quit
)) {
3289 DBG("consumer_thread_receive_fds received quit from signal");
3290 err
= 0; /* All is OK */
3293 DBG("Received command on sock");
3299 DBG("Consumer thread sessiond poll exiting");
3302 * Close metadata streams since the producer is the session daemon which
3305 * NOTE: for now, this only applies to the UST tracer.
3307 lttng_consumer_close_all_metadata();
3310 * when all fds have hung up, the polling thread
3313 CMM_STORE_SHARED(consumer_quit
, 1);
3316 * Notify the data poll thread to poll back again and test the
3317 * consumer_quit state that we just set so to quit gracefully.
3319 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3321 notify_channel_pipe(ctx
, nullptr, -1, CONSUMER_CHANNEL_QUIT
);
3323 notify_health_quit_pipe(health_quit_pipe
);
3325 /* Cleaning up possibly open sockets. */
3329 PERROR("close sock sessiond poll");
3332 if (client_socket
>= 0) {
3333 ret
= close(client_socket
);
3335 PERROR("close client_socket sessiond poll");
3342 ERR("Health error occurred in %s", __func__
);
3344 health_unregister(health_consumerd
);
3346 rcu_unregister_thread();
3350 static int post_consume(struct lttng_consumer_stream
*stream
,
3351 const struct stream_subbuffer
*subbuffer
,
3352 struct lttng_consumer_local_data
*ctx
)
3356 const size_t count
=
3357 lttng_dynamic_array_get_count(&stream
->read_subbuffer_ops
.post_consume_cbs
);
3359 for (i
= 0; i
< count
; i
++) {
3360 const post_consume_cb op
= *(post_consume_cb
*) lttng_dynamic_array_get_element(
3361 &stream
->read_subbuffer_ops
.post_consume_cbs
, i
);
3363 ret
= op(stream
, subbuffer
, ctx
);
3372 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3373 struct lttng_consumer_local_data
*ctx
,
3374 bool locked_by_caller
)
3376 ssize_t ret
, written_bytes
= 0;
3378 struct stream_subbuffer subbuffer
= {};
3379 enum get_next_subbuffer_status get_next_status
;
3381 if (!locked_by_caller
) {
3382 stream
->read_subbuffer_ops
.lock(stream
);
3384 stream
->read_subbuffer_ops
.assert_locked(stream
);
3387 if (stream
->read_subbuffer_ops
.on_wake_up
) {
3388 ret
= stream
->read_subbuffer_ops
.on_wake_up(stream
);
3395 * If the stream was flagged to be ready for rotation before we extract
3396 * the next packet, rotate it now.
3398 if (stream
->rotate_ready
) {
3399 DBG("Rotate stream before consuming data");
3400 ret
= lttng_consumer_rotate_stream(stream
);
3402 ERR("Stream rotation error before consuming data");
3407 get_next_status
= stream
->read_subbuffer_ops
.get_next_subbuffer(stream
, &subbuffer
);
3408 switch (get_next_status
) {
3409 case GET_NEXT_SUBBUFFER_STATUS_OK
:
3411 case GET_NEXT_SUBBUFFER_STATUS_NO_DATA
:
3415 case GET_NEXT_SUBBUFFER_STATUS_ERROR
:
3422 ret
= stream
->read_subbuffer_ops
.pre_consume_subbuffer(stream
, &subbuffer
);
3424 goto error_put_subbuf
;
3427 written_bytes
= stream
->read_subbuffer_ops
.consume_subbuffer(ctx
, stream
, &subbuffer
);
3428 if (written_bytes
<= 0) {
3429 ERR("Error consuming subbuffer: (%zd)", written_bytes
);
3430 ret
= (int) written_bytes
;
3431 goto error_put_subbuf
;
3434 ret
= stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3439 ret
= post_consume(stream
, &subbuffer
, ctx
);
3445 * After extracting the packet, we check if the stream is now ready to
3446 * be rotated and perform the action immediately.
3448 * Don't overwrite `ret` as callers expect the number of bytes
3449 * consumed to be returned on success.
3451 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
3452 if (rotation_ret
== 1) {
3453 rotation_ret
= lttng_consumer_rotate_stream(stream
);
3454 if (rotation_ret
< 0) {
3456 ERR("Stream rotation error after consuming data");
3460 } else if (rotation_ret
< 0) {
3462 ERR("Failed to check if stream was ready to rotate after consuming data");
3467 if (stream
->read_subbuffer_ops
.on_sleep
) {
3468 stream
->read_subbuffer_ops
.on_sleep(stream
, ctx
);
3471 ret
= written_bytes
;
3473 if (!locked_by_caller
) {
3474 stream
->read_subbuffer_ops
.unlock(stream
);
3479 (void) stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3483 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3485 switch (the_consumer_data
.type
) {
3486 case LTTNG_CONSUMER_KERNEL
:
3487 return lttng_kconsumer_on_recv_stream(stream
);
3488 case LTTNG_CONSUMER32_UST
:
3489 case LTTNG_CONSUMER64_UST
:
3490 return lttng_ustconsumer_on_recv_stream(stream
);
3492 ERR("Unknown consumer_data type");
3499 * Allocate and set consumer data hash tables.
3501 int lttng_consumer_init()
3503 the_consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3504 if (!the_consumer_data
.channel_ht
) {
3508 the_consumer_data
.channels_by_session_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3509 if (!the_consumer_data
.channels_by_session_id_ht
) {
3513 the_consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3514 if (!the_consumer_data
.relayd_ht
) {
3518 the_consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3519 if (!the_consumer_data
.stream_list_ht
) {
3523 the_consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3524 if (!the_consumer_data
.stream_per_chan_id_ht
) {
3528 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3533 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3538 the_consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3539 if (!the_consumer_data
.chunk_registry
) {
3550 * Process the ADD_RELAYD command receive by a consumer.
3552 * This will create a relayd socket pair and add it to the relayd hash table.
3553 * The caller MUST acquire a RCU read side lock before calling it.
3555 void consumer_add_relayd_socket(uint64_t net_seq_idx
,
3557 struct lttng_consumer_local_data
*ctx
,
3559 struct pollfd
*consumer_sockpoll
,
3560 uint64_t sessiond_id
,
3561 uint64_t relayd_session_id
,
3562 uint32_t relayd_version_major
,
3563 uint32_t relayd_version_minor
,
3564 enum lttcomm_sock_proto relayd_socket_protocol
)
3566 int fd
= -1, ret
= -1, relayd_created
= 0;
3567 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3568 struct consumer_relayd_sock_pair
*relayd
= nullptr;
3571 LTTNG_ASSERT(sock
>= 0);
3572 ASSERT_RCU_READ_LOCKED();
3574 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3576 /* Get relayd reference if exists. */
3577 relayd
= consumer_find_relayd(net_seq_idx
);
3578 if (relayd
== nullptr) {
3579 LTTNG_ASSERT(sock_type
== LTTNG_STREAM_CONTROL
);
3580 /* Not found. Allocate one. */
3581 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3582 if (relayd
== nullptr) {
3583 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3586 relayd
->sessiond_session_id
= sessiond_id
;
3591 * This code path MUST continue to the consumer send status message to
3592 * we can notify the session daemon and continue our work without
3593 * killing everything.
3597 * relayd key should never be found for control socket.
3599 LTTNG_ASSERT(sock_type
!= LTTNG_STREAM_CONTROL
);
3602 /* First send a status message before receiving the fds. */
3603 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3605 /* Somehow, the session daemon is not responding anymore. */
3606 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3607 goto error_nosignal
;
3610 /* Poll on consumer socket. */
3611 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3613 /* Needing to exit in the middle of a command: error. */
3614 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3615 goto error_nosignal
;
3618 /* Get relayd socket from session daemon */
3619 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3620 if (ret
!= sizeof(fd
)) {
3621 fd
= -1; /* Just in case it gets set with an invalid value. */
3624 * Failing to receive FDs might indicate a major problem such as
3625 * reaching a fd limit during the receive where the kernel returns a
3626 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3627 * don't take any chances and stop everything.
3629 * XXX: Feature request #558 will fix that and avoid this possible
3630 * issue when reaching the fd limit.
3632 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3633 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3637 /* Copy socket information and received FD */
3638 switch (sock_type
) {
3639 case LTTNG_STREAM_CONTROL
:
3640 /* Copy received lttcomm socket */
3641 ret
= lttcomm_populate_sock_from_open_socket(
3642 &relayd
->control_sock
.sock
, fd
, relayd_socket_protocol
);
3644 /* Assign version values. */
3645 relayd
->control_sock
.major
= relayd_version_major
;
3646 relayd
->control_sock
.minor
= relayd_version_minor
;
3648 relayd
->relayd_session_id
= relayd_session_id
;
3651 case LTTNG_STREAM_DATA
:
3652 /* Copy received lttcomm socket */
3653 ret
= lttcomm_populate_sock_from_open_socket(
3654 &relayd
->data_sock
.sock
, fd
, relayd_socket_protocol
);
3655 /* Assign version values. */
3656 relayd
->data_sock
.major
= relayd_version_major
;
3657 relayd
->data_sock
.minor
= relayd_version_minor
;
3660 ERR("Unknown relayd socket type (%d)", sock_type
);
3661 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3666 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3670 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3671 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3672 relayd
->net_seq_idx
,
3675 * We gave the ownership of the fd to the relayd structure. Set the
3676 * fd to -1 so we don't call close() on it in the error path below.
3680 /* We successfully added the socket. Send status back. */
3681 ret
= consumer_send_status_msg(sock
, ret_code
);
3683 /* Somehow, the session daemon is not responding anymore. */
3684 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3685 goto error_nosignal
;
3689 * Add relayd socket pair to consumer data hashtable. If object already
3690 * exists or on error, the function gracefully returns.
3699 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3700 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3704 /* Close received socket if valid. */
3707 PERROR("close received socket");
3711 if (relayd_created
) {
3717 * Search for a relayd associated to the session id and return the reference.
3719 * A rcu read side lock MUST be acquire before calling this function and locked
3720 * until the relayd object is no longer necessary.
3722 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3724 struct lttng_ht_iter iter
;
3725 struct consumer_relayd_sock_pair
*relayd
= nullptr;
3727 ASSERT_RCU_READ_LOCKED();
3729 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3730 cds_lfht_for_each_entry (the_consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
, node
.node
) {
3732 * Check by sessiond id which is unique here where the relayd session
3733 * id might not be when having multiple relayd.
3735 if (relayd
->sessiond_session_id
== id
) {
3736 /* Found the relayd. There can be only one per id. */
3748 * Check if for a given session id there is still data needed to be extract
3751 * Return 1 if data is pending or else 0 meaning ready to be read.
3753 int consumer_data_pending(uint64_t id
)
3756 struct lttng_ht_iter iter
;
3757 struct lttng_ht
*ht
;
3758 struct lttng_consumer_stream
*stream
;
3759 struct consumer_relayd_sock_pair
*relayd
= nullptr;
3760 int (*data_pending
)(struct lttng_consumer_stream
*);
3762 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3764 lttng::urcu::read_lock_guard read_lock
;
3765 pthread_mutex_lock(&the_consumer_data
.lock
);
3767 switch (the_consumer_data
.type
) {
3768 case LTTNG_CONSUMER_KERNEL
:
3769 data_pending
= lttng_kconsumer_data_pending
;
3771 case LTTNG_CONSUMER32_UST
:
3772 case LTTNG_CONSUMER64_UST
:
3773 data_pending
= lttng_ustconsumer_data_pending
;
3776 ERR("Unknown consumer data type");
3780 /* Ease our life a bit */
3781 ht
= the_consumer_data
.stream_list_ht
;
3783 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3784 ht
->hash_fct(&id
, lttng_ht_seed
),
3789 node_session_id
.node
)
3791 pthread_mutex_lock(&stream
->lock
);
3794 * A removed node from the hash table indicates that the stream has
3795 * been deleted thus having a guarantee that the buffers are closed
3796 * on the consumer side. However, data can still be transmitted
3797 * over the network so don't skip the relayd check.
3799 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3801 /* Check the stream if there is data in the buffers. */
3802 ret
= data_pending(stream
);
3804 pthread_mutex_unlock(&stream
->lock
);
3809 pthread_mutex_unlock(&stream
->lock
);
3812 relayd
= find_relayd_by_session_id(id
);
3814 unsigned int is_data_inflight
= 0;
3816 /* Send init command for data pending. */
3817 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3818 ret
= relayd_begin_data_pending(&relayd
->control_sock
, relayd
->relayd_session_id
);
3820 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3821 /* Communication error thus the relayd so no data pending. */
3822 goto data_not_pending
;
3825 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3826 ht
->hash_fct(&id
, lttng_ht_seed
),
3831 node_session_id
.node
)
3833 if (stream
->metadata_flag
) {
3834 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3835 stream
->relayd_stream_id
);
3837 ret
= relayd_data_pending(&relayd
->control_sock
,
3838 stream
->relayd_stream_id
,
3839 stream
->next_net_seq_num
- 1);
3843 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3845 } else if (ret
< 0) {
3846 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".",
3847 relayd
->net_seq_idx
);
3848 lttng_consumer_cleanup_relayd(relayd
);
3849 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3850 goto data_not_pending
;
3854 /* Send end command for data pending. */
3855 ret
= relayd_end_data_pending(
3856 &relayd
->control_sock
, relayd
->relayd_session_id
, &is_data_inflight
);
3857 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3859 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".",
3860 relayd
->net_seq_idx
);
3861 lttng_consumer_cleanup_relayd(relayd
);
3862 goto data_not_pending
;
3864 if (is_data_inflight
) {
3870 * Finding _no_ node in the hash table and no inflight data means that the
3871 * stream(s) have been removed thus data is guaranteed to be available for
3872 * analysis from the trace files.
3876 /* Data is available to be read by a viewer. */
3877 pthread_mutex_unlock(&the_consumer_data
.lock
);
3881 /* Data is still being extracted from buffers. */
3882 pthread_mutex_unlock(&the_consumer_data
.lock
);
3887 * Send a ret code status message to the sessiond daemon.
3889 * Return the sendmsg() return value.
3891 int consumer_send_status_msg(int sock
, int ret_code
)
3893 struct lttcomm_consumer_status_msg msg
;
3895 memset(&msg
, 0, sizeof(msg
));
3896 msg
.ret_code
= (lttcomm_return_code
) ret_code
;
3898 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3902 * Send a channel status message to the sessiond daemon.
3904 * Return the sendmsg() return value.
3906 int consumer_send_status_channel(int sock
, struct lttng_consumer_channel
*channel
)
3908 struct lttcomm_consumer_status_channel msg
;
3910 LTTNG_ASSERT(sock
>= 0);
3912 memset(&msg
, 0, sizeof(msg
));
3914 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3916 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3917 msg
.key
= channel
->key
;
3918 msg
.stream_count
= channel
->streams
.count
;
3921 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3924 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3925 unsigned long produced_pos
,
3926 uint64_t nb_packets_per_stream
,
3927 uint64_t max_sb_size
)
3929 unsigned long start_pos
;
3931 if (!nb_packets_per_stream
) {
3932 return consumed_pos
; /* Grab everything */
3934 start_pos
= produced_pos
- lttng_offset_align_floor(produced_pos
, max_sb_size
);
3935 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3936 if ((long) (start_pos
- consumed_pos
) < 0) {
3937 return consumed_pos
; /* Grab everything */
3942 /* Stream lock must be held by the caller. */
3943 static int sample_stream_positions(struct lttng_consumer_stream
*stream
,
3944 unsigned long *produced
,
3945 unsigned long *consumed
)
3949 ASSERT_LOCKED(stream
->lock
);
3951 ret
= lttng_consumer_sample_snapshot_positions(stream
);
3953 ERR("Failed to sample snapshot positions");
3957 ret
= lttng_consumer_get_produced_snapshot(stream
, produced
);
3959 ERR("Failed to sample produced position");
3963 ret
= lttng_consumer_get_consumed_snapshot(stream
, consumed
);
3965 ERR("Failed to sample consumed position");
3974 * Sample the rotate position for all the streams of a channel. If a stream
3975 * is already at the rotate position (produced == consumed), we flag it as
3976 * ready for rotation. The rotation of ready streams occurs after we have
3977 * replied to the session daemon that we have finished sampling the positions.
3978 * Must be called with RCU read-side lock held to ensure existence of channel.
3980 * Returns 0 on success, < 0 on error
3982 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
3987 struct lttng_consumer_stream
*stream
;
3988 struct lttng_ht_iter iter
;
3989 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
3990 struct lttng_dynamic_array stream_rotation_positions
;
3991 uint64_t next_chunk_id
, stream_count
= 0;
3992 enum lttng_trace_chunk_status chunk_status
;
3993 const bool is_local_trace
= relayd_id
== -1ULL;
3994 struct consumer_relayd_sock_pair
*relayd
= nullptr;
3995 bool rotating_to_new_chunk
= true;
3996 /* Array of `struct lttng_consumer_stream *` */
3997 struct lttng_dynamic_pointer_array streams_packet_to_open
;
4000 ASSERT_RCU_READ_LOCKED();
4002 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
4004 lttng_dynamic_array_init(&stream_rotation_positions
,
4005 sizeof(struct relayd_stream_rotation_position
),
4007 lttng_dynamic_pointer_array_init(&streams_packet_to_open
, nullptr);
4009 lttng::urcu::read_lock_guard read_lock
;
4011 pthread_mutex_lock(&channel
->lock
);
4012 LTTNG_ASSERT(channel
->trace_chunk
);
4013 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
, &next_chunk_id
);
4014 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4016 goto end_unlock_channel
;
4019 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4020 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4025 node_channel_id
.node
)
4027 unsigned long produced_pos
= 0, consumed_pos
= 0;
4029 health_code_update();
4032 * Lock stream because we are about to change its state.
4034 pthread_mutex_lock(&stream
->lock
);
4036 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
4037 rotating_to_new_chunk
= false;
4041 * Do not flush a packet when rotating from a NULL trace
4042 * chunk. The stream has no means to output data, and the prior
4043 * rotation which rotated to NULL performed that side-effect
4044 * already. No new data can be produced when a stream has no
4045 * associated trace chunk (e.g. a stop followed by a rotate).
4047 if (stream
->trace_chunk
) {
4050 if (stream
->metadata_flag
) {
4052 * Don't produce an empty metadata packet,
4053 * simply close the current one.
4055 * Metadata is regenerated on every trace chunk
4056 * switch; there is no concern that no data was
4059 flush_active
= true;
4062 * Only flush an empty packet if the "packet
4063 * open" could not be performed on transition
4064 * to a new trace chunk and no packets were
4065 * consumed within the chunk's lifetime.
4067 if (stream
->opened_packet_in_current_trace_chunk
) {
4068 flush_active
= true;
4071 * Stream could have been full at the
4072 * time of rotation, but then have had
4073 * no activity at all.
4075 * It is important to flush a packet
4076 * to prevent 0-length files from being
4077 * produced as most viewers choke on
4080 * Unfortunately viewers will not be
4081 * able to know that tracing was active
4082 * for this stream during this trace
4085 ret
= sample_stream_positions(
4086 stream
, &produced_pos
, &consumed_pos
);
4088 goto end_unlock_stream
;
4092 * Don't flush an empty packet if data
4093 * was produced; it will be consumed
4094 * before the rotation completes.
4096 flush_active
= produced_pos
!= consumed_pos
;
4097 if (!flush_active
) {
4098 const char *trace_chunk_name
;
4099 uint64_t trace_chunk_id
;
4101 chunk_status
= lttng_trace_chunk_get_name(
4102 stream
->trace_chunk
,
4105 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NONE
) {
4106 trace_chunk_name
= "none";
4110 * Consumer trace chunks are
4113 chunk_status
= lttng_trace_chunk_get_id(
4114 stream
->trace_chunk
, &trace_chunk_id
);
4115 LTTNG_ASSERT(chunk_status
==
4116 LTTNG_TRACE_CHUNK_STATUS_OK
);
4118 DBG("Unable to open packet for stream during trace chunk's lifetime. "
4119 "Flushing an empty packet to prevent an empty file from being created: "
4120 "stream id = %" PRIu64
4121 ", trace chunk name = `%s`, trace chunk id = %" PRIu64
,
4130 * Close the current packet before sampling the
4131 * ring buffer positions.
4133 ret
= consumer_stream_flush_buffer(stream
, flush_active
);
4135 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4137 goto end_unlock_stream
;
4141 ret
= lttng_consumer_take_snapshot(stream
);
4142 if (ret
< 0 && ret
!= -ENODATA
&& ret
!= -EAGAIN
) {
4143 ERR("Failed to sample snapshot position during channel rotation");
4144 goto end_unlock_stream
;
4147 ret
= lttng_consumer_get_produced_snapshot(stream
, &produced_pos
);
4149 ERR("Failed to sample produced position during channel rotation");
4150 goto end_unlock_stream
;
4153 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos
);
4155 ERR("Failed to sample consumed position during channel rotation");
4156 goto end_unlock_stream
;
4160 * Align produced position on the start-of-packet boundary of the first
4161 * packet going into the next trace chunk.
4163 produced_pos
= lttng_align_floor(produced_pos
, stream
->max_sb_size
);
4164 if (consumed_pos
== produced_pos
) {
4165 DBG("Set rotate ready for stream %" PRIu64
" produced = %lu consumed = %lu",
4169 stream
->rotate_ready
= true;
4171 DBG("Different consumed and produced positions "
4172 "for stream %" PRIu64
" produced = %lu consumed = %lu",
4178 * The rotation position is based on the packet_seq_num of the
4179 * packet following the last packet that was consumed for this
4180 * stream, incremented by the offset between produced and
4181 * consumed positions. This rotation position is a lower bound
4182 * (inclusive) at which the next trace chunk starts. Since it
4183 * is a lower bound, it is OK if the packet_seq_num does not
4184 * correspond exactly to the same packet identified by the
4185 * consumed_pos, which can happen in overwrite mode.
4187 if (stream
->sequence_number_unavailable
) {
4189 * Rotation should never be performed on a session which
4190 * interacts with a pre-2.8 lttng-modules, which does
4191 * not implement packet sequence number.
4193 ERR("Failure to rotate stream %" PRIu64
": sequence number unavailable",
4196 goto end_unlock_stream
;
4198 stream
->rotate_position
= stream
->last_sequence_number
+ 1 +
4199 ((produced_pos
- consumed_pos
) / stream
->max_sb_size
);
4200 DBG("Set rotation position for stream %" PRIu64
" at position %" PRIu64
,
4202 stream
->rotate_position
);
4204 if (!is_local_trace
) {
4206 * The relay daemon control protocol expects a rotation
4207 * position as "the sequence number of the first packet
4208 * _after_ the current trace chunk".
4210 const struct relayd_stream_rotation_position position
= {
4211 .stream_id
= stream
->relayd_stream_id
,
4212 .rotate_at_seq_num
= stream
->rotate_position
,
4215 ret
= lttng_dynamic_array_add_element(&stream_rotation_positions
,
4218 ERR("Failed to allocate stream rotation position");
4219 goto end_unlock_stream
;
4224 stream
->opened_packet_in_current_trace_chunk
= false;
4226 if (rotating_to_new_chunk
&& !stream
->metadata_flag
) {
4228 * Attempt to flush an empty packet as close to the
4229 * rotation point as possible. In the event where a
4230 * stream remains inactive after the rotation point,
4231 * this ensures that the new trace chunk has a
4232 * beginning timestamp set at the begining of the
4233 * trace chunk instead of only creating an empty
4234 * packet when the trace chunk is stopped.
4236 * This indicates to the viewers that the stream
4237 * was being recorded, but more importantly it
4238 * allows viewers to determine a useable trace
4241 * This presents a problem in the case where the
4242 * ring-buffer is completely full.
4244 * Consider the following scenario:
4245 * - The consumption of data is slow (slow network,
4247 * - The ring buffer is full,
4248 * - A rotation is initiated,
4249 * - The flush below does nothing (no space left to
4250 * open a new packet),
4251 * - The other streams rotate very soon, and new
4252 * data is produced in the new chunk,
4253 * - This stream completes its rotation long after the
4254 * rotation was initiated
4255 * - The session is stopped before any event can be
4256 * produced in this stream's buffers.
4258 * The resulting trace chunk will have a single packet
4259 * temporaly at the end of the trace chunk for this
4260 * stream making the stream intersection more narrow
4261 * than it should be.
4263 * To work-around this, an empty flush is performed
4264 * after the first consumption of a packet during a
4265 * rotation if open_packet fails. The idea is that
4266 * consuming a packet frees enough space to switch
4267 * packets in this scenario and allows the tracer to
4268 * "stamp" the beginning of the new trace chunk at the
4269 * earliest possible point.
4271 * The packet open is performed after the channel
4272 * rotation to ensure that no attempt to open a packet
4273 * is performed in a stream that has no active trace
4276 ret
= lttng_dynamic_pointer_array_add_pointer(&streams_packet_to_open
,
4279 PERROR("Failed to add a stream pointer to array of streams in which to open a packet");
4281 goto end_unlock_stream
;
4285 pthread_mutex_unlock(&stream
->lock
);
4289 if (!is_local_trace
) {
4290 relayd
= consumer_find_relayd(relayd_id
);
4292 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4294 goto end_unlock_channel
;
4297 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4298 ret
= relayd_rotate_streams(&relayd
->control_sock
,
4300 rotating_to_new_chunk
? &next_chunk_id
: nullptr,
4301 (const struct relayd_stream_rotation_position
*)
4302 stream_rotation_positions
.buffer
.data
);
4303 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4305 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4306 relayd
->net_seq_idx
);
4307 lttng_consumer_cleanup_relayd(relayd
);
4308 goto end_unlock_channel
;
4312 for (stream_idx
= 0;
4313 stream_idx
< lttng_dynamic_pointer_array_get_count(&streams_packet_to_open
);
4315 enum consumer_stream_open_packet_status status
;
4317 stream
= (lttng_consumer_stream
*) lttng_dynamic_pointer_array_get_pointer(
4318 &streams_packet_to_open
, stream_idx
);
4320 pthread_mutex_lock(&stream
->lock
);
4321 status
= consumer_stream_open_packet(stream
);
4322 pthread_mutex_unlock(&stream
->lock
);
4324 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
4325 DBG("Opened a packet after a rotation: stream id = %" PRIu64
4326 ", channel name = %s, session id = %" PRIu64
,
4329 stream
->chan
->session_id
);
4331 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
4333 * Can't open a packet as there is no space left
4334 * in the buffer. A new packet will be opened
4335 * once one has been consumed.
4337 DBG("No space left to open a packet after a rotation: stream id = %" PRIu64
4338 ", channel name = %s, session id = %" PRIu64
,
4341 stream
->chan
->session_id
);
4343 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
4344 /* Logged by callee. */
4346 goto end_unlock_channel
;
4352 pthread_mutex_unlock(&channel
->lock
);
4357 pthread_mutex_unlock(&stream
->lock
);
4359 pthread_mutex_unlock(&channel
->lock
);
4361 lttng_dynamic_array_reset(&stream_rotation_positions
);
4362 lttng_dynamic_pointer_array_reset(&streams_packet_to_open
);
4366 static int consumer_clear_buffer(struct lttng_consumer_stream
*stream
)
4369 unsigned long consumed_pos_before
, consumed_pos_after
;
4371 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4373 ERR("Taking snapshot positions");
4377 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_before
);
4379 ERR("Consumed snapshot position");
4383 switch (the_consumer_data
.type
) {
4384 case LTTNG_CONSUMER_KERNEL
:
4385 ret
= kernctl_buffer_clear(stream
->wait_fd
);
4387 ERR("Failed to clear kernel stream (ret = %d)", ret
);
4391 case LTTNG_CONSUMER32_UST
:
4392 case LTTNG_CONSUMER64_UST
:
4393 ret
= lttng_ustconsumer_clear_buffer(stream
);
4395 ERR("Failed to clear ust stream (ret = %d)", ret
);
4400 ERR("Unknown consumer_data type");
4404 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4406 ERR("Taking snapshot positions");
4409 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_after
);
4411 ERR("Consumed snapshot position");
4414 DBG("clear: before: %lu after: %lu", consumed_pos_before
, consumed_pos_after
);
4419 static int consumer_clear_stream(struct lttng_consumer_stream
*stream
)
4423 ret
= consumer_stream_flush_buffer(stream
, true);
4425 ERR("Failed to flush stream %" PRIu64
" during channel clear", stream
->key
);
4426 ret
= LTTCOMM_CONSUMERD_FATAL
;
4430 ret
= consumer_clear_buffer(stream
);
4432 ERR("Failed to clear stream %" PRIu64
" during channel clear", stream
->key
);
4433 ret
= LTTCOMM_CONSUMERD_FATAL
;
4437 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4442 static int consumer_clear_unmonitored_channel(struct lttng_consumer_channel
*channel
)
4445 struct lttng_consumer_stream
*stream
;
4447 lttng::urcu::read_lock_guard read_lock
;
4448 pthread_mutex_lock(&channel
->lock
);
4449 cds_list_for_each_entry (stream
, &channel
->streams
.head
, send_node
) {
4450 health_code_update();
4451 pthread_mutex_lock(&stream
->lock
);
4452 ret
= consumer_clear_stream(stream
);
4456 pthread_mutex_unlock(&stream
->lock
);
4458 pthread_mutex_unlock(&channel
->lock
);
4462 pthread_mutex_unlock(&stream
->lock
);
4463 pthread_mutex_unlock(&channel
->lock
);
4468 * Check if a stream is ready to be rotated after extracting it.
4470 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4471 * error. Stream lock must be held.
4473 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4475 DBG("Check is rotate ready for stream %" PRIu64
" ready %u rotate_position %" PRIu64
4476 " last_sequence_number %" PRIu64
,
4478 stream
->rotate_ready
,
4479 stream
->rotate_position
,
4480 stream
->last_sequence_number
);
4481 if (stream
->rotate_ready
) {
4486 * If packet seq num is unavailable, it means we are interacting
4487 * with a pre-2.8 lttng-modules which does not implement the
4488 * sequence number. Rotation should never be used by sessiond in this
4491 if (stream
->sequence_number_unavailable
) {
4492 ERR("Internal error: rotation used on stream %" PRIu64
4493 " with unavailable sequence number",
4498 if (stream
->rotate_position
== -1ULL || stream
->last_sequence_number
== -1ULL) {
4503 * Rotate position not reached yet. The stream rotate position is
4504 * the position of the next packet belonging to the next trace chunk,
4505 * but consumerd considers rotation ready when reaching the last
4506 * packet of the current chunk, hence the "rotate_position - 1".
4509 DBG("Check is rotate ready for stream %" PRIu64
" last_sequence_number %" PRIu64
4510 " rotate_position %" PRIu64
,
4512 stream
->last_sequence_number
,
4513 stream
->rotate_position
);
4514 if (stream
->last_sequence_number
>= stream
->rotate_position
- 1) {
4522 * Reset the state for a stream after a rotation occurred.
4524 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4526 DBG("lttng_consumer_reset_stream_rotate_state for stream %" PRIu64
, stream
->key
);
4527 stream
->rotate_position
= -1ULL;
4528 stream
->rotate_ready
= false;
4532 * Perform the rotation a local stream file.
4534 static int rotate_local_stream(struct lttng_consumer_stream
*stream
)
4538 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4541 stream
->tracefile_size_current
= 0;
4542 stream
->tracefile_count_current
= 0;
4544 if (stream
->out_fd
>= 0) {
4545 ret
= close(stream
->out_fd
);
4547 PERROR("Failed to close stream out_fd of channel \"%s\"",
4548 stream
->chan
->name
);
4550 stream
->out_fd
= -1;
4553 if (stream
->index_file
) {
4554 lttng_index_file_put(stream
->index_file
);
4555 stream
->index_file
= nullptr;
4558 if (!stream
->trace_chunk
) {
4562 ret
= consumer_stream_create_output_files(stream
, true);
4568 * Performs the stream rotation for the rotate session feature if needed.
4569 * It must be called with the channel and stream locks held.
4571 * Return 0 on success, a negative number of error.
4573 int lttng_consumer_rotate_stream(struct lttng_consumer_stream
*stream
)
4577 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4580 * Update the stream's 'current' chunk to the session's (channel)
4581 * now-current chunk.
4583 lttng_trace_chunk_put(stream
->trace_chunk
);
4584 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4586 * A channel can be rotated and not have a "next" chunk
4587 * to transition to. In that case, the channel's "current chunk"
4588 * has not been closed yet, but it has not been updated to
4589 * a "next" trace chunk either. Hence, the stream, like its
4590 * parent channel, becomes part of no chunk and can't output
4591 * anything until a new trace chunk is created.
4593 stream
->trace_chunk
= nullptr;
4594 } else if (stream
->chan
->trace_chunk
&& !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4595 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4600 * Update the stream's trace chunk to its parent channel's
4601 * current trace chunk.
4603 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4606 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4607 ret
= rotate_local_stream(stream
);
4609 ERR("Failed to rotate stream, ret = %i", ret
);
4614 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4616 * If the stream has transitioned to a new trace
4617 * chunk, the metadata should be re-dumped to the
4620 * However, it is possible for a stream to transition to
4621 * a "no-chunk" state. This can happen if a rotation
4622 * occurs on an inactive session. In such cases, the metadata
4623 * regeneration will happen when the next trace chunk is
4626 ret
= consumer_metadata_stream_dump(stream
);
4631 lttng_consumer_reset_stream_rotate_state(stream
);
4640 * Rotate all the ready streams now.
4642 * This is especially important for low throughput streams that have already
4643 * been consumed, we cannot wait for their next packet to perform the
4645 * Need to be called with RCU read-side lock held to ensure existence of
4648 * Returns 0 on success, < 0 on error
4650 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
, uint64_t key
)
4653 struct lttng_consumer_stream
*stream
;
4654 struct lttng_ht_iter iter
;
4655 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
4657 ASSERT_RCU_READ_LOCKED();
4659 lttng::urcu::read_lock_guard read_lock
;
4661 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4663 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4664 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4669 node_channel_id
.node
)
4671 health_code_update();
4673 pthread_mutex_lock(&stream
->chan
->lock
);
4674 pthread_mutex_lock(&stream
->lock
);
4676 if (!stream
->rotate_ready
) {
4677 pthread_mutex_unlock(&stream
->lock
);
4678 pthread_mutex_unlock(&stream
->chan
->lock
);
4681 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4683 ret
= lttng_consumer_rotate_stream(stream
);
4684 pthread_mutex_unlock(&stream
->lock
);
4685 pthread_mutex_unlock(&stream
->chan
->lock
);
4697 enum lttcomm_return_code
lttng_consumer_init_command(struct lttng_consumer_local_data
*ctx
,
4698 const lttng_uuid
& sessiond_uuid
)
4700 enum lttcomm_return_code ret
;
4701 char uuid_str
[LTTNG_UUID_STR_LEN
];
4703 if (ctx
->sessiond_uuid
.is_set
) {
4704 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4708 ctx
->sessiond_uuid
.is_set
= true;
4709 ctx
->sessiond_uuid
.value
= sessiond_uuid
;
4710 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4711 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4712 DBG("Received session daemon UUID: %s", uuid_str
);
4717 enum lttcomm_return_code
4718 lttng_consumer_create_trace_chunk(const uint64_t *relayd_id
,
4719 uint64_t session_id
,
4721 time_t chunk_creation_timestamp
,
4722 const char *chunk_override_name
,
4723 const struct lttng_credentials
*credentials
,
4724 struct lttng_directory_handle
*chunk_directory_handle
)
4727 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4728 struct lttng_trace_chunk
*created_chunk
= nullptr, *published_chunk
= nullptr;
4729 enum lttng_trace_chunk_status chunk_status
;
4730 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4731 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4732 const char *relayd_id_str
= "(none)";
4733 const char *creation_timestamp_str
;
4734 struct lttng_ht_iter iter
;
4735 struct lttng_consumer_channel
*channel
;
4738 /* Only used for logging purposes. */
4739 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
), "%" PRIu64
, *relayd_id
);
4740 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4741 relayd_id_str
= relayd_id_buffer
;
4743 relayd_id_str
= "(formatting error)";
4747 /* Local protocol error. */
4748 LTTNG_ASSERT(chunk_creation_timestamp
);
4749 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4750 creation_timestamp_buffer
,
4751 sizeof(creation_timestamp_buffer
));
4752 creation_timestamp_str
= !ret
? creation_timestamp_buffer
: "(formatting error)";
4754 DBG("Consumer create trace chunk command: relay_id = %s"
4755 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
", chunk_override_name = %s"
4756 ", chunk_creation_timestamp = %s",
4760 chunk_override_name
?: "(none)",
4761 creation_timestamp_str
);
4764 * The trace chunk registry, as used by the consumer daemon, implicitly
4765 * owns the trace chunks. This is only needed in the consumer since
4766 * the consumer has no notion of a session beyond session IDs being
4767 * used to identify other objects.
4769 * The lttng_trace_chunk_registry_publish() call below provides a
4770 * reference which is not released; it implicitly becomes the session
4771 * daemon's reference to the chunk in the consumer daemon.
4773 * The lifetime of trace chunks in the consumer daemon is managed by
4774 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4775 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4777 created_chunk
= lttng_trace_chunk_create(chunk_id
, chunk_creation_timestamp
, nullptr);
4778 if (!created_chunk
) {
4779 ERR("Failed to create trace chunk");
4780 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4784 if (chunk_override_name
) {
4785 chunk_status
= lttng_trace_chunk_override_name(created_chunk
, chunk_override_name
);
4786 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4787 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4792 if (chunk_directory_handle
) {
4793 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
, credentials
);
4794 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4795 ERR("Failed to set trace chunk credentials");
4796 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4800 * The consumer daemon has no ownership of the chunk output
4803 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
, chunk_directory_handle
);
4804 chunk_directory_handle
= nullptr;
4805 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4806 ERR("Failed to set trace chunk's directory handle");
4807 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4812 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4813 the_consumer_data
.chunk_registry
, session_id
, created_chunk
);
4814 lttng_trace_chunk_put(created_chunk
);
4815 created_chunk
= nullptr;
4816 if (!published_chunk
) {
4817 ERR("Failed to publish trace chunk");
4818 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4823 lttng::urcu::read_lock_guard read_lock
;
4824 cds_lfht_for_each_entry_duplicate(
4825 the_consumer_data
.channels_by_session_id_ht
->ht
,
4826 the_consumer_data
.channels_by_session_id_ht
->hash_fct(&session_id
,
4828 the_consumer_data
.channels_by_session_id_ht
->match_fct
,
4832 channels_by_session_id_ht_node
.node
)
4834 ret
= lttng_consumer_channel_set_trace_chunk(channel
, published_chunk
);
4837 * Roll-back the creation of this chunk.
4839 * This is important since the session daemon will
4840 * assume that the creation of this chunk failed and
4841 * will never ask for it to be closed, resulting
4842 * in a leak and an inconsistent state for some
4845 enum lttcomm_return_code close_ret
;
4846 char path
[LTTNG_PATH_MAX
];
4848 DBG("Failed to set new trace chunk on existing channels, rolling back");
4850 lttng_consumer_close_trace_chunk(relayd_id
,
4853 chunk_creation_timestamp
,
4856 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4857 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
4858 ", chunk_id = %" PRIu64
,
4863 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4870 struct consumer_relayd_sock_pair
*relayd
;
4872 relayd
= consumer_find_relayd(*relayd_id
);
4874 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4875 ret
= relayd_create_trace_chunk(&relayd
->control_sock
, published_chunk
);
4876 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4878 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4881 if (!relayd
|| ret
) {
4882 enum lttcomm_return_code close_ret
;
4883 char path
[LTTNG_PATH_MAX
];
4885 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4888 chunk_creation_timestamp
,
4891 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4892 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
4893 ", chunk_id = %" PRIu64
,
4898 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4904 /* Release the reference returned by the "publish" operation. */
4905 lttng_trace_chunk_put(published_chunk
);
4906 lttng_trace_chunk_put(created_chunk
);
4910 enum lttcomm_return_code
4911 lttng_consumer_close_trace_chunk(const uint64_t *relayd_id
,
4912 uint64_t session_id
,
4914 time_t chunk_close_timestamp
,
4915 const enum lttng_trace_chunk_command_type
*close_command
,
4918 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4919 struct lttng_trace_chunk
*chunk
;
4920 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4921 const char *relayd_id_str
= "(none)";
4922 const char *close_command_name
= "none";
4923 struct lttng_ht_iter iter
;
4924 struct lttng_consumer_channel
*channel
;
4925 enum lttng_trace_chunk_status chunk_status
;
4930 /* Only used for logging purposes. */
4931 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
), "%" PRIu64
, *relayd_id
);
4932 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4933 relayd_id_str
= relayd_id_buffer
;
4935 relayd_id_str
= "(formatting error)";
4938 if (close_command
) {
4939 close_command_name
= lttng_trace_chunk_command_type_get_name(*close_command
);
4942 DBG("Consumer close trace chunk command: relayd_id = %s"
4943 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
", close command = %s",
4947 close_command_name
);
4949 chunk
= lttng_trace_chunk_registry_find_chunk(
4950 the_consumer_data
.chunk_registry
, session_id
, chunk_id
);
4952 ERR("Failed to find chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4955 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4959 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
, chunk_close_timestamp
);
4960 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4961 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4965 if (close_command
) {
4966 chunk_status
= lttng_trace_chunk_set_close_command(chunk
, *close_command
);
4967 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4968 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4974 * chunk is now invalid to access as we no longer hold a reference to
4975 * it; it is only kept around to compare it (by address) to the
4976 * current chunk found in the session's channels.
4979 lttng::urcu::read_lock_guard read_lock
;
4980 cds_lfht_for_each_entry (
4981 the_consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
, node
.node
) {
4985 * Only change the channel's chunk to NULL if it still
4986 * references the chunk being closed. The channel may
4987 * reference a newer channel in the case of a session
4988 * rotation. When a session rotation occurs, the "next"
4989 * chunk is created before the "current" chunk is closed.
4991 if (channel
->trace_chunk
!= chunk
) {
4994 ret
= lttng_consumer_channel_set_trace_chunk(channel
, nullptr);
4997 * Attempt to close the chunk on as many channels as
5000 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5006 struct consumer_relayd_sock_pair
*relayd
;
5008 relayd
= consumer_find_relayd(*relayd_id
);
5010 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5011 ret
= relayd_close_trace_chunk(&relayd
->control_sock
, chunk
, path
);
5012 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5014 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
5017 if (!relayd
|| ret
) {
5018 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5025 * Release the reference returned by the "find" operation and
5026 * the session daemon's implicit reference to the chunk.
5028 lttng_trace_chunk_put(chunk
);
5029 lttng_trace_chunk_put(chunk
);
5034 enum lttcomm_return_code
5035 lttng_consumer_trace_chunk_exists(const uint64_t *relayd_id
, uint64_t session_id
, uint64_t chunk_id
)
5038 enum lttcomm_return_code ret_code
;
5039 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
5040 const char *relayd_id_str
= "(none)";
5041 const bool is_local_trace
= !relayd_id
;
5042 struct consumer_relayd_sock_pair
*relayd
= nullptr;
5043 bool chunk_exists_local
, chunk_exists_remote
;
5044 lttng::urcu::read_lock_guard read_lock
;
5047 /* Only used for logging purposes. */
5048 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
), "%" PRIu64
, *relayd_id
);
5049 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
5050 relayd_id_str
= relayd_id_buffer
;
5052 relayd_id_str
= "(formatting error)";
5056 DBG("Consumer trace chunk exists command: relayd_id = %s"
5057 ", chunk_id = %" PRIu64
,
5060 ret
= lttng_trace_chunk_registry_chunk_exists(
5061 the_consumer_data
.chunk_registry
, session_id
, chunk_id
, &chunk_exists_local
);
5063 /* Internal error. */
5064 ERR("Failed to query the existence of a trace chunk");
5065 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
5068 DBG("Trace chunk %s locally", chunk_exists_local
? "exists" : "does not exist");
5069 if (chunk_exists_local
) {
5070 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
5072 } else if (is_local_trace
) {
5073 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5077 relayd
= consumer_find_relayd(*relayd_id
);
5079 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
5080 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5081 goto end_rcu_unlock
;
5083 DBG("Looking up existence of trace chunk on relay daemon");
5084 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5085 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
, &chunk_exists_remote
);
5086 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5088 ERR("Failed to look-up the existence of trace chunk on relay daemon");
5089 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
5090 goto end_rcu_unlock
;
5093 ret_code
= chunk_exists_remote
? LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
5094 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5095 DBG("Trace chunk %s on relay daemon", chunk_exists_remote
? "exists" : "does not exist");
5102 static int consumer_clear_monitored_channel(struct lttng_consumer_channel
*channel
)
5104 struct lttng_ht
*ht
;
5105 struct lttng_consumer_stream
*stream
;
5106 struct lttng_ht_iter iter
;
5109 ht
= the_consumer_data
.stream_per_chan_id_ht
;
5111 lttng::urcu::read_lock_guard read_lock
;
5112 cds_lfht_for_each_entry_duplicate(ht
->ht
,
5113 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
5118 node_channel_id
.node
)
5121 * Protect against teardown with mutex.
5123 pthread_mutex_lock(&stream
->lock
);
5124 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5127 ret
= consumer_clear_stream(stream
);
5132 pthread_mutex_unlock(&stream
->lock
);
5134 return LTTCOMM_CONSUMERD_SUCCESS
;
5137 pthread_mutex_unlock(&stream
->lock
);
5141 int lttng_consumer_clear_channel(struct lttng_consumer_channel
*channel
)
5145 DBG("Consumer clear channel %" PRIu64
, channel
->key
);
5147 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
5149 * Nothing to do for the metadata channel/stream.
5150 * Snapshot mechanism already take care of the metadata
5151 * handling/generation, and monitored channels only need to
5152 * have their data stream cleared..
5154 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5158 if (!channel
->monitor
) {
5159 ret
= consumer_clear_unmonitored_channel(channel
);
5161 ret
= consumer_clear_monitored_channel(channel
);
5167 enum lttcomm_return_code
lttng_consumer_open_channel_packets(struct lttng_consumer_channel
*channel
)
5169 struct lttng_consumer_stream
*stream
;
5170 enum lttcomm_return_code ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5172 if (channel
->metadata_stream
) {
5173 ERR("Open channel packets command attempted on a metadata channel");
5174 ret
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5179 lttng::urcu::read_lock_guard read_lock
;
5180 cds_list_for_each_entry (stream
, &channel
->streams
.head
, send_node
) {
5181 enum consumer_stream_open_packet_status status
;
5183 pthread_mutex_lock(&stream
->lock
);
5184 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5188 status
= consumer_stream_open_packet(stream
);
5190 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
5191 DBG("Opened a packet in \"open channel packets\" command: stream id = %" PRIu64
5192 ", channel name = %s, session id = %" PRIu64
,
5195 stream
->chan
->session_id
);
5196 stream
->opened_packet_in_current_trace_chunk
= true;
5198 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
5199 DBG("No space left to open a packet in \"open channel packets\" command: stream id = %" PRIu64
5200 ", channel name = %s, session id = %" PRIu64
,
5203 stream
->chan
->session_id
);
5205 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
5207 * Only unexpected internal errors can lead to this
5208 * failing. Report an unknown error.
5210 ERR("Failed to flush empty buffer in \"open channel packets\" command: stream id = %" PRIu64
5211 ", channel id = %" PRIu64
", channel name = %s"
5212 ", session id = %" PRIu64
,
5216 channel
->session_id
);
5217 ret
= LTTCOMM_CONSUMERD_UNKNOWN_ERROR
;
5224 pthread_mutex_unlock(&stream
->lock
);
5232 pthread_mutex_unlock(&stream
->lock
);
5233 goto end_rcu_unlock
;
5236 void lttng_consumer_sigbus_handle(void *addr
)
5238 lttng_ustconsumer_sigbus_handle(addr
);