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>
31 #include <common/common.h>
32 #include <common/kernel-ctl/kernel-ctl.h>
33 #include <common/sessiond-comm/relayd.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/kernel-consumer/kernel-consumer.h>
36 #include <common/relayd/relayd.h>
37 #include <common/ust-consumer/ust-consumer.h>
41 struct lttng_consumer_global_data consumer_data
= {
44 .type
= LTTNG_CONSUMER_UNKNOWN
,
47 /* timeout parameter, to control the polling thread grace period. */
48 int consumer_poll_timeout
= -1;
51 * Flag to inform the polling thread to quit when all fd hung up. Updated by
52 * the consumer_thread_receive_fds when it notices that all fds has hung up.
53 * Also updated by the signal handler (consumer_should_exit()). Read by the
56 volatile int consumer_quit
= 0;
59 * Find a stream. The consumer_data.lock must be locked during this
62 static struct lttng_consumer_stream
*consumer_find_stream(int key
)
64 struct lttng_ht_iter iter
;
65 struct lttng_ht_node_ulong
*node
;
66 struct lttng_consumer_stream
*stream
= NULL
;
68 /* Negative keys are lookup failures */
74 lttng_ht_lookup(consumer_data
.stream_ht
, (void *)((unsigned long) key
),
76 node
= lttng_ht_iter_get_node_ulong(&iter
);
78 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
86 static void consumer_steal_stream_key(int key
)
88 struct lttng_consumer_stream
*stream
;
91 stream
= consumer_find_stream(key
);
95 * We don't want the lookup to match, but we still need
96 * to iterate on this stream when iterating over the hash table. Just
97 * change the node key.
99 stream
->node
.key
= -1;
104 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
106 struct lttng_ht_iter iter
;
107 struct lttng_ht_node_ulong
*node
;
108 struct lttng_consumer_channel
*channel
= NULL
;
110 /* Negative keys are lookup failures */
116 lttng_ht_lookup(consumer_data
.channel_ht
, (void *)((unsigned long) key
),
118 node
= lttng_ht_iter_get_node_ulong(&iter
);
120 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
128 static void consumer_steal_channel_key(int key
)
130 struct lttng_consumer_channel
*channel
;
133 channel
= consumer_find_channel(key
);
137 * We don't want the lookup to match, but we still need
138 * to iterate on this channel when iterating over the hash table. Just
139 * change the node key.
141 channel
->node
.key
= -1;
147 void consumer_free_stream(struct rcu_head
*head
)
149 struct lttng_ht_node_ulong
*node
=
150 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
151 struct lttng_consumer_stream
*stream
=
152 caa_container_of(node
, struct lttng_consumer_stream
, node
);
158 * RCU protected relayd socket pair free.
160 static void consumer_rcu_free_relayd(struct rcu_head
*head
)
162 struct lttng_ht_node_ulong
*node
=
163 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
164 struct consumer_relayd_sock_pair
*relayd
=
165 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
171 * Destroy and free relayd socket pair object.
173 * This function MUST be called with the consumer_data lock acquired.
175 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
178 struct lttng_ht_iter iter
;
180 DBG("Consumer destroy and close relayd socket pair");
182 iter
.iter
.node
= &relayd
->node
.node
;
183 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
186 /* Close all sockets */
187 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
188 (void) relayd_close(&relayd
->control_sock
);
189 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
190 (void) relayd_close(&relayd
->data_sock
);
192 /* RCU free() call */
193 call_rcu(&relayd
->node
.head
, consumer_rcu_free_relayd
);
197 * Remove a stream from the global list protected by a mutex. This
198 * function is also responsible for freeing its data structures.
200 void consumer_del_stream(struct lttng_consumer_stream
*stream
)
203 struct lttng_ht_iter iter
;
204 struct lttng_consumer_channel
*free_chan
= NULL
;
205 struct consumer_relayd_sock_pair
*relayd
;
209 pthread_mutex_lock(&consumer_data
.lock
);
211 switch (consumer_data
.type
) {
212 case LTTNG_CONSUMER_KERNEL
:
213 if (stream
->mmap_base
!= NULL
) {
214 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
220 case LTTNG_CONSUMER32_UST
:
221 case LTTNG_CONSUMER64_UST
:
222 lttng_ustconsumer_del_stream(stream
);
225 ERR("Unknown consumer_data type");
231 iter
.iter
.node
= &stream
->node
.node
;
232 ret
= lttng_ht_del(consumer_data
.stream_ht
, &iter
);
237 if (consumer_data
.stream_count
<= 0) {
240 consumer_data
.stream_count
--;
244 if (stream
->out_fd
>= 0) {
245 ret
= close(stream
->out_fd
);
250 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
251 ret
= close(stream
->wait_fd
);
256 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
257 ret
= close(stream
->shm_fd
);
263 /* Check and cleanup relayd */
264 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
265 if (relayd
!= NULL
) {
266 /* We are about to modify the relayd refcount */
268 if (!--relayd
->refcount
) {
269 /* Refcount of the relayd struct is 0, destroy it */
270 consumer_destroy_relayd(relayd
);
275 if (!--stream
->chan
->refcount
) {
276 free_chan
= stream
->chan
;
280 call_rcu(&stream
->node
.head
, consumer_free_stream
);
282 consumer_data
.need_update
= 1;
283 pthread_mutex_unlock(&consumer_data
.lock
);
286 consumer_del_channel(free_chan
);
289 struct lttng_consumer_stream
*consumer_allocate_stream(
290 int channel_key
, int stream_key
,
291 int shm_fd
, int wait_fd
,
292 enum lttng_consumer_stream_state state
,
294 enum lttng_event_output output
,
295 const char *path_name
,
301 struct lttng_consumer_stream
*stream
;
304 stream
= zmalloc(sizeof(*stream
));
305 if (stream
== NULL
) {
306 perror("malloc struct lttng_consumer_stream");
309 stream
->chan
= consumer_find_channel(channel_key
);
311 perror("Unable to find channel key");
314 stream
->chan
->refcount
++;
315 stream
->key
= stream_key
;
316 stream
->shm_fd
= shm_fd
;
317 stream
->wait_fd
= wait_fd
;
319 stream
->out_fd_offset
= 0;
320 stream
->state
= state
;
321 stream
->mmap_len
= mmap_len
;
322 stream
->mmap_base
= NULL
;
323 stream
->output
= output
;
326 stream
->net_seq_idx
= net_index
;
327 stream
->metadata_flag
= metadata_flag
;
328 strncpy(stream
->path_name
, path_name
, sizeof(stream
->path_name
));
329 stream
->path_name
[sizeof(stream
->path_name
) - 1] = '\0';
330 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
331 lttng_ht_node_init_ulong(&stream
->waitfd_node
, stream
->wait_fd
);
333 switch (consumer_data
.type
) {
334 case LTTNG_CONSUMER_KERNEL
:
336 case LTTNG_CONSUMER32_UST
:
337 case LTTNG_CONSUMER64_UST
:
338 stream
->cpu
= stream
->chan
->cpucount
++;
339 ret
= lttng_ustconsumer_allocate_stream(stream
);
346 ERR("Unknown consumer_data type");
350 DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d, net_seq_idx %d)",
351 stream
->path_name
, stream
->key
,
354 (unsigned long long) stream
->mmap_len
,
356 stream
->net_seq_idx
);
362 * Add a stream to the global list protected by a mutex.
364 int consumer_add_stream(struct lttng_consumer_stream
*stream
)
367 struct lttng_ht_node_ulong
*node
;
368 struct lttng_ht_iter iter
;
369 struct consumer_relayd_sock_pair
*relayd
;
371 pthread_mutex_lock(&consumer_data
.lock
);
372 /* Steal stream identifier, for UST */
373 consumer_steal_stream_key(stream
->key
);
376 lttng_ht_lookup(consumer_data
.stream_ht
,
377 (void *)((unsigned long) stream
->key
), &iter
);
378 node
= lttng_ht_iter_get_node_ulong(&iter
);
381 /* Stream already exist. Ignore the insertion */
385 lttng_ht_add_unique_ulong(consumer_data
.stream_ht
, &stream
->node
);
388 /* Check and cleanup relayd */
389 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
390 if (relayd
!= NULL
) {
391 /* We are about to modify the relayd refcount */
397 /* Update consumer data */
398 consumer_data
.stream_count
++;
399 consumer_data
.need_update
= 1;
401 switch (consumer_data
.type
) {
402 case LTTNG_CONSUMER_KERNEL
:
404 case LTTNG_CONSUMER32_UST
:
405 case LTTNG_CONSUMER64_UST
:
406 /* Streams are in CPU number order (we rely on this) */
407 stream
->cpu
= stream
->chan
->nr_streams
++;
410 ERR("Unknown consumer_data type");
416 pthread_mutex_unlock(&consumer_data
.lock
);
422 * Add relayd socket to global consumer data hashtable.
424 int consumer_add_relayd(struct consumer_relayd_sock_pair
*relayd
)
427 struct lttng_ht_node_ulong
*node
;
428 struct lttng_ht_iter iter
;
430 if (relayd
== NULL
) {
437 lttng_ht_lookup(consumer_data
.relayd_ht
,
438 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
439 node
= lttng_ht_iter_get_node_ulong(&iter
);
442 /* Relayd already exist. Ignore the insertion */
445 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
454 * Allocate and return a consumer relayd socket.
456 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
459 struct consumer_relayd_sock_pair
*obj
= NULL
;
461 /* Negative net sequence index is a failure */
462 if (net_seq_idx
< 0) {
466 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
468 PERROR("zmalloc relayd sock");
472 obj
->net_seq_idx
= net_seq_idx
;
474 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
475 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
482 * Find a relayd socket pair in the global consumer data.
484 * Return the object if found else NULL.
486 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
488 struct lttng_ht_iter iter
;
489 struct lttng_ht_node_ulong
*node
;
490 struct consumer_relayd_sock_pair
*relayd
= NULL
;
492 /* Negative keys are lookup failures */
499 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
501 node
= lttng_ht_iter_get_node_ulong(&iter
);
503 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
513 * Handle stream for relayd transmission if the stream applies for network
514 * streaming where the net sequence index is set.
516 * Return destination file descriptor or negative value on error.
518 int consumer_handle_stream_before_relayd(struct lttng_consumer_stream
*stream
,
522 struct consumer_relayd_sock_pair
*relayd
;
523 struct lttcomm_relayd_data_hdr data_hdr
;
528 /* Reset data header */
529 memset(&data_hdr
, 0, sizeof(data_hdr
));
531 /* Get relayd reference of the stream. */
532 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
533 if (relayd
== NULL
) {
534 /* Stream is either local or corrupted */
538 DBG("Consumer found relayd socks with index %d", stream
->net_seq_idx
);
539 if (stream
->metadata_flag
) {
540 /* Caller MUST acquire the relayd control socket lock */
541 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
546 /* Metadata are always sent on the control socket. */
547 outfd
= relayd
->control_sock
.fd
;
549 /* Set header with stream information */
550 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
551 data_hdr
.data_size
= htobe32(data_size
);
552 /* Other fields are zeroed previously */
554 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
560 /* Set to go on data socket */
561 outfd
= relayd
->data_sock
.fd
;
569 * Update a stream according to what we just received.
571 void consumer_change_stream_state(int stream_key
,
572 enum lttng_consumer_stream_state state
)
574 struct lttng_consumer_stream
*stream
;
576 pthread_mutex_lock(&consumer_data
.lock
);
577 stream
= consumer_find_stream(stream_key
);
579 stream
->state
= state
;
581 consumer_data
.need_update
= 1;
582 pthread_mutex_unlock(&consumer_data
.lock
);
586 void consumer_free_channel(struct rcu_head
*head
)
588 struct lttng_ht_node_ulong
*node
=
589 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
590 struct lttng_consumer_channel
*channel
=
591 caa_container_of(node
, struct lttng_consumer_channel
, node
);
597 * Remove a channel from the global list protected by a mutex. This
598 * function is also responsible for freeing its data structures.
600 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
603 struct lttng_ht_iter iter
;
605 pthread_mutex_lock(&consumer_data
.lock
);
607 switch (consumer_data
.type
) {
608 case LTTNG_CONSUMER_KERNEL
:
610 case LTTNG_CONSUMER32_UST
:
611 case LTTNG_CONSUMER64_UST
:
612 lttng_ustconsumer_del_channel(channel
);
615 ERR("Unknown consumer_data type");
621 iter
.iter
.node
= &channel
->node
.node
;
622 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
626 if (channel
->mmap_base
!= NULL
) {
627 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
632 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
633 ret
= close(channel
->wait_fd
);
638 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
639 ret
= close(channel
->shm_fd
);
645 call_rcu(&channel
->node
.head
, consumer_free_channel
);
647 pthread_mutex_unlock(&consumer_data
.lock
);
650 struct lttng_consumer_channel
*consumer_allocate_channel(
652 int shm_fd
, int wait_fd
,
654 uint64_t max_sb_size
)
656 struct lttng_consumer_channel
*channel
;
659 channel
= zmalloc(sizeof(*channel
));
660 if (channel
== NULL
) {
661 perror("malloc struct lttng_consumer_channel");
664 channel
->key
= channel_key
;
665 channel
->shm_fd
= shm_fd
;
666 channel
->wait_fd
= wait_fd
;
667 channel
->mmap_len
= mmap_len
;
668 channel
->max_sb_size
= max_sb_size
;
669 channel
->refcount
= 0;
670 channel
->nr_streams
= 0;
671 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
673 switch (consumer_data
.type
) {
674 case LTTNG_CONSUMER_KERNEL
:
675 channel
->mmap_base
= NULL
;
676 channel
->mmap_len
= 0;
678 case LTTNG_CONSUMER32_UST
:
679 case LTTNG_CONSUMER64_UST
:
680 ret
= lttng_ustconsumer_allocate_channel(channel
);
687 ERR("Unknown consumer_data type");
691 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
692 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
693 (unsigned long long) channel
->mmap_len
,
694 (unsigned long long) channel
->max_sb_size
);
700 * Add a channel to the global list protected by a mutex.
702 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
704 struct lttng_ht_node_ulong
*node
;
705 struct lttng_ht_iter iter
;
707 pthread_mutex_lock(&consumer_data
.lock
);
708 /* Steal channel identifier, for UST */
709 consumer_steal_channel_key(channel
->key
);
712 lttng_ht_lookup(consumer_data
.channel_ht
,
713 (void *)((unsigned long) channel
->key
), &iter
);
714 node
= lttng_ht_iter_get_node_ulong(&iter
);
716 /* Channel already exist. Ignore the insertion */
720 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
724 pthread_mutex_unlock(&consumer_data
.lock
);
730 * Allocate the pollfd structure and the local view of the out fds to avoid
731 * doing a lookup in the linked list and concurrency issues when writing is
732 * needed. Called with consumer_data.lock held.
734 * Returns the number of fds in the structures.
736 int consumer_update_poll_array(
737 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
738 struct lttng_consumer_stream
**local_stream
,
739 struct lttng_ht
*metadata_ht
)
742 struct lttng_ht_iter iter
;
743 struct lttng_consumer_stream
*stream
;
745 DBG("Updating poll fd array");
747 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, stream
,
749 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
752 DBG("Active FD %d", stream
->wait_fd
);
753 (*pollfd
)[i
].fd
= stream
->wait_fd
;
754 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
755 if (stream
->metadata_flag
&& metadata_ht
) {
756 lttng_ht_add_unique_ulong(metadata_ht
, &stream
->waitfd_node
);
757 DBG("Active FD added to metadata hash table");
759 local_stream
[i
] = stream
;
765 * Insert the consumer_poll_pipe at the end of the array and don't
766 * increment i so nb_fd is the number of real FD.
768 (*pollfd
)[i
].fd
= ctx
->consumer_poll_pipe
[0];
769 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
774 * Poll on the should_quit pipe and the command socket return -1 on error and
775 * should exit, 0 if data is available on the command socket
777 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
782 num_rdy
= poll(consumer_sockpoll
, 2, -1);
785 * Restart interrupted system call.
787 if (errno
== EINTR
) {
790 perror("Poll error");
793 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
794 DBG("consumer_should_quit wake up");
804 * Set the error socket.
806 void lttng_consumer_set_error_sock(
807 struct lttng_consumer_local_data
*ctx
, int sock
)
809 ctx
->consumer_error_socket
= sock
;
813 * Set the command socket path.
815 void lttng_consumer_set_command_sock_path(
816 struct lttng_consumer_local_data
*ctx
, char *sock
)
818 ctx
->consumer_command_sock_path
= sock
;
822 * Send return code to the session daemon.
823 * If the socket is not defined, we return 0, it is not a fatal error
825 int lttng_consumer_send_error(
826 struct lttng_consumer_local_data
*ctx
, int cmd
)
828 if (ctx
->consumer_error_socket
> 0) {
829 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
830 sizeof(enum lttcomm_sessiond_command
));
837 * Close all the tracefiles and stream fds, should be called when all instances
840 void lttng_consumer_cleanup(void)
842 struct lttng_ht_iter iter
;
843 struct lttng_ht_node_ulong
*node
;
848 * close all outfd. Called when there are no more threads running (after
849 * joining on the threads), no need to protect list iteration with mutex.
851 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, node
,
853 struct lttng_consumer_stream
*stream
=
854 caa_container_of(node
, struct lttng_consumer_stream
, node
);
855 consumer_del_stream(stream
);
858 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
860 struct lttng_consumer_channel
*channel
=
861 caa_container_of(node
, struct lttng_consumer_channel
, node
);
862 consumer_del_channel(channel
);
867 lttng_ht_destroy(consumer_data
.stream_ht
);
868 lttng_ht_destroy(consumer_data
.channel_ht
);
872 * Called from signal handler.
874 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
878 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
880 perror("write consumer quit");
884 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
887 int outfd
= stream
->out_fd
;
890 * This does a blocking write-and-wait on any page that belongs to the
891 * subbuffer prior to the one we just wrote.
892 * Don't care about error values, as these are just hints and ways to
893 * limit the amount of page cache used.
895 if (orig_offset
< stream
->chan
->max_sb_size
) {
898 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
899 stream
->chan
->max_sb_size
,
900 SYNC_FILE_RANGE_WAIT_BEFORE
901 | SYNC_FILE_RANGE_WRITE
902 | SYNC_FILE_RANGE_WAIT_AFTER
);
904 * Give hints to the kernel about how we access the file:
905 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
908 * We need to call fadvise again after the file grows because the
909 * kernel does not seem to apply fadvise to non-existing parts of the
912 * Call fadvise _after_ having waited for the page writeback to
913 * complete because the dirty page writeback semantic is not well
914 * defined. So it can be expected to lead to lower throughput in
917 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
918 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
922 * Initialise the necessary environnement :
923 * - create a new context
924 * - create the poll_pipe
925 * - create the should_quit pipe (for signal handler)
926 * - create the thread pipe (for splice)
928 * Takes a function pointer as argument, this function is called when data is
929 * available on a buffer. This function is responsible to do the
930 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
931 * buffer configuration and then kernctl_put_next_subbuf at the end.
933 * Returns a pointer to the new context or NULL on error.
935 struct lttng_consumer_local_data
*lttng_consumer_create(
936 enum lttng_consumer_type type
,
937 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
938 struct lttng_consumer_local_data
*ctx
),
939 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
940 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
941 int (*update_stream
)(int stream_key
, uint32_t state
))
944 struct lttng_consumer_local_data
*ctx
;
946 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
947 consumer_data
.type
== type
);
948 consumer_data
.type
= type
;
950 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
952 perror("allocating context");
956 ctx
->consumer_error_socket
= -1;
957 /* assign the callbacks */
958 ctx
->on_buffer_ready
= buffer_ready
;
959 ctx
->on_recv_channel
= recv_channel
;
960 ctx
->on_recv_stream
= recv_stream
;
961 ctx
->on_update_stream
= update_stream
;
963 ret
= pipe(ctx
->consumer_poll_pipe
);
965 perror("Error creating poll pipe");
966 goto error_poll_pipe
;
969 /* set read end of the pipe to non-blocking */
970 ret
= fcntl(ctx
->consumer_poll_pipe
[0], F_SETFL
, O_NONBLOCK
);
972 perror("fcntl O_NONBLOCK");
973 goto error_poll_fcntl
;
976 /* set write end of the pipe to non-blocking */
977 ret
= fcntl(ctx
->consumer_poll_pipe
[1], F_SETFL
, O_NONBLOCK
);
979 perror("fcntl O_NONBLOCK");
980 goto error_poll_fcntl
;
983 ret
= pipe(ctx
->consumer_should_quit
);
985 perror("Error creating recv pipe");
986 goto error_quit_pipe
;
989 ret
= pipe(ctx
->consumer_thread_pipe
);
991 perror("Error creating thread pipe");
992 goto error_thread_pipe
;
999 for (i
= 0; i
< 2; i
++) {
1002 err
= close(ctx
->consumer_should_quit
[i
]);
1009 for (i
= 0; i
< 2; i
++) {
1012 err
= close(ctx
->consumer_poll_pipe
[i
]);
1024 * Close all fds associated with the instance and free the context.
1026 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1030 ret
= close(ctx
->consumer_error_socket
);
1034 ret
= close(ctx
->consumer_thread_pipe
[0]);
1038 ret
= close(ctx
->consumer_thread_pipe
[1]);
1042 ret
= close(ctx
->consumer_poll_pipe
[0]);
1046 ret
= close(ctx
->consumer_poll_pipe
[1]);
1050 ret
= close(ctx
->consumer_should_quit
[0]);
1054 ret
= close(ctx
->consumer_should_quit
[1]);
1058 unlink(ctx
->consumer_command_sock_path
);
1063 * Mmap the ring buffer, read it and write the data to the tracefile.
1065 * Returns the number of bytes written
1067 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1068 struct lttng_consumer_local_data
*ctx
,
1069 struct lttng_consumer_stream
*stream
, unsigned long len
)
1071 switch (consumer_data
.type
) {
1072 case LTTNG_CONSUMER_KERNEL
:
1073 return lttng_kconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
1074 case LTTNG_CONSUMER32_UST
:
1075 case LTTNG_CONSUMER64_UST
:
1076 return lttng_ustconsumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
1078 ERR("Unknown consumer_data type");
1086 * Splice the data from the ring buffer to the tracefile.
1088 * Returns the number of bytes spliced.
1090 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1091 struct lttng_consumer_local_data
*ctx
,
1092 struct lttng_consumer_stream
*stream
, unsigned long len
)
1094 switch (consumer_data
.type
) {
1095 case LTTNG_CONSUMER_KERNEL
:
1096 return lttng_kconsumer_on_read_subbuffer_splice(ctx
, stream
, len
);
1097 case LTTNG_CONSUMER32_UST
:
1098 case LTTNG_CONSUMER64_UST
:
1101 ERR("Unknown consumer_data type");
1109 * Take a snapshot for a specific fd
1111 * Returns 0 on success, < 0 on error
1113 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1114 struct lttng_consumer_stream
*stream
)
1116 switch (consumer_data
.type
) {
1117 case LTTNG_CONSUMER_KERNEL
:
1118 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1119 case LTTNG_CONSUMER32_UST
:
1120 case LTTNG_CONSUMER64_UST
:
1121 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1123 ERR("Unknown consumer_data type");
1131 * Get the produced position
1133 * Returns 0 on success, < 0 on error
1135 int lttng_consumer_get_produced_snapshot(
1136 struct lttng_consumer_local_data
*ctx
,
1137 struct lttng_consumer_stream
*stream
,
1140 switch (consumer_data
.type
) {
1141 case LTTNG_CONSUMER_KERNEL
:
1142 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1143 case LTTNG_CONSUMER32_UST
:
1144 case LTTNG_CONSUMER64_UST
:
1145 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1147 ERR("Unknown consumer_data type");
1153 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1154 int sock
, struct pollfd
*consumer_sockpoll
)
1156 switch (consumer_data
.type
) {
1157 case LTTNG_CONSUMER_KERNEL
:
1158 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1159 case LTTNG_CONSUMER32_UST
:
1160 case LTTNG_CONSUMER64_UST
:
1161 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1163 ERR("Unknown consumer_data type");
1170 * This thread polls the fds in the set to consume the data and write
1171 * it to tracefile if necessary.
1173 void *lttng_consumer_thread_poll_fds(void *data
)
1175 int num_rdy
, num_hup
, high_prio
, ret
, i
;
1176 struct pollfd
*pollfd
= NULL
;
1177 /* local view of the streams */
1178 struct lttng_consumer_stream
**local_stream
= NULL
;
1179 /* local view of consumer_data.fds_count */
1181 struct lttng_consumer_local_data
*ctx
= data
;
1182 struct lttng_ht
*metadata_ht
;
1183 struct lttng_ht_iter iter
;
1184 struct lttng_ht_node_ulong
*node
;
1185 struct lttng_consumer_stream
*metadata_stream
;
1188 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1190 rcu_register_thread();
1192 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
1199 * the fds set has been updated, we need to update our
1200 * local array as well
1202 pthread_mutex_lock(&consumer_data
.lock
);
1203 if (consumer_data
.need_update
) {
1204 if (pollfd
!= NULL
) {
1208 if (local_stream
!= NULL
) {
1210 local_stream
= NULL
;
1213 /* allocate for all fds + 1 for the consumer_poll_pipe */
1214 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
1215 if (pollfd
== NULL
) {
1216 perror("pollfd malloc");
1217 pthread_mutex_unlock(&consumer_data
.lock
);
1221 /* allocate for all fds + 1 for the consumer_poll_pipe */
1222 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
1223 sizeof(struct lttng_consumer_stream
));
1224 if (local_stream
== NULL
) {
1225 perror("local_stream malloc");
1226 pthread_mutex_unlock(&consumer_data
.lock
);
1229 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
,
1232 ERR("Error in allocating pollfd or local_outfds");
1233 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
1234 pthread_mutex_unlock(&consumer_data
.lock
);
1238 consumer_data
.need_update
= 0;
1240 pthread_mutex_unlock(&consumer_data
.lock
);
1242 /* No FDs and consumer_quit, consumer_cleanup the thread */
1243 if (nb_fd
== 0 && consumer_quit
== 1) {
1246 /* poll on the array of fds */
1248 DBG("polling on %d fd", nb_fd
+ 1);
1249 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
1250 DBG("poll num_rdy : %d", num_rdy
);
1251 if (num_rdy
== -1) {
1253 * Restart interrupted system call.
1255 if (errno
== EINTR
) {
1258 perror("Poll error");
1259 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
1261 } else if (num_rdy
== 0) {
1262 DBG("Polling thread timed out");
1267 * If the consumer_poll_pipe triggered poll go directly to the
1268 * beginning of the loop to update the array. We want to prioritize
1269 * array update over low-priority reads.
1271 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
1272 size_t pipe_readlen
;
1275 DBG("consumer_poll_pipe wake up");
1276 /* Consume 1 byte of pipe data */
1278 pipe_readlen
= read(ctx
->consumer_poll_pipe
[0], &tmp
, 1);
1279 } while (pipe_readlen
== -1 && errno
== EINTR
);
1283 /* Take care of high priority channels first. */
1284 for (i
= 0; i
< nb_fd
; i
++) {
1285 /* Lookup for metadata which is the highest priority */
1286 lttng_ht_lookup(metadata_ht
,
1287 (void *)((unsigned long) pollfd
[i
].fd
), &iter
);
1288 node
= lttng_ht_iter_get_node_ulong(&iter
);
1290 (pollfd
[i
].revents
& (POLLIN
| POLLPRI
))) {
1291 DBG("Urgent metadata read on fd %d", pollfd
[i
].fd
);
1292 metadata_stream
= caa_container_of(node
,
1293 struct lttng_consumer_stream
, waitfd_node
);
1295 len
= ctx
->on_buffer_ready(metadata_stream
, ctx
);
1296 /* it's ok to have an unavailable sub-buffer */
1297 if (len
< 0 && len
!= -EAGAIN
) {
1299 } else if (len
> 0) {
1300 metadata_stream
->data_read
= 1;
1302 } else if (pollfd
[i
].revents
& POLLPRI
) {
1303 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
1305 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1306 /* it's ok to have an unavailable sub-buffer */
1307 if (len
< 0 && len
!= -EAGAIN
) {
1309 } else if (len
> 0) {
1310 local_stream
[i
]->data_read
= 1;
1316 * If we read high prio channel in this loop, try again
1317 * for more high prio data.
1323 /* Take care of low priority channels. */
1324 for (i
= 0; i
< nb_fd
; i
++) {
1325 if ((pollfd
[i
].revents
& POLLIN
) ||
1326 local_stream
[i
]->hangup_flush_done
) {
1327 DBG("Normal read on fd %d", pollfd
[i
].fd
);
1328 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1329 /* it's ok to have an unavailable sub-buffer */
1330 if (len
< 0 && len
!= -EAGAIN
) {
1332 } else if (len
> 0) {
1333 local_stream
[i
]->data_read
= 1;
1338 /* Handle hangup and errors */
1339 for (i
= 0; i
< nb_fd
; i
++) {
1340 if (!local_stream
[i
]->hangup_flush_done
1341 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
1342 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1343 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1344 DBG("fd %d is hup|err|nval. Attempting flush and read.",
1346 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
1347 /* Attempt read again, for the data we just flushed. */
1348 local_stream
[i
]->data_read
= 1;
1351 * If the poll flag is HUP/ERR/NVAL and we have
1352 * read no data in this pass, we can remove the
1353 * stream from its hash table.
1355 if ((pollfd
[i
].revents
& POLLHUP
)) {
1356 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
1357 if (!local_stream
[i
]->data_read
) {
1358 if (local_stream
[i
]->metadata_flag
) {
1359 iter
.iter
.node
= &local_stream
[i
]->waitfd_node
.node
;
1360 ret
= lttng_ht_del(metadata_ht
, &iter
);
1363 consumer_del_stream(local_stream
[i
]);
1366 } else if (pollfd
[i
].revents
& POLLERR
) {
1367 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
1368 if (!local_stream
[i
]->data_read
) {
1369 if (local_stream
[i
]->metadata_flag
) {
1370 iter
.iter
.node
= &local_stream
[i
]->waitfd_node
.node
;
1371 ret
= lttng_ht_del(metadata_ht
, &iter
);
1374 consumer_del_stream(local_stream
[i
]);
1377 } else if (pollfd
[i
].revents
& POLLNVAL
) {
1378 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
1379 if (!local_stream
[i
]->data_read
) {
1380 if (local_stream
[i
]->metadata_flag
) {
1381 iter
.iter
.node
= &local_stream
[i
]->waitfd_node
.node
;
1382 ret
= lttng_ht_del(metadata_ht
, &iter
);
1385 consumer_del_stream(local_stream
[i
]);
1389 local_stream
[i
]->data_read
= 0;
1393 DBG("polling thread exiting");
1394 if (pollfd
!= NULL
) {
1398 if (local_stream
!= NULL
) {
1400 local_stream
= NULL
;
1402 rcu_unregister_thread();
1407 * This thread listens on the consumerd socket and receives the file
1408 * descriptors from the session daemon.
1410 void *lttng_consumer_thread_receive_fds(void *data
)
1412 int sock
, client_socket
, ret
;
1414 * structure to poll for incoming data on communication socket avoids
1415 * making blocking sockets.
1417 struct pollfd consumer_sockpoll
[2];
1418 struct lttng_consumer_local_data
*ctx
= data
;
1420 rcu_register_thread();
1422 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
1423 unlink(ctx
->consumer_command_sock_path
);
1424 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
1425 if (client_socket
< 0) {
1426 ERR("Cannot create command socket");
1430 ret
= lttcomm_listen_unix_sock(client_socket
);
1435 DBG("Sending ready command to lttng-sessiond");
1436 ret
= lttng_consumer_send_error(ctx
, CONSUMERD_COMMAND_SOCK_READY
);
1437 /* return < 0 on error, but == 0 is not fatal */
1439 ERR("Error sending ready command to lttng-sessiond");
1443 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
1445 perror("fcntl O_NONBLOCK");
1449 /* prepare the FDs to poll : to client socket and the should_quit pipe */
1450 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
1451 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
1452 consumer_sockpoll
[1].fd
= client_socket
;
1453 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1455 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1458 DBG("Connection on client_socket");
1460 /* Blocking call, waiting for transmission */
1461 sock
= lttcomm_accept_unix_sock(client_socket
);
1466 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
1468 perror("fcntl O_NONBLOCK");
1472 /* update the polling structure to poll on the established socket */
1473 consumer_sockpoll
[1].fd
= sock
;
1474 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1477 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1480 DBG("Incoming command on sock");
1481 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1482 if (ret
== -ENOENT
) {
1483 DBG("Received STOP command");
1487 ERR("Communication interrupted on command socket");
1490 if (consumer_quit
) {
1491 DBG("consumer_thread_receive_fds received quit from signal");
1494 DBG("received fds on sock");
1497 DBG("consumer_thread_receive_fds exiting");
1500 * when all fds have hung up, the polling thread
1506 * 2s of grace period, if no polling events occur during
1507 * this period, the polling thread will exit even if there
1508 * are still open FDs (should not happen, but safety mechanism).
1510 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
1513 * Wake-up the other end by writing a null byte in the pipe
1514 * (non-blocking). Important note: Because writing into the
1515 * pipe is non-blocking (and therefore we allow dropping wakeup
1516 * data, as long as there is wakeup data present in the pipe
1517 * buffer to wake up the other end), the other end should
1518 * perform the following sequence for waiting:
1519 * 1) empty the pipe (reads).
1520 * 2) perform update operation.
1521 * 3) wait on the pipe (poll).
1524 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
1525 } while (ret
== -1UL && errno
== EINTR
);
1526 rcu_unregister_thread();
1530 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1531 struct lttng_consumer_local_data
*ctx
)
1533 switch (consumer_data
.type
) {
1534 case LTTNG_CONSUMER_KERNEL
:
1535 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
1536 case LTTNG_CONSUMER32_UST
:
1537 case LTTNG_CONSUMER64_UST
:
1538 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
1540 ERR("Unknown consumer_data type");
1546 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1548 switch (consumer_data
.type
) {
1549 case LTTNG_CONSUMER_KERNEL
:
1550 return lttng_kconsumer_on_recv_stream(stream
);
1551 case LTTNG_CONSUMER32_UST
:
1552 case LTTNG_CONSUMER64_UST
:
1553 return lttng_ustconsumer_on_recv_stream(stream
);
1555 ERR("Unknown consumer_data type");
1562 * Allocate and set consumer data hash tables.
1564 void lttng_consumer_init(void)
1566 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1567 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1568 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);