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
);
296 pthread_mutex_lock(&channel
->timer_lock
);
298 switch (consumer_data
.type
) {
299 case LTTNG_CONSUMER_KERNEL
:
301 case LTTNG_CONSUMER32_UST
:
302 case LTTNG_CONSUMER64_UST
:
303 /* Delete streams that might have been left in the stream list. */
304 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
306 cds_list_del(&stream
->send_node
);
307 lttng_ustconsumer_del_stream(stream
);
310 lttng_ustconsumer_del_channel(channel
);
313 ERR("Unknown consumer_data type");
319 iter
.iter
.node
= &channel
->node
.node
;
320 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
324 call_rcu(&channel
->node
.head
, free_channel_rcu
);
326 pthread_mutex_unlock(&channel
->timer_lock
);
327 pthread_mutex_unlock(&channel
->lock
);
328 pthread_mutex_unlock(&consumer_data
.lock
);
332 * Iterate over the relayd hash table and destroy each element. Finally,
333 * destroy the whole hash table.
335 static void cleanup_relayd_ht(void)
337 struct lttng_ht_iter iter
;
338 struct consumer_relayd_sock_pair
*relayd
;
342 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
344 destroy_relayd(relayd
);
349 lttng_ht_destroy(consumer_data
.relayd_ht
);
353 * Update the end point status of all streams having the given network sequence
354 * index (relayd index).
356 * It's atomically set without having the stream mutex locked which is fine
357 * because we handle the write/read race with a pipe wakeup for each thread.
359 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
360 enum consumer_endpoint_status status
)
362 struct lttng_ht_iter iter
;
363 struct lttng_consumer_stream
*stream
;
365 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
369 /* Let's begin with metadata */
370 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
371 if (stream
->net_seq_idx
== net_seq_idx
) {
372 uatomic_set(&stream
->endpoint_status
, status
);
373 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
377 /* Follow up by the data streams */
378 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
379 if (stream
->net_seq_idx
== net_seq_idx
) {
380 uatomic_set(&stream
->endpoint_status
, status
);
381 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
388 * Cleanup a relayd object by flagging every associated streams for deletion,
389 * destroying the object meaning removing it from the relayd hash table,
390 * closing the sockets and freeing the memory in a RCU call.
392 * If a local data context is available, notify the threads that the streams'
393 * state have changed.
395 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
396 struct lttng_consumer_local_data
*ctx
)
402 DBG("Cleaning up relayd sockets");
404 /* Save the net sequence index before destroying the object */
405 netidx
= relayd
->net_seq_idx
;
408 * Delete the relayd from the relayd hash table, close the sockets and free
409 * the object in a RCU call.
411 destroy_relayd(relayd
);
413 /* Set inactive endpoint to all streams */
414 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
417 * With a local data context, notify the threads that the streams' state
418 * have changed. The write() action on the pipe acts as an "implicit"
419 * memory barrier ordering the updates of the end point status from the
420 * read of this status which happens AFTER receiving this notify.
423 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
424 notify_thread_lttng_pipe(ctx
->consumer_metadata_pipe
);
429 * Flag a relayd socket pair for destruction. Destroy it if the refcount
432 * RCU read side lock MUST be aquired before calling this function.
434 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
438 /* Set destroy flag for this object */
439 uatomic_set(&relayd
->destroy_flag
, 1);
441 /* Destroy the relayd if refcount is 0 */
442 if (uatomic_read(&relayd
->refcount
) == 0) {
443 destroy_relayd(relayd
);
448 * Remove a stream from the global list protected by a mutex. This
449 * function is also responsible for freeing its data structures.
451 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
455 struct lttng_ht_iter iter
;
456 struct lttng_consumer_channel
*free_chan
= NULL
;
457 struct consumer_relayd_sock_pair
*relayd
;
461 DBG("Consumer del stream %d", stream
->wait_fd
);
464 /* Means the stream was allocated but not successfully added */
465 goto free_stream_rcu
;
468 pthread_mutex_lock(&consumer_data
.lock
);
469 pthread_mutex_lock(&stream
->lock
);
471 switch (consumer_data
.type
) {
472 case LTTNG_CONSUMER_KERNEL
:
473 if (stream
->mmap_base
!= NULL
) {
474 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
480 if (stream
->wait_fd
>= 0) {
481 ret
= close(stream
->wait_fd
);
487 case LTTNG_CONSUMER32_UST
:
488 case LTTNG_CONSUMER64_UST
:
489 lttng_ustconsumer_del_stream(stream
);
492 ERR("Unknown consumer_data type");
498 iter
.iter
.node
= &stream
->node
.node
;
499 ret
= lttng_ht_del(ht
, &iter
);
502 iter
.iter
.node
= &stream
->node_channel_id
.node
;
503 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
506 iter
.iter
.node
= &stream
->node_session_id
.node
;
507 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
511 assert(consumer_data
.stream_count
> 0);
512 consumer_data
.stream_count
--;
514 if (stream
->out_fd
>= 0) {
515 ret
= close(stream
->out_fd
);
521 /* Check and cleanup relayd */
523 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
524 if (relayd
!= NULL
) {
525 uatomic_dec(&relayd
->refcount
);
526 assert(uatomic_read(&relayd
->refcount
) >= 0);
528 /* Closing streams requires to lock the control socket. */
529 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
530 ret
= relayd_send_close_stream(&relayd
->control_sock
,
531 stream
->relayd_stream_id
,
532 stream
->next_net_seq_num
- 1);
533 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
535 DBG("Unable to close stream on the relayd. Continuing");
537 * Continue here. There is nothing we can do for the relayd.
538 * Chances are that the relayd has closed the socket so we just
539 * continue cleaning up.
543 /* Both conditions are met, we destroy the relayd. */
544 if (uatomic_read(&relayd
->refcount
) == 0 &&
545 uatomic_read(&relayd
->destroy_flag
)) {
546 destroy_relayd(relayd
);
551 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
552 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
553 free_chan
= stream
->chan
;
557 consumer_data
.need_update
= 1;
558 pthread_mutex_unlock(&stream
->lock
);
559 pthread_mutex_unlock(&consumer_data
.lock
);
562 consumer_del_channel(free_chan
);
566 call_rcu(&stream
->node
.head
, free_stream_rcu
);
569 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
571 enum lttng_consumer_stream_state state
,
572 const char *channel_name
,
579 enum consumer_channel_type type
)
582 struct lttng_consumer_stream
*stream
;
584 stream
= zmalloc(sizeof(*stream
));
585 if (stream
== NULL
) {
586 PERROR("malloc struct lttng_consumer_stream");
593 stream
->key
= stream_key
;
595 stream
->out_fd_offset
= 0;
596 stream
->state
= state
;
599 stream
->net_seq_idx
= relayd_id
;
600 stream
->session_id
= session_id
;
601 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
602 pthread_mutex_init(&stream
->lock
, NULL
);
604 /* If channel is the metadata, flag this stream as metadata. */
605 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
606 stream
->metadata_flag
= 1;
607 /* Metadata is flat out. */
608 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
610 /* Format stream name to <channel_name>_<cpu_number> */
611 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
614 PERROR("snprintf stream name");
619 /* Key is always the wait_fd for streams. */
620 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
622 /* Init node per channel id key */
623 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
625 /* Init session id node with the stream session id */
626 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
628 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
" relayd_id %" PRIu64
", session_id %" PRIu64
,
629 stream
->name
, stream
->key
, channel_key
, stream
->net_seq_idx
, stream
->session_id
);
645 * Add a stream to the global list protected by a mutex.
647 static int add_stream(struct lttng_consumer_stream
*stream
,
651 struct consumer_relayd_sock_pair
*relayd
;
656 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
658 pthread_mutex_lock(&consumer_data
.lock
);
659 pthread_mutex_lock(&stream
->chan
->lock
);
660 pthread_mutex_lock(&stream
->chan
->timer_lock
);
661 pthread_mutex_lock(&stream
->lock
);
664 /* Steal stream identifier to avoid having streams with the same key */
665 steal_stream_key(stream
->key
, ht
);
667 lttng_ht_add_unique_u64(ht
, &stream
->node
);
669 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
670 &stream
->node_channel_id
);
673 * Add stream to the stream_list_ht of the consumer data. No need to steal
674 * the key since the HT does not use it and we allow to add redundant keys
677 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
679 /* Check and cleanup relayd */
680 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
681 if (relayd
!= NULL
) {
682 uatomic_inc(&relayd
->refcount
);
686 * When nb_init_stream_left reaches 0, we don't need to trigger any action
687 * in terms of destroying the associated channel, because the action that
688 * causes the count to become 0 also causes a stream to be added. The
689 * channel deletion will thus be triggered by the following removal of this
692 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
693 /* Increment refcount before decrementing nb_init_stream_left */
695 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
698 /* Update consumer data once the node is inserted. */
699 consumer_data
.stream_count
++;
700 consumer_data
.need_update
= 1;
703 pthread_mutex_unlock(&stream
->lock
);
704 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
705 pthread_mutex_unlock(&stream
->chan
->lock
);
706 pthread_mutex_unlock(&consumer_data
.lock
);
712 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
713 * be acquired before calling this.
715 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
718 struct lttng_ht_node_u64
*node
;
719 struct lttng_ht_iter iter
;
723 lttng_ht_lookup(consumer_data
.relayd_ht
,
724 &relayd
->net_seq_idx
, &iter
);
725 node
= lttng_ht_iter_get_node_u64(&iter
);
729 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
736 * Allocate and return a consumer relayd socket.
738 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
739 uint64_t net_seq_idx
)
741 struct consumer_relayd_sock_pair
*obj
= NULL
;
743 /* net sequence index of -1 is a failure */
744 if (net_seq_idx
== (uint64_t) -1ULL) {
748 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
750 PERROR("zmalloc relayd sock");
754 obj
->net_seq_idx
= net_seq_idx
;
756 obj
->destroy_flag
= 0;
757 obj
->control_sock
.sock
.fd
= -1;
758 obj
->data_sock
.sock
.fd
= -1;
759 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
760 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
767 * Find a relayd socket pair in the global consumer data.
769 * Return the object if found else NULL.
770 * RCU read-side lock must be held across this call and while using the
773 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
775 struct lttng_ht_iter iter
;
776 struct lttng_ht_node_u64
*node
;
777 struct consumer_relayd_sock_pair
*relayd
= NULL
;
779 /* Negative keys are lookup failures */
780 if (key
== (uint64_t) -1ULL) {
784 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
786 node
= lttng_ht_iter_get_node_u64(&iter
);
788 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
796 * Handle stream for relayd transmission if the stream applies for network
797 * streaming where the net sequence index is set.
799 * Return destination file descriptor or negative value on error.
801 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
802 size_t data_size
, unsigned long padding
,
803 struct consumer_relayd_sock_pair
*relayd
)
806 struct lttcomm_relayd_data_hdr data_hdr
;
812 /* Reset data header */
813 memset(&data_hdr
, 0, sizeof(data_hdr
));
815 if (stream
->metadata_flag
) {
816 /* Caller MUST acquire the relayd control socket lock */
817 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
822 /* Metadata are always sent on the control socket. */
823 outfd
= relayd
->control_sock
.sock
.fd
;
825 /* Set header with stream information */
826 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
827 data_hdr
.data_size
= htobe32(data_size
);
828 data_hdr
.padding_size
= htobe32(padding
);
830 * Note that net_seq_num below is assigned with the *current* value of
831 * next_net_seq_num and only after that the next_net_seq_num will be
832 * increment. This is why when issuing a command on the relayd using
833 * this next value, 1 should always be substracted in order to compare
834 * the last seen sequence number on the relayd side to the last sent.
836 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
837 /* Other fields are zeroed previously */
839 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
845 ++stream
->next_net_seq_num
;
847 /* Set to go on data socket */
848 outfd
= relayd
->data_sock
.sock
.fd
;
856 * Allocate and return a new lttng_consumer_channel object using the given key
857 * to initialize the hash table node.
859 * On error, return NULL.
861 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
863 const char *pathname
,
868 enum lttng_event_output output
,
869 uint64_t tracefile_size
,
870 uint64_t tracefile_count
,
871 uint64_t session_id_per_pid
)
873 struct lttng_consumer_channel
*channel
;
875 channel
= zmalloc(sizeof(*channel
));
876 if (channel
== NULL
) {
877 PERROR("malloc struct lttng_consumer_channel");
882 channel
->refcount
= 0;
883 channel
->session_id
= session_id
;
884 channel
->session_id_per_pid
= session_id_per_pid
;
887 channel
->relayd_id
= relayd_id
;
888 channel
->output
= output
;
889 channel
->tracefile_size
= tracefile_size
;
890 channel
->tracefile_count
= tracefile_count
;
891 pthread_mutex_init(&channel
->lock
, NULL
);
892 pthread_mutex_init(&channel
->timer_lock
, NULL
);
894 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
895 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
897 strncpy(channel
->name
, name
, sizeof(channel
->name
));
898 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
900 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
902 channel
->wait_fd
= -1;
904 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
906 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
913 * Add a channel to the global list protected by a mutex.
915 * On success 0 is returned else a negative value.
917 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
918 struct lttng_consumer_local_data
*ctx
)
921 struct lttng_ht_node_u64
*node
;
922 struct lttng_ht_iter iter
;
924 pthread_mutex_lock(&consumer_data
.lock
);
925 pthread_mutex_lock(&channel
->lock
);
926 pthread_mutex_lock(&channel
->timer_lock
);
929 lttng_ht_lookup(consumer_data
.channel_ht
, &channel
->key
, &iter
);
930 node
= lttng_ht_iter_get_node_u64(&iter
);
932 /* Channel already exist. Ignore the insertion */
933 ERR("Consumer add channel key %" PRIu64
" already exists!",
939 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
943 pthread_mutex_unlock(&channel
->timer_lock
);
944 pthread_mutex_unlock(&channel
->lock
);
945 pthread_mutex_unlock(&consumer_data
.lock
);
947 if (!ret
&& channel
->wait_fd
!= -1 &&
948 channel
->metadata_stream
== NULL
) {
949 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
955 * Allocate the pollfd structure and the local view of the out fds to avoid
956 * doing a lookup in the linked list and concurrency issues when writing is
957 * needed. Called with consumer_data.lock held.
959 * Returns the number of fds in the structures.
961 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
962 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
966 struct lttng_ht_iter iter
;
967 struct lttng_consumer_stream
*stream
;
972 assert(local_stream
);
974 DBG("Updating poll fd array");
976 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
978 * Only active streams with an active end point can be added to the
979 * poll set and local stream storage of the thread.
981 * There is a potential race here for endpoint_status to be updated
982 * just after the check. However, this is OK since the stream(s) will
983 * be deleted once the thread is notified that the end point state has
984 * changed where this function will be called back again.
986 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
987 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
991 * This clobbers way too much the debug output. Uncomment that if you
992 * need it for debugging purposes.
994 * DBG("Active FD %d", stream->wait_fd);
996 (*pollfd
)[i
].fd
= stream
->wait_fd
;
997 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
998 local_stream
[i
] = stream
;
1004 * Insert the consumer_data_pipe at the end of the array and don't
1005 * increment i so nb_fd is the number of real FD.
1007 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1008 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1013 * Poll on the should_quit pipe and the command socket return -1 on error and
1014 * should exit, 0 if data is available on the command socket
1016 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1021 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1022 if (num_rdy
== -1) {
1024 * Restart interrupted system call.
1026 if (errno
== EINTR
) {
1029 PERROR("Poll error");
1032 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1033 DBG("consumer_should_quit wake up");
1043 * Set the error socket.
1045 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1048 ctx
->consumer_error_socket
= sock
;
1052 * Set the command socket path.
1054 void lttng_consumer_set_command_sock_path(
1055 struct lttng_consumer_local_data
*ctx
, char *sock
)
1057 ctx
->consumer_command_sock_path
= sock
;
1061 * Send return code to the session daemon.
1062 * If the socket is not defined, we return 0, it is not a fatal error
1064 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1066 if (ctx
->consumer_error_socket
> 0) {
1067 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1068 sizeof(enum lttcomm_sessiond_command
));
1075 * Close all the tracefiles and stream fds and MUST be called when all
1076 * instances are destroyed i.e. when all threads were joined and are ended.
1078 void lttng_consumer_cleanup(void)
1080 struct lttng_ht_iter iter
;
1081 struct lttng_consumer_channel
*channel
;
1085 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1087 consumer_del_channel(channel
);
1092 lttng_ht_destroy(consumer_data
.channel_ht
);
1094 cleanup_relayd_ht();
1096 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1099 * This HT contains streams that are freed by either the metadata thread or
1100 * the data thread so we do *nothing* on the hash table and simply destroy
1103 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1107 * Called from signal handler.
1109 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1114 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1115 } while (ret
< 0 && errno
== EINTR
);
1116 if (ret
< 0 || ret
!= 1) {
1117 PERROR("write consumer quit");
1120 DBG("Consumer flag that it should quit");
1123 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1126 int outfd
= stream
->out_fd
;
1129 * This does a blocking write-and-wait on any page that belongs to the
1130 * subbuffer prior to the one we just wrote.
1131 * Don't care about error values, as these are just hints and ways to
1132 * limit the amount of page cache used.
1134 if (orig_offset
< stream
->max_sb_size
) {
1137 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1138 stream
->max_sb_size
,
1139 SYNC_FILE_RANGE_WAIT_BEFORE
1140 | SYNC_FILE_RANGE_WRITE
1141 | SYNC_FILE_RANGE_WAIT_AFTER
);
1143 * Give hints to the kernel about how we access the file:
1144 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1147 * We need to call fadvise again after the file grows because the
1148 * kernel does not seem to apply fadvise to non-existing parts of the
1151 * Call fadvise _after_ having waited for the page writeback to
1152 * complete because the dirty page writeback semantic is not well
1153 * defined. So it can be expected to lead to lower throughput in
1156 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1157 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1161 * Initialise the necessary environnement :
1162 * - create a new context
1163 * - create the poll_pipe
1164 * - create the should_quit pipe (for signal handler)
1165 * - create the thread pipe (for splice)
1167 * Takes a function pointer as argument, this function is called when data is
1168 * available on a buffer. This function is responsible to do the
1169 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1170 * buffer configuration and then kernctl_put_next_subbuf at the end.
1172 * Returns a pointer to the new context or NULL on error.
1174 struct lttng_consumer_local_data
*lttng_consumer_create(
1175 enum lttng_consumer_type type
,
1176 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1177 struct lttng_consumer_local_data
*ctx
),
1178 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1179 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1180 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1183 struct lttng_consumer_local_data
*ctx
;
1185 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1186 consumer_data
.type
== type
);
1187 consumer_data
.type
= type
;
1189 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1191 PERROR("allocating context");
1195 ctx
->consumer_error_socket
= -1;
1196 ctx
->consumer_metadata_socket
= -1;
1197 /* assign the callbacks */
1198 ctx
->on_buffer_ready
= buffer_ready
;
1199 ctx
->on_recv_channel
= recv_channel
;
1200 ctx
->on_recv_stream
= recv_stream
;
1201 ctx
->on_update_stream
= update_stream
;
1203 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1204 if (!ctx
->consumer_data_pipe
) {
1205 goto error_poll_pipe
;
1208 ret
= pipe(ctx
->consumer_should_quit
);
1210 PERROR("Error creating recv pipe");
1211 goto error_quit_pipe
;
1214 ret
= pipe(ctx
->consumer_thread_pipe
);
1216 PERROR("Error creating thread pipe");
1217 goto error_thread_pipe
;
1220 ret
= pipe(ctx
->consumer_channel_pipe
);
1222 PERROR("Error creating channel pipe");
1223 goto error_channel_pipe
;
1226 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1227 if (!ctx
->consumer_metadata_pipe
) {
1228 goto error_metadata_pipe
;
1231 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1233 goto error_splice_pipe
;
1239 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1240 error_metadata_pipe
:
1241 utils_close_pipe(ctx
->consumer_channel_pipe
);
1243 utils_close_pipe(ctx
->consumer_thread_pipe
);
1245 utils_close_pipe(ctx
->consumer_should_quit
);
1247 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1255 * Close all fds associated with the instance and free the context.
1257 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1261 DBG("Consumer destroying it. Closing everything.");
1263 ret
= close(ctx
->consumer_error_socket
);
1267 ret
= close(ctx
->consumer_metadata_socket
);
1271 utils_close_pipe(ctx
->consumer_thread_pipe
);
1272 utils_close_pipe(ctx
->consumer_channel_pipe
);
1273 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1274 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1275 utils_close_pipe(ctx
->consumer_should_quit
);
1276 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1278 unlink(ctx
->consumer_command_sock_path
);
1283 * Write the metadata stream id on the specified file descriptor.
1285 static int write_relayd_metadata_id(int fd
,
1286 struct lttng_consumer_stream
*stream
,
1287 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1290 struct lttcomm_relayd_metadata_payload hdr
;
1292 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1293 hdr
.padding_size
= htobe32(padding
);
1295 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1296 } while (ret
< 0 && errno
== EINTR
);
1297 if (ret
< 0 || ret
!= sizeof(hdr
)) {
1299 * This error means that the fd's end is closed so ignore the perror
1300 * not to clubber the error output since this can happen in a normal
1303 if (errno
!= EPIPE
) {
1304 PERROR("write metadata stream id");
1306 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1308 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1309 * handle writting the missing part so report that as an error and
1310 * don't lie to the caller.
1315 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1316 stream
->relayd_stream_id
, padding
);
1323 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1324 * core function for writing trace buffers to either the local filesystem or
1327 * It must be called with the stream lock held.
1329 * Careful review MUST be put if any changes occur!
1331 * Returns the number of bytes written
1333 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1334 struct lttng_consumer_local_data
*ctx
,
1335 struct lttng_consumer_stream
*stream
, unsigned long len
,
1336 unsigned long padding
)
1338 unsigned long mmap_offset
;
1340 ssize_t ret
= 0, written
= 0;
1341 off_t orig_offset
= stream
->out_fd_offset
;
1342 /* Default is on the disk */
1343 int outfd
= stream
->out_fd
;
1344 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1345 unsigned int relayd_hang_up
= 0;
1347 /* RCU lock for the relayd pointer */
1350 /* Flag that the current stream if set for network streaming. */
1351 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1352 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1353 if (relayd
== NULL
) {
1358 /* get the offset inside the fd to mmap */
1359 switch (consumer_data
.type
) {
1360 case LTTNG_CONSUMER_KERNEL
:
1361 mmap_base
= stream
->mmap_base
;
1362 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1364 case LTTNG_CONSUMER32_UST
:
1365 case LTTNG_CONSUMER64_UST
:
1366 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1368 ERR("read mmap get mmap base for stream %s", stream
->name
);
1372 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1376 ERR("Unknown consumer_data type");
1381 PERROR("tracer ctl get_mmap_read_offset");
1386 /* Handle stream on the relayd if the output is on the network */
1388 unsigned long netlen
= len
;
1391 * Lock the control socket for the complete duration of the function
1392 * since from this point on we will use the socket.
1394 if (stream
->metadata_flag
) {
1395 /* Metadata requires the control socket. */
1396 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1397 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1400 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1402 /* Use the returned socket. */
1405 /* Write metadata stream id before payload */
1406 if (stream
->metadata_flag
) {
1407 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1410 /* Socket operation failed. We consider the relayd dead */
1411 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1419 /* Socket operation failed. We consider the relayd dead */
1420 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1424 /* Else, use the default set before which is the filesystem. */
1427 /* No streaming, we have to set the len with the full padding */
1431 * Check if we need to change the tracefile before writing the packet.
1433 if (stream
->chan
->tracefile_size
> 0 &&
1434 (stream
->tracefile_size_current
+ len
) >
1435 stream
->chan
->tracefile_size
) {
1436 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1437 stream
->name
, stream
->chan
->tracefile_size
,
1438 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1439 stream
->out_fd
, &(stream
->tracefile_count_current
));
1441 ERR("Rotating output file");
1444 outfd
= stream
->out_fd
= ret
;
1445 /* Reset current size because we just perform a rotation. */
1446 stream
->tracefile_size_current
= 0;
1448 stream
->tracefile_size_current
+= len
;
1453 ret
= write(outfd
, mmap_base
+ mmap_offset
, len
);
1454 } while (ret
< 0 && errno
== EINTR
);
1455 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1458 * This is possible if the fd is closed on the other side (outfd)
1459 * or any write problem. It can be verbose a bit for a normal
1460 * execution if for instance the relayd is stopped abruptly. This
1461 * can happen so set this to a DBG statement.
1463 DBG("Error in file write mmap");
1467 /* Socket operation failed. We consider the relayd dead */
1468 if (errno
== EPIPE
|| errno
== EINVAL
) {
1473 } else if (ret
> len
) {
1474 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1482 /* This call is useless on a socket so better save a syscall. */
1484 /* This won't block, but will start writeout asynchronously */
1485 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1486 SYNC_FILE_RANGE_WRITE
);
1487 stream
->out_fd_offset
+= ret
;
1491 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1495 * This is a special case that the relayd has closed its socket. Let's
1496 * cleanup the relayd object and all associated streams.
1498 if (relayd
&& relayd_hang_up
) {
1499 cleanup_relayd(relayd
, ctx
);
1503 /* Unlock only if ctrl socket used */
1504 if (relayd
&& stream
->metadata_flag
) {
1505 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1513 * Splice the data from the ring buffer to the tracefile.
1515 * It must be called with the stream lock held.
1517 * Returns the number of bytes spliced.
1519 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1520 struct lttng_consumer_local_data
*ctx
,
1521 struct lttng_consumer_stream
*stream
, unsigned long len
,
1522 unsigned long padding
)
1524 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1526 off_t orig_offset
= stream
->out_fd_offset
;
1527 int fd
= stream
->wait_fd
;
1528 /* Default is on the disk */
1529 int outfd
= stream
->out_fd
;
1530 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1532 unsigned int relayd_hang_up
= 0;
1534 switch (consumer_data
.type
) {
1535 case LTTNG_CONSUMER_KERNEL
:
1537 case LTTNG_CONSUMER32_UST
:
1538 case LTTNG_CONSUMER64_UST
:
1539 /* Not supported for user space tracing */
1542 ERR("Unknown consumer_data type");
1546 /* RCU lock for the relayd pointer */
1549 /* Flag that the current stream if set for network streaming. */
1550 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1551 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1552 if (relayd
== NULL
) {
1558 * Choose right pipe for splice. Metadata and trace data are handled by
1559 * different threads hence the use of two pipes in order not to race or
1560 * corrupt the written data.
1562 if (stream
->metadata_flag
) {
1563 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1565 splice_pipe
= ctx
->consumer_thread_pipe
;
1568 /* Write metadata stream id before payload */
1570 int total_len
= len
;
1572 if (stream
->metadata_flag
) {
1574 * Lock the control socket for the complete duration of the function
1575 * since from this point on we will use the socket.
1577 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1579 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1583 /* Socket operation failed. We consider the relayd dead */
1584 if (ret
== -EBADF
) {
1585 WARN("Remote relayd disconnected. Stopping");
1592 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1595 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1597 /* Use the returned socket. */
1600 /* Socket operation failed. We consider the relayd dead */
1601 if (ret
== -EBADF
) {
1602 WARN("Remote relayd disconnected. Stopping");
1609 /* No streaming, we have to set the len with the full padding */
1613 * Check if we need to change the tracefile before writing the packet.
1615 if (stream
->chan
->tracefile_size
> 0 &&
1616 (stream
->tracefile_size_current
+ len
) >
1617 stream
->chan
->tracefile_size
) {
1618 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1619 stream
->name
, stream
->chan
->tracefile_size
,
1620 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1621 stream
->out_fd
, &(stream
->tracefile_count_current
));
1623 ERR("Rotating output file");
1626 outfd
= stream
->out_fd
= ret
;
1627 /* Reset current size because we just perform a rotation. */
1628 stream
->tracefile_size_current
= 0;
1630 stream
->tracefile_size_current
+= len
;
1634 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1635 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1636 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1637 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1638 DBG("splice chan to pipe, ret %zd", ret_splice
);
1639 if (ret_splice
< 0) {
1640 PERROR("Error in relay splice");
1642 written
= ret_splice
;
1648 /* Handle stream on the relayd if the output is on the network */
1650 if (stream
->metadata_flag
) {
1651 size_t metadata_payload_size
=
1652 sizeof(struct lttcomm_relayd_metadata_payload
);
1654 /* Update counter to fit the spliced data */
1655 ret_splice
+= metadata_payload_size
;
1656 len
+= metadata_payload_size
;
1658 * We do this so the return value can match the len passed as
1659 * argument to this function.
1661 written
-= metadata_payload_size
;
1665 /* Splice data out */
1666 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1667 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1668 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1669 if (ret_splice
< 0) {
1670 PERROR("Error in file splice");
1672 written
= ret_splice
;
1674 /* Socket operation failed. We consider the relayd dead */
1675 if (errno
== EBADF
|| errno
== EPIPE
) {
1676 WARN("Remote relayd disconnected. Stopping");
1682 } else if (ret_splice
> len
) {
1684 PERROR("Wrote more data than requested %zd (len: %lu)",
1686 written
+= ret_splice
;
1692 /* This call is useless on a socket so better save a syscall. */
1694 /* This won't block, but will start writeout asynchronously */
1695 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1696 SYNC_FILE_RANGE_WRITE
);
1697 stream
->out_fd_offset
+= ret_splice
;
1699 written
+= ret_splice
;
1701 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1709 * This is a special case that the relayd has closed its socket. Let's
1710 * cleanup the relayd object and all associated streams.
1712 if (relayd
&& relayd_hang_up
) {
1713 cleanup_relayd(relayd
, ctx
);
1714 /* Skip splice error so the consumer does not fail */
1719 /* send the appropriate error description to sessiond */
1722 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1725 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1728 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1733 if (relayd
&& stream
->metadata_flag
) {
1734 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1742 * Take a snapshot for a specific fd
1744 * Returns 0 on success, < 0 on error
1746 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1748 switch (consumer_data
.type
) {
1749 case LTTNG_CONSUMER_KERNEL
:
1750 return lttng_kconsumer_take_snapshot(stream
);
1751 case LTTNG_CONSUMER32_UST
:
1752 case LTTNG_CONSUMER64_UST
:
1753 return lttng_ustconsumer_take_snapshot(stream
);
1755 ERR("Unknown consumer_data type");
1762 * Get the produced position
1764 * Returns 0 on success, < 0 on error
1766 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1769 switch (consumer_data
.type
) {
1770 case LTTNG_CONSUMER_KERNEL
:
1771 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1772 case LTTNG_CONSUMER32_UST
:
1773 case LTTNG_CONSUMER64_UST
:
1774 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1776 ERR("Unknown consumer_data type");
1782 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1783 int sock
, struct pollfd
*consumer_sockpoll
)
1785 switch (consumer_data
.type
) {
1786 case LTTNG_CONSUMER_KERNEL
:
1787 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1788 case LTTNG_CONSUMER32_UST
:
1789 case LTTNG_CONSUMER64_UST
:
1790 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1792 ERR("Unknown consumer_data type");
1799 * Iterate over all streams of the hashtable and free them properly.
1801 * WARNING: *MUST* be used with data stream only.
1803 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1805 struct lttng_ht_iter iter
;
1806 struct lttng_consumer_stream
*stream
;
1813 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1815 * Ignore return value since we are currently cleaning up so any error
1818 (void) consumer_del_stream(stream
, ht
);
1822 lttng_ht_destroy(ht
);
1826 * Iterate over all streams of the hashtable and free them properly.
1828 * XXX: Should not be only for metadata stream or else use an other name.
1830 static void destroy_stream_ht(struct lttng_ht
*ht
)
1832 struct lttng_ht_iter iter
;
1833 struct lttng_consumer_stream
*stream
;
1840 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1842 * Ignore return value since we are currently cleaning up so any error
1845 (void) consumer_del_metadata_stream(stream
, ht
);
1849 lttng_ht_destroy(ht
);
1852 void lttng_consumer_close_metadata(void)
1854 switch (consumer_data
.type
) {
1855 case LTTNG_CONSUMER_KERNEL
:
1857 * The Kernel consumer has a different metadata scheme so we don't
1858 * close anything because the stream will be closed by the session
1862 case LTTNG_CONSUMER32_UST
:
1863 case LTTNG_CONSUMER64_UST
:
1865 * Close all metadata streams. The metadata hash table is passed and
1866 * this call iterates over it by closing all wakeup fd. This is safe
1867 * because at this point we are sure that the metadata producer is
1868 * either dead or blocked.
1870 lttng_ustconsumer_close_metadata(metadata_ht
);
1873 ERR("Unknown consumer_data type");
1879 * Clean up a metadata stream and free its memory.
1881 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1882 struct lttng_ht
*ht
)
1885 struct lttng_ht_iter iter
;
1886 struct lttng_consumer_channel
*free_chan
= NULL
;
1887 struct consumer_relayd_sock_pair
*relayd
;
1891 * This call should NEVER receive regular stream. It must always be
1892 * metadata stream and this is crucial for data structure synchronization.
1894 assert(stream
->metadata_flag
);
1896 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1899 /* Means the stream was allocated but not successfully added */
1900 goto free_stream_rcu
;
1903 pthread_mutex_lock(&consumer_data
.lock
);
1904 pthread_mutex_lock(&stream
->chan
->lock
);
1905 pthread_mutex_lock(&stream
->chan
->timer_lock
);
1906 pthread_mutex_lock(&stream
->lock
);
1908 switch (consumer_data
.type
) {
1909 case LTTNG_CONSUMER_KERNEL
:
1910 if (stream
->mmap_base
!= NULL
) {
1911 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1913 PERROR("munmap metadata stream");
1917 if (stream
->wait_fd
>= 0) {
1918 ret
= close(stream
->wait_fd
);
1920 PERROR("close kernel metadata wait_fd");
1924 case LTTNG_CONSUMER32_UST
:
1925 case LTTNG_CONSUMER64_UST
:
1926 lttng_ustconsumer_del_stream(stream
);
1929 ERR("Unknown consumer_data type");
1935 iter
.iter
.node
= &stream
->node
.node
;
1936 ret
= lttng_ht_del(ht
, &iter
);
1939 iter
.iter
.node
= &stream
->node_channel_id
.node
;
1940 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
1943 iter
.iter
.node
= &stream
->node_session_id
.node
;
1944 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1948 if (stream
->out_fd
>= 0) {
1949 ret
= close(stream
->out_fd
);
1955 /* Check and cleanup relayd */
1957 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1958 if (relayd
!= NULL
) {
1959 uatomic_dec(&relayd
->refcount
);
1960 assert(uatomic_read(&relayd
->refcount
) >= 0);
1962 /* Closing streams requires to lock the control socket. */
1963 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1964 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1965 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1966 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1968 DBG("Unable to close stream on the relayd. Continuing");
1970 * Continue here. There is nothing we can do for the relayd.
1971 * Chances are that the relayd has closed the socket so we just
1972 * continue cleaning up.
1976 /* Both conditions are met, we destroy the relayd. */
1977 if (uatomic_read(&relayd
->refcount
) == 0 &&
1978 uatomic_read(&relayd
->destroy_flag
)) {
1979 destroy_relayd(relayd
);
1984 /* Atomically decrement channel refcount since other threads can use it. */
1985 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
1986 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
1987 /* Go for channel deletion! */
1988 free_chan
= stream
->chan
;
1993 * Nullify the stream reference so it is not used after deletion. The
1994 * consumer data lock MUST be acquired before being able to check for a
1995 * NULL pointer value.
1997 stream
->chan
->metadata_stream
= NULL
;
1999 pthread_mutex_unlock(&stream
->lock
);
2000 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2001 pthread_mutex_unlock(&stream
->chan
->lock
);
2002 pthread_mutex_unlock(&consumer_data
.lock
);
2005 consumer_del_channel(free_chan
);
2009 call_rcu(&stream
->node
.head
, free_stream_rcu
);
2013 * Action done with the metadata stream when adding it to the consumer internal
2014 * data structures to handle it.
2016 static int add_metadata_stream(struct lttng_consumer_stream
*stream
,
2017 struct lttng_ht
*ht
)
2020 struct consumer_relayd_sock_pair
*relayd
;
2021 struct lttng_ht_iter iter
;
2022 struct lttng_ht_node_u64
*node
;
2027 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2029 pthread_mutex_lock(&consumer_data
.lock
);
2030 pthread_mutex_lock(&stream
->chan
->lock
);
2031 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2032 pthread_mutex_lock(&stream
->lock
);
2035 * From here, refcounts are updated so be _careful_ when returning an error
2042 * Lookup the stream just to make sure it does not exist in our internal
2043 * state. This should NEVER happen.
2045 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2046 node
= lttng_ht_iter_get_node_u64(&iter
);
2049 /* Find relayd and, if one is found, increment refcount. */
2050 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
2051 if (relayd
!= NULL
) {
2052 uatomic_inc(&relayd
->refcount
);
2056 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2057 * in terms of destroying the associated channel, because the action that
2058 * causes the count to become 0 also causes a stream to be added. The
2059 * channel deletion will thus be triggered by the following removal of this
2062 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2063 /* Increment refcount before decrementing nb_init_stream_left */
2065 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2068 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2070 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2071 &stream
->node_channel_id
);
2074 * Add stream to the stream_list_ht of the consumer data. No need to steal
2075 * the key since the HT does not use it and we allow to add redundant keys
2078 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2082 pthread_mutex_unlock(&stream
->lock
);
2083 pthread_mutex_unlock(&stream
->chan
->lock
);
2084 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2085 pthread_mutex_unlock(&consumer_data
.lock
);
2090 * Delete data stream that are flagged for deletion (endpoint_status).
2092 static void validate_endpoint_status_data_stream(void)
2094 struct lttng_ht_iter iter
;
2095 struct lttng_consumer_stream
*stream
;
2097 DBG("Consumer delete flagged data stream");
2100 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2101 /* Validate delete flag of the stream */
2102 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2105 /* Delete it right now */
2106 consumer_del_stream(stream
, data_ht
);
2112 * Delete metadata stream that are flagged for deletion (endpoint_status).
2114 static void validate_endpoint_status_metadata_stream(
2115 struct lttng_poll_event
*pollset
)
2117 struct lttng_ht_iter iter
;
2118 struct lttng_consumer_stream
*stream
;
2120 DBG("Consumer delete flagged metadata stream");
2125 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2126 /* Validate delete flag of the stream */
2127 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2131 * Remove from pollset so the metadata thread can continue without
2132 * blocking on a deleted stream.
2134 lttng_poll_del(pollset
, stream
->wait_fd
);
2136 /* Delete it right now */
2137 consumer_del_metadata_stream(stream
, metadata_ht
);
2143 * Thread polls on metadata file descriptor and write them on disk or on the
2146 void *consumer_thread_metadata_poll(void *data
)
2149 uint32_t revents
, nb_fd
;
2150 struct lttng_consumer_stream
*stream
= NULL
;
2151 struct lttng_ht_iter iter
;
2152 struct lttng_ht_node_u64
*node
;
2153 struct lttng_poll_event events
;
2154 struct lttng_consumer_local_data
*ctx
= data
;
2157 rcu_register_thread();
2159 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2161 /* ENOMEM at this point. Better to bail out. */
2165 DBG("Thread metadata poll started");
2167 /* Size is set to 1 for the consumer_metadata pipe */
2168 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2170 ERR("Poll set creation failed");
2174 ret
= lttng_poll_add(&events
,
2175 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2181 DBG("Metadata main loop started");
2184 /* Only the metadata pipe is set */
2185 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2190 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2191 ret
= lttng_poll_wait(&events
, -1);
2192 DBG("Metadata event catched in thread");
2194 if (errno
== EINTR
) {
2195 ERR("Poll EINTR catched");
2203 /* From here, the event is a metadata wait fd */
2204 for (i
= 0; i
< nb_fd
; i
++) {
2205 revents
= LTTNG_POLL_GETEV(&events
, i
);
2206 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2208 /* Just don't waste time if no returned events for the fd */
2213 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2214 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2215 DBG("Metadata thread pipe hung up");
2217 * Remove the pipe from the poll set and continue the loop
2218 * since their might be data to consume.
2220 lttng_poll_del(&events
,
2221 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2222 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2224 } else if (revents
& LPOLLIN
) {
2227 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2228 &stream
, sizeof(stream
));
2230 ERR("read metadata stream, ret: %ld", pipe_len
);
2232 * Continue here to handle the rest of the streams.
2237 /* A NULL stream means that the state has changed. */
2238 if (stream
== NULL
) {
2239 /* Check for deleted streams. */
2240 validate_endpoint_status_metadata_stream(&events
);
2244 DBG("Adding metadata stream %d to poll set",
2247 ret
= add_metadata_stream(stream
, metadata_ht
);
2249 ERR("Unable to add metadata stream");
2250 /* Stream was not setup properly. Continuing. */
2251 consumer_del_metadata_stream(stream
, NULL
);
2255 /* Add metadata stream to the global poll events list */
2256 lttng_poll_add(&events
, stream
->wait_fd
,
2257 LPOLLIN
| LPOLLPRI
);
2260 /* Handle other stream */
2266 uint64_t tmp_id
= (uint64_t) pollfd
;
2268 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2270 node
= lttng_ht_iter_get_node_u64(&iter
);
2273 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2276 /* Check for error event */
2277 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2278 DBG("Metadata fd %d is hup|err.", pollfd
);
2279 if (!stream
->hangup_flush_done
2280 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2281 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2282 DBG("Attempting to flush and consume the UST buffers");
2283 lttng_ustconsumer_on_stream_hangup(stream
);
2285 /* We just flushed the stream now read it. */
2287 len
= ctx
->on_buffer_ready(stream
, ctx
);
2289 * We don't check the return value here since if we get
2290 * a negative len, it means an error occured thus we
2291 * simply remove it from the poll set and free the
2297 lttng_poll_del(&events
, stream
->wait_fd
);
2299 * This call update the channel states, closes file descriptors
2300 * and securely free the stream.
2302 consumer_del_metadata_stream(stream
, metadata_ht
);
2303 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2304 /* Get the data out of the metadata file descriptor */
2305 DBG("Metadata available on fd %d", pollfd
);
2306 assert(stream
->wait_fd
== pollfd
);
2308 len
= ctx
->on_buffer_ready(stream
, ctx
);
2309 /* It's ok to have an unavailable sub-buffer */
2310 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2311 /* Clean up stream from consumer and free it. */
2312 lttng_poll_del(&events
, stream
->wait_fd
);
2313 consumer_del_metadata_stream(stream
, metadata_ht
);
2314 } else if (len
> 0) {
2315 stream
->data_read
= 1;
2319 /* Release RCU lock for the stream looked up */
2326 DBG("Metadata poll thread exiting");
2328 lttng_poll_clean(&events
);
2330 destroy_stream_ht(metadata_ht
);
2332 rcu_unregister_thread();
2337 * This thread polls the fds in the set to consume the data and write
2338 * it to tracefile if necessary.
2340 void *consumer_thread_data_poll(void *data
)
2342 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2343 struct pollfd
*pollfd
= NULL
;
2344 /* local view of the streams */
2345 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2346 /* local view of consumer_data.fds_count */
2348 struct lttng_consumer_local_data
*ctx
= data
;
2351 rcu_register_thread();
2353 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2354 if (data_ht
== NULL
) {
2355 /* ENOMEM at this point. Better to bail out. */
2359 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
2366 * the fds set has been updated, we need to update our
2367 * local array as well
2369 pthread_mutex_lock(&consumer_data
.lock
);
2370 if (consumer_data
.need_update
) {
2375 local_stream
= NULL
;
2377 /* allocate for all fds + 1 for the consumer_data_pipe */
2378 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2379 if (pollfd
== NULL
) {
2380 PERROR("pollfd malloc");
2381 pthread_mutex_unlock(&consumer_data
.lock
);
2385 /* allocate for all fds + 1 for the consumer_data_pipe */
2386 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2387 sizeof(struct lttng_consumer_stream
*));
2388 if (local_stream
== NULL
) {
2389 PERROR("local_stream malloc");
2390 pthread_mutex_unlock(&consumer_data
.lock
);
2393 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2396 ERR("Error in allocating pollfd or local_outfds");
2397 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2398 pthread_mutex_unlock(&consumer_data
.lock
);
2402 consumer_data
.need_update
= 0;
2404 pthread_mutex_unlock(&consumer_data
.lock
);
2406 /* No FDs and consumer_quit, consumer_cleanup the thread */
2407 if (nb_fd
== 0 && consumer_quit
== 1) {
2410 /* poll on the array of fds */
2412 DBG("polling on %d fd", nb_fd
+ 1);
2413 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2414 DBG("poll num_rdy : %d", num_rdy
);
2415 if (num_rdy
== -1) {
2417 * Restart interrupted system call.
2419 if (errno
== EINTR
) {
2422 PERROR("Poll error");
2423 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2425 } else if (num_rdy
== 0) {
2426 DBG("Polling thread timed out");
2431 * If the consumer_data_pipe triggered poll go directly to the
2432 * beginning of the loop to update the array. We want to prioritize
2433 * array update over low-priority reads.
2435 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2436 ssize_t pipe_readlen
;
2438 DBG("consumer_data_pipe wake up");
2439 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2440 &new_stream
, sizeof(new_stream
));
2441 if (pipe_readlen
< 0) {
2442 ERR("Consumer data pipe ret %ld", pipe_readlen
);
2443 /* Continue so we can at least handle the current stream(s). */
2448 * If the stream is NULL, just ignore it. It's also possible that
2449 * the sessiond poll thread changed the consumer_quit state and is
2450 * waking us up to test it.
2452 if (new_stream
== NULL
) {
2453 validate_endpoint_status_data_stream();
2457 ret
= add_stream(new_stream
, data_ht
);
2459 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
2462 * At this point, if the add_stream fails, it is not in the
2463 * hash table thus passing the NULL value here.
2465 consumer_del_stream(new_stream
, NULL
);
2468 /* Continue to update the local streams and handle prio ones */
2472 /* Take care of high priority channels first. */
2473 for (i
= 0; i
< nb_fd
; i
++) {
2474 if (local_stream
[i
] == NULL
) {
2477 if (pollfd
[i
].revents
& POLLPRI
) {
2478 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2480 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2481 /* it's ok to have an unavailable sub-buffer */
2482 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2483 /* Clean the stream and free it. */
2484 consumer_del_stream(local_stream
[i
], data_ht
);
2485 local_stream
[i
] = NULL
;
2486 } else if (len
> 0) {
2487 local_stream
[i
]->data_read
= 1;
2493 * If we read high prio channel in this loop, try again
2494 * for more high prio data.
2500 /* Take care of low priority channels. */
2501 for (i
= 0; i
< nb_fd
; i
++) {
2502 if (local_stream
[i
] == NULL
) {
2505 if ((pollfd
[i
].revents
& POLLIN
) ||
2506 local_stream
[i
]->hangup_flush_done
) {
2507 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2508 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2509 /* it's ok to have an unavailable sub-buffer */
2510 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2511 /* Clean the stream and free it. */
2512 consumer_del_stream(local_stream
[i
], data_ht
);
2513 local_stream
[i
] = NULL
;
2514 } else if (len
> 0) {
2515 local_stream
[i
]->data_read
= 1;
2520 /* Handle hangup and errors */
2521 for (i
= 0; i
< nb_fd
; i
++) {
2522 if (local_stream
[i
] == NULL
) {
2525 if (!local_stream
[i
]->hangup_flush_done
2526 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2527 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2528 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2529 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2531 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2532 /* Attempt read again, for the data we just flushed. */
2533 local_stream
[i
]->data_read
= 1;
2536 * If the poll flag is HUP/ERR/NVAL and we have
2537 * read no data in this pass, we can remove the
2538 * stream from its hash table.
2540 if ((pollfd
[i
].revents
& POLLHUP
)) {
2541 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2542 if (!local_stream
[i
]->data_read
) {
2543 consumer_del_stream(local_stream
[i
], data_ht
);
2544 local_stream
[i
] = NULL
;
2547 } else if (pollfd
[i
].revents
& POLLERR
) {
2548 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2549 if (!local_stream
[i
]->data_read
) {
2550 consumer_del_stream(local_stream
[i
], data_ht
);
2551 local_stream
[i
] = NULL
;
2554 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2555 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2556 if (!local_stream
[i
]->data_read
) {
2557 consumer_del_stream(local_stream
[i
], data_ht
);
2558 local_stream
[i
] = NULL
;
2562 if (local_stream
[i
] != NULL
) {
2563 local_stream
[i
]->data_read
= 0;
2568 DBG("polling thread exiting");
2573 * Close the write side of the pipe so epoll_wait() in
2574 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2575 * read side of the pipe. If we close them both, epoll_wait strangely does
2576 * not return and could create a endless wait period if the pipe is the
2577 * only tracked fd in the poll set. The thread will take care of closing
2580 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2582 destroy_data_stream_ht(data_ht
);
2584 rcu_unregister_thread();
2589 * Close wake-up end of each stream belonging to the channel. This will
2590 * allow the poll() on the stream read-side to detect when the
2591 * write-side (application) finally closes them.
2594 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2596 struct lttng_ht
*ht
;
2597 struct lttng_consumer_stream
*stream
;
2598 struct lttng_ht_iter iter
;
2600 ht
= consumer_data
.stream_per_chan_id_ht
;
2603 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2604 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2605 ht
->match_fct
, &channel
->key
,
2606 &iter
.iter
, stream
, node_channel_id
.node
) {
2608 * Protect against teardown with mutex.
2610 pthread_mutex_lock(&stream
->lock
);
2611 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2614 switch (consumer_data
.type
) {
2615 case LTTNG_CONSUMER_KERNEL
:
2617 case LTTNG_CONSUMER32_UST
:
2618 case LTTNG_CONSUMER64_UST
:
2620 * Note: a mutex is taken internally within
2621 * liblttng-ust-ctl to protect timer wakeup_fd
2622 * use from concurrent close.
2624 lttng_ustconsumer_close_stream_wakeup(stream
);
2627 ERR("Unknown consumer_data type");
2631 pthread_mutex_unlock(&stream
->lock
);
2636 static void destroy_channel_ht(struct lttng_ht
*ht
)
2638 struct lttng_ht_iter iter
;
2639 struct lttng_consumer_channel
*channel
;
2647 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2648 ret
= lttng_ht_del(ht
, &iter
);
2653 lttng_ht_destroy(ht
);
2657 * This thread polls the channel fds to detect when they are being
2658 * closed. It closes all related streams if the channel is detected as
2659 * closed. It is currently only used as a shim layer for UST because the
2660 * consumerd needs to keep the per-stream wakeup end of pipes open for
2663 void *consumer_thread_channel_poll(void *data
)
2666 uint32_t revents
, nb_fd
;
2667 struct lttng_consumer_channel
*chan
= NULL
;
2668 struct lttng_ht_iter iter
;
2669 struct lttng_ht_node_u64
*node
;
2670 struct lttng_poll_event events
;
2671 struct lttng_consumer_local_data
*ctx
= data
;
2672 struct lttng_ht
*channel_ht
;
2674 rcu_register_thread();
2676 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2678 /* ENOMEM at this point. Better to bail out. */
2682 DBG("Thread channel poll started");
2684 /* Size is set to 1 for the consumer_channel pipe */
2685 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2687 ERR("Poll set creation failed");
2691 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2697 DBG("Channel main loop started");
2700 /* Only the channel pipe is set */
2701 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2706 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2707 ret
= lttng_poll_wait(&events
, -1);
2708 DBG("Channel event catched in thread");
2710 if (errno
== EINTR
) {
2711 ERR("Poll EINTR catched");
2719 /* From here, the event is a channel wait fd */
2720 for (i
= 0; i
< nb_fd
; i
++) {
2721 revents
= LTTNG_POLL_GETEV(&events
, i
);
2722 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2724 /* Just don't waste time if no returned events for the fd */
2728 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2729 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2730 DBG("Channel thread pipe hung up");
2732 * Remove the pipe from the poll set and continue the loop
2733 * since their might be data to consume.
2735 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2737 } else if (revents
& LPOLLIN
) {
2738 enum consumer_channel_action action
;
2741 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2743 ERR("Error reading channel pipe");
2748 case CONSUMER_CHANNEL_ADD
:
2749 DBG("Adding channel %d to poll set",
2752 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2755 lttng_ht_add_unique_u64(channel_ht
,
2756 &chan
->wait_fd_node
);
2758 /* Add channel to the global poll events list */
2759 lttng_poll_add(&events
, chan
->wait_fd
,
2760 LPOLLIN
| LPOLLPRI
);
2762 case CONSUMER_CHANNEL_DEL
:
2764 struct lttng_consumer_stream
*stream
, *stmp
;
2767 chan
= consumer_find_channel(key
);
2770 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2773 lttng_poll_del(&events
, chan
->wait_fd
);
2774 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2775 ret
= lttng_ht_del(channel_ht
, &iter
);
2777 consumer_close_channel_streams(chan
);
2779 switch (consumer_data
.type
) {
2780 case LTTNG_CONSUMER_KERNEL
:
2782 case LTTNG_CONSUMER32_UST
:
2783 case LTTNG_CONSUMER64_UST
:
2784 /* Delete streams that might have been left in the stream list. */
2785 cds_list_for_each_entry_safe(stream
, stmp
, &chan
->streams
.head
,
2787 cds_list_del(&stream
->send_node
);
2788 lttng_ustconsumer_del_stream(stream
);
2789 uatomic_sub(&stream
->chan
->refcount
, 1);
2790 assert(&chan
->refcount
);
2795 ERR("Unknown consumer_data type");
2800 * Release our own refcount. Force channel deletion even if
2801 * streams were not initialized.
2803 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2804 consumer_del_channel(chan
);
2809 case CONSUMER_CHANNEL_QUIT
:
2811 * Remove the pipe from the poll set and continue the loop
2812 * since their might be data to consume.
2814 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2817 ERR("Unknown action");
2822 /* Handle other stream */
2828 uint64_t tmp_id
= (uint64_t) pollfd
;
2830 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2832 node
= lttng_ht_iter_get_node_u64(&iter
);
2835 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2838 /* Check for error event */
2839 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2840 DBG("Channel fd %d is hup|err.", pollfd
);
2842 lttng_poll_del(&events
, chan
->wait_fd
);
2843 ret
= lttng_ht_del(channel_ht
, &iter
);
2845 assert(cds_list_empty(&chan
->streams
.head
));
2846 consumer_close_channel_streams(chan
);
2848 /* Release our own refcount */
2849 if (!uatomic_sub_return(&chan
->refcount
, 1)
2850 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2851 consumer_del_channel(chan
);
2855 /* Release RCU lock for the channel looked up */
2861 lttng_poll_clean(&events
);
2863 destroy_channel_ht(channel_ht
);
2865 DBG("Channel poll thread exiting");
2866 rcu_unregister_thread();
2870 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2871 struct pollfd
*sockpoll
, int client_socket
)
2878 if (lttng_consumer_poll_socket(sockpoll
) < 0) {
2882 DBG("Metadata connection on client_socket");
2884 /* Blocking call, waiting for transmission */
2885 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2886 if (ctx
->consumer_metadata_socket
< 0) {
2887 WARN("On accept metadata");
2898 * This thread listens on the consumerd socket and receives the file
2899 * descriptors from the session daemon.
2901 void *consumer_thread_sessiond_poll(void *data
)
2903 int sock
= -1, client_socket
, ret
;
2905 * structure to poll for incoming data on communication socket avoids
2906 * making blocking sockets.
2908 struct pollfd consumer_sockpoll
[2];
2909 struct lttng_consumer_local_data
*ctx
= data
;
2911 rcu_register_thread();
2913 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2914 unlink(ctx
->consumer_command_sock_path
);
2915 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2916 if (client_socket
< 0) {
2917 ERR("Cannot create command socket");
2921 ret
= lttcomm_listen_unix_sock(client_socket
);
2926 DBG("Sending ready command to lttng-sessiond");
2927 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2928 /* return < 0 on error, but == 0 is not fatal */
2930 ERR("Error sending ready command to lttng-sessiond");
2934 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2935 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2936 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2937 consumer_sockpoll
[1].fd
= client_socket
;
2938 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2940 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2943 DBG("Connection on client_socket");
2945 /* Blocking call, waiting for transmission */
2946 sock
= lttcomm_accept_unix_sock(client_socket
);
2953 * Setup metadata socket which is the second socket connection on the
2954 * command unix socket.
2956 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
2961 /* This socket is not useful anymore. */
2962 ret
= close(client_socket
);
2964 PERROR("close client_socket");
2968 /* update the polling structure to poll on the established socket */
2969 consumer_sockpoll
[1].fd
= sock
;
2970 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2973 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2976 DBG("Incoming command on sock");
2977 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2978 if (ret
== -ENOENT
) {
2979 DBG("Received STOP command");
2984 * This could simply be a session daemon quitting. Don't output
2987 DBG("Communication interrupted on command socket");
2990 if (consumer_quit
) {
2991 DBG("consumer_thread_receive_fds received quit from signal");
2994 DBG("received command on sock");
2997 DBG("Consumer thread sessiond poll exiting");
3000 * Close metadata streams since the producer is the session daemon which
3003 * NOTE: for now, this only applies to the UST tracer.
3005 lttng_consumer_close_metadata();
3008 * when all fds have hung up, the polling thread
3014 * Notify the data poll thread to poll back again and test the
3015 * consumer_quit state that we just set so to quit gracefully.
3017 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3019 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3021 /* Cleaning up possibly open sockets. */
3025 PERROR("close sock sessiond poll");
3028 if (client_socket
>= 0) {
3029 ret
= close(client_socket
);
3031 PERROR("close client_socket sessiond poll");
3035 rcu_unregister_thread();
3039 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3040 struct lttng_consumer_local_data
*ctx
)
3044 pthread_mutex_lock(&stream
->lock
);
3046 switch (consumer_data
.type
) {
3047 case LTTNG_CONSUMER_KERNEL
:
3048 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3050 case LTTNG_CONSUMER32_UST
:
3051 case LTTNG_CONSUMER64_UST
:
3052 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3055 ERR("Unknown consumer_data type");
3061 pthread_mutex_unlock(&stream
->lock
);
3065 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3067 switch (consumer_data
.type
) {
3068 case LTTNG_CONSUMER_KERNEL
:
3069 return lttng_kconsumer_on_recv_stream(stream
);
3070 case LTTNG_CONSUMER32_UST
:
3071 case LTTNG_CONSUMER64_UST
:
3072 return lttng_ustconsumer_on_recv_stream(stream
);
3074 ERR("Unknown consumer_data type");
3081 * Allocate and set consumer data hash tables.
3083 void lttng_consumer_init(void)
3085 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3086 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3087 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3088 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3092 * Process the ADD_RELAYD command receive by a consumer.
3094 * This will create a relayd socket pair and add it to the relayd hash table.
3095 * The caller MUST acquire a RCU read side lock before calling it.
3097 int consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3098 struct lttng_consumer_local_data
*ctx
, int sock
,
3099 struct pollfd
*consumer_sockpoll
,
3100 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
)
3102 int fd
= -1, ret
= -1, relayd_created
= 0;
3103 enum lttng_error_code ret_code
= LTTNG_OK
;
3104 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3107 assert(relayd_sock
);
3109 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3111 /* Get relayd reference if exists. */
3112 relayd
= consumer_find_relayd(net_seq_idx
);
3113 if (relayd
== NULL
) {
3114 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3115 /* Not found. Allocate one. */
3116 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3117 if (relayd
== NULL
) {
3119 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3122 relayd
->sessiond_session_id
= sessiond_id
;
3127 * This code path MUST continue to the consumer send status message to
3128 * we can notify the session daemon and continue our work without
3129 * killing everything.
3133 * relayd key should never be found for control socket.
3135 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3138 /* First send a status message before receiving the fds. */
3139 ret
= consumer_send_status_msg(sock
, LTTNG_OK
);
3141 /* Somehow, the session daemon is not responding anymore. */
3142 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3143 goto error_nosignal
;
3146 /* Poll on consumer socket. */
3147 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
3148 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3150 goto error_nosignal
;
3153 /* Get relayd socket from session daemon */
3154 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3155 if (ret
!= sizeof(fd
)) {
3157 fd
= -1; /* Just in case it gets set with an invalid value. */
3160 * Failing to receive FDs might indicate a major problem such as
3161 * reaching a fd limit during the receive where the kernel returns a
3162 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3163 * don't take any chances and stop everything.
3165 * XXX: Feature request #558 will fix that and avoid this possible
3166 * issue when reaching the fd limit.
3168 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3169 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3173 /* Copy socket information and received FD */
3174 switch (sock_type
) {
3175 case LTTNG_STREAM_CONTROL
:
3176 /* Copy received lttcomm socket */
3177 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3178 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3179 /* Handle create_sock error. */
3181 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3185 * Close the socket created internally by
3186 * lttcomm_create_sock, so we can replace it by the one
3187 * received from sessiond.
3189 if (close(relayd
->control_sock
.sock
.fd
)) {
3193 /* Assign new file descriptor */
3194 relayd
->control_sock
.sock
.fd
= fd
;
3195 fd
= -1; /* For error path */
3196 /* Assign version values. */
3197 relayd
->control_sock
.major
= relayd_sock
->major
;
3198 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3201 * Create a session on the relayd and store the returned id. Lock the
3202 * control socket mutex if the relayd was NOT created before.
3204 if (!relayd_created
) {
3205 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3207 ret
= relayd_create_session(&relayd
->control_sock
,
3208 &relayd
->relayd_session_id
);
3209 if (!relayd_created
) {
3210 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3214 * Close all sockets of a relayd object. It will be freed if it was
3215 * created at the error code path or else it will be garbage
3218 (void) relayd_close(&relayd
->control_sock
);
3219 (void) relayd_close(&relayd
->data_sock
);
3220 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
3225 case LTTNG_STREAM_DATA
:
3226 /* Copy received lttcomm socket */
3227 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3228 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3229 /* Handle create_sock error. */
3231 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3235 * Close the socket created internally by
3236 * lttcomm_create_sock, so we can replace it by the one
3237 * received from sessiond.
3239 if (close(relayd
->data_sock
.sock
.fd
)) {
3243 /* Assign new file descriptor */
3244 relayd
->data_sock
.sock
.fd
= fd
;
3245 fd
= -1; /* for eventual error paths */
3246 /* Assign version values. */
3247 relayd
->data_sock
.major
= relayd_sock
->major
;
3248 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3251 ERR("Unknown relayd socket type (%d)", sock_type
);
3253 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3257 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3258 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3259 relayd
->net_seq_idx
, fd
);
3261 /* We successfully added the socket. Send status back. */
3262 ret
= consumer_send_status_msg(sock
, ret_code
);
3264 /* Somehow, the session daemon is not responding anymore. */
3265 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3266 goto error_nosignal
;
3270 * Add relayd socket pair to consumer data hashtable. If object already
3271 * exists or on error, the function gracefully returns.
3279 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3280 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3284 /* Close received socket if valid. */
3287 PERROR("close received socket");
3291 if (relayd_created
) {
3299 * Try to lock the stream mutex.
3301 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3303 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3310 * Try to lock the stream mutex. On failure, we know that the stream is
3311 * being used else where hence there is data still being extracted.
3313 ret
= pthread_mutex_trylock(&stream
->lock
);
3315 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3327 * Search for a relayd associated to the session id and return the reference.
3329 * A rcu read side lock MUST be acquire before calling this function and locked
3330 * until the relayd object is no longer necessary.
3332 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3334 struct lttng_ht_iter iter
;
3335 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3337 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3338 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3341 * Check by sessiond id which is unique here where the relayd session
3342 * id might not be when having multiple relayd.
3344 if (relayd
->sessiond_session_id
== id
) {
3345 /* Found the relayd. There can be only one per id. */
3357 * Check if for a given session id there is still data needed to be extract
3360 * Return 1 if data is pending or else 0 meaning ready to be read.
3362 int consumer_data_pending(uint64_t id
)
3365 struct lttng_ht_iter iter
;
3366 struct lttng_ht
*ht
;
3367 struct lttng_consumer_stream
*stream
;
3368 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3369 int (*data_pending
)(struct lttng_consumer_stream
*);
3371 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3374 pthread_mutex_lock(&consumer_data
.lock
);
3376 switch (consumer_data
.type
) {
3377 case LTTNG_CONSUMER_KERNEL
:
3378 data_pending
= lttng_kconsumer_data_pending
;
3380 case LTTNG_CONSUMER32_UST
:
3381 case LTTNG_CONSUMER64_UST
:
3382 data_pending
= lttng_ustconsumer_data_pending
;
3385 ERR("Unknown consumer data type");
3389 /* Ease our life a bit */
3390 ht
= consumer_data
.stream_list_ht
;
3392 relayd
= find_relayd_by_session_id(id
);
3394 /* Send init command for data pending. */
3395 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3396 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3397 relayd
->relayd_session_id
);
3398 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3400 /* Communication error thus the relayd so no data pending. */
3401 goto data_not_pending
;
3405 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3406 ht
->hash_fct(&id
, lttng_ht_seed
),
3408 &iter
.iter
, stream
, node_session_id
.node
) {
3409 /* If this call fails, the stream is being used hence data pending. */
3410 ret
= stream_try_lock(stream
);
3416 * A removed node from the hash table indicates that the stream has
3417 * been deleted thus having a guarantee that the buffers are closed
3418 * on the consumer side. However, data can still be transmitted
3419 * over the network so don't skip the relayd check.
3421 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3423 /* Check the stream if there is data in the buffers. */
3424 ret
= data_pending(stream
);
3426 pthread_mutex_unlock(&stream
->lock
);
3433 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3434 if (stream
->metadata_flag
) {
3435 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3436 stream
->relayd_stream_id
);
3438 ret
= relayd_data_pending(&relayd
->control_sock
,
3439 stream
->relayd_stream_id
,
3440 stream
->next_net_seq_num
- 1);
3442 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3444 pthread_mutex_unlock(&stream
->lock
);
3448 pthread_mutex_unlock(&stream
->lock
);
3452 unsigned int is_data_inflight
= 0;
3454 /* Send init command for data pending. */
3455 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3456 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3457 relayd
->relayd_session_id
, &is_data_inflight
);
3458 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3460 goto data_not_pending
;
3462 if (is_data_inflight
) {
3468 * Finding _no_ node in the hash table and no inflight data means that the
3469 * stream(s) have been removed thus data is guaranteed to be available for
3470 * analysis from the trace files.
3474 /* Data is available to be read by a viewer. */
3475 pthread_mutex_unlock(&consumer_data
.lock
);
3480 /* Data is still being extracted from buffers. */
3481 pthread_mutex_unlock(&consumer_data
.lock
);
3487 * Send a ret code status message to the sessiond daemon.
3489 * Return the sendmsg() return value.
3491 int consumer_send_status_msg(int sock
, int ret_code
)
3493 struct lttcomm_consumer_status_msg msg
;
3495 msg
.ret_code
= ret_code
;
3497 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3501 * Send a channel status message to the sessiond daemon.
3503 * Return the sendmsg() return value.
3505 int consumer_send_status_channel(int sock
,
3506 struct lttng_consumer_channel
*channel
)
3508 struct lttcomm_consumer_status_channel msg
;
3513 msg
.ret_code
= -LTTNG_ERR_UST_CHAN_FAIL
;
3515 msg
.ret_code
= LTTNG_OK
;
3516 msg
.key
= channel
->key
;
3517 msg
.stream_count
= channel
->streams
.count
;
3520 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));