2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2012 - David Goulet <dgoulet@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "common/index/ctf-index.h"
28 #include <sys/socket.h>
29 #include <sys/types.h>
34 #include <bin/lttng-consumerd/health-consumerd.h>
35 #include <common/common.h>
36 #include <common/utils.h>
37 #include <common/time.h>
38 #include <common/compat/poll.h>
39 #include <common/compat/endian.h>
40 #include <common/index/index.h>
41 #include <common/kernel-ctl/kernel-ctl.h>
42 #include <common/sessiond-comm/relayd.h>
43 #include <common/sessiond-comm/sessiond-comm.h>
44 #include <common/kernel-consumer/kernel-consumer.h>
45 #include <common/relayd/relayd.h>
46 #include <common/ust-consumer/ust-consumer.h>
47 #include <common/consumer/consumer-timer.h>
48 #include <common/consumer/consumer.h>
49 #include <common/consumer/consumer-stream.h>
50 #include <common/consumer/consumer-testpoint.h>
51 #include <common/align.h>
52 #include <common/consumer/consumer-metadata-cache.h>
53 #include <common/trace-chunk.h>
54 #include <common/trace-chunk-registry.h>
55 #include <common/string-utils/format.h>
56 #include <common/dynamic-array.h>
58 struct lttng_consumer_global_data consumer_data
= {
61 .type
= LTTNG_CONSUMER_UNKNOWN
,
64 enum consumer_channel_action
{
67 CONSUMER_CHANNEL_QUIT
,
70 struct consumer_channel_msg
{
71 enum consumer_channel_action action
;
72 struct lttng_consumer_channel
*chan
; /* add */
73 uint64_t key
; /* del */
76 /* Flag used to temporarily pause data consumption from testpoints. */
77 int data_consumption_paused
;
80 * Flag to inform the polling thread to quit when all fd hung up. Updated by
81 * the consumer_thread_receive_fds when it notices that all fds has hung up.
82 * Also updated by the signal handler (consumer_should_exit()). Read by the
88 * Global hash table containing respectively metadata and data streams. The
89 * stream element in this ht should only be updated by the metadata poll thread
90 * for the metadata and the data poll thread for the data.
92 static struct lttng_ht
*metadata_ht
;
93 static struct lttng_ht
*data_ht
;
96 * Notify a thread lttng pipe to poll back again. This usually means that some
97 * global state has changed so we just send back the thread in a poll wait
100 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
102 struct lttng_consumer_stream
*null_stream
= NULL
;
106 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
109 static void notify_health_quit_pipe(int *pipe
)
113 ret
= lttng_write(pipe
[1], "4", 1);
115 PERROR("write consumer health quit");
119 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
120 struct lttng_consumer_channel
*chan
,
122 enum consumer_channel_action action
)
124 struct consumer_channel_msg msg
;
127 memset(&msg
, 0, sizeof(msg
));
132 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
133 if (ret
< sizeof(msg
)) {
134 PERROR("notify_channel_pipe write error");
138 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
141 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
144 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
145 struct lttng_consumer_channel
**chan
,
147 enum consumer_channel_action
*action
)
149 struct consumer_channel_msg msg
;
152 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
153 if (ret
< sizeof(msg
)) {
157 *action
= msg
.action
;
165 * Cleanup the stream list of a channel. Those streams are not yet globally
168 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
170 struct lttng_consumer_stream
*stream
, *stmp
;
174 /* Delete streams that might have been left in the stream list. */
175 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
177 cds_list_del(&stream
->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
, NULL
);
190 * Find a stream. The consumer_data.lock must be locked during this
193 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
196 struct lttng_ht_iter iter
;
197 struct lttng_ht_node_u64
*node
;
198 struct lttng_consumer_stream
*stream
= NULL
;
202 /* -1ULL keys are lookup failures */
203 if (key
== (uint64_t) -1ULL) {
209 lttng_ht_lookup(ht
, &key
, &iter
);
210 node
= lttng_ht_iter_get_node_u64(&iter
);
212 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
220 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
222 struct lttng_consumer_stream
*stream
;
225 stream
= find_stream(key
, ht
);
227 stream
->key
= (uint64_t) -1ULL;
229 * We don't want the lookup to match, but we still need
230 * to iterate on this stream when iterating over the hash table. Just
231 * change the node key.
233 stream
->node
.key
= (uint64_t) -1ULL;
239 * Return a channel object for the given key.
241 * RCU read side lock MUST be acquired before calling this function and
242 * protects the channel ptr.
244 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
246 struct lttng_ht_iter iter
;
247 struct lttng_ht_node_u64
*node
;
248 struct lttng_consumer_channel
*channel
= NULL
;
250 /* -1ULL keys are lookup failures */
251 if (key
== (uint64_t) -1ULL) {
255 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
256 node
= lttng_ht_iter_get_node_u64(&iter
);
258 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
265 * There is a possibility that the consumer does not have enough time between
266 * the close of the channel on the session daemon and the cleanup in here thus
267 * once we have a channel add with an existing key, we know for sure that this
268 * channel will eventually get cleaned up by all streams being closed.
270 * This function just nullifies the already existing channel key.
272 static void steal_channel_key(uint64_t key
)
274 struct lttng_consumer_channel
*channel
;
277 channel
= consumer_find_channel(key
);
279 channel
->key
= (uint64_t) -1ULL;
281 * We don't want the lookup to match, but we still need to iterate on
282 * this channel when iterating over the hash table. Just change the
285 channel
->node
.key
= (uint64_t) -1ULL;
290 static void free_channel_rcu(struct rcu_head
*head
)
292 struct lttng_ht_node_u64
*node
=
293 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
294 struct lttng_consumer_channel
*channel
=
295 caa_container_of(node
, struct lttng_consumer_channel
, node
);
297 switch (consumer_data
.type
) {
298 case LTTNG_CONSUMER_KERNEL
:
300 case LTTNG_CONSUMER32_UST
:
301 case LTTNG_CONSUMER64_UST
:
302 lttng_ustconsumer_free_channel(channel
);
305 ERR("Unknown consumer_data type");
312 * RCU protected relayd socket pair free.
314 static void free_relayd_rcu(struct rcu_head
*head
)
316 struct lttng_ht_node_u64
*node
=
317 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
318 struct consumer_relayd_sock_pair
*relayd
=
319 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
322 * Close all sockets. This is done in the call RCU since we don't want the
323 * socket fds to be reassigned thus potentially creating bad state of the
326 * We do not have to lock the control socket mutex here since at this stage
327 * there is no one referencing to this relayd object.
329 (void) relayd_close(&relayd
->control_sock
);
330 (void) relayd_close(&relayd
->data_sock
);
332 pthread_mutex_destroy(&relayd
->ctrl_sock_mutex
);
337 * Destroy and free relayd socket pair object.
339 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
342 struct lttng_ht_iter iter
;
344 if (relayd
== NULL
) {
348 DBG("Consumer destroy and close relayd socket pair");
350 iter
.iter
.node
= &relayd
->node
.node
;
351 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
353 /* We assume the relayd is being or is destroyed */
357 /* RCU free() call */
358 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
362 * Remove a channel from the global list protected by a mutex. This function is
363 * also responsible for freeing its data structures.
365 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
367 struct lttng_ht_iter iter
;
369 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
371 pthread_mutex_lock(&consumer_data
.lock
);
372 pthread_mutex_lock(&channel
->lock
);
374 /* Destroy streams that might have been left in the stream list. */
375 clean_channel_stream_list(channel
);
377 if (channel
->live_timer_enabled
== 1) {
378 consumer_timer_live_stop(channel
);
380 if (channel
->monitor_timer_enabled
== 1) {
381 consumer_timer_monitor_stop(channel
);
384 switch (consumer_data
.type
) {
385 case LTTNG_CONSUMER_KERNEL
:
387 case LTTNG_CONSUMER32_UST
:
388 case LTTNG_CONSUMER64_UST
:
389 lttng_ustconsumer_del_channel(channel
);
392 ERR("Unknown consumer_data type");
397 lttng_trace_chunk_put(channel
->trace_chunk
);
398 channel
->trace_chunk
= NULL
;
400 if (channel
->is_published
) {
404 iter
.iter
.node
= &channel
->node
.node
;
405 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
408 iter
.iter
.node
= &channel
->channels_by_session_id_ht_node
.node
;
409 ret
= lttng_ht_del(consumer_data
.channels_by_session_id_ht
,
415 channel
->is_deleted
= true;
416 call_rcu(&channel
->node
.head
, free_channel_rcu
);
418 pthread_mutex_unlock(&channel
->lock
);
419 pthread_mutex_unlock(&consumer_data
.lock
);
423 * Iterate over the relayd hash table and destroy each element. Finally,
424 * destroy the whole hash table.
426 static void cleanup_relayd_ht(void)
428 struct lttng_ht_iter iter
;
429 struct consumer_relayd_sock_pair
*relayd
;
433 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
435 consumer_destroy_relayd(relayd
);
440 lttng_ht_destroy(consumer_data
.relayd_ht
);
444 * Update the end point status of all streams having the given network sequence
445 * index (relayd index).
447 * It's atomically set without having the stream mutex locked which is fine
448 * because we handle the write/read race with a pipe wakeup for each thread.
450 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
451 enum consumer_endpoint_status status
)
453 struct lttng_ht_iter iter
;
454 struct lttng_consumer_stream
*stream
;
456 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
460 /* Let's begin with metadata */
461 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
462 if (stream
->net_seq_idx
== net_seq_idx
) {
463 uatomic_set(&stream
->endpoint_status
, status
);
464 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
468 /* Follow up by the data streams */
469 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
470 if (stream
->net_seq_idx
== net_seq_idx
) {
471 uatomic_set(&stream
->endpoint_status
, status
);
472 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
479 * Cleanup a relayd object by flagging every associated streams for deletion,
480 * destroying the object meaning removing it from the relayd hash table,
481 * closing the sockets and freeing the memory in a RCU call.
483 * If a local data context is available, notify the threads that the streams'
484 * state have changed.
486 void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
)
492 DBG("Cleaning up relayd object ID %"PRIu64
, relayd
->net_seq_idx
);
494 /* Save the net sequence index before destroying the object */
495 netidx
= relayd
->net_seq_idx
;
498 * Delete the relayd from the relayd hash table, close the sockets and free
499 * the object in a RCU call.
501 consumer_destroy_relayd(relayd
);
503 /* Set inactive endpoint to all streams */
504 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
507 * With a local data context, notify the threads that the streams' state
508 * have changed. The write() action on the pipe acts as an "implicit"
509 * memory barrier ordering the updates of the end point status from the
510 * read of this status which happens AFTER receiving this notify.
512 notify_thread_lttng_pipe(relayd
->ctx
->consumer_data_pipe
);
513 notify_thread_lttng_pipe(relayd
->ctx
->consumer_metadata_pipe
);
517 * Flag a relayd socket pair for destruction. Destroy it if the refcount
520 * RCU read side lock MUST be aquired before calling this function.
522 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
526 /* Set destroy flag for this object */
527 uatomic_set(&relayd
->destroy_flag
, 1);
529 /* Destroy the relayd if refcount is 0 */
530 if (uatomic_read(&relayd
->refcount
) == 0) {
531 consumer_destroy_relayd(relayd
);
536 * Completly destroy stream from every visiable data structure and the given
539 * One this call returns, the stream object is not longer usable nor visible.
541 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
544 consumer_stream_destroy(stream
, ht
);
548 * XXX naming of del vs destroy is all mixed up.
550 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
552 consumer_stream_destroy(stream
, data_ht
);
555 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
557 consumer_stream_destroy(stream
, metadata_ht
);
560 void consumer_stream_update_channel_attributes(
561 struct lttng_consumer_stream
*stream
,
562 struct lttng_consumer_channel
*channel
)
564 stream
->channel_read_only_attributes
.tracefile_size
=
565 channel
->tracefile_size
;
569 * Add a stream to the global list protected by a mutex.
571 void consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
573 struct lttng_ht
*ht
= data_ht
;
578 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
580 pthread_mutex_lock(&consumer_data
.lock
);
581 pthread_mutex_lock(&stream
->chan
->lock
);
582 pthread_mutex_lock(&stream
->chan
->timer_lock
);
583 pthread_mutex_lock(&stream
->lock
);
586 /* Steal stream identifier to avoid having streams with the same key */
587 steal_stream_key(stream
->key
, ht
);
589 lttng_ht_add_unique_u64(ht
, &stream
->node
);
591 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
592 &stream
->node_channel_id
);
595 * Add stream to the stream_list_ht of the consumer data. No need to steal
596 * the key since the HT does not use it and we allow to add redundant keys
599 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
602 * When nb_init_stream_left reaches 0, we don't need to trigger any action
603 * in terms of destroying the associated channel, because the action that
604 * causes the count to become 0 also causes a stream to be added. The
605 * channel deletion will thus be triggered by the following removal of this
608 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
609 /* Increment refcount before decrementing nb_init_stream_left */
611 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
614 /* Update consumer data once the node is inserted. */
615 consumer_data
.stream_count
++;
616 consumer_data
.need_update
= 1;
619 pthread_mutex_unlock(&stream
->lock
);
620 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
621 pthread_mutex_unlock(&stream
->chan
->lock
);
622 pthread_mutex_unlock(&consumer_data
.lock
);
625 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
627 consumer_del_stream(stream
, data_ht
);
631 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
632 * be acquired before calling this.
634 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
637 struct lttng_ht_node_u64
*node
;
638 struct lttng_ht_iter iter
;
642 lttng_ht_lookup(consumer_data
.relayd_ht
,
643 &relayd
->net_seq_idx
, &iter
);
644 node
= lttng_ht_iter_get_node_u64(&iter
);
648 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
655 * Allocate and return a consumer relayd socket.
657 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
658 uint64_t net_seq_idx
)
660 struct consumer_relayd_sock_pair
*obj
= NULL
;
662 /* net sequence index of -1 is a failure */
663 if (net_seq_idx
== (uint64_t) -1ULL) {
667 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
669 PERROR("zmalloc relayd sock");
673 obj
->net_seq_idx
= net_seq_idx
;
675 obj
->destroy_flag
= 0;
676 obj
->control_sock
.sock
.fd
= -1;
677 obj
->data_sock
.sock
.fd
= -1;
678 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
679 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
686 * Find a relayd socket pair in the global consumer data.
688 * Return the object if found else NULL.
689 * RCU read-side lock must be held across this call and while using the
692 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
694 struct lttng_ht_iter iter
;
695 struct lttng_ht_node_u64
*node
;
696 struct consumer_relayd_sock_pair
*relayd
= NULL
;
698 /* Negative keys are lookup failures */
699 if (key
== (uint64_t) -1ULL) {
703 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
705 node
= lttng_ht_iter_get_node_u64(&iter
);
707 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
715 * Find a relayd and send the stream
717 * Returns 0 on success, < 0 on error
719 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
723 struct consumer_relayd_sock_pair
*relayd
;
726 assert(stream
->net_seq_idx
!= -1ULL);
729 /* The stream is not metadata. Get relayd reference if exists. */
731 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
732 if (relayd
!= NULL
) {
733 /* Add stream on the relayd */
734 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
735 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
736 path
, &stream
->relayd_stream_id
,
737 stream
->chan
->tracefile_size
,
738 stream
->chan
->tracefile_count
,
739 stream
->trace_chunk
);
740 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
742 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
743 lttng_consumer_cleanup_relayd(relayd
);
747 uatomic_inc(&relayd
->refcount
);
748 stream
->sent_to_relayd
= 1;
750 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
751 stream
->key
, stream
->net_seq_idx
);
756 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
757 stream
->name
, stream
->key
, stream
->net_seq_idx
);
765 * Find a relayd and send the streams sent message
767 * Returns 0 on success, < 0 on error
769 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
772 struct consumer_relayd_sock_pair
*relayd
;
774 assert(net_seq_idx
!= -1ULL);
776 /* The stream is not metadata. Get relayd reference if exists. */
778 relayd
= consumer_find_relayd(net_seq_idx
);
779 if (relayd
!= NULL
) {
780 /* Add stream on the relayd */
781 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
782 ret
= relayd_streams_sent(&relayd
->control_sock
);
783 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
785 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
786 lttng_consumer_cleanup_relayd(relayd
);
790 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
797 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
805 * Find a relayd and close the stream
807 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
809 struct consumer_relayd_sock_pair
*relayd
;
811 /* The stream is not metadata. Get relayd reference if exists. */
813 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
815 consumer_stream_relayd_close(stream
, relayd
);
821 * Handle stream for relayd transmission if the stream applies for network
822 * streaming where the net sequence index is set.
824 * Return destination file descriptor or negative value on error.
826 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
827 size_t data_size
, unsigned long padding
,
828 struct consumer_relayd_sock_pair
*relayd
)
831 struct lttcomm_relayd_data_hdr data_hdr
;
837 /* Reset data header */
838 memset(&data_hdr
, 0, sizeof(data_hdr
));
840 if (stream
->metadata_flag
) {
841 /* Caller MUST acquire the relayd control socket lock */
842 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
847 /* Metadata are always sent on the control socket. */
848 outfd
= relayd
->control_sock
.sock
.fd
;
850 /* Set header with stream information */
851 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
852 data_hdr
.data_size
= htobe32(data_size
);
853 data_hdr
.padding_size
= htobe32(padding
);
856 * Note that net_seq_num below is assigned with the *current* value of
857 * next_net_seq_num and only after that the next_net_seq_num will be
858 * increment. This is why when issuing a command on the relayd using
859 * this next value, 1 should always be substracted in order to compare
860 * the last seen sequence number on the relayd side to the last sent.
862 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
863 /* Other fields are zeroed previously */
865 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
871 ++stream
->next_net_seq_num
;
873 /* Set to go on data socket */
874 outfd
= relayd
->data_sock
.sock
.fd
;
882 * Trigger a dump of the metadata content. Following/during the succesful
883 * completion of this call, the metadata poll thread will start receiving
884 * metadata packets to consume.
886 * The caller must hold the channel and stream locks.
889 int consumer_metadata_stream_dump(struct lttng_consumer_stream
*stream
)
893 ASSERT_LOCKED(stream
->chan
->lock
);
894 ASSERT_LOCKED(stream
->lock
);
895 assert(stream
->metadata_flag
);
896 assert(stream
->chan
->trace_chunk
);
898 switch (consumer_data
.type
) {
899 case LTTNG_CONSUMER_KERNEL
:
901 * Reset the position of what has been read from the
902 * metadata cache to 0 so we can dump it again.
904 ret
= kernctl_metadata_cache_dump(stream
->wait_fd
);
906 case LTTNG_CONSUMER32_UST
:
907 case LTTNG_CONSUMER64_UST
:
909 * Reset the position pushed from the metadata cache so it
910 * will write from the beginning on the next push.
912 stream
->ust_metadata_pushed
= 0;
913 ret
= consumer_metadata_wakeup_pipe(stream
->chan
);
916 ERR("Unknown consumer_data type");
920 ERR("Failed to dump the metadata cache");
926 int lttng_consumer_channel_set_trace_chunk(
927 struct lttng_consumer_channel
*channel
,
928 struct lttng_trace_chunk
*new_trace_chunk
)
930 pthread_mutex_lock(&channel
->lock
);
931 if (channel
->is_deleted
) {
933 * The channel has been logically deleted and should no longer
934 * be used. It has released its reference to its current trace
935 * chunk and should not acquire a new one.
937 * Return success as there is nothing for the caller to do.
943 * The acquisition of the reference cannot fail (barring
944 * a severe internal error) since a reference to the published
945 * chunk is already held by the caller.
947 if (new_trace_chunk
) {
948 const bool acquired_reference
= lttng_trace_chunk_get(
951 assert(acquired_reference
);
954 lttng_trace_chunk_put(channel
->trace_chunk
);
955 channel
->trace_chunk
= new_trace_chunk
;
957 pthread_mutex_unlock(&channel
->lock
);
962 * Allocate and return a new lttng_consumer_channel object using the given key
963 * to initialize the hash table node.
965 * On error, return NULL.
967 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
969 const uint64_t *chunk_id
,
970 const char *pathname
,
973 enum lttng_event_output output
,
974 uint64_t tracefile_size
,
975 uint64_t tracefile_count
,
976 uint64_t session_id_per_pid
,
977 unsigned int monitor
,
978 unsigned int live_timer_interval
,
979 bool is_in_live_session
,
980 const char *root_shm_path
,
981 const char *shm_path
)
983 struct lttng_consumer_channel
*channel
= NULL
;
984 struct lttng_trace_chunk
*trace_chunk
= NULL
;
987 trace_chunk
= lttng_trace_chunk_registry_find_chunk(
988 consumer_data
.chunk_registry
, session_id
,
991 ERR("Failed to find trace chunk reference during creation of channel");
996 channel
= zmalloc(sizeof(*channel
));
997 if (channel
== NULL
) {
998 PERROR("malloc struct lttng_consumer_channel");
1003 channel
->refcount
= 0;
1004 channel
->session_id
= session_id
;
1005 channel
->session_id_per_pid
= session_id_per_pid
;
1006 channel
->relayd_id
= relayd_id
;
1007 channel
->tracefile_size
= tracefile_size
;
1008 channel
->tracefile_count
= tracefile_count
;
1009 channel
->monitor
= monitor
;
1010 channel
->live_timer_interval
= live_timer_interval
;
1011 channel
->is_live
= is_in_live_session
;
1012 pthread_mutex_init(&channel
->lock
, NULL
);
1013 pthread_mutex_init(&channel
->timer_lock
, NULL
);
1016 case LTTNG_EVENT_SPLICE
:
1017 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
1019 case LTTNG_EVENT_MMAP
:
1020 channel
->output
= CONSUMER_CHANNEL_MMAP
;
1030 * In monitor mode, the streams associated with the channel will be put in
1031 * a special list ONLY owned by this channel. So, the refcount is set to 1
1032 * here meaning that the channel itself has streams that are referenced.
1034 * On a channel deletion, once the channel is no longer visible, the
1035 * refcount is decremented and checked for a zero value to delete it. With
1036 * streams in no monitor mode, it will now be safe to destroy the channel.
1038 if (!channel
->monitor
) {
1039 channel
->refcount
= 1;
1042 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1043 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1045 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1046 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1048 if (root_shm_path
) {
1049 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1050 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1053 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1054 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1057 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1058 lttng_ht_node_init_u64(&channel
->channels_by_session_id_ht_node
,
1059 channel
->session_id
);
1061 channel
->wait_fd
= -1;
1062 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1065 int ret
= lttng_consumer_channel_set_trace_chunk(channel
,
1072 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1075 lttng_trace_chunk_put(trace_chunk
);
1078 consumer_del_channel(channel
);
1084 * Add a channel to the global list protected by a mutex.
1086 * Always return 0 indicating success.
1088 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1089 struct lttng_consumer_local_data
*ctx
)
1091 pthread_mutex_lock(&consumer_data
.lock
);
1092 pthread_mutex_lock(&channel
->lock
);
1093 pthread_mutex_lock(&channel
->timer_lock
);
1096 * This gives us a guarantee that the channel we are about to add to the
1097 * channel hash table will be unique. See this function comment on the why
1098 * we need to steel the channel key at this stage.
1100 steal_channel_key(channel
->key
);
1103 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1104 lttng_ht_add_u64(consumer_data
.channels_by_session_id_ht
,
1105 &channel
->channels_by_session_id_ht_node
);
1107 channel
->is_published
= true;
1109 pthread_mutex_unlock(&channel
->timer_lock
);
1110 pthread_mutex_unlock(&channel
->lock
);
1111 pthread_mutex_unlock(&consumer_data
.lock
);
1113 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1114 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1121 * Allocate the pollfd structure and the local view of the out fds to avoid
1122 * doing a lookup in the linked list and concurrency issues when writing is
1123 * needed. Called with consumer_data.lock held.
1125 * Returns the number of fds in the structures.
1127 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1128 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1129 struct lttng_ht
*ht
, int *nb_inactive_fd
)
1132 struct lttng_ht_iter iter
;
1133 struct lttng_consumer_stream
*stream
;
1138 assert(local_stream
);
1140 DBG("Updating poll fd array");
1141 *nb_inactive_fd
= 0;
1143 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1145 * Only active streams with an active end point can be added to the
1146 * poll set and local stream storage of the thread.
1148 * There is a potential race here for endpoint_status to be updated
1149 * just after the check. However, this is OK since the stream(s) will
1150 * be deleted once the thread is notified that the end point state has
1151 * changed where this function will be called back again.
1153 * We track the number of inactive FDs because they still need to be
1154 * closed by the polling thread after a wakeup on the data_pipe or
1157 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1158 (*nb_inactive_fd
)++;
1162 * This clobbers way too much the debug output. Uncomment that if you
1163 * need it for debugging purposes.
1165 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1166 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1167 local_stream
[i
] = stream
;
1173 * Insert the consumer_data_pipe at the end of the array and don't
1174 * increment i so nb_fd is the number of real FD.
1176 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1177 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1179 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1180 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1185 * Poll on the should_quit pipe and the command socket return -1 on
1186 * error, 1 if should exit, 0 if data is available on the command socket
1188 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1193 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1194 if (num_rdy
== -1) {
1196 * Restart interrupted system call.
1198 if (errno
== EINTR
) {
1201 PERROR("Poll error");
1204 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1205 DBG("consumer_should_quit wake up");
1212 * Set the error socket.
1214 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1217 ctx
->consumer_error_socket
= sock
;
1221 * Set the command socket path.
1223 void lttng_consumer_set_command_sock_path(
1224 struct lttng_consumer_local_data
*ctx
, char *sock
)
1226 ctx
->consumer_command_sock_path
= sock
;
1230 * Send return code to the session daemon.
1231 * If the socket is not defined, we return 0, it is not a fatal error
1233 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1235 if (ctx
->consumer_error_socket
> 0) {
1236 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1237 sizeof(enum lttcomm_sessiond_command
));
1244 * Close all the tracefiles and stream fds and MUST be called when all
1245 * instances are destroyed i.e. when all threads were joined and are ended.
1247 void lttng_consumer_cleanup(void)
1249 struct lttng_ht_iter iter
;
1250 struct lttng_consumer_channel
*channel
;
1251 unsigned int trace_chunks_left
;
1255 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1257 consumer_del_channel(channel
);
1262 lttng_ht_destroy(consumer_data
.channel_ht
);
1263 lttng_ht_destroy(consumer_data
.channels_by_session_id_ht
);
1265 cleanup_relayd_ht();
1267 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1270 * This HT contains streams that are freed by either the metadata thread or
1271 * the data thread so we do *nothing* on the hash table and simply destroy
1274 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1277 * Trace chunks in the registry may still exist if the session
1278 * daemon has encountered an internal error and could not
1279 * tear down its sessions and/or trace chunks properly.
1281 * Release the session daemon's implicit reference to any remaining
1282 * trace chunk and print an error if any trace chunk was found. Note
1283 * that there are _no_ legitimate cases for trace chunks to be left,
1284 * it is a leak. However, it can happen following a crash of the
1285 * session daemon and not emptying the registry would cause an assertion
1288 trace_chunks_left
= lttng_trace_chunk_registry_put_each_chunk(
1289 consumer_data
.chunk_registry
);
1290 if (trace_chunks_left
) {
1291 ERR("%u trace chunks are leaked by lttng-consumerd. "
1292 "This can be caused by an internal error of the session daemon.",
1295 /* Run all callbacks freeing each chunk. */
1297 lttng_trace_chunk_registry_destroy(consumer_data
.chunk_registry
);
1301 * Called from signal handler.
1303 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1307 CMM_STORE_SHARED(consumer_quit
, 1);
1308 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1310 PERROR("write consumer quit");
1313 DBG("Consumer flag that it should quit");
1318 * Flush pending writes to trace output disk file.
1321 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1325 int outfd
= stream
->out_fd
;
1328 * This does a blocking write-and-wait on any page that belongs to the
1329 * subbuffer prior to the one we just wrote.
1330 * Don't care about error values, as these are just hints and ways to
1331 * limit the amount of page cache used.
1333 if (orig_offset
< stream
->max_sb_size
) {
1336 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1337 stream
->max_sb_size
,
1338 SYNC_FILE_RANGE_WAIT_BEFORE
1339 | SYNC_FILE_RANGE_WRITE
1340 | SYNC_FILE_RANGE_WAIT_AFTER
);
1342 * Give hints to the kernel about how we access the file:
1343 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1346 * We need to call fadvise again after the file grows because the
1347 * kernel does not seem to apply fadvise to non-existing parts of the
1350 * Call fadvise _after_ having waited for the page writeback to
1351 * complete because the dirty page writeback semantic is not well
1352 * defined. So it can be expected to lead to lower throughput in
1355 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1356 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1357 if (ret
&& ret
!= -ENOSYS
) {
1359 PERROR("posix_fadvise on fd %i", outfd
);
1364 * Initialise the necessary environnement :
1365 * - create a new context
1366 * - create the poll_pipe
1367 * - create the should_quit pipe (for signal handler)
1368 * - create the thread pipe (for splice)
1370 * Takes a function pointer as argument, this function is called when data is
1371 * available on a buffer. This function is responsible to do the
1372 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1373 * buffer configuration and then kernctl_put_next_subbuf at the end.
1375 * Returns a pointer to the new context or NULL on error.
1377 struct lttng_consumer_local_data
*lttng_consumer_create(
1378 enum lttng_consumer_type type
,
1379 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1380 struct lttng_consumer_local_data
*ctx
, bool locked_by_caller
),
1381 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1382 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1383 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1386 struct lttng_consumer_local_data
*ctx
;
1388 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1389 consumer_data
.type
== type
);
1390 consumer_data
.type
= type
;
1392 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1394 PERROR("allocating context");
1398 ctx
->consumer_error_socket
= -1;
1399 ctx
->consumer_metadata_socket
= -1;
1400 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1401 /* assign the callbacks */
1402 ctx
->on_buffer_ready
= buffer_ready
;
1403 ctx
->on_recv_channel
= recv_channel
;
1404 ctx
->on_recv_stream
= recv_stream
;
1405 ctx
->on_update_stream
= update_stream
;
1407 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1408 if (!ctx
->consumer_data_pipe
) {
1409 goto error_poll_pipe
;
1412 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1413 if (!ctx
->consumer_wakeup_pipe
) {
1414 goto error_wakeup_pipe
;
1417 ret
= pipe(ctx
->consumer_should_quit
);
1419 PERROR("Error creating recv pipe");
1420 goto error_quit_pipe
;
1423 ret
= pipe(ctx
->consumer_channel_pipe
);
1425 PERROR("Error creating channel pipe");
1426 goto error_channel_pipe
;
1429 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1430 if (!ctx
->consumer_metadata_pipe
) {
1431 goto error_metadata_pipe
;
1434 ctx
->channel_monitor_pipe
= -1;
1438 error_metadata_pipe
:
1439 utils_close_pipe(ctx
->consumer_channel_pipe
);
1441 utils_close_pipe(ctx
->consumer_should_quit
);
1443 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1445 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1453 * Iterate over all streams of the hashtable and free them properly.
1455 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1457 struct lttng_ht_iter iter
;
1458 struct lttng_consumer_stream
*stream
;
1465 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1467 * Ignore return value since we are currently cleaning up so any error
1470 (void) consumer_del_stream(stream
, ht
);
1474 lttng_ht_destroy(ht
);
1478 * Iterate over all streams of the metadata hashtable and free them
1481 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1483 struct lttng_ht_iter iter
;
1484 struct lttng_consumer_stream
*stream
;
1491 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1493 * Ignore return value since we are currently cleaning up so any error
1496 (void) consumer_del_metadata_stream(stream
, ht
);
1500 lttng_ht_destroy(ht
);
1504 * Close all fds associated with the instance and free the context.
1506 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1510 DBG("Consumer destroying it. Closing everything.");
1516 destroy_data_stream_ht(data_ht
);
1517 destroy_metadata_stream_ht(metadata_ht
);
1519 ret
= close(ctx
->consumer_error_socket
);
1523 ret
= close(ctx
->consumer_metadata_socket
);
1527 utils_close_pipe(ctx
->consumer_channel_pipe
);
1528 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1529 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1530 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1531 utils_close_pipe(ctx
->consumer_should_quit
);
1533 unlink(ctx
->consumer_command_sock_path
);
1538 * Write the metadata stream id on the specified file descriptor.
1540 static int write_relayd_metadata_id(int fd
,
1541 struct lttng_consumer_stream
*stream
,
1542 unsigned long padding
)
1545 struct lttcomm_relayd_metadata_payload hdr
;
1547 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1548 hdr
.padding_size
= htobe32(padding
);
1549 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1550 if (ret
< sizeof(hdr
)) {
1552 * This error means that the fd's end is closed so ignore the PERROR
1553 * not to clubber the error output since this can happen in a normal
1556 if (errno
!= EPIPE
) {
1557 PERROR("write metadata stream id");
1559 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1561 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1562 * handle writting the missing part so report that as an error and
1563 * don't lie to the caller.
1568 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1569 stream
->relayd_stream_id
, padding
);
1576 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1577 * core function for writing trace buffers to either the local filesystem or
1580 * It must be called with the stream and the channel lock held.
1582 * Careful review MUST be put if any changes occur!
1584 * Returns the number of bytes written
1586 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1587 struct lttng_consumer_stream
*stream
,
1588 const struct lttng_buffer_view
*buffer
,
1589 unsigned long padding
)
1592 off_t orig_offset
= stream
->out_fd_offset
;
1593 /* Default is on the disk */
1594 int outfd
= stream
->out_fd
;
1595 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1596 unsigned int relayd_hang_up
= 0;
1597 const size_t subbuf_content_size
= buffer
->size
- padding
;
1600 /* RCU lock for the relayd pointer */
1602 assert(stream
->net_seq_idx
!= (uint64_t) -1ULL ||
1603 stream
->trace_chunk
);
1605 /* Flag that the current stream if set for network streaming. */
1606 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1607 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1608 if (relayd
== NULL
) {
1614 /* Handle stream on the relayd if the output is on the network */
1616 unsigned long netlen
= subbuf_content_size
;
1619 * Lock the control socket for the complete duration of the function
1620 * since from this point on we will use the socket.
1622 if (stream
->metadata_flag
) {
1623 /* Metadata requires the control socket. */
1624 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1625 if (stream
->reset_metadata_flag
) {
1626 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1627 stream
->relayd_stream_id
,
1628 stream
->metadata_version
);
1633 stream
->reset_metadata_flag
= 0;
1635 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1638 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1643 /* Use the returned socket. */
1646 /* Write metadata stream id before payload */
1647 if (stream
->metadata_flag
) {
1648 ret
= write_relayd_metadata_id(outfd
, stream
, padding
);
1655 write_len
= subbuf_content_size
;
1657 /* No streaming; we have to write the full padding. */
1658 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1659 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1661 ERR("Reset metadata file");
1664 stream
->reset_metadata_flag
= 0;
1668 * Check if we need to change the tracefile before writing the packet.
1670 if (stream
->chan
->tracefile_size
> 0 &&
1671 (stream
->tracefile_size_current
+ buffer
->size
) >
1672 stream
->chan
->tracefile_size
) {
1673 ret
= consumer_stream_rotate_output_files(stream
);
1677 outfd
= stream
->out_fd
;
1680 stream
->tracefile_size_current
+= buffer
->size
;
1681 write_len
= buffer
->size
;
1685 * This call guarantee that len or less is returned. It's impossible to
1686 * receive a ret value that is bigger than len.
1688 ret
= lttng_write(outfd
, buffer
->data
, write_len
);
1689 DBG("Consumer mmap write() ret %zd (len %zu)", ret
, write_len
);
1690 if (ret
< 0 || ((size_t) ret
!= write_len
)) {
1692 * Report error to caller if nothing was written else at least send the
1700 /* Socket operation failed. We consider the relayd dead */
1701 if (errno
== EPIPE
) {
1703 * This is possible if the fd is closed on the other side
1704 * (outfd) or any write problem. It can be verbose a bit for a
1705 * normal execution if for instance the relayd is stopped
1706 * abruptly. This can happen so set this to a DBG statement.
1708 DBG("Consumer mmap write detected relayd hang up");
1710 /* Unhandled error, print it and stop function right now. */
1711 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret
,
1716 stream
->output_written
+= ret
;
1718 /* This call is useless on a socket so better save a syscall. */
1720 /* This won't block, but will start writeout asynchronously */
1721 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, write_len
,
1722 SYNC_FILE_RANGE_WRITE
);
1723 stream
->out_fd_offset
+= write_len
;
1724 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1729 * This is a special case that the relayd has closed its socket. Let's
1730 * cleanup the relayd object and all associated streams.
1732 if (relayd
&& relayd_hang_up
) {
1733 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1734 lttng_consumer_cleanup_relayd(relayd
);
1738 /* Unlock only if ctrl socket used */
1739 if (relayd
&& stream
->metadata_flag
) {
1740 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1748 * Splice the data from the ring buffer to the tracefile.
1750 * It must be called with the stream lock held.
1752 * Returns the number of bytes spliced.
1754 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1755 struct lttng_consumer_local_data
*ctx
,
1756 struct lttng_consumer_stream
*stream
, unsigned long len
,
1757 unsigned long padding
)
1759 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1761 off_t orig_offset
= stream
->out_fd_offset
;
1762 int fd
= stream
->wait_fd
;
1763 /* Default is on the disk */
1764 int outfd
= stream
->out_fd
;
1765 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1767 unsigned int relayd_hang_up
= 0;
1769 switch (consumer_data
.type
) {
1770 case LTTNG_CONSUMER_KERNEL
:
1772 case LTTNG_CONSUMER32_UST
:
1773 case LTTNG_CONSUMER64_UST
:
1774 /* Not supported for user space tracing */
1777 ERR("Unknown consumer_data type");
1781 /* RCU lock for the relayd pointer */
1784 /* Flag that the current stream if set for network streaming. */
1785 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1786 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1787 if (relayd
== NULL
) {
1792 splice_pipe
= stream
->splice_pipe
;
1794 /* Write metadata stream id before payload */
1796 unsigned long total_len
= len
;
1798 if (stream
->metadata_flag
) {
1800 * Lock the control socket for the complete duration of the function
1801 * since from this point on we will use the socket.
1803 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1805 if (stream
->reset_metadata_flag
) {
1806 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1807 stream
->relayd_stream_id
,
1808 stream
->metadata_version
);
1813 stream
->reset_metadata_flag
= 0;
1815 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
,
1823 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1826 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1832 /* Use the returned socket. */
1835 /* No streaming, we have to set the len with the full padding */
1838 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1839 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1841 ERR("Reset metadata file");
1844 stream
->reset_metadata_flag
= 0;
1847 * Check if we need to change the tracefile before writing the packet.
1849 if (stream
->chan
->tracefile_size
> 0 &&
1850 (stream
->tracefile_size_current
+ len
) >
1851 stream
->chan
->tracefile_size
) {
1852 ret
= consumer_stream_rotate_output_files(stream
);
1857 outfd
= stream
->out_fd
;
1860 stream
->tracefile_size_current
+= len
;
1864 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1865 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1866 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1867 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1868 DBG("splice chan to pipe, ret %zd", ret_splice
);
1869 if (ret_splice
< 0) {
1872 PERROR("Error in relay splice");
1876 /* Handle stream on the relayd if the output is on the network */
1877 if (relayd
&& stream
->metadata_flag
) {
1878 size_t metadata_payload_size
=
1879 sizeof(struct lttcomm_relayd_metadata_payload
);
1881 /* Update counter to fit the spliced data */
1882 ret_splice
+= metadata_payload_size
;
1883 len
+= metadata_payload_size
;
1885 * We do this so the return value can match the len passed as
1886 * argument to this function.
1888 written
-= metadata_payload_size
;
1891 /* Splice data out */
1892 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1893 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1894 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1896 if (ret_splice
< 0) {
1901 } else if (ret_splice
> len
) {
1903 * We don't expect this code path to be executed but you never know
1904 * so this is an extra protection agains a buggy splice().
1907 written
+= ret_splice
;
1908 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1912 /* All good, update current len and continue. */
1916 /* This call is useless on a socket so better save a syscall. */
1918 /* This won't block, but will start writeout asynchronously */
1919 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1920 SYNC_FILE_RANGE_WRITE
);
1921 stream
->out_fd_offset
+= ret_splice
;
1923 stream
->output_written
+= ret_splice
;
1924 written
+= ret_splice
;
1927 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1933 * This is a special case that the relayd has closed its socket. Let's
1934 * cleanup the relayd object and all associated streams.
1936 if (relayd
&& relayd_hang_up
) {
1937 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1938 lttng_consumer_cleanup_relayd(relayd
);
1939 /* Skip splice error so the consumer does not fail */
1944 /* send the appropriate error description to sessiond */
1947 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1950 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1953 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1958 if (relayd
&& stream
->metadata_flag
) {
1959 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1967 * Sample the snapshot positions for a specific fd
1969 * Returns 0 on success, < 0 on error
1971 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
1973 switch (consumer_data
.type
) {
1974 case LTTNG_CONSUMER_KERNEL
:
1975 return lttng_kconsumer_sample_snapshot_positions(stream
);
1976 case LTTNG_CONSUMER32_UST
:
1977 case LTTNG_CONSUMER64_UST
:
1978 return lttng_ustconsumer_sample_snapshot_positions(stream
);
1980 ERR("Unknown consumer_data type");
1986 * Take a snapshot for a specific fd
1988 * Returns 0 on success, < 0 on error
1990 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1992 switch (consumer_data
.type
) {
1993 case LTTNG_CONSUMER_KERNEL
:
1994 return lttng_kconsumer_take_snapshot(stream
);
1995 case LTTNG_CONSUMER32_UST
:
1996 case LTTNG_CONSUMER64_UST
:
1997 return lttng_ustconsumer_take_snapshot(stream
);
1999 ERR("Unknown consumer_data type");
2006 * Get the produced position
2008 * Returns 0 on success, < 0 on error
2010 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
2013 switch (consumer_data
.type
) {
2014 case LTTNG_CONSUMER_KERNEL
:
2015 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
2016 case LTTNG_CONSUMER32_UST
:
2017 case LTTNG_CONSUMER64_UST
:
2018 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2020 ERR("Unknown consumer_data type");
2027 * Get the consumed position (free-running counter position in bytes).
2029 * Returns 0 on success, < 0 on error
2031 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
2034 switch (consumer_data
.type
) {
2035 case LTTNG_CONSUMER_KERNEL
:
2036 return lttng_kconsumer_get_consumed_snapshot(stream
, pos
);
2037 case LTTNG_CONSUMER32_UST
:
2038 case LTTNG_CONSUMER64_UST
:
2039 return lttng_ustconsumer_get_consumed_snapshot(stream
, pos
);
2041 ERR("Unknown consumer_data type");
2047 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2048 int sock
, struct pollfd
*consumer_sockpoll
)
2050 switch (consumer_data
.type
) {
2051 case LTTNG_CONSUMER_KERNEL
:
2052 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2053 case LTTNG_CONSUMER32_UST
:
2054 case LTTNG_CONSUMER64_UST
:
2055 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2057 ERR("Unknown consumer_data type");
2063 void lttng_consumer_close_all_metadata(void)
2065 switch (consumer_data
.type
) {
2066 case LTTNG_CONSUMER_KERNEL
:
2068 * The Kernel consumer has a different metadata scheme so we don't
2069 * close anything because the stream will be closed by the session
2073 case LTTNG_CONSUMER32_UST
:
2074 case LTTNG_CONSUMER64_UST
:
2076 * Close all metadata streams. The metadata hash table is passed and
2077 * this call iterates over it by closing all wakeup fd. This is safe
2078 * because at this point we are sure that the metadata producer is
2079 * either dead or blocked.
2081 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2084 ERR("Unknown consumer_data type");
2090 * Clean up a metadata stream and free its memory.
2092 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2093 struct lttng_ht
*ht
)
2095 struct lttng_consumer_channel
*channel
= NULL
;
2096 bool free_channel
= false;
2100 * This call should NEVER receive regular stream. It must always be
2101 * metadata stream and this is crucial for data structure synchronization.
2103 assert(stream
->metadata_flag
);
2105 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2107 pthread_mutex_lock(&consumer_data
.lock
);
2109 * Note that this assumes that a stream's channel is never changed and
2110 * that the stream's lock doesn't need to be taken to sample its
2113 channel
= stream
->chan
;
2114 pthread_mutex_lock(&channel
->lock
);
2115 pthread_mutex_lock(&stream
->lock
);
2116 if (channel
->metadata_cache
) {
2117 /* Only applicable to userspace consumers. */
2118 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
2121 /* Remove any reference to that stream. */
2122 consumer_stream_delete(stream
, ht
);
2124 /* Close down everything including the relayd if one. */
2125 consumer_stream_close(stream
);
2126 /* Destroy tracer buffers of the stream. */
2127 consumer_stream_destroy_buffers(stream
);
2129 /* Atomically decrement channel refcount since other threads can use it. */
2130 if (!uatomic_sub_return(&channel
->refcount
, 1)
2131 && !uatomic_read(&channel
->nb_init_stream_left
)) {
2132 /* Go for channel deletion! */
2133 free_channel
= true;
2135 stream
->chan
= NULL
;
2138 * Nullify the stream reference so it is not used after deletion. The
2139 * channel lock MUST be acquired before being able to check for a NULL
2142 channel
->metadata_stream
= NULL
;
2144 if (channel
->metadata_cache
) {
2145 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
2147 pthread_mutex_unlock(&stream
->lock
);
2148 pthread_mutex_unlock(&channel
->lock
);
2149 pthread_mutex_unlock(&consumer_data
.lock
);
2152 consumer_del_channel(channel
);
2155 lttng_trace_chunk_put(stream
->trace_chunk
);
2156 stream
->trace_chunk
= NULL
;
2157 consumer_stream_free(stream
);
2161 * Action done with the metadata stream when adding it to the consumer internal
2162 * data structures to handle it.
2164 void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2166 struct lttng_ht
*ht
= metadata_ht
;
2167 struct lttng_ht_iter iter
;
2168 struct lttng_ht_node_u64
*node
;
2173 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2175 pthread_mutex_lock(&consumer_data
.lock
);
2176 pthread_mutex_lock(&stream
->chan
->lock
);
2177 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2178 pthread_mutex_lock(&stream
->lock
);
2181 * From here, refcounts are updated so be _careful_ when returning an error
2188 * Lookup the stream just to make sure it does not exist in our internal
2189 * state. This should NEVER happen.
2191 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2192 node
= lttng_ht_iter_get_node_u64(&iter
);
2196 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2197 * in terms of destroying the associated channel, because the action that
2198 * causes the count to become 0 also causes a stream to be added. The
2199 * channel deletion will thus be triggered by the following removal of this
2202 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2203 /* Increment refcount before decrementing nb_init_stream_left */
2205 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2208 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2210 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
2211 &stream
->node_channel_id
);
2214 * Add stream to the stream_list_ht of the consumer data. No need to steal
2215 * the key since the HT does not use it and we allow to add redundant keys
2218 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2222 pthread_mutex_unlock(&stream
->lock
);
2223 pthread_mutex_unlock(&stream
->chan
->lock
);
2224 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2225 pthread_mutex_unlock(&consumer_data
.lock
);
2229 * Delete data stream that are flagged for deletion (endpoint_status).
2231 static void validate_endpoint_status_data_stream(void)
2233 struct lttng_ht_iter iter
;
2234 struct lttng_consumer_stream
*stream
;
2236 DBG("Consumer delete flagged data stream");
2239 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2240 /* Validate delete flag of the stream */
2241 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2244 /* Delete it right now */
2245 consumer_del_stream(stream
, data_ht
);
2251 * Delete metadata stream that are flagged for deletion (endpoint_status).
2253 static void validate_endpoint_status_metadata_stream(
2254 struct lttng_poll_event
*pollset
)
2256 struct lttng_ht_iter iter
;
2257 struct lttng_consumer_stream
*stream
;
2259 DBG("Consumer delete flagged metadata stream");
2264 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2265 /* Validate delete flag of the stream */
2266 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2270 * Remove from pollset so the metadata thread can continue without
2271 * blocking on a deleted stream.
2273 lttng_poll_del(pollset
, stream
->wait_fd
);
2275 /* Delete it right now */
2276 consumer_del_metadata_stream(stream
, metadata_ht
);
2282 * Thread polls on metadata file descriptor and write them on disk or on the
2285 void *consumer_thread_metadata_poll(void *data
)
2287 int ret
, i
, pollfd
, err
= -1;
2288 uint32_t revents
, nb_fd
;
2289 struct lttng_consumer_stream
*stream
= NULL
;
2290 struct lttng_ht_iter iter
;
2291 struct lttng_ht_node_u64
*node
;
2292 struct lttng_poll_event events
;
2293 struct lttng_consumer_local_data
*ctx
= data
;
2296 rcu_register_thread();
2298 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2300 if (testpoint(consumerd_thread_metadata
)) {
2301 goto error_testpoint
;
2304 health_code_update();
2306 DBG("Thread metadata poll started");
2308 /* Size is set to 1 for the consumer_metadata pipe */
2309 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2311 ERR("Poll set creation failed");
2315 ret
= lttng_poll_add(&events
,
2316 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2322 DBG("Metadata main loop started");
2326 health_code_update();
2327 health_poll_entry();
2328 DBG("Metadata poll wait");
2329 ret
= lttng_poll_wait(&events
, -1);
2330 DBG("Metadata poll return from wait with %d fd(s)",
2331 LTTNG_POLL_GETNB(&events
));
2333 DBG("Metadata event caught in thread");
2335 if (errno
== EINTR
) {
2336 ERR("Poll EINTR caught");
2339 if (LTTNG_POLL_GETNB(&events
) == 0) {
2340 err
= 0; /* All is OK */
2347 /* From here, the event is a metadata wait fd */
2348 for (i
= 0; i
< nb_fd
; i
++) {
2349 health_code_update();
2351 revents
= LTTNG_POLL_GETEV(&events
, i
);
2352 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2354 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2355 if (revents
& LPOLLIN
) {
2358 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2359 &stream
, sizeof(stream
));
2360 if (pipe_len
< sizeof(stream
)) {
2362 PERROR("read metadata stream");
2365 * Remove the pipe from the poll set and continue the loop
2366 * since their might be data to consume.
2368 lttng_poll_del(&events
,
2369 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2370 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2374 /* A NULL stream means that the state has changed. */
2375 if (stream
== NULL
) {
2376 /* Check for deleted streams. */
2377 validate_endpoint_status_metadata_stream(&events
);
2381 DBG("Adding metadata stream %d to poll set",
2384 /* Add metadata stream to the global poll events list */
2385 lttng_poll_add(&events
, stream
->wait_fd
,
2386 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2387 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2388 DBG("Metadata thread pipe hung up");
2390 * Remove the pipe from the poll set and continue the loop
2391 * since their might be data to consume.
2393 lttng_poll_del(&events
,
2394 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2395 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2398 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2402 /* Handle other stream */
2408 uint64_t tmp_id
= (uint64_t) pollfd
;
2410 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2412 node
= lttng_ht_iter_get_node_u64(&iter
);
2415 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2418 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2419 /* Get the data out of the metadata file descriptor */
2420 DBG("Metadata available on fd %d", pollfd
);
2421 assert(stream
->wait_fd
== pollfd
);
2424 health_code_update();
2426 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2428 * We don't check the return value here since if we get
2429 * a negative len, it means an error occurred thus we
2430 * simply remove it from the poll set and free the
2435 /* It's ok to have an unavailable sub-buffer */
2436 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2437 /* Clean up stream from consumer and free it. */
2438 lttng_poll_del(&events
, stream
->wait_fd
);
2439 consumer_del_metadata_stream(stream
, metadata_ht
);
2441 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2442 DBG("Metadata fd %d is hup|err.", pollfd
);
2443 if (!stream
->hangup_flush_done
2444 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2445 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2446 DBG("Attempting to flush and consume the UST buffers");
2447 lttng_ustconsumer_on_stream_hangup(stream
);
2449 /* We just flushed the stream now read it. */
2451 health_code_update();
2453 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2455 * We don't check the return value here since if we get
2456 * a negative len, it means an error occurred thus we
2457 * simply remove it from the poll set and free the
2463 lttng_poll_del(&events
, stream
->wait_fd
);
2465 * This call update the channel states, closes file descriptors
2466 * and securely free the stream.
2468 consumer_del_metadata_stream(stream
, metadata_ht
);
2470 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2474 /* Release RCU lock for the stream looked up */
2482 DBG("Metadata poll thread exiting");
2484 lttng_poll_clean(&events
);
2489 ERR("Health error occurred in %s", __func__
);
2491 health_unregister(health_consumerd
);
2492 rcu_unregister_thread();
2497 * This thread polls the fds in the set to consume the data and write
2498 * it to tracefile if necessary.
2500 void *consumer_thread_data_poll(void *data
)
2502 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2503 struct pollfd
*pollfd
= NULL
;
2504 /* local view of the streams */
2505 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2506 /* local view of consumer_data.fds_count */
2508 /* 2 for the consumer_data_pipe and wake up pipe */
2509 const int nb_pipes_fd
= 2;
2510 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2511 int nb_inactive_fd
= 0;
2512 struct lttng_consumer_local_data
*ctx
= data
;
2515 rcu_register_thread();
2517 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2519 if (testpoint(consumerd_thread_data
)) {
2520 goto error_testpoint
;
2523 health_code_update();
2525 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2526 if (local_stream
== NULL
) {
2527 PERROR("local_stream malloc");
2532 health_code_update();
2538 * the fds set has been updated, we need to update our
2539 * local array as well
2541 pthread_mutex_lock(&consumer_data
.lock
);
2542 if (consumer_data
.need_update
) {
2547 local_stream
= NULL
;
2549 /* Allocate for all fds */
2550 pollfd
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) * sizeof(struct pollfd
));
2551 if (pollfd
== NULL
) {
2552 PERROR("pollfd malloc");
2553 pthread_mutex_unlock(&consumer_data
.lock
);
2557 local_stream
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) *
2558 sizeof(struct lttng_consumer_stream
*));
2559 if (local_stream
== NULL
) {
2560 PERROR("local_stream malloc");
2561 pthread_mutex_unlock(&consumer_data
.lock
);
2564 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2565 data_ht
, &nb_inactive_fd
);
2567 ERR("Error in allocating pollfd or local_outfds");
2568 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2569 pthread_mutex_unlock(&consumer_data
.lock
);
2573 consumer_data
.need_update
= 0;
2575 pthread_mutex_unlock(&consumer_data
.lock
);
2577 /* No FDs and consumer_quit, consumer_cleanup the thread */
2578 if (nb_fd
== 0 && nb_inactive_fd
== 0 &&
2579 CMM_LOAD_SHARED(consumer_quit
) == 1) {
2580 err
= 0; /* All is OK */
2583 /* poll on the array of fds */
2585 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2586 if (testpoint(consumerd_thread_data_poll
)) {
2589 health_poll_entry();
2590 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2592 DBG("poll num_rdy : %d", num_rdy
);
2593 if (num_rdy
== -1) {
2595 * Restart interrupted system call.
2597 if (errno
== EINTR
) {
2600 PERROR("Poll error");
2601 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2603 } else if (num_rdy
== 0) {
2604 DBG("Polling thread timed out");
2608 if (caa_unlikely(data_consumption_paused
)) {
2609 DBG("Data consumption paused, sleeping...");
2615 * If the consumer_data_pipe triggered poll go directly to the
2616 * beginning of the loop to update the array. We want to prioritize
2617 * array update over low-priority reads.
2619 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2620 ssize_t pipe_readlen
;
2622 DBG("consumer_data_pipe wake up");
2623 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2624 &new_stream
, sizeof(new_stream
));
2625 if (pipe_readlen
< sizeof(new_stream
)) {
2626 PERROR("Consumer data pipe");
2627 /* Continue so we can at least handle the current stream(s). */
2632 * If the stream is NULL, just ignore it. It's also possible that
2633 * the sessiond poll thread changed the consumer_quit state and is
2634 * waking us up to test it.
2636 if (new_stream
== NULL
) {
2637 validate_endpoint_status_data_stream();
2641 /* Continue to update the local streams and handle prio ones */
2645 /* Handle wakeup pipe. */
2646 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2648 ssize_t pipe_readlen
;
2650 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2652 if (pipe_readlen
< 0) {
2653 PERROR("Consumer data wakeup pipe");
2655 /* We've been awakened to handle stream(s). */
2656 ctx
->has_wakeup
= 0;
2659 /* Take care of high priority channels first. */
2660 for (i
= 0; i
< nb_fd
; i
++) {
2661 health_code_update();
2663 if (local_stream
[i
] == NULL
) {
2666 if (pollfd
[i
].revents
& POLLPRI
) {
2667 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2669 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2670 /* it's ok to have an unavailable sub-buffer */
2671 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2672 /* Clean the stream and free it. */
2673 consumer_del_stream(local_stream
[i
], data_ht
);
2674 local_stream
[i
] = NULL
;
2675 } else if (len
> 0) {
2676 local_stream
[i
]->data_read
= 1;
2682 * If we read high prio channel in this loop, try again
2683 * for more high prio data.
2689 /* Take care of low priority channels. */
2690 for (i
= 0; i
< nb_fd
; i
++) {
2691 health_code_update();
2693 if (local_stream
[i
] == NULL
) {
2696 if ((pollfd
[i
].revents
& POLLIN
) ||
2697 local_stream
[i
]->hangup_flush_done
||
2698 local_stream
[i
]->has_data
) {
2699 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2700 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2701 /* it's ok to have an unavailable sub-buffer */
2702 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2703 /* Clean the stream and free it. */
2704 consumer_del_stream(local_stream
[i
], data_ht
);
2705 local_stream
[i
] = NULL
;
2706 } else if (len
> 0) {
2707 local_stream
[i
]->data_read
= 1;
2712 /* Handle hangup and errors */
2713 for (i
= 0; i
< nb_fd
; i
++) {
2714 health_code_update();
2716 if (local_stream
[i
] == NULL
) {
2719 if (!local_stream
[i
]->hangup_flush_done
2720 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2721 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2722 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2723 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2725 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2726 /* Attempt read again, for the data we just flushed. */
2727 local_stream
[i
]->data_read
= 1;
2730 * If the poll flag is HUP/ERR/NVAL and we have
2731 * read no data in this pass, we can remove the
2732 * stream from its hash table.
2734 if ((pollfd
[i
].revents
& POLLHUP
)) {
2735 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2736 if (!local_stream
[i
]->data_read
) {
2737 consumer_del_stream(local_stream
[i
], data_ht
);
2738 local_stream
[i
] = NULL
;
2741 } else if (pollfd
[i
].revents
& POLLERR
) {
2742 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2743 if (!local_stream
[i
]->data_read
) {
2744 consumer_del_stream(local_stream
[i
], data_ht
);
2745 local_stream
[i
] = NULL
;
2748 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2749 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2750 if (!local_stream
[i
]->data_read
) {
2751 consumer_del_stream(local_stream
[i
], data_ht
);
2752 local_stream
[i
] = NULL
;
2756 if (local_stream
[i
] != NULL
) {
2757 local_stream
[i
]->data_read
= 0;
2764 DBG("polling thread exiting");
2769 * Close the write side of the pipe so epoll_wait() in
2770 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2771 * read side of the pipe. If we close them both, epoll_wait strangely does
2772 * not return and could create a endless wait period if the pipe is the
2773 * only tracked fd in the poll set. The thread will take care of closing
2776 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2781 ERR("Health error occurred in %s", __func__
);
2783 health_unregister(health_consumerd
);
2785 rcu_unregister_thread();
2790 * Close wake-up end of each stream belonging to the channel. This will
2791 * allow the poll() on the stream read-side to detect when the
2792 * write-side (application) finally closes them.
2795 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2797 struct lttng_ht
*ht
;
2798 struct lttng_consumer_stream
*stream
;
2799 struct lttng_ht_iter iter
;
2801 ht
= consumer_data
.stream_per_chan_id_ht
;
2804 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2805 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2806 ht
->match_fct
, &channel
->key
,
2807 &iter
.iter
, stream
, node_channel_id
.node
) {
2809 * Protect against teardown with mutex.
2811 pthread_mutex_lock(&stream
->lock
);
2812 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2815 switch (consumer_data
.type
) {
2816 case LTTNG_CONSUMER_KERNEL
:
2818 case LTTNG_CONSUMER32_UST
:
2819 case LTTNG_CONSUMER64_UST
:
2820 if (stream
->metadata_flag
) {
2821 /* Safe and protected by the stream lock. */
2822 lttng_ustconsumer_close_metadata(stream
->chan
);
2825 * Note: a mutex is taken internally within
2826 * liblttng-ust-ctl to protect timer wakeup_fd
2827 * use from concurrent close.
2829 lttng_ustconsumer_close_stream_wakeup(stream
);
2833 ERR("Unknown consumer_data type");
2837 pthread_mutex_unlock(&stream
->lock
);
2842 static void destroy_channel_ht(struct lttng_ht
*ht
)
2844 struct lttng_ht_iter iter
;
2845 struct lttng_consumer_channel
*channel
;
2853 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2854 ret
= lttng_ht_del(ht
, &iter
);
2859 lttng_ht_destroy(ht
);
2863 * This thread polls the channel fds to detect when they are being
2864 * closed. It closes all related streams if the channel is detected as
2865 * closed. It is currently only used as a shim layer for UST because the
2866 * consumerd needs to keep the per-stream wakeup end of pipes open for
2869 void *consumer_thread_channel_poll(void *data
)
2871 int ret
, i
, pollfd
, err
= -1;
2872 uint32_t revents
, nb_fd
;
2873 struct lttng_consumer_channel
*chan
= NULL
;
2874 struct lttng_ht_iter iter
;
2875 struct lttng_ht_node_u64
*node
;
2876 struct lttng_poll_event events
;
2877 struct lttng_consumer_local_data
*ctx
= data
;
2878 struct lttng_ht
*channel_ht
;
2880 rcu_register_thread();
2882 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2884 if (testpoint(consumerd_thread_channel
)) {
2885 goto error_testpoint
;
2888 health_code_update();
2890 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2892 /* ENOMEM at this point. Better to bail out. */
2896 DBG("Thread channel poll started");
2898 /* Size is set to 1 for the consumer_channel pipe */
2899 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2901 ERR("Poll set creation failed");
2905 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2911 DBG("Channel main loop started");
2915 health_code_update();
2916 DBG("Channel poll wait");
2917 health_poll_entry();
2918 ret
= lttng_poll_wait(&events
, -1);
2919 DBG("Channel poll return from wait with %d fd(s)",
2920 LTTNG_POLL_GETNB(&events
));
2922 DBG("Channel event caught in thread");
2924 if (errno
== EINTR
) {
2925 ERR("Poll EINTR caught");
2928 if (LTTNG_POLL_GETNB(&events
) == 0) {
2929 err
= 0; /* All is OK */
2936 /* From here, the event is a channel wait fd */
2937 for (i
= 0; i
< nb_fd
; i
++) {
2938 health_code_update();
2940 revents
= LTTNG_POLL_GETEV(&events
, i
);
2941 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2943 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2944 if (revents
& LPOLLIN
) {
2945 enum consumer_channel_action action
;
2948 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2951 ERR("Error reading channel pipe");
2953 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2958 case CONSUMER_CHANNEL_ADD
:
2959 DBG("Adding channel %d to poll set",
2962 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2965 lttng_ht_add_unique_u64(channel_ht
,
2966 &chan
->wait_fd_node
);
2968 /* Add channel to the global poll events list */
2969 lttng_poll_add(&events
, chan
->wait_fd
,
2970 LPOLLERR
| LPOLLHUP
);
2972 case CONSUMER_CHANNEL_DEL
:
2975 * This command should never be called if the channel
2976 * has streams monitored by either the data or metadata
2977 * thread. The consumer only notify this thread with a
2978 * channel del. command if it receives a destroy
2979 * channel command from the session daemon that send it
2980 * if a command prior to the GET_CHANNEL failed.
2984 chan
= consumer_find_channel(key
);
2987 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2990 lttng_poll_del(&events
, chan
->wait_fd
);
2991 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2992 ret
= lttng_ht_del(channel_ht
, &iter
);
2995 switch (consumer_data
.type
) {
2996 case LTTNG_CONSUMER_KERNEL
:
2998 case LTTNG_CONSUMER32_UST
:
2999 case LTTNG_CONSUMER64_UST
:
3000 health_code_update();
3001 /* Destroy streams that might have been left in the stream list. */
3002 clean_channel_stream_list(chan
);
3005 ERR("Unknown consumer_data type");
3010 * Release our own refcount. Force channel deletion even if
3011 * streams were not initialized.
3013 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3014 consumer_del_channel(chan
);
3019 case CONSUMER_CHANNEL_QUIT
:
3021 * Remove the pipe from the poll set and continue the loop
3022 * since their might be data to consume.
3024 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3027 ERR("Unknown action");
3030 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3031 DBG("Channel thread pipe hung up");
3033 * Remove the pipe from the poll set and continue the loop
3034 * since their might be data to consume.
3036 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3039 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3043 /* Handle other stream */
3049 uint64_t tmp_id
= (uint64_t) pollfd
;
3051 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3053 node
= lttng_ht_iter_get_node_u64(&iter
);
3056 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
3059 /* Check for error event */
3060 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3061 DBG("Channel fd %d is hup|err.", pollfd
);
3063 lttng_poll_del(&events
, chan
->wait_fd
);
3064 ret
= lttng_ht_del(channel_ht
, &iter
);
3068 * This will close the wait fd for each stream associated to
3069 * this channel AND monitored by the data/metadata thread thus
3070 * will be clean by the right thread.
3072 consumer_close_channel_streams(chan
);
3074 /* Release our own refcount */
3075 if (!uatomic_sub_return(&chan
->refcount
, 1)
3076 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3077 consumer_del_channel(chan
);
3080 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3085 /* Release RCU lock for the channel looked up */
3093 lttng_poll_clean(&events
);
3095 destroy_channel_ht(channel_ht
);
3098 DBG("Channel poll thread exiting");
3101 ERR("Health error occurred in %s", __func__
);
3103 health_unregister(health_consumerd
);
3104 rcu_unregister_thread();
3108 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3109 struct pollfd
*sockpoll
, int client_socket
)
3116 ret
= lttng_consumer_poll_socket(sockpoll
);
3120 DBG("Metadata connection on client_socket");
3122 /* Blocking call, waiting for transmission */
3123 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3124 if (ctx
->consumer_metadata_socket
< 0) {
3125 WARN("On accept metadata");
3136 * This thread listens on the consumerd socket and receives the file
3137 * descriptors from the session daemon.
3139 void *consumer_thread_sessiond_poll(void *data
)
3141 int sock
= -1, client_socket
, ret
, err
= -1;
3143 * structure to poll for incoming data on communication socket avoids
3144 * making blocking sockets.
3146 struct pollfd consumer_sockpoll
[2];
3147 struct lttng_consumer_local_data
*ctx
= data
;
3149 rcu_register_thread();
3151 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3153 if (testpoint(consumerd_thread_sessiond
)) {
3154 goto error_testpoint
;
3157 health_code_update();
3159 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3160 unlink(ctx
->consumer_command_sock_path
);
3161 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3162 if (client_socket
< 0) {
3163 ERR("Cannot create command socket");
3167 ret
= lttcomm_listen_unix_sock(client_socket
);
3172 DBG("Sending ready command to lttng-sessiond");
3173 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3174 /* return < 0 on error, but == 0 is not fatal */
3176 ERR("Error sending ready command to lttng-sessiond");
3180 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3181 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3182 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3183 consumer_sockpoll
[1].fd
= client_socket
;
3184 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3186 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3194 DBG("Connection on client_socket");
3196 /* Blocking call, waiting for transmission */
3197 sock
= lttcomm_accept_unix_sock(client_socket
);
3204 * Setup metadata socket which is the second socket connection on the
3205 * command unix socket.
3207 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3216 /* This socket is not useful anymore. */
3217 ret
= close(client_socket
);
3219 PERROR("close client_socket");
3223 /* update the polling structure to poll on the established socket */
3224 consumer_sockpoll
[1].fd
= sock
;
3225 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3228 health_code_update();
3230 health_poll_entry();
3231 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3240 DBG("Incoming command on sock");
3241 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3244 * This could simply be a session daemon quitting. Don't output
3247 DBG("Communication interrupted on command socket");
3251 if (CMM_LOAD_SHARED(consumer_quit
)) {
3252 DBG("consumer_thread_receive_fds received quit from signal");
3253 err
= 0; /* All is OK */
3256 DBG("received command on sock");
3262 DBG("Consumer thread sessiond poll exiting");
3265 * Close metadata streams since the producer is the session daemon which
3268 * NOTE: for now, this only applies to the UST tracer.
3270 lttng_consumer_close_all_metadata();
3273 * when all fds have hung up, the polling thread
3276 CMM_STORE_SHARED(consumer_quit
, 1);
3279 * Notify the data poll thread to poll back again and test the
3280 * consumer_quit state that we just set so to quit gracefully.
3282 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3284 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3286 notify_health_quit_pipe(health_quit_pipe
);
3288 /* Cleaning up possibly open sockets. */
3292 PERROR("close sock sessiond poll");
3295 if (client_socket
>= 0) {
3296 ret
= close(client_socket
);
3298 PERROR("close client_socket sessiond poll");
3305 ERR("Health error occurred in %s", __func__
);
3307 health_unregister(health_consumerd
);
3309 rcu_unregister_thread();
3313 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3314 struct lttng_consumer_local_data
*ctx
,
3315 bool locked_by_caller
)
3317 ssize_t ret
, written_bytes
;
3319 struct stream_subbuffer subbuffer
= {};
3321 if (!locked_by_caller
) {
3322 stream
->read_subbuffer_ops
.lock(stream
);
3325 if (stream
->read_subbuffer_ops
.on_wake_up
) {
3326 ret
= stream
->read_subbuffer_ops
.on_wake_up(stream
);
3333 * If the stream was flagged to be ready for rotation before we extract
3334 * the next packet, rotate it now.
3336 if (stream
->rotate_ready
) {
3337 DBG("Rotate stream before consuming data");
3338 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3340 ERR("Stream rotation error before consuming data");
3345 ret
= stream
->read_subbuffer_ops
.get_next_subbuffer(stream
, &subbuffer
);
3347 if (ret
== -ENODATA
) {
3354 ret
= stream
->read_subbuffer_ops
.pre_consume_subbuffer(
3355 stream
, &subbuffer
);
3357 goto error_put_subbuf
;
3360 written_bytes
= stream
->read_subbuffer_ops
.consume_subbuffer(
3361 ctx
, stream
, &subbuffer
);
3363 * Should write subbuf_size amount of data when network streaming or
3364 * the full padded size when we are not streaming.
3366 if ((written_bytes
!= subbuffer
.info
.data
.subbuf_size
&&
3367 stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
3368 (written_bytes
!= subbuffer
.info
.data
.padded_subbuf_size
&&
3369 stream
->net_seq_idx
==
3370 (uint64_t) -1ULL)) {
3372 * Display the error but continue processing to try to
3373 * release the subbuffer. This is a DBG statement
3374 * since this can happen without being a critical
3377 DBG("Failed to write to tracefile (written_bytes: %zd != padded subbuffer size: %lu, subbuffer size: %lu)",
3378 written_bytes
, subbuffer
.info
.data
.subbuf_size
,
3379 subbuffer
.info
.data
.padded_subbuf_size
);
3382 ret
= stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3387 if (stream
->read_subbuffer_ops
.post_consume
) {
3388 ret
= stream
->read_subbuffer_ops
.post_consume(stream
, &subbuffer
, ctx
);
3395 * After extracting the packet, we check if the stream is now ready to
3396 * be rotated and perform the action immediately.
3398 * Don't overwrite `ret` as callers expect the number of bytes
3399 * consumed to be returned on success.
3401 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
3402 if (rotation_ret
== 1) {
3403 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3404 if (rotation_ret
< 0) {
3406 ERR("Stream rotation error after consuming data");
3409 } else if (rotation_ret
< 0) {
3411 ERR("Failed to check if stream was ready to rotate after consuming data");
3415 if (stream
->read_subbuffer_ops
.on_sleep
) {
3416 stream
->read_subbuffer_ops
.on_sleep(stream
, ctx
);
3419 ret
= written_bytes
;
3421 if (!locked_by_caller
) {
3422 stream
->read_subbuffer_ops
.unlock(stream
);
3427 (void) stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3431 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3433 switch (consumer_data
.type
) {
3434 case LTTNG_CONSUMER_KERNEL
:
3435 return lttng_kconsumer_on_recv_stream(stream
);
3436 case LTTNG_CONSUMER32_UST
:
3437 case LTTNG_CONSUMER64_UST
:
3438 return lttng_ustconsumer_on_recv_stream(stream
);
3440 ERR("Unknown consumer_data type");
3447 * Allocate and set consumer data hash tables.
3449 int lttng_consumer_init(void)
3451 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3452 if (!consumer_data
.channel_ht
) {
3456 consumer_data
.channels_by_session_id_ht
=
3457 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3458 if (!consumer_data
.channels_by_session_id_ht
) {
3462 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3463 if (!consumer_data
.relayd_ht
) {
3467 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3468 if (!consumer_data
.stream_list_ht
) {
3472 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3473 if (!consumer_data
.stream_per_chan_id_ht
) {
3477 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3482 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3487 consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3488 if (!consumer_data
.chunk_registry
) {
3499 * Process the ADD_RELAYD command receive by a consumer.
3501 * This will create a relayd socket pair and add it to the relayd hash table.
3502 * The caller MUST acquire a RCU read side lock before calling it.
3504 void consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3505 struct lttng_consumer_local_data
*ctx
, int sock
,
3506 struct pollfd
*consumer_sockpoll
,
3507 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3508 uint64_t relayd_session_id
)
3510 int fd
= -1, ret
= -1, relayd_created
= 0;
3511 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3512 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3515 assert(relayd_sock
);
3517 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3519 /* Get relayd reference if exists. */
3520 relayd
= consumer_find_relayd(net_seq_idx
);
3521 if (relayd
== NULL
) {
3522 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3523 /* Not found. Allocate one. */
3524 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3525 if (relayd
== NULL
) {
3526 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3529 relayd
->sessiond_session_id
= sessiond_id
;
3534 * This code path MUST continue to the consumer send status message to
3535 * we can notify the session daemon and continue our work without
3536 * killing everything.
3540 * relayd key should never be found for control socket.
3542 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3545 /* First send a status message before receiving the fds. */
3546 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3548 /* Somehow, the session daemon is not responding anymore. */
3549 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3550 goto error_nosignal
;
3553 /* Poll on consumer socket. */
3554 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3556 /* Needing to exit in the middle of a command: error. */
3557 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3558 goto error_nosignal
;
3561 /* Get relayd socket from session daemon */
3562 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3563 if (ret
!= sizeof(fd
)) {
3564 fd
= -1; /* Just in case it gets set with an invalid value. */
3567 * Failing to receive FDs might indicate a major problem such as
3568 * reaching a fd limit during the receive where the kernel returns a
3569 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3570 * don't take any chances and stop everything.
3572 * XXX: Feature request #558 will fix that and avoid this possible
3573 * issue when reaching the fd limit.
3575 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3576 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3580 /* Copy socket information and received FD */
3581 switch (sock_type
) {
3582 case LTTNG_STREAM_CONTROL
:
3583 /* Copy received lttcomm socket */
3584 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3585 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3586 /* Handle create_sock error. */
3588 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3592 * Close the socket created internally by
3593 * lttcomm_create_sock, so we can replace it by the one
3594 * received from sessiond.
3596 if (close(relayd
->control_sock
.sock
.fd
)) {
3600 /* Assign new file descriptor */
3601 relayd
->control_sock
.sock
.fd
= fd
;
3602 /* Assign version values. */
3603 relayd
->control_sock
.major
= relayd_sock
->major
;
3604 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3606 relayd
->relayd_session_id
= relayd_session_id
;
3609 case LTTNG_STREAM_DATA
:
3610 /* Copy received lttcomm socket */
3611 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3612 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3613 /* Handle create_sock error. */
3615 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3619 * Close the socket created internally by
3620 * lttcomm_create_sock, so we can replace it by the one
3621 * received from sessiond.
3623 if (close(relayd
->data_sock
.sock
.fd
)) {
3627 /* Assign new file descriptor */
3628 relayd
->data_sock
.sock
.fd
= fd
;
3629 /* Assign version values. */
3630 relayd
->data_sock
.major
= relayd_sock
->major
;
3631 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3634 ERR("Unknown relayd socket type (%d)", sock_type
);
3635 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3639 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3640 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3641 relayd
->net_seq_idx
, fd
);
3643 * We gave the ownership of the fd to the relayd structure. Set the
3644 * fd to -1 so we don't call close() on it in the error path below.
3648 /* We successfully added the socket. Send status back. */
3649 ret
= consumer_send_status_msg(sock
, ret_code
);
3651 /* Somehow, the session daemon is not responding anymore. */
3652 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3653 goto error_nosignal
;
3657 * Add relayd socket pair to consumer data hashtable. If object already
3658 * exists or on error, the function gracefully returns.
3667 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3668 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3672 /* Close received socket if valid. */
3675 PERROR("close received socket");
3679 if (relayd_created
) {
3685 * Search for a relayd associated to the session id and return the reference.
3687 * A rcu read side lock MUST be acquire before calling this function and locked
3688 * until the relayd object is no longer necessary.
3690 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3692 struct lttng_ht_iter iter
;
3693 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3695 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3696 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3699 * Check by sessiond id which is unique here where the relayd session
3700 * id might not be when having multiple relayd.
3702 if (relayd
->sessiond_session_id
== id
) {
3703 /* Found the relayd. There can be only one per id. */
3715 * Check if for a given session id there is still data needed to be extract
3718 * Return 1 if data is pending or else 0 meaning ready to be read.
3720 int consumer_data_pending(uint64_t id
)
3723 struct lttng_ht_iter iter
;
3724 struct lttng_ht
*ht
;
3725 struct lttng_consumer_stream
*stream
;
3726 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3727 int (*data_pending
)(struct lttng_consumer_stream
*);
3729 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3732 pthread_mutex_lock(&consumer_data
.lock
);
3734 switch (consumer_data
.type
) {
3735 case LTTNG_CONSUMER_KERNEL
:
3736 data_pending
= lttng_kconsumer_data_pending
;
3738 case LTTNG_CONSUMER32_UST
:
3739 case LTTNG_CONSUMER64_UST
:
3740 data_pending
= lttng_ustconsumer_data_pending
;
3743 ERR("Unknown consumer data type");
3747 /* Ease our life a bit */
3748 ht
= consumer_data
.stream_list_ht
;
3750 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3751 ht
->hash_fct(&id
, lttng_ht_seed
),
3753 &iter
.iter
, stream
, node_session_id
.node
) {
3754 pthread_mutex_lock(&stream
->lock
);
3757 * A removed node from the hash table indicates that the stream has
3758 * been deleted thus having a guarantee that the buffers are closed
3759 * on the consumer side. However, data can still be transmitted
3760 * over the network so don't skip the relayd check.
3762 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3764 /* Check the stream if there is data in the buffers. */
3765 ret
= data_pending(stream
);
3767 pthread_mutex_unlock(&stream
->lock
);
3772 pthread_mutex_unlock(&stream
->lock
);
3775 relayd
= find_relayd_by_session_id(id
);
3777 unsigned int is_data_inflight
= 0;
3779 /* Send init command for data pending. */
3780 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3781 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3782 relayd
->relayd_session_id
);
3784 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3785 /* Communication error thus the relayd so no data pending. */
3786 goto data_not_pending
;
3789 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3790 ht
->hash_fct(&id
, lttng_ht_seed
),
3792 &iter
.iter
, stream
, node_session_id
.node
) {
3793 if (stream
->metadata_flag
) {
3794 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3795 stream
->relayd_stream_id
);
3797 ret
= relayd_data_pending(&relayd
->control_sock
,
3798 stream
->relayd_stream_id
,
3799 stream
->next_net_seq_num
- 1);
3803 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3805 } else if (ret
< 0) {
3806 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3807 lttng_consumer_cleanup_relayd(relayd
);
3808 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3809 goto data_not_pending
;
3813 /* Send end command for data pending. */
3814 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3815 relayd
->relayd_session_id
, &is_data_inflight
);
3816 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3818 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3819 lttng_consumer_cleanup_relayd(relayd
);
3820 goto data_not_pending
;
3822 if (is_data_inflight
) {
3828 * Finding _no_ node in the hash table and no inflight data means that the
3829 * stream(s) have been removed thus data is guaranteed to be available for
3830 * analysis from the trace files.
3834 /* Data is available to be read by a viewer. */
3835 pthread_mutex_unlock(&consumer_data
.lock
);
3840 /* Data is still being extracted from buffers. */
3841 pthread_mutex_unlock(&consumer_data
.lock
);
3847 * Send a ret code status message to the sessiond daemon.
3849 * Return the sendmsg() return value.
3851 int consumer_send_status_msg(int sock
, int ret_code
)
3853 struct lttcomm_consumer_status_msg msg
;
3855 memset(&msg
, 0, sizeof(msg
));
3856 msg
.ret_code
= ret_code
;
3858 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3862 * Send a channel status message to the sessiond daemon.
3864 * Return the sendmsg() return value.
3866 int consumer_send_status_channel(int sock
,
3867 struct lttng_consumer_channel
*channel
)
3869 struct lttcomm_consumer_status_channel msg
;
3873 memset(&msg
, 0, sizeof(msg
));
3875 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3877 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3878 msg
.key
= channel
->key
;
3879 msg
.stream_count
= channel
->streams
.count
;
3882 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3885 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3886 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3887 uint64_t max_sb_size
)
3889 unsigned long start_pos
;
3891 if (!nb_packets_per_stream
) {
3892 return consumed_pos
; /* Grab everything */
3894 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
3895 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3896 if ((long) (start_pos
- consumed_pos
) < 0) {
3897 return consumed_pos
; /* Grab everything */
3903 int consumer_flush_buffer(struct lttng_consumer_stream
*stream
, int producer_active
)
3907 switch (consumer_data
.type
) {
3908 case LTTNG_CONSUMER_KERNEL
:
3909 if (producer_active
) {
3910 ret
= kernctl_buffer_flush(stream
->wait_fd
);
3912 ERR("Failed to flush kernel stream");
3916 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
3918 ERR("Failed to flush kernel stream");
3923 case LTTNG_CONSUMER32_UST
:
3924 case LTTNG_CONSUMER64_UST
:
3925 lttng_ustconsumer_flush_buffer(stream
, producer_active
);
3928 ERR("Unknown consumer_data type");
3937 * Sample the rotate position for all the streams of a channel. If a stream
3938 * is already at the rotate position (produced == consumed), we flag it as
3939 * ready for rotation. The rotation of ready streams occurs after we have
3940 * replied to the session daemon that we have finished sampling the positions.
3941 * Must be called with RCU read-side lock held to ensure existence of channel.
3943 * Returns 0 on success, < 0 on error
3945 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
3946 uint64_t key
, uint64_t relayd_id
, uint32_t metadata
,
3947 struct lttng_consumer_local_data
*ctx
)
3950 struct lttng_consumer_stream
*stream
;
3951 struct lttng_ht_iter iter
;
3952 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
3953 struct lttng_dynamic_array stream_rotation_positions
;
3954 uint64_t next_chunk_id
, stream_count
= 0;
3955 enum lttng_trace_chunk_status chunk_status
;
3956 const bool is_local_trace
= relayd_id
== -1ULL;
3957 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3958 bool rotating_to_new_chunk
= true;
3960 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
3962 lttng_dynamic_array_init(&stream_rotation_positions
,
3963 sizeof(struct relayd_stream_rotation_position
), NULL
);
3967 pthread_mutex_lock(&channel
->lock
);
3968 assert(channel
->trace_chunk
);
3969 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
,
3971 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
3973 goto end_unlock_channel
;
3976 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3977 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
3978 ht
->match_fct
, &channel
->key
, &iter
.iter
,
3979 stream
, node_channel_id
.node
) {
3980 unsigned long produced_pos
= 0, consumed_pos
= 0;
3982 health_code_update();
3985 * Lock stream because we are about to change its state.
3987 pthread_mutex_lock(&stream
->lock
);
3989 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
3990 rotating_to_new_chunk
= false;
3994 * Do not flush an empty packet when rotating from a NULL trace
3995 * chunk. The stream has no means to output data, and the prior
3996 * rotation which rotated to NULL performed that side-effect already.
3998 if (stream
->trace_chunk
) {
4000 * For metadata stream, do an active flush, which does not
4001 * produce empty packets. For data streams, empty-flush;
4002 * ensures we have at least one packet in each stream per trace
4003 * chunk, even if no data was produced.
4005 ret
= consumer_flush_buffer(stream
, stream
->metadata_flag
? 1 : 0);
4007 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4009 goto end_unlock_stream
;
4013 ret
= lttng_consumer_take_snapshot(stream
);
4014 if (ret
< 0 && ret
!= -ENODATA
&& ret
!= -EAGAIN
) {
4015 ERR("Failed to sample snapshot position during channel rotation");
4016 goto end_unlock_stream
;
4019 ret
= lttng_consumer_get_produced_snapshot(stream
,
4022 ERR("Failed to sample produced position during channel rotation");
4023 goto end_unlock_stream
;
4026 ret
= lttng_consumer_get_consumed_snapshot(stream
,
4029 ERR("Failed to sample consumed position during channel rotation");
4030 goto end_unlock_stream
;
4034 * Align produced position on the start-of-packet boundary of the first
4035 * packet going into the next trace chunk.
4037 produced_pos
= ALIGN_FLOOR(produced_pos
, stream
->max_sb_size
);
4038 if (consumed_pos
== produced_pos
) {
4039 stream
->rotate_ready
= true;
4042 * The rotation position is based on the packet_seq_num of the
4043 * packet following the last packet that was consumed for this
4044 * stream, incremented by the offset between produced and
4045 * consumed positions. This rotation position is a lower bound
4046 * (inclusive) at which the next trace chunk starts. Since it
4047 * is a lower bound, it is OK if the packet_seq_num does not
4048 * correspond exactly to the same packet identified by the
4049 * consumed_pos, which can happen in overwrite mode.
4051 if (stream
->sequence_number_unavailable
) {
4053 * Rotation should never be performed on a session which
4054 * interacts with a pre-2.8 lttng-modules, which does
4055 * not implement packet sequence number.
4057 ERR("Failure to rotate stream %" PRIu64
": sequence number unavailable",
4060 goto end_unlock_stream
;
4062 stream
->rotate_position
= stream
->last_sequence_number
+ 1 +
4063 ((produced_pos
- consumed_pos
) / stream
->max_sb_size
);
4065 if (!is_local_trace
) {
4067 * The relay daemon control protocol expects a rotation
4068 * position as "the sequence number of the first packet
4069 * _after_ the current trace chunk".
4071 const struct relayd_stream_rotation_position position
= {
4072 .stream_id
= stream
->relayd_stream_id
,
4073 .rotate_at_seq_num
= stream
->rotate_position
,
4076 ret
= lttng_dynamic_array_add_element(
4077 &stream_rotation_positions
,
4080 ERR("Failed to allocate stream rotation position");
4081 goto end_unlock_stream
;
4085 pthread_mutex_unlock(&stream
->lock
);
4088 pthread_mutex_unlock(&channel
->lock
);
4090 if (is_local_trace
) {
4095 relayd
= consumer_find_relayd(relayd_id
);
4097 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4102 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4103 ret
= relayd_rotate_streams(&relayd
->control_sock
, stream_count
,
4104 rotating_to_new_chunk
? &next_chunk_id
: NULL
,
4105 (const struct relayd_stream_rotation_position
*)
4106 stream_rotation_positions
.buffer
.data
);
4107 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4109 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4110 relayd
->net_seq_idx
);
4111 lttng_consumer_cleanup_relayd(relayd
);
4119 pthread_mutex_unlock(&stream
->lock
);
4121 pthread_mutex_unlock(&channel
->lock
);
4124 lttng_dynamic_array_reset(&stream_rotation_positions
);
4129 * Check if a stream is ready to be rotated after extracting it.
4131 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4132 * error. Stream lock must be held.
4134 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4136 if (stream
->rotate_ready
) {
4141 * If packet seq num is unavailable, it means we are interacting
4142 * with a pre-2.8 lttng-modules which does not implement the
4143 * sequence number. Rotation should never be used by sessiond in this
4146 if (stream
->sequence_number_unavailable
) {
4147 ERR("Internal error: rotation used on stream %" PRIu64
4148 " with unavailable sequence number",
4153 if (stream
->rotate_position
== -1ULL ||
4154 stream
->last_sequence_number
== -1ULL) {
4159 * Rotate position not reached yet. The stream rotate position is
4160 * the position of the next packet belonging to the next trace chunk,
4161 * but consumerd considers rotation ready when reaching the last
4162 * packet of the current chunk, hence the "rotate_position - 1".
4164 if (stream
->last_sequence_number
>= stream
->rotate_position
- 1) {
4172 * Reset the state for a stream after a rotation occurred.
4174 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4176 stream
->rotate_position
= -1ULL;
4177 stream
->rotate_ready
= false;
4181 * Perform the rotation a local stream file.
4184 int rotate_local_stream(struct lttng_consumer_local_data
*ctx
,
4185 struct lttng_consumer_stream
*stream
)
4189 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4192 stream
->tracefile_size_current
= 0;
4193 stream
->tracefile_count_current
= 0;
4195 if (stream
->out_fd
>= 0) {
4196 ret
= close(stream
->out_fd
);
4198 PERROR("Failed to close stream out_fd of channel \"%s\"",
4199 stream
->chan
->name
);
4201 stream
->out_fd
= -1;
4204 if (stream
->index_file
) {
4205 lttng_index_file_put(stream
->index_file
);
4206 stream
->index_file
= NULL
;
4209 if (!stream
->trace_chunk
) {
4213 ret
= consumer_stream_create_output_files(stream
, true);
4219 * Performs the stream rotation for the rotate session feature if needed.
4220 * It must be called with the channel and stream locks held.
4222 * Return 0 on success, a negative number of error.
4224 int lttng_consumer_rotate_stream(struct lttng_consumer_local_data
*ctx
,
4225 struct lttng_consumer_stream
*stream
)
4229 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4232 * Update the stream's 'current' chunk to the session's (channel)
4233 * now-current chunk.
4235 lttng_trace_chunk_put(stream
->trace_chunk
);
4236 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4238 * A channel can be rotated and not have a "next" chunk
4239 * to transition to. In that case, the channel's "current chunk"
4240 * has not been closed yet, but it has not been updated to
4241 * a "next" trace chunk either. Hence, the stream, like its
4242 * parent channel, becomes part of no chunk and can't output
4243 * anything until a new trace chunk is created.
4245 stream
->trace_chunk
= NULL
;
4246 } else if (stream
->chan
->trace_chunk
&&
4247 !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4248 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4253 * Update the stream's trace chunk to its parent channel's
4254 * current trace chunk.
4256 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4259 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4260 ret
= rotate_local_stream(ctx
, stream
);
4262 ERR("Failed to rotate stream, ret = %i", ret
);
4267 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4269 * If the stream has transitioned to a new trace
4270 * chunk, the metadata should be re-dumped to the
4273 * However, it is possible for a stream to transition to
4274 * a "no-chunk" state. This can happen if a rotation
4275 * occurs on an inactive session. In such cases, the metadata
4276 * regeneration will happen when the next trace chunk is
4279 ret
= consumer_metadata_stream_dump(stream
);
4284 lttng_consumer_reset_stream_rotate_state(stream
);
4293 * Rotate all the ready streams now.
4295 * This is especially important for low throughput streams that have already
4296 * been consumed, we cannot wait for their next packet to perform the
4298 * Need to be called with RCU read-side lock held to ensure existence of
4301 * Returns 0 on success, < 0 on error
4303 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
,
4304 uint64_t key
, struct lttng_consumer_local_data
*ctx
)
4307 struct lttng_consumer_stream
*stream
;
4308 struct lttng_ht_iter iter
;
4309 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
4313 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4315 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4316 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4317 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4318 stream
, node_channel_id
.node
) {
4319 health_code_update();
4321 pthread_mutex_lock(&stream
->chan
->lock
);
4322 pthread_mutex_lock(&stream
->lock
);
4324 if (!stream
->rotate_ready
) {
4325 pthread_mutex_unlock(&stream
->lock
);
4326 pthread_mutex_unlock(&stream
->chan
->lock
);
4329 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4331 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
4332 pthread_mutex_unlock(&stream
->lock
);
4333 pthread_mutex_unlock(&stream
->chan
->lock
);
4346 enum lttcomm_return_code
lttng_consumer_init_command(
4347 struct lttng_consumer_local_data
*ctx
,
4348 const lttng_uuid sessiond_uuid
)
4350 enum lttcomm_return_code ret
;
4351 char uuid_str
[UUID_STR_LEN
];
4353 if (ctx
->sessiond_uuid
.is_set
) {
4354 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4358 ctx
->sessiond_uuid
.is_set
= true;
4359 memcpy(ctx
->sessiond_uuid
.value
, sessiond_uuid
, sizeof(lttng_uuid
));
4360 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4361 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4362 DBG("Received session daemon UUID: %s", uuid_str
);
4367 enum lttcomm_return_code
lttng_consumer_create_trace_chunk(
4368 const uint64_t *relayd_id
, uint64_t session_id
,
4370 time_t chunk_creation_timestamp
,
4371 const char *chunk_override_name
,
4372 const struct lttng_credentials
*credentials
,
4373 struct lttng_directory_handle
*chunk_directory_handle
)
4376 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4377 struct lttng_trace_chunk
*created_chunk
= NULL
, *published_chunk
= NULL
;
4378 enum lttng_trace_chunk_status chunk_status
;
4379 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4380 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4381 const char *relayd_id_str
= "(none)";
4382 const char *creation_timestamp_str
;
4383 struct lttng_ht_iter iter
;
4384 struct lttng_consumer_channel
*channel
;
4387 /* Only used for logging purposes. */
4388 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4389 "%" PRIu64
, *relayd_id
);
4390 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4391 relayd_id_str
= relayd_id_buffer
;
4393 relayd_id_str
= "(formatting error)";
4397 /* Local protocol error. */
4398 assert(chunk_creation_timestamp
);
4399 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4400 creation_timestamp_buffer
,
4401 sizeof(creation_timestamp_buffer
));
4402 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
4403 "(formatting error)";
4405 DBG("Consumer create trace chunk command: relay_id = %s"
4406 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4407 ", chunk_override_name = %s"
4408 ", chunk_creation_timestamp = %s",
4409 relayd_id_str
, session_id
, chunk_id
,
4410 chunk_override_name
? : "(none)",
4411 creation_timestamp_str
);
4414 * The trace chunk registry, as used by the consumer daemon, implicitly
4415 * owns the trace chunks. This is only needed in the consumer since
4416 * the consumer has no notion of a session beyond session IDs being
4417 * used to identify other objects.
4419 * The lttng_trace_chunk_registry_publish() call below provides a
4420 * reference which is not released; it implicitly becomes the session
4421 * daemon's reference to the chunk in the consumer daemon.
4423 * The lifetime of trace chunks in the consumer daemon is managed by
4424 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4425 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4427 created_chunk
= lttng_trace_chunk_create(chunk_id
,
4428 chunk_creation_timestamp
);
4429 if (!created_chunk
) {
4430 ERR("Failed to create trace chunk");
4431 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4435 if (chunk_override_name
) {
4436 chunk_status
= lttng_trace_chunk_override_name(created_chunk
,
4437 chunk_override_name
);
4438 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4439 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4444 if (chunk_directory_handle
) {
4445 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
,
4447 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4448 ERR("Failed to set trace chunk credentials");
4449 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4453 * The consumer daemon has no ownership of the chunk output
4456 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
,
4457 chunk_directory_handle
);
4458 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4459 ERR("Failed to set trace chunk's directory handle");
4460 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4465 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4466 consumer_data
.chunk_registry
, session_id
,
4468 lttng_trace_chunk_put(created_chunk
);
4469 created_chunk
= NULL
;
4470 if (!published_chunk
) {
4471 ERR("Failed to publish trace chunk");
4472 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4477 cds_lfht_for_each_entry_duplicate(consumer_data
.channels_by_session_id_ht
->ht
,
4478 consumer_data
.channels_by_session_id_ht
->hash_fct(
4479 &session_id
, lttng_ht_seed
),
4480 consumer_data
.channels_by_session_id_ht
->match_fct
,
4481 &session_id
, &iter
.iter
, channel
,
4482 channels_by_session_id_ht_node
.node
) {
4483 ret
= lttng_consumer_channel_set_trace_chunk(channel
,
4487 * Roll-back the creation of this chunk.
4489 * This is important since the session daemon will
4490 * assume that the creation of this chunk failed and
4491 * will never ask for it to be closed, resulting
4492 * in a leak and an inconsistent state for some
4495 enum lttcomm_return_code close_ret
;
4496 char path
[LTTNG_PATH_MAX
];
4498 DBG("Failed to set new trace chunk on existing channels, rolling back");
4499 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4500 session_id
, chunk_id
,
4501 chunk_creation_timestamp
, NULL
,
4503 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4504 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4505 session_id
, chunk_id
);
4508 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4514 struct consumer_relayd_sock_pair
*relayd
;
4516 relayd
= consumer_find_relayd(*relayd_id
);
4518 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4519 ret
= relayd_create_trace_chunk(
4520 &relayd
->control_sock
, published_chunk
);
4521 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4523 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4526 if (!relayd
|| ret
) {
4527 enum lttcomm_return_code close_ret
;
4528 char path
[LTTNG_PATH_MAX
];
4530 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4533 chunk_creation_timestamp
,
4535 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4536 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4541 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4548 /* Release the reference returned by the "publish" operation. */
4549 lttng_trace_chunk_put(published_chunk
);
4550 lttng_trace_chunk_put(created_chunk
);
4554 enum lttcomm_return_code
lttng_consumer_close_trace_chunk(
4555 const uint64_t *relayd_id
, uint64_t session_id
,
4556 uint64_t chunk_id
, time_t chunk_close_timestamp
,
4557 const enum lttng_trace_chunk_command_type
*close_command
,
4560 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4561 struct lttng_trace_chunk
*chunk
;
4562 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4563 const char *relayd_id_str
= "(none)";
4564 const char *close_command_name
= "none";
4565 struct lttng_ht_iter iter
;
4566 struct lttng_consumer_channel
*channel
;
4567 enum lttng_trace_chunk_status chunk_status
;
4572 /* Only used for logging purposes. */
4573 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4574 "%" PRIu64
, *relayd_id
);
4575 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4576 relayd_id_str
= relayd_id_buffer
;
4578 relayd_id_str
= "(formatting error)";
4581 if (close_command
) {
4582 close_command_name
= lttng_trace_chunk_command_type_get_name(
4586 DBG("Consumer close trace chunk command: relayd_id = %s"
4587 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4588 ", close command = %s",
4589 relayd_id_str
, session_id
, chunk_id
,
4590 close_command_name
);
4592 chunk
= lttng_trace_chunk_registry_find_chunk(
4593 consumer_data
.chunk_registry
, session_id
, chunk_id
);
4595 ERR("Failed to find chunk: session_id = %" PRIu64
4596 ", chunk_id = %" PRIu64
,
4597 session_id
, chunk_id
);
4598 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4602 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
,
4603 chunk_close_timestamp
);
4604 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4605 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4609 if (close_command
) {
4610 chunk_status
= lttng_trace_chunk_set_close_command(
4611 chunk
, *close_command
);
4612 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4613 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4619 * chunk is now invalid to access as we no longer hold a reference to
4620 * it; it is only kept around to compare it (by address) to the
4621 * current chunk found in the session's channels.
4624 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
,
4625 channel
, node
.node
) {
4629 * Only change the channel's chunk to NULL if it still
4630 * references the chunk being closed. The channel may
4631 * reference a newer channel in the case of a session
4632 * rotation. When a session rotation occurs, the "next"
4633 * chunk is created before the "current" chunk is closed.
4635 if (channel
->trace_chunk
!= chunk
) {
4638 ret
= lttng_consumer_channel_set_trace_chunk(channel
, NULL
);
4641 * Attempt to close the chunk on as many channels as
4644 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4650 struct consumer_relayd_sock_pair
*relayd
;
4652 relayd
= consumer_find_relayd(*relayd_id
);
4654 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4655 ret
= relayd_close_trace_chunk(
4656 &relayd
->control_sock
, chunk
,
4658 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4660 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
,
4664 if (!relayd
|| ret
) {
4665 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4673 * Release the reference returned by the "find" operation and
4674 * the session daemon's implicit reference to the chunk.
4676 lttng_trace_chunk_put(chunk
);
4677 lttng_trace_chunk_put(chunk
);
4682 enum lttcomm_return_code
lttng_consumer_trace_chunk_exists(
4683 const uint64_t *relayd_id
, uint64_t session_id
,
4687 enum lttcomm_return_code ret_code
;
4688 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4689 const char *relayd_id_str
= "(none)";
4690 const bool is_local_trace
= !relayd_id
;
4691 struct consumer_relayd_sock_pair
*relayd
= NULL
;
4692 bool chunk_exists_local
, chunk_exists_remote
;
4697 /* Only used for logging purposes. */
4698 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4699 "%" PRIu64
, *relayd_id
);
4700 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4701 relayd_id_str
= relayd_id_buffer
;
4703 relayd_id_str
= "(formatting error)";
4707 DBG("Consumer trace chunk exists command: relayd_id = %s"
4708 ", chunk_id = %" PRIu64
, relayd_id_str
,
4710 ret
= lttng_trace_chunk_registry_chunk_exists(
4711 consumer_data
.chunk_registry
, session_id
,
4712 chunk_id
, &chunk_exists_local
);
4714 /* Internal error. */
4715 ERR("Failed to query the existence of a trace chunk");
4716 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
4719 DBG("Trace chunk %s locally",
4720 chunk_exists_local
? "exists" : "does not exist");
4721 if (chunk_exists_local
) {
4722 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
4724 } else if (is_local_trace
) {
4725 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4730 relayd
= consumer_find_relayd(*relayd_id
);
4732 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
4733 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
4734 goto end_rcu_unlock
;
4736 DBG("Looking up existence of trace chunk on relay daemon");
4737 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4738 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
,
4739 &chunk_exists_remote
);
4740 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4742 ERR("Failed to look-up the existence of trace chunk on relay daemon");
4743 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
4744 goto end_rcu_unlock
;
4747 ret_code
= chunk_exists_remote
?
4748 LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
4749 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4750 DBG("Trace chunk %s on relay daemon",
4751 chunk_exists_remote
? "exists" : "does not exist");