2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2012 - David Goulet <dgoulet@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/socket.h>
28 #include <sys/types.h>
32 #include <common/common.h>
33 #include <common/utils.h>
34 #include <common/compat/poll.h>
35 #include <common/kernel-ctl/kernel-ctl.h>
36 #include <common/sessiond-comm/relayd.h>
37 #include <common/sessiond-comm/sessiond-comm.h>
38 #include <common/kernel-consumer/kernel-consumer.h>
39 #include <common/relayd/relayd.h>
40 #include <common/ust-consumer/ust-consumer.h>
44 struct lttng_consumer_global_data consumer_data
= {
47 .type
= LTTNG_CONSUMER_UNKNOWN
,
50 /* timeout parameter, to control the polling thread grace period. */
51 int consumer_poll_timeout
= -1;
54 * Flag to inform the polling thread to quit when all fd hung up. Updated by
55 * the consumer_thread_receive_fds when it notices that all fds has hung up.
56 * Also updated by the signal handler (consumer_should_exit()). Read by the
59 volatile int consumer_quit
= 0;
62 * Find a stream. The consumer_data.lock must be locked during this
65 static struct lttng_consumer_stream
*consumer_find_stream(int key
)
67 struct lttng_ht_iter iter
;
68 struct lttng_ht_node_ulong
*node
;
69 struct lttng_consumer_stream
*stream
= NULL
;
71 /* Negative keys are lookup failures */
77 lttng_ht_lookup(consumer_data
.stream_ht
, (void *)((unsigned long) key
),
79 node
= lttng_ht_iter_get_node_ulong(&iter
);
81 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
89 static void consumer_steal_stream_key(int key
)
91 struct lttng_consumer_stream
*stream
;
94 stream
= consumer_find_stream(key
);
98 * We don't want the lookup to match, but we still need
99 * to iterate on this stream when iterating over the hash table. Just
100 * change the node key.
102 stream
->node
.key
= -1;
107 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
109 struct lttng_ht_iter iter
;
110 struct lttng_ht_node_ulong
*node
;
111 struct lttng_consumer_channel
*channel
= NULL
;
113 /* Negative keys are lookup failures */
119 lttng_ht_lookup(consumer_data
.channel_ht
, (void *)((unsigned long) key
),
121 node
= lttng_ht_iter_get_node_ulong(&iter
);
123 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
131 static void consumer_steal_channel_key(int key
)
133 struct lttng_consumer_channel
*channel
;
136 channel
= consumer_find_channel(key
);
140 * We don't want the lookup to match, but we still need
141 * to iterate on this channel when iterating over the hash table. Just
142 * change the node key.
144 channel
->node
.key
= -1;
150 void consumer_free_stream(struct rcu_head
*head
)
152 struct lttng_ht_node_ulong
*node
=
153 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
154 struct lttng_consumer_stream
*stream
=
155 caa_container_of(node
, struct lttng_consumer_stream
, node
);
161 * RCU protected relayd socket pair free.
163 static void consumer_rcu_free_relayd(struct rcu_head
*head
)
165 struct lttng_ht_node_ulong
*node
=
166 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
167 struct consumer_relayd_sock_pair
*relayd
=
168 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
174 * Destroy and free relayd socket pair object.
176 * This function MUST be called with the consumer_data lock acquired.
178 static void destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
181 struct lttng_ht_iter iter
;
183 if (relayd
== NULL
) {
187 DBG("Consumer destroy and close relayd socket pair");
189 iter
.iter
.node
= &relayd
->node
.node
;
190 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
192 /* We assume the relayd was already destroyed */
196 /* Close all sockets */
197 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
198 (void) relayd_close(&relayd
->control_sock
);
199 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
200 (void) relayd_close(&relayd
->data_sock
);
202 /* RCU free() call */
203 call_rcu(&relayd
->node
.head
, consumer_rcu_free_relayd
);
207 * Flag a relayd socket pair for destruction. Destroy it if the refcount
210 * RCU read side lock MUST be aquired before calling this function.
212 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
216 /* Set destroy flag for this object */
217 uatomic_set(&relayd
->destroy_flag
, 1);
219 /* Destroy the relayd if refcount is 0 */
220 if (uatomic_read(&relayd
->refcount
) == 0) {
221 destroy_relayd(relayd
);
226 * Remove a stream from the global list protected by a mutex. This
227 * function is also responsible for freeing its data structures.
229 void consumer_del_stream(struct lttng_consumer_stream
*stream
)
232 struct lttng_ht_iter iter
;
233 struct lttng_consumer_channel
*free_chan
= NULL
;
234 struct consumer_relayd_sock_pair
*relayd
;
238 pthread_mutex_lock(&consumer_data
.lock
);
240 switch (consumer_data
.type
) {
241 case LTTNG_CONSUMER_KERNEL
:
242 if (stream
->mmap_base
!= NULL
) {
243 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
249 case LTTNG_CONSUMER32_UST
:
250 case LTTNG_CONSUMER64_UST
:
251 lttng_ustconsumer_del_stream(stream
);
254 ERR("Unknown consumer_data type");
260 iter
.iter
.node
= &stream
->node
.node
;
261 ret
= lttng_ht_del(consumer_data
.stream_ht
, &iter
);
266 if (consumer_data
.stream_count
<= 0) {
269 consumer_data
.stream_count
--;
273 if (stream
->out_fd
>= 0) {
274 ret
= close(stream
->out_fd
);
279 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
280 ret
= close(stream
->wait_fd
);
285 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
286 ret
= close(stream
->shm_fd
);
292 /* Check and cleanup relayd */
294 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
295 if (relayd
!= NULL
) {
296 uatomic_dec(&relayd
->refcount
);
297 assert(uatomic_read(&relayd
->refcount
) >= 0);
299 /* Closing streams requires to lock the control socket. */
300 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
301 ret
= relayd_send_close_stream(&relayd
->control_sock
,
302 stream
->relayd_stream_id
,
303 stream
->next_net_seq_num
- 1);
304 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
306 DBG("Unable to close stream on the relayd. Continuing");
308 * Continue here. There is nothing we can do for the relayd.
309 * Chances are that the relayd has closed the socket so we just
310 * continue cleaning up.
314 /* Both conditions are met, we destroy the relayd. */
315 if (uatomic_read(&relayd
->refcount
) == 0 &&
316 uatomic_read(&relayd
->destroy_flag
)) {
317 destroy_relayd(relayd
);
322 uatomic_dec(&stream
->chan
->refcount
);
323 if (!uatomic_read(&stream
->chan
->refcount
)
324 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
325 free_chan
= stream
->chan
;
328 call_rcu(&stream
->node
.head
, consumer_free_stream
);
330 consumer_data
.need_update
= 1;
331 pthread_mutex_unlock(&consumer_data
.lock
);
334 consumer_del_channel(free_chan
);
338 struct lttng_consumer_stream
*consumer_allocate_stream(
339 int channel_key
, int stream_key
,
340 int shm_fd
, int wait_fd
,
341 enum lttng_consumer_stream_state state
,
343 enum lttng_event_output output
,
344 const char *path_name
,
351 struct lttng_consumer_stream
*stream
;
354 stream
= zmalloc(sizeof(*stream
));
355 if (stream
== NULL
) {
356 perror("malloc struct lttng_consumer_stream");
357 *alloc_ret
= -ENOMEM
;
360 stream
->chan
= consumer_find_channel(channel_key
);
362 *alloc_ret
= -ENOENT
;
365 stream
->chan
->refcount
++;
366 stream
->key
= stream_key
;
367 stream
->shm_fd
= shm_fd
;
368 stream
->wait_fd
= wait_fd
;
370 stream
->out_fd_offset
= 0;
371 stream
->state
= state
;
372 stream
->mmap_len
= mmap_len
;
373 stream
->mmap_base
= NULL
;
374 stream
->output
= output
;
377 stream
->net_seq_idx
= net_index
;
378 stream
->metadata_flag
= metadata_flag
;
379 strncpy(stream
->path_name
, path_name
, sizeof(stream
->path_name
));
380 stream
->path_name
[sizeof(stream
->path_name
) - 1] = '\0';
381 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
382 lttng_ht_node_init_ulong(&stream
->waitfd_node
, stream
->wait_fd
);
384 switch (consumer_data
.type
) {
385 case LTTNG_CONSUMER_KERNEL
:
387 case LTTNG_CONSUMER32_UST
:
388 case LTTNG_CONSUMER64_UST
:
389 stream
->cpu
= stream
->chan
->cpucount
++;
390 ret
= lttng_ustconsumer_allocate_stream(stream
);
392 *alloc_ret
= -EINVAL
;
397 ERR("Unknown consumer_data type");
398 *alloc_ret
= -EINVAL
;
403 * When nb_init_streams reaches 0, we don't need to trigger any action in
404 * terms of destroying the associated channel, because the action that
405 * causes the count to become 0 also causes a stream to be added. The
406 * channel deletion will thus be triggered by the following removal of this
409 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
410 uatomic_dec(&stream
->chan
->nb_init_streams
);
413 DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu,"
414 " out_fd %d, net_seq_idx %d)", stream
->path_name
, stream
->key
,
415 stream
->shm_fd
, stream
->wait_fd
,
416 (unsigned long long) stream
->mmap_len
, stream
->out_fd
,
417 stream
->net_seq_idx
);
426 * Add a stream to the global list protected by a mutex.
428 int consumer_add_stream(struct lttng_consumer_stream
*stream
)
431 struct lttng_ht_node_ulong
*node
;
432 struct lttng_ht_iter iter
;
433 struct consumer_relayd_sock_pair
*relayd
;
435 pthread_mutex_lock(&consumer_data
.lock
);
436 /* Steal stream identifier, for UST */
437 consumer_steal_stream_key(stream
->key
);
440 lttng_ht_lookup(consumer_data
.stream_ht
,
441 (void *)((unsigned long) stream
->key
), &iter
);
442 node
= lttng_ht_iter_get_node_ulong(&iter
);
445 /* Stream already exist. Ignore the insertion */
449 lttng_ht_add_unique_ulong(consumer_data
.stream_ht
, &stream
->node
);
451 /* Check and cleanup relayd */
452 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
453 if (relayd
!= NULL
) {
454 uatomic_inc(&relayd
->refcount
);
458 /* Update consumer data */
459 consumer_data
.stream_count
++;
460 consumer_data
.need_update
= 1;
463 pthread_mutex_unlock(&consumer_data
.lock
);
469 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
470 * be acquired before calling this.
472 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
475 struct lttng_ht_node_ulong
*node
;
476 struct lttng_ht_iter iter
;
478 if (relayd
== NULL
) {
483 lttng_ht_lookup(consumer_data
.relayd_ht
,
484 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
485 node
= lttng_ht_iter_get_node_ulong(&iter
);
487 /* Relayd already exist. Ignore the insertion */
490 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
497 * Allocate and return a consumer relayd socket.
499 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
502 struct consumer_relayd_sock_pair
*obj
= NULL
;
504 /* Negative net sequence index is a failure */
505 if (net_seq_idx
< 0) {
509 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
511 PERROR("zmalloc relayd sock");
515 obj
->net_seq_idx
= net_seq_idx
;
517 obj
->destroy_flag
= 0;
518 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
519 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
526 * Find a relayd socket pair in the global consumer data.
528 * Return the object if found else NULL.
529 * RCU read-side lock must be held across this call and while using the
532 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
534 struct lttng_ht_iter iter
;
535 struct lttng_ht_node_ulong
*node
;
536 struct consumer_relayd_sock_pair
*relayd
= NULL
;
538 /* Negative keys are lookup failures */
543 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
545 node
= lttng_ht_iter_get_node_ulong(&iter
);
547 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
555 * Handle stream for relayd transmission if the stream applies for network
556 * streaming where the net sequence index is set.
558 * Return destination file descriptor or negative value on error.
560 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
561 size_t data_size
, unsigned long padding
,
562 struct consumer_relayd_sock_pair
*relayd
)
565 struct lttcomm_relayd_data_hdr data_hdr
;
571 /* Reset data header */
572 memset(&data_hdr
, 0, sizeof(data_hdr
));
574 if (stream
->metadata_flag
) {
575 /* Caller MUST acquire the relayd control socket lock */
576 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
581 /* Metadata are always sent on the control socket. */
582 outfd
= relayd
->control_sock
.fd
;
584 /* Set header with stream information */
585 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
586 data_hdr
.data_size
= htobe32(data_size
);
587 data_hdr
.padding_size
= htobe32(padding
);
588 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
589 /* Other fields are zeroed previously */
591 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
597 /* Set to go on data socket */
598 outfd
= relayd
->data_sock
.fd
;
606 * Update a stream according to what we just received.
608 void consumer_change_stream_state(int stream_key
,
609 enum lttng_consumer_stream_state state
)
611 struct lttng_consumer_stream
*stream
;
613 pthread_mutex_lock(&consumer_data
.lock
);
614 stream
= consumer_find_stream(stream_key
);
616 stream
->state
= state
;
618 consumer_data
.need_update
= 1;
619 pthread_mutex_unlock(&consumer_data
.lock
);
623 void consumer_free_channel(struct rcu_head
*head
)
625 struct lttng_ht_node_ulong
*node
=
626 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
627 struct lttng_consumer_channel
*channel
=
628 caa_container_of(node
, struct lttng_consumer_channel
, node
);
634 * Remove a channel from the global list protected by a mutex. This
635 * function is also responsible for freeing its data structures.
637 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
640 struct lttng_ht_iter iter
;
642 pthread_mutex_lock(&consumer_data
.lock
);
644 switch (consumer_data
.type
) {
645 case LTTNG_CONSUMER_KERNEL
:
647 case LTTNG_CONSUMER32_UST
:
648 case LTTNG_CONSUMER64_UST
:
649 lttng_ustconsumer_del_channel(channel
);
652 ERR("Unknown consumer_data type");
658 iter
.iter
.node
= &channel
->node
.node
;
659 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
663 if (channel
->mmap_base
!= NULL
) {
664 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
669 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
670 ret
= close(channel
->wait_fd
);
675 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
676 ret
= close(channel
->shm_fd
);
682 call_rcu(&channel
->node
.head
, consumer_free_channel
);
684 pthread_mutex_unlock(&consumer_data
.lock
);
687 struct lttng_consumer_channel
*consumer_allocate_channel(
689 int shm_fd
, int wait_fd
,
691 uint64_t max_sb_size
,
692 unsigned int nb_init_streams
)
694 struct lttng_consumer_channel
*channel
;
697 channel
= zmalloc(sizeof(*channel
));
698 if (channel
== NULL
) {
699 perror("malloc struct lttng_consumer_channel");
702 channel
->key
= channel_key
;
703 channel
->shm_fd
= shm_fd
;
704 channel
->wait_fd
= wait_fd
;
705 channel
->mmap_len
= mmap_len
;
706 channel
->max_sb_size
= max_sb_size
;
707 channel
->refcount
= 0;
708 channel
->nb_init_streams
= nb_init_streams
;
709 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
711 switch (consumer_data
.type
) {
712 case LTTNG_CONSUMER_KERNEL
:
713 channel
->mmap_base
= NULL
;
714 channel
->mmap_len
= 0;
716 case LTTNG_CONSUMER32_UST
:
717 case LTTNG_CONSUMER64_UST
:
718 ret
= lttng_ustconsumer_allocate_channel(channel
);
725 ERR("Unknown consumer_data type");
729 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
730 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
731 (unsigned long long) channel
->mmap_len
,
732 (unsigned long long) channel
->max_sb_size
);
738 * Add a channel to the global list protected by a mutex.
740 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
742 struct lttng_ht_node_ulong
*node
;
743 struct lttng_ht_iter iter
;
745 pthread_mutex_lock(&consumer_data
.lock
);
746 /* Steal channel identifier, for UST */
747 consumer_steal_channel_key(channel
->key
);
750 lttng_ht_lookup(consumer_data
.channel_ht
,
751 (void *)((unsigned long) channel
->key
), &iter
);
752 node
= lttng_ht_iter_get_node_ulong(&iter
);
754 /* Channel already exist. Ignore the insertion */
758 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
762 pthread_mutex_unlock(&consumer_data
.lock
);
768 * Allocate the pollfd structure and the local view of the out fds to avoid
769 * doing a lookup in the linked list and concurrency issues when writing is
770 * needed. Called with consumer_data.lock held.
772 * Returns the number of fds in the structures.
774 int consumer_update_poll_array(
775 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
776 struct lttng_consumer_stream
**local_stream
)
779 struct lttng_ht_iter iter
;
780 struct lttng_consumer_stream
*stream
;
782 DBG("Updating poll fd array");
784 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, stream
,
786 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
789 DBG("Active FD %d", stream
->wait_fd
);
790 (*pollfd
)[i
].fd
= stream
->wait_fd
;
791 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
792 local_stream
[i
] = stream
;
798 * Insert the consumer_poll_pipe at the end of the array and don't
799 * increment i so nb_fd is the number of real FD.
801 (*pollfd
)[i
].fd
= ctx
->consumer_poll_pipe
[0];
802 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
807 * Poll on the should_quit pipe and the command socket return -1 on error and
808 * should exit, 0 if data is available on the command socket
810 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
815 num_rdy
= poll(consumer_sockpoll
, 2, -1);
818 * Restart interrupted system call.
820 if (errno
== EINTR
) {
823 perror("Poll error");
826 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
827 DBG("consumer_should_quit wake up");
837 * Set the error socket.
839 void lttng_consumer_set_error_sock(
840 struct lttng_consumer_local_data
*ctx
, int sock
)
842 ctx
->consumer_error_socket
= sock
;
846 * Set the command socket path.
848 void lttng_consumer_set_command_sock_path(
849 struct lttng_consumer_local_data
*ctx
, char *sock
)
851 ctx
->consumer_command_sock_path
= sock
;
855 * Send return code to the session daemon.
856 * If the socket is not defined, we return 0, it is not a fatal error
858 int lttng_consumer_send_error(
859 struct lttng_consumer_local_data
*ctx
, int cmd
)
861 if (ctx
->consumer_error_socket
> 0) {
862 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
863 sizeof(enum lttcomm_sessiond_command
));
870 * Close all the tracefiles and stream fds, should be called when all instances
873 void lttng_consumer_cleanup(void)
875 struct lttng_ht_iter iter
;
876 struct lttng_ht_node_ulong
*node
;
881 * close all outfd. Called when there are no more threads running (after
882 * joining on the threads), no need to protect list iteration with mutex.
884 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, node
,
886 struct lttng_consumer_stream
*stream
=
887 caa_container_of(node
, struct lttng_consumer_stream
, node
);
888 consumer_del_stream(stream
);
891 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
893 struct lttng_consumer_channel
*channel
=
894 caa_container_of(node
, struct lttng_consumer_channel
, node
);
895 consumer_del_channel(channel
);
900 lttng_ht_destroy(consumer_data
.stream_ht
);
901 lttng_ht_destroy(consumer_data
.channel_ht
);
905 * Called from signal handler.
907 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
912 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
913 } while (ret
< 0 && errno
== EINTR
);
915 perror("write consumer quit");
919 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
922 int outfd
= stream
->out_fd
;
925 * This does a blocking write-and-wait on any page that belongs to the
926 * subbuffer prior to the one we just wrote.
927 * Don't care about error values, as these are just hints and ways to
928 * limit the amount of page cache used.
930 if (orig_offset
< stream
->chan
->max_sb_size
) {
933 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
934 stream
->chan
->max_sb_size
,
935 SYNC_FILE_RANGE_WAIT_BEFORE
936 | SYNC_FILE_RANGE_WRITE
937 | SYNC_FILE_RANGE_WAIT_AFTER
);
939 * Give hints to the kernel about how we access the file:
940 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
943 * We need to call fadvise again after the file grows because the
944 * kernel does not seem to apply fadvise to non-existing parts of the
947 * Call fadvise _after_ having waited for the page writeback to
948 * complete because the dirty page writeback semantic is not well
949 * defined. So it can be expected to lead to lower throughput in
952 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
953 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
957 * Initialise the necessary environnement :
958 * - create a new context
959 * - create the poll_pipe
960 * - create the should_quit pipe (for signal handler)
961 * - create the thread pipe (for splice)
963 * Takes a function pointer as argument, this function is called when data is
964 * available on a buffer. This function is responsible to do the
965 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
966 * buffer configuration and then kernctl_put_next_subbuf at the end.
968 * Returns a pointer to the new context or NULL on error.
970 struct lttng_consumer_local_data
*lttng_consumer_create(
971 enum lttng_consumer_type type
,
972 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
973 struct lttng_consumer_local_data
*ctx
),
974 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
975 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
976 int (*update_stream
)(int stream_key
, uint32_t state
))
979 struct lttng_consumer_local_data
*ctx
;
981 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
982 consumer_data
.type
== type
);
983 consumer_data
.type
= type
;
985 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
987 perror("allocating context");
991 ctx
->consumer_error_socket
= -1;
992 /* assign the callbacks */
993 ctx
->on_buffer_ready
= buffer_ready
;
994 ctx
->on_recv_channel
= recv_channel
;
995 ctx
->on_recv_stream
= recv_stream
;
996 ctx
->on_update_stream
= update_stream
;
998 ret
= pipe(ctx
->consumer_poll_pipe
);
1000 perror("Error creating poll pipe");
1001 goto error_poll_pipe
;
1004 /* set read end of the pipe to non-blocking */
1005 ret
= fcntl(ctx
->consumer_poll_pipe
[0], F_SETFL
, O_NONBLOCK
);
1007 perror("fcntl O_NONBLOCK");
1008 goto error_poll_fcntl
;
1011 /* set write end of the pipe to non-blocking */
1012 ret
= fcntl(ctx
->consumer_poll_pipe
[1], F_SETFL
, O_NONBLOCK
);
1014 perror("fcntl O_NONBLOCK");
1015 goto error_poll_fcntl
;
1018 ret
= pipe(ctx
->consumer_should_quit
);
1020 perror("Error creating recv pipe");
1021 goto error_quit_pipe
;
1024 ret
= pipe(ctx
->consumer_thread_pipe
);
1026 perror("Error creating thread pipe");
1027 goto error_thread_pipe
;
1030 ret
= utils_create_pipe(ctx
->consumer_metadata_pipe
);
1032 goto error_metadata_pipe
;
1035 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1037 goto error_splice_pipe
;
1043 utils_close_pipe(ctx
->consumer_metadata_pipe
);
1044 error_metadata_pipe
:
1045 utils_close_pipe(ctx
->consumer_thread_pipe
);
1047 for (i
= 0; i
< 2; i
++) {
1050 err
= close(ctx
->consumer_should_quit
[i
]);
1057 for (i
= 0; i
< 2; i
++) {
1060 err
= close(ctx
->consumer_poll_pipe
[i
]);
1072 * Close all fds associated with the instance and free the context.
1074 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1078 ret
= close(ctx
->consumer_error_socket
);
1082 ret
= close(ctx
->consumer_thread_pipe
[0]);
1086 ret
= close(ctx
->consumer_thread_pipe
[1]);
1090 ret
= close(ctx
->consumer_poll_pipe
[0]);
1094 ret
= close(ctx
->consumer_poll_pipe
[1]);
1098 ret
= close(ctx
->consumer_should_quit
[0]);
1102 ret
= close(ctx
->consumer_should_quit
[1]);
1106 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1108 unlink(ctx
->consumer_command_sock_path
);
1113 * Write the metadata stream id on the specified file descriptor.
1115 static int write_relayd_metadata_id(int fd
,
1116 struct lttng_consumer_stream
*stream
,
1117 struct consumer_relayd_sock_pair
*relayd
,
1118 unsigned long padding
)
1121 struct lttcomm_relayd_metadata_payload hdr
;
1123 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1124 hdr
.padding_size
= htobe32(padding
);
1126 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1127 } while (ret
< 0 && errno
== EINTR
);
1129 PERROR("write metadata stream id");
1132 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1133 stream
->relayd_stream_id
, padding
);
1140 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1141 * core function for writing trace buffers to either the local filesystem or
1144 * Careful review MUST be put if any changes occur!
1146 * Returns the number of bytes written
1148 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1149 struct lttng_consumer_local_data
*ctx
,
1150 struct lttng_consumer_stream
*stream
, unsigned long len
,
1151 unsigned long padding
)
1153 unsigned long mmap_offset
;
1154 ssize_t ret
= 0, written
= 0;
1155 off_t orig_offset
= stream
->out_fd_offset
;
1156 /* Default is on the disk */
1157 int outfd
= stream
->out_fd
;
1158 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1160 /* RCU lock for the relayd pointer */
1163 /* Flag that the current stream if set for network streaming. */
1164 if (stream
->net_seq_idx
!= -1) {
1165 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1166 if (relayd
== NULL
) {
1171 /* get the offset inside the fd to mmap */
1172 switch (consumer_data
.type
) {
1173 case LTTNG_CONSUMER_KERNEL
:
1174 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1176 case LTTNG_CONSUMER32_UST
:
1177 case LTTNG_CONSUMER64_UST
:
1178 ret
= lttng_ustctl_get_mmap_read_offset(stream
->chan
->handle
,
1179 stream
->buf
, &mmap_offset
);
1182 ERR("Unknown consumer_data type");
1187 PERROR("tracer ctl get_mmap_read_offset");
1192 /* Handle stream on the relayd if the output is on the network */
1194 unsigned long netlen
= len
;
1197 * Lock the control socket for the complete duration of the function
1198 * since from this point on we will use the socket.
1200 if (stream
->metadata_flag
) {
1201 /* Metadata requires the control socket. */
1202 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1203 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1206 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1208 /* Use the returned socket. */
1211 /* Write metadata stream id before payload */
1212 if (stream
->metadata_flag
) {
1213 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1220 /* Else, use the default set before which is the filesystem. */
1222 /* No streaming, we have to set the len with the full padding */
1228 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
1229 } while (ret
< 0 && errno
== EINTR
);
1230 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1232 PERROR("Error in file write");
1237 } else if (ret
> len
) {
1238 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1246 /* This call is useless on a socket so better save a syscall. */
1248 /* This won't block, but will start writeout asynchronously */
1249 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1250 SYNC_FILE_RANGE_WRITE
);
1251 stream
->out_fd_offset
+= ret
;
1255 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1258 /* Unlock only if ctrl socket used */
1259 if (relayd
&& stream
->metadata_flag
) {
1260 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1268 * Splice the data from the ring buffer to the tracefile.
1270 * Returns the number of bytes spliced.
1272 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1273 struct lttng_consumer_local_data
*ctx
,
1274 struct lttng_consumer_stream
*stream
, unsigned long len
,
1275 unsigned long padding
)
1277 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1279 off_t orig_offset
= stream
->out_fd_offset
;
1280 int fd
= stream
->wait_fd
;
1281 /* Default is on the disk */
1282 int outfd
= stream
->out_fd
;
1283 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1286 switch (consumer_data
.type
) {
1287 case LTTNG_CONSUMER_KERNEL
:
1289 case LTTNG_CONSUMER32_UST
:
1290 case LTTNG_CONSUMER64_UST
:
1291 /* Not supported for user space tracing */
1294 ERR("Unknown consumer_data type");
1298 /* RCU lock for the relayd pointer */
1301 /* Flag that the current stream if set for network streaming. */
1302 if (stream
->net_seq_idx
!= -1) {
1303 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1304 if (relayd
== NULL
) {
1310 * Choose right pipe for splice. Metadata and trace data are handled by
1311 * different threads hence the use of two pipes in order not to race or
1312 * corrupt the written data.
1314 if (stream
->metadata_flag
) {
1315 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1317 splice_pipe
= ctx
->consumer_thread_pipe
;
1320 /* Write metadata stream id before payload */
1322 int total_len
= len
;
1324 if (stream
->metadata_flag
) {
1326 * Lock the control socket for the complete duration of the function
1327 * since from this point on we will use the socket.
1329 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1331 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1338 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1341 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1343 /* Use the returned socket. */
1346 ERR("Remote relayd disconnected. Stopping");
1350 /* No streaming, we have to set the len with the full padding */
1355 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1356 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1357 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1358 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1359 DBG("splice chan to pipe, ret %zd", ret_splice
);
1360 if (ret_splice
< 0) {
1361 PERROR("Error in relay splice");
1363 written
= ret_splice
;
1369 /* Handle stream on the relayd if the output is on the network */
1371 if (stream
->metadata_flag
) {
1372 size_t metadata_payload_size
=
1373 sizeof(struct lttcomm_relayd_metadata_payload
);
1375 /* Update counter to fit the spliced data */
1376 ret_splice
+= metadata_payload_size
;
1377 len
+= metadata_payload_size
;
1379 * We do this so the return value can match the len passed as
1380 * argument to this function.
1382 written
-= metadata_payload_size
;
1386 /* Splice data out */
1387 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1388 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1389 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1390 if (ret_splice
< 0) {
1391 PERROR("Error in file splice");
1393 written
= ret_splice
;
1397 } else if (ret_splice
> len
) {
1399 PERROR("Wrote more data than requested %zd (len: %lu)",
1401 written
+= ret_splice
;
1407 /* This call is useless on a socket so better save a syscall. */
1409 /* This won't block, but will start writeout asynchronously */
1410 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1411 SYNC_FILE_RANGE_WRITE
);
1412 stream
->out_fd_offset
+= ret_splice
;
1414 written
+= ret_splice
;
1416 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1423 /* send the appropriate error description to sessiond */
1426 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EBADF
);
1429 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1432 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1435 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1440 if (relayd
&& stream
->metadata_flag
) {
1441 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1449 * Take a snapshot for a specific fd
1451 * Returns 0 on success, < 0 on error
1453 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1454 struct lttng_consumer_stream
*stream
)
1456 switch (consumer_data
.type
) {
1457 case LTTNG_CONSUMER_KERNEL
:
1458 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1459 case LTTNG_CONSUMER32_UST
:
1460 case LTTNG_CONSUMER64_UST
:
1461 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1463 ERR("Unknown consumer_data type");
1471 * Get the produced position
1473 * Returns 0 on success, < 0 on error
1475 int lttng_consumer_get_produced_snapshot(
1476 struct lttng_consumer_local_data
*ctx
,
1477 struct lttng_consumer_stream
*stream
,
1480 switch (consumer_data
.type
) {
1481 case LTTNG_CONSUMER_KERNEL
:
1482 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1483 case LTTNG_CONSUMER32_UST
:
1484 case LTTNG_CONSUMER64_UST
:
1485 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1487 ERR("Unknown consumer_data type");
1493 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1494 int sock
, struct pollfd
*consumer_sockpoll
)
1496 switch (consumer_data
.type
) {
1497 case LTTNG_CONSUMER_KERNEL
:
1498 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1499 case LTTNG_CONSUMER32_UST
:
1500 case LTTNG_CONSUMER64_UST
:
1501 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1503 ERR("Unknown consumer_data type");
1510 * Iterate over all stream element of the hashtable and free them. This is race
1511 * free since the hashtable received MUST be in a race free synchronization
1512 * state. It's the caller responsability to make sure of that.
1514 static void destroy_stream_ht(struct lttng_ht
*ht
)
1517 struct lttng_ht_iter iter
;
1518 struct lttng_consumer_stream
*stream
;
1525 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1526 ret
= lttng_ht_del(ht
, &iter
);
1533 lttng_ht_destroy(ht
);
1537 * Clean up a metadata stream and free its memory.
1539 static void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
)
1542 struct consumer_relayd_sock_pair
*relayd
;
1546 * This call should NEVER receive regular stream. It must always be
1547 * metadata stream and this is crucial for data structure synchronization.
1549 assert(stream
->metadata_flag
);
1551 pthread_mutex_lock(&consumer_data
.lock
);
1552 switch (consumer_data
.type
) {
1553 case LTTNG_CONSUMER_KERNEL
:
1554 if (stream
->mmap_base
!= NULL
) {
1555 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1557 PERROR("munmap metadata stream");
1561 case LTTNG_CONSUMER32_UST
:
1562 case LTTNG_CONSUMER64_UST
:
1563 lttng_ustconsumer_del_stream(stream
);
1566 ERR("Unknown consumer_data type");
1569 pthread_mutex_unlock(&consumer_data
.lock
);
1571 if (stream
->out_fd
>= 0) {
1572 ret
= close(stream
->out_fd
);
1578 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
1579 ret
= close(stream
->wait_fd
);
1585 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
1586 ret
= close(stream
->shm_fd
);
1592 /* Check and cleanup relayd */
1594 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1595 if (relayd
!= NULL
) {
1596 uatomic_dec(&relayd
->refcount
);
1597 assert(uatomic_read(&relayd
->refcount
) >= 0);
1599 /* Closing streams requires to lock the control socket. */
1600 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1601 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1602 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1603 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1605 DBG("Unable to close stream on the relayd. Continuing");
1607 * Continue here. There is nothing we can do for the relayd.
1608 * Chances are that the relayd has closed the socket so we just
1609 * continue cleaning up.
1613 /* Both conditions are met, we destroy the relayd. */
1614 if (uatomic_read(&relayd
->refcount
) == 0 &&
1615 uatomic_read(&relayd
->destroy_flag
)) {
1616 destroy_relayd(relayd
);
1621 /* Atomically decrement channel refcount since other threads can use it. */
1622 uatomic_dec(&stream
->chan
->refcount
);
1623 if (!uatomic_read(&stream
->chan
->refcount
)
1624 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
1625 /* Go for channel deletion! */
1626 consumer_del_channel(stream
->chan
);
1633 * Action done with the metadata stream when adding it to the consumer internal
1634 * data structures to handle it.
1636 static void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
1638 struct consumer_relayd_sock_pair
*relayd
;
1640 /* Find relayd and, if one is found, increment refcount. */
1642 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1643 if (relayd
!= NULL
) {
1644 uatomic_inc(&relayd
->refcount
);
1650 * Thread polls on metadata file descriptor and write them on disk or on the
1653 void *lttng_consumer_thread_poll_metadata(void *data
)
1656 uint32_t revents
, nb_fd
;
1657 struct lttng_consumer_stream
*stream
;
1658 struct lttng_ht_iter iter
;
1659 struct lttng_ht_node_ulong
*node
;
1660 struct lttng_ht
*metadata_ht
= NULL
;
1661 struct lttng_poll_event events
;
1662 struct lttng_consumer_local_data
*ctx
= data
;
1665 rcu_register_thread();
1667 DBG("Thread metadata poll started");
1669 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1670 if (metadata_ht
== NULL
) {
1674 /* Size is set to 1 for the consumer_metadata pipe */
1675 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
1677 ERR("Poll set creation failed");
1681 ret
= lttng_poll_add(&events
, ctx
->consumer_metadata_pipe
[0], LPOLLIN
);
1687 DBG("Metadata main loop started");
1690 lttng_poll_reset(&events
);
1692 nb_fd
= LTTNG_POLL_GETNB(&events
);
1694 /* Only the metadata pipe is set */
1695 if (nb_fd
== 0 && consumer_quit
== 1) {
1700 DBG("Metadata poll wait with %d fd(s)", nb_fd
);
1701 ret
= lttng_poll_wait(&events
, -1);
1702 DBG("Metadata event catched in thread");
1704 if (errno
== EINTR
) {
1710 for (i
= 0; i
< nb_fd
; i
++) {
1711 revents
= LTTNG_POLL_GETEV(&events
, i
);
1712 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1714 /* Check the metadata pipe for incoming metadata. */
1715 if (pollfd
== ctx
->consumer_metadata_pipe
[0]) {
1716 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
1717 DBG("Metadata thread pipe hung up");
1719 * Remove the pipe from the poll set and continue the loop
1720 * since their might be data to consume.
1722 lttng_poll_del(&events
, ctx
->consumer_metadata_pipe
[0]);
1723 close(ctx
->consumer_metadata_pipe
[0]);
1725 } else if (revents
& LPOLLIN
) {
1727 /* Get the stream pointer received */
1728 ret
= read(pollfd
, &stream
, sizeof(stream
));
1729 } while (ret
< 0 && errno
== EINTR
);
1731 ret
< sizeof(struct lttng_consumer_stream
*)) {
1732 PERROR("read metadata stream");
1734 * Let's continue here and hope we can still work
1735 * without stopping the consumer. XXX: Should we?
1740 DBG("Adding metadata stream %d to poll set",
1744 /* The node should be init at this point */
1745 lttng_ht_add_unique_ulong(metadata_ht
,
1746 &stream
->waitfd_node
);
1749 /* Add metadata stream to the global poll events list */
1750 lttng_poll_add(&events
, stream
->wait_fd
,
1751 LPOLLIN
| LPOLLPRI
);
1753 consumer_add_metadata_stream(stream
);
1756 /* Metadata pipe handled. Continue handling the others */
1760 /* From here, the event is a metadata wait fd */
1763 lttng_ht_lookup(metadata_ht
, (void *)((unsigned long) pollfd
),
1765 node
= lttng_ht_iter_get_node_ulong(&iter
);
1767 /* FD not found, continue loop */
1772 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
1775 /* Get the data out of the metadata file descriptor */
1776 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
1777 DBG("Metadata available on fd %d", pollfd
);
1778 assert(stream
->wait_fd
== pollfd
);
1780 len
= ctx
->on_buffer_ready(stream
, ctx
);
1781 /* It's ok to have an unavailable sub-buffer */
1782 if (len
< 0 && len
!= -EAGAIN
) {
1785 } else if (len
> 0) {
1786 stream
->data_read
= 1;
1791 * Remove the stream from the hash table since there is no data
1792 * left on the fd because we previously did a read on the buffer.
1794 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
1795 DBG("Metadata fd %d is hup|err|nval.", pollfd
);
1796 if (!stream
->hangup_flush_done
1797 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1798 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1799 DBG("Attempting to flush and consume the UST buffers");
1800 lttng_ustconsumer_on_stream_hangup(stream
);
1802 /* We just flushed the stream now read it. */
1803 len
= ctx
->on_buffer_ready(stream
, ctx
);
1804 /* It's ok to have an unavailable sub-buffer */
1805 if (len
< 0 && len
!= -EAGAIN
) {
1811 /* Removing it from hash table, poll set and free memory */
1812 lttng_ht_del(metadata_ht
, &iter
);
1814 lttng_poll_del(&events
, stream
->wait_fd
);
1815 consumer_del_metadata_stream(stream
);
1823 DBG("Metadata poll thread exiting");
1824 lttng_poll_clean(&events
);
1827 destroy_stream_ht(metadata_ht
);
1830 rcu_unregister_thread();
1835 * This thread polls the fds in the set to consume the data and write
1836 * it to tracefile if necessary.
1838 void *lttng_consumer_thread_poll_fds(void *data
)
1840 int num_rdy
, num_hup
, high_prio
, ret
, i
;
1841 struct pollfd
*pollfd
= NULL
;
1842 /* local view of the streams */
1843 struct lttng_consumer_stream
**local_stream
= NULL
;
1844 /* local view of consumer_data.fds_count */
1846 struct lttng_consumer_local_data
*ctx
= data
;
1848 pthread_t metadata_thread
;
1851 rcu_register_thread();
1853 /* Start metadata polling thread */
1854 ret
= pthread_create(&metadata_thread
, NULL
,
1855 lttng_consumer_thread_poll_metadata
, (void *) ctx
);
1857 PERROR("pthread_create metadata thread");
1861 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
1868 * the fds set has been updated, we need to update our
1869 * local array as well
1871 pthread_mutex_lock(&consumer_data
.lock
);
1872 if (consumer_data
.need_update
) {
1873 if (pollfd
!= NULL
) {
1877 if (local_stream
!= NULL
) {
1879 local_stream
= NULL
;
1882 /* allocate for all fds + 1 for the consumer_poll_pipe */
1883 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
1884 if (pollfd
== NULL
) {
1885 perror("pollfd malloc");
1886 pthread_mutex_unlock(&consumer_data
.lock
);
1890 /* allocate for all fds + 1 for the consumer_poll_pipe */
1891 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
1892 sizeof(struct lttng_consumer_stream
));
1893 if (local_stream
== NULL
) {
1894 perror("local_stream malloc");
1895 pthread_mutex_unlock(&consumer_data
.lock
);
1898 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
);
1900 ERR("Error in allocating pollfd or local_outfds");
1901 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
1902 pthread_mutex_unlock(&consumer_data
.lock
);
1906 consumer_data
.need_update
= 0;
1908 pthread_mutex_unlock(&consumer_data
.lock
);
1910 /* No FDs and consumer_quit, consumer_cleanup the thread */
1911 if (nb_fd
== 0 && consumer_quit
== 1) {
1914 /* poll on the array of fds */
1916 DBG("polling on %d fd", nb_fd
+ 1);
1917 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
1918 DBG("poll num_rdy : %d", num_rdy
);
1919 if (num_rdy
== -1) {
1921 * Restart interrupted system call.
1923 if (errno
== EINTR
) {
1926 perror("Poll error");
1927 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
1929 } else if (num_rdy
== 0) {
1930 DBG("Polling thread timed out");
1935 * If the consumer_poll_pipe triggered poll go directly to the
1936 * beginning of the loop to update the array. We want to prioritize
1937 * array update over low-priority reads.
1939 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
1940 size_t pipe_readlen
;
1943 DBG("consumer_poll_pipe wake up");
1944 /* Consume 1 byte of pipe data */
1946 pipe_readlen
= read(ctx
->consumer_poll_pipe
[0], &tmp
, 1);
1947 } while (pipe_readlen
== -1 && errno
== EINTR
);
1951 /* Take care of high priority channels first. */
1952 for (i
= 0; i
< nb_fd
; i
++) {
1953 if (pollfd
[i
].revents
& POLLPRI
) {
1954 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
1956 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1957 /* it's ok to have an unavailable sub-buffer */
1958 if (len
< 0 && len
!= -EAGAIN
) {
1960 } else if (len
> 0) {
1961 local_stream
[i
]->data_read
= 1;
1967 * If we read high prio channel in this loop, try again
1968 * for more high prio data.
1974 /* Take care of low priority channels. */
1975 for (i
= 0; i
< nb_fd
; i
++) {
1976 if ((pollfd
[i
].revents
& POLLIN
) ||
1977 local_stream
[i
]->hangup_flush_done
) {
1978 DBG("Normal read on fd %d", pollfd
[i
].fd
);
1979 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1980 /* it's ok to have an unavailable sub-buffer */
1981 if (len
< 0 && len
!= -EAGAIN
) {
1983 } else if (len
> 0) {
1984 local_stream
[i
]->data_read
= 1;
1989 /* Handle hangup and errors */
1990 for (i
= 0; i
< nb_fd
; i
++) {
1991 if (!local_stream
[i
]->hangup_flush_done
1992 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
1993 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1994 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1995 DBG("fd %d is hup|err|nval. Attempting flush and read.",
1997 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
1998 /* Attempt read again, for the data we just flushed. */
1999 local_stream
[i
]->data_read
= 1;
2002 * If the poll flag is HUP/ERR/NVAL and we have
2003 * read no data in this pass, we can remove the
2004 * stream from its hash table.
2006 if ((pollfd
[i
].revents
& POLLHUP
)) {
2007 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2008 if (!local_stream
[i
]->data_read
) {
2009 consumer_del_stream(local_stream
[i
]);
2012 } else if (pollfd
[i
].revents
& POLLERR
) {
2013 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2014 if (!local_stream
[i
]->data_read
) {
2015 consumer_del_stream(local_stream
[i
]);
2018 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2019 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2020 if (!local_stream
[i
]->data_read
) {
2021 consumer_del_stream(local_stream
[i
]);
2025 local_stream
[i
]->data_read
= 0;
2029 DBG("polling thread exiting");
2030 if (pollfd
!= NULL
) {
2034 if (local_stream
!= NULL
) {
2036 local_stream
= NULL
;
2040 * Close the write side of the pipe so epoll_wait() in
2041 * lttng_consumer_thread_poll_metadata can catch it. The thread is
2042 * monitoring the read side of the pipe. If we close them both, epoll_wait
2043 * strangely does not return and could create a endless wait period if the
2044 * pipe is the only tracked fd in the poll set. The thread will take care
2045 * of closing the read side.
2047 close(ctx
->consumer_metadata_pipe
[1]);
2049 ret
= pthread_join(metadata_thread
, &status
);
2051 PERROR("pthread_join metadata thread");
2055 rcu_unregister_thread();
2060 * This thread listens on the consumerd socket and receives the file
2061 * descriptors from the session daemon.
2063 void *lttng_consumer_thread_receive_fds(void *data
)
2065 int sock
, client_socket
, ret
;
2067 * structure to poll for incoming data on communication socket avoids
2068 * making blocking sockets.
2070 struct pollfd consumer_sockpoll
[2];
2071 struct lttng_consumer_local_data
*ctx
= data
;
2073 rcu_register_thread();
2075 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2076 unlink(ctx
->consumer_command_sock_path
);
2077 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2078 if (client_socket
< 0) {
2079 ERR("Cannot create command socket");
2083 ret
= lttcomm_listen_unix_sock(client_socket
);
2088 DBG("Sending ready command to lttng-sessiond");
2089 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2090 /* return < 0 on error, but == 0 is not fatal */
2092 ERR("Error sending ready command to lttng-sessiond");
2096 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2098 perror("fcntl O_NONBLOCK");
2102 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2103 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2104 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2105 consumer_sockpoll
[1].fd
= client_socket
;
2106 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2108 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2111 DBG("Connection on client_socket");
2113 /* Blocking call, waiting for transmission */
2114 sock
= lttcomm_accept_unix_sock(client_socket
);
2119 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2121 perror("fcntl O_NONBLOCK");
2125 /* update the polling structure to poll on the established socket */
2126 consumer_sockpoll
[1].fd
= sock
;
2127 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2130 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2133 DBG("Incoming command on sock");
2134 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2135 if (ret
== -ENOENT
) {
2136 DBG("Received STOP command");
2141 * This could simply be a session daemon quitting. Don't output
2144 DBG("Communication interrupted on command socket");
2147 if (consumer_quit
) {
2148 DBG("consumer_thread_receive_fds received quit from signal");
2151 DBG("received fds on sock");
2154 DBG("consumer_thread_receive_fds exiting");
2157 * when all fds have hung up, the polling thread
2163 * 2s of grace period, if no polling events occur during
2164 * this period, the polling thread will exit even if there
2165 * are still open FDs (should not happen, but safety mechanism).
2167 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
2170 * Wake-up the other end by writing a null byte in the pipe
2171 * (non-blocking). Important note: Because writing into the
2172 * pipe is non-blocking (and therefore we allow dropping wakeup
2173 * data, as long as there is wakeup data present in the pipe
2174 * buffer to wake up the other end), the other end should
2175 * perform the following sequence for waiting:
2176 * 1) empty the pipe (reads).
2177 * 2) perform update operation.
2178 * 3) wait on the pipe (poll).
2181 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
2182 } while (ret
< 0 && errno
== EINTR
);
2183 rcu_unregister_thread();
2187 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2188 struct lttng_consumer_local_data
*ctx
)
2190 switch (consumer_data
.type
) {
2191 case LTTNG_CONSUMER_KERNEL
:
2192 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
2193 case LTTNG_CONSUMER32_UST
:
2194 case LTTNG_CONSUMER64_UST
:
2195 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2197 ERR("Unknown consumer_data type");
2203 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2205 switch (consumer_data
.type
) {
2206 case LTTNG_CONSUMER_KERNEL
:
2207 return lttng_kconsumer_on_recv_stream(stream
);
2208 case LTTNG_CONSUMER32_UST
:
2209 case LTTNG_CONSUMER64_UST
:
2210 return lttng_ustconsumer_on_recv_stream(stream
);
2212 ERR("Unknown consumer_data type");
2219 * Allocate and set consumer data hash tables.
2221 void lttng_consumer_init(void)
2223 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2224 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2225 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2229 * Process the ADD_RELAYD command receive by a consumer.
2231 * This will create a relayd socket pair and add it to the relayd hash table.
2232 * The caller MUST acquire a RCU read side lock before calling it.
2234 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
2235 struct lttng_consumer_local_data
*ctx
, int sock
,
2236 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
)
2239 struct consumer_relayd_sock_pair
*relayd
;
2241 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
2243 /* Get relayd reference if exists. */
2244 relayd
= consumer_find_relayd(net_seq_idx
);
2245 if (relayd
== NULL
) {
2246 /* Not found. Allocate one. */
2247 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
2248 if (relayd
== NULL
) {
2249 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
2254 /* Poll on consumer socket. */
2255 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2260 /* Get relayd socket from session daemon */
2261 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2262 if (ret
!= sizeof(fd
)) {
2263 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
2268 /* Copy socket information and received FD */
2269 switch (sock_type
) {
2270 case LTTNG_STREAM_CONTROL
:
2271 /* Copy received lttcomm socket */
2272 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
2273 ret
= lttcomm_create_sock(&relayd
->control_sock
);
2278 /* Close the created socket fd which is useless */
2279 close(relayd
->control_sock
.fd
);
2281 /* Assign new file descriptor */
2282 relayd
->control_sock
.fd
= fd
;
2284 case LTTNG_STREAM_DATA
:
2285 /* Copy received lttcomm socket */
2286 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
2287 ret
= lttcomm_create_sock(&relayd
->data_sock
);
2292 /* Close the created socket fd which is useless */
2293 close(relayd
->data_sock
.fd
);
2295 /* Assign new file descriptor */
2296 relayd
->data_sock
.fd
= fd
;
2299 ERR("Unknown relayd socket type (%d)", sock_type
);
2303 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2304 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
2305 relayd
->net_seq_idx
, fd
);
2308 * Add relayd socket pair to consumer data hashtable. If object already
2309 * exists or on error, the function gracefully returns.