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
);
568 * XXX naming of del vs destroy is all mixed up.
570 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
572 consumer_del_stream(stream
, data_ht
);
575 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
577 consumer_del_stream(stream
, metadata_ht
);
580 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
582 enum lttng_consumer_stream_state state
,
583 const char *channel_name
,
590 enum consumer_channel_type type
)
593 struct lttng_consumer_stream
*stream
;
595 stream
= zmalloc(sizeof(*stream
));
596 if (stream
== NULL
) {
597 PERROR("malloc struct lttng_consumer_stream");
604 stream
->key
= stream_key
;
606 stream
->out_fd_offset
= 0;
607 stream
->state
= state
;
610 stream
->net_seq_idx
= relayd_id
;
611 stream
->session_id
= session_id
;
612 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
613 pthread_mutex_init(&stream
->lock
, NULL
);
615 /* If channel is the metadata, flag this stream as metadata. */
616 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
617 stream
->metadata_flag
= 1;
618 /* Metadata is flat out. */
619 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
621 /* Format stream name to <channel_name>_<cpu_number> */
622 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
625 PERROR("snprintf stream name");
630 /* Key is always the wait_fd for streams. */
631 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
633 /* Init node per channel id key */
634 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
636 /* Init session id node with the stream session id */
637 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
639 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
" relayd_id %" PRIu64
", session_id %" PRIu64
,
640 stream
->name
, stream
->key
, channel_key
, stream
->net_seq_idx
, stream
->session_id
);
656 * Add a stream to the global list protected by a mutex.
658 int consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
660 struct lttng_ht
*ht
= data_ht
;
662 struct consumer_relayd_sock_pair
*relayd
;
667 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
669 pthread_mutex_lock(&consumer_data
.lock
);
670 pthread_mutex_lock(&stream
->chan
->lock
);
671 pthread_mutex_lock(&stream
->chan
->timer_lock
);
672 pthread_mutex_lock(&stream
->lock
);
675 /* Steal stream identifier to avoid having streams with the same key */
676 steal_stream_key(stream
->key
, ht
);
678 lttng_ht_add_unique_u64(ht
, &stream
->node
);
680 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
681 &stream
->node_channel_id
);
684 * Add stream to the stream_list_ht of the consumer data. No need to steal
685 * the key since the HT does not use it and we allow to add redundant keys
688 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
690 /* Check and cleanup relayd */
691 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
692 if (relayd
!= NULL
) {
693 uatomic_inc(&relayd
->refcount
);
697 * When nb_init_stream_left reaches 0, we don't need to trigger any action
698 * in terms of destroying the associated channel, because the action that
699 * causes the count to become 0 also causes a stream to be added. The
700 * channel deletion will thus be triggered by the following removal of this
703 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
704 /* Increment refcount before decrementing nb_init_stream_left */
706 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
709 /* Update consumer data once the node is inserted. */
710 consumer_data
.stream_count
++;
711 consumer_data
.need_update
= 1;
714 pthread_mutex_unlock(&stream
->lock
);
715 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
716 pthread_mutex_unlock(&stream
->chan
->lock
);
717 pthread_mutex_unlock(&consumer_data
.lock
);
722 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
724 consumer_del_stream(stream
, data_ht
);
728 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
729 * be acquired before calling this.
731 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
734 struct lttng_ht_node_u64
*node
;
735 struct lttng_ht_iter iter
;
739 lttng_ht_lookup(consumer_data
.relayd_ht
,
740 &relayd
->net_seq_idx
, &iter
);
741 node
= lttng_ht_iter_get_node_u64(&iter
);
745 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
752 * Allocate and return a consumer relayd socket.
754 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
755 uint64_t net_seq_idx
)
757 struct consumer_relayd_sock_pair
*obj
= NULL
;
759 /* net sequence index of -1 is a failure */
760 if (net_seq_idx
== (uint64_t) -1ULL) {
764 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
766 PERROR("zmalloc relayd sock");
770 obj
->net_seq_idx
= net_seq_idx
;
772 obj
->destroy_flag
= 0;
773 obj
->control_sock
.sock
.fd
= -1;
774 obj
->data_sock
.sock
.fd
= -1;
775 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
776 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
783 * Find a relayd socket pair in the global consumer data.
785 * Return the object if found else NULL.
786 * RCU read-side lock must be held across this call and while using the
789 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
791 struct lttng_ht_iter iter
;
792 struct lttng_ht_node_u64
*node
;
793 struct consumer_relayd_sock_pair
*relayd
= NULL
;
795 /* Negative keys are lookup failures */
796 if (key
== (uint64_t) -1ULL) {
800 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
802 node
= lttng_ht_iter_get_node_u64(&iter
);
804 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
812 * Handle stream for relayd transmission if the stream applies for network
813 * streaming where the net sequence index is set.
815 * Return destination file descriptor or negative value on error.
817 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
818 size_t data_size
, unsigned long padding
,
819 struct consumer_relayd_sock_pair
*relayd
)
822 struct lttcomm_relayd_data_hdr data_hdr
;
828 /* Reset data header */
829 memset(&data_hdr
, 0, sizeof(data_hdr
));
831 if (stream
->metadata_flag
) {
832 /* Caller MUST acquire the relayd control socket lock */
833 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
838 /* Metadata are always sent on the control socket. */
839 outfd
= relayd
->control_sock
.sock
.fd
;
841 /* Set header with stream information */
842 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
843 data_hdr
.data_size
= htobe32(data_size
);
844 data_hdr
.padding_size
= htobe32(padding
);
846 * Note that net_seq_num below is assigned with the *current* value of
847 * next_net_seq_num and only after that the next_net_seq_num will be
848 * increment. This is why when issuing a command on the relayd using
849 * this next value, 1 should always be substracted in order to compare
850 * the last seen sequence number on the relayd side to the last sent.
852 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
853 /* Other fields are zeroed previously */
855 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
861 ++stream
->next_net_seq_num
;
863 /* Set to go on data socket */
864 outfd
= relayd
->data_sock
.sock
.fd
;
872 * Allocate and return a new lttng_consumer_channel object using the given key
873 * to initialize the hash table node.
875 * On error, return NULL.
877 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
879 const char *pathname
,
884 enum lttng_event_output output
,
885 uint64_t tracefile_size
,
886 uint64_t tracefile_count
,
887 uint64_t session_id_per_pid
)
889 struct lttng_consumer_channel
*channel
;
891 channel
= zmalloc(sizeof(*channel
));
892 if (channel
== NULL
) {
893 PERROR("malloc struct lttng_consumer_channel");
898 channel
->refcount
= 0;
899 channel
->session_id
= session_id
;
900 channel
->session_id_per_pid
= session_id_per_pid
;
903 channel
->relayd_id
= relayd_id
;
904 channel
->output
= output
;
905 channel
->tracefile_size
= tracefile_size
;
906 channel
->tracefile_count
= tracefile_count
;
907 pthread_mutex_init(&channel
->lock
, NULL
);
908 pthread_mutex_init(&channel
->timer_lock
, NULL
);
910 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
911 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
913 strncpy(channel
->name
, name
, sizeof(channel
->name
));
914 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
916 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
918 channel
->wait_fd
= -1;
920 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
922 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
929 * Add a channel to the global list protected by a mutex.
931 * On success 0 is returned else a negative value.
933 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
934 struct lttng_consumer_local_data
*ctx
)
937 struct lttng_ht_node_u64
*node
;
938 struct lttng_ht_iter iter
;
940 pthread_mutex_lock(&consumer_data
.lock
);
941 pthread_mutex_lock(&channel
->lock
);
942 pthread_mutex_lock(&channel
->timer_lock
);
945 lttng_ht_lookup(consumer_data
.channel_ht
, &channel
->key
, &iter
);
946 node
= lttng_ht_iter_get_node_u64(&iter
);
948 /* Channel already exist. Ignore the insertion */
949 ERR("Consumer add channel key %" PRIu64
" already exists!",
955 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
959 pthread_mutex_unlock(&channel
->timer_lock
);
960 pthread_mutex_unlock(&channel
->lock
);
961 pthread_mutex_unlock(&consumer_data
.lock
);
963 if (!ret
&& channel
->wait_fd
!= -1 &&
964 channel
->metadata_stream
== NULL
) {
965 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
971 * Allocate the pollfd structure and the local view of the out fds to avoid
972 * doing a lookup in the linked list and concurrency issues when writing is
973 * needed. Called with consumer_data.lock held.
975 * Returns the number of fds in the structures.
977 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
978 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
982 struct lttng_ht_iter iter
;
983 struct lttng_consumer_stream
*stream
;
988 assert(local_stream
);
990 DBG("Updating poll fd array");
992 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
994 * Only active streams with an active end point can be added to the
995 * poll set and local stream storage of the thread.
997 * There is a potential race here for endpoint_status to be updated
998 * just after the check. However, this is OK since the stream(s) will
999 * be deleted once the thread is notified that the end point state has
1000 * changed where this function will be called back again.
1002 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
1003 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1007 * This clobbers way too much the debug output. Uncomment that if you
1008 * need it for debugging purposes.
1010 * DBG("Active FD %d", stream->wait_fd);
1012 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1013 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1014 local_stream
[i
] = stream
;
1020 * Insert the consumer_data_pipe at the end of the array and don't
1021 * increment i so nb_fd is the number of real FD.
1023 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1024 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1029 * Poll on the should_quit pipe and the command socket return -1 on error and
1030 * should exit, 0 if data is available on the command socket
1032 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1037 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1038 if (num_rdy
== -1) {
1040 * Restart interrupted system call.
1042 if (errno
== EINTR
) {
1045 PERROR("Poll error");
1048 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1049 DBG("consumer_should_quit wake up");
1059 * Set the error socket.
1061 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1064 ctx
->consumer_error_socket
= sock
;
1068 * Set the command socket path.
1070 void lttng_consumer_set_command_sock_path(
1071 struct lttng_consumer_local_data
*ctx
, char *sock
)
1073 ctx
->consumer_command_sock_path
= sock
;
1077 * Send return code to the session daemon.
1078 * If the socket is not defined, we return 0, it is not a fatal error
1080 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1082 if (ctx
->consumer_error_socket
> 0) {
1083 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1084 sizeof(enum lttcomm_sessiond_command
));
1091 * Close all the tracefiles and stream fds and MUST be called when all
1092 * instances are destroyed i.e. when all threads were joined and are ended.
1094 void lttng_consumer_cleanup(void)
1096 struct lttng_ht_iter iter
;
1097 struct lttng_consumer_channel
*channel
;
1101 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1103 consumer_del_channel(channel
);
1108 lttng_ht_destroy(consumer_data
.channel_ht
);
1110 cleanup_relayd_ht();
1112 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1115 * This HT contains streams that are freed by either the metadata thread or
1116 * the data thread so we do *nothing* on the hash table and simply destroy
1119 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1123 * Called from signal handler.
1125 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1130 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1131 } while (ret
< 0 && errno
== EINTR
);
1132 if (ret
< 0 || ret
!= 1) {
1133 PERROR("write consumer quit");
1136 DBG("Consumer flag that it should quit");
1139 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1142 int outfd
= stream
->out_fd
;
1145 * This does a blocking write-and-wait on any page that belongs to the
1146 * subbuffer prior to the one we just wrote.
1147 * Don't care about error values, as these are just hints and ways to
1148 * limit the amount of page cache used.
1150 if (orig_offset
< stream
->max_sb_size
) {
1153 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1154 stream
->max_sb_size
,
1155 SYNC_FILE_RANGE_WAIT_BEFORE
1156 | SYNC_FILE_RANGE_WRITE
1157 | SYNC_FILE_RANGE_WAIT_AFTER
);
1159 * Give hints to the kernel about how we access the file:
1160 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1163 * We need to call fadvise again after the file grows because the
1164 * kernel does not seem to apply fadvise to non-existing parts of the
1167 * Call fadvise _after_ having waited for the page writeback to
1168 * complete because the dirty page writeback semantic is not well
1169 * defined. So it can be expected to lead to lower throughput in
1172 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1173 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1177 * Initialise the necessary environnement :
1178 * - create a new context
1179 * - create the poll_pipe
1180 * - create the should_quit pipe (for signal handler)
1181 * - create the thread pipe (for splice)
1183 * Takes a function pointer as argument, this function is called when data is
1184 * available on a buffer. This function is responsible to do the
1185 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1186 * buffer configuration and then kernctl_put_next_subbuf at the end.
1188 * Returns a pointer to the new context or NULL on error.
1190 struct lttng_consumer_local_data
*lttng_consumer_create(
1191 enum lttng_consumer_type type
,
1192 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1193 struct lttng_consumer_local_data
*ctx
),
1194 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1195 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1196 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1199 struct lttng_consumer_local_data
*ctx
;
1201 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1202 consumer_data
.type
== type
);
1203 consumer_data
.type
= type
;
1205 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1207 PERROR("allocating context");
1211 ctx
->consumer_error_socket
= -1;
1212 ctx
->consumer_metadata_socket
= -1;
1213 /* assign the callbacks */
1214 ctx
->on_buffer_ready
= buffer_ready
;
1215 ctx
->on_recv_channel
= recv_channel
;
1216 ctx
->on_recv_stream
= recv_stream
;
1217 ctx
->on_update_stream
= update_stream
;
1219 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1220 if (!ctx
->consumer_data_pipe
) {
1221 goto error_poll_pipe
;
1224 ret
= pipe(ctx
->consumer_should_quit
);
1226 PERROR("Error creating recv pipe");
1227 goto error_quit_pipe
;
1230 ret
= pipe(ctx
->consumer_thread_pipe
);
1232 PERROR("Error creating thread pipe");
1233 goto error_thread_pipe
;
1236 ret
= pipe(ctx
->consumer_channel_pipe
);
1238 PERROR("Error creating channel pipe");
1239 goto error_channel_pipe
;
1242 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1243 if (!ctx
->consumer_metadata_pipe
) {
1244 goto error_metadata_pipe
;
1247 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1249 goto error_splice_pipe
;
1255 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1256 error_metadata_pipe
:
1257 utils_close_pipe(ctx
->consumer_channel_pipe
);
1259 utils_close_pipe(ctx
->consumer_thread_pipe
);
1261 utils_close_pipe(ctx
->consumer_should_quit
);
1263 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1271 * Close all fds associated with the instance and free the context.
1273 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1277 DBG("Consumer destroying it. Closing everything.");
1279 ret
= close(ctx
->consumer_error_socket
);
1283 ret
= close(ctx
->consumer_metadata_socket
);
1287 utils_close_pipe(ctx
->consumer_thread_pipe
);
1288 utils_close_pipe(ctx
->consumer_channel_pipe
);
1289 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1290 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1291 utils_close_pipe(ctx
->consumer_should_quit
);
1292 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1294 unlink(ctx
->consumer_command_sock_path
);
1299 * Write the metadata stream id on the specified file descriptor.
1301 static int write_relayd_metadata_id(int fd
,
1302 struct lttng_consumer_stream
*stream
,
1303 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1306 struct lttcomm_relayd_metadata_payload hdr
;
1308 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1309 hdr
.padding_size
= htobe32(padding
);
1311 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1312 } while (ret
< 0 && errno
== EINTR
);
1313 if (ret
< 0 || ret
!= sizeof(hdr
)) {
1315 * This error means that the fd's end is closed so ignore the perror
1316 * not to clubber the error output since this can happen in a normal
1319 if (errno
!= EPIPE
) {
1320 PERROR("write metadata stream id");
1322 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1324 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1325 * handle writting the missing part so report that as an error and
1326 * don't lie to the caller.
1331 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1332 stream
->relayd_stream_id
, padding
);
1339 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1340 * core function for writing trace buffers to either the local filesystem or
1343 * It must be called with the stream lock held.
1345 * Careful review MUST be put if any changes occur!
1347 * Returns the number of bytes written
1349 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1350 struct lttng_consumer_local_data
*ctx
,
1351 struct lttng_consumer_stream
*stream
, unsigned long len
,
1352 unsigned long padding
)
1354 unsigned long mmap_offset
;
1356 ssize_t ret
= 0, written
= 0;
1357 off_t orig_offset
= stream
->out_fd_offset
;
1358 /* Default is on the disk */
1359 int outfd
= stream
->out_fd
;
1360 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1361 unsigned int relayd_hang_up
= 0;
1363 /* RCU lock for the relayd pointer */
1366 /* Flag that the current stream if set for network streaming. */
1367 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1368 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1369 if (relayd
== NULL
) {
1374 /* get the offset inside the fd to mmap */
1375 switch (consumer_data
.type
) {
1376 case LTTNG_CONSUMER_KERNEL
:
1377 mmap_base
= stream
->mmap_base
;
1378 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1380 case LTTNG_CONSUMER32_UST
:
1381 case LTTNG_CONSUMER64_UST
:
1382 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1384 ERR("read mmap get mmap base for stream %s", stream
->name
);
1388 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1392 ERR("Unknown consumer_data type");
1397 PERROR("tracer ctl get_mmap_read_offset");
1402 /* Handle stream on the relayd if the output is on the network */
1404 unsigned long netlen
= len
;
1407 * Lock the control socket for the complete duration of the function
1408 * since from this point on we will use the socket.
1410 if (stream
->metadata_flag
) {
1411 /* Metadata requires the control socket. */
1412 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1413 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1416 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1418 /* Use the returned socket. */
1421 /* Write metadata stream id before payload */
1422 if (stream
->metadata_flag
) {
1423 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1426 /* Socket operation failed. We consider the relayd dead */
1427 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1435 /* Socket operation failed. We consider the relayd dead */
1436 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1440 /* Else, use the default set before which is the filesystem. */
1443 /* No streaming, we have to set the len with the full padding */
1447 * Check if we need to change the tracefile before writing the packet.
1449 if (stream
->chan
->tracefile_size
> 0 &&
1450 (stream
->tracefile_size_current
+ len
) >
1451 stream
->chan
->tracefile_size
) {
1452 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1453 stream
->name
, stream
->chan
->tracefile_size
,
1454 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1455 stream
->out_fd
, &(stream
->tracefile_count_current
));
1457 ERR("Rotating output file");
1460 outfd
= stream
->out_fd
= ret
;
1461 /* Reset current size because we just perform a rotation. */
1462 stream
->tracefile_size_current
= 0;
1464 stream
->tracefile_size_current
+= len
;
1469 ret
= write(outfd
, mmap_base
+ mmap_offset
, len
);
1470 } while (ret
< 0 && errno
== EINTR
);
1471 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1474 * This is possible if the fd is closed on the other side (outfd)
1475 * or any write problem. It can be verbose a bit for a normal
1476 * execution if for instance the relayd is stopped abruptly. This
1477 * can happen so set this to a DBG statement.
1479 DBG("Error in file write mmap");
1483 /* Socket operation failed. We consider the relayd dead */
1484 if (errno
== EPIPE
|| errno
== EINVAL
) {
1489 } else if (ret
> len
) {
1490 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1498 /* This call is useless on a socket so better save a syscall. */
1500 /* This won't block, but will start writeout asynchronously */
1501 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1502 SYNC_FILE_RANGE_WRITE
);
1503 stream
->out_fd_offset
+= ret
;
1507 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1511 * This is a special case that the relayd has closed its socket. Let's
1512 * cleanup the relayd object and all associated streams.
1514 if (relayd
&& relayd_hang_up
) {
1515 cleanup_relayd(relayd
, ctx
);
1519 /* Unlock only if ctrl socket used */
1520 if (relayd
&& stream
->metadata_flag
) {
1521 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1529 * Splice the data from the ring buffer to the tracefile.
1531 * It must be called with the stream lock held.
1533 * Returns the number of bytes spliced.
1535 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1536 struct lttng_consumer_local_data
*ctx
,
1537 struct lttng_consumer_stream
*stream
, unsigned long len
,
1538 unsigned long padding
)
1540 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1542 off_t orig_offset
= stream
->out_fd_offset
;
1543 int fd
= stream
->wait_fd
;
1544 /* Default is on the disk */
1545 int outfd
= stream
->out_fd
;
1546 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1548 unsigned int relayd_hang_up
= 0;
1550 switch (consumer_data
.type
) {
1551 case LTTNG_CONSUMER_KERNEL
:
1553 case LTTNG_CONSUMER32_UST
:
1554 case LTTNG_CONSUMER64_UST
:
1555 /* Not supported for user space tracing */
1558 ERR("Unknown consumer_data type");
1562 /* RCU lock for the relayd pointer */
1565 /* Flag that the current stream if set for network streaming. */
1566 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1567 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1568 if (relayd
== NULL
) {
1574 * Choose right pipe for splice. Metadata and trace data are handled by
1575 * different threads hence the use of two pipes in order not to race or
1576 * corrupt the written data.
1578 if (stream
->metadata_flag
) {
1579 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1581 splice_pipe
= ctx
->consumer_thread_pipe
;
1584 /* Write metadata stream id before payload */
1586 int total_len
= len
;
1588 if (stream
->metadata_flag
) {
1590 * Lock the control socket for the complete duration of the function
1591 * since from this point on we will use the socket.
1593 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1595 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1599 /* Socket operation failed. We consider the relayd dead */
1600 if (ret
== -EBADF
) {
1601 WARN("Remote relayd disconnected. Stopping");
1608 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1611 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1613 /* Use the returned socket. */
1616 /* Socket operation failed. We consider the relayd dead */
1617 if (ret
== -EBADF
) {
1618 WARN("Remote relayd disconnected. Stopping");
1625 /* No streaming, we have to set the len with the full padding */
1629 * Check if we need to change the tracefile before writing the packet.
1631 if (stream
->chan
->tracefile_size
> 0 &&
1632 (stream
->tracefile_size_current
+ len
) >
1633 stream
->chan
->tracefile_size
) {
1634 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1635 stream
->name
, stream
->chan
->tracefile_size
,
1636 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1637 stream
->out_fd
, &(stream
->tracefile_count_current
));
1639 ERR("Rotating output file");
1642 outfd
= stream
->out_fd
= ret
;
1643 /* Reset current size because we just perform a rotation. */
1644 stream
->tracefile_size_current
= 0;
1646 stream
->tracefile_size_current
+= len
;
1650 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1651 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1652 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1653 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1654 DBG("splice chan to pipe, ret %zd", ret_splice
);
1655 if (ret_splice
< 0) {
1656 PERROR("Error in relay splice");
1658 written
= ret_splice
;
1664 /* Handle stream on the relayd if the output is on the network */
1666 if (stream
->metadata_flag
) {
1667 size_t metadata_payload_size
=
1668 sizeof(struct lttcomm_relayd_metadata_payload
);
1670 /* Update counter to fit the spliced data */
1671 ret_splice
+= metadata_payload_size
;
1672 len
+= metadata_payload_size
;
1674 * We do this so the return value can match the len passed as
1675 * argument to this function.
1677 written
-= metadata_payload_size
;
1681 /* Splice data out */
1682 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1683 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1684 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1685 if (ret_splice
< 0) {
1686 PERROR("Error in file splice");
1688 written
= ret_splice
;
1690 /* Socket operation failed. We consider the relayd dead */
1691 if (errno
== EBADF
|| errno
== EPIPE
) {
1692 WARN("Remote relayd disconnected. Stopping");
1698 } else if (ret_splice
> len
) {
1700 PERROR("Wrote more data than requested %zd (len: %lu)",
1702 written
+= ret_splice
;
1708 /* This call is useless on a socket so better save a syscall. */
1710 /* This won't block, but will start writeout asynchronously */
1711 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1712 SYNC_FILE_RANGE_WRITE
);
1713 stream
->out_fd_offset
+= ret_splice
;
1715 written
+= ret_splice
;
1717 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1725 * This is a special case that the relayd has closed its socket. Let's
1726 * cleanup the relayd object and all associated streams.
1728 if (relayd
&& relayd_hang_up
) {
1729 cleanup_relayd(relayd
, ctx
);
1730 /* Skip splice error so the consumer does not fail */
1735 /* send the appropriate error description to sessiond */
1738 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1741 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1744 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1749 if (relayd
&& stream
->metadata_flag
) {
1750 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1758 * Take a snapshot for a specific fd
1760 * Returns 0 on success, < 0 on error
1762 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1764 switch (consumer_data
.type
) {
1765 case LTTNG_CONSUMER_KERNEL
:
1766 return lttng_kconsumer_take_snapshot(stream
);
1767 case LTTNG_CONSUMER32_UST
:
1768 case LTTNG_CONSUMER64_UST
:
1769 return lttng_ustconsumer_take_snapshot(stream
);
1771 ERR("Unknown consumer_data type");
1778 * Get the produced position
1780 * Returns 0 on success, < 0 on error
1782 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1785 switch (consumer_data
.type
) {
1786 case LTTNG_CONSUMER_KERNEL
:
1787 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1788 case LTTNG_CONSUMER32_UST
:
1789 case LTTNG_CONSUMER64_UST
:
1790 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1792 ERR("Unknown consumer_data type");
1798 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1799 int sock
, struct pollfd
*consumer_sockpoll
)
1801 switch (consumer_data
.type
) {
1802 case LTTNG_CONSUMER_KERNEL
:
1803 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1804 case LTTNG_CONSUMER32_UST
:
1805 case LTTNG_CONSUMER64_UST
:
1806 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1808 ERR("Unknown consumer_data type");
1815 * Iterate over all streams of the hashtable and free them properly.
1817 * WARNING: *MUST* be used with data stream only.
1819 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1821 struct lttng_ht_iter iter
;
1822 struct lttng_consumer_stream
*stream
;
1829 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1831 * Ignore return value since we are currently cleaning up so any error
1834 (void) consumer_del_stream(stream
, ht
);
1838 lttng_ht_destroy(ht
);
1842 * Iterate over all streams of the hashtable and free them properly.
1844 * XXX: Should not be only for metadata stream or else use an other name.
1846 static void destroy_stream_ht(struct lttng_ht
*ht
)
1848 struct lttng_ht_iter iter
;
1849 struct lttng_consumer_stream
*stream
;
1856 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1858 * Ignore return value since we are currently cleaning up so any error
1861 (void) consumer_del_metadata_stream(stream
, ht
);
1865 lttng_ht_destroy(ht
);
1868 void lttng_consumer_close_metadata(void)
1870 switch (consumer_data
.type
) {
1871 case LTTNG_CONSUMER_KERNEL
:
1873 * The Kernel consumer has a different metadata scheme so we don't
1874 * close anything because the stream will be closed by the session
1878 case LTTNG_CONSUMER32_UST
:
1879 case LTTNG_CONSUMER64_UST
:
1881 * Close all metadata streams. The metadata hash table is passed and
1882 * this call iterates over it by closing all wakeup fd. This is safe
1883 * because at this point we are sure that the metadata producer is
1884 * either dead or blocked.
1886 lttng_ustconsumer_close_metadata(metadata_ht
);
1889 ERR("Unknown consumer_data type");
1895 * Clean up a metadata stream and free its memory.
1897 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1898 struct lttng_ht
*ht
)
1901 struct lttng_ht_iter iter
;
1902 struct lttng_consumer_channel
*free_chan
= NULL
;
1903 struct consumer_relayd_sock_pair
*relayd
;
1907 * This call should NEVER receive regular stream. It must always be
1908 * metadata stream and this is crucial for data structure synchronization.
1910 assert(stream
->metadata_flag
);
1912 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1915 /* Means the stream was allocated but not successfully added */
1916 goto free_stream_rcu
;
1919 pthread_mutex_lock(&consumer_data
.lock
);
1920 pthread_mutex_lock(&stream
->chan
->lock
);
1921 pthread_mutex_lock(&stream
->lock
);
1923 switch (consumer_data
.type
) {
1924 case LTTNG_CONSUMER_KERNEL
:
1925 if (stream
->mmap_base
!= NULL
) {
1926 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1928 PERROR("munmap metadata stream");
1932 if (stream
->wait_fd
>= 0) {
1933 ret
= close(stream
->wait_fd
);
1935 PERROR("close kernel metadata wait_fd");
1939 case LTTNG_CONSUMER32_UST
:
1940 case LTTNG_CONSUMER64_UST
:
1941 lttng_ustconsumer_del_stream(stream
);
1944 ERR("Unknown consumer_data type");
1950 iter
.iter
.node
= &stream
->node
.node
;
1951 ret
= lttng_ht_del(ht
, &iter
);
1954 iter
.iter
.node
= &stream
->node_channel_id
.node
;
1955 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
1958 iter
.iter
.node
= &stream
->node_session_id
.node
;
1959 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1963 if (stream
->out_fd
>= 0) {
1964 ret
= close(stream
->out_fd
);
1970 /* Check and cleanup relayd */
1972 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1973 if (relayd
!= NULL
) {
1974 uatomic_dec(&relayd
->refcount
);
1975 assert(uatomic_read(&relayd
->refcount
) >= 0);
1977 /* Closing streams requires to lock the control socket. */
1978 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1979 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1980 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1981 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1983 DBG("Unable to close stream on the relayd. Continuing");
1985 * Continue here. There is nothing we can do for the relayd.
1986 * Chances are that the relayd has closed the socket so we just
1987 * continue cleaning up.
1991 /* Both conditions are met, we destroy the relayd. */
1992 if (uatomic_read(&relayd
->refcount
) == 0 &&
1993 uatomic_read(&relayd
->destroy_flag
)) {
1994 destroy_relayd(relayd
);
1999 /* Atomically decrement channel refcount since other threads can use it. */
2000 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
2001 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
2002 /* Go for channel deletion! */
2003 free_chan
= stream
->chan
;
2008 * Nullify the stream reference so it is not used after deletion. The
2009 * channel lock MUST be acquired before being able to check for
2010 * a NULL pointer value.
2012 stream
->chan
->metadata_stream
= NULL
;
2014 pthread_mutex_unlock(&stream
->lock
);
2015 pthread_mutex_unlock(&stream
->chan
->lock
);
2016 pthread_mutex_unlock(&consumer_data
.lock
);
2019 consumer_del_channel(free_chan
);
2023 call_rcu(&stream
->node
.head
, free_stream_rcu
);
2027 * Action done with the metadata stream when adding it to the consumer internal
2028 * data structures to handle it.
2030 int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2032 struct lttng_ht
*ht
= metadata_ht
;
2034 struct consumer_relayd_sock_pair
*relayd
;
2035 struct lttng_ht_iter iter
;
2036 struct lttng_ht_node_u64
*node
;
2041 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2043 pthread_mutex_lock(&consumer_data
.lock
);
2044 pthread_mutex_lock(&stream
->chan
->lock
);
2045 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2046 pthread_mutex_lock(&stream
->lock
);
2049 * From here, refcounts are updated so be _careful_ when returning an error
2056 * Lookup the stream just to make sure it does not exist in our internal
2057 * state. This should NEVER happen.
2059 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2060 node
= lttng_ht_iter_get_node_u64(&iter
);
2063 /* Find relayd and, if one is found, increment refcount. */
2064 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
2065 if (relayd
!= NULL
) {
2066 uatomic_inc(&relayd
->refcount
);
2070 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2071 * in terms of destroying the associated channel, because the action that
2072 * causes the count to become 0 also causes a stream to be added. The
2073 * channel deletion will thus be triggered by the following removal of this
2076 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2077 /* Increment refcount before decrementing nb_init_stream_left */
2079 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2082 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2084 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2085 &stream
->node_channel_id
);
2088 * Add stream to the stream_list_ht of the consumer data. No need to steal
2089 * the key since the HT does not use it and we allow to add redundant keys
2092 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2096 pthread_mutex_unlock(&stream
->lock
);
2097 pthread_mutex_unlock(&stream
->chan
->lock
);
2098 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2099 pthread_mutex_unlock(&consumer_data
.lock
);
2104 * Delete data stream that are flagged for deletion (endpoint_status).
2106 static void validate_endpoint_status_data_stream(void)
2108 struct lttng_ht_iter iter
;
2109 struct lttng_consumer_stream
*stream
;
2111 DBG("Consumer delete flagged data stream");
2114 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2115 /* Validate delete flag of the stream */
2116 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2119 /* Delete it right now */
2120 consumer_del_stream(stream
, data_ht
);
2126 * Delete metadata stream that are flagged for deletion (endpoint_status).
2128 static void validate_endpoint_status_metadata_stream(
2129 struct lttng_poll_event
*pollset
)
2131 struct lttng_ht_iter iter
;
2132 struct lttng_consumer_stream
*stream
;
2134 DBG("Consumer delete flagged metadata stream");
2139 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2140 /* Validate delete flag of the stream */
2141 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2145 * Remove from pollset so the metadata thread can continue without
2146 * blocking on a deleted stream.
2148 lttng_poll_del(pollset
, stream
->wait_fd
);
2150 /* Delete it right now */
2151 consumer_del_metadata_stream(stream
, metadata_ht
);
2157 * Thread polls on metadata file descriptor and write them on disk or on the
2160 void *consumer_thread_metadata_poll(void *data
)
2163 uint32_t revents
, nb_fd
;
2164 struct lttng_consumer_stream
*stream
= NULL
;
2165 struct lttng_ht_iter iter
;
2166 struct lttng_ht_node_u64
*node
;
2167 struct lttng_poll_event events
;
2168 struct lttng_consumer_local_data
*ctx
= data
;
2171 rcu_register_thread();
2173 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2175 /* ENOMEM at this point. Better to bail out. */
2179 DBG("Thread metadata poll started");
2181 /* Size is set to 1 for the consumer_metadata pipe */
2182 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2184 ERR("Poll set creation failed");
2188 ret
= lttng_poll_add(&events
,
2189 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2195 DBG("Metadata main loop started");
2198 /* Only the metadata pipe is set */
2199 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2204 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2205 ret
= lttng_poll_wait(&events
, -1);
2206 DBG("Metadata event catched in thread");
2208 if (errno
== EINTR
) {
2209 ERR("Poll EINTR catched");
2217 /* From here, the event is a metadata wait fd */
2218 for (i
= 0; i
< nb_fd
; i
++) {
2219 revents
= LTTNG_POLL_GETEV(&events
, i
);
2220 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2222 /* Just don't waste time if no returned events for the fd */
2227 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2228 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2229 DBG("Metadata thread pipe hung up");
2231 * Remove the pipe from the poll set and continue the loop
2232 * since their might be data to consume.
2234 lttng_poll_del(&events
,
2235 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2236 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2238 } else if (revents
& LPOLLIN
) {
2241 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2242 &stream
, sizeof(stream
));
2244 ERR("read metadata stream, ret: %zd", pipe_len
);
2246 * Continue here to handle the rest of the streams.
2251 /* A NULL stream means that the state has changed. */
2252 if (stream
== NULL
) {
2253 /* Check for deleted streams. */
2254 validate_endpoint_status_metadata_stream(&events
);
2258 DBG("Adding metadata stream %d to poll set",
2261 /* Add metadata stream to the global poll events list */
2262 lttng_poll_add(&events
, stream
->wait_fd
,
2263 LPOLLIN
| LPOLLPRI
);
2266 /* Handle other stream */
2272 uint64_t tmp_id
= (uint64_t) pollfd
;
2274 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2276 node
= lttng_ht_iter_get_node_u64(&iter
);
2279 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2282 /* Check for error event */
2283 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2284 DBG("Metadata fd %d is hup|err.", pollfd
);
2285 if (!stream
->hangup_flush_done
2286 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2287 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2288 DBG("Attempting to flush and consume the UST buffers");
2289 lttng_ustconsumer_on_stream_hangup(stream
);
2291 /* We just flushed the stream now read it. */
2293 len
= ctx
->on_buffer_ready(stream
, ctx
);
2295 * We don't check the return value here since if we get
2296 * a negative len, it means an error occured thus we
2297 * simply remove it from the poll set and free the
2303 lttng_poll_del(&events
, stream
->wait_fd
);
2305 * This call update the channel states, closes file descriptors
2306 * and securely free the stream.
2308 consumer_del_metadata_stream(stream
, metadata_ht
);
2309 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2310 /* Get the data out of the metadata file descriptor */
2311 DBG("Metadata available on fd %d", pollfd
);
2312 assert(stream
->wait_fd
== pollfd
);
2314 len
= ctx
->on_buffer_ready(stream
, ctx
);
2315 /* It's ok to have an unavailable sub-buffer */
2316 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2317 /* Clean up stream from consumer and free it. */
2318 lttng_poll_del(&events
, stream
->wait_fd
);
2319 consumer_del_metadata_stream(stream
, metadata_ht
);
2320 } else if (len
> 0) {
2321 stream
->data_read
= 1;
2325 /* Release RCU lock for the stream looked up */
2332 DBG("Metadata poll thread exiting");
2334 lttng_poll_clean(&events
);
2336 destroy_stream_ht(metadata_ht
);
2338 rcu_unregister_thread();
2343 * This thread polls the fds in the set to consume the data and write
2344 * it to tracefile if necessary.
2346 void *consumer_thread_data_poll(void *data
)
2348 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2349 struct pollfd
*pollfd
= NULL
;
2350 /* local view of the streams */
2351 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2352 /* local view of consumer_data.fds_count */
2354 struct lttng_consumer_local_data
*ctx
= data
;
2357 rcu_register_thread();
2359 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2360 if (data_ht
== NULL
) {
2361 /* ENOMEM at this point. Better to bail out. */
2365 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2366 if (local_stream
== NULL
) {
2367 PERROR("local_stream malloc");
2376 * the fds set has been updated, we need to update our
2377 * local array as well
2379 pthread_mutex_lock(&consumer_data
.lock
);
2380 if (consumer_data
.need_update
) {
2385 local_stream
= NULL
;
2387 /* allocate for all fds + 1 for the consumer_data_pipe */
2388 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2389 if (pollfd
== NULL
) {
2390 PERROR("pollfd malloc");
2391 pthread_mutex_unlock(&consumer_data
.lock
);
2395 /* allocate for all fds + 1 for the consumer_data_pipe */
2396 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2397 sizeof(struct lttng_consumer_stream
*));
2398 if (local_stream
== NULL
) {
2399 PERROR("local_stream malloc");
2400 pthread_mutex_unlock(&consumer_data
.lock
);
2403 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2406 ERR("Error in allocating pollfd or local_outfds");
2407 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2408 pthread_mutex_unlock(&consumer_data
.lock
);
2412 consumer_data
.need_update
= 0;
2414 pthread_mutex_unlock(&consumer_data
.lock
);
2416 /* No FDs and consumer_quit, consumer_cleanup the thread */
2417 if (nb_fd
== 0 && consumer_quit
== 1) {
2420 /* poll on the array of fds */
2422 DBG("polling on %d fd", nb_fd
+ 1);
2423 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2424 DBG("poll num_rdy : %d", num_rdy
);
2425 if (num_rdy
== -1) {
2427 * Restart interrupted system call.
2429 if (errno
== EINTR
) {
2432 PERROR("Poll error");
2433 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2435 } else if (num_rdy
== 0) {
2436 DBG("Polling thread timed out");
2441 * If the consumer_data_pipe triggered poll go directly to the
2442 * beginning of the loop to update the array. We want to prioritize
2443 * array update over low-priority reads.
2445 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2446 ssize_t pipe_readlen
;
2448 DBG("consumer_data_pipe wake up");
2449 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2450 &new_stream
, sizeof(new_stream
));
2451 if (pipe_readlen
< 0) {
2452 ERR("Consumer data pipe ret %zd", pipe_readlen
);
2453 /* Continue so we can at least handle the current stream(s). */
2458 * If the stream is NULL, just ignore it. It's also possible that
2459 * the sessiond poll thread changed the consumer_quit state and is
2460 * waking us up to test it.
2462 if (new_stream
== NULL
) {
2463 validate_endpoint_status_data_stream();
2467 /* Continue to update the local streams and handle prio ones */
2471 /* Take care of high priority channels first. */
2472 for (i
= 0; i
< nb_fd
; i
++) {
2473 if (local_stream
[i
] == NULL
) {
2476 if (pollfd
[i
].revents
& POLLPRI
) {
2477 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2479 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2480 /* it's ok to have an unavailable sub-buffer */
2481 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2482 /* Clean the stream and free it. */
2483 consumer_del_stream(local_stream
[i
], data_ht
);
2484 local_stream
[i
] = NULL
;
2485 } else if (len
> 0) {
2486 local_stream
[i
]->data_read
= 1;
2492 * If we read high prio channel in this loop, try again
2493 * for more high prio data.
2499 /* Take care of low priority channels. */
2500 for (i
= 0; i
< nb_fd
; i
++) {
2501 if (local_stream
[i
] == NULL
) {
2504 if ((pollfd
[i
].revents
& POLLIN
) ||
2505 local_stream
[i
]->hangup_flush_done
) {
2506 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2507 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2508 /* it's ok to have an unavailable sub-buffer */
2509 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2510 /* Clean the stream and free it. */
2511 consumer_del_stream(local_stream
[i
], data_ht
);
2512 local_stream
[i
] = NULL
;
2513 } else if (len
> 0) {
2514 local_stream
[i
]->data_read
= 1;
2519 /* Handle hangup and errors */
2520 for (i
= 0; i
< nb_fd
; i
++) {
2521 if (local_stream
[i
] == NULL
) {
2524 if (!local_stream
[i
]->hangup_flush_done
2525 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2526 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2527 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2528 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2530 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2531 /* Attempt read again, for the data we just flushed. */
2532 local_stream
[i
]->data_read
= 1;
2535 * If the poll flag is HUP/ERR/NVAL and we have
2536 * read no data in this pass, we can remove the
2537 * stream from its hash table.
2539 if ((pollfd
[i
].revents
& POLLHUP
)) {
2540 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2541 if (!local_stream
[i
]->data_read
) {
2542 consumer_del_stream(local_stream
[i
], data_ht
);
2543 local_stream
[i
] = NULL
;
2546 } else if (pollfd
[i
].revents
& POLLERR
) {
2547 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2548 if (!local_stream
[i
]->data_read
) {
2549 consumer_del_stream(local_stream
[i
], data_ht
);
2550 local_stream
[i
] = NULL
;
2553 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2554 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2555 if (!local_stream
[i
]->data_read
) {
2556 consumer_del_stream(local_stream
[i
], data_ht
);
2557 local_stream
[i
] = NULL
;
2561 if (local_stream
[i
] != NULL
) {
2562 local_stream
[i
]->data_read
= 0;
2567 DBG("polling thread exiting");
2572 * Close the write side of the pipe so epoll_wait() in
2573 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2574 * read side of the pipe. If we close them both, epoll_wait strangely does
2575 * not return and could create a endless wait period if the pipe is the
2576 * only tracked fd in the poll set. The thread will take care of closing
2579 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2581 destroy_data_stream_ht(data_ht
);
2583 rcu_unregister_thread();
2588 * Close wake-up end of each stream belonging to the channel. This will
2589 * allow the poll() on the stream read-side to detect when the
2590 * write-side (application) finally closes them.
2593 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2595 struct lttng_ht
*ht
;
2596 struct lttng_consumer_stream
*stream
;
2597 struct lttng_ht_iter iter
;
2599 ht
= consumer_data
.stream_per_chan_id_ht
;
2602 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2603 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2604 ht
->match_fct
, &channel
->key
,
2605 &iter
.iter
, stream
, node_channel_id
.node
) {
2607 * Protect against teardown with mutex.
2609 pthread_mutex_lock(&stream
->lock
);
2610 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2613 switch (consumer_data
.type
) {
2614 case LTTNG_CONSUMER_KERNEL
:
2616 case LTTNG_CONSUMER32_UST
:
2617 case LTTNG_CONSUMER64_UST
:
2619 * Note: a mutex is taken internally within
2620 * liblttng-ust-ctl to protect timer wakeup_fd
2621 * use from concurrent close.
2623 lttng_ustconsumer_close_stream_wakeup(stream
);
2626 ERR("Unknown consumer_data type");
2630 pthread_mutex_unlock(&stream
->lock
);
2635 static void destroy_channel_ht(struct lttng_ht
*ht
)
2637 struct lttng_ht_iter iter
;
2638 struct lttng_consumer_channel
*channel
;
2646 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2647 ret
= lttng_ht_del(ht
, &iter
);
2652 lttng_ht_destroy(ht
);
2656 * This thread polls the channel fds to detect when they are being
2657 * closed. It closes all related streams if the channel is detected as
2658 * closed. It is currently only used as a shim layer for UST because the
2659 * consumerd needs to keep the per-stream wakeup end of pipes open for
2662 void *consumer_thread_channel_poll(void *data
)
2665 uint32_t revents
, nb_fd
;
2666 struct lttng_consumer_channel
*chan
= NULL
;
2667 struct lttng_ht_iter iter
;
2668 struct lttng_ht_node_u64
*node
;
2669 struct lttng_poll_event events
;
2670 struct lttng_consumer_local_data
*ctx
= data
;
2671 struct lttng_ht
*channel_ht
;
2673 rcu_register_thread();
2675 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2677 /* ENOMEM at this point. Better to bail out. */
2681 DBG("Thread channel poll started");
2683 /* Size is set to 1 for the consumer_channel pipe */
2684 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2686 ERR("Poll set creation failed");
2690 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2696 DBG("Channel main loop started");
2699 /* Only the channel pipe is set */
2700 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2705 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2706 ret
= lttng_poll_wait(&events
, -1);
2707 DBG("Channel event catched in thread");
2709 if (errno
== EINTR
) {
2710 ERR("Poll EINTR catched");
2718 /* From here, the event is a channel wait fd */
2719 for (i
= 0; i
< nb_fd
; i
++) {
2720 revents
= LTTNG_POLL_GETEV(&events
, i
);
2721 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2723 /* Just don't waste time if no returned events for the fd */
2727 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2728 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2729 DBG("Channel thread pipe hung up");
2731 * Remove the pipe from the poll set and continue the loop
2732 * since their might be data to consume.
2734 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2736 } else if (revents
& LPOLLIN
) {
2737 enum consumer_channel_action action
;
2740 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2742 ERR("Error reading channel pipe");
2747 case CONSUMER_CHANNEL_ADD
:
2748 DBG("Adding channel %d to poll set",
2751 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2754 lttng_ht_add_unique_u64(channel_ht
,
2755 &chan
->wait_fd_node
);
2757 /* Add channel to the global poll events list */
2758 lttng_poll_add(&events
, chan
->wait_fd
,
2759 LPOLLIN
| LPOLLPRI
);
2761 case CONSUMER_CHANNEL_DEL
:
2763 struct lttng_consumer_stream
*stream
, *stmp
;
2766 chan
= consumer_find_channel(key
);
2769 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2772 lttng_poll_del(&events
, chan
->wait_fd
);
2773 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2774 ret
= lttng_ht_del(channel_ht
, &iter
);
2776 consumer_close_channel_streams(chan
);
2778 switch (consumer_data
.type
) {
2779 case LTTNG_CONSUMER_KERNEL
:
2781 case LTTNG_CONSUMER32_UST
:
2782 case LTTNG_CONSUMER64_UST
:
2783 /* Delete streams that might have been left in the stream list. */
2784 cds_list_for_each_entry_safe(stream
, stmp
, &chan
->streams
.head
,
2786 cds_list_del(&stream
->send_node
);
2787 lttng_ustconsumer_del_stream(stream
);
2788 uatomic_sub(&stream
->chan
->refcount
, 1);
2789 assert(&chan
->refcount
);
2794 ERR("Unknown consumer_data type");
2799 * Release our own refcount. Force channel deletion even if
2800 * streams were not initialized.
2802 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2803 consumer_del_channel(chan
);
2808 case CONSUMER_CHANNEL_QUIT
:
2810 * Remove the pipe from the poll set and continue the loop
2811 * since their might be data to consume.
2813 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2816 ERR("Unknown action");
2821 /* Handle other stream */
2827 uint64_t tmp_id
= (uint64_t) pollfd
;
2829 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2831 node
= lttng_ht_iter_get_node_u64(&iter
);
2834 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2837 /* Check for error event */
2838 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2839 DBG("Channel fd %d is hup|err.", pollfd
);
2841 lttng_poll_del(&events
, chan
->wait_fd
);
2842 ret
= lttng_ht_del(channel_ht
, &iter
);
2844 assert(cds_list_empty(&chan
->streams
.head
));
2845 consumer_close_channel_streams(chan
);
2847 /* Release our own refcount */
2848 if (!uatomic_sub_return(&chan
->refcount
, 1)
2849 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2850 consumer_del_channel(chan
);
2854 /* Release RCU lock for the channel looked up */
2860 lttng_poll_clean(&events
);
2862 destroy_channel_ht(channel_ht
);
2864 DBG("Channel poll thread exiting");
2865 rcu_unregister_thread();
2869 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2870 struct pollfd
*sockpoll
, int client_socket
)
2877 if (lttng_consumer_poll_socket(sockpoll
) < 0) {
2881 DBG("Metadata connection on client_socket");
2883 /* Blocking call, waiting for transmission */
2884 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2885 if (ctx
->consumer_metadata_socket
< 0) {
2886 WARN("On accept metadata");
2897 * This thread listens on the consumerd socket and receives the file
2898 * descriptors from the session daemon.
2900 void *consumer_thread_sessiond_poll(void *data
)
2902 int sock
= -1, client_socket
, ret
;
2904 * structure to poll for incoming data on communication socket avoids
2905 * making blocking sockets.
2907 struct pollfd consumer_sockpoll
[2];
2908 struct lttng_consumer_local_data
*ctx
= data
;
2910 rcu_register_thread();
2912 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2913 unlink(ctx
->consumer_command_sock_path
);
2914 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2915 if (client_socket
< 0) {
2916 ERR("Cannot create command socket");
2920 ret
= lttcomm_listen_unix_sock(client_socket
);
2925 DBG("Sending ready command to lttng-sessiond");
2926 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2927 /* return < 0 on error, but == 0 is not fatal */
2929 ERR("Error sending ready command to lttng-sessiond");
2933 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2934 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2935 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2936 consumer_sockpoll
[1].fd
= client_socket
;
2937 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2939 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2942 DBG("Connection on client_socket");
2944 /* Blocking call, waiting for transmission */
2945 sock
= lttcomm_accept_unix_sock(client_socket
);
2952 * Setup metadata socket which is the second socket connection on the
2953 * command unix socket.
2955 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
2960 /* This socket is not useful anymore. */
2961 ret
= close(client_socket
);
2963 PERROR("close client_socket");
2967 /* update the polling structure to poll on the established socket */
2968 consumer_sockpoll
[1].fd
= sock
;
2969 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2972 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2975 DBG("Incoming command on sock");
2976 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2977 if (ret
== -ENOENT
) {
2978 DBG("Received STOP command");
2983 * This could simply be a session daemon quitting. Don't output
2986 DBG("Communication interrupted on command socket");
2989 if (consumer_quit
) {
2990 DBG("consumer_thread_receive_fds received quit from signal");
2993 DBG("received command on sock");
2996 DBG("Consumer thread sessiond poll exiting");
2999 * Close metadata streams since the producer is the session daemon which
3002 * NOTE: for now, this only applies to the UST tracer.
3004 lttng_consumer_close_metadata();
3007 * when all fds have hung up, the polling thread
3013 * Notify the data poll thread to poll back again and test the
3014 * consumer_quit state that we just set so to quit gracefully.
3016 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3018 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3020 /* Cleaning up possibly open sockets. */
3024 PERROR("close sock sessiond poll");
3027 if (client_socket
>= 0) {
3028 ret
= close(client_socket
);
3030 PERROR("close client_socket sessiond poll");
3034 rcu_unregister_thread();
3038 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3039 struct lttng_consumer_local_data
*ctx
)
3043 pthread_mutex_lock(&stream
->lock
);
3045 switch (consumer_data
.type
) {
3046 case LTTNG_CONSUMER_KERNEL
:
3047 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3049 case LTTNG_CONSUMER32_UST
:
3050 case LTTNG_CONSUMER64_UST
:
3051 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3054 ERR("Unknown consumer_data type");
3060 pthread_mutex_unlock(&stream
->lock
);
3064 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3066 switch (consumer_data
.type
) {
3067 case LTTNG_CONSUMER_KERNEL
:
3068 return lttng_kconsumer_on_recv_stream(stream
);
3069 case LTTNG_CONSUMER32_UST
:
3070 case LTTNG_CONSUMER64_UST
:
3071 return lttng_ustconsumer_on_recv_stream(stream
);
3073 ERR("Unknown consumer_data type");
3080 * Allocate and set consumer data hash tables.
3082 void lttng_consumer_init(void)
3084 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3085 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3086 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3087 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3091 * Process the ADD_RELAYD command receive by a consumer.
3093 * This will create a relayd socket pair and add it to the relayd hash table.
3094 * The caller MUST acquire a RCU read side lock before calling it.
3096 int consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3097 struct lttng_consumer_local_data
*ctx
, int sock
,
3098 struct pollfd
*consumer_sockpoll
,
3099 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
)
3101 int fd
= -1, ret
= -1, relayd_created
= 0;
3102 enum lttng_error_code ret_code
= LTTNG_OK
;
3103 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3106 assert(relayd_sock
);
3108 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3110 /* Get relayd reference if exists. */
3111 relayd
= consumer_find_relayd(net_seq_idx
);
3112 if (relayd
== NULL
) {
3113 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3114 /* Not found. Allocate one. */
3115 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3116 if (relayd
== NULL
) {
3118 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3121 relayd
->sessiond_session_id
= sessiond_id
;
3126 * This code path MUST continue to the consumer send status message to
3127 * we can notify the session daemon and continue our work without
3128 * killing everything.
3132 * relayd key should never be found for control socket.
3134 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3137 /* First send a status message before receiving the fds. */
3138 ret
= consumer_send_status_msg(sock
, LTTNG_OK
);
3140 /* Somehow, the session daemon is not responding anymore. */
3141 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3142 goto error_nosignal
;
3145 /* Poll on consumer socket. */
3146 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
3147 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3149 goto error_nosignal
;
3152 /* Get relayd socket from session daemon */
3153 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3154 if (ret
!= sizeof(fd
)) {
3156 fd
= -1; /* Just in case it gets set with an invalid value. */
3159 * Failing to receive FDs might indicate a major problem such as
3160 * reaching a fd limit during the receive where the kernel returns a
3161 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3162 * don't take any chances and stop everything.
3164 * XXX: Feature request #558 will fix that and avoid this possible
3165 * issue when reaching the fd limit.
3167 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3168 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3172 /* Copy socket information and received FD */
3173 switch (sock_type
) {
3174 case LTTNG_STREAM_CONTROL
:
3175 /* Copy received lttcomm socket */
3176 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3177 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3178 /* Handle create_sock error. */
3180 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3184 * Close the socket created internally by
3185 * lttcomm_create_sock, so we can replace it by the one
3186 * received from sessiond.
3188 if (close(relayd
->control_sock
.sock
.fd
)) {
3192 /* Assign new file descriptor */
3193 relayd
->control_sock
.sock
.fd
= fd
;
3194 fd
= -1; /* For error path */
3195 /* Assign version values. */
3196 relayd
->control_sock
.major
= relayd_sock
->major
;
3197 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3200 * Create a session on the relayd and store the returned id. Lock the
3201 * control socket mutex if the relayd was NOT created before.
3203 if (!relayd_created
) {
3204 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3206 ret
= relayd_create_session(&relayd
->control_sock
,
3207 &relayd
->relayd_session_id
);
3208 if (!relayd_created
) {
3209 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3213 * Close all sockets of a relayd object. It will be freed if it was
3214 * created at the error code path or else it will be garbage
3217 (void) relayd_close(&relayd
->control_sock
);
3218 (void) relayd_close(&relayd
->data_sock
);
3219 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
3224 case LTTNG_STREAM_DATA
:
3225 /* Copy received lttcomm socket */
3226 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3227 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3228 /* Handle create_sock error. */
3230 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3234 * Close the socket created internally by
3235 * lttcomm_create_sock, so we can replace it by the one
3236 * received from sessiond.
3238 if (close(relayd
->data_sock
.sock
.fd
)) {
3242 /* Assign new file descriptor */
3243 relayd
->data_sock
.sock
.fd
= fd
;
3244 fd
= -1; /* for eventual error paths */
3245 /* Assign version values. */
3246 relayd
->data_sock
.major
= relayd_sock
->major
;
3247 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3250 ERR("Unknown relayd socket type (%d)", sock_type
);
3252 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3256 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3257 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3258 relayd
->net_seq_idx
, fd
);
3260 /* We successfully added the socket. Send status back. */
3261 ret
= consumer_send_status_msg(sock
, ret_code
);
3263 /* Somehow, the session daemon is not responding anymore. */
3264 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3265 goto error_nosignal
;
3269 * Add relayd socket pair to consumer data hashtable. If object already
3270 * exists or on error, the function gracefully returns.
3278 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3279 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3283 /* Close received socket if valid. */
3286 PERROR("close received socket");
3290 if (relayd_created
) {
3298 * Try to lock the stream mutex.
3300 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3302 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3309 * Try to lock the stream mutex. On failure, we know that the stream is
3310 * being used else where hence there is data still being extracted.
3312 ret
= pthread_mutex_trylock(&stream
->lock
);
3314 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3326 * Search for a relayd associated to the session id and return the reference.
3328 * A rcu read side lock MUST be acquire before calling this function and locked
3329 * until the relayd object is no longer necessary.
3331 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3333 struct lttng_ht_iter iter
;
3334 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3336 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3337 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3340 * Check by sessiond id which is unique here where the relayd session
3341 * id might not be when having multiple relayd.
3343 if (relayd
->sessiond_session_id
== id
) {
3344 /* Found the relayd. There can be only one per id. */
3356 * Check if for a given session id there is still data needed to be extract
3359 * Return 1 if data is pending or else 0 meaning ready to be read.
3361 int consumer_data_pending(uint64_t id
)
3364 struct lttng_ht_iter iter
;
3365 struct lttng_ht
*ht
;
3366 struct lttng_consumer_stream
*stream
;
3367 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3368 int (*data_pending
)(struct lttng_consumer_stream
*);
3370 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3373 pthread_mutex_lock(&consumer_data
.lock
);
3375 switch (consumer_data
.type
) {
3376 case LTTNG_CONSUMER_KERNEL
:
3377 data_pending
= lttng_kconsumer_data_pending
;
3379 case LTTNG_CONSUMER32_UST
:
3380 case LTTNG_CONSUMER64_UST
:
3381 data_pending
= lttng_ustconsumer_data_pending
;
3384 ERR("Unknown consumer data type");
3388 /* Ease our life a bit */
3389 ht
= consumer_data
.stream_list_ht
;
3391 relayd
= find_relayd_by_session_id(id
);
3393 /* Send init command for data pending. */
3394 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3395 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3396 relayd
->relayd_session_id
);
3397 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3399 /* Communication error thus the relayd so no data pending. */
3400 goto data_not_pending
;
3404 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3405 ht
->hash_fct(&id
, lttng_ht_seed
),
3407 &iter
.iter
, stream
, node_session_id
.node
) {
3408 /* If this call fails, the stream is being used hence data pending. */
3409 ret
= stream_try_lock(stream
);
3415 * A removed node from the hash table indicates that the stream has
3416 * been deleted thus having a guarantee that the buffers are closed
3417 * on the consumer side. However, data can still be transmitted
3418 * over the network so don't skip the relayd check.
3420 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3422 /* Check the stream if there is data in the buffers. */
3423 ret
= data_pending(stream
);
3425 pthread_mutex_unlock(&stream
->lock
);
3432 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3433 if (stream
->metadata_flag
) {
3434 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3435 stream
->relayd_stream_id
);
3437 ret
= relayd_data_pending(&relayd
->control_sock
,
3438 stream
->relayd_stream_id
,
3439 stream
->next_net_seq_num
- 1);
3441 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3443 pthread_mutex_unlock(&stream
->lock
);
3447 pthread_mutex_unlock(&stream
->lock
);
3451 unsigned int is_data_inflight
= 0;
3453 /* Send init command for data pending. */
3454 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3455 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3456 relayd
->relayd_session_id
, &is_data_inflight
);
3457 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3459 goto data_not_pending
;
3461 if (is_data_inflight
) {
3467 * Finding _no_ node in the hash table and no inflight data means that the
3468 * stream(s) have been removed thus data is guaranteed to be available for
3469 * analysis from the trace files.
3473 /* Data is available to be read by a viewer. */
3474 pthread_mutex_unlock(&consumer_data
.lock
);
3479 /* Data is still being extracted from buffers. */
3480 pthread_mutex_unlock(&consumer_data
.lock
);
3486 * Send a ret code status message to the sessiond daemon.
3488 * Return the sendmsg() return value.
3490 int consumer_send_status_msg(int sock
, int ret_code
)
3492 struct lttcomm_consumer_status_msg msg
;
3494 msg
.ret_code
= ret_code
;
3496 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3500 * Send a channel status message to the sessiond daemon.
3502 * Return the sendmsg() return value.
3504 int consumer_send_status_channel(int sock
,
3505 struct lttng_consumer_channel
*channel
)
3507 struct lttcomm_consumer_status_channel msg
;
3512 msg
.ret_code
= -LTTNG_ERR_UST_CHAN_FAIL
;
3514 msg
.ret_code
= LTTNG_OK
;
3515 msg
.key
= channel
->key
;
3516 msg
.stream_count
= channel
->streams
.count
;
3519 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));