2 * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
10 #include "common/index/ctf-index.h"
18 #include <sys/socket.h>
19 #include <sys/types.h>
24 #include <bin/lttng-consumerd/health-consumerd.h>
25 #include <common/common.h>
26 #include <common/utils.h>
27 #include <common/time.h>
28 #include <common/compat/poll.h>
29 #include <common/compat/endian.h>
30 #include <common/index/index.h>
31 #include <common/kernel-ctl/kernel-ctl.h>
32 #include <common/sessiond-comm/relayd.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
34 #include <common/kernel-consumer/kernel-consumer.h>
35 #include <common/relayd/relayd.h>
36 #include <common/ust-consumer/ust-consumer.h>
37 #include <common/consumer/consumer-timer.h>
38 #include <common/consumer/consumer.h>
39 #include <common/consumer/consumer-stream.h>
40 #include <common/consumer/consumer-testpoint.h>
41 #include <common/align.h>
42 #include <common/consumer/consumer-metadata-cache.h>
43 #include <common/trace-chunk.h>
44 #include <common/trace-chunk-registry.h>
45 #include <common/string-utils/format.h>
46 #include <common/dynamic-array.h>
48 struct lttng_consumer_global_data consumer_data
= {
51 .type
= LTTNG_CONSUMER_UNKNOWN
,
54 enum consumer_channel_action
{
57 CONSUMER_CHANNEL_QUIT
,
60 struct consumer_channel_msg
{
61 enum consumer_channel_action action
;
62 struct lttng_consumer_channel
*chan
; /* add */
63 uint64_t key
; /* del */
66 enum open_packet_status
{
67 OPEN_PACKET_STATUS_OPENED
,
68 OPEN_PACKET_STATUS_NO_SPACE
,
69 OPEN_PACKET_STATUS_ERROR
,
72 /* Flag used to temporarily pause data consumption from testpoints. */
73 int data_consumption_paused
;
76 * Flag to inform the polling thread to quit when all fd hung up. Updated by
77 * the consumer_thread_receive_fds when it notices that all fds has hung up.
78 * Also updated by the signal handler (consumer_should_exit()). Read by the
84 * Global hash table containing respectively metadata and data streams. The
85 * stream element in this ht should only be updated by the metadata poll thread
86 * for the metadata and the data poll thread for the data.
88 static struct lttng_ht
*metadata_ht
;
89 static struct lttng_ht
*data_ht
;
91 static const char *get_consumer_domain(void)
93 switch (consumer_data
.type
) {
94 case LTTNG_CONSUMER_KERNEL
:
95 return DEFAULT_KERNEL_TRACE_DIR
;
96 case LTTNG_CONSUMER64_UST
:
98 case LTTNG_CONSUMER32_UST
:
99 return DEFAULT_UST_TRACE_DIR
;
106 * Notify a thread lttng pipe to poll back again. This usually means that some
107 * global state has changed so we just send back the thread in a poll wait
110 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
112 struct lttng_consumer_stream
*null_stream
= NULL
;
116 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
119 static void notify_health_quit_pipe(int *pipe
)
123 ret
= lttng_write(pipe
[1], "4", 1);
125 PERROR("write consumer health quit");
129 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
130 struct lttng_consumer_channel
*chan
,
132 enum consumer_channel_action action
)
134 struct consumer_channel_msg msg
;
137 memset(&msg
, 0, sizeof(msg
));
142 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
143 if (ret
< sizeof(msg
)) {
144 PERROR("notify_channel_pipe write error");
148 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
151 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
154 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
155 struct lttng_consumer_channel
**chan
,
157 enum consumer_channel_action
*action
)
159 struct consumer_channel_msg msg
;
162 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
163 if (ret
< sizeof(msg
)) {
167 *action
= msg
.action
;
175 * Cleanup the stream list of a channel. Those streams are not yet globally
178 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
180 struct lttng_consumer_stream
*stream
, *stmp
;
184 /* Delete streams that might have been left in the stream list. */
185 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
187 cds_list_del(&stream
->send_node
);
189 * Once a stream is added to this list, the buffers were created so we
190 * have a guarantee that this call will succeed. Setting the monitor
191 * mode to 0 so we don't lock nor try to delete the stream from the
195 consumer_stream_destroy(stream
, NULL
);
200 * Find a stream. The consumer_data.lock must be locked during this
203 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
206 struct lttng_ht_iter iter
;
207 struct lttng_ht_node_u64
*node
;
208 struct lttng_consumer_stream
*stream
= NULL
;
212 /* -1ULL keys are lookup failures */
213 if (key
== (uint64_t) -1ULL) {
219 lttng_ht_lookup(ht
, &key
, &iter
);
220 node
= lttng_ht_iter_get_node_u64(&iter
);
222 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
230 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
232 struct lttng_consumer_stream
*stream
;
235 stream
= find_stream(key
, ht
);
237 stream
->key
= (uint64_t) -1ULL;
239 * We don't want the lookup to match, but we still need
240 * to iterate on this stream when iterating over the hash table. Just
241 * change the node key.
243 stream
->node
.key
= (uint64_t) -1ULL;
249 * Return a channel object for the given key.
251 * RCU read side lock MUST be acquired before calling this function and
252 * protects the channel ptr.
254 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
256 struct lttng_ht_iter iter
;
257 struct lttng_ht_node_u64
*node
;
258 struct lttng_consumer_channel
*channel
= NULL
;
260 /* -1ULL keys are lookup failures */
261 if (key
== (uint64_t) -1ULL) {
265 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
266 node
= lttng_ht_iter_get_node_u64(&iter
);
268 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
275 * There is a possibility that the consumer does not have enough time between
276 * the close of the channel on the session daemon and the cleanup in here thus
277 * once we have a channel add with an existing key, we know for sure that this
278 * channel will eventually get cleaned up by all streams being closed.
280 * This function just nullifies the already existing channel key.
282 static void steal_channel_key(uint64_t key
)
284 struct lttng_consumer_channel
*channel
;
287 channel
= consumer_find_channel(key
);
289 channel
->key
= (uint64_t) -1ULL;
291 * We don't want the lookup to match, but we still need to iterate on
292 * this channel when iterating over the hash table. Just change the
295 channel
->node
.key
= (uint64_t) -1ULL;
300 static void free_channel_rcu(struct rcu_head
*head
)
302 struct lttng_ht_node_u64
*node
=
303 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
304 struct lttng_consumer_channel
*channel
=
305 caa_container_of(node
, struct lttng_consumer_channel
, node
);
307 switch (consumer_data
.type
) {
308 case LTTNG_CONSUMER_KERNEL
:
310 case LTTNG_CONSUMER32_UST
:
311 case LTTNG_CONSUMER64_UST
:
312 lttng_ustconsumer_free_channel(channel
);
315 ERR("Unknown consumer_data type");
322 * RCU protected relayd socket pair free.
324 static void free_relayd_rcu(struct rcu_head
*head
)
326 struct lttng_ht_node_u64
*node
=
327 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
328 struct consumer_relayd_sock_pair
*relayd
=
329 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
332 * Close all sockets. This is done in the call RCU since we don't want the
333 * socket fds to be reassigned thus potentially creating bad state of the
336 * We do not have to lock the control socket mutex here since at this stage
337 * there is no one referencing to this relayd object.
339 (void) relayd_close(&relayd
->control_sock
);
340 (void) relayd_close(&relayd
->data_sock
);
342 pthread_mutex_destroy(&relayd
->ctrl_sock_mutex
);
347 * Destroy and free relayd socket pair object.
349 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
352 struct lttng_ht_iter iter
;
354 if (relayd
== NULL
) {
358 DBG("Consumer destroy and close relayd socket pair");
360 iter
.iter
.node
= &relayd
->node
.node
;
361 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
363 /* We assume the relayd is being or is destroyed */
367 /* RCU free() call */
368 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
372 * Remove a channel from the global list protected by a mutex. This function is
373 * also responsible for freeing its data structures.
375 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
377 struct lttng_ht_iter iter
;
379 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
381 pthread_mutex_lock(&consumer_data
.lock
);
382 pthread_mutex_lock(&channel
->lock
);
384 /* Destroy streams that might have been left in the stream list. */
385 clean_channel_stream_list(channel
);
387 if (channel
->live_timer_enabled
== 1) {
388 consumer_timer_live_stop(channel
);
390 if (channel
->monitor_timer_enabled
== 1) {
391 consumer_timer_monitor_stop(channel
);
394 switch (consumer_data
.type
) {
395 case LTTNG_CONSUMER_KERNEL
:
397 case LTTNG_CONSUMER32_UST
:
398 case LTTNG_CONSUMER64_UST
:
399 lttng_ustconsumer_del_channel(channel
);
402 ERR("Unknown consumer_data type");
407 lttng_trace_chunk_put(channel
->trace_chunk
);
408 channel
->trace_chunk
= NULL
;
410 if (channel
->is_published
) {
414 iter
.iter
.node
= &channel
->node
.node
;
415 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
418 iter
.iter
.node
= &channel
->channels_by_session_id_ht_node
.node
;
419 ret
= lttng_ht_del(consumer_data
.channels_by_session_id_ht
,
425 channel
->is_deleted
= true;
426 call_rcu(&channel
->node
.head
, free_channel_rcu
);
428 pthread_mutex_unlock(&channel
->lock
);
429 pthread_mutex_unlock(&consumer_data
.lock
);
433 * Iterate over the relayd hash table and destroy each element. Finally,
434 * destroy the whole hash table.
436 static void cleanup_relayd_ht(void)
438 struct lttng_ht_iter iter
;
439 struct consumer_relayd_sock_pair
*relayd
;
443 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
445 consumer_destroy_relayd(relayd
);
450 lttng_ht_destroy(consumer_data
.relayd_ht
);
454 * Update the end point status of all streams having the given network sequence
455 * index (relayd index).
457 * It's atomically set without having the stream mutex locked which is fine
458 * because we handle the write/read race with a pipe wakeup for each thread.
460 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
461 enum consumer_endpoint_status status
)
463 struct lttng_ht_iter iter
;
464 struct lttng_consumer_stream
*stream
;
466 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
470 /* Let's begin with metadata */
471 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
472 if (stream
->net_seq_idx
== net_seq_idx
) {
473 uatomic_set(&stream
->endpoint_status
, status
);
474 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
478 /* Follow up by the data streams */
479 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
480 if (stream
->net_seq_idx
== net_seq_idx
) {
481 uatomic_set(&stream
->endpoint_status
, status
);
482 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
489 * Cleanup a relayd object by flagging every associated streams for deletion,
490 * destroying the object meaning removing it from the relayd hash table,
491 * closing the sockets and freeing the memory in a RCU call.
493 * If a local data context is available, notify the threads that the streams'
494 * state have changed.
496 void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
)
502 DBG("Cleaning up relayd object ID %"PRIu64
, relayd
->net_seq_idx
);
504 /* Save the net sequence index before destroying the object */
505 netidx
= relayd
->net_seq_idx
;
508 * Delete the relayd from the relayd hash table, close the sockets and free
509 * the object in a RCU call.
511 consumer_destroy_relayd(relayd
);
513 /* Set inactive endpoint to all streams */
514 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
517 * With a local data context, notify the threads that the streams' state
518 * have changed. The write() action on the pipe acts as an "implicit"
519 * memory barrier ordering the updates of the end point status from the
520 * read of this status which happens AFTER receiving this notify.
522 notify_thread_lttng_pipe(relayd
->ctx
->consumer_data_pipe
);
523 notify_thread_lttng_pipe(relayd
->ctx
->consumer_metadata_pipe
);
527 * Flag a relayd socket pair for destruction. Destroy it if the refcount
530 * RCU read side lock MUST be aquired before calling this function.
532 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
536 /* Set destroy flag for this object */
537 uatomic_set(&relayd
->destroy_flag
, 1);
539 /* Destroy the relayd if refcount is 0 */
540 if (uatomic_read(&relayd
->refcount
) == 0) {
541 consumer_destroy_relayd(relayd
);
546 * Completly destroy stream from every visiable data structure and the given
549 * One this call returns, the stream object is not longer usable nor visible.
551 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
554 consumer_stream_destroy(stream
, ht
);
558 * XXX naming of del vs destroy is all mixed up.
560 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
562 consumer_stream_destroy(stream
, data_ht
);
565 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
567 consumer_stream_destroy(stream
, metadata_ht
);
570 void consumer_stream_update_channel_attributes(
571 struct lttng_consumer_stream
*stream
,
572 struct lttng_consumer_channel
*channel
)
574 stream
->channel_read_only_attributes
.tracefile_size
=
575 channel
->tracefile_size
;
579 * Add a stream to the global list protected by a mutex.
581 void consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
583 struct lttng_ht
*ht
= data_ht
;
588 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
590 pthread_mutex_lock(&consumer_data
.lock
);
591 pthread_mutex_lock(&stream
->chan
->lock
);
592 pthread_mutex_lock(&stream
->chan
->timer_lock
);
593 pthread_mutex_lock(&stream
->lock
);
596 /* Steal stream identifier to avoid having streams with the same key */
597 steal_stream_key(stream
->key
, ht
);
599 lttng_ht_add_unique_u64(ht
, &stream
->node
);
601 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
602 &stream
->node_channel_id
);
605 * Add stream to the stream_list_ht of the consumer data. No need to steal
606 * the key since the HT does not use it and we allow to add redundant keys
609 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
612 * When nb_init_stream_left reaches 0, we don't need to trigger any action
613 * in terms of destroying the associated channel, because the action that
614 * causes the count to become 0 also causes a stream to be added. The
615 * channel deletion will thus be triggered by the following removal of this
618 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
619 /* Increment refcount before decrementing nb_init_stream_left */
621 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
624 /* Update consumer data once the node is inserted. */
625 consumer_data
.stream_count
++;
626 consumer_data
.need_update
= 1;
629 pthread_mutex_unlock(&stream
->lock
);
630 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
631 pthread_mutex_unlock(&stream
->chan
->lock
);
632 pthread_mutex_unlock(&consumer_data
.lock
);
636 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
637 * be acquired before calling this.
639 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
642 struct lttng_ht_node_u64
*node
;
643 struct lttng_ht_iter iter
;
647 lttng_ht_lookup(consumer_data
.relayd_ht
,
648 &relayd
->net_seq_idx
, &iter
);
649 node
= lttng_ht_iter_get_node_u64(&iter
);
653 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
660 * Allocate and return a consumer relayd socket.
662 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
663 uint64_t net_seq_idx
)
665 struct consumer_relayd_sock_pair
*obj
= NULL
;
667 /* net sequence index of -1 is a failure */
668 if (net_seq_idx
== (uint64_t) -1ULL) {
672 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
674 PERROR("zmalloc relayd sock");
678 obj
->net_seq_idx
= net_seq_idx
;
680 obj
->destroy_flag
= 0;
681 obj
->control_sock
.sock
.fd
= -1;
682 obj
->data_sock
.sock
.fd
= -1;
683 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
684 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
691 * Find a relayd socket pair in the global consumer data.
693 * Return the object if found else NULL.
694 * RCU read-side lock must be held across this call and while using the
697 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
699 struct lttng_ht_iter iter
;
700 struct lttng_ht_node_u64
*node
;
701 struct consumer_relayd_sock_pair
*relayd
= NULL
;
703 /* Negative keys are lookup failures */
704 if (key
== (uint64_t) -1ULL) {
708 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
710 node
= lttng_ht_iter_get_node_u64(&iter
);
712 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
720 * Find a relayd and send the stream
722 * Returns 0 on success, < 0 on error
724 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
728 struct consumer_relayd_sock_pair
*relayd
;
731 assert(stream
->net_seq_idx
!= -1ULL);
734 /* The stream is not metadata. Get relayd reference if exists. */
736 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
737 if (relayd
!= NULL
) {
738 /* Add stream on the relayd */
739 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
740 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
741 get_consumer_domain(), path
, &stream
->relayd_stream_id
,
742 stream
->chan
->tracefile_size
,
743 stream
->chan
->tracefile_count
,
744 stream
->trace_chunk
);
745 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
747 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
748 lttng_consumer_cleanup_relayd(relayd
);
752 uatomic_inc(&relayd
->refcount
);
753 stream
->sent_to_relayd
= 1;
755 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
756 stream
->key
, stream
->net_seq_idx
);
761 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
762 stream
->name
, stream
->key
, stream
->net_seq_idx
);
770 * Find a relayd and send the streams sent message
772 * Returns 0 on success, < 0 on error
774 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
777 struct consumer_relayd_sock_pair
*relayd
;
779 assert(net_seq_idx
!= -1ULL);
781 /* The stream is not metadata. Get relayd reference if exists. */
783 relayd
= consumer_find_relayd(net_seq_idx
);
784 if (relayd
!= NULL
) {
785 /* Add stream on the relayd */
786 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
787 ret
= relayd_streams_sent(&relayd
->control_sock
);
788 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
790 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
791 lttng_consumer_cleanup_relayd(relayd
);
795 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
802 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
810 * Find a relayd and close the stream
812 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
814 struct consumer_relayd_sock_pair
*relayd
;
816 /* The stream is not metadata. Get relayd reference if exists. */
818 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
820 consumer_stream_relayd_close(stream
, relayd
);
826 * Handle stream for relayd transmission if the stream applies for network
827 * streaming where the net sequence index is set.
829 * Return destination file descriptor or negative value on error.
831 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
832 size_t data_size
, unsigned long padding
,
833 struct consumer_relayd_sock_pair
*relayd
)
836 struct lttcomm_relayd_data_hdr data_hdr
;
842 /* Reset data header */
843 memset(&data_hdr
, 0, sizeof(data_hdr
));
845 if (stream
->metadata_flag
) {
846 /* Caller MUST acquire the relayd control socket lock */
847 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
852 /* Metadata are always sent on the control socket. */
853 outfd
= relayd
->control_sock
.sock
.fd
;
855 /* Set header with stream information */
856 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
857 data_hdr
.data_size
= htobe32(data_size
);
858 data_hdr
.padding_size
= htobe32(padding
);
861 * Note that net_seq_num below is assigned with the *current* value of
862 * next_net_seq_num and only after that the next_net_seq_num will be
863 * increment. This is why when issuing a command on the relayd using
864 * this next value, 1 should always be substracted in order to compare
865 * the last seen sequence number on the relayd side to the last sent.
867 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
868 /* Other fields are zeroed previously */
870 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
876 ++stream
->next_net_seq_num
;
878 /* Set to go on data socket */
879 outfd
= relayd
->data_sock
.sock
.fd
;
887 * Trigger a dump of the metadata content. Following/during the succesful
888 * completion of this call, the metadata poll thread will start receiving
889 * metadata packets to consume.
891 * The caller must hold the channel and stream locks.
894 int consumer_metadata_stream_dump(struct lttng_consumer_stream
*stream
)
898 ASSERT_LOCKED(stream
->chan
->lock
);
899 ASSERT_LOCKED(stream
->lock
);
900 assert(stream
->metadata_flag
);
901 assert(stream
->chan
->trace_chunk
);
903 switch (consumer_data
.type
) {
904 case LTTNG_CONSUMER_KERNEL
:
906 * Reset the position of what has been read from the
907 * metadata cache to 0 so we can dump it again.
909 ret
= kernctl_metadata_cache_dump(stream
->wait_fd
);
911 case LTTNG_CONSUMER32_UST
:
912 case LTTNG_CONSUMER64_UST
:
914 * Reset the position pushed from the metadata cache so it
915 * will write from the beginning on the next push.
917 stream
->ust_metadata_pushed
= 0;
918 ret
= consumer_metadata_wakeup_pipe(stream
->chan
);
921 ERR("Unknown consumer_data type");
925 ERR("Failed to dump the metadata cache");
931 int lttng_consumer_channel_set_trace_chunk(
932 struct lttng_consumer_channel
*channel
,
933 struct lttng_trace_chunk
*new_trace_chunk
)
935 pthread_mutex_lock(&channel
->lock
);
936 if (channel
->is_deleted
) {
938 * The channel has been logically deleted and should no longer
939 * be used. It has released its reference to its current trace
940 * chunk and should not acquire a new one.
942 * Return success as there is nothing for the caller to do.
948 * The acquisition of the reference cannot fail (barring
949 * a severe internal error) since a reference to the published
950 * chunk is already held by the caller.
952 if (new_trace_chunk
) {
953 const bool acquired_reference
= lttng_trace_chunk_get(
956 assert(acquired_reference
);
959 lttng_trace_chunk_put(channel
->trace_chunk
);
960 channel
->trace_chunk
= new_trace_chunk
;
962 pthread_mutex_unlock(&channel
->lock
);
967 * Allocate and return a new lttng_consumer_channel object using the given key
968 * to initialize the hash table node.
970 * On error, return NULL.
972 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
974 const uint64_t *chunk_id
,
975 const char *pathname
,
978 enum lttng_event_output output
,
979 uint64_t tracefile_size
,
980 uint64_t tracefile_count
,
981 uint64_t session_id_per_pid
,
982 unsigned int monitor
,
983 unsigned int live_timer_interval
,
984 bool is_in_live_session
,
985 const char *root_shm_path
,
986 const char *shm_path
)
988 struct lttng_consumer_channel
*channel
= NULL
;
989 struct lttng_trace_chunk
*trace_chunk
= NULL
;
992 trace_chunk
= lttng_trace_chunk_registry_find_chunk(
993 consumer_data
.chunk_registry
, session_id
,
996 ERR("Failed to find trace chunk reference during creation of channel");
1001 channel
= zmalloc(sizeof(*channel
));
1002 if (channel
== NULL
) {
1003 PERROR("malloc struct lttng_consumer_channel");
1008 channel
->refcount
= 0;
1009 channel
->session_id
= session_id
;
1010 channel
->session_id_per_pid
= session_id_per_pid
;
1011 channel
->relayd_id
= relayd_id
;
1012 channel
->tracefile_size
= tracefile_size
;
1013 channel
->tracefile_count
= tracefile_count
;
1014 channel
->monitor
= monitor
;
1015 channel
->live_timer_interval
= live_timer_interval
;
1016 channel
->is_live
= is_in_live_session
;
1017 pthread_mutex_init(&channel
->lock
, NULL
);
1018 pthread_mutex_init(&channel
->timer_lock
, NULL
);
1021 case LTTNG_EVENT_SPLICE
:
1022 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
1024 case LTTNG_EVENT_MMAP
:
1025 channel
->output
= CONSUMER_CHANNEL_MMAP
;
1035 * In monitor mode, the streams associated with the channel will be put in
1036 * a special list ONLY owned by this channel. So, the refcount is set to 1
1037 * here meaning that the channel itself has streams that are referenced.
1039 * On a channel deletion, once the channel is no longer visible, the
1040 * refcount is decremented and checked for a zero value to delete it. With
1041 * streams in no monitor mode, it will now be safe to destroy the channel.
1043 if (!channel
->monitor
) {
1044 channel
->refcount
= 1;
1047 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1048 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1050 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1051 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1053 if (root_shm_path
) {
1054 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1055 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1058 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1059 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1062 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1063 lttng_ht_node_init_u64(&channel
->channels_by_session_id_ht_node
,
1064 channel
->session_id
);
1066 channel
->wait_fd
= -1;
1067 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1070 int ret
= lttng_consumer_channel_set_trace_chunk(channel
,
1077 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1080 lttng_trace_chunk_put(trace_chunk
);
1083 consumer_del_channel(channel
);
1089 * Add a channel to the global list protected by a mutex.
1091 * Always return 0 indicating success.
1093 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1094 struct lttng_consumer_local_data
*ctx
)
1096 pthread_mutex_lock(&consumer_data
.lock
);
1097 pthread_mutex_lock(&channel
->lock
);
1098 pthread_mutex_lock(&channel
->timer_lock
);
1101 * This gives us a guarantee that the channel we are about to add to the
1102 * channel hash table will be unique. See this function comment on the why
1103 * we need to steel the channel key at this stage.
1105 steal_channel_key(channel
->key
);
1108 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1109 lttng_ht_add_u64(consumer_data
.channels_by_session_id_ht
,
1110 &channel
->channels_by_session_id_ht_node
);
1112 channel
->is_published
= true;
1114 pthread_mutex_unlock(&channel
->timer_lock
);
1115 pthread_mutex_unlock(&channel
->lock
);
1116 pthread_mutex_unlock(&consumer_data
.lock
);
1118 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1119 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1126 * Allocate the pollfd structure and the local view of the out fds to avoid
1127 * doing a lookup in the linked list and concurrency issues when writing is
1128 * needed. Called with consumer_data.lock held.
1130 * Returns the number of fds in the structures.
1132 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1133 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1134 struct lttng_ht
*ht
, int *nb_inactive_fd
)
1137 struct lttng_ht_iter iter
;
1138 struct lttng_consumer_stream
*stream
;
1143 assert(local_stream
);
1145 DBG("Updating poll fd array");
1146 *nb_inactive_fd
= 0;
1148 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1150 * Only active streams with an active end point can be added to the
1151 * poll set and local stream storage of the thread.
1153 * There is a potential race here for endpoint_status to be updated
1154 * just after the check. However, this is OK since the stream(s) will
1155 * be deleted once the thread is notified that the end point state has
1156 * changed where this function will be called back again.
1158 * We track the number of inactive FDs because they still need to be
1159 * closed by the polling thread after a wakeup on the data_pipe or
1162 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1163 (*nb_inactive_fd
)++;
1167 * This clobbers way too much the debug output. Uncomment that if you
1168 * need it for debugging purposes.
1170 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1171 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1172 local_stream
[i
] = stream
;
1178 * Insert the consumer_data_pipe at the end of the array and don't
1179 * increment i so nb_fd is the number of real FD.
1181 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1182 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1184 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1185 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1190 * Poll on the should_quit pipe and the command socket return -1 on
1191 * error, 1 if should exit, 0 if data is available on the command socket
1193 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1198 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1199 if (num_rdy
== -1) {
1201 * Restart interrupted system call.
1203 if (errno
== EINTR
) {
1206 PERROR("Poll error");
1209 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1210 DBG("consumer_should_quit wake up");
1217 * Set the error socket.
1219 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1222 ctx
->consumer_error_socket
= sock
;
1226 * Set the command socket path.
1228 void lttng_consumer_set_command_sock_path(
1229 struct lttng_consumer_local_data
*ctx
, char *sock
)
1231 ctx
->consumer_command_sock_path
= sock
;
1235 * Send return code to the session daemon.
1236 * If the socket is not defined, we return 0, it is not a fatal error
1238 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1240 if (ctx
->consumer_error_socket
> 0) {
1241 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1242 sizeof(enum lttcomm_sessiond_command
));
1249 * Close all the tracefiles and stream fds and MUST be called when all
1250 * instances are destroyed i.e. when all threads were joined and are ended.
1252 void lttng_consumer_cleanup(void)
1254 struct lttng_ht_iter iter
;
1255 struct lttng_consumer_channel
*channel
;
1256 unsigned int trace_chunks_left
;
1260 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1262 consumer_del_channel(channel
);
1267 lttng_ht_destroy(consumer_data
.channel_ht
);
1268 lttng_ht_destroy(consumer_data
.channels_by_session_id_ht
);
1270 cleanup_relayd_ht();
1272 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1275 * This HT contains streams that are freed by either the metadata thread or
1276 * the data thread so we do *nothing* on the hash table and simply destroy
1279 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1282 * Trace chunks in the registry may still exist if the session
1283 * daemon has encountered an internal error and could not
1284 * tear down its sessions and/or trace chunks properly.
1286 * Release the session daemon's implicit reference to any remaining
1287 * trace chunk and print an error if any trace chunk was found. Note
1288 * that there are _no_ legitimate cases for trace chunks to be left,
1289 * it is a leak. However, it can happen following a crash of the
1290 * session daemon and not emptying the registry would cause an assertion
1293 trace_chunks_left
= lttng_trace_chunk_registry_put_each_chunk(
1294 consumer_data
.chunk_registry
);
1295 if (trace_chunks_left
) {
1296 ERR("%u trace chunks are leaked by lttng-consumerd. "
1297 "This can be caused by an internal error of the session daemon.",
1300 /* Run all callbacks freeing each chunk. */
1302 lttng_trace_chunk_registry_destroy(consumer_data
.chunk_registry
);
1306 * Called from signal handler.
1308 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1312 CMM_STORE_SHARED(consumer_quit
, 1);
1313 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1315 PERROR("write consumer quit");
1318 DBG("Consumer flag that it should quit");
1323 * Flush pending writes to trace output disk file.
1326 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1330 int outfd
= stream
->out_fd
;
1333 * This does a blocking write-and-wait on any page that belongs to the
1334 * subbuffer prior to the one we just wrote.
1335 * Don't care about error values, as these are just hints and ways to
1336 * limit the amount of page cache used.
1338 if (orig_offset
< stream
->max_sb_size
) {
1341 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1342 stream
->max_sb_size
,
1343 SYNC_FILE_RANGE_WAIT_BEFORE
1344 | SYNC_FILE_RANGE_WRITE
1345 | SYNC_FILE_RANGE_WAIT_AFTER
);
1347 * Give hints to the kernel about how we access the file:
1348 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1351 * We need to call fadvise again after the file grows because the
1352 * kernel does not seem to apply fadvise to non-existing parts of the
1355 * Call fadvise _after_ having waited for the page writeback to
1356 * complete because the dirty page writeback semantic is not well
1357 * defined. So it can be expected to lead to lower throughput in
1360 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1361 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1362 if (ret
&& ret
!= -ENOSYS
) {
1364 PERROR("posix_fadvise on fd %i", outfd
);
1369 * Initialise the necessary environnement :
1370 * - create a new context
1371 * - create the poll_pipe
1372 * - create the should_quit pipe (for signal handler)
1373 * - create the thread pipe (for splice)
1375 * Takes a function pointer as argument, this function is called when data is
1376 * available on a buffer. This function is responsible to do the
1377 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1378 * buffer configuration and then kernctl_put_next_subbuf at the end.
1380 * Returns a pointer to the new context or NULL on error.
1382 struct lttng_consumer_local_data
*lttng_consumer_create(
1383 enum lttng_consumer_type type
,
1384 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1385 struct lttng_consumer_local_data
*ctx
, bool locked_by_caller
),
1386 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1387 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1388 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1391 struct lttng_consumer_local_data
*ctx
;
1393 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1394 consumer_data
.type
== type
);
1395 consumer_data
.type
= type
;
1397 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1399 PERROR("allocating context");
1403 ctx
->consumer_error_socket
= -1;
1404 ctx
->consumer_metadata_socket
= -1;
1405 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1406 /* assign the callbacks */
1407 ctx
->on_buffer_ready
= buffer_ready
;
1408 ctx
->on_recv_channel
= recv_channel
;
1409 ctx
->on_recv_stream
= recv_stream
;
1410 ctx
->on_update_stream
= update_stream
;
1412 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1413 if (!ctx
->consumer_data_pipe
) {
1414 goto error_poll_pipe
;
1417 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1418 if (!ctx
->consumer_wakeup_pipe
) {
1419 goto error_wakeup_pipe
;
1422 ret
= pipe(ctx
->consumer_should_quit
);
1424 PERROR("Error creating recv pipe");
1425 goto error_quit_pipe
;
1428 ret
= pipe(ctx
->consumer_channel_pipe
);
1430 PERROR("Error creating channel pipe");
1431 goto error_channel_pipe
;
1434 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1435 if (!ctx
->consumer_metadata_pipe
) {
1436 goto error_metadata_pipe
;
1439 ctx
->channel_monitor_pipe
= -1;
1443 error_metadata_pipe
:
1444 utils_close_pipe(ctx
->consumer_channel_pipe
);
1446 utils_close_pipe(ctx
->consumer_should_quit
);
1448 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1450 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1458 * Iterate over all streams of the hashtable and free them properly.
1460 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1462 struct lttng_ht_iter iter
;
1463 struct lttng_consumer_stream
*stream
;
1470 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1472 * Ignore return value since we are currently cleaning up so any error
1475 (void) consumer_del_stream(stream
, ht
);
1479 lttng_ht_destroy(ht
);
1483 * Iterate over all streams of the metadata hashtable and free them
1486 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1488 struct lttng_ht_iter iter
;
1489 struct lttng_consumer_stream
*stream
;
1496 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1498 * Ignore return value since we are currently cleaning up so any error
1501 (void) consumer_del_metadata_stream(stream
, ht
);
1505 lttng_ht_destroy(ht
);
1509 * Close all fds associated with the instance and free the context.
1511 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1515 DBG("Consumer destroying it. Closing everything.");
1521 destroy_data_stream_ht(data_ht
);
1522 destroy_metadata_stream_ht(metadata_ht
);
1524 ret
= close(ctx
->consumer_error_socket
);
1528 ret
= close(ctx
->consumer_metadata_socket
);
1532 utils_close_pipe(ctx
->consumer_channel_pipe
);
1533 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1534 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1535 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1536 utils_close_pipe(ctx
->consumer_should_quit
);
1538 unlink(ctx
->consumer_command_sock_path
);
1543 * Write the metadata stream id on the specified file descriptor.
1545 static int write_relayd_metadata_id(int fd
,
1546 struct lttng_consumer_stream
*stream
,
1547 unsigned long padding
)
1550 struct lttcomm_relayd_metadata_payload hdr
;
1552 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1553 hdr
.padding_size
= htobe32(padding
);
1554 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1555 if (ret
< sizeof(hdr
)) {
1557 * This error means that the fd's end is closed so ignore the PERROR
1558 * not to clubber the error output since this can happen in a normal
1561 if (errno
!= EPIPE
) {
1562 PERROR("write metadata stream id");
1564 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1566 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1567 * handle writting the missing part so report that as an error and
1568 * don't lie to the caller.
1573 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1574 stream
->relayd_stream_id
, padding
);
1581 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1582 * core function for writing trace buffers to either the local filesystem or
1585 * It must be called with the stream and the channel lock held.
1587 * Careful review MUST be put if any changes occur!
1589 * Returns the number of bytes written
1591 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1592 struct lttng_consumer_stream
*stream
,
1593 const struct lttng_buffer_view
*buffer
,
1594 unsigned long padding
)
1597 off_t orig_offset
= stream
->out_fd_offset
;
1598 /* Default is on the disk */
1599 int outfd
= stream
->out_fd
;
1600 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1601 unsigned int relayd_hang_up
= 0;
1602 const size_t subbuf_content_size
= buffer
->size
- padding
;
1605 /* RCU lock for the relayd pointer */
1607 assert(stream
->net_seq_idx
!= (uint64_t) -1ULL ||
1608 stream
->trace_chunk
);
1610 /* Flag that the current stream if set for network streaming. */
1611 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1612 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1613 if (relayd
== NULL
) {
1619 /* Handle stream on the relayd if the output is on the network */
1621 unsigned long netlen
= subbuf_content_size
;
1624 * Lock the control socket for the complete duration of the function
1625 * since from this point on we will use the socket.
1627 if (stream
->metadata_flag
) {
1628 /* Metadata requires the control socket. */
1629 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1630 if (stream
->reset_metadata_flag
) {
1631 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1632 stream
->relayd_stream_id
,
1633 stream
->metadata_version
);
1638 stream
->reset_metadata_flag
= 0;
1640 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1643 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1648 /* Use the returned socket. */
1651 /* Write metadata stream id before payload */
1652 if (stream
->metadata_flag
) {
1653 ret
= write_relayd_metadata_id(outfd
, stream
, padding
);
1660 write_len
= subbuf_content_size
;
1662 /* No streaming; we have to write the full padding. */
1663 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1664 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1666 ERR("Reset metadata file");
1669 stream
->reset_metadata_flag
= 0;
1673 * Check if we need to change the tracefile before writing the packet.
1675 if (stream
->chan
->tracefile_size
> 0 &&
1676 (stream
->tracefile_size_current
+ buffer
->size
) >
1677 stream
->chan
->tracefile_size
) {
1678 ret
= consumer_stream_rotate_output_files(stream
);
1682 outfd
= stream
->out_fd
;
1685 stream
->tracefile_size_current
+= buffer
->size
;
1686 write_len
= buffer
->size
;
1690 * This call guarantee that len or less is returned. It's impossible to
1691 * receive a ret value that is bigger than len.
1693 ret
= lttng_write(outfd
, buffer
->data
, write_len
);
1694 DBG("Consumer mmap write() ret %zd (len %zu)", ret
, write_len
);
1695 if (ret
< 0 || ((size_t) ret
!= write_len
)) {
1697 * Report error to caller if nothing was written else at least send the
1705 /* Socket operation failed. We consider the relayd dead */
1706 if (errno
== EPIPE
) {
1708 * This is possible if the fd is closed on the other side
1709 * (outfd) or any write problem. It can be verbose a bit for a
1710 * normal execution if for instance the relayd is stopped
1711 * abruptly. This can happen so set this to a DBG statement.
1713 DBG("Consumer mmap write detected relayd hang up");
1715 /* Unhandled error, print it and stop function right now. */
1716 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret
,
1721 stream
->output_written
+= ret
;
1723 /* This call is useless on a socket so better save a syscall. */
1725 /* This won't block, but will start writeout asynchronously */
1726 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, write_len
,
1727 SYNC_FILE_RANGE_WRITE
);
1728 stream
->out_fd_offset
+= write_len
;
1729 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1734 * This is a special case that the relayd has closed its socket. Let's
1735 * cleanup the relayd object and all associated streams.
1737 if (relayd
&& relayd_hang_up
) {
1738 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1739 lttng_consumer_cleanup_relayd(relayd
);
1743 /* Unlock only if ctrl socket used */
1744 if (relayd
&& stream
->metadata_flag
) {
1745 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1753 * Splice the data from the ring buffer to the tracefile.
1755 * It must be called with the stream lock held.
1757 * Returns the number of bytes spliced.
1759 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1760 struct lttng_consumer_local_data
*ctx
,
1761 struct lttng_consumer_stream
*stream
, unsigned long len
,
1762 unsigned long padding
)
1764 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1766 off_t orig_offset
= stream
->out_fd_offset
;
1767 int fd
= stream
->wait_fd
;
1768 /* Default is on the disk */
1769 int outfd
= stream
->out_fd
;
1770 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1772 unsigned int relayd_hang_up
= 0;
1774 switch (consumer_data
.type
) {
1775 case LTTNG_CONSUMER_KERNEL
:
1777 case LTTNG_CONSUMER32_UST
:
1778 case LTTNG_CONSUMER64_UST
:
1779 /* Not supported for user space tracing */
1782 ERR("Unknown consumer_data type");
1786 /* RCU lock for the relayd pointer */
1789 /* Flag that the current stream if set for network streaming. */
1790 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1791 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1792 if (relayd
== NULL
) {
1797 splice_pipe
= stream
->splice_pipe
;
1799 /* Write metadata stream id before payload */
1801 unsigned long total_len
= len
;
1803 if (stream
->metadata_flag
) {
1805 * Lock the control socket for the complete duration of the function
1806 * since from this point on we will use the socket.
1808 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1810 if (stream
->reset_metadata_flag
) {
1811 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1812 stream
->relayd_stream_id
,
1813 stream
->metadata_version
);
1818 stream
->reset_metadata_flag
= 0;
1820 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
,
1828 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1831 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1837 /* Use the returned socket. */
1840 /* No streaming, we have to set the len with the full padding */
1843 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1844 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1846 ERR("Reset metadata file");
1849 stream
->reset_metadata_flag
= 0;
1852 * Check if we need to change the tracefile before writing the packet.
1854 if (stream
->chan
->tracefile_size
> 0 &&
1855 (stream
->tracefile_size_current
+ len
) >
1856 stream
->chan
->tracefile_size
) {
1857 ret
= consumer_stream_rotate_output_files(stream
);
1862 outfd
= stream
->out_fd
;
1865 stream
->tracefile_size_current
+= len
;
1869 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1870 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1871 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1872 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1873 DBG("splice chan to pipe, ret %zd", ret_splice
);
1874 if (ret_splice
< 0) {
1877 PERROR("Error in relay splice");
1881 /* Handle stream on the relayd if the output is on the network */
1882 if (relayd
&& stream
->metadata_flag
) {
1883 size_t metadata_payload_size
=
1884 sizeof(struct lttcomm_relayd_metadata_payload
);
1886 /* Update counter to fit the spliced data */
1887 ret_splice
+= metadata_payload_size
;
1888 len
+= metadata_payload_size
;
1890 * We do this so the return value can match the len passed as
1891 * argument to this function.
1893 written
-= metadata_payload_size
;
1896 /* Splice data out */
1897 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1898 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1899 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1901 if (ret_splice
< 0) {
1906 } else if (ret_splice
> len
) {
1908 * We don't expect this code path to be executed but you never know
1909 * so this is an extra protection agains a buggy splice().
1912 written
+= ret_splice
;
1913 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1917 /* All good, update current len and continue. */
1921 /* This call is useless on a socket so better save a syscall. */
1923 /* This won't block, but will start writeout asynchronously */
1924 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1925 SYNC_FILE_RANGE_WRITE
);
1926 stream
->out_fd_offset
+= ret_splice
;
1928 stream
->output_written
+= ret_splice
;
1929 written
+= ret_splice
;
1932 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1938 * This is a special case that the relayd has closed its socket. Let's
1939 * cleanup the relayd object and all associated streams.
1941 if (relayd
&& relayd_hang_up
) {
1942 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1943 lttng_consumer_cleanup_relayd(relayd
);
1944 /* Skip splice error so the consumer does not fail */
1949 /* send the appropriate error description to sessiond */
1952 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1955 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1958 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1963 if (relayd
&& stream
->metadata_flag
) {
1964 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1972 * Sample the snapshot positions for a specific fd
1974 * Returns 0 on success, < 0 on error
1976 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
1978 switch (consumer_data
.type
) {
1979 case LTTNG_CONSUMER_KERNEL
:
1980 return lttng_kconsumer_sample_snapshot_positions(stream
);
1981 case LTTNG_CONSUMER32_UST
:
1982 case LTTNG_CONSUMER64_UST
:
1983 return lttng_ustconsumer_sample_snapshot_positions(stream
);
1985 ERR("Unknown consumer_data type");
1991 * Take a snapshot for a specific fd
1993 * Returns 0 on success, < 0 on error
1995 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1997 switch (consumer_data
.type
) {
1998 case LTTNG_CONSUMER_KERNEL
:
1999 return lttng_kconsumer_take_snapshot(stream
);
2000 case LTTNG_CONSUMER32_UST
:
2001 case LTTNG_CONSUMER64_UST
:
2002 return lttng_ustconsumer_take_snapshot(stream
);
2004 ERR("Unknown consumer_data type");
2011 * Get the produced position
2013 * Returns 0 on success, < 0 on error
2015 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
2018 switch (consumer_data
.type
) {
2019 case LTTNG_CONSUMER_KERNEL
:
2020 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
2021 case LTTNG_CONSUMER32_UST
:
2022 case LTTNG_CONSUMER64_UST
:
2023 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2025 ERR("Unknown consumer_data type");
2032 * Get the consumed position (free-running counter position in bytes).
2034 * Returns 0 on success, < 0 on error
2036 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
2039 switch (consumer_data
.type
) {
2040 case LTTNG_CONSUMER_KERNEL
:
2041 return lttng_kconsumer_get_consumed_snapshot(stream
, pos
);
2042 case LTTNG_CONSUMER32_UST
:
2043 case LTTNG_CONSUMER64_UST
:
2044 return lttng_ustconsumer_get_consumed_snapshot(stream
, pos
);
2046 ERR("Unknown consumer_data type");
2052 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2053 int sock
, struct pollfd
*consumer_sockpoll
)
2055 switch (consumer_data
.type
) {
2056 case LTTNG_CONSUMER_KERNEL
:
2057 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2058 case LTTNG_CONSUMER32_UST
:
2059 case LTTNG_CONSUMER64_UST
:
2060 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2062 ERR("Unknown consumer_data type");
2069 void lttng_consumer_close_all_metadata(void)
2071 switch (consumer_data
.type
) {
2072 case LTTNG_CONSUMER_KERNEL
:
2074 * The Kernel consumer has a different metadata scheme so we don't
2075 * close anything because the stream will be closed by the session
2079 case LTTNG_CONSUMER32_UST
:
2080 case LTTNG_CONSUMER64_UST
:
2082 * Close all metadata streams. The metadata hash table is passed and
2083 * this call iterates over it by closing all wakeup fd. This is safe
2084 * because at this point we are sure that the metadata producer is
2085 * either dead or blocked.
2087 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2090 ERR("Unknown consumer_data type");
2096 * Clean up a metadata stream and free its memory.
2098 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2099 struct lttng_ht
*ht
)
2101 struct lttng_consumer_channel
*channel
= NULL
;
2102 bool free_channel
= false;
2106 * This call should NEVER receive regular stream. It must always be
2107 * metadata stream and this is crucial for data structure synchronization.
2109 assert(stream
->metadata_flag
);
2111 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2113 pthread_mutex_lock(&consumer_data
.lock
);
2115 * Note that this assumes that a stream's channel is never changed and
2116 * that the stream's lock doesn't need to be taken to sample its
2119 channel
= stream
->chan
;
2120 pthread_mutex_lock(&channel
->lock
);
2121 pthread_mutex_lock(&stream
->lock
);
2122 if (channel
->metadata_cache
) {
2123 /* Only applicable to userspace consumers. */
2124 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
2127 /* Remove any reference to that stream. */
2128 consumer_stream_delete(stream
, ht
);
2130 /* Close down everything including the relayd if one. */
2131 consumer_stream_close(stream
);
2132 /* Destroy tracer buffers of the stream. */
2133 consumer_stream_destroy_buffers(stream
);
2135 /* Atomically decrement channel refcount since other threads can use it. */
2136 if (!uatomic_sub_return(&channel
->refcount
, 1)
2137 && !uatomic_read(&channel
->nb_init_stream_left
)) {
2138 /* Go for channel deletion! */
2139 free_channel
= true;
2141 stream
->chan
= NULL
;
2144 * Nullify the stream reference so it is not used after deletion. The
2145 * channel lock MUST be acquired before being able to check for a NULL
2148 channel
->metadata_stream
= NULL
;
2150 if (channel
->metadata_cache
) {
2151 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
2153 pthread_mutex_unlock(&stream
->lock
);
2154 pthread_mutex_unlock(&channel
->lock
);
2155 pthread_mutex_unlock(&consumer_data
.lock
);
2158 consumer_del_channel(channel
);
2161 lttng_trace_chunk_put(stream
->trace_chunk
);
2162 stream
->trace_chunk
= NULL
;
2163 consumer_stream_free(stream
);
2167 * Action done with the metadata stream when adding it to the consumer internal
2168 * data structures to handle it.
2170 void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2172 struct lttng_ht
*ht
= metadata_ht
;
2173 struct lttng_ht_iter iter
;
2174 struct lttng_ht_node_u64
*node
;
2179 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2181 pthread_mutex_lock(&consumer_data
.lock
);
2182 pthread_mutex_lock(&stream
->chan
->lock
);
2183 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2184 pthread_mutex_lock(&stream
->lock
);
2187 * From here, refcounts are updated so be _careful_ when returning an error
2194 * Lookup the stream just to make sure it does not exist in our internal
2195 * state. This should NEVER happen.
2197 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2198 node
= lttng_ht_iter_get_node_u64(&iter
);
2202 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2203 * in terms of destroying the associated channel, because the action that
2204 * causes the count to become 0 also causes a stream to be added. The
2205 * channel deletion will thus be triggered by the following removal of this
2208 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2209 /* Increment refcount before decrementing nb_init_stream_left */
2211 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2214 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2216 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
2217 &stream
->node_channel_id
);
2220 * Add stream to the stream_list_ht of the consumer data. No need to steal
2221 * the key since the HT does not use it and we allow to add redundant keys
2224 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2228 pthread_mutex_unlock(&stream
->lock
);
2229 pthread_mutex_unlock(&stream
->chan
->lock
);
2230 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2231 pthread_mutex_unlock(&consumer_data
.lock
);
2235 * Delete data stream that are flagged for deletion (endpoint_status).
2237 static void validate_endpoint_status_data_stream(void)
2239 struct lttng_ht_iter iter
;
2240 struct lttng_consumer_stream
*stream
;
2242 DBG("Consumer delete flagged data stream");
2245 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2246 /* Validate delete flag of the stream */
2247 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2250 /* Delete it right now */
2251 consumer_del_stream(stream
, data_ht
);
2257 * Delete metadata stream that are flagged for deletion (endpoint_status).
2259 static void validate_endpoint_status_metadata_stream(
2260 struct lttng_poll_event
*pollset
)
2262 struct lttng_ht_iter iter
;
2263 struct lttng_consumer_stream
*stream
;
2265 DBG("Consumer delete flagged metadata stream");
2270 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2271 /* Validate delete flag of the stream */
2272 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2276 * Remove from pollset so the metadata thread can continue without
2277 * blocking on a deleted stream.
2279 lttng_poll_del(pollset
, stream
->wait_fd
);
2281 /* Delete it right now */
2282 consumer_del_metadata_stream(stream
, metadata_ht
);
2288 * Thread polls on metadata file descriptor and write them on disk or on the
2291 void *consumer_thread_metadata_poll(void *data
)
2293 int ret
, i
, pollfd
, err
= -1;
2294 uint32_t revents
, nb_fd
;
2295 struct lttng_consumer_stream
*stream
= NULL
;
2296 struct lttng_ht_iter iter
;
2297 struct lttng_ht_node_u64
*node
;
2298 struct lttng_poll_event events
;
2299 struct lttng_consumer_local_data
*ctx
= data
;
2302 rcu_register_thread();
2304 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2306 if (testpoint(consumerd_thread_metadata
)) {
2307 goto error_testpoint
;
2310 health_code_update();
2312 DBG("Thread metadata poll started");
2314 /* Size is set to 1 for the consumer_metadata pipe */
2315 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2317 ERR("Poll set creation failed");
2321 ret
= lttng_poll_add(&events
,
2322 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2328 DBG("Metadata main loop started");
2332 health_code_update();
2333 health_poll_entry();
2334 DBG("Metadata poll wait");
2335 ret
= lttng_poll_wait(&events
, -1);
2336 DBG("Metadata poll return from wait with %d fd(s)",
2337 LTTNG_POLL_GETNB(&events
));
2339 DBG("Metadata event caught in thread");
2341 if (errno
== EINTR
) {
2342 ERR("Poll EINTR caught");
2345 if (LTTNG_POLL_GETNB(&events
) == 0) {
2346 err
= 0; /* All is OK */
2353 /* From here, the event is a metadata wait fd */
2354 for (i
= 0; i
< nb_fd
; i
++) {
2355 health_code_update();
2357 revents
= LTTNG_POLL_GETEV(&events
, i
);
2358 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2360 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2361 if (revents
& LPOLLIN
) {
2364 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2365 &stream
, sizeof(stream
));
2366 if (pipe_len
< sizeof(stream
)) {
2368 PERROR("read metadata stream");
2371 * Remove the pipe from the poll set and continue the loop
2372 * since their might be data to consume.
2374 lttng_poll_del(&events
,
2375 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2376 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2380 /* A NULL stream means that the state has changed. */
2381 if (stream
== NULL
) {
2382 /* Check for deleted streams. */
2383 validate_endpoint_status_metadata_stream(&events
);
2387 DBG("Adding metadata stream %d to poll set",
2390 /* Add metadata stream to the global poll events list */
2391 lttng_poll_add(&events
, stream
->wait_fd
,
2392 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2393 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2394 DBG("Metadata thread pipe hung up");
2396 * Remove the pipe from the poll set and continue the loop
2397 * since their might be data to consume.
2399 lttng_poll_del(&events
,
2400 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2401 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2404 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2408 /* Handle other stream */
2414 uint64_t tmp_id
= (uint64_t) pollfd
;
2416 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2418 node
= lttng_ht_iter_get_node_u64(&iter
);
2421 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2424 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2425 /* Get the data out of the metadata file descriptor */
2426 DBG("Metadata available on fd %d", pollfd
);
2427 assert(stream
->wait_fd
== pollfd
);
2430 health_code_update();
2432 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2434 * We don't check the return value here since if we get
2435 * a negative len, it means an error occurred thus we
2436 * simply remove it from the poll set and free the
2441 /* It's ok to have an unavailable sub-buffer */
2442 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2443 /* Clean up stream from consumer and free it. */
2444 lttng_poll_del(&events
, stream
->wait_fd
);
2445 consumer_del_metadata_stream(stream
, metadata_ht
);
2447 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2448 DBG("Metadata fd %d is hup|err.", pollfd
);
2449 if (!stream
->hangup_flush_done
2450 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2451 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2452 DBG("Attempting to flush and consume the UST buffers");
2453 lttng_ustconsumer_on_stream_hangup(stream
);
2455 /* We just flushed the stream now read it. */
2457 health_code_update();
2459 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2461 * We don't check the return value here since if we get
2462 * a negative len, it means an error occurred thus we
2463 * simply remove it from the poll set and free the
2469 lttng_poll_del(&events
, stream
->wait_fd
);
2471 * This call update the channel states, closes file descriptors
2472 * and securely free the stream.
2474 consumer_del_metadata_stream(stream
, metadata_ht
);
2476 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2480 /* Release RCU lock for the stream looked up */
2488 DBG("Metadata poll thread exiting");
2490 lttng_poll_clean(&events
);
2495 ERR("Health error occurred in %s", __func__
);
2497 health_unregister(health_consumerd
);
2498 rcu_unregister_thread();
2503 * This thread polls the fds in the set to consume the data and write
2504 * it to tracefile if necessary.
2506 void *consumer_thread_data_poll(void *data
)
2508 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2509 struct pollfd
*pollfd
= NULL
;
2510 /* local view of the streams */
2511 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2512 /* local view of consumer_data.fds_count */
2514 /* 2 for the consumer_data_pipe and wake up pipe */
2515 const int nb_pipes_fd
= 2;
2516 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2517 int nb_inactive_fd
= 0;
2518 struct lttng_consumer_local_data
*ctx
= data
;
2521 rcu_register_thread();
2523 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2525 if (testpoint(consumerd_thread_data
)) {
2526 goto error_testpoint
;
2529 health_code_update();
2531 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2532 if (local_stream
== NULL
) {
2533 PERROR("local_stream malloc");
2538 health_code_update();
2544 * the fds set has been updated, we need to update our
2545 * local array as well
2547 pthread_mutex_lock(&consumer_data
.lock
);
2548 if (consumer_data
.need_update
) {
2553 local_stream
= NULL
;
2555 /* Allocate for all fds */
2556 pollfd
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) * sizeof(struct pollfd
));
2557 if (pollfd
== NULL
) {
2558 PERROR("pollfd malloc");
2559 pthread_mutex_unlock(&consumer_data
.lock
);
2563 local_stream
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) *
2564 sizeof(struct lttng_consumer_stream
*));
2565 if (local_stream
== NULL
) {
2566 PERROR("local_stream malloc");
2567 pthread_mutex_unlock(&consumer_data
.lock
);
2570 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2571 data_ht
, &nb_inactive_fd
);
2573 ERR("Error in allocating pollfd or local_outfds");
2574 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2575 pthread_mutex_unlock(&consumer_data
.lock
);
2579 consumer_data
.need_update
= 0;
2581 pthread_mutex_unlock(&consumer_data
.lock
);
2583 /* No FDs and consumer_quit, consumer_cleanup the thread */
2584 if (nb_fd
== 0 && nb_inactive_fd
== 0 &&
2585 CMM_LOAD_SHARED(consumer_quit
) == 1) {
2586 err
= 0; /* All is OK */
2589 /* poll on the array of fds */
2591 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2592 if (testpoint(consumerd_thread_data_poll
)) {
2595 health_poll_entry();
2596 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2598 DBG("poll num_rdy : %d", num_rdy
);
2599 if (num_rdy
== -1) {
2601 * Restart interrupted system call.
2603 if (errno
== EINTR
) {
2606 PERROR("Poll error");
2607 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2609 } else if (num_rdy
== 0) {
2610 DBG("Polling thread timed out");
2614 if (caa_unlikely(data_consumption_paused
)) {
2615 DBG("Data consumption paused, sleeping...");
2621 * If the consumer_data_pipe triggered poll go directly to the
2622 * beginning of the loop to update the array. We want to prioritize
2623 * array update over low-priority reads.
2625 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2626 ssize_t pipe_readlen
;
2628 DBG("consumer_data_pipe wake up");
2629 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2630 &new_stream
, sizeof(new_stream
));
2631 if (pipe_readlen
< sizeof(new_stream
)) {
2632 PERROR("Consumer data pipe");
2633 /* Continue so we can at least handle the current stream(s). */
2638 * If the stream is NULL, just ignore it. It's also possible that
2639 * the sessiond poll thread changed the consumer_quit state and is
2640 * waking us up to test it.
2642 if (new_stream
== NULL
) {
2643 validate_endpoint_status_data_stream();
2647 /* Continue to update the local streams and handle prio ones */
2651 /* Handle wakeup pipe. */
2652 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2654 ssize_t pipe_readlen
;
2656 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2658 if (pipe_readlen
< 0) {
2659 PERROR("Consumer data wakeup pipe");
2661 /* We've been awakened to handle stream(s). */
2662 ctx
->has_wakeup
= 0;
2665 /* Take care of high priority channels first. */
2666 for (i
= 0; i
< nb_fd
; i
++) {
2667 health_code_update();
2669 if (local_stream
[i
] == NULL
) {
2672 if (pollfd
[i
].revents
& POLLPRI
) {
2673 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2675 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2676 /* it's ok to have an unavailable sub-buffer */
2677 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2678 /* Clean the stream and free it. */
2679 consumer_del_stream(local_stream
[i
], data_ht
);
2680 local_stream
[i
] = NULL
;
2681 } else if (len
> 0) {
2682 local_stream
[i
]->data_read
= 1;
2688 * If we read high prio channel in this loop, try again
2689 * for more high prio data.
2695 /* Take care of low priority channels. */
2696 for (i
= 0; i
< nb_fd
; i
++) {
2697 health_code_update();
2699 if (local_stream
[i
] == NULL
) {
2702 if ((pollfd
[i
].revents
& POLLIN
) ||
2703 local_stream
[i
]->hangup_flush_done
||
2704 local_stream
[i
]->has_data
) {
2705 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2706 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2707 /* it's ok to have an unavailable sub-buffer */
2708 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2709 /* Clean the stream and free it. */
2710 consumer_del_stream(local_stream
[i
], data_ht
);
2711 local_stream
[i
] = NULL
;
2712 } else if (len
> 0) {
2713 local_stream
[i
]->data_read
= 1;
2718 /* Handle hangup and errors */
2719 for (i
= 0; i
< nb_fd
; i
++) {
2720 health_code_update();
2722 if (local_stream
[i
] == NULL
) {
2725 if (!local_stream
[i
]->hangup_flush_done
2726 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2727 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2728 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2729 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2731 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2732 /* Attempt read again, for the data we just flushed. */
2733 local_stream
[i
]->data_read
= 1;
2736 * If the poll flag is HUP/ERR/NVAL and we have
2737 * read no data in this pass, we can remove the
2738 * stream from its hash table.
2740 if ((pollfd
[i
].revents
& POLLHUP
)) {
2741 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2742 if (!local_stream
[i
]->data_read
) {
2743 consumer_del_stream(local_stream
[i
], data_ht
);
2744 local_stream
[i
] = NULL
;
2747 } else if (pollfd
[i
].revents
& POLLERR
) {
2748 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2749 if (!local_stream
[i
]->data_read
) {
2750 consumer_del_stream(local_stream
[i
], data_ht
);
2751 local_stream
[i
] = NULL
;
2754 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2755 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2756 if (!local_stream
[i
]->data_read
) {
2757 consumer_del_stream(local_stream
[i
], data_ht
);
2758 local_stream
[i
] = NULL
;
2762 if (local_stream
[i
] != NULL
) {
2763 local_stream
[i
]->data_read
= 0;
2770 DBG("polling thread exiting");
2775 * Close the write side of the pipe so epoll_wait() in
2776 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2777 * read side of the pipe. If we close them both, epoll_wait strangely does
2778 * not return and could create a endless wait period if the pipe is the
2779 * only tracked fd in the poll set. The thread will take care of closing
2782 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2787 ERR("Health error occurred in %s", __func__
);
2789 health_unregister(health_consumerd
);
2791 rcu_unregister_thread();
2796 * Close wake-up end of each stream belonging to the channel. This will
2797 * allow the poll() on the stream read-side to detect when the
2798 * write-side (application) finally closes them.
2801 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2803 struct lttng_ht
*ht
;
2804 struct lttng_consumer_stream
*stream
;
2805 struct lttng_ht_iter iter
;
2807 ht
= consumer_data
.stream_per_chan_id_ht
;
2810 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2811 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2812 ht
->match_fct
, &channel
->key
,
2813 &iter
.iter
, stream
, node_channel_id
.node
) {
2815 * Protect against teardown with mutex.
2817 pthread_mutex_lock(&stream
->lock
);
2818 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2821 switch (consumer_data
.type
) {
2822 case LTTNG_CONSUMER_KERNEL
:
2824 case LTTNG_CONSUMER32_UST
:
2825 case LTTNG_CONSUMER64_UST
:
2826 if (stream
->metadata_flag
) {
2827 /* Safe and protected by the stream lock. */
2828 lttng_ustconsumer_close_metadata(stream
->chan
);
2831 * Note: a mutex is taken internally within
2832 * liblttng-ust-ctl to protect timer wakeup_fd
2833 * use from concurrent close.
2835 lttng_ustconsumer_close_stream_wakeup(stream
);
2839 ERR("Unknown consumer_data type");
2843 pthread_mutex_unlock(&stream
->lock
);
2848 static void destroy_channel_ht(struct lttng_ht
*ht
)
2850 struct lttng_ht_iter iter
;
2851 struct lttng_consumer_channel
*channel
;
2859 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2860 ret
= lttng_ht_del(ht
, &iter
);
2865 lttng_ht_destroy(ht
);
2869 * This thread polls the channel fds to detect when they are being
2870 * closed. It closes all related streams if the channel is detected as
2871 * closed. It is currently only used as a shim layer for UST because the
2872 * consumerd needs to keep the per-stream wakeup end of pipes open for
2875 void *consumer_thread_channel_poll(void *data
)
2877 int ret
, i
, pollfd
, err
= -1;
2878 uint32_t revents
, nb_fd
;
2879 struct lttng_consumer_channel
*chan
= NULL
;
2880 struct lttng_ht_iter iter
;
2881 struct lttng_ht_node_u64
*node
;
2882 struct lttng_poll_event events
;
2883 struct lttng_consumer_local_data
*ctx
= data
;
2884 struct lttng_ht
*channel_ht
;
2886 rcu_register_thread();
2888 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2890 if (testpoint(consumerd_thread_channel
)) {
2891 goto error_testpoint
;
2894 health_code_update();
2896 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2898 /* ENOMEM at this point. Better to bail out. */
2902 DBG("Thread channel poll started");
2904 /* Size is set to 1 for the consumer_channel pipe */
2905 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2907 ERR("Poll set creation failed");
2911 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2917 DBG("Channel main loop started");
2921 health_code_update();
2922 DBG("Channel poll wait");
2923 health_poll_entry();
2924 ret
= lttng_poll_wait(&events
, -1);
2925 DBG("Channel poll return from wait with %d fd(s)",
2926 LTTNG_POLL_GETNB(&events
));
2928 DBG("Channel event caught in thread");
2930 if (errno
== EINTR
) {
2931 ERR("Poll EINTR caught");
2934 if (LTTNG_POLL_GETNB(&events
) == 0) {
2935 err
= 0; /* All is OK */
2942 /* From here, the event is a channel wait fd */
2943 for (i
= 0; i
< nb_fd
; i
++) {
2944 health_code_update();
2946 revents
= LTTNG_POLL_GETEV(&events
, i
);
2947 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2949 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2950 if (revents
& LPOLLIN
) {
2951 enum consumer_channel_action action
;
2954 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2957 ERR("Error reading channel pipe");
2959 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2964 case CONSUMER_CHANNEL_ADD
:
2965 DBG("Adding channel %d to poll set",
2968 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2971 lttng_ht_add_unique_u64(channel_ht
,
2972 &chan
->wait_fd_node
);
2974 /* Add channel to the global poll events list */
2975 lttng_poll_add(&events
, chan
->wait_fd
,
2976 LPOLLERR
| LPOLLHUP
);
2978 case CONSUMER_CHANNEL_DEL
:
2981 * This command should never be called if the channel
2982 * has streams monitored by either the data or metadata
2983 * thread. The consumer only notify this thread with a
2984 * channel del. command if it receives a destroy
2985 * channel command from the session daemon that send it
2986 * if a command prior to the GET_CHANNEL failed.
2990 chan
= consumer_find_channel(key
);
2993 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2996 lttng_poll_del(&events
, chan
->wait_fd
);
2997 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2998 ret
= lttng_ht_del(channel_ht
, &iter
);
3001 switch (consumer_data
.type
) {
3002 case LTTNG_CONSUMER_KERNEL
:
3004 case LTTNG_CONSUMER32_UST
:
3005 case LTTNG_CONSUMER64_UST
:
3006 health_code_update();
3007 /* Destroy streams that might have been left in the stream list. */
3008 clean_channel_stream_list(chan
);
3011 ERR("Unknown consumer_data type");
3016 * Release our own refcount. Force channel deletion even if
3017 * streams were not initialized.
3019 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3020 consumer_del_channel(chan
);
3025 case CONSUMER_CHANNEL_QUIT
:
3027 * Remove the pipe from the poll set and continue the loop
3028 * since their might be data to consume.
3030 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3033 ERR("Unknown action");
3036 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3037 DBG("Channel thread pipe hung up");
3039 * Remove the pipe from the poll set and continue the loop
3040 * since their might be data to consume.
3042 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3045 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3049 /* Handle other stream */
3055 uint64_t tmp_id
= (uint64_t) pollfd
;
3057 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3059 node
= lttng_ht_iter_get_node_u64(&iter
);
3062 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
3065 /* Check for error event */
3066 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3067 DBG("Channel fd %d is hup|err.", pollfd
);
3069 lttng_poll_del(&events
, chan
->wait_fd
);
3070 ret
= lttng_ht_del(channel_ht
, &iter
);
3074 * This will close the wait fd for each stream associated to
3075 * this channel AND monitored by the data/metadata thread thus
3076 * will be clean by the right thread.
3078 consumer_close_channel_streams(chan
);
3080 /* Release our own refcount */
3081 if (!uatomic_sub_return(&chan
->refcount
, 1)
3082 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3083 consumer_del_channel(chan
);
3086 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3091 /* Release RCU lock for the channel looked up */
3099 lttng_poll_clean(&events
);
3101 destroy_channel_ht(channel_ht
);
3104 DBG("Channel poll thread exiting");
3107 ERR("Health error occurred in %s", __func__
);
3109 health_unregister(health_consumerd
);
3110 rcu_unregister_thread();
3114 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3115 struct pollfd
*sockpoll
, int client_socket
)
3122 ret
= lttng_consumer_poll_socket(sockpoll
);
3126 DBG("Metadata connection on client_socket");
3128 /* Blocking call, waiting for transmission */
3129 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3130 if (ctx
->consumer_metadata_socket
< 0) {
3131 WARN("On accept metadata");
3142 * This thread listens on the consumerd socket and receives the file
3143 * descriptors from the session daemon.
3145 void *consumer_thread_sessiond_poll(void *data
)
3147 int sock
= -1, client_socket
, ret
, err
= -1;
3149 * structure to poll for incoming data on communication socket avoids
3150 * making blocking sockets.
3152 struct pollfd consumer_sockpoll
[2];
3153 struct lttng_consumer_local_data
*ctx
= data
;
3155 rcu_register_thread();
3157 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3159 if (testpoint(consumerd_thread_sessiond
)) {
3160 goto error_testpoint
;
3163 health_code_update();
3165 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3166 unlink(ctx
->consumer_command_sock_path
);
3167 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3168 if (client_socket
< 0) {
3169 ERR("Cannot create command socket");
3173 ret
= lttcomm_listen_unix_sock(client_socket
);
3178 DBG("Sending ready command to lttng-sessiond");
3179 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3180 /* return < 0 on error, but == 0 is not fatal */
3182 ERR("Error sending ready command to lttng-sessiond");
3186 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3187 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3188 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3189 consumer_sockpoll
[1].fd
= client_socket
;
3190 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3192 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3200 DBG("Connection on client_socket");
3202 /* Blocking call, waiting for transmission */
3203 sock
= lttcomm_accept_unix_sock(client_socket
);
3210 * Setup metadata socket which is the second socket connection on the
3211 * command unix socket.
3213 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3222 /* This socket is not useful anymore. */
3223 ret
= close(client_socket
);
3225 PERROR("close client_socket");
3229 /* update the polling structure to poll on the established socket */
3230 consumer_sockpoll
[1].fd
= sock
;
3231 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3234 health_code_update();
3236 health_poll_entry();
3237 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3246 DBG("Incoming command on sock");
3247 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3250 * This could simply be a session daemon quitting. Don't output
3253 DBG("Communication interrupted on command socket");
3257 if (CMM_LOAD_SHARED(consumer_quit
)) {
3258 DBG("consumer_thread_receive_fds received quit from signal");
3259 err
= 0; /* All is OK */
3262 DBG("received command on sock");
3268 DBG("Consumer thread sessiond poll exiting");
3271 * Close metadata streams since the producer is the session daemon which
3274 * NOTE: for now, this only applies to the UST tracer.
3276 lttng_consumer_close_all_metadata();
3279 * when all fds have hung up, the polling thread
3282 CMM_STORE_SHARED(consumer_quit
, 1);
3285 * Notify the data poll thread to poll back again and test the
3286 * consumer_quit state that we just set so to quit gracefully.
3288 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3290 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3292 notify_health_quit_pipe(health_quit_pipe
);
3294 /* Cleaning up possibly open sockets. */
3298 PERROR("close sock sessiond poll");
3301 if (client_socket
>= 0) {
3302 ret
= close(client_socket
);
3304 PERROR("close client_socket sessiond poll");
3311 ERR("Health error occurred in %s", __func__
);
3313 health_unregister(health_consumerd
);
3315 rcu_unregister_thread();
3320 int consumer_flush_buffer(struct lttng_consumer_stream
*stream
,
3321 int producer_active
)
3325 switch (consumer_data
.type
) {
3326 case LTTNG_CONSUMER_KERNEL
:
3327 if (producer_active
) {
3328 ret
= kernctl_buffer_flush(stream
->wait_fd
);
3330 ERR("Failed to flush kernel stream");
3334 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
3337 * Doing a buffer flush which does not take into
3338 * account empty packets. This is not perfect,
3339 * but required as a fall-back when
3340 * "flush_empty" is not implemented by
3343 ret
= kernctl_buffer_flush(stream
->wait_fd
);
3345 ERR("Failed to flush kernel stream");
3351 case LTTNG_CONSUMER32_UST
:
3352 case LTTNG_CONSUMER64_UST
:
3353 lttng_ustconsumer_flush_buffer(stream
, producer_active
);
3356 ERR("Unknown consumer_data type");
3364 static enum open_packet_status
open_packet(struct lttng_consumer_stream
*stream
)
3367 enum open_packet_status status
;
3368 unsigned long produced_pos_before
, produced_pos_after
;
3370 ret
= lttng_consumer_sample_snapshot_positions(stream
);
3372 ERR("Failed to snapshot positions before post-rotation empty packet flush: stream id = %" PRIu64
3373 ", channel name = %s, session id = %" PRIu64
,
3374 stream
->key
, stream
->chan
->name
,
3375 stream
->chan
->session_id
);
3376 status
= OPEN_PACKET_STATUS_ERROR
;
3380 ret
= lttng_consumer_get_produced_snapshot(
3381 stream
, &produced_pos_before
);
3383 ERR("Failed to read produced position before post-rotation empty packet flush: stream id = %" PRIu64
3384 ", channel name = %s, session id = %" PRIu64
,
3385 stream
->key
, stream
->chan
->name
,
3386 stream
->chan
->session_id
);
3387 status
= OPEN_PACKET_STATUS_ERROR
;
3391 ret
= consumer_flush_buffer(stream
, 0);
3393 ERR("Failed to flush an empty packet at rotation point: stream id = %" PRIu64
3394 ", channel name = %s, session id = %" PRIu64
,
3395 stream
->key
, stream
->chan
->name
,
3396 stream
->chan
->session_id
);
3397 status
= OPEN_PACKET_STATUS_ERROR
;
3401 ret
= lttng_consumer_sample_snapshot_positions(stream
);
3403 ERR("Failed to snapshot positions after post-rotation empty packet flush: stream id = %" PRIu64
3404 ", channel name = %s, session id = %" PRIu64
,
3405 stream
->key
, stream
->chan
->name
,
3406 stream
->chan
->session_id
);
3407 status
= OPEN_PACKET_STATUS_ERROR
;
3411 ret
= lttng_consumer_get_produced_snapshot(stream
, &produced_pos_after
);
3413 ERR("Failed to read produced position after post-rotation empty packet flush: stream id = %" PRIu64
3414 ", channel name = %s, session id = %" PRIu64
,
3415 stream
->key
, stream
->chan
->name
,
3416 stream
->chan
->session_id
);
3417 status
= OPEN_PACKET_STATUS_ERROR
;
3422 * Determine if the flush had an effect by comparing the produced
3423 * positons before and after the flush.
3425 status
= produced_pos_before
!= produced_pos_after
?
3426 OPEN_PACKET_STATUS_OPENED
:
3427 OPEN_PACKET_STATUS_NO_SPACE
;
3428 if (status
== OPEN_PACKET_STATUS_OPENED
) {
3429 stream
->opened_packet_in_current_trace_chunk
= true;
3435 static bool stream_is_rotating_to_null_chunk(
3436 const struct lttng_consumer_stream
*stream
)
3438 bool rotating_to_null_chunk
= false;
3440 if (stream
->rotate_position
== -1ULL) {
3441 /* No rotation ongoing. */
3445 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
||
3446 !stream
->chan
->trace_chunk
) {
3447 rotating_to_null_chunk
= true;
3450 return rotating_to_null_chunk
;
3453 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3454 struct lttng_consumer_local_data
*ctx
,
3455 bool locked_by_caller
)
3457 ssize_t ret
, written_bytes
= 0;
3459 struct stream_subbuffer subbuffer
= {};
3461 if (!locked_by_caller
) {
3462 stream
->read_subbuffer_ops
.lock(stream
);
3465 if (stream
->read_subbuffer_ops
.on_wake_up
) {
3466 ret
= stream
->read_subbuffer_ops
.on_wake_up(stream
);
3473 * If the stream was flagged to be ready for rotation before we extract
3474 * the next packet, rotate it now.
3476 if (stream
->rotate_ready
) {
3477 DBG("Rotate stream before consuming data");
3478 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3480 ERR("Stream rotation error before consuming data");
3485 ret
= stream
->read_subbuffer_ops
.get_next_subbuffer(stream
, &subbuffer
);
3487 if (ret
== -ENODATA
) {
3495 ret
= stream
->read_subbuffer_ops
.pre_consume_subbuffer(
3496 stream
, &subbuffer
);
3498 goto error_put_subbuf
;
3501 written_bytes
= stream
->read_subbuffer_ops
.consume_subbuffer(
3502 ctx
, stream
, &subbuffer
);
3504 * Should write subbuf_size amount of data when network streaming or
3505 * the full padded size when we are not streaming.
3507 if ((written_bytes
!= subbuffer
.info
.data
.subbuf_size
&&
3508 stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
3509 (written_bytes
!= subbuffer
.info
.data
.padded_subbuf_size
&&
3510 stream
->net_seq_idx
==
3511 (uint64_t) -1ULL)) {
3513 * Display the error but continue processing to try to
3514 * release the subbuffer. This is a DBG statement
3515 * since this can happen without being a critical
3518 DBG("Failed to write to tracefile (written_bytes: %zd != padded subbuffer size: %lu, subbuffer size: %lu)",
3519 written_bytes
, subbuffer
.info
.data
.padded_subbuf_size
,
3520 subbuffer
.info
.data
.subbuf_size
);
3523 ret
= stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3528 if (stream
->read_subbuffer_ops
.post_consume
) {
3529 ret
= stream
->read_subbuffer_ops
.post_consume(stream
, &subbuffer
, ctx
);
3536 * After extracting the packet, we check if the stream is now ready to
3537 * be rotated and perform the action immediately.
3539 * Don't overwrite `ret` as callers expect the number of bytes
3540 * consumed to be returned on success.
3542 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
3543 if (rotation_ret
== 1) {
3544 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3545 if (rotation_ret
< 0) {
3547 ERR("Stream rotation error after consuming data");
3550 } else if (rotation_ret
< 0) {
3552 ERR("Failed to check if stream was ready to rotate after consuming data");
3557 * TODO roll into a post_consume op as this doesn't apply to metadata
3560 if (!stream
->opened_packet_in_current_trace_chunk
&&
3561 stream
->trace_chunk
&& !stream
->metadata_flag
&&
3562 !stream_is_rotating_to_null_chunk(stream
)) {
3563 const enum open_packet_status status
= open_packet(stream
);
3566 case OPEN_PACKET_STATUS_OPENED
:
3567 DBG("Opened a packet after consuming a packet rotation: stream id = %" PRIu64
3568 ", channel name = %s, session id = %" PRIu64
,
3569 stream
->key
, stream
->chan
->name
,
3570 stream
->chan
->session_id
);
3572 case OPEN_PACKET_STATUS_NO_SPACE
:
3574 * Can't open a packet as there is no space left.
3575 * This means that new events were produced, resulting
3576 * in a packet being opened, which is what we wanted
3579 DBG("No space left to open a packet after consuming a packet: stream id = %" PRIu64
3580 ", channel name = %s, session id = %" PRIu64
,
3581 stream
->key
, stream
->chan
->name
,
3582 stream
->chan
->session_id
);
3583 stream
->opened_packet_in_current_trace_chunk
= true;
3585 case OPEN_PACKET_STATUS_ERROR
:
3586 /* Logged by callee. */
3595 if (stream
->read_subbuffer_ops
.on_sleep
) {
3596 stream
->read_subbuffer_ops
.on_sleep(stream
, ctx
);
3599 ret
= written_bytes
;
3601 if (!locked_by_caller
) {
3602 stream
->read_subbuffer_ops
.unlock(stream
);
3607 (void) stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3611 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3613 switch (consumer_data
.type
) {
3614 case LTTNG_CONSUMER_KERNEL
:
3615 return lttng_kconsumer_on_recv_stream(stream
);
3616 case LTTNG_CONSUMER32_UST
:
3617 case LTTNG_CONSUMER64_UST
:
3618 return lttng_ustconsumer_on_recv_stream(stream
);
3620 ERR("Unknown consumer_data type");
3627 * Allocate and set consumer data hash tables.
3629 int lttng_consumer_init(void)
3631 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3632 if (!consumer_data
.channel_ht
) {
3636 consumer_data
.channels_by_session_id_ht
=
3637 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3638 if (!consumer_data
.channels_by_session_id_ht
) {
3642 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3643 if (!consumer_data
.relayd_ht
) {
3647 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3648 if (!consumer_data
.stream_list_ht
) {
3652 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3653 if (!consumer_data
.stream_per_chan_id_ht
) {
3657 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3662 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3667 consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3668 if (!consumer_data
.chunk_registry
) {
3679 * Process the ADD_RELAYD command receive by a consumer.
3681 * This will create a relayd socket pair and add it to the relayd hash table.
3682 * The caller MUST acquire a RCU read side lock before calling it.
3684 void consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3685 struct lttng_consumer_local_data
*ctx
, int sock
,
3686 struct pollfd
*consumer_sockpoll
,
3687 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3688 uint64_t relayd_session_id
)
3690 int fd
= -1, ret
= -1, relayd_created
= 0;
3691 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3692 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3695 assert(relayd_sock
);
3697 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3699 /* Get relayd reference if exists. */
3700 relayd
= consumer_find_relayd(net_seq_idx
);
3701 if (relayd
== NULL
) {
3702 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3703 /* Not found. Allocate one. */
3704 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3705 if (relayd
== NULL
) {
3706 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3709 relayd
->sessiond_session_id
= sessiond_id
;
3714 * This code path MUST continue to the consumer send status message to
3715 * we can notify the session daemon and continue our work without
3716 * killing everything.
3720 * relayd key should never be found for control socket.
3722 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3725 /* First send a status message before receiving the fds. */
3726 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3728 /* Somehow, the session daemon is not responding anymore. */
3729 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3730 goto error_nosignal
;
3733 /* Poll on consumer socket. */
3734 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3736 /* Needing to exit in the middle of a command: error. */
3737 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3738 goto error_nosignal
;
3741 /* Get relayd socket from session daemon */
3742 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3743 if (ret
!= sizeof(fd
)) {
3744 fd
= -1; /* Just in case it gets set with an invalid value. */
3747 * Failing to receive FDs might indicate a major problem such as
3748 * reaching a fd limit during the receive where the kernel returns a
3749 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3750 * don't take any chances and stop everything.
3752 * XXX: Feature request #558 will fix that and avoid this possible
3753 * issue when reaching the fd limit.
3755 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3756 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3760 /* Copy socket information and received FD */
3761 switch (sock_type
) {
3762 case LTTNG_STREAM_CONTROL
:
3763 /* Copy received lttcomm socket */
3764 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3765 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3766 /* Handle create_sock error. */
3768 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3772 * Close the socket created internally by
3773 * lttcomm_create_sock, so we can replace it by the one
3774 * received from sessiond.
3776 if (close(relayd
->control_sock
.sock
.fd
)) {
3780 /* Assign new file descriptor */
3781 relayd
->control_sock
.sock
.fd
= fd
;
3782 /* Assign version values. */
3783 relayd
->control_sock
.major
= relayd_sock
->major
;
3784 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3786 relayd
->relayd_session_id
= relayd_session_id
;
3789 case LTTNG_STREAM_DATA
:
3790 /* Copy received lttcomm socket */
3791 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3792 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3793 /* Handle create_sock error. */
3795 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3799 * Close the socket created internally by
3800 * lttcomm_create_sock, so we can replace it by the one
3801 * received from sessiond.
3803 if (close(relayd
->data_sock
.sock
.fd
)) {
3807 /* Assign new file descriptor */
3808 relayd
->data_sock
.sock
.fd
= fd
;
3809 /* Assign version values. */
3810 relayd
->data_sock
.major
= relayd_sock
->major
;
3811 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3814 ERR("Unknown relayd socket type (%d)", sock_type
);
3815 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3819 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3820 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3821 relayd
->net_seq_idx
, fd
);
3823 * We gave the ownership of the fd to the relayd structure. Set the
3824 * fd to -1 so we don't call close() on it in the error path below.
3828 /* We successfully added the socket. Send status back. */
3829 ret
= consumer_send_status_msg(sock
, ret_code
);
3831 /* Somehow, the session daemon is not responding anymore. */
3832 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3833 goto error_nosignal
;
3837 * Add relayd socket pair to consumer data hashtable. If object already
3838 * exists or on error, the function gracefully returns.
3847 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3848 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3852 /* Close received socket if valid. */
3855 PERROR("close received socket");
3859 if (relayd_created
) {
3865 * Search for a relayd associated to the session id and return the reference.
3867 * A rcu read side lock MUST be acquire before calling this function and locked
3868 * until the relayd object is no longer necessary.
3870 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3872 struct lttng_ht_iter iter
;
3873 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3875 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3876 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3879 * Check by sessiond id which is unique here where the relayd session
3880 * id might not be when having multiple relayd.
3882 if (relayd
->sessiond_session_id
== id
) {
3883 /* Found the relayd. There can be only one per id. */
3895 * Check if for a given session id there is still data needed to be extract
3898 * Return 1 if data is pending or else 0 meaning ready to be read.
3900 int consumer_data_pending(uint64_t id
)
3903 struct lttng_ht_iter iter
;
3904 struct lttng_ht
*ht
;
3905 struct lttng_consumer_stream
*stream
;
3906 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3907 int (*data_pending
)(struct lttng_consumer_stream
*);
3909 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3912 pthread_mutex_lock(&consumer_data
.lock
);
3914 switch (consumer_data
.type
) {
3915 case LTTNG_CONSUMER_KERNEL
:
3916 data_pending
= lttng_kconsumer_data_pending
;
3918 case LTTNG_CONSUMER32_UST
:
3919 case LTTNG_CONSUMER64_UST
:
3920 data_pending
= lttng_ustconsumer_data_pending
;
3923 ERR("Unknown consumer data type");
3927 /* Ease our life a bit */
3928 ht
= consumer_data
.stream_list_ht
;
3930 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3931 ht
->hash_fct(&id
, lttng_ht_seed
),
3933 &iter
.iter
, stream
, node_session_id
.node
) {
3934 pthread_mutex_lock(&stream
->lock
);
3937 * A removed node from the hash table indicates that the stream has
3938 * been deleted thus having a guarantee that the buffers are closed
3939 * on the consumer side. However, data can still be transmitted
3940 * over the network so don't skip the relayd check.
3942 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3944 /* Check the stream if there is data in the buffers. */
3945 ret
= data_pending(stream
);
3947 pthread_mutex_unlock(&stream
->lock
);
3952 pthread_mutex_unlock(&stream
->lock
);
3955 relayd
= find_relayd_by_session_id(id
);
3957 unsigned int is_data_inflight
= 0;
3959 /* Send init command for data pending. */
3960 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3961 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3962 relayd
->relayd_session_id
);
3964 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3965 /* Communication error thus the relayd so no data pending. */
3966 goto data_not_pending
;
3969 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3970 ht
->hash_fct(&id
, lttng_ht_seed
),
3972 &iter
.iter
, stream
, node_session_id
.node
) {
3973 if (stream
->metadata_flag
) {
3974 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3975 stream
->relayd_stream_id
);
3977 ret
= relayd_data_pending(&relayd
->control_sock
,
3978 stream
->relayd_stream_id
,
3979 stream
->next_net_seq_num
- 1);
3983 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3985 } else if (ret
< 0) {
3986 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3987 lttng_consumer_cleanup_relayd(relayd
);
3988 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3989 goto data_not_pending
;
3993 /* Send end command for data pending. */
3994 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3995 relayd
->relayd_session_id
, &is_data_inflight
);
3996 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3998 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3999 lttng_consumer_cleanup_relayd(relayd
);
4000 goto data_not_pending
;
4002 if (is_data_inflight
) {
4008 * Finding _no_ node in the hash table and no inflight data means that the
4009 * stream(s) have been removed thus data is guaranteed to be available for
4010 * analysis from the trace files.
4014 /* Data is available to be read by a viewer. */
4015 pthread_mutex_unlock(&consumer_data
.lock
);
4020 /* Data is still being extracted from buffers. */
4021 pthread_mutex_unlock(&consumer_data
.lock
);
4027 * Send a ret code status message to the sessiond daemon.
4029 * Return the sendmsg() return value.
4031 int consumer_send_status_msg(int sock
, int ret_code
)
4033 struct lttcomm_consumer_status_msg msg
;
4035 memset(&msg
, 0, sizeof(msg
));
4036 msg
.ret_code
= ret_code
;
4038 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
4042 * Send a channel status message to the sessiond daemon.
4044 * Return the sendmsg() return value.
4046 int consumer_send_status_channel(int sock
,
4047 struct lttng_consumer_channel
*channel
)
4049 struct lttcomm_consumer_status_channel msg
;
4053 memset(&msg
, 0, sizeof(msg
));
4055 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
4057 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4058 msg
.key
= channel
->key
;
4059 msg
.stream_count
= channel
->streams
.count
;
4062 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
4065 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
4066 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
4067 uint64_t max_sb_size
)
4069 unsigned long start_pos
;
4071 if (!nb_packets_per_stream
) {
4072 return consumed_pos
; /* Grab everything */
4074 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
4075 start_pos
-= max_sb_size
* nb_packets_per_stream
;
4076 if ((long) (start_pos
- consumed_pos
) < 0) {
4077 return consumed_pos
; /* Grab everything */
4083 * Sample the rotate position for all the streams of a channel. If a stream
4084 * is already at the rotate position (produced == consumed), we flag it as
4085 * ready for rotation. The rotation of ready streams occurs after we have
4086 * replied to the session daemon that we have finished sampling the positions.
4087 * Must be called with RCU read-side lock held to ensure existence of channel.
4089 * Returns 0 on success, < 0 on error
4091 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
4092 uint64_t key
, uint64_t relayd_id
, uint32_t metadata
,
4093 struct lttng_consumer_local_data
*ctx
)
4096 struct lttng_consumer_stream
*stream
;
4097 struct lttng_ht_iter iter
;
4098 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
4099 struct lttng_dynamic_array stream_rotation_positions
;
4100 uint64_t next_chunk_id
, stream_count
= 0;
4101 enum lttng_trace_chunk_status chunk_status
;
4102 const bool is_local_trace
= relayd_id
== -1ULL;
4103 struct consumer_relayd_sock_pair
*relayd
= NULL
;
4104 bool rotating_to_new_chunk
= true;
4105 /* Array of `struct lttng_consumer_stream *` */
4106 struct lttng_dynamic_pointer_array streams_packet_to_open
;
4109 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
4111 lttng_dynamic_array_init(&stream_rotation_positions
,
4112 sizeof(struct relayd_stream_rotation_position
), NULL
);
4113 lttng_dynamic_pointer_array_init(&streams_packet_to_open
, NULL
);
4117 pthread_mutex_lock(&channel
->lock
);
4118 assert(channel
->trace_chunk
);
4119 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
,
4121 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4123 goto end_unlock_channel
;
4126 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4127 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4128 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4129 stream
, node_channel_id
.node
) {
4130 unsigned long produced_pos
= 0, consumed_pos
= 0;
4132 health_code_update();
4135 * Lock stream because we are about to change its state.
4137 pthread_mutex_lock(&stream
->lock
);
4139 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
4140 rotating_to_new_chunk
= false;
4144 * Do not flush an empty packet when rotating from a NULL trace
4145 * chunk. The stream has no means to output data, and the prior
4146 * rotation which rotated to NULL performed that side-effect already.
4148 if (stream
->trace_chunk
) {
4150 * For metadata stream, do an active flush, which does not
4151 * produce empty packets. For data streams, empty-flush;
4152 * ensures we have at least one packet in each stream per trace
4153 * chunk, even if no data was produced.
4155 ret
= consumer_flush_buffer(stream
, stream
->metadata_flag
? 1 : 0);
4157 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4159 goto end_unlock_stream
;
4163 ret
= lttng_consumer_take_snapshot(stream
);
4164 if (ret
< 0 && ret
!= -ENODATA
&& ret
!= -EAGAIN
) {
4165 ERR("Failed to sample snapshot position during channel rotation");
4166 goto end_unlock_stream
;
4169 ret
= lttng_consumer_get_produced_snapshot(stream
,
4172 ERR("Failed to sample produced position during channel rotation");
4173 goto end_unlock_stream
;
4176 ret
= lttng_consumer_get_consumed_snapshot(stream
,
4179 ERR("Failed to sample consumed position during channel rotation");
4180 goto end_unlock_stream
;
4184 * Align produced position on the start-of-packet boundary of the first
4185 * packet going into the next trace chunk.
4187 produced_pos
= ALIGN_FLOOR(produced_pos
, stream
->max_sb_size
);
4188 if (consumed_pos
== produced_pos
) {
4189 DBG("Set rotate ready for stream %" PRIu64
" produced = %lu consumed = %lu",
4190 stream
->key
, produced_pos
, consumed_pos
);
4191 stream
->rotate_ready
= true;
4193 DBG("Different consumed and produced positions "
4194 "for stream %" PRIu64
" produced = %lu consumed = %lu",
4195 stream
->key
, produced_pos
, consumed_pos
);
4198 * The rotation position is based on the packet_seq_num of the
4199 * packet following the last packet that was consumed for this
4200 * stream, incremented by the offset between produced and
4201 * consumed positions. This rotation position is a lower bound
4202 * (inclusive) at which the next trace chunk starts. Since it
4203 * is a lower bound, it is OK if the packet_seq_num does not
4204 * correspond exactly to the same packet identified by the
4205 * consumed_pos, which can happen in overwrite mode.
4207 if (stream
->sequence_number_unavailable
) {
4209 * Rotation should never be performed on a session which
4210 * interacts with a pre-2.8 lttng-modules, which does
4211 * not implement packet sequence number.
4213 ERR("Failure to rotate stream %" PRIu64
": sequence number unavailable",
4216 goto end_unlock_stream
;
4218 stream
->rotate_position
= stream
->last_sequence_number
+ 1 +
4219 ((produced_pos
- consumed_pos
) / stream
->max_sb_size
);
4220 DBG("Set rotation position for stream %" PRIu64
" at position %" PRIu64
,
4221 stream
->key
, stream
->rotate_position
);
4223 if (!is_local_trace
) {
4225 * The relay daemon control protocol expects a rotation
4226 * position as "the sequence number of the first packet
4227 * _after_ the current trace chunk".
4229 const struct relayd_stream_rotation_position position
= {
4230 .stream_id
= stream
->relayd_stream_id
,
4231 .rotate_at_seq_num
= stream
->rotate_position
,
4234 ret
= lttng_dynamic_array_add_element(
4235 &stream_rotation_positions
,
4238 ERR("Failed to allocate stream rotation position");
4239 goto end_unlock_stream
;
4244 stream
->opened_packet_in_current_trace_chunk
= false;
4246 if (rotating_to_new_chunk
&& !stream
->metadata_flag
) {
4248 * Attempt to flush an empty packet as close to the
4249 * rotation point as possible. In the event where a
4250 * stream remains inactive after the rotation point,
4251 * this ensures that the new trace chunk has a
4252 * beginning timestamp set at the begining of the
4253 * trace chunk instead of only creating an empty
4254 * packet when the trace chunk is stopped.
4256 * This indicates to the viewers that the stream
4257 * was being recorded, but more importantly it
4258 * allows viewers to determine a useable trace
4261 * This presents a problem in the case where the
4262 * ring-buffer is completely full.
4264 * Consider the following scenario:
4265 * - The consumption of data is slow (slow network,
4267 * - The ring buffer is full,
4268 * - A rotation is initiated,
4269 * - The flush below does nothing (no space left to
4270 * open a new packet),
4271 * - The other streams rotate very soon, and new
4272 * data is produced in the new chunk,
4273 * - This stream completes its rotation long after the
4274 * rotation was initiated
4275 * - The session is stopped before any event can be
4276 * produced in this stream's buffers.
4278 * The resulting trace chunk will have a single packet
4279 * temporaly at the end of the trace chunk for this
4280 * stream making the stream intersection more narrow
4281 * than it should be.
4283 * To work-around this, an empty flush is performed
4284 * after the first consumption of a packet during a
4285 * rotation if open_packet fails. The idea is that
4286 * consuming a packet frees enough space to switch
4287 * packets in this scenario and allows the tracer to
4288 * "stamp" the beginning of the new trace chunk at the
4289 * earliest possible point.
4291 * The packet open is performed after the channel
4292 * rotation to ensure that no attempt to open a packet
4293 * is performed in a stream that has no active trace
4296 ret
= lttng_dynamic_pointer_array_add_pointer(
4297 &streams_packet_to_open
, stream
);
4299 PERROR("Failed to add a stream pointer to array of streams in which to open a packet");
4301 goto end_unlock_stream
;
4305 pthread_mutex_unlock(&stream
->lock
);
4309 if (!is_local_trace
) {
4310 relayd
= consumer_find_relayd(relayd_id
);
4312 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4314 goto end_unlock_channel
;
4317 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4318 ret
= relayd_rotate_streams(&relayd
->control_sock
, stream_count
,
4319 rotating_to_new_chunk
? &next_chunk_id
: NULL
,
4320 (const struct relayd_stream_rotation_position
*)
4321 stream_rotation_positions
.buffer
4323 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4325 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4326 relayd
->net_seq_idx
);
4327 lttng_consumer_cleanup_relayd(relayd
);
4328 goto end_unlock_channel
;
4332 for (stream_idx
= 0;
4333 stream_idx
< lttng_dynamic_pointer_array_get_count(
4334 &streams_packet_to_open
);
4336 enum open_packet_status status
;
4338 stream
= lttng_dynamic_pointer_array_get_pointer(
4339 &streams_packet_to_open
, stream_idx
);
4341 pthread_mutex_lock(&stream
->lock
);
4342 status
= open_packet(stream
);
4343 pthread_mutex_unlock(&stream
->lock
);
4345 case OPEN_PACKET_STATUS_OPENED
:
4346 DBG("Opened a packet after a rotation: stream id = %" PRIu64
4347 ", channel name = %s, session id = %" PRIu64
,
4348 stream
->key
, stream
->chan
->name
,
4349 stream
->chan
->session_id
);
4351 case OPEN_PACKET_STATUS_NO_SPACE
:
4353 * Can't open a packet as there is no space left
4354 * in the buffer. A new packet will be opened
4355 * once one has been consumed.
4357 DBG("No space left to open a packet after a rotation: stream id = %" PRIu64
4358 ", channel name = %s, session id = %" PRIu64
,
4359 stream
->key
, stream
->chan
->name
,
4360 stream
->chan
->session_id
);
4362 case OPEN_PACKET_STATUS_ERROR
:
4363 /* Logged by callee. */
4365 goto end_unlock_stream
;
4371 pthread_mutex_unlock(&channel
->lock
);
4376 pthread_mutex_unlock(&stream
->lock
);
4378 pthread_mutex_unlock(&channel
->lock
);
4381 lttng_dynamic_array_reset(&stream_rotation_positions
);
4382 lttng_dynamic_pointer_array_reset(&streams_packet_to_open
);
4387 int consumer_clear_buffer(struct lttng_consumer_stream
*stream
)
4390 unsigned long consumed_pos_before
, consumed_pos_after
;
4392 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4394 ERR("Taking snapshot positions");
4398 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_before
);
4400 ERR("Consumed snapshot position");
4404 switch (consumer_data
.type
) {
4405 case LTTNG_CONSUMER_KERNEL
:
4406 ret
= kernctl_buffer_clear(stream
->wait_fd
);
4408 ERR("Failed to clear kernel stream (ret = %d)", ret
);
4412 case LTTNG_CONSUMER32_UST
:
4413 case LTTNG_CONSUMER64_UST
:
4414 lttng_ustconsumer_clear_buffer(stream
);
4417 ERR("Unknown consumer_data type");
4421 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4423 ERR("Taking snapshot positions");
4426 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_after
);
4428 ERR("Consumed snapshot position");
4431 DBG("clear: before: %lu after: %lu", consumed_pos_before
, consumed_pos_after
);
4437 int consumer_clear_stream(struct lttng_consumer_stream
*stream
)
4441 ret
= consumer_flush_buffer(stream
, 1);
4443 ERR("Failed to flush stream %" PRIu64
" during channel clear",
4445 ret
= LTTCOMM_CONSUMERD_FATAL
;
4449 ret
= consumer_clear_buffer(stream
);
4451 ERR("Failed to clear stream %" PRIu64
" during channel clear",
4453 ret
= LTTCOMM_CONSUMERD_FATAL
;
4457 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4463 int consumer_clear_unmonitored_channel(struct lttng_consumer_channel
*channel
)
4466 struct lttng_consumer_stream
*stream
;
4469 pthread_mutex_lock(&channel
->lock
);
4470 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
4471 health_code_update();
4472 pthread_mutex_lock(&stream
->lock
);
4473 ret
= consumer_clear_stream(stream
);
4477 pthread_mutex_unlock(&stream
->lock
);
4479 pthread_mutex_unlock(&channel
->lock
);
4484 pthread_mutex_unlock(&stream
->lock
);
4485 pthread_mutex_unlock(&channel
->lock
);
4491 * Check if a stream is ready to be rotated after extracting it.
4493 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4494 * error. Stream lock must be held.
4496 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4498 DBG("Check is rotate ready for stream %" PRIu64
4499 " ready %u rotate_position %" PRIu64
4500 " last_sequence_number %" PRIu64
,
4501 stream
->key
, stream
->rotate_ready
,
4502 stream
->rotate_position
, stream
->last_sequence_number
);
4503 if (stream
->rotate_ready
) {
4508 * If packet seq num is unavailable, it means we are interacting
4509 * with a pre-2.8 lttng-modules which does not implement the
4510 * sequence number. Rotation should never be used by sessiond in this
4513 if (stream
->sequence_number_unavailable
) {
4514 ERR("Internal error: rotation used on stream %" PRIu64
4515 " with unavailable sequence number",
4520 if (stream
->rotate_position
== -1ULL ||
4521 stream
->last_sequence_number
== -1ULL) {
4526 * Rotate position not reached yet. The stream rotate position is
4527 * the position of the next packet belonging to the next trace chunk,
4528 * but consumerd considers rotation ready when reaching the last
4529 * packet of the current chunk, hence the "rotate_position - 1".
4532 DBG("Check is rotate ready for stream %" PRIu64
4533 " last_sequence_number %" PRIu64
4534 " rotate_position %" PRIu64
,
4535 stream
->key
, stream
->last_sequence_number
,
4536 stream
->rotate_position
);
4537 if (stream
->last_sequence_number
>= stream
->rotate_position
- 1) {
4545 * Reset the state for a stream after a rotation occurred.
4547 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4549 DBG("lttng_consumer_reset_stream_rotate_state for stream %" PRIu64
,
4551 stream
->rotate_position
= -1ULL;
4552 stream
->rotate_ready
= false;
4556 * Perform the rotation a local stream file.
4559 int rotate_local_stream(struct lttng_consumer_local_data
*ctx
,
4560 struct lttng_consumer_stream
*stream
)
4564 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4567 stream
->tracefile_size_current
= 0;
4568 stream
->tracefile_count_current
= 0;
4570 if (stream
->out_fd
>= 0) {
4571 ret
= close(stream
->out_fd
);
4573 PERROR("Failed to close stream out_fd of channel \"%s\"",
4574 stream
->chan
->name
);
4576 stream
->out_fd
= -1;
4579 if (stream
->index_file
) {
4580 lttng_index_file_put(stream
->index_file
);
4581 stream
->index_file
= NULL
;
4584 if (!stream
->trace_chunk
) {
4588 ret
= consumer_stream_create_output_files(stream
, true);
4594 * Performs the stream rotation for the rotate session feature if needed.
4595 * It must be called with the channel and stream locks held.
4597 * Return 0 on success, a negative number of error.
4599 int lttng_consumer_rotate_stream(struct lttng_consumer_local_data
*ctx
,
4600 struct lttng_consumer_stream
*stream
)
4604 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4607 * Update the stream's 'current' chunk to the session's (channel)
4608 * now-current chunk.
4610 lttng_trace_chunk_put(stream
->trace_chunk
);
4611 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4613 * A channel can be rotated and not have a "next" chunk
4614 * to transition to. In that case, the channel's "current chunk"
4615 * has not been closed yet, but it has not been updated to
4616 * a "next" trace chunk either. Hence, the stream, like its
4617 * parent channel, becomes part of no chunk and can't output
4618 * anything until a new trace chunk is created.
4620 stream
->trace_chunk
= NULL
;
4621 } else if (stream
->chan
->trace_chunk
&&
4622 !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4623 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4628 * Update the stream's trace chunk to its parent channel's
4629 * current trace chunk.
4631 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4634 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4635 ret
= rotate_local_stream(ctx
, stream
);
4637 ERR("Failed to rotate stream, ret = %i", ret
);
4642 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4644 * If the stream has transitioned to a new trace
4645 * chunk, the metadata should be re-dumped to the
4648 * However, it is possible for a stream to transition to
4649 * a "no-chunk" state. This can happen if a rotation
4650 * occurs on an inactive session. In such cases, the metadata
4651 * regeneration will happen when the next trace chunk is
4654 ret
= consumer_metadata_stream_dump(stream
);
4659 lttng_consumer_reset_stream_rotate_state(stream
);
4668 * Rotate all the ready streams now.
4670 * This is especially important for low throughput streams that have already
4671 * been consumed, we cannot wait for their next packet to perform the
4673 * Need to be called with RCU read-side lock held to ensure existence of
4676 * Returns 0 on success, < 0 on error
4678 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
,
4679 uint64_t key
, struct lttng_consumer_local_data
*ctx
)
4682 struct lttng_consumer_stream
*stream
;
4683 struct lttng_ht_iter iter
;
4684 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
4688 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4690 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4691 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4692 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4693 stream
, node_channel_id
.node
) {
4694 health_code_update();
4696 pthread_mutex_lock(&stream
->chan
->lock
);
4697 pthread_mutex_lock(&stream
->lock
);
4699 if (!stream
->rotate_ready
) {
4700 pthread_mutex_unlock(&stream
->lock
);
4701 pthread_mutex_unlock(&stream
->chan
->lock
);
4704 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4706 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
4707 pthread_mutex_unlock(&stream
->lock
);
4708 pthread_mutex_unlock(&stream
->chan
->lock
);
4721 enum lttcomm_return_code
lttng_consumer_init_command(
4722 struct lttng_consumer_local_data
*ctx
,
4723 const lttng_uuid sessiond_uuid
)
4725 enum lttcomm_return_code ret
;
4726 char uuid_str
[LTTNG_UUID_STR_LEN
];
4728 if (ctx
->sessiond_uuid
.is_set
) {
4729 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4733 ctx
->sessiond_uuid
.is_set
= true;
4734 memcpy(ctx
->sessiond_uuid
.value
, sessiond_uuid
, sizeof(lttng_uuid
));
4735 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4736 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4737 DBG("Received session daemon UUID: %s", uuid_str
);
4742 enum lttcomm_return_code
lttng_consumer_create_trace_chunk(
4743 const uint64_t *relayd_id
, uint64_t session_id
,
4745 time_t chunk_creation_timestamp
,
4746 const char *chunk_override_name
,
4747 const struct lttng_credentials
*credentials
,
4748 struct lttng_directory_handle
*chunk_directory_handle
)
4751 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4752 struct lttng_trace_chunk
*created_chunk
= NULL
, *published_chunk
= NULL
;
4753 enum lttng_trace_chunk_status chunk_status
;
4754 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4755 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4756 const char *relayd_id_str
= "(none)";
4757 const char *creation_timestamp_str
;
4758 struct lttng_ht_iter iter
;
4759 struct lttng_consumer_channel
*channel
;
4762 /* Only used for logging purposes. */
4763 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4764 "%" PRIu64
, *relayd_id
);
4765 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4766 relayd_id_str
= relayd_id_buffer
;
4768 relayd_id_str
= "(formatting error)";
4772 /* Local protocol error. */
4773 assert(chunk_creation_timestamp
);
4774 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4775 creation_timestamp_buffer
,
4776 sizeof(creation_timestamp_buffer
));
4777 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
4778 "(formatting error)";
4780 DBG("Consumer create trace chunk command: relay_id = %s"
4781 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4782 ", chunk_override_name = %s"
4783 ", chunk_creation_timestamp = %s",
4784 relayd_id_str
, session_id
, chunk_id
,
4785 chunk_override_name
? : "(none)",
4786 creation_timestamp_str
);
4789 * The trace chunk registry, as used by the consumer daemon, implicitly
4790 * owns the trace chunks. This is only needed in the consumer since
4791 * the consumer has no notion of a session beyond session IDs being
4792 * used to identify other objects.
4794 * The lttng_trace_chunk_registry_publish() call below provides a
4795 * reference which is not released; it implicitly becomes the session
4796 * daemon's reference to the chunk in the consumer daemon.
4798 * The lifetime of trace chunks in the consumer daemon is managed by
4799 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4800 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4802 created_chunk
= lttng_trace_chunk_create(chunk_id
,
4803 chunk_creation_timestamp
, NULL
);
4804 if (!created_chunk
) {
4805 ERR("Failed to create trace chunk");
4806 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4810 if (chunk_override_name
) {
4811 chunk_status
= lttng_trace_chunk_override_name(created_chunk
,
4812 chunk_override_name
);
4813 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4814 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4819 if (chunk_directory_handle
) {
4820 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
,
4822 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4823 ERR("Failed to set trace chunk credentials");
4824 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4828 * The consumer daemon has no ownership of the chunk output
4831 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
,
4832 chunk_directory_handle
);
4833 chunk_directory_handle
= NULL
;
4834 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4835 ERR("Failed to set trace chunk's directory handle");
4836 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4841 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4842 consumer_data
.chunk_registry
, session_id
,
4844 lttng_trace_chunk_put(created_chunk
);
4845 created_chunk
= NULL
;
4846 if (!published_chunk
) {
4847 ERR("Failed to publish trace chunk");
4848 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4853 cds_lfht_for_each_entry_duplicate(consumer_data
.channels_by_session_id_ht
->ht
,
4854 consumer_data
.channels_by_session_id_ht
->hash_fct(
4855 &session_id
, lttng_ht_seed
),
4856 consumer_data
.channels_by_session_id_ht
->match_fct
,
4857 &session_id
, &iter
.iter
, channel
,
4858 channels_by_session_id_ht_node
.node
) {
4859 ret
= lttng_consumer_channel_set_trace_chunk(channel
,
4863 * Roll-back the creation of this chunk.
4865 * This is important since the session daemon will
4866 * assume that the creation of this chunk failed and
4867 * will never ask for it to be closed, resulting
4868 * in a leak and an inconsistent state for some
4871 enum lttcomm_return_code close_ret
;
4872 char path
[LTTNG_PATH_MAX
];
4874 DBG("Failed to set new trace chunk on existing channels, rolling back");
4875 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4876 session_id
, chunk_id
,
4877 chunk_creation_timestamp
, NULL
,
4879 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4880 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4881 session_id
, chunk_id
);
4884 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4890 struct consumer_relayd_sock_pair
*relayd
;
4892 relayd
= consumer_find_relayd(*relayd_id
);
4894 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4895 ret
= relayd_create_trace_chunk(
4896 &relayd
->control_sock
, published_chunk
);
4897 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4899 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4902 if (!relayd
|| ret
) {
4903 enum lttcomm_return_code close_ret
;
4904 char path
[LTTNG_PATH_MAX
];
4906 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4909 chunk_creation_timestamp
,
4911 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4912 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4917 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4924 /* Release the reference returned by the "publish" operation. */
4925 lttng_trace_chunk_put(published_chunk
);
4926 lttng_trace_chunk_put(created_chunk
);
4930 enum lttcomm_return_code
lttng_consumer_close_trace_chunk(
4931 const uint64_t *relayd_id
, uint64_t session_id
,
4932 uint64_t chunk_id
, time_t chunk_close_timestamp
,
4933 const enum lttng_trace_chunk_command_type
*close_command
,
4936 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4937 struct lttng_trace_chunk
*chunk
;
4938 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4939 const char *relayd_id_str
= "(none)";
4940 const char *close_command_name
= "none";
4941 struct lttng_ht_iter iter
;
4942 struct lttng_consumer_channel
*channel
;
4943 enum lttng_trace_chunk_status chunk_status
;
4948 /* Only used for logging purposes. */
4949 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4950 "%" PRIu64
, *relayd_id
);
4951 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4952 relayd_id_str
= relayd_id_buffer
;
4954 relayd_id_str
= "(formatting error)";
4957 if (close_command
) {
4958 close_command_name
= lttng_trace_chunk_command_type_get_name(
4962 DBG("Consumer close trace chunk command: relayd_id = %s"
4963 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4964 ", close command = %s",
4965 relayd_id_str
, session_id
, chunk_id
,
4966 close_command_name
);
4968 chunk
= lttng_trace_chunk_registry_find_chunk(
4969 consumer_data
.chunk_registry
, session_id
, chunk_id
);
4971 ERR("Failed to find chunk: session_id = %" PRIu64
4972 ", chunk_id = %" PRIu64
,
4973 session_id
, chunk_id
);
4974 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4978 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
,
4979 chunk_close_timestamp
);
4980 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4981 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4985 if (close_command
) {
4986 chunk_status
= lttng_trace_chunk_set_close_command(
4987 chunk
, *close_command
);
4988 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4989 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4995 * chunk is now invalid to access as we no longer hold a reference to
4996 * it; it is only kept around to compare it (by address) to the
4997 * current chunk found in the session's channels.
5000 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
,
5001 channel
, node
.node
) {
5005 * Only change the channel's chunk to NULL if it still
5006 * references the chunk being closed. The channel may
5007 * reference a newer channel in the case of a session
5008 * rotation. When a session rotation occurs, the "next"
5009 * chunk is created before the "current" chunk is closed.
5011 if (channel
->trace_chunk
!= chunk
) {
5014 ret
= lttng_consumer_channel_set_trace_chunk(channel
, NULL
);
5017 * Attempt to close the chunk on as many channels as
5020 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5026 struct consumer_relayd_sock_pair
*relayd
;
5028 relayd
= consumer_find_relayd(*relayd_id
);
5030 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5031 ret
= relayd_close_trace_chunk(
5032 &relayd
->control_sock
, chunk
,
5034 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5036 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
,
5040 if (!relayd
|| ret
) {
5041 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5049 * Release the reference returned by the "find" operation and
5050 * the session daemon's implicit reference to the chunk.
5052 lttng_trace_chunk_put(chunk
);
5053 lttng_trace_chunk_put(chunk
);
5058 enum lttcomm_return_code
lttng_consumer_trace_chunk_exists(
5059 const uint64_t *relayd_id
, uint64_t session_id
,
5063 enum lttcomm_return_code ret_code
;
5064 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
5065 const char *relayd_id_str
= "(none)";
5066 const bool is_local_trace
= !relayd_id
;
5067 struct consumer_relayd_sock_pair
*relayd
= NULL
;
5068 bool chunk_exists_local
, chunk_exists_remote
;
5073 /* Only used for logging purposes. */
5074 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
5075 "%" PRIu64
, *relayd_id
);
5076 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
5077 relayd_id_str
= relayd_id_buffer
;
5079 relayd_id_str
= "(formatting error)";
5083 DBG("Consumer trace chunk exists command: relayd_id = %s"
5084 ", chunk_id = %" PRIu64
, relayd_id_str
,
5086 ret
= lttng_trace_chunk_registry_chunk_exists(
5087 consumer_data
.chunk_registry
, session_id
,
5088 chunk_id
, &chunk_exists_local
);
5090 /* Internal error. */
5091 ERR("Failed to query the existence of a trace chunk");
5092 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
5095 DBG("Trace chunk %s locally",
5096 chunk_exists_local
? "exists" : "does not exist");
5097 if (chunk_exists_local
) {
5098 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
5100 } else if (is_local_trace
) {
5101 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5106 relayd
= consumer_find_relayd(*relayd_id
);
5108 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
5109 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5110 goto end_rcu_unlock
;
5112 DBG("Looking up existence of trace chunk on relay daemon");
5113 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5114 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
,
5115 &chunk_exists_remote
);
5116 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5118 ERR("Failed to look-up the existence of trace chunk on relay daemon");
5119 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
5120 goto end_rcu_unlock
;
5123 ret_code
= chunk_exists_remote
?
5124 LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
5125 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5126 DBG("Trace chunk %s on relay daemon",
5127 chunk_exists_remote
? "exists" : "does not exist");
5136 int consumer_clear_monitored_channel(struct lttng_consumer_channel
*channel
)
5138 struct lttng_ht
*ht
;
5139 struct lttng_consumer_stream
*stream
;
5140 struct lttng_ht_iter iter
;
5143 ht
= consumer_data
.stream_per_chan_id_ht
;
5146 cds_lfht_for_each_entry_duplicate(ht
->ht
,
5147 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
5148 ht
->match_fct
, &channel
->key
,
5149 &iter
.iter
, stream
, node_channel_id
.node
) {
5151 * Protect against teardown with mutex.
5153 pthread_mutex_lock(&stream
->lock
);
5154 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5157 ret
= consumer_clear_stream(stream
);
5162 pthread_mutex_unlock(&stream
->lock
);
5165 return LTTCOMM_CONSUMERD_SUCCESS
;
5168 pthread_mutex_unlock(&stream
->lock
);
5173 int lttng_consumer_clear_channel(struct lttng_consumer_channel
*channel
)
5177 DBG("Consumer clear channel %" PRIu64
, channel
->key
);
5179 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
5181 * Nothing to do for the metadata channel/stream.
5182 * Snapshot mechanism already take care of the metadata
5183 * handling/generation, and monitored channels only need to
5184 * have their data stream cleared..
5186 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5190 if (!channel
->monitor
) {
5191 ret
= consumer_clear_unmonitored_channel(channel
);
5193 ret
= consumer_clear_monitored_channel(channel
);
5199 enum lttcomm_return_code
lttng_consumer_open_channel_packets(
5200 struct lttng_consumer_channel
*channel
)
5202 struct lttng_consumer_stream
*stream
;
5203 enum lttcomm_return_code ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5205 if (channel
->metadata_stream
) {
5206 ERR("Open channel packets command attempted on a metadata channel");
5207 ret
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5212 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
5213 enum open_packet_status status
;
5215 pthread_mutex_lock(&stream
->lock
);
5216 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5220 status
= open_packet(stream
);
5222 case OPEN_PACKET_STATUS_OPENED
:
5223 DBG("Opened a packet in \"open channel packets\" command: stream id = %" PRIu64
5224 ", channel name = %s, session id = %" PRIu64
,
5225 stream
->key
, stream
->chan
->name
,
5226 stream
->chan
->session_id
);
5227 stream
->opened_packet_in_current_trace_chunk
= true;
5229 case OPEN_PACKET_STATUS_NO_SPACE
:
5230 DBG("No space left to open a packet in \"open channel packets\" command: stream id = %" PRIu64
5231 ", channel name = %s, session id = %" PRIu64
,
5232 stream
->key
, stream
->chan
->name
,
5233 stream
->chan
->session_id
);
5235 case OPEN_PACKET_STATUS_ERROR
:
5237 * Only unexpected internal errors can lead to this
5238 * failing. Report an unknown error.
5240 ERR("Failed to flush empty buffer in \"open channel packets\" command: stream id = %" PRIu64
5241 ", channel id = %" PRIu64
5242 ", channel name = %s"
5243 ", session id = %" PRIu64
,
5244 stream
->key
, channel
->key
,
5245 channel
->name
, channel
->session_id
);
5246 ret
= LTTCOMM_CONSUMERD_UNKNOWN_ERROR
;
5253 pthread_mutex_unlock(&stream
->lock
);
5262 pthread_mutex_unlock(&stream
->lock
);
5263 goto end_rcu_unlock
;