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 void consumer_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 consumer_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 consumer_destroy_relayd(relayd
);
322 if (!--stream
->chan
->refcount
) {
323 free_chan
= stream
->chan
;
327 call_rcu(&stream
->node
.head
, consumer_free_stream
);
329 consumer_data
.need_update
= 1;
330 pthread_mutex_unlock(&consumer_data
.lock
);
333 consumer_del_channel(free_chan
);
336 struct lttng_consumer_stream
*consumer_allocate_stream(
337 int channel_key
, int stream_key
,
338 int shm_fd
, int wait_fd
,
339 enum lttng_consumer_stream_state state
,
341 enum lttng_event_output output
,
342 const char *path_name
,
348 struct lttng_consumer_stream
*stream
;
351 stream
= zmalloc(sizeof(*stream
));
352 if (stream
== NULL
) {
353 perror("malloc struct lttng_consumer_stream");
356 stream
->chan
= consumer_find_channel(channel_key
);
358 perror("Unable to find channel key");
361 stream
->chan
->refcount
++;
362 stream
->key
= stream_key
;
363 stream
->shm_fd
= shm_fd
;
364 stream
->wait_fd
= wait_fd
;
366 stream
->out_fd_offset
= 0;
367 stream
->state
= state
;
368 stream
->mmap_len
= mmap_len
;
369 stream
->mmap_base
= NULL
;
370 stream
->output
= output
;
373 stream
->net_seq_idx
= net_index
;
374 stream
->metadata_flag
= metadata_flag
;
375 strncpy(stream
->path_name
, path_name
, sizeof(stream
->path_name
));
376 stream
->path_name
[sizeof(stream
->path_name
) - 1] = '\0';
377 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
378 lttng_ht_node_init_ulong(&stream
->waitfd_node
, stream
->wait_fd
);
380 switch (consumer_data
.type
) {
381 case LTTNG_CONSUMER_KERNEL
:
383 case LTTNG_CONSUMER32_UST
:
384 case LTTNG_CONSUMER64_UST
:
385 stream
->cpu
= stream
->chan
->cpucount
++;
386 ret
= lttng_ustconsumer_allocate_stream(stream
);
393 ERR("Unknown consumer_data type");
397 DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d, net_seq_idx %d)",
398 stream
->path_name
, stream
->key
,
401 (unsigned long long) stream
->mmap_len
,
403 stream
->net_seq_idx
);
409 * Add a stream to the global list protected by a mutex.
411 int consumer_add_stream(struct lttng_consumer_stream
*stream
)
414 struct lttng_ht_node_ulong
*node
;
415 struct lttng_ht_iter iter
;
416 struct consumer_relayd_sock_pair
*relayd
;
418 pthread_mutex_lock(&consumer_data
.lock
);
419 /* Steal stream identifier, for UST */
420 consumer_steal_stream_key(stream
->key
);
423 lttng_ht_lookup(consumer_data
.stream_ht
,
424 (void *)((unsigned long) stream
->key
), &iter
);
425 node
= lttng_ht_iter_get_node_ulong(&iter
);
428 /* Stream already exist. Ignore the insertion */
432 lttng_ht_add_unique_ulong(consumer_data
.stream_ht
, &stream
->node
);
434 /* Check and cleanup relayd */
435 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
436 if (relayd
!= NULL
) {
437 uatomic_inc(&relayd
->refcount
);
441 /* Update consumer data */
442 consumer_data
.stream_count
++;
443 consumer_data
.need_update
= 1;
446 pthread_mutex_unlock(&consumer_data
.lock
);
452 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
453 * be acquired before calling this.
456 int consumer_add_relayd(struct consumer_relayd_sock_pair
*relayd
)
459 struct lttng_ht_node_ulong
*node
;
460 struct lttng_ht_iter iter
;
462 if (relayd
== NULL
) {
467 lttng_ht_lookup(consumer_data
.relayd_ht
,
468 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
469 node
= lttng_ht_iter_get_node_ulong(&iter
);
471 /* Relayd already exist. Ignore the insertion */
474 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
481 * Allocate and return a consumer relayd socket.
483 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
486 struct consumer_relayd_sock_pair
*obj
= NULL
;
488 /* Negative net sequence index is a failure */
489 if (net_seq_idx
< 0) {
493 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
495 PERROR("zmalloc relayd sock");
499 obj
->net_seq_idx
= net_seq_idx
;
501 obj
->destroy_flag
= 0;
502 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
503 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
510 * Find a relayd socket pair in the global consumer data.
512 * Return the object if found else NULL.
513 * RCU read-side lock must be held across this call and while using the
516 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
518 struct lttng_ht_iter iter
;
519 struct lttng_ht_node_ulong
*node
;
520 struct consumer_relayd_sock_pair
*relayd
= NULL
;
522 /* Negative keys are lookup failures */
527 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
529 node
= lttng_ht_iter_get_node_ulong(&iter
);
531 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
539 * Handle stream for relayd transmission if the stream applies for network
540 * streaming where the net sequence index is set.
542 * Return destination file descriptor or negative value on error.
544 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
545 size_t data_size
, unsigned long padding
,
546 struct consumer_relayd_sock_pair
*relayd
)
549 struct lttcomm_relayd_data_hdr data_hdr
;
555 /* Reset data header */
556 memset(&data_hdr
, 0, sizeof(data_hdr
));
558 if (stream
->metadata_flag
) {
559 /* Caller MUST acquire the relayd control socket lock */
560 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
565 /* Metadata are always sent on the control socket. */
566 outfd
= relayd
->control_sock
.fd
;
568 /* Set header with stream information */
569 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
570 data_hdr
.data_size
= htobe32(data_size
);
571 data_hdr
.padding_size
= htobe32(padding
);
572 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
573 /* Other fields are zeroed previously */
575 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
581 /* Set to go on data socket */
582 outfd
= relayd
->data_sock
.fd
;
590 * Update a stream according to what we just received.
592 void consumer_change_stream_state(int stream_key
,
593 enum lttng_consumer_stream_state state
)
595 struct lttng_consumer_stream
*stream
;
597 pthread_mutex_lock(&consumer_data
.lock
);
598 stream
= consumer_find_stream(stream_key
);
600 stream
->state
= state
;
602 consumer_data
.need_update
= 1;
603 pthread_mutex_unlock(&consumer_data
.lock
);
607 void consumer_free_channel(struct rcu_head
*head
)
609 struct lttng_ht_node_ulong
*node
=
610 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
611 struct lttng_consumer_channel
*channel
=
612 caa_container_of(node
, struct lttng_consumer_channel
, node
);
618 * Remove a channel from the global list protected by a mutex. This
619 * function is also responsible for freeing its data structures.
621 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
624 struct lttng_ht_iter iter
;
626 pthread_mutex_lock(&consumer_data
.lock
);
628 switch (consumer_data
.type
) {
629 case LTTNG_CONSUMER_KERNEL
:
631 case LTTNG_CONSUMER32_UST
:
632 case LTTNG_CONSUMER64_UST
:
633 lttng_ustconsumer_del_channel(channel
);
636 ERR("Unknown consumer_data type");
642 iter
.iter
.node
= &channel
->node
.node
;
643 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
647 if (channel
->mmap_base
!= NULL
) {
648 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
653 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
654 ret
= close(channel
->wait_fd
);
659 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
660 ret
= close(channel
->shm_fd
);
666 call_rcu(&channel
->node
.head
, consumer_free_channel
);
668 pthread_mutex_unlock(&consumer_data
.lock
);
671 struct lttng_consumer_channel
*consumer_allocate_channel(
673 int shm_fd
, int wait_fd
,
675 uint64_t max_sb_size
)
677 struct lttng_consumer_channel
*channel
;
680 channel
= zmalloc(sizeof(*channel
));
681 if (channel
== NULL
) {
682 perror("malloc struct lttng_consumer_channel");
685 channel
->key
= channel_key
;
686 channel
->shm_fd
= shm_fd
;
687 channel
->wait_fd
= wait_fd
;
688 channel
->mmap_len
= mmap_len
;
689 channel
->max_sb_size
= max_sb_size
;
690 channel
->refcount
= 0;
691 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
693 switch (consumer_data
.type
) {
694 case LTTNG_CONSUMER_KERNEL
:
695 channel
->mmap_base
= NULL
;
696 channel
->mmap_len
= 0;
698 case LTTNG_CONSUMER32_UST
:
699 case LTTNG_CONSUMER64_UST
:
700 ret
= lttng_ustconsumer_allocate_channel(channel
);
707 ERR("Unknown consumer_data type");
711 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
712 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
713 (unsigned long long) channel
->mmap_len
,
714 (unsigned long long) channel
->max_sb_size
);
720 * Add a channel to the global list protected by a mutex.
722 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
724 struct lttng_ht_node_ulong
*node
;
725 struct lttng_ht_iter iter
;
727 pthread_mutex_lock(&consumer_data
.lock
);
728 /* Steal channel identifier, for UST */
729 consumer_steal_channel_key(channel
->key
);
732 lttng_ht_lookup(consumer_data
.channel_ht
,
733 (void *)((unsigned long) channel
->key
), &iter
);
734 node
= lttng_ht_iter_get_node_ulong(&iter
);
736 /* Channel already exist. Ignore the insertion */
740 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
744 pthread_mutex_unlock(&consumer_data
.lock
);
750 * Allocate the pollfd structure and the local view of the out fds to avoid
751 * doing a lookup in the linked list and concurrency issues when writing is
752 * needed. Called with consumer_data.lock held.
754 * Returns the number of fds in the structures.
756 int consumer_update_poll_array(
757 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
758 struct lttng_consumer_stream
**local_stream
)
761 struct lttng_ht_iter iter
;
762 struct lttng_consumer_stream
*stream
;
764 DBG("Updating poll fd array");
766 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, stream
,
768 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
771 DBG("Active FD %d", stream
->wait_fd
);
772 (*pollfd
)[i
].fd
= stream
->wait_fd
;
773 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
774 local_stream
[i
] = stream
;
780 * Insert the consumer_poll_pipe at the end of the array and don't
781 * increment i so nb_fd is the number of real FD.
783 (*pollfd
)[i
].fd
= ctx
->consumer_poll_pipe
[0];
784 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
789 * Poll on the should_quit pipe and the command socket return -1 on error and
790 * should exit, 0 if data is available on the command socket
792 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
797 num_rdy
= poll(consumer_sockpoll
, 2, -1);
800 * Restart interrupted system call.
802 if (errno
== EINTR
) {
805 perror("Poll error");
808 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
809 DBG("consumer_should_quit wake up");
819 * Set the error socket.
821 void lttng_consumer_set_error_sock(
822 struct lttng_consumer_local_data
*ctx
, int sock
)
824 ctx
->consumer_error_socket
= sock
;
828 * Set the command socket path.
830 void lttng_consumer_set_command_sock_path(
831 struct lttng_consumer_local_data
*ctx
, char *sock
)
833 ctx
->consumer_command_sock_path
= sock
;
837 * Send return code to the session daemon.
838 * If the socket is not defined, we return 0, it is not a fatal error
840 int lttng_consumer_send_error(
841 struct lttng_consumer_local_data
*ctx
, int cmd
)
843 if (ctx
->consumer_error_socket
> 0) {
844 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
845 sizeof(enum lttcomm_sessiond_command
));
852 * Close all the tracefiles and stream fds, should be called when all instances
855 void lttng_consumer_cleanup(void)
857 struct lttng_ht_iter iter
;
858 struct lttng_ht_node_ulong
*node
;
863 * close all outfd. Called when there are no more threads running (after
864 * joining on the threads), no need to protect list iteration with mutex.
866 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, node
,
868 struct lttng_consumer_stream
*stream
=
869 caa_container_of(node
, struct lttng_consumer_stream
, node
);
870 consumer_del_stream(stream
);
873 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
875 struct lttng_consumer_channel
*channel
=
876 caa_container_of(node
, struct lttng_consumer_channel
, node
);
877 consumer_del_channel(channel
);
882 lttng_ht_destroy(consumer_data
.stream_ht
);
883 lttng_ht_destroy(consumer_data
.channel_ht
);
887 * Called from signal handler.
889 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
894 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
895 } while (ret
< 0 && errno
== EINTR
);
897 perror("write consumer quit");
901 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
904 int outfd
= stream
->out_fd
;
907 * This does a blocking write-and-wait on any page that belongs to the
908 * subbuffer prior to the one we just wrote.
909 * Don't care about error values, as these are just hints and ways to
910 * limit the amount of page cache used.
912 if (orig_offset
< stream
->chan
->max_sb_size
) {
915 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
916 stream
->chan
->max_sb_size
,
917 SYNC_FILE_RANGE_WAIT_BEFORE
918 | SYNC_FILE_RANGE_WRITE
919 | SYNC_FILE_RANGE_WAIT_AFTER
);
921 * Give hints to the kernel about how we access the file:
922 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
925 * We need to call fadvise again after the file grows because the
926 * kernel does not seem to apply fadvise to non-existing parts of the
929 * Call fadvise _after_ having waited for the page writeback to
930 * complete because the dirty page writeback semantic is not well
931 * defined. So it can be expected to lead to lower throughput in
934 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
935 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
939 * Initialise the necessary environnement :
940 * - create a new context
941 * - create the poll_pipe
942 * - create the should_quit pipe (for signal handler)
943 * - create the thread pipe (for splice)
945 * Takes a function pointer as argument, this function is called when data is
946 * available on a buffer. This function is responsible to do the
947 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
948 * buffer configuration and then kernctl_put_next_subbuf at the end.
950 * Returns a pointer to the new context or NULL on error.
952 struct lttng_consumer_local_data
*lttng_consumer_create(
953 enum lttng_consumer_type type
,
954 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
955 struct lttng_consumer_local_data
*ctx
),
956 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
957 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
958 int (*update_stream
)(int stream_key
, uint32_t state
))
961 struct lttng_consumer_local_data
*ctx
;
963 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
964 consumer_data
.type
== type
);
965 consumer_data
.type
= type
;
967 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
969 perror("allocating context");
973 ctx
->consumer_error_socket
= -1;
974 /* assign the callbacks */
975 ctx
->on_buffer_ready
= buffer_ready
;
976 ctx
->on_recv_channel
= recv_channel
;
977 ctx
->on_recv_stream
= recv_stream
;
978 ctx
->on_update_stream
= update_stream
;
980 ret
= pipe(ctx
->consumer_poll_pipe
);
982 perror("Error creating poll pipe");
983 goto error_poll_pipe
;
986 /* set read end of the pipe to non-blocking */
987 ret
= fcntl(ctx
->consumer_poll_pipe
[0], F_SETFL
, O_NONBLOCK
);
989 perror("fcntl O_NONBLOCK");
990 goto error_poll_fcntl
;
993 /* set write end of the pipe to non-blocking */
994 ret
= fcntl(ctx
->consumer_poll_pipe
[1], F_SETFL
, O_NONBLOCK
);
996 perror("fcntl O_NONBLOCK");
997 goto error_poll_fcntl
;
1000 ret
= pipe(ctx
->consumer_should_quit
);
1002 perror("Error creating recv pipe");
1003 goto error_quit_pipe
;
1006 ret
= pipe(ctx
->consumer_thread_pipe
);
1008 perror("Error creating thread pipe");
1009 goto error_thread_pipe
;
1012 ret
= utils_create_pipe(ctx
->consumer_metadata_pipe
);
1014 goto error_metadata_pipe
;
1017 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1019 goto error_splice_pipe
;
1025 utils_close_pipe(ctx
->consumer_metadata_pipe
);
1026 error_metadata_pipe
:
1027 utils_close_pipe(ctx
->consumer_thread_pipe
);
1029 for (i
= 0; i
< 2; i
++) {
1032 err
= close(ctx
->consumer_should_quit
[i
]);
1039 for (i
= 0; i
< 2; i
++) {
1042 err
= close(ctx
->consumer_poll_pipe
[i
]);
1054 * Close all fds associated with the instance and free the context.
1056 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1060 ret
= close(ctx
->consumer_error_socket
);
1064 ret
= close(ctx
->consumer_thread_pipe
[0]);
1068 ret
= close(ctx
->consumer_thread_pipe
[1]);
1072 ret
= close(ctx
->consumer_poll_pipe
[0]);
1076 ret
= close(ctx
->consumer_poll_pipe
[1]);
1080 ret
= close(ctx
->consumer_should_quit
[0]);
1084 ret
= close(ctx
->consumer_should_quit
[1]);
1088 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1090 unlink(ctx
->consumer_command_sock_path
);
1095 * Write the metadata stream id on the specified file descriptor.
1097 static int write_relayd_metadata_id(int fd
,
1098 struct lttng_consumer_stream
*stream
,
1099 struct consumer_relayd_sock_pair
*relayd
,
1100 unsigned long padding
)
1103 struct lttcomm_relayd_metadata_payload hdr
;
1105 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1106 hdr
.padding_size
= htobe32(padding
);
1108 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1109 } while (ret
< 0 && errno
== EINTR
);
1111 PERROR("write metadata stream id");
1114 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1115 stream
->relayd_stream_id
, padding
);
1122 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1123 * core function for writing trace buffers to either the local filesystem or
1126 * Careful review MUST be put if any changes occur!
1128 * Returns the number of bytes written
1130 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1131 struct lttng_consumer_local_data
*ctx
,
1132 struct lttng_consumer_stream
*stream
, unsigned long len
,
1133 unsigned long padding
)
1135 unsigned long mmap_offset
;
1136 ssize_t ret
= 0, written
= 0;
1137 off_t orig_offset
= stream
->out_fd_offset
;
1138 /* Default is on the disk */
1139 int outfd
= stream
->out_fd
;
1140 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1142 /* RCU lock for the relayd pointer */
1145 /* Flag that the current stream if set for network streaming. */
1146 if (stream
->net_seq_idx
!= -1) {
1147 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1148 if (relayd
== NULL
) {
1153 /* get the offset inside the fd to mmap */
1154 switch (consumer_data
.type
) {
1155 case LTTNG_CONSUMER_KERNEL
:
1156 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1158 case LTTNG_CONSUMER32_UST
:
1159 case LTTNG_CONSUMER64_UST
:
1160 ret
= lttng_ustctl_get_mmap_read_offset(stream
->chan
->handle
,
1161 stream
->buf
, &mmap_offset
);
1164 ERR("Unknown consumer_data type");
1169 PERROR("tracer ctl get_mmap_read_offset");
1174 /* Handle stream on the relayd if the output is on the network */
1176 unsigned long netlen
= len
;
1179 * Lock the control socket for the complete duration of the function
1180 * since from this point on we will use the socket.
1182 if (stream
->metadata_flag
) {
1183 /* Metadata requires the control socket. */
1184 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1185 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1188 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1190 /* Use the returned socket. */
1193 /* Write metadata stream id before payload */
1194 if (stream
->metadata_flag
) {
1195 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1202 /* Else, use the default set before which is the filesystem. */
1204 /* No streaming, we have to set the len with the full padding */
1210 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
1211 } while (ret
< 0 && errno
== EINTR
);
1212 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1214 PERROR("Error in file write");
1219 } else if (ret
> len
) {
1220 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1228 /* This call is useless on a socket so better save a syscall. */
1230 /* This won't block, but will start writeout asynchronously */
1231 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1232 SYNC_FILE_RANGE_WRITE
);
1233 stream
->out_fd_offset
+= ret
;
1237 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1240 /* Unlock only if ctrl socket used */
1241 if (relayd
&& stream
->metadata_flag
) {
1242 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1250 * Splice the data from the ring buffer to the tracefile.
1252 * Returns the number of bytes spliced.
1254 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1255 struct lttng_consumer_local_data
*ctx
,
1256 struct lttng_consumer_stream
*stream
, unsigned long len
,
1257 unsigned long padding
)
1259 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1261 off_t orig_offset
= stream
->out_fd_offset
;
1262 int fd
= stream
->wait_fd
;
1263 /* Default is on the disk */
1264 int outfd
= stream
->out_fd
;
1265 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1268 switch (consumer_data
.type
) {
1269 case LTTNG_CONSUMER_KERNEL
:
1271 case LTTNG_CONSUMER32_UST
:
1272 case LTTNG_CONSUMER64_UST
:
1273 /* Not supported for user space tracing */
1276 ERR("Unknown consumer_data type");
1280 /* RCU lock for the relayd pointer */
1283 /* Flag that the current stream if set for network streaming. */
1284 if (stream
->net_seq_idx
!= -1) {
1285 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1286 if (relayd
== NULL
) {
1292 * Choose right pipe for splice. Metadata and trace data are handled by
1293 * different threads hence the use of two pipes in order not to race or
1294 * corrupt the written data.
1296 if (stream
->metadata_flag
) {
1297 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1299 splice_pipe
= ctx
->consumer_thread_pipe
;
1302 /* Write metadata stream id before payload */
1304 int total_len
= len
;
1306 if (stream
->metadata_flag
) {
1308 * Lock the control socket for the complete duration of the function
1309 * since from this point on we will use the socket.
1311 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1313 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1320 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1323 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1325 /* Use the returned socket. */
1328 ERR("Remote relayd disconnected. Stopping");
1332 /* No streaming, we have to set the len with the full padding */
1337 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1338 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1339 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1340 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1341 DBG("splice chan to pipe, ret %zd", ret_splice
);
1342 if (ret_splice
< 0) {
1343 PERROR("Error in relay splice");
1345 written
= ret_splice
;
1351 /* Handle stream on the relayd if the output is on the network */
1353 if (stream
->metadata_flag
) {
1354 size_t metadata_payload_size
=
1355 sizeof(struct lttcomm_relayd_metadata_payload
);
1357 /* Update counter to fit the spliced data */
1358 ret_splice
+= metadata_payload_size
;
1359 len
+= metadata_payload_size
;
1361 * We do this so the return value can match the len passed as
1362 * argument to this function.
1364 written
-= metadata_payload_size
;
1368 /* Splice data out */
1369 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1370 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1371 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1372 if (ret_splice
< 0) {
1373 PERROR("Error in file splice");
1375 written
= ret_splice
;
1379 } else if (ret_splice
> len
) {
1381 PERROR("Wrote more data than requested %zd (len: %lu)",
1383 written
+= ret_splice
;
1389 /* This call is useless on a socket so better save a syscall. */
1391 /* This won't block, but will start writeout asynchronously */
1392 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1393 SYNC_FILE_RANGE_WRITE
);
1394 stream
->out_fd_offset
+= ret_splice
;
1396 written
+= ret_splice
;
1398 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1405 /* send the appropriate error description to sessiond */
1408 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EBADF
);
1411 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1414 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1417 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1422 if (relayd
&& stream
->metadata_flag
) {
1423 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1431 * Take a snapshot for a specific fd
1433 * Returns 0 on success, < 0 on error
1435 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1436 struct lttng_consumer_stream
*stream
)
1438 switch (consumer_data
.type
) {
1439 case LTTNG_CONSUMER_KERNEL
:
1440 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1441 case LTTNG_CONSUMER32_UST
:
1442 case LTTNG_CONSUMER64_UST
:
1443 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1445 ERR("Unknown consumer_data type");
1453 * Get the produced position
1455 * Returns 0 on success, < 0 on error
1457 int lttng_consumer_get_produced_snapshot(
1458 struct lttng_consumer_local_data
*ctx
,
1459 struct lttng_consumer_stream
*stream
,
1462 switch (consumer_data
.type
) {
1463 case LTTNG_CONSUMER_KERNEL
:
1464 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1465 case LTTNG_CONSUMER32_UST
:
1466 case LTTNG_CONSUMER64_UST
:
1467 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1469 ERR("Unknown consumer_data type");
1475 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1476 int sock
, struct pollfd
*consumer_sockpoll
)
1478 switch (consumer_data
.type
) {
1479 case LTTNG_CONSUMER_KERNEL
:
1480 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1481 case LTTNG_CONSUMER32_UST
:
1482 case LTTNG_CONSUMER64_UST
:
1483 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1485 ERR("Unknown consumer_data type");
1492 * Iterate over all stream element of the hashtable and free them. This is race
1493 * free since the hashtable received MUST be in a race free synchronization
1494 * state. It's the caller responsability to make sure of that.
1496 static void destroy_stream_ht(struct lttng_ht
*ht
)
1499 struct lttng_ht_iter iter
;
1500 struct lttng_consumer_stream
*stream
;
1506 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1507 ret
= lttng_ht_del(ht
, &iter
);
1513 lttng_ht_destroy(ht
);
1517 * Clean up a metadata stream and free its memory.
1519 static void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
)
1522 struct lttng_consumer_channel
*free_chan
= NULL
;
1523 struct consumer_relayd_sock_pair
*relayd
;
1527 * This call should NEVER receive regular stream. It must always be
1528 * metadata stream and this is crucial for data structure synchronization.
1530 assert(stream
->metadata_flag
);
1532 pthread_mutex_lock(&consumer_data
.lock
);
1533 switch (consumer_data
.type
) {
1534 case LTTNG_CONSUMER_KERNEL
:
1535 if (stream
->mmap_base
!= NULL
) {
1536 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1538 PERROR("munmap metadata stream");
1542 case LTTNG_CONSUMER32_UST
:
1543 case LTTNG_CONSUMER64_UST
:
1544 lttng_ustconsumer_del_stream(stream
);
1547 ERR("Unknown consumer_data type");
1550 pthread_mutex_unlock(&consumer_data
.lock
);
1552 if (stream
->out_fd
>= 0) {
1553 ret
= close(stream
->out_fd
);
1559 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
1560 ret
= close(stream
->wait_fd
);
1566 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
1567 ret
= close(stream
->shm_fd
);
1573 /* Check and cleanup relayd */
1575 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1576 if (relayd
!= NULL
) {
1577 uatomic_dec(&relayd
->refcount
);
1578 assert(uatomic_read(&relayd
->refcount
) >= 0);
1580 /* Closing streams requires to lock the control socket. */
1581 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1582 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1583 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1584 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1586 DBG("Unable to close stream on the relayd. Continuing");
1588 * Continue here. There is nothing we can do for the relayd.
1589 * Chances are that the relayd has closed the socket so we just
1590 * continue cleaning up.
1594 /* Both conditions are met, we destroy the relayd. */
1595 if (uatomic_read(&relayd
->refcount
) == 0 &&
1596 uatomic_read(&relayd
->destroy_flag
)) {
1597 consumer_destroy_relayd(relayd
);
1602 /* Atomically decrement channel refcount since other threads can use it. */
1603 uatomic_dec(&stream
->chan
->refcount
);
1604 if (!uatomic_read(&stream
->chan
->refcount
)) {
1605 free_chan
= stream
->chan
;
1609 consumer_del_channel(free_chan
);
1616 * Action done with the metadata stream when adding it to the consumer internal
1617 * data structures to handle it.
1619 static void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
1621 struct consumer_relayd_sock_pair
*relayd
;
1623 /* Find relayd and, if one is found, increment refcount. */
1625 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1626 if (relayd
!= NULL
) {
1627 uatomic_inc(&relayd
->refcount
);
1633 * Thread polls on metadata file descriptor and write them on disk or on the
1636 void *lttng_consumer_thread_poll_metadata(void *data
)
1639 uint32_t revents
, nb_fd
;
1640 struct lttng_consumer_stream
*stream
;
1641 struct lttng_ht_iter iter
;
1642 struct lttng_ht_node_ulong
*node
;
1643 struct lttng_ht
*metadata_ht
= NULL
;
1644 struct lttng_poll_event events
;
1645 struct lttng_consumer_local_data
*ctx
= data
;
1648 rcu_register_thread();
1650 DBG("Thread metadata poll started");
1652 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1653 if (metadata_ht
== NULL
) {
1657 /* Size is set to 1 for the consumer_metadata pipe */
1658 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
1660 ERR("Poll set creation failed");
1664 ret
= lttng_poll_add(&events
, ctx
->consumer_metadata_pipe
[0], LPOLLIN
);
1670 DBG("Metadata main loop started");
1673 lttng_poll_reset(&events
);
1675 nb_fd
= LTTNG_POLL_GETNB(&events
);
1677 /* Only the metadata pipe is set */
1678 if (nb_fd
== 0 && consumer_quit
== 1) {
1683 DBG("Metadata poll wait with %d fd(s)", nb_fd
);
1684 ret
= lttng_poll_wait(&events
, -1);
1685 DBG("Metadata event catched in thread");
1687 if (errno
== EINTR
) {
1693 for (i
= 0; i
< nb_fd
; i
++) {
1694 revents
= LTTNG_POLL_GETEV(&events
, i
);
1695 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1697 /* Check the metadata pipe for incoming metadata. */
1698 if (pollfd
== ctx
->consumer_metadata_pipe
[0]) {
1699 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLNVAL
)) {
1700 DBG("Metadata thread pipe hung up");
1702 * Remove the pipe from the poll set and continue the loop
1703 * since their might be data to consume.
1705 lttng_poll_del(&events
, ctx
->consumer_metadata_pipe
[0]);
1706 close(ctx
->consumer_metadata_pipe
[0]);
1708 } else if (revents
& LPOLLIN
) {
1709 stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
1710 if (stream
== NULL
) {
1711 PERROR("zmalloc metadata consumer stream");
1716 /* Get the stream and add it to the local hash table */
1717 ret
= read(pollfd
, stream
,
1718 sizeof(struct lttng_consumer_stream
));
1719 } while (ret
< 0 && errno
== EINTR
);
1720 if (ret
< 0 || ret
< sizeof(struct lttng_consumer_stream
)) {
1721 PERROR("read metadata stream");
1724 * Let's continue here and hope we can still work
1725 * without stopping the consumer. XXX: Should we?
1730 DBG("Adding metadata stream %d to poll set",
1733 /* The node should be init at this point */
1734 lttng_ht_add_unique_ulong(metadata_ht
,
1735 &stream
->waitfd_node
);
1737 /* Add metadata stream to the global poll events list */
1738 lttng_poll_add(&events
, stream
->wait_fd
,
1739 LPOLLIN
| LPOLLPRI
);
1741 consumer_add_metadata_stream(stream
);
1744 /* Metadata pipe handled. Continue handling the others */
1748 /* From here, the event is a metadata wait fd */
1750 lttng_ht_lookup(metadata_ht
, (void *)((unsigned long) pollfd
),
1752 node
= lttng_ht_iter_get_node_ulong(&iter
);
1754 /* FD not found, continue loop */
1758 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
1761 /* Get the data out of the metadata file descriptor */
1762 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
1763 DBG("Metadata available on fd %d", pollfd
);
1764 assert(stream
->wait_fd
== pollfd
);
1766 len
= ctx
->on_buffer_ready(stream
, ctx
);
1767 /* It's ok to have an unavailable sub-buffer */
1768 if (len
< 0 && len
!= -EAGAIN
) {
1770 } else if (len
> 0) {
1771 stream
->data_read
= 1;
1776 * Remove the stream from the hash table since there is no data
1777 * left on the fd because we previously did a read on the buffer.
1779 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLNVAL
)) {
1780 DBG("Metadata fd %d is hup|err|nval.", pollfd
);
1781 if (!stream
->hangup_flush_done
1782 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1783 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1784 DBG("Attempting to flush and consume the UST buffers");
1785 lttng_ustconsumer_on_stream_hangup(stream
);
1787 /* We just flushed the stream now read it. */
1788 len
= ctx
->on_buffer_ready(stream
, ctx
);
1789 /* It's ok to have an unavailable sub-buffer */
1790 if (len
< 0 && len
!= -EAGAIN
) {
1795 /* Removing it from hash table, poll set and free memory */
1796 lttng_ht_del(metadata_ht
, &iter
);
1797 lttng_poll_del(&events
, stream
->wait_fd
);
1798 consumer_del_metadata_stream(stream
);
1805 DBG("Metadata poll thread exiting");
1806 lttng_poll_clean(&events
);
1809 destroy_stream_ht(metadata_ht
);
1812 rcu_unregister_thread();
1817 * This thread polls the fds in the set to consume the data and write
1818 * it to tracefile if necessary.
1820 void *lttng_consumer_thread_poll_fds(void *data
)
1822 int num_rdy
, num_hup
, high_prio
, ret
, i
;
1823 struct pollfd
*pollfd
= NULL
;
1824 /* local view of the streams */
1825 struct lttng_consumer_stream
**local_stream
= NULL
;
1826 /* local view of consumer_data.fds_count */
1828 struct lttng_consumer_local_data
*ctx
= data
;
1830 pthread_t metadata_thread
;
1833 rcu_register_thread();
1835 /* Start metadata polling thread */
1836 ret
= pthread_create(&metadata_thread
, NULL
,
1837 lttng_consumer_thread_poll_metadata
, (void *) ctx
);
1839 PERROR("pthread_create metadata thread");
1843 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
1850 * the fds set has been updated, we need to update our
1851 * local array as well
1853 pthread_mutex_lock(&consumer_data
.lock
);
1854 if (consumer_data
.need_update
) {
1855 if (pollfd
!= NULL
) {
1859 if (local_stream
!= NULL
) {
1861 local_stream
= NULL
;
1864 /* allocate for all fds + 1 for the consumer_poll_pipe */
1865 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
1866 if (pollfd
== NULL
) {
1867 perror("pollfd malloc");
1868 pthread_mutex_unlock(&consumer_data
.lock
);
1872 /* allocate for all fds + 1 for the consumer_poll_pipe */
1873 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
1874 sizeof(struct lttng_consumer_stream
));
1875 if (local_stream
== NULL
) {
1876 perror("local_stream malloc");
1877 pthread_mutex_unlock(&consumer_data
.lock
);
1880 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
);
1882 ERR("Error in allocating pollfd or local_outfds");
1883 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
1884 pthread_mutex_unlock(&consumer_data
.lock
);
1888 consumer_data
.need_update
= 0;
1890 pthread_mutex_unlock(&consumer_data
.lock
);
1892 /* No FDs and consumer_quit, consumer_cleanup the thread */
1893 if (nb_fd
== 0 && consumer_quit
== 1) {
1896 /* poll on the array of fds */
1898 DBG("polling on %d fd", nb_fd
+ 1);
1899 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
1900 DBG("poll num_rdy : %d", num_rdy
);
1901 if (num_rdy
== -1) {
1903 * Restart interrupted system call.
1905 if (errno
== EINTR
) {
1908 perror("Poll error");
1909 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
1911 } else if (num_rdy
== 0) {
1912 DBG("Polling thread timed out");
1917 * If the consumer_poll_pipe triggered poll go directly to the
1918 * beginning of the loop to update the array. We want to prioritize
1919 * array update over low-priority reads.
1921 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
1922 size_t pipe_readlen
;
1925 DBG("consumer_poll_pipe wake up");
1926 /* Consume 1 byte of pipe data */
1928 pipe_readlen
= read(ctx
->consumer_poll_pipe
[0], &tmp
, 1);
1929 } while (pipe_readlen
== -1 && errno
== EINTR
);
1933 /* Take care of high priority channels first. */
1934 for (i
= 0; i
< nb_fd
; i
++) {
1935 if (pollfd
[i
].revents
& POLLPRI
) {
1936 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
1938 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1939 /* it's ok to have an unavailable sub-buffer */
1940 if (len
< 0 && len
!= -EAGAIN
) {
1942 } else if (len
> 0) {
1943 local_stream
[i
]->data_read
= 1;
1949 * If we read high prio channel in this loop, try again
1950 * for more high prio data.
1956 /* Take care of low priority channels. */
1957 for (i
= 0; i
< nb_fd
; i
++) {
1958 if ((pollfd
[i
].revents
& POLLIN
) ||
1959 local_stream
[i
]->hangup_flush_done
) {
1960 DBG("Normal read on fd %d", pollfd
[i
].fd
);
1961 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1962 /* it's ok to have an unavailable sub-buffer */
1963 if (len
< 0 && len
!= -EAGAIN
) {
1965 } else if (len
> 0) {
1966 local_stream
[i
]->data_read
= 1;
1971 /* Handle hangup and errors */
1972 for (i
= 0; i
< nb_fd
; i
++) {
1973 if (!local_stream
[i
]->hangup_flush_done
1974 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
1975 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1976 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1977 DBG("fd %d is hup|err|nval. Attempting flush and read.",
1979 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
1980 /* Attempt read again, for the data we just flushed. */
1981 local_stream
[i
]->data_read
= 1;
1984 * If the poll flag is HUP/ERR/NVAL and we have
1985 * read no data in this pass, we can remove the
1986 * stream from its hash table.
1988 if ((pollfd
[i
].revents
& POLLHUP
)) {
1989 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
1990 if (!local_stream
[i
]->data_read
) {
1991 consumer_del_stream(local_stream
[i
]);
1994 } else if (pollfd
[i
].revents
& POLLERR
) {
1995 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
1996 if (!local_stream
[i
]->data_read
) {
1997 consumer_del_stream(local_stream
[i
]);
2000 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2001 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2002 if (!local_stream
[i
]->data_read
) {
2003 consumer_del_stream(local_stream
[i
]);
2007 local_stream
[i
]->data_read
= 0;
2011 DBG("polling thread exiting");
2012 if (pollfd
!= NULL
) {
2016 if (local_stream
!= NULL
) {
2018 local_stream
= NULL
;
2022 * Close the write side of the pipe so epoll_wait() in
2023 * lttng_consumer_thread_poll_metadata can catch it. The thread is
2024 * monitoring the read side of the pipe. If we close them both, epoll_wait
2025 * strangely does not return and could create a endless wait period if the
2026 * pipe is the only tracked fd in the poll set. The thread will take care
2027 * of closing the read side.
2029 close(ctx
->consumer_metadata_pipe
[1]);
2031 ret
= pthread_join(metadata_thread
, &status
);
2033 PERROR("pthread_join metadata thread");
2037 rcu_unregister_thread();
2042 * This thread listens on the consumerd socket and receives the file
2043 * descriptors from the session daemon.
2045 void *lttng_consumer_thread_receive_fds(void *data
)
2047 int sock
, client_socket
, ret
;
2049 * structure to poll for incoming data on communication socket avoids
2050 * making blocking sockets.
2052 struct pollfd consumer_sockpoll
[2];
2053 struct lttng_consumer_local_data
*ctx
= data
;
2055 rcu_register_thread();
2057 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2058 unlink(ctx
->consumer_command_sock_path
);
2059 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2060 if (client_socket
< 0) {
2061 ERR("Cannot create command socket");
2065 ret
= lttcomm_listen_unix_sock(client_socket
);
2070 DBG("Sending ready command to lttng-sessiond");
2071 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2072 /* return < 0 on error, but == 0 is not fatal */
2074 ERR("Error sending ready command to lttng-sessiond");
2078 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2080 perror("fcntl O_NONBLOCK");
2084 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2085 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2086 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2087 consumer_sockpoll
[1].fd
= client_socket
;
2088 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2090 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2093 DBG("Connection on client_socket");
2095 /* Blocking call, waiting for transmission */
2096 sock
= lttcomm_accept_unix_sock(client_socket
);
2101 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2103 perror("fcntl O_NONBLOCK");
2107 /* update the polling structure to poll on the established socket */
2108 consumer_sockpoll
[1].fd
= sock
;
2109 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2112 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2115 DBG("Incoming command on sock");
2116 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2117 if (ret
== -ENOENT
) {
2118 DBG("Received STOP command");
2123 * This could simply be a session daemon quitting. Don't output
2126 DBG("Communication interrupted on command socket");
2129 if (consumer_quit
) {
2130 DBG("consumer_thread_receive_fds received quit from signal");
2133 DBG("received fds on sock");
2136 DBG("consumer_thread_receive_fds exiting");
2139 * when all fds have hung up, the polling thread
2145 * 2s of grace period, if no polling events occur during
2146 * this period, the polling thread will exit even if there
2147 * are still open FDs (should not happen, but safety mechanism).
2149 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
2152 * Wake-up the other end by writing a null byte in the pipe
2153 * (non-blocking). Important note: Because writing into the
2154 * pipe is non-blocking (and therefore we allow dropping wakeup
2155 * data, as long as there is wakeup data present in the pipe
2156 * buffer to wake up the other end), the other end should
2157 * perform the following sequence for waiting:
2158 * 1) empty the pipe (reads).
2159 * 2) perform update operation.
2160 * 3) wait on the pipe (poll).
2163 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
2164 } while (ret
< 0 && errno
== EINTR
);
2165 rcu_unregister_thread();
2169 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2170 struct lttng_consumer_local_data
*ctx
)
2172 switch (consumer_data
.type
) {
2173 case LTTNG_CONSUMER_KERNEL
:
2174 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
2175 case LTTNG_CONSUMER32_UST
:
2176 case LTTNG_CONSUMER64_UST
:
2177 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2179 ERR("Unknown consumer_data type");
2185 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2187 switch (consumer_data
.type
) {
2188 case LTTNG_CONSUMER_KERNEL
:
2189 return lttng_kconsumer_on_recv_stream(stream
);
2190 case LTTNG_CONSUMER32_UST
:
2191 case LTTNG_CONSUMER64_UST
:
2192 return lttng_ustconsumer_on_recv_stream(stream
);
2194 ERR("Unknown consumer_data type");
2201 * Allocate and set consumer data hash tables.
2203 void lttng_consumer_init(void)
2205 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2206 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2207 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2211 * Process the ADD_RELAYD command receive by a consumer.
2213 * This will create a relayd socket pair and add it to the relayd hash table.
2214 * The caller MUST acquire a RCU read side lock before calling it.
2216 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
2217 struct lttng_consumer_local_data
*ctx
, int sock
,
2218 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
)
2221 struct consumer_relayd_sock_pair
*relayd
;
2223 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
2225 /* Get relayd reference if exists. */
2226 relayd
= consumer_find_relayd(net_seq_idx
);
2227 if (relayd
== NULL
) {
2228 /* Not found. Allocate one. */
2229 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
2230 if (relayd
== NULL
) {
2231 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
2236 /* Poll on consumer socket. */
2237 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2242 /* Get relayd socket from session daemon */
2243 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2244 if (ret
!= sizeof(fd
)) {
2245 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
2250 /* Copy socket information and received FD */
2251 switch (sock_type
) {
2252 case LTTNG_STREAM_CONTROL
:
2253 /* Copy received lttcomm socket */
2254 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
2255 ret
= lttcomm_create_sock(&relayd
->control_sock
);
2260 /* Close the created socket fd which is useless */
2261 close(relayd
->control_sock
.fd
);
2263 /* Assign new file descriptor */
2264 relayd
->control_sock
.fd
= fd
;
2266 case LTTNG_STREAM_DATA
:
2267 /* Copy received lttcomm socket */
2268 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
2269 ret
= lttcomm_create_sock(&relayd
->data_sock
);
2274 /* Close the created socket fd which is useless */
2275 close(relayd
->data_sock
.fd
);
2277 /* Assign new file descriptor */
2278 relayd
->data_sock
.fd
= fd
;
2281 ERR("Unknown relayd socket type (%d)", sock_type
);
2285 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2286 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
2287 relayd
->net_seq_idx
, fd
);
2290 * Add relayd socket pair to consumer data hashtable. If object already
2291 * exists or on error, the function gracefully returns.
2293 consumer_add_relayd(relayd
);