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
, int cmd
)
1274 if (ctx
->consumer_error_socket
> 0) {
1275 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1276 sizeof(enum lttcomm_sessiond_command
));
1283 * Close all the tracefiles and stream fds and MUST be called when all
1284 * instances are destroyed i.e. when all threads were joined and are ended.
1286 void lttng_consumer_cleanup(void)
1288 struct lttng_ht_iter iter
;
1289 struct lttng_consumer_channel
*channel
;
1290 unsigned int trace_chunks_left
;
1294 cds_lfht_for_each_entry(the_consumer_data
.channel_ht
->ht
, &iter
.iter
,
1295 channel
, node
.node
) {
1296 consumer_del_channel(channel
);
1301 lttng_ht_destroy(the_consumer_data
.channel_ht
);
1302 lttng_ht_destroy(the_consumer_data
.channels_by_session_id_ht
);
1304 cleanup_relayd_ht();
1306 lttng_ht_destroy(the_consumer_data
.stream_per_chan_id_ht
);
1309 * This HT contains streams that are freed by either the metadata thread or
1310 * the data thread so we do *nothing* on the hash table and simply destroy
1313 lttng_ht_destroy(the_consumer_data
.stream_list_ht
);
1316 * Trace chunks in the registry may still exist if the session
1317 * daemon has encountered an internal error and could not
1318 * tear down its sessions and/or trace chunks properly.
1320 * Release the session daemon's implicit reference to any remaining
1321 * trace chunk and print an error if any trace chunk was found. Note
1322 * that there are _no_ legitimate cases for trace chunks to be left,
1323 * it is a leak. However, it can happen following a crash of the
1324 * session daemon and not emptying the registry would cause an assertion
1327 trace_chunks_left
= lttng_trace_chunk_registry_put_each_chunk(
1328 the_consumer_data
.chunk_registry
);
1329 if (trace_chunks_left
) {
1330 ERR("%u trace chunks are leaked by lttng-consumerd. "
1331 "This can be caused by an internal error of the session daemon.",
1334 /* Run all callbacks freeing each chunk. */
1336 lttng_trace_chunk_registry_destroy(the_consumer_data
.chunk_registry
);
1340 * Called from signal handler.
1342 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1346 CMM_STORE_SHARED(consumer_quit
, 1);
1347 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1349 PERROR("write consumer quit");
1352 DBG("Consumer flag that it should quit");
1357 * Flush pending writes to trace output disk file.
1360 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1364 int outfd
= stream
->out_fd
;
1367 * This does a blocking write-and-wait on any page that belongs to the
1368 * subbuffer prior to the one we just wrote.
1369 * Don't care about error values, as these are just hints and ways to
1370 * limit the amount of page cache used.
1372 if (orig_offset
< stream
->max_sb_size
) {
1375 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1376 stream
->max_sb_size
,
1377 SYNC_FILE_RANGE_WAIT_BEFORE
1378 | SYNC_FILE_RANGE_WRITE
1379 | SYNC_FILE_RANGE_WAIT_AFTER
);
1381 * Give hints to the kernel about how we access the file:
1382 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1385 * We need to call fadvise again after the file grows because the
1386 * kernel does not seem to apply fadvise to non-existing parts of the
1389 * Call fadvise _after_ having waited for the page writeback to
1390 * complete because the dirty page writeback semantic is not well
1391 * defined. So it can be expected to lead to lower throughput in
1394 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1395 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1396 if (ret
&& ret
!= -ENOSYS
) {
1398 PERROR("posix_fadvise on fd %i", outfd
);
1403 * Initialise the necessary environnement :
1404 * - create a new context
1405 * - create the poll_pipe
1406 * - create the should_quit pipe (for signal handler)
1407 * - create the thread pipe (for splice)
1409 * Takes a function pointer as argument, this function is called when data is
1410 * available on a buffer. This function is responsible to do the
1411 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1412 * buffer configuration and then kernctl_put_next_subbuf at the end.
1414 * Returns a pointer to the new context or NULL on error.
1416 struct lttng_consumer_local_data
*lttng_consumer_create(
1417 enum lttng_consumer_type type
,
1418 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1419 struct lttng_consumer_local_data
*ctx
, bool locked_by_caller
),
1420 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1421 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1422 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1425 struct lttng_consumer_local_data
*ctx
;
1427 assert(the_consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1428 the_consumer_data
.type
== type
);
1429 the_consumer_data
.type
= type
;
1431 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1433 PERROR("allocating context");
1437 ctx
->consumer_error_socket
= -1;
1438 ctx
->consumer_metadata_socket
= -1;
1439 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1440 /* assign the callbacks */
1441 ctx
->on_buffer_ready
= buffer_ready
;
1442 ctx
->on_recv_channel
= recv_channel
;
1443 ctx
->on_recv_stream
= recv_stream
;
1444 ctx
->on_update_stream
= update_stream
;
1446 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1447 if (!ctx
->consumer_data_pipe
) {
1448 goto error_poll_pipe
;
1451 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1452 if (!ctx
->consumer_wakeup_pipe
) {
1453 goto error_wakeup_pipe
;
1456 ret
= pipe(ctx
->consumer_should_quit
);
1458 PERROR("Error creating recv pipe");
1459 goto error_quit_pipe
;
1462 ret
= pipe(ctx
->consumer_channel_pipe
);
1464 PERROR("Error creating channel pipe");
1465 goto error_channel_pipe
;
1468 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1469 if (!ctx
->consumer_metadata_pipe
) {
1470 goto error_metadata_pipe
;
1473 ctx
->channel_monitor_pipe
= -1;
1477 error_metadata_pipe
:
1478 utils_close_pipe(ctx
->consumer_channel_pipe
);
1480 utils_close_pipe(ctx
->consumer_should_quit
);
1482 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1484 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1492 * Iterate over all streams of the hashtable and free them properly.
1494 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1496 struct lttng_ht_iter iter
;
1497 struct lttng_consumer_stream
*stream
;
1504 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1506 * Ignore return value since we are currently cleaning up so any error
1509 (void) consumer_del_stream(stream
, ht
);
1513 lttng_ht_destroy(ht
);
1517 * Iterate over all streams of the metadata hashtable and free them
1520 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1522 struct lttng_ht_iter iter
;
1523 struct lttng_consumer_stream
*stream
;
1530 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1532 * Ignore return value since we are currently cleaning up so any error
1535 (void) consumer_del_metadata_stream(stream
, ht
);
1539 lttng_ht_destroy(ht
);
1543 * Close all fds associated with the instance and free the context.
1545 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1549 DBG("Consumer destroying it. Closing everything.");
1555 destroy_data_stream_ht(data_ht
);
1556 destroy_metadata_stream_ht(metadata_ht
);
1558 ret
= close(ctx
->consumer_error_socket
);
1562 ret
= close(ctx
->consumer_metadata_socket
);
1566 utils_close_pipe(ctx
->consumer_channel_pipe
);
1567 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1568 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1569 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1570 utils_close_pipe(ctx
->consumer_should_quit
);
1572 unlink(ctx
->consumer_command_sock_path
);
1577 * Write the metadata stream id on the specified file descriptor.
1579 static int write_relayd_metadata_id(int fd
,
1580 struct lttng_consumer_stream
*stream
,
1581 unsigned long padding
)
1584 struct lttcomm_relayd_metadata_payload hdr
;
1586 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1587 hdr
.padding_size
= htobe32(padding
);
1588 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1589 if (ret
< sizeof(hdr
)) {
1591 * This error means that the fd's end is closed so ignore the PERROR
1592 * not to clubber the error output since this can happen in a normal
1595 if (errno
!= EPIPE
) {
1596 PERROR("write metadata stream id");
1598 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1600 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1601 * handle writting the missing part so report that as an error and
1602 * don't lie to the caller.
1607 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1608 stream
->relayd_stream_id
, padding
);
1615 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1616 * core function for writing trace buffers to either the local filesystem or
1619 * It must be called with the stream and the channel lock held.
1621 * Careful review MUST be put if any changes occur!
1623 * Returns the number of bytes written
1625 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1626 struct lttng_consumer_stream
*stream
,
1627 const struct lttng_buffer_view
*buffer
,
1628 unsigned long padding
)
1631 off_t orig_offset
= stream
->out_fd_offset
;
1632 /* Default is on the disk */
1633 int outfd
= stream
->out_fd
;
1634 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1635 unsigned int relayd_hang_up
= 0;
1636 const size_t subbuf_content_size
= buffer
->size
- padding
;
1639 /* RCU lock for the relayd pointer */
1641 assert(stream
->net_seq_idx
!= (uint64_t) -1ULL ||
1642 stream
->trace_chunk
);
1644 /* Flag that the current stream if set for network streaming. */
1645 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1646 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1647 if (relayd
== NULL
) {
1653 /* Handle stream on the relayd if the output is on the network */
1655 unsigned long netlen
= subbuf_content_size
;
1658 * Lock the control socket for the complete duration of the function
1659 * since from this point on we will use the socket.
1661 if (stream
->metadata_flag
) {
1662 /* Metadata requires the control socket. */
1663 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1664 if (stream
->reset_metadata_flag
) {
1665 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1666 stream
->relayd_stream_id
,
1667 stream
->metadata_version
);
1672 stream
->reset_metadata_flag
= 0;
1674 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1677 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1682 /* Use the returned socket. */
1685 /* Write metadata stream id before payload */
1686 if (stream
->metadata_flag
) {
1687 ret
= write_relayd_metadata_id(outfd
, stream
, padding
);
1694 write_len
= subbuf_content_size
;
1696 /* No streaming; we have to write the full padding. */
1697 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1698 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1700 ERR("Reset metadata file");
1703 stream
->reset_metadata_flag
= 0;
1707 * Check if we need to change the tracefile before writing the packet.
1709 if (stream
->chan
->tracefile_size
> 0 &&
1710 (stream
->tracefile_size_current
+ buffer
->size
) >
1711 stream
->chan
->tracefile_size
) {
1712 ret
= consumer_stream_rotate_output_files(stream
);
1716 outfd
= stream
->out_fd
;
1719 stream
->tracefile_size_current
+= buffer
->size
;
1720 write_len
= buffer
->size
;
1724 * This call guarantee that len or less is returned. It's impossible to
1725 * receive a ret value that is bigger than len.
1727 ret
= lttng_write(outfd
, buffer
->data
, write_len
);
1728 DBG("Consumer mmap write() ret %zd (len %zu)", ret
, write_len
);
1729 if (ret
< 0 || ((size_t) ret
!= write_len
)) {
1731 * Report error to caller if nothing was written else at least send the
1739 /* Socket operation failed. We consider the relayd dead */
1740 if (errno
== EPIPE
) {
1742 * This is possible if the fd is closed on the other side
1743 * (outfd) or any write problem. It can be verbose a bit for a
1744 * normal execution if for instance the relayd is stopped
1745 * abruptly. This can happen so set this to a DBG statement.
1747 DBG("Consumer mmap write detected relayd hang up");
1749 /* Unhandled error, print it and stop function right now. */
1750 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret
,
1755 stream
->output_written
+= ret
;
1757 /* This call is useless on a socket so better save a syscall. */
1759 /* This won't block, but will start writeout asynchronously */
1760 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, write_len
,
1761 SYNC_FILE_RANGE_WRITE
);
1762 stream
->out_fd_offset
+= write_len
;
1763 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1768 * This is a special case that the relayd has closed its socket. Let's
1769 * cleanup the relayd object and all associated streams.
1771 if (relayd
&& relayd_hang_up
) {
1772 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1773 lttng_consumer_cleanup_relayd(relayd
);
1777 /* Unlock only if ctrl socket used */
1778 if (relayd
&& stream
->metadata_flag
) {
1779 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1787 * Splice the data from the ring buffer to the tracefile.
1789 * It must be called with the stream lock held.
1791 * Returns the number of bytes spliced.
1793 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1794 struct lttng_consumer_local_data
*ctx
,
1795 struct lttng_consumer_stream
*stream
, unsigned long len
,
1796 unsigned long padding
)
1798 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1800 off_t orig_offset
= stream
->out_fd_offset
;
1801 int fd
= stream
->wait_fd
;
1802 /* Default is on the disk */
1803 int outfd
= stream
->out_fd
;
1804 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1806 unsigned int relayd_hang_up
= 0;
1808 switch (the_consumer_data
.type
) {
1809 case LTTNG_CONSUMER_KERNEL
:
1811 case LTTNG_CONSUMER32_UST
:
1812 case LTTNG_CONSUMER64_UST
:
1813 /* Not supported for user space tracing */
1816 ERR("Unknown consumer_data type");
1820 /* RCU lock for the relayd pointer */
1823 /* Flag that the current stream if set for network streaming. */
1824 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1825 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1826 if (relayd
== NULL
) {
1831 splice_pipe
= stream
->splice_pipe
;
1833 /* Write metadata stream id before payload */
1835 unsigned long total_len
= len
;
1837 if (stream
->metadata_flag
) {
1839 * Lock the control socket for the complete duration of the function
1840 * since from this point on we will use the socket.
1842 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1844 if (stream
->reset_metadata_flag
) {
1845 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1846 stream
->relayd_stream_id
,
1847 stream
->metadata_version
);
1852 stream
->reset_metadata_flag
= 0;
1854 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
,
1862 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1865 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1871 /* Use the returned socket. */
1874 /* No streaming, we have to set the len with the full padding */
1877 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1878 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1880 ERR("Reset metadata file");
1883 stream
->reset_metadata_flag
= 0;
1886 * Check if we need to change the tracefile before writing the packet.
1888 if (stream
->chan
->tracefile_size
> 0 &&
1889 (stream
->tracefile_size_current
+ len
) >
1890 stream
->chan
->tracefile_size
) {
1891 ret
= consumer_stream_rotate_output_files(stream
);
1896 outfd
= stream
->out_fd
;
1899 stream
->tracefile_size_current
+= len
;
1903 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1904 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1905 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1906 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1907 DBG("splice chan to pipe, ret %zd", ret_splice
);
1908 if (ret_splice
< 0) {
1911 PERROR("Error in relay splice");
1915 /* Handle stream on the relayd if the output is on the network */
1916 if (relayd
&& stream
->metadata_flag
) {
1917 size_t metadata_payload_size
=
1918 sizeof(struct lttcomm_relayd_metadata_payload
);
1920 /* Update counter to fit the spliced data */
1921 ret_splice
+= metadata_payload_size
;
1922 len
+= metadata_payload_size
;
1924 * We do this so the return value can match the len passed as
1925 * argument to this function.
1927 written
-= metadata_payload_size
;
1930 /* Splice data out */
1931 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1932 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1933 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1935 if (ret_splice
< 0) {
1940 } else if (ret_splice
> len
) {
1942 * We don't expect this code path to be executed but you never know
1943 * so this is an extra protection agains a buggy splice().
1946 written
+= ret_splice
;
1947 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1951 /* All good, update current len and continue. */
1955 /* This call is useless on a socket so better save a syscall. */
1957 /* This won't block, but will start writeout asynchronously */
1958 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1959 SYNC_FILE_RANGE_WRITE
);
1960 stream
->out_fd_offset
+= ret_splice
;
1962 stream
->output_written
+= ret_splice
;
1963 written
+= ret_splice
;
1966 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1972 * This is a special case that the relayd has closed its socket. Let's
1973 * cleanup the relayd object and all associated streams.
1975 if (relayd
&& relayd_hang_up
) {
1976 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1977 lttng_consumer_cleanup_relayd(relayd
);
1978 /* Skip splice error so the consumer does not fail */
1983 /* send the appropriate error description to sessiond */
1986 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1989 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1992 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1997 if (relayd
&& stream
->metadata_flag
) {
1998 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2006 * Sample the snapshot positions for a specific fd
2008 * Returns 0 on success, < 0 on error
2010 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
2012 switch (the_consumer_data
.type
) {
2013 case LTTNG_CONSUMER_KERNEL
:
2014 return lttng_kconsumer_sample_snapshot_positions(stream
);
2015 case LTTNG_CONSUMER32_UST
:
2016 case LTTNG_CONSUMER64_UST
:
2017 return lttng_ustconsumer_sample_snapshot_positions(stream
);
2019 ERR("Unknown consumer_data type");
2025 * Take a snapshot for a specific fd
2027 * Returns 0 on success, < 0 on error
2029 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2031 switch (the_consumer_data
.type
) {
2032 case LTTNG_CONSUMER_KERNEL
:
2033 return lttng_kconsumer_take_snapshot(stream
);
2034 case LTTNG_CONSUMER32_UST
:
2035 case LTTNG_CONSUMER64_UST
:
2036 return lttng_ustconsumer_take_snapshot(stream
);
2038 ERR("Unknown consumer_data type");
2045 * Get the produced position
2047 * Returns 0 on success, < 0 on error
2049 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
2052 switch (the_consumer_data
.type
) {
2053 case LTTNG_CONSUMER_KERNEL
:
2054 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
2055 case LTTNG_CONSUMER32_UST
:
2056 case LTTNG_CONSUMER64_UST
:
2057 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2059 ERR("Unknown consumer_data type");
2066 * Get the consumed position (free-running counter position in bytes).
2068 * Returns 0 on success, < 0 on error
2070 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
2073 switch (the_consumer_data
.type
) {
2074 case LTTNG_CONSUMER_KERNEL
:
2075 return lttng_kconsumer_get_consumed_snapshot(stream
, pos
);
2076 case LTTNG_CONSUMER32_UST
:
2077 case LTTNG_CONSUMER64_UST
:
2078 return lttng_ustconsumer_get_consumed_snapshot(stream
, pos
);
2080 ERR("Unknown consumer_data type");
2086 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2087 int sock
, struct pollfd
*consumer_sockpoll
)
2089 switch (the_consumer_data
.type
) {
2090 case LTTNG_CONSUMER_KERNEL
:
2091 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2092 case LTTNG_CONSUMER32_UST
:
2093 case LTTNG_CONSUMER64_UST
:
2094 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2096 ERR("Unknown consumer_data type");
2103 void lttng_consumer_close_all_metadata(void)
2105 switch (the_consumer_data
.type
) {
2106 case LTTNG_CONSUMER_KERNEL
:
2108 * The Kernel consumer has a different metadata scheme so we don't
2109 * close anything because the stream will be closed by the session
2113 case LTTNG_CONSUMER32_UST
:
2114 case LTTNG_CONSUMER64_UST
:
2116 * Close all metadata streams. The metadata hash table is passed and
2117 * this call iterates over it by closing all wakeup fd. This is safe
2118 * because at this point we are sure that the metadata producer is
2119 * either dead or blocked.
2121 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2124 ERR("Unknown consumer_data type");
2130 * Clean up a metadata stream and free its memory.
2132 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2133 struct lttng_ht
*ht
)
2135 struct lttng_consumer_channel
*channel
= NULL
;
2136 bool free_channel
= false;
2140 * This call should NEVER receive regular stream. It must always be
2141 * metadata stream and this is crucial for data structure synchronization.
2143 assert(stream
->metadata_flag
);
2145 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2147 pthread_mutex_lock(&the_consumer_data
.lock
);
2149 * Note that this assumes that a stream's channel is never changed and
2150 * that the stream's lock doesn't need to be taken to sample its
2153 channel
= stream
->chan
;
2154 pthread_mutex_lock(&channel
->lock
);
2155 pthread_mutex_lock(&stream
->lock
);
2156 if (channel
->metadata_cache
) {
2157 /* Only applicable to userspace consumers. */
2158 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
2161 /* Remove any reference to that stream. */
2162 consumer_stream_delete(stream
, ht
);
2164 /* Close down everything including the relayd if one. */
2165 consumer_stream_close(stream
);
2166 /* Destroy tracer buffers of the stream. */
2167 consumer_stream_destroy_buffers(stream
);
2169 /* Atomically decrement channel refcount since other threads can use it. */
2170 if (!uatomic_sub_return(&channel
->refcount
, 1)
2171 && !uatomic_read(&channel
->nb_init_stream_left
)) {
2172 /* Go for channel deletion! */
2173 free_channel
= true;
2175 stream
->chan
= NULL
;
2178 * Nullify the stream reference so it is not used after deletion. The
2179 * channel lock MUST be acquired before being able to check for a NULL
2182 channel
->metadata_stream
= NULL
;
2183 lttng_wait_queue_wake_all(&channel
->metadata_pushed_wait_queue
);
2185 if (channel
->metadata_cache
) {
2186 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
2188 pthread_mutex_unlock(&stream
->lock
);
2189 pthread_mutex_unlock(&channel
->lock
);
2190 pthread_mutex_unlock(&the_consumer_data
.lock
);
2193 consumer_del_channel(channel
);
2196 lttng_trace_chunk_put(stream
->trace_chunk
);
2197 stream
->trace_chunk
= NULL
;
2198 consumer_stream_free(stream
);
2202 * Action done with the metadata stream when adding it to the consumer internal
2203 * data structures to handle it.
2205 void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2207 struct lttng_ht
*ht
= metadata_ht
;
2208 struct lttng_ht_iter iter
;
2209 struct lttng_ht_node_u64
*node
;
2214 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2216 pthread_mutex_lock(&the_consumer_data
.lock
);
2217 pthread_mutex_lock(&stream
->chan
->lock
);
2218 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2219 pthread_mutex_lock(&stream
->lock
);
2222 * From here, refcounts are updated so be _careful_ when returning an error
2229 * Lookup the stream just to make sure it does not exist in our internal
2230 * state. This should NEVER happen.
2232 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2233 node
= lttng_ht_iter_get_node_u64(&iter
);
2237 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2238 * in terms of destroying the associated channel, because the action that
2239 * causes the count to become 0 also causes a stream to be added. The
2240 * channel deletion will thus be triggered by the following removal of this
2243 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2244 /* Increment refcount before decrementing nb_init_stream_left */
2246 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2249 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2251 lttng_ht_add_u64(the_consumer_data
.stream_per_chan_id_ht
,
2252 &stream
->node_channel_id
);
2255 * Add stream to the stream_list_ht of the consumer data. No need to steal
2256 * the key since the HT does not use it and we allow to add redundant keys
2259 lttng_ht_add_u64(the_consumer_data
.stream_list_ht
,
2260 &stream
->node_session_id
);
2264 pthread_mutex_unlock(&stream
->lock
);
2265 pthread_mutex_unlock(&stream
->chan
->lock
);
2266 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2267 pthread_mutex_unlock(&the_consumer_data
.lock
);
2271 * Delete data stream that are flagged for deletion (endpoint_status).
2273 static void validate_endpoint_status_data_stream(void)
2275 struct lttng_ht_iter iter
;
2276 struct lttng_consumer_stream
*stream
;
2278 DBG("Consumer delete flagged data stream");
2281 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2282 /* Validate delete flag of the stream */
2283 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2286 /* Delete it right now */
2287 consumer_del_stream(stream
, data_ht
);
2293 * Delete metadata stream that are flagged for deletion (endpoint_status).
2295 static void validate_endpoint_status_metadata_stream(
2296 struct lttng_poll_event
*pollset
)
2298 struct lttng_ht_iter iter
;
2299 struct lttng_consumer_stream
*stream
;
2301 DBG("Consumer delete flagged metadata stream");
2306 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2307 /* Validate delete flag of the stream */
2308 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2312 * Remove from pollset so the metadata thread can continue without
2313 * blocking on a deleted stream.
2315 lttng_poll_del(pollset
, stream
->wait_fd
);
2317 /* Delete it right now */
2318 consumer_del_metadata_stream(stream
, metadata_ht
);
2324 * Thread polls on metadata file descriptor and write them on disk or on the
2327 void *consumer_thread_metadata_poll(void *data
)
2329 int ret
, i
, pollfd
, err
= -1;
2330 uint32_t revents
, nb_fd
;
2331 struct lttng_consumer_stream
*stream
= NULL
;
2332 struct lttng_ht_iter iter
;
2333 struct lttng_ht_node_u64
*node
;
2334 struct lttng_poll_event events
;
2335 struct lttng_consumer_local_data
*ctx
= data
;
2338 rcu_register_thread();
2340 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2342 if (testpoint(consumerd_thread_metadata
)) {
2343 goto error_testpoint
;
2346 health_code_update();
2348 DBG("Thread metadata poll started");
2350 /* Size is set to 1 for the consumer_metadata pipe */
2351 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2353 ERR("Poll set creation failed");
2357 ret
= lttng_poll_add(&events
,
2358 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2364 DBG("Metadata main loop started");
2368 health_code_update();
2369 health_poll_entry();
2370 DBG("Metadata poll wait");
2371 ret
= lttng_poll_wait(&events
, -1);
2372 DBG("Metadata poll return from wait with %d fd(s)",
2373 LTTNG_POLL_GETNB(&events
));
2375 DBG("Metadata event caught in thread");
2377 if (errno
== EINTR
) {
2378 ERR("Poll EINTR caught");
2381 if (LTTNG_POLL_GETNB(&events
) == 0) {
2382 err
= 0; /* All is OK */
2389 /* From here, the event is a metadata wait fd */
2390 for (i
= 0; i
< nb_fd
; i
++) {
2391 health_code_update();
2393 revents
= LTTNG_POLL_GETEV(&events
, i
);
2394 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2396 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2397 if (revents
& LPOLLIN
) {
2400 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2401 &stream
, sizeof(stream
));
2402 if (pipe_len
< sizeof(stream
)) {
2404 PERROR("read metadata stream");
2407 * Remove the pipe from the poll set and continue the loop
2408 * since their might be data to consume.
2410 lttng_poll_del(&events
,
2411 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2412 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2416 /* A NULL stream means that the state has changed. */
2417 if (stream
== NULL
) {
2418 /* Check for deleted streams. */
2419 validate_endpoint_status_metadata_stream(&events
);
2423 DBG("Adding metadata stream %d to poll set",
2426 /* Add metadata stream to the global poll events list */
2427 lttng_poll_add(&events
, stream
->wait_fd
,
2428 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2429 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2430 DBG("Metadata thread pipe hung up");
2432 * Remove the pipe from the poll set and continue the loop
2433 * since their might be data to consume.
2435 lttng_poll_del(&events
,
2436 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2437 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2440 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2444 /* Handle other stream */
2450 uint64_t tmp_id
= (uint64_t) pollfd
;
2452 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2454 node
= lttng_ht_iter_get_node_u64(&iter
);
2457 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2460 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2461 /* Get the data out of the metadata file descriptor */
2462 DBG("Metadata available on fd %d", pollfd
);
2463 assert(stream
->wait_fd
== pollfd
);
2466 health_code_update();
2468 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2470 * We don't check the return value here since if we get
2471 * a negative len, it means an error occurred thus we
2472 * simply remove it from the poll set and free the
2477 /* It's ok to have an unavailable sub-buffer */
2478 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2479 /* Clean up stream from consumer and free it. */
2480 lttng_poll_del(&events
, stream
->wait_fd
);
2481 consumer_del_metadata_stream(stream
, metadata_ht
);
2483 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2484 DBG("Metadata fd %d is hup|err.", pollfd
);
2485 if (!stream
->hangup_flush_done
&&
2486 (the_consumer_data
.type
== LTTNG_CONSUMER32_UST
||
2487 the_consumer_data
.type
==
2488 LTTNG_CONSUMER64_UST
)) {
2489 DBG("Attempting to flush and consume the UST buffers");
2490 lttng_ustconsumer_on_stream_hangup(stream
);
2492 /* We just flushed the stream now read it. */
2494 health_code_update();
2496 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2498 * We don't check the return value here since if we get
2499 * a negative len, it means an error occurred thus we
2500 * simply remove it from the poll set and free the
2506 lttng_poll_del(&events
, stream
->wait_fd
);
2508 * This call update the channel states, closes file descriptors
2509 * and securely free the stream.
2511 consumer_del_metadata_stream(stream
, metadata_ht
);
2513 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2517 /* Release RCU lock for the stream looked up */
2525 DBG("Metadata poll thread exiting");
2527 lttng_poll_clean(&events
);
2532 ERR("Health error occurred in %s", __func__
);
2534 health_unregister(health_consumerd
);
2535 rcu_unregister_thread();
2540 * This thread polls the fds in the set to consume the data and write
2541 * it to tracefile if necessary.
2543 void *consumer_thread_data_poll(void *data
)
2545 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2546 struct pollfd
*pollfd
= NULL
;
2547 /* local view of the streams */
2548 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2549 /* local view of consumer_data.fds_count */
2551 /* 2 for the consumer_data_pipe and wake up pipe */
2552 const int nb_pipes_fd
= 2;
2553 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2554 int nb_inactive_fd
= 0;
2555 struct lttng_consumer_local_data
*ctx
= data
;
2558 rcu_register_thread();
2560 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2562 if (testpoint(consumerd_thread_data
)) {
2563 goto error_testpoint
;
2566 health_code_update();
2568 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2569 if (local_stream
== NULL
) {
2570 PERROR("local_stream malloc");
2575 health_code_update();
2581 * the fds set has been updated, we need to update our
2582 * local array as well
2584 pthread_mutex_lock(&the_consumer_data
.lock
);
2585 if (the_consumer_data
.need_update
) {
2590 local_stream
= NULL
;
2592 /* Allocate for all fds */
2593 pollfd
= zmalloc((the_consumer_data
.stream_count
+
2595 sizeof(struct pollfd
));
2596 if (pollfd
== NULL
) {
2597 PERROR("pollfd malloc");
2598 pthread_mutex_unlock(&the_consumer_data
.lock
);
2602 local_stream
= zmalloc((the_consumer_data
.stream_count
+
2604 sizeof(struct lttng_consumer_stream
*));
2605 if (local_stream
== NULL
) {
2606 PERROR("local_stream malloc");
2607 pthread_mutex_unlock(&the_consumer_data
.lock
);
2610 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2611 data_ht
, &nb_inactive_fd
);
2613 ERR("Error in allocating pollfd or local_outfds");
2614 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2615 pthread_mutex_unlock(&the_consumer_data
.lock
);
2619 the_consumer_data
.need_update
= 0;
2621 pthread_mutex_unlock(&the_consumer_data
.lock
);
2623 /* No FDs and consumer_quit, consumer_cleanup the thread */
2624 if (nb_fd
== 0 && nb_inactive_fd
== 0 &&
2625 CMM_LOAD_SHARED(consumer_quit
) == 1) {
2626 err
= 0; /* All is OK */
2629 /* poll on the array of fds */
2631 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2632 if (testpoint(consumerd_thread_data_poll
)) {
2635 health_poll_entry();
2636 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2638 DBG("poll num_rdy : %d", num_rdy
);
2639 if (num_rdy
== -1) {
2641 * Restart interrupted system call.
2643 if (errno
== EINTR
) {
2646 PERROR("Poll error");
2647 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2649 } else if (num_rdy
== 0) {
2650 DBG("Polling thread timed out");
2654 if (caa_unlikely(data_consumption_paused
)) {
2655 DBG("Data consumption paused, sleeping...");
2661 * If the consumer_data_pipe triggered poll go directly to the
2662 * beginning of the loop to update the array. We want to prioritize
2663 * array update over low-priority reads.
2665 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2666 ssize_t pipe_readlen
;
2668 DBG("consumer_data_pipe wake up");
2669 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2670 &new_stream
, sizeof(new_stream
));
2671 if (pipe_readlen
< sizeof(new_stream
)) {
2672 PERROR("Consumer data pipe");
2673 /* Continue so we can at least handle the current stream(s). */
2678 * If the stream is NULL, just ignore it. It's also possible that
2679 * the sessiond poll thread changed the consumer_quit state and is
2680 * waking us up to test it.
2682 if (new_stream
== NULL
) {
2683 validate_endpoint_status_data_stream();
2687 /* Continue to update the local streams and handle prio ones */
2691 /* Handle wakeup pipe. */
2692 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2694 ssize_t pipe_readlen
;
2696 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2698 if (pipe_readlen
< 0) {
2699 PERROR("Consumer data wakeup pipe");
2701 /* We've been awakened to handle stream(s). */
2702 ctx
->has_wakeup
= 0;
2705 /* Take care of high priority channels first. */
2706 for (i
= 0; i
< nb_fd
; i
++) {
2707 health_code_update();
2709 if (local_stream
[i
] == NULL
) {
2712 if (pollfd
[i
].revents
& POLLPRI
) {
2713 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2715 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2716 /* it's ok to have an unavailable sub-buffer */
2717 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2718 /* Clean the stream and free it. */
2719 consumer_del_stream(local_stream
[i
], data_ht
);
2720 local_stream
[i
] = NULL
;
2721 } else if (len
> 0) {
2722 local_stream
[i
]->data_read
= 1;
2728 * If we read high prio channel in this loop, try again
2729 * for more high prio data.
2735 /* Take care of low priority channels. */
2736 for (i
= 0; i
< nb_fd
; i
++) {
2737 health_code_update();
2739 if (local_stream
[i
] == NULL
) {
2742 if ((pollfd
[i
].revents
& POLLIN
) ||
2743 local_stream
[i
]->hangup_flush_done
||
2744 local_stream
[i
]->has_data
) {
2745 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2746 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2747 /* it's ok to have an unavailable sub-buffer */
2748 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2749 /* Clean the stream and free it. */
2750 consumer_del_stream(local_stream
[i
], data_ht
);
2751 local_stream
[i
] = NULL
;
2752 } else if (len
> 0) {
2753 local_stream
[i
]->data_read
= 1;
2758 /* Handle hangup and errors */
2759 for (i
= 0; i
< nb_fd
; i
++) {
2760 health_code_update();
2762 if (local_stream
[i
] == NULL
) {
2765 if (!local_stream
[i
]->hangup_flush_done
2766 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2767 && (the_consumer_data
.type
== LTTNG_CONSUMER32_UST
2768 || the_consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2769 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2771 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2772 /* Attempt read again, for the data we just flushed. */
2773 local_stream
[i
]->data_read
= 1;
2776 * If the poll flag is HUP/ERR/NVAL and we have
2777 * read no data in this pass, we can remove the
2778 * stream from its hash table.
2780 if ((pollfd
[i
].revents
& POLLHUP
)) {
2781 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2782 if (!local_stream
[i
]->data_read
) {
2783 consumer_del_stream(local_stream
[i
], data_ht
);
2784 local_stream
[i
] = NULL
;
2787 } else if (pollfd
[i
].revents
& POLLERR
) {
2788 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2789 if (!local_stream
[i
]->data_read
) {
2790 consumer_del_stream(local_stream
[i
], data_ht
);
2791 local_stream
[i
] = NULL
;
2794 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2795 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2796 if (!local_stream
[i
]->data_read
) {
2797 consumer_del_stream(local_stream
[i
], data_ht
);
2798 local_stream
[i
] = NULL
;
2802 if (local_stream
[i
] != NULL
) {
2803 local_stream
[i
]->data_read
= 0;
2810 DBG("polling thread exiting");
2815 * Close the write side of the pipe so epoll_wait() in
2816 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2817 * read side of the pipe. If we close them both, epoll_wait strangely does
2818 * not return and could create a endless wait period if the pipe is the
2819 * only tracked fd in the poll set. The thread will take care of closing
2822 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2827 ERR("Health error occurred in %s", __func__
);
2829 health_unregister(health_consumerd
);
2831 rcu_unregister_thread();
2836 * Close wake-up end of each stream belonging to the channel. This will
2837 * allow the poll() on the stream read-side to detect when the
2838 * write-side (application) finally closes them.
2841 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2843 struct lttng_ht
*ht
;
2844 struct lttng_consumer_stream
*stream
;
2845 struct lttng_ht_iter iter
;
2847 ht
= the_consumer_data
.stream_per_chan_id_ht
;
2850 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2851 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2852 ht
->match_fct
, &channel
->key
,
2853 &iter
.iter
, stream
, node_channel_id
.node
) {
2855 * Protect against teardown with mutex.
2857 pthread_mutex_lock(&stream
->lock
);
2858 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2861 switch (the_consumer_data
.type
) {
2862 case LTTNG_CONSUMER_KERNEL
:
2864 case LTTNG_CONSUMER32_UST
:
2865 case LTTNG_CONSUMER64_UST
:
2866 if (stream
->metadata_flag
) {
2867 /* Safe and protected by the stream lock. */
2868 lttng_ustconsumer_close_metadata(stream
->chan
);
2871 * Note: a mutex is taken internally within
2872 * liblttng-ust-ctl to protect timer wakeup_fd
2873 * use from concurrent close.
2875 lttng_ustconsumer_close_stream_wakeup(stream
);
2879 ERR("Unknown consumer_data type");
2883 pthread_mutex_unlock(&stream
->lock
);
2888 static void destroy_channel_ht(struct lttng_ht
*ht
)
2890 struct lttng_ht_iter iter
;
2891 struct lttng_consumer_channel
*channel
;
2899 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2900 ret
= lttng_ht_del(ht
, &iter
);
2905 lttng_ht_destroy(ht
);
2909 * This thread polls the channel fds to detect when they are being
2910 * closed. It closes all related streams if the channel is detected as
2911 * closed. It is currently only used as a shim layer for UST because the
2912 * consumerd needs to keep the per-stream wakeup end of pipes open for
2915 void *consumer_thread_channel_poll(void *data
)
2917 int ret
, i
, pollfd
, err
= -1;
2918 uint32_t revents
, nb_fd
;
2919 struct lttng_consumer_channel
*chan
= NULL
;
2920 struct lttng_ht_iter iter
;
2921 struct lttng_ht_node_u64
*node
;
2922 struct lttng_poll_event events
;
2923 struct lttng_consumer_local_data
*ctx
= data
;
2924 struct lttng_ht
*channel_ht
;
2926 rcu_register_thread();
2928 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2930 if (testpoint(consumerd_thread_channel
)) {
2931 goto error_testpoint
;
2934 health_code_update();
2936 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2938 /* ENOMEM at this point. Better to bail out. */
2942 DBG("Thread channel poll started");
2944 /* Size is set to 1 for the consumer_channel pipe */
2945 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2947 ERR("Poll set creation failed");
2951 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2957 DBG("Channel main loop started");
2961 health_code_update();
2962 DBG("Channel poll wait");
2963 health_poll_entry();
2964 ret
= lttng_poll_wait(&events
, -1);
2965 DBG("Channel poll return from wait with %d fd(s)",
2966 LTTNG_POLL_GETNB(&events
));
2968 DBG("Channel event caught in thread");
2970 if (errno
== EINTR
) {
2971 ERR("Poll EINTR caught");
2974 if (LTTNG_POLL_GETNB(&events
) == 0) {
2975 err
= 0; /* All is OK */
2982 /* From here, the event is a channel wait fd */
2983 for (i
= 0; i
< nb_fd
; i
++) {
2984 health_code_update();
2986 revents
= LTTNG_POLL_GETEV(&events
, i
);
2987 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2989 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2990 if (revents
& LPOLLIN
) {
2991 enum consumer_channel_action action
;
2994 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2997 ERR("Error reading channel pipe");
2999 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3004 case CONSUMER_CHANNEL_ADD
:
3005 DBG("Adding channel %d to poll set",
3008 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
3011 lttng_ht_add_unique_u64(channel_ht
,
3012 &chan
->wait_fd_node
);
3014 /* Add channel to the global poll events list */
3015 lttng_poll_add(&events
, chan
->wait_fd
,
3016 LPOLLERR
| LPOLLHUP
);
3018 case CONSUMER_CHANNEL_DEL
:
3021 * This command should never be called if the channel
3022 * has streams monitored by either the data or metadata
3023 * thread. The consumer only notify this thread with a
3024 * channel del. command if it receives a destroy
3025 * channel command from the session daemon that send it
3026 * if a command prior to the GET_CHANNEL failed.
3030 chan
= consumer_find_channel(key
);
3033 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
3036 lttng_poll_del(&events
, chan
->wait_fd
);
3037 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
3038 ret
= lttng_ht_del(channel_ht
, &iter
);
3041 switch (the_consumer_data
.type
) {
3042 case LTTNG_CONSUMER_KERNEL
:
3044 case LTTNG_CONSUMER32_UST
:
3045 case LTTNG_CONSUMER64_UST
:
3046 health_code_update();
3047 /* Destroy streams that might have been left in the stream list. */
3048 clean_channel_stream_list(chan
);
3051 ERR("Unknown consumer_data type");
3056 * Release our own refcount. Force channel deletion even if
3057 * streams were not initialized.
3059 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3060 consumer_del_channel(chan
);
3065 case CONSUMER_CHANNEL_QUIT
:
3067 * Remove the pipe from the poll set and continue the loop
3068 * since their might be data to consume.
3070 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3073 ERR("Unknown action");
3076 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3077 DBG("Channel thread pipe hung up");
3079 * Remove the pipe from the poll set and continue the loop
3080 * since their might be data to consume.
3082 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3085 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3089 /* Handle other stream */
3095 uint64_t tmp_id
= (uint64_t) pollfd
;
3097 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3099 node
= lttng_ht_iter_get_node_u64(&iter
);
3102 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
3105 /* Check for error event */
3106 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3107 DBG("Channel fd %d is hup|err.", pollfd
);
3109 lttng_poll_del(&events
, chan
->wait_fd
);
3110 ret
= lttng_ht_del(channel_ht
, &iter
);
3114 * This will close the wait fd for each stream associated to
3115 * this channel AND monitored by the data/metadata thread thus
3116 * will be clean by the right thread.
3118 consumer_close_channel_streams(chan
);
3120 /* Release our own refcount */
3121 if (!uatomic_sub_return(&chan
->refcount
, 1)
3122 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3123 consumer_del_channel(chan
);
3126 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3131 /* Release RCU lock for the channel looked up */
3139 lttng_poll_clean(&events
);
3141 destroy_channel_ht(channel_ht
);
3144 DBG("Channel poll thread exiting");
3147 ERR("Health error occurred in %s", __func__
);
3149 health_unregister(health_consumerd
);
3150 rcu_unregister_thread();
3154 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3155 struct pollfd
*sockpoll
, int client_socket
)
3162 ret
= lttng_consumer_poll_socket(sockpoll
);
3166 DBG("Metadata connection on client_socket");
3168 /* Blocking call, waiting for transmission */
3169 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3170 if (ctx
->consumer_metadata_socket
< 0) {
3171 WARN("On accept metadata");
3182 * This thread listens on the consumerd socket and receives the file
3183 * descriptors from the session daemon.
3185 void *consumer_thread_sessiond_poll(void *data
)
3187 int sock
= -1, client_socket
, ret
, err
= -1;
3189 * structure to poll for incoming data on communication socket avoids
3190 * making blocking sockets.
3192 struct pollfd consumer_sockpoll
[2];
3193 struct lttng_consumer_local_data
*ctx
= data
;
3195 rcu_register_thread();
3197 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3199 if (testpoint(consumerd_thread_sessiond
)) {
3200 goto error_testpoint
;
3203 health_code_update();
3205 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3206 unlink(ctx
->consumer_command_sock_path
);
3207 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3208 if (client_socket
< 0) {
3209 ERR("Cannot create command socket");
3213 ret
= lttcomm_listen_unix_sock(client_socket
);
3218 DBG("Sending ready command to lttng-sessiond");
3219 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3220 /* return < 0 on error, but == 0 is not fatal */
3222 ERR("Error sending ready command to lttng-sessiond");
3226 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3227 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3228 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3229 consumer_sockpoll
[1].fd
= client_socket
;
3230 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3232 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3240 DBG("Connection on client_socket");
3242 /* Blocking call, waiting for transmission */
3243 sock
= lttcomm_accept_unix_sock(client_socket
);
3250 * Setup metadata socket which is the second socket connection on the
3251 * command unix socket.
3253 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3262 /* This socket is not useful anymore. */
3263 ret
= close(client_socket
);
3265 PERROR("close client_socket");
3269 /* update the polling structure to poll on the established socket */
3270 consumer_sockpoll
[1].fd
= sock
;
3271 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3274 health_code_update();
3276 health_poll_entry();
3277 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3286 DBG("Incoming command on sock");
3287 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3290 * This could simply be a session daemon quitting. Don't output
3293 DBG("Communication interrupted on command socket");
3297 if (CMM_LOAD_SHARED(consumer_quit
)) {
3298 DBG("consumer_thread_receive_fds received quit from signal");
3299 err
= 0; /* All is OK */
3302 DBG("Received command on sock");
3308 DBG("Consumer thread sessiond poll exiting");
3311 * Close metadata streams since the producer is the session daemon which
3314 * NOTE: for now, this only applies to the UST tracer.
3316 lttng_consumer_close_all_metadata();
3319 * when all fds have hung up, the polling thread
3322 CMM_STORE_SHARED(consumer_quit
, 1);
3325 * Notify the data poll thread to poll back again and test the
3326 * consumer_quit state that we just set so to quit gracefully.
3328 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3330 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3332 notify_health_quit_pipe(health_quit_pipe
);
3334 /* Cleaning up possibly open sockets. */
3338 PERROR("close sock sessiond poll");
3341 if (client_socket
>= 0) {
3342 ret
= close(client_socket
);
3344 PERROR("close client_socket sessiond poll");
3351 ERR("Health error occurred in %s", __func__
);
3353 health_unregister(health_consumerd
);
3355 rcu_unregister_thread();
3359 static int post_consume(struct lttng_consumer_stream
*stream
,
3360 const struct stream_subbuffer
*subbuffer
,
3361 struct lttng_consumer_local_data
*ctx
)
3365 const size_t count
= lttng_dynamic_array_get_count(
3366 &stream
->read_subbuffer_ops
.post_consume_cbs
);
3368 for (i
= 0; i
< count
; i
++) {
3369 const post_consume_cb op
= *(post_consume_cb
*) lttng_dynamic_array_get_element(
3370 &stream
->read_subbuffer_ops
.post_consume_cbs
,
3373 ret
= op(stream
, subbuffer
, ctx
);
3382 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3383 struct lttng_consumer_local_data
*ctx
,
3384 bool locked_by_caller
)
3386 ssize_t ret
, written_bytes
= 0;
3388 struct stream_subbuffer subbuffer
= {};
3389 enum get_next_subbuffer_status get_next_status
;
3391 if (!locked_by_caller
) {
3392 stream
->read_subbuffer_ops
.lock(stream
);
3394 stream
->read_subbuffer_ops
.assert_locked(stream
);
3397 if (stream
->read_subbuffer_ops
.on_wake_up
) {
3398 ret
= stream
->read_subbuffer_ops
.on_wake_up(stream
);
3405 * If the stream was flagged to be ready for rotation before we extract
3406 * the next packet, rotate it now.
3408 if (stream
->rotate_ready
) {
3409 DBG("Rotate stream before consuming data");
3410 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3412 ERR("Stream rotation error before consuming data");
3417 get_next_status
= stream
->read_subbuffer_ops
.get_next_subbuffer(
3418 stream
, &subbuffer
);
3419 switch (get_next_status
) {
3420 case GET_NEXT_SUBBUFFER_STATUS_OK
:
3422 case GET_NEXT_SUBBUFFER_STATUS_NO_DATA
:
3426 case GET_NEXT_SUBBUFFER_STATUS_ERROR
:
3433 ret
= stream
->read_subbuffer_ops
.pre_consume_subbuffer(
3434 stream
, &subbuffer
);
3436 goto error_put_subbuf
;
3439 written_bytes
= stream
->read_subbuffer_ops
.consume_subbuffer(
3440 ctx
, stream
, &subbuffer
);
3441 if (written_bytes
<= 0) {
3442 ERR("Error consuming subbuffer: (%zd)", written_bytes
);
3443 ret
= (int) written_bytes
;
3444 goto error_put_subbuf
;
3447 ret
= stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3452 ret
= post_consume(stream
, &subbuffer
, ctx
);
3458 * After extracting the packet, we check if the stream is now ready to
3459 * be rotated and perform the action immediately.
3461 * Don't overwrite `ret` as callers expect the number of bytes
3462 * consumed to be returned on success.
3464 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
3465 if (rotation_ret
== 1) {
3466 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3467 if (rotation_ret
< 0) {
3469 ERR("Stream rotation error after consuming data");
3473 } else if (rotation_ret
< 0) {
3475 ERR("Failed to check if stream was ready to rotate after consuming data");
3480 if (stream
->read_subbuffer_ops
.on_sleep
) {
3481 stream
->read_subbuffer_ops
.on_sleep(stream
, ctx
);
3484 ret
= written_bytes
;
3486 if (!locked_by_caller
) {
3487 stream
->read_subbuffer_ops
.unlock(stream
);
3492 (void) stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3496 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3498 switch (the_consumer_data
.type
) {
3499 case LTTNG_CONSUMER_KERNEL
:
3500 return lttng_kconsumer_on_recv_stream(stream
);
3501 case LTTNG_CONSUMER32_UST
:
3502 case LTTNG_CONSUMER64_UST
:
3503 return lttng_ustconsumer_on_recv_stream(stream
);
3505 ERR("Unknown consumer_data type");
3512 * Allocate and set consumer data hash tables.
3514 int lttng_consumer_init(void)
3516 the_consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3517 if (!the_consumer_data
.channel_ht
) {
3521 the_consumer_data
.channels_by_session_id_ht
=
3522 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3523 if (!the_consumer_data
.channels_by_session_id_ht
) {
3527 the_consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3528 if (!the_consumer_data
.relayd_ht
) {
3532 the_consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3533 if (!the_consumer_data
.stream_list_ht
) {
3537 the_consumer_data
.stream_per_chan_id_ht
=
3538 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3539 if (!the_consumer_data
.stream_per_chan_id_ht
) {
3543 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3548 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3553 the_consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3554 if (!the_consumer_data
.chunk_registry
) {
3565 * Process the ADD_RELAYD command receive by a consumer.
3567 * This will create a relayd socket pair and add it to the relayd hash table.
3568 * The caller MUST acquire a RCU read side lock before calling it.
3570 void consumer_add_relayd_socket(uint64_t net_seq_idx
,
3572 struct lttng_consumer_local_data
*ctx
,
3574 struct pollfd
*consumer_sockpoll
,
3575 uint64_t sessiond_id
,
3576 uint64_t relayd_session_id
,
3577 uint32_t relayd_version_major
,
3578 uint32_t relayd_version_minor
,
3579 enum lttcomm_sock_proto relayd_socket_protocol
)
3581 int fd
= -1, ret
= -1, relayd_created
= 0;
3582 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3583 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3587 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3589 /* Get relayd reference if exists. */
3590 relayd
= consumer_find_relayd(net_seq_idx
);
3591 if (relayd
== NULL
) {
3592 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3593 /* Not found. Allocate one. */
3594 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3595 if (relayd
== NULL
) {
3596 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3599 relayd
->sessiond_session_id
= sessiond_id
;
3604 * This code path MUST continue to the consumer send status message to
3605 * we can notify the session daemon and continue our work without
3606 * killing everything.
3610 * relayd key should never be found for control socket.
3612 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3615 /* First send a status message before receiving the fds. */
3616 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3618 /* Somehow, the session daemon is not responding anymore. */
3619 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3620 goto error_nosignal
;
3623 /* Poll on consumer socket. */
3624 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3626 /* Needing to exit in the middle of a command: error. */
3627 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3628 goto error_nosignal
;
3631 /* Get relayd socket from session daemon */
3632 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3633 if (ret
!= sizeof(fd
)) {
3634 fd
= -1; /* Just in case it gets set with an invalid value. */
3637 * Failing to receive FDs might indicate a major problem such as
3638 * reaching a fd limit during the receive where the kernel returns a
3639 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3640 * don't take any chances and stop everything.
3642 * XXX: Feature request #558 will fix that and avoid this possible
3643 * issue when reaching the fd limit.
3645 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3646 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3650 /* Copy socket information and received FD */
3651 switch (sock_type
) {
3652 case LTTNG_STREAM_CONTROL
:
3653 /* Copy received lttcomm socket */
3654 ret
= lttcomm_populate_sock_from_open_socket(
3655 &relayd
->control_sock
.sock
, fd
,
3656 relayd_socket_protocol
);
3658 /* Assign version values. */
3659 relayd
->control_sock
.major
= relayd_version_major
;
3660 relayd
->control_sock
.minor
= relayd_version_minor
;
3662 relayd
->relayd_session_id
= relayd_session_id
;
3665 case LTTNG_STREAM_DATA
:
3666 /* Copy received lttcomm socket */
3667 ret
= lttcomm_populate_sock_from_open_socket(
3668 &relayd
->data_sock
.sock
, fd
,
3669 relayd_socket_protocol
);
3670 /* Assign version values. */
3671 relayd
->data_sock
.major
= relayd_version_major
;
3672 relayd
->data_sock
.minor
= relayd_version_minor
;
3675 ERR("Unknown relayd socket type (%d)", sock_type
);
3676 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3681 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3685 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3686 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3687 relayd
->net_seq_idx
, fd
);
3689 * We gave the ownership of the fd to the relayd structure. Set the
3690 * fd to -1 so we don't call close() on it in the error path below.
3694 /* We successfully added the socket. Send status back. */
3695 ret
= consumer_send_status_msg(sock
, ret_code
);
3697 /* Somehow, the session daemon is not responding anymore. */
3698 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3699 goto error_nosignal
;
3703 * Add relayd socket pair to consumer data hashtable. If object already
3704 * exists or on error, the function gracefully returns.
3713 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3714 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3718 /* Close received socket if valid. */
3721 PERROR("close received socket");
3725 if (relayd_created
) {
3731 * Search for a relayd associated to the session id and return the reference.
3733 * A rcu read side lock MUST be acquire before calling this function and locked
3734 * until the relayd object is no longer necessary.
3736 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3738 struct lttng_ht_iter iter
;
3739 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3741 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3742 cds_lfht_for_each_entry(the_consumer_data
.relayd_ht
->ht
, &iter
.iter
,
3743 relayd
, node
.node
) {
3745 * Check by sessiond id which is unique here where the relayd session
3746 * id might not be when having multiple relayd.
3748 if (relayd
->sessiond_session_id
== id
) {
3749 /* Found the relayd. There can be only one per id. */
3761 * Check if for a given session id there is still data needed to be extract
3764 * Return 1 if data is pending or else 0 meaning ready to be read.
3766 int consumer_data_pending(uint64_t id
)
3769 struct lttng_ht_iter iter
;
3770 struct lttng_ht
*ht
;
3771 struct lttng_consumer_stream
*stream
;
3772 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3773 int (*data_pending
)(struct lttng_consumer_stream
*);
3775 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3778 pthread_mutex_lock(&the_consumer_data
.lock
);
3780 switch (the_consumer_data
.type
) {
3781 case LTTNG_CONSUMER_KERNEL
:
3782 data_pending
= lttng_kconsumer_data_pending
;
3784 case LTTNG_CONSUMER32_UST
:
3785 case LTTNG_CONSUMER64_UST
:
3786 data_pending
= lttng_ustconsumer_data_pending
;
3789 ERR("Unknown consumer data type");
3793 /* Ease our life a bit */
3794 ht
= the_consumer_data
.stream_list_ht
;
3796 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3797 ht
->hash_fct(&id
, lttng_ht_seed
),
3799 &iter
.iter
, stream
, node_session_id
.node
) {
3800 pthread_mutex_lock(&stream
->lock
);
3803 * A removed node from the hash table indicates that the stream has
3804 * been deleted thus having a guarantee that the buffers are closed
3805 * on the consumer side. However, data can still be transmitted
3806 * over the network so don't skip the relayd check.
3808 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3810 /* Check the stream if there is data in the buffers. */
3811 ret
= data_pending(stream
);
3813 pthread_mutex_unlock(&stream
->lock
);
3818 pthread_mutex_unlock(&stream
->lock
);
3821 relayd
= find_relayd_by_session_id(id
);
3823 unsigned int is_data_inflight
= 0;
3825 /* Send init command for data pending. */
3826 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3827 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3828 relayd
->relayd_session_id
);
3830 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3831 /* Communication error thus the relayd so no data pending. */
3832 goto data_not_pending
;
3835 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3836 ht
->hash_fct(&id
, lttng_ht_seed
),
3838 &iter
.iter
, stream
, node_session_id
.node
) {
3839 if (stream
->metadata_flag
) {
3840 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3841 stream
->relayd_stream_id
);
3843 ret
= relayd_data_pending(&relayd
->control_sock
,
3844 stream
->relayd_stream_id
,
3845 stream
->next_net_seq_num
- 1);
3849 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3851 } else if (ret
< 0) {
3852 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3853 lttng_consumer_cleanup_relayd(relayd
);
3854 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3855 goto data_not_pending
;
3859 /* Send end command for data pending. */
3860 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3861 relayd
->relayd_session_id
, &is_data_inflight
);
3862 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3864 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3865 lttng_consumer_cleanup_relayd(relayd
);
3866 goto data_not_pending
;
3868 if (is_data_inflight
) {
3874 * Finding _no_ node in the hash table and no inflight data means that the
3875 * stream(s) have been removed thus data is guaranteed to be available for
3876 * analysis from the trace files.
3880 /* Data is available to be read by a viewer. */
3881 pthread_mutex_unlock(&the_consumer_data
.lock
);
3886 /* Data is still being extracted from buffers. */
3887 pthread_mutex_unlock(&the_consumer_data
.lock
);
3893 * Send a ret code status message to the sessiond daemon.
3895 * Return the sendmsg() return value.
3897 int consumer_send_status_msg(int sock
, int ret_code
)
3899 struct lttcomm_consumer_status_msg msg
;
3901 memset(&msg
, 0, sizeof(msg
));
3902 msg
.ret_code
= ret_code
;
3904 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3908 * Send a channel status message to the sessiond daemon.
3910 * Return the sendmsg() return value.
3912 int consumer_send_status_channel(int sock
,
3913 struct lttng_consumer_channel
*channel
)
3915 struct lttcomm_consumer_status_channel msg
;
3919 memset(&msg
, 0, sizeof(msg
));
3921 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3923 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3924 msg
.key
= channel
->key
;
3925 msg
.stream_count
= channel
->streams
.count
;
3928 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3931 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3932 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3933 uint64_t max_sb_size
)
3935 unsigned long start_pos
;
3937 if (!nb_packets_per_stream
) {
3938 return consumed_pos
; /* Grab everything */
3940 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
3941 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3942 if ((long) (start_pos
- consumed_pos
) < 0) {
3943 return consumed_pos
; /* Grab everything */
3948 /* Stream lock must be held by the caller. */
3949 static int sample_stream_positions(struct lttng_consumer_stream
*stream
,
3950 unsigned long *produced
, unsigned long *consumed
)
3954 ASSERT_LOCKED(stream
->lock
);
3956 ret
= lttng_consumer_sample_snapshot_positions(stream
);
3958 ERR("Failed to sample snapshot positions");
3962 ret
= lttng_consumer_get_produced_snapshot(stream
, produced
);
3964 ERR("Failed to sample produced position");
3968 ret
= lttng_consumer_get_consumed_snapshot(stream
, consumed
);
3970 ERR("Failed to sample consumed position");
3979 * Sample the rotate position for all the streams of a channel. If a stream
3980 * is already at the rotate position (produced == consumed), we flag it as
3981 * ready for rotation. The rotation of ready streams occurs after we have
3982 * replied to the session daemon that we have finished sampling the positions.
3983 * Must be called with RCU read-side lock held to ensure existence of channel.
3985 * Returns 0 on success, < 0 on error
3987 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
3988 uint64_t key
, uint64_t relayd_id
, uint32_t metadata
,
3989 struct lttng_consumer_local_data
*ctx
)
3992 struct lttng_consumer_stream
*stream
;
3993 struct lttng_ht_iter iter
;
3994 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
3995 struct lttng_dynamic_array stream_rotation_positions
;
3996 uint64_t next_chunk_id
, stream_count
= 0;
3997 enum lttng_trace_chunk_status chunk_status
;
3998 const bool is_local_trace
= relayd_id
== -1ULL;
3999 struct consumer_relayd_sock_pair
*relayd
= NULL
;
4000 bool rotating_to_new_chunk
= true;
4001 /* Array of `struct lttng_consumer_stream *` */
4002 struct lttng_dynamic_pointer_array streams_packet_to_open
;
4005 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
4007 lttng_dynamic_array_init(&stream_rotation_positions
,
4008 sizeof(struct relayd_stream_rotation_position
), NULL
);
4009 lttng_dynamic_pointer_array_init(&streams_packet_to_open
, NULL
);
4013 pthread_mutex_lock(&channel
->lock
);
4014 assert(channel
->trace_chunk
);
4015 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
,
4017 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4019 goto end_unlock_channel
;
4022 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4023 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4024 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4025 stream
, node_channel_id
.node
) {
4026 unsigned long produced_pos
= 0, consumed_pos
= 0;
4028 health_code_update();
4031 * Lock stream because we are about to change its state.
4033 pthread_mutex_lock(&stream
->lock
);
4035 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
4036 rotating_to_new_chunk
= false;
4040 * Do not flush a packet when rotating from a NULL trace
4041 * chunk. The stream has no means to output data, and the prior
4042 * rotation which rotated to NULL performed that side-effect
4043 * already. No new data can be produced when a stream has no
4044 * associated trace chunk (e.g. a stop followed by a rotate).
4046 if (stream
->trace_chunk
) {
4049 if (stream
->metadata_flag
) {
4051 * Don't produce an empty metadata packet,
4052 * simply close the current one.
4054 * Metadata is regenerated on every trace chunk
4055 * switch; there is no concern that no data was
4058 flush_active
= true;
4061 * Only flush an empty packet if the "packet
4062 * open" could not be performed on transition
4063 * to a new trace chunk and no packets were
4064 * consumed within the chunk's lifetime.
4066 if (stream
->opened_packet_in_current_trace_chunk
) {
4067 flush_active
= true;
4070 * Stream could have been full at the
4071 * time of rotation, but then have had
4072 * no activity at all.
4074 * It is important to flush a packet
4075 * to prevent 0-length files from being
4076 * produced as most viewers choke on
4079 * Unfortunately viewers will not be
4080 * able to know that tracing was active
4081 * for this stream during this trace
4084 ret
= sample_stream_positions(stream
, &produced_pos
, &consumed_pos
);
4086 goto end_unlock_stream
;
4090 * Don't flush an empty packet if data
4091 * was produced; it will be consumed
4092 * before the rotation completes.
4094 flush_active
= produced_pos
!= consumed_pos
;
4095 if (!flush_active
) {
4096 const char *trace_chunk_name
;
4097 uint64_t trace_chunk_id
;
4099 chunk_status
= lttng_trace_chunk_get_name(
4100 stream
->trace_chunk
,
4103 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NONE
) {
4104 trace_chunk_name
= "none";
4108 * Consumer trace chunks are
4111 chunk_status
= lttng_trace_chunk_get_id(
4112 stream
->trace_chunk
,
4114 assert(chunk_status
==
4115 LTTNG_TRACE_CHUNK_STATUS_OK
);
4117 DBG("Unable to open packet for stream during trace chunk's lifetime. "
4118 "Flushing an empty packet to prevent an empty file from being created: "
4119 "stream id = %" PRIu64
", trace chunk name = `%s`, trace chunk id = %" PRIu64
,
4120 stream
->key
, trace_chunk_name
, trace_chunk_id
);
4126 * Close the current packet before sampling the
4127 * ring buffer positions.
4129 ret
= consumer_stream_flush_buffer(stream
, flush_active
);
4131 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4133 goto end_unlock_stream
;
4137 ret
= lttng_consumer_take_snapshot(stream
);
4138 if (ret
< 0 && ret
!= -ENODATA
&& ret
!= -EAGAIN
) {
4139 ERR("Failed to sample snapshot position during channel rotation");
4140 goto end_unlock_stream
;
4143 ret
= lttng_consumer_get_produced_snapshot(stream
,
4146 ERR("Failed to sample produced position during channel rotation");
4147 goto end_unlock_stream
;
4150 ret
= lttng_consumer_get_consumed_snapshot(stream
,
4153 ERR("Failed to sample consumed position during channel rotation");
4154 goto end_unlock_stream
;
4158 * Align produced position on the start-of-packet boundary of the first
4159 * packet going into the next trace chunk.
4161 produced_pos
= ALIGN_FLOOR(produced_pos
, stream
->max_sb_size
);
4162 if (consumed_pos
== produced_pos
) {
4163 DBG("Set rotate ready for stream %" PRIu64
" produced = %lu consumed = %lu",
4164 stream
->key
, produced_pos
, consumed_pos
);
4165 stream
->rotate_ready
= true;
4167 DBG("Different consumed and produced positions "
4168 "for stream %" PRIu64
" produced = %lu consumed = %lu",
4169 stream
->key
, produced_pos
, consumed_pos
);
4172 * The rotation position is based on the packet_seq_num of the
4173 * packet following the last packet that was consumed for this
4174 * stream, incremented by the offset between produced and
4175 * consumed positions. This rotation position is a lower bound
4176 * (inclusive) at which the next trace chunk starts. Since it
4177 * is a lower bound, it is OK if the packet_seq_num does not
4178 * correspond exactly to the same packet identified by the
4179 * consumed_pos, which can happen in overwrite mode.
4181 if (stream
->sequence_number_unavailable
) {
4183 * Rotation should never be performed on a session which
4184 * interacts with a pre-2.8 lttng-modules, which does
4185 * not implement packet sequence number.
4187 ERR("Failure to rotate stream %" PRIu64
": sequence number unavailable",
4190 goto end_unlock_stream
;
4192 stream
->rotate_position
= stream
->last_sequence_number
+ 1 +
4193 ((produced_pos
- consumed_pos
) / stream
->max_sb_size
);
4194 DBG("Set rotation position for stream %" PRIu64
" at position %" PRIu64
,
4195 stream
->key
, stream
->rotate_position
);
4197 if (!is_local_trace
) {
4199 * The relay daemon control protocol expects a rotation
4200 * position as "the sequence number of the first packet
4201 * _after_ the current trace chunk".
4203 const struct relayd_stream_rotation_position position
= {
4204 .stream_id
= stream
->relayd_stream_id
,
4205 .rotate_at_seq_num
= stream
->rotate_position
,
4208 ret
= lttng_dynamic_array_add_element(
4209 &stream_rotation_positions
,
4212 ERR("Failed to allocate stream rotation position");
4213 goto end_unlock_stream
;
4218 stream
->opened_packet_in_current_trace_chunk
= false;
4220 if (rotating_to_new_chunk
&& !stream
->metadata_flag
) {
4222 * Attempt to flush an empty packet as close to the
4223 * rotation point as possible. In the event where a
4224 * stream remains inactive after the rotation point,
4225 * this ensures that the new trace chunk has a
4226 * beginning timestamp set at the begining of the
4227 * trace chunk instead of only creating an empty
4228 * packet when the trace chunk is stopped.
4230 * This indicates to the viewers that the stream
4231 * was being recorded, but more importantly it
4232 * allows viewers to determine a useable trace
4235 * This presents a problem in the case where the
4236 * ring-buffer is completely full.
4238 * Consider the following scenario:
4239 * - The consumption of data is slow (slow network,
4241 * - The ring buffer is full,
4242 * - A rotation is initiated,
4243 * - The flush below does nothing (no space left to
4244 * open a new packet),
4245 * - The other streams rotate very soon, and new
4246 * data is produced in the new chunk,
4247 * - This stream completes its rotation long after the
4248 * rotation was initiated
4249 * - The session is stopped before any event can be
4250 * produced in this stream's buffers.
4252 * The resulting trace chunk will have a single packet
4253 * temporaly at the end of the trace chunk for this
4254 * stream making the stream intersection more narrow
4255 * than it should be.
4257 * To work-around this, an empty flush is performed
4258 * after the first consumption of a packet during a
4259 * rotation if open_packet fails. The idea is that
4260 * consuming a packet frees enough space to switch
4261 * packets in this scenario and allows the tracer to
4262 * "stamp" the beginning of the new trace chunk at the
4263 * earliest possible point.
4265 * The packet open is performed after the channel
4266 * rotation to ensure that no attempt to open a packet
4267 * is performed in a stream that has no active trace
4270 ret
= lttng_dynamic_pointer_array_add_pointer(
4271 &streams_packet_to_open
, stream
);
4273 PERROR("Failed to add a stream pointer to array of streams in which to open a packet");
4275 goto end_unlock_stream
;
4279 pthread_mutex_unlock(&stream
->lock
);
4283 if (!is_local_trace
) {
4284 relayd
= consumer_find_relayd(relayd_id
);
4286 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4288 goto end_unlock_channel
;
4291 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4292 ret
= relayd_rotate_streams(&relayd
->control_sock
, stream_count
,
4293 rotating_to_new_chunk
? &next_chunk_id
: NULL
,
4294 (const struct relayd_stream_rotation_position
*)
4295 stream_rotation_positions
.buffer
4297 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4299 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4300 relayd
->net_seq_idx
);
4301 lttng_consumer_cleanup_relayd(relayd
);
4302 goto end_unlock_channel
;
4306 for (stream_idx
= 0;
4307 stream_idx
< lttng_dynamic_pointer_array_get_count(
4308 &streams_packet_to_open
);
4310 enum consumer_stream_open_packet_status status
;
4312 stream
= lttng_dynamic_pointer_array_get_pointer(
4313 &streams_packet_to_open
, stream_idx
);
4315 pthread_mutex_lock(&stream
->lock
);
4316 status
= consumer_stream_open_packet(stream
);
4317 pthread_mutex_unlock(&stream
->lock
);
4319 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
4320 DBG("Opened a packet after a rotation: stream id = %" PRIu64
4321 ", channel name = %s, session id = %" PRIu64
,
4322 stream
->key
, stream
->chan
->name
,
4323 stream
->chan
->session_id
);
4325 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
4327 * Can't open a packet as there is no space left
4328 * in the buffer. A new packet will be opened
4329 * once one has been consumed.
4331 DBG("No space left to open a packet after a rotation: stream id = %" PRIu64
4332 ", channel name = %s, session id = %" PRIu64
,
4333 stream
->key
, stream
->chan
->name
,
4334 stream
->chan
->session_id
);
4336 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
4337 /* Logged by callee. */
4339 goto end_unlock_channel
;
4345 pthread_mutex_unlock(&channel
->lock
);
4350 pthread_mutex_unlock(&stream
->lock
);
4352 pthread_mutex_unlock(&channel
->lock
);
4355 lttng_dynamic_array_reset(&stream_rotation_positions
);
4356 lttng_dynamic_pointer_array_reset(&streams_packet_to_open
);
4361 int consumer_clear_buffer(struct lttng_consumer_stream
*stream
)
4364 unsigned long consumed_pos_before
, consumed_pos_after
;
4366 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4368 ERR("Taking snapshot positions");
4372 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_before
);
4374 ERR("Consumed snapshot position");
4378 switch (the_consumer_data
.type
) {
4379 case LTTNG_CONSUMER_KERNEL
:
4380 ret
= kernctl_buffer_clear(stream
->wait_fd
);
4382 ERR("Failed to clear kernel stream (ret = %d)", ret
);
4386 case LTTNG_CONSUMER32_UST
:
4387 case LTTNG_CONSUMER64_UST
:
4388 ret
= lttng_ustconsumer_clear_buffer(stream
);
4390 ERR("Failed to clear ust stream (ret = %d)", ret
);
4395 ERR("Unknown consumer_data type");
4399 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4401 ERR("Taking snapshot positions");
4404 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_after
);
4406 ERR("Consumed snapshot position");
4409 DBG("clear: before: %lu after: %lu", consumed_pos_before
, consumed_pos_after
);
4415 int consumer_clear_stream(struct lttng_consumer_stream
*stream
)
4419 ret
= consumer_stream_flush_buffer(stream
, 1);
4421 ERR("Failed to flush stream %" PRIu64
" during channel clear",
4423 ret
= LTTCOMM_CONSUMERD_FATAL
;
4427 ret
= consumer_clear_buffer(stream
);
4429 ERR("Failed to clear stream %" PRIu64
" during channel clear",
4431 ret
= LTTCOMM_CONSUMERD_FATAL
;
4435 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4441 int consumer_clear_unmonitored_channel(struct lttng_consumer_channel
*channel
)
4444 struct lttng_consumer_stream
*stream
;
4447 pthread_mutex_lock(&channel
->lock
);
4448 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
4449 health_code_update();
4450 pthread_mutex_lock(&stream
->lock
);
4451 ret
= consumer_clear_stream(stream
);
4455 pthread_mutex_unlock(&stream
->lock
);
4457 pthread_mutex_unlock(&channel
->lock
);
4462 pthread_mutex_unlock(&stream
->lock
);
4463 pthread_mutex_unlock(&channel
->lock
);
4469 * Check if a stream is ready to be rotated after extracting it.
4471 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4472 * error. Stream lock must be held.
4474 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4476 DBG("Check is rotate ready for stream %" PRIu64
4477 " ready %u rotate_position %" PRIu64
4478 " last_sequence_number %" PRIu64
,
4479 stream
->key
, stream
->rotate_ready
,
4480 stream
->rotate_position
, stream
->last_sequence_number
);
4481 if (stream
->rotate_ready
) {
4486 * If packet seq num is unavailable, it means we are interacting
4487 * with a pre-2.8 lttng-modules which does not implement the
4488 * sequence number. Rotation should never be used by sessiond in this
4491 if (stream
->sequence_number_unavailable
) {
4492 ERR("Internal error: rotation used on stream %" PRIu64
4493 " with unavailable sequence number",
4498 if (stream
->rotate_position
== -1ULL ||
4499 stream
->last_sequence_number
== -1ULL) {
4504 * Rotate position not reached yet. The stream rotate position is
4505 * the position of the next packet belonging to the next trace chunk,
4506 * but consumerd considers rotation ready when reaching the last
4507 * packet of the current chunk, hence the "rotate_position - 1".
4510 DBG("Check is rotate ready for stream %" PRIu64
4511 " last_sequence_number %" PRIu64
4512 " rotate_position %" PRIu64
,
4513 stream
->key
, stream
->last_sequence_number
,
4514 stream
->rotate_position
);
4515 if (stream
->last_sequence_number
>= stream
->rotate_position
- 1) {
4523 * Reset the state for a stream after a rotation occurred.
4525 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4527 DBG("lttng_consumer_reset_stream_rotate_state for stream %" PRIu64
,
4529 stream
->rotate_position
= -1ULL;
4530 stream
->rotate_ready
= false;
4534 * Perform the rotation a local stream file.
4537 int rotate_local_stream(struct lttng_consumer_local_data
*ctx
,
4538 struct lttng_consumer_stream
*stream
)
4542 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4545 stream
->tracefile_size_current
= 0;
4546 stream
->tracefile_count_current
= 0;
4548 if (stream
->out_fd
>= 0) {
4549 ret
= close(stream
->out_fd
);
4551 PERROR("Failed to close stream out_fd of channel \"%s\"",
4552 stream
->chan
->name
);
4554 stream
->out_fd
= -1;
4557 if (stream
->index_file
) {
4558 lttng_index_file_put(stream
->index_file
);
4559 stream
->index_file
= NULL
;
4562 if (!stream
->trace_chunk
) {
4566 ret
= consumer_stream_create_output_files(stream
, true);
4572 * Performs the stream rotation for the rotate session feature if needed.
4573 * It must be called with the channel and stream locks held.
4575 * Return 0 on success, a negative number of error.
4577 int lttng_consumer_rotate_stream(struct lttng_consumer_local_data
*ctx
,
4578 struct lttng_consumer_stream
*stream
)
4582 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4585 * Update the stream's 'current' chunk to the session's (channel)
4586 * now-current chunk.
4588 lttng_trace_chunk_put(stream
->trace_chunk
);
4589 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4591 * A channel can be rotated and not have a "next" chunk
4592 * to transition to. In that case, the channel's "current chunk"
4593 * has not been closed yet, but it has not been updated to
4594 * a "next" trace chunk either. Hence, the stream, like its
4595 * parent channel, becomes part of no chunk and can't output
4596 * anything until a new trace chunk is created.
4598 stream
->trace_chunk
= NULL
;
4599 } else if (stream
->chan
->trace_chunk
&&
4600 !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4601 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4606 * Update the stream's trace chunk to its parent channel's
4607 * current trace chunk.
4609 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4612 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4613 ret
= rotate_local_stream(ctx
, stream
);
4615 ERR("Failed to rotate stream, ret = %i", ret
);
4620 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4622 * If the stream has transitioned to a new trace
4623 * chunk, the metadata should be re-dumped to the
4626 * However, it is possible for a stream to transition to
4627 * a "no-chunk" state. This can happen if a rotation
4628 * occurs on an inactive session. In such cases, the metadata
4629 * regeneration will happen when the next trace chunk is
4632 ret
= consumer_metadata_stream_dump(stream
);
4637 lttng_consumer_reset_stream_rotate_state(stream
);
4646 * Rotate all the ready streams now.
4648 * This is especially important for low throughput streams that have already
4649 * been consumed, we cannot wait for their next packet to perform the
4651 * Need to be called with RCU read-side lock held to ensure existence of
4654 * Returns 0 on success, < 0 on error
4656 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
,
4657 uint64_t key
, struct lttng_consumer_local_data
*ctx
)
4660 struct lttng_consumer_stream
*stream
;
4661 struct lttng_ht_iter iter
;
4662 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
4666 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4668 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4669 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4670 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4671 stream
, node_channel_id
.node
) {
4672 health_code_update();
4674 pthread_mutex_lock(&stream
->chan
->lock
);
4675 pthread_mutex_lock(&stream
->lock
);
4677 if (!stream
->rotate_ready
) {
4678 pthread_mutex_unlock(&stream
->lock
);
4679 pthread_mutex_unlock(&stream
->chan
->lock
);
4682 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4684 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
4685 pthread_mutex_unlock(&stream
->lock
);
4686 pthread_mutex_unlock(&stream
->chan
->lock
);
4699 enum lttcomm_return_code
lttng_consumer_init_command(
4700 struct lttng_consumer_local_data
*ctx
,
4701 const lttng_uuid sessiond_uuid
)
4703 enum lttcomm_return_code ret
;
4704 char uuid_str
[LTTNG_UUID_STR_LEN
];
4706 if (ctx
->sessiond_uuid
.is_set
) {
4707 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4711 ctx
->sessiond_uuid
.is_set
= true;
4712 memcpy(ctx
->sessiond_uuid
.value
, sessiond_uuid
, sizeof(lttng_uuid
));
4713 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4714 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4715 DBG("Received session daemon UUID: %s", uuid_str
);
4720 enum lttcomm_return_code
lttng_consumer_create_trace_chunk(
4721 const uint64_t *relayd_id
, uint64_t session_id
,
4723 time_t chunk_creation_timestamp
,
4724 const char *chunk_override_name
,
4725 const struct lttng_credentials
*credentials
,
4726 struct lttng_directory_handle
*chunk_directory_handle
)
4729 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4730 struct lttng_trace_chunk
*created_chunk
= NULL
, *published_chunk
= NULL
;
4731 enum lttng_trace_chunk_status chunk_status
;
4732 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4733 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4734 const char *relayd_id_str
= "(none)";
4735 const char *creation_timestamp_str
;
4736 struct lttng_ht_iter iter
;
4737 struct lttng_consumer_channel
*channel
;
4740 /* Only used for logging purposes. */
4741 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4742 "%" PRIu64
, *relayd_id
);
4743 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4744 relayd_id_str
= relayd_id_buffer
;
4746 relayd_id_str
= "(formatting error)";
4750 /* Local protocol error. */
4751 assert(chunk_creation_timestamp
);
4752 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4753 creation_timestamp_buffer
,
4754 sizeof(creation_timestamp_buffer
));
4755 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
4756 "(formatting error)";
4758 DBG("Consumer create trace chunk command: relay_id = %s"
4759 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4760 ", chunk_override_name = %s"
4761 ", chunk_creation_timestamp = %s",
4762 relayd_id_str
, session_id
, chunk_id
,
4763 chunk_override_name
? : "(none)",
4764 creation_timestamp_str
);
4767 * The trace chunk registry, as used by the consumer daemon, implicitly
4768 * owns the trace chunks. This is only needed in the consumer since
4769 * the consumer has no notion of a session beyond session IDs being
4770 * used to identify other objects.
4772 * The lttng_trace_chunk_registry_publish() call below provides a
4773 * reference which is not released; it implicitly becomes the session
4774 * daemon's reference to the chunk in the consumer daemon.
4776 * The lifetime of trace chunks in the consumer daemon is managed by
4777 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4778 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4780 created_chunk
= lttng_trace_chunk_create(chunk_id
,
4781 chunk_creation_timestamp
, NULL
);
4782 if (!created_chunk
) {
4783 ERR("Failed to create trace chunk");
4784 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4788 if (chunk_override_name
) {
4789 chunk_status
= lttng_trace_chunk_override_name(created_chunk
,
4790 chunk_override_name
);
4791 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4792 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4797 if (chunk_directory_handle
) {
4798 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
,
4800 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4801 ERR("Failed to set trace chunk credentials");
4802 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4806 * The consumer daemon has no ownership of the chunk output
4809 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
,
4810 chunk_directory_handle
);
4811 chunk_directory_handle
= NULL
;
4812 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4813 ERR("Failed to set trace chunk's directory handle");
4814 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4819 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4820 the_consumer_data
.chunk_registry
, session_id
,
4822 lttng_trace_chunk_put(created_chunk
);
4823 created_chunk
= NULL
;
4824 if (!published_chunk
) {
4825 ERR("Failed to publish trace chunk");
4826 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4831 cds_lfht_for_each_entry_duplicate(
4832 the_consumer_data
.channels_by_session_id_ht
->ht
,
4833 the_consumer_data
.channels_by_session_id_ht
->hash_fct(
4834 &session_id
, lttng_ht_seed
),
4835 the_consumer_data
.channels_by_session_id_ht
->match_fct
,
4836 &session_id
, &iter
.iter
, channel
,
4837 channels_by_session_id_ht_node
.node
) {
4838 ret
= lttng_consumer_channel_set_trace_chunk(channel
,
4842 * Roll-back the creation of this chunk.
4844 * This is important since the session daemon will
4845 * assume that the creation of this chunk failed and
4846 * will never ask for it to be closed, resulting
4847 * in a leak and an inconsistent state for some
4850 enum lttcomm_return_code close_ret
;
4851 char path
[LTTNG_PATH_MAX
];
4853 DBG("Failed to set new trace chunk on existing channels, rolling back");
4854 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4855 session_id
, chunk_id
,
4856 chunk_creation_timestamp
, NULL
,
4858 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4859 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4860 session_id
, chunk_id
);
4863 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4869 struct consumer_relayd_sock_pair
*relayd
;
4871 relayd
= consumer_find_relayd(*relayd_id
);
4873 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4874 ret
= relayd_create_trace_chunk(
4875 &relayd
->control_sock
, published_chunk
);
4876 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4878 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4881 if (!relayd
|| ret
) {
4882 enum lttcomm_return_code close_ret
;
4883 char path
[LTTNG_PATH_MAX
];
4885 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4888 chunk_creation_timestamp
,
4890 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4891 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4896 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4903 /* Release the reference returned by the "publish" operation. */
4904 lttng_trace_chunk_put(published_chunk
);
4905 lttng_trace_chunk_put(created_chunk
);
4909 enum lttcomm_return_code
lttng_consumer_close_trace_chunk(
4910 const uint64_t *relayd_id
, uint64_t session_id
,
4911 uint64_t chunk_id
, time_t chunk_close_timestamp
,
4912 const enum lttng_trace_chunk_command_type
*close_command
,
4915 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4916 struct lttng_trace_chunk
*chunk
;
4917 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4918 const char *relayd_id_str
= "(none)";
4919 const char *close_command_name
= "none";
4920 struct lttng_ht_iter iter
;
4921 struct lttng_consumer_channel
*channel
;
4922 enum lttng_trace_chunk_status chunk_status
;
4927 /* Only used for logging purposes. */
4928 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4929 "%" PRIu64
, *relayd_id
);
4930 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4931 relayd_id_str
= relayd_id_buffer
;
4933 relayd_id_str
= "(formatting error)";
4936 if (close_command
) {
4937 close_command_name
= lttng_trace_chunk_command_type_get_name(
4941 DBG("Consumer close trace chunk command: relayd_id = %s"
4942 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4943 ", close command = %s",
4944 relayd_id_str
, session_id
, chunk_id
,
4945 close_command_name
);
4947 chunk
= lttng_trace_chunk_registry_find_chunk(
4948 the_consumer_data
.chunk_registry
, session_id
, chunk_id
);
4950 ERR("Failed to find chunk: session_id = %" PRIu64
4951 ", chunk_id = %" PRIu64
,
4952 session_id
, chunk_id
);
4953 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4957 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
,
4958 chunk_close_timestamp
);
4959 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4960 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4964 if (close_command
) {
4965 chunk_status
= lttng_trace_chunk_set_close_command(
4966 chunk
, *close_command
);
4967 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4968 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4974 * chunk is now invalid to access as we no longer hold a reference to
4975 * it; it is only kept around to compare it (by address) to the
4976 * current chunk found in the session's channels.
4979 cds_lfht_for_each_entry(the_consumer_data
.channel_ht
->ht
, &iter
.iter
,
4980 channel
, node
.node
) {
4984 * Only change the channel's chunk to NULL if it still
4985 * references the chunk being closed. The channel may
4986 * reference a newer channel in the case of a session
4987 * rotation. When a session rotation occurs, the "next"
4988 * chunk is created before the "current" chunk is closed.
4990 if (channel
->trace_chunk
!= chunk
) {
4993 ret
= lttng_consumer_channel_set_trace_chunk(channel
, NULL
);
4996 * Attempt to close the chunk on as many channels as
4999 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5005 struct consumer_relayd_sock_pair
*relayd
;
5007 relayd
= consumer_find_relayd(*relayd_id
);
5009 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5010 ret
= relayd_close_trace_chunk(
5011 &relayd
->control_sock
, chunk
,
5013 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5015 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
,
5019 if (!relayd
|| ret
) {
5020 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5028 * Release the reference returned by the "find" operation and
5029 * the session daemon's implicit reference to the chunk.
5031 lttng_trace_chunk_put(chunk
);
5032 lttng_trace_chunk_put(chunk
);
5037 enum lttcomm_return_code
lttng_consumer_trace_chunk_exists(
5038 const uint64_t *relayd_id
, uint64_t session_id
,
5042 enum lttcomm_return_code ret_code
;
5043 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
5044 const char *relayd_id_str
= "(none)";
5045 const bool is_local_trace
= !relayd_id
;
5046 struct consumer_relayd_sock_pair
*relayd
= NULL
;
5047 bool chunk_exists_local
, chunk_exists_remote
;
5050 /* Only used for logging purposes. */
5051 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
5052 "%" PRIu64
, *relayd_id
);
5053 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
5054 relayd_id_str
= relayd_id_buffer
;
5056 relayd_id_str
= "(formatting error)";
5060 DBG("Consumer trace chunk exists command: relayd_id = %s"
5061 ", chunk_id = %" PRIu64
, relayd_id_str
,
5063 ret
= lttng_trace_chunk_registry_chunk_exists(
5064 the_consumer_data
.chunk_registry
, session_id
, chunk_id
,
5065 &chunk_exists_local
);
5067 /* Internal error. */
5068 ERR("Failed to query the existence of a trace chunk");
5069 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
5072 DBG("Trace chunk %s locally",
5073 chunk_exists_local
? "exists" : "does not exist");
5074 if (chunk_exists_local
) {
5075 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
5077 } else if (is_local_trace
) {
5078 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5083 relayd
= consumer_find_relayd(*relayd_id
);
5085 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
5086 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5087 goto end_rcu_unlock
;
5089 DBG("Looking up existence of trace chunk on relay daemon");
5090 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5091 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
,
5092 &chunk_exists_remote
);
5093 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5095 ERR("Failed to look-up the existence of trace chunk on relay daemon");
5096 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
5097 goto end_rcu_unlock
;
5100 ret_code
= chunk_exists_remote
?
5101 LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
5102 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5103 DBG("Trace chunk %s on relay daemon",
5104 chunk_exists_remote
? "exists" : "does not exist");
5113 int consumer_clear_monitored_channel(struct lttng_consumer_channel
*channel
)
5115 struct lttng_ht
*ht
;
5116 struct lttng_consumer_stream
*stream
;
5117 struct lttng_ht_iter iter
;
5120 ht
= the_consumer_data
.stream_per_chan_id_ht
;
5123 cds_lfht_for_each_entry_duplicate(ht
->ht
,
5124 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
5125 ht
->match_fct
, &channel
->key
,
5126 &iter
.iter
, stream
, node_channel_id
.node
) {
5128 * Protect against teardown with mutex.
5130 pthread_mutex_lock(&stream
->lock
);
5131 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5134 ret
= consumer_clear_stream(stream
);
5139 pthread_mutex_unlock(&stream
->lock
);
5142 return LTTCOMM_CONSUMERD_SUCCESS
;
5145 pthread_mutex_unlock(&stream
->lock
);
5150 int lttng_consumer_clear_channel(struct lttng_consumer_channel
*channel
)
5154 DBG("Consumer clear channel %" PRIu64
, channel
->key
);
5156 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
5158 * Nothing to do for the metadata channel/stream.
5159 * Snapshot mechanism already take care of the metadata
5160 * handling/generation, and monitored channels only need to
5161 * have their data stream cleared..
5163 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5167 if (!channel
->monitor
) {
5168 ret
= consumer_clear_unmonitored_channel(channel
);
5170 ret
= consumer_clear_monitored_channel(channel
);
5176 enum lttcomm_return_code
lttng_consumer_open_channel_packets(
5177 struct lttng_consumer_channel
*channel
)
5179 struct lttng_consumer_stream
*stream
;
5180 enum lttcomm_return_code ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5182 if (channel
->metadata_stream
) {
5183 ERR("Open channel packets command attempted on a metadata channel");
5184 ret
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5189 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
5190 enum consumer_stream_open_packet_status status
;
5192 pthread_mutex_lock(&stream
->lock
);
5193 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5197 status
= consumer_stream_open_packet(stream
);
5199 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
5200 DBG("Opened a packet in \"open channel packets\" command: stream id = %" PRIu64
5201 ", channel name = %s, session id = %" PRIu64
,
5202 stream
->key
, stream
->chan
->name
,
5203 stream
->chan
->session_id
);
5204 stream
->opened_packet_in_current_trace_chunk
= true;
5206 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
5207 DBG("No space left to open a packet in \"open channel packets\" command: stream id = %" PRIu64
5208 ", channel name = %s, session id = %" PRIu64
,
5209 stream
->key
, stream
->chan
->name
,
5210 stream
->chan
->session_id
);
5212 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
5214 * Only unexpected internal errors can lead to this
5215 * failing. Report an unknown error.
5217 ERR("Failed to flush empty buffer in \"open channel packets\" command: stream id = %" PRIu64
5218 ", channel id = %" PRIu64
5219 ", channel name = %s"
5220 ", session id = %" PRIu64
,
5221 stream
->key
, channel
->key
,
5222 channel
->name
, channel
->session_id
);
5223 ret
= LTTCOMM_CONSUMERD_UNKNOWN_ERROR
;
5230 pthread_mutex_unlock(&stream
->lock
);
5239 pthread_mutex_unlock(&stream
->lock
);
5240 goto end_rcu_unlock
;
5243 void lttng_consumer_sigbus_handle(void *addr
)
5245 lttng_ustconsumer_sigbus_handle(addr
);