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>
33 #include <common/common.h>
34 #include <common/utils.h>
35 #include <common/compat/poll.h>
36 #include <common/kernel-ctl/kernel-ctl.h>
37 #include <common/sessiond-comm/relayd.h>
38 #include <common/sessiond-comm/sessiond-comm.h>
39 #include <common/kernel-consumer/kernel-consumer.h>
40 #include <common/relayd/relayd.h>
41 #include <common/ust-consumer/ust-consumer.h>
45 struct lttng_consumer_global_data consumer_data
= {
48 .type
= LTTNG_CONSUMER_UNKNOWN
,
51 enum consumer_channel_action
{
54 CONSUMER_CHANNEL_QUIT
,
57 struct consumer_channel_msg
{
58 enum consumer_channel_action action
;
59 struct lttng_consumer_channel
*chan
; /* add */
60 uint64_t key
; /* del */
64 * Flag to inform the polling thread to quit when all fd hung up. Updated by
65 * the consumer_thread_receive_fds when it notices that all fds has hung up.
66 * Also updated by the signal handler (consumer_should_exit()). Read by the
69 volatile int consumer_quit
;
72 * Global hash table containing respectively metadata and data streams. The
73 * stream element in this ht should only be updated by the metadata poll thread
74 * for the metadata and the data poll thread for the data.
76 static struct lttng_ht
*metadata_ht
;
77 static struct lttng_ht
*data_ht
;
80 * Notify a thread lttng pipe to poll back again. This usually means that some
81 * global state has changed so we just send back the thread in a poll wait
84 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
86 struct lttng_consumer_stream
*null_stream
= NULL
;
90 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
93 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
94 struct lttng_consumer_channel
*chan
,
96 enum consumer_channel_action action
)
98 struct consumer_channel_msg msg
;
101 memset(&msg
, 0, sizeof(msg
));
107 ret
= write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
108 } while (ret
< 0 && errno
== EINTR
);
111 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
114 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
117 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
118 struct lttng_consumer_channel
**chan
,
120 enum consumer_channel_action
*action
)
122 struct consumer_channel_msg msg
;
126 ret
= read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
127 } while (ret
< 0 && errno
== EINTR
);
129 *action
= msg
.action
;
137 * Find a stream. The consumer_data.lock must be locked during this
140 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
143 struct lttng_ht_iter iter
;
144 struct lttng_ht_node_u64
*node
;
145 struct lttng_consumer_stream
*stream
= NULL
;
149 /* -1ULL keys are lookup failures */
150 if (key
== (uint64_t) -1ULL) {
156 lttng_ht_lookup(ht
, &key
, &iter
);
157 node
= lttng_ht_iter_get_node_u64(&iter
);
159 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
167 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
169 struct lttng_consumer_stream
*stream
;
172 stream
= find_stream(key
, ht
);
174 stream
->key
= (uint64_t) -1ULL;
176 * We don't want the lookup to match, but we still need
177 * to iterate on this stream when iterating over the hash table. Just
178 * change the node key.
180 stream
->node
.key
= (uint64_t) -1ULL;
186 * Return a channel object for the given key.
188 * RCU read side lock MUST be acquired before calling this function and
189 * protects the channel ptr.
191 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
193 struct lttng_ht_iter iter
;
194 struct lttng_ht_node_u64
*node
;
195 struct lttng_consumer_channel
*channel
= NULL
;
197 /* -1ULL keys are lookup failures */
198 if (key
== (uint64_t) -1ULL) {
202 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
203 node
= lttng_ht_iter_get_node_u64(&iter
);
205 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
211 static void free_stream_rcu(struct rcu_head
*head
)
213 struct lttng_ht_node_u64
*node
=
214 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
215 struct lttng_consumer_stream
*stream
=
216 caa_container_of(node
, struct lttng_consumer_stream
, node
);
221 static void free_channel_rcu(struct rcu_head
*head
)
223 struct lttng_ht_node_u64
*node
=
224 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
225 struct lttng_consumer_channel
*channel
=
226 caa_container_of(node
, struct lttng_consumer_channel
, node
);
232 * RCU protected relayd socket pair free.
234 static void free_relayd_rcu(struct rcu_head
*head
)
236 struct lttng_ht_node_u64
*node
=
237 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
238 struct consumer_relayd_sock_pair
*relayd
=
239 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
242 * Close all sockets. This is done in the call RCU since we don't want the
243 * socket fds to be reassigned thus potentially creating bad state of the
246 * We do not have to lock the control socket mutex here since at this stage
247 * there is no one referencing to this relayd object.
249 (void) relayd_close(&relayd
->control_sock
);
250 (void) relayd_close(&relayd
->data_sock
);
256 * Destroy and free relayd socket pair object.
258 * This function MUST be called with the consumer_data lock acquired.
260 static void destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
263 struct lttng_ht_iter iter
;
265 if (relayd
== NULL
) {
269 DBG("Consumer destroy and close relayd socket pair");
271 iter
.iter
.node
= &relayd
->node
.node
;
272 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
274 /* We assume the relayd is being or is destroyed */
278 /* RCU free() call */
279 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
283 * Remove a channel from the global list protected by a mutex. This function is
284 * also responsible for freeing its data structures.
286 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
289 struct lttng_ht_iter iter
;
290 struct lttng_consumer_stream
*stream
, *stmp
;
292 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
294 pthread_mutex_lock(&consumer_data
.lock
);
295 pthread_mutex_lock(&channel
->lock
);
297 switch (consumer_data
.type
) {
298 case LTTNG_CONSUMER_KERNEL
:
300 case LTTNG_CONSUMER32_UST
:
301 case LTTNG_CONSUMER64_UST
:
302 /* Delete streams that might have been left in the stream list. */
303 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
305 cds_list_del(&stream
->send_node
);
306 lttng_ustconsumer_del_stream(stream
);
309 lttng_ustconsumer_del_channel(channel
);
312 ERR("Unknown consumer_data type");
318 iter
.iter
.node
= &channel
->node
.node
;
319 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
323 call_rcu(&channel
->node
.head
, free_channel_rcu
);
325 pthread_mutex_unlock(&channel
->lock
);
326 pthread_mutex_unlock(&consumer_data
.lock
);
330 * Iterate over the relayd hash table and destroy each element. Finally,
331 * destroy the whole hash table.
333 static void cleanup_relayd_ht(void)
335 struct lttng_ht_iter iter
;
336 struct consumer_relayd_sock_pair
*relayd
;
340 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
342 destroy_relayd(relayd
);
347 lttng_ht_destroy(consumer_data
.relayd_ht
);
351 * Update the end point status of all streams having the given network sequence
352 * index (relayd index).
354 * It's atomically set without having the stream mutex locked which is fine
355 * because we handle the write/read race with a pipe wakeup for each thread.
357 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
358 enum consumer_endpoint_status status
)
360 struct lttng_ht_iter iter
;
361 struct lttng_consumer_stream
*stream
;
363 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
367 /* Let's begin with metadata */
368 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
369 if (stream
->net_seq_idx
== net_seq_idx
) {
370 uatomic_set(&stream
->endpoint_status
, status
);
371 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
375 /* Follow up by the data streams */
376 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
377 if (stream
->net_seq_idx
== net_seq_idx
) {
378 uatomic_set(&stream
->endpoint_status
, status
);
379 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
386 * Cleanup a relayd object by flagging every associated streams for deletion,
387 * destroying the object meaning removing it from the relayd hash table,
388 * closing the sockets and freeing the memory in a RCU call.
390 * If a local data context is available, notify the threads that the streams'
391 * state have changed.
393 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
394 struct lttng_consumer_local_data
*ctx
)
400 DBG("Cleaning up relayd sockets");
402 /* Save the net sequence index before destroying the object */
403 netidx
= relayd
->net_seq_idx
;
406 * Delete the relayd from the relayd hash table, close the sockets and free
407 * the object in a RCU call.
409 destroy_relayd(relayd
);
411 /* Set inactive endpoint to all streams */
412 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
415 * With a local data context, notify the threads that the streams' state
416 * have changed. The write() action on the pipe acts as an "implicit"
417 * memory barrier ordering the updates of the end point status from the
418 * read of this status which happens AFTER receiving this notify.
421 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
422 notify_thread_lttng_pipe(ctx
->consumer_metadata_pipe
);
427 * Flag a relayd socket pair for destruction. Destroy it if the refcount
430 * RCU read side lock MUST be aquired before calling this function.
432 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
436 /* Set destroy flag for this object */
437 uatomic_set(&relayd
->destroy_flag
, 1);
439 /* Destroy the relayd if refcount is 0 */
440 if (uatomic_read(&relayd
->refcount
) == 0) {
441 destroy_relayd(relayd
);
446 * Remove a stream from the global list protected by a mutex. This
447 * function is also responsible for freeing its data structures.
449 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
453 struct lttng_ht_iter iter
;
454 struct lttng_consumer_channel
*free_chan
= NULL
;
455 struct consumer_relayd_sock_pair
*relayd
;
459 DBG("Consumer del stream %d", stream
->wait_fd
);
462 /* Means the stream was allocated but not successfully added */
463 goto free_stream_rcu
;
466 pthread_mutex_lock(&consumer_data
.lock
);
467 pthread_mutex_lock(&stream
->lock
);
469 switch (consumer_data
.type
) {
470 case LTTNG_CONSUMER_KERNEL
:
471 if (stream
->mmap_base
!= NULL
) {
472 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
478 if (stream
->wait_fd
>= 0) {
479 ret
= close(stream
->wait_fd
);
485 case LTTNG_CONSUMER32_UST
:
486 case LTTNG_CONSUMER64_UST
:
487 lttng_ustconsumer_del_stream(stream
);
490 ERR("Unknown consumer_data type");
496 iter
.iter
.node
= &stream
->node
.node
;
497 ret
= lttng_ht_del(ht
, &iter
);
500 iter
.iter
.node
= &stream
->node_channel_id
.node
;
501 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
504 iter
.iter
.node
= &stream
->node_session_id
.node
;
505 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
509 assert(consumer_data
.stream_count
> 0);
510 consumer_data
.stream_count
--;
512 if (stream
->out_fd
>= 0) {
513 ret
= close(stream
->out_fd
);
519 /* Check and cleanup relayd */
521 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
522 if (relayd
!= NULL
) {
523 uatomic_dec(&relayd
->refcount
);
524 assert(uatomic_read(&relayd
->refcount
) >= 0);
526 /* Closing streams requires to lock the control socket. */
527 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
528 ret
= relayd_send_close_stream(&relayd
->control_sock
,
529 stream
->relayd_stream_id
,
530 stream
->next_net_seq_num
- 1);
531 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
533 DBG("Unable to close stream on the relayd. Continuing");
535 * Continue here. There is nothing we can do for the relayd.
536 * Chances are that the relayd has closed the socket so we just
537 * continue cleaning up.
541 /* Both conditions are met, we destroy the relayd. */
542 if (uatomic_read(&relayd
->refcount
) == 0 &&
543 uatomic_read(&relayd
->destroy_flag
)) {
544 destroy_relayd(relayd
);
549 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
550 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
551 free_chan
= stream
->chan
;
555 consumer_data
.need_update
= 1;
556 pthread_mutex_unlock(&stream
->lock
);
557 pthread_mutex_unlock(&consumer_data
.lock
);
560 consumer_del_channel(free_chan
);
564 call_rcu(&stream
->node
.head
, free_stream_rcu
);
567 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
569 enum lttng_consumer_stream_state state
,
570 const char *channel_name
,
577 enum consumer_channel_type type
)
580 struct lttng_consumer_stream
*stream
;
582 stream
= zmalloc(sizeof(*stream
));
583 if (stream
== NULL
) {
584 PERROR("malloc struct lttng_consumer_stream");
591 stream
->key
= stream_key
;
593 stream
->out_fd_offset
= 0;
594 stream
->state
= state
;
597 stream
->net_seq_idx
= relayd_id
;
598 stream
->session_id
= session_id
;
599 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
600 pthread_mutex_init(&stream
->lock
, NULL
);
602 /* If channel is the metadata, flag this stream as metadata. */
603 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
604 stream
->metadata_flag
= 1;
605 /* Metadata is flat out. */
606 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
608 /* Format stream name to <channel_name>_<cpu_number> */
609 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
612 PERROR("snprintf stream name");
617 /* Key is always the wait_fd for streams. */
618 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
620 /* Init node per channel id key */
621 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
623 /* Init session id node with the stream session id */
624 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
626 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
" relayd_id %" PRIu64
", session_id %" PRIu64
,
627 stream
->name
, stream
->key
, channel_key
, stream
->net_seq_idx
, stream
->session_id
);
643 * Add a stream to the global list protected by a mutex.
645 static int add_stream(struct lttng_consumer_stream
*stream
,
649 struct consumer_relayd_sock_pair
*relayd
;
654 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
656 pthread_mutex_lock(&consumer_data
.lock
);
657 pthread_mutex_lock(&stream
->chan
->lock
);
658 pthread_mutex_lock(&stream
->chan
->timer_lock
);
659 pthread_mutex_lock(&stream
->lock
);
662 /* Steal stream identifier to avoid having streams with the same key */
663 steal_stream_key(stream
->key
, ht
);
665 lttng_ht_add_unique_u64(ht
, &stream
->node
);
667 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
668 &stream
->node_channel_id
);
671 * Add stream to the stream_list_ht of the consumer data. No need to steal
672 * the key since the HT does not use it and we allow to add redundant keys
675 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
677 /* Check and cleanup relayd */
678 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
679 if (relayd
!= NULL
) {
680 uatomic_inc(&relayd
->refcount
);
684 * When nb_init_stream_left reaches 0, we don't need to trigger any action
685 * in terms of destroying the associated channel, because the action that
686 * causes the count to become 0 also causes a stream to be added. The
687 * channel deletion will thus be triggered by the following removal of this
690 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
691 /* Increment refcount before decrementing nb_init_stream_left */
693 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
696 /* Update consumer data once the node is inserted. */
697 consumer_data
.stream_count
++;
698 consumer_data
.need_update
= 1;
701 pthread_mutex_unlock(&stream
->lock
);
702 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
703 pthread_mutex_unlock(&stream
->chan
->lock
);
704 pthread_mutex_unlock(&consumer_data
.lock
);
710 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
711 * be acquired before calling this.
713 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
716 struct lttng_ht_node_u64
*node
;
717 struct lttng_ht_iter iter
;
721 lttng_ht_lookup(consumer_data
.relayd_ht
,
722 &relayd
->net_seq_idx
, &iter
);
723 node
= lttng_ht_iter_get_node_u64(&iter
);
727 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
734 * Allocate and return a consumer relayd socket.
736 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
737 uint64_t net_seq_idx
)
739 struct consumer_relayd_sock_pair
*obj
= NULL
;
741 /* net sequence index of -1 is a failure */
742 if (net_seq_idx
== (uint64_t) -1ULL) {
746 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
748 PERROR("zmalloc relayd sock");
752 obj
->net_seq_idx
= net_seq_idx
;
754 obj
->destroy_flag
= 0;
755 obj
->control_sock
.sock
.fd
= -1;
756 obj
->data_sock
.sock
.fd
= -1;
757 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
758 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
765 * Find a relayd socket pair in the global consumer data.
767 * Return the object if found else NULL.
768 * RCU read-side lock must be held across this call and while using the
771 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
773 struct lttng_ht_iter iter
;
774 struct lttng_ht_node_u64
*node
;
775 struct consumer_relayd_sock_pair
*relayd
= NULL
;
777 /* Negative keys are lookup failures */
778 if (key
== (uint64_t) -1ULL) {
782 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
784 node
= lttng_ht_iter_get_node_u64(&iter
);
786 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
794 * Handle stream for relayd transmission if the stream applies for network
795 * streaming where the net sequence index is set.
797 * Return destination file descriptor or negative value on error.
799 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
800 size_t data_size
, unsigned long padding
,
801 struct consumer_relayd_sock_pair
*relayd
)
804 struct lttcomm_relayd_data_hdr data_hdr
;
810 /* Reset data header */
811 memset(&data_hdr
, 0, sizeof(data_hdr
));
813 if (stream
->metadata_flag
) {
814 /* Caller MUST acquire the relayd control socket lock */
815 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
820 /* Metadata are always sent on the control socket. */
821 outfd
= relayd
->control_sock
.sock
.fd
;
823 /* Set header with stream information */
824 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
825 data_hdr
.data_size
= htobe32(data_size
);
826 data_hdr
.padding_size
= htobe32(padding
);
828 * Note that net_seq_num below is assigned with the *current* value of
829 * next_net_seq_num and only after that the next_net_seq_num will be
830 * increment. This is why when issuing a command on the relayd using
831 * this next value, 1 should always be substracted in order to compare
832 * the last seen sequence number on the relayd side to the last sent.
834 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
835 /* Other fields are zeroed previously */
837 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
843 ++stream
->next_net_seq_num
;
845 /* Set to go on data socket */
846 outfd
= relayd
->data_sock
.sock
.fd
;
854 * Allocate and return a new lttng_consumer_channel object using the given key
855 * to initialize the hash table node.
857 * On error, return NULL.
859 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
861 const char *pathname
,
866 enum lttng_event_output output
,
867 uint64_t tracefile_size
,
868 uint64_t tracefile_count
,
869 uint64_t session_id_per_pid
)
871 struct lttng_consumer_channel
*channel
;
873 channel
= zmalloc(sizeof(*channel
));
874 if (channel
== NULL
) {
875 PERROR("malloc struct lttng_consumer_channel");
880 channel
->refcount
= 0;
881 channel
->session_id
= session_id
;
882 channel
->session_id_per_pid
= session_id_per_pid
;
885 channel
->relayd_id
= relayd_id
;
886 channel
->output
= output
;
887 channel
->tracefile_size
= tracefile_size
;
888 channel
->tracefile_count
= tracefile_count
;
889 pthread_mutex_init(&channel
->lock
, NULL
);
890 pthread_mutex_init(&channel
->timer_lock
, NULL
);
892 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
893 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
895 strncpy(channel
->name
, name
, sizeof(channel
->name
));
896 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
898 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
900 channel
->wait_fd
= -1;
902 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
904 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
911 * Add a channel to the global list protected by a mutex.
913 * On success 0 is returned else a negative value.
915 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
916 struct lttng_consumer_local_data
*ctx
)
919 struct lttng_ht_node_u64
*node
;
920 struct lttng_ht_iter iter
;
922 pthread_mutex_lock(&consumer_data
.lock
);
923 pthread_mutex_lock(&channel
->lock
);
924 pthread_mutex_lock(&channel
->timer_lock
);
927 lttng_ht_lookup(consumer_data
.channel_ht
, &channel
->key
, &iter
);
928 node
= lttng_ht_iter_get_node_u64(&iter
);
930 /* Channel already exist. Ignore the insertion */
931 ERR("Consumer add channel key %" PRIu64
" already exists!",
937 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
941 pthread_mutex_unlock(&channel
->timer_lock
);
942 pthread_mutex_unlock(&channel
->lock
);
943 pthread_mutex_unlock(&consumer_data
.lock
);
945 if (!ret
&& channel
->wait_fd
!= -1 &&
946 channel
->metadata_stream
== NULL
) {
947 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
953 * Allocate the pollfd structure and the local view of the out fds to avoid
954 * doing a lookup in the linked list and concurrency issues when writing is
955 * needed. Called with consumer_data.lock held.
957 * Returns the number of fds in the structures.
959 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
960 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
964 struct lttng_ht_iter iter
;
965 struct lttng_consumer_stream
*stream
;
970 assert(local_stream
);
972 DBG("Updating poll fd array");
974 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
976 * Only active streams with an active end point can be added to the
977 * poll set and local stream storage of the thread.
979 * There is a potential race here for endpoint_status to be updated
980 * just after the check. However, this is OK since the stream(s) will
981 * be deleted once the thread is notified that the end point state has
982 * changed where this function will be called back again.
984 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
985 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
989 * This clobbers way too much the debug output. Uncomment that if you
990 * need it for debugging purposes.
992 * DBG("Active FD %d", stream->wait_fd);
994 (*pollfd
)[i
].fd
= stream
->wait_fd
;
995 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
996 local_stream
[i
] = stream
;
1002 * Insert the consumer_data_pipe at the end of the array and don't
1003 * increment i so nb_fd is the number of real FD.
1005 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1006 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1011 * Poll on the should_quit pipe and the command socket return -1 on error and
1012 * should exit, 0 if data is available on the command socket
1014 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1019 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1020 if (num_rdy
== -1) {
1022 * Restart interrupted system call.
1024 if (errno
== EINTR
) {
1027 PERROR("Poll error");
1030 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1031 DBG("consumer_should_quit wake up");
1041 * Set the error socket.
1043 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1046 ctx
->consumer_error_socket
= sock
;
1050 * Set the command socket path.
1052 void lttng_consumer_set_command_sock_path(
1053 struct lttng_consumer_local_data
*ctx
, char *sock
)
1055 ctx
->consumer_command_sock_path
= sock
;
1059 * Send return code to the session daemon.
1060 * If the socket is not defined, we return 0, it is not a fatal error
1062 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1064 if (ctx
->consumer_error_socket
> 0) {
1065 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1066 sizeof(enum lttcomm_sessiond_command
));
1073 * Close all the tracefiles and stream fds and MUST be called when all
1074 * instances are destroyed i.e. when all threads were joined and are ended.
1076 void lttng_consumer_cleanup(void)
1078 struct lttng_ht_iter iter
;
1079 struct lttng_consumer_channel
*channel
;
1083 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1085 consumer_del_channel(channel
);
1090 lttng_ht_destroy(consumer_data
.channel_ht
);
1092 cleanup_relayd_ht();
1094 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1097 * This HT contains streams that are freed by either the metadata thread or
1098 * the data thread so we do *nothing* on the hash table and simply destroy
1101 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1105 * Called from signal handler.
1107 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1112 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1113 } while (ret
< 0 && errno
== EINTR
);
1114 if (ret
< 0 || ret
!= 1) {
1115 PERROR("write consumer quit");
1118 DBG("Consumer flag that it should quit");
1121 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1124 int outfd
= stream
->out_fd
;
1127 * This does a blocking write-and-wait on any page that belongs to the
1128 * subbuffer prior to the one we just wrote.
1129 * Don't care about error values, as these are just hints and ways to
1130 * limit the amount of page cache used.
1132 if (orig_offset
< stream
->max_sb_size
) {
1135 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1136 stream
->max_sb_size
,
1137 SYNC_FILE_RANGE_WAIT_BEFORE
1138 | SYNC_FILE_RANGE_WRITE
1139 | SYNC_FILE_RANGE_WAIT_AFTER
);
1141 * Give hints to the kernel about how we access the file:
1142 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1145 * We need to call fadvise again after the file grows because the
1146 * kernel does not seem to apply fadvise to non-existing parts of the
1149 * Call fadvise _after_ having waited for the page writeback to
1150 * complete because the dirty page writeback semantic is not well
1151 * defined. So it can be expected to lead to lower throughput in
1154 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1155 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1159 * Initialise the necessary environnement :
1160 * - create a new context
1161 * - create the poll_pipe
1162 * - create the should_quit pipe (for signal handler)
1163 * - create the thread pipe (for splice)
1165 * Takes a function pointer as argument, this function is called when data is
1166 * available on a buffer. This function is responsible to do the
1167 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1168 * buffer configuration and then kernctl_put_next_subbuf at the end.
1170 * Returns a pointer to the new context or NULL on error.
1172 struct lttng_consumer_local_data
*lttng_consumer_create(
1173 enum lttng_consumer_type type
,
1174 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1175 struct lttng_consumer_local_data
*ctx
),
1176 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1177 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1178 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1181 struct lttng_consumer_local_data
*ctx
;
1183 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1184 consumer_data
.type
== type
);
1185 consumer_data
.type
= type
;
1187 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1189 PERROR("allocating context");
1193 ctx
->consumer_error_socket
= -1;
1194 ctx
->consumer_metadata_socket
= -1;
1195 /* assign the callbacks */
1196 ctx
->on_buffer_ready
= buffer_ready
;
1197 ctx
->on_recv_channel
= recv_channel
;
1198 ctx
->on_recv_stream
= recv_stream
;
1199 ctx
->on_update_stream
= update_stream
;
1201 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1202 if (!ctx
->consumer_data_pipe
) {
1203 goto error_poll_pipe
;
1206 ret
= pipe(ctx
->consumer_should_quit
);
1208 PERROR("Error creating recv pipe");
1209 goto error_quit_pipe
;
1212 ret
= pipe(ctx
->consumer_thread_pipe
);
1214 PERROR("Error creating thread pipe");
1215 goto error_thread_pipe
;
1218 ret
= pipe(ctx
->consumer_channel_pipe
);
1220 PERROR("Error creating channel pipe");
1221 goto error_channel_pipe
;
1224 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1225 if (!ctx
->consumer_metadata_pipe
) {
1226 goto error_metadata_pipe
;
1229 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1231 goto error_splice_pipe
;
1237 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1238 error_metadata_pipe
:
1239 utils_close_pipe(ctx
->consumer_channel_pipe
);
1241 utils_close_pipe(ctx
->consumer_thread_pipe
);
1243 utils_close_pipe(ctx
->consumer_should_quit
);
1245 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1253 * Close all fds associated with the instance and free the context.
1255 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1259 DBG("Consumer destroying it. Closing everything.");
1261 ret
= close(ctx
->consumer_error_socket
);
1265 ret
= close(ctx
->consumer_metadata_socket
);
1269 utils_close_pipe(ctx
->consumer_thread_pipe
);
1270 utils_close_pipe(ctx
->consumer_channel_pipe
);
1271 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1272 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1273 utils_close_pipe(ctx
->consumer_should_quit
);
1274 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1276 unlink(ctx
->consumer_command_sock_path
);
1281 * Write the metadata stream id on the specified file descriptor.
1283 static int write_relayd_metadata_id(int fd
,
1284 struct lttng_consumer_stream
*stream
,
1285 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1288 struct lttcomm_relayd_metadata_payload hdr
;
1290 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1291 hdr
.padding_size
= htobe32(padding
);
1293 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1294 } while (ret
< 0 && errno
== EINTR
);
1295 if (ret
< 0 || ret
!= sizeof(hdr
)) {
1297 * This error means that the fd's end is closed so ignore the perror
1298 * not to clubber the error output since this can happen in a normal
1301 if (errno
!= EPIPE
) {
1302 PERROR("write metadata stream id");
1304 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1306 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1307 * handle writting the missing part so report that as an error and
1308 * don't lie to the caller.
1313 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1314 stream
->relayd_stream_id
, padding
);
1321 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1322 * core function for writing trace buffers to either the local filesystem or
1325 * It must be called with the stream lock held.
1327 * Careful review MUST be put if any changes occur!
1329 * Returns the number of bytes written
1331 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1332 struct lttng_consumer_local_data
*ctx
,
1333 struct lttng_consumer_stream
*stream
, unsigned long len
,
1334 unsigned long padding
)
1336 unsigned long mmap_offset
;
1338 ssize_t ret
= 0, written
= 0;
1339 off_t orig_offset
= stream
->out_fd_offset
;
1340 /* Default is on the disk */
1341 int outfd
= stream
->out_fd
;
1342 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1343 unsigned int relayd_hang_up
= 0;
1345 /* RCU lock for the relayd pointer */
1348 /* Flag that the current stream if set for network streaming. */
1349 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1350 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1351 if (relayd
== NULL
) {
1356 /* get the offset inside the fd to mmap */
1357 switch (consumer_data
.type
) {
1358 case LTTNG_CONSUMER_KERNEL
:
1359 mmap_base
= stream
->mmap_base
;
1360 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1362 case LTTNG_CONSUMER32_UST
:
1363 case LTTNG_CONSUMER64_UST
:
1364 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1366 ERR("read mmap get mmap base for stream %s", stream
->name
);
1370 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1374 ERR("Unknown consumer_data type");
1379 PERROR("tracer ctl get_mmap_read_offset");
1384 /* Handle stream on the relayd if the output is on the network */
1386 unsigned long netlen
= len
;
1389 * Lock the control socket for the complete duration of the function
1390 * since from this point on we will use the socket.
1392 if (stream
->metadata_flag
) {
1393 /* Metadata requires the control socket. */
1394 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1395 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1398 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1400 /* Use the returned socket. */
1403 /* Write metadata stream id before payload */
1404 if (stream
->metadata_flag
) {
1405 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1408 /* Socket operation failed. We consider the relayd dead */
1409 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1417 /* Socket operation failed. We consider the relayd dead */
1418 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1422 /* Else, use the default set before which is the filesystem. */
1425 /* No streaming, we have to set the len with the full padding */
1429 * Check if we need to change the tracefile before writing the packet.
1431 if (stream
->chan
->tracefile_size
> 0 &&
1432 (stream
->tracefile_size_current
+ len
) >
1433 stream
->chan
->tracefile_size
) {
1434 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1435 stream
->name
, stream
->chan
->tracefile_size
,
1436 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1437 stream
->out_fd
, &(stream
->tracefile_count_current
));
1439 ERR("Rotating output file");
1442 outfd
= stream
->out_fd
= ret
;
1443 /* Reset current size because we just perform a rotation. */
1444 stream
->tracefile_size_current
= 0;
1446 stream
->tracefile_size_current
+= len
;
1451 ret
= write(outfd
, mmap_base
+ mmap_offset
, len
);
1452 } while (ret
< 0 && errno
== EINTR
);
1453 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1456 * This is possible if the fd is closed on the other side (outfd)
1457 * or any write problem. It can be verbose a bit for a normal
1458 * execution if for instance the relayd is stopped abruptly. This
1459 * can happen so set this to a DBG statement.
1461 DBG("Error in file write mmap");
1465 /* Socket operation failed. We consider the relayd dead */
1466 if (errno
== EPIPE
|| errno
== EINVAL
) {
1471 } else if (ret
> len
) {
1472 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1480 /* This call is useless on a socket so better save a syscall. */
1482 /* This won't block, but will start writeout asynchronously */
1483 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1484 SYNC_FILE_RANGE_WRITE
);
1485 stream
->out_fd_offset
+= ret
;
1489 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1493 * This is a special case that the relayd has closed its socket. Let's
1494 * cleanup the relayd object and all associated streams.
1496 if (relayd
&& relayd_hang_up
) {
1497 cleanup_relayd(relayd
, ctx
);
1501 /* Unlock only if ctrl socket used */
1502 if (relayd
&& stream
->metadata_flag
) {
1503 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1511 * Splice the data from the ring buffer to the tracefile.
1513 * It must be called with the stream lock held.
1515 * Returns the number of bytes spliced.
1517 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1518 struct lttng_consumer_local_data
*ctx
,
1519 struct lttng_consumer_stream
*stream
, unsigned long len
,
1520 unsigned long padding
)
1522 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1524 off_t orig_offset
= stream
->out_fd_offset
;
1525 int fd
= stream
->wait_fd
;
1526 /* Default is on the disk */
1527 int outfd
= stream
->out_fd
;
1528 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1530 unsigned int relayd_hang_up
= 0;
1532 switch (consumer_data
.type
) {
1533 case LTTNG_CONSUMER_KERNEL
:
1535 case LTTNG_CONSUMER32_UST
:
1536 case LTTNG_CONSUMER64_UST
:
1537 /* Not supported for user space tracing */
1540 ERR("Unknown consumer_data type");
1544 /* RCU lock for the relayd pointer */
1547 /* Flag that the current stream if set for network streaming. */
1548 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1549 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1550 if (relayd
== NULL
) {
1556 * Choose right pipe for splice. Metadata and trace data are handled by
1557 * different threads hence the use of two pipes in order not to race or
1558 * corrupt the written data.
1560 if (stream
->metadata_flag
) {
1561 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1563 splice_pipe
= ctx
->consumer_thread_pipe
;
1566 /* Write metadata stream id before payload */
1568 int total_len
= len
;
1570 if (stream
->metadata_flag
) {
1572 * Lock the control socket for the complete duration of the function
1573 * since from this point on we will use the socket.
1575 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1577 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1581 /* Socket operation failed. We consider the relayd dead */
1582 if (ret
== -EBADF
) {
1583 WARN("Remote relayd disconnected. Stopping");
1590 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1593 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1595 /* Use the returned socket. */
1598 /* Socket operation failed. We consider the relayd dead */
1599 if (ret
== -EBADF
) {
1600 WARN("Remote relayd disconnected. Stopping");
1607 /* No streaming, we have to set the len with the full padding */
1611 * Check if we need to change the tracefile before writing the packet.
1613 if (stream
->chan
->tracefile_size
> 0 &&
1614 (stream
->tracefile_size_current
+ len
) >
1615 stream
->chan
->tracefile_size
) {
1616 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1617 stream
->name
, stream
->chan
->tracefile_size
,
1618 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1619 stream
->out_fd
, &(stream
->tracefile_count_current
));
1621 ERR("Rotating output file");
1624 outfd
= stream
->out_fd
= ret
;
1625 /* Reset current size because we just perform a rotation. */
1626 stream
->tracefile_size_current
= 0;
1628 stream
->tracefile_size_current
+= len
;
1632 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1633 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1634 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1635 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1636 DBG("splice chan to pipe, ret %zd", ret_splice
);
1637 if (ret_splice
< 0) {
1638 PERROR("Error in relay splice");
1640 written
= ret_splice
;
1646 /* Handle stream on the relayd if the output is on the network */
1648 if (stream
->metadata_flag
) {
1649 size_t metadata_payload_size
=
1650 sizeof(struct lttcomm_relayd_metadata_payload
);
1652 /* Update counter to fit the spliced data */
1653 ret_splice
+= metadata_payload_size
;
1654 len
+= metadata_payload_size
;
1656 * We do this so the return value can match the len passed as
1657 * argument to this function.
1659 written
-= metadata_payload_size
;
1663 /* Splice data out */
1664 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1665 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1666 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1667 if (ret_splice
< 0) {
1668 PERROR("Error in file splice");
1670 written
= ret_splice
;
1672 /* Socket operation failed. We consider the relayd dead */
1673 if (errno
== EBADF
|| errno
== EPIPE
) {
1674 WARN("Remote relayd disconnected. Stopping");
1680 } else if (ret_splice
> len
) {
1682 PERROR("Wrote more data than requested %zd (len: %lu)",
1684 written
+= ret_splice
;
1690 /* This call is useless on a socket so better save a syscall. */
1692 /* This won't block, but will start writeout asynchronously */
1693 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1694 SYNC_FILE_RANGE_WRITE
);
1695 stream
->out_fd_offset
+= ret_splice
;
1697 written
+= ret_splice
;
1699 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1707 * This is a special case that the relayd has closed its socket. Let's
1708 * cleanup the relayd object and all associated streams.
1710 if (relayd
&& relayd_hang_up
) {
1711 cleanup_relayd(relayd
, ctx
);
1712 /* Skip splice error so the consumer does not fail */
1717 /* send the appropriate error description to sessiond */
1720 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1723 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1726 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1731 if (relayd
&& stream
->metadata_flag
) {
1732 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1740 * Take a snapshot for a specific fd
1742 * Returns 0 on success, < 0 on error
1744 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1746 switch (consumer_data
.type
) {
1747 case LTTNG_CONSUMER_KERNEL
:
1748 return lttng_kconsumer_take_snapshot(stream
);
1749 case LTTNG_CONSUMER32_UST
:
1750 case LTTNG_CONSUMER64_UST
:
1751 return lttng_ustconsumer_take_snapshot(stream
);
1753 ERR("Unknown consumer_data type");
1760 * Get the produced position
1762 * Returns 0 on success, < 0 on error
1764 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1767 switch (consumer_data
.type
) {
1768 case LTTNG_CONSUMER_KERNEL
:
1769 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1770 case LTTNG_CONSUMER32_UST
:
1771 case LTTNG_CONSUMER64_UST
:
1772 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1774 ERR("Unknown consumer_data type");
1780 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1781 int sock
, struct pollfd
*consumer_sockpoll
)
1783 switch (consumer_data
.type
) {
1784 case LTTNG_CONSUMER_KERNEL
:
1785 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1786 case LTTNG_CONSUMER32_UST
:
1787 case LTTNG_CONSUMER64_UST
:
1788 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1790 ERR("Unknown consumer_data type");
1797 * Iterate over all streams of the hashtable and free them properly.
1799 * WARNING: *MUST* be used with data stream only.
1801 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1803 struct lttng_ht_iter iter
;
1804 struct lttng_consumer_stream
*stream
;
1811 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1813 * Ignore return value since we are currently cleaning up so any error
1816 (void) consumer_del_stream(stream
, ht
);
1820 lttng_ht_destroy(ht
);
1824 * Iterate over all streams of the hashtable and free them properly.
1826 * XXX: Should not be only for metadata stream or else use an other name.
1828 static void destroy_stream_ht(struct lttng_ht
*ht
)
1830 struct lttng_ht_iter iter
;
1831 struct lttng_consumer_stream
*stream
;
1838 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1840 * Ignore return value since we are currently cleaning up so any error
1843 (void) consumer_del_metadata_stream(stream
, ht
);
1847 lttng_ht_destroy(ht
);
1850 void lttng_consumer_close_metadata(void)
1852 switch (consumer_data
.type
) {
1853 case LTTNG_CONSUMER_KERNEL
:
1855 * The Kernel consumer has a different metadata scheme so we don't
1856 * close anything because the stream will be closed by the session
1860 case LTTNG_CONSUMER32_UST
:
1861 case LTTNG_CONSUMER64_UST
:
1863 * Close all metadata streams. The metadata hash table is passed and
1864 * this call iterates over it by closing all wakeup fd. This is safe
1865 * because at this point we are sure that the metadata producer is
1866 * either dead or blocked.
1868 lttng_ustconsumer_close_metadata(metadata_ht
);
1871 ERR("Unknown consumer_data type");
1877 * Clean up a metadata stream and free its memory.
1879 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1880 struct lttng_ht
*ht
)
1883 struct lttng_ht_iter iter
;
1884 struct lttng_consumer_channel
*free_chan
= NULL
;
1885 struct consumer_relayd_sock_pair
*relayd
;
1889 * This call should NEVER receive regular stream. It must always be
1890 * metadata stream and this is crucial for data structure synchronization.
1892 assert(stream
->metadata_flag
);
1894 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1897 /* Means the stream was allocated but not successfully added */
1898 goto free_stream_rcu
;
1901 pthread_mutex_lock(&consumer_data
.lock
);
1902 pthread_mutex_lock(&stream
->chan
->lock
);
1903 pthread_mutex_lock(&stream
->lock
);
1905 switch (consumer_data
.type
) {
1906 case LTTNG_CONSUMER_KERNEL
:
1907 if (stream
->mmap_base
!= NULL
) {
1908 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1910 PERROR("munmap metadata stream");
1914 if (stream
->wait_fd
>= 0) {
1915 ret
= close(stream
->wait_fd
);
1917 PERROR("close kernel metadata wait_fd");
1921 case LTTNG_CONSUMER32_UST
:
1922 case LTTNG_CONSUMER64_UST
:
1923 lttng_ustconsumer_del_stream(stream
);
1926 ERR("Unknown consumer_data type");
1932 iter
.iter
.node
= &stream
->node
.node
;
1933 ret
= lttng_ht_del(ht
, &iter
);
1936 iter
.iter
.node
= &stream
->node_channel_id
.node
;
1937 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
1940 iter
.iter
.node
= &stream
->node_session_id
.node
;
1941 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1945 if (stream
->out_fd
>= 0) {
1946 ret
= close(stream
->out_fd
);
1952 /* Check and cleanup relayd */
1954 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1955 if (relayd
!= NULL
) {
1956 uatomic_dec(&relayd
->refcount
);
1957 assert(uatomic_read(&relayd
->refcount
) >= 0);
1959 /* Closing streams requires to lock the control socket. */
1960 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1961 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1962 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1963 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1965 DBG("Unable to close stream on the relayd. Continuing");
1967 * Continue here. There is nothing we can do for the relayd.
1968 * Chances are that the relayd has closed the socket so we just
1969 * continue cleaning up.
1973 /* Both conditions are met, we destroy the relayd. */
1974 if (uatomic_read(&relayd
->refcount
) == 0 &&
1975 uatomic_read(&relayd
->destroy_flag
)) {
1976 destroy_relayd(relayd
);
1981 /* Atomically decrement channel refcount since other threads can use it. */
1982 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
1983 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
1984 /* Go for channel deletion! */
1985 free_chan
= stream
->chan
;
1990 * Nullify the stream reference so it is not used after deletion. The
1991 * channel lock MUST be acquired before being able to check for
1992 * a NULL pointer value.
1994 stream
->chan
->metadata_stream
= NULL
;
1996 pthread_mutex_unlock(&stream
->lock
);
1997 pthread_mutex_unlock(&stream
->chan
->lock
);
1998 pthread_mutex_unlock(&consumer_data
.lock
);
2001 consumer_del_channel(free_chan
);
2005 call_rcu(&stream
->node
.head
, free_stream_rcu
);
2009 * Action done with the metadata stream when adding it to the consumer internal
2010 * data structures to handle it.
2012 static int add_metadata_stream(struct lttng_consumer_stream
*stream
,
2013 struct lttng_ht
*ht
)
2016 struct consumer_relayd_sock_pair
*relayd
;
2017 struct lttng_ht_iter iter
;
2018 struct lttng_ht_node_u64
*node
;
2023 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2025 pthread_mutex_lock(&consumer_data
.lock
);
2026 pthread_mutex_lock(&stream
->chan
->lock
);
2027 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2028 pthread_mutex_lock(&stream
->lock
);
2031 * From here, refcounts are updated so be _careful_ when returning an error
2038 * Lookup the stream just to make sure it does not exist in our internal
2039 * state. This should NEVER happen.
2041 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2042 node
= lttng_ht_iter_get_node_u64(&iter
);
2045 /* Find relayd and, if one is found, increment refcount. */
2046 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
2047 if (relayd
!= NULL
) {
2048 uatomic_inc(&relayd
->refcount
);
2052 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2053 * in terms of destroying the associated channel, because the action that
2054 * causes the count to become 0 also causes a stream to be added. The
2055 * channel deletion will thus be triggered by the following removal of this
2058 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2059 /* Increment refcount before decrementing nb_init_stream_left */
2061 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2064 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2066 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2067 &stream
->node_channel_id
);
2070 * Add stream to the stream_list_ht of the consumer data. No need to steal
2071 * the key since the HT does not use it and we allow to add redundant keys
2074 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2078 pthread_mutex_unlock(&stream
->lock
);
2079 pthread_mutex_unlock(&stream
->chan
->lock
);
2080 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2081 pthread_mutex_unlock(&consumer_data
.lock
);
2086 * Delete data stream that are flagged for deletion (endpoint_status).
2088 static void validate_endpoint_status_data_stream(void)
2090 struct lttng_ht_iter iter
;
2091 struct lttng_consumer_stream
*stream
;
2093 DBG("Consumer delete flagged data stream");
2096 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2097 /* Validate delete flag of the stream */
2098 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2101 /* Delete it right now */
2102 consumer_del_stream(stream
, data_ht
);
2108 * Delete metadata stream that are flagged for deletion (endpoint_status).
2110 static void validate_endpoint_status_metadata_stream(
2111 struct lttng_poll_event
*pollset
)
2113 struct lttng_ht_iter iter
;
2114 struct lttng_consumer_stream
*stream
;
2116 DBG("Consumer delete flagged metadata stream");
2121 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2122 /* Validate delete flag of the stream */
2123 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2127 * Remove from pollset so the metadata thread can continue without
2128 * blocking on a deleted stream.
2130 lttng_poll_del(pollset
, stream
->wait_fd
);
2132 /* Delete it right now */
2133 consumer_del_metadata_stream(stream
, metadata_ht
);
2139 * Thread polls on metadata file descriptor and write them on disk or on the
2142 void *consumer_thread_metadata_poll(void *data
)
2145 uint32_t revents
, nb_fd
;
2146 struct lttng_consumer_stream
*stream
= NULL
;
2147 struct lttng_ht_iter iter
;
2148 struct lttng_ht_node_u64
*node
;
2149 struct lttng_poll_event events
;
2150 struct lttng_consumer_local_data
*ctx
= data
;
2153 rcu_register_thread();
2155 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2157 /* ENOMEM at this point. Better to bail out. */
2161 DBG("Thread metadata poll started");
2163 /* Size is set to 1 for the consumer_metadata pipe */
2164 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2166 ERR("Poll set creation failed");
2170 ret
= lttng_poll_add(&events
,
2171 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2177 DBG("Metadata main loop started");
2180 /* Only the metadata pipe is set */
2181 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2186 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2187 ret
= lttng_poll_wait(&events
, -1);
2188 DBG("Metadata event catched in thread");
2190 if (errno
== EINTR
) {
2191 ERR("Poll EINTR catched");
2199 /* From here, the event is a metadata wait fd */
2200 for (i
= 0; i
< nb_fd
; i
++) {
2201 revents
= LTTNG_POLL_GETEV(&events
, i
);
2202 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2204 /* Just don't waste time if no returned events for the fd */
2209 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2210 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2211 DBG("Metadata thread pipe hung up");
2213 * Remove the pipe from the poll set and continue the loop
2214 * since their might be data to consume.
2216 lttng_poll_del(&events
,
2217 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2218 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2220 } else if (revents
& LPOLLIN
) {
2223 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2224 &stream
, sizeof(stream
));
2226 ERR("read metadata stream, ret: %ld", pipe_len
);
2228 * Continue here to handle the rest of the streams.
2233 /* A NULL stream means that the state has changed. */
2234 if (stream
== NULL
) {
2235 /* Check for deleted streams. */
2236 validate_endpoint_status_metadata_stream(&events
);
2240 DBG("Adding metadata stream %d to poll set",
2243 ret
= add_metadata_stream(stream
, metadata_ht
);
2245 ERR("Unable to add metadata stream");
2246 /* Stream was not setup properly. Continuing. */
2247 consumer_del_metadata_stream(stream
, NULL
);
2251 /* Add metadata stream to the global poll events list */
2252 lttng_poll_add(&events
, stream
->wait_fd
,
2253 LPOLLIN
| LPOLLPRI
);
2256 /* Handle other stream */
2262 uint64_t tmp_id
= (uint64_t) pollfd
;
2264 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2266 node
= lttng_ht_iter_get_node_u64(&iter
);
2269 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2272 /* Check for error event */
2273 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2274 DBG("Metadata fd %d is hup|err.", pollfd
);
2275 if (!stream
->hangup_flush_done
2276 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2277 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2278 DBG("Attempting to flush and consume the UST buffers");
2279 lttng_ustconsumer_on_stream_hangup(stream
);
2281 /* We just flushed the stream now read it. */
2283 len
= ctx
->on_buffer_ready(stream
, ctx
);
2285 * We don't check the return value here since if we get
2286 * a negative len, it means an error occured thus we
2287 * simply remove it from the poll set and free the
2293 lttng_poll_del(&events
, stream
->wait_fd
);
2295 * This call update the channel states, closes file descriptors
2296 * and securely free the stream.
2298 consumer_del_metadata_stream(stream
, metadata_ht
);
2299 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2300 /* Get the data out of the metadata file descriptor */
2301 DBG("Metadata available on fd %d", pollfd
);
2302 assert(stream
->wait_fd
== pollfd
);
2304 len
= ctx
->on_buffer_ready(stream
, ctx
);
2305 /* It's ok to have an unavailable sub-buffer */
2306 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2307 /* Clean up stream from consumer and free it. */
2308 lttng_poll_del(&events
, stream
->wait_fd
);
2309 consumer_del_metadata_stream(stream
, metadata_ht
);
2310 } else if (len
> 0) {
2311 stream
->data_read
= 1;
2315 /* Release RCU lock for the stream looked up */
2322 DBG("Metadata poll thread exiting");
2324 lttng_poll_clean(&events
);
2326 destroy_stream_ht(metadata_ht
);
2328 rcu_unregister_thread();
2333 * This thread polls the fds in the set to consume the data and write
2334 * it to tracefile if necessary.
2336 void *consumer_thread_data_poll(void *data
)
2338 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2339 struct pollfd
*pollfd
= NULL
;
2340 /* local view of the streams */
2341 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2342 /* local view of consumer_data.fds_count */
2344 struct lttng_consumer_local_data
*ctx
= data
;
2347 rcu_register_thread();
2349 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2350 if (data_ht
== NULL
) {
2351 /* ENOMEM at this point. Better to bail out. */
2355 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
2362 * the fds set has been updated, we need to update our
2363 * local array as well
2365 pthread_mutex_lock(&consumer_data
.lock
);
2366 if (consumer_data
.need_update
) {
2371 local_stream
= NULL
;
2373 /* allocate for all fds + 1 for the consumer_data_pipe */
2374 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2375 if (pollfd
== NULL
) {
2376 PERROR("pollfd malloc");
2377 pthread_mutex_unlock(&consumer_data
.lock
);
2381 /* allocate for all fds + 1 for the consumer_data_pipe */
2382 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2383 sizeof(struct lttng_consumer_stream
*));
2384 if (local_stream
== NULL
) {
2385 PERROR("local_stream malloc");
2386 pthread_mutex_unlock(&consumer_data
.lock
);
2389 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2392 ERR("Error in allocating pollfd or local_outfds");
2393 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2394 pthread_mutex_unlock(&consumer_data
.lock
);
2398 consumer_data
.need_update
= 0;
2400 pthread_mutex_unlock(&consumer_data
.lock
);
2402 /* No FDs and consumer_quit, consumer_cleanup the thread */
2403 if (nb_fd
== 0 && consumer_quit
== 1) {
2406 /* poll on the array of fds */
2408 DBG("polling on %d fd", nb_fd
+ 1);
2409 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2410 DBG("poll num_rdy : %d", num_rdy
);
2411 if (num_rdy
== -1) {
2413 * Restart interrupted system call.
2415 if (errno
== EINTR
) {
2418 PERROR("Poll error");
2419 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2421 } else if (num_rdy
== 0) {
2422 DBG("Polling thread timed out");
2427 * If the consumer_data_pipe triggered poll go directly to the
2428 * beginning of the loop to update the array. We want to prioritize
2429 * array update over low-priority reads.
2431 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2432 ssize_t pipe_readlen
;
2434 DBG("consumer_data_pipe wake up");
2435 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2436 &new_stream
, sizeof(new_stream
));
2437 if (pipe_readlen
< 0) {
2438 ERR("Consumer data pipe ret %ld", pipe_readlen
);
2439 /* Continue so we can at least handle the current stream(s). */
2444 * If the stream is NULL, just ignore it. It's also possible that
2445 * the sessiond poll thread changed the consumer_quit state and is
2446 * waking us up to test it.
2448 if (new_stream
== NULL
) {
2449 validate_endpoint_status_data_stream();
2453 ret
= add_stream(new_stream
, data_ht
);
2455 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
2458 * At this point, if the add_stream fails, it is not in the
2459 * hash table thus passing the NULL value here.
2461 consumer_del_stream(new_stream
, NULL
);
2464 /* Continue to update the local streams and handle prio ones */
2468 /* Take care of high priority channels first. */
2469 for (i
= 0; i
< nb_fd
; i
++) {
2470 if (local_stream
[i
] == NULL
) {
2473 if (pollfd
[i
].revents
& POLLPRI
) {
2474 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2476 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2477 /* it's ok to have an unavailable sub-buffer */
2478 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2479 /* Clean the stream and free it. */
2480 consumer_del_stream(local_stream
[i
], data_ht
);
2481 local_stream
[i
] = NULL
;
2482 } else if (len
> 0) {
2483 local_stream
[i
]->data_read
= 1;
2489 * If we read high prio channel in this loop, try again
2490 * for more high prio data.
2496 /* Take care of low priority channels. */
2497 for (i
= 0; i
< nb_fd
; i
++) {
2498 if (local_stream
[i
] == NULL
) {
2501 if ((pollfd
[i
].revents
& POLLIN
) ||
2502 local_stream
[i
]->hangup_flush_done
) {
2503 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2504 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2505 /* it's ok to have an unavailable sub-buffer */
2506 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2507 /* Clean the stream and free it. */
2508 consumer_del_stream(local_stream
[i
], data_ht
);
2509 local_stream
[i
] = NULL
;
2510 } else if (len
> 0) {
2511 local_stream
[i
]->data_read
= 1;
2516 /* Handle hangup and errors */
2517 for (i
= 0; i
< nb_fd
; i
++) {
2518 if (local_stream
[i
] == NULL
) {
2521 if (!local_stream
[i
]->hangup_flush_done
2522 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2523 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2524 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2525 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2527 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2528 /* Attempt read again, for the data we just flushed. */
2529 local_stream
[i
]->data_read
= 1;
2532 * If the poll flag is HUP/ERR/NVAL and we have
2533 * read no data in this pass, we can remove the
2534 * stream from its hash table.
2536 if ((pollfd
[i
].revents
& POLLHUP
)) {
2537 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2538 if (!local_stream
[i
]->data_read
) {
2539 consumer_del_stream(local_stream
[i
], data_ht
);
2540 local_stream
[i
] = NULL
;
2543 } else if (pollfd
[i
].revents
& POLLERR
) {
2544 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2545 if (!local_stream
[i
]->data_read
) {
2546 consumer_del_stream(local_stream
[i
], data_ht
);
2547 local_stream
[i
] = NULL
;
2550 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2551 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2552 if (!local_stream
[i
]->data_read
) {
2553 consumer_del_stream(local_stream
[i
], data_ht
);
2554 local_stream
[i
] = NULL
;
2558 if (local_stream
[i
] != NULL
) {
2559 local_stream
[i
]->data_read
= 0;
2564 DBG("polling thread exiting");
2569 * Close the write side of the pipe so epoll_wait() in
2570 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2571 * read side of the pipe. If we close them both, epoll_wait strangely does
2572 * not return and could create a endless wait period if the pipe is the
2573 * only tracked fd in the poll set. The thread will take care of closing
2576 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2578 destroy_data_stream_ht(data_ht
);
2580 rcu_unregister_thread();
2585 * Close wake-up end of each stream belonging to the channel. This will
2586 * allow the poll() on the stream read-side to detect when the
2587 * write-side (application) finally closes them.
2590 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2592 struct lttng_ht
*ht
;
2593 struct lttng_consumer_stream
*stream
;
2594 struct lttng_ht_iter iter
;
2596 ht
= consumer_data
.stream_per_chan_id_ht
;
2599 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2600 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2601 ht
->match_fct
, &channel
->key
,
2602 &iter
.iter
, stream
, node_channel_id
.node
) {
2604 * Protect against teardown with mutex.
2606 pthread_mutex_lock(&stream
->lock
);
2607 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2610 switch (consumer_data
.type
) {
2611 case LTTNG_CONSUMER_KERNEL
:
2613 case LTTNG_CONSUMER32_UST
:
2614 case LTTNG_CONSUMER64_UST
:
2616 * Note: a mutex is taken internally within
2617 * liblttng-ust-ctl to protect timer wakeup_fd
2618 * use from concurrent close.
2620 lttng_ustconsumer_close_stream_wakeup(stream
);
2623 ERR("Unknown consumer_data type");
2627 pthread_mutex_unlock(&stream
->lock
);
2632 static void destroy_channel_ht(struct lttng_ht
*ht
)
2634 struct lttng_ht_iter iter
;
2635 struct lttng_consumer_channel
*channel
;
2643 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2644 ret
= lttng_ht_del(ht
, &iter
);
2649 lttng_ht_destroy(ht
);
2653 * This thread polls the channel fds to detect when they are being
2654 * closed. It closes all related streams if the channel is detected as
2655 * closed. It is currently only used as a shim layer for UST because the
2656 * consumerd needs to keep the per-stream wakeup end of pipes open for
2659 void *consumer_thread_channel_poll(void *data
)
2662 uint32_t revents
, nb_fd
;
2663 struct lttng_consumer_channel
*chan
= NULL
;
2664 struct lttng_ht_iter iter
;
2665 struct lttng_ht_node_u64
*node
;
2666 struct lttng_poll_event events
;
2667 struct lttng_consumer_local_data
*ctx
= data
;
2668 struct lttng_ht
*channel_ht
;
2670 rcu_register_thread();
2672 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2674 /* ENOMEM at this point. Better to bail out. */
2678 DBG("Thread channel poll started");
2680 /* Size is set to 1 for the consumer_channel pipe */
2681 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2683 ERR("Poll set creation failed");
2687 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2693 DBG("Channel main loop started");
2696 /* Only the channel pipe is set */
2697 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2702 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2703 ret
= lttng_poll_wait(&events
, -1);
2704 DBG("Channel event catched in thread");
2706 if (errno
== EINTR
) {
2707 ERR("Poll EINTR catched");
2715 /* From here, the event is a channel wait fd */
2716 for (i
= 0; i
< nb_fd
; i
++) {
2717 revents
= LTTNG_POLL_GETEV(&events
, i
);
2718 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2720 /* Just don't waste time if no returned events for the fd */
2724 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2725 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2726 DBG("Channel thread pipe hung up");
2728 * Remove the pipe from the poll set and continue the loop
2729 * since their might be data to consume.
2731 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2733 } else if (revents
& LPOLLIN
) {
2734 enum consumer_channel_action action
;
2737 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2739 ERR("Error reading channel pipe");
2744 case CONSUMER_CHANNEL_ADD
:
2745 DBG("Adding channel %d to poll set",
2748 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2751 lttng_ht_add_unique_u64(channel_ht
,
2752 &chan
->wait_fd_node
);
2754 /* Add channel to the global poll events list */
2755 lttng_poll_add(&events
, chan
->wait_fd
,
2756 LPOLLIN
| LPOLLPRI
);
2758 case CONSUMER_CHANNEL_DEL
:
2760 struct lttng_consumer_stream
*stream
, *stmp
;
2763 chan
= consumer_find_channel(key
);
2766 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2769 lttng_poll_del(&events
, chan
->wait_fd
);
2770 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2771 ret
= lttng_ht_del(channel_ht
, &iter
);
2773 consumer_close_channel_streams(chan
);
2775 switch (consumer_data
.type
) {
2776 case LTTNG_CONSUMER_KERNEL
:
2778 case LTTNG_CONSUMER32_UST
:
2779 case LTTNG_CONSUMER64_UST
:
2780 /* Delete streams that might have been left in the stream list. */
2781 cds_list_for_each_entry_safe(stream
, stmp
, &chan
->streams
.head
,
2783 cds_list_del(&stream
->send_node
);
2784 lttng_ustconsumer_del_stream(stream
);
2785 uatomic_sub(&stream
->chan
->refcount
, 1);
2786 assert(&chan
->refcount
);
2791 ERR("Unknown consumer_data type");
2796 * Release our own refcount. Force channel deletion even if
2797 * streams were not initialized.
2799 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2800 consumer_del_channel(chan
);
2805 case CONSUMER_CHANNEL_QUIT
:
2807 * Remove the pipe from the poll set and continue the loop
2808 * since their might be data to consume.
2810 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2813 ERR("Unknown action");
2818 /* Handle other stream */
2824 uint64_t tmp_id
= (uint64_t) pollfd
;
2826 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2828 node
= lttng_ht_iter_get_node_u64(&iter
);
2831 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2834 /* Check for error event */
2835 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2836 DBG("Channel fd %d is hup|err.", pollfd
);
2838 lttng_poll_del(&events
, chan
->wait_fd
);
2839 ret
= lttng_ht_del(channel_ht
, &iter
);
2841 assert(cds_list_empty(&chan
->streams
.head
));
2842 consumer_close_channel_streams(chan
);
2844 /* Release our own refcount */
2845 if (!uatomic_sub_return(&chan
->refcount
, 1)
2846 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2847 consumer_del_channel(chan
);
2851 /* Release RCU lock for the channel looked up */
2857 lttng_poll_clean(&events
);
2859 destroy_channel_ht(channel_ht
);
2861 DBG("Channel poll thread exiting");
2862 rcu_unregister_thread();
2866 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2867 struct pollfd
*sockpoll
, int client_socket
)
2874 if (lttng_consumer_poll_socket(sockpoll
) < 0) {
2878 DBG("Metadata connection on client_socket");
2880 /* Blocking call, waiting for transmission */
2881 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2882 if (ctx
->consumer_metadata_socket
< 0) {
2883 WARN("On accept metadata");
2894 * This thread listens on the consumerd socket and receives the file
2895 * descriptors from the session daemon.
2897 void *consumer_thread_sessiond_poll(void *data
)
2899 int sock
= -1, client_socket
, ret
;
2901 * structure to poll for incoming data on communication socket avoids
2902 * making blocking sockets.
2904 struct pollfd consumer_sockpoll
[2];
2905 struct lttng_consumer_local_data
*ctx
= data
;
2907 rcu_register_thread();
2909 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2910 unlink(ctx
->consumer_command_sock_path
);
2911 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2912 if (client_socket
< 0) {
2913 ERR("Cannot create command socket");
2917 ret
= lttcomm_listen_unix_sock(client_socket
);
2922 DBG("Sending ready command to lttng-sessiond");
2923 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2924 /* return < 0 on error, but == 0 is not fatal */
2926 ERR("Error sending ready command to lttng-sessiond");
2930 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2931 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2932 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2933 consumer_sockpoll
[1].fd
= client_socket
;
2934 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2936 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2939 DBG("Connection on client_socket");
2941 /* Blocking call, waiting for transmission */
2942 sock
= lttcomm_accept_unix_sock(client_socket
);
2949 * Setup metadata socket which is the second socket connection on the
2950 * command unix socket.
2952 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
2957 /* This socket is not useful anymore. */
2958 ret
= close(client_socket
);
2960 PERROR("close client_socket");
2964 /* update the polling structure to poll on the established socket */
2965 consumer_sockpoll
[1].fd
= sock
;
2966 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2969 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2972 DBG("Incoming command on sock");
2973 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2974 if (ret
== -ENOENT
) {
2975 DBG("Received STOP command");
2980 * This could simply be a session daemon quitting. Don't output
2983 DBG("Communication interrupted on command socket");
2986 if (consumer_quit
) {
2987 DBG("consumer_thread_receive_fds received quit from signal");
2990 DBG("received command on sock");
2993 DBG("Consumer thread sessiond poll exiting");
2996 * Close metadata streams since the producer is the session daemon which
2999 * NOTE: for now, this only applies to the UST tracer.
3001 lttng_consumer_close_metadata();
3004 * when all fds have hung up, the polling thread
3010 * Notify the data poll thread to poll back again and test the
3011 * consumer_quit state that we just set so to quit gracefully.
3013 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3015 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3017 /* Cleaning up possibly open sockets. */
3021 PERROR("close sock sessiond poll");
3024 if (client_socket
>= 0) {
3025 ret
= close(client_socket
);
3027 PERROR("close client_socket sessiond poll");
3031 rcu_unregister_thread();
3035 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3036 struct lttng_consumer_local_data
*ctx
)
3040 pthread_mutex_lock(&stream
->lock
);
3042 switch (consumer_data
.type
) {
3043 case LTTNG_CONSUMER_KERNEL
:
3044 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3046 case LTTNG_CONSUMER32_UST
:
3047 case LTTNG_CONSUMER64_UST
:
3048 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3051 ERR("Unknown consumer_data type");
3057 pthread_mutex_unlock(&stream
->lock
);
3061 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3063 switch (consumer_data
.type
) {
3064 case LTTNG_CONSUMER_KERNEL
:
3065 return lttng_kconsumer_on_recv_stream(stream
);
3066 case LTTNG_CONSUMER32_UST
:
3067 case LTTNG_CONSUMER64_UST
:
3068 return lttng_ustconsumer_on_recv_stream(stream
);
3070 ERR("Unknown consumer_data type");
3077 * Allocate and set consumer data hash tables.
3079 void lttng_consumer_init(void)
3081 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3082 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3083 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3084 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3088 * Process the ADD_RELAYD command receive by a consumer.
3090 * This will create a relayd socket pair and add it to the relayd hash table.
3091 * The caller MUST acquire a RCU read side lock before calling it.
3093 int consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3094 struct lttng_consumer_local_data
*ctx
, int sock
,
3095 struct pollfd
*consumer_sockpoll
,
3096 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
)
3098 int fd
= -1, ret
= -1, relayd_created
= 0;
3099 enum lttng_error_code ret_code
= LTTNG_OK
;
3100 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3103 assert(relayd_sock
);
3105 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3107 /* Get relayd reference if exists. */
3108 relayd
= consumer_find_relayd(net_seq_idx
);
3109 if (relayd
== NULL
) {
3110 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3111 /* Not found. Allocate one. */
3112 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3113 if (relayd
== NULL
) {
3115 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3118 relayd
->sessiond_session_id
= sessiond_id
;
3123 * This code path MUST continue to the consumer send status message to
3124 * we can notify the session daemon and continue our work without
3125 * killing everything.
3129 * relayd key should never be found for control socket.
3131 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3134 /* First send a status message before receiving the fds. */
3135 ret
= consumer_send_status_msg(sock
, LTTNG_OK
);
3137 /* Somehow, the session daemon is not responding anymore. */
3138 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3139 goto error_nosignal
;
3142 /* Poll on consumer socket. */
3143 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
3144 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3146 goto error_nosignal
;
3149 /* Get relayd socket from session daemon */
3150 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3151 if (ret
!= sizeof(fd
)) {
3153 fd
= -1; /* Just in case it gets set with an invalid value. */
3156 * Failing to receive FDs might indicate a major problem such as
3157 * reaching a fd limit during the receive where the kernel returns a
3158 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3159 * don't take any chances and stop everything.
3161 * XXX: Feature request #558 will fix that and avoid this possible
3162 * issue when reaching the fd limit.
3164 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3165 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3169 /* Copy socket information and received FD */
3170 switch (sock_type
) {
3171 case LTTNG_STREAM_CONTROL
:
3172 /* Copy received lttcomm socket */
3173 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3174 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3175 /* Handle create_sock error. */
3177 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3181 * Close the socket created internally by
3182 * lttcomm_create_sock, so we can replace it by the one
3183 * received from sessiond.
3185 if (close(relayd
->control_sock
.sock
.fd
)) {
3189 /* Assign new file descriptor */
3190 relayd
->control_sock
.sock
.fd
= fd
;
3191 fd
= -1; /* For error path */
3192 /* Assign version values. */
3193 relayd
->control_sock
.major
= relayd_sock
->major
;
3194 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3197 * Create a session on the relayd and store the returned id. Lock the
3198 * control socket mutex if the relayd was NOT created before.
3200 if (!relayd_created
) {
3201 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3203 ret
= relayd_create_session(&relayd
->control_sock
,
3204 &relayd
->relayd_session_id
);
3205 if (!relayd_created
) {
3206 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3210 * Close all sockets of a relayd object. It will be freed if it was
3211 * created at the error code path or else it will be garbage
3214 (void) relayd_close(&relayd
->control_sock
);
3215 (void) relayd_close(&relayd
->data_sock
);
3216 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
3221 case LTTNG_STREAM_DATA
:
3222 /* Copy received lttcomm socket */
3223 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3224 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3225 /* Handle create_sock error. */
3227 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3231 * Close the socket created internally by
3232 * lttcomm_create_sock, so we can replace it by the one
3233 * received from sessiond.
3235 if (close(relayd
->data_sock
.sock
.fd
)) {
3239 /* Assign new file descriptor */
3240 relayd
->data_sock
.sock
.fd
= fd
;
3241 fd
= -1; /* for eventual error paths */
3242 /* Assign version values. */
3243 relayd
->data_sock
.major
= relayd_sock
->major
;
3244 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3247 ERR("Unknown relayd socket type (%d)", sock_type
);
3249 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3253 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3254 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3255 relayd
->net_seq_idx
, fd
);
3257 /* We successfully added the socket. Send status back. */
3258 ret
= consumer_send_status_msg(sock
, ret_code
);
3260 /* Somehow, the session daemon is not responding anymore. */
3261 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3262 goto error_nosignal
;
3266 * Add relayd socket pair to consumer data hashtable. If object already
3267 * exists or on error, the function gracefully returns.
3275 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3276 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3280 /* Close received socket if valid. */
3283 PERROR("close received socket");
3287 if (relayd_created
) {
3295 * Try to lock the stream mutex.
3297 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3299 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3306 * Try to lock the stream mutex. On failure, we know that the stream is
3307 * being used else where hence there is data still being extracted.
3309 ret
= pthread_mutex_trylock(&stream
->lock
);
3311 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3323 * Search for a relayd associated to the session id and return the reference.
3325 * A rcu read side lock MUST be acquire before calling this function and locked
3326 * until the relayd object is no longer necessary.
3328 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3330 struct lttng_ht_iter iter
;
3331 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3333 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3334 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3337 * Check by sessiond id which is unique here where the relayd session
3338 * id might not be when having multiple relayd.
3340 if (relayd
->sessiond_session_id
== id
) {
3341 /* Found the relayd. There can be only one per id. */
3353 * Check if for a given session id there is still data needed to be extract
3356 * Return 1 if data is pending or else 0 meaning ready to be read.
3358 int consumer_data_pending(uint64_t id
)
3361 struct lttng_ht_iter iter
;
3362 struct lttng_ht
*ht
;
3363 struct lttng_consumer_stream
*stream
;
3364 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3365 int (*data_pending
)(struct lttng_consumer_stream
*);
3367 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3370 pthread_mutex_lock(&consumer_data
.lock
);
3372 switch (consumer_data
.type
) {
3373 case LTTNG_CONSUMER_KERNEL
:
3374 data_pending
= lttng_kconsumer_data_pending
;
3376 case LTTNG_CONSUMER32_UST
:
3377 case LTTNG_CONSUMER64_UST
:
3378 data_pending
= lttng_ustconsumer_data_pending
;
3381 ERR("Unknown consumer data type");
3385 /* Ease our life a bit */
3386 ht
= consumer_data
.stream_list_ht
;
3388 relayd
= find_relayd_by_session_id(id
);
3390 /* Send init command for data pending. */
3391 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3392 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3393 relayd
->relayd_session_id
);
3394 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3396 /* Communication error thus the relayd so no data pending. */
3397 goto data_not_pending
;
3401 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3402 ht
->hash_fct(&id
, lttng_ht_seed
),
3404 &iter
.iter
, stream
, node_session_id
.node
) {
3405 /* If this call fails, the stream is being used hence data pending. */
3406 ret
= stream_try_lock(stream
);
3412 * A removed node from the hash table indicates that the stream has
3413 * been deleted thus having a guarantee that the buffers are closed
3414 * on the consumer side. However, data can still be transmitted
3415 * over the network so don't skip the relayd check.
3417 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3419 /* Check the stream if there is data in the buffers. */
3420 ret
= data_pending(stream
);
3422 pthread_mutex_unlock(&stream
->lock
);
3429 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3430 if (stream
->metadata_flag
) {
3431 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3432 stream
->relayd_stream_id
);
3434 ret
= relayd_data_pending(&relayd
->control_sock
,
3435 stream
->relayd_stream_id
,
3436 stream
->next_net_seq_num
- 1);
3438 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3440 pthread_mutex_unlock(&stream
->lock
);
3444 pthread_mutex_unlock(&stream
->lock
);
3448 unsigned int is_data_inflight
= 0;
3450 /* Send init command for data pending. */
3451 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3452 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3453 relayd
->relayd_session_id
, &is_data_inflight
);
3454 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3456 goto data_not_pending
;
3458 if (is_data_inflight
) {
3464 * Finding _no_ node in the hash table and no inflight data means that the
3465 * stream(s) have been removed thus data is guaranteed to be available for
3466 * analysis from the trace files.
3470 /* Data is available to be read by a viewer. */
3471 pthread_mutex_unlock(&consumer_data
.lock
);
3476 /* Data is still being extracted from buffers. */
3477 pthread_mutex_unlock(&consumer_data
.lock
);
3483 * Send a ret code status message to the sessiond daemon.
3485 * Return the sendmsg() return value.
3487 int consumer_send_status_msg(int sock
, int ret_code
)
3489 struct lttcomm_consumer_status_msg msg
;
3491 msg
.ret_code
= ret_code
;
3493 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3497 * Send a channel status message to the sessiond daemon.
3499 * Return the sendmsg() return value.
3501 int consumer_send_status_channel(int sock
,
3502 struct lttng_consumer_channel
*channel
)
3504 struct lttcomm_consumer_status_channel msg
;
3509 msg
.ret_code
= -LTTNG_ERR_UST_CHAN_FAIL
;
3511 msg
.ret_code
= LTTNG_OK
;
3512 msg
.key
= channel
->key
;
3513 msg
.stream_count
= channel
->streams
.count
;
3516 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));