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>
32 #include <common/common.h>
33 #include <common/utils.h>
34 #include <common/compat/poll.h>
35 #include <common/kernel-ctl/kernel-ctl.h>
36 #include <common/sessiond-comm/relayd.h>
37 #include <common/sessiond-comm/sessiond-comm.h>
38 #include <common/kernel-consumer/kernel-consumer.h>
39 #include <common/relayd/relayd.h>
40 #include <common/ust-consumer/ust-consumer.h>
44 struct lttng_consumer_global_data consumer_data
= {
47 .type
= LTTNG_CONSUMER_UNKNOWN
,
51 * Flag to inform the polling thread to quit when all fd hung up. Updated by
52 * the consumer_thread_receive_fds when it notices that all fds has hung up.
53 * Also updated by the signal handler (consumer_should_exit()). Read by the
56 volatile int consumer_quit
;
59 * Global hash table containing respectively metadata and data streams. The
60 * stream element in this ht should only be updated by the metadata poll thread
61 * for the metadata and the data poll thread for the data.
63 static struct lttng_ht
*metadata_ht
;
64 static struct lttng_ht
*data_ht
;
67 * This hash table contains the mapping between the session id of the sessiond
68 * and the relayd session id. Element of the ht are indexed by sessiond session
71 * Node can be added when a relayd communication is opened in the sessiond
74 * Note that a session id of the session daemon is unique to a tracing session
75 * and not to a domain session. However, a domain session has one consumer
76 * which forces the 1-1 mapping between a consumer and a domain session (ex:
77 * UST). This means that we can't have duplicate in this ht.
79 static struct lttng_ht
*relayd_session_id_ht
;
82 * Notify a thread pipe to poll back again. This usually means that some global
83 * state has changed so we just send back the thread in a poll wait call.
85 static void notify_thread_pipe(int wpipe
)
90 struct lttng_consumer_stream
*null_stream
= NULL
;
92 ret
= write(wpipe
, &null_stream
, sizeof(null_stream
));
93 } while (ret
< 0 && errno
== EINTR
);
97 * Find a stream. The consumer_data.lock must be locked during this
100 static struct lttng_consumer_stream
*consumer_find_stream(int key
,
103 struct lttng_ht_iter iter
;
104 struct lttng_ht_node_ulong
*node
;
105 struct lttng_consumer_stream
*stream
= NULL
;
109 /* Negative keys are lookup failures */
116 lttng_ht_lookup(ht
, (void *)((unsigned long) key
), &iter
);
117 node
= lttng_ht_iter_get_node_ulong(&iter
);
119 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
127 void consumer_steal_stream_key(int key
, struct lttng_ht
*ht
)
129 struct lttng_consumer_stream
*stream
;
132 stream
= consumer_find_stream(key
, ht
);
136 * We don't want the lookup to match, but we still need
137 * to iterate on this stream when iterating over the hash table. Just
138 * change the node key.
140 stream
->node
.key
= -1;
146 * Return a channel object for the given key.
148 * RCU read side lock MUST be acquired before calling this function and
149 * protects the channel ptr.
151 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
153 struct lttng_ht_iter iter
;
154 struct lttng_ht_node_ulong
*node
;
155 struct lttng_consumer_channel
*channel
= NULL
;
157 /* Negative keys are lookup failures */
162 lttng_ht_lookup(consumer_data
.channel_ht
, (void *)((unsigned long) key
),
164 node
= lttng_ht_iter_get_node_ulong(&iter
);
166 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
172 static void consumer_steal_channel_key(int key
)
174 struct lttng_consumer_channel
*channel
;
177 channel
= consumer_find_channel(key
);
181 * We don't want the lookup to match, but we still need
182 * to iterate on this channel when iterating over the hash table. Just
183 * change the node key.
185 channel
->node
.key
= -1;
191 void consumer_free_stream(struct rcu_head
*head
)
193 struct lttng_ht_node_ulong
*node
=
194 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
195 struct lttng_consumer_stream
*stream
=
196 caa_container_of(node
, struct lttng_consumer_stream
, node
);
202 * RCU protected relayd socket pair free.
204 static void consumer_rcu_free_relayd(struct rcu_head
*head
)
206 struct lttng_ht_node_ulong
*node
=
207 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
208 struct consumer_relayd_sock_pair
*relayd
=
209 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
212 * Close all sockets. This is done in the call RCU since we don't want the
213 * socket fds to be reassigned thus potentially creating bad state of the
216 * We do not have to lock the control socket mutex here since at this stage
217 * there is no one referencing to this relayd object.
219 (void) relayd_close(&relayd
->control_sock
);
220 (void) relayd_close(&relayd
->data_sock
);
226 * Destroy and free relayd socket pair object.
228 * This function MUST be called with the consumer_data lock acquired.
230 static void destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
233 struct lttng_ht_iter iter
;
234 struct lttng_ht_node_ulong
*node
;
236 if (relayd
== NULL
) {
240 DBG("Consumer destroy and close relayd socket pair");
242 /* Loockup for a relayd node in the session id map hash table. */
243 lttng_ht_lookup(relayd_session_id_ht
,
244 (void *)((unsigned long) relayd
->sessiond_session_id
), &iter
);
245 node
= lttng_ht_iter_get_node_ulong(&iter
);
247 /* We assume the relayd is being or is destroyed */
252 * Try to delete it from the relayd session id ht. The return value is of
253 * no importance since either way we are going to try to delete the relayd
254 * from the global relayd_ht.
256 lttng_ht_del(relayd_session_id_ht
, &iter
);
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
, consumer_rcu_free_relayd
);
270 * Iterate over the relayd hash table and destroy each element. Finally,
271 * destroy the whole hash table.
273 static void cleanup_relayd_ht(void)
275 struct lttng_ht_iter iter
;
276 struct consumer_relayd_sock_pair
*relayd
;
280 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
282 destroy_relayd(relayd
);
285 lttng_ht_destroy(consumer_data
.relayd_ht
);
286 /* The destroy_relayd call makes sure that this ht is empty here. */
287 lttng_ht_destroy(relayd_session_id_ht
);
293 * Update the end point status of all streams having the given network sequence
294 * index (relayd index).
296 * It's atomically set without having the stream mutex locked which is fine
297 * because we handle the write/read race with a pipe wakeup for each thread.
299 static void update_endpoint_status_by_netidx(int net_seq_idx
,
300 enum consumer_endpoint_status status
)
302 struct lttng_ht_iter iter
;
303 struct lttng_consumer_stream
*stream
;
305 DBG("Consumer set delete flag on stream by idx %d", net_seq_idx
);
309 /* Let's begin with metadata */
310 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
311 if (stream
->net_seq_idx
== net_seq_idx
) {
312 uatomic_set(&stream
->endpoint_status
, status
);
313 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
317 /* Follow up by the data streams */
318 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
319 if (stream
->net_seq_idx
== net_seq_idx
) {
320 uatomic_set(&stream
->endpoint_status
, status
);
321 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
328 * Cleanup a relayd object by flagging every associated streams for deletion,
329 * destroying the object meaning removing it from the relayd hash table,
330 * closing the sockets and freeing the memory in a RCU call.
332 * If a local data context is available, notify the threads that the streams'
333 * state have changed.
335 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
336 struct lttng_consumer_local_data
*ctx
)
342 DBG("Cleaning up relayd sockets");
344 /* Save the net sequence index before destroying the object */
345 netidx
= relayd
->net_seq_idx
;
348 * Delete the relayd from the relayd hash table, close the sockets and free
349 * the object in a RCU call.
351 destroy_relayd(relayd
);
353 /* Set inactive endpoint to all streams */
354 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
357 * With a local data context, notify the threads that the streams' state
358 * have changed. The write() action on the pipe acts as an "implicit"
359 * memory barrier ordering the updates of the end point status from the
360 * read of this status which happens AFTER receiving this notify.
363 notify_thread_pipe(ctx
->consumer_data_pipe
[1]);
364 notify_thread_pipe(ctx
->consumer_metadata_pipe
[1]);
369 * Flag a relayd socket pair for destruction. Destroy it if the refcount
372 * RCU read side lock MUST be aquired before calling this function.
374 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
378 /* Set destroy flag for this object */
379 uatomic_set(&relayd
->destroy_flag
, 1);
381 /* Destroy the relayd if refcount is 0 */
382 if (uatomic_read(&relayd
->refcount
) == 0) {
383 destroy_relayd(relayd
);
388 * Remove a stream from the global list protected by a mutex. This
389 * function is also responsible for freeing its data structures.
391 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
395 struct lttng_ht_iter iter
;
396 struct lttng_consumer_channel
*free_chan
= NULL
;
397 struct consumer_relayd_sock_pair
*relayd
;
401 DBG("Consumer del stream %d", stream
->wait_fd
);
404 /* Means the stream was allocated but not successfully added */
408 pthread_mutex_lock(&consumer_data
.lock
);
409 pthread_mutex_lock(&stream
->lock
);
411 switch (consumer_data
.type
) {
412 case LTTNG_CONSUMER_KERNEL
:
413 if (stream
->mmap_base
!= NULL
) {
414 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
420 case LTTNG_CONSUMER32_UST
:
421 case LTTNG_CONSUMER64_UST
:
422 lttng_ustconsumer_del_stream(stream
);
425 ERR("Unknown consumer_data type");
431 iter
.iter
.node
= &stream
->node
.node
;
432 ret
= lttng_ht_del(ht
, &iter
);
435 /* Remove node session id from the consumer_data stream ht */
436 iter
.iter
.node
= &stream
->node_session_id
.node
;
437 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
441 assert(consumer_data
.stream_count
> 0);
442 consumer_data
.stream_count
--;
444 if (stream
->out_fd
>= 0) {
445 ret
= close(stream
->out_fd
);
450 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
451 ret
= close(stream
->wait_fd
);
456 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
457 ret
= close(stream
->shm_fd
);
463 /* Check and cleanup relayd */
465 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
466 if (relayd
!= NULL
) {
467 uatomic_dec(&relayd
->refcount
);
468 assert(uatomic_read(&relayd
->refcount
) >= 0);
470 /* Closing streams requires to lock the control socket. */
471 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
472 ret
= relayd_send_close_stream(&relayd
->control_sock
,
473 stream
->relayd_stream_id
,
474 stream
->next_net_seq_num
- 1);
475 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
477 DBG("Unable to close stream on the relayd. Continuing");
479 * Continue here. There is nothing we can do for the relayd.
480 * Chances are that the relayd has closed the socket so we just
481 * continue cleaning up.
485 /* Both conditions are met, we destroy the relayd. */
486 if (uatomic_read(&relayd
->refcount
) == 0 &&
487 uatomic_read(&relayd
->destroy_flag
)) {
488 destroy_relayd(relayd
);
493 uatomic_dec(&stream
->chan
->refcount
);
494 if (!uatomic_read(&stream
->chan
->refcount
)
495 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
496 free_chan
= stream
->chan
;
500 consumer_data
.need_update
= 1;
501 pthread_mutex_unlock(&stream
->lock
);
502 pthread_mutex_unlock(&consumer_data
.lock
);
505 consumer_del_channel(free_chan
);
509 call_rcu(&stream
->node
.head
, consumer_free_stream
);
512 struct lttng_consumer_stream
*consumer_allocate_stream(
513 int channel_key
, int stream_key
,
514 int shm_fd
, int wait_fd
,
515 enum lttng_consumer_stream_state state
,
517 enum lttng_event_output output
,
518 const char *path_name
,
526 struct lttng_consumer_stream
*stream
;
528 stream
= zmalloc(sizeof(*stream
));
529 if (stream
== NULL
) {
530 PERROR("malloc struct lttng_consumer_stream");
531 *alloc_ret
= -ENOMEM
;
538 * Get stream's channel reference. Needed when adding the stream to the
541 stream
->chan
= consumer_find_channel(channel_key
);
543 *alloc_ret
= -ENOENT
;
544 ERR("Unable to find channel for stream %d", stream_key
);
548 stream
->key
= stream_key
;
549 stream
->shm_fd
= shm_fd
;
550 stream
->wait_fd
= wait_fd
;
552 stream
->out_fd_offset
= 0;
553 stream
->state
= state
;
554 stream
->mmap_len
= mmap_len
;
555 stream
->mmap_base
= NULL
;
556 stream
->output
= output
;
559 stream
->net_seq_idx
= net_index
;
560 stream
->metadata_flag
= metadata_flag
;
561 stream
->session_id
= session_id
;
562 strncpy(stream
->path_name
, path_name
, sizeof(stream
->path_name
));
563 stream
->path_name
[sizeof(stream
->path_name
) - 1] = '\0';
564 pthread_mutex_init(&stream
->lock
, NULL
);
567 * Index differently the metadata node because the thread is using an
568 * internal hash table to match streams in the metadata_ht to the epoll set
572 lttng_ht_node_init_ulong(&stream
->node
, stream
->wait_fd
);
574 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
577 /* Init session id node with the stream session id */
578 lttng_ht_node_init_ulong(&stream
->node_session_id
, stream
->session_id
);
581 * The cpu number is needed before using any ustctl_* actions. Ignored for
582 * the kernel so the value does not matter.
584 pthread_mutex_lock(&consumer_data
.lock
);
585 stream
->cpu
= stream
->chan
->cpucount
++;
586 pthread_mutex_unlock(&consumer_data
.lock
);
588 DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu,"
589 " out_fd %d, net_seq_idx %d, session_id %" PRIu64
,
590 stream
->path_name
, stream
->key
, stream
->shm_fd
, stream
->wait_fd
,
591 (unsigned long long) stream
->mmap_len
, stream
->out_fd
,
592 stream
->net_seq_idx
, stream
->session_id
);
605 * Add a stream to the global list protected by a mutex.
607 static int consumer_add_stream(struct lttng_consumer_stream
*stream
,
611 struct consumer_relayd_sock_pair
*relayd
;
616 DBG3("Adding consumer stream %d", stream
->key
);
618 pthread_mutex_lock(&consumer_data
.lock
);
619 pthread_mutex_lock(&stream
->lock
);
622 /* Steal stream identifier to avoid having streams with the same key */
623 consumer_steal_stream_key(stream
->key
, ht
);
625 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
628 * Add stream to the stream_list_ht of the consumer data. No need to steal
629 * the key since the HT does not use it and we allow to add redundant keys
632 lttng_ht_add_ulong(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
634 /* Check and cleanup relayd */
635 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
636 if (relayd
!= NULL
) {
637 uatomic_inc(&relayd
->refcount
);
640 /* Update channel refcount once added without error(s). */
641 uatomic_inc(&stream
->chan
->refcount
);
644 * When nb_init_streams reaches 0, we don't need to trigger any action in
645 * terms of destroying the associated channel, because the action that
646 * causes the count to become 0 also causes a stream to be added. The
647 * channel deletion will thus be triggered by the following removal of this
650 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
651 uatomic_dec(&stream
->chan
->nb_init_streams
);
654 /* Update consumer data once the node is inserted. */
655 consumer_data
.stream_count
++;
656 consumer_data
.need_update
= 1;
659 pthread_mutex_unlock(&stream
->lock
);
660 pthread_mutex_unlock(&consumer_data
.lock
);
666 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
667 * be acquired before calling this.
669 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
672 struct lttng_ht_node_ulong
*node
;
673 struct lttng_ht_iter iter
;
675 if (relayd
== NULL
) {
680 lttng_ht_lookup(consumer_data
.relayd_ht
,
681 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
682 node
= lttng_ht_iter_get_node_ulong(&iter
);
684 /* Relayd already exist. Ignore the insertion */
687 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
694 * Allocate and return a consumer relayd socket.
696 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
699 struct consumer_relayd_sock_pair
*obj
= NULL
;
701 /* Negative net sequence index is a failure */
702 if (net_seq_idx
< 0) {
706 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
708 PERROR("zmalloc relayd sock");
712 obj
->net_seq_idx
= net_seq_idx
;
714 obj
->destroy_flag
= 0;
715 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
716 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
723 * Find a relayd socket pair in the global consumer data.
725 * Return the object if found else NULL.
726 * RCU read-side lock must be held across this call and while using the
729 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
731 struct lttng_ht_iter iter
;
732 struct lttng_ht_node_ulong
*node
;
733 struct consumer_relayd_sock_pair
*relayd
= NULL
;
735 /* Negative keys are lookup failures */
740 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
742 node
= lttng_ht_iter_get_node_ulong(&iter
);
744 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
752 * Handle stream for relayd transmission if the stream applies for network
753 * streaming where the net sequence index is set.
755 * Return destination file descriptor or negative value on error.
757 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
758 size_t data_size
, unsigned long padding
,
759 struct consumer_relayd_sock_pair
*relayd
)
762 struct lttcomm_relayd_data_hdr data_hdr
;
768 /* Reset data header */
769 memset(&data_hdr
, 0, sizeof(data_hdr
));
771 if (stream
->metadata_flag
) {
772 /* Caller MUST acquire the relayd control socket lock */
773 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
778 /* Metadata are always sent on the control socket. */
779 outfd
= relayd
->control_sock
.fd
;
781 /* Set header with stream information */
782 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
783 data_hdr
.data_size
= htobe32(data_size
);
784 data_hdr
.padding_size
= htobe32(padding
);
785 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
786 /* Other fields are zeroed previously */
788 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
794 /* Set to go on data socket */
795 outfd
= relayd
->data_sock
.fd
;
803 void consumer_free_channel(struct rcu_head
*head
)
805 struct lttng_ht_node_ulong
*node
=
806 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
807 struct lttng_consumer_channel
*channel
=
808 caa_container_of(node
, struct lttng_consumer_channel
, node
);
814 * Remove a channel from the global list protected by a mutex. This
815 * function is also responsible for freeing its data structures.
817 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
820 struct lttng_ht_iter iter
;
822 DBG("Consumer delete channel key %d", channel
->key
);
824 pthread_mutex_lock(&consumer_data
.lock
);
826 switch (consumer_data
.type
) {
827 case LTTNG_CONSUMER_KERNEL
:
829 case LTTNG_CONSUMER32_UST
:
830 case LTTNG_CONSUMER64_UST
:
831 lttng_ustconsumer_del_channel(channel
);
834 ERR("Unknown consumer_data type");
840 iter
.iter
.node
= &channel
->node
.node
;
841 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
845 if (channel
->mmap_base
!= NULL
) {
846 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
851 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
852 ret
= close(channel
->wait_fd
);
857 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
858 ret
= close(channel
->shm_fd
);
864 call_rcu(&channel
->node
.head
, consumer_free_channel
);
866 pthread_mutex_unlock(&consumer_data
.lock
);
869 struct lttng_consumer_channel
*consumer_allocate_channel(
871 int shm_fd
, int wait_fd
,
873 uint64_t max_sb_size
,
874 unsigned int nb_init_streams
)
876 struct lttng_consumer_channel
*channel
;
879 channel
= zmalloc(sizeof(*channel
));
880 if (channel
== NULL
) {
881 PERROR("malloc struct lttng_consumer_channel");
884 channel
->key
= channel_key
;
885 channel
->shm_fd
= shm_fd
;
886 channel
->wait_fd
= wait_fd
;
887 channel
->mmap_len
= mmap_len
;
888 channel
->max_sb_size
= max_sb_size
;
889 channel
->refcount
= 0;
890 channel
->nb_init_streams
= nb_init_streams
;
891 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
893 switch (consumer_data
.type
) {
894 case LTTNG_CONSUMER_KERNEL
:
895 channel
->mmap_base
= NULL
;
896 channel
->mmap_len
= 0;
898 case LTTNG_CONSUMER32_UST
:
899 case LTTNG_CONSUMER64_UST
:
900 ret
= lttng_ustconsumer_allocate_channel(channel
);
907 ERR("Unknown consumer_data type");
911 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
912 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
913 (unsigned long long) channel
->mmap_len
,
914 (unsigned long long) channel
->max_sb_size
);
920 * Add a channel to the global list protected by a mutex.
922 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
924 struct lttng_ht_node_ulong
*node
;
925 struct lttng_ht_iter iter
;
927 pthread_mutex_lock(&consumer_data
.lock
);
928 /* Steal channel identifier, for UST */
929 consumer_steal_channel_key(channel
->key
);
932 lttng_ht_lookup(consumer_data
.channel_ht
,
933 (void *)((unsigned long) channel
->key
), &iter
);
934 node
= lttng_ht_iter_get_node_ulong(&iter
);
936 /* Channel already exist. Ignore the insertion */
940 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
944 pthread_mutex_unlock(&consumer_data
.lock
);
950 * Allocate the pollfd structure and the local view of the out fds to avoid
951 * doing a lookup in the linked list and concurrency issues when writing is
952 * needed. Called with consumer_data.lock held.
954 * Returns the number of fds in the structures.
956 static int consumer_update_poll_array(
957 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
958 struct lttng_consumer_stream
**local_stream
, struct lttng_ht
*ht
)
961 struct lttng_ht_iter iter
;
962 struct lttng_consumer_stream
*stream
;
964 DBG("Updating poll fd array");
966 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
968 * Only active streams with an active end point can be added to the
969 * poll set and local stream storage of the thread.
971 * There is a potential race here for endpoint_status to be updated
972 * just after the check. However, this is OK since the stream(s) will
973 * be deleted once the thread is notified that the end point state has
974 * changed where this function will be called back again.
976 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
977 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
980 DBG("Active FD %d", stream
->wait_fd
);
981 (*pollfd
)[i
].fd
= stream
->wait_fd
;
982 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
983 local_stream
[i
] = stream
;
989 * Insert the consumer_data_pipe at the end of the array and don't
990 * increment i so nb_fd is the number of real FD.
992 (*pollfd
)[i
].fd
= ctx
->consumer_data_pipe
[0];
993 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
998 * Poll on the should_quit pipe and the command socket return -1 on error and
999 * should exit, 0 if data is available on the command socket
1001 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1006 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1007 if (num_rdy
== -1) {
1009 * Restart interrupted system call.
1011 if (errno
== EINTR
) {
1014 PERROR("Poll error");
1017 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1018 DBG("consumer_should_quit wake up");
1028 * Set the error socket.
1030 void lttng_consumer_set_error_sock(
1031 struct lttng_consumer_local_data
*ctx
, int sock
)
1033 ctx
->consumer_error_socket
= sock
;
1037 * Set the command socket path.
1039 void lttng_consumer_set_command_sock_path(
1040 struct lttng_consumer_local_data
*ctx
, char *sock
)
1042 ctx
->consumer_command_sock_path
= sock
;
1046 * Send return code to the session daemon.
1047 * If the socket is not defined, we return 0, it is not a fatal error
1049 int lttng_consumer_send_error(
1050 struct lttng_consumer_local_data
*ctx
, int cmd
)
1052 if (ctx
->consumer_error_socket
> 0) {
1053 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1054 sizeof(enum lttcomm_sessiond_command
));
1061 * Close all the tracefiles and stream fds and MUST be called when all
1062 * instances are destroyed i.e. when all threads were joined and are ended.
1064 void lttng_consumer_cleanup(void)
1066 struct lttng_ht_iter iter
;
1067 struct lttng_ht_node_ulong
*node
;
1071 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
1073 struct lttng_consumer_channel
*channel
=
1074 caa_container_of(node
, struct lttng_consumer_channel
, node
);
1075 consumer_del_channel(channel
);
1080 lttng_ht_destroy(consumer_data
.channel_ht
);
1082 cleanup_relayd_ht();
1085 * This HT contains streams that are freed by either the metadata thread or
1086 * the data thread so we do *nothing* on the hash table and simply destroy
1089 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1093 * Called from signal handler.
1095 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1100 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1101 } while (ret
< 0 && errno
== EINTR
);
1103 PERROR("write consumer quit");
1106 DBG("Consumer flag that it should quit");
1109 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1112 int outfd
= stream
->out_fd
;
1115 * This does a blocking write-and-wait on any page that belongs to the
1116 * subbuffer prior to the one we just wrote.
1117 * Don't care about error values, as these are just hints and ways to
1118 * limit the amount of page cache used.
1120 if (orig_offset
< stream
->chan
->max_sb_size
) {
1123 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
1124 stream
->chan
->max_sb_size
,
1125 SYNC_FILE_RANGE_WAIT_BEFORE
1126 | SYNC_FILE_RANGE_WRITE
1127 | SYNC_FILE_RANGE_WAIT_AFTER
);
1129 * Give hints to the kernel about how we access the file:
1130 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1133 * We need to call fadvise again after the file grows because the
1134 * kernel does not seem to apply fadvise to non-existing parts of the
1137 * Call fadvise _after_ having waited for the page writeback to
1138 * complete because the dirty page writeback semantic is not well
1139 * defined. So it can be expected to lead to lower throughput in
1142 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
1143 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
1147 * Initialise the necessary environnement :
1148 * - create a new context
1149 * - create the poll_pipe
1150 * - create the should_quit pipe (for signal handler)
1151 * - create the thread pipe (for splice)
1153 * Takes a function pointer as argument, this function is called when data is
1154 * available on a buffer. This function is responsible to do the
1155 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1156 * buffer configuration and then kernctl_put_next_subbuf at the end.
1158 * Returns a pointer to the new context or NULL on error.
1160 struct lttng_consumer_local_data
*lttng_consumer_create(
1161 enum lttng_consumer_type type
,
1162 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1163 struct lttng_consumer_local_data
*ctx
),
1164 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1165 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1166 int (*update_stream
)(int stream_key
, uint32_t state
))
1169 struct lttng_consumer_local_data
*ctx
;
1171 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1172 consumer_data
.type
== type
);
1173 consumer_data
.type
= type
;
1175 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1177 PERROR("allocating context");
1181 ctx
->consumer_error_socket
= -1;
1182 /* assign the callbacks */
1183 ctx
->on_buffer_ready
= buffer_ready
;
1184 ctx
->on_recv_channel
= recv_channel
;
1185 ctx
->on_recv_stream
= recv_stream
;
1186 ctx
->on_update_stream
= update_stream
;
1188 ret
= pipe(ctx
->consumer_data_pipe
);
1190 PERROR("Error creating poll pipe");
1191 goto error_poll_pipe
;
1194 /* set read end of the pipe to non-blocking */
1195 ret
= fcntl(ctx
->consumer_data_pipe
[0], F_SETFL
, O_NONBLOCK
);
1197 PERROR("fcntl O_NONBLOCK");
1198 goto error_poll_fcntl
;
1201 /* set write end of the pipe to non-blocking */
1202 ret
= fcntl(ctx
->consumer_data_pipe
[1], F_SETFL
, O_NONBLOCK
);
1204 PERROR("fcntl O_NONBLOCK");
1205 goto error_poll_fcntl
;
1208 ret
= pipe(ctx
->consumer_should_quit
);
1210 PERROR("Error creating recv pipe");
1211 goto error_quit_pipe
;
1214 ret
= pipe(ctx
->consumer_thread_pipe
);
1216 PERROR("Error creating thread pipe");
1217 goto error_thread_pipe
;
1220 ret
= utils_create_pipe(ctx
->consumer_metadata_pipe
);
1222 goto error_metadata_pipe
;
1225 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1227 goto error_splice_pipe
;
1233 utils_close_pipe(ctx
->consumer_metadata_pipe
);
1234 error_metadata_pipe
:
1235 utils_close_pipe(ctx
->consumer_thread_pipe
);
1237 for (i
= 0; i
< 2; i
++) {
1240 err
= close(ctx
->consumer_should_quit
[i
]);
1247 for (i
= 0; i
< 2; i
++) {
1250 err
= close(ctx
->consumer_data_pipe
[i
]);
1262 * Close all fds associated with the instance and free the context.
1264 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1268 DBG("Consumer destroying it. Closing everything.");
1270 ret
= close(ctx
->consumer_error_socket
);
1274 ret
= close(ctx
->consumer_thread_pipe
[0]);
1278 ret
= close(ctx
->consumer_thread_pipe
[1]);
1282 ret
= close(ctx
->consumer_data_pipe
[0]);
1286 ret
= close(ctx
->consumer_data_pipe
[1]);
1290 ret
= close(ctx
->consumer_should_quit
[0]);
1294 ret
= close(ctx
->consumer_should_quit
[1]);
1298 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1300 unlink(ctx
->consumer_command_sock_path
);
1305 * Write the metadata stream id on the specified file descriptor.
1307 static int write_relayd_metadata_id(int fd
,
1308 struct lttng_consumer_stream
*stream
,
1309 struct consumer_relayd_sock_pair
*relayd
,
1310 unsigned long padding
)
1313 struct lttcomm_relayd_metadata_payload hdr
;
1315 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1316 hdr
.padding_size
= htobe32(padding
);
1318 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1319 } while (ret
< 0 && errno
== EINTR
);
1321 PERROR("write metadata stream id");
1324 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1325 stream
->relayd_stream_id
, padding
);
1332 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1333 * core function for writing trace buffers to either the local filesystem or
1336 * It must be called with the stream lock held.
1338 * Careful review MUST be put if any changes occur!
1340 * Returns the number of bytes written
1342 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1343 struct lttng_consumer_local_data
*ctx
,
1344 struct lttng_consumer_stream
*stream
, unsigned long len
,
1345 unsigned long padding
)
1347 unsigned long mmap_offset
;
1348 ssize_t ret
= 0, written
= 0;
1349 off_t orig_offset
= stream
->out_fd_offset
;
1350 /* Default is on the disk */
1351 int outfd
= stream
->out_fd
;
1352 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1353 unsigned int relayd_hang_up
= 0;
1355 /* RCU lock for the relayd pointer */
1358 /* Flag that the current stream if set for network streaming. */
1359 if (stream
->net_seq_idx
!= -1) {
1360 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1361 if (relayd
== NULL
) {
1366 /* get the offset inside the fd to mmap */
1367 switch (consumer_data
.type
) {
1368 case LTTNG_CONSUMER_KERNEL
:
1369 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1371 case LTTNG_CONSUMER32_UST
:
1372 case LTTNG_CONSUMER64_UST
:
1373 ret
= lttng_ustctl_get_mmap_read_offset(stream
->chan
->handle
,
1374 stream
->buf
, &mmap_offset
);
1377 ERR("Unknown consumer_data type");
1382 PERROR("tracer ctl get_mmap_read_offset");
1387 /* Handle stream on the relayd if the output is on the network */
1389 unsigned long netlen
= len
;
1392 * Lock the control socket for the complete duration of the function
1393 * since from this point on we will use the socket.
1395 if (stream
->metadata_flag
) {
1396 /* Metadata requires the control socket. */
1397 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1398 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1401 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1403 /* Use the returned socket. */
1406 /* Write metadata stream id before payload */
1407 if (stream
->metadata_flag
) {
1408 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1411 /* Socket operation failed. We consider the relayd dead */
1412 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1420 /* Socket operation failed. We consider the relayd dead */
1421 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1425 /* Else, use the default set before which is the filesystem. */
1428 /* No streaming, we have to set the len with the full padding */
1434 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
1435 } while (ret
< 0 && errno
== EINTR
);
1436 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1438 PERROR("Error in file write");
1442 /* Socket operation failed. We consider the relayd dead */
1443 if (errno
== EPIPE
|| errno
== EINVAL
) {
1448 } else if (ret
> len
) {
1449 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1457 /* This call is useless on a socket so better save a syscall. */
1459 /* This won't block, but will start writeout asynchronously */
1460 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1461 SYNC_FILE_RANGE_WRITE
);
1462 stream
->out_fd_offset
+= ret
;
1466 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1470 * This is a special case that the relayd has closed its socket. Let's
1471 * cleanup the relayd object and all associated streams.
1473 if (relayd
&& relayd_hang_up
) {
1474 cleanup_relayd(relayd
, ctx
);
1478 /* Unlock only if ctrl socket used */
1479 if (relayd
&& stream
->metadata_flag
) {
1480 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1488 * Splice the data from the ring buffer to the tracefile.
1490 * It must be called with the stream lock held.
1492 * Returns the number of bytes spliced.
1494 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1495 struct lttng_consumer_local_data
*ctx
,
1496 struct lttng_consumer_stream
*stream
, unsigned long len
,
1497 unsigned long padding
)
1499 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1501 off_t orig_offset
= stream
->out_fd_offset
;
1502 int fd
= stream
->wait_fd
;
1503 /* Default is on the disk */
1504 int outfd
= stream
->out_fd
;
1505 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1507 unsigned int relayd_hang_up
= 0;
1509 switch (consumer_data
.type
) {
1510 case LTTNG_CONSUMER_KERNEL
:
1512 case LTTNG_CONSUMER32_UST
:
1513 case LTTNG_CONSUMER64_UST
:
1514 /* Not supported for user space tracing */
1517 ERR("Unknown consumer_data type");
1521 /* RCU lock for the relayd pointer */
1524 /* Flag that the current stream if set for network streaming. */
1525 if (stream
->net_seq_idx
!= -1) {
1526 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1527 if (relayd
== NULL
) {
1533 * Choose right pipe for splice. Metadata and trace data are handled by
1534 * different threads hence the use of two pipes in order not to race or
1535 * corrupt the written data.
1537 if (stream
->metadata_flag
) {
1538 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1540 splice_pipe
= ctx
->consumer_thread_pipe
;
1543 /* Write metadata stream id before payload */
1545 int total_len
= len
;
1547 if (stream
->metadata_flag
) {
1549 * Lock the control socket for the complete duration of the function
1550 * since from this point on we will use the socket.
1552 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1554 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1558 /* Socket operation failed. We consider the relayd dead */
1559 if (ret
== -EBADF
) {
1560 WARN("Remote relayd disconnected. Stopping");
1567 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1570 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1572 /* Use the returned socket. */
1575 /* Socket operation failed. We consider the relayd dead */
1576 if (ret
== -EBADF
) {
1577 WARN("Remote relayd disconnected. Stopping");
1584 /* No streaming, we have to set the len with the full padding */
1589 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1590 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1591 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1592 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1593 DBG("splice chan to pipe, ret %zd", ret_splice
);
1594 if (ret_splice
< 0) {
1595 PERROR("Error in relay splice");
1597 written
= ret_splice
;
1603 /* Handle stream on the relayd if the output is on the network */
1605 if (stream
->metadata_flag
) {
1606 size_t metadata_payload_size
=
1607 sizeof(struct lttcomm_relayd_metadata_payload
);
1609 /* Update counter to fit the spliced data */
1610 ret_splice
+= metadata_payload_size
;
1611 len
+= metadata_payload_size
;
1613 * We do this so the return value can match the len passed as
1614 * argument to this function.
1616 written
-= metadata_payload_size
;
1620 /* Splice data out */
1621 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1622 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1623 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1624 if (ret_splice
< 0) {
1625 PERROR("Error in file splice");
1627 written
= ret_splice
;
1629 /* Socket operation failed. We consider the relayd dead */
1630 if (errno
== EBADF
|| errno
== EPIPE
) {
1631 WARN("Remote relayd disconnected. Stopping");
1637 } else if (ret_splice
> len
) {
1639 PERROR("Wrote more data than requested %zd (len: %lu)",
1641 written
+= ret_splice
;
1647 /* This call is useless on a socket so better save a syscall. */
1649 /* This won't block, but will start writeout asynchronously */
1650 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1651 SYNC_FILE_RANGE_WRITE
);
1652 stream
->out_fd_offset
+= ret_splice
;
1654 written
+= ret_splice
;
1656 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1664 * This is a special case that the relayd has closed its socket. Let's
1665 * cleanup the relayd object and all associated streams.
1667 if (relayd
&& relayd_hang_up
) {
1668 cleanup_relayd(relayd
, ctx
);
1669 /* Skip splice error so the consumer does not fail */
1674 /* send the appropriate error description to sessiond */
1677 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1680 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1683 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1688 if (relayd
&& stream
->metadata_flag
) {
1689 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1697 * Take a snapshot for a specific fd
1699 * Returns 0 on success, < 0 on error
1701 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1702 struct lttng_consumer_stream
*stream
)
1704 switch (consumer_data
.type
) {
1705 case LTTNG_CONSUMER_KERNEL
:
1706 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1707 case LTTNG_CONSUMER32_UST
:
1708 case LTTNG_CONSUMER64_UST
:
1709 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1711 ERR("Unknown consumer_data type");
1719 * Get the produced position
1721 * Returns 0 on success, < 0 on error
1723 int lttng_consumer_get_produced_snapshot(
1724 struct lttng_consumer_local_data
*ctx
,
1725 struct lttng_consumer_stream
*stream
,
1728 switch (consumer_data
.type
) {
1729 case LTTNG_CONSUMER_KERNEL
:
1730 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1731 case LTTNG_CONSUMER32_UST
:
1732 case LTTNG_CONSUMER64_UST
:
1733 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1735 ERR("Unknown consumer_data type");
1741 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1742 int sock
, struct pollfd
*consumer_sockpoll
)
1744 switch (consumer_data
.type
) {
1745 case LTTNG_CONSUMER_KERNEL
:
1746 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1747 case LTTNG_CONSUMER32_UST
:
1748 case LTTNG_CONSUMER64_UST
:
1749 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1751 ERR("Unknown consumer_data type");
1758 * Iterate over all streams of the hashtable and free them properly.
1760 * WARNING: *MUST* be used with data stream only.
1762 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1764 struct lttng_ht_iter iter
;
1765 struct lttng_consumer_stream
*stream
;
1772 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1774 * Ignore return value since we are currently cleaning up so any error
1777 (void) consumer_del_stream(stream
, ht
);
1781 lttng_ht_destroy(ht
);
1785 * Iterate over all streams of the hashtable and free them properly.
1787 * XXX: Should not be only for metadata stream or else use an other name.
1789 static void destroy_stream_ht(struct lttng_ht
*ht
)
1791 struct lttng_ht_iter iter
;
1792 struct lttng_consumer_stream
*stream
;
1799 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1801 * Ignore return value since we are currently cleaning up so any error
1804 (void) consumer_del_metadata_stream(stream
, ht
);
1808 lttng_ht_destroy(ht
);
1812 * Clean up a metadata stream and free its memory.
1814 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1815 struct lttng_ht
*ht
)
1818 struct lttng_ht_iter iter
;
1819 struct lttng_consumer_channel
*free_chan
= NULL
;
1820 struct consumer_relayd_sock_pair
*relayd
;
1824 * This call should NEVER receive regular stream. It must always be
1825 * metadata stream and this is crucial for data structure synchronization.
1827 assert(stream
->metadata_flag
);
1829 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1832 /* Means the stream was allocated but not successfully added */
1836 pthread_mutex_lock(&consumer_data
.lock
);
1837 pthread_mutex_lock(&stream
->lock
);
1839 switch (consumer_data
.type
) {
1840 case LTTNG_CONSUMER_KERNEL
:
1841 if (stream
->mmap_base
!= NULL
) {
1842 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1844 PERROR("munmap metadata stream");
1848 case LTTNG_CONSUMER32_UST
:
1849 case LTTNG_CONSUMER64_UST
:
1850 lttng_ustconsumer_del_stream(stream
);
1853 ERR("Unknown consumer_data type");
1859 iter
.iter
.node
= &stream
->node
.node
;
1860 ret
= lttng_ht_del(ht
, &iter
);
1863 /* Remove node session id from the consumer_data stream ht */
1864 iter
.iter
.node
= &stream
->node_session_id
.node
;
1865 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1869 if (stream
->out_fd
>= 0) {
1870 ret
= close(stream
->out_fd
);
1876 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
1877 ret
= close(stream
->wait_fd
);
1883 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
1884 ret
= close(stream
->shm_fd
);
1890 /* Check and cleanup relayd */
1892 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1893 if (relayd
!= NULL
) {
1894 uatomic_dec(&relayd
->refcount
);
1895 assert(uatomic_read(&relayd
->refcount
) >= 0);
1897 /* Closing streams requires to lock the control socket. */
1898 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1899 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1900 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1901 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1903 DBG("Unable to close stream on the relayd. Continuing");
1905 * Continue here. There is nothing we can do for the relayd.
1906 * Chances are that the relayd has closed the socket so we just
1907 * continue cleaning up.
1911 /* Both conditions are met, we destroy the relayd. */
1912 if (uatomic_read(&relayd
->refcount
) == 0 &&
1913 uatomic_read(&relayd
->destroy_flag
)) {
1914 destroy_relayd(relayd
);
1919 /* Atomically decrement channel refcount since other threads can use it. */
1920 uatomic_dec(&stream
->chan
->refcount
);
1921 if (!uatomic_read(&stream
->chan
->refcount
)
1922 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
1923 /* Go for channel deletion! */
1924 free_chan
= stream
->chan
;
1928 pthread_mutex_unlock(&stream
->lock
);
1929 pthread_mutex_unlock(&consumer_data
.lock
);
1932 consumer_del_channel(free_chan
);
1936 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1940 * Action done with the metadata stream when adding it to the consumer internal
1941 * data structures to handle it.
1943 static int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
,
1944 struct lttng_ht
*ht
)
1947 struct consumer_relayd_sock_pair
*relayd
;
1948 struct lttng_ht_iter iter
;
1949 struct lttng_ht_node_ulong
*node
;
1954 DBG3("Adding metadata stream %d to hash table", stream
->wait_fd
);
1956 pthread_mutex_lock(&consumer_data
.lock
);
1957 pthread_mutex_lock(&stream
->lock
);
1960 * From here, refcounts are updated so be _careful_ when returning an error
1967 * Lookup the stream just to make sure it does not exist in our internal
1968 * state. This should NEVER happen.
1970 lttng_ht_lookup(ht
, (void *)((unsigned long) stream
->wait_fd
), &iter
);
1971 node
= lttng_ht_iter_get_node_ulong(&iter
);
1974 /* Find relayd and, if one is found, increment refcount. */
1975 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1976 if (relayd
!= NULL
) {
1977 uatomic_inc(&relayd
->refcount
);
1980 /* Update channel refcount once added without error(s). */
1981 uatomic_inc(&stream
->chan
->refcount
);
1984 * When nb_init_streams reaches 0, we don't need to trigger any action in
1985 * terms of destroying the associated channel, because the action that
1986 * causes the count to become 0 also causes a stream to be added. The
1987 * channel deletion will thus be triggered by the following removal of this
1990 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
1991 uatomic_dec(&stream
->chan
->nb_init_streams
);
1994 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
1997 * Add stream to the stream_list_ht of the consumer data. No need to steal
1998 * the key since the HT does not use it and we allow to add redundant keys
2001 lttng_ht_add_ulong(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2005 pthread_mutex_unlock(&stream
->lock
);
2006 pthread_mutex_unlock(&consumer_data
.lock
);
2011 * Delete data stream that are flagged for deletion (endpoint_status).
2013 static void validate_endpoint_status_data_stream(void)
2015 struct lttng_ht_iter iter
;
2016 struct lttng_consumer_stream
*stream
;
2018 DBG("Consumer delete flagged data stream");
2021 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2022 /* Validate delete flag of the stream */
2023 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2026 /* Delete it right now */
2027 consumer_del_stream(stream
, data_ht
);
2033 * Delete metadata stream that are flagged for deletion (endpoint_status).
2035 static void validate_endpoint_status_metadata_stream(
2036 struct lttng_poll_event
*pollset
)
2038 struct lttng_ht_iter iter
;
2039 struct lttng_consumer_stream
*stream
;
2041 DBG("Consumer delete flagged metadata stream");
2046 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2047 /* Validate delete flag of the stream */
2048 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2052 * Remove from pollset so the metadata thread can continue without
2053 * blocking on a deleted stream.
2055 lttng_poll_del(pollset
, stream
->wait_fd
);
2057 /* Delete it right now */
2058 consumer_del_metadata_stream(stream
, metadata_ht
);
2064 * Thread polls on metadata file descriptor and write them on disk or on the
2067 void *consumer_thread_metadata_poll(void *data
)
2070 uint32_t revents
, nb_fd
;
2071 struct lttng_consumer_stream
*stream
= NULL
;
2072 struct lttng_ht_iter iter
;
2073 struct lttng_ht_node_ulong
*node
;
2074 struct lttng_poll_event events
;
2075 struct lttng_consumer_local_data
*ctx
= data
;
2078 rcu_register_thread();
2080 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2082 /* ENOMEM at this point. Better to bail out. */
2086 DBG("Thread metadata poll started");
2088 /* Size is set to 1 for the consumer_metadata pipe */
2089 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2091 ERR("Poll set creation failed");
2095 ret
= lttng_poll_add(&events
, ctx
->consumer_metadata_pipe
[0], LPOLLIN
);
2101 DBG("Metadata main loop started");
2104 /* Only the metadata pipe is set */
2105 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2110 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2111 ret
= lttng_poll_wait(&events
, -1);
2112 DBG("Metadata event catched in thread");
2114 if (errno
== EINTR
) {
2115 ERR("Poll EINTR catched");
2123 /* From here, the event is a metadata wait fd */
2124 for (i
= 0; i
< nb_fd
; i
++) {
2125 revents
= LTTNG_POLL_GETEV(&events
, i
);
2126 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2128 /* Just don't waste time if no returned events for the fd */
2133 if (pollfd
== ctx
->consumer_metadata_pipe
[0]) {
2134 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2135 DBG("Metadata thread pipe hung up");
2137 * Remove the pipe from the poll set and continue the loop
2138 * since their might be data to consume.
2140 lttng_poll_del(&events
, ctx
->consumer_metadata_pipe
[0]);
2141 ret
= close(ctx
->consumer_metadata_pipe
[0]);
2143 PERROR("close metadata pipe");
2146 } else if (revents
& LPOLLIN
) {
2148 /* Get the stream pointer received */
2149 ret
= read(pollfd
, &stream
, sizeof(stream
));
2150 } while (ret
< 0 && errno
== EINTR
);
2152 ret
< sizeof(struct lttng_consumer_stream
*)) {
2153 PERROR("read metadata stream");
2155 * Let's continue here and hope we can still work
2156 * without stopping the consumer. XXX: Should we?
2161 /* A NULL stream means that the state has changed. */
2162 if (stream
== NULL
) {
2163 /* Check for deleted streams. */
2164 validate_endpoint_status_metadata_stream(&events
);
2168 DBG("Adding metadata stream %d to poll set",
2171 ret
= consumer_add_metadata_stream(stream
, metadata_ht
);
2173 ERR("Unable to add metadata stream");
2174 /* Stream was not setup properly. Continuing. */
2175 consumer_del_metadata_stream(stream
, NULL
);
2179 /* Add metadata stream to the global poll events list */
2180 lttng_poll_add(&events
, stream
->wait_fd
,
2181 LPOLLIN
| LPOLLPRI
);
2184 /* Handle other stream */
2189 lttng_ht_lookup(metadata_ht
, (void *)((unsigned long) pollfd
),
2191 node
= lttng_ht_iter_get_node_ulong(&iter
);
2194 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2197 /* Check for error event */
2198 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2199 DBG("Metadata fd %d is hup|err.", pollfd
);
2200 if (!stream
->hangup_flush_done
2201 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2202 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2203 DBG("Attempting to flush and consume the UST buffers");
2204 lttng_ustconsumer_on_stream_hangup(stream
);
2206 /* We just flushed the stream now read it. */
2208 len
= ctx
->on_buffer_ready(stream
, ctx
);
2210 * We don't check the return value here since if we get
2211 * a negative len, it means an error occured thus we
2212 * simply remove it from the poll set and free the
2218 lttng_poll_del(&events
, stream
->wait_fd
);
2220 * This call update the channel states, closes file descriptors
2221 * and securely free the stream.
2223 consumer_del_metadata_stream(stream
, metadata_ht
);
2224 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2225 /* Get the data out of the metadata file descriptor */
2226 DBG("Metadata available on fd %d", pollfd
);
2227 assert(stream
->wait_fd
== pollfd
);
2229 len
= ctx
->on_buffer_ready(stream
, ctx
);
2230 /* It's ok to have an unavailable sub-buffer */
2231 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2232 /* Clean up stream from consumer and free it. */
2233 lttng_poll_del(&events
, stream
->wait_fd
);
2234 consumer_del_metadata_stream(stream
, metadata_ht
);
2235 } else if (len
> 0) {
2236 stream
->data_read
= 1;
2240 /* Release RCU lock for the stream looked up */
2247 DBG("Metadata poll thread exiting");
2248 lttng_poll_clean(&events
);
2250 destroy_stream_ht(metadata_ht
);
2252 rcu_unregister_thread();
2257 * This thread polls the fds in the set to consume the data and write
2258 * it to tracefile if necessary.
2260 void *consumer_thread_data_poll(void *data
)
2262 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2263 struct pollfd
*pollfd
= NULL
;
2264 /* local view of the streams */
2265 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2266 /* local view of consumer_data.fds_count */
2268 struct lttng_consumer_local_data
*ctx
= data
;
2271 rcu_register_thread();
2273 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2274 if (data_ht
== NULL
) {
2275 /* ENOMEM at this point. Better to bail out. */
2279 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
2286 * the fds set has been updated, we need to update our
2287 * local array as well
2289 pthread_mutex_lock(&consumer_data
.lock
);
2290 if (consumer_data
.need_update
) {
2291 if (pollfd
!= NULL
) {
2295 if (local_stream
!= NULL
) {
2297 local_stream
= NULL
;
2300 /* allocate for all fds + 1 for the consumer_data_pipe */
2301 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2302 if (pollfd
== NULL
) {
2303 PERROR("pollfd malloc");
2304 pthread_mutex_unlock(&consumer_data
.lock
);
2308 /* allocate for all fds + 1 for the consumer_data_pipe */
2309 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2310 sizeof(struct lttng_consumer_stream
));
2311 if (local_stream
== NULL
) {
2312 PERROR("local_stream malloc");
2313 pthread_mutex_unlock(&consumer_data
.lock
);
2316 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
,
2319 ERR("Error in allocating pollfd or local_outfds");
2320 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2321 pthread_mutex_unlock(&consumer_data
.lock
);
2325 consumer_data
.need_update
= 0;
2327 pthread_mutex_unlock(&consumer_data
.lock
);
2329 /* No FDs and consumer_quit, consumer_cleanup the thread */
2330 if (nb_fd
== 0 && consumer_quit
== 1) {
2333 /* poll on the array of fds */
2335 DBG("polling on %d fd", nb_fd
+ 1);
2336 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2337 DBG("poll num_rdy : %d", num_rdy
);
2338 if (num_rdy
== -1) {
2340 * Restart interrupted system call.
2342 if (errno
== EINTR
) {
2345 PERROR("Poll error");
2346 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2348 } else if (num_rdy
== 0) {
2349 DBG("Polling thread timed out");
2354 * If the consumer_data_pipe triggered poll go directly to the
2355 * beginning of the loop to update the array. We want to prioritize
2356 * array update over low-priority reads.
2358 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2359 size_t pipe_readlen
;
2361 DBG("consumer_data_pipe wake up");
2362 /* Consume 1 byte of pipe data */
2364 pipe_readlen
= read(ctx
->consumer_data_pipe
[0], &new_stream
,
2365 sizeof(new_stream
));
2366 } while (pipe_readlen
== -1 && errno
== EINTR
);
2369 * If the stream is NULL, just ignore it. It's also possible that
2370 * the sessiond poll thread changed the consumer_quit state and is
2371 * waking us up to test it.
2373 if (new_stream
== NULL
) {
2374 validate_endpoint_status_data_stream();
2378 ret
= consumer_add_stream(new_stream
, data_ht
);
2380 ERR("Consumer add stream %d failed. Continuing",
2383 * At this point, if the add_stream fails, it is not in the
2384 * hash table thus passing the NULL value here.
2386 consumer_del_stream(new_stream
, NULL
);
2389 /* Continue to update the local streams and handle prio ones */
2393 /* Take care of high priority channels first. */
2394 for (i
= 0; i
< nb_fd
; i
++) {
2395 if (local_stream
[i
] == NULL
) {
2398 if (pollfd
[i
].revents
& POLLPRI
) {
2399 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2401 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2402 /* it's ok to have an unavailable sub-buffer */
2403 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2404 /* Clean the stream and free it. */
2405 consumer_del_stream(local_stream
[i
], data_ht
);
2406 local_stream
[i
] = NULL
;
2407 } else if (len
> 0) {
2408 local_stream
[i
]->data_read
= 1;
2414 * If we read high prio channel in this loop, try again
2415 * for more high prio data.
2421 /* Take care of low priority channels. */
2422 for (i
= 0; i
< nb_fd
; i
++) {
2423 if (local_stream
[i
] == NULL
) {
2426 if ((pollfd
[i
].revents
& POLLIN
) ||
2427 local_stream
[i
]->hangup_flush_done
) {
2428 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2429 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2430 /* it's ok to have an unavailable sub-buffer */
2431 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2432 /* Clean the stream and free it. */
2433 consumer_del_stream(local_stream
[i
], data_ht
);
2434 local_stream
[i
] = NULL
;
2435 } else if (len
> 0) {
2436 local_stream
[i
]->data_read
= 1;
2441 /* Handle hangup and errors */
2442 for (i
= 0; i
< nb_fd
; i
++) {
2443 if (local_stream
[i
] == NULL
) {
2446 if (!local_stream
[i
]->hangup_flush_done
2447 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2448 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2449 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2450 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2452 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2453 /* Attempt read again, for the data we just flushed. */
2454 local_stream
[i
]->data_read
= 1;
2457 * If the poll flag is HUP/ERR/NVAL and we have
2458 * read no data in this pass, we can remove the
2459 * stream from its hash table.
2461 if ((pollfd
[i
].revents
& POLLHUP
)) {
2462 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2463 if (!local_stream
[i
]->data_read
) {
2464 consumer_del_stream(local_stream
[i
], data_ht
);
2465 local_stream
[i
] = NULL
;
2468 } else if (pollfd
[i
].revents
& POLLERR
) {
2469 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2470 if (!local_stream
[i
]->data_read
) {
2471 consumer_del_stream(local_stream
[i
], data_ht
);
2472 local_stream
[i
] = NULL
;
2475 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2476 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2477 if (!local_stream
[i
]->data_read
) {
2478 consumer_del_stream(local_stream
[i
], data_ht
);
2479 local_stream
[i
] = NULL
;
2483 if (local_stream
[i
] != NULL
) {
2484 local_stream
[i
]->data_read
= 0;
2489 DBG("polling thread exiting");
2490 if (pollfd
!= NULL
) {
2494 if (local_stream
!= NULL
) {
2496 local_stream
= NULL
;
2500 * Close the write side of the pipe so epoll_wait() in
2501 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2502 * read side of the pipe. If we close them both, epoll_wait strangely does
2503 * not return and could create a endless wait period if the pipe is the
2504 * only tracked fd in the poll set. The thread will take care of closing
2507 ret
= close(ctx
->consumer_metadata_pipe
[1]);
2509 PERROR("close data pipe");
2512 destroy_data_stream_ht(data_ht
);
2514 rcu_unregister_thread();
2519 * This thread listens on the consumerd socket and receives the file
2520 * descriptors from the session daemon.
2522 void *consumer_thread_sessiond_poll(void *data
)
2524 int sock
= -1, client_socket
, ret
;
2526 * structure to poll for incoming data on communication socket avoids
2527 * making blocking sockets.
2529 struct pollfd consumer_sockpoll
[2];
2530 struct lttng_consumer_local_data
*ctx
= data
;
2532 rcu_register_thread();
2534 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2535 unlink(ctx
->consumer_command_sock_path
);
2536 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2537 if (client_socket
< 0) {
2538 ERR("Cannot create command socket");
2542 ret
= lttcomm_listen_unix_sock(client_socket
);
2547 DBG("Sending ready command to lttng-sessiond");
2548 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2549 /* return < 0 on error, but == 0 is not fatal */
2551 ERR("Error sending ready command to lttng-sessiond");
2555 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2557 PERROR("fcntl O_NONBLOCK");
2561 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2562 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2563 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2564 consumer_sockpoll
[1].fd
= client_socket
;
2565 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2567 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2570 DBG("Connection on client_socket");
2572 /* Blocking call, waiting for transmission */
2573 sock
= lttcomm_accept_unix_sock(client_socket
);
2578 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2580 PERROR("fcntl O_NONBLOCK");
2584 /* This socket is not useful anymore. */
2585 ret
= close(client_socket
);
2587 PERROR("close client_socket");
2591 /* update the polling structure to poll on the established socket */
2592 consumer_sockpoll
[1].fd
= sock
;
2593 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2596 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2599 DBG("Incoming command on sock");
2600 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2601 if (ret
== -ENOENT
) {
2602 DBG("Received STOP command");
2607 * This could simply be a session daemon quitting. Don't output
2610 DBG("Communication interrupted on command socket");
2613 if (consumer_quit
) {
2614 DBG("consumer_thread_receive_fds received quit from signal");
2617 DBG("received fds on sock");
2620 DBG("consumer_thread_receive_fds exiting");
2623 * when all fds have hung up, the polling thread
2629 * Notify the data poll thread to poll back again and test the
2630 * consumer_quit state that we just set so to quit gracefully.
2632 notify_thread_pipe(ctx
->consumer_data_pipe
[1]);
2634 /* Cleaning up possibly open sockets. */
2638 PERROR("close sock sessiond poll");
2641 if (client_socket
>= 0) {
2644 PERROR("close client_socket sessiond poll");
2648 rcu_unregister_thread();
2652 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2653 struct lttng_consumer_local_data
*ctx
)
2657 pthread_mutex_lock(&stream
->lock
);
2659 switch (consumer_data
.type
) {
2660 case LTTNG_CONSUMER_KERNEL
:
2661 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
2663 case LTTNG_CONSUMER32_UST
:
2664 case LTTNG_CONSUMER64_UST
:
2665 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2668 ERR("Unknown consumer_data type");
2674 pthread_mutex_unlock(&stream
->lock
);
2678 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2680 switch (consumer_data
.type
) {
2681 case LTTNG_CONSUMER_KERNEL
:
2682 return lttng_kconsumer_on_recv_stream(stream
);
2683 case LTTNG_CONSUMER32_UST
:
2684 case LTTNG_CONSUMER64_UST
:
2685 return lttng_ustconsumer_on_recv_stream(stream
);
2687 ERR("Unknown consumer_data type");
2694 * Allocate and set consumer data hash tables.
2696 void lttng_consumer_init(void)
2698 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2699 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2700 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2701 relayd_session_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2705 * Process the ADD_RELAYD command receive by a consumer.
2707 * This will create a relayd socket pair and add it to the relayd hash table.
2708 * The caller MUST acquire a RCU read side lock before calling it.
2710 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
2711 struct lttng_consumer_local_data
*ctx
, int sock
,
2712 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
,
2713 unsigned int sessiond_id
)
2715 int fd
= -1, ret
= -1, relayd_created
= 0;
2716 enum lttng_error_code ret_code
= LTTNG_OK
;
2717 struct consumer_relayd_sock_pair
*relayd
;
2718 struct consumer_relayd_session_id
*relayd_id_node
;
2720 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
2722 /* First send a status message before receiving the fds. */
2723 ret
= consumer_send_status_msg(sock
, ret_code
);
2725 /* Somehow, the session daemon is not responding anymore. */
2729 /* Get relayd reference if exists. */
2730 relayd
= consumer_find_relayd(net_seq_idx
);
2731 if (relayd
== NULL
) {
2732 /* Not found. Allocate one. */
2733 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
2734 if (relayd
== NULL
) {
2735 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
2738 relayd
->sessiond_session_id
= (uint64_t) sessiond_id
;
2742 /* Poll on consumer socket. */
2743 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2748 /* Get relayd socket from session daemon */
2749 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2750 if (ret
!= sizeof(fd
)) {
2751 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
2753 fd
= -1; /* Just in case it gets set with an invalid value. */
2757 /* We have the fds without error. Send status back. */
2758 ret
= consumer_send_status_msg(sock
, ret_code
);
2760 /* Somehow, the session daemon is not responding anymore. */
2764 /* Copy socket information and received FD */
2765 switch (sock_type
) {
2766 case LTTNG_STREAM_CONTROL
:
2767 /* Copy received lttcomm socket */
2768 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
2769 ret
= lttcomm_create_sock(&relayd
->control_sock
);
2770 /* Immediately try to close the created socket if valid. */
2771 if (relayd
->control_sock
.fd
>= 0) {
2772 if (close(relayd
->control_sock
.fd
)) {
2773 PERROR("close relayd control socket");
2776 /* Handle create_sock error. */
2781 /* Assign new file descriptor */
2782 relayd
->control_sock
.fd
= fd
;
2785 * Create a session on the relayd and store the returned id. No need to
2786 * grab the socket lock since the relayd object is not yet visible.
2788 ret
= relayd_create_session(&relayd
->control_sock
,
2789 &relayd
->relayd_session_id
);
2794 /* Set up a relayd session id node. */
2795 relayd_id_node
= zmalloc(sizeof(struct consumer_relayd_session_id
));
2796 if (!relayd_id_node
) {
2797 PERROR("zmalloc relayd id node");
2801 relayd_id_node
->relayd_id
= relayd
->relayd_session_id
;
2802 relayd_id_node
->sessiond_id
= (uint64_t) sessiond_id
;
2804 /* Indexed by session id of the sessiond. */
2805 lttng_ht_node_init_ulong(&relayd_id_node
->node
,
2806 relayd_id_node
->sessiond_id
);
2808 lttng_ht_add_unique_ulong(relayd_session_id_ht
, &relayd_id_node
->node
);
2812 case LTTNG_STREAM_DATA
:
2813 /* Copy received lttcomm socket */
2814 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
2815 ret
= lttcomm_create_sock(&relayd
->data_sock
);
2816 /* Immediately try to close the created socket if valid. */
2817 if (relayd
->data_sock
.fd
>= 0) {
2818 if (close(relayd
->data_sock
.fd
)) {
2819 PERROR("close relayd data socket");
2822 /* Handle create_sock error. */
2827 /* Assign new file descriptor */
2828 relayd
->data_sock
.fd
= fd
;
2831 ERR("Unknown relayd socket type (%d)", sock_type
);
2835 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2836 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
2837 relayd
->net_seq_idx
, fd
);
2840 * Add relayd socket pair to consumer data hashtable. If object already
2841 * exists or on error, the function gracefully returns.
2849 /* Close received socket if valid. */
2852 PERROR("close received socket");
2856 if (relayd_created
) {
2857 /* We just want to cleanup. Ignore ret value. */
2858 (void) relayd_close(&relayd
->control_sock
);
2859 (void) relayd_close(&relayd
->data_sock
);
2867 * Try to lock the stream mutex.
2869 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
2871 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
2878 * Try to lock the stream mutex. On failure, we know that the stream is
2879 * being used else where hence there is data still being extracted.
2881 ret
= pthread_mutex_trylock(&stream
->lock
);
2883 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
2895 * Search for a relayd associated to the session id and return the reference.
2897 * A rcu read side lock MUST be acquire before calling this function and locked
2898 * until the relayd object is no longer necessary.
2900 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
2902 struct lttng_ht_iter iter
;
2903 struct lttng_ht_node_ulong
*node
;
2904 struct consumer_relayd_sock_pair
*relayd
= NULL
;
2905 struct consumer_relayd_session_id
*session_id_map
;
2907 /* Get the session id map. */
2908 lttng_ht_lookup(relayd_session_id_ht
, (void *)((unsigned long) id
), &iter
);
2909 node
= lttng_ht_iter_get_node_ulong(&iter
);
2914 session_id_map
= caa_container_of(node
, struct consumer_relayd_session_id
,
2917 /* Iterate over all relayd since they are indexed by net_seq_idx. */
2918 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
2920 if (relayd
->relayd_session_id
== session_id_map
->relayd_id
) {
2921 /* Found the relayd. There can be only one per id. */
2931 * Check if for a given session id there is still data needed to be extract
2934 * Return 1 if data is pending or else 0 meaning ready to be read.
2936 int consumer_data_pending(uint64_t id
)
2939 struct lttng_ht_iter iter
;
2940 struct lttng_ht
*ht
;
2941 struct lttng_consumer_stream
*stream
;
2942 struct consumer_relayd_sock_pair
*relayd
= NULL
;
2943 int (*data_pending
)(struct lttng_consumer_stream
*);
2945 DBG("Consumer data pending command on session id %" PRIu64
, id
);
2948 pthread_mutex_lock(&consumer_data
.lock
);
2950 switch (consumer_data
.type
) {
2951 case LTTNG_CONSUMER_KERNEL
:
2952 data_pending
= lttng_kconsumer_data_pending
;
2954 case LTTNG_CONSUMER32_UST
:
2955 case LTTNG_CONSUMER64_UST
:
2956 data_pending
= lttng_ustconsumer_data_pending
;
2959 ERR("Unknown consumer data type");
2963 /* Ease our life a bit */
2964 ht
= consumer_data
.stream_list_ht
;
2966 relayd
= find_relayd_by_session_id(id
);
2968 /* Send init command for data pending. */
2969 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
2970 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
2971 relayd
->relayd_session_id
);
2972 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2974 /* Communication error thus the relayd so no data pending. */
2975 goto data_not_pending
;
2979 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2980 ht
->hash_fct((void *)((unsigned long) id
), lttng_ht_seed
),
2981 ht
->match_fct
, (void *)((unsigned long) id
),
2982 &iter
.iter
, stream
, node_session_id
.node
) {
2983 /* If this call fails, the stream is being used hence data pending. */
2984 ret
= stream_try_lock(stream
);
2990 * A removed node from the hash table indicates that the stream has
2991 * been deleted thus having a guarantee that the buffers are closed
2992 * on the consumer side. However, data can still be transmitted
2993 * over the network so don't skip the relayd check.
2995 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
2997 /* Check the stream if there is data in the buffers. */
2998 ret
= data_pending(stream
);
3000 pthread_mutex_unlock(&stream
->lock
);
3007 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3008 if (stream
->metadata_flag
) {
3009 ret
= relayd_quiescent_control(&relayd
->control_sock
);
3011 ret
= relayd_data_pending(&relayd
->control_sock
,
3012 stream
->relayd_stream_id
, stream
->next_net_seq_num
);
3014 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3016 pthread_mutex_unlock(&stream
->lock
);
3020 pthread_mutex_unlock(&stream
->lock
);
3024 unsigned int is_data_inflight
= 0;
3026 /* Send init command for data pending. */
3027 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3028 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3029 relayd
->relayd_session_id
, &is_data_inflight
);
3030 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3032 goto data_not_pending
;
3034 if (is_data_inflight
) {
3040 * Finding _no_ node in the hash table and no inflight data means that the
3041 * stream(s) have been removed thus data is guaranteed to be available for
3042 * analysis from the trace files.
3046 /* Data is available to be read by a viewer. */
3047 pthread_mutex_unlock(&consumer_data
.lock
);
3052 /* Data is still being extracted from buffers. */
3053 pthread_mutex_unlock(&consumer_data
.lock
);
3059 * Send a ret code status message to the sessiond daemon.
3061 * Return the sendmsg() return value.
3063 int consumer_send_status_msg(int sock
, int ret_code
)
3065 struct lttcomm_consumer_status_msg msg
;
3067 msg
.ret_code
= ret_code
;
3069 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));