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
, int cmd
)
1277 if (ctx
->consumer_error_socket
> 0) {
1278 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1279 sizeof(enum lttcomm_sessiond_command
));
1286 * Close all the tracefiles and stream fds and MUST be called when all
1287 * instances are destroyed i.e. when all threads were joined and are ended.
1289 void lttng_consumer_cleanup(void)
1291 struct lttng_ht_iter iter
;
1292 struct lttng_consumer_channel
*channel
;
1293 unsigned int trace_chunks_left
;
1297 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1299 consumer_del_channel(channel
);
1304 lttng_ht_destroy(consumer_data
.channel_ht
);
1305 lttng_ht_destroy(consumer_data
.channels_by_session_id_ht
);
1307 cleanup_relayd_ht();
1309 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1312 * This HT contains streams that are freed by either the metadata thread or
1313 * the data thread so we do *nothing* on the hash table and simply destroy
1316 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1319 * Trace chunks in the registry may still exist if the session
1320 * daemon has encountered an internal error and could not
1321 * tear down its sessions and/or trace chunks properly.
1323 * Release the session daemon's implicit reference to any remaining
1324 * trace chunk and print an error if any trace chunk was found. Note
1325 * that there are _no_ legitimate cases for trace chunks to be left,
1326 * it is a leak. However, it can happen following a crash of the
1327 * session daemon and not emptying the registry would cause an assertion
1330 trace_chunks_left
= lttng_trace_chunk_registry_put_each_chunk(
1331 consumer_data
.chunk_registry
);
1332 if (trace_chunks_left
) {
1333 ERR("%u trace chunks are leaked by lttng-consumerd. "
1334 "This can be caused by an internal error of the session daemon.",
1337 /* Run all callbacks freeing each chunk. */
1339 lttng_trace_chunk_registry_destroy(consumer_data
.chunk_registry
);
1343 * Called from signal handler.
1345 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1349 CMM_STORE_SHARED(consumer_quit
, 1);
1350 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1352 PERROR("write consumer quit");
1355 DBG("Consumer flag that it should quit");
1360 * Flush pending writes to trace output disk file.
1363 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1367 int outfd
= stream
->out_fd
;
1370 * This does a blocking write-and-wait on any page that belongs to the
1371 * subbuffer prior to the one we just wrote.
1372 * Don't care about error values, as these are just hints and ways to
1373 * limit the amount of page cache used.
1375 if (orig_offset
< stream
->max_sb_size
) {
1378 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1379 stream
->max_sb_size
,
1380 SYNC_FILE_RANGE_WAIT_BEFORE
1381 | SYNC_FILE_RANGE_WRITE
1382 | SYNC_FILE_RANGE_WAIT_AFTER
);
1384 * Give hints to the kernel about how we access the file:
1385 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1388 * We need to call fadvise again after the file grows because the
1389 * kernel does not seem to apply fadvise to non-existing parts of the
1392 * Call fadvise _after_ having waited for the page writeback to
1393 * complete because the dirty page writeback semantic is not well
1394 * defined. So it can be expected to lead to lower throughput in
1397 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1398 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1399 if (ret
&& ret
!= -ENOSYS
) {
1401 PERROR("posix_fadvise on fd %i", outfd
);
1406 * Initialise the necessary environnement :
1407 * - create a new context
1408 * - create the poll_pipe
1409 * - create the should_quit pipe (for signal handler)
1410 * - create the thread pipe (for splice)
1412 * Takes a function pointer as argument, this function is called when data is
1413 * available on a buffer. This function is responsible to do the
1414 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1415 * buffer configuration and then kernctl_put_next_subbuf at the end.
1417 * Returns a pointer to the new context or NULL on error.
1419 struct lttng_consumer_local_data
*lttng_consumer_create(
1420 enum lttng_consumer_type type
,
1421 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1422 struct lttng_consumer_local_data
*ctx
, bool locked_by_caller
),
1423 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1424 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1425 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1428 struct lttng_consumer_local_data
*ctx
;
1430 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1431 consumer_data
.type
== type
);
1432 consumer_data
.type
= type
;
1434 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1436 PERROR("allocating context");
1440 ctx
->consumer_error_socket
= -1;
1441 ctx
->consumer_metadata_socket
= -1;
1442 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1443 /* assign the callbacks */
1444 ctx
->on_buffer_ready
= buffer_ready
;
1445 ctx
->on_recv_channel
= recv_channel
;
1446 ctx
->on_recv_stream
= recv_stream
;
1447 ctx
->on_update_stream
= update_stream
;
1449 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1450 if (!ctx
->consumer_data_pipe
) {
1451 goto error_poll_pipe
;
1454 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1455 if (!ctx
->consumer_wakeup_pipe
) {
1456 goto error_wakeup_pipe
;
1459 ret
= pipe(ctx
->consumer_should_quit
);
1461 PERROR("Error creating recv pipe");
1462 goto error_quit_pipe
;
1465 ret
= pipe(ctx
->consumer_channel_pipe
);
1467 PERROR("Error creating channel pipe");
1468 goto error_channel_pipe
;
1471 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1472 if (!ctx
->consumer_metadata_pipe
) {
1473 goto error_metadata_pipe
;
1476 ctx
->channel_monitor_pipe
= -1;
1480 error_metadata_pipe
:
1481 utils_close_pipe(ctx
->consumer_channel_pipe
);
1483 utils_close_pipe(ctx
->consumer_should_quit
);
1485 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1487 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1495 * Iterate over all streams of the hashtable and free them properly.
1497 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1499 struct lttng_ht_iter iter
;
1500 struct lttng_consumer_stream
*stream
;
1507 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1509 * Ignore return value since we are currently cleaning up so any error
1512 (void) consumer_del_stream(stream
, ht
);
1516 lttng_ht_destroy(ht
);
1520 * Iterate over all streams of the metadata hashtable and free them
1523 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1525 struct lttng_ht_iter iter
;
1526 struct lttng_consumer_stream
*stream
;
1533 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1535 * Ignore return value since we are currently cleaning up so any error
1538 (void) consumer_del_metadata_stream(stream
, ht
);
1542 lttng_ht_destroy(ht
);
1546 * Close all fds associated with the instance and free the context.
1548 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1552 DBG("Consumer destroying it. Closing everything.");
1558 destroy_data_stream_ht(data_ht
);
1559 destroy_metadata_stream_ht(metadata_ht
);
1561 ret
= close(ctx
->consumer_error_socket
);
1565 ret
= close(ctx
->consumer_metadata_socket
);
1569 utils_close_pipe(ctx
->consumer_channel_pipe
);
1570 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1571 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1572 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1573 utils_close_pipe(ctx
->consumer_should_quit
);
1575 unlink(ctx
->consumer_command_sock_path
);
1580 * Write the metadata stream id on the specified file descriptor.
1582 static int write_relayd_metadata_id(int fd
,
1583 struct lttng_consumer_stream
*stream
,
1584 unsigned long padding
)
1587 struct lttcomm_relayd_metadata_payload hdr
;
1589 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1590 hdr
.padding_size
= htobe32(padding
);
1591 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1592 if (ret
< sizeof(hdr
)) {
1594 * This error means that the fd's end is closed so ignore the PERROR
1595 * not to clubber the error output since this can happen in a normal
1598 if (errno
!= EPIPE
) {
1599 PERROR("write metadata stream id");
1601 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1603 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1604 * handle writting the missing part so report that as an error and
1605 * don't lie to the caller.
1610 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1611 stream
->relayd_stream_id
, padding
);
1618 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1619 * core function for writing trace buffers to either the local filesystem or
1622 * It must be called with the stream and the channel lock held.
1624 * Careful review MUST be put if any changes occur!
1626 * Returns the number of bytes written
1628 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1629 struct lttng_consumer_stream
*stream
,
1630 const struct lttng_buffer_view
*buffer
,
1631 unsigned long padding
)
1634 off_t orig_offset
= stream
->out_fd_offset
;
1635 /* Default is on the disk */
1636 int outfd
= stream
->out_fd
;
1637 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1638 unsigned int relayd_hang_up
= 0;
1639 const size_t subbuf_content_size
= buffer
->size
- padding
;
1642 /* RCU lock for the relayd pointer */
1644 assert(stream
->net_seq_idx
!= (uint64_t) -1ULL ||
1645 stream
->trace_chunk
);
1647 /* Flag that the current stream if set for network streaming. */
1648 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1649 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1650 if (relayd
== NULL
) {
1656 /* Handle stream on the relayd if the output is on the network */
1658 unsigned long netlen
= subbuf_content_size
;
1661 * Lock the control socket for the complete duration of the function
1662 * since from this point on we will use the socket.
1664 if (stream
->metadata_flag
) {
1665 /* Metadata requires the control socket. */
1666 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1667 if (stream
->reset_metadata_flag
) {
1668 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1669 stream
->relayd_stream_id
,
1670 stream
->metadata_version
);
1675 stream
->reset_metadata_flag
= 0;
1677 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1680 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1685 /* Use the returned socket. */
1688 /* Write metadata stream id before payload */
1689 if (stream
->metadata_flag
) {
1690 ret
= write_relayd_metadata_id(outfd
, stream
, padding
);
1697 write_len
= subbuf_content_size
;
1699 /* No streaming; we have to write the full padding. */
1700 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1701 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1703 ERR("Reset metadata file");
1706 stream
->reset_metadata_flag
= 0;
1710 * Check if we need to change the tracefile before writing the packet.
1712 if (stream
->chan
->tracefile_size
> 0 &&
1713 (stream
->tracefile_size_current
+ buffer
->size
) >
1714 stream
->chan
->tracefile_size
) {
1715 ret
= consumer_stream_rotate_output_files(stream
);
1719 outfd
= stream
->out_fd
;
1722 stream
->tracefile_size_current
+= buffer
->size
;
1723 write_len
= buffer
->size
;
1727 * This call guarantee that len or less is returned. It's impossible to
1728 * receive a ret value that is bigger than len.
1730 ret
= lttng_write(outfd
, buffer
->data
, write_len
);
1731 DBG("Consumer mmap write() ret %zd (len %zu)", ret
, write_len
);
1732 if (ret
< 0 || ((size_t) ret
!= write_len
)) {
1734 * Report error to caller if nothing was written else at least send the
1742 /* Socket operation failed. We consider the relayd dead */
1743 if (errno
== EPIPE
) {
1745 * This is possible if the fd is closed on the other side
1746 * (outfd) or any write problem. It can be verbose a bit for a
1747 * normal execution if for instance the relayd is stopped
1748 * abruptly. This can happen so set this to a DBG statement.
1750 DBG("Consumer mmap write detected relayd hang up");
1752 /* Unhandled error, print it and stop function right now. */
1753 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret
,
1758 stream
->output_written
+= ret
;
1760 /* This call is useless on a socket so better save a syscall. */
1762 /* This won't block, but will start writeout asynchronously */
1763 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, write_len
,
1764 SYNC_FILE_RANGE_WRITE
);
1765 stream
->out_fd_offset
+= write_len
;
1766 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1771 * This is a special case that the relayd has closed its socket. Let's
1772 * cleanup the relayd object and all associated streams.
1774 if (relayd
&& relayd_hang_up
) {
1775 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1776 lttng_consumer_cleanup_relayd(relayd
);
1780 /* Unlock only if ctrl socket used */
1781 if (relayd
&& stream
->metadata_flag
) {
1782 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1790 * Splice the data from the ring buffer to the tracefile.
1792 * It must be called with the stream lock held.
1794 * Returns the number of bytes spliced.
1796 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1797 struct lttng_consumer_local_data
*ctx
,
1798 struct lttng_consumer_stream
*stream
, unsigned long len
,
1799 unsigned long padding
)
1801 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1803 off_t orig_offset
= stream
->out_fd_offset
;
1804 int fd
= stream
->wait_fd
;
1805 /* Default is on the disk */
1806 int outfd
= stream
->out_fd
;
1807 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1809 unsigned int relayd_hang_up
= 0;
1811 switch (consumer_data
.type
) {
1812 case LTTNG_CONSUMER_KERNEL
:
1814 case LTTNG_CONSUMER32_UST
:
1815 case LTTNG_CONSUMER64_UST
:
1816 /* Not supported for user space tracing */
1819 ERR("Unknown consumer_data type");
1823 /* RCU lock for the relayd pointer */
1826 /* Flag that the current stream if set for network streaming. */
1827 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1828 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1829 if (relayd
== NULL
) {
1834 splice_pipe
= stream
->splice_pipe
;
1836 /* Write metadata stream id before payload */
1838 unsigned long total_len
= len
;
1840 if (stream
->metadata_flag
) {
1842 * Lock the control socket for the complete duration of the function
1843 * since from this point on we will use the socket.
1845 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1847 if (stream
->reset_metadata_flag
) {
1848 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1849 stream
->relayd_stream_id
,
1850 stream
->metadata_version
);
1855 stream
->reset_metadata_flag
= 0;
1857 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
,
1865 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1868 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1874 /* Use the returned socket. */
1877 /* No streaming, we have to set the len with the full padding */
1880 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1881 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1883 ERR("Reset metadata file");
1886 stream
->reset_metadata_flag
= 0;
1889 * Check if we need to change the tracefile before writing the packet.
1891 if (stream
->chan
->tracefile_size
> 0 &&
1892 (stream
->tracefile_size_current
+ len
) >
1893 stream
->chan
->tracefile_size
) {
1894 ret
= consumer_stream_rotate_output_files(stream
);
1899 outfd
= stream
->out_fd
;
1902 stream
->tracefile_size_current
+= len
;
1906 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1907 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1908 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1909 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1910 DBG("splice chan to pipe, ret %zd", ret_splice
);
1911 if (ret_splice
< 0) {
1914 PERROR("Error in relay splice");
1918 /* Handle stream on the relayd if the output is on the network */
1919 if (relayd
&& stream
->metadata_flag
) {
1920 size_t metadata_payload_size
=
1921 sizeof(struct lttcomm_relayd_metadata_payload
);
1923 /* Update counter to fit the spliced data */
1924 ret_splice
+= metadata_payload_size
;
1925 len
+= metadata_payload_size
;
1927 * We do this so the return value can match the len passed as
1928 * argument to this function.
1930 written
-= metadata_payload_size
;
1933 /* Splice data out */
1934 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1935 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1936 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1938 if (ret_splice
< 0) {
1943 } else if (ret_splice
> len
) {
1945 * We don't expect this code path to be executed but you never know
1946 * so this is an extra protection agains a buggy splice().
1949 written
+= ret_splice
;
1950 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1954 /* All good, update current len and continue. */
1958 /* This call is useless on a socket so better save a syscall. */
1960 /* This won't block, but will start writeout asynchronously */
1961 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1962 SYNC_FILE_RANGE_WRITE
);
1963 stream
->out_fd_offset
+= ret_splice
;
1965 stream
->output_written
+= ret_splice
;
1966 written
+= ret_splice
;
1969 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1975 * This is a special case that the relayd has closed its socket. Let's
1976 * cleanup the relayd object and all associated streams.
1978 if (relayd
&& relayd_hang_up
) {
1979 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1980 lttng_consumer_cleanup_relayd(relayd
);
1981 /* Skip splice error so the consumer does not fail */
1986 /* send the appropriate error description to sessiond */
1989 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1992 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1995 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
2000 if (relayd
&& stream
->metadata_flag
) {
2001 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2009 * Sample the snapshot positions for a specific fd
2011 * Returns 0 on success, < 0 on error
2013 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
2015 switch (consumer_data
.type
) {
2016 case LTTNG_CONSUMER_KERNEL
:
2017 return lttng_kconsumer_sample_snapshot_positions(stream
);
2018 case LTTNG_CONSUMER32_UST
:
2019 case LTTNG_CONSUMER64_UST
:
2020 return lttng_ustconsumer_sample_snapshot_positions(stream
);
2022 ERR("Unknown consumer_data type");
2028 * Take a snapshot for a specific fd
2030 * Returns 0 on success, < 0 on error
2032 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2034 switch (consumer_data
.type
) {
2035 case LTTNG_CONSUMER_KERNEL
:
2036 return lttng_kconsumer_take_snapshot(stream
);
2037 case LTTNG_CONSUMER32_UST
:
2038 case LTTNG_CONSUMER64_UST
:
2039 return lttng_ustconsumer_take_snapshot(stream
);
2041 ERR("Unknown consumer_data type");
2048 * Get the produced position
2050 * Returns 0 on success, < 0 on error
2052 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
2055 switch (consumer_data
.type
) {
2056 case LTTNG_CONSUMER_KERNEL
:
2057 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
2058 case LTTNG_CONSUMER32_UST
:
2059 case LTTNG_CONSUMER64_UST
:
2060 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2062 ERR("Unknown consumer_data type");
2069 * Get the consumed position (free-running counter position in bytes).
2071 * Returns 0 on success, < 0 on error
2073 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
2076 switch (consumer_data
.type
) {
2077 case LTTNG_CONSUMER_KERNEL
:
2078 return lttng_kconsumer_get_consumed_snapshot(stream
, pos
);
2079 case LTTNG_CONSUMER32_UST
:
2080 case LTTNG_CONSUMER64_UST
:
2081 return lttng_ustconsumer_get_consumed_snapshot(stream
, pos
);
2083 ERR("Unknown consumer_data type");
2089 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2090 int sock
, struct pollfd
*consumer_sockpoll
)
2092 switch (consumer_data
.type
) {
2093 case LTTNG_CONSUMER_KERNEL
:
2094 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2095 case LTTNG_CONSUMER32_UST
:
2096 case LTTNG_CONSUMER64_UST
:
2097 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2099 ERR("Unknown consumer_data type");
2106 void lttng_consumer_close_all_metadata(void)
2108 switch (consumer_data
.type
) {
2109 case LTTNG_CONSUMER_KERNEL
:
2111 * The Kernel consumer has a different metadata scheme so we don't
2112 * close anything because the stream will be closed by the session
2116 case LTTNG_CONSUMER32_UST
:
2117 case LTTNG_CONSUMER64_UST
:
2119 * Close all metadata streams. The metadata hash table is passed and
2120 * this call iterates over it by closing all wakeup fd. This is safe
2121 * because at this point we are sure that the metadata producer is
2122 * either dead or blocked.
2124 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2127 ERR("Unknown consumer_data type");
2133 * Clean up a metadata stream and free its memory.
2135 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2136 struct lttng_ht
*ht
)
2138 struct lttng_consumer_channel
*channel
= NULL
;
2139 bool free_channel
= false;
2143 * This call should NEVER receive regular stream. It must always be
2144 * metadata stream and this is crucial for data structure synchronization.
2146 assert(stream
->metadata_flag
);
2148 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2150 pthread_mutex_lock(&consumer_data
.lock
);
2152 * Note that this assumes that a stream's channel is never changed and
2153 * that the stream's lock doesn't need to be taken to sample its
2156 channel
= stream
->chan
;
2157 pthread_mutex_lock(&channel
->lock
);
2158 pthread_mutex_lock(&stream
->lock
);
2159 if (channel
->metadata_cache
) {
2160 /* Only applicable to userspace consumers. */
2161 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
2164 /* Remove any reference to that stream. */
2165 consumer_stream_delete(stream
, ht
);
2167 /* Close down everything including the relayd if one. */
2168 consumer_stream_close(stream
);
2169 /* Destroy tracer buffers of the stream. */
2170 consumer_stream_destroy_buffers(stream
);
2172 /* Atomically decrement channel refcount since other threads can use it. */
2173 if (!uatomic_sub_return(&channel
->refcount
, 1)
2174 && !uatomic_read(&channel
->nb_init_stream_left
)) {
2175 /* Go for channel deletion! */
2176 free_channel
= true;
2178 stream
->chan
= NULL
;
2181 * Nullify the stream reference so it is not used after deletion. The
2182 * channel lock MUST be acquired before being able to check for a NULL
2185 channel
->metadata_stream
= NULL
;
2187 if (channel
->metadata_cache
) {
2188 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
2190 pthread_mutex_unlock(&stream
->lock
);
2191 pthread_mutex_unlock(&channel
->lock
);
2192 pthread_mutex_unlock(&consumer_data
.lock
);
2195 consumer_del_channel(channel
);
2198 lttng_trace_chunk_put(stream
->trace_chunk
);
2199 stream
->trace_chunk
= NULL
;
2200 consumer_stream_free(stream
);
2204 * Action done with the metadata stream when adding it to the consumer internal
2205 * data structures to handle it.
2207 void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2209 struct lttng_ht
*ht
= metadata_ht
;
2210 struct lttng_ht_iter iter
;
2211 struct lttng_ht_node_u64
*node
;
2216 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2218 pthread_mutex_lock(&consumer_data
.lock
);
2219 pthread_mutex_lock(&stream
->chan
->lock
);
2220 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2221 pthread_mutex_lock(&stream
->lock
);
2224 * From here, refcounts are updated so be _careful_ when returning an error
2231 * Lookup the stream just to make sure it does not exist in our internal
2232 * state. This should NEVER happen.
2234 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2235 node
= lttng_ht_iter_get_node_u64(&iter
);
2239 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2240 * in terms of destroying the associated channel, because the action that
2241 * causes the count to become 0 also causes a stream to be added. The
2242 * channel deletion will thus be triggered by the following removal of this
2245 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2246 /* Increment refcount before decrementing nb_init_stream_left */
2248 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2251 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2253 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
2254 &stream
->node_channel_id
);
2257 * Add stream to the stream_list_ht of the consumer data. No need to steal
2258 * the key since the HT does not use it and we allow to add redundant keys
2261 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2265 pthread_mutex_unlock(&stream
->lock
);
2266 pthread_mutex_unlock(&stream
->chan
->lock
);
2267 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2268 pthread_mutex_unlock(&consumer_data
.lock
);
2272 * Delete data stream that are flagged for deletion (endpoint_status).
2274 static void validate_endpoint_status_data_stream(void)
2276 struct lttng_ht_iter iter
;
2277 struct lttng_consumer_stream
*stream
;
2279 DBG("Consumer delete flagged data stream");
2282 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2283 /* Validate delete flag of the stream */
2284 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2287 /* Delete it right now */
2288 consumer_del_stream(stream
, data_ht
);
2294 * Delete metadata stream that are flagged for deletion (endpoint_status).
2296 static void validate_endpoint_status_metadata_stream(
2297 struct lttng_poll_event
*pollset
)
2299 struct lttng_ht_iter iter
;
2300 struct lttng_consumer_stream
*stream
;
2302 DBG("Consumer delete flagged metadata stream");
2307 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2308 /* Validate delete flag of the stream */
2309 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2313 * Remove from pollset so the metadata thread can continue without
2314 * blocking on a deleted stream.
2316 lttng_poll_del(pollset
, stream
->wait_fd
);
2318 /* Delete it right now */
2319 consumer_del_metadata_stream(stream
, metadata_ht
);
2325 * Thread polls on metadata file descriptor and write them on disk or on the
2328 void *consumer_thread_metadata_poll(void *data
)
2330 int ret
, i
, pollfd
, err
= -1;
2331 uint32_t revents
, nb_fd
;
2332 struct lttng_consumer_stream
*stream
= NULL
;
2333 struct lttng_ht_iter iter
;
2334 struct lttng_ht_node_u64
*node
;
2335 struct lttng_poll_event events
;
2336 struct lttng_consumer_local_data
*ctx
= data
;
2339 rcu_register_thread();
2341 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2343 if (testpoint(consumerd_thread_metadata
)) {
2344 goto error_testpoint
;
2347 health_code_update();
2349 DBG("Thread metadata poll started");
2351 /* Size is set to 1 for the consumer_metadata pipe */
2352 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2354 ERR("Poll set creation failed");
2358 ret
= lttng_poll_add(&events
,
2359 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2365 DBG("Metadata main loop started");
2369 health_code_update();
2370 health_poll_entry();
2371 DBG("Metadata poll wait");
2372 ret
= lttng_poll_wait(&events
, -1);
2373 DBG("Metadata poll return from wait with %d fd(s)",
2374 LTTNG_POLL_GETNB(&events
));
2376 DBG("Metadata event caught in thread");
2378 if (errno
== EINTR
) {
2379 ERR("Poll EINTR caught");
2382 if (LTTNG_POLL_GETNB(&events
) == 0) {
2383 err
= 0; /* All is OK */
2390 /* From here, the event is a metadata wait fd */
2391 for (i
= 0; i
< nb_fd
; i
++) {
2392 health_code_update();
2394 revents
= LTTNG_POLL_GETEV(&events
, i
);
2395 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2397 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2398 if (revents
& LPOLLIN
) {
2401 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2402 &stream
, sizeof(stream
));
2403 if (pipe_len
< sizeof(stream
)) {
2405 PERROR("read metadata stream");
2408 * Remove the pipe from the poll set and continue the loop
2409 * since their might be data to consume.
2411 lttng_poll_del(&events
,
2412 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2413 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2417 /* A NULL stream means that the state has changed. */
2418 if (stream
== NULL
) {
2419 /* Check for deleted streams. */
2420 validate_endpoint_status_metadata_stream(&events
);
2424 DBG("Adding metadata stream %d to poll set",
2427 /* Add metadata stream to the global poll events list */
2428 lttng_poll_add(&events
, stream
->wait_fd
,
2429 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2430 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2431 DBG("Metadata thread pipe hung up");
2433 * Remove the pipe from the poll set and continue the loop
2434 * since their might be data to consume.
2436 lttng_poll_del(&events
,
2437 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2438 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2441 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2445 /* Handle other stream */
2451 uint64_t tmp_id
= (uint64_t) pollfd
;
2453 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2455 node
= lttng_ht_iter_get_node_u64(&iter
);
2458 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2461 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2462 /* Get the data out of the metadata file descriptor */
2463 DBG("Metadata available on fd %d", pollfd
);
2464 assert(stream
->wait_fd
== pollfd
);
2467 health_code_update();
2469 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2471 * We don't check the return value here since if we get
2472 * a negative len, it means an error occurred thus we
2473 * simply remove it from the poll set and free the
2478 /* It's ok to have an unavailable sub-buffer */
2479 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2480 /* Clean up stream from consumer and free it. */
2481 lttng_poll_del(&events
, stream
->wait_fd
);
2482 consumer_del_metadata_stream(stream
, metadata_ht
);
2484 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2485 DBG("Metadata fd %d is hup|err.", pollfd
);
2486 if (!stream
->hangup_flush_done
2487 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2488 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2489 DBG("Attempting to flush and consume the UST buffers");
2490 lttng_ustconsumer_on_stream_hangup(stream
);
2492 /* We just flushed the stream now read it. */
2494 health_code_update();
2496 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2498 * We don't check the return value here since if we get
2499 * a negative len, it means an error occurred thus we
2500 * simply remove it from the poll set and free the
2506 lttng_poll_del(&events
, stream
->wait_fd
);
2508 * This call update the channel states, closes file descriptors
2509 * and securely free the stream.
2511 consumer_del_metadata_stream(stream
, metadata_ht
);
2513 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2517 /* Release RCU lock for the stream looked up */
2525 DBG("Metadata poll thread exiting");
2527 lttng_poll_clean(&events
);
2532 ERR("Health error occurred in %s", __func__
);
2534 health_unregister(health_consumerd
);
2535 rcu_unregister_thread();
2540 * This thread polls the fds in the set to consume the data and write
2541 * it to tracefile if necessary.
2543 void *consumer_thread_data_poll(void *data
)
2545 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2546 struct pollfd
*pollfd
= NULL
;
2547 /* local view of the streams */
2548 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2549 /* local view of consumer_data.fds_count */
2551 /* 2 for the consumer_data_pipe and wake up pipe */
2552 const int nb_pipes_fd
= 2;
2553 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2554 int nb_inactive_fd
= 0;
2555 struct lttng_consumer_local_data
*ctx
= data
;
2558 rcu_register_thread();
2560 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2562 if (testpoint(consumerd_thread_data
)) {
2563 goto error_testpoint
;
2566 health_code_update();
2568 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2569 if (local_stream
== NULL
) {
2570 PERROR("local_stream malloc");
2575 health_code_update();
2581 * the fds set has been updated, we need to update our
2582 * local array as well
2584 pthread_mutex_lock(&consumer_data
.lock
);
2585 if (consumer_data
.need_update
) {
2590 local_stream
= NULL
;
2592 /* Allocate for all fds */
2593 pollfd
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) * sizeof(struct pollfd
));
2594 if (pollfd
== NULL
) {
2595 PERROR("pollfd malloc");
2596 pthread_mutex_unlock(&consumer_data
.lock
);
2600 local_stream
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) *
2601 sizeof(struct lttng_consumer_stream
*));
2602 if (local_stream
== NULL
) {
2603 PERROR("local_stream malloc");
2604 pthread_mutex_unlock(&consumer_data
.lock
);
2607 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2608 data_ht
, &nb_inactive_fd
);
2610 ERR("Error in allocating pollfd or local_outfds");
2611 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2612 pthread_mutex_unlock(&consumer_data
.lock
);
2616 consumer_data
.need_update
= 0;
2618 pthread_mutex_unlock(&consumer_data
.lock
);
2620 /* No FDs and consumer_quit, consumer_cleanup the thread */
2621 if (nb_fd
== 0 && nb_inactive_fd
== 0 &&
2622 CMM_LOAD_SHARED(consumer_quit
) == 1) {
2623 err
= 0; /* All is OK */
2626 /* poll on the array of fds */
2628 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2629 if (testpoint(consumerd_thread_data_poll
)) {
2632 health_poll_entry();
2633 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2635 DBG("poll num_rdy : %d", num_rdy
);
2636 if (num_rdy
== -1) {
2638 * Restart interrupted system call.
2640 if (errno
== EINTR
) {
2643 PERROR("Poll error");
2644 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2646 } else if (num_rdy
== 0) {
2647 DBG("Polling thread timed out");
2651 if (caa_unlikely(data_consumption_paused
)) {
2652 DBG("Data consumption paused, sleeping...");
2658 * If the consumer_data_pipe triggered poll go directly to the
2659 * beginning of the loop to update the array. We want to prioritize
2660 * array update over low-priority reads.
2662 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2663 ssize_t pipe_readlen
;
2665 DBG("consumer_data_pipe wake up");
2666 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2667 &new_stream
, sizeof(new_stream
));
2668 if (pipe_readlen
< sizeof(new_stream
)) {
2669 PERROR("Consumer data pipe");
2670 /* Continue so we can at least handle the current stream(s). */
2675 * If the stream is NULL, just ignore it. It's also possible that
2676 * the sessiond poll thread changed the consumer_quit state and is
2677 * waking us up to test it.
2679 if (new_stream
== NULL
) {
2680 validate_endpoint_status_data_stream();
2684 /* Continue to update the local streams and handle prio ones */
2688 /* Handle wakeup pipe. */
2689 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2691 ssize_t pipe_readlen
;
2693 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2695 if (pipe_readlen
< 0) {
2696 PERROR("Consumer data wakeup pipe");
2698 /* We've been awakened to handle stream(s). */
2699 ctx
->has_wakeup
= 0;
2702 /* Take care of high priority channels first. */
2703 for (i
= 0; i
< nb_fd
; i
++) {
2704 health_code_update();
2706 if (local_stream
[i
] == NULL
) {
2709 if (pollfd
[i
].revents
& POLLPRI
) {
2710 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2712 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2713 /* it's ok to have an unavailable sub-buffer */
2714 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2715 /* Clean the stream and free it. */
2716 consumer_del_stream(local_stream
[i
], data_ht
);
2717 local_stream
[i
] = NULL
;
2718 } else if (len
> 0) {
2719 local_stream
[i
]->data_read
= 1;
2725 * If we read high prio channel in this loop, try again
2726 * for more high prio data.
2732 /* Take care of low priority channels. */
2733 for (i
= 0; i
< nb_fd
; i
++) {
2734 health_code_update();
2736 if (local_stream
[i
] == NULL
) {
2739 if ((pollfd
[i
].revents
& POLLIN
) ||
2740 local_stream
[i
]->hangup_flush_done
||
2741 local_stream
[i
]->has_data
) {
2742 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2743 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2744 /* it's ok to have an unavailable sub-buffer */
2745 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2746 /* Clean the stream and free it. */
2747 consumer_del_stream(local_stream
[i
], data_ht
);
2748 local_stream
[i
] = NULL
;
2749 } else if (len
> 0) {
2750 local_stream
[i
]->data_read
= 1;
2755 /* Handle hangup and errors */
2756 for (i
= 0; i
< nb_fd
; i
++) {
2757 health_code_update();
2759 if (local_stream
[i
] == NULL
) {
2762 if (!local_stream
[i
]->hangup_flush_done
2763 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2764 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2765 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2766 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2768 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2769 /* Attempt read again, for the data we just flushed. */
2770 local_stream
[i
]->data_read
= 1;
2773 * If the poll flag is HUP/ERR/NVAL and we have
2774 * read no data in this pass, we can remove the
2775 * stream from its hash table.
2777 if ((pollfd
[i
].revents
& POLLHUP
)) {
2778 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2779 if (!local_stream
[i
]->data_read
) {
2780 consumer_del_stream(local_stream
[i
], data_ht
);
2781 local_stream
[i
] = NULL
;
2784 } else if (pollfd
[i
].revents
& POLLERR
) {
2785 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2786 if (!local_stream
[i
]->data_read
) {
2787 consumer_del_stream(local_stream
[i
], data_ht
);
2788 local_stream
[i
] = NULL
;
2791 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2792 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2793 if (!local_stream
[i
]->data_read
) {
2794 consumer_del_stream(local_stream
[i
], data_ht
);
2795 local_stream
[i
] = NULL
;
2799 if (local_stream
[i
] != NULL
) {
2800 local_stream
[i
]->data_read
= 0;
2807 DBG("polling thread exiting");
2812 * Close the write side of the pipe so epoll_wait() in
2813 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2814 * read side of the pipe. If we close them both, epoll_wait strangely does
2815 * not return and could create a endless wait period if the pipe is the
2816 * only tracked fd in the poll set. The thread will take care of closing
2819 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2824 ERR("Health error occurred in %s", __func__
);
2826 health_unregister(health_consumerd
);
2828 rcu_unregister_thread();
2833 * Close wake-up end of each stream belonging to the channel. This will
2834 * allow the poll() on the stream read-side to detect when the
2835 * write-side (application) finally closes them.
2838 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2840 struct lttng_ht
*ht
;
2841 struct lttng_consumer_stream
*stream
;
2842 struct lttng_ht_iter iter
;
2844 ht
= consumer_data
.stream_per_chan_id_ht
;
2847 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2848 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2849 ht
->match_fct
, &channel
->key
,
2850 &iter
.iter
, stream
, node_channel_id
.node
) {
2852 * Protect against teardown with mutex.
2854 pthread_mutex_lock(&stream
->lock
);
2855 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2858 switch (consumer_data
.type
) {
2859 case LTTNG_CONSUMER_KERNEL
:
2861 case LTTNG_CONSUMER32_UST
:
2862 case LTTNG_CONSUMER64_UST
:
2863 if (stream
->metadata_flag
) {
2864 /* Safe and protected by the stream lock. */
2865 lttng_ustconsumer_close_metadata(stream
->chan
);
2868 * Note: a mutex is taken internally within
2869 * liblttng-ust-ctl to protect timer wakeup_fd
2870 * use from concurrent close.
2872 lttng_ustconsumer_close_stream_wakeup(stream
);
2876 ERR("Unknown consumer_data type");
2880 pthread_mutex_unlock(&stream
->lock
);
2885 static void destroy_channel_ht(struct lttng_ht
*ht
)
2887 struct lttng_ht_iter iter
;
2888 struct lttng_consumer_channel
*channel
;
2896 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2897 ret
= lttng_ht_del(ht
, &iter
);
2902 lttng_ht_destroy(ht
);
2906 * This thread polls the channel fds to detect when they are being
2907 * closed. It closes all related streams if the channel is detected as
2908 * closed. It is currently only used as a shim layer for UST because the
2909 * consumerd needs to keep the per-stream wakeup end of pipes open for
2912 void *consumer_thread_channel_poll(void *data
)
2914 int ret
, i
, pollfd
, err
= -1;
2915 uint32_t revents
, nb_fd
;
2916 struct lttng_consumer_channel
*chan
= NULL
;
2917 struct lttng_ht_iter iter
;
2918 struct lttng_ht_node_u64
*node
;
2919 struct lttng_poll_event events
;
2920 struct lttng_consumer_local_data
*ctx
= data
;
2921 struct lttng_ht
*channel_ht
;
2923 rcu_register_thread();
2925 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2927 if (testpoint(consumerd_thread_channel
)) {
2928 goto error_testpoint
;
2931 health_code_update();
2933 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2935 /* ENOMEM at this point. Better to bail out. */
2939 DBG("Thread channel poll started");
2941 /* Size is set to 1 for the consumer_channel pipe */
2942 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2944 ERR("Poll set creation failed");
2948 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2954 DBG("Channel main loop started");
2958 health_code_update();
2959 DBG("Channel poll wait");
2960 health_poll_entry();
2961 ret
= lttng_poll_wait(&events
, -1);
2962 DBG("Channel poll return from wait with %d fd(s)",
2963 LTTNG_POLL_GETNB(&events
));
2965 DBG("Channel event caught in thread");
2967 if (errno
== EINTR
) {
2968 ERR("Poll EINTR caught");
2971 if (LTTNG_POLL_GETNB(&events
) == 0) {
2972 err
= 0; /* All is OK */
2979 /* From here, the event is a channel wait fd */
2980 for (i
= 0; i
< nb_fd
; i
++) {
2981 health_code_update();
2983 revents
= LTTNG_POLL_GETEV(&events
, i
);
2984 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2986 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2987 if (revents
& LPOLLIN
) {
2988 enum consumer_channel_action action
;
2991 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2994 ERR("Error reading channel pipe");
2996 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3001 case CONSUMER_CHANNEL_ADD
:
3002 DBG("Adding channel %d to poll set",
3005 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
3008 lttng_ht_add_unique_u64(channel_ht
,
3009 &chan
->wait_fd_node
);
3011 /* Add channel to the global poll events list */
3012 lttng_poll_add(&events
, chan
->wait_fd
,
3013 LPOLLERR
| LPOLLHUP
);
3015 case CONSUMER_CHANNEL_DEL
:
3018 * This command should never be called if the channel
3019 * has streams monitored by either the data or metadata
3020 * thread. The consumer only notify this thread with a
3021 * channel del. command if it receives a destroy
3022 * channel command from the session daemon that send it
3023 * if a command prior to the GET_CHANNEL failed.
3027 chan
= consumer_find_channel(key
);
3030 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
3033 lttng_poll_del(&events
, chan
->wait_fd
);
3034 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
3035 ret
= lttng_ht_del(channel_ht
, &iter
);
3038 switch (consumer_data
.type
) {
3039 case LTTNG_CONSUMER_KERNEL
:
3041 case LTTNG_CONSUMER32_UST
:
3042 case LTTNG_CONSUMER64_UST
:
3043 health_code_update();
3044 /* Destroy streams that might have been left in the stream list. */
3045 clean_channel_stream_list(chan
);
3048 ERR("Unknown consumer_data type");
3053 * Release our own refcount. Force channel deletion even if
3054 * streams were not initialized.
3056 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3057 consumer_del_channel(chan
);
3062 case CONSUMER_CHANNEL_QUIT
:
3064 * Remove the pipe from the poll set and continue the loop
3065 * since their might be data to consume.
3067 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3070 ERR("Unknown action");
3073 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3074 DBG("Channel thread pipe hung up");
3076 * Remove the pipe from the poll set and continue the loop
3077 * since their might be data to consume.
3079 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3082 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3086 /* Handle other stream */
3092 uint64_t tmp_id
= (uint64_t) pollfd
;
3094 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3096 node
= lttng_ht_iter_get_node_u64(&iter
);
3099 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
3102 /* Check for error event */
3103 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3104 DBG("Channel fd %d is hup|err.", pollfd
);
3106 lttng_poll_del(&events
, chan
->wait_fd
);
3107 ret
= lttng_ht_del(channel_ht
, &iter
);
3111 * This will close the wait fd for each stream associated to
3112 * this channel AND monitored by the data/metadata thread thus
3113 * will be clean by the right thread.
3115 consumer_close_channel_streams(chan
);
3117 /* Release our own refcount */
3118 if (!uatomic_sub_return(&chan
->refcount
, 1)
3119 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3120 consumer_del_channel(chan
);
3123 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3128 /* Release RCU lock for the channel looked up */
3136 lttng_poll_clean(&events
);
3138 destroy_channel_ht(channel_ht
);
3141 DBG("Channel poll thread exiting");
3144 ERR("Health error occurred in %s", __func__
);
3146 health_unregister(health_consumerd
);
3147 rcu_unregister_thread();
3151 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3152 struct pollfd
*sockpoll
, int client_socket
)
3159 ret
= lttng_consumer_poll_socket(sockpoll
);
3163 DBG("Metadata connection on client_socket");
3165 /* Blocking call, waiting for transmission */
3166 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3167 if (ctx
->consumer_metadata_socket
< 0) {
3168 WARN("On accept metadata");
3179 * This thread listens on the consumerd socket and receives the file
3180 * descriptors from the session daemon.
3182 void *consumer_thread_sessiond_poll(void *data
)
3184 int sock
= -1, client_socket
, ret
, err
= -1;
3186 * structure to poll for incoming data on communication socket avoids
3187 * making blocking sockets.
3189 struct pollfd consumer_sockpoll
[2];
3190 struct lttng_consumer_local_data
*ctx
= data
;
3192 rcu_register_thread();
3194 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3196 if (testpoint(consumerd_thread_sessiond
)) {
3197 goto error_testpoint
;
3200 health_code_update();
3202 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3203 unlink(ctx
->consumer_command_sock_path
);
3204 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3205 if (client_socket
< 0) {
3206 ERR("Cannot create command socket");
3210 ret
= lttcomm_listen_unix_sock(client_socket
);
3215 DBG("Sending ready command to lttng-sessiond");
3216 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3217 /* return < 0 on error, but == 0 is not fatal */
3219 ERR("Error sending ready command to lttng-sessiond");
3223 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3224 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3225 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3226 consumer_sockpoll
[1].fd
= client_socket
;
3227 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3229 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3237 DBG("Connection on client_socket");
3239 /* Blocking call, waiting for transmission */
3240 sock
= lttcomm_accept_unix_sock(client_socket
);
3247 * Setup metadata socket which is the second socket connection on the
3248 * command unix socket.
3250 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3259 /* This socket is not useful anymore. */
3260 ret
= close(client_socket
);
3262 PERROR("close client_socket");
3266 /* update the polling structure to poll on the established socket */
3267 consumer_sockpoll
[1].fd
= sock
;
3268 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3271 health_code_update();
3273 health_poll_entry();
3274 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3283 DBG("Incoming command on sock");
3284 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3287 * This could simply be a session daemon quitting. Don't output
3290 DBG("Communication interrupted on command socket");
3294 if (CMM_LOAD_SHARED(consumer_quit
)) {
3295 DBG("consumer_thread_receive_fds received quit from signal");
3296 err
= 0; /* All is OK */
3299 DBG("received command on sock");
3305 DBG("Consumer thread sessiond poll exiting");
3308 * Close metadata streams since the producer is the session daemon which
3311 * NOTE: for now, this only applies to the UST tracer.
3313 lttng_consumer_close_all_metadata();
3316 * when all fds have hung up, the polling thread
3319 CMM_STORE_SHARED(consumer_quit
, 1);
3322 * Notify the data poll thread to poll back again and test the
3323 * consumer_quit state that we just set so to quit gracefully.
3325 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3327 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3329 notify_health_quit_pipe(health_quit_pipe
);
3331 /* Cleaning up possibly open sockets. */
3335 PERROR("close sock sessiond poll");
3338 if (client_socket
>= 0) {
3339 ret
= close(client_socket
);
3341 PERROR("close client_socket sessiond poll");
3348 ERR("Health error occurred in %s", __func__
);
3350 health_unregister(health_consumerd
);
3352 rcu_unregister_thread();
3357 int consumer_flush_buffer(struct lttng_consumer_stream
*stream
,
3358 int producer_active
)
3362 switch (consumer_data
.type
) {
3363 case LTTNG_CONSUMER_KERNEL
:
3364 if (producer_active
) {
3365 ret
= kernctl_buffer_flush(stream
->wait_fd
);
3367 ERR("Failed to flush kernel stream");
3371 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
3374 * Doing a buffer flush which does not take into
3375 * account empty packets. This is not perfect,
3376 * but required as a fall-back when
3377 * "flush_empty" is not implemented by
3380 ret
= kernctl_buffer_flush(stream
->wait_fd
);
3382 ERR("Failed to flush kernel stream");
3388 case LTTNG_CONSUMER32_UST
:
3389 case LTTNG_CONSUMER64_UST
:
3390 lttng_ustconsumer_flush_buffer(stream
, producer_active
);
3393 ERR("Unknown consumer_data type");
3401 static enum open_packet_status
open_packet(struct lttng_consumer_stream
*stream
)
3404 enum open_packet_status status
;
3405 unsigned long produced_pos_before
, produced_pos_after
;
3407 ret
= lttng_consumer_sample_snapshot_positions(stream
);
3409 ERR("Failed to snapshot positions before post-rotation empty packet flush: stream id = %" PRIu64
3410 ", channel name = %s, session id = %" PRIu64
,
3411 stream
->key
, stream
->chan
->name
,
3412 stream
->chan
->session_id
);
3413 status
= OPEN_PACKET_STATUS_ERROR
;
3417 ret
= lttng_consumer_get_produced_snapshot(
3418 stream
, &produced_pos_before
);
3420 ERR("Failed to read produced position before post-rotation empty packet flush: stream id = %" PRIu64
3421 ", channel name = %s, session id = %" PRIu64
,
3422 stream
->key
, stream
->chan
->name
,
3423 stream
->chan
->session_id
);
3424 status
= OPEN_PACKET_STATUS_ERROR
;
3428 ret
= consumer_flush_buffer(stream
, 0);
3430 ERR("Failed to flush an empty packet at rotation point: stream id = %" PRIu64
3431 ", channel name = %s, session id = %" PRIu64
,
3432 stream
->key
, stream
->chan
->name
,
3433 stream
->chan
->session_id
);
3434 status
= OPEN_PACKET_STATUS_ERROR
;
3438 ret
= lttng_consumer_sample_snapshot_positions(stream
);
3440 ERR("Failed to snapshot positions after post-rotation empty packet flush: stream id = %" PRIu64
3441 ", channel name = %s, session id = %" PRIu64
,
3442 stream
->key
, stream
->chan
->name
,
3443 stream
->chan
->session_id
);
3444 status
= OPEN_PACKET_STATUS_ERROR
;
3448 ret
= lttng_consumer_get_produced_snapshot(stream
, &produced_pos_after
);
3450 ERR("Failed to read produced position after post-rotation empty packet flush: stream id = %" PRIu64
3451 ", channel name = %s, session id = %" PRIu64
,
3452 stream
->key
, stream
->chan
->name
,
3453 stream
->chan
->session_id
);
3454 status
= OPEN_PACKET_STATUS_ERROR
;
3459 * Determine if the flush had an effect by comparing the produced
3460 * positons before and after the flush.
3462 status
= produced_pos_before
!= produced_pos_after
?
3463 OPEN_PACKET_STATUS_OPENED
:
3464 OPEN_PACKET_STATUS_NO_SPACE
;
3465 if (status
== OPEN_PACKET_STATUS_OPENED
) {
3466 stream
->opened_packet_in_current_trace_chunk
= true;
3472 static bool stream_is_rotating_to_null_chunk(
3473 const struct lttng_consumer_stream
*stream
)
3475 bool rotating_to_null_chunk
= false;
3477 if (stream
->rotate_position
== -1ULL) {
3478 /* No rotation ongoing. */
3482 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
||
3483 !stream
->chan
->trace_chunk
) {
3484 rotating_to_null_chunk
= true;
3487 return rotating_to_null_chunk
;
3490 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3491 struct lttng_consumer_local_data
*ctx
,
3492 bool locked_by_caller
)
3494 ssize_t ret
, written_bytes
= 0;
3496 struct stream_subbuffer subbuffer
= {};
3498 if (!locked_by_caller
) {
3499 stream
->read_subbuffer_ops
.lock(stream
);
3501 stream
->read_subbuffer_ops
.assert_locked(stream
);
3504 if (stream
->read_subbuffer_ops
.on_wake_up
) {
3505 ret
= stream
->read_subbuffer_ops
.on_wake_up(stream
);
3512 * If the stream was flagged to be ready for rotation before we extract
3513 * the next packet, rotate it now.
3515 if (stream
->rotate_ready
) {
3516 DBG("Rotate stream before consuming data");
3517 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3519 ERR("Stream rotation error before consuming data");
3524 ret
= stream
->read_subbuffer_ops
.get_next_subbuffer(stream
, &subbuffer
);
3526 if (ret
== -ENODATA
) {
3534 ret
= stream
->read_subbuffer_ops
.pre_consume_subbuffer(
3535 stream
, &subbuffer
);
3537 goto error_put_subbuf
;
3540 written_bytes
= stream
->read_subbuffer_ops
.consume_subbuffer(
3541 ctx
, stream
, &subbuffer
);
3542 if (written_bytes
<= 0) {
3543 ERR("Error consuming subbuffer: (%zd)", written_bytes
);
3544 ret
= (int) written_bytes
;
3545 goto error_put_subbuf
;
3548 ret
= stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3553 if (stream
->read_subbuffer_ops
.post_consume
) {
3554 ret
= stream
->read_subbuffer_ops
.post_consume(stream
, &subbuffer
, ctx
);
3561 * After extracting the packet, we check if the stream is now ready to
3562 * be rotated and perform the action immediately.
3564 * Don't overwrite `ret` as callers expect the number of bytes
3565 * consumed to be returned on success.
3567 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
3568 if (rotation_ret
== 1) {
3569 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3570 if (rotation_ret
< 0) {
3572 ERR("Stream rotation error after consuming data");
3575 } else if (rotation_ret
< 0) {
3577 ERR("Failed to check if stream was ready to rotate after consuming data");
3582 * TODO roll into a post_consume op as this doesn't apply to metadata
3585 if (!stream
->opened_packet_in_current_trace_chunk
&&
3586 stream
->trace_chunk
&& !stream
->metadata_flag
&&
3587 !stream_is_rotating_to_null_chunk(stream
)) {
3588 const enum open_packet_status status
= open_packet(stream
);
3591 case OPEN_PACKET_STATUS_OPENED
:
3592 DBG("Opened a packet after consuming a packet rotation: stream id = %" PRIu64
3593 ", channel name = %s, session id = %" PRIu64
,
3594 stream
->key
, stream
->chan
->name
,
3595 stream
->chan
->session_id
);
3597 case OPEN_PACKET_STATUS_NO_SPACE
:
3599 * Can't open a packet as there is no space left.
3600 * This means that new events were produced, resulting
3601 * in a packet being opened, which is what we wanted
3604 DBG("No space left to open a packet after consuming a packet: stream id = %" PRIu64
3605 ", channel name = %s, session id = %" PRIu64
,
3606 stream
->key
, stream
->chan
->name
,
3607 stream
->chan
->session_id
);
3608 stream
->opened_packet_in_current_trace_chunk
= true;
3610 case OPEN_PACKET_STATUS_ERROR
:
3611 /* Logged by callee. */
3620 if (stream
->read_subbuffer_ops
.on_sleep
) {
3621 stream
->read_subbuffer_ops
.on_sleep(stream
, ctx
);
3624 ret
= written_bytes
;
3626 if (!locked_by_caller
) {
3627 stream
->read_subbuffer_ops
.unlock(stream
);
3632 (void) stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3636 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3638 switch (consumer_data
.type
) {
3639 case LTTNG_CONSUMER_KERNEL
:
3640 return lttng_kconsumer_on_recv_stream(stream
);
3641 case LTTNG_CONSUMER32_UST
:
3642 case LTTNG_CONSUMER64_UST
:
3643 return lttng_ustconsumer_on_recv_stream(stream
);
3645 ERR("Unknown consumer_data type");
3652 * Allocate and set consumer data hash tables.
3654 int lttng_consumer_init(void)
3656 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3657 if (!consumer_data
.channel_ht
) {
3661 consumer_data
.channels_by_session_id_ht
=
3662 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3663 if (!consumer_data
.channels_by_session_id_ht
) {
3667 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3668 if (!consumer_data
.relayd_ht
) {
3672 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3673 if (!consumer_data
.stream_list_ht
) {
3677 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3678 if (!consumer_data
.stream_per_chan_id_ht
) {
3682 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3687 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3692 consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3693 if (!consumer_data
.chunk_registry
) {
3704 * Process the ADD_RELAYD command receive by a consumer.
3706 * This will create a relayd socket pair and add it to the relayd hash table.
3707 * The caller MUST acquire a RCU read side lock before calling it.
3709 void consumer_add_relayd_socket(uint64_t net_seq_idx
,
3711 struct lttng_consumer_local_data
*ctx
,
3713 struct pollfd
*consumer_sockpoll
,
3714 uint64_t sessiond_id
,
3715 uint64_t relayd_session_id
,
3716 uint32_t relayd_version_major
,
3717 uint32_t relayd_version_minor
,
3718 enum lttcomm_sock_proto relayd_socket_protocol
)
3720 int fd
= -1, ret
= -1, relayd_created
= 0;
3721 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3722 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3726 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3728 /* Get relayd reference if exists. */
3729 relayd
= consumer_find_relayd(net_seq_idx
);
3730 if (relayd
== NULL
) {
3731 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3732 /* Not found. Allocate one. */
3733 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3734 if (relayd
== NULL
) {
3735 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3738 relayd
->sessiond_session_id
= sessiond_id
;
3743 * This code path MUST continue to the consumer send status message to
3744 * we can notify the session daemon and continue our work without
3745 * killing everything.
3749 * relayd key should never be found for control socket.
3751 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3754 /* First send a status message before receiving the fds. */
3755 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3757 /* Somehow, the session daemon is not responding anymore. */
3758 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3759 goto error_nosignal
;
3762 /* Poll on consumer socket. */
3763 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3765 /* Needing to exit in the middle of a command: error. */
3766 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3767 goto error_nosignal
;
3770 /* Get relayd socket from session daemon */
3771 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3772 if (ret
!= sizeof(fd
)) {
3773 fd
= -1; /* Just in case it gets set with an invalid value. */
3776 * Failing to receive FDs might indicate a major problem such as
3777 * reaching a fd limit during the receive where the kernel returns a
3778 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3779 * don't take any chances and stop everything.
3781 * XXX: Feature request #558 will fix that and avoid this possible
3782 * issue when reaching the fd limit.
3784 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3785 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3789 /* Copy socket information and received FD */
3790 switch (sock_type
) {
3791 case LTTNG_STREAM_CONTROL
:
3792 /* Copy received lttcomm socket */
3793 ret
= lttcomm_populate_sock_from_open_socket(
3794 &relayd
->control_sock
.sock
, fd
,
3795 relayd_socket_protocol
);
3797 /* Assign version values. */
3798 relayd
->control_sock
.major
= relayd_version_major
;
3799 relayd
->control_sock
.minor
= relayd_version_minor
;
3801 relayd
->relayd_session_id
= relayd_session_id
;
3804 case LTTNG_STREAM_DATA
:
3805 /* Copy received lttcomm socket */
3806 ret
= lttcomm_populate_sock_from_open_socket(
3807 &relayd
->data_sock
.sock
, fd
,
3808 relayd_socket_protocol
);
3809 /* Assign version values. */
3810 relayd
->data_sock
.major
= relayd_version_major
;
3811 relayd
->data_sock
.minor
= relayd_version_minor
;
3814 ERR("Unknown relayd socket type (%d)", sock_type
);
3815 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3820 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3824 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3825 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3826 relayd
->net_seq_idx
, fd
);
3828 * We gave the ownership of the fd to the relayd structure. Set the
3829 * fd to -1 so we don't call close() on it in the error path below.
3833 /* We successfully added the socket. Send status back. */
3834 ret
= consumer_send_status_msg(sock
, ret_code
);
3836 /* Somehow, the session daemon is not responding anymore. */
3837 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3838 goto error_nosignal
;
3842 * Add relayd socket pair to consumer data hashtable. If object already
3843 * exists or on error, the function gracefully returns.
3852 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3853 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3857 /* Close received socket if valid. */
3860 PERROR("close received socket");
3864 if (relayd_created
) {
3870 * Search for a relayd associated to the session id and return the reference.
3872 * A rcu read side lock MUST be acquire before calling this function and locked
3873 * until the relayd object is no longer necessary.
3875 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3877 struct lttng_ht_iter iter
;
3878 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3880 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3881 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3884 * Check by sessiond id which is unique here where the relayd session
3885 * id might not be when having multiple relayd.
3887 if (relayd
->sessiond_session_id
== id
) {
3888 /* Found the relayd. There can be only one per id. */
3900 * Check if for a given session id there is still data needed to be extract
3903 * Return 1 if data is pending or else 0 meaning ready to be read.
3905 int consumer_data_pending(uint64_t id
)
3908 struct lttng_ht_iter iter
;
3909 struct lttng_ht
*ht
;
3910 struct lttng_consumer_stream
*stream
;
3911 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3912 int (*data_pending
)(struct lttng_consumer_stream
*);
3914 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3917 pthread_mutex_lock(&consumer_data
.lock
);
3919 switch (consumer_data
.type
) {
3920 case LTTNG_CONSUMER_KERNEL
:
3921 data_pending
= lttng_kconsumer_data_pending
;
3923 case LTTNG_CONSUMER32_UST
:
3924 case LTTNG_CONSUMER64_UST
:
3925 data_pending
= lttng_ustconsumer_data_pending
;
3928 ERR("Unknown consumer data type");
3932 /* Ease our life a bit */
3933 ht
= consumer_data
.stream_list_ht
;
3935 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3936 ht
->hash_fct(&id
, lttng_ht_seed
),
3938 &iter
.iter
, stream
, node_session_id
.node
) {
3939 pthread_mutex_lock(&stream
->lock
);
3942 * A removed node from the hash table indicates that the stream has
3943 * been deleted thus having a guarantee that the buffers are closed
3944 * on the consumer side. However, data can still be transmitted
3945 * over the network so don't skip the relayd check.
3947 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3949 /* Check the stream if there is data in the buffers. */
3950 ret
= data_pending(stream
);
3952 pthread_mutex_unlock(&stream
->lock
);
3957 pthread_mutex_unlock(&stream
->lock
);
3960 relayd
= find_relayd_by_session_id(id
);
3962 unsigned int is_data_inflight
= 0;
3964 /* Send init command for data pending. */
3965 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3966 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3967 relayd
->relayd_session_id
);
3969 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3970 /* Communication error thus the relayd so no data pending. */
3971 goto data_not_pending
;
3974 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3975 ht
->hash_fct(&id
, lttng_ht_seed
),
3977 &iter
.iter
, stream
, node_session_id
.node
) {
3978 if (stream
->metadata_flag
) {
3979 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3980 stream
->relayd_stream_id
);
3982 ret
= relayd_data_pending(&relayd
->control_sock
,
3983 stream
->relayd_stream_id
,
3984 stream
->next_net_seq_num
- 1);
3988 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3990 } else if (ret
< 0) {
3991 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3992 lttng_consumer_cleanup_relayd(relayd
);
3993 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3994 goto data_not_pending
;
3998 /* Send end command for data pending. */
3999 ret
= relayd_end_data_pending(&relayd
->control_sock
,
4000 relayd
->relayd_session_id
, &is_data_inflight
);
4001 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4003 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
4004 lttng_consumer_cleanup_relayd(relayd
);
4005 goto data_not_pending
;
4007 if (is_data_inflight
) {
4013 * Finding _no_ node in the hash table and no inflight data means that the
4014 * stream(s) have been removed thus data is guaranteed to be available for
4015 * analysis from the trace files.
4019 /* Data is available to be read by a viewer. */
4020 pthread_mutex_unlock(&consumer_data
.lock
);
4025 /* Data is still being extracted from buffers. */
4026 pthread_mutex_unlock(&consumer_data
.lock
);
4032 * Send a ret code status message to the sessiond daemon.
4034 * Return the sendmsg() return value.
4036 int consumer_send_status_msg(int sock
, int ret_code
)
4038 struct lttcomm_consumer_status_msg msg
;
4040 memset(&msg
, 0, sizeof(msg
));
4041 msg
.ret_code
= ret_code
;
4043 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
4047 * Send a channel status message to the sessiond daemon.
4049 * Return the sendmsg() return value.
4051 int consumer_send_status_channel(int sock
,
4052 struct lttng_consumer_channel
*channel
)
4054 struct lttcomm_consumer_status_channel msg
;
4058 memset(&msg
, 0, sizeof(msg
));
4060 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
4062 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4063 msg
.key
= channel
->key
;
4064 msg
.stream_count
= channel
->streams
.count
;
4067 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
4070 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
4071 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
4072 uint64_t max_sb_size
)
4074 unsigned long start_pos
;
4076 if (!nb_packets_per_stream
) {
4077 return consumed_pos
; /* Grab everything */
4079 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
4080 start_pos
-= max_sb_size
* nb_packets_per_stream
;
4081 if ((long) (start_pos
- consumed_pos
) < 0) {
4082 return consumed_pos
; /* Grab everything */
4087 /* Stream lock must be held by the caller. */
4088 static int sample_stream_positions(struct lttng_consumer_stream
*stream
,
4089 unsigned long *produced
, unsigned long *consumed
)
4093 ASSERT_LOCKED(stream
->lock
);
4095 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4097 ERR("Failed to sample snapshot positions");
4101 ret
= lttng_consumer_get_produced_snapshot(stream
, produced
);
4103 ERR("Failed to sample produced position");
4107 ret
= lttng_consumer_get_consumed_snapshot(stream
, consumed
);
4109 ERR("Failed to sample consumed position");
4118 * Sample the rotate position for all the streams of a channel. If a stream
4119 * is already at the rotate position (produced == consumed), we flag it as
4120 * ready for rotation. The rotation of ready streams occurs after we have
4121 * replied to the session daemon that we have finished sampling the positions.
4122 * Must be called with RCU read-side lock held to ensure existence of channel.
4124 * Returns 0 on success, < 0 on error
4126 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
4127 uint64_t key
, uint64_t relayd_id
, uint32_t metadata
,
4128 struct lttng_consumer_local_data
*ctx
)
4131 struct lttng_consumer_stream
*stream
;
4132 struct lttng_ht_iter iter
;
4133 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
4134 struct lttng_dynamic_array stream_rotation_positions
;
4135 uint64_t next_chunk_id
, stream_count
= 0;
4136 enum lttng_trace_chunk_status chunk_status
;
4137 const bool is_local_trace
= relayd_id
== -1ULL;
4138 struct consumer_relayd_sock_pair
*relayd
= NULL
;
4139 bool rotating_to_new_chunk
= true;
4140 /* Array of `struct lttng_consumer_stream *` */
4141 struct lttng_dynamic_pointer_array streams_packet_to_open
;
4144 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
4146 lttng_dynamic_array_init(&stream_rotation_positions
,
4147 sizeof(struct relayd_stream_rotation_position
), NULL
);
4148 lttng_dynamic_pointer_array_init(&streams_packet_to_open
, NULL
);
4152 pthread_mutex_lock(&channel
->lock
);
4153 assert(channel
->trace_chunk
);
4154 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
,
4156 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4158 goto end_unlock_channel
;
4161 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4162 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4163 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4164 stream
, node_channel_id
.node
) {
4165 unsigned long produced_pos
= 0, consumed_pos
= 0;
4167 health_code_update();
4170 * Lock stream because we are about to change its state.
4172 pthread_mutex_lock(&stream
->lock
);
4174 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
4175 rotating_to_new_chunk
= false;
4179 * Do not flush a packet when rotating from a NULL trace
4180 * chunk. The stream has no means to output data, and the prior
4181 * rotation which rotated to NULL performed that side-effect
4182 * already. No new data can be produced when a stream has no
4183 * associated trace chunk (e.g. a stop followed by a rotate).
4185 if (stream
->trace_chunk
) {
4188 if (stream
->metadata_flag
) {
4190 * Don't produce an empty metadata packet,
4191 * simply close the current one.
4193 * Metadata is regenerated on every trace chunk
4194 * switch; there is no concern that no data was
4197 flush_active
= true;
4200 * Only flush an empty packet if the "packet
4201 * open" could not be performed on transition
4202 * to a new trace chunk and no packets were
4203 * consumed within the chunk's lifetime.
4205 if (stream
->opened_packet_in_current_trace_chunk
) {
4206 flush_active
= true;
4209 * Stream could have been full at the
4210 * time of rotation, but then have had
4211 * no activity at all.
4213 * It is important to flush a packet
4214 * to prevent 0-length files from being
4215 * produced as most viewers choke on
4218 * Unfortunately viewers will not be
4219 * able to know that tracing was active
4220 * for this stream during this trace
4223 ret
= sample_stream_positions(stream
, &produced_pos
, &consumed_pos
);
4225 goto end_unlock_stream
;
4229 * Don't flush an empty packet if data
4230 * was produced; it will be consumed
4231 * before the rotation completes.
4233 flush_active
= produced_pos
!= consumed_pos
;
4234 if (!flush_active
) {
4235 enum lttng_trace_chunk_status chunk_status
;
4236 const char *trace_chunk_name
;
4237 uint64_t trace_chunk_id
;
4239 chunk_status
= lttng_trace_chunk_get_name(
4240 stream
->trace_chunk
,
4243 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NONE
) {
4244 trace_chunk_name
= "none";
4248 * Consumer trace chunks are
4251 chunk_status
= lttng_trace_chunk_get_id(
4252 stream
->trace_chunk
,
4254 assert(chunk_status
==
4255 LTTNG_TRACE_CHUNK_STATUS_OK
);
4257 DBG("Unable to open packet for stream during trace chunk's lifetime. "
4258 "Flushing an empty packet to prevent an empty file from being created: "
4259 "stream id = %" PRIu64
", trace chunk name = `%s`, trace chunk id = %" PRIu64
,
4260 stream
->key
, trace_chunk_name
, trace_chunk_id
);
4266 * Close the current packet before sampling the
4267 * ring buffer positions.
4269 ret
= consumer_flush_buffer(stream
, flush_active
);
4271 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4273 goto end_unlock_stream
;
4277 ret
= lttng_consumer_take_snapshot(stream
);
4278 if (ret
< 0 && ret
!= -ENODATA
&& ret
!= -EAGAIN
) {
4279 ERR("Failed to sample snapshot position during channel rotation");
4280 goto end_unlock_stream
;
4283 ret
= lttng_consumer_get_produced_snapshot(stream
,
4286 ERR("Failed to sample produced position during channel rotation");
4287 goto end_unlock_stream
;
4290 ret
= lttng_consumer_get_consumed_snapshot(stream
,
4293 ERR("Failed to sample consumed position during channel rotation");
4294 goto end_unlock_stream
;
4298 * Align produced position on the start-of-packet boundary of the first
4299 * packet going into the next trace chunk.
4301 produced_pos
= ALIGN_FLOOR(produced_pos
, stream
->max_sb_size
);
4302 if (consumed_pos
== produced_pos
) {
4303 DBG("Set rotate ready for stream %" PRIu64
" produced = %lu consumed = %lu",
4304 stream
->key
, produced_pos
, consumed_pos
);
4305 stream
->rotate_ready
= true;
4307 DBG("Different consumed and produced positions "
4308 "for stream %" PRIu64
" produced = %lu consumed = %lu",
4309 stream
->key
, produced_pos
, consumed_pos
);
4312 * The rotation position is based on the packet_seq_num of the
4313 * packet following the last packet that was consumed for this
4314 * stream, incremented by the offset between produced and
4315 * consumed positions. This rotation position is a lower bound
4316 * (inclusive) at which the next trace chunk starts. Since it
4317 * is a lower bound, it is OK if the packet_seq_num does not
4318 * correspond exactly to the same packet identified by the
4319 * consumed_pos, which can happen in overwrite mode.
4321 if (stream
->sequence_number_unavailable
) {
4323 * Rotation should never be performed on a session which
4324 * interacts with a pre-2.8 lttng-modules, which does
4325 * not implement packet sequence number.
4327 ERR("Failure to rotate stream %" PRIu64
": sequence number unavailable",
4330 goto end_unlock_stream
;
4332 stream
->rotate_position
= stream
->last_sequence_number
+ 1 +
4333 ((produced_pos
- consumed_pos
) / stream
->max_sb_size
);
4334 DBG("Set rotation position for stream %" PRIu64
" at position %" PRIu64
,
4335 stream
->key
, stream
->rotate_position
);
4337 if (!is_local_trace
) {
4339 * The relay daemon control protocol expects a rotation
4340 * position as "the sequence number of the first packet
4341 * _after_ the current trace chunk".
4343 const struct relayd_stream_rotation_position position
= {
4344 .stream_id
= stream
->relayd_stream_id
,
4345 .rotate_at_seq_num
= stream
->rotate_position
,
4348 ret
= lttng_dynamic_array_add_element(
4349 &stream_rotation_positions
,
4352 ERR("Failed to allocate stream rotation position");
4353 goto end_unlock_stream
;
4358 stream
->opened_packet_in_current_trace_chunk
= false;
4360 if (rotating_to_new_chunk
&& !stream
->metadata_flag
) {
4362 * Attempt to flush an empty packet as close to the
4363 * rotation point as possible. In the event where a
4364 * stream remains inactive after the rotation point,
4365 * this ensures that the new trace chunk has a
4366 * beginning timestamp set at the begining of the
4367 * trace chunk instead of only creating an empty
4368 * packet when the trace chunk is stopped.
4370 * This indicates to the viewers that the stream
4371 * was being recorded, but more importantly it
4372 * allows viewers to determine a useable trace
4375 * This presents a problem in the case where the
4376 * ring-buffer is completely full.
4378 * Consider the following scenario:
4379 * - The consumption of data is slow (slow network,
4381 * - The ring buffer is full,
4382 * - A rotation is initiated,
4383 * - The flush below does nothing (no space left to
4384 * open a new packet),
4385 * - The other streams rotate very soon, and new
4386 * data is produced in the new chunk,
4387 * - This stream completes its rotation long after the
4388 * rotation was initiated
4389 * - The session is stopped before any event can be
4390 * produced in this stream's buffers.
4392 * The resulting trace chunk will have a single packet
4393 * temporaly at the end of the trace chunk for this
4394 * stream making the stream intersection more narrow
4395 * than it should be.
4397 * To work-around this, an empty flush is performed
4398 * after the first consumption of a packet during a
4399 * rotation if open_packet fails. The idea is that
4400 * consuming a packet frees enough space to switch
4401 * packets in this scenario and allows the tracer to
4402 * "stamp" the beginning of the new trace chunk at the
4403 * earliest possible point.
4405 * The packet open is performed after the channel
4406 * rotation to ensure that no attempt to open a packet
4407 * is performed in a stream that has no active trace
4410 ret
= lttng_dynamic_pointer_array_add_pointer(
4411 &streams_packet_to_open
, stream
);
4413 PERROR("Failed to add a stream pointer to array of streams in which to open a packet");
4415 goto end_unlock_stream
;
4419 pthread_mutex_unlock(&stream
->lock
);
4423 if (!is_local_trace
) {
4424 relayd
= consumer_find_relayd(relayd_id
);
4426 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4428 goto end_unlock_channel
;
4431 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4432 ret
= relayd_rotate_streams(&relayd
->control_sock
, stream_count
,
4433 rotating_to_new_chunk
? &next_chunk_id
: NULL
,
4434 (const struct relayd_stream_rotation_position
*)
4435 stream_rotation_positions
.buffer
4437 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4439 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4440 relayd
->net_seq_idx
);
4441 lttng_consumer_cleanup_relayd(relayd
);
4442 goto end_unlock_channel
;
4446 for (stream_idx
= 0;
4447 stream_idx
< lttng_dynamic_pointer_array_get_count(
4448 &streams_packet_to_open
);
4450 enum open_packet_status status
;
4452 stream
= lttng_dynamic_pointer_array_get_pointer(
4453 &streams_packet_to_open
, stream_idx
);
4455 pthread_mutex_lock(&stream
->lock
);
4456 status
= open_packet(stream
);
4457 pthread_mutex_unlock(&stream
->lock
);
4459 case OPEN_PACKET_STATUS_OPENED
:
4460 DBG("Opened a packet after a rotation: stream id = %" PRIu64
4461 ", channel name = %s, session id = %" PRIu64
,
4462 stream
->key
, stream
->chan
->name
,
4463 stream
->chan
->session_id
);
4465 case OPEN_PACKET_STATUS_NO_SPACE
:
4467 * Can't open a packet as there is no space left
4468 * in the buffer. A new packet will be opened
4469 * once one has been consumed.
4471 DBG("No space left to open a packet after a rotation: stream id = %" PRIu64
4472 ", channel name = %s, session id = %" PRIu64
,
4473 stream
->key
, stream
->chan
->name
,
4474 stream
->chan
->session_id
);
4476 case OPEN_PACKET_STATUS_ERROR
:
4477 /* Logged by callee. */
4479 goto end_unlock_channel
;
4485 pthread_mutex_unlock(&channel
->lock
);
4490 pthread_mutex_unlock(&stream
->lock
);
4492 pthread_mutex_unlock(&channel
->lock
);
4495 lttng_dynamic_array_reset(&stream_rotation_positions
);
4496 lttng_dynamic_pointer_array_reset(&streams_packet_to_open
);
4501 int consumer_clear_buffer(struct lttng_consumer_stream
*stream
)
4504 unsigned long consumed_pos_before
, consumed_pos_after
;
4506 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4508 ERR("Taking snapshot positions");
4512 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_before
);
4514 ERR("Consumed snapshot position");
4518 switch (consumer_data
.type
) {
4519 case LTTNG_CONSUMER_KERNEL
:
4520 ret
= kernctl_buffer_clear(stream
->wait_fd
);
4522 ERR("Failed to clear kernel stream (ret = %d)", ret
);
4526 case LTTNG_CONSUMER32_UST
:
4527 case LTTNG_CONSUMER64_UST
:
4528 lttng_ustconsumer_clear_buffer(stream
);
4531 ERR("Unknown consumer_data type");
4535 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4537 ERR("Taking snapshot positions");
4540 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_after
);
4542 ERR("Consumed snapshot position");
4545 DBG("clear: before: %lu after: %lu", consumed_pos_before
, consumed_pos_after
);
4551 int consumer_clear_stream(struct lttng_consumer_stream
*stream
)
4555 ret
= consumer_flush_buffer(stream
, 1);
4557 ERR("Failed to flush stream %" PRIu64
" during channel clear",
4559 ret
= LTTCOMM_CONSUMERD_FATAL
;
4563 ret
= consumer_clear_buffer(stream
);
4565 ERR("Failed to clear stream %" PRIu64
" during channel clear",
4567 ret
= LTTCOMM_CONSUMERD_FATAL
;
4571 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4577 int consumer_clear_unmonitored_channel(struct lttng_consumer_channel
*channel
)
4580 struct lttng_consumer_stream
*stream
;
4583 pthread_mutex_lock(&channel
->lock
);
4584 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
4585 health_code_update();
4586 pthread_mutex_lock(&stream
->lock
);
4587 ret
= consumer_clear_stream(stream
);
4591 pthread_mutex_unlock(&stream
->lock
);
4593 pthread_mutex_unlock(&channel
->lock
);
4598 pthread_mutex_unlock(&stream
->lock
);
4599 pthread_mutex_unlock(&channel
->lock
);
4605 * Check if a stream is ready to be rotated after extracting it.
4607 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4608 * error. Stream lock must be held.
4610 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4612 DBG("Check is rotate ready for stream %" PRIu64
4613 " ready %u rotate_position %" PRIu64
4614 " last_sequence_number %" PRIu64
,
4615 stream
->key
, stream
->rotate_ready
,
4616 stream
->rotate_position
, stream
->last_sequence_number
);
4617 if (stream
->rotate_ready
) {
4622 * If packet seq num is unavailable, it means we are interacting
4623 * with a pre-2.8 lttng-modules which does not implement the
4624 * sequence number. Rotation should never be used by sessiond in this
4627 if (stream
->sequence_number_unavailable
) {
4628 ERR("Internal error: rotation used on stream %" PRIu64
4629 " with unavailable sequence number",
4634 if (stream
->rotate_position
== -1ULL ||
4635 stream
->last_sequence_number
== -1ULL) {
4640 * Rotate position not reached yet. The stream rotate position is
4641 * the position of the next packet belonging to the next trace chunk,
4642 * but consumerd considers rotation ready when reaching the last
4643 * packet of the current chunk, hence the "rotate_position - 1".
4646 DBG("Check is rotate ready for stream %" PRIu64
4647 " last_sequence_number %" PRIu64
4648 " rotate_position %" PRIu64
,
4649 stream
->key
, stream
->last_sequence_number
,
4650 stream
->rotate_position
);
4651 if (stream
->last_sequence_number
>= stream
->rotate_position
- 1) {
4659 * Reset the state for a stream after a rotation occurred.
4661 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4663 DBG("lttng_consumer_reset_stream_rotate_state for stream %" PRIu64
,
4665 stream
->rotate_position
= -1ULL;
4666 stream
->rotate_ready
= false;
4670 * Perform the rotation a local stream file.
4673 int rotate_local_stream(struct lttng_consumer_local_data
*ctx
,
4674 struct lttng_consumer_stream
*stream
)
4678 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4681 stream
->tracefile_size_current
= 0;
4682 stream
->tracefile_count_current
= 0;
4684 if (stream
->out_fd
>= 0) {
4685 ret
= close(stream
->out_fd
);
4687 PERROR("Failed to close stream out_fd of channel \"%s\"",
4688 stream
->chan
->name
);
4690 stream
->out_fd
= -1;
4693 if (stream
->index_file
) {
4694 lttng_index_file_put(stream
->index_file
);
4695 stream
->index_file
= NULL
;
4698 if (!stream
->trace_chunk
) {
4702 ret
= consumer_stream_create_output_files(stream
, true);
4708 * Performs the stream rotation for the rotate session feature if needed.
4709 * It must be called with the channel and stream locks held.
4711 * Return 0 on success, a negative number of error.
4713 int lttng_consumer_rotate_stream(struct lttng_consumer_local_data
*ctx
,
4714 struct lttng_consumer_stream
*stream
)
4718 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4721 * Update the stream's 'current' chunk to the session's (channel)
4722 * now-current chunk.
4724 lttng_trace_chunk_put(stream
->trace_chunk
);
4725 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4727 * A channel can be rotated and not have a "next" chunk
4728 * to transition to. In that case, the channel's "current chunk"
4729 * has not been closed yet, but it has not been updated to
4730 * a "next" trace chunk either. Hence, the stream, like its
4731 * parent channel, becomes part of no chunk and can't output
4732 * anything until a new trace chunk is created.
4734 stream
->trace_chunk
= NULL
;
4735 } else if (stream
->chan
->trace_chunk
&&
4736 !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4737 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4742 * Update the stream's trace chunk to its parent channel's
4743 * current trace chunk.
4745 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4748 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4749 ret
= rotate_local_stream(ctx
, stream
);
4751 ERR("Failed to rotate stream, ret = %i", ret
);
4756 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4758 * If the stream has transitioned to a new trace
4759 * chunk, the metadata should be re-dumped to the
4762 * However, it is possible for a stream to transition to
4763 * a "no-chunk" state. This can happen if a rotation
4764 * occurs on an inactive session. In such cases, the metadata
4765 * regeneration will happen when the next trace chunk is
4768 ret
= consumer_metadata_stream_dump(stream
);
4773 lttng_consumer_reset_stream_rotate_state(stream
);
4782 * Rotate all the ready streams now.
4784 * This is especially important for low throughput streams that have already
4785 * been consumed, we cannot wait for their next packet to perform the
4787 * Need to be called with RCU read-side lock held to ensure existence of
4790 * Returns 0 on success, < 0 on error
4792 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
,
4793 uint64_t key
, struct lttng_consumer_local_data
*ctx
)
4796 struct lttng_consumer_stream
*stream
;
4797 struct lttng_ht_iter iter
;
4798 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
4802 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4804 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4805 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4806 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4807 stream
, node_channel_id
.node
) {
4808 health_code_update();
4810 pthread_mutex_lock(&stream
->chan
->lock
);
4811 pthread_mutex_lock(&stream
->lock
);
4813 if (!stream
->rotate_ready
) {
4814 pthread_mutex_unlock(&stream
->lock
);
4815 pthread_mutex_unlock(&stream
->chan
->lock
);
4818 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4820 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
4821 pthread_mutex_unlock(&stream
->lock
);
4822 pthread_mutex_unlock(&stream
->chan
->lock
);
4835 enum lttcomm_return_code
lttng_consumer_init_command(
4836 struct lttng_consumer_local_data
*ctx
,
4837 const lttng_uuid sessiond_uuid
)
4839 enum lttcomm_return_code ret
;
4840 char uuid_str
[LTTNG_UUID_STR_LEN
];
4842 if (ctx
->sessiond_uuid
.is_set
) {
4843 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4847 ctx
->sessiond_uuid
.is_set
= true;
4848 memcpy(ctx
->sessiond_uuid
.value
, sessiond_uuid
, sizeof(lttng_uuid
));
4849 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4850 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4851 DBG("Received session daemon UUID: %s", uuid_str
);
4856 enum lttcomm_return_code
lttng_consumer_create_trace_chunk(
4857 const uint64_t *relayd_id
, uint64_t session_id
,
4859 time_t chunk_creation_timestamp
,
4860 const char *chunk_override_name
,
4861 const struct lttng_credentials
*credentials
,
4862 struct lttng_directory_handle
*chunk_directory_handle
)
4865 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4866 struct lttng_trace_chunk
*created_chunk
= NULL
, *published_chunk
= NULL
;
4867 enum lttng_trace_chunk_status chunk_status
;
4868 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4869 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4870 const char *relayd_id_str
= "(none)";
4871 const char *creation_timestamp_str
;
4872 struct lttng_ht_iter iter
;
4873 struct lttng_consumer_channel
*channel
;
4876 /* Only used for logging purposes. */
4877 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4878 "%" PRIu64
, *relayd_id
);
4879 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4880 relayd_id_str
= relayd_id_buffer
;
4882 relayd_id_str
= "(formatting error)";
4886 /* Local protocol error. */
4887 assert(chunk_creation_timestamp
);
4888 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4889 creation_timestamp_buffer
,
4890 sizeof(creation_timestamp_buffer
));
4891 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
4892 "(formatting error)";
4894 DBG("Consumer create trace chunk command: relay_id = %s"
4895 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4896 ", chunk_override_name = %s"
4897 ", chunk_creation_timestamp = %s",
4898 relayd_id_str
, session_id
, chunk_id
,
4899 chunk_override_name
? : "(none)",
4900 creation_timestamp_str
);
4903 * The trace chunk registry, as used by the consumer daemon, implicitly
4904 * owns the trace chunks. This is only needed in the consumer since
4905 * the consumer has no notion of a session beyond session IDs being
4906 * used to identify other objects.
4908 * The lttng_trace_chunk_registry_publish() call below provides a
4909 * reference which is not released; it implicitly becomes the session
4910 * daemon's reference to the chunk in the consumer daemon.
4912 * The lifetime of trace chunks in the consumer daemon is managed by
4913 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4914 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4916 created_chunk
= lttng_trace_chunk_create(chunk_id
,
4917 chunk_creation_timestamp
, NULL
);
4918 if (!created_chunk
) {
4919 ERR("Failed to create trace chunk");
4920 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4924 if (chunk_override_name
) {
4925 chunk_status
= lttng_trace_chunk_override_name(created_chunk
,
4926 chunk_override_name
);
4927 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4928 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4933 if (chunk_directory_handle
) {
4934 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
,
4936 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4937 ERR("Failed to set trace chunk credentials");
4938 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4942 * The consumer daemon has no ownership of the chunk output
4945 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
,
4946 chunk_directory_handle
);
4947 chunk_directory_handle
= NULL
;
4948 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4949 ERR("Failed to set trace chunk's directory handle");
4950 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4955 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4956 consumer_data
.chunk_registry
, session_id
,
4958 lttng_trace_chunk_put(created_chunk
);
4959 created_chunk
= NULL
;
4960 if (!published_chunk
) {
4961 ERR("Failed to publish trace chunk");
4962 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4967 cds_lfht_for_each_entry_duplicate(consumer_data
.channels_by_session_id_ht
->ht
,
4968 consumer_data
.channels_by_session_id_ht
->hash_fct(
4969 &session_id
, lttng_ht_seed
),
4970 consumer_data
.channels_by_session_id_ht
->match_fct
,
4971 &session_id
, &iter
.iter
, channel
,
4972 channels_by_session_id_ht_node
.node
) {
4973 ret
= lttng_consumer_channel_set_trace_chunk(channel
,
4977 * Roll-back the creation of this chunk.
4979 * This is important since the session daemon will
4980 * assume that the creation of this chunk failed and
4981 * will never ask for it to be closed, resulting
4982 * in a leak and an inconsistent state for some
4985 enum lttcomm_return_code close_ret
;
4986 char path
[LTTNG_PATH_MAX
];
4988 DBG("Failed to set new trace chunk on existing channels, rolling back");
4989 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4990 session_id
, chunk_id
,
4991 chunk_creation_timestamp
, NULL
,
4993 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4994 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4995 session_id
, chunk_id
);
4998 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
5004 struct consumer_relayd_sock_pair
*relayd
;
5006 relayd
= consumer_find_relayd(*relayd_id
);
5008 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5009 ret
= relayd_create_trace_chunk(
5010 &relayd
->control_sock
, published_chunk
);
5011 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5013 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
5016 if (!relayd
|| ret
) {
5017 enum lttcomm_return_code close_ret
;
5018 char path
[LTTNG_PATH_MAX
];
5020 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
5023 chunk_creation_timestamp
,
5025 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
5026 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
5031 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
5038 /* Release the reference returned by the "publish" operation. */
5039 lttng_trace_chunk_put(published_chunk
);
5040 lttng_trace_chunk_put(created_chunk
);
5044 enum lttcomm_return_code
lttng_consumer_close_trace_chunk(
5045 const uint64_t *relayd_id
, uint64_t session_id
,
5046 uint64_t chunk_id
, time_t chunk_close_timestamp
,
5047 const enum lttng_trace_chunk_command_type
*close_command
,
5050 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
5051 struct lttng_trace_chunk
*chunk
;
5052 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
5053 const char *relayd_id_str
= "(none)";
5054 const char *close_command_name
= "none";
5055 struct lttng_ht_iter iter
;
5056 struct lttng_consumer_channel
*channel
;
5057 enum lttng_trace_chunk_status chunk_status
;
5062 /* Only used for logging purposes. */
5063 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
5064 "%" PRIu64
, *relayd_id
);
5065 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
5066 relayd_id_str
= relayd_id_buffer
;
5068 relayd_id_str
= "(formatting error)";
5071 if (close_command
) {
5072 close_command_name
= lttng_trace_chunk_command_type_get_name(
5076 DBG("Consumer close trace chunk command: relayd_id = %s"
5077 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
5078 ", close command = %s",
5079 relayd_id_str
, session_id
, chunk_id
,
5080 close_command_name
);
5082 chunk
= lttng_trace_chunk_registry_find_chunk(
5083 consumer_data
.chunk_registry
, session_id
, chunk_id
);
5085 ERR("Failed to find chunk: session_id = %" PRIu64
5086 ", chunk_id = %" PRIu64
,
5087 session_id
, chunk_id
);
5088 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5092 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
,
5093 chunk_close_timestamp
);
5094 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
5095 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5099 if (close_command
) {
5100 chunk_status
= lttng_trace_chunk_set_close_command(
5101 chunk
, *close_command
);
5102 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
5103 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5109 * chunk is now invalid to access as we no longer hold a reference to
5110 * it; it is only kept around to compare it (by address) to the
5111 * current chunk found in the session's channels.
5114 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
,
5115 channel
, node
.node
) {
5119 * Only change the channel's chunk to NULL if it still
5120 * references the chunk being closed. The channel may
5121 * reference a newer channel in the case of a session
5122 * rotation. When a session rotation occurs, the "next"
5123 * chunk is created before the "current" chunk is closed.
5125 if (channel
->trace_chunk
!= chunk
) {
5128 ret
= lttng_consumer_channel_set_trace_chunk(channel
, NULL
);
5131 * Attempt to close the chunk on as many channels as
5134 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5140 struct consumer_relayd_sock_pair
*relayd
;
5142 relayd
= consumer_find_relayd(*relayd_id
);
5144 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5145 ret
= relayd_close_trace_chunk(
5146 &relayd
->control_sock
, chunk
,
5148 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5150 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
,
5154 if (!relayd
|| ret
) {
5155 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5163 * Release the reference returned by the "find" operation and
5164 * the session daemon's implicit reference to the chunk.
5166 lttng_trace_chunk_put(chunk
);
5167 lttng_trace_chunk_put(chunk
);
5172 enum lttcomm_return_code
lttng_consumer_trace_chunk_exists(
5173 const uint64_t *relayd_id
, uint64_t session_id
,
5177 enum lttcomm_return_code ret_code
;
5178 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
5179 const char *relayd_id_str
= "(none)";
5180 const bool is_local_trace
= !relayd_id
;
5181 struct consumer_relayd_sock_pair
*relayd
= NULL
;
5182 bool chunk_exists_local
, chunk_exists_remote
;
5187 /* Only used for logging purposes. */
5188 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
5189 "%" PRIu64
, *relayd_id
);
5190 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
5191 relayd_id_str
= relayd_id_buffer
;
5193 relayd_id_str
= "(formatting error)";
5197 DBG("Consumer trace chunk exists command: relayd_id = %s"
5198 ", chunk_id = %" PRIu64
, relayd_id_str
,
5200 ret
= lttng_trace_chunk_registry_chunk_exists(
5201 consumer_data
.chunk_registry
, session_id
,
5202 chunk_id
, &chunk_exists_local
);
5204 /* Internal error. */
5205 ERR("Failed to query the existence of a trace chunk");
5206 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
5209 DBG("Trace chunk %s locally",
5210 chunk_exists_local
? "exists" : "does not exist");
5211 if (chunk_exists_local
) {
5212 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
5214 } else if (is_local_trace
) {
5215 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5220 relayd
= consumer_find_relayd(*relayd_id
);
5222 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
5223 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5224 goto end_rcu_unlock
;
5226 DBG("Looking up existence of trace chunk on relay daemon");
5227 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5228 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
,
5229 &chunk_exists_remote
);
5230 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5232 ERR("Failed to look-up the existence of trace chunk on relay daemon");
5233 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
5234 goto end_rcu_unlock
;
5237 ret_code
= chunk_exists_remote
?
5238 LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
5239 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5240 DBG("Trace chunk %s on relay daemon",
5241 chunk_exists_remote
? "exists" : "does not exist");
5250 int consumer_clear_monitored_channel(struct lttng_consumer_channel
*channel
)
5252 struct lttng_ht
*ht
;
5253 struct lttng_consumer_stream
*stream
;
5254 struct lttng_ht_iter iter
;
5257 ht
= consumer_data
.stream_per_chan_id_ht
;
5260 cds_lfht_for_each_entry_duplicate(ht
->ht
,
5261 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
5262 ht
->match_fct
, &channel
->key
,
5263 &iter
.iter
, stream
, node_channel_id
.node
) {
5265 * Protect against teardown with mutex.
5267 pthread_mutex_lock(&stream
->lock
);
5268 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5271 ret
= consumer_clear_stream(stream
);
5276 pthread_mutex_unlock(&stream
->lock
);
5279 return LTTCOMM_CONSUMERD_SUCCESS
;
5282 pthread_mutex_unlock(&stream
->lock
);
5287 int lttng_consumer_clear_channel(struct lttng_consumer_channel
*channel
)
5291 DBG("Consumer clear channel %" PRIu64
, channel
->key
);
5293 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
5295 * Nothing to do for the metadata channel/stream.
5296 * Snapshot mechanism already take care of the metadata
5297 * handling/generation, and monitored channels only need to
5298 * have their data stream cleared..
5300 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5304 if (!channel
->monitor
) {
5305 ret
= consumer_clear_unmonitored_channel(channel
);
5307 ret
= consumer_clear_monitored_channel(channel
);
5313 enum lttcomm_return_code
lttng_consumer_open_channel_packets(
5314 struct lttng_consumer_channel
*channel
)
5316 struct lttng_consumer_stream
*stream
;
5317 enum lttcomm_return_code ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5319 if (channel
->metadata_stream
) {
5320 ERR("Open channel packets command attempted on a metadata channel");
5321 ret
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5326 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
5327 enum open_packet_status status
;
5329 pthread_mutex_lock(&stream
->lock
);
5330 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5334 status
= open_packet(stream
);
5336 case OPEN_PACKET_STATUS_OPENED
:
5337 DBG("Opened a packet in \"open channel packets\" command: stream id = %" PRIu64
5338 ", channel name = %s, session id = %" PRIu64
,
5339 stream
->key
, stream
->chan
->name
,
5340 stream
->chan
->session_id
);
5341 stream
->opened_packet_in_current_trace_chunk
= true;
5343 case OPEN_PACKET_STATUS_NO_SPACE
:
5344 DBG("No space left to open a packet in \"open channel packets\" command: stream id = %" PRIu64
5345 ", channel name = %s, session id = %" PRIu64
,
5346 stream
->key
, stream
->chan
->name
,
5347 stream
->chan
->session_id
);
5349 case OPEN_PACKET_STATUS_ERROR
:
5351 * Only unexpected internal errors can lead to this
5352 * failing. Report an unknown error.
5354 ERR("Failed to flush empty buffer in \"open channel packets\" command: stream id = %" PRIu64
5355 ", channel id = %" PRIu64
5356 ", channel name = %s"
5357 ", session id = %" PRIu64
,
5358 stream
->key
, channel
->key
,
5359 channel
->name
, channel
->session_id
);
5360 ret
= LTTCOMM_CONSUMERD_UNKNOWN_ERROR
;
5367 pthread_mutex_unlock(&stream
->lock
);
5376 pthread_mutex_unlock(&stream
->lock
);
5377 goto end_rcu_unlock
;