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
);
286 assert(consumer_data
.stream_count
> 0);
287 consumer_data
.stream_count
--;
289 if (stream
->out_fd
>= 0) {
290 ret
= close(stream
->out_fd
);
295 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
296 ret
= close(stream
->wait_fd
);
301 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
302 ret
= close(stream
->shm_fd
);
308 /* Check and cleanup relayd */
310 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
311 if (relayd
!= NULL
) {
312 uatomic_dec(&relayd
->refcount
);
313 assert(uatomic_read(&relayd
->refcount
) >= 0);
315 /* Closing streams requires to lock the control socket. */
316 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
317 ret
= relayd_send_close_stream(&relayd
->control_sock
,
318 stream
->relayd_stream_id
,
319 stream
->next_net_seq_num
- 1);
320 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
322 DBG("Unable to close stream on the relayd. Continuing");
324 * Continue here. There is nothing we can do for the relayd.
325 * Chances are that the relayd has closed the socket so we just
326 * continue cleaning up.
330 /* Both conditions are met, we destroy the relayd. */
331 if (uatomic_read(&relayd
->refcount
) == 0 &&
332 uatomic_read(&relayd
->destroy_flag
)) {
333 destroy_relayd(relayd
);
338 uatomic_dec(&stream
->chan
->refcount
);
339 if (!uatomic_read(&stream
->chan
->refcount
)
340 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
341 free_chan
= stream
->chan
;
345 consumer_data
.need_update
= 1;
346 pthread_mutex_unlock(&consumer_data
.lock
);
349 consumer_del_channel(free_chan
);
353 call_rcu(&stream
->node
.head
, consumer_free_stream
);
356 struct lttng_consumer_stream
*consumer_allocate_stream(
357 int channel_key
, int stream_key
,
358 int shm_fd
, int wait_fd
,
359 enum lttng_consumer_stream_state state
,
361 enum lttng_event_output output
,
362 const char *path_name
,
369 struct lttng_consumer_stream
*stream
;
371 stream
= zmalloc(sizeof(*stream
));
372 if (stream
== NULL
) {
373 PERROR("malloc struct lttng_consumer_stream");
374 *alloc_ret
= -ENOMEM
;
379 * Get stream's channel reference. Needed when adding the stream to the
382 stream
->chan
= consumer_find_channel(channel_key
);
384 *alloc_ret
= -ENOENT
;
385 ERR("Unable to find channel for stream %d", stream_key
);
389 stream
->key
= stream_key
;
390 stream
->shm_fd
= shm_fd
;
391 stream
->wait_fd
= wait_fd
;
393 stream
->out_fd_offset
= 0;
394 stream
->state
= state
;
395 stream
->mmap_len
= mmap_len
;
396 stream
->mmap_base
= NULL
;
397 stream
->output
= output
;
400 stream
->net_seq_idx
= net_index
;
401 stream
->metadata_flag
= metadata_flag
;
402 strncpy(stream
->path_name
, path_name
, sizeof(stream
->path_name
));
403 stream
->path_name
[sizeof(stream
->path_name
) - 1] = '\0';
406 * Index differently the metadata node because the thread is using an
407 * internal hash table to match streams in the metadata_ht to the epoll set
411 lttng_ht_node_init_ulong(&stream
->node
, stream
->wait_fd
);
413 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
417 * The cpu number is needed before using any ustctl_* actions. Ignored for
418 * the kernel so the value does not matter.
420 pthread_mutex_lock(&consumer_data
.lock
);
421 stream
->cpu
= stream
->chan
->cpucount
++;
422 pthread_mutex_unlock(&consumer_data
.lock
);
424 DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu,"
425 " out_fd %d, net_seq_idx %d)", stream
->path_name
, stream
->key
,
426 stream
->shm_fd
, stream
->wait_fd
,
427 (unsigned long long) stream
->mmap_len
, stream
->out_fd
,
428 stream
->net_seq_idx
);
438 * Add a stream to the global list protected by a mutex.
440 static int consumer_add_stream(struct lttng_consumer_stream
*stream
,
444 struct consumer_relayd_sock_pair
*relayd
;
449 DBG3("Adding consumer stream %d", stream
->key
);
451 pthread_mutex_lock(&consumer_data
.lock
);
454 /* Steal stream identifier to avoid having streams with the same key */
455 consumer_steal_stream_key(stream
->key
, ht
);
457 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
459 /* Check and cleanup relayd */
460 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
461 if (relayd
!= NULL
) {
462 uatomic_inc(&relayd
->refcount
);
465 /* Update channel refcount once added without error(s). */
466 uatomic_inc(&stream
->chan
->refcount
);
469 * When nb_init_streams reaches 0, we don't need to trigger any action in
470 * terms of destroying the associated channel, because the action that
471 * causes the count to become 0 also causes a stream to be added. The
472 * channel deletion will thus be triggered by the following removal of this
475 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
476 uatomic_dec(&stream
->chan
->nb_init_streams
);
479 /* Update consumer data once the node is inserted. */
480 consumer_data
.stream_count
++;
481 consumer_data
.need_update
= 1;
484 pthread_mutex_unlock(&consumer_data
.lock
);
490 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
491 * be acquired before calling this.
493 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
496 struct lttng_ht_node_ulong
*node
;
497 struct lttng_ht_iter iter
;
499 if (relayd
== NULL
) {
504 lttng_ht_lookup(consumer_data
.relayd_ht
,
505 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
506 node
= lttng_ht_iter_get_node_ulong(&iter
);
508 /* Relayd already exist. Ignore the insertion */
511 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
518 * Allocate and return a consumer relayd socket.
520 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
523 struct consumer_relayd_sock_pair
*obj
= NULL
;
525 /* Negative net sequence index is a failure */
526 if (net_seq_idx
< 0) {
530 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
532 PERROR("zmalloc relayd sock");
536 obj
->net_seq_idx
= net_seq_idx
;
538 obj
->destroy_flag
= 0;
539 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
540 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
547 * Find a relayd socket pair in the global consumer data.
549 * Return the object if found else NULL.
550 * RCU read-side lock must be held across this call and while using the
553 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
555 struct lttng_ht_iter iter
;
556 struct lttng_ht_node_ulong
*node
;
557 struct consumer_relayd_sock_pair
*relayd
= NULL
;
559 /* Negative keys are lookup failures */
564 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
566 node
= lttng_ht_iter_get_node_ulong(&iter
);
568 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
576 * Handle stream for relayd transmission if the stream applies for network
577 * streaming where the net sequence index is set.
579 * Return destination file descriptor or negative value on error.
581 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
582 size_t data_size
, unsigned long padding
,
583 struct consumer_relayd_sock_pair
*relayd
)
586 struct lttcomm_relayd_data_hdr data_hdr
;
592 /* Reset data header */
593 memset(&data_hdr
, 0, sizeof(data_hdr
));
595 if (stream
->metadata_flag
) {
596 /* Caller MUST acquire the relayd control socket lock */
597 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
602 /* Metadata are always sent on the control socket. */
603 outfd
= relayd
->control_sock
.fd
;
605 /* Set header with stream information */
606 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
607 data_hdr
.data_size
= htobe32(data_size
);
608 data_hdr
.padding_size
= htobe32(padding
);
609 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
610 /* Other fields are zeroed previously */
612 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
618 /* Set to go on data socket */
619 outfd
= relayd
->data_sock
.fd
;
627 void consumer_free_channel(struct rcu_head
*head
)
629 struct lttng_ht_node_ulong
*node
=
630 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
631 struct lttng_consumer_channel
*channel
=
632 caa_container_of(node
, struct lttng_consumer_channel
, node
);
638 * Remove a channel from the global list protected by a mutex. This
639 * function is also responsible for freeing its data structures.
641 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
644 struct lttng_ht_iter iter
;
646 pthread_mutex_lock(&consumer_data
.lock
);
648 switch (consumer_data
.type
) {
649 case LTTNG_CONSUMER_KERNEL
:
651 case LTTNG_CONSUMER32_UST
:
652 case LTTNG_CONSUMER64_UST
:
653 lttng_ustconsumer_del_channel(channel
);
656 ERR("Unknown consumer_data type");
662 iter
.iter
.node
= &channel
->node
.node
;
663 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
667 if (channel
->mmap_base
!= NULL
) {
668 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
673 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
674 ret
= close(channel
->wait_fd
);
679 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
680 ret
= close(channel
->shm_fd
);
686 call_rcu(&channel
->node
.head
, consumer_free_channel
);
688 pthread_mutex_unlock(&consumer_data
.lock
);
691 struct lttng_consumer_channel
*consumer_allocate_channel(
693 int shm_fd
, int wait_fd
,
695 uint64_t max_sb_size
,
696 unsigned int nb_init_streams
)
698 struct lttng_consumer_channel
*channel
;
701 channel
= zmalloc(sizeof(*channel
));
702 if (channel
== NULL
) {
703 PERROR("malloc struct lttng_consumer_channel");
706 channel
->key
= channel_key
;
707 channel
->shm_fd
= shm_fd
;
708 channel
->wait_fd
= wait_fd
;
709 channel
->mmap_len
= mmap_len
;
710 channel
->max_sb_size
= max_sb_size
;
711 channel
->refcount
= 0;
712 channel
->nb_init_streams
= nb_init_streams
;
713 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
715 switch (consumer_data
.type
) {
716 case LTTNG_CONSUMER_KERNEL
:
717 channel
->mmap_base
= NULL
;
718 channel
->mmap_len
= 0;
720 case LTTNG_CONSUMER32_UST
:
721 case LTTNG_CONSUMER64_UST
:
722 ret
= lttng_ustconsumer_allocate_channel(channel
);
729 ERR("Unknown consumer_data type");
733 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
734 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
735 (unsigned long long) channel
->mmap_len
,
736 (unsigned long long) channel
->max_sb_size
);
742 * Add a channel to the global list protected by a mutex.
744 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
746 struct lttng_ht_node_ulong
*node
;
747 struct lttng_ht_iter iter
;
749 pthread_mutex_lock(&consumer_data
.lock
);
750 /* Steal channel identifier, for UST */
751 consumer_steal_channel_key(channel
->key
);
754 lttng_ht_lookup(consumer_data
.channel_ht
,
755 (void *)((unsigned long) channel
->key
), &iter
);
756 node
= lttng_ht_iter_get_node_ulong(&iter
);
758 /* Channel already exist. Ignore the insertion */
762 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
766 pthread_mutex_unlock(&consumer_data
.lock
);
772 * Allocate the pollfd structure and the local view of the out fds to avoid
773 * doing a lookup in the linked list and concurrency issues when writing is
774 * needed. Called with consumer_data.lock held.
776 * Returns the number of fds in the structures.
778 static int consumer_update_poll_array(
779 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
780 struct lttng_consumer_stream
**local_stream
, struct lttng_ht
*ht
)
783 struct lttng_ht_iter iter
;
784 struct lttng_consumer_stream
*stream
;
786 DBG("Updating poll fd array");
788 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
789 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
792 DBG("Active FD %d", stream
->wait_fd
);
793 (*pollfd
)[i
].fd
= stream
->wait_fd
;
794 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
795 local_stream
[i
] = stream
;
801 * Insert the consumer_data_pipe at the end of the array and don't
802 * increment i so nb_fd is the number of real FD.
804 (*pollfd
)[i
].fd
= ctx
->consumer_data_pipe
[0];
805 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
810 * Poll on the should_quit pipe and the command socket return -1 on error and
811 * should exit, 0 if data is available on the command socket
813 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
818 num_rdy
= poll(consumer_sockpoll
, 2, -1);
821 * Restart interrupted system call.
823 if (errno
== EINTR
) {
826 PERROR("Poll error");
829 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
830 DBG("consumer_should_quit wake up");
840 * Set the error socket.
842 void lttng_consumer_set_error_sock(
843 struct lttng_consumer_local_data
*ctx
, int sock
)
845 ctx
->consumer_error_socket
= sock
;
849 * Set the command socket path.
851 void lttng_consumer_set_command_sock_path(
852 struct lttng_consumer_local_data
*ctx
, char *sock
)
854 ctx
->consumer_command_sock_path
= sock
;
858 * Send return code to the session daemon.
859 * If the socket is not defined, we return 0, it is not a fatal error
861 int lttng_consumer_send_error(
862 struct lttng_consumer_local_data
*ctx
, int cmd
)
864 if (ctx
->consumer_error_socket
> 0) {
865 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
866 sizeof(enum lttcomm_sessiond_command
));
873 * Close all the tracefiles and stream fds, should be called when all instances
876 void lttng_consumer_cleanup(void)
878 struct lttng_ht_iter iter
;
879 struct lttng_ht_node_ulong
*node
;
883 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
885 struct lttng_consumer_channel
*channel
=
886 caa_container_of(node
, struct lttng_consumer_channel
, node
);
887 consumer_del_channel(channel
);
892 lttng_ht_destroy(consumer_data
.channel_ht
);
896 * Called from signal handler.
898 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
903 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
904 } while (ret
< 0 && errno
== EINTR
);
906 PERROR("write consumer quit");
910 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
913 int outfd
= stream
->out_fd
;
916 * This does a blocking write-and-wait on any page that belongs to the
917 * subbuffer prior to the one we just wrote.
918 * Don't care about error values, as these are just hints and ways to
919 * limit the amount of page cache used.
921 if (orig_offset
< stream
->chan
->max_sb_size
) {
924 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
925 stream
->chan
->max_sb_size
,
926 SYNC_FILE_RANGE_WAIT_BEFORE
927 | SYNC_FILE_RANGE_WRITE
928 | SYNC_FILE_RANGE_WAIT_AFTER
);
930 * Give hints to the kernel about how we access the file:
931 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
934 * We need to call fadvise again after the file grows because the
935 * kernel does not seem to apply fadvise to non-existing parts of the
938 * Call fadvise _after_ having waited for the page writeback to
939 * complete because the dirty page writeback semantic is not well
940 * defined. So it can be expected to lead to lower throughput in
943 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
944 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
948 * Initialise the necessary environnement :
949 * - create a new context
950 * - create the poll_pipe
951 * - create the should_quit pipe (for signal handler)
952 * - create the thread pipe (for splice)
954 * Takes a function pointer as argument, this function is called when data is
955 * available on a buffer. This function is responsible to do the
956 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
957 * buffer configuration and then kernctl_put_next_subbuf at the end.
959 * Returns a pointer to the new context or NULL on error.
961 struct lttng_consumer_local_data
*lttng_consumer_create(
962 enum lttng_consumer_type type
,
963 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
964 struct lttng_consumer_local_data
*ctx
),
965 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
966 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
967 int (*update_stream
)(int stream_key
, uint32_t state
))
970 struct lttng_consumer_local_data
*ctx
;
972 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
973 consumer_data
.type
== type
);
974 consumer_data
.type
= type
;
976 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
978 PERROR("allocating context");
982 ctx
->consumer_error_socket
= -1;
983 /* assign the callbacks */
984 ctx
->on_buffer_ready
= buffer_ready
;
985 ctx
->on_recv_channel
= recv_channel
;
986 ctx
->on_recv_stream
= recv_stream
;
987 ctx
->on_update_stream
= update_stream
;
989 ret
= pipe(ctx
->consumer_data_pipe
);
991 PERROR("Error creating poll pipe");
992 goto error_poll_pipe
;
995 /* set read end of the pipe to non-blocking */
996 ret
= fcntl(ctx
->consumer_data_pipe
[0], F_SETFL
, O_NONBLOCK
);
998 PERROR("fcntl O_NONBLOCK");
999 goto error_poll_fcntl
;
1002 /* set write end of the pipe to non-blocking */
1003 ret
= fcntl(ctx
->consumer_data_pipe
[1], F_SETFL
, O_NONBLOCK
);
1005 PERROR("fcntl O_NONBLOCK");
1006 goto error_poll_fcntl
;
1009 ret
= pipe(ctx
->consumer_should_quit
);
1011 PERROR("Error creating recv pipe");
1012 goto error_quit_pipe
;
1015 ret
= pipe(ctx
->consumer_thread_pipe
);
1017 PERROR("Error creating thread pipe");
1018 goto error_thread_pipe
;
1021 ret
= utils_create_pipe(ctx
->consumer_metadata_pipe
);
1023 goto error_metadata_pipe
;
1026 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1028 goto error_splice_pipe
;
1034 utils_close_pipe(ctx
->consumer_metadata_pipe
);
1035 error_metadata_pipe
:
1036 utils_close_pipe(ctx
->consumer_thread_pipe
);
1038 for (i
= 0; i
< 2; i
++) {
1041 err
= close(ctx
->consumer_should_quit
[i
]);
1048 for (i
= 0; i
< 2; i
++) {
1051 err
= close(ctx
->consumer_data_pipe
[i
]);
1063 * Close all fds associated with the instance and free the context.
1065 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1069 ret
= close(ctx
->consumer_error_socket
);
1073 ret
= close(ctx
->consumer_thread_pipe
[0]);
1077 ret
= close(ctx
->consumer_thread_pipe
[1]);
1081 ret
= close(ctx
->consumer_data_pipe
[0]);
1085 ret
= close(ctx
->consumer_data_pipe
[1]);
1089 ret
= close(ctx
->consumer_should_quit
[0]);
1093 ret
= close(ctx
->consumer_should_quit
[1]);
1097 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1099 unlink(ctx
->consumer_command_sock_path
);
1104 * Write the metadata stream id on the specified file descriptor.
1106 static int write_relayd_metadata_id(int fd
,
1107 struct lttng_consumer_stream
*stream
,
1108 struct consumer_relayd_sock_pair
*relayd
,
1109 unsigned long padding
)
1112 struct lttcomm_relayd_metadata_payload hdr
;
1114 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1115 hdr
.padding_size
= htobe32(padding
);
1117 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1118 } while (ret
< 0 && errno
== EINTR
);
1120 PERROR("write metadata stream id");
1123 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1124 stream
->relayd_stream_id
, padding
);
1131 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1132 * core function for writing trace buffers to either the local filesystem or
1135 * Careful review MUST be put if any changes occur!
1137 * Returns the number of bytes written
1139 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1140 struct lttng_consumer_local_data
*ctx
,
1141 struct lttng_consumer_stream
*stream
, unsigned long len
,
1142 unsigned long padding
)
1144 unsigned long mmap_offset
;
1145 ssize_t ret
= 0, written
= 0;
1146 off_t orig_offset
= stream
->out_fd_offset
;
1147 /* Default is on the disk */
1148 int outfd
= stream
->out_fd
;
1149 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1151 /* RCU lock for the relayd pointer */
1154 /* Flag that the current stream if set for network streaming. */
1155 if (stream
->net_seq_idx
!= -1) {
1156 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1157 if (relayd
== NULL
) {
1162 /* get the offset inside the fd to mmap */
1163 switch (consumer_data
.type
) {
1164 case LTTNG_CONSUMER_KERNEL
:
1165 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1167 case LTTNG_CONSUMER32_UST
:
1168 case LTTNG_CONSUMER64_UST
:
1169 ret
= lttng_ustctl_get_mmap_read_offset(stream
->chan
->handle
,
1170 stream
->buf
, &mmap_offset
);
1173 ERR("Unknown consumer_data type");
1178 PERROR("tracer ctl get_mmap_read_offset");
1183 /* Handle stream on the relayd if the output is on the network */
1185 unsigned long netlen
= len
;
1188 * Lock the control socket for the complete duration of the function
1189 * since from this point on we will use the socket.
1191 if (stream
->metadata_flag
) {
1192 /* Metadata requires the control socket. */
1193 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1194 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1197 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1199 /* Use the returned socket. */
1202 /* Write metadata stream id before payload */
1203 if (stream
->metadata_flag
) {
1204 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1211 /* Else, use the default set before which is the filesystem. */
1213 /* No streaming, we have to set the len with the full padding */
1219 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
1220 } while (ret
< 0 && errno
== EINTR
);
1221 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1223 PERROR("Error in file write");
1228 } else if (ret
> len
) {
1229 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1237 /* This call is useless on a socket so better save a syscall. */
1239 /* This won't block, but will start writeout asynchronously */
1240 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1241 SYNC_FILE_RANGE_WRITE
);
1242 stream
->out_fd_offset
+= ret
;
1246 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1249 /* Unlock only if ctrl socket used */
1250 if (relayd
&& stream
->metadata_flag
) {
1251 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1259 * Splice the data from the ring buffer to the tracefile.
1261 * Returns the number of bytes spliced.
1263 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1264 struct lttng_consumer_local_data
*ctx
,
1265 struct lttng_consumer_stream
*stream
, unsigned long len
,
1266 unsigned long padding
)
1268 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1270 off_t orig_offset
= stream
->out_fd_offset
;
1271 int fd
= stream
->wait_fd
;
1272 /* Default is on the disk */
1273 int outfd
= stream
->out_fd
;
1274 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1277 switch (consumer_data
.type
) {
1278 case LTTNG_CONSUMER_KERNEL
:
1280 case LTTNG_CONSUMER32_UST
:
1281 case LTTNG_CONSUMER64_UST
:
1282 /* Not supported for user space tracing */
1285 ERR("Unknown consumer_data type");
1289 /* RCU lock for the relayd pointer */
1292 /* Flag that the current stream if set for network streaming. */
1293 if (stream
->net_seq_idx
!= -1) {
1294 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1295 if (relayd
== NULL
) {
1301 * Choose right pipe for splice. Metadata and trace data are handled by
1302 * different threads hence the use of two pipes in order not to race or
1303 * corrupt the written data.
1305 if (stream
->metadata_flag
) {
1306 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1308 splice_pipe
= ctx
->consumer_thread_pipe
;
1311 /* Write metadata stream id before payload */
1313 int total_len
= len
;
1315 if (stream
->metadata_flag
) {
1317 * Lock the control socket for the complete duration of the function
1318 * since from this point on we will use the socket.
1320 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1322 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1329 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1332 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1334 /* Use the returned socket. */
1337 ERR("Remote relayd disconnected. Stopping");
1341 /* No streaming, we have to set the len with the full padding */
1346 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1347 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1348 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1349 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1350 DBG("splice chan to pipe, ret %zd", ret_splice
);
1351 if (ret_splice
< 0) {
1352 PERROR("Error in relay splice");
1354 written
= ret_splice
;
1360 /* Handle stream on the relayd if the output is on the network */
1362 if (stream
->metadata_flag
) {
1363 size_t metadata_payload_size
=
1364 sizeof(struct lttcomm_relayd_metadata_payload
);
1366 /* Update counter to fit the spliced data */
1367 ret_splice
+= metadata_payload_size
;
1368 len
+= metadata_payload_size
;
1370 * We do this so the return value can match the len passed as
1371 * argument to this function.
1373 written
-= metadata_payload_size
;
1377 /* Splice data out */
1378 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1379 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1380 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1381 if (ret_splice
< 0) {
1382 PERROR("Error in file splice");
1384 written
= ret_splice
;
1388 } else if (ret_splice
> len
) {
1390 PERROR("Wrote more data than requested %zd (len: %lu)",
1392 written
+= ret_splice
;
1398 /* This call is useless on a socket so better save a syscall. */
1400 /* This won't block, but will start writeout asynchronously */
1401 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1402 SYNC_FILE_RANGE_WRITE
);
1403 stream
->out_fd_offset
+= ret_splice
;
1405 written
+= ret_splice
;
1407 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1414 /* send the appropriate error description to sessiond */
1417 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EBADF
);
1420 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1423 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1426 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1431 if (relayd
&& stream
->metadata_flag
) {
1432 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1440 * Take a snapshot for a specific fd
1442 * Returns 0 on success, < 0 on error
1444 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1445 struct lttng_consumer_stream
*stream
)
1447 switch (consumer_data
.type
) {
1448 case LTTNG_CONSUMER_KERNEL
:
1449 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1450 case LTTNG_CONSUMER32_UST
:
1451 case LTTNG_CONSUMER64_UST
:
1452 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1454 ERR("Unknown consumer_data type");
1462 * Get the produced position
1464 * Returns 0 on success, < 0 on error
1466 int lttng_consumer_get_produced_snapshot(
1467 struct lttng_consumer_local_data
*ctx
,
1468 struct lttng_consumer_stream
*stream
,
1471 switch (consumer_data
.type
) {
1472 case LTTNG_CONSUMER_KERNEL
:
1473 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1474 case LTTNG_CONSUMER32_UST
:
1475 case LTTNG_CONSUMER64_UST
:
1476 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1478 ERR("Unknown consumer_data type");
1484 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1485 int sock
, struct pollfd
*consumer_sockpoll
)
1487 switch (consumer_data
.type
) {
1488 case LTTNG_CONSUMER_KERNEL
:
1489 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1490 case LTTNG_CONSUMER32_UST
:
1491 case LTTNG_CONSUMER64_UST
:
1492 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1494 ERR("Unknown consumer_data type");
1501 * Iterate over all streams of the hashtable and free them properly.
1503 * WARNING: *MUST* be used with data stream only.
1505 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1508 struct lttng_ht_iter iter
;
1509 struct lttng_consumer_stream
*stream
;
1516 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1517 ret
= lttng_ht_del(ht
, &iter
);
1520 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1524 lttng_ht_destroy(ht
);
1528 * Iterate over all streams of the hashtable and free them properly.
1530 * XXX: Should not be only for metadata stream or else use an other name.
1532 static void destroy_stream_ht(struct lttng_ht
*ht
)
1535 struct lttng_ht_iter iter
;
1536 struct lttng_consumer_stream
*stream
;
1543 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1544 ret
= lttng_ht_del(ht
, &iter
);
1547 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1551 lttng_ht_destroy(ht
);
1555 * Clean up a metadata stream and free its memory.
1557 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1558 struct lttng_ht
*ht
)
1561 struct lttng_ht_iter iter
;
1562 struct lttng_consumer_channel
*free_chan
= NULL
;
1563 struct consumer_relayd_sock_pair
*relayd
;
1567 * This call should NEVER receive regular stream. It must always be
1568 * metadata stream and this is crucial for data structure synchronization.
1570 assert(stream
->metadata_flag
);
1572 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1575 /* Means the stream was allocated but not successfully added */
1579 pthread_mutex_lock(&consumer_data
.lock
);
1580 switch (consumer_data
.type
) {
1581 case LTTNG_CONSUMER_KERNEL
:
1582 if (stream
->mmap_base
!= NULL
) {
1583 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1585 PERROR("munmap metadata stream");
1589 case LTTNG_CONSUMER32_UST
:
1590 case LTTNG_CONSUMER64_UST
:
1591 lttng_ustconsumer_del_stream(stream
);
1594 ERR("Unknown consumer_data type");
1600 iter
.iter
.node
= &stream
->node
.node
;
1601 ret
= lttng_ht_del(ht
, &iter
);
1605 if (stream
->out_fd
>= 0) {
1606 ret
= close(stream
->out_fd
);
1612 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
1613 ret
= close(stream
->wait_fd
);
1619 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
1620 ret
= close(stream
->shm_fd
);
1626 /* Check and cleanup relayd */
1628 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1629 if (relayd
!= NULL
) {
1630 uatomic_dec(&relayd
->refcount
);
1631 assert(uatomic_read(&relayd
->refcount
) >= 0);
1633 /* Closing streams requires to lock the control socket. */
1634 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1635 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1636 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1637 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1639 DBG("Unable to close stream on the relayd. Continuing");
1641 * Continue here. There is nothing we can do for the relayd.
1642 * Chances are that the relayd has closed the socket so we just
1643 * continue cleaning up.
1647 /* Both conditions are met, we destroy the relayd. */
1648 if (uatomic_read(&relayd
->refcount
) == 0 &&
1649 uatomic_read(&relayd
->destroy_flag
)) {
1650 destroy_relayd(relayd
);
1655 /* Atomically decrement channel refcount since other threads can use it. */
1656 uatomic_dec(&stream
->chan
->refcount
);
1657 if (!uatomic_read(&stream
->chan
->refcount
)
1658 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
1659 /* Go for channel deletion! */
1660 free_chan
= stream
->chan
;
1664 pthread_mutex_unlock(&consumer_data
.lock
);
1667 consumer_del_channel(free_chan
);
1671 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1675 * Action done with the metadata stream when adding it to the consumer internal
1676 * data structures to handle it.
1678 static int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
,
1679 struct lttng_ht
*ht
)
1682 struct consumer_relayd_sock_pair
*relayd
;
1687 DBG3("Adding metadata stream %d to hash table", stream
->wait_fd
);
1689 pthread_mutex_lock(&consumer_data
.lock
);
1692 * From here, refcounts are updated so be _careful_ when returning an error
1697 /* Find relayd and, if one is found, increment refcount. */
1698 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1699 if (relayd
!= NULL
) {
1700 uatomic_inc(&relayd
->refcount
);
1703 /* Update channel refcount once added without error(s). */
1704 uatomic_inc(&stream
->chan
->refcount
);
1707 * When nb_init_streams reaches 0, we don't need to trigger any action in
1708 * terms of destroying the associated channel, because the action that
1709 * causes the count to become 0 also causes a stream to be added. The
1710 * channel deletion will thus be triggered by the following removal of this
1713 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
1714 uatomic_dec(&stream
->chan
->nb_init_streams
);
1717 /* Steal stream identifier to avoid having streams with the same key */
1718 consumer_steal_stream_key(stream
->key
, ht
);
1720 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
1723 pthread_mutex_unlock(&consumer_data
.lock
);
1728 * Thread polls on metadata file descriptor and write them on disk or on the
1731 void *consumer_thread_metadata_poll(void *data
)
1734 uint32_t revents
, nb_fd
;
1735 struct lttng_consumer_stream
*stream
= NULL
;
1736 struct lttng_ht_iter iter
;
1737 struct lttng_ht_node_ulong
*node
;
1738 struct lttng_poll_event events
;
1739 struct lttng_consumer_local_data
*ctx
= data
;
1742 rcu_register_thread();
1744 DBG("Thread metadata poll started");
1746 /* Size is set to 1 for the consumer_metadata pipe */
1747 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
1749 ERR("Poll set creation failed");
1753 ret
= lttng_poll_add(&events
, ctx
->consumer_metadata_pipe
[0], LPOLLIN
);
1759 DBG("Metadata main loop started");
1762 lttng_poll_reset(&events
);
1764 nb_fd
= LTTNG_POLL_GETNB(&events
);
1766 /* Only the metadata pipe is set */
1767 if (nb_fd
== 0 && consumer_quit
== 1) {
1772 DBG("Metadata poll wait with %d fd(s)", nb_fd
);
1773 ret
= lttng_poll_wait(&events
, -1);
1774 DBG("Metadata event catched in thread");
1776 if (errno
== EINTR
) {
1777 ERR("Poll EINTR catched");
1783 /* From here, the event is a metadata wait fd */
1784 for (i
= 0; i
< nb_fd
; i
++) {
1785 revents
= LTTNG_POLL_GETEV(&events
, i
);
1786 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1788 /* Just don't waste time if no returned events for the fd */
1793 if (pollfd
== ctx
->consumer_metadata_pipe
[0]) {
1794 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
1795 DBG("Metadata thread pipe hung up");
1797 * Remove the pipe from the poll set and continue the loop
1798 * since their might be data to consume.
1800 lttng_poll_del(&events
, ctx
->consumer_metadata_pipe
[0]);
1801 close(ctx
->consumer_metadata_pipe
[0]);
1803 } else if (revents
& LPOLLIN
) {
1805 /* Get the stream pointer received */
1806 ret
= read(pollfd
, &stream
, sizeof(stream
));
1807 } while (ret
< 0 && errno
== EINTR
);
1809 ret
< sizeof(struct lttng_consumer_stream
*)) {
1810 PERROR("read metadata stream");
1812 * Let's continue here and hope we can still work
1813 * without stopping the consumer. XXX: Should we?
1818 DBG("Adding metadata stream %d to poll set",
1821 ret
= consumer_add_metadata_stream(stream
, metadata_ht
);
1823 ERR("Unable to add metadata stream");
1824 /* Stream was not setup properly. Continuing. */
1825 consumer_del_metadata_stream(stream
, NULL
);
1829 /* Add metadata stream to the global poll events list */
1830 lttng_poll_add(&events
, stream
->wait_fd
,
1831 LPOLLIN
| LPOLLPRI
);
1834 /* Handle other stream */
1839 lttng_ht_lookup(metadata_ht
, (void *)((unsigned long) pollfd
),
1841 node
= lttng_ht_iter_get_node_ulong(&iter
);
1844 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
1847 /* Check for error event */
1848 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
1849 DBG("Metadata fd %d is hup|err.", pollfd
);
1850 if (!stream
->hangup_flush_done
1851 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1852 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1853 DBG("Attempting to flush and consume the UST buffers");
1854 lttng_ustconsumer_on_stream_hangup(stream
);
1856 /* We just flushed the stream now read it. */
1858 len
= ctx
->on_buffer_ready(stream
, ctx
);
1860 * We don't check the return value here since if we get
1861 * a negative len, it means an error occured thus we
1862 * simply remove it from the poll set and free the
1868 lttng_poll_del(&events
, stream
->wait_fd
);
1870 * This call update the channel states, closes file descriptors
1871 * and securely free the stream.
1873 consumer_del_metadata_stream(stream
, metadata_ht
);
1874 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
1875 /* Get the data out of the metadata file descriptor */
1876 DBG("Metadata available on fd %d", pollfd
);
1877 assert(stream
->wait_fd
== pollfd
);
1879 len
= ctx
->on_buffer_ready(stream
, ctx
);
1880 /* It's ok to have an unavailable sub-buffer */
1881 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
1884 } else if (len
> 0) {
1885 stream
->data_read
= 1;
1889 /* Release RCU lock for the stream looked up */
1896 DBG("Metadata poll thread exiting");
1897 lttng_poll_clean(&events
);
1900 destroy_stream_ht(metadata_ht
);
1903 rcu_unregister_thread();
1908 * This thread polls the fds in the set to consume the data and write
1909 * it to tracefile if necessary.
1911 void *consumer_thread_data_poll(void *data
)
1913 int num_rdy
, num_hup
, high_prio
, ret
, i
;
1914 struct pollfd
*pollfd
= NULL
;
1915 /* local view of the streams */
1916 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
1917 /* local view of consumer_data.fds_count */
1919 struct lttng_consumer_local_data
*ctx
= data
;
1922 rcu_register_thread();
1924 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1925 if (data_ht
== NULL
) {
1929 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
1936 * the fds set has been updated, we need to update our
1937 * local array as well
1939 pthread_mutex_lock(&consumer_data
.lock
);
1940 if (consumer_data
.need_update
) {
1941 if (pollfd
!= NULL
) {
1945 if (local_stream
!= NULL
) {
1947 local_stream
= NULL
;
1950 /* allocate for all fds + 1 for the consumer_data_pipe */
1951 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
1952 if (pollfd
== NULL
) {
1953 PERROR("pollfd malloc");
1954 pthread_mutex_unlock(&consumer_data
.lock
);
1958 /* allocate for all fds + 1 for the consumer_data_pipe */
1959 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
1960 sizeof(struct lttng_consumer_stream
));
1961 if (local_stream
== NULL
) {
1962 PERROR("local_stream malloc");
1963 pthread_mutex_unlock(&consumer_data
.lock
);
1966 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
,
1969 ERR("Error in allocating pollfd or local_outfds");
1970 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
1971 pthread_mutex_unlock(&consumer_data
.lock
);
1975 consumer_data
.need_update
= 0;
1977 pthread_mutex_unlock(&consumer_data
.lock
);
1979 /* No FDs and consumer_quit, consumer_cleanup the thread */
1980 if (nb_fd
== 0 && consumer_quit
== 1) {
1983 /* poll on the array of fds */
1985 DBG("polling on %d fd", nb_fd
+ 1);
1986 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
1987 DBG("poll num_rdy : %d", num_rdy
);
1988 if (num_rdy
== -1) {
1990 * Restart interrupted system call.
1992 if (errno
== EINTR
) {
1995 PERROR("Poll error");
1996 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
1998 } else if (num_rdy
== 0) {
1999 DBG("Polling thread timed out");
2004 * If the consumer_data_pipe triggered poll go directly to the
2005 * beginning of the loop to update the array. We want to prioritize
2006 * array update over low-priority reads.
2008 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2009 size_t pipe_readlen
;
2011 DBG("consumer_data_pipe wake up");
2012 /* Consume 1 byte of pipe data */
2014 pipe_readlen
= read(ctx
->consumer_data_pipe
[0], &new_stream
,
2015 sizeof(new_stream
));
2016 } while (pipe_readlen
== -1 && errno
== EINTR
);
2019 * If the stream is NULL, just ignore it. It's also possible that
2020 * the sessiond poll thread changed the consumer_quit state and is
2021 * waking us up to test it.
2023 if (new_stream
== NULL
) {
2027 ret
= consumer_add_stream(new_stream
, data_ht
);
2029 ERR("Consumer add stream %d failed. Continuing",
2032 * At this point, if the add_stream fails, it is not in the
2033 * hash table thus passing the NULL value here.
2035 consumer_del_stream(new_stream
, NULL
);
2038 /* Continue to update the local streams and handle prio ones */
2042 /* Take care of high priority channels first. */
2043 for (i
= 0; i
< nb_fd
; i
++) {
2044 if (pollfd
[i
].revents
& POLLPRI
) {
2045 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2047 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2048 /* it's ok to have an unavailable sub-buffer */
2049 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2051 } else if (len
> 0) {
2052 local_stream
[i
]->data_read
= 1;
2058 * If we read high prio channel in this loop, try again
2059 * for more high prio data.
2065 /* Take care of low priority channels. */
2066 for (i
= 0; i
< nb_fd
; i
++) {
2067 if ((pollfd
[i
].revents
& POLLIN
) ||
2068 local_stream
[i
]->hangup_flush_done
) {
2069 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2070 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2071 /* it's ok to have an unavailable sub-buffer */
2072 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2074 } else if (len
> 0) {
2075 local_stream
[i
]->data_read
= 1;
2080 /* Handle hangup and errors */
2081 for (i
= 0; i
< nb_fd
; i
++) {
2082 if (!local_stream
[i
]->hangup_flush_done
2083 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2084 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2085 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2086 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2088 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2089 /* Attempt read again, for the data we just flushed. */
2090 local_stream
[i
]->data_read
= 1;
2093 * If the poll flag is HUP/ERR/NVAL and we have
2094 * read no data in this pass, we can remove the
2095 * stream from its hash table.
2097 if ((pollfd
[i
].revents
& POLLHUP
)) {
2098 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2099 if (!local_stream
[i
]->data_read
) {
2100 consumer_del_stream(local_stream
[i
], data_ht
);
2103 } else if (pollfd
[i
].revents
& POLLERR
) {
2104 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2105 if (!local_stream
[i
]->data_read
) {
2106 consumer_del_stream(local_stream
[i
], data_ht
);
2109 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2110 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2111 if (!local_stream
[i
]->data_read
) {
2112 consumer_del_stream(local_stream
[i
], data_ht
);
2116 local_stream
[i
]->data_read
= 0;
2120 DBG("polling thread exiting");
2121 if (pollfd
!= NULL
) {
2125 if (local_stream
!= NULL
) {
2127 local_stream
= NULL
;
2131 * Close the write side of the pipe so epoll_wait() in
2132 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2133 * read side of the pipe. If we close them both, epoll_wait strangely does
2134 * not return and could create a endless wait period if the pipe is the
2135 * only tracked fd in the poll set. The thread will take care of closing
2138 close(ctx
->consumer_metadata_pipe
[1]);
2141 destroy_data_stream_ht(data_ht
);
2144 rcu_unregister_thread();
2149 * This thread listens on the consumerd socket and receives the file
2150 * descriptors from the session daemon.
2152 void *consumer_thread_sessiond_poll(void *data
)
2154 int sock
, client_socket
, ret
;
2156 * structure to poll for incoming data on communication socket avoids
2157 * making blocking sockets.
2159 struct pollfd consumer_sockpoll
[2];
2160 struct lttng_consumer_local_data
*ctx
= data
;
2162 rcu_register_thread();
2164 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2165 unlink(ctx
->consumer_command_sock_path
);
2166 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2167 if (client_socket
< 0) {
2168 ERR("Cannot create command socket");
2172 ret
= lttcomm_listen_unix_sock(client_socket
);
2177 DBG("Sending ready command to lttng-sessiond");
2178 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2179 /* return < 0 on error, but == 0 is not fatal */
2181 ERR("Error sending ready command to lttng-sessiond");
2185 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2187 PERROR("fcntl O_NONBLOCK");
2191 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2192 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2193 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2194 consumer_sockpoll
[1].fd
= client_socket
;
2195 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2197 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2200 DBG("Connection on client_socket");
2202 /* Blocking call, waiting for transmission */
2203 sock
= lttcomm_accept_unix_sock(client_socket
);
2208 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2210 PERROR("fcntl O_NONBLOCK");
2214 /* update the polling structure to poll on the established socket */
2215 consumer_sockpoll
[1].fd
= sock
;
2216 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2219 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2222 DBG("Incoming command on sock");
2223 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2224 if (ret
== -ENOENT
) {
2225 DBG("Received STOP command");
2230 * This could simply be a session daemon quitting. Don't output
2233 DBG("Communication interrupted on command socket");
2236 if (consumer_quit
) {
2237 DBG("consumer_thread_receive_fds received quit from signal");
2240 DBG("received fds on sock");
2243 DBG("consumer_thread_receive_fds exiting");
2246 * when all fds have hung up, the polling thread
2252 * 2s of grace period, if no polling events occur during
2253 * this period, the polling thread will exit even if there
2254 * are still open FDs (should not happen, but safety mechanism).
2256 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
2259 * Notify the data poll thread to poll back again and test the
2260 * consumer_quit state to quit gracefully.
2263 struct lttng_consumer_stream
*null_stream
= NULL
;
2265 ret
= write(ctx
->consumer_data_pipe
[1], &null_stream
,
2266 sizeof(null_stream
));
2267 } while (ret
< 0 && errno
== EINTR
);
2269 rcu_unregister_thread();
2273 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2274 struct lttng_consumer_local_data
*ctx
)
2276 switch (consumer_data
.type
) {
2277 case LTTNG_CONSUMER_KERNEL
:
2278 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
2279 case LTTNG_CONSUMER32_UST
:
2280 case LTTNG_CONSUMER64_UST
:
2281 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2283 ERR("Unknown consumer_data type");
2289 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2291 switch (consumer_data
.type
) {
2292 case LTTNG_CONSUMER_KERNEL
:
2293 return lttng_kconsumer_on_recv_stream(stream
);
2294 case LTTNG_CONSUMER32_UST
:
2295 case LTTNG_CONSUMER64_UST
:
2296 return lttng_ustconsumer_on_recv_stream(stream
);
2298 ERR("Unknown consumer_data type");
2305 * Allocate and set consumer data hash tables.
2307 void lttng_consumer_init(void)
2309 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2310 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2312 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2313 assert(metadata_ht
);
2314 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2319 * Process the ADD_RELAYD command receive by a consumer.
2321 * This will create a relayd socket pair and add it to the relayd hash table.
2322 * The caller MUST acquire a RCU read side lock before calling it.
2324 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
2325 struct lttng_consumer_local_data
*ctx
, int sock
,
2326 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
)
2329 struct consumer_relayd_sock_pair
*relayd
;
2331 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
2333 /* Get relayd reference if exists. */
2334 relayd
= consumer_find_relayd(net_seq_idx
);
2335 if (relayd
== NULL
) {
2336 /* Not found. Allocate one. */
2337 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
2338 if (relayd
== NULL
) {
2339 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
2344 /* Poll on consumer socket. */
2345 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2350 /* Get relayd socket from session daemon */
2351 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2352 if (ret
!= sizeof(fd
)) {
2353 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
2358 /* Copy socket information and received FD */
2359 switch (sock_type
) {
2360 case LTTNG_STREAM_CONTROL
:
2361 /* Copy received lttcomm socket */
2362 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
2363 ret
= lttcomm_create_sock(&relayd
->control_sock
);
2368 /* Close the created socket fd which is useless */
2369 close(relayd
->control_sock
.fd
);
2371 /* Assign new file descriptor */
2372 relayd
->control_sock
.fd
= fd
;
2374 case LTTNG_STREAM_DATA
:
2375 /* Copy received lttcomm socket */
2376 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
2377 ret
= lttcomm_create_sock(&relayd
->data_sock
);
2382 /* Close the created socket fd which is useless */
2383 close(relayd
->data_sock
.fd
);
2385 /* Assign new file descriptor */
2386 relayd
->data_sock
.fd
= fd
;
2389 ERR("Unknown relayd socket type (%d)", sock_type
);
2393 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2394 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
2395 relayd
->net_seq_idx
, fd
);
2398 * Add relayd socket pair to consumer data hashtable. If object already
2399 * exists or on error, the function gracefully returns.