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 the_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 (the_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(the_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 (the_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(the_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(&the_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 (the_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(the_consumer_data
.channel_ht
, &iter
);
412 iter
.iter
.node
= &channel
->channels_by_session_id_ht_node
.node
;
413 ret
= lttng_ht_del(the_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(&the_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(the_consumer_data
.relayd_ht
->ht
, &iter
.iter
,
439 consumer_destroy_relayd(relayd
);
444 lttng_ht_destroy(the_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(&the_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(the_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(the_consumer_data
.stream_list_ht
,
604 &stream
->node_session_id
);
607 * When nb_init_stream_left reaches 0, we don't need to trigger any action
608 * in terms of destroying the associated channel, because the action that
609 * causes the count to become 0 also causes a stream to be added. The
610 * channel deletion will thus be triggered by the following removal of this
613 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
614 /* Increment refcount before decrementing nb_init_stream_left */
616 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
619 /* Update consumer data once the node is inserted. */
620 the_consumer_data
.stream_count
++;
621 the_consumer_data
.need_update
= 1;
624 pthread_mutex_unlock(&stream
->lock
);
625 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
626 pthread_mutex_unlock(&stream
->chan
->lock
);
627 pthread_mutex_unlock(&the_consumer_data
.lock
);
631 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
632 * be acquired before calling this.
634 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
637 struct lttng_ht_node_u64
*node
;
638 struct lttng_ht_iter iter
;
642 lttng_ht_lookup(the_consumer_data
.relayd_ht
, &relayd
->net_seq_idx
,
644 node
= lttng_ht_iter_get_node_u64(&iter
);
648 lttng_ht_add_unique_u64(the_consumer_data
.relayd_ht
, &relayd
->node
);
655 * Allocate and return a consumer relayd socket.
657 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
658 uint64_t net_seq_idx
)
660 struct consumer_relayd_sock_pair
*obj
= NULL
;
662 /* net sequence index of -1 is a failure */
663 if (net_seq_idx
== (uint64_t) -1ULL) {
667 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
669 PERROR("zmalloc relayd sock");
673 obj
->net_seq_idx
= net_seq_idx
;
675 obj
->destroy_flag
= 0;
676 obj
->control_sock
.sock
.fd
= -1;
677 obj
->data_sock
.sock
.fd
= -1;
678 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
679 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
686 * Find a relayd socket pair in the global consumer data.
688 * Return the object if found else NULL.
689 * RCU read-side lock must be held across this call and while using the
692 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
694 struct lttng_ht_iter iter
;
695 struct lttng_ht_node_u64
*node
;
696 struct consumer_relayd_sock_pair
*relayd
= NULL
;
698 /* Negative keys are lookup failures */
699 if (key
== (uint64_t) -1ULL) {
703 lttng_ht_lookup(the_consumer_data
.relayd_ht
, &key
, &iter
);
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 * Write a character on the metadata poll pipe to wake the metadata thread.
882 * Returns 0 on success, -1 on error.
884 int consumer_metadata_wakeup_pipe(const struct lttng_consumer_channel
*channel
)
888 DBG("Waking up metadata poll thread (writing to pipe): channel name = '%s'",
890 if (channel
->monitor
&& channel
->metadata_stream
) {
891 const char dummy
= 'c';
892 const ssize_t write_ret
= lttng_write(
893 channel
->metadata_stream
->ust_metadata_poll_pipe
[1],
897 if (errno
== EWOULDBLOCK
) {
899 * This is fine, the metadata poll thread
900 * is having a hard time keeping-up, but
901 * it will eventually wake-up and consume
902 * the available data.
906 PERROR("Failed to write to UST metadata pipe while attempting to wake-up the metadata poll thread");
918 * Trigger a dump of the metadata content. Following/during the succesful
919 * completion of this call, the metadata poll thread will start receiving
920 * metadata packets to consume.
922 * The caller must hold the channel and stream locks.
925 int consumer_metadata_stream_dump(struct lttng_consumer_stream
*stream
)
929 ASSERT_LOCKED(stream
->chan
->lock
);
930 ASSERT_LOCKED(stream
->lock
);
931 assert(stream
->metadata_flag
);
932 assert(stream
->chan
->trace_chunk
);
934 switch (the_consumer_data
.type
) {
935 case LTTNG_CONSUMER_KERNEL
:
937 * Reset the position of what has been read from the
938 * metadata cache to 0 so we can dump it again.
940 ret
= kernctl_metadata_cache_dump(stream
->wait_fd
);
942 case LTTNG_CONSUMER32_UST
:
943 case LTTNG_CONSUMER64_UST
:
945 * Reset the position pushed from the metadata cache so it
946 * will write from the beginning on the next push.
948 stream
->ust_metadata_pushed
= 0;
949 ret
= consumer_metadata_wakeup_pipe(stream
->chan
);
952 ERR("Unknown consumer_data type");
956 ERR("Failed to dump the metadata cache");
962 int lttng_consumer_channel_set_trace_chunk(
963 struct lttng_consumer_channel
*channel
,
964 struct lttng_trace_chunk
*new_trace_chunk
)
966 pthread_mutex_lock(&channel
->lock
);
967 if (channel
->is_deleted
) {
969 * The channel has been logically deleted and should no longer
970 * be used. It has released its reference to its current trace
971 * chunk and should not acquire a new one.
973 * Return success as there is nothing for the caller to do.
979 * The acquisition of the reference cannot fail (barring
980 * a severe internal error) since a reference to the published
981 * chunk is already held by the caller.
983 if (new_trace_chunk
) {
984 const bool acquired_reference
= lttng_trace_chunk_get(
987 assert(acquired_reference
);
990 lttng_trace_chunk_put(channel
->trace_chunk
);
991 channel
->trace_chunk
= new_trace_chunk
;
993 pthread_mutex_unlock(&channel
->lock
);
998 * Allocate and return a new lttng_consumer_channel object using the given key
999 * to initialize the hash table node.
1001 * On error, return NULL.
1003 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
1004 uint64_t session_id
,
1005 const uint64_t *chunk_id
,
1006 const char *pathname
,
1009 enum lttng_event_output output
,
1010 uint64_t tracefile_size
,
1011 uint64_t tracefile_count
,
1012 uint64_t session_id_per_pid
,
1013 unsigned int monitor
,
1014 unsigned int live_timer_interval
,
1015 bool is_in_live_session
,
1016 const char *root_shm_path
,
1017 const char *shm_path
)
1019 struct lttng_consumer_channel
*channel
= NULL
;
1020 struct lttng_trace_chunk
*trace_chunk
= NULL
;
1023 trace_chunk
= lttng_trace_chunk_registry_find_chunk(
1024 the_consumer_data
.chunk_registry
, session_id
,
1027 ERR("Failed to find trace chunk reference during creation of channel");
1032 channel
= zmalloc(sizeof(*channel
));
1033 if (channel
== NULL
) {
1034 PERROR("malloc struct lttng_consumer_channel");
1039 channel
->refcount
= 0;
1040 channel
->session_id
= session_id
;
1041 channel
->session_id_per_pid
= session_id_per_pid
;
1042 channel
->relayd_id
= relayd_id
;
1043 channel
->tracefile_size
= tracefile_size
;
1044 channel
->tracefile_count
= tracefile_count
;
1045 channel
->monitor
= monitor
;
1046 channel
->live_timer_interval
= live_timer_interval
;
1047 channel
->is_live
= is_in_live_session
;
1048 pthread_mutex_init(&channel
->lock
, NULL
);
1049 pthread_mutex_init(&channel
->timer_lock
, NULL
);
1052 case LTTNG_EVENT_SPLICE
:
1053 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
1055 case LTTNG_EVENT_MMAP
:
1056 channel
->output
= CONSUMER_CHANNEL_MMAP
;
1066 * In monitor mode, the streams associated with the channel will be put in
1067 * a special list ONLY owned by this channel. So, the refcount is set to 1
1068 * here meaning that the channel itself has streams that are referenced.
1070 * On a channel deletion, once the channel is no longer visible, the
1071 * refcount is decremented and checked for a zero value to delete it. With
1072 * streams in no monitor mode, it will now be safe to destroy the channel.
1074 if (!channel
->monitor
) {
1075 channel
->refcount
= 1;
1078 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1079 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1081 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1082 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1084 if (root_shm_path
) {
1085 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1086 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1089 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1090 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1093 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1094 lttng_ht_node_init_u64(&channel
->channels_by_session_id_ht_node
,
1095 channel
->session_id
);
1097 channel
->wait_fd
= -1;
1098 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1101 int ret
= lttng_consumer_channel_set_trace_chunk(channel
,
1108 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1111 lttng_trace_chunk_put(trace_chunk
);
1114 consumer_del_channel(channel
);
1120 * Add a channel to the global list protected by a mutex.
1122 * Always return 0 indicating success.
1124 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1125 struct lttng_consumer_local_data
*ctx
)
1127 pthread_mutex_lock(&the_consumer_data
.lock
);
1128 pthread_mutex_lock(&channel
->lock
);
1129 pthread_mutex_lock(&channel
->timer_lock
);
1132 * This gives us a guarantee that the channel we are about to add to the
1133 * channel hash table will be unique. See this function comment on the why
1134 * we need to steel the channel key at this stage.
1136 steal_channel_key(channel
->key
);
1139 lttng_ht_add_unique_u64(the_consumer_data
.channel_ht
, &channel
->node
);
1140 lttng_ht_add_u64(the_consumer_data
.channels_by_session_id_ht
,
1141 &channel
->channels_by_session_id_ht_node
);
1143 channel
->is_published
= true;
1145 pthread_mutex_unlock(&channel
->timer_lock
);
1146 pthread_mutex_unlock(&channel
->lock
);
1147 pthread_mutex_unlock(&the_consumer_data
.lock
);
1149 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1150 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1157 * Allocate the pollfd structure and the local view of the out fds to avoid
1158 * doing a lookup in the linked list and concurrency issues when writing is
1159 * needed. Called with consumer_data.lock held.
1161 * Returns the number of fds in the structures.
1163 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1164 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1165 struct lttng_ht
*ht
, int *nb_inactive_fd
)
1168 struct lttng_ht_iter iter
;
1169 struct lttng_consumer_stream
*stream
;
1174 assert(local_stream
);
1176 DBG("Updating poll fd array");
1177 *nb_inactive_fd
= 0;
1179 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1181 * Only active streams with an active end point can be added to the
1182 * poll set and local stream storage of the thread.
1184 * There is a potential race here for endpoint_status to be updated
1185 * just after the check. However, this is OK since the stream(s) will
1186 * be deleted once the thread is notified that the end point state has
1187 * changed where this function will be called back again.
1189 * We track the number of inactive FDs because they still need to be
1190 * closed by the polling thread after a wakeup on the data_pipe or
1193 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1194 (*nb_inactive_fd
)++;
1198 * This clobbers way too much the debug output. Uncomment that if you
1199 * need it for debugging purposes.
1201 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1202 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1203 local_stream
[i
] = stream
;
1209 * Insert the consumer_data_pipe at the end of the array and don't
1210 * increment i so nb_fd is the number of real FD.
1212 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1213 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1215 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1216 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1221 * Poll on the should_quit pipe and the command socket return -1 on
1222 * error, 1 if should exit, 0 if data is available on the command socket
1224 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1229 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1230 if (num_rdy
== -1) {
1232 * Restart interrupted system call.
1234 if (errno
== EINTR
) {
1237 PERROR("Poll error");
1240 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1241 DBG("consumer_should_quit wake up");
1248 * Set the error socket.
1250 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1253 ctx
->consumer_error_socket
= sock
;
1257 * Set the command socket path.
1259 void lttng_consumer_set_command_sock_path(
1260 struct lttng_consumer_local_data
*ctx
, char *sock
)
1262 ctx
->consumer_command_sock_path
= sock
;
1266 * Send return code to the session daemon.
1267 * If the socket is not defined, we return 0, it is not a fatal error
1269 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1271 if (ctx
->consumer_error_socket
> 0) {
1272 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1273 sizeof(enum lttcomm_sessiond_command
));
1280 * Close all the tracefiles and stream fds and MUST be called when all
1281 * instances are destroyed i.e. when all threads were joined and are ended.
1283 void lttng_consumer_cleanup(void)
1285 struct lttng_ht_iter iter
;
1286 struct lttng_consumer_channel
*channel
;
1287 unsigned int trace_chunks_left
;
1291 cds_lfht_for_each_entry(the_consumer_data
.channel_ht
->ht
, &iter
.iter
,
1292 channel
, node
.node
) {
1293 consumer_del_channel(channel
);
1298 lttng_ht_destroy(the_consumer_data
.channel_ht
);
1299 lttng_ht_destroy(the_consumer_data
.channels_by_session_id_ht
);
1301 cleanup_relayd_ht();
1303 lttng_ht_destroy(the_consumer_data
.stream_per_chan_id_ht
);
1306 * This HT contains streams that are freed by either the metadata thread or
1307 * the data thread so we do *nothing* on the hash table and simply destroy
1310 lttng_ht_destroy(the_consumer_data
.stream_list_ht
);
1313 * Trace chunks in the registry may still exist if the session
1314 * daemon has encountered an internal error and could not
1315 * tear down its sessions and/or trace chunks properly.
1317 * Release the session daemon's implicit reference to any remaining
1318 * trace chunk and print an error if any trace chunk was found. Note
1319 * that there are _no_ legitimate cases for trace chunks to be left,
1320 * it is a leak. However, it can happen following a crash of the
1321 * session daemon and not emptying the registry would cause an assertion
1324 trace_chunks_left
= lttng_trace_chunk_registry_put_each_chunk(
1325 the_consumer_data
.chunk_registry
);
1326 if (trace_chunks_left
) {
1327 ERR("%u trace chunks are leaked by lttng-consumerd. "
1328 "This can be caused by an internal error of the session daemon.",
1331 /* Run all callbacks freeing each chunk. */
1333 lttng_trace_chunk_registry_destroy(the_consumer_data
.chunk_registry
);
1337 * Called from signal handler.
1339 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1343 CMM_STORE_SHARED(consumer_quit
, 1);
1344 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1346 PERROR("write consumer quit");
1349 DBG("Consumer flag that it should quit");
1354 * Flush pending writes to trace output disk file.
1357 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1361 int outfd
= stream
->out_fd
;
1364 * This does a blocking write-and-wait on any page that belongs to the
1365 * subbuffer prior to the one we just wrote.
1366 * Don't care about error values, as these are just hints and ways to
1367 * limit the amount of page cache used.
1369 if (orig_offset
< stream
->max_sb_size
) {
1372 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1373 stream
->max_sb_size
,
1374 SYNC_FILE_RANGE_WAIT_BEFORE
1375 | SYNC_FILE_RANGE_WRITE
1376 | SYNC_FILE_RANGE_WAIT_AFTER
);
1378 * Give hints to the kernel about how we access the file:
1379 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1382 * We need to call fadvise again after the file grows because the
1383 * kernel does not seem to apply fadvise to non-existing parts of the
1386 * Call fadvise _after_ having waited for the page writeback to
1387 * complete because the dirty page writeback semantic is not well
1388 * defined. So it can be expected to lead to lower throughput in
1391 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1392 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1393 if (ret
&& ret
!= -ENOSYS
) {
1395 PERROR("posix_fadvise on fd %i", outfd
);
1400 * Initialise the necessary environnement :
1401 * - create a new context
1402 * - create the poll_pipe
1403 * - create the should_quit pipe (for signal handler)
1404 * - create the thread pipe (for splice)
1406 * Takes a function pointer as argument, this function is called when data is
1407 * available on a buffer. This function is responsible to do the
1408 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1409 * buffer configuration and then kernctl_put_next_subbuf at the end.
1411 * Returns a pointer to the new context or NULL on error.
1413 struct lttng_consumer_local_data
*lttng_consumer_create(
1414 enum lttng_consumer_type type
,
1415 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1416 struct lttng_consumer_local_data
*ctx
, bool locked_by_caller
),
1417 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1418 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1419 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1422 struct lttng_consumer_local_data
*ctx
;
1424 assert(the_consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1425 the_consumer_data
.type
== type
);
1426 the_consumer_data
.type
= type
;
1428 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1430 PERROR("allocating context");
1434 ctx
->consumer_error_socket
= -1;
1435 ctx
->consumer_metadata_socket
= -1;
1436 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1437 /* assign the callbacks */
1438 ctx
->on_buffer_ready
= buffer_ready
;
1439 ctx
->on_recv_channel
= recv_channel
;
1440 ctx
->on_recv_stream
= recv_stream
;
1441 ctx
->on_update_stream
= update_stream
;
1443 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1444 if (!ctx
->consumer_data_pipe
) {
1445 goto error_poll_pipe
;
1448 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1449 if (!ctx
->consumer_wakeup_pipe
) {
1450 goto error_wakeup_pipe
;
1453 ret
= pipe(ctx
->consumer_should_quit
);
1455 PERROR("Error creating recv pipe");
1456 goto error_quit_pipe
;
1459 ret
= pipe(ctx
->consumer_channel_pipe
);
1461 PERROR("Error creating channel pipe");
1462 goto error_channel_pipe
;
1465 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1466 if (!ctx
->consumer_metadata_pipe
) {
1467 goto error_metadata_pipe
;
1470 ctx
->channel_monitor_pipe
= -1;
1474 error_metadata_pipe
:
1475 utils_close_pipe(ctx
->consumer_channel_pipe
);
1477 utils_close_pipe(ctx
->consumer_should_quit
);
1479 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1481 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1489 * Iterate over all streams of the hashtable and free them properly.
1491 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1493 struct lttng_ht_iter iter
;
1494 struct lttng_consumer_stream
*stream
;
1501 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1503 * Ignore return value since we are currently cleaning up so any error
1506 (void) consumer_del_stream(stream
, ht
);
1510 lttng_ht_destroy(ht
);
1514 * Iterate over all streams of the metadata hashtable and free them
1517 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1519 struct lttng_ht_iter iter
;
1520 struct lttng_consumer_stream
*stream
;
1527 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1529 * Ignore return value since we are currently cleaning up so any error
1532 (void) consumer_del_metadata_stream(stream
, ht
);
1536 lttng_ht_destroy(ht
);
1540 * Close all fds associated with the instance and free the context.
1542 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1546 DBG("Consumer destroying it. Closing everything.");
1552 destroy_data_stream_ht(data_ht
);
1553 destroy_metadata_stream_ht(metadata_ht
);
1555 ret
= close(ctx
->consumer_error_socket
);
1559 ret
= close(ctx
->consumer_metadata_socket
);
1563 utils_close_pipe(ctx
->consumer_channel_pipe
);
1564 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1565 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1566 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1567 utils_close_pipe(ctx
->consumer_should_quit
);
1569 unlink(ctx
->consumer_command_sock_path
);
1574 * Write the metadata stream id on the specified file descriptor.
1576 static int write_relayd_metadata_id(int fd
,
1577 struct lttng_consumer_stream
*stream
,
1578 unsigned long padding
)
1581 struct lttcomm_relayd_metadata_payload hdr
;
1583 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1584 hdr
.padding_size
= htobe32(padding
);
1585 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1586 if (ret
< sizeof(hdr
)) {
1588 * This error means that the fd's end is closed so ignore the PERROR
1589 * not to clubber the error output since this can happen in a normal
1592 if (errno
!= EPIPE
) {
1593 PERROR("write metadata stream id");
1595 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1597 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1598 * handle writting the missing part so report that as an error and
1599 * don't lie to the caller.
1604 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1605 stream
->relayd_stream_id
, padding
);
1612 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1613 * core function for writing trace buffers to either the local filesystem or
1616 * It must be called with the stream and the channel lock held.
1618 * Careful review MUST be put if any changes occur!
1620 * Returns the number of bytes written
1622 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1623 struct lttng_consumer_stream
*stream
,
1624 const struct lttng_buffer_view
*buffer
,
1625 unsigned long padding
)
1628 off_t orig_offset
= stream
->out_fd_offset
;
1629 /* Default is on the disk */
1630 int outfd
= stream
->out_fd
;
1631 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1632 unsigned int relayd_hang_up
= 0;
1633 const size_t subbuf_content_size
= buffer
->size
- padding
;
1636 /* RCU lock for the relayd pointer */
1638 assert(stream
->net_seq_idx
!= (uint64_t) -1ULL ||
1639 stream
->trace_chunk
);
1641 /* Flag that the current stream if set for network streaming. */
1642 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1643 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1644 if (relayd
== NULL
) {
1650 /* Handle stream on the relayd if the output is on the network */
1652 unsigned long netlen
= subbuf_content_size
;
1655 * Lock the control socket for the complete duration of the function
1656 * since from this point on we will use the socket.
1658 if (stream
->metadata_flag
) {
1659 /* Metadata requires the control socket. */
1660 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1661 if (stream
->reset_metadata_flag
) {
1662 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1663 stream
->relayd_stream_id
,
1664 stream
->metadata_version
);
1669 stream
->reset_metadata_flag
= 0;
1671 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1674 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1679 /* Use the returned socket. */
1682 /* Write metadata stream id before payload */
1683 if (stream
->metadata_flag
) {
1684 ret
= write_relayd_metadata_id(outfd
, stream
, padding
);
1691 write_len
= subbuf_content_size
;
1693 /* No streaming; we have to write the full padding. */
1694 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1695 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1697 ERR("Reset metadata file");
1700 stream
->reset_metadata_flag
= 0;
1704 * Check if we need to change the tracefile before writing the packet.
1706 if (stream
->chan
->tracefile_size
> 0 &&
1707 (stream
->tracefile_size_current
+ buffer
->size
) >
1708 stream
->chan
->tracefile_size
) {
1709 ret
= consumer_stream_rotate_output_files(stream
);
1713 outfd
= stream
->out_fd
;
1716 stream
->tracefile_size_current
+= buffer
->size
;
1717 write_len
= buffer
->size
;
1721 * This call guarantee that len or less is returned. It's impossible to
1722 * receive a ret value that is bigger than len.
1724 ret
= lttng_write(outfd
, buffer
->data
, write_len
);
1725 DBG("Consumer mmap write() ret %zd (len %zu)", ret
, write_len
);
1726 if (ret
< 0 || ((size_t) ret
!= write_len
)) {
1728 * Report error to caller if nothing was written else at least send the
1736 /* Socket operation failed. We consider the relayd dead */
1737 if (errno
== EPIPE
) {
1739 * This is possible if the fd is closed on the other side
1740 * (outfd) or any write problem. It can be verbose a bit for a
1741 * normal execution if for instance the relayd is stopped
1742 * abruptly. This can happen so set this to a DBG statement.
1744 DBG("Consumer mmap write detected relayd hang up");
1746 /* Unhandled error, print it and stop function right now. */
1747 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret
,
1752 stream
->output_written
+= ret
;
1754 /* This call is useless on a socket so better save a syscall. */
1756 /* This won't block, but will start writeout asynchronously */
1757 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, write_len
,
1758 SYNC_FILE_RANGE_WRITE
);
1759 stream
->out_fd_offset
+= write_len
;
1760 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1765 * This is a special case that the relayd has closed its socket. Let's
1766 * cleanup the relayd object and all associated streams.
1768 if (relayd
&& relayd_hang_up
) {
1769 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1770 lttng_consumer_cleanup_relayd(relayd
);
1774 /* Unlock only if ctrl socket used */
1775 if (relayd
&& stream
->metadata_flag
) {
1776 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1784 * Splice the data from the ring buffer to the tracefile.
1786 * It must be called with the stream lock held.
1788 * Returns the number of bytes spliced.
1790 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1791 struct lttng_consumer_local_data
*ctx
,
1792 struct lttng_consumer_stream
*stream
, unsigned long len
,
1793 unsigned long padding
)
1795 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1797 off_t orig_offset
= stream
->out_fd_offset
;
1798 int fd
= stream
->wait_fd
;
1799 /* Default is on the disk */
1800 int outfd
= stream
->out_fd
;
1801 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1803 unsigned int relayd_hang_up
= 0;
1805 switch (the_consumer_data
.type
) {
1806 case LTTNG_CONSUMER_KERNEL
:
1808 case LTTNG_CONSUMER32_UST
:
1809 case LTTNG_CONSUMER64_UST
:
1810 /* Not supported for user space tracing */
1813 ERR("Unknown consumer_data type");
1817 /* RCU lock for the relayd pointer */
1820 /* Flag that the current stream if set for network streaming. */
1821 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1822 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1823 if (relayd
== NULL
) {
1828 splice_pipe
= stream
->splice_pipe
;
1830 /* Write metadata stream id before payload */
1832 unsigned long total_len
= len
;
1834 if (stream
->metadata_flag
) {
1836 * Lock the control socket for the complete duration of the function
1837 * since from this point on we will use the socket.
1839 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1841 if (stream
->reset_metadata_flag
) {
1842 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1843 stream
->relayd_stream_id
,
1844 stream
->metadata_version
);
1849 stream
->reset_metadata_flag
= 0;
1851 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
,
1859 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1862 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1868 /* Use the returned socket. */
1871 /* No streaming, we have to set the len with the full padding */
1874 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1875 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1877 ERR("Reset metadata file");
1880 stream
->reset_metadata_flag
= 0;
1883 * Check if we need to change the tracefile before writing the packet.
1885 if (stream
->chan
->tracefile_size
> 0 &&
1886 (stream
->tracefile_size_current
+ len
) >
1887 stream
->chan
->tracefile_size
) {
1888 ret
= consumer_stream_rotate_output_files(stream
);
1893 outfd
= stream
->out_fd
;
1896 stream
->tracefile_size_current
+= len
;
1900 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1901 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1902 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1903 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1904 DBG("splice chan to pipe, ret %zd", ret_splice
);
1905 if (ret_splice
< 0) {
1908 PERROR("Error in relay splice");
1912 /* Handle stream on the relayd if the output is on the network */
1913 if (relayd
&& stream
->metadata_flag
) {
1914 size_t metadata_payload_size
=
1915 sizeof(struct lttcomm_relayd_metadata_payload
);
1917 /* Update counter to fit the spliced data */
1918 ret_splice
+= metadata_payload_size
;
1919 len
+= metadata_payload_size
;
1921 * We do this so the return value can match the len passed as
1922 * argument to this function.
1924 written
-= metadata_payload_size
;
1927 /* Splice data out */
1928 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1929 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1930 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1932 if (ret_splice
< 0) {
1937 } else if (ret_splice
> len
) {
1939 * We don't expect this code path to be executed but you never know
1940 * so this is an extra protection agains a buggy splice().
1943 written
+= ret_splice
;
1944 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1948 /* All good, update current len and continue. */
1952 /* This call is useless on a socket so better save a syscall. */
1954 /* This won't block, but will start writeout asynchronously */
1955 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1956 SYNC_FILE_RANGE_WRITE
);
1957 stream
->out_fd_offset
+= ret_splice
;
1959 stream
->output_written
+= ret_splice
;
1960 written
+= ret_splice
;
1963 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1969 * This is a special case that the relayd has closed its socket. Let's
1970 * cleanup the relayd object and all associated streams.
1972 if (relayd
&& relayd_hang_up
) {
1973 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1974 lttng_consumer_cleanup_relayd(relayd
);
1975 /* Skip splice error so the consumer does not fail */
1980 /* send the appropriate error description to sessiond */
1983 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1986 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1989 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1994 if (relayd
&& stream
->metadata_flag
) {
1995 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2003 * Sample the snapshot positions for a specific fd
2005 * Returns 0 on success, < 0 on error
2007 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
2009 switch (the_consumer_data
.type
) {
2010 case LTTNG_CONSUMER_KERNEL
:
2011 return lttng_kconsumer_sample_snapshot_positions(stream
);
2012 case LTTNG_CONSUMER32_UST
:
2013 case LTTNG_CONSUMER64_UST
:
2014 return lttng_ustconsumer_sample_snapshot_positions(stream
);
2016 ERR("Unknown consumer_data type");
2022 * Take a snapshot for a specific fd
2024 * Returns 0 on success, < 0 on error
2026 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2028 switch (the_consumer_data
.type
) {
2029 case LTTNG_CONSUMER_KERNEL
:
2030 return lttng_kconsumer_take_snapshot(stream
);
2031 case LTTNG_CONSUMER32_UST
:
2032 case LTTNG_CONSUMER64_UST
:
2033 return lttng_ustconsumer_take_snapshot(stream
);
2035 ERR("Unknown consumer_data type");
2042 * Get the produced position
2044 * Returns 0 on success, < 0 on error
2046 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
2049 switch (the_consumer_data
.type
) {
2050 case LTTNG_CONSUMER_KERNEL
:
2051 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
2052 case LTTNG_CONSUMER32_UST
:
2053 case LTTNG_CONSUMER64_UST
:
2054 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2056 ERR("Unknown consumer_data type");
2063 * Get the consumed position (free-running counter position in bytes).
2065 * Returns 0 on success, < 0 on error
2067 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
2070 switch (the_consumer_data
.type
) {
2071 case LTTNG_CONSUMER_KERNEL
:
2072 return lttng_kconsumer_get_consumed_snapshot(stream
, pos
);
2073 case LTTNG_CONSUMER32_UST
:
2074 case LTTNG_CONSUMER64_UST
:
2075 return lttng_ustconsumer_get_consumed_snapshot(stream
, pos
);
2077 ERR("Unknown consumer_data type");
2083 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2084 int sock
, struct pollfd
*consumer_sockpoll
)
2086 switch (the_consumer_data
.type
) {
2087 case LTTNG_CONSUMER_KERNEL
:
2088 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2089 case LTTNG_CONSUMER32_UST
:
2090 case LTTNG_CONSUMER64_UST
:
2091 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2093 ERR("Unknown consumer_data type");
2100 void lttng_consumer_close_all_metadata(void)
2102 switch (the_consumer_data
.type
) {
2103 case LTTNG_CONSUMER_KERNEL
:
2105 * The Kernel consumer has a different metadata scheme so we don't
2106 * close anything because the stream will be closed by the session
2110 case LTTNG_CONSUMER32_UST
:
2111 case LTTNG_CONSUMER64_UST
:
2113 * Close all metadata streams. The metadata hash table is passed and
2114 * this call iterates over it by closing all wakeup fd. This is safe
2115 * because at this point we are sure that the metadata producer is
2116 * either dead or blocked.
2118 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2121 ERR("Unknown consumer_data type");
2127 * Clean up a metadata stream and free its memory.
2129 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2130 struct lttng_ht
*ht
)
2132 struct lttng_consumer_channel
*channel
= NULL
;
2133 bool free_channel
= false;
2137 * This call should NEVER receive regular stream. It must always be
2138 * metadata stream and this is crucial for data structure synchronization.
2140 assert(stream
->metadata_flag
);
2142 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2144 pthread_mutex_lock(&the_consumer_data
.lock
);
2146 * Note that this assumes that a stream's channel is never changed and
2147 * that the stream's lock doesn't need to be taken to sample its
2150 channel
= stream
->chan
;
2151 pthread_mutex_lock(&channel
->lock
);
2152 pthread_mutex_lock(&stream
->lock
);
2153 if (channel
->metadata_cache
) {
2154 /* Only applicable to userspace consumers. */
2155 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
2158 /* Remove any reference to that stream. */
2159 consumer_stream_delete(stream
, ht
);
2161 /* Close down everything including the relayd if one. */
2162 consumer_stream_close(stream
);
2163 /* Destroy tracer buffers of the stream. */
2164 consumer_stream_destroy_buffers(stream
);
2166 /* Atomically decrement channel refcount since other threads can use it. */
2167 if (!uatomic_sub_return(&channel
->refcount
, 1)
2168 && !uatomic_read(&channel
->nb_init_stream_left
)) {
2169 /* Go for channel deletion! */
2170 free_channel
= true;
2172 stream
->chan
= NULL
;
2175 * Nullify the stream reference so it is not used after deletion. The
2176 * channel lock MUST be acquired before being able to check for a NULL
2179 channel
->metadata_stream
= NULL
;
2181 if (channel
->metadata_cache
) {
2182 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
2184 pthread_mutex_unlock(&stream
->lock
);
2185 pthread_mutex_unlock(&channel
->lock
);
2186 pthread_mutex_unlock(&the_consumer_data
.lock
);
2189 consumer_del_channel(channel
);
2192 lttng_trace_chunk_put(stream
->trace_chunk
);
2193 stream
->trace_chunk
= NULL
;
2194 consumer_stream_free(stream
);
2198 * Action done with the metadata stream when adding it to the consumer internal
2199 * data structures to handle it.
2201 void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2203 struct lttng_ht
*ht
= metadata_ht
;
2204 struct lttng_ht_iter iter
;
2205 struct lttng_ht_node_u64
*node
;
2210 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2212 pthread_mutex_lock(&the_consumer_data
.lock
);
2213 pthread_mutex_lock(&stream
->chan
->lock
);
2214 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2215 pthread_mutex_lock(&stream
->lock
);
2218 * From here, refcounts are updated so be _careful_ when returning an error
2225 * Lookup the stream just to make sure it does not exist in our internal
2226 * state. This should NEVER happen.
2228 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2229 node
= lttng_ht_iter_get_node_u64(&iter
);
2233 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2234 * in terms of destroying the associated channel, because the action that
2235 * causes the count to become 0 also causes a stream to be added. The
2236 * channel deletion will thus be triggered by the following removal of this
2239 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2240 /* Increment refcount before decrementing nb_init_stream_left */
2242 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2245 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2247 lttng_ht_add_u64(the_consumer_data
.stream_per_chan_id_ht
,
2248 &stream
->node_channel_id
);
2251 * Add stream to the stream_list_ht of the consumer data. No need to steal
2252 * the key since the HT does not use it and we allow to add redundant keys
2255 lttng_ht_add_u64(the_consumer_data
.stream_list_ht
,
2256 &stream
->node_session_id
);
2260 pthread_mutex_unlock(&stream
->lock
);
2261 pthread_mutex_unlock(&stream
->chan
->lock
);
2262 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2263 pthread_mutex_unlock(&the_consumer_data
.lock
);
2267 * Delete data stream that are flagged for deletion (endpoint_status).
2269 static void validate_endpoint_status_data_stream(void)
2271 struct lttng_ht_iter iter
;
2272 struct lttng_consumer_stream
*stream
;
2274 DBG("Consumer delete flagged data stream");
2277 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2278 /* Validate delete flag of the stream */
2279 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2282 /* Delete it right now */
2283 consumer_del_stream(stream
, data_ht
);
2289 * Delete metadata stream that are flagged for deletion (endpoint_status).
2291 static void validate_endpoint_status_metadata_stream(
2292 struct lttng_poll_event
*pollset
)
2294 struct lttng_ht_iter iter
;
2295 struct lttng_consumer_stream
*stream
;
2297 DBG("Consumer delete flagged metadata stream");
2302 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2303 /* Validate delete flag of the stream */
2304 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2308 * Remove from pollset so the metadata thread can continue without
2309 * blocking on a deleted stream.
2311 lttng_poll_del(pollset
, stream
->wait_fd
);
2313 /* Delete it right now */
2314 consumer_del_metadata_stream(stream
, metadata_ht
);
2320 * Thread polls on metadata file descriptor and write them on disk or on the
2323 void *consumer_thread_metadata_poll(void *data
)
2325 int ret
, i
, pollfd
, err
= -1;
2326 uint32_t revents
, nb_fd
;
2327 struct lttng_consumer_stream
*stream
= NULL
;
2328 struct lttng_ht_iter iter
;
2329 struct lttng_ht_node_u64
*node
;
2330 struct lttng_poll_event events
;
2331 struct lttng_consumer_local_data
*ctx
= data
;
2334 rcu_register_thread();
2336 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2338 if (testpoint(consumerd_thread_metadata
)) {
2339 goto error_testpoint
;
2342 health_code_update();
2344 DBG("Thread metadata poll started");
2346 /* Size is set to 1 for the consumer_metadata pipe */
2347 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2349 ERR("Poll set creation failed");
2353 ret
= lttng_poll_add(&events
,
2354 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2360 DBG("Metadata main loop started");
2364 health_code_update();
2365 health_poll_entry();
2366 DBG("Metadata poll wait");
2367 ret
= lttng_poll_wait(&events
, -1);
2368 DBG("Metadata poll return from wait with %d fd(s)",
2369 LTTNG_POLL_GETNB(&events
));
2371 DBG("Metadata event caught in thread");
2373 if (errno
== EINTR
) {
2374 ERR("Poll EINTR caught");
2377 if (LTTNG_POLL_GETNB(&events
) == 0) {
2378 err
= 0; /* All is OK */
2385 /* From here, the event is a metadata wait fd */
2386 for (i
= 0; i
< nb_fd
; i
++) {
2387 health_code_update();
2389 revents
= LTTNG_POLL_GETEV(&events
, i
);
2390 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2392 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2393 if (revents
& LPOLLIN
) {
2396 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2397 &stream
, sizeof(stream
));
2398 if (pipe_len
< sizeof(stream
)) {
2400 PERROR("read metadata stream");
2403 * Remove the pipe from the poll set and continue the loop
2404 * since their might be data to consume.
2406 lttng_poll_del(&events
,
2407 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2408 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2412 /* A NULL stream means that the state has changed. */
2413 if (stream
== NULL
) {
2414 /* Check for deleted streams. */
2415 validate_endpoint_status_metadata_stream(&events
);
2419 DBG("Adding metadata stream %d to poll set",
2422 /* Add metadata stream to the global poll events list */
2423 lttng_poll_add(&events
, stream
->wait_fd
,
2424 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2425 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2426 DBG("Metadata thread pipe hung up");
2428 * Remove the pipe from the poll set and continue the loop
2429 * since their might be data to consume.
2431 lttng_poll_del(&events
,
2432 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2433 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2436 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2440 /* Handle other stream */
2446 uint64_t tmp_id
= (uint64_t) pollfd
;
2448 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2450 node
= lttng_ht_iter_get_node_u64(&iter
);
2453 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2456 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2457 /* Get the data out of the metadata file descriptor */
2458 DBG("Metadata available on fd %d", pollfd
);
2459 assert(stream
->wait_fd
== pollfd
);
2462 health_code_update();
2464 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2466 * We don't check the return value here since if we get
2467 * a negative len, it means an error occurred thus we
2468 * simply remove it from the poll set and free the
2473 /* It's ok to have an unavailable sub-buffer */
2474 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2475 /* Clean up stream from consumer and free it. */
2476 lttng_poll_del(&events
, stream
->wait_fd
);
2477 consumer_del_metadata_stream(stream
, metadata_ht
);
2479 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2480 DBG("Metadata fd %d is hup|err.", pollfd
);
2481 if (!stream
->hangup_flush_done
&&
2482 (the_consumer_data
.type
== LTTNG_CONSUMER32_UST
||
2483 the_consumer_data
.type
==
2484 LTTNG_CONSUMER64_UST
)) {
2485 DBG("Attempting to flush and consume the UST buffers");
2486 lttng_ustconsumer_on_stream_hangup(stream
);
2488 /* We just flushed the stream now read it. */
2490 health_code_update();
2492 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2494 * We don't check the return value here since if we get
2495 * a negative len, it means an error occurred thus we
2496 * simply remove it from the poll set and free the
2502 lttng_poll_del(&events
, stream
->wait_fd
);
2504 * This call update the channel states, closes file descriptors
2505 * and securely free the stream.
2507 consumer_del_metadata_stream(stream
, metadata_ht
);
2509 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2513 /* Release RCU lock for the stream looked up */
2521 DBG("Metadata poll thread exiting");
2523 lttng_poll_clean(&events
);
2528 ERR("Health error occurred in %s", __func__
);
2530 health_unregister(health_consumerd
);
2531 rcu_unregister_thread();
2536 * This thread polls the fds in the set to consume the data and write
2537 * it to tracefile if necessary.
2539 void *consumer_thread_data_poll(void *data
)
2541 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2542 struct pollfd
*pollfd
= NULL
;
2543 /* local view of the streams */
2544 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2545 /* local view of consumer_data.fds_count */
2547 /* 2 for the consumer_data_pipe and wake up pipe */
2548 const int nb_pipes_fd
= 2;
2549 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2550 int nb_inactive_fd
= 0;
2551 struct lttng_consumer_local_data
*ctx
= data
;
2554 rcu_register_thread();
2556 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2558 if (testpoint(consumerd_thread_data
)) {
2559 goto error_testpoint
;
2562 health_code_update();
2564 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2565 if (local_stream
== NULL
) {
2566 PERROR("local_stream malloc");
2571 health_code_update();
2577 * the fds set has been updated, we need to update our
2578 * local array as well
2580 pthread_mutex_lock(&the_consumer_data
.lock
);
2581 if (the_consumer_data
.need_update
) {
2586 local_stream
= NULL
;
2588 /* Allocate for all fds */
2589 pollfd
= zmalloc((the_consumer_data
.stream_count
+
2591 sizeof(struct pollfd
));
2592 if (pollfd
== NULL
) {
2593 PERROR("pollfd malloc");
2594 pthread_mutex_unlock(&the_consumer_data
.lock
);
2598 local_stream
= zmalloc((the_consumer_data
.stream_count
+
2600 sizeof(struct lttng_consumer_stream
*));
2601 if (local_stream
== NULL
) {
2602 PERROR("local_stream malloc");
2603 pthread_mutex_unlock(&the_consumer_data
.lock
);
2606 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2607 data_ht
, &nb_inactive_fd
);
2609 ERR("Error in allocating pollfd or local_outfds");
2610 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2611 pthread_mutex_unlock(&the_consumer_data
.lock
);
2615 the_consumer_data
.need_update
= 0;
2617 pthread_mutex_unlock(&the_consumer_data
.lock
);
2619 /* No FDs and consumer_quit, consumer_cleanup the thread */
2620 if (nb_fd
== 0 && nb_inactive_fd
== 0 &&
2621 CMM_LOAD_SHARED(consumer_quit
) == 1) {
2622 err
= 0; /* All is OK */
2625 /* poll on the array of fds */
2627 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2628 if (testpoint(consumerd_thread_data_poll
)) {
2631 health_poll_entry();
2632 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2634 DBG("poll num_rdy : %d", num_rdy
);
2635 if (num_rdy
== -1) {
2637 * Restart interrupted system call.
2639 if (errno
== EINTR
) {
2642 PERROR("Poll error");
2643 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2645 } else if (num_rdy
== 0) {
2646 DBG("Polling thread timed out");
2650 if (caa_unlikely(data_consumption_paused
)) {
2651 DBG("Data consumption paused, sleeping...");
2657 * If the consumer_data_pipe triggered poll go directly to the
2658 * beginning of the loop to update the array. We want to prioritize
2659 * array update over low-priority reads.
2661 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2662 ssize_t pipe_readlen
;
2664 DBG("consumer_data_pipe wake up");
2665 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2666 &new_stream
, sizeof(new_stream
));
2667 if (pipe_readlen
< sizeof(new_stream
)) {
2668 PERROR("Consumer data pipe");
2669 /* Continue so we can at least handle the current stream(s). */
2674 * If the stream is NULL, just ignore it. It's also possible that
2675 * the sessiond poll thread changed the consumer_quit state and is
2676 * waking us up to test it.
2678 if (new_stream
== NULL
) {
2679 validate_endpoint_status_data_stream();
2683 /* Continue to update the local streams and handle prio ones */
2687 /* Handle wakeup pipe. */
2688 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2690 ssize_t pipe_readlen
;
2692 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2694 if (pipe_readlen
< 0) {
2695 PERROR("Consumer data wakeup pipe");
2697 /* We've been awakened to handle stream(s). */
2698 ctx
->has_wakeup
= 0;
2701 /* Take care of high priority channels first. */
2702 for (i
= 0; i
< nb_fd
; i
++) {
2703 health_code_update();
2705 if (local_stream
[i
] == NULL
) {
2708 if (pollfd
[i
].revents
& POLLPRI
) {
2709 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2711 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2712 /* it's ok to have an unavailable sub-buffer */
2713 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2714 /* Clean the stream and free it. */
2715 consumer_del_stream(local_stream
[i
], data_ht
);
2716 local_stream
[i
] = NULL
;
2717 } else if (len
> 0) {
2718 local_stream
[i
]->data_read
= 1;
2724 * If we read high prio channel in this loop, try again
2725 * for more high prio data.
2731 /* Take care of low priority channels. */
2732 for (i
= 0; i
< nb_fd
; i
++) {
2733 health_code_update();
2735 if (local_stream
[i
] == NULL
) {
2738 if ((pollfd
[i
].revents
& POLLIN
) ||
2739 local_stream
[i
]->hangup_flush_done
||
2740 local_stream
[i
]->has_data
) {
2741 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2742 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2743 /* it's ok to have an unavailable sub-buffer */
2744 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2745 /* Clean the stream and free it. */
2746 consumer_del_stream(local_stream
[i
], data_ht
);
2747 local_stream
[i
] = NULL
;
2748 } else if (len
> 0) {
2749 local_stream
[i
]->data_read
= 1;
2754 /* Handle hangup and errors */
2755 for (i
= 0; i
< nb_fd
; i
++) {
2756 health_code_update();
2758 if (local_stream
[i
] == NULL
) {
2761 if (!local_stream
[i
]->hangup_flush_done
2762 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2763 && (the_consumer_data
.type
== LTTNG_CONSUMER32_UST
2764 || the_consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2765 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2767 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2768 /* Attempt read again, for the data we just flushed. */
2769 local_stream
[i
]->data_read
= 1;
2772 * If the poll flag is HUP/ERR/NVAL and we have
2773 * read no data in this pass, we can remove the
2774 * stream from its hash table.
2776 if ((pollfd
[i
].revents
& POLLHUP
)) {
2777 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2778 if (!local_stream
[i
]->data_read
) {
2779 consumer_del_stream(local_stream
[i
], data_ht
);
2780 local_stream
[i
] = NULL
;
2783 } else if (pollfd
[i
].revents
& POLLERR
) {
2784 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2785 if (!local_stream
[i
]->data_read
) {
2786 consumer_del_stream(local_stream
[i
], data_ht
);
2787 local_stream
[i
] = NULL
;
2790 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2791 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2792 if (!local_stream
[i
]->data_read
) {
2793 consumer_del_stream(local_stream
[i
], data_ht
);
2794 local_stream
[i
] = NULL
;
2798 if (local_stream
[i
] != NULL
) {
2799 local_stream
[i
]->data_read
= 0;
2806 DBG("polling thread exiting");
2811 * Close the write side of the pipe so epoll_wait() in
2812 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2813 * read side of the pipe. If we close them both, epoll_wait strangely does
2814 * not return and could create a endless wait period if the pipe is the
2815 * only tracked fd in the poll set. The thread will take care of closing
2818 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2823 ERR("Health error occurred in %s", __func__
);
2825 health_unregister(health_consumerd
);
2827 rcu_unregister_thread();
2832 * Close wake-up end of each stream belonging to the channel. This will
2833 * allow the poll() on the stream read-side to detect when the
2834 * write-side (application) finally closes them.
2837 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2839 struct lttng_ht
*ht
;
2840 struct lttng_consumer_stream
*stream
;
2841 struct lttng_ht_iter iter
;
2843 ht
= the_consumer_data
.stream_per_chan_id_ht
;
2846 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2847 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2848 ht
->match_fct
, &channel
->key
,
2849 &iter
.iter
, stream
, node_channel_id
.node
) {
2851 * Protect against teardown with mutex.
2853 pthread_mutex_lock(&stream
->lock
);
2854 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2857 switch (the_consumer_data
.type
) {
2858 case LTTNG_CONSUMER_KERNEL
:
2860 case LTTNG_CONSUMER32_UST
:
2861 case LTTNG_CONSUMER64_UST
:
2862 if (stream
->metadata_flag
) {
2863 /* Safe and protected by the stream lock. */
2864 lttng_ustconsumer_close_metadata(stream
->chan
);
2867 * Note: a mutex is taken internally within
2868 * liblttng-ust-ctl to protect timer wakeup_fd
2869 * use from concurrent close.
2871 lttng_ustconsumer_close_stream_wakeup(stream
);
2875 ERR("Unknown consumer_data type");
2879 pthread_mutex_unlock(&stream
->lock
);
2884 static void destroy_channel_ht(struct lttng_ht
*ht
)
2886 struct lttng_ht_iter iter
;
2887 struct lttng_consumer_channel
*channel
;
2895 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2896 ret
= lttng_ht_del(ht
, &iter
);
2901 lttng_ht_destroy(ht
);
2905 * This thread polls the channel fds to detect when they are being
2906 * closed. It closes all related streams if the channel is detected as
2907 * closed. It is currently only used as a shim layer for UST because the
2908 * consumerd needs to keep the per-stream wakeup end of pipes open for
2911 void *consumer_thread_channel_poll(void *data
)
2913 int ret
, i
, pollfd
, err
= -1;
2914 uint32_t revents
, nb_fd
;
2915 struct lttng_consumer_channel
*chan
= NULL
;
2916 struct lttng_ht_iter iter
;
2917 struct lttng_ht_node_u64
*node
;
2918 struct lttng_poll_event events
;
2919 struct lttng_consumer_local_data
*ctx
= data
;
2920 struct lttng_ht
*channel_ht
;
2922 rcu_register_thread();
2924 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2926 if (testpoint(consumerd_thread_channel
)) {
2927 goto error_testpoint
;
2930 health_code_update();
2932 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2934 /* ENOMEM at this point. Better to bail out. */
2938 DBG("Thread channel poll started");
2940 /* Size is set to 1 for the consumer_channel pipe */
2941 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2943 ERR("Poll set creation failed");
2947 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2953 DBG("Channel main loop started");
2957 health_code_update();
2958 DBG("Channel poll wait");
2959 health_poll_entry();
2960 ret
= lttng_poll_wait(&events
, -1);
2961 DBG("Channel poll return from wait with %d fd(s)",
2962 LTTNG_POLL_GETNB(&events
));
2964 DBG("Channel event caught in thread");
2966 if (errno
== EINTR
) {
2967 ERR("Poll EINTR caught");
2970 if (LTTNG_POLL_GETNB(&events
) == 0) {
2971 err
= 0; /* All is OK */
2978 /* From here, the event is a channel wait fd */
2979 for (i
= 0; i
< nb_fd
; i
++) {
2980 health_code_update();
2982 revents
= LTTNG_POLL_GETEV(&events
, i
);
2983 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2985 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2986 if (revents
& LPOLLIN
) {
2987 enum consumer_channel_action action
;
2990 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2993 ERR("Error reading channel pipe");
2995 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3000 case CONSUMER_CHANNEL_ADD
:
3001 DBG("Adding channel %d to poll set",
3004 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
3007 lttng_ht_add_unique_u64(channel_ht
,
3008 &chan
->wait_fd_node
);
3010 /* Add channel to the global poll events list */
3011 lttng_poll_add(&events
, chan
->wait_fd
,
3012 LPOLLERR
| LPOLLHUP
);
3014 case CONSUMER_CHANNEL_DEL
:
3017 * This command should never be called if the channel
3018 * has streams monitored by either the data or metadata
3019 * thread. The consumer only notify this thread with a
3020 * channel del. command if it receives a destroy
3021 * channel command from the session daemon that send it
3022 * if a command prior to the GET_CHANNEL failed.
3026 chan
= consumer_find_channel(key
);
3029 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
3032 lttng_poll_del(&events
, chan
->wait_fd
);
3033 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
3034 ret
= lttng_ht_del(channel_ht
, &iter
);
3037 switch (the_consumer_data
.type
) {
3038 case LTTNG_CONSUMER_KERNEL
:
3040 case LTTNG_CONSUMER32_UST
:
3041 case LTTNG_CONSUMER64_UST
:
3042 health_code_update();
3043 /* Destroy streams that might have been left in the stream list. */
3044 clean_channel_stream_list(chan
);
3047 ERR("Unknown consumer_data type");
3052 * Release our own refcount. Force channel deletion even if
3053 * streams were not initialized.
3055 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3056 consumer_del_channel(chan
);
3061 case CONSUMER_CHANNEL_QUIT
:
3063 * Remove the pipe from the poll set and continue the loop
3064 * since their might be data to consume.
3066 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3069 ERR("Unknown action");
3072 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3073 DBG("Channel thread pipe hung up");
3075 * Remove the pipe from the poll set and continue the loop
3076 * since their might be data to consume.
3078 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3081 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3085 /* Handle other stream */
3091 uint64_t tmp_id
= (uint64_t) pollfd
;
3093 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3095 node
= lttng_ht_iter_get_node_u64(&iter
);
3098 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
3101 /* Check for error event */
3102 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3103 DBG("Channel fd %d is hup|err.", pollfd
);
3105 lttng_poll_del(&events
, chan
->wait_fd
);
3106 ret
= lttng_ht_del(channel_ht
, &iter
);
3110 * This will close the wait fd for each stream associated to
3111 * this channel AND monitored by the data/metadata thread thus
3112 * will be clean by the right thread.
3114 consumer_close_channel_streams(chan
);
3116 /* Release our own refcount */
3117 if (!uatomic_sub_return(&chan
->refcount
, 1)
3118 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3119 consumer_del_channel(chan
);
3122 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3127 /* Release RCU lock for the channel looked up */
3135 lttng_poll_clean(&events
);
3137 destroy_channel_ht(channel_ht
);
3140 DBG("Channel poll thread exiting");
3143 ERR("Health error occurred in %s", __func__
);
3145 health_unregister(health_consumerd
);
3146 rcu_unregister_thread();
3150 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3151 struct pollfd
*sockpoll
, int client_socket
)
3158 ret
= lttng_consumer_poll_socket(sockpoll
);
3162 DBG("Metadata connection on client_socket");
3164 /* Blocking call, waiting for transmission */
3165 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3166 if (ctx
->consumer_metadata_socket
< 0) {
3167 WARN("On accept metadata");
3178 * This thread listens on the consumerd socket and receives the file
3179 * descriptors from the session daemon.
3181 void *consumer_thread_sessiond_poll(void *data
)
3183 int sock
= -1, client_socket
, ret
, err
= -1;
3185 * structure to poll for incoming data on communication socket avoids
3186 * making blocking sockets.
3188 struct pollfd consumer_sockpoll
[2];
3189 struct lttng_consumer_local_data
*ctx
= data
;
3191 rcu_register_thread();
3193 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3195 if (testpoint(consumerd_thread_sessiond
)) {
3196 goto error_testpoint
;
3199 health_code_update();
3201 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3202 unlink(ctx
->consumer_command_sock_path
);
3203 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3204 if (client_socket
< 0) {
3205 ERR("Cannot create command socket");
3209 ret
= lttcomm_listen_unix_sock(client_socket
);
3214 DBG("Sending ready command to lttng-sessiond");
3215 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3216 /* return < 0 on error, but == 0 is not fatal */
3218 ERR("Error sending ready command to lttng-sessiond");
3222 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3223 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3224 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3225 consumer_sockpoll
[1].fd
= client_socket
;
3226 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3228 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3236 DBG("Connection on client_socket");
3238 /* Blocking call, waiting for transmission */
3239 sock
= lttcomm_accept_unix_sock(client_socket
);
3246 * Setup metadata socket which is the second socket connection on the
3247 * command unix socket.
3249 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3258 /* This socket is not useful anymore. */
3259 ret
= close(client_socket
);
3261 PERROR("close client_socket");
3265 /* update the polling structure to poll on the established socket */
3266 consumer_sockpoll
[1].fd
= sock
;
3267 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3270 health_code_update();
3272 health_poll_entry();
3273 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3282 DBG("Incoming command on sock");
3283 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3286 * This could simply be a session daemon quitting. Don't output
3289 DBG("Communication interrupted on command socket");
3293 if (CMM_LOAD_SHARED(consumer_quit
)) {
3294 DBG("consumer_thread_receive_fds received quit from signal");
3295 err
= 0; /* All is OK */
3298 DBG("Received command on sock");
3304 DBG("Consumer thread sessiond poll exiting");
3307 * Close metadata streams since the producer is the session daemon which
3310 * NOTE: for now, this only applies to the UST tracer.
3312 lttng_consumer_close_all_metadata();
3315 * when all fds have hung up, the polling thread
3318 CMM_STORE_SHARED(consumer_quit
, 1);
3321 * Notify the data poll thread to poll back again and test the
3322 * consumer_quit state that we just set so to quit gracefully.
3324 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3326 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3328 notify_health_quit_pipe(health_quit_pipe
);
3330 /* Cleaning up possibly open sockets. */
3334 PERROR("close sock sessiond poll");
3337 if (client_socket
>= 0) {
3338 ret
= close(client_socket
);
3340 PERROR("close client_socket sessiond poll");
3347 ERR("Health error occurred in %s", __func__
);
3349 health_unregister(health_consumerd
);
3351 rcu_unregister_thread();
3355 static int post_consume(struct lttng_consumer_stream
*stream
,
3356 const struct stream_subbuffer
*subbuffer
,
3357 struct lttng_consumer_local_data
*ctx
)
3361 const size_t count
= lttng_dynamic_array_get_count(
3362 &stream
->read_subbuffer_ops
.post_consume_cbs
);
3364 for (i
= 0; i
< count
; i
++) {
3365 const post_consume_cb op
= *(post_consume_cb
*) lttng_dynamic_array_get_element(
3366 &stream
->read_subbuffer_ops
.post_consume_cbs
,
3369 ret
= op(stream
, subbuffer
, ctx
);
3378 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3379 struct lttng_consumer_local_data
*ctx
,
3380 bool locked_by_caller
)
3382 ssize_t ret
, written_bytes
= 0;
3384 struct stream_subbuffer subbuffer
= {};
3386 if (!locked_by_caller
) {
3387 stream
->read_subbuffer_ops
.lock(stream
);
3390 if (stream
->read_subbuffer_ops
.on_wake_up
) {
3391 ret
= stream
->read_subbuffer_ops
.on_wake_up(stream
);
3398 * If the stream was flagged to be ready for rotation before we extract
3399 * the next packet, rotate it now.
3401 if (stream
->rotate_ready
) {
3402 DBG("Rotate stream before consuming data");
3403 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3405 ERR("Stream rotation error before consuming data");
3410 ret
= stream
->read_subbuffer_ops
.get_next_subbuffer(stream
, &subbuffer
);
3412 if (ret
== -ENODATA
) {
3420 ret
= stream
->read_subbuffer_ops
.pre_consume_subbuffer(
3421 stream
, &subbuffer
);
3423 goto error_put_subbuf
;
3426 written_bytes
= stream
->read_subbuffer_ops
.consume_subbuffer(
3427 ctx
, stream
, &subbuffer
);
3428 if (written_bytes
<= 0) {
3429 ERR("Error consuming subbuffer: (%zd)", written_bytes
);
3430 ret
= (int) written_bytes
;
3431 goto error_put_subbuf
;
3434 ret
= stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3439 ret
= post_consume(stream
, &subbuffer
, ctx
);
3445 * After extracting the packet, we check if the stream is now ready to
3446 * be rotated and perform the action immediately.
3448 * Don't overwrite `ret` as callers expect the number of bytes
3449 * consumed to be returned on success.
3451 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
3452 if (rotation_ret
== 1) {
3453 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3454 if (rotation_ret
< 0) {
3456 ERR("Stream rotation error after consuming data");
3460 } else if (rotation_ret
< 0) {
3462 ERR("Failed to check if stream was ready to rotate after consuming data");
3467 if (stream
->read_subbuffer_ops
.on_sleep
) {
3468 stream
->read_subbuffer_ops
.on_sleep(stream
, ctx
);
3471 ret
= written_bytes
;
3473 if (!locked_by_caller
) {
3474 stream
->read_subbuffer_ops
.unlock(stream
);
3479 (void) stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3483 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3485 switch (the_consumer_data
.type
) {
3486 case LTTNG_CONSUMER_KERNEL
:
3487 return lttng_kconsumer_on_recv_stream(stream
);
3488 case LTTNG_CONSUMER32_UST
:
3489 case LTTNG_CONSUMER64_UST
:
3490 return lttng_ustconsumer_on_recv_stream(stream
);
3492 ERR("Unknown consumer_data type");
3499 * Allocate and set consumer data hash tables.
3501 int lttng_consumer_init(void)
3503 the_consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3504 if (!the_consumer_data
.channel_ht
) {
3508 the_consumer_data
.channels_by_session_id_ht
=
3509 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3510 if (!the_consumer_data
.channels_by_session_id_ht
) {
3514 the_consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3515 if (!the_consumer_data
.relayd_ht
) {
3519 the_consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3520 if (!the_consumer_data
.stream_list_ht
) {
3524 the_consumer_data
.stream_per_chan_id_ht
=
3525 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3526 if (!the_consumer_data
.stream_per_chan_id_ht
) {
3530 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3535 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3540 the_consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3541 if (!the_consumer_data
.chunk_registry
) {
3552 * Process the ADD_RELAYD command receive by a consumer.
3554 * This will create a relayd socket pair and add it to the relayd hash table.
3555 * The caller MUST acquire a RCU read side lock before calling it.
3557 void consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3558 struct lttng_consumer_local_data
*ctx
, int sock
,
3559 struct pollfd
*consumer_sockpoll
,
3560 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3561 uint64_t relayd_session_id
)
3563 int fd
= -1, ret
= -1, relayd_created
= 0;
3564 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3565 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3568 assert(relayd_sock
);
3570 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3572 /* Get relayd reference if exists. */
3573 relayd
= consumer_find_relayd(net_seq_idx
);
3574 if (relayd
== NULL
) {
3575 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3576 /* Not found. Allocate one. */
3577 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3578 if (relayd
== NULL
) {
3579 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3582 relayd
->sessiond_session_id
= sessiond_id
;
3587 * This code path MUST continue to the consumer send status message to
3588 * we can notify the session daemon and continue our work without
3589 * killing everything.
3593 * relayd key should never be found for control socket.
3595 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3598 /* First send a status message before receiving the fds. */
3599 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3601 /* Somehow, the session daemon is not responding anymore. */
3602 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3603 goto error_nosignal
;
3606 /* Poll on consumer socket. */
3607 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3609 /* Needing to exit in the middle of a command: error. */
3610 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3611 goto error_nosignal
;
3614 /* Get relayd socket from session daemon */
3615 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3616 if (ret
!= sizeof(fd
)) {
3617 fd
= -1; /* Just in case it gets set with an invalid value. */
3620 * Failing to receive FDs might indicate a major problem such as
3621 * reaching a fd limit during the receive where the kernel returns a
3622 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3623 * don't take any chances and stop everything.
3625 * XXX: Feature request #558 will fix that and avoid this possible
3626 * issue when reaching the fd limit.
3628 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3629 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3633 /* Copy socket information and received FD */
3634 switch (sock_type
) {
3635 case LTTNG_STREAM_CONTROL
:
3636 /* Copy received lttcomm socket */
3637 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3638 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3639 /* Handle create_sock error. */
3641 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3645 * Close the socket created internally by
3646 * lttcomm_create_sock, so we can replace it by the one
3647 * received from sessiond.
3649 if (close(relayd
->control_sock
.sock
.fd
)) {
3653 /* Assign new file descriptor */
3654 relayd
->control_sock
.sock
.fd
= fd
;
3655 /* Assign version values. */
3656 relayd
->control_sock
.major
= relayd_sock
->major
;
3657 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3659 relayd
->relayd_session_id
= relayd_session_id
;
3662 case LTTNG_STREAM_DATA
:
3663 /* Copy received lttcomm socket */
3664 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3665 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3666 /* Handle create_sock error. */
3668 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3672 * Close the socket created internally by
3673 * lttcomm_create_sock, so we can replace it by the one
3674 * received from sessiond.
3676 if (close(relayd
->data_sock
.sock
.fd
)) {
3680 /* Assign new file descriptor */
3681 relayd
->data_sock
.sock
.fd
= fd
;
3682 /* Assign version values. */
3683 relayd
->data_sock
.major
= relayd_sock
->major
;
3684 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3687 ERR("Unknown relayd socket type (%d)", sock_type
);
3688 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3692 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3693 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3694 relayd
->net_seq_idx
, fd
);
3696 * We gave the ownership of the fd to the relayd structure. Set the
3697 * fd to -1 so we don't call close() on it in the error path below.
3701 /* We successfully added the socket. Send status back. */
3702 ret
= consumer_send_status_msg(sock
, ret_code
);
3704 /* Somehow, the session daemon is not responding anymore. */
3705 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3706 goto error_nosignal
;
3710 * Add relayd socket pair to consumer data hashtable. If object already
3711 * exists or on error, the function gracefully returns.
3720 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3721 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3725 /* Close received socket if valid. */
3728 PERROR("close received socket");
3732 if (relayd_created
) {
3738 * Search for a relayd associated to the session id and return the reference.
3740 * A rcu read side lock MUST be acquire before calling this function and locked
3741 * until the relayd object is no longer necessary.
3743 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3745 struct lttng_ht_iter iter
;
3746 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3748 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3749 cds_lfht_for_each_entry(the_consumer_data
.relayd_ht
->ht
, &iter
.iter
,
3750 relayd
, node
.node
) {
3752 * Check by sessiond id which is unique here where the relayd session
3753 * id might not be when having multiple relayd.
3755 if (relayd
->sessiond_session_id
== id
) {
3756 /* Found the relayd. There can be only one per id. */
3768 * Check if for a given session id there is still data needed to be extract
3771 * Return 1 if data is pending or else 0 meaning ready to be read.
3773 int consumer_data_pending(uint64_t id
)
3776 struct lttng_ht_iter iter
;
3777 struct lttng_ht
*ht
;
3778 struct lttng_consumer_stream
*stream
;
3779 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3780 int (*data_pending
)(struct lttng_consumer_stream
*);
3782 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3785 pthread_mutex_lock(&the_consumer_data
.lock
);
3787 switch (the_consumer_data
.type
) {
3788 case LTTNG_CONSUMER_KERNEL
:
3789 data_pending
= lttng_kconsumer_data_pending
;
3791 case LTTNG_CONSUMER32_UST
:
3792 case LTTNG_CONSUMER64_UST
:
3793 data_pending
= lttng_ustconsumer_data_pending
;
3796 ERR("Unknown consumer data type");
3800 /* Ease our life a bit */
3801 ht
= the_consumer_data
.stream_list_ht
;
3803 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3804 ht
->hash_fct(&id
, lttng_ht_seed
),
3806 &iter
.iter
, stream
, node_session_id
.node
) {
3807 pthread_mutex_lock(&stream
->lock
);
3810 * A removed node from the hash table indicates that the stream has
3811 * been deleted thus having a guarantee that the buffers are closed
3812 * on the consumer side. However, data can still be transmitted
3813 * over the network so don't skip the relayd check.
3815 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3817 /* Check the stream if there is data in the buffers. */
3818 ret
= data_pending(stream
);
3820 pthread_mutex_unlock(&stream
->lock
);
3825 pthread_mutex_unlock(&stream
->lock
);
3828 relayd
= find_relayd_by_session_id(id
);
3830 unsigned int is_data_inflight
= 0;
3832 /* Send init command for data pending. */
3833 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3834 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3835 relayd
->relayd_session_id
);
3837 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3838 /* Communication error thus the relayd so no data pending. */
3839 goto data_not_pending
;
3842 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3843 ht
->hash_fct(&id
, lttng_ht_seed
),
3845 &iter
.iter
, stream
, node_session_id
.node
) {
3846 if (stream
->metadata_flag
) {
3847 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3848 stream
->relayd_stream_id
);
3850 ret
= relayd_data_pending(&relayd
->control_sock
,
3851 stream
->relayd_stream_id
,
3852 stream
->next_net_seq_num
- 1);
3856 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3858 } else if (ret
< 0) {
3859 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3860 lttng_consumer_cleanup_relayd(relayd
);
3861 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3862 goto data_not_pending
;
3866 /* Send end command for data pending. */
3867 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3868 relayd
->relayd_session_id
, &is_data_inflight
);
3869 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3871 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3872 lttng_consumer_cleanup_relayd(relayd
);
3873 goto data_not_pending
;
3875 if (is_data_inflight
) {
3881 * Finding _no_ node in the hash table and no inflight data means that the
3882 * stream(s) have been removed thus data is guaranteed to be available for
3883 * analysis from the trace files.
3887 /* Data is available to be read by a viewer. */
3888 pthread_mutex_unlock(&the_consumer_data
.lock
);
3893 /* Data is still being extracted from buffers. */
3894 pthread_mutex_unlock(&the_consumer_data
.lock
);
3900 * Send a ret code status message to the sessiond daemon.
3902 * Return the sendmsg() return value.
3904 int consumer_send_status_msg(int sock
, int ret_code
)
3906 struct lttcomm_consumer_status_msg msg
;
3908 memset(&msg
, 0, sizeof(msg
));
3909 msg
.ret_code
= ret_code
;
3911 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3915 * Send a channel status message to the sessiond daemon.
3917 * Return the sendmsg() return value.
3919 int consumer_send_status_channel(int sock
,
3920 struct lttng_consumer_channel
*channel
)
3922 struct lttcomm_consumer_status_channel msg
;
3926 memset(&msg
, 0, sizeof(msg
));
3928 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3930 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3931 msg
.key
= channel
->key
;
3932 msg
.stream_count
= channel
->streams
.count
;
3935 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3938 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3939 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3940 uint64_t max_sb_size
)
3942 unsigned long start_pos
;
3944 if (!nb_packets_per_stream
) {
3945 return consumed_pos
; /* Grab everything */
3947 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
3948 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3949 if ((long) (start_pos
- consumed_pos
) < 0) {
3950 return consumed_pos
; /* Grab everything */
3955 /* Stream lock must be held by the caller. */
3956 static int sample_stream_positions(struct lttng_consumer_stream
*stream
,
3957 unsigned long *produced
, unsigned long *consumed
)
3961 ASSERT_LOCKED(stream
->lock
);
3963 ret
= lttng_consumer_sample_snapshot_positions(stream
);
3965 ERR("Failed to sample snapshot positions");
3969 ret
= lttng_consumer_get_produced_snapshot(stream
, produced
);
3971 ERR("Failed to sample produced position");
3975 ret
= lttng_consumer_get_consumed_snapshot(stream
, consumed
);
3977 ERR("Failed to sample consumed position");
3986 * Sample the rotate position for all the streams of a channel. If a stream
3987 * is already at the rotate position (produced == consumed), we flag it as
3988 * ready for rotation. The rotation of ready streams occurs after we have
3989 * replied to the session daemon that we have finished sampling the positions.
3990 * Must be called with RCU read-side lock held to ensure existence of channel.
3992 * Returns 0 on success, < 0 on error
3994 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
3995 uint64_t key
, uint64_t relayd_id
, uint32_t metadata
,
3996 struct lttng_consumer_local_data
*ctx
)
3999 struct lttng_consumer_stream
*stream
;
4000 struct lttng_ht_iter iter
;
4001 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
4002 struct lttng_dynamic_array stream_rotation_positions
;
4003 uint64_t next_chunk_id
, stream_count
= 0;
4004 enum lttng_trace_chunk_status chunk_status
;
4005 const bool is_local_trace
= relayd_id
== -1ULL;
4006 struct consumer_relayd_sock_pair
*relayd
= NULL
;
4007 bool rotating_to_new_chunk
= true;
4008 /* Array of `struct lttng_consumer_stream *` */
4009 struct lttng_dynamic_pointer_array streams_packet_to_open
;
4012 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
4014 lttng_dynamic_array_init(&stream_rotation_positions
,
4015 sizeof(struct relayd_stream_rotation_position
), NULL
);
4016 lttng_dynamic_pointer_array_init(&streams_packet_to_open
, NULL
);
4020 pthread_mutex_lock(&channel
->lock
);
4021 assert(channel
->trace_chunk
);
4022 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
,
4024 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4026 goto end_unlock_channel
;
4029 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4030 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4031 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4032 stream
, node_channel_id
.node
) {
4033 unsigned long produced_pos
= 0, consumed_pos
= 0;
4035 health_code_update();
4038 * Lock stream because we are about to change its state.
4040 pthread_mutex_lock(&stream
->lock
);
4042 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
4043 rotating_to_new_chunk
= false;
4047 * Do not flush a packet when rotating from a NULL trace
4048 * chunk. The stream has no means to output data, and the prior
4049 * rotation which rotated to NULL performed that side-effect
4050 * already. No new data can be produced when a stream has no
4051 * associated trace chunk (e.g. a stop followed by a rotate).
4053 if (stream
->trace_chunk
) {
4056 if (stream
->metadata_flag
) {
4058 * Don't produce an empty metadata packet,
4059 * simply close the current one.
4061 * Metadata is regenerated on every trace chunk
4062 * switch; there is no concern that no data was
4065 flush_active
= true;
4068 * Only flush an empty packet if the "packet
4069 * open" could not be performed on transition
4070 * to a new trace chunk and no packets were
4071 * consumed within the chunk's lifetime.
4073 if (stream
->opened_packet_in_current_trace_chunk
) {
4074 flush_active
= true;
4077 * Stream could have been full at the
4078 * time of rotation, but then have had
4079 * no activity at all.
4081 * It is important to flush a packet
4082 * to prevent 0-length files from being
4083 * produced as most viewers choke on
4086 * Unfortunately viewers will not be
4087 * able to know that tracing was active
4088 * for this stream during this trace
4091 ret
= sample_stream_positions(stream
, &produced_pos
, &consumed_pos
);
4093 goto end_unlock_stream
;
4097 * Don't flush an empty packet if data
4098 * was produced; it will be consumed
4099 * before the rotation completes.
4101 flush_active
= produced_pos
!= consumed_pos
;
4102 if (!flush_active
) {
4103 const char *trace_chunk_name
;
4104 uint64_t trace_chunk_id
;
4106 chunk_status
= lttng_trace_chunk_get_name(
4107 stream
->trace_chunk
,
4110 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NONE
) {
4111 trace_chunk_name
= "none";
4115 * Consumer trace chunks are
4118 chunk_status
= lttng_trace_chunk_get_id(
4119 stream
->trace_chunk
,
4121 assert(chunk_status
==
4122 LTTNG_TRACE_CHUNK_STATUS_OK
);
4124 DBG("Unable to open packet for stream during trace chunk's lifetime. "
4125 "Flushing an empty packet to prevent an empty file from being created: "
4126 "stream id = %" PRIu64
", trace chunk name = `%s`, trace chunk id = %" PRIu64
,
4127 stream
->key
, trace_chunk_name
, trace_chunk_id
);
4133 * Close the current packet before sampling the
4134 * ring buffer positions.
4136 ret
= consumer_stream_flush_buffer(stream
, flush_active
);
4138 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4140 goto end_unlock_stream
;
4144 ret
= lttng_consumer_take_snapshot(stream
);
4145 if (ret
< 0 && ret
!= -ENODATA
&& ret
!= -EAGAIN
) {
4146 ERR("Failed to sample snapshot position during channel rotation");
4147 goto end_unlock_stream
;
4150 ret
= lttng_consumer_get_produced_snapshot(stream
,
4153 ERR("Failed to sample produced position during channel rotation");
4154 goto end_unlock_stream
;
4157 ret
= lttng_consumer_get_consumed_snapshot(stream
,
4160 ERR("Failed to sample consumed position during channel rotation");
4161 goto end_unlock_stream
;
4165 * Align produced position on the start-of-packet boundary of the first
4166 * packet going into the next trace chunk.
4168 produced_pos
= ALIGN_FLOOR(produced_pos
, stream
->max_sb_size
);
4169 if (consumed_pos
== produced_pos
) {
4170 DBG("Set rotate ready for stream %" PRIu64
" produced = %lu consumed = %lu",
4171 stream
->key
, produced_pos
, consumed_pos
);
4172 stream
->rotate_ready
= true;
4174 DBG("Different consumed and produced positions "
4175 "for stream %" PRIu64
" produced = %lu consumed = %lu",
4176 stream
->key
, produced_pos
, consumed_pos
);
4179 * The rotation position is based on the packet_seq_num of the
4180 * packet following the last packet that was consumed for this
4181 * stream, incremented by the offset between produced and
4182 * consumed positions. This rotation position is a lower bound
4183 * (inclusive) at which the next trace chunk starts. Since it
4184 * is a lower bound, it is OK if the packet_seq_num does not
4185 * correspond exactly to the same packet identified by the
4186 * consumed_pos, which can happen in overwrite mode.
4188 if (stream
->sequence_number_unavailable
) {
4190 * Rotation should never be performed on a session which
4191 * interacts with a pre-2.8 lttng-modules, which does
4192 * not implement packet sequence number.
4194 ERR("Failure to rotate stream %" PRIu64
": sequence number unavailable",
4197 goto end_unlock_stream
;
4199 stream
->rotate_position
= stream
->last_sequence_number
+ 1 +
4200 ((produced_pos
- consumed_pos
) / stream
->max_sb_size
);
4201 DBG("Set rotation position for stream %" PRIu64
" at position %" PRIu64
,
4202 stream
->key
, stream
->rotate_position
);
4204 if (!is_local_trace
) {
4206 * The relay daemon control protocol expects a rotation
4207 * position as "the sequence number of the first packet
4208 * _after_ the current trace chunk".
4210 const struct relayd_stream_rotation_position position
= {
4211 .stream_id
= stream
->relayd_stream_id
,
4212 .rotate_at_seq_num
= stream
->rotate_position
,
4215 ret
= lttng_dynamic_array_add_element(
4216 &stream_rotation_positions
,
4219 ERR("Failed to allocate stream rotation position");
4220 goto end_unlock_stream
;
4225 stream
->opened_packet_in_current_trace_chunk
= false;
4227 if (rotating_to_new_chunk
&& !stream
->metadata_flag
) {
4229 * Attempt to flush an empty packet as close to the
4230 * rotation point as possible. In the event where a
4231 * stream remains inactive after the rotation point,
4232 * this ensures that the new trace chunk has a
4233 * beginning timestamp set at the begining of the
4234 * trace chunk instead of only creating an empty
4235 * packet when the trace chunk is stopped.
4237 * This indicates to the viewers that the stream
4238 * was being recorded, but more importantly it
4239 * allows viewers to determine a useable trace
4242 * This presents a problem in the case where the
4243 * ring-buffer is completely full.
4245 * Consider the following scenario:
4246 * - The consumption of data is slow (slow network,
4248 * - The ring buffer is full,
4249 * - A rotation is initiated,
4250 * - The flush below does nothing (no space left to
4251 * open a new packet),
4252 * - The other streams rotate very soon, and new
4253 * data is produced in the new chunk,
4254 * - This stream completes its rotation long after the
4255 * rotation was initiated
4256 * - The session is stopped before any event can be
4257 * produced in this stream's buffers.
4259 * The resulting trace chunk will have a single packet
4260 * temporaly at the end of the trace chunk for this
4261 * stream making the stream intersection more narrow
4262 * than it should be.
4264 * To work-around this, an empty flush is performed
4265 * after the first consumption of a packet during a
4266 * rotation if open_packet fails. The idea is that
4267 * consuming a packet frees enough space to switch
4268 * packets in this scenario and allows the tracer to
4269 * "stamp" the beginning of the new trace chunk at the
4270 * earliest possible point.
4272 * The packet open is performed after the channel
4273 * rotation to ensure that no attempt to open a packet
4274 * is performed in a stream that has no active trace
4277 ret
= lttng_dynamic_pointer_array_add_pointer(
4278 &streams_packet_to_open
, stream
);
4280 PERROR("Failed to add a stream pointer to array of streams in which to open a packet");
4282 goto end_unlock_stream
;
4286 pthread_mutex_unlock(&stream
->lock
);
4290 if (!is_local_trace
) {
4291 relayd
= consumer_find_relayd(relayd_id
);
4293 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4295 goto end_unlock_channel
;
4298 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4299 ret
= relayd_rotate_streams(&relayd
->control_sock
, stream_count
,
4300 rotating_to_new_chunk
? &next_chunk_id
: NULL
,
4301 (const struct relayd_stream_rotation_position
*)
4302 stream_rotation_positions
.buffer
4304 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4306 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4307 relayd
->net_seq_idx
);
4308 lttng_consumer_cleanup_relayd(relayd
);
4309 goto end_unlock_channel
;
4313 for (stream_idx
= 0;
4314 stream_idx
< lttng_dynamic_pointer_array_get_count(
4315 &streams_packet_to_open
);
4317 enum consumer_stream_open_packet_status status
;
4319 stream
= lttng_dynamic_pointer_array_get_pointer(
4320 &streams_packet_to_open
, stream_idx
);
4322 pthread_mutex_lock(&stream
->lock
);
4323 status
= consumer_stream_open_packet(stream
);
4324 pthread_mutex_unlock(&stream
->lock
);
4326 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
4327 DBG("Opened a packet after a rotation: stream id = %" PRIu64
4328 ", channel name = %s, session id = %" PRIu64
,
4329 stream
->key
, stream
->chan
->name
,
4330 stream
->chan
->session_id
);
4332 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
4334 * Can't open a packet as there is no space left
4335 * in the buffer. A new packet will be opened
4336 * once one has been consumed.
4338 DBG("No space left to open a packet after a rotation: stream id = %" PRIu64
4339 ", channel name = %s, session id = %" PRIu64
,
4340 stream
->key
, stream
->chan
->name
,
4341 stream
->chan
->session_id
);
4343 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
4344 /* Logged by callee. */
4346 goto end_unlock_channel
;
4352 pthread_mutex_unlock(&channel
->lock
);
4357 pthread_mutex_unlock(&stream
->lock
);
4359 pthread_mutex_unlock(&channel
->lock
);
4362 lttng_dynamic_array_reset(&stream_rotation_positions
);
4363 lttng_dynamic_pointer_array_reset(&streams_packet_to_open
);
4368 int consumer_clear_buffer(struct lttng_consumer_stream
*stream
)
4371 unsigned long consumed_pos_before
, consumed_pos_after
;
4373 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4375 ERR("Taking snapshot positions");
4379 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_before
);
4381 ERR("Consumed snapshot position");
4385 switch (the_consumer_data
.type
) {
4386 case LTTNG_CONSUMER_KERNEL
:
4387 ret
= kernctl_buffer_clear(stream
->wait_fd
);
4389 ERR("Failed to clear kernel stream (ret = %d)", ret
);
4393 case LTTNG_CONSUMER32_UST
:
4394 case LTTNG_CONSUMER64_UST
:
4395 lttng_ustconsumer_clear_buffer(stream
);
4398 ERR("Unknown consumer_data type");
4402 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4404 ERR("Taking snapshot positions");
4407 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_after
);
4409 ERR("Consumed snapshot position");
4412 DBG("clear: before: %lu after: %lu", consumed_pos_before
, consumed_pos_after
);
4418 int consumer_clear_stream(struct lttng_consumer_stream
*stream
)
4422 ret
= consumer_stream_flush_buffer(stream
, 1);
4424 ERR("Failed to flush stream %" PRIu64
" during channel clear",
4426 ret
= LTTCOMM_CONSUMERD_FATAL
;
4430 ret
= consumer_clear_buffer(stream
);
4432 ERR("Failed to clear stream %" PRIu64
" during channel clear",
4434 ret
= LTTCOMM_CONSUMERD_FATAL
;
4438 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4444 int consumer_clear_unmonitored_channel(struct lttng_consumer_channel
*channel
)
4447 struct lttng_consumer_stream
*stream
;
4450 pthread_mutex_lock(&channel
->lock
);
4451 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
4452 health_code_update();
4453 pthread_mutex_lock(&stream
->lock
);
4454 ret
= consumer_clear_stream(stream
);
4458 pthread_mutex_unlock(&stream
->lock
);
4460 pthread_mutex_unlock(&channel
->lock
);
4465 pthread_mutex_unlock(&stream
->lock
);
4466 pthread_mutex_unlock(&channel
->lock
);
4472 * Check if a stream is ready to be rotated after extracting it.
4474 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4475 * error. Stream lock must be held.
4477 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4479 DBG("Check is rotate ready for stream %" PRIu64
4480 " ready %u rotate_position %" PRIu64
4481 " last_sequence_number %" PRIu64
,
4482 stream
->key
, stream
->rotate_ready
,
4483 stream
->rotate_position
, stream
->last_sequence_number
);
4484 if (stream
->rotate_ready
) {
4489 * If packet seq num is unavailable, it means we are interacting
4490 * with a pre-2.8 lttng-modules which does not implement the
4491 * sequence number. Rotation should never be used by sessiond in this
4494 if (stream
->sequence_number_unavailable
) {
4495 ERR("Internal error: rotation used on stream %" PRIu64
4496 " with unavailable sequence number",
4501 if (stream
->rotate_position
== -1ULL ||
4502 stream
->last_sequence_number
== -1ULL) {
4507 * Rotate position not reached yet. The stream rotate position is
4508 * the position of the next packet belonging to the next trace chunk,
4509 * but consumerd considers rotation ready when reaching the last
4510 * packet of the current chunk, hence the "rotate_position - 1".
4513 DBG("Check is rotate ready for stream %" PRIu64
4514 " last_sequence_number %" PRIu64
4515 " rotate_position %" PRIu64
,
4516 stream
->key
, stream
->last_sequence_number
,
4517 stream
->rotate_position
);
4518 if (stream
->last_sequence_number
>= stream
->rotate_position
- 1) {
4526 * Reset the state for a stream after a rotation occurred.
4528 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4530 DBG("lttng_consumer_reset_stream_rotate_state for stream %" PRIu64
,
4532 stream
->rotate_position
= -1ULL;
4533 stream
->rotate_ready
= false;
4537 * Perform the rotation a local stream file.
4540 int rotate_local_stream(struct lttng_consumer_local_data
*ctx
,
4541 struct lttng_consumer_stream
*stream
)
4545 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4548 stream
->tracefile_size_current
= 0;
4549 stream
->tracefile_count_current
= 0;
4551 if (stream
->out_fd
>= 0) {
4552 ret
= close(stream
->out_fd
);
4554 PERROR("Failed to close stream out_fd of channel \"%s\"",
4555 stream
->chan
->name
);
4557 stream
->out_fd
= -1;
4560 if (stream
->index_file
) {
4561 lttng_index_file_put(stream
->index_file
);
4562 stream
->index_file
= NULL
;
4565 if (!stream
->trace_chunk
) {
4569 ret
= consumer_stream_create_output_files(stream
, true);
4575 * Performs the stream rotation for the rotate session feature if needed.
4576 * It must be called with the channel and stream locks held.
4578 * Return 0 on success, a negative number of error.
4580 int lttng_consumer_rotate_stream(struct lttng_consumer_local_data
*ctx
,
4581 struct lttng_consumer_stream
*stream
)
4585 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4588 * Update the stream's 'current' chunk to the session's (channel)
4589 * now-current chunk.
4591 lttng_trace_chunk_put(stream
->trace_chunk
);
4592 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4594 * A channel can be rotated and not have a "next" chunk
4595 * to transition to. In that case, the channel's "current chunk"
4596 * has not been closed yet, but it has not been updated to
4597 * a "next" trace chunk either. Hence, the stream, like its
4598 * parent channel, becomes part of no chunk and can't output
4599 * anything until a new trace chunk is created.
4601 stream
->trace_chunk
= NULL
;
4602 } else if (stream
->chan
->trace_chunk
&&
4603 !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4604 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4609 * Update the stream's trace chunk to its parent channel's
4610 * current trace chunk.
4612 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4615 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4616 ret
= rotate_local_stream(ctx
, stream
);
4618 ERR("Failed to rotate stream, ret = %i", ret
);
4623 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4625 * If the stream has transitioned to a new trace
4626 * chunk, the metadata should be re-dumped to the
4629 * However, it is possible for a stream to transition to
4630 * a "no-chunk" state. This can happen if a rotation
4631 * occurs on an inactive session. In such cases, the metadata
4632 * regeneration will happen when the next trace chunk is
4635 ret
= consumer_metadata_stream_dump(stream
);
4640 lttng_consumer_reset_stream_rotate_state(stream
);
4649 * Rotate all the ready streams now.
4651 * This is especially important for low throughput streams that have already
4652 * been consumed, we cannot wait for their next packet to perform the
4654 * Need to be called with RCU read-side lock held to ensure existence of
4657 * Returns 0 on success, < 0 on error
4659 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
,
4660 uint64_t key
, struct lttng_consumer_local_data
*ctx
)
4663 struct lttng_consumer_stream
*stream
;
4664 struct lttng_ht_iter iter
;
4665 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
4669 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4671 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4672 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4673 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4674 stream
, node_channel_id
.node
) {
4675 health_code_update();
4677 pthread_mutex_lock(&stream
->chan
->lock
);
4678 pthread_mutex_lock(&stream
->lock
);
4680 if (!stream
->rotate_ready
) {
4681 pthread_mutex_unlock(&stream
->lock
);
4682 pthread_mutex_unlock(&stream
->chan
->lock
);
4685 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4687 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
4688 pthread_mutex_unlock(&stream
->lock
);
4689 pthread_mutex_unlock(&stream
->chan
->lock
);
4702 enum lttcomm_return_code
lttng_consumer_init_command(
4703 struct lttng_consumer_local_data
*ctx
,
4704 const lttng_uuid sessiond_uuid
)
4706 enum lttcomm_return_code ret
;
4707 char uuid_str
[LTTNG_UUID_STR_LEN
];
4709 if (ctx
->sessiond_uuid
.is_set
) {
4710 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4714 ctx
->sessiond_uuid
.is_set
= true;
4715 memcpy(ctx
->sessiond_uuid
.value
, sessiond_uuid
, sizeof(lttng_uuid
));
4716 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4717 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4718 DBG("Received session daemon UUID: %s", uuid_str
);
4723 enum lttcomm_return_code
lttng_consumer_create_trace_chunk(
4724 const uint64_t *relayd_id
, uint64_t session_id
,
4726 time_t chunk_creation_timestamp
,
4727 const char *chunk_override_name
,
4728 const struct lttng_credentials
*credentials
,
4729 struct lttng_directory_handle
*chunk_directory_handle
)
4732 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4733 struct lttng_trace_chunk
*created_chunk
= NULL
, *published_chunk
= NULL
;
4734 enum lttng_trace_chunk_status chunk_status
;
4735 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4736 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4737 const char *relayd_id_str
= "(none)";
4738 const char *creation_timestamp_str
;
4739 struct lttng_ht_iter iter
;
4740 struct lttng_consumer_channel
*channel
;
4743 /* Only used for logging purposes. */
4744 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4745 "%" PRIu64
, *relayd_id
);
4746 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4747 relayd_id_str
= relayd_id_buffer
;
4749 relayd_id_str
= "(formatting error)";
4753 /* Local protocol error. */
4754 assert(chunk_creation_timestamp
);
4755 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4756 creation_timestamp_buffer
,
4757 sizeof(creation_timestamp_buffer
));
4758 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
4759 "(formatting error)";
4761 DBG("Consumer create trace chunk command: relay_id = %s"
4762 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4763 ", chunk_override_name = %s"
4764 ", chunk_creation_timestamp = %s",
4765 relayd_id_str
, session_id
, chunk_id
,
4766 chunk_override_name
? : "(none)",
4767 creation_timestamp_str
);
4770 * The trace chunk registry, as used by the consumer daemon, implicitly
4771 * owns the trace chunks. This is only needed in the consumer since
4772 * the consumer has no notion of a session beyond session IDs being
4773 * used to identify other objects.
4775 * The lttng_trace_chunk_registry_publish() call below provides a
4776 * reference which is not released; it implicitly becomes the session
4777 * daemon's reference to the chunk in the consumer daemon.
4779 * The lifetime of trace chunks in the consumer daemon is managed by
4780 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4781 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4783 created_chunk
= lttng_trace_chunk_create(chunk_id
,
4784 chunk_creation_timestamp
, NULL
);
4785 if (!created_chunk
) {
4786 ERR("Failed to create trace chunk");
4787 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4791 if (chunk_override_name
) {
4792 chunk_status
= lttng_trace_chunk_override_name(created_chunk
,
4793 chunk_override_name
);
4794 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4795 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4800 if (chunk_directory_handle
) {
4801 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
,
4803 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4804 ERR("Failed to set trace chunk credentials");
4805 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4809 * The consumer daemon has no ownership of the chunk output
4812 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
,
4813 chunk_directory_handle
);
4814 chunk_directory_handle
= NULL
;
4815 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4816 ERR("Failed to set trace chunk's directory handle");
4817 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4822 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4823 the_consumer_data
.chunk_registry
, session_id
,
4825 lttng_trace_chunk_put(created_chunk
);
4826 created_chunk
= NULL
;
4827 if (!published_chunk
) {
4828 ERR("Failed to publish trace chunk");
4829 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4834 cds_lfht_for_each_entry_duplicate(
4835 the_consumer_data
.channels_by_session_id_ht
->ht
,
4836 the_consumer_data
.channels_by_session_id_ht
->hash_fct(
4837 &session_id
, lttng_ht_seed
),
4838 the_consumer_data
.channels_by_session_id_ht
->match_fct
,
4839 &session_id
, &iter
.iter
, channel
,
4840 channels_by_session_id_ht_node
.node
) {
4841 ret
= lttng_consumer_channel_set_trace_chunk(channel
,
4845 * Roll-back the creation of this chunk.
4847 * This is important since the session daemon will
4848 * assume that the creation of this chunk failed and
4849 * will never ask for it to be closed, resulting
4850 * in a leak and an inconsistent state for some
4853 enum lttcomm_return_code close_ret
;
4854 char path
[LTTNG_PATH_MAX
];
4856 DBG("Failed to set new trace chunk on existing channels, rolling back");
4857 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4858 session_id
, chunk_id
,
4859 chunk_creation_timestamp
, NULL
,
4861 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4862 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4863 session_id
, chunk_id
);
4866 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4872 struct consumer_relayd_sock_pair
*relayd
;
4874 relayd
= consumer_find_relayd(*relayd_id
);
4876 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4877 ret
= relayd_create_trace_chunk(
4878 &relayd
->control_sock
, published_chunk
);
4879 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4881 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4884 if (!relayd
|| ret
) {
4885 enum lttcomm_return_code close_ret
;
4886 char path
[LTTNG_PATH_MAX
];
4888 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4891 chunk_creation_timestamp
,
4893 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4894 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4899 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4906 /* Release the reference returned by the "publish" operation. */
4907 lttng_trace_chunk_put(published_chunk
);
4908 lttng_trace_chunk_put(created_chunk
);
4912 enum lttcomm_return_code
lttng_consumer_close_trace_chunk(
4913 const uint64_t *relayd_id
, uint64_t session_id
,
4914 uint64_t chunk_id
, time_t chunk_close_timestamp
,
4915 const enum lttng_trace_chunk_command_type
*close_command
,
4918 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4919 struct lttng_trace_chunk
*chunk
;
4920 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4921 const char *relayd_id_str
= "(none)";
4922 const char *close_command_name
= "none";
4923 struct lttng_ht_iter iter
;
4924 struct lttng_consumer_channel
*channel
;
4925 enum lttng_trace_chunk_status chunk_status
;
4930 /* Only used for logging purposes. */
4931 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4932 "%" PRIu64
, *relayd_id
);
4933 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4934 relayd_id_str
= relayd_id_buffer
;
4936 relayd_id_str
= "(formatting error)";
4939 if (close_command
) {
4940 close_command_name
= lttng_trace_chunk_command_type_get_name(
4944 DBG("Consumer close trace chunk command: relayd_id = %s"
4945 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4946 ", close command = %s",
4947 relayd_id_str
, session_id
, chunk_id
,
4948 close_command_name
);
4950 chunk
= lttng_trace_chunk_registry_find_chunk(
4951 the_consumer_data
.chunk_registry
, session_id
, chunk_id
);
4953 ERR("Failed to find chunk: session_id = %" PRIu64
4954 ", chunk_id = %" PRIu64
,
4955 session_id
, chunk_id
);
4956 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4960 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
,
4961 chunk_close_timestamp
);
4962 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4963 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4967 if (close_command
) {
4968 chunk_status
= lttng_trace_chunk_set_close_command(
4969 chunk
, *close_command
);
4970 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4971 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4977 * chunk is now invalid to access as we no longer hold a reference to
4978 * it; it is only kept around to compare it (by address) to the
4979 * current chunk found in the session's channels.
4982 cds_lfht_for_each_entry(the_consumer_data
.channel_ht
->ht
, &iter
.iter
,
4983 channel
, node
.node
) {
4987 * Only change the channel's chunk to NULL if it still
4988 * references the chunk being closed. The channel may
4989 * reference a newer channel in the case of a session
4990 * rotation. When a session rotation occurs, the "next"
4991 * chunk is created before the "current" chunk is closed.
4993 if (channel
->trace_chunk
!= chunk
) {
4996 ret
= lttng_consumer_channel_set_trace_chunk(channel
, NULL
);
4999 * Attempt to close the chunk on as many channels as
5002 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5008 struct consumer_relayd_sock_pair
*relayd
;
5010 relayd
= consumer_find_relayd(*relayd_id
);
5012 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5013 ret
= relayd_close_trace_chunk(
5014 &relayd
->control_sock
, chunk
,
5016 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5018 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
,
5022 if (!relayd
|| ret
) {
5023 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5031 * Release the reference returned by the "find" operation and
5032 * the session daemon's implicit reference to the chunk.
5034 lttng_trace_chunk_put(chunk
);
5035 lttng_trace_chunk_put(chunk
);
5040 enum lttcomm_return_code
lttng_consumer_trace_chunk_exists(
5041 const uint64_t *relayd_id
, uint64_t session_id
,
5045 enum lttcomm_return_code ret_code
;
5046 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
5047 const char *relayd_id_str
= "(none)";
5048 const bool is_local_trace
= !relayd_id
;
5049 struct consumer_relayd_sock_pair
*relayd
= NULL
;
5050 bool chunk_exists_local
, chunk_exists_remote
;
5053 /* Only used for logging purposes. */
5054 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
5055 "%" PRIu64
, *relayd_id
);
5056 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
5057 relayd_id_str
= relayd_id_buffer
;
5059 relayd_id_str
= "(formatting error)";
5063 DBG("Consumer trace chunk exists command: relayd_id = %s"
5064 ", chunk_id = %" PRIu64
, relayd_id_str
,
5066 ret
= lttng_trace_chunk_registry_chunk_exists(
5067 the_consumer_data
.chunk_registry
, session_id
, chunk_id
,
5068 &chunk_exists_local
);
5070 /* Internal error. */
5071 ERR("Failed to query the existence of a trace chunk");
5072 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
5075 DBG("Trace chunk %s locally",
5076 chunk_exists_local
? "exists" : "does not exist");
5077 if (chunk_exists_local
) {
5078 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
5080 } else if (is_local_trace
) {
5081 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5086 relayd
= consumer_find_relayd(*relayd_id
);
5088 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
5089 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5090 goto end_rcu_unlock
;
5092 DBG("Looking up existence of trace chunk on relay daemon");
5093 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5094 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
,
5095 &chunk_exists_remote
);
5096 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5098 ERR("Failed to look-up the existence of trace chunk on relay daemon");
5099 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
5100 goto end_rcu_unlock
;
5103 ret_code
= chunk_exists_remote
?
5104 LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
5105 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5106 DBG("Trace chunk %s on relay daemon",
5107 chunk_exists_remote
? "exists" : "does not exist");
5116 int consumer_clear_monitored_channel(struct lttng_consumer_channel
*channel
)
5118 struct lttng_ht
*ht
;
5119 struct lttng_consumer_stream
*stream
;
5120 struct lttng_ht_iter iter
;
5123 ht
= the_consumer_data
.stream_per_chan_id_ht
;
5126 cds_lfht_for_each_entry_duplicate(ht
->ht
,
5127 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
5128 ht
->match_fct
, &channel
->key
,
5129 &iter
.iter
, stream
, node_channel_id
.node
) {
5131 * Protect against teardown with mutex.
5133 pthread_mutex_lock(&stream
->lock
);
5134 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5137 ret
= consumer_clear_stream(stream
);
5142 pthread_mutex_unlock(&stream
->lock
);
5145 return LTTCOMM_CONSUMERD_SUCCESS
;
5148 pthread_mutex_unlock(&stream
->lock
);
5153 int lttng_consumer_clear_channel(struct lttng_consumer_channel
*channel
)
5157 DBG("Consumer clear channel %" PRIu64
, channel
->key
);
5159 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
5161 * Nothing to do for the metadata channel/stream.
5162 * Snapshot mechanism already take care of the metadata
5163 * handling/generation, and monitored channels only need to
5164 * have their data stream cleared..
5166 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5170 if (!channel
->monitor
) {
5171 ret
= consumer_clear_unmonitored_channel(channel
);
5173 ret
= consumer_clear_monitored_channel(channel
);
5179 enum lttcomm_return_code
lttng_consumer_open_channel_packets(
5180 struct lttng_consumer_channel
*channel
)
5182 struct lttng_consumer_stream
*stream
;
5183 enum lttcomm_return_code ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5185 if (channel
->metadata_stream
) {
5186 ERR("Open channel packets command attempted on a metadata channel");
5187 ret
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5192 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
5193 enum consumer_stream_open_packet_status status
;
5195 pthread_mutex_lock(&stream
->lock
);
5196 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5200 status
= consumer_stream_open_packet(stream
);
5202 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
5203 DBG("Opened a packet in \"open channel packets\" command: stream id = %" PRIu64
5204 ", channel name = %s, session id = %" PRIu64
,
5205 stream
->key
, stream
->chan
->name
,
5206 stream
->chan
->session_id
);
5207 stream
->opened_packet_in_current_trace_chunk
= true;
5209 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
5210 DBG("No space left to open a packet in \"open channel packets\" command: stream id = %" PRIu64
5211 ", channel name = %s, session id = %" PRIu64
,
5212 stream
->key
, stream
->chan
->name
,
5213 stream
->chan
->session_id
);
5215 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
5217 * Only unexpected internal errors can lead to this
5218 * failing. Report an unknown error.
5220 ERR("Failed to flush empty buffer in \"open channel packets\" command: stream id = %" PRIu64
5221 ", channel id = %" PRIu64
5222 ", channel name = %s"
5223 ", session id = %" PRIu64
,
5224 stream
->key
, channel
->key
,
5225 channel
->name
, channel
->session_id
);
5226 ret
= LTTCOMM_CONSUMERD_UNKNOWN_ERROR
;
5233 pthread_mutex_unlock(&stream
->lock
);
5242 pthread_mutex_unlock(&stream
->lock
);
5243 goto end_rcu_unlock
;