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
,
50 /* timeout parameter, to control the polling thread grace period. */
51 int consumer_poll_timeout
= -1;
54 * Flag to inform the polling thread to quit when all fd hung up. Updated by
55 * the consumer_thread_receive_fds when it notices that all fds has hung up.
56 * Also updated by the signal handler (consumer_should_exit()). Read by the
59 volatile int consumer_quit
= 0;
62 * The following two hash tables are visible by all threads which are separated
63 * in different source files.
65 * Global hash table containing respectively metadata and data streams. The
66 * stream element in this ht should only be updated by the metadata poll thread
67 * for the metadata and the data poll thread for the data.
69 struct lttng_ht
*metadata_ht
= NULL
;
70 struct lttng_ht
*data_ht
= NULL
;
73 * Find a stream. The consumer_data.lock must be locked during this
76 static struct lttng_consumer_stream
*consumer_find_stream(int key
,
79 struct lttng_ht_iter iter
;
80 struct lttng_ht_node_ulong
*node
;
81 struct lttng_consumer_stream
*stream
= NULL
;
85 /* Negative keys are lookup failures */
92 lttng_ht_lookup(ht
, (void *)((unsigned long) key
), &iter
);
93 node
= lttng_ht_iter_get_node_ulong(&iter
);
95 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
103 void consumer_steal_stream_key(int key
, struct lttng_ht
*ht
)
105 struct lttng_consumer_stream
*stream
;
108 stream
= consumer_find_stream(key
, ht
);
112 * We don't want the lookup to match, but we still need
113 * to iterate on this stream when iterating over the hash table. Just
114 * change the node key.
116 stream
->node
.key
= -1;
121 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
123 struct lttng_ht_iter iter
;
124 struct lttng_ht_node_ulong
*node
;
125 struct lttng_consumer_channel
*channel
= NULL
;
127 /* Negative keys are lookup failures */
134 lttng_ht_lookup(consumer_data
.channel_ht
, (void *)((unsigned long) key
),
136 node
= lttng_ht_iter_get_node_ulong(&iter
);
138 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
146 static void consumer_steal_channel_key(int key
)
148 struct lttng_consumer_channel
*channel
;
151 channel
= consumer_find_channel(key
);
155 * We don't want the lookup to match, but we still need
156 * to iterate on this channel when iterating over the hash table. Just
157 * change the node key.
159 channel
->node
.key
= -1;
165 void consumer_free_stream(struct rcu_head
*head
)
167 struct lttng_ht_node_ulong
*node
=
168 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
169 struct lttng_consumer_stream
*stream
=
170 caa_container_of(node
, struct lttng_consumer_stream
, node
);
176 void consumer_free_metadata_stream(struct rcu_head
*head
)
178 struct lttng_ht_node_ulong
*node
=
179 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
180 struct lttng_consumer_stream
*stream
=
181 caa_container_of(node
, struct lttng_consumer_stream
, waitfd_node
);
187 * RCU protected relayd socket pair free.
189 static void consumer_rcu_free_relayd(struct rcu_head
*head
)
191 struct lttng_ht_node_ulong
*node
=
192 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
193 struct consumer_relayd_sock_pair
*relayd
=
194 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
200 * Destroy and free relayd socket pair object.
202 * This function MUST be called with the consumer_data lock acquired.
204 static void destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
207 struct lttng_ht_iter iter
;
209 if (relayd
== NULL
) {
213 DBG("Consumer destroy and close relayd socket pair");
215 iter
.iter
.node
= &relayd
->node
.node
;
216 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
218 /* We assume the relayd was already destroyed */
222 /* Close all sockets */
223 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
224 (void) relayd_close(&relayd
->control_sock
);
225 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
226 (void) relayd_close(&relayd
->data_sock
);
228 /* RCU free() call */
229 call_rcu(&relayd
->node
.head
, consumer_rcu_free_relayd
);
233 * Flag a relayd socket pair for destruction. Destroy it if the refcount
236 * RCU read side lock MUST be aquired before calling this function.
238 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
242 /* Set destroy flag for this object */
243 uatomic_set(&relayd
->destroy_flag
, 1);
245 /* Destroy the relayd if refcount is 0 */
246 if (uatomic_read(&relayd
->refcount
) == 0) {
247 destroy_relayd(relayd
);
252 * Remove a stream from the global list protected by a mutex. This
253 * function is also responsible for freeing its data structures.
255 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
259 struct lttng_ht_iter iter
;
260 struct lttng_consumer_channel
*free_chan
= NULL
;
261 struct consumer_relayd_sock_pair
*relayd
;
266 /* Means the stream was allocated but not successfully added */
270 pthread_mutex_lock(&consumer_data
.lock
);
272 switch (consumer_data
.type
) {
273 case LTTNG_CONSUMER_KERNEL
:
274 if (stream
->mmap_base
!= NULL
) {
275 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
281 case LTTNG_CONSUMER32_UST
:
282 case LTTNG_CONSUMER64_UST
:
283 lttng_ustconsumer_del_stream(stream
);
286 ERR("Unknown consumer_data type");
292 iter
.iter
.node
= &stream
->node
.node
;
293 ret
= lttng_ht_del(ht
, &iter
);
298 if (consumer_data
.stream_count
<= 0) {
301 consumer_data
.stream_count
--;
305 if (stream
->out_fd
>= 0) {
306 ret
= close(stream
->out_fd
);
311 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
312 ret
= close(stream
->wait_fd
);
317 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
318 ret
= close(stream
->shm_fd
);
324 /* Check and cleanup relayd */
326 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
327 if (relayd
!= NULL
) {
328 uatomic_dec(&relayd
->refcount
);
329 assert(uatomic_read(&relayd
->refcount
) >= 0);
331 /* Closing streams requires to lock the control socket. */
332 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
333 ret
= relayd_send_close_stream(&relayd
->control_sock
,
334 stream
->relayd_stream_id
,
335 stream
->next_net_seq_num
- 1);
336 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
338 DBG("Unable to close stream on the relayd. Continuing");
340 * Continue here. There is nothing we can do for the relayd.
341 * Chances are that the relayd has closed the socket so we just
342 * continue cleaning up.
346 /* Both conditions are met, we destroy the relayd. */
347 if (uatomic_read(&relayd
->refcount
) == 0 &&
348 uatomic_read(&relayd
->destroy_flag
)) {
349 destroy_relayd(relayd
);
354 uatomic_dec(&stream
->chan
->refcount
);
355 if (!uatomic_read(&stream
->chan
->refcount
)
356 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
357 free_chan
= stream
->chan
;
361 consumer_data
.need_update
= 1;
362 pthread_mutex_unlock(&consumer_data
.lock
);
365 consumer_del_channel(free_chan
);
369 call_rcu(&stream
->node
.head
, consumer_free_stream
);
372 struct lttng_consumer_stream
*consumer_allocate_stream(
373 int channel_key
, int stream_key
,
374 int shm_fd
, int wait_fd
,
375 enum lttng_consumer_stream_state state
,
377 enum lttng_event_output output
,
378 const char *path_name
,
385 struct lttng_consumer_stream
*stream
;
387 stream
= zmalloc(sizeof(*stream
));
388 if (stream
== NULL
) {
389 PERROR("malloc struct lttng_consumer_stream");
390 *alloc_ret
= -ENOMEM
;
395 * Get stream's channel reference. Needed when adding the stream to the
398 stream
->chan
= consumer_find_channel(channel_key
);
400 *alloc_ret
= -ENOENT
;
401 ERR("Unable to find channel for stream %d", stream_key
);
405 stream
->key
= stream_key
;
406 stream
->shm_fd
= shm_fd
;
407 stream
->wait_fd
= wait_fd
;
409 stream
->out_fd_offset
= 0;
410 stream
->state
= state
;
411 stream
->mmap_len
= mmap_len
;
412 stream
->mmap_base
= NULL
;
413 stream
->output
= output
;
416 stream
->net_seq_idx
= net_index
;
417 stream
->metadata_flag
= metadata_flag
;
418 strncpy(stream
->path_name
, path_name
, sizeof(stream
->path_name
));
419 stream
->path_name
[sizeof(stream
->path_name
) - 1] = '\0';
420 lttng_ht_node_init_ulong(&stream
->waitfd_node
, stream
->wait_fd
);
421 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
424 * The cpu number is needed before using any ustctl_* actions. Ignored for
425 * the kernel so the value does not matter.
427 pthread_mutex_lock(&consumer_data
.lock
);
428 stream
->cpu
= stream
->chan
->cpucount
++;
429 pthread_mutex_unlock(&consumer_data
.lock
);
431 DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu,"
432 " out_fd %d, net_seq_idx %d)", stream
->path_name
, stream
->key
,
433 stream
->shm_fd
, stream
->wait_fd
,
434 (unsigned long long) stream
->mmap_len
, stream
->out_fd
,
435 stream
->net_seq_idx
);
445 * Add a stream to the global list protected by a mutex.
447 static int consumer_add_stream(struct lttng_consumer_stream
*stream
,
451 struct consumer_relayd_sock_pair
*relayd
;
456 DBG3("Adding consumer stream %d", stream
->key
);
458 pthread_mutex_lock(&consumer_data
.lock
);
461 /* Steal stream identifier to avoid having streams with the same key */
462 consumer_steal_stream_key(stream
->key
, ht
);
464 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
466 /* Check and cleanup relayd */
467 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
468 if (relayd
!= NULL
) {
469 uatomic_inc(&relayd
->refcount
);
472 /* Update channel refcount once added without error(s). */
473 uatomic_inc(&stream
->chan
->refcount
);
476 * When nb_init_streams reaches 0, we don't need to trigger any action in
477 * terms of destroying the associated channel, because the action that
478 * causes the count to become 0 also causes a stream to be added. The
479 * channel deletion will thus be triggered by the following removal of this
482 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
483 uatomic_dec(&stream
->chan
->nb_init_streams
);
486 /* Update consumer data once the node is inserted. */
487 consumer_data
.stream_count
++;
488 consumer_data
.need_update
= 1;
491 pthread_mutex_unlock(&consumer_data
.lock
);
497 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
498 * be acquired before calling this.
500 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
503 struct lttng_ht_node_ulong
*node
;
504 struct lttng_ht_iter iter
;
506 if (relayd
== NULL
) {
511 lttng_ht_lookup(consumer_data
.relayd_ht
,
512 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
513 node
= lttng_ht_iter_get_node_ulong(&iter
);
515 /* Relayd already exist. Ignore the insertion */
518 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
525 * Allocate and return a consumer relayd socket.
527 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
530 struct consumer_relayd_sock_pair
*obj
= NULL
;
532 /* Negative net sequence index is a failure */
533 if (net_seq_idx
< 0) {
537 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
539 PERROR("zmalloc relayd sock");
543 obj
->net_seq_idx
= net_seq_idx
;
545 obj
->destroy_flag
= 0;
546 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
547 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
554 * Find a relayd socket pair in the global consumer data.
556 * Return the object if found else NULL.
557 * RCU read-side lock must be held across this call and while using the
560 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
562 struct lttng_ht_iter iter
;
563 struct lttng_ht_node_ulong
*node
;
564 struct consumer_relayd_sock_pair
*relayd
= NULL
;
566 /* Negative keys are lookup failures */
571 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
573 node
= lttng_ht_iter_get_node_ulong(&iter
);
575 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
583 * Handle stream for relayd transmission if the stream applies for network
584 * streaming where the net sequence index is set.
586 * Return destination file descriptor or negative value on error.
588 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
589 size_t data_size
, unsigned long padding
,
590 struct consumer_relayd_sock_pair
*relayd
)
593 struct lttcomm_relayd_data_hdr data_hdr
;
599 /* Reset data header */
600 memset(&data_hdr
, 0, sizeof(data_hdr
));
602 if (stream
->metadata_flag
) {
603 /* Caller MUST acquire the relayd control socket lock */
604 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
609 /* Metadata are always sent on the control socket. */
610 outfd
= relayd
->control_sock
.fd
;
612 /* Set header with stream information */
613 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
614 data_hdr
.data_size
= htobe32(data_size
);
615 data_hdr
.padding_size
= htobe32(padding
);
616 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
617 /* Other fields are zeroed previously */
619 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
625 /* Set to go on data socket */
626 outfd
= relayd
->data_sock
.fd
;
634 * Update a stream according to what we just received.
636 void consumer_change_stream_state(int stream_key
,
637 enum lttng_consumer_stream_state state
)
639 struct lttng_consumer_stream
*stream
;
641 pthread_mutex_lock(&consumer_data
.lock
);
642 stream
= consumer_find_stream(stream_key
, consumer_data
.stream_ht
);
644 stream
->state
= state
;
646 consumer_data
.need_update
= 1;
647 pthread_mutex_unlock(&consumer_data
.lock
);
651 void consumer_free_channel(struct rcu_head
*head
)
653 struct lttng_ht_node_ulong
*node
=
654 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
655 struct lttng_consumer_channel
*channel
=
656 caa_container_of(node
, struct lttng_consumer_channel
, node
);
662 * Remove a channel from the global list protected by a mutex. This
663 * function is also responsible for freeing its data structures.
665 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
668 struct lttng_ht_iter iter
;
670 pthread_mutex_lock(&consumer_data
.lock
);
672 switch (consumer_data
.type
) {
673 case LTTNG_CONSUMER_KERNEL
:
675 case LTTNG_CONSUMER32_UST
:
676 case LTTNG_CONSUMER64_UST
:
677 lttng_ustconsumer_del_channel(channel
);
680 ERR("Unknown consumer_data type");
686 iter
.iter
.node
= &channel
->node
.node
;
687 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
691 if (channel
->mmap_base
!= NULL
) {
692 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
697 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
698 ret
= close(channel
->wait_fd
);
703 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
704 ret
= close(channel
->shm_fd
);
710 call_rcu(&channel
->node
.head
, consumer_free_channel
);
712 pthread_mutex_unlock(&consumer_data
.lock
);
715 struct lttng_consumer_channel
*consumer_allocate_channel(
717 int shm_fd
, int wait_fd
,
719 uint64_t max_sb_size
,
720 unsigned int nb_init_streams
)
722 struct lttng_consumer_channel
*channel
;
725 channel
= zmalloc(sizeof(*channel
));
726 if (channel
== NULL
) {
727 PERROR("malloc struct lttng_consumer_channel");
730 channel
->key
= channel_key
;
731 channel
->shm_fd
= shm_fd
;
732 channel
->wait_fd
= wait_fd
;
733 channel
->mmap_len
= mmap_len
;
734 channel
->max_sb_size
= max_sb_size
;
735 channel
->refcount
= 0;
736 channel
->nb_init_streams
= nb_init_streams
;
737 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
739 switch (consumer_data
.type
) {
740 case LTTNG_CONSUMER_KERNEL
:
741 channel
->mmap_base
= NULL
;
742 channel
->mmap_len
= 0;
744 case LTTNG_CONSUMER32_UST
:
745 case LTTNG_CONSUMER64_UST
:
746 ret
= lttng_ustconsumer_allocate_channel(channel
);
753 ERR("Unknown consumer_data type");
757 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
758 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
759 (unsigned long long) channel
->mmap_len
,
760 (unsigned long long) channel
->max_sb_size
);
766 * Add a channel to the global list protected by a mutex.
768 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
770 struct lttng_ht_node_ulong
*node
;
771 struct lttng_ht_iter iter
;
773 pthread_mutex_lock(&consumer_data
.lock
);
774 /* Steal channel identifier, for UST */
775 consumer_steal_channel_key(channel
->key
);
778 lttng_ht_lookup(consumer_data
.channel_ht
,
779 (void *)((unsigned long) channel
->key
), &iter
);
780 node
= lttng_ht_iter_get_node_ulong(&iter
);
782 /* Channel already exist. Ignore the insertion */
786 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
790 pthread_mutex_unlock(&consumer_data
.lock
);
796 * Allocate the pollfd structure and the local view of the out fds to avoid
797 * doing a lookup in the linked list and concurrency issues when writing is
798 * needed. Called with consumer_data.lock held.
800 * Returns the number of fds in the structures.
802 static int consumer_update_poll_array(
803 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
804 struct lttng_consumer_stream
**local_stream
, struct lttng_ht
*ht
)
807 struct lttng_ht_iter iter
;
808 struct lttng_consumer_stream
*stream
;
810 DBG("Updating poll fd array");
812 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
813 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
816 DBG("Active FD %d", stream
->wait_fd
);
817 (*pollfd
)[i
].fd
= stream
->wait_fd
;
818 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
819 local_stream
[i
] = stream
;
825 * Insert the consumer_poll_pipe at the end of the array and don't
826 * increment i so nb_fd is the number of real FD.
828 (*pollfd
)[i
].fd
= ctx
->consumer_poll_pipe
[0];
829 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
834 * Poll on the should_quit pipe and the command socket return -1 on error and
835 * should exit, 0 if data is available on the command socket
837 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
842 num_rdy
= poll(consumer_sockpoll
, 2, -1);
845 * Restart interrupted system call.
847 if (errno
== EINTR
) {
850 PERROR("Poll error");
853 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
854 DBG("consumer_should_quit wake up");
864 * Set the error socket.
866 void lttng_consumer_set_error_sock(
867 struct lttng_consumer_local_data
*ctx
, int sock
)
869 ctx
->consumer_error_socket
= sock
;
873 * Set the command socket path.
875 void lttng_consumer_set_command_sock_path(
876 struct lttng_consumer_local_data
*ctx
, char *sock
)
878 ctx
->consumer_command_sock_path
= sock
;
882 * Send return code to the session daemon.
883 * If the socket is not defined, we return 0, it is not a fatal error
885 int lttng_consumer_send_error(
886 struct lttng_consumer_local_data
*ctx
, int cmd
)
888 if (ctx
->consumer_error_socket
> 0) {
889 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
890 sizeof(enum lttcomm_sessiond_command
));
897 * Close all the tracefiles and stream fds, should be called when all instances
900 void lttng_consumer_cleanup(void)
902 struct lttng_ht_iter iter
;
903 struct lttng_ht_node_ulong
*node
;
908 * close all outfd. Called when there are no more threads running (after
909 * joining on the threads), no need to protect list iteration with mutex.
911 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, node
,
913 struct lttng_consumer_stream
*stream
=
914 caa_container_of(node
, struct lttng_consumer_stream
, node
);
915 consumer_del_stream(stream
, consumer_data
.stream_ht
);
918 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
920 struct lttng_consumer_channel
*channel
=
921 caa_container_of(node
, struct lttng_consumer_channel
, node
);
922 consumer_del_channel(channel
);
927 lttng_ht_destroy(consumer_data
.stream_ht
);
928 lttng_ht_destroy(consumer_data
.channel_ht
);
932 * Called from signal handler.
934 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
939 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
940 } while (ret
< 0 && errno
== EINTR
);
942 PERROR("write consumer quit");
946 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
949 int outfd
= stream
->out_fd
;
952 * This does a blocking write-and-wait on any page that belongs to the
953 * subbuffer prior to the one we just wrote.
954 * Don't care about error values, as these are just hints and ways to
955 * limit the amount of page cache used.
957 if (orig_offset
< stream
->chan
->max_sb_size
) {
960 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
961 stream
->chan
->max_sb_size
,
962 SYNC_FILE_RANGE_WAIT_BEFORE
963 | SYNC_FILE_RANGE_WRITE
964 | SYNC_FILE_RANGE_WAIT_AFTER
);
966 * Give hints to the kernel about how we access the file:
967 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
970 * We need to call fadvise again after the file grows because the
971 * kernel does not seem to apply fadvise to non-existing parts of the
974 * Call fadvise _after_ having waited for the page writeback to
975 * complete because the dirty page writeback semantic is not well
976 * defined. So it can be expected to lead to lower throughput in
979 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
980 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
984 * Initialise the necessary environnement :
985 * - create a new context
986 * - create the poll_pipe
987 * - create the should_quit pipe (for signal handler)
988 * - create the thread pipe (for splice)
990 * Takes a function pointer as argument, this function is called when data is
991 * available on a buffer. This function is responsible to do the
992 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
993 * buffer configuration and then kernctl_put_next_subbuf at the end.
995 * Returns a pointer to the new context or NULL on error.
997 struct lttng_consumer_local_data
*lttng_consumer_create(
998 enum lttng_consumer_type type
,
999 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1000 struct lttng_consumer_local_data
*ctx
),
1001 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1002 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1003 int (*update_stream
)(int stream_key
, uint32_t state
))
1006 struct lttng_consumer_local_data
*ctx
;
1008 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1009 consumer_data
.type
== type
);
1010 consumer_data
.type
= type
;
1012 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1014 PERROR("allocating context");
1018 ctx
->consumer_error_socket
= -1;
1019 /* assign the callbacks */
1020 ctx
->on_buffer_ready
= buffer_ready
;
1021 ctx
->on_recv_channel
= recv_channel
;
1022 ctx
->on_recv_stream
= recv_stream
;
1023 ctx
->on_update_stream
= update_stream
;
1025 ret
= pipe(ctx
->consumer_poll_pipe
);
1027 PERROR("Error creating poll pipe");
1028 goto error_poll_pipe
;
1031 /* set read end of the pipe to non-blocking */
1032 ret
= fcntl(ctx
->consumer_poll_pipe
[0], F_SETFL
, O_NONBLOCK
);
1034 PERROR("fcntl O_NONBLOCK");
1035 goto error_poll_fcntl
;
1038 /* set write end of the pipe to non-blocking */
1039 ret
= fcntl(ctx
->consumer_poll_pipe
[1], F_SETFL
, O_NONBLOCK
);
1041 PERROR("fcntl O_NONBLOCK");
1042 goto error_poll_fcntl
;
1045 ret
= pipe(ctx
->consumer_should_quit
);
1047 PERROR("Error creating recv pipe");
1048 goto error_quit_pipe
;
1051 ret
= pipe(ctx
->consumer_thread_pipe
);
1053 PERROR("Error creating thread pipe");
1054 goto error_thread_pipe
;
1057 ret
= utils_create_pipe(ctx
->consumer_metadata_pipe
);
1059 goto error_metadata_pipe
;
1062 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1064 goto error_splice_pipe
;
1070 utils_close_pipe(ctx
->consumer_metadata_pipe
);
1071 error_metadata_pipe
:
1072 utils_close_pipe(ctx
->consumer_thread_pipe
);
1074 for (i
= 0; i
< 2; i
++) {
1077 err
= close(ctx
->consumer_should_quit
[i
]);
1084 for (i
= 0; i
< 2; i
++) {
1087 err
= close(ctx
->consumer_poll_pipe
[i
]);
1099 * Close all fds associated with the instance and free the context.
1101 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1105 ret
= close(ctx
->consumer_error_socket
);
1109 ret
= close(ctx
->consumer_thread_pipe
[0]);
1113 ret
= close(ctx
->consumer_thread_pipe
[1]);
1117 ret
= close(ctx
->consumer_poll_pipe
[0]);
1121 ret
= close(ctx
->consumer_poll_pipe
[1]);
1125 ret
= close(ctx
->consumer_should_quit
[0]);
1129 ret
= close(ctx
->consumer_should_quit
[1]);
1133 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1135 unlink(ctx
->consumer_command_sock_path
);
1140 * Write the metadata stream id on the specified file descriptor.
1142 static int write_relayd_metadata_id(int fd
,
1143 struct lttng_consumer_stream
*stream
,
1144 struct consumer_relayd_sock_pair
*relayd
,
1145 unsigned long padding
)
1148 struct lttcomm_relayd_metadata_payload hdr
;
1150 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1151 hdr
.padding_size
= htobe32(padding
);
1153 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1154 } while (ret
< 0 && errno
== EINTR
);
1156 PERROR("write metadata stream id");
1159 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1160 stream
->relayd_stream_id
, padding
);
1167 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1168 * core function for writing trace buffers to either the local filesystem or
1171 * Careful review MUST be put if any changes occur!
1173 * Returns the number of bytes written
1175 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1176 struct lttng_consumer_local_data
*ctx
,
1177 struct lttng_consumer_stream
*stream
, unsigned long len
,
1178 unsigned long padding
)
1180 unsigned long mmap_offset
;
1181 ssize_t ret
= 0, written
= 0;
1182 off_t orig_offset
= stream
->out_fd_offset
;
1183 /* Default is on the disk */
1184 int outfd
= stream
->out_fd
;
1185 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1187 /* RCU lock for the relayd pointer */
1190 /* Flag that the current stream if set for network streaming. */
1191 if (stream
->net_seq_idx
!= -1) {
1192 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1193 if (relayd
== NULL
) {
1198 /* get the offset inside the fd to mmap */
1199 switch (consumer_data
.type
) {
1200 case LTTNG_CONSUMER_KERNEL
:
1201 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1203 case LTTNG_CONSUMER32_UST
:
1204 case LTTNG_CONSUMER64_UST
:
1205 ret
= lttng_ustctl_get_mmap_read_offset(stream
->chan
->handle
,
1206 stream
->buf
, &mmap_offset
);
1209 ERR("Unknown consumer_data type");
1214 PERROR("tracer ctl get_mmap_read_offset");
1219 /* Handle stream on the relayd if the output is on the network */
1221 unsigned long netlen
= len
;
1224 * Lock the control socket for the complete duration of the function
1225 * since from this point on we will use the socket.
1227 if (stream
->metadata_flag
) {
1228 /* Metadata requires the control socket. */
1229 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1230 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1233 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1235 /* Use the returned socket. */
1238 /* Write metadata stream id before payload */
1239 if (stream
->metadata_flag
) {
1240 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1247 /* Else, use the default set before which is the filesystem. */
1249 /* No streaming, we have to set the len with the full padding */
1255 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
1256 } while (ret
< 0 && errno
== EINTR
);
1257 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1259 PERROR("Error in file write");
1264 } else if (ret
> len
) {
1265 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1273 /* This call is useless on a socket so better save a syscall. */
1275 /* This won't block, but will start writeout asynchronously */
1276 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1277 SYNC_FILE_RANGE_WRITE
);
1278 stream
->out_fd_offset
+= ret
;
1282 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1285 /* Unlock only if ctrl socket used */
1286 if (relayd
&& stream
->metadata_flag
) {
1287 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1295 * Splice the data from the ring buffer to the tracefile.
1297 * Returns the number of bytes spliced.
1299 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1300 struct lttng_consumer_local_data
*ctx
,
1301 struct lttng_consumer_stream
*stream
, unsigned long len
,
1302 unsigned long padding
)
1304 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1306 off_t orig_offset
= stream
->out_fd_offset
;
1307 int fd
= stream
->wait_fd
;
1308 /* Default is on the disk */
1309 int outfd
= stream
->out_fd
;
1310 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1313 switch (consumer_data
.type
) {
1314 case LTTNG_CONSUMER_KERNEL
:
1316 case LTTNG_CONSUMER32_UST
:
1317 case LTTNG_CONSUMER64_UST
:
1318 /* Not supported for user space tracing */
1321 ERR("Unknown consumer_data type");
1325 /* RCU lock for the relayd pointer */
1328 /* Flag that the current stream if set for network streaming. */
1329 if (stream
->net_seq_idx
!= -1) {
1330 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1331 if (relayd
== NULL
) {
1337 * Choose right pipe for splice. Metadata and trace data are handled by
1338 * different threads hence the use of two pipes in order not to race or
1339 * corrupt the written data.
1341 if (stream
->metadata_flag
) {
1342 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1344 splice_pipe
= ctx
->consumer_thread_pipe
;
1347 /* Write metadata stream id before payload */
1349 int total_len
= len
;
1351 if (stream
->metadata_flag
) {
1353 * Lock the control socket for the complete duration of the function
1354 * since from this point on we will use the socket.
1356 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1358 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1365 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1368 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1370 /* Use the returned socket. */
1373 ERR("Remote relayd disconnected. Stopping");
1377 /* No streaming, we have to set the len with the full padding */
1382 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1383 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1384 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1385 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1386 DBG("splice chan to pipe, ret %zd", ret_splice
);
1387 if (ret_splice
< 0) {
1388 PERROR("Error in relay splice");
1390 written
= ret_splice
;
1396 /* Handle stream on the relayd if the output is on the network */
1398 if (stream
->metadata_flag
) {
1399 size_t metadata_payload_size
=
1400 sizeof(struct lttcomm_relayd_metadata_payload
);
1402 /* Update counter to fit the spliced data */
1403 ret_splice
+= metadata_payload_size
;
1404 len
+= metadata_payload_size
;
1406 * We do this so the return value can match the len passed as
1407 * argument to this function.
1409 written
-= metadata_payload_size
;
1413 /* Splice data out */
1414 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1415 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1416 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1417 if (ret_splice
< 0) {
1418 PERROR("Error in file splice");
1420 written
= ret_splice
;
1424 } else if (ret_splice
> len
) {
1426 PERROR("Wrote more data than requested %zd (len: %lu)",
1428 written
+= ret_splice
;
1434 /* This call is useless on a socket so better save a syscall. */
1436 /* This won't block, but will start writeout asynchronously */
1437 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1438 SYNC_FILE_RANGE_WRITE
);
1439 stream
->out_fd_offset
+= ret_splice
;
1441 written
+= ret_splice
;
1443 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1450 /* send the appropriate error description to sessiond */
1453 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EBADF
);
1456 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1459 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1462 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1467 if (relayd
&& stream
->metadata_flag
) {
1468 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1476 * Take a snapshot for a specific fd
1478 * Returns 0 on success, < 0 on error
1480 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1481 struct lttng_consumer_stream
*stream
)
1483 switch (consumer_data
.type
) {
1484 case LTTNG_CONSUMER_KERNEL
:
1485 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1486 case LTTNG_CONSUMER32_UST
:
1487 case LTTNG_CONSUMER64_UST
:
1488 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1490 ERR("Unknown consumer_data type");
1498 * Get the produced position
1500 * Returns 0 on success, < 0 on error
1502 int lttng_consumer_get_produced_snapshot(
1503 struct lttng_consumer_local_data
*ctx
,
1504 struct lttng_consumer_stream
*stream
,
1507 switch (consumer_data
.type
) {
1508 case LTTNG_CONSUMER_KERNEL
:
1509 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1510 case LTTNG_CONSUMER32_UST
:
1511 case LTTNG_CONSUMER64_UST
:
1512 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1514 ERR("Unknown consumer_data type");
1520 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1521 int sock
, struct pollfd
*consumer_sockpoll
)
1523 switch (consumer_data
.type
) {
1524 case LTTNG_CONSUMER_KERNEL
:
1525 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1526 case LTTNG_CONSUMER32_UST
:
1527 case LTTNG_CONSUMER64_UST
:
1528 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1530 ERR("Unknown consumer_data type");
1537 * Iterate over all streams of the hashtable and free them properly.
1539 * WARNING: *MUST* be used with data stream only.
1541 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1544 struct lttng_ht_iter iter
;
1545 struct lttng_consumer_stream
*stream
;
1552 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1553 ret
= lttng_ht_del(ht
, &iter
);
1556 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1560 lttng_ht_destroy(ht
);
1564 * Iterate over all streams of the hashtable and free them properly.
1566 * XXX: Should not be only for metadata stream or else use an other name.
1568 static void destroy_stream_ht(struct lttng_ht
*ht
)
1571 struct lttng_ht_iter iter
;
1572 struct lttng_consumer_stream
*stream
;
1579 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, waitfd_node
.node
) {
1580 ret
= lttng_ht_del(ht
, &iter
);
1583 call_rcu(&stream
->waitfd_node
.head
, consumer_free_metadata_stream
);
1587 lttng_ht_destroy(ht
);
1591 * Clean up a metadata stream and free its memory.
1593 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1594 struct lttng_ht
*ht
)
1597 struct lttng_ht_iter iter
;
1598 struct lttng_consumer_channel
*free_chan
= NULL
;
1599 struct consumer_relayd_sock_pair
*relayd
;
1603 * This call should NEVER receive regular stream. It must always be
1604 * metadata stream and this is crucial for data structure synchronization.
1606 assert(stream
->metadata_flag
);
1608 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1611 /* Means the stream was allocated but not successfully added */
1615 pthread_mutex_lock(&consumer_data
.lock
);
1616 switch (consumer_data
.type
) {
1617 case LTTNG_CONSUMER_KERNEL
:
1618 if (stream
->mmap_base
!= NULL
) {
1619 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1621 PERROR("munmap metadata stream");
1625 case LTTNG_CONSUMER32_UST
:
1626 case LTTNG_CONSUMER64_UST
:
1627 lttng_ustconsumer_del_stream(stream
);
1630 ERR("Unknown consumer_data type");
1636 iter
.iter
.node
= &stream
->waitfd_node
.node
;
1637 ret
= lttng_ht_del(ht
, &iter
);
1641 if (stream
->out_fd
>= 0) {
1642 ret
= close(stream
->out_fd
);
1648 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
1649 ret
= close(stream
->wait_fd
);
1655 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
1656 ret
= close(stream
->shm_fd
);
1662 /* Check and cleanup relayd */
1664 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1665 if (relayd
!= NULL
) {
1666 uatomic_dec(&relayd
->refcount
);
1667 assert(uatomic_read(&relayd
->refcount
) >= 0);
1669 /* Closing streams requires to lock the control socket. */
1670 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1671 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1672 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1673 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1675 DBG("Unable to close stream on the relayd. Continuing");
1677 * Continue here. There is nothing we can do for the relayd.
1678 * Chances are that the relayd has closed the socket so we just
1679 * continue cleaning up.
1683 /* Both conditions are met, we destroy the relayd. */
1684 if (uatomic_read(&relayd
->refcount
) == 0 &&
1685 uatomic_read(&relayd
->destroy_flag
)) {
1686 destroy_relayd(relayd
);
1691 /* Atomically decrement channel refcount since other threads can use it. */
1692 uatomic_dec(&stream
->chan
->refcount
);
1693 if (!uatomic_read(&stream
->chan
->refcount
)
1694 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
1695 /* Go for channel deletion! */
1696 free_chan
= stream
->chan
;
1700 pthread_mutex_unlock(&consumer_data
.lock
);
1703 consumer_del_channel(free_chan
);
1707 call_rcu(&stream
->waitfd_node
.head
, consumer_free_metadata_stream
);
1711 * Action done with the metadata stream when adding it to the consumer internal
1712 * data structures to handle it.
1714 static int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
,
1715 struct lttng_ht
*ht
)
1718 struct consumer_relayd_sock_pair
*relayd
;
1723 DBG3("Adding metadata stream %d to hash table", stream
->wait_fd
);
1725 pthread_mutex_lock(&consumer_data
.lock
);
1728 * From here, refcounts are updated so be _careful_ when returning an error
1733 /* Find relayd and, if one is found, increment refcount. */
1734 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1735 if (relayd
!= NULL
) {
1736 uatomic_inc(&relayd
->refcount
);
1739 /* Update channel refcount once added without error(s). */
1740 uatomic_inc(&stream
->chan
->refcount
);
1743 * When nb_init_streams reaches 0, we don't need to trigger any action in
1744 * terms of destroying the associated channel, because the action that
1745 * causes the count to become 0 also causes a stream to be added. The
1746 * channel deletion will thus be triggered by the following removal of this
1749 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
1750 uatomic_dec(&stream
->chan
->nb_init_streams
);
1753 /* Steal stream identifier to avoid having streams with the same key */
1754 consumer_steal_stream_key(stream
->key
, ht
);
1756 lttng_ht_add_unique_ulong(ht
, &stream
->waitfd_node
);
1759 pthread_mutex_unlock(&consumer_data
.lock
);
1764 * Thread polls on metadata file descriptor and write them on disk or on the
1767 void *consumer_thread_metadata_poll(void *data
)
1770 uint32_t revents
, nb_fd
;
1771 struct lttng_consumer_stream
*stream
= NULL
;
1772 struct lttng_ht_iter iter
;
1773 struct lttng_ht_node_ulong
*node
;
1774 struct lttng_poll_event events
;
1775 struct lttng_consumer_local_data
*ctx
= data
;
1778 rcu_register_thread();
1780 DBG("Thread metadata poll started");
1782 /* Size is set to 1 for the consumer_metadata pipe */
1783 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
1785 ERR("Poll set creation failed");
1789 ret
= lttng_poll_add(&events
, ctx
->consumer_metadata_pipe
[0], LPOLLIN
);
1795 DBG("Metadata main loop started");
1798 lttng_poll_reset(&events
);
1800 nb_fd
= LTTNG_POLL_GETNB(&events
);
1802 /* Only the metadata pipe is set */
1803 if (nb_fd
== 0 && consumer_quit
== 1) {
1808 DBG("Metadata poll wait with %d fd(s)", nb_fd
);
1809 ret
= lttng_poll_wait(&events
, -1);
1810 DBG("Metadata event catched in thread");
1812 if (errno
== EINTR
) {
1813 ERR("Poll EINTR catched");
1819 /* From here, the event is a metadata wait fd */
1820 for (i
= 0; i
< nb_fd
; i
++) {
1821 revents
= LTTNG_POLL_GETEV(&events
, i
);
1822 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1824 /* Just don't waste time if no returned events for the fd */
1829 if (pollfd
== ctx
->consumer_metadata_pipe
[0]) {
1830 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
1831 DBG("Metadata thread pipe hung up");
1833 * Remove the pipe from the poll set and continue the loop
1834 * since their might be data to consume.
1836 lttng_poll_del(&events
, ctx
->consumer_metadata_pipe
[0]);
1837 close(ctx
->consumer_metadata_pipe
[0]);
1839 } else if (revents
& LPOLLIN
) {
1841 /* Get the stream pointer received */
1842 ret
= read(pollfd
, &stream
, sizeof(stream
));
1843 } while (ret
< 0 && errno
== EINTR
);
1845 ret
< sizeof(struct lttng_consumer_stream
*)) {
1846 PERROR("read metadata stream");
1848 * Let's continue here and hope we can still work
1849 * without stopping the consumer. XXX: Should we?
1854 DBG("Adding metadata stream %d to poll set",
1857 ret
= consumer_add_metadata_stream(stream
, metadata_ht
);
1859 ERR("Unable to add metadata stream");
1860 /* Stream was not setup properly. Continuing. */
1861 consumer_del_metadata_stream(stream
, NULL
);
1865 /* Add metadata stream to the global poll events list */
1866 lttng_poll_add(&events
, stream
->wait_fd
,
1867 LPOLLIN
| LPOLLPRI
);
1870 /* Handle other stream */
1875 lttng_ht_lookup(metadata_ht
, (void *)((unsigned long) pollfd
),
1877 node
= lttng_ht_iter_get_node_ulong(&iter
);
1880 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
1883 /* Check for error event */
1884 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
1885 DBG("Metadata fd %d is hup|err.", pollfd
);
1886 if (!stream
->hangup_flush_done
1887 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1888 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1889 DBG("Attempting to flush and consume the UST buffers");
1890 lttng_ustconsumer_on_stream_hangup(stream
);
1892 /* We just flushed the stream now read it. */
1894 len
= ctx
->on_buffer_ready(stream
, ctx
);
1896 * We don't check the return value here since if we get
1897 * a negative len, it means an error occured thus we
1898 * simply remove it from the poll set and free the
1904 lttng_poll_del(&events
, stream
->wait_fd
);
1906 * This call update the channel states, closes file descriptors
1907 * and securely free the stream.
1909 consumer_del_metadata_stream(stream
, metadata_ht
);
1910 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
1911 /* Get the data out of the metadata file descriptor */
1912 DBG("Metadata available on fd %d", pollfd
);
1913 assert(stream
->wait_fd
== pollfd
);
1915 len
= ctx
->on_buffer_ready(stream
, ctx
);
1916 /* It's ok to have an unavailable sub-buffer */
1917 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
1920 } else if (len
> 0) {
1921 stream
->data_read
= 1;
1925 /* Release RCU lock for the stream looked up */
1932 DBG("Metadata poll thread exiting");
1933 lttng_poll_clean(&events
);
1936 destroy_stream_ht(metadata_ht
);
1939 rcu_unregister_thread();
1944 * This thread polls the fds in the set to consume the data and write
1945 * it to tracefile if necessary.
1947 void *consumer_thread_data_poll(void *data
)
1949 int num_rdy
, num_hup
, high_prio
, ret
, i
;
1950 struct pollfd
*pollfd
= NULL
;
1951 /* local view of the streams */
1952 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
1953 /* local view of consumer_data.fds_count */
1955 struct lttng_consumer_local_data
*ctx
= data
;
1958 rcu_register_thread();
1960 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1961 if (data_ht
== NULL
) {
1965 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
1972 * the fds set has been updated, we need to update our
1973 * local array as well
1975 pthread_mutex_lock(&consumer_data
.lock
);
1976 if (consumer_data
.need_update
) {
1977 if (pollfd
!= NULL
) {
1981 if (local_stream
!= NULL
) {
1983 local_stream
= NULL
;
1986 /* allocate for all fds + 1 for the consumer_poll_pipe */
1987 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
1988 if (pollfd
== NULL
) {
1989 PERROR("pollfd malloc");
1990 pthread_mutex_unlock(&consumer_data
.lock
);
1994 /* allocate for all fds + 1 for the consumer_poll_pipe */
1995 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
1996 sizeof(struct lttng_consumer_stream
));
1997 if (local_stream
== NULL
) {
1998 PERROR("local_stream malloc");
1999 pthread_mutex_unlock(&consumer_data
.lock
);
2002 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
,
2005 ERR("Error in allocating pollfd or local_outfds");
2006 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2007 pthread_mutex_unlock(&consumer_data
.lock
);
2011 consumer_data
.need_update
= 0;
2013 pthread_mutex_unlock(&consumer_data
.lock
);
2015 /* No FDs and consumer_quit, consumer_cleanup the thread */
2016 if (nb_fd
== 0 && consumer_quit
== 1) {
2019 /* poll on the array of fds */
2021 DBG("polling on %d fd", nb_fd
+ 1);
2022 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
2023 DBG("poll num_rdy : %d", num_rdy
);
2024 if (num_rdy
== -1) {
2026 * Restart interrupted system call.
2028 if (errno
== EINTR
) {
2031 PERROR("Poll error");
2032 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2034 } else if (num_rdy
== 0) {
2035 DBG("Polling thread timed out");
2040 * If the consumer_poll_pipe triggered poll go directly to the
2041 * beginning of the loop to update the array. We want to prioritize
2042 * array update over low-priority reads.
2044 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2045 size_t pipe_readlen
;
2047 DBG("consumer_poll_pipe wake up");
2048 /* Consume 1 byte of pipe data */
2050 pipe_readlen
= read(ctx
->consumer_poll_pipe
[0], &new_stream
,
2051 sizeof(new_stream
));
2052 } while (pipe_readlen
== -1 && errno
== EINTR
);
2055 * If the stream is NULL, just ignore it. It's also possible that
2056 * the sessiond poll thread changed the consumer_quit state and is
2057 * waking us up to test it.
2059 if (new_stream
== NULL
) {
2063 ret
= consumer_add_stream(new_stream
, data_ht
);
2065 ERR("Consumer add stream %d failed. Continuing",
2068 * At this point, if the add_stream fails, it is not in the
2069 * hash table thus passing the NULL value here.
2071 consumer_del_stream(new_stream
, NULL
);
2074 /* Continue to update the local streams and handle prio ones */
2078 /* Take care of high priority channels first. */
2079 for (i
= 0; i
< nb_fd
; i
++) {
2080 if (pollfd
[i
].revents
& POLLPRI
) {
2081 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2083 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2084 /* it's ok to have an unavailable sub-buffer */
2085 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2087 } else if (len
> 0) {
2088 local_stream
[i
]->data_read
= 1;
2094 * If we read high prio channel in this loop, try again
2095 * for more high prio data.
2101 /* Take care of low priority channels. */
2102 for (i
= 0; i
< nb_fd
; i
++) {
2103 if ((pollfd
[i
].revents
& POLLIN
) ||
2104 local_stream
[i
]->hangup_flush_done
) {
2105 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2106 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2107 /* it's ok to have an unavailable sub-buffer */
2108 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2110 } else if (len
> 0) {
2111 local_stream
[i
]->data_read
= 1;
2116 /* Handle hangup and errors */
2117 for (i
= 0; i
< nb_fd
; i
++) {
2118 if (!local_stream
[i
]->hangup_flush_done
2119 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2120 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2121 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2122 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2124 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2125 /* Attempt read again, for the data we just flushed. */
2126 local_stream
[i
]->data_read
= 1;
2129 * If the poll flag is HUP/ERR/NVAL and we have
2130 * read no data in this pass, we can remove the
2131 * stream from its hash table.
2133 if ((pollfd
[i
].revents
& POLLHUP
)) {
2134 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2135 if (!local_stream
[i
]->data_read
) {
2136 consumer_del_stream(local_stream
[i
], data_ht
);
2139 } else if (pollfd
[i
].revents
& POLLERR
) {
2140 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2141 if (!local_stream
[i
]->data_read
) {
2142 consumer_del_stream(local_stream
[i
], data_ht
);
2145 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2146 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2147 if (!local_stream
[i
]->data_read
) {
2148 consumer_del_stream(local_stream
[i
], data_ht
);
2152 local_stream
[i
]->data_read
= 0;
2156 DBG("polling thread exiting");
2157 if (pollfd
!= NULL
) {
2161 if (local_stream
!= NULL
) {
2163 local_stream
= NULL
;
2167 * Close the write side of the pipe so epoll_wait() in
2168 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2169 * read side of the pipe. If we close them both, epoll_wait strangely does
2170 * not return and could create a endless wait period if the pipe is the
2171 * only tracked fd in the poll set. The thread will take care of closing
2174 close(ctx
->consumer_metadata_pipe
[1]);
2177 destroy_data_stream_ht(data_ht
);
2180 rcu_unregister_thread();
2185 * This thread listens on the consumerd socket and receives the file
2186 * descriptors from the session daemon.
2188 void *consumer_thread_sessiond_poll(void *data
)
2190 int sock
, client_socket
, ret
;
2192 * structure to poll for incoming data on communication socket avoids
2193 * making blocking sockets.
2195 struct pollfd consumer_sockpoll
[2];
2196 struct lttng_consumer_local_data
*ctx
= data
;
2198 rcu_register_thread();
2200 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2201 unlink(ctx
->consumer_command_sock_path
);
2202 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2203 if (client_socket
< 0) {
2204 ERR("Cannot create command socket");
2208 ret
= lttcomm_listen_unix_sock(client_socket
);
2213 DBG("Sending ready command to lttng-sessiond");
2214 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2215 /* return < 0 on error, but == 0 is not fatal */
2217 ERR("Error sending ready command to lttng-sessiond");
2221 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2223 PERROR("fcntl O_NONBLOCK");
2227 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2228 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2229 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2230 consumer_sockpoll
[1].fd
= client_socket
;
2231 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2233 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2236 DBG("Connection on client_socket");
2238 /* Blocking call, waiting for transmission */
2239 sock
= lttcomm_accept_unix_sock(client_socket
);
2244 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2246 PERROR("fcntl O_NONBLOCK");
2250 /* update the polling structure to poll on the established socket */
2251 consumer_sockpoll
[1].fd
= sock
;
2252 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2255 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2258 DBG("Incoming command on sock");
2259 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2260 if (ret
== -ENOENT
) {
2261 DBG("Received STOP command");
2266 * This could simply be a session daemon quitting. Don't output
2269 DBG("Communication interrupted on command socket");
2272 if (consumer_quit
) {
2273 DBG("consumer_thread_receive_fds received quit from signal");
2276 DBG("received fds on sock");
2279 DBG("consumer_thread_receive_fds exiting");
2282 * when all fds have hung up, the polling thread
2288 * 2s of grace period, if no polling events occur during
2289 * this period, the polling thread will exit even if there
2290 * are still open FDs (should not happen, but safety mechanism).
2292 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
2295 * Notify the data poll thread to poll back again and test the
2296 * consumer_quit state to quit gracefully.
2299 struct lttng_consumer_stream
*null_stream
= NULL
;
2301 ret
= write(ctx
->consumer_poll_pipe
[1], &null_stream
,
2302 sizeof(null_stream
));
2303 } while (ret
< 0 && errno
== EINTR
);
2305 rcu_unregister_thread();
2309 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2310 struct lttng_consumer_local_data
*ctx
)
2312 switch (consumer_data
.type
) {
2313 case LTTNG_CONSUMER_KERNEL
:
2314 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
2315 case LTTNG_CONSUMER32_UST
:
2316 case LTTNG_CONSUMER64_UST
:
2317 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2319 ERR("Unknown consumer_data type");
2325 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2327 switch (consumer_data
.type
) {
2328 case LTTNG_CONSUMER_KERNEL
:
2329 return lttng_kconsumer_on_recv_stream(stream
);
2330 case LTTNG_CONSUMER32_UST
:
2331 case LTTNG_CONSUMER64_UST
:
2332 return lttng_ustconsumer_on_recv_stream(stream
);
2334 ERR("Unknown consumer_data type");
2341 * Allocate and set consumer data hash tables.
2343 void lttng_consumer_init(void)
2345 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2346 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2347 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2349 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2350 assert(metadata_ht
);
2351 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2356 * Process the ADD_RELAYD command receive by a consumer.
2358 * This will create a relayd socket pair and add it to the relayd hash table.
2359 * The caller MUST acquire a RCU read side lock before calling it.
2361 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
2362 struct lttng_consumer_local_data
*ctx
, int sock
,
2363 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
)
2366 struct consumer_relayd_sock_pair
*relayd
;
2368 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
2370 /* Get relayd reference if exists. */
2371 relayd
= consumer_find_relayd(net_seq_idx
);
2372 if (relayd
== NULL
) {
2373 /* Not found. Allocate one. */
2374 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
2375 if (relayd
== NULL
) {
2376 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
2381 /* Poll on consumer socket. */
2382 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2387 /* Get relayd socket from session daemon */
2388 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2389 if (ret
!= sizeof(fd
)) {
2390 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
2395 /* Copy socket information and received FD */
2396 switch (sock_type
) {
2397 case LTTNG_STREAM_CONTROL
:
2398 /* Copy received lttcomm socket */
2399 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
2400 ret
= lttcomm_create_sock(&relayd
->control_sock
);
2405 /* Close the created socket fd which is useless */
2406 close(relayd
->control_sock
.fd
);
2408 /* Assign new file descriptor */
2409 relayd
->control_sock
.fd
= fd
;
2411 case LTTNG_STREAM_DATA
:
2412 /* Copy received lttcomm socket */
2413 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
2414 ret
= lttcomm_create_sock(&relayd
->data_sock
);
2419 /* Close the created socket fd which is useless */
2420 close(relayd
->data_sock
.fd
);
2422 /* Assign new file descriptor */
2423 relayd
->data_sock
.fd
= fd
;
2426 ERR("Unknown relayd socket type (%d)", sock_type
);
2430 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2431 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
2432 relayd
->net_seq_idx
, fd
);
2435 * Add relayd socket pair to consumer data hashtable. If object already
2436 * exists or on error, the function gracefully returns.