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.
27 #include <sys/socket.h>
28 #include <sys/types.h>
33 #include <common/common.h>
34 #include <common/utils.h>
35 #include <common/compat/poll.h>
36 #include <common/kernel-ctl/kernel-ctl.h>
37 #include <common/sessiond-comm/relayd.h>
38 #include <common/sessiond-comm/sessiond-comm.h>
39 #include <common/kernel-consumer/kernel-consumer.h>
40 #include <common/relayd/relayd.h>
41 #include <common/ust-consumer/ust-consumer.h>
45 struct lttng_consumer_global_data consumer_data
= {
48 .type
= LTTNG_CONSUMER_UNKNOWN
,
51 enum consumer_channel_action
{
54 CONSUMER_CHANNEL_QUIT
,
57 struct consumer_channel_msg
{
58 enum consumer_channel_action action
;
59 struct lttng_consumer_channel
*chan
; /* add */
60 uint64_t key
; /* del */
64 * Flag to inform the polling thread to quit when all fd hung up. Updated by
65 * the consumer_thread_receive_fds when it notices that all fds has hung up.
66 * Also updated by the signal handler (consumer_should_exit()). Read by the
69 volatile int consumer_quit
;
72 * Global hash table containing respectively metadata and data streams. The
73 * stream element in this ht should only be updated by the metadata poll thread
74 * for the metadata and the data poll thread for the data.
76 static struct lttng_ht
*metadata_ht
;
77 static struct lttng_ht
*data_ht
;
80 * Notify a thread lttng pipe to poll back again. This usually means that some
81 * global state has changed so we just send back the thread in a poll wait
84 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
86 struct lttng_consumer_stream
*null_stream
= NULL
;
90 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
93 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
94 struct lttng_consumer_channel
*chan
,
96 enum consumer_channel_action action
)
98 struct consumer_channel_msg msg
;
101 memset(&msg
, 0, sizeof(msg
));
107 ret
= write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
108 } while (ret
< 0 && errno
== EINTR
);
111 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
114 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
117 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
118 struct lttng_consumer_channel
**chan
,
120 enum consumer_channel_action
*action
)
122 struct consumer_channel_msg msg
;
126 ret
= read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
127 } while (ret
< 0 && errno
== EINTR
);
129 *action
= msg
.action
;
137 * Find a stream. The consumer_data.lock must be locked during this
140 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
143 struct lttng_ht_iter iter
;
144 struct lttng_ht_node_u64
*node
;
145 struct lttng_consumer_stream
*stream
= NULL
;
149 /* -1ULL keys are lookup failures */
150 if (key
== (uint64_t) -1ULL) {
156 lttng_ht_lookup(ht
, &key
, &iter
);
157 node
= lttng_ht_iter_get_node_u64(&iter
);
159 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
167 static void steal_stream_key(int key
, struct lttng_ht
*ht
)
169 struct lttng_consumer_stream
*stream
;
172 stream
= find_stream(key
, ht
);
176 * We don't want the lookup to match, but we still need
177 * to iterate on this stream when iterating over the hash table. Just
178 * change the node key.
180 stream
->node
.key
= -1ULL;
186 * Return a channel object for the given key.
188 * RCU read side lock MUST be acquired before calling this function and
189 * protects the channel ptr.
191 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
193 struct lttng_ht_iter iter
;
194 struct lttng_ht_node_u64
*node
;
195 struct lttng_consumer_channel
*channel
= NULL
;
197 /* -1ULL keys are lookup failures */
198 if (key
== (uint64_t) -1ULL) {
202 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
203 node
= lttng_ht_iter_get_node_u64(&iter
);
205 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
211 static void free_stream_rcu(struct rcu_head
*head
)
213 struct lttng_ht_node_u64
*node
=
214 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
215 struct lttng_consumer_stream
*stream
=
216 caa_container_of(node
, struct lttng_consumer_stream
, node
);
221 static void free_channel_rcu(struct rcu_head
*head
)
223 struct lttng_ht_node_u64
*node
=
224 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
225 struct lttng_consumer_channel
*channel
=
226 caa_container_of(node
, struct lttng_consumer_channel
, node
);
232 * RCU protected relayd socket pair free.
234 static void free_relayd_rcu(struct rcu_head
*head
)
236 struct lttng_ht_node_u64
*node
=
237 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
238 struct consumer_relayd_sock_pair
*relayd
=
239 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
242 * Close all sockets. This is done in the call RCU since we don't want the
243 * socket fds to be reassigned thus potentially creating bad state of the
246 * We do not have to lock the control socket mutex here since at this stage
247 * there is no one referencing to this relayd object.
249 (void) relayd_close(&relayd
->control_sock
);
250 (void) relayd_close(&relayd
->data_sock
);
256 * Destroy and free relayd socket pair object.
258 * This function MUST be called with the consumer_data lock acquired.
260 static void destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
263 struct lttng_ht_iter iter
;
265 if (relayd
== NULL
) {
269 DBG("Consumer destroy and close relayd socket pair");
271 iter
.iter
.node
= &relayd
->node
.node
;
272 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
274 /* We assume the relayd is being or is destroyed */
278 /* RCU free() call */
279 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
283 * Remove a channel from the global list protected by a mutex. This function is
284 * also responsible for freeing its data structures.
286 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
289 struct lttng_ht_iter iter
;
291 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
293 pthread_mutex_lock(&consumer_data
.lock
);
295 switch (consumer_data
.type
) {
296 case LTTNG_CONSUMER_KERNEL
:
298 case LTTNG_CONSUMER32_UST
:
299 case LTTNG_CONSUMER64_UST
:
300 lttng_ustconsumer_del_channel(channel
);
303 ERR("Unknown consumer_data type");
309 iter
.iter
.node
= &channel
->node
.node
;
310 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
314 call_rcu(&channel
->node
.head
, free_channel_rcu
);
316 pthread_mutex_unlock(&consumer_data
.lock
);
320 * Iterate over the relayd hash table and destroy each element. Finally,
321 * destroy the whole hash table.
323 static void cleanup_relayd_ht(void)
325 struct lttng_ht_iter iter
;
326 struct consumer_relayd_sock_pair
*relayd
;
330 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
332 destroy_relayd(relayd
);
337 lttng_ht_destroy(consumer_data
.relayd_ht
);
341 * Update the end point status of all streams having the given network sequence
342 * index (relayd index).
344 * It's atomically set without having the stream mutex locked which is fine
345 * because we handle the write/read race with a pipe wakeup for each thread.
347 static void update_endpoint_status_by_netidx(int net_seq_idx
,
348 enum consumer_endpoint_status status
)
350 struct lttng_ht_iter iter
;
351 struct lttng_consumer_stream
*stream
;
353 DBG("Consumer set delete flag on stream by idx %d", net_seq_idx
);
357 /* Let's begin with metadata */
358 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
359 if (stream
->net_seq_idx
== net_seq_idx
) {
360 uatomic_set(&stream
->endpoint_status
, status
);
361 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
365 /* Follow up by the data streams */
366 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
367 if (stream
->net_seq_idx
== net_seq_idx
) {
368 uatomic_set(&stream
->endpoint_status
, status
);
369 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
376 * Cleanup a relayd object by flagging every associated streams for deletion,
377 * destroying the object meaning removing it from the relayd hash table,
378 * closing the sockets and freeing the memory in a RCU call.
380 * If a local data context is available, notify the threads that the streams'
381 * state have changed.
383 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
384 struct lttng_consumer_local_data
*ctx
)
390 DBG("Cleaning up relayd sockets");
392 /* Save the net sequence index before destroying the object */
393 netidx
= relayd
->net_seq_idx
;
396 * Delete the relayd from the relayd hash table, close the sockets and free
397 * the object in a RCU call.
399 destroy_relayd(relayd
);
401 /* Set inactive endpoint to all streams */
402 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
405 * With a local data context, notify the threads that the streams' state
406 * have changed. The write() action on the pipe acts as an "implicit"
407 * memory barrier ordering the updates of the end point status from the
408 * read of this status which happens AFTER receiving this notify.
411 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
412 notify_thread_lttng_pipe(ctx
->consumer_metadata_pipe
);
417 * Flag a relayd socket pair for destruction. Destroy it if the refcount
420 * RCU read side lock MUST be aquired before calling this function.
422 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
426 /* Set destroy flag for this object */
427 uatomic_set(&relayd
->destroy_flag
, 1);
429 /* Destroy the relayd if refcount is 0 */
430 if (uatomic_read(&relayd
->refcount
) == 0) {
431 destroy_relayd(relayd
);
436 * Remove a stream from the global list protected by a mutex. This
437 * function is also responsible for freeing its data structures.
439 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
443 struct lttng_ht_iter iter
;
444 struct lttng_consumer_channel
*free_chan
= NULL
;
445 struct consumer_relayd_sock_pair
*relayd
;
449 DBG("Consumer del stream %d", stream
->wait_fd
);
452 /* Means the stream was allocated but not successfully added */
453 goto free_stream_rcu
;
456 pthread_mutex_lock(&consumer_data
.lock
);
457 pthread_mutex_lock(&stream
->lock
);
459 switch (consumer_data
.type
) {
460 case LTTNG_CONSUMER_KERNEL
:
461 if (stream
->mmap_base
!= NULL
) {
462 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
468 if (stream
->wait_fd
>= 0) {
469 ret
= close(stream
->wait_fd
);
475 case LTTNG_CONSUMER32_UST
:
476 case LTTNG_CONSUMER64_UST
:
477 lttng_ustconsumer_del_stream(stream
);
480 ERR("Unknown consumer_data type");
486 iter
.iter
.node
= &stream
->node
.node
;
487 ret
= lttng_ht_del(ht
, &iter
);
490 iter
.iter
.node
= &stream
->node_channel_id
.node
;
491 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
494 iter
.iter
.node
= &stream
->node_session_id
.node
;
495 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
499 assert(consumer_data
.stream_count
> 0);
500 consumer_data
.stream_count
--;
502 if (stream
->out_fd
>= 0) {
503 ret
= close(stream
->out_fd
);
509 /* Check and cleanup relayd */
511 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
512 if (relayd
!= NULL
) {
513 uatomic_dec(&relayd
->refcount
);
514 assert(uatomic_read(&relayd
->refcount
) >= 0);
516 /* Closing streams requires to lock the control socket. */
517 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
518 ret
= relayd_send_close_stream(&relayd
->control_sock
,
519 stream
->relayd_stream_id
,
520 stream
->next_net_seq_num
- 1);
521 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
523 DBG("Unable to close stream on the relayd. Continuing");
525 * Continue here. There is nothing we can do for the relayd.
526 * Chances are that the relayd has closed the socket so we just
527 * continue cleaning up.
531 /* Both conditions are met, we destroy the relayd. */
532 if (uatomic_read(&relayd
->refcount
) == 0 &&
533 uatomic_read(&relayd
->destroy_flag
)) {
534 destroy_relayd(relayd
);
539 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
540 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
541 free_chan
= stream
->chan
;
545 consumer_data
.need_update
= 1;
546 pthread_mutex_unlock(&stream
->lock
);
547 pthread_mutex_unlock(&consumer_data
.lock
);
550 consumer_del_channel(free_chan
);
554 call_rcu(&stream
->node
.head
, free_stream_rcu
);
557 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
559 enum lttng_consumer_stream_state state
,
560 const char *channel_name
,
567 enum consumer_channel_type type
)
570 struct lttng_consumer_stream
*stream
;
572 stream
= zmalloc(sizeof(*stream
));
573 if (stream
== NULL
) {
574 PERROR("malloc struct lttng_consumer_stream");
581 stream
->key
= stream_key
;
583 stream
->out_fd_offset
= 0;
584 stream
->state
= state
;
587 stream
->net_seq_idx
= relayd_id
;
588 stream
->session_id
= session_id
;
589 pthread_mutex_init(&stream
->lock
, NULL
);
591 /* If channel is the metadata, flag this stream as metadata. */
592 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
593 stream
->metadata_flag
= 1;
594 /* Metadata is flat out. */
595 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
597 /* Format stream name to <channel_name>_<cpu_number> */
598 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
601 PERROR("snprintf stream name");
606 /* Key is always the wait_fd for streams. */
607 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
609 /* Init node per channel id key */
610 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
612 /* Init session id node with the stream session id */
613 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
615 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
" relayd_id %" PRIu64
", session_id %" PRIu64
,
616 stream
->name
, stream
->key
, channel_key
, stream
->net_seq_idx
, stream
->session_id
);
632 * Add a stream to the global list protected by a mutex.
634 static int add_stream(struct lttng_consumer_stream
*stream
,
638 struct consumer_relayd_sock_pair
*relayd
;
643 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
645 pthread_mutex_lock(&consumer_data
.lock
);
646 pthread_mutex_lock(&stream
->lock
);
649 /* Steal stream identifier to avoid having streams with the same key */
650 steal_stream_key(stream
->key
, ht
);
652 lttng_ht_add_unique_u64(ht
, &stream
->node
);
654 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
655 &stream
->node_channel_id
);
658 * Add stream to the stream_list_ht of the consumer data. No need to steal
659 * the key since the HT does not use it and we allow to add redundant keys
662 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
664 /* Check and cleanup relayd */
665 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
666 if (relayd
!= NULL
) {
667 uatomic_inc(&relayd
->refcount
);
671 * When nb_init_stream_left reaches 0, we don't need to trigger any action
672 * in terms of destroying the associated channel, because the action that
673 * causes the count to become 0 also causes a stream to be added. The
674 * channel deletion will thus be triggered by the following removal of this
677 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
678 /* Increment refcount before decrementing nb_init_stream_left */
680 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
683 /* Update consumer data once the node is inserted. */
684 consumer_data
.stream_count
++;
685 consumer_data
.need_update
= 1;
688 pthread_mutex_unlock(&stream
->lock
);
689 pthread_mutex_unlock(&consumer_data
.lock
);
695 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
696 * be acquired before calling this.
698 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
701 struct lttng_ht_node_u64
*node
;
702 struct lttng_ht_iter iter
;
706 lttng_ht_lookup(consumer_data
.relayd_ht
,
707 &relayd
->net_seq_idx
, &iter
);
708 node
= lttng_ht_iter_get_node_u64(&iter
);
712 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
719 * Allocate and return a consumer relayd socket.
721 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
724 struct consumer_relayd_sock_pair
*obj
= NULL
;
726 /* Negative net sequence index is a failure */
727 if (net_seq_idx
< 0) {
731 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
733 PERROR("zmalloc relayd sock");
737 obj
->net_seq_idx
= net_seq_idx
;
739 obj
->destroy_flag
= 0;
740 obj
->control_sock
.sock
.fd
= -1;
741 obj
->data_sock
.sock
.fd
= -1;
742 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
743 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
750 * Find a relayd socket pair in the global consumer data.
752 * Return the object if found else NULL.
753 * RCU read-side lock must be held across this call and while using the
756 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
758 struct lttng_ht_iter iter
;
759 struct lttng_ht_node_u64
*node
;
760 struct consumer_relayd_sock_pair
*relayd
= NULL
;
762 /* Negative keys are lookup failures */
763 if (key
== (uint64_t) -1ULL) {
767 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
769 node
= lttng_ht_iter_get_node_u64(&iter
);
771 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
779 * Handle stream for relayd transmission if the stream applies for network
780 * streaming where the net sequence index is set.
782 * Return destination file descriptor or negative value on error.
784 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
785 size_t data_size
, unsigned long padding
,
786 struct consumer_relayd_sock_pair
*relayd
)
789 struct lttcomm_relayd_data_hdr data_hdr
;
795 /* Reset data header */
796 memset(&data_hdr
, 0, sizeof(data_hdr
));
798 if (stream
->metadata_flag
) {
799 /* Caller MUST acquire the relayd control socket lock */
800 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
805 /* Metadata are always sent on the control socket. */
806 outfd
= relayd
->control_sock
.sock
.fd
;
808 /* Set header with stream information */
809 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
810 data_hdr
.data_size
= htobe32(data_size
);
811 data_hdr
.padding_size
= htobe32(padding
);
813 * Note that net_seq_num below is assigned with the *current* value of
814 * next_net_seq_num and only after that the next_net_seq_num will be
815 * increment. This is why when issuing a command on the relayd using
816 * this next value, 1 should always be substracted in order to compare
817 * the last seen sequence number on the relayd side to the last sent.
819 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
820 /* Other fields are zeroed previously */
822 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
828 ++stream
->next_net_seq_num
;
830 /* Set to go on data socket */
831 outfd
= relayd
->data_sock
.sock
.fd
;
839 * Allocate and return a new lttng_consumer_channel object using the given key
840 * to initialize the hash table node.
842 * On error, return NULL.
844 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
846 const char *pathname
,
851 enum lttng_event_output output
,
852 uint64_t tracefile_size
,
853 uint64_t tracefile_count
)
855 struct lttng_consumer_channel
*channel
;
857 channel
= zmalloc(sizeof(*channel
));
858 if (channel
== NULL
) {
859 PERROR("malloc struct lttng_consumer_channel");
864 channel
->refcount
= 0;
865 channel
->session_id
= session_id
;
868 channel
->relayd_id
= relayd_id
;
869 channel
->output
= output
;
870 channel
->tracefile_size
= tracefile_size
;
871 channel
->tracefile_count
= tracefile_count
;
873 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
874 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
876 strncpy(channel
->name
, name
, sizeof(channel
->name
));
877 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
879 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
881 channel
->wait_fd
= -1;
883 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
885 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
892 * Add a channel to the global list protected by a mutex.
894 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
895 struct lttng_consumer_local_data
*ctx
)
898 struct lttng_ht_node_u64
*node
;
899 struct lttng_ht_iter iter
;
901 pthread_mutex_lock(&consumer_data
.lock
);
904 lttng_ht_lookup(consumer_data
.channel_ht
, &channel
->key
, &iter
);
905 node
= lttng_ht_iter_get_node_u64(&iter
);
907 /* Channel already exist. Ignore the insertion */
908 ERR("Consumer add channel key %" PRIu64
" already exists!",
910 ret
= LTTNG_ERR_KERN_CHAN_EXIST
;
914 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
918 pthread_mutex_unlock(&consumer_data
.lock
);
920 if (!ret
&& channel
->wait_fd
!= -1 &&
921 channel
->metadata_stream
== NULL
) {
922 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
928 * Allocate the pollfd structure and the local view of the out fds to avoid
929 * doing a lookup in the linked list and concurrency issues when writing is
930 * needed. Called with consumer_data.lock held.
932 * Returns the number of fds in the structures.
934 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
935 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
939 struct lttng_ht_iter iter
;
940 struct lttng_consumer_stream
*stream
;
945 assert(local_stream
);
947 DBG("Updating poll fd array");
949 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
951 * Only active streams with an active end point can be added to the
952 * poll set and local stream storage of the thread.
954 * There is a potential race here for endpoint_status to be updated
955 * just after the check. However, this is OK since the stream(s) will
956 * be deleted once the thread is notified that the end point state has
957 * changed where this function will be called back again.
959 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
960 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
964 * This clobbers way too much the debug output. Uncomment that if you
965 * need it for debugging purposes.
967 * DBG("Active FD %d", stream->wait_fd);
969 (*pollfd
)[i
].fd
= stream
->wait_fd
;
970 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
971 local_stream
[i
] = stream
;
977 * Insert the consumer_data_pipe at the end of the array and don't
978 * increment i so nb_fd is the number of real FD.
980 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
981 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
986 * Poll on the should_quit pipe and the command socket return -1 on error and
987 * should exit, 0 if data is available on the command socket
989 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
994 num_rdy
= poll(consumer_sockpoll
, 2, -1);
997 * Restart interrupted system call.
999 if (errno
== EINTR
) {
1002 PERROR("Poll error");
1005 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1006 DBG("consumer_should_quit wake up");
1016 * Set the error socket.
1018 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1021 ctx
->consumer_error_socket
= sock
;
1025 * Set the command socket path.
1027 void lttng_consumer_set_command_sock_path(
1028 struct lttng_consumer_local_data
*ctx
, char *sock
)
1030 ctx
->consumer_command_sock_path
= sock
;
1034 * Send return code to the session daemon.
1035 * If the socket is not defined, we return 0, it is not a fatal error
1037 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1039 if (ctx
->consumer_error_socket
> 0) {
1040 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1041 sizeof(enum lttcomm_sessiond_command
));
1048 * Close all the tracefiles and stream fds and MUST be called when all
1049 * instances are destroyed i.e. when all threads were joined and are ended.
1051 void lttng_consumer_cleanup(void)
1053 struct lttng_ht_iter iter
;
1054 struct lttng_consumer_channel
*channel
;
1058 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1060 consumer_del_channel(channel
);
1065 lttng_ht_destroy(consumer_data
.channel_ht
);
1067 cleanup_relayd_ht();
1069 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1072 * This HT contains streams that are freed by either the metadata thread or
1073 * the data thread so we do *nothing* on the hash table and simply destroy
1076 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1080 * Called from signal handler.
1082 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1087 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1088 } while (ret
< 0 && errno
== EINTR
);
1089 if (ret
< 0 || ret
!= 1) {
1090 PERROR("write consumer quit");
1093 DBG("Consumer flag that it should quit");
1096 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1099 int outfd
= stream
->out_fd
;
1102 * This does a blocking write-and-wait on any page that belongs to the
1103 * subbuffer prior to the one we just wrote.
1104 * Don't care about error values, as these are just hints and ways to
1105 * limit the amount of page cache used.
1107 if (orig_offset
< stream
->max_sb_size
) {
1110 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1111 stream
->max_sb_size
,
1112 SYNC_FILE_RANGE_WAIT_BEFORE
1113 | SYNC_FILE_RANGE_WRITE
1114 | SYNC_FILE_RANGE_WAIT_AFTER
);
1116 * Give hints to the kernel about how we access the file:
1117 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1120 * We need to call fadvise again after the file grows because the
1121 * kernel does not seem to apply fadvise to non-existing parts of the
1124 * Call fadvise _after_ having waited for the page writeback to
1125 * complete because the dirty page writeback semantic is not well
1126 * defined. So it can be expected to lead to lower throughput in
1129 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1130 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1134 * Initialise the necessary environnement :
1135 * - create a new context
1136 * - create the poll_pipe
1137 * - create the should_quit pipe (for signal handler)
1138 * - create the thread pipe (for splice)
1140 * Takes a function pointer as argument, this function is called when data is
1141 * available on a buffer. This function is responsible to do the
1142 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1143 * buffer configuration and then kernctl_put_next_subbuf at the end.
1145 * Returns a pointer to the new context or NULL on error.
1147 struct lttng_consumer_local_data
*lttng_consumer_create(
1148 enum lttng_consumer_type type
,
1149 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1150 struct lttng_consumer_local_data
*ctx
),
1151 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1152 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1153 int (*update_stream
)(int stream_key
, uint32_t state
))
1156 struct lttng_consumer_local_data
*ctx
;
1158 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1159 consumer_data
.type
== type
);
1160 consumer_data
.type
= type
;
1162 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1164 PERROR("allocating context");
1168 ctx
->consumer_error_socket
= -1;
1169 ctx
->consumer_metadata_socket
= -1;
1170 /* assign the callbacks */
1171 ctx
->on_buffer_ready
= buffer_ready
;
1172 ctx
->on_recv_channel
= recv_channel
;
1173 ctx
->on_recv_stream
= recv_stream
;
1174 ctx
->on_update_stream
= update_stream
;
1176 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1177 if (!ctx
->consumer_data_pipe
) {
1178 goto error_poll_pipe
;
1181 ret
= pipe(ctx
->consumer_should_quit
);
1183 PERROR("Error creating recv pipe");
1184 goto error_quit_pipe
;
1187 ret
= pipe(ctx
->consumer_thread_pipe
);
1189 PERROR("Error creating thread pipe");
1190 goto error_thread_pipe
;
1193 ret
= pipe(ctx
->consumer_channel_pipe
);
1195 PERROR("Error creating channel pipe");
1196 goto error_channel_pipe
;
1199 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1200 if (!ctx
->consumer_metadata_pipe
) {
1201 goto error_metadata_pipe
;
1204 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1206 goto error_splice_pipe
;
1212 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1213 error_metadata_pipe
:
1214 utils_close_pipe(ctx
->consumer_channel_pipe
);
1216 utils_close_pipe(ctx
->consumer_thread_pipe
);
1218 utils_close_pipe(ctx
->consumer_should_quit
);
1220 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1228 * Close all fds associated with the instance and free the context.
1230 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1234 DBG("Consumer destroying it. Closing everything.");
1236 ret
= close(ctx
->consumer_error_socket
);
1240 ret
= close(ctx
->consumer_metadata_socket
);
1244 utils_close_pipe(ctx
->consumer_thread_pipe
);
1245 utils_close_pipe(ctx
->consumer_channel_pipe
);
1246 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1247 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1248 utils_close_pipe(ctx
->consumer_should_quit
);
1249 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1251 unlink(ctx
->consumer_command_sock_path
);
1256 * Write the metadata stream id on the specified file descriptor.
1258 static int write_relayd_metadata_id(int fd
,
1259 struct lttng_consumer_stream
*stream
,
1260 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1263 struct lttcomm_relayd_metadata_payload hdr
;
1265 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1266 hdr
.padding_size
= htobe32(padding
);
1268 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1269 } while (ret
< 0 && errno
== EINTR
);
1270 if (ret
< 0 || ret
!= sizeof(hdr
)) {
1272 * This error means that the fd's end is closed so ignore the perror
1273 * not to clubber the error output since this can happen in a normal
1276 if (errno
!= EPIPE
) {
1277 PERROR("write metadata stream id");
1279 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1281 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1282 * handle writting the missing part so report that as an error and
1283 * don't lie to the caller.
1288 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1289 stream
->relayd_stream_id
, padding
);
1296 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1297 * core function for writing trace buffers to either the local filesystem or
1300 * It must be called with the stream lock held.
1302 * Careful review MUST be put if any changes occur!
1304 * Returns the number of bytes written
1306 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1307 struct lttng_consumer_local_data
*ctx
,
1308 struct lttng_consumer_stream
*stream
, unsigned long len
,
1309 unsigned long padding
)
1311 unsigned long mmap_offset
;
1313 ssize_t ret
= 0, written
= 0;
1314 off_t orig_offset
= stream
->out_fd_offset
;
1315 /* Default is on the disk */
1316 int outfd
= stream
->out_fd
;
1317 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1318 unsigned int relayd_hang_up
= 0;
1320 /* RCU lock for the relayd pointer */
1323 /* Flag that the current stream if set for network streaming. */
1324 if (stream
->net_seq_idx
!= -1) {
1325 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1326 if (relayd
== NULL
) {
1331 /* get the offset inside the fd to mmap */
1332 switch (consumer_data
.type
) {
1333 case LTTNG_CONSUMER_KERNEL
:
1334 mmap_base
= stream
->mmap_base
;
1335 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1337 case LTTNG_CONSUMER32_UST
:
1338 case LTTNG_CONSUMER64_UST
:
1339 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1341 ERR("read mmap get mmap base for stream %s", stream
->name
);
1345 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1349 ERR("Unknown consumer_data type");
1354 PERROR("tracer ctl get_mmap_read_offset");
1359 /* Handle stream on the relayd if the output is on the network */
1361 unsigned long netlen
= len
;
1364 * Lock the control socket for the complete duration of the function
1365 * since from this point on we will use the socket.
1367 if (stream
->metadata_flag
) {
1368 /* Metadata requires the control socket. */
1369 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1370 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1373 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1375 /* Use the returned socket. */
1378 /* Write metadata stream id before payload */
1379 if (stream
->metadata_flag
) {
1380 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1383 /* Socket operation failed. We consider the relayd dead */
1384 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1392 /* Socket operation failed. We consider the relayd dead */
1393 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1397 /* Else, use the default set before which is the filesystem. */
1400 /* No streaming, we have to set the len with the full padding */
1404 * Check if we need to change the tracefile before writing the packet.
1406 if (stream
->chan
->tracefile_size
> 0 &&
1407 (stream
->tracefile_size_current
+ len
) >
1408 stream
->chan
->tracefile_size
) {
1409 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1410 stream
->name
, stream
->chan
->tracefile_size
,
1411 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1412 stream
->out_fd
, &(stream
->tracefile_count_current
));
1414 ERR("Rotating output file");
1417 outfd
= stream
->out_fd
= ret
;
1418 /* Reset current size because we just perform a rotation. */
1419 stream
->tracefile_size_current
= 0;
1421 stream
->tracefile_size_current
+= len
;
1426 ret
= write(outfd
, mmap_base
+ mmap_offset
, len
);
1427 } while (ret
< 0 && errno
== EINTR
);
1428 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1431 * This is possible if the fd is closed on the other side (outfd)
1432 * or any write problem. It can be verbose a bit for a normal
1433 * execution if for instance the relayd is stopped abruptly. This
1434 * can happen so set this to a DBG statement.
1436 DBG("Error in file write mmap");
1440 /* Socket operation failed. We consider the relayd dead */
1441 if (errno
== EPIPE
|| errno
== EINVAL
) {
1446 } else if (ret
> len
) {
1447 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1455 /* This call is useless on a socket so better save a syscall. */
1457 /* This won't block, but will start writeout asynchronously */
1458 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1459 SYNC_FILE_RANGE_WRITE
);
1460 stream
->out_fd_offset
+= ret
;
1464 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1468 * This is a special case that the relayd has closed its socket. Let's
1469 * cleanup the relayd object and all associated streams.
1471 if (relayd
&& relayd_hang_up
) {
1472 cleanup_relayd(relayd
, ctx
);
1476 /* Unlock only if ctrl socket used */
1477 if (relayd
&& stream
->metadata_flag
) {
1478 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1486 * Splice the data from the ring buffer to the tracefile.
1488 * It must be called with the stream lock held.
1490 * Returns the number of bytes spliced.
1492 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1493 struct lttng_consumer_local_data
*ctx
,
1494 struct lttng_consumer_stream
*stream
, unsigned long len
,
1495 unsigned long padding
)
1497 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1499 off_t orig_offset
= stream
->out_fd_offset
;
1500 int fd
= stream
->wait_fd
;
1501 /* Default is on the disk */
1502 int outfd
= stream
->out_fd
;
1503 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1505 unsigned int relayd_hang_up
= 0;
1507 switch (consumer_data
.type
) {
1508 case LTTNG_CONSUMER_KERNEL
:
1510 case LTTNG_CONSUMER32_UST
:
1511 case LTTNG_CONSUMER64_UST
:
1512 /* Not supported for user space tracing */
1515 ERR("Unknown consumer_data type");
1519 /* RCU lock for the relayd pointer */
1522 /* Flag that the current stream if set for network streaming. */
1523 if (stream
->net_seq_idx
!= -1) {
1524 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1525 if (relayd
== NULL
) {
1531 * Choose right pipe for splice. Metadata and trace data are handled by
1532 * different threads hence the use of two pipes in order not to race or
1533 * corrupt the written data.
1535 if (stream
->metadata_flag
) {
1536 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1538 splice_pipe
= ctx
->consumer_thread_pipe
;
1541 /* Write metadata stream id before payload */
1543 int total_len
= len
;
1545 if (stream
->metadata_flag
) {
1547 * Lock the control socket for the complete duration of the function
1548 * since from this point on we will use the socket.
1550 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1552 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1556 /* Socket operation failed. We consider the relayd dead */
1557 if (ret
== -EBADF
) {
1558 WARN("Remote relayd disconnected. Stopping");
1565 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1568 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1570 /* Use the returned socket. */
1573 /* Socket operation failed. We consider the relayd dead */
1574 if (ret
== -EBADF
) {
1575 WARN("Remote relayd disconnected. Stopping");
1582 /* No streaming, we have to set the len with the full padding */
1586 * Check if we need to change the tracefile before writing the packet.
1588 if (stream
->chan
->tracefile_size
> 0 &&
1589 (stream
->tracefile_size_current
+ len
) >
1590 stream
->chan
->tracefile_size
) {
1591 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1592 stream
->name
, stream
->chan
->tracefile_size
,
1593 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1594 stream
->out_fd
, &(stream
->tracefile_count_current
));
1596 ERR("Rotating output file");
1599 outfd
= stream
->out_fd
= ret
;
1600 /* Reset current size because we just perform a rotation. */
1601 stream
->tracefile_size_current
= 0;
1603 stream
->tracefile_size_current
+= len
;
1607 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1608 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1609 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1610 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1611 DBG("splice chan to pipe, ret %zd", ret_splice
);
1612 if (ret_splice
< 0) {
1613 PERROR("Error in relay splice");
1615 written
= ret_splice
;
1621 /* Handle stream on the relayd if the output is on the network */
1623 if (stream
->metadata_flag
) {
1624 size_t metadata_payload_size
=
1625 sizeof(struct lttcomm_relayd_metadata_payload
);
1627 /* Update counter to fit the spliced data */
1628 ret_splice
+= metadata_payload_size
;
1629 len
+= metadata_payload_size
;
1631 * We do this so the return value can match the len passed as
1632 * argument to this function.
1634 written
-= metadata_payload_size
;
1638 /* Splice data out */
1639 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1640 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1641 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1642 if (ret_splice
< 0) {
1643 PERROR("Error in file splice");
1645 written
= ret_splice
;
1647 /* Socket operation failed. We consider the relayd dead */
1648 if (errno
== EBADF
|| errno
== EPIPE
) {
1649 WARN("Remote relayd disconnected. Stopping");
1655 } else if (ret_splice
> len
) {
1657 PERROR("Wrote more data than requested %zd (len: %lu)",
1659 written
+= ret_splice
;
1665 /* This call is useless on a socket so better save a syscall. */
1667 /* This won't block, but will start writeout asynchronously */
1668 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1669 SYNC_FILE_RANGE_WRITE
);
1670 stream
->out_fd_offset
+= ret_splice
;
1672 written
+= ret_splice
;
1674 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1682 * This is a special case that the relayd has closed its socket. Let's
1683 * cleanup the relayd object and all associated streams.
1685 if (relayd
&& relayd_hang_up
) {
1686 cleanup_relayd(relayd
, ctx
);
1687 /* Skip splice error so the consumer does not fail */
1692 /* send the appropriate error description to sessiond */
1695 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1698 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1701 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1706 if (relayd
&& stream
->metadata_flag
) {
1707 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1715 * Take a snapshot for a specific fd
1717 * Returns 0 on success, < 0 on error
1719 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1721 switch (consumer_data
.type
) {
1722 case LTTNG_CONSUMER_KERNEL
:
1723 return lttng_kconsumer_take_snapshot(stream
);
1724 case LTTNG_CONSUMER32_UST
:
1725 case LTTNG_CONSUMER64_UST
:
1726 return lttng_ustconsumer_take_snapshot(stream
);
1728 ERR("Unknown consumer_data type");
1735 * Get the produced position
1737 * Returns 0 on success, < 0 on error
1739 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1742 switch (consumer_data
.type
) {
1743 case LTTNG_CONSUMER_KERNEL
:
1744 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1745 case LTTNG_CONSUMER32_UST
:
1746 case LTTNG_CONSUMER64_UST
:
1747 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1749 ERR("Unknown consumer_data type");
1755 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1756 int sock
, struct pollfd
*consumer_sockpoll
)
1758 switch (consumer_data
.type
) {
1759 case LTTNG_CONSUMER_KERNEL
:
1760 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1761 case LTTNG_CONSUMER32_UST
:
1762 case LTTNG_CONSUMER64_UST
:
1763 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1765 ERR("Unknown consumer_data type");
1772 * Iterate over all streams of the hashtable and free them properly.
1774 * WARNING: *MUST* be used with data stream only.
1776 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1778 struct lttng_ht_iter iter
;
1779 struct lttng_consumer_stream
*stream
;
1786 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1788 * Ignore return value since we are currently cleaning up so any error
1791 (void) consumer_del_stream(stream
, ht
);
1795 lttng_ht_destroy(ht
);
1799 * Iterate over all streams of the hashtable and free them properly.
1801 * XXX: Should not be only for metadata stream or else use an other name.
1803 static void destroy_stream_ht(struct lttng_ht
*ht
)
1805 struct lttng_ht_iter iter
;
1806 struct lttng_consumer_stream
*stream
;
1813 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1815 * Ignore return value since we are currently cleaning up so any error
1818 (void) consumer_del_metadata_stream(stream
, ht
);
1822 lttng_ht_destroy(ht
);
1825 void lttng_consumer_close_metadata(void)
1827 switch (consumer_data
.type
) {
1828 case LTTNG_CONSUMER_KERNEL
:
1830 * The Kernel consumer has a different metadata scheme so we don't
1831 * close anything because the stream will be closed by the session
1835 case LTTNG_CONSUMER32_UST
:
1836 case LTTNG_CONSUMER64_UST
:
1838 * Close all metadata streams. The metadata hash table is passed and
1839 * this call iterates over it by closing all wakeup fd. This is safe
1840 * because at this point we are sure that the metadata producer is
1841 * either dead or blocked.
1843 lttng_ustconsumer_close_metadata(metadata_ht
);
1846 ERR("Unknown consumer_data type");
1852 * Clean up a metadata stream and free its memory.
1854 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1855 struct lttng_ht
*ht
)
1858 struct lttng_ht_iter iter
;
1859 struct lttng_consumer_channel
*free_chan
= NULL
;
1860 struct consumer_relayd_sock_pair
*relayd
;
1864 * This call should NEVER receive regular stream. It must always be
1865 * metadata stream and this is crucial for data structure synchronization.
1867 assert(stream
->metadata_flag
);
1869 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1872 /* Means the stream was allocated but not successfully added */
1873 goto free_stream_rcu
;
1876 pthread_mutex_lock(&consumer_data
.lock
);
1877 pthread_mutex_lock(&stream
->lock
);
1879 switch (consumer_data
.type
) {
1880 case LTTNG_CONSUMER_KERNEL
:
1881 if (stream
->mmap_base
!= NULL
) {
1882 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1884 PERROR("munmap metadata stream");
1888 if (stream
->wait_fd
>= 0) {
1889 ret
= close(stream
->wait_fd
);
1891 PERROR("close kernel metadata wait_fd");
1895 case LTTNG_CONSUMER32_UST
:
1896 case LTTNG_CONSUMER64_UST
:
1897 lttng_ustconsumer_del_stream(stream
);
1900 ERR("Unknown consumer_data type");
1906 iter
.iter
.node
= &stream
->node
.node
;
1907 ret
= lttng_ht_del(ht
, &iter
);
1910 iter
.iter
.node
= &stream
->node_channel_id
.node
;
1911 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
1914 iter
.iter
.node
= &stream
->node_session_id
.node
;
1915 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1919 if (stream
->out_fd
>= 0) {
1920 ret
= close(stream
->out_fd
);
1926 /* Check and cleanup relayd */
1928 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1929 if (relayd
!= NULL
) {
1930 uatomic_dec(&relayd
->refcount
);
1931 assert(uatomic_read(&relayd
->refcount
) >= 0);
1933 /* Closing streams requires to lock the control socket. */
1934 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1935 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1936 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1937 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1939 DBG("Unable to close stream on the relayd. Continuing");
1941 * Continue here. There is nothing we can do for the relayd.
1942 * Chances are that the relayd has closed the socket so we just
1943 * continue cleaning up.
1947 /* Both conditions are met, we destroy the relayd. */
1948 if (uatomic_read(&relayd
->refcount
) == 0 &&
1949 uatomic_read(&relayd
->destroy_flag
)) {
1950 destroy_relayd(relayd
);
1955 /* Atomically decrement channel refcount since other threads can use it. */
1956 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
1957 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
1958 /* Go for channel deletion! */
1959 free_chan
= stream
->chan
;
1963 pthread_mutex_unlock(&stream
->lock
);
1964 pthread_mutex_unlock(&consumer_data
.lock
);
1967 consumer_del_channel(free_chan
);
1971 call_rcu(&stream
->node
.head
, free_stream_rcu
);
1975 * Action done with the metadata stream when adding it to the consumer internal
1976 * data structures to handle it.
1978 static int add_metadata_stream(struct lttng_consumer_stream
*stream
,
1979 struct lttng_ht
*ht
)
1982 struct consumer_relayd_sock_pair
*relayd
;
1983 struct lttng_ht_iter iter
;
1984 struct lttng_ht_node_u64
*node
;
1989 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
1991 pthread_mutex_lock(&consumer_data
.lock
);
1992 pthread_mutex_lock(&stream
->lock
);
1995 * From here, refcounts are updated so be _careful_ when returning an error
2002 * Lookup the stream just to make sure it does not exist in our internal
2003 * state. This should NEVER happen.
2005 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2006 node
= lttng_ht_iter_get_node_u64(&iter
);
2009 /* Find relayd and, if one is found, increment refcount. */
2010 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
2011 if (relayd
!= NULL
) {
2012 uatomic_inc(&relayd
->refcount
);
2015 /* Update channel refcount once added without error(s). */
2016 uatomic_inc(&stream
->chan
->refcount
);
2019 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2020 * in terms of destroying the associated channel, because the action that
2021 * causes the count to become 0 also causes a stream to be added. The
2022 * channel deletion will thus be triggered by the following removal of this
2025 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2026 /* Increment refcount before decrementing nb_init_stream_left */
2028 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2031 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2033 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2034 &stream
->node_channel_id
);
2037 * Add stream to the stream_list_ht of the consumer data. No need to steal
2038 * the key since the HT does not use it and we allow to add redundant keys
2041 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2045 pthread_mutex_unlock(&stream
->lock
);
2046 pthread_mutex_unlock(&consumer_data
.lock
);
2051 * Delete data stream that are flagged for deletion (endpoint_status).
2053 static void validate_endpoint_status_data_stream(void)
2055 struct lttng_ht_iter iter
;
2056 struct lttng_consumer_stream
*stream
;
2058 DBG("Consumer delete flagged data stream");
2061 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2062 /* Validate delete flag of the stream */
2063 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2066 /* Delete it right now */
2067 consumer_del_stream(stream
, data_ht
);
2073 * Delete metadata stream that are flagged for deletion (endpoint_status).
2075 static void validate_endpoint_status_metadata_stream(
2076 struct lttng_poll_event
*pollset
)
2078 struct lttng_ht_iter iter
;
2079 struct lttng_consumer_stream
*stream
;
2081 DBG("Consumer delete flagged metadata stream");
2086 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2087 /* Validate delete flag of the stream */
2088 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2092 * Remove from pollset so the metadata thread can continue without
2093 * blocking on a deleted stream.
2095 lttng_poll_del(pollset
, stream
->wait_fd
);
2097 /* Delete it right now */
2098 consumer_del_metadata_stream(stream
, metadata_ht
);
2104 * Thread polls on metadata file descriptor and write them on disk or on the
2107 void *consumer_thread_metadata_poll(void *data
)
2110 uint32_t revents
, nb_fd
;
2111 struct lttng_consumer_stream
*stream
= NULL
;
2112 struct lttng_ht_iter iter
;
2113 struct lttng_ht_node_u64
*node
;
2114 struct lttng_poll_event events
;
2115 struct lttng_consumer_local_data
*ctx
= data
;
2118 rcu_register_thread();
2120 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2122 /* ENOMEM at this point. Better to bail out. */
2126 DBG("Thread metadata poll started");
2128 /* Size is set to 1 for the consumer_metadata pipe */
2129 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2131 ERR("Poll set creation failed");
2135 ret
= lttng_poll_add(&events
,
2136 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2142 DBG("Metadata main loop started");
2145 /* Only the metadata pipe is set */
2146 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2151 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2152 ret
= lttng_poll_wait(&events
, -1);
2153 DBG("Metadata event catched in thread");
2155 if (errno
== EINTR
) {
2156 ERR("Poll EINTR catched");
2164 /* From here, the event is a metadata wait fd */
2165 for (i
= 0; i
< nb_fd
; i
++) {
2166 revents
= LTTNG_POLL_GETEV(&events
, i
);
2167 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2169 /* Just don't waste time if no returned events for the fd */
2174 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2175 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2176 DBG("Metadata thread pipe hung up");
2178 * Remove the pipe from the poll set and continue the loop
2179 * since their might be data to consume.
2181 lttng_poll_del(&events
,
2182 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2183 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2185 } else if (revents
& LPOLLIN
) {
2188 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2189 &stream
, sizeof(stream
));
2191 ERR("read metadata stream, ret: %ld", pipe_len
);
2193 * Continue here to handle the rest of the streams.
2198 /* A NULL stream means that the state has changed. */
2199 if (stream
== NULL
) {
2200 /* Check for deleted streams. */
2201 validate_endpoint_status_metadata_stream(&events
);
2205 DBG("Adding metadata stream %d to poll set",
2208 ret
= add_metadata_stream(stream
, metadata_ht
);
2210 ERR("Unable to add metadata stream");
2211 /* Stream was not setup properly. Continuing. */
2212 consumer_del_metadata_stream(stream
, NULL
);
2216 /* Add metadata stream to the global poll events list */
2217 lttng_poll_add(&events
, stream
->wait_fd
,
2218 LPOLLIN
| LPOLLPRI
);
2221 /* Handle other stream */
2227 uint64_t tmp_id
= (uint64_t) pollfd
;
2229 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2231 node
= lttng_ht_iter_get_node_u64(&iter
);
2234 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2237 /* Check for error event */
2238 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2239 DBG("Metadata fd %d is hup|err.", pollfd
);
2240 if (!stream
->hangup_flush_done
2241 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2242 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2243 DBG("Attempting to flush and consume the UST buffers");
2244 lttng_ustconsumer_on_stream_hangup(stream
);
2246 /* We just flushed the stream now read it. */
2248 len
= ctx
->on_buffer_ready(stream
, ctx
);
2250 * We don't check the return value here since if we get
2251 * a negative len, it means an error occured thus we
2252 * simply remove it from the poll set and free the
2258 lttng_poll_del(&events
, stream
->wait_fd
);
2260 * This call update the channel states, closes file descriptors
2261 * and securely free the stream.
2263 consumer_del_metadata_stream(stream
, metadata_ht
);
2264 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2265 /* Get the data out of the metadata file descriptor */
2266 DBG("Metadata available on fd %d", pollfd
);
2267 assert(stream
->wait_fd
== pollfd
);
2269 len
= ctx
->on_buffer_ready(stream
, ctx
);
2270 /* It's ok to have an unavailable sub-buffer */
2271 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2272 /* Clean up stream from consumer and free it. */
2273 lttng_poll_del(&events
, stream
->wait_fd
);
2274 consumer_del_metadata_stream(stream
, metadata_ht
);
2275 } else if (len
> 0) {
2276 stream
->data_read
= 1;
2280 /* Release RCU lock for the stream looked up */
2287 DBG("Metadata poll thread exiting");
2289 lttng_poll_clean(&events
);
2291 destroy_stream_ht(metadata_ht
);
2293 rcu_unregister_thread();
2298 * This thread polls the fds in the set to consume the data and write
2299 * it to tracefile if necessary.
2301 void *consumer_thread_data_poll(void *data
)
2303 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2304 struct pollfd
*pollfd
= NULL
;
2305 /* local view of the streams */
2306 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2307 /* local view of consumer_data.fds_count */
2309 struct lttng_consumer_local_data
*ctx
= data
;
2312 rcu_register_thread();
2314 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2315 if (data_ht
== NULL
) {
2316 /* ENOMEM at this point. Better to bail out. */
2320 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
2327 * the fds set has been updated, we need to update our
2328 * local array as well
2330 pthread_mutex_lock(&consumer_data
.lock
);
2331 if (consumer_data
.need_update
) {
2336 local_stream
= NULL
;
2338 /* allocate for all fds + 1 for the consumer_data_pipe */
2339 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2340 if (pollfd
== NULL
) {
2341 PERROR("pollfd malloc");
2342 pthread_mutex_unlock(&consumer_data
.lock
);
2346 /* allocate for all fds + 1 for the consumer_data_pipe */
2347 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2348 sizeof(struct lttng_consumer_stream
));
2349 if (local_stream
== NULL
) {
2350 PERROR("local_stream malloc");
2351 pthread_mutex_unlock(&consumer_data
.lock
);
2354 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2357 ERR("Error in allocating pollfd or local_outfds");
2358 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2359 pthread_mutex_unlock(&consumer_data
.lock
);
2363 consumer_data
.need_update
= 0;
2365 pthread_mutex_unlock(&consumer_data
.lock
);
2367 /* No FDs and consumer_quit, consumer_cleanup the thread */
2368 if (nb_fd
== 0 && consumer_quit
== 1) {
2371 /* poll on the array of fds */
2373 DBG("polling on %d fd", nb_fd
+ 1);
2374 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2375 DBG("poll num_rdy : %d", num_rdy
);
2376 if (num_rdy
== -1) {
2378 * Restart interrupted system call.
2380 if (errno
== EINTR
) {
2383 PERROR("Poll error");
2384 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2386 } else if (num_rdy
== 0) {
2387 DBG("Polling thread timed out");
2392 * If the consumer_data_pipe triggered poll go directly to the
2393 * beginning of the loop to update the array. We want to prioritize
2394 * array update over low-priority reads.
2396 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2397 ssize_t pipe_readlen
;
2399 DBG("consumer_data_pipe wake up");
2400 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2401 &new_stream
, sizeof(new_stream
));
2402 if (pipe_readlen
< 0) {
2403 ERR("Consumer data pipe ret %ld", pipe_readlen
);
2404 /* Continue so we can at least handle the current stream(s). */
2409 * If the stream is NULL, just ignore it. It's also possible that
2410 * the sessiond poll thread changed the consumer_quit state and is
2411 * waking us up to test it.
2413 if (new_stream
== NULL
) {
2414 validate_endpoint_status_data_stream();
2418 ret
= add_stream(new_stream
, data_ht
);
2420 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
2423 * At this point, if the add_stream fails, it is not in the
2424 * hash table thus passing the NULL value here.
2426 consumer_del_stream(new_stream
, NULL
);
2429 /* Continue to update the local streams and handle prio ones */
2433 /* Take care of high priority channels first. */
2434 for (i
= 0; i
< nb_fd
; i
++) {
2435 if (local_stream
[i
] == NULL
) {
2438 if (pollfd
[i
].revents
& POLLPRI
) {
2439 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2441 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2442 /* it's ok to have an unavailable sub-buffer */
2443 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2444 /* Clean the stream and free it. */
2445 consumer_del_stream(local_stream
[i
], data_ht
);
2446 local_stream
[i
] = NULL
;
2447 } else if (len
> 0) {
2448 local_stream
[i
]->data_read
= 1;
2454 * If we read high prio channel in this loop, try again
2455 * for more high prio data.
2461 /* Take care of low priority channels. */
2462 for (i
= 0; i
< nb_fd
; i
++) {
2463 if (local_stream
[i
] == NULL
) {
2466 if ((pollfd
[i
].revents
& POLLIN
) ||
2467 local_stream
[i
]->hangup_flush_done
) {
2468 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2469 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2470 /* it's ok to have an unavailable sub-buffer */
2471 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2472 /* Clean the stream and free it. */
2473 consumer_del_stream(local_stream
[i
], data_ht
);
2474 local_stream
[i
] = NULL
;
2475 } else if (len
> 0) {
2476 local_stream
[i
]->data_read
= 1;
2481 /* Handle hangup and errors */
2482 for (i
= 0; i
< nb_fd
; i
++) {
2483 if (local_stream
[i
] == NULL
) {
2486 if (!local_stream
[i
]->hangup_flush_done
2487 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2488 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2489 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2490 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2492 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2493 /* Attempt read again, for the data we just flushed. */
2494 local_stream
[i
]->data_read
= 1;
2497 * If the poll flag is HUP/ERR/NVAL and we have
2498 * read no data in this pass, we can remove the
2499 * stream from its hash table.
2501 if ((pollfd
[i
].revents
& POLLHUP
)) {
2502 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2503 if (!local_stream
[i
]->data_read
) {
2504 consumer_del_stream(local_stream
[i
], data_ht
);
2505 local_stream
[i
] = NULL
;
2508 } else if (pollfd
[i
].revents
& POLLERR
) {
2509 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2510 if (!local_stream
[i
]->data_read
) {
2511 consumer_del_stream(local_stream
[i
], data_ht
);
2512 local_stream
[i
] = NULL
;
2515 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2516 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2517 if (!local_stream
[i
]->data_read
) {
2518 consumer_del_stream(local_stream
[i
], data_ht
);
2519 local_stream
[i
] = NULL
;
2523 if (local_stream
[i
] != NULL
) {
2524 local_stream
[i
]->data_read
= 0;
2529 DBG("polling thread exiting");
2534 * Close the write side of the pipe so epoll_wait() in
2535 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2536 * read side of the pipe. If we close them both, epoll_wait strangely does
2537 * not return and could create a endless wait period if the pipe is the
2538 * only tracked fd in the poll set. The thread will take care of closing
2541 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2543 destroy_data_stream_ht(data_ht
);
2545 rcu_unregister_thread();
2550 * Close wake-up end of each stream belonging to the channel. This will
2551 * allow the poll() on the stream read-side to detect when the
2552 * write-side (application) finally closes them.
2555 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2557 struct lttng_ht
*ht
;
2558 struct lttng_consumer_stream
*stream
;
2559 struct lttng_ht_iter iter
;
2561 ht
= consumer_data
.stream_per_chan_id_ht
;
2564 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2565 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2566 ht
->match_fct
, &channel
->key
,
2567 &iter
.iter
, stream
, node_channel_id
.node
) {
2569 * Protect against teardown with mutex.
2571 pthread_mutex_lock(&stream
->lock
);
2572 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2575 switch (consumer_data
.type
) {
2576 case LTTNG_CONSUMER_KERNEL
:
2578 case LTTNG_CONSUMER32_UST
:
2579 case LTTNG_CONSUMER64_UST
:
2581 * Note: a mutex is taken internally within
2582 * liblttng-ust-ctl to protect timer wakeup_fd
2583 * use from concurrent close.
2585 lttng_ustconsumer_close_stream_wakeup(stream
);
2588 ERR("Unknown consumer_data type");
2592 pthread_mutex_unlock(&stream
->lock
);
2597 static void destroy_channel_ht(struct lttng_ht
*ht
)
2599 struct lttng_ht_iter iter
;
2600 struct lttng_consumer_channel
*channel
;
2608 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2609 ret
= lttng_ht_del(ht
, &iter
);
2614 lttng_ht_destroy(ht
);
2618 * This thread polls the channel fds to detect when they are being
2619 * closed. It closes all related streams if the channel is detected as
2620 * closed. It is currently only used as a shim layer for UST because the
2621 * consumerd needs to keep the per-stream wakeup end of pipes open for
2624 void *consumer_thread_channel_poll(void *data
)
2627 uint32_t revents
, nb_fd
;
2628 struct lttng_consumer_channel
*chan
= NULL
;
2629 struct lttng_ht_iter iter
;
2630 struct lttng_ht_node_u64
*node
;
2631 struct lttng_poll_event events
;
2632 struct lttng_consumer_local_data
*ctx
= data
;
2633 struct lttng_ht
*channel_ht
;
2635 rcu_register_thread();
2637 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2639 /* ENOMEM at this point. Better to bail out. */
2643 DBG("Thread channel poll started");
2645 /* Size is set to 1 for the consumer_channel pipe */
2646 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2648 ERR("Poll set creation failed");
2652 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2658 DBG("Channel main loop started");
2661 /* Only the channel pipe is set */
2662 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2667 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2668 ret
= lttng_poll_wait(&events
, -1);
2669 DBG("Channel event catched in thread");
2671 if (errno
== EINTR
) {
2672 ERR("Poll EINTR catched");
2680 /* From here, the event is a channel wait fd */
2681 for (i
= 0; i
< nb_fd
; i
++) {
2682 revents
= LTTNG_POLL_GETEV(&events
, i
);
2683 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2685 /* Just don't waste time if no returned events for the fd */
2689 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2690 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2691 DBG("Channel thread pipe hung up");
2693 * Remove the pipe from the poll set and continue the loop
2694 * since their might be data to consume.
2696 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2698 } else if (revents
& LPOLLIN
) {
2699 enum consumer_channel_action action
;
2702 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2704 ERR("Error reading channel pipe");
2709 case CONSUMER_CHANNEL_ADD
:
2710 DBG("Adding channel %d to poll set",
2713 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2715 lttng_ht_add_unique_u64(channel_ht
,
2716 &chan
->wait_fd_node
);
2717 /* Add channel to the global poll events list */
2718 lttng_poll_add(&events
, chan
->wait_fd
,
2719 LPOLLIN
| LPOLLPRI
);
2721 case CONSUMER_CHANNEL_DEL
:
2723 chan
= consumer_find_channel(key
);
2725 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2728 lttng_poll_del(&events
, chan
->wait_fd
);
2729 ret
= lttng_ht_del(channel_ht
, &iter
);
2731 consumer_close_channel_streams(chan
);
2734 * Release our own refcount. Force channel deletion even if
2735 * streams were not initialized.
2737 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2738 consumer_del_channel(chan
);
2742 case CONSUMER_CHANNEL_QUIT
:
2744 * Remove the pipe from the poll set and continue the loop
2745 * since their might be data to consume.
2747 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2750 ERR("Unknown action");
2755 /* Handle other stream */
2761 uint64_t tmp_id
= (uint64_t) pollfd
;
2763 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2765 node
= lttng_ht_iter_get_node_u64(&iter
);
2768 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2771 /* Check for error event */
2772 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2773 DBG("Channel fd %d is hup|err.", pollfd
);
2775 lttng_poll_del(&events
, chan
->wait_fd
);
2776 ret
= lttng_ht_del(channel_ht
, &iter
);
2778 consumer_close_channel_streams(chan
);
2780 /* Release our own refcount */
2781 if (!uatomic_sub_return(&chan
->refcount
, 1)
2782 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2783 consumer_del_channel(chan
);
2787 /* Release RCU lock for the channel looked up */
2793 lttng_poll_clean(&events
);
2795 destroy_channel_ht(channel_ht
);
2797 DBG("Channel poll thread exiting");
2798 rcu_unregister_thread();
2802 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2803 struct pollfd
*sockpoll
, int client_socket
)
2810 if (lttng_consumer_poll_socket(sockpoll
) < 0) {
2814 DBG("Metadata connection on client_socket");
2816 /* Blocking call, waiting for transmission */
2817 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2818 if (ctx
->consumer_metadata_socket
< 0) {
2819 WARN("On accept metadata");
2830 * This thread listens on the consumerd socket and receives the file
2831 * descriptors from the session daemon.
2833 void *consumer_thread_sessiond_poll(void *data
)
2835 int sock
= -1, client_socket
, ret
;
2837 * structure to poll for incoming data on communication socket avoids
2838 * making blocking sockets.
2840 struct pollfd consumer_sockpoll
[2];
2841 struct lttng_consumer_local_data
*ctx
= data
;
2843 rcu_register_thread();
2845 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2846 unlink(ctx
->consumer_command_sock_path
);
2847 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2848 if (client_socket
< 0) {
2849 ERR("Cannot create command socket");
2853 ret
= lttcomm_listen_unix_sock(client_socket
);
2858 DBG("Sending ready command to lttng-sessiond");
2859 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2860 /* return < 0 on error, but == 0 is not fatal */
2862 ERR("Error sending ready command to lttng-sessiond");
2866 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2868 PERROR("fcntl O_NONBLOCK");
2872 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2873 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2874 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2875 consumer_sockpoll
[1].fd
= client_socket
;
2876 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2878 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2881 DBG("Connection on client_socket");
2883 /* Blocking call, waiting for transmission */
2884 sock
= lttcomm_accept_unix_sock(client_socket
);
2889 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2891 PERROR("fcntl O_NONBLOCK");
2896 * Setup metadata socket which is the second socket connection on the
2897 * command unix socket.
2899 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
2904 /* This socket is not useful anymore. */
2905 ret
= close(client_socket
);
2907 PERROR("close client_socket");
2911 /* update the polling structure to poll on the established socket */
2912 consumer_sockpoll
[1].fd
= sock
;
2913 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2916 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2919 DBG("Incoming command on sock");
2920 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2921 if (ret
== -ENOENT
) {
2922 DBG("Received STOP command");
2927 * This could simply be a session daemon quitting. Don't output
2930 DBG("Communication interrupted on command socket");
2933 if (consumer_quit
) {
2934 DBG("consumer_thread_receive_fds received quit from signal");
2937 DBG("received command on sock");
2940 DBG("Consumer thread sessiond poll exiting");
2943 * Close metadata streams since the producer is the session daemon which
2946 * NOTE: for now, this only applies to the UST tracer.
2948 lttng_consumer_close_metadata();
2951 * when all fds have hung up, the polling thread
2957 * Notify the data poll thread to poll back again and test the
2958 * consumer_quit state that we just set so to quit gracefully.
2960 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
2962 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
2964 /* Cleaning up possibly open sockets. */
2968 PERROR("close sock sessiond poll");
2971 if (client_socket
>= 0) {
2974 PERROR("close client_socket sessiond poll");
2978 rcu_unregister_thread();
2982 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2983 struct lttng_consumer_local_data
*ctx
)
2987 pthread_mutex_lock(&stream
->lock
);
2989 switch (consumer_data
.type
) {
2990 case LTTNG_CONSUMER_KERNEL
:
2991 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
2993 case LTTNG_CONSUMER32_UST
:
2994 case LTTNG_CONSUMER64_UST
:
2995 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2998 ERR("Unknown consumer_data type");
3004 pthread_mutex_unlock(&stream
->lock
);
3008 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3010 switch (consumer_data
.type
) {
3011 case LTTNG_CONSUMER_KERNEL
:
3012 return lttng_kconsumer_on_recv_stream(stream
);
3013 case LTTNG_CONSUMER32_UST
:
3014 case LTTNG_CONSUMER64_UST
:
3015 return lttng_ustconsumer_on_recv_stream(stream
);
3017 ERR("Unknown consumer_data type");
3024 * Allocate and set consumer data hash tables.
3026 void lttng_consumer_init(void)
3028 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3029 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3030 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3031 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3035 * Process the ADD_RELAYD command receive by a consumer.
3037 * This will create a relayd socket pair and add it to the relayd hash table.
3038 * The caller MUST acquire a RCU read side lock before calling it.
3040 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
3041 struct lttng_consumer_local_data
*ctx
, int sock
,
3042 struct pollfd
*consumer_sockpoll
,
3043 struct lttcomm_relayd_sock
*relayd_sock
, unsigned int sessiond_id
)
3045 int fd
= -1, ret
= -1, relayd_created
= 0;
3046 enum lttng_error_code ret_code
= LTTNG_OK
;
3047 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3050 assert(relayd_sock
);
3052 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
3054 /* First send a status message before receiving the fds. */
3055 ret
= consumer_send_status_msg(sock
, ret_code
);
3057 /* Somehow, the session daemon is not responding anymore. */
3061 /* Get relayd reference if exists. */
3062 relayd
= consumer_find_relayd(net_seq_idx
);
3063 if (relayd
== NULL
) {
3064 /* Not found. Allocate one. */
3065 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3066 if (relayd
== NULL
) {
3067 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
3071 relayd
->sessiond_session_id
= (uint64_t) sessiond_id
;
3075 /* Poll on consumer socket. */
3076 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
3081 /* Get relayd socket from session daemon */
3082 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3083 if (ret
!= sizeof(fd
)) {
3084 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3086 fd
= -1; /* Just in case it gets set with an invalid value. */
3090 /* We have the fds without error. Send status back. */
3091 ret
= consumer_send_status_msg(sock
, ret_code
);
3093 /* Somehow, the session daemon is not responding anymore. */
3097 /* Copy socket information and received FD */
3098 switch (sock_type
) {
3099 case LTTNG_STREAM_CONTROL
:
3100 /* Copy received lttcomm socket */
3101 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3102 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3103 /* Immediately try to close the created socket if valid. */
3104 if (relayd
->control_sock
.sock
.fd
>= 0) {
3105 if (close(relayd
->control_sock
.sock
.fd
)) {
3106 PERROR("close relayd control socket");
3109 /* Handle create_sock error. */
3114 /* Assign new file descriptor */
3115 relayd
->control_sock
.sock
.fd
= fd
;
3116 /* Assign version values. */
3117 relayd
->control_sock
.major
= relayd_sock
->major
;
3118 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3121 * Create a session on the relayd and store the returned id. Lock the
3122 * control socket mutex if the relayd was NOT created before.
3124 if (!relayd_created
) {
3125 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3127 ret
= relayd_create_session(&relayd
->control_sock
,
3128 &relayd
->relayd_session_id
);
3129 if (!relayd_created
) {
3130 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3134 * Close all sockets of a relayd object. It will be freed if it was
3135 * created at the error code path or else it will be garbage
3138 (void) relayd_close(&relayd
->control_sock
);
3139 (void) relayd_close(&relayd
->data_sock
);
3144 case LTTNG_STREAM_DATA
:
3145 /* Copy received lttcomm socket */
3146 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3147 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3148 /* Immediately try to close the created socket if valid. */
3149 if (relayd
->data_sock
.sock
.fd
>= 0) {
3150 if (close(relayd
->data_sock
.sock
.fd
)) {
3151 PERROR("close relayd data socket");
3154 /* Handle create_sock error. */
3159 /* Assign new file descriptor */
3160 relayd
->data_sock
.sock
.fd
= fd
;
3161 /* Assign version values. */
3162 relayd
->data_sock
.major
= relayd_sock
->major
;
3163 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3166 ERR("Unknown relayd socket type (%d)", sock_type
);
3171 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3172 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3173 relayd
->net_seq_idx
, fd
);
3176 * Add relayd socket pair to consumer data hashtable. If object already
3177 * exists or on error, the function gracefully returns.
3185 /* Close received socket if valid. */
3188 PERROR("close received socket");
3193 if (relayd_created
) {
3201 * Try to lock the stream mutex.
3203 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3205 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3212 * Try to lock the stream mutex. On failure, we know that the stream is
3213 * being used else where hence there is data still being extracted.
3215 ret
= pthread_mutex_trylock(&stream
->lock
);
3217 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3229 * Search for a relayd associated to the session id and return the reference.
3231 * A rcu read side lock MUST be acquire before calling this function and locked
3232 * until the relayd object is no longer necessary.
3234 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3236 struct lttng_ht_iter iter
;
3237 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3239 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3240 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3243 * Check by sessiond id which is unique here where the relayd session
3244 * id might not be when having multiple relayd.
3246 if (relayd
->sessiond_session_id
== id
) {
3247 /* Found the relayd. There can be only one per id. */
3259 * Check if for a given session id there is still data needed to be extract
3262 * Return 1 if data is pending or else 0 meaning ready to be read.
3264 int consumer_data_pending(uint64_t id
)
3267 struct lttng_ht_iter iter
;
3268 struct lttng_ht
*ht
;
3269 struct lttng_consumer_stream
*stream
;
3270 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3271 int (*data_pending
)(struct lttng_consumer_stream
*);
3273 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3276 pthread_mutex_lock(&consumer_data
.lock
);
3278 switch (consumer_data
.type
) {
3279 case LTTNG_CONSUMER_KERNEL
:
3280 data_pending
= lttng_kconsumer_data_pending
;
3282 case LTTNG_CONSUMER32_UST
:
3283 case LTTNG_CONSUMER64_UST
:
3284 data_pending
= lttng_ustconsumer_data_pending
;
3287 ERR("Unknown consumer data type");
3291 /* Ease our life a bit */
3292 ht
= consumer_data
.stream_list_ht
;
3294 relayd
= find_relayd_by_session_id(id
);
3296 /* Send init command for data pending. */
3297 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3298 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3299 relayd
->relayd_session_id
);
3300 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3302 /* Communication error thus the relayd so no data pending. */
3303 goto data_not_pending
;
3307 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3308 ht
->hash_fct(&id
, lttng_ht_seed
),
3310 &iter
.iter
, stream
, node_session_id
.node
) {
3311 /* If this call fails, the stream is being used hence data pending. */
3312 ret
= stream_try_lock(stream
);
3318 * A removed node from the hash table indicates that the stream has
3319 * been deleted thus having a guarantee that the buffers are closed
3320 * on the consumer side. However, data can still be transmitted
3321 * over the network so don't skip the relayd check.
3323 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3325 /* Check the stream if there is data in the buffers. */
3326 ret
= data_pending(stream
);
3328 pthread_mutex_unlock(&stream
->lock
);
3335 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3336 if (stream
->metadata_flag
) {
3337 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3338 stream
->relayd_stream_id
);
3340 ret
= relayd_data_pending(&relayd
->control_sock
,
3341 stream
->relayd_stream_id
,
3342 stream
->next_net_seq_num
- 1);
3344 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3346 pthread_mutex_unlock(&stream
->lock
);
3350 pthread_mutex_unlock(&stream
->lock
);
3354 unsigned int is_data_inflight
= 0;
3356 /* Send init command for data pending. */
3357 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3358 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3359 relayd
->relayd_session_id
, &is_data_inflight
);
3360 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3362 goto data_not_pending
;
3364 if (is_data_inflight
) {
3370 * Finding _no_ node in the hash table and no inflight data means that the
3371 * stream(s) have been removed thus data is guaranteed to be available for
3372 * analysis from the trace files.
3376 /* Data is available to be read by a viewer. */
3377 pthread_mutex_unlock(&consumer_data
.lock
);
3382 /* Data is still being extracted from buffers. */
3383 pthread_mutex_unlock(&consumer_data
.lock
);
3389 * Send a ret code status message to the sessiond daemon.
3391 * Return the sendmsg() return value.
3393 int consumer_send_status_msg(int sock
, int ret_code
)
3395 struct lttcomm_consumer_status_msg msg
;
3397 msg
.ret_code
= ret_code
;
3399 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3403 * Send a channel status message to the sessiond daemon.
3405 * Return the sendmsg() return value.
3407 int consumer_send_status_channel(int sock
,
3408 struct lttng_consumer_channel
*channel
)
3410 struct lttcomm_consumer_status_channel msg
;
3415 msg
.ret_code
= -LTTNG_ERR_UST_CHAN_FAIL
;
3417 msg
.ret_code
= LTTNG_OK
;
3418 msg
.key
= channel
->key
;
3419 msg
.stream_count
= channel
->streams
.count
;
3422 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));