2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2012 - David Goulet <dgoulet@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/socket.h>
28 #include <sys/types.h>
32 #include <common/common.h>
33 #include <common/utils.h>
34 #include <common/compat/poll.h>
35 #include <common/kernel-ctl/kernel-ctl.h>
36 #include <common/sessiond-comm/relayd.h>
37 #include <common/sessiond-comm/sessiond-comm.h>
38 #include <common/kernel-consumer/kernel-consumer.h>
39 #include <common/relayd/relayd.h>
40 #include <common/ust-consumer/ust-consumer.h>
44 struct lttng_consumer_global_data consumer_data
= {
47 .type
= LTTNG_CONSUMER_UNKNOWN
,
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
;
59 * Global hash table containing respectively metadata and data streams. The
60 * stream element in this ht should only be updated by the metadata poll thread
61 * for the metadata and the data poll thread for the data.
63 static struct lttng_ht
*metadata_ht
;
64 static struct lttng_ht
*data_ht
;
67 * Notify a thread pipe to poll back again. This usually means that some global
68 * state has changed so we just send back the thread in a poll wait call.
70 static void notify_thread_pipe(int wpipe
)
75 struct lttng_consumer_stream
*null_stream
= NULL
;
77 ret
= write(wpipe
, &null_stream
, sizeof(null_stream
));
78 } while (ret
< 0 && errno
== EINTR
);
82 * Find a stream. The consumer_data.lock must be locked during this
85 static struct lttng_consumer_stream
*consumer_find_stream(int key
,
88 struct lttng_ht_iter iter
;
89 struct lttng_ht_node_ulong
*node
;
90 struct lttng_consumer_stream
*stream
= NULL
;
94 /* Negative keys are lookup failures */
101 lttng_ht_lookup(ht
, (void *)((unsigned long) key
), &iter
);
102 node
= lttng_ht_iter_get_node_ulong(&iter
);
104 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
112 void consumer_steal_stream_key(int key
, struct lttng_ht
*ht
)
114 struct lttng_consumer_stream
*stream
;
117 stream
= consumer_find_stream(key
, ht
);
121 * We don't want the lookup to match, but we still need
122 * to iterate on this stream when iterating over the hash table. Just
123 * change the node key.
125 stream
->node
.key
= -1;
130 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
132 struct lttng_ht_iter iter
;
133 struct lttng_ht_node_ulong
*node
;
134 struct lttng_consumer_channel
*channel
= NULL
;
136 /* Negative keys are lookup failures */
143 lttng_ht_lookup(consumer_data
.channel_ht
, (void *)((unsigned long) key
),
145 node
= lttng_ht_iter_get_node_ulong(&iter
);
147 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
155 static void consumer_steal_channel_key(int key
)
157 struct lttng_consumer_channel
*channel
;
160 channel
= consumer_find_channel(key
);
164 * We don't want the lookup to match, but we still need
165 * to iterate on this channel when iterating over the hash table. Just
166 * change the node key.
168 channel
->node
.key
= -1;
174 void consumer_free_stream(struct rcu_head
*head
)
176 struct lttng_ht_node_ulong
*node
=
177 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
178 struct lttng_consumer_stream
*stream
=
179 caa_container_of(node
, struct lttng_consumer_stream
, node
);
185 * RCU protected relayd socket pair free.
187 static void consumer_rcu_free_relayd(struct rcu_head
*head
)
189 struct lttng_ht_node_ulong
*node
=
190 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
191 struct consumer_relayd_sock_pair
*relayd
=
192 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
195 * Close all sockets. This is done in the call RCU since we don't want the
196 * socket fds to be reassigned thus potentially creating bad state of the
199 * We do not have to lock the control socket mutex here since at this stage
200 * there is no one referencing to this relayd object.
202 (void) relayd_close(&relayd
->control_sock
);
203 (void) relayd_close(&relayd
->data_sock
);
209 * Destroy and free relayd socket pair object.
211 * This function MUST be called with the consumer_data lock acquired.
213 static void destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
216 struct lttng_ht_iter iter
;
218 if (relayd
== NULL
) {
222 DBG("Consumer destroy and close relayd socket pair");
224 iter
.iter
.node
= &relayd
->node
.node
;
225 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
227 /* We assume the relayd is being or is destroyed */
231 /* RCU free() call */
232 call_rcu(&relayd
->node
.head
, consumer_rcu_free_relayd
);
236 * Update the end point status of all streams having the given network sequence
237 * index (relayd index).
239 * It's atomically set without having the stream mutex locked which is fine
240 * because we handle the write/read race with a pipe wakeup for each thread.
242 static void update_endpoint_status_by_netidx(int net_seq_idx
,
243 enum consumer_endpoint_status status
)
245 struct lttng_ht_iter iter
;
246 struct lttng_consumer_stream
*stream
;
248 DBG("Consumer set delete flag on stream by idx %d", net_seq_idx
);
252 /* Let's begin with metadata */
253 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
254 if (stream
->net_seq_idx
== net_seq_idx
) {
255 uatomic_set(&stream
->endpoint_status
, status
);
256 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
260 /* Follow up by the data streams */
261 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
262 if (stream
->net_seq_idx
== net_seq_idx
) {
263 uatomic_set(&stream
->endpoint_status
, status
);
264 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
271 * Cleanup a relayd object by flagging every associated streams for deletion,
272 * destroying the object meaning removing it from the relayd hash table,
273 * closing the sockets and freeing the memory in a RCU call.
275 * If a local data context is available, notify the threads that the streams'
276 * state have changed.
278 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
279 struct lttng_consumer_local_data
*ctx
)
285 DBG("Cleaning up relayd sockets");
287 /* Save the net sequence index before destroying the object */
288 netidx
= relayd
->net_seq_idx
;
291 * Delete the relayd from the relayd hash table, close the sockets and free
292 * the object in a RCU call.
294 destroy_relayd(relayd
);
296 /* Set inactive endpoint to all streams */
297 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
300 * With a local data context, notify the threads that the streams' state
301 * have changed. The write() action on the pipe acts as an "implicit"
302 * memory barrier ordering the updates of the end point status from the
303 * read of this status which happens AFTER receiving this notify.
306 notify_thread_pipe(ctx
->consumer_data_pipe
[1]);
307 notify_thread_pipe(ctx
->consumer_metadata_pipe
[1]);
312 * Flag a relayd socket pair for destruction. Destroy it if the refcount
315 * RCU read side lock MUST be aquired before calling this function.
317 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
321 /* Set destroy flag for this object */
322 uatomic_set(&relayd
->destroy_flag
, 1);
324 /* Destroy the relayd if refcount is 0 */
325 if (uatomic_read(&relayd
->refcount
) == 0) {
326 destroy_relayd(relayd
);
331 * Remove a stream from the global list protected by a mutex. This
332 * function is also responsible for freeing its data structures.
334 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
338 struct lttng_ht_iter iter
;
339 struct lttng_consumer_channel
*free_chan
= NULL
;
340 struct consumer_relayd_sock_pair
*relayd
;
344 DBG("Consumer del stream %d", stream
->wait_fd
);
347 /* Means the stream was allocated but not successfully added */
351 pthread_mutex_lock(&stream
->lock
);
352 pthread_mutex_lock(&consumer_data
.lock
);
354 switch (consumer_data
.type
) {
355 case LTTNG_CONSUMER_KERNEL
:
356 if (stream
->mmap_base
!= NULL
) {
357 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
363 case LTTNG_CONSUMER32_UST
:
364 case LTTNG_CONSUMER64_UST
:
365 lttng_ustconsumer_del_stream(stream
);
368 ERR("Unknown consumer_data type");
374 iter
.iter
.node
= &stream
->node
.node
;
375 ret
= lttng_ht_del(ht
, &iter
);
378 /* Remove node session id from the consumer_data stream ht */
379 iter
.iter
.node
= &stream
->node_session_id
.node
;
380 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
384 assert(consumer_data
.stream_count
> 0);
385 consumer_data
.stream_count
--;
387 if (stream
->out_fd
>= 0) {
388 ret
= close(stream
->out_fd
);
393 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
394 ret
= close(stream
->wait_fd
);
399 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
400 ret
= close(stream
->shm_fd
);
406 /* Check and cleanup relayd */
408 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
409 if (relayd
!= NULL
) {
410 uatomic_dec(&relayd
->refcount
);
411 assert(uatomic_read(&relayd
->refcount
) >= 0);
413 /* Closing streams requires to lock the control socket. */
414 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
415 ret
= relayd_send_close_stream(&relayd
->control_sock
,
416 stream
->relayd_stream_id
,
417 stream
->next_net_seq_num
- 1);
418 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
420 DBG("Unable to close stream on the relayd. Continuing");
422 * Continue here. There is nothing we can do for the relayd.
423 * Chances are that the relayd has closed the socket so we just
424 * continue cleaning up.
428 /* Both conditions are met, we destroy the relayd. */
429 if (uatomic_read(&relayd
->refcount
) == 0 &&
430 uatomic_read(&relayd
->destroy_flag
)) {
431 destroy_relayd(relayd
);
436 uatomic_dec(&stream
->chan
->refcount
);
437 if (!uatomic_read(&stream
->chan
->refcount
)
438 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
439 free_chan
= stream
->chan
;
443 consumer_data
.need_update
= 1;
444 pthread_mutex_unlock(&consumer_data
.lock
);
445 pthread_mutex_unlock(&stream
->lock
);
448 consumer_del_channel(free_chan
);
452 call_rcu(&stream
->node
.head
, consumer_free_stream
);
455 struct lttng_consumer_stream
*consumer_allocate_stream(
456 int channel_key
, int stream_key
,
457 int shm_fd
, int wait_fd
,
458 enum lttng_consumer_stream_state state
,
460 enum lttng_event_output output
,
461 const char *path_name
,
469 struct lttng_consumer_stream
*stream
;
471 stream
= zmalloc(sizeof(*stream
));
472 if (stream
== NULL
) {
473 PERROR("malloc struct lttng_consumer_stream");
474 *alloc_ret
= -ENOMEM
;
479 * Get stream's channel reference. Needed when adding the stream to the
482 stream
->chan
= consumer_find_channel(channel_key
);
484 *alloc_ret
= -ENOENT
;
485 ERR("Unable to find channel for stream %d", stream_key
);
489 stream
->key
= stream_key
;
490 stream
->shm_fd
= shm_fd
;
491 stream
->wait_fd
= wait_fd
;
493 stream
->out_fd_offset
= 0;
494 stream
->state
= state
;
495 stream
->mmap_len
= mmap_len
;
496 stream
->mmap_base
= NULL
;
497 stream
->output
= output
;
500 stream
->net_seq_idx
= net_index
;
501 stream
->metadata_flag
= metadata_flag
;
502 stream
->session_id
= session_id
;
503 strncpy(stream
->path_name
, path_name
, sizeof(stream
->path_name
));
504 stream
->path_name
[sizeof(stream
->path_name
) - 1] = '\0';
505 pthread_mutex_init(&stream
->lock
, NULL
);
508 * Index differently the metadata node because the thread is using an
509 * internal hash table to match streams in the metadata_ht to the epoll set
513 lttng_ht_node_init_ulong(&stream
->node
, stream
->wait_fd
);
515 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
518 /* Init session id node with the stream session id */
519 lttng_ht_node_init_ulong(&stream
->node_session_id
, stream
->session_id
);
522 * The cpu number is needed before using any ustctl_* actions. Ignored for
523 * the kernel so the value does not matter.
525 pthread_mutex_lock(&consumer_data
.lock
);
526 stream
->cpu
= stream
->chan
->cpucount
++;
527 pthread_mutex_unlock(&consumer_data
.lock
);
529 DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu,"
530 " out_fd %d, net_seq_idx %d, session_id %" PRIu64
,
531 stream
->path_name
, stream
->key
, stream
->shm_fd
, stream
->wait_fd
,
532 (unsigned long long) stream
->mmap_len
, stream
->out_fd
,
533 stream
->net_seq_idx
, stream
->session_id
);
543 * Add a stream to the global list protected by a mutex.
545 static int consumer_add_stream(struct lttng_consumer_stream
*stream
,
549 struct consumer_relayd_sock_pair
*relayd
;
554 DBG3("Adding consumer stream %d", stream
->key
);
556 pthread_mutex_lock(&consumer_data
.lock
);
557 pthread_mutex_lock(&stream
->lock
);
560 /* Steal stream identifier to avoid having streams with the same key */
561 consumer_steal_stream_key(stream
->key
, ht
);
563 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
566 * Add stream to the stream_list_ht of the consumer data. No need to steal
567 * the key since the HT does not use it and we allow to add redundant keys
570 lttng_ht_add_ulong(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
572 /* Check and cleanup relayd */
573 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
574 if (relayd
!= NULL
) {
575 uatomic_inc(&relayd
->refcount
);
578 /* Update channel refcount once added without error(s). */
579 uatomic_inc(&stream
->chan
->refcount
);
582 * When nb_init_streams reaches 0, we don't need to trigger any action in
583 * terms of destroying the associated channel, because the action that
584 * causes the count to become 0 also causes a stream to be added. The
585 * channel deletion will thus be triggered by the following removal of this
588 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
589 uatomic_dec(&stream
->chan
->nb_init_streams
);
592 /* Update consumer data once the node is inserted. */
593 consumer_data
.stream_count
++;
594 consumer_data
.need_update
= 1;
597 pthread_mutex_unlock(&stream
->lock
);
598 pthread_mutex_unlock(&consumer_data
.lock
);
604 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
605 * be acquired before calling this.
607 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
610 struct lttng_ht_node_ulong
*node
;
611 struct lttng_ht_iter iter
;
613 if (relayd
== NULL
) {
618 lttng_ht_lookup(consumer_data
.relayd_ht
,
619 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
620 node
= lttng_ht_iter_get_node_ulong(&iter
);
622 /* Relayd already exist. Ignore the insertion */
625 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
632 * Allocate and return a consumer relayd socket.
634 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
637 struct consumer_relayd_sock_pair
*obj
= NULL
;
639 /* Negative net sequence index is a failure */
640 if (net_seq_idx
< 0) {
644 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
646 PERROR("zmalloc relayd sock");
650 obj
->net_seq_idx
= net_seq_idx
;
652 obj
->destroy_flag
= 0;
653 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
654 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
661 * Find a relayd socket pair in the global consumer data.
663 * Return the object if found else NULL.
664 * RCU read-side lock must be held across this call and while using the
667 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
669 struct lttng_ht_iter iter
;
670 struct lttng_ht_node_ulong
*node
;
671 struct consumer_relayd_sock_pair
*relayd
= NULL
;
673 /* Negative keys are lookup failures */
678 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
680 node
= lttng_ht_iter_get_node_ulong(&iter
);
682 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
690 * Handle stream for relayd transmission if the stream applies for network
691 * streaming where the net sequence index is set.
693 * Return destination file descriptor or negative value on error.
695 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
696 size_t data_size
, unsigned long padding
,
697 struct consumer_relayd_sock_pair
*relayd
)
700 struct lttcomm_relayd_data_hdr data_hdr
;
706 /* Reset data header */
707 memset(&data_hdr
, 0, sizeof(data_hdr
));
709 if (stream
->metadata_flag
) {
710 /* Caller MUST acquire the relayd control socket lock */
711 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
716 /* Metadata are always sent on the control socket. */
717 outfd
= relayd
->control_sock
.fd
;
719 /* Set header with stream information */
720 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
721 data_hdr
.data_size
= htobe32(data_size
);
722 data_hdr
.padding_size
= htobe32(padding
);
723 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
724 /* Other fields are zeroed previously */
726 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
732 /* Set to go on data socket */
733 outfd
= relayd
->data_sock
.fd
;
741 void consumer_free_channel(struct rcu_head
*head
)
743 struct lttng_ht_node_ulong
*node
=
744 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
745 struct lttng_consumer_channel
*channel
=
746 caa_container_of(node
, struct lttng_consumer_channel
, node
);
752 * Remove a channel from the global list protected by a mutex. This
753 * function is also responsible for freeing its data structures.
755 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
758 struct lttng_ht_iter iter
;
760 pthread_mutex_lock(&consumer_data
.lock
);
762 switch (consumer_data
.type
) {
763 case LTTNG_CONSUMER_KERNEL
:
765 case LTTNG_CONSUMER32_UST
:
766 case LTTNG_CONSUMER64_UST
:
767 lttng_ustconsumer_del_channel(channel
);
770 ERR("Unknown consumer_data type");
776 iter
.iter
.node
= &channel
->node
.node
;
777 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
781 if (channel
->mmap_base
!= NULL
) {
782 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
787 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
788 ret
= close(channel
->wait_fd
);
793 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
794 ret
= close(channel
->shm_fd
);
800 call_rcu(&channel
->node
.head
, consumer_free_channel
);
802 pthread_mutex_unlock(&consumer_data
.lock
);
805 struct lttng_consumer_channel
*consumer_allocate_channel(
807 int shm_fd
, int wait_fd
,
809 uint64_t max_sb_size
,
810 unsigned int nb_init_streams
)
812 struct lttng_consumer_channel
*channel
;
815 channel
= zmalloc(sizeof(*channel
));
816 if (channel
== NULL
) {
817 PERROR("malloc struct lttng_consumer_channel");
820 channel
->key
= channel_key
;
821 channel
->shm_fd
= shm_fd
;
822 channel
->wait_fd
= wait_fd
;
823 channel
->mmap_len
= mmap_len
;
824 channel
->max_sb_size
= max_sb_size
;
825 channel
->refcount
= 0;
826 channel
->nb_init_streams
= nb_init_streams
;
827 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
829 switch (consumer_data
.type
) {
830 case LTTNG_CONSUMER_KERNEL
:
831 channel
->mmap_base
= NULL
;
832 channel
->mmap_len
= 0;
834 case LTTNG_CONSUMER32_UST
:
835 case LTTNG_CONSUMER64_UST
:
836 ret
= lttng_ustconsumer_allocate_channel(channel
);
843 ERR("Unknown consumer_data type");
847 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
848 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
849 (unsigned long long) channel
->mmap_len
,
850 (unsigned long long) channel
->max_sb_size
);
856 * Add a channel to the global list protected by a mutex.
858 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
860 struct lttng_ht_node_ulong
*node
;
861 struct lttng_ht_iter iter
;
863 pthread_mutex_lock(&consumer_data
.lock
);
864 /* Steal channel identifier, for UST */
865 consumer_steal_channel_key(channel
->key
);
868 lttng_ht_lookup(consumer_data
.channel_ht
,
869 (void *)((unsigned long) channel
->key
), &iter
);
870 node
= lttng_ht_iter_get_node_ulong(&iter
);
872 /* Channel already exist. Ignore the insertion */
876 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
880 pthread_mutex_unlock(&consumer_data
.lock
);
886 * Allocate the pollfd structure and the local view of the out fds to avoid
887 * doing a lookup in the linked list and concurrency issues when writing is
888 * needed. Called with consumer_data.lock held.
890 * Returns the number of fds in the structures.
892 static int consumer_update_poll_array(
893 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
894 struct lttng_consumer_stream
**local_stream
, struct lttng_ht
*ht
)
897 struct lttng_ht_iter iter
;
898 struct lttng_consumer_stream
*stream
;
900 DBG("Updating poll fd array");
902 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
904 * Only active streams with an active end point can be added to the
905 * poll set and local stream storage of the thread.
907 * There is a potential race here for endpoint_status to be updated
908 * just after the check. However, this is OK since the stream(s) will
909 * be deleted once the thread is notified that the end point state has
910 * changed where this function will be called back again.
912 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
913 stream
->endpoint_status
) {
916 DBG("Active FD %d", stream
->wait_fd
);
917 (*pollfd
)[i
].fd
= stream
->wait_fd
;
918 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
919 local_stream
[i
] = stream
;
925 * Insert the consumer_data_pipe at the end of the array and don't
926 * increment i so nb_fd is the number of real FD.
928 (*pollfd
)[i
].fd
= ctx
->consumer_data_pipe
[0];
929 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
934 * Poll on the should_quit pipe and the command socket return -1 on error and
935 * should exit, 0 if data is available on the command socket
937 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
942 num_rdy
= poll(consumer_sockpoll
, 2, -1);
945 * Restart interrupted system call.
947 if (errno
== EINTR
) {
950 PERROR("Poll error");
953 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
954 DBG("consumer_should_quit wake up");
964 * Set the error socket.
966 void lttng_consumer_set_error_sock(
967 struct lttng_consumer_local_data
*ctx
, int sock
)
969 ctx
->consumer_error_socket
= sock
;
973 * Set the command socket path.
975 void lttng_consumer_set_command_sock_path(
976 struct lttng_consumer_local_data
*ctx
, char *sock
)
978 ctx
->consumer_command_sock_path
= sock
;
982 * Send return code to the session daemon.
983 * If the socket is not defined, we return 0, it is not a fatal error
985 int lttng_consumer_send_error(
986 struct lttng_consumer_local_data
*ctx
, int cmd
)
988 if (ctx
->consumer_error_socket
> 0) {
989 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
990 sizeof(enum lttcomm_sessiond_command
));
997 * Close all the tracefiles and stream fds, should be called when all instances
1000 void lttng_consumer_cleanup(void)
1002 struct lttng_ht_iter iter
;
1003 struct lttng_ht_node_ulong
*node
;
1007 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
1009 struct lttng_consumer_channel
*channel
=
1010 caa_container_of(node
, struct lttng_consumer_channel
, node
);
1011 consumer_del_channel(channel
);
1016 lttng_ht_destroy(consumer_data
.channel_ht
);
1020 * Called from signal handler.
1022 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1027 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1028 } while (ret
< 0 && errno
== EINTR
);
1030 PERROR("write consumer quit");
1033 DBG("Consumer flag that it should quit");
1036 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1039 int outfd
= stream
->out_fd
;
1042 * This does a blocking write-and-wait on any page that belongs to the
1043 * subbuffer prior to the one we just wrote.
1044 * Don't care about error values, as these are just hints and ways to
1045 * limit the amount of page cache used.
1047 if (orig_offset
< stream
->chan
->max_sb_size
) {
1050 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
1051 stream
->chan
->max_sb_size
,
1052 SYNC_FILE_RANGE_WAIT_BEFORE
1053 | SYNC_FILE_RANGE_WRITE
1054 | SYNC_FILE_RANGE_WAIT_AFTER
);
1056 * Give hints to the kernel about how we access the file:
1057 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1060 * We need to call fadvise again after the file grows because the
1061 * kernel does not seem to apply fadvise to non-existing parts of the
1064 * Call fadvise _after_ having waited for the page writeback to
1065 * complete because the dirty page writeback semantic is not well
1066 * defined. So it can be expected to lead to lower throughput in
1069 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
1070 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
1074 * Initialise the necessary environnement :
1075 * - create a new context
1076 * - create the poll_pipe
1077 * - create the should_quit pipe (for signal handler)
1078 * - create the thread pipe (for splice)
1080 * Takes a function pointer as argument, this function is called when data is
1081 * available on a buffer. This function is responsible to do the
1082 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1083 * buffer configuration and then kernctl_put_next_subbuf at the end.
1085 * Returns a pointer to the new context or NULL on error.
1087 struct lttng_consumer_local_data
*lttng_consumer_create(
1088 enum lttng_consumer_type type
,
1089 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1090 struct lttng_consumer_local_data
*ctx
),
1091 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1092 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1093 int (*update_stream
)(int stream_key
, uint32_t state
))
1096 struct lttng_consumer_local_data
*ctx
;
1098 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1099 consumer_data
.type
== type
);
1100 consumer_data
.type
= type
;
1102 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1104 PERROR("allocating context");
1108 ctx
->consumer_error_socket
= -1;
1109 /* assign the callbacks */
1110 ctx
->on_buffer_ready
= buffer_ready
;
1111 ctx
->on_recv_channel
= recv_channel
;
1112 ctx
->on_recv_stream
= recv_stream
;
1113 ctx
->on_update_stream
= update_stream
;
1115 ret
= pipe(ctx
->consumer_data_pipe
);
1117 PERROR("Error creating poll pipe");
1118 goto error_poll_pipe
;
1121 /* set read end of the pipe to non-blocking */
1122 ret
= fcntl(ctx
->consumer_data_pipe
[0], F_SETFL
, O_NONBLOCK
);
1124 PERROR("fcntl O_NONBLOCK");
1125 goto error_poll_fcntl
;
1128 /* set write end of the pipe to non-blocking */
1129 ret
= fcntl(ctx
->consumer_data_pipe
[1], F_SETFL
, O_NONBLOCK
);
1131 PERROR("fcntl O_NONBLOCK");
1132 goto error_poll_fcntl
;
1135 ret
= pipe(ctx
->consumer_should_quit
);
1137 PERROR("Error creating recv pipe");
1138 goto error_quit_pipe
;
1141 ret
= pipe(ctx
->consumer_thread_pipe
);
1143 PERROR("Error creating thread pipe");
1144 goto error_thread_pipe
;
1147 ret
= utils_create_pipe(ctx
->consumer_metadata_pipe
);
1149 goto error_metadata_pipe
;
1152 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1154 goto error_splice_pipe
;
1160 utils_close_pipe(ctx
->consumer_metadata_pipe
);
1161 error_metadata_pipe
:
1162 utils_close_pipe(ctx
->consumer_thread_pipe
);
1164 for (i
= 0; i
< 2; i
++) {
1167 err
= close(ctx
->consumer_should_quit
[i
]);
1174 for (i
= 0; i
< 2; i
++) {
1177 err
= close(ctx
->consumer_data_pipe
[i
]);
1189 * Close all fds associated with the instance and free the context.
1191 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1195 DBG("Consumer destroying it. Closing everything.");
1197 ret
= close(ctx
->consumer_error_socket
);
1201 ret
= close(ctx
->consumer_thread_pipe
[0]);
1205 ret
= close(ctx
->consumer_thread_pipe
[1]);
1209 ret
= close(ctx
->consumer_data_pipe
[0]);
1213 ret
= close(ctx
->consumer_data_pipe
[1]);
1217 ret
= close(ctx
->consumer_should_quit
[0]);
1221 ret
= close(ctx
->consumer_should_quit
[1]);
1225 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1227 unlink(ctx
->consumer_command_sock_path
);
1232 * Write the metadata stream id on the specified file descriptor.
1234 static int write_relayd_metadata_id(int fd
,
1235 struct lttng_consumer_stream
*stream
,
1236 struct consumer_relayd_sock_pair
*relayd
,
1237 unsigned long padding
)
1240 struct lttcomm_relayd_metadata_payload hdr
;
1242 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1243 hdr
.padding_size
= htobe32(padding
);
1245 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1246 } while (ret
< 0 && errno
== EINTR
);
1248 PERROR("write metadata stream id");
1251 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1252 stream
->relayd_stream_id
, padding
);
1259 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1260 * core function for writing trace buffers to either the local filesystem or
1263 * Careful review MUST be put if any changes occur!
1265 * Returns the number of bytes written
1267 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1268 struct lttng_consumer_local_data
*ctx
,
1269 struct lttng_consumer_stream
*stream
, unsigned long len
,
1270 unsigned long padding
)
1272 unsigned long mmap_offset
;
1273 ssize_t ret
= 0, written
= 0;
1274 off_t orig_offset
= stream
->out_fd_offset
;
1275 /* Default is on the disk */
1276 int outfd
= stream
->out_fd
;
1277 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1278 unsigned int relayd_hang_up
= 0;
1280 /* RCU lock for the relayd pointer */
1283 pthread_mutex_lock(&stream
->lock
);
1285 /* Flag that the current stream if set for network streaming. */
1286 if (stream
->net_seq_idx
!= -1) {
1287 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1288 if (relayd
== NULL
) {
1293 /* get the offset inside the fd to mmap */
1294 switch (consumer_data
.type
) {
1295 case LTTNG_CONSUMER_KERNEL
:
1296 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1298 case LTTNG_CONSUMER32_UST
:
1299 case LTTNG_CONSUMER64_UST
:
1300 ret
= lttng_ustctl_get_mmap_read_offset(stream
->chan
->handle
,
1301 stream
->buf
, &mmap_offset
);
1304 ERR("Unknown consumer_data type");
1309 PERROR("tracer ctl get_mmap_read_offset");
1314 /* Handle stream on the relayd if the output is on the network */
1316 unsigned long netlen
= len
;
1319 * Lock the control socket for the complete duration of the function
1320 * since from this point on we will use the socket.
1322 if (stream
->metadata_flag
) {
1323 /* Metadata requires the control socket. */
1324 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1325 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1328 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1330 /* Use the returned socket. */
1333 /* Write metadata stream id before payload */
1334 if (stream
->metadata_flag
) {
1335 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1338 /* Socket operation failed. We consider the relayd dead */
1339 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1347 /* Socket operation failed. We consider the relayd dead */
1348 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1352 /* Else, use the default set before which is the filesystem. */
1355 /* No streaming, we have to set the len with the full padding */
1361 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
1362 } while (ret
< 0 && errno
== EINTR
);
1363 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1365 PERROR("Error in file write");
1369 /* Socket operation failed. We consider the relayd dead */
1370 if (errno
== EPIPE
|| errno
== EINVAL
) {
1375 } else if (ret
> len
) {
1376 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1384 /* This call is useless on a socket so better save a syscall. */
1386 /* This won't block, but will start writeout asynchronously */
1387 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1388 SYNC_FILE_RANGE_WRITE
);
1389 stream
->out_fd_offset
+= ret
;
1393 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1397 * This is a special case that the relayd has closed its socket. Let's
1398 * cleanup the relayd object and all associated streams.
1400 if (relayd
&& relayd_hang_up
) {
1401 cleanup_relayd(relayd
, ctx
);
1405 /* Unlock only if ctrl socket used */
1406 if (relayd
&& stream
->metadata_flag
) {
1407 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1409 pthread_mutex_unlock(&stream
->lock
);
1416 * Splice the data from the ring buffer to the tracefile.
1418 * Returns the number of bytes spliced.
1420 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1421 struct lttng_consumer_local_data
*ctx
,
1422 struct lttng_consumer_stream
*stream
, unsigned long len
,
1423 unsigned long padding
)
1425 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1427 off_t orig_offset
= stream
->out_fd_offset
;
1428 int fd
= stream
->wait_fd
;
1429 /* Default is on the disk */
1430 int outfd
= stream
->out_fd
;
1431 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1433 unsigned int relayd_hang_up
= 0;
1435 switch (consumer_data
.type
) {
1436 case LTTNG_CONSUMER_KERNEL
:
1438 case LTTNG_CONSUMER32_UST
:
1439 case LTTNG_CONSUMER64_UST
:
1440 /* Not supported for user space tracing */
1443 ERR("Unknown consumer_data type");
1447 /* RCU lock for the relayd pointer */
1450 pthread_mutex_lock(&stream
->lock
);
1452 /* Flag that the current stream if set for network streaming. */
1453 if (stream
->net_seq_idx
!= -1) {
1454 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1455 if (relayd
== NULL
) {
1461 * Choose right pipe for splice. Metadata and trace data are handled by
1462 * different threads hence the use of two pipes in order not to race or
1463 * corrupt the written data.
1465 if (stream
->metadata_flag
) {
1466 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1468 splice_pipe
= ctx
->consumer_thread_pipe
;
1471 /* Write metadata stream id before payload */
1473 int total_len
= len
;
1475 if (stream
->metadata_flag
) {
1477 * Lock the control socket for the complete duration of the function
1478 * since from this point on we will use the socket.
1480 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1482 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1486 /* Socket operation failed. We consider the relayd dead */
1487 if (ret
== -EBADF
) {
1488 WARN("Remote relayd disconnected. Stopping");
1495 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1498 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1500 /* Use the returned socket. */
1503 /* Socket operation failed. We consider the relayd dead */
1504 if (ret
== -EBADF
) {
1505 WARN("Remote relayd disconnected. Stopping");
1512 /* No streaming, we have to set the len with the full padding */
1517 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1518 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1519 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1520 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1521 DBG("splice chan to pipe, ret %zd", ret_splice
);
1522 if (ret_splice
< 0) {
1523 PERROR("Error in relay splice");
1525 written
= ret_splice
;
1531 /* Handle stream on the relayd if the output is on the network */
1533 if (stream
->metadata_flag
) {
1534 size_t metadata_payload_size
=
1535 sizeof(struct lttcomm_relayd_metadata_payload
);
1537 /* Update counter to fit the spliced data */
1538 ret_splice
+= metadata_payload_size
;
1539 len
+= metadata_payload_size
;
1541 * We do this so the return value can match the len passed as
1542 * argument to this function.
1544 written
-= metadata_payload_size
;
1548 /* Splice data out */
1549 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1550 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1551 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1552 if (ret_splice
< 0) {
1553 PERROR("Error in file splice");
1555 written
= ret_splice
;
1557 /* Socket operation failed. We consider the relayd dead */
1558 if (errno
== EBADF
|| errno
== EPIPE
) {
1559 WARN("Remote relayd disconnected. Stopping");
1565 } else if (ret_splice
> len
) {
1567 PERROR("Wrote more data than requested %zd (len: %lu)",
1569 written
+= ret_splice
;
1575 /* This call is useless on a socket so better save a syscall. */
1577 /* This won't block, but will start writeout asynchronously */
1578 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1579 SYNC_FILE_RANGE_WRITE
);
1580 stream
->out_fd_offset
+= ret_splice
;
1582 written
+= ret_splice
;
1584 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1592 * This is a special case that the relayd has closed its socket. Let's
1593 * cleanup the relayd object and all associated streams.
1595 if (relayd
&& relayd_hang_up
) {
1596 cleanup_relayd(relayd
, ctx
);
1597 /* Skip splice error so the consumer does not fail */
1602 /* send the appropriate error description to sessiond */
1605 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1608 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1611 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1616 if (relayd
&& stream
->metadata_flag
) {
1617 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1619 pthread_mutex_unlock(&stream
->lock
);
1626 * Take a snapshot for a specific fd
1628 * Returns 0 on success, < 0 on error
1630 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1631 struct lttng_consumer_stream
*stream
)
1633 switch (consumer_data
.type
) {
1634 case LTTNG_CONSUMER_KERNEL
:
1635 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1636 case LTTNG_CONSUMER32_UST
:
1637 case LTTNG_CONSUMER64_UST
:
1638 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1640 ERR("Unknown consumer_data type");
1648 * Get the produced position
1650 * Returns 0 on success, < 0 on error
1652 int lttng_consumer_get_produced_snapshot(
1653 struct lttng_consumer_local_data
*ctx
,
1654 struct lttng_consumer_stream
*stream
,
1657 switch (consumer_data
.type
) {
1658 case LTTNG_CONSUMER_KERNEL
:
1659 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1660 case LTTNG_CONSUMER32_UST
:
1661 case LTTNG_CONSUMER64_UST
:
1662 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1664 ERR("Unknown consumer_data type");
1670 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1671 int sock
, struct pollfd
*consumer_sockpoll
)
1673 switch (consumer_data
.type
) {
1674 case LTTNG_CONSUMER_KERNEL
:
1675 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1676 case LTTNG_CONSUMER32_UST
:
1677 case LTTNG_CONSUMER64_UST
:
1678 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1680 ERR("Unknown consumer_data type");
1687 * Iterate over all streams of the hashtable and free them properly.
1689 * WARNING: *MUST* be used with data stream only.
1691 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1694 struct lttng_ht_iter iter
;
1695 struct lttng_consumer_stream
*stream
;
1702 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1703 ret
= lttng_ht_del(ht
, &iter
);
1706 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1710 lttng_ht_destroy(ht
);
1714 * Iterate over all streams of the hashtable and free them properly.
1716 * XXX: Should not be only for metadata stream or else use an other name.
1718 static void destroy_stream_ht(struct lttng_ht
*ht
)
1721 struct lttng_ht_iter iter
;
1722 struct lttng_consumer_stream
*stream
;
1729 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1730 ret
= lttng_ht_del(ht
, &iter
);
1733 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1737 lttng_ht_destroy(ht
);
1741 * Clean up a metadata stream and free its memory.
1743 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1744 struct lttng_ht
*ht
)
1747 struct lttng_ht_iter iter
;
1748 struct lttng_consumer_channel
*free_chan
= NULL
;
1749 struct consumer_relayd_sock_pair
*relayd
;
1753 * This call should NEVER receive regular stream. It must always be
1754 * metadata stream and this is crucial for data structure synchronization.
1756 assert(stream
->metadata_flag
);
1758 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1761 /* Means the stream was allocated but not successfully added */
1765 pthread_mutex_lock(&stream
->lock
);
1767 pthread_mutex_lock(&consumer_data
.lock
);
1768 switch (consumer_data
.type
) {
1769 case LTTNG_CONSUMER_KERNEL
:
1770 if (stream
->mmap_base
!= NULL
) {
1771 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1773 PERROR("munmap metadata stream");
1777 case LTTNG_CONSUMER32_UST
:
1778 case LTTNG_CONSUMER64_UST
:
1779 lttng_ustconsumer_del_stream(stream
);
1782 ERR("Unknown consumer_data type");
1788 iter
.iter
.node
= &stream
->node
.node
;
1789 ret
= lttng_ht_del(ht
, &iter
);
1792 /* Remove node session id from the consumer_data stream ht */
1793 iter
.iter
.node
= &stream
->node_session_id
.node
;
1794 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1798 if (stream
->out_fd
>= 0) {
1799 ret
= close(stream
->out_fd
);
1805 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
1806 ret
= close(stream
->wait_fd
);
1812 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
1813 ret
= close(stream
->shm_fd
);
1819 /* Check and cleanup relayd */
1821 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1822 if (relayd
!= NULL
) {
1823 uatomic_dec(&relayd
->refcount
);
1824 assert(uatomic_read(&relayd
->refcount
) >= 0);
1826 /* Closing streams requires to lock the control socket. */
1827 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1828 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1829 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1830 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1832 DBG("Unable to close stream on the relayd. Continuing");
1834 * Continue here. There is nothing we can do for the relayd.
1835 * Chances are that the relayd has closed the socket so we just
1836 * continue cleaning up.
1840 /* Both conditions are met, we destroy the relayd. */
1841 if (uatomic_read(&relayd
->refcount
) == 0 &&
1842 uatomic_read(&relayd
->destroy_flag
)) {
1843 destroy_relayd(relayd
);
1848 /* Atomically decrement channel refcount since other threads can use it. */
1849 uatomic_dec(&stream
->chan
->refcount
);
1850 if (!uatomic_read(&stream
->chan
->refcount
)
1851 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
1852 /* Go for channel deletion! */
1853 free_chan
= stream
->chan
;
1857 pthread_mutex_unlock(&consumer_data
.lock
);
1858 pthread_mutex_unlock(&stream
->lock
);
1861 consumer_del_channel(free_chan
);
1865 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1869 * Action done with the metadata stream when adding it to the consumer internal
1870 * data structures to handle it.
1872 static int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
,
1873 struct lttng_ht
*ht
)
1876 struct consumer_relayd_sock_pair
*relayd
;
1881 DBG3("Adding metadata stream %d to hash table", stream
->wait_fd
);
1883 pthread_mutex_lock(&consumer_data
.lock
);
1884 pthread_mutex_lock(&stream
->lock
);
1887 * From here, refcounts are updated so be _careful_ when returning an error
1892 /* Find relayd and, if one is found, increment refcount. */
1893 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1894 if (relayd
!= NULL
) {
1895 uatomic_inc(&relayd
->refcount
);
1898 /* Update channel refcount once added without error(s). */
1899 uatomic_inc(&stream
->chan
->refcount
);
1902 * When nb_init_streams reaches 0, we don't need to trigger any action in
1903 * terms of destroying the associated channel, because the action that
1904 * causes the count to become 0 also causes a stream to be added. The
1905 * channel deletion will thus be triggered by the following removal of this
1908 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
1909 uatomic_dec(&stream
->chan
->nb_init_streams
);
1912 /* Steal stream identifier to avoid having streams with the same key */
1913 consumer_steal_stream_key(stream
->key
, ht
);
1915 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
1918 * Add stream to the stream_list_ht of the consumer data. No need to steal
1919 * the key since the HT does not use it and we allow to add redundant keys
1922 lttng_ht_add_ulong(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
1926 pthread_mutex_unlock(&stream
->lock
);
1927 pthread_mutex_unlock(&consumer_data
.lock
);
1932 * Delete data stream that are flagged for deletion (endpoint_status).
1934 static void validate_endpoint_status_data_stream(void)
1936 struct lttng_ht_iter iter
;
1937 struct lttng_consumer_stream
*stream
;
1939 DBG("Consumer delete flagged data stream");
1942 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1943 /* Validate delete flag of the stream */
1944 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_INACTIVE
) {
1947 /* Delete it right now */
1948 consumer_del_stream(stream
, data_ht
);
1954 * Delete metadata stream that are flagged for deletion (endpoint_status).
1956 static void validate_endpoint_status_metadata_stream(
1957 struct lttng_poll_event
*pollset
)
1959 struct lttng_ht_iter iter
;
1960 struct lttng_consumer_stream
*stream
;
1962 DBG("Consumer delete flagged metadata stream");
1967 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1968 /* Validate delete flag of the stream */
1969 if (!stream
->endpoint_status
) {
1973 * Remove from pollset so the metadata thread can continue without
1974 * blocking on a deleted stream.
1976 lttng_poll_del(pollset
, stream
->wait_fd
);
1978 /* Delete it right now */
1979 consumer_del_metadata_stream(stream
, metadata_ht
);
1985 * Thread polls on metadata file descriptor and write them on disk or on the
1988 void *consumer_thread_metadata_poll(void *data
)
1991 uint32_t revents
, nb_fd
;
1992 struct lttng_consumer_stream
*stream
= NULL
;
1993 struct lttng_ht_iter iter
;
1994 struct lttng_ht_node_ulong
*node
;
1995 struct lttng_poll_event events
;
1996 struct lttng_consumer_local_data
*ctx
= data
;
1999 rcu_register_thread();
2001 DBG("Thread metadata poll started");
2003 /* Size is set to 1 for the consumer_metadata pipe */
2004 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2006 ERR("Poll set creation failed");
2010 ret
= lttng_poll_add(&events
, ctx
->consumer_metadata_pipe
[0], LPOLLIN
);
2016 DBG("Metadata main loop started");
2019 lttng_poll_reset(&events
);
2021 nb_fd
= LTTNG_POLL_GETNB(&events
);
2023 /* Only the metadata pipe is set */
2024 if (nb_fd
== 0 && consumer_quit
== 1) {
2029 DBG("Metadata poll wait with %d fd(s)", nb_fd
);
2030 ret
= lttng_poll_wait(&events
, -1);
2031 DBG("Metadata event catched in thread");
2033 if (errno
== EINTR
) {
2034 ERR("Poll EINTR catched");
2040 /* From here, the event is a metadata wait fd */
2041 for (i
= 0; i
< nb_fd
; i
++) {
2042 revents
= LTTNG_POLL_GETEV(&events
, i
);
2043 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2045 /* Just don't waste time if no returned events for the fd */
2050 if (pollfd
== ctx
->consumer_metadata_pipe
[0]) {
2051 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2052 DBG("Metadata thread pipe hung up");
2054 * Remove the pipe from the poll set and continue the loop
2055 * since their might be data to consume.
2057 lttng_poll_del(&events
, ctx
->consumer_metadata_pipe
[0]);
2058 ret
= close(ctx
->consumer_metadata_pipe
[0]);
2060 PERROR("close metadata pipe");
2063 } else if (revents
& LPOLLIN
) {
2065 /* Get the stream pointer received */
2066 ret
= read(pollfd
, &stream
, sizeof(stream
));
2067 } while (ret
< 0 && errno
== EINTR
);
2069 ret
< sizeof(struct lttng_consumer_stream
*)) {
2070 PERROR("read metadata stream");
2072 * Let's continue here and hope we can still work
2073 * without stopping the consumer. XXX: Should we?
2078 /* A NULL stream means that the state has changed. */
2079 if (stream
== NULL
) {
2080 /* Check for deleted streams. */
2081 validate_endpoint_status_metadata_stream(&events
);
2085 DBG("Adding metadata stream %d to poll set",
2088 ret
= consumer_add_metadata_stream(stream
, metadata_ht
);
2090 ERR("Unable to add metadata stream");
2091 /* Stream was not setup properly. Continuing. */
2092 consumer_del_metadata_stream(stream
, NULL
);
2096 /* Add metadata stream to the global poll events list */
2097 lttng_poll_add(&events
, stream
->wait_fd
,
2098 LPOLLIN
| LPOLLPRI
);
2101 /* Handle other stream */
2106 lttng_ht_lookup(metadata_ht
, (void *)((unsigned long) pollfd
),
2108 node
= lttng_ht_iter_get_node_ulong(&iter
);
2111 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2114 /* Check for error event */
2115 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2116 DBG("Metadata fd %d is hup|err.", pollfd
);
2117 if (!stream
->hangup_flush_done
2118 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2119 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2120 DBG("Attempting to flush and consume the UST buffers");
2121 lttng_ustconsumer_on_stream_hangup(stream
);
2123 /* We just flushed the stream now read it. */
2125 len
= ctx
->on_buffer_ready(stream
, ctx
);
2127 * We don't check the return value here since if we get
2128 * a negative len, it means an error occured thus we
2129 * simply remove it from the poll set and free the
2135 lttng_poll_del(&events
, stream
->wait_fd
);
2137 * This call update the channel states, closes file descriptors
2138 * and securely free the stream.
2140 consumer_del_metadata_stream(stream
, metadata_ht
);
2141 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2142 /* Get the data out of the metadata file descriptor */
2143 DBG("Metadata available on fd %d", pollfd
);
2144 assert(stream
->wait_fd
== pollfd
);
2146 len
= ctx
->on_buffer_ready(stream
, ctx
);
2147 /* It's ok to have an unavailable sub-buffer */
2148 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2149 /* Clean up stream from consumer and free it. */
2150 lttng_poll_del(&events
, stream
->wait_fd
);
2151 consumer_del_metadata_stream(stream
, metadata_ht
);
2152 } else if (len
> 0) {
2153 stream
->data_read
= 1;
2157 /* Release RCU lock for the stream looked up */
2164 DBG("Metadata poll thread exiting");
2165 lttng_poll_clean(&events
);
2168 destroy_stream_ht(metadata_ht
);
2171 rcu_unregister_thread();
2176 * This thread polls the fds in the set to consume the data and write
2177 * it to tracefile if necessary.
2179 void *consumer_thread_data_poll(void *data
)
2181 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2182 struct pollfd
*pollfd
= NULL
;
2183 /* local view of the streams */
2184 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2185 /* local view of consumer_data.fds_count */
2187 struct lttng_consumer_local_data
*ctx
= data
;
2190 rcu_register_thread();
2192 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2193 if (data_ht
== NULL
) {
2197 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
2204 * the fds set has been updated, we need to update our
2205 * local array as well
2207 pthread_mutex_lock(&consumer_data
.lock
);
2208 if (consumer_data
.need_update
) {
2209 if (pollfd
!= NULL
) {
2213 if (local_stream
!= NULL
) {
2215 local_stream
= NULL
;
2218 /* allocate for all fds + 1 for the consumer_data_pipe */
2219 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2220 if (pollfd
== NULL
) {
2221 PERROR("pollfd malloc");
2222 pthread_mutex_unlock(&consumer_data
.lock
);
2226 /* allocate for all fds + 1 for the consumer_data_pipe */
2227 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2228 sizeof(struct lttng_consumer_stream
));
2229 if (local_stream
== NULL
) {
2230 PERROR("local_stream malloc");
2231 pthread_mutex_unlock(&consumer_data
.lock
);
2234 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
,
2237 ERR("Error in allocating pollfd or local_outfds");
2238 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2239 pthread_mutex_unlock(&consumer_data
.lock
);
2243 consumer_data
.need_update
= 0;
2245 pthread_mutex_unlock(&consumer_data
.lock
);
2247 /* No FDs and consumer_quit, consumer_cleanup the thread */
2248 if (nb_fd
== 0 && consumer_quit
== 1) {
2251 /* poll on the array of fds */
2253 DBG("polling on %d fd", nb_fd
+ 1);
2254 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2255 DBG("poll num_rdy : %d", num_rdy
);
2256 if (num_rdy
== -1) {
2258 * Restart interrupted system call.
2260 if (errno
== EINTR
) {
2263 PERROR("Poll error");
2264 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2266 } else if (num_rdy
== 0) {
2267 DBG("Polling thread timed out");
2272 * If the consumer_data_pipe triggered poll go directly to the
2273 * beginning of the loop to update the array. We want to prioritize
2274 * array update over low-priority reads.
2276 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2277 size_t pipe_readlen
;
2279 DBG("consumer_data_pipe wake up");
2280 /* Consume 1 byte of pipe data */
2282 pipe_readlen
= read(ctx
->consumer_data_pipe
[0], &new_stream
,
2283 sizeof(new_stream
));
2284 } while (pipe_readlen
== -1 && errno
== EINTR
);
2287 * If the stream is NULL, just ignore it. It's also possible that
2288 * the sessiond poll thread changed the consumer_quit state and is
2289 * waking us up to test it.
2291 if (new_stream
== NULL
) {
2292 validate_endpoint_status_data_stream();
2296 ret
= consumer_add_stream(new_stream
, data_ht
);
2298 ERR("Consumer add stream %d failed. Continuing",
2301 * At this point, if the add_stream fails, it is not in the
2302 * hash table thus passing the NULL value here.
2304 consumer_del_stream(new_stream
, NULL
);
2307 /* Continue to update the local streams and handle prio ones */
2311 /* Take care of high priority channels first. */
2312 for (i
= 0; i
< nb_fd
; i
++) {
2313 if (local_stream
[i
] == NULL
) {
2316 if (pollfd
[i
].revents
& POLLPRI
) {
2317 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2319 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2320 /* it's ok to have an unavailable sub-buffer */
2321 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2322 /* Clean the stream and free it. */
2323 consumer_del_stream(local_stream
[i
], data_ht
);
2324 local_stream
[i
] = NULL
;
2325 } else if (len
> 0) {
2326 local_stream
[i
]->data_read
= 1;
2332 * If we read high prio channel in this loop, try again
2333 * for more high prio data.
2339 /* Take care of low priority channels. */
2340 for (i
= 0; i
< nb_fd
; i
++) {
2341 if (local_stream
[i
] == NULL
) {
2344 if ((pollfd
[i
].revents
& POLLIN
) ||
2345 local_stream
[i
]->hangup_flush_done
) {
2346 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2347 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2348 /* it's ok to have an unavailable sub-buffer */
2349 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2350 /* Clean the stream and free it. */
2351 consumer_del_stream(local_stream
[i
], data_ht
);
2352 local_stream
[i
] = NULL
;
2353 } else if (len
> 0) {
2354 local_stream
[i
]->data_read
= 1;
2359 /* Handle hangup and errors */
2360 for (i
= 0; i
< nb_fd
; i
++) {
2361 if (local_stream
[i
] == NULL
) {
2364 if (!local_stream
[i
]->hangup_flush_done
2365 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2366 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2367 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2368 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2370 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2371 /* Attempt read again, for the data we just flushed. */
2372 local_stream
[i
]->data_read
= 1;
2375 * If the poll flag is HUP/ERR/NVAL and we have
2376 * read no data in this pass, we can remove the
2377 * stream from its hash table.
2379 if ((pollfd
[i
].revents
& POLLHUP
)) {
2380 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2381 if (!local_stream
[i
]->data_read
) {
2382 consumer_del_stream(local_stream
[i
], data_ht
);
2383 local_stream
[i
] = NULL
;
2386 } else if (pollfd
[i
].revents
& POLLERR
) {
2387 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2388 if (!local_stream
[i
]->data_read
) {
2389 consumer_del_stream(local_stream
[i
], data_ht
);
2390 local_stream
[i
] = NULL
;
2393 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2394 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2395 if (!local_stream
[i
]->data_read
) {
2396 consumer_del_stream(local_stream
[i
], data_ht
);
2397 local_stream
[i
] = NULL
;
2401 if (local_stream
[i
] != NULL
) {
2402 local_stream
[i
]->data_read
= 0;
2407 DBG("polling thread exiting");
2408 if (pollfd
!= NULL
) {
2412 if (local_stream
!= NULL
) {
2414 local_stream
= NULL
;
2418 * Close the write side of the pipe so epoll_wait() in
2419 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2420 * read side of the pipe. If we close them both, epoll_wait strangely does
2421 * not return and could create a endless wait period if the pipe is the
2422 * only tracked fd in the poll set. The thread will take care of closing
2425 ret
= close(ctx
->consumer_metadata_pipe
[1]);
2427 PERROR("close data pipe");
2431 destroy_data_stream_ht(data_ht
);
2434 rcu_unregister_thread();
2439 * This thread listens on the consumerd socket and receives the file
2440 * descriptors from the session daemon.
2442 void *consumer_thread_sessiond_poll(void *data
)
2444 int sock
, client_socket
, ret
;
2446 * structure to poll for incoming data on communication socket avoids
2447 * making blocking sockets.
2449 struct pollfd consumer_sockpoll
[2];
2450 struct lttng_consumer_local_data
*ctx
= data
;
2452 rcu_register_thread();
2454 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2455 unlink(ctx
->consumer_command_sock_path
);
2456 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2457 if (client_socket
< 0) {
2458 ERR("Cannot create command socket");
2462 ret
= lttcomm_listen_unix_sock(client_socket
);
2467 DBG("Sending ready command to lttng-sessiond");
2468 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2469 /* return < 0 on error, but == 0 is not fatal */
2471 ERR("Error sending ready command to lttng-sessiond");
2475 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2477 PERROR("fcntl O_NONBLOCK");
2481 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2482 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2483 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2484 consumer_sockpoll
[1].fd
= client_socket
;
2485 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2487 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2490 DBG("Connection on client_socket");
2492 /* Blocking call, waiting for transmission */
2493 sock
= lttcomm_accept_unix_sock(client_socket
);
2498 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2500 PERROR("fcntl O_NONBLOCK");
2504 /* update the polling structure to poll on the established socket */
2505 consumer_sockpoll
[1].fd
= sock
;
2506 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2509 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2512 DBG("Incoming command on sock");
2513 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2514 if (ret
== -ENOENT
) {
2515 DBG("Received STOP command");
2520 * This could simply be a session daemon quitting. Don't output
2523 DBG("Communication interrupted on command socket");
2526 if (consumer_quit
) {
2527 DBG("consumer_thread_receive_fds received quit from signal");
2530 DBG("received fds on sock");
2533 DBG("consumer_thread_receive_fds exiting");
2536 * when all fds have hung up, the polling thread
2542 * Notify the data poll thread to poll back again and test the
2543 * consumer_quit state that we just set so to quit gracefully.
2545 notify_thread_pipe(ctx
->consumer_data_pipe
[1]);
2547 rcu_unregister_thread();
2551 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2552 struct lttng_consumer_local_data
*ctx
)
2554 switch (consumer_data
.type
) {
2555 case LTTNG_CONSUMER_KERNEL
:
2556 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
2557 case LTTNG_CONSUMER32_UST
:
2558 case LTTNG_CONSUMER64_UST
:
2559 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2561 ERR("Unknown consumer_data type");
2567 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2569 switch (consumer_data
.type
) {
2570 case LTTNG_CONSUMER_KERNEL
:
2571 return lttng_kconsumer_on_recv_stream(stream
);
2572 case LTTNG_CONSUMER32_UST
:
2573 case LTTNG_CONSUMER64_UST
:
2574 return lttng_ustconsumer_on_recv_stream(stream
);
2576 ERR("Unknown consumer_data type");
2583 * Allocate and set consumer data hash tables.
2585 void lttng_consumer_init(void)
2587 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2588 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2589 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2591 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2592 assert(metadata_ht
);
2593 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2598 * Process the ADD_RELAYD command receive by a consumer.
2600 * This will create a relayd socket pair and add it to the relayd hash table.
2601 * The caller MUST acquire a RCU read side lock before calling it.
2603 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
2604 struct lttng_consumer_local_data
*ctx
, int sock
,
2605 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
)
2608 struct consumer_relayd_sock_pair
*relayd
;
2610 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
2612 /* Get relayd reference if exists. */
2613 relayd
= consumer_find_relayd(net_seq_idx
);
2614 if (relayd
== NULL
) {
2615 /* Not found. Allocate one. */
2616 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
2617 if (relayd
== NULL
) {
2618 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
2623 /* Poll on consumer socket. */
2624 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2629 /* Get relayd socket from session daemon */
2630 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2631 if (ret
!= sizeof(fd
)) {
2632 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
2637 /* Copy socket information and received FD */
2638 switch (sock_type
) {
2639 case LTTNG_STREAM_CONTROL
:
2640 /* Copy received lttcomm socket */
2641 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
2642 ret
= lttcomm_create_sock(&relayd
->control_sock
);
2647 /* Close the created socket fd which is useless */
2648 ret
= close(relayd
->control_sock
.fd
);
2650 PERROR("close relayd control socket");
2653 /* Assign new file descriptor */
2654 relayd
->control_sock
.fd
= fd
;
2656 case LTTNG_STREAM_DATA
:
2657 /* Copy received lttcomm socket */
2658 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
2659 ret
= lttcomm_create_sock(&relayd
->data_sock
);
2664 /* Close the created socket fd which is useless */
2665 ret
= close(relayd
->data_sock
.fd
);
2667 PERROR("close relayd control socket");
2670 /* Assign new file descriptor */
2671 relayd
->data_sock
.fd
= fd
;
2674 ERR("Unknown relayd socket type (%d)", sock_type
);
2678 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2679 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
2680 relayd
->net_seq_idx
, fd
);
2683 * Add relayd socket pair to consumer data hashtable. If object already
2684 * exists or on error, the function gracefully returns.
2696 * Try to lock the stream mutex.
2698 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
2700 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
2707 * Try to lock the stream mutex. On failure, we know that the stream is
2708 * being used else where hence there is data still being extracted.
2710 ret
= pthread_mutex_trylock(&stream
->lock
);
2712 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
2724 * Check if for a given session id there is still data needed to be extract
2727 * Return 1 if data is pending or else 0 meaning ready to be read.
2729 int consumer_data_pending(uint64_t id
)
2732 struct lttng_ht_iter iter
;
2733 struct lttng_ht
*ht
;
2734 struct lttng_consumer_stream
*stream
;
2735 struct consumer_relayd_sock_pair
*relayd
;
2736 int (*data_pending
)(struct lttng_consumer_stream
*);
2738 DBG("Consumer data pending command on session id %" PRIu64
, id
);
2741 pthread_mutex_lock(&consumer_data
.lock
);
2743 switch (consumer_data
.type
) {
2744 case LTTNG_CONSUMER_KERNEL
:
2745 data_pending
= lttng_kconsumer_data_pending
;
2747 case LTTNG_CONSUMER32_UST
:
2748 case LTTNG_CONSUMER64_UST
:
2749 data_pending
= lttng_ustconsumer_data_pending
;
2752 ERR("Unknown consumer data type");
2756 /* Ease our life a bit */
2757 ht
= consumer_data
.stream_list_ht
;
2759 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2760 ht
->hash_fct((void *)((unsigned long) id
), lttng_ht_seed
),
2761 ht
->match_fct
, (void *)((unsigned long) id
),
2762 &iter
.iter
, stream
, node_session_id
.node
) {
2763 /* If this call fails, the stream is being used hence data pending. */
2764 ret
= stream_try_lock(stream
);
2766 goto data_not_pending
;
2770 * A removed node from the hash table indicates that the stream has
2771 * been deleted thus having a guarantee that the buffers are closed
2772 * on the consumer side. However, data can still be transmitted
2773 * over the network so don't skip the relayd check.
2775 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
2777 /* Check the stream if there is data in the buffers. */
2778 ret
= data_pending(stream
);
2780 pthread_mutex_unlock(&stream
->lock
);
2781 goto data_not_pending
;
2786 if (stream
->net_seq_idx
!= -1) {
2787 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
2790 * At this point, if the relayd object is not available for the
2791 * given stream, it is because the relayd is being cleaned up
2792 * so every stream associated with it (for a session id value)
2793 * are or will be marked for deletion hence no data pending.
2795 pthread_mutex_unlock(&stream
->lock
);
2796 goto data_not_pending
;
2799 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
2800 if (stream
->metadata_flag
) {
2801 ret
= relayd_quiescent_control(&relayd
->control_sock
);
2803 ret
= relayd_data_pending(&relayd
->control_sock
,
2804 stream
->relayd_stream_id
, stream
->next_net_seq_num
);
2806 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2808 pthread_mutex_unlock(&stream
->lock
);
2809 goto data_not_pending
;
2812 pthread_mutex_unlock(&stream
->lock
);
2816 * Finding _no_ node in the hash table means that the stream(s) have been
2817 * removed thus data is guaranteed to be available for analysis from the
2818 * trace files. This is *only* true for local consumer and not network
2822 /* Data is available to be read by a viewer. */
2823 pthread_mutex_unlock(&consumer_data
.lock
);
2828 /* Data is still being extracted from buffers. */
2829 pthread_mutex_unlock(&consumer_data
.lock
);