2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2012 - David Goulet <dgoulet@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/socket.h>
28 #include <sys/types.h>
32 #include <common/common.h>
33 #include <common/utils.h>
34 #include <common/compat/poll.h>
35 #include <common/kernel-ctl/kernel-ctl.h>
36 #include <common/sessiond-comm/relayd.h>
37 #include <common/sessiond-comm/sessiond-comm.h>
38 #include <common/kernel-consumer/kernel-consumer.h>
39 #include <common/relayd/relayd.h>
40 #include <common/ust-consumer/ust-consumer.h>
44 struct lttng_consumer_global_data consumer_data
= {
47 .type
= LTTNG_CONSUMER_UNKNOWN
,
50 /* timeout parameter, to control the polling thread grace period. */
51 int consumer_poll_timeout
= -1;
54 * Flag to inform the polling thread to quit when all fd hung up. Updated by
55 * the consumer_thread_receive_fds when it notices that all fds has hung up.
56 * Also updated by the signal handler (consumer_should_exit()). Read by the
59 volatile int consumer_quit
= 0;
62 * Find a stream. The consumer_data.lock must be locked during this
65 static struct lttng_consumer_stream
*consumer_find_stream(int key
)
67 struct lttng_ht_iter iter
;
68 struct lttng_ht_node_ulong
*node
;
69 struct lttng_consumer_stream
*stream
= NULL
;
71 /* Negative keys are lookup failures */
77 lttng_ht_lookup(consumer_data
.stream_ht
, (void *)((unsigned long) key
),
79 node
= lttng_ht_iter_get_node_ulong(&iter
);
81 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
89 static void consumer_steal_stream_key(int key
)
91 struct lttng_consumer_stream
*stream
;
94 stream
= consumer_find_stream(key
);
98 * We don't want the lookup to match, but we still need
99 * to iterate on this stream when iterating over the hash table. Just
100 * change the node key.
102 stream
->node
.key
= -1;
107 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
109 struct lttng_ht_iter iter
;
110 struct lttng_ht_node_ulong
*node
;
111 struct lttng_consumer_channel
*channel
= NULL
;
113 /* Negative keys are lookup failures */
119 lttng_ht_lookup(consumer_data
.channel_ht
, (void *)((unsigned long) key
),
121 node
= lttng_ht_iter_get_node_ulong(&iter
);
123 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
131 static void consumer_steal_channel_key(int key
)
133 struct lttng_consumer_channel
*channel
;
136 channel
= consumer_find_channel(key
);
140 * We don't want the lookup to match, but we still need
141 * to iterate on this channel when iterating over the hash table. Just
142 * change the node key.
144 channel
->node
.key
= -1;
150 void consumer_free_stream(struct rcu_head
*head
)
152 struct lttng_ht_node_ulong
*node
=
153 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
154 struct lttng_consumer_stream
*stream
=
155 caa_container_of(node
, struct lttng_consumer_stream
, node
);
161 * RCU protected relayd socket pair free.
163 static void consumer_rcu_free_relayd(struct rcu_head
*head
)
165 struct lttng_ht_node_ulong
*node
=
166 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
167 struct consumer_relayd_sock_pair
*relayd
=
168 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
174 * Destroy and free relayd socket pair object.
176 * This function MUST be called with the consumer_data lock acquired.
178 static void destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
181 struct lttng_ht_iter iter
;
183 if (relayd
== NULL
) {
187 DBG("Consumer destroy and close relayd socket pair");
189 iter
.iter
.node
= &relayd
->node
.node
;
190 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
192 /* We assume the relayd was already destroyed */
196 /* Close all sockets */
197 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
198 (void) relayd_close(&relayd
->control_sock
);
199 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
200 (void) relayd_close(&relayd
->data_sock
);
202 /* RCU free() call */
203 call_rcu(&relayd
->node
.head
, consumer_rcu_free_relayd
);
207 * Flag a relayd socket pair for destruction. Destroy it if the refcount
210 * RCU read side lock MUST be aquired before calling this function.
212 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
216 /* Set destroy flag for this object */
217 uatomic_set(&relayd
->destroy_flag
, 1);
219 /* Destroy the relayd if refcount is 0 */
220 if (uatomic_read(&relayd
->refcount
) == 0) {
221 destroy_relayd(relayd
);
226 * Remove a stream from the global list protected by a mutex. This
227 * function is also responsible for freeing its data structures.
229 void consumer_del_stream(struct lttng_consumer_stream
*stream
)
232 struct lttng_ht_iter iter
;
233 struct lttng_consumer_channel
*free_chan
= NULL
;
234 struct consumer_relayd_sock_pair
*relayd
;
238 pthread_mutex_lock(&consumer_data
.lock
);
240 switch (consumer_data
.type
) {
241 case LTTNG_CONSUMER_KERNEL
:
242 if (stream
->mmap_base
!= NULL
) {
243 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
249 case LTTNG_CONSUMER32_UST
:
250 case LTTNG_CONSUMER64_UST
:
251 lttng_ustconsumer_del_stream(stream
);
254 ERR("Unknown consumer_data type");
260 iter
.iter
.node
= &stream
->node
.node
;
261 ret
= lttng_ht_del(consumer_data
.stream_ht
, &iter
);
266 if (consumer_data
.stream_count
<= 0) {
269 consumer_data
.stream_count
--;
273 if (stream
->out_fd
>= 0) {
274 ret
= close(stream
->out_fd
);
279 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
280 ret
= close(stream
->wait_fd
);
285 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
286 ret
= close(stream
->shm_fd
);
292 /* Check and cleanup relayd */
294 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
295 if (relayd
!= NULL
) {
296 uatomic_dec(&relayd
->refcount
);
297 assert(uatomic_read(&relayd
->refcount
) >= 0);
299 /* Closing streams requires to lock the control socket. */
300 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
301 ret
= relayd_send_close_stream(&relayd
->control_sock
,
302 stream
->relayd_stream_id
,
303 stream
->next_net_seq_num
- 1);
304 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
306 DBG("Unable to close stream on the relayd. Continuing");
308 * Continue here. There is nothing we can do for the relayd.
309 * Chances are that the relayd has closed the socket so we just
310 * continue cleaning up.
314 /* Both conditions are met, we destroy the relayd. */
315 if (uatomic_read(&relayd
->refcount
) == 0 &&
316 uatomic_read(&relayd
->destroy_flag
)) {
317 destroy_relayd(relayd
);
322 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.
455 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
458 struct lttng_ht_node_ulong
*node
;
459 struct lttng_ht_iter iter
;
461 if (relayd
== NULL
) {
466 lttng_ht_lookup(consumer_data
.relayd_ht
,
467 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
468 node
= lttng_ht_iter_get_node_ulong(&iter
);
470 /* Relayd already exist. Ignore the insertion */
473 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
480 * Allocate and return a consumer relayd socket.
482 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
485 struct consumer_relayd_sock_pair
*obj
= NULL
;
487 /* Negative net sequence index is a failure */
488 if (net_seq_idx
< 0) {
492 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
494 PERROR("zmalloc relayd sock");
498 obj
->net_seq_idx
= net_seq_idx
;
500 obj
->destroy_flag
= 0;
501 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
502 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
509 * Find a relayd socket pair in the global consumer data.
511 * Return the object if found else NULL.
512 * RCU read-side lock must be held across this call and while using the
515 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
517 struct lttng_ht_iter iter
;
518 struct lttng_ht_node_ulong
*node
;
519 struct consumer_relayd_sock_pair
*relayd
= NULL
;
521 /* Negative keys are lookup failures */
526 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
528 node
= lttng_ht_iter_get_node_ulong(&iter
);
530 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
538 * Handle stream for relayd transmission if the stream applies for network
539 * streaming where the net sequence index is set.
541 * Return destination file descriptor or negative value on error.
543 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
544 size_t data_size
, unsigned long padding
,
545 struct consumer_relayd_sock_pair
*relayd
)
548 struct lttcomm_relayd_data_hdr data_hdr
;
554 /* Reset data header */
555 memset(&data_hdr
, 0, sizeof(data_hdr
));
557 if (stream
->metadata_flag
) {
558 /* Caller MUST acquire the relayd control socket lock */
559 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
564 /* Metadata are always sent on the control socket. */
565 outfd
= relayd
->control_sock
.fd
;
567 /* Set header with stream information */
568 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
569 data_hdr
.data_size
= htobe32(data_size
);
570 data_hdr
.padding_size
= htobe32(padding
);
571 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
572 /* Other fields are zeroed previously */
574 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
580 /* Set to go on data socket */
581 outfd
= relayd
->data_sock
.fd
;
589 * Update a stream according to what we just received.
591 void consumer_change_stream_state(int stream_key
,
592 enum lttng_consumer_stream_state state
)
594 struct lttng_consumer_stream
*stream
;
596 pthread_mutex_lock(&consumer_data
.lock
);
597 stream
= consumer_find_stream(stream_key
);
599 stream
->state
= state
;
601 consumer_data
.need_update
= 1;
602 pthread_mutex_unlock(&consumer_data
.lock
);
606 void consumer_free_channel(struct rcu_head
*head
)
608 struct lttng_ht_node_ulong
*node
=
609 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
610 struct lttng_consumer_channel
*channel
=
611 caa_container_of(node
, struct lttng_consumer_channel
, node
);
617 * Remove a channel from the global list protected by a mutex. This
618 * function is also responsible for freeing its data structures.
620 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
623 struct lttng_ht_iter iter
;
625 pthread_mutex_lock(&consumer_data
.lock
);
627 switch (consumer_data
.type
) {
628 case LTTNG_CONSUMER_KERNEL
:
630 case LTTNG_CONSUMER32_UST
:
631 case LTTNG_CONSUMER64_UST
:
632 lttng_ustconsumer_del_channel(channel
);
635 ERR("Unknown consumer_data type");
641 iter
.iter
.node
= &channel
->node
.node
;
642 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
646 if (channel
->mmap_base
!= NULL
) {
647 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
652 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
653 ret
= close(channel
->wait_fd
);
658 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
659 ret
= close(channel
->shm_fd
);
665 call_rcu(&channel
->node
.head
, consumer_free_channel
);
667 pthread_mutex_unlock(&consumer_data
.lock
);
670 struct lttng_consumer_channel
*consumer_allocate_channel(
672 int shm_fd
, int wait_fd
,
674 uint64_t max_sb_size
)
676 struct lttng_consumer_channel
*channel
;
679 channel
= zmalloc(sizeof(*channel
));
680 if (channel
== NULL
) {
681 perror("malloc struct lttng_consumer_channel");
684 channel
->key
= channel_key
;
685 channel
->shm_fd
= shm_fd
;
686 channel
->wait_fd
= wait_fd
;
687 channel
->mmap_len
= mmap_len
;
688 channel
->max_sb_size
= max_sb_size
;
689 channel
->refcount
= 0;
690 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
692 switch (consumer_data
.type
) {
693 case LTTNG_CONSUMER_KERNEL
:
694 channel
->mmap_base
= NULL
;
695 channel
->mmap_len
= 0;
697 case LTTNG_CONSUMER32_UST
:
698 case LTTNG_CONSUMER64_UST
:
699 ret
= lttng_ustconsumer_allocate_channel(channel
);
706 ERR("Unknown consumer_data type");
710 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
711 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
712 (unsigned long long) channel
->mmap_len
,
713 (unsigned long long) channel
->max_sb_size
);
719 * Add a channel to the global list protected by a mutex.
721 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
723 struct lttng_ht_node_ulong
*node
;
724 struct lttng_ht_iter iter
;
726 pthread_mutex_lock(&consumer_data
.lock
);
727 /* Steal channel identifier, for UST */
728 consumer_steal_channel_key(channel
->key
);
731 lttng_ht_lookup(consumer_data
.channel_ht
,
732 (void *)((unsigned long) channel
->key
), &iter
);
733 node
= lttng_ht_iter_get_node_ulong(&iter
);
735 /* Channel already exist. Ignore the insertion */
739 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
743 pthread_mutex_unlock(&consumer_data
.lock
);
749 * Allocate the pollfd structure and the local view of the out fds to avoid
750 * doing a lookup in the linked list and concurrency issues when writing is
751 * needed. Called with consumer_data.lock held.
753 * Returns the number of fds in the structures.
755 int consumer_update_poll_array(
756 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
757 struct lttng_consumer_stream
**local_stream
)
760 struct lttng_ht_iter iter
;
761 struct lttng_consumer_stream
*stream
;
763 DBG("Updating poll fd array");
765 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, stream
,
767 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
770 DBG("Active FD %d", stream
->wait_fd
);
771 (*pollfd
)[i
].fd
= stream
->wait_fd
;
772 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
773 local_stream
[i
] = stream
;
779 * Insert the consumer_poll_pipe at the end of the array and don't
780 * increment i so nb_fd is the number of real FD.
782 (*pollfd
)[i
].fd
= ctx
->consumer_poll_pipe
[0];
783 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
788 * Poll on the should_quit pipe and the command socket return -1 on error and
789 * should exit, 0 if data is available on the command socket
791 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
796 num_rdy
= poll(consumer_sockpoll
, 2, -1);
799 * Restart interrupted system call.
801 if (errno
== EINTR
) {
804 perror("Poll error");
807 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
808 DBG("consumer_should_quit wake up");
818 * Set the error socket.
820 void lttng_consumer_set_error_sock(
821 struct lttng_consumer_local_data
*ctx
, int sock
)
823 ctx
->consumer_error_socket
= sock
;
827 * Set the command socket path.
829 void lttng_consumer_set_command_sock_path(
830 struct lttng_consumer_local_data
*ctx
, char *sock
)
832 ctx
->consumer_command_sock_path
= sock
;
836 * Send return code to the session daemon.
837 * If the socket is not defined, we return 0, it is not a fatal error
839 int lttng_consumer_send_error(
840 struct lttng_consumer_local_data
*ctx
, int cmd
)
842 if (ctx
->consumer_error_socket
> 0) {
843 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
844 sizeof(enum lttcomm_sessiond_command
));
851 * Close all the tracefiles and stream fds, should be called when all instances
854 void lttng_consumer_cleanup(void)
856 struct lttng_ht_iter iter
;
857 struct lttng_ht_node_ulong
*node
;
862 * close all outfd. Called when there are no more threads running (after
863 * joining on the threads), no need to protect list iteration with mutex.
865 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, node
,
867 struct lttng_consumer_stream
*stream
=
868 caa_container_of(node
, struct lttng_consumer_stream
, node
);
869 consumer_del_stream(stream
);
872 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
874 struct lttng_consumer_channel
*channel
=
875 caa_container_of(node
, struct lttng_consumer_channel
, node
);
876 consumer_del_channel(channel
);
881 lttng_ht_destroy(consumer_data
.stream_ht
);
882 lttng_ht_destroy(consumer_data
.channel_ht
);
886 * Called from signal handler.
888 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
893 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
894 } while (ret
< 0 && errno
== EINTR
);
896 perror("write consumer quit");
900 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
903 int outfd
= stream
->out_fd
;
906 * This does a blocking write-and-wait on any page that belongs to the
907 * subbuffer prior to the one we just wrote.
908 * Don't care about error values, as these are just hints and ways to
909 * limit the amount of page cache used.
911 if (orig_offset
< stream
->chan
->max_sb_size
) {
914 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
915 stream
->chan
->max_sb_size
,
916 SYNC_FILE_RANGE_WAIT_BEFORE
917 | SYNC_FILE_RANGE_WRITE
918 | SYNC_FILE_RANGE_WAIT_AFTER
);
920 * Give hints to the kernel about how we access the file:
921 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
924 * We need to call fadvise again after the file grows because the
925 * kernel does not seem to apply fadvise to non-existing parts of the
928 * Call fadvise _after_ having waited for the page writeback to
929 * complete because the dirty page writeback semantic is not well
930 * defined. So it can be expected to lead to lower throughput in
933 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
934 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
938 * Initialise the necessary environnement :
939 * - create a new context
940 * - create the poll_pipe
941 * - create the should_quit pipe (for signal handler)
942 * - create the thread pipe (for splice)
944 * Takes a function pointer as argument, this function is called when data is
945 * available on a buffer. This function is responsible to do the
946 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
947 * buffer configuration and then kernctl_put_next_subbuf at the end.
949 * Returns a pointer to the new context or NULL on error.
951 struct lttng_consumer_local_data
*lttng_consumer_create(
952 enum lttng_consumer_type type
,
953 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
954 struct lttng_consumer_local_data
*ctx
),
955 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
956 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
957 int (*update_stream
)(int stream_key
, uint32_t state
))
960 struct lttng_consumer_local_data
*ctx
;
962 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
963 consumer_data
.type
== type
);
964 consumer_data
.type
= type
;
966 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
968 perror("allocating context");
972 ctx
->consumer_error_socket
= -1;
973 /* assign the callbacks */
974 ctx
->on_buffer_ready
= buffer_ready
;
975 ctx
->on_recv_channel
= recv_channel
;
976 ctx
->on_recv_stream
= recv_stream
;
977 ctx
->on_update_stream
= update_stream
;
979 ret
= pipe(ctx
->consumer_poll_pipe
);
981 perror("Error creating poll pipe");
982 goto error_poll_pipe
;
985 /* set read end of the pipe to non-blocking */
986 ret
= fcntl(ctx
->consumer_poll_pipe
[0], F_SETFL
, O_NONBLOCK
);
988 perror("fcntl O_NONBLOCK");
989 goto error_poll_fcntl
;
992 /* set write end of the pipe to non-blocking */
993 ret
= fcntl(ctx
->consumer_poll_pipe
[1], F_SETFL
, O_NONBLOCK
);
995 perror("fcntl O_NONBLOCK");
996 goto error_poll_fcntl
;
999 ret
= pipe(ctx
->consumer_should_quit
);
1001 perror("Error creating recv pipe");
1002 goto error_quit_pipe
;
1005 ret
= pipe(ctx
->consumer_thread_pipe
);
1007 perror("Error creating thread pipe");
1008 goto error_thread_pipe
;
1011 ret
= utils_create_pipe(ctx
->consumer_metadata_pipe
);
1013 goto error_metadata_pipe
;
1016 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1018 goto error_splice_pipe
;
1024 utils_close_pipe(ctx
->consumer_metadata_pipe
);
1025 error_metadata_pipe
:
1026 utils_close_pipe(ctx
->consumer_thread_pipe
);
1028 for (i
= 0; i
< 2; i
++) {
1031 err
= close(ctx
->consumer_should_quit
[i
]);
1038 for (i
= 0; i
< 2; i
++) {
1041 err
= close(ctx
->consumer_poll_pipe
[i
]);
1053 * Close all fds associated with the instance and free the context.
1055 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1059 ret
= close(ctx
->consumer_error_socket
);
1063 ret
= close(ctx
->consumer_thread_pipe
[0]);
1067 ret
= close(ctx
->consumer_thread_pipe
[1]);
1071 ret
= close(ctx
->consumer_poll_pipe
[0]);
1075 ret
= close(ctx
->consumer_poll_pipe
[1]);
1079 ret
= close(ctx
->consumer_should_quit
[0]);
1083 ret
= close(ctx
->consumer_should_quit
[1]);
1087 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1089 unlink(ctx
->consumer_command_sock_path
);
1094 * Write the metadata stream id on the specified file descriptor.
1096 static int write_relayd_metadata_id(int fd
,
1097 struct lttng_consumer_stream
*stream
,
1098 struct consumer_relayd_sock_pair
*relayd
,
1099 unsigned long padding
)
1102 struct lttcomm_relayd_metadata_payload hdr
;
1104 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1105 hdr
.padding_size
= htobe32(padding
);
1107 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1108 } while (ret
< 0 && errno
== EINTR
);
1110 PERROR("write metadata stream id");
1113 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1114 stream
->relayd_stream_id
, padding
);
1121 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1122 * core function for writing trace buffers to either the local filesystem or
1125 * Careful review MUST be put if any changes occur!
1127 * Returns the number of bytes written
1129 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1130 struct lttng_consumer_local_data
*ctx
,
1131 struct lttng_consumer_stream
*stream
, unsigned long len
,
1132 unsigned long padding
)
1134 unsigned long mmap_offset
;
1135 ssize_t ret
= 0, written
= 0;
1136 off_t orig_offset
= stream
->out_fd_offset
;
1137 /* Default is on the disk */
1138 int outfd
= stream
->out_fd
;
1139 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1141 /* RCU lock for the relayd pointer */
1144 /* Flag that the current stream if set for network streaming. */
1145 if (stream
->net_seq_idx
!= -1) {
1146 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1147 if (relayd
== NULL
) {
1152 /* get the offset inside the fd to mmap */
1153 switch (consumer_data
.type
) {
1154 case LTTNG_CONSUMER_KERNEL
:
1155 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1157 case LTTNG_CONSUMER32_UST
:
1158 case LTTNG_CONSUMER64_UST
:
1159 ret
= lttng_ustctl_get_mmap_read_offset(stream
->chan
->handle
,
1160 stream
->buf
, &mmap_offset
);
1163 ERR("Unknown consumer_data type");
1168 PERROR("tracer ctl get_mmap_read_offset");
1173 /* Handle stream on the relayd if the output is on the network */
1175 unsigned long netlen
= len
;
1178 * Lock the control socket for the complete duration of the function
1179 * since from this point on we will use the socket.
1181 if (stream
->metadata_flag
) {
1182 /* Metadata requires the control socket. */
1183 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1184 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1187 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1189 /* Use the returned socket. */
1192 /* Write metadata stream id before payload */
1193 if (stream
->metadata_flag
) {
1194 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1201 /* Else, use the default set before which is the filesystem. */
1203 /* No streaming, we have to set the len with the full padding */
1209 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
1210 } while (ret
< 0 && errno
== EINTR
);
1211 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1213 PERROR("Error in file write");
1218 } else if (ret
> len
) {
1219 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1227 /* This call is useless on a socket so better save a syscall. */
1229 /* This won't block, but will start writeout asynchronously */
1230 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1231 SYNC_FILE_RANGE_WRITE
);
1232 stream
->out_fd_offset
+= ret
;
1236 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1239 /* Unlock only if ctrl socket used */
1240 if (relayd
&& stream
->metadata_flag
) {
1241 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1249 * Splice the data from the ring buffer to the tracefile.
1251 * Returns the number of bytes spliced.
1253 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1254 struct lttng_consumer_local_data
*ctx
,
1255 struct lttng_consumer_stream
*stream
, unsigned long len
,
1256 unsigned long padding
)
1258 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1260 off_t orig_offset
= stream
->out_fd_offset
;
1261 int fd
= stream
->wait_fd
;
1262 /* Default is on the disk */
1263 int outfd
= stream
->out_fd
;
1264 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1267 switch (consumer_data
.type
) {
1268 case LTTNG_CONSUMER_KERNEL
:
1270 case LTTNG_CONSUMER32_UST
:
1271 case LTTNG_CONSUMER64_UST
:
1272 /* Not supported for user space tracing */
1275 ERR("Unknown consumer_data type");
1279 /* RCU lock for the relayd pointer */
1282 /* Flag that the current stream if set for network streaming. */
1283 if (stream
->net_seq_idx
!= -1) {
1284 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1285 if (relayd
== NULL
) {
1291 * Choose right pipe for splice. Metadata and trace data are handled by
1292 * different threads hence the use of two pipes in order not to race or
1293 * corrupt the written data.
1295 if (stream
->metadata_flag
) {
1296 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1298 splice_pipe
= ctx
->consumer_thread_pipe
;
1301 /* Write metadata stream id before payload */
1303 int total_len
= len
;
1305 if (stream
->metadata_flag
) {
1307 * Lock the control socket for the complete duration of the function
1308 * since from this point on we will use the socket.
1310 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1312 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1319 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1322 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1324 /* Use the returned socket. */
1327 ERR("Remote relayd disconnected. Stopping");
1331 /* No streaming, we have to set the len with the full padding */
1336 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1337 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1338 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1339 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1340 DBG("splice chan to pipe, ret %zd", ret_splice
);
1341 if (ret_splice
< 0) {
1342 PERROR("Error in relay splice");
1344 written
= ret_splice
;
1350 /* Handle stream on the relayd if the output is on the network */
1352 if (stream
->metadata_flag
) {
1353 size_t metadata_payload_size
=
1354 sizeof(struct lttcomm_relayd_metadata_payload
);
1356 /* Update counter to fit the spliced data */
1357 ret_splice
+= metadata_payload_size
;
1358 len
+= metadata_payload_size
;
1360 * We do this so the return value can match the len passed as
1361 * argument to this function.
1363 written
-= metadata_payload_size
;
1367 /* Splice data out */
1368 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1369 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1370 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1371 if (ret_splice
< 0) {
1372 PERROR("Error in file splice");
1374 written
= ret_splice
;
1378 } else if (ret_splice
> len
) {
1380 PERROR("Wrote more data than requested %zd (len: %lu)",
1382 written
+= ret_splice
;
1388 /* This call is useless on a socket so better save a syscall. */
1390 /* This won't block, but will start writeout asynchronously */
1391 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1392 SYNC_FILE_RANGE_WRITE
);
1393 stream
->out_fd_offset
+= ret_splice
;
1395 written
+= ret_splice
;
1397 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1404 /* send the appropriate error description to sessiond */
1407 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EBADF
);
1410 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1413 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1416 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1421 if (relayd
&& stream
->metadata_flag
) {
1422 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1430 * Take a snapshot for a specific fd
1432 * Returns 0 on success, < 0 on error
1434 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1435 struct lttng_consumer_stream
*stream
)
1437 switch (consumer_data
.type
) {
1438 case LTTNG_CONSUMER_KERNEL
:
1439 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1440 case LTTNG_CONSUMER32_UST
:
1441 case LTTNG_CONSUMER64_UST
:
1442 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1444 ERR("Unknown consumer_data type");
1452 * Get the produced position
1454 * Returns 0 on success, < 0 on error
1456 int lttng_consumer_get_produced_snapshot(
1457 struct lttng_consumer_local_data
*ctx
,
1458 struct lttng_consumer_stream
*stream
,
1461 switch (consumer_data
.type
) {
1462 case LTTNG_CONSUMER_KERNEL
:
1463 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1464 case LTTNG_CONSUMER32_UST
:
1465 case LTTNG_CONSUMER64_UST
:
1466 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1468 ERR("Unknown consumer_data type");
1474 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1475 int sock
, struct pollfd
*consumer_sockpoll
)
1477 switch (consumer_data
.type
) {
1478 case LTTNG_CONSUMER_KERNEL
:
1479 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1480 case LTTNG_CONSUMER32_UST
:
1481 case LTTNG_CONSUMER64_UST
:
1482 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1484 ERR("Unknown consumer_data type");
1491 * Iterate over all stream element of the hashtable and free them. This is race
1492 * free since the hashtable received MUST be in a race free synchronization
1493 * state. It's the caller responsability to make sure of that.
1495 static void destroy_stream_ht(struct lttng_ht
*ht
)
1498 struct lttng_ht_iter iter
;
1499 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
);
1514 lttng_ht_destroy(ht
);
1518 * Clean up a metadata stream and free its memory.
1520 static void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
)
1523 struct lttng_consumer_channel
*free_chan
= NULL
;
1524 struct consumer_relayd_sock_pair
*relayd
;
1528 * This call should NEVER receive regular stream. It must always be
1529 * metadata stream and this is crucial for data structure synchronization.
1531 assert(stream
->metadata_flag
);
1533 pthread_mutex_lock(&consumer_data
.lock
);
1534 switch (consumer_data
.type
) {
1535 case LTTNG_CONSUMER_KERNEL
:
1536 if (stream
->mmap_base
!= NULL
) {
1537 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1539 PERROR("munmap metadata stream");
1543 case LTTNG_CONSUMER32_UST
:
1544 case LTTNG_CONSUMER64_UST
:
1545 lttng_ustconsumer_del_stream(stream
);
1548 ERR("Unknown consumer_data type");
1551 pthread_mutex_unlock(&consumer_data
.lock
);
1553 if (stream
->out_fd
>= 0) {
1554 ret
= close(stream
->out_fd
);
1560 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
1561 ret
= close(stream
->wait_fd
);
1567 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
1568 ret
= close(stream
->shm_fd
);
1574 /* Check and cleanup relayd */
1576 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1577 if (relayd
!= NULL
) {
1578 uatomic_dec(&relayd
->refcount
);
1579 assert(uatomic_read(&relayd
->refcount
) >= 0);
1581 /* Closing streams requires to lock the control socket. */
1582 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1583 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1584 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1585 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1587 DBG("Unable to close stream on the relayd. Continuing");
1589 * Continue here. There is nothing we can do for the relayd.
1590 * Chances are that the relayd has closed the socket so we just
1591 * continue cleaning up.
1595 /* Both conditions are met, we destroy the relayd. */
1596 if (uatomic_read(&relayd
->refcount
) == 0 &&
1597 uatomic_read(&relayd
->destroy_flag
)) {
1598 destroy_relayd(relayd
);
1603 /* Atomically decrement channel refcount since other threads can use it. */
1604 uatomic_dec(&stream
->chan
->refcount
);
1605 if (!uatomic_read(&stream
->chan
->refcount
)) {
1606 free_chan
= stream
->chan
;
1610 consumer_del_channel(free_chan
);
1617 * Action done with the metadata stream when adding it to the consumer internal
1618 * data structures to handle it.
1620 static void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
1622 struct consumer_relayd_sock_pair
*relayd
;
1624 /* Find relayd and, if one is found, increment refcount. */
1626 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1627 if (relayd
!= NULL
) {
1628 uatomic_inc(&relayd
->refcount
);
1634 * Thread polls on metadata file descriptor and write them on disk or on the
1637 void *lttng_consumer_thread_poll_metadata(void *data
)
1640 uint32_t revents
, nb_fd
;
1641 struct lttng_consumer_stream
*stream
;
1642 struct lttng_ht_iter iter
;
1643 struct lttng_ht_node_ulong
*node
;
1644 struct lttng_ht
*metadata_ht
= NULL
;
1645 struct lttng_poll_event events
;
1646 struct lttng_consumer_local_data
*ctx
= data
;
1649 rcu_register_thread();
1651 DBG("Thread metadata poll started");
1653 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1654 if (metadata_ht
== NULL
) {
1658 /* Size is set to 1 for the consumer_metadata pipe */
1659 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
1661 ERR("Poll set creation failed");
1665 ret
= lttng_poll_add(&events
, ctx
->consumer_metadata_pipe
[0], LPOLLIN
);
1671 DBG("Metadata main loop started");
1674 lttng_poll_reset(&events
);
1676 nb_fd
= LTTNG_POLL_GETNB(&events
);
1678 /* Only the metadata pipe is set */
1679 if (nb_fd
== 0 && consumer_quit
== 1) {
1684 DBG("Metadata poll wait with %d fd(s)", nb_fd
);
1685 ret
= lttng_poll_wait(&events
, -1);
1686 DBG("Metadata event catched in thread");
1688 if (errno
== EINTR
) {
1694 for (i
= 0; i
< nb_fd
; i
++) {
1695 revents
= LTTNG_POLL_GETEV(&events
, i
);
1696 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1698 /* Check the metadata pipe for incoming metadata. */
1699 if (pollfd
== ctx
->consumer_metadata_pipe
[0]) {
1700 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
1701 DBG("Metadata thread pipe hung up");
1703 * Remove the pipe from the poll set and continue the loop
1704 * since their might be data to consume.
1706 lttng_poll_del(&events
, ctx
->consumer_metadata_pipe
[0]);
1707 close(ctx
->consumer_metadata_pipe
[0]);
1709 } else if (revents
& LPOLLIN
) {
1710 stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
1711 if (stream
== NULL
) {
1712 PERROR("zmalloc metadata consumer stream");
1717 /* Get the stream and add it to the local hash table */
1718 ret
= read(pollfd
, stream
,
1719 sizeof(struct lttng_consumer_stream
));
1720 } while (ret
< 0 && errno
== EINTR
);
1721 if (ret
< 0 || ret
< sizeof(struct lttng_consumer_stream
)) {
1722 PERROR("read metadata stream");
1725 * Let's continue here and hope we can still work
1726 * without stopping the consumer. XXX: Should we?
1731 DBG("Adding metadata stream %d to poll set",
1735 /* The node should be init at this point */
1736 lttng_ht_add_unique_ulong(metadata_ht
,
1737 &stream
->waitfd_node
);
1740 /* Add metadata stream to the global poll events list */
1741 lttng_poll_add(&events
, stream
->wait_fd
,
1742 LPOLLIN
| LPOLLPRI
);
1744 consumer_add_metadata_stream(stream
);
1747 /* Metadata pipe handled. Continue handling the others */
1751 /* From here, the event is a metadata wait fd */
1754 lttng_ht_lookup(metadata_ht
, (void *)((unsigned long) pollfd
),
1756 node
= lttng_ht_iter_get_node_ulong(&iter
);
1758 /* FD not found, continue loop */
1763 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
1766 /* Get the data out of the metadata file descriptor */
1767 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
1768 DBG("Metadata available on fd %d", pollfd
);
1769 assert(stream
->wait_fd
== pollfd
);
1771 len
= ctx
->on_buffer_ready(stream
, ctx
);
1772 /* It's ok to have an unavailable sub-buffer */
1773 if (len
< 0 && len
!= -EAGAIN
) {
1776 } else if (len
> 0) {
1777 stream
->data_read
= 1;
1782 * Remove the stream from the hash table since there is no data
1783 * left on the fd because we previously did a read on the buffer.
1785 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
1786 DBG("Metadata fd %d is hup|err|nval.", pollfd
);
1787 if (!stream
->hangup_flush_done
1788 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1789 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1790 DBG("Attempting to flush and consume the UST buffers");
1791 lttng_ustconsumer_on_stream_hangup(stream
);
1793 /* We just flushed the stream now read it. */
1794 len
= ctx
->on_buffer_ready(stream
, ctx
);
1795 /* It's ok to have an unavailable sub-buffer */
1796 if (len
< 0 && len
!= -EAGAIN
) {
1802 /* Removing it from hash table, poll set and free memory */
1803 lttng_ht_del(metadata_ht
, &iter
);
1805 lttng_poll_del(&events
, stream
->wait_fd
);
1806 consumer_del_metadata_stream(stream
);
1814 DBG("Metadata poll thread exiting");
1815 lttng_poll_clean(&events
);
1818 destroy_stream_ht(metadata_ht
);
1821 rcu_unregister_thread();
1826 * This thread polls the fds in the set to consume the data and write
1827 * it to tracefile if necessary.
1829 void *lttng_consumer_thread_poll_fds(void *data
)
1831 int num_rdy
, num_hup
, high_prio
, ret
, i
;
1832 struct pollfd
*pollfd
= NULL
;
1833 /* local view of the streams */
1834 struct lttng_consumer_stream
**local_stream
= NULL
;
1835 /* local view of consumer_data.fds_count */
1837 struct lttng_consumer_local_data
*ctx
= data
;
1839 pthread_t metadata_thread
;
1842 rcu_register_thread();
1844 /* Start metadata polling thread */
1845 ret
= pthread_create(&metadata_thread
, NULL
,
1846 lttng_consumer_thread_poll_metadata
, (void *) ctx
);
1848 PERROR("pthread_create metadata thread");
1852 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
1859 * the fds set has been updated, we need to update our
1860 * local array as well
1862 pthread_mutex_lock(&consumer_data
.lock
);
1863 if (consumer_data
.need_update
) {
1864 if (pollfd
!= NULL
) {
1868 if (local_stream
!= NULL
) {
1870 local_stream
= NULL
;
1873 /* allocate for all fds + 1 for the consumer_poll_pipe */
1874 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
1875 if (pollfd
== NULL
) {
1876 perror("pollfd malloc");
1877 pthread_mutex_unlock(&consumer_data
.lock
);
1881 /* allocate for all fds + 1 for the consumer_poll_pipe */
1882 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
1883 sizeof(struct lttng_consumer_stream
));
1884 if (local_stream
== NULL
) {
1885 perror("local_stream malloc");
1886 pthread_mutex_unlock(&consumer_data
.lock
);
1889 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
);
1891 ERR("Error in allocating pollfd or local_outfds");
1892 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
1893 pthread_mutex_unlock(&consumer_data
.lock
);
1897 consumer_data
.need_update
= 0;
1899 pthread_mutex_unlock(&consumer_data
.lock
);
1901 /* No FDs and consumer_quit, consumer_cleanup the thread */
1902 if (nb_fd
== 0 && consumer_quit
== 1) {
1905 /* poll on the array of fds */
1907 DBG("polling on %d fd", nb_fd
+ 1);
1908 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
1909 DBG("poll num_rdy : %d", num_rdy
);
1910 if (num_rdy
== -1) {
1912 * Restart interrupted system call.
1914 if (errno
== EINTR
) {
1917 perror("Poll error");
1918 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
1920 } else if (num_rdy
== 0) {
1921 DBG("Polling thread timed out");
1926 * If the consumer_poll_pipe triggered poll go directly to the
1927 * beginning of the loop to update the array. We want to prioritize
1928 * array update over low-priority reads.
1930 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
1931 size_t pipe_readlen
;
1934 DBG("consumer_poll_pipe wake up");
1935 /* Consume 1 byte of pipe data */
1937 pipe_readlen
= read(ctx
->consumer_poll_pipe
[0], &tmp
, 1);
1938 } while (pipe_readlen
== -1 && errno
== EINTR
);
1942 /* Take care of high priority channels first. */
1943 for (i
= 0; i
< nb_fd
; i
++) {
1944 if (pollfd
[i
].revents
& POLLPRI
) {
1945 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
1947 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1948 /* it's ok to have an unavailable sub-buffer */
1949 if (len
< 0 && len
!= -EAGAIN
) {
1951 } else if (len
> 0) {
1952 local_stream
[i
]->data_read
= 1;
1958 * If we read high prio channel in this loop, try again
1959 * for more high prio data.
1965 /* Take care of low priority channels. */
1966 for (i
= 0; i
< nb_fd
; i
++) {
1967 if ((pollfd
[i
].revents
& POLLIN
) ||
1968 local_stream
[i
]->hangup_flush_done
) {
1969 DBG("Normal read on fd %d", pollfd
[i
].fd
);
1970 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1971 /* it's ok to have an unavailable sub-buffer */
1972 if (len
< 0 && len
!= -EAGAIN
) {
1974 } else if (len
> 0) {
1975 local_stream
[i
]->data_read
= 1;
1980 /* Handle hangup and errors */
1981 for (i
= 0; i
< nb_fd
; i
++) {
1982 if (!local_stream
[i
]->hangup_flush_done
1983 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
1984 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1985 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1986 DBG("fd %d is hup|err|nval. Attempting flush and read.",
1988 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
1989 /* Attempt read again, for the data we just flushed. */
1990 local_stream
[i
]->data_read
= 1;
1993 * If the poll flag is HUP/ERR/NVAL and we have
1994 * read no data in this pass, we can remove the
1995 * stream from its hash table.
1997 if ((pollfd
[i
].revents
& POLLHUP
)) {
1998 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
1999 if (!local_stream
[i
]->data_read
) {
2000 consumer_del_stream(local_stream
[i
]);
2003 } else if (pollfd
[i
].revents
& POLLERR
) {
2004 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2005 if (!local_stream
[i
]->data_read
) {
2006 consumer_del_stream(local_stream
[i
]);
2009 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2010 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2011 if (!local_stream
[i
]->data_read
) {
2012 consumer_del_stream(local_stream
[i
]);
2016 local_stream
[i
]->data_read
= 0;
2020 DBG("polling thread exiting");
2021 if (pollfd
!= NULL
) {
2025 if (local_stream
!= NULL
) {
2027 local_stream
= NULL
;
2031 * Close the write side of the pipe so epoll_wait() in
2032 * lttng_consumer_thread_poll_metadata can catch it. The thread is
2033 * monitoring the read side of the pipe. If we close them both, epoll_wait
2034 * strangely does not return and could create a endless wait period if the
2035 * pipe is the only tracked fd in the poll set. The thread will take care
2036 * of closing the read side.
2038 close(ctx
->consumer_metadata_pipe
[1]);
2040 ret
= pthread_join(metadata_thread
, &status
);
2042 PERROR("pthread_join metadata thread");
2046 rcu_unregister_thread();
2051 * This thread listens on the consumerd socket and receives the file
2052 * descriptors from the session daemon.
2054 void *lttng_consumer_thread_receive_fds(void *data
)
2056 int sock
, client_socket
, ret
;
2058 * structure to poll for incoming data on communication socket avoids
2059 * making blocking sockets.
2061 struct pollfd consumer_sockpoll
[2];
2062 struct lttng_consumer_local_data
*ctx
= data
;
2064 rcu_register_thread();
2066 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2067 unlink(ctx
->consumer_command_sock_path
);
2068 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2069 if (client_socket
< 0) {
2070 ERR("Cannot create command socket");
2074 ret
= lttcomm_listen_unix_sock(client_socket
);
2079 DBG("Sending ready command to lttng-sessiond");
2080 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2081 /* return < 0 on error, but == 0 is not fatal */
2083 ERR("Error sending ready command to lttng-sessiond");
2087 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2089 perror("fcntl O_NONBLOCK");
2093 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2094 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2095 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2096 consumer_sockpoll
[1].fd
= client_socket
;
2097 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2099 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2102 DBG("Connection on client_socket");
2104 /* Blocking call, waiting for transmission */
2105 sock
= lttcomm_accept_unix_sock(client_socket
);
2110 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2112 perror("fcntl O_NONBLOCK");
2116 /* update the polling structure to poll on the established socket */
2117 consumer_sockpoll
[1].fd
= sock
;
2118 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2121 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2124 DBG("Incoming command on sock");
2125 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2126 if (ret
== -ENOENT
) {
2127 DBG("Received STOP command");
2132 * This could simply be a session daemon quitting. Don't output
2135 DBG("Communication interrupted on command socket");
2138 if (consumer_quit
) {
2139 DBG("consumer_thread_receive_fds received quit from signal");
2142 DBG("received fds on sock");
2145 DBG("consumer_thread_receive_fds exiting");
2148 * when all fds have hung up, the polling thread
2154 * 2s of grace period, if no polling events occur during
2155 * this period, the polling thread will exit even if there
2156 * are still open FDs (should not happen, but safety mechanism).
2158 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
2161 * Wake-up the other end by writing a null byte in the pipe
2162 * (non-blocking). Important note: Because writing into the
2163 * pipe is non-blocking (and therefore we allow dropping wakeup
2164 * data, as long as there is wakeup data present in the pipe
2165 * buffer to wake up the other end), the other end should
2166 * perform the following sequence for waiting:
2167 * 1) empty the pipe (reads).
2168 * 2) perform update operation.
2169 * 3) wait on the pipe (poll).
2172 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
2173 } while (ret
< 0 && errno
== EINTR
);
2174 rcu_unregister_thread();
2178 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2179 struct lttng_consumer_local_data
*ctx
)
2181 switch (consumer_data
.type
) {
2182 case LTTNG_CONSUMER_KERNEL
:
2183 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
2184 case LTTNG_CONSUMER32_UST
:
2185 case LTTNG_CONSUMER64_UST
:
2186 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2188 ERR("Unknown consumer_data type");
2194 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2196 switch (consumer_data
.type
) {
2197 case LTTNG_CONSUMER_KERNEL
:
2198 return lttng_kconsumer_on_recv_stream(stream
);
2199 case LTTNG_CONSUMER32_UST
:
2200 case LTTNG_CONSUMER64_UST
:
2201 return lttng_ustconsumer_on_recv_stream(stream
);
2203 ERR("Unknown consumer_data type");
2210 * Allocate and set consumer data hash tables.
2212 void lttng_consumer_init(void)
2214 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2215 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2216 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2220 * Process the ADD_RELAYD command receive by a consumer.
2222 * This will create a relayd socket pair and add it to the relayd hash table.
2223 * The caller MUST acquire a RCU read side lock before calling it.
2225 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
2226 struct lttng_consumer_local_data
*ctx
, int sock
,
2227 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
)
2230 struct consumer_relayd_sock_pair
*relayd
;
2232 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
2234 /* Get relayd reference if exists. */
2235 relayd
= consumer_find_relayd(net_seq_idx
);
2236 if (relayd
== NULL
) {
2237 /* Not found. Allocate one. */
2238 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
2239 if (relayd
== NULL
) {
2240 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
2245 /* Poll on consumer socket. */
2246 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2251 /* Get relayd socket from session daemon */
2252 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2253 if (ret
!= sizeof(fd
)) {
2254 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
2259 /* Copy socket information and received FD */
2260 switch (sock_type
) {
2261 case LTTNG_STREAM_CONTROL
:
2262 /* Copy received lttcomm socket */
2263 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
2264 ret
= lttcomm_create_sock(&relayd
->control_sock
);
2269 /* Close the created socket fd which is useless */
2270 close(relayd
->control_sock
.fd
);
2272 /* Assign new file descriptor */
2273 relayd
->control_sock
.fd
= fd
;
2275 case LTTNG_STREAM_DATA
:
2276 /* Copy received lttcomm socket */
2277 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
2278 ret
= lttcomm_create_sock(&relayd
->data_sock
);
2283 /* Close the created socket fd which is useless */
2284 close(relayd
->data_sock
.fd
);
2286 /* Assign new file descriptor */
2287 relayd
->data_sock
.fd
= fd
;
2290 ERR("Unknown relayd socket type (%d)", sock_type
);
2294 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2295 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
2296 relayd
->net_seq_idx
, fd
);
2299 * Add relayd socket pair to consumer data hashtable. If object already
2300 * exists or on error, the function gracefully returns.