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 if (relayd
== NULL
) {
184 DBG("Consumer destroy and close relayd socket pair");
186 iter
.iter
.node
= &relayd
->node
.node
;
187 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
189 /* We assume the relayd was already destroyed */
193 /* Close all sockets */
194 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
195 (void) relayd_close(&relayd
->control_sock
);
196 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
197 (void) relayd_close(&relayd
->data_sock
);
199 /* RCU free() call */
200 call_rcu(&relayd
->node
.head
, consumer_rcu_free_relayd
);
204 * Remove a stream from the global list protected by a mutex. This
205 * function is also responsible for freeing its data structures.
207 void consumer_del_stream(struct lttng_consumer_stream
*stream
)
210 struct lttng_ht_iter iter
;
211 struct lttng_consumer_channel
*free_chan
= NULL
;
212 struct consumer_relayd_sock_pair
*relayd
;
216 pthread_mutex_lock(&consumer_data
.lock
);
218 switch (consumer_data
.type
) {
219 case LTTNG_CONSUMER_KERNEL
:
220 if (stream
->mmap_base
!= NULL
) {
221 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
227 case LTTNG_CONSUMER32_UST
:
228 case LTTNG_CONSUMER64_UST
:
229 lttng_ustconsumer_del_stream(stream
);
232 ERR("Unknown consumer_data type");
238 iter
.iter
.node
= &stream
->node
.node
;
239 ret
= lttng_ht_del(consumer_data
.stream_ht
, &iter
);
244 if (consumer_data
.stream_count
<= 0) {
247 consumer_data
.stream_count
--;
251 if (stream
->out_fd
>= 0) {
252 ret
= close(stream
->out_fd
);
257 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
258 ret
= close(stream
->wait_fd
);
263 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
264 ret
= close(stream
->shm_fd
);
270 /* Check and cleanup relayd */
272 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
273 if (relayd
!= NULL
) {
274 uatomic_dec(&relayd
->refcount
);
275 assert(uatomic_read(&relayd
->refcount
) >= 0);
277 ret
= relayd_send_close_stream(&relayd
->control_sock
,
278 stream
->relayd_stream_id
,
279 stream
->next_net_seq_num
- 1);
281 DBG("Unable to close stream on the relayd. Continuing");
283 * Continue here. There is nothing we can do for the relayd.
284 * Chances are that the relayd has closed the socket so we just
285 * continue cleaning up.
289 /* Both conditions are met, we destroy the relayd. */
290 if (uatomic_read(&relayd
->refcount
) == 0 &&
291 uatomic_read(&relayd
->destroy_flag
)) {
292 consumer_destroy_relayd(relayd
);
297 if (!--stream
->chan
->refcount
) {
298 free_chan
= stream
->chan
;
302 call_rcu(&stream
->node
.head
, consumer_free_stream
);
304 consumer_data
.need_update
= 1;
305 pthread_mutex_unlock(&consumer_data
.lock
);
308 consumer_del_channel(free_chan
);
311 struct lttng_consumer_stream
*consumer_allocate_stream(
312 int channel_key
, int stream_key
,
313 int shm_fd
, int wait_fd
,
314 enum lttng_consumer_stream_state state
,
316 enum lttng_event_output output
,
317 const char *path_name
,
323 struct lttng_consumer_stream
*stream
;
326 stream
= zmalloc(sizeof(*stream
));
327 if (stream
== NULL
) {
328 perror("malloc struct lttng_consumer_stream");
331 stream
->chan
= consumer_find_channel(channel_key
);
333 perror("Unable to find channel key");
336 stream
->chan
->refcount
++;
337 stream
->key
= stream_key
;
338 stream
->shm_fd
= shm_fd
;
339 stream
->wait_fd
= wait_fd
;
341 stream
->out_fd_offset
= 0;
342 stream
->state
= state
;
343 stream
->mmap_len
= mmap_len
;
344 stream
->mmap_base
= NULL
;
345 stream
->output
= output
;
348 stream
->net_seq_idx
= net_index
;
349 stream
->metadata_flag
= metadata_flag
;
350 strncpy(stream
->path_name
, path_name
, sizeof(stream
->path_name
));
351 stream
->path_name
[sizeof(stream
->path_name
) - 1] = '\0';
352 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
353 lttng_ht_node_init_ulong(&stream
->waitfd_node
, stream
->wait_fd
);
355 switch (consumer_data
.type
) {
356 case LTTNG_CONSUMER_KERNEL
:
358 case LTTNG_CONSUMER32_UST
:
359 case LTTNG_CONSUMER64_UST
:
360 stream
->cpu
= stream
->chan
->cpucount
++;
361 ret
= lttng_ustconsumer_allocate_stream(stream
);
368 ERR("Unknown consumer_data type");
372 DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d, net_seq_idx %d)",
373 stream
->path_name
, stream
->key
,
376 (unsigned long long) stream
->mmap_len
,
378 stream
->net_seq_idx
);
384 * Add a stream to the global list protected by a mutex.
386 int consumer_add_stream(struct lttng_consumer_stream
*stream
)
389 struct lttng_ht_node_ulong
*node
;
390 struct lttng_ht_iter iter
;
391 struct consumer_relayd_sock_pair
*relayd
;
393 pthread_mutex_lock(&consumer_data
.lock
);
394 /* Steal stream identifier, for UST */
395 consumer_steal_stream_key(stream
->key
);
398 lttng_ht_lookup(consumer_data
.stream_ht
,
399 (void *)((unsigned long) stream
->key
), &iter
);
400 node
= lttng_ht_iter_get_node_ulong(&iter
);
403 /* Stream already exist. Ignore the insertion */
407 lttng_ht_add_unique_ulong(consumer_data
.stream_ht
, &stream
->node
);
409 /* Check and cleanup relayd */
410 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
411 if (relayd
!= NULL
) {
412 uatomic_inc(&relayd
->refcount
);
416 /* Update consumer data */
417 consumer_data
.stream_count
++;
418 consumer_data
.need_update
= 1;
420 switch (consumer_data
.type
) {
421 case LTTNG_CONSUMER_KERNEL
:
423 case LTTNG_CONSUMER32_UST
:
424 case LTTNG_CONSUMER64_UST
:
425 /* Streams are in CPU number order (we rely on this) */
426 stream
->cpu
= stream
->chan
->nr_streams
++;
429 ERR("Unknown consumer_data type");
435 pthread_mutex_unlock(&consumer_data
.lock
);
441 * Add relayd socket to global consumer data hashtable.
443 int consumer_add_relayd(struct consumer_relayd_sock_pair
*relayd
)
446 struct lttng_ht_node_ulong
*node
;
447 struct lttng_ht_iter iter
;
449 if (relayd
== NULL
) {
456 lttng_ht_lookup(consumer_data
.relayd_ht
,
457 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
458 node
= lttng_ht_iter_get_node_ulong(&iter
);
461 /* Relayd already exist. Ignore the insertion */
464 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
473 * Allocate and return a consumer relayd socket.
475 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
478 struct consumer_relayd_sock_pair
*obj
= NULL
;
480 /* Negative net sequence index is a failure */
481 if (net_seq_idx
< 0) {
485 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
487 PERROR("zmalloc relayd sock");
491 obj
->net_seq_idx
= net_seq_idx
;
493 obj
->destroy_flag
= 0;
494 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
495 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
502 * Find a relayd socket pair in the global consumer data.
504 * Return the object if found else NULL.
505 * RCU read-side lock must be held across this call and while using the
508 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
510 struct lttng_ht_iter iter
;
511 struct lttng_ht_node_ulong
*node
;
512 struct consumer_relayd_sock_pair
*relayd
= NULL
;
514 /* Negative keys are lookup failures */
519 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
521 node
= lttng_ht_iter_get_node_ulong(&iter
);
523 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
531 * Handle stream for relayd transmission if the stream applies for network
532 * streaming where the net sequence index is set.
534 * Return destination file descriptor or negative value on error.
536 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
537 size_t data_size
, struct consumer_relayd_sock_pair
*relayd
)
540 struct lttcomm_relayd_data_hdr data_hdr
;
546 /* Reset data header */
547 memset(&data_hdr
, 0, sizeof(data_hdr
));
549 if (stream
->metadata_flag
) {
550 /* Caller MUST acquire the relayd control socket lock */
551 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
556 /* Metadata are always sent on the control socket. */
557 outfd
= relayd
->control_sock
.fd
;
559 /* Set header with stream information */
560 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
561 data_hdr
.data_size
= htobe32(data_size
);
562 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
563 /* Other fields are zeroed previously */
565 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
571 /* Set to go on data socket */
572 outfd
= relayd
->data_sock
.fd
;
580 * Update a stream according to what we just received.
582 void consumer_change_stream_state(int stream_key
,
583 enum lttng_consumer_stream_state state
)
585 struct lttng_consumer_stream
*stream
;
587 pthread_mutex_lock(&consumer_data
.lock
);
588 stream
= consumer_find_stream(stream_key
);
590 stream
->state
= state
;
592 consumer_data
.need_update
= 1;
593 pthread_mutex_unlock(&consumer_data
.lock
);
597 void consumer_free_channel(struct rcu_head
*head
)
599 struct lttng_ht_node_ulong
*node
=
600 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
601 struct lttng_consumer_channel
*channel
=
602 caa_container_of(node
, struct lttng_consumer_channel
, node
);
608 * Remove a channel from the global list protected by a mutex. This
609 * function is also responsible for freeing its data structures.
611 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
614 struct lttng_ht_iter iter
;
616 pthread_mutex_lock(&consumer_data
.lock
);
618 switch (consumer_data
.type
) {
619 case LTTNG_CONSUMER_KERNEL
:
621 case LTTNG_CONSUMER32_UST
:
622 case LTTNG_CONSUMER64_UST
:
623 lttng_ustconsumer_del_channel(channel
);
626 ERR("Unknown consumer_data type");
632 iter
.iter
.node
= &channel
->node
.node
;
633 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
637 if (channel
->mmap_base
!= NULL
) {
638 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
643 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
644 ret
= close(channel
->wait_fd
);
649 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
650 ret
= close(channel
->shm_fd
);
656 call_rcu(&channel
->node
.head
, consumer_free_channel
);
658 pthread_mutex_unlock(&consumer_data
.lock
);
661 struct lttng_consumer_channel
*consumer_allocate_channel(
663 int shm_fd
, int wait_fd
,
665 uint64_t max_sb_size
)
667 struct lttng_consumer_channel
*channel
;
670 channel
= zmalloc(sizeof(*channel
));
671 if (channel
== NULL
) {
672 perror("malloc struct lttng_consumer_channel");
675 channel
->key
= channel_key
;
676 channel
->shm_fd
= shm_fd
;
677 channel
->wait_fd
= wait_fd
;
678 channel
->mmap_len
= mmap_len
;
679 channel
->max_sb_size
= max_sb_size
;
680 channel
->refcount
= 0;
681 channel
->nr_streams
= 0;
682 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
684 switch (consumer_data
.type
) {
685 case LTTNG_CONSUMER_KERNEL
:
686 channel
->mmap_base
= NULL
;
687 channel
->mmap_len
= 0;
689 case LTTNG_CONSUMER32_UST
:
690 case LTTNG_CONSUMER64_UST
:
691 ret
= lttng_ustconsumer_allocate_channel(channel
);
698 ERR("Unknown consumer_data type");
702 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
703 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
704 (unsigned long long) channel
->mmap_len
,
705 (unsigned long long) channel
->max_sb_size
);
711 * Add a channel to the global list protected by a mutex.
713 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
715 struct lttng_ht_node_ulong
*node
;
716 struct lttng_ht_iter iter
;
718 pthread_mutex_lock(&consumer_data
.lock
);
719 /* Steal channel identifier, for UST */
720 consumer_steal_channel_key(channel
->key
);
723 lttng_ht_lookup(consumer_data
.channel_ht
,
724 (void *)((unsigned long) channel
->key
), &iter
);
725 node
= lttng_ht_iter_get_node_ulong(&iter
);
727 /* Channel already exist. Ignore the insertion */
731 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
735 pthread_mutex_unlock(&consumer_data
.lock
);
741 * Allocate the pollfd structure and the local view of the out fds to avoid
742 * doing a lookup in the linked list and concurrency issues when writing is
743 * needed. Called with consumer_data.lock held.
745 * Returns the number of fds in the structures.
747 int consumer_update_poll_array(
748 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
749 struct lttng_consumer_stream
**local_stream
,
750 struct lttng_ht
*metadata_ht
)
753 struct lttng_ht_iter iter
;
754 struct lttng_consumer_stream
*stream
;
756 DBG("Updating poll fd array");
758 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, stream
,
760 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
763 DBG("Active FD %d", stream
->wait_fd
);
764 (*pollfd
)[i
].fd
= stream
->wait_fd
;
765 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
766 if (stream
->metadata_flag
&& metadata_ht
) {
767 lttng_ht_add_unique_ulong(metadata_ht
, &stream
->waitfd_node
);
768 DBG("Active FD added to metadata hash table");
770 local_stream
[i
] = stream
;
776 * Insert the consumer_poll_pipe at the end of the array and don't
777 * increment i so nb_fd is the number of real FD.
779 (*pollfd
)[i
].fd
= ctx
->consumer_poll_pipe
[0];
780 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
785 * Poll on the should_quit pipe and the command socket return -1 on error and
786 * should exit, 0 if data is available on the command socket
788 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
793 num_rdy
= poll(consumer_sockpoll
, 2, -1);
796 * Restart interrupted system call.
798 if (errno
== EINTR
) {
801 perror("Poll error");
804 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
805 DBG("consumer_should_quit wake up");
815 * Set the error socket.
817 void lttng_consumer_set_error_sock(
818 struct lttng_consumer_local_data
*ctx
, int sock
)
820 ctx
->consumer_error_socket
= sock
;
824 * Set the command socket path.
826 void lttng_consumer_set_command_sock_path(
827 struct lttng_consumer_local_data
*ctx
, char *sock
)
829 ctx
->consumer_command_sock_path
= sock
;
833 * Send return code to the session daemon.
834 * If the socket is not defined, we return 0, it is not a fatal error
836 int lttng_consumer_send_error(
837 struct lttng_consumer_local_data
*ctx
, int cmd
)
839 if (ctx
->consumer_error_socket
> 0) {
840 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
841 sizeof(enum lttcomm_sessiond_command
));
848 * Close all the tracefiles and stream fds, should be called when all instances
851 void lttng_consumer_cleanup(void)
853 struct lttng_ht_iter iter
;
854 struct lttng_ht_node_ulong
*node
;
859 * close all outfd. Called when there are no more threads running (after
860 * joining on the threads), no need to protect list iteration with mutex.
862 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, node
,
864 struct lttng_consumer_stream
*stream
=
865 caa_container_of(node
, struct lttng_consumer_stream
, node
);
866 consumer_del_stream(stream
);
869 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
871 struct lttng_consumer_channel
*channel
=
872 caa_container_of(node
, struct lttng_consumer_channel
, node
);
873 consumer_del_channel(channel
);
878 lttng_ht_destroy(consumer_data
.stream_ht
);
879 lttng_ht_destroy(consumer_data
.channel_ht
);
883 * Called from signal handler.
885 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
890 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
891 } while (ret
< 0 && errno
== EINTR
);
893 perror("write consumer quit");
897 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
900 int outfd
= stream
->out_fd
;
903 * This does a blocking write-and-wait on any page that belongs to the
904 * subbuffer prior to the one we just wrote.
905 * Don't care about error values, as these are just hints and ways to
906 * limit the amount of page cache used.
908 if (orig_offset
< stream
->chan
->max_sb_size
) {
911 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
912 stream
->chan
->max_sb_size
,
913 SYNC_FILE_RANGE_WAIT_BEFORE
914 | SYNC_FILE_RANGE_WRITE
915 | SYNC_FILE_RANGE_WAIT_AFTER
);
917 * Give hints to the kernel about how we access the file:
918 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
921 * We need to call fadvise again after the file grows because the
922 * kernel does not seem to apply fadvise to non-existing parts of the
925 * Call fadvise _after_ having waited for the page writeback to
926 * complete because the dirty page writeback semantic is not well
927 * defined. So it can be expected to lead to lower throughput in
930 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
931 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
935 * Initialise the necessary environnement :
936 * - create a new context
937 * - create the poll_pipe
938 * - create the should_quit pipe (for signal handler)
939 * - create the thread pipe (for splice)
941 * Takes a function pointer as argument, this function is called when data is
942 * available on a buffer. This function is responsible to do the
943 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
944 * buffer configuration and then kernctl_put_next_subbuf at the end.
946 * Returns a pointer to the new context or NULL on error.
948 struct lttng_consumer_local_data
*lttng_consumer_create(
949 enum lttng_consumer_type type
,
950 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
951 struct lttng_consumer_local_data
*ctx
),
952 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
953 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
954 int (*update_stream
)(int stream_key
, uint32_t state
))
957 struct lttng_consumer_local_data
*ctx
;
959 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
960 consumer_data
.type
== type
);
961 consumer_data
.type
= type
;
963 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
965 perror("allocating context");
969 ctx
->consumer_error_socket
= -1;
970 /* assign the callbacks */
971 ctx
->on_buffer_ready
= buffer_ready
;
972 ctx
->on_recv_channel
= recv_channel
;
973 ctx
->on_recv_stream
= recv_stream
;
974 ctx
->on_update_stream
= update_stream
;
976 ret
= pipe(ctx
->consumer_poll_pipe
);
978 perror("Error creating poll pipe");
979 goto error_poll_pipe
;
982 /* set read end of the pipe to non-blocking */
983 ret
= fcntl(ctx
->consumer_poll_pipe
[0], F_SETFL
, O_NONBLOCK
);
985 perror("fcntl O_NONBLOCK");
986 goto error_poll_fcntl
;
989 /* set write end of the pipe to non-blocking */
990 ret
= fcntl(ctx
->consumer_poll_pipe
[1], F_SETFL
, O_NONBLOCK
);
992 perror("fcntl O_NONBLOCK");
993 goto error_poll_fcntl
;
996 ret
= pipe(ctx
->consumer_should_quit
);
998 perror("Error creating recv pipe");
999 goto error_quit_pipe
;
1002 ret
= pipe(ctx
->consumer_thread_pipe
);
1004 perror("Error creating thread pipe");
1005 goto error_thread_pipe
;
1012 for (i
= 0; i
< 2; i
++) {
1015 err
= close(ctx
->consumer_should_quit
[i
]);
1022 for (i
= 0; i
< 2; i
++) {
1025 err
= close(ctx
->consumer_poll_pipe
[i
]);
1037 * Close all fds associated with the instance and free the context.
1039 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1043 ret
= close(ctx
->consumer_error_socket
);
1047 ret
= close(ctx
->consumer_thread_pipe
[0]);
1051 ret
= close(ctx
->consumer_thread_pipe
[1]);
1055 ret
= close(ctx
->consumer_poll_pipe
[0]);
1059 ret
= close(ctx
->consumer_poll_pipe
[1]);
1063 ret
= close(ctx
->consumer_should_quit
[0]);
1067 ret
= close(ctx
->consumer_should_quit
[1]);
1071 unlink(ctx
->consumer_command_sock_path
);
1076 * Write the metadata stream id on the specified file descriptor.
1078 static int write_relayd_metadata_id(int fd
,
1079 struct lttng_consumer_stream
*stream
,
1080 struct consumer_relayd_sock_pair
*relayd
)
1083 uint64_t metadata_id
;
1085 metadata_id
= htobe64(stream
->relayd_stream_id
);
1087 ret
= write(fd
, (void *) &metadata_id
,
1088 sizeof(stream
->relayd_stream_id
));
1089 } while (ret
< 0 && errno
== EINTR
);
1091 PERROR("write metadata stream id");
1094 DBG("Metadata stream id %zu written before data",
1095 stream
->relayd_stream_id
);
1102 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1103 * core function for writing trace buffers to either the local filesystem or
1106 * Careful review MUST be put if any changes occur!
1108 * Returns the number of bytes written
1110 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1111 struct lttng_consumer_local_data
*ctx
,
1112 struct lttng_consumer_stream
*stream
, unsigned long len
)
1114 unsigned long mmap_offset
;
1115 ssize_t ret
= 0, written
= 0;
1116 off_t orig_offset
= stream
->out_fd_offset
;
1117 /* Default is on the disk */
1118 int outfd
= stream
->out_fd
;
1119 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1121 /* RCU lock for the relayd pointer */
1124 /* Flag that the current stream if set for network streaming. */
1125 if (stream
->net_seq_idx
!= -1) {
1126 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1127 if (relayd
== NULL
) {
1132 /* get the offset inside the fd to mmap */
1133 switch (consumer_data
.type
) {
1134 case LTTNG_CONSUMER_KERNEL
:
1135 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1137 case LTTNG_CONSUMER32_UST
:
1138 case LTTNG_CONSUMER64_UST
:
1139 ret
= lttng_ustctl_get_mmap_read_offset(stream
->chan
->handle
,
1140 stream
->buf
, &mmap_offset
);
1143 ERR("Unknown consumer_data type");
1148 PERROR("tracer ctl get_mmap_read_offset");
1153 /* Handle stream on the relayd if the output is on the network */
1155 unsigned long netlen
= len
;
1158 * Lock the control socket for the complete duration of the function
1159 * since from this point on we will use the socket.
1161 if (stream
->metadata_flag
) {
1162 /* Metadata requires the control socket. */
1163 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1164 netlen
+= sizeof(stream
->relayd_stream_id
);
1167 ret
= write_relayd_stream_header(stream
, netlen
, relayd
);
1169 /* Use the returned socket. */
1172 /* Write metadata stream id before payload */
1173 if (stream
->metadata_flag
) {
1174 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
);
1181 /* Else, use the default set before which is the filesystem. */
1186 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
1187 } while (ret
< 0 && errno
== EINTR
);
1189 PERROR("Error in file write");
1194 } else if (ret
> len
) {
1195 PERROR("Error in file write (ret %ld > len %lu)", ret
, len
);
1202 DBG("Consumer mmap write() ret %ld (len %lu)", ret
, len
);
1204 /* This call is useless on a socket so better save a syscall. */
1206 /* This won't block, but will start writeout asynchronously */
1207 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1208 SYNC_FILE_RANGE_WRITE
);
1209 stream
->out_fd_offset
+= ret
;
1213 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1216 /* Unlock only if ctrl socket used */
1217 if (relayd
&& stream
->metadata_flag
) {
1218 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1226 * Splice the data from the ring buffer to the tracefile.
1228 * Returns the number of bytes spliced.
1230 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1231 struct lttng_consumer_local_data
*ctx
,
1232 struct lttng_consumer_stream
*stream
, unsigned long len
)
1234 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1236 off_t orig_offset
= stream
->out_fd_offset
;
1237 int fd
= stream
->wait_fd
;
1238 /* Default is on the disk */
1239 int outfd
= stream
->out_fd
;
1240 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1242 switch (consumer_data
.type
) {
1243 case LTTNG_CONSUMER_KERNEL
:
1245 case LTTNG_CONSUMER32_UST
:
1246 case LTTNG_CONSUMER64_UST
:
1247 /* Not supported for user space tracing */
1250 ERR("Unknown consumer_data type");
1254 /* RCU lock for the relayd pointer */
1257 /* Flag that the current stream if set for network streaming. */
1258 if (stream
->net_seq_idx
!= -1) {
1259 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1260 if (relayd
== NULL
) {
1265 /* Write metadata stream id before payload */
1266 if (stream
->metadata_flag
&& relayd
) {
1268 * Lock the control socket for the complete duration of the function
1269 * since from this point on we will use the socket.
1271 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1273 ret
= write_relayd_metadata_id(ctx
->consumer_thread_pipe
[1],
1282 DBG("splice chan to pipe offset %lu of len %lu (fd : %d)",
1283 (unsigned long)offset
, len
, fd
);
1284 ret_splice
= splice(fd
, &offset
, ctx
->consumer_thread_pipe
[1], NULL
, len
,
1285 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1286 DBG("splice chan to pipe, ret %zd", ret_splice
);
1287 if (ret_splice
< 0) {
1288 PERROR("Error in relay splice");
1290 written
= ret_splice
;
1296 /* Handle stream on the relayd if the output is on the network */
1298 if (stream
->metadata_flag
) {
1299 /* Update counter to fit the spliced data */
1300 ret_splice
+= sizeof(stream
->relayd_stream_id
);
1301 len
+= sizeof(stream
->relayd_stream_id
);
1303 * We do this so the return value can match the len passed as
1304 * argument to this function.
1306 written
-= sizeof(stream
->relayd_stream_id
);
1309 ret
= write_relayd_stream_header(stream
, ret_splice
, relayd
);
1311 /* Use the returned socket. */
1314 ERR("Remote relayd disconnected. Stopping");
1319 /* Splice data out */
1320 ret_splice
= splice(ctx
->consumer_thread_pipe
[0], NULL
, outfd
, NULL
,
1321 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1322 DBG("Kernel consumer splice pipe to file, ret %zd", ret_splice
);
1323 if (ret_splice
< 0) {
1324 PERROR("Error in file splice");
1326 written
= ret_splice
;
1330 } else if (ret_splice
> len
) {
1332 PERROR("Wrote more data than requested %zd (len: %lu)",
1334 written
+= ret_splice
;
1340 /* This call is useless on a socket so better save a syscall. */
1342 /* This won't block, but will start writeout asynchronously */
1343 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1344 SYNC_FILE_RANGE_WRITE
);
1345 stream
->out_fd_offset
+= ret_splice
;
1347 written
+= ret_splice
;
1349 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1356 /* send the appropriate error description to sessiond */
1359 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_EBADF
);
1362 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_EINVAL
);
1365 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_ENOMEM
);
1368 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_ESPIPE
);
1373 if (relayd
&& stream
->metadata_flag
) {
1374 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1382 * Take a snapshot for a specific fd
1384 * Returns 0 on success, < 0 on error
1386 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1387 struct lttng_consumer_stream
*stream
)
1389 switch (consumer_data
.type
) {
1390 case LTTNG_CONSUMER_KERNEL
:
1391 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1392 case LTTNG_CONSUMER32_UST
:
1393 case LTTNG_CONSUMER64_UST
:
1394 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1396 ERR("Unknown consumer_data type");
1404 * Get the produced position
1406 * Returns 0 on success, < 0 on error
1408 int lttng_consumer_get_produced_snapshot(
1409 struct lttng_consumer_local_data
*ctx
,
1410 struct lttng_consumer_stream
*stream
,
1413 switch (consumer_data
.type
) {
1414 case LTTNG_CONSUMER_KERNEL
:
1415 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1416 case LTTNG_CONSUMER32_UST
:
1417 case LTTNG_CONSUMER64_UST
:
1418 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1420 ERR("Unknown consumer_data type");
1426 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1427 int sock
, struct pollfd
*consumer_sockpoll
)
1429 switch (consumer_data
.type
) {
1430 case LTTNG_CONSUMER_KERNEL
:
1431 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1432 case LTTNG_CONSUMER32_UST
:
1433 case LTTNG_CONSUMER64_UST
:
1434 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1436 ERR("Unknown consumer_data type");
1443 * This thread polls the fds in the set to consume the data and write
1444 * it to tracefile if necessary.
1446 void *lttng_consumer_thread_poll_fds(void *data
)
1448 int num_rdy
, num_hup
, high_prio
, ret
, i
;
1449 struct pollfd
*pollfd
= NULL
;
1450 /* local view of the streams */
1451 struct lttng_consumer_stream
**local_stream
= NULL
;
1452 /* local view of consumer_data.fds_count */
1454 struct lttng_consumer_local_data
*ctx
= data
;
1455 struct lttng_ht
*metadata_ht
;
1456 struct lttng_ht_iter iter
;
1457 struct lttng_ht_node_ulong
*node
;
1458 struct lttng_consumer_stream
*metadata_stream
;
1461 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1463 rcu_register_thread();
1465 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
1472 * the fds set has been updated, we need to update our
1473 * local array as well
1475 pthread_mutex_lock(&consumer_data
.lock
);
1476 if (consumer_data
.need_update
) {
1477 if (pollfd
!= NULL
) {
1481 if (local_stream
!= NULL
) {
1483 local_stream
= NULL
;
1486 /* allocate for all fds + 1 for the consumer_poll_pipe */
1487 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
1488 if (pollfd
== NULL
) {
1489 perror("pollfd malloc");
1490 pthread_mutex_unlock(&consumer_data
.lock
);
1494 /* allocate for all fds + 1 for the consumer_poll_pipe */
1495 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
1496 sizeof(struct lttng_consumer_stream
));
1497 if (local_stream
== NULL
) {
1498 perror("local_stream malloc");
1499 pthread_mutex_unlock(&consumer_data
.lock
);
1502 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
,
1505 ERR("Error in allocating pollfd or local_outfds");
1506 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
1507 pthread_mutex_unlock(&consumer_data
.lock
);
1511 consumer_data
.need_update
= 0;
1513 pthread_mutex_unlock(&consumer_data
.lock
);
1515 /* No FDs and consumer_quit, consumer_cleanup the thread */
1516 if (nb_fd
== 0 && consumer_quit
== 1) {
1519 /* poll on the array of fds */
1521 DBG("polling on %d fd", nb_fd
+ 1);
1522 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
1523 DBG("poll num_rdy : %d", num_rdy
);
1524 if (num_rdy
== -1) {
1526 * Restart interrupted system call.
1528 if (errno
== EINTR
) {
1531 perror("Poll error");
1532 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
1534 } else if (num_rdy
== 0) {
1535 DBG("Polling thread timed out");
1540 * If the consumer_poll_pipe triggered poll go directly to the
1541 * beginning of the loop to update the array. We want to prioritize
1542 * array update over low-priority reads.
1544 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
1545 size_t pipe_readlen
;
1548 DBG("consumer_poll_pipe wake up");
1549 /* Consume 1 byte of pipe data */
1551 pipe_readlen
= read(ctx
->consumer_poll_pipe
[0], &tmp
, 1);
1552 } while (pipe_readlen
== -1 && errno
== EINTR
);
1556 /* Take care of high priority channels first. */
1557 for (i
= 0; i
< nb_fd
; i
++) {
1558 /* Lookup for metadata which is the highest priority */
1559 lttng_ht_lookup(metadata_ht
,
1560 (void *)((unsigned long) pollfd
[i
].fd
), &iter
);
1561 node
= lttng_ht_iter_get_node_ulong(&iter
);
1563 (pollfd
[i
].revents
& (POLLIN
| POLLPRI
))) {
1564 DBG("Urgent metadata read on fd %d", pollfd
[i
].fd
);
1565 metadata_stream
= caa_container_of(node
,
1566 struct lttng_consumer_stream
, waitfd_node
);
1568 len
= ctx
->on_buffer_ready(metadata_stream
, ctx
);
1569 /* it's ok to have an unavailable sub-buffer */
1570 if (len
< 0 && len
!= -EAGAIN
) {
1572 } else if (len
> 0) {
1573 metadata_stream
->data_read
= 1;
1575 } else if (pollfd
[i
].revents
& POLLPRI
) {
1576 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
1578 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1579 /* it's ok to have an unavailable sub-buffer */
1580 if (len
< 0 && len
!= -EAGAIN
) {
1582 } else if (len
> 0) {
1583 local_stream
[i
]->data_read
= 1;
1589 * If we read high prio channel in this loop, try again
1590 * for more high prio data.
1596 /* Take care of low priority channels. */
1597 for (i
= 0; i
< nb_fd
; i
++) {
1598 if ((pollfd
[i
].revents
& POLLIN
) ||
1599 local_stream
[i
]->hangup_flush_done
) {
1600 DBG("Normal read on fd %d", pollfd
[i
].fd
);
1601 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1602 /* it's ok to have an unavailable sub-buffer */
1603 if (len
< 0 && len
!= -EAGAIN
) {
1605 } else if (len
> 0) {
1606 local_stream
[i
]->data_read
= 1;
1611 /* Handle hangup and errors */
1612 for (i
= 0; i
< nb_fd
; i
++) {
1613 if (!local_stream
[i
]->hangup_flush_done
1614 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
1615 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1616 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1617 DBG("fd %d is hup|err|nval. Attempting flush and read.",
1619 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
1620 /* Attempt read again, for the data we just flushed. */
1621 local_stream
[i
]->data_read
= 1;
1624 * If the poll flag is HUP/ERR/NVAL and we have
1625 * read no data in this pass, we can remove the
1626 * stream from its hash table.
1628 if ((pollfd
[i
].revents
& POLLHUP
)) {
1629 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
1630 if (!local_stream
[i
]->data_read
) {
1631 if (local_stream
[i
]->metadata_flag
) {
1632 iter
.iter
.node
= &local_stream
[i
]->waitfd_node
.node
;
1633 ret
= lttng_ht_del(metadata_ht
, &iter
);
1636 consumer_del_stream(local_stream
[i
]);
1639 } else if (pollfd
[i
].revents
& POLLERR
) {
1640 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
1641 if (!local_stream
[i
]->data_read
) {
1642 if (local_stream
[i
]->metadata_flag
) {
1643 iter
.iter
.node
= &local_stream
[i
]->waitfd_node
.node
;
1644 ret
= lttng_ht_del(metadata_ht
, &iter
);
1647 consumer_del_stream(local_stream
[i
]);
1650 } else if (pollfd
[i
].revents
& POLLNVAL
) {
1651 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
1652 if (!local_stream
[i
]->data_read
) {
1653 if (local_stream
[i
]->metadata_flag
) {
1654 iter
.iter
.node
= &local_stream
[i
]->waitfd_node
.node
;
1655 ret
= lttng_ht_del(metadata_ht
, &iter
);
1658 consumer_del_stream(local_stream
[i
]);
1662 local_stream
[i
]->data_read
= 0;
1666 DBG("polling thread exiting");
1667 if (pollfd
!= NULL
) {
1671 if (local_stream
!= NULL
) {
1673 local_stream
= NULL
;
1675 rcu_unregister_thread();
1680 * This thread listens on the consumerd socket and receives the file
1681 * descriptors from the session daemon.
1683 void *lttng_consumer_thread_receive_fds(void *data
)
1685 int sock
, client_socket
, ret
;
1687 * structure to poll for incoming data on communication socket avoids
1688 * making blocking sockets.
1690 struct pollfd consumer_sockpoll
[2];
1691 struct lttng_consumer_local_data
*ctx
= data
;
1693 rcu_register_thread();
1695 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
1696 unlink(ctx
->consumer_command_sock_path
);
1697 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
1698 if (client_socket
< 0) {
1699 ERR("Cannot create command socket");
1703 ret
= lttcomm_listen_unix_sock(client_socket
);
1708 DBG("Sending ready command to lttng-sessiond");
1709 ret
= lttng_consumer_send_error(ctx
, CONSUMERD_COMMAND_SOCK_READY
);
1710 /* return < 0 on error, but == 0 is not fatal */
1712 ERR("Error sending ready command to lttng-sessiond");
1716 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
1718 perror("fcntl O_NONBLOCK");
1722 /* prepare the FDs to poll : to client socket and the should_quit pipe */
1723 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
1724 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
1725 consumer_sockpoll
[1].fd
= client_socket
;
1726 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1728 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1731 DBG("Connection on client_socket");
1733 /* Blocking call, waiting for transmission */
1734 sock
= lttcomm_accept_unix_sock(client_socket
);
1739 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
1741 perror("fcntl O_NONBLOCK");
1745 /* update the polling structure to poll on the established socket */
1746 consumer_sockpoll
[1].fd
= sock
;
1747 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1750 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1753 DBG("Incoming command on sock");
1754 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1755 if (ret
== -ENOENT
) {
1756 DBG("Received STOP command");
1760 ERR("Communication interrupted on command socket");
1763 if (consumer_quit
) {
1764 DBG("consumer_thread_receive_fds received quit from signal");
1767 DBG("received fds on sock");
1770 DBG("consumer_thread_receive_fds exiting");
1773 * when all fds have hung up, the polling thread
1779 * 2s of grace period, if no polling events occur during
1780 * this period, the polling thread will exit even if there
1781 * are still open FDs (should not happen, but safety mechanism).
1783 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
1786 * Wake-up the other end by writing a null byte in the pipe
1787 * (non-blocking). Important note: Because writing into the
1788 * pipe is non-blocking (and therefore we allow dropping wakeup
1789 * data, as long as there is wakeup data present in the pipe
1790 * buffer to wake up the other end), the other end should
1791 * perform the following sequence for waiting:
1792 * 1) empty the pipe (reads).
1793 * 2) perform update operation.
1794 * 3) wait on the pipe (poll).
1797 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
1798 } while (ret
< 0 && errno
== EINTR
);
1799 rcu_unregister_thread();
1803 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1804 struct lttng_consumer_local_data
*ctx
)
1806 switch (consumer_data
.type
) {
1807 case LTTNG_CONSUMER_KERNEL
:
1808 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
1809 case LTTNG_CONSUMER32_UST
:
1810 case LTTNG_CONSUMER64_UST
:
1811 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
1813 ERR("Unknown consumer_data type");
1819 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1821 switch (consumer_data
.type
) {
1822 case LTTNG_CONSUMER_KERNEL
:
1823 return lttng_kconsumer_on_recv_stream(stream
);
1824 case LTTNG_CONSUMER32_UST
:
1825 case LTTNG_CONSUMER64_UST
:
1826 return lttng_ustconsumer_on_recv_stream(stream
);
1828 ERR("Unknown consumer_data type");
1835 * Allocate and set consumer data hash tables.
1837 void lttng_consumer_init(void)
1839 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1840 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1841 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);