2 * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
17 #include <sys/socket.h>
18 #include <sys/types.h>
23 #include <bin/lttng-consumerd/health-consumerd.h>
24 #include <common/common.h>
25 #include <common/utils.h>
26 #include <common/time.h>
27 #include <common/compat/poll.h>
28 #include <common/compat/endian.h>
29 #include <common/index/index.h>
30 #include <common/kernel-ctl/kernel-ctl.h>
31 #include <common/sessiond-comm/relayd.h>
32 #include <common/sessiond-comm/sessiond-comm.h>
33 #include <common/kernel-consumer/kernel-consumer.h>
34 #include <common/relayd/relayd.h>
35 #include <common/ust-consumer/ust-consumer.h>
36 #include <common/consumer/consumer-timer.h>
37 #include <common/consumer/consumer.h>
38 #include <common/consumer/consumer-stream.h>
39 #include <common/consumer/consumer-testpoint.h>
40 #include <common/align.h>
41 #include <common/consumer/consumer-metadata-cache.h>
42 #include <common/trace-chunk.h>
43 #include <common/trace-chunk-registry.h>
44 #include <common/string-utils/format.h>
45 #include <common/dynamic-array.h>
47 struct lttng_consumer_global_data consumer_data
= {
50 .type
= LTTNG_CONSUMER_UNKNOWN
,
53 enum consumer_channel_action
{
56 CONSUMER_CHANNEL_QUIT
,
59 struct consumer_channel_msg
{
60 enum consumer_channel_action action
;
61 struct lttng_consumer_channel
*chan
; /* add */
62 uint64_t key
; /* del */
65 /* Flag used to temporarily pause data consumption from testpoints. */
66 int data_consumption_paused
;
69 * Flag to inform the polling thread to quit when all fd hung up. Updated by
70 * the consumer_thread_receive_fds when it notices that all fds has hung up.
71 * Also updated by the signal handler (consumer_should_exit()). Read by the
77 * Global hash table containing respectively metadata and data streams. The
78 * stream element in this ht should only be updated by the metadata poll thread
79 * for the metadata and the data poll thread for the data.
81 static struct lttng_ht
*metadata_ht
;
82 static struct lttng_ht
*data_ht
;
84 static const char *get_consumer_domain(void)
86 switch (consumer_data
.type
) {
87 case LTTNG_CONSUMER_KERNEL
:
88 return DEFAULT_KERNEL_TRACE_DIR
;
89 case LTTNG_CONSUMER64_UST
:
91 case LTTNG_CONSUMER32_UST
:
92 return DEFAULT_UST_TRACE_DIR
;
99 * Notify a thread lttng pipe to poll back again. This usually means that some
100 * global state has changed so we just send back the thread in a poll wait
103 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
105 struct lttng_consumer_stream
*null_stream
= NULL
;
109 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
112 static void notify_health_quit_pipe(int *pipe
)
116 ret
= lttng_write(pipe
[1], "4", 1);
118 PERROR("write consumer health quit");
122 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
123 struct lttng_consumer_channel
*chan
,
125 enum consumer_channel_action action
)
127 struct consumer_channel_msg msg
;
130 memset(&msg
, 0, sizeof(msg
));
135 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
136 if (ret
< sizeof(msg
)) {
137 PERROR("notify_channel_pipe write error");
141 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
144 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
147 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
148 struct lttng_consumer_channel
**chan
,
150 enum consumer_channel_action
*action
)
152 struct consumer_channel_msg msg
;
155 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
156 if (ret
< sizeof(msg
)) {
160 *action
= msg
.action
;
168 * Cleanup the stream list of a channel. Those streams are not yet globally
171 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
173 struct lttng_consumer_stream
*stream
, *stmp
;
177 /* Delete streams that might have been left in the stream list. */
178 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
180 cds_list_del(&stream
->send_node
);
182 * Once a stream is added to this list, the buffers were created so we
183 * have a guarantee that this call will succeed. Setting the monitor
184 * mode to 0 so we don't lock nor try to delete the stream from the
188 consumer_stream_destroy(stream
, NULL
);
193 * Find a stream. The consumer_data.lock must be locked during this
196 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
199 struct lttng_ht_iter iter
;
200 struct lttng_ht_node_u64
*node
;
201 struct lttng_consumer_stream
*stream
= NULL
;
205 /* -1ULL keys are lookup failures */
206 if (key
== (uint64_t) -1ULL) {
212 lttng_ht_lookup(ht
, &key
, &iter
);
213 node
= lttng_ht_iter_get_node_u64(&iter
);
215 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
223 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
225 struct lttng_consumer_stream
*stream
;
228 stream
= find_stream(key
, ht
);
230 stream
->key
= (uint64_t) -1ULL;
232 * We don't want the lookup to match, but we still need
233 * to iterate on this stream when iterating over the hash table. Just
234 * change the node key.
236 stream
->node
.key
= (uint64_t) -1ULL;
242 * Return a channel object for the given key.
244 * RCU read side lock MUST be acquired before calling this function and
245 * protects the channel ptr.
247 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
249 struct lttng_ht_iter iter
;
250 struct lttng_ht_node_u64
*node
;
251 struct lttng_consumer_channel
*channel
= NULL
;
253 /* -1ULL keys are lookup failures */
254 if (key
== (uint64_t) -1ULL) {
258 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
259 node
= lttng_ht_iter_get_node_u64(&iter
);
261 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
268 * There is a possibility that the consumer does not have enough time between
269 * the close of the channel on the session daemon and the cleanup in here thus
270 * once we have a channel add with an existing key, we know for sure that this
271 * channel will eventually get cleaned up by all streams being closed.
273 * This function just nullifies the already existing channel key.
275 static void steal_channel_key(uint64_t key
)
277 struct lttng_consumer_channel
*channel
;
280 channel
= consumer_find_channel(key
);
282 channel
->key
= (uint64_t) -1ULL;
284 * We don't want the lookup to match, but we still need to iterate on
285 * this channel when iterating over the hash table. Just change the
288 channel
->node
.key
= (uint64_t) -1ULL;
293 static void free_channel_rcu(struct rcu_head
*head
)
295 struct lttng_ht_node_u64
*node
=
296 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
297 struct lttng_consumer_channel
*channel
=
298 caa_container_of(node
, struct lttng_consumer_channel
, node
);
300 switch (consumer_data
.type
) {
301 case LTTNG_CONSUMER_KERNEL
:
303 case LTTNG_CONSUMER32_UST
:
304 case LTTNG_CONSUMER64_UST
:
305 lttng_ustconsumer_free_channel(channel
);
308 ERR("Unknown consumer_data type");
315 * RCU protected relayd socket pair free.
317 static void free_relayd_rcu(struct rcu_head
*head
)
319 struct lttng_ht_node_u64
*node
=
320 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
321 struct consumer_relayd_sock_pair
*relayd
=
322 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
325 * Close all sockets. This is done in the call RCU since we don't want the
326 * socket fds to be reassigned thus potentially creating bad state of the
329 * We do not have to lock the control socket mutex here since at this stage
330 * there is no one referencing to this relayd object.
332 (void) relayd_close(&relayd
->control_sock
);
333 (void) relayd_close(&relayd
->data_sock
);
335 pthread_mutex_destroy(&relayd
->ctrl_sock_mutex
);
340 * Destroy and free relayd socket pair object.
342 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
345 struct lttng_ht_iter iter
;
347 if (relayd
== NULL
) {
351 DBG("Consumer destroy and close relayd socket pair");
353 iter
.iter
.node
= &relayd
->node
.node
;
354 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
356 /* We assume the relayd is being or is destroyed */
360 /* RCU free() call */
361 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
365 * Remove a channel from the global list protected by a mutex. This function is
366 * also responsible for freeing its data structures.
368 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
370 struct lttng_ht_iter iter
;
372 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
374 pthread_mutex_lock(&consumer_data
.lock
);
375 pthread_mutex_lock(&channel
->lock
);
377 /* Destroy streams that might have been left in the stream list. */
378 clean_channel_stream_list(channel
);
380 if (channel
->live_timer_enabled
== 1) {
381 consumer_timer_live_stop(channel
);
383 if (channel
->monitor_timer_enabled
== 1) {
384 consumer_timer_monitor_stop(channel
);
387 switch (consumer_data
.type
) {
388 case LTTNG_CONSUMER_KERNEL
:
390 case LTTNG_CONSUMER32_UST
:
391 case LTTNG_CONSUMER64_UST
:
392 lttng_ustconsumer_del_channel(channel
);
395 ERR("Unknown consumer_data type");
400 lttng_trace_chunk_put(channel
->trace_chunk
);
401 channel
->trace_chunk
= NULL
;
403 if (channel
->is_published
) {
407 iter
.iter
.node
= &channel
->node
.node
;
408 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
411 iter
.iter
.node
= &channel
->channels_by_session_id_ht_node
.node
;
412 ret
= lttng_ht_del(consumer_data
.channels_by_session_id_ht
,
418 channel
->is_deleted
= true;
419 call_rcu(&channel
->node
.head
, free_channel_rcu
);
421 pthread_mutex_unlock(&channel
->lock
);
422 pthread_mutex_unlock(&consumer_data
.lock
);
426 * Iterate over the relayd hash table and destroy each element. Finally,
427 * destroy the whole hash table.
429 static void cleanup_relayd_ht(void)
431 struct lttng_ht_iter iter
;
432 struct consumer_relayd_sock_pair
*relayd
;
436 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
438 consumer_destroy_relayd(relayd
);
443 lttng_ht_destroy(consumer_data
.relayd_ht
);
447 * Update the end point status of all streams having the given network sequence
448 * index (relayd index).
450 * It's atomically set without having the stream mutex locked which is fine
451 * because we handle the write/read race with a pipe wakeup for each thread.
453 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
454 enum consumer_endpoint_status status
)
456 struct lttng_ht_iter iter
;
457 struct lttng_consumer_stream
*stream
;
459 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
463 /* Let's begin with metadata */
464 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
465 if (stream
->net_seq_idx
== net_seq_idx
) {
466 uatomic_set(&stream
->endpoint_status
, status
);
467 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
471 /* Follow up by the data streams */
472 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
473 if (stream
->net_seq_idx
== net_seq_idx
) {
474 uatomic_set(&stream
->endpoint_status
, status
);
475 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
482 * Cleanup a relayd object by flagging every associated streams for deletion,
483 * destroying the object meaning removing it from the relayd hash table,
484 * closing the sockets and freeing the memory in a RCU call.
486 * If a local data context is available, notify the threads that the streams'
487 * state have changed.
489 void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
)
495 DBG("Cleaning up relayd object ID %"PRIu64
, relayd
->net_seq_idx
);
497 /* Save the net sequence index before destroying the object */
498 netidx
= relayd
->net_seq_idx
;
501 * Delete the relayd from the relayd hash table, close the sockets and free
502 * the object in a RCU call.
504 consumer_destroy_relayd(relayd
);
506 /* Set inactive endpoint to all streams */
507 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
510 * With a local data context, notify the threads that the streams' state
511 * have changed. The write() action on the pipe acts as an "implicit"
512 * memory barrier ordering the updates of the end point status from the
513 * read of this status which happens AFTER receiving this notify.
515 notify_thread_lttng_pipe(relayd
->ctx
->consumer_data_pipe
);
516 notify_thread_lttng_pipe(relayd
->ctx
->consumer_metadata_pipe
);
520 * Flag a relayd socket pair for destruction. Destroy it if the refcount
523 * RCU read side lock MUST be aquired before calling this function.
525 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
529 /* Set destroy flag for this object */
530 uatomic_set(&relayd
->destroy_flag
, 1);
532 /* Destroy the relayd if refcount is 0 */
533 if (uatomic_read(&relayd
->refcount
) == 0) {
534 consumer_destroy_relayd(relayd
);
539 * Completly destroy stream from every visiable data structure and the given
542 * One this call returns, the stream object is not longer usable nor visible.
544 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
547 consumer_stream_destroy(stream
, ht
);
551 * XXX naming of del vs destroy is all mixed up.
553 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
555 consumer_stream_destroy(stream
, data_ht
);
558 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
560 consumer_stream_destroy(stream
, metadata_ht
);
563 void consumer_stream_update_channel_attributes(
564 struct lttng_consumer_stream
*stream
,
565 struct lttng_consumer_channel
*channel
)
567 stream
->channel_read_only_attributes
.tracefile_size
=
568 channel
->tracefile_size
;
571 struct lttng_consumer_stream
*consumer_allocate_stream(
572 struct lttng_consumer_channel
*channel
,
573 uint64_t channel_key
,
575 const char *channel_name
,
578 struct lttng_trace_chunk
*trace_chunk
,
581 enum consumer_channel_type type
,
582 unsigned int monitor
)
585 struct lttng_consumer_stream
*stream
;
587 stream
= zmalloc(sizeof(*stream
));
588 if (stream
== NULL
) {
589 PERROR("malloc struct lttng_consumer_stream");
594 if (trace_chunk
&& !lttng_trace_chunk_get(trace_chunk
)) {
595 ERR("Failed to acquire trace chunk reference during the creation of a stream");
601 stream
->chan
= channel
;
602 stream
->key
= stream_key
;
603 stream
->trace_chunk
= trace_chunk
;
605 stream
->out_fd_offset
= 0;
606 stream
->output_written
= 0;
607 stream
->net_seq_idx
= relayd_id
;
608 stream
->session_id
= session_id
;
609 stream
->monitor
= monitor
;
610 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
611 stream
->index_file
= NULL
;
612 stream
->last_sequence_number
= -1ULL;
613 stream
->rotate_position
= -1ULL;
614 pthread_mutex_init(&stream
->lock
, NULL
);
615 pthread_mutex_init(&stream
->metadata_timer_lock
, NULL
);
617 /* If channel is the metadata, flag this stream as metadata. */
618 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
619 stream
->metadata_flag
= 1;
620 /* Metadata is flat out. */
621 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
622 /* Live rendez-vous point. */
623 pthread_cond_init(&stream
->metadata_rdv
, NULL
);
624 pthread_mutex_init(&stream
->metadata_rdv_lock
, NULL
);
626 /* Format stream name to <channel_name>_<cpu_number> */
627 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
630 PERROR("snprintf stream name");
635 /* Key is always the wait_fd for streams. */
636 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
638 /* Init node per channel id key */
639 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
641 /* Init session id node with the stream session id */
642 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
644 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
645 " relayd_id %" PRIu64
", session_id %" PRIu64
,
646 stream
->name
, stream
->key
, channel_key
,
647 stream
->net_seq_idx
, stream
->session_id
);
654 lttng_trace_chunk_put(stream
->trace_chunk
);
664 * Add a stream to the global list protected by a mutex.
666 void consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
668 struct lttng_ht
*ht
= data_ht
;
673 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
675 pthread_mutex_lock(&consumer_data
.lock
);
676 pthread_mutex_lock(&stream
->chan
->lock
);
677 pthread_mutex_lock(&stream
->chan
->timer_lock
);
678 pthread_mutex_lock(&stream
->lock
);
681 /* Steal stream identifier to avoid having streams with the same key */
682 steal_stream_key(stream
->key
, ht
);
684 lttng_ht_add_unique_u64(ht
, &stream
->node
);
686 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
687 &stream
->node_channel_id
);
690 * Add stream to the stream_list_ht of the consumer data. No need to steal
691 * the key since the HT does not use it and we allow to add redundant keys
694 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
697 * When nb_init_stream_left reaches 0, we don't need to trigger any action
698 * in terms of destroying the associated channel, because the action that
699 * causes the count to become 0 also causes a stream to be added. The
700 * channel deletion will thus be triggered by the following removal of this
703 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
704 /* Increment refcount before decrementing nb_init_stream_left */
706 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
709 /* Update consumer data once the node is inserted. */
710 consumer_data
.stream_count
++;
711 consumer_data
.need_update
= 1;
714 pthread_mutex_unlock(&stream
->lock
);
715 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
716 pthread_mutex_unlock(&stream
->chan
->lock
);
717 pthread_mutex_unlock(&consumer_data
.lock
);
721 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
722 * be acquired before calling this.
724 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
727 struct lttng_ht_node_u64
*node
;
728 struct lttng_ht_iter iter
;
732 lttng_ht_lookup(consumer_data
.relayd_ht
,
733 &relayd
->net_seq_idx
, &iter
);
734 node
= lttng_ht_iter_get_node_u64(&iter
);
738 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
745 * Allocate and return a consumer relayd socket.
747 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
748 uint64_t net_seq_idx
)
750 struct consumer_relayd_sock_pair
*obj
= NULL
;
752 /* net sequence index of -1 is a failure */
753 if (net_seq_idx
== (uint64_t) -1ULL) {
757 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
759 PERROR("zmalloc relayd sock");
763 obj
->net_seq_idx
= net_seq_idx
;
765 obj
->destroy_flag
= 0;
766 obj
->control_sock
.sock
.fd
= -1;
767 obj
->data_sock
.sock
.fd
= -1;
768 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
769 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
776 * Find a relayd socket pair in the global consumer data.
778 * Return the object if found else NULL.
779 * RCU read-side lock must be held across this call and while using the
782 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
784 struct lttng_ht_iter iter
;
785 struct lttng_ht_node_u64
*node
;
786 struct consumer_relayd_sock_pair
*relayd
= NULL
;
788 /* Negative keys are lookup failures */
789 if (key
== (uint64_t) -1ULL) {
793 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
795 node
= lttng_ht_iter_get_node_u64(&iter
);
797 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
805 * Find a relayd and send the stream
807 * Returns 0 on success, < 0 on error
809 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
813 struct consumer_relayd_sock_pair
*relayd
;
816 assert(stream
->net_seq_idx
!= -1ULL);
819 /* The stream is not metadata. Get relayd reference if exists. */
821 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
822 if (relayd
!= NULL
) {
823 /* Add stream on the relayd */
824 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
825 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
826 get_consumer_domain(), path
, &stream
->relayd_stream_id
,
827 stream
->chan
->tracefile_size
,
828 stream
->chan
->tracefile_count
,
829 stream
->trace_chunk
);
830 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
832 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
833 lttng_consumer_cleanup_relayd(relayd
);
837 uatomic_inc(&relayd
->refcount
);
838 stream
->sent_to_relayd
= 1;
840 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
841 stream
->key
, stream
->net_seq_idx
);
846 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
847 stream
->name
, stream
->key
, stream
->net_seq_idx
);
855 * Find a relayd and send the streams sent message
857 * Returns 0 on success, < 0 on error
859 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
862 struct consumer_relayd_sock_pair
*relayd
;
864 assert(net_seq_idx
!= -1ULL);
866 /* The stream is not metadata. Get relayd reference if exists. */
868 relayd
= consumer_find_relayd(net_seq_idx
);
869 if (relayd
!= NULL
) {
870 /* Add stream on the relayd */
871 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
872 ret
= relayd_streams_sent(&relayd
->control_sock
);
873 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
875 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
876 lttng_consumer_cleanup_relayd(relayd
);
880 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
887 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
895 * Find a relayd and close the stream
897 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
899 struct consumer_relayd_sock_pair
*relayd
;
901 /* The stream is not metadata. Get relayd reference if exists. */
903 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
905 consumer_stream_relayd_close(stream
, relayd
);
911 * Handle stream for relayd transmission if the stream applies for network
912 * streaming where the net sequence index is set.
914 * Return destination file descriptor or negative value on error.
916 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
917 size_t data_size
, unsigned long padding
,
918 struct consumer_relayd_sock_pair
*relayd
)
921 struct lttcomm_relayd_data_hdr data_hdr
;
927 /* Reset data header */
928 memset(&data_hdr
, 0, sizeof(data_hdr
));
930 if (stream
->metadata_flag
) {
931 /* Caller MUST acquire the relayd control socket lock */
932 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
937 /* Metadata are always sent on the control socket. */
938 outfd
= relayd
->control_sock
.sock
.fd
;
940 /* Set header with stream information */
941 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
942 data_hdr
.data_size
= htobe32(data_size
);
943 data_hdr
.padding_size
= htobe32(padding
);
946 * Note that net_seq_num below is assigned with the *current* value of
947 * next_net_seq_num and only after that the next_net_seq_num will be
948 * increment. This is why when issuing a command on the relayd using
949 * this next value, 1 should always be substracted in order to compare
950 * the last seen sequence number on the relayd side to the last sent.
952 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
953 /* Other fields are zeroed previously */
955 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
961 ++stream
->next_net_seq_num
;
963 /* Set to go on data socket */
964 outfd
= relayd
->data_sock
.sock
.fd
;
972 * Trigger a dump of the metadata content. Following/during the succesful
973 * completion of this call, the metadata poll thread will start receiving
974 * metadata packets to consume.
976 * The caller must hold the channel and stream locks.
979 int consumer_metadata_stream_dump(struct lttng_consumer_stream
*stream
)
983 ASSERT_LOCKED(stream
->chan
->lock
);
984 ASSERT_LOCKED(stream
->lock
);
985 assert(stream
->metadata_flag
);
986 assert(stream
->chan
->trace_chunk
);
988 switch (consumer_data
.type
) {
989 case LTTNG_CONSUMER_KERNEL
:
991 * Reset the position of what has been read from the
992 * metadata cache to 0 so we can dump it again.
994 ret
= kernctl_metadata_cache_dump(stream
->wait_fd
);
996 case LTTNG_CONSUMER32_UST
:
997 case LTTNG_CONSUMER64_UST
:
999 * Reset the position pushed from the metadata cache so it
1000 * will write from the beginning on the next push.
1002 stream
->ust_metadata_pushed
= 0;
1003 ret
= consumer_metadata_wakeup_pipe(stream
->chan
);
1006 ERR("Unknown consumer_data type");
1010 ERR("Failed to dump the metadata cache");
1016 int lttng_consumer_channel_set_trace_chunk(
1017 struct lttng_consumer_channel
*channel
,
1018 struct lttng_trace_chunk
*new_trace_chunk
)
1020 pthread_mutex_lock(&channel
->lock
);
1021 if (channel
->is_deleted
) {
1023 * The channel has been logically deleted and should no longer
1024 * be used. It has released its reference to its current trace
1025 * chunk and should not acquire a new one.
1027 * Return success as there is nothing for the caller to do.
1033 * The acquisition of the reference cannot fail (barring
1034 * a severe internal error) since a reference to the published
1035 * chunk is already held by the caller.
1037 if (new_trace_chunk
) {
1038 const bool acquired_reference
= lttng_trace_chunk_get(
1041 assert(acquired_reference
);
1044 lttng_trace_chunk_put(channel
->trace_chunk
);
1045 channel
->trace_chunk
= new_trace_chunk
;
1047 pthread_mutex_unlock(&channel
->lock
);
1052 * Allocate and return a new lttng_consumer_channel object using the given key
1053 * to initialize the hash table node.
1055 * On error, return NULL.
1057 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
1058 uint64_t session_id
,
1059 const uint64_t *chunk_id
,
1060 const char *pathname
,
1063 enum lttng_event_output output
,
1064 uint64_t tracefile_size
,
1065 uint64_t tracefile_count
,
1066 uint64_t session_id_per_pid
,
1067 unsigned int monitor
,
1068 unsigned int live_timer_interval
,
1069 bool is_in_live_session
,
1070 const char *root_shm_path
,
1071 const char *shm_path
)
1073 struct lttng_consumer_channel
*channel
= NULL
;
1074 struct lttng_trace_chunk
*trace_chunk
= NULL
;
1077 trace_chunk
= lttng_trace_chunk_registry_find_chunk(
1078 consumer_data
.chunk_registry
, session_id
,
1081 ERR("Failed to find trace chunk reference during creation of channel");
1086 channel
= zmalloc(sizeof(*channel
));
1087 if (channel
== NULL
) {
1088 PERROR("malloc struct lttng_consumer_channel");
1093 channel
->refcount
= 0;
1094 channel
->session_id
= session_id
;
1095 channel
->session_id_per_pid
= session_id_per_pid
;
1096 channel
->relayd_id
= relayd_id
;
1097 channel
->tracefile_size
= tracefile_size
;
1098 channel
->tracefile_count
= tracefile_count
;
1099 channel
->monitor
= monitor
;
1100 channel
->live_timer_interval
= live_timer_interval
;
1101 channel
->is_live
= is_in_live_session
;
1102 pthread_mutex_init(&channel
->lock
, NULL
);
1103 pthread_mutex_init(&channel
->timer_lock
, NULL
);
1106 case LTTNG_EVENT_SPLICE
:
1107 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
1109 case LTTNG_EVENT_MMAP
:
1110 channel
->output
= CONSUMER_CHANNEL_MMAP
;
1120 * In monitor mode, the streams associated with the channel will be put in
1121 * a special list ONLY owned by this channel. So, the refcount is set to 1
1122 * here meaning that the channel itself has streams that are referenced.
1124 * On a channel deletion, once the channel is no longer visible, the
1125 * refcount is decremented and checked for a zero value to delete it. With
1126 * streams in no monitor mode, it will now be safe to destroy the channel.
1128 if (!channel
->monitor
) {
1129 channel
->refcount
= 1;
1132 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1133 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1135 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1136 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1138 if (root_shm_path
) {
1139 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1140 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1143 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1144 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1147 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1148 lttng_ht_node_init_u64(&channel
->channels_by_session_id_ht_node
,
1149 channel
->session_id
);
1151 channel
->wait_fd
= -1;
1152 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1155 int ret
= lttng_consumer_channel_set_trace_chunk(channel
,
1162 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1165 lttng_trace_chunk_put(trace_chunk
);
1168 consumer_del_channel(channel
);
1174 * Add a channel to the global list protected by a mutex.
1176 * Always return 0 indicating success.
1178 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1179 struct lttng_consumer_local_data
*ctx
)
1181 pthread_mutex_lock(&consumer_data
.lock
);
1182 pthread_mutex_lock(&channel
->lock
);
1183 pthread_mutex_lock(&channel
->timer_lock
);
1186 * This gives us a guarantee that the channel we are about to add to the
1187 * channel hash table will be unique. See this function comment on the why
1188 * we need to steel the channel key at this stage.
1190 steal_channel_key(channel
->key
);
1193 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1194 lttng_ht_add_u64(consumer_data
.channels_by_session_id_ht
,
1195 &channel
->channels_by_session_id_ht_node
);
1197 channel
->is_published
= true;
1199 pthread_mutex_unlock(&channel
->timer_lock
);
1200 pthread_mutex_unlock(&channel
->lock
);
1201 pthread_mutex_unlock(&consumer_data
.lock
);
1203 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1204 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1211 * Allocate the pollfd structure and the local view of the out fds to avoid
1212 * doing a lookup in the linked list and concurrency issues when writing is
1213 * needed. Called with consumer_data.lock held.
1215 * Returns the number of fds in the structures.
1217 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1218 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1219 struct lttng_ht
*ht
, int *nb_inactive_fd
)
1222 struct lttng_ht_iter iter
;
1223 struct lttng_consumer_stream
*stream
;
1228 assert(local_stream
);
1230 DBG("Updating poll fd array");
1231 *nb_inactive_fd
= 0;
1233 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1235 * Only active streams with an active end point can be added to the
1236 * poll set and local stream storage of the thread.
1238 * There is a potential race here for endpoint_status to be updated
1239 * just after the check. However, this is OK since the stream(s) will
1240 * be deleted once the thread is notified that the end point state has
1241 * changed where this function will be called back again.
1243 * We track the number of inactive FDs because they still need to be
1244 * closed by the polling thread after a wakeup on the data_pipe or
1247 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1248 (*nb_inactive_fd
)++;
1252 * This clobbers way too much the debug output. Uncomment that if you
1253 * need it for debugging purposes.
1255 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1256 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1257 local_stream
[i
] = stream
;
1263 * Insert the consumer_data_pipe at the end of the array and don't
1264 * increment i so nb_fd is the number of real FD.
1266 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1267 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1269 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1270 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1275 * Poll on the should_quit pipe and the command socket return -1 on
1276 * error, 1 if should exit, 0 if data is available on the command socket
1278 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1283 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1284 if (num_rdy
== -1) {
1286 * Restart interrupted system call.
1288 if (errno
== EINTR
) {
1291 PERROR("Poll error");
1294 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1295 DBG("consumer_should_quit wake up");
1302 * Set the error socket.
1304 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1307 ctx
->consumer_error_socket
= sock
;
1311 * Set the command socket path.
1313 void lttng_consumer_set_command_sock_path(
1314 struct lttng_consumer_local_data
*ctx
, char *sock
)
1316 ctx
->consumer_command_sock_path
= sock
;
1320 * Send return code to the session daemon.
1321 * If the socket is not defined, we return 0, it is not a fatal error
1323 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1325 if (ctx
->consumer_error_socket
> 0) {
1326 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1327 sizeof(enum lttcomm_sessiond_command
));
1334 * Close all the tracefiles and stream fds and MUST be called when all
1335 * instances are destroyed i.e. when all threads were joined and are ended.
1337 void lttng_consumer_cleanup(void)
1339 struct lttng_ht_iter iter
;
1340 struct lttng_consumer_channel
*channel
;
1341 unsigned int trace_chunks_left
;
1345 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1347 consumer_del_channel(channel
);
1352 lttng_ht_destroy(consumer_data
.channel_ht
);
1353 lttng_ht_destroy(consumer_data
.channels_by_session_id_ht
);
1355 cleanup_relayd_ht();
1357 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1360 * This HT contains streams that are freed by either the metadata thread or
1361 * the data thread so we do *nothing* on the hash table and simply destroy
1364 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1367 * Trace chunks in the registry may still exist if the session
1368 * daemon has encountered an internal error and could not
1369 * tear down its sessions and/or trace chunks properly.
1371 * Release the session daemon's implicit reference to any remaining
1372 * trace chunk and print an error if any trace chunk was found. Note
1373 * that there are _no_ legitimate cases for trace chunks to be left,
1374 * it is a leak. However, it can happen following a crash of the
1375 * session daemon and not emptying the registry would cause an assertion
1378 trace_chunks_left
= lttng_trace_chunk_registry_put_each_chunk(
1379 consumer_data
.chunk_registry
);
1380 if (trace_chunks_left
) {
1381 ERR("%u trace chunks are leaked by lttng-consumerd. "
1382 "This can be caused by an internal error of the session daemon.",
1385 /* Run all callbacks freeing each chunk. */
1387 lttng_trace_chunk_registry_destroy(consumer_data
.chunk_registry
);
1391 * Called from signal handler.
1393 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1397 CMM_STORE_SHARED(consumer_quit
, 1);
1398 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1400 PERROR("write consumer quit");
1403 DBG("Consumer flag that it should quit");
1408 * Flush pending writes to trace output disk file.
1411 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1415 int outfd
= stream
->out_fd
;
1418 * This does a blocking write-and-wait on any page that belongs to the
1419 * subbuffer prior to the one we just wrote.
1420 * Don't care about error values, as these are just hints and ways to
1421 * limit the amount of page cache used.
1423 if (orig_offset
< stream
->max_sb_size
) {
1426 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1427 stream
->max_sb_size
,
1428 SYNC_FILE_RANGE_WAIT_BEFORE
1429 | SYNC_FILE_RANGE_WRITE
1430 | SYNC_FILE_RANGE_WAIT_AFTER
);
1432 * Give hints to the kernel about how we access the file:
1433 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1436 * We need to call fadvise again after the file grows because the
1437 * kernel does not seem to apply fadvise to non-existing parts of the
1440 * Call fadvise _after_ having waited for the page writeback to
1441 * complete because the dirty page writeback semantic is not well
1442 * defined. So it can be expected to lead to lower throughput in
1445 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1446 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1447 if (ret
&& ret
!= -ENOSYS
) {
1449 PERROR("posix_fadvise on fd %i", outfd
);
1454 * Initialise the necessary environnement :
1455 * - create a new context
1456 * - create the poll_pipe
1457 * - create the should_quit pipe (for signal handler)
1458 * - create the thread pipe (for splice)
1460 * Takes a function pointer as argument, this function is called when data is
1461 * available on a buffer. This function is responsible to do the
1462 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1463 * buffer configuration and then kernctl_put_next_subbuf at the end.
1465 * Returns a pointer to the new context or NULL on error.
1467 struct lttng_consumer_local_data
*lttng_consumer_create(
1468 enum lttng_consumer_type type
,
1469 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1470 struct lttng_consumer_local_data
*ctx
),
1471 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1472 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1473 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1476 struct lttng_consumer_local_data
*ctx
;
1478 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1479 consumer_data
.type
== type
);
1480 consumer_data
.type
= type
;
1482 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1484 PERROR("allocating context");
1488 ctx
->consumer_error_socket
= -1;
1489 ctx
->consumer_metadata_socket
= -1;
1490 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1491 /* assign the callbacks */
1492 ctx
->on_buffer_ready
= buffer_ready
;
1493 ctx
->on_recv_channel
= recv_channel
;
1494 ctx
->on_recv_stream
= recv_stream
;
1495 ctx
->on_update_stream
= update_stream
;
1497 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1498 if (!ctx
->consumer_data_pipe
) {
1499 goto error_poll_pipe
;
1502 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1503 if (!ctx
->consumer_wakeup_pipe
) {
1504 goto error_wakeup_pipe
;
1507 ret
= pipe(ctx
->consumer_should_quit
);
1509 PERROR("Error creating recv pipe");
1510 goto error_quit_pipe
;
1513 ret
= pipe(ctx
->consumer_channel_pipe
);
1515 PERROR("Error creating channel pipe");
1516 goto error_channel_pipe
;
1519 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1520 if (!ctx
->consumer_metadata_pipe
) {
1521 goto error_metadata_pipe
;
1524 ctx
->channel_monitor_pipe
= -1;
1528 error_metadata_pipe
:
1529 utils_close_pipe(ctx
->consumer_channel_pipe
);
1531 utils_close_pipe(ctx
->consumer_should_quit
);
1533 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1535 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1543 * Iterate over all streams of the hashtable and free them properly.
1545 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1547 struct lttng_ht_iter iter
;
1548 struct lttng_consumer_stream
*stream
;
1555 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1557 * Ignore return value since we are currently cleaning up so any error
1560 (void) consumer_del_stream(stream
, ht
);
1564 lttng_ht_destroy(ht
);
1568 * Iterate over all streams of the metadata hashtable and free them
1571 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1573 struct lttng_ht_iter iter
;
1574 struct lttng_consumer_stream
*stream
;
1581 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1583 * Ignore return value since we are currently cleaning up so any error
1586 (void) consumer_del_metadata_stream(stream
, ht
);
1590 lttng_ht_destroy(ht
);
1594 * Close all fds associated with the instance and free the context.
1596 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1600 DBG("Consumer destroying it. Closing everything.");
1606 destroy_data_stream_ht(data_ht
);
1607 destroy_metadata_stream_ht(metadata_ht
);
1609 ret
= close(ctx
->consumer_error_socket
);
1613 ret
= close(ctx
->consumer_metadata_socket
);
1617 utils_close_pipe(ctx
->consumer_channel_pipe
);
1618 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1619 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1620 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1621 utils_close_pipe(ctx
->consumer_should_quit
);
1623 unlink(ctx
->consumer_command_sock_path
);
1628 * Write the metadata stream id on the specified file descriptor.
1630 static int write_relayd_metadata_id(int fd
,
1631 struct lttng_consumer_stream
*stream
,
1632 unsigned long padding
)
1635 struct lttcomm_relayd_metadata_payload hdr
;
1637 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1638 hdr
.padding_size
= htobe32(padding
);
1639 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1640 if (ret
< sizeof(hdr
)) {
1642 * This error means that the fd's end is closed so ignore the PERROR
1643 * not to clubber the error output since this can happen in a normal
1646 if (errno
!= EPIPE
) {
1647 PERROR("write metadata stream id");
1649 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1651 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1652 * handle writting the missing part so report that as an error and
1653 * don't lie to the caller.
1658 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1659 stream
->relayd_stream_id
, padding
);
1666 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1667 * core function for writing trace buffers to either the local filesystem or
1670 * It must be called with the stream and the channel lock held.
1672 * Careful review MUST be put if any changes occur!
1674 * Returns the number of bytes written
1676 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1677 struct lttng_consumer_local_data
*ctx
,
1678 struct lttng_consumer_stream
*stream
,
1679 const struct lttng_buffer_view
*buffer
,
1680 unsigned long padding
,
1681 struct ctf_packet_index
*index
)
1684 off_t orig_offset
= stream
->out_fd_offset
;
1685 /* Default is on the disk */
1686 int outfd
= stream
->out_fd
;
1687 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1688 unsigned int relayd_hang_up
= 0;
1689 const size_t subbuf_content_size
= buffer
->size
- padding
;
1692 /* RCU lock for the relayd pointer */
1694 assert(stream
->net_seq_idx
!= (uint64_t) -1ULL ||
1695 stream
->trace_chunk
);
1697 /* Flag that the current stream if set for network streaming. */
1698 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1699 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1700 if (relayd
== NULL
) {
1706 /* Handle stream on the relayd if the output is on the network */
1708 unsigned long netlen
= subbuf_content_size
;
1711 * Lock the control socket for the complete duration of the function
1712 * since from this point on we will use the socket.
1714 if (stream
->metadata_flag
) {
1715 /* Metadata requires the control socket. */
1716 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1717 if (stream
->reset_metadata_flag
) {
1718 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1719 stream
->relayd_stream_id
,
1720 stream
->metadata_version
);
1725 stream
->reset_metadata_flag
= 0;
1727 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1730 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1735 /* Use the returned socket. */
1738 /* Write metadata stream id before payload */
1739 if (stream
->metadata_flag
) {
1740 ret
= write_relayd_metadata_id(outfd
, stream
, padding
);
1747 write_len
= subbuf_content_size
;
1749 /* No streaming; we have to write the full padding. */
1750 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1751 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1753 ERR("Reset metadata file");
1756 stream
->reset_metadata_flag
= 0;
1760 * Check if we need to change the tracefile before writing the packet.
1762 if (stream
->chan
->tracefile_size
> 0 &&
1763 (stream
->tracefile_size_current
+ buffer
->size
) >
1764 stream
->chan
->tracefile_size
) {
1765 ret
= consumer_stream_rotate_output_files(stream
);
1769 outfd
= stream
->out_fd
;
1772 stream
->tracefile_size_current
+= buffer
->size
;
1774 index
->offset
= htobe64(stream
->out_fd_offset
);
1777 write_len
= buffer
->size
;
1781 * This call guarantee that len or less is returned. It's impossible to
1782 * receive a ret value that is bigger than len.
1784 ret
= lttng_write(outfd
, buffer
->data
, write_len
);
1785 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, write_len
);
1786 if (ret
< 0 || ((size_t) ret
!= write_len
)) {
1788 * Report error to caller if nothing was written else at least send the
1796 /* Socket operation failed. We consider the relayd dead */
1797 if (errno
== EPIPE
) {
1799 * This is possible if the fd is closed on the other side
1800 * (outfd) or any write problem. It can be verbose a bit for a
1801 * normal execution if for instance the relayd is stopped
1802 * abruptly. This can happen so set this to a DBG statement.
1804 DBG("Consumer mmap write detected relayd hang up");
1806 /* Unhandled error, print it and stop function right now. */
1807 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret
,
1812 stream
->output_written
+= ret
;
1814 /* This call is useless on a socket so better save a syscall. */
1816 /* This won't block, but will start writeout asynchronously */
1817 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, write_len
,
1818 SYNC_FILE_RANGE_WRITE
);
1819 stream
->out_fd_offset
+= write_len
;
1820 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1825 * This is a special case that the relayd has closed its socket. Let's
1826 * cleanup the relayd object and all associated streams.
1828 if (relayd
&& relayd_hang_up
) {
1829 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1830 lttng_consumer_cleanup_relayd(relayd
);
1834 /* Unlock only if ctrl socket used */
1835 if (relayd
&& stream
->metadata_flag
) {
1836 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1844 * Splice the data from the ring buffer to the tracefile.
1846 * It must be called with the stream lock held.
1848 * Returns the number of bytes spliced.
1850 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1851 struct lttng_consumer_local_data
*ctx
,
1852 struct lttng_consumer_stream
*stream
, unsigned long len
,
1853 unsigned long padding
,
1854 struct ctf_packet_index
*index
)
1856 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1858 off_t orig_offset
= stream
->out_fd_offset
;
1859 int fd
= stream
->wait_fd
;
1860 /* Default is on the disk */
1861 int outfd
= stream
->out_fd
;
1862 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1864 unsigned int relayd_hang_up
= 0;
1866 switch (consumer_data
.type
) {
1867 case LTTNG_CONSUMER_KERNEL
:
1869 case LTTNG_CONSUMER32_UST
:
1870 case LTTNG_CONSUMER64_UST
:
1871 /* Not supported for user space tracing */
1874 ERR("Unknown consumer_data type");
1878 /* RCU lock for the relayd pointer */
1881 /* Flag that the current stream if set for network streaming. */
1882 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1883 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1884 if (relayd
== NULL
) {
1889 splice_pipe
= stream
->splice_pipe
;
1891 /* Write metadata stream id before payload */
1893 unsigned long total_len
= len
;
1895 if (stream
->metadata_flag
) {
1897 * Lock the control socket for the complete duration of the function
1898 * since from this point on we will use the socket.
1900 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1902 if (stream
->reset_metadata_flag
) {
1903 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1904 stream
->relayd_stream_id
,
1905 stream
->metadata_version
);
1910 stream
->reset_metadata_flag
= 0;
1912 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
,
1920 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1923 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1929 /* Use the returned socket. */
1932 /* No streaming, we have to set the len with the full padding */
1935 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1936 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1938 ERR("Reset metadata file");
1941 stream
->reset_metadata_flag
= 0;
1944 * Check if we need to change the tracefile before writing the packet.
1946 if (stream
->chan
->tracefile_size
> 0 &&
1947 (stream
->tracefile_size_current
+ len
) >
1948 stream
->chan
->tracefile_size
) {
1949 ret
= consumer_stream_rotate_output_files(stream
);
1954 outfd
= stream
->out_fd
;
1957 stream
->tracefile_size_current
+= len
;
1958 index
->offset
= htobe64(stream
->out_fd_offset
);
1962 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1963 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1964 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1965 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1966 DBG("splice chan to pipe, ret %zd", ret_splice
);
1967 if (ret_splice
< 0) {
1970 PERROR("Error in relay splice");
1974 /* Handle stream on the relayd if the output is on the network */
1975 if (relayd
&& stream
->metadata_flag
) {
1976 size_t metadata_payload_size
=
1977 sizeof(struct lttcomm_relayd_metadata_payload
);
1979 /* Update counter to fit the spliced data */
1980 ret_splice
+= metadata_payload_size
;
1981 len
+= metadata_payload_size
;
1983 * We do this so the return value can match the len passed as
1984 * argument to this function.
1986 written
-= metadata_payload_size
;
1989 /* Splice data out */
1990 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1991 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1992 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1994 if (ret_splice
< 0) {
1999 } else if (ret_splice
> len
) {
2001 * We don't expect this code path to be executed but you never know
2002 * so this is an extra protection agains a buggy splice().
2005 written
+= ret_splice
;
2006 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
2010 /* All good, update current len and continue. */
2014 /* This call is useless on a socket so better save a syscall. */
2016 /* This won't block, but will start writeout asynchronously */
2017 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
2018 SYNC_FILE_RANGE_WRITE
);
2019 stream
->out_fd_offset
+= ret_splice
;
2021 stream
->output_written
+= ret_splice
;
2022 written
+= ret_splice
;
2025 lttng_consumer_sync_trace_file(stream
, orig_offset
);
2031 * This is a special case that the relayd has closed its socket. Let's
2032 * cleanup the relayd object and all associated streams.
2034 if (relayd
&& relayd_hang_up
) {
2035 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
2036 lttng_consumer_cleanup_relayd(relayd
);
2037 /* Skip splice error so the consumer does not fail */
2042 /* send the appropriate error description to sessiond */
2045 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
2048 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
2051 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
2056 if (relayd
&& stream
->metadata_flag
) {
2057 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2065 * Sample the snapshot positions for a specific fd
2067 * Returns 0 on success, < 0 on error
2069 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
2071 switch (consumer_data
.type
) {
2072 case LTTNG_CONSUMER_KERNEL
:
2073 return lttng_kconsumer_sample_snapshot_positions(stream
);
2074 case LTTNG_CONSUMER32_UST
:
2075 case LTTNG_CONSUMER64_UST
:
2076 return lttng_ustconsumer_sample_snapshot_positions(stream
);
2078 ERR("Unknown consumer_data type");
2084 * Take a snapshot for a specific fd
2086 * Returns 0 on success, < 0 on error
2088 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2090 switch (consumer_data
.type
) {
2091 case LTTNG_CONSUMER_KERNEL
:
2092 return lttng_kconsumer_take_snapshot(stream
);
2093 case LTTNG_CONSUMER32_UST
:
2094 case LTTNG_CONSUMER64_UST
:
2095 return lttng_ustconsumer_take_snapshot(stream
);
2097 ERR("Unknown consumer_data type");
2104 * Get the produced position
2106 * Returns 0 on success, < 0 on error
2108 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
2111 switch (consumer_data
.type
) {
2112 case LTTNG_CONSUMER_KERNEL
:
2113 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
2114 case LTTNG_CONSUMER32_UST
:
2115 case LTTNG_CONSUMER64_UST
:
2116 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2118 ERR("Unknown consumer_data type");
2125 * Get the consumed position (free-running counter position in bytes).
2127 * Returns 0 on success, < 0 on error
2129 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
2132 switch (consumer_data
.type
) {
2133 case LTTNG_CONSUMER_KERNEL
:
2134 return lttng_kconsumer_get_consumed_snapshot(stream
, pos
);
2135 case LTTNG_CONSUMER32_UST
:
2136 case LTTNG_CONSUMER64_UST
:
2137 return lttng_ustconsumer_get_consumed_snapshot(stream
, pos
);
2139 ERR("Unknown consumer_data type");
2145 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2146 int sock
, struct pollfd
*consumer_sockpoll
)
2148 switch (consumer_data
.type
) {
2149 case LTTNG_CONSUMER_KERNEL
:
2150 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2151 case LTTNG_CONSUMER32_UST
:
2152 case LTTNG_CONSUMER64_UST
:
2153 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2155 ERR("Unknown consumer_data type");
2162 void lttng_consumer_close_all_metadata(void)
2164 switch (consumer_data
.type
) {
2165 case LTTNG_CONSUMER_KERNEL
:
2167 * The Kernel consumer has a different metadata scheme so we don't
2168 * close anything because the stream will be closed by the session
2172 case LTTNG_CONSUMER32_UST
:
2173 case LTTNG_CONSUMER64_UST
:
2175 * Close all metadata streams. The metadata hash table is passed and
2176 * this call iterates over it by closing all wakeup fd. This is safe
2177 * because at this point we are sure that the metadata producer is
2178 * either dead or blocked.
2180 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2183 ERR("Unknown consumer_data type");
2189 * Clean up a metadata stream and free its memory.
2191 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2192 struct lttng_ht
*ht
)
2194 struct lttng_consumer_channel
*channel
= NULL
;
2195 bool free_channel
= false;
2199 * This call should NEVER receive regular stream. It must always be
2200 * metadata stream and this is crucial for data structure synchronization.
2202 assert(stream
->metadata_flag
);
2204 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2206 pthread_mutex_lock(&consumer_data
.lock
);
2208 * Note that this assumes that a stream's channel is never changed and
2209 * that the stream's lock doesn't need to be taken to sample its
2212 channel
= stream
->chan
;
2213 pthread_mutex_lock(&channel
->lock
);
2214 pthread_mutex_lock(&stream
->lock
);
2215 if (channel
->metadata_cache
) {
2216 /* Only applicable to userspace consumers. */
2217 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
2220 /* Remove any reference to that stream. */
2221 consumer_stream_delete(stream
, ht
);
2223 /* Close down everything including the relayd if one. */
2224 consumer_stream_close(stream
);
2225 /* Destroy tracer buffers of the stream. */
2226 consumer_stream_destroy_buffers(stream
);
2228 /* Atomically decrement channel refcount since other threads can use it. */
2229 if (!uatomic_sub_return(&channel
->refcount
, 1)
2230 && !uatomic_read(&channel
->nb_init_stream_left
)) {
2231 /* Go for channel deletion! */
2232 free_channel
= true;
2234 stream
->chan
= NULL
;
2237 * Nullify the stream reference so it is not used after deletion. The
2238 * channel lock MUST be acquired before being able to check for a NULL
2241 channel
->metadata_stream
= NULL
;
2243 if (channel
->metadata_cache
) {
2244 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
2246 pthread_mutex_unlock(&stream
->lock
);
2247 pthread_mutex_unlock(&channel
->lock
);
2248 pthread_mutex_unlock(&consumer_data
.lock
);
2251 consumer_del_channel(channel
);
2254 lttng_trace_chunk_put(stream
->trace_chunk
);
2255 stream
->trace_chunk
= NULL
;
2256 consumer_stream_free(stream
);
2260 * Action done with the metadata stream when adding it to the consumer internal
2261 * data structures to handle it.
2263 void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2265 struct lttng_ht
*ht
= metadata_ht
;
2266 struct lttng_ht_iter iter
;
2267 struct lttng_ht_node_u64
*node
;
2272 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2274 pthread_mutex_lock(&consumer_data
.lock
);
2275 pthread_mutex_lock(&stream
->chan
->lock
);
2276 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2277 pthread_mutex_lock(&stream
->lock
);
2280 * From here, refcounts are updated so be _careful_ when returning an error
2287 * Lookup the stream just to make sure it does not exist in our internal
2288 * state. This should NEVER happen.
2290 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2291 node
= lttng_ht_iter_get_node_u64(&iter
);
2295 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2296 * in terms of destroying the associated channel, because the action that
2297 * causes the count to become 0 also causes a stream to be added. The
2298 * channel deletion will thus be triggered by the following removal of this
2301 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2302 /* Increment refcount before decrementing nb_init_stream_left */
2304 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2307 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2309 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
2310 &stream
->node_channel_id
);
2313 * Add stream to the stream_list_ht of the consumer data. No need to steal
2314 * the key since the HT does not use it and we allow to add redundant keys
2317 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2321 pthread_mutex_unlock(&stream
->lock
);
2322 pthread_mutex_unlock(&stream
->chan
->lock
);
2323 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2324 pthread_mutex_unlock(&consumer_data
.lock
);
2328 * Delete data stream that are flagged for deletion (endpoint_status).
2330 static void validate_endpoint_status_data_stream(void)
2332 struct lttng_ht_iter iter
;
2333 struct lttng_consumer_stream
*stream
;
2335 DBG("Consumer delete flagged data stream");
2338 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2339 /* Validate delete flag of the stream */
2340 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2343 /* Delete it right now */
2344 consumer_del_stream(stream
, data_ht
);
2350 * Delete metadata stream that are flagged for deletion (endpoint_status).
2352 static void validate_endpoint_status_metadata_stream(
2353 struct lttng_poll_event
*pollset
)
2355 struct lttng_ht_iter iter
;
2356 struct lttng_consumer_stream
*stream
;
2358 DBG("Consumer delete flagged metadata stream");
2363 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2364 /* Validate delete flag of the stream */
2365 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2369 * Remove from pollset so the metadata thread can continue without
2370 * blocking on a deleted stream.
2372 lttng_poll_del(pollset
, stream
->wait_fd
);
2374 /* Delete it right now */
2375 consumer_del_metadata_stream(stream
, metadata_ht
);
2381 * Thread polls on metadata file descriptor and write them on disk or on the
2384 void *consumer_thread_metadata_poll(void *data
)
2386 int ret
, i
, pollfd
, err
= -1;
2387 uint32_t revents
, nb_fd
;
2388 struct lttng_consumer_stream
*stream
= NULL
;
2389 struct lttng_ht_iter iter
;
2390 struct lttng_ht_node_u64
*node
;
2391 struct lttng_poll_event events
;
2392 struct lttng_consumer_local_data
*ctx
= data
;
2395 rcu_register_thread();
2397 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2399 if (testpoint(consumerd_thread_metadata
)) {
2400 goto error_testpoint
;
2403 health_code_update();
2405 DBG("Thread metadata poll started");
2407 /* Size is set to 1 for the consumer_metadata pipe */
2408 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2410 ERR("Poll set creation failed");
2414 ret
= lttng_poll_add(&events
,
2415 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2421 DBG("Metadata main loop started");
2425 health_code_update();
2426 health_poll_entry();
2427 DBG("Metadata poll wait");
2428 ret
= lttng_poll_wait(&events
, -1);
2429 DBG("Metadata poll return from wait with %d fd(s)",
2430 LTTNG_POLL_GETNB(&events
));
2432 DBG("Metadata event caught in thread");
2434 if (errno
== EINTR
) {
2435 ERR("Poll EINTR caught");
2438 if (LTTNG_POLL_GETNB(&events
) == 0) {
2439 err
= 0; /* All is OK */
2446 /* From here, the event is a metadata wait fd */
2447 for (i
= 0; i
< nb_fd
; i
++) {
2448 health_code_update();
2450 revents
= LTTNG_POLL_GETEV(&events
, i
);
2451 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2453 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2454 if (revents
& LPOLLIN
) {
2457 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2458 &stream
, sizeof(stream
));
2459 if (pipe_len
< sizeof(stream
)) {
2461 PERROR("read metadata stream");
2464 * Remove the pipe from the poll set and continue the loop
2465 * since their might be data to consume.
2467 lttng_poll_del(&events
,
2468 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2469 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2473 /* A NULL stream means that the state has changed. */
2474 if (stream
== NULL
) {
2475 /* Check for deleted streams. */
2476 validate_endpoint_status_metadata_stream(&events
);
2480 DBG("Adding metadata stream %d to poll set",
2483 /* Add metadata stream to the global poll events list */
2484 lttng_poll_add(&events
, stream
->wait_fd
,
2485 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2486 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2487 DBG("Metadata thread pipe hung up");
2489 * Remove the pipe from the poll set and continue the loop
2490 * since their might be data to consume.
2492 lttng_poll_del(&events
,
2493 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2494 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2497 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2501 /* Handle other stream */
2507 uint64_t tmp_id
= (uint64_t) pollfd
;
2509 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2511 node
= lttng_ht_iter_get_node_u64(&iter
);
2514 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2517 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2518 /* Get the data out of the metadata file descriptor */
2519 DBG("Metadata available on fd %d", pollfd
);
2520 assert(stream
->wait_fd
== pollfd
);
2523 health_code_update();
2525 len
= ctx
->on_buffer_ready(stream
, ctx
);
2527 * We don't check the return value here since if we get
2528 * a negative len, it means an error occurred thus we
2529 * simply remove it from the poll set and free the
2534 /* It's ok to have an unavailable sub-buffer */
2535 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2536 /* Clean up stream from consumer and free it. */
2537 lttng_poll_del(&events
, stream
->wait_fd
);
2538 consumer_del_metadata_stream(stream
, metadata_ht
);
2540 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2541 DBG("Metadata fd %d is hup|err.", pollfd
);
2542 if (!stream
->hangup_flush_done
2543 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2544 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2545 DBG("Attempting to flush and consume the UST buffers");
2546 lttng_ustconsumer_on_stream_hangup(stream
);
2548 /* We just flushed the stream now read it. */
2550 health_code_update();
2552 len
= ctx
->on_buffer_ready(stream
, ctx
);
2554 * We don't check the return value here since if we get
2555 * a negative len, it means an error occurred thus we
2556 * simply remove it from the poll set and free the
2562 lttng_poll_del(&events
, stream
->wait_fd
);
2564 * This call update the channel states, closes file descriptors
2565 * and securely free the stream.
2567 consumer_del_metadata_stream(stream
, metadata_ht
);
2569 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2573 /* Release RCU lock for the stream looked up */
2581 DBG("Metadata poll thread exiting");
2583 lttng_poll_clean(&events
);
2588 ERR("Health error occurred in %s", __func__
);
2590 health_unregister(health_consumerd
);
2591 rcu_unregister_thread();
2596 * This thread polls the fds in the set to consume the data and write
2597 * it to tracefile if necessary.
2599 void *consumer_thread_data_poll(void *data
)
2601 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2602 struct pollfd
*pollfd
= NULL
;
2603 /* local view of the streams */
2604 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2605 /* local view of consumer_data.fds_count */
2607 /* 2 for the consumer_data_pipe and wake up pipe */
2608 const int nb_pipes_fd
= 2;
2609 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2610 int nb_inactive_fd
= 0;
2611 struct lttng_consumer_local_data
*ctx
= data
;
2614 rcu_register_thread();
2616 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2618 if (testpoint(consumerd_thread_data
)) {
2619 goto error_testpoint
;
2622 health_code_update();
2624 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2625 if (local_stream
== NULL
) {
2626 PERROR("local_stream malloc");
2631 health_code_update();
2637 * the fds set has been updated, we need to update our
2638 * local array as well
2640 pthread_mutex_lock(&consumer_data
.lock
);
2641 if (consumer_data
.need_update
) {
2646 local_stream
= NULL
;
2648 /* Allocate for all fds */
2649 pollfd
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) * sizeof(struct pollfd
));
2650 if (pollfd
== NULL
) {
2651 PERROR("pollfd malloc");
2652 pthread_mutex_unlock(&consumer_data
.lock
);
2656 local_stream
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) *
2657 sizeof(struct lttng_consumer_stream
*));
2658 if (local_stream
== NULL
) {
2659 PERROR("local_stream malloc");
2660 pthread_mutex_unlock(&consumer_data
.lock
);
2663 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2664 data_ht
, &nb_inactive_fd
);
2666 ERR("Error in allocating pollfd or local_outfds");
2667 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2668 pthread_mutex_unlock(&consumer_data
.lock
);
2672 consumer_data
.need_update
= 0;
2674 pthread_mutex_unlock(&consumer_data
.lock
);
2676 /* No FDs and consumer_quit, consumer_cleanup the thread */
2677 if (nb_fd
== 0 && nb_inactive_fd
== 0 &&
2678 CMM_LOAD_SHARED(consumer_quit
) == 1) {
2679 err
= 0; /* All is OK */
2682 /* poll on the array of fds */
2684 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2685 if (testpoint(consumerd_thread_data_poll
)) {
2688 health_poll_entry();
2689 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2691 DBG("poll num_rdy : %d", num_rdy
);
2692 if (num_rdy
== -1) {
2694 * Restart interrupted system call.
2696 if (errno
== EINTR
) {
2699 PERROR("Poll error");
2700 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2702 } else if (num_rdy
== 0) {
2703 DBG("Polling thread timed out");
2707 if (caa_unlikely(data_consumption_paused
)) {
2708 DBG("Data consumption paused, sleeping...");
2714 * If the consumer_data_pipe triggered poll go directly to the
2715 * beginning of the loop to update the array. We want to prioritize
2716 * array update over low-priority reads.
2718 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2719 ssize_t pipe_readlen
;
2721 DBG("consumer_data_pipe wake up");
2722 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2723 &new_stream
, sizeof(new_stream
));
2724 if (pipe_readlen
< sizeof(new_stream
)) {
2725 PERROR("Consumer data pipe");
2726 /* Continue so we can at least handle the current stream(s). */
2731 * If the stream is NULL, just ignore it. It's also possible that
2732 * the sessiond poll thread changed the consumer_quit state and is
2733 * waking us up to test it.
2735 if (new_stream
== NULL
) {
2736 validate_endpoint_status_data_stream();
2740 /* Continue to update the local streams and handle prio ones */
2744 /* Handle wakeup pipe. */
2745 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2747 ssize_t pipe_readlen
;
2749 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2751 if (pipe_readlen
< 0) {
2752 PERROR("Consumer data wakeup pipe");
2754 /* We've been awakened to handle stream(s). */
2755 ctx
->has_wakeup
= 0;
2758 /* Take care of high priority channels first. */
2759 for (i
= 0; i
< nb_fd
; i
++) {
2760 health_code_update();
2762 if (local_stream
[i
] == NULL
) {
2765 if (pollfd
[i
].revents
& POLLPRI
) {
2766 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2768 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2769 /* it's ok to have an unavailable sub-buffer */
2770 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2771 /* Clean the stream and free it. */
2772 consumer_del_stream(local_stream
[i
], data_ht
);
2773 local_stream
[i
] = NULL
;
2774 } else if (len
> 0) {
2775 local_stream
[i
]->data_read
= 1;
2781 * If we read high prio channel in this loop, try again
2782 * for more high prio data.
2788 /* Take care of low priority channels. */
2789 for (i
= 0; i
< nb_fd
; i
++) {
2790 health_code_update();
2792 if (local_stream
[i
] == NULL
) {
2795 if ((pollfd
[i
].revents
& POLLIN
) ||
2796 local_stream
[i
]->hangup_flush_done
||
2797 local_stream
[i
]->has_data
) {
2798 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2799 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2800 /* it's ok to have an unavailable sub-buffer */
2801 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2802 /* Clean the stream and free it. */
2803 consumer_del_stream(local_stream
[i
], data_ht
);
2804 local_stream
[i
] = NULL
;
2805 } else if (len
> 0) {
2806 local_stream
[i
]->data_read
= 1;
2811 /* Handle hangup and errors */
2812 for (i
= 0; i
< nb_fd
; i
++) {
2813 health_code_update();
2815 if (local_stream
[i
] == NULL
) {
2818 if (!local_stream
[i
]->hangup_flush_done
2819 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2820 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2821 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2822 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2824 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2825 /* Attempt read again, for the data we just flushed. */
2826 local_stream
[i
]->data_read
= 1;
2829 * If the poll flag is HUP/ERR/NVAL and we have
2830 * read no data in this pass, we can remove the
2831 * stream from its hash table.
2833 if ((pollfd
[i
].revents
& POLLHUP
)) {
2834 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2835 if (!local_stream
[i
]->data_read
) {
2836 consumer_del_stream(local_stream
[i
], data_ht
);
2837 local_stream
[i
] = NULL
;
2840 } else if (pollfd
[i
].revents
& POLLERR
) {
2841 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2842 if (!local_stream
[i
]->data_read
) {
2843 consumer_del_stream(local_stream
[i
], data_ht
);
2844 local_stream
[i
] = NULL
;
2847 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2848 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2849 if (!local_stream
[i
]->data_read
) {
2850 consumer_del_stream(local_stream
[i
], data_ht
);
2851 local_stream
[i
] = NULL
;
2855 if (local_stream
[i
] != NULL
) {
2856 local_stream
[i
]->data_read
= 0;
2863 DBG("polling thread exiting");
2868 * Close the write side of the pipe so epoll_wait() in
2869 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2870 * read side of the pipe. If we close them both, epoll_wait strangely does
2871 * not return and could create a endless wait period if the pipe is the
2872 * only tracked fd in the poll set. The thread will take care of closing
2875 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2880 ERR("Health error occurred in %s", __func__
);
2882 health_unregister(health_consumerd
);
2884 rcu_unregister_thread();
2889 * Close wake-up end of each stream belonging to the channel. This will
2890 * allow the poll() on the stream read-side to detect when the
2891 * write-side (application) finally closes them.
2894 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2896 struct lttng_ht
*ht
;
2897 struct lttng_consumer_stream
*stream
;
2898 struct lttng_ht_iter iter
;
2900 ht
= consumer_data
.stream_per_chan_id_ht
;
2903 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2904 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2905 ht
->match_fct
, &channel
->key
,
2906 &iter
.iter
, stream
, node_channel_id
.node
) {
2908 * Protect against teardown with mutex.
2910 pthread_mutex_lock(&stream
->lock
);
2911 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2914 switch (consumer_data
.type
) {
2915 case LTTNG_CONSUMER_KERNEL
:
2917 case LTTNG_CONSUMER32_UST
:
2918 case LTTNG_CONSUMER64_UST
:
2919 if (stream
->metadata_flag
) {
2920 /* Safe and protected by the stream lock. */
2921 lttng_ustconsumer_close_metadata(stream
->chan
);
2924 * Note: a mutex is taken internally within
2925 * liblttng-ust-ctl to protect timer wakeup_fd
2926 * use from concurrent close.
2928 lttng_ustconsumer_close_stream_wakeup(stream
);
2932 ERR("Unknown consumer_data type");
2936 pthread_mutex_unlock(&stream
->lock
);
2941 static void destroy_channel_ht(struct lttng_ht
*ht
)
2943 struct lttng_ht_iter iter
;
2944 struct lttng_consumer_channel
*channel
;
2952 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2953 ret
= lttng_ht_del(ht
, &iter
);
2958 lttng_ht_destroy(ht
);
2962 * This thread polls the channel fds to detect when they are being
2963 * closed. It closes all related streams if the channel is detected as
2964 * closed. It is currently only used as a shim layer for UST because the
2965 * consumerd needs to keep the per-stream wakeup end of pipes open for
2968 void *consumer_thread_channel_poll(void *data
)
2970 int ret
, i
, pollfd
, err
= -1;
2971 uint32_t revents
, nb_fd
;
2972 struct lttng_consumer_channel
*chan
= NULL
;
2973 struct lttng_ht_iter iter
;
2974 struct lttng_ht_node_u64
*node
;
2975 struct lttng_poll_event events
;
2976 struct lttng_consumer_local_data
*ctx
= data
;
2977 struct lttng_ht
*channel_ht
;
2979 rcu_register_thread();
2981 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2983 if (testpoint(consumerd_thread_channel
)) {
2984 goto error_testpoint
;
2987 health_code_update();
2989 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2991 /* ENOMEM at this point. Better to bail out. */
2995 DBG("Thread channel poll started");
2997 /* Size is set to 1 for the consumer_channel pipe */
2998 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
3000 ERR("Poll set creation failed");
3004 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
3010 DBG("Channel main loop started");
3014 health_code_update();
3015 DBG("Channel poll wait");
3016 health_poll_entry();
3017 ret
= lttng_poll_wait(&events
, -1);
3018 DBG("Channel poll return from wait with %d fd(s)",
3019 LTTNG_POLL_GETNB(&events
));
3021 DBG("Channel event caught in thread");
3023 if (errno
== EINTR
) {
3024 ERR("Poll EINTR caught");
3027 if (LTTNG_POLL_GETNB(&events
) == 0) {
3028 err
= 0; /* All is OK */
3035 /* From here, the event is a channel wait fd */
3036 for (i
= 0; i
< nb_fd
; i
++) {
3037 health_code_update();
3039 revents
= LTTNG_POLL_GETEV(&events
, i
);
3040 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3042 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
3043 if (revents
& LPOLLIN
) {
3044 enum consumer_channel_action action
;
3047 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
3050 ERR("Error reading channel pipe");
3052 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3057 case CONSUMER_CHANNEL_ADD
:
3058 DBG("Adding channel %d to poll set",
3061 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
3064 lttng_ht_add_unique_u64(channel_ht
,
3065 &chan
->wait_fd_node
);
3067 /* Add channel to the global poll events list */
3068 lttng_poll_add(&events
, chan
->wait_fd
,
3069 LPOLLERR
| LPOLLHUP
);
3071 case CONSUMER_CHANNEL_DEL
:
3074 * This command should never be called if the channel
3075 * has streams monitored by either the data or metadata
3076 * thread. The consumer only notify this thread with a
3077 * channel del. command if it receives a destroy
3078 * channel command from the session daemon that send it
3079 * if a command prior to the GET_CHANNEL failed.
3083 chan
= consumer_find_channel(key
);
3086 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
3089 lttng_poll_del(&events
, chan
->wait_fd
);
3090 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
3091 ret
= lttng_ht_del(channel_ht
, &iter
);
3094 switch (consumer_data
.type
) {
3095 case LTTNG_CONSUMER_KERNEL
:
3097 case LTTNG_CONSUMER32_UST
:
3098 case LTTNG_CONSUMER64_UST
:
3099 health_code_update();
3100 /* Destroy streams that might have been left in the stream list. */
3101 clean_channel_stream_list(chan
);
3104 ERR("Unknown consumer_data type");
3109 * Release our own refcount. Force channel deletion even if
3110 * streams were not initialized.
3112 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3113 consumer_del_channel(chan
);
3118 case CONSUMER_CHANNEL_QUIT
:
3120 * Remove the pipe from the poll set and continue the loop
3121 * since their might be data to consume.
3123 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3126 ERR("Unknown action");
3129 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3130 DBG("Channel thread pipe hung up");
3132 * Remove the pipe from the poll set and continue the loop
3133 * since their might be data to consume.
3135 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3138 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3142 /* Handle other stream */
3148 uint64_t tmp_id
= (uint64_t) pollfd
;
3150 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3152 node
= lttng_ht_iter_get_node_u64(&iter
);
3155 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
3158 /* Check for error event */
3159 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3160 DBG("Channel fd %d is hup|err.", pollfd
);
3162 lttng_poll_del(&events
, chan
->wait_fd
);
3163 ret
= lttng_ht_del(channel_ht
, &iter
);
3167 * This will close the wait fd for each stream associated to
3168 * this channel AND monitored by the data/metadata thread thus
3169 * will be clean by the right thread.
3171 consumer_close_channel_streams(chan
);
3173 /* Release our own refcount */
3174 if (!uatomic_sub_return(&chan
->refcount
, 1)
3175 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3176 consumer_del_channel(chan
);
3179 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3184 /* Release RCU lock for the channel looked up */
3192 lttng_poll_clean(&events
);
3194 destroy_channel_ht(channel_ht
);
3197 DBG("Channel poll thread exiting");
3200 ERR("Health error occurred in %s", __func__
);
3202 health_unregister(health_consumerd
);
3203 rcu_unregister_thread();
3207 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3208 struct pollfd
*sockpoll
, int client_socket
)
3215 ret
= lttng_consumer_poll_socket(sockpoll
);
3219 DBG("Metadata connection on client_socket");
3221 /* Blocking call, waiting for transmission */
3222 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3223 if (ctx
->consumer_metadata_socket
< 0) {
3224 WARN("On accept metadata");
3235 * This thread listens on the consumerd socket and receives the file
3236 * descriptors from the session daemon.
3238 void *consumer_thread_sessiond_poll(void *data
)
3240 int sock
= -1, client_socket
, ret
, err
= -1;
3242 * structure to poll for incoming data on communication socket avoids
3243 * making blocking sockets.
3245 struct pollfd consumer_sockpoll
[2];
3246 struct lttng_consumer_local_data
*ctx
= data
;
3248 rcu_register_thread();
3250 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3252 if (testpoint(consumerd_thread_sessiond
)) {
3253 goto error_testpoint
;
3256 health_code_update();
3258 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3259 unlink(ctx
->consumer_command_sock_path
);
3260 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3261 if (client_socket
< 0) {
3262 ERR("Cannot create command socket");
3266 ret
= lttcomm_listen_unix_sock(client_socket
);
3271 DBG("Sending ready command to lttng-sessiond");
3272 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3273 /* return < 0 on error, but == 0 is not fatal */
3275 ERR("Error sending ready command to lttng-sessiond");
3279 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3280 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3281 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3282 consumer_sockpoll
[1].fd
= client_socket
;
3283 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3285 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3293 DBG("Connection on client_socket");
3295 /* Blocking call, waiting for transmission */
3296 sock
= lttcomm_accept_unix_sock(client_socket
);
3303 * Setup metadata socket which is the second socket connection on the
3304 * command unix socket.
3306 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3315 /* This socket is not useful anymore. */
3316 ret
= close(client_socket
);
3318 PERROR("close client_socket");
3322 /* update the polling structure to poll on the established socket */
3323 consumer_sockpoll
[1].fd
= sock
;
3324 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3327 health_code_update();
3329 health_poll_entry();
3330 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3339 DBG("Incoming command on sock");
3340 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3343 * This could simply be a session daemon quitting. Don't output
3346 DBG("Communication interrupted on command socket");
3350 if (CMM_LOAD_SHARED(consumer_quit
)) {
3351 DBG("consumer_thread_receive_fds received quit from signal");
3352 err
= 0; /* All is OK */
3355 DBG("received command on sock");
3361 DBG("Consumer thread sessiond poll exiting");
3364 * Close metadata streams since the producer is the session daemon which
3367 * NOTE: for now, this only applies to the UST tracer.
3369 lttng_consumer_close_all_metadata();
3372 * when all fds have hung up, the polling thread
3375 CMM_STORE_SHARED(consumer_quit
, 1);
3378 * Notify the data poll thread to poll back again and test the
3379 * consumer_quit state that we just set so to quit gracefully.
3381 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3383 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3385 notify_health_quit_pipe(health_quit_pipe
);
3387 /* Cleaning up possibly open sockets. */
3391 PERROR("close sock sessiond poll");
3394 if (client_socket
>= 0) {
3395 ret
= close(client_socket
);
3397 PERROR("close client_socket sessiond poll");
3404 ERR("Health error occurred in %s", __func__
);
3406 health_unregister(health_consumerd
);
3408 rcu_unregister_thread();
3412 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3413 struct lttng_consumer_local_data
*ctx
)
3417 pthread_mutex_lock(&stream
->chan
->lock
);
3418 pthread_mutex_lock(&stream
->lock
);
3419 if (stream
->metadata_flag
) {
3420 pthread_mutex_lock(&stream
->metadata_rdv_lock
);
3423 switch (consumer_data
.type
) {
3424 case LTTNG_CONSUMER_KERNEL
:
3425 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3427 case LTTNG_CONSUMER32_UST
:
3428 case LTTNG_CONSUMER64_UST
:
3429 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3432 ERR("Unknown consumer_data type");
3438 if (stream
->metadata_flag
) {
3439 pthread_cond_broadcast(&stream
->metadata_rdv
);
3440 pthread_mutex_unlock(&stream
->metadata_rdv_lock
);
3442 pthread_mutex_unlock(&stream
->lock
);
3443 pthread_mutex_unlock(&stream
->chan
->lock
);
3448 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3450 switch (consumer_data
.type
) {
3451 case LTTNG_CONSUMER_KERNEL
:
3452 return lttng_kconsumer_on_recv_stream(stream
);
3453 case LTTNG_CONSUMER32_UST
:
3454 case LTTNG_CONSUMER64_UST
:
3455 return lttng_ustconsumer_on_recv_stream(stream
);
3457 ERR("Unknown consumer_data type");
3464 * Allocate and set consumer data hash tables.
3466 int lttng_consumer_init(void)
3468 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3469 if (!consumer_data
.channel_ht
) {
3473 consumer_data
.channels_by_session_id_ht
=
3474 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3475 if (!consumer_data
.channels_by_session_id_ht
) {
3479 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3480 if (!consumer_data
.relayd_ht
) {
3484 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3485 if (!consumer_data
.stream_list_ht
) {
3489 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3490 if (!consumer_data
.stream_per_chan_id_ht
) {
3494 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3499 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3504 consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3505 if (!consumer_data
.chunk_registry
) {
3516 * Process the ADD_RELAYD command receive by a consumer.
3518 * This will create a relayd socket pair and add it to the relayd hash table.
3519 * The caller MUST acquire a RCU read side lock before calling it.
3521 void consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3522 struct lttng_consumer_local_data
*ctx
, int sock
,
3523 struct pollfd
*consumer_sockpoll
,
3524 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3525 uint64_t relayd_session_id
)
3527 int fd
= -1, ret
= -1, relayd_created
= 0;
3528 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3529 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3532 assert(relayd_sock
);
3534 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3536 /* Get relayd reference if exists. */
3537 relayd
= consumer_find_relayd(net_seq_idx
);
3538 if (relayd
== NULL
) {
3539 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3540 /* Not found. Allocate one. */
3541 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3542 if (relayd
== NULL
) {
3543 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3546 relayd
->sessiond_session_id
= sessiond_id
;
3551 * This code path MUST continue to the consumer send status message to
3552 * we can notify the session daemon and continue our work without
3553 * killing everything.
3557 * relayd key should never be found for control socket.
3559 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3562 /* First send a status message before receiving the fds. */
3563 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3565 /* Somehow, the session daemon is not responding anymore. */
3566 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3567 goto error_nosignal
;
3570 /* Poll on consumer socket. */
3571 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3573 /* Needing to exit in the middle of a command: error. */
3574 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3575 goto error_nosignal
;
3578 /* Get relayd socket from session daemon */
3579 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3580 if (ret
!= sizeof(fd
)) {
3581 fd
= -1; /* Just in case it gets set with an invalid value. */
3584 * Failing to receive FDs might indicate a major problem such as
3585 * reaching a fd limit during the receive where the kernel returns a
3586 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3587 * don't take any chances and stop everything.
3589 * XXX: Feature request #558 will fix that and avoid this possible
3590 * issue when reaching the fd limit.
3592 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3593 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3597 /* Copy socket information and received FD */
3598 switch (sock_type
) {
3599 case LTTNG_STREAM_CONTROL
:
3600 /* Copy received lttcomm socket */
3601 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3602 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3603 /* Handle create_sock error. */
3605 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3609 * Close the socket created internally by
3610 * lttcomm_create_sock, so we can replace it by the one
3611 * received from sessiond.
3613 if (close(relayd
->control_sock
.sock
.fd
)) {
3617 /* Assign new file descriptor */
3618 relayd
->control_sock
.sock
.fd
= fd
;
3619 /* Assign version values. */
3620 relayd
->control_sock
.major
= relayd_sock
->major
;
3621 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3623 relayd
->relayd_session_id
= relayd_session_id
;
3626 case LTTNG_STREAM_DATA
:
3627 /* Copy received lttcomm socket */
3628 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3629 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3630 /* Handle create_sock error. */
3632 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3636 * Close the socket created internally by
3637 * lttcomm_create_sock, so we can replace it by the one
3638 * received from sessiond.
3640 if (close(relayd
->data_sock
.sock
.fd
)) {
3644 /* Assign new file descriptor */
3645 relayd
->data_sock
.sock
.fd
= fd
;
3646 /* Assign version values. */
3647 relayd
->data_sock
.major
= relayd_sock
->major
;
3648 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3651 ERR("Unknown relayd socket type (%d)", sock_type
);
3652 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3656 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3657 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3658 relayd
->net_seq_idx
, fd
);
3660 * We gave the ownership of the fd to the relayd structure. Set the
3661 * fd to -1 so we don't call close() on it in the error path below.
3665 /* We successfully added the socket. Send status back. */
3666 ret
= consumer_send_status_msg(sock
, ret_code
);
3668 /* Somehow, the session daemon is not responding anymore. */
3669 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3670 goto error_nosignal
;
3674 * Add relayd socket pair to consumer data hashtable. If object already
3675 * exists or on error, the function gracefully returns.
3684 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3685 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3689 /* Close received socket if valid. */
3692 PERROR("close received socket");
3696 if (relayd_created
) {
3702 * Search for a relayd associated to the session id and return the reference.
3704 * A rcu read side lock MUST be acquire before calling this function and locked
3705 * until the relayd object is no longer necessary.
3707 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3709 struct lttng_ht_iter iter
;
3710 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3712 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3713 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3716 * Check by sessiond id which is unique here where the relayd session
3717 * id might not be when having multiple relayd.
3719 if (relayd
->sessiond_session_id
== id
) {
3720 /* Found the relayd. There can be only one per id. */
3732 * Check if for a given session id there is still data needed to be extract
3735 * Return 1 if data is pending or else 0 meaning ready to be read.
3737 int consumer_data_pending(uint64_t id
)
3740 struct lttng_ht_iter iter
;
3741 struct lttng_ht
*ht
;
3742 struct lttng_consumer_stream
*stream
;
3743 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3744 int (*data_pending
)(struct lttng_consumer_stream
*);
3746 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3749 pthread_mutex_lock(&consumer_data
.lock
);
3751 switch (consumer_data
.type
) {
3752 case LTTNG_CONSUMER_KERNEL
:
3753 data_pending
= lttng_kconsumer_data_pending
;
3755 case LTTNG_CONSUMER32_UST
:
3756 case LTTNG_CONSUMER64_UST
:
3757 data_pending
= lttng_ustconsumer_data_pending
;
3760 ERR("Unknown consumer data type");
3764 /* Ease our life a bit */
3765 ht
= consumer_data
.stream_list_ht
;
3767 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3768 ht
->hash_fct(&id
, lttng_ht_seed
),
3770 &iter
.iter
, stream
, node_session_id
.node
) {
3771 pthread_mutex_lock(&stream
->lock
);
3774 * A removed node from the hash table indicates that the stream has
3775 * been deleted thus having a guarantee that the buffers are closed
3776 * on the consumer side. However, data can still be transmitted
3777 * over the network so don't skip the relayd check.
3779 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3781 /* Check the stream if there is data in the buffers. */
3782 ret
= data_pending(stream
);
3784 pthread_mutex_unlock(&stream
->lock
);
3789 pthread_mutex_unlock(&stream
->lock
);
3792 relayd
= find_relayd_by_session_id(id
);
3794 unsigned int is_data_inflight
= 0;
3796 /* Send init command for data pending. */
3797 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3798 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3799 relayd
->relayd_session_id
);
3801 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3802 /* Communication error thus the relayd so no data pending. */
3803 goto data_not_pending
;
3806 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3807 ht
->hash_fct(&id
, lttng_ht_seed
),
3809 &iter
.iter
, stream
, node_session_id
.node
) {
3810 if (stream
->metadata_flag
) {
3811 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3812 stream
->relayd_stream_id
);
3814 ret
= relayd_data_pending(&relayd
->control_sock
,
3815 stream
->relayd_stream_id
,
3816 stream
->next_net_seq_num
- 1);
3820 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3822 } else if (ret
< 0) {
3823 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3824 lttng_consumer_cleanup_relayd(relayd
);
3825 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3826 goto data_not_pending
;
3830 /* Send end command for data pending. */
3831 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3832 relayd
->relayd_session_id
, &is_data_inflight
);
3833 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3835 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3836 lttng_consumer_cleanup_relayd(relayd
);
3837 goto data_not_pending
;
3839 if (is_data_inflight
) {
3845 * Finding _no_ node in the hash table and no inflight data means that the
3846 * stream(s) have been removed thus data is guaranteed to be available for
3847 * analysis from the trace files.
3851 /* Data is available to be read by a viewer. */
3852 pthread_mutex_unlock(&consumer_data
.lock
);
3857 /* Data is still being extracted from buffers. */
3858 pthread_mutex_unlock(&consumer_data
.lock
);
3864 * Send a ret code status message to the sessiond daemon.
3866 * Return the sendmsg() return value.
3868 int consumer_send_status_msg(int sock
, int ret_code
)
3870 struct lttcomm_consumer_status_msg msg
;
3872 memset(&msg
, 0, sizeof(msg
));
3873 msg
.ret_code
= ret_code
;
3875 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3879 * Send a channel status message to the sessiond daemon.
3881 * Return the sendmsg() return value.
3883 int consumer_send_status_channel(int sock
,
3884 struct lttng_consumer_channel
*channel
)
3886 struct lttcomm_consumer_status_channel msg
;
3890 memset(&msg
, 0, sizeof(msg
));
3892 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3894 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3895 msg
.key
= channel
->key
;
3896 msg
.stream_count
= channel
->streams
.count
;
3899 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3902 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3903 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3904 uint64_t max_sb_size
)
3906 unsigned long start_pos
;
3908 if (!nb_packets_per_stream
) {
3909 return consumed_pos
; /* Grab everything */
3911 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
3912 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3913 if ((long) (start_pos
- consumed_pos
) < 0) {
3914 return consumed_pos
; /* Grab everything */
3920 int consumer_flush_buffer(struct lttng_consumer_stream
*stream
, int producer_active
)
3924 switch (consumer_data
.type
) {
3925 case LTTNG_CONSUMER_KERNEL
:
3926 if (producer_active
) {
3927 ret
= kernctl_buffer_flush(stream
->wait_fd
);
3929 ERR("Failed to flush kernel stream");
3933 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
3936 * Doing a buffer flush which does not take into
3937 * account empty packets. This is not perfect,
3938 * but required as a fall-back when
3939 * "flush_empty" is not implemented by
3942 ret
= kernctl_buffer_flush(stream
->wait_fd
);
3944 ERR("Failed to flush kernel stream");
3950 case LTTNG_CONSUMER32_UST
:
3951 case LTTNG_CONSUMER64_UST
:
3952 lttng_ustconsumer_flush_buffer(stream
, producer_active
);
3955 ERR("Unknown consumer_data type");
3964 * Sample the rotate position for all the streams of a channel. If a stream
3965 * is already at the rotate position (produced == consumed), we flag it as
3966 * ready for rotation. The rotation of ready streams occurs after we have
3967 * replied to the session daemon that we have finished sampling the positions.
3968 * Must be called with RCU read-side lock held to ensure existence of channel.
3970 * Returns 0 on success, < 0 on error
3972 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
3973 uint64_t key
, uint64_t relayd_id
, uint32_t metadata
,
3974 struct lttng_consumer_local_data
*ctx
)
3977 struct lttng_consumer_stream
*stream
;
3978 struct lttng_ht_iter iter
;
3979 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
3980 struct lttng_dynamic_array stream_rotation_positions
;
3981 uint64_t next_chunk_id
, stream_count
= 0;
3982 enum lttng_trace_chunk_status chunk_status
;
3983 const bool is_local_trace
= relayd_id
== -1ULL;
3984 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3985 bool rotating_to_new_chunk
= true;
3987 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
3989 lttng_dynamic_array_init(&stream_rotation_positions
,
3990 sizeof(struct relayd_stream_rotation_position
), NULL
);
3994 pthread_mutex_lock(&channel
->lock
);
3995 assert(channel
->trace_chunk
);
3996 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
,
3998 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4000 goto end_unlock_channel
;
4003 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4004 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4005 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4006 stream
, node_channel_id
.node
) {
4007 unsigned long produced_pos
= 0, consumed_pos
= 0;
4009 health_code_update();
4012 * Lock stream because we are about to change its state.
4014 pthread_mutex_lock(&stream
->lock
);
4016 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
4017 rotating_to_new_chunk
= false;
4021 * Do not flush an empty packet when rotating from a NULL trace
4022 * chunk. The stream has no means to output data, and the prior
4023 * rotation which rotated to NULL performed that side-effect already.
4025 if (stream
->trace_chunk
) {
4027 * For metadata stream, do an active flush, which does not
4028 * produce empty packets. For data streams, empty-flush;
4029 * ensures we have at least one packet in each stream per trace
4030 * chunk, even if no data was produced.
4032 ret
= consumer_flush_buffer(stream
, stream
->metadata_flag
? 1 : 0);
4034 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4036 goto end_unlock_stream
;
4040 ret
= lttng_consumer_take_snapshot(stream
);
4041 if (ret
< 0 && ret
!= -ENODATA
&& ret
!= -EAGAIN
) {
4042 ERR("Failed to sample snapshot position during channel rotation");
4043 goto end_unlock_stream
;
4046 ret
= lttng_consumer_get_produced_snapshot(stream
,
4049 ERR("Failed to sample produced position during channel rotation");
4050 goto end_unlock_stream
;
4053 ret
= lttng_consumer_get_consumed_snapshot(stream
,
4056 ERR("Failed to sample consumed position during channel rotation");
4057 goto end_unlock_stream
;
4061 * Align produced position on the start-of-packet boundary of the first
4062 * packet going into the next trace chunk.
4064 produced_pos
= ALIGN_FLOOR(produced_pos
, stream
->max_sb_size
);
4065 if (consumed_pos
== produced_pos
) {
4066 DBG("Set rotate ready for stream %" PRIu64
" produced = %lu consumed = %lu",
4067 stream
->key
, produced_pos
, consumed_pos
);
4068 stream
->rotate_ready
= true;
4070 DBG("Different consumed and produced positions "
4071 "for stream %" PRIu64
" produced = %lu consumed = %lu",
4072 stream
->key
, produced_pos
, consumed_pos
);
4075 * The rotation position is based on the packet_seq_num of the
4076 * packet following the last packet that was consumed for this
4077 * stream, incremented by the offset between produced and
4078 * consumed positions. This rotation position is a lower bound
4079 * (inclusive) at which the next trace chunk starts. Since it
4080 * is a lower bound, it is OK if the packet_seq_num does not
4081 * correspond exactly to the same packet identified by the
4082 * consumed_pos, which can happen in overwrite mode.
4084 if (stream
->sequence_number_unavailable
) {
4086 * Rotation should never be performed on a session which
4087 * interacts with a pre-2.8 lttng-modules, which does
4088 * not implement packet sequence number.
4090 ERR("Failure to rotate stream %" PRIu64
": sequence number unavailable",
4093 goto end_unlock_stream
;
4095 stream
->rotate_position
= stream
->last_sequence_number
+ 1 +
4096 ((produced_pos
- consumed_pos
) / stream
->max_sb_size
);
4097 DBG("Set rotation position for stream %" PRIu64
" at position %" PRIu64
,
4098 stream
->key
, stream
->rotate_position
);
4100 if (!is_local_trace
) {
4102 * The relay daemon control protocol expects a rotation
4103 * position as "the sequence number of the first packet
4104 * _after_ the current trace chunk".
4106 const struct relayd_stream_rotation_position position
= {
4107 .stream_id
= stream
->relayd_stream_id
,
4108 .rotate_at_seq_num
= stream
->rotate_position
,
4111 ret
= lttng_dynamic_array_add_element(
4112 &stream_rotation_positions
,
4115 ERR("Failed to allocate stream rotation position");
4116 goto end_unlock_stream
;
4120 pthread_mutex_unlock(&stream
->lock
);
4123 pthread_mutex_unlock(&channel
->lock
);
4125 if (is_local_trace
) {
4130 relayd
= consumer_find_relayd(relayd_id
);
4132 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4137 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4138 ret
= relayd_rotate_streams(&relayd
->control_sock
, stream_count
,
4139 rotating_to_new_chunk
? &next_chunk_id
: NULL
,
4140 (const struct relayd_stream_rotation_position
*)
4141 stream_rotation_positions
.buffer
.data
);
4142 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4144 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4145 relayd
->net_seq_idx
);
4146 lttng_consumer_cleanup_relayd(relayd
);
4154 pthread_mutex_unlock(&stream
->lock
);
4156 pthread_mutex_unlock(&channel
->lock
);
4159 lttng_dynamic_array_reset(&stream_rotation_positions
);
4164 int consumer_clear_buffer(struct lttng_consumer_stream
*stream
)
4167 unsigned long consumed_pos_before
, consumed_pos_after
;
4169 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4171 ERR("Taking snapshot positions");
4175 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_before
);
4177 ERR("Consumed snapshot position");
4181 switch (consumer_data
.type
) {
4182 case LTTNG_CONSUMER_KERNEL
:
4183 ret
= kernctl_buffer_clear(stream
->wait_fd
);
4185 ERR("Failed to clear kernel stream (ret = %d)", ret
);
4189 case LTTNG_CONSUMER32_UST
:
4190 case LTTNG_CONSUMER64_UST
:
4191 lttng_ustconsumer_clear_buffer(stream
);
4194 ERR("Unknown consumer_data type");
4198 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4200 ERR("Taking snapshot positions");
4203 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_after
);
4205 ERR("Consumed snapshot position");
4208 DBG("clear: before: %lu after: %lu", consumed_pos_before
, consumed_pos_after
);
4214 int consumer_clear_stream(struct lttng_consumer_stream
*stream
)
4218 ret
= consumer_flush_buffer(stream
, 1);
4220 ERR("Failed to flush stream %" PRIu64
" during channel clear",
4222 ret
= LTTCOMM_CONSUMERD_FATAL
;
4226 ret
= consumer_clear_buffer(stream
);
4228 ERR("Failed to clear stream %" PRIu64
" during channel clear",
4230 ret
= LTTCOMM_CONSUMERD_FATAL
;
4234 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4240 int consumer_clear_unmonitored_channel(struct lttng_consumer_channel
*channel
)
4243 struct lttng_consumer_stream
*stream
;
4246 pthread_mutex_lock(&channel
->lock
);
4247 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
4248 health_code_update();
4249 pthread_mutex_lock(&stream
->lock
);
4250 ret
= consumer_clear_stream(stream
);
4254 pthread_mutex_unlock(&stream
->lock
);
4256 pthread_mutex_unlock(&channel
->lock
);
4261 pthread_mutex_unlock(&stream
->lock
);
4262 pthread_mutex_unlock(&channel
->lock
);
4268 * Check if a stream is ready to be rotated after extracting it.
4270 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4271 * error. Stream lock must be held.
4273 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4275 DBG("Check is rotate ready for stream %" PRIu64
4276 " ready %u rotate_position %" PRIu64
4277 " last_sequence_number %" PRIu64
,
4278 stream
->key
, stream
->rotate_ready
,
4279 stream
->rotate_position
, stream
->last_sequence_number
);
4280 if (stream
->rotate_ready
) {
4285 * If packet seq num is unavailable, it means we are interacting
4286 * with a pre-2.8 lttng-modules which does not implement the
4287 * sequence number. Rotation should never be used by sessiond in this
4290 if (stream
->sequence_number_unavailable
) {
4291 ERR("Internal error: rotation used on stream %" PRIu64
4292 " with unavailable sequence number",
4297 if (stream
->rotate_position
== -1ULL ||
4298 stream
->last_sequence_number
== -1ULL) {
4303 * Rotate position not reached yet. The stream rotate position is
4304 * the position of the next packet belonging to the next trace chunk,
4305 * but consumerd considers rotation ready when reaching the last
4306 * packet of the current chunk, hence the "rotate_position - 1".
4309 DBG("Check is rotate ready for stream %" PRIu64
4310 " last_sequence_number %" PRIu64
4311 " rotate_position %" PRIu64
,
4312 stream
->key
, stream
->last_sequence_number
,
4313 stream
->rotate_position
);
4314 if (stream
->last_sequence_number
>= stream
->rotate_position
- 1) {
4322 * Reset the state for a stream after a rotation occurred.
4324 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4326 DBG("lttng_consumer_reset_stream_rotate_state for stream %" PRIu64
,
4328 stream
->rotate_position
= -1ULL;
4329 stream
->rotate_ready
= false;
4333 * Perform the rotation a local stream file.
4336 int rotate_local_stream(struct lttng_consumer_local_data
*ctx
,
4337 struct lttng_consumer_stream
*stream
)
4341 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4344 stream
->tracefile_size_current
= 0;
4345 stream
->tracefile_count_current
= 0;
4347 if (stream
->out_fd
>= 0) {
4348 ret
= close(stream
->out_fd
);
4350 PERROR("Failed to close stream out_fd of channel \"%s\"",
4351 stream
->chan
->name
);
4353 stream
->out_fd
= -1;
4356 if (stream
->index_file
) {
4357 lttng_index_file_put(stream
->index_file
);
4358 stream
->index_file
= NULL
;
4361 if (!stream
->trace_chunk
) {
4365 ret
= consumer_stream_create_output_files(stream
, true);
4371 * Performs the stream rotation for the rotate session feature if needed.
4372 * It must be called with the channel and stream locks held.
4374 * Return 0 on success, a negative number of error.
4376 int lttng_consumer_rotate_stream(struct lttng_consumer_local_data
*ctx
,
4377 struct lttng_consumer_stream
*stream
)
4381 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4384 * Update the stream's 'current' chunk to the session's (channel)
4385 * now-current chunk.
4387 lttng_trace_chunk_put(stream
->trace_chunk
);
4388 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4390 * A channel can be rotated and not have a "next" chunk
4391 * to transition to. In that case, the channel's "current chunk"
4392 * has not been closed yet, but it has not been updated to
4393 * a "next" trace chunk either. Hence, the stream, like its
4394 * parent channel, becomes part of no chunk and can't output
4395 * anything until a new trace chunk is created.
4397 stream
->trace_chunk
= NULL
;
4398 } else if (stream
->chan
->trace_chunk
&&
4399 !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4400 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4405 * Update the stream's trace chunk to its parent channel's
4406 * current trace chunk.
4408 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4411 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4412 ret
= rotate_local_stream(ctx
, stream
);
4414 ERR("Failed to rotate stream, ret = %i", ret
);
4419 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4421 * If the stream has transitioned to a new trace
4422 * chunk, the metadata should be re-dumped to the
4425 * However, it is possible for a stream to transition to
4426 * a "no-chunk" state. This can happen if a rotation
4427 * occurs on an inactive session. In such cases, the metadata
4428 * regeneration will happen when the next trace chunk is
4431 ret
= consumer_metadata_stream_dump(stream
);
4436 lttng_consumer_reset_stream_rotate_state(stream
);
4445 * Rotate all the ready streams now.
4447 * This is especially important for low throughput streams that have already
4448 * been consumed, we cannot wait for their next packet to perform the
4450 * Need to be called with RCU read-side lock held to ensure existence of
4453 * Returns 0 on success, < 0 on error
4455 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
,
4456 uint64_t key
, struct lttng_consumer_local_data
*ctx
)
4459 struct lttng_consumer_stream
*stream
;
4460 struct lttng_ht_iter iter
;
4461 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
4465 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4467 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4468 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4469 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4470 stream
, node_channel_id
.node
) {
4471 health_code_update();
4473 pthread_mutex_lock(&stream
->chan
->lock
);
4474 pthread_mutex_lock(&stream
->lock
);
4476 if (!stream
->rotate_ready
) {
4477 pthread_mutex_unlock(&stream
->lock
);
4478 pthread_mutex_unlock(&stream
->chan
->lock
);
4481 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4483 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
4484 pthread_mutex_unlock(&stream
->lock
);
4485 pthread_mutex_unlock(&stream
->chan
->lock
);
4498 enum lttcomm_return_code
lttng_consumer_init_command(
4499 struct lttng_consumer_local_data
*ctx
,
4500 const lttng_uuid sessiond_uuid
)
4502 enum lttcomm_return_code ret
;
4503 char uuid_str
[LTTNG_UUID_STR_LEN
];
4505 if (ctx
->sessiond_uuid
.is_set
) {
4506 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4510 ctx
->sessiond_uuid
.is_set
= true;
4511 memcpy(ctx
->sessiond_uuid
.value
, sessiond_uuid
, sizeof(lttng_uuid
));
4512 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4513 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4514 DBG("Received session daemon UUID: %s", uuid_str
);
4519 enum lttcomm_return_code
lttng_consumer_create_trace_chunk(
4520 const uint64_t *relayd_id
, uint64_t session_id
,
4522 time_t chunk_creation_timestamp
,
4523 const char *chunk_override_name
,
4524 const struct lttng_credentials
*credentials
,
4525 struct lttng_directory_handle
*chunk_directory_handle
)
4528 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4529 struct lttng_trace_chunk
*created_chunk
= NULL
, *published_chunk
= NULL
;
4530 enum lttng_trace_chunk_status chunk_status
;
4531 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4532 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4533 const char *relayd_id_str
= "(none)";
4534 const char *creation_timestamp_str
;
4535 struct lttng_ht_iter iter
;
4536 struct lttng_consumer_channel
*channel
;
4539 /* Only used for logging purposes. */
4540 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4541 "%" PRIu64
, *relayd_id
);
4542 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4543 relayd_id_str
= relayd_id_buffer
;
4545 relayd_id_str
= "(formatting error)";
4549 /* Local protocol error. */
4550 assert(chunk_creation_timestamp
);
4551 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4552 creation_timestamp_buffer
,
4553 sizeof(creation_timestamp_buffer
));
4554 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
4555 "(formatting error)";
4557 DBG("Consumer create trace chunk command: relay_id = %s"
4558 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4559 ", chunk_override_name = %s"
4560 ", chunk_creation_timestamp = %s",
4561 relayd_id_str
, session_id
, chunk_id
,
4562 chunk_override_name
? : "(none)",
4563 creation_timestamp_str
);
4566 * The trace chunk registry, as used by the consumer daemon, implicitly
4567 * owns the trace chunks. This is only needed in the consumer since
4568 * the consumer has no notion of a session beyond session IDs being
4569 * used to identify other objects.
4571 * The lttng_trace_chunk_registry_publish() call below provides a
4572 * reference which is not released; it implicitly becomes the session
4573 * daemon's reference to the chunk in the consumer daemon.
4575 * The lifetime of trace chunks in the consumer daemon is managed by
4576 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4577 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4579 created_chunk
= lttng_trace_chunk_create(chunk_id
,
4580 chunk_creation_timestamp
, NULL
);
4581 if (!created_chunk
) {
4582 ERR("Failed to create trace chunk");
4583 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4587 if (chunk_override_name
) {
4588 chunk_status
= lttng_trace_chunk_override_name(created_chunk
,
4589 chunk_override_name
);
4590 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4591 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4596 if (chunk_directory_handle
) {
4597 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
,
4599 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4600 ERR("Failed to set trace chunk credentials");
4601 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4605 * The consumer daemon has no ownership of the chunk output
4608 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
,
4609 chunk_directory_handle
);
4610 chunk_directory_handle
= NULL
;
4611 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4612 ERR("Failed to set trace chunk's directory handle");
4613 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4618 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4619 consumer_data
.chunk_registry
, session_id
,
4621 lttng_trace_chunk_put(created_chunk
);
4622 created_chunk
= NULL
;
4623 if (!published_chunk
) {
4624 ERR("Failed to publish trace chunk");
4625 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4630 cds_lfht_for_each_entry_duplicate(consumer_data
.channels_by_session_id_ht
->ht
,
4631 consumer_data
.channels_by_session_id_ht
->hash_fct(
4632 &session_id
, lttng_ht_seed
),
4633 consumer_data
.channels_by_session_id_ht
->match_fct
,
4634 &session_id
, &iter
.iter
, channel
,
4635 channels_by_session_id_ht_node
.node
) {
4636 ret
= lttng_consumer_channel_set_trace_chunk(channel
,
4640 * Roll-back the creation of this chunk.
4642 * This is important since the session daemon will
4643 * assume that the creation of this chunk failed and
4644 * will never ask for it to be closed, resulting
4645 * in a leak and an inconsistent state for some
4648 enum lttcomm_return_code close_ret
;
4649 char path
[LTTNG_PATH_MAX
];
4651 DBG("Failed to set new trace chunk on existing channels, rolling back");
4652 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4653 session_id
, chunk_id
,
4654 chunk_creation_timestamp
, NULL
,
4656 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4657 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4658 session_id
, chunk_id
);
4661 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4667 struct consumer_relayd_sock_pair
*relayd
;
4669 relayd
= consumer_find_relayd(*relayd_id
);
4671 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4672 ret
= relayd_create_trace_chunk(
4673 &relayd
->control_sock
, published_chunk
);
4674 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4676 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4679 if (!relayd
|| ret
) {
4680 enum lttcomm_return_code close_ret
;
4681 char path
[LTTNG_PATH_MAX
];
4683 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4686 chunk_creation_timestamp
,
4688 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4689 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4694 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4701 /* Release the reference returned by the "publish" operation. */
4702 lttng_trace_chunk_put(published_chunk
);
4703 lttng_trace_chunk_put(created_chunk
);
4707 enum lttcomm_return_code
lttng_consumer_close_trace_chunk(
4708 const uint64_t *relayd_id
, uint64_t session_id
,
4709 uint64_t chunk_id
, time_t chunk_close_timestamp
,
4710 const enum lttng_trace_chunk_command_type
*close_command
,
4713 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4714 struct lttng_trace_chunk
*chunk
;
4715 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4716 const char *relayd_id_str
= "(none)";
4717 const char *close_command_name
= "none";
4718 struct lttng_ht_iter iter
;
4719 struct lttng_consumer_channel
*channel
;
4720 enum lttng_trace_chunk_status chunk_status
;
4725 /* Only used for logging purposes. */
4726 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4727 "%" PRIu64
, *relayd_id
);
4728 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4729 relayd_id_str
= relayd_id_buffer
;
4731 relayd_id_str
= "(formatting error)";
4734 if (close_command
) {
4735 close_command_name
= lttng_trace_chunk_command_type_get_name(
4739 DBG("Consumer close trace chunk command: relayd_id = %s"
4740 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4741 ", close command = %s",
4742 relayd_id_str
, session_id
, chunk_id
,
4743 close_command_name
);
4745 chunk
= lttng_trace_chunk_registry_find_chunk(
4746 consumer_data
.chunk_registry
, session_id
, chunk_id
);
4748 ERR("Failed to find chunk: session_id = %" PRIu64
4749 ", chunk_id = %" PRIu64
,
4750 session_id
, chunk_id
);
4751 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4755 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
,
4756 chunk_close_timestamp
);
4757 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4758 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4762 if (close_command
) {
4763 chunk_status
= lttng_trace_chunk_set_close_command(
4764 chunk
, *close_command
);
4765 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4766 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4772 * chunk is now invalid to access as we no longer hold a reference to
4773 * it; it is only kept around to compare it (by address) to the
4774 * current chunk found in the session's channels.
4777 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
,
4778 channel
, node
.node
) {
4782 * Only change the channel's chunk to NULL if it still
4783 * references the chunk being closed. The channel may
4784 * reference a newer channel in the case of a session
4785 * rotation. When a session rotation occurs, the "next"
4786 * chunk is created before the "current" chunk is closed.
4788 if (channel
->trace_chunk
!= chunk
) {
4791 ret
= lttng_consumer_channel_set_trace_chunk(channel
, NULL
);
4794 * Attempt to close the chunk on as many channels as
4797 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4803 struct consumer_relayd_sock_pair
*relayd
;
4805 relayd
= consumer_find_relayd(*relayd_id
);
4807 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4808 ret
= relayd_close_trace_chunk(
4809 &relayd
->control_sock
, chunk
,
4811 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4813 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
,
4817 if (!relayd
|| ret
) {
4818 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4826 * Release the reference returned by the "find" operation and
4827 * the session daemon's implicit reference to the chunk.
4829 lttng_trace_chunk_put(chunk
);
4830 lttng_trace_chunk_put(chunk
);
4835 enum lttcomm_return_code
lttng_consumer_trace_chunk_exists(
4836 const uint64_t *relayd_id
, uint64_t session_id
,
4840 enum lttcomm_return_code ret_code
;
4841 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4842 const char *relayd_id_str
= "(none)";
4843 const bool is_local_trace
= !relayd_id
;
4844 struct consumer_relayd_sock_pair
*relayd
= NULL
;
4845 bool chunk_exists_local
, chunk_exists_remote
;
4850 /* Only used for logging purposes. */
4851 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4852 "%" PRIu64
, *relayd_id
);
4853 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4854 relayd_id_str
= relayd_id_buffer
;
4856 relayd_id_str
= "(formatting error)";
4860 DBG("Consumer trace chunk exists command: relayd_id = %s"
4861 ", chunk_id = %" PRIu64
, relayd_id_str
,
4863 ret
= lttng_trace_chunk_registry_chunk_exists(
4864 consumer_data
.chunk_registry
, session_id
,
4865 chunk_id
, &chunk_exists_local
);
4867 /* Internal error. */
4868 ERR("Failed to query the existence of a trace chunk");
4869 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
4872 DBG("Trace chunk %s locally",
4873 chunk_exists_local
? "exists" : "does not exist");
4874 if (chunk_exists_local
) {
4875 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
4877 } else if (is_local_trace
) {
4878 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4883 relayd
= consumer_find_relayd(*relayd_id
);
4885 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
4886 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
4887 goto end_rcu_unlock
;
4889 DBG("Looking up existence of trace chunk on relay daemon");
4890 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4891 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
,
4892 &chunk_exists_remote
);
4893 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4895 ERR("Failed to look-up the existence of trace chunk on relay daemon");
4896 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
4897 goto end_rcu_unlock
;
4900 ret_code
= chunk_exists_remote
?
4901 LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
4902 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4903 DBG("Trace chunk %s on relay daemon",
4904 chunk_exists_remote
? "exists" : "does not exist");
4913 int consumer_clear_monitored_channel(struct lttng_consumer_channel
*channel
)
4915 struct lttng_ht
*ht
;
4916 struct lttng_consumer_stream
*stream
;
4917 struct lttng_ht_iter iter
;
4920 ht
= consumer_data
.stream_per_chan_id_ht
;
4923 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4924 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4925 ht
->match_fct
, &channel
->key
,
4926 &iter
.iter
, stream
, node_channel_id
.node
) {
4928 * Protect against teardown with mutex.
4930 pthread_mutex_lock(&stream
->lock
);
4931 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
4934 ret
= consumer_clear_stream(stream
);
4939 pthread_mutex_unlock(&stream
->lock
);
4942 return LTTCOMM_CONSUMERD_SUCCESS
;
4945 pthread_mutex_unlock(&stream
->lock
);
4950 int lttng_consumer_clear_channel(struct lttng_consumer_channel
*channel
)
4954 DBG("Consumer clear channel %" PRIu64
, channel
->key
);
4956 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
4958 * Nothing to do for the metadata channel/stream.
4959 * Snapshot mechanism already take care of the metadata
4960 * handling/generation, and monitored channels only need to
4961 * have their data stream cleared..
4963 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4967 if (!channel
->monitor
) {
4968 ret
= consumer_clear_unmonitored_channel(channel
);
4970 ret
= consumer_clear_monitored_channel(channel
);