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 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
);
413 * The cpu number is needed before using any ustctl_* actions. Ignored for
414 * the kernel so the value does not matter.
416 pthread_mutex_lock(&consumer_data
.lock
);
417 stream
->cpu
= stream
->chan
->cpucount
++;
418 pthread_mutex_unlock(&consumer_data
.lock
);
420 DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu,"
421 " out_fd %d, net_seq_idx %d)", stream
->path_name
, stream
->key
,
422 stream
->shm_fd
, stream
->wait_fd
,
423 (unsigned long long) stream
->mmap_len
, stream
->out_fd
,
424 stream
->net_seq_idx
);
434 * Add a stream to the global list protected by a mutex.
436 int consumer_add_stream(struct lttng_consumer_stream
*stream
)
439 struct consumer_relayd_sock_pair
*relayd
;
443 DBG3("Adding consumer stream %d", stream
->key
);
445 pthread_mutex_lock(&consumer_data
.lock
);
448 lttng_ht_add_unique_ulong(consumer_data
.stream_ht
, &stream
->node
);
450 /* Check and cleanup relayd */
451 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
452 if (relayd
!= NULL
) {
453 uatomic_inc(&relayd
->refcount
);
456 /* Update channel refcount once added without error(s). */
457 uatomic_inc(&stream
->chan
->refcount
);
460 * When nb_init_streams reaches 0, we don't need to trigger any action in
461 * terms of destroying the associated channel, because the action that
462 * causes the count to become 0 also causes a stream to be added. The
463 * channel deletion will thus be triggered by the following removal of this
466 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
467 uatomic_dec(&stream
->chan
->nb_init_streams
);
470 /* Update consumer data once the node is inserted. */
471 consumer_data
.stream_count
++;
472 consumer_data
.need_update
= 1;
475 pthread_mutex_unlock(&consumer_data
.lock
);
481 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
482 * be acquired before calling this.
484 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
487 struct lttng_ht_node_ulong
*node
;
488 struct lttng_ht_iter iter
;
490 if (relayd
== NULL
) {
495 lttng_ht_lookup(consumer_data
.relayd_ht
,
496 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
497 node
= lttng_ht_iter_get_node_ulong(&iter
);
499 /* Relayd already exist. Ignore the insertion */
502 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
509 * Allocate and return a consumer relayd socket.
511 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
514 struct consumer_relayd_sock_pair
*obj
= NULL
;
516 /* Negative net sequence index is a failure */
517 if (net_seq_idx
< 0) {
521 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
523 PERROR("zmalloc relayd sock");
527 obj
->net_seq_idx
= net_seq_idx
;
529 obj
->destroy_flag
= 0;
530 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
531 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
538 * Find a relayd socket pair in the global consumer data.
540 * Return the object if found else NULL.
541 * RCU read-side lock must be held across this call and while using the
544 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
546 struct lttng_ht_iter iter
;
547 struct lttng_ht_node_ulong
*node
;
548 struct consumer_relayd_sock_pair
*relayd
= NULL
;
550 /* Negative keys are lookup failures */
555 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
557 node
= lttng_ht_iter_get_node_ulong(&iter
);
559 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
567 * Handle stream for relayd transmission if the stream applies for network
568 * streaming where the net sequence index is set.
570 * Return destination file descriptor or negative value on error.
572 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
573 size_t data_size
, unsigned long padding
,
574 struct consumer_relayd_sock_pair
*relayd
)
577 struct lttcomm_relayd_data_hdr data_hdr
;
583 /* Reset data header */
584 memset(&data_hdr
, 0, sizeof(data_hdr
));
586 if (stream
->metadata_flag
) {
587 /* Caller MUST acquire the relayd control socket lock */
588 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
593 /* Metadata are always sent on the control socket. */
594 outfd
= relayd
->control_sock
.fd
;
596 /* Set header with stream information */
597 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
598 data_hdr
.data_size
= htobe32(data_size
);
599 data_hdr
.padding_size
= htobe32(padding
);
600 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
601 /* Other fields are zeroed previously */
603 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
609 /* Set to go on data socket */
610 outfd
= relayd
->data_sock
.fd
;
618 * Update a stream according to what we just received.
620 void consumer_change_stream_state(int stream_key
,
621 enum lttng_consumer_stream_state state
)
623 struct lttng_consumer_stream
*stream
;
625 pthread_mutex_lock(&consumer_data
.lock
);
626 stream
= consumer_find_stream(stream_key
, consumer_data
.stream_ht
);
628 stream
->state
= state
;
630 consumer_data
.need_update
= 1;
631 pthread_mutex_unlock(&consumer_data
.lock
);
635 void consumer_free_channel(struct rcu_head
*head
)
637 struct lttng_ht_node_ulong
*node
=
638 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
639 struct lttng_consumer_channel
*channel
=
640 caa_container_of(node
, struct lttng_consumer_channel
, node
);
646 * Remove a channel from the global list protected by a mutex. This
647 * function is also responsible for freeing its data structures.
649 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
652 struct lttng_ht_iter iter
;
654 pthread_mutex_lock(&consumer_data
.lock
);
656 switch (consumer_data
.type
) {
657 case LTTNG_CONSUMER_KERNEL
:
659 case LTTNG_CONSUMER32_UST
:
660 case LTTNG_CONSUMER64_UST
:
661 lttng_ustconsumer_del_channel(channel
);
664 ERR("Unknown consumer_data type");
670 iter
.iter
.node
= &channel
->node
.node
;
671 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
675 if (channel
->mmap_base
!= NULL
) {
676 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
681 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
682 ret
= close(channel
->wait_fd
);
687 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
688 ret
= close(channel
->shm_fd
);
694 call_rcu(&channel
->node
.head
, consumer_free_channel
);
696 pthread_mutex_unlock(&consumer_data
.lock
);
699 struct lttng_consumer_channel
*consumer_allocate_channel(
701 int shm_fd
, int wait_fd
,
703 uint64_t max_sb_size
,
704 unsigned int nb_init_streams
)
706 struct lttng_consumer_channel
*channel
;
709 channel
= zmalloc(sizeof(*channel
));
710 if (channel
== NULL
) {
711 PERROR("malloc struct lttng_consumer_channel");
714 channel
->key
= channel_key
;
715 channel
->shm_fd
= shm_fd
;
716 channel
->wait_fd
= wait_fd
;
717 channel
->mmap_len
= mmap_len
;
718 channel
->max_sb_size
= max_sb_size
;
719 channel
->refcount
= 0;
720 channel
->nb_init_streams
= nb_init_streams
;
721 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
723 switch (consumer_data
.type
) {
724 case LTTNG_CONSUMER_KERNEL
:
725 channel
->mmap_base
= NULL
;
726 channel
->mmap_len
= 0;
728 case LTTNG_CONSUMER32_UST
:
729 case LTTNG_CONSUMER64_UST
:
730 ret
= lttng_ustconsumer_allocate_channel(channel
);
737 ERR("Unknown consumer_data type");
741 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
742 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
743 (unsigned long long) channel
->mmap_len
,
744 (unsigned long long) channel
->max_sb_size
);
750 * Add a channel to the global list protected by a mutex.
752 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
754 struct lttng_ht_node_ulong
*node
;
755 struct lttng_ht_iter iter
;
757 pthread_mutex_lock(&consumer_data
.lock
);
758 /* Steal channel identifier, for UST */
759 consumer_steal_channel_key(channel
->key
);
762 lttng_ht_lookup(consumer_data
.channel_ht
,
763 (void *)((unsigned long) channel
->key
), &iter
);
764 node
= lttng_ht_iter_get_node_ulong(&iter
);
766 /* Channel already exist. Ignore the insertion */
770 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
774 pthread_mutex_unlock(&consumer_data
.lock
);
780 * Allocate the pollfd structure and the local view of the out fds to avoid
781 * doing a lookup in the linked list and concurrency issues when writing is
782 * needed. Called with consumer_data.lock held.
784 * Returns the number of fds in the structures.
786 int consumer_update_poll_array(
787 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
788 struct lttng_consumer_stream
**local_stream
)
791 struct lttng_ht_iter iter
;
792 struct lttng_consumer_stream
*stream
;
794 DBG("Updating poll fd array");
796 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, stream
,
798 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
801 DBG("Active FD %d", stream
->wait_fd
);
802 (*pollfd
)[i
].fd
= stream
->wait_fd
;
803 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
804 local_stream
[i
] = stream
;
810 * Insert the consumer_poll_pipe at the end of the array and don't
811 * increment i so nb_fd is the number of real FD.
813 (*pollfd
)[i
].fd
= ctx
->consumer_poll_pipe
[0];
814 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
819 * Poll on the should_quit pipe and the command socket return -1 on error and
820 * should exit, 0 if data is available on the command socket
822 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
827 num_rdy
= poll(consumer_sockpoll
, 2, -1);
830 * Restart interrupted system call.
832 if (errno
== EINTR
) {
835 PERROR("Poll error");
838 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
839 DBG("consumer_should_quit wake up");
849 * Set the error socket.
851 void lttng_consumer_set_error_sock(
852 struct lttng_consumer_local_data
*ctx
, int sock
)
854 ctx
->consumer_error_socket
= sock
;
858 * Set the command socket path.
860 void lttng_consumer_set_command_sock_path(
861 struct lttng_consumer_local_data
*ctx
, char *sock
)
863 ctx
->consumer_command_sock_path
= sock
;
867 * Send return code to the session daemon.
868 * If the socket is not defined, we return 0, it is not a fatal error
870 int lttng_consumer_send_error(
871 struct lttng_consumer_local_data
*ctx
, int cmd
)
873 if (ctx
->consumer_error_socket
> 0) {
874 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
875 sizeof(enum lttcomm_sessiond_command
));
882 * Close all the tracefiles and stream fds, should be called when all instances
885 void lttng_consumer_cleanup(void)
887 struct lttng_ht_iter iter
;
888 struct lttng_ht_node_ulong
*node
;
893 * close all outfd. Called when there are no more threads running (after
894 * joining on the threads), no need to protect list iteration with mutex.
896 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, node
,
898 struct lttng_consumer_stream
*stream
=
899 caa_container_of(node
, struct lttng_consumer_stream
, node
);
900 consumer_del_stream(stream
, consumer_data
.stream_ht
);
903 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
905 struct lttng_consumer_channel
*channel
=
906 caa_container_of(node
, struct lttng_consumer_channel
, node
);
907 consumer_del_channel(channel
);
912 lttng_ht_destroy(consumer_data
.stream_ht
);
913 lttng_ht_destroy(consumer_data
.channel_ht
);
917 * Called from signal handler.
919 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
924 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
925 } while (ret
< 0 && errno
== EINTR
);
927 PERROR("write consumer quit");
931 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
934 int outfd
= stream
->out_fd
;
937 * This does a blocking write-and-wait on any page that belongs to the
938 * subbuffer prior to the one we just wrote.
939 * Don't care about error values, as these are just hints and ways to
940 * limit the amount of page cache used.
942 if (orig_offset
< stream
->chan
->max_sb_size
) {
945 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
946 stream
->chan
->max_sb_size
,
947 SYNC_FILE_RANGE_WAIT_BEFORE
948 | SYNC_FILE_RANGE_WRITE
949 | SYNC_FILE_RANGE_WAIT_AFTER
);
951 * Give hints to the kernel about how we access the file:
952 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
955 * We need to call fadvise again after the file grows because the
956 * kernel does not seem to apply fadvise to non-existing parts of the
959 * Call fadvise _after_ having waited for the page writeback to
960 * complete because the dirty page writeback semantic is not well
961 * defined. So it can be expected to lead to lower throughput in
964 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
965 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
969 * Initialise the necessary environnement :
970 * - create a new context
971 * - create the poll_pipe
972 * - create the should_quit pipe (for signal handler)
973 * - create the thread pipe (for splice)
975 * Takes a function pointer as argument, this function is called when data is
976 * available on a buffer. This function is responsible to do the
977 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
978 * buffer configuration and then kernctl_put_next_subbuf at the end.
980 * Returns a pointer to the new context or NULL on error.
982 struct lttng_consumer_local_data
*lttng_consumer_create(
983 enum lttng_consumer_type type
,
984 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
985 struct lttng_consumer_local_data
*ctx
),
986 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
987 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
988 int (*update_stream
)(int stream_key
, uint32_t state
))
991 struct lttng_consumer_local_data
*ctx
;
993 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
994 consumer_data
.type
== type
);
995 consumer_data
.type
= type
;
997 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
999 PERROR("allocating context");
1003 ctx
->consumer_error_socket
= -1;
1004 /* assign the callbacks */
1005 ctx
->on_buffer_ready
= buffer_ready
;
1006 ctx
->on_recv_channel
= recv_channel
;
1007 ctx
->on_recv_stream
= recv_stream
;
1008 ctx
->on_update_stream
= update_stream
;
1010 ret
= pipe(ctx
->consumer_poll_pipe
);
1012 PERROR("Error creating poll pipe");
1013 goto error_poll_pipe
;
1016 /* set read end of the pipe to non-blocking */
1017 ret
= fcntl(ctx
->consumer_poll_pipe
[0], F_SETFL
, O_NONBLOCK
);
1019 PERROR("fcntl O_NONBLOCK");
1020 goto error_poll_fcntl
;
1023 /* set write end of the pipe to non-blocking */
1024 ret
= fcntl(ctx
->consumer_poll_pipe
[1], F_SETFL
, O_NONBLOCK
);
1026 PERROR("fcntl O_NONBLOCK");
1027 goto error_poll_fcntl
;
1030 ret
= pipe(ctx
->consumer_should_quit
);
1032 PERROR("Error creating recv pipe");
1033 goto error_quit_pipe
;
1036 ret
= pipe(ctx
->consumer_thread_pipe
);
1038 PERROR("Error creating thread pipe");
1039 goto error_thread_pipe
;
1042 ret
= utils_create_pipe(ctx
->consumer_metadata_pipe
);
1044 goto error_metadata_pipe
;
1047 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1049 goto error_splice_pipe
;
1055 utils_close_pipe(ctx
->consumer_metadata_pipe
);
1056 error_metadata_pipe
:
1057 utils_close_pipe(ctx
->consumer_thread_pipe
);
1059 for (i
= 0; i
< 2; i
++) {
1062 err
= close(ctx
->consumer_should_quit
[i
]);
1069 for (i
= 0; i
< 2; i
++) {
1072 err
= close(ctx
->consumer_poll_pipe
[i
]);
1084 * Close all fds associated with the instance and free the context.
1086 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1090 ret
= close(ctx
->consumer_error_socket
);
1094 ret
= close(ctx
->consumer_thread_pipe
[0]);
1098 ret
= close(ctx
->consumer_thread_pipe
[1]);
1102 ret
= close(ctx
->consumer_poll_pipe
[0]);
1106 ret
= close(ctx
->consumer_poll_pipe
[1]);
1110 ret
= close(ctx
->consumer_should_quit
[0]);
1114 ret
= close(ctx
->consumer_should_quit
[1]);
1118 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1120 unlink(ctx
->consumer_command_sock_path
);
1125 * Write the metadata stream id on the specified file descriptor.
1127 static int write_relayd_metadata_id(int fd
,
1128 struct lttng_consumer_stream
*stream
,
1129 struct consumer_relayd_sock_pair
*relayd
,
1130 unsigned long padding
)
1133 struct lttcomm_relayd_metadata_payload hdr
;
1135 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1136 hdr
.padding_size
= htobe32(padding
);
1138 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1139 } while (ret
< 0 && errno
== EINTR
);
1141 PERROR("write metadata stream id");
1144 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1145 stream
->relayd_stream_id
, padding
);
1152 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1153 * core function for writing trace buffers to either the local filesystem or
1156 * Careful review MUST be put if any changes occur!
1158 * Returns the number of bytes written
1160 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1161 struct lttng_consumer_local_data
*ctx
,
1162 struct lttng_consumer_stream
*stream
, unsigned long len
,
1163 unsigned long padding
)
1165 unsigned long mmap_offset
;
1166 ssize_t ret
= 0, written
= 0;
1167 off_t orig_offset
= stream
->out_fd_offset
;
1168 /* Default is on the disk */
1169 int outfd
= stream
->out_fd
;
1170 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1172 /* RCU lock for the relayd pointer */
1175 /* Flag that the current stream if set for network streaming. */
1176 if (stream
->net_seq_idx
!= -1) {
1177 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1178 if (relayd
== NULL
) {
1183 /* get the offset inside the fd to mmap */
1184 switch (consumer_data
.type
) {
1185 case LTTNG_CONSUMER_KERNEL
:
1186 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1188 case LTTNG_CONSUMER32_UST
:
1189 case LTTNG_CONSUMER64_UST
:
1190 ret
= lttng_ustctl_get_mmap_read_offset(stream
->chan
->handle
,
1191 stream
->buf
, &mmap_offset
);
1194 ERR("Unknown consumer_data type");
1199 PERROR("tracer ctl get_mmap_read_offset");
1204 /* Handle stream on the relayd if the output is on the network */
1206 unsigned long netlen
= len
;
1209 * Lock the control socket for the complete duration of the function
1210 * since from this point on we will use the socket.
1212 if (stream
->metadata_flag
) {
1213 /* Metadata requires the control socket. */
1214 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1215 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1218 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1220 /* Use the returned socket. */
1223 /* Write metadata stream id before payload */
1224 if (stream
->metadata_flag
) {
1225 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1232 /* Else, use the default set before which is the filesystem. */
1234 /* No streaming, we have to set the len with the full padding */
1240 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
1241 } while (ret
< 0 && errno
== EINTR
);
1242 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1244 PERROR("Error in file write");
1249 } else if (ret
> len
) {
1250 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1258 /* This call is useless on a socket so better save a syscall. */
1260 /* This won't block, but will start writeout asynchronously */
1261 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1262 SYNC_FILE_RANGE_WRITE
);
1263 stream
->out_fd_offset
+= ret
;
1267 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1270 /* Unlock only if ctrl socket used */
1271 if (relayd
&& stream
->metadata_flag
) {
1272 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1280 * Splice the data from the ring buffer to the tracefile.
1282 * Returns the number of bytes spliced.
1284 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1285 struct lttng_consumer_local_data
*ctx
,
1286 struct lttng_consumer_stream
*stream
, unsigned long len
,
1287 unsigned long padding
)
1289 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1291 off_t orig_offset
= stream
->out_fd_offset
;
1292 int fd
= stream
->wait_fd
;
1293 /* Default is on the disk */
1294 int outfd
= stream
->out_fd
;
1295 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1298 switch (consumer_data
.type
) {
1299 case LTTNG_CONSUMER_KERNEL
:
1301 case LTTNG_CONSUMER32_UST
:
1302 case LTTNG_CONSUMER64_UST
:
1303 /* Not supported for user space tracing */
1306 ERR("Unknown consumer_data type");
1310 /* RCU lock for the relayd pointer */
1313 /* Flag that the current stream if set for network streaming. */
1314 if (stream
->net_seq_idx
!= -1) {
1315 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1316 if (relayd
== NULL
) {
1322 * Choose right pipe for splice. Metadata and trace data are handled by
1323 * different threads hence the use of two pipes in order not to race or
1324 * corrupt the written data.
1326 if (stream
->metadata_flag
) {
1327 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1329 splice_pipe
= ctx
->consumer_thread_pipe
;
1332 /* Write metadata stream id before payload */
1334 int total_len
= len
;
1336 if (stream
->metadata_flag
) {
1338 * Lock the control socket for the complete duration of the function
1339 * since from this point on we will use the socket.
1341 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1343 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1350 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1353 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1355 /* Use the returned socket. */
1358 ERR("Remote relayd disconnected. Stopping");
1362 /* No streaming, we have to set the len with the full padding */
1367 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1368 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1369 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1370 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1371 DBG("splice chan to pipe, ret %zd", ret_splice
);
1372 if (ret_splice
< 0) {
1373 PERROR("Error in relay splice");
1375 written
= ret_splice
;
1381 /* Handle stream on the relayd if the output is on the network */
1383 if (stream
->metadata_flag
) {
1384 size_t metadata_payload_size
=
1385 sizeof(struct lttcomm_relayd_metadata_payload
);
1387 /* Update counter to fit the spliced data */
1388 ret_splice
+= metadata_payload_size
;
1389 len
+= metadata_payload_size
;
1391 * We do this so the return value can match the len passed as
1392 * argument to this function.
1394 written
-= metadata_payload_size
;
1398 /* Splice data out */
1399 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1400 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1401 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1402 if (ret_splice
< 0) {
1403 PERROR("Error in file splice");
1405 written
= ret_splice
;
1409 } else if (ret_splice
> len
) {
1411 PERROR("Wrote more data than requested %zd (len: %lu)",
1413 written
+= ret_splice
;
1419 /* This call is useless on a socket so better save a syscall. */
1421 /* This won't block, but will start writeout asynchronously */
1422 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1423 SYNC_FILE_RANGE_WRITE
);
1424 stream
->out_fd_offset
+= ret_splice
;
1426 written
+= ret_splice
;
1428 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1435 /* send the appropriate error description to sessiond */
1438 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EBADF
);
1441 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1444 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1447 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1452 if (relayd
&& stream
->metadata_flag
) {
1453 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1461 * Take a snapshot for a specific fd
1463 * Returns 0 on success, < 0 on error
1465 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1466 struct lttng_consumer_stream
*stream
)
1468 switch (consumer_data
.type
) {
1469 case LTTNG_CONSUMER_KERNEL
:
1470 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1471 case LTTNG_CONSUMER32_UST
:
1472 case LTTNG_CONSUMER64_UST
:
1473 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1475 ERR("Unknown consumer_data type");
1483 * Get the produced position
1485 * Returns 0 on success, < 0 on error
1487 int lttng_consumer_get_produced_snapshot(
1488 struct lttng_consumer_local_data
*ctx
,
1489 struct lttng_consumer_stream
*stream
,
1492 switch (consumer_data
.type
) {
1493 case LTTNG_CONSUMER_KERNEL
:
1494 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1495 case LTTNG_CONSUMER32_UST
:
1496 case LTTNG_CONSUMER64_UST
:
1497 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1499 ERR("Unknown consumer_data type");
1505 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1506 int sock
, struct pollfd
*consumer_sockpoll
)
1508 switch (consumer_data
.type
) {
1509 case LTTNG_CONSUMER_KERNEL
:
1510 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1511 case LTTNG_CONSUMER32_UST
:
1512 case LTTNG_CONSUMER64_UST
:
1513 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1515 ERR("Unknown consumer_data type");
1522 * Iterate over all streams of the hashtable and free them properly.
1524 * XXX: Should not be only for metadata stream or else use an other name.
1526 static void destroy_stream_ht(struct lttng_ht
*ht
)
1529 struct lttng_ht_iter iter
;
1530 struct lttng_consumer_stream
*stream
;
1537 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, waitfd_node
.node
) {
1538 ret
= lttng_ht_del(ht
, &iter
);
1541 call_rcu(&stream
->waitfd_node
.head
, consumer_free_metadata_stream
);
1545 lttng_ht_destroy(ht
);
1549 * Clean up a metadata stream and free its memory.
1551 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1552 struct lttng_ht
*ht
)
1555 struct lttng_ht_iter iter
;
1556 struct lttng_consumer_channel
*free_chan
= NULL
;
1557 struct consumer_relayd_sock_pair
*relayd
;
1561 * This call should NEVER receive regular stream. It must always be
1562 * metadata stream and this is crucial for data structure synchronization.
1564 assert(stream
->metadata_flag
);
1566 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1569 /* Means the stream was allocated but not successfully added */
1573 pthread_mutex_lock(&consumer_data
.lock
);
1574 switch (consumer_data
.type
) {
1575 case LTTNG_CONSUMER_KERNEL
:
1576 if (stream
->mmap_base
!= NULL
) {
1577 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1579 PERROR("munmap metadata stream");
1583 case LTTNG_CONSUMER32_UST
:
1584 case LTTNG_CONSUMER64_UST
:
1585 lttng_ustconsumer_del_stream(stream
);
1588 ERR("Unknown consumer_data type");
1594 iter
.iter
.node
= &stream
->waitfd_node
.node
;
1595 ret
= lttng_ht_del(ht
, &iter
);
1599 if (stream
->out_fd
>= 0) {
1600 ret
= close(stream
->out_fd
);
1606 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
1607 ret
= close(stream
->wait_fd
);
1613 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
1614 ret
= close(stream
->shm_fd
);
1620 /* Check and cleanup relayd */
1622 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1623 if (relayd
!= NULL
) {
1624 uatomic_dec(&relayd
->refcount
);
1625 assert(uatomic_read(&relayd
->refcount
) >= 0);
1627 /* Closing streams requires to lock the control socket. */
1628 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1629 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1630 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1631 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1633 DBG("Unable to close stream on the relayd. Continuing");
1635 * Continue here. There is nothing we can do for the relayd.
1636 * Chances are that the relayd has closed the socket so we just
1637 * continue cleaning up.
1641 /* Both conditions are met, we destroy the relayd. */
1642 if (uatomic_read(&relayd
->refcount
) == 0 &&
1643 uatomic_read(&relayd
->destroy_flag
)) {
1644 destroy_relayd(relayd
);
1649 /* Atomically decrement channel refcount since other threads can use it. */
1650 uatomic_dec(&stream
->chan
->refcount
);
1651 if (!uatomic_read(&stream
->chan
->refcount
)
1652 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
1653 /* Go for channel deletion! */
1654 free_chan
= stream
->chan
;
1658 pthread_mutex_unlock(&consumer_data
.lock
);
1661 consumer_del_channel(free_chan
);
1665 call_rcu(&stream
->waitfd_node
.head
, consumer_free_metadata_stream
);
1669 * Action done with the metadata stream when adding it to the consumer internal
1670 * data structures to handle it.
1672 static int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
,
1673 struct lttng_ht
*ht
)
1676 struct consumer_relayd_sock_pair
*relayd
;
1681 DBG3("Adding metadata stream %d to hash table", stream
->wait_fd
);
1683 pthread_mutex_lock(&consumer_data
.lock
);
1686 * From here, refcounts are updated so be _careful_ when returning an error
1691 /* Find relayd and, if one is found, increment refcount. */
1692 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1693 if (relayd
!= NULL
) {
1694 uatomic_inc(&relayd
->refcount
);
1697 /* Update channel refcount once added without error(s). */
1698 uatomic_inc(&stream
->chan
->refcount
);
1701 * When nb_init_streams reaches 0, we don't need to trigger any action in
1702 * terms of destroying the associated channel, because the action that
1703 * causes the count to become 0 also causes a stream to be added. The
1704 * channel deletion will thus be triggered by the following removal of this
1707 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
1708 uatomic_dec(&stream
->chan
->nb_init_streams
);
1711 lttng_ht_add_unique_ulong(ht
, &stream
->waitfd_node
);
1714 pthread_mutex_unlock(&consumer_data
.lock
);
1719 * Thread polls on metadata file descriptor and write them on disk or on the
1722 void *consumer_thread_metadata_poll(void *data
)
1725 uint32_t revents
, nb_fd
;
1726 struct lttng_consumer_stream
*stream
= NULL
;
1727 struct lttng_ht_iter iter
;
1728 struct lttng_ht_node_ulong
*node
;
1729 struct lttng_ht
*metadata_ht
= NULL
;
1730 struct lttng_poll_event events
;
1731 struct lttng_consumer_local_data
*ctx
= data
;
1734 rcu_register_thread();
1736 DBG("Thread metadata poll started");
1738 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1739 if (metadata_ht
== NULL
) {
1743 /* Size is set to 1 for the consumer_metadata pipe */
1744 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
1746 ERR("Poll set creation failed");
1750 ret
= lttng_poll_add(&events
, ctx
->consumer_metadata_pipe
[0], LPOLLIN
);
1756 DBG("Metadata main loop started");
1759 lttng_poll_reset(&events
);
1761 nb_fd
= LTTNG_POLL_GETNB(&events
);
1763 /* Only the metadata pipe is set */
1764 if (nb_fd
== 0 && consumer_quit
== 1) {
1769 DBG("Metadata poll wait with %d fd(s)", nb_fd
);
1770 ret
= lttng_poll_wait(&events
, -1);
1771 DBG("Metadata event catched in thread");
1773 if (errno
== EINTR
) {
1774 ERR("Poll EINTR catched");
1780 /* From here, the event is a metadata wait fd */
1781 for (i
= 0; i
< nb_fd
; i
++) {
1782 revents
= LTTNG_POLL_GETEV(&events
, i
);
1783 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1785 /* Just don't waste time if no returned events for the fd */
1790 if (pollfd
== ctx
->consumer_metadata_pipe
[0]) {
1791 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
1792 DBG("Metadata thread pipe hung up");
1794 * Remove the pipe from the poll set and continue the loop
1795 * since their might be data to consume.
1797 lttng_poll_del(&events
, ctx
->consumer_metadata_pipe
[0]);
1798 close(ctx
->consumer_metadata_pipe
[0]);
1800 } else if (revents
& LPOLLIN
) {
1802 /* Get the stream pointer received */
1803 ret
= read(pollfd
, &stream
, sizeof(stream
));
1804 } while (ret
< 0 && errno
== EINTR
);
1806 ret
< sizeof(struct lttng_consumer_stream
*)) {
1807 PERROR("read metadata stream");
1809 * Let's continue here and hope we can still work
1810 * without stopping the consumer. XXX: Should we?
1815 DBG("Adding metadata stream %d to poll set",
1818 ret
= consumer_add_metadata_stream(stream
, metadata_ht
);
1820 ERR("Unable to add metadata stream");
1821 /* Stream was not setup properly. Continuing. */
1822 consumer_del_metadata_stream(stream
, NULL
);
1826 /* Add metadata stream to the global poll events list */
1827 lttng_poll_add(&events
, stream
->wait_fd
,
1828 LPOLLIN
| LPOLLPRI
);
1831 /* Handle other stream */
1836 lttng_ht_lookup(metadata_ht
, (void *)((unsigned long) pollfd
),
1838 node
= lttng_ht_iter_get_node_ulong(&iter
);
1841 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
1844 /* Check for error event */
1845 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
1846 DBG("Metadata fd %d is hup|err.", pollfd
);
1847 if (!stream
->hangup_flush_done
1848 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1849 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1850 DBG("Attempting to flush and consume the UST buffers");
1851 lttng_ustconsumer_on_stream_hangup(stream
);
1853 /* We just flushed the stream now read it. */
1855 len
= ctx
->on_buffer_ready(stream
, ctx
);
1857 * We don't check the return value here since if we get
1858 * a negative len, it means an error occured thus we
1859 * simply remove it from the poll set and free the
1865 lttng_poll_del(&events
, stream
->wait_fd
);
1867 * This call update the channel states, closes file descriptors
1868 * and securely free the stream.
1870 consumer_del_metadata_stream(stream
, metadata_ht
);
1871 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
1872 /* Get the data out of the metadata file descriptor */
1873 DBG("Metadata available on fd %d", pollfd
);
1874 assert(stream
->wait_fd
== pollfd
);
1876 len
= ctx
->on_buffer_ready(stream
, ctx
);
1877 /* It's ok to have an unavailable sub-buffer */
1878 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
1881 } else if (len
> 0) {
1882 stream
->data_read
= 1;
1886 /* Release RCU lock for the stream looked up */
1893 DBG("Metadata poll thread exiting");
1894 lttng_poll_clean(&events
);
1897 destroy_stream_ht(metadata_ht
);
1900 rcu_unregister_thread();
1905 * This thread polls the fds in the set to consume the data and write
1906 * it to tracefile if necessary.
1908 void *consumer_thread_data_poll(void *data
)
1910 int num_rdy
, num_hup
, high_prio
, ret
, i
;
1911 struct pollfd
*pollfd
= NULL
;
1912 /* local view of the streams */
1913 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
1914 /* local view of consumer_data.fds_count */
1916 struct lttng_consumer_local_data
*ctx
= data
;
1919 rcu_register_thread();
1921 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
1928 * the fds set has been updated, we need to update our
1929 * local array as well
1931 pthread_mutex_lock(&consumer_data
.lock
);
1932 if (consumer_data
.need_update
) {
1933 if (pollfd
!= NULL
) {
1937 if (local_stream
!= NULL
) {
1939 local_stream
= NULL
;
1942 /* allocate for all fds + 1 for the consumer_poll_pipe */
1943 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
1944 if (pollfd
== NULL
) {
1945 PERROR("pollfd malloc");
1946 pthread_mutex_unlock(&consumer_data
.lock
);
1950 /* allocate for all fds + 1 for the consumer_poll_pipe */
1951 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
1952 sizeof(struct lttng_consumer_stream
));
1953 if (local_stream
== NULL
) {
1954 PERROR("local_stream malloc");
1955 pthread_mutex_unlock(&consumer_data
.lock
);
1958 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
);
1960 ERR("Error in allocating pollfd or local_outfds");
1961 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
1962 pthread_mutex_unlock(&consumer_data
.lock
);
1966 consumer_data
.need_update
= 0;
1968 pthread_mutex_unlock(&consumer_data
.lock
);
1970 /* No FDs and consumer_quit, consumer_cleanup the thread */
1971 if (nb_fd
== 0 && consumer_quit
== 1) {
1974 /* poll on the array of fds */
1976 DBG("polling on %d fd", nb_fd
+ 1);
1977 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
1978 DBG("poll num_rdy : %d", num_rdy
);
1979 if (num_rdy
== -1) {
1981 * Restart interrupted system call.
1983 if (errno
== EINTR
) {
1986 PERROR("Poll error");
1987 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
1989 } else if (num_rdy
== 0) {
1990 DBG("Polling thread timed out");
1995 * If the consumer_poll_pipe triggered poll go directly to the
1996 * beginning of the loop to update the array. We want to prioritize
1997 * array update over low-priority reads.
1999 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2000 size_t pipe_readlen
;
2002 DBG("consumer_poll_pipe wake up");
2003 /* Consume 1 byte of pipe data */
2005 pipe_readlen
= read(ctx
->consumer_poll_pipe
[0], &new_stream
,
2006 sizeof(new_stream
));
2007 } while (pipe_readlen
== -1 && errno
== EINTR
);
2010 * If the stream is NULL, just ignore it. It's also possible that
2011 * the sessiond poll thread changed the consumer_quit state and is
2012 * waking us up to test it.
2014 if (new_stream
== NULL
) {
2018 ret
= consumer_add_stream(new_stream
);
2020 ERR("Consumer add stream %d failed. Continuing",
2023 * At this point, if the add_stream fails, it is not in the
2024 * hash table thus passing the NULL value here.
2026 consumer_del_stream(new_stream
, NULL
);
2029 /* Continue to update the local streams and handle prio ones */
2033 /* Take care of high priority channels first. */
2034 for (i
= 0; i
< nb_fd
; i
++) {
2035 if (pollfd
[i
].revents
& POLLPRI
) {
2036 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2038 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2039 /* it's ok to have an unavailable sub-buffer */
2040 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2042 } else if (len
> 0) {
2043 local_stream
[i
]->data_read
= 1;
2049 * If we read high prio channel in this loop, try again
2050 * for more high prio data.
2056 /* Take care of low priority channels. */
2057 for (i
= 0; i
< nb_fd
; i
++) {
2058 if ((pollfd
[i
].revents
& POLLIN
) ||
2059 local_stream
[i
]->hangup_flush_done
) {
2060 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2061 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2062 /* it's ok to have an unavailable sub-buffer */
2063 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2065 } else if (len
> 0) {
2066 local_stream
[i
]->data_read
= 1;
2071 /* Handle hangup and errors */
2072 for (i
= 0; i
< nb_fd
; i
++) {
2073 if (!local_stream
[i
]->hangup_flush_done
2074 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2075 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2076 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2077 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2079 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2080 /* Attempt read again, for the data we just flushed. */
2081 local_stream
[i
]->data_read
= 1;
2084 * If the poll flag is HUP/ERR/NVAL and we have
2085 * read no data in this pass, we can remove the
2086 * stream from its hash table.
2088 if ((pollfd
[i
].revents
& POLLHUP
)) {
2089 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2090 if (!local_stream
[i
]->data_read
) {
2091 consumer_del_stream(local_stream
[i
],
2092 consumer_data
.stream_ht
);
2095 } else if (pollfd
[i
].revents
& POLLERR
) {
2096 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2097 if (!local_stream
[i
]->data_read
) {
2098 consumer_del_stream(local_stream
[i
],
2099 consumer_data
.stream_ht
);
2102 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2103 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2104 if (!local_stream
[i
]->data_read
) {
2105 consumer_del_stream(local_stream
[i
],
2106 consumer_data
.stream_ht
);
2110 local_stream
[i
]->data_read
= 0;
2114 DBG("polling thread exiting");
2115 if (pollfd
!= NULL
) {
2119 if (local_stream
!= NULL
) {
2121 local_stream
= NULL
;
2125 * Close the write side of the pipe so epoll_wait() in
2126 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2127 * read side of the pipe. If we close them both, epoll_wait strangely does
2128 * not return and could create a endless wait period if the pipe is the
2129 * only tracked fd in the poll set. The thread will take care of closing
2132 close(ctx
->consumer_metadata_pipe
[1]);
2134 rcu_unregister_thread();
2139 * This thread listens on the consumerd socket and receives the file
2140 * descriptors from the session daemon.
2142 void *consumer_thread_sessiond_poll(void *data
)
2144 int sock
, client_socket
, ret
;
2146 * structure to poll for incoming data on communication socket avoids
2147 * making blocking sockets.
2149 struct pollfd consumer_sockpoll
[2];
2150 struct lttng_consumer_local_data
*ctx
= data
;
2152 rcu_register_thread();
2154 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2155 unlink(ctx
->consumer_command_sock_path
);
2156 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2157 if (client_socket
< 0) {
2158 ERR("Cannot create command socket");
2162 ret
= lttcomm_listen_unix_sock(client_socket
);
2167 DBG("Sending ready command to lttng-sessiond");
2168 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2169 /* return < 0 on error, but == 0 is not fatal */
2171 ERR("Error sending ready command to lttng-sessiond");
2175 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2177 PERROR("fcntl O_NONBLOCK");
2181 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2182 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2183 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2184 consumer_sockpoll
[1].fd
= client_socket
;
2185 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2187 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2190 DBG("Connection on client_socket");
2192 /* Blocking call, waiting for transmission */
2193 sock
= lttcomm_accept_unix_sock(client_socket
);
2198 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2200 PERROR("fcntl O_NONBLOCK");
2204 /* update the polling structure to poll on the established socket */
2205 consumer_sockpoll
[1].fd
= sock
;
2206 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2209 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2212 DBG("Incoming command on sock");
2213 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2214 if (ret
== -ENOENT
) {
2215 DBG("Received STOP command");
2220 * This could simply be a session daemon quitting. Don't output
2223 DBG("Communication interrupted on command socket");
2226 if (consumer_quit
) {
2227 DBG("consumer_thread_receive_fds received quit from signal");
2230 DBG("received fds on sock");
2233 DBG("consumer_thread_receive_fds exiting");
2236 * when all fds have hung up, the polling thread
2242 * 2s of grace period, if no polling events occur during
2243 * this period, the polling thread will exit even if there
2244 * are still open FDs (should not happen, but safety mechanism).
2246 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
2249 * Notify the data poll thread to poll back again and test the
2250 * consumer_quit state to quit gracefully.
2253 struct lttng_consumer_stream
*null_stream
= NULL
;
2255 ret
= write(ctx
->consumer_poll_pipe
[1], &null_stream
,
2256 sizeof(null_stream
));
2257 } while (ret
< 0 && errno
== EINTR
);
2259 rcu_unregister_thread();
2263 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2264 struct lttng_consumer_local_data
*ctx
)
2266 switch (consumer_data
.type
) {
2267 case LTTNG_CONSUMER_KERNEL
:
2268 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
2269 case LTTNG_CONSUMER32_UST
:
2270 case LTTNG_CONSUMER64_UST
:
2271 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2273 ERR("Unknown consumer_data type");
2279 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2281 switch (consumer_data
.type
) {
2282 case LTTNG_CONSUMER_KERNEL
:
2283 return lttng_kconsumer_on_recv_stream(stream
);
2284 case LTTNG_CONSUMER32_UST
:
2285 case LTTNG_CONSUMER64_UST
:
2286 return lttng_ustconsumer_on_recv_stream(stream
);
2288 ERR("Unknown consumer_data type");
2295 * Allocate and set consumer data hash tables.
2297 void lttng_consumer_init(void)
2299 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2300 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2301 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2305 * Process the ADD_RELAYD command receive by a consumer.
2307 * This will create a relayd socket pair and add it to the relayd hash table.
2308 * The caller MUST acquire a RCU read side lock before calling it.
2310 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
2311 struct lttng_consumer_local_data
*ctx
, int sock
,
2312 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
)
2315 struct consumer_relayd_sock_pair
*relayd
;
2317 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
2319 /* Get relayd reference if exists. */
2320 relayd
= consumer_find_relayd(net_seq_idx
);
2321 if (relayd
== NULL
) {
2322 /* Not found. Allocate one. */
2323 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
2324 if (relayd
== NULL
) {
2325 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
2330 /* Poll on consumer socket. */
2331 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2336 /* Get relayd socket from session daemon */
2337 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2338 if (ret
!= sizeof(fd
)) {
2339 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
2344 /* Copy socket information and received FD */
2345 switch (sock_type
) {
2346 case LTTNG_STREAM_CONTROL
:
2347 /* Copy received lttcomm socket */
2348 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
2349 ret
= lttcomm_create_sock(&relayd
->control_sock
);
2354 /* Close the created socket fd which is useless */
2355 close(relayd
->control_sock
.fd
);
2357 /* Assign new file descriptor */
2358 relayd
->control_sock
.fd
= fd
;
2360 case LTTNG_STREAM_DATA
:
2361 /* Copy received lttcomm socket */
2362 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
2363 ret
= lttcomm_create_sock(&relayd
->data_sock
);
2368 /* Close the created socket fd which is useless */
2369 close(relayd
->data_sock
.fd
);
2371 /* Assign new file descriptor */
2372 relayd
->data_sock
.fd
= fd
;
2375 ERR("Unknown relayd socket type (%d)", sock_type
);
2379 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2380 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
2381 relayd
->net_seq_idx
, fd
);
2384 * Add relayd socket pair to consumer data hashtable. If object already
2385 * exists or on error, the function gracefully returns.