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
{
53 CONSUMER_CHANNEL_QUIT
,
56 struct consumer_channel_msg
{
57 enum consumer_channel_action action
;
58 struct lttng_consumer_channel
*chan
;
62 * Flag to inform the polling thread to quit when all fd hung up. Updated by
63 * the consumer_thread_receive_fds when it notices that all fds has hung up.
64 * Also updated by the signal handler (consumer_should_exit()). Read by the
67 volatile int consumer_quit
;
70 * Global hash table containing respectively metadata and data streams. The
71 * stream element in this ht should only be updated by the metadata poll thread
72 * for the metadata and the data poll thread for the data.
74 static struct lttng_ht
*metadata_ht
;
75 static struct lttng_ht
*data_ht
;
78 * Notify a thread pipe to poll back again. This usually means that some global
79 * state has changed so we just send back the thread in a poll wait call.
81 static void notify_thread_pipe(int wpipe
)
86 struct lttng_consumer_stream
*null_stream
= NULL
;
88 ret
= write(wpipe
, &null_stream
, sizeof(null_stream
));
89 } while (ret
< 0 && errno
== EINTR
);
92 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
93 struct lttng_consumer_channel
*chan
,
94 enum consumer_channel_action action
)
96 struct consumer_channel_msg msg
;
102 ret
= write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
103 } while (ret
< 0 && errno
== EINTR
);
106 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
107 struct lttng_consumer_channel
**chan
,
108 enum consumer_channel_action
*action
)
110 struct consumer_channel_msg msg
;
114 ret
= read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
115 } while (ret
< 0 && errno
== EINTR
);
117 *action
= msg
.action
;
124 * Find a stream. The consumer_data.lock must be locked during this
127 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
130 struct lttng_ht_iter iter
;
131 struct lttng_ht_node_u64
*node
;
132 struct lttng_consumer_stream
*stream
= NULL
;
136 /* -1ULL keys are lookup failures */
137 if (key
== (uint64_t) -1ULL) {
143 lttng_ht_lookup(ht
, &key
, &iter
);
144 node
= lttng_ht_iter_get_node_u64(&iter
);
146 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
154 static void steal_stream_key(int key
, struct lttng_ht
*ht
)
156 struct lttng_consumer_stream
*stream
;
159 stream
= find_stream(key
, ht
);
163 * We don't want the lookup to match, but we still need
164 * to iterate on this stream when iterating over the hash table. Just
165 * change the node key.
167 stream
->node
.key
= -1ULL;
173 * Return a channel object for the given key.
175 * RCU read side lock MUST be acquired before calling this function and
176 * protects the channel ptr.
178 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
180 struct lttng_ht_iter iter
;
181 struct lttng_ht_node_u64
*node
;
182 struct lttng_consumer_channel
*channel
= NULL
;
184 /* -1ULL keys are lookup failures */
185 if (key
== (uint64_t) -1ULL) {
189 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
190 node
= lttng_ht_iter_get_node_u64(&iter
);
192 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
198 static void free_stream_rcu(struct rcu_head
*head
)
200 struct lttng_ht_node_u64
*node
=
201 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
202 struct lttng_consumer_stream
*stream
=
203 caa_container_of(node
, struct lttng_consumer_stream
, node
);
208 static void free_channel_rcu(struct rcu_head
*head
)
210 struct lttng_ht_node_u64
*node
=
211 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
212 struct lttng_consumer_channel
*channel
=
213 caa_container_of(node
, struct lttng_consumer_channel
, node
);
219 * RCU protected relayd socket pair free.
221 static void free_relayd_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 consumer_relayd_sock_pair
*relayd
=
226 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
229 * Close all sockets. This is done in the call RCU since we don't want the
230 * socket fds to be reassigned thus potentially creating bad state of the
233 * We do not have to lock the control socket mutex here since at this stage
234 * there is no one referencing to this relayd object.
236 (void) relayd_close(&relayd
->control_sock
);
237 (void) relayd_close(&relayd
->data_sock
);
243 * Destroy and free relayd socket pair object.
245 * This function MUST be called with the consumer_data lock acquired.
247 static void destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
250 struct lttng_ht_iter iter
;
252 if (relayd
== NULL
) {
256 DBG("Consumer destroy and close relayd socket pair");
258 iter
.iter
.node
= &relayd
->node
.node
;
259 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
261 /* We assume the relayd is being or is destroyed */
265 /* RCU free() call */
266 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
270 * Remove a channel from the global list protected by a mutex. This function is
271 * also responsible for freeing its data structures.
273 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
276 struct lttng_ht_iter iter
;
278 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
280 pthread_mutex_lock(&consumer_data
.lock
);
282 switch (consumer_data
.type
) {
283 case LTTNG_CONSUMER_KERNEL
:
285 case LTTNG_CONSUMER32_UST
:
286 case LTTNG_CONSUMER64_UST
:
287 lttng_ustconsumer_del_channel(channel
);
290 ERR("Unknown consumer_data type");
296 iter
.iter
.node
= &channel
->node
.node
;
297 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
301 call_rcu(&channel
->node
.head
, free_channel_rcu
);
303 pthread_mutex_unlock(&consumer_data
.lock
);
307 * Iterate over the relayd hash table and destroy each element. Finally,
308 * destroy the whole hash table.
310 static void cleanup_relayd_ht(void)
312 struct lttng_ht_iter iter
;
313 struct consumer_relayd_sock_pair
*relayd
;
317 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
319 destroy_relayd(relayd
);
324 lttng_ht_destroy(consumer_data
.relayd_ht
);
328 * Update the end point status of all streams having the given network sequence
329 * index (relayd index).
331 * It's atomically set without having the stream mutex locked which is fine
332 * because we handle the write/read race with a pipe wakeup for each thread.
334 static void update_endpoint_status_by_netidx(int net_seq_idx
,
335 enum consumer_endpoint_status status
)
337 struct lttng_ht_iter iter
;
338 struct lttng_consumer_stream
*stream
;
340 DBG("Consumer set delete flag on stream by idx %d", net_seq_idx
);
344 /* Let's begin with metadata */
345 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
346 if (stream
->net_seq_idx
== net_seq_idx
) {
347 uatomic_set(&stream
->endpoint_status
, status
);
348 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
352 /* Follow up by the data streams */
353 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
354 if (stream
->net_seq_idx
== net_seq_idx
) {
355 uatomic_set(&stream
->endpoint_status
, status
);
356 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
363 * Cleanup a relayd object by flagging every associated streams for deletion,
364 * destroying the object meaning removing it from the relayd hash table,
365 * closing the sockets and freeing the memory in a RCU call.
367 * If a local data context is available, notify the threads that the streams'
368 * state have changed.
370 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
371 struct lttng_consumer_local_data
*ctx
)
377 DBG("Cleaning up relayd sockets");
379 /* Save the net sequence index before destroying the object */
380 netidx
= relayd
->net_seq_idx
;
383 * Delete the relayd from the relayd hash table, close the sockets and free
384 * the object in a RCU call.
386 destroy_relayd(relayd
);
388 /* Set inactive endpoint to all streams */
389 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
392 * With a local data context, notify the threads that the streams' state
393 * have changed. The write() action on the pipe acts as an "implicit"
394 * memory barrier ordering the updates of the end point status from the
395 * read of this status which happens AFTER receiving this notify.
398 notify_thread_pipe(ctx
->consumer_data_pipe
[1]);
399 notify_thread_pipe(ctx
->consumer_metadata_pipe
[1]);
404 * Flag a relayd socket pair for destruction. Destroy it if the refcount
407 * RCU read side lock MUST be aquired before calling this function.
409 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
413 /* Set destroy flag for this object */
414 uatomic_set(&relayd
->destroy_flag
, 1);
416 /* Destroy the relayd if refcount is 0 */
417 if (uatomic_read(&relayd
->refcount
) == 0) {
418 destroy_relayd(relayd
);
423 * Remove a stream from the global list protected by a mutex. This
424 * function is also responsible for freeing its data structures.
426 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
430 struct lttng_ht_iter iter
;
431 struct lttng_consumer_channel
*free_chan
= NULL
;
432 struct consumer_relayd_sock_pair
*relayd
;
436 DBG("Consumer del stream %d", stream
->wait_fd
);
439 /* Means the stream was allocated but not successfully added */
440 goto free_stream_rcu
;
443 pthread_mutex_lock(&consumer_data
.lock
);
444 pthread_mutex_lock(&stream
->lock
);
446 switch (consumer_data
.type
) {
447 case LTTNG_CONSUMER_KERNEL
:
448 if (stream
->mmap_base
!= NULL
) {
449 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
455 case LTTNG_CONSUMER32_UST
:
456 case LTTNG_CONSUMER64_UST
:
457 lttng_ustconsumer_del_stream(stream
);
460 ERR("Unknown consumer_data type");
466 iter
.iter
.node
= &stream
->node
.node
;
467 ret
= lttng_ht_del(ht
, &iter
);
470 iter
.iter
.node
= &stream
->node_channel_id
.node
;
471 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
474 iter
.iter
.node
= &stream
->node_session_id
.node
;
475 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
479 assert(consumer_data
.stream_count
> 0);
480 consumer_data
.stream_count
--;
482 if (stream
->out_fd
>= 0) {
483 ret
= close(stream
->out_fd
);
489 /* Check and cleanup relayd */
491 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
492 if (relayd
!= NULL
) {
493 uatomic_dec(&relayd
->refcount
);
494 assert(uatomic_read(&relayd
->refcount
) >= 0);
496 /* Closing streams requires to lock the control socket. */
497 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
498 ret
= relayd_send_close_stream(&relayd
->control_sock
,
499 stream
->relayd_stream_id
,
500 stream
->next_net_seq_num
- 1);
501 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
503 DBG("Unable to close stream on the relayd. Continuing");
505 * Continue here. There is nothing we can do for the relayd.
506 * Chances are that the relayd has closed the socket so we just
507 * continue cleaning up.
511 /* Both conditions are met, we destroy the relayd. */
512 if (uatomic_read(&relayd
->refcount
) == 0 &&
513 uatomic_read(&relayd
->destroy_flag
)) {
514 destroy_relayd(relayd
);
519 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
520 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
521 free_chan
= stream
->chan
;
525 consumer_data
.need_update
= 1;
526 pthread_mutex_unlock(&stream
->lock
);
527 pthread_mutex_unlock(&consumer_data
.lock
);
530 consumer_del_channel(free_chan
);
534 call_rcu(&stream
->node
.head
, free_stream_rcu
);
537 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
539 enum lttng_consumer_stream_state state
,
540 const char *channel_name
,
547 enum consumer_channel_type type
)
550 struct lttng_consumer_stream
*stream
;
552 stream
= zmalloc(sizeof(*stream
));
553 if (stream
== NULL
) {
554 PERROR("malloc struct lttng_consumer_stream");
561 stream
->key
= stream_key
;
563 stream
->out_fd_offset
= 0;
564 stream
->state
= state
;
567 stream
->net_seq_idx
= relayd_id
;
568 stream
->session_id
= session_id
;
569 pthread_mutex_init(&stream
->lock
, NULL
);
571 /* If channel is the metadata, flag this stream as metadata. */
572 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
573 stream
->metadata_flag
= 1;
574 /* Metadata is flat out. */
575 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
577 /* Format stream name to <channel_name>_<cpu_number> */
578 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
581 PERROR("snprintf stream name");
586 /* Key is always the wait_fd for streams. */
587 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
589 /* Init node per channel id key */
590 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
592 /* Init session id node with the stream session id */
593 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
595 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
" relayd_id %" PRIu64
", session_id %" PRIu64
,
596 stream
->name
, stream
->key
, channel_key
, stream
->net_seq_idx
, stream
->session_id
);
612 * Add a stream to the global list protected by a mutex.
614 static int add_stream(struct lttng_consumer_stream
*stream
,
618 struct consumer_relayd_sock_pair
*relayd
;
623 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
625 pthread_mutex_lock(&consumer_data
.lock
);
626 pthread_mutex_lock(&stream
->lock
);
629 /* Steal stream identifier to avoid having streams with the same key */
630 steal_stream_key(stream
->key
, ht
);
632 lttng_ht_add_unique_u64(ht
, &stream
->node
);
634 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
635 &stream
->node_channel_id
);
638 * Add stream to the stream_list_ht of the consumer data. No need to steal
639 * the key since the HT does not use it and we allow to add redundant keys
642 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
644 /* Check and cleanup relayd */
645 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
646 if (relayd
!= NULL
) {
647 uatomic_inc(&relayd
->refcount
);
650 /* Update channel refcount once added without error(s). */
651 uatomic_inc(&stream
->chan
->refcount
);
654 * When nb_init_stream_left reaches 0, we don't need to trigger any action
655 * in terms of destroying the associated channel, because the action that
656 * causes the count to become 0 also causes a stream to be added. The
657 * channel deletion will thus be triggered by the following removal of this
660 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
661 /* Increment refcount before decrementing nb_init_stream_left */
663 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
666 /* Update consumer data once the node is inserted. */
667 consumer_data
.stream_count
++;
668 consumer_data
.need_update
= 1;
671 pthread_mutex_unlock(&stream
->lock
);
672 pthread_mutex_unlock(&consumer_data
.lock
);
678 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
679 * be acquired before calling this.
681 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
684 struct lttng_ht_node_u64
*node
;
685 struct lttng_ht_iter iter
;
689 lttng_ht_lookup(consumer_data
.relayd_ht
,
690 &relayd
->net_seq_idx
, &iter
);
691 node
= lttng_ht_iter_get_node_u64(&iter
);
695 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
702 * Allocate and return a consumer relayd socket.
704 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
707 struct consumer_relayd_sock_pair
*obj
= NULL
;
709 /* Negative net sequence index is a failure */
710 if (net_seq_idx
< 0) {
714 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
716 PERROR("zmalloc relayd sock");
720 obj
->net_seq_idx
= net_seq_idx
;
722 obj
->destroy_flag
= 0;
723 obj
->control_sock
.sock
.fd
= -1;
724 obj
->data_sock
.sock
.fd
= -1;
725 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
726 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
733 * Find a relayd socket pair in the global consumer data.
735 * Return the object if found else NULL.
736 * RCU read-side lock must be held across this call and while using the
739 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
741 struct lttng_ht_iter iter
;
742 struct lttng_ht_node_u64
*node
;
743 struct consumer_relayd_sock_pair
*relayd
= NULL
;
745 /* Negative keys are lookup failures */
746 if (key
== (uint64_t) -1ULL) {
750 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
752 node
= lttng_ht_iter_get_node_u64(&iter
);
754 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
762 * Handle stream for relayd transmission if the stream applies for network
763 * streaming where the net sequence index is set.
765 * Return destination file descriptor or negative value on error.
767 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
768 size_t data_size
, unsigned long padding
,
769 struct consumer_relayd_sock_pair
*relayd
)
772 struct lttcomm_relayd_data_hdr data_hdr
;
778 /* Reset data header */
779 memset(&data_hdr
, 0, sizeof(data_hdr
));
781 if (stream
->metadata_flag
) {
782 /* Caller MUST acquire the relayd control socket lock */
783 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
788 /* Metadata are always sent on the control socket. */
789 outfd
= relayd
->control_sock
.sock
.fd
;
791 /* Set header with stream information */
792 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
793 data_hdr
.data_size
= htobe32(data_size
);
794 data_hdr
.padding_size
= htobe32(padding
);
796 * Note that net_seq_num below is assigned with the *current* value of
797 * next_net_seq_num and only after that the next_net_seq_num will be
798 * increment. This is why when issuing a command on the relayd using
799 * this next value, 1 should always be substracted in order to compare
800 * the last seen sequence number on the relayd side to the last sent.
802 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
803 /* Other fields are zeroed previously */
805 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
811 ++stream
->next_net_seq_num
;
813 /* Set to go on data socket */
814 outfd
= relayd
->data_sock
.sock
.fd
;
822 * Allocate and return a new lttng_consumer_channel object using the given key
823 * to initialize the hash table node.
825 * On error, return NULL.
827 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
829 const char *pathname
,
834 enum lttng_event_output output
,
835 uint64_t tracefile_size
,
836 uint64_t tracefile_count
)
838 struct lttng_consumer_channel
*channel
;
840 channel
= zmalloc(sizeof(*channel
));
841 if (channel
== NULL
) {
842 PERROR("malloc struct lttng_consumer_channel");
847 channel
->refcount
= 0;
848 channel
->session_id
= session_id
;
851 channel
->relayd_id
= relayd_id
;
852 channel
->output
= output
;
853 channel
->tracefile_size
= tracefile_size
;
854 channel
->tracefile_count
= tracefile_count
;
856 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
857 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
859 strncpy(channel
->name
, name
, sizeof(channel
->name
));
860 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
862 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
864 channel
->wait_fd
= -1;
866 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
868 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
875 * Add a channel to the global list protected by a mutex.
877 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
878 struct lttng_consumer_local_data
*ctx
)
881 struct lttng_ht_node_u64
*node
;
882 struct lttng_ht_iter iter
;
884 pthread_mutex_lock(&consumer_data
.lock
);
887 lttng_ht_lookup(consumer_data
.channel_ht
, &channel
->key
, &iter
);
888 node
= lttng_ht_iter_get_node_u64(&iter
);
890 /* Channel already exist. Ignore the insertion */
891 ERR("Consumer add channel key %" PRIu64
" already exists!",
897 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
901 pthread_mutex_unlock(&consumer_data
.lock
);
903 if (!ret
&& channel
->wait_fd
!= -1 &&
904 channel
->metadata_stream
== NULL
) {
905 notify_channel_pipe(ctx
, channel
, CONSUMER_CHANNEL_ADD
);
911 * Allocate the pollfd structure and the local view of the out fds to avoid
912 * doing a lookup in the linked list and concurrency issues when writing is
913 * needed. Called with consumer_data.lock held.
915 * Returns the number of fds in the structures.
917 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
918 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
922 struct lttng_ht_iter iter
;
923 struct lttng_consumer_stream
*stream
;
928 assert(local_stream
);
930 DBG("Updating poll fd array");
932 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
934 * Only active streams with an active end point can be added to the
935 * poll set and local stream storage of the thread.
937 * There is a potential race here for endpoint_status to be updated
938 * just after the check. However, this is OK since the stream(s) will
939 * be deleted once the thread is notified that the end point state has
940 * changed where this function will be called back again.
942 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
943 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
947 * This clobbers way too much the debug output. Uncomment that if you
948 * need it for debugging purposes.
950 * DBG("Active FD %d", stream->wait_fd);
952 (*pollfd
)[i
].fd
= stream
->wait_fd
;
953 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
954 local_stream
[i
] = stream
;
960 * Insert the consumer_data_pipe at the end of the array and don't
961 * increment i so nb_fd is the number of real FD.
963 (*pollfd
)[i
].fd
= ctx
->consumer_data_pipe
[0];
964 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
969 * Poll on the should_quit pipe and the command socket return -1 on error and
970 * should exit, 0 if data is available on the command socket
972 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
977 num_rdy
= poll(consumer_sockpoll
, 2, -1);
980 * Restart interrupted system call.
982 if (errno
== EINTR
) {
985 PERROR("Poll error");
988 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
989 DBG("consumer_should_quit wake up");
999 * Set the error socket.
1001 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1004 ctx
->consumer_error_socket
= sock
;
1008 * Set the command socket path.
1010 void lttng_consumer_set_command_sock_path(
1011 struct lttng_consumer_local_data
*ctx
, char *sock
)
1013 ctx
->consumer_command_sock_path
= sock
;
1017 * Send return code to the session daemon.
1018 * If the socket is not defined, we return 0, it is not a fatal error
1020 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1022 if (ctx
->consumer_error_socket
> 0) {
1023 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1024 sizeof(enum lttcomm_sessiond_command
));
1031 * Close all the tracefiles and stream fds and MUST be called when all
1032 * instances are destroyed i.e. when all threads were joined and are ended.
1034 void lttng_consumer_cleanup(void)
1036 struct lttng_ht_iter iter
;
1037 struct lttng_consumer_channel
*channel
;
1041 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1043 consumer_del_channel(channel
);
1048 lttng_ht_destroy(consumer_data
.channel_ht
);
1050 cleanup_relayd_ht();
1052 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1055 * This HT contains streams that are freed by either the metadata thread or
1056 * the data thread so we do *nothing* on the hash table and simply destroy
1059 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1063 * Called from signal handler.
1065 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1070 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1071 } while (ret
< 0 && errno
== EINTR
);
1072 if (ret
< 0 || ret
!= 1) {
1073 PERROR("write consumer quit");
1076 DBG("Consumer flag that it should quit");
1079 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1082 int outfd
= stream
->out_fd
;
1085 * This does a blocking write-and-wait on any page that belongs to the
1086 * subbuffer prior to the one we just wrote.
1087 * Don't care about error values, as these are just hints and ways to
1088 * limit the amount of page cache used.
1090 if (orig_offset
< stream
->max_sb_size
) {
1093 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1094 stream
->max_sb_size
,
1095 SYNC_FILE_RANGE_WAIT_BEFORE
1096 | SYNC_FILE_RANGE_WRITE
1097 | SYNC_FILE_RANGE_WAIT_AFTER
);
1099 * Give hints to the kernel about how we access the file:
1100 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1103 * We need to call fadvise again after the file grows because the
1104 * kernel does not seem to apply fadvise to non-existing parts of the
1107 * Call fadvise _after_ having waited for the page writeback to
1108 * complete because the dirty page writeback semantic is not well
1109 * defined. So it can be expected to lead to lower throughput in
1112 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1113 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1117 * Initialise the necessary environnement :
1118 * - create a new context
1119 * - create the poll_pipe
1120 * - create the should_quit pipe (for signal handler)
1121 * - create the thread pipe (for splice)
1123 * Takes a function pointer as argument, this function is called when data is
1124 * available on a buffer. This function is responsible to do the
1125 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1126 * buffer configuration and then kernctl_put_next_subbuf at the end.
1128 * Returns a pointer to the new context or NULL on error.
1130 struct lttng_consumer_local_data
*lttng_consumer_create(
1131 enum lttng_consumer_type type
,
1132 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1133 struct lttng_consumer_local_data
*ctx
),
1134 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1135 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1136 int (*update_stream
)(int stream_key
, uint32_t state
))
1139 struct lttng_consumer_local_data
*ctx
;
1141 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1142 consumer_data
.type
== type
);
1143 consumer_data
.type
= type
;
1145 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1147 PERROR("allocating context");
1151 ctx
->consumer_error_socket
= -1;
1152 ctx
->consumer_metadata_socket
= -1;
1153 /* assign the callbacks */
1154 ctx
->on_buffer_ready
= buffer_ready
;
1155 ctx
->on_recv_channel
= recv_channel
;
1156 ctx
->on_recv_stream
= recv_stream
;
1157 ctx
->on_update_stream
= update_stream
;
1159 ret
= pipe(ctx
->consumer_data_pipe
);
1161 PERROR("Error creating poll pipe");
1162 goto error_poll_pipe
;
1165 /* set read end of the pipe to non-blocking */
1166 ret
= fcntl(ctx
->consumer_data_pipe
[0], F_SETFL
, O_NONBLOCK
);
1168 PERROR("fcntl O_NONBLOCK");
1169 goto error_poll_fcntl
;
1172 /* set write end of the pipe to non-blocking */
1173 ret
= fcntl(ctx
->consumer_data_pipe
[1], F_SETFL
, O_NONBLOCK
);
1175 PERROR("fcntl O_NONBLOCK");
1176 goto error_poll_fcntl
;
1179 ret
= pipe(ctx
->consumer_should_quit
);
1181 PERROR("Error creating recv pipe");
1182 goto error_quit_pipe
;
1185 ret
= pipe(ctx
->consumer_thread_pipe
);
1187 PERROR("Error creating thread pipe");
1188 goto error_thread_pipe
;
1191 ret
= pipe(ctx
->consumer_channel_pipe
);
1193 PERROR("Error creating channel pipe");
1194 goto error_channel_pipe
;
1197 ret
= utils_create_pipe(ctx
->consumer_metadata_pipe
);
1199 goto error_metadata_pipe
;
1202 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1204 goto error_splice_pipe
;
1210 utils_close_pipe(ctx
->consumer_metadata_pipe
);
1211 error_metadata_pipe
:
1212 utils_close_pipe(ctx
->consumer_channel_pipe
);
1214 utils_close_pipe(ctx
->consumer_thread_pipe
);
1216 utils_close_pipe(ctx
->consumer_should_quit
);
1219 utils_close_pipe(ctx
->consumer_data_pipe
);
1227 * Close all fds associated with the instance and free the context.
1229 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1233 DBG("Consumer destroying it. Closing everything.");
1235 ret
= close(ctx
->consumer_error_socket
);
1239 ret
= close(ctx
->consumer_metadata_socket
);
1243 utils_close_pipe(ctx
->consumer_thread_pipe
);
1244 utils_close_pipe(ctx
->consumer_channel_pipe
);
1245 utils_close_pipe(ctx
->consumer_data_pipe
);
1246 utils_close_pipe(ctx
->consumer_should_quit
);
1247 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1249 unlink(ctx
->consumer_command_sock_path
);
1254 * Write the metadata stream id on the specified file descriptor.
1256 static int write_relayd_metadata_id(int fd
,
1257 struct lttng_consumer_stream
*stream
,
1258 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1261 struct lttcomm_relayd_metadata_payload hdr
;
1263 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1264 hdr
.padding_size
= htobe32(padding
);
1266 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1267 } while (ret
< 0 && errno
== EINTR
);
1268 if (ret
< 0 || ret
!= sizeof(hdr
)) {
1270 * This error means that the fd's end is closed so ignore the perror
1271 * not to clubber the error output since this can happen in a normal
1274 if (errno
!= EPIPE
) {
1275 PERROR("write metadata stream id");
1277 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1279 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1280 * handle writting the missing part so report that as an error and
1281 * don't lie to the caller.
1286 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1287 stream
->relayd_stream_id
, padding
);
1294 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1295 * core function for writing trace buffers to either the local filesystem or
1298 * It must be called with the stream lock held.
1300 * Careful review MUST be put if any changes occur!
1302 * Returns the number of bytes written
1304 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1305 struct lttng_consumer_local_data
*ctx
,
1306 struct lttng_consumer_stream
*stream
, unsigned long len
,
1307 unsigned long padding
)
1309 unsigned long mmap_offset
;
1311 ssize_t ret
= 0, written
= 0;
1312 off_t orig_offset
= stream
->out_fd_offset
;
1313 /* Default is on the disk */
1314 int outfd
= stream
->out_fd
;
1315 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1316 unsigned int relayd_hang_up
= 0;
1318 /* RCU lock for the relayd pointer */
1321 /* Flag that the current stream if set for network streaming. */
1322 if (stream
->net_seq_idx
!= -1) {
1323 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1324 if (relayd
== NULL
) {
1329 /* get the offset inside the fd to mmap */
1330 switch (consumer_data
.type
) {
1331 case LTTNG_CONSUMER_KERNEL
:
1332 mmap_base
= stream
->mmap_base
;
1333 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1335 case LTTNG_CONSUMER32_UST
:
1336 case LTTNG_CONSUMER64_UST
:
1337 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1339 ERR("read mmap get mmap base for stream %s", stream
->name
);
1343 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1347 ERR("Unknown consumer_data type");
1352 PERROR("tracer ctl get_mmap_read_offset");
1357 /* Handle stream on the relayd if the output is on the network */
1359 unsigned long netlen
= len
;
1362 * Lock the control socket for the complete duration of the function
1363 * since from this point on we will use the socket.
1365 if (stream
->metadata_flag
) {
1366 /* Metadata requires the control socket. */
1367 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1368 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1371 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1373 /* Use the returned socket. */
1376 /* Write metadata stream id before payload */
1377 if (stream
->metadata_flag
) {
1378 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1381 /* Socket operation failed. We consider the relayd dead */
1382 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1390 /* Socket operation failed. We consider the relayd dead */
1391 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1395 /* Else, use the default set before which is the filesystem. */
1398 /* No streaming, we have to set the len with the full padding */
1402 * Check if we need to change the tracefile before writing the packet.
1404 if (stream
->chan
->tracefile_size
> 0 &&
1405 (stream
->tracefile_size_current
+ len
) >
1406 stream
->chan
->tracefile_size
) {
1407 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1408 stream
->name
, stream
->chan
->tracefile_size
,
1409 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1410 stream
->out_fd
, &(stream
->tracefile_count_current
));
1412 ERR("Rotating output file");
1415 outfd
= stream
->out_fd
= ret
;
1417 stream
->tracefile_size_current
+= len
;
1422 ret
= write(outfd
, mmap_base
+ mmap_offset
, len
);
1423 } while (ret
< 0 && errno
== EINTR
);
1424 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1427 * This is possible if the fd is closed on the other side (outfd)
1428 * or any write problem. It can be verbose a bit for a normal
1429 * execution if for instance the relayd is stopped abruptly. This
1430 * can happen so set this to a DBG statement.
1432 DBG("Error in file write mmap");
1436 /* Socket operation failed. We consider the relayd dead */
1437 if (errno
== EPIPE
|| errno
== EINVAL
) {
1442 } else if (ret
> len
) {
1443 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1451 /* This call is useless on a socket so better save a syscall. */
1453 /* This won't block, but will start writeout asynchronously */
1454 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1455 SYNC_FILE_RANGE_WRITE
);
1456 stream
->out_fd_offset
+= ret
;
1460 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1464 * This is a special case that the relayd has closed its socket. Let's
1465 * cleanup the relayd object and all associated streams.
1467 if (relayd
&& relayd_hang_up
) {
1468 cleanup_relayd(relayd
, ctx
);
1472 /* Unlock only if ctrl socket used */
1473 if (relayd
&& stream
->metadata_flag
) {
1474 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1482 * Splice the data from the ring buffer to the tracefile.
1484 * It must be called with the stream lock held.
1486 * Returns the number of bytes spliced.
1488 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1489 struct lttng_consumer_local_data
*ctx
,
1490 struct lttng_consumer_stream
*stream
, unsigned long len
,
1491 unsigned long padding
)
1493 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1495 off_t orig_offset
= stream
->out_fd_offset
;
1496 int fd
= stream
->wait_fd
;
1497 /* Default is on the disk */
1498 int outfd
= stream
->out_fd
;
1499 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1501 unsigned int relayd_hang_up
= 0;
1503 switch (consumer_data
.type
) {
1504 case LTTNG_CONSUMER_KERNEL
:
1506 case LTTNG_CONSUMER32_UST
:
1507 case LTTNG_CONSUMER64_UST
:
1508 /* Not supported for user space tracing */
1511 ERR("Unknown consumer_data type");
1515 /* RCU lock for the relayd pointer */
1518 /* Flag that the current stream if set for network streaming. */
1519 if (stream
->net_seq_idx
!= -1) {
1520 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1521 if (relayd
== NULL
) {
1527 * Choose right pipe for splice. Metadata and trace data are handled by
1528 * different threads hence the use of two pipes in order not to race or
1529 * corrupt the written data.
1531 if (stream
->metadata_flag
) {
1532 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1534 splice_pipe
= ctx
->consumer_thread_pipe
;
1537 /* Write metadata stream id before payload */
1539 int total_len
= len
;
1541 if (stream
->metadata_flag
) {
1543 * Lock the control socket for the complete duration of the function
1544 * since from this point on we will use the socket.
1546 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1548 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1552 /* Socket operation failed. We consider the relayd dead */
1553 if (ret
== -EBADF
) {
1554 WARN("Remote relayd disconnected. Stopping");
1561 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1564 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1566 /* Use the returned socket. */
1569 /* Socket operation failed. We consider the relayd dead */
1570 if (ret
== -EBADF
) {
1571 WARN("Remote relayd disconnected. Stopping");
1578 /* No streaming, we have to set the len with the full padding */
1582 * Check if we need to change the tracefile before writing the packet.
1584 if (stream
->chan
->tracefile_size
> 0 &&
1585 (stream
->tracefile_size_current
+ len
) >
1586 stream
->chan
->tracefile_size
) {
1587 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1588 stream
->name
, stream
->chan
->tracefile_size
,
1589 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1590 stream
->out_fd
, &(stream
->tracefile_count_current
));
1592 ERR("Rotating output file");
1595 outfd
= stream
->out_fd
= ret
;
1597 stream
->tracefile_size_current
+= len
;
1601 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1602 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1603 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1604 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1605 DBG("splice chan to pipe, ret %zd", ret_splice
);
1606 if (ret_splice
< 0) {
1607 PERROR("Error in relay splice");
1609 written
= ret_splice
;
1615 /* Handle stream on the relayd if the output is on the network */
1617 if (stream
->metadata_flag
) {
1618 size_t metadata_payload_size
=
1619 sizeof(struct lttcomm_relayd_metadata_payload
);
1621 /* Update counter to fit the spliced data */
1622 ret_splice
+= metadata_payload_size
;
1623 len
+= metadata_payload_size
;
1625 * We do this so the return value can match the len passed as
1626 * argument to this function.
1628 written
-= metadata_payload_size
;
1632 /* Splice data out */
1633 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1634 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1635 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1636 if (ret_splice
< 0) {
1637 PERROR("Error in file splice");
1639 written
= ret_splice
;
1641 /* Socket operation failed. We consider the relayd dead */
1642 if (errno
== EBADF
|| errno
== EPIPE
) {
1643 WARN("Remote relayd disconnected. Stopping");
1649 } else if (ret_splice
> len
) {
1651 PERROR("Wrote more data than requested %zd (len: %lu)",
1653 written
+= ret_splice
;
1659 /* This call is useless on a socket so better save a syscall. */
1661 /* This won't block, but will start writeout asynchronously */
1662 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1663 SYNC_FILE_RANGE_WRITE
);
1664 stream
->out_fd_offset
+= ret_splice
;
1666 written
+= ret_splice
;
1668 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1676 * This is a special case that the relayd has closed its socket. Let's
1677 * cleanup the relayd object and all associated streams.
1679 if (relayd
&& relayd_hang_up
) {
1680 cleanup_relayd(relayd
, ctx
);
1681 /* Skip splice error so the consumer does not fail */
1686 /* send the appropriate error description to sessiond */
1689 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1692 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1695 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1700 if (relayd
&& stream
->metadata_flag
) {
1701 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1709 * Take a snapshot for a specific fd
1711 * Returns 0 on success, < 0 on error
1713 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1715 switch (consumer_data
.type
) {
1716 case LTTNG_CONSUMER_KERNEL
:
1717 return lttng_kconsumer_take_snapshot(stream
);
1718 case LTTNG_CONSUMER32_UST
:
1719 case LTTNG_CONSUMER64_UST
:
1720 return lttng_ustconsumer_take_snapshot(stream
);
1722 ERR("Unknown consumer_data type");
1729 * Get the produced position
1731 * Returns 0 on success, < 0 on error
1733 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1736 switch (consumer_data
.type
) {
1737 case LTTNG_CONSUMER_KERNEL
:
1738 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1739 case LTTNG_CONSUMER32_UST
:
1740 case LTTNG_CONSUMER64_UST
:
1741 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1743 ERR("Unknown consumer_data type");
1749 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1750 int sock
, struct pollfd
*consumer_sockpoll
)
1752 switch (consumer_data
.type
) {
1753 case LTTNG_CONSUMER_KERNEL
:
1754 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1755 case LTTNG_CONSUMER32_UST
:
1756 case LTTNG_CONSUMER64_UST
:
1757 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1759 ERR("Unknown consumer_data type");
1766 * Iterate over all streams of the hashtable and free them properly.
1768 * WARNING: *MUST* be used with data stream only.
1770 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1772 struct lttng_ht_iter iter
;
1773 struct lttng_consumer_stream
*stream
;
1780 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1782 * Ignore return value since we are currently cleaning up so any error
1785 (void) consumer_del_stream(stream
, ht
);
1789 lttng_ht_destroy(ht
);
1793 * Iterate over all streams of the hashtable and free them properly.
1795 * XXX: Should not be only for metadata stream or else use an other name.
1797 static void destroy_stream_ht(struct lttng_ht
*ht
)
1799 struct lttng_ht_iter iter
;
1800 struct lttng_consumer_stream
*stream
;
1807 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1809 * Ignore return value since we are currently cleaning up so any error
1812 (void) consumer_del_metadata_stream(stream
, ht
);
1816 lttng_ht_destroy(ht
);
1819 void lttng_consumer_close_metadata(void)
1821 switch (consumer_data
.type
) {
1822 case LTTNG_CONSUMER_KERNEL
:
1824 * The Kernel consumer has a different metadata scheme so we don't
1825 * close anything because the stream will be closed by the session
1829 case LTTNG_CONSUMER32_UST
:
1830 case LTTNG_CONSUMER64_UST
:
1832 * Close all metadata streams. The metadata hash table is passed and
1833 * this call iterates over it by closing all wakeup fd. This is safe
1834 * because at this point we are sure that the metadata producer is
1835 * either dead or blocked.
1837 lttng_ustconsumer_close_metadata(metadata_ht
);
1840 ERR("Unknown consumer_data type");
1846 * Clean up a metadata stream and free its memory.
1848 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1849 struct lttng_ht
*ht
)
1852 struct lttng_ht_iter iter
;
1853 struct lttng_consumer_channel
*free_chan
= NULL
;
1854 struct consumer_relayd_sock_pair
*relayd
;
1858 * This call should NEVER receive regular stream. It must always be
1859 * metadata stream and this is crucial for data structure synchronization.
1861 assert(stream
->metadata_flag
);
1863 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1866 /* Means the stream was allocated but not successfully added */
1867 goto free_stream_rcu
;
1870 pthread_mutex_lock(&consumer_data
.lock
);
1871 pthread_mutex_lock(&stream
->lock
);
1873 switch (consumer_data
.type
) {
1874 case LTTNG_CONSUMER_KERNEL
:
1875 if (stream
->mmap_base
!= NULL
) {
1876 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1878 PERROR("munmap metadata stream");
1882 case LTTNG_CONSUMER32_UST
:
1883 case LTTNG_CONSUMER64_UST
:
1884 lttng_ustconsumer_del_stream(stream
);
1887 ERR("Unknown consumer_data type");
1893 iter
.iter
.node
= &stream
->node
.node
;
1894 ret
= lttng_ht_del(ht
, &iter
);
1897 iter
.iter
.node
= &stream
->node_channel_id
.node
;
1898 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
1901 iter
.iter
.node
= &stream
->node_session_id
.node
;
1902 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1906 if (stream
->out_fd
>= 0) {
1907 ret
= close(stream
->out_fd
);
1913 /* Check and cleanup relayd */
1915 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1916 if (relayd
!= NULL
) {
1917 uatomic_dec(&relayd
->refcount
);
1918 assert(uatomic_read(&relayd
->refcount
) >= 0);
1920 /* Closing streams requires to lock the control socket. */
1921 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1922 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1923 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1924 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1926 DBG("Unable to close stream on the relayd. Continuing");
1928 * Continue here. There is nothing we can do for the relayd.
1929 * Chances are that the relayd has closed the socket so we just
1930 * continue cleaning up.
1934 /* Both conditions are met, we destroy the relayd. */
1935 if (uatomic_read(&relayd
->refcount
) == 0 &&
1936 uatomic_read(&relayd
->destroy_flag
)) {
1937 destroy_relayd(relayd
);
1942 /* Atomically decrement channel refcount since other threads can use it. */
1943 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
1944 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
1945 /* Go for channel deletion! */
1946 free_chan
= stream
->chan
;
1950 pthread_mutex_unlock(&stream
->lock
);
1951 pthread_mutex_unlock(&consumer_data
.lock
);
1954 consumer_del_channel(free_chan
);
1958 call_rcu(&stream
->node
.head
, free_stream_rcu
);
1962 * Action done with the metadata stream when adding it to the consumer internal
1963 * data structures to handle it.
1965 static int add_metadata_stream(struct lttng_consumer_stream
*stream
,
1966 struct lttng_ht
*ht
)
1969 struct consumer_relayd_sock_pair
*relayd
;
1970 struct lttng_ht_iter iter
;
1971 struct lttng_ht_node_u64
*node
;
1976 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
1978 pthread_mutex_lock(&consumer_data
.lock
);
1979 pthread_mutex_lock(&stream
->lock
);
1982 * From here, refcounts are updated so be _careful_ when returning an error
1989 * Lookup the stream just to make sure it does not exist in our internal
1990 * state. This should NEVER happen.
1992 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
1993 node
= lttng_ht_iter_get_node_u64(&iter
);
1996 /* Find relayd and, if one is found, increment refcount. */
1997 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1998 if (relayd
!= NULL
) {
1999 uatomic_inc(&relayd
->refcount
);
2002 /* Update channel refcount once added without error(s). */
2003 uatomic_inc(&stream
->chan
->refcount
);
2006 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2007 * in terms of destroying the associated channel, because the action that
2008 * causes the count to become 0 also causes a stream to be added. The
2009 * channel deletion will thus be triggered by the following removal of this
2012 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2013 /* Increment refcount before decrementing nb_init_stream_left */
2015 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2018 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2020 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2021 &stream
->node_channel_id
);
2024 * Add stream to the stream_list_ht of the consumer data. No need to steal
2025 * the key since the HT does not use it and we allow to add redundant keys
2028 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2032 pthread_mutex_unlock(&stream
->lock
);
2033 pthread_mutex_unlock(&consumer_data
.lock
);
2038 * Delete data stream that are flagged for deletion (endpoint_status).
2040 static void validate_endpoint_status_data_stream(void)
2042 struct lttng_ht_iter iter
;
2043 struct lttng_consumer_stream
*stream
;
2045 DBG("Consumer delete flagged data stream");
2048 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2049 /* Validate delete flag of the stream */
2050 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2053 /* Delete it right now */
2054 consumer_del_stream(stream
, data_ht
);
2060 * Delete metadata stream that are flagged for deletion (endpoint_status).
2062 static void validate_endpoint_status_metadata_stream(
2063 struct lttng_poll_event
*pollset
)
2065 struct lttng_ht_iter iter
;
2066 struct lttng_consumer_stream
*stream
;
2068 DBG("Consumer delete flagged metadata stream");
2073 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2074 /* Validate delete flag of the stream */
2075 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2079 * Remove from pollset so the metadata thread can continue without
2080 * blocking on a deleted stream.
2082 lttng_poll_del(pollset
, stream
->wait_fd
);
2084 /* Delete it right now */
2085 consumer_del_metadata_stream(stream
, metadata_ht
);
2091 * Thread polls on metadata file descriptor and write them on disk or on the
2094 void *consumer_thread_metadata_poll(void *data
)
2097 uint32_t revents
, nb_fd
;
2098 struct lttng_consumer_stream
*stream
= NULL
;
2099 struct lttng_ht_iter iter
;
2100 struct lttng_ht_node_u64
*node
;
2101 struct lttng_poll_event events
;
2102 struct lttng_consumer_local_data
*ctx
= data
;
2105 rcu_register_thread();
2107 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2109 /* ENOMEM at this point. Better to bail out. */
2113 DBG("Thread metadata poll started");
2115 /* Size is set to 1 for the consumer_metadata pipe */
2116 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2118 ERR("Poll set creation failed");
2122 ret
= lttng_poll_add(&events
, ctx
->consumer_metadata_pipe
[0], LPOLLIN
);
2128 DBG("Metadata main loop started");
2131 /* Only the metadata pipe is set */
2132 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2137 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2138 ret
= lttng_poll_wait(&events
, -1);
2139 DBG("Metadata event catched in thread");
2141 if (errno
== EINTR
) {
2142 ERR("Poll EINTR catched");
2150 /* From here, the event is a metadata wait fd */
2151 for (i
= 0; i
< nb_fd
; i
++) {
2152 revents
= LTTNG_POLL_GETEV(&events
, i
);
2153 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2155 /* Just don't waste time if no returned events for the fd */
2160 if (pollfd
== ctx
->consumer_metadata_pipe
[0]) {
2161 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2162 DBG("Metadata thread pipe hung up");
2164 * Remove the pipe from the poll set and continue the loop
2165 * since their might be data to consume.
2167 lttng_poll_del(&events
, ctx
->consumer_metadata_pipe
[0]);
2168 ret
= close(ctx
->consumer_metadata_pipe
[0]);
2170 PERROR("close metadata pipe");
2173 } else if (revents
& LPOLLIN
) {
2175 /* Get the stream pointer received */
2176 ret
= read(pollfd
, &stream
, sizeof(stream
));
2177 } while (ret
< 0 && errno
== EINTR
);
2179 ret
< sizeof(struct lttng_consumer_stream
*)) {
2180 PERROR("read metadata stream");
2182 * Let's continue here and hope we can still work
2183 * without stopping the consumer. XXX: Should we?
2188 /* A NULL stream means that the state has changed. */
2189 if (stream
== NULL
) {
2190 /* Check for deleted streams. */
2191 validate_endpoint_status_metadata_stream(&events
);
2195 DBG("Adding metadata stream %d to poll set",
2198 ret
= add_metadata_stream(stream
, metadata_ht
);
2200 ERR("Unable to add metadata stream");
2201 /* Stream was not setup properly. Continuing. */
2202 consumer_del_metadata_stream(stream
, NULL
);
2206 /* Add metadata stream to the global poll events list */
2207 lttng_poll_add(&events
, stream
->wait_fd
,
2208 LPOLLIN
| LPOLLPRI
);
2211 /* Handle other stream */
2217 uint64_t tmp_id
= (uint64_t) pollfd
;
2219 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2221 node
= lttng_ht_iter_get_node_u64(&iter
);
2224 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2227 /* Check for error event */
2228 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2229 DBG("Metadata fd %d is hup|err.", pollfd
);
2230 if (!stream
->hangup_flush_done
2231 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2232 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2233 DBG("Attempting to flush and consume the UST buffers");
2234 lttng_ustconsumer_on_stream_hangup(stream
);
2236 /* We just flushed the stream now read it. */
2238 len
= ctx
->on_buffer_ready(stream
, ctx
);
2240 * We don't check the return value here since if we get
2241 * a negative len, it means an error occured thus we
2242 * simply remove it from the poll set and free the
2248 lttng_poll_del(&events
, stream
->wait_fd
);
2250 * This call update the channel states, closes file descriptors
2251 * and securely free the stream.
2253 consumer_del_metadata_stream(stream
, metadata_ht
);
2254 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2255 /* Get the data out of the metadata file descriptor */
2256 DBG("Metadata available on fd %d", pollfd
);
2257 assert(stream
->wait_fd
== pollfd
);
2259 len
= ctx
->on_buffer_ready(stream
, ctx
);
2260 /* It's ok to have an unavailable sub-buffer */
2261 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2262 /* Clean up stream from consumer and free it. */
2263 lttng_poll_del(&events
, stream
->wait_fd
);
2264 consumer_del_metadata_stream(stream
, metadata_ht
);
2265 } else if (len
> 0) {
2266 stream
->data_read
= 1;
2270 /* Release RCU lock for the stream looked up */
2277 DBG("Metadata poll thread exiting");
2279 lttng_poll_clean(&events
);
2281 destroy_stream_ht(metadata_ht
);
2283 rcu_unregister_thread();
2288 * This thread polls the fds in the set to consume the data and write
2289 * it to tracefile if necessary.
2291 void *consumer_thread_data_poll(void *data
)
2293 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2294 struct pollfd
*pollfd
= NULL
;
2295 /* local view of the streams */
2296 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2297 /* local view of consumer_data.fds_count */
2299 struct lttng_consumer_local_data
*ctx
= data
;
2302 rcu_register_thread();
2304 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2305 if (data_ht
== NULL
) {
2306 /* ENOMEM at this point. Better to bail out. */
2310 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
2317 * the fds set has been updated, we need to update our
2318 * local array as well
2320 pthread_mutex_lock(&consumer_data
.lock
);
2321 if (consumer_data
.need_update
) {
2326 local_stream
= NULL
;
2328 /* allocate for all fds + 1 for the consumer_data_pipe */
2329 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2330 if (pollfd
== NULL
) {
2331 PERROR("pollfd malloc");
2332 pthread_mutex_unlock(&consumer_data
.lock
);
2336 /* allocate for all fds + 1 for the consumer_data_pipe */
2337 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2338 sizeof(struct lttng_consumer_stream
));
2339 if (local_stream
== NULL
) {
2340 PERROR("local_stream malloc");
2341 pthread_mutex_unlock(&consumer_data
.lock
);
2344 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2347 ERR("Error in allocating pollfd or local_outfds");
2348 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2349 pthread_mutex_unlock(&consumer_data
.lock
);
2353 consumer_data
.need_update
= 0;
2355 pthread_mutex_unlock(&consumer_data
.lock
);
2357 /* No FDs and consumer_quit, consumer_cleanup the thread */
2358 if (nb_fd
== 0 && consumer_quit
== 1) {
2361 /* poll on the array of fds */
2363 DBG("polling on %d fd", nb_fd
+ 1);
2364 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2365 DBG("poll num_rdy : %d", num_rdy
);
2366 if (num_rdy
== -1) {
2368 * Restart interrupted system call.
2370 if (errno
== EINTR
) {
2373 PERROR("Poll error");
2374 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2376 } else if (num_rdy
== 0) {
2377 DBG("Polling thread timed out");
2382 * If the consumer_data_pipe triggered poll go directly to the
2383 * beginning of the loop to update the array. We want to prioritize
2384 * array update over low-priority reads.
2386 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2387 ssize_t pipe_readlen
;
2389 DBG("consumer_data_pipe wake up");
2390 /* Consume 1 byte of pipe data */
2392 pipe_readlen
= read(ctx
->consumer_data_pipe
[0], &new_stream
,
2393 sizeof(new_stream
));
2394 } while (pipe_readlen
== -1 && errno
== EINTR
);
2395 if (pipe_readlen
< 0) {
2396 PERROR("read consumer data pipe");
2397 /* Continue so we can at least handle the current stream(s). */
2402 * If the stream is NULL, just ignore it. It's also possible that
2403 * the sessiond poll thread changed the consumer_quit state and is
2404 * waking us up to test it.
2406 if (new_stream
== NULL
) {
2407 validate_endpoint_status_data_stream();
2411 ret
= add_stream(new_stream
, data_ht
);
2413 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
2416 * At this point, if the add_stream fails, it is not in the
2417 * hash table thus passing the NULL value here.
2419 consumer_del_stream(new_stream
, NULL
);
2422 /* Continue to update the local streams and handle prio ones */
2426 /* Take care of high priority channels first. */
2427 for (i
= 0; i
< nb_fd
; i
++) {
2428 if (local_stream
[i
] == NULL
) {
2431 if (pollfd
[i
].revents
& POLLPRI
) {
2432 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2434 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2435 /* it's ok to have an unavailable sub-buffer */
2436 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2437 /* Clean the stream and free it. */
2438 consumer_del_stream(local_stream
[i
], data_ht
);
2439 local_stream
[i
] = NULL
;
2440 } else if (len
> 0) {
2441 local_stream
[i
]->data_read
= 1;
2447 * If we read high prio channel in this loop, try again
2448 * for more high prio data.
2454 /* Take care of low priority channels. */
2455 for (i
= 0; i
< nb_fd
; i
++) {
2456 if (local_stream
[i
] == NULL
) {
2459 if ((pollfd
[i
].revents
& POLLIN
) ||
2460 local_stream
[i
]->hangup_flush_done
) {
2461 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2462 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2463 /* it's ok to have an unavailable sub-buffer */
2464 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2465 /* Clean the stream and free it. */
2466 consumer_del_stream(local_stream
[i
], data_ht
);
2467 local_stream
[i
] = NULL
;
2468 } else if (len
> 0) {
2469 local_stream
[i
]->data_read
= 1;
2474 /* Handle hangup and errors */
2475 for (i
= 0; i
< nb_fd
; i
++) {
2476 if (local_stream
[i
] == NULL
) {
2479 if (!local_stream
[i
]->hangup_flush_done
2480 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2481 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2482 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2483 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2485 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2486 /* Attempt read again, for the data we just flushed. */
2487 local_stream
[i
]->data_read
= 1;
2490 * If the poll flag is HUP/ERR/NVAL and we have
2491 * read no data in this pass, we can remove the
2492 * stream from its hash table.
2494 if ((pollfd
[i
].revents
& POLLHUP
)) {
2495 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2496 if (!local_stream
[i
]->data_read
) {
2497 consumer_del_stream(local_stream
[i
], data_ht
);
2498 local_stream
[i
] = NULL
;
2501 } else if (pollfd
[i
].revents
& POLLERR
) {
2502 ERR("Error returned in polling fd %d.", 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
& POLLNVAL
) {
2509 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2510 if (!local_stream
[i
]->data_read
) {
2511 consumer_del_stream(local_stream
[i
], data_ht
);
2512 local_stream
[i
] = NULL
;
2516 if (local_stream
[i
] != NULL
) {
2517 local_stream
[i
]->data_read
= 0;
2522 DBG("polling thread exiting");
2527 * Close the write side of the pipe so epoll_wait() in
2528 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2529 * read side of the pipe. If we close them both, epoll_wait strangely does
2530 * not return and could create a endless wait period if the pipe is the
2531 * only tracked fd in the poll set. The thread will take care of closing
2534 ret
= close(ctx
->consumer_metadata_pipe
[1]);
2536 PERROR("close data pipe");
2539 destroy_data_stream_ht(data_ht
);
2541 rcu_unregister_thread();
2546 * Close wake-up end of each stream belonging to the channel. This will
2547 * allow the poll() on the stream read-side to detect when the
2548 * write-side (application) finally closes them.
2551 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2553 struct lttng_ht
*ht
;
2554 struct lttng_consumer_stream
*stream
;
2555 struct lttng_ht_iter iter
;
2557 ht
= consumer_data
.stream_per_chan_id_ht
;
2560 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2561 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2562 ht
->match_fct
, &channel
->key
,
2563 &iter
.iter
, stream
, node_channel_id
.node
) {
2565 * Protect against teardown with mutex.
2567 pthread_mutex_lock(&stream
->lock
);
2568 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2571 switch (consumer_data
.type
) {
2572 case LTTNG_CONSUMER_KERNEL
:
2574 case LTTNG_CONSUMER32_UST
:
2575 case LTTNG_CONSUMER64_UST
:
2577 * Note: a mutex is taken internally within
2578 * liblttng-ust-ctl to protect timer wakeup_fd
2579 * use from concurrent close.
2581 lttng_ustconsumer_close_stream_wakeup(stream
);
2584 ERR("Unknown consumer_data type");
2588 pthread_mutex_unlock(&stream
->lock
);
2593 static void destroy_channel_ht(struct lttng_ht
*ht
)
2595 struct lttng_ht_iter iter
;
2596 struct lttng_consumer_channel
*channel
;
2604 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2605 ret
= lttng_ht_del(ht
, &iter
);
2610 lttng_ht_destroy(ht
);
2614 * This thread polls the channel fds to detect when they are being
2615 * closed. It closes all related streams if the channel is detected as
2616 * closed. It is currently only used as a shim layer for UST because the
2617 * consumerd needs to keep the per-stream wakeup end of pipes open for
2620 void *consumer_thread_channel_poll(void *data
)
2623 uint32_t revents
, nb_fd
;
2624 struct lttng_consumer_channel
*chan
= NULL
;
2625 struct lttng_ht_iter iter
;
2626 struct lttng_ht_node_u64
*node
;
2627 struct lttng_poll_event events
;
2628 struct lttng_consumer_local_data
*ctx
= data
;
2629 struct lttng_ht
*channel_ht
;
2631 rcu_register_thread();
2633 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2635 /* ENOMEM at this point. Better to bail out. */
2639 DBG("Thread channel poll started");
2641 /* Size is set to 1 for the consumer_channel pipe */
2642 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2644 ERR("Poll set creation failed");
2648 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2654 DBG("Channel main loop started");
2657 /* Only the channel pipe is set */
2658 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2663 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2664 ret
= lttng_poll_wait(&events
, -1);
2665 DBG("Channel event catched in thread");
2667 if (errno
== EINTR
) {
2668 ERR("Poll EINTR catched");
2676 /* From here, the event is a channel wait fd */
2677 for (i
= 0; i
< nb_fd
; i
++) {
2678 revents
= LTTNG_POLL_GETEV(&events
, i
);
2679 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2681 /* Just don't waste time if no returned events for the fd */
2685 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2686 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2687 DBG("Channel thread pipe hung up");
2689 * Remove the pipe from the poll set and continue the loop
2690 * since their might be data to consume.
2692 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2694 } else if (revents
& LPOLLIN
) {
2695 enum consumer_channel_action action
;
2697 ret
= read_channel_pipe(ctx
, &chan
, &action
);
2699 ERR("Error reading channel pipe");
2704 case CONSUMER_CHANNEL_ADD
:
2705 DBG("Adding channel %d to poll set",
2708 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2710 lttng_ht_add_unique_u64(channel_ht
,
2711 &chan
->wait_fd_node
);
2712 /* Add channel to the global poll events list */
2713 lttng_poll_add(&events
, chan
->wait_fd
,
2714 LPOLLIN
| LPOLLPRI
);
2716 case CONSUMER_CHANNEL_QUIT
:
2718 * Remove the pipe from the poll set and continue the loop
2719 * since their might be data to consume.
2721 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2724 ERR("Unknown action");
2729 /* Handle other stream */
2735 uint64_t tmp_id
= (uint64_t) pollfd
;
2737 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2739 node
= lttng_ht_iter_get_node_u64(&iter
);
2742 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2745 /* Check for error event */
2746 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2747 DBG("Channel fd %d is hup|err.", pollfd
);
2749 lttng_poll_del(&events
, chan
->wait_fd
);
2750 ret
= lttng_ht_del(channel_ht
, &iter
);
2752 consumer_close_channel_streams(chan
);
2754 /* Release our own refcount */
2755 if (!uatomic_sub_return(&chan
->refcount
, 1)
2756 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2757 consumer_del_channel(chan
);
2761 /* Release RCU lock for the channel looked up */
2767 lttng_poll_clean(&events
);
2769 destroy_channel_ht(channel_ht
);
2771 DBG("Channel poll thread exiting");
2772 rcu_unregister_thread();
2776 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2777 struct pollfd
*sockpoll
, int client_socket
)
2784 if (lttng_consumer_poll_socket(sockpoll
) < 0) {
2788 DBG("Metadata connection on client_socket");
2790 /* Blocking call, waiting for transmission */
2791 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2792 if (ctx
->consumer_metadata_socket
< 0) {
2793 WARN("On accept metadata");
2804 * This thread listens on the consumerd socket and receives the file
2805 * descriptors from the session daemon.
2807 void *consumer_thread_sessiond_poll(void *data
)
2809 int sock
= -1, client_socket
, ret
;
2811 * structure to poll for incoming data on communication socket avoids
2812 * making blocking sockets.
2814 struct pollfd consumer_sockpoll
[2];
2815 struct lttng_consumer_local_data
*ctx
= data
;
2817 rcu_register_thread();
2819 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2820 unlink(ctx
->consumer_command_sock_path
);
2821 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2822 if (client_socket
< 0) {
2823 ERR("Cannot create command socket");
2827 ret
= lttcomm_listen_unix_sock(client_socket
);
2832 DBG("Sending ready command to lttng-sessiond");
2833 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2834 /* return < 0 on error, but == 0 is not fatal */
2836 ERR("Error sending ready command to lttng-sessiond");
2840 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2842 PERROR("fcntl O_NONBLOCK");
2846 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2847 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2848 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2849 consumer_sockpoll
[1].fd
= client_socket
;
2850 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2852 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2855 DBG("Connection on client_socket");
2857 /* Blocking call, waiting for transmission */
2858 sock
= lttcomm_accept_unix_sock(client_socket
);
2863 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2865 PERROR("fcntl O_NONBLOCK");
2870 * Setup metadata socket which is the second socket connection on the
2871 * command unix socket.
2873 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
2878 /* This socket is not useful anymore. */
2879 ret
= close(client_socket
);
2881 PERROR("close client_socket");
2885 /* update the polling structure to poll on the established socket */
2886 consumer_sockpoll
[1].fd
= sock
;
2887 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2890 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2893 DBG("Incoming command on sock");
2894 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2895 if (ret
== -ENOENT
) {
2896 DBG("Received STOP command");
2901 * This could simply be a session daemon quitting. Don't output
2904 DBG("Communication interrupted on command socket");
2907 if (consumer_quit
) {
2908 DBG("consumer_thread_receive_fds received quit from signal");
2911 DBG("received command on sock");
2914 DBG("Consumer thread sessiond poll exiting");
2917 * Close metadata streams since the producer is the session daemon which
2920 * NOTE: for now, this only applies to the UST tracer.
2922 lttng_consumer_close_metadata();
2925 * when all fds have hung up, the polling thread
2931 * Notify the data poll thread to poll back again and test the
2932 * consumer_quit state that we just set so to quit gracefully.
2934 notify_thread_pipe(ctx
->consumer_data_pipe
[1]);
2936 notify_channel_pipe(ctx
, NULL
, CONSUMER_CHANNEL_QUIT
);
2938 /* Cleaning up possibly open sockets. */
2942 PERROR("close sock sessiond poll");
2945 if (client_socket
>= 0) {
2948 PERROR("close client_socket sessiond poll");
2952 rcu_unregister_thread();
2956 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2957 struct lttng_consumer_local_data
*ctx
)
2961 pthread_mutex_lock(&stream
->lock
);
2963 switch (consumer_data
.type
) {
2964 case LTTNG_CONSUMER_KERNEL
:
2965 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
2967 case LTTNG_CONSUMER32_UST
:
2968 case LTTNG_CONSUMER64_UST
:
2969 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2972 ERR("Unknown consumer_data type");
2978 pthread_mutex_unlock(&stream
->lock
);
2982 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2984 switch (consumer_data
.type
) {
2985 case LTTNG_CONSUMER_KERNEL
:
2986 return lttng_kconsumer_on_recv_stream(stream
);
2987 case LTTNG_CONSUMER32_UST
:
2988 case LTTNG_CONSUMER64_UST
:
2989 return lttng_ustconsumer_on_recv_stream(stream
);
2991 ERR("Unknown consumer_data type");
2998 * Allocate and set consumer data hash tables.
3000 void lttng_consumer_init(void)
3002 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3003 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3004 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3005 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3009 * Process the ADD_RELAYD command receive by a consumer.
3011 * This will create a relayd socket pair and add it to the relayd hash table.
3012 * The caller MUST acquire a RCU read side lock before calling it.
3014 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
3015 struct lttng_consumer_local_data
*ctx
, int sock
,
3016 struct pollfd
*consumer_sockpoll
,
3017 struct lttcomm_relayd_sock
*relayd_sock
, unsigned int sessiond_id
)
3019 int fd
= -1, ret
= -1, relayd_created
= 0;
3020 enum lttng_error_code ret_code
= LTTNG_OK
;
3021 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3024 assert(relayd_sock
);
3026 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
3028 /* First send a status message before receiving the fds. */
3029 ret
= consumer_send_status_msg(sock
, ret_code
);
3031 /* Somehow, the session daemon is not responding anymore. */
3035 /* Get relayd reference if exists. */
3036 relayd
= consumer_find_relayd(net_seq_idx
);
3037 if (relayd
== NULL
) {
3038 /* Not found. Allocate one. */
3039 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3040 if (relayd
== NULL
) {
3041 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
3045 relayd
->sessiond_session_id
= (uint64_t) sessiond_id
;
3049 /* Poll on consumer socket. */
3050 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
3055 /* Get relayd socket from session daemon */
3056 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3057 if (ret
!= sizeof(fd
)) {
3058 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3060 fd
= -1; /* Just in case it gets set with an invalid value. */
3064 /* We have the fds without error. Send status back. */
3065 ret
= consumer_send_status_msg(sock
, ret_code
);
3067 /* Somehow, the session daemon is not responding anymore. */
3071 /* Copy socket information and received FD */
3072 switch (sock_type
) {
3073 case LTTNG_STREAM_CONTROL
:
3074 /* Copy received lttcomm socket */
3075 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3076 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3077 /* Immediately try to close the created socket if valid. */
3078 if (relayd
->control_sock
.sock
.fd
>= 0) {
3079 if (close(relayd
->control_sock
.sock
.fd
)) {
3080 PERROR("close relayd control socket");
3083 /* Handle create_sock error. */
3088 /* Assign new file descriptor */
3089 relayd
->control_sock
.sock
.fd
= fd
;
3090 /* Assign version values. */
3091 relayd
->control_sock
.major
= relayd_sock
->major
;
3092 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3095 * Create a session on the relayd and store the returned id. Lock the
3096 * control socket mutex if the relayd was NOT created before.
3098 if (!relayd_created
) {
3099 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3101 ret
= relayd_create_session(&relayd
->control_sock
,
3102 &relayd
->relayd_session_id
);
3103 if (!relayd_created
) {
3104 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3108 * Close all sockets of a relayd object. It will be freed if it was
3109 * created at the error code path or else it will be garbage
3112 (void) relayd_close(&relayd
->control_sock
);
3113 (void) relayd_close(&relayd
->data_sock
);
3118 case LTTNG_STREAM_DATA
:
3119 /* Copy received lttcomm socket */
3120 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3121 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3122 /* Immediately try to close the created socket if valid. */
3123 if (relayd
->data_sock
.sock
.fd
>= 0) {
3124 if (close(relayd
->data_sock
.sock
.fd
)) {
3125 PERROR("close relayd data socket");
3128 /* Handle create_sock error. */
3133 /* Assign new file descriptor */
3134 relayd
->data_sock
.sock
.fd
= fd
;
3135 /* Assign version values. */
3136 relayd
->data_sock
.major
= relayd_sock
->major
;
3137 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3140 ERR("Unknown relayd socket type (%d)", sock_type
);
3145 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3146 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3147 relayd
->net_seq_idx
, fd
);
3150 * Add relayd socket pair to consumer data hashtable. If object already
3151 * exists or on error, the function gracefully returns.
3159 /* Close received socket if valid. */
3162 PERROR("close received socket");
3167 if (relayd_created
) {
3175 * Try to lock the stream mutex.
3177 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3179 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3186 * Try to lock the stream mutex. On failure, we know that the stream is
3187 * being used else where hence there is data still being extracted.
3189 ret
= pthread_mutex_trylock(&stream
->lock
);
3191 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3203 * Search for a relayd associated to the session id and return the reference.
3205 * A rcu read side lock MUST be acquire before calling this function and locked
3206 * until the relayd object is no longer necessary.
3208 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3210 struct lttng_ht_iter iter
;
3211 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3213 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3214 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3217 * Check by sessiond id which is unique here where the relayd session
3218 * id might not be when having multiple relayd.
3220 if (relayd
->sessiond_session_id
== id
) {
3221 /* Found the relayd. There can be only one per id. */
3233 * Check if for a given session id there is still data needed to be extract
3236 * Return 1 if data is pending or else 0 meaning ready to be read.
3238 int consumer_data_pending(uint64_t id
)
3241 struct lttng_ht_iter iter
;
3242 struct lttng_ht
*ht
;
3243 struct lttng_consumer_stream
*stream
;
3244 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3245 int (*data_pending
)(struct lttng_consumer_stream
*);
3247 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3250 pthread_mutex_lock(&consumer_data
.lock
);
3252 switch (consumer_data
.type
) {
3253 case LTTNG_CONSUMER_KERNEL
:
3254 data_pending
= lttng_kconsumer_data_pending
;
3256 case LTTNG_CONSUMER32_UST
:
3257 case LTTNG_CONSUMER64_UST
:
3258 data_pending
= lttng_ustconsumer_data_pending
;
3261 ERR("Unknown consumer data type");
3265 /* Ease our life a bit */
3266 ht
= consumer_data
.stream_list_ht
;
3268 relayd
= find_relayd_by_session_id(id
);
3270 /* Send init command for data pending. */
3271 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3272 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3273 relayd
->relayd_session_id
);
3274 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3276 /* Communication error thus the relayd so no data pending. */
3277 goto data_not_pending
;
3281 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3282 ht
->hash_fct(&id
, lttng_ht_seed
),
3284 &iter
.iter
, stream
, node_session_id
.node
) {
3285 /* If this call fails, the stream is being used hence data pending. */
3286 ret
= stream_try_lock(stream
);
3292 * A removed node from the hash table indicates that the stream has
3293 * been deleted thus having a guarantee that the buffers are closed
3294 * on the consumer side. However, data can still be transmitted
3295 * over the network so don't skip the relayd check.
3297 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3299 /* Check the stream if there is data in the buffers. */
3300 ret
= data_pending(stream
);
3302 pthread_mutex_unlock(&stream
->lock
);
3309 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3310 if (stream
->metadata_flag
) {
3311 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3312 stream
->relayd_stream_id
);
3314 ret
= relayd_data_pending(&relayd
->control_sock
,
3315 stream
->relayd_stream_id
,
3316 stream
->next_net_seq_num
- 1);
3318 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3320 pthread_mutex_unlock(&stream
->lock
);
3324 pthread_mutex_unlock(&stream
->lock
);
3328 unsigned int is_data_inflight
= 0;
3330 /* Send init command for data pending. */
3331 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3332 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3333 relayd
->relayd_session_id
, &is_data_inflight
);
3334 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3336 goto data_not_pending
;
3338 if (is_data_inflight
) {
3344 * Finding _no_ node in the hash table and no inflight data means that the
3345 * stream(s) have been removed thus data is guaranteed to be available for
3346 * analysis from the trace files.
3350 /* Data is available to be read by a viewer. */
3351 pthread_mutex_unlock(&consumer_data
.lock
);
3356 /* Data is still being extracted from buffers. */
3357 pthread_mutex_unlock(&consumer_data
.lock
);
3363 * Send a ret code status message to the sessiond daemon.
3365 * Return the sendmsg() return value.
3367 int consumer_send_status_msg(int sock
, int ret_code
)
3369 struct lttcomm_consumer_status_msg msg
;
3371 msg
.ret_code
= ret_code
;
3373 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3377 * Send a channel status message to the sessiond daemon.
3379 * Return the sendmsg() return value.
3381 int consumer_send_status_channel(int sock
,
3382 struct lttng_consumer_channel
*channel
)
3384 struct lttcomm_consumer_status_channel msg
;
3389 msg
.ret_code
= -LTTNG_ERR_UST_CHAN_FAIL
;
3391 msg
.ret_code
= LTTNG_OK
;
3392 msg
.key
= channel
->key
;
3393 msg
.stream_count
= channel
->streams
.count
;
3396 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));