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.
20 #include "common/index/ctf-index.h"
28 #include <sys/socket.h>
29 #include <sys/types.h>
34 #include <bin/lttng-consumerd/health-consumerd.h>
35 #include <common/common.h>
36 #include <common/utils.h>
37 #include <common/compat/poll.h>
38 #include <common/compat/endian.h>
39 #include <common/index/index.h>
40 #include <common/kernel-ctl/kernel-ctl.h>
41 #include <common/sessiond-comm/relayd.h>
42 #include <common/sessiond-comm/sessiond-comm.h>
43 #include <common/kernel-consumer/kernel-consumer.h>
44 #include <common/relayd/relayd.h>
45 #include <common/ust-consumer/ust-consumer.h>
46 #include <common/consumer/consumer-timer.h>
47 #include <common/consumer/consumer.h>
48 #include <common/consumer/consumer-stream.h>
49 #include <common/consumer/consumer-testpoint.h>
50 #include <common/align.h>
51 #include <common/consumer/consumer-metadata-cache.h>
53 struct lttng_consumer_global_data consumer_data
= {
56 .type
= LTTNG_CONSUMER_UNKNOWN
,
59 enum consumer_channel_action
{
62 CONSUMER_CHANNEL_QUIT
,
65 struct consumer_channel_msg
{
66 enum consumer_channel_action action
;
67 struct lttng_consumer_channel
*chan
; /* add */
68 uint64_t key
; /* del */
72 * Flag to inform the polling thread to quit when all fd hung up. Updated by
73 * the consumer_thread_receive_fds when it notices that all fds has hung up.
74 * Also updated by the signal handler (consumer_should_exit()). Read by the
77 volatile int consumer_quit
;
80 * Global hash table containing respectively metadata and data streams. The
81 * stream element in this ht should only be updated by the metadata poll thread
82 * for the metadata and the data poll thread for the data.
84 static struct lttng_ht
*metadata_ht
;
85 static struct lttng_ht
*data_ht
;
88 * Notify a thread lttng pipe to poll back again. This usually means that some
89 * global state has changed so we just send back the thread in a poll wait
92 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
94 struct lttng_consumer_stream
*null_stream
= NULL
;
98 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
101 static void notify_health_quit_pipe(int *pipe
)
105 ret
= lttng_write(pipe
[1], "4", 1);
107 PERROR("write consumer health quit");
111 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
112 struct lttng_consumer_channel
*chan
,
114 enum consumer_channel_action action
)
116 struct consumer_channel_msg msg
;
119 memset(&msg
, 0, sizeof(msg
));
124 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
125 if (ret
< sizeof(msg
)) {
126 PERROR("notify_channel_pipe write error");
130 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
133 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
136 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
137 struct lttng_consumer_channel
**chan
,
139 enum consumer_channel_action
*action
)
141 struct consumer_channel_msg msg
;
144 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
145 if (ret
< sizeof(msg
)) {
149 *action
= msg
.action
;
157 * Cleanup the stream list of a channel. Those streams are not yet globally
160 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
162 struct lttng_consumer_stream
*stream
, *stmp
;
166 /* Delete streams that might have been left in the stream list. */
167 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
169 cds_list_del(&stream
->send_node
);
171 * Once a stream is added to this list, the buffers were created so we
172 * have a guarantee that this call will succeed. Setting the monitor
173 * mode to 0 so we don't lock nor try to delete the stream from the
177 consumer_stream_destroy(stream
, NULL
);
182 * Find a stream. The consumer_data.lock must be locked during this
185 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
188 struct lttng_ht_iter iter
;
189 struct lttng_ht_node_u64
*node
;
190 struct lttng_consumer_stream
*stream
= NULL
;
194 /* -1ULL keys are lookup failures */
195 if (key
== (uint64_t) -1ULL) {
201 lttng_ht_lookup(ht
, &key
, &iter
);
202 node
= lttng_ht_iter_get_node_u64(&iter
);
204 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
212 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
214 struct lttng_consumer_stream
*stream
;
217 stream
= find_stream(key
, ht
);
219 stream
->key
= (uint64_t) -1ULL;
221 * We don't want the lookup to match, but we still need
222 * to iterate on this stream when iterating over the hash table. Just
223 * change the node key.
225 stream
->node
.key
= (uint64_t) -1ULL;
231 * Return a channel object for the given key.
233 * RCU read side lock MUST be acquired before calling this function and
234 * protects the channel ptr.
236 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
238 struct lttng_ht_iter iter
;
239 struct lttng_ht_node_u64
*node
;
240 struct lttng_consumer_channel
*channel
= NULL
;
242 /* -1ULL keys are lookup failures */
243 if (key
== (uint64_t) -1ULL) {
247 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
248 node
= lttng_ht_iter_get_node_u64(&iter
);
250 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
257 * There is a possibility that the consumer does not have enough time between
258 * the close of the channel on the session daemon and the cleanup in here thus
259 * once we have a channel add with an existing key, we know for sure that this
260 * channel will eventually get cleaned up by all streams being closed.
262 * This function just nullifies the already existing channel key.
264 static void steal_channel_key(uint64_t key
)
266 struct lttng_consumer_channel
*channel
;
269 channel
= consumer_find_channel(key
);
271 channel
->key
= (uint64_t) -1ULL;
273 * We don't want the lookup to match, but we still need to iterate on
274 * this channel when iterating over the hash table. Just change the
277 channel
->node
.key
= (uint64_t) -1ULL;
282 static void free_channel_rcu(struct rcu_head
*head
)
284 struct lttng_ht_node_u64
*node
=
285 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
286 struct lttng_consumer_channel
*channel
=
287 caa_container_of(node
, struct lttng_consumer_channel
, node
);
289 switch (consumer_data
.type
) {
290 case LTTNG_CONSUMER_KERNEL
:
292 case LTTNG_CONSUMER32_UST
:
293 case LTTNG_CONSUMER64_UST
:
294 lttng_ustconsumer_free_channel(channel
);
297 ERR("Unknown consumer_data type");
304 * RCU protected relayd socket pair free.
306 static void free_relayd_rcu(struct rcu_head
*head
)
308 struct lttng_ht_node_u64
*node
=
309 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
310 struct consumer_relayd_sock_pair
*relayd
=
311 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
314 * Close all sockets. This is done in the call RCU since we don't want the
315 * socket fds to be reassigned thus potentially creating bad state of the
318 * We do not have to lock the control socket mutex here since at this stage
319 * there is no one referencing to this relayd object.
321 (void) relayd_close(&relayd
->control_sock
);
322 (void) relayd_close(&relayd
->data_sock
);
324 pthread_mutex_destroy(&relayd
->ctrl_sock_mutex
);
329 * Destroy and free relayd socket pair object.
331 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
334 struct lttng_ht_iter iter
;
336 if (relayd
== NULL
) {
340 DBG("Consumer destroy and close relayd socket pair");
342 iter
.iter
.node
= &relayd
->node
.node
;
343 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
345 /* We assume the relayd is being or is destroyed */
349 /* RCU free() call */
350 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
354 * Remove a channel from the global list protected by a mutex. This function is
355 * also responsible for freeing its data structures.
357 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
360 struct lttng_ht_iter iter
;
362 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
364 pthread_mutex_lock(&consumer_data
.lock
);
365 pthread_mutex_lock(&channel
->lock
);
367 /* Destroy streams that might have been left in the stream list. */
368 clean_channel_stream_list(channel
);
370 if (channel
->live_timer_enabled
== 1) {
371 consumer_timer_live_stop(channel
);
374 switch (consumer_data
.type
) {
375 case LTTNG_CONSUMER_KERNEL
:
377 case LTTNG_CONSUMER32_UST
:
378 case LTTNG_CONSUMER64_UST
:
379 lttng_ustconsumer_del_channel(channel
);
382 ERR("Unknown consumer_data type");
388 iter
.iter
.node
= &channel
->node
.node
;
389 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
393 call_rcu(&channel
->node
.head
, free_channel_rcu
);
395 pthread_mutex_unlock(&channel
->lock
);
396 pthread_mutex_unlock(&consumer_data
.lock
);
400 * Iterate over the relayd hash table and destroy each element. Finally,
401 * destroy the whole hash table.
403 static void cleanup_relayd_ht(void)
405 struct lttng_ht_iter iter
;
406 struct consumer_relayd_sock_pair
*relayd
;
410 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
412 consumer_destroy_relayd(relayd
);
417 lttng_ht_destroy(consumer_data
.relayd_ht
);
421 * Update the end point status of all streams having the given relayd id.
423 * It's atomically set without having the stream mutex locked which is fine
424 * because we handle the write/read race with a pipe wakeup for each thread.
426 static void update_endpoint_status_by_netidx(uint64_t relayd_id
,
427 enum consumer_endpoint_status status
)
429 struct lttng_ht_iter iter
;
430 struct lttng_consumer_stream
*stream
;
432 DBG("Consumer set delete flag on stream by idx %" PRIu64
, relayd_id
);
436 /* Let's begin with metadata */
437 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
438 if (stream
->relayd_id
== relayd_id
) {
439 uatomic_set(&stream
->endpoint_status
, status
);
440 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
444 /* Follow up by the data streams */
445 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
446 if (stream
->relayd_id
== relayd_id
) {
447 uatomic_set(&stream
->endpoint_status
, status
);
448 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
455 * Cleanup a relayd object by flagging every associated streams for deletion,
456 * destroying the object meaning removing it from the relayd hash table,
457 * closing the sockets and freeing the memory in a RCU call.
459 * If a local data context is available, notify the threads that the streams'
460 * state have changed.
462 void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
)
468 DBG("Cleaning up relayd object ID %"PRIu64
, relayd
->id
);
470 /* Save the net sequence index before destroying the object */
474 * Delete the relayd from the relayd hash table, close the sockets and free
475 * the object in a RCU call.
477 consumer_destroy_relayd(relayd
);
479 /* Set inactive endpoint to all streams */
480 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
483 * With a local data context, notify the threads that the streams' state
484 * have changed. The write() action on the pipe acts as an "implicit"
485 * memory barrier ordering the updates of the end point status from the
486 * read of this status which happens AFTER receiving this notify.
488 notify_thread_lttng_pipe(relayd
->ctx
->consumer_data_pipe
);
489 notify_thread_lttng_pipe(relayd
->ctx
->consumer_metadata_pipe
);
493 * Flag a relayd socket pair for destruction. Destroy it if the refcount
496 * RCU read side lock MUST be aquired before calling this function.
498 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
502 /* Set destroy flag for this object */
503 uatomic_set(&relayd
->destroy_flag
, 1);
505 /* Destroy the relayd if refcount is 0 */
506 if (uatomic_read(&relayd
->refcount
) == 0) {
507 consumer_destroy_relayd(relayd
);
512 * Completly destroy stream from every visiable data structure and the given
515 * One this call returns, the stream object is not longer usable nor visible.
517 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
520 consumer_stream_destroy(stream
, ht
);
524 * XXX naming of del vs destroy is all mixed up.
526 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
528 consumer_stream_destroy(stream
, data_ht
);
531 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
533 consumer_stream_destroy(stream
, metadata_ht
);
537 * Add a stream to the global list protected by a mutex.
539 int consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
541 struct lttng_ht
*ht
= data_ht
;
547 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
549 pthread_mutex_lock(&consumer_data
.lock
);
550 pthread_mutex_lock(&stream
->chan
->lock
);
551 pthread_mutex_lock(&stream
->chan
->timer_lock
);
552 pthread_mutex_lock(&stream
->lock
);
555 /* Steal stream identifier to avoid having streams with the same key */
556 steal_stream_key(stream
->key
, ht
);
558 lttng_ht_add_unique_u64(ht
, &stream
->node
);
560 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
561 &stream
->node_channel_id
);
564 * Add stream to the stream_list_ht of the consumer data. No need to steal
565 * the key since the HT does not use it and we allow to add redundant keys
568 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
571 * When nb_init_stream_left reaches 0, we don't need to trigger any action
572 * in terms of destroying the associated channel, because the action that
573 * causes the count to become 0 also causes a stream to be added. The
574 * channel deletion will thus be triggered by the following removal of this
577 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
578 /* Increment refcount before decrementing nb_init_stream_left */
580 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
583 /* Update consumer data once the node is inserted. */
584 consumer_data
.stream_count
++;
585 consumer_data
.need_update
= 1;
588 pthread_mutex_unlock(&stream
->lock
);
589 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
590 pthread_mutex_unlock(&stream
->chan
->lock
);
591 pthread_mutex_unlock(&consumer_data
.lock
);
596 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
598 consumer_del_stream(stream
, data_ht
);
602 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
603 * be acquired before calling this.
605 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
608 struct lttng_ht_node_u64
*node
;
609 struct lttng_ht_iter iter
;
613 lttng_ht_lookup(consumer_data
.relayd_ht
,
615 node
= lttng_ht_iter_get_node_u64(&iter
);
619 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
626 * Allocate and return a consumer relayd socket.
628 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
631 struct consumer_relayd_sock_pair
*obj
= NULL
;
633 /* net sequence index of -1 is a failure */
634 if (relayd_id
== (uint64_t) -1ULL) {
638 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
640 PERROR("zmalloc relayd sock");
646 obj
->destroy_flag
= 0;
647 obj
->control_sock
.sock
.fd
= -1;
648 obj
->data_sock
.sock
.fd
= -1;
649 lttng_ht_node_init_u64(&obj
->node
, obj
->id
);
650 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
657 * Find a relayd socket pair in the global consumer data.
659 * Return the object if found else NULL.
660 * RCU read-side lock must be held across this call and while using the
663 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
665 struct lttng_ht_iter iter
;
666 struct lttng_ht_node_u64
*node
;
667 struct consumer_relayd_sock_pair
*relayd
= NULL
;
669 /* Negative keys are lookup failures */
670 if (key
== (uint64_t) -1ULL) {
674 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
676 node
= lttng_ht_iter_get_node_u64(&iter
);
678 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
686 * Find a relayd and send the stream
688 * Returns 0 on success, < 0 on error
690 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
694 struct consumer_relayd_sock_pair
*relayd
;
697 assert(stream
->relayd_id
!= -1ULL);
700 /* The stream is not metadata. Get relayd reference if exists. */
702 relayd
= consumer_find_relayd(stream
->relayd_id
);
703 if (relayd
!= NULL
) {
704 /* Add stream on the relayd */
705 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
706 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
707 path
, &stream
->relayd_stream_id
,
708 stream
->chan
->tracefile_size
, stream
->chan
->tracefile_count
);
709 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
711 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".", relayd
->id
);
712 lttng_consumer_cleanup_relayd(relayd
);
716 uatomic_inc(&relayd
->refcount
);
717 stream
->sent_to_relayd
= 1;
719 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
720 stream
->key
, stream
->relayd_id
);
725 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
726 stream
->name
, stream
->key
, stream
->relayd_id
);
733 int consumer_send_relayd_channel_bulk(struct lttng_consumer_channel
*channel
)
736 struct consumer_relayd_sock_pair
*relayd
;
737 struct lttng_consumer_stream
*stream
;
742 relayd
= consumer_find_relayd(channel
->relayd_id
);
744 if (relayd
== NULL
) {
745 ERR("relayd ID %" PRIu64
" unknown. Can't send streams.",
752 * Perform the send part of the relayd_add_stream for all stream.
754 * This ensure that we do not wait for response in between each command
755 * before sending the next one.
757 * This result in a waterfall of send command and a waterfall of recv.
759 * This leverage the TCP order guarantee for send and receive.
761 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
762 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
763 health_code_update();
764 ret
= relayd_add_stream_send(&relayd
->control_sock
, stream
->name
,
765 stream
->chan
->pathname
,
766 stream
->chan
->tracefile_size
, stream
->chan
->tracefile_count
);
768 ERR("Relayd add stream send failed. Cleaning up relayd %" PRIu64
".", relayd
->id
);
769 lttng_consumer_cleanup_relayd(relayd
);
770 goto end_socket_mutex
;
774 /* Perform individual recv part of relayd_add_stream */
775 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
776 health_code_update();
778 ret
= relayd_add_stream_rcv(&relayd
->control_sock
, &stream
->relayd_stream_id
);
780 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".", relayd
->id
);
781 lttng_consumer_cleanup_relayd(relayd
);
782 goto end_socket_mutex
;
785 uatomic_inc(&relayd
->refcount
);
786 stream
->sent_to_relayd
= 1;
788 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
789 stream
->name
, stream
->key
, stream
->relayd_id
);
793 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
800 * Find a relayd and send the streams sent message
802 * Returns 0 on success, < 0 on error
804 int consumer_send_relayd_streams_sent(uint64_t relayd_id
)
807 struct consumer_relayd_sock_pair
*relayd
;
809 assert(relayd_id
!= -1ULL);
811 /* The stream is not metadata. Get relayd reference if exists. */
813 relayd
= consumer_find_relayd(relayd_id
);
814 if (relayd
!= NULL
) {
815 /* Add stream on the relayd */
816 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
817 ret
= relayd_streams_sent(&relayd
->control_sock
);
818 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
820 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64
".", relayd
->id
);
821 lttng_consumer_cleanup_relayd(relayd
);
825 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
832 DBG("All streams sent relayd id %" PRIu64
, relayd_id
);
840 * Find a relayd and close the stream
842 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
844 struct consumer_relayd_sock_pair
*relayd
;
846 /* The stream is not metadata. Get relayd reference if exists. */
848 relayd
= consumer_find_relayd(stream
->relayd_id
);
850 consumer_stream_relayd_close(stream
, relayd
);
856 * Handle stream for relayd transmission if the stream applies for network
857 * streaming where the net sequence index is set.
859 * Return destination file descriptor or negative value on error.
861 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
862 size_t data_size
, unsigned long padding
,
863 struct consumer_relayd_sock_pair
*relayd
)
866 struct lttcomm_relayd_data_hdr data_hdr
;
872 /* Reset data header */
873 memset(&data_hdr
, 0, sizeof(data_hdr
));
875 if (stream
->metadata_flag
) {
876 /* Caller MUST acquire the relayd control socket lock */
877 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
882 /* Metadata are always sent on the control socket. */
883 outfd
= relayd
->control_sock
.sock
.fd
;
885 /* Set header with stream information */
886 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
887 data_hdr
.data_size
= htobe32(data_size
);
888 data_hdr
.padding_size
= htobe32(padding
);
890 * Note that net_seq_num below is assigned with the *current* value of
891 * next_net_seq_num and only after that the next_net_seq_num will be
892 * increment. This is why when issuing a command on the relayd using
893 * this next value, 1 should always be substracted in order to compare
894 * the last seen sequence number on the relayd side to the last sent.
896 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
897 /* Other fields are zeroed previously */
899 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
905 ++stream
->next_net_seq_num
;
907 /* Set to go on data socket */
908 outfd
= relayd
->data_sock
.sock
.fd
;
916 * Allocate and return a new lttng_consumer_channel object using the given key
917 * to initialize the hash table node.
919 * On error, return NULL.
921 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
923 const char *pathname
,
928 enum lttng_event_output output
,
929 uint64_t tracefile_size
,
930 uint64_t tracefile_count
,
931 uint64_t session_id_per_pid
,
932 unsigned int monitor
,
933 unsigned int live_timer_interval
,
934 bool is_in_live_session
,
935 const char *root_shm_path
,
936 const char *shm_path
)
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 channel
->is_live
= is_in_live_session
;
958 pthread_mutex_init(&channel
->lock
, NULL
);
959 pthread_mutex_init(&channel
->timer_lock
, NULL
);
962 case LTTNG_EVENT_SPLICE
:
963 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
965 case LTTNG_EVENT_MMAP
:
966 channel
->output
= CONSUMER_CHANNEL_MMAP
;
976 * In monitor mode, the streams associated with the channel will be put in
977 * a special list ONLY owned by this channel. So, the refcount is set to 1
978 * here meaning that the channel itself has streams that are referenced.
980 * On a channel deletion, once the channel is no longer visible, the
981 * refcount is decremented and checked for a zero value to delete it. With
982 * streams in no monitor mode, it will now be safe to destroy the channel.
984 if (!channel
->monitor
) {
985 channel
->refcount
= 1;
988 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
989 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
991 strncpy(channel
->name
, name
, sizeof(channel
->name
));
992 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
995 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
996 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
999 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1000 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1003 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1005 channel
->wait_fd
= -1;
1007 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1009 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1016 * Add a channel to the global list protected by a mutex.
1018 * Always return 0 indicating success.
1020 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1021 struct lttng_consumer_local_data
*ctx
)
1023 pthread_mutex_lock(&consumer_data
.lock
);
1024 pthread_mutex_lock(&channel
->lock
);
1025 pthread_mutex_lock(&channel
->timer_lock
);
1028 * This gives us a guarantee that the channel we are about to add to the
1029 * channel hash table will be unique. See this function comment on the why
1030 * we need to steel the channel key at this stage.
1032 steal_channel_key(channel
->key
);
1035 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1038 pthread_mutex_unlock(&channel
->timer_lock
);
1039 pthread_mutex_unlock(&channel
->lock
);
1040 pthread_mutex_unlock(&consumer_data
.lock
);
1042 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1043 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1050 * Allocate the pollfd structure and the local view of the out fds to avoid
1051 * doing a lookup in the linked list and concurrency issues when writing is
1052 * needed. Called with consumer_data.lock held.
1054 * Returns the number of fds in the structures.
1056 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1057 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1058 struct lttng_ht
*ht
, int *nb_inactive_fd
)
1061 struct lttng_ht_iter iter
;
1062 struct lttng_consumer_stream
*stream
;
1067 assert(local_stream
);
1069 DBG("Updating poll fd array");
1070 *nb_inactive_fd
= 0;
1072 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1074 * Only active streams with an active end point can be added to the
1075 * poll set and local stream storage of the thread.
1077 * There is a potential race here for endpoint_status to be updated
1078 * just after the check. However, this is OK since the stream(s) will
1079 * be deleted once the thread is notified that the end point state has
1080 * changed where this function will be called back again.
1082 * We track the number of inactive FDs because they still need to be
1083 * closed by the polling thread after a wakeup on the data_pipe or
1086 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
1087 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1088 (*nb_inactive_fd
)++;
1092 * This clobbers way too much the debug output. Uncomment that if you
1093 * need it for debugging purposes.
1095 * DBG("Active FD %d", stream->wait_fd);
1097 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1098 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1099 local_stream
[i
] = stream
;
1105 * Insert the consumer_data_pipe at the end of the array and don't
1106 * increment i so nb_fd is the number of real FD.
1108 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1109 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1111 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1112 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1117 * Poll on the should_quit pipe and the command socket return -1 on
1118 * error, 1 if should exit, 0 if data is available on the command socket
1120 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1125 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1126 if (num_rdy
== -1) {
1128 * Restart interrupted system call.
1130 if (errno
== EINTR
) {
1133 PERROR("Poll error");
1136 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1137 DBG("consumer_should_quit wake up");
1144 * Set the error socket.
1146 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1149 ctx
->consumer_error_socket
= sock
;
1153 * Set the command socket path.
1155 void lttng_consumer_set_command_sock_path(
1156 struct lttng_consumer_local_data
*ctx
, char *sock
)
1158 ctx
->consumer_command_sock_path
= sock
;
1162 * Send return code to the session daemon.
1163 * If the socket is not defined, we return 0, it is not a fatal error
1165 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1167 if (ctx
->consumer_error_socket
> 0) {
1168 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1169 sizeof(enum lttcomm_sessiond_command
));
1176 * Close all the tracefiles and stream fds and MUST be called when all
1177 * instances are destroyed i.e. when all threads were joined and are ended.
1179 void lttng_consumer_cleanup(void)
1181 struct lttng_ht_iter iter
;
1182 struct lttng_consumer_channel
*channel
;
1186 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1188 consumer_del_channel(channel
);
1193 lttng_ht_destroy(consumer_data
.channel_ht
);
1195 cleanup_relayd_ht();
1197 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1200 * This HT contains streams that are freed by either the metadata thread or
1201 * the data thread so we do *nothing* on the hash table and simply destroy
1204 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1208 * Called from signal handler.
1210 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1215 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1217 PERROR("write consumer quit");
1220 DBG("Consumer flag that it should quit");
1225 * Flush pending writes to trace output disk file.
1228 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1232 int outfd
= stream
->out_fd
;
1235 * This does a blocking write-and-wait on any page that belongs to the
1236 * subbuffer prior to the one we just wrote.
1237 * Don't care about error values, as these are just hints and ways to
1238 * limit the amount of page cache used.
1240 if (orig_offset
< stream
->max_sb_size
) {
1243 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1244 stream
->max_sb_size
,
1245 SYNC_FILE_RANGE_WAIT_BEFORE
1246 | SYNC_FILE_RANGE_WRITE
1247 | SYNC_FILE_RANGE_WAIT_AFTER
);
1249 * Give hints to the kernel about how we access the file:
1250 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1253 * We need to call fadvise again after the file grows because the
1254 * kernel does not seem to apply fadvise to non-existing parts of the
1257 * Call fadvise _after_ having waited for the page writeback to
1258 * complete because the dirty page writeback semantic is not well
1259 * defined. So it can be expected to lead to lower throughput in
1262 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1263 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1264 if (ret
&& ret
!= -ENOSYS
) {
1266 PERROR("posix_fadvise on fd %i", outfd
);
1271 * Initialise the necessary environnement :
1272 * - create a new context
1273 * - create the poll_pipe
1274 * - create the should_quit pipe (for signal handler)
1275 * - create the thread pipe (for splice)
1277 * Takes a function pointer as argument, this function is called when data is
1278 * available on a buffer. This function is responsible to do the
1279 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1280 * buffer configuration and then kernctl_put_next_subbuf at the end.
1282 * Returns a pointer to the new context or NULL on error.
1284 struct lttng_consumer_local_data
*lttng_consumer_create(
1285 enum lttng_consumer_type type
,
1286 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1287 struct lttng_consumer_local_data
*ctx
, bool locked_by_caller
),
1288 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1289 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1290 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1293 struct lttng_consumer_local_data
*ctx
;
1295 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1296 consumer_data
.type
== type
);
1297 consumer_data
.type
= type
;
1299 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1301 PERROR("allocating context");
1305 ctx
->consumer_error_socket
= -1;
1306 ctx
->consumer_metadata_socket
= -1;
1307 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1308 /* assign the callbacks */
1309 ctx
->on_buffer_ready
= buffer_ready
;
1310 ctx
->on_recv_channel
= recv_channel
;
1311 ctx
->on_recv_stream
= recv_stream
;
1312 ctx
->on_update_stream
= update_stream
;
1314 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1315 if (!ctx
->consumer_data_pipe
) {
1316 goto error_poll_pipe
;
1319 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1320 if (!ctx
->consumer_wakeup_pipe
) {
1321 goto error_wakeup_pipe
;
1324 ret
= pipe(ctx
->consumer_should_quit
);
1326 PERROR("Error creating recv pipe");
1327 goto error_quit_pipe
;
1330 ret
= pipe(ctx
->consumer_channel_pipe
);
1332 PERROR("Error creating channel pipe");
1333 goto error_channel_pipe
;
1336 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1337 if (!ctx
->consumer_metadata_pipe
) {
1338 goto error_metadata_pipe
;
1343 error_metadata_pipe
:
1344 utils_close_pipe(ctx
->consumer_channel_pipe
);
1346 utils_close_pipe(ctx
->consumer_should_quit
);
1348 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1350 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1358 * Iterate over all streams of the hashtable and free them properly.
1360 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1362 struct lttng_ht_iter iter
;
1363 struct lttng_consumer_stream
*stream
;
1370 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1372 * Ignore return value since we are currently cleaning up so any error
1375 (void) consumer_del_stream(stream
, ht
);
1379 lttng_ht_destroy(ht
);
1383 * Iterate over all streams of the metadata hashtable and free them
1386 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1388 struct lttng_ht_iter iter
;
1389 struct lttng_consumer_stream
*stream
;
1396 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1398 * Ignore return value since we are currently cleaning up so any error
1401 (void) consumer_del_metadata_stream(stream
, ht
);
1405 lttng_ht_destroy(ht
);
1409 * Close all fds associated with the instance and free the context.
1411 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1415 DBG("Consumer destroying it. Closing everything.");
1421 destroy_data_stream_ht(data_ht
);
1422 destroy_metadata_stream_ht(metadata_ht
);
1424 ret
= close(ctx
->consumer_error_socket
);
1428 ret
= close(ctx
->consumer_metadata_socket
);
1432 utils_close_pipe(ctx
->consumer_channel_pipe
);
1433 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1434 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1435 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1436 utils_close_pipe(ctx
->consumer_should_quit
);
1438 unlink(ctx
->consumer_command_sock_path
);
1443 * Write the metadata stream id on the specified file descriptor.
1445 static int write_relayd_metadata_id(int fd
,
1446 struct lttng_consumer_stream
*stream
,
1447 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1450 struct lttcomm_relayd_metadata_payload hdr
;
1452 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1453 hdr
.padding_size
= htobe32(padding
);
1454 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1455 if (ret
< sizeof(hdr
)) {
1457 * This error means that the fd's end is closed so ignore the PERROR
1458 * not to clubber the error output since this can happen in a normal
1461 if (errno
!= EPIPE
) {
1462 PERROR("write metadata stream id");
1464 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1466 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1467 * handle writting the missing part so report that as an error and
1468 * don't lie to the caller.
1473 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1474 stream
->relayd_stream_id
, padding
);
1481 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1482 * core function for writing trace buffers to either the local filesystem or
1485 * It must be called with the stream lock held.
1487 * Careful review MUST be put if any changes occur!
1489 * Returns the number of bytes written
1491 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1492 struct lttng_consumer_stream
*stream
,
1493 const struct lttng_buffer_view
*buffer
,
1494 unsigned long padding
)
1497 off_t orig_offset
= stream
->out_fd_offset
;
1498 /* Default is on the disk */
1499 int outfd
= stream
->out_fd
;
1500 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1501 unsigned int relayd_hang_up
= 0;
1502 const size_t subbuf_content_size
= buffer
->size
- padding
;
1505 /* RCU lock for the relayd pointer */
1508 /* Flag that the current stream if set for network streaming. */
1509 if (stream
->relayd_id
!= (uint64_t) -1ULL) {
1510 relayd
= consumer_find_relayd(stream
->relayd_id
);
1511 if (relayd
== NULL
) {
1517 /* Handle stream on the relayd if the output is on the network */
1519 unsigned long netlen
= subbuf_content_size
;
1522 * Lock the control socket for the complete duration of the function
1523 * since from this point on we will use the socket.
1525 if (stream
->metadata_flag
) {
1526 /* Metadata requires the control socket. */
1527 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1528 if (stream
->reset_metadata_flag
) {
1529 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1530 stream
->relayd_stream_id
,
1531 stream
->metadata_version
);
1536 stream
->reset_metadata_flag
= 0;
1538 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1541 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1546 /* Use the returned socket. */
1549 /* Write metadata stream id before payload */
1550 if (stream
->metadata_flag
) {
1551 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1558 write_len
= subbuf_content_size
;
1560 /* No streaming; we have to write the full padding. */
1561 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1562 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1564 ERR("Reset metadata file");
1567 stream
->reset_metadata_flag
= 0;
1571 * Check if we need to change the tracefile before writing the packet.
1573 if (stream
->chan
->tracefile_size
> 0 &&
1574 (stream
->tracefile_size_current
+ buffer
->size
) >
1575 stream
->chan
->tracefile_size
) {
1576 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1577 stream
->name
, stream
->chan
->tracefile_size
,
1578 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1579 stream
->out_fd
, &(stream
->tracefile_count_current
),
1582 ERR("Rotating output file");
1585 outfd
= stream
->out_fd
;
1587 if (stream
->index_file
) {
1588 lttng_index_file_put(stream
->index_file
);
1589 stream
->index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1590 stream
->name
, stream
->uid
, stream
->gid
,
1591 stream
->chan
->tracefile_size
,
1592 stream
->tracefile_count_current
,
1593 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1594 if (!stream
->index_file
) {
1599 /* Reset current size because we just perform a rotation. */
1600 stream
->tracefile_size_current
= 0;
1601 stream
->out_fd_offset
= 0;
1604 stream
->tracefile_size_current
+= buffer
->size
;
1605 write_len
= buffer
->size
;
1609 * This call guarantee that len or less is returned. It's impossible to
1610 * receive a ret value that is bigger than len.
1612 ret
= lttng_write(outfd
, buffer
->data
, write_len
);
1613 DBG("Consumer mmap write() ret %zd (len %zu)", ret
, write_len
);
1614 if (ret
< 0 || ((size_t) ret
!= write_len
)) {
1616 * Report error to caller if nothing was written else at least send the
1624 /* Socket operation failed. We consider the relayd dead */
1625 if (errno
== EPIPE
|| errno
== EINVAL
|| errno
== EBADF
) {
1627 * This is possible if the fd is closed on the other side
1628 * (outfd) or any write problem. It can be verbose a bit for a
1629 * normal execution if for instance the relayd is stopped
1630 * abruptly. This can happen so set this to a DBG statement.
1632 DBG("Consumer mmap write detected relayd hang up");
1634 /* Unhandled error, print it and stop function right now. */
1635 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret
,
1640 stream
->output_written
+= ret
;
1642 /* This call is useless on a socket so better save a syscall. */
1644 /* This won't block, but will start writeout asynchronously */
1645 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, write_len
,
1646 SYNC_FILE_RANGE_WRITE
);
1647 stream
->out_fd_offset
+= write_len
;
1648 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1653 * This is a special case that the relayd has closed its socket. Let's
1654 * cleanup the relayd object and all associated streams.
1656 if (relayd
&& relayd_hang_up
) {
1657 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->id
);
1658 lttng_consumer_cleanup_relayd(relayd
);
1662 /* Unlock only if ctrl socket used */
1663 if (relayd
&& stream
->metadata_flag
) {
1664 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1672 * Splice the data from the ring buffer to the tracefile.
1674 * It must be called with the stream lock held.
1676 * Returns the number of bytes spliced.
1678 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1679 struct lttng_consumer_local_data
*ctx
,
1680 struct lttng_consumer_stream
*stream
, unsigned long len
,
1681 unsigned long padding
)
1683 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1685 off_t orig_offset
= stream
->out_fd_offset
;
1686 int fd
= stream
->wait_fd
;
1687 /* Default is on the disk */
1688 int outfd
= stream
->out_fd
;
1689 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1691 unsigned int relayd_hang_up
= 0;
1693 switch (consumer_data
.type
) {
1694 case LTTNG_CONSUMER_KERNEL
:
1696 case LTTNG_CONSUMER32_UST
:
1697 case LTTNG_CONSUMER64_UST
:
1698 /* Not supported for user space tracing */
1701 ERR("Unknown consumer_data type");
1705 /* RCU lock for the relayd pointer */
1708 /* Flag that the current stream if set for network streaming. */
1709 if (stream
->relayd_id
!= (uint64_t) -1ULL) {
1710 relayd
= consumer_find_relayd(stream
->relayd_id
);
1711 if (relayd
== NULL
) {
1716 splice_pipe
= stream
->splice_pipe
;
1718 /* Write metadata stream id before payload */
1720 unsigned long total_len
= len
;
1722 if (stream
->metadata_flag
) {
1724 * Lock the control socket for the complete duration of the function
1725 * since from this point on we will use the socket.
1727 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1729 if (stream
->reset_metadata_flag
) {
1730 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1731 stream
->relayd_stream_id
,
1732 stream
->metadata_version
);
1737 stream
->reset_metadata_flag
= 0;
1739 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1747 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1750 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1756 /* Use the returned socket. */
1759 /* No streaming, we have to set the len with the full padding */
1762 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1763 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1765 ERR("Reset metadata file");
1768 stream
->reset_metadata_flag
= 0;
1771 * Check if we need to change the tracefile before writing the packet.
1773 if (stream
->chan
->tracefile_size
> 0 &&
1774 (stream
->tracefile_size_current
+ len
) >
1775 stream
->chan
->tracefile_size
) {
1776 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1777 stream
->name
, stream
->chan
->tracefile_size
,
1778 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1779 stream
->out_fd
, &(stream
->tracefile_count_current
),
1783 ERR("Rotating output file");
1786 outfd
= stream
->out_fd
;
1788 if (stream
->index_file
) {
1789 lttng_index_file_put(stream
->index_file
);
1790 stream
->index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1791 stream
->name
, stream
->uid
, stream
->gid
,
1792 stream
->chan
->tracefile_size
,
1793 stream
->tracefile_count_current
,
1794 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1795 if (!stream
->index_file
) {
1800 /* Reset current size because we just perform a rotation. */
1801 stream
->tracefile_size_current
= 0;
1802 stream
->out_fd_offset
= 0;
1805 stream
->tracefile_size_current
+= len
;
1809 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1810 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1811 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1812 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1813 DBG("splice chan to pipe, ret %zd", ret_splice
);
1814 if (ret_splice
< 0) {
1817 PERROR("Error in relay splice");
1821 /* Handle stream on the relayd if the output is on the network */
1822 if (relayd
&& stream
->metadata_flag
) {
1823 size_t metadata_payload_size
=
1824 sizeof(struct lttcomm_relayd_metadata_payload
);
1826 /* Update counter to fit the spliced data */
1827 ret_splice
+= metadata_payload_size
;
1828 len
+= metadata_payload_size
;
1830 * We do this so the return value can match the len passed as
1831 * argument to this function.
1833 written
-= metadata_payload_size
;
1836 /* Splice data out */
1837 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1838 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1839 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1841 if (ret_splice
< 0) {
1846 } else if (ret_splice
> len
) {
1848 * We don't expect this code path to be executed but you never know
1849 * so this is an extra protection agains a buggy splice().
1852 written
+= ret_splice
;
1853 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1857 /* All good, update current len and continue. */
1861 /* This call is useless on a socket so better save a syscall. */
1863 /* This won't block, but will start writeout asynchronously */
1864 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1865 SYNC_FILE_RANGE_WRITE
);
1866 stream
->out_fd_offset
+= ret_splice
;
1868 stream
->output_written
+= ret_splice
;
1869 written
+= ret_splice
;
1872 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1878 * This is a special case that the relayd has closed its socket. Let's
1879 * cleanup the relayd object and all associated streams.
1881 if (relayd
&& relayd_hang_up
) {
1882 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->id
);
1883 lttng_consumer_cleanup_relayd(relayd
);
1884 /* Skip splice error so the consumer does not fail */
1889 /* send the appropriate error description to sessiond */
1892 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1895 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1898 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1903 if (relayd
&& stream
->metadata_flag
) {
1904 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1912 * Take a snapshot for a specific fd
1914 * Returns 0 on success, < 0 on error
1916 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1918 switch (consumer_data
.type
) {
1919 case LTTNG_CONSUMER_KERNEL
:
1920 return lttng_kconsumer_take_snapshot(stream
);
1921 case LTTNG_CONSUMER32_UST
:
1922 case LTTNG_CONSUMER64_UST
:
1923 return lttng_ustconsumer_take_snapshot(stream
);
1925 ERR("Unknown consumer_data type");
1932 * Get the produced position
1934 * Returns 0 on success, < 0 on error
1936 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1939 switch (consumer_data
.type
) {
1940 case LTTNG_CONSUMER_KERNEL
:
1941 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1942 case LTTNG_CONSUMER32_UST
:
1943 case LTTNG_CONSUMER64_UST
:
1944 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1946 ERR("Unknown consumer_data type");
1952 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1953 int sock
, struct pollfd
*consumer_sockpoll
)
1955 switch (consumer_data
.type
) {
1956 case LTTNG_CONSUMER_KERNEL
:
1957 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1958 case LTTNG_CONSUMER32_UST
:
1959 case LTTNG_CONSUMER64_UST
:
1960 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1962 ERR("Unknown consumer_data type");
1968 void lttng_consumer_close_all_metadata(void)
1970 switch (consumer_data
.type
) {
1971 case LTTNG_CONSUMER_KERNEL
:
1973 * The Kernel consumer has a different metadata scheme so we don't
1974 * close anything because the stream will be closed by the session
1978 case LTTNG_CONSUMER32_UST
:
1979 case LTTNG_CONSUMER64_UST
:
1981 * Close all metadata streams. The metadata hash table is passed and
1982 * this call iterates over it by closing all wakeup fd. This is safe
1983 * because at this point we are sure that the metadata producer is
1984 * either dead or blocked.
1986 lttng_ustconsumer_close_all_metadata(metadata_ht
);
1989 ERR("Unknown consumer_data type");
1995 * Clean up a metadata stream and free its memory.
1997 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1998 struct lttng_ht
*ht
)
2000 struct lttng_consumer_channel
*free_chan
= NULL
;
2004 * This call should NEVER receive regular stream. It must always be
2005 * metadata stream and this is crucial for data structure synchronization.
2007 assert(stream
->metadata_flag
);
2009 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2011 pthread_mutex_lock(&consumer_data
.lock
);
2012 pthread_mutex_lock(&stream
->chan
->lock
);
2013 pthread_mutex_lock(&stream
->lock
);
2014 if (stream
->chan
->metadata_cache
) {
2015 /* Only applicable to userspace consumers. */
2016 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2019 /* Remove any reference to that stream. */
2020 consumer_stream_delete(stream
, ht
);
2022 /* Close down everything including the relayd if one. */
2023 consumer_stream_close(stream
);
2024 /* Destroy tracer buffers of the stream. */
2025 consumer_stream_destroy_buffers(stream
);
2027 /* Atomically decrement channel refcount since other threads can use it. */
2028 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
2029 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
2030 /* Go for channel deletion! */
2031 free_chan
= stream
->chan
;
2035 * Nullify the stream reference so it is not used after deletion. The
2036 * channel lock MUST be acquired before being able to check for a NULL
2039 stream
->chan
->metadata_stream
= NULL
;
2041 if (stream
->chan
->metadata_cache
) {
2042 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2044 pthread_mutex_unlock(&stream
->lock
);
2045 pthread_mutex_unlock(&stream
->chan
->lock
);
2046 pthread_mutex_unlock(&consumer_data
.lock
);
2049 consumer_del_channel(free_chan
);
2052 consumer_stream_free(stream
);
2056 * Action done with the metadata stream when adding it to the consumer internal
2057 * data structures to handle it.
2059 int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2061 struct lttng_ht
*ht
= metadata_ht
;
2063 struct lttng_ht_iter iter
;
2064 struct lttng_ht_node_u64
*node
;
2069 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2071 pthread_mutex_lock(&consumer_data
.lock
);
2072 pthread_mutex_lock(&stream
->chan
->lock
);
2073 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2074 pthread_mutex_lock(&stream
->lock
);
2077 * From here, refcounts are updated so be _careful_ when returning an error
2084 * Lookup the stream just to make sure it does not exist in our internal
2085 * state. This should NEVER happen.
2087 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2088 node
= lttng_ht_iter_get_node_u64(&iter
);
2092 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2093 * in terms of destroying the associated channel, because the action that
2094 * causes the count to become 0 also causes a stream to be added. The
2095 * channel deletion will thus be triggered by the following removal of this
2098 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2099 /* Increment refcount before decrementing nb_init_stream_left */
2101 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2104 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2106 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
2107 &stream
->node_channel_id
);
2110 * Add stream to the stream_list_ht of the consumer data. No need to steal
2111 * the key since the HT does not use it and we allow to add redundant keys
2114 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2118 pthread_mutex_unlock(&stream
->lock
);
2119 pthread_mutex_unlock(&stream
->chan
->lock
);
2120 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2121 pthread_mutex_unlock(&consumer_data
.lock
);
2126 * Delete data stream that are flagged for deletion (endpoint_status).
2128 static void validate_endpoint_status_data_stream(void)
2130 struct lttng_ht_iter iter
;
2131 struct lttng_consumer_stream
*stream
;
2133 DBG("Consumer delete flagged data stream");
2136 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2137 /* Validate delete flag of the stream */
2138 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2141 /* Delete it right now */
2142 consumer_del_stream(stream
, data_ht
);
2148 * Delete metadata stream that are flagged for deletion (endpoint_status).
2150 static void validate_endpoint_status_metadata_stream(
2151 struct lttng_poll_event
*pollset
)
2153 struct lttng_ht_iter iter
;
2154 struct lttng_consumer_stream
*stream
;
2156 DBG("Consumer delete flagged metadata stream");
2161 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2162 /* Validate delete flag of the stream */
2163 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2167 * Remove from pollset so the metadata thread can continue without
2168 * blocking on a deleted stream.
2170 lttng_poll_del(pollset
, stream
->wait_fd
);
2172 /* Delete it right now */
2173 consumer_del_metadata_stream(stream
, metadata_ht
);
2179 * Thread polls on metadata file descriptor and write them on disk or on the
2182 void *consumer_thread_metadata_poll(void *data
)
2184 int ret
, i
, pollfd
, err
= -1;
2185 uint32_t revents
, nb_fd
;
2186 struct lttng_consumer_stream
*stream
= NULL
;
2187 struct lttng_ht_iter iter
;
2188 struct lttng_ht_node_u64
*node
;
2189 struct lttng_poll_event events
;
2190 struct lttng_consumer_local_data
*ctx
= data
;
2193 rcu_register_thread();
2195 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2197 if (testpoint(consumerd_thread_metadata
)) {
2198 goto error_testpoint
;
2201 health_code_update();
2203 DBG("Thread metadata poll started");
2205 /* Size is set to 1 for the consumer_metadata pipe */
2206 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2208 ERR("Poll set creation failed");
2212 ret
= lttng_poll_add(&events
,
2213 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2219 DBG("Metadata main loop started");
2223 health_code_update();
2224 health_poll_entry();
2225 DBG("Metadata poll wait");
2226 ret
= lttng_poll_wait(&events
, -1);
2227 DBG("Metadata poll return from wait with %d fd(s)",
2228 LTTNG_POLL_GETNB(&events
));
2230 DBG("Metadata event caught in thread");
2232 if (errno
== EINTR
) {
2233 ERR("Poll EINTR caught");
2236 if (LTTNG_POLL_GETNB(&events
) == 0) {
2237 err
= 0; /* All is OK */
2244 /* From here, the event is a metadata wait fd */
2245 for (i
= 0; i
< nb_fd
; i
++) {
2246 health_code_update();
2248 revents
= LTTNG_POLL_GETEV(&events
, i
);
2249 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2252 /* No activity for this FD (poll implementation). */
2256 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2257 if (revents
& LPOLLIN
) {
2260 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2261 &stream
, sizeof(stream
));
2262 if (pipe_len
< sizeof(stream
)) {
2264 PERROR("read metadata stream");
2267 * Remove the pipe from the poll set and continue the loop
2268 * since their might be data to consume.
2270 lttng_poll_del(&events
,
2271 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2272 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2276 /* A NULL stream means that the state has changed. */
2277 if (stream
== NULL
) {
2278 /* Check for deleted streams. */
2279 validate_endpoint_status_metadata_stream(&events
);
2283 DBG("Adding metadata stream %d to poll set",
2286 /* Add metadata stream to the global poll events list */
2287 lttng_poll_add(&events
, stream
->wait_fd
,
2288 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2289 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2290 DBG("Metadata thread pipe hung up");
2292 * Remove the pipe from the poll set and continue the loop
2293 * since their might be data to consume.
2295 lttng_poll_del(&events
,
2296 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2297 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2300 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2304 /* Handle other stream */
2310 uint64_t tmp_id
= (uint64_t) pollfd
;
2312 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2314 node
= lttng_ht_iter_get_node_u64(&iter
);
2317 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2320 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2321 /* Get the data out of the metadata file descriptor */
2322 DBG("Metadata available on fd %d", pollfd
);
2323 assert(stream
->wait_fd
== pollfd
);
2326 health_code_update();
2328 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2330 * We don't check the return value here since if we get
2331 * a negative len, it means an error occurred thus we
2332 * simply remove it from the poll set and free the
2337 /* It's ok to have an unavailable sub-buffer */
2338 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2339 /* Clean up stream from consumer and free it. */
2340 lttng_poll_del(&events
, stream
->wait_fd
);
2341 consumer_del_metadata_stream(stream
, metadata_ht
);
2343 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2344 DBG("Metadata fd %d is hup|err.", pollfd
);
2345 if (!stream
->hangup_flush_done
2346 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2347 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2348 DBG("Attempting to flush and consume the UST buffers");
2349 lttng_ustconsumer_on_stream_hangup(stream
);
2351 /* We just flushed the stream now read it. */
2353 health_code_update();
2355 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2357 * We don't check the return value here since if we get
2358 * a negative len, it means an error occurred thus we
2359 * simply remove it from the poll set and free the
2365 lttng_poll_del(&events
, stream
->wait_fd
);
2367 * This call update the channel states, closes file descriptors
2368 * and securely free the stream.
2370 consumer_del_metadata_stream(stream
, metadata_ht
);
2372 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2376 /* Release RCU lock for the stream looked up */
2384 DBG("Metadata poll thread exiting");
2386 lttng_poll_clean(&events
);
2391 ERR("Health error occurred in %s", __func__
);
2393 health_unregister(health_consumerd
);
2394 rcu_unregister_thread();
2399 * This thread polls the fds in the set to consume the data and write
2400 * it to tracefile if necessary.
2402 void *consumer_thread_data_poll(void *data
)
2404 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2405 struct pollfd
*pollfd
= NULL
;
2406 /* local view of the streams */
2407 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2408 /* local view of consumer_data.fds_count */
2410 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2411 int nb_inactive_fd
= 0;
2412 struct lttng_consumer_local_data
*ctx
= data
;
2415 rcu_register_thread();
2417 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2419 if (testpoint(consumerd_thread_data
)) {
2420 goto error_testpoint
;
2423 health_code_update();
2425 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2426 if (local_stream
== NULL
) {
2427 PERROR("local_stream malloc");
2432 health_code_update();
2438 * the fds set has been updated, we need to update our
2439 * local array as well
2441 pthread_mutex_lock(&consumer_data
.lock
);
2442 if (consumer_data
.need_update
) {
2447 local_stream
= NULL
;
2450 * Allocate for all fds +1 for the consumer_data_pipe and +1 for
2453 pollfd
= zmalloc((consumer_data
.stream_count
+ 2) * sizeof(struct pollfd
));
2454 if (pollfd
== NULL
) {
2455 PERROR("pollfd malloc");
2456 pthread_mutex_unlock(&consumer_data
.lock
);
2460 local_stream
= zmalloc((consumer_data
.stream_count
+ 2) *
2461 sizeof(struct lttng_consumer_stream
*));
2462 if (local_stream
== NULL
) {
2463 PERROR("local_stream malloc");
2464 pthread_mutex_unlock(&consumer_data
.lock
);
2467 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2468 data_ht
, &nb_inactive_fd
);
2470 ERR("Error in allocating pollfd or local_outfds");
2471 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2472 pthread_mutex_unlock(&consumer_data
.lock
);
2476 consumer_data
.need_update
= 0;
2478 pthread_mutex_unlock(&consumer_data
.lock
);
2480 /* No FDs and consumer_quit, consumer_cleanup the thread */
2481 if (nb_fd
== 0 && consumer_quit
== 1 && nb_inactive_fd
== 0) {
2482 err
= 0; /* All is OK */
2485 /* poll on the array of fds */
2487 DBG("polling on %d fd", nb_fd
+ 2);
2488 health_poll_entry();
2489 num_rdy
= poll(pollfd
, nb_fd
+ 2, -1);
2491 DBG("poll num_rdy : %d", num_rdy
);
2492 if (num_rdy
== -1) {
2494 * Restart interrupted system call.
2496 if (errno
== EINTR
) {
2499 PERROR("Poll error");
2500 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2502 } else if (num_rdy
== 0) {
2503 DBG("Polling thread timed out");
2508 * If the consumer_data_pipe triggered poll go directly to the
2509 * beginning of the loop to update the array. We want to prioritize
2510 * array update over low-priority reads.
2512 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2513 ssize_t pipe_readlen
;
2515 DBG("consumer_data_pipe wake up");
2516 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2517 &new_stream
, sizeof(new_stream
));
2518 if (pipe_readlen
< sizeof(new_stream
)) {
2519 PERROR("Consumer data pipe");
2520 /* Continue so we can at least handle the current stream(s). */
2525 * If the stream is NULL, just ignore it. It's also possible that
2526 * the sessiond poll thread changed the consumer_quit state and is
2527 * waking us up to test it.
2529 if (new_stream
== NULL
) {
2530 validate_endpoint_status_data_stream();
2534 /* Continue to update the local streams and handle prio ones */
2538 /* Handle wakeup pipe. */
2539 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2541 ssize_t pipe_readlen
;
2543 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2545 if (pipe_readlen
< 0) {
2546 PERROR("Consumer data wakeup pipe");
2548 /* We've been awakened to handle stream(s). */
2549 ctx
->has_wakeup
= 0;
2552 /* Take care of high priority channels first. */
2553 for (i
= 0; i
< nb_fd
; i
++) {
2554 health_code_update();
2556 if (local_stream
[i
] == NULL
) {
2559 if (pollfd
[i
].revents
& POLLPRI
) {
2560 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2562 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2563 /* it's ok to have an unavailable sub-buffer */
2564 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2565 /* Clean the stream and free it. */
2566 consumer_del_stream(local_stream
[i
], data_ht
);
2567 local_stream
[i
] = NULL
;
2568 } else if (len
> 0) {
2569 local_stream
[i
]->data_read
= 1;
2575 * If we read high prio channel in this loop, try again
2576 * for more high prio data.
2582 /* Take care of low priority channels. */
2583 for (i
= 0; i
< nb_fd
; i
++) {
2584 health_code_update();
2586 if (local_stream
[i
] == NULL
) {
2589 if ((pollfd
[i
].revents
& POLLIN
) ||
2590 local_stream
[i
]->hangup_flush_done
||
2591 local_stream
[i
]->has_data
) {
2592 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2593 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2594 /* it's ok to have an unavailable sub-buffer */
2595 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2596 /* Clean the stream and free it. */
2597 consumer_del_stream(local_stream
[i
], data_ht
);
2598 local_stream
[i
] = NULL
;
2599 } else if (len
> 0) {
2600 local_stream
[i
]->data_read
= 1;
2605 /* Handle hangup and errors */
2606 for (i
= 0; i
< nb_fd
; i
++) {
2607 health_code_update();
2609 if (local_stream
[i
] == NULL
) {
2612 if (!local_stream
[i
]->hangup_flush_done
2613 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2614 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2615 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2616 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2618 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2619 /* Attempt read again, for the data we just flushed. */
2620 local_stream
[i
]->data_read
= 1;
2623 * If the poll flag is HUP/ERR/NVAL and we have
2624 * read no data in this pass, we can remove the
2625 * stream from its hash table.
2627 if ((pollfd
[i
].revents
& POLLHUP
)) {
2628 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2629 if (!local_stream
[i
]->data_read
) {
2630 consumer_del_stream(local_stream
[i
], data_ht
);
2631 local_stream
[i
] = NULL
;
2634 } else if (pollfd
[i
].revents
& POLLERR
) {
2635 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2636 if (!local_stream
[i
]->data_read
) {
2637 consumer_del_stream(local_stream
[i
], data_ht
);
2638 local_stream
[i
] = NULL
;
2641 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2642 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2643 if (!local_stream
[i
]->data_read
) {
2644 consumer_del_stream(local_stream
[i
], data_ht
);
2645 local_stream
[i
] = NULL
;
2649 if (local_stream
[i
] != NULL
) {
2650 local_stream
[i
]->data_read
= 0;
2657 DBG("polling thread exiting");
2662 * Close the write side of the pipe so epoll_wait() in
2663 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2664 * read side of the pipe. If we close them both, epoll_wait strangely does
2665 * not return and could create a endless wait period if the pipe is the
2666 * only tracked fd in the poll set. The thread will take care of closing
2669 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2674 ERR("Health error occurred in %s", __func__
);
2676 health_unregister(health_consumerd
);
2678 rcu_unregister_thread();
2683 * Close wake-up end of each stream belonging to the channel. This will
2684 * allow the poll() on the stream read-side to detect when the
2685 * write-side (application) finally closes them.
2688 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2690 struct lttng_ht
*ht
;
2691 struct lttng_consumer_stream
*stream
;
2692 struct lttng_ht_iter iter
;
2694 ht
= consumer_data
.stream_per_chan_id_ht
;
2697 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2698 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2699 ht
->match_fct
, &channel
->key
,
2700 &iter
.iter
, stream
, node_channel_id
.node
) {
2702 * Protect against teardown with mutex.
2704 pthread_mutex_lock(&stream
->lock
);
2705 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2708 switch (consumer_data
.type
) {
2709 case LTTNG_CONSUMER_KERNEL
:
2711 case LTTNG_CONSUMER32_UST
:
2712 case LTTNG_CONSUMER64_UST
:
2713 if (stream
->metadata_flag
) {
2714 /* Safe and protected by the stream lock. */
2715 lttng_ustconsumer_close_metadata(stream
->chan
);
2718 * Note: a mutex is taken internally within
2719 * liblttng-ust-ctl to protect timer wakeup_fd
2720 * use from concurrent close.
2722 lttng_ustconsumer_close_stream_wakeup(stream
);
2726 ERR("Unknown consumer_data type");
2730 pthread_mutex_unlock(&stream
->lock
);
2735 static void destroy_channel_ht(struct lttng_ht
*ht
)
2737 struct lttng_ht_iter iter
;
2738 struct lttng_consumer_channel
*channel
;
2746 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2747 ret
= lttng_ht_del(ht
, &iter
);
2752 lttng_ht_destroy(ht
);
2756 * This thread polls the channel fds to detect when they are being
2757 * closed. It closes all related streams if the channel is detected as
2758 * closed. It is currently only used as a shim layer for UST because the
2759 * consumerd needs to keep the per-stream wakeup end of pipes open for
2762 void *consumer_thread_channel_poll(void *data
)
2764 int ret
, i
, pollfd
, err
= -1;
2765 uint32_t revents
, nb_fd
;
2766 struct lttng_consumer_channel
*chan
= NULL
;
2767 struct lttng_ht_iter iter
;
2768 struct lttng_ht_node_u64
*node
;
2769 struct lttng_poll_event events
;
2770 struct lttng_consumer_local_data
*ctx
= data
;
2771 struct lttng_ht
*channel_ht
;
2773 rcu_register_thread();
2775 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2777 if (testpoint(consumerd_thread_channel
)) {
2778 goto error_testpoint
;
2781 health_code_update();
2783 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2785 /* ENOMEM at this point. Better to bail out. */
2789 DBG("Thread channel poll started");
2791 /* Size is set to 1 for the consumer_channel pipe */
2792 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2794 ERR("Poll set creation failed");
2798 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2804 DBG("Channel main loop started");
2808 health_code_update();
2809 DBG("Channel poll wait");
2810 health_poll_entry();
2811 ret
= lttng_poll_wait(&events
, -1);
2812 DBG("Channel poll return from wait with %d fd(s)",
2813 LTTNG_POLL_GETNB(&events
));
2815 DBG("Channel event caught in thread");
2817 if (errno
== EINTR
) {
2818 ERR("Poll EINTR caught");
2821 if (LTTNG_POLL_GETNB(&events
) == 0) {
2822 err
= 0; /* All is OK */
2829 /* From here, the event is a channel wait fd */
2830 for (i
= 0; i
< nb_fd
; i
++) {
2831 health_code_update();
2833 revents
= LTTNG_POLL_GETEV(&events
, i
);
2834 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2837 /* No activity for this FD (poll implementation). */
2841 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2842 if (revents
& LPOLLIN
) {
2843 enum consumer_channel_action action
;
2846 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2849 ERR("Error reading channel pipe");
2851 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2856 case CONSUMER_CHANNEL_ADD
:
2857 DBG("Adding channel %d to poll set",
2860 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2863 lttng_ht_add_unique_u64(channel_ht
,
2864 &chan
->wait_fd_node
);
2866 /* Add channel to the global poll events list */
2867 lttng_poll_add(&events
, chan
->wait_fd
,
2868 LPOLLERR
| LPOLLHUP
);
2870 case CONSUMER_CHANNEL_DEL
:
2873 * This command should never be called if the channel
2874 * has streams monitored by either the data or metadata
2875 * thread. The consumer only notify this thread with a
2876 * channel del. command if it receives a destroy
2877 * channel command from the session daemon that send it
2878 * if a command prior to the GET_CHANNEL failed.
2882 chan
= consumer_find_channel(key
);
2885 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2888 lttng_poll_del(&events
, chan
->wait_fd
);
2889 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2890 ret
= lttng_ht_del(channel_ht
, &iter
);
2893 switch (consumer_data
.type
) {
2894 case LTTNG_CONSUMER_KERNEL
:
2896 case LTTNG_CONSUMER32_UST
:
2897 case LTTNG_CONSUMER64_UST
:
2898 health_code_update();
2899 /* Destroy streams that might have been left in the stream list. */
2900 clean_channel_stream_list(chan
);
2903 ERR("Unknown consumer_data type");
2908 * Release our own refcount. Force channel deletion even if
2909 * streams were not initialized.
2911 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2912 consumer_del_channel(chan
);
2917 case CONSUMER_CHANNEL_QUIT
:
2919 * Remove the pipe from the poll set and continue the loop
2920 * since their might be data to consume.
2922 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2925 ERR("Unknown action");
2928 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2929 DBG("Channel thread pipe hung up");
2931 * Remove the pipe from the poll set and continue the loop
2932 * since their might be data to consume.
2934 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2937 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2941 /* Handle other stream */
2947 uint64_t tmp_id
= (uint64_t) pollfd
;
2949 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2951 node
= lttng_ht_iter_get_node_u64(&iter
);
2954 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2957 /* Check for error event */
2958 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2959 DBG("Channel fd %d is hup|err.", pollfd
);
2961 lttng_poll_del(&events
, chan
->wait_fd
);
2962 ret
= lttng_ht_del(channel_ht
, &iter
);
2966 * This will close the wait fd for each stream associated to
2967 * this channel AND monitored by the data/metadata thread thus
2968 * will be clean by the right thread.
2970 consumer_close_channel_streams(chan
);
2972 /* Release our own refcount */
2973 if (!uatomic_sub_return(&chan
->refcount
, 1)
2974 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2975 consumer_del_channel(chan
);
2978 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2983 /* Release RCU lock for the channel looked up */
2991 lttng_poll_clean(&events
);
2993 destroy_channel_ht(channel_ht
);
2996 DBG("Channel poll thread exiting");
2999 ERR("Health error occurred in %s", __func__
);
3001 health_unregister(health_consumerd
);
3002 rcu_unregister_thread();
3006 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3007 struct pollfd
*sockpoll
, int client_socket
)
3014 ret
= lttng_consumer_poll_socket(sockpoll
);
3018 DBG("Metadata connection on client_socket");
3020 /* Blocking call, waiting for transmission */
3021 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3022 if (ctx
->consumer_metadata_socket
< 0) {
3023 WARN("On accept metadata");
3034 * This thread listens on the consumerd socket and receives the file
3035 * descriptors from the session daemon.
3037 void *consumer_thread_sessiond_poll(void *data
)
3039 int sock
= -1, client_socket
, ret
, err
= -1;
3041 * structure to poll for incoming data on communication socket avoids
3042 * making blocking sockets.
3044 struct pollfd consumer_sockpoll
[2];
3045 struct lttng_consumer_local_data
*ctx
= data
;
3047 rcu_register_thread();
3049 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3051 if (testpoint(consumerd_thread_sessiond
)) {
3052 goto error_testpoint
;
3055 health_code_update();
3057 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3058 unlink(ctx
->consumer_command_sock_path
);
3059 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3060 if (client_socket
< 0) {
3061 ERR("Cannot create command socket");
3065 ret
= lttcomm_listen_unix_sock(client_socket
);
3070 DBG("Sending ready command to lttng-sessiond");
3071 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3072 /* return < 0 on error, but == 0 is not fatal */
3074 ERR("Error sending ready command to lttng-sessiond");
3078 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3079 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3080 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3081 consumer_sockpoll
[1].fd
= client_socket
;
3082 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3084 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3092 DBG("Connection on client_socket");
3094 /* Blocking call, waiting for transmission */
3095 sock
= lttcomm_accept_unix_sock(client_socket
);
3102 * Setup metadata socket which is the second socket connection on the
3103 * command unix socket.
3105 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3114 /* This socket is not useful anymore. */
3115 ret
= close(client_socket
);
3117 PERROR("close client_socket");
3121 /* update the polling structure to poll on the established socket */
3122 consumer_sockpoll
[1].fd
= sock
;
3123 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3126 health_code_update();
3128 health_poll_entry();
3129 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3138 DBG("Incoming command on sock");
3139 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3142 * This could simply be a session daemon quitting. Don't output
3145 DBG("Communication interrupted on command socket");
3149 if (consumer_quit
) {
3150 DBG("consumer_thread_receive_fds received quit from signal");
3151 err
= 0; /* All is OK */
3154 DBG("received command on sock");
3160 DBG("Consumer thread sessiond poll exiting");
3163 * Close metadata streams since the producer is the session daemon which
3166 * NOTE: for now, this only applies to the UST tracer.
3168 lttng_consumer_close_all_metadata();
3171 * when all fds have hung up, the polling thread
3177 * Notify the data poll thread to poll back again and test the
3178 * consumer_quit state that we just set so to quit gracefully.
3180 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3182 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3184 notify_health_quit_pipe(health_quit_pipe
);
3186 /* Cleaning up possibly open sockets. */
3190 PERROR("close sock sessiond poll");
3193 if (client_socket
>= 0) {
3194 ret
= close(client_socket
);
3196 PERROR("close client_socket sessiond poll");
3203 ERR("Health error occurred in %s", __func__
);
3205 health_unregister(health_consumerd
);
3207 rcu_unregister_thread();
3211 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3212 struct lttng_consumer_local_data
*ctx
,
3213 bool locked_by_caller
)
3215 ssize_t ret
, written_bytes
= 0;
3216 struct stream_subbuffer subbuffer
= {};
3218 if (!locked_by_caller
) {
3219 stream
->read_subbuffer_ops
.lock(stream
);
3222 if (stream
->read_subbuffer_ops
.on_wake_up
) {
3223 ret
= stream
->read_subbuffer_ops
.on_wake_up(stream
);
3229 ret
= stream
->read_subbuffer_ops
.get_next_subbuffer(stream
, &subbuffer
);
3231 if (ret
== -ENODATA
) {
3239 ret
= stream
->read_subbuffer_ops
.pre_consume_subbuffer(
3240 stream
, &subbuffer
);
3242 goto error_put_subbuf
;
3245 written_bytes
= stream
->read_subbuffer_ops
.consume_subbuffer(
3246 ctx
, stream
, &subbuffer
);
3248 * Should write subbuf_size amount of data when network streaming or
3249 * the full padded size when we are not streaming.
3251 if ((written_bytes
!= subbuffer
.info
.data
.subbuf_size
&&
3252 stream
->relayd_id
!= (uint64_t) -1ULL) ||
3253 (written_bytes
!= subbuffer
.info
.data
.padded_subbuf_size
&&
3254 stream
->relayd_id
==
3255 (uint64_t) -1ULL)) {
3257 * Display the error but continue processing to try to
3258 * release the subbuffer. This is a DBG statement
3259 * since this can happen without being a critical
3262 DBG("Failed to write to tracefile (written_bytes: %zd != padded subbuffer size: %lu, subbuffer size: %lu)",
3263 written_bytes
, subbuffer
.info
.data
.subbuf_size
,
3264 subbuffer
.info
.data
.padded_subbuf_size
);
3267 ret
= stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3272 if (stream
->read_subbuffer_ops
.post_consume
) {
3273 ret
= stream
->read_subbuffer_ops
.post_consume(stream
, &subbuffer
, ctx
);
3280 if (stream
->read_subbuffer_ops
.on_sleep
) {
3281 stream
->read_subbuffer_ops
.on_sleep(stream
, ctx
);
3284 ret
= written_bytes
;
3286 if (!locked_by_caller
) {
3287 stream
->read_subbuffer_ops
.unlock(stream
);
3292 (void) stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3296 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3298 switch (consumer_data
.type
) {
3299 case LTTNG_CONSUMER_KERNEL
:
3300 return lttng_kconsumer_on_recv_stream(stream
);
3301 case LTTNG_CONSUMER32_UST
:
3302 case LTTNG_CONSUMER64_UST
:
3303 return lttng_ustconsumer_on_recv_stream(stream
);
3305 ERR("Unknown consumer_data type");
3312 * Allocate and set consumer data hash tables.
3314 int lttng_consumer_init(void)
3316 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3317 if (!consumer_data
.channel_ht
) {
3321 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3322 if (!consumer_data
.relayd_ht
) {
3326 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3327 if (!consumer_data
.stream_list_ht
) {
3331 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3332 if (!consumer_data
.stream_per_chan_id_ht
) {
3336 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3341 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3353 * Process the ADD_RELAYD command receive by a consumer.
3355 * This will create a relayd socket pair and add it to the relayd hash table.
3356 * The caller MUST acquire a RCU read side lock before calling it.
3358 void consumer_add_relayd_socket(uint64_t relayd_id
, int sock_type
,
3359 struct lttng_consumer_local_data
*ctx
, int sock
,
3360 struct pollfd
*consumer_sockpoll
,
3361 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3362 uint64_t relayd_session_id
)
3364 int fd
= -1, ret
= -1, relayd_created
= 0;
3365 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3366 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3369 assert(relayd_sock
);
3371 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", relayd_id
);
3373 /* Get relayd reference if exists. */
3374 relayd
= consumer_find_relayd(relayd_id
);
3375 if (relayd
== NULL
) {
3376 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3377 /* Not found. Allocate one. */
3378 relayd
= consumer_allocate_relayd_sock_pair(relayd_id
);
3379 if (relayd
== NULL
) {
3381 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3384 relayd
->sessiond_session_id
= sessiond_id
;
3389 * This code path MUST continue to the consumer send status message to
3390 * we can notify the session daemon and continue our work without
3391 * killing everything.
3395 * relayd key should never be found for control socket.
3397 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3400 /* First send a status message before receiving the fds. */
3401 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3403 /* Somehow, the session daemon is not responding anymore. */
3404 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3405 goto error_nosignal
;
3408 /* Poll on consumer socket. */
3409 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3411 /* Needing to exit in the middle of a command: error. */
3412 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3414 goto error_nosignal
;
3417 /* Get relayd socket from session daemon */
3418 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3419 if (ret
!= sizeof(fd
)) {
3421 fd
= -1; /* Just in case it gets set with an invalid value. */
3424 * Failing to receive FDs might indicate a major problem such as
3425 * reaching a fd limit during the receive where the kernel returns a
3426 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3427 * don't take any chances and stop everything.
3429 * XXX: Feature request #558 will fix that and avoid this possible
3430 * issue when reaching the fd limit.
3432 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3433 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3437 /* Copy socket information and received FD */
3438 switch (sock_type
) {
3439 case LTTNG_STREAM_CONTROL
:
3440 /* Copy received lttcomm socket */
3441 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3442 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3443 /* Handle create_sock error. */
3445 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3449 * Close the socket created internally by
3450 * lttcomm_create_sock, so we can replace it by the one
3451 * received from sessiond.
3453 if (close(relayd
->control_sock
.sock
.fd
)) {
3457 /* Assign new file descriptor */
3458 relayd
->control_sock
.sock
.fd
= fd
;
3459 fd
= -1; /* For error path */
3460 /* Assign version values. */
3461 relayd
->control_sock
.major
= relayd_sock
->major
;
3462 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3464 relayd
->relayd_session_id
= relayd_session_id
;
3467 case LTTNG_STREAM_DATA
:
3468 /* Copy received lttcomm socket */
3469 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3470 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3471 /* Handle create_sock error. */
3473 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3477 * Close the socket created internally by
3478 * lttcomm_create_sock, so we can replace it by the one
3479 * received from sessiond.
3481 if (close(relayd
->data_sock
.sock
.fd
)) {
3485 /* Assign new file descriptor */
3486 relayd
->data_sock
.sock
.fd
= fd
;
3487 fd
= -1; /* for eventual error paths */
3488 /* Assign version values. */
3489 relayd
->data_sock
.major
= relayd_sock
->major
;
3490 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3493 ERR("Unknown relayd socket type (%d)", sock_type
);
3495 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3499 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3500 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3503 /* We successfully added the socket. Send status back. */
3504 ret
= consumer_send_status_msg(sock
, ret_code
);
3506 /* Somehow, the session daemon is not responding anymore. */
3507 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3508 goto error_nosignal
;
3512 * Add relayd socket pair to consumer data hashtable. If object already
3513 * exists or on error, the function gracefully returns.
3522 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3523 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3527 /* Close received socket if valid. */
3530 PERROR("close received socket");
3534 if (relayd_created
) {
3540 * Search for a relayd associated to the session id and return the reference.
3542 * A rcu read side lock MUST be acquire before calling this function and locked
3543 * until the relayd object is no longer necessary.
3545 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3547 struct lttng_ht_iter iter
;
3548 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3550 /* Iterate over all relayd since they are indexed by relayd_id. */
3551 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3554 * Check by sessiond id which is unique here where the relayd session
3555 * id might not be when having multiple relayd.
3557 if (relayd
->sessiond_session_id
== id
) {
3558 /* Found the relayd. There can be only one per id. */
3570 * Check if for a given session id there is still data needed to be extract
3573 * Return 1 if data is pending or else 0 meaning ready to be read.
3575 int consumer_data_pending(uint64_t id
)
3578 struct lttng_ht_iter iter
;
3579 struct lttng_ht
*ht
;
3580 struct lttng_consumer_stream
*stream
;
3581 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3582 int (*data_pending
)(struct lttng_consumer_stream
*);
3584 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3587 pthread_mutex_lock(&consumer_data
.lock
);
3589 switch (consumer_data
.type
) {
3590 case LTTNG_CONSUMER_KERNEL
:
3591 data_pending
= lttng_kconsumer_data_pending
;
3593 case LTTNG_CONSUMER32_UST
:
3594 case LTTNG_CONSUMER64_UST
:
3595 data_pending
= lttng_ustconsumer_data_pending
;
3598 ERR("Unknown consumer data type");
3602 /* Ease our life a bit */
3603 ht
= consumer_data
.stream_list_ht
;
3605 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3606 ht
->hash_fct(&id
, lttng_ht_seed
),
3608 &iter
.iter
, stream
, node_session_id
.node
) {
3609 pthread_mutex_lock(&stream
->lock
);
3612 * A removed node from the hash table indicates that the stream has
3613 * been deleted thus having a guarantee that the buffers are closed
3614 * on the consumer side. However, data can still be transmitted
3615 * over the network so don't skip the relayd check.
3617 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3619 /* Check the stream if there is data in the buffers. */
3620 ret
= data_pending(stream
);
3622 DBG("Data is pending locally on stream %" PRIu64
, stream
->key
);
3623 pthread_mutex_unlock(&stream
->lock
);
3628 pthread_mutex_unlock(&stream
->lock
);
3631 relayd
= find_relayd_by_session_id(id
);
3633 unsigned int is_data_inflight
= 0;
3635 /* Send init command for data pending. */
3636 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3637 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3638 relayd
->relayd_session_id
);
3640 /* Communication error thus the relayd so no data pending. */
3641 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3642 ERR("Relayd begin data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->id
);
3643 lttng_consumer_cleanup_relayd(relayd
);
3644 goto data_not_pending
;
3647 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3648 ht
->hash_fct(&id
, lttng_ht_seed
),
3650 &iter
.iter
, stream
, node_session_id
.node
) {
3651 if (stream
->metadata_flag
) {
3652 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3653 stream
->relayd_stream_id
);
3655 ret
= relayd_data_pending(&relayd
->control_sock
,
3656 stream
->relayd_stream_id
,
3657 stream
->next_net_seq_num
- 1);
3660 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3664 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->id
);
3665 lttng_consumer_cleanup_relayd(relayd
);
3666 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3667 goto data_not_pending
;
3671 /* Send end command for data pending. */
3672 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3673 relayd
->relayd_session_id
, &is_data_inflight
);
3674 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3676 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->id
);
3677 lttng_consumer_cleanup_relayd(relayd
);
3678 goto data_not_pending
;
3680 if (is_data_inflight
) {
3681 DBG("Data is in flight on relayd %" PRIu64
, relayd
->id
);
3687 * Finding _no_ node in the hash table and no inflight data means that the
3688 * stream(s) have been removed thus data is guaranteed to be available for
3689 * analysis from the trace files.
3693 /* Data is available to be read by a viewer. */
3694 pthread_mutex_unlock(&consumer_data
.lock
);
3699 /* Data is still being extracted from buffers. */
3700 pthread_mutex_unlock(&consumer_data
.lock
);
3706 * Send a ret code status message to the sessiond daemon.
3708 * Return the sendmsg() return value.
3710 int consumer_send_status_msg(int sock
, int ret_code
)
3712 struct lttcomm_consumer_status_msg msg
;
3714 memset(&msg
, 0, sizeof(msg
));
3715 msg
.ret_code
= ret_code
;
3717 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3721 * Send a channel status message to the sessiond daemon.
3723 * Return the sendmsg() return value.
3725 int consumer_send_status_channel(int sock
,
3726 struct lttng_consumer_channel
*channel
)
3728 struct lttcomm_consumer_status_channel msg
;
3732 memset(&msg
, 0, sizeof(msg
));
3734 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3736 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3737 msg
.key
= channel
->key
;
3738 msg
.stream_count
= channel
->streams
.count
;
3741 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3744 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3745 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3746 uint64_t max_sb_size
)
3748 unsigned long start_pos
;
3750 if (!nb_packets_per_stream
) {
3751 return consumed_pos
; /* Grab everything */
3753 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
3754 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3755 if ((long) (start_pos
- consumed_pos
) < 0) {
3756 return consumed_pos
; /* Grab everything */