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"
18 #include <sys/socket.h>
19 #include <sys/types.h>
24 #include <bin/lttng-consumerd/health-consumerd.h>
25 #include <common/common.h>
26 #include <common/utils.h>
27 #include <common/time.h>
28 #include <common/compat/poll.h>
29 #include <common/compat/endian.h>
30 #include <common/index/index.h>
31 #include <common/kernel-ctl/kernel-ctl.h>
32 #include <common/sessiond-comm/relayd.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
34 #include <common/kernel-consumer/kernel-consumer.h>
35 #include <common/relayd/relayd.h>
36 #include <common/ust-consumer/ust-consumer.h>
37 #include <common/consumer/consumer-timer.h>
38 #include <common/consumer/consumer.h>
39 #include <common/consumer/consumer-stream.h>
40 #include <common/consumer/consumer-testpoint.h>
41 #include <common/align.h>
42 #include <common/consumer/consumer-metadata-cache.h>
43 #include <common/trace-chunk.h>
44 #include <common/trace-chunk-registry.h>
45 #include <common/string-utils/format.h>
46 #include <common/dynamic-array.h>
48 lttng_consumer_global_data the_consumer_data
;
50 enum consumer_channel_action
{
53 CONSUMER_CHANNEL_QUIT
,
56 struct consumer_channel_msg
{
57 enum consumer_channel_action action
;
58 struct lttng_consumer_channel
*chan
; /* add */
59 uint64_t key
; /* del */
62 /* Flag used to temporarily pause data consumption from testpoints. */
63 int data_consumption_paused
;
66 * Flag to inform the polling thread to quit when all fd hung up. Updated by
67 * the consumer_thread_receive_fds when it notices that all fds has hung up.
68 * Also updated by the signal handler (consumer_should_exit()). Read by the
74 * Global hash table containing respectively metadata and data streams. The
75 * stream element in this ht should only be updated by the metadata poll thread
76 * for the metadata and the data poll thread for the data.
78 static struct lttng_ht
*metadata_ht
;
79 static struct lttng_ht
*data_ht
;
81 static const char *get_consumer_domain(void)
83 switch (the_consumer_data
.type
) {
84 case LTTNG_CONSUMER_KERNEL
:
85 return DEFAULT_KERNEL_TRACE_DIR
;
86 case LTTNG_CONSUMER64_UST
:
88 case LTTNG_CONSUMER32_UST
:
89 return DEFAULT_UST_TRACE_DIR
;
96 * Notify a thread lttng pipe to poll back again. This usually means that some
97 * global state has changed so we just send back the thread in a poll wait
100 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
102 struct lttng_consumer_stream
*null_stream
= NULL
;
106 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
109 static void notify_health_quit_pipe(int *pipe
)
113 ret
= lttng_write(pipe
[1], "4", 1);
115 PERROR("write consumer health quit");
119 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
120 struct lttng_consumer_channel
*chan
,
122 enum consumer_channel_action action
)
124 struct consumer_channel_msg msg
;
127 memset(&msg
, 0, sizeof(msg
));
132 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
133 if (ret
< sizeof(msg
)) {
134 PERROR("notify_channel_pipe write error");
138 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
141 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
144 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
145 struct lttng_consumer_channel
**chan
,
147 enum consumer_channel_action
*action
)
149 struct consumer_channel_msg msg
;
152 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
153 if (ret
< sizeof(msg
)) {
157 *action
= msg
.action
;
165 * Cleanup the stream list of a channel. Those streams are not yet globally
168 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
170 struct lttng_consumer_stream
*stream
, *stmp
;
172 LTTNG_ASSERT(channel
);
174 /* Delete streams that might have been left in the stream list. */
175 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
178 * Once a stream is added to this list, the buffers were created so we
179 * have a guarantee that this call will succeed. Setting the monitor
180 * mode to 0 so we don't lock nor try to delete the stream from the
184 consumer_stream_destroy(stream
, NULL
);
189 * Find a stream. The consumer_data.lock must be locked during this
192 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
195 struct lttng_ht_iter iter
;
196 struct lttng_ht_node_u64
*node
;
197 struct lttng_consumer_stream
*stream
= NULL
;
201 /* -1ULL keys are lookup failures */
202 if (key
== (uint64_t) -1ULL) {
208 lttng_ht_lookup(ht
, &key
, &iter
);
209 node
= lttng_ht_iter_get_node_u64(&iter
);
211 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
219 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
221 struct lttng_consumer_stream
*stream
;
224 stream
= find_stream(key
, ht
);
226 stream
->key
= (uint64_t) -1ULL;
228 * We don't want the lookup to match, but we still need
229 * to iterate on this stream when iterating over the hash table. Just
230 * change the node key.
232 stream
->node
.key
= (uint64_t) -1ULL;
238 * Return a channel object for the given key.
240 * RCU read side lock MUST be acquired before calling this function and
241 * protects the channel ptr.
243 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
245 struct lttng_ht_iter iter
;
246 struct lttng_ht_node_u64
*node
;
247 struct lttng_consumer_channel
*channel
= NULL
;
249 ASSERT_RCU_READ_LOCKED();
251 /* -1ULL keys are lookup failures */
252 if (key
== (uint64_t) -1ULL) {
256 lttng_ht_lookup(the_consumer_data
.channel_ht
, &key
, &iter
);
257 node
= lttng_ht_iter_get_node_u64(&iter
);
259 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
266 * There is a possibility that the consumer does not have enough time between
267 * the close of the channel on the session daemon and the cleanup in here thus
268 * once we have a channel add with an existing key, we know for sure that this
269 * channel will eventually get cleaned up by all streams being closed.
271 * This function just nullifies the already existing channel key.
273 static void steal_channel_key(uint64_t key
)
275 struct lttng_consumer_channel
*channel
;
278 channel
= consumer_find_channel(key
);
280 channel
->key
= (uint64_t) -1ULL;
282 * We don't want the lookup to match, but we still need to iterate on
283 * this channel when iterating over the hash table. Just change the
286 channel
->node
.key
= (uint64_t) -1ULL;
291 static void free_channel_rcu(struct rcu_head
*head
)
293 struct lttng_ht_node_u64
*node
=
294 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
295 struct lttng_consumer_channel
*channel
=
296 caa_container_of(node
, struct lttng_consumer_channel
, node
);
298 switch (the_consumer_data
.type
) {
299 case LTTNG_CONSUMER_KERNEL
:
301 case LTTNG_CONSUMER32_UST
:
302 case LTTNG_CONSUMER64_UST
:
303 lttng_ustconsumer_free_channel(channel
);
306 ERR("Unknown consumer_data type");
313 * RCU protected relayd socket pair free.
315 static void free_relayd_rcu(struct rcu_head
*head
)
317 struct lttng_ht_node_u64
*node
=
318 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
319 struct consumer_relayd_sock_pair
*relayd
=
320 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
323 * Close all sockets. This is done in the call RCU since we don't want the
324 * socket fds to be reassigned thus potentially creating bad state of the
327 * We do not have to lock the control socket mutex here since at this stage
328 * there is no one referencing to this relayd object.
330 (void) relayd_close(&relayd
->control_sock
);
331 (void) relayd_close(&relayd
->data_sock
);
333 pthread_mutex_destroy(&relayd
->ctrl_sock_mutex
);
338 * Destroy and free relayd socket pair object.
340 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
343 struct lttng_ht_iter iter
;
345 if (relayd
== NULL
) {
349 DBG("Consumer destroy and close relayd socket pair");
351 iter
.iter
.node
= &relayd
->node
.node
;
352 ret
= lttng_ht_del(the_consumer_data
.relayd_ht
, &iter
);
354 /* We assume the relayd is being or is destroyed */
358 /* RCU free() call */
359 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
363 * Remove a channel from the global list protected by a mutex. This function is
364 * also responsible for freeing its data structures.
366 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
368 struct lttng_ht_iter iter
;
370 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
372 pthread_mutex_lock(&the_consumer_data
.lock
);
373 pthread_mutex_lock(&channel
->lock
);
375 /* Destroy streams that might have been left in the stream list. */
376 clean_channel_stream_list(channel
);
378 if (channel
->live_timer_enabled
== 1) {
379 consumer_timer_live_stop(channel
);
381 if (channel
->monitor_timer_enabled
== 1) {
382 consumer_timer_monitor_stop(channel
);
385 switch (the_consumer_data
.type
) {
386 case LTTNG_CONSUMER_KERNEL
:
388 case LTTNG_CONSUMER32_UST
:
389 case LTTNG_CONSUMER64_UST
:
390 lttng_ustconsumer_del_channel(channel
);
393 ERR("Unknown consumer_data type");
398 lttng_trace_chunk_put(channel
->trace_chunk
);
399 channel
->trace_chunk
= NULL
;
401 if (channel
->is_published
) {
405 iter
.iter
.node
= &channel
->node
.node
;
406 ret
= lttng_ht_del(the_consumer_data
.channel_ht
, &iter
);
409 iter
.iter
.node
= &channel
->channels_by_session_id_ht_node
.node
;
410 ret
= lttng_ht_del(the_consumer_data
.channels_by_session_id_ht
,
416 channel
->is_deleted
= true;
417 call_rcu(&channel
->node
.head
, free_channel_rcu
);
419 pthread_mutex_unlock(&channel
->lock
);
420 pthread_mutex_unlock(&the_consumer_data
.lock
);
424 * Iterate over the relayd hash table and destroy each element. Finally,
425 * destroy the whole hash table.
427 static void cleanup_relayd_ht(void)
429 struct lttng_ht_iter iter
;
430 struct consumer_relayd_sock_pair
*relayd
;
434 cds_lfht_for_each_entry(the_consumer_data
.relayd_ht
->ht
, &iter
.iter
,
436 consumer_destroy_relayd(relayd
);
441 lttng_ht_destroy(the_consumer_data
.relayd_ht
);
445 * Update the end point status of all streams having the given network sequence
446 * index (relayd index).
448 * It's atomically set without having the stream mutex locked which is fine
449 * because we handle the write/read race with a pipe wakeup for each thread.
451 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
452 enum consumer_endpoint_status status
)
454 struct lttng_ht_iter iter
;
455 struct lttng_consumer_stream
*stream
;
457 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
461 /* Let's begin with metadata */
462 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
463 if (stream
->net_seq_idx
== net_seq_idx
) {
464 uatomic_set(&stream
->endpoint_status
, status
);
465 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
469 /* Follow up by the data streams */
470 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
471 if (stream
->net_seq_idx
== net_seq_idx
) {
472 uatomic_set(&stream
->endpoint_status
, status
);
473 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
480 * Cleanup a relayd object by flagging every associated streams for deletion,
481 * destroying the object meaning removing it from the relayd hash table,
482 * closing the sockets and freeing the memory in a RCU call.
484 * If a local data context is available, notify the threads that the streams'
485 * state have changed.
487 void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
)
491 LTTNG_ASSERT(relayd
);
493 DBG("Cleaning up relayd object ID %" PRIu64
, relayd
->net_seq_idx
);
495 /* Save the net sequence index before destroying the object */
496 netidx
= relayd
->net_seq_idx
;
499 * Delete the relayd from the relayd hash table, close the sockets and free
500 * the object in a RCU call.
502 consumer_destroy_relayd(relayd
);
504 /* Set inactive endpoint to all streams */
505 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
508 * With a local data context, notify the threads that the streams' state
509 * have changed. The write() action on the pipe acts as an "implicit"
510 * memory barrier ordering the updates of the end point status from the
511 * read of this status which happens AFTER receiving this notify.
513 notify_thread_lttng_pipe(relayd
->ctx
->consumer_data_pipe
);
514 notify_thread_lttng_pipe(relayd
->ctx
->consumer_metadata_pipe
);
518 * Flag a relayd socket pair for destruction. Destroy it if the refcount
521 * RCU read side lock MUST be aquired before calling this function.
523 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
525 LTTNG_ASSERT(relayd
);
526 ASSERT_RCU_READ_LOCKED();
528 /* Set destroy flag for this object */
529 uatomic_set(&relayd
->destroy_flag
, 1);
531 /* Destroy the relayd if refcount is 0 */
532 if (uatomic_read(&relayd
->refcount
) == 0) {
533 consumer_destroy_relayd(relayd
);
538 * Completly destroy stream from every visiable data structure and the given
541 * One this call returns, the stream object is not longer usable nor visible.
543 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
546 consumer_stream_destroy(stream
, ht
);
550 * XXX naming of del vs destroy is all mixed up.
552 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
554 consumer_stream_destroy(stream
, data_ht
);
557 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
559 consumer_stream_destroy(stream
, metadata_ht
);
562 void consumer_stream_update_channel_attributes(
563 struct lttng_consumer_stream
*stream
,
564 struct lttng_consumer_channel
*channel
)
566 stream
->channel_read_only_attributes
.tracefile_size
=
567 channel
->tracefile_size
;
571 * Add a stream to the global list protected by a mutex.
573 void consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
575 struct lttng_ht
*ht
= data_ht
;
577 LTTNG_ASSERT(stream
);
580 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
582 pthread_mutex_lock(&the_consumer_data
.lock
);
583 pthread_mutex_lock(&stream
->chan
->lock
);
584 pthread_mutex_lock(&stream
->chan
->timer_lock
);
585 pthread_mutex_lock(&stream
->lock
);
588 /* Steal stream identifier to avoid having streams with the same key */
589 steal_stream_key(stream
->key
, ht
);
591 lttng_ht_add_unique_u64(ht
, &stream
->node
);
593 lttng_ht_add_u64(the_consumer_data
.stream_per_chan_id_ht
,
594 &stream
->node_channel_id
);
597 * Add stream to the stream_list_ht of the consumer data. No need to steal
598 * the key since the HT does not use it and we allow to add redundant keys
601 lttng_ht_add_u64(the_consumer_data
.stream_list_ht
,
602 &stream
->node_session_id
);
605 * When nb_init_stream_left reaches 0, we don't need to trigger any action
606 * in terms of destroying the associated channel, because the action that
607 * causes the count to become 0 also causes a stream to be added. The
608 * channel deletion will thus be triggered by the following removal of this
611 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
612 /* Increment refcount before decrementing nb_init_stream_left */
614 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
617 /* Update consumer data once the node is inserted. */
618 the_consumer_data
.stream_count
++;
619 the_consumer_data
.need_update
= 1;
622 pthread_mutex_unlock(&stream
->lock
);
623 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
624 pthread_mutex_unlock(&stream
->chan
->lock
);
625 pthread_mutex_unlock(&the_consumer_data
.lock
);
629 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
630 * be acquired before calling this.
632 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
635 struct lttng_ht_node_u64
*node
;
636 struct lttng_ht_iter iter
;
638 LTTNG_ASSERT(relayd
);
639 ASSERT_RCU_READ_LOCKED();
641 lttng_ht_lookup(the_consumer_data
.relayd_ht
, &relayd
->net_seq_idx
,
643 node
= lttng_ht_iter_get_node_u64(&iter
);
647 lttng_ht_add_unique_u64(the_consumer_data
.relayd_ht
, &relayd
->node
);
654 * Allocate and return a consumer relayd socket.
656 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
657 uint64_t net_seq_idx
)
659 struct consumer_relayd_sock_pair
*obj
= NULL
;
661 /* net sequence index of -1 is a failure */
662 if (net_seq_idx
== (uint64_t) -1ULL) {
666 obj
= (consumer_relayd_sock_pair
*) zmalloc(sizeof(struct consumer_relayd_sock_pair
));
668 PERROR("zmalloc relayd sock");
672 obj
->net_seq_idx
= net_seq_idx
;
674 obj
->destroy_flag
= 0;
675 obj
->control_sock
.sock
.fd
= -1;
676 obj
->data_sock
.sock
.fd
= -1;
677 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
678 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
685 * Find a relayd socket pair in the global consumer data.
687 * Return the object if found else NULL.
688 * RCU read-side lock must be held across this call and while using the
691 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
693 struct lttng_ht_iter iter
;
694 struct lttng_ht_node_u64
*node
;
695 struct consumer_relayd_sock_pair
*relayd
= NULL
;
697 ASSERT_RCU_READ_LOCKED();
699 /* Negative keys are lookup failures */
700 if (key
== (uint64_t) -1ULL) {
704 lttng_ht_lookup(the_consumer_data
.relayd_ht
, &key
, &iter
);
705 node
= lttng_ht_iter_get_node_u64(&iter
);
707 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
715 * Find a relayd and send the stream
717 * Returns 0 on success, < 0 on error
719 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
723 struct consumer_relayd_sock_pair
*relayd
;
725 LTTNG_ASSERT(stream
);
726 LTTNG_ASSERT(stream
->net_seq_idx
!= -1ULL);
729 /* The stream is not metadata. Get relayd reference if exists. */
731 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
732 if (relayd
!= NULL
) {
733 /* Add stream on the relayd */
734 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
735 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
736 get_consumer_domain(), path
, &stream
->relayd_stream_id
,
737 stream
->chan
->tracefile_size
,
738 stream
->chan
->tracefile_count
,
739 stream
->trace_chunk
);
740 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
742 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
743 lttng_consumer_cleanup_relayd(relayd
);
747 uatomic_inc(&relayd
->refcount
);
748 stream
->sent_to_relayd
= 1;
750 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
751 stream
->key
, stream
->net_seq_idx
);
756 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
757 stream
->name
, stream
->key
, stream
->net_seq_idx
);
765 * Find a relayd and send the streams sent message
767 * Returns 0 on success, < 0 on error
769 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
772 struct consumer_relayd_sock_pair
*relayd
;
774 LTTNG_ASSERT(net_seq_idx
!= -1ULL);
776 /* The stream is not metadata. Get relayd reference if exists. */
778 relayd
= consumer_find_relayd(net_seq_idx
);
779 if (relayd
!= NULL
) {
780 /* Add stream on the relayd */
781 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
782 ret
= relayd_streams_sent(&relayd
->control_sock
);
783 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
785 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
786 lttng_consumer_cleanup_relayd(relayd
);
790 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
797 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
805 * Find a relayd and close the stream
807 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
809 struct consumer_relayd_sock_pair
*relayd
;
811 /* The stream is not metadata. Get relayd reference if exists. */
813 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
815 consumer_stream_relayd_close(stream
, relayd
);
821 * Handle stream for relayd transmission if the stream applies for network
822 * streaming where the net sequence index is set.
824 * Return destination file descriptor or negative value on error.
826 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
827 size_t data_size
, unsigned long padding
,
828 struct consumer_relayd_sock_pair
*relayd
)
831 struct lttcomm_relayd_data_hdr data_hdr
;
834 LTTNG_ASSERT(stream
);
835 LTTNG_ASSERT(relayd
);
837 /* Reset data header */
838 memset(&data_hdr
, 0, sizeof(data_hdr
));
840 if (stream
->metadata_flag
) {
841 /* Caller MUST acquire the relayd control socket lock */
842 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
847 /* Metadata are always sent on the control socket. */
848 outfd
= relayd
->control_sock
.sock
.fd
;
850 /* Set header with stream information */
851 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
852 data_hdr
.data_size
= htobe32(data_size
);
853 data_hdr
.padding_size
= htobe32(padding
);
856 * Note that net_seq_num below is assigned with the *current* value of
857 * next_net_seq_num and only after that the next_net_seq_num will be
858 * increment. This is why when issuing a command on the relayd using
859 * this next value, 1 should always be substracted in order to compare
860 * the last seen sequence number on the relayd side to the last sent.
862 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
863 /* Other fields are zeroed previously */
865 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
871 ++stream
->next_net_seq_num
;
873 /* Set to go on data socket */
874 outfd
= relayd
->data_sock
.sock
.fd
;
882 * Write a character on the metadata poll pipe to wake the metadata thread.
883 * Returns 0 on success, -1 on error.
885 int consumer_metadata_wakeup_pipe(const struct lttng_consumer_channel
*channel
)
889 DBG("Waking up metadata poll thread (writing to pipe): channel name = '%s'",
891 if (channel
->monitor
&& channel
->metadata_stream
) {
892 const char dummy
= 'c';
893 const ssize_t write_ret
= lttng_write(
894 channel
->metadata_stream
->ust_metadata_poll_pipe
[1],
898 if (errno
== EWOULDBLOCK
) {
900 * This is fine, the metadata poll thread
901 * is having a hard time keeping-up, but
902 * it will eventually wake-up and consume
903 * the available data.
907 PERROR("Failed to write to UST metadata pipe while attempting to wake-up the metadata poll thread");
919 * Trigger a dump of the metadata content. Following/during the succesful
920 * completion of this call, the metadata poll thread will start receiving
921 * metadata packets to consume.
923 * The caller must hold the channel and stream locks.
926 int consumer_metadata_stream_dump(struct lttng_consumer_stream
*stream
)
930 ASSERT_LOCKED(stream
->chan
->lock
);
931 ASSERT_LOCKED(stream
->lock
);
932 LTTNG_ASSERT(stream
->metadata_flag
);
933 LTTNG_ASSERT(stream
->chan
->trace_chunk
);
935 switch (the_consumer_data
.type
) {
936 case LTTNG_CONSUMER_KERNEL
:
938 * Reset the position of what has been read from the
939 * metadata cache to 0 so we can dump it again.
941 ret
= kernctl_metadata_cache_dump(stream
->wait_fd
);
943 case LTTNG_CONSUMER32_UST
:
944 case LTTNG_CONSUMER64_UST
:
946 * Reset the position pushed from the metadata cache so it
947 * will write from the beginning on the next push.
949 stream
->ust_metadata_pushed
= 0;
950 ret
= consumer_metadata_wakeup_pipe(stream
->chan
);
953 ERR("Unknown consumer_data type");
957 ERR("Failed to dump the metadata cache");
963 int lttng_consumer_channel_set_trace_chunk(
964 struct lttng_consumer_channel
*channel
,
965 struct lttng_trace_chunk
*new_trace_chunk
)
967 pthread_mutex_lock(&channel
->lock
);
968 if (channel
->is_deleted
) {
970 * The channel has been logically deleted and should no longer
971 * be used. It has released its reference to its current trace
972 * chunk and should not acquire a new one.
974 * Return success as there is nothing for the caller to do.
980 * The acquisition of the reference cannot fail (barring
981 * a severe internal error) since a reference to the published
982 * chunk is already held by the caller.
984 if (new_trace_chunk
) {
985 const bool acquired_reference
= lttng_trace_chunk_get(
988 LTTNG_ASSERT(acquired_reference
);
991 lttng_trace_chunk_put(channel
->trace_chunk
);
992 channel
->trace_chunk
= new_trace_chunk
;
994 pthread_mutex_unlock(&channel
->lock
);
999 * Allocate and return a new lttng_consumer_channel object using the given key
1000 * to initialize the hash table node.
1002 * On error, return NULL.
1004 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
1005 uint64_t session_id
,
1006 const uint64_t *chunk_id
,
1007 const char *pathname
,
1010 enum lttng_event_output output
,
1011 uint64_t tracefile_size
,
1012 uint64_t tracefile_count
,
1013 uint64_t session_id_per_pid
,
1014 unsigned int monitor
,
1015 unsigned int live_timer_interval
,
1016 bool is_in_live_session
,
1017 const char *root_shm_path
,
1018 const char *shm_path
)
1020 struct lttng_consumer_channel
*channel
= NULL
;
1021 struct lttng_trace_chunk
*trace_chunk
= NULL
;
1024 trace_chunk
= lttng_trace_chunk_registry_find_chunk(
1025 the_consumer_data
.chunk_registry
, session_id
,
1028 ERR("Failed to find trace chunk reference during creation of channel");
1033 channel
= (lttng_consumer_channel
*) zmalloc(sizeof(*channel
));
1034 if (channel
== NULL
) {
1035 PERROR("malloc struct lttng_consumer_channel");
1040 channel
->refcount
= 0;
1041 channel
->session_id
= session_id
;
1042 channel
->session_id_per_pid
= session_id_per_pid
;
1043 channel
->relayd_id
= relayd_id
;
1044 channel
->tracefile_size
= tracefile_size
;
1045 channel
->tracefile_count
= tracefile_count
;
1046 channel
->monitor
= monitor
;
1047 channel
->live_timer_interval
= live_timer_interval
;
1048 channel
->is_live
= is_in_live_session
;
1049 pthread_mutex_init(&channel
->lock
, NULL
);
1050 pthread_mutex_init(&channel
->timer_lock
, NULL
);
1053 case LTTNG_EVENT_SPLICE
:
1054 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
1056 case LTTNG_EVENT_MMAP
:
1057 channel
->output
= CONSUMER_CHANNEL_MMAP
;
1067 * In monitor mode, the streams associated with the channel will be put in
1068 * a special list ONLY owned by this channel. So, the refcount is set to 1
1069 * here meaning that the channel itself has streams that are referenced.
1071 * On a channel deletion, once the channel is no longer visible, the
1072 * refcount is decremented and checked for a zero value to delete it. With
1073 * streams in no monitor mode, it will now be safe to destroy the channel.
1075 if (!channel
->monitor
) {
1076 channel
->refcount
= 1;
1079 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1080 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1082 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1083 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1085 if (root_shm_path
) {
1086 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1087 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1090 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1091 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1094 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1095 lttng_ht_node_init_u64(&channel
->channels_by_session_id_ht_node
,
1096 channel
->session_id
);
1098 channel
->wait_fd
= -1;
1099 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1102 int ret
= lttng_consumer_channel_set_trace_chunk(channel
,
1109 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1112 lttng_trace_chunk_put(trace_chunk
);
1115 consumer_del_channel(channel
);
1121 * Add a channel to the global list protected by a mutex.
1123 * Always return 0 indicating success.
1125 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1126 struct lttng_consumer_local_data
*ctx
)
1128 pthread_mutex_lock(&the_consumer_data
.lock
);
1129 pthread_mutex_lock(&channel
->lock
);
1130 pthread_mutex_lock(&channel
->timer_lock
);
1133 * This gives us a guarantee that the channel we are about to add to the
1134 * channel hash table will be unique. See this function comment on the why
1135 * we need to steel the channel key at this stage.
1137 steal_channel_key(channel
->key
);
1140 lttng_ht_add_unique_u64(the_consumer_data
.channel_ht
, &channel
->node
);
1141 lttng_ht_add_u64(the_consumer_data
.channels_by_session_id_ht
,
1142 &channel
->channels_by_session_id_ht_node
);
1144 channel
->is_published
= true;
1146 pthread_mutex_unlock(&channel
->timer_lock
);
1147 pthread_mutex_unlock(&channel
->lock
);
1148 pthread_mutex_unlock(&the_consumer_data
.lock
);
1150 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1151 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1158 * Allocate the pollfd structure and the local view of the out fds to avoid
1159 * doing a lookup in the linked list and concurrency issues when writing is
1160 * needed. Called with consumer_data.lock held.
1162 * Returns the number of fds in the structures.
1164 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1165 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1166 struct lttng_ht
*ht
, int *nb_inactive_fd
)
1169 struct lttng_ht_iter iter
;
1170 struct lttng_consumer_stream
*stream
;
1174 LTTNG_ASSERT(pollfd
);
1175 LTTNG_ASSERT(local_stream
);
1177 DBG("Updating poll fd array");
1178 *nb_inactive_fd
= 0;
1180 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1182 * Only active streams with an active end point can be added to the
1183 * poll set and local stream storage of the thread.
1185 * There is a potential race here for endpoint_status to be updated
1186 * just after the check. However, this is OK since the stream(s) will
1187 * be deleted once the thread is notified that the end point state has
1188 * changed where this function will be called back again.
1190 * We track the number of inactive FDs because they still need to be
1191 * closed by the polling thread after a wakeup on the data_pipe or
1194 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1195 (*nb_inactive_fd
)++;
1199 * This clobbers way too much the debug output. Uncomment that if you
1200 * need it for debugging purposes.
1202 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1203 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1204 local_stream
[i
] = stream
;
1210 * Insert the consumer_data_pipe at the end of the array and don't
1211 * increment i so nb_fd is the number of real FD.
1213 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1214 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1216 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1217 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1222 * Poll on the should_quit pipe and the command socket return -1 on
1223 * error, 1 if should exit, 0 if data is available on the command socket
1225 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1230 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1231 if (num_rdy
== -1) {
1233 * Restart interrupted system call.
1235 if (errno
== EINTR
) {
1238 PERROR("Poll error");
1241 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1242 DBG("consumer_should_quit wake up");
1249 * Set the error socket.
1251 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1254 ctx
->consumer_error_socket
= sock
;
1258 * Set the command socket path.
1260 void lttng_consumer_set_command_sock_path(
1261 struct lttng_consumer_local_data
*ctx
, char *sock
)
1263 ctx
->consumer_command_sock_path
= sock
;
1267 * Send return code to the session daemon.
1268 * If the socket is not defined, we return 0, it is not a fatal error
1270 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1272 if (ctx
->consumer_error_socket
> 0) {
1273 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1274 sizeof(enum lttcomm_sessiond_command
));
1281 * Close all the tracefiles and stream fds and MUST be called when all
1282 * instances are destroyed i.e. when all threads were joined and are ended.
1284 void lttng_consumer_cleanup(void)
1286 struct lttng_ht_iter iter
;
1287 struct lttng_consumer_channel
*channel
;
1288 unsigned int trace_chunks_left
;
1292 cds_lfht_for_each_entry(the_consumer_data
.channel_ht
->ht
, &iter
.iter
,
1293 channel
, node
.node
) {
1294 consumer_del_channel(channel
);
1299 lttng_ht_destroy(the_consumer_data
.channel_ht
);
1300 lttng_ht_destroy(the_consumer_data
.channels_by_session_id_ht
);
1302 cleanup_relayd_ht();
1304 lttng_ht_destroy(the_consumer_data
.stream_per_chan_id_ht
);
1307 * This HT contains streams that are freed by either the metadata thread or
1308 * the data thread so we do *nothing* on the hash table and simply destroy
1311 lttng_ht_destroy(the_consumer_data
.stream_list_ht
);
1314 * Trace chunks in the registry may still exist if the session
1315 * daemon has encountered an internal error and could not
1316 * tear down its sessions and/or trace chunks properly.
1318 * Release the session daemon's implicit reference to any remaining
1319 * trace chunk and print an error if any trace chunk was found. Note
1320 * that there are _no_ legitimate cases for trace chunks to be left,
1321 * it is a leak. However, it can happen following a crash of the
1322 * session daemon and not emptying the registry would cause an assertion
1325 trace_chunks_left
= lttng_trace_chunk_registry_put_each_chunk(
1326 the_consumer_data
.chunk_registry
);
1327 if (trace_chunks_left
) {
1328 ERR("%u trace chunks are leaked by lttng-consumerd. "
1329 "This can be caused by an internal error of the session daemon.",
1332 /* Run all callbacks freeing each chunk. */
1334 lttng_trace_chunk_registry_destroy(the_consumer_data
.chunk_registry
);
1338 * Called from signal handler.
1340 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1344 CMM_STORE_SHARED(consumer_quit
, 1);
1345 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1347 PERROR("write consumer quit");
1350 DBG("Consumer flag that it should quit");
1355 * Flush pending writes to trace output disk file.
1358 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1362 int outfd
= stream
->out_fd
;
1365 * This does a blocking write-and-wait on any page that belongs to the
1366 * subbuffer prior to the one we just wrote.
1367 * Don't care about error values, as these are just hints and ways to
1368 * limit the amount of page cache used.
1370 if (orig_offset
< stream
->max_sb_size
) {
1373 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1374 stream
->max_sb_size
,
1375 SYNC_FILE_RANGE_WAIT_BEFORE
1376 | SYNC_FILE_RANGE_WRITE
1377 | SYNC_FILE_RANGE_WAIT_AFTER
);
1379 * Give hints to the kernel about how we access the file:
1380 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1383 * We need to call fadvise again after the file grows because the
1384 * kernel does not seem to apply fadvise to non-existing parts of the
1387 * Call fadvise _after_ having waited for the page writeback to
1388 * complete because the dirty page writeback semantic is not well
1389 * defined. So it can be expected to lead to lower throughput in
1392 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1393 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1394 if (ret
&& ret
!= -ENOSYS
) {
1396 PERROR("posix_fadvise on fd %i", outfd
);
1401 * Initialise the necessary environnement :
1402 * - create a new context
1403 * - create the poll_pipe
1404 * - create the should_quit pipe (for signal handler)
1405 * - create the thread pipe (for splice)
1407 * Takes a function pointer as argument, this function is called when data is
1408 * available on a buffer. This function is responsible to do the
1409 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1410 * buffer configuration and then kernctl_put_next_subbuf at the end.
1412 * Returns a pointer to the new context or NULL on error.
1414 struct lttng_consumer_local_data
*lttng_consumer_create(
1415 enum lttng_consumer_type type
,
1416 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1417 struct lttng_consumer_local_data
*ctx
, bool locked_by_caller
),
1418 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1419 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1420 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1423 struct lttng_consumer_local_data
*ctx
;
1425 LTTNG_ASSERT(the_consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1426 the_consumer_data
.type
== type
);
1427 the_consumer_data
.type
= type
;
1429 ctx
= (lttng_consumer_local_data
*) zmalloc(sizeof(struct lttng_consumer_local_data
));
1431 PERROR("allocating context");
1435 ctx
->consumer_error_socket
= -1;
1436 ctx
->consumer_metadata_socket
= -1;
1437 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1438 /* assign the callbacks */
1439 ctx
->on_buffer_ready
= buffer_ready
;
1440 ctx
->on_recv_channel
= recv_channel
;
1441 ctx
->on_recv_stream
= recv_stream
;
1442 ctx
->on_update_stream
= update_stream
;
1444 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1445 if (!ctx
->consumer_data_pipe
) {
1446 goto error_poll_pipe
;
1449 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1450 if (!ctx
->consumer_wakeup_pipe
) {
1451 goto error_wakeup_pipe
;
1454 ret
= pipe(ctx
->consumer_should_quit
);
1456 PERROR("Error creating recv pipe");
1457 goto error_quit_pipe
;
1460 ret
= pipe(ctx
->consumer_channel_pipe
);
1462 PERROR("Error creating channel pipe");
1463 goto error_channel_pipe
;
1466 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1467 if (!ctx
->consumer_metadata_pipe
) {
1468 goto error_metadata_pipe
;
1471 ctx
->channel_monitor_pipe
= -1;
1475 error_metadata_pipe
:
1476 utils_close_pipe(ctx
->consumer_channel_pipe
);
1478 utils_close_pipe(ctx
->consumer_should_quit
);
1480 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1482 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1490 * Iterate over all streams of the hashtable and free them properly.
1492 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1494 struct lttng_ht_iter iter
;
1495 struct lttng_consumer_stream
*stream
;
1502 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1504 * Ignore return value since we are currently cleaning up so any error
1507 (void) consumer_del_stream(stream
, ht
);
1511 lttng_ht_destroy(ht
);
1515 * Iterate over all streams of the metadata hashtable and free them
1518 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1520 struct lttng_ht_iter iter
;
1521 struct lttng_consumer_stream
*stream
;
1528 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1530 * Ignore return value since we are currently cleaning up so any error
1533 (void) consumer_del_metadata_stream(stream
, ht
);
1537 lttng_ht_destroy(ht
);
1541 * Close all fds associated with the instance and free the context.
1543 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1547 DBG("Consumer destroying it. Closing everything.");
1553 destroy_data_stream_ht(data_ht
);
1554 destroy_metadata_stream_ht(metadata_ht
);
1556 ret
= close(ctx
->consumer_error_socket
);
1560 ret
= close(ctx
->consumer_metadata_socket
);
1564 utils_close_pipe(ctx
->consumer_channel_pipe
);
1565 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1566 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1567 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1568 utils_close_pipe(ctx
->consumer_should_quit
);
1570 unlink(ctx
->consumer_command_sock_path
);
1575 * Write the metadata stream id on the specified file descriptor.
1577 static int write_relayd_metadata_id(int fd
,
1578 struct lttng_consumer_stream
*stream
,
1579 unsigned long padding
)
1582 struct lttcomm_relayd_metadata_payload hdr
;
1584 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1585 hdr
.padding_size
= htobe32(padding
);
1586 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1587 if (ret
< sizeof(hdr
)) {
1589 * This error means that the fd's end is closed so ignore the PERROR
1590 * not to clubber the error output since this can happen in a normal
1593 if (errno
!= EPIPE
) {
1594 PERROR("write metadata stream id");
1596 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1598 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1599 * handle writting the missing part so report that as an error and
1600 * don't lie to the caller.
1605 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1606 stream
->relayd_stream_id
, padding
);
1613 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1614 * core function for writing trace buffers to either the local filesystem or
1617 * It must be called with the stream and the channel lock held.
1619 * Careful review MUST be put if any changes occur!
1621 * Returns the number of bytes written
1623 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1624 struct lttng_consumer_stream
*stream
,
1625 const struct lttng_buffer_view
*buffer
,
1626 unsigned long padding
)
1629 off_t orig_offset
= stream
->out_fd_offset
;
1630 /* Default is on the disk */
1631 int outfd
= stream
->out_fd
;
1632 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1633 unsigned int relayd_hang_up
= 0;
1634 const size_t subbuf_content_size
= buffer
->size
- padding
;
1637 /* RCU lock for the relayd pointer */
1639 LTTNG_ASSERT(stream
->net_seq_idx
!= (uint64_t) -1ULL ||
1640 stream
->trace_chunk
);
1642 /* Flag that the current stream if set for network streaming. */
1643 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1644 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1645 if (relayd
== NULL
) {
1651 /* Handle stream on the relayd if the output is on the network */
1653 unsigned long netlen
= subbuf_content_size
;
1656 * Lock the control socket for the complete duration of the function
1657 * since from this point on we will use the socket.
1659 if (stream
->metadata_flag
) {
1660 /* Metadata requires the control socket. */
1661 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1662 if (stream
->reset_metadata_flag
) {
1663 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1664 stream
->relayd_stream_id
,
1665 stream
->metadata_version
);
1670 stream
->reset_metadata_flag
= 0;
1672 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1675 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1680 /* Use the returned socket. */
1683 /* Write metadata stream id before payload */
1684 if (stream
->metadata_flag
) {
1685 ret
= write_relayd_metadata_id(outfd
, stream
, padding
);
1692 write_len
= subbuf_content_size
;
1694 /* No streaming; we have to write the full padding. */
1695 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1696 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1698 ERR("Reset metadata file");
1701 stream
->reset_metadata_flag
= 0;
1705 * Check if we need to change the tracefile before writing the packet.
1707 if (stream
->chan
->tracefile_size
> 0 &&
1708 (stream
->tracefile_size_current
+ buffer
->size
) >
1709 stream
->chan
->tracefile_size
) {
1710 ret
= consumer_stream_rotate_output_files(stream
);
1714 outfd
= stream
->out_fd
;
1717 stream
->tracefile_size_current
+= buffer
->size
;
1718 write_len
= buffer
->size
;
1722 * This call guarantee that len or less is returned. It's impossible to
1723 * receive a ret value that is bigger than len.
1725 ret
= lttng_write(outfd
, buffer
->data
, write_len
);
1726 DBG("Consumer mmap write() ret %zd (len %zu)", ret
, write_len
);
1727 if (ret
< 0 || ((size_t) ret
!= write_len
)) {
1729 * Report error to caller if nothing was written else at least send the
1737 /* Socket operation failed. We consider the relayd dead */
1738 if (errno
== EPIPE
) {
1740 * This is possible if the fd is closed on the other side
1741 * (outfd) or any write problem. It can be verbose a bit for a
1742 * normal execution if for instance the relayd is stopped
1743 * abruptly. This can happen so set this to a DBG statement.
1745 DBG("Consumer mmap write detected relayd hang up");
1747 /* Unhandled error, print it and stop function right now. */
1748 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret
,
1753 stream
->output_written
+= ret
;
1755 /* This call is useless on a socket so better save a syscall. */
1757 /* This won't block, but will start writeout asynchronously */
1758 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, write_len
,
1759 SYNC_FILE_RANGE_WRITE
);
1760 stream
->out_fd_offset
+= write_len
;
1761 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1766 * This is a special case that the relayd has closed its socket. Let's
1767 * cleanup the relayd object and all associated streams.
1769 if (relayd
&& relayd_hang_up
) {
1770 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1771 lttng_consumer_cleanup_relayd(relayd
);
1775 /* Unlock only if ctrl socket used */
1776 if (relayd
&& stream
->metadata_flag
) {
1777 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1785 * Splice the data from the ring buffer to the tracefile.
1787 * It must be called with the stream lock held.
1789 * Returns the number of bytes spliced.
1791 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1792 struct lttng_consumer_local_data
*ctx
,
1793 struct lttng_consumer_stream
*stream
, unsigned long len
,
1794 unsigned long padding
)
1796 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1798 off_t orig_offset
= stream
->out_fd_offset
;
1799 int fd
= stream
->wait_fd
;
1800 /* Default is on the disk */
1801 int outfd
= stream
->out_fd
;
1802 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1804 unsigned int relayd_hang_up
= 0;
1806 switch (the_consumer_data
.type
) {
1807 case LTTNG_CONSUMER_KERNEL
:
1809 case LTTNG_CONSUMER32_UST
:
1810 case LTTNG_CONSUMER64_UST
:
1811 /* Not supported for user space tracing */
1814 ERR("Unknown consumer_data type");
1818 /* RCU lock for the relayd pointer */
1821 /* Flag that the current stream if set for network streaming. */
1822 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1823 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1824 if (relayd
== NULL
) {
1829 splice_pipe
= stream
->splice_pipe
;
1831 /* Write metadata stream id before payload */
1833 unsigned long total_len
= len
;
1835 if (stream
->metadata_flag
) {
1837 * Lock the control socket for the complete duration of the function
1838 * since from this point on we will use the socket.
1840 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1842 if (stream
->reset_metadata_flag
) {
1843 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1844 stream
->relayd_stream_id
,
1845 stream
->metadata_version
);
1850 stream
->reset_metadata_flag
= 0;
1852 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
,
1860 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1863 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1869 /* Use the returned socket. */
1872 /* No streaming, we have to set the len with the full padding */
1875 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1876 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1878 ERR("Reset metadata file");
1881 stream
->reset_metadata_flag
= 0;
1884 * Check if we need to change the tracefile before writing the packet.
1886 if (stream
->chan
->tracefile_size
> 0 &&
1887 (stream
->tracefile_size_current
+ len
) >
1888 stream
->chan
->tracefile_size
) {
1889 ret
= consumer_stream_rotate_output_files(stream
);
1894 outfd
= stream
->out_fd
;
1897 stream
->tracefile_size_current
+= len
;
1901 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1902 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1903 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1904 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1905 DBG("splice chan to pipe, ret %zd", ret_splice
);
1906 if (ret_splice
< 0) {
1909 PERROR("Error in relay splice");
1913 /* Handle stream on the relayd if the output is on the network */
1914 if (relayd
&& stream
->metadata_flag
) {
1915 size_t metadata_payload_size
=
1916 sizeof(struct lttcomm_relayd_metadata_payload
);
1918 /* Update counter to fit the spliced data */
1919 ret_splice
+= metadata_payload_size
;
1920 len
+= metadata_payload_size
;
1922 * We do this so the return value can match the len passed as
1923 * argument to this function.
1925 written
-= metadata_payload_size
;
1928 /* Splice data out */
1929 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1930 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1931 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1933 if (ret_splice
< 0) {
1938 } else if (ret_splice
> len
) {
1940 * We don't expect this code path to be executed but you never know
1941 * so this is an extra protection agains a buggy splice().
1944 written
+= ret_splice
;
1945 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1949 /* All good, update current len and continue. */
1953 /* This call is useless on a socket so better save a syscall. */
1955 /* This won't block, but will start writeout asynchronously */
1956 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1957 SYNC_FILE_RANGE_WRITE
);
1958 stream
->out_fd_offset
+= ret_splice
;
1960 stream
->output_written
+= ret_splice
;
1961 written
+= ret_splice
;
1964 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1970 * This is a special case that the relayd has closed its socket. Let's
1971 * cleanup the relayd object and all associated streams.
1973 if (relayd
&& relayd_hang_up
) {
1974 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1975 lttng_consumer_cleanup_relayd(relayd
);
1976 /* Skip splice error so the consumer does not fail */
1981 /* send the appropriate error description to sessiond */
1984 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1987 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1990 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1995 if (relayd
&& stream
->metadata_flag
) {
1996 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2004 * Sample the snapshot positions for a specific fd
2006 * Returns 0 on success, < 0 on error
2008 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
2010 switch (the_consumer_data
.type
) {
2011 case LTTNG_CONSUMER_KERNEL
:
2012 return lttng_kconsumer_sample_snapshot_positions(stream
);
2013 case LTTNG_CONSUMER32_UST
:
2014 case LTTNG_CONSUMER64_UST
:
2015 return lttng_ustconsumer_sample_snapshot_positions(stream
);
2017 ERR("Unknown consumer_data type");
2023 * Take a snapshot for a specific fd
2025 * Returns 0 on success, < 0 on error
2027 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2029 switch (the_consumer_data
.type
) {
2030 case LTTNG_CONSUMER_KERNEL
:
2031 return lttng_kconsumer_take_snapshot(stream
);
2032 case LTTNG_CONSUMER32_UST
:
2033 case LTTNG_CONSUMER64_UST
:
2034 return lttng_ustconsumer_take_snapshot(stream
);
2036 ERR("Unknown consumer_data type");
2043 * Get the produced position
2045 * Returns 0 on success, < 0 on error
2047 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
2050 switch (the_consumer_data
.type
) {
2051 case LTTNG_CONSUMER_KERNEL
:
2052 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
2053 case LTTNG_CONSUMER32_UST
:
2054 case LTTNG_CONSUMER64_UST
:
2055 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2057 ERR("Unknown consumer_data type");
2064 * Get the consumed position (free-running counter position in bytes).
2066 * Returns 0 on success, < 0 on error
2068 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
2071 switch (the_consumer_data
.type
) {
2072 case LTTNG_CONSUMER_KERNEL
:
2073 return lttng_kconsumer_get_consumed_snapshot(stream
, pos
);
2074 case LTTNG_CONSUMER32_UST
:
2075 case LTTNG_CONSUMER64_UST
:
2076 return lttng_ustconsumer_get_consumed_snapshot(stream
, pos
);
2078 ERR("Unknown consumer_data type");
2084 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2085 int sock
, struct pollfd
*consumer_sockpoll
)
2087 switch (the_consumer_data
.type
) {
2088 case LTTNG_CONSUMER_KERNEL
:
2089 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2090 case LTTNG_CONSUMER32_UST
:
2091 case LTTNG_CONSUMER64_UST
:
2092 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2094 ERR("Unknown consumer_data type");
2101 void lttng_consumer_close_all_metadata(void)
2103 switch (the_consumer_data
.type
) {
2104 case LTTNG_CONSUMER_KERNEL
:
2106 * The Kernel consumer has a different metadata scheme so we don't
2107 * close anything because the stream will be closed by the session
2111 case LTTNG_CONSUMER32_UST
:
2112 case LTTNG_CONSUMER64_UST
:
2114 * Close all metadata streams. The metadata hash table is passed and
2115 * this call iterates over it by closing all wakeup fd. This is safe
2116 * because at this point we are sure that the metadata producer is
2117 * either dead or blocked.
2119 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2122 ERR("Unknown consumer_data type");
2128 * Clean up a metadata stream and free its memory.
2130 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2131 struct lttng_ht
*ht
)
2133 struct lttng_consumer_channel
*channel
= NULL
;
2134 bool free_channel
= false;
2136 LTTNG_ASSERT(stream
);
2138 * This call should NEVER receive regular stream. It must always be
2139 * metadata stream and this is crucial for data structure synchronization.
2141 LTTNG_ASSERT(stream
->metadata_flag
);
2143 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2145 pthread_mutex_lock(&the_consumer_data
.lock
);
2147 * Note that this assumes that a stream's channel is never changed and
2148 * that the stream's lock doesn't need to be taken to sample its
2151 channel
= stream
->chan
;
2152 pthread_mutex_lock(&channel
->lock
);
2153 pthread_mutex_lock(&stream
->lock
);
2154 if (channel
->metadata_cache
) {
2155 /* Only applicable to userspace consumers. */
2156 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
2159 /* Remove any reference to that stream. */
2160 consumer_stream_delete(stream
, ht
);
2162 /* Close down everything including the relayd if one. */
2163 consumer_stream_close(stream
);
2164 /* Destroy tracer buffers of the stream. */
2165 consumer_stream_destroy_buffers(stream
);
2167 /* Atomically decrement channel refcount since other threads can use it. */
2168 if (!uatomic_sub_return(&channel
->refcount
, 1)
2169 && !uatomic_read(&channel
->nb_init_stream_left
)) {
2170 /* Go for channel deletion! */
2171 free_channel
= true;
2173 stream
->chan
= NULL
;
2176 * Nullify the stream reference so it is not used after deletion. The
2177 * channel lock MUST be acquired before being able to check for a NULL
2180 channel
->metadata_stream
= NULL
;
2182 if (channel
->metadata_cache
) {
2183 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
2185 pthread_mutex_unlock(&stream
->lock
);
2186 pthread_mutex_unlock(&channel
->lock
);
2187 pthread_mutex_unlock(&the_consumer_data
.lock
);
2190 consumer_del_channel(channel
);
2193 lttng_trace_chunk_put(stream
->trace_chunk
);
2194 stream
->trace_chunk
= NULL
;
2195 consumer_stream_free(stream
);
2199 * Action done with the metadata stream when adding it to the consumer internal
2200 * data structures to handle it.
2202 void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2204 struct lttng_ht
*ht
= metadata_ht
;
2205 struct lttng_ht_iter iter
;
2206 struct lttng_ht_node_u64
*node
;
2208 LTTNG_ASSERT(stream
);
2211 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2213 pthread_mutex_lock(&the_consumer_data
.lock
);
2214 pthread_mutex_lock(&stream
->chan
->lock
);
2215 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2216 pthread_mutex_lock(&stream
->lock
);
2219 * From here, refcounts are updated so be _careful_ when returning an error
2226 * Lookup the stream just to make sure it does not exist in our internal
2227 * state. This should NEVER happen.
2229 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2230 node
= lttng_ht_iter_get_node_u64(&iter
);
2231 LTTNG_ASSERT(!node
);
2234 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2235 * in terms of destroying the associated channel, because the action that
2236 * causes the count to become 0 also causes a stream to be added. The
2237 * channel deletion will thus be triggered by the following removal of this
2240 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2241 /* Increment refcount before decrementing nb_init_stream_left */
2243 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2246 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2248 lttng_ht_add_u64(the_consumer_data
.stream_per_chan_id_ht
,
2249 &stream
->node_channel_id
);
2252 * Add stream to the stream_list_ht of the consumer data. No need to steal
2253 * the key since the HT does not use it and we allow to add redundant keys
2256 lttng_ht_add_u64(the_consumer_data
.stream_list_ht
,
2257 &stream
->node_session_id
);
2261 pthread_mutex_unlock(&stream
->lock
);
2262 pthread_mutex_unlock(&stream
->chan
->lock
);
2263 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2264 pthread_mutex_unlock(&the_consumer_data
.lock
);
2268 * Delete data stream that are flagged for deletion (endpoint_status).
2270 static void validate_endpoint_status_data_stream(void)
2272 struct lttng_ht_iter iter
;
2273 struct lttng_consumer_stream
*stream
;
2275 DBG("Consumer delete flagged data stream");
2278 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2279 /* Validate delete flag of the stream */
2280 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2283 /* Delete it right now */
2284 consumer_del_stream(stream
, data_ht
);
2290 * Delete metadata stream that are flagged for deletion (endpoint_status).
2292 static void validate_endpoint_status_metadata_stream(
2293 struct lttng_poll_event
*pollset
)
2295 struct lttng_ht_iter iter
;
2296 struct lttng_consumer_stream
*stream
;
2298 DBG("Consumer delete flagged metadata stream");
2300 LTTNG_ASSERT(pollset
);
2303 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2304 /* Validate delete flag of the stream */
2305 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2309 * Remove from pollset so the metadata thread can continue without
2310 * blocking on a deleted stream.
2312 lttng_poll_del(pollset
, stream
->wait_fd
);
2314 /* Delete it right now */
2315 consumer_del_metadata_stream(stream
, metadata_ht
);
2321 * Thread polls on metadata file descriptor and write them on disk or on the
2324 void *consumer_thread_metadata_poll(void *data
)
2326 int ret
, i
, pollfd
, err
= -1;
2327 uint32_t revents
, nb_fd
;
2328 struct lttng_consumer_stream
*stream
= NULL
;
2329 struct lttng_ht_iter iter
;
2330 struct lttng_ht_node_u64
*node
;
2331 struct lttng_poll_event events
;
2332 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
2335 rcu_register_thread();
2337 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2339 if (testpoint(consumerd_thread_metadata
)) {
2340 goto error_testpoint
;
2343 health_code_update();
2345 DBG("Thread metadata poll started");
2347 /* Size is set to 1 for the consumer_metadata pipe */
2348 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2350 ERR("Poll set creation failed");
2354 ret
= lttng_poll_add(&events
,
2355 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2361 DBG("Metadata main loop started");
2365 health_code_update();
2366 health_poll_entry();
2367 DBG("Metadata poll wait");
2368 ret
= lttng_poll_wait(&events
, -1);
2369 DBG("Metadata poll return from wait with %d fd(s)",
2370 LTTNG_POLL_GETNB(&events
));
2372 DBG("Metadata event caught in thread");
2374 if (errno
== EINTR
) {
2375 ERR("Poll EINTR caught");
2378 if (LTTNG_POLL_GETNB(&events
) == 0) {
2379 err
= 0; /* All is OK */
2386 /* From here, the event is a metadata wait fd */
2387 for (i
= 0; i
< nb_fd
; i
++) {
2388 health_code_update();
2390 revents
= LTTNG_POLL_GETEV(&events
, i
);
2391 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2393 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2394 if (revents
& LPOLLIN
) {
2397 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2398 &stream
, sizeof(stream
));
2399 if (pipe_len
< sizeof(stream
)) {
2401 PERROR("read metadata stream");
2404 * Remove the pipe from the poll set and continue the loop
2405 * since their might be data to consume.
2407 lttng_poll_del(&events
,
2408 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2409 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2413 /* A NULL stream means that the state has changed. */
2414 if (stream
== NULL
) {
2415 /* Check for deleted streams. */
2416 validate_endpoint_status_metadata_stream(&events
);
2420 DBG("Adding metadata stream %d to poll set",
2423 /* Add metadata stream to the global poll events list */
2424 lttng_poll_add(&events
, stream
->wait_fd
,
2425 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2426 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2427 DBG("Metadata thread pipe hung up");
2429 * Remove the pipe from the poll set and continue the loop
2430 * since their might be data to consume.
2432 lttng_poll_del(&events
,
2433 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2434 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2437 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2441 /* Handle other stream */
2447 uint64_t tmp_id
= (uint64_t) pollfd
;
2449 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2451 node
= lttng_ht_iter_get_node_u64(&iter
);
2454 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2457 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2458 /* Get the data out of the metadata file descriptor */
2459 DBG("Metadata available on fd %d", pollfd
);
2460 LTTNG_ASSERT(stream
->wait_fd
== pollfd
);
2463 health_code_update();
2465 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2467 * We don't check the return value here since if we get
2468 * a negative len, it means an error occurred thus we
2469 * simply remove it from the poll set and free the
2474 /* It's ok to have an unavailable sub-buffer */
2475 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2476 /* Clean up stream from consumer and free it. */
2477 lttng_poll_del(&events
, stream
->wait_fd
);
2478 consumer_del_metadata_stream(stream
, metadata_ht
);
2480 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2481 DBG("Metadata fd %d is hup|err.", pollfd
);
2482 if (!stream
->hangup_flush_done
&&
2483 (the_consumer_data
.type
== LTTNG_CONSUMER32_UST
||
2484 the_consumer_data
.type
==
2485 LTTNG_CONSUMER64_UST
)) {
2486 DBG("Attempting to flush and consume the UST buffers");
2487 lttng_ustconsumer_on_stream_hangup(stream
);
2489 /* We just flushed the stream now read it. */
2491 health_code_update();
2493 len
= ctx
->on_buffer_ready(stream
, ctx
, false);
2495 * We don't check the return value here since if we get
2496 * a negative len, it means an error occurred thus we
2497 * simply remove it from the poll set and free the
2503 lttng_poll_del(&events
, stream
->wait_fd
);
2505 * This call update the channel states, closes file descriptors
2506 * and securely free the stream.
2508 consumer_del_metadata_stream(stream
, metadata_ht
);
2510 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2514 /* Release RCU lock for the stream looked up */
2522 DBG("Metadata poll thread exiting");
2524 lttng_poll_clean(&events
);
2529 ERR("Health error occurred in %s", __func__
);
2531 health_unregister(health_consumerd
);
2532 rcu_unregister_thread();
2537 * This thread polls the fds in the set to consume the data and write
2538 * it to tracefile if necessary.
2540 void *consumer_thread_data_poll(void *data
)
2542 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2543 struct pollfd
*pollfd
= NULL
;
2544 /* local view of the streams */
2545 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2546 /* local view of consumer_data.fds_count */
2548 /* 2 for the consumer_data_pipe and wake up pipe */
2549 const int nb_pipes_fd
= 2;
2550 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2551 int nb_inactive_fd
= 0;
2552 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
2555 rcu_register_thread();
2557 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2559 if (testpoint(consumerd_thread_data
)) {
2560 goto error_testpoint
;
2563 health_code_update();
2565 local_stream
= (lttng_consumer_stream
**) zmalloc(sizeof(struct lttng_consumer_stream
*));
2566 if (local_stream
== NULL
) {
2567 PERROR("local_stream malloc");
2572 health_code_update();
2578 * the fds set has been updated, we need to update our
2579 * local array as well
2581 pthread_mutex_lock(&the_consumer_data
.lock
);
2582 if (the_consumer_data
.need_update
) {
2587 local_stream
= NULL
;
2589 /* Allocate for all fds */
2590 pollfd
= (struct pollfd
*) zmalloc((the_consumer_data
.stream_count
+
2592 sizeof(struct pollfd
));
2593 if (pollfd
== NULL
) {
2594 PERROR("pollfd malloc");
2595 pthread_mutex_unlock(&the_consumer_data
.lock
);
2599 local_stream
= (lttng_consumer_stream
**) zmalloc((the_consumer_data
.stream_count
+
2601 sizeof(struct lttng_consumer_stream
*));
2602 if (local_stream
== NULL
) {
2603 PERROR("local_stream malloc");
2604 pthread_mutex_unlock(&the_consumer_data
.lock
);
2607 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2608 data_ht
, &nb_inactive_fd
);
2610 ERR("Error in allocating pollfd or local_outfds");
2611 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2612 pthread_mutex_unlock(&the_consumer_data
.lock
);
2616 the_consumer_data
.need_update
= 0;
2618 pthread_mutex_unlock(&the_consumer_data
.lock
);
2620 /* No FDs and consumer_quit, consumer_cleanup the thread */
2621 if (nb_fd
== 0 && nb_inactive_fd
== 0 &&
2622 CMM_LOAD_SHARED(consumer_quit
) == 1) {
2623 err
= 0; /* All is OK */
2626 /* poll on the array of fds */
2628 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2629 if (testpoint(consumerd_thread_data_poll
)) {
2632 health_poll_entry();
2633 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2635 DBG("poll num_rdy : %d", num_rdy
);
2636 if (num_rdy
== -1) {
2638 * Restart interrupted system call.
2640 if (errno
== EINTR
) {
2643 PERROR("Poll error");
2644 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2646 } else if (num_rdy
== 0) {
2647 DBG("Polling thread timed out");
2651 if (caa_unlikely(data_consumption_paused
)) {
2652 DBG("Data consumption paused, sleeping...");
2658 * If the consumer_data_pipe triggered poll go directly to the
2659 * beginning of the loop to update the array. We want to prioritize
2660 * array update over low-priority reads.
2662 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2663 ssize_t pipe_readlen
;
2665 DBG("consumer_data_pipe wake up");
2666 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2667 &new_stream
, sizeof(new_stream
));
2668 if (pipe_readlen
< sizeof(new_stream
)) {
2669 PERROR("Consumer data pipe");
2670 /* Continue so we can at least handle the current stream(s). */
2675 * If the stream is NULL, just ignore it. It's also possible that
2676 * the sessiond poll thread changed the consumer_quit state and is
2677 * waking us up to test it.
2679 if (new_stream
== NULL
) {
2680 validate_endpoint_status_data_stream();
2684 /* Continue to update the local streams and handle prio ones */
2688 /* Handle wakeup pipe. */
2689 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2691 ssize_t pipe_readlen
;
2693 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2695 if (pipe_readlen
< 0) {
2696 PERROR("Consumer data wakeup pipe");
2698 /* We've been awakened to handle stream(s). */
2699 ctx
->has_wakeup
= 0;
2702 /* Take care of high priority channels first. */
2703 for (i
= 0; i
< nb_fd
; i
++) {
2704 health_code_update();
2706 if (local_stream
[i
] == NULL
) {
2709 if (pollfd
[i
].revents
& POLLPRI
) {
2710 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2712 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2713 /* it's ok to have an unavailable sub-buffer */
2714 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2715 /* Clean the stream and free it. */
2716 consumer_del_stream(local_stream
[i
], data_ht
);
2717 local_stream
[i
] = NULL
;
2718 } else if (len
> 0) {
2719 local_stream
[i
]->data_read
= 1;
2725 * If we read high prio channel in this loop, try again
2726 * for more high prio data.
2732 /* Take care of low priority channels. */
2733 for (i
= 0; i
< nb_fd
; i
++) {
2734 health_code_update();
2736 if (local_stream
[i
] == NULL
) {
2739 if ((pollfd
[i
].revents
& POLLIN
) ||
2740 local_stream
[i
]->hangup_flush_done
||
2741 local_stream
[i
]->has_data
) {
2742 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2743 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
, false);
2744 /* it's ok to have an unavailable sub-buffer */
2745 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2746 /* Clean the stream and free it. */
2747 consumer_del_stream(local_stream
[i
], data_ht
);
2748 local_stream
[i
] = NULL
;
2749 } else if (len
> 0) {
2750 local_stream
[i
]->data_read
= 1;
2755 /* Handle hangup and errors */
2756 for (i
= 0; i
< nb_fd
; i
++) {
2757 health_code_update();
2759 if (local_stream
[i
] == NULL
) {
2762 if (!local_stream
[i
]->hangup_flush_done
2763 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2764 && (the_consumer_data
.type
== LTTNG_CONSUMER32_UST
2765 || the_consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2766 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2768 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2769 /* Attempt read again, for the data we just flushed. */
2770 local_stream
[i
]->data_read
= 1;
2773 * If the poll flag is HUP/ERR/NVAL and we have
2774 * read no data in this pass, we can remove the
2775 * stream from its hash table.
2777 if ((pollfd
[i
].revents
& POLLHUP
)) {
2778 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2779 if (!local_stream
[i
]->data_read
) {
2780 consumer_del_stream(local_stream
[i
], data_ht
);
2781 local_stream
[i
] = NULL
;
2784 } else if (pollfd
[i
].revents
& POLLERR
) {
2785 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2786 if (!local_stream
[i
]->data_read
) {
2787 consumer_del_stream(local_stream
[i
], data_ht
);
2788 local_stream
[i
] = NULL
;
2791 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2792 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2793 if (!local_stream
[i
]->data_read
) {
2794 consumer_del_stream(local_stream
[i
], data_ht
);
2795 local_stream
[i
] = NULL
;
2799 if (local_stream
[i
] != NULL
) {
2800 local_stream
[i
]->data_read
= 0;
2807 DBG("polling thread exiting");
2812 * Close the write side of the pipe so epoll_wait() in
2813 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2814 * read side of the pipe. If we close them both, epoll_wait strangely does
2815 * not return and could create a endless wait period if the pipe is the
2816 * only tracked fd in the poll set. The thread will take care of closing
2819 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2824 ERR("Health error occurred in %s", __func__
);
2826 health_unregister(health_consumerd
);
2828 rcu_unregister_thread();
2833 * Close wake-up end of each stream belonging to the channel. This will
2834 * allow the poll() on the stream read-side to detect when the
2835 * write-side (application) finally closes them.
2838 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2840 struct lttng_ht
*ht
;
2841 struct lttng_consumer_stream
*stream
;
2842 struct lttng_ht_iter iter
;
2844 ht
= the_consumer_data
.stream_per_chan_id_ht
;
2847 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2848 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2849 ht
->match_fct
, &channel
->key
,
2850 &iter
.iter
, stream
, node_channel_id
.node
) {
2852 * Protect against teardown with mutex.
2854 pthread_mutex_lock(&stream
->lock
);
2855 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2858 switch (the_consumer_data
.type
) {
2859 case LTTNG_CONSUMER_KERNEL
:
2861 case LTTNG_CONSUMER32_UST
:
2862 case LTTNG_CONSUMER64_UST
:
2863 if (stream
->metadata_flag
) {
2864 /* Safe and protected by the stream lock. */
2865 lttng_ustconsumer_close_metadata(stream
->chan
);
2868 * Note: a mutex is taken internally within
2869 * liblttng-ust-ctl to protect timer wakeup_fd
2870 * use from concurrent close.
2872 lttng_ustconsumer_close_stream_wakeup(stream
);
2876 ERR("Unknown consumer_data type");
2880 pthread_mutex_unlock(&stream
->lock
);
2885 static void destroy_channel_ht(struct lttng_ht
*ht
)
2887 struct lttng_ht_iter iter
;
2888 struct lttng_consumer_channel
*channel
;
2896 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2897 ret
= lttng_ht_del(ht
, &iter
);
2898 LTTNG_ASSERT(ret
!= 0);
2902 lttng_ht_destroy(ht
);
2906 * This thread polls the channel fds to detect when they are being
2907 * closed. It closes all related streams if the channel is detected as
2908 * closed. It is currently only used as a shim layer for UST because the
2909 * consumerd needs to keep the per-stream wakeup end of pipes open for
2912 void *consumer_thread_channel_poll(void *data
)
2914 int ret
, i
, pollfd
, err
= -1;
2915 uint32_t revents
, nb_fd
;
2916 struct lttng_consumer_channel
*chan
= NULL
;
2917 struct lttng_ht_iter iter
;
2918 struct lttng_ht_node_u64
*node
;
2919 struct lttng_poll_event events
;
2920 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
2921 struct lttng_ht
*channel_ht
;
2923 rcu_register_thread();
2925 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2927 if (testpoint(consumerd_thread_channel
)) {
2928 goto error_testpoint
;
2931 health_code_update();
2933 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2935 /* ENOMEM at this point. Better to bail out. */
2939 DBG("Thread channel poll started");
2941 /* Size is set to 1 for the consumer_channel pipe */
2942 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2944 ERR("Poll set creation failed");
2948 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2954 DBG("Channel main loop started");
2958 health_code_update();
2959 DBG("Channel poll wait");
2960 health_poll_entry();
2961 ret
= lttng_poll_wait(&events
, -1);
2962 DBG("Channel poll return from wait with %d fd(s)",
2963 LTTNG_POLL_GETNB(&events
));
2965 DBG("Channel event caught in thread");
2967 if (errno
== EINTR
) {
2968 ERR("Poll EINTR caught");
2971 if (LTTNG_POLL_GETNB(&events
) == 0) {
2972 err
= 0; /* All is OK */
2979 /* From here, the event is a channel wait fd */
2980 for (i
= 0; i
< nb_fd
; i
++) {
2981 health_code_update();
2983 revents
= LTTNG_POLL_GETEV(&events
, i
);
2984 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2986 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2987 if (revents
& LPOLLIN
) {
2988 enum consumer_channel_action action
;
2991 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2994 ERR("Error reading channel pipe");
2996 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3001 case CONSUMER_CHANNEL_ADD
:
3002 DBG("Adding channel %d to poll set",
3005 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
3008 lttng_ht_add_unique_u64(channel_ht
,
3009 &chan
->wait_fd_node
);
3011 /* Add channel to the global poll events list */
3012 lttng_poll_add(&events
, chan
->wait_fd
,
3013 LPOLLERR
| LPOLLHUP
);
3015 case CONSUMER_CHANNEL_DEL
:
3018 * This command should never be called if the channel
3019 * has streams monitored by either the data or metadata
3020 * thread. The consumer only notify this thread with a
3021 * channel del. command if it receives a destroy
3022 * channel command from the session daemon that send it
3023 * if a command prior to the GET_CHANNEL failed.
3027 chan
= consumer_find_channel(key
);
3030 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
3033 lttng_poll_del(&events
, chan
->wait_fd
);
3034 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
3035 ret
= lttng_ht_del(channel_ht
, &iter
);
3036 LTTNG_ASSERT(ret
== 0);
3038 switch (the_consumer_data
.type
) {
3039 case LTTNG_CONSUMER_KERNEL
:
3041 case LTTNG_CONSUMER32_UST
:
3042 case LTTNG_CONSUMER64_UST
:
3043 health_code_update();
3044 /* Destroy streams that might have been left in the stream list. */
3045 clean_channel_stream_list(chan
);
3048 ERR("Unknown consumer_data type");
3053 * Release our own refcount. Force channel deletion even if
3054 * streams were not initialized.
3056 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3057 consumer_del_channel(chan
);
3062 case CONSUMER_CHANNEL_QUIT
:
3064 * Remove the pipe from the poll set and continue the loop
3065 * since their might be data to consume.
3067 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3070 ERR("Unknown action");
3073 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3074 DBG("Channel thread pipe hung up");
3076 * Remove the pipe from the poll set and continue the loop
3077 * since their might be data to consume.
3079 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3082 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3086 /* Handle other stream */
3092 uint64_t tmp_id
= (uint64_t) pollfd
;
3094 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3096 node
= lttng_ht_iter_get_node_u64(&iter
);
3099 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
3102 /* Check for error event */
3103 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3104 DBG("Channel fd %d is hup|err.", pollfd
);
3106 lttng_poll_del(&events
, chan
->wait_fd
);
3107 ret
= lttng_ht_del(channel_ht
, &iter
);
3108 LTTNG_ASSERT(ret
== 0);
3111 * This will close the wait fd for each stream associated to
3112 * this channel AND monitored by the data/metadata thread thus
3113 * will be clean by the right thread.
3115 consumer_close_channel_streams(chan
);
3117 /* Release our own refcount */
3118 if (!uatomic_sub_return(&chan
->refcount
, 1)
3119 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3120 consumer_del_channel(chan
);
3123 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3128 /* Release RCU lock for the channel looked up */
3136 lttng_poll_clean(&events
);
3138 destroy_channel_ht(channel_ht
);
3141 DBG("Channel poll thread exiting");
3144 ERR("Health error occurred in %s", __func__
);
3146 health_unregister(health_consumerd
);
3147 rcu_unregister_thread();
3151 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3152 struct pollfd
*sockpoll
, int client_socket
)
3157 LTTNG_ASSERT(sockpoll
);
3159 ret
= lttng_consumer_poll_socket(sockpoll
);
3163 DBG("Metadata connection on client_socket");
3165 /* Blocking call, waiting for transmission */
3166 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3167 if (ctx
->consumer_metadata_socket
< 0) {
3168 WARN("On accept metadata");
3179 * This thread listens on the consumerd socket and receives the file
3180 * descriptors from the session daemon.
3182 void *consumer_thread_sessiond_poll(void *data
)
3184 int sock
= -1, client_socket
, ret
, err
= -1;
3186 * structure to poll for incoming data on communication socket avoids
3187 * making blocking sockets.
3189 struct pollfd consumer_sockpoll
[2];
3190 struct lttng_consumer_local_data
*ctx
= (lttng_consumer_local_data
*) data
;
3192 rcu_register_thread();
3194 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3196 if (testpoint(consumerd_thread_sessiond
)) {
3197 goto error_testpoint
;
3200 health_code_update();
3202 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3203 unlink(ctx
->consumer_command_sock_path
);
3204 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3205 if (client_socket
< 0) {
3206 ERR("Cannot create command socket");
3210 ret
= lttcomm_listen_unix_sock(client_socket
);
3215 DBG("Sending ready command to lttng-sessiond");
3216 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3217 /* return < 0 on error, but == 0 is not fatal */
3219 ERR("Error sending ready command to lttng-sessiond");
3223 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3224 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3225 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3226 consumer_sockpoll
[1].fd
= client_socket
;
3227 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3229 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3237 DBG("Connection on client_socket");
3239 /* Blocking call, waiting for transmission */
3240 sock
= lttcomm_accept_unix_sock(client_socket
);
3247 * Setup metadata socket which is the second socket connection on the
3248 * command unix socket.
3250 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3259 /* This socket is not useful anymore. */
3260 ret
= close(client_socket
);
3262 PERROR("close client_socket");
3266 /* update the polling structure to poll on the established socket */
3267 consumer_sockpoll
[1].fd
= sock
;
3268 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3271 health_code_update();
3273 health_poll_entry();
3274 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3283 DBG("Incoming command on sock");
3284 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3287 * This could simply be a session daemon quitting. Don't output
3290 DBG("Communication interrupted on command socket");
3294 if (CMM_LOAD_SHARED(consumer_quit
)) {
3295 DBG("consumer_thread_receive_fds received quit from signal");
3296 err
= 0; /* All is OK */
3299 DBG("Received command on sock");
3305 DBG("Consumer thread sessiond poll exiting");
3308 * Close metadata streams since the producer is the session daemon which
3311 * NOTE: for now, this only applies to the UST tracer.
3313 lttng_consumer_close_all_metadata();
3316 * when all fds have hung up, the polling thread
3319 CMM_STORE_SHARED(consumer_quit
, 1);
3322 * Notify the data poll thread to poll back again and test the
3323 * consumer_quit state that we just set so to quit gracefully.
3325 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3327 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3329 notify_health_quit_pipe(health_quit_pipe
);
3331 /* Cleaning up possibly open sockets. */
3335 PERROR("close sock sessiond poll");
3338 if (client_socket
>= 0) {
3339 ret
= close(client_socket
);
3341 PERROR("close client_socket sessiond poll");
3348 ERR("Health error occurred in %s", __func__
);
3350 health_unregister(health_consumerd
);
3352 rcu_unregister_thread();
3356 static int post_consume(struct lttng_consumer_stream
*stream
,
3357 const struct stream_subbuffer
*subbuffer
,
3358 struct lttng_consumer_local_data
*ctx
)
3362 const size_t count
= lttng_dynamic_array_get_count(
3363 &stream
->read_subbuffer_ops
.post_consume_cbs
);
3365 for (i
= 0; i
< count
; i
++) {
3366 const post_consume_cb op
= *(post_consume_cb
*) lttng_dynamic_array_get_element(
3367 &stream
->read_subbuffer_ops
.post_consume_cbs
,
3370 ret
= op(stream
, subbuffer
, ctx
);
3379 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3380 struct lttng_consumer_local_data
*ctx
,
3381 bool locked_by_caller
)
3383 ssize_t ret
, written_bytes
= 0;
3385 struct stream_subbuffer subbuffer
= {};
3386 enum get_next_subbuffer_status get_next_status
;
3388 if (!locked_by_caller
) {
3389 stream
->read_subbuffer_ops
.lock(stream
);
3391 stream
->read_subbuffer_ops
.assert_locked(stream
);
3394 if (stream
->read_subbuffer_ops
.on_wake_up
) {
3395 ret
= stream
->read_subbuffer_ops
.on_wake_up(stream
);
3402 * If the stream was flagged to be ready for rotation before we extract
3403 * the next packet, rotate it now.
3405 if (stream
->rotate_ready
) {
3406 DBG("Rotate stream before consuming data");
3407 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3409 ERR("Stream rotation error before consuming data");
3414 get_next_status
= stream
->read_subbuffer_ops
.get_next_subbuffer(
3415 stream
, &subbuffer
);
3416 switch (get_next_status
) {
3417 case GET_NEXT_SUBBUFFER_STATUS_OK
:
3419 case GET_NEXT_SUBBUFFER_STATUS_NO_DATA
:
3423 case GET_NEXT_SUBBUFFER_STATUS_ERROR
:
3430 ret
= stream
->read_subbuffer_ops
.pre_consume_subbuffer(
3431 stream
, &subbuffer
);
3433 goto error_put_subbuf
;
3436 written_bytes
= stream
->read_subbuffer_ops
.consume_subbuffer(
3437 ctx
, stream
, &subbuffer
);
3438 if (written_bytes
<= 0) {
3439 ERR("Error consuming subbuffer: (%zd)", written_bytes
);
3440 ret
= (int) written_bytes
;
3441 goto error_put_subbuf
;
3444 ret
= stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3449 ret
= post_consume(stream
, &subbuffer
, ctx
);
3455 * After extracting the packet, we check if the stream is now ready to
3456 * be rotated and perform the action immediately.
3458 * Don't overwrite `ret` as callers expect the number of bytes
3459 * consumed to be returned on success.
3461 rotation_ret
= lttng_consumer_stream_is_rotate_ready(stream
);
3462 if (rotation_ret
== 1) {
3463 rotation_ret
= lttng_consumer_rotate_stream(ctx
, stream
);
3464 if (rotation_ret
< 0) {
3466 ERR("Stream rotation error after consuming data");
3470 } else if (rotation_ret
< 0) {
3472 ERR("Failed to check if stream was ready to rotate after consuming data");
3477 if (stream
->read_subbuffer_ops
.on_sleep
) {
3478 stream
->read_subbuffer_ops
.on_sleep(stream
, ctx
);
3481 ret
= written_bytes
;
3483 if (!locked_by_caller
) {
3484 stream
->read_subbuffer_ops
.unlock(stream
);
3489 (void) stream
->read_subbuffer_ops
.put_next_subbuffer(stream
, &subbuffer
);
3493 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3495 switch (the_consumer_data
.type
) {
3496 case LTTNG_CONSUMER_KERNEL
:
3497 return lttng_kconsumer_on_recv_stream(stream
);
3498 case LTTNG_CONSUMER32_UST
:
3499 case LTTNG_CONSUMER64_UST
:
3500 return lttng_ustconsumer_on_recv_stream(stream
);
3502 ERR("Unknown consumer_data type");
3509 * Allocate and set consumer data hash tables.
3511 int lttng_consumer_init(void)
3513 the_consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3514 if (!the_consumer_data
.channel_ht
) {
3518 the_consumer_data
.channels_by_session_id_ht
=
3519 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3520 if (!the_consumer_data
.channels_by_session_id_ht
) {
3524 the_consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3525 if (!the_consumer_data
.relayd_ht
) {
3529 the_consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3530 if (!the_consumer_data
.stream_list_ht
) {
3534 the_consumer_data
.stream_per_chan_id_ht
=
3535 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3536 if (!the_consumer_data
.stream_per_chan_id_ht
) {
3540 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3545 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3550 the_consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3551 if (!the_consumer_data
.chunk_registry
) {
3562 * Process the ADD_RELAYD command receive by a consumer.
3564 * This will create a relayd socket pair and add it to the relayd hash table.
3565 * The caller MUST acquire a RCU read side lock before calling it.
3567 void consumer_add_relayd_socket(uint64_t net_seq_idx
,
3569 struct lttng_consumer_local_data
*ctx
,
3571 struct pollfd
*consumer_sockpoll
,
3572 uint64_t sessiond_id
,
3573 uint64_t relayd_session_id
,
3574 uint32_t relayd_version_major
,
3575 uint32_t relayd_version_minor
,
3576 enum lttcomm_sock_proto relayd_socket_protocol
)
3578 int fd
= -1, ret
= -1, relayd_created
= 0;
3579 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3580 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3583 LTTNG_ASSERT(sock
>= 0);
3584 ASSERT_RCU_READ_LOCKED();
3586 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3588 /* Get relayd reference if exists. */
3589 relayd
= consumer_find_relayd(net_seq_idx
);
3590 if (relayd
== NULL
) {
3591 LTTNG_ASSERT(sock_type
== LTTNG_STREAM_CONTROL
);
3592 /* Not found. Allocate one. */
3593 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3594 if (relayd
== NULL
) {
3595 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3598 relayd
->sessiond_session_id
= sessiond_id
;
3603 * This code path MUST continue to the consumer send status message to
3604 * we can notify the session daemon and continue our work without
3605 * killing everything.
3609 * relayd key should never be found for control socket.
3611 LTTNG_ASSERT(sock_type
!= LTTNG_STREAM_CONTROL
);
3614 /* First send a status message before receiving the fds. */
3615 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3617 /* Somehow, the session daemon is not responding anymore. */
3618 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3619 goto error_nosignal
;
3622 /* Poll on consumer socket. */
3623 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3625 /* Needing to exit in the middle of a command: error. */
3626 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3627 goto error_nosignal
;
3630 /* Get relayd socket from session daemon */
3631 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3632 if (ret
!= sizeof(fd
)) {
3633 fd
= -1; /* Just in case it gets set with an invalid value. */
3636 * Failing to receive FDs might indicate a major problem such as
3637 * reaching a fd limit during the receive where the kernel returns a
3638 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3639 * don't take any chances and stop everything.
3641 * XXX: Feature request #558 will fix that and avoid this possible
3642 * issue when reaching the fd limit.
3644 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3645 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3649 /* Copy socket information and received FD */
3650 switch (sock_type
) {
3651 case LTTNG_STREAM_CONTROL
:
3652 /* Copy received lttcomm socket */
3653 ret
= lttcomm_populate_sock_from_open_socket(
3654 &relayd
->control_sock
.sock
, fd
,
3655 relayd_socket_protocol
);
3657 /* Assign version values. */
3658 relayd
->control_sock
.major
= relayd_version_major
;
3659 relayd
->control_sock
.minor
= relayd_version_minor
;
3661 relayd
->relayd_session_id
= relayd_session_id
;
3664 case LTTNG_STREAM_DATA
:
3665 /* Copy received lttcomm socket */
3666 ret
= lttcomm_populate_sock_from_open_socket(
3667 &relayd
->data_sock
.sock
, fd
,
3668 relayd_socket_protocol
);
3669 /* Assign version values. */
3670 relayd
->data_sock
.major
= relayd_version_major
;
3671 relayd
->data_sock
.minor
= relayd_version_minor
;
3674 ERR("Unknown relayd socket type (%d)", sock_type
);
3675 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3680 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3684 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3685 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3686 relayd
->net_seq_idx
, fd
);
3688 * We gave the ownership of the fd to the relayd structure. Set the
3689 * fd to -1 so we don't call close() on it in the error path below.
3693 /* We successfully added the socket. Send status back. */
3694 ret
= consumer_send_status_msg(sock
, ret_code
);
3696 /* Somehow, the session daemon is not responding anymore. */
3697 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3698 goto error_nosignal
;
3702 * Add relayd socket pair to consumer data hashtable. If object already
3703 * exists or on error, the function gracefully returns.
3712 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3713 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3717 /* Close received socket if valid. */
3720 PERROR("close received socket");
3724 if (relayd_created
) {
3730 * Search for a relayd associated to the session id and return the reference.
3732 * A rcu read side lock MUST be acquire before calling this function and locked
3733 * until the relayd object is no longer necessary.
3735 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3737 struct lttng_ht_iter iter
;
3738 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3740 ASSERT_RCU_READ_LOCKED();
3742 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3743 cds_lfht_for_each_entry(the_consumer_data
.relayd_ht
->ht
, &iter
.iter
,
3744 relayd
, node
.node
) {
3746 * Check by sessiond id which is unique here where the relayd session
3747 * id might not be when having multiple relayd.
3749 if (relayd
->sessiond_session_id
== id
) {
3750 /* Found the relayd. There can be only one per id. */
3762 * Check if for a given session id there is still data needed to be extract
3765 * Return 1 if data is pending or else 0 meaning ready to be read.
3767 int consumer_data_pending(uint64_t id
)
3770 struct lttng_ht_iter iter
;
3771 struct lttng_ht
*ht
;
3772 struct lttng_consumer_stream
*stream
;
3773 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3774 int (*data_pending
)(struct lttng_consumer_stream
*);
3776 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3779 pthread_mutex_lock(&the_consumer_data
.lock
);
3781 switch (the_consumer_data
.type
) {
3782 case LTTNG_CONSUMER_KERNEL
:
3783 data_pending
= lttng_kconsumer_data_pending
;
3785 case LTTNG_CONSUMER32_UST
:
3786 case LTTNG_CONSUMER64_UST
:
3787 data_pending
= lttng_ustconsumer_data_pending
;
3790 ERR("Unknown consumer data type");
3794 /* Ease our life a bit */
3795 ht
= the_consumer_data
.stream_list_ht
;
3797 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3798 ht
->hash_fct(&id
, lttng_ht_seed
),
3800 &iter
.iter
, stream
, node_session_id
.node
) {
3801 pthread_mutex_lock(&stream
->lock
);
3804 * A removed node from the hash table indicates that the stream has
3805 * been deleted thus having a guarantee that the buffers are closed
3806 * on the consumer side. However, data can still be transmitted
3807 * over the network so don't skip the relayd check.
3809 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3811 /* Check the stream if there is data in the buffers. */
3812 ret
= data_pending(stream
);
3814 pthread_mutex_unlock(&stream
->lock
);
3819 pthread_mutex_unlock(&stream
->lock
);
3822 relayd
= find_relayd_by_session_id(id
);
3824 unsigned int is_data_inflight
= 0;
3826 /* Send init command for data pending. */
3827 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3828 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3829 relayd
->relayd_session_id
);
3831 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3832 /* Communication error thus the relayd so no data pending. */
3833 goto data_not_pending
;
3836 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3837 ht
->hash_fct(&id
, lttng_ht_seed
),
3839 &iter
.iter
, stream
, node_session_id
.node
) {
3840 if (stream
->metadata_flag
) {
3841 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3842 stream
->relayd_stream_id
);
3844 ret
= relayd_data_pending(&relayd
->control_sock
,
3845 stream
->relayd_stream_id
,
3846 stream
->next_net_seq_num
- 1);
3850 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3852 } else if (ret
< 0) {
3853 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3854 lttng_consumer_cleanup_relayd(relayd
);
3855 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3856 goto data_not_pending
;
3860 /* Send end command for data pending. */
3861 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3862 relayd
->relayd_session_id
, &is_data_inflight
);
3863 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3865 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3866 lttng_consumer_cleanup_relayd(relayd
);
3867 goto data_not_pending
;
3869 if (is_data_inflight
) {
3875 * Finding _no_ node in the hash table and no inflight data means that the
3876 * stream(s) have been removed thus data is guaranteed to be available for
3877 * analysis from the trace files.
3881 /* Data is available to be read by a viewer. */
3882 pthread_mutex_unlock(&the_consumer_data
.lock
);
3887 /* Data is still being extracted from buffers. */
3888 pthread_mutex_unlock(&the_consumer_data
.lock
);
3894 * Send a ret code status message to the sessiond daemon.
3896 * Return the sendmsg() return value.
3898 int consumer_send_status_msg(int sock
, int ret_code
)
3900 struct lttcomm_consumer_status_msg msg
;
3902 memset(&msg
, 0, sizeof(msg
));
3903 msg
.ret_code
= (lttcomm_return_code
) ret_code
;
3905 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3909 * Send a channel status message to the sessiond daemon.
3911 * Return the sendmsg() return value.
3913 int consumer_send_status_channel(int sock
,
3914 struct lttng_consumer_channel
*channel
)
3916 struct lttcomm_consumer_status_channel msg
;
3918 LTTNG_ASSERT(sock
>= 0);
3920 memset(&msg
, 0, sizeof(msg
));
3922 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3924 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3925 msg
.key
= channel
->key
;
3926 msg
.stream_count
= channel
->streams
.count
;
3929 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3932 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3933 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3934 uint64_t max_sb_size
)
3936 unsigned long start_pos
;
3938 if (!nb_packets_per_stream
) {
3939 return consumed_pos
; /* Grab everything */
3941 start_pos
= produced_pos
- lttng_offset_align_floor(produced_pos
, max_sb_size
);
3942 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3943 if ((long) (start_pos
- consumed_pos
) < 0) {
3944 return consumed_pos
; /* Grab everything */
3949 /* Stream lock must be held by the caller. */
3950 static int sample_stream_positions(struct lttng_consumer_stream
*stream
,
3951 unsigned long *produced
, unsigned long *consumed
)
3955 ASSERT_LOCKED(stream
->lock
);
3957 ret
= lttng_consumer_sample_snapshot_positions(stream
);
3959 ERR("Failed to sample snapshot positions");
3963 ret
= lttng_consumer_get_produced_snapshot(stream
, produced
);
3965 ERR("Failed to sample produced position");
3969 ret
= lttng_consumer_get_consumed_snapshot(stream
, consumed
);
3971 ERR("Failed to sample consumed position");
3980 * Sample the rotate position for all the streams of a channel. If a stream
3981 * is already at the rotate position (produced == consumed), we flag it as
3982 * ready for rotation. The rotation of ready streams occurs after we have
3983 * replied to the session daemon that we have finished sampling the positions.
3984 * Must be called with RCU read-side lock held to ensure existence of channel.
3986 * Returns 0 on success, < 0 on error
3988 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
3989 uint64_t key
, uint64_t relayd_id
, uint32_t metadata
,
3990 struct lttng_consumer_local_data
*ctx
)
3993 struct lttng_consumer_stream
*stream
;
3994 struct lttng_ht_iter iter
;
3995 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
3996 struct lttng_dynamic_array stream_rotation_positions
;
3997 uint64_t next_chunk_id
, stream_count
= 0;
3998 enum lttng_trace_chunk_status chunk_status
;
3999 const bool is_local_trace
= relayd_id
== -1ULL;
4000 struct consumer_relayd_sock_pair
*relayd
= NULL
;
4001 bool rotating_to_new_chunk
= true;
4002 /* Array of `struct lttng_consumer_stream *` */
4003 struct lttng_dynamic_pointer_array streams_packet_to_open
;
4006 ASSERT_RCU_READ_LOCKED();
4008 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
4010 lttng_dynamic_array_init(&stream_rotation_positions
,
4011 sizeof(struct relayd_stream_rotation_position
), NULL
);
4012 lttng_dynamic_pointer_array_init(&streams_packet_to_open
, NULL
);
4016 pthread_mutex_lock(&channel
->lock
);
4017 LTTNG_ASSERT(channel
->trace_chunk
);
4018 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
,
4020 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4022 goto end_unlock_channel
;
4025 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4026 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4027 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4028 stream
, node_channel_id
.node
) {
4029 unsigned long produced_pos
= 0, consumed_pos
= 0;
4031 health_code_update();
4034 * Lock stream because we are about to change its state.
4036 pthread_mutex_lock(&stream
->lock
);
4038 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
4039 rotating_to_new_chunk
= false;
4043 * Do not flush a packet when rotating from a NULL trace
4044 * chunk. The stream has no means to output data, and the prior
4045 * rotation which rotated to NULL performed that side-effect
4046 * already. No new data can be produced when a stream has no
4047 * associated trace chunk (e.g. a stop followed by a rotate).
4049 if (stream
->trace_chunk
) {
4052 if (stream
->metadata_flag
) {
4054 * Don't produce an empty metadata packet,
4055 * simply close the current one.
4057 * Metadata is regenerated on every trace chunk
4058 * switch; there is no concern that no data was
4061 flush_active
= true;
4064 * Only flush an empty packet if the "packet
4065 * open" could not be performed on transition
4066 * to a new trace chunk and no packets were
4067 * consumed within the chunk's lifetime.
4069 if (stream
->opened_packet_in_current_trace_chunk
) {
4070 flush_active
= true;
4073 * Stream could have been full at the
4074 * time of rotation, but then have had
4075 * no activity at all.
4077 * It is important to flush a packet
4078 * to prevent 0-length files from being
4079 * produced as most viewers choke on
4082 * Unfortunately viewers will not be
4083 * able to know that tracing was active
4084 * for this stream during this trace
4087 ret
= sample_stream_positions(stream
, &produced_pos
, &consumed_pos
);
4089 goto end_unlock_stream
;
4093 * Don't flush an empty packet if data
4094 * was produced; it will be consumed
4095 * before the rotation completes.
4097 flush_active
= produced_pos
!= consumed_pos
;
4098 if (!flush_active
) {
4099 const char *trace_chunk_name
;
4100 uint64_t trace_chunk_id
;
4102 chunk_status
= lttng_trace_chunk_get_name(
4103 stream
->trace_chunk
,
4106 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_NONE
) {
4107 trace_chunk_name
= "none";
4111 * Consumer trace chunks are
4114 chunk_status
= lttng_trace_chunk_get_id(
4115 stream
->trace_chunk
,
4117 LTTNG_ASSERT(chunk_status
==
4118 LTTNG_TRACE_CHUNK_STATUS_OK
);
4120 DBG("Unable to open packet for stream during trace chunk's lifetime. "
4121 "Flushing an empty packet to prevent an empty file from being created: "
4122 "stream id = %" PRIu64
", trace chunk name = `%s`, trace chunk id = %" PRIu64
,
4123 stream
->key
, trace_chunk_name
, trace_chunk_id
);
4129 * Close the current packet before sampling the
4130 * ring buffer positions.
4132 ret
= consumer_stream_flush_buffer(stream
, flush_active
);
4134 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4136 goto end_unlock_stream
;
4140 ret
= lttng_consumer_take_snapshot(stream
);
4141 if (ret
< 0 && ret
!= -ENODATA
&& ret
!= -EAGAIN
) {
4142 ERR("Failed to sample snapshot position during channel rotation");
4143 goto end_unlock_stream
;
4146 ret
= lttng_consumer_get_produced_snapshot(stream
,
4149 ERR("Failed to sample produced position during channel rotation");
4150 goto end_unlock_stream
;
4153 ret
= lttng_consumer_get_consumed_snapshot(stream
,
4156 ERR("Failed to sample consumed position during channel rotation");
4157 goto end_unlock_stream
;
4161 * Align produced position on the start-of-packet boundary of the first
4162 * packet going into the next trace chunk.
4164 produced_pos
= lttng_align_floor(produced_pos
, stream
->max_sb_size
);
4165 if (consumed_pos
== produced_pos
) {
4166 DBG("Set rotate ready for stream %" PRIu64
" produced = %lu consumed = %lu",
4167 stream
->key
, produced_pos
, consumed_pos
);
4168 stream
->rotate_ready
= true;
4170 DBG("Different consumed and produced positions "
4171 "for stream %" PRIu64
" produced = %lu consumed = %lu",
4172 stream
->key
, produced_pos
, consumed_pos
);
4175 * The rotation position is based on the packet_seq_num of the
4176 * packet following the last packet that was consumed for this
4177 * stream, incremented by the offset between produced and
4178 * consumed positions. This rotation position is a lower bound
4179 * (inclusive) at which the next trace chunk starts. Since it
4180 * is a lower bound, it is OK if the packet_seq_num does not
4181 * correspond exactly to the same packet identified by the
4182 * consumed_pos, which can happen in overwrite mode.
4184 if (stream
->sequence_number_unavailable
) {
4186 * Rotation should never be performed on a session which
4187 * interacts with a pre-2.8 lttng-modules, which does
4188 * not implement packet sequence number.
4190 ERR("Failure to rotate stream %" PRIu64
": sequence number unavailable",
4193 goto end_unlock_stream
;
4195 stream
->rotate_position
= stream
->last_sequence_number
+ 1 +
4196 ((produced_pos
- consumed_pos
) / stream
->max_sb_size
);
4197 DBG("Set rotation position for stream %" PRIu64
" at position %" PRIu64
,
4198 stream
->key
, stream
->rotate_position
);
4200 if (!is_local_trace
) {
4202 * The relay daemon control protocol expects a rotation
4203 * position as "the sequence number of the first packet
4204 * _after_ the current trace chunk".
4206 const struct relayd_stream_rotation_position position
= {
4207 .stream_id
= stream
->relayd_stream_id
,
4208 .rotate_at_seq_num
= stream
->rotate_position
,
4211 ret
= lttng_dynamic_array_add_element(
4212 &stream_rotation_positions
,
4215 ERR("Failed to allocate stream rotation position");
4216 goto end_unlock_stream
;
4221 stream
->opened_packet_in_current_trace_chunk
= false;
4223 if (rotating_to_new_chunk
&& !stream
->metadata_flag
) {
4225 * Attempt to flush an empty packet as close to the
4226 * rotation point as possible. In the event where a
4227 * stream remains inactive after the rotation point,
4228 * this ensures that the new trace chunk has a
4229 * beginning timestamp set at the begining of the
4230 * trace chunk instead of only creating an empty
4231 * packet when the trace chunk is stopped.
4233 * This indicates to the viewers that the stream
4234 * was being recorded, but more importantly it
4235 * allows viewers to determine a useable trace
4238 * This presents a problem in the case where the
4239 * ring-buffer is completely full.
4241 * Consider the following scenario:
4242 * - The consumption of data is slow (slow network,
4244 * - The ring buffer is full,
4245 * - A rotation is initiated,
4246 * - The flush below does nothing (no space left to
4247 * open a new packet),
4248 * - The other streams rotate very soon, and new
4249 * data is produced in the new chunk,
4250 * - This stream completes its rotation long after the
4251 * rotation was initiated
4252 * - The session is stopped before any event can be
4253 * produced in this stream's buffers.
4255 * The resulting trace chunk will have a single packet
4256 * temporaly at the end of the trace chunk for this
4257 * stream making the stream intersection more narrow
4258 * than it should be.
4260 * To work-around this, an empty flush is performed
4261 * after the first consumption of a packet during a
4262 * rotation if open_packet fails. The idea is that
4263 * consuming a packet frees enough space to switch
4264 * packets in this scenario and allows the tracer to
4265 * "stamp" the beginning of the new trace chunk at the
4266 * earliest possible point.
4268 * The packet open is performed after the channel
4269 * rotation to ensure that no attempt to open a packet
4270 * is performed in a stream that has no active trace
4273 ret
= lttng_dynamic_pointer_array_add_pointer(
4274 &streams_packet_to_open
, stream
);
4276 PERROR("Failed to add a stream pointer to array of streams in which to open a packet");
4278 goto end_unlock_stream
;
4282 pthread_mutex_unlock(&stream
->lock
);
4286 if (!is_local_trace
) {
4287 relayd
= consumer_find_relayd(relayd_id
);
4289 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4291 goto end_unlock_channel
;
4294 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4295 ret
= relayd_rotate_streams(&relayd
->control_sock
, stream_count
,
4296 rotating_to_new_chunk
? &next_chunk_id
: NULL
,
4297 (const struct relayd_stream_rotation_position
*)
4298 stream_rotation_positions
.buffer
4300 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4302 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4303 relayd
->net_seq_idx
);
4304 lttng_consumer_cleanup_relayd(relayd
);
4305 goto end_unlock_channel
;
4309 for (stream_idx
= 0;
4310 stream_idx
< lttng_dynamic_pointer_array_get_count(
4311 &streams_packet_to_open
);
4313 enum consumer_stream_open_packet_status status
;
4315 stream
= (lttng_consumer_stream
*) lttng_dynamic_pointer_array_get_pointer(
4316 &streams_packet_to_open
, stream_idx
);
4318 pthread_mutex_lock(&stream
->lock
);
4319 status
= consumer_stream_open_packet(stream
);
4320 pthread_mutex_unlock(&stream
->lock
);
4322 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
4323 DBG("Opened a packet after a rotation: stream id = %" PRIu64
4324 ", channel name = %s, session id = %" PRIu64
,
4325 stream
->key
, stream
->chan
->name
,
4326 stream
->chan
->session_id
);
4328 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
4330 * Can't open a packet as there is no space left
4331 * in the buffer. A new packet will be opened
4332 * once one has been consumed.
4334 DBG("No space left to open a packet after a rotation: stream id = %" PRIu64
4335 ", channel name = %s, session id = %" PRIu64
,
4336 stream
->key
, stream
->chan
->name
,
4337 stream
->chan
->session_id
);
4339 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
4340 /* Logged by callee. */
4342 goto end_unlock_channel
;
4348 pthread_mutex_unlock(&channel
->lock
);
4353 pthread_mutex_unlock(&stream
->lock
);
4355 pthread_mutex_unlock(&channel
->lock
);
4358 lttng_dynamic_array_reset(&stream_rotation_positions
);
4359 lttng_dynamic_pointer_array_reset(&streams_packet_to_open
);
4364 int consumer_clear_buffer(struct lttng_consumer_stream
*stream
)
4367 unsigned long consumed_pos_before
, consumed_pos_after
;
4369 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4371 ERR("Taking snapshot positions");
4375 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_before
);
4377 ERR("Consumed snapshot position");
4381 switch (the_consumer_data
.type
) {
4382 case LTTNG_CONSUMER_KERNEL
:
4383 ret
= kernctl_buffer_clear(stream
->wait_fd
);
4385 ERR("Failed to clear kernel stream (ret = %d)", ret
);
4389 case LTTNG_CONSUMER32_UST
:
4390 case LTTNG_CONSUMER64_UST
:
4391 ret
= lttng_ustconsumer_clear_buffer(stream
);
4393 ERR("Failed to clear ust stream (ret = %d)", ret
);
4398 ERR("Unknown consumer_data type");
4402 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4404 ERR("Taking snapshot positions");
4407 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_after
);
4409 ERR("Consumed snapshot position");
4412 DBG("clear: before: %lu after: %lu", consumed_pos_before
, consumed_pos_after
);
4418 int consumer_clear_stream(struct lttng_consumer_stream
*stream
)
4422 ret
= consumer_stream_flush_buffer(stream
, 1);
4424 ERR("Failed to flush stream %" PRIu64
" during channel clear",
4426 ret
= LTTCOMM_CONSUMERD_FATAL
;
4430 ret
= consumer_clear_buffer(stream
);
4432 ERR("Failed to clear stream %" PRIu64
" during channel clear",
4434 ret
= LTTCOMM_CONSUMERD_FATAL
;
4438 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4444 int consumer_clear_unmonitored_channel(struct lttng_consumer_channel
*channel
)
4447 struct lttng_consumer_stream
*stream
;
4450 pthread_mutex_lock(&channel
->lock
);
4451 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
4452 health_code_update();
4453 pthread_mutex_lock(&stream
->lock
);
4454 ret
= consumer_clear_stream(stream
);
4458 pthread_mutex_unlock(&stream
->lock
);
4460 pthread_mutex_unlock(&channel
->lock
);
4465 pthread_mutex_unlock(&stream
->lock
);
4466 pthread_mutex_unlock(&channel
->lock
);
4472 * Check if a stream is ready to be rotated after extracting it.
4474 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4475 * error. Stream lock must be held.
4477 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4479 DBG("Check is rotate ready for stream %" PRIu64
4480 " ready %u rotate_position %" PRIu64
4481 " last_sequence_number %" PRIu64
,
4482 stream
->key
, stream
->rotate_ready
,
4483 stream
->rotate_position
, stream
->last_sequence_number
);
4484 if (stream
->rotate_ready
) {
4489 * If packet seq num is unavailable, it means we are interacting
4490 * with a pre-2.8 lttng-modules which does not implement the
4491 * sequence number. Rotation should never be used by sessiond in this
4494 if (stream
->sequence_number_unavailable
) {
4495 ERR("Internal error: rotation used on stream %" PRIu64
4496 " with unavailable sequence number",
4501 if (stream
->rotate_position
== -1ULL ||
4502 stream
->last_sequence_number
== -1ULL) {
4507 * Rotate position not reached yet. The stream rotate position is
4508 * the position of the next packet belonging to the next trace chunk,
4509 * but consumerd considers rotation ready when reaching the last
4510 * packet of the current chunk, hence the "rotate_position - 1".
4513 DBG("Check is rotate ready for stream %" PRIu64
4514 " last_sequence_number %" PRIu64
4515 " rotate_position %" PRIu64
,
4516 stream
->key
, stream
->last_sequence_number
,
4517 stream
->rotate_position
);
4518 if (stream
->last_sequence_number
>= stream
->rotate_position
- 1) {
4526 * Reset the state for a stream after a rotation occurred.
4528 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4530 DBG("lttng_consumer_reset_stream_rotate_state for stream %" PRIu64
,
4532 stream
->rotate_position
= -1ULL;
4533 stream
->rotate_ready
= false;
4537 * Perform the rotation a local stream file.
4540 int rotate_local_stream(struct lttng_consumer_local_data
*ctx
,
4541 struct lttng_consumer_stream
*stream
)
4545 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4548 stream
->tracefile_size_current
= 0;
4549 stream
->tracefile_count_current
= 0;
4551 if (stream
->out_fd
>= 0) {
4552 ret
= close(stream
->out_fd
);
4554 PERROR("Failed to close stream out_fd of channel \"%s\"",
4555 stream
->chan
->name
);
4557 stream
->out_fd
= -1;
4560 if (stream
->index_file
) {
4561 lttng_index_file_put(stream
->index_file
);
4562 stream
->index_file
= NULL
;
4565 if (!stream
->trace_chunk
) {
4569 ret
= consumer_stream_create_output_files(stream
, true);
4575 * Performs the stream rotation for the rotate session feature if needed.
4576 * It must be called with the channel and stream locks held.
4578 * Return 0 on success, a negative number of error.
4580 int lttng_consumer_rotate_stream(struct lttng_consumer_local_data
*ctx
,
4581 struct lttng_consumer_stream
*stream
)
4585 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4588 * Update the stream's 'current' chunk to the session's (channel)
4589 * now-current chunk.
4591 lttng_trace_chunk_put(stream
->trace_chunk
);
4592 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4594 * A channel can be rotated and not have a "next" chunk
4595 * to transition to. In that case, the channel's "current chunk"
4596 * has not been closed yet, but it has not been updated to
4597 * a "next" trace chunk either. Hence, the stream, like its
4598 * parent channel, becomes part of no chunk and can't output
4599 * anything until a new trace chunk is created.
4601 stream
->trace_chunk
= NULL
;
4602 } else if (stream
->chan
->trace_chunk
&&
4603 !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4604 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4609 * Update the stream's trace chunk to its parent channel's
4610 * current trace chunk.
4612 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4615 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4616 ret
= rotate_local_stream(ctx
, stream
);
4618 ERR("Failed to rotate stream, ret = %i", ret
);
4623 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4625 * If the stream has transitioned to a new trace
4626 * chunk, the metadata should be re-dumped to the
4629 * However, it is possible for a stream to transition to
4630 * a "no-chunk" state. This can happen if a rotation
4631 * occurs on an inactive session. In such cases, the metadata
4632 * regeneration will happen when the next trace chunk is
4635 ret
= consumer_metadata_stream_dump(stream
);
4640 lttng_consumer_reset_stream_rotate_state(stream
);
4649 * Rotate all the ready streams now.
4651 * This is especially important for low throughput streams that have already
4652 * been consumed, we cannot wait for their next packet to perform the
4654 * Need to be called with RCU read-side lock held to ensure existence of
4657 * Returns 0 on success, < 0 on error
4659 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
,
4660 uint64_t key
, struct lttng_consumer_local_data
*ctx
)
4663 struct lttng_consumer_stream
*stream
;
4664 struct lttng_ht_iter iter
;
4665 struct lttng_ht
*ht
= the_consumer_data
.stream_per_chan_id_ht
;
4667 ASSERT_RCU_READ_LOCKED();
4671 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4673 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4674 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4675 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4676 stream
, node_channel_id
.node
) {
4677 health_code_update();
4679 pthread_mutex_lock(&stream
->chan
->lock
);
4680 pthread_mutex_lock(&stream
->lock
);
4682 if (!stream
->rotate_ready
) {
4683 pthread_mutex_unlock(&stream
->lock
);
4684 pthread_mutex_unlock(&stream
->chan
->lock
);
4687 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4689 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
4690 pthread_mutex_unlock(&stream
->lock
);
4691 pthread_mutex_unlock(&stream
->chan
->lock
);
4704 enum lttcomm_return_code
lttng_consumer_init_command(
4705 struct lttng_consumer_local_data
*ctx
,
4706 const lttng_uuid sessiond_uuid
)
4708 enum lttcomm_return_code ret
;
4709 char uuid_str
[LTTNG_UUID_STR_LEN
];
4711 if (ctx
->sessiond_uuid
.is_set
) {
4712 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4716 ctx
->sessiond_uuid
.is_set
= true;
4717 memcpy(ctx
->sessiond_uuid
.value
, sessiond_uuid
, sizeof(lttng_uuid
));
4718 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4719 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4720 DBG("Received session daemon UUID: %s", uuid_str
);
4725 enum lttcomm_return_code
lttng_consumer_create_trace_chunk(
4726 const uint64_t *relayd_id
, uint64_t session_id
,
4728 time_t chunk_creation_timestamp
,
4729 const char *chunk_override_name
,
4730 const struct lttng_credentials
*credentials
,
4731 struct lttng_directory_handle
*chunk_directory_handle
)
4734 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4735 struct lttng_trace_chunk
*created_chunk
= NULL
, *published_chunk
= NULL
;
4736 enum lttng_trace_chunk_status chunk_status
;
4737 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4738 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4739 const char *relayd_id_str
= "(none)";
4740 const char *creation_timestamp_str
;
4741 struct lttng_ht_iter iter
;
4742 struct lttng_consumer_channel
*channel
;
4745 /* Only used for logging purposes. */
4746 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4747 "%" PRIu64
, *relayd_id
);
4748 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4749 relayd_id_str
= relayd_id_buffer
;
4751 relayd_id_str
= "(formatting error)";
4755 /* Local protocol error. */
4756 LTTNG_ASSERT(chunk_creation_timestamp
);
4757 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4758 creation_timestamp_buffer
,
4759 sizeof(creation_timestamp_buffer
));
4760 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
4761 "(formatting error)";
4763 DBG("Consumer create trace chunk command: relay_id = %s"
4764 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4765 ", chunk_override_name = %s"
4766 ", chunk_creation_timestamp = %s",
4767 relayd_id_str
, session_id
, chunk_id
,
4768 chunk_override_name
? : "(none)",
4769 creation_timestamp_str
);
4772 * The trace chunk registry, as used by the consumer daemon, implicitly
4773 * owns the trace chunks. This is only needed in the consumer since
4774 * the consumer has no notion of a session beyond session IDs being
4775 * used to identify other objects.
4777 * The lttng_trace_chunk_registry_publish() call below provides a
4778 * reference which is not released; it implicitly becomes the session
4779 * daemon's reference to the chunk in the consumer daemon.
4781 * The lifetime of trace chunks in the consumer daemon is managed by
4782 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4783 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4785 created_chunk
= lttng_trace_chunk_create(chunk_id
,
4786 chunk_creation_timestamp
, NULL
);
4787 if (!created_chunk
) {
4788 ERR("Failed to create trace chunk");
4789 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4793 if (chunk_override_name
) {
4794 chunk_status
= lttng_trace_chunk_override_name(created_chunk
,
4795 chunk_override_name
);
4796 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4797 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4802 if (chunk_directory_handle
) {
4803 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
,
4805 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4806 ERR("Failed to set trace chunk credentials");
4807 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4811 * The consumer daemon has no ownership of the chunk output
4814 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
,
4815 chunk_directory_handle
);
4816 chunk_directory_handle
= NULL
;
4817 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4818 ERR("Failed to set trace chunk's directory handle");
4819 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4824 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4825 the_consumer_data
.chunk_registry
, session_id
,
4827 lttng_trace_chunk_put(created_chunk
);
4828 created_chunk
= NULL
;
4829 if (!published_chunk
) {
4830 ERR("Failed to publish trace chunk");
4831 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4836 cds_lfht_for_each_entry_duplicate(
4837 the_consumer_data
.channels_by_session_id_ht
->ht
,
4838 the_consumer_data
.channels_by_session_id_ht
->hash_fct(
4839 &session_id
, lttng_ht_seed
),
4840 the_consumer_data
.channels_by_session_id_ht
->match_fct
,
4841 &session_id
, &iter
.iter
, channel
,
4842 channels_by_session_id_ht_node
.node
) {
4843 ret
= lttng_consumer_channel_set_trace_chunk(channel
,
4847 * Roll-back the creation of this chunk.
4849 * This is important since the session daemon will
4850 * assume that the creation of this chunk failed and
4851 * will never ask for it to be closed, resulting
4852 * in a leak and an inconsistent state for some
4855 enum lttcomm_return_code close_ret
;
4856 char path
[LTTNG_PATH_MAX
];
4858 DBG("Failed to set new trace chunk on existing channels, rolling back");
4859 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4860 session_id
, chunk_id
,
4861 chunk_creation_timestamp
, NULL
,
4863 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4864 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4865 session_id
, chunk_id
);
4868 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4874 struct consumer_relayd_sock_pair
*relayd
;
4876 relayd
= consumer_find_relayd(*relayd_id
);
4878 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4879 ret
= relayd_create_trace_chunk(
4880 &relayd
->control_sock
, published_chunk
);
4881 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4883 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4886 if (!relayd
|| ret
) {
4887 enum lttcomm_return_code close_ret
;
4888 char path
[LTTNG_PATH_MAX
];
4890 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4893 chunk_creation_timestamp
,
4895 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4896 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4901 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4908 /* Release the reference returned by the "publish" operation. */
4909 lttng_trace_chunk_put(published_chunk
);
4910 lttng_trace_chunk_put(created_chunk
);
4914 enum lttcomm_return_code
lttng_consumer_close_trace_chunk(
4915 const uint64_t *relayd_id
, uint64_t session_id
,
4916 uint64_t chunk_id
, time_t chunk_close_timestamp
,
4917 const enum lttng_trace_chunk_command_type
*close_command
,
4920 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4921 struct lttng_trace_chunk
*chunk
;
4922 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4923 const char *relayd_id_str
= "(none)";
4924 const char *close_command_name
= "none";
4925 struct lttng_ht_iter iter
;
4926 struct lttng_consumer_channel
*channel
;
4927 enum lttng_trace_chunk_status chunk_status
;
4932 /* Only used for logging purposes. */
4933 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4934 "%" PRIu64
, *relayd_id
);
4935 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4936 relayd_id_str
= relayd_id_buffer
;
4938 relayd_id_str
= "(formatting error)";
4941 if (close_command
) {
4942 close_command_name
= lttng_trace_chunk_command_type_get_name(
4946 DBG("Consumer close trace chunk command: relayd_id = %s"
4947 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4948 ", close command = %s",
4949 relayd_id_str
, session_id
, chunk_id
,
4950 close_command_name
);
4952 chunk
= lttng_trace_chunk_registry_find_chunk(
4953 the_consumer_data
.chunk_registry
, session_id
, chunk_id
);
4955 ERR("Failed to find chunk: session_id = %" PRIu64
4956 ", chunk_id = %" PRIu64
,
4957 session_id
, chunk_id
);
4958 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4962 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
,
4963 chunk_close_timestamp
);
4964 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4965 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4969 if (close_command
) {
4970 chunk_status
= lttng_trace_chunk_set_close_command(
4971 chunk
, *close_command
);
4972 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4973 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4979 * chunk is now invalid to access as we no longer hold a reference to
4980 * it; it is only kept around to compare it (by address) to the
4981 * current chunk found in the session's channels.
4984 cds_lfht_for_each_entry(the_consumer_data
.channel_ht
->ht
, &iter
.iter
,
4985 channel
, node
.node
) {
4989 * Only change the channel's chunk to NULL if it still
4990 * references the chunk being closed. The channel may
4991 * reference a newer channel in the case of a session
4992 * rotation. When a session rotation occurs, the "next"
4993 * chunk is created before the "current" chunk is closed.
4995 if (channel
->trace_chunk
!= chunk
) {
4998 ret
= lttng_consumer_channel_set_trace_chunk(channel
, NULL
);
5001 * Attempt to close the chunk on as many channels as
5004 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5010 struct consumer_relayd_sock_pair
*relayd
;
5012 relayd
= consumer_find_relayd(*relayd_id
);
5014 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5015 ret
= relayd_close_trace_chunk(
5016 &relayd
->control_sock
, chunk
,
5018 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5020 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
,
5024 if (!relayd
|| ret
) {
5025 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
5033 * Release the reference returned by the "find" operation and
5034 * the session daemon's implicit reference to the chunk.
5036 lttng_trace_chunk_put(chunk
);
5037 lttng_trace_chunk_put(chunk
);
5042 enum lttcomm_return_code
lttng_consumer_trace_chunk_exists(
5043 const uint64_t *relayd_id
, uint64_t session_id
,
5047 enum lttcomm_return_code ret_code
;
5048 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
5049 const char *relayd_id_str
= "(none)";
5050 const bool is_local_trace
= !relayd_id
;
5051 struct consumer_relayd_sock_pair
*relayd
= NULL
;
5052 bool chunk_exists_local
, chunk_exists_remote
;
5055 /* Only used for logging purposes. */
5056 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
5057 "%" PRIu64
, *relayd_id
);
5058 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
5059 relayd_id_str
= relayd_id_buffer
;
5061 relayd_id_str
= "(formatting error)";
5065 DBG("Consumer trace chunk exists command: relayd_id = %s"
5066 ", chunk_id = %" PRIu64
, relayd_id_str
,
5068 ret
= lttng_trace_chunk_registry_chunk_exists(
5069 the_consumer_data
.chunk_registry
, session_id
, chunk_id
,
5070 &chunk_exists_local
);
5072 /* Internal error. */
5073 ERR("Failed to query the existence of a trace chunk");
5074 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
5077 DBG("Trace chunk %s locally",
5078 chunk_exists_local
? "exists" : "does not exist");
5079 if (chunk_exists_local
) {
5080 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
5082 } else if (is_local_trace
) {
5083 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5088 relayd
= consumer_find_relayd(*relayd_id
);
5090 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
5091 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5092 goto end_rcu_unlock
;
5094 DBG("Looking up existence of trace chunk on relay daemon");
5095 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
5096 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
,
5097 &chunk_exists_remote
);
5098 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
5100 ERR("Failed to look-up the existence of trace chunk on relay daemon");
5101 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
5102 goto end_rcu_unlock
;
5105 ret_code
= chunk_exists_remote
?
5106 LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
5107 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
5108 DBG("Trace chunk %s on relay daemon",
5109 chunk_exists_remote
? "exists" : "does not exist");
5118 int consumer_clear_monitored_channel(struct lttng_consumer_channel
*channel
)
5120 struct lttng_ht
*ht
;
5121 struct lttng_consumer_stream
*stream
;
5122 struct lttng_ht_iter iter
;
5125 ht
= the_consumer_data
.stream_per_chan_id_ht
;
5128 cds_lfht_for_each_entry_duplicate(ht
->ht
,
5129 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
5130 ht
->match_fct
, &channel
->key
,
5131 &iter
.iter
, stream
, node_channel_id
.node
) {
5133 * Protect against teardown with mutex.
5135 pthread_mutex_lock(&stream
->lock
);
5136 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5139 ret
= consumer_clear_stream(stream
);
5144 pthread_mutex_unlock(&stream
->lock
);
5147 return LTTCOMM_CONSUMERD_SUCCESS
;
5150 pthread_mutex_unlock(&stream
->lock
);
5155 int lttng_consumer_clear_channel(struct lttng_consumer_channel
*channel
)
5159 DBG("Consumer clear channel %" PRIu64
, channel
->key
);
5161 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
5163 * Nothing to do for the metadata channel/stream.
5164 * Snapshot mechanism already take care of the metadata
5165 * handling/generation, and monitored channels only need to
5166 * have their data stream cleared..
5168 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5172 if (!channel
->monitor
) {
5173 ret
= consumer_clear_unmonitored_channel(channel
);
5175 ret
= consumer_clear_monitored_channel(channel
);
5181 enum lttcomm_return_code
lttng_consumer_open_channel_packets(
5182 struct lttng_consumer_channel
*channel
)
5184 struct lttng_consumer_stream
*stream
;
5185 enum lttcomm_return_code ret
= LTTCOMM_CONSUMERD_SUCCESS
;
5187 if (channel
->metadata_stream
) {
5188 ERR("Open channel packets command attempted on a metadata channel");
5189 ret
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
5194 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
5195 enum consumer_stream_open_packet_status status
;
5197 pthread_mutex_lock(&stream
->lock
);
5198 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
5202 status
= consumer_stream_open_packet(stream
);
5204 case CONSUMER_STREAM_OPEN_PACKET_STATUS_OPENED
:
5205 DBG("Opened a packet in \"open channel packets\" command: stream id = %" PRIu64
5206 ", channel name = %s, session id = %" PRIu64
,
5207 stream
->key
, stream
->chan
->name
,
5208 stream
->chan
->session_id
);
5209 stream
->opened_packet_in_current_trace_chunk
= true;
5211 case CONSUMER_STREAM_OPEN_PACKET_STATUS_NO_SPACE
:
5212 DBG("No space left to open a packet in \"open channel packets\" command: stream id = %" PRIu64
5213 ", channel name = %s, session id = %" PRIu64
,
5214 stream
->key
, stream
->chan
->name
,
5215 stream
->chan
->session_id
);
5217 case CONSUMER_STREAM_OPEN_PACKET_STATUS_ERROR
:
5219 * Only unexpected internal errors can lead to this
5220 * failing. Report an unknown error.
5222 ERR("Failed to flush empty buffer in \"open channel packets\" command: stream id = %" PRIu64
5223 ", channel id = %" PRIu64
5224 ", channel name = %s"
5225 ", session id = %" PRIu64
,
5226 stream
->key
, channel
->key
,
5227 channel
->name
, channel
->session_id
);
5228 ret
= LTTCOMM_CONSUMERD_UNKNOWN_ERROR
;
5235 pthread_mutex_unlock(&stream
->lock
);
5244 pthread_mutex_unlock(&stream
->lock
);
5245 goto end_rcu_unlock
;
5248 void lttng_consumer_sigbus_handle(void *addr
)
5250 lttng_ustconsumer_sigbus_handle(addr
);