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/index/index.h>
38 #include <common/kernel-ctl/kernel-ctl.h>
39 #include <common/sessiond-comm/relayd.h>
40 #include <common/sessiond-comm/sessiond-comm.h>
41 #include <common/kernel-consumer/kernel-consumer.h>
42 #include <common/relayd/relayd.h>
43 #include <common/ust-consumer/ust-consumer.h>
44 #include <common/consumer-timer.h>
47 #include "consumer-stream.h"
48 #include "consumer-testpoint.h"
50 struct lttng_consumer_global_data consumer_data
= {
53 .type
= LTTNG_CONSUMER_UNKNOWN
,
56 enum consumer_channel_action
{
59 CONSUMER_CHANNEL_QUIT
,
62 struct consumer_channel_msg
{
63 enum consumer_channel_action action
;
64 struct lttng_consumer_channel
*chan
; /* add */
65 uint64_t key
; /* del */
69 * Flag to inform the polling thread to quit when all fd hung up. Updated by
70 * the consumer_thread_receive_fds when it notices that all fds has hung up.
71 * Also updated by the signal handler (consumer_should_exit()). Read by the
74 volatile int consumer_quit
;
77 * Global hash table containing respectively metadata and data streams. The
78 * stream element in this ht should only be updated by the metadata poll thread
79 * for the metadata and the data poll thread for the data.
81 static struct lttng_ht
*metadata_ht
;
82 static struct lttng_ht
*data_ht
;
85 * Notify a thread lttng pipe to poll back again. This usually means that some
86 * global state has changed so we just send back the thread in a poll wait
89 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
91 struct lttng_consumer_stream
*null_stream
= NULL
;
95 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
98 static void notify_health_quit_pipe(int *pipe
)
102 ret
= lttng_write(pipe
[1], "4", 1);
104 PERROR("write consumer health quit");
108 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
109 struct lttng_consumer_channel
*chan
,
111 enum consumer_channel_action action
)
113 struct consumer_channel_msg msg
;
116 memset(&msg
, 0, sizeof(msg
));
121 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
122 if (ret
< sizeof(msg
)) {
123 PERROR("notify_channel_pipe write error");
127 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
130 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
133 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
134 struct lttng_consumer_channel
**chan
,
136 enum consumer_channel_action
*action
)
138 struct consumer_channel_msg msg
;
141 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
142 if (ret
< sizeof(msg
)) {
146 *action
= msg
.action
;
154 * Cleanup the stream list of a channel. Those streams are not yet globally
157 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
159 struct lttng_consumer_stream
*stream
, *stmp
;
163 /* Delete streams that might have been left in the stream list. */
164 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
166 cds_list_del(&stream
->send_node
);
168 * Once a stream is added to this list, the buffers were created so we
169 * have a guarantee that this call will succeed. Setting the monitor
170 * mode to 0 so we don't lock nor try to delete the stream from the
174 consumer_stream_destroy(stream
, NULL
);
179 * Find a stream. The consumer_data.lock must be locked during this
182 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
185 struct lttng_ht_iter iter
;
186 struct lttng_ht_node_u64
*node
;
187 struct lttng_consumer_stream
*stream
= NULL
;
191 /* -1ULL keys are lookup failures */
192 if (key
== (uint64_t) -1ULL) {
198 lttng_ht_lookup(ht
, &key
, &iter
);
199 node
= lttng_ht_iter_get_node_u64(&iter
);
201 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
209 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
211 struct lttng_consumer_stream
*stream
;
214 stream
= find_stream(key
, ht
);
216 stream
->key
= (uint64_t) -1ULL;
218 * We don't want the lookup to match, but we still need
219 * to iterate on this stream when iterating over the hash table. Just
220 * change the node key.
222 stream
->node
.key
= (uint64_t) -1ULL;
228 * Return a channel object for the given key.
230 * RCU read side lock MUST be acquired before calling this function and
231 * protects the channel ptr.
233 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
235 struct lttng_ht_iter iter
;
236 struct lttng_ht_node_u64
*node
;
237 struct lttng_consumer_channel
*channel
= NULL
;
239 /* -1ULL keys are lookup failures */
240 if (key
== (uint64_t) -1ULL) {
244 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
245 node
= lttng_ht_iter_get_node_u64(&iter
);
247 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
254 * There is a possibility that the consumer does not have enough time between
255 * the close of the channel on the session daemon and the cleanup in here thus
256 * once we have a channel add with an existing key, we know for sure that this
257 * channel will eventually get cleaned up by all streams being closed.
259 * This function just nullifies the already existing channel key.
261 static void steal_channel_key(uint64_t key
)
263 struct lttng_consumer_channel
*channel
;
266 channel
= consumer_find_channel(key
);
268 channel
->key
= (uint64_t) -1ULL;
270 * We don't want the lookup to match, but we still need to iterate on
271 * this channel when iterating over the hash table. Just change the
274 channel
->node
.key
= (uint64_t) -1ULL;
279 static void free_channel_rcu(struct rcu_head
*head
)
281 struct lttng_ht_node_u64
*node
=
282 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
283 struct lttng_consumer_channel
*channel
=
284 caa_container_of(node
, struct lttng_consumer_channel
, node
);
290 * RCU protected relayd socket pair free.
292 static void free_relayd_rcu(struct rcu_head
*head
)
294 struct lttng_ht_node_u64
*node
=
295 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
296 struct consumer_relayd_sock_pair
*relayd
=
297 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
300 * Close all sockets. This is done in the call RCU since we don't want the
301 * socket fds to be reassigned thus potentially creating bad state of the
304 * We do not have to lock the control socket mutex here since at this stage
305 * there is no one referencing to this relayd object.
307 (void) relayd_close(&relayd
->control_sock
);
308 (void) relayd_close(&relayd
->data_sock
);
314 * Destroy and free relayd socket pair object.
316 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
319 struct lttng_ht_iter iter
;
321 if (relayd
== NULL
) {
325 DBG("Consumer destroy and close relayd socket pair");
327 iter
.iter
.node
= &relayd
->node
.node
;
328 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
330 /* We assume the relayd is being or is destroyed */
334 /* RCU free() call */
335 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
339 * Remove a channel from the global list protected by a mutex. This function is
340 * also responsible for freeing its data structures.
342 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
345 struct lttng_ht_iter iter
;
347 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
349 pthread_mutex_lock(&consumer_data
.lock
);
350 pthread_mutex_lock(&channel
->lock
);
352 /* Destroy streams that might have been left in the stream list. */
353 clean_channel_stream_list(channel
);
355 if (channel
->live_timer_enabled
== 1) {
356 consumer_timer_live_stop(channel
);
359 switch (consumer_data
.type
) {
360 case LTTNG_CONSUMER_KERNEL
:
362 case LTTNG_CONSUMER32_UST
:
363 case LTTNG_CONSUMER64_UST
:
364 lttng_ustconsumer_del_channel(channel
);
367 ERR("Unknown consumer_data type");
373 iter
.iter
.node
= &channel
->node
.node
;
374 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
378 call_rcu(&channel
->node
.head
, free_channel_rcu
);
380 pthread_mutex_unlock(&channel
->lock
);
381 pthread_mutex_unlock(&consumer_data
.lock
);
385 * Iterate over the relayd hash table and destroy each element. Finally,
386 * destroy the whole hash table.
388 static void cleanup_relayd_ht(void)
390 struct lttng_ht_iter iter
;
391 struct consumer_relayd_sock_pair
*relayd
;
395 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
397 consumer_destroy_relayd(relayd
);
402 lttng_ht_destroy(consumer_data
.relayd_ht
);
406 * Update the end point status of all streams having the given network sequence
407 * index (relayd index).
409 * It's atomically set without having the stream mutex locked which is fine
410 * because we handle the write/read race with a pipe wakeup for each thread.
412 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
413 enum consumer_endpoint_status status
)
415 struct lttng_ht_iter iter
;
416 struct lttng_consumer_stream
*stream
;
418 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
422 /* Let's begin with metadata */
423 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
424 if (stream
->net_seq_idx
== net_seq_idx
) {
425 uatomic_set(&stream
->endpoint_status
, status
);
426 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
430 /* Follow up by the data streams */
431 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
432 if (stream
->net_seq_idx
== net_seq_idx
) {
433 uatomic_set(&stream
->endpoint_status
, status
);
434 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
441 * Cleanup a relayd object by flagging every associated streams for deletion,
442 * destroying the object meaning removing it from the relayd hash table,
443 * closing the sockets and freeing the memory in a RCU call.
445 * If a local data context is available, notify the threads that the streams'
446 * state have changed.
448 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
449 struct lttng_consumer_local_data
*ctx
)
455 DBG("Cleaning up relayd sockets");
457 /* Save the net sequence index before destroying the object */
458 netidx
= relayd
->net_seq_idx
;
461 * Delete the relayd from the relayd hash table, close the sockets and free
462 * the object in a RCU call.
464 consumer_destroy_relayd(relayd
);
466 /* Set inactive endpoint to all streams */
467 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
470 * With a local data context, notify the threads that the streams' state
471 * have changed. The write() action on the pipe acts as an "implicit"
472 * memory barrier ordering the updates of the end point status from the
473 * read of this status which happens AFTER receiving this notify.
476 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
477 notify_thread_lttng_pipe(ctx
->consumer_metadata_pipe
);
482 * Flag a relayd socket pair for destruction. Destroy it if the refcount
485 * RCU read side lock MUST be aquired before calling this function.
487 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
491 /* Set destroy flag for this object */
492 uatomic_set(&relayd
->destroy_flag
, 1);
494 /* Destroy the relayd if refcount is 0 */
495 if (uatomic_read(&relayd
->refcount
) == 0) {
496 consumer_destroy_relayd(relayd
);
501 * Completly destroy stream from every visiable data structure and the given
504 * One this call returns, the stream object is not longer usable nor visible.
506 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
509 consumer_stream_destroy(stream
, ht
);
513 * XXX naming of del vs destroy is all mixed up.
515 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
517 consumer_stream_destroy(stream
, data_ht
);
520 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
522 consumer_stream_destroy(stream
, metadata_ht
);
525 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
527 enum lttng_consumer_stream_state state
,
528 const char *channel_name
,
535 enum consumer_channel_type type
,
536 unsigned int monitor
)
539 struct lttng_consumer_stream
*stream
;
541 stream
= zmalloc(sizeof(*stream
));
542 if (stream
== NULL
) {
543 PERROR("malloc struct lttng_consumer_stream");
550 stream
->key
= stream_key
;
552 stream
->out_fd_offset
= 0;
553 stream
->output_written
= 0;
554 stream
->state
= state
;
557 stream
->net_seq_idx
= relayd_id
;
558 stream
->session_id
= session_id
;
559 stream
->monitor
= monitor
;
560 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
561 stream
->index_fd
= -1;
562 pthread_mutex_init(&stream
->lock
, NULL
);
564 /* If channel is the metadata, flag this stream as metadata. */
565 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
566 stream
->metadata_flag
= 1;
567 /* Metadata is flat out. */
568 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
569 /* Live rendez-vous point. */
570 pthread_cond_init(&stream
->metadata_rdv
, NULL
);
571 pthread_mutex_init(&stream
->metadata_rdv_lock
, NULL
);
573 /* Format stream name to <channel_name>_<cpu_number> */
574 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
577 PERROR("snprintf stream name");
582 /* Key is always the wait_fd for streams. */
583 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
585 /* Init node per channel id key */
586 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
588 /* Init session id node with the stream session id */
589 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
591 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
592 " relayd_id %" PRIu64
", session_id %" PRIu64
,
593 stream
->name
, stream
->key
, channel_key
,
594 stream
->net_seq_idx
, stream
->session_id
);
610 * Add a stream to the global list protected by a mutex.
612 int consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
614 struct lttng_ht
*ht
= data_ht
;
620 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
622 pthread_mutex_lock(&consumer_data
.lock
);
623 pthread_mutex_lock(&stream
->chan
->lock
);
624 pthread_mutex_lock(&stream
->chan
->timer_lock
);
625 pthread_mutex_lock(&stream
->lock
);
628 /* Steal stream identifier to avoid having streams with the same key */
629 steal_stream_key(stream
->key
, ht
);
631 lttng_ht_add_unique_u64(ht
, &stream
->node
);
633 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
634 &stream
->node_channel_id
);
637 * Add stream to the stream_list_ht of the consumer data. No need to steal
638 * the key since the HT does not use it and we allow to add redundant keys
641 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
644 * When nb_init_stream_left reaches 0, we don't need to trigger any action
645 * in terms of destroying the associated channel, because the action that
646 * causes the count to become 0 also causes a stream to be added. The
647 * channel deletion will thus be triggered by the following removal of this
650 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
651 /* Increment refcount before decrementing nb_init_stream_left */
653 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
656 /* Update consumer data once the node is inserted. */
657 consumer_data
.stream_count
++;
658 consumer_data
.need_update
= 1;
661 pthread_mutex_unlock(&stream
->lock
);
662 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
663 pthread_mutex_unlock(&stream
->chan
->lock
);
664 pthread_mutex_unlock(&consumer_data
.lock
);
669 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
671 consumer_del_stream(stream
, data_ht
);
675 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
676 * be acquired before calling this.
678 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
681 struct lttng_ht_node_u64
*node
;
682 struct lttng_ht_iter iter
;
686 lttng_ht_lookup(consumer_data
.relayd_ht
,
687 &relayd
->net_seq_idx
, &iter
);
688 node
= lttng_ht_iter_get_node_u64(&iter
);
692 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
699 * Allocate and return a consumer relayd socket.
701 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
702 uint64_t net_seq_idx
)
704 struct consumer_relayd_sock_pair
*obj
= NULL
;
706 /* net sequence index of -1 is a failure */
707 if (net_seq_idx
== (uint64_t) -1ULL) {
711 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
713 PERROR("zmalloc relayd sock");
717 obj
->net_seq_idx
= net_seq_idx
;
719 obj
->destroy_flag
= 0;
720 obj
->control_sock
.sock
.fd
= -1;
721 obj
->data_sock
.sock
.fd
= -1;
722 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
723 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
730 * Find a relayd socket pair in the global consumer data.
732 * Return the object if found else NULL.
733 * RCU read-side lock must be held across this call and while using the
736 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
738 struct lttng_ht_iter iter
;
739 struct lttng_ht_node_u64
*node
;
740 struct consumer_relayd_sock_pair
*relayd
= NULL
;
742 /* Negative keys are lookup failures */
743 if (key
== (uint64_t) -1ULL) {
747 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
749 node
= lttng_ht_iter_get_node_u64(&iter
);
751 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
759 * Find a relayd and send the stream
761 * Returns 0 on success, < 0 on error
763 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
767 struct consumer_relayd_sock_pair
*relayd
;
770 assert(stream
->net_seq_idx
!= -1ULL);
773 /* The stream is not metadata. Get relayd reference if exists. */
775 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
776 if (relayd
!= NULL
) {
777 /* Add stream on the relayd */
778 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
779 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
780 path
, &stream
->relayd_stream_id
,
781 stream
->chan
->tracefile_size
, stream
->chan
->tracefile_count
);
782 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
787 uatomic_inc(&relayd
->refcount
);
788 stream
->sent_to_relayd
= 1;
790 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
791 stream
->key
, stream
->net_seq_idx
);
796 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
797 stream
->name
, stream
->key
, stream
->net_seq_idx
);
805 * Find a relayd and send the streams sent message
807 * Returns 0 on success, < 0 on error
809 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
812 struct consumer_relayd_sock_pair
*relayd
;
814 assert(net_seq_idx
!= -1ULL);
816 /* The stream is not metadata. Get relayd reference if exists. */
818 relayd
= consumer_find_relayd(net_seq_idx
);
819 if (relayd
!= NULL
) {
820 /* Add stream on the relayd */
821 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
822 ret
= relayd_streams_sent(&relayd
->control_sock
);
823 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
828 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
835 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
843 * Find a relayd and close the stream
845 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
847 struct consumer_relayd_sock_pair
*relayd
;
849 /* The stream is not metadata. Get relayd reference if exists. */
851 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
853 consumer_stream_relayd_close(stream
, relayd
);
859 * Handle stream for relayd transmission if the stream applies for network
860 * streaming where the net sequence index is set.
862 * Return destination file descriptor or negative value on error.
864 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
865 size_t data_size
, unsigned long padding
,
866 struct consumer_relayd_sock_pair
*relayd
)
869 struct lttcomm_relayd_data_hdr data_hdr
;
875 /* Reset data header */
876 memset(&data_hdr
, 0, sizeof(data_hdr
));
878 if (stream
->metadata_flag
) {
879 /* Caller MUST acquire the relayd control socket lock */
880 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
885 /* Metadata are always sent on the control socket. */
886 outfd
= relayd
->control_sock
.sock
.fd
;
888 /* Set header with stream information */
889 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
890 data_hdr
.data_size
= htobe32(data_size
);
891 data_hdr
.padding_size
= htobe32(padding
);
893 * Note that net_seq_num below is assigned with the *current* value of
894 * next_net_seq_num and only after that the next_net_seq_num will be
895 * increment. This is why when issuing a command on the relayd using
896 * this next value, 1 should always be substracted in order to compare
897 * the last seen sequence number on the relayd side to the last sent.
899 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
900 /* Other fields are zeroed previously */
902 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
908 ++stream
->next_net_seq_num
;
910 /* Set to go on data socket */
911 outfd
= relayd
->data_sock
.sock
.fd
;
919 * Allocate and return a new lttng_consumer_channel object using the given key
920 * to initialize the hash table node.
922 * On error, return NULL.
924 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
926 const char *pathname
,
931 enum lttng_event_output output
,
932 uint64_t tracefile_size
,
933 uint64_t tracefile_count
,
934 uint64_t session_id_per_pid
,
935 unsigned int monitor
,
936 unsigned int live_timer_interval
)
938 struct lttng_consumer_channel
*channel
;
940 channel
= zmalloc(sizeof(*channel
));
941 if (channel
== NULL
) {
942 PERROR("malloc struct lttng_consumer_channel");
947 channel
->refcount
= 0;
948 channel
->session_id
= session_id
;
949 channel
->session_id_per_pid
= session_id_per_pid
;
952 channel
->relayd_id
= relayd_id
;
953 channel
->tracefile_size
= tracefile_size
;
954 channel
->tracefile_count
= tracefile_count
;
955 channel
->monitor
= monitor
;
956 channel
->live_timer_interval
= live_timer_interval
;
957 pthread_mutex_init(&channel
->lock
, NULL
);
958 pthread_mutex_init(&channel
->timer_lock
, NULL
);
961 case LTTNG_EVENT_SPLICE
:
962 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
964 case LTTNG_EVENT_MMAP
:
965 channel
->output
= CONSUMER_CHANNEL_MMAP
;
975 * In monitor mode, the streams associated with the channel will be put in
976 * a special list ONLY owned by this channel. So, the refcount is set to 1
977 * here meaning that the channel itself has streams that are referenced.
979 * On a channel deletion, once the channel is no longer visible, the
980 * refcount is decremented and checked for a zero value to delete it. With
981 * streams in no monitor mode, it will now be safe to destroy the channel.
983 if (!channel
->monitor
) {
984 channel
->refcount
= 1;
987 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
988 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
990 strncpy(channel
->name
, name
, sizeof(channel
->name
));
991 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
993 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
995 channel
->wait_fd
= -1;
997 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
999 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
1006 * Add a channel to the global list protected by a mutex.
1008 * Always return 0 indicating success.
1010 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1011 struct lttng_consumer_local_data
*ctx
)
1013 pthread_mutex_lock(&consumer_data
.lock
);
1014 pthread_mutex_lock(&channel
->lock
);
1015 pthread_mutex_lock(&channel
->timer_lock
);
1018 * This gives us a guarantee that the channel we are about to add to the
1019 * channel hash table will be unique. See this function comment on the why
1020 * we need to steel the channel key at this stage.
1022 steal_channel_key(channel
->key
);
1025 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1028 pthread_mutex_unlock(&channel
->timer_lock
);
1029 pthread_mutex_unlock(&channel
->lock
);
1030 pthread_mutex_unlock(&consumer_data
.lock
);
1032 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1033 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1040 * Allocate the pollfd structure and the local view of the out fds to avoid
1041 * doing a lookup in the linked list and concurrency issues when writing is
1042 * needed. Called with consumer_data.lock held.
1044 * Returns the number of fds in the structures.
1046 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1047 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1048 struct lttng_ht
*ht
)
1051 struct lttng_ht_iter iter
;
1052 struct lttng_consumer_stream
*stream
;
1057 assert(local_stream
);
1059 DBG("Updating poll fd array");
1061 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1063 * Only active streams with an active end point can be added to the
1064 * poll set and local stream storage of the thread.
1066 * There is a potential race here for endpoint_status to be updated
1067 * just after the check. However, this is OK since the stream(s) will
1068 * be deleted once the thread is notified that the end point state has
1069 * changed where this function will be called back again.
1071 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
1072 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1076 * This clobbers way too much the debug output. Uncomment that if you
1077 * need it for debugging purposes.
1079 * DBG("Active FD %d", stream->wait_fd);
1081 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1082 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1083 local_stream
[i
] = stream
;
1089 * Insert the consumer_data_pipe at the end of the array and don't
1090 * increment i so nb_fd is the number of real FD.
1092 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1093 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1095 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1096 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1101 * Poll on the should_quit pipe and the command socket return -1 on
1102 * error, 1 if should exit, 0 if data is available on the command socket
1104 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1109 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1110 if (num_rdy
== -1) {
1112 * Restart interrupted system call.
1114 if (errno
== EINTR
) {
1117 PERROR("Poll error");
1120 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1121 DBG("consumer_should_quit wake up");
1128 * Set the error socket.
1130 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1133 ctx
->consumer_error_socket
= sock
;
1137 * Set the command socket path.
1139 void lttng_consumer_set_command_sock_path(
1140 struct lttng_consumer_local_data
*ctx
, char *sock
)
1142 ctx
->consumer_command_sock_path
= sock
;
1146 * Send return code to the session daemon.
1147 * If the socket is not defined, we return 0, it is not a fatal error
1149 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1151 if (ctx
->consumer_error_socket
> 0) {
1152 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1153 sizeof(enum lttcomm_sessiond_command
));
1160 * Close all the tracefiles and stream fds and MUST be called when all
1161 * instances are destroyed i.e. when all threads were joined and are ended.
1163 void lttng_consumer_cleanup(void)
1165 struct lttng_ht_iter iter
;
1166 struct lttng_consumer_channel
*channel
;
1170 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1172 consumer_del_channel(channel
);
1177 lttng_ht_destroy(consumer_data
.channel_ht
);
1179 cleanup_relayd_ht();
1181 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1184 * This HT contains streams that are freed by either the metadata thread or
1185 * the data thread so we do *nothing* on the hash table and simply destroy
1188 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1192 * Called from signal handler.
1194 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1199 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1201 PERROR("write consumer quit");
1204 DBG("Consumer flag that it should quit");
1207 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1210 int outfd
= stream
->out_fd
;
1213 * This does a blocking write-and-wait on any page that belongs to the
1214 * subbuffer prior to the one we just wrote.
1215 * Don't care about error values, as these are just hints and ways to
1216 * limit the amount of page cache used.
1218 if (orig_offset
< stream
->max_sb_size
) {
1221 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1222 stream
->max_sb_size
,
1223 SYNC_FILE_RANGE_WAIT_BEFORE
1224 | SYNC_FILE_RANGE_WRITE
1225 | SYNC_FILE_RANGE_WAIT_AFTER
);
1227 * Give hints to the kernel about how we access the file:
1228 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1231 * We need to call fadvise again after the file grows because the
1232 * kernel does not seem to apply fadvise to non-existing parts of the
1235 * Call fadvise _after_ having waited for the page writeback to
1236 * complete because the dirty page writeback semantic is not well
1237 * defined. So it can be expected to lead to lower throughput in
1240 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1241 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1245 * Initialise the necessary environnement :
1246 * - create a new context
1247 * - create the poll_pipe
1248 * - create the should_quit pipe (for signal handler)
1249 * - create the thread pipe (for splice)
1251 * Takes a function pointer as argument, this function is called when data is
1252 * available on a buffer. This function is responsible to do the
1253 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1254 * buffer configuration and then kernctl_put_next_subbuf at the end.
1256 * Returns a pointer to the new context or NULL on error.
1258 struct lttng_consumer_local_data
*lttng_consumer_create(
1259 enum lttng_consumer_type type
,
1260 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1261 struct lttng_consumer_local_data
*ctx
),
1262 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1263 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1264 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1267 struct lttng_consumer_local_data
*ctx
;
1269 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1270 consumer_data
.type
== type
);
1271 consumer_data
.type
= type
;
1273 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1275 PERROR("allocating context");
1279 ctx
->consumer_error_socket
= -1;
1280 ctx
->consumer_metadata_socket
= -1;
1281 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1282 /* assign the callbacks */
1283 ctx
->on_buffer_ready
= buffer_ready
;
1284 ctx
->on_recv_channel
= recv_channel
;
1285 ctx
->on_recv_stream
= recv_stream
;
1286 ctx
->on_update_stream
= update_stream
;
1288 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1289 if (!ctx
->consumer_data_pipe
) {
1290 goto error_poll_pipe
;
1293 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1294 if (!ctx
->consumer_wakeup_pipe
) {
1295 goto error_wakeup_pipe
;
1298 ret
= pipe(ctx
->consumer_should_quit
);
1300 PERROR("Error creating recv pipe");
1301 goto error_quit_pipe
;
1304 ret
= pipe(ctx
->consumer_thread_pipe
);
1306 PERROR("Error creating thread pipe");
1307 goto error_thread_pipe
;
1310 ret
= pipe(ctx
->consumer_channel_pipe
);
1312 PERROR("Error creating channel pipe");
1313 goto error_channel_pipe
;
1316 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1317 if (!ctx
->consumer_metadata_pipe
) {
1318 goto error_metadata_pipe
;
1321 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1323 goto error_splice_pipe
;
1329 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1330 error_metadata_pipe
:
1331 utils_close_pipe(ctx
->consumer_channel_pipe
);
1333 utils_close_pipe(ctx
->consumer_thread_pipe
);
1335 utils_close_pipe(ctx
->consumer_should_quit
);
1337 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1339 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1347 * Iterate over all streams of the hashtable and free them properly.
1349 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1351 struct lttng_ht_iter iter
;
1352 struct lttng_consumer_stream
*stream
;
1359 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1361 * Ignore return value since we are currently cleaning up so any error
1364 (void) consumer_del_stream(stream
, ht
);
1368 lttng_ht_destroy(ht
);
1372 * Iterate over all streams of the metadata hashtable and free them
1375 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1377 struct lttng_ht_iter iter
;
1378 struct lttng_consumer_stream
*stream
;
1385 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1387 * Ignore return value since we are currently cleaning up so any error
1390 (void) consumer_del_metadata_stream(stream
, ht
);
1394 lttng_ht_destroy(ht
);
1398 * Close all fds associated with the instance and free the context.
1400 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1404 DBG("Consumer destroying it. Closing everything.");
1410 destroy_data_stream_ht(data_ht
);
1411 destroy_metadata_stream_ht(metadata_ht
);
1413 ret
= close(ctx
->consumer_error_socket
);
1417 ret
= close(ctx
->consumer_metadata_socket
);
1421 utils_close_pipe(ctx
->consumer_thread_pipe
);
1422 utils_close_pipe(ctx
->consumer_channel_pipe
);
1423 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1424 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1425 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1426 utils_close_pipe(ctx
->consumer_should_quit
);
1427 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1429 unlink(ctx
->consumer_command_sock_path
);
1434 * Write the metadata stream id on the specified file descriptor.
1436 static int write_relayd_metadata_id(int fd
,
1437 struct lttng_consumer_stream
*stream
,
1438 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1441 struct lttcomm_relayd_metadata_payload hdr
;
1443 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1444 hdr
.padding_size
= htobe32(padding
);
1445 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1446 if (ret
< sizeof(hdr
)) {
1448 * This error means that the fd's end is closed so ignore the perror
1449 * not to clubber the error output since this can happen in a normal
1452 if (errno
!= EPIPE
) {
1453 PERROR("write metadata stream id");
1455 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1457 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1458 * handle writting the missing part so report that as an error and
1459 * don't lie to the caller.
1464 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1465 stream
->relayd_stream_id
, padding
);
1472 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1473 * core function for writing trace buffers to either the local filesystem or
1476 * It must be called with the stream lock held.
1478 * Careful review MUST be put if any changes occur!
1480 * Returns the number of bytes written
1482 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1483 struct lttng_consumer_local_data
*ctx
,
1484 struct lttng_consumer_stream
*stream
, unsigned long len
,
1485 unsigned long padding
,
1486 struct ctf_packet_index
*index
)
1488 unsigned long mmap_offset
;
1491 off_t orig_offset
= stream
->out_fd_offset
;
1492 /* Default is on the disk */
1493 int outfd
= stream
->out_fd
;
1494 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1495 unsigned int relayd_hang_up
= 0;
1497 /* RCU lock for the relayd pointer */
1500 /* Flag that the current stream if set for network streaming. */
1501 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1502 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1503 if (relayd
== NULL
) {
1509 /* get the offset inside the fd to mmap */
1510 switch (consumer_data
.type
) {
1511 case LTTNG_CONSUMER_KERNEL
:
1512 mmap_base
= stream
->mmap_base
;
1513 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1516 PERROR("tracer ctl get_mmap_read_offset");
1520 case LTTNG_CONSUMER32_UST
:
1521 case LTTNG_CONSUMER64_UST
:
1522 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1524 ERR("read mmap get mmap base for stream %s", stream
->name
);
1528 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1530 PERROR("tracer ctl get_mmap_read_offset");
1536 ERR("Unknown consumer_data type");
1540 /* Handle stream on the relayd if the output is on the network */
1542 unsigned long netlen
= len
;
1545 * Lock the control socket for the complete duration of the function
1546 * since from this point on we will use the socket.
1548 if (stream
->metadata_flag
) {
1549 /* Metadata requires the control socket. */
1550 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1551 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1554 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1559 /* Use the returned socket. */
1562 /* Write metadata stream id before payload */
1563 if (stream
->metadata_flag
) {
1564 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1571 /* No streaming, we have to set the len with the full padding */
1575 * Check if we need to change the tracefile before writing the packet.
1577 if (stream
->chan
->tracefile_size
> 0 &&
1578 (stream
->tracefile_size_current
+ len
) >
1579 stream
->chan
->tracefile_size
) {
1580 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1581 stream
->name
, stream
->chan
->tracefile_size
,
1582 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1583 stream
->out_fd
, &(stream
->tracefile_count_current
),
1586 ERR("Rotating output file");
1589 outfd
= stream
->out_fd
;
1591 if (stream
->index_fd
>= 0) {
1592 ret
= index_create_file(stream
->chan
->pathname
,
1593 stream
->name
, stream
->uid
, stream
->gid
,
1594 stream
->chan
->tracefile_size
,
1595 stream
->tracefile_count_current
);
1599 stream
->index_fd
= ret
;
1602 /* Reset current size because we just perform a rotation. */
1603 stream
->tracefile_size_current
= 0;
1604 stream
->out_fd_offset
= 0;
1607 stream
->tracefile_size_current
+= len
;
1609 index
->offset
= htobe64(stream
->out_fd_offset
);
1614 * This call guarantee that len or less is returned. It's impossible to
1615 * receive a ret value that is bigger than len.
1617 ret
= lttng_write(outfd
, mmap_base
+ mmap_offset
, len
);
1618 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1619 if (ret
< 0 || ((size_t) ret
!= len
)) {
1621 * Report error to caller if nothing was written else at least send the
1629 /* Socket operation failed. We consider the relayd dead */
1630 if (errno
== EPIPE
|| errno
== EINVAL
|| errno
== EBADF
) {
1632 * This is possible if the fd is closed on the other side
1633 * (outfd) or any write problem. It can be verbose a bit for a
1634 * normal execution if for instance the relayd is stopped
1635 * abruptly. This can happen so set this to a DBG statement.
1637 DBG("Consumer mmap write detected relayd hang up");
1639 /* Unhandled error, print it and stop function right now. */
1640 PERROR("Error in write mmap (ret %zd != len %lu)", ret
, len
);
1644 stream
->output_written
+= ret
;
1646 /* This call is useless on a socket so better save a syscall. */
1648 /* This won't block, but will start writeout asynchronously */
1649 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, len
,
1650 SYNC_FILE_RANGE_WRITE
);
1651 stream
->out_fd_offset
+= len
;
1653 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1657 * This is a special case that the relayd has closed its socket. Let's
1658 * cleanup the relayd object and all associated streams.
1660 if (relayd
&& relayd_hang_up
) {
1661 cleanup_relayd(relayd
, ctx
);
1665 /* Unlock only if ctrl socket used */
1666 if (relayd
&& stream
->metadata_flag
) {
1667 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1675 * Splice the data from the ring buffer to the tracefile.
1677 * It must be called with the stream lock held.
1679 * Returns the number of bytes spliced.
1681 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1682 struct lttng_consumer_local_data
*ctx
,
1683 struct lttng_consumer_stream
*stream
, unsigned long len
,
1684 unsigned long padding
,
1685 struct ctf_packet_index
*index
)
1687 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1689 off_t orig_offset
= stream
->out_fd_offset
;
1690 int fd
= stream
->wait_fd
;
1691 /* Default is on the disk */
1692 int outfd
= stream
->out_fd
;
1693 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1695 unsigned int relayd_hang_up
= 0;
1697 switch (consumer_data
.type
) {
1698 case LTTNG_CONSUMER_KERNEL
:
1700 case LTTNG_CONSUMER32_UST
:
1701 case LTTNG_CONSUMER64_UST
:
1702 /* Not supported for user space tracing */
1705 ERR("Unknown consumer_data type");
1709 /* RCU lock for the relayd pointer */
1712 /* Flag that the current stream if set for network streaming. */
1713 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1714 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1715 if (relayd
== NULL
) {
1722 * Choose right pipe for splice. Metadata and trace data are handled by
1723 * different threads hence the use of two pipes in order not to race or
1724 * corrupt the written data.
1726 if (stream
->metadata_flag
) {
1727 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1729 splice_pipe
= ctx
->consumer_thread_pipe
;
1732 /* Write metadata stream id before payload */
1734 unsigned long total_len
= len
;
1736 if (stream
->metadata_flag
) {
1738 * Lock the control socket for the complete duration of the function
1739 * since from this point on we will use the socket.
1741 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1743 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1751 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1754 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1760 /* Use the returned socket. */
1763 /* No streaming, we have to set the len with the full padding */
1767 * Check if we need to change the tracefile before writing the packet.
1769 if (stream
->chan
->tracefile_size
> 0 &&
1770 (stream
->tracefile_size_current
+ len
) >
1771 stream
->chan
->tracefile_size
) {
1772 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1773 stream
->name
, stream
->chan
->tracefile_size
,
1774 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1775 stream
->out_fd
, &(stream
->tracefile_count_current
),
1779 ERR("Rotating output file");
1782 outfd
= stream
->out_fd
;
1784 if (stream
->index_fd
>= 0) {
1785 ret
= index_create_file(stream
->chan
->pathname
,
1786 stream
->name
, stream
->uid
, stream
->gid
,
1787 stream
->chan
->tracefile_size
,
1788 stream
->tracefile_count_current
);
1793 stream
->index_fd
= ret
;
1796 /* Reset current size because we just perform a rotation. */
1797 stream
->tracefile_size_current
= 0;
1798 stream
->out_fd_offset
= 0;
1801 stream
->tracefile_size_current
+= len
;
1802 index
->offset
= htobe64(stream
->out_fd_offset
);
1806 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1807 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1808 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1809 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1810 DBG("splice chan to pipe, ret %zd", ret_splice
);
1811 if (ret_splice
< 0) {
1814 PERROR("Error in relay splice");
1818 /* Handle stream on the relayd if the output is on the network */
1819 if (relayd
&& stream
->metadata_flag
) {
1820 size_t metadata_payload_size
=
1821 sizeof(struct lttcomm_relayd_metadata_payload
);
1823 /* Update counter to fit the spliced data */
1824 ret_splice
+= metadata_payload_size
;
1825 len
+= metadata_payload_size
;
1827 * We do this so the return value can match the len passed as
1828 * argument to this function.
1830 written
-= metadata_payload_size
;
1833 /* Splice data out */
1834 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1835 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1836 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1837 if (ret_splice
< 0) {
1842 } else if (ret_splice
> len
) {
1844 * We don't expect this code path to be executed but you never know
1845 * so this is an extra protection agains a buggy splice().
1848 written
+= ret_splice
;
1849 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1853 /* All good, update current len and continue. */
1857 /* This call is useless on a socket so better save a syscall. */
1859 /* This won't block, but will start writeout asynchronously */
1860 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1861 SYNC_FILE_RANGE_WRITE
);
1862 stream
->out_fd_offset
+= ret_splice
;
1864 stream
->output_written
+= ret_splice
;
1865 written
+= ret_splice
;
1867 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1872 * This is a special case that the relayd has closed its socket. Let's
1873 * cleanup the relayd object and all associated streams.
1875 if (relayd
&& relayd_hang_up
) {
1876 cleanup_relayd(relayd
, ctx
);
1877 /* Skip splice error so the consumer does not fail */
1882 /* send the appropriate error description to sessiond */
1885 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1888 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1891 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1896 if (relayd
&& stream
->metadata_flag
) {
1897 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1905 * Take a snapshot for a specific fd
1907 * Returns 0 on success, < 0 on error
1909 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1911 switch (consumer_data
.type
) {
1912 case LTTNG_CONSUMER_KERNEL
:
1913 return lttng_kconsumer_take_snapshot(stream
);
1914 case LTTNG_CONSUMER32_UST
:
1915 case LTTNG_CONSUMER64_UST
:
1916 return lttng_ustconsumer_take_snapshot(stream
);
1918 ERR("Unknown consumer_data type");
1925 * Get the produced position
1927 * Returns 0 on success, < 0 on error
1929 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1932 switch (consumer_data
.type
) {
1933 case LTTNG_CONSUMER_KERNEL
:
1934 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1935 case LTTNG_CONSUMER32_UST
:
1936 case LTTNG_CONSUMER64_UST
:
1937 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1939 ERR("Unknown consumer_data type");
1945 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1946 int sock
, struct pollfd
*consumer_sockpoll
)
1948 switch (consumer_data
.type
) {
1949 case LTTNG_CONSUMER_KERNEL
:
1950 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1951 case LTTNG_CONSUMER32_UST
:
1952 case LTTNG_CONSUMER64_UST
:
1953 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1955 ERR("Unknown consumer_data type");
1961 void lttng_consumer_close_all_metadata(void)
1963 switch (consumer_data
.type
) {
1964 case LTTNG_CONSUMER_KERNEL
:
1966 * The Kernel consumer has a different metadata scheme so we don't
1967 * close anything because the stream will be closed by the session
1971 case LTTNG_CONSUMER32_UST
:
1972 case LTTNG_CONSUMER64_UST
:
1974 * Close all metadata streams. The metadata hash table is passed and
1975 * this call iterates over it by closing all wakeup fd. This is safe
1976 * because at this point we are sure that the metadata producer is
1977 * either dead or blocked.
1979 lttng_ustconsumer_close_all_metadata(metadata_ht
);
1982 ERR("Unknown consumer_data type");
1988 * Clean up a metadata stream and free its memory.
1990 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1991 struct lttng_ht
*ht
)
1993 struct lttng_consumer_channel
*free_chan
= NULL
;
1997 * This call should NEVER receive regular stream. It must always be
1998 * metadata stream and this is crucial for data structure synchronization.
2000 assert(stream
->metadata_flag
);
2002 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2004 pthread_mutex_lock(&consumer_data
.lock
);
2005 pthread_mutex_lock(&stream
->chan
->lock
);
2006 pthread_mutex_lock(&stream
->lock
);
2008 /* Remove any reference to that stream. */
2009 consumer_stream_delete(stream
, ht
);
2011 /* Close down everything including the relayd if one. */
2012 consumer_stream_close(stream
);
2013 /* Destroy tracer buffers of the stream. */
2014 consumer_stream_destroy_buffers(stream
);
2016 /* Atomically decrement channel refcount since other threads can use it. */
2017 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
2018 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
2019 /* Go for channel deletion! */
2020 free_chan
= stream
->chan
;
2024 * Nullify the stream reference so it is not used after deletion. The
2025 * channel lock MUST be acquired before being able to check for a NULL
2028 stream
->chan
->metadata_stream
= NULL
;
2030 pthread_mutex_unlock(&stream
->lock
);
2031 pthread_mutex_unlock(&stream
->chan
->lock
);
2032 pthread_mutex_unlock(&consumer_data
.lock
);
2035 consumer_del_channel(free_chan
);
2038 consumer_stream_free(stream
);
2042 * Action done with the metadata stream when adding it to the consumer internal
2043 * data structures to handle it.
2045 int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2047 struct lttng_ht
*ht
= metadata_ht
;
2049 struct lttng_ht_iter iter
;
2050 struct lttng_ht_node_u64
*node
;
2055 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2057 pthread_mutex_lock(&consumer_data
.lock
);
2058 pthread_mutex_lock(&stream
->chan
->lock
);
2059 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2060 pthread_mutex_lock(&stream
->lock
);
2063 * From here, refcounts are updated so be _careful_ when returning an error
2070 * Lookup the stream just to make sure it does not exist in our internal
2071 * state. This should NEVER happen.
2073 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2074 node
= lttng_ht_iter_get_node_u64(&iter
);
2078 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2079 * in terms of destroying the associated channel, because the action that
2080 * causes the count to become 0 also causes a stream to be added. The
2081 * channel deletion will thus be triggered by the following removal of this
2084 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2085 /* Increment refcount before decrementing nb_init_stream_left */
2087 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2090 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2092 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2093 &stream
->node_channel_id
);
2096 * Add stream to the stream_list_ht of the consumer data. No need to steal
2097 * the key since the HT does not use it and we allow to add redundant keys
2100 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2104 pthread_mutex_unlock(&stream
->lock
);
2105 pthread_mutex_unlock(&stream
->chan
->lock
);
2106 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2107 pthread_mutex_unlock(&consumer_data
.lock
);
2112 * Delete data stream that are flagged for deletion (endpoint_status).
2114 static void validate_endpoint_status_data_stream(void)
2116 struct lttng_ht_iter iter
;
2117 struct lttng_consumer_stream
*stream
;
2119 DBG("Consumer delete flagged data stream");
2122 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2123 /* Validate delete flag of the stream */
2124 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2127 /* Delete it right now */
2128 consumer_del_stream(stream
, data_ht
);
2134 * Delete metadata stream that are flagged for deletion (endpoint_status).
2136 static void validate_endpoint_status_metadata_stream(
2137 struct lttng_poll_event
*pollset
)
2139 struct lttng_ht_iter iter
;
2140 struct lttng_consumer_stream
*stream
;
2142 DBG("Consumer delete flagged metadata stream");
2147 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2148 /* Validate delete flag of the stream */
2149 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2153 * Remove from pollset so the metadata thread can continue without
2154 * blocking on a deleted stream.
2156 lttng_poll_del(pollset
, stream
->wait_fd
);
2158 /* Delete it right now */
2159 consumer_del_metadata_stream(stream
, metadata_ht
);
2165 * Thread polls on metadata file descriptor and write them on disk or on the
2168 void *consumer_thread_metadata_poll(void *data
)
2170 int ret
, i
, pollfd
, err
= -1;
2171 uint32_t revents
, nb_fd
;
2172 struct lttng_consumer_stream
*stream
= NULL
;
2173 struct lttng_ht_iter iter
;
2174 struct lttng_ht_node_u64
*node
;
2175 struct lttng_poll_event events
;
2176 struct lttng_consumer_local_data
*ctx
= data
;
2179 rcu_register_thread();
2181 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2183 if (testpoint(consumerd_thread_metadata
)) {
2184 goto error_testpoint
;
2187 health_code_update();
2189 DBG("Thread metadata poll started");
2191 /* Size is set to 1 for the consumer_metadata pipe */
2192 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2194 ERR("Poll set creation failed");
2198 ret
= lttng_poll_add(&events
,
2199 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2205 DBG("Metadata main loop started");
2208 health_code_update();
2210 /* Only the metadata pipe is set */
2211 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2212 err
= 0; /* All is OK */
2217 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2218 health_poll_entry();
2219 ret
= lttng_poll_wait(&events
, -1);
2221 DBG("Metadata event catched in thread");
2223 if (errno
== EINTR
) {
2224 ERR("Poll EINTR catched");
2232 /* From here, the event is a metadata wait fd */
2233 for (i
= 0; i
< nb_fd
; i
++) {
2234 health_code_update();
2236 revents
= LTTNG_POLL_GETEV(&events
, i
);
2237 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2239 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2240 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2241 DBG("Metadata thread pipe hung up");
2243 * Remove the pipe from the poll set and continue the loop
2244 * since their might be data to consume.
2246 lttng_poll_del(&events
,
2247 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2248 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2250 } else if (revents
& LPOLLIN
) {
2253 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2254 &stream
, sizeof(stream
));
2255 if (pipe_len
< sizeof(stream
)) {
2256 PERROR("read metadata stream");
2258 * Continue here to handle the rest of the streams.
2263 /* A NULL stream means that the state has changed. */
2264 if (stream
== NULL
) {
2265 /* Check for deleted streams. */
2266 validate_endpoint_status_metadata_stream(&events
);
2270 DBG("Adding metadata stream %d to poll set",
2273 /* Add metadata stream to the global poll events list */
2274 lttng_poll_add(&events
, stream
->wait_fd
,
2275 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2278 /* Handle other stream */
2284 uint64_t tmp_id
= (uint64_t) pollfd
;
2286 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2288 node
= lttng_ht_iter_get_node_u64(&iter
);
2291 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2294 /* Check for error event */
2295 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2296 DBG("Metadata fd %d is hup|err.", pollfd
);
2297 if (!stream
->hangup_flush_done
2298 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2299 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2300 DBG("Attempting to flush and consume the UST buffers");
2301 lttng_ustconsumer_on_stream_hangup(stream
);
2303 /* We just flushed the stream now read it. */
2305 health_code_update();
2307 len
= ctx
->on_buffer_ready(stream
, ctx
);
2309 * We don't check the return value here since if we get
2310 * a negative len, it means an error occured thus we
2311 * simply remove it from the poll set and free the
2317 lttng_poll_del(&events
, stream
->wait_fd
);
2319 * This call update the channel states, closes file descriptors
2320 * and securely free the stream.
2322 consumer_del_metadata_stream(stream
, metadata_ht
);
2323 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2324 /* Get the data out of the metadata file descriptor */
2325 DBG("Metadata available on fd %d", pollfd
);
2326 assert(stream
->wait_fd
== pollfd
);
2329 health_code_update();
2331 len
= ctx
->on_buffer_ready(stream
, ctx
);
2333 * We don't check the return value here since if we get
2334 * a negative len, it means an error occured thus we
2335 * simply remove it from the poll set and free the
2340 /* It's ok to have an unavailable sub-buffer */
2341 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2342 /* Clean up stream from consumer and free it. */
2343 lttng_poll_del(&events
, stream
->wait_fd
);
2344 consumer_del_metadata_stream(stream
, metadata_ht
);
2348 /* Release RCU lock for the stream looked up */
2357 DBG("Metadata poll thread exiting");
2359 lttng_poll_clean(&events
);
2364 ERR("Health error occurred in %s", __func__
);
2366 health_unregister(health_consumerd
);
2367 rcu_unregister_thread();
2372 * This thread polls the fds in the set to consume the data and write
2373 * it to tracefile if necessary.
2375 void *consumer_thread_data_poll(void *data
)
2377 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2378 struct pollfd
*pollfd
= NULL
;
2379 /* local view of the streams */
2380 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2381 /* local view of consumer_data.fds_count */
2383 struct lttng_consumer_local_data
*ctx
= data
;
2386 rcu_register_thread();
2388 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2390 if (testpoint(consumerd_thread_data
)) {
2391 goto error_testpoint
;
2394 health_code_update();
2396 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2397 if (local_stream
== NULL
) {
2398 PERROR("local_stream malloc");
2403 health_code_update();
2409 * the fds set has been updated, we need to update our
2410 * local array as well
2412 pthread_mutex_lock(&consumer_data
.lock
);
2413 if (consumer_data
.need_update
) {
2418 local_stream
= NULL
;
2421 * Allocate for all fds +1 for the consumer_data_pipe and +1 for
2424 pollfd
= zmalloc((consumer_data
.stream_count
+ 2) * sizeof(struct pollfd
));
2425 if (pollfd
== NULL
) {
2426 PERROR("pollfd malloc");
2427 pthread_mutex_unlock(&consumer_data
.lock
);
2431 local_stream
= zmalloc((consumer_data
.stream_count
+ 2) *
2432 sizeof(struct lttng_consumer_stream
*));
2433 if (local_stream
== NULL
) {
2434 PERROR("local_stream malloc");
2435 pthread_mutex_unlock(&consumer_data
.lock
);
2438 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2441 ERR("Error in allocating pollfd or local_outfds");
2442 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2443 pthread_mutex_unlock(&consumer_data
.lock
);
2447 consumer_data
.need_update
= 0;
2449 pthread_mutex_unlock(&consumer_data
.lock
);
2451 /* No FDs and consumer_quit, consumer_cleanup the thread */
2452 if (nb_fd
== 0 && consumer_quit
== 1) {
2453 err
= 0; /* All is OK */
2456 /* poll on the array of fds */
2458 DBG("polling on %d fd", nb_fd
+ 2);
2459 health_poll_entry();
2460 num_rdy
= poll(pollfd
, nb_fd
+ 2, -1);
2462 DBG("poll num_rdy : %d", num_rdy
);
2463 if (num_rdy
== -1) {
2465 * Restart interrupted system call.
2467 if (errno
== EINTR
) {
2470 PERROR("Poll error");
2471 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2473 } else if (num_rdy
== 0) {
2474 DBG("Polling thread timed out");
2479 * If the consumer_data_pipe triggered poll go directly to the
2480 * beginning of the loop to update the array. We want to prioritize
2481 * array update over low-priority reads.
2483 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2484 ssize_t pipe_readlen
;
2486 DBG("consumer_data_pipe wake up");
2487 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2488 &new_stream
, sizeof(new_stream
));
2489 if (pipe_readlen
< sizeof(new_stream
)) {
2490 PERROR("Consumer data pipe");
2491 /* Continue so we can at least handle the current stream(s). */
2496 * If the stream is NULL, just ignore it. It's also possible that
2497 * the sessiond poll thread changed the consumer_quit state and is
2498 * waking us up to test it.
2500 if (new_stream
== NULL
) {
2501 validate_endpoint_status_data_stream();
2505 /* Continue to update the local streams and handle prio ones */
2509 /* Handle wakeup pipe. */
2510 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2512 ssize_t pipe_readlen
;
2514 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2516 if (pipe_readlen
< 0) {
2517 PERROR("Consumer data wakeup pipe");
2519 /* We've been awakened to handle stream(s). */
2520 ctx
->has_wakeup
= 0;
2523 /* Take care of high priority channels first. */
2524 for (i
= 0; i
< nb_fd
; i
++) {
2525 health_code_update();
2527 if (local_stream
[i
] == NULL
) {
2530 if (pollfd
[i
].revents
& POLLPRI
) {
2531 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2533 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2534 /* it's ok to have an unavailable sub-buffer */
2535 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2536 /* Clean the stream and free it. */
2537 consumer_del_stream(local_stream
[i
], data_ht
);
2538 local_stream
[i
] = NULL
;
2539 } else if (len
> 0) {
2540 local_stream
[i
]->data_read
= 1;
2546 * If we read high prio channel in this loop, try again
2547 * for more high prio data.
2553 /* Take care of low priority channels. */
2554 for (i
= 0; i
< nb_fd
; i
++) {
2555 health_code_update();
2557 if (local_stream
[i
] == NULL
) {
2560 if ((pollfd
[i
].revents
& POLLIN
) ||
2561 local_stream
[i
]->hangup_flush_done
||
2562 local_stream
[i
]->has_data
) {
2563 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2564 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2565 /* it's ok to have an unavailable sub-buffer */
2566 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2567 /* Clean the stream and free it. */
2568 consumer_del_stream(local_stream
[i
], data_ht
);
2569 local_stream
[i
] = NULL
;
2570 } else if (len
> 0) {
2571 local_stream
[i
]->data_read
= 1;
2576 /* Handle hangup and errors */
2577 for (i
= 0; i
< nb_fd
; i
++) {
2578 health_code_update();
2580 if (local_stream
[i
] == NULL
) {
2583 if (!local_stream
[i
]->hangup_flush_done
2584 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2585 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2586 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2587 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2589 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2590 /* Attempt read again, for the data we just flushed. */
2591 local_stream
[i
]->data_read
= 1;
2594 * If the poll flag is HUP/ERR/NVAL and we have
2595 * read no data in this pass, we can remove the
2596 * stream from its hash table.
2598 if ((pollfd
[i
].revents
& POLLHUP
)) {
2599 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2600 if (!local_stream
[i
]->data_read
) {
2601 consumer_del_stream(local_stream
[i
], data_ht
);
2602 local_stream
[i
] = NULL
;
2605 } else if (pollfd
[i
].revents
& POLLERR
) {
2606 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2607 if (!local_stream
[i
]->data_read
) {
2608 consumer_del_stream(local_stream
[i
], data_ht
);
2609 local_stream
[i
] = NULL
;
2612 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2613 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2614 if (!local_stream
[i
]->data_read
) {
2615 consumer_del_stream(local_stream
[i
], data_ht
);
2616 local_stream
[i
] = NULL
;
2620 if (local_stream
[i
] != NULL
) {
2621 local_stream
[i
]->data_read
= 0;
2628 DBG("polling thread exiting");
2633 * Close the write side of the pipe so epoll_wait() in
2634 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2635 * read side of the pipe. If we close them both, epoll_wait strangely does
2636 * not return and could create a endless wait period if the pipe is the
2637 * only tracked fd in the poll set. The thread will take care of closing
2640 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2645 ERR("Health error occurred in %s", __func__
);
2647 health_unregister(health_consumerd
);
2649 rcu_unregister_thread();
2654 * Close wake-up end of each stream belonging to the channel. This will
2655 * allow the poll() on the stream read-side to detect when the
2656 * write-side (application) finally closes them.
2659 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2661 struct lttng_ht
*ht
;
2662 struct lttng_consumer_stream
*stream
;
2663 struct lttng_ht_iter iter
;
2665 ht
= consumer_data
.stream_per_chan_id_ht
;
2668 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2669 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2670 ht
->match_fct
, &channel
->key
,
2671 &iter
.iter
, stream
, node_channel_id
.node
) {
2673 * Protect against teardown with mutex.
2675 pthread_mutex_lock(&stream
->lock
);
2676 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2679 switch (consumer_data
.type
) {
2680 case LTTNG_CONSUMER_KERNEL
:
2682 case LTTNG_CONSUMER32_UST
:
2683 case LTTNG_CONSUMER64_UST
:
2684 if (stream
->metadata_flag
) {
2685 /* Safe and protected by the stream lock. */
2686 lttng_ustconsumer_close_metadata(stream
->chan
);
2689 * Note: a mutex is taken internally within
2690 * liblttng-ust-ctl to protect timer wakeup_fd
2691 * use from concurrent close.
2693 lttng_ustconsumer_close_stream_wakeup(stream
);
2697 ERR("Unknown consumer_data type");
2701 pthread_mutex_unlock(&stream
->lock
);
2706 static void destroy_channel_ht(struct lttng_ht
*ht
)
2708 struct lttng_ht_iter iter
;
2709 struct lttng_consumer_channel
*channel
;
2717 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2718 ret
= lttng_ht_del(ht
, &iter
);
2723 lttng_ht_destroy(ht
);
2727 * This thread polls the channel fds to detect when they are being
2728 * closed. It closes all related streams if the channel is detected as
2729 * closed. It is currently only used as a shim layer for UST because the
2730 * consumerd needs to keep the per-stream wakeup end of pipes open for
2733 void *consumer_thread_channel_poll(void *data
)
2735 int ret
, i
, pollfd
, err
= -1;
2736 uint32_t revents
, nb_fd
;
2737 struct lttng_consumer_channel
*chan
= NULL
;
2738 struct lttng_ht_iter iter
;
2739 struct lttng_ht_node_u64
*node
;
2740 struct lttng_poll_event events
;
2741 struct lttng_consumer_local_data
*ctx
= data
;
2742 struct lttng_ht
*channel_ht
;
2744 rcu_register_thread();
2746 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2748 if (testpoint(consumerd_thread_channel
)) {
2749 goto error_testpoint
;
2752 health_code_update();
2754 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2756 /* ENOMEM at this point. Better to bail out. */
2760 DBG("Thread channel poll started");
2762 /* Size is set to 1 for the consumer_channel pipe */
2763 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2765 ERR("Poll set creation failed");
2769 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2775 DBG("Channel main loop started");
2778 health_code_update();
2780 /* Only the channel pipe is set */
2781 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2782 err
= 0; /* All is OK */
2787 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2788 health_poll_entry();
2789 ret
= lttng_poll_wait(&events
, -1);
2791 DBG("Channel event catched in thread");
2793 if (errno
== EINTR
) {
2794 ERR("Poll EINTR catched");
2802 /* From here, the event is a channel wait fd */
2803 for (i
= 0; i
< nb_fd
; i
++) {
2804 health_code_update();
2806 revents
= LTTNG_POLL_GETEV(&events
, i
);
2807 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2809 /* Just don't waste time if no returned events for the fd */
2813 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2814 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2815 DBG("Channel thread pipe hung up");
2817 * Remove the pipe from the poll set and continue the loop
2818 * since their might be data to consume.
2820 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2822 } else if (revents
& LPOLLIN
) {
2823 enum consumer_channel_action action
;
2826 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2828 ERR("Error reading channel pipe");
2833 case CONSUMER_CHANNEL_ADD
:
2834 DBG("Adding channel %d to poll set",
2837 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2840 lttng_ht_add_unique_u64(channel_ht
,
2841 &chan
->wait_fd_node
);
2843 /* Add channel to the global poll events list */
2844 lttng_poll_add(&events
, chan
->wait_fd
,
2845 LPOLLIN
| LPOLLPRI
);
2847 case CONSUMER_CHANNEL_DEL
:
2850 * This command should never be called if the channel
2851 * has streams monitored by either the data or metadata
2852 * thread. The consumer only notify this thread with a
2853 * channel del. command if it receives a destroy
2854 * channel command from the session daemon that send it
2855 * if a command prior to the GET_CHANNEL failed.
2859 chan
= consumer_find_channel(key
);
2862 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2865 lttng_poll_del(&events
, chan
->wait_fd
);
2866 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2867 ret
= lttng_ht_del(channel_ht
, &iter
);
2870 switch (consumer_data
.type
) {
2871 case LTTNG_CONSUMER_KERNEL
:
2873 case LTTNG_CONSUMER32_UST
:
2874 case LTTNG_CONSUMER64_UST
:
2875 health_code_update();
2876 /* Destroy streams that might have been left in the stream list. */
2877 clean_channel_stream_list(chan
);
2880 ERR("Unknown consumer_data type");
2885 * Release our own refcount. Force channel deletion even if
2886 * streams were not initialized.
2888 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2889 consumer_del_channel(chan
);
2894 case CONSUMER_CHANNEL_QUIT
:
2896 * Remove the pipe from the poll set and continue the loop
2897 * since their might be data to consume.
2899 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2902 ERR("Unknown action");
2907 /* Handle other stream */
2913 uint64_t tmp_id
= (uint64_t) pollfd
;
2915 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2917 node
= lttng_ht_iter_get_node_u64(&iter
);
2920 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2923 /* Check for error event */
2924 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2925 DBG("Channel fd %d is hup|err.", pollfd
);
2927 lttng_poll_del(&events
, chan
->wait_fd
);
2928 ret
= lttng_ht_del(channel_ht
, &iter
);
2932 * This will close the wait fd for each stream associated to
2933 * this channel AND monitored by the data/metadata thread thus
2934 * will be clean by the right thread.
2936 consumer_close_channel_streams(chan
);
2938 /* Release our own refcount */
2939 if (!uatomic_sub_return(&chan
->refcount
, 1)
2940 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2941 consumer_del_channel(chan
);
2945 /* Release RCU lock for the channel looked up */
2953 lttng_poll_clean(&events
);
2955 destroy_channel_ht(channel_ht
);
2958 DBG("Channel poll thread exiting");
2961 ERR("Health error occurred in %s", __func__
);
2963 health_unregister(health_consumerd
);
2964 rcu_unregister_thread();
2968 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2969 struct pollfd
*sockpoll
, int client_socket
)
2976 ret
= lttng_consumer_poll_socket(sockpoll
);
2980 DBG("Metadata connection on client_socket");
2982 /* Blocking call, waiting for transmission */
2983 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2984 if (ctx
->consumer_metadata_socket
< 0) {
2985 WARN("On accept metadata");
2996 * This thread listens on the consumerd socket and receives the file
2997 * descriptors from the session daemon.
2999 void *consumer_thread_sessiond_poll(void *data
)
3001 int sock
= -1, client_socket
, ret
, err
= -1;
3003 * structure to poll for incoming data on communication socket avoids
3004 * making blocking sockets.
3006 struct pollfd consumer_sockpoll
[2];
3007 struct lttng_consumer_local_data
*ctx
= data
;
3009 rcu_register_thread();
3011 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3013 if (testpoint(consumerd_thread_sessiond
)) {
3014 goto error_testpoint
;
3017 health_code_update();
3019 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3020 unlink(ctx
->consumer_command_sock_path
);
3021 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3022 if (client_socket
< 0) {
3023 ERR("Cannot create command socket");
3027 ret
= lttcomm_listen_unix_sock(client_socket
);
3032 DBG("Sending ready command to lttng-sessiond");
3033 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3034 /* return < 0 on error, but == 0 is not fatal */
3036 ERR("Error sending ready command to lttng-sessiond");
3040 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3041 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3042 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3043 consumer_sockpoll
[1].fd
= client_socket
;
3044 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3046 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3054 DBG("Connection on client_socket");
3056 /* Blocking call, waiting for transmission */
3057 sock
= lttcomm_accept_unix_sock(client_socket
);
3064 * Setup metadata socket which is the second socket connection on the
3065 * command unix socket.
3067 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3076 /* This socket is not useful anymore. */
3077 ret
= close(client_socket
);
3079 PERROR("close client_socket");
3083 /* update the polling structure to poll on the established socket */
3084 consumer_sockpoll
[1].fd
= sock
;
3085 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3088 health_code_update();
3090 health_poll_entry();
3091 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3100 DBG("Incoming command on sock");
3101 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3104 * This could simply be a session daemon quitting. Don't output
3107 DBG("Communication interrupted on command socket");
3111 if (consumer_quit
) {
3112 DBG("consumer_thread_receive_fds received quit from signal");
3113 err
= 0; /* All is OK */
3116 DBG("received command on sock");
3122 DBG("Consumer thread sessiond poll exiting");
3125 * Close metadata streams since the producer is the session daemon which
3128 * NOTE: for now, this only applies to the UST tracer.
3130 lttng_consumer_close_all_metadata();
3133 * when all fds have hung up, the polling thread
3139 * Notify the data poll thread to poll back again and test the
3140 * consumer_quit state that we just set so to quit gracefully.
3142 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3144 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3146 notify_health_quit_pipe(health_quit_pipe
);
3148 /* Cleaning up possibly open sockets. */
3152 PERROR("close sock sessiond poll");
3155 if (client_socket
>= 0) {
3156 ret
= close(client_socket
);
3158 PERROR("close client_socket sessiond poll");
3165 ERR("Health error occurred in %s", __func__
);
3167 health_unregister(health_consumerd
);
3169 rcu_unregister_thread();
3173 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3174 struct lttng_consumer_local_data
*ctx
)
3178 pthread_mutex_lock(&stream
->lock
);
3179 if (stream
->metadata_flag
) {
3180 pthread_mutex_lock(&stream
->metadata_rdv_lock
);
3183 switch (consumer_data
.type
) {
3184 case LTTNG_CONSUMER_KERNEL
:
3185 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3187 case LTTNG_CONSUMER32_UST
:
3188 case LTTNG_CONSUMER64_UST
:
3189 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3192 ERR("Unknown consumer_data type");
3198 if (stream
->metadata_flag
) {
3199 pthread_cond_broadcast(&stream
->metadata_rdv
);
3200 pthread_mutex_unlock(&stream
->metadata_rdv_lock
);
3202 pthread_mutex_unlock(&stream
->lock
);
3206 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3208 switch (consumer_data
.type
) {
3209 case LTTNG_CONSUMER_KERNEL
:
3210 return lttng_kconsumer_on_recv_stream(stream
);
3211 case LTTNG_CONSUMER32_UST
:
3212 case LTTNG_CONSUMER64_UST
:
3213 return lttng_ustconsumer_on_recv_stream(stream
);
3215 ERR("Unknown consumer_data type");
3222 * Allocate and set consumer data hash tables.
3224 int lttng_consumer_init(void)
3226 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3227 if (!consumer_data
.channel_ht
) {
3231 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3232 if (!consumer_data
.relayd_ht
) {
3236 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3237 if (!consumer_data
.stream_list_ht
) {
3241 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3242 if (!consumer_data
.stream_per_chan_id_ht
) {
3246 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3251 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3263 * Process the ADD_RELAYD command receive by a consumer.
3265 * This will create a relayd socket pair and add it to the relayd hash table.
3266 * The caller MUST acquire a RCU read side lock before calling it.
3268 int consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3269 struct lttng_consumer_local_data
*ctx
, int sock
,
3270 struct pollfd
*consumer_sockpoll
,
3271 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3272 uint64_t relayd_session_id
)
3274 int fd
= -1, ret
= -1, relayd_created
= 0;
3275 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3276 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3279 assert(relayd_sock
);
3281 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3283 /* Get relayd reference if exists. */
3284 relayd
= consumer_find_relayd(net_seq_idx
);
3285 if (relayd
== NULL
) {
3286 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3287 /* Not found. Allocate one. */
3288 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3289 if (relayd
== NULL
) {
3291 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3294 relayd
->sessiond_session_id
= sessiond_id
;
3299 * This code path MUST continue to the consumer send status message to
3300 * we can notify the session daemon and continue our work without
3301 * killing everything.
3305 * relayd key should never be found for control socket.
3307 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3310 /* First send a status message before receiving the fds. */
3311 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3313 /* Somehow, the session daemon is not responding anymore. */
3314 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3315 goto error_nosignal
;
3318 /* Poll on consumer socket. */
3319 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3321 /* Needing to exit in the middle of a command: error. */
3322 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3324 goto error_nosignal
;
3327 /* Get relayd socket from session daemon */
3328 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3329 if (ret
!= sizeof(fd
)) {
3331 fd
= -1; /* Just in case it gets set with an invalid value. */
3334 * Failing to receive FDs might indicate a major problem such as
3335 * reaching a fd limit during the receive where the kernel returns a
3336 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3337 * don't take any chances and stop everything.
3339 * XXX: Feature request #558 will fix that and avoid this possible
3340 * issue when reaching the fd limit.
3342 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3343 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3347 /* Copy socket information and received FD */
3348 switch (sock_type
) {
3349 case LTTNG_STREAM_CONTROL
:
3350 /* Copy received lttcomm socket */
3351 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3352 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3353 /* Handle create_sock error. */
3355 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3359 * Close the socket created internally by
3360 * lttcomm_create_sock, so we can replace it by the one
3361 * received from sessiond.
3363 if (close(relayd
->control_sock
.sock
.fd
)) {
3367 /* Assign new file descriptor */
3368 relayd
->control_sock
.sock
.fd
= fd
;
3369 fd
= -1; /* For error path */
3370 /* Assign version values. */
3371 relayd
->control_sock
.major
= relayd_sock
->major
;
3372 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3374 relayd
->relayd_session_id
= relayd_session_id
;
3377 case LTTNG_STREAM_DATA
:
3378 /* Copy received lttcomm socket */
3379 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3380 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3381 /* Handle create_sock error. */
3383 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3387 * Close the socket created internally by
3388 * lttcomm_create_sock, so we can replace it by the one
3389 * received from sessiond.
3391 if (close(relayd
->data_sock
.sock
.fd
)) {
3395 /* Assign new file descriptor */
3396 relayd
->data_sock
.sock
.fd
= fd
;
3397 fd
= -1; /* for eventual error paths */
3398 /* Assign version values. */
3399 relayd
->data_sock
.major
= relayd_sock
->major
;
3400 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3403 ERR("Unknown relayd socket type (%d)", sock_type
);
3405 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3409 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3410 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3411 relayd
->net_seq_idx
, fd
);
3413 /* We successfully added the socket. Send status back. */
3414 ret
= consumer_send_status_msg(sock
, ret_code
);
3416 /* Somehow, the session daemon is not responding anymore. */
3417 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3418 goto error_nosignal
;
3422 * Add relayd socket pair to consumer data hashtable. If object already
3423 * exists or on error, the function gracefully returns.
3431 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3432 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3436 /* Close received socket if valid. */
3439 PERROR("close received socket");
3443 if (relayd_created
) {
3451 * Try to lock the stream mutex.
3453 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3455 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3462 * Try to lock the stream mutex. On failure, we know that the stream is
3463 * being used else where hence there is data still being extracted.
3465 ret
= pthread_mutex_trylock(&stream
->lock
);
3467 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3479 * Search for a relayd associated to the session id and return the reference.
3481 * A rcu read side lock MUST be acquire before calling this function and locked
3482 * until the relayd object is no longer necessary.
3484 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3486 struct lttng_ht_iter iter
;
3487 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3489 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3490 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3493 * Check by sessiond id which is unique here where the relayd session
3494 * id might not be when having multiple relayd.
3496 if (relayd
->sessiond_session_id
== id
) {
3497 /* Found the relayd. There can be only one per id. */
3509 * Check if for a given session id there is still data needed to be extract
3512 * Return 1 if data is pending or else 0 meaning ready to be read.
3514 int consumer_data_pending(uint64_t id
)
3517 struct lttng_ht_iter iter
;
3518 struct lttng_ht
*ht
;
3519 struct lttng_consumer_stream
*stream
;
3520 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3521 int (*data_pending
)(struct lttng_consumer_stream
*);
3523 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3526 pthread_mutex_lock(&consumer_data
.lock
);
3528 switch (consumer_data
.type
) {
3529 case LTTNG_CONSUMER_KERNEL
:
3530 data_pending
= lttng_kconsumer_data_pending
;
3532 case LTTNG_CONSUMER32_UST
:
3533 case LTTNG_CONSUMER64_UST
:
3534 data_pending
= lttng_ustconsumer_data_pending
;
3537 ERR("Unknown consumer data type");
3541 /* Ease our life a bit */
3542 ht
= consumer_data
.stream_list_ht
;
3544 relayd
= find_relayd_by_session_id(id
);
3546 /* Send init command for data pending. */
3547 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3548 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3549 relayd
->relayd_session_id
);
3550 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3552 /* Communication error thus the relayd so no data pending. */
3553 goto data_not_pending
;
3557 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3558 ht
->hash_fct(&id
, lttng_ht_seed
),
3560 &iter
.iter
, stream
, node_session_id
.node
) {
3561 /* If this call fails, the stream is being used hence data pending. */
3562 ret
= stream_try_lock(stream
);
3568 * A removed node from the hash table indicates that the stream has
3569 * been deleted thus having a guarantee that the buffers are closed
3570 * on the consumer side. However, data can still be transmitted
3571 * over the network so don't skip the relayd check.
3573 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3576 * An empty output file is not valid. We need at least one packet
3577 * generated per stream, even if it contains no event, so it
3578 * contains at least one packet header.
3580 if (stream
->output_written
== 0) {
3581 pthread_mutex_unlock(&stream
->lock
);
3584 /* Check the stream if there is data in the buffers. */
3585 ret
= data_pending(stream
);
3587 pthread_mutex_unlock(&stream
->lock
);
3594 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3595 if (stream
->metadata_flag
) {
3596 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3597 stream
->relayd_stream_id
);
3599 ret
= relayd_data_pending(&relayd
->control_sock
,
3600 stream
->relayd_stream_id
,
3601 stream
->next_net_seq_num
- 1);
3603 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3605 pthread_mutex_unlock(&stream
->lock
);
3609 pthread_mutex_unlock(&stream
->lock
);
3613 unsigned int is_data_inflight
= 0;
3615 /* Send init command for data pending. */
3616 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3617 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3618 relayd
->relayd_session_id
, &is_data_inflight
);
3619 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3621 goto data_not_pending
;
3623 if (is_data_inflight
) {
3629 * Finding _no_ node in the hash table and no inflight data means that the
3630 * stream(s) have been removed thus data is guaranteed to be available for
3631 * analysis from the trace files.
3635 /* Data is available to be read by a viewer. */
3636 pthread_mutex_unlock(&consumer_data
.lock
);
3641 /* Data is still being extracted from buffers. */
3642 pthread_mutex_unlock(&consumer_data
.lock
);
3648 * Send a ret code status message to the sessiond daemon.
3650 * Return the sendmsg() return value.
3652 int consumer_send_status_msg(int sock
, int ret_code
)
3654 struct lttcomm_consumer_status_msg msg
;
3656 memset(&msg
, 0, sizeof(msg
));
3657 msg
.ret_code
= ret_code
;
3659 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3663 * Send a channel status message to the sessiond daemon.
3665 * Return the sendmsg() return value.
3667 int consumer_send_status_channel(int sock
,
3668 struct lttng_consumer_channel
*channel
)
3670 struct lttcomm_consumer_status_channel msg
;
3674 memset(&msg
, 0, sizeof(msg
));
3676 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3678 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3679 msg
.key
= channel
->key
;
3680 msg
.stream_count
= channel
->streams
.count
;
3683 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3687 * Using a maximum stream size with the produced and consumed position of a
3688 * stream, computes the new consumed position to be as close as possible to the
3689 * maximum possible stream size.
3691 * If maximum stream size is lower than the possible buffer size (produced -
3692 * consumed), the consumed_pos given is returned untouched else the new value
3695 unsigned long consumer_get_consumed_maxsize(unsigned long consumed_pos
,
3696 unsigned long produced_pos
, uint64_t max_stream_size
)
3698 if (max_stream_size
&& max_stream_size
< (produced_pos
- consumed_pos
)) {
3699 /* Offset from the produced position to get the latest buffers. */
3700 return produced_pos
- max_stream_size
;
3703 return consumed_pos
;