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/kernel-ctl/kernel-ctl.h>
34 #include <common/sessiond-comm/relayd.h>
35 #include <common/sessiond-comm/sessiond-comm.h>
36 #include <common/kernel-consumer/kernel-consumer.h>
37 #include <common/relayd/relayd.h>
38 #include <common/ust-consumer/ust-consumer.h>
42 struct lttng_consumer_global_data consumer_data
= {
45 .type
= LTTNG_CONSUMER_UNKNOWN
,
48 /* timeout parameter, to control the polling thread grace period. */
49 int consumer_poll_timeout
= -1;
52 * Flag to inform the polling thread to quit when all fd hung up. Updated by
53 * the consumer_thread_receive_fds when it notices that all fds has hung up.
54 * Also updated by the signal handler (consumer_should_exit()). Read by the
57 volatile int consumer_quit
= 0;
60 * Find a stream. The consumer_data.lock must be locked during this
63 static struct lttng_consumer_stream
*consumer_find_stream(int key
)
65 struct lttng_ht_iter iter
;
66 struct lttng_ht_node_ulong
*node
;
67 struct lttng_consumer_stream
*stream
= NULL
;
69 /* Negative keys are lookup failures */
75 lttng_ht_lookup(consumer_data
.stream_ht
, (void *)((unsigned long) key
),
77 node
= lttng_ht_iter_get_node_ulong(&iter
);
79 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
87 static void consumer_steal_stream_key(int key
)
89 struct lttng_consumer_stream
*stream
;
92 stream
= consumer_find_stream(key
);
96 * We don't want the lookup to match, but we still need
97 * to iterate on this stream when iterating over the hash table. Just
98 * change the node key.
100 stream
->node
.key
= -1;
105 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
107 struct lttng_ht_iter iter
;
108 struct lttng_ht_node_ulong
*node
;
109 struct lttng_consumer_channel
*channel
= NULL
;
111 /* Negative keys are lookup failures */
117 lttng_ht_lookup(consumer_data
.channel_ht
, (void *)((unsigned long) key
),
119 node
= lttng_ht_iter_get_node_ulong(&iter
);
121 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
129 static void consumer_steal_channel_key(int key
)
131 struct lttng_consumer_channel
*channel
;
134 channel
= consumer_find_channel(key
);
138 * We don't want the lookup to match, but we still need
139 * to iterate on this channel when iterating over the hash table. Just
140 * change the node key.
142 channel
->node
.key
= -1;
148 void consumer_free_stream(struct rcu_head
*head
)
150 struct lttng_ht_node_ulong
*node
=
151 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
152 struct lttng_consumer_stream
*stream
=
153 caa_container_of(node
, struct lttng_consumer_stream
, node
);
159 * RCU protected relayd socket pair free.
161 static void consumer_rcu_free_relayd(struct rcu_head
*head
)
163 struct lttng_ht_node_ulong
*node
=
164 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
165 struct consumer_relayd_sock_pair
*relayd
=
166 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
172 * Destroy and free relayd socket pair object.
174 * This function MUST be called with the consumer_data lock acquired.
176 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
179 struct lttng_ht_iter iter
;
181 if (relayd
== NULL
) {
185 DBG("Consumer destroy and close relayd socket pair");
187 iter
.iter
.node
= &relayd
->node
.node
;
188 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
190 /* We assume the relayd was already destroyed */
194 /* Close all sockets */
195 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
196 (void) relayd_close(&relayd
->control_sock
);
197 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
198 (void) relayd_close(&relayd
->data_sock
);
200 /* RCU free() call */
201 call_rcu(&relayd
->node
.head
, consumer_rcu_free_relayd
);
205 * Remove a stream from the global list protected by a mutex. This
206 * function is also responsible for freeing its data structures.
208 void consumer_del_stream(struct lttng_consumer_stream
*stream
)
211 struct lttng_ht_iter iter
;
212 struct lttng_consumer_channel
*free_chan
= NULL
;
213 struct consumer_relayd_sock_pair
*relayd
;
217 pthread_mutex_lock(&consumer_data
.lock
);
219 switch (consumer_data
.type
) {
220 case LTTNG_CONSUMER_KERNEL
:
221 if (stream
->mmap_base
!= NULL
) {
222 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
228 case LTTNG_CONSUMER32_UST
:
229 case LTTNG_CONSUMER64_UST
:
230 lttng_ustconsumer_del_stream(stream
);
233 ERR("Unknown consumer_data type");
239 iter
.iter
.node
= &stream
->node
.node
;
240 ret
= lttng_ht_del(consumer_data
.stream_ht
, &iter
);
245 if (consumer_data
.stream_count
<= 0) {
248 consumer_data
.stream_count
--;
252 if (stream
->out_fd
>= 0) {
253 ret
= close(stream
->out_fd
);
258 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
259 ret
= close(stream
->wait_fd
);
264 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
265 ret
= close(stream
->shm_fd
);
271 /* Check and cleanup relayd */
273 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
274 if (relayd
!= NULL
) {
275 uatomic_dec(&relayd
->refcount
);
276 assert(uatomic_read(&relayd
->refcount
) >= 0);
278 /* Closing streams requires to lock the control socket. */
279 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
280 ret
= relayd_send_close_stream(&relayd
->control_sock
,
281 stream
->relayd_stream_id
,
282 stream
->next_net_seq_num
- 1);
283 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
285 DBG("Unable to close stream on the relayd. Continuing");
287 * Continue here. There is nothing we can do for the relayd.
288 * Chances are that the relayd has closed the socket so we just
289 * continue cleaning up.
293 /* Both conditions are met, we destroy the relayd. */
294 if (uatomic_read(&relayd
->refcount
) == 0 &&
295 uatomic_read(&relayd
->destroy_flag
)) {
296 consumer_destroy_relayd(relayd
);
301 if (!--stream
->chan
->refcount
) {
302 free_chan
= stream
->chan
;
306 call_rcu(&stream
->node
.head
, consumer_free_stream
);
308 consumer_data
.need_update
= 1;
309 pthread_mutex_unlock(&consumer_data
.lock
);
312 consumer_del_channel(free_chan
);
315 struct lttng_consumer_stream
*consumer_allocate_stream(
316 int channel_key
, int stream_key
,
317 int shm_fd
, int wait_fd
,
318 enum lttng_consumer_stream_state state
,
320 enum lttng_event_output output
,
321 const char *path_name
,
327 struct lttng_consumer_stream
*stream
;
330 stream
= zmalloc(sizeof(*stream
));
331 if (stream
== NULL
) {
332 perror("malloc struct lttng_consumer_stream");
335 stream
->chan
= consumer_find_channel(channel_key
);
337 perror("Unable to find channel key");
340 stream
->chan
->refcount
++;
341 stream
->key
= stream_key
;
342 stream
->shm_fd
= shm_fd
;
343 stream
->wait_fd
= wait_fd
;
345 stream
->out_fd_offset
= 0;
346 stream
->state
= state
;
347 stream
->mmap_len
= mmap_len
;
348 stream
->mmap_base
= NULL
;
349 stream
->output
= output
;
352 stream
->net_seq_idx
= net_index
;
353 stream
->metadata_flag
= metadata_flag
;
354 strncpy(stream
->path_name
, path_name
, sizeof(stream
->path_name
));
355 stream
->path_name
[sizeof(stream
->path_name
) - 1] = '\0';
356 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
357 lttng_ht_node_init_ulong(&stream
->waitfd_node
, stream
->wait_fd
);
359 switch (consumer_data
.type
) {
360 case LTTNG_CONSUMER_KERNEL
:
362 case LTTNG_CONSUMER32_UST
:
363 case LTTNG_CONSUMER64_UST
:
364 stream
->cpu
= stream
->chan
->cpucount
++;
365 ret
= lttng_ustconsumer_allocate_stream(stream
);
372 ERR("Unknown consumer_data type");
376 DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d, net_seq_idx %d)",
377 stream
->path_name
, stream
->key
,
380 (unsigned long long) stream
->mmap_len
,
382 stream
->net_seq_idx
);
388 * Add a stream to the global list protected by a mutex.
390 int consumer_add_stream(struct lttng_consumer_stream
*stream
)
393 struct lttng_ht_node_ulong
*node
;
394 struct lttng_ht_iter iter
;
395 struct consumer_relayd_sock_pair
*relayd
;
397 pthread_mutex_lock(&consumer_data
.lock
);
398 /* Steal stream identifier, for UST */
399 consumer_steal_stream_key(stream
->key
);
402 lttng_ht_lookup(consumer_data
.stream_ht
,
403 (void *)((unsigned long) stream
->key
), &iter
);
404 node
= lttng_ht_iter_get_node_ulong(&iter
);
407 /* Stream already exist. Ignore the insertion */
411 lttng_ht_add_unique_ulong(consumer_data
.stream_ht
, &stream
->node
);
413 /* Check and cleanup relayd */
414 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
415 if (relayd
!= NULL
) {
416 uatomic_inc(&relayd
->refcount
);
420 /* Update consumer data */
421 consumer_data
.stream_count
++;
422 consumer_data
.need_update
= 1;
424 switch (consumer_data
.type
) {
425 case LTTNG_CONSUMER_KERNEL
:
427 case LTTNG_CONSUMER32_UST
:
428 case LTTNG_CONSUMER64_UST
:
429 /* Streams are in CPU number order (we rely on this) */
430 stream
->cpu
= stream
->chan
->nr_streams
++;
433 ERR("Unknown consumer_data type");
439 pthread_mutex_unlock(&consumer_data
.lock
);
445 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
446 * be acquired before calling this.
449 int consumer_add_relayd(struct consumer_relayd_sock_pair
*relayd
)
452 struct lttng_ht_node_ulong
*node
;
453 struct lttng_ht_iter iter
;
455 if (relayd
== NULL
) {
460 lttng_ht_lookup(consumer_data
.relayd_ht
,
461 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
462 node
= lttng_ht_iter_get_node_ulong(&iter
);
464 /* Relayd already exist. Ignore the insertion */
467 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
474 * Allocate and return a consumer relayd socket.
476 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
479 struct consumer_relayd_sock_pair
*obj
= NULL
;
481 /* Negative net sequence index is a failure */
482 if (net_seq_idx
< 0) {
486 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
488 PERROR("zmalloc relayd sock");
492 obj
->net_seq_idx
= net_seq_idx
;
494 obj
->destroy_flag
= 0;
495 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
496 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
503 * Find a relayd socket pair in the global consumer data.
505 * Return the object if found else NULL.
506 * RCU read-side lock must be held across this call and while using the
509 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
511 struct lttng_ht_iter iter
;
512 struct lttng_ht_node_ulong
*node
;
513 struct consumer_relayd_sock_pair
*relayd
= NULL
;
515 /* Negative keys are lookup failures */
520 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
522 node
= lttng_ht_iter_get_node_ulong(&iter
);
524 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
532 * Handle stream for relayd transmission if the stream applies for network
533 * streaming where the net sequence index is set.
535 * Return destination file descriptor or negative value on error.
537 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
538 size_t data_size
, struct consumer_relayd_sock_pair
*relayd
)
541 struct lttcomm_relayd_data_hdr data_hdr
;
547 /* Reset data header */
548 memset(&data_hdr
, 0, sizeof(data_hdr
));
550 if (stream
->metadata_flag
) {
551 /* Caller MUST acquire the relayd control socket lock */
552 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
557 /* Metadata are always sent on the control socket. */
558 outfd
= relayd
->control_sock
.fd
;
560 /* Set header with stream information */
561 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
562 data_hdr
.data_size
= htobe32(data_size
);
563 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
564 /* Other fields are zeroed previously */
566 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
572 /* Set to go on data socket */
573 outfd
= relayd
->data_sock
.fd
;
581 * Update a stream according to what we just received.
583 void consumer_change_stream_state(int stream_key
,
584 enum lttng_consumer_stream_state state
)
586 struct lttng_consumer_stream
*stream
;
588 pthread_mutex_lock(&consumer_data
.lock
);
589 stream
= consumer_find_stream(stream_key
);
591 stream
->state
= state
;
593 consumer_data
.need_update
= 1;
594 pthread_mutex_unlock(&consumer_data
.lock
);
598 void consumer_free_channel(struct rcu_head
*head
)
600 struct lttng_ht_node_ulong
*node
=
601 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
602 struct lttng_consumer_channel
*channel
=
603 caa_container_of(node
, struct lttng_consumer_channel
, node
);
609 * Remove a channel from the global list protected by a mutex. This
610 * function is also responsible for freeing its data structures.
612 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
615 struct lttng_ht_iter iter
;
617 pthread_mutex_lock(&consumer_data
.lock
);
619 switch (consumer_data
.type
) {
620 case LTTNG_CONSUMER_KERNEL
:
622 case LTTNG_CONSUMER32_UST
:
623 case LTTNG_CONSUMER64_UST
:
624 lttng_ustconsumer_del_channel(channel
);
627 ERR("Unknown consumer_data type");
633 iter
.iter
.node
= &channel
->node
.node
;
634 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
638 if (channel
->mmap_base
!= NULL
) {
639 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
644 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
645 ret
= close(channel
->wait_fd
);
650 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
651 ret
= close(channel
->shm_fd
);
657 call_rcu(&channel
->node
.head
, consumer_free_channel
);
659 pthread_mutex_unlock(&consumer_data
.lock
);
662 struct lttng_consumer_channel
*consumer_allocate_channel(
664 int shm_fd
, int wait_fd
,
666 uint64_t max_sb_size
)
668 struct lttng_consumer_channel
*channel
;
671 channel
= zmalloc(sizeof(*channel
));
672 if (channel
== NULL
) {
673 perror("malloc struct lttng_consumer_channel");
676 channel
->key
= channel_key
;
677 channel
->shm_fd
= shm_fd
;
678 channel
->wait_fd
= wait_fd
;
679 channel
->mmap_len
= mmap_len
;
680 channel
->max_sb_size
= max_sb_size
;
681 channel
->refcount
= 0;
682 channel
->nr_streams
= 0;
683 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
685 switch (consumer_data
.type
) {
686 case LTTNG_CONSUMER_KERNEL
:
687 channel
->mmap_base
= NULL
;
688 channel
->mmap_len
= 0;
690 case LTTNG_CONSUMER32_UST
:
691 case LTTNG_CONSUMER64_UST
:
692 ret
= lttng_ustconsumer_allocate_channel(channel
);
699 ERR("Unknown consumer_data type");
703 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
704 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
705 (unsigned long long) channel
->mmap_len
,
706 (unsigned long long) channel
->max_sb_size
);
712 * Add a channel to the global list protected by a mutex.
714 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
716 struct lttng_ht_node_ulong
*node
;
717 struct lttng_ht_iter iter
;
719 pthread_mutex_lock(&consumer_data
.lock
);
720 /* Steal channel identifier, for UST */
721 consumer_steal_channel_key(channel
->key
);
724 lttng_ht_lookup(consumer_data
.channel_ht
,
725 (void *)((unsigned long) channel
->key
), &iter
);
726 node
= lttng_ht_iter_get_node_ulong(&iter
);
728 /* Channel already exist. Ignore the insertion */
732 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
736 pthread_mutex_unlock(&consumer_data
.lock
);
742 * Allocate the pollfd structure and the local view of the out fds to avoid
743 * doing a lookup in the linked list and concurrency issues when writing is
744 * needed. Called with consumer_data.lock held.
746 * Returns the number of fds in the structures.
748 int consumer_update_poll_array(
749 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
750 struct lttng_consumer_stream
**local_stream
,
751 struct lttng_ht
*metadata_ht
)
754 struct lttng_ht_iter iter
;
755 struct lttng_consumer_stream
*stream
;
757 DBG("Updating poll fd array");
759 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, stream
,
761 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
764 DBG("Active FD %d", stream
->wait_fd
);
765 (*pollfd
)[i
].fd
= stream
->wait_fd
;
766 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
767 if (stream
->metadata_flag
&& metadata_ht
) {
768 lttng_ht_add_unique_ulong(metadata_ht
, &stream
->waitfd_node
);
769 DBG("Active FD added to metadata hash table");
771 local_stream
[i
] = stream
;
777 * Insert the consumer_poll_pipe at the end of the array and don't
778 * increment i so nb_fd is the number of real FD.
780 (*pollfd
)[i
].fd
= ctx
->consumer_poll_pipe
[0];
781 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
786 * Poll on the should_quit pipe and the command socket return -1 on error and
787 * should exit, 0 if data is available on the command socket
789 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
794 num_rdy
= poll(consumer_sockpoll
, 2, -1);
797 * Restart interrupted system call.
799 if (errno
== EINTR
) {
802 perror("Poll error");
805 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
806 DBG("consumer_should_quit wake up");
816 * Set the error socket.
818 void lttng_consumer_set_error_sock(
819 struct lttng_consumer_local_data
*ctx
, int sock
)
821 ctx
->consumer_error_socket
= sock
;
825 * Set the command socket path.
827 void lttng_consumer_set_command_sock_path(
828 struct lttng_consumer_local_data
*ctx
, char *sock
)
830 ctx
->consumer_command_sock_path
= sock
;
834 * Send return code to the session daemon.
835 * If the socket is not defined, we return 0, it is not a fatal error
837 int lttng_consumer_send_error(
838 struct lttng_consumer_local_data
*ctx
, int cmd
)
840 if (ctx
->consumer_error_socket
> 0) {
841 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
842 sizeof(enum lttcomm_sessiond_command
));
849 * Close all the tracefiles and stream fds, should be called when all instances
852 void lttng_consumer_cleanup(void)
854 struct lttng_ht_iter iter
;
855 struct lttng_ht_node_ulong
*node
;
860 * close all outfd. Called when there are no more threads running (after
861 * joining on the threads), no need to protect list iteration with mutex.
863 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, node
,
865 struct lttng_consumer_stream
*stream
=
866 caa_container_of(node
, struct lttng_consumer_stream
, node
);
867 consumer_del_stream(stream
);
870 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
872 struct lttng_consumer_channel
*channel
=
873 caa_container_of(node
, struct lttng_consumer_channel
, node
);
874 consumer_del_channel(channel
);
879 lttng_ht_destroy(consumer_data
.stream_ht
);
880 lttng_ht_destroy(consumer_data
.channel_ht
);
884 * Called from signal handler.
886 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
891 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
892 } while (ret
< 0 && errno
== EINTR
);
894 perror("write consumer quit");
898 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
901 int outfd
= stream
->out_fd
;
904 * This does a blocking write-and-wait on any page that belongs to the
905 * subbuffer prior to the one we just wrote.
906 * Don't care about error values, as these are just hints and ways to
907 * limit the amount of page cache used.
909 if (orig_offset
< stream
->chan
->max_sb_size
) {
912 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
913 stream
->chan
->max_sb_size
,
914 SYNC_FILE_RANGE_WAIT_BEFORE
915 | SYNC_FILE_RANGE_WRITE
916 | SYNC_FILE_RANGE_WAIT_AFTER
);
918 * Give hints to the kernel about how we access the file:
919 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
922 * We need to call fadvise again after the file grows because the
923 * kernel does not seem to apply fadvise to non-existing parts of the
926 * Call fadvise _after_ having waited for the page writeback to
927 * complete because the dirty page writeback semantic is not well
928 * defined. So it can be expected to lead to lower throughput in
931 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
932 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
936 * Initialise the necessary environnement :
937 * - create a new context
938 * - create the poll_pipe
939 * - create the should_quit pipe (for signal handler)
940 * - create the thread pipe (for splice)
942 * Takes a function pointer as argument, this function is called when data is
943 * available on a buffer. This function is responsible to do the
944 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
945 * buffer configuration and then kernctl_put_next_subbuf at the end.
947 * Returns a pointer to the new context or NULL on error.
949 struct lttng_consumer_local_data
*lttng_consumer_create(
950 enum lttng_consumer_type type
,
951 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
952 struct lttng_consumer_local_data
*ctx
),
953 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
954 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
955 int (*update_stream
)(int stream_key
, uint32_t state
))
958 struct lttng_consumer_local_data
*ctx
;
960 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
961 consumer_data
.type
== type
);
962 consumer_data
.type
= type
;
964 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
966 perror("allocating context");
970 ctx
->consumer_error_socket
= -1;
971 /* assign the callbacks */
972 ctx
->on_buffer_ready
= buffer_ready
;
973 ctx
->on_recv_channel
= recv_channel
;
974 ctx
->on_recv_stream
= recv_stream
;
975 ctx
->on_update_stream
= update_stream
;
977 ret
= pipe(ctx
->consumer_poll_pipe
);
979 perror("Error creating poll pipe");
980 goto error_poll_pipe
;
983 /* set read end of the pipe to non-blocking */
984 ret
= fcntl(ctx
->consumer_poll_pipe
[0], F_SETFL
, O_NONBLOCK
);
986 perror("fcntl O_NONBLOCK");
987 goto error_poll_fcntl
;
990 /* set write end of the pipe to non-blocking */
991 ret
= fcntl(ctx
->consumer_poll_pipe
[1], F_SETFL
, O_NONBLOCK
);
993 perror("fcntl O_NONBLOCK");
994 goto error_poll_fcntl
;
997 ret
= pipe(ctx
->consumer_should_quit
);
999 perror("Error creating recv pipe");
1000 goto error_quit_pipe
;
1003 ret
= pipe(ctx
->consumer_thread_pipe
);
1005 perror("Error creating thread pipe");
1006 goto error_thread_pipe
;
1013 for (i
= 0; i
< 2; i
++) {
1016 err
= close(ctx
->consumer_should_quit
[i
]);
1023 for (i
= 0; i
< 2; i
++) {
1026 err
= close(ctx
->consumer_poll_pipe
[i
]);
1038 * Close all fds associated with the instance and free the context.
1040 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1044 ret
= close(ctx
->consumer_error_socket
);
1048 ret
= close(ctx
->consumer_thread_pipe
[0]);
1052 ret
= close(ctx
->consumer_thread_pipe
[1]);
1056 ret
= close(ctx
->consumer_poll_pipe
[0]);
1060 ret
= close(ctx
->consumer_poll_pipe
[1]);
1064 ret
= close(ctx
->consumer_should_quit
[0]);
1068 ret
= close(ctx
->consumer_should_quit
[1]);
1072 unlink(ctx
->consumer_command_sock_path
);
1077 * Write the metadata stream id on the specified file descriptor.
1079 static int write_relayd_metadata_id(int fd
,
1080 struct lttng_consumer_stream
*stream
,
1081 struct consumer_relayd_sock_pair
*relayd
)
1084 uint64_t metadata_id
;
1086 metadata_id
= htobe64(stream
->relayd_stream_id
);
1088 ret
= write(fd
, (void *) &metadata_id
,
1089 sizeof(stream
->relayd_stream_id
));
1090 } while (ret
< 0 && errno
== EINTR
);
1092 PERROR("write metadata stream id");
1095 DBG("Metadata stream id %" PRIu64
" written before data",
1096 stream
->relayd_stream_id
);
1103 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1104 * core function for writing trace buffers to either the local filesystem or
1107 * Careful review MUST be put if any changes occur!
1109 * Returns the number of bytes written
1111 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1112 struct lttng_consumer_local_data
*ctx
,
1113 struct lttng_consumer_stream
*stream
, unsigned long len
)
1115 unsigned long mmap_offset
;
1116 ssize_t ret
= 0, written
= 0;
1117 off_t orig_offset
= stream
->out_fd_offset
;
1118 /* Default is on the disk */
1119 int outfd
= stream
->out_fd
;
1120 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1122 /* RCU lock for the relayd pointer */
1125 /* Flag that the current stream if set for network streaming. */
1126 if (stream
->net_seq_idx
!= -1) {
1127 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1128 if (relayd
== NULL
) {
1133 /* get the offset inside the fd to mmap */
1134 switch (consumer_data
.type
) {
1135 case LTTNG_CONSUMER_KERNEL
:
1136 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1138 case LTTNG_CONSUMER32_UST
:
1139 case LTTNG_CONSUMER64_UST
:
1140 ret
= lttng_ustctl_get_mmap_read_offset(stream
->chan
->handle
,
1141 stream
->buf
, &mmap_offset
);
1144 ERR("Unknown consumer_data type");
1149 PERROR("tracer ctl get_mmap_read_offset");
1154 /* Handle stream on the relayd if the output is on the network */
1156 unsigned long netlen
= len
;
1159 * Lock the control socket for the complete duration of the function
1160 * since from this point on we will use the socket.
1162 if (stream
->metadata_flag
) {
1163 /* Metadata requires the control socket. */
1164 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1165 netlen
+= sizeof(stream
->relayd_stream_id
);
1168 ret
= write_relayd_stream_header(stream
, netlen
, relayd
);
1170 /* Use the returned socket. */
1173 /* Write metadata stream id before payload */
1174 if (stream
->metadata_flag
) {
1175 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
);
1182 /* Else, use the default set before which is the filesystem. */
1187 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
1188 } while (ret
< 0 && errno
== EINTR
);
1190 PERROR("Error in file write");
1195 } else if (ret
> len
) {
1196 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1203 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1205 /* This call is useless on a socket so better save a syscall. */
1207 /* This won't block, but will start writeout asynchronously */
1208 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1209 SYNC_FILE_RANGE_WRITE
);
1210 stream
->out_fd_offset
+= ret
;
1214 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1217 /* Unlock only if ctrl socket used */
1218 if (relayd
&& stream
->metadata_flag
) {
1219 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1227 * Splice the data from the ring buffer to the tracefile.
1229 * Returns the number of bytes spliced.
1231 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1232 struct lttng_consumer_local_data
*ctx
,
1233 struct lttng_consumer_stream
*stream
, unsigned long len
)
1235 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1237 off_t orig_offset
= stream
->out_fd_offset
;
1238 int fd
= stream
->wait_fd
;
1239 /* Default is on the disk */
1240 int outfd
= stream
->out_fd
;
1241 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1243 switch (consumer_data
.type
) {
1244 case LTTNG_CONSUMER_KERNEL
:
1246 case LTTNG_CONSUMER32_UST
:
1247 case LTTNG_CONSUMER64_UST
:
1248 /* Not supported for user space tracing */
1251 ERR("Unknown consumer_data type");
1255 /* RCU lock for the relayd pointer */
1258 /* Flag that the current stream if set for network streaming. */
1259 if (stream
->net_seq_idx
!= -1) {
1260 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1261 if (relayd
== NULL
) {
1266 /* Write metadata stream id before payload */
1267 if (stream
->metadata_flag
&& relayd
) {
1269 * Lock the control socket for the complete duration of the function
1270 * since from this point on we will use the socket.
1272 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1274 ret
= write_relayd_metadata_id(ctx
->consumer_thread_pipe
[1],
1283 DBG("splice chan to pipe offset %lu of len %lu (fd : %d)",
1284 (unsigned long)offset
, len
, fd
);
1285 ret_splice
= splice(fd
, &offset
, ctx
->consumer_thread_pipe
[1], NULL
, len
,
1286 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1287 DBG("splice chan to pipe, ret %zd", ret_splice
);
1288 if (ret_splice
< 0) {
1289 PERROR("Error in relay splice");
1291 written
= ret_splice
;
1297 /* Handle stream on the relayd if the output is on the network */
1299 if (stream
->metadata_flag
) {
1300 /* Update counter to fit the spliced data */
1301 ret_splice
+= sizeof(stream
->relayd_stream_id
);
1302 len
+= sizeof(stream
->relayd_stream_id
);
1304 * We do this so the return value can match the len passed as
1305 * argument to this function.
1307 written
-= sizeof(stream
->relayd_stream_id
);
1310 ret
= write_relayd_stream_header(stream
, ret_splice
, relayd
);
1312 /* Use the returned socket. */
1315 ERR("Remote relayd disconnected. Stopping");
1320 /* Splice data out */
1321 ret_splice
= splice(ctx
->consumer_thread_pipe
[0], NULL
, outfd
, NULL
,
1322 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1323 DBG("Kernel consumer splice pipe to file, ret %zd", ret_splice
);
1324 if (ret_splice
< 0) {
1325 PERROR("Error in file splice");
1327 written
= ret_splice
;
1331 } else if (ret_splice
> len
) {
1333 PERROR("Wrote more data than requested %zd (len: %lu)",
1335 written
+= ret_splice
;
1341 /* This call is useless on a socket so better save a syscall. */
1343 /* This won't block, but will start writeout asynchronously */
1344 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1345 SYNC_FILE_RANGE_WRITE
);
1346 stream
->out_fd_offset
+= ret_splice
;
1348 written
+= ret_splice
;
1350 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1357 /* send the appropriate error description to sessiond */
1360 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_EBADF
);
1363 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_EINVAL
);
1366 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_ENOMEM
);
1369 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_ESPIPE
);
1374 if (relayd
&& stream
->metadata_flag
) {
1375 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1383 * Take a snapshot for a specific fd
1385 * Returns 0 on success, < 0 on error
1387 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1388 struct lttng_consumer_stream
*stream
)
1390 switch (consumer_data
.type
) {
1391 case LTTNG_CONSUMER_KERNEL
:
1392 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1393 case LTTNG_CONSUMER32_UST
:
1394 case LTTNG_CONSUMER64_UST
:
1395 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1397 ERR("Unknown consumer_data type");
1405 * Get the produced position
1407 * Returns 0 on success, < 0 on error
1409 int lttng_consumer_get_produced_snapshot(
1410 struct lttng_consumer_local_data
*ctx
,
1411 struct lttng_consumer_stream
*stream
,
1414 switch (consumer_data
.type
) {
1415 case LTTNG_CONSUMER_KERNEL
:
1416 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1417 case LTTNG_CONSUMER32_UST
:
1418 case LTTNG_CONSUMER64_UST
:
1419 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1421 ERR("Unknown consumer_data type");
1427 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1428 int sock
, struct pollfd
*consumer_sockpoll
)
1430 switch (consumer_data
.type
) {
1431 case LTTNG_CONSUMER_KERNEL
:
1432 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1433 case LTTNG_CONSUMER32_UST
:
1434 case LTTNG_CONSUMER64_UST
:
1435 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1437 ERR("Unknown consumer_data type");
1444 * This thread polls the fds in the set to consume the data and write
1445 * it to tracefile if necessary.
1447 void *lttng_consumer_thread_poll_fds(void *data
)
1449 int num_rdy
, num_hup
, high_prio
, ret
, i
;
1450 struct pollfd
*pollfd
= NULL
;
1451 /* local view of the streams */
1452 struct lttng_consumer_stream
**local_stream
= NULL
;
1453 /* local view of consumer_data.fds_count */
1455 struct lttng_consumer_local_data
*ctx
= data
;
1456 struct lttng_ht
*metadata_ht
;
1457 struct lttng_ht_iter iter
;
1458 struct lttng_ht_node_ulong
*node
;
1459 struct lttng_consumer_stream
*metadata_stream
;
1462 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1464 rcu_register_thread();
1466 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
1473 * the fds set has been updated, we need to update our
1474 * local array as well
1476 pthread_mutex_lock(&consumer_data
.lock
);
1477 if (consumer_data
.need_update
) {
1478 if (pollfd
!= NULL
) {
1482 if (local_stream
!= NULL
) {
1484 local_stream
= NULL
;
1487 /* allocate for all fds + 1 for the consumer_poll_pipe */
1488 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
1489 if (pollfd
== NULL
) {
1490 perror("pollfd malloc");
1491 pthread_mutex_unlock(&consumer_data
.lock
);
1495 /* allocate for all fds + 1 for the consumer_poll_pipe */
1496 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
1497 sizeof(struct lttng_consumer_stream
));
1498 if (local_stream
== NULL
) {
1499 perror("local_stream malloc");
1500 pthread_mutex_unlock(&consumer_data
.lock
);
1503 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
,
1506 ERR("Error in allocating pollfd or local_outfds");
1507 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
1508 pthread_mutex_unlock(&consumer_data
.lock
);
1512 consumer_data
.need_update
= 0;
1514 pthread_mutex_unlock(&consumer_data
.lock
);
1516 /* No FDs and consumer_quit, consumer_cleanup the thread */
1517 if (nb_fd
== 0 && consumer_quit
== 1) {
1520 /* poll on the array of fds */
1522 DBG("polling on %d fd", nb_fd
+ 1);
1523 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
1524 DBG("poll num_rdy : %d", num_rdy
);
1525 if (num_rdy
== -1) {
1527 * Restart interrupted system call.
1529 if (errno
== EINTR
) {
1532 perror("Poll error");
1533 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
1535 } else if (num_rdy
== 0) {
1536 DBG("Polling thread timed out");
1541 * If the consumer_poll_pipe triggered poll go directly to the
1542 * beginning of the loop to update the array. We want to prioritize
1543 * array update over low-priority reads.
1545 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
1546 size_t pipe_readlen
;
1549 DBG("consumer_poll_pipe wake up");
1550 /* Consume 1 byte of pipe data */
1552 pipe_readlen
= read(ctx
->consumer_poll_pipe
[0], &tmp
, 1);
1553 } while (pipe_readlen
== -1 && errno
== EINTR
);
1557 /* Take care of high priority channels first. */
1558 for (i
= 0; i
< nb_fd
; i
++) {
1559 /* Lookup for metadata which is the highest priority */
1560 lttng_ht_lookup(metadata_ht
,
1561 (void *)((unsigned long) pollfd
[i
].fd
), &iter
);
1562 node
= lttng_ht_iter_get_node_ulong(&iter
);
1564 (pollfd
[i
].revents
& (POLLIN
| POLLPRI
))) {
1565 DBG("Urgent metadata read on fd %d", pollfd
[i
].fd
);
1566 metadata_stream
= caa_container_of(node
,
1567 struct lttng_consumer_stream
, waitfd_node
);
1569 len
= ctx
->on_buffer_ready(metadata_stream
, ctx
);
1570 /* it's ok to have an unavailable sub-buffer */
1571 if (len
< 0 && len
!= -EAGAIN
) {
1573 } else if (len
> 0) {
1574 metadata_stream
->data_read
= 1;
1576 } else if (pollfd
[i
].revents
& POLLPRI
) {
1577 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
1579 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1580 /* it's ok to have an unavailable sub-buffer */
1581 if (len
< 0 && len
!= -EAGAIN
) {
1583 } else if (len
> 0) {
1584 local_stream
[i
]->data_read
= 1;
1590 * If we read high prio channel in this loop, try again
1591 * for more high prio data.
1597 /* Take care of low priority channels. */
1598 for (i
= 0; i
< nb_fd
; i
++) {
1599 if ((pollfd
[i
].revents
& POLLIN
) ||
1600 local_stream
[i
]->hangup_flush_done
) {
1601 DBG("Normal read on fd %d", pollfd
[i
].fd
);
1602 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1603 /* it's ok to have an unavailable sub-buffer */
1604 if (len
< 0 && len
!= -EAGAIN
) {
1606 } else if (len
> 0) {
1607 local_stream
[i
]->data_read
= 1;
1612 /* Handle hangup and errors */
1613 for (i
= 0; i
< nb_fd
; i
++) {
1614 if (!local_stream
[i
]->hangup_flush_done
1615 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
1616 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1617 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1618 DBG("fd %d is hup|err|nval. Attempting flush and read.",
1620 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
1621 /* Attempt read again, for the data we just flushed. */
1622 local_stream
[i
]->data_read
= 1;
1625 * If the poll flag is HUP/ERR/NVAL and we have
1626 * read no data in this pass, we can remove the
1627 * stream from its hash table.
1629 if ((pollfd
[i
].revents
& POLLHUP
)) {
1630 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
1631 if (!local_stream
[i
]->data_read
) {
1632 if (local_stream
[i
]->metadata_flag
) {
1633 iter
.iter
.node
= &local_stream
[i
]->waitfd_node
.node
;
1634 ret
= lttng_ht_del(metadata_ht
, &iter
);
1637 consumer_del_stream(local_stream
[i
]);
1640 } else if (pollfd
[i
].revents
& POLLERR
) {
1641 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
1642 if (!local_stream
[i
]->data_read
) {
1643 if (local_stream
[i
]->metadata_flag
) {
1644 iter
.iter
.node
= &local_stream
[i
]->waitfd_node
.node
;
1645 ret
= lttng_ht_del(metadata_ht
, &iter
);
1648 consumer_del_stream(local_stream
[i
]);
1651 } else if (pollfd
[i
].revents
& POLLNVAL
) {
1652 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
1653 if (!local_stream
[i
]->data_read
) {
1654 if (local_stream
[i
]->metadata_flag
) {
1655 iter
.iter
.node
= &local_stream
[i
]->waitfd_node
.node
;
1656 ret
= lttng_ht_del(metadata_ht
, &iter
);
1659 consumer_del_stream(local_stream
[i
]);
1663 local_stream
[i
]->data_read
= 0;
1667 DBG("polling thread exiting");
1668 if (pollfd
!= NULL
) {
1672 if (local_stream
!= NULL
) {
1674 local_stream
= NULL
;
1676 rcu_unregister_thread();
1681 * This thread listens on the consumerd socket and receives the file
1682 * descriptors from the session daemon.
1684 void *lttng_consumer_thread_receive_fds(void *data
)
1686 int sock
, client_socket
, ret
;
1688 * structure to poll for incoming data on communication socket avoids
1689 * making blocking sockets.
1691 struct pollfd consumer_sockpoll
[2];
1692 struct lttng_consumer_local_data
*ctx
= data
;
1694 rcu_register_thread();
1696 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
1697 unlink(ctx
->consumer_command_sock_path
);
1698 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
1699 if (client_socket
< 0) {
1700 ERR("Cannot create command socket");
1704 ret
= lttcomm_listen_unix_sock(client_socket
);
1709 DBG("Sending ready command to lttng-sessiond");
1710 ret
= lttng_consumer_send_error(ctx
, CONSUMERD_COMMAND_SOCK_READY
);
1711 /* return < 0 on error, but == 0 is not fatal */
1713 ERR("Error sending ready command to lttng-sessiond");
1717 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
1719 perror("fcntl O_NONBLOCK");
1723 /* prepare the FDs to poll : to client socket and the should_quit pipe */
1724 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
1725 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
1726 consumer_sockpoll
[1].fd
= client_socket
;
1727 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1729 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1732 DBG("Connection on client_socket");
1734 /* Blocking call, waiting for transmission */
1735 sock
= lttcomm_accept_unix_sock(client_socket
);
1740 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
1742 perror("fcntl O_NONBLOCK");
1746 /* update the polling structure to poll on the established socket */
1747 consumer_sockpoll
[1].fd
= sock
;
1748 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1751 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1754 DBG("Incoming command on sock");
1755 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1756 if (ret
== -ENOENT
) {
1757 DBG("Received STOP command");
1761 ERR("Communication interrupted on command socket");
1764 if (consumer_quit
) {
1765 DBG("consumer_thread_receive_fds received quit from signal");
1768 DBG("received fds on sock");
1771 DBG("consumer_thread_receive_fds exiting");
1774 * when all fds have hung up, the polling thread
1780 * 2s of grace period, if no polling events occur during
1781 * this period, the polling thread will exit even if there
1782 * are still open FDs (should not happen, but safety mechanism).
1784 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
1787 * Wake-up the other end by writing a null byte in the pipe
1788 * (non-blocking). Important note: Because writing into the
1789 * pipe is non-blocking (and therefore we allow dropping wakeup
1790 * data, as long as there is wakeup data present in the pipe
1791 * buffer to wake up the other end), the other end should
1792 * perform the following sequence for waiting:
1793 * 1) empty the pipe (reads).
1794 * 2) perform update operation.
1795 * 3) wait on the pipe (poll).
1798 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
1799 } while (ret
< 0 && errno
== EINTR
);
1800 rcu_unregister_thread();
1804 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1805 struct lttng_consumer_local_data
*ctx
)
1807 switch (consumer_data
.type
) {
1808 case LTTNG_CONSUMER_KERNEL
:
1809 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
1810 case LTTNG_CONSUMER32_UST
:
1811 case LTTNG_CONSUMER64_UST
:
1812 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
1814 ERR("Unknown consumer_data type");
1820 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1822 switch (consumer_data
.type
) {
1823 case LTTNG_CONSUMER_KERNEL
:
1824 return lttng_kconsumer_on_recv_stream(stream
);
1825 case LTTNG_CONSUMER32_UST
:
1826 case LTTNG_CONSUMER64_UST
:
1827 return lttng_ustconsumer_on_recv_stream(stream
);
1829 ERR("Unknown consumer_data type");
1836 * Allocate and set consumer data hash tables.
1838 void lttng_consumer_init(void)
1840 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1841 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1842 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1846 * Process the ADD_RELAYD command receive by a consumer.
1848 * This will create a relayd socket pair and add it to the relayd hash table.
1849 * The caller MUST acquire a RCU read side lock before calling it.
1851 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
1852 struct lttng_consumer_local_data
*ctx
, int sock
,
1853 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
)
1856 struct consumer_relayd_sock_pair
*relayd
;
1858 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
1860 /* Get relayd reference if exists. */
1861 relayd
= consumer_find_relayd(net_seq_idx
);
1862 if (relayd
== NULL
) {
1863 /* Not found. Allocate one. */
1864 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
1865 if (relayd
== NULL
) {
1866 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
1871 /* Poll on consumer socket. */
1872 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1877 /* Get relayd socket from session daemon */
1878 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
1879 if (ret
!= sizeof(fd
)) {
1880 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
1885 /* Copy socket information and received FD */
1886 switch (sock_type
) {
1887 case LTTNG_STREAM_CONTROL
:
1888 /* Copy received lttcomm socket */
1889 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
1890 ret
= lttcomm_create_sock(&relayd
->control_sock
);
1895 /* Close the created socket fd which is useless */
1896 close(relayd
->control_sock
.fd
);
1898 /* Assign new file descriptor */
1899 relayd
->control_sock
.fd
= fd
;
1901 case LTTNG_STREAM_DATA
:
1902 /* Copy received lttcomm socket */
1903 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
1904 ret
= lttcomm_create_sock(&relayd
->data_sock
);
1909 /* Close the created socket fd which is useless */
1910 close(relayd
->data_sock
.fd
);
1912 /* Assign new file descriptor */
1913 relayd
->data_sock
.fd
= fd
;
1916 ERR("Unknown relayd socket type (%d)", sock_type
);
1920 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
1921 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
1922 relayd
->net_seq_idx
, fd
);
1925 * Add relayd socket pair to consumer data hashtable. If object already
1926 * exists or on error, the function gracefully returns.
1928 consumer_add_relayd(relayd
);