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
= 0;
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
) {
3355 ret
= stream
->read_subbuffer_ops
.pre_consume_subbuffer(
3356 stream
, &subbuffer
);
3358 goto error_put_subbuf
;
3361 written_bytes
= stream
->read_subbuffer_ops
.consume_subbuffer(
3362 ctx
, stream
, &subbuffer
);
3363 if (written_bytes
<= 0) {
3364 ERR("Error consuming subbuffer: (%zd)", written_bytes
);
3365 ret
= (int) written_bytes
;
3366 goto error_put_subbuf
;
3369 ret
= stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3374 if (stream
->read_subbuffer_ops
.post_consume
) {
3375 ret
= stream
->read_subbuffer_ops
.post_consume(stream
, &subbuffer
, ctx
);
3382 * After extracting the packet, we check if the stream is now ready to
3383 * be rotated and perform the action immediately.
3385 * Don't overwrite `ret` as callers expect the number of bytes
3386 * consumed to be returned on success.
3388 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
3389 if (rotation_ret
== 1) {
3390 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3391 if (rotation_ret
< 0) {
3393 ERR("Stream rotation error after consuming data");
3396 } else if (rotation_ret
< 0) {
3398 ERR("Failed to check if stream was ready to rotate after consuming data");
3403 if (stream
->read_subbuffer_ops
.on_sleep
) {
3404 stream
->read_subbuffer_ops
.on_sleep(stream
, ctx
);
3407 ret
= written_bytes
;
3409 if (!locked_by_caller
) {
3410 stream
->read_subbuffer_ops
.unlock(stream
);
3415 (void) stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3419 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3421 switch (consumer_data
.type
) {
3422 case LTTNG_CONSUMER_KERNEL
:
3423 return lttng_kconsumer_on_recv_stream(stream
);
3424 case LTTNG_CONSUMER32_UST
:
3425 case LTTNG_CONSUMER64_UST
:
3426 return lttng_ustconsumer_on_recv_stream(stream
);
3428 ERR("Unknown consumer_data type");
3435 * Allocate and set consumer data hash tables.
3437 int lttng_consumer_init(void)
3439 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3440 if (!consumer_data
.channel_ht
) {
3444 consumer_data
.channels_by_session_id_ht
=
3445 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3446 if (!consumer_data
.channels_by_session_id_ht
) {
3450 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3451 if (!consumer_data
.relayd_ht
) {
3455 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3456 if (!consumer_data
.stream_list_ht
) {
3460 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3461 if (!consumer_data
.stream_per_chan_id_ht
) {
3465 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3470 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3475 consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3476 if (!consumer_data
.chunk_registry
) {
3487 * Process the ADD_RELAYD command receive by a consumer.
3489 * This will create a relayd socket pair and add it to the relayd hash table.
3490 * The caller MUST acquire a RCU read side lock before calling it.
3492 void consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3493 struct lttng_consumer_local_data
*ctx
, int sock
,
3494 struct pollfd
*consumer_sockpoll
,
3495 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3496 uint64_t relayd_session_id
)
3498 int fd
= -1, ret
= -1, relayd_created
= 0;
3499 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3500 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3503 assert(relayd_sock
);
3505 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3507 /* Get relayd reference if exists. */
3508 relayd
= consumer_find_relayd(net_seq_idx
);
3509 if (relayd
== NULL
) {
3510 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3511 /* Not found. Allocate one. */
3512 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3513 if (relayd
== NULL
) {
3514 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3517 relayd
->sessiond_session_id
= sessiond_id
;
3522 * This code path MUST continue to the consumer send status message to
3523 * we can notify the session daemon and continue our work without
3524 * killing everything.
3528 * relayd key should never be found for control socket.
3530 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3533 /* First send a status message before receiving the fds. */
3534 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3536 /* Somehow, the session daemon is not responding anymore. */
3537 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3538 goto error_nosignal
;
3541 /* Poll on consumer socket. */
3542 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3544 /* Needing to exit in the middle of a command: error. */
3545 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3546 goto error_nosignal
;
3549 /* Get relayd socket from session daemon */
3550 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3551 if (ret
!= sizeof(fd
)) {
3552 fd
= -1; /* Just in case it gets set with an invalid value. */
3555 * Failing to receive FDs might indicate a major problem such as
3556 * reaching a fd limit during the receive where the kernel returns a
3557 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3558 * don't take any chances and stop everything.
3560 * XXX: Feature request #558 will fix that and avoid this possible
3561 * issue when reaching the fd limit.
3563 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3564 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3568 /* Copy socket information and received FD */
3569 switch (sock_type
) {
3570 case LTTNG_STREAM_CONTROL
:
3571 /* Copy received lttcomm socket */
3572 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3573 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3574 /* Handle create_sock error. */
3576 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3580 * Close the socket created internally by
3581 * lttcomm_create_sock, so we can replace it by the one
3582 * received from sessiond.
3584 if (close(relayd
->control_sock
.sock
.fd
)) {
3588 /* Assign new file descriptor */
3589 relayd
->control_sock
.sock
.fd
= fd
;
3590 /* Assign version values. */
3591 relayd
->control_sock
.major
= relayd_sock
->major
;
3592 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3594 relayd
->relayd_session_id
= relayd_session_id
;
3597 case LTTNG_STREAM_DATA
:
3598 /* Copy received lttcomm socket */
3599 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3600 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3601 /* Handle create_sock error. */
3603 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3607 * Close the socket created internally by
3608 * lttcomm_create_sock, so we can replace it by the one
3609 * received from sessiond.
3611 if (close(relayd
->data_sock
.sock
.fd
)) {
3615 /* Assign new file descriptor */
3616 relayd
->data_sock
.sock
.fd
= fd
;
3617 /* Assign version values. */
3618 relayd
->data_sock
.major
= relayd_sock
->major
;
3619 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3622 ERR("Unknown relayd socket type (%d)", sock_type
);
3623 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3627 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3628 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3629 relayd
->net_seq_idx
, fd
);
3631 * We gave the ownership of the fd to the relayd structure. Set the
3632 * fd to -1 so we don't call close() on it in the error path below.
3636 /* We successfully added the socket. Send status back. */
3637 ret
= consumer_send_status_msg(sock
, ret_code
);
3639 /* Somehow, the session daemon is not responding anymore. */
3640 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3641 goto error_nosignal
;
3645 * Add relayd socket pair to consumer data hashtable. If object already
3646 * exists or on error, the function gracefully returns.
3655 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3656 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3660 /* Close received socket if valid. */
3663 PERROR("close received socket");
3667 if (relayd_created
) {
3673 * Search for a relayd associated to the session id and return the reference.
3675 * A rcu read side lock MUST be acquire before calling this function and locked
3676 * until the relayd object is no longer necessary.
3678 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3680 struct lttng_ht_iter iter
;
3681 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3683 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3684 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3687 * Check by sessiond id which is unique here where the relayd session
3688 * id might not be when having multiple relayd.
3690 if (relayd
->sessiond_session_id
== id
) {
3691 /* Found the relayd. There can be only one per id. */
3703 * Check if for a given session id there is still data needed to be extract
3706 * Return 1 if data is pending or else 0 meaning ready to be read.
3708 int consumer_data_pending(uint64_t id
)
3711 struct lttng_ht_iter iter
;
3712 struct lttng_ht
*ht
;
3713 struct lttng_consumer_stream
*stream
;
3714 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3715 int (*data_pending
)(struct lttng_consumer_stream
*);
3717 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3720 pthread_mutex_lock(&consumer_data
.lock
);
3722 switch (consumer_data
.type
) {
3723 case LTTNG_CONSUMER_KERNEL
:
3724 data_pending
= lttng_kconsumer_data_pending
;
3726 case LTTNG_CONSUMER32_UST
:
3727 case LTTNG_CONSUMER64_UST
:
3728 data_pending
= lttng_ustconsumer_data_pending
;
3731 ERR("Unknown consumer data type");
3735 /* Ease our life a bit */
3736 ht
= consumer_data
.stream_list_ht
;
3738 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3739 ht
->hash_fct(&id
, lttng_ht_seed
),
3741 &iter
.iter
, stream
, node_session_id
.node
) {
3742 pthread_mutex_lock(&stream
->lock
);
3745 * A removed node from the hash table indicates that the stream has
3746 * been deleted thus having a guarantee that the buffers are closed
3747 * on the consumer side. However, data can still be transmitted
3748 * over the network so don't skip the relayd check.
3750 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3752 /* Check the stream if there is data in the buffers. */
3753 ret
= data_pending(stream
);
3755 pthread_mutex_unlock(&stream
->lock
);
3760 pthread_mutex_unlock(&stream
->lock
);
3763 relayd
= find_relayd_by_session_id(id
);
3765 unsigned int is_data_inflight
= 0;
3767 /* Send init command for data pending. */
3768 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3769 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3770 relayd
->relayd_session_id
);
3772 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3773 /* Communication error thus the relayd so no data pending. */
3774 goto data_not_pending
;
3777 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3778 ht
->hash_fct(&id
, lttng_ht_seed
),
3780 &iter
.iter
, stream
, node_session_id
.node
) {
3781 if (stream
->metadata_flag
) {
3782 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3783 stream
->relayd_stream_id
);
3785 ret
= relayd_data_pending(&relayd
->control_sock
,
3786 stream
->relayd_stream_id
,
3787 stream
->next_net_seq_num
- 1);
3791 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3793 } else if (ret
< 0) {
3794 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3795 lttng_consumer_cleanup_relayd(relayd
);
3796 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3797 goto data_not_pending
;
3801 /* Send end command for data pending. */
3802 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3803 relayd
->relayd_session_id
, &is_data_inflight
);
3804 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3806 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3807 lttng_consumer_cleanup_relayd(relayd
);
3808 goto data_not_pending
;
3810 if (is_data_inflight
) {
3816 * Finding _no_ node in the hash table and no inflight data means that the
3817 * stream(s) have been removed thus data is guaranteed to be available for
3818 * analysis from the trace files.
3822 /* Data is available to be read by a viewer. */
3823 pthread_mutex_unlock(&consumer_data
.lock
);
3828 /* Data is still being extracted from buffers. */
3829 pthread_mutex_unlock(&consumer_data
.lock
);
3835 * Send a ret code status message to the sessiond daemon.
3837 * Return the sendmsg() return value.
3839 int consumer_send_status_msg(int sock
, int ret_code
)
3841 struct lttcomm_consumer_status_msg msg
;
3843 memset(&msg
, 0, sizeof(msg
));
3844 msg
.ret_code
= ret_code
;
3846 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3850 * Send a channel status message to the sessiond daemon.
3852 * Return the sendmsg() return value.
3854 int consumer_send_status_channel(int sock
,
3855 struct lttng_consumer_channel
*channel
)
3857 struct lttcomm_consumer_status_channel msg
;
3861 memset(&msg
, 0, sizeof(msg
));
3863 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3865 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3866 msg
.key
= channel
->key
;
3867 msg
.stream_count
= channel
->streams
.count
;
3870 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3873 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3874 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3875 uint64_t max_sb_size
)
3877 unsigned long start_pos
;
3879 if (!nb_packets_per_stream
) {
3880 return consumed_pos
; /* Grab everything */
3882 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
3883 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3884 if ((long) (start_pos
- consumed_pos
) < 0) {
3885 return consumed_pos
; /* Grab everything */
3891 int consumer_flush_buffer(struct lttng_consumer_stream
*stream
, int producer_active
)
3895 switch (consumer_data
.type
) {
3896 case LTTNG_CONSUMER_KERNEL
:
3897 if (producer_active
) {
3898 ret
= kernctl_buffer_flush(stream
->wait_fd
);
3900 ERR("Failed to flush kernel stream");
3904 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
3906 ERR("Failed to flush kernel stream");
3911 case LTTNG_CONSUMER32_UST
:
3912 case LTTNG_CONSUMER64_UST
:
3913 lttng_ustconsumer_flush_buffer(stream
, producer_active
);
3916 ERR("Unknown consumer_data type");
3925 * Sample the rotate position for all the streams of a channel. If a stream
3926 * is already at the rotate position (produced == consumed), we flag it as
3927 * ready for rotation. The rotation of ready streams occurs after we have
3928 * replied to the session daemon that we have finished sampling the positions.
3929 * Must be called with RCU read-side lock held to ensure existence of channel.
3931 * Returns 0 on success, < 0 on error
3933 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
3934 uint64_t key
, uint64_t relayd_id
, uint32_t metadata
,
3935 struct lttng_consumer_local_data
*ctx
)
3938 struct lttng_consumer_stream
*stream
;
3939 struct lttng_ht_iter iter
;
3940 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
3941 struct lttng_dynamic_array stream_rotation_positions
;
3942 uint64_t next_chunk_id
, stream_count
= 0;
3943 enum lttng_trace_chunk_status chunk_status
;
3944 const bool is_local_trace
= relayd_id
== -1ULL;
3945 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3946 bool rotating_to_new_chunk
= true;
3948 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
3950 lttng_dynamic_array_init(&stream_rotation_positions
,
3951 sizeof(struct relayd_stream_rotation_position
), NULL
);
3955 pthread_mutex_lock(&channel
->lock
);
3956 assert(channel
->trace_chunk
);
3957 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
,
3959 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
3961 goto end_unlock_channel
;
3964 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3965 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
3966 ht
->match_fct
, &channel
->key
, &iter
.iter
,
3967 stream
, node_channel_id
.node
) {
3968 unsigned long produced_pos
= 0, consumed_pos
= 0;
3970 health_code_update();
3973 * Lock stream because we are about to change its state.
3975 pthread_mutex_lock(&stream
->lock
);
3977 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
3978 rotating_to_new_chunk
= false;
3982 * Do not flush an empty packet when rotating from a NULL trace
3983 * chunk. The stream has no means to output data, and the prior
3984 * rotation which rotated to NULL performed that side-effect already.
3986 if (stream
->trace_chunk
) {
3988 * For metadata stream, do an active flush, which does not
3989 * produce empty packets. For data streams, empty-flush;
3990 * ensures we have at least one packet in each stream per trace
3991 * chunk, even if no data was produced.
3993 ret
= consumer_flush_buffer(stream
, stream
->metadata_flag
? 1 : 0);
3995 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
3997 goto end_unlock_stream
;
4001 ret
= lttng_consumer_take_snapshot(stream
);
4002 if (ret
< 0 && ret
!= -ENODATA
&& ret
!= -EAGAIN
) {
4003 ERR("Failed to sample snapshot position during channel rotation");
4004 goto end_unlock_stream
;
4007 ret
= lttng_consumer_get_produced_snapshot(stream
,
4010 ERR("Failed to sample produced position during channel rotation");
4011 goto end_unlock_stream
;
4014 ret
= lttng_consumer_get_consumed_snapshot(stream
,
4017 ERR("Failed to sample consumed position during channel rotation");
4018 goto end_unlock_stream
;
4022 * Align produced position on the start-of-packet boundary of the first
4023 * packet going into the next trace chunk.
4025 produced_pos
= ALIGN_FLOOR(produced_pos
, stream
->max_sb_size
);
4026 if (consumed_pos
== produced_pos
) {
4027 stream
->rotate_ready
= true;
4030 * The rotation position is based on the packet_seq_num of the
4031 * packet following the last packet that was consumed for this
4032 * stream, incremented by the offset between produced and
4033 * consumed positions. This rotation position is a lower bound
4034 * (inclusive) at which the next trace chunk starts. Since it
4035 * is a lower bound, it is OK if the packet_seq_num does not
4036 * correspond exactly to the same packet identified by the
4037 * consumed_pos, which can happen in overwrite mode.
4039 if (stream
->sequence_number_unavailable
) {
4041 * Rotation should never be performed on a session which
4042 * interacts with a pre-2.8 lttng-modules, which does
4043 * not implement packet sequence number.
4045 ERR("Failure to rotate stream %" PRIu64
": sequence number unavailable",
4048 goto end_unlock_stream
;
4050 stream
->rotate_position
= stream
->last_sequence_number
+ 1 +
4051 ((produced_pos
- consumed_pos
) / stream
->max_sb_size
);
4053 if (!is_local_trace
) {
4055 * The relay daemon control protocol expects a rotation
4056 * position as "the sequence number of the first packet
4057 * _after_ the current trace chunk".
4059 const struct relayd_stream_rotation_position position
= {
4060 .stream_id
= stream
->relayd_stream_id
,
4061 .rotate_at_seq_num
= stream
->rotate_position
,
4064 ret
= lttng_dynamic_array_add_element(
4065 &stream_rotation_positions
,
4068 ERR("Failed to allocate stream rotation position");
4069 goto end_unlock_stream
;
4073 pthread_mutex_unlock(&stream
->lock
);
4076 pthread_mutex_unlock(&channel
->lock
);
4078 if (is_local_trace
) {
4083 relayd
= consumer_find_relayd(relayd_id
);
4085 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4090 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4091 ret
= relayd_rotate_streams(&relayd
->control_sock
, stream_count
,
4092 rotating_to_new_chunk
? &next_chunk_id
: NULL
,
4093 (const struct relayd_stream_rotation_position
*)
4094 stream_rotation_positions
.buffer
.data
);
4095 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4097 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4098 relayd
->net_seq_idx
);
4099 lttng_consumer_cleanup_relayd(relayd
);
4107 pthread_mutex_unlock(&stream
->lock
);
4109 pthread_mutex_unlock(&channel
->lock
);
4112 lttng_dynamic_array_reset(&stream_rotation_positions
);
4117 * Check if a stream is ready to be rotated after extracting it.
4119 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4120 * error. Stream lock must be held.
4122 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4124 if (stream
->rotate_ready
) {
4129 * If packet seq num is unavailable, it means we are interacting
4130 * with a pre-2.8 lttng-modules which does not implement the
4131 * sequence number. Rotation should never be used by sessiond in this
4134 if (stream
->sequence_number_unavailable
) {
4135 ERR("Internal error: rotation used on stream %" PRIu64
4136 " with unavailable sequence number",
4141 if (stream
->rotate_position
== -1ULL ||
4142 stream
->last_sequence_number
== -1ULL) {
4147 * Rotate position not reached yet. The stream rotate position is
4148 * the position of the next packet belonging to the next trace chunk,
4149 * but consumerd considers rotation ready when reaching the last
4150 * packet of the current chunk, hence the "rotate_position - 1".
4152 if (stream
->last_sequence_number
>= stream
->rotate_position
- 1) {
4160 * Reset the state for a stream after a rotation occurred.
4162 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4164 stream
->rotate_position
= -1ULL;
4165 stream
->rotate_ready
= false;
4169 * Perform the rotation a local stream file.
4172 int rotate_local_stream(struct lttng_consumer_local_data
*ctx
,
4173 struct lttng_consumer_stream
*stream
)
4177 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4180 stream
->tracefile_size_current
= 0;
4181 stream
->tracefile_count_current
= 0;
4183 if (stream
->out_fd
>= 0) {
4184 ret
= close(stream
->out_fd
);
4186 PERROR("Failed to close stream out_fd of channel \"%s\"",
4187 stream
->chan
->name
);
4189 stream
->out_fd
= -1;
4192 if (stream
->index_file
) {
4193 lttng_index_file_put(stream
->index_file
);
4194 stream
->index_file
= NULL
;
4197 if (!stream
->trace_chunk
) {
4201 ret
= consumer_stream_create_output_files(stream
, true);
4207 * Performs the stream rotation for the rotate session feature if needed.
4208 * It must be called with the channel and stream locks held.
4210 * Return 0 on success, a negative number of error.
4212 int lttng_consumer_rotate_stream(struct lttng_consumer_local_data
*ctx
,
4213 struct lttng_consumer_stream
*stream
)
4217 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4220 * Update the stream's 'current' chunk to the session's (channel)
4221 * now-current chunk.
4223 lttng_trace_chunk_put(stream
->trace_chunk
);
4224 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4226 * A channel can be rotated and not have a "next" chunk
4227 * to transition to. In that case, the channel's "current chunk"
4228 * has not been closed yet, but it has not been updated to
4229 * a "next" trace chunk either. Hence, the stream, like its
4230 * parent channel, becomes part of no chunk and can't output
4231 * anything until a new trace chunk is created.
4233 stream
->trace_chunk
= NULL
;
4234 } else if (stream
->chan
->trace_chunk
&&
4235 !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4236 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4241 * Update the stream's trace chunk to its parent channel's
4242 * current trace chunk.
4244 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4247 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4248 ret
= rotate_local_stream(ctx
, stream
);
4250 ERR("Failed to rotate stream, ret = %i", ret
);
4255 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4257 * If the stream has transitioned to a new trace
4258 * chunk, the metadata should be re-dumped to the
4261 * However, it is possible for a stream to transition to
4262 * a "no-chunk" state. This can happen if a rotation
4263 * occurs on an inactive session. In such cases, the metadata
4264 * regeneration will happen when the next trace chunk is
4267 ret
= consumer_metadata_stream_dump(stream
);
4272 lttng_consumer_reset_stream_rotate_state(stream
);
4281 * Rotate all the ready streams now.
4283 * This is especially important for low throughput streams that have already
4284 * been consumed, we cannot wait for their next packet to perform the
4286 * Need to be called with RCU read-side lock held to ensure existence of
4289 * Returns 0 on success, < 0 on error
4291 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
,
4292 uint64_t key
, struct lttng_consumer_local_data
*ctx
)
4295 struct lttng_consumer_stream
*stream
;
4296 struct lttng_ht_iter iter
;
4297 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
4301 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4303 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4304 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4305 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4306 stream
, node_channel_id
.node
) {
4307 health_code_update();
4309 pthread_mutex_lock(&stream
->chan
->lock
);
4310 pthread_mutex_lock(&stream
->lock
);
4312 if (!stream
->rotate_ready
) {
4313 pthread_mutex_unlock(&stream
->lock
);
4314 pthread_mutex_unlock(&stream
->chan
->lock
);
4317 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4319 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
4320 pthread_mutex_unlock(&stream
->lock
);
4321 pthread_mutex_unlock(&stream
->chan
->lock
);
4334 enum lttcomm_return_code
lttng_consumer_init_command(
4335 struct lttng_consumer_local_data
*ctx
,
4336 const lttng_uuid sessiond_uuid
)
4338 enum lttcomm_return_code ret
;
4339 char uuid_str
[UUID_STR_LEN
];
4341 if (ctx
->sessiond_uuid
.is_set
) {
4342 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4346 ctx
->sessiond_uuid
.is_set
= true;
4347 memcpy(ctx
->sessiond_uuid
.value
, sessiond_uuid
, sizeof(lttng_uuid
));
4348 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4349 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4350 DBG("Received session daemon UUID: %s", uuid_str
);
4355 enum lttcomm_return_code
lttng_consumer_create_trace_chunk(
4356 const uint64_t *relayd_id
, uint64_t session_id
,
4358 time_t chunk_creation_timestamp
,
4359 const char *chunk_override_name
,
4360 const struct lttng_credentials
*credentials
,
4361 struct lttng_directory_handle
*chunk_directory_handle
)
4364 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4365 struct lttng_trace_chunk
*created_chunk
= NULL
, *published_chunk
= NULL
;
4366 enum lttng_trace_chunk_status chunk_status
;
4367 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4368 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4369 const char *relayd_id_str
= "(none)";
4370 const char *creation_timestamp_str
;
4371 struct lttng_ht_iter iter
;
4372 struct lttng_consumer_channel
*channel
;
4375 /* Only used for logging purposes. */
4376 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4377 "%" PRIu64
, *relayd_id
);
4378 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4379 relayd_id_str
= relayd_id_buffer
;
4381 relayd_id_str
= "(formatting error)";
4385 /* Local protocol error. */
4386 assert(chunk_creation_timestamp
);
4387 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4388 creation_timestamp_buffer
,
4389 sizeof(creation_timestamp_buffer
));
4390 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
4391 "(formatting error)";
4393 DBG("Consumer create trace chunk command: relay_id = %s"
4394 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4395 ", chunk_override_name = %s"
4396 ", chunk_creation_timestamp = %s",
4397 relayd_id_str
, session_id
, chunk_id
,
4398 chunk_override_name
? : "(none)",
4399 creation_timestamp_str
);
4402 * The trace chunk registry, as used by the consumer daemon, implicitly
4403 * owns the trace chunks. This is only needed in the consumer since
4404 * the consumer has no notion of a session beyond session IDs being
4405 * used to identify other objects.
4407 * The lttng_trace_chunk_registry_publish() call below provides a
4408 * reference which is not released; it implicitly becomes the session
4409 * daemon's reference to the chunk in the consumer daemon.
4411 * The lifetime of trace chunks in the consumer daemon is managed by
4412 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4413 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4415 created_chunk
= lttng_trace_chunk_create(chunk_id
,
4416 chunk_creation_timestamp
);
4417 if (!created_chunk
) {
4418 ERR("Failed to create trace chunk");
4419 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4423 if (chunk_override_name
) {
4424 chunk_status
= lttng_trace_chunk_override_name(created_chunk
,
4425 chunk_override_name
);
4426 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4427 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4432 if (chunk_directory_handle
) {
4433 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
,
4435 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4436 ERR("Failed to set trace chunk credentials");
4437 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4441 * The consumer daemon has no ownership of the chunk output
4444 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
,
4445 chunk_directory_handle
);
4446 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4447 ERR("Failed to set trace chunk's directory handle");
4448 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4453 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4454 consumer_data
.chunk_registry
, session_id
,
4456 lttng_trace_chunk_put(created_chunk
);
4457 created_chunk
= NULL
;
4458 if (!published_chunk
) {
4459 ERR("Failed to publish trace chunk");
4460 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4465 cds_lfht_for_each_entry_duplicate(consumer_data
.channels_by_session_id_ht
->ht
,
4466 consumer_data
.channels_by_session_id_ht
->hash_fct(
4467 &session_id
, lttng_ht_seed
),
4468 consumer_data
.channels_by_session_id_ht
->match_fct
,
4469 &session_id
, &iter
.iter
, channel
,
4470 channels_by_session_id_ht_node
.node
) {
4471 ret
= lttng_consumer_channel_set_trace_chunk(channel
,
4475 * Roll-back the creation of this chunk.
4477 * This is important since the session daemon will
4478 * assume that the creation of this chunk failed and
4479 * will never ask for it to be closed, resulting
4480 * in a leak and an inconsistent state for some
4483 enum lttcomm_return_code close_ret
;
4484 char path
[LTTNG_PATH_MAX
];
4486 DBG("Failed to set new trace chunk on existing channels, rolling back");
4487 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4488 session_id
, chunk_id
,
4489 chunk_creation_timestamp
, NULL
,
4491 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4492 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4493 session_id
, chunk_id
);
4496 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4502 struct consumer_relayd_sock_pair
*relayd
;
4504 relayd
= consumer_find_relayd(*relayd_id
);
4506 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4507 ret
= relayd_create_trace_chunk(
4508 &relayd
->control_sock
, published_chunk
);
4509 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4511 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4514 if (!relayd
|| ret
) {
4515 enum lttcomm_return_code close_ret
;
4516 char path
[LTTNG_PATH_MAX
];
4518 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4521 chunk_creation_timestamp
,
4523 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4524 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4529 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4536 /* Release the reference returned by the "publish" operation. */
4537 lttng_trace_chunk_put(published_chunk
);
4538 lttng_trace_chunk_put(created_chunk
);
4542 enum lttcomm_return_code
lttng_consumer_close_trace_chunk(
4543 const uint64_t *relayd_id
, uint64_t session_id
,
4544 uint64_t chunk_id
, time_t chunk_close_timestamp
,
4545 const enum lttng_trace_chunk_command_type
*close_command
,
4548 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4549 struct lttng_trace_chunk
*chunk
;
4550 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4551 const char *relayd_id_str
= "(none)";
4552 const char *close_command_name
= "none";
4553 struct lttng_ht_iter iter
;
4554 struct lttng_consumer_channel
*channel
;
4555 enum lttng_trace_chunk_status chunk_status
;
4560 /* Only used for logging purposes. */
4561 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4562 "%" PRIu64
, *relayd_id
);
4563 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4564 relayd_id_str
= relayd_id_buffer
;
4566 relayd_id_str
= "(formatting error)";
4569 if (close_command
) {
4570 close_command_name
= lttng_trace_chunk_command_type_get_name(
4574 DBG("Consumer close trace chunk command: relayd_id = %s"
4575 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4576 ", close command = %s",
4577 relayd_id_str
, session_id
, chunk_id
,
4578 close_command_name
);
4580 chunk
= lttng_trace_chunk_registry_find_chunk(
4581 consumer_data
.chunk_registry
, session_id
, chunk_id
);
4583 ERR("Failed to find chunk: session_id = %" PRIu64
4584 ", chunk_id = %" PRIu64
,
4585 session_id
, chunk_id
);
4586 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4590 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
,
4591 chunk_close_timestamp
);
4592 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4593 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4597 if (close_command
) {
4598 chunk_status
= lttng_trace_chunk_set_close_command(
4599 chunk
, *close_command
);
4600 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4601 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4607 * chunk is now invalid to access as we no longer hold a reference to
4608 * it; it is only kept around to compare it (by address) to the
4609 * current chunk found in the session's channels.
4612 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
,
4613 channel
, node
.node
) {
4617 * Only change the channel's chunk to NULL if it still
4618 * references the chunk being closed. The channel may
4619 * reference a newer channel in the case of a session
4620 * rotation. When a session rotation occurs, the "next"
4621 * chunk is created before the "current" chunk is closed.
4623 if (channel
->trace_chunk
!= chunk
) {
4626 ret
= lttng_consumer_channel_set_trace_chunk(channel
, NULL
);
4629 * Attempt to close the chunk on as many channels as
4632 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4638 struct consumer_relayd_sock_pair
*relayd
;
4640 relayd
= consumer_find_relayd(*relayd_id
);
4642 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4643 ret
= relayd_close_trace_chunk(
4644 &relayd
->control_sock
, chunk
,
4646 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4648 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
,
4652 if (!relayd
|| ret
) {
4653 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4661 * Release the reference returned by the "find" operation and
4662 * the session daemon's implicit reference to the chunk.
4664 lttng_trace_chunk_put(chunk
);
4665 lttng_trace_chunk_put(chunk
);
4670 enum lttcomm_return_code
lttng_consumer_trace_chunk_exists(
4671 const uint64_t *relayd_id
, uint64_t session_id
,
4675 enum lttcomm_return_code ret_code
;
4676 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4677 const char *relayd_id_str
= "(none)";
4678 const bool is_local_trace
= !relayd_id
;
4679 struct consumer_relayd_sock_pair
*relayd
= NULL
;
4680 bool chunk_exists_local
, chunk_exists_remote
;
4685 /* Only used for logging purposes. */
4686 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4687 "%" PRIu64
, *relayd_id
);
4688 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4689 relayd_id_str
= relayd_id_buffer
;
4691 relayd_id_str
= "(formatting error)";
4695 DBG("Consumer trace chunk exists command: relayd_id = %s"
4696 ", chunk_id = %" PRIu64
, relayd_id_str
,
4698 ret
= lttng_trace_chunk_registry_chunk_exists(
4699 consumer_data
.chunk_registry
, session_id
,
4700 chunk_id
, &chunk_exists_local
);
4702 /* Internal error. */
4703 ERR("Failed to query the existence of a trace chunk");
4704 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
4707 DBG("Trace chunk %s locally",
4708 chunk_exists_local
? "exists" : "does not exist");
4709 if (chunk_exists_local
) {
4710 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
4712 } else if (is_local_trace
) {
4713 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4718 relayd
= consumer_find_relayd(*relayd_id
);
4720 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
4721 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
4722 goto end_rcu_unlock
;
4724 DBG("Looking up existence of trace chunk on relay daemon");
4725 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4726 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
,
4727 &chunk_exists_remote
);
4728 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4730 ERR("Failed to look-up the existence of trace chunk on relay daemon");
4731 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
4732 goto end_rcu_unlock
;
4735 ret_code
= chunk_exists_remote
?
4736 LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
4737 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4738 DBG("Trace chunk %s on relay daemon",
4739 chunk_exists_remote
? "exists" : "does not exist");