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 * Find a stream. The consumer_data.lock must be locked during this
65 static struct lttng_consumer_stream
*consumer_find_stream(int key
,
68 struct lttng_ht_iter iter
;
69 struct lttng_ht_node_ulong
*node
;
70 struct lttng_consumer_stream
*stream
= NULL
;
74 /* Negative keys are lookup failures */
81 lttng_ht_lookup(ht
, (void *)((unsigned long) key
), &iter
);
82 node
= lttng_ht_iter_get_node_ulong(&iter
);
84 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
92 static void consumer_steal_stream_key(int key
, struct lttng_ht
*ht
)
94 struct lttng_consumer_stream
*stream
;
97 stream
= consumer_find_stream(key
, ht
);
101 * We don't want the lookup to match, but we still need
102 * to iterate on this stream when iterating over the hash table. Just
103 * change the node key.
105 stream
->node
.key
= -1;
110 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
112 struct lttng_ht_iter iter
;
113 struct lttng_ht_node_ulong
*node
;
114 struct lttng_consumer_channel
*channel
= NULL
;
116 /* Negative keys are lookup failures */
123 lttng_ht_lookup(consumer_data
.channel_ht
, (void *)((unsigned long) key
),
125 node
= lttng_ht_iter_get_node_ulong(&iter
);
127 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
135 static void consumer_steal_channel_key(int key
)
137 struct lttng_consumer_channel
*channel
;
140 channel
= consumer_find_channel(key
);
144 * We don't want the lookup to match, but we still need
145 * to iterate on this channel when iterating over the hash table. Just
146 * change the node key.
148 channel
->node
.key
= -1;
154 void consumer_free_stream(struct rcu_head
*head
)
156 struct lttng_ht_node_ulong
*node
=
157 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
158 struct lttng_consumer_stream
*stream
=
159 caa_container_of(node
, struct lttng_consumer_stream
, node
);
165 void consumer_free_metadata_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
, waitfd_node
);
176 * RCU protected relayd socket pair free.
178 static void consumer_rcu_free_relayd(struct rcu_head
*head
)
180 struct lttng_ht_node_ulong
*node
=
181 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
182 struct consumer_relayd_sock_pair
*relayd
=
183 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
189 * Destroy and free relayd socket pair object.
191 * This function MUST be called with the consumer_data lock acquired.
193 static void destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
196 struct lttng_ht_iter iter
;
198 if (relayd
== NULL
) {
202 DBG("Consumer destroy and close relayd socket pair");
204 iter
.iter
.node
= &relayd
->node
.node
;
205 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
207 /* We assume the relayd was already destroyed */
211 /* Close all sockets */
212 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
213 (void) relayd_close(&relayd
->control_sock
);
214 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
215 (void) relayd_close(&relayd
->data_sock
);
217 /* RCU free() call */
218 call_rcu(&relayd
->node
.head
, consumer_rcu_free_relayd
);
222 * Flag a relayd socket pair for destruction. Destroy it if the refcount
225 * RCU read side lock MUST be aquired before calling this function.
227 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
231 /* Set destroy flag for this object */
232 uatomic_set(&relayd
->destroy_flag
, 1);
234 /* Destroy the relayd if refcount is 0 */
235 if (uatomic_read(&relayd
->refcount
) == 0) {
236 destroy_relayd(relayd
);
241 * Remove a stream from the global list protected by a mutex. This
242 * function is also responsible for freeing its data structures.
244 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
248 struct lttng_ht_iter iter
;
249 struct lttng_consumer_channel
*free_chan
= NULL
;
250 struct consumer_relayd_sock_pair
*relayd
;
255 /* Means the stream was allocated but not successfully added */
259 pthread_mutex_lock(&consumer_data
.lock
);
261 switch (consumer_data
.type
) {
262 case LTTNG_CONSUMER_KERNEL
:
263 if (stream
->mmap_base
!= NULL
) {
264 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
270 case LTTNG_CONSUMER32_UST
:
271 case LTTNG_CONSUMER64_UST
:
272 lttng_ustconsumer_del_stream(stream
);
275 ERR("Unknown consumer_data type");
281 iter
.iter
.node
= &stream
->node
.node
;
282 ret
= lttng_ht_del(ht
, &iter
);
287 if (consumer_data
.stream_count
<= 0) {
290 consumer_data
.stream_count
--;
294 if (stream
->out_fd
>= 0) {
295 ret
= close(stream
->out_fd
);
300 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
301 ret
= close(stream
->wait_fd
);
306 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
307 ret
= close(stream
->shm_fd
);
313 /* Check and cleanup relayd */
315 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
316 if (relayd
!= NULL
) {
317 uatomic_dec(&relayd
->refcount
);
318 assert(uatomic_read(&relayd
->refcount
) >= 0);
320 /* Closing streams requires to lock the control socket. */
321 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
322 ret
= relayd_send_close_stream(&relayd
->control_sock
,
323 stream
->relayd_stream_id
,
324 stream
->next_net_seq_num
- 1);
325 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
327 DBG("Unable to close stream on the relayd. Continuing");
329 * Continue here. There is nothing we can do for the relayd.
330 * Chances are that the relayd has closed the socket so we just
331 * continue cleaning up.
335 /* Both conditions are met, we destroy the relayd. */
336 if (uatomic_read(&relayd
->refcount
) == 0 &&
337 uatomic_read(&relayd
->destroy_flag
)) {
338 destroy_relayd(relayd
);
343 uatomic_dec(&stream
->chan
->refcount
);
344 if (!uatomic_read(&stream
->chan
->refcount
)
345 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
346 free_chan
= stream
->chan
;
350 consumer_data
.need_update
= 1;
351 pthread_mutex_unlock(&consumer_data
.lock
);
354 consumer_del_channel(free_chan
);
358 call_rcu(&stream
->node
.head
, consumer_free_stream
);
361 struct lttng_consumer_stream
*consumer_allocate_stream(
362 int channel_key
, int stream_key
,
363 int shm_fd
, int wait_fd
,
364 enum lttng_consumer_stream_state state
,
366 enum lttng_event_output output
,
367 const char *path_name
,
374 struct lttng_consumer_stream
*stream
;
376 stream
= zmalloc(sizeof(*stream
));
377 if (stream
== NULL
) {
378 PERROR("malloc struct lttng_consumer_stream");
379 *alloc_ret
= -ENOMEM
;
384 * Get stream's channel reference. Needed when adding the stream to the
387 stream
->chan
= consumer_find_channel(channel_key
);
389 *alloc_ret
= -ENOENT
;
390 ERR("Unable to find channel for stream %d", stream_key
);
394 stream
->key
= stream_key
;
395 stream
->shm_fd
= shm_fd
;
396 stream
->wait_fd
= wait_fd
;
398 stream
->out_fd_offset
= 0;
399 stream
->state
= state
;
400 stream
->mmap_len
= mmap_len
;
401 stream
->mmap_base
= NULL
;
402 stream
->output
= output
;
405 stream
->net_seq_idx
= net_index
;
406 stream
->metadata_flag
= metadata_flag
;
407 strncpy(stream
->path_name
, path_name
, sizeof(stream
->path_name
));
408 stream
->path_name
[sizeof(stream
->path_name
) - 1] = '\0';
409 lttng_ht_node_init_ulong(&stream
->waitfd_node
, stream
->wait_fd
);
410 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
412 DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu,"
413 " out_fd %d, net_seq_idx %d)", stream
->path_name
, stream
->key
,
414 stream
->shm_fd
, stream
->wait_fd
,
415 (unsigned long long) stream
->mmap_len
, stream
->out_fd
,
416 stream
->net_seq_idx
);
426 * Add a stream to the global list protected by a mutex.
428 int consumer_add_stream(struct lttng_consumer_stream
*stream
)
431 struct consumer_relayd_sock_pair
*relayd
;
435 DBG3("Adding consumer stream %d", stream
->key
);
437 pthread_mutex_lock(&consumer_data
.lock
);
440 switch (consumer_data
.type
) {
441 case LTTNG_CONSUMER_KERNEL
:
443 case LTTNG_CONSUMER32_UST
:
444 case LTTNG_CONSUMER64_UST
:
445 stream
->cpu
= stream
->chan
->cpucount
++;
446 ret
= lttng_ustconsumer_add_stream(stream
);
452 /* Steal stream identifier only for UST */
453 consumer_steal_stream_key(stream
->key
, consumer_data
.stream_ht
);
456 ERR("Unknown consumer_data type");
462 lttng_ht_add_unique_ulong(consumer_data
.stream_ht
, &stream
->node
);
464 /* Check and cleanup relayd */
465 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
466 if (relayd
!= NULL
) {
467 uatomic_inc(&relayd
->refcount
);
470 /* Update channel refcount once added without error(s). */
471 uatomic_inc(&stream
->chan
->refcount
);
474 * When nb_init_streams reaches 0, we don't need to trigger any action in
475 * terms of destroying the associated channel, because the action that
476 * causes the count to become 0 also causes a stream to be added. The
477 * channel deletion will thus be triggered by the following removal of this
480 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
481 uatomic_dec(&stream
->chan
->nb_init_streams
);
484 /* Update consumer data once the node is inserted. */
485 consumer_data
.stream_count
++;
486 consumer_data
.need_update
= 1;
490 pthread_mutex_unlock(&consumer_data
.lock
);
496 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
497 * be acquired before calling this.
499 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
502 struct lttng_ht_node_ulong
*node
;
503 struct lttng_ht_iter iter
;
505 if (relayd
== NULL
) {
510 lttng_ht_lookup(consumer_data
.relayd_ht
,
511 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
512 node
= lttng_ht_iter_get_node_ulong(&iter
);
514 /* Relayd already exist. Ignore the insertion */
517 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
524 * Allocate and return a consumer relayd socket.
526 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
529 struct consumer_relayd_sock_pair
*obj
= NULL
;
531 /* Negative net sequence index is a failure */
532 if (net_seq_idx
< 0) {
536 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
538 PERROR("zmalloc relayd sock");
542 obj
->net_seq_idx
= net_seq_idx
;
544 obj
->destroy_flag
= 0;
545 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
546 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
553 * Find a relayd socket pair in the global consumer data.
555 * Return the object if found else NULL.
556 * RCU read-side lock must be held across this call and while using the
559 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
561 struct lttng_ht_iter iter
;
562 struct lttng_ht_node_ulong
*node
;
563 struct consumer_relayd_sock_pair
*relayd
= NULL
;
565 /* Negative keys are lookup failures */
570 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
572 node
= lttng_ht_iter_get_node_ulong(&iter
);
574 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
582 * Handle stream for relayd transmission if the stream applies for network
583 * streaming where the net sequence index is set.
585 * Return destination file descriptor or negative value on error.
587 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
588 size_t data_size
, unsigned long padding
,
589 struct consumer_relayd_sock_pair
*relayd
)
592 struct lttcomm_relayd_data_hdr data_hdr
;
598 /* Reset data header */
599 memset(&data_hdr
, 0, sizeof(data_hdr
));
601 if (stream
->metadata_flag
) {
602 /* Caller MUST acquire the relayd control socket lock */
603 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
608 /* Metadata are always sent on the control socket. */
609 outfd
= relayd
->control_sock
.fd
;
611 /* Set header with stream information */
612 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
613 data_hdr
.data_size
= htobe32(data_size
);
614 data_hdr
.padding_size
= htobe32(padding
);
615 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
616 /* Other fields are zeroed previously */
618 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
624 /* Set to go on data socket */
625 outfd
= relayd
->data_sock
.fd
;
633 * Update a stream according to what we just received.
635 void consumer_change_stream_state(int stream_key
,
636 enum lttng_consumer_stream_state state
)
638 struct lttng_consumer_stream
*stream
;
640 pthread_mutex_lock(&consumer_data
.lock
);
641 stream
= consumer_find_stream(stream_key
, consumer_data
.stream_ht
);
643 stream
->state
= state
;
645 consumer_data
.need_update
= 1;
646 pthread_mutex_unlock(&consumer_data
.lock
);
650 void consumer_free_channel(struct rcu_head
*head
)
652 struct lttng_ht_node_ulong
*node
=
653 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
654 struct lttng_consumer_channel
*channel
=
655 caa_container_of(node
, struct lttng_consumer_channel
, node
);
661 * Remove a channel from the global list protected by a mutex. This
662 * function is also responsible for freeing its data structures.
664 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
667 struct lttng_ht_iter iter
;
669 pthread_mutex_lock(&consumer_data
.lock
);
671 switch (consumer_data
.type
) {
672 case LTTNG_CONSUMER_KERNEL
:
674 case LTTNG_CONSUMER32_UST
:
675 case LTTNG_CONSUMER64_UST
:
676 lttng_ustconsumer_del_channel(channel
);
679 ERR("Unknown consumer_data type");
685 iter
.iter
.node
= &channel
->node
.node
;
686 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
690 if (channel
->mmap_base
!= NULL
) {
691 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
696 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
697 ret
= close(channel
->wait_fd
);
702 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
703 ret
= close(channel
->shm_fd
);
709 call_rcu(&channel
->node
.head
, consumer_free_channel
);
711 pthread_mutex_unlock(&consumer_data
.lock
);
714 struct lttng_consumer_channel
*consumer_allocate_channel(
716 int shm_fd
, int wait_fd
,
718 uint64_t max_sb_size
,
719 unsigned int nb_init_streams
)
721 struct lttng_consumer_channel
*channel
;
724 channel
= zmalloc(sizeof(*channel
));
725 if (channel
== NULL
) {
726 PERROR("malloc struct lttng_consumer_channel");
729 channel
->key
= channel_key
;
730 channel
->shm_fd
= shm_fd
;
731 channel
->wait_fd
= wait_fd
;
732 channel
->mmap_len
= mmap_len
;
733 channel
->max_sb_size
= max_sb_size
;
734 channel
->refcount
= 0;
735 channel
->nb_init_streams
= nb_init_streams
;
736 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
738 switch (consumer_data
.type
) {
739 case LTTNG_CONSUMER_KERNEL
:
740 channel
->mmap_base
= NULL
;
741 channel
->mmap_len
= 0;
743 case LTTNG_CONSUMER32_UST
:
744 case LTTNG_CONSUMER64_UST
:
745 ret
= lttng_ustconsumer_allocate_channel(channel
);
752 ERR("Unknown consumer_data type");
756 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
757 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
758 (unsigned long long) channel
->mmap_len
,
759 (unsigned long long) channel
->max_sb_size
);
765 * Add a channel to the global list protected by a mutex.
767 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
769 struct lttng_ht_node_ulong
*node
;
770 struct lttng_ht_iter iter
;
772 pthread_mutex_lock(&consumer_data
.lock
);
773 /* Steal channel identifier, for UST */
774 consumer_steal_channel_key(channel
->key
);
777 lttng_ht_lookup(consumer_data
.channel_ht
,
778 (void *)((unsigned long) channel
->key
), &iter
);
779 node
= lttng_ht_iter_get_node_ulong(&iter
);
781 /* Channel already exist. Ignore the insertion */
785 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
789 pthread_mutex_unlock(&consumer_data
.lock
);
795 * Allocate the pollfd structure and the local view of the out fds to avoid
796 * doing a lookup in the linked list and concurrency issues when writing is
797 * needed. Called with consumer_data.lock held.
799 * Returns the number of fds in the structures.
801 int consumer_update_poll_array(
802 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
803 struct lttng_consumer_stream
**local_stream
)
806 struct lttng_ht_iter iter
;
807 struct lttng_consumer_stream
*stream
;
809 DBG("Updating poll fd array");
811 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, stream
,
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 * XXX: Should not be only for metadata stream or else use an other name.
1541 static void destroy_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
, waitfd_node
.node
) {
1553 ret
= lttng_ht_del(ht
, &iter
);
1556 call_rcu(&stream
->waitfd_node
.head
, consumer_free_metadata_stream
);
1560 lttng_ht_destroy(ht
);
1564 * Clean up a metadata stream and free its memory.
1566 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1567 struct lttng_ht
*ht
)
1570 struct lttng_ht_iter iter
;
1571 struct lttng_consumer_channel
*free_chan
= NULL
;
1572 struct consumer_relayd_sock_pair
*relayd
;
1576 * This call should NEVER receive regular stream. It must always be
1577 * metadata stream and this is crucial for data structure synchronization.
1579 assert(stream
->metadata_flag
);
1581 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1584 /* Means the stream was allocated but not successfully added */
1589 iter
.iter
.node
= &stream
->waitfd_node
.node
;
1590 ret
= lttng_ht_del(ht
, &iter
);
1594 pthread_mutex_lock(&consumer_data
.lock
);
1595 switch (consumer_data
.type
) {
1596 case LTTNG_CONSUMER_KERNEL
:
1597 if (stream
->mmap_base
!= NULL
) {
1598 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1600 PERROR("munmap metadata stream");
1604 case LTTNG_CONSUMER32_UST
:
1605 case LTTNG_CONSUMER64_UST
:
1606 lttng_ustconsumer_del_stream(stream
);
1609 ERR("Unknown consumer_data type");
1614 if (stream
->out_fd
>= 0) {
1615 ret
= close(stream
->out_fd
);
1621 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
1622 ret
= close(stream
->wait_fd
);
1628 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
1629 ret
= close(stream
->shm_fd
);
1635 /* Check and cleanup relayd */
1637 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1638 if (relayd
!= NULL
) {
1639 uatomic_dec(&relayd
->refcount
);
1640 assert(uatomic_read(&relayd
->refcount
) >= 0);
1642 /* Closing streams requires to lock the control socket. */
1643 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1644 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1645 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1646 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1648 DBG("Unable to close stream on the relayd. Continuing");
1650 * Continue here. There is nothing we can do for the relayd.
1651 * Chances are that the relayd has closed the socket so we just
1652 * continue cleaning up.
1656 /* Both conditions are met, we destroy the relayd. */
1657 if (uatomic_read(&relayd
->refcount
) == 0 &&
1658 uatomic_read(&relayd
->destroy_flag
)) {
1659 destroy_relayd(relayd
);
1664 /* Atomically decrement channel refcount since other threads can use it. */
1665 uatomic_dec(&stream
->chan
->refcount
);
1666 if (!uatomic_read(&stream
->chan
->refcount
)
1667 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
1668 /* Go for channel deletion! */
1669 free_chan
= stream
->chan
;
1673 pthread_mutex_unlock(&consumer_data
.lock
);
1676 consumer_del_channel(free_chan
);
1680 call_rcu(&stream
->waitfd_node
.head
, consumer_free_metadata_stream
);
1684 * Action done with the metadata stream when adding it to the consumer internal
1685 * data structures to handle it.
1687 static int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
,
1688 struct lttng_ht
*ht
)
1691 struct consumer_relayd_sock_pair
*relayd
;
1696 DBG3("Adding metadata stream %d to hash table", stream
->wait_fd
);
1698 pthread_mutex_lock(&consumer_data
.lock
);
1700 switch (consumer_data
.type
) {
1701 case LTTNG_CONSUMER_KERNEL
:
1703 case LTTNG_CONSUMER32_UST
:
1704 case LTTNG_CONSUMER64_UST
:
1705 ret
= lttng_ustconsumer_add_stream(stream
);
1711 /* Steal stream identifier only for UST */
1712 consumer_steal_stream_key(stream
->wait_fd
, ht
);
1715 ERR("Unknown consumer_data type");
1722 * From here, refcounts are updated so be _careful_ when returning an error
1727 /* Find relayd and, if one is found, increment refcount. */
1728 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1729 if (relayd
!= NULL
) {
1730 uatomic_inc(&relayd
->refcount
);
1733 /* Update channel refcount once added without error(s). */
1734 uatomic_inc(&stream
->chan
->refcount
);
1737 * When nb_init_streams reaches 0, we don't need to trigger any action in
1738 * terms of destroying the associated channel, because the action that
1739 * causes the count to become 0 also causes a stream to be added. The
1740 * channel deletion will thus be triggered by the following removal of this
1743 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
1744 uatomic_dec(&stream
->chan
->nb_init_streams
);
1747 lttng_ht_add_unique_ulong(ht
, &stream
->waitfd_node
);
1751 pthread_mutex_unlock(&consumer_data
.lock
);
1756 * Thread polls on metadata file descriptor and write them on disk or on the
1759 void *lttng_consumer_thread_poll_metadata(void *data
)
1762 uint32_t revents
, nb_fd
;
1763 struct lttng_consumer_stream
*stream
= NULL
;
1764 struct lttng_ht_iter iter
;
1765 struct lttng_ht_node_ulong
*node
;
1766 struct lttng_ht
*metadata_ht
= NULL
;
1767 struct lttng_poll_event events
;
1768 struct lttng_consumer_local_data
*ctx
= data
;
1771 rcu_register_thread();
1773 DBG("Thread metadata poll started");
1775 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1776 if (metadata_ht
== NULL
) {
1780 /* Size is set to 1 for the consumer_metadata pipe */
1781 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
1783 ERR("Poll set creation failed");
1787 ret
= lttng_poll_add(&events
, ctx
->consumer_metadata_pipe
[0], LPOLLIN
);
1793 DBG("Metadata main loop started");
1796 lttng_poll_reset(&events
);
1798 nb_fd
= LTTNG_POLL_GETNB(&events
);
1800 /* Only the metadata pipe is set */
1801 if (nb_fd
== 0 && consumer_quit
== 1) {
1806 DBG("Metadata poll wait with %d fd(s)", nb_fd
);
1807 ret
= lttng_poll_wait(&events
, -1);
1808 DBG("Metadata event catched in thread");
1810 if (errno
== EINTR
) {
1811 ERR("Poll EINTR catched");
1817 /* From here, the event is a metadata wait fd */
1818 for (i
= 0; i
< nb_fd
; i
++) {
1819 revents
= LTTNG_POLL_GETEV(&events
, i
);
1820 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1822 /* Just don't waste time if no returned events for the fd */
1827 if (pollfd
== ctx
->consumer_metadata_pipe
[0]) {
1828 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
1829 DBG("Metadata thread pipe hung up");
1831 * Remove the pipe from the poll set and continue the loop
1832 * since their might be data to consume.
1834 lttng_poll_del(&events
, ctx
->consumer_metadata_pipe
[0]);
1835 close(ctx
->consumer_metadata_pipe
[0]);
1837 } else if (revents
& LPOLLIN
) {
1839 /* Get the stream pointer received */
1840 ret
= read(pollfd
, &stream
, sizeof(stream
));
1841 } while (ret
< 0 && errno
== EINTR
);
1843 ret
< sizeof(struct lttng_consumer_stream
*)) {
1844 PERROR("read metadata stream");
1846 * Let's continue here and hope we can still work
1847 * without stopping the consumer. XXX: Should we?
1852 DBG("Adding metadata stream %d to poll set",
1855 ret
= consumer_add_metadata_stream(stream
, metadata_ht
);
1857 ERR("Unable to add metadata stream");
1858 /* Stream was not setup properly. Continuing. */
1859 consumer_del_metadata_stream(stream
, NULL
);
1863 /* Add metadata stream to the global poll events list */
1864 lttng_poll_add(&events
, stream
->wait_fd
,
1865 LPOLLIN
| LPOLLPRI
);
1868 /* Handle other stream */
1873 lttng_ht_lookup(metadata_ht
, (void *)((unsigned long) pollfd
),
1875 node
= lttng_ht_iter_get_node_ulong(&iter
);
1878 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
1881 /* Check for error event */
1882 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
1883 DBG("Metadata fd %d is hup|err.", pollfd
);
1884 if (!stream
->hangup_flush_done
1885 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1886 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1887 DBG("Attempting to flush and consume the UST buffers");
1888 lttng_ustconsumer_on_stream_hangup(stream
);
1890 /* We just flushed the stream now read it. */
1892 len
= ctx
->on_buffer_ready(stream
, ctx
);
1894 * We don't check the return value here since if we get
1895 * a negative len, it means an error occured thus we
1896 * simply remove it from the poll set and free the
1902 lttng_poll_del(&events
, stream
->wait_fd
);
1904 * This call update the channel states, closes file descriptors
1905 * and securely free the stream.
1907 consumer_del_metadata_stream(stream
, metadata_ht
);
1908 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
1909 /* Get the data out of the metadata file descriptor */
1910 DBG("Metadata available on fd %d", pollfd
);
1911 assert(stream
->wait_fd
== pollfd
);
1913 len
= ctx
->on_buffer_ready(stream
, ctx
);
1914 /* It's ok to have an unavailable sub-buffer */
1915 if (len
< 0 && len
!= -EAGAIN
) {
1918 } else if (len
> 0) {
1919 stream
->data_read
= 1;
1923 /* Release RCU lock for the stream looked up */
1930 DBG("Metadata poll thread exiting");
1931 lttng_poll_clean(&events
);
1934 destroy_stream_ht(metadata_ht
);
1937 rcu_unregister_thread();
1942 * This thread polls the fds in the set to consume the data and write
1943 * it to tracefile if necessary.
1945 void *lttng_consumer_thread_poll_fds(void *data
)
1947 int num_rdy
, num_hup
, high_prio
, ret
, i
;
1948 struct pollfd
*pollfd
= NULL
;
1949 /* local view of the streams */
1950 struct lttng_consumer_stream
**local_stream
= NULL
;
1951 /* local view of consumer_data.fds_count */
1953 struct lttng_consumer_local_data
*ctx
= data
;
1955 pthread_t metadata_thread
;
1958 rcu_register_thread();
1960 /* Start metadata polling thread */
1961 ret
= pthread_create(&metadata_thread
, NULL
,
1962 lttng_consumer_thread_poll_metadata
, (void *) ctx
);
1964 PERROR("pthread_create metadata thread");
1968 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
1975 * the fds set has been updated, we need to update our
1976 * local array as well
1978 pthread_mutex_lock(&consumer_data
.lock
);
1979 if (consumer_data
.need_update
) {
1980 if (pollfd
!= NULL
) {
1984 if (local_stream
!= NULL
) {
1986 local_stream
= NULL
;
1989 /* allocate for all fds + 1 for the consumer_poll_pipe */
1990 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
1991 if (pollfd
== NULL
) {
1992 PERROR("pollfd malloc");
1993 pthread_mutex_unlock(&consumer_data
.lock
);
1997 /* allocate for all fds + 1 for the consumer_poll_pipe */
1998 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
1999 sizeof(struct lttng_consumer_stream
));
2000 if (local_stream
== NULL
) {
2001 PERROR("local_stream malloc");
2002 pthread_mutex_unlock(&consumer_data
.lock
);
2005 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
);
2007 ERR("Error in allocating pollfd or local_outfds");
2008 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2009 pthread_mutex_unlock(&consumer_data
.lock
);
2013 consumer_data
.need_update
= 0;
2015 pthread_mutex_unlock(&consumer_data
.lock
);
2017 /* No FDs and consumer_quit, consumer_cleanup the thread */
2018 if (nb_fd
== 0 && consumer_quit
== 1) {
2021 /* poll on the array of fds */
2023 DBG("polling on %d fd", nb_fd
+ 1);
2024 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
2025 DBG("poll num_rdy : %d", num_rdy
);
2026 if (num_rdy
== -1) {
2028 * Restart interrupted system call.
2030 if (errno
== EINTR
) {
2033 PERROR("Poll error");
2034 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2036 } else if (num_rdy
== 0) {
2037 DBG("Polling thread timed out");
2042 * If the consumer_poll_pipe triggered poll go directly to the
2043 * beginning of the loop to update the array. We want to prioritize
2044 * array update over low-priority reads.
2046 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2047 size_t pipe_readlen
;
2050 DBG("consumer_poll_pipe wake up");
2051 /* Consume 1 byte of pipe data */
2053 pipe_readlen
= read(ctx
->consumer_poll_pipe
[0], &tmp
, 1);
2054 } while (pipe_readlen
== -1 && errno
== EINTR
);
2058 /* Take care of high priority channels first. */
2059 for (i
= 0; i
< nb_fd
; i
++) {
2060 if (pollfd
[i
].revents
& POLLPRI
) {
2061 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2063 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2064 /* it's ok to have an unavailable sub-buffer */
2065 if (len
< 0 && len
!= -EAGAIN
) {
2067 } else if (len
> 0) {
2068 local_stream
[i
]->data_read
= 1;
2074 * If we read high prio channel in this loop, try again
2075 * for more high prio data.
2081 /* Take care of low priority channels. */
2082 for (i
= 0; i
< nb_fd
; i
++) {
2083 if ((pollfd
[i
].revents
& POLLIN
) ||
2084 local_stream
[i
]->hangup_flush_done
) {
2085 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2086 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2087 /* it's ok to have an unavailable sub-buffer */
2088 if (len
< 0 && len
!= -EAGAIN
) {
2090 } else if (len
> 0) {
2091 local_stream
[i
]->data_read
= 1;
2096 /* Handle hangup and errors */
2097 for (i
= 0; i
< nb_fd
; i
++) {
2098 if (!local_stream
[i
]->hangup_flush_done
2099 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2100 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2101 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2102 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2104 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2105 /* Attempt read again, for the data we just flushed. */
2106 local_stream
[i
]->data_read
= 1;
2109 * If the poll flag is HUP/ERR/NVAL and we have
2110 * read no data in this pass, we can remove the
2111 * stream from its hash table.
2113 if ((pollfd
[i
].revents
& POLLHUP
)) {
2114 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2115 if (!local_stream
[i
]->data_read
) {
2116 consumer_del_stream(local_stream
[i
],
2117 consumer_data
.stream_ht
);
2120 } else if (pollfd
[i
].revents
& POLLERR
) {
2121 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2122 if (!local_stream
[i
]->data_read
) {
2123 consumer_del_stream(local_stream
[i
],
2124 consumer_data
.stream_ht
);
2127 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2128 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2129 if (!local_stream
[i
]->data_read
) {
2130 consumer_del_stream(local_stream
[i
],
2131 consumer_data
.stream_ht
);
2135 local_stream
[i
]->data_read
= 0;
2139 DBG("polling thread exiting");
2140 if (pollfd
!= NULL
) {
2144 if (local_stream
!= NULL
) {
2146 local_stream
= NULL
;
2150 * Close the write side of the pipe so epoll_wait() in
2151 * lttng_consumer_thread_poll_metadata can catch it. The thread is
2152 * monitoring the read side of the pipe. If we close them both, epoll_wait
2153 * strangely does not return and could create a endless wait period if the
2154 * pipe is the only tracked fd in the poll set. The thread will take care
2155 * of closing the read side.
2157 close(ctx
->consumer_metadata_pipe
[1]);
2159 ret
= pthread_join(metadata_thread
, &status
);
2161 PERROR("pthread_join metadata thread");
2165 rcu_unregister_thread();
2170 * This thread listens on the consumerd socket and receives the file
2171 * descriptors from the session daemon.
2173 void *lttng_consumer_thread_receive_fds(void *data
)
2175 int sock
, client_socket
, ret
;
2177 * structure to poll for incoming data on communication socket avoids
2178 * making blocking sockets.
2180 struct pollfd consumer_sockpoll
[2];
2181 struct lttng_consumer_local_data
*ctx
= data
;
2183 rcu_register_thread();
2185 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2186 unlink(ctx
->consumer_command_sock_path
);
2187 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2188 if (client_socket
< 0) {
2189 ERR("Cannot create command socket");
2193 ret
= lttcomm_listen_unix_sock(client_socket
);
2198 DBG("Sending ready command to lttng-sessiond");
2199 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2200 /* return < 0 on error, but == 0 is not fatal */
2202 ERR("Error sending ready command to lttng-sessiond");
2206 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2208 PERROR("fcntl O_NONBLOCK");
2212 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2213 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2214 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2215 consumer_sockpoll
[1].fd
= client_socket
;
2216 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2218 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2221 DBG("Connection on client_socket");
2223 /* Blocking call, waiting for transmission */
2224 sock
= lttcomm_accept_unix_sock(client_socket
);
2229 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2231 PERROR("fcntl O_NONBLOCK");
2235 /* update the polling structure to poll on the established socket */
2236 consumer_sockpoll
[1].fd
= sock
;
2237 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2240 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2243 DBG("Incoming command on sock");
2244 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2245 if (ret
== -ENOENT
) {
2246 DBG("Received STOP command");
2251 * This could simply be a session daemon quitting. Don't output
2254 DBG("Communication interrupted on command socket");
2257 if (consumer_quit
) {
2258 DBG("consumer_thread_receive_fds received quit from signal");
2261 DBG("received fds on sock");
2264 DBG("consumer_thread_receive_fds exiting");
2267 * when all fds have hung up, the polling thread
2273 * 2s of grace period, if no polling events occur during
2274 * this period, the polling thread will exit even if there
2275 * are still open FDs (should not happen, but safety mechanism).
2277 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
2280 * Wake-up the other end by writing a null byte in the pipe
2281 * (non-blocking). Important note: Because writing into the
2282 * pipe is non-blocking (and therefore we allow dropping wakeup
2283 * data, as long as there is wakeup data present in the pipe
2284 * buffer to wake up the other end), the other end should
2285 * perform the following sequence for waiting:
2286 * 1) empty the pipe (reads).
2287 * 2) perform update operation.
2288 * 3) wait on the pipe (poll).
2291 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
2292 } while (ret
< 0 && errno
== EINTR
);
2293 rcu_unregister_thread();
2297 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2298 struct lttng_consumer_local_data
*ctx
)
2300 switch (consumer_data
.type
) {
2301 case LTTNG_CONSUMER_KERNEL
:
2302 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
2303 case LTTNG_CONSUMER32_UST
:
2304 case LTTNG_CONSUMER64_UST
:
2305 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2307 ERR("Unknown consumer_data type");
2313 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2315 switch (consumer_data
.type
) {
2316 case LTTNG_CONSUMER_KERNEL
:
2317 return lttng_kconsumer_on_recv_stream(stream
);
2318 case LTTNG_CONSUMER32_UST
:
2319 case LTTNG_CONSUMER64_UST
:
2320 return lttng_ustconsumer_on_recv_stream(stream
);
2322 ERR("Unknown consumer_data type");
2329 * Allocate and set consumer data hash tables.
2331 void lttng_consumer_init(void)
2333 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2334 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2335 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2339 * Process the ADD_RELAYD command receive by a consumer.
2341 * This will create a relayd socket pair and add it to the relayd hash table.
2342 * The caller MUST acquire a RCU read side lock before calling it.
2344 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
2345 struct lttng_consumer_local_data
*ctx
, int sock
,
2346 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
)
2349 struct consumer_relayd_sock_pair
*relayd
;
2351 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
2353 /* Get relayd reference if exists. */
2354 relayd
= consumer_find_relayd(net_seq_idx
);
2355 if (relayd
== NULL
) {
2356 /* Not found. Allocate one. */
2357 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
2358 if (relayd
== NULL
) {
2359 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
2364 /* Poll on consumer socket. */
2365 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2370 /* Get relayd socket from session daemon */
2371 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2372 if (ret
!= sizeof(fd
)) {
2373 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
2378 /* Copy socket information and received FD */
2379 switch (sock_type
) {
2380 case LTTNG_STREAM_CONTROL
:
2381 /* Copy received lttcomm socket */
2382 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
2383 ret
= lttcomm_create_sock(&relayd
->control_sock
);
2388 /* Close the created socket fd which is useless */
2389 close(relayd
->control_sock
.fd
);
2391 /* Assign new file descriptor */
2392 relayd
->control_sock
.fd
= fd
;
2394 case LTTNG_STREAM_DATA
:
2395 /* Copy received lttcomm socket */
2396 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
2397 ret
= lttcomm_create_sock(&relayd
->data_sock
);
2402 /* Close the created socket fd which is useless */
2403 close(relayd
->data_sock
.fd
);
2405 /* Assign new file descriptor */
2406 relayd
->data_sock
.fd
= fd
;
2409 ERR("Unknown relayd socket type (%d)", sock_type
);
2413 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2414 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
2415 relayd
->net_seq_idx
, fd
);
2418 * Add relayd socket pair to consumer data hashtable. If object already
2419 * exists or on error, the function gracefully returns.