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
));
106 ret
= write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
107 } while (ret
< 0 && errno
== EINTR
);
110 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
113 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
116 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
117 struct lttng_consumer_channel
**chan
,
119 enum consumer_channel_action
*action
)
121 struct consumer_channel_msg msg
;
125 ret
= read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
126 } while (ret
< 0 && errno
== EINTR
);
128 *action
= msg
.action
;
136 * Find a stream. The consumer_data.lock must be locked during this
139 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
142 struct lttng_ht_iter iter
;
143 struct lttng_ht_node_u64
*node
;
144 struct lttng_consumer_stream
*stream
= NULL
;
148 /* -1ULL keys are lookup failures */
149 if (key
== (uint64_t) -1ULL) {
155 lttng_ht_lookup(ht
, &key
, &iter
);
156 node
= lttng_ht_iter_get_node_u64(&iter
);
158 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
166 static void steal_stream_key(int key
, struct lttng_ht
*ht
)
168 struct lttng_consumer_stream
*stream
;
171 stream
= find_stream(key
, ht
);
175 * We don't want the lookup to match, but we still need
176 * to iterate on this stream when iterating over the hash table. Just
177 * change the node key.
179 stream
->node
.key
= -1ULL;
185 * Return a channel object for the given key.
187 * RCU read side lock MUST be acquired before calling this function and
188 * protects the channel ptr.
190 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
192 struct lttng_ht_iter iter
;
193 struct lttng_ht_node_u64
*node
;
194 struct lttng_consumer_channel
*channel
= NULL
;
196 /* -1ULL keys are lookup failures */
197 if (key
== (uint64_t) -1ULL) {
201 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
202 node
= lttng_ht_iter_get_node_u64(&iter
);
204 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
210 static void free_stream_rcu(struct rcu_head
*head
)
212 struct lttng_ht_node_u64
*node
=
213 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
214 struct lttng_consumer_stream
*stream
=
215 caa_container_of(node
, struct lttng_consumer_stream
, node
);
220 static void free_channel_rcu(struct rcu_head
*head
)
222 struct lttng_ht_node_u64
*node
=
223 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
224 struct lttng_consumer_channel
*channel
=
225 caa_container_of(node
, struct lttng_consumer_channel
, node
);
231 * RCU protected relayd socket pair free.
233 static void free_relayd_rcu(struct rcu_head
*head
)
235 struct lttng_ht_node_u64
*node
=
236 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
237 struct consumer_relayd_sock_pair
*relayd
=
238 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
241 * Close all sockets. This is done in the call RCU since we don't want the
242 * socket fds to be reassigned thus potentially creating bad state of the
245 * We do not have to lock the control socket mutex here since at this stage
246 * there is no one referencing to this relayd object.
248 (void) relayd_close(&relayd
->control_sock
);
249 (void) relayd_close(&relayd
->data_sock
);
255 * Destroy and free relayd socket pair object.
257 * This function MUST be called with the consumer_data lock acquired.
259 static void destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
262 struct lttng_ht_iter iter
;
264 if (relayd
== NULL
) {
268 DBG("Consumer destroy and close relayd socket pair");
270 iter
.iter
.node
= &relayd
->node
.node
;
271 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
273 /* We assume the relayd is being or is destroyed */
277 /* RCU free() call */
278 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
282 * Remove a channel from the global list protected by a mutex. This function is
283 * also responsible for freeing its data structures.
285 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
288 struct lttng_ht_iter iter
;
290 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
292 pthread_mutex_lock(&consumer_data
.lock
);
294 switch (consumer_data
.type
) {
295 case LTTNG_CONSUMER_KERNEL
:
297 case LTTNG_CONSUMER32_UST
:
298 case LTTNG_CONSUMER64_UST
:
299 lttng_ustconsumer_del_channel(channel
);
302 ERR("Unknown consumer_data type");
308 iter
.iter
.node
= &channel
->node
.node
;
309 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
313 call_rcu(&channel
->node
.head
, free_channel_rcu
);
315 pthread_mutex_unlock(&consumer_data
.lock
);
319 * Iterate over the relayd hash table and destroy each element. Finally,
320 * destroy the whole hash table.
322 static void cleanup_relayd_ht(void)
324 struct lttng_ht_iter iter
;
325 struct consumer_relayd_sock_pair
*relayd
;
329 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
331 destroy_relayd(relayd
);
336 lttng_ht_destroy(consumer_data
.relayd_ht
);
340 * Update the end point status of all streams having the given network sequence
341 * index (relayd index).
343 * It's atomically set without having the stream mutex locked which is fine
344 * because we handle the write/read race with a pipe wakeup for each thread.
346 static void update_endpoint_status_by_netidx(int net_seq_idx
,
347 enum consumer_endpoint_status status
)
349 struct lttng_ht_iter iter
;
350 struct lttng_consumer_stream
*stream
;
352 DBG("Consumer set delete flag on stream by idx %d", net_seq_idx
);
356 /* Let's begin with metadata */
357 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
358 if (stream
->net_seq_idx
== net_seq_idx
) {
359 uatomic_set(&stream
->endpoint_status
, status
);
360 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
364 /* Follow up by the data streams */
365 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
366 if (stream
->net_seq_idx
== net_seq_idx
) {
367 uatomic_set(&stream
->endpoint_status
, status
);
368 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
375 * Cleanup a relayd object by flagging every associated streams for deletion,
376 * destroying the object meaning removing it from the relayd hash table,
377 * closing the sockets and freeing the memory in a RCU call.
379 * If a local data context is available, notify the threads that the streams'
380 * state have changed.
382 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
383 struct lttng_consumer_local_data
*ctx
)
389 DBG("Cleaning up relayd sockets");
391 /* Save the net sequence index before destroying the object */
392 netidx
= relayd
->net_seq_idx
;
395 * Delete the relayd from the relayd hash table, close the sockets and free
396 * the object in a RCU call.
398 destroy_relayd(relayd
);
400 /* Set inactive endpoint to all streams */
401 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
404 * With a local data context, notify the threads that the streams' state
405 * have changed. The write() action on the pipe acts as an "implicit"
406 * memory barrier ordering the updates of the end point status from the
407 * read of this status which happens AFTER receiving this notify.
410 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
411 notify_thread_lttng_pipe(ctx
->consumer_metadata_pipe
);
416 * Flag a relayd socket pair for destruction. Destroy it if the refcount
419 * RCU read side lock MUST be aquired before calling this function.
421 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
425 /* Set destroy flag for this object */
426 uatomic_set(&relayd
->destroy_flag
, 1);
428 /* Destroy the relayd if refcount is 0 */
429 if (uatomic_read(&relayd
->refcount
) == 0) {
430 destroy_relayd(relayd
);
435 * Remove a stream from the global list protected by a mutex. This
436 * function is also responsible for freeing its data structures.
438 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
442 struct lttng_ht_iter iter
;
443 struct lttng_consumer_channel
*free_chan
= NULL
;
444 struct consumer_relayd_sock_pair
*relayd
;
448 DBG("Consumer del stream %d", stream
->wait_fd
);
451 /* Means the stream was allocated but not successfully added */
452 goto free_stream_rcu
;
455 pthread_mutex_lock(&consumer_data
.lock
);
456 pthread_mutex_lock(&stream
->lock
);
458 switch (consumer_data
.type
) {
459 case LTTNG_CONSUMER_KERNEL
:
460 if (stream
->mmap_base
!= NULL
) {
461 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
467 if (stream
->wait_fd
>= 0) {
468 ret
= close(stream
->wait_fd
);
474 case LTTNG_CONSUMER32_UST
:
475 case LTTNG_CONSUMER64_UST
:
476 lttng_ustconsumer_del_stream(stream
);
479 ERR("Unknown consumer_data type");
485 iter
.iter
.node
= &stream
->node
.node
;
486 ret
= lttng_ht_del(ht
, &iter
);
489 iter
.iter
.node
= &stream
->node_channel_id
.node
;
490 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
493 iter
.iter
.node
= &stream
->node_session_id
.node
;
494 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
498 assert(consumer_data
.stream_count
> 0);
499 consumer_data
.stream_count
--;
501 if (stream
->out_fd
>= 0) {
502 ret
= close(stream
->out_fd
);
508 /* Check and cleanup relayd */
510 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
511 if (relayd
!= NULL
) {
512 uatomic_dec(&relayd
->refcount
);
513 assert(uatomic_read(&relayd
->refcount
) >= 0);
515 /* Closing streams requires to lock the control socket. */
516 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
517 ret
= relayd_send_close_stream(&relayd
->control_sock
,
518 stream
->relayd_stream_id
,
519 stream
->next_net_seq_num
- 1);
520 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
522 DBG("Unable to close stream on the relayd. Continuing");
524 * Continue here. There is nothing we can do for the relayd.
525 * Chances are that the relayd has closed the socket so we just
526 * continue cleaning up.
530 /* Both conditions are met, we destroy the relayd. */
531 if (uatomic_read(&relayd
->refcount
) == 0 &&
532 uatomic_read(&relayd
->destroy_flag
)) {
533 destroy_relayd(relayd
);
538 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
539 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
540 free_chan
= stream
->chan
;
544 consumer_data
.need_update
= 1;
545 pthread_mutex_unlock(&stream
->lock
);
546 pthread_mutex_unlock(&consumer_data
.lock
);
549 consumer_del_channel(free_chan
);
553 call_rcu(&stream
->node
.head
, free_stream_rcu
);
556 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
558 enum lttng_consumer_stream_state state
,
559 const char *channel_name
,
566 enum consumer_channel_type type
)
569 struct lttng_consumer_stream
*stream
;
571 stream
= zmalloc(sizeof(*stream
));
572 if (stream
== NULL
) {
573 PERROR("malloc struct lttng_consumer_stream");
580 stream
->key
= stream_key
;
582 stream
->out_fd_offset
= 0;
583 stream
->state
= state
;
586 stream
->net_seq_idx
= relayd_id
;
587 stream
->session_id
= session_id
;
588 pthread_mutex_init(&stream
->lock
, NULL
);
590 /* If channel is the metadata, flag this stream as metadata. */
591 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
592 stream
->metadata_flag
= 1;
593 /* Metadata is flat out. */
594 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
596 /* Format stream name to <channel_name>_<cpu_number> */
597 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
600 PERROR("snprintf stream name");
605 /* Key is always the wait_fd for streams. */
606 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
608 /* Init node per channel id key */
609 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
611 /* Init session id node with the stream session id */
612 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
614 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
" relayd_id %" PRIu64
", session_id %" PRIu64
,
615 stream
->name
, stream
->key
, channel_key
, stream
->net_seq_idx
, stream
->session_id
);
631 * Add a stream to the global list protected by a mutex.
633 static int add_stream(struct lttng_consumer_stream
*stream
,
637 struct consumer_relayd_sock_pair
*relayd
;
642 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
644 pthread_mutex_lock(&consumer_data
.lock
);
645 pthread_mutex_lock(&stream
->lock
);
648 /* Steal stream identifier to avoid having streams with the same key */
649 steal_stream_key(stream
->key
, ht
);
651 lttng_ht_add_unique_u64(ht
, &stream
->node
);
653 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
654 &stream
->node_channel_id
);
657 * Add stream to the stream_list_ht of the consumer data. No need to steal
658 * the key since the HT does not use it and we allow to add redundant keys
661 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
663 /* Check and cleanup relayd */
664 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
665 if (relayd
!= NULL
) {
666 uatomic_inc(&relayd
->refcount
);
670 * When nb_init_stream_left reaches 0, we don't need to trigger any action
671 * in terms of destroying the associated channel, because the action that
672 * causes the count to become 0 also causes a stream to be added. The
673 * channel deletion will thus be triggered by the following removal of this
676 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
677 /* Increment refcount before decrementing nb_init_stream_left */
679 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
682 /* Update consumer data once the node is inserted. */
683 consumer_data
.stream_count
++;
684 consumer_data
.need_update
= 1;
687 pthread_mutex_unlock(&stream
->lock
);
688 pthread_mutex_unlock(&consumer_data
.lock
);
694 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
695 * be acquired before calling this.
697 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
700 struct lttng_ht_node_u64
*node
;
701 struct lttng_ht_iter iter
;
705 lttng_ht_lookup(consumer_data
.relayd_ht
,
706 &relayd
->net_seq_idx
, &iter
);
707 node
= lttng_ht_iter_get_node_u64(&iter
);
711 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
718 * Allocate and return a consumer relayd socket.
720 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
723 struct consumer_relayd_sock_pair
*obj
= NULL
;
725 /* Negative net sequence index is a failure */
726 if (net_seq_idx
< 0) {
730 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
732 PERROR("zmalloc relayd sock");
736 obj
->net_seq_idx
= net_seq_idx
;
738 obj
->destroy_flag
= 0;
739 obj
->control_sock
.sock
.fd
= -1;
740 obj
->data_sock
.sock
.fd
= -1;
741 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
742 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
749 * Find a relayd socket pair in the global consumer data.
751 * Return the object if found else NULL.
752 * RCU read-side lock must be held across this call and while using the
755 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
757 struct lttng_ht_iter iter
;
758 struct lttng_ht_node_u64
*node
;
759 struct consumer_relayd_sock_pair
*relayd
= NULL
;
761 /* Negative keys are lookup failures */
762 if (key
== (uint64_t) -1ULL) {
766 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
768 node
= lttng_ht_iter_get_node_u64(&iter
);
770 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
778 * Handle stream for relayd transmission if the stream applies for network
779 * streaming where the net sequence index is set.
781 * Return destination file descriptor or negative value on error.
783 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
784 size_t data_size
, unsigned long padding
,
785 struct consumer_relayd_sock_pair
*relayd
)
788 struct lttcomm_relayd_data_hdr data_hdr
;
794 /* Reset data header */
795 memset(&data_hdr
, 0, sizeof(data_hdr
));
797 if (stream
->metadata_flag
) {
798 /* Caller MUST acquire the relayd control socket lock */
799 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
804 /* Metadata are always sent on the control socket. */
805 outfd
= relayd
->control_sock
.sock
.fd
;
807 /* Set header with stream information */
808 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
809 data_hdr
.data_size
= htobe32(data_size
);
810 data_hdr
.padding_size
= htobe32(padding
);
812 * Note that net_seq_num below is assigned with the *current* value of
813 * next_net_seq_num and only after that the next_net_seq_num will be
814 * increment. This is why when issuing a command on the relayd using
815 * this next value, 1 should always be substracted in order to compare
816 * the last seen sequence number on the relayd side to the last sent.
818 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
819 /* Other fields are zeroed previously */
821 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
827 ++stream
->next_net_seq_num
;
829 /* Set to go on data socket */
830 outfd
= relayd
->data_sock
.sock
.fd
;
838 * Allocate and return a new lttng_consumer_channel object using the given key
839 * to initialize the hash table node.
841 * On error, return NULL.
843 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
845 const char *pathname
,
850 enum lttng_event_output output
,
851 uint64_t tracefile_size
,
852 uint64_t tracefile_count
)
854 struct lttng_consumer_channel
*channel
;
856 channel
= zmalloc(sizeof(*channel
));
857 if (channel
== NULL
) {
858 PERROR("malloc struct lttng_consumer_channel");
863 channel
->refcount
= 0;
864 channel
->session_id
= session_id
;
867 channel
->relayd_id
= relayd_id
;
868 channel
->output
= output
;
869 channel
->tracefile_size
= tracefile_size
;
870 channel
->tracefile_count
= tracefile_count
;
872 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
873 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
875 strncpy(channel
->name
, name
, sizeof(channel
->name
));
876 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
878 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
880 channel
->wait_fd
= -1;
882 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
884 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
891 * Add a channel to the global list protected by a mutex.
893 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
894 struct lttng_consumer_local_data
*ctx
)
897 struct lttng_ht_node_u64
*node
;
898 struct lttng_ht_iter iter
;
900 pthread_mutex_lock(&consumer_data
.lock
);
903 lttng_ht_lookup(consumer_data
.channel_ht
, &channel
->key
, &iter
);
904 node
= lttng_ht_iter_get_node_u64(&iter
);
906 /* Channel already exist. Ignore the insertion */
907 ERR("Consumer add channel key %" PRIu64
" already exists!",
913 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
917 pthread_mutex_unlock(&consumer_data
.lock
);
919 if (!ret
&& channel
->wait_fd
!= -1 &&
920 channel
->metadata_stream
== NULL
) {
921 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
927 * Allocate the pollfd structure and the local view of the out fds to avoid
928 * doing a lookup in the linked list and concurrency issues when writing is
929 * needed. Called with consumer_data.lock held.
931 * Returns the number of fds in the structures.
933 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
934 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
938 struct lttng_ht_iter iter
;
939 struct lttng_consumer_stream
*stream
;
944 assert(local_stream
);
946 DBG("Updating poll fd array");
948 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
950 * Only active streams with an active end point can be added to the
951 * poll set and local stream storage of the thread.
953 * There is a potential race here for endpoint_status to be updated
954 * just after the check. However, this is OK since the stream(s) will
955 * be deleted once the thread is notified that the end point state has
956 * changed where this function will be called back again.
958 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
959 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
963 * This clobbers way too much the debug output. Uncomment that if you
964 * need it for debugging purposes.
966 * DBG("Active FD %d", stream->wait_fd);
968 (*pollfd
)[i
].fd
= stream
->wait_fd
;
969 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
970 local_stream
[i
] = stream
;
976 * Insert the consumer_data_pipe at the end of the array and don't
977 * increment i so nb_fd is the number of real FD.
979 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
980 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
985 * Poll on the should_quit pipe and the command socket return -1 on error and
986 * should exit, 0 if data is available on the command socket
988 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
993 num_rdy
= poll(consumer_sockpoll
, 2, -1);
996 * Restart interrupted system call.
998 if (errno
== EINTR
) {
1001 PERROR("Poll error");
1004 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1005 DBG("consumer_should_quit wake up");
1015 * Set the error socket.
1017 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1020 ctx
->consumer_error_socket
= sock
;
1024 * Set the command socket path.
1026 void lttng_consumer_set_command_sock_path(
1027 struct lttng_consumer_local_data
*ctx
, char *sock
)
1029 ctx
->consumer_command_sock_path
= sock
;
1033 * Send return code to the session daemon.
1034 * If the socket is not defined, we return 0, it is not a fatal error
1036 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1038 if (ctx
->consumer_error_socket
> 0) {
1039 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1040 sizeof(enum lttcomm_sessiond_command
));
1047 * Close all the tracefiles and stream fds and MUST be called when all
1048 * instances are destroyed i.e. when all threads were joined and are ended.
1050 void lttng_consumer_cleanup(void)
1052 struct lttng_ht_iter iter
;
1053 struct lttng_consumer_channel
*channel
;
1057 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1059 consumer_del_channel(channel
);
1064 lttng_ht_destroy(consumer_data
.channel_ht
);
1066 cleanup_relayd_ht();
1068 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1071 * This HT contains streams that are freed by either the metadata thread or
1072 * the data thread so we do *nothing* on the hash table and simply destroy
1075 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1079 * Called from signal handler.
1081 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1086 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1087 } while (ret
< 0 && errno
== EINTR
);
1088 if (ret
< 0 || ret
!= 1) {
1089 PERROR("write consumer quit");
1092 DBG("Consumer flag that it should quit");
1095 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1098 int outfd
= stream
->out_fd
;
1101 * This does a blocking write-and-wait on any page that belongs to the
1102 * subbuffer prior to the one we just wrote.
1103 * Don't care about error values, as these are just hints and ways to
1104 * limit the amount of page cache used.
1106 if (orig_offset
< stream
->max_sb_size
) {
1109 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1110 stream
->max_sb_size
,
1111 SYNC_FILE_RANGE_WAIT_BEFORE
1112 | SYNC_FILE_RANGE_WRITE
1113 | SYNC_FILE_RANGE_WAIT_AFTER
);
1115 * Give hints to the kernel about how we access the file:
1116 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1119 * We need to call fadvise again after the file grows because the
1120 * kernel does not seem to apply fadvise to non-existing parts of the
1123 * Call fadvise _after_ having waited for the page writeback to
1124 * complete because the dirty page writeback semantic is not well
1125 * defined. So it can be expected to lead to lower throughput in
1128 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1129 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1133 * Initialise the necessary environnement :
1134 * - create a new context
1135 * - create the poll_pipe
1136 * - create the should_quit pipe (for signal handler)
1137 * - create the thread pipe (for splice)
1139 * Takes a function pointer as argument, this function is called when data is
1140 * available on a buffer. This function is responsible to do the
1141 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1142 * buffer configuration and then kernctl_put_next_subbuf at the end.
1144 * Returns a pointer to the new context or NULL on error.
1146 struct lttng_consumer_local_data
*lttng_consumer_create(
1147 enum lttng_consumer_type type
,
1148 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1149 struct lttng_consumer_local_data
*ctx
),
1150 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1151 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1152 int (*update_stream
)(int stream_key
, uint32_t state
))
1155 struct lttng_consumer_local_data
*ctx
;
1157 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1158 consumer_data
.type
== type
);
1159 consumer_data
.type
= type
;
1161 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1163 PERROR("allocating context");
1167 ctx
->consumer_error_socket
= -1;
1168 ctx
->consumer_metadata_socket
= -1;
1169 /* assign the callbacks */
1170 ctx
->on_buffer_ready
= buffer_ready
;
1171 ctx
->on_recv_channel
= recv_channel
;
1172 ctx
->on_recv_stream
= recv_stream
;
1173 ctx
->on_update_stream
= update_stream
;
1175 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1176 if (!ctx
->consumer_data_pipe
) {
1177 goto error_poll_pipe
;
1180 ret
= pipe(ctx
->consumer_should_quit
);
1182 PERROR("Error creating recv pipe");
1183 goto error_quit_pipe
;
1186 ret
= pipe(ctx
->consumer_thread_pipe
);
1188 PERROR("Error creating thread pipe");
1189 goto error_thread_pipe
;
1192 ret
= pipe(ctx
->consumer_channel_pipe
);
1194 PERROR("Error creating channel pipe");
1195 goto error_channel_pipe
;
1198 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1199 if (!ctx
->consumer_metadata_pipe
) {
1200 goto error_metadata_pipe
;
1203 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1205 goto error_splice_pipe
;
1211 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1212 error_metadata_pipe
:
1213 utils_close_pipe(ctx
->consumer_channel_pipe
);
1215 utils_close_pipe(ctx
->consumer_thread_pipe
);
1217 utils_close_pipe(ctx
->consumer_should_quit
);
1219 lttng_pipe_destroy(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 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1246 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1247 utils_close_pipe(ctx
->consumer_should_quit
);
1248 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1250 unlink(ctx
->consumer_command_sock_path
);
1255 * Write the metadata stream id on the specified file descriptor.
1257 static int write_relayd_metadata_id(int fd
,
1258 struct lttng_consumer_stream
*stream
,
1259 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1262 struct lttcomm_relayd_metadata_payload hdr
;
1264 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1265 hdr
.padding_size
= htobe32(padding
);
1267 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1268 } while (ret
< 0 && errno
== EINTR
);
1269 if (ret
< 0 || ret
!= sizeof(hdr
)) {
1271 * This error means that the fd's end is closed so ignore the perror
1272 * not to clubber the error output since this can happen in a normal
1275 if (errno
!= EPIPE
) {
1276 PERROR("write metadata stream id");
1278 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1280 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1281 * handle writting the missing part so report that as an error and
1282 * don't lie to the caller.
1287 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1288 stream
->relayd_stream_id
, padding
);
1295 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1296 * core function for writing trace buffers to either the local filesystem or
1299 * It must be called with the stream lock held.
1301 * Careful review MUST be put if any changes occur!
1303 * Returns the number of bytes written
1305 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1306 struct lttng_consumer_local_data
*ctx
,
1307 struct lttng_consumer_stream
*stream
, unsigned long len
,
1308 unsigned long padding
)
1310 unsigned long mmap_offset
;
1312 ssize_t ret
= 0, written
= 0;
1313 off_t orig_offset
= stream
->out_fd_offset
;
1314 /* Default is on the disk */
1315 int outfd
= stream
->out_fd
;
1316 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1317 unsigned int relayd_hang_up
= 0;
1319 /* RCU lock for the relayd pointer */
1322 /* Flag that the current stream if set for network streaming. */
1323 if (stream
->net_seq_idx
!= -1) {
1324 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1325 if (relayd
== NULL
) {
1330 /* get the offset inside the fd to mmap */
1331 switch (consumer_data
.type
) {
1332 case LTTNG_CONSUMER_KERNEL
:
1333 mmap_base
= stream
->mmap_base
;
1334 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1336 case LTTNG_CONSUMER32_UST
:
1337 case LTTNG_CONSUMER64_UST
:
1338 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1340 ERR("read mmap get mmap base for stream %s", stream
->name
);
1344 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1348 ERR("Unknown consumer_data type");
1353 PERROR("tracer ctl get_mmap_read_offset");
1358 /* Handle stream on the relayd if the output is on the network */
1360 unsigned long netlen
= len
;
1363 * Lock the control socket for the complete duration of the function
1364 * since from this point on we will use the socket.
1366 if (stream
->metadata_flag
) {
1367 /* Metadata requires the control socket. */
1368 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1369 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1372 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1374 /* Use the returned socket. */
1377 /* Write metadata stream id before payload */
1378 if (stream
->metadata_flag
) {
1379 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1382 /* Socket operation failed. We consider the relayd dead */
1383 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1391 /* Socket operation failed. We consider the relayd dead */
1392 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1396 /* Else, use the default set before which is the filesystem. */
1399 /* No streaming, we have to set the len with the full padding */
1403 * Check if we need to change the tracefile before writing the packet.
1405 if (stream
->chan
->tracefile_size
> 0 &&
1406 (stream
->tracefile_size_current
+ len
) >
1407 stream
->chan
->tracefile_size
) {
1408 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1409 stream
->name
, stream
->chan
->tracefile_size
,
1410 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1411 stream
->out_fd
, &(stream
->tracefile_count_current
));
1413 ERR("Rotating output file");
1416 outfd
= stream
->out_fd
= ret
;
1417 /* Reset current size because we just perform a rotation. */
1418 stream
->tracefile_size_current
= 0;
1420 stream
->tracefile_size_current
+= len
;
1425 ret
= write(outfd
, mmap_base
+ mmap_offset
, len
);
1426 } while (ret
< 0 && errno
== EINTR
);
1427 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1430 * This is possible if the fd is closed on the other side (outfd)
1431 * or any write problem. It can be verbose a bit for a normal
1432 * execution if for instance the relayd is stopped abruptly. This
1433 * can happen so set this to a DBG statement.
1435 DBG("Error in file write mmap");
1439 /* Socket operation failed. We consider the relayd dead */
1440 if (errno
== EPIPE
|| errno
== EINVAL
) {
1445 } else if (ret
> len
) {
1446 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1454 /* This call is useless on a socket so better save a syscall. */
1456 /* This won't block, but will start writeout asynchronously */
1457 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1458 SYNC_FILE_RANGE_WRITE
);
1459 stream
->out_fd_offset
+= ret
;
1463 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1467 * This is a special case that the relayd has closed its socket. Let's
1468 * cleanup the relayd object and all associated streams.
1470 if (relayd
&& relayd_hang_up
) {
1471 cleanup_relayd(relayd
, ctx
);
1475 /* Unlock only if ctrl socket used */
1476 if (relayd
&& stream
->metadata_flag
) {
1477 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1485 * Splice the data from the ring buffer to the tracefile.
1487 * It must be called with the stream lock held.
1489 * Returns the number of bytes spliced.
1491 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1492 struct lttng_consumer_local_data
*ctx
,
1493 struct lttng_consumer_stream
*stream
, unsigned long len
,
1494 unsigned long padding
)
1496 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1498 off_t orig_offset
= stream
->out_fd_offset
;
1499 int fd
= stream
->wait_fd
;
1500 /* Default is on the disk */
1501 int outfd
= stream
->out_fd
;
1502 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1504 unsigned int relayd_hang_up
= 0;
1506 switch (consumer_data
.type
) {
1507 case LTTNG_CONSUMER_KERNEL
:
1509 case LTTNG_CONSUMER32_UST
:
1510 case LTTNG_CONSUMER64_UST
:
1511 /* Not supported for user space tracing */
1514 ERR("Unknown consumer_data type");
1518 /* RCU lock for the relayd pointer */
1521 /* Flag that the current stream if set for network streaming. */
1522 if (stream
->net_seq_idx
!= -1) {
1523 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1524 if (relayd
== NULL
) {
1530 * Choose right pipe for splice. Metadata and trace data are handled by
1531 * different threads hence the use of two pipes in order not to race or
1532 * corrupt the written data.
1534 if (stream
->metadata_flag
) {
1535 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1537 splice_pipe
= ctx
->consumer_thread_pipe
;
1540 /* Write metadata stream id before payload */
1542 int total_len
= len
;
1544 if (stream
->metadata_flag
) {
1546 * Lock the control socket for the complete duration of the function
1547 * since from this point on we will use the socket.
1549 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1551 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1555 /* Socket operation failed. We consider the relayd dead */
1556 if (ret
== -EBADF
) {
1557 WARN("Remote relayd disconnected. Stopping");
1564 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1567 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1569 /* Use the returned socket. */
1572 /* Socket operation failed. We consider the relayd dead */
1573 if (ret
== -EBADF
) {
1574 WARN("Remote relayd disconnected. Stopping");
1581 /* No streaming, we have to set the len with the full padding */
1585 * Check if we need to change the tracefile before writing the packet.
1587 if (stream
->chan
->tracefile_size
> 0 &&
1588 (stream
->tracefile_size_current
+ len
) >
1589 stream
->chan
->tracefile_size
) {
1590 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1591 stream
->name
, stream
->chan
->tracefile_size
,
1592 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1593 stream
->out_fd
, &(stream
->tracefile_count_current
));
1595 ERR("Rotating output file");
1598 outfd
= stream
->out_fd
= ret
;
1599 /* Reset current size because we just perform a rotation. */
1600 stream
->tracefile_size_current
= 0;
1602 stream
->tracefile_size_current
+= len
;
1606 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1607 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1608 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1609 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1610 DBG("splice chan to pipe, ret %zd", ret_splice
);
1611 if (ret_splice
< 0) {
1612 PERROR("Error in relay splice");
1614 written
= ret_splice
;
1620 /* Handle stream on the relayd if the output is on the network */
1622 if (stream
->metadata_flag
) {
1623 size_t metadata_payload_size
=
1624 sizeof(struct lttcomm_relayd_metadata_payload
);
1626 /* Update counter to fit the spliced data */
1627 ret_splice
+= metadata_payload_size
;
1628 len
+= metadata_payload_size
;
1630 * We do this so the return value can match the len passed as
1631 * argument to this function.
1633 written
-= metadata_payload_size
;
1637 /* Splice data out */
1638 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1639 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1640 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1641 if (ret_splice
< 0) {
1642 PERROR("Error in file splice");
1644 written
= ret_splice
;
1646 /* Socket operation failed. We consider the relayd dead */
1647 if (errno
== EBADF
|| errno
== EPIPE
) {
1648 WARN("Remote relayd disconnected. Stopping");
1654 } else if (ret_splice
> len
) {
1656 PERROR("Wrote more data than requested %zd (len: %lu)",
1658 written
+= ret_splice
;
1664 /* This call is useless on a socket so better save a syscall. */
1666 /* This won't block, but will start writeout asynchronously */
1667 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1668 SYNC_FILE_RANGE_WRITE
);
1669 stream
->out_fd_offset
+= ret_splice
;
1671 written
+= ret_splice
;
1673 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1681 * This is a special case that the relayd has closed its socket. Let's
1682 * cleanup the relayd object and all associated streams.
1684 if (relayd
&& relayd_hang_up
) {
1685 cleanup_relayd(relayd
, ctx
);
1686 /* Skip splice error so the consumer does not fail */
1691 /* send the appropriate error description to sessiond */
1694 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1697 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1700 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1705 if (relayd
&& stream
->metadata_flag
) {
1706 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1714 * Take a snapshot for a specific fd
1716 * Returns 0 on success, < 0 on error
1718 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1720 switch (consumer_data
.type
) {
1721 case LTTNG_CONSUMER_KERNEL
:
1722 return lttng_kconsumer_take_snapshot(stream
);
1723 case LTTNG_CONSUMER32_UST
:
1724 case LTTNG_CONSUMER64_UST
:
1725 return lttng_ustconsumer_take_snapshot(stream
);
1727 ERR("Unknown consumer_data type");
1734 * Get the produced position
1736 * Returns 0 on success, < 0 on error
1738 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1741 switch (consumer_data
.type
) {
1742 case LTTNG_CONSUMER_KERNEL
:
1743 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1744 case LTTNG_CONSUMER32_UST
:
1745 case LTTNG_CONSUMER64_UST
:
1746 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1748 ERR("Unknown consumer_data type");
1754 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1755 int sock
, struct pollfd
*consumer_sockpoll
)
1757 switch (consumer_data
.type
) {
1758 case LTTNG_CONSUMER_KERNEL
:
1759 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1760 case LTTNG_CONSUMER32_UST
:
1761 case LTTNG_CONSUMER64_UST
:
1762 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1764 ERR("Unknown consumer_data type");
1771 * Iterate over all streams of the hashtable and free them properly.
1773 * WARNING: *MUST* be used with data stream only.
1775 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1777 struct lttng_ht_iter iter
;
1778 struct lttng_consumer_stream
*stream
;
1785 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1787 * Ignore return value since we are currently cleaning up so any error
1790 (void) consumer_del_stream(stream
, ht
);
1794 lttng_ht_destroy(ht
);
1798 * Iterate over all streams of the hashtable and free them properly.
1800 * XXX: Should not be only for metadata stream or else use an other name.
1802 static void destroy_stream_ht(struct lttng_ht
*ht
)
1804 struct lttng_ht_iter iter
;
1805 struct lttng_consumer_stream
*stream
;
1812 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1814 * Ignore return value since we are currently cleaning up so any error
1817 (void) consumer_del_metadata_stream(stream
, ht
);
1821 lttng_ht_destroy(ht
);
1824 void lttng_consumer_close_metadata(void)
1826 switch (consumer_data
.type
) {
1827 case LTTNG_CONSUMER_KERNEL
:
1829 * The Kernel consumer has a different metadata scheme so we don't
1830 * close anything because the stream will be closed by the session
1834 case LTTNG_CONSUMER32_UST
:
1835 case LTTNG_CONSUMER64_UST
:
1837 * Close all metadata streams. The metadata hash table is passed and
1838 * this call iterates over it by closing all wakeup fd. This is safe
1839 * because at this point we are sure that the metadata producer is
1840 * either dead or blocked.
1842 lttng_ustconsumer_close_metadata(metadata_ht
);
1845 ERR("Unknown consumer_data type");
1851 * Clean up a metadata stream and free its memory.
1853 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1854 struct lttng_ht
*ht
)
1857 struct lttng_ht_iter iter
;
1858 struct lttng_consumer_channel
*free_chan
= NULL
;
1859 struct consumer_relayd_sock_pair
*relayd
;
1863 * This call should NEVER receive regular stream. It must always be
1864 * metadata stream and this is crucial for data structure synchronization.
1866 assert(stream
->metadata_flag
);
1868 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1871 /* Means the stream was allocated but not successfully added */
1872 goto free_stream_rcu
;
1875 pthread_mutex_lock(&consumer_data
.lock
);
1876 pthread_mutex_lock(&stream
->lock
);
1878 switch (consumer_data
.type
) {
1879 case LTTNG_CONSUMER_KERNEL
:
1880 if (stream
->mmap_base
!= NULL
) {
1881 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1883 PERROR("munmap metadata stream");
1887 if (stream
->wait_fd
>= 0) {
1888 ret
= close(stream
->wait_fd
);
1890 PERROR("close kernel metadata wait_fd");
1894 case LTTNG_CONSUMER32_UST
:
1895 case LTTNG_CONSUMER64_UST
:
1896 lttng_ustconsumer_del_stream(stream
);
1899 ERR("Unknown consumer_data type");
1905 iter
.iter
.node
= &stream
->node
.node
;
1906 ret
= lttng_ht_del(ht
, &iter
);
1909 iter
.iter
.node
= &stream
->node_channel_id
.node
;
1910 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
1913 iter
.iter
.node
= &stream
->node_session_id
.node
;
1914 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1918 if (stream
->out_fd
>= 0) {
1919 ret
= close(stream
->out_fd
);
1925 /* Check and cleanup relayd */
1927 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1928 if (relayd
!= NULL
) {
1929 uatomic_dec(&relayd
->refcount
);
1930 assert(uatomic_read(&relayd
->refcount
) >= 0);
1932 /* Closing streams requires to lock the control socket. */
1933 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1934 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1935 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1936 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1938 DBG("Unable to close stream on the relayd. Continuing");
1940 * Continue here. There is nothing we can do for the relayd.
1941 * Chances are that the relayd has closed the socket so we just
1942 * continue cleaning up.
1946 /* Both conditions are met, we destroy the relayd. */
1947 if (uatomic_read(&relayd
->refcount
) == 0 &&
1948 uatomic_read(&relayd
->destroy_flag
)) {
1949 destroy_relayd(relayd
);
1954 /* Atomically decrement channel refcount since other threads can use it. */
1955 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
1956 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
1957 /* Go for channel deletion! */
1958 free_chan
= stream
->chan
;
1962 pthread_mutex_unlock(&stream
->lock
);
1963 pthread_mutex_unlock(&consumer_data
.lock
);
1966 consumer_del_channel(free_chan
);
1970 call_rcu(&stream
->node
.head
, free_stream_rcu
);
1974 * Action done with the metadata stream when adding it to the consumer internal
1975 * data structures to handle it.
1977 static int add_metadata_stream(struct lttng_consumer_stream
*stream
,
1978 struct lttng_ht
*ht
)
1981 struct consumer_relayd_sock_pair
*relayd
;
1982 struct lttng_ht_iter iter
;
1983 struct lttng_ht_node_u64
*node
;
1988 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
1990 pthread_mutex_lock(&consumer_data
.lock
);
1991 pthread_mutex_lock(&stream
->lock
);
1994 * From here, refcounts are updated so be _careful_ when returning an error
2001 * Lookup the stream just to make sure it does not exist in our internal
2002 * state. This should NEVER happen.
2004 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2005 node
= lttng_ht_iter_get_node_u64(&iter
);
2008 /* Find relayd and, if one is found, increment refcount. */
2009 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
2010 if (relayd
!= NULL
) {
2011 uatomic_inc(&relayd
->refcount
);
2014 /* Update channel refcount once added without error(s). */
2015 uatomic_inc(&stream
->chan
->refcount
);
2018 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2019 * in terms of destroying the associated channel, because the action that
2020 * causes the count to become 0 also causes a stream to be added. The
2021 * channel deletion will thus be triggered by the following removal of this
2024 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2025 /* Increment refcount before decrementing nb_init_stream_left */
2027 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2030 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2032 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2033 &stream
->node_channel_id
);
2036 * Add stream to the stream_list_ht of the consumer data. No need to steal
2037 * the key since the HT does not use it and we allow to add redundant keys
2040 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2044 pthread_mutex_unlock(&stream
->lock
);
2045 pthread_mutex_unlock(&consumer_data
.lock
);
2050 * Delete data stream that are flagged for deletion (endpoint_status).
2052 static void validate_endpoint_status_data_stream(void)
2054 struct lttng_ht_iter iter
;
2055 struct lttng_consumer_stream
*stream
;
2057 DBG("Consumer delete flagged data stream");
2060 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2061 /* Validate delete flag of the stream */
2062 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2065 /* Delete it right now */
2066 consumer_del_stream(stream
, data_ht
);
2072 * Delete metadata stream that are flagged for deletion (endpoint_status).
2074 static void validate_endpoint_status_metadata_stream(
2075 struct lttng_poll_event
*pollset
)
2077 struct lttng_ht_iter iter
;
2078 struct lttng_consumer_stream
*stream
;
2080 DBG("Consumer delete flagged metadata stream");
2085 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2086 /* Validate delete flag of the stream */
2087 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2091 * Remove from pollset so the metadata thread can continue without
2092 * blocking on a deleted stream.
2094 lttng_poll_del(pollset
, stream
->wait_fd
);
2096 /* Delete it right now */
2097 consumer_del_metadata_stream(stream
, metadata_ht
);
2103 * Thread polls on metadata file descriptor and write them on disk or on the
2106 void *consumer_thread_metadata_poll(void *data
)
2109 uint32_t revents
, nb_fd
;
2110 struct lttng_consumer_stream
*stream
= NULL
;
2111 struct lttng_ht_iter iter
;
2112 struct lttng_ht_node_u64
*node
;
2113 struct lttng_poll_event events
;
2114 struct lttng_consumer_local_data
*ctx
= data
;
2117 rcu_register_thread();
2119 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2121 /* ENOMEM at this point. Better to bail out. */
2125 DBG("Thread metadata poll started");
2127 /* Size is set to 1 for the consumer_metadata pipe */
2128 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2130 ERR("Poll set creation failed");
2134 ret
= lttng_poll_add(&events
,
2135 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2141 DBG("Metadata main loop started");
2144 /* Only the metadata pipe is set */
2145 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2150 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2151 ret
= lttng_poll_wait(&events
, -1);
2152 DBG("Metadata event catched in thread");
2154 if (errno
== EINTR
) {
2155 ERR("Poll EINTR catched");
2163 /* From here, the event is a metadata wait fd */
2164 for (i
= 0; i
< nb_fd
; i
++) {
2165 revents
= LTTNG_POLL_GETEV(&events
, i
);
2166 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2168 /* Just don't waste time if no returned events for the fd */
2173 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2174 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2175 DBG("Metadata thread pipe hung up");
2177 * Remove the pipe from the poll set and continue the loop
2178 * since their might be data to consume.
2180 lttng_poll_del(&events
,
2181 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2182 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2184 } else if (revents
& LPOLLIN
) {
2187 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2188 &stream
, sizeof(stream
));
2190 ERR("read metadata stream, ret: %ld", pipe_len
);
2192 * Continue here to handle the rest of the streams.
2197 /* A NULL stream means that the state has changed. */
2198 if (stream
== NULL
) {
2199 /* Check for deleted streams. */
2200 validate_endpoint_status_metadata_stream(&events
);
2204 DBG("Adding metadata stream %d to poll set",
2207 ret
= add_metadata_stream(stream
, metadata_ht
);
2209 ERR("Unable to add metadata stream");
2210 /* Stream was not setup properly. Continuing. */
2211 consumer_del_metadata_stream(stream
, NULL
);
2215 /* Add metadata stream to the global poll events list */
2216 lttng_poll_add(&events
, stream
->wait_fd
,
2217 LPOLLIN
| LPOLLPRI
);
2220 /* Handle other stream */
2226 uint64_t tmp_id
= (uint64_t) pollfd
;
2228 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2230 node
= lttng_ht_iter_get_node_u64(&iter
);
2233 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2236 /* Check for error event */
2237 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2238 DBG("Metadata fd %d is hup|err.", pollfd
);
2239 if (!stream
->hangup_flush_done
2240 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2241 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2242 DBG("Attempting to flush and consume the UST buffers");
2243 lttng_ustconsumer_on_stream_hangup(stream
);
2245 /* We just flushed the stream now read it. */
2247 len
= ctx
->on_buffer_ready(stream
, ctx
);
2249 * We don't check the return value here since if we get
2250 * a negative len, it means an error occured thus we
2251 * simply remove it from the poll set and free the
2257 lttng_poll_del(&events
, stream
->wait_fd
);
2259 * This call update the channel states, closes file descriptors
2260 * and securely free the stream.
2262 consumer_del_metadata_stream(stream
, metadata_ht
);
2263 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2264 /* Get the data out of the metadata file descriptor */
2265 DBG("Metadata available on fd %d", pollfd
);
2266 assert(stream
->wait_fd
== pollfd
);
2268 len
= ctx
->on_buffer_ready(stream
, ctx
);
2269 /* It's ok to have an unavailable sub-buffer */
2270 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2271 /* Clean up stream from consumer and free it. */
2272 lttng_poll_del(&events
, stream
->wait_fd
);
2273 consumer_del_metadata_stream(stream
, metadata_ht
);
2274 } else if (len
> 0) {
2275 stream
->data_read
= 1;
2279 /* Release RCU lock for the stream looked up */
2286 DBG("Metadata poll thread exiting");
2288 lttng_poll_clean(&events
);
2290 destroy_stream_ht(metadata_ht
);
2292 rcu_unregister_thread();
2297 * This thread polls the fds in the set to consume the data and write
2298 * it to tracefile if necessary.
2300 void *consumer_thread_data_poll(void *data
)
2302 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2303 struct pollfd
*pollfd
= NULL
;
2304 /* local view of the streams */
2305 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2306 /* local view of consumer_data.fds_count */
2308 struct lttng_consumer_local_data
*ctx
= data
;
2311 rcu_register_thread();
2313 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2314 if (data_ht
== NULL
) {
2315 /* ENOMEM at this point. Better to bail out. */
2319 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
2326 * the fds set has been updated, we need to update our
2327 * local array as well
2329 pthread_mutex_lock(&consumer_data
.lock
);
2330 if (consumer_data
.need_update
) {
2335 local_stream
= NULL
;
2337 /* allocate for all fds + 1 for the consumer_data_pipe */
2338 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2339 if (pollfd
== NULL
) {
2340 PERROR("pollfd malloc");
2341 pthread_mutex_unlock(&consumer_data
.lock
);
2345 /* allocate for all fds + 1 for the consumer_data_pipe */
2346 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2347 sizeof(struct lttng_consumer_stream
));
2348 if (local_stream
== NULL
) {
2349 PERROR("local_stream malloc");
2350 pthread_mutex_unlock(&consumer_data
.lock
);
2353 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2356 ERR("Error in allocating pollfd or local_outfds");
2357 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2358 pthread_mutex_unlock(&consumer_data
.lock
);
2362 consumer_data
.need_update
= 0;
2364 pthread_mutex_unlock(&consumer_data
.lock
);
2366 /* No FDs and consumer_quit, consumer_cleanup the thread */
2367 if (nb_fd
== 0 && consumer_quit
== 1) {
2370 /* poll on the array of fds */
2372 DBG("polling on %d fd", nb_fd
+ 1);
2373 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2374 DBG("poll num_rdy : %d", num_rdy
);
2375 if (num_rdy
== -1) {
2377 * Restart interrupted system call.
2379 if (errno
== EINTR
) {
2382 PERROR("Poll error");
2383 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2385 } else if (num_rdy
== 0) {
2386 DBG("Polling thread timed out");
2391 * If the consumer_data_pipe triggered poll go directly to the
2392 * beginning of the loop to update the array. We want to prioritize
2393 * array update over low-priority reads.
2395 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2396 ssize_t pipe_readlen
;
2398 DBG("consumer_data_pipe wake up");
2399 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2400 &new_stream
, sizeof(new_stream
));
2401 if (pipe_readlen
< 0) {
2402 ERR("Consumer data pipe ret %ld", pipe_readlen
);
2403 /* Continue so we can at least handle the current stream(s). */
2408 * If the stream is NULL, just ignore it. It's also possible that
2409 * the sessiond poll thread changed the consumer_quit state and is
2410 * waking us up to test it.
2412 if (new_stream
== NULL
) {
2413 validate_endpoint_status_data_stream();
2417 ret
= add_stream(new_stream
, data_ht
);
2419 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
2422 * At this point, if the add_stream fails, it is not in the
2423 * hash table thus passing the NULL value here.
2425 consumer_del_stream(new_stream
, NULL
);
2428 /* Continue to update the local streams and handle prio ones */
2432 /* Take care of high priority channels first. */
2433 for (i
= 0; i
< nb_fd
; i
++) {
2434 if (local_stream
[i
] == NULL
) {
2437 if (pollfd
[i
].revents
& POLLPRI
) {
2438 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2440 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2441 /* it's ok to have an unavailable sub-buffer */
2442 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2443 /* Clean the stream and free it. */
2444 consumer_del_stream(local_stream
[i
], data_ht
);
2445 local_stream
[i
] = NULL
;
2446 } else if (len
> 0) {
2447 local_stream
[i
]->data_read
= 1;
2453 * If we read high prio channel in this loop, try again
2454 * for more high prio data.
2460 /* Take care of low priority channels. */
2461 for (i
= 0; i
< nb_fd
; i
++) {
2462 if (local_stream
[i
] == NULL
) {
2465 if ((pollfd
[i
].revents
& POLLIN
) ||
2466 local_stream
[i
]->hangup_flush_done
) {
2467 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2468 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2469 /* it's ok to have an unavailable sub-buffer */
2470 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2471 /* Clean the stream and free it. */
2472 consumer_del_stream(local_stream
[i
], data_ht
);
2473 local_stream
[i
] = NULL
;
2474 } else if (len
> 0) {
2475 local_stream
[i
]->data_read
= 1;
2480 /* Handle hangup and errors */
2481 for (i
= 0; i
< nb_fd
; i
++) {
2482 if (local_stream
[i
] == NULL
) {
2485 if (!local_stream
[i
]->hangup_flush_done
2486 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2487 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2488 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2489 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2491 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2492 /* Attempt read again, for the data we just flushed. */
2493 local_stream
[i
]->data_read
= 1;
2496 * If the poll flag is HUP/ERR/NVAL and we have
2497 * read no data in this pass, we can remove the
2498 * stream from its hash table.
2500 if ((pollfd
[i
].revents
& POLLHUP
)) {
2501 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2502 if (!local_stream
[i
]->data_read
) {
2503 consumer_del_stream(local_stream
[i
], data_ht
);
2504 local_stream
[i
] = NULL
;
2507 } else if (pollfd
[i
].revents
& POLLERR
) {
2508 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2509 if (!local_stream
[i
]->data_read
) {
2510 consumer_del_stream(local_stream
[i
], data_ht
);
2511 local_stream
[i
] = NULL
;
2514 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2515 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2516 if (!local_stream
[i
]->data_read
) {
2517 consumer_del_stream(local_stream
[i
], data_ht
);
2518 local_stream
[i
] = NULL
;
2522 if (local_stream
[i
] != NULL
) {
2523 local_stream
[i
]->data_read
= 0;
2528 DBG("polling thread exiting");
2533 * Close the write side of the pipe so epoll_wait() in
2534 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2535 * read side of the pipe. If we close them both, epoll_wait strangely does
2536 * not return and could create a endless wait period if the pipe is the
2537 * only tracked fd in the poll set. The thread will take care of closing
2540 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2542 destroy_data_stream_ht(data_ht
);
2544 rcu_unregister_thread();
2549 * Close wake-up end of each stream belonging to the channel. This will
2550 * allow the poll() on the stream read-side to detect when the
2551 * write-side (application) finally closes them.
2554 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2556 struct lttng_ht
*ht
;
2557 struct lttng_consumer_stream
*stream
;
2558 struct lttng_ht_iter iter
;
2560 ht
= consumer_data
.stream_per_chan_id_ht
;
2563 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2564 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2565 ht
->match_fct
, &channel
->key
,
2566 &iter
.iter
, stream
, node_channel_id
.node
) {
2568 * Protect against teardown with mutex.
2570 pthread_mutex_lock(&stream
->lock
);
2571 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2574 switch (consumer_data
.type
) {
2575 case LTTNG_CONSUMER_KERNEL
:
2577 case LTTNG_CONSUMER32_UST
:
2578 case LTTNG_CONSUMER64_UST
:
2580 * Note: a mutex is taken internally within
2581 * liblttng-ust-ctl to protect timer wakeup_fd
2582 * use from concurrent close.
2584 lttng_ustconsumer_close_stream_wakeup(stream
);
2587 ERR("Unknown consumer_data type");
2591 pthread_mutex_unlock(&stream
->lock
);
2596 static void destroy_channel_ht(struct lttng_ht
*ht
)
2598 struct lttng_ht_iter iter
;
2599 struct lttng_consumer_channel
*channel
;
2607 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2608 ret
= lttng_ht_del(ht
, &iter
);
2613 lttng_ht_destroy(ht
);
2617 * This thread polls the channel fds to detect when they are being
2618 * closed. It closes all related streams if the channel is detected as
2619 * closed. It is currently only used as a shim layer for UST because the
2620 * consumerd needs to keep the per-stream wakeup end of pipes open for
2623 void *consumer_thread_channel_poll(void *data
)
2626 uint32_t revents
, nb_fd
;
2627 struct lttng_consumer_channel
*chan
= NULL
;
2628 struct lttng_ht_iter iter
;
2629 struct lttng_ht_node_u64
*node
;
2630 struct lttng_poll_event events
;
2631 struct lttng_consumer_local_data
*ctx
= data
;
2632 struct lttng_ht
*channel_ht
;
2634 rcu_register_thread();
2636 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2638 /* ENOMEM at this point. Better to bail out. */
2642 DBG("Thread channel poll started");
2644 /* Size is set to 1 for the consumer_channel pipe */
2645 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2647 ERR("Poll set creation failed");
2651 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2657 DBG("Channel main loop started");
2660 /* Only the channel pipe is set */
2661 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2666 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2667 ret
= lttng_poll_wait(&events
, -1);
2668 DBG("Channel event catched in thread");
2670 if (errno
== EINTR
) {
2671 ERR("Poll EINTR catched");
2679 /* From here, the event is a channel wait fd */
2680 for (i
= 0; i
< nb_fd
; i
++) {
2681 revents
= LTTNG_POLL_GETEV(&events
, i
);
2682 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2684 /* Just don't waste time if no returned events for the fd */
2688 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2689 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2690 DBG("Channel thread pipe hung up");
2692 * Remove the pipe from the poll set and continue the loop
2693 * since their might be data to consume.
2695 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2697 } else if (revents
& LPOLLIN
) {
2698 enum consumer_channel_action action
;
2701 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2703 ERR("Error reading channel pipe");
2708 case CONSUMER_CHANNEL_ADD
:
2709 DBG("Adding channel %d to poll set",
2712 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2714 lttng_ht_add_unique_u64(channel_ht
,
2715 &chan
->wait_fd_node
);
2716 /* Add channel to the global poll events list */
2717 lttng_poll_add(&events
, chan
->wait_fd
,
2718 LPOLLIN
| LPOLLPRI
);
2720 case CONSUMER_CHANNEL_DEL
:
2722 chan
= consumer_find_channel(key
);
2724 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2727 lttng_poll_del(&events
, chan
->wait_fd
);
2728 ret
= lttng_ht_del(channel_ht
, &iter
);
2730 consumer_close_channel_streams(chan
);
2733 * Release our own refcount. Force channel deletion even if
2734 * streams were not initialized.
2736 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2737 consumer_del_channel(chan
);
2741 case CONSUMER_CHANNEL_QUIT
:
2743 * Remove the pipe from the poll set and continue the loop
2744 * since their might be data to consume.
2746 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2749 ERR("Unknown action");
2754 /* Handle other stream */
2760 uint64_t tmp_id
= (uint64_t) pollfd
;
2762 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2764 node
= lttng_ht_iter_get_node_u64(&iter
);
2767 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2770 /* Check for error event */
2771 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2772 DBG("Channel fd %d is hup|err.", pollfd
);
2774 lttng_poll_del(&events
, chan
->wait_fd
);
2775 ret
= lttng_ht_del(channel_ht
, &iter
);
2777 consumer_close_channel_streams(chan
);
2779 /* Release our own refcount */
2780 if (!uatomic_sub_return(&chan
->refcount
, 1)
2781 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2782 consumer_del_channel(chan
);
2786 /* Release RCU lock for the channel looked up */
2792 lttng_poll_clean(&events
);
2794 destroy_channel_ht(channel_ht
);
2796 DBG("Channel poll thread exiting");
2797 rcu_unregister_thread();
2801 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2802 struct pollfd
*sockpoll
, int client_socket
)
2809 if (lttng_consumer_poll_socket(sockpoll
) < 0) {
2813 DBG("Metadata connection on client_socket");
2815 /* Blocking call, waiting for transmission */
2816 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2817 if (ctx
->consumer_metadata_socket
< 0) {
2818 WARN("On accept metadata");
2829 * This thread listens on the consumerd socket and receives the file
2830 * descriptors from the session daemon.
2832 void *consumer_thread_sessiond_poll(void *data
)
2834 int sock
= -1, client_socket
, ret
;
2836 * structure to poll for incoming data on communication socket avoids
2837 * making blocking sockets.
2839 struct pollfd consumer_sockpoll
[2];
2840 struct lttng_consumer_local_data
*ctx
= data
;
2842 rcu_register_thread();
2844 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2845 unlink(ctx
->consumer_command_sock_path
);
2846 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2847 if (client_socket
< 0) {
2848 ERR("Cannot create command socket");
2852 ret
= lttcomm_listen_unix_sock(client_socket
);
2857 DBG("Sending ready command to lttng-sessiond");
2858 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2859 /* return < 0 on error, but == 0 is not fatal */
2861 ERR("Error sending ready command to lttng-sessiond");
2865 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2867 PERROR("fcntl O_NONBLOCK");
2871 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2872 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2873 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2874 consumer_sockpoll
[1].fd
= client_socket
;
2875 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2877 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2880 DBG("Connection on client_socket");
2882 /* Blocking call, waiting for transmission */
2883 sock
= lttcomm_accept_unix_sock(client_socket
);
2888 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2890 PERROR("fcntl O_NONBLOCK");
2895 * Setup metadata socket which is the second socket connection on the
2896 * command unix socket.
2898 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
2903 /* This socket is not useful anymore. */
2904 ret
= close(client_socket
);
2906 PERROR("close client_socket");
2910 /* update the polling structure to poll on the established socket */
2911 consumer_sockpoll
[1].fd
= sock
;
2912 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2915 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2918 DBG("Incoming command on sock");
2919 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2920 if (ret
== -ENOENT
) {
2921 DBG("Received STOP command");
2926 * This could simply be a session daemon quitting. Don't output
2929 DBG("Communication interrupted on command socket");
2932 if (consumer_quit
) {
2933 DBG("consumer_thread_receive_fds received quit from signal");
2936 DBG("received command on sock");
2939 DBG("Consumer thread sessiond poll exiting");
2942 * Close metadata streams since the producer is the session daemon which
2945 * NOTE: for now, this only applies to the UST tracer.
2947 lttng_consumer_close_metadata();
2950 * when all fds have hung up, the polling thread
2956 * Notify the data poll thread to poll back again and test the
2957 * consumer_quit state that we just set so to quit gracefully.
2959 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
2961 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
2963 /* Cleaning up possibly open sockets. */
2967 PERROR("close sock sessiond poll");
2970 if (client_socket
>= 0) {
2973 PERROR("close client_socket sessiond poll");
2977 rcu_unregister_thread();
2981 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2982 struct lttng_consumer_local_data
*ctx
)
2986 pthread_mutex_lock(&stream
->lock
);
2988 switch (consumer_data
.type
) {
2989 case LTTNG_CONSUMER_KERNEL
:
2990 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
2992 case LTTNG_CONSUMER32_UST
:
2993 case LTTNG_CONSUMER64_UST
:
2994 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2997 ERR("Unknown consumer_data type");
3003 pthread_mutex_unlock(&stream
->lock
);
3007 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3009 switch (consumer_data
.type
) {
3010 case LTTNG_CONSUMER_KERNEL
:
3011 return lttng_kconsumer_on_recv_stream(stream
);
3012 case LTTNG_CONSUMER32_UST
:
3013 case LTTNG_CONSUMER64_UST
:
3014 return lttng_ustconsumer_on_recv_stream(stream
);
3016 ERR("Unknown consumer_data type");
3023 * Allocate and set consumer data hash tables.
3025 void lttng_consumer_init(void)
3027 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3028 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3029 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3030 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3034 * Process the ADD_RELAYD command receive by a consumer.
3036 * This will create a relayd socket pair and add it to the relayd hash table.
3037 * The caller MUST acquire a RCU read side lock before calling it.
3039 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
3040 struct lttng_consumer_local_data
*ctx
, int sock
,
3041 struct pollfd
*consumer_sockpoll
,
3042 struct lttcomm_relayd_sock
*relayd_sock
, unsigned int sessiond_id
)
3044 int fd
= -1, ret
= -1, relayd_created
= 0;
3045 enum lttng_error_code ret_code
= LTTNG_OK
;
3046 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3049 assert(relayd_sock
);
3051 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
3053 /* First send a status message before receiving the fds. */
3054 ret
= consumer_send_status_msg(sock
, ret_code
);
3056 /* Somehow, the session daemon is not responding anymore. */
3060 /* Get relayd reference if exists. */
3061 relayd
= consumer_find_relayd(net_seq_idx
);
3062 if (relayd
== NULL
) {
3063 /* Not found. Allocate one. */
3064 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3065 if (relayd
== NULL
) {
3066 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
3070 relayd
->sessiond_session_id
= (uint64_t) sessiond_id
;
3074 /* Poll on consumer socket. */
3075 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
3080 /* Get relayd socket from session daemon */
3081 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3082 if (ret
!= sizeof(fd
)) {
3083 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3085 fd
= -1; /* Just in case it gets set with an invalid value. */
3089 /* We have the fds without error. Send status back. */
3090 ret
= consumer_send_status_msg(sock
, ret_code
);
3092 /* Somehow, the session daemon is not responding anymore. */
3096 /* Copy socket information and received FD */
3097 switch (sock_type
) {
3098 case LTTNG_STREAM_CONTROL
:
3099 /* Copy received lttcomm socket */
3100 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3101 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3102 /* Immediately try to close the created socket if valid. */
3103 if (relayd
->control_sock
.sock
.fd
>= 0) {
3104 if (close(relayd
->control_sock
.sock
.fd
)) {
3105 PERROR("close relayd control socket");
3108 /* Handle create_sock error. */
3113 /* Assign new file descriptor */
3114 relayd
->control_sock
.sock
.fd
= fd
;
3115 /* Assign version values. */
3116 relayd
->control_sock
.major
= relayd_sock
->major
;
3117 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3120 * Create a session on the relayd and store the returned id. Lock the
3121 * control socket mutex if the relayd was NOT created before.
3123 if (!relayd_created
) {
3124 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3126 ret
= relayd_create_session(&relayd
->control_sock
,
3127 &relayd
->relayd_session_id
);
3128 if (!relayd_created
) {
3129 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3133 * Close all sockets of a relayd object. It will be freed if it was
3134 * created at the error code path or else it will be garbage
3137 (void) relayd_close(&relayd
->control_sock
);
3138 (void) relayd_close(&relayd
->data_sock
);
3143 case LTTNG_STREAM_DATA
:
3144 /* Copy received lttcomm socket */
3145 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3146 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3147 /* Immediately try to close the created socket if valid. */
3148 if (relayd
->data_sock
.sock
.fd
>= 0) {
3149 if (close(relayd
->data_sock
.sock
.fd
)) {
3150 PERROR("close relayd data socket");
3153 /* Handle create_sock error. */
3158 /* Assign new file descriptor */
3159 relayd
->data_sock
.sock
.fd
= fd
;
3160 /* Assign version values. */
3161 relayd
->data_sock
.major
= relayd_sock
->major
;
3162 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3165 ERR("Unknown relayd socket type (%d)", sock_type
);
3170 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3171 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3172 relayd
->net_seq_idx
, fd
);
3175 * Add relayd socket pair to consumer data hashtable. If object already
3176 * exists or on error, the function gracefully returns.
3184 /* Close received socket if valid. */
3187 PERROR("close received socket");
3192 if (relayd_created
) {
3200 * Try to lock the stream mutex.
3202 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3204 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3211 * Try to lock the stream mutex. On failure, we know that the stream is
3212 * being used else where hence there is data still being extracted.
3214 ret
= pthread_mutex_trylock(&stream
->lock
);
3216 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3228 * Search for a relayd associated to the session id and return the reference.
3230 * A rcu read side lock MUST be acquire before calling this function and locked
3231 * until the relayd object is no longer necessary.
3233 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3235 struct lttng_ht_iter iter
;
3236 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3238 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3239 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3242 * Check by sessiond id which is unique here where the relayd session
3243 * id might not be when having multiple relayd.
3245 if (relayd
->sessiond_session_id
== id
) {
3246 /* Found the relayd. There can be only one per id. */
3258 * Check if for a given session id there is still data needed to be extract
3261 * Return 1 if data is pending or else 0 meaning ready to be read.
3263 int consumer_data_pending(uint64_t id
)
3266 struct lttng_ht_iter iter
;
3267 struct lttng_ht
*ht
;
3268 struct lttng_consumer_stream
*stream
;
3269 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3270 int (*data_pending
)(struct lttng_consumer_stream
*);
3272 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3275 pthread_mutex_lock(&consumer_data
.lock
);
3277 switch (consumer_data
.type
) {
3278 case LTTNG_CONSUMER_KERNEL
:
3279 data_pending
= lttng_kconsumer_data_pending
;
3281 case LTTNG_CONSUMER32_UST
:
3282 case LTTNG_CONSUMER64_UST
:
3283 data_pending
= lttng_ustconsumer_data_pending
;
3286 ERR("Unknown consumer data type");
3290 /* Ease our life a bit */
3291 ht
= consumer_data
.stream_list_ht
;
3293 relayd
= find_relayd_by_session_id(id
);
3295 /* Send init command for data pending. */
3296 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3297 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3298 relayd
->relayd_session_id
);
3299 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3301 /* Communication error thus the relayd so no data pending. */
3302 goto data_not_pending
;
3306 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3307 ht
->hash_fct(&id
, lttng_ht_seed
),
3309 &iter
.iter
, stream
, node_session_id
.node
) {
3310 /* If this call fails, the stream is being used hence data pending. */
3311 ret
= stream_try_lock(stream
);
3317 * A removed node from the hash table indicates that the stream has
3318 * been deleted thus having a guarantee that the buffers are closed
3319 * on the consumer side. However, data can still be transmitted
3320 * over the network so don't skip the relayd check.
3322 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3324 /* Check the stream if there is data in the buffers. */
3325 ret
= data_pending(stream
);
3327 pthread_mutex_unlock(&stream
->lock
);
3334 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3335 if (stream
->metadata_flag
) {
3336 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3337 stream
->relayd_stream_id
);
3339 ret
= relayd_data_pending(&relayd
->control_sock
,
3340 stream
->relayd_stream_id
,
3341 stream
->next_net_seq_num
- 1);
3343 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3345 pthread_mutex_unlock(&stream
->lock
);
3349 pthread_mutex_unlock(&stream
->lock
);
3353 unsigned int is_data_inflight
= 0;
3355 /* Send init command for data pending. */
3356 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3357 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3358 relayd
->relayd_session_id
, &is_data_inflight
);
3359 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3361 goto data_not_pending
;
3363 if (is_data_inflight
) {
3369 * Finding _no_ node in the hash table and no inflight data means that the
3370 * stream(s) have been removed thus data is guaranteed to be available for
3371 * analysis from the trace files.
3375 /* Data is available to be read by a viewer. */
3376 pthread_mutex_unlock(&consumer_data
.lock
);
3381 /* Data is still being extracted from buffers. */
3382 pthread_mutex_unlock(&consumer_data
.lock
);
3388 * Send a ret code status message to the sessiond daemon.
3390 * Return the sendmsg() return value.
3392 int consumer_send_status_msg(int sock
, int ret_code
)
3394 struct lttcomm_consumer_status_msg msg
;
3396 msg
.ret_code
= ret_code
;
3398 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3402 * Send a channel status message to the sessiond daemon.
3404 * Return the sendmsg() return value.
3406 int consumer_send_status_channel(int sock
,
3407 struct lttng_consumer_channel
*channel
)
3409 struct lttcomm_consumer_status_channel msg
;
3414 msg
.ret_code
= -LTTNG_ERR_UST_CHAN_FAIL
;
3416 msg
.ret_code
= LTTNG_OK
;
3417 msg
.key
= channel
->key
;
3418 msg
.stream_count
= channel
->streams
.count
;
3421 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));