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 * Iterate over the relayd hash table and destroy each element. Finally,
239 * destroy the whole hash table.
241 static void cleanup_relayd_ht(void)
243 struct lttng_ht_iter iter
;
244 struct consumer_relayd_sock_pair
*relayd
;
248 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
250 destroy_relayd(relayd
);
253 lttng_ht_destroy(consumer_data
.relayd_ht
);
259 * Update the end point status of all streams having the given network sequence
260 * index (relayd index).
262 * It's atomically set without having the stream mutex locked which is fine
263 * because we handle the write/read race with a pipe wakeup for each thread.
265 static void update_endpoint_status_by_netidx(int net_seq_idx
,
266 enum consumer_endpoint_status status
)
268 struct lttng_ht_iter iter
;
269 struct lttng_consumer_stream
*stream
;
271 DBG("Consumer set delete flag on stream by idx %d", net_seq_idx
);
275 /* Let's begin with metadata */
276 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
277 if (stream
->net_seq_idx
== net_seq_idx
) {
278 uatomic_set(&stream
->endpoint_status
, status
);
279 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
283 /* Follow up by the data streams */
284 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
285 if (stream
->net_seq_idx
== net_seq_idx
) {
286 uatomic_set(&stream
->endpoint_status
, status
);
287 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
294 * Cleanup a relayd object by flagging every associated streams for deletion,
295 * destroying the object meaning removing it from the relayd hash table,
296 * closing the sockets and freeing the memory in a RCU call.
298 * If a local data context is available, notify the threads that the streams'
299 * state have changed.
301 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
302 struct lttng_consumer_local_data
*ctx
)
308 DBG("Cleaning up relayd sockets");
310 /* Save the net sequence index before destroying the object */
311 netidx
= relayd
->net_seq_idx
;
314 * Delete the relayd from the relayd hash table, close the sockets and free
315 * the object in a RCU call.
317 destroy_relayd(relayd
);
319 /* Set inactive endpoint to all streams */
320 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
323 * With a local data context, notify the threads that the streams' state
324 * have changed. The write() action on the pipe acts as an "implicit"
325 * memory barrier ordering the updates of the end point status from the
326 * read of this status which happens AFTER receiving this notify.
329 notify_thread_pipe(ctx
->consumer_data_pipe
[1]);
330 notify_thread_pipe(ctx
->consumer_metadata_pipe
[1]);
335 * Flag a relayd socket pair for destruction. Destroy it if the refcount
338 * RCU read side lock MUST be aquired before calling this function.
340 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
344 /* Set destroy flag for this object */
345 uatomic_set(&relayd
->destroy_flag
, 1);
347 /* Destroy the relayd if refcount is 0 */
348 if (uatomic_read(&relayd
->refcount
) == 0) {
349 destroy_relayd(relayd
);
354 * Remove a stream from the global list protected by a mutex. This
355 * function is also responsible for freeing its data structures.
357 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
361 struct lttng_ht_iter iter
;
362 struct lttng_consumer_channel
*free_chan
= NULL
;
363 struct consumer_relayd_sock_pair
*relayd
;
367 DBG("Consumer del stream %d", stream
->wait_fd
);
370 /* Means the stream was allocated but not successfully added */
374 pthread_mutex_lock(&consumer_data
.lock
);
375 pthread_mutex_lock(&stream
->lock
);
377 switch (consumer_data
.type
) {
378 case LTTNG_CONSUMER_KERNEL
:
379 if (stream
->mmap_base
!= NULL
) {
380 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
386 case LTTNG_CONSUMER32_UST
:
387 case LTTNG_CONSUMER64_UST
:
388 lttng_ustconsumer_del_stream(stream
);
391 ERR("Unknown consumer_data type");
397 iter
.iter
.node
= &stream
->node
.node
;
398 ret
= lttng_ht_del(ht
, &iter
);
401 /* Remove node session id from the consumer_data stream ht */
402 iter
.iter
.node
= &stream
->node_session_id
.node
;
403 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
407 assert(consumer_data
.stream_count
> 0);
408 consumer_data
.stream_count
--;
410 if (stream
->out_fd
>= 0) {
411 ret
= close(stream
->out_fd
);
416 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
417 ret
= close(stream
->wait_fd
);
422 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
423 ret
= close(stream
->shm_fd
);
429 /* Check and cleanup relayd */
431 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
432 if (relayd
!= NULL
) {
433 uatomic_dec(&relayd
->refcount
);
434 assert(uatomic_read(&relayd
->refcount
) >= 0);
436 /* Closing streams requires to lock the control socket. */
437 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
438 ret
= relayd_send_close_stream(&relayd
->control_sock
,
439 stream
->relayd_stream_id
,
440 stream
->next_net_seq_num
- 1);
441 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
443 DBG("Unable to close stream on the relayd. Continuing");
445 * Continue here. There is nothing we can do for the relayd.
446 * Chances are that the relayd has closed the socket so we just
447 * continue cleaning up.
451 /* Both conditions are met, we destroy the relayd. */
452 if (uatomic_read(&relayd
->refcount
) == 0 &&
453 uatomic_read(&relayd
->destroy_flag
)) {
454 destroy_relayd(relayd
);
459 uatomic_dec(&stream
->chan
->refcount
);
460 if (!uatomic_read(&stream
->chan
->refcount
)
461 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
462 free_chan
= stream
->chan
;
466 consumer_data
.need_update
= 1;
467 pthread_mutex_unlock(&stream
->lock
);
468 pthread_mutex_unlock(&consumer_data
.lock
);
471 consumer_del_channel(free_chan
);
475 call_rcu(&stream
->node
.head
, consumer_free_stream
);
478 struct lttng_consumer_stream
*consumer_allocate_stream(
479 int channel_key
, int stream_key
,
480 int shm_fd
, int wait_fd
,
481 enum lttng_consumer_stream_state state
,
483 enum lttng_event_output output
,
484 const char *path_name
,
492 struct lttng_consumer_stream
*stream
;
494 stream
= zmalloc(sizeof(*stream
));
495 if (stream
== NULL
) {
496 PERROR("malloc struct lttng_consumer_stream");
497 *alloc_ret
= -ENOMEM
;
504 * Get stream's channel reference. Needed when adding the stream to the
507 stream
->chan
= consumer_find_channel(channel_key
);
509 *alloc_ret
= -ENOENT
;
510 ERR("Unable to find channel for stream %d", stream_key
);
514 stream
->key
= stream_key
;
515 stream
->shm_fd
= shm_fd
;
516 stream
->wait_fd
= wait_fd
;
518 stream
->out_fd_offset
= 0;
519 stream
->state
= state
;
520 stream
->mmap_len
= mmap_len
;
521 stream
->mmap_base
= NULL
;
522 stream
->output
= output
;
525 stream
->net_seq_idx
= net_index
;
526 stream
->metadata_flag
= metadata_flag
;
527 stream
->session_id
= session_id
;
528 strncpy(stream
->path_name
, path_name
, sizeof(stream
->path_name
));
529 stream
->path_name
[sizeof(stream
->path_name
) - 1] = '\0';
530 pthread_mutex_init(&stream
->lock
, NULL
);
533 * Index differently the metadata node because the thread is using an
534 * internal hash table to match streams in the metadata_ht to the epoll set
538 lttng_ht_node_init_ulong(&stream
->node
, stream
->wait_fd
);
540 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
543 /* Init session id node with the stream session id */
544 lttng_ht_node_init_ulong(&stream
->node_session_id
, stream
->session_id
);
547 * The cpu number is needed before using any ustctl_* actions. Ignored for
548 * the kernel so the value does not matter.
550 pthread_mutex_lock(&consumer_data
.lock
);
551 stream
->cpu
= stream
->chan
->cpucount
++;
552 pthread_mutex_unlock(&consumer_data
.lock
);
554 DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu,"
555 " out_fd %d, net_seq_idx %d, session_id %" PRIu64
,
556 stream
->path_name
, stream
->key
, stream
->shm_fd
, stream
->wait_fd
,
557 (unsigned long long) stream
->mmap_len
, stream
->out_fd
,
558 stream
->net_seq_idx
, stream
->session_id
);
571 * Add a stream to the global list protected by a mutex.
573 static int consumer_add_stream(struct lttng_consumer_stream
*stream
,
577 struct consumer_relayd_sock_pair
*relayd
;
582 DBG3("Adding consumer stream %d", stream
->key
);
584 pthread_mutex_lock(&consumer_data
.lock
);
585 pthread_mutex_lock(&stream
->lock
);
588 /* Steal stream identifier to avoid having streams with the same key */
589 consumer_steal_stream_key(stream
->key
, ht
);
591 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
594 * Add stream to the stream_list_ht of the consumer data. No need to steal
595 * the key since the HT does not use it and we allow to add redundant keys
598 lttng_ht_add_ulong(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
600 /* Check and cleanup relayd */
601 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
602 if (relayd
!= NULL
) {
603 uatomic_inc(&relayd
->refcount
);
606 /* Update channel refcount once added without error(s). */
607 uatomic_inc(&stream
->chan
->refcount
);
610 * When nb_init_streams reaches 0, we don't need to trigger any action in
611 * terms of destroying the associated channel, because the action that
612 * causes the count to become 0 also causes a stream to be added. The
613 * channel deletion will thus be triggered by the following removal of this
616 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
617 uatomic_dec(&stream
->chan
->nb_init_streams
);
620 /* Update consumer data once the node is inserted. */
621 consumer_data
.stream_count
++;
622 consumer_data
.need_update
= 1;
625 pthread_mutex_unlock(&stream
->lock
);
626 pthread_mutex_unlock(&consumer_data
.lock
);
632 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
633 * be acquired before calling this.
635 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
638 struct lttng_ht_node_ulong
*node
;
639 struct lttng_ht_iter iter
;
641 if (relayd
== NULL
) {
646 lttng_ht_lookup(consumer_data
.relayd_ht
,
647 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
648 node
= lttng_ht_iter_get_node_ulong(&iter
);
650 /* Relayd already exist. Ignore the insertion */
653 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
660 * Allocate and return a consumer relayd socket.
662 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
665 struct consumer_relayd_sock_pair
*obj
= NULL
;
667 /* Negative net sequence index is a failure */
668 if (net_seq_idx
< 0) {
672 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
674 PERROR("zmalloc relayd sock");
678 obj
->net_seq_idx
= net_seq_idx
;
680 obj
->destroy_flag
= 0;
681 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
682 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
689 * Find a relayd socket pair in the global consumer data.
691 * Return the object if found else NULL.
692 * RCU read-side lock must be held across this call and while using the
695 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
697 struct lttng_ht_iter iter
;
698 struct lttng_ht_node_ulong
*node
;
699 struct consumer_relayd_sock_pair
*relayd
= NULL
;
701 /* Negative keys are lookup failures */
706 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
708 node
= lttng_ht_iter_get_node_ulong(&iter
);
710 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
718 * Handle stream for relayd transmission if the stream applies for network
719 * streaming where the net sequence index is set.
721 * Return destination file descriptor or negative value on error.
723 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
724 size_t data_size
, unsigned long padding
,
725 struct consumer_relayd_sock_pair
*relayd
)
728 struct lttcomm_relayd_data_hdr data_hdr
;
734 /* Reset data header */
735 memset(&data_hdr
, 0, sizeof(data_hdr
));
737 if (stream
->metadata_flag
) {
738 /* Caller MUST acquire the relayd control socket lock */
739 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
744 /* Metadata are always sent on the control socket. */
745 outfd
= relayd
->control_sock
.fd
;
747 /* Set header with stream information */
748 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
749 data_hdr
.data_size
= htobe32(data_size
);
750 data_hdr
.padding_size
= htobe32(padding
);
751 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
752 /* Other fields are zeroed previously */
754 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
760 /* Set to go on data socket */
761 outfd
= relayd
->data_sock
.fd
;
769 void consumer_free_channel(struct rcu_head
*head
)
771 struct lttng_ht_node_ulong
*node
=
772 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
773 struct lttng_consumer_channel
*channel
=
774 caa_container_of(node
, struct lttng_consumer_channel
, node
);
780 * Remove a channel from the global list protected by a mutex. This
781 * function is also responsible for freeing its data structures.
783 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
786 struct lttng_ht_iter iter
;
788 DBG("Consumer delete channel key %d", channel
->key
);
790 pthread_mutex_lock(&consumer_data
.lock
);
792 switch (consumer_data
.type
) {
793 case LTTNG_CONSUMER_KERNEL
:
795 case LTTNG_CONSUMER32_UST
:
796 case LTTNG_CONSUMER64_UST
:
797 lttng_ustconsumer_del_channel(channel
);
800 ERR("Unknown consumer_data type");
806 iter
.iter
.node
= &channel
->node
.node
;
807 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
811 if (channel
->mmap_base
!= NULL
) {
812 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
817 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
818 ret
= close(channel
->wait_fd
);
823 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
824 ret
= close(channel
->shm_fd
);
830 call_rcu(&channel
->node
.head
, consumer_free_channel
);
832 pthread_mutex_unlock(&consumer_data
.lock
);
835 struct lttng_consumer_channel
*consumer_allocate_channel(
837 int shm_fd
, int wait_fd
,
839 uint64_t max_sb_size
,
840 unsigned int nb_init_streams
)
842 struct lttng_consumer_channel
*channel
;
845 channel
= zmalloc(sizeof(*channel
));
846 if (channel
== NULL
) {
847 PERROR("malloc struct lttng_consumer_channel");
850 channel
->key
= channel_key
;
851 channel
->shm_fd
= shm_fd
;
852 channel
->wait_fd
= wait_fd
;
853 channel
->mmap_len
= mmap_len
;
854 channel
->max_sb_size
= max_sb_size
;
855 channel
->refcount
= 0;
856 channel
->nb_init_streams
= nb_init_streams
;
857 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
859 switch (consumer_data
.type
) {
860 case LTTNG_CONSUMER_KERNEL
:
861 channel
->mmap_base
= NULL
;
862 channel
->mmap_len
= 0;
864 case LTTNG_CONSUMER32_UST
:
865 case LTTNG_CONSUMER64_UST
:
866 ret
= lttng_ustconsumer_allocate_channel(channel
);
873 ERR("Unknown consumer_data type");
877 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
878 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
879 (unsigned long long) channel
->mmap_len
,
880 (unsigned long long) channel
->max_sb_size
);
886 * Add a channel to the global list protected by a mutex.
888 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
890 struct lttng_ht_node_ulong
*node
;
891 struct lttng_ht_iter iter
;
893 pthread_mutex_lock(&consumer_data
.lock
);
894 /* Steal channel identifier, for UST */
895 consumer_steal_channel_key(channel
->key
);
898 lttng_ht_lookup(consumer_data
.channel_ht
,
899 (void *)((unsigned long) channel
->key
), &iter
);
900 node
= lttng_ht_iter_get_node_ulong(&iter
);
902 /* Channel already exist. Ignore the insertion */
906 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
910 pthread_mutex_unlock(&consumer_data
.lock
);
916 * Allocate the pollfd structure and the local view of the out fds to avoid
917 * doing a lookup in the linked list and concurrency issues when writing is
918 * needed. Called with consumer_data.lock held.
920 * Returns the number of fds in the structures.
922 static int consumer_update_poll_array(
923 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
924 struct lttng_consumer_stream
**local_stream
, struct lttng_ht
*ht
)
927 struct lttng_ht_iter iter
;
928 struct lttng_consumer_stream
*stream
;
930 DBG("Updating poll fd array");
932 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
934 * Only active streams with an active end point can be added to the
935 * poll set and local stream storage of the thread.
937 * There is a potential race here for endpoint_status to be updated
938 * just after the check. However, this is OK since the stream(s) will
939 * be deleted once the thread is notified that the end point state has
940 * changed where this function will be called back again.
942 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
943 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
946 DBG("Active FD %d", stream
->wait_fd
);
947 (*pollfd
)[i
].fd
= stream
->wait_fd
;
948 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
949 local_stream
[i
] = stream
;
955 * Insert the consumer_data_pipe at the end of the array and don't
956 * increment i so nb_fd is the number of real FD.
958 (*pollfd
)[i
].fd
= ctx
->consumer_data_pipe
[0];
959 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
964 * Poll on the should_quit pipe and the command socket return -1 on error and
965 * should exit, 0 if data is available on the command socket
967 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
972 num_rdy
= poll(consumer_sockpoll
, 2, -1);
975 * Restart interrupted system call.
977 if (errno
== EINTR
) {
980 PERROR("Poll error");
983 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
984 DBG("consumer_should_quit wake up");
994 * Set the error socket.
996 void lttng_consumer_set_error_sock(
997 struct lttng_consumer_local_data
*ctx
, int sock
)
999 ctx
->consumer_error_socket
= sock
;
1003 * Set the command socket path.
1005 void lttng_consumer_set_command_sock_path(
1006 struct lttng_consumer_local_data
*ctx
, char *sock
)
1008 ctx
->consumer_command_sock_path
= sock
;
1012 * Send return code to the session daemon.
1013 * If the socket is not defined, we return 0, it is not a fatal error
1015 int lttng_consumer_send_error(
1016 struct lttng_consumer_local_data
*ctx
, int cmd
)
1018 if (ctx
->consumer_error_socket
> 0) {
1019 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1020 sizeof(enum lttcomm_sessiond_command
));
1027 * Close all the tracefiles and stream fds and MUST be called when all
1028 * instances are destroyed i.e. when all threads were joined and are ended.
1030 void lttng_consumer_cleanup(void)
1032 struct lttng_ht_iter iter
;
1033 struct lttng_ht_node_ulong
*node
;
1037 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
1039 struct lttng_consumer_channel
*channel
=
1040 caa_container_of(node
, struct lttng_consumer_channel
, node
);
1041 consumer_del_channel(channel
);
1046 lttng_ht_destroy(consumer_data
.channel_ht
);
1048 cleanup_relayd_ht();
1051 * This HT contains streams that are freed by either the metadata thread or
1052 * the data thread so we do *nothing* on the hash table and simply destroy
1055 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1059 * Called from signal handler.
1061 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1066 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1067 } while (ret
< 0 && errno
== EINTR
);
1069 PERROR("write consumer quit");
1072 DBG("Consumer flag that it should quit");
1075 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1078 int outfd
= stream
->out_fd
;
1081 * This does a blocking write-and-wait on any page that belongs to the
1082 * subbuffer prior to the one we just wrote.
1083 * Don't care about error values, as these are just hints and ways to
1084 * limit the amount of page cache used.
1086 if (orig_offset
< stream
->chan
->max_sb_size
) {
1089 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
1090 stream
->chan
->max_sb_size
,
1091 SYNC_FILE_RANGE_WAIT_BEFORE
1092 | SYNC_FILE_RANGE_WRITE
1093 | SYNC_FILE_RANGE_WAIT_AFTER
);
1095 * Give hints to the kernel about how we access the file:
1096 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1099 * We need to call fadvise again after the file grows because the
1100 * kernel does not seem to apply fadvise to non-existing parts of the
1103 * Call fadvise _after_ having waited for the page writeback to
1104 * complete because the dirty page writeback semantic is not well
1105 * defined. So it can be expected to lead to lower throughput in
1108 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
1109 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
1113 * Initialise the necessary environnement :
1114 * - create a new context
1115 * - create the poll_pipe
1116 * - create the should_quit pipe (for signal handler)
1117 * - create the thread pipe (for splice)
1119 * Takes a function pointer as argument, this function is called when data is
1120 * available on a buffer. This function is responsible to do the
1121 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1122 * buffer configuration and then kernctl_put_next_subbuf at the end.
1124 * Returns a pointer to the new context or NULL on error.
1126 struct lttng_consumer_local_data
*lttng_consumer_create(
1127 enum lttng_consumer_type type
,
1128 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1129 struct lttng_consumer_local_data
*ctx
),
1130 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1131 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1132 int (*update_stream
)(int stream_key
, uint32_t state
))
1135 struct lttng_consumer_local_data
*ctx
;
1137 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1138 consumer_data
.type
== type
);
1139 consumer_data
.type
= type
;
1141 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1143 PERROR("allocating context");
1147 ctx
->consumer_error_socket
= -1;
1148 /* assign the callbacks */
1149 ctx
->on_buffer_ready
= buffer_ready
;
1150 ctx
->on_recv_channel
= recv_channel
;
1151 ctx
->on_recv_stream
= recv_stream
;
1152 ctx
->on_update_stream
= update_stream
;
1154 ret
= pipe(ctx
->consumer_data_pipe
);
1156 PERROR("Error creating poll pipe");
1157 goto error_poll_pipe
;
1160 /* set read end of the pipe to non-blocking */
1161 ret
= fcntl(ctx
->consumer_data_pipe
[0], F_SETFL
, O_NONBLOCK
);
1163 PERROR("fcntl O_NONBLOCK");
1164 goto error_poll_fcntl
;
1167 /* set write end of the pipe to non-blocking */
1168 ret
= fcntl(ctx
->consumer_data_pipe
[1], F_SETFL
, O_NONBLOCK
);
1170 PERROR("fcntl O_NONBLOCK");
1171 goto error_poll_fcntl
;
1174 ret
= pipe(ctx
->consumer_should_quit
);
1176 PERROR("Error creating recv pipe");
1177 goto error_quit_pipe
;
1180 ret
= pipe(ctx
->consumer_thread_pipe
);
1182 PERROR("Error creating thread pipe");
1183 goto error_thread_pipe
;
1186 ret
= utils_create_pipe(ctx
->consumer_metadata_pipe
);
1188 goto error_metadata_pipe
;
1191 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1193 goto error_splice_pipe
;
1199 utils_close_pipe(ctx
->consumer_metadata_pipe
);
1200 error_metadata_pipe
:
1201 utils_close_pipe(ctx
->consumer_thread_pipe
);
1203 for (i
= 0; i
< 2; i
++) {
1206 err
= close(ctx
->consumer_should_quit
[i
]);
1213 for (i
= 0; i
< 2; i
++) {
1216 err
= close(ctx
->consumer_data_pipe
[i
]);
1228 * Close all fds associated with the instance and free the context.
1230 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1234 DBG("Consumer destroying it. Closing everything.");
1236 ret
= close(ctx
->consumer_error_socket
);
1240 ret
= close(ctx
->consumer_thread_pipe
[0]);
1244 ret
= close(ctx
->consumer_thread_pipe
[1]);
1248 ret
= close(ctx
->consumer_data_pipe
[0]);
1252 ret
= close(ctx
->consumer_data_pipe
[1]);
1256 ret
= close(ctx
->consumer_should_quit
[0]);
1260 ret
= close(ctx
->consumer_should_quit
[1]);
1264 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1266 unlink(ctx
->consumer_command_sock_path
);
1271 * Write the metadata stream id on the specified file descriptor.
1273 static int write_relayd_metadata_id(int fd
,
1274 struct lttng_consumer_stream
*stream
,
1275 struct consumer_relayd_sock_pair
*relayd
,
1276 unsigned long padding
)
1279 struct lttcomm_relayd_metadata_payload hdr
;
1281 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1282 hdr
.padding_size
= htobe32(padding
);
1284 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1285 } while (ret
< 0 && errno
== EINTR
);
1287 PERROR("write metadata stream id");
1290 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1291 stream
->relayd_stream_id
, padding
);
1298 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1299 * core function for writing trace buffers to either the local filesystem or
1302 * It must be called with the stream lock held.
1304 * Careful review MUST be put if any changes occur!
1306 * Returns the number of bytes written
1308 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1309 struct lttng_consumer_local_data
*ctx
,
1310 struct lttng_consumer_stream
*stream
, unsigned long len
,
1311 unsigned long padding
)
1313 unsigned long mmap_offset
;
1314 ssize_t ret
= 0, written
= 0;
1315 off_t orig_offset
= stream
->out_fd_offset
;
1316 /* Default is on the disk */
1317 int outfd
= stream
->out_fd
;
1318 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1319 unsigned int relayd_hang_up
= 0;
1321 /* RCU lock for the relayd pointer */
1324 /* Flag that the current stream if set for network streaming. */
1325 if (stream
->net_seq_idx
!= -1) {
1326 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1327 if (relayd
== NULL
) {
1332 /* get the offset inside the fd to mmap */
1333 switch (consumer_data
.type
) {
1334 case LTTNG_CONSUMER_KERNEL
:
1335 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1337 case LTTNG_CONSUMER32_UST
:
1338 case LTTNG_CONSUMER64_UST
:
1339 ret
= lttng_ustctl_get_mmap_read_offset(stream
->chan
->handle
,
1340 stream
->buf
, &mmap_offset
);
1343 ERR("Unknown consumer_data type");
1348 PERROR("tracer ctl get_mmap_read_offset");
1353 /* Handle stream on the relayd if the output is on the network */
1355 unsigned long netlen
= len
;
1358 * Lock the control socket for the complete duration of the function
1359 * since from this point on we will use the socket.
1361 if (stream
->metadata_flag
) {
1362 /* Metadata requires the control socket. */
1363 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1364 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1367 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1369 /* Use the returned socket. */
1372 /* Write metadata stream id before payload */
1373 if (stream
->metadata_flag
) {
1374 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1377 /* Socket operation failed. We consider the relayd dead */
1378 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1386 /* Socket operation failed. We consider the relayd dead */
1387 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1391 /* Else, use the default set before which is the filesystem. */
1394 /* No streaming, we have to set the len with the full padding */
1400 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
1401 } while (ret
< 0 && errno
== EINTR
);
1402 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1404 PERROR("Error in file write");
1408 /* Socket operation failed. We consider the relayd dead */
1409 if (errno
== EPIPE
|| errno
== EINVAL
) {
1414 } else if (ret
> len
) {
1415 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1423 /* This call is useless on a socket so better save a syscall. */
1425 /* This won't block, but will start writeout asynchronously */
1426 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1427 SYNC_FILE_RANGE_WRITE
);
1428 stream
->out_fd_offset
+= ret
;
1432 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1436 * This is a special case that the relayd has closed its socket. Let's
1437 * cleanup the relayd object and all associated streams.
1439 if (relayd
&& relayd_hang_up
) {
1440 cleanup_relayd(relayd
, ctx
);
1444 /* Unlock only if ctrl socket used */
1445 if (relayd
&& stream
->metadata_flag
) {
1446 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1454 * Splice the data from the ring buffer to the tracefile.
1456 * It must be called with the stream lock held.
1458 * Returns the number of bytes spliced.
1460 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1461 struct lttng_consumer_local_data
*ctx
,
1462 struct lttng_consumer_stream
*stream
, unsigned long len
,
1463 unsigned long padding
)
1465 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1467 off_t orig_offset
= stream
->out_fd_offset
;
1468 int fd
= stream
->wait_fd
;
1469 /* Default is on the disk */
1470 int outfd
= stream
->out_fd
;
1471 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1473 unsigned int relayd_hang_up
= 0;
1475 switch (consumer_data
.type
) {
1476 case LTTNG_CONSUMER_KERNEL
:
1478 case LTTNG_CONSUMER32_UST
:
1479 case LTTNG_CONSUMER64_UST
:
1480 /* Not supported for user space tracing */
1483 ERR("Unknown consumer_data type");
1487 /* RCU lock for the relayd pointer */
1490 /* Flag that the current stream if set for network streaming. */
1491 if (stream
->net_seq_idx
!= -1) {
1492 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1493 if (relayd
== NULL
) {
1499 * Choose right pipe for splice. Metadata and trace data are handled by
1500 * different threads hence the use of two pipes in order not to race or
1501 * corrupt the written data.
1503 if (stream
->metadata_flag
) {
1504 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1506 splice_pipe
= ctx
->consumer_thread_pipe
;
1509 /* Write metadata stream id before payload */
1511 int total_len
= len
;
1513 if (stream
->metadata_flag
) {
1515 * Lock the control socket for the complete duration of the function
1516 * since from this point on we will use the socket.
1518 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1520 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1524 /* Socket operation failed. We consider the relayd dead */
1525 if (ret
== -EBADF
) {
1526 WARN("Remote relayd disconnected. Stopping");
1533 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1536 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1538 /* Use the returned socket. */
1541 /* Socket operation failed. We consider the relayd dead */
1542 if (ret
== -EBADF
) {
1543 WARN("Remote relayd disconnected. Stopping");
1550 /* No streaming, we have to set the len with the full padding */
1555 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1556 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1557 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1558 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1559 DBG("splice chan to pipe, ret %zd", ret_splice
);
1560 if (ret_splice
< 0) {
1561 PERROR("Error in relay splice");
1563 written
= ret_splice
;
1569 /* Handle stream on the relayd if the output is on the network */
1571 if (stream
->metadata_flag
) {
1572 size_t metadata_payload_size
=
1573 sizeof(struct lttcomm_relayd_metadata_payload
);
1575 /* Update counter to fit the spliced data */
1576 ret_splice
+= metadata_payload_size
;
1577 len
+= metadata_payload_size
;
1579 * We do this so the return value can match the len passed as
1580 * argument to this function.
1582 written
-= metadata_payload_size
;
1586 /* Splice data out */
1587 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1588 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1589 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1590 if (ret_splice
< 0) {
1591 PERROR("Error in file splice");
1593 written
= ret_splice
;
1595 /* Socket operation failed. We consider the relayd dead */
1596 if (errno
== EBADF
|| errno
== EPIPE
) {
1597 WARN("Remote relayd disconnected. Stopping");
1603 } else if (ret_splice
> len
) {
1605 PERROR("Wrote more data than requested %zd (len: %lu)",
1607 written
+= ret_splice
;
1613 /* This call is useless on a socket so better save a syscall. */
1615 /* This won't block, but will start writeout asynchronously */
1616 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1617 SYNC_FILE_RANGE_WRITE
);
1618 stream
->out_fd_offset
+= ret_splice
;
1620 written
+= ret_splice
;
1622 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1630 * This is a special case that the relayd has closed its socket. Let's
1631 * cleanup the relayd object and all associated streams.
1633 if (relayd
&& relayd_hang_up
) {
1634 cleanup_relayd(relayd
, ctx
);
1635 /* Skip splice error so the consumer does not fail */
1640 /* send the appropriate error description to sessiond */
1643 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1646 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1649 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1654 if (relayd
&& stream
->metadata_flag
) {
1655 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1663 * Take a snapshot for a specific fd
1665 * Returns 0 on success, < 0 on error
1667 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1668 struct lttng_consumer_stream
*stream
)
1670 switch (consumer_data
.type
) {
1671 case LTTNG_CONSUMER_KERNEL
:
1672 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1673 case LTTNG_CONSUMER32_UST
:
1674 case LTTNG_CONSUMER64_UST
:
1675 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1677 ERR("Unknown consumer_data type");
1685 * Get the produced position
1687 * Returns 0 on success, < 0 on error
1689 int lttng_consumer_get_produced_snapshot(
1690 struct lttng_consumer_local_data
*ctx
,
1691 struct lttng_consumer_stream
*stream
,
1694 switch (consumer_data
.type
) {
1695 case LTTNG_CONSUMER_KERNEL
:
1696 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1697 case LTTNG_CONSUMER32_UST
:
1698 case LTTNG_CONSUMER64_UST
:
1699 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1701 ERR("Unknown consumer_data type");
1707 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1708 int sock
, struct pollfd
*consumer_sockpoll
)
1710 switch (consumer_data
.type
) {
1711 case LTTNG_CONSUMER_KERNEL
:
1712 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1713 case LTTNG_CONSUMER32_UST
:
1714 case LTTNG_CONSUMER64_UST
:
1715 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1717 ERR("Unknown consumer_data type");
1724 * Iterate over all streams of the hashtable and free them properly.
1726 * WARNING: *MUST* be used with data stream only.
1728 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1730 struct lttng_ht_iter iter
;
1731 struct lttng_consumer_stream
*stream
;
1738 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1740 * Ignore return value since we are currently cleaning up so any error
1743 (void) consumer_del_stream(stream
, ht
);
1747 lttng_ht_destroy(ht
);
1751 * Iterate over all streams of the hashtable and free them properly.
1753 * XXX: Should not be only for metadata stream or else use an other name.
1755 static void destroy_stream_ht(struct lttng_ht
*ht
)
1757 struct lttng_ht_iter iter
;
1758 struct lttng_consumer_stream
*stream
;
1765 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1767 * Ignore return value since we are currently cleaning up so any error
1770 (void) consumer_del_metadata_stream(stream
, ht
);
1774 lttng_ht_destroy(ht
);
1778 * Clean up a metadata stream and free its memory.
1780 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1781 struct lttng_ht
*ht
)
1784 struct lttng_ht_iter iter
;
1785 struct lttng_consumer_channel
*free_chan
= NULL
;
1786 struct consumer_relayd_sock_pair
*relayd
;
1790 * This call should NEVER receive regular stream. It must always be
1791 * metadata stream and this is crucial for data structure synchronization.
1793 assert(stream
->metadata_flag
);
1795 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1798 /* Means the stream was allocated but not successfully added */
1802 pthread_mutex_lock(&consumer_data
.lock
);
1803 pthread_mutex_lock(&stream
->lock
);
1805 switch (consumer_data
.type
) {
1806 case LTTNG_CONSUMER_KERNEL
:
1807 if (stream
->mmap_base
!= NULL
) {
1808 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1810 PERROR("munmap metadata stream");
1814 case LTTNG_CONSUMER32_UST
:
1815 case LTTNG_CONSUMER64_UST
:
1816 lttng_ustconsumer_del_stream(stream
);
1819 ERR("Unknown consumer_data type");
1825 iter
.iter
.node
= &stream
->node
.node
;
1826 ret
= lttng_ht_del(ht
, &iter
);
1829 /* Remove node session id from the consumer_data stream ht */
1830 iter
.iter
.node
= &stream
->node_session_id
.node
;
1831 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1835 if (stream
->out_fd
>= 0) {
1836 ret
= close(stream
->out_fd
);
1842 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
1843 ret
= close(stream
->wait_fd
);
1849 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
1850 ret
= close(stream
->shm_fd
);
1856 /* Check and cleanup relayd */
1858 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1859 if (relayd
!= NULL
) {
1860 uatomic_dec(&relayd
->refcount
);
1861 assert(uatomic_read(&relayd
->refcount
) >= 0);
1863 /* Closing streams requires to lock the control socket. */
1864 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1865 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1866 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1867 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1869 DBG("Unable to close stream on the relayd. Continuing");
1871 * Continue here. There is nothing we can do for the relayd.
1872 * Chances are that the relayd has closed the socket so we just
1873 * continue cleaning up.
1877 /* Both conditions are met, we destroy the relayd. */
1878 if (uatomic_read(&relayd
->refcount
) == 0 &&
1879 uatomic_read(&relayd
->destroy_flag
)) {
1880 destroy_relayd(relayd
);
1885 /* Atomically decrement channel refcount since other threads can use it. */
1886 uatomic_dec(&stream
->chan
->refcount
);
1887 if (!uatomic_read(&stream
->chan
->refcount
)
1888 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
1889 /* Go for channel deletion! */
1890 free_chan
= stream
->chan
;
1894 pthread_mutex_unlock(&stream
->lock
);
1895 pthread_mutex_unlock(&consumer_data
.lock
);
1898 consumer_del_channel(free_chan
);
1902 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1906 * Action done with the metadata stream when adding it to the consumer internal
1907 * data structures to handle it.
1909 static int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
,
1910 struct lttng_ht
*ht
)
1913 struct consumer_relayd_sock_pair
*relayd
;
1914 struct lttng_ht_iter iter
;
1915 struct lttng_ht_node_ulong
*node
;
1920 DBG3("Adding metadata stream %d to hash table", stream
->wait_fd
);
1922 pthread_mutex_lock(&consumer_data
.lock
);
1923 pthread_mutex_lock(&stream
->lock
);
1926 * From here, refcounts are updated so be _careful_ when returning an error
1933 * Lookup the stream just to make sure it does not exist in our internal
1934 * state. This should NEVER happen.
1936 lttng_ht_lookup(ht
, (void *)((unsigned long) stream
->wait_fd
), &iter
);
1937 node
= lttng_ht_iter_get_node_ulong(&iter
);
1940 /* Find relayd and, if one is found, increment refcount. */
1941 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1942 if (relayd
!= NULL
) {
1943 uatomic_inc(&relayd
->refcount
);
1946 /* Update channel refcount once added without error(s). */
1947 uatomic_inc(&stream
->chan
->refcount
);
1950 * When nb_init_streams reaches 0, we don't need to trigger any action in
1951 * terms of destroying the associated channel, because the action that
1952 * causes the count to become 0 also causes a stream to be added. The
1953 * channel deletion will thus be triggered by the following removal of this
1956 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
1957 uatomic_dec(&stream
->chan
->nb_init_streams
);
1960 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
1963 * Add stream to the stream_list_ht of the consumer data. No need to steal
1964 * the key since the HT does not use it and we allow to add redundant keys
1967 lttng_ht_add_ulong(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
1971 pthread_mutex_unlock(&stream
->lock
);
1972 pthread_mutex_unlock(&consumer_data
.lock
);
1977 * Delete data stream that are flagged for deletion (endpoint_status).
1979 static void validate_endpoint_status_data_stream(void)
1981 struct lttng_ht_iter iter
;
1982 struct lttng_consumer_stream
*stream
;
1984 DBG("Consumer delete flagged data stream");
1987 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1988 /* Validate delete flag of the stream */
1989 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
1992 /* Delete it right now */
1993 consumer_del_stream(stream
, data_ht
);
1999 * Delete metadata stream that are flagged for deletion (endpoint_status).
2001 static void validate_endpoint_status_metadata_stream(
2002 struct lttng_poll_event
*pollset
)
2004 struct lttng_ht_iter iter
;
2005 struct lttng_consumer_stream
*stream
;
2007 DBG("Consumer delete flagged metadata stream");
2012 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2013 /* Validate delete flag of the stream */
2014 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2018 * Remove from pollset so the metadata thread can continue without
2019 * blocking on a deleted stream.
2021 lttng_poll_del(pollset
, stream
->wait_fd
);
2023 /* Delete it right now */
2024 consumer_del_metadata_stream(stream
, metadata_ht
);
2030 * Thread polls on metadata file descriptor and write them on disk or on the
2033 void *consumer_thread_metadata_poll(void *data
)
2036 uint32_t revents
, nb_fd
;
2037 struct lttng_consumer_stream
*stream
= NULL
;
2038 struct lttng_ht_iter iter
;
2039 struct lttng_ht_node_ulong
*node
;
2040 struct lttng_poll_event events
;
2041 struct lttng_consumer_local_data
*ctx
= data
;
2044 rcu_register_thread();
2046 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2048 /* ENOMEM at this point. Better to bail out. */
2052 DBG("Thread metadata poll started");
2054 /* Size is set to 1 for the consumer_metadata pipe */
2055 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2057 ERR("Poll set creation failed");
2061 ret
= lttng_poll_add(&events
, ctx
->consumer_metadata_pipe
[0], LPOLLIN
);
2067 DBG("Metadata main loop started");
2070 lttng_poll_reset(&events
);
2072 nb_fd
= LTTNG_POLL_GETNB(&events
);
2074 /* Only the metadata pipe is set */
2075 if (nb_fd
== 0 && consumer_quit
== 1) {
2080 DBG("Metadata poll wait with %d fd(s)", nb_fd
);
2081 ret
= lttng_poll_wait(&events
, -1);
2082 DBG("Metadata event catched in thread");
2084 if (errno
== EINTR
) {
2085 ERR("Poll EINTR catched");
2091 /* From here, the event is a metadata wait fd */
2092 for (i
= 0; i
< nb_fd
; i
++) {
2093 revents
= LTTNG_POLL_GETEV(&events
, i
);
2094 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2096 /* Just don't waste time if no returned events for the fd */
2101 if (pollfd
== ctx
->consumer_metadata_pipe
[0]) {
2102 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2103 DBG("Metadata thread pipe hung up");
2105 * Remove the pipe from the poll set and continue the loop
2106 * since their might be data to consume.
2108 lttng_poll_del(&events
, ctx
->consumer_metadata_pipe
[0]);
2109 ret
= close(ctx
->consumer_metadata_pipe
[0]);
2111 PERROR("close metadata pipe");
2114 } else if (revents
& LPOLLIN
) {
2116 /* Get the stream pointer received */
2117 ret
= read(pollfd
, &stream
, sizeof(stream
));
2118 } while (ret
< 0 && errno
== EINTR
);
2120 ret
< sizeof(struct lttng_consumer_stream
*)) {
2121 PERROR("read metadata stream");
2123 * Let's continue here and hope we can still work
2124 * without stopping the consumer. XXX: Should we?
2129 /* A NULL stream means that the state has changed. */
2130 if (stream
== NULL
) {
2131 /* Check for deleted streams. */
2132 validate_endpoint_status_metadata_stream(&events
);
2136 DBG("Adding metadata stream %d to poll set",
2139 ret
= consumer_add_metadata_stream(stream
, metadata_ht
);
2141 ERR("Unable to add metadata stream");
2142 /* Stream was not setup properly. Continuing. */
2143 consumer_del_metadata_stream(stream
, NULL
);
2147 /* Add metadata stream to the global poll events list */
2148 lttng_poll_add(&events
, stream
->wait_fd
,
2149 LPOLLIN
| LPOLLPRI
);
2152 /* Handle other stream */
2157 lttng_ht_lookup(metadata_ht
, (void *)((unsigned long) pollfd
),
2159 node
= lttng_ht_iter_get_node_ulong(&iter
);
2162 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2165 /* Check for error event */
2166 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2167 DBG("Metadata fd %d is hup|err.", pollfd
);
2168 if (!stream
->hangup_flush_done
2169 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2170 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2171 DBG("Attempting to flush and consume the UST buffers");
2172 lttng_ustconsumer_on_stream_hangup(stream
);
2174 /* We just flushed the stream now read it. */
2176 len
= ctx
->on_buffer_ready(stream
, ctx
);
2178 * We don't check the return value here since if we get
2179 * a negative len, it means an error occured thus we
2180 * simply remove it from the poll set and free the
2186 lttng_poll_del(&events
, stream
->wait_fd
);
2188 * This call update the channel states, closes file descriptors
2189 * and securely free the stream.
2191 consumer_del_metadata_stream(stream
, metadata_ht
);
2192 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2193 /* Get the data out of the metadata file descriptor */
2194 DBG("Metadata available on fd %d", pollfd
);
2195 assert(stream
->wait_fd
== pollfd
);
2197 len
= ctx
->on_buffer_ready(stream
, ctx
);
2198 /* It's ok to have an unavailable sub-buffer */
2199 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2200 /* Clean up stream from consumer and free it. */
2201 lttng_poll_del(&events
, stream
->wait_fd
);
2202 consumer_del_metadata_stream(stream
, metadata_ht
);
2203 } else if (len
> 0) {
2204 stream
->data_read
= 1;
2208 /* Release RCU lock for the stream looked up */
2215 DBG("Metadata poll thread exiting");
2216 lttng_poll_clean(&events
);
2218 destroy_stream_ht(metadata_ht
);
2220 rcu_unregister_thread();
2225 * This thread polls the fds in the set to consume the data and write
2226 * it to tracefile if necessary.
2228 void *consumer_thread_data_poll(void *data
)
2230 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2231 struct pollfd
*pollfd
= NULL
;
2232 /* local view of the streams */
2233 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2234 /* local view of consumer_data.fds_count */
2236 struct lttng_consumer_local_data
*ctx
= data
;
2239 rcu_register_thread();
2241 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2242 if (data_ht
== NULL
) {
2243 /* ENOMEM at this point. Better to bail out. */
2247 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
2254 * the fds set has been updated, we need to update our
2255 * local array as well
2257 pthread_mutex_lock(&consumer_data
.lock
);
2258 if (consumer_data
.need_update
) {
2259 if (pollfd
!= NULL
) {
2263 if (local_stream
!= NULL
) {
2265 local_stream
= NULL
;
2268 /* allocate for all fds + 1 for the consumer_data_pipe */
2269 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2270 if (pollfd
== NULL
) {
2271 PERROR("pollfd malloc");
2272 pthread_mutex_unlock(&consumer_data
.lock
);
2276 /* allocate for all fds + 1 for the consumer_data_pipe */
2277 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2278 sizeof(struct lttng_consumer_stream
));
2279 if (local_stream
== NULL
) {
2280 PERROR("local_stream malloc");
2281 pthread_mutex_unlock(&consumer_data
.lock
);
2284 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
,
2287 ERR("Error in allocating pollfd or local_outfds");
2288 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2289 pthread_mutex_unlock(&consumer_data
.lock
);
2293 consumer_data
.need_update
= 0;
2295 pthread_mutex_unlock(&consumer_data
.lock
);
2297 /* No FDs and consumer_quit, consumer_cleanup the thread */
2298 if (nb_fd
== 0 && consumer_quit
== 1) {
2301 /* poll on the array of fds */
2303 DBG("polling on %d fd", nb_fd
+ 1);
2304 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2305 DBG("poll num_rdy : %d", num_rdy
);
2306 if (num_rdy
== -1) {
2308 * Restart interrupted system call.
2310 if (errno
== EINTR
) {
2313 PERROR("Poll error");
2314 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2316 } else if (num_rdy
== 0) {
2317 DBG("Polling thread timed out");
2322 * If the consumer_data_pipe triggered poll go directly to the
2323 * beginning of the loop to update the array. We want to prioritize
2324 * array update over low-priority reads.
2326 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2327 size_t pipe_readlen
;
2329 DBG("consumer_data_pipe wake up");
2330 /* Consume 1 byte of pipe data */
2332 pipe_readlen
= read(ctx
->consumer_data_pipe
[0], &new_stream
,
2333 sizeof(new_stream
));
2334 } while (pipe_readlen
== -1 && errno
== EINTR
);
2337 * If the stream is NULL, just ignore it. It's also possible that
2338 * the sessiond poll thread changed the consumer_quit state and is
2339 * waking us up to test it.
2341 if (new_stream
== NULL
) {
2342 validate_endpoint_status_data_stream();
2346 ret
= consumer_add_stream(new_stream
, data_ht
);
2348 ERR("Consumer add stream %d failed. Continuing",
2351 * At this point, if the add_stream fails, it is not in the
2352 * hash table thus passing the NULL value here.
2354 consumer_del_stream(new_stream
, NULL
);
2357 /* Continue to update the local streams and handle prio ones */
2361 /* Take care of high priority channels first. */
2362 for (i
= 0; i
< nb_fd
; i
++) {
2363 if (local_stream
[i
] == NULL
) {
2366 if (pollfd
[i
].revents
& POLLPRI
) {
2367 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2369 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2370 /* it's ok to have an unavailable sub-buffer */
2371 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2372 /* Clean the stream and free it. */
2373 consumer_del_stream(local_stream
[i
], data_ht
);
2374 local_stream
[i
] = NULL
;
2375 } else if (len
> 0) {
2376 local_stream
[i
]->data_read
= 1;
2382 * If we read high prio channel in this loop, try again
2383 * for more high prio data.
2389 /* Take care of low priority channels. */
2390 for (i
= 0; i
< nb_fd
; i
++) {
2391 if (local_stream
[i
] == NULL
) {
2394 if ((pollfd
[i
].revents
& POLLIN
) ||
2395 local_stream
[i
]->hangup_flush_done
) {
2396 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2397 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2398 /* it's ok to have an unavailable sub-buffer */
2399 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2400 /* Clean the stream and free it. */
2401 consumer_del_stream(local_stream
[i
], data_ht
);
2402 local_stream
[i
] = NULL
;
2403 } else if (len
> 0) {
2404 local_stream
[i
]->data_read
= 1;
2409 /* Handle hangup and errors */
2410 for (i
= 0; i
< nb_fd
; i
++) {
2411 if (local_stream
[i
] == NULL
) {
2414 if (!local_stream
[i
]->hangup_flush_done
2415 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2416 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2417 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2418 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2420 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2421 /* Attempt read again, for the data we just flushed. */
2422 local_stream
[i
]->data_read
= 1;
2425 * If the poll flag is HUP/ERR/NVAL and we have
2426 * read no data in this pass, we can remove the
2427 * stream from its hash table.
2429 if ((pollfd
[i
].revents
& POLLHUP
)) {
2430 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2431 if (!local_stream
[i
]->data_read
) {
2432 consumer_del_stream(local_stream
[i
], data_ht
);
2433 local_stream
[i
] = NULL
;
2436 } else if (pollfd
[i
].revents
& POLLERR
) {
2437 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2438 if (!local_stream
[i
]->data_read
) {
2439 consumer_del_stream(local_stream
[i
], data_ht
);
2440 local_stream
[i
] = NULL
;
2443 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2444 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2445 if (!local_stream
[i
]->data_read
) {
2446 consumer_del_stream(local_stream
[i
], data_ht
);
2447 local_stream
[i
] = NULL
;
2451 if (local_stream
[i
] != NULL
) {
2452 local_stream
[i
]->data_read
= 0;
2457 DBG("polling thread exiting");
2458 if (pollfd
!= NULL
) {
2462 if (local_stream
!= NULL
) {
2464 local_stream
= NULL
;
2468 * Close the write side of the pipe so epoll_wait() in
2469 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2470 * read side of the pipe. If we close them both, epoll_wait strangely does
2471 * not return and could create a endless wait period if the pipe is the
2472 * only tracked fd in the poll set. The thread will take care of closing
2475 ret
= close(ctx
->consumer_metadata_pipe
[1]);
2477 PERROR("close data pipe");
2480 destroy_data_stream_ht(data_ht
);
2482 rcu_unregister_thread();
2487 * This thread listens on the consumerd socket and receives the file
2488 * descriptors from the session daemon.
2490 void *consumer_thread_sessiond_poll(void *data
)
2492 int sock
= -1, client_socket
, ret
;
2494 * structure to poll for incoming data on communication socket avoids
2495 * making blocking sockets.
2497 struct pollfd consumer_sockpoll
[2];
2498 struct lttng_consumer_local_data
*ctx
= data
;
2500 rcu_register_thread();
2502 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2503 unlink(ctx
->consumer_command_sock_path
);
2504 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2505 if (client_socket
< 0) {
2506 ERR("Cannot create command socket");
2510 ret
= lttcomm_listen_unix_sock(client_socket
);
2515 DBG("Sending ready command to lttng-sessiond");
2516 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2517 /* return < 0 on error, but == 0 is not fatal */
2519 ERR("Error sending ready command to lttng-sessiond");
2523 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2525 PERROR("fcntl O_NONBLOCK");
2529 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2530 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2531 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2532 consumer_sockpoll
[1].fd
= client_socket
;
2533 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2535 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2538 DBG("Connection on client_socket");
2540 /* Blocking call, waiting for transmission */
2541 sock
= lttcomm_accept_unix_sock(client_socket
);
2546 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2548 PERROR("fcntl O_NONBLOCK");
2552 /* This socket is not useful anymore. */
2553 ret
= close(client_socket
);
2555 PERROR("close client_socket");
2559 /* update the polling structure to poll on the established socket */
2560 consumer_sockpoll
[1].fd
= sock
;
2561 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2564 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2567 DBG("Incoming command on sock");
2568 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2569 if (ret
== -ENOENT
) {
2570 DBG("Received STOP command");
2575 * This could simply be a session daemon quitting. Don't output
2578 DBG("Communication interrupted on command socket");
2581 if (consumer_quit
) {
2582 DBG("consumer_thread_receive_fds received quit from signal");
2585 DBG("received fds on sock");
2588 DBG("consumer_thread_receive_fds exiting");
2591 * when all fds have hung up, the polling thread
2597 * Notify the data poll thread to poll back again and test the
2598 * consumer_quit state that we just set so to quit gracefully.
2600 notify_thread_pipe(ctx
->consumer_data_pipe
[1]);
2602 /* Cleaning up possibly open sockets. */
2606 PERROR("close sock sessiond poll");
2609 if (client_socket
>= 0) {
2612 PERROR("close client_socket sessiond poll");
2616 rcu_unregister_thread();
2620 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2621 struct lttng_consumer_local_data
*ctx
)
2625 pthread_mutex_lock(&stream
->lock
);
2627 switch (consumer_data
.type
) {
2628 case LTTNG_CONSUMER_KERNEL
:
2629 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
2631 case LTTNG_CONSUMER32_UST
:
2632 case LTTNG_CONSUMER64_UST
:
2633 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2636 ERR("Unknown consumer_data type");
2642 pthread_mutex_unlock(&stream
->lock
);
2646 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2648 switch (consumer_data
.type
) {
2649 case LTTNG_CONSUMER_KERNEL
:
2650 return lttng_kconsumer_on_recv_stream(stream
);
2651 case LTTNG_CONSUMER32_UST
:
2652 case LTTNG_CONSUMER64_UST
:
2653 return lttng_ustconsumer_on_recv_stream(stream
);
2655 ERR("Unknown consumer_data type");
2662 * Allocate and set consumer data hash tables.
2664 void lttng_consumer_init(void)
2666 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2667 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2668 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2672 * Process the ADD_RELAYD command receive by a consumer.
2674 * This will create a relayd socket pair and add it to the relayd hash table.
2675 * The caller MUST acquire a RCU read side lock before calling it.
2677 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
2678 struct lttng_consumer_local_data
*ctx
, int sock
,
2679 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
)
2681 int fd
= -1, ret
= -1;
2682 enum lttng_error_code ret_code
= LTTNG_OK
;
2683 struct consumer_relayd_sock_pair
*relayd
;
2685 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
2687 /* First send a status message before receiving the fds. */
2688 ret
= consumer_send_status_msg(sock
, ret_code
);
2690 /* Somehow, the session daemon is not responding anymore. */
2694 /* Get relayd reference if exists. */
2695 relayd
= consumer_find_relayd(net_seq_idx
);
2696 if (relayd
== NULL
) {
2697 /* Not found. Allocate one. */
2698 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
2699 if (relayd
== NULL
) {
2700 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
2705 /* Poll on consumer socket. */
2706 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2711 /* Get relayd socket from session daemon */
2712 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2713 if (ret
!= sizeof(fd
)) {
2714 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
2716 fd
= -1; /* Just in case it gets set with an invalid value. */
2720 /* We have the fds without error. Send status back. */
2721 ret
= consumer_send_status_msg(sock
, ret_code
);
2723 /* Somehow, the session daemon is not responding anymore. */
2727 /* Copy socket information and received FD */
2728 switch (sock_type
) {
2729 case LTTNG_STREAM_CONTROL
:
2730 /* Copy received lttcomm socket */
2731 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
2732 ret
= lttcomm_create_sock(&relayd
->control_sock
);
2733 /* Immediately try to close the created socket if valid. */
2734 if (relayd
->control_sock
.fd
>= 0) {
2735 if (close(relayd
->control_sock
.fd
)) {
2736 PERROR("close relayd control socket");
2739 /* Handle create_sock error. */
2744 /* Assign new file descriptor */
2745 relayd
->control_sock
.fd
= fd
;
2747 case LTTNG_STREAM_DATA
:
2748 /* Copy received lttcomm socket */
2749 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
2750 ret
= lttcomm_create_sock(&relayd
->data_sock
);
2751 /* Immediately try to close the created socket if valid. */
2752 if (relayd
->data_sock
.fd
>= 0) {
2753 if (close(relayd
->data_sock
.fd
)) {
2754 PERROR("close relayd data socket");
2757 /* Handle create_sock error. */
2762 /* Assign new file descriptor */
2763 relayd
->data_sock
.fd
= fd
;
2766 ERR("Unknown relayd socket type (%d)", sock_type
);
2770 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2771 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
2772 relayd
->net_seq_idx
, fd
);
2775 * Add relayd socket pair to consumer data hashtable. If object already
2776 * exists or on error, the function gracefully returns.
2784 /* Close received socket if valid. */
2787 PERROR("close received socket");
2794 * Try to lock the stream mutex.
2796 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
2798 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
2805 * Try to lock the stream mutex. On failure, we know that the stream is
2806 * being used else where hence there is data still being extracted.
2808 ret
= pthread_mutex_trylock(&stream
->lock
);
2810 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
2822 * Check if for a given session id there is still data needed to be extract
2825 * Return 1 if data is pending or else 0 meaning ready to be read.
2827 int consumer_data_pending(uint64_t id
)
2830 struct lttng_ht_iter iter
;
2831 struct lttng_ht
*ht
;
2832 struct lttng_consumer_stream
*stream
;
2833 struct consumer_relayd_sock_pair
*relayd
;
2834 int (*data_pending
)(struct lttng_consumer_stream
*);
2836 DBG("Consumer data pending command on session id %" PRIu64
, id
);
2839 pthread_mutex_lock(&consumer_data
.lock
);
2841 switch (consumer_data
.type
) {
2842 case LTTNG_CONSUMER_KERNEL
:
2843 data_pending
= lttng_kconsumer_data_pending
;
2845 case LTTNG_CONSUMER32_UST
:
2846 case LTTNG_CONSUMER64_UST
:
2847 data_pending
= lttng_ustconsumer_data_pending
;
2850 ERR("Unknown consumer data type");
2854 /* Ease our life a bit */
2855 ht
= consumer_data
.stream_list_ht
;
2857 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2858 ht
->hash_fct((void *)((unsigned long) id
), lttng_ht_seed
),
2859 ht
->match_fct
, (void *)((unsigned long) id
),
2860 &iter
.iter
, stream
, node_session_id
.node
) {
2861 /* If this call fails, the stream is being used hence data pending. */
2862 ret
= stream_try_lock(stream
);
2864 goto data_not_pending
;
2868 * A removed node from the hash table indicates that the stream has
2869 * been deleted thus having a guarantee that the buffers are closed
2870 * on the consumer side. However, data can still be transmitted
2871 * over the network so don't skip the relayd check.
2873 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
2875 /* Check the stream if there is data in the buffers. */
2876 ret
= data_pending(stream
);
2878 pthread_mutex_unlock(&stream
->lock
);
2879 goto data_not_pending
;
2884 if (stream
->net_seq_idx
!= -1) {
2885 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
2888 * At this point, if the relayd object is not available for the
2889 * given stream, it is because the relayd is being cleaned up
2890 * so every stream associated with it (for a session id value)
2891 * are or will be marked for deletion hence no data pending.
2893 pthread_mutex_unlock(&stream
->lock
);
2894 goto data_not_pending
;
2897 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
2898 if (stream
->metadata_flag
) {
2899 ret
= relayd_quiescent_control(&relayd
->control_sock
);
2901 ret
= relayd_data_pending(&relayd
->control_sock
,
2902 stream
->relayd_stream_id
, stream
->next_net_seq_num
);
2904 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2906 pthread_mutex_unlock(&stream
->lock
);
2907 goto data_not_pending
;
2910 pthread_mutex_unlock(&stream
->lock
);
2914 * Finding _no_ node in the hash table means that the stream(s) have been
2915 * removed thus data is guaranteed to be available for analysis from the
2916 * trace files. This is *only* true for local consumer and not network
2920 /* Data is available to be read by a viewer. */
2921 pthread_mutex_unlock(&consumer_data
.lock
);
2926 /* Data is still being extracted from buffers. */
2927 pthread_mutex_unlock(&consumer_data
.lock
);
2933 * Send a ret code status message to the sessiond daemon.
2935 * Return the sendmsg() return value.
2937 int consumer_send_status_msg(int sock
, int ret_code
)
2939 struct lttcomm_consumer_status_msg msg
;
2941 msg
.ret_code
= ret_code
;
2943 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));