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"
52 struct lttng_consumer_global_data consumer_data
= {
55 .type
= LTTNG_CONSUMER_UNKNOWN
,
58 enum consumer_channel_action
{
61 CONSUMER_CHANNEL_QUIT
,
64 struct consumer_channel_msg
{
65 enum consumer_channel_action action
;
66 struct lttng_consumer_channel
*chan
; /* add */
67 uint64_t key
; /* del */
71 * Flag to inform the polling thread to quit when all fd hung up. Updated by
72 * the consumer_thread_receive_fds when it notices that all fds has hung up.
73 * Also updated by the signal handler (consumer_should_exit()). Read by the
76 volatile int consumer_quit
;
79 * Global hash table containing respectively metadata and data streams. The
80 * stream element in this ht should only be updated by the metadata poll thread
81 * for the metadata and the data poll thread for the data.
83 static struct lttng_ht
*metadata_ht
;
84 static struct lttng_ht
*data_ht
;
87 * Notify a thread lttng pipe to poll back again. This usually means that some
88 * global state has changed so we just send back the thread in a poll wait
91 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
93 struct lttng_consumer_stream
*null_stream
= NULL
;
97 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
100 static void notify_health_quit_pipe(int *pipe
)
104 ret
= lttng_write(pipe
[1], "4", 1);
106 PERROR("write consumer health quit");
110 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
111 struct lttng_consumer_channel
*chan
,
113 enum consumer_channel_action action
)
115 struct consumer_channel_msg msg
;
118 memset(&msg
, 0, sizeof(msg
));
123 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
124 if (ret
< sizeof(msg
)) {
125 PERROR("notify_channel_pipe write error");
129 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
132 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
135 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
136 struct lttng_consumer_channel
**chan
,
138 enum consumer_channel_action
*action
)
140 struct consumer_channel_msg msg
;
143 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
144 if (ret
< sizeof(msg
)) {
148 *action
= msg
.action
;
156 * Cleanup the stream list of a channel. Those streams are not yet globally
159 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
161 struct lttng_consumer_stream
*stream
, *stmp
;
165 /* Delete streams that might have been left in the stream list. */
166 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
168 cds_list_del(&stream
->send_node
);
170 * Once a stream is added to this list, the buffers were created so we
171 * have a guarantee that this call will succeed. Setting the monitor
172 * mode to 0 so we don't lock nor try to delete the stream from the
176 consumer_stream_destroy(stream
, NULL
);
181 * Find a stream. The consumer_data.lock must be locked during this
184 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
187 struct lttng_ht_iter iter
;
188 struct lttng_ht_node_u64
*node
;
189 struct lttng_consumer_stream
*stream
= NULL
;
193 /* -1ULL keys are lookup failures */
194 if (key
== (uint64_t) -1ULL) {
200 lttng_ht_lookup(ht
, &key
, &iter
);
201 node
= lttng_ht_iter_get_node_u64(&iter
);
203 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
211 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
213 struct lttng_consumer_stream
*stream
;
216 stream
= find_stream(key
, ht
);
218 stream
->key
= (uint64_t) -1ULL;
220 * We don't want the lookup to match, but we still need
221 * to iterate on this stream when iterating over the hash table. Just
222 * change the node key.
224 stream
->node
.key
= (uint64_t) -1ULL;
230 * Return a channel object for the given key.
232 * RCU read side lock MUST be acquired before calling this function and
233 * protects the channel ptr.
235 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
237 struct lttng_ht_iter iter
;
238 struct lttng_ht_node_u64
*node
;
239 struct lttng_consumer_channel
*channel
= NULL
;
241 /* -1ULL keys are lookup failures */
242 if (key
== (uint64_t) -1ULL) {
246 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
247 node
= lttng_ht_iter_get_node_u64(&iter
);
249 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
256 * There is a possibility that the consumer does not have enough time between
257 * the close of the channel on the session daemon and the cleanup in here thus
258 * once we have a channel add with an existing key, we know for sure that this
259 * channel will eventually get cleaned up by all streams being closed.
261 * This function just nullifies the already existing channel key.
263 static void steal_channel_key(uint64_t key
)
265 struct lttng_consumer_channel
*channel
;
268 channel
= consumer_find_channel(key
);
270 channel
->key
= (uint64_t) -1ULL;
272 * We don't want the lookup to match, but we still need to iterate on
273 * this channel when iterating over the hash table. Just change the
276 channel
->node
.key
= (uint64_t) -1ULL;
281 static void free_channel_rcu(struct rcu_head
*head
)
283 struct lttng_ht_node_u64
*node
=
284 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
285 struct lttng_consumer_channel
*channel
=
286 caa_container_of(node
, struct lttng_consumer_channel
, node
);
292 * RCU protected relayd socket pair free.
294 static void free_relayd_rcu(struct rcu_head
*head
)
296 struct lttng_ht_node_u64
*node
=
297 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
298 struct consumer_relayd_sock_pair
*relayd
=
299 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
302 * Close all sockets. This is done in the call RCU since we don't want the
303 * socket fds to be reassigned thus potentially creating bad state of the
306 * We do not have to lock the control socket mutex here since at this stage
307 * there is no one referencing to this relayd object.
309 (void) relayd_close(&relayd
->control_sock
);
310 (void) relayd_close(&relayd
->data_sock
);
316 * Destroy and free relayd socket pair object.
318 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
321 struct lttng_ht_iter iter
;
323 if (relayd
== NULL
) {
327 DBG("Consumer destroy and close relayd socket pair");
329 iter
.iter
.node
= &relayd
->node
.node
;
330 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
332 /* We assume the relayd is being or is destroyed */
336 /* RCU free() call */
337 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
341 * Remove a channel from the global list protected by a mutex. This function is
342 * also responsible for freeing its data structures.
344 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
347 struct lttng_ht_iter iter
;
349 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
351 pthread_mutex_lock(&consumer_data
.lock
);
352 pthread_mutex_lock(&channel
->lock
);
354 /* Destroy streams that might have been left in the stream list. */
355 clean_channel_stream_list(channel
);
357 if (channel
->live_timer_enabled
== 1) {
358 consumer_timer_live_stop(channel
);
361 switch (consumer_data
.type
) {
362 case LTTNG_CONSUMER_KERNEL
:
364 case LTTNG_CONSUMER32_UST
:
365 case LTTNG_CONSUMER64_UST
:
366 lttng_ustconsumer_del_channel(channel
);
369 ERR("Unknown consumer_data type");
375 iter
.iter
.node
= &channel
->node
.node
;
376 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
380 call_rcu(&channel
->node
.head
, free_channel_rcu
);
382 pthread_mutex_unlock(&channel
->lock
);
383 pthread_mutex_unlock(&consumer_data
.lock
);
387 * Iterate over the relayd hash table and destroy each element. Finally,
388 * destroy the whole hash table.
390 static void cleanup_relayd_ht(void)
392 struct lttng_ht_iter iter
;
393 struct consumer_relayd_sock_pair
*relayd
;
397 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
399 consumer_destroy_relayd(relayd
);
404 lttng_ht_destroy(consumer_data
.relayd_ht
);
408 * Update the end point status of all streams having the given network sequence
409 * index (relayd index).
411 * It's atomically set without having the stream mutex locked which is fine
412 * because we handle the write/read race with a pipe wakeup for each thread.
414 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
415 enum consumer_endpoint_status status
)
417 struct lttng_ht_iter iter
;
418 struct lttng_consumer_stream
*stream
;
420 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
424 /* Let's begin with metadata */
425 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
426 if (stream
->net_seq_idx
== net_seq_idx
) {
427 uatomic_set(&stream
->endpoint_status
, status
);
428 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
432 /* Follow up by the data streams */
433 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
434 if (stream
->net_seq_idx
== net_seq_idx
) {
435 uatomic_set(&stream
->endpoint_status
, status
);
436 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
443 * Cleanup a relayd object by flagging every associated streams for deletion,
444 * destroying the object meaning removing it from the relayd hash table,
445 * closing the sockets and freeing the memory in a RCU call.
447 * If a local data context is available, notify the threads that the streams'
448 * state have changed.
450 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
451 struct lttng_consumer_local_data
*ctx
)
457 DBG("Cleaning up relayd sockets");
459 /* Save the net sequence index before destroying the object */
460 netidx
= relayd
->net_seq_idx
;
463 * Delete the relayd from the relayd hash table, close the sockets and free
464 * the object in a RCU call.
466 consumer_destroy_relayd(relayd
);
468 /* Set inactive endpoint to all streams */
469 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
472 * With a local data context, notify the threads that the streams' state
473 * have changed. The write() action on the pipe acts as an "implicit"
474 * memory barrier ordering the updates of the end point status from the
475 * read of this status which happens AFTER receiving this notify.
478 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
479 notify_thread_lttng_pipe(ctx
->consumer_metadata_pipe
);
484 * Flag a relayd socket pair for destruction. Destroy it if the refcount
487 * RCU read side lock MUST be aquired before calling this function.
489 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
493 /* Set destroy flag for this object */
494 uatomic_set(&relayd
->destroy_flag
, 1);
496 /* Destroy the relayd if refcount is 0 */
497 if (uatomic_read(&relayd
->refcount
) == 0) {
498 consumer_destroy_relayd(relayd
);
503 * Completly destroy stream from every visiable data structure and the given
506 * One this call returns, the stream object is not longer usable nor visible.
508 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
511 consumer_stream_destroy(stream
, ht
);
515 * XXX naming of del vs destroy is all mixed up.
517 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
519 consumer_stream_destroy(stream
, data_ht
);
522 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
524 consumer_stream_destroy(stream
, metadata_ht
);
527 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
529 enum lttng_consumer_stream_state state
,
530 const char *channel_name
,
537 enum consumer_channel_type type
,
538 unsigned int monitor
)
541 struct lttng_consumer_stream
*stream
;
543 stream
= zmalloc(sizeof(*stream
));
544 if (stream
== NULL
) {
545 PERROR("malloc struct lttng_consumer_stream");
552 stream
->key
= stream_key
;
554 stream
->out_fd_offset
= 0;
555 stream
->output_written
= 0;
556 stream
->state
= state
;
559 stream
->net_seq_idx
= relayd_id
;
560 stream
->session_id
= session_id
;
561 stream
->monitor
= monitor
;
562 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
563 stream
->index_fd
= -1;
564 pthread_mutex_init(&stream
->lock
, NULL
);
565 pthread_mutex_init(&stream
->metadata_timer_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
)
941 struct lttng_consumer_channel
*channel
;
943 channel
= zmalloc(sizeof(*channel
));
944 if (channel
== NULL
) {
945 PERROR("malloc struct lttng_consumer_channel");
950 channel
->refcount
= 0;
951 channel
->session_id
= session_id
;
952 channel
->session_id_per_pid
= session_id_per_pid
;
955 channel
->relayd_id
= relayd_id
;
956 channel
->tracefile_size
= tracefile_size
;
957 channel
->tracefile_count
= tracefile_count
;
958 channel
->monitor
= monitor
;
959 channel
->live_timer_interval
= live_timer_interval
;
960 pthread_mutex_init(&channel
->lock
, NULL
);
961 pthread_mutex_init(&channel
->timer_lock
, NULL
);
964 case LTTNG_EVENT_SPLICE
:
965 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
967 case LTTNG_EVENT_MMAP
:
968 channel
->output
= CONSUMER_CHANNEL_MMAP
;
978 * In monitor mode, the streams associated with the channel will be put in
979 * a special list ONLY owned by this channel. So, the refcount is set to 1
980 * here meaning that the channel itself has streams that are referenced.
982 * On a channel deletion, once the channel is no longer visible, the
983 * refcount is decremented and checked for a zero value to delete it. With
984 * streams in no monitor mode, it will now be safe to destroy the channel.
986 if (!channel
->monitor
) {
987 channel
->refcount
= 1;
990 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
991 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
993 strncpy(channel
->name
, name
, sizeof(channel
->name
));
994 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
996 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
998 channel
->wait_fd
= -1;
1000 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1002 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
1009 * Add a channel to the global list protected by a mutex.
1011 * Always return 0 indicating success.
1013 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1014 struct lttng_consumer_local_data
*ctx
)
1016 pthread_mutex_lock(&consumer_data
.lock
);
1017 pthread_mutex_lock(&channel
->lock
);
1018 pthread_mutex_lock(&channel
->timer_lock
);
1021 * This gives us a guarantee that the channel we are about to add to the
1022 * channel hash table will be unique. See this function comment on the why
1023 * we need to steel the channel key at this stage.
1025 steal_channel_key(channel
->key
);
1028 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1031 pthread_mutex_unlock(&channel
->timer_lock
);
1032 pthread_mutex_unlock(&channel
->lock
);
1033 pthread_mutex_unlock(&consumer_data
.lock
);
1035 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1036 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1043 * Allocate the pollfd structure and the local view of the out fds to avoid
1044 * doing a lookup in the linked list and concurrency issues when writing is
1045 * needed. Called with consumer_data.lock held.
1047 * Returns the number of fds in the structures.
1049 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1050 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1051 struct lttng_ht
*ht
)
1054 struct lttng_ht_iter iter
;
1055 struct lttng_consumer_stream
*stream
;
1060 assert(local_stream
);
1062 DBG("Updating poll fd array");
1064 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1066 * Only active streams with an active end point can be added to the
1067 * poll set and local stream storage of the thread.
1069 * There is a potential race here for endpoint_status to be updated
1070 * just after the check. However, this is OK since the stream(s) will
1071 * be deleted once the thread is notified that the end point state has
1072 * changed where this function will be called back again.
1074 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
1075 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1079 * This clobbers way too much the debug output. Uncomment that if you
1080 * need it for debugging purposes.
1082 * DBG("Active FD %d", stream->wait_fd);
1084 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1085 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1086 local_stream
[i
] = stream
;
1092 * Insert the consumer_data_pipe at the end of the array and don't
1093 * increment i so nb_fd is the number of real FD.
1095 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1096 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1098 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1099 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1104 * Poll on the should_quit pipe and the command socket return -1 on
1105 * error, 1 if should exit, 0 if data is available on the command socket
1107 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1112 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1113 if (num_rdy
== -1) {
1115 * Restart interrupted system call.
1117 if (errno
== EINTR
) {
1120 PERROR("Poll error");
1123 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1124 DBG("consumer_should_quit wake up");
1131 * Set the error socket.
1133 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1136 ctx
->consumer_error_socket
= sock
;
1140 * Set the command socket path.
1142 void lttng_consumer_set_command_sock_path(
1143 struct lttng_consumer_local_data
*ctx
, char *sock
)
1145 ctx
->consumer_command_sock_path
= sock
;
1149 * Send return code to the session daemon.
1150 * If the socket is not defined, we return 0, it is not a fatal error
1152 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1154 if (ctx
->consumer_error_socket
> 0) {
1155 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1156 sizeof(enum lttcomm_sessiond_command
));
1163 * Close all the tracefiles and stream fds and MUST be called when all
1164 * instances are destroyed i.e. when all threads were joined and are ended.
1166 void lttng_consumer_cleanup(void)
1168 struct lttng_ht_iter iter
;
1169 struct lttng_consumer_channel
*channel
;
1173 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1175 consumer_del_channel(channel
);
1180 lttng_ht_destroy(consumer_data
.channel_ht
);
1182 cleanup_relayd_ht();
1184 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1187 * This HT contains streams that are freed by either the metadata thread or
1188 * the data thread so we do *nothing* on the hash table and simply destroy
1191 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1195 * Called from signal handler.
1197 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1202 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1204 PERROR("write consumer quit");
1207 DBG("Consumer flag that it should quit");
1210 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1213 int outfd
= stream
->out_fd
;
1216 * This does a blocking write-and-wait on any page that belongs to the
1217 * subbuffer prior to the one we just wrote.
1218 * Don't care about error values, as these are just hints and ways to
1219 * limit the amount of page cache used.
1221 if (orig_offset
< stream
->max_sb_size
) {
1224 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1225 stream
->max_sb_size
,
1226 SYNC_FILE_RANGE_WAIT_BEFORE
1227 | SYNC_FILE_RANGE_WRITE
1228 | SYNC_FILE_RANGE_WAIT_AFTER
);
1230 * Give hints to the kernel about how we access the file:
1231 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1234 * We need to call fadvise again after the file grows because the
1235 * kernel does not seem to apply fadvise to non-existing parts of the
1238 * Call fadvise _after_ having waited for the page writeback to
1239 * complete because the dirty page writeback semantic is not well
1240 * defined. So it can be expected to lead to lower throughput in
1243 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1244 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1248 * Initialise the necessary environnement :
1249 * - create a new context
1250 * - create the poll_pipe
1251 * - create the should_quit pipe (for signal handler)
1252 * - create the thread pipe (for splice)
1254 * Takes a function pointer as argument, this function is called when data is
1255 * available on a buffer. This function is responsible to do the
1256 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1257 * buffer configuration and then kernctl_put_next_subbuf at the end.
1259 * Returns a pointer to the new context or NULL on error.
1261 struct lttng_consumer_local_data
*lttng_consumer_create(
1262 enum lttng_consumer_type type
,
1263 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1264 struct lttng_consumer_local_data
*ctx
),
1265 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1266 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1267 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1270 struct lttng_consumer_local_data
*ctx
;
1272 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1273 consumer_data
.type
== type
);
1274 consumer_data
.type
= type
;
1276 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1278 PERROR("allocating context");
1282 ctx
->consumer_error_socket
= -1;
1283 ctx
->consumer_metadata_socket
= -1;
1284 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1285 /* assign the callbacks */
1286 ctx
->on_buffer_ready
= buffer_ready
;
1287 ctx
->on_recv_channel
= recv_channel
;
1288 ctx
->on_recv_stream
= recv_stream
;
1289 ctx
->on_update_stream
= update_stream
;
1291 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1292 if (!ctx
->consumer_data_pipe
) {
1293 goto error_poll_pipe
;
1296 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1297 if (!ctx
->consumer_wakeup_pipe
) {
1298 goto error_wakeup_pipe
;
1301 ret
= pipe(ctx
->consumer_should_quit
);
1303 PERROR("Error creating recv pipe");
1304 goto error_quit_pipe
;
1307 ret
= pipe(ctx
->consumer_channel_pipe
);
1309 PERROR("Error creating channel pipe");
1310 goto error_channel_pipe
;
1313 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1314 if (!ctx
->consumer_metadata_pipe
) {
1315 goto error_metadata_pipe
;
1320 error_metadata_pipe
:
1321 utils_close_pipe(ctx
->consumer_channel_pipe
);
1323 utils_close_pipe(ctx
->consumer_should_quit
);
1325 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1327 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1335 * Iterate over all streams of the hashtable and free them properly.
1337 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1339 struct lttng_ht_iter iter
;
1340 struct lttng_consumer_stream
*stream
;
1347 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1349 * Ignore return value since we are currently cleaning up so any error
1352 (void) consumer_del_stream(stream
, ht
);
1356 lttng_ht_destroy(ht
);
1360 * Iterate over all streams of the metadata hashtable and free them
1363 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1365 struct lttng_ht_iter iter
;
1366 struct lttng_consumer_stream
*stream
;
1373 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1375 * Ignore return value since we are currently cleaning up so any error
1378 (void) consumer_del_metadata_stream(stream
, ht
);
1382 lttng_ht_destroy(ht
);
1386 * Close all fds associated with the instance and free the context.
1388 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1392 DBG("Consumer destroying it. Closing everything.");
1398 destroy_data_stream_ht(data_ht
);
1399 destroy_metadata_stream_ht(metadata_ht
);
1401 ret
= close(ctx
->consumer_error_socket
);
1405 ret
= close(ctx
->consumer_metadata_socket
);
1409 utils_close_pipe(ctx
->consumer_channel_pipe
);
1410 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1411 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1412 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1413 utils_close_pipe(ctx
->consumer_should_quit
);
1415 unlink(ctx
->consumer_command_sock_path
);
1420 * Write the metadata stream id on the specified file descriptor.
1422 static int write_relayd_metadata_id(int fd
,
1423 struct lttng_consumer_stream
*stream
,
1424 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1427 struct lttcomm_relayd_metadata_payload hdr
;
1429 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1430 hdr
.padding_size
= htobe32(padding
);
1431 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1432 if (ret
< sizeof(hdr
)) {
1434 * This error means that the fd's end is closed so ignore the perror
1435 * not to clubber the error output since this can happen in a normal
1438 if (errno
!= EPIPE
) {
1439 PERROR("write metadata stream id");
1441 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1443 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1444 * handle writting the missing part so report that as an error and
1445 * don't lie to the caller.
1450 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1451 stream
->relayd_stream_id
, padding
);
1458 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1459 * core function for writing trace buffers to either the local filesystem or
1462 * It must be called with the stream lock held.
1464 * Careful review MUST be put if any changes occur!
1466 * Returns the number of bytes written
1468 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1469 struct lttng_consumer_local_data
*ctx
,
1470 struct lttng_consumer_stream
*stream
, unsigned long len
,
1471 unsigned long padding
,
1472 struct ctf_packet_index
*index
)
1474 unsigned long mmap_offset
;
1477 off_t orig_offset
= stream
->out_fd_offset
;
1478 /* Default is on the disk */
1479 int outfd
= stream
->out_fd
;
1480 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1481 unsigned int relayd_hang_up
= 0;
1483 /* RCU lock for the relayd pointer */
1486 /* Flag that the current stream if set for network streaming. */
1487 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1488 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1489 if (relayd
== NULL
) {
1495 /* get the offset inside the fd to mmap */
1496 switch (consumer_data
.type
) {
1497 case LTTNG_CONSUMER_KERNEL
:
1498 mmap_base
= stream
->mmap_base
;
1499 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1502 PERROR("tracer ctl get_mmap_read_offset");
1506 case LTTNG_CONSUMER32_UST
:
1507 case LTTNG_CONSUMER64_UST
:
1508 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1510 ERR("read mmap get mmap base for stream %s", stream
->name
);
1514 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1516 PERROR("tracer ctl get_mmap_read_offset");
1522 ERR("Unknown consumer_data type");
1526 /* Handle stream on the relayd if the output is on the network */
1528 unsigned long netlen
= len
;
1531 * Lock the control socket for the complete duration of the function
1532 * since from this point on we will use the socket.
1534 if (stream
->metadata_flag
) {
1535 /* Metadata requires the control socket. */
1536 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1537 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1540 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1545 /* Use the returned socket. */
1548 /* Write metadata stream id before payload */
1549 if (stream
->metadata_flag
) {
1550 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1557 /* No streaming, we have to set the len with the full padding */
1561 * Check if we need to change the tracefile before writing the packet.
1563 if (stream
->chan
->tracefile_size
> 0 &&
1564 (stream
->tracefile_size_current
+ len
) >
1565 stream
->chan
->tracefile_size
) {
1566 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1567 stream
->name
, stream
->chan
->tracefile_size
,
1568 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1569 stream
->out_fd
, &(stream
->tracefile_count_current
),
1572 ERR("Rotating output file");
1575 outfd
= stream
->out_fd
;
1577 if (stream
->index_fd
>= 0) {
1578 ret
= index_create_file(stream
->chan
->pathname
,
1579 stream
->name
, stream
->uid
, stream
->gid
,
1580 stream
->chan
->tracefile_size
,
1581 stream
->tracefile_count_current
);
1585 stream
->index_fd
= ret
;
1588 /* Reset current size because we just perform a rotation. */
1589 stream
->tracefile_size_current
= 0;
1590 stream
->out_fd_offset
= 0;
1593 stream
->tracefile_size_current
+= len
;
1595 index
->offset
= htobe64(stream
->out_fd_offset
);
1600 * This call guarantee that len or less is returned. It's impossible to
1601 * receive a ret value that is bigger than len.
1603 ret
= lttng_write(outfd
, mmap_base
+ mmap_offset
, len
);
1604 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1605 if (ret
< 0 || ((size_t) ret
!= len
)) {
1607 * Report error to caller if nothing was written else at least send the
1615 /* Socket operation failed. We consider the relayd dead */
1616 if (errno
== EPIPE
|| errno
== EINVAL
|| errno
== EBADF
) {
1618 * This is possible if the fd is closed on the other side
1619 * (outfd) or any write problem. It can be verbose a bit for a
1620 * normal execution if for instance the relayd is stopped
1621 * abruptly. This can happen so set this to a DBG statement.
1623 DBG("Consumer mmap write detected relayd hang up");
1625 /* Unhandled error, print it and stop function right now. */
1626 PERROR("Error in write mmap (ret %zd != len %lu)", ret
, len
);
1630 stream
->output_written
+= ret
;
1632 /* This call is useless on a socket so better save a syscall. */
1634 /* This won't block, but will start writeout asynchronously */
1635 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, len
,
1636 SYNC_FILE_RANGE_WRITE
);
1637 stream
->out_fd_offset
+= len
;
1639 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1643 * This is a special case that the relayd has closed its socket. Let's
1644 * cleanup the relayd object and all associated streams.
1646 if (relayd
&& relayd_hang_up
) {
1647 cleanup_relayd(relayd
, ctx
);
1651 /* Unlock only if ctrl socket used */
1652 if (relayd
&& stream
->metadata_flag
) {
1653 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1661 * Splice the data from the ring buffer to the tracefile.
1663 * It must be called with the stream lock held.
1665 * Returns the number of bytes spliced.
1667 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1668 struct lttng_consumer_local_data
*ctx
,
1669 struct lttng_consumer_stream
*stream
, unsigned long len
,
1670 unsigned long padding
,
1671 struct ctf_packet_index
*index
)
1673 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1675 off_t orig_offset
= stream
->out_fd_offset
;
1676 int fd
= stream
->wait_fd
;
1677 /* Default is on the disk */
1678 int outfd
= stream
->out_fd
;
1679 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1681 unsigned int relayd_hang_up
= 0;
1683 switch (consumer_data
.type
) {
1684 case LTTNG_CONSUMER_KERNEL
:
1686 case LTTNG_CONSUMER32_UST
:
1687 case LTTNG_CONSUMER64_UST
:
1688 /* Not supported for user space tracing */
1691 ERR("Unknown consumer_data type");
1695 /* RCU lock for the relayd pointer */
1698 /* Flag that the current stream if set for network streaming. */
1699 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1700 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1701 if (relayd
== NULL
) {
1706 splice_pipe
= stream
->splice_pipe
;
1708 /* Write metadata stream id before payload */
1710 unsigned long total_len
= len
;
1712 if (stream
->metadata_flag
) {
1714 * Lock the control socket for the complete duration of the function
1715 * since from this point on we will use the socket.
1717 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1719 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1727 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1730 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1736 /* Use the returned socket. */
1739 /* No streaming, we have to set the len with the full padding */
1743 * Check if we need to change the tracefile before writing the packet.
1745 if (stream
->chan
->tracefile_size
> 0 &&
1746 (stream
->tracefile_size_current
+ len
) >
1747 stream
->chan
->tracefile_size
) {
1748 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1749 stream
->name
, stream
->chan
->tracefile_size
,
1750 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1751 stream
->out_fd
, &(stream
->tracefile_count_current
),
1755 ERR("Rotating output file");
1758 outfd
= stream
->out_fd
;
1760 if (stream
->index_fd
>= 0) {
1761 ret
= close(stream
->index_fd
);
1763 PERROR("Closing index");
1766 stream
->index_fd
= -1;
1767 ret
= index_create_file(stream
->chan
->pathname
,
1768 stream
->name
, stream
->uid
, stream
->gid
,
1769 stream
->chan
->tracefile_size
,
1770 stream
->tracefile_count_current
);
1775 stream
->index_fd
= ret
;
1778 /* Reset current size because we just perform a rotation. */
1779 stream
->tracefile_size_current
= 0;
1780 stream
->out_fd_offset
= 0;
1783 stream
->tracefile_size_current
+= len
;
1784 index
->offset
= htobe64(stream
->out_fd_offset
);
1788 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1789 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1790 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1791 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1792 DBG("splice chan to pipe, ret %zd", ret_splice
);
1793 if (ret_splice
< 0) {
1796 PERROR("Error in relay splice");
1800 /* Handle stream on the relayd if the output is on the network */
1801 if (relayd
&& stream
->metadata_flag
) {
1802 size_t metadata_payload_size
=
1803 sizeof(struct lttcomm_relayd_metadata_payload
);
1805 /* Update counter to fit the spliced data */
1806 ret_splice
+= metadata_payload_size
;
1807 len
+= metadata_payload_size
;
1809 * We do this so the return value can match the len passed as
1810 * argument to this function.
1812 written
-= metadata_payload_size
;
1815 /* Splice data out */
1816 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1817 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1818 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1820 if (ret_splice
< 0) {
1825 } else if (ret_splice
> len
) {
1827 * We don't expect this code path to be executed but you never know
1828 * so this is an extra protection agains a buggy splice().
1831 written
+= ret_splice
;
1832 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1836 /* All good, update current len and continue. */
1840 /* This call is useless on a socket so better save a syscall. */
1842 /* This won't block, but will start writeout asynchronously */
1843 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1844 SYNC_FILE_RANGE_WRITE
);
1845 stream
->out_fd_offset
+= ret_splice
;
1847 stream
->output_written
+= ret_splice
;
1848 written
+= ret_splice
;
1850 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1855 * This is a special case that the relayd has closed its socket. Let's
1856 * cleanup the relayd object and all associated streams.
1858 if (relayd
&& relayd_hang_up
) {
1859 cleanup_relayd(relayd
, ctx
);
1860 /* Skip splice error so the consumer does not fail */
1865 /* send the appropriate error description to sessiond */
1868 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1871 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1874 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1879 if (relayd
&& stream
->metadata_flag
) {
1880 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1888 * Take a snapshot for a specific fd
1890 * Returns 0 on success, < 0 on error
1892 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1894 switch (consumer_data
.type
) {
1895 case LTTNG_CONSUMER_KERNEL
:
1896 return lttng_kconsumer_take_snapshot(stream
);
1897 case LTTNG_CONSUMER32_UST
:
1898 case LTTNG_CONSUMER64_UST
:
1899 return lttng_ustconsumer_take_snapshot(stream
);
1901 ERR("Unknown consumer_data type");
1908 * Get the produced position
1910 * Returns 0 on success, < 0 on error
1912 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1915 switch (consumer_data
.type
) {
1916 case LTTNG_CONSUMER_KERNEL
:
1917 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1918 case LTTNG_CONSUMER32_UST
:
1919 case LTTNG_CONSUMER64_UST
:
1920 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1922 ERR("Unknown consumer_data type");
1928 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1929 int sock
, struct pollfd
*consumer_sockpoll
)
1931 switch (consumer_data
.type
) {
1932 case LTTNG_CONSUMER_KERNEL
:
1933 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1934 case LTTNG_CONSUMER32_UST
:
1935 case LTTNG_CONSUMER64_UST
:
1936 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1938 ERR("Unknown consumer_data type");
1944 void lttng_consumer_close_all_metadata(void)
1946 switch (consumer_data
.type
) {
1947 case LTTNG_CONSUMER_KERNEL
:
1949 * The Kernel consumer has a different metadata scheme so we don't
1950 * close anything because the stream will be closed by the session
1954 case LTTNG_CONSUMER32_UST
:
1955 case LTTNG_CONSUMER64_UST
:
1957 * Close all metadata streams. The metadata hash table is passed and
1958 * this call iterates over it by closing all wakeup fd. This is safe
1959 * because at this point we are sure that the metadata producer is
1960 * either dead or blocked.
1962 lttng_ustconsumer_close_all_metadata(metadata_ht
);
1965 ERR("Unknown consumer_data type");
1971 * Clean up a metadata stream and free its memory.
1973 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1974 struct lttng_ht
*ht
)
1976 struct lttng_consumer_channel
*free_chan
= NULL
;
1980 * This call should NEVER receive regular stream. It must always be
1981 * metadata stream and this is crucial for data structure synchronization.
1983 assert(stream
->metadata_flag
);
1985 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1987 pthread_mutex_lock(&consumer_data
.lock
);
1988 pthread_mutex_lock(&stream
->chan
->lock
);
1989 pthread_mutex_lock(&stream
->lock
);
1991 /* Remove any reference to that stream. */
1992 consumer_stream_delete(stream
, ht
);
1994 /* Close down everything including the relayd if one. */
1995 consumer_stream_close(stream
);
1996 /* Destroy tracer buffers of the stream. */
1997 consumer_stream_destroy_buffers(stream
);
1999 /* Atomically decrement channel refcount since other threads can use it. */
2000 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
2001 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
2002 /* Go for channel deletion! */
2003 free_chan
= stream
->chan
;
2007 * Nullify the stream reference so it is not used after deletion. The
2008 * channel lock MUST be acquired before being able to check for a NULL
2011 stream
->chan
->metadata_stream
= NULL
;
2013 pthread_mutex_unlock(&stream
->lock
);
2014 pthread_mutex_unlock(&stream
->chan
->lock
);
2015 pthread_mutex_unlock(&consumer_data
.lock
);
2018 consumer_del_channel(free_chan
);
2021 consumer_stream_free(stream
);
2025 * Action done with the metadata stream when adding it to the consumer internal
2026 * data structures to handle it.
2028 int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2030 struct lttng_ht
*ht
= metadata_ht
;
2032 struct lttng_ht_iter iter
;
2033 struct lttng_ht_node_u64
*node
;
2038 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2040 pthread_mutex_lock(&consumer_data
.lock
);
2041 pthread_mutex_lock(&stream
->chan
->lock
);
2042 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2043 pthread_mutex_lock(&stream
->lock
);
2046 * From here, refcounts are updated so be _careful_ when returning an error
2053 * Lookup the stream just to make sure it does not exist in our internal
2054 * state. This should NEVER happen.
2056 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2057 node
= lttng_ht_iter_get_node_u64(&iter
);
2061 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2062 * in terms of destroying the associated channel, because the action that
2063 * causes the count to become 0 also causes a stream to be added. The
2064 * channel deletion will thus be triggered by the following removal of this
2067 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2068 /* Increment refcount before decrementing nb_init_stream_left */
2070 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2073 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2075 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2076 &stream
->node_channel_id
);
2079 * Add stream to the stream_list_ht of the consumer data. No need to steal
2080 * the key since the HT does not use it and we allow to add redundant keys
2083 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2087 pthread_mutex_unlock(&stream
->lock
);
2088 pthread_mutex_unlock(&stream
->chan
->lock
);
2089 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2090 pthread_mutex_unlock(&consumer_data
.lock
);
2095 * Delete data stream that are flagged for deletion (endpoint_status).
2097 static void validate_endpoint_status_data_stream(void)
2099 struct lttng_ht_iter iter
;
2100 struct lttng_consumer_stream
*stream
;
2102 DBG("Consumer delete flagged data stream");
2105 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2106 /* Validate delete flag of the stream */
2107 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2110 /* Delete it right now */
2111 consumer_del_stream(stream
, data_ht
);
2117 * Delete metadata stream that are flagged for deletion (endpoint_status).
2119 static void validate_endpoint_status_metadata_stream(
2120 struct lttng_poll_event
*pollset
)
2122 struct lttng_ht_iter iter
;
2123 struct lttng_consumer_stream
*stream
;
2125 DBG("Consumer delete flagged metadata stream");
2130 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2131 /* Validate delete flag of the stream */
2132 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2136 * Remove from pollset so the metadata thread can continue without
2137 * blocking on a deleted stream.
2139 lttng_poll_del(pollset
, stream
->wait_fd
);
2141 /* Delete it right now */
2142 consumer_del_metadata_stream(stream
, metadata_ht
);
2148 * Thread polls on metadata file descriptor and write them on disk or on the
2151 void *consumer_thread_metadata_poll(void *data
)
2153 int ret
, i
, pollfd
, err
= -1;
2154 uint32_t revents
, nb_fd
;
2155 struct lttng_consumer_stream
*stream
= NULL
;
2156 struct lttng_ht_iter iter
;
2157 struct lttng_ht_node_u64
*node
;
2158 struct lttng_poll_event events
;
2159 struct lttng_consumer_local_data
*ctx
= data
;
2162 rcu_register_thread();
2164 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2166 if (testpoint(consumerd_thread_metadata
)) {
2167 goto error_testpoint
;
2170 health_code_update();
2172 DBG("Thread metadata poll started");
2174 /* Size is set to 1 for the consumer_metadata pipe */
2175 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2177 ERR("Poll set creation failed");
2181 ret
= lttng_poll_add(&events
,
2182 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2188 DBG("Metadata main loop started");
2192 health_code_update();
2193 health_poll_entry();
2194 DBG("Metadata poll wait");
2195 ret
= lttng_poll_wait(&events
, -1);
2196 DBG("Metadata poll return from wait with %d fd(s)",
2197 LTTNG_POLL_GETNB(&events
));
2199 DBG("Metadata event catched in thread");
2201 if (errno
== EINTR
) {
2202 ERR("Poll EINTR catched");
2205 if (LTTNG_POLL_GETNB(&events
) == 0) {
2206 err
= 0; /* All is OK */
2213 /* From here, the event is a metadata wait fd */
2214 for (i
= 0; i
< nb_fd
; i
++) {
2215 health_code_update();
2217 revents
= LTTNG_POLL_GETEV(&events
, i
);
2218 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2221 /* No activity for this FD (poll implementation). */
2225 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2226 if (revents
& LPOLLIN
) {
2229 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2230 &stream
, sizeof(stream
));
2231 if (pipe_len
< sizeof(stream
)) {
2233 PERROR("read metadata stream");
2236 * Remove the pipe from the poll set and continue the loop
2237 * since their might be data to consume.
2239 lttng_poll_del(&events
,
2240 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2241 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2245 /* A NULL stream means that the state has changed. */
2246 if (stream
== NULL
) {
2247 /* Check for deleted streams. */
2248 validate_endpoint_status_metadata_stream(&events
);
2252 DBG("Adding metadata stream %d to poll set",
2255 /* Add metadata stream to the global poll events list */
2256 lttng_poll_add(&events
, stream
->wait_fd
,
2257 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2258 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2259 DBG("Metadata thread pipe hung up");
2261 * Remove the pipe from the poll set and continue the loop
2262 * since their might be data to consume.
2264 lttng_poll_del(&events
,
2265 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2266 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2269 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2273 /* Handle other stream */
2279 uint64_t tmp_id
= (uint64_t) pollfd
;
2281 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2283 node
= lttng_ht_iter_get_node_u64(&iter
);
2286 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2289 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2290 /* Get the data out of the metadata file descriptor */
2291 DBG("Metadata available on fd %d", pollfd
);
2292 assert(stream
->wait_fd
== pollfd
);
2295 health_code_update();
2297 len
= ctx
->on_buffer_ready(stream
, ctx
);
2299 * We don't check the return value here since if we get
2300 * a negative len, it means an error occured thus we
2301 * simply remove it from the poll set and free the
2306 /* It's ok to have an unavailable sub-buffer */
2307 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2308 /* Clean up stream from consumer and free it. */
2309 lttng_poll_del(&events
, stream
->wait_fd
);
2310 consumer_del_metadata_stream(stream
, metadata_ht
);
2312 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2313 DBG("Metadata fd %d is hup|err.", pollfd
);
2314 if (!stream
->hangup_flush_done
2315 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2316 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2317 DBG("Attempting to flush and consume the UST buffers");
2318 lttng_ustconsumer_on_stream_hangup(stream
);
2320 /* We just flushed the stream now read it. */
2322 health_code_update();
2324 len
= ctx
->on_buffer_ready(stream
, ctx
);
2326 * We don't check the return value here since if we get
2327 * a negative len, it means an error occured thus we
2328 * simply remove it from the poll set and free the
2334 lttng_poll_del(&events
, stream
->wait_fd
);
2336 * This call update the channel states, closes file descriptors
2337 * and securely free the stream.
2339 consumer_del_metadata_stream(stream
, metadata_ht
);
2341 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2345 /* Release RCU lock for the stream looked up */
2353 DBG("Metadata poll thread exiting");
2355 lttng_poll_clean(&events
);
2360 ERR("Health error occurred in %s", __func__
);
2362 health_unregister(health_consumerd
);
2363 rcu_unregister_thread();
2368 * This thread polls the fds in the set to consume the data and write
2369 * it to tracefile if necessary.
2371 void *consumer_thread_data_poll(void *data
)
2373 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2374 struct pollfd
*pollfd
= NULL
;
2375 /* local view of the streams */
2376 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2377 /* local view of consumer_data.fds_count */
2379 struct lttng_consumer_local_data
*ctx
= data
;
2382 rcu_register_thread();
2384 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2386 if (testpoint(consumerd_thread_data
)) {
2387 goto error_testpoint
;
2390 health_code_update();
2392 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2393 if (local_stream
== NULL
) {
2394 PERROR("local_stream malloc");
2399 health_code_update();
2405 * the fds set has been updated, we need to update our
2406 * local array as well
2408 pthread_mutex_lock(&consumer_data
.lock
);
2409 if (consumer_data
.need_update
) {
2414 local_stream
= NULL
;
2417 * Allocate for all fds +1 for the consumer_data_pipe and +1 for
2420 pollfd
= zmalloc((consumer_data
.stream_count
+ 2) * sizeof(struct pollfd
));
2421 if (pollfd
== NULL
) {
2422 PERROR("pollfd malloc");
2423 pthread_mutex_unlock(&consumer_data
.lock
);
2427 local_stream
= zmalloc((consumer_data
.stream_count
+ 2) *
2428 sizeof(struct lttng_consumer_stream
*));
2429 if (local_stream
== NULL
) {
2430 PERROR("local_stream malloc");
2431 pthread_mutex_unlock(&consumer_data
.lock
);
2434 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2437 ERR("Error in allocating pollfd or local_outfds");
2438 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2439 pthread_mutex_unlock(&consumer_data
.lock
);
2443 consumer_data
.need_update
= 0;
2445 pthread_mutex_unlock(&consumer_data
.lock
);
2447 /* No FDs and consumer_quit, consumer_cleanup the thread */
2448 if (nb_fd
== 0 && consumer_quit
== 1) {
2449 err
= 0; /* All is OK */
2452 /* poll on the array of fds */
2454 DBG("polling on %d fd", nb_fd
+ 2);
2455 health_poll_entry();
2456 num_rdy
= poll(pollfd
, nb_fd
+ 2, -1);
2458 DBG("poll num_rdy : %d", num_rdy
);
2459 if (num_rdy
== -1) {
2461 * Restart interrupted system call.
2463 if (errno
== EINTR
) {
2466 PERROR("Poll error");
2467 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2469 } else if (num_rdy
== 0) {
2470 DBG("Polling thread timed out");
2475 * If the consumer_data_pipe triggered poll go directly to the
2476 * beginning of the loop to update the array. We want to prioritize
2477 * array update over low-priority reads.
2479 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2480 ssize_t pipe_readlen
;
2482 DBG("consumer_data_pipe wake up");
2483 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2484 &new_stream
, sizeof(new_stream
));
2485 if (pipe_readlen
< sizeof(new_stream
)) {
2486 PERROR("Consumer data pipe");
2487 /* Continue so we can at least handle the current stream(s). */
2492 * If the stream is NULL, just ignore it. It's also possible that
2493 * the sessiond poll thread changed the consumer_quit state and is
2494 * waking us up to test it.
2496 if (new_stream
== NULL
) {
2497 validate_endpoint_status_data_stream();
2501 /* Continue to update the local streams and handle prio ones */
2505 /* Handle wakeup pipe. */
2506 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2508 ssize_t pipe_readlen
;
2510 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2512 if (pipe_readlen
< 0) {
2513 PERROR("Consumer data wakeup pipe");
2515 /* We've been awakened to handle stream(s). */
2516 ctx
->has_wakeup
= 0;
2519 /* Take care of high priority channels first. */
2520 for (i
= 0; i
< nb_fd
; i
++) {
2521 health_code_update();
2523 if (local_stream
[i
] == NULL
) {
2526 if (pollfd
[i
].revents
& POLLPRI
) {
2527 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2529 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2530 /* it's ok to have an unavailable sub-buffer */
2531 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2532 /* Clean the stream and free it. */
2533 consumer_del_stream(local_stream
[i
], data_ht
);
2534 local_stream
[i
] = NULL
;
2535 } else if (len
> 0) {
2536 local_stream
[i
]->data_read
= 1;
2542 * If we read high prio channel in this loop, try again
2543 * for more high prio data.
2549 /* Take care of low priority channels. */
2550 for (i
= 0; i
< nb_fd
; i
++) {
2551 health_code_update();
2553 if (local_stream
[i
] == NULL
) {
2556 if ((pollfd
[i
].revents
& POLLIN
) ||
2557 local_stream
[i
]->hangup_flush_done
||
2558 local_stream
[i
]->has_data
) {
2559 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2560 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2561 /* it's ok to have an unavailable sub-buffer */
2562 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2563 /* Clean the stream and free it. */
2564 consumer_del_stream(local_stream
[i
], data_ht
);
2565 local_stream
[i
] = NULL
;
2566 } else if (len
> 0) {
2567 local_stream
[i
]->data_read
= 1;
2572 /* Handle hangup and errors */
2573 for (i
= 0; i
< nb_fd
; i
++) {
2574 health_code_update();
2576 if (local_stream
[i
] == NULL
) {
2579 if (!local_stream
[i
]->hangup_flush_done
2580 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2581 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2582 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2583 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2585 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2586 /* Attempt read again, for the data we just flushed. */
2587 local_stream
[i
]->data_read
= 1;
2590 * If the poll flag is HUP/ERR/NVAL and we have
2591 * read no data in this pass, we can remove the
2592 * stream from its hash table.
2594 if ((pollfd
[i
].revents
& POLLHUP
)) {
2595 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2596 if (!local_stream
[i
]->data_read
) {
2597 consumer_del_stream(local_stream
[i
], data_ht
);
2598 local_stream
[i
] = NULL
;
2601 } else if (pollfd
[i
].revents
& POLLERR
) {
2602 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2603 if (!local_stream
[i
]->data_read
) {
2604 consumer_del_stream(local_stream
[i
], data_ht
);
2605 local_stream
[i
] = NULL
;
2608 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2609 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2610 if (!local_stream
[i
]->data_read
) {
2611 consumer_del_stream(local_stream
[i
], data_ht
);
2612 local_stream
[i
] = NULL
;
2616 if (local_stream
[i
] != NULL
) {
2617 local_stream
[i
]->data_read
= 0;
2624 DBG("polling thread exiting");
2629 * Close the write side of the pipe so epoll_wait() in
2630 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2631 * read side of the pipe. If we close them both, epoll_wait strangely does
2632 * not return and could create a endless wait period if the pipe is the
2633 * only tracked fd in the poll set. The thread will take care of closing
2636 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2641 ERR("Health error occurred in %s", __func__
);
2643 health_unregister(health_consumerd
);
2645 rcu_unregister_thread();
2650 * Close wake-up end of each stream belonging to the channel. This will
2651 * allow the poll() on the stream read-side to detect when the
2652 * write-side (application) finally closes them.
2655 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2657 struct lttng_ht
*ht
;
2658 struct lttng_consumer_stream
*stream
;
2659 struct lttng_ht_iter iter
;
2661 ht
= consumer_data
.stream_per_chan_id_ht
;
2664 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2665 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2666 ht
->match_fct
, &channel
->key
,
2667 &iter
.iter
, stream
, node_channel_id
.node
) {
2669 * Protect against teardown with mutex.
2671 pthread_mutex_lock(&stream
->lock
);
2672 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2675 switch (consumer_data
.type
) {
2676 case LTTNG_CONSUMER_KERNEL
:
2678 case LTTNG_CONSUMER32_UST
:
2679 case LTTNG_CONSUMER64_UST
:
2680 if (stream
->metadata_flag
) {
2681 /* Safe and protected by the stream lock. */
2682 lttng_ustconsumer_close_metadata(stream
->chan
);
2685 * Note: a mutex is taken internally within
2686 * liblttng-ust-ctl to protect timer wakeup_fd
2687 * use from concurrent close.
2689 lttng_ustconsumer_close_stream_wakeup(stream
);
2693 ERR("Unknown consumer_data type");
2697 pthread_mutex_unlock(&stream
->lock
);
2702 static void destroy_channel_ht(struct lttng_ht
*ht
)
2704 struct lttng_ht_iter iter
;
2705 struct lttng_consumer_channel
*channel
;
2713 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2714 ret
= lttng_ht_del(ht
, &iter
);
2719 lttng_ht_destroy(ht
);
2723 * This thread polls the channel fds to detect when they are being
2724 * closed. It closes all related streams if the channel is detected as
2725 * closed. It is currently only used as a shim layer for UST because the
2726 * consumerd needs to keep the per-stream wakeup end of pipes open for
2729 void *consumer_thread_channel_poll(void *data
)
2731 int ret
, i
, pollfd
, err
= -1;
2732 uint32_t revents
, nb_fd
;
2733 struct lttng_consumer_channel
*chan
= NULL
;
2734 struct lttng_ht_iter iter
;
2735 struct lttng_ht_node_u64
*node
;
2736 struct lttng_poll_event events
;
2737 struct lttng_consumer_local_data
*ctx
= data
;
2738 struct lttng_ht
*channel_ht
;
2740 rcu_register_thread();
2742 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2744 if (testpoint(consumerd_thread_channel
)) {
2745 goto error_testpoint
;
2748 health_code_update();
2750 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2752 /* ENOMEM at this point. Better to bail out. */
2756 DBG("Thread channel poll started");
2758 /* Size is set to 1 for the consumer_channel pipe */
2759 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2761 ERR("Poll set creation failed");
2765 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2771 DBG("Channel main loop started");
2775 health_code_update();
2776 DBG("Channel poll wait");
2777 health_poll_entry();
2778 ret
= lttng_poll_wait(&events
, -1);
2779 DBG("Channel poll return from wait with %d fd(s)",
2780 LTTNG_POLL_GETNB(&events
));
2782 DBG("Channel event catched in thread");
2784 if (errno
== EINTR
) {
2785 ERR("Poll EINTR catched");
2788 if (LTTNG_POLL_GETNB(&events
) == 0) {
2789 err
= 0; /* All is OK */
2796 /* From here, the event is a channel wait fd */
2797 for (i
= 0; i
< nb_fd
; i
++) {
2798 health_code_update();
2800 revents
= LTTNG_POLL_GETEV(&events
, i
);
2801 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2804 /* No activity for this FD (poll implementation). */
2808 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2809 if (revents
& LPOLLIN
) {
2810 enum consumer_channel_action action
;
2813 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2816 ERR("Error reading channel pipe");
2818 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2823 case CONSUMER_CHANNEL_ADD
:
2824 DBG("Adding channel %d to poll set",
2827 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2830 lttng_ht_add_unique_u64(channel_ht
,
2831 &chan
->wait_fd_node
);
2833 /* Add channel to the global poll events list */
2834 lttng_poll_add(&events
, chan
->wait_fd
,
2835 LPOLLERR
| LPOLLHUP
);
2837 case CONSUMER_CHANNEL_DEL
:
2840 * This command should never be called if the channel
2841 * has streams monitored by either the data or metadata
2842 * thread. The consumer only notify this thread with a
2843 * channel del. command if it receives a destroy
2844 * channel command from the session daemon that send it
2845 * if a command prior to the GET_CHANNEL failed.
2849 chan
= consumer_find_channel(key
);
2852 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2855 lttng_poll_del(&events
, chan
->wait_fd
);
2856 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2857 ret
= lttng_ht_del(channel_ht
, &iter
);
2860 switch (consumer_data
.type
) {
2861 case LTTNG_CONSUMER_KERNEL
:
2863 case LTTNG_CONSUMER32_UST
:
2864 case LTTNG_CONSUMER64_UST
:
2865 health_code_update();
2866 /* Destroy streams that might have been left in the stream list. */
2867 clean_channel_stream_list(chan
);
2870 ERR("Unknown consumer_data type");
2875 * Release our own refcount. Force channel deletion even if
2876 * streams were not initialized.
2878 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2879 consumer_del_channel(chan
);
2884 case CONSUMER_CHANNEL_QUIT
:
2886 * Remove the pipe from the poll set and continue the loop
2887 * since their might be data to consume.
2889 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2892 ERR("Unknown action");
2895 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2896 DBG("Channel thread pipe hung up");
2898 * Remove the pipe from the poll set and continue the loop
2899 * since their might be data to consume.
2901 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2904 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2908 /* Handle other stream */
2914 uint64_t tmp_id
= (uint64_t) pollfd
;
2916 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2918 node
= lttng_ht_iter_get_node_u64(&iter
);
2921 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2924 /* Check for error event */
2925 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2926 DBG("Channel fd %d is hup|err.", pollfd
);
2928 lttng_poll_del(&events
, chan
->wait_fd
);
2929 ret
= lttng_ht_del(channel_ht
, &iter
);
2933 * This will close the wait fd for each stream associated to
2934 * this channel AND monitored by the data/metadata thread thus
2935 * will be clean by the right thread.
2937 consumer_close_channel_streams(chan
);
2939 /* Release our own refcount */
2940 if (!uatomic_sub_return(&chan
->refcount
, 1)
2941 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2942 consumer_del_channel(chan
);
2945 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2950 /* Release RCU lock for the channel looked up */
2958 lttng_poll_clean(&events
);
2960 destroy_channel_ht(channel_ht
);
2963 DBG("Channel poll thread exiting");
2966 ERR("Health error occurred in %s", __func__
);
2968 health_unregister(health_consumerd
);
2969 rcu_unregister_thread();
2973 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2974 struct pollfd
*sockpoll
, int client_socket
)
2981 ret
= lttng_consumer_poll_socket(sockpoll
);
2985 DBG("Metadata connection on client_socket");
2987 /* Blocking call, waiting for transmission */
2988 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2989 if (ctx
->consumer_metadata_socket
< 0) {
2990 WARN("On accept metadata");
3001 * This thread listens on the consumerd socket and receives the file
3002 * descriptors from the session daemon.
3004 void *consumer_thread_sessiond_poll(void *data
)
3006 int sock
= -1, client_socket
, ret
, err
= -1;
3008 * structure to poll for incoming data on communication socket avoids
3009 * making blocking sockets.
3011 struct pollfd consumer_sockpoll
[2];
3012 struct lttng_consumer_local_data
*ctx
= data
;
3014 rcu_register_thread();
3016 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3018 if (testpoint(consumerd_thread_sessiond
)) {
3019 goto error_testpoint
;
3022 health_code_update();
3024 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3025 unlink(ctx
->consumer_command_sock_path
);
3026 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3027 if (client_socket
< 0) {
3028 ERR("Cannot create command socket");
3032 ret
= lttcomm_listen_unix_sock(client_socket
);
3037 DBG("Sending ready command to lttng-sessiond");
3038 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3039 /* return < 0 on error, but == 0 is not fatal */
3041 ERR("Error sending ready command to lttng-sessiond");
3045 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3046 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3047 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3048 consumer_sockpoll
[1].fd
= client_socket
;
3049 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3051 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3059 DBG("Connection on client_socket");
3061 /* Blocking call, waiting for transmission */
3062 sock
= lttcomm_accept_unix_sock(client_socket
);
3069 * Setup metadata socket which is the second socket connection on the
3070 * command unix socket.
3072 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3081 /* This socket is not useful anymore. */
3082 ret
= close(client_socket
);
3084 PERROR("close client_socket");
3088 /* update the polling structure to poll on the established socket */
3089 consumer_sockpoll
[1].fd
= sock
;
3090 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3093 health_code_update();
3095 health_poll_entry();
3096 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3105 DBG("Incoming command on sock");
3106 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3109 * This could simply be a session daemon quitting. Don't output
3112 DBG("Communication interrupted on command socket");
3116 if (consumer_quit
) {
3117 DBG("consumer_thread_receive_fds received quit from signal");
3118 err
= 0; /* All is OK */
3121 DBG("received command on sock");
3127 DBG("Consumer thread sessiond poll exiting");
3130 * Close metadata streams since the producer is the session daemon which
3133 * NOTE: for now, this only applies to the UST tracer.
3135 lttng_consumer_close_all_metadata();
3138 * when all fds have hung up, the polling thread
3144 * Notify the data poll thread to poll back again and test the
3145 * consumer_quit state that we just set so to quit gracefully.
3147 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3149 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3151 notify_health_quit_pipe(health_quit_pipe
);
3153 /* Cleaning up possibly open sockets. */
3157 PERROR("close sock sessiond poll");
3160 if (client_socket
>= 0) {
3161 ret
= close(client_socket
);
3163 PERROR("close client_socket sessiond poll");
3170 ERR("Health error occurred in %s", __func__
);
3172 health_unregister(health_consumerd
);
3174 rcu_unregister_thread();
3178 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3179 struct lttng_consumer_local_data
*ctx
)
3183 pthread_mutex_lock(&stream
->lock
);
3184 if (stream
->metadata_flag
) {
3185 pthread_mutex_lock(&stream
->metadata_rdv_lock
);
3188 switch (consumer_data
.type
) {
3189 case LTTNG_CONSUMER_KERNEL
:
3190 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3192 case LTTNG_CONSUMER32_UST
:
3193 case LTTNG_CONSUMER64_UST
:
3194 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3197 ERR("Unknown consumer_data type");
3203 if (stream
->metadata_flag
) {
3204 pthread_cond_broadcast(&stream
->metadata_rdv
);
3205 pthread_mutex_unlock(&stream
->metadata_rdv_lock
);
3207 pthread_mutex_unlock(&stream
->lock
);
3211 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3213 switch (consumer_data
.type
) {
3214 case LTTNG_CONSUMER_KERNEL
:
3215 return lttng_kconsumer_on_recv_stream(stream
);
3216 case LTTNG_CONSUMER32_UST
:
3217 case LTTNG_CONSUMER64_UST
:
3218 return lttng_ustconsumer_on_recv_stream(stream
);
3220 ERR("Unknown consumer_data type");
3227 * Allocate and set consumer data hash tables.
3229 int lttng_consumer_init(void)
3231 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3232 if (!consumer_data
.channel_ht
) {
3236 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3237 if (!consumer_data
.relayd_ht
) {
3241 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3242 if (!consumer_data
.stream_list_ht
) {
3246 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3247 if (!consumer_data
.stream_per_chan_id_ht
) {
3251 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3256 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3268 * Process the ADD_RELAYD command receive by a consumer.
3270 * This will create a relayd socket pair and add it to the relayd hash table.
3271 * The caller MUST acquire a RCU read side lock before calling it.
3273 int consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3274 struct lttng_consumer_local_data
*ctx
, int sock
,
3275 struct pollfd
*consumer_sockpoll
,
3276 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3277 uint64_t relayd_session_id
)
3279 int fd
= -1, ret
= -1, relayd_created
= 0;
3280 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3281 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3284 assert(relayd_sock
);
3286 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3288 /* Get relayd reference if exists. */
3289 relayd
= consumer_find_relayd(net_seq_idx
);
3290 if (relayd
== NULL
) {
3291 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3292 /* Not found. Allocate one. */
3293 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3294 if (relayd
== NULL
) {
3296 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3299 relayd
->sessiond_session_id
= sessiond_id
;
3304 * This code path MUST continue to the consumer send status message to
3305 * we can notify the session daemon and continue our work without
3306 * killing everything.
3310 * relayd key should never be found for control socket.
3312 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3315 /* First send a status message before receiving the fds. */
3316 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3318 /* Somehow, the session daemon is not responding anymore. */
3319 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3320 goto error_nosignal
;
3323 /* Poll on consumer socket. */
3324 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3326 /* Needing to exit in the middle of a command: error. */
3327 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3329 goto error_nosignal
;
3332 /* Get relayd socket from session daemon */
3333 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3334 if (ret
!= sizeof(fd
)) {
3336 fd
= -1; /* Just in case it gets set with an invalid value. */
3339 * Failing to receive FDs might indicate a major problem such as
3340 * reaching a fd limit during the receive where the kernel returns a
3341 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3342 * don't take any chances and stop everything.
3344 * XXX: Feature request #558 will fix that and avoid this possible
3345 * issue when reaching the fd limit.
3347 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3348 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3352 /* Copy socket information and received FD */
3353 switch (sock_type
) {
3354 case LTTNG_STREAM_CONTROL
:
3355 /* Copy received lttcomm socket */
3356 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3357 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3358 /* Handle create_sock error. */
3360 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3364 * Close the socket created internally by
3365 * lttcomm_create_sock, so we can replace it by the one
3366 * received from sessiond.
3368 if (close(relayd
->control_sock
.sock
.fd
)) {
3372 /* Assign new file descriptor */
3373 relayd
->control_sock
.sock
.fd
= fd
;
3374 fd
= -1; /* For error path */
3375 /* Assign version values. */
3376 relayd
->control_sock
.major
= relayd_sock
->major
;
3377 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3379 relayd
->relayd_session_id
= relayd_session_id
;
3382 case LTTNG_STREAM_DATA
:
3383 /* Copy received lttcomm socket */
3384 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3385 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3386 /* Handle create_sock error. */
3388 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3392 * Close the socket created internally by
3393 * lttcomm_create_sock, so we can replace it by the one
3394 * received from sessiond.
3396 if (close(relayd
->data_sock
.sock
.fd
)) {
3400 /* Assign new file descriptor */
3401 relayd
->data_sock
.sock
.fd
= fd
;
3402 fd
= -1; /* for eventual error paths */
3403 /* Assign version values. */
3404 relayd
->data_sock
.major
= relayd_sock
->major
;
3405 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3408 ERR("Unknown relayd socket type (%d)", sock_type
);
3410 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3414 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3415 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3416 relayd
->net_seq_idx
, fd
);
3418 /* We successfully added the socket. Send status back. */
3419 ret
= consumer_send_status_msg(sock
, ret_code
);
3421 /* Somehow, the session daemon is not responding anymore. */
3422 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3423 goto error_nosignal
;
3427 * Add relayd socket pair to consumer data hashtable. If object already
3428 * exists or on error, the function gracefully returns.
3436 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3437 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3441 /* Close received socket if valid. */
3444 PERROR("close received socket");
3448 if (relayd_created
) {
3456 * Try to lock the stream mutex.
3458 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3460 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3467 * Try to lock the stream mutex. On failure, we know that the stream is
3468 * being used else where hence there is data still being extracted.
3470 ret
= pthread_mutex_trylock(&stream
->lock
);
3472 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3484 * Search for a relayd associated to the session id and return the reference.
3486 * A rcu read side lock MUST be acquire before calling this function and locked
3487 * until the relayd object is no longer necessary.
3489 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3491 struct lttng_ht_iter iter
;
3492 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3494 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3495 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3498 * Check by sessiond id which is unique here where the relayd session
3499 * id might not be when having multiple relayd.
3501 if (relayd
->sessiond_session_id
== id
) {
3502 /* Found the relayd. There can be only one per id. */
3514 * Check if for a given session id there is still data needed to be extract
3517 * Return 1 if data is pending or else 0 meaning ready to be read.
3519 int consumer_data_pending(uint64_t id
)
3522 struct lttng_ht_iter iter
;
3523 struct lttng_ht
*ht
;
3524 struct lttng_consumer_stream
*stream
;
3525 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3526 int (*data_pending
)(struct lttng_consumer_stream
*);
3528 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3531 pthread_mutex_lock(&consumer_data
.lock
);
3533 switch (consumer_data
.type
) {
3534 case LTTNG_CONSUMER_KERNEL
:
3535 data_pending
= lttng_kconsumer_data_pending
;
3537 case LTTNG_CONSUMER32_UST
:
3538 case LTTNG_CONSUMER64_UST
:
3539 data_pending
= lttng_ustconsumer_data_pending
;
3542 ERR("Unknown consumer data type");
3546 /* Ease our life a bit */
3547 ht
= consumer_data
.stream_list_ht
;
3549 relayd
= find_relayd_by_session_id(id
);
3551 /* Send init command for data pending. */
3552 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3553 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3554 relayd
->relayd_session_id
);
3555 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3557 /* Communication error thus the relayd so no data pending. */
3558 goto data_not_pending
;
3562 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3563 ht
->hash_fct(&id
, lttng_ht_seed
),
3565 &iter
.iter
, stream
, node_session_id
.node
) {
3566 /* If this call fails, the stream is being used hence data pending. */
3567 ret
= stream_try_lock(stream
);
3573 * A removed node from the hash table indicates that the stream has
3574 * been deleted thus having a guarantee that the buffers are closed
3575 * on the consumer side. However, data can still be transmitted
3576 * over the network so don't skip the relayd check.
3578 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3580 /* Check the stream if there is data in the buffers. */
3581 ret
= data_pending(stream
);
3583 pthread_mutex_unlock(&stream
->lock
);
3590 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3591 if (stream
->metadata_flag
) {
3592 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3593 stream
->relayd_stream_id
);
3595 ret
= relayd_data_pending(&relayd
->control_sock
,
3596 stream
->relayd_stream_id
,
3597 stream
->next_net_seq_num
- 1);
3599 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3601 pthread_mutex_unlock(&stream
->lock
);
3605 pthread_mutex_unlock(&stream
->lock
);
3609 unsigned int is_data_inflight
= 0;
3611 /* Send init command for data pending. */
3612 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3613 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3614 relayd
->relayd_session_id
, &is_data_inflight
);
3615 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3617 goto data_not_pending
;
3619 if (is_data_inflight
) {
3625 * Finding _no_ node in the hash table and no inflight data means that the
3626 * stream(s) have been removed thus data is guaranteed to be available for
3627 * analysis from the trace files.
3631 /* Data is available to be read by a viewer. */
3632 pthread_mutex_unlock(&consumer_data
.lock
);
3637 /* Data is still being extracted from buffers. */
3638 pthread_mutex_unlock(&consumer_data
.lock
);
3644 * Send a ret code status message to the sessiond daemon.
3646 * Return the sendmsg() return value.
3648 int consumer_send_status_msg(int sock
, int ret_code
)
3650 struct lttcomm_consumer_status_msg msg
;
3652 memset(&msg
, 0, sizeof(msg
));
3653 msg
.ret_code
= ret_code
;
3655 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3659 * Send a channel status message to the sessiond daemon.
3661 * Return the sendmsg() return value.
3663 int consumer_send_status_channel(int sock
,
3664 struct lttng_consumer_channel
*channel
)
3666 struct lttcomm_consumer_status_channel msg
;
3670 memset(&msg
, 0, sizeof(msg
));
3672 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3674 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3675 msg
.key
= channel
->key
;
3676 msg
.stream_count
= channel
->streams
.count
;
3679 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3682 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3683 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3684 uint64_t max_sb_size
)
3686 unsigned long start_pos
;
3688 if (!nb_packets_per_stream
) {
3689 return consumed_pos
; /* Grab everything */
3691 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
3692 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3693 if ((long) (start_pos
- consumed_pos
) < 0) {
3694 return consumed_pos
; /* Grab everything */