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 <bin/lttng-consumerd/health-consumerd.h>
34 #include <common/common.h>
35 #include <common/utils.h>
36 #include <common/compat/poll.h>
37 #include <common/compat/endian.h>
38 #include <common/index/index.h>
39 #include <common/kernel-ctl/kernel-ctl.h>
40 #include <common/sessiond-comm/relayd.h>
41 #include <common/sessiond-comm/sessiond-comm.h>
42 #include <common/kernel-consumer/kernel-consumer.h>
43 #include <common/relayd/relayd.h>
44 #include <common/ust-consumer/ust-consumer.h>
45 #include <common/consumer-timer.h>
48 #include "consumer-stream.h"
49 #include "consumer-testpoint.h"
51 struct lttng_consumer_global_data consumer_data
= {
54 .type
= LTTNG_CONSUMER_UNKNOWN
,
57 enum consumer_channel_action
{
60 CONSUMER_CHANNEL_QUIT
,
63 struct consumer_channel_msg
{
64 enum consumer_channel_action action
;
65 struct lttng_consumer_channel
*chan
; /* add */
66 uint64_t key
; /* del */
70 * Flag to inform the polling thread to quit when all fd hung up. Updated by
71 * the consumer_thread_receive_fds when it notices that all fds has hung up.
72 * Also updated by the signal handler (consumer_should_exit()). Read by the
75 volatile int consumer_quit
;
78 * Global hash table containing respectively metadata and data streams. The
79 * stream element in this ht should only be updated by the metadata poll thread
80 * for the metadata and the data poll thread for the data.
82 static struct lttng_ht
*metadata_ht
;
83 static struct lttng_ht
*data_ht
;
86 * Notify a thread lttng pipe to poll back again. This usually means that some
87 * global state has changed so we just send back the thread in a poll wait
90 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
92 struct lttng_consumer_stream
*null_stream
= NULL
;
96 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
99 static void notify_health_quit_pipe(int *pipe
)
103 ret
= lttng_write(pipe
[1], "4", 1);
105 PERROR("write consumer health quit");
109 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
110 struct lttng_consumer_channel
*chan
,
112 enum consumer_channel_action action
)
114 struct consumer_channel_msg msg
;
117 memset(&msg
, 0, sizeof(msg
));
122 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
123 if (ret
< sizeof(msg
)) {
124 PERROR("notify_channel_pipe write error");
128 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
131 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
134 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
135 struct lttng_consumer_channel
**chan
,
137 enum consumer_channel_action
*action
)
139 struct consumer_channel_msg msg
;
142 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
143 if (ret
< sizeof(msg
)) {
147 *action
= msg
.action
;
155 * Cleanup the stream list of a channel. Those streams are not yet globally
158 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
160 struct lttng_consumer_stream
*stream
, *stmp
;
164 /* Delete streams that might have been left in the stream list. */
165 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
167 cds_list_del(&stream
->send_node
);
169 * Once a stream is added to this list, the buffers were created so we
170 * have a guarantee that this call will succeed. Setting the monitor
171 * mode to 0 so we don't lock nor try to delete the stream from the
175 consumer_stream_destroy(stream
, NULL
);
180 * Find a stream. The consumer_data.lock must be locked during this
183 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
186 struct lttng_ht_iter iter
;
187 struct lttng_ht_node_u64
*node
;
188 struct lttng_consumer_stream
*stream
= NULL
;
192 /* -1ULL keys are lookup failures */
193 if (key
== (uint64_t) -1ULL) {
199 lttng_ht_lookup(ht
, &key
, &iter
);
200 node
= lttng_ht_iter_get_node_u64(&iter
);
202 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
210 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
212 struct lttng_consumer_stream
*stream
;
215 stream
= find_stream(key
, ht
);
217 stream
->key
= (uint64_t) -1ULL;
219 * We don't want the lookup to match, but we still need
220 * to iterate on this stream when iterating over the hash table. Just
221 * change the node key.
223 stream
->node
.key
= (uint64_t) -1ULL;
229 * Return a channel object for the given key.
231 * RCU read side lock MUST be acquired before calling this function and
232 * protects the channel ptr.
234 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
236 struct lttng_ht_iter iter
;
237 struct lttng_ht_node_u64
*node
;
238 struct lttng_consumer_channel
*channel
= NULL
;
240 /* -1ULL keys are lookup failures */
241 if (key
== (uint64_t) -1ULL) {
245 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
246 node
= lttng_ht_iter_get_node_u64(&iter
);
248 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
255 * There is a possibility that the consumer does not have enough time between
256 * the close of the channel on the session daemon and the cleanup in here thus
257 * once we have a channel add with an existing key, we know for sure that this
258 * channel will eventually get cleaned up by all streams being closed.
260 * This function just nullifies the already existing channel key.
262 static void steal_channel_key(uint64_t key
)
264 struct lttng_consumer_channel
*channel
;
267 channel
= consumer_find_channel(key
);
269 channel
->key
= (uint64_t) -1ULL;
271 * We don't want the lookup to match, but we still need to iterate on
272 * this channel when iterating over the hash table. Just change the
275 channel
->node
.key
= (uint64_t) -1ULL;
280 static void free_channel_rcu(struct rcu_head
*head
)
282 struct lttng_ht_node_u64
*node
=
283 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
284 struct lttng_consumer_channel
*channel
=
285 caa_container_of(node
, struct lttng_consumer_channel
, node
);
291 * RCU protected relayd socket pair free.
293 static void free_relayd_rcu(struct rcu_head
*head
)
295 struct lttng_ht_node_u64
*node
=
296 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
297 struct consumer_relayd_sock_pair
*relayd
=
298 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
301 * Close all sockets. This is done in the call RCU since we don't want the
302 * socket fds to be reassigned thus potentially creating bad state of the
305 * We do not have to lock the control socket mutex here since at this stage
306 * there is no one referencing to this relayd object.
308 (void) relayd_close(&relayd
->control_sock
);
309 (void) relayd_close(&relayd
->data_sock
);
315 * Destroy and free relayd socket pair object.
317 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
320 struct lttng_ht_iter iter
;
322 if (relayd
== NULL
) {
326 DBG("Consumer destroy and close relayd socket pair");
328 iter
.iter
.node
= &relayd
->node
.node
;
329 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
331 /* We assume the relayd is being or is destroyed */
335 /* RCU free() call */
336 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
340 * Remove a channel from the global list protected by a mutex. This function is
341 * also responsible for freeing its data structures.
343 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
346 struct lttng_ht_iter iter
;
348 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
350 pthread_mutex_lock(&consumer_data
.lock
);
351 pthread_mutex_lock(&channel
->lock
);
353 /* Destroy streams that might have been left in the stream list. */
354 clean_channel_stream_list(channel
);
356 if (channel
->live_timer_enabled
== 1) {
357 consumer_timer_live_stop(channel
);
360 switch (consumer_data
.type
) {
361 case LTTNG_CONSUMER_KERNEL
:
363 case LTTNG_CONSUMER32_UST
:
364 case LTTNG_CONSUMER64_UST
:
365 lttng_ustconsumer_del_channel(channel
);
368 ERR("Unknown consumer_data type");
374 iter
.iter
.node
= &channel
->node
.node
;
375 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
379 call_rcu(&channel
->node
.head
, free_channel_rcu
);
381 pthread_mutex_unlock(&channel
->lock
);
382 pthread_mutex_unlock(&consumer_data
.lock
);
386 * Iterate over the relayd hash table and destroy each element. Finally,
387 * destroy the whole hash table.
389 static void cleanup_relayd_ht(void)
391 struct lttng_ht_iter iter
;
392 struct consumer_relayd_sock_pair
*relayd
;
396 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
398 consumer_destroy_relayd(relayd
);
403 lttng_ht_destroy(consumer_data
.relayd_ht
);
407 * Update the end point status of all streams having the given network sequence
408 * index (relayd index).
410 * It's atomically set without having the stream mutex locked which is fine
411 * because we handle the write/read race with a pipe wakeup for each thread.
413 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
414 enum consumer_endpoint_status status
)
416 struct lttng_ht_iter iter
;
417 struct lttng_consumer_stream
*stream
;
419 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
423 /* Let's begin with metadata */
424 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
425 if (stream
->net_seq_idx
== net_seq_idx
) {
426 uatomic_set(&stream
->endpoint_status
, status
);
427 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
431 /* Follow up by the data streams */
432 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
433 if (stream
->net_seq_idx
== net_seq_idx
) {
434 uatomic_set(&stream
->endpoint_status
, status
);
435 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
442 * Cleanup a relayd object by flagging every associated streams for deletion,
443 * destroying the object meaning removing it from the relayd hash table,
444 * closing the sockets and freeing the memory in a RCU call.
446 * If a local data context is available, notify the threads that the streams'
447 * state have changed.
449 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
450 struct lttng_consumer_local_data
*ctx
)
456 DBG("Cleaning up relayd sockets");
458 /* Save the net sequence index before destroying the object */
459 netidx
= relayd
->net_seq_idx
;
462 * Delete the relayd from the relayd hash table, close the sockets and free
463 * the object in a RCU call.
465 consumer_destroy_relayd(relayd
);
467 /* Set inactive endpoint to all streams */
468 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
471 * With a local data context, notify the threads that the streams' state
472 * have changed. The write() action on the pipe acts as an "implicit"
473 * memory barrier ordering the updates of the end point status from the
474 * read of this status which happens AFTER receiving this notify.
477 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
478 notify_thread_lttng_pipe(ctx
->consumer_metadata_pipe
);
483 * Flag a relayd socket pair for destruction. Destroy it if the refcount
486 * RCU read side lock MUST be aquired before calling this function.
488 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
492 /* Set destroy flag for this object */
493 uatomic_set(&relayd
->destroy_flag
, 1);
495 /* Destroy the relayd if refcount is 0 */
496 if (uatomic_read(&relayd
->refcount
) == 0) {
497 consumer_destroy_relayd(relayd
);
502 * Completly destroy stream from every visiable data structure and the given
505 * One this call returns, the stream object is not longer usable nor visible.
507 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
510 consumer_stream_destroy(stream
, ht
);
514 * XXX naming of del vs destroy is all mixed up.
516 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
518 consumer_stream_destroy(stream
, data_ht
);
521 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
523 consumer_stream_destroy(stream
, metadata_ht
);
526 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
528 enum lttng_consumer_stream_state state
,
529 const char *channel_name
,
536 enum consumer_channel_type type
,
537 unsigned int monitor
)
540 struct lttng_consumer_stream
*stream
;
542 stream
= zmalloc(sizeof(*stream
));
543 if (stream
== NULL
) {
544 PERROR("malloc struct lttng_consumer_stream");
551 stream
->key
= stream_key
;
553 stream
->out_fd_offset
= 0;
554 stream
->output_written
= 0;
555 stream
->state
= state
;
558 stream
->net_seq_idx
= relayd_id
;
559 stream
->session_id
= session_id
;
560 stream
->monitor
= monitor
;
561 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
562 stream
->index_fd
= -1;
563 pthread_mutex_init(&stream
->lock
, NULL
);
565 /* If channel is the metadata, flag this stream as metadata. */
566 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
567 stream
->metadata_flag
= 1;
568 /* Metadata is flat out. */
569 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
570 /* Live rendez-vous point. */
571 pthread_cond_init(&stream
->metadata_rdv
, NULL
);
572 pthread_mutex_init(&stream
->metadata_rdv_lock
, NULL
);
574 /* Format stream name to <channel_name>_<cpu_number> */
575 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
578 PERROR("snprintf stream name");
583 /* Key is always the wait_fd for streams. */
584 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
586 /* Init node per channel id key */
587 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
589 /* Init session id node with the stream session id */
590 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
592 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
593 " relayd_id %" PRIu64
", session_id %" PRIu64
,
594 stream
->name
, stream
->key
, channel_key
,
595 stream
->net_seq_idx
, stream
->session_id
);
611 * Add a stream to the global list protected by a mutex.
613 int consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
615 struct lttng_ht
*ht
= data_ht
;
621 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
623 pthread_mutex_lock(&consumer_data
.lock
);
624 pthread_mutex_lock(&stream
->chan
->lock
);
625 pthread_mutex_lock(&stream
->chan
->timer_lock
);
626 pthread_mutex_lock(&stream
->lock
);
629 /* Steal stream identifier to avoid having streams with the same key */
630 steal_stream_key(stream
->key
, ht
);
632 lttng_ht_add_unique_u64(ht
, &stream
->node
);
634 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
635 &stream
->node_channel_id
);
638 * Add stream to the stream_list_ht of the consumer data. No need to steal
639 * the key since the HT does not use it and we allow to add redundant keys
642 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
645 * When nb_init_stream_left reaches 0, we don't need to trigger any action
646 * in terms of destroying the associated channel, because the action that
647 * causes the count to become 0 also causes a stream to be added. The
648 * channel deletion will thus be triggered by the following removal of this
651 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
652 /* Increment refcount before decrementing nb_init_stream_left */
654 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
657 /* Update consumer data once the node is inserted. */
658 consumer_data
.stream_count
++;
659 consumer_data
.need_update
= 1;
662 pthread_mutex_unlock(&stream
->lock
);
663 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
664 pthread_mutex_unlock(&stream
->chan
->lock
);
665 pthread_mutex_unlock(&consumer_data
.lock
);
670 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
672 consumer_del_stream(stream
, data_ht
);
676 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
677 * be acquired before calling this.
679 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
682 struct lttng_ht_node_u64
*node
;
683 struct lttng_ht_iter iter
;
687 lttng_ht_lookup(consumer_data
.relayd_ht
,
688 &relayd
->net_seq_idx
, &iter
);
689 node
= lttng_ht_iter_get_node_u64(&iter
);
693 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
700 * Allocate and return a consumer relayd socket.
702 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
703 uint64_t net_seq_idx
)
705 struct consumer_relayd_sock_pair
*obj
= NULL
;
707 /* net sequence index of -1 is a failure */
708 if (net_seq_idx
== (uint64_t) -1ULL) {
712 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
714 PERROR("zmalloc relayd sock");
718 obj
->net_seq_idx
= net_seq_idx
;
720 obj
->destroy_flag
= 0;
721 obj
->control_sock
.sock
.fd
= -1;
722 obj
->data_sock
.sock
.fd
= -1;
723 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
724 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
731 * Find a relayd socket pair in the global consumer data.
733 * Return the object if found else NULL.
734 * RCU read-side lock must be held across this call and while using the
737 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
739 struct lttng_ht_iter iter
;
740 struct lttng_ht_node_u64
*node
;
741 struct consumer_relayd_sock_pair
*relayd
= NULL
;
743 /* Negative keys are lookup failures */
744 if (key
== (uint64_t) -1ULL) {
748 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
750 node
= lttng_ht_iter_get_node_u64(&iter
);
752 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
760 * Find a relayd and send the stream
762 * Returns 0 on success, < 0 on error
764 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
768 struct consumer_relayd_sock_pair
*relayd
;
771 assert(stream
->net_seq_idx
!= -1ULL);
774 /* The stream is not metadata. Get relayd reference if exists. */
776 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
777 if (relayd
!= NULL
) {
778 /* Add stream on the relayd */
779 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
780 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
781 path
, &stream
->relayd_stream_id
,
782 stream
->chan
->tracefile_size
, stream
->chan
->tracefile_count
);
783 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
788 uatomic_inc(&relayd
->refcount
);
789 stream
->sent_to_relayd
= 1;
791 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
792 stream
->key
, stream
->net_seq_idx
);
797 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
798 stream
->name
, stream
->key
, stream
->net_seq_idx
);
806 * Find a relayd and send the streams sent message
808 * Returns 0 on success, < 0 on error
810 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
813 struct consumer_relayd_sock_pair
*relayd
;
815 assert(net_seq_idx
!= -1ULL);
817 /* The stream is not metadata. Get relayd reference if exists. */
819 relayd
= consumer_find_relayd(net_seq_idx
);
820 if (relayd
!= NULL
) {
821 /* Add stream on the relayd */
822 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
823 ret
= relayd_streams_sent(&relayd
->control_sock
);
824 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
829 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
836 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
844 * Find a relayd and close the stream
846 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
848 struct consumer_relayd_sock_pair
*relayd
;
850 /* The stream is not metadata. Get relayd reference if exists. */
852 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
854 consumer_stream_relayd_close(stream
, relayd
);
860 * Handle stream for relayd transmission if the stream applies for network
861 * streaming where the net sequence index is set.
863 * Return destination file descriptor or negative value on error.
865 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
866 size_t data_size
, unsigned long padding
,
867 struct consumer_relayd_sock_pair
*relayd
)
870 struct lttcomm_relayd_data_hdr data_hdr
;
876 /* Reset data header */
877 memset(&data_hdr
, 0, sizeof(data_hdr
));
879 if (stream
->metadata_flag
) {
880 /* Caller MUST acquire the relayd control socket lock */
881 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
886 /* Metadata are always sent on the control socket. */
887 outfd
= relayd
->control_sock
.sock
.fd
;
889 /* Set header with stream information */
890 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
891 data_hdr
.data_size
= htobe32(data_size
);
892 data_hdr
.padding_size
= htobe32(padding
);
894 * Note that net_seq_num below is assigned with the *current* value of
895 * next_net_seq_num and only after that the next_net_seq_num will be
896 * increment. This is why when issuing a command on the relayd using
897 * this next value, 1 should always be substracted in order to compare
898 * the last seen sequence number on the relayd side to the last sent.
900 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
901 /* Other fields are zeroed previously */
903 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
909 ++stream
->next_net_seq_num
;
911 /* Set to go on data socket */
912 outfd
= relayd
->data_sock
.sock
.fd
;
920 * Allocate and return a new lttng_consumer_channel object using the given key
921 * to initialize the hash table node.
923 * On error, return NULL.
925 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
927 const char *pathname
,
932 enum lttng_event_output output
,
933 uint64_t tracefile_size
,
934 uint64_t tracefile_count
,
935 uint64_t session_id_per_pid
,
936 unsigned int monitor
,
937 unsigned int live_timer_interval
)
939 struct lttng_consumer_channel
*channel
;
941 channel
= zmalloc(sizeof(*channel
));
942 if (channel
== NULL
) {
943 PERROR("malloc struct lttng_consumer_channel");
948 channel
->refcount
= 0;
949 channel
->session_id
= session_id
;
950 channel
->session_id_per_pid
= session_id_per_pid
;
953 channel
->relayd_id
= relayd_id
;
954 channel
->tracefile_size
= tracefile_size
;
955 channel
->tracefile_count
= tracefile_count
;
956 channel
->monitor
= monitor
;
957 channel
->live_timer_interval
= live_timer_interval
;
958 pthread_mutex_init(&channel
->lock
, NULL
);
959 pthread_mutex_init(&channel
->timer_lock
, NULL
);
962 case LTTNG_EVENT_SPLICE
:
963 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
965 case LTTNG_EVENT_MMAP
:
966 channel
->output
= CONSUMER_CHANNEL_MMAP
;
976 * In monitor mode, the streams associated with the channel will be put in
977 * a special list ONLY owned by this channel. So, the refcount is set to 1
978 * here meaning that the channel itself has streams that are referenced.
980 * On a channel deletion, once the channel is no longer visible, the
981 * refcount is decremented and checked for a zero value to delete it. With
982 * streams in no monitor mode, it will now be safe to destroy the channel.
984 if (!channel
->monitor
) {
985 channel
->refcount
= 1;
988 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
989 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
991 strncpy(channel
->name
, name
, sizeof(channel
->name
));
992 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
994 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
996 channel
->wait_fd
= -1;
998 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1000 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
1007 * Add a channel to the global list protected by a mutex.
1009 * Always return 0 indicating success.
1011 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1012 struct lttng_consumer_local_data
*ctx
)
1014 pthread_mutex_lock(&consumer_data
.lock
);
1015 pthread_mutex_lock(&channel
->lock
);
1016 pthread_mutex_lock(&channel
->timer_lock
);
1019 * This gives us a guarantee that the channel we are about to add to the
1020 * channel hash table will be unique. See this function comment on the why
1021 * we need to steel the channel key at this stage.
1023 steal_channel_key(channel
->key
);
1026 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1029 pthread_mutex_unlock(&channel
->timer_lock
);
1030 pthread_mutex_unlock(&channel
->lock
);
1031 pthread_mutex_unlock(&consumer_data
.lock
);
1033 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1034 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1041 * Allocate the pollfd structure and the local view of the out fds to avoid
1042 * doing a lookup in the linked list and concurrency issues when writing is
1043 * needed. Called with consumer_data.lock held.
1045 * Returns the number of fds in the structures.
1047 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1048 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1049 struct lttng_ht
*ht
)
1052 struct lttng_ht_iter iter
;
1053 struct lttng_consumer_stream
*stream
;
1058 assert(local_stream
);
1060 DBG("Updating poll fd array");
1062 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1064 * Only active streams with an active end point can be added to the
1065 * poll set and local stream storage of the thread.
1067 * There is a potential race here for endpoint_status to be updated
1068 * just after the check. However, this is OK since the stream(s) will
1069 * be deleted once the thread is notified that the end point state has
1070 * changed where this function will be called back again.
1072 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
1073 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1077 * This clobbers way too much the debug output. Uncomment that if you
1078 * need it for debugging purposes.
1080 * DBG("Active FD %d", stream->wait_fd);
1082 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1083 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1084 local_stream
[i
] = stream
;
1090 * Insert the consumer_data_pipe at the end of the array and don't
1091 * increment i so nb_fd is the number of real FD.
1093 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1094 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1096 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1097 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1102 * Poll on the should_quit pipe and the command socket return -1 on
1103 * error, 1 if should exit, 0 if data is available on the command socket
1105 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1110 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1111 if (num_rdy
== -1) {
1113 * Restart interrupted system call.
1115 if (errno
== EINTR
) {
1118 PERROR("Poll error");
1121 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1122 DBG("consumer_should_quit wake up");
1129 * Set the error socket.
1131 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1134 ctx
->consumer_error_socket
= sock
;
1138 * Set the command socket path.
1140 void lttng_consumer_set_command_sock_path(
1141 struct lttng_consumer_local_data
*ctx
, char *sock
)
1143 ctx
->consumer_command_sock_path
= sock
;
1147 * Send return code to the session daemon.
1148 * If the socket is not defined, we return 0, it is not a fatal error
1150 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1152 if (ctx
->consumer_error_socket
> 0) {
1153 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1154 sizeof(enum lttcomm_sessiond_command
));
1161 * Close all the tracefiles and stream fds and MUST be called when all
1162 * instances are destroyed i.e. when all threads were joined and are ended.
1164 void lttng_consumer_cleanup(void)
1166 struct lttng_ht_iter iter
;
1167 struct lttng_consumer_channel
*channel
;
1171 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1173 consumer_del_channel(channel
);
1178 lttng_ht_destroy(consumer_data
.channel_ht
);
1180 cleanup_relayd_ht();
1182 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1185 * This HT contains streams that are freed by either the metadata thread or
1186 * the data thread so we do *nothing* on the hash table and simply destroy
1189 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1193 * Called from signal handler.
1195 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1200 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1202 PERROR("write consumer quit");
1205 DBG("Consumer flag that it should quit");
1208 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1211 int outfd
= stream
->out_fd
;
1214 * This does a blocking write-and-wait on any page that belongs to the
1215 * subbuffer prior to the one we just wrote.
1216 * Don't care about error values, as these are just hints and ways to
1217 * limit the amount of page cache used.
1219 if (orig_offset
< stream
->max_sb_size
) {
1222 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1223 stream
->max_sb_size
,
1224 SYNC_FILE_RANGE_WAIT_BEFORE
1225 | SYNC_FILE_RANGE_WRITE
1226 | SYNC_FILE_RANGE_WAIT_AFTER
);
1228 * Give hints to the kernel about how we access the file:
1229 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1232 * We need to call fadvise again after the file grows because the
1233 * kernel does not seem to apply fadvise to non-existing parts of the
1236 * Call fadvise _after_ having waited for the page writeback to
1237 * complete because the dirty page writeback semantic is not well
1238 * defined. So it can be expected to lead to lower throughput in
1241 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1242 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1246 * Initialise the necessary environnement :
1247 * - create a new context
1248 * - create the poll_pipe
1249 * - create the should_quit pipe (for signal handler)
1250 * - create the thread pipe (for splice)
1252 * Takes a function pointer as argument, this function is called when data is
1253 * available on a buffer. This function is responsible to do the
1254 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1255 * buffer configuration and then kernctl_put_next_subbuf at the end.
1257 * Returns a pointer to the new context or NULL on error.
1259 struct lttng_consumer_local_data
*lttng_consumer_create(
1260 enum lttng_consumer_type type
,
1261 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1262 struct lttng_consumer_local_data
*ctx
),
1263 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1264 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1265 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1268 struct lttng_consumer_local_data
*ctx
;
1270 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1271 consumer_data
.type
== type
);
1272 consumer_data
.type
= type
;
1274 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1276 PERROR("allocating context");
1280 ctx
->consumer_error_socket
= -1;
1281 ctx
->consumer_metadata_socket
= -1;
1282 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1283 /* assign the callbacks */
1284 ctx
->on_buffer_ready
= buffer_ready
;
1285 ctx
->on_recv_channel
= recv_channel
;
1286 ctx
->on_recv_stream
= recv_stream
;
1287 ctx
->on_update_stream
= update_stream
;
1289 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1290 if (!ctx
->consumer_data_pipe
) {
1291 goto error_poll_pipe
;
1294 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1295 if (!ctx
->consumer_wakeup_pipe
) {
1296 goto error_wakeup_pipe
;
1299 ret
= pipe(ctx
->consumer_should_quit
);
1301 PERROR("Error creating recv pipe");
1302 goto error_quit_pipe
;
1305 ret
= pipe(ctx
->consumer_thread_pipe
);
1307 PERROR("Error creating thread pipe");
1308 goto error_thread_pipe
;
1311 ret
= pipe(ctx
->consumer_channel_pipe
);
1313 PERROR("Error creating channel pipe");
1314 goto error_channel_pipe
;
1317 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1318 if (!ctx
->consumer_metadata_pipe
) {
1319 goto error_metadata_pipe
;
1322 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1324 goto error_splice_pipe
;
1330 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1331 error_metadata_pipe
:
1332 utils_close_pipe(ctx
->consumer_channel_pipe
);
1334 utils_close_pipe(ctx
->consumer_thread_pipe
);
1336 utils_close_pipe(ctx
->consumer_should_quit
);
1338 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1340 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1348 * Iterate over all streams of the hashtable and free them properly.
1350 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1352 struct lttng_ht_iter iter
;
1353 struct lttng_consumer_stream
*stream
;
1360 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1362 * Ignore return value since we are currently cleaning up so any error
1365 (void) consumer_del_stream(stream
, ht
);
1369 lttng_ht_destroy(ht
);
1373 * Iterate over all streams of the metadata hashtable and free them
1376 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1378 struct lttng_ht_iter iter
;
1379 struct lttng_consumer_stream
*stream
;
1386 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1388 * Ignore return value since we are currently cleaning up so any error
1391 (void) consumer_del_metadata_stream(stream
, ht
);
1395 lttng_ht_destroy(ht
);
1399 * Close all fds associated with the instance and free the context.
1401 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1405 DBG("Consumer destroying it. Closing everything.");
1407 destroy_data_stream_ht(data_ht
);
1408 destroy_metadata_stream_ht(metadata_ht
);
1410 ret
= close(ctx
->consumer_error_socket
);
1414 ret
= close(ctx
->consumer_metadata_socket
);
1418 utils_close_pipe(ctx
->consumer_thread_pipe
);
1419 utils_close_pipe(ctx
->consumer_channel_pipe
);
1420 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1421 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1422 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1423 utils_close_pipe(ctx
->consumer_should_quit
);
1424 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1426 unlink(ctx
->consumer_command_sock_path
);
1431 * Write the metadata stream id on the specified file descriptor.
1433 static int write_relayd_metadata_id(int fd
,
1434 struct lttng_consumer_stream
*stream
,
1435 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1438 struct lttcomm_relayd_metadata_payload hdr
;
1440 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1441 hdr
.padding_size
= htobe32(padding
);
1442 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1443 if (ret
< sizeof(hdr
)) {
1445 * This error means that the fd's end is closed so ignore the perror
1446 * not to clubber the error output since this can happen in a normal
1449 if (errno
!= EPIPE
) {
1450 PERROR("write metadata stream id");
1452 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1454 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1455 * handle writting the missing part so report that as an error and
1456 * don't lie to the caller.
1461 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1462 stream
->relayd_stream_id
, padding
);
1469 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1470 * core function for writing trace buffers to either the local filesystem or
1473 * It must be called with the stream lock held.
1475 * Careful review MUST be put if any changes occur!
1477 * Returns the number of bytes written
1479 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1480 struct lttng_consumer_local_data
*ctx
,
1481 struct lttng_consumer_stream
*stream
, unsigned long len
,
1482 unsigned long padding
,
1483 struct ctf_packet_index
*index
)
1485 unsigned long mmap_offset
;
1488 off_t orig_offset
= stream
->out_fd_offset
;
1489 /* Default is on the disk */
1490 int outfd
= stream
->out_fd
;
1491 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1492 unsigned int relayd_hang_up
= 0;
1494 /* RCU lock for the relayd pointer */
1497 /* Flag that the current stream if set for network streaming. */
1498 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1499 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1500 if (relayd
== NULL
) {
1506 /* get the offset inside the fd to mmap */
1507 switch (consumer_data
.type
) {
1508 case LTTNG_CONSUMER_KERNEL
:
1509 mmap_base
= stream
->mmap_base
;
1510 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1513 PERROR("tracer ctl get_mmap_read_offset");
1517 case LTTNG_CONSUMER32_UST
:
1518 case LTTNG_CONSUMER64_UST
:
1519 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1521 ERR("read mmap get mmap base for stream %s", stream
->name
);
1525 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1527 PERROR("tracer ctl get_mmap_read_offset");
1533 ERR("Unknown consumer_data type");
1537 /* Handle stream on the relayd if the output is on the network */
1539 unsigned long netlen
= len
;
1542 * Lock the control socket for the complete duration of the function
1543 * since from this point on we will use the socket.
1545 if (stream
->metadata_flag
) {
1546 /* Metadata requires the control socket. */
1547 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1548 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1551 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1556 /* Use the returned socket. */
1559 /* Write metadata stream id before payload */
1560 if (stream
->metadata_flag
) {
1561 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1568 /* No streaming, we have to set the len with the full padding */
1572 * Check if we need to change the tracefile before writing the packet.
1574 if (stream
->chan
->tracefile_size
> 0 &&
1575 (stream
->tracefile_size_current
+ len
) >
1576 stream
->chan
->tracefile_size
) {
1577 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1578 stream
->name
, stream
->chan
->tracefile_size
,
1579 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1580 stream
->out_fd
, &(stream
->tracefile_count_current
),
1583 ERR("Rotating output file");
1586 outfd
= stream
->out_fd
;
1588 if (stream
->index_fd
>= 0) {
1589 ret
= index_create_file(stream
->chan
->pathname
,
1590 stream
->name
, stream
->uid
, stream
->gid
,
1591 stream
->chan
->tracefile_size
,
1592 stream
->tracefile_count_current
);
1596 stream
->index_fd
= ret
;
1599 /* Reset current size because we just perform a rotation. */
1600 stream
->tracefile_size_current
= 0;
1601 stream
->out_fd_offset
= 0;
1604 stream
->tracefile_size_current
+= len
;
1606 index
->offset
= htobe64(stream
->out_fd_offset
);
1611 * This call guarantee that len or less is returned. It's impossible to
1612 * receive a ret value that is bigger than len.
1614 ret
= lttng_write(outfd
, mmap_base
+ mmap_offset
, len
);
1615 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1616 if (ret
< 0 || ((size_t) ret
!= len
)) {
1618 * Report error to caller if nothing was written else at least send the
1626 /* Socket operation failed. We consider the relayd dead */
1627 if (errno
== EPIPE
|| errno
== EINVAL
|| errno
== EBADF
) {
1629 * This is possible if the fd is closed on the other side
1630 * (outfd) or any write problem. It can be verbose a bit for a
1631 * normal execution if for instance the relayd is stopped
1632 * abruptly. This can happen so set this to a DBG statement.
1634 DBG("Consumer mmap write detected relayd hang up");
1636 /* Unhandled error, print it and stop function right now. */
1637 PERROR("Error in write mmap (ret %zd != len %lu)", ret
, len
);
1641 stream
->output_written
+= ret
;
1643 /* This call is useless on a socket so better save a syscall. */
1645 /* This won't block, but will start writeout asynchronously */
1646 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, len
,
1647 SYNC_FILE_RANGE_WRITE
);
1648 stream
->out_fd_offset
+= len
;
1650 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1654 * This is a special case that the relayd has closed its socket. Let's
1655 * cleanup the relayd object and all associated streams.
1657 if (relayd
&& relayd_hang_up
) {
1658 cleanup_relayd(relayd
, ctx
);
1662 /* Unlock only if ctrl socket used */
1663 if (relayd
&& stream
->metadata_flag
) {
1664 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1672 * Splice the data from the ring buffer to the tracefile.
1674 * It must be called with the stream lock held.
1676 * Returns the number of bytes spliced.
1678 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1679 struct lttng_consumer_local_data
*ctx
,
1680 struct lttng_consumer_stream
*stream
, unsigned long len
,
1681 unsigned long padding
,
1682 struct ctf_packet_index
*index
)
1684 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1686 off_t orig_offset
= stream
->out_fd_offset
;
1687 int fd
= stream
->wait_fd
;
1688 /* Default is on the disk */
1689 int outfd
= stream
->out_fd
;
1690 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1692 unsigned int relayd_hang_up
= 0;
1694 switch (consumer_data
.type
) {
1695 case LTTNG_CONSUMER_KERNEL
:
1697 case LTTNG_CONSUMER32_UST
:
1698 case LTTNG_CONSUMER64_UST
:
1699 /* Not supported for user space tracing */
1702 ERR("Unknown consumer_data type");
1706 /* RCU lock for the relayd pointer */
1709 /* Flag that the current stream if set for network streaming. */
1710 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1711 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1712 if (relayd
== NULL
) {
1719 * Choose right pipe for splice. Metadata and trace data are handled by
1720 * different threads hence the use of two pipes in order not to race or
1721 * corrupt the written data.
1723 if (stream
->metadata_flag
) {
1724 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1726 splice_pipe
= ctx
->consumer_thread_pipe
;
1729 /* Write metadata stream id before payload */
1731 unsigned long total_len
= len
;
1733 if (stream
->metadata_flag
) {
1735 * Lock the control socket for the complete duration of the function
1736 * since from this point on we will use the socket.
1738 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1740 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1748 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1751 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1757 /* Use the returned socket. */
1760 /* No streaming, we have to set the len with the full padding */
1764 * Check if we need to change the tracefile before writing the packet.
1766 if (stream
->chan
->tracefile_size
> 0 &&
1767 (stream
->tracefile_size_current
+ len
) >
1768 stream
->chan
->tracefile_size
) {
1769 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1770 stream
->name
, stream
->chan
->tracefile_size
,
1771 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1772 stream
->out_fd
, &(stream
->tracefile_count_current
),
1776 ERR("Rotating output file");
1779 outfd
= stream
->out_fd
;
1781 if (stream
->index_fd
>= 0) {
1782 ret
= index_create_file(stream
->chan
->pathname
,
1783 stream
->name
, stream
->uid
, stream
->gid
,
1784 stream
->chan
->tracefile_size
,
1785 stream
->tracefile_count_current
);
1790 stream
->index_fd
= ret
;
1793 /* Reset current size because we just perform a rotation. */
1794 stream
->tracefile_size_current
= 0;
1795 stream
->out_fd_offset
= 0;
1798 stream
->tracefile_size_current
+= len
;
1799 index
->offset
= htobe64(stream
->out_fd_offset
);
1803 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1804 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1805 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1806 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1807 DBG("splice chan to pipe, ret %zd", ret_splice
);
1808 if (ret_splice
< 0) {
1811 PERROR("Error in relay splice");
1815 /* Handle stream on the relayd if the output is on the network */
1816 if (relayd
&& stream
->metadata_flag
) {
1817 size_t metadata_payload_size
=
1818 sizeof(struct lttcomm_relayd_metadata_payload
);
1820 /* Update counter to fit the spliced data */
1821 ret_splice
+= metadata_payload_size
;
1822 len
+= metadata_payload_size
;
1824 * We do this so the return value can match the len passed as
1825 * argument to this function.
1827 written
-= metadata_payload_size
;
1830 /* Splice data out */
1831 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1832 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1833 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1834 if (ret_splice
< 0) {
1839 } else if (ret_splice
> len
) {
1841 * We don't expect this code path to be executed but you never know
1842 * so this is an extra protection agains a buggy splice().
1845 written
+= ret_splice
;
1846 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1850 /* All good, update current len and continue. */
1854 /* This call is useless on a socket so better save a syscall. */
1856 /* This won't block, but will start writeout asynchronously */
1857 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1858 SYNC_FILE_RANGE_WRITE
);
1859 stream
->out_fd_offset
+= ret_splice
;
1861 stream
->output_written
+= ret_splice
;
1862 written
+= ret_splice
;
1864 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1869 * This is a special case that the relayd has closed its socket. Let's
1870 * cleanup the relayd object and all associated streams.
1872 if (relayd
&& relayd_hang_up
) {
1873 cleanup_relayd(relayd
, ctx
);
1874 /* Skip splice error so the consumer does not fail */
1879 /* send the appropriate error description to sessiond */
1882 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1885 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1888 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1893 if (relayd
&& stream
->metadata_flag
) {
1894 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1902 * Take a snapshot for a specific fd
1904 * Returns 0 on success, < 0 on error
1906 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1908 switch (consumer_data
.type
) {
1909 case LTTNG_CONSUMER_KERNEL
:
1910 return lttng_kconsumer_take_snapshot(stream
);
1911 case LTTNG_CONSUMER32_UST
:
1912 case LTTNG_CONSUMER64_UST
:
1913 return lttng_ustconsumer_take_snapshot(stream
);
1915 ERR("Unknown consumer_data type");
1922 * Get the produced position
1924 * Returns 0 on success, < 0 on error
1926 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1929 switch (consumer_data
.type
) {
1930 case LTTNG_CONSUMER_KERNEL
:
1931 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1932 case LTTNG_CONSUMER32_UST
:
1933 case LTTNG_CONSUMER64_UST
:
1934 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1936 ERR("Unknown consumer_data type");
1942 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1943 int sock
, struct pollfd
*consumer_sockpoll
)
1945 switch (consumer_data
.type
) {
1946 case LTTNG_CONSUMER_KERNEL
:
1947 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1948 case LTTNG_CONSUMER32_UST
:
1949 case LTTNG_CONSUMER64_UST
:
1950 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1952 ERR("Unknown consumer_data type");
1958 void lttng_consumer_close_all_metadata(void)
1960 switch (consumer_data
.type
) {
1961 case LTTNG_CONSUMER_KERNEL
:
1963 * The Kernel consumer has a different metadata scheme so we don't
1964 * close anything because the stream will be closed by the session
1968 case LTTNG_CONSUMER32_UST
:
1969 case LTTNG_CONSUMER64_UST
:
1971 * Close all metadata streams. The metadata hash table is passed and
1972 * this call iterates over it by closing all wakeup fd. This is safe
1973 * because at this point we are sure that the metadata producer is
1974 * either dead or blocked.
1976 lttng_ustconsumer_close_all_metadata(metadata_ht
);
1979 ERR("Unknown consumer_data type");
1985 * Clean up a metadata stream and free its memory.
1987 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1988 struct lttng_ht
*ht
)
1990 struct lttng_consumer_channel
*free_chan
= NULL
;
1994 * This call should NEVER receive regular stream. It must always be
1995 * metadata stream and this is crucial for data structure synchronization.
1997 assert(stream
->metadata_flag
);
1999 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2001 pthread_mutex_lock(&consumer_data
.lock
);
2002 pthread_mutex_lock(&stream
->chan
->lock
);
2003 pthread_mutex_lock(&stream
->lock
);
2005 /* Remove any reference to that stream. */
2006 consumer_stream_delete(stream
, ht
);
2008 /* Close down everything including the relayd if one. */
2009 consumer_stream_close(stream
);
2010 /* Destroy tracer buffers of the stream. */
2011 consumer_stream_destroy_buffers(stream
);
2013 /* Atomically decrement channel refcount since other threads can use it. */
2014 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
2015 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
2016 /* Go for channel deletion! */
2017 free_chan
= stream
->chan
;
2021 * Nullify the stream reference so it is not used after deletion. The
2022 * channel lock MUST be acquired before being able to check for a NULL
2025 stream
->chan
->metadata_stream
= NULL
;
2027 pthread_mutex_unlock(&stream
->lock
);
2028 pthread_mutex_unlock(&stream
->chan
->lock
);
2029 pthread_mutex_unlock(&consumer_data
.lock
);
2032 consumer_del_channel(free_chan
);
2035 consumer_stream_free(stream
);
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 lttng_ht_iter iter
;
2047 struct lttng_ht_node_u64
*node
;
2052 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2054 pthread_mutex_lock(&consumer_data
.lock
);
2055 pthread_mutex_lock(&stream
->chan
->lock
);
2056 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2057 pthread_mutex_lock(&stream
->lock
);
2060 * From here, refcounts are updated so be _careful_ when returning an error
2067 * Lookup the stream just to make sure it does not exist in our internal
2068 * state. This should NEVER happen.
2070 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2071 node
= lttng_ht_iter_get_node_u64(&iter
);
2075 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2076 * in terms of destroying the associated channel, because the action that
2077 * causes the count to become 0 also causes a stream to be added. The
2078 * channel deletion will thus be triggered by the following removal of this
2081 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2082 /* Increment refcount before decrementing nb_init_stream_left */
2084 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2087 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2089 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2090 &stream
->node_channel_id
);
2093 * Add stream to the stream_list_ht of the consumer data. No need to steal
2094 * the key since the HT does not use it and we allow to add redundant keys
2097 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2101 pthread_mutex_unlock(&stream
->lock
);
2102 pthread_mutex_unlock(&stream
->chan
->lock
);
2103 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2104 pthread_mutex_unlock(&consumer_data
.lock
);
2109 * Delete data stream that are flagged for deletion (endpoint_status).
2111 static void validate_endpoint_status_data_stream(void)
2113 struct lttng_ht_iter iter
;
2114 struct lttng_consumer_stream
*stream
;
2116 DBG("Consumer delete flagged data stream");
2119 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2120 /* Validate delete flag of the stream */
2121 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2124 /* Delete it right now */
2125 consumer_del_stream(stream
, data_ht
);
2131 * Delete metadata stream that are flagged for deletion (endpoint_status).
2133 static void validate_endpoint_status_metadata_stream(
2134 struct lttng_poll_event
*pollset
)
2136 struct lttng_ht_iter iter
;
2137 struct lttng_consumer_stream
*stream
;
2139 DBG("Consumer delete flagged metadata stream");
2144 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2145 /* Validate delete flag of the stream */
2146 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2150 * Remove from pollset so the metadata thread can continue without
2151 * blocking on a deleted stream.
2153 lttng_poll_del(pollset
, stream
->wait_fd
);
2155 /* Delete it right now */
2156 consumer_del_metadata_stream(stream
, metadata_ht
);
2162 * Thread polls on metadata file descriptor and write them on disk or on the
2165 void *consumer_thread_metadata_poll(void *data
)
2167 int ret
, i
, pollfd
, err
= -1;
2168 uint32_t revents
, nb_fd
;
2169 struct lttng_consumer_stream
*stream
= NULL
;
2170 struct lttng_ht_iter iter
;
2171 struct lttng_ht_node_u64
*node
;
2172 struct lttng_poll_event events
;
2173 struct lttng_consumer_local_data
*ctx
= data
;
2176 rcu_register_thread();
2178 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2180 if (testpoint(consumerd_thread_metadata
)) {
2181 goto error_testpoint
;
2184 health_code_update();
2186 DBG("Thread metadata poll started");
2188 /* Size is set to 1 for the consumer_metadata pipe */
2189 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2191 ERR("Poll set creation failed");
2195 ret
= lttng_poll_add(&events
,
2196 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2202 DBG("Metadata main loop started");
2205 health_code_update();
2207 /* Only the metadata pipe is set */
2208 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2209 err
= 0; /* All is OK */
2214 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2215 health_poll_entry();
2216 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 health_code_update();
2233 revents
= LTTNG_POLL_GETEV(&events
, i
);
2234 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2236 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2237 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2238 DBG("Metadata thread pipe hung up");
2240 * Remove the pipe from the poll set and continue the loop
2241 * since their might be data to consume.
2243 lttng_poll_del(&events
,
2244 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2245 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2247 } else if (revents
& LPOLLIN
) {
2250 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2251 &stream
, sizeof(stream
));
2252 if (pipe_len
< sizeof(stream
)) {
2253 PERROR("read metadata stream");
2255 * Continue here to handle the rest of the streams.
2260 /* A NULL stream means that the state has changed. */
2261 if (stream
== NULL
) {
2262 /* Check for deleted streams. */
2263 validate_endpoint_status_metadata_stream(&events
);
2267 DBG("Adding metadata stream %d to poll set",
2270 /* Add metadata stream to the global poll events list */
2271 lttng_poll_add(&events
, stream
->wait_fd
,
2272 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2275 /* Handle other stream */
2281 uint64_t tmp_id
= (uint64_t) pollfd
;
2283 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2285 node
= lttng_ht_iter_get_node_u64(&iter
);
2288 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2291 /* Check for error event */
2292 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2293 DBG("Metadata fd %d is hup|err.", pollfd
);
2294 if (!stream
->hangup_flush_done
2295 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2296 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2297 DBG("Attempting to flush and consume the UST buffers");
2298 lttng_ustconsumer_on_stream_hangup(stream
);
2300 /* We just flushed the stream now read it. */
2302 health_code_update();
2304 len
= ctx
->on_buffer_ready(stream
, ctx
);
2306 * We don't check the return value here since if we get
2307 * a negative len, it means an error occured thus we
2308 * simply remove it from the poll set and free the
2314 lttng_poll_del(&events
, stream
->wait_fd
);
2316 * This call update the channel states, closes file descriptors
2317 * and securely free the stream.
2319 consumer_del_metadata_stream(stream
, metadata_ht
);
2320 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2321 /* Get the data out of the metadata file descriptor */
2322 DBG("Metadata available on fd %d", pollfd
);
2323 assert(stream
->wait_fd
== pollfd
);
2326 health_code_update();
2328 len
= ctx
->on_buffer_ready(stream
, ctx
);
2330 * We don't check the return value here since if we get
2331 * a negative len, it means an error occured thus we
2332 * simply remove it from the poll set and free the
2337 /* It's ok to have an unavailable sub-buffer */
2338 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2339 /* Clean up stream from consumer and free it. */
2340 lttng_poll_del(&events
, stream
->wait_fd
);
2341 consumer_del_metadata_stream(stream
, metadata_ht
);
2345 /* Release RCU lock for the stream looked up */
2354 DBG("Metadata poll thread exiting");
2356 lttng_poll_clean(&events
);
2361 ERR("Health error occurred in %s", __func__
);
2363 health_unregister(health_consumerd
);
2364 rcu_unregister_thread();
2369 * This thread polls the fds in the set to consume the data and write
2370 * it to tracefile if necessary.
2372 void *consumer_thread_data_poll(void *data
)
2374 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2375 struct pollfd
*pollfd
= NULL
;
2376 /* local view of the streams */
2377 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2378 /* local view of consumer_data.fds_count */
2380 struct lttng_consumer_local_data
*ctx
= data
;
2383 rcu_register_thread();
2385 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2387 if (testpoint(consumerd_thread_data
)) {
2388 goto error_testpoint
;
2391 health_code_update();
2393 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2394 if (local_stream
== NULL
) {
2395 PERROR("local_stream malloc");
2400 health_code_update();
2406 * the fds set has been updated, we need to update our
2407 * local array as well
2409 pthread_mutex_lock(&consumer_data
.lock
);
2410 if (consumer_data
.need_update
) {
2415 local_stream
= NULL
;
2418 * Allocate for all fds +1 for the consumer_data_pipe and +1 for
2421 pollfd
= zmalloc((consumer_data
.stream_count
+ 2) * sizeof(struct pollfd
));
2422 if (pollfd
== NULL
) {
2423 PERROR("pollfd malloc");
2424 pthread_mutex_unlock(&consumer_data
.lock
);
2428 local_stream
= zmalloc((consumer_data
.stream_count
+ 2) *
2429 sizeof(struct lttng_consumer_stream
*));
2430 if (local_stream
== NULL
) {
2431 PERROR("local_stream malloc");
2432 pthread_mutex_unlock(&consumer_data
.lock
);
2435 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2438 ERR("Error in allocating pollfd or local_outfds");
2439 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2440 pthread_mutex_unlock(&consumer_data
.lock
);
2444 consumer_data
.need_update
= 0;
2446 pthread_mutex_unlock(&consumer_data
.lock
);
2448 /* No FDs and consumer_quit, consumer_cleanup the thread */
2449 if (nb_fd
== 0 && consumer_quit
== 1) {
2450 err
= 0; /* All is OK */
2453 /* poll on the array of fds */
2455 DBG("polling on %d fd", nb_fd
+ 2);
2456 health_poll_entry();
2457 num_rdy
= poll(pollfd
, nb_fd
+ 2, -1);
2459 DBG("poll num_rdy : %d", num_rdy
);
2460 if (num_rdy
== -1) {
2462 * Restart interrupted system call.
2464 if (errno
== EINTR
) {
2467 PERROR("Poll error");
2468 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2470 } else if (num_rdy
== 0) {
2471 DBG("Polling thread timed out");
2476 * If the consumer_data_pipe triggered poll go directly to the
2477 * beginning of the loop to update the array. We want to prioritize
2478 * array update over low-priority reads.
2480 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2481 ssize_t pipe_readlen
;
2483 DBG("consumer_data_pipe wake up");
2484 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2485 &new_stream
, sizeof(new_stream
));
2486 if (pipe_readlen
< sizeof(new_stream
)) {
2487 PERROR("Consumer data pipe");
2488 /* Continue so we can at least handle the current stream(s). */
2493 * If the stream is NULL, just ignore it. It's also possible that
2494 * the sessiond poll thread changed the consumer_quit state and is
2495 * waking us up to test it.
2497 if (new_stream
== NULL
) {
2498 validate_endpoint_status_data_stream();
2502 /* Continue to update the local streams and handle prio ones */
2506 /* Handle wakeup pipe. */
2507 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2509 ssize_t pipe_readlen
;
2511 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2513 if (pipe_readlen
< 0) {
2514 PERROR("Consumer data wakeup pipe");
2516 /* We've been awakened to handle stream(s). */
2517 ctx
->has_wakeup
= 0;
2520 /* Take care of high priority channels first. */
2521 for (i
= 0; i
< nb_fd
; i
++) {
2522 health_code_update();
2524 if (local_stream
[i
] == NULL
) {
2527 if (pollfd
[i
].revents
& POLLPRI
) {
2528 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2530 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2531 /* it's ok to have an unavailable sub-buffer */
2532 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2533 /* Clean the stream and free it. */
2534 consumer_del_stream(local_stream
[i
], data_ht
);
2535 local_stream
[i
] = NULL
;
2536 } else if (len
> 0) {
2537 local_stream
[i
]->data_read
= 1;
2543 * If we read high prio channel in this loop, try again
2544 * for more high prio data.
2550 /* Take care of low priority channels. */
2551 for (i
= 0; i
< nb_fd
; i
++) {
2552 health_code_update();
2554 if (local_stream
[i
] == NULL
) {
2557 if ((pollfd
[i
].revents
& POLLIN
) ||
2558 local_stream
[i
]->hangup_flush_done
||
2559 local_stream
[i
]->has_data
) {
2560 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2561 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2562 /* it's ok to have an unavailable sub-buffer */
2563 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2564 /* Clean the stream and free it. */
2565 consumer_del_stream(local_stream
[i
], data_ht
);
2566 local_stream
[i
] = NULL
;
2567 } else if (len
> 0) {
2568 local_stream
[i
]->data_read
= 1;
2573 /* Handle hangup and errors */
2574 for (i
= 0; i
< nb_fd
; i
++) {
2575 health_code_update();
2577 if (local_stream
[i
] == NULL
) {
2580 if (!local_stream
[i
]->hangup_flush_done
2581 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2582 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2583 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2584 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2586 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2587 /* Attempt read again, for the data we just flushed. */
2588 local_stream
[i
]->data_read
= 1;
2591 * If the poll flag is HUP/ERR/NVAL and we have
2592 * read no data in this pass, we can remove the
2593 * stream from its hash table.
2595 if ((pollfd
[i
].revents
& POLLHUP
)) {
2596 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2597 if (!local_stream
[i
]->data_read
) {
2598 consumer_del_stream(local_stream
[i
], data_ht
);
2599 local_stream
[i
] = NULL
;
2602 } else if (pollfd
[i
].revents
& POLLERR
) {
2603 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2604 if (!local_stream
[i
]->data_read
) {
2605 consumer_del_stream(local_stream
[i
], data_ht
);
2606 local_stream
[i
] = NULL
;
2609 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2610 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2611 if (!local_stream
[i
]->data_read
) {
2612 consumer_del_stream(local_stream
[i
], data_ht
);
2613 local_stream
[i
] = NULL
;
2617 if (local_stream
[i
] != NULL
) {
2618 local_stream
[i
]->data_read
= 0;
2625 DBG("polling thread exiting");
2630 * Close the write side of the pipe so epoll_wait() in
2631 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2632 * read side of the pipe. If we close them both, epoll_wait strangely does
2633 * not return and could create a endless wait period if the pipe is the
2634 * only tracked fd in the poll set. The thread will take care of closing
2637 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2642 ERR("Health error occurred in %s", __func__
);
2644 health_unregister(health_consumerd
);
2646 rcu_unregister_thread();
2651 * Close wake-up end of each stream belonging to the channel. This will
2652 * allow the poll() on the stream read-side to detect when the
2653 * write-side (application) finally closes them.
2656 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2658 struct lttng_ht
*ht
;
2659 struct lttng_consumer_stream
*stream
;
2660 struct lttng_ht_iter iter
;
2662 ht
= consumer_data
.stream_per_chan_id_ht
;
2665 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2666 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2667 ht
->match_fct
, &channel
->key
,
2668 &iter
.iter
, stream
, node_channel_id
.node
) {
2670 * Protect against teardown with mutex.
2672 pthread_mutex_lock(&stream
->lock
);
2673 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2676 switch (consumer_data
.type
) {
2677 case LTTNG_CONSUMER_KERNEL
:
2679 case LTTNG_CONSUMER32_UST
:
2680 case LTTNG_CONSUMER64_UST
:
2681 if (stream
->metadata_flag
) {
2682 /* Safe and protected by the stream lock. */
2683 lttng_ustconsumer_close_metadata(stream
->chan
);
2686 * Note: a mutex is taken internally within
2687 * liblttng-ust-ctl to protect timer wakeup_fd
2688 * use from concurrent close.
2690 lttng_ustconsumer_close_stream_wakeup(stream
);
2694 ERR("Unknown consumer_data type");
2698 pthread_mutex_unlock(&stream
->lock
);
2703 static void destroy_channel_ht(struct lttng_ht
*ht
)
2705 struct lttng_ht_iter iter
;
2706 struct lttng_consumer_channel
*channel
;
2714 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2715 ret
= lttng_ht_del(ht
, &iter
);
2720 lttng_ht_destroy(ht
);
2724 * This thread polls the channel fds to detect when they are being
2725 * closed. It closes all related streams if the channel is detected as
2726 * closed. It is currently only used as a shim layer for UST because the
2727 * consumerd needs to keep the per-stream wakeup end of pipes open for
2730 void *consumer_thread_channel_poll(void *data
)
2732 int ret
, i
, pollfd
, err
= -1;
2733 uint32_t revents
, nb_fd
;
2734 struct lttng_consumer_channel
*chan
= NULL
;
2735 struct lttng_ht_iter iter
;
2736 struct lttng_ht_node_u64
*node
;
2737 struct lttng_poll_event events
;
2738 struct lttng_consumer_local_data
*ctx
= data
;
2739 struct lttng_ht
*channel_ht
;
2741 rcu_register_thread();
2743 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2745 if (testpoint(consumerd_thread_channel
)) {
2746 goto error_testpoint
;
2749 health_code_update();
2751 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2753 /* ENOMEM at this point. Better to bail out. */
2757 DBG("Thread channel poll started");
2759 /* Size is set to 1 for the consumer_channel pipe */
2760 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2762 ERR("Poll set creation failed");
2766 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2772 DBG("Channel main loop started");
2775 health_code_update();
2777 /* Only the channel pipe is set */
2778 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2779 err
= 0; /* All is OK */
2784 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2785 health_poll_entry();
2786 ret
= lttng_poll_wait(&events
, -1);
2788 DBG("Channel event catched in thread");
2790 if (errno
== EINTR
) {
2791 ERR("Poll EINTR catched");
2799 /* From here, the event is a channel wait fd */
2800 for (i
= 0; i
< nb_fd
; i
++) {
2801 health_code_update();
2803 revents
= LTTNG_POLL_GETEV(&events
, i
);
2804 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2806 /* Just don't waste time if no returned events for the fd */
2810 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2811 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2812 DBG("Channel thread pipe hung up");
2814 * Remove the pipe from the poll set and continue the loop
2815 * since their might be data to consume.
2817 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2819 } else if (revents
& LPOLLIN
) {
2820 enum consumer_channel_action action
;
2823 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2825 ERR("Error reading channel pipe");
2830 case CONSUMER_CHANNEL_ADD
:
2831 DBG("Adding channel %d to poll set",
2834 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2837 lttng_ht_add_unique_u64(channel_ht
,
2838 &chan
->wait_fd_node
);
2840 /* Add channel to the global poll events list */
2841 lttng_poll_add(&events
, chan
->wait_fd
,
2842 LPOLLIN
| LPOLLPRI
);
2844 case CONSUMER_CHANNEL_DEL
:
2847 * This command should never be called if the channel
2848 * has streams monitored by either the data or metadata
2849 * thread. The consumer only notify this thread with a
2850 * channel del. command if it receives a destroy
2851 * channel command from the session daemon that send it
2852 * if a command prior to the GET_CHANNEL failed.
2856 chan
= consumer_find_channel(key
);
2859 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2862 lttng_poll_del(&events
, chan
->wait_fd
);
2863 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2864 ret
= lttng_ht_del(channel_ht
, &iter
);
2867 switch (consumer_data
.type
) {
2868 case LTTNG_CONSUMER_KERNEL
:
2870 case LTTNG_CONSUMER32_UST
:
2871 case LTTNG_CONSUMER64_UST
:
2872 health_code_update();
2873 /* Destroy streams that might have been left in the stream list. */
2874 clean_channel_stream_list(chan
);
2877 ERR("Unknown consumer_data type");
2882 * Release our own refcount. Force channel deletion even if
2883 * streams were not initialized.
2885 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2886 consumer_del_channel(chan
);
2891 case CONSUMER_CHANNEL_QUIT
:
2893 * Remove the pipe from the poll set and continue the loop
2894 * since their might be data to consume.
2896 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2899 ERR("Unknown action");
2904 /* Handle other stream */
2910 uint64_t tmp_id
= (uint64_t) pollfd
;
2912 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2914 node
= lttng_ht_iter_get_node_u64(&iter
);
2917 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2920 /* Check for error event */
2921 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2922 DBG("Channel fd %d is hup|err.", pollfd
);
2924 lttng_poll_del(&events
, chan
->wait_fd
);
2925 ret
= lttng_ht_del(channel_ht
, &iter
);
2929 * This will close the wait fd for each stream associated to
2930 * this channel AND monitored by the data/metadata thread thus
2931 * will be clean by the right thread.
2933 consumer_close_channel_streams(chan
);
2935 /* Release our own refcount */
2936 if (!uatomic_sub_return(&chan
->refcount
, 1)
2937 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2938 consumer_del_channel(chan
);
2942 /* Release RCU lock for the channel looked up */
2950 lttng_poll_clean(&events
);
2952 destroy_channel_ht(channel_ht
);
2955 DBG("Channel poll thread exiting");
2958 ERR("Health error occurred in %s", __func__
);
2960 health_unregister(health_consumerd
);
2961 rcu_unregister_thread();
2965 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2966 struct pollfd
*sockpoll
, int client_socket
)
2973 ret
= lttng_consumer_poll_socket(sockpoll
);
2977 DBG("Metadata connection on client_socket");
2979 /* Blocking call, waiting for transmission */
2980 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2981 if (ctx
->consumer_metadata_socket
< 0) {
2982 WARN("On accept metadata");
2993 * This thread listens on the consumerd socket and receives the file
2994 * descriptors from the session daemon.
2996 void *consumer_thread_sessiond_poll(void *data
)
2998 int sock
= -1, client_socket
, ret
, err
= -1;
3000 * structure to poll for incoming data on communication socket avoids
3001 * making blocking sockets.
3003 struct pollfd consumer_sockpoll
[2];
3004 struct lttng_consumer_local_data
*ctx
= data
;
3006 rcu_register_thread();
3008 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3010 if (testpoint(consumerd_thread_sessiond
)) {
3011 goto error_testpoint
;
3014 health_code_update();
3016 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3017 unlink(ctx
->consumer_command_sock_path
);
3018 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3019 if (client_socket
< 0) {
3020 ERR("Cannot create command socket");
3024 ret
= lttcomm_listen_unix_sock(client_socket
);
3029 DBG("Sending ready command to lttng-sessiond");
3030 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3031 /* return < 0 on error, but == 0 is not fatal */
3033 ERR("Error sending ready command to lttng-sessiond");
3037 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3038 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3039 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3040 consumer_sockpoll
[1].fd
= client_socket
;
3041 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3043 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3051 DBG("Connection on client_socket");
3053 /* Blocking call, waiting for transmission */
3054 sock
= lttcomm_accept_unix_sock(client_socket
);
3061 * Setup metadata socket which is the second socket connection on the
3062 * command unix socket.
3064 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3073 /* This socket is not useful anymore. */
3074 ret
= close(client_socket
);
3076 PERROR("close client_socket");
3080 /* update the polling structure to poll on the established socket */
3081 consumer_sockpoll
[1].fd
= sock
;
3082 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3085 health_code_update();
3087 health_poll_entry();
3088 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3097 DBG("Incoming command on sock");
3098 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3101 * This could simply be a session daemon quitting. Don't output
3104 DBG("Communication interrupted on command socket");
3108 if (consumer_quit
) {
3109 DBG("consumer_thread_receive_fds received quit from signal");
3110 err
= 0; /* All is OK */
3113 DBG("received command on sock");
3119 DBG("Consumer thread sessiond poll exiting");
3122 * Close metadata streams since the producer is the session daemon which
3125 * NOTE: for now, this only applies to the UST tracer.
3127 lttng_consumer_close_all_metadata();
3130 * when all fds have hung up, the polling thread
3136 * Notify the data poll thread to poll back again and test the
3137 * consumer_quit state that we just set so to quit gracefully.
3139 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3141 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3143 notify_health_quit_pipe(health_quit_pipe
);
3145 /* Cleaning up possibly open sockets. */
3149 PERROR("close sock sessiond poll");
3152 if (client_socket
>= 0) {
3153 ret
= close(client_socket
);
3155 PERROR("close client_socket sessiond poll");
3162 ERR("Health error occurred in %s", __func__
);
3164 health_unregister(health_consumerd
);
3166 rcu_unregister_thread();
3170 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3171 struct lttng_consumer_local_data
*ctx
)
3175 pthread_mutex_lock(&stream
->lock
);
3176 if (stream
->metadata_flag
) {
3177 pthread_mutex_lock(&stream
->metadata_rdv_lock
);
3180 switch (consumer_data
.type
) {
3181 case LTTNG_CONSUMER_KERNEL
:
3182 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3184 case LTTNG_CONSUMER32_UST
:
3185 case LTTNG_CONSUMER64_UST
:
3186 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3189 ERR("Unknown consumer_data type");
3195 if (stream
->metadata_flag
) {
3196 pthread_cond_broadcast(&stream
->metadata_rdv
);
3197 pthread_mutex_unlock(&stream
->metadata_rdv_lock
);
3199 pthread_mutex_unlock(&stream
->lock
);
3203 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3205 switch (consumer_data
.type
) {
3206 case LTTNG_CONSUMER_KERNEL
:
3207 return lttng_kconsumer_on_recv_stream(stream
);
3208 case LTTNG_CONSUMER32_UST
:
3209 case LTTNG_CONSUMER64_UST
:
3210 return lttng_ustconsumer_on_recv_stream(stream
);
3212 ERR("Unknown consumer_data type");
3219 * Allocate and set consumer data hash tables.
3221 int lttng_consumer_init(void)
3223 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3224 if (!consumer_data
.channel_ht
) {
3228 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3229 if (!consumer_data
.relayd_ht
) {
3233 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3234 if (!consumer_data
.stream_list_ht
) {
3238 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3239 if (!consumer_data
.stream_per_chan_id_ht
) {
3243 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3248 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3260 * Process the ADD_RELAYD command receive by a consumer.
3262 * This will create a relayd socket pair and add it to the relayd hash table.
3263 * The caller MUST acquire a RCU read side lock before calling it.
3265 int consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3266 struct lttng_consumer_local_data
*ctx
, int sock
,
3267 struct pollfd
*consumer_sockpoll
,
3268 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3269 uint64_t relayd_session_id
)
3271 int fd
= -1, ret
= -1, relayd_created
= 0;
3272 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3273 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3276 assert(relayd_sock
);
3278 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3280 /* Get relayd reference if exists. */
3281 relayd
= consumer_find_relayd(net_seq_idx
);
3282 if (relayd
== NULL
) {
3283 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3284 /* Not found. Allocate one. */
3285 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3286 if (relayd
== NULL
) {
3288 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3291 relayd
->sessiond_session_id
= sessiond_id
;
3296 * This code path MUST continue to the consumer send status message to
3297 * we can notify the session daemon and continue our work without
3298 * killing everything.
3302 * relayd key should never be found for control socket.
3304 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3307 /* First send a status message before receiving the fds. */
3308 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3310 /* Somehow, the session daemon is not responding anymore. */
3311 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3312 goto error_nosignal
;
3315 /* Poll on consumer socket. */
3316 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3318 /* Needing to exit in the middle of a command: error. */
3319 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3321 goto error_nosignal
;
3324 /* Get relayd socket from session daemon */
3325 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3326 if (ret
!= sizeof(fd
)) {
3328 fd
= -1; /* Just in case it gets set with an invalid value. */
3331 * Failing to receive FDs might indicate a major problem such as
3332 * reaching a fd limit during the receive where the kernel returns a
3333 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3334 * don't take any chances and stop everything.
3336 * XXX: Feature request #558 will fix that and avoid this possible
3337 * issue when reaching the fd limit.
3339 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3340 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3344 /* Copy socket information and received FD */
3345 switch (sock_type
) {
3346 case LTTNG_STREAM_CONTROL
:
3347 /* Copy received lttcomm socket */
3348 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3349 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3350 /* Handle create_sock error. */
3352 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3356 * Close the socket created internally by
3357 * lttcomm_create_sock, so we can replace it by the one
3358 * received from sessiond.
3360 if (close(relayd
->control_sock
.sock
.fd
)) {
3364 /* Assign new file descriptor */
3365 relayd
->control_sock
.sock
.fd
= fd
;
3366 fd
= -1; /* For error path */
3367 /* Assign version values. */
3368 relayd
->control_sock
.major
= relayd_sock
->major
;
3369 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3371 relayd
->relayd_session_id
= relayd_session_id
;
3374 case LTTNG_STREAM_DATA
:
3375 /* Copy received lttcomm socket */
3376 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3377 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3378 /* Handle create_sock error. */
3380 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3384 * Close the socket created internally by
3385 * lttcomm_create_sock, so we can replace it by the one
3386 * received from sessiond.
3388 if (close(relayd
->data_sock
.sock
.fd
)) {
3392 /* Assign new file descriptor */
3393 relayd
->data_sock
.sock
.fd
= fd
;
3394 fd
= -1; /* for eventual error paths */
3395 /* Assign version values. */
3396 relayd
->data_sock
.major
= relayd_sock
->major
;
3397 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3400 ERR("Unknown relayd socket type (%d)", sock_type
);
3402 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3406 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3407 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3408 relayd
->net_seq_idx
, fd
);
3410 /* We successfully added the socket. Send status back. */
3411 ret
= consumer_send_status_msg(sock
, ret_code
);
3413 /* Somehow, the session daemon is not responding anymore. */
3414 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3415 goto error_nosignal
;
3419 * Add relayd socket pair to consumer data hashtable. If object already
3420 * exists or on error, the function gracefully returns.
3428 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3429 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3433 /* Close received socket if valid. */
3436 PERROR("close received socket");
3440 if (relayd_created
) {
3448 * Try to lock the stream mutex.
3450 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3452 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3459 * Try to lock the stream mutex. On failure, we know that the stream is
3460 * being used else where hence there is data still being extracted.
3462 ret
= pthread_mutex_trylock(&stream
->lock
);
3464 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3476 * Search for a relayd associated to the session id and return the reference.
3478 * A rcu read side lock MUST be acquire before calling this function and locked
3479 * until the relayd object is no longer necessary.
3481 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3483 struct lttng_ht_iter iter
;
3484 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3486 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3487 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3490 * Check by sessiond id which is unique here where the relayd session
3491 * id might not be when having multiple relayd.
3493 if (relayd
->sessiond_session_id
== id
) {
3494 /* Found the relayd. There can be only one per id. */
3506 * Check if for a given session id there is still data needed to be extract
3509 * Return 1 if data is pending or else 0 meaning ready to be read.
3511 int consumer_data_pending(uint64_t id
)
3514 struct lttng_ht_iter iter
;
3515 struct lttng_ht
*ht
;
3516 struct lttng_consumer_stream
*stream
;
3517 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3518 int (*data_pending
)(struct lttng_consumer_stream
*);
3520 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3523 pthread_mutex_lock(&consumer_data
.lock
);
3525 switch (consumer_data
.type
) {
3526 case LTTNG_CONSUMER_KERNEL
:
3527 data_pending
= lttng_kconsumer_data_pending
;
3529 case LTTNG_CONSUMER32_UST
:
3530 case LTTNG_CONSUMER64_UST
:
3531 data_pending
= lttng_ustconsumer_data_pending
;
3534 ERR("Unknown consumer data type");
3538 /* Ease our life a bit */
3539 ht
= consumer_data
.stream_list_ht
;
3541 relayd
= find_relayd_by_session_id(id
);
3543 /* Send init command for data pending. */
3544 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3545 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3546 relayd
->relayd_session_id
);
3547 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3549 /* Communication error thus the relayd so no data pending. */
3550 goto data_not_pending
;
3554 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3555 ht
->hash_fct(&id
, lttng_ht_seed
),
3557 &iter
.iter
, stream
, node_session_id
.node
) {
3558 /* If this call fails, the stream is being used hence data pending. */
3559 ret
= stream_try_lock(stream
);
3565 * A removed node from the hash table indicates that the stream has
3566 * been deleted thus having a guarantee that the buffers are closed
3567 * on the consumer side. However, data can still be transmitted
3568 * over the network so don't skip the relayd check.
3570 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3573 * An empty output file is not valid. We need at least one packet
3574 * generated per stream, even if it contains no event, so it
3575 * contains at least one packet header.
3577 if (stream
->output_written
== 0) {
3578 pthread_mutex_unlock(&stream
->lock
);
3581 /* Check the stream if there is data in the buffers. */
3582 ret
= data_pending(stream
);
3584 pthread_mutex_unlock(&stream
->lock
);
3591 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3592 if (stream
->metadata_flag
) {
3593 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3594 stream
->relayd_stream_id
);
3596 ret
= relayd_data_pending(&relayd
->control_sock
,
3597 stream
->relayd_stream_id
,
3598 stream
->next_net_seq_num
- 1);
3600 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3602 pthread_mutex_unlock(&stream
->lock
);
3606 pthread_mutex_unlock(&stream
->lock
);
3610 unsigned int is_data_inflight
= 0;
3612 /* Send init command for data pending. */
3613 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3614 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3615 relayd
->relayd_session_id
, &is_data_inflight
);
3616 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3618 goto data_not_pending
;
3620 if (is_data_inflight
) {
3626 * Finding _no_ node in the hash table and no inflight data means that the
3627 * stream(s) have been removed thus data is guaranteed to be available for
3628 * analysis from the trace files.
3632 /* Data is available to be read by a viewer. */
3633 pthread_mutex_unlock(&consumer_data
.lock
);
3638 /* Data is still being extracted from buffers. */
3639 pthread_mutex_unlock(&consumer_data
.lock
);
3645 * Send a ret code status message to the sessiond daemon.
3647 * Return the sendmsg() return value.
3649 int consumer_send_status_msg(int sock
, int ret_code
)
3651 struct lttcomm_consumer_status_msg msg
;
3653 memset(&msg
, 0, sizeof(msg
));
3654 msg
.ret_code
= ret_code
;
3656 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3660 * Send a channel status message to the sessiond daemon.
3662 * Return the sendmsg() return value.
3664 int consumer_send_status_channel(int sock
,
3665 struct lttng_consumer_channel
*channel
)
3667 struct lttcomm_consumer_status_channel msg
;
3671 memset(&msg
, 0, sizeof(msg
));
3673 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3675 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3676 msg
.key
= channel
->key
;
3677 msg
.stream_count
= channel
->streams
.count
;
3680 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3684 * Using a maximum stream size with the produced and consumed position of a
3685 * stream, computes the new consumed position to be as close as possible to the
3686 * maximum possible stream size.
3688 * If maximum stream size is lower than the possible buffer size (produced -
3689 * consumed), the consumed_pos given is returned untouched else the new value
3692 unsigned long consumer_get_consumed_maxsize(unsigned long consumed_pos
,
3693 unsigned long produced_pos
, uint64_t max_stream_size
)
3695 if (max_stream_size
&& max_stream_size
< (produced_pos
- consumed_pos
)) {
3696 /* Offset from the produced position to get the latest buffers. */
3697 return produced_pos
- max_stream_size
;
3700 return consumed_pos
;