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/time.h>
38 #include <common/compat/poll.h>
39 #include <common/compat/endian.h>
40 #include <common/index/index.h>
41 #include <common/kernel-ctl/kernel-ctl.h>
42 #include <common/sessiond-comm/relayd.h>
43 #include <common/sessiond-comm/sessiond-comm.h>
44 #include <common/kernel-consumer/kernel-consumer.h>
45 #include <common/relayd/relayd.h>
46 #include <common/ust-consumer/ust-consumer.h>
47 #include <common/consumer/consumer-timer.h>
48 #include <common/consumer/consumer.h>
49 #include <common/consumer/consumer-stream.h>
50 #include <common/consumer/consumer-testpoint.h>
51 #include <common/align.h>
52 #include <common/consumer/consumer-metadata-cache.h>
53 #include <common/trace-chunk.h>
54 #include <common/trace-chunk-registry.h>
55 #include <common/string-utils/format.h>
56 #include <common/dynamic-array.h>
58 struct lttng_consumer_global_data consumer_data
= {
61 .type
= LTTNG_CONSUMER_UNKNOWN
,
64 enum consumer_channel_action
{
67 CONSUMER_CHANNEL_QUIT
,
70 struct consumer_channel_msg
{
71 enum consumer_channel_action action
;
72 struct lttng_consumer_channel
*chan
; /* add */
73 uint64_t key
; /* del */
76 /* Flag used to temporarily pause data consumption from testpoints. */
77 int data_consumption_paused
;
80 * Flag to inform the polling thread to quit when all fd hung up. Updated by
81 * the consumer_thread_receive_fds when it notices that all fds has hung up.
82 * Also updated by the signal handler (consumer_should_exit()). Read by the
88 * Global hash table containing respectively metadata and data streams. The
89 * stream element in this ht should only be updated by the metadata poll thread
90 * for the metadata and the data poll thread for the data.
92 static struct lttng_ht
*metadata_ht
;
93 static struct lttng_ht
*data_ht
;
96 * Notify a thread lttng pipe to poll back again. This usually means that some
97 * global state has changed so we just send back the thread in a poll wait
100 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
102 struct lttng_consumer_stream
*null_stream
= NULL
;
106 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
109 static void notify_health_quit_pipe(int *pipe
)
113 ret
= lttng_write(pipe
[1], "4", 1);
115 PERROR("write consumer health quit");
119 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
120 struct lttng_consumer_channel
*chan
,
122 enum consumer_channel_action action
)
124 struct consumer_channel_msg msg
;
127 memset(&msg
, 0, sizeof(msg
));
132 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
133 if (ret
< sizeof(msg
)) {
134 PERROR("notify_channel_pipe write error");
138 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
141 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
144 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
145 struct lttng_consumer_channel
**chan
,
147 enum consumer_channel_action
*action
)
149 struct consumer_channel_msg msg
;
152 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
153 if (ret
< sizeof(msg
)) {
157 *action
= msg
.action
;
165 * Cleanup the stream list of a channel. Those streams are not yet globally
168 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
170 struct lttng_consumer_stream
*stream
, *stmp
;
174 /* Delete streams that might have been left in the stream list. */
175 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
177 cds_list_del(&stream
->send_node
);
179 * Once a stream is added to this list, the buffers were created so we
180 * have a guarantee that this call will succeed. Setting the monitor
181 * mode to 0 so we don't lock nor try to delete the stream from the
185 consumer_stream_destroy(stream
, NULL
);
190 * Find a stream. The consumer_data.lock must be locked during this
193 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
196 struct lttng_ht_iter iter
;
197 struct lttng_ht_node_u64
*node
;
198 struct lttng_consumer_stream
*stream
= NULL
;
202 /* -1ULL keys are lookup failures */
203 if (key
== (uint64_t) -1ULL) {
209 lttng_ht_lookup(ht
, &key
, &iter
);
210 node
= lttng_ht_iter_get_node_u64(&iter
);
212 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
220 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
222 struct lttng_consumer_stream
*stream
;
225 stream
= find_stream(key
, ht
);
227 stream
->key
= (uint64_t) -1ULL;
229 * We don't want the lookup to match, but we still need
230 * to iterate on this stream when iterating over the hash table. Just
231 * change the node key.
233 stream
->node
.key
= (uint64_t) -1ULL;
239 * Return a channel object for the given key.
241 * RCU read side lock MUST be acquired before calling this function and
242 * protects the channel ptr.
244 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
246 struct lttng_ht_iter iter
;
247 struct lttng_ht_node_u64
*node
;
248 struct lttng_consumer_channel
*channel
= NULL
;
250 /* -1ULL keys are lookup failures */
251 if (key
== (uint64_t) -1ULL) {
255 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
256 node
= lttng_ht_iter_get_node_u64(&iter
);
258 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
265 * There is a possibility that the consumer does not have enough time between
266 * the close of the channel on the session daemon and the cleanup in here thus
267 * once we have a channel add with an existing key, we know for sure that this
268 * channel will eventually get cleaned up by all streams being closed.
270 * This function just nullifies the already existing channel key.
272 static void steal_channel_key(uint64_t key
)
274 struct lttng_consumer_channel
*channel
;
277 channel
= consumer_find_channel(key
);
279 channel
->key
= (uint64_t) -1ULL;
281 * We don't want the lookup to match, but we still need to iterate on
282 * this channel when iterating over the hash table. Just change the
285 channel
->node
.key
= (uint64_t) -1ULL;
290 static void free_channel_rcu(struct rcu_head
*head
)
292 struct lttng_ht_node_u64
*node
=
293 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
294 struct lttng_consumer_channel
*channel
=
295 caa_container_of(node
, struct lttng_consumer_channel
, node
);
297 switch (consumer_data
.type
) {
298 case LTTNG_CONSUMER_KERNEL
:
300 case LTTNG_CONSUMER32_UST
:
301 case LTTNG_CONSUMER64_UST
:
302 lttng_ustconsumer_free_channel(channel
);
305 ERR("Unknown consumer_data type");
312 * RCU protected relayd socket pair free.
314 static void free_relayd_rcu(struct rcu_head
*head
)
316 struct lttng_ht_node_u64
*node
=
317 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
318 struct consumer_relayd_sock_pair
*relayd
=
319 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
322 * Close all sockets. This is done in the call RCU since we don't want the
323 * socket fds to be reassigned thus potentially creating bad state of the
326 * We do not have to lock the control socket mutex here since at this stage
327 * there is no one referencing to this relayd object.
329 (void) relayd_close(&relayd
->control_sock
);
330 (void) relayd_close(&relayd
->data_sock
);
332 pthread_mutex_destroy(&relayd
->ctrl_sock_mutex
);
337 * Destroy and free relayd socket pair object.
339 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
342 struct lttng_ht_iter iter
;
344 if (relayd
== NULL
) {
348 DBG("Consumer destroy and close relayd socket pair");
350 iter
.iter
.node
= &relayd
->node
.node
;
351 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
353 /* We assume the relayd is being or is destroyed */
357 /* RCU free() call */
358 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
362 * Remove a channel from the global list protected by a mutex. This function is
363 * also responsible for freeing its data structures.
365 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
367 struct lttng_ht_iter iter
;
369 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
371 pthread_mutex_lock(&consumer_data
.lock
);
372 pthread_mutex_lock(&channel
->lock
);
374 /* Destroy streams that might have been left in the stream list. */
375 clean_channel_stream_list(channel
);
377 if (channel
->live_timer_enabled
== 1) {
378 consumer_timer_live_stop(channel
);
380 if (channel
->monitor_timer_enabled
== 1) {
381 consumer_timer_monitor_stop(channel
);
384 switch (consumer_data
.type
) {
385 case LTTNG_CONSUMER_KERNEL
:
387 case LTTNG_CONSUMER32_UST
:
388 case LTTNG_CONSUMER64_UST
:
389 lttng_ustconsumer_del_channel(channel
);
392 ERR("Unknown consumer_data type");
397 lttng_trace_chunk_put(channel
->trace_chunk
);
398 channel
->trace_chunk
= NULL
;
400 if (channel
->is_published
) {
404 iter
.iter
.node
= &channel
->node
.node
;
405 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
408 iter
.iter
.node
= &channel
->channels_by_session_id_ht_node
.node
;
409 ret
= lttng_ht_del(consumer_data
.channels_by_session_id_ht
,
415 channel
->is_deleted
= true;
416 call_rcu(&channel
->node
.head
, free_channel_rcu
);
418 pthread_mutex_unlock(&channel
->lock
);
419 pthread_mutex_unlock(&consumer_data
.lock
);
423 * Iterate over the relayd hash table and destroy each element. Finally,
424 * destroy the whole hash table.
426 static void cleanup_relayd_ht(void)
428 struct lttng_ht_iter iter
;
429 struct consumer_relayd_sock_pair
*relayd
;
433 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
435 consumer_destroy_relayd(relayd
);
440 lttng_ht_destroy(consumer_data
.relayd_ht
);
444 * Update the end point status of all streams having the given network sequence
445 * index (relayd index).
447 * It's atomically set without having the stream mutex locked which is fine
448 * because we handle the write/read race with a pipe wakeup for each thread.
450 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
451 enum consumer_endpoint_status status
)
453 struct lttng_ht_iter iter
;
454 struct lttng_consumer_stream
*stream
;
456 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
460 /* Let's begin with metadata */
461 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
462 if (stream
->net_seq_idx
== net_seq_idx
) {
463 uatomic_set(&stream
->endpoint_status
, status
);
464 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
468 /* Follow up by the data streams */
469 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
470 if (stream
->net_seq_idx
== net_seq_idx
) {
471 uatomic_set(&stream
->endpoint_status
, status
);
472 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
479 * Cleanup a relayd object by flagging every associated streams for deletion,
480 * destroying the object meaning removing it from the relayd hash table,
481 * closing the sockets and freeing the memory in a RCU call.
483 * If a local data context is available, notify the threads that the streams'
484 * state have changed.
486 void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
)
492 DBG("Cleaning up relayd object ID %"PRIu64
, relayd
->net_seq_idx
);
494 /* Save the net sequence index before destroying the object */
495 netidx
= relayd
->net_seq_idx
;
498 * Delete the relayd from the relayd hash table, close the sockets and free
499 * the object in a RCU call.
501 consumer_destroy_relayd(relayd
);
503 /* Set inactive endpoint to all streams */
504 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
507 * With a local data context, notify the threads that the streams' state
508 * have changed. The write() action on the pipe acts as an "implicit"
509 * memory barrier ordering the updates of the end point status from the
510 * read of this status which happens AFTER receiving this notify.
512 notify_thread_lttng_pipe(relayd
->ctx
->consumer_data_pipe
);
513 notify_thread_lttng_pipe(relayd
->ctx
->consumer_metadata_pipe
);
517 * Flag a relayd socket pair for destruction. Destroy it if the refcount
520 * RCU read side lock MUST be aquired before calling this function.
522 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
526 /* Set destroy flag for this object */
527 uatomic_set(&relayd
->destroy_flag
, 1);
529 /* Destroy the relayd if refcount is 0 */
530 if (uatomic_read(&relayd
->refcount
) == 0) {
531 consumer_destroy_relayd(relayd
);
536 * Completly destroy stream from every visiable data structure and the given
539 * One this call returns, the stream object is not longer usable nor visible.
541 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
544 consumer_stream_destroy(stream
, ht
);
548 * XXX naming of del vs destroy is all mixed up.
550 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
552 consumer_stream_destroy(stream
, data_ht
);
555 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
557 consumer_stream_destroy(stream
, metadata_ht
);
560 void consumer_stream_update_channel_attributes(
561 struct lttng_consumer_stream
*stream
,
562 struct lttng_consumer_channel
*channel
)
564 stream
->channel_read_only_attributes
.tracefile_size
=
565 channel
->tracefile_size
;
569 * Add a stream to the global list protected by a mutex.
571 void consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
573 struct lttng_ht
*ht
= data_ht
;
578 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
580 pthread_mutex_lock(&consumer_data
.lock
);
581 pthread_mutex_lock(&stream
->chan
->lock
);
582 pthread_mutex_lock(&stream
->chan
->timer_lock
);
583 pthread_mutex_lock(&stream
->lock
);
586 /* Steal stream identifier to avoid having streams with the same key */
587 steal_stream_key(stream
->key
, ht
);
589 lttng_ht_add_unique_u64(ht
, &stream
->node
);
591 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
592 &stream
->node_channel_id
);
595 * Add stream to the stream_list_ht of the consumer data. No need to steal
596 * the key since the HT does not use it and we allow to add redundant keys
599 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
602 * When nb_init_stream_left reaches 0, we don't need to trigger any action
603 * in terms of destroying the associated channel, because the action that
604 * causes the count to become 0 also causes a stream to be added. The
605 * channel deletion will thus be triggered by the following removal of this
608 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
609 /* Increment refcount before decrementing nb_init_stream_left */
611 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
614 /* Update consumer data once the node is inserted. */
615 consumer_data
.stream_count
++;
616 consumer_data
.need_update
= 1;
619 pthread_mutex_unlock(&stream
->lock
);
620 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
621 pthread_mutex_unlock(&stream
->chan
->lock
);
622 pthread_mutex_unlock(&consumer_data
.lock
);
625 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
627 consumer_del_stream(stream
, data_ht
);
631 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
632 * be acquired before calling this.
634 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
637 struct lttng_ht_node_u64
*node
;
638 struct lttng_ht_iter iter
;
642 lttng_ht_lookup(consumer_data
.relayd_ht
,
643 &relayd
->net_seq_idx
, &iter
);
644 node
= lttng_ht_iter_get_node_u64(&iter
);
648 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
655 * Allocate and return a consumer relayd socket.
657 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
658 uint64_t net_seq_idx
)
660 struct consumer_relayd_sock_pair
*obj
= NULL
;
662 /* net sequence index of -1 is a failure */
663 if (net_seq_idx
== (uint64_t) -1ULL) {
667 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
669 PERROR("zmalloc relayd sock");
673 obj
->net_seq_idx
= net_seq_idx
;
675 obj
->destroy_flag
= 0;
676 obj
->control_sock
.sock
.fd
= -1;
677 obj
->data_sock
.sock
.fd
= -1;
678 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
679 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
686 * Find a relayd socket pair in the global consumer data.
688 * Return the object if found else NULL.
689 * RCU read-side lock must be held across this call and while using the
692 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
694 struct lttng_ht_iter iter
;
695 struct lttng_ht_node_u64
*node
;
696 struct consumer_relayd_sock_pair
*relayd
= NULL
;
698 /* Negative keys are lookup failures */
699 if (key
== (uint64_t) -1ULL) {
703 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
705 node
= lttng_ht_iter_get_node_u64(&iter
);
707 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
715 * Find a relayd and send the stream
717 * Returns 0 on success, < 0 on error
719 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
723 struct consumer_relayd_sock_pair
*relayd
;
726 assert(stream
->net_seq_idx
!= -1ULL);
729 /* The stream is not metadata. Get relayd reference if exists. */
731 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
732 if (relayd
!= NULL
) {
733 /* Add stream on the relayd */
734 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
735 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
736 path
, &stream
->relayd_stream_id
,
737 stream
->chan
->tracefile_size
,
738 stream
->chan
->tracefile_count
,
739 stream
->trace_chunk
);
740 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
742 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
743 lttng_consumer_cleanup_relayd(relayd
);
747 uatomic_inc(&relayd
->refcount
);
748 stream
->sent_to_relayd
= 1;
750 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
751 stream
->key
, stream
->net_seq_idx
);
756 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
757 stream
->name
, stream
->key
, stream
->net_seq_idx
);
765 * Find a relayd and send the streams sent message
767 * Returns 0 on success, < 0 on error
769 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
772 struct consumer_relayd_sock_pair
*relayd
;
774 assert(net_seq_idx
!= -1ULL);
776 /* The stream is not metadata. Get relayd reference if exists. */
778 relayd
= consumer_find_relayd(net_seq_idx
);
779 if (relayd
!= NULL
) {
780 /* Add stream on the relayd */
781 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
782 ret
= relayd_streams_sent(&relayd
->control_sock
);
783 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
785 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
786 lttng_consumer_cleanup_relayd(relayd
);
790 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
797 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
805 * Find a relayd and close the stream
807 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
809 struct consumer_relayd_sock_pair
*relayd
;
811 /* The stream is not metadata. Get relayd reference if exists. */
813 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
815 consumer_stream_relayd_close(stream
, relayd
);
821 * Handle stream for relayd transmission if the stream applies for network
822 * streaming where the net sequence index is set.
824 * Return destination file descriptor or negative value on error.
826 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
827 size_t data_size
, unsigned long padding
,
828 struct consumer_relayd_sock_pair
*relayd
)
831 struct lttcomm_relayd_data_hdr data_hdr
;
837 /* Reset data header */
838 memset(&data_hdr
, 0, sizeof(data_hdr
));
840 if (stream
->metadata_flag
) {
841 /* Caller MUST acquire the relayd control socket lock */
842 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
847 /* Metadata are always sent on the control socket. */
848 outfd
= relayd
->control_sock
.sock
.fd
;
850 /* Set header with stream information */
851 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
852 data_hdr
.data_size
= htobe32(data_size
);
853 data_hdr
.padding_size
= htobe32(padding
);
856 * Note that net_seq_num below is assigned with the *current* value of
857 * next_net_seq_num and only after that the next_net_seq_num will be
858 * increment. This is why when issuing a command on the relayd using
859 * this next value, 1 should always be substracted in order to compare
860 * the last seen sequence number on the relayd side to the last sent.
862 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
863 /* Other fields are zeroed previously */
865 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
871 ++stream
->next_net_seq_num
;
873 /* Set to go on data socket */
874 outfd
= relayd
->data_sock
.sock
.fd
;
882 * Trigger a dump of the metadata content. Following/during the succesful
883 * completion of this call, the metadata poll thread will start receiving
884 * metadata packets to consume.
886 * The caller must hold the channel and stream locks.
889 int consumer_metadata_stream_dump(struct lttng_consumer_stream
*stream
)
893 ASSERT_LOCKED(stream
->chan
->lock
);
894 ASSERT_LOCKED(stream
->lock
);
895 assert(stream
->metadata_flag
);
896 assert(stream
->chan
->trace_chunk
);
898 switch (consumer_data
.type
) {
899 case LTTNG_CONSUMER_KERNEL
:
901 * Reset the position of what has been read from the
902 * metadata cache to 0 so we can dump it again.
904 ret
= kernctl_metadata_cache_dump(stream
->wait_fd
);
906 case LTTNG_CONSUMER32_UST
:
907 case LTTNG_CONSUMER64_UST
:
909 * Reset the position pushed from the metadata cache so it
910 * will write from the beginning on the next push.
912 stream
->ust_metadata_pushed
= 0;
913 ret
= consumer_metadata_wakeup_pipe(stream
->chan
);
916 ERR("Unknown consumer_data type");
920 ERR("Failed to dump the metadata cache");
926 int lttng_consumer_channel_set_trace_chunk(
927 struct lttng_consumer_channel
*channel
,
928 struct lttng_trace_chunk
*new_trace_chunk
)
930 pthread_mutex_lock(&channel
->lock
);
931 if (channel
->is_deleted
) {
933 * The channel has been logically deleted and should no longer
934 * be used. It has released its reference to its current trace
935 * chunk and should not acquire a new one.
937 * Return success as there is nothing for the caller to do.
943 * The acquisition of the reference cannot fail (barring
944 * a severe internal error) since a reference to the published
945 * chunk is already held by the caller.
947 if (new_trace_chunk
) {
948 const bool acquired_reference
= lttng_trace_chunk_get(
951 assert(acquired_reference
);
954 lttng_trace_chunk_put(channel
->trace_chunk
);
955 channel
->trace_chunk
= new_trace_chunk
;
957 pthread_mutex_unlock(&channel
->lock
);
962 * Allocate and return a new lttng_consumer_channel object using the given key
963 * to initialize the hash table node.
965 * On error, return NULL.
967 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
969 const uint64_t *chunk_id
,
970 const char *pathname
,
973 enum lttng_event_output output
,
974 uint64_t tracefile_size
,
975 uint64_t tracefile_count
,
976 uint64_t session_id_per_pid
,
977 unsigned int monitor
,
978 unsigned int live_timer_interval
,
979 bool is_in_live_session
,
980 const char *root_shm_path
,
981 const char *shm_path
)
983 struct lttng_consumer_channel
*channel
= NULL
;
984 struct lttng_trace_chunk
*trace_chunk
= NULL
;
987 trace_chunk
= lttng_trace_chunk_registry_find_chunk(
988 consumer_data
.chunk_registry
, session_id
,
991 ERR("Failed to find trace chunk reference during creation of channel");
996 channel
= zmalloc(sizeof(*channel
));
997 if (channel
== NULL
) {
998 PERROR("malloc struct lttng_consumer_channel");
1003 channel
->refcount
= 0;
1004 channel
->session_id
= session_id
;
1005 channel
->session_id_per_pid
= session_id_per_pid
;
1006 channel
->relayd_id
= relayd_id
;
1007 channel
->tracefile_size
= tracefile_size
;
1008 channel
->tracefile_count
= tracefile_count
;
1009 channel
->monitor
= monitor
;
1010 channel
->live_timer_interval
= live_timer_interval
;
1011 channel
->is_live
= is_in_live_session
;
1012 pthread_mutex_init(&channel
->lock
, NULL
);
1013 pthread_mutex_init(&channel
->timer_lock
, NULL
);
1016 case LTTNG_EVENT_SPLICE
:
1017 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
1019 case LTTNG_EVENT_MMAP
:
1020 channel
->output
= CONSUMER_CHANNEL_MMAP
;
1030 * In monitor mode, the streams associated with the channel will be put in
1031 * a special list ONLY owned by this channel. So, the refcount is set to 1
1032 * here meaning that the channel itself has streams that are referenced.
1034 * On a channel deletion, once the channel is no longer visible, the
1035 * refcount is decremented and checked for a zero value to delete it. With
1036 * streams in no monitor mode, it will now be safe to destroy the channel.
1038 if (!channel
->monitor
) {
1039 channel
->refcount
= 1;
1042 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1043 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1045 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1046 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1048 if (root_shm_path
) {
1049 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1050 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1053 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1054 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1057 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1058 lttng_ht_node_init_u64(&channel
->channels_by_session_id_ht_node
,
1059 channel
->session_id
);
1061 channel
->wait_fd
= -1;
1062 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1065 int ret
= lttng_consumer_channel_set_trace_chunk(channel
,
1072 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1075 lttng_trace_chunk_put(trace_chunk
);
1078 consumer_del_channel(channel
);
1084 * Add a channel to the global list protected by a mutex.
1086 * Always return 0 indicating success.
1088 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1089 struct lttng_consumer_local_data
*ctx
)
1091 pthread_mutex_lock(&consumer_data
.lock
);
1092 pthread_mutex_lock(&channel
->lock
);
1093 pthread_mutex_lock(&channel
->timer_lock
);
1096 * This gives us a guarantee that the channel we are about to add to the
1097 * channel hash table will be unique. See this function comment on the why
1098 * we need to steel the channel key at this stage.
1100 steal_channel_key(channel
->key
);
1103 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1104 lttng_ht_add_u64(consumer_data
.channels_by_session_id_ht
,
1105 &channel
->channels_by_session_id_ht_node
);
1107 channel
->is_published
= true;
1109 pthread_mutex_unlock(&channel
->timer_lock
);
1110 pthread_mutex_unlock(&channel
->lock
);
1111 pthread_mutex_unlock(&consumer_data
.lock
);
1113 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1114 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1121 * Allocate the pollfd structure and the local view of the out fds to avoid
1122 * doing a lookup in the linked list and concurrency issues when writing is
1123 * needed. Called with consumer_data.lock held.
1125 * Returns the number of fds in the structures.
1127 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1128 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1129 struct lttng_ht
*ht
, int *nb_inactive_fd
)
1132 struct lttng_ht_iter iter
;
1133 struct lttng_consumer_stream
*stream
;
1138 assert(local_stream
);
1140 DBG("Updating poll fd array");
1141 *nb_inactive_fd
= 0;
1143 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1145 * Only active streams with an active end point can be added to the
1146 * poll set and local stream storage of the thread.
1148 * There is a potential race here for endpoint_status to be updated
1149 * just after the check. However, this is OK since the stream(s) will
1150 * be deleted once the thread is notified that the end point state has
1151 * changed where this function will be called back again.
1153 * We track the number of inactive FDs because they still need to be
1154 * closed by the polling thread after a wakeup on the data_pipe or
1157 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1158 (*nb_inactive_fd
)++;
1162 * This clobbers way too much the debug output. Uncomment that if you
1163 * need it for debugging purposes.
1165 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1166 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1167 local_stream
[i
] = stream
;
1173 * Insert the consumer_data_pipe at the end of the array and don't
1174 * increment i so nb_fd is the number of real FD.
1176 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1177 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1179 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1180 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1185 * Poll on the should_quit pipe and the command socket return -1 on
1186 * error, 1 if should exit, 0 if data is available on the command socket
1188 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1193 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1194 if (num_rdy
== -1) {
1196 * Restart interrupted system call.
1198 if (errno
== EINTR
) {
1201 PERROR("Poll error");
1204 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1205 DBG("consumer_should_quit wake up");
1212 * Set the error socket.
1214 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1217 ctx
->consumer_error_socket
= sock
;
1221 * Set the command socket path.
1223 void lttng_consumer_set_command_sock_path(
1224 struct lttng_consumer_local_data
*ctx
, char *sock
)
1226 ctx
->consumer_command_sock_path
= sock
;
1230 * Send return code to the session daemon.
1231 * If the socket is not defined, we return 0, it is not a fatal error
1233 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1235 if (ctx
->consumer_error_socket
> 0) {
1236 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1237 sizeof(enum lttcomm_sessiond_command
));
1244 * Close all the tracefiles and stream fds and MUST be called when all
1245 * instances are destroyed i.e. when all threads were joined and are ended.
1247 void lttng_consumer_cleanup(void)
1249 struct lttng_ht_iter iter
;
1250 struct lttng_consumer_channel
*channel
;
1251 unsigned int trace_chunks_left
;
1255 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1257 consumer_del_channel(channel
);
1262 lttng_ht_destroy(consumer_data
.channel_ht
);
1263 lttng_ht_destroy(consumer_data
.channels_by_session_id_ht
);
1265 cleanup_relayd_ht();
1267 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1270 * This HT contains streams that are freed by either the metadata thread or
1271 * the data thread so we do *nothing* on the hash table and simply destroy
1274 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1277 * Trace chunks in the registry may still exist if the session
1278 * daemon has encountered an internal error and could not
1279 * tear down its sessions and/or trace chunks properly.
1281 * Release the session daemon's implicit reference to any remaining
1282 * trace chunk and print an error if any trace chunk was found. Note
1283 * that there are _no_ legitimate cases for trace chunks to be left,
1284 * it is a leak. However, it can happen following a crash of the
1285 * session daemon and not emptying the registry would cause an assertion
1288 trace_chunks_left
= lttng_trace_chunk_registry_put_each_chunk(
1289 consumer_data
.chunk_registry
);
1290 if (trace_chunks_left
) {
1291 ERR("%u trace chunks are leaked by lttng-consumerd. "
1292 "This can be caused by an internal error of the session daemon.",
1295 /* Run all callbacks freeing each chunk. */
1297 lttng_trace_chunk_registry_destroy(consumer_data
.chunk_registry
);
1301 * Called from signal handler.
1303 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1307 CMM_STORE_SHARED(consumer_quit
, 1);
1308 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1310 PERROR("write consumer quit");
1313 DBG("Consumer flag that it should quit");
1318 * Flush pending writes to trace output disk file.
1321 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1325 int outfd
= stream
->out_fd
;
1328 * This does a blocking write-and-wait on any page that belongs to the
1329 * subbuffer prior to the one we just wrote.
1330 * Don't care about error values, as these are just hints and ways to
1331 * limit the amount of page cache used.
1333 if (orig_offset
< stream
->max_sb_size
) {
1336 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1337 stream
->max_sb_size
,
1338 SYNC_FILE_RANGE_WAIT_BEFORE
1339 | SYNC_FILE_RANGE_WRITE
1340 | SYNC_FILE_RANGE_WAIT_AFTER
);
1342 * Give hints to the kernel about how we access the file:
1343 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1346 * We need to call fadvise again after the file grows because the
1347 * kernel does not seem to apply fadvise to non-existing parts of the
1350 * Call fadvise _after_ having waited for the page writeback to
1351 * complete because the dirty page writeback semantic is not well
1352 * defined. So it can be expected to lead to lower throughput in
1355 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1356 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1357 if (ret
&& ret
!= -ENOSYS
) {
1359 PERROR("posix_fadvise on fd %i", outfd
);
1364 * Initialise the necessary environnement :
1365 * - create a new context
1366 * - create the poll_pipe
1367 * - create the should_quit pipe (for signal handler)
1368 * - create the thread pipe (for splice)
1370 * Takes a function pointer as argument, this function is called when data is
1371 * available on a buffer. This function is responsible to do the
1372 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1373 * buffer configuration and then kernctl_put_next_subbuf at the end.
1375 * Returns a pointer to the new context or NULL on error.
1377 struct lttng_consumer_local_data
*lttng_consumer_create(
1378 enum lttng_consumer_type type
,
1379 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1380 struct lttng_consumer_local_data
*ctx
, bool locked_by_caller
),
1381 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1382 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1383 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1386 struct lttng_consumer_local_data
*ctx
;
1388 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1389 consumer_data
.type
== type
);
1390 consumer_data
.type
= type
;
1392 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1394 PERROR("allocating context");
1398 ctx
->consumer_error_socket
= -1;
1399 ctx
->consumer_metadata_socket
= -1;
1400 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1401 /* assign the callbacks */
1402 ctx
->on_buffer_ready
= buffer_ready
;
1403 ctx
->on_recv_channel
= recv_channel
;
1404 ctx
->on_recv_stream
= recv_stream
;
1405 ctx
->on_update_stream
= update_stream
;
1407 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1408 if (!ctx
->consumer_data_pipe
) {
1409 goto error_poll_pipe
;
1412 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1413 if (!ctx
->consumer_wakeup_pipe
) {
1414 goto error_wakeup_pipe
;
1417 ret
= pipe(ctx
->consumer_should_quit
);
1419 PERROR("Error creating recv pipe");
1420 goto error_quit_pipe
;
1423 ret
= pipe(ctx
->consumer_channel_pipe
);
1425 PERROR("Error creating channel pipe");
1426 goto error_channel_pipe
;
1429 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1430 if (!ctx
->consumer_metadata_pipe
) {
1431 goto error_metadata_pipe
;
1434 ctx
->channel_monitor_pipe
= -1;
1438 error_metadata_pipe
:
1439 utils_close_pipe(ctx
->consumer_channel_pipe
);
1441 utils_close_pipe(ctx
->consumer_should_quit
);
1443 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1445 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1453 * Iterate over all streams of the hashtable and free them properly.
1455 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1457 struct lttng_ht_iter iter
;
1458 struct lttng_consumer_stream
*stream
;
1465 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1467 * Ignore return value since we are currently cleaning up so any error
1470 (void) consumer_del_stream(stream
, ht
);
1474 lttng_ht_destroy(ht
);
1478 * Iterate over all streams of the metadata hashtable and free them
1481 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1483 struct lttng_ht_iter iter
;
1484 struct lttng_consumer_stream
*stream
;
1491 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1493 * Ignore return value since we are currently cleaning up so any error
1496 (void) consumer_del_metadata_stream(stream
, ht
);
1500 lttng_ht_destroy(ht
);
1504 * Close all fds associated with the instance and free the context.
1506 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1510 DBG("Consumer destroying it. Closing everything.");
1516 destroy_data_stream_ht(data_ht
);
1517 destroy_metadata_stream_ht(metadata_ht
);
1519 ret
= close(ctx
->consumer_error_socket
);
1523 ret
= close(ctx
->consumer_metadata_socket
);
1527 utils_close_pipe(ctx
->consumer_channel_pipe
);
1528 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1529 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1530 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1531 utils_close_pipe(ctx
->consumer_should_quit
);
1533 unlink(ctx
->consumer_command_sock_path
);
1538 * Write the metadata stream id on the specified file descriptor.
1540 static int write_relayd_metadata_id(int fd
,
1541 struct lttng_consumer_stream
*stream
,
1542 unsigned long padding
)
1545 struct lttcomm_relayd_metadata_payload hdr
;
1547 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1548 hdr
.padding_size
= htobe32(padding
);
1549 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1550 if (ret
< sizeof(hdr
)) {
1552 * This error means that the fd's end is closed so ignore the PERROR
1553 * not to clubber the error output since this can happen in a normal
1556 if (errno
!= EPIPE
) {
1557 PERROR("write metadata stream id");
1559 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1561 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1562 * handle writting the missing part so report that as an error and
1563 * don't lie to the caller.
1568 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1569 stream
->relayd_stream_id
, padding
);
1576 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1577 * core function for writing trace buffers to either the local filesystem or
1580 * It must be called with the stream and the channel lock held.
1582 * Careful review MUST be put if any changes occur!
1584 * Returns the number of bytes written
1586 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1587 struct lttng_consumer_local_data
*ctx
,
1588 struct lttng_consumer_stream
*stream
,
1589 const struct lttng_buffer_view
*buffer
,
1590 unsigned long padding
)
1593 off_t orig_offset
= stream
->out_fd_offset
;
1594 /* Default is on the disk */
1595 int outfd
= stream
->out_fd
;
1596 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1597 unsigned int relayd_hang_up
= 0;
1598 const size_t subbuf_content_size
= buffer
->size
- padding
;
1601 /* RCU lock for the relayd pointer */
1603 assert(stream
->net_seq_idx
!= (uint64_t) -1ULL ||
1604 stream
->trace_chunk
);
1606 /* Flag that the current stream if set for network streaming. */
1607 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1608 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1609 if (relayd
== NULL
) {
1615 /* Handle stream on the relayd if the output is on the network */
1617 unsigned long netlen
= subbuf_content_size
;
1620 * Lock the control socket for the complete duration of the function
1621 * since from this point on we will use the socket.
1623 if (stream
->metadata_flag
) {
1624 /* Metadata requires the control socket. */
1625 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1626 if (stream
->reset_metadata_flag
) {
1627 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1628 stream
->relayd_stream_id
,
1629 stream
->metadata_version
);
1634 stream
->reset_metadata_flag
= 0;
1636 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1639 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1644 /* Use the returned socket. */
1647 /* Write metadata stream id before payload */
1648 if (stream
->metadata_flag
) {
1649 ret
= write_relayd_metadata_id(outfd
, stream
, padding
);
1656 write_len
= subbuf_content_size
;
1658 /* No streaming; we have to write the full padding. */
1659 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1660 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1662 ERR("Reset metadata file");
1665 stream
->reset_metadata_flag
= 0;
1669 * Check if we need to change the tracefile before writing the packet.
1671 if (stream
->chan
->tracefile_size
> 0 &&
1672 (stream
->tracefile_size_current
+ buffer
->size
) >
1673 stream
->chan
->tracefile_size
) {
1674 ret
= consumer_stream_rotate_output_files(stream
);
1678 outfd
= stream
->out_fd
;
1681 stream
->tracefile_size_current
+= buffer
->size
;
1682 write_len
= buffer
->size
;
1686 * This call guarantee that len or less is returned. It's impossible to
1687 * receive a ret value that is bigger than len.
1689 ret
= lttng_write(outfd
, buffer
->data
, write_len
);
1690 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, write_len
);
1691 if (ret
< 0 || ((size_t) ret
!= write_len
)) {
1693 * Report error to caller if nothing was written else at least send the
1701 /* Socket operation failed. We consider the relayd dead */
1702 if (errno
== EPIPE
) {
1704 * This is possible if the fd is closed on the other side
1705 * (outfd) or any write problem. It can be verbose a bit for a
1706 * normal execution if for instance the relayd is stopped
1707 * abruptly. This can happen so set this to a DBG statement.
1709 DBG("Consumer mmap write detected relayd hang up");
1711 /* Unhandled error, print it and stop function right now. */
1712 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret
,
1717 stream
->output_written
+= ret
;
1719 /* This call is useless on a socket so better save a syscall. */
1721 /* This won't block, but will start writeout asynchronously */
1722 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, write_len
,
1723 SYNC_FILE_RANGE_WRITE
);
1724 stream
->out_fd_offset
+= write_len
;
1725 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1730 * This is a special case that the relayd has closed its socket. Let's
1731 * cleanup the relayd object and all associated streams.
1733 if (relayd
&& relayd_hang_up
) {
1734 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1735 lttng_consumer_cleanup_relayd(relayd
);
1739 /* Unlock only if ctrl socket used */
1740 if (relayd
&& stream
->metadata_flag
) {
1741 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1749 * Splice the data from the ring buffer to the tracefile.
1751 * It must be called with the stream lock held.
1753 * Returns the number of bytes spliced.
1755 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1756 struct lttng_consumer_local_data
*ctx
,
1757 struct lttng_consumer_stream
*stream
, unsigned long len
,
1758 unsigned long padding
)
1760 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1762 off_t orig_offset
= stream
->out_fd_offset
;
1763 int fd
= stream
->wait_fd
;
1764 /* Default is on the disk */
1765 int outfd
= stream
->out_fd
;
1766 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1768 unsigned int relayd_hang_up
= 0;
1770 switch (consumer_data
.type
) {
1771 case LTTNG_CONSUMER_KERNEL
:
1773 case LTTNG_CONSUMER32_UST
:
1774 case LTTNG_CONSUMER64_UST
:
1775 /* Not supported for user space tracing */
1778 ERR("Unknown consumer_data type");
1782 /* RCU lock for the relayd pointer */
1785 /* Flag that the current stream if set for network streaming. */
1786 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1787 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1788 if (relayd
== NULL
) {
1793 splice_pipe
= stream
->splice_pipe
;
1795 /* Write metadata stream id before payload */
1797 unsigned long total_len
= len
;
1799 if (stream
->metadata_flag
) {
1801 * Lock the control socket for the complete duration of the function
1802 * since from this point on we will use the socket.
1804 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1806 if (stream
->reset_metadata_flag
) {
1807 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1808 stream
->relayd_stream_id
,
1809 stream
->metadata_version
);
1814 stream
->reset_metadata_flag
= 0;
1816 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
,
1824 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1827 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1833 /* Use the returned socket. */
1836 /* No streaming, we have to set the len with the full padding */
1839 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1840 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1842 ERR("Reset metadata file");
1845 stream
->reset_metadata_flag
= 0;
1848 * Check if we need to change the tracefile before writing the packet.
1850 if (stream
->chan
->tracefile_size
> 0 &&
1851 (stream
->tracefile_size_current
+ len
) >
1852 stream
->chan
->tracefile_size
) {
1853 ret
= consumer_stream_rotate_output_files(stream
);
1858 outfd
= stream
->out_fd
;
1861 stream
->tracefile_size_current
+= len
;
1865 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1866 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1867 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1868 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1869 DBG("splice chan to pipe, ret %zd", ret_splice
);
1870 if (ret_splice
< 0) {
1873 PERROR("Error in relay splice");
1877 /* Handle stream on the relayd if the output is on the network */
1878 if (relayd
&& stream
->metadata_flag
) {
1879 size_t metadata_payload_size
=
1880 sizeof(struct lttcomm_relayd_metadata_payload
);
1882 /* Update counter to fit the spliced data */
1883 ret_splice
+= metadata_payload_size
;
1884 len
+= metadata_payload_size
;
1886 * We do this so the return value can match the len passed as
1887 * argument to this function.
1889 written
-= metadata_payload_size
;
1892 /* Splice data out */
1893 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1894 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1895 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1897 if (ret_splice
< 0) {
1902 } else if (ret_splice
> len
) {
1904 * We don't expect this code path to be executed but you never know
1905 * so this is an extra protection agains a buggy splice().
1908 written
+= ret_splice
;
1909 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1913 /* All good, update current len and continue. */
1917 /* This call is useless on a socket so better save a syscall. */
1919 /* This won't block, but will start writeout asynchronously */
1920 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1921 SYNC_FILE_RANGE_WRITE
);
1922 stream
->out_fd_offset
+= ret_splice
;
1924 stream
->output_written
+= ret_splice
;
1925 written
+= ret_splice
;
1928 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1934 * This is a special case that the relayd has closed its socket. Let's
1935 * cleanup the relayd object and all associated streams.
1937 if (relayd
&& relayd_hang_up
) {
1938 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1939 lttng_consumer_cleanup_relayd(relayd
);
1940 /* Skip splice error so the consumer does not fail */
1945 /* send the appropriate error description to sessiond */
1948 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1951 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1954 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1959 if (relayd
&& stream
->metadata_flag
) {
1960 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1968 * Sample the snapshot positions for a specific fd
1970 * Returns 0 on success, < 0 on error
1972 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
1974 switch (consumer_data
.type
) {
1975 case LTTNG_CONSUMER_KERNEL
:
1976 return lttng_kconsumer_sample_snapshot_positions(stream
);
1977 case LTTNG_CONSUMER32_UST
:
1978 case LTTNG_CONSUMER64_UST
:
1979 return lttng_ustconsumer_sample_snapshot_positions(stream
);
1981 ERR("Unknown consumer_data type");
1987 * Take a snapshot for a specific fd
1989 * Returns 0 on success, < 0 on error
1991 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1993 switch (consumer_data
.type
) {
1994 case LTTNG_CONSUMER_KERNEL
:
1995 return lttng_kconsumer_take_snapshot(stream
);
1996 case LTTNG_CONSUMER32_UST
:
1997 case LTTNG_CONSUMER64_UST
:
1998 return lttng_ustconsumer_take_snapshot(stream
);
2000 ERR("Unknown consumer_data type");
2007 * Get the produced position
2009 * Returns 0 on success, < 0 on error
2011 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
2014 switch (consumer_data
.type
) {
2015 case LTTNG_CONSUMER_KERNEL
:
2016 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
2017 case LTTNG_CONSUMER32_UST
:
2018 case LTTNG_CONSUMER64_UST
:
2019 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2021 ERR("Unknown consumer_data type");
2028 * Get the consumed position (free-running counter position in bytes).
2030 * Returns 0 on success, < 0 on error
2032 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
2035 switch (consumer_data
.type
) {
2036 case LTTNG_CONSUMER_KERNEL
:
2037 return lttng_kconsumer_get_consumed_snapshot(stream
, pos
);
2038 case LTTNG_CONSUMER32_UST
:
2039 case LTTNG_CONSUMER64_UST
:
2040 return lttng_ustconsumer_get_consumed_snapshot(stream
, pos
);
2042 ERR("Unknown consumer_data type");
2048 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2049 int sock
, struct pollfd
*consumer_sockpoll
)
2051 switch (consumer_data
.type
) {
2052 case LTTNG_CONSUMER_KERNEL
:
2053 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2054 case LTTNG_CONSUMER32_UST
:
2055 case LTTNG_CONSUMER64_UST
:
2056 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2058 ERR("Unknown consumer_data type");
2064 void lttng_consumer_close_all_metadata(void)
2066 switch (consumer_data
.type
) {
2067 case LTTNG_CONSUMER_KERNEL
:
2069 * The Kernel consumer has a different metadata scheme so we don't
2070 * close anything because the stream will be closed by the session
2074 case LTTNG_CONSUMER32_UST
:
2075 case LTTNG_CONSUMER64_UST
:
2077 * Close all metadata streams. The metadata hash table is passed and
2078 * this call iterates over it by closing all wakeup fd. This is safe
2079 * because at this point we are sure that the metadata producer is
2080 * either dead or blocked.
2082 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2085 ERR("Unknown consumer_data type");
2091 * Clean up a metadata stream and free its memory.
2093 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2094 struct lttng_ht
*ht
)
2096 struct lttng_consumer_channel
*channel
= NULL
;
2097 bool free_channel
= false;
2101 * This call should NEVER receive regular stream. It must always be
2102 * metadata stream and this is crucial for data structure synchronization.
2104 assert(stream
->metadata_flag
);
2106 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2108 pthread_mutex_lock(&consumer_data
.lock
);
2110 * Note that this assumes that a stream's channel is never changed and
2111 * that the stream's lock doesn't need to be taken to sample its
2114 channel
= stream
->chan
;
2115 pthread_mutex_lock(&channel
->lock
);
2116 pthread_mutex_lock(&stream
->lock
);
2117 if (channel
->metadata_cache
) {
2118 /* Only applicable to userspace consumers. */
2119 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
2122 /* Remove any reference to that stream. */
2123 consumer_stream_delete(stream
, ht
);
2125 /* Close down everything including the relayd if one. */
2126 consumer_stream_close(stream
);
2127 /* Destroy tracer buffers of the stream. */
2128 consumer_stream_destroy_buffers(stream
);
2130 /* Atomically decrement channel refcount since other threads can use it. */
2131 if (!uatomic_sub_return(&channel
->refcount
, 1)
2132 && !uatomic_read(&channel
->nb_init_stream_left
)) {
2133 /* Go for channel deletion! */
2134 free_channel
= true;
2136 stream
->chan
= NULL
;
2139 * Nullify the stream reference so it is not used after deletion. The
2140 * channel lock MUST be acquired before being able to check for a NULL
2143 channel
->metadata_stream
= NULL
;
2145 if (channel
->metadata_cache
) {
2146 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
2148 pthread_mutex_unlock(&stream
->lock
);
2149 pthread_mutex_unlock(&channel
->lock
);
2150 pthread_mutex_unlock(&consumer_data
.lock
);
2153 consumer_del_channel(channel
);
2156 lttng_trace_chunk_put(stream
->trace_chunk
);
2157 stream
->trace_chunk
= NULL
;
2158 consumer_stream_free(stream
);
2162 * Action done with the metadata stream when adding it to the consumer internal
2163 * data structures to handle it.
2165 void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2167 struct lttng_ht
*ht
= metadata_ht
;
2168 struct lttng_ht_iter iter
;
2169 struct lttng_ht_node_u64
*node
;
2174 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2176 pthread_mutex_lock(&consumer_data
.lock
);
2177 pthread_mutex_lock(&stream
->chan
->lock
);
2178 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2179 pthread_mutex_lock(&stream
->lock
);
2182 * From here, refcounts are updated so be _careful_ when returning an error
2189 * Lookup the stream just to make sure it does not exist in our internal
2190 * state. This should NEVER happen.
2192 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2193 node
= lttng_ht_iter_get_node_u64(&iter
);
2197 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2198 * in terms of destroying the associated channel, because the action that
2199 * causes the count to become 0 also causes a stream to be added. The
2200 * channel deletion will thus be triggered by the following removal of this
2203 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2204 /* Increment refcount before decrementing nb_init_stream_left */
2206 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2209 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2211 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
2212 &stream
->node_channel_id
);
2215 * Add stream to the stream_list_ht of the consumer data. No need to steal
2216 * the key since the HT does not use it and we allow to add redundant keys
2219 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2223 pthread_mutex_unlock(&stream
->lock
);
2224 pthread_mutex_unlock(&stream
->chan
->lock
);
2225 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2226 pthread_mutex_unlock(&consumer_data
.lock
);
2230 * Delete data stream that are flagged for deletion (endpoint_status).
2232 static void validate_endpoint_status_data_stream(void)
2234 struct lttng_ht_iter iter
;
2235 struct lttng_consumer_stream
*stream
;
2237 DBG("Consumer delete flagged data stream");
2240 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2241 /* Validate delete flag of the stream */
2242 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2245 /* Delete it right now */
2246 consumer_del_stream(stream
, data_ht
);
2252 * Delete metadata stream that are flagged for deletion (endpoint_status).
2254 static void validate_endpoint_status_metadata_stream(
2255 struct lttng_poll_event
*pollset
)
2257 struct lttng_ht_iter iter
;
2258 struct lttng_consumer_stream
*stream
;
2260 DBG("Consumer delete flagged metadata stream");
2265 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2266 /* Validate delete flag of the stream */
2267 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2271 * Remove from pollset so the metadata thread can continue without
2272 * blocking on a deleted stream.
2274 lttng_poll_del(pollset
, stream
->wait_fd
);
2276 /* Delete it right now */
2277 consumer_del_metadata_stream(stream
, metadata_ht
);
2283 * Thread polls on metadata file descriptor and write them on disk or on the
2286 void *consumer_thread_metadata_poll(void *data
)
2288 int ret
, i
, pollfd
, err
= -1;
2289 uint32_t revents
, nb_fd
;
2290 struct lttng_consumer_stream
*stream
= NULL
;
2291 struct lttng_ht_iter iter
;
2292 struct lttng_ht_node_u64
*node
;
2293 struct lttng_poll_event events
;
2294 struct lttng_consumer_local_data
*ctx
= data
;
2297 rcu_register_thread();
2299 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2301 if (testpoint(consumerd_thread_metadata
)) {
2302 goto error_testpoint
;
2305 health_code_update();
2307 DBG("Thread metadata poll started");
2309 /* Size is set to 1 for the consumer_metadata pipe */
2310 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2312 ERR("Poll set creation failed");
2316 ret
= lttng_poll_add(&events
,
2317 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2323 DBG("Metadata main loop started");
2327 health_code_update();
2328 health_poll_entry();
2329 DBG("Metadata poll wait");
2330 ret
= lttng_poll_wait(&events
, -1);
2331 DBG("Metadata poll return from wait with %d fd(s)",
2332 LTTNG_POLL_GETNB(&events
));
2334 DBG("Metadata event caught in thread");
2336 if (errno
== EINTR
) {
2337 ERR("Poll EINTR caught");
2340 if (LTTNG_POLL_GETNB(&events
) == 0) {
2341 err
= 0; /* All is OK */
2348 /* From here, the event is a metadata wait fd */
2349 for (i
= 0; i
< nb_fd
; i
++) {
2350 health_code_update();
2352 revents
= LTTNG_POLL_GETEV(&events
, i
);
2353 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2355 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2356 if (revents
& LPOLLIN
) {
2359 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2360 &stream
, sizeof(stream
));
2361 if (pipe_len
< sizeof(stream
)) {
2363 PERROR("read metadata stream");
2366 * Remove the pipe from the poll set and continue the loop
2367 * since their might be data to consume.
2369 lttng_poll_del(&events
,
2370 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2371 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2375 /* A NULL stream means that the state has changed. */
2376 if (stream
== NULL
) {
2377 /* Check for deleted streams. */
2378 validate_endpoint_status_metadata_stream(&events
);
2382 DBG("Adding metadata stream %d to poll set",
2385 /* Add metadata stream to the global poll events list */
2386 lttng_poll_add(&events
, stream
->wait_fd
,
2387 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2388 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2389 DBG("Metadata thread pipe hung up");
2391 * Remove the pipe from the poll set and continue the loop
2392 * since their might be data to consume.
2394 lttng_poll_del(&events
,
2395 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2396 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2399 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2403 /* Handle other stream */
2409 uint64_t tmp_id
= (uint64_t) pollfd
;
2411 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2413 node
= lttng_ht_iter_get_node_u64(&iter
);
2416 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2419 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2420 /* Get the data out of the metadata file descriptor */
2421 DBG("Metadata available on fd %d", pollfd
);
2422 assert(stream
->wait_fd
== pollfd
);
2425 health_code_update();
2427 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2429 * We don't check the return value here since if we get
2430 * a negative len, it means an error occurred thus we
2431 * simply remove it from the poll set and free the
2436 /* It's ok to have an unavailable sub-buffer */
2437 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2438 /* Clean up stream from consumer and free it. */
2439 lttng_poll_del(&events
, stream
->wait_fd
);
2440 consumer_del_metadata_stream(stream
, metadata_ht
);
2442 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2443 DBG("Metadata fd %d is hup|err.", pollfd
);
2444 if (!stream
->hangup_flush_done
2445 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2446 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2447 DBG("Attempting to flush and consume the UST buffers");
2448 lttng_ustconsumer_on_stream_hangup(stream
);
2450 /* We just flushed the stream now read it. */
2452 health_code_update();
2454 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2456 * We don't check the return value here since if we get
2457 * a negative len, it means an error occurred thus we
2458 * simply remove it from the poll set and free the
2464 lttng_poll_del(&events
, stream
->wait_fd
);
2466 * This call update the channel states, closes file descriptors
2467 * and securely free the stream.
2469 consumer_del_metadata_stream(stream
, metadata_ht
);
2471 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2475 /* Release RCU lock for the stream looked up */
2483 DBG("Metadata poll thread exiting");
2485 lttng_poll_clean(&events
);
2490 ERR("Health error occurred in %s", __func__
);
2492 health_unregister(health_consumerd
);
2493 rcu_unregister_thread();
2498 * This thread polls the fds in the set to consume the data and write
2499 * it to tracefile if necessary.
2501 void *consumer_thread_data_poll(void *data
)
2503 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2504 struct pollfd
*pollfd
= NULL
;
2505 /* local view of the streams */
2506 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2507 /* local view of consumer_data.fds_count */
2509 /* 2 for the consumer_data_pipe and wake up pipe */
2510 const int nb_pipes_fd
= 2;
2511 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2512 int nb_inactive_fd
= 0;
2513 struct lttng_consumer_local_data
*ctx
= data
;
2516 rcu_register_thread();
2518 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2520 if (testpoint(consumerd_thread_data
)) {
2521 goto error_testpoint
;
2524 health_code_update();
2526 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2527 if (local_stream
== NULL
) {
2528 PERROR("local_stream malloc");
2533 health_code_update();
2539 * the fds set has been updated, we need to update our
2540 * local array as well
2542 pthread_mutex_lock(&consumer_data
.lock
);
2543 if (consumer_data
.need_update
) {
2548 local_stream
= NULL
;
2550 /* Allocate for all fds */
2551 pollfd
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) * sizeof(struct pollfd
));
2552 if (pollfd
== NULL
) {
2553 PERROR("pollfd malloc");
2554 pthread_mutex_unlock(&consumer_data
.lock
);
2558 local_stream
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) *
2559 sizeof(struct lttng_consumer_stream
*));
2560 if (local_stream
== NULL
) {
2561 PERROR("local_stream malloc");
2562 pthread_mutex_unlock(&consumer_data
.lock
);
2565 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2566 data_ht
, &nb_inactive_fd
);
2568 ERR("Error in allocating pollfd or local_outfds");
2569 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2570 pthread_mutex_unlock(&consumer_data
.lock
);
2574 consumer_data
.need_update
= 0;
2576 pthread_mutex_unlock(&consumer_data
.lock
);
2578 /* No FDs and consumer_quit, consumer_cleanup the thread */
2579 if (nb_fd
== 0 && nb_inactive_fd
== 0 &&
2580 CMM_LOAD_SHARED(consumer_quit
) == 1) {
2581 err
= 0; /* All is OK */
2584 /* poll on the array of fds */
2586 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2587 if (testpoint(consumerd_thread_data_poll
)) {
2590 health_poll_entry();
2591 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2593 DBG("poll num_rdy : %d", num_rdy
);
2594 if (num_rdy
== -1) {
2596 * Restart interrupted system call.
2598 if (errno
== EINTR
) {
2601 PERROR("Poll error");
2602 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2604 } else if (num_rdy
== 0) {
2605 DBG("Polling thread timed out");
2609 if (caa_unlikely(data_consumption_paused
)) {
2610 DBG("Data consumption paused, sleeping...");
2616 * If the consumer_data_pipe triggered poll go directly to the
2617 * beginning of the loop to update the array. We want to prioritize
2618 * array update over low-priority reads.
2620 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2621 ssize_t pipe_readlen
;
2623 DBG("consumer_data_pipe wake up");
2624 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2625 &new_stream
, sizeof(new_stream
));
2626 if (pipe_readlen
< sizeof(new_stream
)) {
2627 PERROR("Consumer data pipe");
2628 /* Continue so we can at least handle the current stream(s). */
2633 * If the stream is NULL, just ignore it. It's also possible that
2634 * the sessiond poll thread changed the consumer_quit state and is
2635 * waking us up to test it.
2637 if (new_stream
== NULL
) {
2638 validate_endpoint_status_data_stream();
2642 /* Continue to update the local streams and handle prio ones */
2646 /* Handle wakeup pipe. */
2647 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2649 ssize_t pipe_readlen
;
2651 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2653 if (pipe_readlen
< 0) {
2654 PERROR("Consumer data wakeup pipe");
2656 /* We've been awakened to handle stream(s). */
2657 ctx
->has_wakeup
= 0;
2660 /* Take care of high priority channels first. */
2661 for (i
= 0; i
< nb_fd
; i
++) {
2662 health_code_update();
2664 if (local_stream
[i
] == NULL
) {
2667 if (pollfd
[i
].revents
& POLLPRI
) {
2668 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2670 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2671 /* it's ok to have an unavailable sub-buffer */
2672 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2673 /* Clean the stream and free it. */
2674 consumer_del_stream(local_stream
[i
], data_ht
);
2675 local_stream
[i
] = NULL
;
2676 } else if (len
> 0) {
2677 local_stream
[i
]->data_read
= 1;
2683 * If we read high prio channel in this loop, try again
2684 * for more high prio data.
2690 /* Take care of low priority channels. */
2691 for (i
= 0; i
< nb_fd
; i
++) {
2692 health_code_update();
2694 if (local_stream
[i
] == NULL
) {
2697 if ((pollfd
[i
].revents
& POLLIN
) ||
2698 local_stream
[i
]->hangup_flush_done
||
2699 local_stream
[i
]->has_data
) {
2700 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2701 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2702 /* it's ok to have an unavailable sub-buffer */
2703 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2704 /* Clean the stream and free it. */
2705 consumer_del_stream(local_stream
[i
], data_ht
);
2706 local_stream
[i
] = NULL
;
2707 } else if (len
> 0) {
2708 local_stream
[i
]->data_read
= 1;
2713 /* Handle hangup and errors */
2714 for (i
= 0; i
< nb_fd
; i
++) {
2715 health_code_update();
2717 if (local_stream
[i
] == NULL
) {
2720 if (!local_stream
[i
]->hangup_flush_done
2721 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2722 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2723 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2724 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2726 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2727 /* Attempt read again, for the data we just flushed. */
2728 local_stream
[i
]->data_read
= 1;
2731 * If the poll flag is HUP/ERR/NVAL and we have
2732 * read no data in this pass, we can remove the
2733 * stream from its hash table.
2735 if ((pollfd
[i
].revents
& POLLHUP
)) {
2736 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2737 if (!local_stream
[i
]->data_read
) {
2738 consumer_del_stream(local_stream
[i
], data_ht
);
2739 local_stream
[i
] = NULL
;
2742 } else if (pollfd
[i
].revents
& POLLERR
) {
2743 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2744 if (!local_stream
[i
]->data_read
) {
2745 consumer_del_stream(local_stream
[i
], data_ht
);
2746 local_stream
[i
] = NULL
;
2749 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2750 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2751 if (!local_stream
[i
]->data_read
) {
2752 consumer_del_stream(local_stream
[i
], data_ht
);
2753 local_stream
[i
] = NULL
;
2757 if (local_stream
[i
] != NULL
) {
2758 local_stream
[i
]->data_read
= 0;
2765 DBG("polling thread exiting");
2770 * Close the write side of the pipe so epoll_wait() in
2771 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2772 * read side of the pipe. If we close them both, epoll_wait strangely does
2773 * not return and could create a endless wait period if the pipe is the
2774 * only tracked fd in the poll set. The thread will take care of closing
2777 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2782 ERR("Health error occurred in %s", __func__
);
2784 health_unregister(health_consumerd
);
2786 rcu_unregister_thread();
2791 * Close wake-up end of each stream belonging to the channel. This will
2792 * allow the poll() on the stream read-side to detect when the
2793 * write-side (application) finally closes them.
2796 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2798 struct lttng_ht
*ht
;
2799 struct lttng_consumer_stream
*stream
;
2800 struct lttng_ht_iter iter
;
2802 ht
= consumer_data
.stream_per_chan_id_ht
;
2805 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2806 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2807 ht
->match_fct
, &channel
->key
,
2808 &iter
.iter
, stream
, node_channel_id
.node
) {
2810 * Protect against teardown with mutex.
2812 pthread_mutex_lock(&stream
->lock
);
2813 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2816 switch (consumer_data
.type
) {
2817 case LTTNG_CONSUMER_KERNEL
:
2819 case LTTNG_CONSUMER32_UST
:
2820 case LTTNG_CONSUMER64_UST
:
2821 if (stream
->metadata_flag
) {
2822 /* Safe and protected by the stream lock. */
2823 lttng_ustconsumer_close_metadata(stream
->chan
);
2826 * Note: a mutex is taken internally within
2827 * liblttng-ust-ctl to protect timer wakeup_fd
2828 * use from concurrent close.
2830 lttng_ustconsumer_close_stream_wakeup(stream
);
2834 ERR("Unknown consumer_data type");
2838 pthread_mutex_unlock(&stream
->lock
);
2843 static void destroy_channel_ht(struct lttng_ht
*ht
)
2845 struct lttng_ht_iter iter
;
2846 struct lttng_consumer_channel
*channel
;
2854 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2855 ret
= lttng_ht_del(ht
, &iter
);
2860 lttng_ht_destroy(ht
);
2864 * This thread polls the channel fds to detect when they are being
2865 * closed. It closes all related streams if the channel is detected as
2866 * closed. It is currently only used as a shim layer for UST because the
2867 * consumerd needs to keep the per-stream wakeup end of pipes open for
2870 void *consumer_thread_channel_poll(void *data
)
2872 int ret
, i
, pollfd
, err
= -1;
2873 uint32_t revents
, nb_fd
;
2874 struct lttng_consumer_channel
*chan
= NULL
;
2875 struct lttng_ht_iter iter
;
2876 struct lttng_ht_node_u64
*node
;
2877 struct lttng_poll_event events
;
2878 struct lttng_consumer_local_data
*ctx
= data
;
2879 struct lttng_ht
*channel_ht
;
2881 rcu_register_thread();
2883 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2885 if (testpoint(consumerd_thread_channel
)) {
2886 goto error_testpoint
;
2889 health_code_update();
2891 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2893 /* ENOMEM at this point. Better to bail out. */
2897 DBG("Thread channel poll started");
2899 /* Size is set to 1 for the consumer_channel pipe */
2900 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2902 ERR("Poll set creation failed");
2906 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2912 DBG("Channel main loop started");
2916 health_code_update();
2917 DBG("Channel poll wait");
2918 health_poll_entry();
2919 ret
= lttng_poll_wait(&events
, -1);
2920 DBG("Channel poll return from wait with %d fd(s)",
2921 LTTNG_POLL_GETNB(&events
));
2923 DBG("Channel event caught in thread");
2925 if (errno
== EINTR
) {
2926 ERR("Poll EINTR caught");
2929 if (LTTNG_POLL_GETNB(&events
) == 0) {
2930 err
= 0; /* All is OK */
2937 /* From here, the event is a channel wait fd */
2938 for (i
= 0; i
< nb_fd
; i
++) {
2939 health_code_update();
2941 revents
= LTTNG_POLL_GETEV(&events
, i
);
2942 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2944 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2945 if (revents
& LPOLLIN
) {
2946 enum consumer_channel_action action
;
2949 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2952 ERR("Error reading channel pipe");
2954 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2959 case CONSUMER_CHANNEL_ADD
:
2960 DBG("Adding channel %d to poll set",
2963 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2966 lttng_ht_add_unique_u64(channel_ht
,
2967 &chan
->wait_fd_node
);
2969 /* Add channel to the global poll events list */
2970 lttng_poll_add(&events
, chan
->wait_fd
,
2971 LPOLLERR
| LPOLLHUP
);
2973 case CONSUMER_CHANNEL_DEL
:
2976 * This command should never be called if the channel
2977 * has streams monitored by either the data or metadata
2978 * thread. The consumer only notify this thread with a
2979 * channel del. command if it receives a destroy
2980 * channel command from the session daemon that send it
2981 * if a command prior to the GET_CHANNEL failed.
2985 chan
= consumer_find_channel(key
);
2988 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2991 lttng_poll_del(&events
, chan
->wait_fd
);
2992 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2993 ret
= lttng_ht_del(channel_ht
, &iter
);
2996 switch (consumer_data
.type
) {
2997 case LTTNG_CONSUMER_KERNEL
:
2999 case LTTNG_CONSUMER32_UST
:
3000 case LTTNG_CONSUMER64_UST
:
3001 health_code_update();
3002 /* Destroy streams that might have been left in the stream list. */
3003 clean_channel_stream_list(chan
);
3006 ERR("Unknown consumer_data type");
3011 * Release our own refcount. Force channel deletion even if
3012 * streams were not initialized.
3014 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3015 consumer_del_channel(chan
);
3020 case CONSUMER_CHANNEL_QUIT
:
3022 * Remove the pipe from the poll set and continue the loop
3023 * since their might be data to consume.
3025 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3028 ERR("Unknown action");
3031 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3032 DBG("Channel thread pipe hung up");
3034 * Remove the pipe from the poll set and continue the loop
3035 * since their might be data to consume.
3037 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3040 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3044 /* Handle other stream */
3050 uint64_t tmp_id
= (uint64_t) pollfd
;
3052 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3054 node
= lttng_ht_iter_get_node_u64(&iter
);
3057 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
3060 /* Check for error event */
3061 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3062 DBG("Channel fd %d is hup|err.", pollfd
);
3064 lttng_poll_del(&events
, chan
->wait_fd
);
3065 ret
= lttng_ht_del(channel_ht
, &iter
);
3069 * This will close the wait fd for each stream associated to
3070 * this channel AND monitored by the data/metadata thread thus
3071 * will be clean by the right thread.
3073 consumer_close_channel_streams(chan
);
3075 /* Release our own refcount */
3076 if (!uatomic_sub_return(&chan
->refcount
, 1)
3077 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3078 consumer_del_channel(chan
);
3081 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3086 /* Release RCU lock for the channel looked up */
3094 lttng_poll_clean(&events
);
3096 destroy_channel_ht(channel_ht
);
3099 DBG("Channel poll thread exiting");
3102 ERR("Health error occurred in %s", __func__
);
3104 health_unregister(health_consumerd
);
3105 rcu_unregister_thread();
3109 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3110 struct pollfd
*sockpoll
, int client_socket
)
3117 ret
= lttng_consumer_poll_socket(sockpoll
);
3121 DBG("Metadata connection on client_socket");
3123 /* Blocking call, waiting for transmission */
3124 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3125 if (ctx
->consumer_metadata_socket
< 0) {
3126 WARN("On accept metadata");
3137 * This thread listens on the consumerd socket and receives the file
3138 * descriptors from the session daemon.
3140 void *consumer_thread_sessiond_poll(void *data
)
3142 int sock
= -1, client_socket
, ret
, err
= -1;
3144 * structure to poll for incoming data on communication socket avoids
3145 * making blocking sockets.
3147 struct pollfd consumer_sockpoll
[2];
3148 struct lttng_consumer_local_data
*ctx
= data
;
3150 rcu_register_thread();
3152 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3154 if (testpoint(consumerd_thread_sessiond
)) {
3155 goto error_testpoint
;
3158 health_code_update();
3160 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3161 unlink(ctx
->consumer_command_sock_path
);
3162 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3163 if (client_socket
< 0) {
3164 ERR("Cannot create command socket");
3168 ret
= lttcomm_listen_unix_sock(client_socket
);
3173 DBG("Sending ready command to lttng-sessiond");
3174 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3175 /* return < 0 on error, but == 0 is not fatal */
3177 ERR("Error sending ready command to lttng-sessiond");
3181 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3182 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3183 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3184 consumer_sockpoll
[1].fd
= client_socket
;
3185 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3187 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3195 DBG("Connection on client_socket");
3197 /* Blocking call, waiting for transmission */
3198 sock
= lttcomm_accept_unix_sock(client_socket
);
3205 * Setup metadata socket which is the second socket connection on the
3206 * command unix socket.
3208 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3217 /* This socket is not useful anymore. */
3218 ret
= close(client_socket
);
3220 PERROR("close client_socket");
3224 /* update the polling structure to poll on the established socket */
3225 consumer_sockpoll
[1].fd
= sock
;
3226 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3229 health_code_update();
3231 health_poll_entry();
3232 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3241 DBG("Incoming command on sock");
3242 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3245 * This could simply be a session daemon quitting. Don't output
3248 DBG("Communication interrupted on command socket");
3252 if (CMM_LOAD_SHARED(consumer_quit
)) {
3253 DBG("consumer_thread_receive_fds received quit from signal");
3254 err
= 0; /* All is OK */
3257 DBG("received command on sock");
3263 DBG("Consumer thread sessiond poll exiting");
3266 * Close metadata streams since the producer is the session daemon which
3269 * NOTE: for now, this only applies to the UST tracer.
3271 lttng_consumer_close_all_metadata();
3274 * when all fds have hung up, the polling thread
3277 CMM_STORE_SHARED(consumer_quit
, 1);
3280 * Notify the data poll thread to poll back again and test the
3281 * consumer_quit state that we just set so to quit gracefully.
3283 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3285 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3287 notify_health_quit_pipe(health_quit_pipe
);
3289 /* Cleaning up possibly open sockets. */
3293 PERROR("close sock sessiond poll");
3296 if (client_socket
>= 0) {
3297 ret
= close(client_socket
);
3299 PERROR("close client_socket sessiond poll");
3306 ERR("Health error occurred in %s", __func__
);
3308 health_unregister(health_consumerd
);
3310 rcu_unregister_thread();
3314 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3315 struct lttng_consumer_local_data
*ctx
,
3316 bool locked_by_caller
)
3318 ssize_t ret
, written_bytes
;
3320 struct stream_subbuffer subbuffer
= {};
3322 if (!locked_by_caller
) {
3323 stream
->read_subbuffer_ops
.lock(stream
);
3326 if (stream
->read_subbuffer_ops
.on_wake_up
) {
3327 ret
= stream
->read_subbuffer_ops
.on_wake_up(stream
);
3334 * If the stream was flagged to be ready for rotation before we extract
3335 * the next packet, rotate it now.
3337 if (stream
->rotate_ready
) {
3338 DBG("Rotate stream before consuming data");
3339 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3341 ERR("Stream rotation error before consuming data");
3346 ret
= stream
->read_subbuffer_ops
.get_next_subbuffer(stream
, &subbuffer
);
3348 if (ret
== -ENODATA
) {
3355 ret
= stream
->read_subbuffer_ops
.pre_consume_subbuffer(
3356 stream
, &subbuffer
);
3358 goto error_put_subbuf
;
3361 written_bytes
= stream
->read_subbuffer_ops
.consume_subbuffer(
3362 ctx
, stream
, &subbuffer
);
3364 * Should write subbuf_size amount of data when network streaming or
3365 * the full padded size when we are not streaming.
3367 if ((written_bytes
!= subbuffer
.info
.data
.subbuf_size
&&
3368 stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
3369 (written_bytes
!= subbuffer
.info
.data
.padded_subbuf_size
&&
3370 stream
->net_seq_idx
==
3371 (uint64_t) -1ULL)) {
3373 * Display the error but continue processing to try to
3374 * release the subbuffer. This is a DBG statement
3375 * since this can happen without being a critical
3378 DBG("Failed to write to tracefile (written_bytes: %zd != padded subbuffer size: %lu, subbuffer size: %lu)",
3379 written_bytes
, subbuffer
.info
.data
.subbuf_size
,
3380 subbuffer
.info
.data
.padded_subbuf_size
);
3383 ret
= stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3388 if (stream
->read_subbuffer_ops
.post_consume
) {
3389 ret
= stream
->read_subbuffer_ops
.post_consume(stream
, &subbuffer
, ctx
);
3396 * After extracting the packet, we check if the stream is now ready to
3397 * be rotated and perform the action immediately.
3399 * Don't overwrite `ret` as callers expect the number of bytes
3400 * consumed to be returned on success.
3402 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
3403 if (rotation_ret
== 1) {
3404 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3405 if (rotation_ret
< 0) {
3407 ERR("Stream rotation error after consuming data");
3410 } else if (rotation_ret
< 0) {
3412 ERR("Failed to check if stream was ready to rotate after consuming data");
3416 if (stream
->read_subbuffer_ops
.on_sleep
) {
3417 stream
->read_subbuffer_ops
.on_sleep(stream
, ctx
);
3420 ret
= written_bytes
;
3422 if (!locked_by_caller
) {
3423 stream
->read_subbuffer_ops
.unlock(stream
);
3428 (void) stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3432 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3434 switch (consumer_data
.type
) {
3435 case LTTNG_CONSUMER_KERNEL
:
3436 return lttng_kconsumer_on_recv_stream(stream
);
3437 case LTTNG_CONSUMER32_UST
:
3438 case LTTNG_CONSUMER64_UST
:
3439 return lttng_ustconsumer_on_recv_stream(stream
);
3441 ERR("Unknown consumer_data type");
3448 * Allocate and set consumer data hash tables.
3450 int lttng_consumer_init(void)
3452 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3453 if (!consumer_data
.channel_ht
) {
3457 consumer_data
.channels_by_session_id_ht
=
3458 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3459 if (!consumer_data
.channels_by_session_id_ht
) {
3463 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3464 if (!consumer_data
.relayd_ht
) {
3468 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3469 if (!consumer_data
.stream_list_ht
) {
3473 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3474 if (!consumer_data
.stream_per_chan_id_ht
) {
3478 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3483 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3488 consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3489 if (!consumer_data
.chunk_registry
) {
3500 * Process the ADD_RELAYD command receive by a consumer.
3502 * This will create a relayd socket pair and add it to the relayd hash table.
3503 * The caller MUST acquire a RCU read side lock before calling it.
3505 void consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3506 struct lttng_consumer_local_data
*ctx
, int sock
,
3507 struct pollfd
*consumer_sockpoll
,
3508 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3509 uint64_t relayd_session_id
)
3511 int fd
= -1, ret
= -1, relayd_created
= 0;
3512 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3513 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3516 assert(relayd_sock
);
3518 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3520 /* Get relayd reference if exists. */
3521 relayd
= consumer_find_relayd(net_seq_idx
);
3522 if (relayd
== NULL
) {
3523 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3524 /* Not found. Allocate one. */
3525 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3526 if (relayd
== NULL
) {
3527 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3530 relayd
->sessiond_session_id
= sessiond_id
;
3535 * This code path MUST continue to the consumer send status message to
3536 * we can notify the session daemon and continue our work without
3537 * killing everything.
3541 * relayd key should never be found for control socket.
3543 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3546 /* First send a status message before receiving the fds. */
3547 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3549 /* Somehow, the session daemon is not responding anymore. */
3550 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3551 goto error_nosignal
;
3554 /* Poll on consumer socket. */
3555 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3557 /* Needing to exit in the middle of a command: error. */
3558 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3559 goto error_nosignal
;
3562 /* Get relayd socket from session daemon */
3563 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3564 if (ret
!= sizeof(fd
)) {
3565 fd
= -1; /* Just in case it gets set with an invalid value. */
3568 * Failing to receive FDs might indicate a major problem such as
3569 * reaching a fd limit during the receive where the kernel returns a
3570 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3571 * don't take any chances and stop everything.
3573 * XXX: Feature request #558 will fix that and avoid this possible
3574 * issue when reaching the fd limit.
3576 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3577 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3581 /* Copy socket information and received FD */
3582 switch (sock_type
) {
3583 case LTTNG_STREAM_CONTROL
:
3584 /* Copy received lttcomm socket */
3585 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3586 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3587 /* Handle create_sock error. */
3589 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3593 * Close the socket created internally by
3594 * lttcomm_create_sock, so we can replace it by the one
3595 * received from sessiond.
3597 if (close(relayd
->control_sock
.sock
.fd
)) {
3601 /* Assign new file descriptor */
3602 relayd
->control_sock
.sock
.fd
= fd
;
3603 /* Assign version values. */
3604 relayd
->control_sock
.major
= relayd_sock
->major
;
3605 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3607 relayd
->relayd_session_id
= relayd_session_id
;
3610 case LTTNG_STREAM_DATA
:
3611 /* Copy received lttcomm socket */
3612 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3613 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3614 /* Handle create_sock error. */
3616 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3620 * Close the socket created internally by
3621 * lttcomm_create_sock, so we can replace it by the one
3622 * received from sessiond.
3624 if (close(relayd
->data_sock
.sock
.fd
)) {
3628 /* Assign new file descriptor */
3629 relayd
->data_sock
.sock
.fd
= fd
;
3630 /* Assign version values. */
3631 relayd
->data_sock
.major
= relayd_sock
->major
;
3632 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3635 ERR("Unknown relayd socket type (%d)", sock_type
);
3636 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3640 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3641 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3642 relayd
->net_seq_idx
, fd
);
3644 * We gave the ownership of the fd to the relayd structure. Set the
3645 * fd to -1 so we don't call close() on it in the error path below.
3649 /* We successfully added the socket. Send status back. */
3650 ret
= consumer_send_status_msg(sock
, ret_code
);
3652 /* Somehow, the session daemon is not responding anymore. */
3653 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3654 goto error_nosignal
;
3658 * Add relayd socket pair to consumer data hashtable. If object already
3659 * exists or on error, the function gracefully returns.
3668 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3669 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3673 /* Close received socket if valid. */
3676 PERROR("close received socket");
3680 if (relayd_created
) {
3686 * Search for a relayd associated to the session id and return the reference.
3688 * A rcu read side lock MUST be acquire before calling this function and locked
3689 * until the relayd object is no longer necessary.
3691 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3693 struct lttng_ht_iter iter
;
3694 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3696 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3697 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3700 * Check by sessiond id which is unique here where the relayd session
3701 * id might not be when having multiple relayd.
3703 if (relayd
->sessiond_session_id
== id
) {
3704 /* Found the relayd. There can be only one per id. */
3716 * Check if for a given session id there is still data needed to be extract
3719 * Return 1 if data is pending or else 0 meaning ready to be read.
3721 int consumer_data_pending(uint64_t id
)
3724 struct lttng_ht_iter iter
;
3725 struct lttng_ht
*ht
;
3726 struct lttng_consumer_stream
*stream
;
3727 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3728 int (*data_pending
)(struct lttng_consumer_stream
*);
3730 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3733 pthread_mutex_lock(&consumer_data
.lock
);
3735 switch (consumer_data
.type
) {
3736 case LTTNG_CONSUMER_KERNEL
:
3737 data_pending
= lttng_kconsumer_data_pending
;
3739 case LTTNG_CONSUMER32_UST
:
3740 case LTTNG_CONSUMER64_UST
:
3741 data_pending
= lttng_ustconsumer_data_pending
;
3744 ERR("Unknown consumer data type");
3748 /* Ease our life a bit */
3749 ht
= consumer_data
.stream_list_ht
;
3751 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3752 ht
->hash_fct(&id
, lttng_ht_seed
),
3754 &iter
.iter
, stream
, node_session_id
.node
) {
3755 pthread_mutex_lock(&stream
->lock
);
3758 * A removed node from the hash table indicates that the stream has
3759 * been deleted thus having a guarantee that the buffers are closed
3760 * on the consumer side. However, data can still be transmitted
3761 * over the network so don't skip the relayd check.
3763 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3765 /* Check the stream if there is data in the buffers. */
3766 ret
= data_pending(stream
);
3768 pthread_mutex_unlock(&stream
->lock
);
3773 pthread_mutex_unlock(&stream
->lock
);
3776 relayd
= find_relayd_by_session_id(id
);
3778 unsigned int is_data_inflight
= 0;
3780 /* Send init command for data pending. */
3781 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3782 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3783 relayd
->relayd_session_id
);
3785 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3786 /* Communication error thus the relayd so no data pending. */
3787 goto data_not_pending
;
3790 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3791 ht
->hash_fct(&id
, lttng_ht_seed
),
3793 &iter
.iter
, stream
, node_session_id
.node
) {
3794 if (stream
->metadata_flag
) {
3795 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3796 stream
->relayd_stream_id
);
3798 ret
= relayd_data_pending(&relayd
->control_sock
,
3799 stream
->relayd_stream_id
,
3800 stream
->next_net_seq_num
- 1);
3804 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3806 } else if (ret
< 0) {
3807 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3808 lttng_consumer_cleanup_relayd(relayd
);
3809 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3810 goto data_not_pending
;
3814 /* Send end command for data pending. */
3815 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3816 relayd
->relayd_session_id
, &is_data_inflight
);
3817 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3819 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3820 lttng_consumer_cleanup_relayd(relayd
);
3821 goto data_not_pending
;
3823 if (is_data_inflight
) {
3829 * Finding _no_ node in the hash table and no inflight data means that the
3830 * stream(s) have been removed thus data is guaranteed to be available for
3831 * analysis from the trace files.
3835 /* Data is available to be read by a viewer. */
3836 pthread_mutex_unlock(&consumer_data
.lock
);
3841 /* Data is still being extracted from buffers. */
3842 pthread_mutex_unlock(&consumer_data
.lock
);
3848 * Send a ret code status message to the sessiond daemon.
3850 * Return the sendmsg() return value.
3852 int consumer_send_status_msg(int sock
, int ret_code
)
3854 struct lttcomm_consumer_status_msg msg
;
3856 memset(&msg
, 0, sizeof(msg
));
3857 msg
.ret_code
= ret_code
;
3859 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3863 * Send a channel status message to the sessiond daemon.
3865 * Return the sendmsg() return value.
3867 int consumer_send_status_channel(int sock
,
3868 struct lttng_consumer_channel
*channel
)
3870 struct lttcomm_consumer_status_channel msg
;
3874 memset(&msg
, 0, sizeof(msg
));
3876 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3878 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3879 msg
.key
= channel
->key
;
3880 msg
.stream_count
= channel
->streams
.count
;
3883 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3886 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3887 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3888 uint64_t max_sb_size
)
3890 unsigned long start_pos
;
3892 if (!nb_packets_per_stream
) {
3893 return consumed_pos
; /* Grab everything */
3895 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
3896 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3897 if ((long) (start_pos
- consumed_pos
) < 0) {
3898 return consumed_pos
; /* Grab everything */
3904 int consumer_flush_buffer(struct lttng_consumer_stream
*stream
, int producer_active
)
3908 switch (consumer_data
.type
) {
3909 case LTTNG_CONSUMER_KERNEL
:
3910 if (producer_active
) {
3911 ret
= kernctl_buffer_flush(stream
->wait_fd
);
3913 ERR("Failed to flush kernel stream");
3917 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
3919 ERR("Failed to flush kernel stream");
3924 case LTTNG_CONSUMER32_UST
:
3925 case LTTNG_CONSUMER64_UST
:
3926 lttng_ustconsumer_flush_buffer(stream
, producer_active
);
3929 ERR("Unknown consumer_data type");
3938 * Sample the rotate position for all the streams of a channel. If a stream
3939 * is already at the rotate position (produced == consumed), we flag it as
3940 * ready for rotation. The rotation of ready streams occurs after we have
3941 * replied to the session daemon that we have finished sampling the positions.
3942 * Must be called with RCU read-side lock held to ensure existence of channel.
3944 * Returns 0 on success, < 0 on error
3946 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
3947 uint64_t key
, uint64_t relayd_id
, uint32_t metadata
,
3948 struct lttng_consumer_local_data
*ctx
)
3951 struct lttng_consumer_stream
*stream
;
3952 struct lttng_ht_iter iter
;
3953 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
3954 struct lttng_dynamic_array stream_rotation_positions
;
3955 uint64_t next_chunk_id
, stream_count
= 0;
3956 enum lttng_trace_chunk_status chunk_status
;
3957 const bool is_local_trace
= relayd_id
== -1ULL;
3958 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3959 bool rotating_to_new_chunk
= true;
3961 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
3963 lttng_dynamic_array_init(&stream_rotation_positions
,
3964 sizeof(struct relayd_stream_rotation_position
), NULL
);
3968 pthread_mutex_lock(&channel
->lock
);
3969 assert(channel
->trace_chunk
);
3970 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
,
3972 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
3974 goto end_unlock_channel
;
3977 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3978 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
3979 ht
->match_fct
, &channel
->key
, &iter
.iter
,
3980 stream
, node_channel_id
.node
) {
3981 unsigned long produced_pos
= 0, consumed_pos
= 0;
3983 health_code_update();
3986 * Lock stream because we are about to change its state.
3988 pthread_mutex_lock(&stream
->lock
);
3990 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
3991 rotating_to_new_chunk
= false;
3995 * Do not flush an empty packet when rotating from a NULL trace
3996 * chunk. The stream has no means to output data, and the prior
3997 * rotation which rotated to NULL performed that side-effect already.
3999 if (stream
->trace_chunk
) {
4001 * For metadata stream, do an active flush, which does not
4002 * produce empty packets. For data streams, empty-flush;
4003 * ensures we have at least one packet in each stream per trace
4004 * chunk, even if no data was produced.
4006 ret
= consumer_flush_buffer(stream
, stream
->metadata_flag
? 1 : 0);
4008 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4010 goto end_unlock_stream
;
4014 ret
= lttng_consumer_take_snapshot(stream
);
4015 if (ret
< 0 && ret
!= -ENODATA
&& ret
!= -EAGAIN
) {
4016 ERR("Failed to sample snapshot position during channel rotation");
4017 goto end_unlock_stream
;
4020 ret
= lttng_consumer_get_produced_snapshot(stream
,
4023 ERR("Failed to sample produced position during channel rotation");
4024 goto end_unlock_stream
;
4027 ret
= lttng_consumer_get_consumed_snapshot(stream
,
4030 ERR("Failed to sample consumed position during channel rotation");
4031 goto end_unlock_stream
;
4035 * Align produced position on the start-of-packet boundary of the first
4036 * packet going into the next trace chunk.
4038 produced_pos
= ALIGN_FLOOR(produced_pos
, stream
->max_sb_size
);
4039 if (consumed_pos
== produced_pos
) {
4040 stream
->rotate_ready
= true;
4043 * The rotation position is based on the packet_seq_num of the
4044 * packet following the last packet that was consumed for this
4045 * stream, incremented by the offset between produced and
4046 * consumed positions. This rotation position is a lower bound
4047 * (inclusive) at which the next trace chunk starts. Since it
4048 * is a lower bound, it is OK if the packet_seq_num does not
4049 * correspond exactly to the same packet identified by the
4050 * consumed_pos, which can happen in overwrite mode.
4052 if (stream
->sequence_number_unavailable
) {
4054 * Rotation should never be performed on a session which
4055 * interacts with a pre-2.8 lttng-modules, which does
4056 * not implement packet sequence number.
4058 ERR("Failure to rotate stream %" PRIu64
": sequence number unavailable",
4061 goto end_unlock_stream
;
4063 stream
->rotate_position
= stream
->last_sequence_number
+ 1 +
4064 ((produced_pos
- consumed_pos
) / stream
->max_sb_size
);
4066 if (!is_local_trace
) {
4068 * The relay daemon control protocol expects a rotation
4069 * position as "the sequence number of the first packet
4070 * _after_ the current trace chunk".
4072 const struct relayd_stream_rotation_position position
= {
4073 .stream_id
= stream
->relayd_stream_id
,
4074 .rotate_at_seq_num
= stream
->rotate_position
,
4077 ret
= lttng_dynamic_array_add_element(
4078 &stream_rotation_positions
,
4081 ERR("Failed to allocate stream rotation position");
4082 goto end_unlock_stream
;
4086 pthread_mutex_unlock(&stream
->lock
);
4089 pthread_mutex_unlock(&channel
->lock
);
4091 if (is_local_trace
) {
4096 relayd
= consumer_find_relayd(relayd_id
);
4098 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4103 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4104 ret
= relayd_rotate_streams(&relayd
->control_sock
, stream_count
,
4105 rotating_to_new_chunk
? &next_chunk_id
: NULL
,
4106 (const struct relayd_stream_rotation_position
*)
4107 stream_rotation_positions
.buffer
.data
);
4108 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4110 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4111 relayd
->net_seq_idx
);
4112 lttng_consumer_cleanup_relayd(relayd
);
4120 pthread_mutex_unlock(&stream
->lock
);
4122 pthread_mutex_unlock(&channel
->lock
);
4125 lttng_dynamic_array_reset(&stream_rotation_positions
);
4130 * Check if a stream is ready to be rotated after extracting it.
4132 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4133 * error. Stream lock must be held.
4135 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4137 if (stream
->rotate_ready
) {
4142 * If packet seq num is unavailable, it means we are interacting
4143 * with a pre-2.8 lttng-modules which does not implement the
4144 * sequence number. Rotation should never be used by sessiond in this
4147 if (stream
->sequence_number_unavailable
) {
4148 ERR("Internal error: rotation used on stream %" PRIu64
4149 " with unavailable sequence number",
4154 if (stream
->rotate_position
== -1ULL ||
4155 stream
->last_sequence_number
== -1ULL) {
4160 * Rotate position not reached yet. The stream rotate position is
4161 * the position of the next packet belonging to the next trace chunk,
4162 * but consumerd considers rotation ready when reaching the last
4163 * packet of the current chunk, hence the "rotate_position - 1".
4165 if (stream
->last_sequence_number
>= stream
->rotate_position
- 1) {
4173 * Reset the state for a stream after a rotation occurred.
4175 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4177 stream
->rotate_position
= -1ULL;
4178 stream
->rotate_ready
= false;
4182 * Perform the rotation a local stream file.
4185 int rotate_local_stream(struct lttng_consumer_local_data
*ctx
,
4186 struct lttng_consumer_stream
*stream
)
4190 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4193 stream
->tracefile_size_current
= 0;
4194 stream
->tracefile_count_current
= 0;
4196 if (stream
->out_fd
>= 0) {
4197 ret
= close(stream
->out_fd
);
4199 PERROR("Failed to close stream out_fd of channel \"%s\"",
4200 stream
->chan
->name
);
4202 stream
->out_fd
= -1;
4205 if (stream
->index_file
) {
4206 lttng_index_file_put(stream
->index_file
);
4207 stream
->index_file
= NULL
;
4210 if (!stream
->trace_chunk
) {
4214 ret
= consumer_stream_create_output_files(stream
, true);
4220 * Performs the stream rotation for the rotate session feature if needed.
4221 * It must be called with the channel and stream locks held.
4223 * Return 0 on success, a negative number of error.
4225 int lttng_consumer_rotate_stream(struct lttng_consumer_local_data
*ctx
,
4226 struct lttng_consumer_stream
*stream
)
4230 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4233 * Update the stream's 'current' chunk to the session's (channel)
4234 * now-current chunk.
4236 lttng_trace_chunk_put(stream
->trace_chunk
);
4237 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4239 * A channel can be rotated and not have a "next" chunk
4240 * to transition to. In that case, the channel's "current chunk"
4241 * has not been closed yet, but it has not been updated to
4242 * a "next" trace chunk either. Hence, the stream, like its
4243 * parent channel, becomes part of no chunk and can't output
4244 * anything until a new trace chunk is created.
4246 stream
->trace_chunk
= NULL
;
4247 } else if (stream
->chan
->trace_chunk
&&
4248 !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4249 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4254 * Update the stream's trace chunk to its parent channel's
4255 * current trace chunk.
4257 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4260 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4261 ret
= rotate_local_stream(ctx
, stream
);
4263 ERR("Failed to rotate stream, ret = %i", ret
);
4268 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4270 * If the stream has transitioned to a new trace
4271 * chunk, the metadata should be re-dumped to the
4274 * However, it is possible for a stream to transition to
4275 * a "no-chunk" state. This can happen if a rotation
4276 * occurs on an inactive session. In such cases, the metadata
4277 * regeneration will happen when the next trace chunk is
4280 ret
= consumer_metadata_stream_dump(stream
);
4285 lttng_consumer_reset_stream_rotate_state(stream
);
4294 * Rotate all the ready streams now.
4296 * This is especially important for low throughput streams that have already
4297 * been consumed, we cannot wait for their next packet to perform the
4299 * Need to be called with RCU read-side lock held to ensure existence of
4302 * Returns 0 on success, < 0 on error
4304 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
,
4305 uint64_t key
, struct lttng_consumer_local_data
*ctx
)
4308 struct lttng_consumer_stream
*stream
;
4309 struct lttng_ht_iter iter
;
4310 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
4314 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4316 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4317 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4318 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4319 stream
, node_channel_id
.node
) {
4320 health_code_update();
4322 pthread_mutex_lock(&stream
->chan
->lock
);
4323 pthread_mutex_lock(&stream
->lock
);
4325 if (!stream
->rotate_ready
) {
4326 pthread_mutex_unlock(&stream
->lock
);
4327 pthread_mutex_unlock(&stream
->chan
->lock
);
4330 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4332 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
4333 pthread_mutex_unlock(&stream
->lock
);
4334 pthread_mutex_unlock(&stream
->chan
->lock
);
4347 enum lttcomm_return_code
lttng_consumer_init_command(
4348 struct lttng_consumer_local_data
*ctx
,
4349 const lttng_uuid sessiond_uuid
)
4351 enum lttcomm_return_code ret
;
4352 char uuid_str
[UUID_STR_LEN
];
4354 if (ctx
->sessiond_uuid
.is_set
) {
4355 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4359 ctx
->sessiond_uuid
.is_set
= true;
4360 memcpy(ctx
->sessiond_uuid
.value
, sessiond_uuid
, sizeof(lttng_uuid
));
4361 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4362 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4363 DBG("Received session daemon UUID: %s", uuid_str
);
4368 enum lttcomm_return_code
lttng_consumer_create_trace_chunk(
4369 const uint64_t *relayd_id
, uint64_t session_id
,
4371 time_t chunk_creation_timestamp
,
4372 const char *chunk_override_name
,
4373 const struct lttng_credentials
*credentials
,
4374 struct lttng_directory_handle
*chunk_directory_handle
)
4377 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4378 struct lttng_trace_chunk
*created_chunk
= NULL
, *published_chunk
= NULL
;
4379 enum lttng_trace_chunk_status chunk_status
;
4380 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4381 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4382 const char *relayd_id_str
= "(none)";
4383 const char *creation_timestamp_str
;
4384 struct lttng_ht_iter iter
;
4385 struct lttng_consumer_channel
*channel
;
4388 /* Only used for logging purposes. */
4389 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4390 "%" PRIu64
, *relayd_id
);
4391 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4392 relayd_id_str
= relayd_id_buffer
;
4394 relayd_id_str
= "(formatting error)";
4398 /* Local protocol error. */
4399 assert(chunk_creation_timestamp
);
4400 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4401 creation_timestamp_buffer
,
4402 sizeof(creation_timestamp_buffer
));
4403 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
4404 "(formatting error)";
4406 DBG("Consumer create trace chunk command: relay_id = %s"
4407 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4408 ", chunk_override_name = %s"
4409 ", chunk_creation_timestamp = %s",
4410 relayd_id_str
, session_id
, chunk_id
,
4411 chunk_override_name
? : "(none)",
4412 creation_timestamp_str
);
4415 * The trace chunk registry, as used by the consumer daemon, implicitly
4416 * owns the trace chunks. This is only needed in the consumer since
4417 * the consumer has no notion of a session beyond session IDs being
4418 * used to identify other objects.
4420 * The lttng_trace_chunk_registry_publish() call below provides a
4421 * reference which is not released; it implicitly becomes the session
4422 * daemon's reference to the chunk in the consumer daemon.
4424 * The lifetime of trace chunks in the consumer daemon is managed by
4425 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4426 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4428 created_chunk
= lttng_trace_chunk_create(chunk_id
,
4429 chunk_creation_timestamp
);
4430 if (!created_chunk
) {
4431 ERR("Failed to create trace chunk");
4432 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4436 if (chunk_override_name
) {
4437 chunk_status
= lttng_trace_chunk_override_name(created_chunk
,
4438 chunk_override_name
);
4439 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4440 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4445 if (chunk_directory_handle
) {
4446 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
,
4448 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4449 ERR("Failed to set trace chunk credentials");
4450 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4454 * The consumer daemon has no ownership of the chunk output
4457 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
,
4458 chunk_directory_handle
);
4459 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4460 ERR("Failed to set trace chunk's directory handle");
4461 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4466 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4467 consumer_data
.chunk_registry
, session_id
,
4469 lttng_trace_chunk_put(created_chunk
);
4470 created_chunk
= NULL
;
4471 if (!published_chunk
) {
4472 ERR("Failed to publish trace chunk");
4473 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4478 cds_lfht_for_each_entry_duplicate(consumer_data
.channels_by_session_id_ht
->ht
,
4479 consumer_data
.channels_by_session_id_ht
->hash_fct(
4480 &session_id
, lttng_ht_seed
),
4481 consumer_data
.channels_by_session_id_ht
->match_fct
,
4482 &session_id
, &iter
.iter
, channel
,
4483 channels_by_session_id_ht_node
.node
) {
4484 ret
= lttng_consumer_channel_set_trace_chunk(channel
,
4488 * Roll-back the creation of this chunk.
4490 * This is important since the session daemon will
4491 * assume that the creation of this chunk failed and
4492 * will never ask for it to be closed, resulting
4493 * in a leak and an inconsistent state for some
4496 enum lttcomm_return_code close_ret
;
4497 char path
[LTTNG_PATH_MAX
];
4499 DBG("Failed to set new trace chunk on existing channels, rolling back");
4500 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4501 session_id
, chunk_id
,
4502 chunk_creation_timestamp
, NULL
,
4504 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4505 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4506 session_id
, chunk_id
);
4509 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4515 struct consumer_relayd_sock_pair
*relayd
;
4517 relayd
= consumer_find_relayd(*relayd_id
);
4519 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4520 ret
= relayd_create_trace_chunk(
4521 &relayd
->control_sock
, published_chunk
);
4522 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4524 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4527 if (!relayd
|| ret
) {
4528 enum lttcomm_return_code close_ret
;
4529 char path
[LTTNG_PATH_MAX
];
4531 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4534 chunk_creation_timestamp
,
4536 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4537 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4542 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4549 /* Release the reference returned by the "publish" operation. */
4550 lttng_trace_chunk_put(published_chunk
);
4551 lttng_trace_chunk_put(created_chunk
);
4555 enum lttcomm_return_code
lttng_consumer_close_trace_chunk(
4556 const uint64_t *relayd_id
, uint64_t session_id
,
4557 uint64_t chunk_id
, time_t chunk_close_timestamp
,
4558 const enum lttng_trace_chunk_command_type
*close_command
,
4561 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4562 struct lttng_trace_chunk
*chunk
;
4563 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4564 const char *relayd_id_str
= "(none)";
4565 const char *close_command_name
= "none";
4566 struct lttng_ht_iter iter
;
4567 struct lttng_consumer_channel
*channel
;
4568 enum lttng_trace_chunk_status chunk_status
;
4573 /* Only used for logging purposes. */
4574 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4575 "%" PRIu64
, *relayd_id
);
4576 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4577 relayd_id_str
= relayd_id_buffer
;
4579 relayd_id_str
= "(formatting error)";
4582 if (close_command
) {
4583 close_command_name
= lttng_trace_chunk_command_type_get_name(
4587 DBG("Consumer close trace chunk command: relayd_id = %s"
4588 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4589 ", close command = %s",
4590 relayd_id_str
, session_id
, chunk_id
,
4591 close_command_name
);
4593 chunk
= lttng_trace_chunk_registry_find_chunk(
4594 consumer_data
.chunk_registry
, session_id
, chunk_id
);
4596 ERR("Failed to find chunk: session_id = %" PRIu64
4597 ", chunk_id = %" PRIu64
,
4598 session_id
, chunk_id
);
4599 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4603 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
,
4604 chunk_close_timestamp
);
4605 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4606 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4610 if (close_command
) {
4611 chunk_status
= lttng_trace_chunk_set_close_command(
4612 chunk
, *close_command
);
4613 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4614 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4620 * chunk is now invalid to access as we no longer hold a reference to
4621 * it; it is only kept around to compare it (by address) to the
4622 * current chunk found in the session's channels.
4625 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
,
4626 channel
, node
.node
) {
4630 * Only change the channel's chunk to NULL if it still
4631 * references the chunk being closed. The channel may
4632 * reference a newer channel in the case of a session
4633 * rotation. When a session rotation occurs, the "next"
4634 * chunk is created before the "current" chunk is closed.
4636 if (channel
->trace_chunk
!= chunk
) {
4639 ret
= lttng_consumer_channel_set_trace_chunk(channel
, NULL
);
4642 * Attempt to close the chunk on as many channels as
4645 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4651 struct consumer_relayd_sock_pair
*relayd
;
4653 relayd
= consumer_find_relayd(*relayd_id
);
4655 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4656 ret
= relayd_close_trace_chunk(
4657 &relayd
->control_sock
, chunk
,
4659 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4661 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
,
4665 if (!relayd
|| ret
) {
4666 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4674 * Release the reference returned by the "find" operation and
4675 * the session daemon's implicit reference to the chunk.
4677 lttng_trace_chunk_put(chunk
);
4678 lttng_trace_chunk_put(chunk
);
4683 enum lttcomm_return_code
lttng_consumer_trace_chunk_exists(
4684 const uint64_t *relayd_id
, uint64_t session_id
,
4688 enum lttcomm_return_code ret_code
;
4689 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4690 const char *relayd_id_str
= "(none)";
4691 const bool is_local_trace
= !relayd_id
;
4692 struct consumer_relayd_sock_pair
*relayd
= NULL
;
4693 bool chunk_exists_local
, chunk_exists_remote
;
4698 /* Only used for logging purposes. */
4699 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4700 "%" PRIu64
, *relayd_id
);
4701 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4702 relayd_id_str
= relayd_id_buffer
;
4704 relayd_id_str
= "(formatting error)";
4708 DBG("Consumer trace chunk exists command: relayd_id = %s"
4709 ", chunk_id = %" PRIu64
, relayd_id_str
,
4711 ret
= lttng_trace_chunk_registry_chunk_exists(
4712 consumer_data
.chunk_registry
, session_id
,
4713 chunk_id
, &chunk_exists_local
);
4715 /* Internal error. */
4716 ERR("Failed to query the existence of a trace chunk");
4717 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
4720 DBG("Trace chunk %s locally",
4721 chunk_exists_local
? "exists" : "does not exist");
4722 if (chunk_exists_local
) {
4723 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
4725 } else if (is_local_trace
) {
4726 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4731 relayd
= consumer_find_relayd(*relayd_id
);
4733 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
4734 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
4735 goto end_rcu_unlock
;
4737 DBG("Looking up existence of trace chunk on relay daemon");
4738 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4739 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
,
4740 &chunk_exists_remote
);
4741 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4743 ERR("Failed to look-up the existence of trace chunk on relay daemon");
4744 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
4745 goto end_rcu_unlock
;
4748 ret_code
= chunk_exists_remote
?
4749 LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
4750 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4751 DBG("Trace chunk %s on relay daemon",
4752 chunk_exists_remote
? "exists" : "does not exist");