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
;
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
;
70 struct lttng_ht
*data_ht
;
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
);
285 /* Remove node session id from the consumer_data stream ht */
286 iter
.iter
.node
= &stream
->node_session_id
.node
;
287 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
291 assert(consumer_data
.stream_count
> 0);
292 consumer_data
.stream_count
--;
294 if (stream
->out_fd
>= 0) {
295 ret
= close(stream
->out_fd
);
300 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
301 ret
= close(stream
->wait_fd
);
306 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
307 ret
= close(stream
->shm_fd
);
313 /* Check and cleanup relayd */
315 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
316 if (relayd
!= NULL
) {
317 uatomic_dec(&relayd
->refcount
);
318 assert(uatomic_read(&relayd
->refcount
) >= 0);
320 /* Closing streams requires to lock the control socket. */
321 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
322 ret
= relayd_send_close_stream(&relayd
->control_sock
,
323 stream
->relayd_stream_id
,
324 stream
->next_net_seq_num
- 1);
325 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
327 DBG("Unable to close stream on the relayd. Continuing");
329 * Continue here. There is nothing we can do for the relayd.
330 * Chances are that the relayd has closed the socket so we just
331 * continue cleaning up.
335 /* Both conditions are met, we destroy the relayd. */
336 if (uatomic_read(&relayd
->refcount
) == 0 &&
337 uatomic_read(&relayd
->destroy_flag
)) {
338 destroy_relayd(relayd
);
343 uatomic_dec(&stream
->chan
->refcount
);
344 if (!uatomic_read(&stream
->chan
->refcount
)
345 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
346 free_chan
= stream
->chan
;
350 consumer_data
.need_update
= 1;
351 pthread_mutex_unlock(&consumer_data
.lock
);
354 consumer_del_channel(free_chan
);
358 call_rcu(&stream
->node
.head
, consumer_free_stream
);
361 struct lttng_consumer_stream
*consumer_allocate_stream(
362 int channel_key
, int stream_key
,
363 int shm_fd
, int wait_fd
,
364 enum lttng_consumer_stream_state state
,
366 enum lttng_event_output output
,
367 const char *path_name
,
375 struct lttng_consumer_stream
*stream
;
377 stream
= zmalloc(sizeof(*stream
));
378 if (stream
== NULL
) {
379 PERROR("malloc struct lttng_consumer_stream");
380 *alloc_ret
= -ENOMEM
;
385 * Get stream's channel reference. Needed when adding the stream to the
388 stream
->chan
= consumer_find_channel(channel_key
);
390 *alloc_ret
= -ENOENT
;
391 ERR("Unable to find channel for stream %d", stream_key
);
395 stream
->key
= stream_key
;
396 stream
->shm_fd
= shm_fd
;
397 stream
->wait_fd
= wait_fd
;
399 stream
->out_fd_offset
= 0;
400 stream
->state
= state
;
401 stream
->mmap_len
= mmap_len
;
402 stream
->mmap_base
= NULL
;
403 stream
->output
= output
;
406 stream
->net_seq_idx
= net_index
;
407 stream
->metadata_flag
= metadata_flag
;
408 stream
->session_id
= session_id
;
409 strncpy(stream
->path_name
, path_name
, sizeof(stream
->path_name
));
410 stream
->path_name
[sizeof(stream
->path_name
) - 1] = '\0';
411 pthread_mutex_init(&stream
->lock
, NULL
);
414 * Index differently the metadata node because the thread is using an
415 * internal hash table to match streams in the metadata_ht to the epoll set
419 lttng_ht_node_init_ulong(&stream
->node
, stream
->wait_fd
);
421 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
424 /* Init session id node with the stream session id */
425 lttng_ht_node_init_ulong(&stream
->node_session_id
, stream
->session_id
);
428 * The cpu number is needed before using any ustctl_* actions. Ignored for
429 * the kernel so the value does not matter.
431 pthread_mutex_lock(&consumer_data
.lock
);
432 stream
->cpu
= stream
->chan
->cpucount
++;
433 pthread_mutex_unlock(&consumer_data
.lock
);
435 DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu,"
436 " out_fd %d, net_seq_idx %d, session_id %" PRIu64
,
437 stream
->path_name
, stream
->key
, stream
->shm_fd
, stream
->wait_fd
,
438 (unsigned long long) stream
->mmap_len
, stream
->out_fd
,
439 stream
->net_seq_idx
, stream
->session_id
);
449 * Add a stream to the global list protected by a mutex.
451 static int consumer_add_stream(struct lttng_consumer_stream
*stream
,
455 struct consumer_relayd_sock_pair
*relayd
;
460 DBG3("Adding consumer stream %d", stream
->key
);
462 pthread_mutex_lock(&consumer_data
.lock
);
465 /* Steal stream identifier to avoid having streams with the same key */
466 consumer_steal_stream_key(stream
->key
, ht
);
468 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
471 * Add stream to the stream_list_ht of the consumer data. No need to steal
472 * the key since the HT does not use it and we allow to add redundant keys
475 lttng_ht_add_ulong(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
477 /* Check and cleanup relayd */
478 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
479 if (relayd
!= NULL
) {
480 uatomic_inc(&relayd
->refcount
);
483 /* Update channel refcount once added without error(s). */
484 uatomic_inc(&stream
->chan
->refcount
);
487 * When nb_init_streams reaches 0, we don't need to trigger any action in
488 * terms of destroying the associated channel, because the action that
489 * causes the count to become 0 also causes a stream to be added. The
490 * channel deletion will thus be triggered by the following removal of this
493 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
494 uatomic_dec(&stream
->chan
->nb_init_streams
);
497 /* Update consumer data once the node is inserted. */
498 consumer_data
.stream_count
++;
499 consumer_data
.need_update
= 1;
502 pthread_mutex_unlock(&consumer_data
.lock
);
508 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
509 * be acquired before calling this.
511 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
514 struct lttng_ht_node_ulong
*node
;
515 struct lttng_ht_iter iter
;
517 if (relayd
== NULL
) {
522 lttng_ht_lookup(consumer_data
.relayd_ht
,
523 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
524 node
= lttng_ht_iter_get_node_ulong(&iter
);
526 /* Relayd already exist. Ignore the insertion */
529 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
536 * Allocate and return a consumer relayd socket.
538 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
541 struct consumer_relayd_sock_pair
*obj
= NULL
;
543 /* Negative net sequence index is a failure */
544 if (net_seq_idx
< 0) {
548 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
550 PERROR("zmalloc relayd sock");
554 obj
->net_seq_idx
= net_seq_idx
;
556 obj
->destroy_flag
= 0;
557 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
558 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
565 * Find a relayd socket pair in the global consumer data.
567 * Return the object if found else NULL.
568 * RCU read-side lock must be held across this call and while using the
571 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
573 struct lttng_ht_iter iter
;
574 struct lttng_ht_node_ulong
*node
;
575 struct consumer_relayd_sock_pair
*relayd
= NULL
;
577 /* Negative keys are lookup failures */
582 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
584 node
= lttng_ht_iter_get_node_ulong(&iter
);
586 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
594 * Handle stream for relayd transmission if the stream applies for network
595 * streaming where the net sequence index is set.
597 * Return destination file descriptor or negative value on error.
599 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
600 size_t data_size
, unsigned long padding
,
601 struct consumer_relayd_sock_pair
*relayd
)
604 struct lttcomm_relayd_data_hdr data_hdr
;
610 /* Reset data header */
611 memset(&data_hdr
, 0, sizeof(data_hdr
));
613 if (stream
->metadata_flag
) {
614 /* Caller MUST acquire the relayd control socket lock */
615 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
620 /* Metadata are always sent on the control socket. */
621 outfd
= relayd
->control_sock
.fd
;
623 /* Set header with stream information */
624 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
625 data_hdr
.data_size
= htobe32(data_size
);
626 data_hdr
.padding_size
= htobe32(padding
);
627 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
628 /* Other fields are zeroed previously */
630 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
636 /* Set to go on data socket */
637 outfd
= relayd
->data_sock
.fd
;
645 void consumer_free_channel(struct rcu_head
*head
)
647 struct lttng_ht_node_ulong
*node
=
648 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
649 struct lttng_consumer_channel
*channel
=
650 caa_container_of(node
, struct lttng_consumer_channel
, node
);
656 * Remove a channel from the global list protected by a mutex. This
657 * function is also responsible for freeing its data structures.
659 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
662 struct lttng_ht_iter iter
;
664 pthread_mutex_lock(&consumer_data
.lock
);
666 switch (consumer_data
.type
) {
667 case LTTNG_CONSUMER_KERNEL
:
669 case LTTNG_CONSUMER32_UST
:
670 case LTTNG_CONSUMER64_UST
:
671 lttng_ustconsumer_del_channel(channel
);
674 ERR("Unknown consumer_data type");
680 iter
.iter
.node
= &channel
->node
.node
;
681 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
685 if (channel
->mmap_base
!= NULL
) {
686 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
691 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
692 ret
= close(channel
->wait_fd
);
697 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
698 ret
= close(channel
->shm_fd
);
704 call_rcu(&channel
->node
.head
, consumer_free_channel
);
706 pthread_mutex_unlock(&consumer_data
.lock
);
709 struct lttng_consumer_channel
*consumer_allocate_channel(
711 int shm_fd
, int wait_fd
,
713 uint64_t max_sb_size
,
714 unsigned int nb_init_streams
)
716 struct lttng_consumer_channel
*channel
;
719 channel
= zmalloc(sizeof(*channel
));
720 if (channel
== NULL
) {
721 PERROR("malloc struct lttng_consumer_channel");
724 channel
->key
= channel_key
;
725 channel
->shm_fd
= shm_fd
;
726 channel
->wait_fd
= wait_fd
;
727 channel
->mmap_len
= mmap_len
;
728 channel
->max_sb_size
= max_sb_size
;
729 channel
->refcount
= 0;
730 channel
->nb_init_streams
= nb_init_streams
;
731 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
733 switch (consumer_data
.type
) {
734 case LTTNG_CONSUMER_KERNEL
:
735 channel
->mmap_base
= NULL
;
736 channel
->mmap_len
= 0;
738 case LTTNG_CONSUMER32_UST
:
739 case LTTNG_CONSUMER64_UST
:
740 ret
= lttng_ustconsumer_allocate_channel(channel
);
747 ERR("Unknown consumer_data type");
751 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
752 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
753 (unsigned long long) channel
->mmap_len
,
754 (unsigned long long) channel
->max_sb_size
);
760 * Add a channel to the global list protected by a mutex.
762 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
764 struct lttng_ht_node_ulong
*node
;
765 struct lttng_ht_iter iter
;
767 pthread_mutex_lock(&consumer_data
.lock
);
768 /* Steal channel identifier, for UST */
769 consumer_steal_channel_key(channel
->key
);
772 lttng_ht_lookup(consumer_data
.channel_ht
,
773 (void *)((unsigned long) channel
->key
), &iter
);
774 node
= lttng_ht_iter_get_node_ulong(&iter
);
776 /* Channel already exist. Ignore the insertion */
780 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
784 pthread_mutex_unlock(&consumer_data
.lock
);
790 * Allocate the pollfd structure and the local view of the out fds to avoid
791 * doing a lookup in the linked list and concurrency issues when writing is
792 * needed. Called with consumer_data.lock held.
794 * Returns the number of fds in the structures.
796 static int consumer_update_poll_array(
797 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
798 struct lttng_consumer_stream
**local_stream
, struct lttng_ht
*ht
)
801 struct lttng_ht_iter iter
;
802 struct lttng_consumer_stream
*stream
;
804 DBG("Updating poll fd array");
806 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
807 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
810 DBG("Active FD %d", stream
->wait_fd
);
811 (*pollfd
)[i
].fd
= stream
->wait_fd
;
812 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
813 local_stream
[i
] = stream
;
819 * Insert the consumer_data_pipe at the end of the array and don't
820 * increment i so nb_fd is the number of real FD.
822 (*pollfd
)[i
].fd
= ctx
->consumer_data_pipe
[0];
823 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
828 * Poll on the should_quit pipe and the command socket return -1 on error and
829 * should exit, 0 if data is available on the command socket
831 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
836 num_rdy
= poll(consumer_sockpoll
, 2, -1);
839 * Restart interrupted system call.
841 if (errno
== EINTR
) {
844 PERROR("Poll error");
847 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
848 DBG("consumer_should_quit wake up");
858 * Set the error socket.
860 void lttng_consumer_set_error_sock(
861 struct lttng_consumer_local_data
*ctx
, int sock
)
863 ctx
->consumer_error_socket
= sock
;
867 * Set the command socket path.
869 void lttng_consumer_set_command_sock_path(
870 struct lttng_consumer_local_data
*ctx
, char *sock
)
872 ctx
->consumer_command_sock_path
= sock
;
876 * Send return code to the session daemon.
877 * If the socket is not defined, we return 0, it is not a fatal error
879 int lttng_consumer_send_error(
880 struct lttng_consumer_local_data
*ctx
, int cmd
)
882 if (ctx
->consumer_error_socket
> 0) {
883 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
884 sizeof(enum lttcomm_sessiond_command
));
891 * Close all the tracefiles and stream fds, should be called when all instances
894 void lttng_consumer_cleanup(void)
896 struct lttng_ht_iter iter
;
897 struct lttng_ht_node_ulong
*node
;
901 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
903 struct lttng_consumer_channel
*channel
=
904 caa_container_of(node
, struct lttng_consumer_channel
, node
);
905 consumer_del_channel(channel
);
910 lttng_ht_destroy(consumer_data
.channel_ht
);
914 * Called from signal handler.
916 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
921 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
922 } while (ret
< 0 && errno
== EINTR
);
924 PERROR("write consumer quit");
927 DBG("Consumer flag that it should quit");
930 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
933 int outfd
= stream
->out_fd
;
936 * This does a blocking write-and-wait on any page that belongs to the
937 * subbuffer prior to the one we just wrote.
938 * Don't care about error values, as these are just hints and ways to
939 * limit the amount of page cache used.
941 if (orig_offset
< stream
->chan
->max_sb_size
) {
944 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
945 stream
->chan
->max_sb_size
,
946 SYNC_FILE_RANGE_WAIT_BEFORE
947 | SYNC_FILE_RANGE_WRITE
948 | SYNC_FILE_RANGE_WAIT_AFTER
);
950 * Give hints to the kernel about how we access the file:
951 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
954 * We need to call fadvise again after the file grows because the
955 * kernel does not seem to apply fadvise to non-existing parts of the
958 * Call fadvise _after_ having waited for the page writeback to
959 * complete because the dirty page writeback semantic is not well
960 * defined. So it can be expected to lead to lower throughput in
963 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
964 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
968 * Initialise the necessary environnement :
969 * - create a new context
970 * - create the poll_pipe
971 * - create the should_quit pipe (for signal handler)
972 * - create the thread pipe (for splice)
974 * Takes a function pointer as argument, this function is called when data is
975 * available on a buffer. This function is responsible to do the
976 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
977 * buffer configuration and then kernctl_put_next_subbuf at the end.
979 * Returns a pointer to the new context or NULL on error.
981 struct lttng_consumer_local_data
*lttng_consumer_create(
982 enum lttng_consumer_type type
,
983 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
984 struct lttng_consumer_local_data
*ctx
),
985 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
986 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
987 int (*update_stream
)(int stream_key
, uint32_t state
))
990 struct lttng_consumer_local_data
*ctx
;
992 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
993 consumer_data
.type
== type
);
994 consumer_data
.type
= type
;
996 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
998 PERROR("allocating context");
1002 ctx
->consumer_error_socket
= -1;
1003 /* assign the callbacks */
1004 ctx
->on_buffer_ready
= buffer_ready
;
1005 ctx
->on_recv_channel
= recv_channel
;
1006 ctx
->on_recv_stream
= recv_stream
;
1007 ctx
->on_update_stream
= update_stream
;
1009 ret
= pipe(ctx
->consumer_data_pipe
);
1011 PERROR("Error creating poll pipe");
1012 goto error_poll_pipe
;
1015 /* set read end of the pipe to non-blocking */
1016 ret
= fcntl(ctx
->consumer_data_pipe
[0], F_SETFL
, O_NONBLOCK
);
1018 PERROR("fcntl O_NONBLOCK");
1019 goto error_poll_fcntl
;
1022 /* set write end of the pipe to non-blocking */
1023 ret
= fcntl(ctx
->consumer_data_pipe
[1], F_SETFL
, O_NONBLOCK
);
1025 PERROR("fcntl O_NONBLOCK");
1026 goto error_poll_fcntl
;
1029 ret
= pipe(ctx
->consumer_should_quit
);
1031 PERROR("Error creating recv pipe");
1032 goto error_quit_pipe
;
1035 ret
= pipe(ctx
->consumer_thread_pipe
);
1037 PERROR("Error creating thread pipe");
1038 goto error_thread_pipe
;
1041 ret
= utils_create_pipe(ctx
->consumer_metadata_pipe
);
1043 goto error_metadata_pipe
;
1046 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1048 goto error_splice_pipe
;
1054 utils_close_pipe(ctx
->consumer_metadata_pipe
);
1055 error_metadata_pipe
:
1056 utils_close_pipe(ctx
->consumer_thread_pipe
);
1058 for (i
= 0; i
< 2; i
++) {
1061 err
= close(ctx
->consumer_should_quit
[i
]);
1068 for (i
= 0; i
< 2; i
++) {
1071 err
= close(ctx
->consumer_data_pipe
[i
]);
1083 * Close all fds associated with the instance and free the context.
1085 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1089 DBG("Consumer destroying it. Closing everything.");
1091 ret
= close(ctx
->consumer_error_socket
);
1095 ret
= close(ctx
->consumer_thread_pipe
[0]);
1099 ret
= close(ctx
->consumer_thread_pipe
[1]);
1103 ret
= close(ctx
->consumer_data_pipe
[0]);
1107 ret
= close(ctx
->consumer_data_pipe
[1]);
1111 ret
= close(ctx
->consumer_should_quit
[0]);
1115 ret
= close(ctx
->consumer_should_quit
[1]);
1119 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1121 unlink(ctx
->consumer_command_sock_path
);
1126 * Write the metadata stream id on the specified file descriptor.
1128 static int write_relayd_metadata_id(int fd
,
1129 struct lttng_consumer_stream
*stream
,
1130 struct consumer_relayd_sock_pair
*relayd
,
1131 unsigned long padding
)
1134 struct lttcomm_relayd_metadata_payload hdr
;
1136 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1137 hdr
.padding_size
= htobe32(padding
);
1139 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1140 } while (ret
< 0 && errno
== EINTR
);
1142 PERROR("write metadata stream id");
1145 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1146 stream
->relayd_stream_id
, padding
);
1153 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1154 * core function for writing trace buffers to either the local filesystem or
1157 * Careful review MUST be put if any changes occur!
1159 * Returns the number of bytes written
1161 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1162 struct lttng_consumer_local_data
*ctx
,
1163 struct lttng_consumer_stream
*stream
, unsigned long len
,
1164 unsigned long padding
)
1166 unsigned long mmap_offset
;
1167 ssize_t ret
= 0, written
= 0;
1168 off_t orig_offset
= stream
->out_fd_offset
;
1169 /* Default is on the disk */
1170 int outfd
= stream
->out_fd
;
1171 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1173 /* RCU lock for the relayd pointer */
1176 pthread_mutex_lock(&stream
->lock
);
1178 /* Flag that the current stream if set for network streaming. */
1179 if (stream
->net_seq_idx
!= -1) {
1180 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1181 if (relayd
== NULL
) {
1186 /* get the offset inside the fd to mmap */
1187 switch (consumer_data
.type
) {
1188 case LTTNG_CONSUMER_KERNEL
:
1189 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1191 case LTTNG_CONSUMER32_UST
:
1192 case LTTNG_CONSUMER64_UST
:
1193 ret
= lttng_ustctl_get_mmap_read_offset(stream
->chan
->handle
,
1194 stream
->buf
, &mmap_offset
);
1197 ERR("Unknown consumer_data type");
1202 PERROR("tracer ctl get_mmap_read_offset");
1207 /* Handle stream on the relayd if the output is on the network */
1209 unsigned long netlen
= len
;
1212 * Lock the control socket for the complete duration of the function
1213 * since from this point on we will use the socket.
1215 if (stream
->metadata_flag
) {
1216 /* Metadata requires the control socket. */
1217 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1218 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1221 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1223 /* Use the returned socket. */
1226 /* Write metadata stream id before payload */
1227 if (stream
->metadata_flag
) {
1228 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1235 /* Else, use the default set before which is the filesystem. */
1237 /* No streaming, we have to set the len with the full padding */
1243 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
1244 } while (ret
< 0 && errno
== EINTR
);
1245 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1247 PERROR("Error in file write");
1252 } else if (ret
> len
) {
1253 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1261 /* This call is useless on a socket so better save a syscall. */
1263 /* This won't block, but will start writeout asynchronously */
1264 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1265 SYNC_FILE_RANGE_WRITE
);
1266 stream
->out_fd_offset
+= ret
;
1270 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1273 /* Unlock only if ctrl socket used */
1274 if (relayd
&& stream
->metadata_flag
) {
1275 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1277 pthread_mutex_unlock(&stream
->lock
);
1284 * Splice the data from the ring buffer to the tracefile.
1286 * Returns the number of bytes spliced.
1288 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1289 struct lttng_consumer_local_data
*ctx
,
1290 struct lttng_consumer_stream
*stream
, unsigned long len
,
1291 unsigned long padding
)
1293 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1295 off_t orig_offset
= stream
->out_fd_offset
;
1296 int fd
= stream
->wait_fd
;
1297 /* Default is on the disk */
1298 int outfd
= stream
->out_fd
;
1299 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1302 switch (consumer_data
.type
) {
1303 case LTTNG_CONSUMER_KERNEL
:
1305 case LTTNG_CONSUMER32_UST
:
1306 case LTTNG_CONSUMER64_UST
:
1307 /* Not supported for user space tracing */
1310 ERR("Unknown consumer_data type");
1314 /* RCU lock for the relayd pointer */
1317 pthread_mutex_lock(&stream
->lock
);
1319 /* Flag that the current stream if set for network streaming. */
1320 if (stream
->net_seq_idx
!= -1) {
1321 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1322 if (relayd
== NULL
) {
1328 * Choose right pipe for splice. Metadata and trace data are handled by
1329 * different threads hence the use of two pipes in order not to race or
1330 * corrupt the written data.
1332 if (stream
->metadata_flag
) {
1333 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1335 splice_pipe
= ctx
->consumer_thread_pipe
;
1338 /* Write metadata stream id before payload */
1340 int total_len
= len
;
1342 if (stream
->metadata_flag
) {
1344 * Lock the control socket for the complete duration of the function
1345 * since from this point on we will use the socket.
1347 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1349 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1356 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1359 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1361 /* Use the returned socket. */
1364 ERR("Remote relayd disconnected. Stopping");
1368 /* No streaming, we have to set the len with the full padding */
1373 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1374 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1375 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1376 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1377 DBG("splice chan to pipe, ret %zd", ret_splice
);
1378 if (ret_splice
< 0) {
1379 PERROR("Error in relay splice");
1381 written
= ret_splice
;
1387 /* Handle stream on the relayd if the output is on the network */
1389 if (stream
->metadata_flag
) {
1390 size_t metadata_payload_size
=
1391 sizeof(struct lttcomm_relayd_metadata_payload
);
1393 /* Update counter to fit the spliced data */
1394 ret_splice
+= metadata_payload_size
;
1395 len
+= metadata_payload_size
;
1397 * We do this so the return value can match the len passed as
1398 * argument to this function.
1400 written
-= metadata_payload_size
;
1404 /* Splice data out */
1405 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1406 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1407 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1408 if (ret_splice
< 0) {
1409 PERROR("Error in file splice");
1411 written
= ret_splice
;
1415 } else if (ret_splice
> len
) {
1417 PERROR("Wrote more data than requested %zd (len: %lu)",
1419 written
+= ret_splice
;
1425 /* This call is useless on a socket so better save a syscall. */
1427 /* This won't block, but will start writeout asynchronously */
1428 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1429 SYNC_FILE_RANGE_WRITE
);
1430 stream
->out_fd_offset
+= ret_splice
;
1432 written
+= ret_splice
;
1434 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1441 /* send the appropriate error description to sessiond */
1444 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EBADF
);
1447 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1450 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1453 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1458 if (relayd
&& stream
->metadata_flag
) {
1459 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1461 pthread_mutex_unlock(&stream
->lock
);
1468 * Take a snapshot for a specific fd
1470 * Returns 0 on success, < 0 on error
1472 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1473 struct lttng_consumer_stream
*stream
)
1475 switch (consumer_data
.type
) {
1476 case LTTNG_CONSUMER_KERNEL
:
1477 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1478 case LTTNG_CONSUMER32_UST
:
1479 case LTTNG_CONSUMER64_UST
:
1480 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1482 ERR("Unknown consumer_data type");
1490 * Get the produced position
1492 * Returns 0 on success, < 0 on error
1494 int lttng_consumer_get_produced_snapshot(
1495 struct lttng_consumer_local_data
*ctx
,
1496 struct lttng_consumer_stream
*stream
,
1499 switch (consumer_data
.type
) {
1500 case LTTNG_CONSUMER_KERNEL
:
1501 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1502 case LTTNG_CONSUMER32_UST
:
1503 case LTTNG_CONSUMER64_UST
:
1504 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1506 ERR("Unknown consumer_data type");
1512 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1513 int sock
, struct pollfd
*consumer_sockpoll
)
1515 switch (consumer_data
.type
) {
1516 case LTTNG_CONSUMER_KERNEL
:
1517 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1518 case LTTNG_CONSUMER32_UST
:
1519 case LTTNG_CONSUMER64_UST
:
1520 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1522 ERR("Unknown consumer_data type");
1529 * Iterate over all streams of the hashtable and free them properly.
1531 * WARNING: *MUST* be used with data stream only.
1533 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1536 struct lttng_ht_iter iter
;
1537 struct lttng_consumer_stream
*stream
;
1544 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1545 ret
= lttng_ht_del(ht
, &iter
);
1548 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1552 lttng_ht_destroy(ht
);
1556 * Iterate over all streams of the hashtable and free them properly.
1558 * XXX: Should not be only for metadata stream or else use an other name.
1560 static void destroy_stream_ht(struct lttng_ht
*ht
)
1563 struct lttng_ht_iter iter
;
1564 struct lttng_consumer_stream
*stream
;
1571 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1572 ret
= lttng_ht_del(ht
, &iter
);
1575 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1579 lttng_ht_destroy(ht
);
1583 * Clean up a metadata stream and free its memory.
1585 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1586 struct lttng_ht
*ht
)
1589 struct lttng_ht_iter iter
;
1590 struct lttng_consumer_channel
*free_chan
= NULL
;
1591 struct consumer_relayd_sock_pair
*relayd
;
1595 * This call should NEVER receive regular stream. It must always be
1596 * metadata stream and this is crucial for data structure synchronization.
1598 assert(stream
->metadata_flag
);
1600 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1603 /* Means the stream was allocated but not successfully added */
1607 pthread_mutex_lock(&consumer_data
.lock
);
1608 switch (consumer_data
.type
) {
1609 case LTTNG_CONSUMER_KERNEL
:
1610 if (stream
->mmap_base
!= NULL
) {
1611 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1613 PERROR("munmap metadata stream");
1617 case LTTNG_CONSUMER32_UST
:
1618 case LTTNG_CONSUMER64_UST
:
1619 lttng_ustconsumer_del_stream(stream
);
1622 ERR("Unknown consumer_data type");
1628 iter
.iter
.node
= &stream
->node
.node
;
1629 ret
= lttng_ht_del(ht
, &iter
);
1632 /* Remove node session id from the consumer_data stream ht */
1633 iter
.iter
.node
= &stream
->node_session_id
.node
;
1634 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1638 if (stream
->out_fd
>= 0) {
1639 ret
= close(stream
->out_fd
);
1645 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
1646 ret
= close(stream
->wait_fd
);
1652 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
1653 ret
= close(stream
->shm_fd
);
1659 /* Check and cleanup relayd */
1661 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1662 if (relayd
!= NULL
) {
1663 uatomic_dec(&relayd
->refcount
);
1664 assert(uatomic_read(&relayd
->refcount
) >= 0);
1666 /* Closing streams requires to lock the control socket. */
1667 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1668 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1669 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1670 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1672 DBG("Unable to close stream on the relayd. Continuing");
1674 * Continue here. There is nothing we can do for the relayd.
1675 * Chances are that the relayd has closed the socket so we just
1676 * continue cleaning up.
1680 /* Both conditions are met, we destroy the relayd. */
1681 if (uatomic_read(&relayd
->refcount
) == 0 &&
1682 uatomic_read(&relayd
->destroy_flag
)) {
1683 destroy_relayd(relayd
);
1688 /* Atomically decrement channel refcount since other threads can use it. */
1689 uatomic_dec(&stream
->chan
->refcount
);
1690 if (!uatomic_read(&stream
->chan
->refcount
)
1691 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
1692 /* Go for channel deletion! */
1693 free_chan
= stream
->chan
;
1697 pthread_mutex_unlock(&consumer_data
.lock
);
1700 consumer_del_channel(free_chan
);
1704 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1708 * Action done with the metadata stream when adding it to the consumer internal
1709 * data structures to handle it.
1711 static int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
,
1712 struct lttng_ht
*ht
)
1715 struct consumer_relayd_sock_pair
*relayd
;
1720 DBG3("Adding metadata stream %d to hash table", stream
->wait_fd
);
1722 pthread_mutex_lock(&consumer_data
.lock
);
1725 * From here, refcounts are updated so be _careful_ when returning an error
1730 /* Find relayd and, if one is found, increment refcount. */
1731 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1732 if (relayd
!= NULL
) {
1733 uatomic_inc(&relayd
->refcount
);
1736 /* Update channel refcount once added without error(s). */
1737 uatomic_inc(&stream
->chan
->refcount
);
1740 * When nb_init_streams reaches 0, we don't need to trigger any action in
1741 * terms of destroying the associated channel, because the action that
1742 * causes the count to become 0 also causes a stream to be added. The
1743 * channel deletion will thus be triggered by the following removal of this
1746 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
1747 uatomic_dec(&stream
->chan
->nb_init_streams
);
1750 /* Steal stream identifier to avoid having streams with the same key */
1751 consumer_steal_stream_key(stream
->key
, ht
);
1753 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
1756 * Add stream to the stream_list_ht of the consumer data. No need to steal
1757 * the key since the HT does not use it and we allow to add redundant keys
1760 lttng_ht_add_ulong(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
1764 pthread_mutex_unlock(&consumer_data
.lock
);
1769 * Thread polls on metadata file descriptor and write them on disk or on the
1772 void *consumer_thread_metadata_poll(void *data
)
1775 uint32_t revents
, nb_fd
;
1776 struct lttng_consumer_stream
*stream
= NULL
;
1777 struct lttng_ht_iter iter
;
1778 struct lttng_ht_node_ulong
*node
;
1779 struct lttng_poll_event events
;
1780 struct lttng_consumer_local_data
*ctx
= data
;
1783 rcu_register_thread();
1785 DBG("Thread metadata poll started");
1787 /* Size is set to 1 for the consumer_metadata pipe */
1788 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
1790 ERR("Poll set creation failed");
1794 ret
= lttng_poll_add(&events
, ctx
->consumer_metadata_pipe
[0], LPOLLIN
);
1800 DBG("Metadata main loop started");
1803 lttng_poll_reset(&events
);
1805 nb_fd
= LTTNG_POLL_GETNB(&events
);
1807 /* Only the metadata pipe is set */
1808 if (nb_fd
== 0 && consumer_quit
== 1) {
1813 DBG("Metadata poll wait with %d fd(s)", nb_fd
);
1814 ret
= lttng_poll_wait(&events
, -1);
1815 DBG("Metadata event catched in thread");
1817 if (errno
== EINTR
) {
1818 ERR("Poll EINTR catched");
1824 /* From here, the event is a metadata wait fd */
1825 for (i
= 0; i
< nb_fd
; i
++) {
1826 revents
= LTTNG_POLL_GETEV(&events
, i
);
1827 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1829 /* Just don't waste time if no returned events for the fd */
1834 if (pollfd
== ctx
->consumer_metadata_pipe
[0]) {
1835 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
1836 DBG("Metadata thread pipe hung up");
1838 * Remove the pipe from the poll set and continue the loop
1839 * since their might be data to consume.
1841 lttng_poll_del(&events
, ctx
->consumer_metadata_pipe
[0]);
1842 close(ctx
->consumer_metadata_pipe
[0]);
1844 } else if (revents
& LPOLLIN
) {
1846 /* Get the stream pointer received */
1847 ret
= read(pollfd
, &stream
, sizeof(stream
));
1848 } while (ret
< 0 && errno
== EINTR
);
1850 ret
< sizeof(struct lttng_consumer_stream
*)) {
1851 PERROR("read metadata stream");
1853 * Let's continue here and hope we can still work
1854 * without stopping the consumer. XXX: Should we?
1859 DBG("Adding metadata stream %d to poll set",
1862 ret
= consumer_add_metadata_stream(stream
, metadata_ht
);
1864 ERR("Unable to add metadata stream");
1865 /* Stream was not setup properly. Continuing. */
1866 consumer_del_metadata_stream(stream
, NULL
);
1870 /* Add metadata stream to the global poll events list */
1871 lttng_poll_add(&events
, stream
->wait_fd
,
1872 LPOLLIN
| LPOLLPRI
);
1875 /* Handle other stream */
1880 lttng_ht_lookup(metadata_ht
, (void *)((unsigned long) pollfd
),
1882 node
= lttng_ht_iter_get_node_ulong(&iter
);
1885 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
1888 /* Check for error event */
1889 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
1890 DBG("Metadata fd %d is hup|err.", pollfd
);
1891 if (!stream
->hangup_flush_done
1892 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1893 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1894 DBG("Attempting to flush and consume the UST buffers");
1895 lttng_ustconsumer_on_stream_hangup(stream
);
1897 /* We just flushed the stream now read it. */
1899 len
= ctx
->on_buffer_ready(stream
, ctx
);
1901 * We don't check the return value here since if we get
1902 * a negative len, it means an error occured thus we
1903 * simply remove it from the poll set and free the
1909 lttng_poll_del(&events
, stream
->wait_fd
);
1911 * This call update the channel states, closes file descriptors
1912 * and securely free the stream.
1914 consumer_del_metadata_stream(stream
, metadata_ht
);
1915 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
1916 /* Get the data out of the metadata file descriptor */
1917 DBG("Metadata available on fd %d", pollfd
);
1918 assert(stream
->wait_fd
== pollfd
);
1920 len
= ctx
->on_buffer_ready(stream
, ctx
);
1921 /* It's ok to have an unavailable sub-buffer */
1922 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
1923 /* Clean up stream from consumer and free it. */
1924 lttng_poll_del(&events
, stream
->wait_fd
);
1925 consumer_del_metadata_stream(stream
, metadata_ht
);
1926 } else if (len
> 0) {
1927 stream
->data_read
= 1;
1931 /* Release RCU lock for the stream looked up */
1938 DBG("Metadata poll thread exiting");
1939 lttng_poll_clean(&events
);
1942 destroy_stream_ht(metadata_ht
);
1945 rcu_unregister_thread();
1950 * This thread polls the fds in the set to consume the data and write
1951 * it to tracefile if necessary.
1953 void *consumer_thread_data_poll(void *data
)
1955 int num_rdy
, num_hup
, high_prio
, ret
, i
;
1956 struct pollfd
*pollfd
= NULL
;
1957 /* local view of the streams */
1958 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
1959 /* local view of consumer_data.fds_count */
1961 struct lttng_consumer_local_data
*ctx
= data
;
1964 rcu_register_thread();
1966 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1967 if (data_ht
== NULL
) {
1971 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
1978 * the fds set has been updated, we need to update our
1979 * local array as well
1981 pthread_mutex_lock(&consumer_data
.lock
);
1982 if (consumer_data
.need_update
) {
1983 if (pollfd
!= NULL
) {
1987 if (local_stream
!= NULL
) {
1989 local_stream
= NULL
;
1992 /* allocate for all fds + 1 for the consumer_data_pipe */
1993 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
1994 if (pollfd
== NULL
) {
1995 PERROR("pollfd malloc");
1996 pthread_mutex_unlock(&consumer_data
.lock
);
2000 /* allocate for all fds + 1 for the consumer_data_pipe */
2001 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2002 sizeof(struct lttng_consumer_stream
));
2003 if (local_stream
== NULL
) {
2004 PERROR("local_stream malloc");
2005 pthread_mutex_unlock(&consumer_data
.lock
);
2008 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
,
2011 ERR("Error in allocating pollfd or local_outfds");
2012 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2013 pthread_mutex_unlock(&consumer_data
.lock
);
2017 consumer_data
.need_update
= 0;
2019 pthread_mutex_unlock(&consumer_data
.lock
);
2021 /* No FDs and consumer_quit, consumer_cleanup the thread */
2022 if (nb_fd
== 0 && consumer_quit
== 1) {
2025 /* poll on the array of fds */
2027 DBG("polling on %d fd", nb_fd
+ 1);
2028 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
2029 DBG("poll num_rdy : %d", num_rdy
);
2030 if (num_rdy
== -1) {
2032 * Restart interrupted system call.
2034 if (errno
== EINTR
) {
2037 PERROR("Poll error");
2038 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2040 } else if (num_rdy
== 0) {
2041 DBG("Polling thread timed out");
2046 * If the consumer_data_pipe triggered poll go directly to the
2047 * beginning of the loop to update the array. We want to prioritize
2048 * array update over low-priority reads.
2050 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2051 size_t pipe_readlen
;
2053 DBG("consumer_data_pipe wake up");
2054 /* Consume 1 byte of pipe data */
2056 pipe_readlen
= read(ctx
->consumer_data_pipe
[0], &new_stream
,
2057 sizeof(new_stream
));
2058 } while (pipe_readlen
== -1 && errno
== EINTR
);
2061 * If the stream is NULL, just ignore it. It's also possible that
2062 * the sessiond poll thread changed the consumer_quit state and is
2063 * waking us up to test it.
2065 if (new_stream
== NULL
) {
2069 ret
= consumer_add_stream(new_stream
, data_ht
);
2071 ERR("Consumer add stream %d failed. Continuing",
2074 * At this point, if the add_stream fails, it is not in the
2075 * hash table thus passing the NULL value here.
2077 consumer_del_stream(new_stream
, NULL
);
2080 /* Continue to update the local streams and handle prio ones */
2084 /* Take care of high priority channels first. */
2085 for (i
= 0; i
< nb_fd
; i
++) {
2086 if (pollfd
[i
].revents
& POLLPRI
) {
2087 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2089 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2090 /* it's ok to have an unavailable sub-buffer */
2091 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2092 /* Clean the stream and free it. */
2093 consumer_del_stream(local_stream
[i
], data_ht
);
2094 } else if (len
> 0) {
2095 local_stream
[i
]->data_read
= 1;
2101 * If we read high prio channel in this loop, try again
2102 * for more high prio data.
2108 /* Take care of low priority channels. */
2109 for (i
= 0; i
< nb_fd
; i
++) {
2110 if ((pollfd
[i
].revents
& POLLIN
) ||
2111 local_stream
[i
]->hangup_flush_done
) {
2112 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2113 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2114 /* it's ok to have an unavailable sub-buffer */
2115 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2116 /* Clean the stream and free it. */
2117 consumer_del_stream(local_stream
[i
], data_ht
);
2118 } else if (len
> 0) {
2119 local_stream
[i
]->data_read
= 1;
2124 /* Handle hangup and errors */
2125 for (i
= 0; i
< nb_fd
; i
++) {
2126 if (!local_stream
[i
]->hangup_flush_done
2127 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2128 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2129 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2130 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2132 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2133 /* Attempt read again, for the data we just flushed. */
2134 local_stream
[i
]->data_read
= 1;
2137 * If the poll flag is HUP/ERR/NVAL and we have
2138 * read no data in this pass, we can remove the
2139 * stream from its hash table.
2141 if ((pollfd
[i
].revents
& POLLHUP
)) {
2142 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2143 if (!local_stream
[i
]->data_read
) {
2144 consumer_del_stream(local_stream
[i
], data_ht
);
2147 } else if (pollfd
[i
].revents
& POLLERR
) {
2148 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2149 if (!local_stream
[i
]->data_read
) {
2150 consumer_del_stream(local_stream
[i
], data_ht
);
2153 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2154 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2155 if (!local_stream
[i
]->data_read
) {
2156 consumer_del_stream(local_stream
[i
], data_ht
);
2160 local_stream
[i
]->data_read
= 0;
2164 DBG("polling thread exiting");
2165 if (pollfd
!= NULL
) {
2169 if (local_stream
!= NULL
) {
2171 local_stream
= NULL
;
2175 * Close the write side of the pipe so epoll_wait() in
2176 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2177 * read side of the pipe. If we close them both, epoll_wait strangely does
2178 * not return and could create a endless wait period if the pipe is the
2179 * only tracked fd in the poll set. The thread will take care of closing
2182 close(ctx
->consumer_metadata_pipe
[1]);
2185 destroy_data_stream_ht(data_ht
);
2188 rcu_unregister_thread();
2193 * This thread listens on the consumerd socket and receives the file
2194 * descriptors from the session daemon.
2196 void *consumer_thread_sessiond_poll(void *data
)
2198 int sock
, client_socket
, ret
;
2200 * structure to poll for incoming data on communication socket avoids
2201 * making blocking sockets.
2203 struct pollfd consumer_sockpoll
[2];
2204 struct lttng_consumer_local_data
*ctx
= data
;
2206 rcu_register_thread();
2208 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2209 unlink(ctx
->consumer_command_sock_path
);
2210 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2211 if (client_socket
< 0) {
2212 ERR("Cannot create command socket");
2216 ret
= lttcomm_listen_unix_sock(client_socket
);
2221 DBG("Sending ready command to lttng-sessiond");
2222 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2223 /* return < 0 on error, but == 0 is not fatal */
2225 ERR("Error sending ready command to lttng-sessiond");
2229 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2231 PERROR("fcntl O_NONBLOCK");
2235 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2236 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2237 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2238 consumer_sockpoll
[1].fd
= client_socket
;
2239 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2241 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2244 DBG("Connection on client_socket");
2246 /* Blocking call, waiting for transmission */
2247 sock
= lttcomm_accept_unix_sock(client_socket
);
2252 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2254 PERROR("fcntl O_NONBLOCK");
2258 /* update the polling structure to poll on the established socket */
2259 consumer_sockpoll
[1].fd
= sock
;
2260 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2263 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2266 DBG("Incoming command on sock");
2267 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2268 if (ret
== -ENOENT
) {
2269 DBG("Received STOP command");
2274 * This could simply be a session daemon quitting. Don't output
2277 DBG("Communication interrupted on command socket");
2280 if (consumer_quit
) {
2281 DBG("consumer_thread_receive_fds received quit from signal");
2284 DBG("received fds on sock");
2287 DBG("consumer_thread_receive_fds exiting");
2290 * when all fds have hung up, the polling thread
2296 * 2s of grace period, if no polling events occur during
2297 * this period, the polling thread will exit even if there
2298 * are still open FDs (should not happen, but safety mechanism).
2300 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
2303 * Notify the data poll thread to poll back again and test the
2304 * consumer_quit state to quit gracefully.
2307 struct lttng_consumer_stream
*null_stream
= NULL
;
2309 ret
= write(ctx
->consumer_data_pipe
[1], &null_stream
,
2310 sizeof(null_stream
));
2311 } while (ret
< 0 && errno
== EINTR
);
2313 rcu_unregister_thread();
2317 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2318 struct lttng_consumer_local_data
*ctx
)
2320 switch (consumer_data
.type
) {
2321 case LTTNG_CONSUMER_KERNEL
:
2322 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
2323 case LTTNG_CONSUMER32_UST
:
2324 case LTTNG_CONSUMER64_UST
:
2325 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2327 ERR("Unknown consumer_data type");
2333 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2335 switch (consumer_data
.type
) {
2336 case LTTNG_CONSUMER_KERNEL
:
2337 return lttng_kconsumer_on_recv_stream(stream
);
2338 case LTTNG_CONSUMER32_UST
:
2339 case LTTNG_CONSUMER64_UST
:
2340 return lttng_ustconsumer_on_recv_stream(stream
);
2342 ERR("Unknown consumer_data type");
2349 * Allocate and set consumer data hash tables.
2351 void lttng_consumer_init(void)
2353 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2354 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2355 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2357 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2358 assert(metadata_ht
);
2359 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2364 * Process the ADD_RELAYD command receive by a consumer.
2366 * This will create a relayd socket pair and add it to the relayd hash table.
2367 * The caller MUST acquire a RCU read side lock before calling it.
2369 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
2370 struct lttng_consumer_local_data
*ctx
, int sock
,
2371 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
)
2374 struct consumer_relayd_sock_pair
*relayd
;
2376 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
2378 /* Get relayd reference if exists. */
2379 relayd
= consumer_find_relayd(net_seq_idx
);
2380 if (relayd
== NULL
) {
2381 /* Not found. Allocate one. */
2382 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
2383 if (relayd
== NULL
) {
2384 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
2389 /* Poll on consumer socket. */
2390 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2395 /* Get relayd socket from session daemon */
2396 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2397 if (ret
!= sizeof(fd
)) {
2398 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
2403 /* Copy socket information and received FD */
2404 switch (sock_type
) {
2405 case LTTNG_STREAM_CONTROL
:
2406 /* Copy received lttcomm socket */
2407 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
2408 ret
= lttcomm_create_sock(&relayd
->control_sock
);
2413 /* Close the created socket fd which is useless */
2414 close(relayd
->control_sock
.fd
);
2416 /* Assign new file descriptor */
2417 relayd
->control_sock
.fd
= fd
;
2419 case LTTNG_STREAM_DATA
:
2420 /* Copy received lttcomm socket */
2421 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
2422 ret
= lttcomm_create_sock(&relayd
->data_sock
);
2427 /* Close the created socket fd which is useless */
2428 close(relayd
->data_sock
.fd
);
2430 /* Assign new file descriptor */
2431 relayd
->data_sock
.fd
= fd
;
2434 ERR("Unknown relayd socket type (%d)", sock_type
);
2438 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2439 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
2440 relayd
->net_seq_idx
, fd
);
2443 * Add relayd socket pair to consumer data hashtable. If object already
2444 * exists or on error, the function gracefully returns.
2456 * Check if for a given session id there is still data needed to be extract
2459 * Return 1 if data is in fact available to be read or else 0.
2461 int consumer_data_available(uint64_t id
)
2464 struct lttng_ht_iter iter
;
2465 struct lttng_ht
*ht
;
2466 struct lttng_consumer_stream
*stream
;
2467 struct consumer_relayd_sock_pair
*relayd
;
2468 int (*data_available
)(struct lttng_consumer_stream
*);
2470 DBG("Consumer data available command on session id %" PRIu64
, id
);
2473 pthread_mutex_lock(&consumer_data
.lock
);
2475 switch (consumer_data
.type
) {
2476 case LTTNG_CONSUMER_KERNEL
:
2477 data_available
= lttng_kconsumer_data_available
;
2479 case LTTNG_CONSUMER32_UST
:
2480 case LTTNG_CONSUMER64_UST
:
2481 data_available
= lttng_ustconsumer_data_available
;
2484 ERR("Unknown consumer data type");
2488 /* Ease our life a bit */
2489 ht
= consumer_data
.stream_list_ht
;
2491 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2492 ht
->hash_fct((void *)((unsigned long) id
), 0x42UL
),
2493 ht
->match_fct
, (void *)((unsigned long) id
),
2494 &iter
.iter
, stream
, node_session_id
.node
) {
2495 /* Check the stream for data. */
2496 ret
= data_available(stream
);
2498 goto data_not_available
;
2501 if (stream
->net_seq_idx
!= -1) {
2502 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
2505 pthread_mutex_lock(&stream
->lock
);
2506 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
2507 if (stream
->metadata_flag
) {
2508 ret
= relayd_quiescent_control(&relayd
->control_sock
);
2510 ret
= relayd_data_available(&relayd
->control_sock
,
2511 stream
->relayd_stream_id
, stream
->next_net_seq_num
);
2513 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2514 pthread_mutex_unlock(&stream
->lock
);
2516 goto data_not_available
;
2522 * Finding _no_ node in the hash table means that the stream(s) have been
2523 * removed thus data is guaranteed to be available for analysis from the
2524 * trace files. This is *only* true for local consumer and not network
2528 /* Data is available to be read by a viewer. */
2529 pthread_mutex_unlock(&consumer_data
.lock
);
2534 /* Data is still being extracted from buffers. */
2535 pthread_mutex_unlock(&consumer_data
.lock
);