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;
131 * Return a channel object for the given key.
133 * RCU read side lock MUST be acquired before calling this function and
134 * protects the channel ptr.
136 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
138 struct lttng_ht_iter iter
;
139 struct lttng_ht_node_ulong
*node
;
140 struct lttng_consumer_channel
*channel
= NULL
;
142 /* Negative keys are lookup failures */
147 lttng_ht_lookup(consumer_data
.channel_ht
, (void *)((unsigned long) key
),
149 node
= lttng_ht_iter_get_node_ulong(&iter
);
151 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
157 static void consumer_steal_channel_key(int key
)
159 struct lttng_consumer_channel
*channel
;
162 channel
= consumer_find_channel(key
);
166 * We don't want the lookup to match, but we still need
167 * to iterate on this channel when iterating over the hash table. Just
168 * change the node key.
170 channel
->node
.key
= -1;
176 void consumer_free_stream(struct rcu_head
*head
)
178 struct lttng_ht_node_ulong
*node
=
179 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
180 struct lttng_consumer_stream
*stream
=
181 caa_container_of(node
, struct lttng_consumer_stream
, node
);
187 * RCU protected relayd socket pair free.
189 static void consumer_rcu_free_relayd(struct rcu_head
*head
)
191 struct lttng_ht_node_ulong
*node
=
192 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
193 struct consumer_relayd_sock_pair
*relayd
=
194 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
197 * Close all sockets. This is done in the call RCU since we don't want the
198 * socket fds to be reassigned thus potentially creating bad state of the
201 * We do not have to lock the control socket mutex here since at this stage
202 * there is no one referencing to this relayd object.
204 (void) relayd_close(&relayd
->control_sock
);
205 (void) relayd_close(&relayd
->data_sock
);
211 * Destroy and free relayd socket pair object.
213 * This function MUST be called with the consumer_data lock acquired.
215 static void destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
218 struct lttng_ht_iter iter
;
220 if (relayd
== NULL
) {
224 DBG("Consumer destroy and close relayd socket pair");
226 iter
.iter
.node
= &relayd
->node
.node
;
227 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
229 /* We assume the relayd is being or is destroyed */
233 /* RCU free() call */
234 call_rcu(&relayd
->node
.head
, consumer_rcu_free_relayd
);
238 * Update the end point status of all streams having the given network sequence
239 * index (relayd index).
241 * It's atomically set without having the stream mutex locked which is fine
242 * because we handle the write/read race with a pipe wakeup for each thread.
244 static void update_endpoint_status_by_netidx(int net_seq_idx
,
245 enum consumer_endpoint_status status
)
247 struct lttng_ht_iter iter
;
248 struct lttng_consumer_stream
*stream
;
250 DBG("Consumer set delete flag on stream by idx %d", net_seq_idx
);
254 /* Let's begin with metadata */
255 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
256 if (stream
->net_seq_idx
== net_seq_idx
) {
257 uatomic_set(&stream
->endpoint_status
, status
);
258 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
262 /* Follow up by the data streams */
263 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
264 if (stream
->net_seq_idx
== net_seq_idx
) {
265 uatomic_set(&stream
->endpoint_status
, status
);
266 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
273 * Cleanup a relayd object by flagging every associated streams for deletion,
274 * destroying the object meaning removing it from the relayd hash table,
275 * closing the sockets and freeing the memory in a RCU call.
277 * If a local data context is available, notify the threads that the streams'
278 * state have changed.
280 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
281 struct lttng_consumer_local_data
*ctx
)
287 DBG("Cleaning up relayd sockets");
289 /* Save the net sequence index before destroying the object */
290 netidx
= relayd
->net_seq_idx
;
293 * Delete the relayd from the relayd hash table, close the sockets and free
294 * the object in a RCU call.
296 destroy_relayd(relayd
);
298 /* Set inactive endpoint to all streams */
299 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
302 * With a local data context, notify the threads that the streams' state
303 * have changed. The write() action on the pipe acts as an "implicit"
304 * memory barrier ordering the updates of the end point status from the
305 * read of this status which happens AFTER receiving this notify.
308 notify_thread_pipe(ctx
->consumer_data_pipe
[1]);
309 notify_thread_pipe(ctx
->consumer_metadata_pipe
[1]);
314 * Flag a relayd socket pair for destruction. Destroy it if the refcount
317 * RCU read side lock MUST be aquired before calling this function.
319 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
323 /* Set destroy flag for this object */
324 uatomic_set(&relayd
->destroy_flag
, 1);
326 /* Destroy the relayd if refcount is 0 */
327 if (uatomic_read(&relayd
->refcount
) == 0) {
328 destroy_relayd(relayd
);
333 * Remove a stream from the global list protected by a mutex. This
334 * function is also responsible for freeing its data structures.
336 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
340 struct lttng_ht_iter iter
;
341 struct lttng_consumer_channel
*free_chan
= NULL
;
342 struct consumer_relayd_sock_pair
*relayd
;
346 DBG("Consumer del stream %d", stream
->wait_fd
);
349 /* Means the stream was allocated but not successfully added */
353 pthread_mutex_lock(&consumer_data
.lock
);
354 pthread_mutex_lock(&stream
->lock
);
356 switch (consumer_data
.type
) {
357 case LTTNG_CONSUMER_KERNEL
:
358 if (stream
->mmap_base
!= NULL
) {
359 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
365 case LTTNG_CONSUMER32_UST
:
366 case LTTNG_CONSUMER64_UST
:
367 lttng_ustconsumer_del_stream(stream
);
370 ERR("Unknown consumer_data type");
376 iter
.iter
.node
= &stream
->node
.node
;
377 ret
= lttng_ht_del(ht
, &iter
);
380 /* Remove node session id from the consumer_data stream ht */
381 iter
.iter
.node
= &stream
->node_session_id
.node
;
382 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
386 assert(consumer_data
.stream_count
> 0);
387 consumer_data
.stream_count
--;
389 if (stream
->out_fd
>= 0) {
390 ret
= close(stream
->out_fd
);
395 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
396 ret
= close(stream
->wait_fd
);
401 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
402 ret
= close(stream
->shm_fd
);
408 /* Check and cleanup relayd */
410 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
411 if (relayd
!= NULL
) {
412 uatomic_dec(&relayd
->refcount
);
413 assert(uatomic_read(&relayd
->refcount
) >= 0);
415 /* Closing streams requires to lock the control socket. */
416 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
417 ret
= relayd_send_close_stream(&relayd
->control_sock
,
418 stream
->relayd_stream_id
,
419 stream
->next_net_seq_num
- 1);
420 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
422 DBG("Unable to close stream on the relayd. Continuing");
424 * Continue here. There is nothing we can do for the relayd.
425 * Chances are that the relayd has closed the socket so we just
426 * continue cleaning up.
430 /* Both conditions are met, we destroy the relayd. */
431 if (uatomic_read(&relayd
->refcount
) == 0 &&
432 uatomic_read(&relayd
->destroy_flag
)) {
433 destroy_relayd(relayd
);
438 uatomic_dec(&stream
->chan
->refcount
);
439 if (!uatomic_read(&stream
->chan
->refcount
)
440 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
441 free_chan
= stream
->chan
;
445 consumer_data
.need_update
= 1;
446 pthread_mutex_unlock(&stream
->lock
);
447 pthread_mutex_unlock(&consumer_data
.lock
);
450 consumer_del_channel(free_chan
);
454 call_rcu(&stream
->node
.head
, consumer_free_stream
);
457 struct lttng_consumer_stream
*consumer_allocate_stream(
458 int channel_key
, int stream_key
,
459 int shm_fd
, int wait_fd
,
460 enum lttng_consumer_stream_state state
,
462 enum lttng_event_output output
,
463 const char *path_name
,
471 struct lttng_consumer_stream
*stream
;
473 stream
= zmalloc(sizeof(*stream
));
474 if (stream
== NULL
) {
475 PERROR("malloc struct lttng_consumer_stream");
476 *alloc_ret
= -ENOMEM
;
483 * Get stream's channel reference. Needed when adding the stream to the
486 stream
->chan
= consumer_find_channel(channel_key
);
488 *alloc_ret
= -ENOENT
;
489 ERR("Unable to find channel for stream %d", stream_key
);
493 stream
->key
= stream_key
;
494 stream
->shm_fd
= shm_fd
;
495 stream
->wait_fd
= wait_fd
;
497 stream
->out_fd_offset
= 0;
498 stream
->state
= state
;
499 stream
->mmap_len
= mmap_len
;
500 stream
->mmap_base
= NULL
;
501 stream
->output
= output
;
504 stream
->net_seq_idx
= net_index
;
505 stream
->metadata_flag
= metadata_flag
;
506 stream
->session_id
= session_id
;
507 strncpy(stream
->path_name
, path_name
, sizeof(stream
->path_name
));
508 stream
->path_name
[sizeof(stream
->path_name
) - 1] = '\0';
509 pthread_mutex_init(&stream
->lock
, NULL
);
512 * Index differently the metadata node because the thread is using an
513 * internal hash table to match streams in the metadata_ht to the epoll set
517 lttng_ht_node_init_ulong(&stream
->node
, stream
->wait_fd
);
519 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
522 /* Init session id node with the stream session id */
523 lttng_ht_node_init_ulong(&stream
->node_session_id
, stream
->session_id
);
526 * The cpu number is needed before using any ustctl_* actions. Ignored for
527 * the kernel so the value does not matter.
529 pthread_mutex_lock(&consumer_data
.lock
);
530 stream
->cpu
= stream
->chan
->cpucount
++;
531 pthread_mutex_unlock(&consumer_data
.lock
);
533 DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu,"
534 " out_fd %d, net_seq_idx %d, session_id %" PRIu64
,
535 stream
->path_name
, stream
->key
, stream
->shm_fd
, stream
->wait_fd
,
536 (unsigned long long) stream
->mmap_len
, stream
->out_fd
,
537 stream
->net_seq_idx
, stream
->session_id
);
550 * Add a stream to the global list protected by a mutex.
552 static int consumer_add_stream(struct lttng_consumer_stream
*stream
,
556 struct consumer_relayd_sock_pair
*relayd
;
561 DBG3("Adding consumer stream %d", stream
->key
);
563 pthread_mutex_lock(&consumer_data
.lock
);
564 pthread_mutex_lock(&stream
->lock
);
567 /* Steal stream identifier to avoid having streams with the same key */
568 consumer_steal_stream_key(stream
->key
, ht
);
570 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
573 * Add stream to the stream_list_ht of the consumer data. No need to steal
574 * the key since the HT does not use it and we allow to add redundant keys
577 lttng_ht_add_ulong(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
579 /* Check and cleanup relayd */
580 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
581 if (relayd
!= NULL
) {
582 uatomic_inc(&relayd
->refcount
);
585 /* Update channel refcount once added without error(s). */
586 uatomic_inc(&stream
->chan
->refcount
);
589 * When nb_init_streams reaches 0, we don't need to trigger any action in
590 * terms of destroying the associated channel, because the action that
591 * causes the count to become 0 also causes a stream to be added. The
592 * channel deletion will thus be triggered by the following removal of this
595 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
596 uatomic_dec(&stream
->chan
->nb_init_streams
);
599 /* Update consumer data once the node is inserted. */
600 consumer_data
.stream_count
++;
601 consumer_data
.need_update
= 1;
604 pthread_mutex_unlock(&stream
->lock
);
605 pthread_mutex_unlock(&consumer_data
.lock
);
611 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
612 * be acquired before calling this.
614 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
617 struct lttng_ht_node_ulong
*node
;
618 struct lttng_ht_iter iter
;
620 if (relayd
== NULL
) {
625 lttng_ht_lookup(consumer_data
.relayd_ht
,
626 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
627 node
= lttng_ht_iter_get_node_ulong(&iter
);
629 /* Relayd already exist. Ignore the insertion */
632 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
639 * Allocate and return a consumer relayd socket.
641 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
644 struct consumer_relayd_sock_pair
*obj
= NULL
;
646 /* Negative net sequence index is a failure */
647 if (net_seq_idx
< 0) {
651 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
653 PERROR("zmalloc relayd sock");
657 obj
->net_seq_idx
= net_seq_idx
;
659 obj
->destroy_flag
= 0;
660 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
661 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
668 * Find a relayd socket pair in the global consumer data.
670 * Return the object if found else NULL.
671 * RCU read-side lock must be held across this call and while using the
674 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
676 struct lttng_ht_iter iter
;
677 struct lttng_ht_node_ulong
*node
;
678 struct consumer_relayd_sock_pair
*relayd
= NULL
;
680 /* Negative keys are lookup failures */
685 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
687 node
= lttng_ht_iter_get_node_ulong(&iter
);
689 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
697 * Handle stream for relayd transmission if the stream applies for network
698 * streaming where the net sequence index is set.
700 * Return destination file descriptor or negative value on error.
702 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
703 size_t data_size
, unsigned long padding
,
704 struct consumer_relayd_sock_pair
*relayd
)
707 struct lttcomm_relayd_data_hdr data_hdr
;
713 /* Reset data header */
714 memset(&data_hdr
, 0, sizeof(data_hdr
));
716 if (stream
->metadata_flag
) {
717 /* Caller MUST acquire the relayd control socket lock */
718 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
723 /* Metadata are always sent on the control socket. */
724 outfd
= relayd
->control_sock
.fd
;
726 /* Set header with stream information */
727 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
728 data_hdr
.data_size
= htobe32(data_size
);
729 data_hdr
.padding_size
= htobe32(padding
);
730 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
731 /* Other fields are zeroed previously */
733 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
739 /* Set to go on data socket */
740 outfd
= relayd
->data_sock
.fd
;
748 void consumer_free_channel(struct rcu_head
*head
)
750 struct lttng_ht_node_ulong
*node
=
751 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
752 struct lttng_consumer_channel
*channel
=
753 caa_container_of(node
, struct lttng_consumer_channel
, node
);
759 * Remove a channel from the global list protected by a mutex. This
760 * function is also responsible for freeing its data structures.
762 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
765 struct lttng_ht_iter iter
;
767 DBG("Consumer delete channel key %d", channel
->key
);
769 pthread_mutex_lock(&consumer_data
.lock
);
771 switch (consumer_data
.type
) {
772 case LTTNG_CONSUMER_KERNEL
:
774 case LTTNG_CONSUMER32_UST
:
775 case LTTNG_CONSUMER64_UST
:
776 lttng_ustconsumer_del_channel(channel
);
779 ERR("Unknown consumer_data type");
785 iter
.iter
.node
= &channel
->node
.node
;
786 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
790 if (channel
->mmap_base
!= NULL
) {
791 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
796 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
797 ret
= close(channel
->wait_fd
);
802 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
803 ret
= close(channel
->shm_fd
);
809 call_rcu(&channel
->node
.head
, consumer_free_channel
);
811 pthread_mutex_unlock(&consumer_data
.lock
);
814 struct lttng_consumer_channel
*consumer_allocate_channel(
816 int shm_fd
, int wait_fd
,
818 uint64_t max_sb_size
,
819 unsigned int nb_init_streams
)
821 struct lttng_consumer_channel
*channel
;
824 channel
= zmalloc(sizeof(*channel
));
825 if (channel
== NULL
) {
826 PERROR("malloc struct lttng_consumer_channel");
829 channel
->key
= channel_key
;
830 channel
->shm_fd
= shm_fd
;
831 channel
->wait_fd
= wait_fd
;
832 channel
->mmap_len
= mmap_len
;
833 channel
->max_sb_size
= max_sb_size
;
834 channel
->refcount
= 0;
835 channel
->nb_init_streams
= nb_init_streams
;
836 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
838 switch (consumer_data
.type
) {
839 case LTTNG_CONSUMER_KERNEL
:
840 channel
->mmap_base
= NULL
;
841 channel
->mmap_len
= 0;
843 case LTTNG_CONSUMER32_UST
:
844 case LTTNG_CONSUMER64_UST
:
845 ret
= lttng_ustconsumer_allocate_channel(channel
);
852 ERR("Unknown consumer_data type");
856 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
857 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
858 (unsigned long long) channel
->mmap_len
,
859 (unsigned long long) channel
->max_sb_size
);
865 * Add a channel to the global list protected by a mutex.
867 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
869 struct lttng_ht_node_ulong
*node
;
870 struct lttng_ht_iter iter
;
872 pthread_mutex_lock(&consumer_data
.lock
);
873 /* Steal channel identifier, for UST */
874 consumer_steal_channel_key(channel
->key
);
877 lttng_ht_lookup(consumer_data
.channel_ht
,
878 (void *)((unsigned long) channel
->key
), &iter
);
879 node
= lttng_ht_iter_get_node_ulong(&iter
);
881 /* Channel already exist. Ignore the insertion */
885 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
889 pthread_mutex_unlock(&consumer_data
.lock
);
895 * Allocate the pollfd structure and the local view of the out fds to avoid
896 * doing a lookup in the linked list and concurrency issues when writing is
897 * needed. Called with consumer_data.lock held.
899 * Returns the number of fds in the structures.
901 static int consumer_update_poll_array(
902 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
903 struct lttng_consumer_stream
**local_stream
, struct lttng_ht
*ht
)
906 struct lttng_ht_iter iter
;
907 struct lttng_consumer_stream
*stream
;
909 DBG("Updating poll fd array");
911 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
913 * Only active streams with an active end point can be added to the
914 * poll set and local stream storage of the thread.
916 * There is a potential race here for endpoint_status to be updated
917 * just after the check. However, this is OK since the stream(s) will
918 * be deleted once the thread is notified that the end point state has
919 * changed where this function will be called back again.
921 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
922 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
925 DBG("Active FD %d", stream
->wait_fd
);
926 (*pollfd
)[i
].fd
= stream
->wait_fd
;
927 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
928 local_stream
[i
] = stream
;
934 * Insert the consumer_data_pipe at the end of the array and don't
935 * increment i so nb_fd is the number of real FD.
937 (*pollfd
)[i
].fd
= ctx
->consumer_data_pipe
[0];
938 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
943 * Poll on the should_quit pipe and the command socket return -1 on error and
944 * should exit, 0 if data is available on the command socket
946 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
951 num_rdy
= poll(consumer_sockpoll
, 2, -1);
954 * Restart interrupted system call.
956 if (errno
== EINTR
) {
959 PERROR("Poll error");
962 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
963 DBG("consumer_should_quit wake up");
973 * Set the error socket.
975 void lttng_consumer_set_error_sock(
976 struct lttng_consumer_local_data
*ctx
, int sock
)
978 ctx
->consumer_error_socket
= sock
;
982 * Set the command socket path.
984 void lttng_consumer_set_command_sock_path(
985 struct lttng_consumer_local_data
*ctx
, char *sock
)
987 ctx
->consumer_command_sock_path
= sock
;
991 * Send return code to the session daemon.
992 * If the socket is not defined, we return 0, it is not a fatal error
994 int lttng_consumer_send_error(
995 struct lttng_consumer_local_data
*ctx
, int cmd
)
997 if (ctx
->consumer_error_socket
> 0) {
998 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
999 sizeof(enum lttcomm_sessiond_command
));
1006 * Close all the tracefiles and stream fds, should be called when all instances
1009 void lttng_consumer_cleanup(void)
1011 struct lttng_ht_iter iter
;
1012 struct lttng_ht_node_ulong
*node
;
1016 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
1018 struct lttng_consumer_channel
*channel
=
1019 caa_container_of(node
, struct lttng_consumer_channel
, node
);
1020 consumer_del_channel(channel
);
1025 lttng_ht_destroy(consumer_data
.channel_ht
);
1029 * Called from signal handler.
1031 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1036 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1037 } while (ret
< 0 && errno
== EINTR
);
1039 PERROR("write consumer quit");
1042 DBG("Consumer flag that it should quit");
1045 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1048 int outfd
= stream
->out_fd
;
1051 * This does a blocking write-and-wait on any page that belongs to the
1052 * subbuffer prior to the one we just wrote.
1053 * Don't care about error values, as these are just hints and ways to
1054 * limit the amount of page cache used.
1056 if (orig_offset
< stream
->chan
->max_sb_size
) {
1059 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
1060 stream
->chan
->max_sb_size
,
1061 SYNC_FILE_RANGE_WAIT_BEFORE
1062 | SYNC_FILE_RANGE_WRITE
1063 | SYNC_FILE_RANGE_WAIT_AFTER
);
1065 * Give hints to the kernel about how we access the file:
1066 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1069 * We need to call fadvise again after the file grows because the
1070 * kernel does not seem to apply fadvise to non-existing parts of the
1073 * Call fadvise _after_ having waited for the page writeback to
1074 * complete because the dirty page writeback semantic is not well
1075 * defined. So it can be expected to lead to lower throughput in
1078 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
1079 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
1083 * Initialise the necessary environnement :
1084 * - create a new context
1085 * - create the poll_pipe
1086 * - create the should_quit pipe (for signal handler)
1087 * - create the thread pipe (for splice)
1089 * Takes a function pointer as argument, this function is called when data is
1090 * available on a buffer. This function is responsible to do the
1091 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1092 * buffer configuration and then kernctl_put_next_subbuf at the end.
1094 * Returns a pointer to the new context or NULL on error.
1096 struct lttng_consumer_local_data
*lttng_consumer_create(
1097 enum lttng_consumer_type type
,
1098 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1099 struct lttng_consumer_local_data
*ctx
),
1100 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1101 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1102 int (*update_stream
)(int stream_key
, uint32_t state
))
1105 struct lttng_consumer_local_data
*ctx
;
1107 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1108 consumer_data
.type
== type
);
1109 consumer_data
.type
= type
;
1111 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1113 PERROR("allocating context");
1117 ctx
->consumer_error_socket
= -1;
1118 /* assign the callbacks */
1119 ctx
->on_buffer_ready
= buffer_ready
;
1120 ctx
->on_recv_channel
= recv_channel
;
1121 ctx
->on_recv_stream
= recv_stream
;
1122 ctx
->on_update_stream
= update_stream
;
1124 ret
= pipe(ctx
->consumer_data_pipe
);
1126 PERROR("Error creating poll pipe");
1127 goto error_poll_pipe
;
1130 /* set read end of the pipe to non-blocking */
1131 ret
= fcntl(ctx
->consumer_data_pipe
[0], F_SETFL
, O_NONBLOCK
);
1133 PERROR("fcntl O_NONBLOCK");
1134 goto error_poll_fcntl
;
1137 /* set write end of the pipe to non-blocking */
1138 ret
= fcntl(ctx
->consumer_data_pipe
[1], F_SETFL
, O_NONBLOCK
);
1140 PERROR("fcntl O_NONBLOCK");
1141 goto error_poll_fcntl
;
1144 ret
= pipe(ctx
->consumer_should_quit
);
1146 PERROR("Error creating recv pipe");
1147 goto error_quit_pipe
;
1150 ret
= pipe(ctx
->consumer_thread_pipe
);
1152 PERROR("Error creating thread pipe");
1153 goto error_thread_pipe
;
1156 ret
= utils_create_pipe(ctx
->consumer_metadata_pipe
);
1158 goto error_metadata_pipe
;
1161 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1163 goto error_splice_pipe
;
1169 utils_close_pipe(ctx
->consumer_metadata_pipe
);
1170 error_metadata_pipe
:
1171 utils_close_pipe(ctx
->consumer_thread_pipe
);
1173 for (i
= 0; i
< 2; i
++) {
1176 err
= close(ctx
->consumer_should_quit
[i
]);
1183 for (i
= 0; i
< 2; i
++) {
1186 err
= close(ctx
->consumer_data_pipe
[i
]);
1198 * Close all fds associated with the instance and free the context.
1200 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1204 DBG("Consumer destroying it. Closing everything.");
1206 ret
= close(ctx
->consumer_error_socket
);
1210 ret
= close(ctx
->consumer_thread_pipe
[0]);
1214 ret
= close(ctx
->consumer_thread_pipe
[1]);
1218 ret
= close(ctx
->consumer_data_pipe
[0]);
1222 ret
= close(ctx
->consumer_data_pipe
[1]);
1226 ret
= close(ctx
->consumer_should_quit
[0]);
1230 ret
= close(ctx
->consumer_should_quit
[1]);
1234 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1236 unlink(ctx
->consumer_command_sock_path
);
1241 * Write the metadata stream id on the specified file descriptor.
1243 static int write_relayd_metadata_id(int fd
,
1244 struct lttng_consumer_stream
*stream
,
1245 struct consumer_relayd_sock_pair
*relayd
,
1246 unsigned long padding
)
1249 struct lttcomm_relayd_metadata_payload hdr
;
1251 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1252 hdr
.padding_size
= htobe32(padding
);
1254 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1255 } while (ret
< 0 && errno
== EINTR
);
1257 PERROR("write metadata stream id");
1260 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1261 stream
->relayd_stream_id
, padding
);
1268 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1269 * core function for writing trace buffers to either the local filesystem or
1272 * It must be called with the stream lock held.
1274 * Careful review MUST be put if any changes occur!
1276 * Returns the number of bytes written
1278 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1279 struct lttng_consumer_local_data
*ctx
,
1280 struct lttng_consumer_stream
*stream
, unsigned long len
,
1281 unsigned long padding
)
1283 unsigned long mmap_offset
;
1284 ssize_t ret
= 0, written
= 0;
1285 off_t orig_offset
= stream
->out_fd_offset
;
1286 /* Default is on the disk */
1287 int outfd
= stream
->out_fd
;
1288 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1289 unsigned int relayd_hang_up
= 0;
1291 /* RCU lock for the relayd pointer */
1294 /* Flag that the current stream if set for network streaming. */
1295 if (stream
->net_seq_idx
!= -1) {
1296 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1297 if (relayd
== NULL
) {
1302 /* get the offset inside the fd to mmap */
1303 switch (consumer_data
.type
) {
1304 case LTTNG_CONSUMER_KERNEL
:
1305 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1307 case LTTNG_CONSUMER32_UST
:
1308 case LTTNG_CONSUMER64_UST
:
1309 ret
= lttng_ustctl_get_mmap_read_offset(stream
->chan
->handle
,
1310 stream
->buf
, &mmap_offset
);
1313 ERR("Unknown consumer_data type");
1318 PERROR("tracer ctl get_mmap_read_offset");
1323 /* Handle stream on the relayd if the output is on the network */
1325 unsigned long netlen
= len
;
1328 * Lock the control socket for the complete duration of the function
1329 * since from this point on we will use the socket.
1331 if (stream
->metadata_flag
) {
1332 /* Metadata requires the control socket. */
1333 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1334 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1337 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1339 /* Use the returned socket. */
1342 /* Write metadata stream id before payload */
1343 if (stream
->metadata_flag
) {
1344 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1347 /* Socket operation failed. We consider the relayd dead */
1348 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1356 /* Socket operation failed. We consider the relayd dead */
1357 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1361 /* Else, use the default set before which is the filesystem. */
1364 /* No streaming, we have to set the len with the full padding */
1370 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
1371 } while (ret
< 0 && errno
== EINTR
);
1372 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1374 PERROR("Error in file write");
1378 /* Socket operation failed. We consider the relayd dead */
1379 if (errno
== EPIPE
|| errno
== EINVAL
) {
1384 } else if (ret
> len
) {
1385 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1393 /* This call is useless on a socket so better save a syscall. */
1395 /* This won't block, but will start writeout asynchronously */
1396 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1397 SYNC_FILE_RANGE_WRITE
);
1398 stream
->out_fd_offset
+= ret
;
1402 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1406 * This is a special case that the relayd has closed its socket. Let's
1407 * cleanup the relayd object and all associated streams.
1409 if (relayd
&& relayd_hang_up
) {
1410 cleanup_relayd(relayd
, ctx
);
1414 /* Unlock only if ctrl socket used */
1415 if (relayd
&& stream
->metadata_flag
) {
1416 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1424 * Splice the data from the ring buffer to the tracefile.
1426 * It must be called with the stream lock held.
1428 * Returns the number of bytes spliced.
1430 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1431 struct lttng_consumer_local_data
*ctx
,
1432 struct lttng_consumer_stream
*stream
, unsigned long len
,
1433 unsigned long padding
)
1435 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1437 off_t orig_offset
= stream
->out_fd_offset
;
1438 int fd
= stream
->wait_fd
;
1439 /* Default is on the disk */
1440 int outfd
= stream
->out_fd
;
1441 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1443 unsigned int relayd_hang_up
= 0;
1445 switch (consumer_data
.type
) {
1446 case LTTNG_CONSUMER_KERNEL
:
1448 case LTTNG_CONSUMER32_UST
:
1449 case LTTNG_CONSUMER64_UST
:
1450 /* Not supported for user space tracing */
1453 ERR("Unknown consumer_data type");
1457 /* RCU lock for the relayd pointer */
1460 /* Flag that the current stream if set for network streaming. */
1461 if (stream
->net_seq_idx
!= -1) {
1462 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1463 if (relayd
== NULL
) {
1469 * Choose right pipe for splice. Metadata and trace data are handled by
1470 * different threads hence the use of two pipes in order not to race or
1471 * corrupt the written data.
1473 if (stream
->metadata_flag
) {
1474 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1476 splice_pipe
= ctx
->consumer_thread_pipe
;
1479 /* Write metadata stream id before payload */
1481 int total_len
= len
;
1483 if (stream
->metadata_flag
) {
1485 * Lock the control socket for the complete duration of the function
1486 * since from this point on we will use the socket.
1488 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1490 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1494 /* Socket operation failed. We consider the relayd dead */
1495 if (ret
== -EBADF
) {
1496 WARN("Remote relayd disconnected. Stopping");
1503 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1506 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1508 /* Use the returned socket. */
1511 /* Socket operation failed. We consider the relayd dead */
1512 if (ret
== -EBADF
) {
1513 WARN("Remote relayd disconnected. Stopping");
1520 /* No streaming, we have to set the len with the full padding */
1525 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1526 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1527 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1528 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1529 DBG("splice chan to pipe, ret %zd", ret_splice
);
1530 if (ret_splice
< 0) {
1531 PERROR("Error in relay splice");
1533 written
= ret_splice
;
1539 /* Handle stream on the relayd if the output is on the network */
1541 if (stream
->metadata_flag
) {
1542 size_t metadata_payload_size
=
1543 sizeof(struct lttcomm_relayd_metadata_payload
);
1545 /* Update counter to fit the spliced data */
1546 ret_splice
+= metadata_payload_size
;
1547 len
+= metadata_payload_size
;
1549 * We do this so the return value can match the len passed as
1550 * argument to this function.
1552 written
-= metadata_payload_size
;
1556 /* Splice data out */
1557 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1558 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1559 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1560 if (ret_splice
< 0) {
1561 PERROR("Error in file splice");
1563 written
= ret_splice
;
1565 /* Socket operation failed. We consider the relayd dead */
1566 if (errno
== EBADF
|| errno
== EPIPE
) {
1567 WARN("Remote relayd disconnected. Stopping");
1573 } else if (ret_splice
> len
) {
1575 PERROR("Wrote more data than requested %zd (len: %lu)",
1577 written
+= ret_splice
;
1583 /* This call is useless on a socket so better save a syscall. */
1585 /* This won't block, but will start writeout asynchronously */
1586 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1587 SYNC_FILE_RANGE_WRITE
);
1588 stream
->out_fd_offset
+= ret_splice
;
1590 written
+= ret_splice
;
1592 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1600 * This is a special case that the relayd has closed its socket. Let's
1601 * cleanup the relayd object and all associated streams.
1603 if (relayd
&& relayd_hang_up
) {
1604 cleanup_relayd(relayd
, ctx
);
1605 /* Skip splice error so the consumer does not fail */
1610 /* send the appropriate error description to sessiond */
1613 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1616 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1619 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1624 if (relayd
&& stream
->metadata_flag
) {
1625 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1633 * Take a snapshot for a specific fd
1635 * Returns 0 on success, < 0 on error
1637 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1638 struct lttng_consumer_stream
*stream
)
1640 switch (consumer_data
.type
) {
1641 case LTTNG_CONSUMER_KERNEL
:
1642 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1643 case LTTNG_CONSUMER32_UST
:
1644 case LTTNG_CONSUMER64_UST
:
1645 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1647 ERR("Unknown consumer_data type");
1655 * Get the produced position
1657 * Returns 0 on success, < 0 on error
1659 int lttng_consumer_get_produced_snapshot(
1660 struct lttng_consumer_local_data
*ctx
,
1661 struct lttng_consumer_stream
*stream
,
1664 switch (consumer_data
.type
) {
1665 case LTTNG_CONSUMER_KERNEL
:
1666 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1667 case LTTNG_CONSUMER32_UST
:
1668 case LTTNG_CONSUMER64_UST
:
1669 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1671 ERR("Unknown consumer_data type");
1677 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1678 int sock
, struct pollfd
*consumer_sockpoll
)
1680 switch (consumer_data
.type
) {
1681 case LTTNG_CONSUMER_KERNEL
:
1682 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1683 case LTTNG_CONSUMER32_UST
:
1684 case LTTNG_CONSUMER64_UST
:
1685 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1687 ERR("Unknown consumer_data type");
1694 * Iterate over all streams of the hashtable and free them properly.
1696 * WARNING: *MUST* be used with data stream only.
1698 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1700 struct lttng_ht_iter iter
;
1701 struct lttng_consumer_stream
*stream
;
1708 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1710 * Ignore return value since we are currently cleaning up so any error
1713 (void) consumer_del_stream(stream
, ht
);
1717 lttng_ht_destroy(ht
);
1721 * Iterate over all streams of the hashtable and free them properly.
1723 * XXX: Should not be only for metadata stream or else use an other name.
1725 static void destroy_stream_ht(struct lttng_ht
*ht
)
1727 struct lttng_ht_iter iter
;
1728 struct lttng_consumer_stream
*stream
;
1735 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1737 * Ignore return value since we are currently cleaning up so any error
1740 (void) consumer_del_metadata_stream(stream
, ht
);
1744 lttng_ht_destroy(ht
);
1748 * Clean up a metadata stream and free its memory.
1750 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1751 struct lttng_ht
*ht
)
1754 struct lttng_ht_iter iter
;
1755 struct lttng_consumer_channel
*free_chan
= NULL
;
1756 struct consumer_relayd_sock_pair
*relayd
;
1760 * This call should NEVER receive regular stream. It must always be
1761 * metadata stream and this is crucial for data structure synchronization.
1763 assert(stream
->metadata_flag
);
1765 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1768 /* Means the stream was allocated but not successfully added */
1772 pthread_mutex_lock(&consumer_data
.lock
);
1773 pthread_mutex_lock(&stream
->lock
);
1775 switch (consumer_data
.type
) {
1776 case LTTNG_CONSUMER_KERNEL
:
1777 if (stream
->mmap_base
!= NULL
) {
1778 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1780 PERROR("munmap metadata stream");
1784 case LTTNG_CONSUMER32_UST
:
1785 case LTTNG_CONSUMER64_UST
:
1786 lttng_ustconsumer_del_stream(stream
);
1789 ERR("Unknown consumer_data type");
1795 iter
.iter
.node
= &stream
->node
.node
;
1796 ret
= lttng_ht_del(ht
, &iter
);
1799 /* Remove node session id from the consumer_data stream ht */
1800 iter
.iter
.node
= &stream
->node_session_id
.node
;
1801 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1805 if (stream
->out_fd
>= 0) {
1806 ret
= close(stream
->out_fd
);
1812 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
1813 ret
= close(stream
->wait_fd
);
1819 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
1820 ret
= close(stream
->shm_fd
);
1826 /* Check and cleanup relayd */
1828 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1829 if (relayd
!= NULL
) {
1830 uatomic_dec(&relayd
->refcount
);
1831 assert(uatomic_read(&relayd
->refcount
) >= 0);
1833 /* Closing streams requires to lock the control socket. */
1834 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1835 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1836 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1837 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1839 DBG("Unable to close stream on the relayd. Continuing");
1841 * Continue here. There is nothing we can do for the relayd.
1842 * Chances are that the relayd has closed the socket so we just
1843 * continue cleaning up.
1847 /* Both conditions are met, we destroy the relayd. */
1848 if (uatomic_read(&relayd
->refcount
) == 0 &&
1849 uatomic_read(&relayd
->destroy_flag
)) {
1850 destroy_relayd(relayd
);
1855 /* Atomically decrement channel refcount since other threads can use it. */
1856 uatomic_dec(&stream
->chan
->refcount
);
1857 if (!uatomic_read(&stream
->chan
->refcount
)
1858 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
1859 /* Go for channel deletion! */
1860 free_chan
= stream
->chan
;
1864 pthread_mutex_unlock(&stream
->lock
);
1865 pthread_mutex_unlock(&consumer_data
.lock
);
1868 consumer_del_channel(free_chan
);
1872 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1876 * Action done with the metadata stream when adding it to the consumer internal
1877 * data structures to handle it.
1879 static int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
,
1880 struct lttng_ht
*ht
)
1883 struct consumer_relayd_sock_pair
*relayd
;
1884 struct lttng_ht_iter iter
;
1885 struct lttng_ht_node_ulong
*node
;
1890 DBG3("Adding metadata stream %d to hash table", stream
->wait_fd
);
1892 pthread_mutex_lock(&consumer_data
.lock
);
1893 pthread_mutex_lock(&stream
->lock
);
1896 * From here, refcounts are updated so be _careful_ when returning an error
1903 * Lookup the stream just to make sure it does not exist in our internal
1904 * state. This should NEVER happen.
1906 lttng_ht_lookup(ht
, (void *)((unsigned long) stream
->wait_fd
), &iter
);
1907 node
= lttng_ht_iter_get_node_ulong(&iter
);
1910 /* Find relayd and, if one is found, increment refcount. */
1911 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1912 if (relayd
!= NULL
) {
1913 uatomic_inc(&relayd
->refcount
);
1916 /* Update channel refcount once added without error(s). */
1917 uatomic_inc(&stream
->chan
->refcount
);
1920 * When nb_init_streams reaches 0, we don't need to trigger any action in
1921 * terms of destroying the associated channel, because the action that
1922 * causes the count to become 0 also causes a stream to be added. The
1923 * channel deletion will thus be triggered by the following removal of this
1926 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
1927 uatomic_dec(&stream
->chan
->nb_init_streams
);
1930 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
1933 * Add stream to the stream_list_ht of the consumer data. No need to steal
1934 * the key since the HT does not use it and we allow to add redundant keys
1937 lttng_ht_add_ulong(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
1941 pthread_mutex_unlock(&stream
->lock
);
1942 pthread_mutex_unlock(&consumer_data
.lock
);
1947 * Delete data stream that are flagged for deletion (endpoint_status).
1949 static void validate_endpoint_status_data_stream(void)
1951 struct lttng_ht_iter iter
;
1952 struct lttng_consumer_stream
*stream
;
1954 DBG("Consumer delete flagged data stream");
1957 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1958 /* Validate delete flag of the stream */
1959 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
1962 /* Delete it right now */
1963 consumer_del_stream(stream
, data_ht
);
1969 * Delete metadata stream that are flagged for deletion (endpoint_status).
1971 static void validate_endpoint_status_metadata_stream(
1972 struct lttng_poll_event
*pollset
)
1974 struct lttng_ht_iter iter
;
1975 struct lttng_consumer_stream
*stream
;
1977 DBG("Consumer delete flagged metadata stream");
1982 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1983 /* Validate delete flag of the stream */
1984 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
1988 * Remove from pollset so the metadata thread can continue without
1989 * blocking on a deleted stream.
1991 lttng_poll_del(pollset
, stream
->wait_fd
);
1993 /* Delete it right now */
1994 consumer_del_metadata_stream(stream
, metadata_ht
);
2000 * Thread polls on metadata file descriptor and write them on disk or on the
2003 void *consumer_thread_metadata_poll(void *data
)
2006 uint32_t revents
, nb_fd
;
2007 struct lttng_consumer_stream
*stream
= NULL
;
2008 struct lttng_ht_iter iter
;
2009 struct lttng_ht_node_ulong
*node
;
2010 struct lttng_poll_event events
;
2011 struct lttng_consumer_local_data
*ctx
= data
;
2014 rcu_register_thread();
2016 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2018 /* ENOMEM at this point. Better to bail out. */
2022 DBG("Thread metadata poll started");
2024 /* Size is set to 1 for the consumer_metadata pipe */
2025 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2027 ERR("Poll set creation failed");
2031 ret
= lttng_poll_add(&events
, ctx
->consumer_metadata_pipe
[0], LPOLLIN
);
2037 DBG("Metadata main loop started");
2040 lttng_poll_reset(&events
);
2042 nb_fd
= LTTNG_POLL_GETNB(&events
);
2044 /* Only the metadata pipe is set */
2045 if (nb_fd
== 0 && consumer_quit
== 1) {
2050 DBG("Metadata poll wait with %d fd(s)", nb_fd
);
2051 ret
= lttng_poll_wait(&events
, -1);
2052 DBG("Metadata event catched in thread");
2054 if (errno
== EINTR
) {
2055 ERR("Poll EINTR catched");
2061 /* From here, the event is a metadata wait fd */
2062 for (i
= 0; i
< nb_fd
; i
++) {
2063 revents
= LTTNG_POLL_GETEV(&events
, i
);
2064 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2066 /* Just don't waste time if no returned events for the fd */
2071 if (pollfd
== ctx
->consumer_metadata_pipe
[0]) {
2072 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2073 DBG("Metadata thread pipe hung up");
2075 * Remove the pipe from the poll set and continue the loop
2076 * since their might be data to consume.
2078 lttng_poll_del(&events
, ctx
->consumer_metadata_pipe
[0]);
2079 ret
= close(ctx
->consumer_metadata_pipe
[0]);
2081 PERROR("close metadata pipe");
2084 } else if (revents
& LPOLLIN
) {
2086 /* Get the stream pointer received */
2087 ret
= read(pollfd
, &stream
, sizeof(stream
));
2088 } while (ret
< 0 && errno
== EINTR
);
2090 ret
< sizeof(struct lttng_consumer_stream
*)) {
2091 PERROR("read metadata stream");
2093 * Let's continue here and hope we can still work
2094 * without stopping the consumer. XXX: Should we?
2099 /* A NULL stream means that the state has changed. */
2100 if (stream
== NULL
) {
2101 /* Check for deleted streams. */
2102 validate_endpoint_status_metadata_stream(&events
);
2106 DBG("Adding metadata stream %d to poll set",
2109 ret
= consumer_add_metadata_stream(stream
, metadata_ht
);
2111 ERR("Unable to add metadata stream");
2112 /* Stream was not setup properly. Continuing. */
2113 consumer_del_metadata_stream(stream
, NULL
);
2117 /* Add metadata stream to the global poll events list */
2118 lttng_poll_add(&events
, stream
->wait_fd
,
2119 LPOLLIN
| LPOLLPRI
);
2122 /* Handle other stream */
2127 lttng_ht_lookup(metadata_ht
, (void *)((unsigned long) pollfd
),
2129 node
= lttng_ht_iter_get_node_ulong(&iter
);
2132 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2135 /* Check for error event */
2136 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2137 DBG("Metadata fd %d is hup|err.", pollfd
);
2138 if (!stream
->hangup_flush_done
2139 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2140 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2141 DBG("Attempting to flush and consume the UST buffers");
2142 lttng_ustconsumer_on_stream_hangup(stream
);
2144 /* We just flushed the stream now read it. */
2146 len
= ctx
->on_buffer_ready(stream
, ctx
);
2148 * We don't check the return value here since if we get
2149 * a negative len, it means an error occured thus we
2150 * simply remove it from the poll set and free the
2156 lttng_poll_del(&events
, stream
->wait_fd
);
2158 * This call update the channel states, closes file descriptors
2159 * and securely free the stream.
2161 consumer_del_metadata_stream(stream
, metadata_ht
);
2162 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2163 /* Get the data out of the metadata file descriptor */
2164 DBG("Metadata available on fd %d", pollfd
);
2165 assert(stream
->wait_fd
== pollfd
);
2167 len
= ctx
->on_buffer_ready(stream
, ctx
);
2168 /* It's ok to have an unavailable sub-buffer */
2169 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2170 /* Clean up stream from consumer and free it. */
2171 lttng_poll_del(&events
, stream
->wait_fd
);
2172 consumer_del_metadata_stream(stream
, metadata_ht
);
2173 } else if (len
> 0) {
2174 stream
->data_read
= 1;
2178 /* Release RCU lock for the stream looked up */
2185 DBG("Metadata poll thread exiting");
2186 lttng_poll_clean(&events
);
2188 destroy_stream_ht(metadata_ht
);
2190 rcu_unregister_thread();
2195 * This thread polls the fds in the set to consume the data and write
2196 * it to tracefile if necessary.
2198 void *consumer_thread_data_poll(void *data
)
2200 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2201 struct pollfd
*pollfd
= NULL
;
2202 /* local view of the streams */
2203 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2204 /* local view of consumer_data.fds_count */
2206 struct lttng_consumer_local_data
*ctx
= data
;
2209 rcu_register_thread();
2211 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2212 if (data_ht
== NULL
) {
2213 /* ENOMEM at this point. Better to bail out. */
2217 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
2224 * the fds set has been updated, we need to update our
2225 * local array as well
2227 pthread_mutex_lock(&consumer_data
.lock
);
2228 if (consumer_data
.need_update
) {
2229 if (pollfd
!= NULL
) {
2233 if (local_stream
!= NULL
) {
2235 local_stream
= NULL
;
2238 /* allocate for all fds + 1 for the consumer_data_pipe */
2239 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2240 if (pollfd
== NULL
) {
2241 PERROR("pollfd malloc");
2242 pthread_mutex_unlock(&consumer_data
.lock
);
2246 /* allocate for all fds + 1 for the consumer_data_pipe */
2247 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2248 sizeof(struct lttng_consumer_stream
));
2249 if (local_stream
== NULL
) {
2250 PERROR("local_stream malloc");
2251 pthread_mutex_unlock(&consumer_data
.lock
);
2254 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
,
2257 ERR("Error in allocating pollfd or local_outfds");
2258 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2259 pthread_mutex_unlock(&consumer_data
.lock
);
2263 consumer_data
.need_update
= 0;
2265 pthread_mutex_unlock(&consumer_data
.lock
);
2267 /* No FDs and consumer_quit, consumer_cleanup the thread */
2268 if (nb_fd
== 0 && consumer_quit
== 1) {
2271 /* poll on the array of fds */
2273 DBG("polling on %d fd", nb_fd
+ 1);
2274 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2275 DBG("poll num_rdy : %d", num_rdy
);
2276 if (num_rdy
== -1) {
2278 * Restart interrupted system call.
2280 if (errno
== EINTR
) {
2283 PERROR("Poll error");
2284 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2286 } else if (num_rdy
== 0) {
2287 DBG("Polling thread timed out");
2292 * If the consumer_data_pipe triggered poll go directly to the
2293 * beginning of the loop to update the array. We want to prioritize
2294 * array update over low-priority reads.
2296 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2297 size_t pipe_readlen
;
2299 DBG("consumer_data_pipe wake up");
2300 /* Consume 1 byte of pipe data */
2302 pipe_readlen
= read(ctx
->consumer_data_pipe
[0], &new_stream
,
2303 sizeof(new_stream
));
2304 } while (pipe_readlen
== -1 && errno
== EINTR
);
2307 * If the stream is NULL, just ignore it. It's also possible that
2308 * the sessiond poll thread changed the consumer_quit state and is
2309 * waking us up to test it.
2311 if (new_stream
== NULL
) {
2312 validate_endpoint_status_data_stream();
2316 ret
= consumer_add_stream(new_stream
, data_ht
);
2318 ERR("Consumer add stream %d failed. Continuing",
2321 * At this point, if the add_stream fails, it is not in the
2322 * hash table thus passing the NULL value here.
2324 consumer_del_stream(new_stream
, NULL
);
2327 /* Continue to update the local streams and handle prio ones */
2331 /* Take care of high priority channels first. */
2332 for (i
= 0; i
< nb_fd
; i
++) {
2333 if (local_stream
[i
] == NULL
) {
2336 if (pollfd
[i
].revents
& POLLPRI
) {
2337 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2339 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2340 /* it's ok to have an unavailable sub-buffer */
2341 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2342 /* Clean the stream and free it. */
2343 consumer_del_stream(local_stream
[i
], data_ht
);
2344 local_stream
[i
] = NULL
;
2345 } else if (len
> 0) {
2346 local_stream
[i
]->data_read
= 1;
2352 * If we read high prio channel in this loop, try again
2353 * for more high prio data.
2359 /* Take care of low priority channels. */
2360 for (i
= 0; i
< nb_fd
; i
++) {
2361 if (local_stream
[i
] == NULL
) {
2364 if ((pollfd
[i
].revents
& POLLIN
) ||
2365 local_stream
[i
]->hangup_flush_done
) {
2366 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2367 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2368 /* it's ok to have an unavailable sub-buffer */
2369 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2370 /* Clean the stream and free it. */
2371 consumer_del_stream(local_stream
[i
], data_ht
);
2372 local_stream
[i
] = NULL
;
2373 } else if (len
> 0) {
2374 local_stream
[i
]->data_read
= 1;
2379 /* Handle hangup and errors */
2380 for (i
= 0; i
< nb_fd
; i
++) {
2381 if (local_stream
[i
] == NULL
) {
2384 if (!local_stream
[i
]->hangup_flush_done
2385 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2386 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2387 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2388 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2390 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2391 /* Attempt read again, for the data we just flushed. */
2392 local_stream
[i
]->data_read
= 1;
2395 * If the poll flag is HUP/ERR/NVAL and we have
2396 * read no data in this pass, we can remove the
2397 * stream from its hash table.
2399 if ((pollfd
[i
].revents
& POLLHUP
)) {
2400 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2401 if (!local_stream
[i
]->data_read
) {
2402 consumer_del_stream(local_stream
[i
], data_ht
);
2403 local_stream
[i
] = NULL
;
2406 } else if (pollfd
[i
].revents
& POLLERR
) {
2407 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2408 if (!local_stream
[i
]->data_read
) {
2409 consumer_del_stream(local_stream
[i
], data_ht
);
2410 local_stream
[i
] = NULL
;
2413 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2414 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2415 if (!local_stream
[i
]->data_read
) {
2416 consumer_del_stream(local_stream
[i
], data_ht
);
2417 local_stream
[i
] = NULL
;
2421 if (local_stream
[i
] != NULL
) {
2422 local_stream
[i
]->data_read
= 0;
2427 DBG("polling thread exiting");
2428 if (pollfd
!= NULL
) {
2432 if (local_stream
!= NULL
) {
2434 local_stream
= NULL
;
2438 * Close the write side of the pipe so epoll_wait() in
2439 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2440 * read side of the pipe. If we close them both, epoll_wait strangely does
2441 * not return and could create a endless wait period if the pipe is the
2442 * only tracked fd in the poll set. The thread will take care of closing
2445 ret
= close(ctx
->consumer_metadata_pipe
[1]);
2447 PERROR("close data pipe");
2450 destroy_data_stream_ht(data_ht
);
2452 rcu_unregister_thread();
2457 * This thread listens on the consumerd socket and receives the file
2458 * descriptors from the session daemon.
2460 void *consumer_thread_sessiond_poll(void *data
)
2462 int sock
= -1, client_socket
, ret
;
2464 * structure to poll for incoming data on communication socket avoids
2465 * making blocking sockets.
2467 struct pollfd consumer_sockpoll
[2];
2468 struct lttng_consumer_local_data
*ctx
= data
;
2470 rcu_register_thread();
2472 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2473 unlink(ctx
->consumer_command_sock_path
);
2474 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2475 if (client_socket
< 0) {
2476 ERR("Cannot create command socket");
2480 ret
= lttcomm_listen_unix_sock(client_socket
);
2485 DBG("Sending ready command to lttng-sessiond");
2486 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2487 /* return < 0 on error, but == 0 is not fatal */
2489 ERR("Error sending ready command to lttng-sessiond");
2493 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2495 PERROR("fcntl O_NONBLOCK");
2499 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2500 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2501 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2502 consumer_sockpoll
[1].fd
= client_socket
;
2503 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2505 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2508 DBG("Connection on client_socket");
2510 /* Blocking call, waiting for transmission */
2511 sock
= lttcomm_accept_unix_sock(client_socket
);
2516 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2518 PERROR("fcntl O_NONBLOCK");
2522 /* This socket is not useful anymore. */
2523 ret
= close(client_socket
);
2525 PERROR("close client_socket");
2529 /* update the polling structure to poll on the established socket */
2530 consumer_sockpoll
[1].fd
= sock
;
2531 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2534 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2537 DBG("Incoming command on sock");
2538 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2539 if (ret
== -ENOENT
) {
2540 DBG("Received STOP command");
2545 * This could simply be a session daemon quitting. Don't output
2548 DBG("Communication interrupted on command socket");
2551 if (consumer_quit
) {
2552 DBG("consumer_thread_receive_fds received quit from signal");
2555 DBG("received fds on sock");
2558 DBG("consumer_thread_receive_fds exiting");
2561 * when all fds have hung up, the polling thread
2567 * Notify the data poll thread to poll back again and test the
2568 * consumer_quit state that we just set so to quit gracefully.
2570 notify_thread_pipe(ctx
->consumer_data_pipe
[1]);
2572 /* Cleaning up possibly open sockets. */
2576 PERROR("close sock sessiond poll");
2579 if (client_socket
>= 0) {
2582 PERROR("close client_socket sessiond poll");
2586 rcu_unregister_thread();
2590 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2591 struct lttng_consumer_local_data
*ctx
)
2595 pthread_mutex_lock(&stream
->lock
);
2597 switch (consumer_data
.type
) {
2598 case LTTNG_CONSUMER_KERNEL
:
2599 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
2601 case LTTNG_CONSUMER32_UST
:
2602 case LTTNG_CONSUMER64_UST
:
2603 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2606 ERR("Unknown consumer_data type");
2612 pthread_mutex_unlock(&stream
->lock
);
2616 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2618 switch (consumer_data
.type
) {
2619 case LTTNG_CONSUMER_KERNEL
:
2620 return lttng_kconsumer_on_recv_stream(stream
);
2621 case LTTNG_CONSUMER32_UST
:
2622 case LTTNG_CONSUMER64_UST
:
2623 return lttng_ustconsumer_on_recv_stream(stream
);
2625 ERR("Unknown consumer_data type");
2632 * Allocate and set consumer data hash tables.
2634 void lttng_consumer_init(void)
2636 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2637 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2638 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2642 * Process the ADD_RELAYD command receive by a consumer.
2644 * This will create a relayd socket pair and add it to the relayd hash table.
2645 * The caller MUST acquire a RCU read side lock before calling it.
2647 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
2648 struct lttng_consumer_local_data
*ctx
, int sock
,
2649 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
)
2651 int fd
= -1, ret
= -1;
2652 struct consumer_relayd_sock_pair
*relayd
;
2654 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
2656 /* Get relayd reference if exists. */
2657 relayd
= consumer_find_relayd(net_seq_idx
);
2658 if (relayd
== NULL
) {
2659 /* Not found. Allocate one. */
2660 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
2661 if (relayd
== NULL
) {
2662 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
2667 /* Poll on consumer socket. */
2668 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2673 /* Get relayd socket from session daemon */
2674 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2675 if (ret
!= sizeof(fd
)) {
2676 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
2678 fd
= -1; /* Just in case it gets set with an invalid value. */
2682 /* Copy socket information and received FD */
2683 switch (sock_type
) {
2684 case LTTNG_STREAM_CONTROL
:
2685 /* Copy received lttcomm socket */
2686 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
2687 ret
= lttcomm_create_sock(&relayd
->control_sock
);
2688 /* Immediately try to close the created socket if valid. */
2689 if (relayd
->control_sock
.fd
>= 0) {
2690 if (close(relayd
->control_sock
.fd
)) {
2691 PERROR("close relayd control socket");
2694 /* Handle create_sock error. */
2699 /* Assign new file descriptor */
2700 relayd
->control_sock
.fd
= fd
;
2702 case LTTNG_STREAM_DATA
:
2703 /* Copy received lttcomm socket */
2704 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
2705 ret
= lttcomm_create_sock(&relayd
->data_sock
);
2706 /* Immediately try to close the created socket if valid. */
2707 if (relayd
->data_sock
.fd
>= 0) {
2708 if (close(relayd
->data_sock
.fd
)) {
2709 PERROR("close relayd data socket");
2712 /* Handle create_sock error. */
2717 /* Assign new file descriptor */
2718 relayd
->data_sock
.fd
= fd
;
2721 ERR("Unknown relayd socket type (%d)", sock_type
);
2725 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2726 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
2727 relayd
->net_seq_idx
, fd
);
2730 * Add relayd socket pair to consumer data hashtable. If object already
2731 * exists or on error, the function gracefully returns.
2739 /* Close received socket if valid. */
2742 PERROR("close received socket");
2749 * Try to lock the stream mutex.
2751 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
2753 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
2760 * Try to lock the stream mutex. On failure, we know that the stream is
2761 * being used else where hence there is data still being extracted.
2763 ret
= pthread_mutex_trylock(&stream
->lock
);
2765 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
2777 * Check if for a given session id there is still data needed to be extract
2780 * Return 1 if data is pending or else 0 meaning ready to be read.
2782 int consumer_data_pending(uint64_t id
)
2785 struct lttng_ht_iter iter
;
2786 struct lttng_ht
*ht
;
2787 struct lttng_consumer_stream
*stream
;
2788 struct consumer_relayd_sock_pair
*relayd
;
2789 int (*data_pending
)(struct lttng_consumer_stream
*);
2791 DBG("Consumer data pending command on session id %" PRIu64
, id
);
2794 pthread_mutex_lock(&consumer_data
.lock
);
2796 switch (consumer_data
.type
) {
2797 case LTTNG_CONSUMER_KERNEL
:
2798 data_pending
= lttng_kconsumer_data_pending
;
2800 case LTTNG_CONSUMER32_UST
:
2801 case LTTNG_CONSUMER64_UST
:
2802 data_pending
= lttng_ustconsumer_data_pending
;
2805 ERR("Unknown consumer data type");
2809 /* Ease our life a bit */
2810 ht
= consumer_data
.stream_list_ht
;
2812 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2813 ht
->hash_fct((void *)((unsigned long) id
), lttng_ht_seed
),
2814 ht
->match_fct
, (void *)((unsigned long) id
),
2815 &iter
.iter
, stream
, node_session_id
.node
) {
2816 /* If this call fails, the stream is being used hence data pending. */
2817 ret
= stream_try_lock(stream
);
2819 goto data_not_pending
;
2823 * A removed node from the hash table indicates that the stream has
2824 * been deleted thus having a guarantee that the buffers are closed
2825 * on the consumer side. However, data can still be transmitted
2826 * over the network so don't skip the relayd check.
2828 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
2830 /* Check the stream if there is data in the buffers. */
2831 ret
= data_pending(stream
);
2833 pthread_mutex_unlock(&stream
->lock
);
2834 goto data_not_pending
;
2839 if (stream
->net_seq_idx
!= -1) {
2840 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
2843 * At this point, if the relayd object is not available for the
2844 * given stream, it is because the relayd is being cleaned up
2845 * so every stream associated with it (for a session id value)
2846 * are or will be marked for deletion hence no data pending.
2848 pthread_mutex_unlock(&stream
->lock
);
2849 goto data_not_pending
;
2852 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
2853 if (stream
->metadata_flag
) {
2854 ret
= relayd_quiescent_control(&relayd
->control_sock
);
2856 ret
= relayd_data_pending(&relayd
->control_sock
,
2857 stream
->relayd_stream_id
, stream
->next_net_seq_num
);
2859 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2861 pthread_mutex_unlock(&stream
->lock
);
2862 goto data_not_pending
;
2865 pthread_mutex_unlock(&stream
->lock
);
2869 * Finding _no_ node in the hash table means that the stream(s) have been
2870 * removed thus data is guaranteed to be available for analysis from the
2871 * trace files. This is *only* true for local consumer and not network
2875 /* Data is available to be read by a viewer. */
2876 pthread_mutex_unlock(&consumer_data
.lock
);
2881 /* Data is still being extracted from buffers. */
2882 pthread_mutex_unlock(&consumer_data
.lock
);