2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2012 - David Goulet <dgoulet@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/socket.h>
28 #include <sys/types.h>
33 #include <bin/lttng-consumerd/health-consumerd.h>
34 #include <common/common.h>
35 #include <common/utils.h>
36 #include <common/time.h>
37 #include <common/compat/poll.h>
38 #include <common/compat/endian.h>
39 #include <common/index/index.h>
40 #include <common/kernel-ctl/kernel-ctl.h>
41 #include <common/sessiond-comm/relayd.h>
42 #include <common/sessiond-comm/sessiond-comm.h>
43 #include <common/kernel-consumer/kernel-consumer.h>
44 #include <common/relayd/relayd.h>
45 #include <common/ust-consumer/ust-consumer.h>
46 #include <common/consumer/consumer-timer.h>
47 #include <common/consumer/consumer.h>
48 #include <common/consumer/consumer-stream.h>
49 #include <common/consumer/consumer-testpoint.h>
50 #include <common/align.h>
51 #include <common/consumer/consumer-metadata-cache.h>
52 #include <common/trace-chunk.h>
53 #include <common/trace-chunk-registry.h>
54 #include <common/string-utils/format.h>
55 #include <common/dynamic-array.h>
57 struct lttng_consumer_global_data consumer_data
= {
60 .type
= LTTNG_CONSUMER_UNKNOWN
,
63 enum consumer_channel_action
{
66 CONSUMER_CHANNEL_QUIT
,
69 struct consumer_channel_msg
{
70 enum consumer_channel_action action
;
71 struct lttng_consumer_channel
*chan
; /* add */
72 uint64_t key
; /* del */
75 /* Flag used to temporarily pause data consumption from testpoints. */
76 int data_consumption_paused
;
79 * Flag to inform the polling thread to quit when all fd hung up. Updated by
80 * the consumer_thread_receive_fds when it notices that all fds has hung up.
81 * Also updated by the signal handler (consumer_should_exit()). Read by the
87 * Global hash table containing respectively metadata and data streams. The
88 * stream element in this ht should only be updated by the metadata poll thread
89 * for the metadata and the data poll thread for the data.
91 static struct lttng_ht
*metadata_ht
;
92 static struct lttng_ht
*data_ht
;
94 static const char *get_consumer_domain(void)
96 switch (consumer_data
.type
) {
97 case LTTNG_CONSUMER_KERNEL
:
98 return DEFAULT_KERNEL_TRACE_DIR
;
99 case LTTNG_CONSUMER64_UST
:
101 case LTTNG_CONSUMER32_UST
:
102 return DEFAULT_UST_TRACE_DIR
;
109 * Notify a thread lttng pipe to poll back again. This usually means that some
110 * global state has changed so we just send back the thread in a poll wait
113 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
115 struct lttng_consumer_stream
*null_stream
= NULL
;
119 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
122 static void notify_health_quit_pipe(int *pipe
)
126 ret
= lttng_write(pipe
[1], "4", 1);
128 PERROR("write consumer health quit");
132 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
133 struct lttng_consumer_channel
*chan
,
135 enum consumer_channel_action action
)
137 struct consumer_channel_msg msg
;
140 memset(&msg
, 0, sizeof(msg
));
145 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
146 if (ret
< sizeof(msg
)) {
147 PERROR("notify_channel_pipe write error");
151 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
154 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
157 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
158 struct lttng_consumer_channel
**chan
,
160 enum consumer_channel_action
*action
)
162 struct consumer_channel_msg msg
;
165 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
166 if (ret
< sizeof(msg
)) {
170 *action
= msg
.action
;
178 * Cleanup the stream list of a channel. Those streams are not yet globally
181 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
183 struct lttng_consumer_stream
*stream
, *stmp
;
187 /* Delete streams that might have been left in the stream list. */
188 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
190 cds_list_del(&stream
->send_node
);
192 * Once a stream is added to this list, the buffers were created so we
193 * have a guarantee that this call will succeed. Setting the monitor
194 * mode to 0 so we don't lock nor try to delete the stream from the
198 consumer_stream_destroy(stream
, NULL
);
203 * Find a stream. The consumer_data.lock must be locked during this
206 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
209 struct lttng_ht_iter iter
;
210 struct lttng_ht_node_u64
*node
;
211 struct lttng_consumer_stream
*stream
= NULL
;
215 /* -1ULL keys are lookup failures */
216 if (key
== (uint64_t) -1ULL) {
222 lttng_ht_lookup(ht
, &key
, &iter
);
223 node
= lttng_ht_iter_get_node_u64(&iter
);
225 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
233 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
235 struct lttng_consumer_stream
*stream
;
238 stream
= find_stream(key
, ht
);
240 stream
->key
= (uint64_t) -1ULL;
242 * We don't want the lookup to match, but we still need
243 * to iterate on this stream when iterating over the hash table. Just
244 * change the node key.
246 stream
->node
.key
= (uint64_t) -1ULL;
252 * Return a channel object for the given key.
254 * RCU read side lock MUST be acquired before calling this function and
255 * protects the channel ptr.
257 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
259 struct lttng_ht_iter iter
;
260 struct lttng_ht_node_u64
*node
;
261 struct lttng_consumer_channel
*channel
= NULL
;
263 /* -1ULL keys are lookup failures */
264 if (key
== (uint64_t) -1ULL) {
268 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
269 node
= lttng_ht_iter_get_node_u64(&iter
);
271 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
278 * There is a possibility that the consumer does not have enough time between
279 * the close of the channel on the session daemon and the cleanup in here thus
280 * once we have a channel add with an existing key, we know for sure that this
281 * channel will eventually get cleaned up by all streams being closed.
283 * This function just nullifies the already existing channel key.
285 static void steal_channel_key(uint64_t key
)
287 struct lttng_consumer_channel
*channel
;
290 channel
= consumer_find_channel(key
);
292 channel
->key
= (uint64_t) -1ULL;
294 * We don't want the lookup to match, but we still need to iterate on
295 * this channel when iterating over the hash table. Just change the
298 channel
->node
.key
= (uint64_t) -1ULL;
303 static void free_channel_rcu(struct rcu_head
*head
)
305 struct lttng_ht_node_u64
*node
=
306 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
307 struct lttng_consumer_channel
*channel
=
308 caa_container_of(node
, struct lttng_consumer_channel
, node
);
310 switch (consumer_data
.type
) {
311 case LTTNG_CONSUMER_KERNEL
:
313 case LTTNG_CONSUMER32_UST
:
314 case LTTNG_CONSUMER64_UST
:
315 lttng_ustconsumer_free_channel(channel
);
318 ERR("Unknown consumer_data type");
325 * RCU protected relayd socket pair free.
327 static void free_relayd_rcu(struct rcu_head
*head
)
329 struct lttng_ht_node_u64
*node
=
330 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
331 struct consumer_relayd_sock_pair
*relayd
=
332 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
335 * Close all sockets. This is done in the call RCU since we don't want the
336 * socket fds to be reassigned thus potentially creating bad state of the
339 * We do not have to lock the control socket mutex here since at this stage
340 * there is no one referencing to this relayd object.
342 (void) relayd_close(&relayd
->control_sock
);
343 (void) relayd_close(&relayd
->data_sock
);
345 pthread_mutex_destroy(&relayd
->ctrl_sock_mutex
);
350 * Destroy and free relayd socket pair object.
352 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
355 struct lttng_ht_iter iter
;
357 if (relayd
== NULL
) {
361 DBG("Consumer destroy and close relayd socket pair");
363 iter
.iter
.node
= &relayd
->node
.node
;
364 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
366 /* We assume the relayd is being or is destroyed */
370 /* RCU free() call */
371 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
375 * Remove a channel from the global list protected by a mutex. This function is
376 * also responsible for freeing its data structures.
378 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
380 struct lttng_ht_iter iter
;
382 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
384 pthread_mutex_lock(&consumer_data
.lock
);
385 pthread_mutex_lock(&channel
->lock
);
387 /* Destroy streams that might have been left in the stream list. */
388 clean_channel_stream_list(channel
);
390 if (channel
->live_timer_enabled
== 1) {
391 consumer_timer_live_stop(channel
);
393 if (channel
->monitor_timer_enabled
== 1) {
394 consumer_timer_monitor_stop(channel
);
397 switch (consumer_data
.type
) {
398 case LTTNG_CONSUMER_KERNEL
:
400 case LTTNG_CONSUMER32_UST
:
401 case LTTNG_CONSUMER64_UST
:
402 lttng_ustconsumer_del_channel(channel
);
405 ERR("Unknown consumer_data type");
410 lttng_trace_chunk_put(channel
->trace_chunk
);
411 channel
->trace_chunk
= NULL
;
413 if (channel
->is_published
) {
417 iter
.iter
.node
= &channel
->node
.node
;
418 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
421 iter
.iter
.node
= &channel
->channels_by_session_id_ht_node
.node
;
422 ret
= lttng_ht_del(consumer_data
.channels_by_session_id_ht
,
428 channel
->is_deleted
= true;
429 call_rcu(&channel
->node
.head
, free_channel_rcu
);
431 pthread_mutex_unlock(&channel
->lock
);
432 pthread_mutex_unlock(&consumer_data
.lock
);
436 * Iterate over the relayd hash table and destroy each element. Finally,
437 * destroy the whole hash table.
439 static void cleanup_relayd_ht(void)
441 struct lttng_ht_iter iter
;
442 struct consumer_relayd_sock_pair
*relayd
;
446 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
448 consumer_destroy_relayd(relayd
);
453 lttng_ht_destroy(consumer_data
.relayd_ht
);
457 * Update the end point status of all streams having the given network sequence
458 * index (relayd index).
460 * It's atomically set without having the stream mutex locked which is fine
461 * because we handle the write/read race with a pipe wakeup for each thread.
463 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
464 enum consumer_endpoint_status status
)
466 struct lttng_ht_iter iter
;
467 struct lttng_consumer_stream
*stream
;
469 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
473 /* Let's begin with metadata */
474 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
475 if (stream
->net_seq_idx
== net_seq_idx
) {
476 uatomic_set(&stream
->endpoint_status
, status
);
477 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
481 /* Follow up by the data streams */
482 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
483 if (stream
->net_seq_idx
== net_seq_idx
) {
484 uatomic_set(&stream
->endpoint_status
, status
);
485 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
492 * Cleanup a relayd object by flagging every associated streams for deletion,
493 * destroying the object meaning removing it from the relayd hash table,
494 * closing the sockets and freeing the memory in a RCU call.
496 * If a local data context is available, notify the threads that the streams'
497 * state have changed.
499 void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
)
505 DBG("Cleaning up relayd object ID %"PRIu64
, relayd
->net_seq_idx
);
507 /* Save the net sequence index before destroying the object */
508 netidx
= relayd
->net_seq_idx
;
511 * Delete the relayd from the relayd hash table, close the sockets and free
512 * the object in a RCU call.
514 consumer_destroy_relayd(relayd
);
516 /* Set inactive endpoint to all streams */
517 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
520 * With a local data context, notify the threads that the streams' state
521 * have changed. The write() action on the pipe acts as an "implicit"
522 * memory barrier ordering the updates of the end point status from the
523 * read of this status which happens AFTER receiving this notify.
525 notify_thread_lttng_pipe(relayd
->ctx
->consumer_data_pipe
);
526 notify_thread_lttng_pipe(relayd
->ctx
->consumer_metadata_pipe
);
530 * Flag a relayd socket pair for destruction. Destroy it if the refcount
533 * RCU read side lock MUST be aquired before calling this function.
535 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
539 /* Set destroy flag for this object */
540 uatomic_set(&relayd
->destroy_flag
, 1);
542 /* Destroy the relayd if refcount is 0 */
543 if (uatomic_read(&relayd
->refcount
) == 0) {
544 consumer_destroy_relayd(relayd
);
549 * Completly destroy stream from every visiable data structure and the given
552 * One this call returns, the stream object is not longer usable nor visible.
554 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
557 consumer_stream_destroy(stream
, ht
);
561 * XXX naming of del vs destroy is all mixed up.
563 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
565 consumer_stream_destroy(stream
, data_ht
);
568 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
570 consumer_stream_destroy(stream
, metadata_ht
);
573 void consumer_stream_update_channel_attributes(
574 struct lttng_consumer_stream
*stream
,
575 struct lttng_consumer_channel
*channel
)
577 stream
->channel_read_only_attributes
.tracefile_size
=
578 channel
->tracefile_size
;
581 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
583 const char *channel_name
,
586 struct lttng_trace_chunk
*trace_chunk
,
589 enum consumer_channel_type type
,
590 unsigned int monitor
)
593 struct lttng_consumer_stream
*stream
;
595 stream
= zmalloc(sizeof(*stream
));
596 if (stream
== NULL
) {
597 PERROR("malloc struct lttng_consumer_stream");
602 if (trace_chunk
&& !lttng_trace_chunk_get(trace_chunk
)) {
603 ERR("Failed to acquire trace chunk reference during the creation of a stream");
609 stream
->key
= stream_key
;
610 stream
->trace_chunk
= trace_chunk
;
612 stream
->out_fd_offset
= 0;
613 stream
->output_written
= 0;
614 stream
->net_seq_idx
= relayd_id
;
615 stream
->session_id
= session_id
;
616 stream
->monitor
= monitor
;
617 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
618 stream
->index_file
= NULL
;
619 stream
->last_sequence_number
= -1ULL;
620 stream
->rotate_position
= -1ULL;
621 pthread_mutex_init(&stream
->lock
, NULL
);
622 pthread_mutex_init(&stream
->metadata_timer_lock
, NULL
);
624 /* If channel is the metadata, flag this stream as metadata. */
625 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
626 stream
->metadata_flag
= 1;
627 /* Metadata is flat out. */
628 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
629 /* Live rendez-vous point. */
630 pthread_cond_init(&stream
->metadata_rdv
, NULL
);
631 pthread_mutex_init(&stream
->metadata_rdv_lock
, NULL
);
633 /* Format stream name to <channel_name>_<cpu_number> */
634 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
637 PERROR("snprintf stream name");
642 /* Key is always the wait_fd for streams. */
643 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
645 /* Init node per channel id key */
646 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
648 /* Init session id node with the stream session id */
649 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
651 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
652 " relayd_id %" PRIu64
", session_id %" PRIu64
,
653 stream
->name
, stream
->key
, channel_key
,
654 stream
->net_seq_idx
, stream
->session_id
);
661 lttng_trace_chunk_put(stream
->trace_chunk
);
671 * Add a stream to the global list protected by a mutex.
673 void consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
675 struct lttng_ht
*ht
= data_ht
;
680 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
682 pthread_mutex_lock(&consumer_data
.lock
);
683 pthread_mutex_lock(&stream
->chan
->lock
);
684 pthread_mutex_lock(&stream
->chan
->timer_lock
);
685 pthread_mutex_lock(&stream
->lock
);
688 /* Steal stream identifier to avoid having streams with the same key */
689 steal_stream_key(stream
->key
, ht
);
691 lttng_ht_add_unique_u64(ht
, &stream
->node
);
693 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
694 &stream
->node_channel_id
);
697 * Add stream to the stream_list_ht of the consumer data. No need to steal
698 * the key since the HT does not use it and we allow to add redundant keys
701 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
704 * When nb_init_stream_left reaches 0, we don't need to trigger any action
705 * in terms of destroying the associated channel, because the action that
706 * causes the count to become 0 also causes a stream to be added. The
707 * channel deletion will thus be triggered by the following removal of this
710 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
711 /* Increment refcount before decrementing nb_init_stream_left */
713 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
716 /* Update consumer data once the node is inserted. */
717 consumer_data
.stream_count
++;
718 consumer_data
.need_update
= 1;
721 pthread_mutex_unlock(&stream
->lock
);
722 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
723 pthread_mutex_unlock(&stream
->chan
->lock
);
724 pthread_mutex_unlock(&consumer_data
.lock
);
728 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
729 * be acquired before calling this.
731 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
734 struct lttng_ht_node_u64
*node
;
735 struct lttng_ht_iter iter
;
739 lttng_ht_lookup(consumer_data
.relayd_ht
,
740 &relayd
->net_seq_idx
, &iter
);
741 node
= lttng_ht_iter_get_node_u64(&iter
);
745 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
752 * Allocate and return a consumer relayd socket.
754 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
755 uint64_t net_seq_idx
)
757 struct consumer_relayd_sock_pair
*obj
= NULL
;
759 /* net sequence index of -1 is a failure */
760 if (net_seq_idx
== (uint64_t) -1ULL) {
764 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
766 PERROR("zmalloc relayd sock");
770 obj
->net_seq_idx
= net_seq_idx
;
772 obj
->destroy_flag
= 0;
773 obj
->control_sock
.sock
.fd
= -1;
774 obj
->data_sock
.sock
.fd
= -1;
775 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
776 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
783 * Find a relayd socket pair in the global consumer data.
785 * Return the object if found else NULL.
786 * RCU read-side lock must be held across this call and while using the
789 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
791 struct lttng_ht_iter iter
;
792 struct lttng_ht_node_u64
*node
;
793 struct consumer_relayd_sock_pair
*relayd
= NULL
;
795 /* Negative keys are lookup failures */
796 if (key
== (uint64_t) -1ULL) {
800 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
802 node
= lttng_ht_iter_get_node_u64(&iter
);
804 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
812 * Find a relayd and send the stream
814 * Returns 0 on success, < 0 on error
816 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
820 struct consumer_relayd_sock_pair
*relayd
;
823 assert(stream
->net_seq_idx
!= -1ULL);
826 /* The stream is not metadata. Get relayd reference if exists. */
828 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
829 if (relayd
!= NULL
) {
830 /* Add stream on the relayd */
831 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
832 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
833 get_consumer_domain(), path
, &stream
->relayd_stream_id
,
834 stream
->chan
->tracefile_size
,
835 stream
->chan
->tracefile_count
,
836 stream
->trace_chunk
);
837 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
839 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
840 lttng_consumer_cleanup_relayd(relayd
);
844 uatomic_inc(&relayd
->refcount
);
845 stream
->sent_to_relayd
= 1;
847 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
848 stream
->key
, stream
->net_seq_idx
);
853 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
854 stream
->name
, stream
->key
, stream
->net_seq_idx
);
862 * Find a relayd and send the streams sent message
864 * Returns 0 on success, < 0 on error
866 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
869 struct consumer_relayd_sock_pair
*relayd
;
871 assert(net_seq_idx
!= -1ULL);
873 /* The stream is not metadata. Get relayd reference if exists. */
875 relayd
= consumer_find_relayd(net_seq_idx
);
876 if (relayd
!= NULL
) {
877 /* Add stream on the relayd */
878 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
879 ret
= relayd_streams_sent(&relayd
->control_sock
);
880 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
882 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
883 lttng_consumer_cleanup_relayd(relayd
);
887 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
894 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
902 * Find a relayd and close the stream
904 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
906 struct consumer_relayd_sock_pair
*relayd
;
908 /* The stream is not metadata. Get relayd reference if exists. */
910 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
912 consumer_stream_relayd_close(stream
, relayd
);
918 * Handle stream for relayd transmission if the stream applies for network
919 * streaming where the net sequence index is set.
921 * Return destination file descriptor or negative value on error.
923 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
924 size_t data_size
, unsigned long padding
,
925 struct consumer_relayd_sock_pair
*relayd
)
928 struct lttcomm_relayd_data_hdr data_hdr
;
934 /* Reset data header */
935 memset(&data_hdr
, 0, sizeof(data_hdr
));
937 if (stream
->metadata_flag
) {
938 /* Caller MUST acquire the relayd control socket lock */
939 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
944 /* Metadata are always sent on the control socket. */
945 outfd
= relayd
->control_sock
.sock
.fd
;
947 /* Set header with stream information */
948 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
949 data_hdr
.data_size
= htobe32(data_size
);
950 data_hdr
.padding_size
= htobe32(padding
);
953 * Note that net_seq_num below is assigned with the *current* value of
954 * next_net_seq_num and only after that the next_net_seq_num will be
955 * increment. This is why when issuing a command on the relayd using
956 * this next value, 1 should always be substracted in order to compare
957 * the last seen sequence number on the relayd side to the last sent.
959 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
960 /* Other fields are zeroed previously */
962 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
968 ++stream
->next_net_seq_num
;
970 /* Set to go on data socket */
971 outfd
= relayd
->data_sock
.sock
.fd
;
979 * Trigger a dump of the metadata content. Following/during the succesful
980 * completion of this call, the metadata poll thread will start receiving
981 * metadata packets to consume.
983 * The caller must hold the channel and stream locks.
986 int consumer_metadata_stream_dump(struct lttng_consumer_stream
*stream
)
990 ASSERT_LOCKED(stream
->chan
->lock
);
991 ASSERT_LOCKED(stream
->lock
);
992 assert(stream
->metadata_flag
);
993 assert(stream
->chan
->trace_chunk
);
995 switch (consumer_data
.type
) {
996 case LTTNG_CONSUMER_KERNEL
:
998 * Reset the position of what has been read from the
999 * metadata cache to 0 so we can dump it again.
1001 ret
= kernctl_metadata_cache_dump(stream
->wait_fd
);
1003 case LTTNG_CONSUMER32_UST
:
1004 case LTTNG_CONSUMER64_UST
:
1006 * Reset the position pushed from the metadata cache so it
1007 * will write from the beginning on the next push.
1009 stream
->ust_metadata_pushed
= 0;
1010 ret
= consumer_metadata_wakeup_pipe(stream
->chan
);
1013 ERR("Unknown consumer_data type");
1017 ERR("Failed to dump the metadata cache");
1023 int lttng_consumer_channel_set_trace_chunk(
1024 struct lttng_consumer_channel
*channel
,
1025 struct lttng_trace_chunk
*new_trace_chunk
)
1027 pthread_mutex_lock(&channel
->lock
);
1028 if (channel
->is_deleted
) {
1030 * The channel has been logically deleted and should no longer
1031 * be used. It has released its reference to its current trace
1032 * chunk and should not acquire a new one.
1034 * Return success as there is nothing for the caller to do.
1040 * The acquisition of the reference cannot fail (barring
1041 * a severe internal error) since a reference to the published
1042 * chunk is already held by the caller.
1044 if (new_trace_chunk
) {
1045 const bool acquired_reference
= lttng_trace_chunk_get(
1048 assert(acquired_reference
);
1051 lttng_trace_chunk_put(channel
->trace_chunk
);
1052 channel
->trace_chunk
= new_trace_chunk
;
1054 pthread_mutex_unlock(&channel
->lock
);
1059 * Allocate and return a new lttng_consumer_channel object using the given key
1060 * to initialize the hash table node.
1062 * On error, return NULL.
1064 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
1065 uint64_t session_id
,
1066 const uint64_t *chunk_id
,
1067 const char *pathname
,
1070 enum lttng_event_output output
,
1071 uint64_t tracefile_size
,
1072 uint64_t tracefile_count
,
1073 uint64_t session_id_per_pid
,
1074 unsigned int monitor
,
1075 unsigned int live_timer_interval
,
1076 const char *root_shm_path
,
1077 const char *shm_path
)
1079 struct lttng_consumer_channel
*channel
= NULL
;
1080 struct lttng_trace_chunk
*trace_chunk
= NULL
;
1083 trace_chunk
= lttng_trace_chunk_registry_find_chunk(
1084 consumer_data
.chunk_registry
, session_id
,
1087 ERR("Failed to find trace chunk reference during creation of channel");
1092 channel
= zmalloc(sizeof(*channel
));
1093 if (channel
== NULL
) {
1094 PERROR("malloc struct lttng_consumer_channel");
1099 channel
->refcount
= 0;
1100 channel
->session_id
= session_id
;
1101 channel
->session_id_per_pid
= session_id_per_pid
;
1102 channel
->relayd_id
= relayd_id
;
1103 channel
->tracefile_size
= tracefile_size
;
1104 channel
->tracefile_count
= tracefile_count
;
1105 channel
->monitor
= monitor
;
1106 channel
->live_timer_interval
= live_timer_interval
;
1107 pthread_mutex_init(&channel
->lock
, NULL
);
1108 pthread_mutex_init(&channel
->timer_lock
, NULL
);
1111 case LTTNG_EVENT_SPLICE
:
1112 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
1114 case LTTNG_EVENT_MMAP
:
1115 channel
->output
= CONSUMER_CHANNEL_MMAP
;
1125 * In monitor mode, the streams associated with the channel will be put in
1126 * a special list ONLY owned by this channel. So, the refcount is set to 1
1127 * here meaning that the channel itself has streams that are referenced.
1129 * On a channel deletion, once the channel is no longer visible, the
1130 * refcount is decremented and checked for a zero value to delete it. With
1131 * streams in no monitor mode, it will now be safe to destroy the channel.
1133 if (!channel
->monitor
) {
1134 channel
->refcount
= 1;
1137 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1138 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1140 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1141 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1143 if (root_shm_path
) {
1144 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1145 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1148 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1149 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1152 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1153 lttng_ht_node_init_u64(&channel
->channels_by_session_id_ht_node
,
1154 channel
->session_id
);
1156 channel
->wait_fd
= -1;
1157 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1160 int ret
= lttng_consumer_channel_set_trace_chunk(channel
,
1167 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1170 lttng_trace_chunk_put(trace_chunk
);
1173 consumer_del_channel(channel
);
1179 * Add a channel to the global list protected by a mutex.
1181 * Always return 0 indicating success.
1183 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1184 struct lttng_consumer_local_data
*ctx
)
1186 pthread_mutex_lock(&consumer_data
.lock
);
1187 pthread_mutex_lock(&channel
->lock
);
1188 pthread_mutex_lock(&channel
->timer_lock
);
1191 * This gives us a guarantee that the channel we are about to add to the
1192 * channel hash table will be unique. See this function comment on the why
1193 * we need to steel the channel key at this stage.
1195 steal_channel_key(channel
->key
);
1198 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1199 lttng_ht_add_u64(consumer_data
.channels_by_session_id_ht
,
1200 &channel
->channels_by_session_id_ht_node
);
1202 channel
->is_published
= true;
1204 pthread_mutex_unlock(&channel
->timer_lock
);
1205 pthread_mutex_unlock(&channel
->lock
);
1206 pthread_mutex_unlock(&consumer_data
.lock
);
1208 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1209 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1216 * Allocate the pollfd structure and the local view of the out fds to avoid
1217 * doing a lookup in the linked list and concurrency issues when writing is
1218 * needed. Called with consumer_data.lock held.
1220 * Returns the number of fds in the structures.
1222 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1223 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1224 struct lttng_ht
*ht
, int *nb_inactive_fd
)
1227 struct lttng_ht_iter iter
;
1228 struct lttng_consumer_stream
*stream
;
1233 assert(local_stream
);
1235 DBG("Updating poll fd array");
1236 *nb_inactive_fd
= 0;
1238 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1240 * Only active streams with an active end point can be added to the
1241 * poll set and local stream storage of the thread.
1243 * There is a potential race here for endpoint_status to be updated
1244 * just after the check. However, this is OK since the stream(s) will
1245 * be deleted once the thread is notified that the end point state has
1246 * changed where this function will be called back again.
1248 * We track the number of inactive FDs because they still need to be
1249 * closed by the polling thread after a wakeup on the data_pipe or
1252 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1253 (*nb_inactive_fd
)++;
1257 * This clobbers way too much the debug output. Uncomment that if you
1258 * need it for debugging purposes.
1260 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1261 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1262 local_stream
[i
] = stream
;
1268 * Insert the consumer_data_pipe at the end of the array and don't
1269 * increment i so nb_fd is the number of real FD.
1271 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1272 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1274 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1275 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1280 * Poll on the should_quit pipe and the command socket return -1 on
1281 * error, 1 if should exit, 0 if data is available on the command socket
1283 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1288 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1289 if (num_rdy
== -1) {
1291 * Restart interrupted system call.
1293 if (errno
== EINTR
) {
1296 PERROR("Poll error");
1299 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1300 DBG("consumer_should_quit wake up");
1307 * Set the error socket.
1309 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1312 ctx
->consumer_error_socket
= sock
;
1316 * Set the command socket path.
1318 void lttng_consumer_set_command_sock_path(
1319 struct lttng_consumer_local_data
*ctx
, char *sock
)
1321 ctx
->consumer_command_sock_path
= sock
;
1325 * Send return code to the session daemon.
1326 * If the socket is not defined, we return 0, it is not a fatal error
1328 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1330 if (ctx
->consumer_error_socket
> 0) {
1331 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1332 sizeof(enum lttcomm_sessiond_command
));
1339 * Close all the tracefiles and stream fds and MUST be called when all
1340 * instances are destroyed i.e. when all threads were joined and are ended.
1342 void lttng_consumer_cleanup(void)
1344 struct lttng_ht_iter iter
;
1345 struct lttng_consumer_channel
*channel
;
1346 unsigned int trace_chunks_left
;
1350 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1352 consumer_del_channel(channel
);
1357 lttng_ht_destroy(consumer_data
.channel_ht
);
1358 lttng_ht_destroy(consumer_data
.channels_by_session_id_ht
);
1360 cleanup_relayd_ht();
1362 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1365 * This HT contains streams that are freed by either the metadata thread or
1366 * the data thread so we do *nothing* on the hash table and simply destroy
1369 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1372 * Trace chunks in the registry may still exist if the session
1373 * daemon has encountered an internal error and could not
1374 * tear down its sessions and/or trace chunks properly.
1376 * Release the session daemon's implicit reference to any remaining
1377 * trace chunk and print an error if any trace chunk was found. Note
1378 * that there are _no_ legitimate cases for trace chunks to be left,
1379 * it is a leak. However, it can happen following a crash of the
1380 * session daemon and not emptying the registry would cause an assertion
1383 trace_chunks_left
= lttng_trace_chunk_registry_put_each_chunk(
1384 consumer_data
.chunk_registry
);
1385 if (trace_chunks_left
) {
1386 ERR("%u trace chunks are leaked by lttng-consumerd. "
1387 "This can be caused by an internal error of the session daemon.",
1390 /* Run all callbacks freeing each chunk. */
1392 lttng_trace_chunk_registry_destroy(consumer_data
.chunk_registry
);
1396 * Called from signal handler.
1398 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1402 CMM_STORE_SHARED(consumer_quit
, 1);
1403 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1405 PERROR("write consumer quit");
1408 DBG("Consumer flag that it should quit");
1413 * Flush pending writes to trace output disk file.
1416 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1420 int outfd
= stream
->out_fd
;
1423 * This does a blocking write-and-wait on any page that belongs to the
1424 * subbuffer prior to the one we just wrote.
1425 * Don't care about error values, as these are just hints and ways to
1426 * limit the amount of page cache used.
1428 if (orig_offset
< stream
->max_sb_size
) {
1431 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1432 stream
->max_sb_size
,
1433 SYNC_FILE_RANGE_WAIT_BEFORE
1434 | SYNC_FILE_RANGE_WRITE
1435 | SYNC_FILE_RANGE_WAIT_AFTER
);
1437 * Give hints to the kernel about how we access the file:
1438 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1441 * We need to call fadvise again after the file grows because the
1442 * kernel does not seem to apply fadvise to non-existing parts of the
1445 * Call fadvise _after_ having waited for the page writeback to
1446 * complete because the dirty page writeback semantic is not well
1447 * defined. So it can be expected to lead to lower throughput in
1450 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1451 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1452 if (ret
&& ret
!= -ENOSYS
) {
1454 PERROR("posix_fadvise on fd %i", outfd
);
1459 * Initialise the necessary environnement :
1460 * - create a new context
1461 * - create the poll_pipe
1462 * - create the should_quit pipe (for signal handler)
1463 * - create the thread pipe (for splice)
1465 * Takes a function pointer as argument, this function is called when data is
1466 * available on a buffer. This function is responsible to do the
1467 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1468 * buffer configuration and then kernctl_put_next_subbuf at the end.
1470 * Returns a pointer to the new context or NULL on error.
1472 struct lttng_consumer_local_data
*lttng_consumer_create(
1473 enum lttng_consumer_type type
,
1474 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1475 struct lttng_consumer_local_data
*ctx
),
1476 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1477 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1478 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1481 struct lttng_consumer_local_data
*ctx
;
1483 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1484 consumer_data
.type
== type
);
1485 consumer_data
.type
= type
;
1487 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1489 PERROR("allocating context");
1493 ctx
->consumer_error_socket
= -1;
1494 ctx
->consumer_metadata_socket
= -1;
1495 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1496 /* assign the callbacks */
1497 ctx
->on_buffer_ready
= buffer_ready
;
1498 ctx
->on_recv_channel
= recv_channel
;
1499 ctx
->on_recv_stream
= recv_stream
;
1500 ctx
->on_update_stream
= update_stream
;
1502 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1503 if (!ctx
->consumer_data_pipe
) {
1504 goto error_poll_pipe
;
1507 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1508 if (!ctx
->consumer_wakeup_pipe
) {
1509 goto error_wakeup_pipe
;
1512 ret
= pipe(ctx
->consumer_should_quit
);
1514 PERROR("Error creating recv pipe");
1515 goto error_quit_pipe
;
1518 ret
= pipe(ctx
->consumer_channel_pipe
);
1520 PERROR("Error creating channel pipe");
1521 goto error_channel_pipe
;
1524 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1525 if (!ctx
->consumer_metadata_pipe
) {
1526 goto error_metadata_pipe
;
1529 ctx
->channel_monitor_pipe
= -1;
1533 error_metadata_pipe
:
1534 utils_close_pipe(ctx
->consumer_channel_pipe
);
1536 utils_close_pipe(ctx
->consumer_should_quit
);
1538 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1540 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1548 * Iterate over all streams of the hashtable and free them properly.
1550 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1552 struct lttng_ht_iter iter
;
1553 struct lttng_consumer_stream
*stream
;
1560 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1562 * Ignore return value since we are currently cleaning up so any error
1565 (void) consumer_del_stream(stream
, ht
);
1569 lttng_ht_destroy(ht
);
1573 * Iterate over all streams of the metadata hashtable and free them
1576 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1578 struct lttng_ht_iter iter
;
1579 struct lttng_consumer_stream
*stream
;
1586 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1588 * Ignore return value since we are currently cleaning up so any error
1591 (void) consumer_del_metadata_stream(stream
, ht
);
1595 lttng_ht_destroy(ht
);
1599 * Close all fds associated with the instance and free the context.
1601 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1605 DBG("Consumer destroying it. Closing everything.");
1611 destroy_data_stream_ht(data_ht
);
1612 destroy_metadata_stream_ht(metadata_ht
);
1614 ret
= close(ctx
->consumer_error_socket
);
1618 ret
= close(ctx
->consumer_metadata_socket
);
1622 utils_close_pipe(ctx
->consumer_channel_pipe
);
1623 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1624 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1625 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1626 utils_close_pipe(ctx
->consumer_should_quit
);
1628 unlink(ctx
->consumer_command_sock_path
);
1633 * Write the metadata stream id on the specified file descriptor.
1635 static int write_relayd_metadata_id(int fd
,
1636 struct lttng_consumer_stream
*stream
,
1637 unsigned long padding
)
1640 struct lttcomm_relayd_metadata_payload hdr
;
1642 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1643 hdr
.padding_size
= htobe32(padding
);
1644 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1645 if (ret
< sizeof(hdr
)) {
1647 * This error means that the fd's end is closed so ignore the PERROR
1648 * not to clubber the error output since this can happen in a normal
1651 if (errno
!= EPIPE
) {
1652 PERROR("write metadata stream id");
1654 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1656 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1657 * handle writting the missing part so report that as an error and
1658 * don't lie to the caller.
1663 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1664 stream
->relayd_stream_id
, padding
);
1671 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1672 * core function for writing trace buffers to either the local filesystem or
1675 * It must be called with the stream and the channel lock held.
1677 * Careful review MUST be put if any changes occur!
1679 * Returns the number of bytes written
1681 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1682 struct lttng_consumer_local_data
*ctx
,
1683 struct lttng_consumer_stream
*stream
, unsigned long len
,
1684 unsigned long padding
,
1685 struct ctf_packet_index
*index
)
1687 unsigned long mmap_offset
;
1690 off_t orig_offset
= stream
->out_fd_offset
;
1691 /* Default is on the disk */
1692 int outfd
= stream
->out_fd
;
1693 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1694 unsigned int relayd_hang_up
= 0;
1696 /* RCU lock for the relayd pointer */
1698 assert(stream
->net_seq_idx
!= (uint64_t) -1ULL ||
1699 stream
->trace_chunk
);
1701 /* Flag that the current stream if set for network streaming. */
1702 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1703 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1704 if (relayd
== NULL
) {
1710 /* get the offset inside the fd to mmap */
1711 switch (consumer_data
.type
) {
1712 case LTTNG_CONSUMER_KERNEL
:
1713 mmap_base
= stream
->mmap_base
;
1714 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1716 PERROR("tracer ctl get_mmap_read_offset");
1720 case LTTNG_CONSUMER32_UST
:
1721 case LTTNG_CONSUMER64_UST
:
1722 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1724 ERR("read mmap get mmap base for stream %s", stream
->name
);
1728 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1730 PERROR("tracer ctl get_mmap_read_offset");
1736 ERR("Unknown consumer_data type");
1740 /* Handle stream on the relayd if the output is on the network */
1742 unsigned long netlen
= len
;
1745 * Lock the control socket for the complete duration of the function
1746 * since from this point on we will use the socket.
1748 if (stream
->metadata_flag
) {
1749 /* Metadata requires the control socket. */
1750 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1751 if (stream
->reset_metadata_flag
) {
1752 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1753 stream
->relayd_stream_id
,
1754 stream
->metadata_version
);
1759 stream
->reset_metadata_flag
= 0;
1761 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1764 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1769 /* Use the returned socket. */
1772 /* Write metadata stream id before payload */
1773 if (stream
->metadata_flag
) {
1774 ret
= write_relayd_metadata_id(outfd
, stream
, padding
);
1781 /* No streaming, we have to set the len with the full padding */
1784 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1785 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1787 ERR("Reset metadata file");
1790 stream
->reset_metadata_flag
= 0;
1794 * Check if we need to change the tracefile before writing the packet.
1796 if (stream
->chan
->tracefile_size
> 0 &&
1797 (stream
->tracefile_size_current
+ len
) >
1798 stream
->chan
->tracefile_size
) {
1799 ret
= consumer_stream_rotate_output_files(stream
);
1803 outfd
= stream
->out_fd
;
1806 stream
->tracefile_size_current
+= len
;
1808 index
->offset
= htobe64(stream
->out_fd_offset
);
1813 * This call guarantee that len or less is returned. It's impossible to
1814 * receive a ret value that is bigger than len.
1816 ret
= lttng_write(outfd
, mmap_base
+ mmap_offset
, len
);
1817 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1818 if (ret
< 0 || ((size_t) ret
!= len
)) {
1820 * Report error to caller if nothing was written else at least send the
1828 /* Socket operation failed. We consider the relayd dead */
1829 if (errno
== EPIPE
) {
1831 * This is possible if the fd is closed on the other side
1832 * (outfd) or any write problem. It can be verbose a bit for a
1833 * normal execution if for instance the relayd is stopped
1834 * abruptly. This can happen so set this to a DBG statement.
1836 DBG("Consumer mmap write detected relayd hang up");
1838 /* Unhandled error, print it and stop function right now. */
1839 PERROR("Error in write mmap (ret %zd != len %lu)", ret
, len
);
1843 stream
->output_written
+= ret
;
1845 /* This call is useless on a socket so better save a syscall. */
1847 /* This won't block, but will start writeout asynchronously */
1848 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, len
,
1849 SYNC_FILE_RANGE_WRITE
);
1850 stream
->out_fd_offset
+= len
;
1851 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1856 * This is a special case that the relayd has closed its socket. Let's
1857 * cleanup the relayd object and all associated streams.
1859 if (relayd
&& relayd_hang_up
) {
1860 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1861 lttng_consumer_cleanup_relayd(relayd
);
1865 /* Unlock only if ctrl socket used */
1866 if (relayd
&& stream
->metadata_flag
) {
1867 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1875 * Splice the data from the ring buffer to the tracefile.
1877 * It must be called with the stream lock held.
1879 * Returns the number of bytes spliced.
1881 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1882 struct lttng_consumer_local_data
*ctx
,
1883 struct lttng_consumer_stream
*stream
, unsigned long len
,
1884 unsigned long padding
,
1885 struct ctf_packet_index
*index
)
1887 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1889 off_t orig_offset
= stream
->out_fd_offset
;
1890 int fd
= stream
->wait_fd
;
1891 /* Default is on the disk */
1892 int outfd
= stream
->out_fd
;
1893 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1895 unsigned int relayd_hang_up
= 0;
1897 switch (consumer_data
.type
) {
1898 case LTTNG_CONSUMER_KERNEL
:
1900 case LTTNG_CONSUMER32_UST
:
1901 case LTTNG_CONSUMER64_UST
:
1902 /* Not supported for user space tracing */
1905 ERR("Unknown consumer_data type");
1909 /* RCU lock for the relayd pointer */
1912 /* Flag that the current stream if set for network streaming. */
1913 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1914 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1915 if (relayd
== NULL
) {
1920 splice_pipe
= stream
->splice_pipe
;
1922 /* Write metadata stream id before payload */
1924 unsigned long total_len
= len
;
1926 if (stream
->metadata_flag
) {
1928 * Lock the control socket for the complete duration of the function
1929 * since from this point on we will use the socket.
1931 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1933 if (stream
->reset_metadata_flag
) {
1934 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1935 stream
->relayd_stream_id
,
1936 stream
->metadata_version
);
1941 stream
->reset_metadata_flag
= 0;
1943 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
,
1951 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1954 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1960 /* Use the returned socket. */
1963 /* No streaming, we have to set the len with the full padding */
1966 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1967 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1969 ERR("Reset metadata file");
1972 stream
->reset_metadata_flag
= 0;
1975 * Check if we need to change the tracefile before writing the packet.
1977 if (stream
->chan
->tracefile_size
> 0 &&
1978 (stream
->tracefile_size_current
+ len
) >
1979 stream
->chan
->tracefile_size
) {
1980 ret
= consumer_stream_rotate_output_files(stream
);
1985 outfd
= stream
->out_fd
;
1988 stream
->tracefile_size_current
+= len
;
1989 index
->offset
= htobe64(stream
->out_fd_offset
);
1993 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1994 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1995 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1996 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1997 DBG("splice chan to pipe, ret %zd", ret_splice
);
1998 if (ret_splice
< 0) {
2001 PERROR("Error in relay splice");
2005 /* Handle stream on the relayd if the output is on the network */
2006 if (relayd
&& stream
->metadata_flag
) {
2007 size_t metadata_payload_size
=
2008 sizeof(struct lttcomm_relayd_metadata_payload
);
2010 /* Update counter to fit the spliced data */
2011 ret_splice
+= metadata_payload_size
;
2012 len
+= metadata_payload_size
;
2014 * We do this so the return value can match the len passed as
2015 * argument to this function.
2017 written
-= metadata_payload_size
;
2020 /* Splice data out */
2021 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
2022 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
2023 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
2025 if (ret_splice
< 0) {
2030 } else if (ret_splice
> len
) {
2032 * We don't expect this code path to be executed but you never know
2033 * so this is an extra protection agains a buggy splice().
2036 written
+= ret_splice
;
2037 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
2041 /* All good, update current len and continue. */
2045 /* This call is useless on a socket so better save a syscall. */
2047 /* This won't block, but will start writeout asynchronously */
2048 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
2049 SYNC_FILE_RANGE_WRITE
);
2050 stream
->out_fd_offset
+= ret_splice
;
2052 stream
->output_written
+= ret_splice
;
2053 written
+= ret_splice
;
2056 lttng_consumer_sync_trace_file(stream
, orig_offset
);
2062 * This is a special case that the relayd has closed its socket. Let's
2063 * cleanup the relayd object and all associated streams.
2065 if (relayd
&& relayd_hang_up
) {
2066 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
2067 lttng_consumer_cleanup_relayd(relayd
);
2068 /* Skip splice error so the consumer does not fail */
2073 /* send the appropriate error description to sessiond */
2076 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
2079 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
2082 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
2087 if (relayd
&& stream
->metadata_flag
) {
2088 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2096 * Sample the snapshot positions for a specific fd
2098 * Returns 0 on success, < 0 on error
2100 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
2102 switch (consumer_data
.type
) {
2103 case LTTNG_CONSUMER_KERNEL
:
2104 return lttng_kconsumer_sample_snapshot_positions(stream
);
2105 case LTTNG_CONSUMER32_UST
:
2106 case LTTNG_CONSUMER64_UST
:
2107 return lttng_ustconsumer_sample_snapshot_positions(stream
);
2109 ERR("Unknown consumer_data type");
2115 * Take a snapshot for a specific fd
2117 * Returns 0 on success, < 0 on error
2119 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2121 switch (consumer_data
.type
) {
2122 case LTTNG_CONSUMER_KERNEL
:
2123 return lttng_kconsumer_take_snapshot(stream
);
2124 case LTTNG_CONSUMER32_UST
:
2125 case LTTNG_CONSUMER64_UST
:
2126 return lttng_ustconsumer_take_snapshot(stream
);
2128 ERR("Unknown consumer_data type");
2135 * Get the produced position
2137 * Returns 0 on success, < 0 on error
2139 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
2142 switch (consumer_data
.type
) {
2143 case LTTNG_CONSUMER_KERNEL
:
2144 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
2145 case LTTNG_CONSUMER32_UST
:
2146 case LTTNG_CONSUMER64_UST
:
2147 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2149 ERR("Unknown consumer_data type");
2156 * Get the consumed position (free-running counter position in bytes).
2158 * Returns 0 on success, < 0 on error
2160 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
2163 switch (consumer_data
.type
) {
2164 case LTTNG_CONSUMER_KERNEL
:
2165 return lttng_kconsumer_get_consumed_snapshot(stream
, pos
);
2166 case LTTNG_CONSUMER32_UST
:
2167 case LTTNG_CONSUMER64_UST
:
2168 return lttng_ustconsumer_get_consumed_snapshot(stream
, pos
);
2170 ERR("Unknown consumer_data type");
2176 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2177 int sock
, struct pollfd
*consumer_sockpoll
)
2179 switch (consumer_data
.type
) {
2180 case LTTNG_CONSUMER_KERNEL
:
2181 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2182 case LTTNG_CONSUMER32_UST
:
2183 case LTTNG_CONSUMER64_UST
:
2184 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2186 ERR("Unknown consumer_data type");
2193 void lttng_consumer_close_all_metadata(void)
2195 switch (consumer_data
.type
) {
2196 case LTTNG_CONSUMER_KERNEL
:
2198 * The Kernel consumer has a different metadata scheme so we don't
2199 * close anything because the stream will be closed by the session
2203 case LTTNG_CONSUMER32_UST
:
2204 case LTTNG_CONSUMER64_UST
:
2206 * Close all metadata streams. The metadata hash table is passed and
2207 * this call iterates over it by closing all wakeup fd. This is safe
2208 * because at this point we are sure that the metadata producer is
2209 * either dead or blocked.
2211 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2214 ERR("Unknown consumer_data type");
2220 * Clean up a metadata stream and free its memory.
2222 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2223 struct lttng_ht
*ht
)
2225 struct lttng_consumer_channel
*channel
= NULL
;
2226 bool free_channel
= false;
2230 * This call should NEVER receive regular stream. It must always be
2231 * metadata stream and this is crucial for data structure synchronization.
2233 assert(stream
->metadata_flag
);
2235 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2237 pthread_mutex_lock(&consumer_data
.lock
);
2239 * Note that this assumes that a stream's channel is never changed and
2240 * that the stream's lock doesn't need to be taken to sample its
2243 channel
= stream
->chan
;
2244 pthread_mutex_lock(&channel
->lock
);
2245 pthread_mutex_lock(&stream
->lock
);
2246 if (channel
->metadata_cache
) {
2247 /* Only applicable to userspace consumers. */
2248 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
2251 /* Remove any reference to that stream. */
2252 consumer_stream_delete(stream
, ht
);
2254 /* Close down everything including the relayd if one. */
2255 consumer_stream_close(stream
);
2256 /* Destroy tracer buffers of the stream. */
2257 consumer_stream_destroy_buffers(stream
);
2259 /* Atomically decrement channel refcount since other threads can use it. */
2260 if (!uatomic_sub_return(&channel
->refcount
, 1)
2261 && !uatomic_read(&channel
->nb_init_stream_left
)) {
2262 /* Go for channel deletion! */
2263 free_channel
= true;
2265 stream
->chan
= NULL
;
2268 * Nullify the stream reference so it is not used after deletion. The
2269 * channel lock MUST be acquired before being able to check for a NULL
2272 channel
->metadata_stream
= NULL
;
2274 if (channel
->metadata_cache
) {
2275 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
2277 pthread_mutex_unlock(&stream
->lock
);
2278 pthread_mutex_unlock(&channel
->lock
);
2279 pthread_mutex_unlock(&consumer_data
.lock
);
2282 consumer_del_channel(channel
);
2285 lttng_trace_chunk_put(stream
->trace_chunk
);
2286 stream
->trace_chunk
= NULL
;
2287 consumer_stream_free(stream
);
2291 * Action done with the metadata stream when adding it to the consumer internal
2292 * data structures to handle it.
2294 void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2296 struct lttng_ht
*ht
= metadata_ht
;
2297 struct lttng_ht_iter iter
;
2298 struct lttng_ht_node_u64
*node
;
2303 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2305 pthread_mutex_lock(&consumer_data
.lock
);
2306 pthread_mutex_lock(&stream
->chan
->lock
);
2307 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2308 pthread_mutex_lock(&stream
->lock
);
2311 * From here, refcounts are updated so be _careful_ when returning an error
2318 * Lookup the stream just to make sure it does not exist in our internal
2319 * state. This should NEVER happen.
2321 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2322 node
= lttng_ht_iter_get_node_u64(&iter
);
2326 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2327 * in terms of destroying the associated channel, because the action that
2328 * causes the count to become 0 also causes a stream to be added. The
2329 * channel deletion will thus be triggered by the following removal of this
2332 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2333 /* Increment refcount before decrementing nb_init_stream_left */
2335 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2338 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2340 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
2341 &stream
->node_channel_id
);
2344 * Add stream to the stream_list_ht of the consumer data. No need to steal
2345 * the key since the HT does not use it and we allow to add redundant keys
2348 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2352 pthread_mutex_unlock(&stream
->lock
);
2353 pthread_mutex_unlock(&stream
->chan
->lock
);
2354 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2355 pthread_mutex_unlock(&consumer_data
.lock
);
2359 * Delete data stream that are flagged for deletion (endpoint_status).
2361 static void validate_endpoint_status_data_stream(void)
2363 struct lttng_ht_iter iter
;
2364 struct lttng_consumer_stream
*stream
;
2366 DBG("Consumer delete flagged data stream");
2369 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2370 /* Validate delete flag of the stream */
2371 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2374 /* Delete it right now */
2375 consumer_del_stream(stream
, data_ht
);
2381 * Delete metadata stream that are flagged for deletion (endpoint_status).
2383 static void validate_endpoint_status_metadata_stream(
2384 struct lttng_poll_event
*pollset
)
2386 struct lttng_ht_iter iter
;
2387 struct lttng_consumer_stream
*stream
;
2389 DBG("Consumer delete flagged metadata stream");
2394 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2395 /* Validate delete flag of the stream */
2396 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2400 * Remove from pollset so the metadata thread can continue without
2401 * blocking on a deleted stream.
2403 lttng_poll_del(pollset
, stream
->wait_fd
);
2405 /* Delete it right now */
2406 consumer_del_metadata_stream(stream
, metadata_ht
);
2412 * Thread polls on metadata file descriptor and write them on disk or on the
2415 void *consumer_thread_metadata_poll(void *data
)
2417 int ret
, i
, pollfd
, err
= -1;
2418 uint32_t revents
, nb_fd
;
2419 struct lttng_consumer_stream
*stream
= NULL
;
2420 struct lttng_ht_iter iter
;
2421 struct lttng_ht_node_u64
*node
;
2422 struct lttng_poll_event events
;
2423 struct lttng_consumer_local_data
*ctx
= data
;
2426 rcu_register_thread();
2428 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2430 if (testpoint(consumerd_thread_metadata
)) {
2431 goto error_testpoint
;
2434 health_code_update();
2436 DBG("Thread metadata poll started");
2438 /* Size is set to 1 for the consumer_metadata pipe */
2439 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2441 ERR("Poll set creation failed");
2445 ret
= lttng_poll_add(&events
,
2446 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2452 DBG("Metadata main loop started");
2456 health_code_update();
2457 health_poll_entry();
2458 DBG("Metadata poll wait");
2459 ret
= lttng_poll_wait(&events
, -1);
2460 DBG("Metadata poll return from wait with %d fd(s)",
2461 LTTNG_POLL_GETNB(&events
));
2463 DBG("Metadata event caught in thread");
2465 if (errno
== EINTR
) {
2466 ERR("Poll EINTR caught");
2469 if (LTTNG_POLL_GETNB(&events
) == 0) {
2470 err
= 0; /* All is OK */
2477 /* From here, the event is a metadata wait fd */
2478 for (i
= 0; i
< nb_fd
; i
++) {
2479 health_code_update();
2481 revents
= LTTNG_POLL_GETEV(&events
, i
);
2482 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2484 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2485 if (revents
& LPOLLIN
) {
2488 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2489 &stream
, sizeof(stream
));
2490 if (pipe_len
< sizeof(stream
)) {
2492 PERROR("read metadata stream");
2495 * Remove the pipe from the poll set and continue the loop
2496 * since their might be data to consume.
2498 lttng_poll_del(&events
,
2499 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2500 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2504 /* A NULL stream means that the state has changed. */
2505 if (stream
== NULL
) {
2506 /* Check for deleted streams. */
2507 validate_endpoint_status_metadata_stream(&events
);
2511 DBG("Adding metadata stream %d to poll set",
2514 /* Add metadata stream to the global poll events list */
2515 lttng_poll_add(&events
, stream
->wait_fd
,
2516 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2517 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2518 DBG("Metadata thread pipe hung up");
2520 * Remove the pipe from the poll set and continue the loop
2521 * since their might be data to consume.
2523 lttng_poll_del(&events
,
2524 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2525 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2528 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2532 /* Handle other stream */
2538 uint64_t tmp_id
= (uint64_t) pollfd
;
2540 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2542 node
= lttng_ht_iter_get_node_u64(&iter
);
2545 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2548 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2549 /* Get the data out of the metadata file descriptor */
2550 DBG("Metadata available on fd %d", pollfd
);
2551 assert(stream
->wait_fd
== pollfd
);
2554 health_code_update();
2556 len
= ctx
->on_buffer_ready(stream
, ctx
);
2558 * We don't check the return value here since if we get
2559 * a negative len, it means an error occurred thus we
2560 * simply remove it from the poll set and free the
2565 /* It's ok to have an unavailable sub-buffer */
2566 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2567 /* Clean up stream from consumer and free it. */
2568 lttng_poll_del(&events
, stream
->wait_fd
);
2569 consumer_del_metadata_stream(stream
, metadata_ht
);
2571 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2572 DBG("Metadata fd %d is hup|err.", pollfd
);
2573 if (!stream
->hangup_flush_done
2574 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2575 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2576 DBG("Attempting to flush and consume the UST buffers");
2577 lttng_ustconsumer_on_stream_hangup(stream
);
2579 /* We just flushed the stream now read it. */
2581 health_code_update();
2583 len
= ctx
->on_buffer_ready(stream
, ctx
);
2585 * We don't check the return value here since if we get
2586 * a negative len, it means an error occurred thus we
2587 * simply remove it from the poll set and free the
2593 lttng_poll_del(&events
, stream
->wait_fd
);
2595 * This call update the channel states, closes file descriptors
2596 * and securely free the stream.
2598 consumer_del_metadata_stream(stream
, metadata_ht
);
2600 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2604 /* Release RCU lock for the stream looked up */
2612 DBG("Metadata poll thread exiting");
2614 lttng_poll_clean(&events
);
2619 ERR("Health error occurred in %s", __func__
);
2621 health_unregister(health_consumerd
);
2622 rcu_unregister_thread();
2627 * This thread polls the fds in the set to consume the data and write
2628 * it to tracefile if necessary.
2630 void *consumer_thread_data_poll(void *data
)
2632 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2633 struct pollfd
*pollfd
= NULL
;
2634 /* local view of the streams */
2635 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2636 /* local view of consumer_data.fds_count */
2638 /* 2 for the consumer_data_pipe and wake up pipe */
2639 const int nb_pipes_fd
= 2;
2640 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2641 int nb_inactive_fd
= 0;
2642 struct lttng_consumer_local_data
*ctx
= data
;
2645 rcu_register_thread();
2647 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2649 if (testpoint(consumerd_thread_data
)) {
2650 goto error_testpoint
;
2653 health_code_update();
2655 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2656 if (local_stream
== NULL
) {
2657 PERROR("local_stream malloc");
2662 health_code_update();
2668 * the fds set has been updated, we need to update our
2669 * local array as well
2671 pthread_mutex_lock(&consumer_data
.lock
);
2672 if (consumer_data
.need_update
) {
2677 local_stream
= NULL
;
2679 /* Allocate for all fds */
2680 pollfd
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) * sizeof(struct pollfd
));
2681 if (pollfd
== NULL
) {
2682 PERROR("pollfd malloc");
2683 pthread_mutex_unlock(&consumer_data
.lock
);
2687 local_stream
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) *
2688 sizeof(struct lttng_consumer_stream
*));
2689 if (local_stream
== NULL
) {
2690 PERROR("local_stream malloc");
2691 pthread_mutex_unlock(&consumer_data
.lock
);
2694 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2695 data_ht
, &nb_inactive_fd
);
2697 ERR("Error in allocating pollfd or local_outfds");
2698 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2699 pthread_mutex_unlock(&consumer_data
.lock
);
2703 consumer_data
.need_update
= 0;
2705 pthread_mutex_unlock(&consumer_data
.lock
);
2707 /* No FDs and consumer_quit, consumer_cleanup the thread */
2708 if (nb_fd
== 0 && nb_inactive_fd
== 0 &&
2709 CMM_LOAD_SHARED(consumer_quit
) == 1) {
2710 err
= 0; /* All is OK */
2713 /* poll on the array of fds */
2715 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2716 if (testpoint(consumerd_thread_data_poll
)) {
2719 health_poll_entry();
2720 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2722 DBG("poll num_rdy : %d", num_rdy
);
2723 if (num_rdy
== -1) {
2725 * Restart interrupted system call.
2727 if (errno
== EINTR
) {
2730 PERROR("Poll error");
2731 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2733 } else if (num_rdy
== 0) {
2734 DBG("Polling thread timed out");
2738 if (caa_unlikely(data_consumption_paused
)) {
2739 DBG("Data consumption paused, sleeping...");
2745 * If the consumer_data_pipe triggered poll go directly to the
2746 * beginning of the loop to update the array. We want to prioritize
2747 * array update over low-priority reads.
2749 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2750 ssize_t pipe_readlen
;
2752 DBG("consumer_data_pipe wake up");
2753 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2754 &new_stream
, sizeof(new_stream
));
2755 if (pipe_readlen
< sizeof(new_stream
)) {
2756 PERROR("Consumer data pipe");
2757 /* Continue so we can at least handle the current stream(s). */
2762 * If the stream is NULL, just ignore it. It's also possible that
2763 * the sessiond poll thread changed the consumer_quit state and is
2764 * waking us up to test it.
2766 if (new_stream
== NULL
) {
2767 validate_endpoint_status_data_stream();
2771 /* Continue to update the local streams and handle prio ones */
2775 /* Handle wakeup pipe. */
2776 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2778 ssize_t pipe_readlen
;
2780 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2782 if (pipe_readlen
< 0) {
2783 PERROR("Consumer data wakeup pipe");
2785 /* We've been awakened to handle stream(s). */
2786 ctx
->has_wakeup
= 0;
2789 /* Take care of high priority channels first. */
2790 for (i
= 0; i
< nb_fd
; i
++) {
2791 health_code_update();
2793 if (local_stream
[i
] == NULL
) {
2796 if (pollfd
[i
].revents
& POLLPRI
) {
2797 DBG("Urgent 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;
2812 * If we read high prio channel in this loop, try again
2813 * for more high prio data.
2819 /* Take care of low priority channels. */
2820 for (i
= 0; i
< nb_fd
; i
++) {
2821 health_code_update();
2823 if (local_stream
[i
] == NULL
) {
2826 if ((pollfd
[i
].revents
& POLLIN
) ||
2827 local_stream
[i
]->hangup_flush_done
||
2828 local_stream
[i
]->has_data
) {
2829 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2830 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2831 /* it's ok to have an unavailable sub-buffer */
2832 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2833 /* Clean the stream and free it. */
2834 consumer_del_stream(local_stream
[i
], data_ht
);
2835 local_stream
[i
] = NULL
;
2836 } else if (len
> 0) {
2837 local_stream
[i
]->data_read
= 1;
2842 /* Handle hangup and errors */
2843 for (i
= 0; i
< nb_fd
; i
++) {
2844 health_code_update();
2846 if (local_stream
[i
] == NULL
) {
2849 if (!local_stream
[i
]->hangup_flush_done
2850 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2851 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2852 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2853 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2855 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2856 /* Attempt read again, for the data we just flushed. */
2857 local_stream
[i
]->data_read
= 1;
2860 * If the poll flag is HUP/ERR/NVAL and we have
2861 * read no data in this pass, we can remove the
2862 * stream from its hash table.
2864 if ((pollfd
[i
].revents
& POLLHUP
)) {
2865 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2866 if (!local_stream
[i
]->data_read
) {
2867 consumer_del_stream(local_stream
[i
], data_ht
);
2868 local_stream
[i
] = NULL
;
2871 } else if (pollfd
[i
].revents
& POLLERR
) {
2872 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2873 if (!local_stream
[i
]->data_read
) {
2874 consumer_del_stream(local_stream
[i
], data_ht
);
2875 local_stream
[i
] = NULL
;
2878 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2879 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2880 if (!local_stream
[i
]->data_read
) {
2881 consumer_del_stream(local_stream
[i
], data_ht
);
2882 local_stream
[i
] = NULL
;
2886 if (local_stream
[i
] != NULL
) {
2887 local_stream
[i
]->data_read
= 0;
2894 DBG("polling thread exiting");
2899 * Close the write side of the pipe so epoll_wait() in
2900 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2901 * read side of the pipe. If we close them both, epoll_wait strangely does
2902 * not return and could create a endless wait period if the pipe is the
2903 * only tracked fd in the poll set. The thread will take care of closing
2906 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2911 ERR("Health error occurred in %s", __func__
);
2913 health_unregister(health_consumerd
);
2915 rcu_unregister_thread();
2920 * Close wake-up end of each stream belonging to the channel. This will
2921 * allow the poll() on the stream read-side to detect when the
2922 * write-side (application) finally closes them.
2925 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2927 struct lttng_ht
*ht
;
2928 struct lttng_consumer_stream
*stream
;
2929 struct lttng_ht_iter iter
;
2931 ht
= consumer_data
.stream_per_chan_id_ht
;
2934 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2935 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2936 ht
->match_fct
, &channel
->key
,
2937 &iter
.iter
, stream
, node_channel_id
.node
) {
2939 * Protect against teardown with mutex.
2941 pthread_mutex_lock(&stream
->lock
);
2942 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2945 switch (consumer_data
.type
) {
2946 case LTTNG_CONSUMER_KERNEL
:
2948 case LTTNG_CONSUMER32_UST
:
2949 case LTTNG_CONSUMER64_UST
:
2950 if (stream
->metadata_flag
) {
2951 /* Safe and protected by the stream lock. */
2952 lttng_ustconsumer_close_metadata(stream
->chan
);
2955 * Note: a mutex is taken internally within
2956 * liblttng-ust-ctl to protect timer wakeup_fd
2957 * use from concurrent close.
2959 lttng_ustconsumer_close_stream_wakeup(stream
);
2963 ERR("Unknown consumer_data type");
2967 pthread_mutex_unlock(&stream
->lock
);
2972 static void destroy_channel_ht(struct lttng_ht
*ht
)
2974 struct lttng_ht_iter iter
;
2975 struct lttng_consumer_channel
*channel
;
2983 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2984 ret
= lttng_ht_del(ht
, &iter
);
2989 lttng_ht_destroy(ht
);
2993 * This thread polls the channel fds to detect when they are being
2994 * closed. It closes all related streams if the channel is detected as
2995 * closed. It is currently only used as a shim layer for UST because the
2996 * consumerd needs to keep the per-stream wakeup end of pipes open for
2999 void *consumer_thread_channel_poll(void *data
)
3001 int ret
, i
, pollfd
, err
= -1;
3002 uint32_t revents
, nb_fd
;
3003 struct lttng_consumer_channel
*chan
= NULL
;
3004 struct lttng_ht_iter iter
;
3005 struct lttng_ht_node_u64
*node
;
3006 struct lttng_poll_event events
;
3007 struct lttng_consumer_local_data
*ctx
= data
;
3008 struct lttng_ht
*channel_ht
;
3010 rcu_register_thread();
3012 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
3014 if (testpoint(consumerd_thread_channel
)) {
3015 goto error_testpoint
;
3018 health_code_update();
3020 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3022 /* ENOMEM at this point. Better to bail out. */
3026 DBG("Thread channel poll started");
3028 /* Size is set to 1 for the consumer_channel pipe */
3029 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
3031 ERR("Poll set creation failed");
3035 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
3041 DBG("Channel main loop started");
3045 health_code_update();
3046 DBG("Channel poll wait");
3047 health_poll_entry();
3048 ret
= lttng_poll_wait(&events
, -1);
3049 DBG("Channel poll return from wait with %d fd(s)",
3050 LTTNG_POLL_GETNB(&events
));
3052 DBG("Channel event caught in thread");
3054 if (errno
== EINTR
) {
3055 ERR("Poll EINTR caught");
3058 if (LTTNG_POLL_GETNB(&events
) == 0) {
3059 err
= 0; /* All is OK */
3066 /* From here, the event is a channel wait fd */
3067 for (i
= 0; i
< nb_fd
; i
++) {
3068 health_code_update();
3070 revents
= LTTNG_POLL_GETEV(&events
, i
);
3071 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3073 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
3074 if (revents
& LPOLLIN
) {
3075 enum consumer_channel_action action
;
3078 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
3081 ERR("Error reading channel pipe");
3083 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3088 case CONSUMER_CHANNEL_ADD
:
3089 DBG("Adding channel %d to poll set",
3092 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
3095 lttng_ht_add_unique_u64(channel_ht
,
3096 &chan
->wait_fd_node
);
3098 /* Add channel to the global poll events list */
3099 lttng_poll_add(&events
, chan
->wait_fd
,
3100 LPOLLERR
| LPOLLHUP
);
3102 case CONSUMER_CHANNEL_DEL
:
3105 * This command should never be called if the channel
3106 * has streams monitored by either the data or metadata
3107 * thread. The consumer only notify this thread with a
3108 * channel del. command if it receives a destroy
3109 * channel command from the session daemon that send it
3110 * if a command prior to the GET_CHANNEL failed.
3114 chan
= consumer_find_channel(key
);
3117 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
3120 lttng_poll_del(&events
, chan
->wait_fd
);
3121 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
3122 ret
= lttng_ht_del(channel_ht
, &iter
);
3125 switch (consumer_data
.type
) {
3126 case LTTNG_CONSUMER_KERNEL
:
3128 case LTTNG_CONSUMER32_UST
:
3129 case LTTNG_CONSUMER64_UST
:
3130 health_code_update();
3131 /* Destroy streams that might have been left in the stream list. */
3132 clean_channel_stream_list(chan
);
3135 ERR("Unknown consumer_data type");
3140 * Release our own refcount. Force channel deletion even if
3141 * streams were not initialized.
3143 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3144 consumer_del_channel(chan
);
3149 case CONSUMER_CHANNEL_QUIT
:
3151 * Remove the pipe from the poll set and continue the loop
3152 * since their might be data to consume.
3154 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3157 ERR("Unknown action");
3160 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3161 DBG("Channel thread pipe hung up");
3163 * Remove the pipe from the poll set and continue the loop
3164 * since their might be data to consume.
3166 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3169 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3173 /* Handle other stream */
3179 uint64_t tmp_id
= (uint64_t) pollfd
;
3181 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3183 node
= lttng_ht_iter_get_node_u64(&iter
);
3186 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
3189 /* Check for error event */
3190 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3191 DBG("Channel fd %d is hup|err.", pollfd
);
3193 lttng_poll_del(&events
, chan
->wait_fd
);
3194 ret
= lttng_ht_del(channel_ht
, &iter
);
3198 * This will close the wait fd for each stream associated to
3199 * this channel AND monitored by the data/metadata thread thus
3200 * will be clean by the right thread.
3202 consumer_close_channel_streams(chan
);
3204 /* Release our own refcount */
3205 if (!uatomic_sub_return(&chan
->refcount
, 1)
3206 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3207 consumer_del_channel(chan
);
3210 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3215 /* Release RCU lock for the channel looked up */
3223 lttng_poll_clean(&events
);
3225 destroy_channel_ht(channel_ht
);
3228 DBG("Channel poll thread exiting");
3231 ERR("Health error occurred in %s", __func__
);
3233 health_unregister(health_consumerd
);
3234 rcu_unregister_thread();
3238 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3239 struct pollfd
*sockpoll
, int client_socket
)
3246 ret
= lttng_consumer_poll_socket(sockpoll
);
3250 DBG("Metadata connection on client_socket");
3252 /* Blocking call, waiting for transmission */
3253 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3254 if (ctx
->consumer_metadata_socket
< 0) {
3255 WARN("On accept metadata");
3266 * This thread listens on the consumerd socket and receives the file
3267 * descriptors from the session daemon.
3269 void *consumer_thread_sessiond_poll(void *data
)
3271 int sock
= -1, client_socket
, ret
, err
= -1;
3273 * structure to poll for incoming data on communication socket avoids
3274 * making blocking sockets.
3276 struct pollfd consumer_sockpoll
[2];
3277 struct lttng_consumer_local_data
*ctx
= data
;
3279 rcu_register_thread();
3281 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3283 if (testpoint(consumerd_thread_sessiond
)) {
3284 goto error_testpoint
;
3287 health_code_update();
3289 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3290 unlink(ctx
->consumer_command_sock_path
);
3291 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3292 if (client_socket
< 0) {
3293 ERR("Cannot create command socket");
3297 ret
= lttcomm_listen_unix_sock(client_socket
);
3302 DBG("Sending ready command to lttng-sessiond");
3303 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3304 /* return < 0 on error, but == 0 is not fatal */
3306 ERR("Error sending ready command to lttng-sessiond");
3310 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3311 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3312 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3313 consumer_sockpoll
[1].fd
= client_socket
;
3314 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3316 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3324 DBG("Connection on client_socket");
3326 /* Blocking call, waiting for transmission */
3327 sock
= lttcomm_accept_unix_sock(client_socket
);
3334 * Setup metadata socket which is the second socket connection on the
3335 * command unix socket.
3337 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3346 /* This socket is not useful anymore. */
3347 ret
= close(client_socket
);
3349 PERROR("close client_socket");
3353 /* update the polling structure to poll on the established socket */
3354 consumer_sockpoll
[1].fd
= sock
;
3355 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3358 health_code_update();
3360 health_poll_entry();
3361 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3370 DBG("Incoming command on sock");
3371 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3374 * This could simply be a session daemon quitting. Don't output
3377 DBG("Communication interrupted on command socket");
3381 if (CMM_LOAD_SHARED(consumer_quit
)) {
3382 DBG("consumer_thread_receive_fds received quit from signal");
3383 err
= 0; /* All is OK */
3386 DBG("received command on sock");
3392 DBG("Consumer thread sessiond poll exiting");
3395 * Close metadata streams since the producer is the session daemon which
3398 * NOTE: for now, this only applies to the UST tracer.
3400 lttng_consumer_close_all_metadata();
3403 * when all fds have hung up, the polling thread
3406 CMM_STORE_SHARED(consumer_quit
, 1);
3409 * Notify the data poll thread to poll back again and test the
3410 * consumer_quit state that we just set so to quit gracefully.
3412 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3414 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3416 notify_health_quit_pipe(health_quit_pipe
);
3418 /* Cleaning up possibly open sockets. */
3422 PERROR("close sock sessiond poll");
3425 if (client_socket
>= 0) {
3426 ret
= close(client_socket
);
3428 PERROR("close client_socket sessiond poll");
3435 ERR("Health error occurred in %s", __func__
);
3437 health_unregister(health_consumerd
);
3439 rcu_unregister_thread();
3443 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3444 struct lttng_consumer_local_data
*ctx
)
3448 pthread_mutex_lock(&stream
->chan
->lock
);
3449 pthread_mutex_lock(&stream
->lock
);
3450 if (stream
->metadata_flag
) {
3451 pthread_mutex_lock(&stream
->metadata_rdv_lock
);
3454 switch (consumer_data
.type
) {
3455 case LTTNG_CONSUMER_KERNEL
:
3456 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3458 case LTTNG_CONSUMER32_UST
:
3459 case LTTNG_CONSUMER64_UST
:
3460 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3463 ERR("Unknown consumer_data type");
3469 if (stream
->metadata_flag
) {
3470 pthread_cond_broadcast(&stream
->metadata_rdv
);
3471 pthread_mutex_unlock(&stream
->metadata_rdv_lock
);
3473 pthread_mutex_unlock(&stream
->lock
);
3474 pthread_mutex_unlock(&stream
->chan
->lock
);
3479 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3481 switch (consumer_data
.type
) {
3482 case LTTNG_CONSUMER_KERNEL
:
3483 return lttng_kconsumer_on_recv_stream(stream
);
3484 case LTTNG_CONSUMER32_UST
:
3485 case LTTNG_CONSUMER64_UST
:
3486 return lttng_ustconsumer_on_recv_stream(stream
);
3488 ERR("Unknown consumer_data type");
3495 * Allocate and set consumer data hash tables.
3497 int lttng_consumer_init(void)
3499 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3500 if (!consumer_data
.channel_ht
) {
3504 consumer_data
.channels_by_session_id_ht
=
3505 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3506 if (!consumer_data
.channels_by_session_id_ht
) {
3510 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3511 if (!consumer_data
.relayd_ht
) {
3515 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3516 if (!consumer_data
.stream_list_ht
) {
3520 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3521 if (!consumer_data
.stream_per_chan_id_ht
) {
3525 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3530 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3535 consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3536 if (!consumer_data
.chunk_registry
) {
3547 * Process the ADD_RELAYD command receive by a consumer.
3549 * This will create a relayd socket pair and add it to the relayd hash table.
3550 * The caller MUST acquire a RCU read side lock before calling it.
3552 void consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3553 struct lttng_consumer_local_data
*ctx
, int sock
,
3554 struct pollfd
*consumer_sockpoll
,
3555 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3556 uint64_t relayd_session_id
)
3558 int fd
= -1, ret
= -1, relayd_created
= 0;
3559 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3560 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3563 assert(relayd_sock
);
3565 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3567 /* Get relayd reference if exists. */
3568 relayd
= consumer_find_relayd(net_seq_idx
);
3569 if (relayd
== NULL
) {
3570 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3571 /* Not found. Allocate one. */
3572 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3573 if (relayd
== NULL
) {
3574 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3577 relayd
->sessiond_session_id
= sessiond_id
;
3582 * This code path MUST continue to the consumer send status message to
3583 * we can notify the session daemon and continue our work without
3584 * killing everything.
3588 * relayd key should never be found for control socket.
3590 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3593 /* First send a status message before receiving the fds. */
3594 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3596 /* Somehow, the session daemon is not responding anymore. */
3597 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3598 goto error_nosignal
;
3601 /* Poll on consumer socket. */
3602 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3604 /* Needing to exit in the middle of a command: error. */
3605 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3606 goto error_nosignal
;
3609 /* Get relayd socket from session daemon */
3610 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3611 if (ret
!= sizeof(fd
)) {
3612 fd
= -1; /* Just in case it gets set with an invalid value. */
3615 * Failing to receive FDs might indicate a major problem such as
3616 * reaching a fd limit during the receive where the kernel returns a
3617 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3618 * don't take any chances and stop everything.
3620 * XXX: Feature request #558 will fix that and avoid this possible
3621 * issue when reaching the fd limit.
3623 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3624 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3628 /* Copy socket information and received FD */
3629 switch (sock_type
) {
3630 case LTTNG_STREAM_CONTROL
:
3631 /* Copy received lttcomm socket */
3632 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3633 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3634 /* Handle create_sock error. */
3636 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3640 * Close the socket created internally by
3641 * lttcomm_create_sock, so we can replace it by the one
3642 * received from sessiond.
3644 if (close(relayd
->control_sock
.sock
.fd
)) {
3648 /* Assign new file descriptor */
3649 relayd
->control_sock
.sock
.fd
= fd
;
3650 /* Assign version values. */
3651 relayd
->control_sock
.major
= relayd_sock
->major
;
3652 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3654 relayd
->relayd_session_id
= relayd_session_id
;
3657 case LTTNG_STREAM_DATA
:
3658 /* Copy received lttcomm socket */
3659 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3660 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3661 /* Handle create_sock error. */
3663 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3667 * Close the socket created internally by
3668 * lttcomm_create_sock, so we can replace it by the one
3669 * received from sessiond.
3671 if (close(relayd
->data_sock
.sock
.fd
)) {
3675 /* Assign new file descriptor */
3676 relayd
->data_sock
.sock
.fd
= fd
;
3677 /* Assign version values. */
3678 relayd
->data_sock
.major
= relayd_sock
->major
;
3679 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3682 ERR("Unknown relayd socket type (%d)", sock_type
);
3683 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3687 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3688 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3689 relayd
->net_seq_idx
, fd
);
3691 * We gave the ownership of the fd to the relayd structure. Set the
3692 * fd to -1 so we don't call close() on it in the error path below.
3696 /* We successfully added the socket. Send status back. */
3697 ret
= consumer_send_status_msg(sock
, ret_code
);
3699 /* Somehow, the session daemon is not responding anymore. */
3700 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3701 goto error_nosignal
;
3705 * Add relayd socket pair to consumer data hashtable. If object already
3706 * exists or on error, the function gracefully returns.
3715 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3716 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3720 /* Close received socket if valid. */
3723 PERROR("close received socket");
3727 if (relayd_created
) {
3733 * Search for a relayd associated to the session id and return the reference.
3735 * A rcu read side lock MUST be acquire before calling this function and locked
3736 * until the relayd object is no longer necessary.
3738 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3740 struct lttng_ht_iter iter
;
3741 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3743 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3744 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3747 * Check by sessiond id which is unique here where the relayd session
3748 * id might not be when having multiple relayd.
3750 if (relayd
->sessiond_session_id
== id
) {
3751 /* Found the relayd. There can be only one per id. */
3763 * Check if for a given session id there is still data needed to be extract
3766 * Return 1 if data is pending or else 0 meaning ready to be read.
3768 int consumer_data_pending(uint64_t id
)
3771 struct lttng_ht_iter iter
;
3772 struct lttng_ht
*ht
;
3773 struct lttng_consumer_stream
*stream
;
3774 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3775 int (*data_pending
)(struct lttng_consumer_stream
*);
3777 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3780 pthread_mutex_lock(&consumer_data
.lock
);
3782 switch (consumer_data
.type
) {
3783 case LTTNG_CONSUMER_KERNEL
:
3784 data_pending
= lttng_kconsumer_data_pending
;
3786 case LTTNG_CONSUMER32_UST
:
3787 case LTTNG_CONSUMER64_UST
:
3788 data_pending
= lttng_ustconsumer_data_pending
;
3791 ERR("Unknown consumer data type");
3795 /* Ease our life a bit */
3796 ht
= consumer_data
.stream_list_ht
;
3798 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3799 ht
->hash_fct(&id
, lttng_ht_seed
),
3801 &iter
.iter
, stream
, node_session_id
.node
) {
3802 pthread_mutex_lock(&stream
->lock
);
3805 * A removed node from the hash table indicates that the stream has
3806 * been deleted thus having a guarantee that the buffers are closed
3807 * on the consumer side. However, data can still be transmitted
3808 * over the network so don't skip the relayd check.
3810 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3812 /* Check the stream if there is data in the buffers. */
3813 ret
= data_pending(stream
);
3815 pthread_mutex_unlock(&stream
->lock
);
3820 pthread_mutex_unlock(&stream
->lock
);
3823 relayd
= find_relayd_by_session_id(id
);
3825 unsigned int is_data_inflight
= 0;
3827 /* Send init command for data pending. */
3828 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3829 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3830 relayd
->relayd_session_id
);
3832 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3833 /* Communication error thus the relayd so no data pending. */
3834 goto data_not_pending
;
3837 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3838 ht
->hash_fct(&id
, lttng_ht_seed
),
3840 &iter
.iter
, stream
, node_session_id
.node
) {
3841 if (stream
->metadata_flag
) {
3842 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3843 stream
->relayd_stream_id
);
3845 ret
= relayd_data_pending(&relayd
->control_sock
,
3846 stream
->relayd_stream_id
,
3847 stream
->next_net_seq_num
- 1);
3851 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3853 } else if (ret
< 0) {
3854 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3855 lttng_consumer_cleanup_relayd(relayd
);
3856 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3857 goto data_not_pending
;
3861 /* Send end command for data pending. */
3862 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3863 relayd
->relayd_session_id
, &is_data_inflight
);
3864 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3866 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3867 lttng_consumer_cleanup_relayd(relayd
);
3868 goto data_not_pending
;
3870 if (is_data_inflight
) {
3876 * Finding _no_ node in the hash table and no inflight data means that the
3877 * stream(s) have been removed thus data is guaranteed to be available for
3878 * analysis from the trace files.
3882 /* Data is available to be read by a viewer. */
3883 pthread_mutex_unlock(&consumer_data
.lock
);
3888 /* Data is still being extracted from buffers. */
3889 pthread_mutex_unlock(&consumer_data
.lock
);
3895 * Send a ret code status message to the sessiond daemon.
3897 * Return the sendmsg() return value.
3899 int consumer_send_status_msg(int sock
, int ret_code
)
3901 struct lttcomm_consumer_status_msg msg
;
3903 memset(&msg
, 0, sizeof(msg
));
3904 msg
.ret_code
= ret_code
;
3906 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3910 * Send a channel status message to the sessiond daemon.
3912 * Return the sendmsg() return value.
3914 int consumer_send_status_channel(int sock
,
3915 struct lttng_consumer_channel
*channel
)
3917 struct lttcomm_consumer_status_channel msg
;
3921 memset(&msg
, 0, sizeof(msg
));
3923 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3925 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3926 msg
.key
= channel
->key
;
3927 msg
.stream_count
= channel
->streams
.count
;
3930 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3933 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3934 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3935 uint64_t max_sb_size
)
3937 unsigned long start_pos
;
3939 if (!nb_packets_per_stream
) {
3940 return consumed_pos
; /* Grab everything */
3942 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
3943 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3944 if ((long) (start_pos
- consumed_pos
) < 0) {
3945 return consumed_pos
; /* Grab everything */
3951 int consumer_flush_buffer(struct lttng_consumer_stream
*stream
, int producer_active
)
3955 switch (consumer_data
.type
) {
3956 case LTTNG_CONSUMER_KERNEL
:
3957 if (producer_active
) {
3958 ret
= kernctl_buffer_flush(stream
->wait_fd
);
3960 ERR("Failed to flush kernel stream");
3964 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
3966 ERR("Failed to flush kernel stream");
3971 case LTTNG_CONSUMER32_UST
:
3972 case LTTNG_CONSUMER64_UST
:
3973 lttng_ustconsumer_flush_buffer(stream
, producer_active
);
3976 ERR("Unknown consumer_data type");
3985 * Sample the rotate position for all the streams of a channel. If a stream
3986 * is already at the rotate position (produced == consumed), we flag it as
3987 * ready for rotation. The rotation of ready streams occurs after we have
3988 * replied to the session daemon that we have finished sampling the positions.
3989 * Must be called with RCU read-side lock held to ensure existence of channel.
3991 * Returns 0 on success, < 0 on error
3993 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
3994 uint64_t key
, uint64_t relayd_id
, uint32_t metadata
,
3995 struct lttng_consumer_local_data
*ctx
)
3998 struct lttng_consumer_stream
*stream
;
3999 struct lttng_ht_iter iter
;
4000 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
4001 struct lttng_dynamic_array stream_rotation_positions
;
4002 uint64_t next_chunk_id
, stream_count
= 0;
4003 enum lttng_trace_chunk_status chunk_status
;
4004 const bool is_local_trace
= relayd_id
== -1ULL;
4005 struct consumer_relayd_sock_pair
*relayd
= NULL
;
4006 bool rotating_to_new_chunk
= true;
4008 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
4010 lttng_dynamic_array_init(&stream_rotation_positions
,
4011 sizeof(struct relayd_stream_rotation_position
), NULL
);
4015 pthread_mutex_lock(&channel
->lock
);
4016 assert(channel
->trace_chunk
);
4017 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
,
4019 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4021 goto end_unlock_channel
;
4024 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4025 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4026 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4027 stream
, node_channel_id
.node
) {
4028 unsigned long produced_pos
= 0, consumed_pos
= 0;
4030 health_code_update();
4033 * Lock stream because we are about to change its state.
4035 pthread_mutex_lock(&stream
->lock
);
4037 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
4038 rotating_to_new_chunk
= false;
4042 * Do not flush an empty packet when rotating from a NULL trace
4043 * chunk. The stream has no means to output data, and the prior
4044 * rotation which rotated to NULL performed that side-effect already.
4046 if (stream
->trace_chunk
) {
4048 * For metadata stream, do an active flush, which does not
4049 * produce empty packets. For data streams, empty-flush;
4050 * ensures we have at least one packet in each stream per trace
4051 * chunk, even if no data was produced.
4053 ret
= consumer_flush_buffer(stream
, stream
->metadata_flag
? 1 : 0);
4055 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4057 goto end_unlock_stream
;
4061 ret
= lttng_consumer_take_snapshot(stream
);
4062 if (ret
< 0 && ret
!= -ENODATA
&& ret
!= -EAGAIN
) {
4063 ERR("Failed to sample snapshot position during channel rotation");
4064 goto end_unlock_stream
;
4067 ret
= lttng_consumer_get_produced_snapshot(stream
,
4070 ERR("Failed to sample produced position during channel rotation");
4071 goto end_unlock_stream
;
4074 ret
= lttng_consumer_get_consumed_snapshot(stream
,
4077 ERR("Failed to sample consumed position during channel rotation");
4078 goto end_unlock_stream
;
4082 * Align produced position on the start-of-packet boundary of the first
4083 * packet going into the next trace chunk.
4085 produced_pos
= ALIGN_FLOOR(produced_pos
, stream
->max_sb_size
);
4086 if (consumed_pos
== produced_pos
) {
4087 DBG("Set rotate ready for stream %" PRIu64
" produced = %lu consumed = %lu",
4088 stream
->key
, produced_pos
, consumed_pos
);
4089 stream
->rotate_ready
= true;
4091 DBG("Different consumed and produced positions "
4092 "for stream %" PRIu64
" produced = %lu consumed = %lu",
4093 stream
->key
, produced_pos
, consumed_pos
);
4096 * The rotation position is based on the packet_seq_num of the
4097 * packet following the last packet that was consumed for this
4098 * stream, incremented by the offset between produced and
4099 * consumed positions. This rotation position is a lower bound
4100 * (inclusive) at which the next trace chunk starts. Since it
4101 * is a lower bound, it is OK if the packet_seq_num does not
4102 * correspond exactly to the same packet identified by the
4103 * consumed_pos, which can happen in overwrite mode.
4105 if (stream
->sequence_number_unavailable
) {
4107 * Rotation should never be performed on a session which
4108 * interacts with a pre-2.8 lttng-modules, which does
4109 * not implement packet sequence number.
4111 ERR("Failure to rotate stream %" PRIu64
": sequence number unavailable",
4114 goto end_unlock_stream
;
4116 stream
->rotate_position
= stream
->last_sequence_number
+ 1 +
4117 ((produced_pos
- consumed_pos
) / stream
->max_sb_size
);
4118 DBG("Set rotation position for stream %" PRIu64
" at position %" PRIu64
,
4119 stream
->key
, stream
->rotate_position
);
4121 if (!is_local_trace
) {
4123 * The relay daemon control protocol expects a rotation
4124 * position as "the sequence number of the first packet
4125 * _after_ the current trace chunk".
4127 const struct relayd_stream_rotation_position position
= {
4128 .stream_id
= stream
->relayd_stream_id
,
4129 .rotate_at_seq_num
= stream
->rotate_position
,
4132 ret
= lttng_dynamic_array_add_element(
4133 &stream_rotation_positions
,
4136 ERR("Failed to allocate stream rotation position");
4137 goto end_unlock_stream
;
4141 pthread_mutex_unlock(&stream
->lock
);
4144 pthread_mutex_unlock(&channel
->lock
);
4146 if (is_local_trace
) {
4151 relayd
= consumer_find_relayd(relayd_id
);
4153 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4158 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4159 ret
= relayd_rotate_streams(&relayd
->control_sock
, stream_count
,
4160 rotating_to_new_chunk
? &next_chunk_id
: NULL
,
4161 (const struct relayd_stream_rotation_position
*)
4162 stream_rotation_positions
.buffer
.data
);
4163 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4165 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4166 relayd
->net_seq_idx
);
4167 lttng_consumer_cleanup_relayd(relayd
);
4175 pthread_mutex_unlock(&stream
->lock
);
4177 pthread_mutex_unlock(&channel
->lock
);
4180 lttng_dynamic_array_reset(&stream_rotation_positions
);
4185 int consumer_clear_buffer(struct lttng_consumer_stream
*stream
)
4188 unsigned long consumed_pos_before
, consumed_pos_after
;
4190 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4192 ERR("Taking snapshot positions");
4196 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_before
);
4198 ERR("Consumed snapshot position");
4202 switch (consumer_data
.type
) {
4203 case LTTNG_CONSUMER_KERNEL
:
4204 ret
= kernctl_buffer_clear(stream
->wait_fd
);
4206 ERR("Failed to flush kernel stream");
4210 case LTTNG_CONSUMER32_UST
:
4211 case LTTNG_CONSUMER64_UST
:
4212 lttng_ustconsumer_clear_buffer(stream
);
4215 ERR("Unknown consumer_data type");
4219 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4221 ERR("Taking snapshot positions");
4224 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos_after
);
4226 ERR("Consumed snapshot position");
4229 DBG("clear: before: %lu after: %lu", consumed_pos_before
, consumed_pos_after
);
4235 int consumer_clear_stream(struct lttng_consumer_stream
*stream
)
4239 ret
= consumer_flush_buffer(stream
, 1);
4241 ERR("Failed to flush stream %" PRIu64
" during channel clear",
4243 ret
= LTTCOMM_CONSUMERD_FATAL
;
4247 ret
= consumer_clear_buffer(stream
);
4249 ERR("Failed to clear stream %" PRIu64
" during channel clear",
4251 ret
= LTTCOMM_CONSUMERD_FATAL
;
4255 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4261 int consumer_clear_unmonitored_channel(struct lttng_consumer_channel
*channel
)
4264 struct lttng_consumer_stream
*stream
;
4267 pthread_mutex_lock(&channel
->lock
);
4268 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
4269 health_code_update();
4270 pthread_mutex_lock(&stream
->lock
);
4271 ret
= consumer_clear_stream(stream
);
4275 pthread_mutex_unlock(&stream
->lock
);
4277 pthread_mutex_unlock(&channel
->lock
);
4282 pthread_mutex_unlock(&stream
->lock
);
4283 pthread_mutex_unlock(&channel
->lock
);
4288 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4294 * Check if a stream is ready to be rotated after extracting it.
4296 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4297 * error. Stream lock must be held.
4299 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4301 DBG("Check is rotate ready for stream %" PRIu64
4302 " ready %u rotate_position %" PRIu64
4303 " last_sequence_number %" PRIu64
,
4304 stream
->key
, stream
->rotate_ready
,
4305 stream
->rotate_position
, stream
->last_sequence_number
);
4306 if (stream
->rotate_ready
) {
4311 * If packet seq num is unavailable, it means we are interacting
4312 * with a pre-2.8 lttng-modules which does not implement the
4313 * sequence number. Rotation should never be used by sessiond in this
4316 if (stream
->sequence_number_unavailable
) {
4317 ERR("Internal error: rotation used on stream %" PRIu64
4318 " with unavailable sequence number",
4323 if (stream
->rotate_position
== -1ULL ||
4324 stream
->last_sequence_number
== -1ULL) {
4329 * Rotate position not reached yet. The stream rotate position is
4330 * the position of the next packet belonging to the next trace chunk,
4331 * but consumerd considers rotation ready when reaching the last
4332 * packet of the current chunk, hence the "rotate_position - 1".
4335 DBG("Check is rotate ready for stream %" PRIu64
4336 " last_sequence_number %" PRIu64
4337 " rotate_position %" PRIu64
,
4338 stream
->key
, stream
->last_sequence_number
,
4339 stream
->rotate_position
);
4340 if (stream
->last_sequence_number
>= stream
->rotate_position
- 1) {
4348 * Reset the state for a stream after a rotation occurred.
4350 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4352 DBG("lttng_consumer_reset_stream_rotate_state for stream %" PRIu64
,
4354 stream
->rotate_position
= -1ULL;
4355 stream
->rotate_ready
= false;
4359 * Perform the rotation a local stream file.
4362 int rotate_local_stream(struct lttng_consumer_local_data
*ctx
,
4363 struct lttng_consumer_stream
*stream
)
4367 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4370 stream
->tracefile_size_current
= 0;
4371 stream
->tracefile_count_current
= 0;
4373 if (stream
->out_fd
>= 0) {
4374 ret
= close(stream
->out_fd
);
4376 PERROR("Failed to close stream out_fd of channel \"%s\"",
4377 stream
->chan
->name
);
4379 stream
->out_fd
= -1;
4382 if (stream
->index_file
) {
4383 lttng_index_file_put(stream
->index_file
);
4384 stream
->index_file
= NULL
;
4387 if (!stream
->trace_chunk
) {
4391 ret
= consumer_stream_create_output_files(stream
, true);
4397 * Performs the stream rotation for the rotate session feature if needed.
4398 * It must be called with the channel and stream locks held.
4400 * Return 0 on success, a negative number of error.
4402 int lttng_consumer_rotate_stream(struct lttng_consumer_local_data
*ctx
,
4403 struct lttng_consumer_stream
*stream
)
4407 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4410 * Update the stream's 'current' chunk to the session's (channel)
4411 * now-current chunk.
4413 lttng_trace_chunk_put(stream
->trace_chunk
);
4414 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4416 * A channel can be rotated and not have a "next" chunk
4417 * to transition to. In that case, the channel's "current chunk"
4418 * has not been closed yet, but it has not been updated to
4419 * a "next" trace chunk either. Hence, the stream, like its
4420 * parent channel, becomes part of no chunk and can't output
4421 * anything until a new trace chunk is created.
4423 stream
->trace_chunk
= NULL
;
4424 } else if (stream
->chan
->trace_chunk
&&
4425 !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4426 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4431 * Update the stream's trace chunk to its parent channel's
4432 * current trace chunk.
4434 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4437 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4438 ret
= rotate_local_stream(ctx
, stream
);
4440 ERR("Failed to rotate stream, ret = %i", ret
);
4445 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4447 * If the stream has transitioned to a new trace
4448 * chunk, the metadata should be re-dumped to the
4451 * However, it is possible for a stream to transition to
4452 * a "no-chunk" state. This can happen if a rotation
4453 * occurs on an inactive session. In such cases, the metadata
4454 * regeneration will happen when the next trace chunk is
4457 ret
= consumer_metadata_stream_dump(stream
);
4462 lttng_consumer_reset_stream_rotate_state(stream
);
4471 * Rotate all the ready streams now.
4473 * This is especially important for low throughput streams that have already
4474 * been consumed, we cannot wait for their next packet to perform the
4476 * Need to be called with RCU read-side lock held to ensure existence of
4479 * Returns 0 on success, < 0 on error
4481 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
,
4482 uint64_t key
, struct lttng_consumer_local_data
*ctx
)
4485 struct lttng_consumer_stream
*stream
;
4486 struct lttng_ht_iter iter
;
4487 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
4491 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4493 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4494 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4495 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4496 stream
, node_channel_id
.node
) {
4497 health_code_update();
4499 pthread_mutex_lock(&stream
->chan
->lock
);
4500 pthread_mutex_lock(&stream
->lock
);
4502 if (!stream
->rotate_ready
) {
4503 pthread_mutex_unlock(&stream
->lock
);
4504 pthread_mutex_unlock(&stream
->chan
->lock
);
4507 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4509 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
4510 pthread_mutex_unlock(&stream
->lock
);
4511 pthread_mutex_unlock(&stream
->chan
->lock
);
4524 enum lttcomm_return_code
lttng_consumer_init_command(
4525 struct lttng_consumer_local_data
*ctx
,
4526 const lttng_uuid sessiond_uuid
)
4528 enum lttcomm_return_code ret
;
4529 char uuid_str
[LTTNG_UUID_STR_LEN
];
4531 if (ctx
->sessiond_uuid
.is_set
) {
4532 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4536 ctx
->sessiond_uuid
.is_set
= true;
4537 memcpy(ctx
->sessiond_uuid
.value
, sessiond_uuid
, sizeof(lttng_uuid
));
4538 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4539 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4540 DBG("Received session daemon UUID: %s", uuid_str
);
4545 enum lttcomm_return_code
lttng_consumer_create_trace_chunk(
4546 const uint64_t *relayd_id
, uint64_t session_id
,
4548 time_t chunk_creation_timestamp
,
4549 const char *chunk_override_name
,
4550 const struct lttng_credentials
*credentials
,
4551 struct lttng_directory_handle
*chunk_directory_handle
)
4554 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4555 struct lttng_trace_chunk
*created_chunk
= NULL
, *published_chunk
= NULL
;
4556 enum lttng_trace_chunk_status chunk_status
;
4557 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4558 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4559 const char *relayd_id_str
= "(none)";
4560 const char *creation_timestamp_str
;
4561 struct lttng_ht_iter iter
;
4562 struct lttng_consumer_channel
*channel
;
4565 /* Only used for logging purposes. */
4566 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4567 "%" PRIu64
, *relayd_id
);
4568 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4569 relayd_id_str
= relayd_id_buffer
;
4571 relayd_id_str
= "(formatting error)";
4575 /* Local protocol error. */
4576 assert(chunk_creation_timestamp
);
4577 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4578 creation_timestamp_buffer
,
4579 sizeof(creation_timestamp_buffer
));
4580 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
4581 "(formatting error)";
4583 DBG("Consumer create trace chunk command: relay_id = %s"
4584 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4585 ", chunk_override_name = %s"
4586 ", chunk_creation_timestamp = %s",
4587 relayd_id_str
, session_id
, chunk_id
,
4588 chunk_override_name
? : "(none)",
4589 creation_timestamp_str
);
4592 * The trace chunk registry, as used by the consumer daemon, implicitly
4593 * owns the trace chunks. This is only needed in the consumer since
4594 * the consumer has no notion of a session beyond session IDs being
4595 * used to identify other objects.
4597 * The lttng_trace_chunk_registry_publish() call below provides a
4598 * reference which is not released; it implicitly becomes the session
4599 * daemon's reference to the chunk in the consumer daemon.
4601 * The lifetime of trace chunks in the consumer daemon is managed by
4602 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4603 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4605 created_chunk
= lttng_trace_chunk_create(chunk_id
,
4606 chunk_creation_timestamp
, NULL
);
4607 if (!created_chunk
) {
4608 ERR("Failed to create trace chunk");
4609 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4613 if (chunk_override_name
) {
4614 chunk_status
= lttng_trace_chunk_override_name(created_chunk
,
4615 chunk_override_name
);
4616 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4617 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4622 if (chunk_directory_handle
) {
4623 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
,
4625 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4626 ERR("Failed to set trace chunk credentials");
4627 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4631 * The consumer daemon has no ownership of the chunk output
4634 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
,
4635 chunk_directory_handle
);
4636 chunk_directory_handle
= NULL
;
4637 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4638 ERR("Failed to set trace chunk's directory handle");
4639 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4644 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4645 consumer_data
.chunk_registry
, session_id
,
4647 lttng_trace_chunk_put(created_chunk
);
4648 created_chunk
= NULL
;
4649 if (!published_chunk
) {
4650 ERR("Failed to publish trace chunk");
4651 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4656 cds_lfht_for_each_entry_duplicate(consumer_data
.channels_by_session_id_ht
->ht
,
4657 consumer_data
.channels_by_session_id_ht
->hash_fct(
4658 &session_id
, lttng_ht_seed
),
4659 consumer_data
.channels_by_session_id_ht
->match_fct
,
4660 &session_id
, &iter
.iter
, channel
,
4661 channels_by_session_id_ht_node
.node
) {
4662 ret
= lttng_consumer_channel_set_trace_chunk(channel
,
4666 * Roll-back the creation of this chunk.
4668 * This is important since the session daemon will
4669 * assume that the creation of this chunk failed and
4670 * will never ask for it to be closed, resulting
4671 * in a leak and an inconsistent state for some
4674 enum lttcomm_return_code close_ret
;
4675 char path
[LTTNG_PATH_MAX
];
4677 DBG("Failed to set new trace chunk on existing channels, rolling back");
4678 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4679 session_id
, chunk_id
,
4680 chunk_creation_timestamp
, NULL
,
4682 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4683 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4684 session_id
, chunk_id
);
4687 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4693 struct consumer_relayd_sock_pair
*relayd
;
4695 relayd
= consumer_find_relayd(*relayd_id
);
4697 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4698 ret
= relayd_create_trace_chunk(
4699 &relayd
->control_sock
, published_chunk
);
4700 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4702 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4705 if (!relayd
|| ret
) {
4706 enum lttcomm_return_code close_ret
;
4707 char path
[LTTNG_PATH_MAX
];
4709 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4712 chunk_creation_timestamp
,
4714 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4715 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4720 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4727 /* Release the reference returned by the "publish" operation. */
4728 lttng_trace_chunk_put(published_chunk
);
4729 lttng_trace_chunk_put(created_chunk
);
4733 enum lttcomm_return_code
lttng_consumer_close_trace_chunk(
4734 const uint64_t *relayd_id
, uint64_t session_id
,
4735 uint64_t chunk_id
, time_t chunk_close_timestamp
,
4736 const enum lttng_trace_chunk_command_type
*close_command
,
4739 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4740 struct lttng_trace_chunk
*chunk
;
4741 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4742 const char *relayd_id_str
= "(none)";
4743 const char *close_command_name
= "none";
4744 struct lttng_ht_iter iter
;
4745 struct lttng_consumer_channel
*channel
;
4746 enum lttng_trace_chunk_status chunk_status
;
4751 /* Only used for logging purposes. */
4752 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4753 "%" PRIu64
, *relayd_id
);
4754 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4755 relayd_id_str
= relayd_id_buffer
;
4757 relayd_id_str
= "(formatting error)";
4760 if (close_command
) {
4761 close_command_name
= lttng_trace_chunk_command_type_get_name(
4765 DBG("Consumer close trace chunk command: relayd_id = %s"
4766 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4767 ", close command = %s",
4768 relayd_id_str
, session_id
, chunk_id
,
4769 close_command_name
);
4771 chunk
= lttng_trace_chunk_registry_find_chunk(
4772 consumer_data
.chunk_registry
, session_id
, chunk_id
);
4774 ERR("Failed to find chunk: session_id = %" PRIu64
4775 ", chunk_id = %" PRIu64
,
4776 session_id
, chunk_id
);
4777 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4781 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
,
4782 chunk_close_timestamp
);
4783 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4784 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4788 if (close_command
) {
4789 chunk_status
= lttng_trace_chunk_set_close_command(
4790 chunk
, *close_command
);
4791 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4792 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4798 * chunk is now invalid to access as we no longer hold a reference to
4799 * it; it is only kept around to compare it (by address) to the
4800 * current chunk found in the session's channels.
4803 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
,
4804 channel
, node
.node
) {
4808 * Only change the channel's chunk to NULL if it still
4809 * references the chunk being closed. The channel may
4810 * reference a newer channel in the case of a session
4811 * rotation. When a session rotation occurs, the "next"
4812 * chunk is created before the "current" chunk is closed.
4814 if (channel
->trace_chunk
!= chunk
) {
4817 ret
= lttng_consumer_channel_set_trace_chunk(channel
, NULL
);
4820 * Attempt to close the chunk on as many channels as
4823 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4829 struct consumer_relayd_sock_pair
*relayd
;
4831 relayd
= consumer_find_relayd(*relayd_id
);
4833 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4834 ret
= relayd_close_trace_chunk(
4835 &relayd
->control_sock
, chunk
,
4837 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4839 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
,
4843 if (!relayd
|| ret
) {
4844 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4852 * Release the reference returned by the "find" operation and
4853 * the session daemon's implicit reference to the chunk.
4855 lttng_trace_chunk_put(chunk
);
4856 lttng_trace_chunk_put(chunk
);
4861 enum lttcomm_return_code
lttng_consumer_trace_chunk_exists(
4862 const uint64_t *relayd_id
, uint64_t session_id
,
4866 enum lttcomm_return_code ret_code
;
4867 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4868 const char *relayd_id_str
= "(none)";
4869 const bool is_local_trace
= !relayd_id
;
4870 struct consumer_relayd_sock_pair
*relayd
= NULL
;
4871 bool chunk_exists_local
, chunk_exists_remote
;
4876 /* Only used for logging purposes. */
4877 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4878 "%" PRIu64
, *relayd_id
);
4879 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4880 relayd_id_str
= relayd_id_buffer
;
4882 relayd_id_str
= "(formatting error)";
4886 DBG("Consumer trace chunk exists command: relayd_id = %s"
4887 ", chunk_id = %" PRIu64
, relayd_id_str
,
4889 ret
= lttng_trace_chunk_registry_chunk_exists(
4890 consumer_data
.chunk_registry
, session_id
,
4891 chunk_id
, &chunk_exists_local
);
4893 /* Internal error. */
4894 ERR("Failed to query the existence of a trace chunk");
4895 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
4898 DBG("Trace chunk %s locally",
4899 chunk_exists_local
? "exists" : "does not exist");
4900 if (chunk_exists_local
) {
4901 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
4903 } else if (is_local_trace
) {
4904 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4909 relayd
= consumer_find_relayd(*relayd_id
);
4911 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
4912 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
4913 goto end_rcu_unlock
;
4915 DBG("Looking up existence of trace chunk on relay daemon");
4916 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4917 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
,
4918 &chunk_exists_remote
);
4919 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4921 ERR("Failed to look-up the existence of trace chunk on relay daemon");
4922 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
4923 goto end_rcu_unlock
;
4926 ret_code
= chunk_exists_remote
?
4927 LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
4928 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4929 DBG("Trace chunk %s on relay daemon",
4930 chunk_exists_remote
? "exists" : "does not exist");
4939 int consumer_clear_monitored_channel(struct lttng_consumer_channel
*channel
)
4941 struct lttng_ht
*ht
;
4942 struct lttng_consumer_stream
*stream
;
4943 struct lttng_ht_iter iter
;
4946 ht
= consumer_data
.stream_per_chan_id_ht
;
4949 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4950 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4951 ht
->match_fct
, &channel
->key
,
4952 &iter
.iter
, stream
, node_channel_id
.node
) {
4954 * Protect against teardown with mutex.
4956 pthread_mutex_lock(&stream
->lock
);
4957 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
4960 ret
= consumer_clear_stream(stream
);
4965 pthread_mutex_unlock(&stream
->lock
);
4968 return LTTCOMM_CONSUMERD_SUCCESS
;
4971 pthread_mutex_unlock(&stream
->lock
);
4976 int lttng_consumer_clear_channel(struct lttng_consumer_channel
*channel
)
4980 DBG("Consumer clear channel %" PRIu64
, channel
->key
);
4982 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
4984 * Nothing to do for the metadata channel/stream.
4985 * Snapshot mechanism already take care of the metadata
4986 * handling/generation, and monitored channels only need to
4987 * have their data stream cleared..
4989 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4993 if (!channel
->monitor
) {
4994 ret
= consumer_clear_unmonitored_channel(channel
);
4996 ret
= consumer_clear_monitored_channel(channel
);