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.
28 #include <sys/socket.h>
29 #include <sys/types.h>
34 #include <bin/lttng-consumerd/health-consumerd.h>
35 #include <common/common.h>
36 #include <common/utils.h>
37 #include <common/compat/poll.h>
38 #include <common/compat/endian.h>
39 #include <common/index/index.h>
40 #include <common/kernel-ctl/kernel-ctl.h>
41 #include <common/sessiond-comm/relayd.h>
42 #include <common/sessiond-comm/sessiond-comm.h>
43 #include <common/kernel-consumer/kernel-consumer.h>
44 #include <common/relayd/relayd.h>
45 #include <common/ust-consumer/ust-consumer.h>
46 #include <common/consumer-timer.h>
49 #include "consumer-stream.h"
50 #include "consumer-testpoint.h"
53 struct lttng_consumer_global_data consumer_data
= {
56 .type
= LTTNG_CONSUMER_UNKNOWN
,
59 enum consumer_channel_action
{
62 CONSUMER_CHANNEL_QUIT
,
65 struct consumer_channel_msg
{
66 enum consumer_channel_action action
;
67 struct lttng_consumer_channel
*chan
; /* add */
68 uint64_t key
; /* del */
72 * Flag to inform the polling thread to quit when all fd hung up. Updated by
73 * the consumer_thread_receive_fds when it notices that all fds has hung up.
74 * Also updated by the signal handler (consumer_should_exit()). Read by the
77 volatile int consumer_quit
;
80 * Global hash table containing respectively metadata and data streams. The
81 * stream element in this ht should only be updated by the metadata poll thread
82 * for the metadata and the data poll thread for the data.
84 static struct lttng_ht
*metadata_ht
;
85 static struct lttng_ht
*data_ht
;
88 * Notify a thread lttng pipe to poll back again. This usually means that some
89 * global state has changed so we just send back the thread in a poll wait
92 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
94 struct lttng_consumer_stream
*null_stream
= NULL
;
98 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
101 static void notify_health_quit_pipe(int *pipe
)
105 ret
= lttng_write(pipe
[1], "4", 1);
107 PERROR("write consumer health quit");
111 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
112 struct lttng_consumer_channel
*chan
,
114 enum consumer_channel_action action
)
116 struct consumer_channel_msg msg
;
119 memset(&msg
, 0, sizeof(msg
));
124 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
125 if (ret
< sizeof(msg
)) {
126 PERROR("notify_channel_pipe write error");
130 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
133 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
136 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
137 struct lttng_consumer_channel
**chan
,
139 enum consumer_channel_action
*action
)
141 struct consumer_channel_msg msg
;
144 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
145 if (ret
< sizeof(msg
)) {
149 *action
= msg
.action
;
157 * Cleanup the stream list of a channel. Those streams are not yet globally
160 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
162 struct lttng_consumer_stream
*stream
, *stmp
;
166 /* Delete streams that might have been left in the stream list. */
167 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
169 cds_list_del(&stream
->send_node
);
171 * Once a stream is added to this list, the buffers were created so we
172 * have a guarantee that this call will succeed. Setting the monitor
173 * mode to 0 so we don't lock nor try to delete the stream from the
177 consumer_stream_destroy(stream
, NULL
);
182 * Find a stream. The consumer_data.lock must be locked during this
185 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
188 struct lttng_ht_iter iter
;
189 struct lttng_ht_node_u64
*node
;
190 struct lttng_consumer_stream
*stream
= NULL
;
194 /* -1ULL keys are lookup failures */
195 if (key
== (uint64_t) -1ULL) {
201 lttng_ht_lookup(ht
, &key
, &iter
);
202 node
= lttng_ht_iter_get_node_u64(&iter
);
204 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
212 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
214 struct lttng_consumer_stream
*stream
;
217 stream
= find_stream(key
, ht
);
219 stream
->key
= (uint64_t) -1ULL;
221 * We don't want the lookup to match, but we still need
222 * to iterate on this stream when iterating over the hash table. Just
223 * change the node key.
225 stream
->node
.key
= (uint64_t) -1ULL;
231 * Return a channel object for the given key.
233 * RCU read side lock MUST be acquired before calling this function and
234 * protects the channel ptr.
236 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
238 struct lttng_ht_iter iter
;
239 struct lttng_ht_node_u64
*node
;
240 struct lttng_consumer_channel
*channel
= NULL
;
242 /* -1ULL keys are lookup failures */
243 if (key
== (uint64_t) -1ULL) {
247 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
248 node
= lttng_ht_iter_get_node_u64(&iter
);
250 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
257 * There is a possibility that the consumer does not have enough time between
258 * the close of the channel on the session daemon and the cleanup in here thus
259 * once we have a channel add with an existing key, we know for sure that this
260 * channel will eventually get cleaned up by all streams being closed.
262 * This function just nullifies the already existing channel key.
264 static void steal_channel_key(uint64_t key
)
266 struct lttng_consumer_channel
*channel
;
269 channel
= consumer_find_channel(key
);
271 channel
->key
= (uint64_t) -1ULL;
273 * We don't want the lookup to match, but we still need to iterate on
274 * this channel when iterating over the hash table. Just change the
277 channel
->node
.key
= (uint64_t) -1ULL;
282 static void free_channel_rcu(struct rcu_head
*head
)
284 struct lttng_ht_node_u64
*node
=
285 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
286 struct lttng_consumer_channel
*channel
=
287 caa_container_of(node
, struct lttng_consumer_channel
, node
);
293 * RCU protected relayd socket pair free.
295 static void free_relayd_rcu(struct rcu_head
*head
)
297 struct lttng_ht_node_u64
*node
=
298 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
299 struct consumer_relayd_sock_pair
*relayd
=
300 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
303 * Close all sockets. This is done in the call RCU since we don't want the
304 * socket fds to be reassigned thus potentially creating bad state of the
307 * We do not have to lock the control socket mutex here since at this stage
308 * there is no one referencing to this relayd object.
310 (void) relayd_close(&relayd
->control_sock
);
311 (void) relayd_close(&relayd
->data_sock
);
317 * Destroy and free relayd socket pair object.
319 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
322 struct lttng_ht_iter iter
;
324 if (relayd
== NULL
) {
328 DBG("Consumer destroy and close relayd socket pair");
330 iter
.iter
.node
= &relayd
->node
.node
;
331 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
333 /* We assume the relayd is being or is destroyed */
337 /* RCU free() call */
338 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
342 * Remove a channel from the global list protected by a mutex. This function is
343 * also responsible for freeing its data structures.
345 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
348 struct lttng_ht_iter iter
;
350 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
352 pthread_mutex_lock(&consumer_data
.lock
);
353 pthread_mutex_lock(&channel
->lock
);
355 /* Destroy streams that might have been left in the stream list. */
356 clean_channel_stream_list(channel
);
358 if (channel
->live_timer_enabled
== 1) {
359 consumer_timer_live_stop(channel
);
362 switch (consumer_data
.type
) {
363 case LTTNG_CONSUMER_KERNEL
:
365 case LTTNG_CONSUMER32_UST
:
366 case LTTNG_CONSUMER64_UST
:
367 lttng_ustconsumer_del_channel(channel
);
370 ERR("Unknown consumer_data type");
376 iter
.iter
.node
= &channel
->node
.node
;
377 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
381 call_rcu(&channel
->node
.head
, free_channel_rcu
);
383 pthread_mutex_unlock(&channel
->lock
);
384 pthread_mutex_unlock(&consumer_data
.lock
);
388 * Iterate over the relayd hash table and destroy each element. Finally,
389 * destroy the whole hash table.
391 static void cleanup_relayd_ht(void)
393 struct lttng_ht_iter iter
;
394 struct consumer_relayd_sock_pair
*relayd
;
398 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
400 consumer_destroy_relayd(relayd
);
405 lttng_ht_destroy(consumer_data
.relayd_ht
);
409 * Update the end point status of all streams having the given network sequence
410 * index (relayd index).
412 * It's atomically set without having the stream mutex locked which is fine
413 * because we handle the write/read race with a pipe wakeup for each thread.
415 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
416 enum consumer_endpoint_status status
)
418 struct lttng_ht_iter iter
;
419 struct lttng_consumer_stream
*stream
;
421 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
425 /* Let's begin with metadata */
426 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
427 if (stream
->net_seq_idx
== net_seq_idx
) {
428 uatomic_set(&stream
->endpoint_status
, status
);
429 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
433 /* Follow up by the data streams */
434 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
435 if (stream
->net_seq_idx
== net_seq_idx
) {
436 uatomic_set(&stream
->endpoint_status
, status
);
437 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
444 * Cleanup a relayd object by flagging every associated streams for deletion,
445 * destroying the object meaning removing it from the relayd hash table,
446 * closing the sockets and freeing the memory in a RCU call.
448 * If a local data context is available, notify the threads that the streams'
449 * state have changed.
451 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
452 struct lttng_consumer_local_data
*ctx
)
458 DBG("Cleaning up relayd sockets");
460 /* Save the net sequence index before destroying the object */
461 netidx
= relayd
->net_seq_idx
;
464 * Delete the relayd from the relayd hash table, close the sockets and free
465 * the object in a RCU call.
467 consumer_destroy_relayd(relayd
);
469 /* Set inactive endpoint to all streams */
470 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
473 * With a local data context, notify the threads that the streams' state
474 * have changed. The write() action on the pipe acts as an "implicit"
475 * memory barrier ordering the updates of the end point status from the
476 * read of this status which happens AFTER receiving this notify.
479 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
480 notify_thread_lttng_pipe(ctx
->consumer_metadata_pipe
);
485 * Flag a relayd socket pair for destruction. Destroy it if the refcount
488 * RCU read side lock MUST be aquired before calling this function.
490 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
494 /* Set destroy flag for this object */
495 uatomic_set(&relayd
->destroy_flag
, 1);
497 /* Destroy the relayd if refcount is 0 */
498 if (uatomic_read(&relayd
->refcount
) == 0) {
499 consumer_destroy_relayd(relayd
);
504 * Completly destroy stream from every visiable data structure and the given
507 * One this call returns, the stream object is not longer usable nor visible.
509 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
512 consumer_stream_destroy(stream
, ht
);
516 * XXX naming of del vs destroy is all mixed up.
518 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
520 consumer_stream_destroy(stream
, data_ht
);
523 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
525 consumer_stream_destroy(stream
, metadata_ht
);
528 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
530 enum lttng_consumer_stream_state state
,
531 const char *channel_name
,
538 enum consumer_channel_type type
,
539 unsigned int monitor
)
542 struct lttng_consumer_stream
*stream
;
544 stream
= zmalloc(sizeof(*stream
));
545 if (stream
== NULL
) {
546 PERROR("malloc struct lttng_consumer_stream");
553 stream
->key
= stream_key
;
555 stream
->out_fd_offset
= 0;
556 stream
->output_written
= 0;
557 stream
->state
= state
;
560 stream
->net_seq_idx
= relayd_id
;
561 stream
->session_id
= session_id
;
562 stream
->monitor
= monitor
;
563 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
564 stream
->index_fd
= -1;
565 pthread_mutex_init(&stream
->lock
, NULL
);
567 /* If channel is the metadata, flag this stream as metadata. */
568 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
569 stream
->metadata_flag
= 1;
570 /* Metadata is flat out. */
571 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
572 /* Live rendez-vous point. */
573 pthread_cond_init(&stream
->metadata_rdv
, NULL
);
574 pthread_mutex_init(&stream
->metadata_rdv_lock
, NULL
);
576 /* Format stream name to <channel_name>_<cpu_number> */
577 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
580 PERROR("snprintf stream name");
585 /* Key is always the wait_fd for streams. */
586 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
588 /* Init node per channel id key */
589 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
591 /* Init session id node with the stream session id */
592 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
594 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
595 " relayd_id %" PRIu64
", session_id %" PRIu64
,
596 stream
->name
, stream
->key
, channel_key
,
597 stream
->net_seq_idx
, stream
->session_id
);
613 * Add a stream to the global list protected by a mutex.
615 int consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
617 struct lttng_ht
*ht
= data_ht
;
623 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
625 pthread_mutex_lock(&consumer_data
.lock
);
626 pthread_mutex_lock(&stream
->chan
->lock
);
627 pthread_mutex_lock(&stream
->chan
->timer_lock
);
628 pthread_mutex_lock(&stream
->lock
);
631 /* Steal stream identifier to avoid having streams with the same key */
632 steal_stream_key(stream
->key
, ht
);
634 lttng_ht_add_unique_u64(ht
, &stream
->node
);
636 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
637 &stream
->node_channel_id
);
640 * Add stream to the stream_list_ht of the consumer data. No need to steal
641 * the key since the HT does not use it and we allow to add redundant keys
644 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
647 * When nb_init_stream_left reaches 0, we don't need to trigger any action
648 * in terms of destroying the associated channel, because the action that
649 * causes the count to become 0 also causes a stream to be added. The
650 * channel deletion will thus be triggered by the following removal of this
653 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
654 /* Increment refcount before decrementing nb_init_stream_left */
656 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
659 /* Update consumer data once the node is inserted. */
660 consumer_data
.stream_count
++;
661 consumer_data
.need_update
= 1;
664 pthread_mutex_unlock(&stream
->lock
);
665 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
666 pthread_mutex_unlock(&stream
->chan
->lock
);
667 pthread_mutex_unlock(&consumer_data
.lock
);
672 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
674 consumer_del_stream(stream
, data_ht
);
678 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
679 * be acquired before calling this.
681 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
684 struct lttng_ht_node_u64
*node
;
685 struct lttng_ht_iter iter
;
689 lttng_ht_lookup(consumer_data
.relayd_ht
,
690 &relayd
->net_seq_idx
, &iter
);
691 node
= lttng_ht_iter_get_node_u64(&iter
);
695 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
702 * Allocate and return a consumer relayd socket.
704 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
705 uint64_t net_seq_idx
)
707 struct consumer_relayd_sock_pair
*obj
= NULL
;
709 /* net sequence index of -1 is a failure */
710 if (net_seq_idx
== (uint64_t) -1ULL) {
714 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
716 PERROR("zmalloc relayd sock");
720 obj
->net_seq_idx
= net_seq_idx
;
722 obj
->destroy_flag
= 0;
723 obj
->control_sock
.sock
.fd
= -1;
724 obj
->data_sock
.sock
.fd
= -1;
725 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
726 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
733 * Find a relayd socket pair in the global consumer data.
735 * Return the object if found else NULL.
736 * RCU read-side lock must be held across this call and while using the
739 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
741 struct lttng_ht_iter iter
;
742 struct lttng_ht_node_u64
*node
;
743 struct consumer_relayd_sock_pair
*relayd
= NULL
;
745 /* Negative keys are lookup failures */
746 if (key
== (uint64_t) -1ULL) {
750 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
752 node
= lttng_ht_iter_get_node_u64(&iter
);
754 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
762 * Find a relayd and send the stream
764 * Returns 0 on success, < 0 on error
766 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
770 struct consumer_relayd_sock_pair
*relayd
;
773 assert(stream
->net_seq_idx
!= -1ULL);
776 /* The stream is not metadata. Get relayd reference if exists. */
778 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
779 if (relayd
!= NULL
) {
780 /* Add stream on the relayd */
781 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
782 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
783 path
, &stream
->relayd_stream_id
,
784 stream
->chan
->tracefile_size
, stream
->chan
->tracefile_count
);
785 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
790 uatomic_inc(&relayd
->refcount
);
791 stream
->sent_to_relayd
= 1;
793 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
794 stream
->key
, stream
->net_seq_idx
);
799 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
800 stream
->name
, stream
->key
, stream
->net_seq_idx
);
808 * Find a relayd and send the streams sent message
810 * Returns 0 on success, < 0 on error
812 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
815 struct consumer_relayd_sock_pair
*relayd
;
817 assert(net_seq_idx
!= -1ULL);
819 /* The stream is not metadata. Get relayd reference if exists. */
821 relayd
= consumer_find_relayd(net_seq_idx
);
822 if (relayd
!= NULL
) {
823 /* Add stream on the relayd */
824 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
825 ret
= relayd_streams_sent(&relayd
->control_sock
);
826 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
831 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
838 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
846 * Find a relayd and close the stream
848 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
850 struct consumer_relayd_sock_pair
*relayd
;
852 /* The stream is not metadata. Get relayd reference if exists. */
854 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
856 consumer_stream_relayd_close(stream
, relayd
);
862 * Handle stream for relayd transmission if the stream applies for network
863 * streaming where the net sequence index is set.
865 * Return destination file descriptor or negative value on error.
867 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
868 size_t data_size
, unsigned long padding
,
869 struct consumer_relayd_sock_pair
*relayd
)
872 struct lttcomm_relayd_data_hdr data_hdr
;
878 /* Reset data header */
879 memset(&data_hdr
, 0, sizeof(data_hdr
));
881 if (stream
->metadata_flag
) {
882 /* Caller MUST acquire the relayd control socket lock */
883 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
888 /* Metadata are always sent on the control socket. */
889 outfd
= relayd
->control_sock
.sock
.fd
;
891 /* Set header with stream information */
892 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
893 data_hdr
.data_size
= htobe32(data_size
);
894 data_hdr
.padding_size
= htobe32(padding
);
896 * Note that net_seq_num below is assigned with the *current* value of
897 * next_net_seq_num and only after that the next_net_seq_num will be
898 * increment. This is why when issuing a command on the relayd using
899 * this next value, 1 should always be substracted in order to compare
900 * the last seen sequence number on the relayd side to the last sent.
902 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
903 /* Other fields are zeroed previously */
905 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
911 ++stream
->next_net_seq_num
;
913 /* Set to go on data socket */
914 outfd
= relayd
->data_sock
.sock
.fd
;
922 * Allocate and return a new lttng_consumer_channel object using the given key
923 * to initialize the hash table node.
925 * On error, return NULL.
927 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
929 const char *pathname
,
934 enum lttng_event_output output
,
935 uint64_t tracefile_size
,
936 uint64_t tracefile_count
,
937 uint64_t session_id_per_pid
,
938 unsigned int monitor
,
939 unsigned int live_timer_interval
,
940 const char *root_shm_path
,
941 const char *shm_path
)
943 struct lttng_consumer_channel
*channel
;
945 channel
= zmalloc(sizeof(*channel
));
946 if (channel
== NULL
) {
947 PERROR("malloc struct lttng_consumer_channel");
952 channel
->refcount
= 0;
953 channel
->session_id
= session_id
;
954 channel
->session_id_per_pid
= session_id_per_pid
;
957 channel
->relayd_id
= relayd_id
;
958 channel
->tracefile_size
= tracefile_size
;
959 channel
->tracefile_count
= tracefile_count
;
960 channel
->monitor
= monitor
;
961 channel
->live_timer_interval
= live_timer_interval
;
962 pthread_mutex_init(&channel
->lock
, NULL
);
963 pthread_mutex_init(&channel
->timer_lock
, NULL
);
966 case LTTNG_EVENT_SPLICE
:
967 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
969 case LTTNG_EVENT_MMAP
:
970 channel
->output
= CONSUMER_CHANNEL_MMAP
;
980 * In monitor mode, the streams associated with the channel will be put in
981 * a special list ONLY owned by this channel. So, the refcount is set to 1
982 * here meaning that the channel itself has streams that are referenced.
984 * On a channel deletion, once the channel is no longer visible, the
985 * refcount is decremented and checked for a zero value to delete it. With
986 * streams in no monitor mode, it will now be safe to destroy the channel.
988 if (!channel
->monitor
) {
989 channel
->refcount
= 1;
992 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
993 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
995 strncpy(channel
->name
, name
, sizeof(channel
->name
));
996 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
999 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1000 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1003 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1004 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1007 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1009 channel
->wait_fd
= -1;
1011 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1013 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
1020 * Add a channel to the global list protected by a mutex.
1022 * Always return 0 indicating success.
1024 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1025 struct lttng_consumer_local_data
*ctx
)
1027 pthread_mutex_lock(&consumer_data
.lock
);
1028 pthread_mutex_lock(&channel
->lock
);
1029 pthread_mutex_lock(&channel
->timer_lock
);
1032 * This gives us a guarantee that the channel we are about to add to the
1033 * channel hash table will be unique. See this function comment on the why
1034 * we need to steel the channel key at this stage.
1036 steal_channel_key(channel
->key
);
1039 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1042 pthread_mutex_unlock(&channel
->timer_lock
);
1043 pthread_mutex_unlock(&channel
->lock
);
1044 pthread_mutex_unlock(&consumer_data
.lock
);
1046 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1047 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1054 * Allocate the pollfd structure and the local view of the out fds to avoid
1055 * doing a lookup in the linked list and concurrency issues when writing is
1056 * needed. Called with consumer_data.lock held.
1058 * Returns the number of fds in the structures.
1060 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1061 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1062 struct lttng_ht
*ht
)
1065 struct lttng_ht_iter iter
;
1066 struct lttng_consumer_stream
*stream
;
1071 assert(local_stream
);
1073 DBG("Updating poll fd array");
1075 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1077 * Only active streams with an active end point can be added to the
1078 * poll set and local stream storage of the thread.
1080 * There is a potential race here for endpoint_status to be updated
1081 * just after the check. However, this is OK since the stream(s) will
1082 * be deleted once the thread is notified that the end point state has
1083 * changed where this function will be called back again.
1085 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
1086 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1090 * This clobbers way too much the debug output. Uncomment that if you
1091 * need it for debugging purposes.
1093 * DBG("Active FD %d", stream->wait_fd);
1095 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1096 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1097 local_stream
[i
] = stream
;
1103 * Insert the consumer_data_pipe at the end of the array and don't
1104 * increment i so nb_fd is the number of real FD.
1106 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1107 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1109 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1110 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1115 * Poll on the should_quit pipe and the command socket return -1 on
1116 * error, 1 if should exit, 0 if data is available on the command socket
1118 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1123 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1124 if (num_rdy
== -1) {
1126 * Restart interrupted system call.
1128 if (errno
== EINTR
) {
1131 PERROR("Poll error");
1134 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1135 DBG("consumer_should_quit wake up");
1142 * Set the error socket.
1144 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1147 ctx
->consumer_error_socket
= sock
;
1151 * Set the command socket path.
1153 void lttng_consumer_set_command_sock_path(
1154 struct lttng_consumer_local_data
*ctx
, char *sock
)
1156 ctx
->consumer_command_sock_path
= sock
;
1160 * Send return code to the session daemon.
1161 * If the socket is not defined, we return 0, it is not a fatal error
1163 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1165 if (ctx
->consumer_error_socket
> 0) {
1166 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1167 sizeof(enum lttcomm_sessiond_command
));
1174 * Close all the tracefiles and stream fds and MUST be called when all
1175 * instances are destroyed i.e. when all threads were joined and are ended.
1177 void lttng_consumer_cleanup(void)
1179 struct lttng_ht_iter iter
;
1180 struct lttng_consumer_channel
*channel
;
1184 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1186 consumer_del_channel(channel
);
1191 lttng_ht_destroy(consumer_data
.channel_ht
);
1193 cleanup_relayd_ht();
1195 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1198 * This HT contains streams that are freed by either the metadata thread or
1199 * the data thread so we do *nothing* on the hash table and simply destroy
1202 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1206 * Called from signal handler.
1208 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1213 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1215 PERROR("write consumer quit");
1218 DBG("Consumer flag that it should quit");
1221 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1224 int outfd
= stream
->out_fd
;
1227 * This does a blocking write-and-wait on any page that belongs to the
1228 * subbuffer prior to the one we just wrote.
1229 * Don't care about error values, as these are just hints and ways to
1230 * limit the amount of page cache used.
1232 if (orig_offset
< stream
->max_sb_size
) {
1235 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1236 stream
->max_sb_size
,
1237 SYNC_FILE_RANGE_WAIT_BEFORE
1238 | SYNC_FILE_RANGE_WRITE
1239 | SYNC_FILE_RANGE_WAIT_AFTER
);
1241 * Give hints to the kernel about how we access the file:
1242 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1245 * We need to call fadvise again after the file grows because the
1246 * kernel does not seem to apply fadvise to non-existing parts of the
1249 * Call fadvise _after_ having waited for the page writeback to
1250 * complete because the dirty page writeback semantic is not well
1251 * defined. So it can be expected to lead to lower throughput in
1254 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1255 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1259 * Initialise the necessary environnement :
1260 * - create a new context
1261 * - create the poll_pipe
1262 * - create the should_quit pipe (for signal handler)
1263 * - create the thread pipe (for splice)
1265 * Takes a function pointer as argument, this function is called when data is
1266 * available on a buffer. This function is responsible to do the
1267 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1268 * buffer configuration and then kernctl_put_next_subbuf at the end.
1270 * Returns a pointer to the new context or NULL on error.
1272 struct lttng_consumer_local_data
*lttng_consumer_create(
1273 enum lttng_consumer_type type
,
1274 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1275 struct lttng_consumer_local_data
*ctx
),
1276 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1277 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1278 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1281 struct lttng_consumer_local_data
*ctx
;
1283 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1284 consumer_data
.type
== type
);
1285 consumer_data
.type
= type
;
1287 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1289 PERROR("allocating context");
1293 ctx
->consumer_error_socket
= -1;
1294 ctx
->consumer_metadata_socket
= -1;
1295 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1296 /* assign the callbacks */
1297 ctx
->on_buffer_ready
= buffer_ready
;
1298 ctx
->on_recv_channel
= recv_channel
;
1299 ctx
->on_recv_stream
= recv_stream
;
1300 ctx
->on_update_stream
= update_stream
;
1302 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1303 if (!ctx
->consumer_data_pipe
) {
1304 goto error_poll_pipe
;
1307 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1308 if (!ctx
->consumer_wakeup_pipe
) {
1309 goto error_wakeup_pipe
;
1312 ret
= pipe(ctx
->consumer_should_quit
);
1314 PERROR("Error creating recv pipe");
1315 goto error_quit_pipe
;
1318 ret
= pipe(ctx
->consumer_channel_pipe
);
1320 PERROR("Error creating channel pipe");
1321 goto error_channel_pipe
;
1324 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1325 if (!ctx
->consumer_metadata_pipe
) {
1326 goto error_metadata_pipe
;
1331 error_metadata_pipe
:
1332 utils_close_pipe(ctx
->consumer_channel_pipe
);
1334 utils_close_pipe(ctx
->consumer_should_quit
);
1336 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1338 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1346 * Iterate over all streams of the hashtable and free them properly.
1348 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1350 struct lttng_ht_iter iter
;
1351 struct lttng_consumer_stream
*stream
;
1358 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1360 * Ignore return value since we are currently cleaning up so any error
1363 (void) consumer_del_stream(stream
, ht
);
1367 lttng_ht_destroy(ht
);
1371 * Iterate over all streams of the metadata hashtable and free them
1374 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1376 struct lttng_ht_iter iter
;
1377 struct lttng_consumer_stream
*stream
;
1384 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1386 * Ignore return value since we are currently cleaning up so any error
1389 (void) consumer_del_metadata_stream(stream
, ht
);
1393 lttng_ht_destroy(ht
);
1397 * Close all fds associated with the instance and free the context.
1399 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1403 DBG("Consumer destroying it. Closing everything.");
1409 destroy_data_stream_ht(data_ht
);
1410 destroy_metadata_stream_ht(metadata_ht
);
1412 ret
= close(ctx
->consumer_error_socket
);
1416 ret
= close(ctx
->consumer_metadata_socket
);
1420 utils_close_pipe(ctx
->consumer_channel_pipe
);
1421 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1422 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1423 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1424 utils_close_pipe(ctx
->consumer_should_quit
);
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
) {
1717 splice_pipe
= stream
->splice_pipe
;
1719 /* Write metadata stream id before payload */
1721 unsigned long total_len
= len
;
1723 if (stream
->metadata_flag
) {
1725 * Lock the control socket for the complete duration of the function
1726 * since from this point on we will use the socket.
1728 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1730 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1738 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1741 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1747 /* Use the returned socket. */
1750 /* No streaming, we have to set the len with the full padding */
1754 * Check if we need to change the tracefile before writing the packet.
1756 if (stream
->chan
->tracefile_size
> 0 &&
1757 (stream
->tracefile_size_current
+ len
) >
1758 stream
->chan
->tracefile_size
) {
1759 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1760 stream
->name
, stream
->chan
->tracefile_size
,
1761 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1762 stream
->out_fd
, &(stream
->tracefile_count_current
),
1766 ERR("Rotating output file");
1769 outfd
= stream
->out_fd
;
1771 if (stream
->index_fd
>= 0) {
1772 ret
= index_create_file(stream
->chan
->pathname
,
1773 stream
->name
, stream
->uid
, stream
->gid
,
1774 stream
->chan
->tracefile_size
,
1775 stream
->tracefile_count_current
);
1780 stream
->index_fd
= ret
;
1783 /* Reset current size because we just perform a rotation. */
1784 stream
->tracefile_size_current
= 0;
1785 stream
->out_fd_offset
= 0;
1788 stream
->tracefile_size_current
+= len
;
1789 index
->offset
= htobe64(stream
->out_fd_offset
);
1793 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1794 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1795 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1796 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1797 DBG("splice chan to pipe, ret %zd", ret_splice
);
1798 if (ret_splice
< 0) {
1801 PERROR("Error in relay splice");
1805 /* Handle stream on the relayd if the output is on the network */
1806 if (relayd
&& stream
->metadata_flag
) {
1807 size_t metadata_payload_size
=
1808 sizeof(struct lttcomm_relayd_metadata_payload
);
1810 /* Update counter to fit the spliced data */
1811 ret_splice
+= metadata_payload_size
;
1812 len
+= metadata_payload_size
;
1814 * We do this so the return value can match the len passed as
1815 * argument to this function.
1817 written
-= metadata_payload_size
;
1820 /* Splice data out */
1821 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1822 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1823 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1825 if (ret_splice
< 0) {
1830 } else if (ret_splice
> len
) {
1832 * We don't expect this code path to be executed but you never know
1833 * so this is an extra protection agains a buggy splice().
1836 written
+= ret_splice
;
1837 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1841 /* All good, update current len and continue. */
1845 /* This call is useless on a socket so better save a syscall. */
1847 /* This won't block, but will start writeout asynchronously */
1848 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1849 SYNC_FILE_RANGE_WRITE
);
1850 stream
->out_fd_offset
+= ret_splice
;
1852 stream
->output_written
+= ret_splice
;
1853 written
+= ret_splice
;
1855 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1860 * This is a special case that the relayd has closed its socket. Let's
1861 * cleanup the relayd object and all associated streams.
1863 if (relayd
&& relayd_hang_up
) {
1864 cleanup_relayd(relayd
, ctx
);
1865 /* Skip splice error so the consumer does not fail */
1870 /* send the appropriate error description to sessiond */
1873 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1876 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1879 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1884 if (relayd
&& stream
->metadata_flag
) {
1885 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1893 * Take a snapshot for a specific fd
1895 * Returns 0 on success, < 0 on error
1897 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1899 switch (consumer_data
.type
) {
1900 case LTTNG_CONSUMER_KERNEL
:
1901 return lttng_kconsumer_take_snapshot(stream
);
1902 case LTTNG_CONSUMER32_UST
:
1903 case LTTNG_CONSUMER64_UST
:
1904 return lttng_ustconsumer_take_snapshot(stream
);
1906 ERR("Unknown consumer_data type");
1913 * Get the produced position
1915 * Returns 0 on success, < 0 on error
1917 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1920 switch (consumer_data
.type
) {
1921 case LTTNG_CONSUMER_KERNEL
:
1922 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1923 case LTTNG_CONSUMER32_UST
:
1924 case LTTNG_CONSUMER64_UST
:
1925 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1927 ERR("Unknown consumer_data type");
1933 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1934 int sock
, struct pollfd
*consumer_sockpoll
)
1936 switch (consumer_data
.type
) {
1937 case LTTNG_CONSUMER_KERNEL
:
1938 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1939 case LTTNG_CONSUMER32_UST
:
1940 case LTTNG_CONSUMER64_UST
:
1941 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1943 ERR("Unknown consumer_data type");
1949 void lttng_consumer_close_all_metadata(void)
1951 switch (consumer_data
.type
) {
1952 case LTTNG_CONSUMER_KERNEL
:
1954 * The Kernel consumer has a different metadata scheme so we don't
1955 * close anything because the stream will be closed by the session
1959 case LTTNG_CONSUMER32_UST
:
1960 case LTTNG_CONSUMER64_UST
:
1962 * Close all metadata streams. The metadata hash table is passed and
1963 * this call iterates over it by closing all wakeup fd. This is safe
1964 * because at this point we are sure that the metadata producer is
1965 * either dead or blocked.
1967 lttng_ustconsumer_close_all_metadata(metadata_ht
);
1970 ERR("Unknown consumer_data type");
1976 * Clean up a metadata stream and free its memory.
1978 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1979 struct lttng_ht
*ht
)
1981 struct lttng_consumer_channel
*free_chan
= NULL
;
1985 * This call should NEVER receive regular stream. It must always be
1986 * metadata stream and this is crucial for data structure synchronization.
1988 assert(stream
->metadata_flag
);
1990 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1992 pthread_mutex_lock(&consumer_data
.lock
);
1993 pthread_mutex_lock(&stream
->chan
->lock
);
1994 pthread_mutex_lock(&stream
->lock
);
1996 /* Remove any reference to that stream. */
1997 consumer_stream_delete(stream
, ht
);
1999 /* Close down everything including the relayd if one. */
2000 consumer_stream_close(stream
);
2001 /* Destroy tracer buffers of the stream. */
2002 consumer_stream_destroy_buffers(stream
);
2004 /* Atomically decrement channel refcount since other threads can use it. */
2005 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
2006 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
2007 /* Go for channel deletion! */
2008 free_chan
= stream
->chan
;
2012 * Nullify the stream reference so it is not used after deletion. The
2013 * channel lock MUST be acquired before being able to check for a NULL
2016 stream
->chan
->metadata_stream
= NULL
;
2018 pthread_mutex_unlock(&stream
->lock
);
2019 pthread_mutex_unlock(&stream
->chan
->lock
);
2020 pthread_mutex_unlock(&consumer_data
.lock
);
2023 consumer_del_channel(free_chan
);
2026 consumer_stream_free(stream
);
2030 * Action done with the metadata stream when adding it to the consumer internal
2031 * data structures to handle it.
2033 int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2035 struct lttng_ht
*ht
= metadata_ht
;
2037 struct lttng_ht_iter iter
;
2038 struct lttng_ht_node_u64
*node
;
2043 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2045 pthread_mutex_lock(&consumer_data
.lock
);
2046 pthread_mutex_lock(&stream
->chan
->lock
);
2047 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2048 pthread_mutex_lock(&stream
->lock
);
2051 * From here, refcounts are updated so be _careful_ when returning an error
2058 * Lookup the stream just to make sure it does not exist in our internal
2059 * state. This should NEVER happen.
2061 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2062 node
= lttng_ht_iter_get_node_u64(&iter
);
2066 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2067 * in terms of destroying the associated channel, because the action that
2068 * causes the count to become 0 also causes a stream to be added. The
2069 * channel deletion will thus be triggered by the following removal of this
2072 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2073 /* Increment refcount before decrementing nb_init_stream_left */
2075 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2078 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2080 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2081 &stream
->node_channel_id
);
2084 * Add stream to the stream_list_ht of the consumer data. No need to steal
2085 * the key since the HT does not use it and we allow to add redundant keys
2088 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2092 pthread_mutex_unlock(&stream
->lock
);
2093 pthread_mutex_unlock(&stream
->chan
->lock
);
2094 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2095 pthread_mutex_unlock(&consumer_data
.lock
);
2100 * Delete data stream that are flagged for deletion (endpoint_status).
2102 static void validate_endpoint_status_data_stream(void)
2104 struct lttng_ht_iter iter
;
2105 struct lttng_consumer_stream
*stream
;
2107 DBG("Consumer delete flagged data stream");
2110 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2111 /* Validate delete flag of the stream */
2112 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2115 /* Delete it right now */
2116 consumer_del_stream(stream
, data_ht
);
2122 * Delete metadata stream that are flagged for deletion (endpoint_status).
2124 static void validate_endpoint_status_metadata_stream(
2125 struct lttng_poll_event
*pollset
)
2127 struct lttng_ht_iter iter
;
2128 struct lttng_consumer_stream
*stream
;
2130 DBG("Consumer delete flagged metadata stream");
2135 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2136 /* Validate delete flag of the stream */
2137 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2141 * Remove from pollset so the metadata thread can continue without
2142 * blocking on a deleted stream.
2144 lttng_poll_del(pollset
, stream
->wait_fd
);
2146 /* Delete it right now */
2147 consumer_del_metadata_stream(stream
, metadata_ht
);
2153 * Thread polls on metadata file descriptor and write them on disk or on the
2156 void *consumer_thread_metadata_poll(void *data
)
2158 int ret
, i
, pollfd
, err
= -1;
2159 uint32_t revents
, nb_fd
;
2160 struct lttng_consumer_stream
*stream
= NULL
;
2161 struct lttng_ht_iter iter
;
2162 struct lttng_ht_node_u64
*node
;
2163 struct lttng_poll_event events
;
2164 struct lttng_consumer_local_data
*ctx
= data
;
2167 rcu_register_thread();
2169 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2171 if (testpoint(consumerd_thread_metadata
)) {
2172 goto error_testpoint
;
2175 health_code_update();
2177 DBG("Thread metadata poll started");
2179 /* Size is set to 1 for the consumer_metadata pipe */
2180 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2182 ERR("Poll set creation failed");
2186 ret
= lttng_poll_add(&events
,
2187 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2193 DBG("Metadata main loop started");
2197 health_code_update();
2198 health_poll_entry();
2199 DBG("Metadata poll wait");
2200 ret
= lttng_poll_wait(&events
, -1);
2201 DBG("Metadata poll return from wait with %d fd(s)",
2202 LTTNG_POLL_GETNB(&events
));
2204 DBG("Metadata event catched in thread");
2206 if (errno
== EINTR
) {
2207 ERR("Poll EINTR catched");
2210 if (LTTNG_POLL_GETNB(&events
) == 0) {
2211 err
= 0; /* All is OK */
2218 /* From here, the event is a metadata wait fd */
2219 for (i
= 0; i
< nb_fd
; i
++) {
2220 health_code_update();
2222 revents
= LTTNG_POLL_GETEV(&events
, i
);
2223 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2226 /* No activity for this FD (poll implementation). */
2230 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2231 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2232 DBG("Metadata thread pipe hung up");
2234 * Remove the pipe from the poll set and continue the loop
2235 * since their might be data to consume.
2237 lttng_poll_del(&events
,
2238 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2239 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2241 } else if (revents
& LPOLLIN
) {
2244 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2245 &stream
, sizeof(stream
));
2246 if (pipe_len
< sizeof(stream
)) {
2247 PERROR("read metadata stream");
2249 * Continue here to handle the rest of the streams.
2254 /* A NULL stream means that the state has changed. */
2255 if (stream
== NULL
) {
2256 /* Check for deleted streams. */
2257 validate_endpoint_status_metadata_stream(&events
);
2261 DBG("Adding metadata stream %d to poll set",
2264 /* Add metadata stream to the global poll events list */
2265 lttng_poll_add(&events
, stream
->wait_fd
,
2266 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2269 /* Handle other stream */
2275 uint64_t tmp_id
= (uint64_t) pollfd
;
2277 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2279 node
= lttng_ht_iter_get_node_u64(&iter
);
2282 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2285 /* Check for error event */
2286 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2287 DBG("Metadata fd %d is hup|err.", pollfd
);
2288 if (!stream
->hangup_flush_done
2289 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2290 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2291 DBG("Attempting to flush and consume the UST buffers");
2292 lttng_ustconsumer_on_stream_hangup(stream
);
2294 /* We just flushed the stream now read it. */
2296 health_code_update();
2298 len
= ctx
->on_buffer_ready(stream
, ctx
);
2300 * We don't check the return value here since if we get
2301 * a negative len, it means an error occured thus we
2302 * simply remove it from the poll set and free the
2308 lttng_poll_del(&events
, stream
->wait_fd
);
2310 * This call update the channel states, closes file descriptors
2311 * and securely free the stream.
2313 consumer_del_metadata_stream(stream
, metadata_ht
);
2314 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2315 /* Get the data out of the metadata file descriptor */
2316 DBG("Metadata available on fd %d", pollfd
);
2317 assert(stream
->wait_fd
== pollfd
);
2320 health_code_update();
2322 len
= ctx
->on_buffer_ready(stream
, ctx
);
2324 * We don't check the return value here since if we get
2325 * a negative len, it means an error occured thus we
2326 * simply remove it from the poll set and free the
2331 /* It's ok to have an unavailable sub-buffer */
2332 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2333 /* Clean up stream from consumer and free it. */
2334 lttng_poll_del(&events
, stream
->wait_fd
);
2335 consumer_del_metadata_stream(stream
, metadata_ht
);
2339 /* Release RCU lock for the stream looked up */
2347 DBG("Metadata poll thread exiting");
2349 lttng_poll_clean(&events
);
2354 ERR("Health error occurred in %s", __func__
);
2356 health_unregister(health_consumerd
);
2357 rcu_unregister_thread();
2362 * This thread polls the fds in the set to consume the data and write
2363 * it to tracefile if necessary.
2365 void *consumer_thread_data_poll(void *data
)
2367 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2368 struct pollfd
*pollfd
= NULL
;
2369 /* local view of the streams */
2370 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2371 /* local view of consumer_data.fds_count */
2373 struct lttng_consumer_local_data
*ctx
= data
;
2376 rcu_register_thread();
2378 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2380 if (testpoint(consumerd_thread_data
)) {
2381 goto error_testpoint
;
2384 health_code_update();
2386 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2387 if (local_stream
== NULL
) {
2388 PERROR("local_stream malloc");
2393 health_code_update();
2399 * the fds set has been updated, we need to update our
2400 * local array as well
2402 pthread_mutex_lock(&consumer_data
.lock
);
2403 if (consumer_data
.need_update
) {
2408 local_stream
= NULL
;
2411 * Allocate for all fds +1 for the consumer_data_pipe and +1 for
2414 pollfd
= zmalloc((consumer_data
.stream_count
+ 2) * sizeof(struct pollfd
));
2415 if (pollfd
== NULL
) {
2416 PERROR("pollfd malloc");
2417 pthread_mutex_unlock(&consumer_data
.lock
);
2421 local_stream
= zmalloc((consumer_data
.stream_count
+ 2) *
2422 sizeof(struct lttng_consumer_stream
*));
2423 if (local_stream
== NULL
) {
2424 PERROR("local_stream malloc");
2425 pthread_mutex_unlock(&consumer_data
.lock
);
2428 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2431 ERR("Error in allocating pollfd or local_outfds");
2432 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2433 pthread_mutex_unlock(&consumer_data
.lock
);
2437 consumer_data
.need_update
= 0;
2439 pthread_mutex_unlock(&consumer_data
.lock
);
2441 /* No FDs and consumer_quit, consumer_cleanup the thread */
2442 if (nb_fd
== 0 && consumer_quit
== 1) {
2443 err
= 0; /* All is OK */
2446 /* poll on the array of fds */
2448 DBG("polling on %d fd", nb_fd
+ 2);
2449 health_poll_entry();
2450 num_rdy
= poll(pollfd
, nb_fd
+ 2, -1);
2452 DBG("poll num_rdy : %d", num_rdy
);
2453 if (num_rdy
== -1) {
2455 * Restart interrupted system call.
2457 if (errno
== EINTR
) {
2460 PERROR("Poll error");
2461 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2463 } else if (num_rdy
== 0) {
2464 DBG("Polling thread timed out");
2469 * If the consumer_data_pipe triggered poll go directly to the
2470 * beginning of the loop to update the array. We want to prioritize
2471 * array update over low-priority reads.
2473 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2474 ssize_t pipe_readlen
;
2476 DBG("consumer_data_pipe wake up");
2477 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2478 &new_stream
, sizeof(new_stream
));
2479 if (pipe_readlen
< sizeof(new_stream
)) {
2480 PERROR("Consumer data pipe");
2481 /* Continue so we can at least handle the current stream(s). */
2486 * If the stream is NULL, just ignore it. It's also possible that
2487 * the sessiond poll thread changed the consumer_quit state and is
2488 * waking us up to test it.
2490 if (new_stream
== NULL
) {
2491 validate_endpoint_status_data_stream();
2495 /* Continue to update the local streams and handle prio ones */
2499 /* Handle wakeup pipe. */
2500 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2502 ssize_t pipe_readlen
;
2504 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2506 if (pipe_readlen
< 0) {
2507 PERROR("Consumer data wakeup pipe");
2509 /* We've been awakened to handle stream(s). */
2510 ctx
->has_wakeup
= 0;
2513 /* Take care of high priority channels first. */
2514 for (i
= 0; i
< nb_fd
; i
++) {
2515 health_code_update();
2517 if (local_stream
[i
] == NULL
) {
2520 if (pollfd
[i
].revents
& POLLPRI
) {
2521 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2523 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2524 /* it's ok to have an unavailable sub-buffer */
2525 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2526 /* Clean the stream and free it. */
2527 consumer_del_stream(local_stream
[i
], data_ht
);
2528 local_stream
[i
] = NULL
;
2529 } else if (len
> 0) {
2530 local_stream
[i
]->data_read
= 1;
2536 * If we read high prio channel in this loop, try again
2537 * for more high prio data.
2543 /* Take care of low priority channels. */
2544 for (i
= 0; i
< nb_fd
; i
++) {
2545 health_code_update();
2547 if (local_stream
[i
] == NULL
) {
2550 if ((pollfd
[i
].revents
& POLLIN
) ||
2551 local_stream
[i
]->hangup_flush_done
||
2552 local_stream
[i
]->has_data
) {
2553 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2554 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2555 /* it's ok to have an unavailable sub-buffer */
2556 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2557 /* Clean the stream and free it. */
2558 consumer_del_stream(local_stream
[i
], data_ht
);
2559 local_stream
[i
] = NULL
;
2560 } else if (len
> 0) {
2561 local_stream
[i
]->data_read
= 1;
2566 /* Handle hangup and errors */
2567 for (i
= 0; i
< nb_fd
; i
++) {
2568 health_code_update();
2570 if (local_stream
[i
] == NULL
) {
2573 if (!local_stream
[i
]->hangup_flush_done
2574 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2575 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2576 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2577 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2579 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2580 /* Attempt read again, for the data we just flushed. */
2581 local_stream
[i
]->data_read
= 1;
2584 * If the poll flag is HUP/ERR/NVAL and we have
2585 * read no data in this pass, we can remove the
2586 * stream from its hash table.
2588 if ((pollfd
[i
].revents
& POLLHUP
)) {
2589 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2590 if (!local_stream
[i
]->data_read
) {
2591 consumer_del_stream(local_stream
[i
], data_ht
);
2592 local_stream
[i
] = NULL
;
2595 } else if (pollfd
[i
].revents
& POLLERR
) {
2596 ERR("Error returned in polling fd %d.", 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
& POLLNVAL
) {
2603 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2604 if (!local_stream
[i
]->data_read
) {
2605 consumer_del_stream(local_stream
[i
], data_ht
);
2606 local_stream
[i
] = NULL
;
2610 if (local_stream
[i
] != NULL
) {
2611 local_stream
[i
]->data_read
= 0;
2618 DBG("polling thread exiting");
2623 * Close the write side of the pipe so epoll_wait() in
2624 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2625 * read side of the pipe. If we close them both, epoll_wait strangely does
2626 * not return and could create a endless wait period if the pipe is the
2627 * only tracked fd in the poll set. The thread will take care of closing
2630 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2635 ERR("Health error occurred in %s", __func__
);
2637 health_unregister(health_consumerd
);
2639 rcu_unregister_thread();
2644 * Close wake-up end of each stream belonging to the channel. This will
2645 * allow the poll() on the stream read-side to detect when the
2646 * write-side (application) finally closes them.
2649 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2651 struct lttng_ht
*ht
;
2652 struct lttng_consumer_stream
*stream
;
2653 struct lttng_ht_iter iter
;
2655 ht
= consumer_data
.stream_per_chan_id_ht
;
2658 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2659 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2660 ht
->match_fct
, &channel
->key
,
2661 &iter
.iter
, stream
, node_channel_id
.node
) {
2663 * Protect against teardown with mutex.
2665 pthread_mutex_lock(&stream
->lock
);
2666 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2669 switch (consumer_data
.type
) {
2670 case LTTNG_CONSUMER_KERNEL
:
2672 case LTTNG_CONSUMER32_UST
:
2673 case LTTNG_CONSUMER64_UST
:
2674 if (stream
->metadata_flag
) {
2675 /* Safe and protected by the stream lock. */
2676 lttng_ustconsumer_close_metadata(stream
->chan
);
2679 * Note: a mutex is taken internally within
2680 * liblttng-ust-ctl to protect timer wakeup_fd
2681 * use from concurrent close.
2683 lttng_ustconsumer_close_stream_wakeup(stream
);
2687 ERR("Unknown consumer_data type");
2691 pthread_mutex_unlock(&stream
->lock
);
2696 static void destroy_channel_ht(struct lttng_ht
*ht
)
2698 struct lttng_ht_iter iter
;
2699 struct lttng_consumer_channel
*channel
;
2707 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2708 ret
= lttng_ht_del(ht
, &iter
);
2713 lttng_ht_destroy(ht
);
2717 * This thread polls the channel fds to detect when they are being
2718 * closed. It closes all related streams if the channel is detected as
2719 * closed. It is currently only used as a shim layer for UST because the
2720 * consumerd needs to keep the per-stream wakeup end of pipes open for
2723 void *consumer_thread_channel_poll(void *data
)
2725 int ret
, i
, pollfd
, err
= -1;
2726 uint32_t revents
, nb_fd
;
2727 struct lttng_consumer_channel
*chan
= NULL
;
2728 struct lttng_ht_iter iter
;
2729 struct lttng_ht_node_u64
*node
;
2730 struct lttng_poll_event events
;
2731 struct lttng_consumer_local_data
*ctx
= data
;
2732 struct lttng_ht
*channel_ht
;
2734 rcu_register_thread();
2736 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2738 if (testpoint(consumerd_thread_channel
)) {
2739 goto error_testpoint
;
2742 health_code_update();
2744 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2746 /* ENOMEM at this point. Better to bail out. */
2750 DBG("Thread channel poll started");
2752 /* Size is set to 1 for the consumer_channel pipe */
2753 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2755 ERR("Poll set creation failed");
2759 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2765 DBG("Channel main loop started");
2769 health_code_update();
2770 DBG("Channel poll wait");
2771 health_poll_entry();
2772 ret
= lttng_poll_wait(&events
, -1);
2773 DBG("Channel poll return from wait with %d fd(s)",
2774 LTTNG_POLL_GETNB(&events
));
2776 DBG("Channel event catched in thread");
2778 if (errno
== EINTR
) {
2779 ERR("Poll EINTR catched");
2782 if (LTTNG_POLL_GETNB(&events
) == 0) {
2783 err
= 0; /* All is OK */
2790 /* From here, the event is a channel wait fd */
2791 for (i
= 0; i
< nb_fd
; i
++) {
2792 health_code_update();
2794 revents
= LTTNG_POLL_GETEV(&events
, i
);
2795 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2798 /* No activity for this FD (poll implementation). */
2802 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2803 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2804 DBG("Channel thread pipe hung up");
2806 * Remove the pipe from the poll set and continue the loop
2807 * since their might be data to consume.
2809 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2811 } else if (revents
& LPOLLIN
) {
2812 enum consumer_channel_action action
;
2815 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2817 ERR("Error reading channel pipe");
2822 case CONSUMER_CHANNEL_ADD
:
2823 DBG("Adding channel %d to poll set",
2826 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2829 lttng_ht_add_unique_u64(channel_ht
,
2830 &chan
->wait_fd_node
);
2832 /* Add channel to the global poll events list */
2833 lttng_poll_add(&events
, chan
->wait_fd
,
2834 LPOLLIN
| LPOLLPRI
);
2836 case CONSUMER_CHANNEL_DEL
:
2839 * This command should never be called if the channel
2840 * has streams monitored by either the data or metadata
2841 * thread. The consumer only notify this thread with a
2842 * channel del. command if it receives a destroy
2843 * channel command from the session daemon that send it
2844 * if a command prior to the GET_CHANNEL failed.
2848 chan
= consumer_find_channel(key
);
2851 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2854 lttng_poll_del(&events
, chan
->wait_fd
);
2855 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2856 ret
= lttng_ht_del(channel_ht
, &iter
);
2859 switch (consumer_data
.type
) {
2860 case LTTNG_CONSUMER_KERNEL
:
2862 case LTTNG_CONSUMER32_UST
:
2863 case LTTNG_CONSUMER64_UST
:
2864 health_code_update();
2865 /* Destroy streams that might have been left in the stream list. */
2866 clean_channel_stream_list(chan
);
2869 ERR("Unknown consumer_data type");
2874 * Release our own refcount. Force channel deletion even if
2875 * streams were not initialized.
2877 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2878 consumer_del_channel(chan
);
2883 case CONSUMER_CHANNEL_QUIT
:
2885 * Remove the pipe from the poll set and continue the loop
2886 * since their might be data to consume.
2888 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2891 ERR("Unknown action");
2896 /* Handle other stream */
2902 uint64_t tmp_id
= (uint64_t) pollfd
;
2904 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2906 node
= lttng_ht_iter_get_node_u64(&iter
);
2909 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2912 /* Check for error event */
2913 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2914 DBG("Channel fd %d is hup|err.", pollfd
);
2916 lttng_poll_del(&events
, chan
->wait_fd
);
2917 ret
= lttng_ht_del(channel_ht
, &iter
);
2921 * This will close the wait fd for each stream associated to
2922 * this channel AND monitored by the data/metadata thread thus
2923 * will be clean by the right thread.
2925 consumer_close_channel_streams(chan
);
2927 /* Release our own refcount */
2928 if (!uatomic_sub_return(&chan
->refcount
, 1)
2929 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2930 consumer_del_channel(chan
);
2934 /* Release RCU lock for the channel looked up */
2942 lttng_poll_clean(&events
);
2944 destroy_channel_ht(channel_ht
);
2947 DBG("Channel poll thread exiting");
2950 ERR("Health error occurred in %s", __func__
);
2952 health_unregister(health_consumerd
);
2953 rcu_unregister_thread();
2957 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2958 struct pollfd
*sockpoll
, int client_socket
)
2965 ret
= lttng_consumer_poll_socket(sockpoll
);
2969 DBG("Metadata connection on client_socket");
2971 /* Blocking call, waiting for transmission */
2972 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2973 if (ctx
->consumer_metadata_socket
< 0) {
2974 WARN("On accept metadata");
2985 * This thread listens on the consumerd socket and receives the file
2986 * descriptors from the session daemon.
2988 void *consumer_thread_sessiond_poll(void *data
)
2990 int sock
= -1, client_socket
, ret
, err
= -1;
2992 * structure to poll for incoming data on communication socket avoids
2993 * making blocking sockets.
2995 struct pollfd consumer_sockpoll
[2];
2996 struct lttng_consumer_local_data
*ctx
= data
;
2998 rcu_register_thread();
3000 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3002 if (testpoint(consumerd_thread_sessiond
)) {
3003 goto error_testpoint
;
3006 health_code_update();
3008 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3009 unlink(ctx
->consumer_command_sock_path
);
3010 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3011 if (client_socket
< 0) {
3012 ERR("Cannot create command socket");
3016 ret
= lttcomm_listen_unix_sock(client_socket
);
3021 DBG("Sending ready command to lttng-sessiond");
3022 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3023 /* return < 0 on error, but == 0 is not fatal */
3025 ERR("Error sending ready command to lttng-sessiond");
3029 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3030 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3031 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3032 consumer_sockpoll
[1].fd
= client_socket
;
3033 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3035 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3043 DBG("Connection on client_socket");
3045 /* Blocking call, waiting for transmission */
3046 sock
= lttcomm_accept_unix_sock(client_socket
);
3053 * Setup metadata socket which is the second socket connection on the
3054 * command unix socket.
3056 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3065 /* This socket is not useful anymore. */
3066 ret
= close(client_socket
);
3068 PERROR("close client_socket");
3072 /* update the polling structure to poll on the established socket */
3073 consumer_sockpoll
[1].fd
= sock
;
3074 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3077 health_code_update();
3079 health_poll_entry();
3080 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3089 DBG("Incoming command on sock");
3090 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3093 * This could simply be a session daemon quitting. Don't output
3096 DBG("Communication interrupted on command socket");
3100 if (consumer_quit
) {
3101 DBG("consumer_thread_receive_fds received quit from signal");
3102 err
= 0; /* All is OK */
3105 DBG("received command on sock");
3111 DBG("Consumer thread sessiond poll exiting");
3114 * Close metadata streams since the producer is the session daemon which
3117 * NOTE: for now, this only applies to the UST tracer.
3119 lttng_consumer_close_all_metadata();
3122 * when all fds have hung up, the polling thread
3128 * Notify the data poll thread to poll back again and test the
3129 * consumer_quit state that we just set so to quit gracefully.
3131 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3133 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3135 notify_health_quit_pipe(health_quit_pipe
);
3137 /* Cleaning up possibly open sockets. */
3141 PERROR("close sock sessiond poll");
3144 if (client_socket
>= 0) {
3145 ret
= close(client_socket
);
3147 PERROR("close client_socket sessiond poll");
3154 ERR("Health error occurred in %s", __func__
);
3156 health_unregister(health_consumerd
);
3158 rcu_unregister_thread();
3162 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3163 struct lttng_consumer_local_data
*ctx
)
3167 pthread_mutex_lock(&stream
->lock
);
3168 if (stream
->metadata_flag
) {
3169 pthread_mutex_lock(&stream
->metadata_rdv_lock
);
3172 switch (consumer_data
.type
) {
3173 case LTTNG_CONSUMER_KERNEL
:
3174 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3176 case LTTNG_CONSUMER32_UST
:
3177 case LTTNG_CONSUMER64_UST
:
3178 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3181 ERR("Unknown consumer_data type");
3187 if (stream
->metadata_flag
) {
3188 pthread_cond_broadcast(&stream
->metadata_rdv
);
3189 pthread_mutex_unlock(&stream
->metadata_rdv_lock
);
3191 pthread_mutex_unlock(&stream
->lock
);
3195 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3197 switch (consumer_data
.type
) {
3198 case LTTNG_CONSUMER_KERNEL
:
3199 return lttng_kconsumer_on_recv_stream(stream
);
3200 case LTTNG_CONSUMER32_UST
:
3201 case LTTNG_CONSUMER64_UST
:
3202 return lttng_ustconsumer_on_recv_stream(stream
);
3204 ERR("Unknown consumer_data type");
3211 * Allocate and set consumer data hash tables.
3213 int lttng_consumer_init(void)
3215 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3216 if (!consumer_data
.channel_ht
) {
3220 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3221 if (!consumer_data
.relayd_ht
) {
3225 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3226 if (!consumer_data
.stream_list_ht
) {
3230 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3231 if (!consumer_data
.stream_per_chan_id_ht
) {
3235 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3240 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3252 * Process the ADD_RELAYD command receive by a consumer.
3254 * This will create a relayd socket pair and add it to the relayd hash table.
3255 * The caller MUST acquire a RCU read side lock before calling it.
3257 int consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3258 struct lttng_consumer_local_data
*ctx
, int sock
,
3259 struct pollfd
*consumer_sockpoll
,
3260 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3261 uint64_t relayd_session_id
)
3263 int fd
= -1, ret
= -1, relayd_created
= 0;
3264 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3265 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3268 assert(relayd_sock
);
3270 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3272 /* Get relayd reference if exists. */
3273 relayd
= consumer_find_relayd(net_seq_idx
);
3274 if (relayd
== NULL
) {
3275 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3276 /* Not found. Allocate one. */
3277 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3278 if (relayd
== NULL
) {
3280 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3283 relayd
->sessiond_session_id
= sessiond_id
;
3288 * This code path MUST continue to the consumer send status message to
3289 * we can notify the session daemon and continue our work without
3290 * killing everything.
3294 * relayd key should never be found for control socket.
3296 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3299 /* First send a status message before receiving the fds. */
3300 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3302 /* Somehow, the session daemon is not responding anymore. */
3303 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3304 goto error_nosignal
;
3307 /* Poll on consumer socket. */
3308 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3310 /* Needing to exit in the middle of a command: error. */
3311 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3313 goto error_nosignal
;
3316 /* Get relayd socket from session daemon */
3317 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3318 if (ret
!= sizeof(fd
)) {
3320 fd
= -1; /* Just in case it gets set with an invalid value. */
3323 * Failing to receive FDs might indicate a major problem such as
3324 * reaching a fd limit during the receive where the kernel returns a
3325 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3326 * don't take any chances and stop everything.
3328 * XXX: Feature request #558 will fix that and avoid this possible
3329 * issue when reaching the fd limit.
3331 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3332 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3336 /* Copy socket information and received FD */
3337 switch (sock_type
) {
3338 case LTTNG_STREAM_CONTROL
:
3339 /* Copy received lttcomm socket */
3340 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3341 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3342 /* Handle create_sock error. */
3344 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3348 * Close the socket created internally by
3349 * lttcomm_create_sock, so we can replace it by the one
3350 * received from sessiond.
3352 if (close(relayd
->control_sock
.sock
.fd
)) {
3356 /* Assign new file descriptor */
3357 relayd
->control_sock
.sock
.fd
= fd
;
3358 fd
= -1; /* For error path */
3359 /* Assign version values. */
3360 relayd
->control_sock
.major
= relayd_sock
->major
;
3361 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3363 relayd
->relayd_session_id
= relayd_session_id
;
3366 case LTTNG_STREAM_DATA
:
3367 /* Copy received lttcomm socket */
3368 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3369 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3370 /* Handle create_sock error. */
3372 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3376 * Close the socket created internally by
3377 * lttcomm_create_sock, so we can replace it by the one
3378 * received from sessiond.
3380 if (close(relayd
->data_sock
.sock
.fd
)) {
3384 /* Assign new file descriptor */
3385 relayd
->data_sock
.sock
.fd
= fd
;
3386 fd
= -1; /* for eventual error paths */
3387 /* Assign version values. */
3388 relayd
->data_sock
.major
= relayd_sock
->major
;
3389 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3392 ERR("Unknown relayd socket type (%d)", sock_type
);
3394 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3398 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3399 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3400 relayd
->net_seq_idx
, fd
);
3402 /* We successfully added the socket. Send status back. */
3403 ret
= consumer_send_status_msg(sock
, ret_code
);
3405 /* Somehow, the session daemon is not responding anymore. */
3406 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3407 goto error_nosignal
;
3411 * Add relayd socket pair to consumer data hashtable. If object already
3412 * exists or on error, the function gracefully returns.
3420 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3421 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3425 /* Close received socket if valid. */
3428 PERROR("close received socket");
3432 if (relayd_created
) {
3440 * Try to lock the stream mutex.
3442 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3444 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3451 * Try to lock the stream mutex. On failure, we know that the stream is
3452 * being used else where hence there is data still being extracted.
3454 ret
= pthread_mutex_trylock(&stream
->lock
);
3456 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3468 * Search for a relayd associated to the session id and return the reference.
3470 * A rcu read side lock MUST be acquire before calling this function and locked
3471 * until the relayd object is no longer necessary.
3473 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3475 struct lttng_ht_iter iter
;
3476 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3478 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3479 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3482 * Check by sessiond id which is unique here where the relayd session
3483 * id might not be when having multiple relayd.
3485 if (relayd
->sessiond_session_id
== id
) {
3486 /* Found the relayd. There can be only one per id. */
3498 * Check if for a given session id there is still data needed to be extract
3501 * Return 1 if data is pending or else 0 meaning ready to be read.
3503 int consumer_data_pending(uint64_t id
)
3506 struct lttng_ht_iter iter
;
3507 struct lttng_ht
*ht
;
3508 struct lttng_consumer_stream
*stream
;
3509 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3510 int (*data_pending
)(struct lttng_consumer_stream
*);
3512 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3515 pthread_mutex_lock(&consumer_data
.lock
);
3517 switch (consumer_data
.type
) {
3518 case LTTNG_CONSUMER_KERNEL
:
3519 data_pending
= lttng_kconsumer_data_pending
;
3521 case LTTNG_CONSUMER32_UST
:
3522 case LTTNG_CONSUMER64_UST
:
3523 data_pending
= lttng_ustconsumer_data_pending
;
3526 ERR("Unknown consumer data type");
3530 /* Ease our life a bit */
3531 ht
= consumer_data
.stream_list_ht
;
3533 relayd
= find_relayd_by_session_id(id
);
3535 /* Send init command for data pending. */
3536 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3537 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3538 relayd
->relayd_session_id
);
3539 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3541 /* Communication error thus the relayd so no data pending. */
3542 goto data_not_pending
;
3546 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3547 ht
->hash_fct(&id
, lttng_ht_seed
),
3549 &iter
.iter
, stream
, node_session_id
.node
) {
3550 /* If this call fails, the stream is being used hence data pending. */
3551 ret
= stream_try_lock(stream
);
3557 * A removed node from the hash table indicates that the stream has
3558 * been deleted thus having a guarantee that the buffers are closed
3559 * on the consumer side. However, data can still be transmitted
3560 * over the network so don't skip the relayd check.
3562 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3564 /* Check the stream if there is data in the buffers. */
3565 ret
= data_pending(stream
);
3567 pthread_mutex_unlock(&stream
->lock
);
3574 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3575 if (stream
->metadata_flag
) {
3576 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3577 stream
->relayd_stream_id
);
3579 ret
= relayd_data_pending(&relayd
->control_sock
,
3580 stream
->relayd_stream_id
,
3581 stream
->next_net_seq_num
- 1);
3583 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3585 pthread_mutex_unlock(&stream
->lock
);
3589 pthread_mutex_unlock(&stream
->lock
);
3593 unsigned int is_data_inflight
= 0;
3595 /* Send init command for data pending. */
3596 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3597 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3598 relayd
->relayd_session_id
, &is_data_inflight
);
3599 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3601 goto data_not_pending
;
3603 if (is_data_inflight
) {
3609 * Finding _no_ node in the hash table and no inflight data means that the
3610 * stream(s) have been removed thus data is guaranteed to be available for
3611 * analysis from the trace files.
3615 /* Data is available to be read by a viewer. */
3616 pthread_mutex_unlock(&consumer_data
.lock
);
3621 /* Data is still being extracted from buffers. */
3622 pthread_mutex_unlock(&consumer_data
.lock
);
3628 * Send a ret code status message to the sessiond daemon.
3630 * Return the sendmsg() return value.
3632 int consumer_send_status_msg(int sock
, int ret_code
)
3634 struct lttcomm_consumer_status_msg msg
;
3636 memset(&msg
, 0, sizeof(msg
));
3637 msg
.ret_code
= ret_code
;
3639 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3643 * Send a channel status message to the sessiond daemon.
3645 * Return the sendmsg() return value.
3647 int consumer_send_status_channel(int sock
,
3648 struct lttng_consumer_channel
*channel
)
3650 struct lttcomm_consumer_status_channel msg
;
3654 memset(&msg
, 0, sizeof(msg
));
3656 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3658 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3659 msg
.key
= channel
->key
;
3660 msg
.stream_count
= channel
->streams
.count
;
3663 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3666 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3667 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3668 uint64_t max_sb_size
)
3670 unsigned long start_pos
;
3672 if (!nb_packets_per_stream
) {
3673 return consumed_pos
; /* Grab everything */
3675 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
3676 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3677 if ((long) (start_pos
- consumed_pos
) < 0) {
3678 return consumed_pos
; /* Grab everything */