2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
10 #include "common/index/ctf-index.h"
19 #include <sys/socket.h>
20 #include <sys/types.h>
25 #include <bin/lttng-consumerd/health-consumerd.h>
26 #include <common/common.h>
27 #include <common/utils.h>
28 #include <common/time.h>
29 #include <common/compat/poll.h>
30 #include <common/compat/endian.h>
31 #include <common/index/index.h>
32 #include <common/kernel-ctl/kernel-ctl.h>
33 #include <common/sessiond-comm/relayd.h>
34 #include <common/sessiond-comm/sessiond-comm.h>
35 #include <common/kernel-consumer/kernel-consumer.h>
36 #include <common/relayd/relayd.h>
37 #include <common/ust-consumer/ust-consumer.h>
38 #include <common/consumer/consumer-timer.h>
39 #include <common/consumer/consumer.h>
40 #include <common/consumer/consumer-stream.h>
41 #include <common/consumer/consumer-testpoint.h>
42 #include <common/align.h>
43 #include <common/consumer/consumer-metadata-cache.h>
44 #include <common/trace-chunk.h>
45 #include <common/trace-chunk-registry.h>
46 #include <common/string-utils/format.h>
47 #include <common/dynamic-array.h>
49 struct lttng_consumer_global_data the_consumer_data
= {
52 .type
= LTTNG_CONSUMER_UNKNOWN
,
55 enum consumer_channel_action
{
58 CONSUMER_CHANNEL_QUIT
,
61 struct consumer_channel_msg
{
62 enum consumer_channel_action action
;
63 struct lttng_consumer_channel
*chan
; /* add */
64 uint64_t key
; /* del */
67 /* Flag used to temporarily pause data consumption from testpoints. */
68 int data_consumption_paused
;
71 * Flag to inform the polling thread to quit when all fd hung up. Updated by
72 * the consumer_thread_receive_fds when it notices that all fds has hung up.
73 * Also updated by the signal handler (consumer_should_exit()). Read by the
79 * Global hash table containing respectively metadata and data streams. The
80 * stream element in this ht should only be updated by the metadata poll thread
81 * for the metadata and the data poll thread for the data.
83 static struct lttng_ht
*metadata_ht
;
84 static struct lttng_ht
*data_ht
;
86 static const char *get_consumer_domain(void)
88 switch (the_consumer_data
.type
) {
89 case LTTNG_CONSUMER_KERNEL
:
90 return DEFAULT_KERNEL_TRACE_DIR
;
91 case LTTNG_CONSUMER64_UST
:
93 case LTTNG_CONSUMER32_UST
:
94 return DEFAULT_UST_TRACE_DIR
;
101 * Notify a thread lttng pipe to poll back again. This usually means that some
102 * global state has changed so we just send back the thread in a poll wait
105 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
107 struct lttng_consumer_stream
*null_stream
= NULL
;
111 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
114 static void notify_health_quit_pipe(int *pipe
)
118 ret
= lttng_write(pipe
[1], "4", 1);
120 PERROR("write consumer health quit");
124 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
125 struct lttng_consumer_channel
*chan
,
127 enum consumer_channel_action action
)
129 struct consumer_channel_msg msg
;
132 memset(&msg
, 0, sizeof(msg
));
137 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
138 if (ret
< sizeof(msg
)) {
139 PERROR("notify_channel_pipe write error");
143 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
146 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
149 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
150 struct lttng_consumer_channel
**chan
,
152 enum consumer_channel_action
*action
)
154 struct consumer_channel_msg msg
;
157 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
158 if (ret
< sizeof(msg
)) {
162 *action
= msg
.action
;
170 * Cleanup the stream list of a channel. Those streams are not yet globally
173 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
175 struct lttng_consumer_stream
*stream
, *stmp
;
179 /* Delete streams that might have been left in the stream list. */
180 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
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 lttng_wait_queue_wake_all(&stream
->chan
->metadata_pushed_wait_queue
);
470 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
474 /* Follow up by the data streams */
475 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
476 if (stream
->net_seq_idx
== net_seq_idx
) {
477 uatomic_set(&stream
->endpoint_status
, status
);
478 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
485 * Cleanup a relayd object by flagging every associated streams for deletion,
486 * destroying the object meaning removing it from the relayd hash table,
487 * closing the sockets and freeing the memory in a RCU call.
489 * If a local data context is available, notify the threads that the streams'
490 * state have changed.
492 void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
)
498 DBG("Cleaning up relayd object ID %"PRIu64
, relayd
->net_seq_idx
);
500 /* Save the net sequence index before destroying the object */
501 netidx
= relayd
->net_seq_idx
;
504 * Delete the relayd from the relayd hash table, close the sockets and free
505 * the object in a RCU call.
507 consumer_destroy_relayd(relayd
);
509 /* Set inactive endpoint to all streams */
510 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
513 * With a local data context, notify the threads that the streams' state
514 * have changed. The write() action on the pipe acts as an "implicit"
515 * memory barrier ordering the updates of the end point status from the
516 * read of this status which happens AFTER receiving this notify.
518 notify_thread_lttng_pipe(relayd
->ctx
->consumer_data_pipe
);
519 notify_thread_lttng_pipe(relayd
->ctx
->consumer_metadata_pipe
);
523 * Flag a relayd socket pair for destruction. Destroy it if the refcount
526 * RCU read side lock MUST be aquired before calling this function.
528 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
532 /* Set destroy flag for this object */
533 uatomic_set(&relayd
->destroy_flag
, 1);
535 /* Destroy the relayd if refcount is 0 */
536 if (uatomic_read(&relayd
->refcount
) == 0) {
537 consumer_destroy_relayd(relayd
);
542 * Completly destroy stream from every visiable data structure and the given
545 * One this call returns, the stream object is not longer usable nor visible.
547 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
550 consumer_stream_destroy(stream
, ht
);
554 * XXX naming of del vs destroy is all mixed up.
556 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
558 consumer_stream_destroy(stream
, data_ht
);
561 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
563 consumer_stream_destroy(stream
, metadata_ht
);
566 void consumer_stream_update_channel_attributes(
567 struct lttng_consumer_stream
*stream
,
568 struct lttng_consumer_channel
*channel
)
570 stream
->channel_read_only_attributes
.tracefile_size
=
571 channel
->tracefile_size
;
575 * Add a stream to the global list protected by a mutex.
577 void consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
579 struct lttng_ht
*ht
= data_ht
;
584 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
586 pthread_mutex_lock(&the_consumer_data
.lock
);
587 pthread_mutex_lock(&stream
->chan
->lock
);
588 pthread_mutex_lock(&stream
->chan
->timer_lock
);
589 pthread_mutex_lock(&stream
->lock
);
592 /* Steal stream identifier to avoid having streams with the same key */
593 steal_stream_key(stream
->key
, ht
);
595 lttng_ht_add_unique_u64(ht
, &stream
->node
);
597 lttng_ht_add_u64(the_consumer_data
.stream_per_chan_id_ht
,
598 &stream
->node_channel_id
);
601 * Add stream to the stream_list_ht of the consumer data. No need to steal
602 * the key since the HT does not use it and we allow to add redundant keys
605 lttng_ht_add_u64(the_consumer_data
.stream_list_ht
,
606 &stream
->node_session_id
);
609 * When nb_init_stream_left reaches 0, we don't need to trigger any action
610 * in terms of destroying the associated channel, because the action that
611 * causes the count to become 0 also causes a stream to be added. The
612 * channel deletion will thus be triggered by the following removal of this
615 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
616 /* Increment refcount before decrementing nb_init_stream_left */
618 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
621 /* Update consumer data once the node is inserted. */
622 the_consumer_data
.stream_count
++;
623 the_consumer_data
.need_update
= 1;
626 pthread_mutex_unlock(&stream
->lock
);
627 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
628 pthread_mutex_unlock(&stream
->chan
->lock
);
629 pthread_mutex_unlock(&the_consumer_data
.lock
);
633 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
634 * be acquired before calling this.
636 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
639 struct lttng_ht_node_u64
*node
;
640 struct lttng_ht_iter iter
;
644 lttng_ht_lookup(the_consumer_data
.relayd_ht
, &relayd
->net_seq_idx
,
646 node
= lttng_ht_iter_get_node_u64(&iter
);
650 lttng_ht_add_unique_u64(the_consumer_data
.relayd_ht
, &relayd
->node
);
657 * Allocate and return a consumer relayd socket.
659 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
660 uint64_t net_seq_idx
)
662 struct consumer_relayd_sock_pair
*obj
= NULL
;
664 /* net sequence index of -1 is a failure */
665 if (net_seq_idx
== (uint64_t) -1ULL) {
669 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
671 PERROR("zmalloc relayd sock");
675 obj
->net_seq_idx
= net_seq_idx
;
677 obj
->destroy_flag
= 0;
678 obj
->control_sock
.sock
.fd
= -1;
679 obj
->data_sock
.sock
.fd
= -1;
680 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
681 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
688 * Find a relayd socket pair in the global consumer data.
690 * Return the object if found else NULL.
691 * RCU read-side lock must be held across this call and while using the
694 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
696 struct lttng_ht_iter iter
;
697 struct lttng_ht_node_u64
*node
;
698 struct consumer_relayd_sock_pair
*relayd
= NULL
;
700 /* Negative keys are lookup failures */
701 if (key
== (uint64_t) -1ULL) {
705 lttng_ht_lookup(the_consumer_data
.relayd_ht
, &key
, &iter
);
706 node
= lttng_ht_iter_get_node_u64(&iter
);
708 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
716 * Find a relayd and send the stream
718 * Returns 0 on success, < 0 on error
720 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
724 struct consumer_relayd_sock_pair
*relayd
;
727 assert(stream
->net_seq_idx
!= -1ULL);
730 /* The stream is not metadata. Get relayd reference if exists. */
732 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
733 if (relayd
!= NULL
) {
734 /* Add stream on the relayd */
735 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
736 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
737 get_consumer_domain(), path
, &stream
->relayd_stream_id
,
738 stream
->chan
->tracefile_size
,
739 stream
->chan
->tracefile_count
,
740 stream
->trace_chunk
);
741 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
743 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
744 lttng_consumer_cleanup_relayd(relayd
);
748 uatomic_inc(&relayd
->refcount
);
749 stream
->sent_to_relayd
= 1;
751 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
752 stream
->key
, stream
->net_seq_idx
);
757 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
758 stream
->name
, stream
->key
, stream
->net_seq_idx
);
766 * Find a relayd and send the streams sent message
768 * Returns 0 on success, < 0 on error
770 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
773 struct consumer_relayd_sock_pair
*relayd
;
775 assert(net_seq_idx
!= -1ULL);
777 /* The stream is not metadata. Get relayd reference if exists. */
779 relayd
= consumer_find_relayd(net_seq_idx
);
780 if (relayd
!= NULL
) {
781 /* Add stream on the relayd */
782 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
783 ret
= relayd_streams_sent(&relayd
->control_sock
);
784 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
786 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
787 lttng_consumer_cleanup_relayd(relayd
);
791 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
798 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
806 * Find a relayd and close the stream
808 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
810 struct consumer_relayd_sock_pair
*relayd
;
812 /* The stream is not metadata. Get relayd reference if exists. */
814 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
816 consumer_stream_relayd_close(stream
, relayd
);
822 * Handle stream for relayd transmission if the stream applies for network
823 * streaming where the net sequence index is set.
825 * Return destination file descriptor or negative value on error.
827 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
828 size_t data_size
, unsigned long padding
,
829 struct consumer_relayd_sock_pair
*relayd
)
832 struct lttcomm_relayd_data_hdr data_hdr
;
838 /* Reset data header */
839 memset(&data_hdr
, 0, sizeof(data_hdr
));
841 if (stream
->metadata_flag
) {
842 /* Caller MUST acquire the relayd control socket lock */
843 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
848 /* Metadata are always sent on the control socket. */
849 outfd
= relayd
->control_sock
.sock
.fd
;
851 /* Set header with stream information */
852 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
853 data_hdr
.data_size
= htobe32(data_size
);
854 data_hdr
.padding_size
= htobe32(padding
);
857 * Note that net_seq_num below is assigned with the *current* value of
858 * next_net_seq_num and only after that the next_net_seq_num will be
859 * increment. This is why when issuing a command on the relayd using
860 * this next value, 1 should always be substracted in order to compare
861 * the last seen sequence number on the relayd side to the last sent.
863 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
864 /* Other fields are zeroed previously */
866 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
872 ++stream
->next_net_seq_num
;
874 /* Set to go on data socket */
875 outfd
= relayd
->data_sock
.sock
.fd
;
883 * Write a character on the metadata poll pipe to wake the metadata thread.
884 * Returns 0 on success, -1 on error.
886 int consumer_metadata_wakeup_pipe(const struct lttng_consumer_channel
*channel
)
890 DBG("Waking up metadata poll thread (writing to pipe): channel name = '%s'",
892 if (channel
->monitor
&& channel
->metadata_stream
) {
893 const char dummy
= 'c';
894 const ssize_t write_ret
= lttng_write(
895 channel
->metadata_stream
->ust_metadata_poll_pipe
[1],
899 if (errno
== EWOULDBLOCK
) {
901 * This is fine, the metadata poll thread
902 * is having a hard time keeping-up, but
903 * it will eventually wake-up and consume
904 * the available data.
908 PERROR("Failed to write to UST metadata pipe while attempting to wake-up the metadata poll thread");
920 * Trigger a dump of the metadata content. Following/during the succesful
921 * completion of this call, the metadata poll thread will start receiving
922 * metadata packets to consume.
924 * The caller must hold the channel and stream locks.
927 int consumer_metadata_stream_dump(struct lttng_consumer_stream
*stream
)
931 ASSERT_LOCKED(stream
->chan
->lock
);
932 ASSERT_LOCKED(stream
->lock
);
933 assert(stream
->metadata_flag
);
934 assert(stream
->chan
->trace_chunk
);
936 switch (the_consumer_data
.type
) {
937 case LTTNG_CONSUMER_KERNEL
:
939 * Reset the position of what has been read from the
940 * metadata cache to 0 so we can dump it again.
942 ret
= kernctl_metadata_cache_dump(stream
->wait_fd
);
944 case LTTNG_CONSUMER32_UST
:
945 case LTTNG_CONSUMER64_UST
:
947 * Reset the position pushed from the metadata cache so it
948 * will write from the beginning on the next push.
950 stream
->ust_metadata_pushed
= 0;
951 ret
= consumer_metadata_wakeup_pipe(stream
->chan
);
954 ERR("Unknown consumer_data type");
958 ERR("Failed to dump the metadata cache");
964 int lttng_consumer_channel_set_trace_chunk(
965 struct lttng_consumer_channel
*channel
,
966 struct lttng_trace_chunk
*new_trace_chunk
)
968 pthread_mutex_lock(&channel
->lock
);
969 if (channel
->is_deleted
) {
971 * The channel has been logically deleted and should no longer
972 * be used. It has released its reference to its current trace
973 * chunk and should not acquire a new one.
975 * Return success as there is nothing for the caller to do.
981 * The acquisition of the reference cannot fail (barring
982 * a severe internal error) since a reference to the published
983 * chunk is already held by the caller.
985 if (new_trace_chunk
) {
986 const bool acquired_reference
= lttng_trace_chunk_get(
989 assert(acquired_reference
);
992 lttng_trace_chunk_put(channel
->trace_chunk
);
993 channel
->trace_chunk
= new_trace_chunk
;
995 pthread_mutex_unlock(&channel
->lock
);
1000 * Allocate and return a new lttng_consumer_channel object using the given key
1001 * to initialize the hash table node.
1003 * On error, return NULL.
1005 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
1006 uint64_t session_id
,
1007 const uint64_t *chunk_id
,
1008 const char *pathname
,
1011 enum lttng_event_output output
,
1012 uint64_t tracefile_size
,
1013 uint64_t tracefile_count
,
1014 uint64_t session_id_per_pid
,
1015 unsigned int monitor
,
1016 unsigned int live_timer_interval
,
1017 bool is_in_live_session
,
1018 const char *root_shm_path
,
1019 const char *shm_path
)
1021 struct lttng_consumer_channel
*channel
= NULL
;
1022 struct lttng_trace_chunk
*trace_chunk
= NULL
;
1025 trace_chunk
= lttng_trace_chunk_registry_find_chunk(
1026 the_consumer_data
.chunk_registry
, session_id
,
1029 ERR("Failed to find trace chunk reference during creation of channel");
1034 channel
= zmalloc(sizeof(*channel
));
1035 if (channel
== NULL
) {
1036 PERROR("malloc struct lttng_consumer_channel");
1041 channel
->refcount
= 0;
1042 channel
->session_id
= session_id
;
1043 channel
->session_id_per_pid
= session_id_per_pid
;
1044 channel
->relayd_id
= relayd_id
;
1045 channel
->tracefile_size
= tracefile_size
;
1046 channel
->tracefile_count
= tracefile_count
;
1047 channel
->monitor
= monitor
;
1048 channel
->live_timer_interval
= live_timer_interval
;
1049 channel
->is_live
= is_in_live_session
;
1050 pthread_mutex_init(&channel
->lock
, NULL
);
1051 pthread_mutex_init(&channel
->timer_lock
, NULL
);
1052 lttng_wait_queue_init(&channel
->metadata_pushed_wait_queue
);
1055 case LTTNG_EVENT_SPLICE
:
1056 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
1058 case LTTNG_EVENT_MMAP
:
1059 channel
->output
= CONSUMER_CHANNEL_MMAP
;
1069 * In monitor mode, the streams associated with the channel will be put in
1070 * a special list ONLY owned by this channel. So, the refcount is set to 1
1071 * here meaning that the channel itself has streams that are referenced.
1073 * On a channel deletion, once the channel is no longer visible, the
1074 * refcount is decremented and checked for a zero value to delete it. With
1075 * streams in no monitor mode, it will now be safe to destroy the channel.
1077 if (!channel
->monitor
) {
1078 channel
->refcount
= 1;
1081 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1082 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1084 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1085 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1087 if (root_shm_path
) {
1088 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1089 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1092 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1093 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1096 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1097 lttng_ht_node_init_u64(&channel
->channels_by_session_id_ht_node
,
1098 channel
->session_id
);
1100 channel
->wait_fd
= -1;
1101 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1104 int ret
= lttng_consumer_channel_set_trace_chunk(channel
,
1111 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1114 lttng_trace_chunk_put(trace_chunk
);
1117 consumer_del_channel(channel
);
1123 * Add a channel to the global list protected by a mutex.
1125 * Always return 0 indicating success.
1127 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1128 struct lttng_consumer_local_data
*ctx
)
1130 pthread_mutex_lock(&the_consumer_data
.lock
);
1131 pthread_mutex_lock(&channel
->lock
);
1132 pthread_mutex_lock(&channel
->timer_lock
);
1135 * This gives us a guarantee that the channel we are about to add to the
1136 * channel hash table will be unique. See this function comment on the why
1137 * we need to steel the channel key at this stage.
1139 steal_channel_key(channel
->key
);
1142 lttng_ht_add_unique_u64(the_consumer_data
.channel_ht
, &channel
->node
);
1143 lttng_ht_add_u64(the_consumer_data
.channels_by_session_id_ht
,
1144 &channel
->channels_by_session_id_ht_node
);
1146 channel
->is_published
= true;
1148 pthread_mutex_unlock(&channel
->timer_lock
);
1149 pthread_mutex_unlock(&channel
->lock
);
1150 pthread_mutex_unlock(&the_consumer_data
.lock
);
1152 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1153 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1160 * Allocate the pollfd structure and the local view of the out fds to avoid
1161 * doing a lookup in the linked list and concurrency issues when writing is
1162 * needed. Called with consumer_data.lock held.
1164 * Returns the number of fds in the structures.
1166 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1167 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1168 struct lttng_ht
*ht
, int *nb_inactive_fd
)
1171 struct lttng_ht_iter iter
;
1172 struct lttng_consumer_stream
*stream
;
1177 assert(local_stream
);
1179 DBG("Updating poll fd array");
1180 *nb_inactive_fd
= 0;
1182 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1184 * Only active streams with an active end point can be added to the
1185 * poll set and local stream storage of the thread.
1187 * There is a potential race here for endpoint_status to be updated
1188 * just after the check. However, this is OK since the stream(s) will
1189 * be deleted once the thread is notified that the end point state has
1190 * changed where this function will be called back again.
1192 * We track the number of inactive FDs because they still need to be
1193 * closed by the polling thread after a wakeup on the data_pipe or
1196 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1197 (*nb_inactive_fd
)++;
1201 * This clobbers way too much the debug output. Uncomment that if you
1202 * need it for debugging purposes.
1204 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1205 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1206 local_stream
[i
] = stream
;
1212 * Insert the consumer_data_pipe at the end of the array and don't
1213 * increment i so nb_fd is the number of real FD.
1215 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1216 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1218 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1219 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1224 * Poll on the should_quit pipe and the command socket return -1 on
1225 * error, 1 if should exit, 0 if data is available on the command socket
1227 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1232 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1233 if (num_rdy
== -1) {
1235 * Restart interrupted system call.
1237 if (errno
== EINTR
) {
1240 PERROR("Poll error");
1243 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1244 DBG("consumer_should_quit wake up");
1251 * Set the error socket.
1253 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1256 ctx
->consumer_error_socket
= sock
;
1260 * Set the command socket path.
1262 void lttng_consumer_set_command_sock_path(
1263 struct lttng_consumer_local_data
*ctx
, char *sock
)
1265 ctx
->consumer_command_sock_path
= sock
;
1269 * Send return code to the session daemon.
1270 * If the socket is not defined, we return 0, it is not a fatal error
1272 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
,
1273 enum lttcomm_return_code error_code
)
1275 if (ctx
->consumer_error_socket
> 0) {
1276 const int32_t comm_code
= (int32_t) error_code
;
1278 return lttcomm_send_unix_sock(
1279 ctx
->consumer_error_socket
, &comm_code
, sizeof(comm_code
));
1286 * Close all the tracefiles and stream fds and MUST be called when all
1287 * instances are destroyed i.e. when all threads were joined and are ended.
1289 void lttng_consumer_cleanup(void)
1291 struct lttng_ht_iter iter
;
1292 struct lttng_consumer_channel
*channel
;
1293 unsigned int trace_chunks_left
;
1297 cds_lfht_for_each_entry(the_consumer_data
.channel_ht
->ht
, &iter
.iter
,
1298 channel
, node
.node
) {
1299 consumer_del_channel(channel
);
1304 lttng_ht_destroy(the_consumer_data
.channel_ht
);
1305 lttng_ht_destroy(the_consumer_data
.channels_by_session_id_ht
);
1307 cleanup_relayd_ht();
1309 lttng_ht_destroy(the_consumer_data
.stream_per_chan_id_ht
);
1312 * This HT contains streams that are freed by either the metadata thread or
1313 * the data thread so we do *nothing* on the hash table and simply destroy
1316 lttng_ht_destroy(the_consumer_data
.stream_list_ht
);
1319 * Trace chunks in the registry may still exist if the session
1320 * daemon has encountered an internal error and could not
1321 * tear down its sessions and/or trace chunks properly.
1323 * Release the session daemon's implicit reference to any remaining
1324 * trace chunk and print an error if any trace chunk was found. Note
1325 * that there are _no_ legitimate cases for trace chunks to be left,
1326 * it is a leak. However, it can happen following a crash of the
1327 * session daemon and not emptying the registry would cause an assertion
1330 trace_chunks_left
= lttng_trace_chunk_registry_put_each_chunk(
1331 the_consumer_data
.chunk_registry
);
1332 if (trace_chunks_left
) {
1333 ERR("%u trace chunks are leaked by lttng-consumerd. "
1334 "This can be caused by an internal error of the session daemon.",
1337 /* Run all callbacks freeing each chunk. */
1339 lttng_trace_chunk_registry_destroy(the_consumer_data
.chunk_registry
);
1343 * Called from signal handler.
1345 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1349 CMM_STORE_SHARED(consumer_quit
, 1);
1350 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1352 PERROR("write consumer quit");
1355 DBG("Consumer flag that it should quit");
1360 * Flush pending writes to trace output disk file.
1363 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1367 int outfd
= stream
->out_fd
;
1370 * This does a blocking write-and-wait on any page that belongs to the
1371 * subbuffer prior to the one we just wrote.
1372 * Don't care about error values, as these are just hints and ways to
1373 * limit the amount of page cache used.
1375 if (orig_offset
< stream
->max_sb_size
) {
1378 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1379 stream
->max_sb_size
,
1380 SYNC_FILE_RANGE_WAIT_BEFORE
1381 | SYNC_FILE_RANGE_WRITE
1382 | SYNC_FILE_RANGE_WAIT_AFTER
);
1384 * Give hints to the kernel about how we access the file:
1385 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1388 * We need to call fadvise again after the file grows because the
1389 * kernel does not seem to apply fadvise to non-existing parts of the
1392 * Call fadvise _after_ having waited for the page writeback to
1393 * complete because the dirty page writeback semantic is not well
1394 * defined. So it can be expected to lead to lower throughput in
1397 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1398 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1399 if (ret
&& ret
!= -ENOSYS
) {
1401 PERROR("posix_fadvise on fd %i", outfd
);
1406 * Initialise the necessary environnement :
1407 * - create a new context
1408 * - create the poll_pipe
1409 * - create the should_quit pipe (for signal handler)
1410 * - create the thread pipe (for splice)
1412 * Takes a function pointer as argument, this function is called when data is
1413 * available on a buffer. This function is responsible to do the
1414 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1415 * buffer configuration and then kernctl_put_next_subbuf at the end.
1417 * Returns a pointer to the new context or NULL on error.
1419 struct lttng_consumer_local_data
*lttng_consumer_create(
1420 enum lttng_consumer_type type
,
1421 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1422 struct lttng_consumer_local_data
*ctx
, bool locked_by_caller
),
1423 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1424 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1425 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1428 struct lttng_consumer_local_data
*ctx
;
1430 assert(the_consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1431 the_consumer_data
.type
== type
);
1432 the_consumer_data
.type
= type
;
1434 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1436 PERROR("allocating context");
1440 ctx
->consumer_error_socket
= -1;
1441 ctx
->consumer_metadata_socket
= -1;
1442 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1443 /* assign the callbacks */
1444 ctx
->on_buffer_ready
= buffer_ready
;
1445 ctx
->on_recv_channel
= recv_channel
;
1446 ctx
->on_recv_stream
= recv_stream
;
1447 ctx
->on_update_stream
= update_stream
;
1449 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1450 if (!ctx
->consumer_data_pipe
) {
1451 goto error_poll_pipe
;
1454 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1455 if (!ctx
->consumer_wakeup_pipe
) {
1456 goto error_wakeup_pipe
;
1459 ret
= pipe(ctx
->consumer_should_quit
);
1461 PERROR("Error creating recv pipe");
1462 goto error_quit_pipe
;
1465 ret
= pipe(ctx
->consumer_channel_pipe
);
1467 PERROR("Error creating channel pipe");
1468 goto error_channel_pipe
;
1471 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1472 if (!ctx
->consumer_metadata_pipe
) {
1473 goto error_metadata_pipe
;
1476 ctx
->channel_monitor_pipe
= -1;
1480 error_metadata_pipe
:
1481 utils_close_pipe(ctx
->consumer_channel_pipe
);
1483 utils_close_pipe(ctx
->consumer_should_quit
);
1485 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1487 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1495 * Iterate over all streams of the hashtable and free them properly.
1497 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1499 struct lttng_ht_iter iter
;
1500 struct lttng_consumer_stream
*stream
;
1507 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1509 * Ignore return value since we are currently cleaning up so any error
1512 (void) consumer_del_stream(stream
, ht
);
1516 lttng_ht_destroy(ht
);
1520 * Iterate over all streams of the metadata hashtable and free them
1523 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1525 struct lttng_ht_iter iter
;
1526 struct lttng_consumer_stream
*stream
;
1533 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1535 * Ignore return value since we are currently cleaning up so any error
1538 (void) consumer_del_metadata_stream(stream
, ht
);
1542 lttng_ht_destroy(ht
);
1546 * Close all fds associated with the instance and free the context.
1548 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1552 DBG("Consumer destroying it. Closing everything.");
1558 destroy_data_stream_ht(data_ht
);
1559 destroy_metadata_stream_ht(metadata_ht
);
1561 ret
= close(ctx
->consumer_error_socket
);
1565 ret
= close(ctx
->consumer_metadata_socket
);
1569 utils_close_pipe(ctx
->consumer_channel_pipe
);
1570 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1571 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1572 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1573 utils_close_pipe(ctx
->consumer_should_quit
);
1575 unlink(ctx
->consumer_command_sock_path
);
1580 * Write the metadata stream id on the specified file descriptor.
1582 static int write_relayd_metadata_id(int fd
,
1583 struct lttng_consumer_stream
*stream
,
1584 unsigned long padding
)
1587 struct lttcomm_relayd_metadata_payload hdr
;
1589 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1590 hdr
.padding_size
= htobe32(padding
);
1591 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1592 if (ret
< sizeof(hdr
)) {
1594 * This error means that the fd's end is closed so ignore the PERROR
1595 * not to clubber the error output since this can happen in a normal
1598 if (errno
!= EPIPE
) {
1599 PERROR("write metadata stream id");
1601 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1603 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1604 * handle writting the missing part so report that as an error and
1605 * don't lie to the caller.
1610 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1611 stream
->relayd_stream_id
, padding
);
1618 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1619 * core function for writing trace buffers to either the local filesystem or
1622 * It must be called with the stream and the channel lock held.
1624 * Careful review MUST be put if any changes occur!
1626 * Returns the number of bytes written
1628 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1629 struct lttng_consumer_stream
*stream
,
1630 const struct lttng_buffer_view
*buffer
,
1631 unsigned long padding
)
1634 off_t orig_offset
= stream
->out_fd_offset
;
1635 /* Default is on the disk */
1636 int outfd
= stream
->out_fd
;
1637 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1638 unsigned int relayd_hang_up
= 0;
1639 const size_t subbuf_content_size
= buffer
->size
- padding
;
1642 /* RCU lock for the relayd pointer */
1644 assert(stream
->net_seq_idx
!= (uint64_t) -1ULL ||
1645 stream
->trace_chunk
);
1647 /* Flag that the current stream if set for network streaming. */
1648 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1649 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1650 if (relayd
== NULL
) {
1656 /* Handle stream on the relayd if the output is on the network */
1658 unsigned long netlen
= subbuf_content_size
;
1661 * Lock the control socket for the complete duration of the function
1662 * since from this point on we will use the socket.
1664 if (stream
->metadata_flag
) {
1665 /* Metadata requires the control socket. */
1666 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1667 if (stream
->reset_metadata_flag
) {
1668 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1669 stream
->relayd_stream_id
,
1670 stream
->metadata_version
);
1675 stream
->reset_metadata_flag
= 0;
1677 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1680 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1685 /* Use the returned socket. */
1688 /* Write metadata stream id before payload */
1689 if (stream
->metadata_flag
) {
1690 ret
= write_relayd_metadata_id(outfd
, stream
, padding
);
1697 write_len
= subbuf_content_size
;
1699 /* No streaming; we have to write the full padding. */
1700 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1701 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1703 ERR("Reset metadata file");
1706 stream
->reset_metadata_flag
= 0;
1710 * Check if we need to change the tracefile before writing the packet.
1712 if (stream
->chan
->tracefile_size
> 0 &&
1713 (stream
->tracefile_size_current
+ buffer
->size
) >
1714 stream
->chan
->tracefile_size
) {
1715 ret
= consumer_stream_rotate_output_files(stream
);
1719 outfd
= stream
->out_fd
;
1722 stream
->tracefile_size_current
+= buffer
->size
;
1723 write_len
= buffer
->size
;
1727 * This call guarantee that len or less is returned. It's impossible to
1728 * receive a ret value that is bigger than len.
1730 ret
= lttng_write(outfd
, buffer
->data
, write_len
);
1731 DBG("Consumer mmap write() ret %zd (len %zu)", ret
, write_len
);
1732 if (ret
< 0 || ((size_t) ret
!= write_len
)) {
1734 * Report error to caller if nothing was written else at least send the
1742 /* Socket operation failed. We consider the relayd dead */
1743 if (errno
== EPIPE
) {
1745 * This is possible if the fd is closed on the other side
1746 * (outfd) or any write problem. It can be verbose a bit for a
1747 * normal execution if for instance the relayd is stopped
1748 * abruptly. This can happen so set this to a DBG statement.
1750 DBG("Consumer mmap write detected relayd hang up");
1752 /* Unhandled error, print it and stop function right now. */
1753 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret
,
1758 stream
->output_written
+= ret
;
1760 /* This call is useless on a socket so better save a syscall. */
1762 /* This won't block, but will start writeout asynchronously */
1763 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, write_len
,
1764 SYNC_FILE_RANGE_WRITE
);
1765 stream
->out_fd_offset
+= write_len
;
1766 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1771 * This is a special case that the relayd has closed its socket. Let's
1772 * cleanup the relayd object and all associated streams.
1774 if (relayd
&& relayd_hang_up
) {
1775 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1776 lttng_consumer_cleanup_relayd(relayd
);
1780 /* Unlock only if ctrl socket used */
1781 if (relayd
&& stream
->metadata_flag
) {
1782 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1790 * Splice the data from the ring buffer to the tracefile.
1792 * It must be called with the stream lock held.
1794 * Returns the number of bytes spliced.
1796 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1797 struct lttng_consumer_local_data
*ctx
,
1798 struct lttng_consumer_stream
*stream
, unsigned long len
,
1799 unsigned long padding
)
1801 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1803 off_t orig_offset
= stream
->out_fd_offset
;
1804 int fd
= stream
->wait_fd
;
1805 /* Default is on the disk */
1806 int outfd
= stream
->out_fd
;
1807 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1809 unsigned int relayd_hang_up
= 0;
1811 switch (the_consumer_data
.type
) {
1812 case LTTNG_CONSUMER_KERNEL
:
1814 case LTTNG_CONSUMER32_UST
:
1815 case LTTNG_CONSUMER64_UST
:
1816 /* Not supported for user space tracing */
1819 ERR("Unknown consumer_data type");
1823 /* RCU lock for the relayd pointer */
1826 /* Flag that the current stream if set for network streaming. */
1827 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1828 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1829 if (relayd
== NULL
) {
1834 splice_pipe
= stream
->splice_pipe
;
1836 /* Write metadata stream id before payload */
1838 unsigned long total_len
= len
;
1840 if (stream
->metadata_flag
) {
1842 * Lock the control socket for the complete duration of the function
1843 * since from this point on we will use the socket.
1845 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1847 if (stream
->reset_metadata_flag
) {
1848 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1849 stream
->relayd_stream_id
,
1850 stream
->metadata_version
);
1855 stream
->reset_metadata_flag
= 0;
1857 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
,
1865 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1868 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1874 /* Use the returned socket. */
1877 /* No streaming, we have to set the len with the full padding */
1880 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1881 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1883 ERR("Reset metadata file");
1886 stream
->reset_metadata_flag
= 0;
1889 * Check if we need to change the tracefile before writing the packet.
1891 if (stream
->chan
->tracefile_size
> 0 &&
1892 (stream
->tracefile_size_current
+ len
) >
1893 stream
->chan
->tracefile_size
) {
1894 ret
= consumer_stream_rotate_output_files(stream
);
1899 outfd
= stream
->out_fd
;
1902 stream
->tracefile_size_current
+= len
;
1906 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1907 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1908 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1909 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1910 DBG("splice chan to pipe, ret %zd", ret_splice
);
1911 if (ret_splice
< 0) {
1914 PERROR("Error in relay splice");
1918 /* Handle stream on the relayd if the output is on the network */
1919 if (relayd
&& stream
->metadata_flag
) {
1920 size_t metadata_payload_size
=
1921 sizeof(struct lttcomm_relayd_metadata_payload
);
1923 /* Update counter to fit the spliced data */
1924 ret_splice
+= metadata_payload_size
;
1925 len
+= metadata_payload_size
;
1927 * We do this so the return value can match the len passed as
1928 * argument to this function.
1930 written
-= metadata_payload_size
;
1933 /* Splice data out */
1934 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1935 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1936 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1938 if (ret_splice
< 0) {
1943 } else if (ret_splice
> len
) {
1945 * We don't expect this code path to be executed but you never know
1946 * so this is an extra protection agains a buggy splice().
1949 written
+= ret_splice
;
1950 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1954 /* All good, update current len and continue. */
1958 /* This call is useless on a socket so better save a syscall. */
1960 /* This won't block, but will start writeout asynchronously */
1961 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1962 SYNC_FILE_RANGE_WRITE
);
1963 stream
->out_fd_offset
+= ret_splice
;
1965 stream
->output_written
+= ret_splice
;
1966 written
+= ret_splice
;
1969 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1975 * This is a special case that the relayd has closed its socket. Let's
1976 * cleanup the relayd object and all associated streams.
1978 if (relayd
&& relayd_hang_up
) {
1979 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1980 lttng_consumer_cleanup_relayd(relayd
);
1981 /* Skip splice error so the consumer does not fail */
1986 /* send the appropriate error description to sessiond */
1989 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1992 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1995 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
2000 if (relayd
&& stream
->metadata_flag
) {
2001 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2009 * Sample the snapshot positions for a specific fd
2011 * Returns 0 on success, < 0 on error
2013 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
2015 switch (the_consumer_data
.type
) {
2016 case LTTNG_CONSUMER_KERNEL
:
2017 return lttng_kconsumer_sample_snapshot_positions(stream
);
2018 case LTTNG_CONSUMER32_UST
:
2019 case LTTNG_CONSUMER64_UST
:
2020 return lttng_ustconsumer_sample_snapshot_positions(stream
);
2022 ERR("Unknown consumer_data type");
2028 * Take a snapshot for a specific fd
2030 * Returns 0 on success, < 0 on error
2032 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2034 switch (the_consumer_data
.type
) {
2035 case LTTNG_CONSUMER_KERNEL
:
2036 return lttng_kconsumer_take_snapshot(stream
);
2037 case LTTNG_CONSUMER32_UST
:
2038 case LTTNG_CONSUMER64_UST
:
2039 return lttng_ustconsumer_take_snapshot(stream
);
2041 ERR("Unknown consumer_data type");
2048 * Get the produced position
2050 * Returns 0 on success, < 0 on error
2052 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
2055 switch (the_consumer_data
.type
) {
2056 case LTTNG_CONSUMER_KERNEL
:
2057 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
2058 case LTTNG_CONSUMER32_UST
:
2059 case LTTNG_CONSUMER64_UST
:
2060 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2062 ERR("Unknown consumer_data type");
2069 * Get the consumed position (free-running counter position in bytes).
2071 * Returns 0 on success, < 0 on error
2073 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
2076 switch (the_consumer_data
.type
) {
2077 case LTTNG_CONSUMER_KERNEL
:
2078 return lttng_kconsumer_get_consumed_snapshot(stream
, pos
);
2079 case LTTNG_CONSUMER32_UST
:
2080 case LTTNG_CONSUMER64_UST
:
2081 return lttng_ustconsumer_get_consumed_snapshot(stream
, pos
);
2083 ERR("Unknown consumer_data type");
2089 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2090 int sock
, struct pollfd
*consumer_sockpoll
)
2092 switch (the_consumer_data
.type
) {
2093 case LTTNG_CONSUMER_KERNEL
:
2094 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2095 case LTTNG_CONSUMER32_UST
:
2096 case LTTNG_CONSUMER64_UST
:
2097 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2099 ERR("Unknown consumer_data type");
2106 void lttng_consumer_close_all_metadata(void)
2108 switch (the_consumer_data
.type
) {
2109 case LTTNG_CONSUMER_KERNEL
:
2111 * The Kernel consumer has a different metadata scheme so we don't
2112 * close anything because the stream will be closed by the session
2116 case LTTNG_CONSUMER32_UST
:
2117 case LTTNG_CONSUMER64_UST
:
2119 * Close all metadata streams. The metadata hash table is passed and
2120 * this call iterates over it by closing all wakeup fd. This is safe
2121 * because at this point we are sure that the metadata producer is
2122 * either dead or blocked.
2124 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2127 ERR("Unknown consumer_data type");
2133 * Clean up a metadata stream and free its memory.
2135 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2136 struct lttng_ht
*ht
)
2138 struct lttng_consumer_channel
*channel
= NULL
;
2139 bool free_channel
= false;
2143 * This call should NEVER receive regular stream. It must always be
2144 * metadata stream and this is crucial for data structure synchronization.
2146 assert(stream
->metadata_flag
);
2148 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2150 pthread_mutex_lock(&the_consumer_data
.lock
);
2152 * Note that this assumes that a stream's channel is never changed and
2153 * that the stream's lock doesn't need to be taken to sample its
2156 channel
= stream
->chan
;
2157 pthread_mutex_lock(&channel
->lock
);
2158 pthread_mutex_lock(&stream
->lock
);
2159 if (channel
->metadata_cache
) {
2160 /* Only applicable to userspace consumers. */
2161 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
2164 /* Remove any reference to that stream. */
2165 consumer_stream_delete(stream
, ht
);
2167 /* Close down everything including the relayd if one. */
2168 consumer_stream_close(stream
);
2169 /* Destroy tracer buffers of the stream. */
2170 consumer_stream_destroy_buffers(stream
);
2172 /* Atomically decrement channel refcount since other threads can use it. */
2173 if (!uatomic_sub_return(&channel
->refcount
, 1)
2174 && !uatomic_read(&channel
->nb_init_stream_left
)) {
2175 /* Go for channel deletion! */
2176 free_channel
= true;
2178 stream
->chan
= NULL
;
2181 * Nullify the stream reference so it is not used after deletion. The
2182 * channel lock MUST be acquired before being able to check for a NULL
2185 channel
->metadata_stream
= NULL
;
2186 lttng_wait_queue_wake_all(&channel
->metadata_pushed_wait_queue
);
2188 if (channel
->metadata_cache
) {
2189 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
2191 pthread_mutex_unlock(&stream
->lock
);
2192 pthread_mutex_unlock(&channel
->lock
);
2193 pthread_mutex_unlock(&the_consumer_data
.lock
);
2196 consumer_del_channel(channel
);
2199 lttng_trace_chunk_put(stream
->trace_chunk
);
2200 stream
->trace_chunk
= NULL
;
2201 consumer_stream_free(stream
);
2205 * Action done with the metadata stream when adding it to the consumer internal
2206 * data structures to handle it.
2208 void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2210 struct lttng_ht
*ht
= metadata_ht
;
2211 struct lttng_ht_iter iter
;
2212 struct lttng_ht_node_u64
*node
;
2217 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2219 pthread_mutex_lock(&the_consumer_data
.lock
);
2220 pthread_mutex_lock(&stream
->chan
->lock
);
2221 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2222 pthread_mutex_lock(&stream
->lock
);
2225 * From here, refcounts are updated so be _careful_ when returning an error
2232 * Lookup the stream just to make sure it does not exist in our internal
2233 * state. This should NEVER happen.
2235 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2236 node
= lttng_ht_iter_get_node_u64(&iter
);
2240 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2241 * in terms of destroying the associated channel, because the action that
2242 * causes the count to become 0 also causes a stream to be added. The
2243 * channel deletion will thus be triggered by the following removal of this
2246 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2247 /* Increment refcount before decrementing nb_init_stream_left */
2249 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2252 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2254 lttng_ht_add_u64(the_consumer_data
.stream_per_chan_id_ht
,
2255 &stream
->node_channel_id
);
2258 * Add stream to the stream_list_ht of the consumer data. No need to steal
2259 * the key since the HT does not use it and we allow to add redundant keys
2262 lttng_ht_add_u64(the_consumer_data
.stream_list_ht
,
2263 &stream
->node_session_id
);
2267 pthread_mutex_unlock(&stream
->lock
);
2268 pthread_mutex_unlock(&stream
->chan
->lock
);
2269 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2270 pthread_mutex_unlock(&the_consumer_data
.lock
);
2274 * Delete data stream that are flagged for deletion (endpoint_status).
2276 static void validate_endpoint_status_data_stream(void)
2278 struct lttng_ht_iter iter
;
2279 struct lttng_consumer_stream
*stream
;
2281 DBG("Consumer delete flagged data stream");
2284 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2285 /* Validate delete flag of the stream */
2286 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2289 /* Delete it right now */
2290 consumer_del_stream(stream
, data_ht
);
2296 * Delete metadata stream that are flagged for deletion (endpoint_status).
2298 static void validate_endpoint_status_metadata_stream(
2299 struct lttng_poll_event
*pollset
)
2301 struct lttng_ht_iter iter
;
2302 struct lttng_consumer_stream
*stream
;
2304 DBG("Consumer delete flagged metadata stream");
2309 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2310 /* Validate delete flag of the stream */
2311 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2315 * Remove from pollset so the metadata thread can continue without
2316 * blocking on a deleted stream.
2318 lttng_poll_del(pollset
, stream
->wait_fd
);
2320 /* Delete it right now */
2321 consumer_del_metadata_stream(stream
, metadata_ht
);
2327 * Thread polls on metadata file descriptor and write them on disk or on the
2330 void *consumer_thread_metadata_poll(void *data
)
2332 int ret
, i
, pollfd
, err
= -1;
2333 uint32_t revents
, nb_fd
;
2334 struct lttng_consumer_stream
*stream
= NULL
;
2335 struct lttng_ht_iter iter
;
2336 struct lttng_ht_node_u64
*node
;
2337 struct lttng_poll_event events
;
2338 struct lttng_consumer_local_data
*ctx
= data
;
2341 rcu_register_thread();
2343 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2345 if (testpoint(consumerd_thread_metadata
)) {
2346 goto error_testpoint
;
2349 health_code_update();
2351 DBG("Thread metadata poll started");
2353 /* Size is set to 1 for the consumer_metadata pipe */
2354 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2356 ERR("Poll set creation failed");
2360 ret
= lttng_poll_add(&events
,
2361 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2367 DBG("Metadata main loop started");
2371 health_code_update();
2372 health_poll_entry();
2373 DBG("Metadata poll wait");
2374 ret
= lttng_poll_wait(&events
, -1);
2375 DBG("Metadata poll return from wait with %d fd(s)",
2376 LTTNG_POLL_GETNB(&events
));
2378 DBG("Metadata event caught in thread");
2380 if (errno
== EINTR
) {
2381 ERR("Poll EINTR caught");
2384 if (LTTNG_POLL_GETNB(&events
) == 0) {
2385 err
= 0; /* All is OK */
2392 /* From here, the event is a metadata wait fd */
2393 for (i
= 0; i
< nb_fd
; i
++) {
2394 health_code_update();
2396 revents
= LTTNG_POLL_GETEV(&events
, i
);
2397 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2399 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2400 if (revents
& LPOLLIN
) {
2403 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2404 &stream
, sizeof(stream
));
2405 if (pipe_len
< sizeof(stream
)) {
2407 PERROR("read metadata stream");
2410 * Remove the pipe from the poll set and continue the loop
2411 * since their might be data to consume.
2413 lttng_poll_del(&events
,
2414 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2415 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2419 /* A NULL stream means that the state has changed. */
2420 if (stream
== NULL
) {
2421 /* Check for deleted streams. */
2422 validate_endpoint_status_metadata_stream(&events
);
2426 DBG("Adding metadata stream %d to poll set",
2429 /* Add metadata stream to the global poll events list */
2430 lttng_poll_add(&events
, stream
->wait_fd
,
2431 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2432 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2433 DBG("Metadata thread pipe hung up");
2435 * Remove the pipe from the poll set and continue the loop
2436 * since their might be data to consume.
2438 lttng_poll_del(&events
,
2439 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2440 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2443 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2447 /* Handle other stream */
2453 uint64_t tmp_id
= (uint64_t) pollfd
;
2455 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2457 node
= lttng_ht_iter_get_node_u64(&iter
);
2460 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2463 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2464 /* Get the data out of the metadata file descriptor */
2465 DBG("Metadata available on fd %d", pollfd
);
2466 assert(stream
->wait_fd
== pollfd
);
2469 health_code_update();
2471 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2473 * We don't check the return value here since if we get
2474 * a negative len, it means an error occurred thus we
2475 * simply remove it from the poll set and free the
2480 /* It's ok to have an unavailable sub-buffer */
2481 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2482 /* Clean up stream from consumer and free it. */
2483 lttng_poll_del(&events
, stream
->wait_fd
);
2484 consumer_del_metadata_stream(stream
, metadata_ht
);
2486 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2487 DBG("Metadata fd %d is hup|err.", pollfd
);
2488 if (!stream
->hangup_flush_done
&&
2489 (the_consumer_data
.type
== LTTNG_CONSUMER32_UST
||
2490 the_consumer_data
.type
==
2491 LTTNG_CONSUMER64_UST
)) {
2492 DBG("Attempting to flush and consume the UST buffers");
2493 lttng_ustconsumer_on_stream_hangup(stream
);
2495 /* We just flushed the stream now read it. */
2497 health_code_update();
2499 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2501 * We don't check the return value here since if we get
2502 * a negative len, it means an error occurred thus we
2503 * simply remove it from the poll set and free the
2509 lttng_poll_del(&events
, stream
->wait_fd
);
2511 * This call update the channel states, closes file descriptors
2512 * and securely free the stream.
2514 consumer_del_metadata_stream(stream
, metadata_ht
);
2516 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2520 /* Release RCU lock for the stream looked up */
2528 DBG("Metadata poll thread exiting");
2530 lttng_poll_clean(&events
);
2535 ERR("Health error occurred in %s", __func__
);
2537 health_unregister(health_consumerd
);
2538 rcu_unregister_thread();
2543 * This thread polls the fds in the set to consume the data and write
2544 * it to tracefile if necessary.
2546 void *consumer_thread_data_poll(void *data
)
2548 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2549 struct pollfd
*pollfd
= NULL
;
2550 /* local view of the streams */
2551 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2552 /* local view of consumer_data.fds_count */
2554 /* 2 for the consumer_data_pipe and wake up pipe */
2555 const int nb_pipes_fd
= 2;
2556 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2557 int nb_inactive_fd
= 0;
2558 struct lttng_consumer_local_data
*ctx
= data
;
2561 rcu_register_thread();
2563 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2565 if (testpoint(consumerd_thread_data
)) {
2566 goto error_testpoint
;
2569 health_code_update();
2571 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2572 if (local_stream
== NULL
) {
2573 PERROR("local_stream malloc");
2578 health_code_update();
2584 * the fds set has been updated, we need to update our
2585 * local array as well
2587 pthread_mutex_lock(&the_consumer_data
.lock
);
2588 if (the_consumer_data
.need_update
) {
2593 local_stream
= NULL
;
2595 /* Allocate for all fds */
2596 pollfd
= zmalloc((the_consumer_data
.stream_count
+
2598 sizeof(struct pollfd
));
2599 if (pollfd
== NULL
) {
2600 PERROR("pollfd malloc");
2601 pthread_mutex_unlock(&the_consumer_data
.lock
);
2605 local_stream
= zmalloc((the_consumer_data
.stream_count
+
2607 sizeof(struct lttng_consumer_stream
*));
2608 if (local_stream
== NULL
) {
2609 PERROR("local_stream malloc");
2610 pthread_mutex_unlock(&the_consumer_data
.lock
);
2613 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2614 data_ht
, &nb_inactive_fd
);
2616 ERR("Error in allocating pollfd or local_outfds");
2617 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2618 pthread_mutex_unlock(&the_consumer_data
.lock
);
2622 the_consumer_data
.need_update
= 0;
2624 pthread_mutex_unlock(&the_consumer_data
.lock
);
2626 /* No FDs and consumer_quit, consumer_cleanup the thread */
2627 if (nb_fd
== 0 && nb_inactive_fd
== 0 &&
2628 CMM_LOAD_SHARED(consumer_quit
) == 1) {
2629 err
= 0; /* All is OK */
2632 /* poll on the array of fds */
2634 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2635 if (testpoint(consumerd_thread_data_poll
)) {
2638 health_poll_entry();
2639 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2641 DBG("poll num_rdy : %d", num_rdy
);
2642 if (num_rdy
== -1) {
2644 * Restart interrupted system call.
2646 if (errno
== EINTR
) {
2649 PERROR("Poll error");
2650 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2652 } else if (num_rdy
== 0) {
2653 DBG("Polling thread timed out");
2657 if (caa_unlikely(data_consumption_paused
)) {
2658 DBG("Data consumption paused, sleeping...");
2664 * If the consumer_data_pipe triggered poll go directly to the
2665 * beginning of the loop to update the array. We want to prioritize
2666 * array update over low-priority reads.
2668 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2669 ssize_t pipe_readlen
;
2671 DBG("consumer_data_pipe wake up");
2672 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2673 &new_stream
, sizeof(new_stream
));
2674 if (pipe_readlen
< sizeof(new_stream
)) {
2675 PERROR("Consumer data pipe");
2676 /* Continue so we can at least handle the current stream(s). */
2681 * If the stream is NULL, just ignore it. It's also possible that
2682 * the sessiond poll thread changed the consumer_quit state and is
2683 * waking us up to test it.
2685 if (new_stream
== NULL
) {
2686 validate_endpoint_status_data_stream();
2690 /* Continue to update the local streams and handle prio ones */
2694 /* Handle wakeup pipe. */
2695 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2697 ssize_t pipe_readlen
;
2699 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2701 if (pipe_readlen
< 0) {
2702 PERROR("Consumer data wakeup pipe");
2704 /* We've been awakened to handle stream(s). */
2705 ctx
->has_wakeup
= 0;
2708 /* Take care of high priority channels first. */
2709 for (i
= 0; i
< nb_fd
; i
++) {
2710 health_code_update();
2712 if (local_stream
[i
] == NULL
) {
2715 if (pollfd
[i
].revents
& POLLPRI
) {
2716 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2718 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2719 /* it's ok to have an unavailable sub-buffer */
2720 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2721 /* Clean the stream and free it. */
2722 consumer_del_stream(local_stream
[i
], data_ht
);
2723 local_stream
[i
] = NULL
;
2724 } else if (len
> 0) {
2725 local_stream
[i
]->data_read
= 1;
2731 * If we read high prio channel in this loop, try again
2732 * for more high prio data.
2738 /* Take care of low priority channels. */
2739 for (i
= 0; i
< nb_fd
; i
++) {
2740 health_code_update();
2742 if (local_stream
[i
] == NULL
) {
2745 if ((pollfd
[i
].revents
& POLLIN
) ||
2746 local_stream
[i
]->hangup_flush_done
||
2747 local_stream
[i
]->has_data
) {
2748 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2749 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2750 /* it's ok to have an unavailable sub-buffer */
2751 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2752 /* Clean the stream and free it. */
2753 consumer_del_stream(local_stream
[i
], data_ht
);
2754 local_stream
[i
] = NULL
;
2755 } else if (len
> 0) {
2756 local_stream
[i
]->data_read
= 1;
2761 /* Handle hangup and errors */
2762 for (i
= 0; i
< nb_fd
; i
++) {
2763 health_code_update();
2765 if (local_stream
[i
] == NULL
) {
2768 if (!local_stream
[i
]->hangup_flush_done
2769 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2770 && (the_consumer_data
.type
== LTTNG_CONSUMER32_UST
2771 || the_consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2772 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2774 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2775 /* Attempt read again, for the data we just flushed. */
2776 local_stream
[i
]->data_read
= 1;
2779 * If the poll flag is HUP/ERR/NVAL and we have
2780 * read no data in this pass, we can remove the
2781 * stream from its hash table.
2783 if ((pollfd
[i
].revents
& POLLHUP
)) {
2784 DBG("Polling fd %d tells it has hung up.", 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
& POLLERR
) {
2791 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2792 if (!local_stream
[i
]->data_read
) {
2793 consumer_del_stream(local_stream
[i
], data_ht
);
2794 local_stream
[i
] = NULL
;
2797 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2798 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2799 if (!local_stream
[i
]->data_read
) {
2800 consumer_del_stream(local_stream
[i
], data_ht
);
2801 local_stream
[i
] = NULL
;
2805 if (local_stream
[i
] != NULL
) {
2806 local_stream
[i
]->data_read
= 0;
2813 DBG("polling thread exiting");
2818 * Close the write side of the pipe so epoll_wait() in
2819 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2820 * read side of the pipe. If we close them both, epoll_wait strangely does
2821 * not return and could create a endless wait period if the pipe is the
2822 * only tracked fd in the poll set. The thread will take care of closing
2825 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2830 ERR("Health error occurred in %s", __func__
);
2832 health_unregister(health_consumerd
);
2834 rcu_unregister_thread();
2839 * Close wake-up end of each stream belonging to the channel. This will
2840 * allow the poll() on the stream read-side to detect when the
2841 * write-side (application) finally closes them.
2844 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2846 struct lttng_ht
*ht
;
2847 struct lttng_consumer_stream
*stream
;
2848 struct lttng_ht_iter iter
;
2850 ht
= the_consumer_data
.stream_per_chan_id_ht
;
2853 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2854 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2855 ht
->match_fct
, &channel
->key
,
2856 &iter
.iter
, stream
, node_channel_id
.node
) {
2858 * Protect against teardown with mutex.
2860 pthread_mutex_lock(&stream
->lock
);
2861 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2864 switch (the_consumer_data
.type
) {
2865 case LTTNG_CONSUMER_KERNEL
:
2867 case LTTNG_CONSUMER32_UST
:
2868 case LTTNG_CONSUMER64_UST
:
2869 if (stream
->metadata_flag
) {
2870 /* Safe and protected by the stream lock. */
2871 lttng_ustconsumer_close_metadata(stream
->chan
);
2874 * Note: a mutex is taken internally within
2875 * liblttng-ust-ctl to protect timer wakeup_fd
2876 * use from concurrent close.
2878 lttng_ustconsumer_close_stream_wakeup(stream
);
2882 ERR("Unknown consumer_data type");
2886 pthread_mutex_unlock(&stream
->lock
);
2891 static void destroy_channel_ht(struct lttng_ht
*ht
)
2893 struct lttng_ht_iter iter
;
2894 struct lttng_consumer_channel
*channel
;
2902 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2903 ret
= lttng_ht_del(ht
, &iter
);
2908 lttng_ht_destroy(ht
);
2912 * This thread polls the channel fds to detect when they are being
2913 * closed. It closes all related streams if the channel is detected as
2914 * closed. It is currently only used as a shim layer for UST because the
2915 * consumerd needs to keep the per-stream wakeup end of pipes open for
2918 void *consumer_thread_channel_poll(void *data
)
2920 int ret
, i
, pollfd
, err
= -1;
2921 uint32_t revents
, nb_fd
;
2922 struct lttng_consumer_channel
*chan
= NULL
;
2923 struct lttng_ht_iter iter
;
2924 struct lttng_ht_node_u64
*node
;
2925 struct lttng_poll_event events
;
2926 struct lttng_consumer_local_data
*ctx
= data
;
2927 struct lttng_ht
*channel_ht
;
2929 rcu_register_thread();
2931 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2933 if (testpoint(consumerd_thread_channel
)) {
2934 goto error_testpoint
;
2937 health_code_update();
2939 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2941 /* ENOMEM at this point. Better to bail out. */
2945 DBG("Thread channel poll started");
2947 /* Size is set to 1 for the consumer_channel pipe */
2948 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2950 ERR("Poll set creation failed");
2954 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2960 DBG("Channel main loop started");
2964 health_code_update();
2965 DBG("Channel poll wait");
2966 health_poll_entry();
2967 ret
= lttng_poll_wait(&events
, -1);
2968 DBG("Channel poll return from wait with %d fd(s)",
2969 LTTNG_POLL_GETNB(&events
));
2971 DBG("Channel event caught in thread");
2973 if (errno
== EINTR
) {
2974 ERR("Poll EINTR caught");
2977 if (LTTNG_POLL_GETNB(&events
) == 0) {
2978 err
= 0; /* All is OK */
2985 /* From here, the event is a channel wait fd */
2986 for (i
= 0; i
< nb_fd
; i
++) {
2987 health_code_update();
2989 revents
= LTTNG_POLL_GETEV(&events
, i
);
2990 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2992 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2993 if (revents
& LPOLLIN
) {
2994 enum consumer_channel_action action
;
2997 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
3000 ERR("Error reading channel pipe");
3002 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3007 case CONSUMER_CHANNEL_ADD
:
3008 DBG("Adding channel %d to poll set",
3011 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
3014 lttng_ht_add_unique_u64(channel_ht
,
3015 &chan
->wait_fd_node
);
3017 /* Add channel to the global poll events list */
3018 lttng_poll_add(&events
, chan
->wait_fd
,
3019 LPOLLERR
| LPOLLHUP
);
3021 case CONSUMER_CHANNEL_DEL
:
3024 * This command should never be called if the channel
3025 * has streams monitored by either the data or metadata
3026 * thread. The consumer only notify this thread with a
3027 * channel del. command if it receives a destroy
3028 * channel command from the session daemon that send it
3029 * if a command prior to the GET_CHANNEL failed.
3033 chan
= consumer_find_channel(key
);
3036 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
3039 lttng_poll_del(&events
, chan
->wait_fd
);
3040 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
3041 ret
= lttng_ht_del(channel_ht
, &iter
);
3044 switch (the_consumer_data
.type
) {
3045 case LTTNG_CONSUMER_KERNEL
:
3047 case LTTNG_CONSUMER32_UST
:
3048 case LTTNG_CONSUMER64_UST
:
3049 health_code_update();
3050 /* Destroy streams that might have been left in the stream list. */
3051 clean_channel_stream_list(chan
);
3054 ERR("Unknown consumer_data type");
3059 * Release our own refcount. Force channel deletion even if
3060 * streams were not initialized.
3062 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3063 consumer_del_channel(chan
);
3068 case CONSUMER_CHANNEL_QUIT
:
3070 * Remove the pipe from the poll set and continue the loop
3071 * since their might be data to consume.
3073 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3076 ERR("Unknown action");
3079 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3080 DBG("Channel thread pipe hung up");
3082 * Remove the pipe from the poll set and continue the loop
3083 * since their might be data to consume.
3085 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3088 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3092 /* Handle other stream */
3098 uint64_t tmp_id
= (uint64_t) pollfd
;
3100 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3102 node
= lttng_ht_iter_get_node_u64(&iter
);
3105 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
3108 /* Check for error event */
3109 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3110 DBG("Channel fd %d is hup|err.", pollfd
);
3112 lttng_poll_del(&events
, chan
->wait_fd
);
3113 ret
= lttng_ht_del(channel_ht
, &iter
);
3117 * This will close the wait fd for each stream associated to
3118 * this channel AND monitored by the data/metadata thread thus
3119 * will be clean by the right thread.
3121 consumer_close_channel_streams(chan
);
3123 /* Release our own refcount */
3124 if (!uatomic_sub_return(&chan
->refcount
, 1)
3125 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3126 consumer_del_channel(chan
);
3129 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3134 /* Release RCU lock for the channel looked up */
3142 lttng_poll_clean(&events
);
3144 destroy_channel_ht(channel_ht
);
3147 DBG("Channel poll thread exiting");
3150 ERR("Health error occurred in %s", __func__
);
3152 health_unregister(health_consumerd
);
3153 rcu_unregister_thread();
3157 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3158 struct pollfd
*sockpoll
, int client_socket
)
3165 ret
= lttng_consumer_poll_socket(sockpoll
);
3169 DBG("Metadata connection on client_socket");
3171 /* Blocking call, waiting for transmission */
3172 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3173 if (ctx
->consumer_metadata_socket
< 0) {
3174 WARN("On accept metadata");
3185 * This thread listens on the consumerd socket and receives the file
3186 * descriptors from the session daemon.
3188 void *consumer_thread_sessiond_poll(void *data
)
3190 int sock
= -1, client_socket
, ret
, err
= -1;
3192 * structure to poll for incoming data on communication socket avoids
3193 * making blocking sockets.
3195 struct pollfd consumer_sockpoll
[2];
3196 struct lttng_consumer_local_data
*ctx
= data
;
3198 rcu_register_thread();
3200 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3202 if (testpoint(consumerd_thread_sessiond
)) {
3203 goto error_testpoint
;
3206 health_code_update();
3208 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3209 unlink(ctx
->consumer_command_sock_path
);
3210 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3211 if (client_socket
< 0) {
3212 ERR("Cannot create command socket");
3216 ret
= lttcomm_listen_unix_sock(client_socket
);
3221 DBG("Sending ready command to lttng-sessiond");
3222 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3223 /* return < 0 on error, but == 0 is not fatal */
3225 ERR("Error sending ready command to lttng-sessiond");
3229 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3230 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3231 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3232 consumer_sockpoll
[1].fd
= client_socket
;
3233 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3235 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3243 DBG("Connection on client_socket");
3245 /* Blocking call, waiting for transmission */
3246 sock
= lttcomm_accept_unix_sock(client_socket
);
3253 * Setup metadata socket which is the second socket connection on the
3254 * command unix socket.
3256 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3265 /* This socket is not useful anymore. */
3266 ret
= close(client_socket
);
3268 PERROR("close client_socket");
3272 /* update the polling structure to poll on the established socket */
3273 consumer_sockpoll
[1].fd
= sock
;
3274 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3277 health_code_update();
3279 health_poll_entry();
3280 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3289 DBG("Incoming command on sock");
3290 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3293 * This could simply be a session daemon quitting. Don't output
3296 DBG("Communication interrupted on command socket");
3300 if (CMM_LOAD_SHARED(consumer_quit
)) {
3301 DBG("consumer_thread_receive_fds received quit from signal");
3302 err
= 0; /* All is OK */
3305 DBG("Received command on sock");
3311 DBG("Consumer thread sessiond poll exiting");
3314 * Close metadata streams since the producer is the session daemon which
3317 * NOTE: for now, this only applies to the UST tracer.
3319 lttng_consumer_close_all_metadata();
3322 * when all fds have hung up, the polling thread
3325 CMM_STORE_SHARED(consumer_quit
, 1);
3328 * Notify the data poll thread to poll back again and test the
3329 * consumer_quit state that we just set so to quit gracefully.
3331 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3333 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3335 notify_health_quit_pipe(health_quit_pipe
);
3337 /* Cleaning up possibly open sockets. */
3341 PERROR("close sock sessiond poll");
3344 if (client_socket
>= 0) {
3345 ret
= close(client_socket
);
3347 PERROR("close client_socket sessiond poll");
3354 ERR("Health error occurred in %s", __func__
);
3356 health_unregister(health_consumerd
);
3358 rcu_unregister_thread();
3362 static int post_consume(struct lttng_consumer_stream
*stream
,
3363 const struct stream_subbuffer
*subbuffer
,
3364 struct lttng_consumer_local_data
*ctx
)
3368 const size_t count
= lttng_dynamic_array_get_count(
3369 &stream
->read_subbuffer_ops
.post_consume_cbs
);
3371 for (i
= 0; i
< count
; i
++) {
3372 const post_consume_cb op
= *(post_consume_cb
*) lttng_dynamic_array_get_element(
3373 &stream
->read_subbuffer_ops
.post_consume_cbs
,
3376 ret
= op(stream
, subbuffer
, ctx
);
3385 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3386 struct lttng_consumer_local_data
*ctx
,
3387 bool locked_by_caller
)
3389 ssize_t ret
, written_bytes
= 0;
3391 struct stream_subbuffer subbuffer
= {};
3392 enum get_next_subbuffer_status get_next_status
;
3394 if (!locked_by_caller
) {
3395 stream
->read_subbuffer_ops
.lock(stream
);
3397 stream
->read_subbuffer_ops
.assert_locked(stream
);
3400 if (stream
->read_subbuffer_ops
.on_wake_up
) {
3401 ret
= stream
->read_subbuffer_ops
.on_wake_up(stream
);
3408 * If the stream was flagged to be ready for rotation before we extract
3409 * the next packet, rotate it now.
3411 if (stream
->rotate_ready
) {
3412 DBG("Rotate stream before consuming data");
3413 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3415 ERR("Stream rotation error before consuming data");
3420 get_next_status
= stream
->read_subbuffer_ops
.get_next_subbuffer(
3421 stream
, &subbuffer
);
3422 switch (get_next_status
) {
3423 case GET_NEXT_SUBBUFFER_STATUS_OK
:
3425 case GET_NEXT_SUBBUFFER_STATUS_NO_DATA
:
3429 case GET_NEXT_SUBBUFFER_STATUS_ERROR
:
3436 ret
= stream
->read_subbuffer_ops
.pre_consume_subbuffer(
3437 stream
, &subbuffer
);
3439 goto error_put_subbuf
;
3442 written_bytes
= stream
->read_subbuffer_ops
.consume_subbuffer(
3443 ctx
, stream
, &subbuffer
);
3444 if (written_bytes
<= 0) {
3445 ERR("Error consuming subbuffer: (%zd)", written_bytes
);
3446 ret
= (int) written_bytes
;
3447 goto error_put_subbuf
;
3450 ret
= stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3455 ret
= post_consume(stream
, &subbuffer
, ctx
);
3461 * After extracting the packet, we check if the stream is now ready to
3462 * be rotated and perform the action immediately.
3464 * Don't overwrite `ret` as callers expect the number of bytes
3465 * consumed to be returned on success.
3467 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
3468 if (rotation_ret
== 1) {
3469 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3470 if (rotation_ret
< 0) {
3472 ERR("Stream rotation error after consuming data");
3476 } else if (rotation_ret
< 0) {
3478 ERR("Failed to check if stream was ready to rotate after consuming data");
3483 if (stream
->read_subbuffer_ops
.on_sleep
) {
3484 stream
->read_subbuffer_ops
.on_sleep(stream
, ctx
);
3487 ret
= written_bytes
;
3489 if (!locked_by_caller
) {
3490 stream
->read_subbuffer_ops
.unlock(stream
);
3495 (void) stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3499 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3501 switch (the_consumer_data
.type
) {
3502 case LTTNG_CONSUMER_KERNEL
:
3503 return lttng_kconsumer_on_recv_stream(stream
);
3504 case LTTNG_CONSUMER32_UST
:
3505 case LTTNG_CONSUMER64_UST
:
3506 return lttng_ustconsumer_on_recv_stream(stream
);
3508 ERR("Unknown consumer_data type");
3515 * Allocate and set consumer data hash tables.
3517 int lttng_consumer_init(void)
3519 the_consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3520 if (!the_consumer_data
.channel_ht
) {
3524 the_consumer_data
.channels_by_session_id_ht
=
3525 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3526 if (!the_consumer_data
.channels_by_session_id_ht
) {
3530 the_consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3531 if (!the_consumer_data
.relayd_ht
) {
3535 the_consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3536 if (!the_consumer_data
.stream_list_ht
) {
3540 the_consumer_data
.stream_per_chan_id_ht
=
3541 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3542 if (!the_consumer_data
.stream_per_chan_id_ht
) {
3546 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3551 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3556 the_consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3557 if (!the_consumer_data
.chunk_registry
) {
3568 * Process the ADD_RELAYD command receive by a consumer.
3570 * This will create a relayd socket pair and add it to the relayd hash table.
3571 * The caller MUST acquire a RCU read side lock before calling it.
3573 void consumer_add_relayd_socket(uint64_t net_seq_idx
,
3575 struct lttng_consumer_local_data
*ctx
,
3577 struct pollfd
*consumer_sockpoll
,
3578 uint64_t sessiond_id
,
3579 uint64_t relayd_session_id
,
3580 uint32_t relayd_version_major
,
3581 uint32_t relayd_version_minor
,
3582 enum lttcomm_sock_proto relayd_socket_protocol
)
3584 int fd
= -1, ret
= -1, relayd_created
= 0;
3585 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3586 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3590 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3592 /* Get relayd reference if exists. */
3593 relayd
= consumer_find_relayd(net_seq_idx
);
3594 if (relayd
== NULL
) {
3595 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3596 /* Not found. Allocate one. */
3597 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3598 if (relayd
== NULL
) {
3599 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3602 relayd
->sessiond_session_id
= sessiond_id
;
3607 * This code path MUST continue to the consumer send status message to
3608 * we can notify the session daemon and continue our work without
3609 * killing everything.
3613 * relayd key should never be found for control socket.
3615 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3618 /* First send a status message before receiving the fds. */
3619 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3621 /* Somehow, the session daemon is not responding anymore. */
3622 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3623 goto error_nosignal
;
3626 /* Poll on consumer socket. */
3627 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3629 /* Needing to exit in the middle of a command: error. */
3630 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3631 goto error_nosignal
;
3634 /* Get relayd socket from session daemon */
3635 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3636 if (ret
!= sizeof(fd
)) {
3637 fd
= -1; /* Just in case it gets set with an invalid value. */
3640 * Failing to receive FDs might indicate a major problem such as
3641 * reaching a fd limit during the receive where the kernel returns a
3642 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3643 * don't take any chances and stop everything.
3645 * XXX: Feature request #558 will fix that and avoid this possible
3646 * issue when reaching the fd limit.
3648 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3649 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3653 /* Copy socket information and received FD */
3654 switch (sock_type
) {
3655 case LTTNG_STREAM_CONTROL
:
3656 /* Copy received lttcomm socket */
3657 ret
= lttcomm_populate_sock_from_open_socket(
3658 &relayd
->control_sock
.sock
, fd
,
3659 relayd_socket_protocol
);
3661 /* Assign version values. */
3662 relayd
->control_sock
.major
= relayd_version_major
;
3663 relayd
->control_sock
.minor
= relayd_version_minor
;
3665 relayd
->relayd_session_id
= relayd_session_id
;
3668 case LTTNG_STREAM_DATA
:
3669 /* Copy received lttcomm socket */
3670 ret
= lttcomm_populate_sock_from_open_socket(
3671 &relayd
->data_sock
.sock
, fd
,
3672 relayd_socket_protocol
);
3673 /* Assign version values. */
3674 relayd
->data_sock
.major
= relayd_version_major
;
3675 relayd
->data_sock
.minor
= relayd_version_minor
;
3678 ERR("Unknown relayd socket type (%d)", sock_type
);
3679 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3684 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3688 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3689 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3690 relayd
->net_seq_idx
, fd
);
3692 * We gave the ownership of the fd to the relayd structure. Set the
3693 * fd to -1 so we don't call close() on it in the error path below.
3697 /* We successfully added the socket. Send status back. */
3698 ret
= consumer_send_status_msg(sock
, ret_code
);
3700 /* Somehow, the session daemon is not responding anymore. */
3701 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3702 goto error_nosignal
;
3706 * Add relayd socket pair to consumer data hashtable. If object already
3707 * exists or on error, the function gracefully returns.
3716 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3717 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3721 /* Close received socket if valid. */
3724 PERROR("close received socket");
3728 if (relayd_created
) {
3734 * Search for a relayd associated to the session id and return the reference.
3736 * A rcu read side lock MUST be acquire before calling this function and locked
3737 * until the relayd object is no longer necessary.
3739 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3741 struct lttng_ht_iter iter
;
3742 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3744 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3745 cds_lfht_for_each_entry(the_consumer_data
.relayd_ht
->ht
, &iter
.iter
,
3746 relayd
, node
.node
) {
3748 * Check by sessiond id which is unique here where the relayd session
3749 * id might not be when having multiple relayd.
3751 if (relayd
->sessiond_session_id
== id
) {
3752 /* Found the relayd. There can be only one per id. */
3764 * Check if for a given session id there is still data needed to be extract
3767 * Return 1 if data is pending or else 0 meaning ready to be read.
3769 int consumer_data_pending(uint64_t id
)
3772 struct lttng_ht_iter iter
;
3773 struct lttng_ht
*ht
;
3774 struct lttng_consumer_stream
*stream
;
3775 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3776 int (*data_pending
)(struct lttng_consumer_stream
*);
3778 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3781 pthread_mutex_lock(&the_consumer_data
.lock
);
3783 switch (the_consumer_data
.type
) {
3784 case LTTNG_CONSUMER_KERNEL
:
3785 data_pending
= lttng_kconsumer_data_pending
;
3787 case LTTNG_CONSUMER32_UST
:
3788 case LTTNG_CONSUMER64_UST
:
3789 data_pending
= lttng_ustconsumer_data_pending
;
3792 ERR("Unknown consumer data type");
3796 /* Ease our life a bit */
3797 ht
= the_consumer_data
.stream_list_ht
;
3799 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3800 ht
->hash_fct(&id
, lttng_ht_seed
),
3802 &iter
.iter
, stream
, node_session_id
.node
) {
3803 pthread_mutex_lock(&stream
->lock
);
3806 * A removed node from the hash table indicates that the stream has
3807 * been deleted thus having a guarantee that the buffers are closed
3808 * on the consumer side. However, data can still be transmitted
3809 * over the network so don't skip the relayd check.
3811 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3813 /* Check the stream if there is data in the buffers. */
3814 ret
= data_pending(stream
);
3816 pthread_mutex_unlock(&stream
->lock
);
3821 pthread_mutex_unlock(&stream
->lock
);
3824 relayd
= find_relayd_by_session_id(id
);
3826 unsigned int is_data_inflight
= 0;
3828 /* Send init command for data pending. */
3829 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3830 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3831 relayd
->relayd_session_id
);
3833 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3834 /* Communication error thus the relayd so no data pending. */
3835 goto data_not_pending
;
3838 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3839 ht
->hash_fct(&id
, lttng_ht_seed
),
3841 &iter
.iter
, stream
, node_session_id
.node
) {
3842 if (stream
->metadata_flag
) {
3843 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3844 stream
->relayd_stream_id
);
3846 ret
= relayd_data_pending(&relayd
->control_sock
,
3847 stream
->relayd_stream_id
,
3848 stream
->next_net_seq_num
- 1);
3852 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3854 } else if (ret
< 0) {
3855 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3856 lttng_consumer_cleanup_relayd(relayd
);
3857 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3858 goto data_not_pending
;
3862 /* Send end command for data pending. */
3863 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3864 relayd
->relayd_session_id
, &is_data_inflight
);
3865 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3867 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3868 lttng_consumer_cleanup_relayd(relayd
);
3869 goto data_not_pending
;
3871 if (is_data_inflight
) {
3877 * Finding _no_ node in the hash table and no inflight data means that the
3878 * stream(s) have been removed thus data is guaranteed to be available for
3879 * analysis from the trace files.
3883 /* Data is available to be read by a viewer. */
3884 pthread_mutex_unlock(&the_consumer_data
.lock
);
3889 /* Data is still being extracted from buffers. */
3890 pthread_mutex_unlock(&the_consumer_data
.lock
);
3896 * Send a ret code status message to the sessiond daemon.
3898 * Return the sendmsg() return value.
3900 int consumer_send_status_msg(int sock
, int ret_code
)
3902 struct lttcomm_consumer_status_msg msg
;
3904 memset(&msg
, 0, sizeof(msg
));
3905 msg
.ret_code
= ret_code
;
3907 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3911 * Send a channel status message to the sessiond daemon.
3913 * Return the sendmsg() return value.
3915 int consumer_send_status_channel(int sock
,
3916 struct lttng_consumer_channel
*channel
)
3918 struct lttcomm_consumer_status_channel msg
;
3922 memset(&msg
, 0, sizeof(msg
));
3924 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3926 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3927 msg
.key
= channel
->key
;
3928 msg
.stream_count
= channel
->streams
.count
;
3931 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3934 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3935 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3936 uint64_t max_sb_size
)
3938 unsigned long start_pos
;
3940 if (!nb_packets_per_stream
) {
3941 return consumed_pos
; /* Grab everything */
3943 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
3944 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3945 if ((long) (start_pos
- consumed_pos
) < 0) {
3946 return consumed_pos
; /* Grab everything */
3951 /* Stream lock must be held by the caller. */
3952 static int sample_stream_positions(struct lttng_consumer_stream
*stream
,
3953 unsigned long *produced
, unsigned long *consumed
)
3957 ASSERT_LOCKED(stream
->lock
);
3959 ret
= lttng_consumer_sample_snapshot_positions(stream
);
3961 ERR("Failed to sample snapshot positions");
3965 ret
= lttng_consumer_get_produced_snapshot(stream
, produced
);
3967 ERR("Failed to sample produced position");
3971 ret
= lttng_consumer_get_consumed_snapshot(stream
, consumed
);
3973 ERR("Failed to sample consumed position");
3982 * Sample the rotate position for all the streams of a channel. If a stream
3983 * is already at the rotate position (produced == consumed), we flag it as
3984 * ready for rotation. The rotation of ready streams occurs after we have
3985 * replied to the session daemon that we have finished sampling the positions.
3986 * Must be called with RCU read-side lock held to ensure existence of channel.
3988 * Returns 0 on success, < 0 on error
3990 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
3991 uint64_t key
, uint64_t relayd_id
, uint32_t metadata
,
3992 struct lttng_consumer_local_data
*ctx
)
3995 struct lttng_consumer_stream
*stream
;
3996 struct lttng_ht_iter iter
;
3997 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
3998 struct lttng_dynamic_array stream_rotation_positions
;
3999 uint64_t next_chunk_id
, stream_count
= 0;
4000 enum lttng_trace_chunk_status chunk_status
;
4001 const bool is_local_trace
= relayd_id
== -1ULL;
4002 struct consumer_relayd_sock_pair
*relayd
= NULL
;
4003 bool rotating_to_new_chunk
= true;
4004 /* Array of `struct lttng_consumer_stream *` */
4005 struct lttng_dynamic_pointer_array streams_packet_to_open
;
4008 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
4010 lttng_dynamic_array_init(&stream_rotation_positions
,
4011 sizeof(struct relayd_stream_rotation_position
), NULL
);
4012 lttng_dynamic_pointer_array_init(&streams_packet_to_open
, NULL
);
4016 pthread_mutex_lock(&channel
->lock
);
4017 assert(channel
->trace_chunk
);
4018 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
,
4020 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4022 goto end_unlock_channel
;
4025 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4026 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4027 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4028 stream
, node_channel_id
.node
) {
4029 unsigned long produced_pos
= 0, consumed_pos
= 0;
4031 health_code_update();
4034 * Lock stream because we are about to change its state.
4036 pthread_mutex_lock(&stream
->lock
);
4038 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
4039 rotating_to_new_chunk
= false;
4043 * Do not flush a packet when rotating from a NULL trace
4044 * chunk. The stream has no means to output data, and the prior
4045 * rotation which rotated to NULL performed that side-effect
4046 * already. No new data can be produced when a stream has no
4047 * associated trace chunk (e.g. a stop followed by a rotate).
4049 if (stream
->trace_chunk
) {
4052 if (stream
->metadata_flag
) {
4054 * Don't produce an empty metadata packet,
4055 * simply close the current one.
4057 * Metadata is regenerated on every trace chunk
4058 * switch; there is no concern that no data was
4061 flush_active
= true;
4064 * Only flush an empty packet if the "packet
4065 * open" could not be performed on transition
4066 * to a new trace chunk and no packets were
4067 * consumed within the chunk's lifetime.
4069 if (stream
->opened_packet_in_current_trace_chunk
) {
4070 flush_active
= true;
4073 * Stream could have been full at the
4074 * time of rotation, but then have had
4075 * no activity at all.
4077 * It is important to flush a packet
4078 * to prevent 0-length files from being
4079 * produced as most viewers choke on
4082 * Unfortunately viewers will not be
4083 * able to know that tracing was active
4084 * for this stream during this trace
4087 ret
= sample_stream_positions(stream
, &produced_pos
, &consumed_pos
);
4089 goto end_unlock_stream
;
4093 * Don't flush an empty packet if data
4094 * was produced; it will be consumed
4095 * before the rotation completes.
4097 flush_active
= produced_pos
!= consumed_pos
;
4098 if (!flush_active
) {
4099 const char *trace_chunk_name
;
4100 uint64_t trace_chunk_id
;
4102 chunk_status
= lttng_trace_chunk_get_name(
4103 stream
->trace_chunk
,
4106 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NONE
) {
4107 trace_chunk_name
= "none";
4111 * Consumer trace chunks are
4114 chunk_status
= lttng_trace_chunk_get_id(
4115 stream
->trace_chunk
,
4117 assert(chunk_status
==
4118 LTTNG_TRACE_CHUNK_STATUS_OK
);
4120 DBG("Unable to open packet for stream during trace chunk's lifetime. "
4121 "Flushing an empty packet to prevent an empty file from being created: "
4122 "stream id = %" PRIu64
", trace chunk name = `%s`, trace chunk id = %" PRIu64
,
4123 stream
->key
, trace_chunk_name
, trace_chunk_id
);
4129 * Close the current packet before sampling the
4130 * ring buffer positions.
4132 ret
= consumer_stream_flush_buffer(stream
, flush_active
);
4134 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4136 goto end_unlock_stream
;
4140 ret
= lttng_consumer_take_snapshot(stream
);
4141 if (ret
< 0 && ret
!= -ENODATA
&& ret
!= -EAGAIN
) {
4142 ERR("Failed to sample snapshot position during channel rotation");
4143 goto end_unlock_stream
;
4146 ret
= lttng_consumer_get_produced_snapshot(stream
,
4149 ERR("Failed to sample produced position during channel rotation");
4150 goto end_unlock_stream
;
4153 ret
= lttng_consumer_get_consumed_snapshot(stream
,
4156 ERR("Failed to sample consumed position during channel rotation");
4157 goto end_unlock_stream
;
4161 * Align produced position on the start-of-packet boundary of the first
4162 * packet going into the next trace chunk.
4164 produced_pos
= ALIGN_FLOOR(produced_pos
, stream
->max_sb_size
);
4165 if (consumed_pos
== produced_pos
) {
4166 DBG("Set rotate ready for stream %" PRIu64
" produced = %lu consumed = %lu",
4167 stream
->key
, produced_pos
, consumed_pos
);
4168 stream
->rotate_ready
= true;
4170 DBG("Different consumed and produced positions "
4171 "for stream %" PRIu64
" produced = %lu consumed = %lu",
4172 stream
->key
, produced_pos
, consumed_pos
);
4175 * The rotation position is based on the packet_seq_num of the
4176 * packet following the last packet that was consumed for this
4177 * stream, incremented by the offset between produced and
4178 * consumed positions. This rotation position is a lower bound
4179 * (inclusive) at which the next trace chunk starts. Since it
4180 * is a lower bound, it is OK if the packet_seq_num does not
4181 * correspond exactly to the same packet identified by the
4182 * consumed_pos, which can happen in overwrite mode.
4184 if (stream
->sequence_number_unavailable
) {
4186 * Rotation should never be performed on a session which
4187 * interacts with a pre-2.8 lttng-modules, which does
4188 * not implement packet sequence number.
4190 ERR("Failure to rotate stream %" PRIu64
": sequence number unavailable",
4193 goto end_unlock_stream
;
4195 stream
->rotate_position
= stream
->last_sequence_number
+ 1 +
4196 ((produced_pos
- consumed_pos
) / stream
->max_sb_size
);
4197 DBG("Set rotation position for stream %" PRIu64
" at position %" PRIu64
,
4198 stream
->key
, stream
->rotate_position
);
4200 if (!is_local_trace
) {
4202 * The relay daemon control protocol expects a rotation
4203 * position as "the sequence number of the first packet
4204 * _after_ the current trace chunk".
4206 const struct relayd_stream_rotation_position position
= {
4207 .stream_id
= stream
->relayd_stream_id
,
4208 .rotate_at_seq_num
= stream
->rotate_position
,
4211 ret
= lttng_dynamic_array_add_element(
4212 &stream_rotation_positions
,
4215 ERR("Failed to allocate stream rotation position");
4216 goto end_unlock_stream
;
4221 stream
->opened_packet_in_current_trace_chunk
= false;
4223 if (rotating_to_new_chunk
&& !stream
->metadata_flag
) {
4225 * Attempt to flush an empty packet as close to the
4226 * rotation point as possible. In the event where a
4227 * stream remains inactive after the rotation point,
4228 * this ensures that the new trace chunk has a
4229 * beginning timestamp set at the begining of the
4230 * trace chunk instead of only creating an empty
4231 * packet when the trace chunk is stopped.
4233 * This indicates to the viewers that the stream
4234 * was being recorded, but more importantly it
4235 * allows viewers to determine a useable trace
4238 * This presents a problem in the case where the
4239 * ring-buffer is completely full.
4241 * Consider the following scenario:
4242 * - The consumption of data is slow (slow network,
4244 * - The ring buffer is full,
4245 * - A rotation is initiated,
4246 * - The flush below does nothing (no space left to
4247 * open a new packet),
4248 * - The other streams rotate very soon, and new
4249 * data is produced in the new chunk,
4250 * - This stream completes its rotation long after the
4251 * rotation was initiated
4252 * - The session is stopped before any event can be
4253 * produced in this stream's buffers.
4255 * The resulting trace chunk will have a single packet
4256 * temporaly at the end of the trace chunk for this
4257 * stream making the stream intersection more narrow
4258 * than it should be.
4260 * To work-around this, an empty flush is performed
4261 * after the first consumption of a packet during a
4262 * rotation if open_packet fails. The idea is that
4263 * consuming a packet frees enough space to switch
4264 * packets in this scenario and allows the tracer to
4265 * "stamp" the beginning of the new trace chunk at the
4266 * earliest possible point.
4268 * The packet open is performed after the channel
4269 * rotation to ensure that no attempt to open a packet
4270 * is performed in a stream that has no active trace
4273 ret
= lttng_dynamic_pointer_array_add_pointer(
4274 &streams_packet_to_open
, stream
);
4276 PERROR("Failed to add a stream pointer to array of streams in which to open a packet");
4278 goto end_unlock_stream
;
4282 pthread_mutex_unlock(&stream
->lock
);
4286 if (!is_local_trace
) {
4287 relayd
= consumer_find_relayd(relayd_id
);
4289 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4291 goto end_unlock_channel
;
4294 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4295 ret
= relayd_rotate_streams(&relayd
->control_sock
, stream_count
,
4296 rotating_to_new_chunk
? &next_chunk_id
: NULL
,
4297 (const struct relayd_stream_rotation_position
*)
4298 stream_rotation_positions
.buffer
4300 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4302 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4303 relayd
->net_seq_idx
);
4304 lttng_consumer_cleanup_relayd(relayd
);
4305 goto end_unlock_channel
;
4309 for (stream_idx
= 0;
4310 stream_idx
< lttng_dynamic_pointer_array_get_count(
4311 &streams_packet_to_open
);
4313 enum consumer_stream_open_packet_status status
;
4315 stream
= lttng_dynamic_pointer_array_get_pointer(
4316 &streams_packet_to_open
, stream_idx
);
4318 pthread_mutex_lock(&stream
->lock
);
4319 status
= consumer_stream_open_packet(stream
);
4320 pthread_mutex_unlock(&stream
->lock
);
4322 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
4323 DBG("Opened a packet after a rotation: stream id = %" PRIu64
4324 ", channel name = %s, session id = %" PRIu64
,
4325 stream
->key
, stream
->chan
->name
,
4326 stream
->chan
->session_id
);
4328 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
4330 * Can't open a packet as there is no space left
4331 * in the buffer. A new packet will be opened
4332 * once one has been consumed.
4334 DBG("No space left to open a packet after a rotation: stream id = %" PRIu64
4335 ", channel name = %s, session id = %" PRIu64
,
4336 stream
->key
, stream
->chan
->name
,
4337 stream
->chan
->session_id
);
4339 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
4340 /* Logged by callee. */
4342 goto end_unlock_channel
;
4348 pthread_mutex_unlock(&channel
->lock
);
4353 pthread_mutex_unlock(&stream
->lock
);
4355 pthread_mutex_unlock(&channel
->lock
);
4358 lttng_dynamic_array_reset(&stream_rotation_positions
);
4359 lttng_dynamic_pointer_array_reset(&streams_packet_to_open
);
4364 int consumer_clear_buffer(struct lttng_consumer_stream
*stream
)
4367 unsigned long consumed_pos_before
, consumed_pos_after
;
4369 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4371 ERR("Taking snapshot positions");
4375 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_before
);
4377 ERR("Consumed snapshot position");
4381 switch (the_consumer_data
.type
) {
4382 case LTTNG_CONSUMER_KERNEL
:
4383 ret
= kernctl_buffer_clear(stream
->wait_fd
);
4385 ERR("Failed to clear kernel stream (ret = %d)", ret
);
4389 case LTTNG_CONSUMER32_UST
:
4390 case LTTNG_CONSUMER64_UST
:
4391 ret
= lttng_ustconsumer_clear_buffer(stream
);
4393 ERR("Failed to clear ust stream (ret = %d)", ret
);
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
;
5246 void lttng_consumer_sigbus_handle(void *addr
)
5248 lttng_ustconsumer_sigbus_handle(addr
);