2 * Copyright (C) 2011 EfficiOS Inc.
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"
19 #include <sys/socket.h>
20 #include <sys/types.h>
25 #include <bin/lttng-consumerd/health-consumerd.h>
26 #include <common/common.h>
27 #include <common/utils.h>
28 #include <common/time.h>
29 #include <common/compat/poll.h>
30 #include <common/compat/endian.h>
31 #include <common/index/index.h>
32 #include <common/kernel-ctl/kernel-ctl.h>
33 #include <common/sessiond-comm/relayd.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/kernel-consumer/kernel-consumer.h>
36 #include <common/relayd/relayd.h>
37 #include <common/ust-consumer/ust-consumer.h>
38 #include <common/consumer/consumer-timer.h>
39 #include <common/consumer/consumer.h>
40 #include <common/consumer/consumer-stream.h>
41 #include <common/consumer/consumer-testpoint.h>
42 #include <common/align.h>
43 #include <common/consumer/consumer-metadata-cache.h>
44 #include <common/trace-chunk.h>
45 #include <common/trace-chunk-registry.h>
46 #include <common/string-utils/format.h>
47 #include <common/dynamic-array.h>
49 struct lttng_consumer_global_data consumer_data
= {
52 .type
= LTTNG_CONSUMER_UNKNOWN
,
55 enum consumer_channel_action
{
58 CONSUMER_CHANNEL_QUIT
,
61 struct consumer_channel_msg
{
62 enum consumer_channel_action action
;
63 struct lttng_consumer_channel
*chan
; /* add */
64 uint64_t key
; /* del */
67 enum open_packet_status
{
68 OPEN_PACKET_STATUS_OPENED
,
69 OPEN_PACKET_STATUS_NO_SPACE
,
70 OPEN_PACKET_STATUS_ERROR
,
73 /* Flag used to temporarily pause data consumption from testpoints. */
74 int data_consumption_paused
;
77 * Flag to inform the polling thread to quit when all fd hung up. Updated by
78 * the consumer_thread_receive_fds when it notices that all fds has hung up.
79 * Also updated by the signal handler (consumer_should_exit()). Read by the
85 * Global hash table containing respectively metadata and data streams. The
86 * stream element in this ht should only be updated by the metadata poll thread
87 * for the metadata and the data poll thread for the data.
89 static struct lttng_ht
*metadata_ht
;
90 static struct lttng_ht
*data_ht
;
92 static const char *get_consumer_domain(void)
94 switch (consumer_data
.type
) {
95 case LTTNG_CONSUMER_KERNEL
:
96 return DEFAULT_KERNEL_TRACE_DIR
;
97 case LTTNG_CONSUMER64_UST
:
99 case LTTNG_CONSUMER32_UST
:
100 return DEFAULT_UST_TRACE_DIR
;
107 * Notify a thread lttng pipe to poll back again. This usually means that some
108 * global state has changed so we just send back the thread in a poll wait
111 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
113 struct lttng_consumer_stream
*null_stream
= NULL
;
117 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
120 static void notify_health_quit_pipe(int *pipe
)
124 ret
= lttng_write(pipe
[1], "4", 1);
126 PERROR("write consumer health quit");
130 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
131 struct lttng_consumer_channel
*chan
,
133 enum consumer_channel_action action
)
135 struct consumer_channel_msg msg
;
138 memset(&msg
, 0, sizeof(msg
));
143 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
144 if (ret
< sizeof(msg
)) {
145 PERROR("notify_channel_pipe write error");
149 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
152 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
155 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
156 struct lttng_consumer_channel
**chan
,
158 enum consumer_channel_action
*action
)
160 struct consumer_channel_msg msg
;
163 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
164 if (ret
< sizeof(msg
)) {
168 *action
= msg
.action
;
176 * Cleanup the stream list of a channel. Those streams are not yet globally
179 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
181 struct lttng_consumer_stream
*stream
, *stmp
;
185 /* Delete streams that might have been left in the stream list. */
186 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
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 * Write a character on the metadata poll pipe to wake the metadata thread.
888 * Returns 0 on success, -1 on error.
890 int consumer_metadata_wakeup_pipe(const struct lttng_consumer_channel
*channel
)
894 DBG("Waking up metadata poll thread (writing to pipe): channel name = '%s'",
896 if (channel
->monitor
&& channel
->metadata_stream
) {
897 const char dummy
= 'c';
898 const ssize_t write_ret
= lttng_write(
899 channel
->metadata_stream
->ust_metadata_poll_pipe
[1],
903 if (errno
== EWOULDBLOCK
) {
905 * This is fine, the metadata poll thread
906 * is having a hard time keeping-up, but
907 * it will eventually wake-up and consume
908 * the available data.
912 PERROR("Failed to write to UST metadata pipe while attempting to wake-up the metadata poll thread");
924 * Trigger a dump of the metadata content. Following/during the succesful
925 * completion of this call, the metadata poll thread will start receiving
926 * metadata packets to consume.
928 * The caller must hold the channel and stream locks.
931 int consumer_metadata_stream_dump(struct lttng_consumer_stream
*stream
)
935 ASSERT_LOCKED(stream
->chan
->lock
);
936 ASSERT_LOCKED(stream
->lock
);
937 assert(stream
->metadata_flag
);
938 assert(stream
->chan
->trace_chunk
);
940 switch (consumer_data
.type
) {
941 case LTTNG_CONSUMER_KERNEL
:
943 * Reset the position of what has been read from the
944 * metadata cache to 0 so we can dump it again.
946 ret
= kernctl_metadata_cache_dump(stream
->wait_fd
);
948 case LTTNG_CONSUMER32_UST
:
949 case LTTNG_CONSUMER64_UST
:
951 * Reset the position pushed from the metadata cache so it
952 * will write from the beginning on the next push.
954 stream
->ust_metadata_pushed
= 0;
955 ret
= consumer_metadata_wakeup_pipe(stream
->chan
);
958 ERR("Unknown consumer_data type");
962 ERR("Failed to dump the metadata cache");
968 int lttng_consumer_channel_set_trace_chunk(
969 struct lttng_consumer_channel
*channel
,
970 struct lttng_trace_chunk
*new_trace_chunk
)
972 pthread_mutex_lock(&channel
->lock
);
973 if (channel
->is_deleted
) {
975 * The channel has been logically deleted and should no longer
976 * be used. It has released its reference to its current trace
977 * chunk and should not acquire a new one.
979 * Return success as there is nothing for the caller to do.
985 * The acquisition of the reference cannot fail (barring
986 * a severe internal error) since a reference to the published
987 * chunk is already held by the caller.
989 if (new_trace_chunk
) {
990 const bool acquired_reference
= lttng_trace_chunk_get(
993 assert(acquired_reference
);
996 lttng_trace_chunk_put(channel
->trace_chunk
);
997 channel
->trace_chunk
= new_trace_chunk
;
999 pthread_mutex_unlock(&channel
->lock
);
1004 * Allocate and return a new lttng_consumer_channel object using the given key
1005 * to initialize the hash table node.
1007 * On error, return NULL.
1009 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
1010 uint64_t session_id
,
1011 const uint64_t *chunk_id
,
1012 const char *pathname
,
1015 enum lttng_event_output output
,
1016 uint64_t tracefile_size
,
1017 uint64_t tracefile_count
,
1018 uint64_t session_id_per_pid
,
1019 unsigned int monitor
,
1020 unsigned int live_timer_interval
,
1021 bool is_in_live_session
,
1022 const char *root_shm_path
,
1023 const char *shm_path
)
1025 struct lttng_consumer_channel
*channel
= NULL
;
1026 struct lttng_trace_chunk
*trace_chunk
= NULL
;
1029 trace_chunk
= lttng_trace_chunk_registry_find_chunk(
1030 consumer_data
.chunk_registry
, session_id
,
1033 ERR("Failed to find trace chunk reference during creation of channel");
1038 channel
= zmalloc(sizeof(*channel
));
1039 if (channel
== NULL
) {
1040 PERROR("malloc struct lttng_consumer_channel");
1045 channel
->refcount
= 0;
1046 channel
->session_id
= session_id
;
1047 channel
->session_id_per_pid
= session_id_per_pid
;
1048 channel
->relayd_id
= relayd_id
;
1049 channel
->tracefile_size
= tracefile_size
;
1050 channel
->tracefile_count
= tracefile_count
;
1051 channel
->monitor
= monitor
;
1052 channel
->live_timer_interval
= live_timer_interval
;
1053 channel
->is_live
= is_in_live_session
;
1054 pthread_mutex_init(&channel
->lock
, NULL
);
1055 pthread_mutex_init(&channel
->timer_lock
, NULL
);
1058 case LTTNG_EVENT_SPLICE
:
1059 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
1061 case LTTNG_EVENT_MMAP
:
1062 channel
->output
= CONSUMER_CHANNEL_MMAP
;
1072 * In monitor mode, the streams associated with the channel will be put in
1073 * a special list ONLY owned by this channel. So, the refcount is set to 1
1074 * here meaning that the channel itself has streams that are referenced.
1076 * On a channel deletion, once the channel is no longer visible, the
1077 * refcount is decremented and checked for a zero value to delete it. With
1078 * streams in no monitor mode, it will now be safe to destroy the channel.
1080 if (!channel
->monitor
) {
1081 channel
->refcount
= 1;
1084 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1085 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1087 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1088 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1090 if (root_shm_path
) {
1091 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1092 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1095 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1096 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1099 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1100 lttng_ht_node_init_u64(&channel
->channels_by_session_id_ht_node
,
1101 channel
->session_id
);
1103 channel
->wait_fd
= -1;
1104 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1107 int ret
= lttng_consumer_channel_set_trace_chunk(channel
,
1114 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1117 lttng_trace_chunk_put(trace_chunk
);
1120 consumer_del_channel(channel
);
1126 * Add a channel to the global list protected by a mutex.
1128 * Always return 0 indicating success.
1130 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1131 struct lttng_consumer_local_data
*ctx
)
1133 pthread_mutex_lock(&consumer_data
.lock
);
1134 pthread_mutex_lock(&channel
->lock
);
1135 pthread_mutex_lock(&channel
->timer_lock
);
1138 * This gives us a guarantee that the channel we are about to add to the
1139 * channel hash table will be unique. See this function comment on the why
1140 * we need to steel the channel key at this stage.
1142 steal_channel_key(channel
->key
);
1145 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1146 lttng_ht_add_u64(consumer_data
.channels_by_session_id_ht
,
1147 &channel
->channels_by_session_id_ht_node
);
1149 channel
->is_published
= true;
1151 pthread_mutex_unlock(&channel
->timer_lock
);
1152 pthread_mutex_unlock(&channel
->lock
);
1153 pthread_mutex_unlock(&consumer_data
.lock
);
1155 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1156 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1163 * Allocate the pollfd structure and the local view of the out fds to avoid
1164 * doing a lookup in the linked list and concurrency issues when writing is
1165 * needed. Called with consumer_data.lock held.
1167 * Returns the number of fds in the structures.
1169 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1170 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1171 struct lttng_ht
*ht
, int *nb_inactive_fd
)
1174 struct lttng_ht_iter iter
;
1175 struct lttng_consumer_stream
*stream
;
1180 assert(local_stream
);
1182 DBG("Updating poll fd array");
1183 *nb_inactive_fd
= 0;
1185 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1187 * Only active streams with an active end point can be added to the
1188 * poll set and local stream storage of the thread.
1190 * There is a potential race here for endpoint_status to be updated
1191 * just after the check. However, this is OK since the stream(s) will
1192 * be deleted once the thread is notified that the end point state has
1193 * changed where this function will be called back again.
1195 * We track the number of inactive FDs because they still need to be
1196 * closed by the polling thread after a wakeup on the data_pipe or
1199 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1200 (*nb_inactive_fd
)++;
1204 * This clobbers way too much the debug output. Uncomment that if you
1205 * need it for debugging purposes.
1207 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1208 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1209 local_stream
[i
] = stream
;
1215 * Insert the consumer_data_pipe at the end of the array and don't
1216 * increment i so nb_fd is the number of real FD.
1218 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1219 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1221 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1222 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1227 * Poll on the should_quit pipe and the command socket return -1 on
1228 * error, 1 if should exit, 0 if data is available on the command socket
1230 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1235 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1236 if (num_rdy
== -1) {
1238 * Restart interrupted system call.
1240 if (errno
== EINTR
) {
1243 PERROR("Poll error");
1246 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1247 DBG("consumer_should_quit wake up");
1254 * Set the error socket.
1256 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1259 ctx
->consumer_error_socket
= sock
;
1263 * Set the command socket path.
1265 void lttng_consumer_set_command_sock_path(
1266 struct lttng_consumer_local_data
*ctx
, char *sock
)
1268 ctx
->consumer_command_sock_path
= sock
;
1272 * Send return code to the session daemon.
1273 * If the socket is not defined, we return 0, it is not a fatal error
1275 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
,
1276 enum lttcomm_return_code error_code
)
1278 if (ctx
->consumer_error_socket
> 0) {
1279 const int32_t comm_code
= (int32_t) error_code
;
1281 return lttcomm_send_unix_sock(
1282 ctx
->consumer_error_socket
, &comm_code
, sizeof(comm_code
));
1289 * Close all the tracefiles and stream fds and MUST be called when all
1290 * instances are destroyed i.e. when all threads were joined and are ended.
1292 void lttng_consumer_cleanup(void)
1294 struct lttng_ht_iter iter
;
1295 struct lttng_consumer_channel
*channel
;
1296 unsigned int trace_chunks_left
;
1300 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1302 consumer_del_channel(channel
);
1307 lttng_ht_destroy(consumer_data
.channel_ht
);
1308 lttng_ht_destroy(consumer_data
.channels_by_session_id_ht
);
1310 cleanup_relayd_ht();
1312 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1315 * This HT contains streams that are freed by either the metadata thread or
1316 * the data thread so we do *nothing* on the hash table and simply destroy
1319 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1322 * Trace chunks in the registry may still exist if the session
1323 * daemon has encountered an internal error and could not
1324 * tear down its sessions and/or trace chunks properly.
1326 * Release the session daemon's implicit reference to any remaining
1327 * trace chunk and print an error if any trace chunk was found. Note
1328 * that there are _no_ legitimate cases for trace chunks to be left,
1329 * it is a leak. However, it can happen following a crash of the
1330 * session daemon and not emptying the registry would cause an assertion
1333 trace_chunks_left
= lttng_trace_chunk_registry_put_each_chunk(
1334 consumer_data
.chunk_registry
);
1335 if (trace_chunks_left
) {
1336 ERR("%u trace chunks are leaked by lttng-consumerd. "
1337 "This can be caused by an internal error of the session daemon.",
1340 /* Run all callbacks freeing each chunk. */
1342 lttng_trace_chunk_registry_destroy(consumer_data
.chunk_registry
);
1346 * Called from signal handler.
1348 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1352 CMM_STORE_SHARED(consumer_quit
, 1);
1353 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1355 PERROR("write consumer quit");
1358 DBG("Consumer flag that it should quit");
1363 * Flush pending writes to trace output disk file.
1366 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1370 int outfd
= stream
->out_fd
;
1373 * This does a blocking write-and-wait on any page that belongs to the
1374 * subbuffer prior to the one we just wrote.
1375 * Don't care about error values, as these are just hints and ways to
1376 * limit the amount of page cache used.
1378 if (orig_offset
< stream
->max_sb_size
) {
1381 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1382 stream
->max_sb_size
,
1383 SYNC_FILE_RANGE_WAIT_BEFORE
1384 | SYNC_FILE_RANGE_WRITE
1385 | SYNC_FILE_RANGE_WAIT_AFTER
);
1387 * Give hints to the kernel about how we access the file:
1388 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1391 * We need to call fadvise again after the file grows because the
1392 * kernel does not seem to apply fadvise to non-existing parts of the
1395 * Call fadvise _after_ having waited for the page writeback to
1396 * complete because the dirty page writeback semantic is not well
1397 * defined. So it can be expected to lead to lower throughput in
1400 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1401 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1402 if (ret
&& ret
!= -ENOSYS
) {
1404 PERROR("posix_fadvise on fd %i", outfd
);
1409 * Initialise the necessary environnement :
1410 * - create a new context
1411 * - create the poll_pipe
1412 * - create the should_quit pipe (for signal handler)
1413 * - create the thread pipe (for splice)
1415 * Takes a function pointer as argument, this function is called when data is
1416 * available on a buffer. This function is responsible to do the
1417 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1418 * buffer configuration and then kernctl_put_next_subbuf at the end.
1420 * Returns a pointer to the new context or NULL on error.
1422 struct lttng_consumer_local_data
*lttng_consumer_create(
1423 enum lttng_consumer_type type
,
1424 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1425 struct lttng_consumer_local_data
*ctx
, bool locked_by_caller
),
1426 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1427 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1428 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1431 struct lttng_consumer_local_data
*ctx
;
1433 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1434 consumer_data
.type
== type
);
1435 consumer_data
.type
= type
;
1437 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1439 PERROR("allocating context");
1443 ctx
->consumer_error_socket
= -1;
1444 ctx
->consumer_metadata_socket
= -1;
1445 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1446 /* assign the callbacks */
1447 ctx
->on_buffer_ready
= buffer_ready
;
1448 ctx
->on_recv_channel
= recv_channel
;
1449 ctx
->on_recv_stream
= recv_stream
;
1450 ctx
->on_update_stream
= update_stream
;
1452 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1453 if (!ctx
->consumer_data_pipe
) {
1454 goto error_poll_pipe
;
1457 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1458 if (!ctx
->consumer_wakeup_pipe
) {
1459 goto error_wakeup_pipe
;
1462 ret
= pipe(ctx
->consumer_should_quit
);
1464 PERROR("Error creating recv pipe");
1465 goto error_quit_pipe
;
1468 ret
= pipe(ctx
->consumer_channel_pipe
);
1470 PERROR("Error creating channel pipe");
1471 goto error_channel_pipe
;
1474 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1475 if (!ctx
->consumer_metadata_pipe
) {
1476 goto error_metadata_pipe
;
1479 ctx
->channel_monitor_pipe
= -1;
1483 error_metadata_pipe
:
1484 utils_close_pipe(ctx
->consumer_channel_pipe
);
1486 utils_close_pipe(ctx
->consumer_should_quit
);
1488 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1490 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1498 * Iterate over all streams of the hashtable and free them properly.
1500 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1502 struct lttng_ht_iter iter
;
1503 struct lttng_consumer_stream
*stream
;
1510 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1512 * Ignore return value since we are currently cleaning up so any error
1515 (void) consumer_del_stream(stream
, ht
);
1519 lttng_ht_destroy(ht
);
1523 * Iterate over all streams of the metadata hashtable and free them
1526 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1528 struct lttng_ht_iter iter
;
1529 struct lttng_consumer_stream
*stream
;
1536 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1538 * Ignore return value since we are currently cleaning up so any error
1541 (void) consumer_del_metadata_stream(stream
, ht
);
1545 lttng_ht_destroy(ht
);
1549 * Close all fds associated with the instance and free the context.
1551 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1555 DBG("Consumer destroying it. Closing everything.");
1561 destroy_data_stream_ht(data_ht
);
1562 destroy_metadata_stream_ht(metadata_ht
);
1564 ret
= close(ctx
->consumer_error_socket
);
1568 ret
= close(ctx
->consumer_metadata_socket
);
1572 utils_close_pipe(ctx
->consumer_channel_pipe
);
1573 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1574 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1575 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1576 utils_close_pipe(ctx
->consumer_should_quit
);
1578 unlink(ctx
->consumer_command_sock_path
);
1583 * Write the metadata stream id on the specified file descriptor.
1585 static int write_relayd_metadata_id(int fd
,
1586 struct lttng_consumer_stream
*stream
,
1587 unsigned long padding
)
1590 struct lttcomm_relayd_metadata_payload hdr
;
1592 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1593 hdr
.padding_size
= htobe32(padding
);
1594 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1595 if (ret
< sizeof(hdr
)) {
1597 * This error means that the fd's end is closed so ignore the PERROR
1598 * not to clubber the error output since this can happen in a normal
1601 if (errno
!= EPIPE
) {
1602 PERROR("write metadata stream id");
1604 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1606 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1607 * handle writting the missing part so report that as an error and
1608 * don't lie to the caller.
1613 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1614 stream
->relayd_stream_id
, padding
);
1621 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1622 * core function for writing trace buffers to either the local filesystem or
1625 * It must be called with the stream and the channel lock held.
1627 * Careful review MUST be put if any changes occur!
1629 * Returns the number of bytes written
1631 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1632 struct lttng_consumer_stream
*stream
,
1633 const struct lttng_buffer_view
*buffer
,
1634 unsigned long padding
)
1637 off_t orig_offset
= stream
->out_fd_offset
;
1638 /* Default is on the disk */
1639 int outfd
= stream
->out_fd
;
1640 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1641 unsigned int relayd_hang_up
= 0;
1642 const size_t subbuf_content_size
= buffer
->size
- padding
;
1645 /* RCU lock for the relayd pointer */
1647 assert(stream
->net_seq_idx
!= (uint64_t) -1ULL ||
1648 stream
->trace_chunk
);
1650 /* Flag that the current stream if set for network streaming. */
1651 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1652 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1653 if (relayd
== NULL
) {
1659 /* Handle stream on the relayd if the output is on the network */
1661 unsigned long netlen
= subbuf_content_size
;
1664 * Lock the control socket for the complete duration of the function
1665 * since from this point on we will use the socket.
1667 if (stream
->metadata_flag
) {
1668 /* Metadata requires the control socket. */
1669 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1670 if (stream
->reset_metadata_flag
) {
1671 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1672 stream
->relayd_stream_id
,
1673 stream
->metadata_version
);
1678 stream
->reset_metadata_flag
= 0;
1680 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1683 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1688 /* Use the returned socket. */
1691 /* Write metadata stream id before payload */
1692 if (stream
->metadata_flag
) {
1693 ret
= write_relayd_metadata_id(outfd
, stream
, padding
);
1700 write_len
= subbuf_content_size
;
1702 /* No streaming; we have to write the full padding. */
1703 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1704 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1706 ERR("Reset metadata file");
1709 stream
->reset_metadata_flag
= 0;
1713 * Check if we need to change the tracefile before writing the packet.
1715 if (stream
->chan
->tracefile_size
> 0 &&
1716 (stream
->tracefile_size_current
+ buffer
->size
) >
1717 stream
->chan
->tracefile_size
) {
1718 ret
= consumer_stream_rotate_output_files(stream
);
1722 outfd
= stream
->out_fd
;
1725 stream
->tracefile_size_current
+= buffer
->size
;
1726 write_len
= buffer
->size
;
1730 * This call guarantee that len or less is returned. It's impossible to
1731 * receive a ret value that is bigger than len.
1733 ret
= lttng_write(outfd
, buffer
->data
, write_len
);
1734 DBG("Consumer mmap write() ret %zd (len %zu)", ret
, write_len
);
1735 if (ret
< 0 || ((size_t) ret
!= write_len
)) {
1737 * Report error to caller if nothing was written else at least send the
1745 /* Socket operation failed. We consider the relayd dead */
1746 if (errno
== EPIPE
) {
1748 * This is possible if the fd is closed on the other side
1749 * (outfd) or any write problem. It can be verbose a bit for a
1750 * normal execution if for instance the relayd is stopped
1751 * abruptly. This can happen so set this to a DBG statement.
1753 DBG("Consumer mmap write detected relayd hang up");
1755 /* Unhandled error, print it and stop function right now. */
1756 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret
,
1761 stream
->output_written
+= ret
;
1763 /* This call is useless on a socket so better save a syscall. */
1765 /* This won't block, but will start writeout asynchronously */
1766 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, write_len
,
1767 SYNC_FILE_RANGE_WRITE
);
1768 stream
->out_fd_offset
+= write_len
;
1769 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1774 * This is a special case that the relayd has closed its socket. Let's
1775 * cleanup the relayd object and all associated streams.
1777 if (relayd
&& relayd_hang_up
) {
1778 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1779 lttng_consumer_cleanup_relayd(relayd
);
1783 /* Unlock only if ctrl socket used */
1784 if (relayd
&& stream
->metadata_flag
) {
1785 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1793 * Splice the data from the ring buffer to the tracefile.
1795 * It must be called with the stream lock held.
1797 * Returns the number of bytes spliced.
1799 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1800 struct lttng_consumer_local_data
*ctx
,
1801 struct lttng_consumer_stream
*stream
, unsigned long len
,
1802 unsigned long padding
)
1804 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1806 off_t orig_offset
= stream
->out_fd_offset
;
1807 int fd
= stream
->wait_fd
;
1808 /* Default is on the disk */
1809 int outfd
= stream
->out_fd
;
1810 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1812 unsigned int relayd_hang_up
= 0;
1814 switch (consumer_data
.type
) {
1815 case LTTNG_CONSUMER_KERNEL
:
1817 case LTTNG_CONSUMER32_UST
:
1818 case LTTNG_CONSUMER64_UST
:
1819 /* Not supported for user space tracing */
1822 ERR("Unknown consumer_data type");
1826 /* RCU lock for the relayd pointer */
1829 /* Flag that the current stream if set for network streaming. */
1830 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1831 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1832 if (relayd
== NULL
) {
1837 splice_pipe
= stream
->splice_pipe
;
1839 /* Write metadata stream id before payload */
1841 unsigned long total_len
= len
;
1843 if (stream
->metadata_flag
) {
1845 * Lock the control socket for the complete duration of the function
1846 * since from this point on we will use the socket.
1848 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1850 if (stream
->reset_metadata_flag
) {
1851 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1852 stream
->relayd_stream_id
,
1853 stream
->metadata_version
);
1858 stream
->reset_metadata_flag
= 0;
1860 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
,
1868 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1871 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1877 /* Use the returned socket. */
1880 /* No streaming, we have to set the len with the full padding */
1883 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1884 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1886 ERR("Reset metadata file");
1889 stream
->reset_metadata_flag
= 0;
1892 * Check if we need to change the tracefile before writing the packet.
1894 if (stream
->chan
->tracefile_size
> 0 &&
1895 (stream
->tracefile_size_current
+ len
) >
1896 stream
->chan
->tracefile_size
) {
1897 ret
= consumer_stream_rotate_output_files(stream
);
1902 outfd
= stream
->out_fd
;
1905 stream
->tracefile_size_current
+= len
;
1909 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1910 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1911 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1912 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1913 DBG("splice chan to pipe, ret %zd", ret_splice
);
1914 if (ret_splice
< 0) {
1917 PERROR("Error in relay splice");
1921 /* Handle stream on the relayd if the output is on the network */
1922 if (relayd
&& stream
->metadata_flag
) {
1923 size_t metadata_payload_size
=
1924 sizeof(struct lttcomm_relayd_metadata_payload
);
1926 /* Update counter to fit the spliced data */
1927 ret_splice
+= metadata_payload_size
;
1928 len
+= metadata_payload_size
;
1930 * We do this so the return value can match the len passed as
1931 * argument to this function.
1933 written
-= metadata_payload_size
;
1936 /* Splice data out */
1937 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1938 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1939 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1941 if (ret_splice
< 0) {
1946 } else if (ret_splice
> len
) {
1948 * We don't expect this code path to be executed but you never know
1949 * so this is an extra protection agains a buggy splice().
1952 written
+= ret_splice
;
1953 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1957 /* All good, update current len and continue. */
1961 /* This call is useless on a socket so better save a syscall. */
1963 /* This won't block, but will start writeout asynchronously */
1964 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1965 SYNC_FILE_RANGE_WRITE
);
1966 stream
->out_fd_offset
+= ret_splice
;
1968 stream
->output_written
+= ret_splice
;
1969 written
+= ret_splice
;
1972 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1978 * This is a special case that the relayd has closed its socket. Let's
1979 * cleanup the relayd object and all associated streams.
1981 if (relayd
&& relayd_hang_up
) {
1982 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1983 lttng_consumer_cleanup_relayd(relayd
);
1984 /* Skip splice error so the consumer does not fail */
1989 /* send the appropriate error description to sessiond */
1992 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1995 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1998 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
2003 if (relayd
&& stream
->metadata_flag
) {
2004 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2012 * Sample the snapshot positions for a specific fd
2014 * Returns 0 on success, < 0 on error
2016 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
2018 switch (consumer_data
.type
) {
2019 case LTTNG_CONSUMER_KERNEL
:
2020 return lttng_kconsumer_sample_snapshot_positions(stream
);
2021 case LTTNG_CONSUMER32_UST
:
2022 case LTTNG_CONSUMER64_UST
:
2023 return lttng_ustconsumer_sample_snapshot_positions(stream
);
2025 ERR("Unknown consumer_data type");
2031 * Take a snapshot for a specific fd
2033 * Returns 0 on success, < 0 on error
2035 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2037 switch (consumer_data
.type
) {
2038 case LTTNG_CONSUMER_KERNEL
:
2039 return lttng_kconsumer_take_snapshot(stream
);
2040 case LTTNG_CONSUMER32_UST
:
2041 case LTTNG_CONSUMER64_UST
:
2042 return lttng_ustconsumer_take_snapshot(stream
);
2044 ERR("Unknown consumer_data type");
2051 * Get the produced position
2053 * Returns 0 on success, < 0 on error
2055 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
2058 switch (consumer_data
.type
) {
2059 case LTTNG_CONSUMER_KERNEL
:
2060 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
2061 case LTTNG_CONSUMER32_UST
:
2062 case LTTNG_CONSUMER64_UST
:
2063 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2065 ERR("Unknown consumer_data type");
2072 * Get the consumed position (free-running counter position in bytes).
2074 * Returns 0 on success, < 0 on error
2076 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
2079 switch (consumer_data
.type
) {
2080 case LTTNG_CONSUMER_KERNEL
:
2081 return lttng_kconsumer_get_consumed_snapshot(stream
, pos
);
2082 case LTTNG_CONSUMER32_UST
:
2083 case LTTNG_CONSUMER64_UST
:
2084 return lttng_ustconsumer_get_consumed_snapshot(stream
, pos
);
2086 ERR("Unknown consumer_data type");
2092 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2093 int sock
, struct pollfd
*consumer_sockpoll
)
2095 switch (consumer_data
.type
) {
2096 case LTTNG_CONSUMER_KERNEL
:
2097 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2098 case LTTNG_CONSUMER32_UST
:
2099 case LTTNG_CONSUMER64_UST
:
2100 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2102 ERR("Unknown consumer_data type");
2109 void lttng_consumer_close_all_metadata(void)
2111 switch (consumer_data
.type
) {
2112 case LTTNG_CONSUMER_KERNEL
:
2114 * The Kernel consumer has a different metadata scheme so we don't
2115 * close anything because the stream will be closed by the session
2119 case LTTNG_CONSUMER32_UST
:
2120 case LTTNG_CONSUMER64_UST
:
2122 * Close all metadata streams. The metadata hash table is passed and
2123 * this call iterates over it by closing all wakeup fd. This is safe
2124 * because at this point we are sure that the metadata producer is
2125 * either dead or blocked.
2127 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2130 ERR("Unknown consumer_data type");
2136 * Clean up a metadata stream and free its memory.
2138 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2139 struct lttng_ht
*ht
)
2141 struct lttng_consumer_channel
*channel
= NULL
;
2142 bool free_channel
= false;
2146 * This call should NEVER receive regular stream. It must always be
2147 * metadata stream and this is crucial for data structure synchronization.
2149 assert(stream
->metadata_flag
);
2151 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2153 pthread_mutex_lock(&consumer_data
.lock
);
2155 * Note that this assumes that a stream's channel is never changed and
2156 * that the stream's lock doesn't need to be taken to sample its
2159 channel
= stream
->chan
;
2160 pthread_mutex_lock(&channel
->lock
);
2161 pthread_mutex_lock(&stream
->lock
);
2162 if (channel
->metadata_cache
) {
2163 /* Only applicable to userspace consumers. */
2164 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
2167 /* Remove any reference to that stream. */
2168 consumer_stream_delete(stream
, ht
);
2170 /* Close down everything including the relayd if one. */
2171 consumer_stream_close(stream
);
2172 /* Destroy tracer buffers of the stream. */
2173 consumer_stream_destroy_buffers(stream
);
2175 /* Atomically decrement channel refcount since other threads can use it. */
2176 if (!uatomic_sub_return(&channel
->refcount
, 1)
2177 && !uatomic_read(&channel
->nb_init_stream_left
)) {
2178 /* Go for channel deletion! */
2179 free_channel
= true;
2181 stream
->chan
= NULL
;
2184 * Nullify the stream reference so it is not used after deletion. The
2185 * channel lock MUST be acquired before being able to check for a NULL
2188 channel
->metadata_stream
= NULL
;
2190 if (channel
->metadata_cache
) {
2191 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
2193 pthread_mutex_unlock(&stream
->lock
);
2194 pthread_mutex_unlock(&channel
->lock
);
2195 pthread_mutex_unlock(&consumer_data
.lock
);
2198 consumer_del_channel(channel
);
2201 lttng_trace_chunk_put(stream
->trace_chunk
);
2202 stream
->trace_chunk
= NULL
;
2203 consumer_stream_free(stream
);
2207 * Action done with the metadata stream when adding it to the consumer internal
2208 * data structures to handle it.
2210 void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2212 struct lttng_ht
*ht
= metadata_ht
;
2213 struct lttng_ht_iter iter
;
2214 struct lttng_ht_node_u64
*node
;
2219 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2221 pthread_mutex_lock(&consumer_data
.lock
);
2222 pthread_mutex_lock(&stream
->chan
->lock
);
2223 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2224 pthread_mutex_lock(&stream
->lock
);
2227 * From here, refcounts are updated so be _careful_ when returning an error
2234 * Lookup the stream just to make sure it does not exist in our internal
2235 * state. This should NEVER happen.
2237 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2238 node
= lttng_ht_iter_get_node_u64(&iter
);
2242 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2243 * in terms of destroying the associated channel, because the action that
2244 * causes the count to become 0 also causes a stream to be added. The
2245 * channel deletion will thus be triggered by the following removal of this
2248 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2249 /* Increment refcount before decrementing nb_init_stream_left */
2251 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2254 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2256 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
2257 &stream
->node_channel_id
);
2260 * Add stream to the stream_list_ht of the consumer data. No need to steal
2261 * the key since the HT does not use it and we allow to add redundant keys
2264 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2268 pthread_mutex_unlock(&stream
->lock
);
2269 pthread_mutex_unlock(&stream
->chan
->lock
);
2270 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2271 pthread_mutex_unlock(&consumer_data
.lock
);
2275 * Delete data stream that are flagged for deletion (endpoint_status).
2277 static void validate_endpoint_status_data_stream(void)
2279 struct lttng_ht_iter iter
;
2280 struct lttng_consumer_stream
*stream
;
2282 DBG("Consumer delete flagged data stream");
2285 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2286 /* Validate delete flag of the stream */
2287 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2290 /* Delete it right now */
2291 consumer_del_stream(stream
, data_ht
);
2297 * Delete metadata stream that are flagged for deletion (endpoint_status).
2299 static void validate_endpoint_status_metadata_stream(
2300 struct lttng_poll_event
*pollset
)
2302 struct lttng_ht_iter iter
;
2303 struct lttng_consumer_stream
*stream
;
2305 DBG("Consumer delete flagged metadata stream");
2310 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2311 /* Validate delete flag of the stream */
2312 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2316 * Remove from pollset so the metadata thread can continue without
2317 * blocking on a deleted stream.
2319 lttng_poll_del(pollset
, stream
->wait_fd
);
2321 /* Delete it right now */
2322 consumer_del_metadata_stream(stream
, metadata_ht
);
2328 * Thread polls on metadata file descriptor and write them on disk or on the
2331 void *consumer_thread_metadata_poll(void *data
)
2333 int ret
, i
, pollfd
, err
= -1;
2334 uint32_t revents
, nb_fd
;
2335 struct lttng_consumer_stream
*stream
= NULL
;
2336 struct lttng_ht_iter iter
;
2337 struct lttng_ht_node_u64
*node
;
2338 struct lttng_poll_event events
;
2339 struct lttng_consumer_local_data
*ctx
= data
;
2342 rcu_register_thread();
2344 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2346 if (testpoint(consumerd_thread_metadata
)) {
2347 goto error_testpoint
;
2350 health_code_update();
2352 DBG("Thread metadata poll started");
2354 /* Size is set to 1 for the consumer_metadata pipe */
2355 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2357 ERR("Poll set creation failed");
2361 ret
= lttng_poll_add(&events
,
2362 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2368 DBG("Metadata main loop started");
2372 health_code_update();
2373 health_poll_entry();
2374 DBG("Metadata poll wait");
2375 ret
= lttng_poll_wait(&events
, -1);
2376 DBG("Metadata poll return from wait with %d fd(s)",
2377 LTTNG_POLL_GETNB(&events
));
2379 DBG("Metadata event caught in thread");
2381 if (errno
== EINTR
) {
2382 ERR("Poll EINTR caught");
2385 if (LTTNG_POLL_GETNB(&events
) == 0) {
2386 err
= 0; /* All is OK */
2393 /* From here, the event is a metadata wait fd */
2394 for (i
= 0; i
< nb_fd
; i
++) {
2395 health_code_update();
2397 revents
= LTTNG_POLL_GETEV(&events
, i
);
2398 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2400 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2401 if (revents
& LPOLLIN
) {
2404 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2405 &stream
, sizeof(stream
));
2406 if (pipe_len
< sizeof(stream
)) {
2408 PERROR("read metadata stream");
2411 * Remove the pipe from the poll set and continue the loop
2412 * since their might be data to consume.
2414 lttng_poll_del(&events
,
2415 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2416 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2420 /* A NULL stream means that the state has changed. */
2421 if (stream
== NULL
) {
2422 /* Check for deleted streams. */
2423 validate_endpoint_status_metadata_stream(&events
);
2427 DBG("Adding metadata stream %d to poll set",
2430 /* Add metadata stream to the global poll events list */
2431 lttng_poll_add(&events
, stream
->wait_fd
,
2432 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2433 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2434 DBG("Metadata thread pipe hung up");
2436 * Remove the pipe from the poll set and continue the loop
2437 * since their might be data to consume.
2439 lttng_poll_del(&events
,
2440 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2441 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2444 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2448 /* Handle other stream */
2454 uint64_t tmp_id
= (uint64_t) pollfd
;
2456 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2458 node
= lttng_ht_iter_get_node_u64(&iter
);
2461 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2464 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2465 /* Get the data out of the metadata file descriptor */
2466 DBG("Metadata available on fd %d", pollfd
);
2467 assert(stream
->wait_fd
== pollfd
);
2470 health_code_update();
2472 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2474 * We don't check the return value here since if we get
2475 * a negative len, it means an error occurred thus we
2476 * simply remove it from the poll set and free the
2481 /* It's ok to have an unavailable sub-buffer */
2482 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2483 /* Clean up stream from consumer and free it. */
2484 lttng_poll_del(&events
, stream
->wait_fd
);
2485 consumer_del_metadata_stream(stream
, metadata_ht
);
2487 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2488 DBG("Metadata fd %d is hup|err.", pollfd
);
2489 if (!stream
->hangup_flush_done
2490 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2491 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2492 DBG("Attempting to flush and consume the UST buffers");
2493 lttng_ustconsumer_on_stream_hangup(stream
);
2495 /* We just flushed the stream now read it. */
2497 health_code_update();
2499 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2501 * We don't check the return value here since if we get
2502 * a negative len, it means an error occurred thus we
2503 * simply remove it from the poll set and free the
2509 lttng_poll_del(&events
, stream
->wait_fd
);
2511 * This call update the channel states, closes file descriptors
2512 * and securely free the stream.
2514 consumer_del_metadata_stream(stream
, metadata_ht
);
2516 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2520 /* Release RCU lock for the stream looked up */
2528 DBG("Metadata poll thread exiting");
2530 lttng_poll_clean(&events
);
2535 ERR("Health error occurred in %s", __func__
);
2537 health_unregister(health_consumerd
);
2538 rcu_unregister_thread();
2543 * This thread polls the fds in the set to consume the data and write
2544 * it to tracefile if necessary.
2546 void *consumer_thread_data_poll(void *data
)
2548 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2549 struct pollfd
*pollfd
= NULL
;
2550 /* local view of the streams */
2551 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2552 /* local view of consumer_data.fds_count */
2554 /* 2 for the consumer_data_pipe and wake up pipe */
2555 const int nb_pipes_fd
= 2;
2556 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2557 int nb_inactive_fd
= 0;
2558 struct lttng_consumer_local_data
*ctx
= data
;
2561 rcu_register_thread();
2563 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2565 if (testpoint(consumerd_thread_data
)) {
2566 goto error_testpoint
;
2569 health_code_update();
2571 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2572 if (local_stream
== NULL
) {
2573 PERROR("local_stream malloc");
2578 health_code_update();
2584 * the fds set has been updated, we need to update our
2585 * local array as well
2587 pthread_mutex_lock(&consumer_data
.lock
);
2588 if (consumer_data
.need_update
) {
2593 local_stream
= NULL
;
2595 /* Allocate for all fds */
2596 pollfd
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) * sizeof(struct pollfd
));
2597 if (pollfd
== NULL
) {
2598 PERROR("pollfd malloc");
2599 pthread_mutex_unlock(&consumer_data
.lock
);
2603 local_stream
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) *
2604 sizeof(struct lttng_consumer_stream
*));
2605 if (local_stream
== NULL
) {
2606 PERROR("local_stream malloc");
2607 pthread_mutex_unlock(&consumer_data
.lock
);
2610 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2611 data_ht
, &nb_inactive_fd
);
2613 ERR("Error in allocating pollfd or local_outfds");
2614 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2615 pthread_mutex_unlock(&consumer_data
.lock
);
2619 consumer_data
.need_update
= 0;
2621 pthread_mutex_unlock(&consumer_data
.lock
);
2623 /* No FDs and consumer_quit, consumer_cleanup the thread */
2624 if (nb_fd
== 0 && nb_inactive_fd
== 0 &&
2625 CMM_LOAD_SHARED(consumer_quit
) == 1) {
2626 err
= 0; /* All is OK */
2629 /* poll on the array of fds */
2631 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2632 if (testpoint(consumerd_thread_data_poll
)) {
2635 health_poll_entry();
2636 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2638 DBG("poll num_rdy : %d", num_rdy
);
2639 if (num_rdy
== -1) {
2641 * Restart interrupted system call.
2643 if (errno
== EINTR
) {
2646 PERROR("Poll error");
2647 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2649 } else if (num_rdy
== 0) {
2650 DBG("Polling thread timed out");
2654 if (caa_unlikely(data_consumption_paused
)) {
2655 DBG("Data consumption paused, sleeping...");
2661 * If the consumer_data_pipe triggered poll go directly to the
2662 * beginning of the loop to update the array. We want to prioritize
2663 * array update over low-priority reads.
2665 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2666 ssize_t pipe_readlen
;
2668 DBG("consumer_data_pipe wake up");
2669 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2670 &new_stream
, sizeof(new_stream
));
2671 if (pipe_readlen
< sizeof(new_stream
)) {
2672 PERROR("Consumer data pipe");
2673 /* Continue so we can at least handle the current stream(s). */
2678 * If the stream is NULL, just ignore it. It's also possible that
2679 * the sessiond poll thread changed the consumer_quit state and is
2680 * waking us up to test it.
2682 if (new_stream
== NULL
) {
2683 validate_endpoint_status_data_stream();
2687 /* Continue to update the local streams and handle prio ones */
2691 /* Handle wakeup pipe. */
2692 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2694 ssize_t pipe_readlen
;
2696 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2698 if (pipe_readlen
< 0) {
2699 PERROR("Consumer data wakeup pipe");
2701 /* We've been awakened to handle stream(s). */
2702 ctx
->has_wakeup
= 0;
2705 /* Take care of high priority channels first. */
2706 for (i
= 0; i
< nb_fd
; i
++) {
2707 health_code_update();
2709 if (local_stream
[i
] == NULL
) {
2712 if (pollfd
[i
].revents
& POLLPRI
) {
2713 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2715 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2716 /* it's ok to have an unavailable sub-buffer */
2717 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2718 /* Clean the stream and free it. */
2719 consumer_del_stream(local_stream
[i
], data_ht
);
2720 local_stream
[i
] = NULL
;
2721 } else if (len
> 0) {
2722 local_stream
[i
]->data_read
= 1;
2728 * If we read high prio channel in this loop, try again
2729 * for more high prio data.
2735 /* Take care of low priority channels. */
2736 for (i
= 0; i
< nb_fd
; i
++) {
2737 health_code_update();
2739 if (local_stream
[i
] == NULL
) {
2742 if ((pollfd
[i
].revents
& POLLIN
) ||
2743 local_stream
[i
]->hangup_flush_done
||
2744 local_stream
[i
]->has_data
) {
2745 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2746 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2747 /* it's ok to have an unavailable sub-buffer */
2748 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2749 /* Clean the stream and free it. */
2750 consumer_del_stream(local_stream
[i
], data_ht
);
2751 local_stream
[i
] = NULL
;
2752 } else if (len
> 0) {
2753 local_stream
[i
]->data_read
= 1;
2758 /* Handle hangup and errors */
2759 for (i
= 0; i
< nb_fd
; i
++) {
2760 health_code_update();
2762 if (local_stream
[i
] == NULL
) {
2765 if (!local_stream
[i
]->hangup_flush_done
2766 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2767 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2768 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2769 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2771 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2772 /* Attempt read again, for the data we just flushed. */
2773 local_stream
[i
]->data_read
= 1;
2776 * If the poll flag is HUP/ERR/NVAL and we have
2777 * read no data in this pass, we can remove the
2778 * stream from its hash table.
2780 if ((pollfd
[i
].revents
& POLLHUP
)) {
2781 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2782 if (!local_stream
[i
]->data_read
) {
2783 consumer_del_stream(local_stream
[i
], data_ht
);
2784 local_stream
[i
] = NULL
;
2787 } else if (pollfd
[i
].revents
& POLLERR
) {
2788 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2789 if (!local_stream
[i
]->data_read
) {
2790 consumer_del_stream(local_stream
[i
], data_ht
);
2791 local_stream
[i
] = NULL
;
2794 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2795 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2796 if (!local_stream
[i
]->data_read
) {
2797 consumer_del_stream(local_stream
[i
], data_ht
);
2798 local_stream
[i
] = NULL
;
2802 if (local_stream
[i
] != NULL
) {
2803 local_stream
[i
]->data_read
= 0;
2810 DBG("polling thread exiting");
2815 * Close the write side of the pipe so epoll_wait() in
2816 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2817 * read side of the pipe. If we close them both, epoll_wait strangely does
2818 * not return and could create a endless wait period if the pipe is the
2819 * only tracked fd in the poll set. The thread will take care of closing
2822 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2827 ERR("Health error occurred in %s", __func__
);
2829 health_unregister(health_consumerd
);
2831 rcu_unregister_thread();
2836 * Close wake-up end of each stream belonging to the channel. This will
2837 * allow the poll() on the stream read-side to detect when the
2838 * write-side (application) finally closes them.
2841 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2843 struct lttng_ht
*ht
;
2844 struct lttng_consumer_stream
*stream
;
2845 struct lttng_ht_iter iter
;
2847 ht
= consumer_data
.stream_per_chan_id_ht
;
2850 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2851 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2852 ht
->match_fct
, &channel
->key
,
2853 &iter
.iter
, stream
, node_channel_id
.node
) {
2855 * Protect against teardown with mutex.
2857 pthread_mutex_lock(&stream
->lock
);
2858 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2861 switch (consumer_data
.type
) {
2862 case LTTNG_CONSUMER_KERNEL
:
2864 case LTTNG_CONSUMER32_UST
:
2865 case LTTNG_CONSUMER64_UST
:
2866 if (stream
->metadata_flag
) {
2867 /* Safe and protected by the stream lock. */
2868 lttng_ustconsumer_close_metadata(stream
->chan
);
2871 * Note: a mutex is taken internally within
2872 * liblttng-ust-ctl to protect timer wakeup_fd
2873 * use from concurrent close.
2875 lttng_ustconsumer_close_stream_wakeup(stream
);
2879 ERR("Unknown consumer_data type");
2883 pthread_mutex_unlock(&stream
->lock
);
2888 static void destroy_channel_ht(struct lttng_ht
*ht
)
2890 struct lttng_ht_iter iter
;
2891 struct lttng_consumer_channel
*channel
;
2899 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2900 ret
= lttng_ht_del(ht
, &iter
);
2905 lttng_ht_destroy(ht
);
2909 * This thread polls the channel fds to detect when they are being
2910 * closed. It closes all related streams if the channel is detected as
2911 * closed. It is currently only used as a shim layer for UST because the
2912 * consumerd needs to keep the per-stream wakeup end of pipes open for
2915 void *consumer_thread_channel_poll(void *data
)
2917 int ret
, i
, pollfd
, err
= -1;
2918 uint32_t revents
, nb_fd
;
2919 struct lttng_consumer_channel
*chan
= NULL
;
2920 struct lttng_ht_iter iter
;
2921 struct lttng_ht_node_u64
*node
;
2922 struct lttng_poll_event events
;
2923 struct lttng_consumer_local_data
*ctx
= data
;
2924 struct lttng_ht
*channel_ht
;
2926 rcu_register_thread();
2928 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2930 if (testpoint(consumerd_thread_channel
)) {
2931 goto error_testpoint
;
2934 health_code_update();
2936 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2938 /* ENOMEM at this point. Better to bail out. */
2942 DBG("Thread channel poll started");
2944 /* Size is set to 1 for the consumer_channel pipe */
2945 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2947 ERR("Poll set creation failed");
2951 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2957 DBG("Channel main loop started");
2961 health_code_update();
2962 DBG("Channel poll wait");
2963 health_poll_entry();
2964 ret
= lttng_poll_wait(&events
, -1);
2965 DBG("Channel poll return from wait with %d fd(s)",
2966 LTTNG_POLL_GETNB(&events
));
2968 DBG("Channel event caught in thread");
2970 if (errno
== EINTR
) {
2971 ERR("Poll EINTR caught");
2974 if (LTTNG_POLL_GETNB(&events
) == 0) {
2975 err
= 0; /* All is OK */
2982 /* From here, the event is a channel wait fd */
2983 for (i
= 0; i
< nb_fd
; i
++) {
2984 health_code_update();
2986 revents
= LTTNG_POLL_GETEV(&events
, i
);
2987 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2989 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2990 if (revents
& LPOLLIN
) {
2991 enum consumer_channel_action action
;
2994 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2997 ERR("Error reading channel pipe");
2999 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3004 case CONSUMER_CHANNEL_ADD
:
3005 DBG("Adding channel %d to poll set",
3008 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
3011 lttng_ht_add_unique_u64(channel_ht
,
3012 &chan
->wait_fd_node
);
3014 /* Add channel to the global poll events list */
3015 lttng_poll_add(&events
, chan
->wait_fd
,
3016 LPOLLERR
| LPOLLHUP
);
3018 case CONSUMER_CHANNEL_DEL
:
3021 * This command should never be called if the channel
3022 * has streams monitored by either the data or metadata
3023 * thread. The consumer only notify this thread with a
3024 * channel del. command if it receives a destroy
3025 * channel command from the session daemon that send it
3026 * if a command prior to the GET_CHANNEL failed.
3030 chan
= consumer_find_channel(key
);
3033 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
3036 lttng_poll_del(&events
, chan
->wait_fd
);
3037 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
3038 ret
= lttng_ht_del(channel_ht
, &iter
);
3041 switch (consumer_data
.type
) {
3042 case LTTNG_CONSUMER_KERNEL
:
3044 case LTTNG_CONSUMER32_UST
:
3045 case LTTNG_CONSUMER64_UST
:
3046 health_code_update();
3047 /* Destroy streams that might have been left in the stream list. */
3048 clean_channel_stream_list(chan
);
3051 ERR("Unknown consumer_data type");
3056 * Release our own refcount. Force channel deletion even if
3057 * streams were not initialized.
3059 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3060 consumer_del_channel(chan
);
3065 case CONSUMER_CHANNEL_QUIT
:
3067 * Remove the pipe from the poll set and continue the loop
3068 * since their might be data to consume.
3070 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3073 ERR("Unknown action");
3076 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3077 DBG("Channel thread pipe hung up");
3079 * Remove the pipe from the poll set and continue the loop
3080 * since their might be data to consume.
3082 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3085 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3089 /* Handle other stream */
3095 uint64_t tmp_id
= (uint64_t) pollfd
;
3097 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3099 node
= lttng_ht_iter_get_node_u64(&iter
);
3102 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
3105 /* Check for error event */
3106 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3107 DBG("Channel fd %d is hup|err.", pollfd
);
3109 lttng_poll_del(&events
, chan
->wait_fd
);
3110 ret
= lttng_ht_del(channel_ht
, &iter
);
3114 * This will close the wait fd for each stream associated to
3115 * this channel AND monitored by the data/metadata thread thus
3116 * will be clean by the right thread.
3118 consumer_close_channel_streams(chan
);
3120 /* Release our own refcount */
3121 if (!uatomic_sub_return(&chan
->refcount
, 1)
3122 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3123 consumer_del_channel(chan
);
3126 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3131 /* Release RCU lock for the channel looked up */
3139 lttng_poll_clean(&events
);
3141 destroy_channel_ht(channel_ht
);
3144 DBG("Channel poll thread exiting");
3147 ERR("Health error occurred in %s", __func__
);
3149 health_unregister(health_consumerd
);
3150 rcu_unregister_thread();
3154 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3155 struct pollfd
*sockpoll
, int client_socket
)
3162 ret
= lttng_consumer_poll_socket(sockpoll
);
3166 DBG("Metadata connection on client_socket");
3168 /* Blocking call, waiting for transmission */
3169 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3170 if (ctx
->consumer_metadata_socket
< 0) {
3171 WARN("On accept metadata");
3182 * This thread listens on the consumerd socket and receives the file
3183 * descriptors from the session daemon.
3185 void *consumer_thread_sessiond_poll(void *data
)
3187 int sock
= -1, client_socket
, ret
, err
= -1;
3189 * structure to poll for incoming data on communication socket avoids
3190 * making blocking sockets.
3192 struct pollfd consumer_sockpoll
[2];
3193 struct lttng_consumer_local_data
*ctx
= data
;
3195 rcu_register_thread();
3197 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3199 if (testpoint(consumerd_thread_sessiond
)) {
3200 goto error_testpoint
;
3203 health_code_update();
3205 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3206 unlink(ctx
->consumer_command_sock_path
);
3207 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3208 if (client_socket
< 0) {
3209 ERR("Cannot create command socket");
3213 ret
= lttcomm_listen_unix_sock(client_socket
);
3218 DBG("Sending ready command to lttng-sessiond");
3219 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3220 /* return < 0 on error, but == 0 is not fatal */
3222 ERR("Error sending ready command to lttng-sessiond");
3226 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3227 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3228 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3229 consumer_sockpoll
[1].fd
= client_socket
;
3230 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3232 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3240 DBG("Connection on client_socket");
3242 /* Blocking call, waiting for transmission */
3243 sock
= lttcomm_accept_unix_sock(client_socket
);
3250 * Setup metadata socket which is the second socket connection on the
3251 * command unix socket.
3253 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3262 /* This socket is not useful anymore. */
3263 ret
= close(client_socket
);
3265 PERROR("close client_socket");
3269 /* update the polling structure to poll on the established socket */
3270 consumer_sockpoll
[1].fd
= sock
;
3271 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3274 health_code_update();
3276 health_poll_entry();
3277 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3286 DBG("Incoming command on sock");
3287 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3290 * This could simply be a session daemon quitting. Don't output
3293 DBG("Communication interrupted on command socket");
3297 if (CMM_LOAD_SHARED(consumer_quit
)) {
3298 DBG("consumer_thread_receive_fds received quit from signal");
3299 err
= 0; /* All is OK */
3302 DBG("received command on sock");
3308 DBG("Consumer thread sessiond poll exiting");
3311 * Close metadata streams since the producer is the session daemon which
3314 * NOTE: for now, this only applies to the UST tracer.
3316 lttng_consumer_close_all_metadata();
3319 * when all fds have hung up, the polling thread
3322 CMM_STORE_SHARED(consumer_quit
, 1);
3325 * Notify the data poll thread to poll back again and test the
3326 * consumer_quit state that we just set so to quit gracefully.
3328 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3330 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3332 notify_health_quit_pipe(health_quit_pipe
);
3334 /* Cleaning up possibly open sockets. */
3338 PERROR("close sock sessiond poll");
3341 if (client_socket
>= 0) {
3342 ret
= close(client_socket
);
3344 PERROR("close client_socket sessiond poll");
3351 ERR("Health error occurred in %s", __func__
);
3353 health_unregister(health_consumerd
);
3355 rcu_unregister_thread();
3360 int consumer_flush_buffer(struct lttng_consumer_stream
*stream
,
3361 int producer_active
)
3365 switch (consumer_data
.type
) {
3366 case LTTNG_CONSUMER_KERNEL
:
3367 if (producer_active
) {
3368 ret
= kernctl_buffer_flush(stream
->wait_fd
);
3370 ERR("Failed to flush kernel stream");
3374 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
3377 * Doing a buffer flush which does not take into
3378 * account empty packets. This is not perfect,
3379 * but required as a fall-back when
3380 * "flush_empty" is not implemented by
3383 ret
= kernctl_buffer_flush(stream
->wait_fd
);
3385 ERR("Failed to flush kernel stream");
3391 case LTTNG_CONSUMER32_UST
:
3392 case LTTNG_CONSUMER64_UST
:
3393 lttng_ustconsumer_flush_buffer(stream
, producer_active
);
3396 ERR("Unknown consumer_data type");
3404 static enum open_packet_status
open_packet(struct lttng_consumer_stream
*stream
)
3407 enum open_packet_status status
;
3408 unsigned long produced_pos_before
, produced_pos_after
;
3410 ret
= lttng_consumer_sample_snapshot_positions(stream
);
3412 ERR("Failed to snapshot positions before post-rotation empty packet flush: stream id = %" PRIu64
3413 ", channel name = %s, session id = %" PRIu64
,
3414 stream
->key
, stream
->chan
->name
,
3415 stream
->chan
->session_id
);
3416 status
= OPEN_PACKET_STATUS_ERROR
;
3420 ret
= lttng_consumer_get_produced_snapshot(
3421 stream
, &produced_pos_before
);
3423 ERR("Failed to read produced position before post-rotation empty packet flush: stream id = %" PRIu64
3424 ", channel name = %s, session id = %" PRIu64
,
3425 stream
->key
, stream
->chan
->name
,
3426 stream
->chan
->session_id
);
3427 status
= OPEN_PACKET_STATUS_ERROR
;
3431 ret
= consumer_flush_buffer(stream
, 0);
3433 ERR("Failed to flush an empty packet at rotation point: stream id = %" PRIu64
3434 ", channel name = %s, session id = %" PRIu64
,
3435 stream
->key
, stream
->chan
->name
,
3436 stream
->chan
->session_id
);
3437 status
= OPEN_PACKET_STATUS_ERROR
;
3441 ret
= lttng_consumer_sample_snapshot_positions(stream
);
3443 ERR("Failed to snapshot positions after post-rotation empty packet flush: stream id = %" PRIu64
3444 ", channel name = %s, session id = %" PRIu64
,
3445 stream
->key
, stream
->chan
->name
,
3446 stream
->chan
->session_id
);
3447 status
= OPEN_PACKET_STATUS_ERROR
;
3451 ret
= lttng_consumer_get_produced_snapshot(stream
, &produced_pos_after
);
3453 ERR("Failed to read produced position after post-rotation empty packet flush: stream id = %" PRIu64
3454 ", channel name = %s, session id = %" PRIu64
,
3455 stream
->key
, stream
->chan
->name
,
3456 stream
->chan
->session_id
);
3457 status
= OPEN_PACKET_STATUS_ERROR
;
3462 * Determine if the flush had an effect by comparing the produced
3463 * positons before and after the flush.
3465 status
= produced_pos_before
!= produced_pos_after
?
3466 OPEN_PACKET_STATUS_OPENED
:
3467 OPEN_PACKET_STATUS_NO_SPACE
;
3468 if (status
== OPEN_PACKET_STATUS_OPENED
) {
3469 stream
->opened_packet_in_current_trace_chunk
= true;
3475 static bool stream_is_rotating_to_null_chunk(
3476 const struct lttng_consumer_stream
*stream
)
3478 bool rotating_to_null_chunk
= false;
3480 if (stream
->rotate_position
== -1ULL) {
3481 /* No rotation ongoing. */
3485 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
||
3486 !stream
->chan
->trace_chunk
) {
3487 rotating_to_null_chunk
= true;
3490 return rotating_to_null_chunk
;
3493 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3494 struct lttng_consumer_local_data
*ctx
,
3495 bool locked_by_caller
)
3497 ssize_t ret
, written_bytes
= 0;
3499 struct stream_subbuffer subbuffer
= {};
3501 if (!locked_by_caller
) {
3502 stream
->read_subbuffer_ops
.lock(stream
);
3504 stream
->read_subbuffer_ops
.assert_locked(stream
);
3507 if (stream
->read_subbuffer_ops
.on_wake_up
) {
3508 ret
= stream
->read_subbuffer_ops
.on_wake_up(stream
);
3515 * If the stream was flagged to be ready for rotation before we extract
3516 * the next packet, rotate it now.
3518 if (stream
->rotate_ready
) {
3519 DBG("Rotate stream before consuming data");
3520 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3522 ERR("Stream rotation error before consuming data");
3527 ret
= stream
->read_subbuffer_ops
.get_next_subbuffer(stream
, &subbuffer
);
3529 if (ret
== -ENODATA
) {
3537 ret
= stream
->read_subbuffer_ops
.pre_consume_subbuffer(
3538 stream
, &subbuffer
);
3540 goto error_put_subbuf
;
3543 written_bytes
= stream
->read_subbuffer_ops
.consume_subbuffer(
3544 ctx
, stream
, &subbuffer
);
3545 if (written_bytes
<= 0) {
3546 ERR("Error consuming subbuffer: (%zd)", written_bytes
);
3547 ret
= (int) written_bytes
;
3548 goto error_put_subbuf
;
3551 ret
= stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3556 if (stream
->read_subbuffer_ops
.post_consume
) {
3557 ret
= stream
->read_subbuffer_ops
.post_consume(stream
, &subbuffer
, ctx
);
3564 * After extracting the packet, we check if the stream is now ready to
3565 * be rotated and perform the action immediately.
3567 * Don't overwrite `ret` as callers expect the number of bytes
3568 * consumed to be returned on success.
3570 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
3571 if (rotation_ret
== 1) {
3572 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3573 if (rotation_ret
< 0) {
3575 ERR("Stream rotation error after consuming data");
3578 } else if (rotation_ret
< 0) {
3580 ERR("Failed to check if stream was ready to rotate after consuming data");
3585 * TODO roll into a post_consume op as this doesn't apply to metadata
3588 if (!stream
->opened_packet_in_current_trace_chunk
&&
3589 stream
->trace_chunk
&& !stream
->metadata_flag
&&
3590 !stream_is_rotating_to_null_chunk(stream
)) {
3591 const enum open_packet_status status
= open_packet(stream
);
3594 case OPEN_PACKET_STATUS_OPENED
:
3595 DBG("Opened a packet after consuming a packet rotation: stream id = %" PRIu64
3596 ", channel name = %s, session id = %" PRIu64
,
3597 stream
->key
, stream
->chan
->name
,
3598 stream
->chan
->session_id
);
3600 case OPEN_PACKET_STATUS_NO_SPACE
:
3602 * Can't open a packet as there is no space left.
3603 * This means that new events were produced, resulting
3604 * in a packet being opened, which is what we wanted
3607 DBG("No space left to open a packet after consuming a packet: stream id = %" PRIu64
3608 ", channel name = %s, session id = %" PRIu64
,
3609 stream
->key
, stream
->chan
->name
,
3610 stream
->chan
->session_id
);
3611 stream
->opened_packet_in_current_trace_chunk
= true;
3613 case OPEN_PACKET_STATUS_ERROR
:
3614 /* Logged by callee. */
3623 if (stream
->read_subbuffer_ops
.on_sleep
) {
3624 stream
->read_subbuffer_ops
.on_sleep(stream
, ctx
);
3627 ret
= written_bytes
;
3629 if (!locked_by_caller
) {
3630 stream
->read_subbuffer_ops
.unlock(stream
);
3635 (void) stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3639 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3641 switch (consumer_data
.type
) {
3642 case LTTNG_CONSUMER_KERNEL
:
3643 return lttng_kconsumer_on_recv_stream(stream
);
3644 case LTTNG_CONSUMER32_UST
:
3645 case LTTNG_CONSUMER64_UST
:
3646 return lttng_ustconsumer_on_recv_stream(stream
);
3648 ERR("Unknown consumer_data type");
3655 * Allocate and set consumer data hash tables.
3657 int lttng_consumer_init(void)
3659 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3660 if (!consumer_data
.channel_ht
) {
3664 consumer_data
.channels_by_session_id_ht
=
3665 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3666 if (!consumer_data
.channels_by_session_id_ht
) {
3670 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3671 if (!consumer_data
.relayd_ht
) {
3675 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3676 if (!consumer_data
.stream_list_ht
) {
3680 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3681 if (!consumer_data
.stream_per_chan_id_ht
) {
3685 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3690 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3695 consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3696 if (!consumer_data
.chunk_registry
) {
3707 * Process the ADD_RELAYD command receive by a consumer.
3709 * This will create a relayd socket pair and add it to the relayd hash table.
3710 * The caller MUST acquire a RCU read side lock before calling it.
3712 void consumer_add_relayd_socket(uint64_t net_seq_idx
,
3714 struct lttng_consumer_local_data
*ctx
,
3716 struct pollfd
*consumer_sockpoll
,
3717 uint64_t sessiond_id
,
3718 uint64_t relayd_session_id
,
3719 uint32_t relayd_version_major
,
3720 uint32_t relayd_version_minor
,
3721 enum lttcomm_sock_proto relayd_socket_protocol
)
3723 int fd
= -1, ret
= -1, relayd_created
= 0;
3724 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3725 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3729 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3731 /* Get relayd reference if exists. */
3732 relayd
= consumer_find_relayd(net_seq_idx
);
3733 if (relayd
== NULL
) {
3734 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3735 /* Not found. Allocate one. */
3736 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3737 if (relayd
== NULL
) {
3738 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3741 relayd
->sessiond_session_id
= sessiond_id
;
3746 * This code path MUST continue to the consumer send status message to
3747 * we can notify the session daemon and continue our work without
3748 * killing everything.
3752 * relayd key should never be found for control socket.
3754 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3757 /* First send a status message before receiving the fds. */
3758 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3760 /* Somehow, the session daemon is not responding anymore. */
3761 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3762 goto error_nosignal
;
3765 /* Poll on consumer socket. */
3766 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3768 /* Needing to exit in the middle of a command: error. */
3769 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3770 goto error_nosignal
;
3773 /* Get relayd socket from session daemon */
3774 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3775 if (ret
!= sizeof(fd
)) {
3776 fd
= -1; /* Just in case it gets set with an invalid value. */
3779 * Failing to receive FDs might indicate a major problem such as
3780 * reaching a fd limit during the receive where the kernel returns a
3781 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3782 * don't take any chances and stop everything.
3784 * XXX: Feature request #558 will fix that and avoid this possible
3785 * issue when reaching the fd limit.
3787 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3788 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3792 /* Copy socket information and received FD */
3793 switch (sock_type
) {
3794 case LTTNG_STREAM_CONTROL
:
3795 /* Copy received lttcomm socket */
3796 ret
= lttcomm_populate_sock_from_open_socket(
3797 &relayd
->control_sock
.sock
, fd
,
3798 relayd_socket_protocol
);
3800 /* Assign version values. */
3801 relayd
->control_sock
.major
= relayd_version_major
;
3802 relayd
->control_sock
.minor
= relayd_version_minor
;
3804 relayd
->relayd_session_id
= relayd_session_id
;
3807 case LTTNG_STREAM_DATA
:
3808 /* Copy received lttcomm socket */
3809 ret
= lttcomm_populate_sock_from_open_socket(
3810 &relayd
->data_sock
.sock
, fd
,
3811 relayd_socket_protocol
);
3812 /* Assign version values. */
3813 relayd
->data_sock
.major
= relayd_version_major
;
3814 relayd
->data_sock
.minor
= relayd_version_minor
;
3817 ERR("Unknown relayd socket type (%d)", sock_type
);
3818 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3823 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3827 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3828 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3829 relayd
->net_seq_idx
, fd
);
3831 * We gave the ownership of the fd to the relayd structure. Set the
3832 * fd to -1 so we don't call close() on it in the error path below.
3836 /* We successfully added the socket. Send status back. */
3837 ret
= consumer_send_status_msg(sock
, ret_code
);
3839 /* Somehow, the session daemon is not responding anymore. */
3840 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3841 goto error_nosignal
;
3845 * Add relayd socket pair to consumer data hashtable. If object already
3846 * exists or on error, the function gracefully returns.
3855 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3856 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3860 /* Close received socket if valid. */
3863 PERROR("close received socket");
3867 if (relayd_created
) {
3873 * Search for a relayd associated to the session id and return the reference.
3875 * A rcu read side lock MUST be acquire before calling this function and locked
3876 * until the relayd object is no longer necessary.
3878 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3880 struct lttng_ht_iter iter
;
3881 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3883 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3884 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3887 * Check by sessiond id which is unique here where the relayd session
3888 * id might not be when having multiple relayd.
3890 if (relayd
->sessiond_session_id
== id
) {
3891 /* Found the relayd. There can be only one per id. */
3903 * Check if for a given session id there is still data needed to be extract
3906 * Return 1 if data is pending or else 0 meaning ready to be read.
3908 int consumer_data_pending(uint64_t id
)
3911 struct lttng_ht_iter iter
;
3912 struct lttng_ht
*ht
;
3913 struct lttng_consumer_stream
*stream
;
3914 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3915 int (*data_pending
)(struct lttng_consumer_stream
*);
3917 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3920 pthread_mutex_lock(&consumer_data
.lock
);
3922 switch (consumer_data
.type
) {
3923 case LTTNG_CONSUMER_KERNEL
:
3924 data_pending
= lttng_kconsumer_data_pending
;
3926 case LTTNG_CONSUMER32_UST
:
3927 case LTTNG_CONSUMER64_UST
:
3928 data_pending
= lttng_ustconsumer_data_pending
;
3931 ERR("Unknown consumer data type");
3935 /* Ease our life a bit */
3936 ht
= consumer_data
.stream_list_ht
;
3938 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3939 ht
->hash_fct(&id
, lttng_ht_seed
),
3941 &iter
.iter
, stream
, node_session_id
.node
) {
3942 pthread_mutex_lock(&stream
->lock
);
3945 * A removed node from the hash table indicates that the stream has
3946 * been deleted thus having a guarantee that the buffers are closed
3947 * on the consumer side. However, data can still be transmitted
3948 * over the network so don't skip the relayd check.
3950 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3952 /* Check the stream if there is data in the buffers. */
3953 ret
= data_pending(stream
);
3955 pthread_mutex_unlock(&stream
->lock
);
3960 pthread_mutex_unlock(&stream
->lock
);
3963 relayd
= find_relayd_by_session_id(id
);
3965 unsigned int is_data_inflight
= 0;
3967 /* Send init command for data pending. */
3968 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3969 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3970 relayd
->relayd_session_id
);
3972 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3973 /* Communication error thus the relayd so no data pending. */
3974 goto data_not_pending
;
3977 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3978 ht
->hash_fct(&id
, lttng_ht_seed
),
3980 &iter
.iter
, stream
, node_session_id
.node
) {
3981 if (stream
->metadata_flag
) {
3982 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3983 stream
->relayd_stream_id
);
3985 ret
= relayd_data_pending(&relayd
->control_sock
,
3986 stream
->relayd_stream_id
,
3987 stream
->next_net_seq_num
- 1);
3991 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3993 } else if (ret
< 0) {
3994 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3995 lttng_consumer_cleanup_relayd(relayd
);
3996 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3997 goto data_not_pending
;
4001 /* Send end command for data pending. */
4002 ret
= relayd_end_data_pending(&relayd
->control_sock
,
4003 relayd
->relayd_session_id
, &is_data_inflight
);
4004 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4006 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
4007 lttng_consumer_cleanup_relayd(relayd
);
4008 goto data_not_pending
;
4010 if (is_data_inflight
) {
4016 * Finding _no_ node in the hash table and no inflight data means that the
4017 * stream(s) have been removed thus data is guaranteed to be available for
4018 * analysis from the trace files.
4022 /* Data is available to be read by a viewer. */
4023 pthread_mutex_unlock(&consumer_data
.lock
);
4028 /* Data is still being extracted from buffers. */
4029 pthread_mutex_unlock(&consumer_data
.lock
);
4035 * Send a ret code status message to the sessiond daemon.
4037 * Return the sendmsg() return value.
4039 int consumer_send_status_msg(int sock
, int ret_code
)
4041 struct lttcomm_consumer_status_msg msg
;
4043 memset(&msg
, 0, sizeof(msg
));
4044 msg
.ret_code
= ret_code
;
4046 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
4050 * Send a channel status message to the sessiond daemon.
4052 * Return the sendmsg() return value.
4054 int consumer_send_status_channel(int sock
,
4055 struct lttng_consumer_channel
*channel
)
4057 struct lttcomm_consumer_status_channel msg
;
4061 memset(&msg
, 0, sizeof(msg
));
4063 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
4065 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4066 msg
.key
= channel
->key
;
4067 msg
.stream_count
= channel
->streams
.count
;
4070 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
4073 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
4074 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
4075 uint64_t max_sb_size
)
4077 unsigned long start_pos
;
4079 if (!nb_packets_per_stream
) {
4080 return consumed_pos
; /* Grab everything */
4082 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
4083 start_pos
-= max_sb_size
* nb_packets_per_stream
;
4084 if ((long) (start_pos
- consumed_pos
) < 0) {
4085 return consumed_pos
; /* Grab everything */
4090 /* Stream lock must be held by the caller. */
4091 static int sample_stream_positions(struct lttng_consumer_stream
*stream
,
4092 unsigned long *produced
, unsigned long *consumed
)
4096 ASSERT_LOCKED(stream
->lock
);
4098 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4100 ERR("Failed to sample snapshot positions");
4104 ret
= lttng_consumer_get_produced_snapshot(stream
, produced
);
4106 ERR("Failed to sample produced position");
4110 ret
= lttng_consumer_get_consumed_snapshot(stream
, consumed
);
4112 ERR("Failed to sample consumed position");
4121 * Sample the rotate position for all the streams of a channel. If a stream
4122 * is already at the rotate position (produced == consumed), we flag it as
4123 * ready for rotation. The rotation of ready streams occurs after we have
4124 * replied to the session daemon that we have finished sampling the positions.
4125 * Must be called with RCU read-side lock held to ensure existence of channel.
4127 * Returns 0 on success, < 0 on error
4129 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
4130 uint64_t key
, uint64_t relayd_id
, uint32_t metadata
,
4131 struct lttng_consumer_local_data
*ctx
)
4134 struct lttng_consumer_stream
*stream
;
4135 struct lttng_ht_iter iter
;
4136 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
4137 struct lttng_dynamic_array stream_rotation_positions
;
4138 uint64_t next_chunk_id
, stream_count
= 0;
4139 enum lttng_trace_chunk_status chunk_status
;
4140 const bool is_local_trace
= relayd_id
== -1ULL;
4141 struct consumer_relayd_sock_pair
*relayd
= NULL
;
4142 bool rotating_to_new_chunk
= true;
4143 /* Array of `struct lttng_consumer_stream *` */
4144 struct lttng_dynamic_pointer_array streams_packet_to_open
;
4147 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
4149 lttng_dynamic_array_init(&stream_rotation_positions
,
4150 sizeof(struct relayd_stream_rotation_position
), NULL
);
4151 lttng_dynamic_pointer_array_init(&streams_packet_to_open
, NULL
);
4155 pthread_mutex_lock(&channel
->lock
);
4156 assert(channel
->trace_chunk
);
4157 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
,
4159 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4161 goto end_unlock_channel
;
4164 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4165 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4166 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4167 stream
, node_channel_id
.node
) {
4168 unsigned long produced_pos
= 0, consumed_pos
= 0;
4170 health_code_update();
4173 * Lock stream because we are about to change its state.
4175 pthread_mutex_lock(&stream
->lock
);
4177 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
4178 rotating_to_new_chunk
= false;
4182 * Do not flush a packet when rotating from a NULL trace
4183 * chunk. The stream has no means to output data, and the prior
4184 * rotation which rotated to NULL performed that side-effect
4185 * already. No new data can be produced when a stream has no
4186 * associated trace chunk (e.g. a stop followed by a rotate).
4188 if (stream
->trace_chunk
) {
4191 if (stream
->metadata_flag
) {
4193 * Don't produce an empty metadata packet,
4194 * simply close the current one.
4196 * Metadata is regenerated on every trace chunk
4197 * switch; there is no concern that no data was
4200 flush_active
= true;
4203 * Only flush an empty packet if the "packet
4204 * open" could not be performed on transition
4205 * to a new trace chunk and no packets were
4206 * consumed within the chunk's lifetime.
4208 if (stream
->opened_packet_in_current_trace_chunk
) {
4209 flush_active
= true;
4212 * Stream could have been full at the
4213 * time of rotation, but then have had
4214 * no activity at all.
4216 * It is important to flush a packet
4217 * to prevent 0-length files from being
4218 * produced as most viewers choke on
4221 * Unfortunately viewers will not be
4222 * able to know that tracing was active
4223 * for this stream during this trace
4226 ret
= sample_stream_positions(stream
, &produced_pos
, &consumed_pos
);
4228 goto end_unlock_stream
;
4232 * Don't flush an empty packet if data
4233 * was produced; it will be consumed
4234 * before the rotation completes.
4236 flush_active
= produced_pos
!= consumed_pos
;
4237 if (!flush_active
) {
4238 enum lttng_trace_chunk_status chunk_status
;
4239 const char *trace_chunk_name
;
4240 uint64_t trace_chunk_id
;
4242 chunk_status
= lttng_trace_chunk_get_name(
4243 stream
->trace_chunk
,
4246 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NONE
) {
4247 trace_chunk_name
= "none";
4251 * Consumer trace chunks are
4254 chunk_status
= lttng_trace_chunk_get_id(
4255 stream
->trace_chunk
,
4257 assert(chunk_status
==
4258 LTTNG_TRACE_CHUNK_STATUS_OK
);
4260 DBG("Unable to open packet for stream during trace chunk's lifetime. "
4261 "Flushing an empty packet to prevent an empty file from being created: "
4262 "stream id = %" PRIu64
", trace chunk name = `%s`, trace chunk id = %" PRIu64
,
4263 stream
->key
, trace_chunk_name
, trace_chunk_id
);
4269 * Close the current packet before sampling the
4270 * ring buffer positions.
4272 ret
= consumer_flush_buffer(stream
, flush_active
);
4274 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4276 goto end_unlock_stream
;
4280 ret
= lttng_consumer_take_snapshot(stream
);
4281 if (ret
< 0 && ret
!= -ENODATA
&& ret
!= -EAGAIN
) {
4282 ERR("Failed to sample snapshot position during channel rotation");
4283 goto end_unlock_stream
;
4286 ret
= lttng_consumer_get_produced_snapshot(stream
,
4289 ERR("Failed to sample produced position during channel rotation");
4290 goto end_unlock_stream
;
4293 ret
= lttng_consumer_get_consumed_snapshot(stream
,
4296 ERR("Failed to sample consumed position during channel rotation");
4297 goto end_unlock_stream
;
4301 * Align produced position on the start-of-packet boundary of the first
4302 * packet going into the next trace chunk.
4304 produced_pos
= ALIGN_FLOOR(produced_pos
, stream
->max_sb_size
);
4305 if (consumed_pos
== produced_pos
) {
4306 DBG("Set rotate ready for stream %" PRIu64
" produced = %lu consumed = %lu",
4307 stream
->key
, produced_pos
, consumed_pos
);
4308 stream
->rotate_ready
= true;
4310 DBG("Different consumed and produced positions "
4311 "for stream %" PRIu64
" produced = %lu consumed = %lu",
4312 stream
->key
, produced_pos
, consumed_pos
);
4315 * The rotation position is based on the packet_seq_num of the
4316 * packet following the last packet that was consumed for this
4317 * stream, incremented by the offset between produced and
4318 * consumed positions. This rotation position is a lower bound
4319 * (inclusive) at which the next trace chunk starts. Since it
4320 * is a lower bound, it is OK if the packet_seq_num does not
4321 * correspond exactly to the same packet identified by the
4322 * consumed_pos, which can happen in overwrite mode.
4324 if (stream
->sequence_number_unavailable
) {
4326 * Rotation should never be performed on a session which
4327 * interacts with a pre-2.8 lttng-modules, which does
4328 * not implement packet sequence number.
4330 ERR("Failure to rotate stream %" PRIu64
": sequence number unavailable",
4333 goto end_unlock_stream
;
4335 stream
->rotate_position
= stream
->last_sequence_number
+ 1 +
4336 ((produced_pos
- consumed_pos
) / stream
->max_sb_size
);
4337 DBG("Set rotation position for stream %" PRIu64
" at position %" PRIu64
,
4338 stream
->key
, stream
->rotate_position
);
4340 if (!is_local_trace
) {
4342 * The relay daemon control protocol expects a rotation
4343 * position as "the sequence number of the first packet
4344 * _after_ the current trace chunk".
4346 const struct relayd_stream_rotation_position position
= {
4347 .stream_id
= stream
->relayd_stream_id
,
4348 .rotate_at_seq_num
= stream
->rotate_position
,
4351 ret
= lttng_dynamic_array_add_element(
4352 &stream_rotation_positions
,
4355 ERR("Failed to allocate stream rotation position");
4356 goto end_unlock_stream
;
4361 stream
->opened_packet_in_current_trace_chunk
= false;
4363 if (rotating_to_new_chunk
&& !stream
->metadata_flag
) {
4365 * Attempt to flush an empty packet as close to the
4366 * rotation point as possible. In the event where a
4367 * stream remains inactive after the rotation point,
4368 * this ensures that the new trace chunk has a
4369 * beginning timestamp set at the begining of the
4370 * trace chunk instead of only creating an empty
4371 * packet when the trace chunk is stopped.
4373 * This indicates to the viewers that the stream
4374 * was being recorded, but more importantly it
4375 * allows viewers to determine a useable trace
4378 * This presents a problem in the case where the
4379 * ring-buffer is completely full.
4381 * Consider the following scenario:
4382 * - The consumption of data is slow (slow network,
4384 * - The ring buffer is full,
4385 * - A rotation is initiated,
4386 * - The flush below does nothing (no space left to
4387 * open a new packet),
4388 * - The other streams rotate very soon, and new
4389 * data is produced in the new chunk,
4390 * - This stream completes its rotation long after the
4391 * rotation was initiated
4392 * - The session is stopped before any event can be
4393 * produced in this stream's buffers.
4395 * The resulting trace chunk will have a single packet
4396 * temporaly at the end of the trace chunk for this
4397 * stream making the stream intersection more narrow
4398 * than it should be.
4400 * To work-around this, an empty flush is performed
4401 * after the first consumption of a packet during a
4402 * rotation if open_packet fails. The idea is that
4403 * consuming a packet frees enough space to switch
4404 * packets in this scenario and allows the tracer to
4405 * "stamp" the beginning of the new trace chunk at the
4406 * earliest possible point.
4408 * The packet open is performed after the channel
4409 * rotation to ensure that no attempt to open a packet
4410 * is performed in a stream that has no active trace
4413 ret
= lttng_dynamic_pointer_array_add_pointer(
4414 &streams_packet_to_open
, stream
);
4416 PERROR("Failed to add a stream pointer to array of streams in which to open a packet");
4418 goto end_unlock_stream
;
4422 pthread_mutex_unlock(&stream
->lock
);
4426 if (!is_local_trace
) {
4427 relayd
= consumer_find_relayd(relayd_id
);
4429 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4431 goto end_unlock_channel
;
4434 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4435 ret
= relayd_rotate_streams(&relayd
->control_sock
, stream_count
,
4436 rotating_to_new_chunk
? &next_chunk_id
: NULL
,
4437 (const struct relayd_stream_rotation_position
*)
4438 stream_rotation_positions
.buffer
4440 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4442 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4443 relayd
->net_seq_idx
);
4444 lttng_consumer_cleanup_relayd(relayd
);
4445 goto end_unlock_channel
;
4449 for (stream_idx
= 0;
4450 stream_idx
< lttng_dynamic_pointer_array_get_count(
4451 &streams_packet_to_open
);
4453 enum open_packet_status status
;
4455 stream
= lttng_dynamic_pointer_array_get_pointer(
4456 &streams_packet_to_open
, stream_idx
);
4458 pthread_mutex_lock(&stream
->lock
);
4459 status
= open_packet(stream
);
4460 pthread_mutex_unlock(&stream
->lock
);
4462 case OPEN_PACKET_STATUS_OPENED
:
4463 DBG("Opened a packet after a rotation: stream id = %" PRIu64
4464 ", channel name = %s, session id = %" PRIu64
,
4465 stream
->key
, stream
->chan
->name
,
4466 stream
->chan
->session_id
);
4468 case OPEN_PACKET_STATUS_NO_SPACE
:
4470 * Can't open a packet as there is no space left
4471 * in the buffer. A new packet will be opened
4472 * once one has been consumed.
4474 DBG("No space left to open a packet after a rotation: stream id = %" PRIu64
4475 ", channel name = %s, session id = %" PRIu64
,
4476 stream
->key
, stream
->chan
->name
,
4477 stream
->chan
->session_id
);
4479 case OPEN_PACKET_STATUS_ERROR
:
4480 /* Logged by callee. */
4482 goto end_unlock_channel
;
4488 pthread_mutex_unlock(&channel
->lock
);
4493 pthread_mutex_unlock(&stream
->lock
);
4495 pthread_mutex_unlock(&channel
->lock
);
4498 lttng_dynamic_array_reset(&stream_rotation_positions
);
4499 lttng_dynamic_pointer_array_reset(&streams_packet_to_open
);
4504 int consumer_clear_buffer(struct lttng_consumer_stream
*stream
)
4507 unsigned long consumed_pos_before
, consumed_pos_after
;
4509 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4511 ERR("Taking snapshot positions");
4515 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_before
);
4517 ERR("Consumed snapshot position");
4521 switch (consumer_data
.type
) {
4522 case LTTNG_CONSUMER_KERNEL
:
4523 ret
= kernctl_buffer_clear(stream
->wait_fd
);
4525 ERR("Failed to clear kernel stream (ret = %d)", ret
);
4529 case LTTNG_CONSUMER32_UST
:
4530 case LTTNG_CONSUMER64_UST
:
4531 lttng_ustconsumer_clear_buffer(stream
);
4534 ERR("Unknown consumer_data type");
4538 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4540 ERR("Taking snapshot positions");
4543 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_after
);
4545 ERR("Consumed snapshot position");
4548 DBG("clear: before: %lu after: %lu", consumed_pos_before
, consumed_pos_after
);
4554 int consumer_clear_stream(struct lttng_consumer_stream
*stream
)
4558 ret
= consumer_flush_buffer(stream
, 1);
4560 ERR("Failed to flush stream %" PRIu64
" during channel clear",
4562 ret
= LTTCOMM_CONSUMERD_FATAL
;
4566 ret
= consumer_clear_buffer(stream
);
4568 ERR("Failed to clear stream %" PRIu64
" during channel clear",
4570 ret
= LTTCOMM_CONSUMERD_FATAL
;
4574 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4580 int consumer_clear_unmonitored_channel(struct lttng_consumer_channel
*channel
)
4583 struct lttng_consumer_stream
*stream
;
4586 pthread_mutex_lock(&channel
->lock
);
4587 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
4588 health_code_update();
4589 pthread_mutex_lock(&stream
->lock
);
4590 ret
= consumer_clear_stream(stream
);
4594 pthread_mutex_unlock(&stream
->lock
);
4596 pthread_mutex_unlock(&channel
->lock
);
4601 pthread_mutex_unlock(&stream
->lock
);
4602 pthread_mutex_unlock(&channel
->lock
);
4608 * Check if a stream is ready to be rotated after extracting it.
4610 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4611 * error. Stream lock must be held.
4613 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4615 DBG("Check is rotate ready for stream %" PRIu64
4616 " ready %u rotate_position %" PRIu64
4617 " last_sequence_number %" PRIu64
,
4618 stream
->key
, stream
->rotate_ready
,
4619 stream
->rotate_position
, stream
->last_sequence_number
);
4620 if (stream
->rotate_ready
) {
4625 * If packet seq num is unavailable, it means we are interacting
4626 * with a pre-2.8 lttng-modules which does not implement the
4627 * sequence number. Rotation should never be used by sessiond in this
4630 if (stream
->sequence_number_unavailable
) {
4631 ERR("Internal error: rotation used on stream %" PRIu64
4632 " with unavailable sequence number",
4637 if (stream
->rotate_position
== -1ULL ||
4638 stream
->last_sequence_number
== -1ULL) {
4643 * Rotate position not reached yet. The stream rotate position is
4644 * the position of the next packet belonging to the next trace chunk,
4645 * but consumerd considers rotation ready when reaching the last
4646 * packet of the current chunk, hence the "rotate_position - 1".
4649 DBG("Check is rotate ready for stream %" PRIu64
4650 " last_sequence_number %" PRIu64
4651 " rotate_position %" PRIu64
,
4652 stream
->key
, stream
->last_sequence_number
,
4653 stream
->rotate_position
);
4654 if (stream
->last_sequence_number
>= stream
->rotate_position
- 1) {
4662 * Reset the state for a stream after a rotation occurred.
4664 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4666 DBG("lttng_consumer_reset_stream_rotate_state for stream %" PRIu64
,
4668 stream
->rotate_position
= -1ULL;
4669 stream
->rotate_ready
= false;
4673 * Perform the rotation a local stream file.
4676 int rotate_local_stream(struct lttng_consumer_local_data
*ctx
,
4677 struct lttng_consumer_stream
*stream
)
4681 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4684 stream
->tracefile_size_current
= 0;
4685 stream
->tracefile_count_current
= 0;
4687 if (stream
->out_fd
>= 0) {
4688 ret
= close(stream
->out_fd
);
4690 PERROR("Failed to close stream out_fd of channel \"%s\"",
4691 stream
->chan
->name
);
4693 stream
->out_fd
= -1;
4696 if (stream
->index_file
) {
4697 lttng_index_file_put(stream
->index_file
);
4698 stream
->index_file
= NULL
;
4701 if (!stream
->trace_chunk
) {
4705 ret
= consumer_stream_create_output_files(stream
, true);
4711 * Performs the stream rotation for the rotate session feature if needed.
4712 * It must be called with the channel and stream locks held.
4714 * Return 0 on success, a negative number of error.
4716 int lttng_consumer_rotate_stream(struct lttng_consumer_local_data
*ctx
,
4717 struct lttng_consumer_stream
*stream
)
4721 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4724 * Update the stream's 'current' chunk to the session's (channel)
4725 * now-current chunk.
4727 lttng_trace_chunk_put(stream
->trace_chunk
);
4728 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4730 * A channel can be rotated and not have a "next" chunk
4731 * to transition to. In that case, the channel's "current chunk"
4732 * has not been closed yet, but it has not been updated to
4733 * a "next" trace chunk either. Hence, the stream, like its
4734 * parent channel, becomes part of no chunk and can't output
4735 * anything until a new trace chunk is created.
4737 stream
->trace_chunk
= NULL
;
4738 } else if (stream
->chan
->trace_chunk
&&
4739 !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4740 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4745 * Update the stream's trace chunk to its parent channel's
4746 * current trace chunk.
4748 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4751 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4752 ret
= rotate_local_stream(ctx
, stream
);
4754 ERR("Failed to rotate stream, ret = %i", ret
);
4759 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4761 * If the stream has transitioned to a new trace
4762 * chunk, the metadata should be re-dumped to the
4765 * However, it is possible for a stream to transition to
4766 * a "no-chunk" state. This can happen if a rotation
4767 * occurs on an inactive session. In such cases, the metadata
4768 * regeneration will happen when the next trace chunk is
4771 ret
= consumer_metadata_stream_dump(stream
);
4776 lttng_consumer_reset_stream_rotate_state(stream
);
4785 * Rotate all the ready streams now.
4787 * This is especially important for low throughput streams that have already
4788 * been consumed, we cannot wait for their next packet to perform the
4790 * Need to be called with RCU read-side lock held to ensure existence of
4793 * Returns 0 on success, < 0 on error
4795 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
,
4796 uint64_t key
, struct lttng_consumer_local_data
*ctx
)
4799 struct lttng_consumer_stream
*stream
;
4800 struct lttng_ht_iter iter
;
4801 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
4805 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4807 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4808 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4809 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4810 stream
, node_channel_id
.node
) {
4811 health_code_update();
4813 pthread_mutex_lock(&stream
->chan
->lock
);
4814 pthread_mutex_lock(&stream
->lock
);
4816 if (!stream
->rotate_ready
) {
4817 pthread_mutex_unlock(&stream
->lock
);
4818 pthread_mutex_unlock(&stream
->chan
->lock
);
4821 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4823 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
4824 pthread_mutex_unlock(&stream
->lock
);
4825 pthread_mutex_unlock(&stream
->chan
->lock
);
4838 enum lttcomm_return_code
lttng_consumer_init_command(
4839 struct lttng_consumer_local_data
*ctx
,
4840 const lttng_uuid sessiond_uuid
)
4842 enum lttcomm_return_code ret
;
4843 char uuid_str
[LTTNG_UUID_STR_LEN
];
4845 if (ctx
->sessiond_uuid
.is_set
) {
4846 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4850 ctx
->sessiond_uuid
.is_set
= true;
4851 memcpy(ctx
->sessiond_uuid
.value
, sessiond_uuid
, sizeof(lttng_uuid
));
4852 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4853 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4854 DBG("Received session daemon UUID: %s", uuid_str
);
4859 enum lttcomm_return_code
lttng_consumer_create_trace_chunk(
4860 const uint64_t *relayd_id
, uint64_t session_id
,
4862 time_t chunk_creation_timestamp
,
4863 const char *chunk_override_name
,
4864 const struct lttng_credentials
*credentials
,
4865 struct lttng_directory_handle
*chunk_directory_handle
)
4868 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4869 struct lttng_trace_chunk
*created_chunk
= NULL
, *published_chunk
= NULL
;
4870 enum lttng_trace_chunk_status chunk_status
;
4871 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4872 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4873 const char *relayd_id_str
= "(none)";
4874 const char *creation_timestamp_str
;
4875 struct lttng_ht_iter iter
;
4876 struct lttng_consumer_channel
*channel
;
4879 /* Only used for logging purposes. */
4880 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4881 "%" PRIu64
, *relayd_id
);
4882 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4883 relayd_id_str
= relayd_id_buffer
;
4885 relayd_id_str
= "(formatting error)";
4889 /* Local protocol error. */
4890 assert(chunk_creation_timestamp
);
4891 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4892 creation_timestamp_buffer
,
4893 sizeof(creation_timestamp_buffer
));
4894 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
4895 "(formatting error)";
4897 DBG("Consumer create trace chunk command: relay_id = %s"
4898 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4899 ", chunk_override_name = %s"
4900 ", chunk_creation_timestamp = %s",
4901 relayd_id_str
, session_id
, chunk_id
,
4902 chunk_override_name
? : "(none)",
4903 creation_timestamp_str
);
4906 * The trace chunk registry, as used by the consumer daemon, implicitly
4907 * owns the trace chunks. This is only needed in the consumer since
4908 * the consumer has no notion of a session beyond session IDs being
4909 * used to identify other objects.
4911 * The lttng_trace_chunk_registry_publish() call below provides a
4912 * reference which is not released; it implicitly becomes the session
4913 * daemon's reference to the chunk in the consumer daemon.
4915 * The lifetime of trace chunks in the consumer daemon is managed by
4916 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4917 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4919 created_chunk
= lttng_trace_chunk_create(chunk_id
,
4920 chunk_creation_timestamp
, NULL
);
4921 if (!created_chunk
) {
4922 ERR("Failed to create trace chunk");
4923 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4927 if (chunk_override_name
) {
4928 chunk_status
= lttng_trace_chunk_override_name(created_chunk
,
4929 chunk_override_name
);
4930 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4931 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4936 if (chunk_directory_handle
) {
4937 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
,
4939 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4940 ERR("Failed to set trace chunk credentials");
4941 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4945 * The consumer daemon has no ownership of the chunk output
4948 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
,
4949 chunk_directory_handle
);
4950 chunk_directory_handle
= NULL
;
4951 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4952 ERR("Failed to set trace chunk's directory handle");
4953 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4958 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4959 consumer_data
.chunk_registry
, session_id
,
4961 lttng_trace_chunk_put(created_chunk
);
4962 created_chunk
= NULL
;
4963 if (!published_chunk
) {
4964 ERR("Failed to publish trace chunk");
4965 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4970 cds_lfht_for_each_entry_duplicate(consumer_data
.channels_by_session_id_ht
->ht
,
4971 consumer_data
.channels_by_session_id_ht
->hash_fct(
4972 &session_id
, lttng_ht_seed
),
4973 consumer_data
.channels_by_session_id_ht
->match_fct
,
4974 &session_id
, &iter
.iter
, channel
,
4975 channels_by_session_id_ht_node
.node
) {
4976 ret
= lttng_consumer_channel_set_trace_chunk(channel
,
4980 * Roll-back the creation of this chunk.
4982 * This is important since the session daemon will
4983 * assume that the creation of this chunk failed and
4984 * will never ask for it to be closed, resulting
4985 * in a leak and an inconsistent state for some
4988 enum lttcomm_return_code close_ret
;
4989 char path
[LTTNG_PATH_MAX
];
4991 DBG("Failed to set new trace chunk on existing channels, rolling back");
4992 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4993 session_id
, chunk_id
,
4994 chunk_creation_timestamp
, NULL
,
4996 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4997 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4998 session_id
, chunk_id
);
5001 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
5007 struct consumer_relayd_sock_pair
*relayd
;
5009 relayd
= consumer_find_relayd(*relayd_id
);
5011 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5012 ret
= relayd_create_trace_chunk(
5013 &relayd
->control_sock
, published_chunk
);
5014 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5016 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
5019 if (!relayd
|| ret
) {
5020 enum lttcomm_return_code close_ret
;
5021 char path
[LTTNG_PATH_MAX
];
5023 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
5026 chunk_creation_timestamp
,
5028 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
5029 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
5034 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
5041 /* Release the reference returned by the "publish" operation. */
5042 lttng_trace_chunk_put(published_chunk
);
5043 lttng_trace_chunk_put(created_chunk
);
5047 enum lttcomm_return_code
lttng_consumer_close_trace_chunk(
5048 const uint64_t *relayd_id
, uint64_t session_id
,
5049 uint64_t chunk_id
, time_t chunk_close_timestamp
,
5050 const enum lttng_trace_chunk_command_type
*close_command
,
5053 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
5054 struct lttng_trace_chunk
*chunk
;
5055 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
5056 const char *relayd_id_str
= "(none)";
5057 const char *close_command_name
= "none";
5058 struct lttng_ht_iter iter
;
5059 struct lttng_consumer_channel
*channel
;
5060 enum lttng_trace_chunk_status chunk_status
;
5065 /* Only used for logging purposes. */
5066 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
5067 "%" PRIu64
, *relayd_id
);
5068 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
5069 relayd_id_str
= relayd_id_buffer
;
5071 relayd_id_str
= "(formatting error)";
5074 if (close_command
) {
5075 close_command_name
= lttng_trace_chunk_command_type_get_name(
5079 DBG("Consumer close trace chunk command: relayd_id = %s"
5080 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
5081 ", close command = %s",
5082 relayd_id_str
, session_id
, chunk_id
,
5083 close_command_name
);
5085 chunk
= lttng_trace_chunk_registry_find_chunk(
5086 consumer_data
.chunk_registry
, session_id
, chunk_id
);
5088 ERR("Failed to find chunk: session_id = %" PRIu64
5089 ", chunk_id = %" PRIu64
,
5090 session_id
, chunk_id
);
5091 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5095 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
,
5096 chunk_close_timestamp
);
5097 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
5098 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5102 if (close_command
) {
5103 chunk_status
= lttng_trace_chunk_set_close_command(
5104 chunk
, *close_command
);
5105 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
5106 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5112 * chunk is now invalid to access as we no longer hold a reference to
5113 * it; it is only kept around to compare it (by address) to the
5114 * current chunk found in the session's channels.
5117 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
,
5118 channel
, node
.node
) {
5122 * Only change the channel's chunk to NULL if it still
5123 * references the chunk being closed. The channel may
5124 * reference a newer channel in the case of a session
5125 * rotation. When a session rotation occurs, the "next"
5126 * chunk is created before the "current" chunk is closed.
5128 if (channel
->trace_chunk
!= chunk
) {
5131 ret
= lttng_consumer_channel_set_trace_chunk(channel
, NULL
);
5134 * Attempt to close the chunk on as many channels as
5137 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5143 struct consumer_relayd_sock_pair
*relayd
;
5145 relayd
= consumer_find_relayd(*relayd_id
);
5147 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5148 ret
= relayd_close_trace_chunk(
5149 &relayd
->control_sock
, chunk
,
5151 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5153 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
,
5157 if (!relayd
|| ret
) {
5158 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5166 * Release the reference returned by the "find" operation and
5167 * the session daemon's implicit reference to the chunk.
5169 lttng_trace_chunk_put(chunk
);
5170 lttng_trace_chunk_put(chunk
);
5175 enum lttcomm_return_code
lttng_consumer_trace_chunk_exists(
5176 const uint64_t *relayd_id
, uint64_t session_id
,
5180 enum lttcomm_return_code ret_code
;
5181 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
5182 const char *relayd_id_str
= "(none)";
5183 const bool is_local_trace
= !relayd_id
;
5184 struct consumer_relayd_sock_pair
*relayd
= NULL
;
5185 bool chunk_exists_local
, chunk_exists_remote
;
5190 /* Only used for logging purposes. */
5191 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
5192 "%" PRIu64
, *relayd_id
);
5193 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
5194 relayd_id_str
= relayd_id_buffer
;
5196 relayd_id_str
= "(formatting error)";
5200 DBG("Consumer trace chunk exists command: relayd_id = %s"
5201 ", chunk_id = %" PRIu64
, relayd_id_str
,
5203 ret
= lttng_trace_chunk_registry_chunk_exists(
5204 consumer_data
.chunk_registry
, session_id
,
5205 chunk_id
, &chunk_exists_local
);
5207 /* Internal error. */
5208 ERR("Failed to query the existence of a trace chunk");
5209 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
5212 DBG("Trace chunk %s locally",
5213 chunk_exists_local
? "exists" : "does not exist");
5214 if (chunk_exists_local
) {
5215 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
5217 } else if (is_local_trace
) {
5218 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5223 relayd
= consumer_find_relayd(*relayd_id
);
5225 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
5226 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5227 goto end_rcu_unlock
;
5229 DBG("Looking up existence of trace chunk on relay daemon");
5230 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5231 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
,
5232 &chunk_exists_remote
);
5233 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5235 ERR("Failed to look-up the existence of trace chunk on relay daemon");
5236 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
5237 goto end_rcu_unlock
;
5240 ret_code
= chunk_exists_remote
?
5241 LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
5242 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5243 DBG("Trace chunk %s on relay daemon",
5244 chunk_exists_remote
? "exists" : "does not exist");
5253 int consumer_clear_monitored_channel(struct lttng_consumer_channel
*channel
)
5255 struct lttng_ht
*ht
;
5256 struct lttng_consumer_stream
*stream
;
5257 struct lttng_ht_iter iter
;
5260 ht
= consumer_data
.stream_per_chan_id_ht
;
5263 cds_lfht_for_each_entry_duplicate(ht
->ht
,
5264 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
5265 ht
->match_fct
, &channel
->key
,
5266 &iter
.iter
, stream
, node_channel_id
.node
) {
5268 * Protect against teardown with mutex.
5270 pthread_mutex_lock(&stream
->lock
);
5271 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5274 ret
= consumer_clear_stream(stream
);
5279 pthread_mutex_unlock(&stream
->lock
);
5282 return LTTCOMM_CONSUMERD_SUCCESS
;
5285 pthread_mutex_unlock(&stream
->lock
);
5290 int lttng_consumer_clear_channel(struct lttng_consumer_channel
*channel
)
5294 DBG("Consumer clear channel %" PRIu64
, channel
->key
);
5296 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
5298 * Nothing to do for the metadata channel/stream.
5299 * Snapshot mechanism already take care of the metadata
5300 * handling/generation, and monitored channels only need to
5301 * have their data stream cleared..
5303 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5307 if (!channel
->monitor
) {
5308 ret
= consumer_clear_unmonitored_channel(channel
);
5310 ret
= consumer_clear_monitored_channel(channel
);
5316 enum lttcomm_return_code
lttng_consumer_open_channel_packets(
5317 struct lttng_consumer_channel
*channel
)
5319 struct lttng_consumer_stream
*stream
;
5320 enum lttcomm_return_code ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5322 if (channel
->metadata_stream
) {
5323 ERR("Open channel packets command attempted on a metadata channel");
5324 ret
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5329 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
5330 enum open_packet_status status
;
5332 pthread_mutex_lock(&stream
->lock
);
5333 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5337 status
= open_packet(stream
);
5339 case OPEN_PACKET_STATUS_OPENED
:
5340 DBG("Opened a packet in \"open channel packets\" command: stream id = %" PRIu64
5341 ", channel name = %s, session id = %" PRIu64
,
5342 stream
->key
, stream
->chan
->name
,
5343 stream
->chan
->session_id
);
5344 stream
->opened_packet_in_current_trace_chunk
= true;
5346 case OPEN_PACKET_STATUS_NO_SPACE
:
5347 DBG("No space left to open a packet in \"open channel packets\" command: stream id = %" PRIu64
5348 ", channel name = %s, session id = %" PRIu64
,
5349 stream
->key
, stream
->chan
->name
,
5350 stream
->chan
->session_id
);
5352 case OPEN_PACKET_STATUS_ERROR
:
5354 * Only unexpected internal errors can lead to this
5355 * failing. Report an unknown error.
5357 ERR("Failed to flush empty buffer in \"open channel packets\" command: stream id = %" PRIu64
5358 ", channel id = %" PRIu64
5359 ", channel name = %s"
5360 ", session id = %" PRIu64
,
5361 stream
->key
, channel
->key
,
5362 channel
->name
, channel
->session_id
);
5363 ret
= LTTCOMM_CONSUMERD_UNKNOWN_ERROR
;
5370 pthread_mutex_unlock(&stream
->lock
);
5379 pthread_mutex_unlock(&stream
->lock
);
5380 goto end_rcu_unlock
;