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
->output_written
= 0;
608 stream
->state
= state
;
611 stream
->net_seq_idx
= relayd_id
;
612 stream
->session_id
= session_id
;
613 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
614 pthread_mutex_init(&stream
->lock
, NULL
);
616 /* If channel is the metadata, flag this stream as metadata. */
617 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
618 stream
->metadata_flag
= 1;
619 /* Metadata is flat out. */
620 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
622 /* Format stream name to <channel_name>_<cpu_number> */
623 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
626 PERROR("snprintf stream name");
631 /* Key is always the wait_fd for streams. */
632 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
634 /* Init node per channel id key */
635 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
637 /* Init session id node with the stream session id */
638 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
640 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
" relayd_id %" PRIu64
", session_id %" PRIu64
,
641 stream
->name
, stream
->key
, channel_key
, stream
->net_seq_idx
, stream
->session_id
);
657 * Add a stream to the global list protected by a mutex.
659 int consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
661 struct lttng_ht
*ht
= data_ht
;
663 struct consumer_relayd_sock_pair
*relayd
;
668 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
670 pthread_mutex_lock(&consumer_data
.lock
);
671 pthread_mutex_lock(&stream
->chan
->lock
);
672 pthread_mutex_lock(&stream
->chan
->timer_lock
);
673 pthread_mutex_lock(&stream
->lock
);
676 /* Steal stream identifier to avoid having streams with the same key */
677 steal_stream_key(stream
->key
, ht
);
679 lttng_ht_add_unique_u64(ht
, &stream
->node
);
681 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
682 &stream
->node_channel_id
);
685 * Add stream to the stream_list_ht of the consumer data. No need to steal
686 * the key since the HT does not use it and we allow to add redundant keys
689 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
691 /* Check and cleanup relayd */
692 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
693 if (relayd
!= NULL
) {
694 uatomic_inc(&relayd
->refcount
);
698 * When nb_init_stream_left reaches 0, we don't need to trigger any action
699 * in terms of destroying the associated channel, because the action that
700 * causes the count to become 0 also causes a stream to be added. The
701 * channel deletion will thus be triggered by the following removal of this
704 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
705 /* Increment refcount before decrementing nb_init_stream_left */
707 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
710 /* Update consumer data once the node is inserted. */
711 consumer_data
.stream_count
++;
712 consumer_data
.need_update
= 1;
715 pthread_mutex_unlock(&stream
->lock
);
716 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
717 pthread_mutex_unlock(&stream
->chan
->lock
);
718 pthread_mutex_unlock(&consumer_data
.lock
);
723 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
725 consumer_del_stream(stream
, data_ht
);
729 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
730 * be acquired before calling this.
732 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
735 struct lttng_ht_node_u64
*node
;
736 struct lttng_ht_iter iter
;
740 lttng_ht_lookup(consumer_data
.relayd_ht
,
741 &relayd
->net_seq_idx
, &iter
);
742 node
= lttng_ht_iter_get_node_u64(&iter
);
746 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
753 * Allocate and return a consumer relayd socket.
755 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
756 uint64_t net_seq_idx
)
758 struct consumer_relayd_sock_pair
*obj
= NULL
;
760 /* net sequence index of -1 is a failure */
761 if (net_seq_idx
== (uint64_t) -1ULL) {
765 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
767 PERROR("zmalloc relayd sock");
771 obj
->net_seq_idx
= net_seq_idx
;
773 obj
->destroy_flag
= 0;
774 obj
->control_sock
.sock
.fd
= -1;
775 obj
->data_sock
.sock
.fd
= -1;
776 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
777 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
784 * Find a relayd socket pair in the global consumer data.
786 * Return the object if found else NULL.
787 * RCU read-side lock must be held across this call and while using the
790 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
792 struct lttng_ht_iter iter
;
793 struct lttng_ht_node_u64
*node
;
794 struct consumer_relayd_sock_pair
*relayd
= NULL
;
796 /* Negative keys are lookup failures */
797 if (key
== (uint64_t) -1ULL) {
801 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
803 node
= lttng_ht_iter_get_node_u64(&iter
);
805 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
813 * Handle stream for relayd transmission if the stream applies for network
814 * streaming where the net sequence index is set.
816 * Return destination file descriptor or negative value on error.
818 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
819 size_t data_size
, unsigned long padding
,
820 struct consumer_relayd_sock_pair
*relayd
)
823 struct lttcomm_relayd_data_hdr data_hdr
;
829 /* Reset data header */
830 memset(&data_hdr
, 0, sizeof(data_hdr
));
832 if (stream
->metadata_flag
) {
833 /* Caller MUST acquire the relayd control socket lock */
834 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
839 /* Metadata are always sent on the control socket. */
840 outfd
= relayd
->control_sock
.sock
.fd
;
842 /* Set header with stream information */
843 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
844 data_hdr
.data_size
= htobe32(data_size
);
845 data_hdr
.padding_size
= htobe32(padding
);
847 * Note that net_seq_num below is assigned with the *current* value of
848 * next_net_seq_num and only after that the next_net_seq_num will be
849 * increment. This is why when issuing a command on the relayd using
850 * this next value, 1 should always be substracted in order to compare
851 * the last seen sequence number on the relayd side to the last sent.
853 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
854 /* Other fields are zeroed previously */
856 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
862 ++stream
->next_net_seq_num
;
864 /* Set to go on data socket */
865 outfd
= relayd
->data_sock
.sock
.fd
;
873 * Allocate and return a new lttng_consumer_channel object using the given key
874 * to initialize the hash table node.
876 * On error, return NULL.
878 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
880 const char *pathname
,
885 enum lttng_event_output output
,
886 uint64_t tracefile_size
,
887 uint64_t tracefile_count
,
888 uint64_t session_id_per_pid
)
890 struct lttng_consumer_channel
*channel
;
892 channel
= zmalloc(sizeof(*channel
));
893 if (channel
== NULL
) {
894 PERROR("malloc struct lttng_consumer_channel");
899 channel
->refcount
= 0;
900 channel
->session_id
= session_id
;
901 channel
->session_id_per_pid
= session_id_per_pid
;
904 channel
->relayd_id
= relayd_id
;
905 channel
->output
= output
;
906 channel
->tracefile_size
= tracefile_size
;
907 channel
->tracefile_count
= tracefile_count
;
908 pthread_mutex_init(&channel
->lock
, NULL
);
909 pthread_mutex_init(&channel
->timer_lock
, NULL
);
911 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
912 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
914 strncpy(channel
->name
, name
, sizeof(channel
->name
));
915 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
917 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
919 channel
->wait_fd
= -1;
921 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
923 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
930 * Add a channel to the global list protected by a mutex.
932 * On success 0 is returned else a negative value.
934 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
935 struct lttng_consumer_local_data
*ctx
)
938 struct lttng_ht_node_u64
*node
;
939 struct lttng_ht_iter iter
;
941 pthread_mutex_lock(&consumer_data
.lock
);
942 pthread_mutex_lock(&channel
->lock
);
943 pthread_mutex_lock(&channel
->timer_lock
);
946 lttng_ht_lookup(consumer_data
.channel_ht
, &channel
->key
, &iter
);
947 node
= lttng_ht_iter_get_node_u64(&iter
);
949 /* Channel already exist. Ignore the insertion */
950 ERR("Consumer add channel key %" PRIu64
" already exists!",
956 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
960 pthread_mutex_unlock(&channel
->timer_lock
);
961 pthread_mutex_unlock(&channel
->lock
);
962 pthread_mutex_unlock(&consumer_data
.lock
);
964 if (!ret
&& channel
->wait_fd
!= -1 &&
965 channel
->metadata_stream
== NULL
) {
966 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
972 * Allocate the pollfd structure and the local view of the out fds to avoid
973 * doing a lookup in the linked list and concurrency issues when writing is
974 * needed. Called with consumer_data.lock held.
976 * Returns the number of fds in the structures.
978 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
979 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
983 struct lttng_ht_iter iter
;
984 struct lttng_consumer_stream
*stream
;
989 assert(local_stream
);
991 DBG("Updating poll fd array");
993 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
995 * Only active streams with an active end point can be added to the
996 * poll set and local stream storage of the thread.
998 * There is a potential race here for endpoint_status to be updated
999 * just after the check. However, this is OK since the stream(s) will
1000 * be deleted once the thread is notified that the end point state has
1001 * changed where this function will be called back again.
1003 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
1004 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1008 * This clobbers way too much the debug output. Uncomment that if you
1009 * need it for debugging purposes.
1011 * DBG("Active FD %d", stream->wait_fd);
1013 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1014 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1015 local_stream
[i
] = stream
;
1021 * Insert the consumer_data_pipe at the end of the array and don't
1022 * increment i so nb_fd is the number of real FD.
1024 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1025 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1030 * Poll on the should_quit pipe and the command socket return -1 on error and
1031 * should exit, 0 if data is available on the command socket
1033 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1038 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1039 if (num_rdy
== -1) {
1041 * Restart interrupted system call.
1043 if (errno
== EINTR
) {
1046 PERROR("Poll error");
1049 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1050 DBG("consumer_should_quit wake up");
1060 * Set the error socket.
1062 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1065 ctx
->consumer_error_socket
= sock
;
1069 * Set the command socket path.
1071 void lttng_consumer_set_command_sock_path(
1072 struct lttng_consumer_local_data
*ctx
, char *sock
)
1074 ctx
->consumer_command_sock_path
= sock
;
1078 * Send return code to the session daemon.
1079 * If the socket is not defined, we return 0, it is not a fatal error
1081 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1083 if (ctx
->consumer_error_socket
> 0) {
1084 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1085 sizeof(enum lttcomm_sessiond_command
));
1092 * Close all the tracefiles and stream fds and MUST be called when all
1093 * instances are destroyed i.e. when all threads were joined and are ended.
1095 void lttng_consumer_cleanup(void)
1097 struct lttng_ht_iter iter
;
1098 struct lttng_consumer_channel
*channel
;
1102 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1104 consumer_del_channel(channel
);
1109 lttng_ht_destroy(consumer_data
.channel_ht
);
1111 cleanup_relayd_ht();
1113 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1116 * This HT contains streams that are freed by either the metadata thread or
1117 * the data thread so we do *nothing* on the hash table and simply destroy
1120 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1124 * Called from signal handler.
1126 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1131 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1132 } while (ret
< 0 && errno
== EINTR
);
1133 if (ret
< 0 || ret
!= 1) {
1134 PERROR("write consumer quit");
1137 DBG("Consumer flag that it should quit");
1140 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1143 int outfd
= stream
->out_fd
;
1146 * This does a blocking write-and-wait on any page that belongs to the
1147 * subbuffer prior to the one we just wrote.
1148 * Don't care about error values, as these are just hints and ways to
1149 * limit the amount of page cache used.
1151 if (orig_offset
< stream
->max_sb_size
) {
1154 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1155 stream
->max_sb_size
,
1156 SYNC_FILE_RANGE_WAIT_BEFORE
1157 | SYNC_FILE_RANGE_WRITE
1158 | SYNC_FILE_RANGE_WAIT_AFTER
);
1160 * Give hints to the kernel about how we access the file:
1161 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1164 * We need to call fadvise again after the file grows because the
1165 * kernel does not seem to apply fadvise to non-existing parts of the
1168 * Call fadvise _after_ having waited for the page writeback to
1169 * complete because the dirty page writeback semantic is not well
1170 * defined. So it can be expected to lead to lower throughput in
1173 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1174 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1178 * Initialise the necessary environnement :
1179 * - create a new context
1180 * - create the poll_pipe
1181 * - create the should_quit pipe (for signal handler)
1182 * - create the thread pipe (for splice)
1184 * Takes a function pointer as argument, this function is called when data is
1185 * available on a buffer. This function is responsible to do the
1186 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1187 * buffer configuration and then kernctl_put_next_subbuf at the end.
1189 * Returns a pointer to the new context or NULL on error.
1191 struct lttng_consumer_local_data
*lttng_consumer_create(
1192 enum lttng_consumer_type type
,
1193 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1194 struct lttng_consumer_local_data
*ctx
),
1195 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1196 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1197 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1200 struct lttng_consumer_local_data
*ctx
;
1202 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1203 consumer_data
.type
== type
);
1204 consumer_data
.type
= type
;
1206 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1208 PERROR("allocating context");
1212 ctx
->consumer_error_socket
= -1;
1213 ctx
->consumer_metadata_socket
= -1;
1214 /* assign the callbacks */
1215 ctx
->on_buffer_ready
= buffer_ready
;
1216 ctx
->on_recv_channel
= recv_channel
;
1217 ctx
->on_recv_stream
= recv_stream
;
1218 ctx
->on_update_stream
= update_stream
;
1220 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1221 if (!ctx
->consumer_data_pipe
) {
1222 goto error_poll_pipe
;
1225 ret
= pipe(ctx
->consumer_should_quit
);
1227 PERROR("Error creating recv pipe");
1228 goto error_quit_pipe
;
1231 ret
= pipe(ctx
->consumer_thread_pipe
);
1233 PERROR("Error creating thread pipe");
1234 goto error_thread_pipe
;
1237 ret
= pipe(ctx
->consumer_channel_pipe
);
1239 PERROR("Error creating channel pipe");
1240 goto error_channel_pipe
;
1243 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1244 if (!ctx
->consumer_metadata_pipe
) {
1245 goto error_metadata_pipe
;
1248 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1250 goto error_splice_pipe
;
1256 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1257 error_metadata_pipe
:
1258 utils_close_pipe(ctx
->consumer_channel_pipe
);
1260 utils_close_pipe(ctx
->consumer_thread_pipe
);
1262 utils_close_pipe(ctx
->consumer_should_quit
);
1264 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1272 * Close all fds associated with the instance and free the context.
1274 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1278 DBG("Consumer destroying it. Closing everything.");
1280 ret
= close(ctx
->consumer_error_socket
);
1284 ret
= close(ctx
->consumer_metadata_socket
);
1288 utils_close_pipe(ctx
->consumer_thread_pipe
);
1289 utils_close_pipe(ctx
->consumer_channel_pipe
);
1290 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1291 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1292 utils_close_pipe(ctx
->consumer_should_quit
);
1293 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1295 unlink(ctx
->consumer_command_sock_path
);
1300 * Write the metadata stream id on the specified file descriptor.
1302 static int write_relayd_metadata_id(int fd
,
1303 struct lttng_consumer_stream
*stream
,
1304 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1307 struct lttcomm_relayd_metadata_payload hdr
;
1309 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1310 hdr
.padding_size
= htobe32(padding
);
1312 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1313 } while (ret
< 0 && errno
== EINTR
);
1314 if (ret
< 0 || ret
!= sizeof(hdr
)) {
1316 * This error means that the fd's end is closed so ignore the perror
1317 * not to clubber the error output since this can happen in a normal
1320 if (errno
!= EPIPE
) {
1321 PERROR("write metadata stream id");
1323 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1325 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1326 * handle writting the missing part so report that as an error and
1327 * don't lie to the caller.
1332 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1333 stream
->relayd_stream_id
, padding
);
1340 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1341 * core function for writing trace buffers to either the local filesystem or
1344 * It must be called with the stream lock held.
1346 * Careful review MUST be put if any changes occur!
1348 * Returns the number of bytes written
1350 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1351 struct lttng_consumer_local_data
*ctx
,
1352 struct lttng_consumer_stream
*stream
, unsigned long len
,
1353 unsigned long padding
)
1355 unsigned long mmap_offset
;
1357 ssize_t ret
= 0, written
= 0;
1358 off_t orig_offset
= stream
->out_fd_offset
;
1359 /* Default is on the disk */
1360 int outfd
= stream
->out_fd
;
1361 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1362 unsigned int relayd_hang_up
= 0;
1364 /* RCU lock for the relayd pointer */
1367 /* Flag that the current stream if set for network streaming. */
1368 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1369 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1370 if (relayd
== NULL
) {
1376 /* get the offset inside the fd to mmap */
1377 switch (consumer_data
.type
) {
1378 case LTTNG_CONSUMER_KERNEL
:
1379 mmap_base
= stream
->mmap_base
;
1380 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1382 PERROR("tracer ctl get_mmap_read_offset");
1387 case LTTNG_CONSUMER32_UST
:
1388 case LTTNG_CONSUMER64_UST
:
1389 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1391 ERR("read mmap get mmap base for stream %s", stream
->name
);
1395 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1397 PERROR("tracer ctl get_mmap_read_offset");
1403 ERR("Unknown consumer_data type");
1407 /* Handle stream on the relayd if the output is on the network */
1409 unsigned long netlen
= len
;
1412 * Lock the control socket for the complete duration of the function
1413 * since from this point on we will use the socket.
1415 if (stream
->metadata_flag
) {
1416 /* Metadata requires the control socket. */
1417 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1418 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1421 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1423 /* Use the returned socket. */
1426 /* Write metadata stream id before payload */
1427 if (stream
->metadata_flag
) {
1428 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1431 /* Socket operation failed. We consider the relayd dead */
1432 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1440 /* Socket operation failed. We consider the relayd dead */
1441 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1445 /* Else, use the default set before which is the filesystem. */
1448 /* No streaming, we have to set the len with the full padding */
1452 * Check if we need to change the tracefile before writing the packet.
1454 if (stream
->chan
->tracefile_size
> 0 &&
1455 (stream
->tracefile_size_current
+ len
) >
1456 stream
->chan
->tracefile_size
) {
1457 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1458 stream
->name
, stream
->chan
->tracefile_size
,
1459 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1460 stream
->out_fd
, &(stream
->tracefile_count_current
));
1462 ERR("Rotating output file");
1465 outfd
= stream
->out_fd
= ret
;
1466 /* Reset current size because we just perform a rotation. */
1467 stream
->tracefile_size_current
= 0;
1468 stream
->out_fd_offset
= 0;
1471 stream
->tracefile_size_current
+= len
;
1476 ret
= write(outfd
, mmap_base
+ mmap_offset
, len
);
1477 } while (ret
< 0 && errno
== EINTR
);
1478 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1481 * This is possible if the fd is closed on the other side (outfd)
1482 * or any write problem. It can be verbose a bit for a normal
1483 * execution if for instance the relayd is stopped abruptly. This
1484 * can happen so set this to a DBG statement.
1486 DBG("Error in file write mmap");
1490 /* Socket operation failed. We consider the relayd dead */
1491 if (errno
== EPIPE
|| errno
== EINVAL
) {
1496 } else if (ret
> len
) {
1497 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1505 /* This call is useless on a socket so better save a syscall. */
1507 /* This won't block, but will start writeout asynchronously */
1508 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1509 SYNC_FILE_RANGE_WRITE
);
1510 stream
->out_fd_offset
+= ret
;
1512 stream
->output_written
+= ret
;
1515 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1519 * This is a special case that the relayd has closed its socket. Let's
1520 * cleanup the relayd object and all associated streams.
1522 if (relayd
&& relayd_hang_up
) {
1523 cleanup_relayd(relayd
, ctx
);
1527 /* Unlock only if ctrl socket used */
1528 if (relayd
&& stream
->metadata_flag
) {
1529 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1537 * Splice the data from the ring buffer to the tracefile.
1539 * It must be called with the stream lock held.
1541 * Returns the number of bytes spliced.
1543 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1544 struct lttng_consumer_local_data
*ctx
,
1545 struct lttng_consumer_stream
*stream
, unsigned long len
,
1546 unsigned long padding
)
1548 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1550 off_t orig_offset
= stream
->out_fd_offset
;
1551 int fd
= stream
->wait_fd
;
1552 /* Default is on the disk */
1553 int outfd
= stream
->out_fd
;
1554 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1556 unsigned int relayd_hang_up
= 0;
1558 switch (consumer_data
.type
) {
1559 case LTTNG_CONSUMER_KERNEL
:
1561 case LTTNG_CONSUMER32_UST
:
1562 case LTTNG_CONSUMER64_UST
:
1563 /* Not supported for user space tracing */
1566 ERR("Unknown consumer_data type");
1570 /* RCU lock for the relayd pointer */
1573 /* Flag that the current stream if set for network streaming. */
1574 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1575 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1576 if (relayd
== NULL
) {
1583 * Choose right pipe for splice. Metadata and trace data are handled by
1584 * different threads hence the use of two pipes in order not to race or
1585 * corrupt the written data.
1587 if (stream
->metadata_flag
) {
1588 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1590 splice_pipe
= ctx
->consumer_thread_pipe
;
1593 /* Write metadata stream id before payload */
1595 int total_len
= len
;
1597 if (stream
->metadata_flag
) {
1599 * Lock the control socket for the complete duration of the function
1600 * since from this point on we will use the socket.
1602 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1604 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1608 /* Socket operation failed. We consider the relayd dead */
1609 if (ret
== -EBADF
) {
1610 WARN("Remote relayd disconnected. Stopping");
1617 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1620 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1622 /* Use the returned socket. */
1625 /* Socket operation failed. We consider the relayd dead */
1626 if (ret
== -EBADF
) {
1627 WARN("Remote relayd disconnected. Stopping");
1634 /* No streaming, we have to set the len with the full padding */
1638 * Check if we need to change the tracefile before writing the packet.
1640 if (stream
->chan
->tracefile_size
> 0 &&
1641 (stream
->tracefile_size_current
+ len
) >
1642 stream
->chan
->tracefile_size
) {
1643 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1644 stream
->name
, stream
->chan
->tracefile_size
,
1645 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1646 stream
->out_fd
, &(stream
->tracefile_count_current
));
1648 ERR("Rotating output file");
1651 outfd
= stream
->out_fd
= ret
;
1652 /* Reset current size because we just perform a rotation. */
1653 stream
->tracefile_size_current
= 0;
1654 stream
->out_fd_offset
= 0;
1657 stream
->tracefile_size_current
+= len
;
1661 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1662 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1663 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1664 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1665 DBG("splice chan to pipe, ret %zd", ret_splice
);
1666 if (ret_splice
< 0) {
1667 PERROR("Error in relay splice");
1669 written
= ret_splice
;
1675 /* Handle stream on the relayd if the output is on the network */
1677 if (stream
->metadata_flag
) {
1678 size_t metadata_payload_size
=
1679 sizeof(struct lttcomm_relayd_metadata_payload
);
1681 /* Update counter to fit the spliced data */
1682 ret_splice
+= metadata_payload_size
;
1683 len
+= metadata_payload_size
;
1685 * We do this so the return value can match the len passed as
1686 * argument to this function.
1688 written
-= metadata_payload_size
;
1692 /* Splice data out */
1693 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1694 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1695 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1696 if (ret_splice
< 0) {
1697 PERROR("Error in file splice");
1699 written
= ret_splice
;
1701 /* Socket operation failed. We consider the relayd dead */
1702 if (errno
== EBADF
|| errno
== EPIPE
) {
1703 WARN("Remote relayd disconnected. Stopping");
1709 } else if (ret_splice
> len
) {
1711 PERROR("Wrote more data than requested %zd (len: %lu)",
1713 written
+= ret_splice
;
1719 /* This call is useless on a socket so better save a syscall. */
1721 /* This won't block, but will start writeout asynchronously */
1722 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1723 SYNC_FILE_RANGE_WRITE
);
1724 stream
->out_fd_offset
+= ret_splice
;
1726 stream
->output_written
+= ret_splice
;
1727 written
+= ret_splice
;
1729 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1737 * This is a special case that the relayd has closed its socket. Let's
1738 * cleanup the relayd object and all associated streams.
1740 if (relayd
&& relayd_hang_up
) {
1741 cleanup_relayd(relayd
, ctx
);
1742 /* Skip splice error so the consumer does not fail */
1747 /* send the appropriate error description to sessiond */
1750 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1753 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1756 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1761 if (relayd
&& stream
->metadata_flag
) {
1762 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1770 * Take a snapshot for a specific fd
1772 * Returns 0 on success, < 0 on error
1774 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1776 switch (consumer_data
.type
) {
1777 case LTTNG_CONSUMER_KERNEL
:
1778 return lttng_kconsumer_take_snapshot(stream
);
1779 case LTTNG_CONSUMER32_UST
:
1780 case LTTNG_CONSUMER64_UST
:
1781 return lttng_ustconsumer_take_snapshot(stream
);
1783 ERR("Unknown consumer_data type");
1790 * Get the produced position
1792 * Returns 0 on success, < 0 on error
1794 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1797 switch (consumer_data
.type
) {
1798 case LTTNG_CONSUMER_KERNEL
:
1799 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1800 case LTTNG_CONSUMER32_UST
:
1801 case LTTNG_CONSUMER64_UST
:
1802 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1804 ERR("Unknown consumer_data type");
1810 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1811 int sock
, struct pollfd
*consumer_sockpoll
)
1813 switch (consumer_data
.type
) {
1814 case LTTNG_CONSUMER_KERNEL
:
1815 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1816 case LTTNG_CONSUMER32_UST
:
1817 case LTTNG_CONSUMER64_UST
:
1818 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1820 ERR("Unknown consumer_data type");
1827 * Iterate over all streams of the hashtable and free them properly.
1829 * WARNING: *MUST* be used with data stream only.
1831 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1833 struct lttng_ht_iter iter
;
1834 struct lttng_consumer_stream
*stream
;
1841 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1843 * Ignore return value since we are currently cleaning up so any error
1846 (void) consumer_del_stream(stream
, ht
);
1850 lttng_ht_destroy(ht
);
1854 * Iterate over all streams of the hashtable and free them properly.
1856 * XXX: Should not be only for metadata stream or else use an other name.
1858 static void destroy_stream_ht(struct lttng_ht
*ht
)
1860 struct lttng_ht_iter iter
;
1861 struct lttng_consumer_stream
*stream
;
1868 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1870 * Ignore return value since we are currently cleaning up so any error
1873 (void) consumer_del_metadata_stream(stream
, ht
);
1877 lttng_ht_destroy(ht
);
1880 void lttng_consumer_close_metadata(void)
1882 switch (consumer_data
.type
) {
1883 case LTTNG_CONSUMER_KERNEL
:
1885 * The Kernel consumer has a different metadata scheme so we don't
1886 * close anything because the stream will be closed by the session
1890 case LTTNG_CONSUMER32_UST
:
1891 case LTTNG_CONSUMER64_UST
:
1893 * Close all metadata streams. The metadata hash table is passed and
1894 * this call iterates over it by closing all wakeup fd. This is safe
1895 * because at this point we are sure that the metadata producer is
1896 * either dead or blocked.
1898 lttng_ustconsumer_close_metadata(metadata_ht
);
1901 ERR("Unknown consumer_data type");
1907 * Clean up a metadata stream and free its memory.
1909 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1910 struct lttng_ht
*ht
)
1913 struct lttng_ht_iter iter
;
1914 struct lttng_consumer_channel
*free_chan
= NULL
;
1915 struct consumer_relayd_sock_pair
*relayd
;
1919 * This call should NEVER receive regular stream. It must always be
1920 * metadata stream and this is crucial for data structure synchronization.
1922 assert(stream
->metadata_flag
);
1924 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1927 /* Means the stream was allocated but not successfully added */
1928 goto free_stream_rcu
;
1931 pthread_mutex_lock(&consumer_data
.lock
);
1932 pthread_mutex_lock(&stream
->chan
->lock
);
1933 pthread_mutex_lock(&stream
->lock
);
1935 switch (consumer_data
.type
) {
1936 case LTTNG_CONSUMER_KERNEL
:
1937 if (stream
->mmap_base
!= NULL
) {
1938 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1940 PERROR("munmap metadata stream");
1944 if (stream
->wait_fd
>= 0) {
1945 ret
= close(stream
->wait_fd
);
1947 PERROR("close kernel metadata wait_fd");
1951 case LTTNG_CONSUMER32_UST
:
1952 case LTTNG_CONSUMER64_UST
:
1953 lttng_ustconsumer_del_stream(stream
);
1956 ERR("Unknown consumer_data type");
1962 iter
.iter
.node
= &stream
->node
.node
;
1963 ret
= lttng_ht_del(ht
, &iter
);
1966 iter
.iter
.node
= &stream
->node_channel_id
.node
;
1967 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
1970 iter
.iter
.node
= &stream
->node_session_id
.node
;
1971 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1975 if (stream
->out_fd
>= 0) {
1976 ret
= close(stream
->out_fd
);
1982 /* Check and cleanup relayd */
1984 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1985 if (relayd
!= NULL
) {
1986 uatomic_dec(&relayd
->refcount
);
1987 assert(uatomic_read(&relayd
->refcount
) >= 0);
1989 /* Closing streams requires to lock the control socket. */
1990 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1991 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1992 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1993 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1995 DBG("Unable to close stream on the relayd. Continuing");
1997 * Continue here. There is nothing we can do for the relayd.
1998 * Chances are that the relayd has closed the socket so we just
1999 * continue cleaning up.
2003 /* Both conditions are met, we destroy the relayd. */
2004 if (uatomic_read(&relayd
->refcount
) == 0 &&
2005 uatomic_read(&relayd
->destroy_flag
)) {
2006 destroy_relayd(relayd
);
2011 /* Atomically decrement channel refcount since other threads can use it. */
2012 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
2013 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
2014 /* Go for channel deletion! */
2015 free_chan
= stream
->chan
;
2020 * Nullify the stream reference so it is not used after deletion. The
2021 * channel lock MUST be acquired before being able to check for
2022 * a NULL pointer value.
2024 stream
->chan
->metadata_stream
= NULL
;
2026 pthread_mutex_unlock(&stream
->lock
);
2027 pthread_mutex_unlock(&stream
->chan
->lock
);
2028 pthread_mutex_unlock(&consumer_data
.lock
);
2031 consumer_del_channel(free_chan
);
2035 call_rcu(&stream
->node
.head
, free_stream_rcu
);
2039 * Action done with the metadata stream when adding it to the consumer internal
2040 * data structures to handle it.
2042 int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2044 struct lttng_ht
*ht
= metadata_ht
;
2046 struct consumer_relayd_sock_pair
*relayd
;
2047 struct lttng_ht_iter iter
;
2048 struct lttng_ht_node_u64
*node
;
2053 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2055 pthread_mutex_lock(&consumer_data
.lock
);
2056 pthread_mutex_lock(&stream
->chan
->lock
);
2057 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2058 pthread_mutex_lock(&stream
->lock
);
2061 * From here, refcounts are updated so be _careful_ when returning an error
2068 * Lookup the stream just to make sure it does not exist in our internal
2069 * state. This should NEVER happen.
2071 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2072 node
= lttng_ht_iter_get_node_u64(&iter
);
2075 /* Find relayd and, if one is found, increment refcount. */
2076 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
2077 if (relayd
!= NULL
) {
2078 uatomic_inc(&relayd
->refcount
);
2082 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2083 * in terms of destroying the associated channel, because the action that
2084 * causes the count to become 0 also causes a stream to be added. The
2085 * channel deletion will thus be triggered by the following removal of this
2088 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2089 /* Increment refcount before decrementing nb_init_stream_left */
2091 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2094 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2096 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2097 &stream
->node_channel_id
);
2100 * Add stream to the stream_list_ht of the consumer data. No need to steal
2101 * the key since the HT does not use it and we allow to add redundant keys
2104 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2108 pthread_mutex_unlock(&stream
->lock
);
2109 pthread_mutex_unlock(&stream
->chan
->lock
);
2110 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2111 pthread_mutex_unlock(&consumer_data
.lock
);
2116 * Delete data stream that are flagged for deletion (endpoint_status).
2118 static void validate_endpoint_status_data_stream(void)
2120 struct lttng_ht_iter iter
;
2121 struct lttng_consumer_stream
*stream
;
2123 DBG("Consumer delete flagged data stream");
2126 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2127 /* Validate delete flag of the stream */
2128 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2131 /* Delete it right now */
2132 consumer_del_stream(stream
, data_ht
);
2138 * Delete metadata stream that are flagged for deletion (endpoint_status).
2140 static void validate_endpoint_status_metadata_stream(
2141 struct lttng_poll_event
*pollset
)
2143 struct lttng_ht_iter iter
;
2144 struct lttng_consumer_stream
*stream
;
2146 DBG("Consumer delete flagged metadata stream");
2151 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2152 /* Validate delete flag of the stream */
2153 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2157 * Remove from pollset so the metadata thread can continue without
2158 * blocking on a deleted stream.
2160 lttng_poll_del(pollset
, stream
->wait_fd
);
2162 /* Delete it right now */
2163 consumer_del_metadata_stream(stream
, metadata_ht
);
2169 * Thread polls on metadata file descriptor and write them on disk or on the
2172 void *consumer_thread_metadata_poll(void *data
)
2175 uint32_t revents
, nb_fd
;
2176 struct lttng_consumer_stream
*stream
= NULL
;
2177 struct lttng_ht_iter iter
;
2178 struct lttng_ht_node_u64
*node
;
2179 struct lttng_poll_event events
;
2180 struct lttng_consumer_local_data
*ctx
= data
;
2183 rcu_register_thread();
2185 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2187 /* ENOMEM at this point. Better to bail out. */
2191 DBG("Thread metadata poll started");
2193 /* Size is set to 1 for the consumer_metadata pipe */
2194 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2196 ERR("Poll set creation failed");
2200 ret
= lttng_poll_add(&events
,
2201 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2207 DBG("Metadata main loop started");
2210 /* Only the metadata pipe is set */
2211 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2216 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2217 ret
= lttng_poll_wait(&events
, -1);
2218 DBG("Metadata event catched in thread");
2220 if (errno
== EINTR
) {
2221 ERR("Poll EINTR catched");
2229 /* From here, the event is a metadata wait fd */
2230 for (i
= 0; i
< nb_fd
; i
++) {
2231 revents
= LTTNG_POLL_GETEV(&events
, i
);
2232 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2234 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2235 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2236 DBG("Metadata thread pipe hung up");
2238 * Remove the pipe from the poll set and continue the loop
2239 * since their might be data to consume.
2241 lttng_poll_del(&events
,
2242 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2243 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2245 } else if (revents
& LPOLLIN
) {
2248 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2249 &stream
, sizeof(stream
));
2251 ERR("read metadata stream, ret: %zd", pipe_len
);
2253 * Continue here to handle the rest of the streams.
2258 /* A NULL stream means that the state has changed. */
2259 if (stream
== NULL
) {
2260 /* Check for deleted streams. */
2261 validate_endpoint_status_metadata_stream(&events
);
2265 DBG("Adding metadata stream %d to poll set",
2268 /* Add metadata stream to the global poll events list */
2269 lttng_poll_add(&events
, stream
->wait_fd
,
2270 LPOLLIN
| LPOLLPRI
);
2273 /* Handle other stream */
2279 uint64_t tmp_id
= (uint64_t) pollfd
;
2281 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2283 node
= lttng_ht_iter_get_node_u64(&iter
);
2286 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2289 /* Check for error event */
2290 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2291 DBG("Metadata fd %d is hup|err.", pollfd
);
2292 if (!stream
->hangup_flush_done
2293 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2294 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2295 DBG("Attempting to flush and consume the UST buffers");
2296 lttng_ustconsumer_on_stream_hangup(stream
);
2298 /* We just flushed the stream now read it. */
2300 len
= ctx
->on_buffer_ready(stream
, ctx
);
2302 * We don't check the return value here since if we get
2303 * a negative len, it means an error occured thus we
2304 * simply remove it from the poll set and free the
2310 lttng_poll_del(&events
, stream
->wait_fd
);
2312 * This call update the channel states, closes file descriptors
2313 * and securely free the stream.
2315 consumer_del_metadata_stream(stream
, metadata_ht
);
2316 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2317 /* Get the data out of the metadata file descriptor */
2318 DBG("Metadata available on fd %d", pollfd
);
2319 assert(stream
->wait_fd
== pollfd
);
2321 len
= ctx
->on_buffer_ready(stream
, ctx
);
2322 /* It's ok to have an unavailable sub-buffer */
2323 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2324 /* Clean up stream from consumer and free it. */
2325 lttng_poll_del(&events
, stream
->wait_fd
);
2326 consumer_del_metadata_stream(stream
, metadata_ht
);
2327 } else if (len
> 0) {
2328 stream
->data_read
= 1;
2332 /* Release RCU lock for the stream looked up */
2339 DBG("Metadata poll thread exiting");
2341 lttng_poll_clean(&events
);
2343 destroy_stream_ht(metadata_ht
);
2345 rcu_unregister_thread();
2350 * This thread polls the fds in the set to consume the data and write
2351 * it to tracefile if necessary.
2353 void *consumer_thread_data_poll(void *data
)
2355 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2356 struct pollfd
*pollfd
= NULL
;
2357 /* local view of the streams */
2358 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2359 /* local view of consumer_data.fds_count */
2361 struct lttng_consumer_local_data
*ctx
= data
;
2364 rcu_register_thread();
2366 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2367 if (data_ht
== NULL
) {
2368 /* ENOMEM at this point. Better to bail out. */
2372 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2373 if (local_stream
== NULL
) {
2374 PERROR("local_stream malloc");
2383 * the fds set has been updated, we need to update our
2384 * local array as well
2386 pthread_mutex_lock(&consumer_data
.lock
);
2387 if (consumer_data
.need_update
) {
2392 local_stream
= NULL
;
2394 /* allocate for all fds + 1 for the consumer_data_pipe */
2395 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2396 if (pollfd
== NULL
) {
2397 PERROR("pollfd malloc");
2398 pthread_mutex_unlock(&consumer_data
.lock
);
2402 /* allocate for all fds + 1 for the consumer_data_pipe */
2403 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2404 sizeof(struct lttng_consumer_stream
*));
2405 if (local_stream
== NULL
) {
2406 PERROR("local_stream malloc");
2407 pthread_mutex_unlock(&consumer_data
.lock
);
2410 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2413 ERR("Error in allocating pollfd or local_outfds");
2414 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2415 pthread_mutex_unlock(&consumer_data
.lock
);
2419 consumer_data
.need_update
= 0;
2421 pthread_mutex_unlock(&consumer_data
.lock
);
2423 /* No FDs and consumer_quit, consumer_cleanup the thread */
2424 if (nb_fd
== 0 && consumer_quit
== 1) {
2427 /* poll on the array of fds */
2429 DBG("polling on %d fd", nb_fd
+ 1);
2430 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2431 DBG("poll num_rdy : %d", num_rdy
);
2432 if (num_rdy
== -1) {
2434 * Restart interrupted system call.
2436 if (errno
== EINTR
) {
2439 PERROR("Poll error");
2440 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2442 } else if (num_rdy
== 0) {
2443 DBG("Polling thread timed out");
2448 * If the consumer_data_pipe triggered poll go directly to the
2449 * beginning of the loop to update the array. We want to prioritize
2450 * array update over low-priority reads.
2452 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2453 ssize_t pipe_readlen
;
2455 DBG("consumer_data_pipe wake up");
2456 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2457 &new_stream
, sizeof(new_stream
));
2458 if (pipe_readlen
< 0) {
2459 ERR("Consumer data pipe ret %zd", pipe_readlen
);
2460 /* Continue so we can at least handle the current stream(s). */
2465 * If the stream is NULL, just ignore it. It's also possible that
2466 * the sessiond poll thread changed the consumer_quit state and is
2467 * waking us up to test it.
2469 if (new_stream
== NULL
) {
2470 validate_endpoint_status_data_stream();
2474 /* Continue to update the local streams and handle prio ones */
2478 /* Take care of high priority channels first. */
2479 for (i
= 0; i
< nb_fd
; i
++) {
2480 if (local_stream
[i
] == NULL
) {
2483 if (pollfd
[i
].revents
& POLLPRI
) {
2484 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2486 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2487 /* it's ok to have an unavailable sub-buffer */
2488 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2489 /* Clean the stream and free it. */
2490 consumer_del_stream(local_stream
[i
], data_ht
);
2491 local_stream
[i
] = NULL
;
2492 } else if (len
> 0) {
2493 local_stream
[i
]->data_read
= 1;
2499 * If we read high prio channel in this loop, try again
2500 * for more high prio data.
2506 /* Take care of low priority channels. */
2507 for (i
= 0; i
< nb_fd
; i
++) {
2508 if (local_stream
[i
] == NULL
) {
2511 if ((pollfd
[i
].revents
& POLLIN
) ||
2512 local_stream
[i
]->hangup_flush_done
) {
2513 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2514 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2515 /* it's ok to have an unavailable sub-buffer */
2516 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2517 /* Clean the stream and free it. */
2518 consumer_del_stream(local_stream
[i
], data_ht
);
2519 local_stream
[i
] = NULL
;
2520 } else if (len
> 0) {
2521 local_stream
[i
]->data_read
= 1;
2526 /* Handle hangup and errors */
2527 for (i
= 0; i
< nb_fd
; i
++) {
2528 if (local_stream
[i
] == NULL
) {
2531 if (!local_stream
[i
]->hangup_flush_done
2532 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2533 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2534 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2535 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2537 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2538 /* Attempt read again, for the data we just flushed. */
2539 local_stream
[i
]->data_read
= 1;
2542 * If the poll flag is HUP/ERR/NVAL and we have
2543 * read no data in this pass, we can remove the
2544 * stream from its hash table.
2546 if ((pollfd
[i
].revents
& POLLHUP
)) {
2547 DBG("Polling fd %d tells it has hung up.", 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
& POLLERR
) {
2554 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2555 if (!local_stream
[i
]->data_read
) {
2556 consumer_del_stream(local_stream
[i
], data_ht
);
2557 local_stream
[i
] = NULL
;
2560 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2561 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2562 if (!local_stream
[i
]->data_read
) {
2563 consumer_del_stream(local_stream
[i
], data_ht
);
2564 local_stream
[i
] = NULL
;
2568 if (local_stream
[i
] != NULL
) {
2569 local_stream
[i
]->data_read
= 0;
2574 DBG("polling thread exiting");
2579 * Close the write side of the pipe so epoll_wait() in
2580 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2581 * read side of the pipe. If we close them both, epoll_wait strangely does
2582 * not return and could create a endless wait period if the pipe is the
2583 * only tracked fd in the poll set. The thread will take care of closing
2586 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2588 destroy_data_stream_ht(data_ht
);
2590 rcu_unregister_thread();
2595 * Close wake-up end of each stream belonging to the channel. This will
2596 * allow the poll() on the stream read-side to detect when the
2597 * write-side (application) finally closes them.
2600 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2602 struct lttng_ht
*ht
;
2603 struct lttng_consumer_stream
*stream
;
2604 struct lttng_ht_iter iter
;
2606 ht
= consumer_data
.stream_per_chan_id_ht
;
2609 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2610 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2611 ht
->match_fct
, &channel
->key
,
2612 &iter
.iter
, stream
, node_channel_id
.node
) {
2614 * Protect against teardown with mutex.
2616 pthread_mutex_lock(&stream
->lock
);
2617 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2620 switch (consumer_data
.type
) {
2621 case LTTNG_CONSUMER_KERNEL
:
2623 case LTTNG_CONSUMER32_UST
:
2624 case LTTNG_CONSUMER64_UST
:
2626 * Note: a mutex is taken internally within
2627 * liblttng-ust-ctl to protect timer wakeup_fd
2628 * use from concurrent close.
2630 lttng_ustconsumer_close_stream_wakeup(stream
);
2633 ERR("Unknown consumer_data type");
2637 pthread_mutex_unlock(&stream
->lock
);
2642 static void destroy_channel_ht(struct lttng_ht
*ht
)
2644 struct lttng_ht_iter iter
;
2645 struct lttng_consumer_channel
*channel
;
2653 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2654 ret
= lttng_ht_del(ht
, &iter
);
2659 lttng_ht_destroy(ht
);
2663 * This thread polls the channel fds to detect when they are being
2664 * closed. It closes all related streams if the channel is detected as
2665 * closed. It is currently only used as a shim layer for UST because the
2666 * consumerd needs to keep the per-stream wakeup end of pipes open for
2669 void *consumer_thread_channel_poll(void *data
)
2672 uint32_t revents
, nb_fd
;
2673 struct lttng_consumer_channel
*chan
= NULL
;
2674 struct lttng_ht_iter iter
;
2675 struct lttng_ht_node_u64
*node
;
2676 struct lttng_poll_event events
;
2677 struct lttng_consumer_local_data
*ctx
= data
;
2678 struct lttng_ht
*channel_ht
;
2680 rcu_register_thread();
2682 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2684 /* ENOMEM at this point. Better to bail out. */
2688 DBG("Thread channel poll started");
2690 /* Size is set to 1 for the consumer_channel pipe */
2691 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2693 ERR("Poll set creation failed");
2697 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2703 DBG("Channel main loop started");
2706 /* Only the channel pipe is set */
2707 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2712 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2713 ret
= lttng_poll_wait(&events
, -1);
2714 DBG("Channel event catched in thread");
2716 if (errno
== EINTR
) {
2717 ERR("Poll EINTR catched");
2725 /* From here, the event is a channel wait fd */
2726 for (i
= 0; i
< nb_fd
; i
++) {
2727 revents
= LTTNG_POLL_GETEV(&events
, i
);
2728 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2730 /* Just don't waste time if no returned events for the fd */
2734 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2735 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2736 DBG("Channel thread pipe hung up");
2738 * Remove the pipe from the poll set and continue the loop
2739 * since their might be data to consume.
2741 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2743 } else if (revents
& LPOLLIN
) {
2744 enum consumer_channel_action action
;
2747 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2749 ERR("Error reading channel pipe");
2754 case CONSUMER_CHANNEL_ADD
:
2755 DBG("Adding channel %d to poll set",
2758 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2761 lttng_ht_add_unique_u64(channel_ht
,
2762 &chan
->wait_fd_node
);
2764 /* Add channel to the global poll events list */
2765 lttng_poll_add(&events
, chan
->wait_fd
,
2766 LPOLLIN
| LPOLLPRI
);
2768 case CONSUMER_CHANNEL_DEL
:
2770 struct lttng_consumer_stream
*stream
, *stmp
;
2773 chan
= consumer_find_channel(key
);
2776 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2779 lttng_poll_del(&events
, chan
->wait_fd
);
2780 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2781 ret
= lttng_ht_del(channel_ht
, &iter
);
2783 consumer_close_channel_streams(chan
);
2785 switch (consumer_data
.type
) {
2786 case LTTNG_CONSUMER_KERNEL
:
2788 case LTTNG_CONSUMER32_UST
:
2789 case LTTNG_CONSUMER64_UST
:
2790 /* Delete streams that might have been left in the stream list. */
2791 cds_list_for_each_entry_safe(stream
, stmp
, &chan
->streams
.head
,
2793 cds_list_del(&stream
->send_node
);
2794 lttng_ustconsumer_del_stream(stream
);
2795 uatomic_sub(&stream
->chan
->refcount
, 1);
2796 assert(&chan
->refcount
);
2801 ERR("Unknown consumer_data type");
2806 * Release our own refcount. Force channel deletion even if
2807 * streams were not initialized.
2809 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2810 consumer_del_channel(chan
);
2815 case CONSUMER_CHANNEL_QUIT
:
2817 * Remove the pipe from the poll set and continue the loop
2818 * since their might be data to consume.
2820 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2823 ERR("Unknown action");
2828 /* Handle other stream */
2834 uint64_t tmp_id
= (uint64_t) pollfd
;
2836 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2838 node
= lttng_ht_iter_get_node_u64(&iter
);
2841 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2844 /* Check for error event */
2845 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2846 DBG("Channel fd %d is hup|err.", pollfd
);
2848 lttng_poll_del(&events
, chan
->wait_fd
);
2849 ret
= lttng_ht_del(channel_ht
, &iter
);
2851 assert(cds_list_empty(&chan
->streams
.head
));
2852 consumer_close_channel_streams(chan
);
2854 /* Release our own refcount */
2855 if (!uatomic_sub_return(&chan
->refcount
, 1)
2856 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2857 consumer_del_channel(chan
);
2861 /* Release RCU lock for the channel looked up */
2867 lttng_poll_clean(&events
);
2869 destroy_channel_ht(channel_ht
);
2871 DBG("Channel poll thread exiting");
2872 rcu_unregister_thread();
2876 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2877 struct pollfd
*sockpoll
, int client_socket
)
2884 if (lttng_consumer_poll_socket(sockpoll
) < 0) {
2888 DBG("Metadata connection on client_socket");
2890 /* Blocking call, waiting for transmission */
2891 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2892 if (ctx
->consumer_metadata_socket
< 0) {
2893 WARN("On accept metadata");
2904 * This thread listens on the consumerd socket and receives the file
2905 * descriptors from the session daemon.
2907 void *consumer_thread_sessiond_poll(void *data
)
2909 int sock
= -1, client_socket
, ret
;
2911 * structure to poll for incoming data on communication socket avoids
2912 * making blocking sockets.
2914 struct pollfd consumer_sockpoll
[2];
2915 struct lttng_consumer_local_data
*ctx
= data
;
2917 rcu_register_thread();
2919 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2920 unlink(ctx
->consumer_command_sock_path
);
2921 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2922 if (client_socket
< 0) {
2923 ERR("Cannot create command socket");
2927 ret
= lttcomm_listen_unix_sock(client_socket
);
2932 DBG("Sending ready command to lttng-sessiond");
2933 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2934 /* return < 0 on error, but == 0 is not fatal */
2936 ERR("Error sending ready command to lttng-sessiond");
2940 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2941 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2942 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2943 consumer_sockpoll
[1].fd
= client_socket
;
2944 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2946 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2949 DBG("Connection on client_socket");
2951 /* Blocking call, waiting for transmission */
2952 sock
= lttcomm_accept_unix_sock(client_socket
);
2959 * Setup metadata socket which is the second socket connection on the
2960 * command unix socket.
2962 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
2967 /* This socket is not useful anymore. */
2968 ret
= close(client_socket
);
2970 PERROR("close client_socket");
2974 /* update the polling structure to poll on the established socket */
2975 consumer_sockpoll
[1].fd
= sock
;
2976 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2979 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2982 DBG("Incoming command on sock");
2983 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2984 if (ret
== -ENOENT
) {
2985 DBG("Received STOP command");
2990 * This could simply be a session daemon quitting. Don't output
2993 DBG("Communication interrupted on command socket");
2996 if (consumer_quit
) {
2997 DBG("consumer_thread_receive_fds received quit from signal");
3000 DBG("received command on sock");
3003 DBG("Consumer thread sessiond poll exiting");
3006 * Close metadata streams since the producer is the session daemon which
3009 * NOTE: for now, this only applies to the UST tracer.
3011 lttng_consumer_close_metadata();
3014 * when all fds have hung up, the polling thread
3020 * Notify the data poll thread to poll back again and test the
3021 * consumer_quit state that we just set so to quit gracefully.
3023 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3025 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3027 /* Cleaning up possibly open sockets. */
3031 PERROR("close sock sessiond poll");
3034 if (client_socket
>= 0) {
3035 ret
= close(client_socket
);
3037 PERROR("close client_socket sessiond poll");
3041 rcu_unregister_thread();
3045 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3046 struct lttng_consumer_local_data
*ctx
)
3050 pthread_mutex_lock(&stream
->lock
);
3052 switch (consumer_data
.type
) {
3053 case LTTNG_CONSUMER_KERNEL
:
3054 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3056 case LTTNG_CONSUMER32_UST
:
3057 case LTTNG_CONSUMER64_UST
:
3058 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3061 ERR("Unknown consumer_data type");
3067 pthread_mutex_unlock(&stream
->lock
);
3071 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3073 switch (consumer_data
.type
) {
3074 case LTTNG_CONSUMER_KERNEL
:
3075 return lttng_kconsumer_on_recv_stream(stream
);
3076 case LTTNG_CONSUMER32_UST
:
3077 case LTTNG_CONSUMER64_UST
:
3078 return lttng_ustconsumer_on_recv_stream(stream
);
3080 ERR("Unknown consumer_data type");
3087 * Allocate and set consumer data hash tables.
3089 void lttng_consumer_init(void)
3091 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3092 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3093 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3094 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3098 * Process the ADD_RELAYD command receive by a consumer.
3100 * This will create a relayd socket pair and add it to the relayd hash table.
3101 * The caller MUST acquire a RCU read side lock before calling it.
3103 int consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3104 struct lttng_consumer_local_data
*ctx
, int sock
,
3105 struct pollfd
*consumer_sockpoll
,
3106 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
)
3108 int fd
= -1, ret
= -1, relayd_created
= 0;
3109 enum lttng_error_code ret_code
= LTTNG_OK
;
3110 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3113 assert(relayd_sock
);
3115 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3117 /* Get relayd reference if exists. */
3118 relayd
= consumer_find_relayd(net_seq_idx
);
3119 if (relayd
== NULL
) {
3120 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3121 /* Not found. Allocate one. */
3122 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3123 if (relayd
== NULL
) {
3125 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3128 relayd
->sessiond_session_id
= sessiond_id
;
3133 * This code path MUST continue to the consumer send status message to
3134 * we can notify the session daemon and continue our work without
3135 * killing everything.
3139 * relayd key should never be found for control socket.
3141 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3144 /* First send a status message before receiving the fds. */
3145 ret
= consumer_send_status_msg(sock
, LTTNG_OK
);
3147 /* Somehow, the session daemon is not responding anymore. */
3148 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3149 goto error_nosignal
;
3152 /* Poll on consumer socket. */
3153 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
3154 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3156 goto error_nosignal
;
3159 /* Get relayd socket from session daemon */
3160 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3161 if (ret
!= sizeof(fd
)) {
3163 fd
= -1; /* Just in case it gets set with an invalid value. */
3166 * Failing to receive FDs might indicate a major problem such as
3167 * reaching a fd limit during the receive where the kernel returns a
3168 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3169 * don't take any chances and stop everything.
3171 * XXX: Feature request #558 will fix that and avoid this possible
3172 * issue when reaching the fd limit.
3174 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3175 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3179 /* Copy socket information and received FD */
3180 switch (sock_type
) {
3181 case LTTNG_STREAM_CONTROL
:
3182 /* Copy received lttcomm socket */
3183 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3184 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3185 /* Handle create_sock error. */
3187 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3191 * Close the socket created internally by
3192 * lttcomm_create_sock, so we can replace it by the one
3193 * received from sessiond.
3195 if (close(relayd
->control_sock
.sock
.fd
)) {
3199 /* Assign new file descriptor */
3200 relayd
->control_sock
.sock
.fd
= fd
;
3201 fd
= -1; /* For error path */
3202 /* Assign version values. */
3203 relayd
->control_sock
.major
= relayd_sock
->major
;
3204 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3207 * Create a session on the relayd and store the returned id. Lock the
3208 * control socket mutex if the relayd was NOT created before.
3210 if (!relayd_created
) {
3211 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3213 ret
= relayd_create_session(&relayd
->control_sock
,
3214 &relayd
->relayd_session_id
);
3215 if (!relayd_created
) {
3216 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3220 * Close all sockets of a relayd object. It will be freed if it was
3221 * created at the error code path or else it will be garbage
3224 (void) relayd_close(&relayd
->control_sock
);
3225 (void) relayd_close(&relayd
->data_sock
);
3226 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
3231 case LTTNG_STREAM_DATA
:
3232 /* Copy received lttcomm socket */
3233 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3234 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3235 /* Handle create_sock error. */
3237 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3241 * Close the socket created internally by
3242 * lttcomm_create_sock, so we can replace it by the one
3243 * received from sessiond.
3245 if (close(relayd
->data_sock
.sock
.fd
)) {
3249 /* Assign new file descriptor */
3250 relayd
->data_sock
.sock
.fd
= fd
;
3251 fd
= -1; /* for eventual error paths */
3252 /* Assign version values. */
3253 relayd
->data_sock
.major
= relayd_sock
->major
;
3254 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3257 ERR("Unknown relayd socket type (%d)", sock_type
);
3259 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3263 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3264 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3265 relayd
->net_seq_idx
, fd
);
3267 /* We successfully added the socket. Send status back. */
3268 ret
= consumer_send_status_msg(sock
, ret_code
);
3270 /* Somehow, the session daemon is not responding anymore. */
3271 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3272 goto error_nosignal
;
3276 * Add relayd socket pair to consumer data hashtable. If object already
3277 * exists or on error, the function gracefully returns.
3285 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3286 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3290 /* Close received socket if valid. */
3293 PERROR("close received socket");
3297 if (relayd_created
) {
3305 * Try to lock the stream mutex.
3307 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3309 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3316 * Try to lock the stream mutex. On failure, we know that the stream is
3317 * being used else where hence there is data still being extracted.
3319 ret
= pthread_mutex_trylock(&stream
->lock
);
3321 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3333 * Search for a relayd associated to the session id and return the reference.
3335 * A rcu read side lock MUST be acquire before calling this function and locked
3336 * until the relayd object is no longer necessary.
3338 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3340 struct lttng_ht_iter iter
;
3341 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3343 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3344 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3347 * Check by sessiond id which is unique here where the relayd session
3348 * id might not be when having multiple relayd.
3350 if (relayd
->sessiond_session_id
== id
) {
3351 /* Found the relayd. There can be only one per id. */
3363 * Check if for a given session id there is still data needed to be extract
3366 * Return 1 if data is pending or else 0 meaning ready to be read.
3368 int consumer_data_pending(uint64_t id
)
3371 struct lttng_ht_iter iter
;
3372 struct lttng_ht
*ht
;
3373 struct lttng_consumer_stream
*stream
;
3374 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3375 int (*data_pending
)(struct lttng_consumer_stream
*);
3377 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3380 pthread_mutex_lock(&consumer_data
.lock
);
3382 switch (consumer_data
.type
) {
3383 case LTTNG_CONSUMER_KERNEL
:
3384 data_pending
= lttng_kconsumer_data_pending
;
3386 case LTTNG_CONSUMER32_UST
:
3387 case LTTNG_CONSUMER64_UST
:
3388 data_pending
= lttng_ustconsumer_data_pending
;
3391 ERR("Unknown consumer data type");
3395 /* Ease our life a bit */
3396 ht
= consumer_data
.stream_list_ht
;
3398 relayd
= find_relayd_by_session_id(id
);
3400 /* Send init command for data pending. */
3401 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3402 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3403 relayd
->relayd_session_id
);
3404 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3406 /* Communication error thus the relayd so no data pending. */
3407 goto data_not_pending
;
3411 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3412 ht
->hash_fct(&id
, lttng_ht_seed
),
3414 &iter
.iter
, stream
, node_session_id
.node
) {
3415 /* If this call fails, the stream is being used hence data pending. */
3416 ret
= stream_try_lock(stream
);
3422 * A removed node from the hash table indicates that the stream has
3423 * been deleted thus having a guarantee that the buffers are closed
3424 * on the consumer side. However, data can still be transmitted
3425 * over the network so don't skip the relayd check.
3427 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3430 * An empty output file is not valid. We need at least one packet
3431 * generated per stream, even if it contains no event, so it
3432 * contains at least one packet header.
3434 if (stream
->output_written
== 0) {
3435 pthread_mutex_unlock(&stream
->lock
);
3438 /* Check the stream if there is data in the buffers. */
3439 ret
= data_pending(stream
);
3441 pthread_mutex_unlock(&stream
->lock
);
3448 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3449 if (stream
->metadata_flag
) {
3450 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3451 stream
->relayd_stream_id
);
3453 ret
= relayd_data_pending(&relayd
->control_sock
,
3454 stream
->relayd_stream_id
,
3455 stream
->next_net_seq_num
- 1);
3457 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3459 pthread_mutex_unlock(&stream
->lock
);
3463 pthread_mutex_unlock(&stream
->lock
);
3467 unsigned int is_data_inflight
= 0;
3469 /* Send init command for data pending. */
3470 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3471 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3472 relayd
->relayd_session_id
, &is_data_inflight
);
3473 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3475 goto data_not_pending
;
3477 if (is_data_inflight
) {
3483 * Finding _no_ node in the hash table and no inflight data means that the
3484 * stream(s) have been removed thus data is guaranteed to be available for
3485 * analysis from the trace files.
3489 /* Data is available to be read by a viewer. */
3490 pthread_mutex_unlock(&consumer_data
.lock
);
3495 /* Data is still being extracted from buffers. */
3496 pthread_mutex_unlock(&consumer_data
.lock
);
3502 * Send a ret code status message to the sessiond daemon.
3504 * Return the sendmsg() return value.
3506 int consumer_send_status_msg(int sock
, int ret_code
)
3508 struct lttcomm_consumer_status_msg msg
;
3510 msg
.ret_code
= ret_code
;
3512 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3516 * Send a channel status message to the sessiond daemon.
3518 * Return the sendmsg() return value.
3520 int consumer_send_status_channel(int sock
,
3521 struct lttng_consumer_channel
*channel
)
3523 struct lttcomm_consumer_status_channel msg
;
3528 msg
.ret_code
= -LTTNG_ERR_UST_CHAN_FAIL
;
3530 msg
.ret_code
= LTTNG_OK
;
3531 msg
.key
= channel
->key
;
3532 msg
.stream_count
= channel
->streams
.count
;
3535 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));