2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2012 - David Goulet <dgoulet@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/socket.h>
28 #include <sys/types.h>
32 #include <common/common.h>
33 #include <common/utils.h>
34 #include <common/compat/poll.h>
35 #include <common/kernel-ctl/kernel-ctl.h>
36 #include <common/sessiond-comm/relayd.h>
37 #include <common/sessiond-comm/sessiond-comm.h>
38 #include <common/kernel-consumer/kernel-consumer.h>
39 #include <common/relayd/relayd.h>
40 #include <common/ust-consumer/ust-consumer.h>
44 struct lttng_consumer_global_data consumer_data
= {
47 .type
= LTTNG_CONSUMER_UNKNOWN
,
50 /* timeout parameter, to control the polling thread grace period. */
51 int consumer_poll_timeout
= -1;
54 * Flag to inform the polling thread to quit when all fd hung up. Updated by
55 * the consumer_thread_receive_fds when it notices that all fds has hung up.
56 * Also updated by the signal handler (consumer_should_exit()). Read by the
59 volatile int consumer_quit
= 0;
62 * The following two hash tables are visible by all threads which are separated
63 * in different source files.
65 * Global hash table containing respectively metadata and data streams. The
66 * stream element in this ht should only be updated by the metadata poll thread
67 * for the metadata and the data poll thread for the data.
69 struct lttng_ht
*metadata_ht
= NULL
;
70 struct lttng_ht
*data_ht
= NULL
;
73 * Find a stream. The consumer_data.lock must be locked during this
76 static struct lttng_consumer_stream
*consumer_find_stream(int key
,
79 struct lttng_ht_iter iter
;
80 struct lttng_ht_node_ulong
*node
;
81 struct lttng_consumer_stream
*stream
= NULL
;
85 /* Negative keys are lookup failures */
92 lttng_ht_lookup(ht
, (void *)((unsigned long) key
), &iter
);
93 node
= lttng_ht_iter_get_node_ulong(&iter
);
95 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
103 void consumer_steal_stream_key(int key
, struct lttng_ht
*ht
)
105 struct lttng_consumer_stream
*stream
;
108 stream
= consumer_find_stream(key
, ht
);
112 * We don't want the lookup to match, but we still need
113 * to iterate on this stream when iterating over the hash table. Just
114 * change the node key.
116 stream
->node
.key
= -1;
121 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
123 struct lttng_ht_iter iter
;
124 struct lttng_ht_node_ulong
*node
;
125 struct lttng_consumer_channel
*channel
= NULL
;
127 /* Negative keys are lookup failures */
134 lttng_ht_lookup(consumer_data
.channel_ht
, (void *)((unsigned long) key
),
136 node
= lttng_ht_iter_get_node_ulong(&iter
);
138 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
146 static void consumer_steal_channel_key(int key
)
148 struct lttng_consumer_channel
*channel
;
151 channel
= consumer_find_channel(key
);
155 * We don't want the lookup to match, but we still need
156 * to iterate on this channel when iterating over the hash table. Just
157 * change the node key.
159 channel
->node
.key
= -1;
165 void consumer_free_stream(struct rcu_head
*head
)
167 struct lttng_ht_node_ulong
*node
=
168 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
169 struct lttng_consumer_stream
*stream
=
170 caa_container_of(node
, struct lttng_consumer_stream
, node
);
176 * 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';
411 * Index differently the metadata node because the thread is using an
412 * internal hash table to match streams in the metadata_ht to the epoll set
416 lttng_ht_node_init_ulong(&stream
->node
, stream
->wait_fd
);
418 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
422 * The cpu number is needed before using any ustctl_* actions. Ignored for
423 * the kernel so the value does not matter.
425 pthread_mutex_lock(&consumer_data
.lock
);
426 stream
->cpu
= stream
->chan
->cpucount
++;
427 pthread_mutex_unlock(&consumer_data
.lock
);
429 DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu,"
430 " out_fd %d, net_seq_idx %d)", stream
->path_name
, stream
->key
,
431 stream
->shm_fd
, stream
->wait_fd
,
432 (unsigned long long) stream
->mmap_len
, stream
->out_fd
,
433 stream
->net_seq_idx
);
443 * Add a stream to the global list protected by a mutex.
445 static int consumer_add_stream(struct lttng_consumer_stream
*stream
,
449 struct consumer_relayd_sock_pair
*relayd
;
454 DBG3("Adding consumer stream %d", stream
->key
);
456 pthread_mutex_lock(&consumer_data
.lock
);
459 /* Steal stream identifier to avoid having streams with the same key */
460 consumer_steal_stream_key(stream
->key
, ht
);
462 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
464 /* Check and cleanup relayd */
465 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
466 if (relayd
!= NULL
) {
467 uatomic_inc(&relayd
->refcount
);
470 /* Update channel refcount once added without error(s). */
471 uatomic_inc(&stream
->chan
->refcount
);
474 * When nb_init_streams reaches 0, we don't need to trigger any action in
475 * terms of destroying the associated channel, because the action that
476 * causes the count to become 0 also causes a stream to be added. The
477 * channel deletion will thus be triggered by the following removal of this
480 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
481 uatomic_dec(&stream
->chan
->nb_init_streams
);
484 /* Update consumer data once the node is inserted. */
485 consumer_data
.stream_count
++;
486 consumer_data
.need_update
= 1;
489 pthread_mutex_unlock(&consumer_data
.lock
);
495 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
496 * be acquired before calling this.
498 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
501 struct lttng_ht_node_ulong
*node
;
502 struct lttng_ht_iter iter
;
504 if (relayd
== NULL
) {
509 lttng_ht_lookup(consumer_data
.relayd_ht
,
510 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
511 node
= lttng_ht_iter_get_node_ulong(&iter
);
513 /* Relayd already exist. Ignore the insertion */
516 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
523 * Allocate and return a consumer relayd socket.
525 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
528 struct consumer_relayd_sock_pair
*obj
= NULL
;
530 /* Negative net sequence index is a failure */
531 if (net_seq_idx
< 0) {
535 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
537 PERROR("zmalloc relayd sock");
541 obj
->net_seq_idx
= net_seq_idx
;
543 obj
->destroy_flag
= 0;
544 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
545 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
552 * Find a relayd socket pair in the global consumer data.
554 * Return the object if found else NULL.
555 * RCU read-side lock must be held across this call and while using the
558 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
560 struct lttng_ht_iter iter
;
561 struct lttng_ht_node_ulong
*node
;
562 struct consumer_relayd_sock_pair
*relayd
= NULL
;
564 /* Negative keys are lookup failures */
569 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
571 node
= lttng_ht_iter_get_node_ulong(&iter
);
573 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
581 * Handle stream for relayd transmission if the stream applies for network
582 * streaming where the net sequence index is set.
584 * Return destination file descriptor or negative value on error.
586 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
587 size_t data_size
, unsigned long padding
,
588 struct consumer_relayd_sock_pair
*relayd
)
591 struct lttcomm_relayd_data_hdr data_hdr
;
597 /* Reset data header */
598 memset(&data_hdr
, 0, sizeof(data_hdr
));
600 if (stream
->metadata_flag
) {
601 /* Caller MUST acquire the relayd control socket lock */
602 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
607 /* Metadata are always sent on the control socket. */
608 outfd
= relayd
->control_sock
.fd
;
610 /* Set header with stream information */
611 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
612 data_hdr
.data_size
= htobe32(data_size
);
613 data_hdr
.padding_size
= htobe32(padding
);
614 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
615 /* Other fields are zeroed previously */
617 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
623 /* Set to go on data socket */
624 outfd
= relayd
->data_sock
.fd
;
632 * Update a stream according to what we just received.
634 void consumer_change_stream_state(int stream_key
,
635 enum lttng_consumer_stream_state state
)
637 struct lttng_consumer_stream
*stream
;
639 pthread_mutex_lock(&consumer_data
.lock
);
640 stream
= consumer_find_stream(stream_key
, consumer_data
.stream_ht
);
642 stream
->state
= state
;
644 consumer_data
.need_update
= 1;
645 pthread_mutex_unlock(&consumer_data
.lock
);
649 void consumer_free_channel(struct rcu_head
*head
)
651 struct lttng_ht_node_ulong
*node
=
652 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
653 struct lttng_consumer_channel
*channel
=
654 caa_container_of(node
, struct lttng_consumer_channel
, node
);
660 * Remove a channel from the global list protected by a mutex. This
661 * function is also responsible for freeing its data structures.
663 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
666 struct lttng_ht_iter iter
;
668 pthread_mutex_lock(&consumer_data
.lock
);
670 switch (consumer_data
.type
) {
671 case LTTNG_CONSUMER_KERNEL
:
673 case LTTNG_CONSUMER32_UST
:
674 case LTTNG_CONSUMER64_UST
:
675 lttng_ustconsumer_del_channel(channel
);
678 ERR("Unknown consumer_data type");
684 iter
.iter
.node
= &channel
->node
.node
;
685 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
689 if (channel
->mmap_base
!= NULL
) {
690 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
695 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
696 ret
= close(channel
->wait_fd
);
701 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
702 ret
= close(channel
->shm_fd
);
708 call_rcu(&channel
->node
.head
, consumer_free_channel
);
710 pthread_mutex_unlock(&consumer_data
.lock
);
713 struct lttng_consumer_channel
*consumer_allocate_channel(
715 int shm_fd
, int wait_fd
,
717 uint64_t max_sb_size
,
718 unsigned int nb_init_streams
)
720 struct lttng_consumer_channel
*channel
;
723 channel
= zmalloc(sizeof(*channel
));
724 if (channel
== NULL
) {
725 PERROR("malloc struct lttng_consumer_channel");
728 channel
->key
= channel_key
;
729 channel
->shm_fd
= shm_fd
;
730 channel
->wait_fd
= wait_fd
;
731 channel
->mmap_len
= mmap_len
;
732 channel
->max_sb_size
= max_sb_size
;
733 channel
->refcount
= 0;
734 channel
->nb_init_streams
= nb_init_streams
;
735 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
737 switch (consumer_data
.type
) {
738 case LTTNG_CONSUMER_KERNEL
:
739 channel
->mmap_base
= NULL
;
740 channel
->mmap_len
= 0;
742 case LTTNG_CONSUMER32_UST
:
743 case LTTNG_CONSUMER64_UST
:
744 ret
= lttng_ustconsumer_allocate_channel(channel
);
751 ERR("Unknown consumer_data type");
755 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
756 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
757 (unsigned long long) channel
->mmap_len
,
758 (unsigned long long) channel
->max_sb_size
);
764 * Add a channel to the global list protected by a mutex.
766 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
768 struct lttng_ht_node_ulong
*node
;
769 struct lttng_ht_iter iter
;
771 pthread_mutex_lock(&consumer_data
.lock
);
772 /* Steal channel identifier, for UST */
773 consumer_steal_channel_key(channel
->key
);
776 lttng_ht_lookup(consumer_data
.channel_ht
,
777 (void *)((unsigned long) channel
->key
), &iter
);
778 node
= lttng_ht_iter_get_node_ulong(&iter
);
780 /* Channel already exist. Ignore the insertion */
784 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
788 pthread_mutex_unlock(&consumer_data
.lock
);
794 * Allocate the pollfd structure and the local view of the out fds to avoid
795 * doing a lookup in the linked list and concurrency issues when writing is
796 * needed. Called with consumer_data.lock held.
798 * Returns the number of fds in the structures.
800 static int consumer_update_poll_array(
801 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
802 struct lttng_consumer_stream
**local_stream
, struct lttng_ht
*ht
)
805 struct lttng_ht_iter iter
;
806 struct lttng_consumer_stream
*stream
;
808 DBG("Updating poll fd array");
810 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
811 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
814 DBG("Active FD %d", stream
->wait_fd
);
815 (*pollfd
)[i
].fd
= stream
->wait_fd
;
816 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
817 local_stream
[i
] = stream
;
823 * Insert the consumer_poll_pipe at the end of the array and don't
824 * increment i so nb_fd is the number of real FD.
826 (*pollfd
)[i
].fd
= ctx
->consumer_poll_pipe
[0];
827 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
832 * Poll on the should_quit pipe and the command socket return -1 on error and
833 * should exit, 0 if data is available on the command socket
835 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
840 num_rdy
= poll(consumer_sockpoll
, 2, -1);
843 * Restart interrupted system call.
845 if (errno
== EINTR
) {
848 PERROR("Poll error");
851 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
852 DBG("consumer_should_quit wake up");
862 * Set the error socket.
864 void lttng_consumer_set_error_sock(
865 struct lttng_consumer_local_data
*ctx
, int sock
)
867 ctx
->consumer_error_socket
= sock
;
871 * Set the command socket path.
873 void lttng_consumer_set_command_sock_path(
874 struct lttng_consumer_local_data
*ctx
, char *sock
)
876 ctx
->consumer_command_sock_path
= sock
;
880 * Send return code to the session daemon.
881 * If the socket is not defined, we return 0, it is not a fatal error
883 int lttng_consumer_send_error(
884 struct lttng_consumer_local_data
*ctx
, int cmd
)
886 if (ctx
->consumer_error_socket
> 0) {
887 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
888 sizeof(enum lttcomm_sessiond_command
));
895 * Close all the tracefiles and stream fds, should be called when all instances
898 void lttng_consumer_cleanup(void)
900 struct lttng_ht_iter iter
;
901 struct lttng_ht_node_ulong
*node
;
906 * close all outfd. Called when there are no more threads running (after
907 * joining on the threads), no need to protect list iteration with mutex.
909 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, node
,
911 struct lttng_consumer_stream
*stream
=
912 caa_container_of(node
, struct lttng_consumer_stream
, node
);
913 consumer_del_stream(stream
, consumer_data
.stream_ht
);
916 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
918 struct lttng_consumer_channel
*channel
=
919 caa_container_of(node
, struct lttng_consumer_channel
, node
);
920 consumer_del_channel(channel
);
925 lttng_ht_destroy(consumer_data
.stream_ht
);
926 lttng_ht_destroy(consumer_data
.channel_ht
);
930 * Called from signal handler.
932 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
937 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
938 } while (ret
< 0 && errno
== EINTR
);
940 PERROR("write consumer quit");
944 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
947 int outfd
= stream
->out_fd
;
950 * This does a blocking write-and-wait on any page that belongs to the
951 * subbuffer prior to the one we just wrote.
952 * Don't care about error values, as these are just hints and ways to
953 * limit the amount of page cache used.
955 if (orig_offset
< stream
->chan
->max_sb_size
) {
958 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
959 stream
->chan
->max_sb_size
,
960 SYNC_FILE_RANGE_WAIT_BEFORE
961 | SYNC_FILE_RANGE_WRITE
962 | SYNC_FILE_RANGE_WAIT_AFTER
);
964 * Give hints to the kernel about how we access the file:
965 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
968 * We need to call fadvise again after the file grows because the
969 * kernel does not seem to apply fadvise to non-existing parts of the
972 * Call fadvise _after_ having waited for the page writeback to
973 * complete because the dirty page writeback semantic is not well
974 * defined. So it can be expected to lead to lower throughput in
977 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
978 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
982 * Initialise the necessary environnement :
983 * - create a new context
984 * - create the poll_pipe
985 * - create the should_quit pipe (for signal handler)
986 * - create the thread pipe (for splice)
988 * Takes a function pointer as argument, this function is called when data is
989 * available on a buffer. This function is responsible to do the
990 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
991 * buffer configuration and then kernctl_put_next_subbuf at the end.
993 * Returns a pointer to the new context or NULL on error.
995 struct lttng_consumer_local_data
*lttng_consumer_create(
996 enum lttng_consumer_type type
,
997 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
998 struct lttng_consumer_local_data
*ctx
),
999 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1000 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1001 int (*update_stream
)(int stream_key
, uint32_t state
))
1004 struct lttng_consumer_local_data
*ctx
;
1006 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1007 consumer_data
.type
== type
);
1008 consumer_data
.type
= type
;
1010 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1012 PERROR("allocating context");
1016 ctx
->consumer_error_socket
= -1;
1017 /* assign the callbacks */
1018 ctx
->on_buffer_ready
= buffer_ready
;
1019 ctx
->on_recv_channel
= recv_channel
;
1020 ctx
->on_recv_stream
= recv_stream
;
1021 ctx
->on_update_stream
= update_stream
;
1023 ret
= pipe(ctx
->consumer_poll_pipe
);
1025 PERROR("Error creating poll pipe");
1026 goto error_poll_pipe
;
1029 /* set read end of the pipe to non-blocking */
1030 ret
= fcntl(ctx
->consumer_poll_pipe
[0], F_SETFL
, O_NONBLOCK
);
1032 PERROR("fcntl O_NONBLOCK");
1033 goto error_poll_fcntl
;
1036 /* set write end of the pipe to non-blocking */
1037 ret
= fcntl(ctx
->consumer_poll_pipe
[1], F_SETFL
, O_NONBLOCK
);
1039 PERROR("fcntl O_NONBLOCK");
1040 goto error_poll_fcntl
;
1043 ret
= pipe(ctx
->consumer_should_quit
);
1045 PERROR("Error creating recv pipe");
1046 goto error_quit_pipe
;
1049 ret
= pipe(ctx
->consumer_thread_pipe
);
1051 PERROR("Error creating thread pipe");
1052 goto error_thread_pipe
;
1055 ret
= utils_create_pipe(ctx
->consumer_metadata_pipe
);
1057 goto error_metadata_pipe
;
1060 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1062 goto error_splice_pipe
;
1068 utils_close_pipe(ctx
->consumer_metadata_pipe
);
1069 error_metadata_pipe
:
1070 utils_close_pipe(ctx
->consumer_thread_pipe
);
1072 for (i
= 0; i
< 2; i
++) {
1075 err
= close(ctx
->consumer_should_quit
[i
]);
1082 for (i
= 0; i
< 2; i
++) {
1085 err
= close(ctx
->consumer_poll_pipe
[i
]);
1097 * Close all fds associated with the instance and free the context.
1099 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1103 ret
= close(ctx
->consumer_error_socket
);
1107 ret
= close(ctx
->consumer_thread_pipe
[0]);
1111 ret
= close(ctx
->consumer_thread_pipe
[1]);
1115 ret
= close(ctx
->consumer_poll_pipe
[0]);
1119 ret
= close(ctx
->consumer_poll_pipe
[1]);
1123 ret
= close(ctx
->consumer_should_quit
[0]);
1127 ret
= close(ctx
->consumer_should_quit
[1]);
1131 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1133 unlink(ctx
->consumer_command_sock_path
);
1138 * Write the metadata stream id on the specified file descriptor.
1140 static int write_relayd_metadata_id(int fd
,
1141 struct lttng_consumer_stream
*stream
,
1142 struct consumer_relayd_sock_pair
*relayd
,
1143 unsigned long padding
)
1146 struct lttcomm_relayd_metadata_payload hdr
;
1148 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1149 hdr
.padding_size
= htobe32(padding
);
1151 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1152 } while (ret
< 0 && errno
== EINTR
);
1154 PERROR("write metadata stream id");
1157 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1158 stream
->relayd_stream_id
, padding
);
1165 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1166 * core function for writing trace buffers to either the local filesystem or
1169 * Careful review MUST be put if any changes occur!
1171 * Returns the number of bytes written
1173 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1174 struct lttng_consumer_local_data
*ctx
,
1175 struct lttng_consumer_stream
*stream
, unsigned long len
,
1176 unsigned long padding
)
1178 unsigned long mmap_offset
;
1179 ssize_t ret
= 0, written
= 0;
1180 off_t orig_offset
= stream
->out_fd_offset
;
1181 /* Default is on the disk */
1182 int outfd
= stream
->out_fd
;
1183 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1185 /* RCU lock for the relayd pointer */
1188 /* Flag that the current stream if set for network streaming. */
1189 if (stream
->net_seq_idx
!= -1) {
1190 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1191 if (relayd
== NULL
) {
1196 /* get the offset inside the fd to mmap */
1197 switch (consumer_data
.type
) {
1198 case LTTNG_CONSUMER_KERNEL
:
1199 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1201 case LTTNG_CONSUMER32_UST
:
1202 case LTTNG_CONSUMER64_UST
:
1203 ret
= lttng_ustctl_get_mmap_read_offset(stream
->chan
->handle
,
1204 stream
->buf
, &mmap_offset
);
1207 ERR("Unknown consumer_data type");
1212 PERROR("tracer ctl get_mmap_read_offset");
1217 /* Handle stream on the relayd if the output is on the network */
1219 unsigned long netlen
= len
;
1222 * Lock the control socket for the complete duration of the function
1223 * since from this point on we will use the socket.
1225 if (stream
->metadata_flag
) {
1226 /* Metadata requires the control socket. */
1227 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1228 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1231 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1233 /* Use the returned socket. */
1236 /* Write metadata stream id before payload */
1237 if (stream
->metadata_flag
) {
1238 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1245 /* Else, use the default set before which is the filesystem. */
1247 /* No streaming, we have to set the len with the full padding */
1253 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
1254 } while (ret
< 0 && errno
== EINTR
);
1255 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1257 PERROR("Error in file write");
1262 } else if (ret
> len
) {
1263 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1271 /* This call is useless on a socket so better save a syscall. */
1273 /* This won't block, but will start writeout asynchronously */
1274 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1275 SYNC_FILE_RANGE_WRITE
);
1276 stream
->out_fd_offset
+= ret
;
1280 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1283 /* Unlock only if ctrl socket used */
1284 if (relayd
&& stream
->metadata_flag
) {
1285 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1293 * Splice the data from the ring buffer to the tracefile.
1295 * Returns the number of bytes spliced.
1297 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1298 struct lttng_consumer_local_data
*ctx
,
1299 struct lttng_consumer_stream
*stream
, unsigned long len
,
1300 unsigned long padding
)
1302 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1304 off_t orig_offset
= stream
->out_fd_offset
;
1305 int fd
= stream
->wait_fd
;
1306 /* Default is on the disk */
1307 int outfd
= stream
->out_fd
;
1308 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1311 switch (consumer_data
.type
) {
1312 case LTTNG_CONSUMER_KERNEL
:
1314 case LTTNG_CONSUMER32_UST
:
1315 case LTTNG_CONSUMER64_UST
:
1316 /* Not supported for user space tracing */
1319 ERR("Unknown consumer_data type");
1323 /* RCU lock for the relayd pointer */
1326 /* Flag that the current stream if set for network streaming. */
1327 if (stream
->net_seq_idx
!= -1) {
1328 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1329 if (relayd
== NULL
) {
1335 * Choose right pipe for splice. Metadata and trace data are handled by
1336 * different threads hence the use of two pipes in order not to race or
1337 * corrupt the written data.
1339 if (stream
->metadata_flag
) {
1340 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1342 splice_pipe
= ctx
->consumer_thread_pipe
;
1345 /* Write metadata stream id before payload */
1347 int total_len
= len
;
1349 if (stream
->metadata_flag
) {
1351 * Lock the control socket for the complete duration of the function
1352 * since from this point on we will use the socket.
1354 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1356 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1363 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1366 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1368 /* Use the returned socket. */
1371 ERR("Remote relayd disconnected. Stopping");
1375 /* No streaming, we have to set the len with the full padding */
1380 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1381 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1382 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1383 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1384 DBG("splice chan to pipe, ret %zd", ret_splice
);
1385 if (ret_splice
< 0) {
1386 PERROR("Error in relay splice");
1388 written
= ret_splice
;
1394 /* Handle stream on the relayd if the output is on the network */
1396 if (stream
->metadata_flag
) {
1397 size_t metadata_payload_size
=
1398 sizeof(struct lttcomm_relayd_metadata_payload
);
1400 /* Update counter to fit the spliced data */
1401 ret_splice
+= metadata_payload_size
;
1402 len
+= metadata_payload_size
;
1404 * We do this so the return value can match the len passed as
1405 * argument to this function.
1407 written
-= metadata_payload_size
;
1411 /* Splice data out */
1412 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1413 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1414 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1415 if (ret_splice
< 0) {
1416 PERROR("Error in file splice");
1418 written
= ret_splice
;
1422 } else if (ret_splice
> len
) {
1424 PERROR("Wrote more data than requested %zd (len: %lu)",
1426 written
+= ret_splice
;
1432 /* This call is useless on a socket so better save a syscall. */
1434 /* This won't block, but will start writeout asynchronously */
1435 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1436 SYNC_FILE_RANGE_WRITE
);
1437 stream
->out_fd_offset
+= ret_splice
;
1439 written
+= ret_splice
;
1441 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1448 /* send the appropriate error description to sessiond */
1451 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EBADF
);
1454 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1457 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1460 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1465 if (relayd
&& stream
->metadata_flag
) {
1466 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1474 * Take a snapshot for a specific fd
1476 * Returns 0 on success, < 0 on error
1478 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1479 struct lttng_consumer_stream
*stream
)
1481 switch (consumer_data
.type
) {
1482 case LTTNG_CONSUMER_KERNEL
:
1483 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1484 case LTTNG_CONSUMER32_UST
:
1485 case LTTNG_CONSUMER64_UST
:
1486 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1488 ERR("Unknown consumer_data type");
1496 * Get the produced position
1498 * Returns 0 on success, < 0 on error
1500 int lttng_consumer_get_produced_snapshot(
1501 struct lttng_consumer_local_data
*ctx
,
1502 struct lttng_consumer_stream
*stream
,
1505 switch (consumer_data
.type
) {
1506 case LTTNG_CONSUMER_KERNEL
:
1507 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1508 case LTTNG_CONSUMER32_UST
:
1509 case LTTNG_CONSUMER64_UST
:
1510 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1512 ERR("Unknown consumer_data type");
1518 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1519 int sock
, struct pollfd
*consumer_sockpoll
)
1521 switch (consumer_data
.type
) {
1522 case LTTNG_CONSUMER_KERNEL
:
1523 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1524 case LTTNG_CONSUMER32_UST
:
1525 case LTTNG_CONSUMER64_UST
:
1526 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1528 ERR("Unknown consumer_data type");
1535 * Iterate over all streams of the hashtable and free them properly.
1537 * WARNING: *MUST* be used with data stream only.
1539 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1542 struct lttng_ht_iter iter
;
1543 struct lttng_consumer_stream
*stream
;
1550 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1551 ret
= lttng_ht_del(ht
, &iter
);
1554 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1558 lttng_ht_destroy(ht
);
1562 * Iterate over all streams of the hashtable and free them properly.
1564 * XXX: Should not be only for metadata stream or else use an other name.
1566 static void destroy_stream_ht(struct lttng_ht
*ht
)
1569 struct lttng_ht_iter iter
;
1570 struct lttng_consumer_stream
*stream
;
1577 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1578 ret
= lttng_ht_del(ht
, &iter
);
1581 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1585 lttng_ht_destroy(ht
);
1589 * Clean up a metadata stream and free its memory.
1591 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1592 struct lttng_ht
*ht
)
1595 struct lttng_ht_iter iter
;
1596 struct lttng_consumer_channel
*free_chan
= NULL
;
1597 struct consumer_relayd_sock_pair
*relayd
;
1601 * This call should NEVER receive regular stream. It must always be
1602 * metadata stream and this is crucial for data structure synchronization.
1604 assert(stream
->metadata_flag
);
1606 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1609 /* Means the stream was allocated but not successfully added */
1613 pthread_mutex_lock(&consumer_data
.lock
);
1614 switch (consumer_data
.type
) {
1615 case LTTNG_CONSUMER_KERNEL
:
1616 if (stream
->mmap_base
!= NULL
) {
1617 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1619 PERROR("munmap metadata stream");
1623 case LTTNG_CONSUMER32_UST
:
1624 case LTTNG_CONSUMER64_UST
:
1625 lttng_ustconsumer_del_stream(stream
);
1628 ERR("Unknown consumer_data type");
1634 iter
.iter
.node
= &stream
->node
.node
;
1635 ret
= lttng_ht_del(ht
, &iter
);
1639 if (stream
->out_fd
>= 0) {
1640 ret
= close(stream
->out_fd
);
1646 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
1647 ret
= close(stream
->wait_fd
);
1653 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
1654 ret
= close(stream
->shm_fd
);
1660 /* Check and cleanup relayd */
1662 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1663 if (relayd
!= NULL
) {
1664 uatomic_dec(&relayd
->refcount
);
1665 assert(uatomic_read(&relayd
->refcount
) >= 0);
1667 /* Closing streams requires to lock the control socket. */
1668 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1669 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1670 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1671 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1673 DBG("Unable to close stream on the relayd. Continuing");
1675 * Continue here. There is nothing we can do for the relayd.
1676 * Chances are that the relayd has closed the socket so we just
1677 * continue cleaning up.
1681 /* Both conditions are met, we destroy the relayd. */
1682 if (uatomic_read(&relayd
->refcount
) == 0 &&
1683 uatomic_read(&relayd
->destroy_flag
)) {
1684 destroy_relayd(relayd
);
1689 /* Atomically decrement channel refcount since other threads can use it. */
1690 uatomic_dec(&stream
->chan
->refcount
);
1691 if (!uatomic_read(&stream
->chan
->refcount
)
1692 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
1693 /* Go for channel deletion! */
1694 free_chan
= stream
->chan
;
1698 pthread_mutex_unlock(&consumer_data
.lock
);
1701 consumer_del_channel(free_chan
);
1705 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1709 * Action done with the metadata stream when adding it to the consumer internal
1710 * data structures to handle it.
1712 static int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
,
1713 struct lttng_ht
*ht
)
1716 struct consumer_relayd_sock_pair
*relayd
;
1721 DBG3("Adding metadata stream %d to hash table", stream
->wait_fd
);
1723 pthread_mutex_lock(&consumer_data
.lock
);
1726 * From here, refcounts are updated so be _careful_ when returning an error
1731 /* Find relayd and, if one is found, increment refcount. */
1732 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1733 if (relayd
!= NULL
) {
1734 uatomic_inc(&relayd
->refcount
);
1737 /* Update channel refcount once added without error(s). */
1738 uatomic_inc(&stream
->chan
->refcount
);
1741 * When nb_init_streams reaches 0, we don't need to trigger any action in
1742 * terms of destroying the associated channel, because the action that
1743 * causes the count to become 0 also causes a stream to be added. The
1744 * channel deletion will thus be triggered by the following removal of this
1747 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
1748 uatomic_dec(&stream
->chan
->nb_init_streams
);
1751 /* Steal stream identifier to avoid having streams with the same key */
1752 consumer_steal_stream_key(stream
->key
, ht
);
1754 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
1757 pthread_mutex_unlock(&consumer_data
.lock
);
1762 * Thread polls on metadata file descriptor and write them on disk or on the
1765 void *consumer_thread_metadata_poll(void *data
)
1768 uint32_t revents
, nb_fd
;
1769 struct lttng_consumer_stream
*stream
= NULL
;
1770 struct lttng_ht_iter iter
;
1771 struct lttng_ht_node_ulong
*node
;
1772 struct lttng_poll_event events
;
1773 struct lttng_consumer_local_data
*ctx
= data
;
1776 rcu_register_thread();
1778 DBG("Thread metadata poll started");
1780 /* Size is set to 1 for the consumer_metadata pipe */
1781 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
1783 ERR("Poll set creation failed");
1787 ret
= lttng_poll_add(&events
, ctx
->consumer_metadata_pipe
[0], LPOLLIN
);
1793 DBG("Metadata main loop started");
1796 lttng_poll_reset(&events
);
1798 nb_fd
= LTTNG_POLL_GETNB(&events
);
1800 /* Only the metadata pipe is set */
1801 if (nb_fd
== 0 && consumer_quit
== 1) {
1806 DBG("Metadata poll wait with %d fd(s)", nb_fd
);
1807 ret
= lttng_poll_wait(&events
, -1);
1808 DBG("Metadata event catched in thread");
1810 if (errno
== EINTR
) {
1811 ERR("Poll EINTR catched");
1817 /* From here, the event is a metadata wait fd */
1818 for (i
= 0; i
< nb_fd
; i
++) {
1819 revents
= LTTNG_POLL_GETEV(&events
, i
);
1820 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1822 /* Just don't waste time if no returned events for the fd */
1827 if (pollfd
== ctx
->consumer_metadata_pipe
[0]) {
1828 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
1829 DBG("Metadata thread pipe hung up");
1831 * Remove the pipe from the poll set and continue the loop
1832 * since their might be data to consume.
1834 lttng_poll_del(&events
, ctx
->consumer_metadata_pipe
[0]);
1835 close(ctx
->consumer_metadata_pipe
[0]);
1837 } else if (revents
& LPOLLIN
) {
1839 /* Get the stream pointer received */
1840 ret
= read(pollfd
, &stream
, sizeof(stream
));
1841 } while (ret
< 0 && errno
== EINTR
);
1843 ret
< sizeof(struct lttng_consumer_stream
*)) {
1844 PERROR("read metadata stream");
1846 * Let's continue here and hope we can still work
1847 * without stopping the consumer. XXX: Should we?
1852 DBG("Adding metadata stream %d to poll set",
1855 ret
= consumer_add_metadata_stream(stream
, metadata_ht
);
1857 ERR("Unable to add metadata stream");
1858 /* Stream was not setup properly. Continuing. */
1859 consumer_del_metadata_stream(stream
, NULL
);
1863 /* Add metadata stream to the global poll events list */
1864 lttng_poll_add(&events
, stream
->wait_fd
,
1865 LPOLLIN
| LPOLLPRI
);
1868 /* Handle other stream */
1873 lttng_ht_lookup(metadata_ht
, (void *)((unsigned long) pollfd
),
1875 node
= lttng_ht_iter_get_node_ulong(&iter
);
1878 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
1881 /* Check for error event */
1882 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
1883 DBG("Metadata fd %d is hup|err.", pollfd
);
1884 if (!stream
->hangup_flush_done
1885 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1886 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1887 DBG("Attempting to flush and consume the UST buffers");
1888 lttng_ustconsumer_on_stream_hangup(stream
);
1890 /* We just flushed the stream now read it. */
1892 len
= ctx
->on_buffer_ready(stream
, ctx
);
1894 * We don't check the return value here since if we get
1895 * a negative len, it means an error occured thus we
1896 * simply remove it from the poll set and free the
1902 lttng_poll_del(&events
, stream
->wait_fd
);
1904 * This call update the channel states, closes file descriptors
1905 * and securely free the stream.
1907 consumer_del_metadata_stream(stream
, metadata_ht
);
1908 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
1909 /* Get the data out of the metadata file descriptor */
1910 DBG("Metadata available on fd %d", pollfd
);
1911 assert(stream
->wait_fd
== pollfd
);
1913 len
= ctx
->on_buffer_ready(stream
, ctx
);
1914 /* It's ok to have an unavailable sub-buffer */
1915 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
1918 } else if (len
> 0) {
1919 stream
->data_read
= 1;
1923 /* Release RCU lock for the stream looked up */
1930 DBG("Metadata poll thread exiting");
1931 lttng_poll_clean(&events
);
1934 destroy_stream_ht(metadata_ht
);
1937 rcu_unregister_thread();
1942 * This thread polls the fds in the set to consume the data and write
1943 * it to tracefile if necessary.
1945 void *consumer_thread_data_poll(void *data
)
1947 int num_rdy
, num_hup
, high_prio
, ret
, i
;
1948 struct pollfd
*pollfd
= NULL
;
1949 /* local view of the streams */
1950 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
1951 /* local view of consumer_data.fds_count */
1953 struct lttng_consumer_local_data
*ctx
= data
;
1956 rcu_register_thread();
1958 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1959 if (data_ht
== NULL
) {
1963 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
1970 * the fds set has been updated, we need to update our
1971 * local array as well
1973 pthread_mutex_lock(&consumer_data
.lock
);
1974 if (consumer_data
.need_update
) {
1975 if (pollfd
!= NULL
) {
1979 if (local_stream
!= NULL
) {
1981 local_stream
= NULL
;
1984 /* allocate for all fds + 1 for the consumer_poll_pipe */
1985 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
1986 if (pollfd
== NULL
) {
1987 PERROR("pollfd malloc");
1988 pthread_mutex_unlock(&consumer_data
.lock
);
1992 /* allocate for all fds + 1 for the consumer_poll_pipe */
1993 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
1994 sizeof(struct lttng_consumer_stream
));
1995 if (local_stream
== NULL
) {
1996 PERROR("local_stream malloc");
1997 pthread_mutex_unlock(&consumer_data
.lock
);
2000 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
,
2003 ERR("Error in allocating pollfd or local_outfds");
2004 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2005 pthread_mutex_unlock(&consumer_data
.lock
);
2009 consumer_data
.need_update
= 0;
2011 pthread_mutex_unlock(&consumer_data
.lock
);
2013 /* No FDs and consumer_quit, consumer_cleanup the thread */
2014 if (nb_fd
== 0 && consumer_quit
== 1) {
2017 /* poll on the array of fds */
2019 DBG("polling on %d fd", nb_fd
+ 1);
2020 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
2021 DBG("poll num_rdy : %d", num_rdy
);
2022 if (num_rdy
== -1) {
2024 * Restart interrupted system call.
2026 if (errno
== EINTR
) {
2029 PERROR("Poll error");
2030 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2032 } else if (num_rdy
== 0) {
2033 DBG("Polling thread timed out");
2038 * If the consumer_poll_pipe triggered poll go directly to the
2039 * beginning of the loop to update the array. We want to prioritize
2040 * array update over low-priority reads.
2042 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2043 size_t pipe_readlen
;
2045 DBG("consumer_poll_pipe wake up");
2046 /* Consume 1 byte of pipe data */
2048 pipe_readlen
= read(ctx
->consumer_poll_pipe
[0], &new_stream
,
2049 sizeof(new_stream
));
2050 } while (pipe_readlen
== -1 && errno
== EINTR
);
2053 * If the stream is NULL, just ignore it. It's also possible that
2054 * the sessiond poll thread changed the consumer_quit state and is
2055 * waking us up to test it.
2057 if (new_stream
== NULL
) {
2061 ret
= consumer_add_stream(new_stream
, data_ht
);
2063 ERR("Consumer add stream %d failed. Continuing",
2066 * At this point, if the add_stream fails, it is not in the
2067 * hash table thus passing the NULL value here.
2069 consumer_del_stream(new_stream
, NULL
);
2072 /* Continue to update the local streams and handle prio ones */
2076 /* Take care of high priority channels first. */
2077 for (i
= 0; i
< nb_fd
; i
++) {
2078 if (pollfd
[i
].revents
& POLLPRI
) {
2079 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2081 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2082 /* it's ok to have an unavailable sub-buffer */
2083 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2085 } else if (len
> 0) {
2086 local_stream
[i
]->data_read
= 1;
2092 * If we read high prio channel in this loop, try again
2093 * for more high prio data.
2099 /* Take care of low priority channels. */
2100 for (i
= 0; i
< nb_fd
; i
++) {
2101 if ((pollfd
[i
].revents
& POLLIN
) ||
2102 local_stream
[i
]->hangup_flush_done
) {
2103 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2104 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2105 /* it's ok to have an unavailable sub-buffer */
2106 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2108 } else if (len
> 0) {
2109 local_stream
[i
]->data_read
= 1;
2114 /* Handle hangup and errors */
2115 for (i
= 0; i
< nb_fd
; i
++) {
2116 if (!local_stream
[i
]->hangup_flush_done
2117 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2118 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2119 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2120 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2122 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2123 /* Attempt read again, for the data we just flushed. */
2124 local_stream
[i
]->data_read
= 1;
2127 * If the poll flag is HUP/ERR/NVAL and we have
2128 * read no data in this pass, we can remove the
2129 * stream from its hash table.
2131 if ((pollfd
[i
].revents
& POLLHUP
)) {
2132 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2133 if (!local_stream
[i
]->data_read
) {
2134 consumer_del_stream(local_stream
[i
], data_ht
);
2137 } else if (pollfd
[i
].revents
& POLLERR
) {
2138 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2139 if (!local_stream
[i
]->data_read
) {
2140 consumer_del_stream(local_stream
[i
], data_ht
);
2143 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2144 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2145 if (!local_stream
[i
]->data_read
) {
2146 consumer_del_stream(local_stream
[i
], data_ht
);
2150 local_stream
[i
]->data_read
= 0;
2154 DBG("polling thread exiting");
2155 if (pollfd
!= NULL
) {
2159 if (local_stream
!= NULL
) {
2161 local_stream
= NULL
;
2165 * Close the write side of the pipe so epoll_wait() in
2166 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2167 * read side of the pipe. If we close them both, epoll_wait strangely does
2168 * not return and could create a endless wait period if the pipe is the
2169 * only tracked fd in the poll set. The thread will take care of closing
2172 close(ctx
->consumer_metadata_pipe
[1]);
2175 destroy_data_stream_ht(data_ht
);
2178 rcu_unregister_thread();
2183 * This thread listens on the consumerd socket and receives the file
2184 * descriptors from the session daemon.
2186 void *consumer_thread_sessiond_poll(void *data
)
2188 int sock
, client_socket
, ret
;
2190 * structure to poll for incoming data on communication socket avoids
2191 * making blocking sockets.
2193 struct pollfd consumer_sockpoll
[2];
2194 struct lttng_consumer_local_data
*ctx
= data
;
2196 rcu_register_thread();
2198 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2199 unlink(ctx
->consumer_command_sock_path
);
2200 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2201 if (client_socket
< 0) {
2202 ERR("Cannot create command socket");
2206 ret
= lttcomm_listen_unix_sock(client_socket
);
2211 DBG("Sending ready command to lttng-sessiond");
2212 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2213 /* return < 0 on error, but == 0 is not fatal */
2215 ERR("Error sending ready command to lttng-sessiond");
2219 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2221 PERROR("fcntl O_NONBLOCK");
2225 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2226 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2227 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2228 consumer_sockpoll
[1].fd
= client_socket
;
2229 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2231 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2234 DBG("Connection on client_socket");
2236 /* Blocking call, waiting for transmission */
2237 sock
= lttcomm_accept_unix_sock(client_socket
);
2242 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2244 PERROR("fcntl O_NONBLOCK");
2248 /* update the polling structure to poll on the established socket */
2249 consumer_sockpoll
[1].fd
= sock
;
2250 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2253 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2256 DBG("Incoming command on sock");
2257 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2258 if (ret
== -ENOENT
) {
2259 DBG("Received STOP command");
2264 * This could simply be a session daemon quitting. Don't output
2267 DBG("Communication interrupted on command socket");
2270 if (consumer_quit
) {
2271 DBG("consumer_thread_receive_fds received quit from signal");
2274 DBG("received fds on sock");
2277 DBG("consumer_thread_receive_fds exiting");
2280 * when all fds have hung up, the polling thread
2286 * 2s of grace period, if no polling events occur during
2287 * this period, the polling thread will exit even if there
2288 * are still open FDs (should not happen, but safety mechanism).
2290 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
2293 * Notify the data poll thread to poll back again and test the
2294 * consumer_quit state to quit gracefully.
2297 struct lttng_consumer_stream
*null_stream
= NULL
;
2299 ret
= write(ctx
->consumer_poll_pipe
[1], &null_stream
,
2300 sizeof(null_stream
));
2301 } while (ret
< 0 && errno
== EINTR
);
2303 rcu_unregister_thread();
2307 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2308 struct lttng_consumer_local_data
*ctx
)
2310 switch (consumer_data
.type
) {
2311 case LTTNG_CONSUMER_KERNEL
:
2312 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
2313 case LTTNG_CONSUMER32_UST
:
2314 case LTTNG_CONSUMER64_UST
:
2315 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2317 ERR("Unknown consumer_data type");
2323 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2325 switch (consumer_data
.type
) {
2326 case LTTNG_CONSUMER_KERNEL
:
2327 return lttng_kconsumer_on_recv_stream(stream
);
2328 case LTTNG_CONSUMER32_UST
:
2329 case LTTNG_CONSUMER64_UST
:
2330 return lttng_ustconsumer_on_recv_stream(stream
);
2332 ERR("Unknown consumer_data type");
2339 * Allocate and set consumer data hash tables.
2341 void lttng_consumer_init(void)
2343 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2344 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2345 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2347 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2348 assert(metadata_ht
);
2349 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2354 * Process the ADD_RELAYD command receive by a consumer.
2356 * This will create a relayd socket pair and add it to the relayd hash table.
2357 * The caller MUST acquire a RCU read side lock before calling it.
2359 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
2360 struct lttng_consumer_local_data
*ctx
, int sock
,
2361 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
)
2364 struct consumer_relayd_sock_pair
*relayd
;
2366 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
2368 /* Get relayd reference if exists. */
2369 relayd
= consumer_find_relayd(net_seq_idx
);
2370 if (relayd
== NULL
) {
2371 /* Not found. Allocate one. */
2372 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
2373 if (relayd
== NULL
) {
2374 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
2379 /* Poll on consumer socket. */
2380 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2385 /* Get relayd socket from session daemon */
2386 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2387 if (ret
!= sizeof(fd
)) {
2388 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
2393 /* Copy socket information and received FD */
2394 switch (sock_type
) {
2395 case LTTNG_STREAM_CONTROL
:
2396 /* Copy received lttcomm socket */
2397 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
2398 ret
= lttcomm_create_sock(&relayd
->control_sock
);
2403 /* Close the created socket fd which is useless */
2404 close(relayd
->control_sock
.fd
);
2406 /* Assign new file descriptor */
2407 relayd
->control_sock
.fd
= fd
;
2409 case LTTNG_STREAM_DATA
:
2410 /* Copy received lttcomm socket */
2411 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
2412 ret
= lttcomm_create_sock(&relayd
->data_sock
);
2417 /* Close the created socket fd which is useless */
2418 close(relayd
->data_sock
.fd
);
2420 /* Assign new file descriptor */
2421 relayd
->data_sock
.fd
= fd
;
2424 ERR("Unknown relayd socket type (%d)", sock_type
);
2428 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2429 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
2430 relayd
->net_seq_idx
, fd
);
2433 * Add relayd socket pair to consumer data hashtable. If object already
2434 * exists or on error, the function gracefully returns.