2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2012 - David Goulet <dgoulet@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/socket.h>
28 #include <sys/types.h>
32 #include <common/common.h>
33 #include <common/utils.h>
34 #include <common/compat/poll.h>
35 #include <common/kernel-ctl/kernel-ctl.h>
36 #include <common/sessiond-comm/relayd.h>
37 #include <common/sessiond-comm/sessiond-comm.h>
38 #include <common/kernel-consumer/kernel-consumer.h>
39 #include <common/relayd/relayd.h>
40 #include <common/ust-consumer/ust-consumer.h>
44 struct lttng_consumer_global_data consumer_data
= {
47 .type
= LTTNG_CONSUMER_UNKNOWN
,
51 * Flag to inform the polling thread to quit when all fd hung up. Updated by
52 * the consumer_thread_receive_fds when it notices that all fds has hung up.
53 * Also updated by the signal handler (consumer_should_exit()). Read by the
56 volatile int consumer_quit
;
59 * Global hash table containing respectively metadata and data streams. The
60 * stream element in this ht should only be updated by the metadata poll thread
61 * for the metadata and the data poll thread for the data.
63 static struct lttng_ht
*metadata_ht
;
64 static struct lttng_ht
*data_ht
;
67 * Notify a thread pipe to poll back again. This usually means that some global
68 * state has changed so we just send back the thread in a poll wait call.
70 static void notify_thread_pipe(int wpipe
)
75 struct lttng_consumer_stream
*null_stream
= NULL
;
77 ret
= write(wpipe
, &null_stream
, sizeof(null_stream
));
78 } while (ret
< 0 && errno
== EINTR
);
82 * Find a stream. The consumer_data.lock must be locked during this
85 static struct lttng_consumer_stream
*consumer_find_stream(int key
,
88 struct lttng_ht_iter iter
;
89 struct lttng_ht_node_ulong
*node
;
90 struct lttng_consumer_stream
*stream
= NULL
;
94 /* Negative keys are lookup failures */
101 lttng_ht_lookup(ht
, (void *)((unsigned long) key
), &iter
);
102 node
= lttng_ht_iter_get_node_ulong(&iter
);
104 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
112 void consumer_steal_stream_key(int key
, struct lttng_ht
*ht
)
114 struct lttng_consumer_stream
*stream
;
117 stream
= consumer_find_stream(key
, ht
);
121 * We don't want the lookup to match, but we still need
122 * to iterate on this stream when iterating over the hash table. Just
123 * change the node key.
125 stream
->node
.key
= -1;
130 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
132 struct lttng_ht_iter iter
;
133 struct lttng_ht_node_ulong
*node
;
134 struct lttng_consumer_channel
*channel
= NULL
;
136 /* Negative keys are lookup failures */
143 lttng_ht_lookup(consumer_data
.channel_ht
, (void *)((unsigned long) key
),
145 node
= lttng_ht_iter_get_node_ulong(&iter
);
147 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
155 static void consumer_steal_channel_key(int key
)
157 struct lttng_consumer_channel
*channel
;
160 channel
= consumer_find_channel(key
);
164 * We don't want the lookup to match, but we still need
165 * to iterate on this channel when iterating over the hash table. Just
166 * change the node key.
168 channel
->node
.key
= -1;
174 void consumer_free_stream(struct rcu_head
*head
)
176 struct lttng_ht_node_ulong
*node
=
177 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
178 struct lttng_consumer_stream
*stream
=
179 caa_container_of(node
, struct lttng_consumer_stream
, node
);
185 * RCU protected relayd socket pair free.
187 static void consumer_rcu_free_relayd(struct rcu_head
*head
)
189 struct lttng_ht_node_ulong
*node
=
190 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
191 struct consumer_relayd_sock_pair
*relayd
=
192 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
195 * Close all sockets. This is done in the call RCU since we don't want the
196 * socket fds to be reassigned thus potentially creating bad state of the
199 * We do not have to lock the control socket mutex here since at this stage
200 * there is no one referencing to this relayd object.
202 (void) relayd_close(&relayd
->control_sock
);
203 (void) relayd_close(&relayd
->data_sock
);
209 * Destroy and free relayd socket pair object.
211 * This function MUST be called with the consumer_data lock acquired.
213 static void destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
216 struct lttng_ht_iter iter
;
218 if (relayd
== NULL
) {
222 DBG("Consumer destroy and close relayd socket pair");
224 iter
.iter
.node
= &relayd
->node
.node
;
225 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
227 /* We assume the relayd is being or is destroyed */
231 /* RCU free() call */
232 call_rcu(&relayd
->node
.head
, consumer_rcu_free_relayd
);
236 * Update the end point status of all streams having the given network sequence
237 * index (relayd index).
239 * It's atomically set without having the stream mutex locked which is fine
240 * because we handle the write/read race with a pipe wakeup for each thread.
242 static void update_endpoint_status_by_netidx(int net_seq_idx
,
243 enum consumer_endpoint_status status
)
245 struct lttng_ht_iter iter
;
246 struct lttng_consumer_stream
*stream
;
248 DBG("Consumer set delete flag on stream by idx %d", net_seq_idx
);
252 /* Let's begin with metadata */
253 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
254 if (stream
->net_seq_idx
== net_seq_idx
) {
255 uatomic_set(&stream
->endpoint_status
, status
);
256 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
260 /* Follow up by the data streams */
261 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
262 if (stream
->net_seq_idx
== net_seq_idx
) {
263 uatomic_set(&stream
->endpoint_status
, status
);
264 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
271 * Cleanup a relayd object by flagging every associated streams for deletion,
272 * destroying the object meaning removing it from the relayd hash table,
273 * closing the sockets and freeing the memory in a RCU call.
275 * If a local data context is available, notify the threads that the streams'
276 * state have changed.
278 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
279 struct lttng_consumer_local_data
*ctx
)
285 DBG("Cleaning up relayd sockets");
287 /* Save the net sequence index before destroying the object */
288 netidx
= relayd
->net_seq_idx
;
291 * Delete the relayd from the relayd hash table, close the sockets and free
292 * the object in a RCU call.
294 destroy_relayd(relayd
);
296 /* Set inactive endpoint to all streams */
297 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
300 * With a local data context, notify the threads that the streams' state
301 * have changed. The write() action on the pipe acts as an "implicit"
302 * memory barrier ordering the updates of the end point status from the
303 * read of this status which happens AFTER receiving this notify.
306 notify_thread_pipe(ctx
->consumer_data_pipe
[1]);
307 notify_thread_pipe(ctx
->consumer_metadata_pipe
[1]);
312 * Flag a relayd socket pair for destruction. Destroy it if the refcount
315 * RCU read side lock MUST be aquired before calling this function.
317 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
321 /* Set destroy flag for this object */
322 uatomic_set(&relayd
->destroy_flag
, 1);
324 /* Destroy the relayd if refcount is 0 */
325 if (uatomic_read(&relayd
->refcount
) == 0) {
326 destroy_relayd(relayd
);
331 * Remove a stream from the global list protected by a mutex. This
332 * function is also responsible for freeing its data structures.
334 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
338 struct lttng_ht_iter iter
;
339 struct lttng_consumer_channel
*free_chan
= NULL
;
340 struct consumer_relayd_sock_pair
*relayd
;
344 DBG("Consumer del stream %d", stream
->wait_fd
);
347 /* Means the stream was allocated but not successfully added */
351 pthread_mutex_lock(&stream
->lock
);
352 pthread_mutex_lock(&consumer_data
.lock
);
354 switch (consumer_data
.type
) {
355 case LTTNG_CONSUMER_KERNEL
:
356 if (stream
->mmap_base
!= NULL
) {
357 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
363 case LTTNG_CONSUMER32_UST
:
364 case LTTNG_CONSUMER64_UST
:
365 lttng_ustconsumer_del_stream(stream
);
368 ERR("Unknown consumer_data type");
374 iter
.iter
.node
= &stream
->node
.node
;
375 ret
= lttng_ht_del(ht
, &iter
);
378 /* Remove node session id from the consumer_data stream ht */
379 iter
.iter
.node
= &stream
->node_session_id
.node
;
380 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
384 assert(consumer_data
.stream_count
> 0);
385 consumer_data
.stream_count
--;
387 if (stream
->out_fd
>= 0) {
388 ret
= close(stream
->out_fd
);
393 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
394 ret
= close(stream
->wait_fd
);
399 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
400 ret
= close(stream
->shm_fd
);
406 /* Check and cleanup relayd */
408 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
409 if (relayd
!= NULL
) {
410 uatomic_dec(&relayd
->refcount
);
411 assert(uatomic_read(&relayd
->refcount
) >= 0);
413 /* Closing streams requires to lock the control socket. */
414 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
415 ret
= relayd_send_close_stream(&relayd
->control_sock
,
416 stream
->relayd_stream_id
,
417 stream
->next_net_seq_num
- 1);
418 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
420 DBG("Unable to close stream on the relayd. Continuing");
422 * Continue here. There is nothing we can do for the relayd.
423 * Chances are that the relayd has closed the socket so we just
424 * continue cleaning up.
428 /* Both conditions are met, we destroy the relayd. */
429 if (uatomic_read(&relayd
->refcount
) == 0 &&
430 uatomic_read(&relayd
->destroy_flag
)) {
431 destroy_relayd(relayd
);
436 uatomic_dec(&stream
->chan
->refcount
);
437 if (!uatomic_read(&stream
->chan
->refcount
)
438 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
439 free_chan
= stream
->chan
;
443 consumer_data
.need_update
= 1;
444 pthread_mutex_unlock(&consumer_data
.lock
);
445 pthread_mutex_unlock(&stream
->lock
);
448 consumer_del_channel(free_chan
);
452 call_rcu(&stream
->node
.head
, consumer_free_stream
);
455 struct lttng_consumer_stream
*consumer_allocate_stream(
456 int channel_key
, int stream_key
,
457 int shm_fd
, int wait_fd
,
458 enum lttng_consumer_stream_state state
,
460 enum lttng_event_output output
,
461 const char *path_name
,
469 struct lttng_consumer_stream
*stream
;
471 stream
= zmalloc(sizeof(*stream
));
472 if (stream
== NULL
) {
473 PERROR("malloc struct lttng_consumer_stream");
474 *alloc_ret
= -ENOMEM
;
479 * Get stream's channel reference. Needed when adding the stream to the
482 stream
->chan
= consumer_find_channel(channel_key
);
484 *alloc_ret
= -ENOENT
;
485 ERR("Unable to find channel for stream %d", stream_key
);
489 stream
->key
= stream_key
;
490 stream
->shm_fd
= shm_fd
;
491 stream
->wait_fd
= wait_fd
;
493 stream
->out_fd_offset
= 0;
494 stream
->state
= state
;
495 stream
->mmap_len
= mmap_len
;
496 stream
->mmap_base
= NULL
;
497 stream
->output
= output
;
500 stream
->net_seq_idx
= net_index
;
501 stream
->metadata_flag
= metadata_flag
;
502 stream
->session_id
= session_id
;
503 strncpy(stream
->path_name
, path_name
, sizeof(stream
->path_name
));
504 stream
->path_name
[sizeof(stream
->path_name
) - 1] = '\0';
505 pthread_mutex_init(&stream
->lock
, NULL
);
508 * Index differently the metadata node because the thread is using an
509 * internal hash table to match streams in the metadata_ht to the epoll set
513 lttng_ht_node_init_ulong(&stream
->node
, stream
->wait_fd
);
515 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
518 /* Init session id node with the stream session id */
519 lttng_ht_node_init_ulong(&stream
->node_session_id
, stream
->session_id
);
522 * The cpu number is needed before using any ustctl_* actions. Ignored for
523 * the kernel so the value does not matter.
525 pthread_mutex_lock(&consumer_data
.lock
);
526 stream
->cpu
= stream
->chan
->cpucount
++;
527 pthread_mutex_unlock(&consumer_data
.lock
);
529 DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu,"
530 " out_fd %d, net_seq_idx %d, session_id %" PRIu64
,
531 stream
->path_name
, stream
->key
, stream
->shm_fd
, stream
->wait_fd
,
532 (unsigned long long) stream
->mmap_len
, stream
->out_fd
,
533 stream
->net_seq_idx
, stream
->session_id
);
543 * Add a stream to the global list protected by a mutex.
545 static int consumer_add_stream(struct lttng_consumer_stream
*stream
,
549 struct consumer_relayd_sock_pair
*relayd
;
554 DBG3("Adding consumer stream %d", stream
->key
);
556 pthread_mutex_lock(&consumer_data
.lock
);
559 /* Steal stream identifier to avoid having streams with the same key */
560 consumer_steal_stream_key(stream
->key
, ht
);
562 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
565 * Add stream to the stream_list_ht of the consumer data. No need to steal
566 * the key since the HT does not use it and we allow to add redundant keys
569 lttng_ht_add_ulong(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
571 /* Check and cleanup relayd */
572 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
573 if (relayd
!= NULL
) {
574 uatomic_inc(&relayd
->refcount
);
577 /* Update channel refcount once added without error(s). */
578 uatomic_inc(&stream
->chan
->refcount
);
581 * When nb_init_streams reaches 0, we don't need to trigger any action in
582 * terms of destroying the associated channel, because the action that
583 * causes the count to become 0 also causes a stream to be added. The
584 * channel deletion will thus be triggered by the following removal of this
587 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
588 uatomic_dec(&stream
->chan
->nb_init_streams
);
591 /* Update consumer data once the node is inserted. */
592 consumer_data
.stream_count
++;
593 consumer_data
.need_update
= 1;
596 pthread_mutex_unlock(&consumer_data
.lock
);
602 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
603 * be acquired before calling this.
605 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
608 struct lttng_ht_node_ulong
*node
;
609 struct lttng_ht_iter iter
;
611 if (relayd
== NULL
) {
616 lttng_ht_lookup(consumer_data
.relayd_ht
,
617 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
618 node
= lttng_ht_iter_get_node_ulong(&iter
);
620 /* Relayd already exist. Ignore the insertion */
623 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
630 * Allocate and return a consumer relayd socket.
632 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
635 struct consumer_relayd_sock_pair
*obj
= NULL
;
637 /* Negative net sequence index is a failure */
638 if (net_seq_idx
< 0) {
642 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
644 PERROR("zmalloc relayd sock");
648 obj
->net_seq_idx
= net_seq_idx
;
650 obj
->destroy_flag
= 0;
651 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
652 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
659 * Find a relayd socket pair in the global consumer data.
661 * Return the object if found else NULL.
662 * RCU read-side lock must be held across this call and while using the
665 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
667 struct lttng_ht_iter iter
;
668 struct lttng_ht_node_ulong
*node
;
669 struct consumer_relayd_sock_pair
*relayd
= NULL
;
671 /* Negative keys are lookup failures */
676 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
678 node
= lttng_ht_iter_get_node_ulong(&iter
);
680 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
688 * Handle stream for relayd transmission if the stream applies for network
689 * streaming where the net sequence index is set.
691 * Return destination file descriptor or negative value on error.
693 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
694 size_t data_size
, unsigned long padding
,
695 struct consumer_relayd_sock_pair
*relayd
)
698 struct lttcomm_relayd_data_hdr data_hdr
;
704 /* Reset data header */
705 memset(&data_hdr
, 0, sizeof(data_hdr
));
707 if (stream
->metadata_flag
) {
708 /* Caller MUST acquire the relayd control socket lock */
709 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
714 /* Metadata are always sent on the control socket. */
715 outfd
= relayd
->control_sock
.fd
;
717 /* Set header with stream information */
718 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
719 data_hdr
.data_size
= htobe32(data_size
);
720 data_hdr
.padding_size
= htobe32(padding
);
721 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
722 /* Other fields are zeroed previously */
724 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
730 /* Set to go on data socket */
731 outfd
= relayd
->data_sock
.fd
;
739 void consumer_free_channel(struct rcu_head
*head
)
741 struct lttng_ht_node_ulong
*node
=
742 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
743 struct lttng_consumer_channel
*channel
=
744 caa_container_of(node
, struct lttng_consumer_channel
, node
);
750 * Remove a channel from the global list protected by a mutex. This
751 * function is also responsible for freeing its data structures.
753 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
756 struct lttng_ht_iter iter
;
758 pthread_mutex_lock(&consumer_data
.lock
);
760 switch (consumer_data
.type
) {
761 case LTTNG_CONSUMER_KERNEL
:
763 case LTTNG_CONSUMER32_UST
:
764 case LTTNG_CONSUMER64_UST
:
765 lttng_ustconsumer_del_channel(channel
);
768 ERR("Unknown consumer_data type");
774 iter
.iter
.node
= &channel
->node
.node
;
775 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
779 if (channel
->mmap_base
!= NULL
) {
780 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
785 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
786 ret
= close(channel
->wait_fd
);
791 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
792 ret
= close(channel
->shm_fd
);
798 call_rcu(&channel
->node
.head
, consumer_free_channel
);
800 pthread_mutex_unlock(&consumer_data
.lock
);
803 struct lttng_consumer_channel
*consumer_allocate_channel(
805 int shm_fd
, int wait_fd
,
807 uint64_t max_sb_size
,
808 unsigned int nb_init_streams
)
810 struct lttng_consumer_channel
*channel
;
813 channel
= zmalloc(sizeof(*channel
));
814 if (channel
== NULL
) {
815 PERROR("malloc struct lttng_consumer_channel");
818 channel
->key
= channel_key
;
819 channel
->shm_fd
= shm_fd
;
820 channel
->wait_fd
= wait_fd
;
821 channel
->mmap_len
= mmap_len
;
822 channel
->max_sb_size
= max_sb_size
;
823 channel
->refcount
= 0;
824 channel
->nb_init_streams
= nb_init_streams
;
825 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
827 switch (consumer_data
.type
) {
828 case LTTNG_CONSUMER_KERNEL
:
829 channel
->mmap_base
= NULL
;
830 channel
->mmap_len
= 0;
832 case LTTNG_CONSUMER32_UST
:
833 case LTTNG_CONSUMER64_UST
:
834 ret
= lttng_ustconsumer_allocate_channel(channel
);
841 ERR("Unknown consumer_data type");
845 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
846 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
847 (unsigned long long) channel
->mmap_len
,
848 (unsigned long long) channel
->max_sb_size
);
854 * Add a channel to the global list protected by a mutex.
856 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
858 struct lttng_ht_node_ulong
*node
;
859 struct lttng_ht_iter iter
;
861 pthread_mutex_lock(&consumer_data
.lock
);
862 /* Steal channel identifier, for UST */
863 consumer_steal_channel_key(channel
->key
);
866 lttng_ht_lookup(consumer_data
.channel_ht
,
867 (void *)((unsigned long) channel
->key
), &iter
);
868 node
= lttng_ht_iter_get_node_ulong(&iter
);
870 /* Channel already exist. Ignore the insertion */
874 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
878 pthread_mutex_unlock(&consumer_data
.lock
);
884 * Allocate the pollfd structure and the local view of the out fds to avoid
885 * doing a lookup in the linked list and concurrency issues when writing is
886 * needed. Called with consumer_data.lock held.
888 * Returns the number of fds in the structures.
890 static int consumer_update_poll_array(
891 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
892 struct lttng_consumer_stream
**local_stream
, struct lttng_ht
*ht
)
895 struct lttng_ht_iter iter
;
896 struct lttng_consumer_stream
*stream
;
898 DBG("Updating poll fd array");
900 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
902 * Only active streams with an active end point can be added to the
903 * poll set and local stream storage of the thread.
905 * There is a potential race here for endpoint_status to be updated
906 * just after the check. However, this is OK since the stream(s) will
907 * be deleted once the thread is notified that the end point state has
908 * changed where this function will be called back again.
910 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
911 stream
->endpoint_status
) {
914 DBG("Active FD %d", stream
->wait_fd
);
915 (*pollfd
)[i
].fd
= stream
->wait_fd
;
916 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
917 local_stream
[i
] = stream
;
923 * Insert the consumer_data_pipe at the end of the array and don't
924 * increment i so nb_fd is the number of real FD.
926 (*pollfd
)[i
].fd
= ctx
->consumer_data_pipe
[0];
927 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
932 * Poll on the should_quit pipe and the command socket return -1 on error and
933 * should exit, 0 if data is available on the command socket
935 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
940 num_rdy
= poll(consumer_sockpoll
, 2, -1);
943 * Restart interrupted system call.
945 if (errno
== EINTR
) {
948 PERROR("Poll error");
951 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
952 DBG("consumer_should_quit wake up");
962 * Set the error socket.
964 void lttng_consumer_set_error_sock(
965 struct lttng_consumer_local_data
*ctx
, int sock
)
967 ctx
->consumer_error_socket
= sock
;
971 * Set the command socket path.
973 void lttng_consumer_set_command_sock_path(
974 struct lttng_consumer_local_data
*ctx
, char *sock
)
976 ctx
->consumer_command_sock_path
= sock
;
980 * Send return code to the session daemon.
981 * If the socket is not defined, we return 0, it is not a fatal error
983 int lttng_consumer_send_error(
984 struct lttng_consumer_local_data
*ctx
, int cmd
)
986 if (ctx
->consumer_error_socket
> 0) {
987 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
988 sizeof(enum lttcomm_sessiond_command
));
995 * Close all the tracefiles and stream fds, should be called when all instances
998 void lttng_consumer_cleanup(void)
1000 struct lttng_ht_iter iter
;
1001 struct lttng_ht_node_ulong
*node
;
1005 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
1007 struct lttng_consumer_channel
*channel
=
1008 caa_container_of(node
, struct lttng_consumer_channel
, node
);
1009 consumer_del_channel(channel
);
1014 lttng_ht_destroy(consumer_data
.channel_ht
);
1018 * Called from signal handler.
1020 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1025 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1026 } while (ret
< 0 && errno
== EINTR
);
1028 PERROR("write consumer quit");
1031 DBG("Consumer flag that it should quit");
1034 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1037 int outfd
= stream
->out_fd
;
1040 * This does a blocking write-and-wait on any page that belongs to the
1041 * subbuffer prior to the one we just wrote.
1042 * Don't care about error values, as these are just hints and ways to
1043 * limit the amount of page cache used.
1045 if (orig_offset
< stream
->chan
->max_sb_size
) {
1048 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
1049 stream
->chan
->max_sb_size
,
1050 SYNC_FILE_RANGE_WAIT_BEFORE
1051 | SYNC_FILE_RANGE_WRITE
1052 | SYNC_FILE_RANGE_WAIT_AFTER
);
1054 * Give hints to the kernel about how we access the file:
1055 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1058 * We need to call fadvise again after the file grows because the
1059 * kernel does not seem to apply fadvise to non-existing parts of the
1062 * Call fadvise _after_ having waited for the page writeback to
1063 * complete because the dirty page writeback semantic is not well
1064 * defined. So it can be expected to lead to lower throughput in
1067 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
1068 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
1072 * Initialise the necessary environnement :
1073 * - create a new context
1074 * - create the poll_pipe
1075 * - create the should_quit pipe (for signal handler)
1076 * - create the thread pipe (for splice)
1078 * Takes a function pointer as argument, this function is called when data is
1079 * available on a buffer. This function is responsible to do the
1080 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1081 * buffer configuration and then kernctl_put_next_subbuf at the end.
1083 * Returns a pointer to the new context or NULL on error.
1085 struct lttng_consumer_local_data
*lttng_consumer_create(
1086 enum lttng_consumer_type type
,
1087 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1088 struct lttng_consumer_local_data
*ctx
),
1089 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1090 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1091 int (*update_stream
)(int stream_key
, uint32_t state
))
1094 struct lttng_consumer_local_data
*ctx
;
1096 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1097 consumer_data
.type
== type
);
1098 consumer_data
.type
= type
;
1100 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1102 PERROR("allocating context");
1106 ctx
->consumer_error_socket
= -1;
1107 /* assign the callbacks */
1108 ctx
->on_buffer_ready
= buffer_ready
;
1109 ctx
->on_recv_channel
= recv_channel
;
1110 ctx
->on_recv_stream
= recv_stream
;
1111 ctx
->on_update_stream
= update_stream
;
1113 ret
= pipe(ctx
->consumer_data_pipe
);
1115 PERROR("Error creating poll pipe");
1116 goto error_poll_pipe
;
1119 /* set read end of the pipe to non-blocking */
1120 ret
= fcntl(ctx
->consumer_data_pipe
[0], F_SETFL
, O_NONBLOCK
);
1122 PERROR("fcntl O_NONBLOCK");
1123 goto error_poll_fcntl
;
1126 /* set write end of the pipe to non-blocking */
1127 ret
= fcntl(ctx
->consumer_data_pipe
[1], F_SETFL
, O_NONBLOCK
);
1129 PERROR("fcntl O_NONBLOCK");
1130 goto error_poll_fcntl
;
1133 ret
= pipe(ctx
->consumer_should_quit
);
1135 PERROR("Error creating recv pipe");
1136 goto error_quit_pipe
;
1139 ret
= pipe(ctx
->consumer_thread_pipe
);
1141 PERROR("Error creating thread pipe");
1142 goto error_thread_pipe
;
1145 ret
= utils_create_pipe(ctx
->consumer_metadata_pipe
);
1147 goto error_metadata_pipe
;
1150 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1152 goto error_splice_pipe
;
1158 utils_close_pipe(ctx
->consumer_metadata_pipe
);
1159 error_metadata_pipe
:
1160 utils_close_pipe(ctx
->consumer_thread_pipe
);
1162 for (i
= 0; i
< 2; i
++) {
1165 err
= close(ctx
->consumer_should_quit
[i
]);
1172 for (i
= 0; i
< 2; i
++) {
1175 err
= close(ctx
->consumer_data_pipe
[i
]);
1187 * Close all fds associated with the instance and free the context.
1189 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1193 DBG("Consumer destroying it. Closing everything.");
1195 ret
= close(ctx
->consumer_error_socket
);
1199 ret
= close(ctx
->consumer_thread_pipe
[0]);
1203 ret
= close(ctx
->consumer_thread_pipe
[1]);
1207 ret
= close(ctx
->consumer_data_pipe
[0]);
1211 ret
= close(ctx
->consumer_data_pipe
[1]);
1215 ret
= close(ctx
->consumer_should_quit
[0]);
1219 ret
= close(ctx
->consumer_should_quit
[1]);
1223 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1225 unlink(ctx
->consumer_command_sock_path
);
1230 * Write the metadata stream id on the specified file descriptor.
1232 static int write_relayd_metadata_id(int fd
,
1233 struct lttng_consumer_stream
*stream
,
1234 struct consumer_relayd_sock_pair
*relayd
,
1235 unsigned long padding
)
1238 struct lttcomm_relayd_metadata_payload hdr
;
1240 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1241 hdr
.padding_size
= htobe32(padding
);
1243 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1244 } while (ret
< 0 && errno
== EINTR
);
1246 PERROR("write metadata stream id");
1249 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1250 stream
->relayd_stream_id
, padding
);
1257 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1258 * core function for writing trace buffers to either the local filesystem or
1261 * Careful review MUST be put if any changes occur!
1263 * Returns the number of bytes written
1265 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1266 struct lttng_consumer_local_data
*ctx
,
1267 struct lttng_consumer_stream
*stream
, unsigned long len
,
1268 unsigned long padding
)
1270 unsigned long mmap_offset
;
1271 ssize_t ret
= 0, written
= 0;
1272 off_t orig_offset
= stream
->out_fd_offset
;
1273 /* Default is on the disk */
1274 int outfd
= stream
->out_fd
;
1275 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1276 unsigned int relayd_hang_up
= 0;
1278 /* RCU lock for the relayd pointer */
1281 pthread_mutex_lock(&stream
->lock
);
1283 /* Flag that the current stream if set for network streaming. */
1284 if (stream
->net_seq_idx
!= -1) {
1285 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1286 if (relayd
== NULL
) {
1291 /* get the offset inside the fd to mmap */
1292 switch (consumer_data
.type
) {
1293 case LTTNG_CONSUMER_KERNEL
:
1294 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1296 case LTTNG_CONSUMER32_UST
:
1297 case LTTNG_CONSUMER64_UST
:
1298 ret
= lttng_ustctl_get_mmap_read_offset(stream
->chan
->handle
,
1299 stream
->buf
, &mmap_offset
);
1302 ERR("Unknown consumer_data type");
1307 PERROR("tracer ctl get_mmap_read_offset");
1312 /* Handle stream on the relayd if the output is on the network */
1314 unsigned long netlen
= len
;
1317 * Lock the control socket for the complete duration of the function
1318 * since from this point on we will use the socket.
1320 if (stream
->metadata_flag
) {
1321 /* Metadata requires the control socket. */
1322 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1323 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1326 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1328 /* Use the returned socket. */
1331 /* Write metadata stream id before payload */
1332 if (stream
->metadata_flag
) {
1333 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1336 /* Socket operation failed. We consider the relayd dead */
1337 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1345 /* Socket operation failed. We consider the relayd dead */
1346 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1350 /* Else, use the default set before which is the filesystem. */
1353 /* No streaming, we have to set the len with the full padding */
1359 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
1360 } while (ret
< 0 && errno
== EINTR
);
1361 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1363 PERROR("Error in file write");
1367 /* Socket operation failed. We consider the relayd dead */
1368 if (errno
== EPIPE
|| errno
== EINVAL
) {
1373 } else if (ret
> len
) {
1374 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1382 /* This call is useless on a socket so better save a syscall. */
1384 /* This won't block, but will start writeout asynchronously */
1385 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1386 SYNC_FILE_RANGE_WRITE
);
1387 stream
->out_fd_offset
+= ret
;
1391 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1395 * This is a special case that the relayd has closed its socket. Let's
1396 * cleanup the relayd object and all associated streams.
1398 if (relayd
&& relayd_hang_up
) {
1399 cleanup_relayd(relayd
, ctx
);
1403 /* Unlock only if ctrl socket used */
1404 if (relayd
&& stream
->metadata_flag
) {
1405 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1407 pthread_mutex_unlock(&stream
->lock
);
1414 * Splice the data from the ring buffer to the tracefile.
1416 * Returns the number of bytes spliced.
1418 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1419 struct lttng_consumer_local_data
*ctx
,
1420 struct lttng_consumer_stream
*stream
, unsigned long len
,
1421 unsigned long padding
)
1423 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1425 off_t orig_offset
= stream
->out_fd_offset
;
1426 int fd
= stream
->wait_fd
;
1427 /* Default is on the disk */
1428 int outfd
= stream
->out_fd
;
1429 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1431 unsigned int relayd_hang_up
= 0;
1433 switch (consumer_data
.type
) {
1434 case LTTNG_CONSUMER_KERNEL
:
1436 case LTTNG_CONSUMER32_UST
:
1437 case LTTNG_CONSUMER64_UST
:
1438 /* Not supported for user space tracing */
1441 ERR("Unknown consumer_data type");
1445 /* RCU lock for the relayd pointer */
1448 pthread_mutex_lock(&stream
->lock
);
1450 /* Flag that the current stream if set for network streaming. */
1451 if (stream
->net_seq_idx
!= -1) {
1452 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1453 if (relayd
== NULL
) {
1459 * Choose right pipe for splice. Metadata and trace data are handled by
1460 * different threads hence the use of two pipes in order not to race or
1461 * corrupt the written data.
1463 if (stream
->metadata_flag
) {
1464 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1466 splice_pipe
= ctx
->consumer_thread_pipe
;
1469 /* Write metadata stream id before payload */
1471 int total_len
= len
;
1473 if (stream
->metadata_flag
) {
1475 * Lock the control socket for the complete duration of the function
1476 * since from this point on we will use the socket.
1478 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1480 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1484 /* Socket operation failed. We consider the relayd dead */
1485 if (ret
== -EBADF
) {
1486 WARN("Remote relayd disconnected. Stopping");
1493 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1496 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1498 /* Use the returned socket. */
1501 /* Socket operation failed. We consider the relayd dead */
1502 if (ret
== -EBADF
) {
1503 WARN("Remote relayd disconnected. Stopping");
1510 /* No streaming, we have to set the len with the full padding */
1515 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1516 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1517 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1518 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1519 DBG("splice chan to pipe, ret %zd", ret_splice
);
1520 if (ret_splice
< 0) {
1521 PERROR("Error in relay splice");
1523 written
= ret_splice
;
1529 /* Handle stream on the relayd if the output is on the network */
1531 if (stream
->metadata_flag
) {
1532 size_t metadata_payload_size
=
1533 sizeof(struct lttcomm_relayd_metadata_payload
);
1535 /* Update counter to fit the spliced data */
1536 ret_splice
+= metadata_payload_size
;
1537 len
+= metadata_payload_size
;
1539 * We do this so the return value can match the len passed as
1540 * argument to this function.
1542 written
-= metadata_payload_size
;
1546 /* Splice data out */
1547 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1548 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1549 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1550 if (ret_splice
< 0) {
1551 PERROR("Error in file splice");
1553 written
= ret_splice
;
1555 /* Socket operation failed. We consider the relayd dead */
1556 if (errno
== EBADF
|| errno
== EPIPE
) {
1557 WARN("Remote relayd disconnected. Stopping");
1563 } else if (ret_splice
> len
) {
1565 PERROR("Wrote more data than requested %zd (len: %lu)",
1567 written
+= ret_splice
;
1573 /* This call is useless on a socket so better save a syscall. */
1575 /* This won't block, but will start writeout asynchronously */
1576 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1577 SYNC_FILE_RANGE_WRITE
);
1578 stream
->out_fd_offset
+= ret_splice
;
1580 written
+= ret_splice
;
1582 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1590 * This is a special case that the relayd has closed its socket. Let's
1591 * cleanup the relayd object and all associated streams.
1593 if (relayd
&& relayd_hang_up
) {
1594 cleanup_relayd(relayd
, ctx
);
1595 /* Skip splice error so the consumer does not fail */
1600 /* send the appropriate error description to sessiond */
1603 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1606 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1609 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1614 if (relayd
&& stream
->metadata_flag
) {
1615 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1617 pthread_mutex_unlock(&stream
->lock
);
1624 * Take a snapshot for a specific fd
1626 * Returns 0 on success, < 0 on error
1628 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1629 struct lttng_consumer_stream
*stream
)
1631 switch (consumer_data
.type
) {
1632 case LTTNG_CONSUMER_KERNEL
:
1633 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1634 case LTTNG_CONSUMER32_UST
:
1635 case LTTNG_CONSUMER64_UST
:
1636 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1638 ERR("Unknown consumer_data type");
1646 * Get the produced position
1648 * Returns 0 on success, < 0 on error
1650 int lttng_consumer_get_produced_snapshot(
1651 struct lttng_consumer_local_data
*ctx
,
1652 struct lttng_consumer_stream
*stream
,
1655 switch (consumer_data
.type
) {
1656 case LTTNG_CONSUMER_KERNEL
:
1657 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1658 case LTTNG_CONSUMER32_UST
:
1659 case LTTNG_CONSUMER64_UST
:
1660 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1662 ERR("Unknown consumer_data type");
1668 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1669 int sock
, struct pollfd
*consumer_sockpoll
)
1671 switch (consumer_data
.type
) {
1672 case LTTNG_CONSUMER_KERNEL
:
1673 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1674 case LTTNG_CONSUMER32_UST
:
1675 case LTTNG_CONSUMER64_UST
:
1676 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1678 ERR("Unknown consumer_data type");
1685 * Iterate over all streams of the hashtable and free them properly.
1687 * WARNING: *MUST* be used with data stream only.
1689 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1692 struct lttng_ht_iter iter
;
1693 struct lttng_consumer_stream
*stream
;
1700 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1701 ret
= lttng_ht_del(ht
, &iter
);
1704 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1708 lttng_ht_destroy(ht
);
1712 * Iterate over all streams of the hashtable and free them properly.
1714 * XXX: Should not be only for metadata stream or else use an other name.
1716 static void destroy_stream_ht(struct lttng_ht
*ht
)
1719 struct lttng_ht_iter iter
;
1720 struct lttng_consumer_stream
*stream
;
1727 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1728 ret
= lttng_ht_del(ht
, &iter
);
1731 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1735 lttng_ht_destroy(ht
);
1739 * Clean up a metadata stream and free its memory.
1741 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1742 struct lttng_ht
*ht
)
1745 struct lttng_ht_iter iter
;
1746 struct lttng_consumer_channel
*free_chan
= NULL
;
1747 struct consumer_relayd_sock_pair
*relayd
;
1751 * This call should NEVER receive regular stream. It must always be
1752 * metadata stream and this is crucial for data structure synchronization.
1754 assert(stream
->metadata_flag
);
1756 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1759 /* Means the stream was allocated but not successfully added */
1763 pthread_mutex_lock(&stream
->lock
);
1765 pthread_mutex_lock(&consumer_data
.lock
);
1766 switch (consumer_data
.type
) {
1767 case LTTNG_CONSUMER_KERNEL
:
1768 if (stream
->mmap_base
!= NULL
) {
1769 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1771 PERROR("munmap metadata stream");
1775 case LTTNG_CONSUMER32_UST
:
1776 case LTTNG_CONSUMER64_UST
:
1777 lttng_ustconsumer_del_stream(stream
);
1780 ERR("Unknown consumer_data type");
1786 iter
.iter
.node
= &stream
->node
.node
;
1787 ret
= lttng_ht_del(ht
, &iter
);
1790 /* Remove node session id from the consumer_data stream ht */
1791 iter
.iter
.node
= &stream
->node_session_id
.node
;
1792 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1796 if (stream
->out_fd
>= 0) {
1797 ret
= close(stream
->out_fd
);
1803 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
1804 ret
= close(stream
->wait_fd
);
1810 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
1811 ret
= close(stream
->shm_fd
);
1817 /* Check and cleanup relayd */
1819 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1820 if (relayd
!= NULL
) {
1821 uatomic_dec(&relayd
->refcount
);
1822 assert(uatomic_read(&relayd
->refcount
) >= 0);
1824 /* Closing streams requires to lock the control socket. */
1825 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1826 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1827 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1828 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1830 DBG("Unable to close stream on the relayd. Continuing");
1832 * Continue here. There is nothing we can do for the relayd.
1833 * Chances are that the relayd has closed the socket so we just
1834 * continue cleaning up.
1838 /* Both conditions are met, we destroy the relayd. */
1839 if (uatomic_read(&relayd
->refcount
) == 0 &&
1840 uatomic_read(&relayd
->destroy_flag
)) {
1841 destroy_relayd(relayd
);
1846 /* Atomically decrement channel refcount since other threads can use it. */
1847 uatomic_dec(&stream
->chan
->refcount
);
1848 if (!uatomic_read(&stream
->chan
->refcount
)
1849 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
1850 /* Go for channel deletion! */
1851 free_chan
= stream
->chan
;
1855 pthread_mutex_unlock(&consumer_data
.lock
);
1856 pthread_mutex_unlock(&stream
->lock
);
1859 consumer_del_channel(free_chan
);
1863 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1867 * Action done with the metadata stream when adding it to the consumer internal
1868 * data structures to handle it.
1870 static int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
,
1871 struct lttng_ht
*ht
)
1874 struct consumer_relayd_sock_pair
*relayd
;
1879 DBG3("Adding metadata stream %d to hash table", stream
->wait_fd
);
1881 pthread_mutex_lock(&consumer_data
.lock
);
1884 * From here, refcounts are updated so be _careful_ when returning an error
1889 /* Find relayd and, if one is found, increment refcount. */
1890 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1891 if (relayd
!= NULL
) {
1892 uatomic_inc(&relayd
->refcount
);
1895 /* Update channel refcount once added without error(s). */
1896 uatomic_inc(&stream
->chan
->refcount
);
1899 * When nb_init_streams reaches 0, we don't need to trigger any action in
1900 * terms of destroying the associated channel, because the action that
1901 * causes the count to become 0 also causes a stream to be added. The
1902 * channel deletion will thus be triggered by the following removal of this
1905 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
1906 uatomic_dec(&stream
->chan
->nb_init_streams
);
1909 /* Steal stream identifier to avoid having streams with the same key */
1910 consumer_steal_stream_key(stream
->key
, ht
);
1912 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
1915 * Add stream to the stream_list_ht of the consumer data. No need to steal
1916 * the key since the HT does not use it and we allow to add redundant keys
1919 lttng_ht_add_ulong(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
1923 pthread_mutex_unlock(&consumer_data
.lock
);
1928 * Delete data stream that are flagged for deletion (endpoint_status).
1930 static void validate_endpoint_status_data_stream(void)
1932 struct lttng_ht_iter iter
;
1933 struct lttng_consumer_stream
*stream
;
1935 DBG("Consumer delete flagged data stream");
1938 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1939 /* Validate delete flag of the stream */
1940 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_INACTIVE
) {
1943 /* Delete it right now */
1944 consumer_del_stream(stream
, data_ht
);
1950 * Delete metadata stream that are flagged for deletion (endpoint_status).
1952 static void validate_endpoint_status_metadata_stream(
1953 struct lttng_poll_event
*pollset
)
1955 struct lttng_ht_iter iter
;
1956 struct lttng_consumer_stream
*stream
;
1958 DBG("Consumer delete flagged metadata stream");
1963 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1964 /* Validate delete flag of the stream */
1965 if (!stream
->endpoint_status
) {
1969 * Remove from pollset so the metadata thread can continue without
1970 * blocking on a deleted stream.
1972 lttng_poll_del(pollset
, stream
->wait_fd
);
1974 /* Delete it right now */
1975 consumer_del_metadata_stream(stream
, metadata_ht
);
1981 * Thread polls on metadata file descriptor and write them on disk or on the
1984 void *consumer_thread_metadata_poll(void *data
)
1987 uint32_t revents
, nb_fd
;
1988 struct lttng_consumer_stream
*stream
= NULL
;
1989 struct lttng_ht_iter iter
;
1990 struct lttng_ht_node_ulong
*node
;
1991 struct lttng_poll_event events
;
1992 struct lttng_consumer_local_data
*ctx
= data
;
1995 rcu_register_thread();
1997 DBG("Thread metadata poll started");
1999 /* Size is set to 1 for the consumer_metadata pipe */
2000 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2002 ERR("Poll set creation failed");
2006 ret
= lttng_poll_add(&events
, ctx
->consumer_metadata_pipe
[0], LPOLLIN
);
2012 DBG("Metadata main loop started");
2015 lttng_poll_reset(&events
);
2017 nb_fd
= LTTNG_POLL_GETNB(&events
);
2019 /* Only the metadata pipe is set */
2020 if (nb_fd
== 0 && consumer_quit
== 1) {
2025 DBG("Metadata poll wait with %d fd(s)", nb_fd
);
2026 ret
= lttng_poll_wait(&events
, -1);
2027 DBG("Metadata event catched in thread");
2029 if (errno
== EINTR
) {
2030 ERR("Poll EINTR catched");
2036 /* From here, the event is a metadata wait fd */
2037 for (i
= 0; i
< nb_fd
; i
++) {
2038 revents
= LTTNG_POLL_GETEV(&events
, i
);
2039 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2041 /* Just don't waste time if no returned events for the fd */
2046 if (pollfd
== ctx
->consumer_metadata_pipe
[0]) {
2047 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2048 DBG("Metadata thread pipe hung up");
2050 * Remove the pipe from the poll set and continue the loop
2051 * since their might be data to consume.
2053 lttng_poll_del(&events
, ctx
->consumer_metadata_pipe
[0]);
2054 close(ctx
->consumer_metadata_pipe
[0]);
2056 } else if (revents
& LPOLLIN
) {
2058 /* Get the stream pointer received */
2059 ret
= read(pollfd
, &stream
, sizeof(stream
));
2060 } while (ret
< 0 && errno
== EINTR
);
2062 ret
< sizeof(struct lttng_consumer_stream
*)) {
2063 PERROR("read metadata stream");
2065 * Let's continue here and hope we can still work
2066 * without stopping the consumer. XXX: Should we?
2071 /* A NULL stream means that the state has changed. */
2072 if (stream
== NULL
) {
2073 /* Check for deleted streams. */
2074 validate_endpoint_status_metadata_stream(&events
);
2078 DBG("Adding metadata stream %d to poll set",
2081 ret
= consumer_add_metadata_stream(stream
, metadata_ht
);
2083 ERR("Unable to add metadata stream");
2084 /* Stream was not setup properly. Continuing. */
2085 consumer_del_metadata_stream(stream
, NULL
);
2089 /* Add metadata stream to the global poll events list */
2090 lttng_poll_add(&events
, stream
->wait_fd
,
2091 LPOLLIN
| LPOLLPRI
);
2094 /* Handle other stream */
2099 lttng_ht_lookup(metadata_ht
, (void *)((unsigned long) pollfd
),
2101 node
= lttng_ht_iter_get_node_ulong(&iter
);
2104 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2107 /* Check for error event */
2108 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2109 DBG("Metadata fd %d is hup|err.", pollfd
);
2110 if (!stream
->hangup_flush_done
2111 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2112 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2113 DBG("Attempting to flush and consume the UST buffers");
2114 lttng_ustconsumer_on_stream_hangup(stream
);
2116 /* We just flushed the stream now read it. */
2118 len
= ctx
->on_buffer_ready(stream
, ctx
);
2120 * We don't check the return value here since if we get
2121 * a negative len, it means an error occured thus we
2122 * simply remove it from the poll set and free the
2128 lttng_poll_del(&events
, stream
->wait_fd
);
2130 * This call update the channel states, closes file descriptors
2131 * and securely free the stream.
2133 consumer_del_metadata_stream(stream
, metadata_ht
);
2134 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2135 /* Get the data out of the metadata file descriptor */
2136 DBG("Metadata available on fd %d", pollfd
);
2137 assert(stream
->wait_fd
== pollfd
);
2139 len
= ctx
->on_buffer_ready(stream
, ctx
);
2140 /* It's ok to have an unavailable sub-buffer */
2141 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2142 /* Clean up stream from consumer and free it. */
2143 lttng_poll_del(&events
, stream
->wait_fd
);
2144 consumer_del_metadata_stream(stream
, metadata_ht
);
2145 } else if (len
> 0) {
2146 stream
->data_read
= 1;
2150 /* Release RCU lock for the stream looked up */
2157 DBG("Metadata poll thread exiting");
2158 lttng_poll_clean(&events
);
2161 destroy_stream_ht(metadata_ht
);
2164 rcu_unregister_thread();
2169 * This thread polls the fds in the set to consume the data and write
2170 * it to tracefile if necessary.
2172 void *consumer_thread_data_poll(void *data
)
2174 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2175 struct pollfd
*pollfd
= NULL
;
2176 /* local view of the streams */
2177 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2178 /* local view of consumer_data.fds_count */
2180 struct lttng_consumer_local_data
*ctx
= data
;
2183 rcu_register_thread();
2185 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2186 if (data_ht
== NULL
) {
2190 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
2197 * the fds set has been updated, we need to update our
2198 * local array as well
2200 pthread_mutex_lock(&consumer_data
.lock
);
2201 if (consumer_data
.need_update
) {
2202 if (pollfd
!= NULL
) {
2206 if (local_stream
!= NULL
) {
2208 local_stream
= NULL
;
2211 /* allocate for all fds + 1 for the consumer_data_pipe */
2212 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2213 if (pollfd
== NULL
) {
2214 PERROR("pollfd malloc");
2215 pthread_mutex_unlock(&consumer_data
.lock
);
2219 /* allocate for all fds + 1 for the consumer_data_pipe */
2220 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2221 sizeof(struct lttng_consumer_stream
));
2222 if (local_stream
== NULL
) {
2223 PERROR("local_stream malloc");
2224 pthread_mutex_unlock(&consumer_data
.lock
);
2227 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
,
2230 ERR("Error in allocating pollfd or local_outfds");
2231 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2232 pthread_mutex_unlock(&consumer_data
.lock
);
2236 consumer_data
.need_update
= 0;
2238 pthread_mutex_unlock(&consumer_data
.lock
);
2240 /* No FDs and consumer_quit, consumer_cleanup the thread */
2241 if (nb_fd
== 0 && consumer_quit
== 1) {
2244 /* poll on the array of fds */
2246 DBG("polling on %d fd", nb_fd
+ 1);
2247 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2248 DBG("poll num_rdy : %d", num_rdy
);
2249 if (num_rdy
== -1) {
2251 * Restart interrupted system call.
2253 if (errno
== EINTR
) {
2256 PERROR("Poll error");
2257 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2259 } else if (num_rdy
== 0) {
2260 DBG("Polling thread timed out");
2265 * If the consumer_data_pipe triggered poll go directly to the
2266 * beginning of the loop to update the array. We want to prioritize
2267 * array update over low-priority reads.
2269 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2270 size_t pipe_readlen
;
2272 DBG("consumer_data_pipe wake up");
2273 /* Consume 1 byte of pipe data */
2275 pipe_readlen
= read(ctx
->consumer_data_pipe
[0], &new_stream
,
2276 sizeof(new_stream
));
2277 } while (pipe_readlen
== -1 && errno
== EINTR
);
2280 * If the stream is NULL, just ignore it. It's also possible that
2281 * the sessiond poll thread changed the consumer_quit state and is
2282 * waking us up to test it.
2284 if (new_stream
== NULL
) {
2285 validate_endpoint_status_data_stream();
2289 ret
= consumer_add_stream(new_stream
, data_ht
);
2291 ERR("Consumer add stream %d failed. Continuing",
2294 * At this point, if the add_stream fails, it is not in the
2295 * hash table thus passing the NULL value here.
2297 consumer_del_stream(new_stream
, NULL
);
2300 /* Continue to update the local streams and handle prio ones */
2304 /* Take care of high priority channels first. */
2305 for (i
= 0; i
< nb_fd
; i
++) {
2306 if (local_stream
[i
] == NULL
) {
2309 if (pollfd
[i
].revents
& POLLPRI
) {
2310 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2312 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2313 /* it's ok to have an unavailable sub-buffer */
2314 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2315 /* Clean the stream and free it. */
2316 consumer_del_stream(local_stream
[i
], data_ht
);
2317 local_stream
[i
] = NULL
;
2318 } else if (len
> 0) {
2319 local_stream
[i
]->data_read
= 1;
2325 * If we read high prio channel in this loop, try again
2326 * for more high prio data.
2332 /* Take care of low priority channels. */
2333 for (i
= 0; i
< nb_fd
; i
++) {
2334 if (local_stream
[i
] == NULL
) {
2337 if ((pollfd
[i
].revents
& POLLIN
) ||
2338 local_stream
[i
]->hangup_flush_done
) {
2339 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2340 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2341 /* it's ok to have an unavailable sub-buffer */
2342 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2343 /* Clean the stream and free it. */
2344 consumer_del_stream(local_stream
[i
], data_ht
);
2345 local_stream
[i
] = NULL
;
2346 } else if (len
> 0) {
2347 local_stream
[i
]->data_read
= 1;
2352 /* Handle hangup and errors */
2353 for (i
= 0; i
< nb_fd
; i
++) {
2354 if (local_stream
[i
] == NULL
) {
2357 if (!local_stream
[i
]->hangup_flush_done
2358 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2359 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2360 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2361 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2363 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2364 /* Attempt read again, for the data we just flushed. */
2365 local_stream
[i
]->data_read
= 1;
2368 * If the poll flag is HUP/ERR/NVAL and we have
2369 * read no data in this pass, we can remove the
2370 * stream from its hash table.
2372 if ((pollfd
[i
].revents
& POLLHUP
)) {
2373 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2374 if (!local_stream
[i
]->data_read
) {
2375 consumer_del_stream(local_stream
[i
], data_ht
);
2376 local_stream
[i
] = NULL
;
2379 } else if (pollfd
[i
].revents
& POLLERR
) {
2380 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2381 if (!local_stream
[i
]->data_read
) {
2382 consumer_del_stream(local_stream
[i
], data_ht
);
2383 local_stream
[i
] = NULL
;
2386 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2387 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2388 if (!local_stream
[i
]->data_read
) {
2389 consumer_del_stream(local_stream
[i
], data_ht
);
2390 local_stream
[i
] = NULL
;
2394 if (local_stream
[i
] != NULL
) {
2395 local_stream
[i
]->data_read
= 0;
2400 DBG("polling thread exiting");
2401 if (pollfd
!= NULL
) {
2405 if (local_stream
!= NULL
) {
2407 local_stream
= NULL
;
2411 * Close the write side of the pipe so epoll_wait() in
2412 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2413 * read side of the pipe. If we close them both, epoll_wait strangely does
2414 * not return and could create a endless wait period if the pipe is the
2415 * only tracked fd in the poll set. The thread will take care of closing
2418 close(ctx
->consumer_metadata_pipe
[1]);
2421 destroy_data_stream_ht(data_ht
);
2424 rcu_unregister_thread();
2429 * This thread listens on the consumerd socket and receives the file
2430 * descriptors from the session daemon.
2432 void *consumer_thread_sessiond_poll(void *data
)
2434 int sock
, client_socket
, ret
;
2436 * structure to poll for incoming data on communication socket avoids
2437 * making blocking sockets.
2439 struct pollfd consumer_sockpoll
[2];
2440 struct lttng_consumer_local_data
*ctx
= data
;
2442 rcu_register_thread();
2444 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2445 unlink(ctx
->consumer_command_sock_path
);
2446 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2447 if (client_socket
< 0) {
2448 ERR("Cannot create command socket");
2452 ret
= lttcomm_listen_unix_sock(client_socket
);
2457 DBG("Sending ready command to lttng-sessiond");
2458 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2459 /* return < 0 on error, but == 0 is not fatal */
2461 ERR("Error sending ready command to lttng-sessiond");
2465 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2467 PERROR("fcntl O_NONBLOCK");
2471 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2472 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2473 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2474 consumer_sockpoll
[1].fd
= client_socket
;
2475 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2477 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2480 DBG("Connection on client_socket");
2482 /* Blocking call, waiting for transmission */
2483 sock
= lttcomm_accept_unix_sock(client_socket
);
2488 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2490 PERROR("fcntl O_NONBLOCK");
2494 /* update the polling structure to poll on the established socket */
2495 consumer_sockpoll
[1].fd
= sock
;
2496 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2499 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2502 DBG("Incoming command on sock");
2503 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2504 if (ret
== -ENOENT
) {
2505 DBG("Received STOP command");
2510 * This could simply be a session daemon quitting. Don't output
2513 DBG("Communication interrupted on command socket");
2516 if (consumer_quit
) {
2517 DBG("consumer_thread_receive_fds received quit from signal");
2520 DBG("received fds on sock");
2523 DBG("consumer_thread_receive_fds exiting");
2526 * when all fds have hung up, the polling thread
2532 * Notify the data poll thread to poll back again and test the
2533 * consumer_quit state that we just set so to quit gracefully.
2535 notify_thread_pipe(ctx
->consumer_data_pipe
[1]);
2537 rcu_unregister_thread();
2541 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2542 struct lttng_consumer_local_data
*ctx
)
2544 switch (consumer_data
.type
) {
2545 case LTTNG_CONSUMER_KERNEL
:
2546 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
2547 case LTTNG_CONSUMER32_UST
:
2548 case LTTNG_CONSUMER64_UST
:
2549 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2551 ERR("Unknown consumer_data type");
2557 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2559 switch (consumer_data
.type
) {
2560 case LTTNG_CONSUMER_KERNEL
:
2561 return lttng_kconsumer_on_recv_stream(stream
);
2562 case LTTNG_CONSUMER32_UST
:
2563 case LTTNG_CONSUMER64_UST
:
2564 return lttng_ustconsumer_on_recv_stream(stream
);
2566 ERR("Unknown consumer_data type");
2573 * Allocate and set consumer data hash tables.
2575 void lttng_consumer_init(void)
2577 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2578 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2579 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2581 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2582 assert(metadata_ht
);
2583 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2588 * Process the ADD_RELAYD command receive by a consumer.
2590 * This will create a relayd socket pair and add it to the relayd hash table.
2591 * The caller MUST acquire a RCU read side lock before calling it.
2593 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
2594 struct lttng_consumer_local_data
*ctx
, int sock
,
2595 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
)
2598 struct consumer_relayd_sock_pair
*relayd
;
2600 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
2602 /* Get relayd reference if exists. */
2603 relayd
= consumer_find_relayd(net_seq_idx
);
2604 if (relayd
== NULL
) {
2605 /* Not found. Allocate one. */
2606 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
2607 if (relayd
== NULL
) {
2608 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
2613 /* Poll on consumer socket. */
2614 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2619 /* Get relayd socket from session daemon */
2620 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2621 if (ret
!= sizeof(fd
)) {
2622 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
2627 /* Copy socket information and received FD */
2628 switch (sock_type
) {
2629 case LTTNG_STREAM_CONTROL
:
2630 /* Copy received lttcomm socket */
2631 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
2632 ret
= lttcomm_create_sock(&relayd
->control_sock
);
2637 /* Close the created socket fd which is useless */
2638 close(relayd
->control_sock
.fd
);
2640 /* Assign new file descriptor */
2641 relayd
->control_sock
.fd
= fd
;
2643 case LTTNG_STREAM_DATA
:
2644 /* Copy received lttcomm socket */
2645 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
2646 ret
= lttcomm_create_sock(&relayd
->data_sock
);
2651 /* Close the created socket fd which is useless */
2652 close(relayd
->data_sock
.fd
);
2654 /* Assign new file descriptor */
2655 relayd
->data_sock
.fd
= fd
;
2658 ERR("Unknown relayd socket type (%d)", sock_type
);
2662 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2663 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
2664 relayd
->net_seq_idx
, fd
);
2667 * Add relayd socket pair to consumer data hashtable. If object already
2668 * exists or on error, the function gracefully returns.
2680 * Try to lock the stream mutex.
2682 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
2684 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
2691 * Try to lock the stream mutex. On failure, we know that the stream is
2692 * being used else where hence there is data still being extracted.
2694 ret
= pthread_mutex_trylock(&stream
->lock
);
2696 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
2708 * Check if for a given session id there is still data needed to be extract
2711 * Return 1 if data is pending or else 0 meaning ready to be read.
2713 int consumer_data_pending(uint64_t id
)
2716 struct lttng_ht_iter iter
;
2717 struct lttng_ht
*ht
;
2718 struct lttng_consumer_stream
*stream
;
2719 struct consumer_relayd_sock_pair
*relayd
;
2720 int (*data_pending
)(struct lttng_consumer_stream
*);
2722 DBG("Consumer data pending command on session id %" PRIu64
, id
);
2725 pthread_mutex_lock(&consumer_data
.lock
);
2727 switch (consumer_data
.type
) {
2728 case LTTNG_CONSUMER_KERNEL
:
2729 data_pending
= lttng_kconsumer_data_pending
;
2731 case LTTNG_CONSUMER32_UST
:
2732 case LTTNG_CONSUMER64_UST
:
2733 data_pending
= lttng_ustconsumer_data_pending
;
2736 ERR("Unknown consumer data type");
2740 /* Ease our life a bit */
2741 ht
= consumer_data
.stream_list_ht
;
2743 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2744 ht
->hash_fct((void *)((unsigned long) id
), lttng_ht_seed
),
2745 ht
->match_fct
, (void *)((unsigned long) id
),
2746 &iter
.iter
, stream
, node_session_id
.node
) {
2747 /* If this call fails, the stream is being used hence data pending. */
2748 ret
= stream_try_lock(stream
);
2750 goto data_not_pending
;
2754 * A removed node from the hash table indicates that the stream has
2755 * been deleted thus having a guarantee that the buffers are closed
2756 * on the consumer side. However, data can still be transmitted
2757 * over the network so don't skip the relayd check.
2759 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
2761 /* Check the stream if there is data in the buffers. */
2762 ret
= data_pending(stream
);
2764 pthread_mutex_unlock(&stream
->lock
);
2765 goto data_not_pending
;
2770 if (stream
->net_seq_idx
!= -1) {
2771 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
2774 * At this point, if the relayd object is not available for the
2775 * given stream, it is because the relayd is being cleaned up
2776 * so every stream associated with it (for a session id value)
2777 * are or will be marked for deletion hence no data pending.
2779 pthread_mutex_unlock(&stream
->lock
);
2780 goto data_not_pending
;
2783 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
2784 if (stream
->metadata_flag
) {
2785 ret
= relayd_quiescent_control(&relayd
->control_sock
);
2787 ret
= relayd_data_pending(&relayd
->control_sock
,
2788 stream
->relayd_stream_id
, stream
->next_net_seq_num
);
2790 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2792 pthread_mutex_unlock(&stream
->lock
);
2793 goto data_not_pending
;
2796 pthread_mutex_unlock(&stream
->lock
);
2800 * Finding _no_ node in the hash table means that the stream(s) have been
2801 * removed thus data is guaranteed to be available for analysis from the
2802 * trace files. This is *only* true for local consumer and not network
2806 /* Data is available to be read by a viewer. */
2807 pthread_mutex_unlock(&consumer_data
.lock
);
2812 /* Data is still being extracted from buffers. */
2813 pthread_mutex_unlock(&consumer_data
.lock
);