2 * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
10 #include "common/index/ctf-index.h"
18 #include <sys/socket.h>
19 #include <sys/types.h>
24 #include <bin/lttng-consumerd/health-consumerd.h>
25 #include <common/common.h>
26 #include <common/utils.h>
27 #include <common/time.h>
28 #include <common/compat/poll.h>
29 #include <common/compat/endian.h>
30 #include <common/index/index.h>
31 #include <common/kernel-ctl/kernel-ctl.h>
32 #include <common/sessiond-comm/relayd.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
34 #include <common/kernel-consumer/kernel-consumer.h>
35 #include <common/relayd/relayd.h>
36 #include <common/ust-consumer/ust-consumer.h>
37 #include <common/consumer/consumer-timer.h>
38 #include <common/consumer/consumer.h>
39 #include <common/consumer/consumer-stream.h>
40 #include <common/consumer/consumer-testpoint.h>
41 #include <common/align.h>
42 #include <common/consumer/consumer-metadata-cache.h>
43 #include <common/trace-chunk.h>
44 #include <common/trace-chunk-registry.h>
45 #include <common/string-utils/format.h>
46 #include <common/dynamic-array.h>
48 struct lttng_consumer_global_data consumer_data
= {
51 .type
= LTTNG_CONSUMER_UNKNOWN
,
54 enum consumer_channel_action
{
57 CONSUMER_CHANNEL_QUIT
,
60 struct consumer_channel_msg
{
61 enum consumer_channel_action action
;
62 struct lttng_consumer_channel
*chan
; /* add */
63 uint64_t key
; /* del */
66 /* Flag used to temporarily pause data consumption from testpoints. */
67 int data_consumption_paused
;
70 * Flag to inform the polling thread to quit when all fd hung up. Updated by
71 * the consumer_thread_receive_fds when it notices that all fds has hung up.
72 * Also updated by the signal handler (consumer_should_exit()). Read by the
78 * Global hash table containing respectively metadata and data streams. The
79 * stream element in this ht should only be updated by the metadata poll thread
80 * for the metadata and the data poll thread for the data.
82 static struct lttng_ht
*metadata_ht
;
83 static struct lttng_ht
*data_ht
;
85 static const char *get_consumer_domain(void)
87 switch (consumer_data
.type
) {
88 case LTTNG_CONSUMER_KERNEL
:
89 return DEFAULT_KERNEL_TRACE_DIR
;
90 case LTTNG_CONSUMER64_UST
:
92 case LTTNG_CONSUMER32_UST
:
93 return DEFAULT_UST_TRACE_DIR
;
100 * Notify a thread lttng pipe to poll back again. This usually means that some
101 * global state has changed so we just send back the thread in a poll wait
104 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
106 struct lttng_consumer_stream
*null_stream
= NULL
;
110 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
113 static void notify_health_quit_pipe(int *pipe
)
117 ret
= lttng_write(pipe
[1], "4", 1);
119 PERROR("write consumer health quit");
123 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
124 struct lttng_consumer_channel
*chan
,
126 enum consumer_channel_action action
)
128 struct consumer_channel_msg msg
;
131 memset(&msg
, 0, sizeof(msg
));
136 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
137 if (ret
< sizeof(msg
)) {
138 PERROR("notify_channel_pipe write error");
142 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
145 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
148 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
149 struct lttng_consumer_channel
**chan
,
151 enum consumer_channel_action
*action
)
153 struct consumer_channel_msg msg
;
156 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
157 if (ret
< sizeof(msg
)) {
161 *action
= msg
.action
;
169 * Cleanup the stream list of a channel. Those streams are not yet globally
172 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
174 struct lttng_consumer_stream
*stream
, *stmp
;
178 /* Delete streams that might have been left in the stream list. */
179 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
181 cds_list_del(&stream
->send_node
);
183 * Once a stream is added to this list, the buffers were created so we
184 * have a guarantee that this call will succeed. Setting the monitor
185 * mode to 0 so we don't lock nor try to delete the stream from the
189 consumer_stream_destroy(stream
, NULL
);
194 * Find a stream. The consumer_data.lock must be locked during this
197 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
200 struct lttng_ht_iter iter
;
201 struct lttng_ht_node_u64
*node
;
202 struct lttng_consumer_stream
*stream
= NULL
;
206 /* -1ULL keys are lookup failures */
207 if (key
== (uint64_t) -1ULL) {
213 lttng_ht_lookup(ht
, &key
, &iter
);
214 node
= lttng_ht_iter_get_node_u64(&iter
);
216 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
224 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
226 struct lttng_consumer_stream
*stream
;
229 stream
= find_stream(key
, ht
);
231 stream
->key
= (uint64_t) -1ULL;
233 * We don't want the lookup to match, but we still need
234 * to iterate on this stream when iterating over the hash table. Just
235 * change the node key.
237 stream
->node
.key
= (uint64_t) -1ULL;
243 * Return a channel object for the given key.
245 * RCU read side lock MUST be acquired before calling this function and
246 * protects the channel ptr.
248 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
250 struct lttng_ht_iter iter
;
251 struct lttng_ht_node_u64
*node
;
252 struct lttng_consumer_channel
*channel
= NULL
;
254 /* -1ULL keys are lookup failures */
255 if (key
== (uint64_t) -1ULL) {
259 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
260 node
= lttng_ht_iter_get_node_u64(&iter
);
262 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
269 * There is a possibility that the consumer does not have enough time between
270 * the close of the channel on the session daemon and the cleanup in here thus
271 * once we have a channel add with an existing key, we know for sure that this
272 * channel will eventually get cleaned up by all streams being closed.
274 * This function just nullifies the already existing channel key.
276 static void steal_channel_key(uint64_t key
)
278 struct lttng_consumer_channel
*channel
;
281 channel
= consumer_find_channel(key
);
283 channel
->key
= (uint64_t) -1ULL;
285 * We don't want the lookup to match, but we still need to iterate on
286 * this channel when iterating over the hash table. Just change the
289 channel
->node
.key
= (uint64_t) -1ULL;
294 static void free_channel_rcu(struct rcu_head
*head
)
296 struct lttng_ht_node_u64
*node
=
297 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
298 struct lttng_consumer_channel
*channel
=
299 caa_container_of(node
, struct lttng_consumer_channel
, node
);
301 switch (consumer_data
.type
) {
302 case LTTNG_CONSUMER_KERNEL
:
304 case LTTNG_CONSUMER32_UST
:
305 case LTTNG_CONSUMER64_UST
:
306 lttng_ustconsumer_free_channel(channel
);
309 ERR("Unknown consumer_data type");
316 * RCU protected relayd socket pair free.
318 static void free_relayd_rcu(struct rcu_head
*head
)
320 struct lttng_ht_node_u64
*node
=
321 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
322 struct consumer_relayd_sock_pair
*relayd
=
323 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
326 * Close all sockets. This is done in the call RCU since we don't want the
327 * socket fds to be reassigned thus potentially creating bad state of the
330 * We do not have to lock the control socket mutex here since at this stage
331 * there is no one referencing to this relayd object.
333 (void) relayd_close(&relayd
->control_sock
);
334 (void) relayd_close(&relayd
->data_sock
);
336 pthread_mutex_destroy(&relayd
->ctrl_sock_mutex
);
341 * Destroy and free relayd socket pair object.
343 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
346 struct lttng_ht_iter iter
;
348 if (relayd
== NULL
) {
352 DBG("Consumer destroy and close relayd socket pair");
354 iter
.iter
.node
= &relayd
->node
.node
;
355 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
357 /* We assume the relayd is being or is destroyed */
361 /* RCU free() call */
362 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
366 * Remove a channel from the global list protected by a mutex. This function is
367 * also responsible for freeing its data structures.
369 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
371 struct lttng_ht_iter iter
;
373 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
375 pthread_mutex_lock(&consumer_data
.lock
);
376 pthread_mutex_lock(&channel
->lock
);
378 /* Destroy streams that might have been left in the stream list. */
379 clean_channel_stream_list(channel
);
381 if (channel
->live_timer_enabled
== 1) {
382 consumer_timer_live_stop(channel
);
384 if (channel
->monitor_timer_enabled
== 1) {
385 consumer_timer_monitor_stop(channel
);
388 switch (consumer_data
.type
) {
389 case LTTNG_CONSUMER_KERNEL
:
391 case LTTNG_CONSUMER32_UST
:
392 case LTTNG_CONSUMER64_UST
:
393 lttng_ustconsumer_del_channel(channel
);
396 ERR("Unknown consumer_data type");
401 lttng_trace_chunk_put(channel
->trace_chunk
);
402 channel
->trace_chunk
= NULL
;
404 if (channel
->is_published
) {
408 iter
.iter
.node
= &channel
->node
.node
;
409 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
412 iter
.iter
.node
= &channel
->channels_by_session_id_ht_node
.node
;
413 ret
= lttng_ht_del(consumer_data
.channels_by_session_id_ht
,
419 channel
->is_deleted
= true;
420 call_rcu(&channel
->node
.head
, free_channel_rcu
);
422 pthread_mutex_unlock(&channel
->lock
);
423 pthread_mutex_unlock(&consumer_data
.lock
);
427 * Iterate over the relayd hash table and destroy each element. Finally,
428 * destroy the whole hash table.
430 static void cleanup_relayd_ht(void)
432 struct lttng_ht_iter iter
;
433 struct consumer_relayd_sock_pair
*relayd
;
437 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
439 consumer_destroy_relayd(relayd
);
444 lttng_ht_destroy(consumer_data
.relayd_ht
);
448 * Update the end point status of all streams having the given network sequence
449 * index (relayd index).
451 * It's atomically set without having the stream mutex locked which is fine
452 * because we handle the write/read race with a pipe wakeup for each thread.
454 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
455 enum consumer_endpoint_status status
)
457 struct lttng_ht_iter iter
;
458 struct lttng_consumer_stream
*stream
;
460 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
464 /* Let's begin with metadata */
465 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
466 if (stream
->net_seq_idx
== net_seq_idx
) {
467 uatomic_set(&stream
->endpoint_status
, status
);
468 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
472 /* Follow up by the data streams */
473 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
474 if (stream
->net_seq_idx
== net_seq_idx
) {
475 uatomic_set(&stream
->endpoint_status
, status
);
476 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
483 * Cleanup a relayd object by flagging every associated streams for deletion,
484 * destroying the object meaning removing it from the relayd hash table,
485 * closing the sockets and freeing the memory in a RCU call.
487 * If a local data context is available, notify the threads that the streams'
488 * state have changed.
490 void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
)
496 DBG("Cleaning up relayd object ID %"PRIu64
, relayd
->net_seq_idx
);
498 /* Save the net sequence index before destroying the object */
499 netidx
= relayd
->net_seq_idx
;
502 * Delete the relayd from the relayd hash table, close the sockets and free
503 * the object in a RCU call.
505 consumer_destroy_relayd(relayd
);
507 /* Set inactive endpoint to all streams */
508 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
511 * With a local data context, notify the threads that the streams' state
512 * have changed. The write() action on the pipe acts as an "implicit"
513 * memory barrier ordering the updates of the end point status from the
514 * read of this status which happens AFTER receiving this notify.
516 notify_thread_lttng_pipe(relayd
->ctx
->consumer_data_pipe
);
517 notify_thread_lttng_pipe(relayd
->ctx
->consumer_metadata_pipe
);
521 * Flag a relayd socket pair for destruction. Destroy it if the refcount
524 * RCU read side lock MUST be aquired before calling this function.
526 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
530 /* Set destroy flag for this object */
531 uatomic_set(&relayd
->destroy_flag
, 1);
533 /* Destroy the relayd if refcount is 0 */
534 if (uatomic_read(&relayd
->refcount
) == 0) {
535 consumer_destroy_relayd(relayd
);
540 * Completly destroy stream from every visiable data structure and the given
543 * One this call returns, the stream object is not longer usable nor visible.
545 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
548 consumer_stream_destroy(stream
, ht
);
552 * XXX naming of del vs destroy is all mixed up.
554 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
556 consumer_stream_destroy(stream
, data_ht
);
559 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
561 consumer_stream_destroy(stream
, metadata_ht
);
564 void consumer_stream_update_channel_attributes(
565 struct lttng_consumer_stream
*stream
,
566 struct lttng_consumer_channel
*channel
)
568 stream
->channel_read_only_attributes
.tracefile_size
=
569 channel
->tracefile_size
;
573 * Add a stream to the global list protected by a mutex.
575 void consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
577 struct lttng_ht
*ht
= data_ht
;
582 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
584 pthread_mutex_lock(&consumer_data
.lock
);
585 pthread_mutex_lock(&stream
->chan
->lock
);
586 pthread_mutex_lock(&stream
->chan
->timer_lock
);
587 pthread_mutex_lock(&stream
->lock
);
590 /* Steal stream identifier to avoid having streams with the same key */
591 steal_stream_key(stream
->key
, ht
);
593 lttng_ht_add_unique_u64(ht
, &stream
->node
);
595 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
596 &stream
->node_channel_id
);
599 * Add stream to the stream_list_ht of the consumer data. No need to steal
600 * the key since the HT does not use it and we allow to add redundant keys
603 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
606 * When nb_init_stream_left reaches 0, we don't need to trigger any action
607 * in terms of destroying the associated channel, because the action that
608 * causes the count to become 0 also causes a stream to be added. The
609 * channel deletion will thus be triggered by the following removal of this
612 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
613 /* Increment refcount before decrementing nb_init_stream_left */
615 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
618 /* Update consumer data once the node is inserted. */
619 consumer_data
.stream_count
++;
620 consumer_data
.need_update
= 1;
623 pthread_mutex_unlock(&stream
->lock
);
624 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
625 pthread_mutex_unlock(&stream
->chan
->lock
);
626 pthread_mutex_unlock(&consumer_data
.lock
);
630 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
631 * be acquired before calling this.
633 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
636 struct lttng_ht_node_u64
*node
;
637 struct lttng_ht_iter iter
;
641 lttng_ht_lookup(consumer_data
.relayd_ht
,
642 &relayd
->net_seq_idx
, &iter
);
643 node
= lttng_ht_iter_get_node_u64(&iter
);
647 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
654 * Allocate and return a consumer relayd socket.
656 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
657 uint64_t net_seq_idx
)
659 struct consumer_relayd_sock_pair
*obj
= NULL
;
661 /* net sequence index of -1 is a failure */
662 if (net_seq_idx
== (uint64_t) -1ULL) {
666 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
668 PERROR("zmalloc relayd sock");
672 obj
->net_seq_idx
= net_seq_idx
;
674 obj
->destroy_flag
= 0;
675 obj
->control_sock
.sock
.fd
= -1;
676 obj
->data_sock
.sock
.fd
= -1;
677 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
678 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
685 * Find a relayd socket pair in the global consumer data.
687 * Return the object if found else NULL.
688 * RCU read-side lock must be held across this call and while using the
691 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
693 struct lttng_ht_iter iter
;
694 struct lttng_ht_node_u64
*node
;
695 struct consumer_relayd_sock_pair
*relayd
= NULL
;
697 /* Negative keys are lookup failures */
698 if (key
== (uint64_t) -1ULL) {
702 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
704 node
= lttng_ht_iter_get_node_u64(&iter
);
706 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
714 * Find a relayd and send the stream
716 * Returns 0 on success, < 0 on error
718 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
722 struct consumer_relayd_sock_pair
*relayd
;
725 assert(stream
->net_seq_idx
!= -1ULL);
728 /* The stream is not metadata. Get relayd reference if exists. */
730 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
731 if (relayd
!= NULL
) {
732 /* Add stream on the relayd */
733 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
734 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
735 get_consumer_domain(), path
, &stream
->relayd_stream_id
,
736 stream
->chan
->tracefile_size
,
737 stream
->chan
->tracefile_count
,
738 stream
->trace_chunk
);
739 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
741 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
742 lttng_consumer_cleanup_relayd(relayd
);
746 uatomic_inc(&relayd
->refcount
);
747 stream
->sent_to_relayd
= 1;
749 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
750 stream
->key
, stream
->net_seq_idx
);
755 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
756 stream
->name
, stream
->key
, stream
->net_seq_idx
);
764 * Find a relayd and send the streams sent message
766 * Returns 0 on success, < 0 on error
768 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
771 struct consumer_relayd_sock_pair
*relayd
;
773 assert(net_seq_idx
!= -1ULL);
775 /* The stream is not metadata. Get relayd reference if exists. */
777 relayd
= consumer_find_relayd(net_seq_idx
);
778 if (relayd
!= NULL
) {
779 /* Add stream on the relayd */
780 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
781 ret
= relayd_streams_sent(&relayd
->control_sock
);
782 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
784 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
785 lttng_consumer_cleanup_relayd(relayd
);
789 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
796 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
804 * Find a relayd and close the stream
806 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
808 struct consumer_relayd_sock_pair
*relayd
;
810 /* The stream is not metadata. Get relayd reference if exists. */
812 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
814 consumer_stream_relayd_close(stream
, relayd
);
820 * Handle stream for relayd transmission if the stream applies for network
821 * streaming where the net sequence index is set.
823 * Return destination file descriptor or negative value on error.
825 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
826 size_t data_size
, unsigned long padding
,
827 struct consumer_relayd_sock_pair
*relayd
)
830 struct lttcomm_relayd_data_hdr data_hdr
;
836 /* Reset data header */
837 memset(&data_hdr
, 0, sizeof(data_hdr
));
839 if (stream
->metadata_flag
) {
840 /* Caller MUST acquire the relayd control socket lock */
841 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
846 /* Metadata are always sent on the control socket. */
847 outfd
= relayd
->control_sock
.sock
.fd
;
849 /* Set header with stream information */
850 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
851 data_hdr
.data_size
= htobe32(data_size
);
852 data_hdr
.padding_size
= htobe32(padding
);
855 * Note that net_seq_num below is assigned with the *current* value of
856 * next_net_seq_num and only after that the next_net_seq_num will be
857 * increment. This is why when issuing a command on the relayd using
858 * this next value, 1 should always be substracted in order to compare
859 * the last seen sequence number on the relayd side to the last sent.
861 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
862 /* Other fields are zeroed previously */
864 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
870 ++stream
->next_net_seq_num
;
872 /* Set to go on data socket */
873 outfd
= relayd
->data_sock
.sock
.fd
;
881 * Trigger a dump of the metadata content. Following/during the succesful
882 * completion of this call, the metadata poll thread will start receiving
883 * metadata packets to consume.
885 * The caller must hold the channel and stream locks.
888 int consumer_metadata_stream_dump(struct lttng_consumer_stream
*stream
)
892 ASSERT_LOCKED(stream
->chan
->lock
);
893 ASSERT_LOCKED(stream
->lock
);
894 assert(stream
->metadata_flag
);
895 assert(stream
->chan
->trace_chunk
);
897 switch (consumer_data
.type
) {
898 case LTTNG_CONSUMER_KERNEL
:
900 * Reset the position of what has been read from the
901 * metadata cache to 0 so we can dump it again.
903 ret
= kernctl_metadata_cache_dump(stream
->wait_fd
);
905 case LTTNG_CONSUMER32_UST
:
906 case LTTNG_CONSUMER64_UST
:
908 * Reset the position pushed from the metadata cache so it
909 * will write from the beginning on the next push.
911 stream
->ust_metadata_pushed
= 0;
912 ret
= consumer_metadata_wakeup_pipe(stream
->chan
);
915 ERR("Unknown consumer_data type");
919 ERR("Failed to dump the metadata cache");
925 int lttng_consumer_channel_set_trace_chunk(
926 struct lttng_consumer_channel
*channel
,
927 struct lttng_trace_chunk
*new_trace_chunk
)
929 pthread_mutex_lock(&channel
->lock
);
930 if (channel
->is_deleted
) {
932 * The channel has been logically deleted and should no longer
933 * be used. It has released its reference to its current trace
934 * chunk and should not acquire a new one.
936 * Return success as there is nothing for the caller to do.
942 * The acquisition of the reference cannot fail (barring
943 * a severe internal error) since a reference to the published
944 * chunk is already held by the caller.
946 if (new_trace_chunk
) {
947 const bool acquired_reference
= lttng_trace_chunk_get(
950 assert(acquired_reference
);
953 lttng_trace_chunk_put(channel
->trace_chunk
);
954 channel
->trace_chunk
= new_trace_chunk
;
956 pthread_mutex_unlock(&channel
->lock
);
961 * Allocate and return a new lttng_consumer_channel object using the given key
962 * to initialize the hash table node.
964 * On error, return NULL.
966 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
968 const uint64_t *chunk_id
,
969 const char *pathname
,
972 enum lttng_event_output output
,
973 uint64_t tracefile_size
,
974 uint64_t tracefile_count
,
975 uint64_t session_id_per_pid
,
976 unsigned int monitor
,
977 unsigned int live_timer_interval
,
978 bool is_in_live_session
,
979 const char *root_shm_path
,
980 const char *shm_path
)
982 struct lttng_consumer_channel
*channel
= NULL
;
983 struct lttng_trace_chunk
*trace_chunk
= NULL
;
986 trace_chunk
= lttng_trace_chunk_registry_find_chunk(
987 consumer_data
.chunk_registry
, session_id
,
990 ERR("Failed to find trace chunk reference during creation of channel");
995 channel
= zmalloc(sizeof(*channel
));
996 if (channel
== NULL
) {
997 PERROR("malloc struct lttng_consumer_channel");
1002 channel
->refcount
= 0;
1003 channel
->session_id
= session_id
;
1004 channel
->session_id_per_pid
= session_id_per_pid
;
1005 channel
->relayd_id
= relayd_id
;
1006 channel
->tracefile_size
= tracefile_size
;
1007 channel
->tracefile_count
= tracefile_count
;
1008 channel
->monitor
= monitor
;
1009 channel
->live_timer_interval
= live_timer_interval
;
1010 channel
->is_live
= is_in_live_session
;
1011 pthread_mutex_init(&channel
->lock
, NULL
);
1012 pthread_mutex_init(&channel
->timer_lock
, NULL
);
1015 case LTTNG_EVENT_SPLICE
:
1016 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
1018 case LTTNG_EVENT_MMAP
:
1019 channel
->output
= CONSUMER_CHANNEL_MMAP
;
1029 * In monitor mode, the streams associated with the channel will be put in
1030 * a special list ONLY owned by this channel. So, the refcount is set to 1
1031 * here meaning that the channel itself has streams that are referenced.
1033 * On a channel deletion, once the channel is no longer visible, the
1034 * refcount is decremented and checked for a zero value to delete it. With
1035 * streams in no monitor mode, it will now be safe to destroy the channel.
1037 if (!channel
->monitor
) {
1038 channel
->refcount
= 1;
1041 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1042 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1044 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1045 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1047 if (root_shm_path
) {
1048 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1049 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1052 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1053 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1056 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1057 lttng_ht_node_init_u64(&channel
->channels_by_session_id_ht_node
,
1058 channel
->session_id
);
1060 channel
->wait_fd
= -1;
1061 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1064 int ret
= lttng_consumer_channel_set_trace_chunk(channel
,
1071 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1074 lttng_trace_chunk_put(trace_chunk
);
1077 consumer_del_channel(channel
);
1083 * Add a channel to the global list protected by a mutex.
1085 * Always return 0 indicating success.
1087 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1088 struct lttng_consumer_local_data
*ctx
)
1090 pthread_mutex_lock(&consumer_data
.lock
);
1091 pthread_mutex_lock(&channel
->lock
);
1092 pthread_mutex_lock(&channel
->timer_lock
);
1095 * This gives us a guarantee that the channel we are about to add to the
1096 * channel hash table will be unique. See this function comment on the why
1097 * we need to steel the channel key at this stage.
1099 steal_channel_key(channel
->key
);
1102 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1103 lttng_ht_add_u64(consumer_data
.channels_by_session_id_ht
,
1104 &channel
->channels_by_session_id_ht_node
);
1106 channel
->is_published
= true;
1108 pthread_mutex_unlock(&channel
->timer_lock
);
1109 pthread_mutex_unlock(&channel
->lock
);
1110 pthread_mutex_unlock(&consumer_data
.lock
);
1112 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1113 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1120 * Allocate the pollfd structure and the local view of the out fds to avoid
1121 * doing a lookup in the linked list and concurrency issues when writing is
1122 * needed. Called with consumer_data.lock held.
1124 * Returns the number of fds in the structures.
1126 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1127 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1128 struct lttng_ht
*ht
, int *nb_inactive_fd
)
1131 struct lttng_ht_iter iter
;
1132 struct lttng_consumer_stream
*stream
;
1137 assert(local_stream
);
1139 DBG("Updating poll fd array");
1140 *nb_inactive_fd
= 0;
1142 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1144 * Only active streams with an active end point can be added to the
1145 * poll set and local stream storage of the thread.
1147 * There is a potential race here for endpoint_status to be updated
1148 * just after the check. However, this is OK since the stream(s) will
1149 * be deleted once the thread is notified that the end point state has
1150 * changed where this function will be called back again.
1152 * We track the number of inactive FDs because they still need to be
1153 * closed by the polling thread after a wakeup on the data_pipe or
1156 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1157 (*nb_inactive_fd
)++;
1161 * This clobbers way too much the debug output. Uncomment that if you
1162 * need it for debugging purposes.
1164 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1165 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1166 local_stream
[i
] = stream
;
1172 * Insert the consumer_data_pipe at the end of the array and don't
1173 * increment i so nb_fd is the number of real FD.
1175 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1176 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1178 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1179 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1184 * Poll on the should_quit pipe and the command socket return -1 on
1185 * error, 1 if should exit, 0 if data is available on the command socket
1187 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1192 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1193 if (num_rdy
== -1) {
1195 * Restart interrupted system call.
1197 if (errno
== EINTR
) {
1200 PERROR("Poll error");
1203 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1204 DBG("consumer_should_quit wake up");
1211 * Set the error socket.
1213 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1216 ctx
->consumer_error_socket
= sock
;
1220 * Set the command socket path.
1222 void lttng_consumer_set_command_sock_path(
1223 struct lttng_consumer_local_data
*ctx
, char *sock
)
1225 ctx
->consumer_command_sock_path
= sock
;
1229 * Send return code to the session daemon.
1230 * If the socket is not defined, we return 0, it is not a fatal error
1232 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1234 if (ctx
->consumer_error_socket
> 0) {
1235 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1236 sizeof(enum lttcomm_sessiond_command
));
1243 * Close all the tracefiles and stream fds and MUST be called when all
1244 * instances are destroyed i.e. when all threads were joined and are ended.
1246 void lttng_consumer_cleanup(void)
1248 struct lttng_ht_iter iter
;
1249 struct lttng_consumer_channel
*channel
;
1250 unsigned int trace_chunks_left
;
1254 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1256 consumer_del_channel(channel
);
1261 lttng_ht_destroy(consumer_data
.channel_ht
);
1262 lttng_ht_destroy(consumer_data
.channels_by_session_id_ht
);
1264 cleanup_relayd_ht();
1266 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1269 * This HT contains streams that are freed by either the metadata thread or
1270 * the data thread so we do *nothing* on the hash table and simply destroy
1273 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1276 * Trace chunks in the registry may still exist if the session
1277 * daemon has encountered an internal error and could not
1278 * tear down its sessions and/or trace chunks properly.
1280 * Release the session daemon's implicit reference to any remaining
1281 * trace chunk and print an error if any trace chunk was found. Note
1282 * that there are _no_ legitimate cases for trace chunks to be left,
1283 * it is a leak. However, it can happen following a crash of the
1284 * session daemon and not emptying the registry would cause an assertion
1287 trace_chunks_left
= lttng_trace_chunk_registry_put_each_chunk(
1288 consumer_data
.chunk_registry
);
1289 if (trace_chunks_left
) {
1290 ERR("%u trace chunks are leaked by lttng-consumerd. "
1291 "This can be caused by an internal error of the session daemon.",
1294 /* Run all callbacks freeing each chunk. */
1296 lttng_trace_chunk_registry_destroy(consumer_data
.chunk_registry
);
1300 * Called from signal handler.
1302 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1306 CMM_STORE_SHARED(consumer_quit
, 1);
1307 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1309 PERROR("write consumer quit");
1312 DBG("Consumer flag that it should quit");
1317 * Flush pending writes to trace output disk file.
1320 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1324 int outfd
= stream
->out_fd
;
1327 * This does a blocking write-and-wait on any page that belongs to the
1328 * subbuffer prior to the one we just wrote.
1329 * Don't care about error values, as these are just hints and ways to
1330 * limit the amount of page cache used.
1332 if (orig_offset
< stream
->max_sb_size
) {
1335 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1336 stream
->max_sb_size
,
1337 SYNC_FILE_RANGE_WAIT_BEFORE
1338 | SYNC_FILE_RANGE_WRITE
1339 | SYNC_FILE_RANGE_WAIT_AFTER
);
1341 * Give hints to the kernel about how we access the file:
1342 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1345 * We need to call fadvise again after the file grows because the
1346 * kernel does not seem to apply fadvise to non-existing parts of the
1349 * Call fadvise _after_ having waited for the page writeback to
1350 * complete because the dirty page writeback semantic is not well
1351 * defined. So it can be expected to lead to lower throughput in
1354 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1355 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1356 if (ret
&& ret
!= -ENOSYS
) {
1358 PERROR("posix_fadvise on fd %i", outfd
);
1363 * Initialise the necessary environnement :
1364 * - create a new context
1365 * - create the poll_pipe
1366 * - create the should_quit pipe (for signal handler)
1367 * - create the thread pipe (for splice)
1369 * Takes a function pointer as argument, this function is called when data is
1370 * available on a buffer. This function is responsible to do the
1371 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1372 * buffer configuration and then kernctl_put_next_subbuf at the end.
1374 * Returns a pointer to the new context or NULL on error.
1376 struct lttng_consumer_local_data
*lttng_consumer_create(
1377 enum lttng_consumer_type type
,
1378 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1379 struct lttng_consumer_local_data
*ctx
, bool locked_by_caller
),
1380 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1381 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1382 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1385 struct lttng_consumer_local_data
*ctx
;
1387 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1388 consumer_data
.type
== type
);
1389 consumer_data
.type
= type
;
1391 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1393 PERROR("allocating context");
1397 ctx
->consumer_error_socket
= -1;
1398 ctx
->consumer_metadata_socket
= -1;
1399 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1400 /* assign the callbacks */
1401 ctx
->on_buffer_ready
= buffer_ready
;
1402 ctx
->on_recv_channel
= recv_channel
;
1403 ctx
->on_recv_stream
= recv_stream
;
1404 ctx
->on_update_stream
= update_stream
;
1406 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1407 if (!ctx
->consumer_data_pipe
) {
1408 goto error_poll_pipe
;
1411 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1412 if (!ctx
->consumer_wakeup_pipe
) {
1413 goto error_wakeup_pipe
;
1416 ret
= pipe(ctx
->consumer_should_quit
);
1418 PERROR("Error creating recv pipe");
1419 goto error_quit_pipe
;
1422 ret
= pipe(ctx
->consumer_channel_pipe
);
1424 PERROR("Error creating channel pipe");
1425 goto error_channel_pipe
;
1428 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1429 if (!ctx
->consumer_metadata_pipe
) {
1430 goto error_metadata_pipe
;
1433 ctx
->channel_monitor_pipe
= -1;
1437 error_metadata_pipe
:
1438 utils_close_pipe(ctx
->consumer_channel_pipe
);
1440 utils_close_pipe(ctx
->consumer_should_quit
);
1442 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1444 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1452 * Iterate over all streams of the hashtable and free them properly.
1454 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1456 struct lttng_ht_iter iter
;
1457 struct lttng_consumer_stream
*stream
;
1464 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1466 * Ignore return value since we are currently cleaning up so any error
1469 (void) consumer_del_stream(stream
, ht
);
1473 lttng_ht_destroy(ht
);
1477 * Iterate over all streams of the metadata hashtable and free them
1480 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1482 struct lttng_ht_iter iter
;
1483 struct lttng_consumer_stream
*stream
;
1490 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1492 * Ignore return value since we are currently cleaning up so any error
1495 (void) consumer_del_metadata_stream(stream
, ht
);
1499 lttng_ht_destroy(ht
);
1503 * Close all fds associated with the instance and free the context.
1505 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1509 DBG("Consumer destroying it. Closing everything.");
1515 destroy_data_stream_ht(data_ht
);
1516 destroy_metadata_stream_ht(metadata_ht
);
1518 ret
= close(ctx
->consumer_error_socket
);
1522 ret
= close(ctx
->consumer_metadata_socket
);
1526 utils_close_pipe(ctx
->consumer_channel_pipe
);
1527 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1528 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1529 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1530 utils_close_pipe(ctx
->consumer_should_quit
);
1532 unlink(ctx
->consumer_command_sock_path
);
1537 * Write the metadata stream id on the specified file descriptor.
1539 static int write_relayd_metadata_id(int fd
,
1540 struct lttng_consumer_stream
*stream
,
1541 unsigned long padding
)
1544 struct lttcomm_relayd_metadata_payload hdr
;
1546 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1547 hdr
.padding_size
= htobe32(padding
);
1548 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1549 if (ret
< sizeof(hdr
)) {
1551 * This error means that the fd's end is closed so ignore the PERROR
1552 * not to clubber the error output since this can happen in a normal
1555 if (errno
!= EPIPE
) {
1556 PERROR("write metadata stream id");
1558 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1560 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1561 * handle writting the missing part so report that as an error and
1562 * don't lie to the caller.
1567 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1568 stream
->relayd_stream_id
, padding
);
1575 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1576 * core function for writing trace buffers to either the local filesystem or
1579 * It must be called with the stream and the channel lock held.
1581 * Careful review MUST be put if any changes occur!
1583 * Returns the number of bytes written
1585 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1586 struct lttng_consumer_stream
*stream
,
1587 const struct lttng_buffer_view
*buffer
,
1588 unsigned long padding
)
1591 off_t orig_offset
= stream
->out_fd_offset
;
1592 /* Default is on the disk */
1593 int outfd
= stream
->out_fd
;
1594 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1595 unsigned int relayd_hang_up
= 0;
1596 const size_t subbuf_content_size
= buffer
->size
- padding
;
1599 /* RCU lock for the relayd pointer */
1601 assert(stream
->net_seq_idx
!= (uint64_t) -1ULL ||
1602 stream
->trace_chunk
);
1604 /* Flag that the current stream if set for network streaming. */
1605 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1606 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1607 if (relayd
== NULL
) {
1613 /* Handle stream on the relayd if the output is on the network */
1615 unsigned long netlen
= subbuf_content_size
;
1618 * Lock the control socket for the complete duration of the function
1619 * since from this point on we will use the socket.
1621 if (stream
->metadata_flag
) {
1622 /* Metadata requires the control socket. */
1623 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1624 if (stream
->reset_metadata_flag
) {
1625 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1626 stream
->relayd_stream_id
,
1627 stream
->metadata_version
);
1632 stream
->reset_metadata_flag
= 0;
1634 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1637 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1642 /* Use the returned socket. */
1645 /* Write metadata stream id before payload */
1646 if (stream
->metadata_flag
) {
1647 ret
= write_relayd_metadata_id(outfd
, stream
, padding
);
1654 write_len
= subbuf_content_size
;
1656 /* No streaming; we have to write the full padding. */
1657 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1658 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1660 ERR("Reset metadata file");
1663 stream
->reset_metadata_flag
= 0;
1667 * Check if we need to change the tracefile before writing the packet.
1669 if (stream
->chan
->tracefile_size
> 0 &&
1670 (stream
->tracefile_size_current
+ buffer
->size
) >
1671 stream
->chan
->tracefile_size
) {
1672 ret
= consumer_stream_rotate_output_files(stream
);
1676 outfd
= stream
->out_fd
;
1679 stream
->tracefile_size_current
+= buffer
->size
;
1680 write_len
= buffer
->size
;
1684 * This call guarantee that len or less is returned. It's impossible to
1685 * receive a ret value that is bigger than len.
1687 ret
= lttng_write(outfd
, buffer
->data
, write_len
);
1688 DBG("Consumer mmap write() ret %zd (len %zu)", ret
, write_len
);
1689 if (ret
< 0 || ((size_t) ret
!= write_len
)) {
1691 * Report error to caller if nothing was written else at least send the
1699 /* Socket operation failed. We consider the relayd dead */
1700 if (errno
== EPIPE
) {
1702 * This is possible if the fd is closed on the other side
1703 * (outfd) or any write problem. It can be verbose a bit for a
1704 * normal execution if for instance the relayd is stopped
1705 * abruptly. This can happen so set this to a DBG statement.
1707 DBG("Consumer mmap write detected relayd hang up");
1709 /* Unhandled error, print it and stop function right now. */
1710 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret
,
1715 stream
->output_written
+= ret
;
1717 /* This call is useless on a socket so better save a syscall. */
1719 /* This won't block, but will start writeout asynchronously */
1720 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, write_len
,
1721 SYNC_FILE_RANGE_WRITE
);
1722 stream
->out_fd_offset
+= write_len
;
1723 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1728 * This is a special case that the relayd has closed its socket. Let's
1729 * cleanup the relayd object and all associated streams.
1731 if (relayd
&& relayd_hang_up
) {
1732 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1733 lttng_consumer_cleanup_relayd(relayd
);
1737 /* Unlock only if ctrl socket used */
1738 if (relayd
&& stream
->metadata_flag
) {
1739 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1747 * Splice the data from the ring buffer to the tracefile.
1749 * It must be called with the stream lock held.
1751 * Returns the number of bytes spliced.
1753 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1754 struct lttng_consumer_local_data
*ctx
,
1755 struct lttng_consumer_stream
*stream
, unsigned long len
,
1756 unsigned long padding
)
1758 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1760 off_t orig_offset
= stream
->out_fd_offset
;
1761 int fd
= stream
->wait_fd
;
1762 /* Default is on the disk */
1763 int outfd
= stream
->out_fd
;
1764 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1766 unsigned int relayd_hang_up
= 0;
1768 switch (consumer_data
.type
) {
1769 case LTTNG_CONSUMER_KERNEL
:
1771 case LTTNG_CONSUMER32_UST
:
1772 case LTTNG_CONSUMER64_UST
:
1773 /* Not supported for user space tracing */
1776 ERR("Unknown consumer_data type");
1780 /* RCU lock for the relayd pointer */
1783 /* Flag that the current stream if set for network streaming. */
1784 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1785 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1786 if (relayd
== NULL
) {
1791 splice_pipe
= stream
->splice_pipe
;
1793 /* Write metadata stream id before payload */
1795 unsigned long total_len
= len
;
1797 if (stream
->metadata_flag
) {
1799 * Lock the control socket for the complete duration of the function
1800 * since from this point on we will use the socket.
1802 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1804 if (stream
->reset_metadata_flag
) {
1805 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1806 stream
->relayd_stream_id
,
1807 stream
->metadata_version
);
1812 stream
->reset_metadata_flag
= 0;
1814 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
,
1822 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1825 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1831 /* Use the returned socket. */
1834 /* No streaming, we have to set the len with the full padding */
1837 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1838 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1840 ERR("Reset metadata file");
1843 stream
->reset_metadata_flag
= 0;
1846 * Check if we need to change the tracefile before writing the packet.
1848 if (stream
->chan
->tracefile_size
> 0 &&
1849 (stream
->tracefile_size_current
+ len
) >
1850 stream
->chan
->tracefile_size
) {
1851 ret
= consumer_stream_rotate_output_files(stream
);
1856 outfd
= stream
->out_fd
;
1859 stream
->tracefile_size_current
+= len
;
1863 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1864 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1865 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1866 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1867 DBG("splice chan to pipe, ret %zd", ret_splice
);
1868 if (ret_splice
< 0) {
1871 PERROR("Error in relay splice");
1875 /* Handle stream on the relayd if the output is on the network */
1876 if (relayd
&& stream
->metadata_flag
) {
1877 size_t metadata_payload_size
=
1878 sizeof(struct lttcomm_relayd_metadata_payload
);
1880 /* Update counter to fit the spliced data */
1881 ret_splice
+= metadata_payload_size
;
1882 len
+= metadata_payload_size
;
1884 * We do this so the return value can match the len passed as
1885 * argument to this function.
1887 written
-= metadata_payload_size
;
1890 /* Splice data out */
1891 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1892 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1893 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1895 if (ret_splice
< 0) {
1900 } else if (ret_splice
> len
) {
1902 * We don't expect this code path to be executed but you never know
1903 * so this is an extra protection agains a buggy splice().
1906 written
+= ret_splice
;
1907 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1911 /* All good, update current len and continue. */
1915 /* This call is useless on a socket so better save a syscall. */
1917 /* This won't block, but will start writeout asynchronously */
1918 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1919 SYNC_FILE_RANGE_WRITE
);
1920 stream
->out_fd_offset
+= ret_splice
;
1922 stream
->output_written
+= ret_splice
;
1923 written
+= ret_splice
;
1926 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1932 * This is a special case that the relayd has closed its socket. Let's
1933 * cleanup the relayd object and all associated streams.
1935 if (relayd
&& relayd_hang_up
) {
1936 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1937 lttng_consumer_cleanup_relayd(relayd
);
1938 /* Skip splice error so the consumer does not fail */
1943 /* send the appropriate error description to sessiond */
1946 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1949 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1952 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1957 if (relayd
&& stream
->metadata_flag
) {
1958 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1966 * Sample the snapshot positions for a specific fd
1968 * Returns 0 on success, < 0 on error
1970 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
1972 switch (consumer_data
.type
) {
1973 case LTTNG_CONSUMER_KERNEL
:
1974 return lttng_kconsumer_sample_snapshot_positions(stream
);
1975 case LTTNG_CONSUMER32_UST
:
1976 case LTTNG_CONSUMER64_UST
:
1977 return lttng_ustconsumer_sample_snapshot_positions(stream
);
1979 ERR("Unknown consumer_data type");
1985 * Take a snapshot for a specific fd
1987 * Returns 0 on success, < 0 on error
1989 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1991 switch (consumer_data
.type
) {
1992 case LTTNG_CONSUMER_KERNEL
:
1993 return lttng_kconsumer_take_snapshot(stream
);
1994 case LTTNG_CONSUMER32_UST
:
1995 case LTTNG_CONSUMER64_UST
:
1996 return lttng_ustconsumer_take_snapshot(stream
);
1998 ERR("Unknown consumer_data type");
2005 * Get the produced position
2007 * Returns 0 on success, < 0 on error
2009 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
2012 switch (consumer_data
.type
) {
2013 case LTTNG_CONSUMER_KERNEL
:
2014 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
2015 case LTTNG_CONSUMER32_UST
:
2016 case LTTNG_CONSUMER64_UST
:
2017 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2019 ERR("Unknown consumer_data type");
2026 * Get the consumed position (free-running counter position in bytes).
2028 * Returns 0 on success, < 0 on error
2030 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
2033 switch (consumer_data
.type
) {
2034 case LTTNG_CONSUMER_KERNEL
:
2035 return lttng_kconsumer_get_consumed_snapshot(stream
, pos
);
2036 case LTTNG_CONSUMER32_UST
:
2037 case LTTNG_CONSUMER64_UST
:
2038 return lttng_ustconsumer_get_consumed_snapshot(stream
, pos
);
2040 ERR("Unknown consumer_data type");
2046 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2047 int sock
, struct pollfd
*consumer_sockpoll
)
2049 switch (consumer_data
.type
) {
2050 case LTTNG_CONSUMER_KERNEL
:
2051 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2052 case LTTNG_CONSUMER32_UST
:
2053 case LTTNG_CONSUMER64_UST
:
2054 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2056 ERR("Unknown consumer_data type");
2063 void lttng_consumer_close_all_metadata(void)
2065 switch (consumer_data
.type
) {
2066 case LTTNG_CONSUMER_KERNEL
:
2068 * The Kernel consumer has a different metadata scheme so we don't
2069 * close anything because the stream will be closed by the session
2073 case LTTNG_CONSUMER32_UST
:
2074 case LTTNG_CONSUMER64_UST
:
2076 * Close all metadata streams. The metadata hash table is passed and
2077 * this call iterates over it by closing all wakeup fd. This is safe
2078 * because at this point we are sure that the metadata producer is
2079 * either dead or blocked.
2081 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2084 ERR("Unknown consumer_data type");
2090 * Clean up a metadata stream and free its memory.
2092 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2093 struct lttng_ht
*ht
)
2095 struct lttng_consumer_channel
*channel
= NULL
;
2096 bool free_channel
= false;
2100 * This call should NEVER receive regular stream. It must always be
2101 * metadata stream and this is crucial for data structure synchronization.
2103 assert(stream
->metadata_flag
);
2105 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2107 pthread_mutex_lock(&consumer_data
.lock
);
2109 * Note that this assumes that a stream's channel is never changed and
2110 * that the stream's lock doesn't need to be taken to sample its
2113 channel
= stream
->chan
;
2114 pthread_mutex_lock(&channel
->lock
);
2115 pthread_mutex_lock(&stream
->lock
);
2116 if (channel
->metadata_cache
) {
2117 /* Only applicable to userspace consumers. */
2118 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
2121 /* Remove any reference to that stream. */
2122 consumer_stream_delete(stream
, ht
);
2124 /* Close down everything including the relayd if one. */
2125 consumer_stream_close(stream
);
2126 /* Destroy tracer buffers of the stream. */
2127 consumer_stream_destroy_buffers(stream
);
2129 /* Atomically decrement channel refcount since other threads can use it. */
2130 if (!uatomic_sub_return(&channel
->refcount
, 1)
2131 && !uatomic_read(&channel
->nb_init_stream_left
)) {
2132 /* Go for channel deletion! */
2133 free_channel
= true;
2135 stream
->chan
= NULL
;
2138 * Nullify the stream reference so it is not used after deletion. The
2139 * channel lock MUST be acquired before being able to check for a NULL
2142 channel
->metadata_stream
= NULL
;
2144 if (channel
->metadata_cache
) {
2145 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
2147 pthread_mutex_unlock(&stream
->lock
);
2148 pthread_mutex_unlock(&channel
->lock
);
2149 pthread_mutex_unlock(&consumer_data
.lock
);
2152 consumer_del_channel(channel
);
2155 lttng_trace_chunk_put(stream
->trace_chunk
);
2156 stream
->trace_chunk
= NULL
;
2157 consumer_stream_free(stream
);
2161 * Action done with the metadata stream when adding it to the consumer internal
2162 * data structures to handle it.
2164 void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2166 struct lttng_ht
*ht
= metadata_ht
;
2167 struct lttng_ht_iter iter
;
2168 struct lttng_ht_node_u64
*node
;
2173 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2175 pthread_mutex_lock(&consumer_data
.lock
);
2176 pthread_mutex_lock(&stream
->chan
->lock
);
2177 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2178 pthread_mutex_lock(&stream
->lock
);
2181 * From here, refcounts are updated so be _careful_ when returning an error
2188 * Lookup the stream just to make sure it does not exist in our internal
2189 * state. This should NEVER happen.
2191 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2192 node
= lttng_ht_iter_get_node_u64(&iter
);
2196 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2197 * in terms of destroying the associated channel, because the action that
2198 * causes the count to become 0 also causes a stream to be added. The
2199 * channel deletion will thus be triggered by the following removal of this
2202 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2203 /* Increment refcount before decrementing nb_init_stream_left */
2205 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2208 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2210 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
2211 &stream
->node_channel_id
);
2214 * Add stream to the stream_list_ht of the consumer data. No need to steal
2215 * the key since the HT does not use it and we allow to add redundant keys
2218 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2222 pthread_mutex_unlock(&stream
->lock
);
2223 pthread_mutex_unlock(&stream
->chan
->lock
);
2224 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2225 pthread_mutex_unlock(&consumer_data
.lock
);
2229 * Delete data stream that are flagged for deletion (endpoint_status).
2231 static void validate_endpoint_status_data_stream(void)
2233 struct lttng_ht_iter iter
;
2234 struct lttng_consumer_stream
*stream
;
2236 DBG("Consumer delete flagged data stream");
2239 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2240 /* Validate delete flag of the stream */
2241 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2244 /* Delete it right now */
2245 consumer_del_stream(stream
, data_ht
);
2251 * Delete metadata stream that are flagged for deletion (endpoint_status).
2253 static void validate_endpoint_status_metadata_stream(
2254 struct lttng_poll_event
*pollset
)
2256 struct lttng_ht_iter iter
;
2257 struct lttng_consumer_stream
*stream
;
2259 DBG("Consumer delete flagged metadata stream");
2264 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2265 /* Validate delete flag of the stream */
2266 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2270 * Remove from pollset so the metadata thread can continue without
2271 * blocking on a deleted stream.
2273 lttng_poll_del(pollset
, stream
->wait_fd
);
2275 /* Delete it right now */
2276 consumer_del_metadata_stream(stream
, metadata_ht
);
2282 * Thread polls on metadata file descriptor and write them on disk or on the
2285 void *consumer_thread_metadata_poll(void *data
)
2287 int ret
, i
, pollfd
, err
= -1;
2288 uint32_t revents
, nb_fd
;
2289 struct lttng_consumer_stream
*stream
= NULL
;
2290 struct lttng_ht_iter iter
;
2291 struct lttng_ht_node_u64
*node
;
2292 struct lttng_poll_event events
;
2293 struct lttng_consumer_local_data
*ctx
= data
;
2296 rcu_register_thread();
2298 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2300 if (testpoint(consumerd_thread_metadata
)) {
2301 goto error_testpoint
;
2304 health_code_update();
2306 DBG("Thread metadata poll started");
2308 /* Size is set to 1 for the consumer_metadata pipe */
2309 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2311 ERR("Poll set creation failed");
2315 ret
= lttng_poll_add(&events
,
2316 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2322 DBG("Metadata main loop started");
2326 health_code_update();
2327 health_poll_entry();
2328 DBG("Metadata poll wait");
2329 ret
= lttng_poll_wait(&events
, -1);
2330 DBG("Metadata poll return from wait with %d fd(s)",
2331 LTTNG_POLL_GETNB(&events
));
2333 DBG("Metadata event caught in thread");
2335 if (errno
== EINTR
) {
2336 ERR("Poll EINTR caught");
2339 if (LTTNG_POLL_GETNB(&events
) == 0) {
2340 err
= 0; /* All is OK */
2347 /* From here, the event is a metadata wait fd */
2348 for (i
= 0; i
< nb_fd
; i
++) {
2349 health_code_update();
2351 revents
= LTTNG_POLL_GETEV(&events
, i
);
2352 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2354 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2355 if (revents
& LPOLLIN
) {
2358 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2359 &stream
, sizeof(stream
));
2360 if (pipe_len
< sizeof(stream
)) {
2362 PERROR("read metadata stream");
2365 * Remove the pipe from the poll set and continue the loop
2366 * since their might be data to consume.
2368 lttng_poll_del(&events
,
2369 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2370 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2374 /* A NULL stream means that the state has changed. */
2375 if (stream
== NULL
) {
2376 /* Check for deleted streams. */
2377 validate_endpoint_status_metadata_stream(&events
);
2381 DBG("Adding metadata stream %d to poll set",
2384 /* Add metadata stream to the global poll events list */
2385 lttng_poll_add(&events
, stream
->wait_fd
,
2386 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2387 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2388 DBG("Metadata thread pipe hung up");
2390 * Remove the pipe from the poll set and continue the loop
2391 * since their might be data to consume.
2393 lttng_poll_del(&events
,
2394 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2395 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2398 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2402 /* Handle other stream */
2408 uint64_t tmp_id
= (uint64_t) pollfd
;
2410 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2412 node
= lttng_ht_iter_get_node_u64(&iter
);
2415 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2418 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2419 /* Get the data out of the metadata file descriptor */
2420 DBG("Metadata available on fd %d", pollfd
);
2421 assert(stream
->wait_fd
== pollfd
);
2424 health_code_update();
2426 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2428 * We don't check the return value here since if we get
2429 * a negative len, it means an error occurred thus we
2430 * simply remove it from the poll set and free the
2435 /* It's ok to have an unavailable sub-buffer */
2436 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2437 /* Clean up stream from consumer and free it. */
2438 lttng_poll_del(&events
, stream
->wait_fd
);
2439 consumer_del_metadata_stream(stream
, metadata_ht
);
2441 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2442 DBG("Metadata fd %d is hup|err.", pollfd
);
2443 if (!stream
->hangup_flush_done
2444 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2445 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2446 DBG("Attempting to flush and consume the UST buffers");
2447 lttng_ustconsumer_on_stream_hangup(stream
);
2449 /* We just flushed the stream now read it. */
2451 health_code_update();
2453 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2455 * We don't check the return value here since if we get
2456 * a negative len, it means an error occurred thus we
2457 * simply remove it from the poll set and free the
2463 lttng_poll_del(&events
, stream
->wait_fd
);
2465 * This call update the channel states, closes file descriptors
2466 * and securely free the stream.
2468 consumer_del_metadata_stream(stream
, metadata_ht
);
2470 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2474 /* Release RCU lock for the stream looked up */
2482 DBG("Metadata poll thread exiting");
2484 lttng_poll_clean(&events
);
2489 ERR("Health error occurred in %s", __func__
);
2491 health_unregister(health_consumerd
);
2492 rcu_unregister_thread();
2497 * This thread polls the fds in the set to consume the data and write
2498 * it to tracefile if necessary.
2500 void *consumer_thread_data_poll(void *data
)
2502 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2503 struct pollfd
*pollfd
= NULL
;
2504 /* local view of the streams */
2505 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2506 /* local view of consumer_data.fds_count */
2508 /* 2 for the consumer_data_pipe and wake up pipe */
2509 const int nb_pipes_fd
= 2;
2510 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2511 int nb_inactive_fd
= 0;
2512 struct lttng_consumer_local_data
*ctx
= data
;
2515 rcu_register_thread();
2517 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2519 if (testpoint(consumerd_thread_data
)) {
2520 goto error_testpoint
;
2523 health_code_update();
2525 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2526 if (local_stream
== NULL
) {
2527 PERROR("local_stream malloc");
2532 health_code_update();
2538 * the fds set has been updated, we need to update our
2539 * local array as well
2541 pthread_mutex_lock(&consumer_data
.lock
);
2542 if (consumer_data
.need_update
) {
2547 local_stream
= NULL
;
2549 /* Allocate for all fds */
2550 pollfd
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) * sizeof(struct pollfd
));
2551 if (pollfd
== NULL
) {
2552 PERROR("pollfd malloc");
2553 pthread_mutex_unlock(&consumer_data
.lock
);
2557 local_stream
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) *
2558 sizeof(struct lttng_consumer_stream
*));
2559 if (local_stream
== NULL
) {
2560 PERROR("local_stream malloc");
2561 pthread_mutex_unlock(&consumer_data
.lock
);
2564 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2565 data_ht
, &nb_inactive_fd
);
2567 ERR("Error in allocating pollfd or local_outfds");
2568 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2569 pthread_mutex_unlock(&consumer_data
.lock
);
2573 consumer_data
.need_update
= 0;
2575 pthread_mutex_unlock(&consumer_data
.lock
);
2577 /* No FDs and consumer_quit, consumer_cleanup the thread */
2578 if (nb_fd
== 0 && nb_inactive_fd
== 0 &&
2579 CMM_LOAD_SHARED(consumer_quit
) == 1) {
2580 err
= 0; /* All is OK */
2583 /* poll on the array of fds */
2585 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2586 if (testpoint(consumerd_thread_data_poll
)) {
2589 health_poll_entry();
2590 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2592 DBG("poll num_rdy : %d", num_rdy
);
2593 if (num_rdy
== -1) {
2595 * Restart interrupted system call.
2597 if (errno
== EINTR
) {
2600 PERROR("Poll error");
2601 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2603 } else if (num_rdy
== 0) {
2604 DBG("Polling thread timed out");
2608 if (caa_unlikely(data_consumption_paused
)) {
2609 DBG("Data consumption paused, sleeping...");
2615 * If the consumer_data_pipe triggered poll go directly to the
2616 * beginning of the loop to update the array. We want to prioritize
2617 * array update over low-priority reads.
2619 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2620 ssize_t pipe_readlen
;
2622 DBG("consumer_data_pipe wake up");
2623 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2624 &new_stream
, sizeof(new_stream
));
2625 if (pipe_readlen
< sizeof(new_stream
)) {
2626 PERROR("Consumer data pipe");
2627 /* Continue so we can at least handle the current stream(s). */
2632 * If the stream is NULL, just ignore it. It's also possible that
2633 * the sessiond poll thread changed the consumer_quit state and is
2634 * waking us up to test it.
2636 if (new_stream
== NULL
) {
2637 validate_endpoint_status_data_stream();
2641 /* Continue to update the local streams and handle prio ones */
2645 /* Handle wakeup pipe. */
2646 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2648 ssize_t pipe_readlen
;
2650 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2652 if (pipe_readlen
< 0) {
2653 PERROR("Consumer data wakeup pipe");
2655 /* We've been awakened to handle stream(s). */
2656 ctx
->has_wakeup
= 0;
2659 /* Take care of high priority channels first. */
2660 for (i
= 0; i
< nb_fd
; i
++) {
2661 health_code_update();
2663 if (local_stream
[i
] == NULL
) {
2666 if (pollfd
[i
].revents
& POLLPRI
) {
2667 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2669 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2670 /* it's ok to have an unavailable sub-buffer */
2671 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2672 /* Clean the stream and free it. */
2673 consumer_del_stream(local_stream
[i
], data_ht
);
2674 local_stream
[i
] = NULL
;
2675 } else if (len
> 0) {
2676 local_stream
[i
]->data_read
= 1;
2682 * If we read high prio channel in this loop, try again
2683 * for more high prio data.
2689 /* Take care of low priority channels. */
2690 for (i
= 0; i
< nb_fd
; i
++) {
2691 health_code_update();
2693 if (local_stream
[i
] == NULL
) {
2696 if ((pollfd
[i
].revents
& POLLIN
) ||
2697 local_stream
[i
]->hangup_flush_done
||
2698 local_stream
[i
]->has_data
) {
2699 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2700 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2701 /* it's ok to have an unavailable sub-buffer */
2702 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2703 /* Clean the stream and free it. */
2704 consumer_del_stream(local_stream
[i
], data_ht
);
2705 local_stream
[i
] = NULL
;
2706 } else if (len
> 0) {
2707 local_stream
[i
]->data_read
= 1;
2712 /* Handle hangup and errors */
2713 for (i
= 0; i
< nb_fd
; i
++) {
2714 health_code_update();
2716 if (local_stream
[i
] == NULL
) {
2719 if (!local_stream
[i
]->hangup_flush_done
2720 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2721 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2722 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2723 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2725 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2726 /* Attempt read again, for the data we just flushed. */
2727 local_stream
[i
]->data_read
= 1;
2730 * If the poll flag is HUP/ERR/NVAL and we have
2731 * read no data in this pass, we can remove the
2732 * stream from its hash table.
2734 if ((pollfd
[i
].revents
& POLLHUP
)) {
2735 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2736 if (!local_stream
[i
]->data_read
) {
2737 consumer_del_stream(local_stream
[i
], data_ht
);
2738 local_stream
[i
] = NULL
;
2741 } else if (pollfd
[i
].revents
& POLLERR
) {
2742 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2743 if (!local_stream
[i
]->data_read
) {
2744 consumer_del_stream(local_stream
[i
], data_ht
);
2745 local_stream
[i
] = NULL
;
2748 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2749 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2750 if (!local_stream
[i
]->data_read
) {
2751 consumer_del_stream(local_stream
[i
], data_ht
);
2752 local_stream
[i
] = NULL
;
2756 if (local_stream
[i
] != NULL
) {
2757 local_stream
[i
]->data_read
= 0;
2764 DBG("polling thread exiting");
2769 * Close the write side of the pipe so epoll_wait() in
2770 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2771 * read side of the pipe. If we close them both, epoll_wait strangely does
2772 * not return and could create a endless wait period if the pipe is the
2773 * only tracked fd in the poll set. The thread will take care of closing
2776 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2781 ERR("Health error occurred in %s", __func__
);
2783 health_unregister(health_consumerd
);
2785 rcu_unregister_thread();
2790 * Close wake-up end of each stream belonging to the channel. This will
2791 * allow the poll() on the stream read-side to detect when the
2792 * write-side (application) finally closes them.
2795 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2797 struct lttng_ht
*ht
;
2798 struct lttng_consumer_stream
*stream
;
2799 struct lttng_ht_iter iter
;
2801 ht
= consumer_data
.stream_per_chan_id_ht
;
2804 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2805 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2806 ht
->match_fct
, &channel
->key
,
2807 &iter
.iter
, stream
, node_channel_id
.node
) {
2809 * Protect against teardown with mutex.
2811 pthread_mutex_lock(&stream
->lock
);
2812 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2815 switch (consumer_data
.type
) {
2816 case LTTNG_CONSUMER_KERNEL
:
2818 case LTTNG_CONSUMER32_UST
:
2819 case LTTNG_CONSUMER64_UST
:
2820 if (stream
->metadata_flag
) {
2821 /* Safe and protected by the stream lock. */
2822 lttng_ustconsumer_close_metadata(stream
->chan
);
2825 * Note: a mutex is taken internally within
2826 * liblttng-ust-ctl to protect timer wakeup_fd
2827 * use from concurrent close.
2829 lttng_ustconsumer_close_stream_wakeup(stream
);
2833 ERR("Unknown consumer_data type");
2837 pthread_mutex_unlock(&stream
->lock
);
2842 static void destroy_channel_ht(struct lttng_ht
*ht
)
2844 struct lttng_ht_iter iter
;
2845 struct lttng_consumer_channel
*channel
;
2853 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2854 ret
= lttng_ht_del(ht
, &iter
);
2859 lttng_ht_destroy(ht
);
2863 * This thread polls the channel fds to detect when they are being
2864 * closed. It closes all related streams if the channel is detected as
2865 * closed. It is currently only used as a shim layer for UST because the
2866 * consumerd needs to keep the per-stream wakeup end of pipes open for
2869 void *consumer_thread_channel_poll(void *data
)
2871 int ret
, i
, pollfd
, err
= -1;
2872 uint32_t revents
, nb_fd
;
2873 struct lttng_consumer_channel
*chan
= NULL
;
2874 struct lttng_ht_iter iter
;
2875 struct lttng_ht_node_u64
*node
;
2876 struct lttng_poll_event events
;
2877 struct lttng_consumer_local_data
*ctx
= data
;
2878 struct lttng_ht
*channel_ht
;
2880 rcu_register_thread();
2882 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2884 if (testpoint(consumerd_thread_channel
)) {
2885 goto error_testpoint
;
2888 health_code_update();
2890 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2892 /* ENOMEM at this point. Better to bail out. */
2896 DBG("Thread channel poll started");
2898 /* Size is set to 1 for the consumer_channel pipe */
2899 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2901 ERR("Poll set creation failed");
2905 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2911 DBG("Channel main loop started");
2915 health_code_update();
2916 DBG("Channel poll wait");
2917 health_poll_entry();
2918 ret
= lttng_poll_wait(&events
, -1);
2919 DBG("Channel poll return from wait with %d fd(s)",
2920 LTTNG_POLL_GETNB(&events
));
2922 DBG("Channel event caught in thread");
2924 if (errno
== EINTR
) {
2925 ERR("Poll EINTR caught");
2928 if (LTTNG_POLL_GETNB(&events
) == 0) {
2929 err
= 0; /* All is OK */
2936 /* From here, the event is a channel wait fd */
2937 for (i
= 0; i
< nb_fd
; i
++) {
2938 health_code_update();
2940 revents
= LTTNG_POLL_GETEV(&events
, i
);
2941 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2943 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2944 if (revents
& LPOLLIN
) {
2945 enum consumer_channel_action action
;
2948 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2951 ERR("Error reading channel pipe");
2953 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2958 case CONSUMER_CHANNEL_ADD
:
2959 DBG("Adding channel %d to poll set",
2962 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2965 lttng_ht_add_unique_u64(channel_ht
,
2966 &chan
->wait_fd_node
);
2968 /* Add channel to the global poll events list */
2969 lttng_poll_add(&events
, chan
->wait_fd
,
2970 LPOLLERR
| LPOLLHUP
);
2972 case CONSUMER_CHANNEL_DEL
:
2975 * This command should never be called if the channel
2976 * has streams monitored by either the data or metadata
2977 * thread. The consumer only notify this thread with a
2978 * channel del. command if it receives a destroy
2979 * channel command from the session daemon that send it
2980 * if a command prior to the GET_CHANNEL failed.
2984 chan
= consumer_find_channel(key
);
2987 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2990 lttng_poll_del(&events
, chan
->wait_fd
);
2991 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2992 ret
= lttng_ht_del(channel_ht
, &iter
);
2995 switch (consumer_data
.type
) {
2996 case LTTNG_CONSUMER_KERNEL
:
2998 case LTTNG_CONSUMER32_UST
:
2999 case LTTNG_CONSUMER64_UST
:
3000 health_code_update();
3001 /* Destroy streams that might have been left in the stream list. */
3002 clean_channel_stream_list(chan
);
3005 ERR("Unknown consumer_data type");
3010 * Release our own refcount. Force channel deletion even if
3011 * streams were not initialized.
3013 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3014 consumer_del_channel(chan
);
3019 case CONSUMER_CHANNEL_QUIT
:
3021 * Remove the pipe from the poll set and continue the loop
3022 * since their might be data to consume.
3024 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3027 ERR("Unknown action");
3030 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3031 DBG("Channel thread pipe hung up");
3033 * Remove the pipe from the poll set and continue the loop
3034 * since their might be data to consume.
3036 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3039 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3043 /* Handle other stream */
3049 uint64_t tmp_id
= (uint64_t) pollfd
;
3051 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3053 node
= lttng_ht_iter_get_node_u64(&iter
);
3056 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
3059 /* Check for error event */
3060 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3061 DBG("Channel fd %d is hup|err.", pollfd
);
3063 lttng_poll_del(&events
, chan
->wait_fd
);
3064 ret
= lttng_ht_del(channel_ht
, &iter
);
3068 * This will close the wait fd for each stream associated to
3069 * this channel AND monitored by the data/metadata thread thus
3070 * will be clean by the right thread.
3072 consumer_close_channel_streams(chan
);
3074 /* Release our own refcount */
3075 if (!uatomic_sub_return(&chan
->refcount
, 1)
3076 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3077 consumer_del_channel(chan
);
3080 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3085 /* Release RCU lock for the channel looked up */
3093 lttng_poll_clean(&events
);
3095 destroy_channel_ht(channel_ht
);
3098 DBG("Channel poll thread exiting");
3101 ERR("Health error occurred in %s", __func__
);
3103 health_unregister(health_consumerd
);
3104 rcu_unregister_thread();
3108 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3109 struct pollfd
*sockpoll
, int client_socket
)
3116 ret
= lttng_consumer_poll_socket(sockpoll
);
3120 DBG("Metadata connection on client_socket");
3122 /* Blocking call, waiting for transmission */
3123 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3124 if (ctx
->consumer_metadata_socket
< 0) {
3125 WARN("On accept metadata");
3136 * This thread listens on the consumerd socket and receives the file
3137 * descriptors from the session daemon.
3139 void *consumer_thread_sessiond_poll(void *data
)
3141 int sock
= -1, client_socket
, ret
, err
= -1;
3143 * structure to poll for incoming data on communication socket avoids
3144 * making blocking sockets.
3146 struct pollfd consumer_sockpoll
[2];
3147 struct lttng_consumer_local_data
*ctx
= data
;
3149 rcu_register_thread();
3151 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3153 if (testpoint(consumerd_thread_sessiond
)) {
3154 goto error_testpoint
;
3157 health_code_update();
3159 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3160 unlink(ctx
->consumer_command_sock_path
);
3161 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3162 if (client_socket
< 0) {
3163 ERR("Cannot create command socket");
3167 ret
= lttcomm_listen_unix_sock(client_socket
);
3172 DBG("Sending ready command to lttng-sessiond");
3173 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3174 /* return < 0 on error, but == 0 is not fatal */
3176 ERR("Error sending ready command to lttng-sessiond");
3180 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3181 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3182 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3183 consumer_sockpoll
[1].fd
= client_socket
;
3184 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3186 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3194 DBG("Connection on client_socket");
3196 /* Blocking call, waiting for transmission */
3197 sock
= lttcomm_accept_unix_sock(client_socket
);
3204 * Setup metadata socket which is the second socket connection on the
3205 * command unix socket.
3207 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3216 /* This socket is not useful anymore. */
3217 ret
= close(client_socket
);
3219 PERROR("close client_socket");
3223 /* update the polling structure to poll on the established socket */
3224 consumer_sockpoll
[1].fd
= sock
;
3225 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3228 health_code_update();
3230 health_poll_entry();
3231 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3240 DBG("Incoming command on sock");
3241 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3244 * This could simply be a session daemon quitting. Don't output
3247 DBG("Communication interrupted on command socket");
3251 if (CMM_LOAD_SHARED(consumer_quit
)) {
3252 DBG("consumer_thread_receive_fds received quit from signal");
3253 err
= 0; /* All is OK */
3256 DBG("Received command on sock");
3262 DBG("Consumer thread sessiond poll exiting");
3265 * Close metadata streams since the producer is the session daemon which
3268 * NOTE: for now, this only applies to the UST tracer.
3270 lttng_consumer_close_all_metadata();
3273 * when all fds have hung up, the polling thread
3276 CMM_STORE_SHARED(consumer_quit
, 1);
3279 * Notify the data poll thread to poll back again and test the
3280 * consumer_quit state that we just set so to quit gracefully.
3282 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3284 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3286 notify_health_quit_pipe(health_quit_pipe
);
3288 /* Cleaning up possibly open sockets. */
3292 PERROR("close sock sessiond poll");
3295 if (client_socket
>= 0) {
3296 ret
= close(client_socket
);
3298 PERROR("close client_socket sessiond poll");
3305 ERR("Health error occurred in %s", __func__
);
3307 health_unregister(health_consumerd
);
3309 rcu_unregister_thread();
3313 static int post_consume(struct lttng_consumer_stream
*stream
,
3314 const struct stream_subbuffer
*subbuffer
,
3315 struct lttng_consumer_local_data
*ctx
)
3319 const size_t count
= lttng_dynamic_array_get_count(
3320 &stream
->read_subbuffer_ops
.post_consume_cbs
);
3322 for (i
= 0; i
< count
; i
++) {
3323 const post_consume_cb op
= *(post_consume_cb
*) lttng_dynamic_array_get_element(
3324 &stream
->read_subbuffer_ops
.post_consume_cbs
,
3327 ret
= op(stream
, subbuffer
, ctx
);
3336 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3337 struct lttng_consumer_local_data
*ctx
,
3338 bool locked_by_caller
)
3340 ssize_t ret
, written_bytes
= 0;
3342 struct stream_subbuffer subbuffer
= {};
3344 if (!locked_by_caller
) {
3345 stream
->read_subbuffer_ops
.lock(stream
);
3348 if (stream
->read_subbuffer_ops
.on_wake_up
) {
3349 ret
= stream
->read_subbuffer_ops
.on_wake_up(stream
);
3356 * If the stream was flagged to be ready for rotation before we extract
3357 * the next packet, rotate it now.
3359 if (stream
->rotate_ready
) {
3360 DBG("Rotate stream before consuming data");
3361 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3363 ERR("Stream rotation error before consuming data");
3368 ret
= stream
->read_subbuffer_ops
.get_next_subbuffer(stream
, &subbuffer
);
3370 if (ret
== -ENODATA
) {
3378 ret
= stream
->read_subbuffer_ops
.pre_consume_subbuffer(
3379 stream
, &subbuffer
);
3381 goto error_put_subbuf
;
3384 written_bytes
= stream
->read_subbuffer_ops
.consume_subbuffer(
3385 ctx
, stream
, &subbuffer
);
3387 * Should write subbuf_size amount of data when network streaming or
3388 * the full padded size when we are not streaming.
3390 if ((written_bytes
!= subbuffer
.info
.data
.subbuf_size
&&
3391 stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
3392 (written_bytes
!= subbuffer
.info
.data
.padded_subbuf_size
&&
3393 stream
->net_seq_idx
==
3394 (uint64_t) -1ULL)) {
3396 * Display the error but continue processing to try to
3397 * release the subbuffer. This is a DBG statement
3398 * since this can happen without being a critical
3401 DBG("Failed to write to tracefile (written_bytes: %zd != padded subbuffer size: %lu, subbuffer size: %lu)",
3402 written_bytes
, subbuffer
.info
.data
.padded_subbuf_size
,
3403 subbuffer
.info
.data
.subbuf_size
);
3406 ret
= stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3411 ret
= post_consume(stream
, &subbuffer
, ctx
);
3417 * After extracting the packet, we check if the stream is now ready to
3418 * be rotated and perform the action immediately.
3420 * Don't overwrite `ret` as callers expect the number of bytes
3421 * consumed to be returned on success.
3423 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
3424 if (rotation_ret
== 1) {
3425 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3426 if (rotation_ret
< 0) {
3428 ERR("Stream rotation error after consuming data");
3432 } else if (rotation_ret
< 0) {
3434 ERR("Failed to check if stream was ready to rotate after consuming data");
3439 if (stream
->read_subbuffer_ops
.on_sleep
) {
3440 stream
->read_subbuffer_ops
.on_sleep(stream
, ctx
);
3443 ret
= written_bytes
;
3445 if (!locked_by_caller
) {
3446 stream
->read_subbuffer_ops
.unlock(stream
);
3451 (void) stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3455 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3457 switch (consumer_data
.type
) {
3458 case LTTNG_CONSUMER_KERNEL
:
3459 return lttng_kconsumer_on_recv_stream(stream
);
3460 case LTTNG_CONSUMER32_UST
:
3461 case LTTNG_CONSUMER64_UST
:
3462 return lttng_ustconsumer_on_recv_stream(stream
);
3464 ERR("Unknown consumer_data type");
3471 * Allocate and set consumer data hash tables.
3473 int lttng_consumer_init(void)
3475 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3476 if (!consumer_data
.channel_ht
) {
3480 consumer_data
.channels_by_session_id_ht
=
3481 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3482 if (!consumer_data
.channels_by_session_id_ht
) {
3486 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3487 if (!consumer_data
.relayd_ht
) {
3491 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3492 if (!consumer_data
.stream_list_ht
) {
3496 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3497 if (!consumer_data
.stream_per_chan_id_ht
) {
3501 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3506 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3511 consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3512 if (!consumer_data
.chunk_registry
) {
3523 * Process the ADD_RELAYD command receive by a consumer.
3525 * This will create a relayd socket pair and add it to the relayd hash table.
3526 * The caller MUST acquire a RCU read side lock before calling it.
3528 void consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3529 struct lttng_consumer_local_data
*ctx
, int sock
,
3530 struct pollfd
*consumer_sockpoll
,
3531 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3532 uint64_t relayd_session_id
)
3534 int fd
= -1, ret
= -1, relayd_created
= 0;
3535 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3536 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3539 assert(relayd_sock
);
3541 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3543 /* Get relayd reference if exists. */
3544 relayd
= consumer_find_relayd(net_seq_idx
);
3545 if (relayd
== NULL
) {
3546 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3547 /* Not found. Allocate one. */
3548 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3549 if (relayd
== NULL
) {
3550 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3553 relayd
->sessiond_session_id
= sessiond_id
;
3558 * This code path MUST continue to the consumer send status message to
3559 * we can notify the session daemon and continue our work without
3560 * killing everything.
3564 * relayd key should never be found for control socket.
3566 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3569 /* First send a status message before receiving the fds. */
3570 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3572 /* Somehow, the session daemon is not responding anymore. */
3573 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3574 goto error_nosignal
;
3577 /* Poll on consumer socket. */
3578 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3580 /* Needing to exit in the middle of a command: error. */
3581 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3582 goto error_nosignal
;
3585 /* Get relayd socket from session daemon */
3586 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3587 if (ret
!= sizeof(fd
)) {
3588 fd
= -1; /* Just in case it gets set with an invalid value. */
3591 * Failing to receive FDs might indicate a major problem such as
3592 * reaching a fd limit during the receive where the kernel returns a
3593 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3594 * don't take any chances and stop everything.
3596 * XXX: Feature request #558 will fix that and avoid this possible
3597 * issue when reaching the fd limit.
3599 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3600 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3604 /* Copy socket information and received FD */
3605 switch (sock_type
) {
3606 case LTTNG_STREAM_CONTROL
:
3607 /* Copy received lttcomm socket */
3608 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3609 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3610 /* Handle create_sock error. */
3612 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3616 * Close the socket created internally by
3617 * lttcomm_create_sock, so we can replace it by the one
3618 * received from sessiond.
3620 if (close(relayd
->control_sock
.sock
.fd
)) {
3624 /* Assign new file descriptor */
3625 relayd
->control_sock
.sock
.fd
= fd
;
3626 /* Assign version values. */
3627 relayd
->control_sock
.major
= relayd_sock
->major
;
3628 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3630 relayd
->relayd_session_id
= relayd_session_id
;
3633 case LTTNG_STREAM_DATA
:
3634 /* Copy received lttcomm socket */
3635 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3636 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3637 /* Handle create_sock error. */
3639 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3643 * Close the socket created internally by
3644 * lttcomm_create_sock, so we can replace it by the one
3645 * received from sessiond.
3647 if (close(relayd
->data_sock
.sock
.fd
)) {
3651 /* Assign new file descriptor */
3652 relayd
->data_sock
.sock
.fd
= fd
;
3653 /* Assign version values. */
3654 relayd
->data_sock
.major
= relayd_sock
->major
;
3655 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3658 ERR("Unknown relayd socket type (%d)", sock_type
);
3659 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3663 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3664 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3665 relayd
->net_seq_idx
, fd
);
3667 * We gave the ownership of the fd to the relayd structure. Set the
3668 * fd to -1 so we don't call close() on it in the error path below.
3672 /* We successfully added the socket. Send status back. */
3673 ret
= consumer_send_status_msg(sock
, ret_code
);
3675 /* Somehow, the session daemon is not responding anymore. */
3676 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3677 goto error_nosignal
;
3681 * Add relayd socket pair to consumer data hashtable. If object already
3682 * exists or on error, the function gracefully returns.
3691 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3692 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3696 /* Close received socket if valid. */
3699 PERROR("close received socket");
3703 if (relayd_created
) {
3709 * Search for a relayd associated to the session id and return the reference.
3711 * A rcu read side lock MUST be acquire before calling this function and locked
3712 * until the relayd object is no longer necessary.
3714 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3716 struct lttng_ht_iter iter
;
3717 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3719 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3720 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3723 * Check by sessiond id which is unique here where the relayd session
3724 * id might not be when having multiple relayd.
3726 if (relayd
->sessiond_session_id
== id
) {
3727 /* Found the relayd. There can be only one per id. */
3739 * Check if for a given session id there is still data needed to be extract
3742 * Return 1 if data is pending or else 0 meaning ready to be read.
3744 int consumer_data_pending(uint64_t id
)
3747 struct lttng_ht_iter iter
;
3748 struct lttng_ht
*ht
;
3749 struct lttng_consumer_stream
*stream
;
3750 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3751 int (*data_pending
)(struct lttng_consumer_stream
*);
3753 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3756 pthread_mutex_lock(&consumer_data
.lock
);
3758 switch (consumer_data
.type
) {
3759 case LTTNG_CONSUMER_KERNEL
:
3760 data_pending
= lttng_kconsumer_data_pending
;
3762 case LTTNG_CONSUMER32_UST
:
3763 case LTTNG_CONSUMER64_UST
:
3764 data_pending
= lttng_ustconsumer_data_pending
;
3767 ERR("Unknown consumer data type");
3771 /* Ease our life a bit */
3772 ht
= consumer_data
.stream_list_ht
;
3774 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3775 ht
->hash_fct(&id
, lttng_ht_seed
),
3777 &iter
.iter
, stream
, node_session_id
.node
) {
3778 pthread_mutex_lock(&stream
->lock
);
3781 * A removed node from the hash table indicates that the stream has
3782 * been deleted thus having a guarantee that the buffers are closed
3783 * on the consumer side. However, data can still be transmitted
3784 * over the network so don't skip the relayd check.
3786 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3788 /* Check the stream if there is data in the buffers. */
3789 ret
= data_pending(stream
);
3791 pthread_mutex_unlock(&stream
->lock
);
3796 pthread_mutex_unlock(&stream
->lock
);
3799 relayd
= find_relayd_by_session_id(id
);
3801 unsigned int is_data_inflight
= 0;
3803 /* Send init command for data pending. */
3804 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3805 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3806 relayd
->relayd_session_id
);
3808 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3809 /* Communication error thus the relayd so no data pending. */
3810 goto data_not_pending
;
3813 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3814 ht
->hash_fct(&id
, lttng_ht_seed
),
3816 &iter
.iter
, stream
, node_session_id
.node
) {
3817 if (stream
->metadata_flag
) {
3818 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3819 stream
->relayd_stream_id
);
3821 ret
= relayd_data_pending(&relayd
->control_sock
,
3822 stream
->relayd_stream_id
,
3823 stream
->next_net_seq_num
- 1);
3827 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3829 } else if (ret
< 0) {
3830 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3831 lttng_consumer_cleanup_relayd(relayd
);
3832 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3833 goto data_not_pending
;
3837 /* Send end command for data pending. */
3838 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3839 relayd
->relayd_session_id
, &is_data_inflight
);
3840 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3842 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3843 lttng_consumer_cleanup_relayd(relayd
);
3844 goto data_not_pending
;
3846 if (is_data_inflight
) {
3852 * Finding _no_ node in the hash table and no inflight data means that the
3853 * stream(s) have been removed thus data is guaranteed to be available for
3854 * analysis from the trace files.
3858 /* Data is available to be read by a viewer. */
3859 pthread_mutex_unlock(&consumer_data
.lock
);
3864 /* Data is still being extracted from buffers. */
3865 pthread_mutex_unlock(&consumer_data
.lock
);
3871 * Send a ret code status message to the sessiond daemon.
3873 * Return the sendmsg() return value.
3875 int consumer_send_status_msg(int sock
, int ret_code
)
3877 struct lttcomm_consumer_status_msg msg
;
3879 memset(&msg
, 0, sizeof(msg
));
3880 msg
.ret_code
= ret_code
;
3882 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3886 * Send a channel status message to the sessiond daemon.
3888 * Return the sendmsg() return value.
3890 int consumer_send_status_channel(int sock
,
3891 struct lttng_consumer_channel
*channel
)
3893 struct lttcomm_consumer_status_channel msg
;
3897 memset(&msg
, 0, sizeof(msg
));
3899 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3901 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3902 msg
.key
= channel
->key
;
3903 msg
.stream_count
= channel
->streams
.count
;
3906 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3909 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3910 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3911 uint64_t max_sb_size
)
3913 unsigned long start_pos
;
3915 if (!nb_packets_per_stream
) {
3916 return consumed_pos
; /* Grab everything */
3918 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
3919 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3920 if ((long) (start_pos
- consumed_pos
) < 0) {
3921 return consumed_pos
; /* Grab everything */
3927 * Sample the rotate position for all the streams of a channel. If a stream
3928 * is already at the rotate position (produced == consumed), we flag it as
3929 * ready for rotation. The rotation of ready streams occurs after we have
3930 * replied to the session daemon that we have finished sampling the positions.
3931 * Must be called with RCU read-side lock held to ensure existence of channel.
3933 * Returns 0 on success, < 0 on error
3935 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
3936 uint64_t key
, uint64_t relayd_id
, uint32_t metadata
,
3937 struct lttng_consumer_local_data
*ctx
)
3940 struct lttng_consumer_stream
*stream
;
3941 struct lttng_ht_iter iter
;
3942 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
3943 struct lttng_dynamic_array stream_rotation_positions
;
3944 uint64_t next_chunk_id
, stream_count
= 0;
3945 enum lttng_trace_chunk_status chunk_status
;
3946 const bool is_local_trace
= relayd_id
== -1ULL;
3947 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3948 bool rotating_to_new_chunk
= true;
3949 /* Array of `struct lttng_consumer_stream *` */
3950 struct lttng_dynamic_pointer_array streams_packet_to_open
;
3953 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
3955 lttng_dynamic_array_init(&stream_rotation_positions
,
3956 sizeof(struct relayd_stream_rotation_position
), NULL
);
3957 lttng_dynamic_pointer_array_init(&streams_packet_to_open
, NULL
);
3961 pthread_mutex_lock(&channel
->lock
);
3962 assert(channel
->trace_chunk
);
3963 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
,
3965 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
3967 goto end_unlock_channel
;
3970 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3971 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
3972 ht
->match_fct
, &channel
->key
, &iter
.iter
,
3973 stream
, node_channel_id
.node
) {
3974 unsigned long produced_pos
= 0, consumed_pos
= 0;
3976 health_code_update();
3979 * Lock stream because we are about to change its state.
3981 pthread_mutex_lock(&stream
->lock
);
3983 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
3984 rotating_to_new_chunk
= false;
3988 * Do not flush an empty packet when rotating from a NULL trace
3989 * chunk. The stream has no means to output data, and the prior
3990 * rotation which rotated to NULL performed that side-effect already.
3992 if (stream
->trace_chunk
) {
3994 * For metadata stream, do an active flush, which does not
3995 * produce empty packets. For data streams, empty-flush;
3996 * ensures we have at least one packet in each stream per trace
3997 * chunk, even if no data was produced.
3999 ret
= consumer_stream_flush_buffer(
4000 stream
, stream
->metadata_flag
? 1 : 0);
4002 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4004 goto end_unlock_stream
;
4008 ret
= lttng_consumer_take_snapshot(stream
);
4009 if (ret
< 0 && ret
!= -ENODATA
&& ret
!= -EAGAIN
) {
4010 ERR("Failed to sample snapshot position during channel rotation");
4011 goto end_unlock_stream
;
4014 ret
= lttng_consumer_get_produced_snapshot(stream
,
4017 ERR("Failed to sample produced position during channel rotation");
4018 goto end_unlock_stream
;
4021 ret
= lttng_consumer_get_consumed_snapshot(stream
,
4024 ERR("Failed to sample consumed position during channel rotation");
4025 goto end_unlock_stream
;
4029 * Align produced position on the start-of-packet boundary of the first
4030 * packet going into the next trace chunk.
4032 produced_pos
= ALIGN_FLOOR(produced_pos
, stream
->max_sb_size
);
4033 if (consumed_pos
== produced_pos
) {
4034 DBG("Set rotate ready for stream %" PRIu64
" produced = %lu consumed = %lu",
4035 stream
->key
, produced_pos
, consumed_pos
);
4036 stream
->rotate_ready
= true;
4038 DBG("Different consumed and produced positions "
4039 "for stream %" PRIu64
" produced = %lu consumed = %lu",
4040 stream
->key
, produced_pos
, consumed_pos
);
4043 * The rotation position is based on the packet_seq_num of the
4044 * packet following the last packet that was consumed for this
4045 * stream, incremented by the offset between produced and
4046 * consumed positions. This rotation position is a lower bound
4047 * (inclusive) at which the next trace chunk starts. Since it
4048 * is a lower bound, it is OK if the packet_seq_num does not
4049 * correspond exactly to the same packet identified by the
4050 * consumed_pos, which can happen in overwrite mode.
4052 if (stream
->sequence_number_unavailable
) {
4054 * Rotation should never be performed on a session which
4055 * interacts with a pre-2.8 lttng-modules, which does
4056 * not implement packet sequence number.
4058 ERR("Failure to rotate stream %" PRIu64
": sequence number unavailable",
4061 goto end_unlock_stream
;
4063 stream
->rotate_position
= stream
->last_sequence_number
+ 1 +
4064 ((produced_pos
- consumed_pos
) / stream
->max_sb_size
);
4065 DBG("Set rotation position for stream %" PRIu64
" at position %" PRIu64
,
4066 stream
->key
, stream
->rotate_position
);
4068 if (!is_local_trace
) {
4070 * The relay daemon control protocol expects a rotation
4071 * position as "the sequence number of the first packet
4072 * _after_ the current trace chunk".
4074 const struct relayd_stream_rotation_position position
= {
4075 .stream_id
= stream
->relayd_stream_id
,
4076 .rotate_at_seq_num
= stream
->rotate_position
,
4079 ret
= lttng_dynamic_array_add_element(
4080 &stream_rotation_positions
,
4083 ERR("Failed to allocate stream rotation position");
4084 goto end_unlock_stream
;
4089 stream
->opened_packet_in_current_trace_chunk
= false;
4091 if (rotating_to_new_chunk
&& !stream
->metadata_flag
) {
4093 * Attempt to flush an empty packet as close to the
4094 * rotation point as possible. In the event where a
4095 * stream remains inactive after the rotation point,
4096 * this ensures that the new trace chunk has a
4097 * beginning timestamp set at the begining of the
4098 * trace chunk instead of only creating an empty
4099 * packet when the trace chunk is stopped.
4101 * This indicates to the viewers that the stream
4102 * was being recorded, but more importantly it
4103 * allows viewers to determine a useable trace
4106 * This presents a problem in the case where the
4107 * ring-buffer is completely full.
4109 * Consider the following scenario:
4110 * - The consumption of data is slow (slow network,
4112 * - The ring buffer is full,
4113 * - A rotation is initiated,
4114 * - The flush below does nothing (no space left to
4115 * open a new packet),
4116 * - The other streams rotate very soon, and new
4117 * data is produced in the new chunk,
4118 * - This stream completes its rotation long after the
4119 * rotation was initiated
4120 * - The session is stopped before any event can be
4121 * produced in this stream's buffers.
4123 * The resulting trace chunk will have a single packet
4124 * temporaly at the end of the trace chunk for this
4125 * stream making the stream intersection more narrow
4126 * than it should be.
4128 * To work-around this, an empty flush is performed
4129 * after the first consumption of a packet during a
4130 * rotation if open_packet fails. The idea is that
4131 * consuming a packet frees enough space to switch
4132 * packets in this scenario and allows the tracer to
4133 * "stamp" the beginning of the new trace chunk at the
4134 * earliest possible point.
4136 * The packet open is performed after the channel
4137 * rotation to ensure that no attempt to open a packet
4138 * is performed in a stream that has no active trace
4141 ret
= lttng_dynamic_pointer_array_add_pointer(
4142 &streams_packet_to_open
, stream
);
4144 PERROR("Failed to add a stream pointer to array of streams in which to open a packet");
4146 goto end_unlock_stream
;
4150 pthread_mutex_unlock(&stream
->lock
);
4154 if (!is_local_trace
) {
4155 relayd
= consumer_find_relayd(relayd_id
);
4157 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4159 goto end_unlock_channel
;
4162 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4163 ret
= relayd_rotate_streams(&relayd
->control_sock
, stream_count
,
4164 rotating_to_new_chunk
? &next_chunk_id
: NULL
,
4165 (const struct relayd_stream_rotation_position
*)
4166 stream_rotation_positions
.buffer
4168 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4170 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4171 relayd
->net_seq_idx
);
4172 lttng_consumer_cleanup_relayd(relayd
);
4173 goto end_unlock_channel
;
4177 for (stream_idx
= 0;
4178 stream_idx
< lttng_dynamic_pointer_array_get_count(
4179 &streams_packet_to_open
);
4181 enum consumer_stream_open_packet_status status
;
4183 stream
= lttng_dynamic_pointer_array_get_pointer(
4184 &streams_packet_to_open
, stream_idx
);
4186 pthread_mutex_lock(&stream
->lock
);
4187 status
= consumer_stream_open_packet(stream
);
4188 pthread_mutex_unlock(&stream
->lock
);
4190 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
4191 DBG("Opened a packet after a rotation: stream id = %" PRIu64
4192 ", channel name = %s, session id = %" PRIu64
,
4193 stream
->key
, stream
->chan
->name
,
4194 stream
->chan
->session_id
);
4196 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
4198 * Can't open a packet as there is no space left
4199 * in the buffer. A new packet will be opened
4200 * once one has been consumed.
4202 DBG("No space left to open a packet after a rotation: stream id = %" PRIu64
4203 ", channel name = %s, session id = %" PRIu64
,
4204 stream
->key
, stream
->chan
->name
,
4205 stream
->chan
->session_id
);
4207 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
4208 /* Logged by callee. */
4210 goto end_unlock_channel
;
4216 pthread_mutex_unlock(&channel
->lock
);
4221 pthread_mutex_unlock(&stream
->lock
);
4223 pthread_mutex_unlock(&channel
->lock
);
4226 lttng_dynamic_array_reset(&stream_rotation_positions
);
4227 lttng_dynamic_pointer_array_reset(&streams_packet_to_open
);
4232 int consumer_clear_buffer(struct lttng_consumer_stream
*stream
)
4235 unsigned long consumed_pos_before
, consumed_pos_after
;
4237 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4239 ERR("Taking snapshot positions");
4243 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_before
);
4245 ERR("Consumed snapshot position");
4249 switch (consumer_data
.type
) {
4250 case LTTNG_CONSUMER_KERNEL
:
4251 ret
= kernctl_buffer_clear(stream
->wait_fd
);
4253 ERR("Failed to clear kernel stream (ret = %d)", ret
);
4257 case LTTNG_CONSUMER32_UST
:
4258 case LTTNG_CONSUMER64_UST
:
4259 lttng_ustconsumer_clear_buffer(stream
);
4262 ERR("Unknown consumer_data type");
4266 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4268 ERR("Taking snapshot positions");
4271 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_after
);
4273 ERR("Consumed snapshot position");
4276 DBG("clear: before: %lu after: %lu", consumed_pos_before
, consumed_pos_after
);
4282 int consumer_clear_stream(struct lttng_consumer_stream
*stream
)
4286 ret
= consumer_stream_flush_buffer(stream
, 1);
4288 ERR("Failed to flush stream %" PRIu64
" during channel clear",
4290 ret
= LTTCOMM_CONSUMERD_FATAL
;
4294 ret
= consumer_clear_buffer(stream
);
4296 ERR("Failed to clear stream %" PRIu64
" during channel clear",
4298 ret
= LTTCOMM_CONSUMERD_FATAL
;
4302 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4308 int consumer_clear_unmonitored_channel(struct lttng_consumer_channel
*channel
)
4311 struct lttng_consumer_stream
*stream
;
4314 pthread_mutex_lock(&channel
->lock
);
4315 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
4316 health_code_update();
4317 pthread_mutex_lock(&stream
->lock
);
4318 ret
= consumer_clear_stream(stream
);
4322 pthread_mutex_unlock(&stream
->lock
);
4324 pthread_mutex_unlock(&channel
->lock
);
4329 pthread_mutex_unlock(&stream
->lock
);
4330 pthread_mutex_unlock(&channel
->lock
);
4336 * Check if a stream is ready to be rotated after extracting it.
4338 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4339 * error. Stream lock must be held.
4341 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4343 DBG("Check is rotate ready for stream %" PRIu64
4344 " ready %u rotate_position %" PRIu64
4345 " last_sequence_number %" PRIu64
,
4346 stream
->key
, stream
->rotate_ready
,
4347 stream
->rotate_position
, stream
->last_sequence_number
);
4348 if (stream
->rotate_ready
) {
4353 * If packet seq num is unavailable, it means we are interacting
4354 * with a pre-2.8 lttng-modules which does not implement the
4355 * sequence number. Rotation should never be used by sessiond in this
4358 if (stream
->sequence_number_unavailable
) {
4359 ERR("Internal error: rotation used on stream %" PRIu64
4360 " with unavailable sequence number",
4365 if (stream
->rotate_position
== -1ULL ||
4366 stream
->last_sequence_number
== -1ULL) {
4371 * Rotate position not reached yet. The stream rotate position is
4372 * the position of the next packet belonging to the next trace chunk,
4373 * but consumerd considers rotation ready when reaching the last
4374 * packet of the current chunk, hence the "rotate_position - 1".
4377 DBG("Check is rotate ready for stream %" PRIu64
4378 " last_sequence_number %" PRIu64
4379 " rotate_position %" PRIu64
,
4380 stream
->key
, stream
->last_sequence_number
,
4381 stream
->rotate_position
);
4382 if (stream
->last_sequence_number
>= stream
->rotate_position
- 1) {
4390 * Reset the state for a stream after a rotation occurred.
4392 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4394 DBG("lttng_consumer_reset_stream_rotate_state for stream %" PRIu64
,
4396 stream
->rotate_position
= -1ULL;
4397 stream
->rotate_ready
= false;
4401 * Perform the rotation a local stream file.
4404 int rotate_local_stream(struct lttng_consumer_local_data
*ctx
,
4405 struct lttng_consumer_stream
*stream
)
4409 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4412 stream
->tracefile_size_current
= 0;
4413 stream
->tracefile_count_current
= 0;
4415 if (stream
->out_fd
>= 0) {
4416 ret
= close(stream
->out_fd
);
4418 PERROR("Failed to close stream out_fd of channel \"%s\"",
4419 stream
->chan
->name
);
4421 stream
->out_fd
= -1;
4424 if (stream
->index_file
) {
4425 lttng_index_file_put(stream
->index_file
);
4426 stream
->index_file
= NULL
;
4429 if (!stream
->trace_chunk
) {
4433 ret
= consumer_stream_create_output_files(stream
, true);
4439 * Performs the stream rotation for the rotate session feature if needed.
4440 * It must be called with the channel and stream locks held.
4442 * Return 0 on success, a negative number of error.
4444 int lttng_consumer_rotate_stream(struct lttng_consumer_local_data
*ctx
,
4445 struct lttng_consumer_stream
*stream
)
4449 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4452 * Update the stream's 'current' chunk to the session's (channel)
4453 * now-current chunk.
4455 lttng_trace_chunk_put(stream
->trace_chunk
);
4456 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4458 * A channel can be rotated and not have a "next" chunk
4459 * to transition to. In that case, the channel's "current chunk"
4460 * has not been closed yet, but it has not been updated to
4461 * a "next" trace chunk either. Hence, the stream, like its
4462 * parent channel, becomes part of no chunk and can't output
4463 * anything until a new trace chunk is created.
4465 stream
->trace_chunk
= NULL
;
4466 } else if (stream
->chan
->trace_chunk
&&
4467 !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4468 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4473 * Update the stream's trace chunk to its parent channel's
4474 * current trace chunk.
4476 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4479 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4480 ret
= rotate_local_stream(ctx
, stream
);
4482 ERR("Failed to rotate stream, ret = %i", ret
);
4487 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4489 * If the stream has transitioned to a new trace
4490 * chunk, the metadata should be re-dumped to the
4493 * However, it is possible for a stream to transition to
4494 * a "no-chunk" state. This can happen if a rotation
4495 * occurs on an inactive session. In such cases, the metadata
4496 * regeneration will happen when the next trace chunk is
4499 ret
= consumer_metadata_stream_dump(stream
);
4504 lttng_consumer_reset_stream_rotate_state(stream
);
4513 * Rotate all the ready streams now.
4515 * This is especially important for low throughput streams that have already
4516 * been consumed, we cannot wait for their next packet to perform the
4518 * Need to be called with RCU read-side lock held to ensure existence of
4521 * Returns 0 on success, < 0 on error
4523 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
,
4524 uint64_t key
, struct lttng_consumer_local_data
*ctx
)
4527 struct lttng_consumer_stream
*stream
;
4528 struct lttng_ht_iter iter
;
4529 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
4533 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4535 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4536 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4537 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4538 stream
, node_channel_id
.node
) {
4539 health_code_update();
4541 pthread_mutex_lock(&stream
->chan
->lock
);
4542 pthread_mutex_lock(&stream
->lock
);
4544 if (!stream
->rotate_ready
) {
4545 pthread_mutex_unlock(&stream
->lock
);
4546 pthread_mutex_unlock(&stream
->chan
->lock
);
4549 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4551 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
4552 pthread_mutex_unlock(&stream
->lock
);
4553 pthread_mutex_unlock(&stream
->chan
->lock
);
4566 enum lttcomm_return_code
lttng_consumer_init_command(
4567 struct lttng_consumer_local_data
*ctx
,
4568 const lttng_uuid sessiond_uuid
)
4570 enum lttcomm_return_code ret
;
4571 char uuid_str
[LTTNG_UUID_STR_LEN
];
4573 if (ctx
->sessiond_uuid
.is_set
) {
4574 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4578 ctx
->sessiond_uuid
.is_set
= true;
4579 memcpy(ctx
->sessiond_uuid
.value
, sessiond_uuid
, sizeof(lttng_uuid
));
4580 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4581 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4582 DBG("Received session daemon UUID: %s", uuid_str
);
4587 enum lttcomm_return_code
lttng_consumer_create_trace_chunk(
4588 const uint64_t *relayd_id
, uint64_t session_id
,
4590 time_t chunk_creation_timestamp
,
4591 const char *chunk_override_name
,
4592 const struct lttng_credentials
*credentials
,
4593 struct lttng_directory_handle
*chunk_directory_handle
)
4596 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4597 struct lttng_trace_chunk
*created_chunk
= NULL
, *published_chunk
= NULL
;
4598 enum lttng_trace_chunk_status chunk_status
;
4599 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4600 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4601 const char *relayd_id_str
= "(none)";
4602 const char *creation_timestamp_str
;
4603 struct lttng_ht_iter iter
;
4604 struct lttng_consumer_channel
*channel
;
4607 /* Only used for logging purposes. */
4608 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4609 "%" PRIu64
, *relayd_id
);
4610 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4611 relayd_id_str
= relayd_id_buffer
;
4613 relayd_id_str
= "(formatting error)";
4617 /* Local protocol error. */
4618 assert(chunk_creation_timestamp
);
4619 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4620 creation_timestamp_buffer
,
4621 sizeof(creation_timestamp_buffer
));
4622 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
4623 "(formatting error)";
4625 DBG("Consumer create trace chunk command: relay_id = %s"
4626 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4627 ", chunk_override_name = %s"
4628 ", chunk_creation_timestamp = %s",
4629 relayd_id_str
, session_id
, chunk_id
,
4630 chunk_override_name
? : "(none)",
4631 creation_timestamp_str
);
4634 * The trace chunk registry, as used by the consumer daemon, implicitly
4635 * owns the trace chunks. This is only needed in the consumer since
4636 * the consumer has no notion of a session beyond session IDs being
4637 * used to identify other objects.
4639 * The lttng_trace_chunk_registry_publish() call below provides a
4640 * reference which is not released; it implicitly becomes the session
4641 * daemon's reference to the chunk in the consumer daemon.
4643 * The lifetime of trace chunks in the consumer daemon is managed by
4644 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4645 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4647 created_chunk
= lttng_trace_chunk_create(chunk_id
,
4648 chunk_creation_timestamp
, NULL
);
4649 if (!created_chunk
) {
4650 ERR("Failed to create trace chunk");
4651 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4655 if (chunk_override_name
) {
4656 chunk_status
= lttng_trace_chunk_override_name(created_chunk
,
4657 chunk_override_name
);
4658 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4659 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4664 if (chunk_directory_handle
) {
4665 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
,
4667 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4668 ERR("Failed to set trace chunk credentials");
4669 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4673 * The consumer daemon has no ownership of the chunk output
4676 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
,
4677 chunk_directory_handle
);
4678 chunk_directory_handle
= NULL
;
4679 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4680 ERR("Failed to set trace chunk's directory handle");
4681 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4686 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4687 consumer_data
.chunk_registry
, session_id
,
4689 lttng_trace_chunk_put(created_chunk
);
4690 created_chunk
= NULL
;
4691 if (!published_chunk
) {
4692 ERR("Failed to publish trace chunk");
4693 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4698 cds_lfht_for_each_entry_duplicate(consumer_data
.channels_by_session_id_ht
->ht
,
4699 consumer_data
.channels_by_session_id_ht
->hash_fct(
4700 &session_id
, lttng_ht_seed
),
4701 consumer_data
.channels_by_session_id_ht
->match_fct
,
4702 &session_id
, &iter
.iter
, channel
,
4703 channels_by_session_id_ht_node
.node
) {
4704 ret
= lttng_consumer_channel_set_trace_chunk(channel
,
4708 * Roll-back the creation of this chunk.
4710 * This is important since the session daemon will
4711 * assume that the creation of this chunk failed and
4712 * will never ask for it to be closed, resulting
4713 * in a leak and an inconsistent state for some
4716 enum lttcomm_return_code close_ret
;
4717 char path
[LTTNG_PATH_MAX
];
4719 DBG("Failed to set new trace chunk on existing channels, rolling back");
4720 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4721 session_id
, chunk_id
,
4722 chunk_creation_timestamp
, NULL
,
4724 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4725 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4726 session_id
, chunk_id
);
4729 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4735 struct consumer_relayd_sock_pair
*relayd
;
4737 relayd
= consumer_find_relayd(*relayd_id
);
4739 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4740 ret
= relayd_create_trace_chunk(
4741 &relayd
->control_sock
, published_chunk
);
4742 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4744 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4747 if (!relayd
|| ret
) {
4748 enum lttcomm_return_code close_ret
;
4749 char path
[LTTNG_PATH_MAX
];
4751 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4754 chunk_creation_timestamp
,
4756 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4757 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4762 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4769 /* Release the reference returned by the "publish" operation. */
4770 lttng_trace_chunk_put(published_chunk
);
4771 lttng_trace_chunk_put(created_chunk
);
4775 enum lttcomm_return_code
lttng_consumer_close_trace_chunk(
4776 const uint64_t *relayd_id
, uint64_t session_id
,
4777 uint64_t chunk_id
, time_t chunk_close_timestamp
,
4778 const enum lttng_trace_chunk_command_type
*close_command
,
4781 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4782 struct lttng_trace_chunk
*chunk
;
4783 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4784 const char *relayd_id_str
= "(none)";
4785 const char *close_command_name
= "none";
4786 struct lttng_ht_iter iter
;
4787 struct lttng_consumer_channel
*channel
;
4788 enum lttng_trace_chunk_status chunk_status
;
4793 /* Only used for logging purposes. */
4794 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4795 "%" PRIu64
, *relayd_id
);
4796 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4797 relayd_id_str
= relayd_id_buffer
;
4799 relayd_id_str
= "(formatting error)";
4802 if (close_command
) {
4803 close_command_name
= lttng_trace_chunk_command_type_get_name(
4807 DBG("Consumer close trace chunk command: relayd_id = %s"
4808 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4809 ", close command = %s",
4810 relayd_id_str
, session_id
, chunk_id
,
4811 close_command_name
);
4813 chunk
= lttng_trace_chunk_registry_find_chunk(
4814 consumer_data
.chunk_registry
, session_id
, chunk_id
);
4816 ERR("Failed to find chunk: session_id = %" PRIu64
4817 ", chunk_id = %" PRIu64
,
4818 session_id
, chunk_id
);
4819 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4823 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
,
4824 chunk_close_timestamp
);
4825 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4826 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4830 if (close_command
) {
4831 chunk_status
= lttng_trace_chunk_set_close_command(
4832 chunk
, *close_command
);
4833 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4834 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4840 * chunk is now invalid to access as we no longer hold a reference to
4841 * it; it is only kept around to compare it (by address) to the
4842 * current chunk found in the session's channels.
4845 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
,
4846 channel
, node
.node
) {
4850 * Only change the channel's chunk to NULL if it still
4851 * references the chunk being closed. The channel may
4852 * reference a newer channel in the case of a session
4853 * rotation. When a session rotation occurs, the "next"
4854 * chunk is created before the "current" chunk is closed.
4856 if (channel
->trace_chunk
!= chunk
) {
4859 ret
= lttng_consumer_channel_set_trace_chunk(channel
, NULL
);
4862 * Attempt to close the chunk on as many channels as
4865 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4871 struct consumer_relayd_sock_pair
*relayd
;
4873 relayd
= consumer_find_relayd(*relayd_id
);
4875 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4876 ret
= relayd_close_trace_chunk(
4877 &relayd
->control_sock
, chunk
,
4879 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4881 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
,
4885 if (!relayd
|| ret
) {
4886 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4894 * Release the reference returned by the "find" operation and
4895 * the session daemon's implicit reference to the chunk.
4897 lttng_trace_chunk_put(chunk
);
4898 lttng_trace_chunk_put(chunk
);
4903 enum lttcomm_return_code
lttng_consumer_trace_chunk_exists(
4904 const uint64_t *relayd_id
, uint64_t session_id
,
4908 enum lttcomm_return_code ret_code
;
4909 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4910 const char *relayd_id_str
= "(none)";
4911 const bool is_local_trace
= !relayd_id
;
4912 struct consumer_relayd_sock_pair
*relayd
= NULL
;
4913 bool chunk_exists_local
, chunk_exists_remote
;
4918 /* Only used for logging purposes. */
4919 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4920 "%" PRIu64
, *relayd_id
);
4921 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4922 relayd_id_str
= relayd_id_buffer
;
4924 relayd_id_str
= "(formatting error)";
4928 DBG("Consumer trace chunk exists command: relayd_id = %s"
4929 ", chunk_id = %" PRIu64
, relayd_id_str
,
4931 ret
= lttng_trace_chunk_registry_chunk_exists(
4932 consumer_data
.chunk_registry
, session_id
,
4933 chunk_id
, &chunk_exists_local
);
4935 /* Internal error. */
4936 ERR("Failed to query the existence of a trace chunk");
4937 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
4940 DBG("Trace chunk %s locally",
4941 chunk_exists_local
? "exists" : "does not exist");
4942 if (chunk_exists_local
) {
4943 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
4945 } else if (is_local_trace
) {
4946 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4951 relayd
= consumer_find_relayd(*relayd_id
);
4953 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
4954 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
4955 goto end_rcu_unlock
;
4957 DBG("Looking up existence of trace chunk on relay daemon");
4958 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4959 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
,
4960 &chunk_exists_remote
);
4961 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4963 ERR("Failed to look-up the existence of trace chunk on relay daemon");
4964 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
4965 goto end_rcu_unlock
;
4968 ret_code
= chunk_exists_remote
?
4969 LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
4970 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4971 DBG("Trace chunk %s on relay daemon",
4972 chunk_exists_remote
? "exists" : "does not exist");
4981 int consumer_clear_monitored_channel(struct lttng_consumer_channel
*channel
)
4983 struct lttng_ht
*ht
;
4984 struct lttng_consumer_stream
*stream
;
4985 struct lttng_ht_iter iter
;
4988 ht
= consumer_data
.stream_per_chan_id_ht
;
4991 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4992 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4993 ht
->match_fct
, &channel
->key
,
4994 &iter
.iter
, stream
, node_channel_id
.node
) {
4996 * Protect against teardown with mutex.
4998 pthread_mutex_lock(&stream
->lock
);
4999 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5002 ret
= consumer_clear_stream(stream
);
5007 pthread_mutex_unlock(&stream
->lock
);
5010 return LTTCOMM_CONSUMERD_SUCCESS
;
5013 pthread_mutex_unlock(&stream
->lock
);
5018 int lttng_consumer_clear_channel(struct lttng_consumer_channel
*channel
)
5022 DBG("Consumer clear channel %" PRIu64
, channel
->key
);
5024 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
5026 * Nothing to do for the metadata channel/stream.
5027 * Snapshot mechanism already take care of the metadata
5028 * handling/generation, and monitored channels only need to
5029 * have their data stream cleared..
5031 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5035 if (!channel
->monitor
) {
5036 ret
= consumer_clear_unmonitored_channel(channel
);
5038 ret
= consumer_clear_monitored_channel(channel
);
5044 enum lttcomm_return_code
lttng_consumer_open_channel_packets(
5045 struct lttng_consumer_channel
*channel
)
5047 struct lttng_consumer_stream
*stream
;
5048 enum lttcomm_return_code ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5050 if (channel
->metadata_stream
) {
5051 ERR("Open channel packets command attempted on a metadata channel");
5052 ret
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5057 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
5058 enum consumer_stream_open_packet_status status
;
5060 pthread_mutex_lock(&stream
->lock
);
5061 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5065 status
= consumer_stream_open_packet(stream
);
5067 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
5068 DBG("Opened a packet in \"open channel packets\" command: stream id = %" PRIu64
5069 ", channel name = %s, session id = %" PRIu64
,
5070 stream
->key
, stream
->chan
->name
,
5071 stream
->chan
->session_id
);
5072 stream
->opened_packet_in_current_trace_chunk
= true;
5074 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
5075 DBG("No space left to open a packet in \"open channel packets\" command: stream id = %" PRIu64
5076 ", channel name = %s, session id = %" PRIu64
,
5077 stream
->key
, stream
->chan
->name
,
5078 stream
->chan
->session_id
);
5080 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
5082 * Only unexpected internal errors can lead to this
5083 * failing. Report an unknown error.
5085 ERR("Failed to flush empty buffer in \"open channel packets\" command: stream id = %" PRIu64
5086 ", channel id = %" PRIu64
5087 ", channel name = %s"
5088 ", session id = %" PRIu64
,
5089 stream
->key
, channel
->key
,
5090 channel
->name
, channel
->session_id
);
5091 ret
= LTTCOMM_CONSUMERD_UNKNOWN_ERROR
;
5098 pthread_mutex_unlock(&stream
->lock
);
5107 pthread_mutex_unlock(&stream
->lock
);
5108 goto end_rcu_unlock
;