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
)
67 struct lttng_ht_iter iter
;
68 struct lttng_ht_node_ulong
*node
;
69 struct lttng_consumer_stream
*stream
= NULL
;
71 /* Negative keys are lookup failures */
77 lttng_ht_lookup(consumer_data
.stream_ht
, (void *)((unsigned long) key
),
79 node
= lttng_ht_iter_get_node_ulong(&iter
);
81 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
89 static void consumer_steal_stream_key(int key
)
91 struct lttng_consumer_stream
*stream
;
94 stream
= consumer_find_stream(key
);
98 * We don't want the lookup to match, but we still need
99 * to iterate on this stream when iterating over the hash table. Just
100 * change the node key.
102 stream
->node
.key
= -1;
107 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
109 struct lttng_ht_iter iter
;
110 struct lttng_ht_node_ulong
*node
;
111 struct lttng_consumer_channel
*channel
= NULL
;
113 /* Negative keys are lookup failures */
119 lttng_ht_lookup(consumer_data
.channel_ht
, (void *)((unsigned long) key
),
121 node
= lttng_ht_iter_get_node_ulong(&iter
);
123 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
131 static void consumer_steal_channel_key(int key
)
133 struct lttng_consumer_channel
*channel
;
136 channel
= consumer_find_channel(key
);
140 * We don't want the lookup to match, but we still need
141 * to iterate on this channel when iterating over the hash table. Just
142 * change the node key.
144 channel
->node
.key
= -1;
150 void consumer_free_stream(struct rcu_head
*head
)
152 struct lttng_ht_node_ulong
*node
=
153 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
154 struct lttng_consumer_stream
*stream
=
155 caa_container_of(node
, struct lttng_consumer_stream
, node
);
161 * RCU protected relayd socket pair free.
163 static void consumer_rcu_free_relayd(struct rcu_head
*head
)
165 struct lttng_ht_node_ulong
*node
=
166 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
167 struct consumer_relayd_sock_pair
*relayd
=
168 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
174 * Destroy and free relayd socket pair object.
176 * This function MUST be called with the consumer_data lock acquired.
178 static void destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
181 struct lttng_ht_iter iter
;
183 if (relayd
== NULL
) {
187 DBG("Consumer destroy and close relayd socket pair");
189 iter
.iter
.node
= &relayd
->node
.node
;
190 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
192 /* We assume the relayd was already destroyed */
196 /* Close all sockets */
197 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
198 (void) relayd_close(&relayd
->control_sock
);
199 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
200 (void) relayd_close(&relayd
->data_sock
);
202 /* RCU free() call */
203 call_rcu(&relayd
->node
.head
, consumer_rcu_free_relayd
);
207 * Flag a relayd socket pair for destruction. Destroy it if the refcount
210 * RCU read side lock MUST be aquired before calling this function.
212 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
216 /* Set destroy flag for this object */
217 uatomic_set(&relayd
->destroy_flag
, 1);
219 /* Destroy the relayd if refcount is 0 */
220 if (uatomic_read(&relayd
->refcount
) == 0) {
221 destroy_relayd(relayd
);
226 * Remove a stream from the global list protected by a mutex. This
227 * function is also responsible for freeing its data structures.
229 void consumer_del_stream(struct lttng_consumer_stream
*stream
)
232 struct lttng_ht_iter iter
;
233 struct lttng_consumer_channel
*free_chan
= NULL
;
234 struct consumer_relayd_sock_pair
*relayd
;
238 pthread_mutex_lock(&consumer_data
.lock
);
240 switch (consumer_data
.type
) {
241 case LTTNG_CONSUMER_KERNEL
:
242 if (stream
->mmap_base
!= NULL
) {
243 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
249 case LTTNG_CONSUMER32_UST
:
250 case LTTNG_CONSUMER64_UST
:
251 lttng_ustconsumer_del_stream(stream
);
254 ERR("Unknown consumer_data type");
260 iter
.iter
.node
= &stream
->node
.node
;
261 ret
= lttng_ht_del(consumer_data
.stream_ht
, &iter
);
266 if (consumer_data
.stream_count
<= 0) {
269 consumer_data
.stream_count
--;
273 if (stream
->out_fd
>= 0) {
274 ret
= close(stream
->out_fd
);
279 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
280 ret
= close(stream
->wait_fd
);
285 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
286 ret
= close(stream
->shm_fd
);
292 /* Check and cleanup relayd */
294 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
295 if (relayd
!= NULL
) {
296 uatomic_dec(&relayd
->refcount
);
297 assert(uatomic_read(&relayd
->refcount
) >= 0);
299 /* Closing streams requires to lock the control socket. */
300 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
301 ret
= relayd_send_close_stream(&relayd
->control_sock
,
302 stream
->relayd_stream_id
,
303 stream
->next_net_seq_num
- 1);
304 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
306 DBG("Unable to close stream on the relayd. Continuing");
308 * Continue here. There is nothing we can do for the relayd.
309 * Chances are that the relayd has closed the socket so we just
310 * continue cleaning up.
314 /* Both conditions are met, we destroy the relayd. */
315 if (uatomic_read(&relayd
->refcount
) == 0 &&
316 uatomic_read(&relayd
->destroy_flag
)) {
317 destroy_relayd(relayd
);
322 uatomic_dec(&stream
->chan
->refcount
);
323 if (!uatomic_read(&stream
->chan
->refcount
)
324 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
325 free_chan
= stream
->chan
;
328 call_rcu(&stream
->node
.head
, consumer_free_stream
);
330 consumer_data
.need_update
= 1;
331 pthread_mutex_unlock(&consumer_data
.lock
);
334 consumer_del_channel(free_chan
);
338 struct lttng_consumer_stream
*consumer_allocate_stream(
339 int channel_key
, int stream_key
,
340 int shm_fd
, int wait_fd
,
341 enum lttng_consumer_stream_state state
,
343 enum lttng_event_output output
,
344 const char *path_name
,
350 struct lttng_consumer_stream
*stream
;
353 stream
= zmalloc(sizeof(*stream
));
354 if (stream
== NULL
) {
355 perror("malloc struct lttng_consumer_stream");
358 stream
->chan
= consumer_find_channel(channel_key
);
360 perror("Unable to find channel key");
363 stream
->chan
->refcount
++;
364 stream
->key
= stream_key
;
365 stream
->shm_fd
= shm_fd
;
366 stream
->wait_fd
= wait_fd
;
368 stream
->out_fd_offset
= 0;
369 stream
->state
= state
;
370 stream
->mmap_len
= mmap_len
;
371 stream
->mmap_base
= NULL
;
372 stream
->output
= output
;
375 stream
->net_seq_idx
= net_index
;
376 stream
->metadata_flag
= metadata_flag
;
377 strncpy(stream
->path_name
, path_name
, sizeof(stream
->path_name
));
378 stream
->path_name
[sizeof(stream
->path_name
) - 1] = '\0';
379 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
380 lttng_ht_node_init_ulong(&stream
->waitfd_node
, stream
->wait_fd
);
382 switch (consumer_data
.type
) {
383 case LTTNG_CONSUMER_KERNEL
:
385 case LTTNG_CONSUMER32_UST
:
386 case LTTNG_CONSUMER64_UST
:
387 stream
->cpu
= stream
->chan
->cpucount
++;
388 ret
= lttng_ustconsumer_allocate_stream(stream
);
395 ERR("Unknown consumer_data type");
401 * When nb_init_streams reaches 0, we don't need to trigger any action in
402 * terms of destroying the associated channel, because the action that
403 * causes the count to become 0 also causes a stream to be added. The
404 * channel deletion will thus be triggered by the following removal of this
407 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
408 uatomic_dec(&stream
->chan
->nb_init_streams
);
411 DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu,"
412 " out_fd %d, net_seq_idx %d)", stream
->path_name
, stream
->key
,
413 stream
->shm_fd
, stream
->wait_fd
,
414 (unsigned long long) stream
->mmap_len
, stream
->out_fd
,
415 stream
->net_seq_idx
);
422 * Add a stream to the global list protected by a mutex.
424 int consumer_add_stream(struct lttng_consumer_stream
*stream
)
427 struct lttng_ht_node_ulong
*node
;
428 struct lttng_ht_iter iter
;
429 struct consumer_relayd_sock_pair
*relayd
;
431 pthread_mutex_lock(&consumer_data
.lock
);
432 /* Steal stream identifier, for UST */
433 consumer_steal_stream_key(stream
->key
);
436 lttng_ht_lookup(consumer_data
.stream_ht
,
437 (void *)((unsigned long) stream
->key
), &iter
);
438 node
= lttng_ht_iter_get_node_ulong(&iter
);
441 /* Stream already exist. Ignore the insertion */
445 lttng_ht_add_unique_ulong(consumer_data
.stream_ht
, &stream
->node
);
447 /* Check and cleanup relayd */
448 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
449 if (relayd
!= NULL
) {
450 uatomic_inc(&relayd
->refcount
);
454 /* Update consumer data */
455 consumer_data
.stream_count
++;
456 consumer_data
.need_update
= 1;
459 pthread_mutex_unlock(&consumer_data
.lock
);
465 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
466 * be acquired before calling this.
468 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
471 struct lttng_ht_node_ulong
*node
;
472 struct lttng_ht_iter iter
;
474 if (relayd
== NULL
) {
479 lttng_ht_lookup(consumer_data
.relayd_ht
,
480 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
481 node
= lttng_ht_iter_get_node_ulong(&iter
);
483 /* Relayd already exist. Ignore the insertion */
486 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
493 * Allocate and return a consumer relayd socket.
495 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
498 struct consumer_relayd_sock_pair
*obj
= NULL
;
500 /* Negative net sequence index is a failure */
501 if (net_seq_idx
< 0) {
505 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
507 PERROR("zmalloc relayd sock");
511 obj
->net_seq_idx
= net_seq_idx
;
513 obj
->destroy_flag
= 0;
514 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
515 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
522 * Find a relayd socket pair in the global consumer data.
524 * Return the object if found else NULL.
525 * RCU read-side lock must be held across this call and while using the
528 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
530 struct lttng_ht_iter iter
;
531 struct lttng_ht_node_ulong
*node
;
532 struct consumer_relayd_sock_pair
*relayd
= NULL
;
534 /* Negative keys are lookup failures */
539 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
541 node
= lttng_ht_iter_get_node_ulong(&iter
);
543 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
551 * Handle stream for relayd transmission if the stream applies for network
552 * streaming where the net sequence index is set.
554 * Return destination file descriptor or negative value on error.
556 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
557 size_t data_size
, unsigned long padding
,
558 struct consumer_relayd_sock_pair
*relayd
)
561 struct lttcomm_relayd_data_hdr data_hdr
;
567 /* Reset data header */
568 memset(&data_hdr
, 0, sizeof(data_hdr
));
570 if (stream
->metadata_flag
) {
571 /* Caller MUST acquire the relayd control socket lock */
572 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
577 /* Metadata are always sent on the control socket. */
578 outfd
= relayd
->control_sock
.fd
;
580 /* Set header with stream information */
581 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
582 data_hdr
.data_size
= htobe32(data_size
);
583 data_hdr
.padding_size
= htobe32(padding
);
584 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
585 /* Other fields are zeroed previously */
587 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
593 /* Set to go on data socket */
594 outfd
= relayd
->data_sock
.fd
;
602 * Update a stream according to what we just received.
604 void consumer_change_stream_state(int stream_key
,
605 enum lttng_consumer_stream_state state
)
607 struct lttng_consumer_stream
*stream
;
609 pthread_mutex_lock(&consumer_data
.lock
);
610 stream
= consumer_find_stream(stream_key
);
612 stream
->state
= state
;
614 consumer_data
.need_update
= 1;
615 pthread_mutex_unlock(&consumer_data
.lock
);
619 void consumer_free_channel(struct rcu_head
*head
)
621 struct lttng_ht_node_ulong
*node
=
622 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
623 struct lttng_consumer_channel
*channel
=
624 caa_container_of(node
, struct lttng_consumer_channel
, node
);
630 * Remove a channel from the global list protected by a mutex. This
631 * function is also responsible for freeing its data structures.
633 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
636 struct lttng_ht_iter iter
;
638 pthread_mutex_lock(&consumer_data
.lock
);
640 switch (consumer_data
.type
) {
641 case LTTNG_CONSUMER_KERNEL
:
643 case LTTNG_CONSUMER32_UST
:
644 case LTTNG_CONSUMER64_UST
:
645 lttng_ustconsumer_del_channel(channel
);
648 ERR("Unknown consumer_data type");
654 iter
.iter
.node
= &channel
->node
.node
;
655 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
659 if (channel
->mmap_base
!= NULL
) {
660 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
665 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
666 ret
= close(channel
->wait_fd
);
671 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
672 ret
= close(channel
->shm_fd
);
678 call_rcu(&channel
->node
.head
, consumer_free_channel
);
680 pthread_mutex_unlock(&consumer_data
.lock
);
683 struct lttng_consumer_channel
*consumer_allocate_channel(
685 int shm_fd
, int wait_fd
,
687 uint64_t max_sb_size
,
688 unsigned int nb_init_streams
)
690 struct lttng_consumer_channel
*channel
;
693 channel
= zmalloc(sizeof(*channel
));
694 if (channel
== NULL
) {
695 perror("malloc struct lttng_consumer_channel");
698 channel
->key
= channel_key
;
699 channel
->shm_fd
= shm_fd
;
700 channel
->wait_fd
= wait_fd
;
701 channel
->mmap_len
= mmap_len
;
702 channel
->max_sb_size
= max_sb_size
;
703 channel
->refcount
= 0;
704 channel
->nb_init_streams
= nb_init_streams
;
705 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
707 switch (consumer_data
.type
) {
708 case LTTNG_CONSUMER_KERNEL
:
709 channel
->mmap_base
= NULL
;
710 channel
->mmap_len
= 0;
712 case LTTNG_CONSUMER32_UST
:
713 case LTTNG_CONSUMER64_UST
:
714 ret
= lttng_ustconsumer_allocate_channel(channel
);
721 ERR("Unknown consumer_data type");
725 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
726 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
727 (unsigned long long) channel
->mmap_len
,
728 (unsigned long long) channel
->max_sb_size
);
734 * Add a channel to the global list protected by a mutex.
736 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
738 struct lttng_ht_node_ulong
*node
;
739 struct lttng_ht_iter iter
;
741 pthread_mutex_lock(&consumer_data
.lock
);
742 /* Steal channel identifier, for UST */
743 consumer_steal_channel_key(channel
->key
);
746 lttng_ht_lookup(consumer_data
.channel_ht
,
747 (void *)((unsigned long) channel
->key
), &iter
);
748 node
= lttng_ht_iter_get_node_ulong(&iter
);
750 /* Channel already exist. Ignore the insertion */
754 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
758 pthread_mutex_unlock(&consumer_data
.lock
);
764 * Allocate the pollfd structure and the local view of the out fds to avoid
765 * doing a lookup in the linked list and concurrency issues when writing is
766 * needed. Called with consumer_data.lock held.
768 * Returns the number of fds in the structures.
770 int consumer_update_poll_array(
771 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
772 struct lttng_consumer_stream
**local_stream
)
775 struct lttng_ht_iter iter
;
776 struct lttng_consumer_stream
*stream
;
778 DBG("Updating poll fd array");
780 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, stream
,
782 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
785 DBG("Active FD %d", stream
->wait_fd
);
786 (*pollfd
)[i
].fd
= stream
->wait_fd
;
787 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
788 local_stream
[i
] = stream
;
794 * Insert the consumer_poll_pipe at the end of the array and don't
795 * increment i so nb_fd is the number of real FD.
797 (*pollfd
)[i
].fd
= ctx
->consumer_poll_pipe
[0];
798 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
803 * Poll on the should_quit pipe and the command socket return -1 on error and
804 * should exit, 0 if data is available on the command socket
806 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
811 num_rdy
= poll(consumer_sockpoll
, 2, -1);
814 * Restart interrupted system call.
816 if (errno
== EINTR
) {
819 perror("Poll error");
822 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
823 DBG("consumer_should_quit wake up");
833 * Set the error socket.
835 void lttng_consumer_set_error_sock(
836 struct lttng_consumer_local_data
*ctx
, int sock
)
838 ctx
->consumer_error_socket
= sock
;
842 * Set the command socket path.
844 void lttng_consumer_set_command_sock_path(
845 struct lttng_consumer_local_data
*ctx
, char *sock
)
847 ctx
->consumer_command_sock_path
= sock
;
851 * Send return code to the session daemon.
852 * If the socket is not defined, we return 0, it is not a fatal error
854 int lttng_consumer_send_error(
855 struct lttng_consumer_local_data
*ctx
, int cmd
)
857 if (ctx
->consumer_error_socket
> 0) {
858 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
859 sizeof(enum lttcomm_sessiond_command
));
866 * Close all the tracefiles and stream fds, should be called when all instances
869 void lttng_consumer_cleanup(void)
871 struct lttng_ht_iter iter
;
872 struct lttng_ht_node_ulong
*node
;
877 * close all outfd. Called when there are no more threads running (after
878 * joining on the threads), no need to protect list iteration with mutex.
880 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, node
,
882 struct lttng_consumer_stream
*stream
=
883 caa_container_of(node
, struct lttng_consumer_stream
, node
);
884 consumer_del_stream(stream
);
887 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
889 struct lttng_consumer_channel
*channel
=
890 caa_container_of(node
, struct lttng_consumer_channel
, node
);
891 consumer_del_channel(channel
);
896 lttng_ht_destroy(consumer_data
.stream_ht
);
897 lttng_ht_destroy(consumer_data
.channel_ht
);
901 * Called from signal handler.
903 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
908 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
909 } while (ret
< 0 && errno
== EINTR
);
911 perror("write consumer quit");
915 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
918 int outfd
= stream
->out_fd
;
921 * This does a blocking write-and-wait on any page that belongs to the
922 * subbuffer prior to the one we just wrote.
923 * Don't care about error values, as these are just hints and ways to
924 * limit the amount of page cache used.
926 if (orig_offset
< stream
->chan
->max_sb_size
) {
929 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
930 stream
->chan
->max_sb_size
,
931 SYNC_FILE_RANGE_WAIT_BEFORE
932 | SYNC_FILE_RANGE_WRITE
933 | SYNC_FILE_RANGE_WAIT_AFTER
);
935 * Give hints to the kernel about how we access the file:
936 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
939 * We need to call fadvise again after the file grows because the
940 * kernel does not seem to apply fadvise to non-existing parts of the
943 * Call fadvise _after_ having waited for the page writeback to
944 * complete because the dirty page writeback semantic is not well
945 * defined. So it can be expected to lead to lower throughput in
948 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
949 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
953 * Initialise the necessary environnement :
954 * - create a new context
955 * - create the poll_pipe
956 * - create the should_quit pipe (for signal handler)
957 * - create the thread pipe (for splice)
959 * Takes a function pointer as argument, this function is called when data is
960 * available on a buffer. This function is responsible to do the
961 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
962 * buffer configuration and then kernctl_put_next_subbuf at the end.
964 * Returns a pointer to the new context or NULL on error.
966 struct lttng_consumer_local_data
*lttng_consumer_create(
967 enum lttng_consumer_type type
,
968 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
969 struct lttng_consumer_local_data
*ctx
),
970 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
971 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
972 int (*update_stream
)(int stream_key
, uint32_t state
))
975 struct lttng_consumer_local_data
*ctx
;
977 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
978 consumer_data
.type
== type
);
979 consumer_data
.type
= type
;
981 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
983 perror("allocating context");
987 ctx
->consumer_error_socket
= -1;
988 /* assign the callbacks */
989 ctx
->on_buffer_ready
= buffer_ready
;
990 ctx
->on_recv_channel
= recv_channel
;
991 ctx
->on_recv_stream
= recv_stream
;
992 ctx
->on_update_stream
= update_stream
;
994 ret
= pipe(ctx
->consumer_poll_pipe
);
996 perror("Error creating poll pipe");
997 goto error_poll_pipe
;
1000 /* set read end of the pipe to non-blocking */
1001 ret
= fcntl(ctx
->consumer_poll_pipe
[0], F_SETFL
, O_NONBLOCK
);
1003 perror("fcntl O_NONBLOCK");
1004 goto error_poll_fcntl
;
1007 /* set write end of the pipe to non-blocking */
1008 ret
= fcntl(ctx
->consumer_poll_pipe
[1], F_SETFL
, O_NONBLOCK
);
1010 perror("fcntl O_NONBLOCK");
1011 goto error_poll_fcntl
;
1014 ret
= pipe(ctx
->consumer_should_quit
);
1016 perror("Error creating recv pipe");
1017 goto error_quit_pipe
;
1020 ret
= pipe(ctx
->consumer_thread_pipe
);
1022 perror("Error creating thread pipe");
1023 goto error_thread_pipe
;
1026 ret
= utils_create_pipe(ctx
->consumer_metadata_pipe
);
1028 goto error_metadata_pipe
;
1031 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1033 goto error_splice_pipe
;
1039 utils_close_pipe(ctx
->consumer_metadata_pipe
);
1040 error_metadata_pipe
:
1041 utils_close_pipe(ctx
->consumer_thread_pipe
);
1043 for (i
= 0; i
< 2; i
++) {
1046 err
= close(ctx
->consumer_should_quit
[i
]);
1053 for (i
= 0; i
< 2; i
++) {
1056 err
= close(ctx
->consumer_poll_pipe
[i
]);
1068 * Close all fds associated with the instance and free the context.
1070 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1074 ret
= close(ctx
->consumer_error_socket
);
1078 ret
= close(ctx
->consumer_thread_pipe
[0]);
1082 ret
= close(ctx
->consumer_thread_pipe
[1]);
1086 ret
= close(ctx
->consumer_poll_pipe
[0]);
1090 ret
= close(ctx
->consumer_poll_pipe
[1]);
1094 ret
= close(ctx
->consumer_should_quit
[0]);
1098 ret
= close(ctx
->consumer_should_quit
[1]);
1102 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1104 unlink(ctx
->consumer_command_sock_path
);
1109 * Write the metadata stream id on the specified file descriptor.
1111 static int write_relayd_metadata_id(int fd
,
1112 struct lttng_consumer_stream
*stream
,
1113 struct consumer_relayd_sock_pair
*relayd
,
1114 unsigned long padding
)
1117 struct lttcomm_relayd_metadata_payload hdr
;
1119 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1120 hdr
.padding_size
= htobe32(padding
);
1122 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1123 } while (ret
< 0 && errno
== EINTR
);
1125 PERROR("write metadata stream id");
1128 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1129 stream
->relayd_stream_id
, padding
);
1136 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1137 * core function for writing trace buffers to either the local filesystem or
1140 * Careful review MUST be put if any changes occur!
1142 * Returns the number of bytes written
1144 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1145 struct lttng_consumer_local_data
*ctx
,
1146 struct lttng_consumer_stream
*stream
, unsigned long len
,
1147 unsigned long padding
)
1149 unsigned long mmap_offset
;
1150 ssize_t ret
= 0, written
= 0;
1151 off_t orig_offset
= stream
->out_fd_offset
;
1152 /* Default is on the disk */
1153 int outfd
= stream
->out_fd
;
1154 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1156 /* RCU lock for the relayd pointer */
1159 /* Flag that the current stream if set for network streaming. */
1160 if (stream
->net_seq_idx
!= -1) {
1161 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1162 if (relayd
== NULL
) {
1167 /* get the offset inside the fd to mmap */
1168 switch (consumer_data
.type
) {
1169 case LTTNG_CONSUMER_KERNEL
:
1170 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1172 case LTTNG_CONSUMER32_UST
:
1173 case LTTNG_CONSUMER64_UST
:
1174 ret
= lttng_ustctl_get_mmap_read_offset(stream
->chan
->handle
,
1175 stream
->buf
, &mmap_offset
);
1178 ERR("Unknown consumer_data type");
1183 PERROR("tracer ctl get_mmap_read_offset");
1188 /* Handle stream on the relayd if the output is on the network */
1190 unsigned long netlen
= len
;
1193 * Lock the control socket for the complete duration of the function
1194 * since from this point on we will use the socket.
1196 if (stream
->metadata_flag
) {
1197 /* Metadata requires the control socket. */
1198 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1199 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1202 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1204 /* Use the returned socket. */
1207 /* Write metadata stream id before payload */
1208 if (stream
->metadata_flag
) {
1209 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1216 /* Else, use the default set before which is the filesystem. */
1218 /* No streaming, we have to set the len with the full padding */
1224 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
1225 } while (ret
< 0 && errno
== EINTR
);
1226 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1228 PERROR("Error in file write");
1233 } else if (ret
> len
) {
1234 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1242 /* This call is useless on a socket so better save a syscall. */
1244 /* This won't block, but will start writeout asynchronously */
1245 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1246 SYNC_FILE_RANGE_WRITE
);
1247 stream
->out_fd_offset
+= ret
;
1251 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1254 /* Unlock only if ctrl socket used */
1255 if (relayd
&& stream
->metadata_flag
) {
1256 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1264 * Splice the data from the ring buffer to the tracefile.
1266 * Returns the number of bytes spliced.
1268 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1269 struct lttng_consumer_local_data
*ctx
,
1270 struct lttng_consumer_stream
*stream
, unsigned long len
,
1271 unsigned long padding
)
1273 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1275 off_t orig_offset
= stream
->out_fd_offset
;
1276 int fd
= stream
->wait_fd
;
1277 /* Default is on the disk */
1278 int outfd
= stream
->out_fd
;
1279 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1282 switch (consumer_data
.type
) {
1283 case LTTNG_CONSUMER_KERNEL
:
1285 case LTTNG_CONSUMER32_UST
:
1286 case LTTNG_CONSUMER64_UST
:
1287 /* Not supported for user space tracing */
1290 ERR("Unknown consumer_data type");
1294 /* RCU lock for the relayd pointer */
1297 /* Flag that the current stream if set for network streaming. */
1298 if (stream
->net_seq_idx
!= -1) {
1299 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1300 if (relayd
== NULL
) {
1306 * Choose right pipe for splice. Metadata and trace data are handled by
1307 * different threads hence the use of two pipes in order not to race or
1308 * corrupt the written data.
1310 if (stream
->metadata_flag
) {
1311 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1313 splice_pipe
= ctx
->consumer_thread_pipe
;
1316 /* Write metadata stream id before payload */
1318 int total_len
= len
;
1320 if (stream
->metadata_flag
) {
1322 * Lock the control socket for the complete duration of the function
1323 * since from this point on we will use the socket.
1325 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1327 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1334 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1337 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1339 /* Use the returned socket. */
1342 ERR("Remote relayd disconnected. Stopping");
1346 /* No streaming, we have to set the len with the full padding */
1351 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1352 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1353 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1354 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1355 DBG("splice chan to pipe, ret %zd", ret_splice
);
1356 if (ret_splice
< 0) {
1357 PERROR("Error in relay splice");
1359 written
= ret_splice
;
1365 /* Handle stream on the relayd if the output is on the network */
1367 if (stream
->metadata_flag
) {
1368 size_t metadata_payload_size
=
1369 sizeof(struct lttcomm_relayd_metadata_payload
);
1371 /* Update counter to fit the spliced data */
1372 ret_splice
+= metadata_payload_size
;
1373 len
+= metadata_payload_size
;
1375 * We do this so the return value can match the len passed as
1376 * argument to this function.
1378 written
-= metadata_payload_size
;
1382 /* Splice data out */
1383 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1384 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1385 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1386 if (ret_splice
< 0) {
1387 PERROR("Error in file splice");
1389 written
= ret_splice
;
1393 } else if (ret_splice
> len
) {
1395 PERROR("Wrote more data than requested %zd (len: %lu)",
1397 written
+= ret_splice
;
1403 /* This call is useless on a socket so better save a syscall. */
1405 /* This won't block, but will start writeout asynchronously */
1406 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1407 SYNC_FILE_RANGE_WRITE
);
1408 stream
->out_fd_offset
+= ret_splice
;
1410 written
+= ret_splice
;
1412 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1419 /* send the appropriate error description to sessiond */
1422 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EBADF
);
1425 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1428 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1431 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1436 if (relayd
&& stream
->metadata_flag
) {
1437 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1445 * Take a snapshot for a specific fd
1447 * Returns 0 on success, < 0 on error
1449 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1450 struct lttng_consumer_stream
*stream
)
1452 switch (consumer_data
.type
) {
1453 case LTTNG_CONSUMER_KERNEL
:
1454 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1455 case LTTNG_CONSUMER32_UST
:
1456 case LTTNG_CONSUMER64_UST
:
1457 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1459 ERR("Unknown consumer_data type");
1467 * Get the produced position
1469 * Returns 0 on success, < 0 on error
1471 int lttng_consumer_get_produced_snapshot(
1472 struct lttng_consumer_local_data
*ctx
,
1473 struct lttng_consumer_stream
*stream
,
1476 switch (consumer_data
.type
) {
1477 case LTTNG_CONSUMER_KERNEL
:
1478 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1479 case LTTNG_CONSUMER32_UST
:
1480 case LTTNG_CONSUMER64_UST
:
1481 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1483 ERR("Unknown consumer_data type");
1489 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1490 int sock
, struct pollfd
*consumer_sockpoll
)
1492 switch (consumer_data
.type
) {
1493 case LTTNG_CONSUMER_KERNEL
:
1494 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1495 case LTTNG_CONSUMER32_UST
:
1496 case LTTNG_CONSUMER64_UST
:
1497 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1499 ERR("Unknown consumer_data type");
1506 * Iterate over all stream element of the hashtable and free them. This is race
1507 * free since the hashtable received MUST be in a race free synchronization
1508 * state. It's the caller responsability to make sure of that.
1510 static void destroy_stream_ht(struct lttng_ht
*ht
)
1513 struct lttng_ht_iter iter
;
1514 struct lttng_consumer_stream
*stream
;
1521 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1522 ret
= lttng_ht_del(ht
, &iter
);
1529 lttng_ht_destroy(ht
);
1533 * Clean up a metadata stream and free its memory.
1535 static void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
)
1538 struct consumer_relayd_sock_pair
*relayd
;
1542 * This call should NEVER receive regular stream. It must always be
1543 * metadata stream and this is crucial for data structure synchronization.
1545 assert(stream
->metadata_flag
);
1547 pthread_mutex_lock(&consumer_data
.lock
);
1548 switch (consumer_data
.type
) {
1549 case LTTNG_CONSUMER_KERNEL
:
1550 if (stream
->mmap_base
!= NULL
) {
1551 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1553 PERROR("munmap metadata stream");
1557 case LTTNG_CONSUMER32_UST
:
1558 case LTTNG_CONSUMER64_UST
:
1559 lttng_ustconsumer_del_stream(stream
);
1562 ERR("Unknown consumer_data type");
1565 pthread_mutex_unlock(&consumer_data
.lock
);
1567 if (stream
->out_fd
>= 0) {
1568 ret
= close(stream
->out_fd
);
1574 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
1575 ret
= close(stream
->wait_fd
);
1581 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
1582 ret
= close(stream
->shm_fd
);
1588 /* Check and cleanup relayd */
1590 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1591 if (relayd
!= NULL
) {
1592 uatomic_dec(&relayd
->refcount
);
1593 assert(uatomic_read(&relayd
->refcount
) >= 0);
1595 /* Closing streams requires to lock the control socket. */
1596 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1597 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1598 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1599 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1601 DBG("Unable to close stream on the relayd. Continuing");
1603 * Continue here. There is nothing we can do for the relayd.
1604 * Chances are that the relayd has closed the socket so we just
1605 * continue cleaning up.
1609 /* Both conditions are met, we destroy the relayd. */
1610 if (uatomic_read(&relayd
->refcount
) == 0 &&
1611 uatomic_read(&relayd
->destroy_flag
)) {
1612 destroy_relayd(relayd
);
1617 /* Atomically decrement channel refcount since other threads can use it. */
1618 uatomic_dec(&stream
->chan
->refcount
);
1619 if (!uatomic_read(&stream
->chan
->refcount
)
1620 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
1621 /* Go for channel deletion! */
1622 consumer_del_channel(stream
->chan
);
1629 * Action done with the metadata stream when adding it to the consumer internal
1630 * data structures to handle it.
1632 static void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
1634 struct consumer_relayd_sock_pair
*relayd
;
1636 /* Find relayd and, if one is found, increment refcount. */
1638 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1639 if (relayd
!= NULL
) {
1640 uatomic_inc(&relayd
->refcount
);
1646 * Thread polls on metadata file descriptor and write them on disk or on the
1649 void *lttng_consumer_thread_poll_metadata(void *data
)
1652 uint32_t revents
, nb_fd
;
1653 struct lttng_consumer_stream
*stream
;
1654 struct lttng_ht_iter iter
;
1655 struct lttng_ht_node_ulong
*node
;
1656 struct lttng_ht
*metadata_ht
= NULL
;
1657 struct lttng_poll_event events
;
1658 struct lttng_consumer_local_data
*ctx
= data
;
1661 rcu_register_thread();
1663 DBG("Thread metadata poll started");
1665 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1666 if (metadata_ht
== NULL
) {
1670 /* Size is set to 1 for the consumer_metadata pipe */
1671 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
1673 ERR("Poll set creation failed");
1677 ret
= lttng_poll_add(&events
, ctx
->consumer_metadata_pipe
[0], LPOLLIN
);
1683 DBG("Metadata main loop started");
1686 lttng_poll_reset(&events
);
1688 nb_fd
= LTTNG_POLL_GETNB(&events
);
1690 /* Only the metadata pipe is set */
1691 if (nb_fd
== 0 && consumer_quit
== 1) {
1696 DBG("Metadata poll wait with %d fd(s)", nb_fd
);
1697 ret
= lttng_poll_wait(&events
, -1);
1698 DBG("Metadata event catched in thread");
1700 if (errno
== EINTR
) {
1706 for (i
= 0; i
< nb_fd
; i
++) {
1707 revents
= LTTNG_POLL_GETEV(&events
, i
);
1708 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1710 /* Check the metadata pipe for incoming metadata. */
1711 if (pollfd
== ctx
->consumer_metadata_pipe
[0]) {
1712 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
1713 DBG("Metadata thread pipe hung up");
1715 * Remove the pipe from the poll set and continue the loop
1716 * since their might be data to consume.
1718 lttng_poll_del(&events
, ctx
->consumer_metadata_pipe
[0]);
1719 close(ctx
->consumer_metadata_pipe
[0]);
1721 } else if (revents
& LPOLLIN
) {
1722 stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
1723 if (stream
== NULL
) {
1724 PERROR("zmalloc metadata consumer stream");
1729 /* Get the stream and add it to the local hash table */
1730 ret
= read(pollfd
, stream
,
1731 sizeof(struct lttng_consumer_stream
));
1732 } while (ret
< 0 && errno
== EINTR
);
1733 if (ret
< 0 || ret
< sizeof(struct lttng_consumer_stream
)) {
1734 PERROR("read metadata stream");
1737 * Let's continue here and hope we can still work
1738 * without stopping the consumer. XXX: Should we?
1743 DBG("Adding metadata stream %d to poll set",
1747 /* The node should be init at this point */
1748 lttng_ht_add_unique_ulong(metadata_ht
,
1749 &stream
->waitfd_node
);
1752 /* Add metadata stream to the global poll events list */
1753 lttng_poll_add(&events
, stream
->wait_fd
,
1754 LPOLLIN
| LPOLLPRI
);
1756 consumer_add_metadata_stream(stream
);
1759 /* Metadata pipe handled. Continue handling the others */
1763 /* From here, the event is a metadata wait fd */
1766 lttng_ht_lookup(metadata_ht
, (void *)((unsigned long) pollfd
),
1768 node
= lttng_ht_iter_get_node_ulong(&iter
);
1770 /* FD not found, continue loop */
1775 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
1778 /* Get the data out of the metadata file descriptor */
1779 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
1780 DBG("Metadata available on fd %d", pollfd
);
1781 assert(stream
->wait_fd
== pollfd
);
1783 len
= ctx
->on_buffer_ready(stream
, ctx
);
1784 /* It's ok to have an unavailable sub-buffer */
1785 if (len
< 0 && len
!= -EAGAIN
) {
1788 } else if (len
> 0) {
1789 stream
->data_read
= 1;
1794 * Remove the stream from the hash table since there is no data
1795 * left on the fd because we previously did a read on the buffer.
1797 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
1798 DBG("Metadata fd %d is hup|err|nval.", pollfd
);
1799 if (!stream
->hangup_flush_done
1800 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1801 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1802 DBG("Attempting to flush and consume the UST buffers");
1803 lttng_ustconsumer_on_stream_hangup(stream
);
1805 /* We just flushed the stream now read it. */
1806 len
= ctx
->on_buffer_ready(stream
, ctx
);
1807 /* It's ok to have an unavailable sub-buffer */
1808 if (len
< 0 && len
!= -EAGAIN
) {
1814 /* Removing it from hash table, poll set and free memory */
1815 lttng_ht_del(metadata_ht
, &iter
);
1817 lttng_poll_del(&events
, stream
->wait_fd
);
1818 consumer_del_metadata_stream(stream
);
1826 DBG("Metadata poll thread exiting");
1827 lttng_poll_clean(&events
);
1830 destroy_stream_ht(metadata_ht
);
1833 rcu_unregister_thread();
1838 * This thread polls the fds in the set to consume the data and write
1839 * it to tracefile if necessary.
1841 void *lttng_consumer_thread_poll_fds(void *data
)
1843 int num_rdy
, num_hup
, high_prio
, ret
, i
;
1844 struct pollfd
*pollfd
= NULL
;
1845 /* local view of the streams */
1846 struct lttng_consumer_stream
**local_stream
= NULL
;
1847 /* local view of consumer_data.fds_count */
1849 struct lttng_consumer_local_data
*ctx
= data
;
1851 pthread_t metadata_thread
;
1854 rcu_register_thread();
1856 /* Start metadata polling thread */
1857 ret
= pthread_create(&metadata_thread
, NULL
,
1858 lttng_consumer_thread_poll_metadata
, (void *) ctx
);
1860 PERROR("pthread_create metadata thread");
1864 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
1871 * the fds set has been updated, we need to update our
1872 * local array as well
1874 pthread_mutex_lock(&consumer_data
.lock
);
1875 if (consumer_data
.need_update
) {
1876 if (pollfd
!= NULL
) {
1880 if (local_stream
!= NULL
) {
1882 local_stream
= NULL
;
1885 /* allocate for all fds + 1 for the consumer_poll_pipe */
1886 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
1887 if (pollfd
== NULL
) {
1888 perror("pollfd malloc");
1889 pthread_mutex_unlock(&consumer_data
.lock
);
1893 /* allocate for all fds + 1 for the consumer_poll_pipe */
1894 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
1895 sizeof(struct lttng_consumer_stream
));
1896 if (local_stream
== NULL
) {
1897 perror("local_stream malloc");
1898 pthread_mutex_unlock(&consumer_data
.lock
);
1901 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
);
1903 ERR("Error in allocating pollfd or local_outfds");
1904 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
1905 pthread_mutex_unlock(&consumer_data
.lock
);
1909 consumer_data
.need_update
= 0;
1911 pthread_mutex_unlock(&consumer_data
.lock
);
1913 /* No FDs and consumer_quit, consumer_cleanup the thread */
1914 if (nb_fd
== 0 && consumer_quit
== 1) {
1917 /* poll on the array of fds */
1919 DBG("polling on %d fd", nb_fd
+ 1);
1920 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
1921 DBG("poll num_rdy : %d", num_rdy
);
1922 if (num_rdy
== -1) {
1924 * Restart interrupted system call.
1926 if (errno
== EINTR
) {
1929 perror("Poll error");
1930 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
1932 } else if (num_rdy
== 0) {
1933 DBG("Polling thread timed out");
1938 * If the consumer_poll_pipe triggered poll go directly to the
1939 * beginning of the loop to update the array. We want to prioritize
1940 * array update over low-priority reads.
1942 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
1943 size_t pipe_readlen
;
1946 DBG("consumer_poll_pipe wake up");
1947 /* Consume 1 byte of pipe data */
1949 pipe_readlen
= read(ctx
->consumer_poll_pipe
[0], &tmp
, 1);
1950 } while (pipe_readlen
== -1 && errno
== EINTR
);
1954 /* Take care of high priority channels first. */
1955 for (i
= 0; i
< nb_fd
; i
++) {
1956 if (pollfd
[i
].revents
& POLLPRI
) {
1957 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
1959 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1960 /* it's ok to have an unavailable sub-buffer */
1961 if (len
< 0 && len
!= -EAGAIN
) {
1963 } else if (len
> 0) {
1964 local_stream
[i
]->data_read
= 1;
1970 * If we read high prio channel in this loop, try again
1971 * for more high prio data.
1977 /* Take care of low priority channels. */
1978 for (i
= 0; i
< nb_fd
; i
++) {
1979 if ((pollfd
[i
].revents
& POLLIN
) ||
1980 local_stream
[i
]->hangup_flush_done
) {
1981 DBG("Normal read on fd %d", pollfd
[i
].fd
);
1982 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1983 /* it's ok to have an unavailable sub-buffer */
1984 if (len
< 0 && len
!= -EAGAIN
) {
1986 } else if (len
> 0) {
1987 local_stream
[i
]->data_read
= 1;
1992 /* Handle hangup and errors */
1993 for (i
= 0; i
< nb_fd
; i
++) {
1994 if (!local_stream
[i
]->hangup_flush_done
1995 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
1996 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1997 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1998 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2000 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2001 /* Attempt read again, for the data we just flushed. */
2002 local_stream
[i
]->data_read
= 1;
2005 * If the poll flag is HUP/ERR/NVAL and we have
2006 * read no data in this pass, we can remove the
2007 * stream from its hash table.
2009 if ((pollfd
[i
].revents
& POLLHUP
)) {
2010 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2011 if (!local_stream
[i
]->data_read
) {
2012 consumer_del_stream(local_stream
[i
]);
2015 } else if (pollfd
[i
].revents
& POLLERR
) {
2016 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2017 if (!local_stream
[i
]->data_read
) {
2018 consumer_del_stream(local_stream
[i
]);
2021 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2022 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2023 if (!local_stream
[i
]->data_read
) {
2024 consumer_del_stream(local_stream
[i
]);
2028 local_stream
[i
]->data_read
= 0;
2032 DBG("polling thread exiting");
2033 if (pollfd
!= NULL
) {
2037 if (local_stream
!= NULL
) {
2039 local_stream
= NULL
;
2043 * Close the write side of the pipe so epoll_wait() in
2044 * lttng_consumer_thread_poll_metadata can catch it. The thread is
2045 * monitoring the read side of the pipe. If we close them both, epoll_wait
2046 * strangely does not return and could create a endless wait period if the
2047 * pipe is the only tracked fd in the poll set. The thread will take care
2048 * of closing the read side.
2050 close(ctx
->consumer_metadata_pipe
[1]);
2052 ret
= pthread_join(metadata_thread
, &status
);
2054 PERROR("pthread_join metadata thread");
2058 rcu_unregister_thread();
2063 * This thread listens on the consumerd socket and receives the file
2064 * descriptors from the session daemon.
2066 void *lttng_consumer_thread_receive_fds(void *data
)
2068 int sock
, client_socket
, ret
;
2070 * structure to poll for incoming data on communication socket avoids
2071 * making blocking sockets.
2073 struct pollfd consumer_sockpoll
[2];
2074 struct lttng_consumer_local_data
*ctx
= data
;
2076 rcu_register_thread();
2078 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2079 unlink(ctx
->consumer_command_sock_path
);
2080 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2081 if (client_socket
< 0) {
2082 ERR("Cannot create command socket");
2086 ret
= lttcomm_listen_unix_sock(client_socket
);
2091 DBG("Sending ready command to lttng-sessiond");
2092 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2093 /* return < 0 on error, but == 0 is not fatal */
2095 ERR("Error sending ready command to lttng-sessiond");
2099 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2101 perror("fcntl O_NONBLOCK");
2105 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2106 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2107 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2108 consumer_sockpoll
[1].fd
= client_socket
;
2109 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2111 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2114 DBG("Connection on client_socket");
2116 /* Blocking call, waiting for transmission */
2117 sock
= lttcomm_accept_unix_sock(client_socket
);
2122 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2124 perror("fcntl O_NONBLOCK");
2128 /* update the polling structure to poll on the established socket */
2129 consumer_sockpoll
[1].fd
= sock
;
2130 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2133 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2136 DBG("Incoming command on sock");
2137 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2138 if (ret
== -ENOENT
) {
2139 DBG("Received STOP command");
2144 * This could simply be a session daemon quitting. Don't output
2147 DBG("Communication interrupted on command socket");
2150 if (consumer_quit
) {
2151 DBG("consumer_thread_receive_fds received quit from signal");
2154 DBG("received fds on sock");
2157 DBG("consumer_thread_receive_fds exiting");
2160 * when all fds have hung up, the polling thread
2166 * 2s of grace period, if no polling events occur during
2167 * this period, the polling thread will exit even if there
2168 * are still open FDs (should not happen, but safety mechanism).
2170 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
2173 * Wake-up the other end by writing a null byte in the pipe
2174 * (non-blocking). Important note: Because writing into the
2175 * pipe is non-blocking (and therefore we allow dropping wakeup
2176 * data, as long as there is wakeup data present in the pipe
2177 * buffer to wake up the other end), the other end should
2178 * perform the following sequence for waiting:
2179 * 1) empty the pipe (reads).
2180 * 2) perform update operation.
2181 * 3) wait on the pipe (poll).
2184 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
2185 } while (ret
< 0 && errno
== EINTR
);
2186 rcu_unregister_thread();
2190 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2191 struct lttng_consumer_local_data
*ctx
)
2193 switch (consumer_data
.type
) {
2194 case LTTNG_CONSUMER_KERNEL
:
2195 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
2196 case LTTNG_CONSUMER32_UST
:
2197 case LTTNG_CONSUMER64_UST
:
2198 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2200 ERR("Unknown consumer_data type");
2206 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2208 switch (consumer_data
.type
) {
2209 case LTTNG_CONSUMER_KERNEL
:
2210 return lttng_kconsumer_on_recv_stream(stream
);
2211 case LTTNG_CONSUMER32_UST
:
2212 case LTTNG_CONSUMER64_UST
:
2213 return lttng_ustconsumer_on_recv_stream(stream
);
2215 ERR("Unknown consumer_data type");
2222 * Allocate and set consumer data hash tables.
2224 void lttng_consumer_init(void)
2226 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2227 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2228 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2232 * Process the ADD_RELAYD command receive by a consumer.
2234 * This will create a relayd socket pair and add it to the relayd hash table.
2235 * The caller MUST acquire a RCU read side lock before calling it.
2237 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
2238 struct lttng_consumer_local_data
*ctx
, int sock
,
2239 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
)
2242 struct consumer_relayd_sock_pair
*relayd
;
2244 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
2246 /* Get relayd reference if exists. */
2247 relayd
= consumer_find_relayd(net_seq_idx
);
2248 if (relayd
== NULL
) {
2249 /* Not found. Allocate one. */
2250 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
2251 if (relayd
== NULL
) {
2252 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
2257 /* Poll on consumer socket. */
2258 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2263 /* Get relayd socket from session daemon */
2264 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2265 if (ret
!= sizeof(fd
)) {
2266 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
2271 /* Copy socket information and received FD */
2272 switch (sock_type
) {
2273 case LTTNG_STREAM_CONTROL
:
2274 /* Copy received lttcomm socket */
2275 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
2276 ret
= lttcomm_create_sock(&relayd
->control_sock
);
2281 /* Close the created socket fd which is useless */
2282 close(relayd
->control_sock
.fd
);
2284 /* Assign new file descriptor */
2285 relayd
->control_sock
.fd
= fd
;
2287 case LTTNG_STREAM_DATA
:
2288 /* Copy received lttcomm socket */
2289 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
2290 ret
= lttcomm_create_sock(&relayd
->data_sock
);
2295 /* Close the created socket fd which is useless */
2296 close(relayd
->data_sock
.fd
);
2298 /* Assign new file descriptor */
2299 relayd
->data_sock
.fd
= fd
;
2302 ERR("Unknown relayd socket type (%d)", sock_type
);
2306 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2307 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
2308 relayd
->net_seq_idx
, fd
);
2311 * Add relayd socket pair to consumer data hashtable. If object already
2312 * exists or on error, the function gracefully returns.