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
,
370 struct lttng_consumer_stream
*stream
;
372 stream
= zmalloc(sizeof(*stream
));
373 if (stream
== NULL
) {
374 PERROR("malloc struct lttng_consumer_stream");
375 *alloc_ret
= -ENOMEM
;
380 * Get stream's channel reference. Needed when adding the stream to the
383 stream
->chan
= consumer_find_channel(channel_key
);
385 *alloc_ret
= -ENOENT
;
386 ERR("Unable to find channel for stream %d", stream_key
);
390 stream
->key
= stream_key
;
391 stream
->shm_fd
= shm_fd
;
392 stream
->wait_fd
= wait_fd
;
394 stream
->out_fd_offset
= 0;
395 stream
->state
= state
;
396 stream
->mmap_len
= mmap_len
;
397 stream
->mmap_base
= NULL
;
398 stream
->output
= output
;
401 stream
->net_seq_idx
= net_index
;
402 stream
->metadata_flag
= metadata_flag
;
403 stream
->session_id
= session_id
;
404 strncpy(stream
->path_name
, path_name
, sizeof(stream
->path_name
));
405 stream
->path_name
[sizeof(stream
->path_name
) - 1] = '\0';
406 pthread_mutex_init(&stream
->lock
, NULL
);
409 * Index differently the metadata node because the thread is using an
410 * internal hash table to match streams in the metadata_ht to the epoll set
414 lttng_ht_node_init_ulong(&stream
->node
, stream
->wait_fd
);
416 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
419 /* Init session id node with the stream session id */
420 lttng_ht_node_init_ulong(&stream
->node_session_id
, stream
->session_id
);
423 * The cpu number is needed before using any ustctl_* actions. Ignored for
424 * the kernel so the value does not matter.
426 pthread_mutex_lock(&consumer_data
.lock
);
427 stream
->cpu
= stream
->chan
->cpucount
++;
428 pthread_mutex_unlock(&consumer_data
.lock
);
430 DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu,"
431 " out_fd %d, net_seq_idx %d, session_id %" PRIu64
,
432 stream
->path_name
, stream
->key
, stream
->shm_fd
, stream
->wait_fd
,
433 (unsigned long long) stream
->mmap_len
, stream
->out_fd
,
434 stream
->net_seq_idx
, stream
->session_id
);
444 * Add a stream to the global list protected by a mutex.
446 static int consumer_add_stream(struct lttng_consumer_stream
*stream
,
450 struct consumer_relayd_sock_pair
*relayd
;
455 DBG3("Adding consumer stream %d", stream
->key
);
457 pthread_mutex_lock(&consumer_data
.lock
);
460 /* Steal stream identifier to avoid having streams with the same key */
461 consumer_steal_stream_key(stream
->key
, ht
);
463 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
465 /* Check and cleanup relayd */
466 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
467 if (relayd
!= NULL
) {
468 uatomic_inc(&relayd
->refcount
);
471 /* Update channel refcount once added without error(s). */
472 uatomic_inc(&stream
->chan
->refcount
);
475 * When nb_init_streams reaches 0, we don't need to trigger any action in
476 * terms of destroying the associated channel, because the action that
477 * causes the count to become 0 also causes a stream to be added. The
478 * channel deletion will thus be triggered by the following removal of this
481 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
482 uatomic_dec(&stream
->chan
->nb_init_streams
);
485 /* Update consumer data once the node is inserted. */
486 consumer_data
.stream_count
++;
487 consumer_data
.need_update
= 1;
490 pthread_mutex_unlock(&consumer_data
.lock
);
496 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
497 * be acquired before calling this.
499 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
502 struct lttng_ht_node_ulong
*node
;
503 struct lttng_ht_iter iter
;
505 if (relayd
== NULL
) {
510 lttng_ht_lookup(consumer_data
.relayd_ht
,
511 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
512 node
= lttng_ht_iter_get_node_ulong(&iter
);
514 /* Relayd already exist. Ignore the insertion */
517 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
524 * Allocate and return a consumer relayd socket.
526 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
529 struct consumer_relayd_sock_pair
*obj
= NULL
;
531 /* Negative net sequence index is a failure */
532 if (net_seq_idx
< 0) {
536 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
538 PERROR("zmalloc relayd sock");
542 obj
->net_seq_idx
= net_seq_idx
;
544 obj
->destroy_flag
= 0;
545 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
546 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
553 * Find a relayd socket pair in the global consumer data.
555 * Return the object if found else NULL.
556 * RCU read-side lock must be held across this call and while using the
559 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
561 struct lttng_ht_iter iter
;
562 struct lttng_ht_node_ulong
*node
;
563 struct consumer_relayd_sock_pair
*relayd
= NULL
;
565 /* Negative keys are lookup failures */
570 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
572 node
= lttng_ht_iter_get_node_ulong(&iter
);
574 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
582 * Handle stream for relayd transmission if the stream applies for network
583 * streaming where the net sequence index is set.
585 * Return destination file descriptor or negative value on error.
587 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
588 size_t data_size
, unsigned long padding
,
589 struct consumer_relayd_sock_pair
*relayd
)
592 struct lttcomm_relayd_data_hdr data_hdr
;
598 /* Reset data header */
599 memset(&data_hdr
, 0, sizeof(data_hdr
));
601 if (stream
->metadata_flag
) {
602 /* Caller MUST acquire the relayd control socket lock */
603 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
608 /* Metadata are always sent on the control socket. */
609 outfd
= relayd
->control_sock
.fd
;
611 /* Set header with stream information */
612 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
613 data_hdr
.data_size
= htobe32(data_size
);
614 data_hdr
.padding_size
= htobe32(padding
);
615 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
616 /* Other fields are zeroed previously */
618 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
624 /* Set to go on data socket */
625 outfd
= relayd
->data_sock
.fd
;
633 void consumer_free_channel(struct rcu_head
*head
)
635 struct lttng_ht_node_ulong
*node
=
636 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
637 struct lttng_consumer_channel
*channel
=
638 caa_container_of(node
, struct lttng_consumer_channel
, node
);
644 * Remove a channel from the global list protected by a mutex. This
645 * function is also responsible for freeing its data structures.
647 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
650 struct lttng_ht_iter iter
;
652 pthread_mutex_lock(&consumer_data
.lock
);
654 switch (consumer_data
.type
) {
655 case LTTNG_CONSUMER_KERNEL
:
657 case LTTNG_CONSUMER32_UST
:
658 case LTTNG_CONSUMER64_UST
:
659 lttng_ustconsumer_del_channel(channel
);
662 ERR("Unknown consumer_data type");
668 iter
.iter
.node
= &channel
->node
.node
;
669 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
673 if (channel
->mmap_base
!= NULL
) {
674 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
679 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
680 ret
= close(channel
->wait_fd
);
685 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
686 ret
= close(channel
->shm_fd
);
692 call_rcu(&channel
->node
.head
, consumer_free_channel
);
694 pthread_mutex_unlock(&consumer_data
.lock
);
697 struct lttng_consumer_channel
*consumer_allocate_channel(
699 int shm_fd
, int wait_fd
,
701 uint64_t max_sb_size
,
702 unsigned int nb_init_streams
)
704 struct lttng_consumer_channel
*channel
;
707 channel
= zmalloc(sizeof(*channel
));
708 if (channel
== NULL
) {
709 PERROR("malloc struct lttng_consumer_channel");
712 channel
->key
= channel_key
;
713 channel
->shm_fd
= shm_fd
;
714 channel
->wait_fd
= wait_fd
;
715 channel
->mmap_len
= mmap_len
;
716 channel
->max_sb_size
= max_sb_size
;
717 channel
->refcount
= 0;
718 channel
->nb_init_streams
= nb_init_streams
;
719 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
721 switch (consumer_data
.type
) {
722 case LTTNG_CONSUMER_KERNEL
:
723 channel
->mmap_base
= NULL
;
724 channel
->mmap_len
= 0;
726 case LTTNG_CONSUMER32_UST
:
727 case LTTNG_CONSUMER64_UST
:
728 ret
= lttng_ustconsumer_allocate_channel(channel
);
735 ERR("Unknown consumer_data type");
739 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
740 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
741 (unsigned long long) channel
->mmap_len
,
742 (unsigned long long) channel
->max_sb_size
);
748 * Add a channel to the global list protected by a mutex.
750 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
752 struct lttng_ht_node_ulong
*node
;
753 struct lttng_ht_iter iter
;
755 pthread_mutex_lock(&consumer_data
.lock
);
756 /* Steal channel identifier, for UST */
757 consumer_steal_channel_key(channel
->key
);
760 lttng_ht_lookup(consumer_data
.channel_ht
,
761 (void *)((unsigned long) channel
->key
), &iter
);
762 node
= lttng_ht_iter_get_node_ulong(&iter
);
764 /* Channel already exist. Ignore the insertion */
768 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
772 pthread_mutex_unlock(&consumer_data
.lock
);
778 * Allocate the pollfd structure and the local view of the out fds to avoid
779 * doing a lookup in the linked list and concurrency issues when writing is
780 * needed. Called with consumer_data.lock held.
782 * Returns the number of fds in the structures.
784 static int consumer_update_poll_array(
785 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
786 struct lttng_consumer_stream
**local_stream
, struct lttng_ht
*ht
)
789 struct lttng_ht_iter iter
;
790 struct lttng_consumer_stream
*stream
;
792 DBG("Updating poll fd array");
794 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
795 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
798 DBG("Active FD %d", stream
->wait_fd
);
799 (*pollfd
)[i
].fd
= stream
->wait_fd
;
800 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
801 local_stream
[i
] = stream
;
807 * Insert the consumer_data_pipe at the end of the array and don't
808 * increment i so nb_fd is the number of real FD.
810 (*pollfd
)[i
].fd
= ctx
->consumer_data_pipe
[0];
811 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
816 * Poll on the should_quit pipe and the command socket return -1 on error and
817 * should exit, 0 if data is available on the command socket
819 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
824 num_rdy
= poll(consumer_sockpoll
, 2, -1);
827 * Restart interrupted system call.
829 if (errno
== EINTR
) {
832 PERROR("Poll error");
835 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
836 DBG("consumer_should_quit wake up");
846 * Set the error socket.
848 void lttng_consumer_set_error_sock(
849 struct lttng_consumer_local_data
*ctx
, int sock
)
851 ctx
->consumer_error_socket
= sock
;
855 * Set the command socket path.
857 void lttng_consumer_set_command_sock_path(
858 struct lttng_consumer_local_data
*ctx
, char *sock
)
860 ctx
->consumer_command_sock_path
= sock
;
864 * Send return code to the session daemon.
865 * If the socket is not defined, we return 0, it is not a fatal error
867 int lttng_consumer_send_error(
868 struct lttng_consumer_local_data
*ctx
, int cmd
)
870 if (ctx
->consumer_error_socket
> 0) {
871 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
872 sizeof(enum lttcomm_sessiond_command
));
879 * Close all the tracefiles and stream fds, should be called when all instances
882 void lttng_consumer_cleanup(void)
884 struct lttng_ht_iter iter
;
885 struct lttng_ht_node_ulong
*node
;
889 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
891 struct lttng_consumer_channel
*channel
=
892 caa_container_of(node
, struct lttng_consumer_channel
, node
);
893 consumer_del_channel(channel
);
898 lttng_ht_destroy(consumer_data
.channel_ht
);
902 * Called from signal handler.
904 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
909 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
910 } while (ret
< 0 && errno
== EINTR
);
912 PERROR("write consumer quit");
916 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
919 int outfd
= stream
->out_fd
;
922 * This does a blocking write-and-wait on any page that belongs to the
923 * subbuffer prior to the one we just wrote.
924 * Don't care about error values, as these are just hints and ways to
925 * limit the amount of page cache used.
927 if (orig_offset
< stream
->chan
->max_sb_size
) {
930 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
931 stream
->chan
->max_sb_size
,
932 SYNC_FILE_RANGE_WAIT_BEFORE
933 | SYNC_FILE_RANGE_WRITE
934 | SYNC_FILE_RANGE_WAIT_AFTER
);
936 * Give hints to the kernel about how we access the file:
937 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
940 * We need to call fadvise again after the file grows because the
941 * kernel does not seem to apply fadvise to non-existing parts of the
944 * Call fadvise _after_ having waited for the page writeback to
945 * complete because the dirty page writeback semantic is not well
946 * defined. So it can be expected to lead to lower throughput in
949 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
950 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
954 * Initialise the necessary environnement :
955 * - create a new context
956 * - create the poll_pipe
957 * - create the should_quit pipe (for signal handler)
958 * - create the thread pipe (for splice)
960 * Takes a function pointer as argument, this function is called when data is
961 * available on a buffer. This function is responsible to do the
962 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
963 * buffer configuration and then kernctl_put_next_subbuf at the end.
965 * Returns a pointer to the new context or NULL on error.
967 struct lttng_consumer_local_data
*lttng_consumer_create(
968 enum lttng_consumer_type type
,
969 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
970 struct lttng_consumer_local_data
*ctx
),
971 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
972 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
973 int (*update_stream
)(int stream_key
, uint32_t state
))
976 struct lttng_consumer_local_data
*ctx
;
978 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
979 consumer_data
.type
== type
);
980 consumer_data
.type
= type
;
982 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
984 PERROR("allocating context");
988 ctx
->consumer_error_socket
= -1;
989 /* assign the callbacks */
990 ctx
->on_buffer_ready
= buffer_ready
;
991 ctx
->on_recv_channel
= recv_channel
;
992 ctx
->on_recv_stream
= recv_stream
;
993 ctx
->on_update_stream
= update_stream
;
995 ret
= pipe(ctx
->consumer_data_pipe
);
997 PERROR("Error creating poll pipe");
998 goto error_poll_pipe
;
1001 /* set read end of the pipe to non-blocking */
1002 ret
= fcntl(ctx
->consumer_data_pipe
[0], F_SETFL
, O_NONBLOCK
);
1004 PERROR("fcntl O_NONBLOCK");
1005 goto error_poll_fcntl
;
1008 /* set write end of the pipe to non-blocking */
1009 ret
= fcntl(ctx
->consumer_data_pipe
[1], F_SETFL
, O_NONBLOCK
);
1011 PERROR("fcntl O_NONBLOCK");
1012 goto error_poll_fcntl
;
1015 ret
= pipe(ctx
->consumer_should_quit
);
1017 PERROR("Error creating recv pipe");
1018 goto error_quit_pipe
;
1021 ret
= pipe(ctx
->consumer_thread_pipe
);
1023 PERROR("Error creating thread pipe");
1024 goto error_thread_pipe
;
1027 ret
= utils_create_pipe(ctx
->consumer_metadata_pipe
);
1029 goto error_metadata_pipe
;
1032 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1034 goto error_splice_pipe
;
1040 utils_close_pipe(ctx
->consumer_metadata_pipe
);
1041 error_metadata_pipe
:
1042 utils_close_pipe(ctx
->consumer_thread_pipe
);
1044 for (i
= 0; i
< 2; i
++) {
1047 err
= close(ctx
->consumer_should_quit
[i
]);
1054 for (i
= 0; i
< 2; i
++) {
1057 err
= close(ctx
->consumer_data_pipe
[i
]);
1069 * Close all fds associated with the instance and free the context.
1071 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1075 ret
= close(ctx
->consumer_error_socket
);
1079 ret
= close(ctx
->consumer_thread_pipe
[0]);
1083 ret
= close(ctx
->consumer_thread_pipe
[1]);
1087 ret
= close(ctx
->consumer_data_pipe
[0]);
1091 ret
= close(ctx
->consumer_data_pipe
[1]);
1095 ret
= close(ctx
->consumer_should_quit
[0]);
1099 ret
= close(ctx
->consumer_should_quit
[1]);
1103 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1105 unlink(ctx
->consumer_command_sock_path
);
1110 * Write the metadata stream id on the specified file descriptor.
1112 static int write_relayd_metadata_id(int fd
,
1113 struct lttng_consumer_stream
*stream
,
1114 struct consumer_relayd_sock_pair
*relayd
,
1115 unsigned long padding
)
1118 struct lttcomm_relayd_metadata_payload hdr
;
1120 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1121 hdr
.padding_size
= htobe32(padding
);
1123 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1124 } while (ret
< 0 && errno
== EINTR
);
1126 PERROR("write metadata stream id");
1129 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1130 stream
->relayd_stream_id
, padding
);
1137 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1138 * core function for writing trace buffers to either the local filesystem or
1141 * Careful review MUST be put if any changes occur!
1143 * Returns the number of bytes written
1145 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1146 struct lttng_consumer_local_data
*ctx
,
1147 struct lttng_consumer_stream
*stream
, unsigned long len
,
1148 unsigned long padding
)
1150 unsigned long mmap_offset
;
1151 ssize_t ret
= 0, written
= 0;
1152 off_t orig_offset
= stream
->out_fd_offset
;
1153 /* Default is on the disk */
1154 int outfd
= stream
->out_fd
;
1155 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1157 /* RCU lock for the relayd pointer */
1160 /* Flag that the current stream if set for network streaming. */
1161 if (stream
->net_seq_idx
!= -1) {
1162 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1163 if (relayd
== NULL
) {
1168 /* get the offset inside the fd to mmap */
1169 switch (consumer_data
.type
) {
1170 case LTTNG_CONSUMER_KERNEL
:
1171 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1173 case LTTNG_CONSUMER32_UST
:
1174 case LTTNG_CONSUMER64_UST
:
1175 ret
= lttng_ustctl_get_mmap_read_offset(stream
->chan
->handle
,
1176 stream
->buf
, &mmap_offset
);
1179 ERR("Unknown consumer_data type");
1184 PERROR("tracer ctl get_mmap_read_offset");
1189 /* Handle stream on the relayd if the output is on the network */
1191 unsigned long netlen
= len
;
1194 * Lock the control socket for the complete duration of the function
1195 * since from this point on we will use the socket.
1197 if (stream
->metadata_flag
) {
1198 /* Metadata requires the control socket. */
1199 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1200 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1203 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1205 /* Use the returned socket. */
1208 /* Write metadata stream id before payload */
1209 if (stream
->metadata_flag
) {
1210 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1217 /* Else, use the default set before which is the filesystem. */
1219 /* No streaming, we have to set the len with the full padding */
1225 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
1226 } while (ret
< 0 && errno
== EINTR
);
1227 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1229 PERROR("Error in file write");
1234 } else if (ret
> len
) {
1235 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1243 /* This call is useless on a socket so better save a syscall. */
1245 /* This won't block, but will start writeout asynchronously */
1246 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1247 SYNC_FILE_RANGE_WRITE
);
1248 stream
->out_fd_offset
+= ret
;
1252 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1255 /* Unlock only if ctrl socket used */
1256 if (relayd
&& stream
->metadata_flag
) {
1257 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1265 * Splice the data from the ring buffer to the tracefile.
1267 * Returns the number of bytes spliced.
1269 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1270 struct lttng_consumer_local_data
*ctx
,
1271 struct lttng_consumer_stream
*stream
, unsigned long len
,
1272 unsigned long padding
)
1274 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1276 off_t orig_offset
= stream
->out_fd_offset
;
1277 int fd
= stream
->wait_fd
;
1278 /* Default is on the disk */
1279 int outfd
= stream
->out_fd
;
1280 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1283 switch (consumer_data
.type
) {
1284 case LTTNG_CONSUMER_KERNEL
:
1286 case LTTNG_CONSUMER32_UST
:
1287 case LTTNG_CONSUMER64_UST
:
1288 /* Not supported for user space tracing */
1291 ERR("Unknown consumer_data type");
1295 /* RCU lock for the relayd pointer */
1298 /* Flag that the current stream if set for network streaming. */
1299 if (stream
->net_seq_idx
!= -1) {
1300 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1301 if (relayd
== NULL
) {
1307 * Choose right pipe for splice. Metadata and trace data are handled by
1308 * different threads hence the use of two pipes in order not to race or
1309 * corrupt the written data.
1311 if (stream
->metadata_flag
) {
1312 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1314 splice_pipe
= ctx
->consumer_thread_pipe
;
1317 /* Write metadata stream id before payload */
1319 int total_len
= len
;
1321 if (stream
->metadata_flag
) {
1323 * Lock the control socket for the complete duration of the function
1324 * since from this point on we will use the socket.
1326 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1328 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1335 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1338 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1340 /* Use the returned socket. */
1343 ERR("Remote relayd disconnected. Stopping");
1347 /* No streaming, we have to set the len with the full padding */
1352 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1353 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1354 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1355 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1356 DBG("splice chan to pipe, ret %zd", ret_splice
);
1357 if (ret_splice
< 0) {
1358 PERROR("Error in relay splice");
1360 written
= ret_splice
;
1366 /* Handle stream on the relayd if the output is on the network */
1368 if (stream
->metadata_flag
) {
1369 size_t metadata_payload_size
=
1370 sizeof(struct lttcomm_relayd_metadata_payload
);
1372 /* Update counter to fit the spliced data */
1373 ret_splice
+= metadata_payload_size
;
1374 len
+= metadata_payload_size
;
1376 * We do this so the return value can match the len passed as
1377 * argument to this function.
1379 written
-= metadata_payload_size
;
1383 /* Splice data out */
1384 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1385 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1386 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1387 if (ret_splice
< 0) {
1388 PERROR("Error in file splice");
1390 written
= ret_splice
;
1394 } else if (ret_splice
> len
) {
1396 PERROR("Wrote more data than requested %zd (len: %lu)",
1398 written
+= ret_splice
;
1404 /* This call is useless on a socket so better save a syscall. */
1406 /* This won't block, but will start writeout asynchronously */
1407 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1408 SYNC_FILE_RANGE_WRITE
);
1409 stream
->out_fd_offset
+= ret_splice
;
1411 written
+= ret_splice
;
1413 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1420 /* send the appropriate error description to sessiond */
1423 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EBADF
);
1426 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1429 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1432 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1437 if (relayd
&& stream
->metadata_flag
) {
1438 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1446 * Take a snapshot for a specific fd
1448 * Returns 0 on success, < 0 on error
1450 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1451 struct lttng_consumer_stream
*stream
)
1453 switch (consumer_data
.type
) {
1454 case LTTNG_CONSUMER_KERNEL
:
1455 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1456 case LTTNG_CONSUMER32_UST
:
1457 case LTTNG_CONSUMER64_UST
:
1458 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1460 ERR("Unknown consumer_data type");
1468 * Get the produced position
1470 * Returns 0 on success, < 0 on error
1472 int lttng_consumer_get_produced_snapshot(
1473 struct lttng_consumer_local_data
*ctx
,
1474 struct lttng_consumer_stream
*stream
,
1477 switch (consumer_data
.type
) {
1478 case LTTNG_CONSUMER_KERNEL
:
1479 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1480 case LTTNG_CONSUMER32_UST
:
1481 case LTTNG_CONSUMER64_UST
:
1482 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1484 ERR("Unknown consumer_data type");
1490 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1491 int sock
, struct pollfd
*consumer_sockpoll
)
1493 switch (consumer_data
.type
) {
1494 case LTTNG_CONSUMER_KERNEL
:
1495 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1496 case LTTNG_CONSUMER32_UST
:
1497 case LTTNG_CONSUMER64_UST
:
1498 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1500 ERR("Unknown consumer_data type");
1507 * Iterate over all streams of the hashtable and free them properly.
1509 * WARNING: *MUST* be used with data stream only.
1511 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1514 struct lttng_ht_iter iter
;
1515 struct lttng_consumer_stream
*stream
;
1522 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1523 ret
= lttng_ht_del(ht
, &iter
);
1526 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1530 lttng_ht_destroy(ht
);
1534 * Iterate over all streams of the hashtable and free them properly.
1536 * XXX: Should not be only for metadata stream or else use an other name.
1538 static void destroy_stream_ht(struct lttng_ht
*ht
)
1541 struct lttng_ht_iter iter
;
1542 struct lttng_consumer_stream
*stream
;
1549 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1550 ret
= lttng_ht_del(ht
, &iter
);
1553 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1557 lttng_ht_destroy(ht
);
1561 * Clean up a metadata stream and free its memory.
1563 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1564 struct lttng_ht
*ht
)
1567 struct lttng_ht_iter iter
;
1568 struct lttng_consumer_channel
*free_chan
= NULL
;
1569 struct consumer_relayd_sock_pair
*relayd
;
1573 * This call should NEVER receive regular stream. It must always be
1574 * metadata stream and this is crucial for data structure synchronization.
1576 assert(stream
->metadata_flag
);
1578 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1581 /* Means the stream was allocated but not successfully added */
1585 pthread_mutex_lock(&consumer_data
.lock
);
1586 switch (consumer_data
.type
) {
1587 case LTTNG_CONSUMER_KERNEL
:
1588 if (stream
->mmap_base
!= NULL
) {
1589 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1591 PERROR("munmap metadata stream");
1595 case LTTNG_CONSUMER32_UST
:
1596 case LTTNG_CONSUMER64_UST
:
1597 lttng_ustconsumer_del_stream(stream
);
1600 ERR("Unknown consumer_data type");
1606 iter
.iter
.node
= &stream
->node
.node
;
1607 ret
= lttng_ht_del(ht
, &iter
);
1611 if (stream
->out_fd
>= 0) {
1612 ret
= close(stream
->out_fd
);
1618 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
1619 ret
= close(stream
->wait_fd
);
1625 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
1626 ret
= close(stream
->shm_fd
);
1632 /* Check and cleanup relayd */
1634 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1635 if (relayd
!= NULL
) {
1636 uatomic_dec(&relayd
->refcount
);
1637 assert(uatomic_read(&relayd
->refcount
) >= 0);
1639 /* Closing streams requires to lock the control socket. */
1640 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1641 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1642 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1643 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1645 DBG("Unable to close stream on the relayd. Continuing");
1647 * Continue here. There is nothing we can do for the relayd.
1648 * Chances are that the relayd has closed the socket so we just
1649 * continue cleaning up.
1653 /* Both conditions are met, we destroy the relayd. */
1654 if (uatomic_read(&relayd
->refcount
) == 0 &&
1655 uatomic_read(&relayd
->destroy_flag
)) {
1656 destroy_relayd(relayd
);
1661 /* Atomically decrement channel refcount since other threads can use it. */
1662 uatomic_dec(&stream
->chan
->refcount
);
1663 if (!uatomic_read(&stream
->chan
->refcount
)
1664 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
1665 /* Go for channel deletion! */
1666 free_chan
= stream
->chan
;
1670 pthread_mutex_unlock(&consumer_data
.lock
);
1673 consumer_del_channel(free_chan
);
1677 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1681 * Action done with the metadata stream when adding it to the consumer internal
1682 * data structures to handle it.
1684 static int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
,
1685 struct lttng_ht
*ht
)
1688 struct consumer_relayd_sock_pair
*relayd
;
1693 DBG3("Adding metadata stream %d to hash table", stream
->wait_fd
);
1695 pthread_mutex_lock(&consumer_data
.lock
);
1698 * From here, refcounts are updated so be _careful_ when returning an error
1703 /* Find relayd and, if one is found, increment refcount. */
1704 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1705 if (relayd
!= NULL
) {
1706 uatomic_inc(&relayd
->refcount
);
1709 /* Update channel refcount once added without error(s). */
1710 uatomic_inc(&stream
->chan
->refcount
);
1713 * When nb_init_streams reaches 0, we don't need to trigger any action in
1714 * terms of destroying the associated channel, because the action that
1715 * causes the count to become 0 also causes a stream to be added. The
1716 * channel deletion will thus be triggered by the following removal of this
1719 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
1720 uatomic_dec(&stream
->chan
->nb_init_streams
);
1723 /* Steal stream identifier to avoid having streams with the same key */
1724 consumer_steal_stream_key(stream
->key
, ht
);
1726 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
1729 pthread_mutex_unlock(&consumer_data
.lock
);
1734 * Thread polls on metadata file descriptor and write them on disk or on the
1737 void *consumer_thread_metadata_poll(void *data
)
1740 uint32_t revents
, nb_fd
;
1741 struct lttng_consumer_stream
*stream
= NULL
;
1742 struct lttng_ht_iter iter
;
1743 struct lttng_ht_node_ulong
*node
;
1744 struct lttng_poll_event events
;
1745 struct lttng_consumer_local_data
*ctx
= data
;
1748 rcu_register_thread();
1750 DBG("Thread metadata poll started");
1752 /* Size is set to 1 for the consumer_metadata pipe */
1753 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
1755 ERR("Poll set creation failed");
1759 ret
= lttng_poll_add(&events
, ctx
->consumer_metadata_pipe
[0], LPOLLIN
);
1765 DBG("Metadata main loop started");
1768 lttng_poll_reset(&events
);
1770 nb_fd
= LTTNG_POLL_GETNB(&events
);
1772 /* Only the metadata pipe is set */
1773 if (nb_fd
== 0 && consumer_quit
== 1) {
1778 DBG("Metadata poll wait with %d fd(s)", nb_fd
);
1779 ret
= lttng_poll_wait(&events
, -1);
1780 DBG("Metadata event catched in thread");
1782 if (errno
== EINTR
) {
1783 ERR("Poll EINTR catched");
1789 /* From here, the event is a metadata wait fd */
1790 for (i
= 0; i
< nb_fd
; i
++) {
1791 revents
= LTTNG_POLL_GETEV(&events
, i
);
1792 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1794 /* Just don't waste time if no returned events for the fd */
1799 if (pollfd
== ctx
->consumer_metadata_pipe
[0]) {
1800 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
1801 DBG("Metadata thread pipe hung up");
1803 * Remove the pipe from the poll set and continue the loop
1804 * since their might be data to consume.
1806 lttng_poll_del(&events
, ctx
->consumer_metadata_pipe
[0]);
1807 close(ctx
->consumer_metadata_pipe
[0]);
1809 } else if (revents
& LPOLLIN
) {
1811 /* Get the stream pointer received */
1812 ret
= read(pollfd
, &stream
, sizeof(stream
));
1813 } while (ret
< 0 && errno
== EINTR
);
1815 ret
< sizeof(struct lttng_consumer_stream
*)) {
1816 PERROR("read metadata stream");
1818 * Let's continue here and hope we can still work
1819 * without stopping the consumer. XXX: Should we?
1824 DBG("Adding metadata stream %d to poll set",
1827 ret
= consumer_add_metadata_stream(stream
, metadata_ht
);
1829 ERR("Unable to add metadata stream");
1830 /* Stream was not setup properly. Continuing. */
1831 consumer_del_metadata_stream(stream
, NULL
);
1835 /* Add metadata stream to the global poll events list */
1836 lttng_poll_add(&events
, stream
->wait_fd
,
1837 LPOLLIN
| LPOLLPRI
);
1840 /* Handle other stream */
1845 lttng_ht_lookup(metadata_ht
, (void *)((unsigned long) pollfd
),
1847 node
= lttng_ht_iter_get_node_ulong(&iter
);
1850 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
1853 /* Check for error event */
1854 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
1855 DBG("Metadata fd %d is hup|err.", pollfd
);
1856 if (!stream
->hangup_flush_done
1857 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1858 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1859 DBG("Attempting to flush and consume the UST buffers");
1860 lttng_ustconsumer_on_stream_hangup(stream
);
1862 /* We just flushed the stream now read it. */
1864 len
= ctx
->on_buffer_ready(stream
, ctx
);
1866 * We don't check the return value here since if we get
1867 * a negative len, it means an error occured thus we
1868 * simply remove it from the poll set and free the
1874 lttng_poll_del(&events
, stream
->wait_fd
);
1876 * This call update the channel states, closes file descriptors
1877 * and securely free the stream.
1879 consumer_del_metadata_stream(stream
, metadata_ht
);
1880 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
1881 /* Get the data out of the metadata file descriptor */
1882 DBG("Metadata available on fd %d", pollfd
);
1883 assert(stream
->wait_fd
== pollfd
);
1885 len
= ctx
->on_buffer_ready(stream
, ctx
);
1886 /* It's ok to have an unavailable sub-buffer */
1887 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
1890 } else if (len
> 0) {
1891 stream
->data_read
= 1;
1895 /* Release RCU lock for the stream looked up */
1902 DBG("Metadata poll thread exiting");
1903 lttng_poll_clean(&events
);
1906 destroy_stream_ht(metadata_ht
);
1909 rcu_unregister_thread();
1914 * This thread polls the fds in the set to consume the data and write
1915 * it to tracefile if necessary.
1917 void *consumer_thread_data_poll(void *data
)
1919 int num_rdy
, num_hup
, high_prio
, ret
, i
;
1920 struct pollfd
*pollfd
= NULL
;
1921 /* local view of the streams */
1922 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
1923 /* local view of consumer_data.fds_count */
1925 struct lttng_consumer_local_data
*ctx
= data
;
1928 rcu_register_thread();
1930 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1931 if (data_ht
== NULL
) {
1935 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
1942 * the fds set has been updated, we need to update our
1943 * local array as well
1945 pthread_mutex_lock(&consumer_data
.lock
);
1946 if (consumer_data
.need_update
) {
1947 if (pollfd
!= NULL
) {
1951 if (local_stream
!= NULL
) {
1953 local_stream
= NULL
;
1956 /* allocate for all fds + 1 for the consumer_data_pipe */
1957 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
1958 if (pollfd
== NULL
) {
1959 PERROR("pollfd malloc");
1960 pthread_mutex_unlock(&consumer_data
.lock
);
1964 /* allocate for all fds + 1 for the consumer_data_pipe */
1965 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
1966 sizeof(struct lttng_consumer_stream
));
1967 if (local_stream
== NULL
) {
1968 PERROR("local_stream malloc");
1969 pthread_mutex_unlock(&consumer_data
.lock
);
1972 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
,
1975 ERR("Error in allocating pollfd or local_outfds");
1976 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
1977 pthread_mutex_unlock(&consumer_data
.lock
);
1981 consumer_data
.need_update
= 0;
1983 pthread_mutex_unlock(&consumer_data
.lock
);
1985 /* No FDs and consumer_quit, consumer_cleanup the thread */
1986 if (nb_fd
== 0 && consumer_quit
== 1) {
1989 /* poll on the array of fds */
1991 DBG("polling on %d fd", nb_fd
+ 1);
1992 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
1993 DBG("poll num_rdy : %d", num_rdy
);
1994 if (num_rdy
== -1) {
1996 * Restart interrupted system call.
1998 if (errno
== EINTR
) {
2001 PERROR("Poll error");
2002 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2004 } else if (num_rdy
== 0) {
2005 DBG("Polling thread timed out");
2010 * If the consumer_data_pipe triggered poll go directly to the
2011 * beginning of the loop to update the array. We want to prioritize
2012 * array update over low-priority reads.
2014 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2015 size_t pipe_readlen
;
2017 DBG("consumer_data_pipe wake up");
2018 /* Consume 1 byte of pipe data */
2020 pipe_readlen
= read(ctx
->consumer_data_pipe
[0], &new_stream
,
2021 sizeof(new_stream
));
2022 } while (pipe_readlen
== -1 && errno
== EINTR
);
2025 * If the stream is NULL, just ignore it. It's also possible that
2026 * the sessiond poll thread changed the consumer_quit state and is
2027 * waking us up to test it.
2029 if (new_stream
== NULL
) {
2033 ret
= consumer_add_stream(new_stream
, data_ht
);
2035 ERR("Consumer add stream %d failed. Continuing",
2038 * At this point, if the add_stream fails, it is not in the
2039 * hash table thus passing the NULL value here.
2041 consumer_del_stream(new_stream
, NULL
);
2044 /* Continue to update the local streams and handle prio ones */
2048 /* Take care of high priority channels first. */
2049 for (i
= 0; i
< nb_fd
; i
++) {
2050 if (pollfd
[i
].revents
& POLLPRI
) {
2051 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2053 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2054 /* it's ok to have an unavailable sub-buffer */
2055 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2057 } else if (len
> 0) {
2058 local_stream
[i
]->data_read
= 1;
2064 * If we read high prio channel in this loop, try again
2065 * for more high prio data.
2071 /* Take care of low priority channels. */
2072 for (i
= 0; i
< nb_fd
; i
++) {
2073 if ((pollfd
[i
].revents
& POLLIN
) ||
2074 local_stream
[i
]->hangup_flush_done
) {
2075 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2076 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2077 /* it's ok to have an unavailable sub-buffer */
2078 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2080 } else if (len
> 0) {
2081 local_stream
[i
]->data_read
= 1;
2086 /* Handle hangup and errors */
2087 for (i
= 0; i
< nb_fd
; i
++) {
2088 if (!local_stream
[i
]->hangup_flush_done
2089 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2090 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2091 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2092 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2094 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2095 /* Attempt read again, for the data we just flushed. */
2096 local_stream
[i
]->data_read
= 1;
2099 * If the poll flag is HUP/ERR/NVAL and we have
2100 * read no data in this pass, we can remove the
2101 * stream from its hash table.
2103 if ((pollfd
[i
].revents
& POLLHUP
)) {
2104 DBG("Polling fd %d tells it has hung up.", 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
& POLLERR
) {
2110 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2111 if (!local_stream
[i
]->data_read
) {
2112 consumer_del_stream(local_stream
[i
], data_ht
);
2115 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2116 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2117 if (!local_stream
[i
]->data_read
) {
2118 consumer_del_stream(local_stream
[i
], data_ht
);
2122 local_stream
[i
]->data_read
= 0;
2126 DBG("polling thread exiting");
2127 if (pollfd
!= NULL
) {
2131 if (local_stream
!= NULL
) {
2133 local_stream
= NULL
;
2137 * Close the write side of the pipe so epoll_wait() in
2138 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2139 * read side of the pipe. If we close them both, epoll_wait strangely does
2140 * not return and could create a endless wait period if the pipe is the
2141 * only tracked fd in the poll set. The thread will take care of closing
2144 close(ctx
->consumer_metadata_pipe
[1]);
2147 destroy_data_stream_ht(data_ht
);
2150 rcu_unregister_thread();
2155 * This thread listens on the consumerd socket and receives the file
2156 * descriptors from the session daemon.
2158 void *consumer_thread_sessiond_poll(void *data
)
2160 int sock
, client_socket
, ret
;
2162 * structure to poll for incoming data on communication socket avoids
2163 * making blocking sockets.
2165 struct pollfd consumer_sockpoll
[2];
2166 struct lttng_consumer_local_data
*ctx
= data
;
2168 rcu_register_thread();
2170 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2171 unlink(ctx
->consumer_command_sock_path
);
2172 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2173 if (client_socket
< 0) {
2174 ERR("Cannot create command socket");
2178 ret
= lttcomm_listen_unix_sock(client_socket
);
2183 DBG("Sending ready command to lttng-sessiond");
2184 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2185 /* return < 0 on error, but == 0 is not fatal */
2187 ERR("Error sending ready command to lttng-sessiond");
2191 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2193 PERROR("fcntl O_NONBLOCK");
2197 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2198 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2199 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2200 consumer_sockpoll
[1].fd
= client_socket
;
2201 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2203 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2206 DBG("Connection on client_socket");
2208 /* Blocking call, waiting for transmission */
2209 sock
= lttcomm_accept_unix_sock(client_socket
);
2214 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2216 PERROR("fcntl O_NONBLOCK");
2220 /* update the polling structure to poll on the established socket */
2221 consumer_sockpoll
[1].fd
= sock
;
2222 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2225 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2228 DBG("Incoming command on sock");
2229 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2230 if (ret
== -ENOENT
) {
2231 DBG("Received STOP command");
2236 * This could simply be a session daemon quitting. Don't output
2239 DBG("Communication interrupted on command socket");
2242 if (consumer_quit
) {
2243 DBG("consumer_thread_receive_fds received quit from signal");
2246 DBG("received fds on sock");
2249 DBG("consumer_thread_receive_fds exiting");
2252 * when all fds have hung up, the polling thread
2258 * 2s of grace period, if no polling events occur during
2259 * this period, the polling thread will exit even if there
2260 * are still open FDs (should not happen, but safety mechanism).
2262 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
2265 * Notify the data poll thread to poll back again and test the
2266 * consumer_quit state to quit gracefully.
2269 struct lttng_consumer_stream
*null_stream
= NULL
;
2271 ret
= write(ctx
->consumer_data_pipe
[1], &null_stream
,
2272 sizeof(null_stream
));
2273 } while (ret
< 0 && errno
== EINTR
);
2275 rcu_unregister_thread();
2279 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2280 struct lttng_consumer_local_data
*ctx
)
2282 switch (consumer_data
.type
) {
2283 case LTTNG_CONSUMER_KERNEL
:
2284 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
2285 case LTTNG_CONSUMER32_UST
:
2286 case LTTNG_CONSUMER64_UST
:
2287 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2289 ERR("Unknown consumer_data type");
2295 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2297 switch (consumer_data
.type
) {
2298 case LTTNG_CONSUMER_KERNEL
:
2299 return lttng_kconsumer_on_recv_stream(stream
);
2300 case LTTNG_CONSUMER32_UST
:
2301 case LTTNG_CONSUMER64_UST
:
2302 return lttng_ustconsumer_on_recv_stream(stream
);
2304 ERR("Unknown consumer_data type");
2311 * Allocate and set consumer data hash tables.
2313 void lttng_consumer_init(void)
2315 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2316 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2317 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2319 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2320 assert(metadata_ht
);
2321 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2326 * Process the ADD_RELAYD command receive by a consumer.
2328 * This will create a relayd socket pair and add it to the relayd hash table.
2329 * The caller MUST acquire a RCU read side lock before calling it.
2331 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
2332 struct lttng_consumer_local_data
*ctx
, int sock
,
2333 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
)
2336 struct consumer_relayd_sock_pair
*relayd
;
2338 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
2340 /* Get relayd reference if exists. */
2341 relayd
= consumer_find_relayd(net_seq_idx
);
2342 if (relayd
== NULL
) {
2343 /* Not found. Allocate one. */
2344 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
2345 if (relayd
== NULL
) {
2346 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
2351 /* Poll on consumer socket. */
2352 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2357 /* Get relayd socket from session daemon */
2358 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2359 if (ret
!= sizeof(fd
)) {
2360 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
2365 /* Copy socket information and received FD */
2366 switch (sock_type
) {
2367 case LTTNG_STREAM_CONTROL
:
2368 /* Copy received lttcomm socket */
2369 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
2370 ret
= lttcomm_create_sock(&relayd
->control_sock
);
2375 /* Close the created socket fd which is useless */
2376 close(relayd
->control_sock
.fd
);
2378 /* Assign new file descriptor */
2379 relayd
->control_sock
.fd
= fd
;
2381 case LTTNG_STREAM_DATA
:
2382 /* Copy received lttcomm socket */
2383 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
2384 ret
= lttcomm_create_sock(&relayd
->data_sock
);
2389 /* Close the created socket fd which is useless */
2390 close(relayd
->data_sock
.fd
);
2392 /* Assign new file descriptor */
2393 relayd
->data_sock
.fd
= fd
;
2396 ERR("Unknown relayd socket type (%d)", sock_type
);
2400 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2401 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
2402 relayd
->net_seq_idx
, fd
);
2405 * Add relayd socket pair to consumer data hashtable. If object already
2406 * exists or on error, the function gracefully returns.