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 * Flag a relayd socket pair for destruction. Destroy it if the refcount
208 * RCU read side lock MUST be aquired before calling this function.
210 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
214 /* Set destroy flag for this object */
215 uatomic_set(&relayd
->destroy_flag
, 1);
217 /* Destroy the relayd if refcount is 0 */
218 if (uatomic_read(&relayd
->refcount
) == 0) {
219 consumer_destroy_relayd(relayd
);
224 * Remove a stream from the global list protected by a mutex. This
225 * function is also responsible for freeing its data structures.
227 void consumer_del_stream(struct lttng_consumer_stream
*stream
)
230 struct lttng_ht_iter iter
;
231 struct lttng_consumer_channel
*free_chan
= NULL
;
232 struct consumer_relayd_sock_pair
*relayd
;
236 pthread_mutex_lock(&consumer_data
.lock
);
238 switch (consumer_data
.type
) {
239 case LTTNG_CONSUMER_KERNEL
:
240 if (stream
->mmap_base
!= NULL
) {
241 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
247 case LTTNG_CONSUMER32_UST
:
248 case LTTNG_CONSUMER64_UST
:
249 lttng_ustconsumer_del_stream(stream
);
252 ERR("Unknown consumer_data type");
258 iter
.iter
.node
= &stream
->node
.node
;
259 ret
= lttng_ht_del(consumer_data
.stream_ht
, &iter
);
264 if (consumer_data
.stream_count
<= 0) {
267 consumer_data
.stream_count
--;
271 if (stream
->out_fd
>= 0) {
272 ret
= close(stream
->out_fd
);
277 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
278 ret
= close(stream
->wait_fd
);
283 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
284 ret
= close(stream
->shm_fd
);
290 /* Check and cleanup relayd */
292 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
293 if (relayd
!= NULL
) {
294 uatomic_dec(&relayd
->refcount
);
295 assert(uatomic_read(&relayd
->refcount
) >= 0);
297 /* Closing streams requires to lock the control socket. */
298 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
299 ret
= relayd_send_close_stream(&relayd
->control_sock
,
300 stream
->relayd_stream_id
,
301 stream
->next_net_seq_num
- 1);
302 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
304 DBG("Unable to close stream on the relayd. Continuing");
306 * Continue here. There is nothing we can do for the relayd.
307 * Chances are that the relayd has closed the socket so we just
308 * continue cleaning up.
312 /* Both conditions are met, we destroy the relayd. */
313 if (uatomic_read(&relayd
->refcount
) == 0 &&
314 uatomic_read(&relayd
->destroy_flag
)) {
315 consumer_destroy_relayd(relayd
);
320 if (!--stream
->chan
->refcount
) {
321 free_chan
= stream
->chan
;
325 call_rcu(&stream
->node
.head
, consumer_free_stream
);
327 consumer_data
.need_update
= 1;
328 pthread_mutex_unlock(&consumer_data
.lock
);
331 consumer_del_channel(free_chan
);
334 struct lttng_consumer_stream
*consumer_allocate_stream(
335 int channel_key
, int stream_key
,
336 int shm_fd
, int wait_fd
,
337 enum lttng_consumer_stream_state state
,
339 enum lttng_event_output output
,
340 const char *path_name
,
346 struct lttng_consumer_stream
*stream
;
349 stream
= zmalloc(sizeof(*stream
));
350 if (stream
== NULL
) {
351 perror("malloc struct lttng_consumer_stream");
354 stream
->chan
= consumer_find_channel(channel_key
);
356 perror("Unable to find channel key");
359 stream
->chan
->refcount
++;
360 stream
->key
= stream_key
;
361 stream
->shm_fd
= shm_fd
;
362 stream
->wait_fd
= wait_fd
;
364 stream
->out_fd_offset
= 0;
365 stream
->state
= state
;
366 stream
->mmap_len
= mmap_len
;
367 stream
->mmap_base
= NULL
;
368 stream
->output
= output
;
371 stream
->net_seq_idx
= net_index
;
372 stream
->metadata_flag
= metadata_flag
;
373 strncpy(stream
->path_name
, path_name
, sizeof(stream
->path_name
));
374 stream
->path_name
[sizeof(stream
->path_name
) - 1] = '\0';
375 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
376 lttng_ht_node_init_ulong(&stream
->waitfd_node
, stream
->wait_fd
);
378 switch (consumer_data
.type
) {
379 case LTTNG_CONSUMER_KERNEL
:
381 case LTTNG_CONSUMER32_UST
:
382 case LTTNG_CONSUMER64_UST
:
383 stream
->cpu
= stream
->chan
->cpucount
++;
384 ret
= lttng_ustconsumer_allocate_stream(stream
);
391 ERR("Unknown consumer_data type");
395 DBG("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, out_fd %d, net_seq_idx %d)",
396 stream
->path_name
, stream
->key
,
399 (unsigned long long) stream
->mmap_len
,
401 stream
->net_seq_idx
);
407 * Add a stream to the global list protected by a mutex.
409 int consumer_add_stream(struct lttng_consumer_stream
*stream
)
412 struct lttng_ht_node_ulong
*node
;
413 struct lttng_ht_iter iter
;
414 struct consumer_relayd_sock_pair
*relayd
;
416 pthread_mutex_lock(&consumer_data
.lock
);
417 /* Steal stream identifier, for UST */
418 consumer_steal_stream_key(stream
->key
);
421 lttng_ht_lookup(consumer_data
.stream_ht
,
422 (void *)((unsigned long) stream
->key
), &iter
);
423 node
= lttng_ht_iter_get_node_ulong(&iter
);
426 /* Stream already exist. Ignore the insertion */
430 lttng_ht_add_unique_ulong(consumer_data
.stream_ht
, &stream
->node
);
432 /* Check and cleanup relayd */
433 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
434 if (relayd
!= NULL
) {
435 uatomic_inc(&relayd
->refcount
);
439 /* Update consumer data */
440 consumer_data
.stream_count
++;
441 consumer_data
.need_update
= 1;
443 switch (consumer_data
.type
) {
444 case LTTNG_CONSUMER_KERNEL
:
446 case LTTNG_CONSUMER32_UST
:
447 case LTTNG_CONSUMER64_UST
:
448 /* Streams are in CPU number order (we rely on this) */
449 stream
->cpu
= stream
->chan
->nr_streams
++;
452 ERR("Unknown consumer_data type");
458 pthread_mutex_unlock(&consumer_data
.lock
);
464 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
465 * be acquired before calling this.
468 int consumer_add_relayd(struct consumer_relayd_sock_pair
*relayd
)
471 struct lttng_ht_node_ulong
*node
;
472 struct lttng_ht_iter iter
;
474 if (relayd
== NULL
) {
479 lttng_ht_lookup(consumer_data
.relayd_ht
,
480 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
481 node
= lttng_ht_iter_get_node_ulong(&iter
);
483 /* Relayd already exist. Ignore the insertion */
486 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
493 * Allocate and return a consumer relayd socket.
495 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
498 struct consumer_relayd_sock_pair
*obj
= NULL
;
500 /* Negative net sequence index is a failure */
501 if (net_seq_idx
< 0) {
505 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
507 PERROR("zmalloc relayd sock");
511 obj
->net_seq_idx
= net_seq_idx
;
513 obj
->destroy_flag
= 0;
514 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
515 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
522 * Find a relayd socket pair in the global consumer data.
524 * Return the object if found else NULL.
525 * RCU read-side lock must be held across this call and while using the
528 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
530 struct lttng_ht_iter iter
;
531 struct lttng_ht_node_ulong
*node
;
532 struct consumer_relayd_sock_pair
*relayd
= NULL
;
534 /* Negative keys are lookup failures */
539 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
541 node
= lttng_ht_iter_get_node_ulong(&iter
);
543 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
551 * Handle stream for relayd transmission if the stream applies for network
552 * streaming where the net sequence index is set.
554 * Return destination file descriptor or negative value on error.
556 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
557 size_t data_size
, struct consumer_relayd_sock_pair
*relayd
)
560 struct lttcomm_relayd_data_hdr data_hdr
;
566 /* Reset data header */
567 memset(&data_hdr
, 0, sizeof(data_hdr
));
569 if (stream
->metadata_flag
) {
570 /* Caller MUST acquire the relayd control socket lock */
571 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
576 /* Metadata are always sent on the control socket. */
577 outfd
= relayd
->control_sock
.fd
;
579 /* Set header with stream information */
580 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
581 data_hdr
.data_size
= htobe32(data_size
);
582 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
583 /* Other fields are zeroed previously */
585 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
591 /* Set to go on data socket */
592 outfd
= relayd
->data_sock
.fd
;
600 * Update a stream according to what we just received.
602 void consumer_change_stream_state(int stream_key
,
603 enum lttng_consumer_stream_state state
)
605 struct lttng_consumer_stream
*stream
;
607 pthread_mutex_lock(&consumer_data
.lock
);
608 stream
= consumer_find_stream(stream_key
);
610 stream
->state
= state
;
612 consumer_data
.need_update
= 1;
613 pthread_mutex_unlock(&consumer_data
.lock
);
617 void consumer_free_channel(struct rcu_head
*head
)
619 struct lttng_ht_node_ulong
*node
=
620 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
621 struct lttng_consumer_channel
*channel
=
622 caa_container_of(node
, struct lttng_consumer_channel
, node
);
628 * Remove a channel from the global list protected by a mutex. This
629 * function is also responsible for freeing its data structures.
631 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
634 struct lttng_ht_iter iter
;
636 pthread_mutex_lock(&consumer_data
.lock
);
638 switch (consumer_data
.type
) {
639 case LTTNG_CONSUMER_KERNEL
:
641 case LTTNG_CONSUMER32_UST
:
642 case LTTNG_CONSUMER64_UST
:
643 lttng_ustconsumer_del_channel(channel
);
646 ERR("Unknown consumer_data type");
652 iter
.iter
.node
= &channel
->node
.node
;
653 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
657 if (channel
->mmap_base
!= NULL
) {
658 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
663 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
664 ret
= close(channel
->wait_fd
);
669 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
670 ret
= close(channel
->shm_fd
);
676 call_rcu(&channel
->node
.head
, consumer_free_channel
);
678 pthread_mutex_unlock(&consumer_data
.lock
);
681 struct lttng_consumer_channel
*consumer_allocate_channel(
683 int shm_fd
, int wait_fd
,
685 uint64_t max_sb_size
)
687 struct lttng_consumer_channel
*channel
;
690 channel
= zmalloc(sizeof(*channel
));
691 if (channel
== NULL
) {
692 perror("malloc struct lttng_consumer_channel");
695 channel
->key
= channel_key
;
696 channel
->shm_fd
= shm_fd
;
697 channel
->wait_fd
= wait_fd
;
698 channel
->mmap_len
= mmap_len
;
699 channel
->max_sb_size
= max_sb_size
;
700 channel
->refcount
= 0;
701 channel
->nr_streams
= 0;
702 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
704 switch (consumer_data
.type
) {
705 case LTTNG_CONSUMER_KERNEL
:
706 channel
->mmap_base
= NULL
;
707 channel
->mmap_len
= 0;
709 case LTTNG_CONSUMER32_UST
:
710 case LTTNG_CONSUMER64_UST
:
711 ret
= lttng_ustconsumer_allocate_channel(channel
);
718 ERR("Unknown consumer_data type");
722 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
723 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
724 (unsigned long long) channel
->mmap_len
,
725 (unsigned long long) channel
->max_sb_size
);
731 * Add a channel to the global list protected by a mutex.
733 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
735 struct lttng_ht_node_ulong
*node
;
736 struct lttng_ht_iter iter
;
738 pthread_mutex_lock(&consumer_data
.lock
);
739 /* Steal channel identifier, for UST */
740 consumer_steal_channel_key(channel
->key
);
743 lttng_ht_lookup(consumer_data
.channel_ht
,
744 (void *)((unsigned long) channel
->key
), &iter
);
745 node
= lttng_ht_iter_get_node_ulong(&iter
);
747 /* Channel already exist. Ignore the insertion */
751 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
755 pthread_mutex_unlock(&consumer_data
.lock
);
761 * Allocate the pollfd structure and the local view of the out fds to avoid
762 * doing a lookup in the linked list and concurrency issues when writing is
763 * needed. Called with consumer_data.lock held.
765 * Returns the number of fds in the structures.
767 int consumer_update_poll_array(
768 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
769 struct lttng_consumer_stream
**local_stream
,
770 struct lttng_ht
*metadata_ht
)
773 struct lttng_ht_iter iter
;
774 struct lttng_consumer_stream
*stream
;
776 DBG("Updating poll fd array");
778 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, stream
,
780 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
) {
783 DBG("Active FD %d", stream
->wait_fd
);
784 (*pollfd
)[i
].fd
= stream
->wait_fd
;
785 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
786 if (stream
->metadata_flag
&& metadata_ht
) {
787 lttng_ht_add_unique_ulong(metadata_ht
, &stream
->waitfd_node
);
788 DBG("Active FD added to metadata hash table");
790 local_stream
[i
] = stream
;
796 * Insert the consumer_poll_pipe at the end of the array and don't
797 * increment i so nb_fd is the number of real FD.
799 (*pollfd
)[i
].fd
= ctx
->consumer_poll_pipe
[0];
800 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
805 * Poll on the should_quit pipe and the command socket return -1 on error and
806 * should exit, 0 if data is available on the command socket
808 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
813 num_rdy
= poll(consumer_sockpoll
, 2, -1);
816 * Restart interrupted system call.
818 if (errno
== EINTR
) {
821 perror("Poll error");
824 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
825 DBG("consumer_should_quit wake up");
835 * Set the error socket.
837 void lttng_consumer_set_error_sock(
838 struct lttng_consumer_local_data
*ctx
, int sock
)
840 ctx
->consumer_error_socket
= sock
;
844 * Set the command socket path.
846 void lttng_consumer_set_command_sock_path(
847 struct lttng_consumer_local_data
*ctx
, char *sock
)
849 ctx
->consumer_command_sock_path
= sock
;
853 * Send return code to the session daemon.
854 * If the socket is not defined, we return 0, it is not a fatal error
856 int lttng_consumer_send_error(
857 struct lttng_consumer_local_data
*ctx
, int cmd
)
859 if (ctx
->consumer_error_socket
> 0) {
860 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
861 sizeof(enum lttcomm_sessiond_command
));
868 * Close all the tracefiles and stream fds, should be called when all instances
871 void lttng_consumer_cleanup(void)
873 struct lttng_ht_iter iter
;
874 struct lttng_ht_node_ulong
*node
;
879 * close all outfd. Called when there are no more threads running (after
880 * joining on the threads), no need to protect list iteration with mutex.
882 cds_lfht_for_each_entry(consumer_data
.stream_ht
->ht
, &iter
.iter
, node
,
884 struct lttng_consumer_stream
*stream
=
885 caa_container_of(node
, struct lttng_consumer_stream
, node
);
886 consumer_del_stream(stream
);
889 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
891 struct lttng_consumer_channel
*channel
=
892 caa_container_of(node
, struct lttng_consumer_channel
, node
);
893 consumer_del_channel(channel
);
898 lttng_ht_destroy(consumer_data
.stream_ht
);
899 lttng_ht_destroy(consumer_data
.channel_ht
);
903 * Called from signal handler.
905 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
910 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
911 } while (ret
< 0 && errno
== EINTR
);
913 perror("write consumer quit");
917 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
920 int outfd
= stream
->out_fd
;
923 * This does a blocking write-and-wait on any page that belongs to the
924 * subbuffer prior to the one we just wrote.
925 * Don't care about error values, as these are just hints and ways to
926 * limit the amount of page cache used.
928 if (orig_offset
< stream
->chan
->max_sb_size
) {
931 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
932 stream
->chan
->max_sb_size
,
933 SYNC_FILE_RANGE_WAIT_BEFORE
934 | SYNC_FILE_RANGE_WRITE
935 | SYNC_FILE_RANGE_WAIT_AFTER
);
937 * Give hints to the kernel about how we access the file:
938 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
941 * We need to call fadvise again after the file grows because the
942 * kernel does not seem to apply fadvise to non-existing parts of the
945 * Call fadvise _after_ having waited for the page writeback to
946 * complete because the dirty page writeback semantic is not well
947 * defined. So it can be expected to lead to lower throughput in
950 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
951 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
955 * Initialise the necessary environnement :
956 * - create a new context
957 * - create the poll_pipe
958 * - create the should_quit pipe (for signal handler)
959 * - create the thread pipe (for splice)
961 * Takes a function pointer as argument, this function is called when data is
962 * available on a buffer. This function is responsible to do the
963 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
964 * buffer configuration and then kernctl_put_next_subbuf at the end.
966 * Returns a pointer to the new context or NULL on error.
968 struct lttng_consumer_local_data
*lttng_consumer_create(
969 enum lttng_consumer_type type
,
970 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
971 struct lttng_consumer_local_data
*ctx
),
972 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
973 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
974 int (*update_stream
)(int stream_key
, uint32_t state
))
977 struct lttng_consumer_local_data
*ctx
;
979 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
980 consumer_data
.type
== type
);
981 consumer_data
.type
= type
;
983 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
985 perror("allocating context");
989 ctx
->consumer_error_socket
= -1;
990 /* assign the callbacks */
991 ctx
->on_buffer_ready
= buffer_ready
;
992 ctx
->on_recv_channel
= recv_channel
;
993 ctx
->on_recv_stream
= recv_stream
;
994 ctx
->on_update_stream
= update_stream
;
996 ret
= pipe(ctx
->consumer_poll_pipe
);
998 perror("Error creating poll pipe");
999 goto error_poll_pipe
;
1002 /* set read end of the pipe to non-blocking */
1003 ret
= fcntl(ctx
->consumer_poll_pipe
[0], F_SETFL
, O_NONBLOCK
);
1005 perror("fcntl O_NONBLOCK");
1006 goto error_poll_fcntl
;
1009 /* set write end of the pipe to non-blocking */
1010 ret
= fcntl(ctx
->consumer_poll_pipe
[1], F_SETFL
, O_NONBLOCK
);
1012 perror("fcntl O_NONBLOCK");
1013 goto error_poll_fcntl
;
1016 ret
= pipe(ctx
->consumer_should_quit
);
1018 perror("Error creating recv pipe");
1019 goto error_quit_pipe
;
1022 ret
= pipe(ctx
->consumer_thread_pipe
);
1024 perror("Error creating thread pipe");
1025 goto error_thread_pipe
;
1032 for (i
= 0; i
< 2; i
++) {
1035 err
= close(ctx
->consumer_should_quit
[i
]);
1042 for (i
= 0; i
< 2; i
++) {
1045 err
= close(ctx
->consumer_poll_pipe
[i
]);
1057 * Close all fds associated with the instance and free the context.
1059 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1063 ret
= close(ctx
->consumer_error_socket
);
1067 ret
= close(ctx
->consumer_thread_pipe
[0]);
1071 ret
= close(ctx
->consumer_thread_pipe
[1]);
1075 ret
= close(ctx
->consumer_poll_pipe
[0]);
1079 ret
= close(ctx
->consumer_poll_pipe
[1]);
1083 ret
= close(ctx
->consumer_should_quit
[0]);
1087 ret
= close(ctx
->consumer_should_quit
[1]);
1091 unlink(ctx
->consumer_command_sock_path
);
1096 * Write the metadata stream id on the specified file descriptor.
1098 static int write_relayd_metadata_id(int fd
,
1099 struct lttng_consumer_stream
*stream
,
1100 struct consumer_relayd_sock_pair
*relayd
)
1103 uint64_t metadata_id
;
1105 metadata_id
= htobe64(stream
->relayd_stream_id
);
1107 ret
= write(fd
, (void *) &metadata_id
,
1108 sizeof(stream
->relayd_stream_id
));
1109 } while (ret
< 0 && errno
== EINTR
);
1111 PERROR("write metadata stream id");
1114 DBG("Metadata stream id %" PRIu64
" written before data",
1115 stream
->relayd_stream_id
);
1122 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1123 * core function for writing trace buffers to either the local filesystem or
1126 * Careful review MUST be put if any changes occur!
1128 * Returns the number of bytes written
1130 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1131 struct lttng_consumer_local_data
*ctx
,
1132 struct lttng_consumer_stream
*stream
, unsigned long len
)
1134 unsigned long mmap_offset
;
1135 ssize_t ret
= 0, written
= 0;
1136 off_t orig_offset
= stream
->out_fd_offset
;
1137 /* Default is on the disk */
1138 int outfd
= stream
->out_fd
;
1139 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1141 /* RCU lock for the relayd pointer */
1144 /* Flag that the current stream if set for network streaming. */
1145 if (stream
->net_seq_idx
!= -1) {
1146 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1147 if (relayd
== NULL
) {
1152 /* get the offset inside the fd to mmap */
1153 switch (consumer_data
.type
) {
1154 case LTTNG_CONSUMER_KERNEL
:
1155 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1157 case LTTNG_CONSUMER32_UST
:
1158 case LTTNG_CONSUMER64_UST
:
1159 ret
= lttng_ustctl_get_mmap_read_offset(stream
->chan
->handle
,
1160 stream
->buf
, &mmap_offset
);
1163 ERR("Unknown consumer_data type");
1168 PERROR("tracer ctl get_mmap_read_offset");
1173 /* Handle stream on the relayd if the output is on the network */
1175 unsigned long netlen
= len
;
1178 * Lock the control socket for the complete duration of the function
1179 * since from this point on we will use the socket.
1181 if (stream
->metadata_flag
) {
1182 /* Metadata requires the control socket. */
1183 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1184 netlen
+= sizeof(stream
->relayd_stream_id
);
1187 ret
= write_relayd_stream_header(stream
, netlen
, relayd
);
1189 /* Use the returned socket. */
1192 /* Write metadata stream id before payload */
1193 if (stream
->metadata_flag
) {
1194 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
);
1201 /* Else, use the default set before which is the filesystem. */
1206 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
1207 } while (ret
< 0 && errno
== EINTR
);
1209 PERROR("Error in file write");
1214 } else if (ret
> len
) {
1215 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1222 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1224 /* This call is useless on a socket so better save a syscall. */
1226 /* This won't block, but will start writeout asynchronously */
1227 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1228 SYNC_FILE_RANGE_WRITE
);
1229 stream
->out_fd_offset
+= ret
;
1233 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1236 /* Unlock only if ctrl socket used */
1237 if (relayd
&& stream
->metadata_flag
) {
1238 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1246 * Splice the data from the ring buffer to the tracefile.
1248 * Returns the number of bytes spliced.
1250 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1251 struct lttng_consumer_local_data
*ctx
,
1252 struct lttng_consumer_stream
*stream
, unsigned long len
)
1254 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1256 off_t orig_offset
= stream
->out_fd_offset
;
1257 int fd
= stream
->wait_fd
;
1258 /* Default is on the disk */
1259 int outfd
= stream
->out_fd
;
1260 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1262 switch (consumer_data
.type
) {
1263 case LTTNG_CONSUMER_KERNEL
:
1265 case LTTNG_CONSUMER32_UST
:
1266 case LTTNG_CONSUMER64_UST
:
1267 /* Not supported for user space tracing */
1270 ERR("Unknown consumer_data type");
1274 /* RCU lock for the relayd pointer */
1277 /* Flag that the current stream if set for network streaming. */
1278 if (stream
->net_seq_idx
!= -1) {
1279 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1280 if (relayd
== NULL
) {
1285 /* Write metadata stream id before payload */
1286 if (stream
->metadata_flag
&& relayd
) {
1288 * Lock the control socket for the complete duration of the function
1289 * since from this point on we will use the socket.
1291 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1293 ret
= write_relayd_metadata_id(ctx
->consumer_thread_pipe
[1],
1302 DBG("splice chan to pipe offset %lu of len %lu (fd : %d)",
1303 (unsigned long)offset
, len
, fd
);
1304 ret_splice
= splice(fd
, &offset
, ctx
->consumer_thread_pipe
[1], NULL
, len
,
1305 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1306 DBG("splice chan to pipe, ret %zd", ret_splice
);
1307 if (ret_splice
< 0) {
1308 PERROR("Error in relay splice");
1310 written
= ret_splice
;
1316 /* Handle stream on the relayd if the output is on the network */
1318 if (stream
->metadata_flag
) {
1319 /* Update counter to fit the spliced data */
1320 ret_splice
+= sizeof(stream
->relayd_stream_id
);
1321 len
+= sizeof(stream
->relayd_stream_id
);
1323 * We do this so the return value can match the len passed as
1324 * argument to this function.
1326 written
-= sizeof(stream
->relayd_stream_id
);
1329 ret
= write_relayd_stream_header(stream
, ret_splice
, relayd
);
1331 /* Use the returned socket. */
1334 ERR("Remote relayd disconnected. Stopping");
1339 /* Splice data out */
1340 ret_splice
= splice(ctx
->consumer_thread_pipe
[0], NULL
, outfd
, NULL
,
1341 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1342 DBG("Kernel consumer splice pipe to file, ret %zd", ret_splice
);
1343 if (ret_splice
< 0) {
1344 PERROR("Error in file splice");
1346 written
= ret_splice
;
1350 } else if (ret_splice
> len
) {
1352 PERROR("Wrote more data than requested %zd (len: %lu)",
1354 written
+= ret_splice
;
1360 /* This call is useless on a socket so better save a syscall. */
1362 /* This won't block, but will start writeout asynchronously */
1363 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1364 SYNC_FILE_RANGE_WRITE
);
1365 stream
->out_fd_offset
+= ret_splice
;
1367 written
+= ret_splice
;
1369 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1376 /* send the appropriate error description to sessiond */
1379 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_EBADF
);
1382 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_EINVAL
);
1385 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_ENOMEM
);
1388 lttng_consumer_send_error(ctx
, CONSUMERD_SPLICE_ESPIPE
);
1393 if (relayd
&& stream
->metadata_flag
) {
1394 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1402 * Take a snapshot for a specific fd
1404 * Returns 0 on success, < 0 on error
1406 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1407 struct lttng_consumer_stream
*stream
)
1409 switch (consumer_data
.type
) {
1410 case LTTNG_CONSUMER_KERNEL
:
1411 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1412 case LTTNG_CONSUMER32_UST
:
1413 case LTTNG_CONSUMER64_UST
:
1414 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1416 ERR("Unknown consumer_data type");
1424 * Get the produced position
1426 * Returns 0 on success, < 0 on error
1428 int lttng_consumer_get_produced_snapshot(
1429 struct lttng_consumer_local_data
*ctx
,
1430 struct lttng_consumer_stream
*stream
,
1433 switch (consumer_data
.type
) {
1434 case LTTNG_CONSUMER_KERNEL
:
1435 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1436 case LTTNG_CONSUMER32_UST
:
1437 case LTTNG_CONSUMER64_UST
:
1438 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1440 ERR("Unknown consumer_data type");
1446 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1447 int sock
, struct pollfd
*consumer_sockpoll
)
1449 switch (consumer_data
.type
) {
1450 case LTTNG_CONSUMER_KERNEL
:
1451 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1452 case LTTNG_CONSUMER32_UST
:
1453 case LTTNG_CONSUMER64_UST
:
1454 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1456 ERR("Unknown consumer_data type");
1463 * This thread polls the fds in the set to consume the data and write
1464 * it to tracefile if necessary.
1466 void *lttng_consumer_thread_poll_fds(void *data
)
1468 int num_rdy
, num_hup
, high_prio
, ret
, i
;
1469 struct pollfd
*pollfd
= NULL
;
1470 /* local view of the streams */
1471 struct lttng_consumer_stream
**local_stream
= NULL
;
1472 /* local view of consumer_data.fds_count */
1474 struct lttng_consumer_local_data
*ctx
= data
;
1475 struct lttng_ht
*metadata_ht
;
1476 struct lttng_ht_iter iter
;
1477 struct lttng_ht_node_ulong
*node
;
1478 struct lttng_consumer_stream
*metadata_stream
;
1481 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1483 rcu_register_thread();
1485 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
1492 * the fds set has been updated, we need to update our
1493 * local array as well
1495 pthread_mutex_lock(&consumer_data
.lock
);
1496 if (consumer_data
.need_update
) {
1497 if (pollfd
!= NULL
) {
1501 if (local_stream
!= NULL
) {
1503 local_stream
= NULL
;
1506 /* allocate for all fds + 1 for the consumer_poll_pipe */
1507 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
1508 if (pollfd
== NULL
) {
1509 perror("pollfd malloc");
1510 pthread_mutex_unlock(&consumer_data
.lock
);
1514 /* allocate for all fds + 1 for the consumer_poll_pipe */
1515 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
1516 sizeof(struct lttng_consumer_stream
));
1517 if (local_stream
== NULL
) {
1518 perror("local_stream malloc");
1519 pthread_mutex_unlock(&consumer_data
.lock
);
1522 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
,
1525 ERR("Error in allocating pollfd or local_outfds");
1526 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
1527 pthread_mutex_unlock(&consumer_data
.lock
);
1531 consumer_data
.need_update
= 0;
1533 pthread_mutex_unlock(&consumer_data
.lock
);
1535 /* No FDs and consumer_quit, consumer_cleanup the thread */
1536 if (nb_fd
== 0 && consumer_quit
== 1) {
1539 /* poll on the array of fds */
1541 DBG("polling on %d fd", nb_fd
+ 1);
1542 num_rdy
= poll(pollfd
, nb_fd
+ 1, consumer_poll_timeout
);
1543 DBG("poll num_rdy : %d", num_rdy
);
1544 if (num_rdy
== -1) {
1546 * Restart interrupted system call.
1548 if (errno
== EINTR
) {
1551 perror("Poll error");
1552 lttng_consumer_send_error(ctx
, CONSUMERD_POLL_ERROR
);
1554 } else if (num_rdy
== 0) {
1555 DBG("Polling thread timed out");
1560 * If the consumer_poll_pipe triggered poll go directly to the
1561 * beginning of the loop to update the array. We want to prioritize
1562 * array update over low-priority reads.
1564 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
1565 size_t pipe_readlen
;
1568 DBG("consumer_poll_pipe wake up");
1569 /* Consume 1 byte of pipe data */
1571 pipe_readlen
= read(ctx
->consumer_poll_pipe
[0], &tmp
, 1);
1572 } while (pipe_readlen
== -1 && errno
== EINTR
);
1576 /* Take care of high priority channels first. */
1577 for (i
= 0; i
< nb_fd
; i
++) {
1578 /* Lookup for metadata which is the highest priority */
1579 lttng_ht_lookup(metadata_ht
,
1580 (void *)((unsigned long) pollfd
[i
].fd
), &iter
);
1581 node
= lttng_ht_iter_get_node_ulong(&iter
);
1583 (pollfd
[i
].revents
& (POLLIN
| POLLPRI
))) {
1584 DBG("Urgent metadata read on fd %d", pollfd
[i
].fd
);
1585 metadata_stream
= caa_container_of(node
,
1586 struct lttng_consumer_stream
, waitfd_node
);
1588 len
= ctx
->on_buffer_ready(metadata_stream
, ctx
);
1589 /* it's ok to have an unavailable sub-buffer */
1590 if (len
< 0 && len
!= -EAGAIN
) {
1592 } else if (len
> 0) {
1593 metadata_stream
->data_read
= 1;
1595 } else if (pollfd
[i
].revents
& POLLPRI
) {
1596 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
1598 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1599 /* it's ok to have an unavailable sub-buffer */
1600 if (len
< 0 && len
!= -EAGAIN
) {
1602 } else if (len
> 0) {
1603 local_stream
[i
]->data_read
= 1;
1609 * If we read high prio channel in this loop, try again
1610 * for more high prio data.
1616 /* Take care of low priority channels. */
1617 for (i
= 0; i
< nb_fd
; i
++) {
1618 if ((pollfd
[i
].revents
& POLLIN
) ||
1619 local_stream
[i
]->hangup_flush_done
) {
1620 DBG("Normal read on fd %d", pollfd
[i
].fd
);
1621 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
1622 /* it's ok to have an unavailable sub-buffer */
1623 if (len
< 0 && len
!= -EAGAIN
) {
1625 } else if (len
> 0) {
1626 local_stream
[i
]->data_read
= 1;
1631 /* Handle hangup and errors */
1632 for (i
= 0; i
< nb_fd
; i
++) {
1633 if (!local_stream
[i
]->hangup_flush_done
1634 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
1635 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
1636 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
1637 DBG("fd %d is hup|err|nval. Attempting flush and read.",
1639 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
1640 /* Attempt read again, for the data we just flushed. */
1641 local_stream
[i
]->data_read
= 1;
1644 * If the poll flag is HUP/ERR/NVAL and we have
1645 * read no data in this pass, we can remove the
1646 * stream from its hash table.
1648 if ((pollfd
[i
].revents
& POLLHUP
)) {
1649 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
1650 if (!local_stream
[i
]->data_read
) {
1651 if (local_stream
[i
]->metadata_flag
) {
1652 iter
.iter
.node
= &local_stream
[i
]->waitfd_node
.node
;
1653 ret
= lttng_ht_del(metadata_ht
, &iter
);
1656 consumer_del_stream(local_stream
[i
]);
1659 } else if (pollfd
[i
].revents
& POLLERR
) {
1660 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
1661 if (!local_stream
[i
]->data_read
) {
1662 if (local_stream
[i
]->metadata_flag
) {
1663 iter
.iter
.node
= &local_stream
[i
]->waitfd_node
.node
;
1664 ret
= lttng_ht_del(metadata_ht
, &iter
);
1667 consumer_del_stream(local_stream
[i
]);
1670 } else if (pollfd
[i
].revents
& POLLNVAL
) {
1671 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
1672 if (!local_stream
[i
]->data_read
) {
1673 if (local_stream
[i
]->metadata_flag
) {
1674 iter
.iter
.node
= &local_stream
[i
]->waitfd_node
.node
;
1675 ret
= lttng_ht_del(metadata_ht
, &iter
);
1678 consumer_del_stream(local_stream
[i
]);
1682 local_stream
[i
]->data_read
= 0;
1686 DBG("polling thread exiting");
1687 if (pollfd
!= NULL
) {
1691 if (local_stream
!= NULL
) {
1693 local_stream
= NULL
;
1695 rcu_unregister_thread();
1700 * This thread listens on the consumerd socket and receives the file
1701 * descriptors from the session daemon.
1703 void *lttng_consumer_thread_receive_fds(void *data
)
1705 int sock
, client_socket
, ret
;
1707 * structure to poll for incoming data on communication socket avoids
1708 * making blocking sockets.
1710 struct pollfd consumer_sockpoll
[2];
1711 struct lttng_consumer_local_data
*ctx
= data
;
1713 rcu_register_thread();
1715 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
1716 unlink(ctx
->consumer_command_sock_path
);
1717 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
1718 if (client_socket
< 0) {
1719 ERR("Cannot create command socket");
1723 ret
= lttcomm_listen_unix_sock(client_socket
);
1728 DBG("Sending ready command to lttng-sessiond");
1729 ret
= lttng_consumer_send_error(ctx
, CONSUMERD_COMMAND_SOCK_READY
);
1730 /* return < 0 on error, but == 0 is not fatal */
1732 ERR("Error sending ready command to lttng-sessiond");
1736 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
1738 perror("fcntl O_NONBLOCK");
1742 /* prepare the FDs to poll : to client socket and the should_quit pipe */
1743 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
1744 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
1745 consumer_sockpoll
[1].fd
= client_socket
;
1746 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1748 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1751 DBG("Connection on client_socket");
1753 /* Blocking call, waiting for transmission */
1754 sock
= lttcomm_accept_unix_sock(client_socket
);
1759 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
1761 perror("fcntl O_NONBLOCK");
1765 /* update the polling structure to poll on the established socket */
1766 consumer_sockpoll
[1].fd
= sock
;
1767 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
1770 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1773 DBG("Incoming command on sock");
1774 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1775 if (ret
== -ENOENT
) {
1776 DBG("Received STOP command");
1780 ERR("Communication interrupted on command socket");
1783 if (consumer_quit
) {
1784 DBG("consumer_thread_receive_fds received quit from signal");
1787 DBG("received fds on sock");
1790 DBG("consumer_thread_receive_fds exiting");
1793 * when all fds have hung up, the polling thread
1799 * 2s of grace period, if no polling events occur during
1800 * this period, the polling thread will exit even if there
1801 * are still open FDs (should not happen, but safety mechanism).
1803 consumer_poll_timeout
= LTTNG_CONSUMER_POLL_TIMEOUT
;
1806 * Wake-up the other end by writing a null byte in the pipe
1807 * (non-blocking). Important note: Because writing into the
1808 * pipe is non-blocking (and therefore we allow dropping wakeup
1809 * data, as long as there is wakeup data present in the pipe
1810 * buffer to wake up the other end), the other end should
1811 * perform the following sequence for waiting:
1812 * 1) empty the pipe (reads).
1813 * 2) perform update operation.
1814 * 3) wait on the pipe (poll).
1817 ret
= write(ctx
->consumer_poll_pipe
[1], "", 1);
1818 } while (ret
< 0 && errno
== EINTR
);
1819 rcu_unregister_thread();
1823 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1824 struct lttng_consumer_local_data
*ctx
)
1826 switch (consumer_data
.type
) {
1827 case LTTNG_CONSUMER_KERNEL
:
1828 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
1829 case LTTNG_CONSUMER32_UST
:
1830 case LTTNG_CONSUMER64_UST
:
1831 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
1833 ERR("Unknown consumer_data type");
1839 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1841 switch (consumer_data
.type
) {
1842 case LTTNG_CONSUMER_KERNEL
:
1843 return lttng_kconsumer_on_recv_stream(stream
);
1844 case LTTNG_CONSUMER32_UST
:
1845 case LTTNG_CONSUMER64_UST
:
1846 return lttng_ustconsumer_on_recv_stream(stream
);
1848 ERR("Unknown consumer_data type");
1855 * Allocate and set consumer data hash tables.
1857 void lttng_consumer_init(void)
1859 consumer_data
.stream_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1860 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1861 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
1865 * Process the ADD_RELAYD command receive by a consumer.
1867 * This will create a relayd socket pair and add it to the relayd hash table.
1868 * The caller MUST acquire a RCU read side lock before calling it.
1870 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
1871 struct lttng_consumer_local_data
*ctx
, int sock
,
1872 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
)
1875 struct consumer_relayd_sock_pair
*relayd
;
1877 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
1879 /* Get relayd reference if exists. */
1880 relayd
= consumer_find_relayd(net_seq_idx
);
1881 if (relayd
== NULL
) {
1882 /* Not found. Allocate one. */
1883 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
1884 if (relayd
== NULL
) {
1885 lttng_consumer_send_error(ctx
, CONSUMERD_OUTFD_ERROR
);
1890 /* Poll on consumer socket. */
1891 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1896 /* Get relayd socket from session daemon */
1897 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
1898 if (ret
!= sizeof(fd
)) {
1899 lttng_consumer_send_error(ctx
, CONSUMERD_ERROR_RECV_FD
);
1904 /* Copy socket information and received FD */
1905 switch (sock_type
) {
1906 case LTTNG_STREAM_CONTROL
:
1907 /* Copy received lttcomm socket */
1908 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
1909 ret
= lttcomm_create_sock(&relayd
->control_sock
);
1914 /* Close the created socket fd which is useless */
1915 close(relayd
->control_sock
.fd
);
1917 /* Assign new file descriptor */
1918 relayd
->control_sock
.fd
= fd
;
1920 case LTTNG_STREAM_DATA
:
1921 /* Copy received lttcomm socket */
1922 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
1923 ret
= lttcomm_create_sock(&relayd
->data_sock
);
1928 /* Close the created socket fd which is useless */
1929 close(relayd
->data_sock
.fd
);
1931 /* Assign new file descriptor */
1932 relayd
->data_sock
.fd
= fd
;
1935 ERR("Unknown relayd socket type (%d)", sock_type
);
1939 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
1940 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
1941 relayd
->net_seq_idx
, fd
);
1944 * Add relayd socket pair to consumer data hashtable. If object already
1945 * exists or on error, the function gracefully returns.
1947 consumer_add_relayd(relayd
);