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
;
95 * Notify a thread lttng pipe to poll back again. This usually means that some
96 * global state has changed so we just send back the thread in a poll wait
99 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
101 struct lttng_consumer_stream
*null_stream
= NULL
;
105 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
108 static void notify_health_quit_pipe(int *pipe
)
112 ret
= lttng_write(pipe
[1], "4", 1);
114 PERROR("write consumer health quit");
118 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
119 struct lttng_consumer_channel
*chan
,
121 enum consumer_channel_action action
)
123 struct consumer_channel_msg msg
;
126 memset(&msg
, 0, sizeof(msg
));
131 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
132 if (ret
< sizeof(msg
)) {
133 PERROR("notify_channel_pipe write error");
137 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
140 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
143 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
144 struct lttng_consumer_channel
**chan
,
146 enum consumer_channel_action
*action
)
148 struct consumer_channel_msg msg
;
151 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
152 if (ret
< sizeof(msg
)) {
156 *action
= msg
.action
;
164 * Cleanup the stream list of a channel. Those streams are not yet globally
167 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
169 struct lttng_consumer_stream
*stream
, *stmp
;
173 /* Delete streams that might have been left in the stream list. */
174 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
176 cds_list_del(&stream
->send_node
);
178 * Once a stream is added to this list, the buffers were created so we
179 * have a guarantee that this call will succeed. Setting the monitor
180 * mode to 0 so we don't lock nor try to delete the stream from the
184 consumer_stream_destroy(stream
, NULL
);
189 * Find a stream. The consumer_data.lock must be locked during this
192 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
195 struct lttng_ht_iter iter
;
196 struct lttng_ht_node_u64
*node
;
197 struct lttng_consumer_stream
*stream
= NULL
;
201 /* -1ULL keys are lookup failures */
202 if (key
== (uint64_t) -1ULL) {
208 lttng_ht_lookup(ht
, &key
, &iter
);
209 node
= lttng_ht_iter_get_node_u64(&iter
);
211 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
219 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
221 struct lttng_consumer_stream
*stream
;
224 stream
= find_stream(key
, ht
);
226 stream
->key
= (uint64_t) -1ULL;
228 * We don't want the lookup to match, but we still need
229 * to iterate on this stream when iterating over the hash table. Just
230 * change the node key.
232 stream
->node
.key
= (uint64_t) -1ULL;
238 * Return a channel object for the given key.
240 * RCU read side lock MUST be acquired before calling this function and
241 * protects the channel ptr.
243 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
245 struct lttng_ht_iter iter
;
246 struct lttng_ht_node_u64
*node
;
247 struct lttng_consumer_channel
*channel
= NULL
;
249 /* -1ULL keys are lookup failures */
250 if (key
== (uint64_t) -1ULL) {
254 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
255 node
= lttng_ht_iter_get_node_u64(&iter
);
257 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
264 * There is a possibility that the consumer does not have enough time between
265 * the close of the channel on the session daemon and the cleanup in here thus
266 * once we have a channel add with an existing key, we know for sure that this
267 * channel will eventually get cleaned up by all streams being closed.
269 * This function just nullifies the already existing channel key.
271 static void steal_channel_key(uint64_t key
)
273 struct lttng_consumer_channel
*channel
;
276 channel
= consumer_find_channel(key
);
278 channel
->key
= (uint64_t) -1ULL;
280 * We don't want the lookup to match, but we still need to iterate on
281 * this channel when iterating over the hash table. Just change the
284 channel
->node
.key
= (uint64_t) -1ULL;
289 static void free_channel_rcu(struct rcu_head
*head
)
291 struct lttng_ht_node_u64
*node
=
292 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
293 struct lttng_consumer_channel
*channel
=
294 caa_container_of(node
, struct lttng_consumer_channel
, node
);
296 switch (consumer_data
.type
) {
297 case LTTNG_CONSUMER_KERNEL
:
299 case LTTNG_CONSUMER32_UST
:
300 case LTTNG_CONSUMER64_UST
:
301 lttng_ustconsumer_free_channel(channel
);
304 ERR("Unknown consumer_data type");
311 * RCU protected relayd socket pair free.
313 static void free_relayd_rcu(struct rcu_head
*head
)
315 struct lttng_ht_node_u64
*node
=
316 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
317 struct consumer_relayd_sock_pair
*relayd
=
318 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
321 * Close all sockets. This is done in the call RCU since we don't want the
322 * socket fds to be reassigned thus potentially creating bad state of the
325 * We do not have to lock the control socket mutex here since at this stage
326 * there is no one referencing to this relayd object.
328 (void) relayd_close(&relayd
->control_sock
);
329 (void) relayd_close(&relayd
->data_sock
);
331 pthread_mutex_destroy(&relayd
->ctrl_sock_mutex
);
336 * Destroy and free relayd socket pair object.
338 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
341 struct lttng_ht_iter iter
;
343 if (relayd
== NULL
) {
347 DBG("Consumer destroy and close relayd socket pair");
349 iter
.iter
.node
= &relayd
->node
.node
;
350 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
352 /* We assume the relayd is being or is destroyed */
356 /* RCU free() call */
357 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
361 * Remove a channel from the global list protected by a mutex. This function is
362 * also responsible for freeing its data structures.
364 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
366 struct lttng_ht_iter iter
;
368 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
370 pthread_mutex_lock(&consumer_data
.lock
);
371 pthread_mutex_lock(&channel
->lock
);
373 /* Destroy streams that might have been left in the stream list. */
374 clean_channel_stream_list(channel
);
376 if (channel
->live_timer_enabled
== 1) {
377 consumer_timer_live_stop(channel
);
379 if (channel
->monitor_timer_enabled
== 1) {
380 consumer_timer_monitor_stop(channel
);
383 switch (consumer_data
.type
) {
384 case LTTNG_CONSUMER_KERNEL
:
386 case LTTNG_CONSUMER32_UST
:
387 case LTTNG_CONSUMER64_UST
:
388 lttng_ustconsumer_del_channel(channel
);
391 ERR("Unknown consumer_data type");
396 lttng_trace_chunk_put(channel
->trace_chunk
);
397 channel
->trace_chunk
= NULL
;
399 if (channel
->is_published
) {
403 iter
.iter
.node
= &channel
->node
.node
;
404 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
407 iter
.iter
.node
= &channel
->channels_by_session_id_ht_node
.node
;
408 ret
= lttng_ht_del(consumer_data
.channels_by_session_id_ht
,
414 channel
->is_deleted
= true;
415 call_rcu(&channel
->node
.head
, free_channel_rcu
);
417 pthread_mutex_unlock(&channel
->lock
);
418 pthread_mutex_unlock(&consumer_data
.lock
);
422 * Iterate over the relayd hash table and destroy each element. Finally,
423 * destroy the whole hash table.
425 static void cleanup_relayd_ht(void)
427 struct lttng_ht_iter iter
;
428 struct consumer_relayd_sock_pair
*relayd
;
432 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
434 consumer_destroy_relayd(relayd
);
439 lttng_ht_destroy(consumer_data
.relayd_ht
);
443 * Update the end point status of all streams having the given network sequence
444 * index (relayd index).
446 * It's atomically set without having the stream mutex locked which is fine
447 * because we handle the write/read race with a pipe wakeup for each thread.
449 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
450 enum consumer_endpoint_status status
)
452 struct lttng_ht_iter iter
;
453 struct lttng_consumer_stream
*stream
;
455 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
459 /* Let's begin with metadata */
460 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
461 if (stream
->net_seq_idx
== net_seq_idx
) {
462 uatomic_set(&stream
->endpoint_status
, status
);
463 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
467 /* Follow up by the data streams */
468 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
469 if (stream
->net_seq_idx
== net_seq_idx
) {
470 uatomic_set(&stream
->endpoint_status
, status
);
471 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
478 * Cleanup a relayd object by flagging every associated streams for deletion,
479 * destroying the object meaning removing it from the relayd hash table,
480 * closing the sockets and freeing the memory in a RCU call.
482 * If a local data context is available, notify the threads that the streams'
483 * state have changed.
485 void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
)
491 DBG("Cleaning up relayd object ID %"PRIu64
, relayd
->net_seq_idx
);
493 /* Save the net sequence index before destroying the object */
494 netidx
= relayd
->net_seq_idx
;
497 * Delete the relayd from the relayd hash table, close the sockets and free
498 * the object in a RCU call.
500 consumer_destroy_relayd(relayd
);
502 /* Set inactive endpoint to all streams */
503 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
506 * With a local data context, notify the threads that the streams' state
507 * have changed. The write() action on the pipe acts as an "implicit"
508 * memory barrier ordering the updates of the end point status from the
509 * read of this status which happens AFTER receiving this notify.
511 notify_thread_lttng_pipe(relayd
->ctx
->consumer_data_pipe
);
512 notify_thread_lttng_pipe(relayd
->ctx
->consumer_metadata_pipe
);
516 * Flag a relayd socket pair for destruction. Destroy it if the refcount
519 * RCU read side lock MUST be aquired before calling this function.
521 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
525 /* Set destroy flag for this object */
526 uatomic_set(&relayd
->destroy_flag
, 1);
528 /* Destroy the relayd if refcount is 0 */
529 if (uatomic_read(&relayd
->refcount
) == 0) {
530 consumer_destroy_relayd(relayd
);
535 * Completly destroy stream from every visiable data structure and the given
538 * One this call returns, the stream object is not longer usable nor visible.
540 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
543 consumer_stream_destroy(stream
, ht
);
547 * XXX naming of del vs destroy is all mixed up.
549 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
551 consumer_stream_destroy(stream
, data_ht
);
554 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
556 consumer_stream_destroy(stream
, metadata_ht
);
559 void consumer_stream_update_channel_attributes(
560 struct lttng_consumer_stream
*stream
,
561 struct lttng_consumer_channel
*channel
)
563 stream
->channel_read_only_attributes
.tracefile_size
=
564 channel
->tracefile_size
;
567 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
569 const char *channel_name
,
572 struct lttng_trace_chunk
*trace_chunk
,
575 enum consumer_channel_type type
,
576 unsigned int monitor
)
579 struct lttng_consumer_stream
*stream
;
581 stream
= zmalloc(sizeof(*stream
));
582 if (stream
== NULL
) {
583 PERROR("malloc struct lttng_consumer_stream");
588 if (trace_chunk
&& !lttng_trace_chunk_get(trace_chunk
)) {
589 ERR("Failed to acquire trace chunk reference during the creation of a stream");
595 stream
->key
= stream_key
;
596 stream
->trace_chunk
= trace_chunk
;
598 stream
->out_fd_offset
= 0;
599 stream
->output_written
= 0;
600 stream
->net_seq_idx
= relayd_id
;
601 stream
->session_id
= session_id
;
602 stream
->monitor
= monitor
;
603 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
604 stream
->index_file
= NULL
;
605 stream
->last_sequence_number
= -1ULL;
606 pthread_mutex_init(&stream
->lock
, NULL
);
607 pthread_mutex_init(&stream
->metadata_timer_lock
, NULL
);
609 /* If channel is the metadata, flag this stream as metadata. */
610 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
611 stream
->metadata_flag
= 1;
612 /* Metadata is flat out. */
613 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
614 /* Live rendez-vous point. */
615 pthread_cond_init(&stream
->metadata_rdv
, NULL
);
616 pthread_mutex_init(&stream
->metadata_rdv_lock
, NULL
);
618 /* Format stream name to <channel_name>_<cpu_number> */
619 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
622 PERROR("snprintf stream name");
627 /* Key is always the wait_fd for streams. */
628 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
630 /* Init node per channel id key */
631 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
633 /* Init session id node with the stream session id */
634 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
636 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
637 " relayd_id %" PRIu64
", session_id %" PRIu64
,
638 stream
->name
, stream
->key
, channel_key
,
639 stream
->net_seq_idx
, stream
->session_id
);
646 lttng_trace_chunk_put(stream
->trace_chunk
);
656 * Add a stream to the global list protected by a mutex.
658 void consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
660 struct lttng_ht
*ht
= data_ht
;
665 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
667 pthread_mutex_lock(&consumer_data
.lock
);
668 pthread_mutex_lock(&stream
->chan
->lock
);
669 pthread_mutex_lock(&stream
->chan
->timer_lock
);
670 pthread_mutex_lock(&stream
->lock
);
673 /* Steal stream identifier to avoid having streams with the same key */
674 steal_stream_key(stream
->key
, ht
);
676 lttng_ht_add_unique_u64(ht
, &stream
->node
);
678 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
679 &stream
->node_channel_id
);
682 * Add stream to the stream_list_ht of the consumer data. No need to steal
683 * the key since the HT does not use it and we allow to add redundant keys
686 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
689 * When nb_init_stream_left reaches 0, we don't need to trigger any action
690 * in terms of destroying the associated channel, because the action that
691 * causes the count to become 0 also causes a stream to be added. The
692 * channel deletion will thus be triggered by the following removal of this
695 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
696 /* Increment refcount before decrementing nb_init_stream_left */
698 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
701 /* Update consumer data once the node is inserted. */
702 consumer_data
.stream_count
++;
703 consumer_data
.need_update
= 1;
706 pthread_mutex_unlock(&stream
->lock
);
707 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
708 pthread_mutex_unlock(&stream
->chan
->lock
);
709 pthread_mutex_unlock(&consumer_data
.lock
);
712 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
714 consumer_del_stream(stream
, data_ht
);
718 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
719 * be acquired before calling this.
721 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
724 struct lttng_ht_node_u64
*node
;
725 struct lttng_ht_iter iter
;
729 lttng_ht_lookup(consumer_data
.relayd_ht
,
730 &relayd
->net_seq_idx
, &iter
);
731 node
= lttng_ht_iter_get_node_u64(&iter
);
735 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
742 * Allocate and return a consumer relayd socket.
744 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
745 uint64_t net_seq_idx
)
747 struct consumer_relayd_sock_pair
*obj
= NULL
;
749 /* net sequence index of -1 is a failure */
750 if (net_seq_idx
== (uint64_t) -1ULL) {
754 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
756 PERROR("zmalloc relayd sock");
760 obj
->net_seq_idx
= net_seq_idx
;
762 obj
->destroy_flag
= 0;
763 obj
->control_sock
.sock
.fd
= -1;
764 obj
->data_sock
.sock
.fd
= -1;
765 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
766 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
773 * Find a relayd socket pair in the global consumer data.
775 * Return the object if found else NULL.
776 * RCU read-side lock must be held across this call and while using the
779 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
781 struct lttng_ht_iter iter
;
782 struct lttng_ht_node_u64
*node
;
783 struct consumer_relayd_sock_pair
*relayd
= NULL
;
785 /* Negative keys are lookup failures */
786 if (key
== (uint64_t) -1ULL) {
790 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
792 node
= lttng_ht_iter_get_node_u64(&iter
);
794 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
802 * Find a relayd and send the stream
804 * Returns 0 on success, < 0 on error
806 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
810 struct consumer_relayd_sock_pair
*relayd
;
813 assert(stream
->net_seq_idx
!= -1ULL);
816 /* The stream is not metadata. Get relayd reference if exists. */
818 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
819 if (relayd
!= NULL
) {
820 /* Add stream on the relayd */
821 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
822 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
823 path
, &stream
->relayd_stream_id
,
824 stream
->chan
->tracefile_size
,
825 stream
->chan
->tracefile_count
,
826 stream
->trace_chunk
);
827 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
829 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
830 lttng_consumer_cleanup_relayd(relayd
);
834 uatomic_inc(&relayd
->refcount
);
835 stream
->sent_to_relayd
= 1;
837 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
838 stream
->key
, stream
->net_seq_idx
);
843 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
844 stream
->name
, stream
->key
, stream
->net_seq_idx
);
852 * Find a relayd and send the streams sent message
854 * Returns 0 on success, < 0 on error
856 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
859 struct consumer_relayd_sock_pair
*relayd
;
861 assert(net_seq_idx
!= -1ULL);
863 /* The stream is not metadata. Get relayd reference if exists. */
865 relayd
= consumer_find_relayd(net_seq_idx
);
866 if (relayd
!= NULL
) {
867 /* Add stream on the relayd */
868 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
869 ret
= relayd_streams_sent(&relayd
->control_sock
);
870 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
872 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
873 lttng_consumer_cleanup_relayd(relayd
);
877 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
884 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
892 * Find a relayd and close the stream
894 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
896 struct consumer_relayd_sock_pair
*relayd
;
898 /* The stream is not metadata. Get relayd reference if exists. */
900 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
902 consumer_stream_relayd_close(stream
, relayd
);
908 * Handle stream for relayd transmission if the stream applies for network
909 * streaming where the net sequence index is set.
911 * Return destination file descriptor or negative value on error.
913 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
914 size_t data_size
, unsigned long padding
,
915 struct consumer_relayd_sock_pair
*relayd
)
918 struct lttcomm_relayd_data_hdr data_hdr
;
924 /* Reset data header */
925 memset(&data_hdr
, 0, sizeof(data_hdr
));
927 if (stream
->metadata_flag
) {
928 /* Caller MUST acquire the relayd control socket lock */
929 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
934 /* Metadata are always sent on the control socket. */
935 outfd
= relayd
->control_sock
.sock
.fd
;
937 /* Set header with stream information */
938 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
939 data_hdr
.data_size
= htobe32(data_size
);
940 data_hdr
.padding_size
= htobe32(padding
);
943 * Note that net_seq_num below is assigned with the *current* value of
944 * next_net_seq_num and only after that the next_net_seq_num will be
945 * increment. This is why when issuing a command on the relayd using
946 * this next value, 1 should always be substracted in order to compare
947 * the last seen sequence number on the relayd side to the last sent.
949 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
950 /* Other fields are zeroed previously */
952 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
958 ++stream
->next_net_seq_num
;
960 /* Set to go on data socket */
961 outfd
= relayd
->data_sock
.sock
.fd
;
969 * Trigger a dump of the metadata content. Following/during the succesful
970 * completion of this call, the metadata poll thread will start receiving
971 * metadata packets to consume.
973 * The caller must hold the channel and stream locks.
976 int consumer_metadata_stream_dump(struct lttng_consumer_stream
*stream
)
980 ASSERT_LOCKED(stream
->chan
->lock
);
981 ASSERT_LOCKED(stream
->lock
);
982 assert(stream
->metadata_flag
);
983 assert(stream
->chan
->trace_chunk
);
985 switch (consumer_data
.type
) {
986 case LTTNG_CONSUMER_KERNEL
:
988 * Reset the position of what has been read from the
989 * metadata cache to 0 so we can dump it again.
991 ret
= kernctl_metadata_cache_dump(stream
->wait_fd
);
993 case LTTNG_CONSUMER32_UST
:
994 case LTTNG_CONSUMER64_UST
:
996 * Reset the position pushed from the metadata cache so it
997 * will write from the beginning on the next push.
999 stream
->ust_metadata_pushed
= 0;
1000 ret
= consumer_metadata_wakeup_pipe(stream
->chan
);
1003 ERR("Unknown consumer_data type");
1007 ERR("Failed to dump the metadata cache");
1013 int lttng_consumer_channel_set_trace_chunk(
1014 struct lttng_consumer_channel
*channel
,
1015 struct lttng_trace_chunk
*new_trace_chunk
)
1018 const bool is_local_trace
= channel
->relayd_id
== -1ULL;
1019 bool update_stream_trace_chunk
;
1020 struct cds_lfht_iter iter
;
1021 struct lttng_consumer_stream
*stream
;
1022 unsigned long channel_hash
;
1024 pthread_mutex_lock(&channel
->lock
);
1025 if (channel
->is_deleted
) {
1027 * The channel has been logically deleted and should no longer
1028 * be used. It has released its reference to its current trace
1029 * chunk and should not acquire a new one.
1031 * Return success as there is nothing for the caller to do.
1036 * A stream can transition to a state where it and its channel
1037 * no longer belong to a trace chunk. For instance, this happens when
1038 * a session is rotated while it is inactive. After the rotation
1039 * of an inactive session completes, the channel and its streams no
1040 * longer belong to a trace chunk.
1042 * However, if a session is stopped, rotated, and started again,
1043 * the session daemon will create a new chunk and send it to its peers.
1044 * In that case, the streams' transition to a new chunk can be performed
1047 * This trace chunk transition could also be performed lazily when
1048 * a buffer is consumed. However, creating the files here allows the
1049 * consumer daemon to report any creation error to the session daemon
1050 * and cause the start of the tracing session to fail.
1052 update_stream_trace_chunk
= !channel
->trace_chunk
&& new_trace_chunk
;
1055 * The acquisition of the reference cannot fail (barring
1056 * a severe internal error) since a reference to the published
1057 * chunk is already held by the caller.
1059 if (new_trace_chunk
) {
1060 const bool acquired_reference
= lttng_trace_chunk_get(
1063 assert(acquired_reference
);
1066 lttng_trace_chunk_put(channel
->trace_chunk
);
1067 channel
->trace_chunk
= new_trace_chunk
;
1068 if (!is_local_trace
|| !new_trace_chunk
) {
1073 if (!update_stream_trace_chunk
) {
1077 channel_hash
= consumer_data
.stream_per_chan_id_ht
->hash_fct(
1078 &channel
->key
, lttng_ht_seed
);
1080 cds_lfht_for_each_entry_duplicate(consumer_data
.stream_per_chan_id_ht
->ht
,
1082 consumer_data
.stream_per_chan_id_ht
->match_fct
,
1083 &channel
->key
, &iter
, stream
, node_channel_id
.node
) {
1084 bool acquired_reference
, should_regenerate_metadata
= false;
1086 acquired_reference
= lttng_trace_chunk_get(channel
->trace_chunk
);
1087 assert(acquired_reference
);
1089 pthread_mutex_lock(&stream
->lock
);
1092 * On a transition from "no-chunk" to a new chunk, a metadata
1093 * stream's content must be entirely dumped. This must occcur
1094 * _after_ the creation of the metadata stream's output files
1095 * as the consumption thread (not necessarily the one executing
1096 * this) may start to consume during the call to
1097 * consumer_metadata_stream_dump().
1099 should_regenerate_metadata
=
1100 stream
->metadata_flag
&&
1101 !stream
->trace_chunk
&& channel
->trace_chunk
;
1102 stream
->trace_chunk
= channel
->trace_chunk
;
1103 ret
= consumer_stream_create_output_files(stream
, true);
1105 pthread_mutex_unlock(&stream
->lock
);
1106 goto end_rcu_unlock
;
1108 if (should_regenerate_metadata
) {
1109 ret
= consumer_metadata_stream_dump(stream
);
1111 pthread_mutex_unlock(&stream
->lock
);
1113 goto end_rcu_unlock
;
1119 pthread_mutex_unlock(&channel
->lock
);
1124 * Allocate and return a new lttng_consumer_channel object using the given key
1125 * to initialize the hash table node.
1127 * On error, return NULL.
1129 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
1130 uint64_t session_id
,
1131 const uint64_t *chunk_id
,
1132 const char *pathname
,
1135 enum lttng_event_output output
,
1136 uint64_t tracefile_size
,
1137 uint64_t tracefile_count
,
1138 uint64_t session_id_per_pid
,
1139 unsigned int monitor
,
1140 unsigned int live_timer_interval
,
1141 const char *root_shm_path
,
1142 const char *shm_path
)
1144 struct lttng_consumer_channel
*channel
= NULL
;
1145 struct lttng_trace_chunk
*trace_chunk
= NULL
;
1148 trace_chunk
= lttng_trace_chunk_registry_find_chunk(
1149 consumer_data
.chunk_registry
, session_id
,
1152 ERR("Failed to find trace chunk reference during creation of channel");
1157 channel
= zmalloc(sizeof(*channel
));
1158 if (channel
== NULL
) {
1159 PERROR("malloc struct lttng_consumer_channel");
1164 channel
->refcount
= 0;
1165 channel
->session_id
= session_id
;
1166 channel
->session_id_per_pid
= session_id_per_pid
;
1167 channel
->relayd_id
= relayd_id
;
1168 channel
->tracefile_size
= tracefile_size
;
1169 channel
->tracefile_count
= tracefile_count
;
1170 channel
->monitor
= monitor
;
1171 channel
->live_timer_interval
= live_timer_interval
;
1172 pthread_mutex_init(&channel
->lock
, NULL
);
1173 pthread_mutex_init(&channel
->timer_lock
, NULL
);
1176 case LTTNG_EVENT_SPLICE
:
1177 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
1179 case LTTNG_EVENT_MMAP
:
1180 channel
->output
= CONSUMER_CHANNEL_MMAP
;
1190 * In monitor mode, the streams associated with the channel will be put in
1191 * a special list ONLY owned by this channel. So, the refcount is set to 1
1192 * here meaning that the channel itself has streams that are referenced.
1194 * On a channel deletion, once the channel is no longer visible, the
1195 * refcount is decremented and checked for a zero value to delete it. With
1196 * streams in no monitor mode, it will now be safe to destroy the channel.
1198 if (!channel
->monitor
) {
1199 channel
->refcount
= 1;
1202 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1203 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1205 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1206 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1208 if (root_shm_path
) {
1209 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1210 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1213 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1214 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1217 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1218 lttng_ht_node_init_u64(&channel
->channels_by_session_id_ht_node
,
1219 channel
->session_id
);
1221 channel
->wait_fd
= -1;
1222 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1225 int ret
= lttng_consumer_channel_set_trace_chunk(channel
,
1232 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1235 lttng_trace_chunk_put(trace_chunk
);
1238 consumer_del_channel(channel
);
1244 * Add a channel to the global list protected by a mutex.
1246 * Always return 0 indicating success.
1248 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1249 struct lttng_consumer_local_data
*ctx
)
1251 pthread_mutex_lock(&consumer_data
.lock
);
1252 pthread_mutex_lock(&channel
->lock
);
1253 pthread_mutex_lock(&channel
->timer_lock
);
1256 * This gives us a guarantee that the channel we are about to add to the
1257 * channel hash table will be unique. See this function comment on the why
1258 * we need to steel the channel key at this stage.
1260 steal_channel_key(channel
->key
);
1263 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1264 lttng_ht_add_u64(consumer_data
.channels_by_session_id_ht
,
1265 &channel
->channels_by_session_id_ht_node
);
1267 channel
->is_published
= true;
1269 pthread_mutex_unlock(&channel
->timer_lock
);
1270 pthread_mutex_unlock(&channel
->lock
);
1271 pthread_mutex_unlock(&consumer_data
.lock
);
1273 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1274 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1281 * Allocate the pollfd structure and the local view of the out fds to avoid
1282 * doing a lookup in the linked list and concurrency issues when writing is
1283 * needed. Called with consumer_data.lock held.
1285 * Returns the number of fds in the structures.
1287 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1288 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1289 struct lttng_ht
*ht
, int *nb_inactive_fd
)
1292 struct lttng_ht_iter iter
;
1293 struct lttng_consumer_stream
*stream
;
1298 assert(local_stream
);
1300 DBG("Updating poll fd array");
1301 *nb_inactive_fd
= 0;
1303 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1305 * Only active streams with an active end point can be added to the
1306 * poll set and local stream storage of the thread.
1308 * There is a potential race here for endpoint_status to be updated
1309 * just after the check. However, this is OK since the stream(s) will
1310 * be deleted once the thread is notified that the end point state has
1311 * changed where this function will be called back again.
1313 * We track the number of inactive FDs because they still need to be
1314 * closed by the polling thread after a wakeup on the data_pipe or
1317 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1318 (*nb_inactive_fd
)++;
1322 * This clobbers way too much the debug output. Uncomment that if you
1323 * need it for debugging purposes.
1325 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1326 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1327 local_stream
[i
] = stream
;
1333 * Insert the consumer_data_pipe at the end of the array and don't
1334 * increment i so nb_fd is the number of real FD.
1336 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1337 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1339 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1340 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1345 * Poll on the should_quit pipe and the command socket return -1 on
1346 * error, 1 if should exit, 0 if data is available on the command socket
1348 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1353 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1354 if (num_rdy
== -1) {
1356 * Restart interrupted system call.
1358 if (errno
== EINTR
) {
1361 PERROR("Poll error");
1364 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1365 DBG("consumer_should_quit wake up");
1372 * Set the error socket.
1374 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1377 ctx
->consumer_error_socket
= sock
;
1381 * Set the command socket path.
1383 void lttng_consumer_set_command_sock_path(
1384 struct lttng_consumer_local_data
*ctx
, char *sock
)
1386 ctx
->consumer_command_sock_path
= sock
;
1390 * Send return code to the session daemon.
1391 * If the socket is not defined, we return 0, it is not a fatal error
1393 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1395 if (ctx
->consumer_error_socket
> 0) {
1396 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1397 sizeof(enum lttcomm_sessiond_command
));
1404 * Close all the tracefiles and stream fds and MUST be called when all
1405 * instances are destroyed i.e. when all threads were joined and are ended.
1407 void lttng_consumer_cleanup(void)
1409 struct lttng_ht_iter iter
;
1410 struct lttng_consumer_channel
*channel
;
1411 unsigned int trace_chunks_left
;
1415 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1417 consumer_del_channel(channel
);
1422 lttng_ht_destroy(consumer_data
.channel_ht
);
1423 lttng_ht_destroy(consumer_data
.channels_by_session_id_ht
);
1425 cleanup_relayd_ht();
1427 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1430 * This HT contains streams that are freed by either the metadata thread or
1431 * the data thread so we do *nothing* on the hash table and simply destroy
1434 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1437 * Trace chunks in the registry may still exist if the session
1438 * daemon has encountered an internal error and could not
1439 * tear down its sessions and/or trace chunks properly.
1441 * Release the session daemon's implicit reference to any remaining
1442 * trace chunk and print an error if any trace chunk was found. Note
1443 * that there are _no_ legitimate cases for trace chunks to be left,
1444 * it is a leak. However, it can happen following a crash of the
1445 * session daemon and not emptying the registry would cause an assertion
1448 trace_chunks_left
= lttng_trace_chunk_registry_put_each_chunk(
1449 consumer_data
.chunk_registry
);
1450 if (trace_chunks_left
) {
1451 ERR("%u trace chunks are leaked by lttng-consumerd. "
1452 "This can be caused by an internal error of the session daemon.",
1455 /* Run all callbacks freeing each chunk. */
1457 lttng_trace_chunk_registry_destroy(consumer_data
.chunk_registry
);
1461 * Called from signal handler.
1463 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1467 CMM_STORE_SHARED(consumer_quit
, 1);
1468 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1470 PERROR("write consumer quit");
1473 DBG("Consumer flag that it should quit");
1478 * Flush pending writes to trace output disk file.
1481 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1485 int outfd
= stream
->out_fd
;
1488 * This does a blocking write-and-wait on any page that belongs to the
1489 * subbuffer prior to the one we just wrote.
1490 * Don't care about error values, as these are just hints and ways to
1491 * limit the amount of page cache used.
1493 if (orig_offset
< stream
->max_sb_size
) {
1496 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1497 stream
->max_sb_size
,
1498 SYNC_FILE_RANGE_WAIT_BEFORE
1499 | SYNC_FILE_RANGE_WRITE
1500 | SYNC_FILE_RANGE_WAIT_AFTER
);
1502 * Give hints to the kernel about how we access the file:
1503 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1506 * We need to call fadvise again after the file grows because the
1507 * kernel does not seem to apply fadvise to non-existing parts of the
1510 * Call fadvise _after_ having waited for the page writeback to
1511 * complete because the dirty page writeback semantic is not well
1512 * defined. So it can be expected to lead to lower throughput in
1515 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1516 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1517 if (ret
&& ret
!= -ENOSYS
) {
1519 PERROR("posix_fadvise on fd %i", outfd
);
1524 * Initialise the necessary environnement :
1525 * - create a new context
1526 * - create the poll_pipe
1527 * - create the should_quit pipe (for signal handler)
1528 * - create the thread pipe (for splice)
1530 * Takes a function pointer as argument, this function is called when data is
1531 * available on a buffer. This function is responsible to do the
1532 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1533 * buffer configuration and then kernctl_put_next_subbuf at the end.
1535 * Returns a pointer to the new context or NULL on error.
1537 struct lttng_consumer_local_data
*lttng_consumer_create(
1538 enum lttng_consumer_type type
,
1539 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1540 struct lttng_consumer_local_data
*ctx
),
1541 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1542 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1543 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1546 struct lttng_consumer_local_data
*ctx
;
1548 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1549 consumer_data
.type
== type
);
1550 consumer_data
.type
= type
;
1552 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1554 PERROR("allocating context");
1558 ctx
->consumer_error_socket
= -1;
1559 ctx
->consumer_metadata_socket
= -1;
1560 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1561 /* assign the callbacks */
1562 ctx
->on_buffer_ready
= buffer_ready
;
1563 ctx
->on_recv_channel
= recv_channel
;
1564 ctx
->on_recv_stream
= recv_stream
;
1565 ctx
->on_update_stream
= update_stream
;
1567 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1568 if (!ctx
->consumer_data_pipe
) {
1569 goto error_poll_pipe
;
1572 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1573 if (!ctx
->consumer_wakeup_pipe
) {
1574 goto error_wakeup_pipe
;
1577 ret
= pipe(ctx
->consumer_should_quit
);
1579 PERROR("Error creating recv pipe");
1580 goto error_quit_pipe
;
1583 ret
= pipe(ctx
->consumer_channel_pipe
);
1585 PERROR("Error creating channel pipe");
1586 goto error_channel_pipe
;
1589 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1590 if (!ctx
->consumer_metadata_pipe
) {
1591 goto error_metadata_pipe
;
1594 ctx
->channel_monitor_pipe
= -1;
1598 error_metadata_pipe
:
1599 utils_close_pipe(ctx
->consumer_channel_pipe
);
1601 utils_close_pipe(ctx
->consumer_should_quit
);
1603 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1605 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1613 * Iterate over all streams of the hashtable and free them properly.
1615 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1617 struct lttng_ht_iter iter
;
1618 struct lttng_consumer_stream
*stream
;
1625 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1627 * Ignore return value since we are currently cleaning up so any error
1630 (void) consumer_del_stream(stream
, ht
);
1634 lttng_ht_destroy(ht
);
1638 * Iterate over all streams of the metadata hashtable and free them
1641 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1643 struct lttng_ht_iter iter
;
1644 struct lttng_consumer_stream
*stream
;
1651 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1653 * Ignore return value since we are currently cleaning up so any error
1656 (void) consumer_del_metadata_stream(stream
, ht
);
1660 lttng_ht_destroy(ht
);
1664 * Close all fds associated with the instance and free the context.
1666 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1670 DBG("Consumer destroying it. Closing everything.");
1676 destroy_data_stream_ht(data_ht
);
1677 destroy_metadata_stream_ht(metadata_ht
);
1679 ret
= close(ctx
->consumer_error_socket
);
1683 ret
= close(ctx
->consumer_metadata_socket
);
1687 utils_close_pipe(ctx
->consumer_channel_pipe
);
1688 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1689 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1690 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1691 utils_close_pipe(ctx
->consumer_should_quit
);
1693 unlink(ctx
->consumer_command_sock_path
);
1698 * Write the metadata stream id on the specified file descriptor.
1700 static int write_relayd_metadata_id(int fd
,
1701 struct lttng_consumer_stream
*stream
,
1702 unsigned long padding
)
1705 struct lttcomm_relayd_metadata_payload hdr
;
1707 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1708 hdr
.padding_size
= htobe32(padding
);
1709 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1710 if (ret
< sizeof(hdr
)) {
1712 * This error means that the fd's end is closed so ignore the PERROR
1713 * not to clubber the error output since this can happen in a normal
1716 if (errno
!= EPIPE
) {
1717 PERROR("write metadata stream id");
1719 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1721 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1722 * handle writting the missing part so report that as an error and
1723 * don't lie to the caller.
1728 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1729 stream
->relayd_stream_id
, padding
);
1736 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1737 * core function for writing trace buffers to either the local filesystem or
1740 * It must be called with the stream and the channel lock held.
1742 * Careful review MUST be put if any changes occur!
1744 * Returns the number of bytes written
1746 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1747 struct lttng_consumer_local_data
*ctx
,
1748 struct lttng_consumer_stream
*stream
, unsigned long len
,
1749 unsigned long padding
,
1750 struct ctf_packet_index
*index
)
1752 unsigned long mmap_offset
;
1755 off_t orig_offset
= stream
->out_fd_offset
;
1756 /* Default is on the disk */
1757 int outfd
= stream
->out_fd
;
1758 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1759 unsigned int relayd_hang_up
= 0;
1761 /* RCU lock for the relayd pointer */
1763 assert(stream
->net_seq_idx
!= (uint64_t) -1ULL ||
1764 stream
->trace_chunk
);
1766 /* Flag that the current stream if set for network streaming. */
1767 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1768 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1769 if (relayd
== NULL
) {
1775 /* get the offset inside the fd to mmap */
1776 switch (consumer_data
.type
) {
1777 case LTTNG_CONSUMER_KERNEL
:
1778 mmap_base
= stream
->mmap_base
;
1779 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1781 PERROR("tracer ctl get_mmap_read_offset");
1785 case LTTNG_CONSUMER32_UST
:
1786 case LTTNG_CONSUMER64_UST
:
1787 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1789 ERR("read mmap get mmap base for stream %s", stream
->name
);
1793 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1795 PERROR("tracer ctl get_mmap_read_offset");
1801 ERR("Unknown consumer_data type");
1805 /* Handle stream on the relayd if the output is on the network */
1807 unsigned long netlen
= len
;
1810 * Lock the control socket for the complete duration of the function
1811 * since from this point on we will use the socket.
1813 if (stream
->metadata_flag
) {
1814 /* Metadata requires the control socket. */
1815 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1816 if (stream
->reset_metadata_flag
) {
1817 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1818 stream
->relayd_stream_id
,
1819 stream
->metadata_version
);
1824 stream
->reset_metadata_flag
= 0;
1826 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1829 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1834 /* Use the returned socket. */
1837 /* Write metadata stream id before payload */
1838 if (stream
->metadata_flag
) {
1839 ret
= write_relayd_metadata_id(outfd
, stream
, padding
);
1846 /* No streaming, we have to set the len with the full padding */
1849 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1850 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1852 ERR("Reset metadata file");
1855 stream
->reset_metadata_flag
= 0;
1859 * Check if we need to change the tracefile before writing the packet.
1861 if (stream
->chan
->tracefile_size
> 0 &&
1862 (stream
->tracefile_size_current
+ len
) >
1863 stream
->chan
->tracefile_size
) {
1864 ret
= consumer_stream_rotate_output_files(stream
);
1868 outfd
= stream
->out_fd
;
1871 stream
->tracefile_size_current
+= len
;
1873 index
->offset
= htobe64(stream
->out_fd_offset
);
1878 * This call guarantee that len or less is returned. It's impossible to
1879 * receive a ret value that is bigger than len.
1881 ret
= lttng_write(outfd
, mmap_base
+ mmap_offset
, len
);
1882 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1883 if (ret
< 0 || ((size_t) ret
!= len
)) {
1885 * Report error to caller if nothing was written else at least send the
1893 /* Socket operation failed. We consider the relayd dead */
1894 if (errno
== EPIPE
) {
1896 * This is possible if the fd is closed on the other side
1897 * (outfd) or any write problem. It can be verbose a bit for a
1898 * normal execution if for instance the relayd is stopped
1899 * abruptly. This can happen so set this to a DBG statement.
1901 DBG("Consumer mmap write detected relayd hang up");
1903 /* Unhandled error, print it and stop function right now. */
1904 PERROR("Error in write mmap (ret %zd != len %lu)", ret
, len
);
1908 stream
->output_written
+= ret
;
1910 /* This call is useless on a socket so better save a syscall. */
1912 /* This won't block, but will start writeout asynchronously */
1913 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, len
,
1914 SYNC_FILE_RANGE_WRITE
);
1915 stream
->out_fd_offset
+= len
;
1916 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1921 * This is a special case that the relayd has closed its socket. Let's
1922 * cleanup the relayd object and all associated streams.
1924 if (relayd
&& relayd_hang_up
) {
1925 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1926 lttng_consumer_cleanup_relayd(relayd
);
1930 /* Unlock only if ctrl socket used */
1931 if (relayd
&& stream
->metadata_flag
) {
1932 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1940 * Splice the data from the ring buffer to the tracefile.
1942 * It must be called with the stream lock held.
1944 * Returns the number of bytes spliced.
1946 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1947 struct lttng_consumer_local_data
*ctx
,
1948 struct lttng_consumer_stream
*stream
, unsigned long len
,
1949 unsigned long padding
,
1950 struct ctf_packet_index
*index
)
1952 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1954 off_t orig_offset
= stream
->out_fd_offset
;
1955 int fd
= stream
->wait_fd
;
1956 /* Default is on the disk */
1957 int outfd
= stream
->out_fd
;
1958 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1960 unsigned int relayd_hang_up
= 0;
1962 switch (consumer_data
.type
) {
1963 case LTTNG_CONSUMER_KERNEL
:
1965 case LTTNG_CONSUMER32_UST
:
1966 case LTTNG_CONSUMER64_UST
:
1967 /* Not supported for user space tracing */
1970 ERR("Unknown consumer_data type");
1974 /* RCU lock for the relayd pointer */
1977 /* Flag that the current stream if set for network streaming. */
1978 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1979 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1980 if (relayd
== NULL
) {
1985 splice_pipe
= stream
->splice_pipe
;
1987 /* Write metadata stream id before payload */
1989 unsigned long total_len
= len
;
1991 if (stream
->metadata_flag
) {
1993 * Lock the control socket for the complete duration of the function
1994 * since from this point on we will use the socket.
1996 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1998 if (stream
->reset_metadata_flag
) {
1999 ret
= relayd_reset_metadata(&relayd
->control_sock
,
2000 stream
->relayd_stream_id
,
2001 stream
->metadata_version
);
2006 stream
->reset_metadata_flag
= 0;
2008 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
,
2016 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
2019 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
2025 /* Use the returned socket. */
2028 /* No streaming, we have to set the len with the full padding */
2031 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
2032 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
2034 ERR("Reset metadata file");
2037 stream
->reset_metadata_flag
= 0;
2040 * Check if we need to change the tracefile before writing the packet.
2042 if (stream
->chan
->tracefile_size
> 0 &&
2043 (stream
->tracefile_size_current
+ len
) >
2044 stream
->chan
->tracefile_size
) {
2045 ret
= consumer_stream_rotate_output_files(stream
);
2050 outfd
= stream
->out_fd
;
2053 stream
->tracefile_size_current
+= len
;
2054 index
->offset
= htobe64(stream
->out_fd_offset
);
2058 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
2059 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
2060 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
2061 SPLICE_F_MOVE
| SPLICE_F_MORE
);
2062 DBG("splice chan to pipe, ret %zd", ret_splice
);
2063 if (ret_splice
< 0) {
2066 PERROR("Error in relay splice");
2070 /* Handle stream on the relayd if the output is on the network */
2071 if (relayd
&& stream
->metadata_flag
) {
2072 size_t metadata_payload_size
=
2073 sizeof(struct lttcomm_relayd_metadata_payload
);
2075 /* Update counter to fit the spliced data */
2076 ret_splice
+= metadata_payload_size
;
2077 len
+= metadata_payload_size
;
2079 * We do this so the return value can match the len passed as
2080 * argument to this function.
2082 written
-= metadata_payload_size
;
2085 /* Splice data out */
2086 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
2087 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
2088 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
2090 if (ret_splice
< 0) {
2095 } else if (ret_splice
> len
) {
2097 * We don't expect this code path to be executed but you never know
2098 * so this is an extra protection agains a buggy splice().
2101 written
+= ret_splice
;
2102 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
2106 /* All good, update current len and continue. */
2110 /* This call is useless on a socket so better save a syscall. */
2112 /* This won't block, but will start writeout asynchronously */
2113 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
2114 SYNC_FILE_RANGE_WRITE
);
2115 stream
->out_fd_offset
+= ret_splice
;
2117 stream
->output_written
+= ret_splice
;
2118 written
+= ret_splice
;
2121 lttng_consumer_sync_trace_file(stream
, orig_offset
);
2127 * This is a special case that the relayd has closed its socket. Let's
2128 * cleanup the relayd object and all associated streams.
2130 if (relayd
&& relayd_hang_up
) {
2131 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
2132 lttng_consumer_cleanup_relayd(relayd
);
2133 /* Skip splice error so the consumer does not fail */
2138 /* send the appropriate error description to sessiond */
2141 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
2144 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
2147 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
2152 if (relayd
&& stream
->metadata_flag
) {
2153 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2161 * Sample the snapshot positions for a specific fd
2163 * Returns 0 on success, < 0 on error
2165 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
2167 switch (consumer_data
.type
) {
2168 case LTTNG_CONSUMER_KERNEL
:
2169 return lttng_kconsumer_sample_snapshot_positions(stream
);
2170 case LTTNG_CONSUMER32_UST
:
2171 case LTTNG_CONSUMER64_UST
:
2172 return lttng_ustconsumer_sample_snapshot_positions(stream
);
2174 ERR("Unknown consumer_data type");
2180 * Take a snapshot for a specific fd
2182 * Returns 0 on success, < 0 on error
2184 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2186 switch (consumer_data
.type
) {
2187 case LTTNG_CONSUMER_KERNEL
:
2188 return lttng_kconsumer_take_snapshot(stream
);
2189 case LTTNG_CONSUMER32_UST
:
2190 case LTTNG_CONSUMER64_UST
:
2191 return lttng_ustconsumer_take_snapshot(stream
);
2193 ERR("Unknown consumer_data type");
2200 * Get the produced position
2202 * Returns 0 on success, < 0 on error
2204 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
2207 switch (consumer_data
.type
) {
2208 case LTTNG_CONSUMER_KERNEL
:
2209 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
2210 case LTTNG_CONSUMER32_UST
:
2211 case LTTNG_CONSUMER64_UST
:
2212 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2214 ERR("Unknown consumer_data type");
2221 * Get the consumed position (free-running counter position in bytes).
2223 * Returns 0 on success, < 0 on error
2225 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
2228 switch (consumer_data
.type
) {
2229 case LTTNG_CONSUMER_KERNEL
:
2230 return lttng_kconsumer_get_consumed_snapshot(stream
, pos
);
2231 case LTTNG_CONSUMER32_UST
:
2232 case LTTNG_CONSUMER64_UST
:
2233 return lttng_ustconsumer_get_consumed_snapshot(stream
, pos
);
2235 ERR("Unknown consumer_data type");
2241 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2242 int sock
, struct pollfd
*consumer_sockpoll
)
2244 switch (consumer_data
.type
) {
2245 case LTTNG_CONSUMER_KERNEL
:
2246 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2247 case LTTNG_CONSUMER32_UST
:
2248 case LTTNG_CONSUMER64_UST
:
2249 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2251 ERR("Unknown consumer_data type");
2257 void lttng_consumer_close_all_metadata(void)
2259 switch (consumer_data
.type
) {
2260 case LTTNG_CONSUMER_KERNEL
:
2262 * The Kernel consumer has a different metadata scheme so we don't
2263 * close anything because the stream will be closed by the session
2267 case LTTNG_CONSUMER32_UST
:
2268 case LTTNG_CONSUMER64_UST
:
2270 * Close all metadata streams. The metadata hash table is passed and
2271 * this call iterates over it by closing all wakeup fd. This is safe
2272 * because at this point we are sure that the metadata producer is
2273 * either dead or blocked.
2275 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2278 ERR("Unknown consumer_data type");
2284 * Clean up a metadata stream and free its memory.
2286 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2287 struct lttng_ht
*ht
)
2289 struct lttng_consumer_channel
*channel
= NULL
;
2290 bool free_channel
= false;
2294 * This call should NEVER receive regular stream. It must always be
2295 * metadata stream and this is crucial for data structure synchronization.
2297 assert(stream
->metadata_flag
);
2299 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2301 pthread_mutex_lock(&consumer_data
.lock
);
2303 * Note that this assumes that a stream's channel is never changed and
2304 * that the stream's lock doesn't need to be taken to sample its
2307 channel
= stream
->chan
;
2308 pthread_mutex_lock(&channel
->lock
);
2309 pthread_mutex_lock(&stream
->lock
);
2310 if (channel
->metadata_cache
) {
2311 /* Only applicable to userspace consumers. */
2312 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
2315 /* Remove any reference to that stream. */
2316 consumer_stream_delete(stream
, ht
);
2318 /* Close down everything including the relayd if one. */
2319 consumer_stream_close(stream
);
2320 /* Destroy tracer buffers of the stream. */
2321 consumer_stream_destroy_buffers(stream
);
2323 /* Atomically decrement channel refcount since other threads can use it. */
2324 if (!uatomic_sub_return(&channel
->refcount
, 1)
2325 && !uatomic_read(&channel
->nb_init_stream_left
)) {
2326 /* Go for channel deletion! */
2327 free_channel
= true;
2329 stream
->chan
= NULL
;
2332 * Nullify the stream reference so it is not used after deletion. The
2333 * channel lock MUST be acquired before being able to check for a NULL
2336 channel
->metadata_stream
= NULL
;
2338 if (channel
->metadata_cache
) {
2339 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
2341 pthread_mutex_unlock(&stream
->lock
);
2342 pthread_mutex_unlock(&channel
->lock
);
2343 pthread_mutex_unlock(&consumer_data
.lock
);
2346 consumer_del_channel(channel
);
2349 lttng_trace_chunk_put(stream
->trace_chunk
);
2350 stream
->trace_chunk
= NULL
;
2351 consumer_stream_free(stream
);
2355 * Action done with the metadata stream when adding it to the consumer internal
2356 * data structures to handle it.
2358 void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2360 struct lttng_ht
*ht
= metadata_ht
;
2361 struct lttng_ht_iter iter
;
2362 struct lttng_ht_node_u64
*node
;
2367 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2369 pthread_mutex_lock(&consumer_data
.lock
);
2370 pthread_mutex_lock(&stream
->chan
->lock
);
2371 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2372 pthread_mutex_lock(&stream
->lock
);
2375 * From here, refcounts are updated so be _careful_ when returning an error
2382 * Lookup the stream just to make sure it does not exist in our internal
2383 * state. This should NEVER happen.
2385 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2386 node
= lttng_ht_iter_get_node_u64(&iter
);
2390 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2391 * in terms of destroying the associated channel, because the action that
2392 * causes the count to become 0 also causes a stream to be added. The
2393 * channel deletion will thus be triggered by the following removal of this
2396 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2397 /* Increment refcount before decrementing nb_init_stream_left */
2399 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2402 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2404 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
2405 &stream
->node_channel_id
);
2408 * Add stream to the stream_list_ht of the consumer data. No need to steal
2409 * the key since the HT does not use it and we allow to add redundant keys
2412 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2416 pthread_mutex_unlock(&stream
->lock
);
2417 pthread_mutex_unlock(&stream
->chan
->lock
);
2418 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2419 pthread_mutex_unlock(&consumer_data
.lock
);
2423 * Delete data stream that are flagged for deletion (endpoint_status).
2425 static void validate_endpoint_status_data_stream(void)
2427 struct lttng_ht_iter iter
;
2428 struct lttng_consumer_stream
*stream
;
2430 DBG("Consumer delete flagged data stream");
2433 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2434 /* Validate delete flag of the stream */
2435 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2438 /* Delete it right now */
2439 consumer_del_stream(stream
, data_ht
);
2445 * Delete metadata stream that are flagged for deletion (endpoint_status).
2447 static void validate_endpoint_status_metadata_stream(
2448 struct lttng_poll_event
*pollset
)
2450 struct lttng_ht_iter iter
;
2451 struct lttng_consumer_stream
*stream
;
2453 DBG("Consumer delete flagged metadata stream");
2458 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2459 /* Validate delete flag of the stream */
2460 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2464 * Remove from pollset so the metadata thread can continue without
2465 * blocking on a deleted stream.
2467 lttng_poll_del(pollset
, stream
->wait_fd
);
2469 /* Delete it right now */
2470 consumer_del_metadata_stream(stream
, metadata_ht
);
2476 * Thread polls on metadata file descriptor and write them on disk or on the
2479 void *consumer_thread_metadata_poll(void *data
)
2481 int ret
, i
, pollfd
, err
= -1;
2482 uint32_t revents
, nb_fd
;
2483 struct lttng_consumer_stream
*stream
= NULL
;
2484 struct lttng_ht_iter iter
;
2485 struct lttng_ht_node_u64
*node
;
2486 struct lttng_poll_event events
;
2487 struct lttng_consumer_local_data
*ctx
= data
;
2490 rcu_register_thread();
2492 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2494 if (testpoint(consumerd_thread_metadata
)) {
2495 goto error_testpoint
;
2498 health_code_update();
2500 DBG("Thread metadata poll started");
2502 /* Size is set to 1 for the consumer_metadata pipe */
2503 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2505 ERR("Poll set creation failed");
2509 ret
= lttng_poll_add(&events
,
2510 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2516 DBG("Metadata main loop started");
2520 health_code_update();
2521 health_poll_entry();
2522 DBG("Metadata poll wait");
2523 ret
= lttng_poll_wait(&events
, -1);
2524 DBG("Metadata poll return from wait with %d fd(s)",
2525 LTTNG_POLL_GETNB(&events
));
2527 DBG("Metadata event caught in thread");
2529 if (errno
== EINTR
) {
2530 ERR("Poll EINTR caught");
2533 if (LTTNG_POLL_GETNB(&events
) == 0) {
2534 err
= 0; /* All is OK */
2541 /* From here, the event is a metadata wait fd */
2542 for (i
= 0; i
< nb_fd
; i
++) {
2543 health_code_update();
2545 revents
= LTTNG_POLL_GETEV(&events
, i
);
2546 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2548 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2549 if (revents
& LPOLLIN
) {
2552 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2553 &stream
, sizeof(stream
));
2554 if (pipe_len
< sizeof(stream
)) {
2556 PERROR("read metadata stream");
2559 * Remove the pipe from the poll set and continue the loop
2560 * since their might be data to consume.
2562 lttng_poll_del(&events
,
2563 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2564 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2568 /* A NULL stream means that the state has changed. */
2569 if (stream
== NULL
) {
2570 /* Check for deleted streams. */
2571 validate_endpoint_status_metadata_stream(&events
);
2575 DBG("Adding metadata stream %d to poll set",
2578 /* Add metadata stream to the global poll events list */
2579 lttng_poll_add(&events
, stream
->wait_fd
,
2580 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2581 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2582 DBG("Metadata thread pipe hung up");
2584 * Remove the pipe from the poll set and continue the loop
2585 * since their might be data to consume.
2587 lttng_poll_del(&events
,
2588 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2589 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2592 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2596 /* Handle other stream */
2602 uint64_t tmp_id
= (uint64_t) pollfd
;
2604 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2606 node
= lttng_ht_iter_get_node_u64(&iter
);
2609 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2612 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2613 /* Get the data out of the metadata file descriptor */
2614 DBG("Metadata available on fd %d", pollfd
);
2615 assert(stream
->wait_fd
== pollfd
);
2618 health_code_update();
2620 len
= ctx
->on_buffer_ready(stream
, ctx
);
2622 * We don't check the return value here since if we get
2623 * a negative len, it means an error occurred thus we
2624 * simply remove it from the poll set and free the
2629 /* It's ok to have an unavailable sub-buffer */
2630 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2631 /* Clean up stream from consumer and free it. */
2632 lttng_poll_del(&events
, stream
->wait_fd
);
2633 consumer_del_metadata_stream(stream
, metadata_ht
);
2635 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2636 DBG("Metadata fd %d is hup|err.", pollfd
);
2637 if (!stream
->hangup_flush_done
2638 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2639 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2640 DBG("Attempting to flush and consume the UST buffers");
2641 lttng_ustconsumer_on_stream_hangup(stream
);
2643 /* We just flushed the stream now read it. */
2645 health_code_update();
2647 len
= ctx
->on_buffer_ready(stream
, ctx
);
2649 * We don't check the return value here since if we get
2650 * a negative len, it means an error occurred thus we
2651 * simply remove it from the poll set and free the
2657 lttng_poll_del(&events
, stream
->wait_fd
);
2659 * This call update the channel states, closes file descriptors
2660 * and securely free the stream.
2662 consumer_del_metadata_stream(stream
, metadata_ht
);
2664 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2668 /* Release RCU lock for the stream looked up */
2676 DBG("Metadata poll thread exiting");
2678 lttng_poll_clean(&events
);
2683 ERR("Health error occurred in %s", __func__
);
2685 health_unregister(health_consumerd
);
2686 rcu_unregister_thread();
2691 * This thread polls the fds in the set to consume the data and write
2692 * it to tracefile if necessary.
2694 void *consumer_thread_data_poll(void *data
)
2696 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2697 struct pollfd
*pollfd
= NULL
;
2698 /* local view of the streams */
2699 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2700 /* local view of consumer_data.fds_count */
2702 /* 2 for the consumer_data_pipe and wake up pipe */
2703 const int nb_pipes_fd
= 2;
2704 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2705 int nb_inactive_fd
= 0;
2706 struct lttng_consumer_local_data
*ctx
= data
;
2709 rcu_register_thread();
2711 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2713 if (testpoint(consumerd_thread_data
)) {
2714 goto error_testpoint
;
2717 health_code_update();
2719 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2720 if (local_stream
== NULL
) {
2721 PERROR("local_stream malloc");
2726 health_code_update();
2732 * the fds set has been updated, we need to update our
2733 * local array as well
2735 pthread_mutex_lock(&consumer_data
.lock
);
2736 if (consumer_data
.need_update
) {
2741 local_stream
= NULL
;
2743 /* Allocate for all fds */
2744 pollfd
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) * sizeof(struct pollfd
));
2745 if (pollfd
== NULL
) {
2746 PERROR("pollfd malloc");
2747 pthread_mutex_unlock(&consumer_data
.lock
);
2751 local_stream
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) *
2752 sizeof(struct lttng_consumer_stream
*));
2753 if (local_stream
== NULL
) {
2754 PERROR("local_stream malloc");
2755 pthread_mutex_unlock(&consumer_data
.lock
);
2758 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2759 data_ht
, &nb_inactive_fd
);
2761 ERR("Error in allocating pollfd or local_outfds");
2762 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2763 pthread_mutex_unlock(&consumer_data
.lock
);
2767 consumer_data
.need_update
= 0;
2769 pthread_mutex_unlock(&consumer_data
.lock
);
2771 /* No FDs and consumer_quit, consumer_cleanup the thread */
2772 if (nb_fd
== 0 && nb_inactive_fd
== 0 &&
2773 CMM_LOAD_SHARED(consumer_quit
) == 1) {
2774 err
= 0; /* All is OK */
2777 /* poll on the array of fds */
2779 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2780 if (testpoint(consumerd_thread_data_poll
)) {
2783 health_poll_entry();
2784 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2786 DBG("poll num_rdy : %d", num_rdy
);
2787 if (num_rdy
== -1) {
2789 * Restart interrupted system call.
2791 if (errno
== EINTR
) {
2794 PERROR("Poll error");
2795 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2797 } else if (num_rdy
== 0) {
2798 DBG("Polling thread timed out");
2802 if (caa_unlikely(data_consumption_paused
)) {
2803 DBG("Data consumption paused, sleeping...");
2809 * If the consumer_data_pipe triggered poll go directly to the
2810 * beginning of the loop to update the array. We want to prioritize
2811 * array update over low-priority reads.
2813 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2814 ssize_t pipe_readlen
;
2816 DBG("consumer_data_pipe wake up");
2817 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2818 &new_stream
, sizeof(new_stream
));
2819 if (pipe_readlen
< sizeof(new_stream
)) {
2820 PERROR("Consumer data pipe");
2821 /* Continue so we can at least handle the current stream(s). */
2826 * If the stream is NULL, just ignore it. It's also possible that
2827 * the sessiond poll thread changed the consumer_quit state and is
2828 * waking us up to test it.
2830 if (new_stream
== NULL
) {
2831 validate_endpoint_status_data_stream();
2835 /* Continue to update the local streams and handle prio ones */
2839 /* Handle wakeup pipe. */
2840 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2842 ssize_t pipe_readlen
;
2844 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2846 if (pipe_readlen
< 0) {
2847 PERROR("Consumer data wakeup pipe");
2849 /* We've been awakened to handle stream(s). */
2850 ctx
->has_wakeup
= 0;
2853 /* Take care of high priority channels first. */
2854 for (i
= 0; i
< nb_fd
; i
++) {
2855 health_code_update();
2857 if (local_stream
[i
] == NULL
) {
2860 if (pollfd
[i
].revents
& POLLPRI
) {
2861 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2863 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2864 /* it's ok to have an unavailable sub-buffer */
2865 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2866 /* Clean the stream and free it. */
2867 consumer_del_stream(local_stream
[i
], data_ht
);
2868 local_stream
[i
] = NULL
;
2869 } else if (len
> 0) {
2870 local_stream
[i
]->data_read
= 1;
2876 * If we read high prio channel in this loop, try again
2877 * for more high prio data.
2883 /* Take care of low priority channels. */
2884 for (i
= 0; i
< nb_fd
; i
++) {
2885 health_code_update();
2887 if (local_stream
[i
] == NULL
) {
2890 if ((pollfd
[i
].revents
& POLLIN
) ||
2891 local_stream
[i
]->hangup_flush_done
||
2892 local_stream
[i
]->has_data
) {
2893 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2894 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2895 /* it's ok to have an unavailable sub-buffer */
2896 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2897 /* Clean the stream and free it. */
2898 consumer_del_stream(local_stream
[i
], data_ht
);
2899 local_stream
[i
] = NULL
;
2900 } else if (len
> 0) {
2901 local_stream
[i
]->data_read
= 1;
2906 /* Handle hangup and errors */
2907 for (i
= 0; i
< nb_fd
; i
++) {
2908 health_code_update();
2910 if (local_stream
[i
] == NULL
) {
2913 if (!local_stream
[i
]->hangup_flush_done
2914 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2915 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2916 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2917 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2919 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2920 /* Attempt read again, for the data we just flushed. */
2921 local_stream
[i
]->data_read
= 1;
2924 * If the poll flag is HUP/ERR/NVAL and we have
2925 * read no data in this pass, we can remove the
2926 * stream from its hash table.
2928 if ((pollfd
[i
].revents
& POLLHUP
)) {
2929 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2930 if (!local_stream
[i
]->data_read
) {
2931 consumer_del_stream(local_stream
[i
], data_ht
);
2932 local_stream
[i
] = NULL
;
2935 } else if (pollfd
[i
].revents
& POLLERR
) {
2936 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2937 if (!local_stream
[i
]->data_read
) {
2938 consumer_del_stream(local_stream
[i
], data_ht
);
2939 local_stream
[i
] = NULL
;
2942 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2943 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2944 if (!local_stream
[i
]->data_read
) {
2945 consumer_del_stream(local_stream
[i
], data_ht
);
2946 local_stream
[i
] = NULL
;
2950 if (local_stream
[i
] != NULL
) {
2951 local_stream
[i
]->data_read
= 0;
2958 DBG("polling thread exiting");
2963 * Close the write side of the pipe so epoll_wait() in
2964 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2965 * read side of the pipe. If we close them both, epoll_wait strangely does
2966 * not return and could create a endless wait period if the pipe is the
2967 * only tracked fd in the poll set. The thread will take care of closing
2970 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2975 ERR("Health error occurred in %s", __func__
);
2977 health_unregister(health_consumerd
);
2979 rcu_unregister_thread();
2984 * Close wake-up end of each stream belonging to the channel. This will
2985 * allow the poll() on the stream read-side to detect when the
2986 * write-side (application) finally closes them.
2989 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2991 struct lttng_ht
*ht
;
2992 struct lttng_consumer_stream
*stream
;
2993 struct lttng_ht_iter iter
;
2995 ht
= consumer_data
.stream_per_chan_id_ht
;
2998 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2999 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
3000 ht
->match_fct
, &channel
->key
,
3001 &iter
.iter
, stream
, node_channel_id
.node
) {
3003 * Protect against teardown with mutex.
3005 pthread_mutex_lock(&stream
->lock
);
3006 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
3009 switch (consumer_data
.type
) {
3010 case LTTNG_CONSUMER_KERNEL
:
3012 case LTTNG_CONSUMER32_UST
:
3013 case LTTNG_CONSUMER64_UST
:
3014 if (stream
->metadata_flag
) {
3015 /* Safe and protected by the stream lock. */
3016 lttng_ustconsumer_close_metadata(stream
->chan
);
3019 * Note: a mutex is taken internally within
3020 * liblttng-ust-ctl to protect timer wakeup_fd
3021 * use from concurrent close.
3023 lttng_ustconsumer_close_stream_wakeup(stream
);
3027 ERR("Unknown consumer_data type");
3031 pthread_mutex_unlock(&stream
->lock
);
3036 static void destroy_channel_ht(struct lttng_ht
*ht
)
3038 struct lttng_ht_iter iter
;
3039 struct lttng_consumer_channel
*channel
;
3047 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
3048 ret
= lttng_ht_del(ht
, &iter
);
3053 lttng_ht_destroy(ht
);
3057 * This thread polls the channel fds to detect when they are being
3058 * closed. It closes all related streams if the channel is detected as
3059 * closed. It is currently only used as a shim layer for UST because the
3060 * consumerd needs to keep the per-stream wakeup end of pipes open for
3063 void *consumer_thread_channel_poll(void *data
)
3065 int ret
, i
, pollfd
, err
= -1;
3066 uint32_t revents
, nb_fd
;
3067 struct lttng_consumer_channel
*chan
= NULL
;
3068 struct lttng_ht_iter iter
;
3069 struct lttng_ht_node_u64
*node
;
3070 struct lttng_poll_event events
;
3071 struct lttng_consumer_local_data
*ctx
= data
;
3072 struct lttng_ht
*channel_ht
;
3074 rcu_register_thread();
3076 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
3078 if (testpoint(consumerd_thread_channel
)) {
3079 goto error_testpoint
;
3082 health_code_update();
3084 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3086 /* ENOMEM at this point. Better to bail out. */
3090 DBG("Thread channel poll started");
3092 /* Size is set to 1 for the consumer_channel pipe */
3093 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
3095 ERR("Poll set creation failed");
3099 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
3105 DBG("Channel main loop started");
3109 health_code_update();
3110 DBG("Channel poll wait");
3111 health_poll_entry();
3112 ret
= lttng_poll_wait(&events
, -1);
3113 DBG("Channel poll return from wait with %d fd(s)",
3114 LTTNG_POLL_GETNB(&events
));
3116 DBG("Channel event caught in thread");
3118 if (errno
== EINTR
) {
3119 ERR("Poll EINTR caught");
3122 if (LTTNG_POLL_GETNB(&events
) == 0) {
3123 err
= 0; /* All is OK */
3130 /* From here, the event is a channel wait fd */
3131 for (i
= 0; i
< nb_fd
; i
++) {
3132 health_code_update();
3134 revents
= LTTNG_POLL_GETEV(&events
, i
);
3135 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3137 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
3138 if (revents
& LPOLLIN
) {
3139 enum consumer_channel_action action
;
3142 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
3145 ERR("Error reading channel pipe");
3147 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3152 case CONSUMER_CHANNEL_ADD
:
3153 DBG("Adding channel %d to poll set",
3156 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
3159 lttng_ht_add_unique_u64(channel_ht
,
3160 &chan
->wait_fd_node
);
3162 /* Add channel to the global poll events list */
3163 lttng_poll_add(&events
, chan
->wait_fd
,
3164 LPOLLERR
| LPOLLHUP
);
3166 case CONSUMER_CHANNEL_DEL
:
3169 * This command should never be called if the channel
3170 * has streams monitored by either the data or metadata
3171 * thread. The consumer only notify this thread with a
3172 * channel del. command if it receives a destroy
3173 * channel command from the session daemon that send it
3174 * if a command prior to the GET_CHANNEL failed.
3178 chan
= consumer_find_channel(key
);
3181 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
3184 lttng_poll_del(&events
, chan
->wait_fd
);
3185 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
3186 ret
= lttng_ht_del(channel_ht
, &iter
);
3189 switch (consumer_data
.type
) {
3190 case LTTNG_CONSUMER_KERNEL
:
3192 case LTTNG_CONSUMER32_UST
:
3193 case LTTNG_CONSUMER64_UST
:
3194 health_code_update();
3195 /* Destroy streams that might have been left in the stream list. */
3196 clean_channel_stream_list(chan
);
3199 ERR("Unknown consumer_data type");
3204 * Release our own refcount. Force channel deletion even if
3205 * streams were not initialized.
3207 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3208 consumer_del_channel(chan
);
3213 case CONSUMER_CHANNEL_QUIT
:
3215 * Remove the pipe from the poll set and continue the loop
3216 * since their might be data to consume.
3218 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3221 ERR("Unknown action");
3224 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3225 DBG("Channel thread pipe hung up");
3227 * Remove the pipe from the poll set and continue the loop
3228 * since their might be data to consume.
3230 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3233 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3237 /* Handle other stream */
3243 uint64_t tmp_id
= (uint64_t) pollfd
;
3245 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3247 node
= lttng_ht_iter_get_node_u64(&iter
);
3250 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
3253 /* Check for error event */
3254 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3255 DBG("Channel fd %d is hup|err.", pollfd
);
3257 lttng_poll_del(&events
, chan
->wait_fd
);
3258 ret
= lttng_ht_del(channel_ht
, &iter
);
3262 * This will close the wait fd for each stream associated to
3263 * this channel AND monitored by the data/metadata thread thus
3264 * will be clean by the right thread.
3266 consumer_close_channel_streams(chan
);
3268 /* Release our own refcount */
3269 if (!uatomic_sub_return(&chan
->refcount
, 1)
3270 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3271 consumer_del_channel(chan
);
3274 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3279 /* Release RCU lock for the channel looked up */
3287 lttng_poll_clean(&events
);
3289 destroy_channel_ht(channel_ht
);
3292 DBG("Channel poll thread exiting");
3295 ERR("Health error occurred in %s", __func__
);
3297 health_unregister(health_consumerd
);
3298 rcu_unregister_thread();
3302 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3303 struct pollfd
*sockpoll
, int client_socket
)
3310 ret
= lttng_consumer_poll_socket(sockpoll
);
3314 DBG("Metadata connection on client_socket");
3316 /* Blocking call, waiting for transmission */
3317 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3318 if (ctx
->consumer_metadata_socket
< 0) {
3319 WARN("On accept metadata");
3330 * This thread listens on the consumerd socket and receives the file
3331 * descriptors from the session daemon.
3333 void *consumer_thread_sessiond_poll(void *data
)
3335 int sock
= -1, client_socket
, ret
, err
= -1;
3337 * structure to poll for incoming data on communication socket avoids
3338 * making blocking sockets.
3340 struct pollfd consumer_sockpoll
[2];
3341 struct lttng_consumer_local_data
*ctx
= data
;
3343 rcu_register_thread();
3345 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3347 if (testpoint(consumerd_thread_sessiond
)) {
3348 goto error_testpoint
;
3351 health_code_update();
3353 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3354 unlink(ctx
->consumer_command_sock_path
);
3355 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3356 if (client_socket
< 0) {
3357 ERR("Cannot create command socket");
3361 ret
= lttcomm_listen_unix_sock(client_socket
);
3366 DBG("Sending ready command to lttng-sessiond");
3367 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3368 /* return < 0 on error, but == 0 is not fatal */
3370 ERR("Error sending ready command to lttng-sessiond");
3374 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3375 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3376 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3377 consumer_sockpoll
[1].fd
= client_socket
;
3378 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3380 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3388 DBG("Connection on client_socket");
3390 /* Blocking call, waiting for transmission */
3391 sock
= lttcomm_accept_unix_sock(client_socket
);
3398 * Setup metadata socket which is the second socket connection on the
3399 * command unix socket.
3401 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3410 /* This socket is not useful anymore. */
3411 ret
= close(client_socket
);
3413 PERROR("close client_socket");
3417 /* update the polling structure to poll on the established socket */
3418 consumer_sockpoll
[1].fd
= sock
;
3419 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3422 health_code_update();
3424 health_poll_entry();
3425 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3434 DBG("Incoming command on sock");
3435 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3438 * This could simply be a session daemon quitting. Don't output
3441 DBG("Communication interrupted on command socket");
3445 if (CMM_LOAD_SHARED(consumer_quit
)) {
3446 DBG("consumer_thread_receive_fds received quit from signal");
3447 err
= 0; /* All is OK */
3450 DBG("received command on sock");
3456 DBG("Consumer thread sessiond poll exiting");
3459 * Close metadata streams since the producer is the session daemon which
3462 * NOTE: for now, this only applies to the UST tracer.
3464 lttng_consumer_close_all_metadata();
3467 * when all fds have hung up, the polling thread
3470 CMM_STORE_SHARED(consumer_quit
, 1);
3473 * Notify the data poll thread to poll back again and test the
3474 * consumer_quit state that we just set so to quit gracefully.
3476 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3478 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3480 notify_health_quit_pipe(health_quit_pipe
);
3482 /* Cleaning up possibly open sockets. */
3486 PERROR("close sock sessiond poll");
3489 if (client_socket
>= 0) {
3490 ret
= close(client_socket
);
3492 PERROR("close client_socket sessiond poll");
3499 ERR("Health error occurred in %s", __func__
);
3501 health_unregister(health_consumerd
);
3503 rcu_unregister_thread();
3507 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3508 struct lttng_consumer_local_data
*ctx
)
3512 pthread_mutex_lock(&stream
->chan
->lock
);
3513 pthread_mutex_lock(&stream
->lock
);
3514 if (stream
->metadata_flag
) {
3515 pthread_mutex_lock(&stream
->metadata_rdv_lock
);
3518 switch (consumer_data
.type
) {
3519 case LTTNG_CONSUMER_KERNEL
:
3520 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3522 case LTTNG_CONSUMER32_UST
:
3523 case LTTNG_CONSUMER64_UST
:
3524 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3527 ERR("Unknown consumer_data type");
3533 if (stream
->metadata_flag
) {
3534 pthread_cond_broadcast(&stream
->metadata_rdv
);
3535 pthread_mutex_unlock(&stream
->metadata_rdv_lock
);
3537 pthread_mutex_unlock(&stream
->lock
);
3538 pthread_mutex_unlock(&stream
->chan
->lock
);
3543 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3545 switch (consumer_data
.type
) {
3546 case LTTNG_CONSUMER_KERNEL
:
3547 return lttng_kconsumer_on_recv_stream(stream
);
3548 case LTTNG_CONSUMER32_UST
:
3549 case LTTNG_CONSUMER64_UST
:
3550 return lttng_ustconsumer_on_recv_stream(stream
);
3552 ERR("Unknown consumer_data type");
3559 * Allocate and set consumer data hash tables.
3561 int lttng_consumer_init(void)
3563 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3564 if (!consumer_data
.channel_ht
) {
3568 consumer_data
.channels_by_session_id_ht
=
3569 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3570 if (!consumer_data
.channels_by_session_id_ht
) {
3574 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3575 if (!consumer_data
.relayd_ht
) {
3579 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3580 if (!consumer_data
.stream_list_ht
) {
3584 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3585 if (!consumer_data
.stream_per_chan_id_ht
) {
3589 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3594 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3599 consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3600 if (!consumer_data
.chunk_registry
) {
3611 * Process the ADD_RELAYD command receive by a consumer.
3613 * This will create a relayd socket pair and add it to the relayd hash table.
3614 * The caller MUST acquire a RCU read side lock before calling it.
3616 void consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3617 struct lttng_consumer_local_data
*ctx
, int sock
,
3618 struct pollfd
*consumer_sockpoll
,
3619 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3620 uint64_t relayd_session_id
)
3622 int fd
= -1, ret
= -1, relayd_created
= 0;
3623 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3624 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3627 assert(relayd_sock
);
3629 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3631 /* Get relayd reference if exists. */
3632 relayd
= consumer_find_relayd(net_seq_idx
);
3633 if (relayd
== NULL
) {
3634 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3635 /* Not found. Allocate one. */
3636 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3637 if (relayd
== NULL
) {
3638 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3641 relayd
->sessiond_session_id
= sessiond_id
;
3646 * This code path MUST continue to the consumer send status message to
3647 * we can notify the session daemon and continue our work without
3648 * killing everything.
3652 * relayd key should never be found for control socket.
3654 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3657 /* First send a status message before receiving the fds. */
3658 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3660 /* Somehow, the session daemon is not responding anymore. */
3661 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3662 goto error_nosignal
;
3665 /* Poll on consumer socket. */
3666 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3668 /* Needing to exit in the middle of a command: error. */
3669 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3670 goto error_nosignal
;
3673 /* Get relayd socket from session daemon */
3674 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3675 if (ret
!= sizeof(fd
)) {
3676 fd
= -1; /* Just in case it gets set with an invalid value. */
3679 * Failing to receive FDs might indicate a major problem such as
3680 * reaching a fd limit during the receive where the kernel returns a
3681 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3682 * don't take any chances and stop everything.
3684 * XXX: Feature request #558 will fix that and avoid this possible
3685 * issue when reaching the fd limit.
3687 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3688 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3692 /* Copy socket information and received FD */
3693 switch (sock_type
) {
3694 case LTTNG_STREAM_CONTROL
:
3695 /* Copy received lttcomm socket */
3696 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3697 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3698 /* Handle create_sock error. */
3700 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3704 * Close the socket created internally by
3705 * lttcomm_create_sock, so we can replace it by the one
3706 * received from sessiond.
3708 if (close(relayd
->control_sock
.sock
.fd
)) {
3712 /* Assign new file descriptor */
3713 relayd
->control_sock
.sock
.fd
= fd
;
3714 /* Assign version values. */
3715 relayd
->control_sock
.major
= relayd_sock
->major
;
3716 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3718 relayd
->relayd_session_id
= relayd_session_id
;
3721 case LTTNG_STREAM_DATA
:
3722 /* Copy received lttcomm socket */
3723 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3724 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3725 /* Handle create_sock error. */
3727 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3731 * Close the socket created internally by
3732 * lttcomm_create_sock, so we can replace it by the one
3733 * received from sessiond.
3735 if (close(relayd
->data_sock
.sock
.fd
)) {
3739 /* Assign new file descriptor */
3740 relayd
->data_sock
.sock
.fd
= fd
;
3741 /* Assign version values. */
3742 relayd
->data_sock
.major
= relayd_sock
->major
;
3743 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3746 ERR("Unknown relayd socket type (%d)", sock_type
);
3747 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3751 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3752 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3753 relayd
->net_seq_idx
, fd
);
3755 * We gave the ownership of the fd to the relayd structure. Set the
3756 * fd to -1 so we don't call close() on it in the error path below.
3760 /* We successfully added the socket. Send status back. */
3761 ret
= consumer_send_status_msg(sock
, ret_code
);
3763 /* Somehow, the session daemon is not responding anymore. */
3764 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3765 goto error_nosignal
;
3769 * Add relayd socket pair to consumer data hashtable. If object already
3770 * exists or on error, the function gracefully returns.
3779 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3780 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3784 /* Close received socket if valid. */
3787 PERROR("close received socket");
3791 if (relayd_created
) {
3797 * Search for a relayd associated to the session id and return the reference.
3799 * A rcu read side lock MUST be acquire before calling this function and locked
3800 * until the relayd object is no longer necessary.
3802 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3804 struct lttng_ht_iter iter
;
3805 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3807 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3808 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3811 * Check by sessiond id which is unique here where the relayd session
3812 * id might not be when having multiple relayd.
3814 if (relayd
->sessiond_session_id
== id
) {
3815 /* Found the relayd. There can be only one per id. */
3827 * Check if for a given session id there is still data needed to be extract
3830 * Return 1 if data is pending or else 0 meaning ready to be read.
3832 int consumer_data_pending(uint64_t id
)
3835 struct lttng_ht_iter iter
;
3836 struct lttng_ht
*ht
;
3837 struct lttng_consumer_stream
*stream
;
3838 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3839 int (*data_pending
)(struct lttng_consumer_stream
*);
3841 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3844 pthread_mutex_lock(&consumer_data
.lock
);
3846 switch (consumer_data
.type
) {
3847 case LTTNG_CONSUMER_KERNEL
:
3848 data_pending
= lttng_kconsumer_data_pending
;
3850 case LTTNG_CONSUMER32_UST
:
3851 case LTTNG_CONSUMER64_UST
:
3852 data_pending
= lttng_ustconsumer_data_pending
;
3855 ERR("Unknown consumer data type");
3859 /* Ease our life a bit */
3860 ht
= consumer_data
.stream_list_ht
;
3862 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3863 ht
->hash_fct(&id
, lttng_ht_seed
),
3865 &iter
.iter
, stream
, node_session_id
.node
) {
3866 pthread_mutex_lock(&stream
->lock
);
3869 * A removed node from the hash table indicates that the stream has
3870 * been deleted thus having a guarantee that the buffers are closed
3871 * on the consumer side. However, data can still be transmitted
3872 * over the network so don't skip the relayd check.
3874 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3876 /* Check the stream if there is data in the buffers. */
3877 ret
= data_pending(stream
);
3879 pthread_mutex_unlock(&stream
->lock
);
3884 pthread_mutex_unlock(&stream
->lock
);
3887 relayd
= find_relayd_by_session_id(id
);
3889 unsigned int is_data_inflight
= 0;
3891 /* Send init command for data pending. */
3892 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3893 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3894 relayd
->relayd_session_id
);
3896 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3897 /* Communication error thus the relayd so no data pending. */
3898 goto data_not_pending
;
3901 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3902 ht
->hash_fct(&id
, lttng_ht_seed
),
3904 &iter
.iter
, stream
, node_session_id
.node
) {
3905 if (stream
->metadata_flag
) {
3906 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3907 stream
->relayd_stream_id
);
3909 ret
= relayd_data_pending(&relayd
->control_sock
,
3910 stream
->relayd_stream_id
,
3911 stream
->next_net_seq_num
- 1);
3915 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3917 } else if (ret
< 0) {
3918 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3919 lttng_consumer_cleanup_relayd(relayd
);
3920 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3921 goto data_not_pending
;
3925 /* Send end command for data pending. */
3926 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3927 relayd
->relayd_session_id
, &is_data_inflight
);
3928 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3930 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3931 lttng_consumer_cleanup_relayd(relayd
);
3932 goto data_not_pending
;
3934 if (is_data_inflight
) {
3940 * Finding _no_ node in the hash table and no inflight data means that the
3941 * stream(s) have been removed thus data is guaranteed to be available for
3942 * analysis from the trace files.
3946 /* Data is available to be read by a viewer. */
3947 pthread_mutex_unlock(&consumer_data
.lock
);
3952 /* Data is still being extracted from buffers. */
3953 pthread_mutex_unlock(&consumer_data
.lock
);
3959 * Send a ret code status message to the sessiond daemon.
3961 * Return the sendmsg() return value.
3963 int consumer_send_status_msg(int sock
, int ret_code
)
3965 struct lttcomm_consumer_status_msg msg
;
3967 memset(&msg
, 0, sizeof(msg
));
3968 msg
.ret_code
= ret_code
;
3970 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3974 * Send a channel status message to the sessiond daemon.
3976 * Return the sendmsg() return value.
3978 int consumer_send_status_channel(int sock
,
3979 struct lttng_consumer_channel
*channel
)
3981 struct lttcomm_consumer_status_channel msg
;
3985 memset(&msg
, 0, sizeof(msg
));
3987 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3989 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3990 msg
.key
= channel
->key
;
3991 msg
.stream_count
= channel
->streams
.count
;
3994 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3997 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3998 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3999 uint64_t max_sb_size
)
4001 unsigned long start_pos
;
4003 if (!nb_packets_per_stream
) {
4004 return consumed_pos
; /* Grab everything */
4006 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
4007 start_pos
-= max_sb_size
* nb_packets_per_stream
;
4008 if ((long) (start_pos
- consumed_pos
) < 0) {
4009 return consumed_pos
; /* Grab everything */
4015 int consumer_flush_buffer(struct lttng_consumer_stream
*stream
, int producer_active
)
4019 switch (consumer_data
.type
) {
4020 case LTTNG_CONSUMER_KERNEL
:
4021 ret
= kernctl_buffer_flush(stream
->wait_fd
);
4023 ERR("Failed to flush kernel stream");
4027 case LTTNG_CONSUMER32_UST
:
4028 case LTTNG_CONSUMER64_UST
:
4029 lttng_ustctl_flush_buffer(stream
, producer_active
);
4032 ERR("Unknown consumer_data type");
4041 * Sample the rotate position for all the streams of a channel. If a stream
4042 * is already at the rotate position (produced == consumed), we flag it as
4043 * ready for rotation. The rotation of ready streams occurs after we have
4044 * replied to the session daemon that we have finished sampling the positions.
4045 * Must be called with RCU read-side lock held to ensure existence of channel.
4047 * Returns 0 on success, < 0 on error
4049 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
4050 uint64_t key
, uint64_t relayd_id
, uint32_t metadata
,
4051 struct lttng_consumer_local_data
*ctx
)
4054 struct lttng_consumer_stream
*stream
;
4055 struct lttng_ht_iter iter
;
4056 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
4057 struct lttng_dynamic_array stream_rotation_positions
;
4058 uint64_t next_chunk_id
, stream_count
= 0;
4059 enum lttng_trace_chunk_status chunk_status
;
4060 const bool is_local_trace
= relayd_id
== -1ULL;
4061 struct consumer_relayd_sock_pair
*relayd
= NULL
;
4062 bool rotating_to_new_chunk
= true;
4064 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
4066 lttng_dynamic_array_init(&stream_rotation_positions
,
4067 sizeof(struct relayd_stream_rotation_position
), NULL
);
4071 pthread_mutex_lock(&channel
->lock
);
4072 assert(channel
->trace_chunk
);
4073 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
,
4075 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4077 goto end_unlock_channel
;
4080 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4081 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4082 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4083 stream
, node_channel_id
.node
) {
4084 unsigned long consumed_pos
;
4086 health_code_update();
4089 * Lock stream because we are about to change its state.
4091 pthread_mutex_lock(&stream
->lock
);
4093 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
4094 rotating_to_new_chunk
= false;
4097 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4099 ERR("Failed to sample snapshot position during channel rotation");
4100 goto end_unlock_stream
;
4103 ret
= lttng_consumer_get_produced_snapshot(stream
,
4104 &stream
->rotate_position
);
4106 ERR("Failed to sample produced position during channel rotation");
4107 goto end_unlock_stream
;
4110 lttng_consumer_get_consumed_snapshot(stream
,
4112 if (consumed_pos
== stream
->rotate_position
) {
4113 stream
->rotate_ready
= true;
4117 * Active flush; has no effect if the production position
4118 * is at a packet boundary.
4120 ret
= consumer_flush_buffer(stream
, 1);
4122 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4124 goto end_unlock_stream
;
4127 if (!is_local_trace
) {
4129 * The relay daemon control protocol expects a rotation
4130 * position as "the sequence number of the first packet
4131 * _after_ the current trace chunk.
4133 * At the moment when the positions of the buffers are
4134 * sampled, the production position does not necessarily
4135 * sit at a packet boundary. The 'active' flush
4136 * operation above will push the production position to
4137 * the next packet boundary _if_ it is not already
4138 * sitting at such a boundary.
4140 * Assuming a current production position that is not
4141 * on the bound of a packet, the 'target' sequence
4143 * (consumed_pos / subbuffer_size) + 1
4144 * Note the '+ 1' to ensure the current packet is
4145 * part of the current trace chunk.
4147 * However, if the production position is already at
4148 * a packet boundary, the '+ 1' is not necessary as the
4149 * last packet of the current chunk is already
4152 const struct relayd_stream_rotation_position position
= {
4153 .stream_id
= stream
->relayd_stream_id
,
4154 .rotate_at_seq_num
= (stream
->rotate_position
/ stream
->max_sb_size
) +
4155 !!(stream
->rotate_position
% stream
->max_sb_size
),
4158 ret
= lttng_dynamic_array_add_element(
4159 &stream_rotation_positions
,
4162 ERR("Failed to allocate stream rotation position");
4163 goto end_unlock_stream
;
4167 pthread_mutex_unlock(&stream
->lock
);
4170 pthread_mutex_unlock(&channel
->lock
);
4172 if (is_local_trace
) {
4177 relayd
= consumer_find_relayd(relayd_id
);
4179 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4184 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4185 ret
= relayd_rotate_streams(&relayd
->control_sock
, stream_count
,
4186 rotating_to_new_chunk
? &next_chunk_id
: NULL
,
4187 (const struct relayd_stream_rotation_position
*)
4188 stream_rotation_positions
.buffer
.data
);
4189 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4191 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4192 relayd
->net_seq_idx
);
4193 lttng_consumer_cleanup_relayd(relayd
);
4201 pthread_mutex_unlock(&stream
->lock
);
4203 pthread_mutex_unlock(&channel
->lock
);
4206 lttng_dynamic_array_reset(&stream_rotation_positions
);
4211 * Check if a stream is ready to be rotated after extracting it.
4213 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4214 * error. Stream lock must be held.
4216 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4219 unsigned long consumed_pos
;
4221 if (!stream
->rotate_position
&& !stream
->rotate_ready
) {
4226 if (stream
->rotate_ready
) {
4232 * If we don't have the rotate_ready flag, check the consumed position
4233 * to determine if we need to rotate.
4235 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4237 ERR("Taking snapshot positions");
4241 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos
);
4243 ERR("Consumed snapshot position");
4247 /* Rotate position not reached yet (with check for overflow). */
4248 if ((long) (consumed_pos
- stream
->rotate_position
) < 0) {
4259 * Reset the state for a stream after a rotation occurred.
4261 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4263 stream
->rotate_position
= 0;
4264 stream
->rotate_ready
= false;
4268 * Perform the rotation a local stream file.
4271 int rotate_local_stream(struct lttng_consumer_local_data
*ctx
,
4272 struct lttng_consumer_stream
*stream
)
4276 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4279 stream
->tracefile_size_current
= 0;
4280 stream
->tracefile_count_current
= 0;
4282 if (stream
->out_fd
>= 0) {
4283 ret
= close(stream
->out_fd
);
4285 PERROR("Failed to close stream out_fd of channel \"%s\"",
4286 stream
->chan
->name
);
4288 stream
->out_fd
= -1;
4291 if (stream
->index_file
) {
4292 lttng_index_file_put(stream
->index_file
);
4293 stream
->index_file
= NULL
;
4296 if (!stream
->trace_chunk
) {
4300 ret
= consumer_stream_create_output_files(stream
, true);
4306 * Performs the stream rotation for the rotate session feature if needed.
4307 * It must be called with the channel and stream locks held.
4309 * Return 0 on success, a negative number of error.
4311 int lttng_consumer_rotate_stream(struct lttng_consumer_local_data
*ctx
,
4312 struct lttng_consumer_stream
*stream
)
4316 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4319 * Update the stream's 'current' chunk to the session's (channel)
4320 * now-current chunk.
4322 lttng_trace_chunk_put(stream
->trace_chunk
);
4323 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4325 * A channel can be rotated and not have a "next" chunk
4326 * to transition to. In that case, the channel's "current chunk"
4327 * has not been closed yet, but it has not been updated to
4328 * a "next" trace chunk either. Hence, the stream, like its
4329 * parent channel, becomes part of no chunk and can't output
4330 * anything until a new trace chunk is created.
4332 stream
->trace_chunk
= NULL
;
4333 } else if (stream
->chan
->trace_chunk
&&
4334 !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4335 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4340 * Update the stream's trace chunk to its parent channel's
4341 * current trace chunk.
4343 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4346 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4347 ret
= rotate_local_stream(ctx
, stream
);
4349 ERR("Failed to rotate stream, ret = %i", ret
);
4354 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4356 * If the stream has transitioned to a new trace
4357 * chunk, the metadata should be re-dumped to the
4360 * However, it is possible for a stream to transition to
4361 * a "no-chunk" state. This can happen if a rotation
4362 * occurs on an inactive session. In such cases, the metadata
4363 * regeneration will happen when the next trace chunk is
4366 ret
= consumer_metadata_stream_dump(stream
);
4371 lttng_consumer_reset_stream_rotate_state(stream
);
4380 * Rotate all the ready streams now.
4382 * This is especially important for low throughput streams that have already
4383 * been consumed, we cannot wait for their next packet to perform the
4385 * Need to be called with RCU read-side lock held to ensure existence of
4388 * Returns 0 on success, < 0 on error
4390 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
,
4391 uint64_t key
, struct lttng_consumer_local_data
*ctx
)
4394 struct lttng_consumer_stream
*stream
;
4395 struct lttng_ht_iter iter
;
4396 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
4400 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4402 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4403 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4404 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4405 stream
, node_channel_id
.node
) {
4406 health_code_update();
4408 pthread_mutex_lock(&stream
->chan
->lock
);
4409 pthread_mutex_lock(&stream
->lock
);
4411 if (!stream
->rotate_ready
) {
4412 pthread_mutex_unlock(&stream
->lock
);
4413 pthread_mutex_unlock(&stream
->chan
->lock
);
4416 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4418 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
4419 pthread_mutex_unlock(&stream
->lock
);
4420 pthread_mutex_unlock(&stream
->chan
->lock
);
4433 enum lttcomm_return_code
lttng_consumer_init_command(
4434 struct lttng_consumer_local_data
*ctx
,
4435 const lttng_uuid sessiond_uuid
)
4437 enum lttcomm_return_code ret
;
4438 char uuid_str
[UUID_STR_LEN
];
4440 if (ctx
->sessiond_uuid
.is_set
) {
4441 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4445 ctx
->sessiond_uuid
.is_set
= true;
4446 memcpy(ctx
->sessiond_uuid
.value
, sessiond_uuid
, sizeof(lttng_uuid
));
4447 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4448 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4449 DBG("Received session daemon UUID: %s", uuid_str
);
4454 enum lttcomm_return_code
lttng_consumer_create_trace_chunk(
4455 const uint64_t *relayd_id
, uint64_t session_id
,
4457 time_t chunk_creation_timestamp
,
4458 const char *chunk_override_name
,
4459 const struct lttng_credentials
*credentials
,
4460 struct lttng_directory_handle
*chunk_directory_handle
)
4463 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4464 struct lttng_trace_chunk
*created_chunk
, *published_chunk
;
4465 enum lttng_trace_chunk_status chunk_status
;
4466 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4467 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4468 const char *relayd_id_str
= "(none)";
4469 const char *creation_timestamp_str
;
4470 struct lttng_ht_iter iter
;
4471 struct lttng_consumer_channel
*channel
;
4474 /* Only used for logging purposes. */
4475 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4476 "%" PRIu64
, *relayd_id
);
4477 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4478 relayd_id_str
= relayd_id_buffer
;
4480 relayd_id_str
= "(formatting error)";
4484 /* Local protocol error. */
4485 assert(chunk_creation_timestamp
);
4486 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4487 creation_timestamp_buffer
,
4488 sizeof(creation_timestamp_buffer
));
4489 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
4490 "(formatting error)";
4492 DBG("Consumer create trace chunk command: relay_id = %s"
4493 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4494 ", chunk_override_name = %s"
4495 ", chunk_creation_timestamp = %s",
4496 relayd_id_str
, session_id
, chunk_id
,
4497 chunk_override_name
? : "(none)",
4498 creation_timestamp_str
);
4501 * The trace chunk registry, as used by the consumer daemon, implicitly
4502 * owns the trace chunks. This is only needed in the consumer since
4503 * the consumer has no notion of a session beyond session IDs being
4504 * used to identify other objects.
4506 * The lttng_trace_chunk_registry_publish() call below provides a
4507 * reference which is not released; it implicitly becomes the session
4508 * daemon's reference to the chunk in the consumer daemon.
4510 * The lifetime of trace chunks in the consumer daemon is managed by
4511 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4512 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4514 created_chunk
= lttng_trace_chunk_create(chunk_id
,
4515 chunk_creation_timestamp
);
4516 if (!created_chunk
) {
4517 ERR("Failed to create trace chunk");
4518 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4522 if (chunk_override_name
) {
4523 chunk_status
= lttng_trace_chunk_override_name(created_chunk
,
4524 chunk_override_name
);
4525 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4526 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4531 if (chunk_directory_handle
) {
4532 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
,
4534 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4535 ERR("Failed to set trace chunk credentials");
4536 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4540 * The consumer daemon has no ownership of the chunk output
4543 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
,
4544 chunk_directory_handle
);
4545 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4546 ERR("Failed to set trace chunk's directory handle");
4547 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4552 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4553 consumer_data
.chunk_registry
, session_id
,
4555 lttng_trace_chunk_put(created_chunk
);
4556 created_chunk
= NULL
;
4557 if (!published_chunk
) {
4558 ERR("Failed to publish trace chunk");
4559 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4564 cds_lfht_for_each_entry_duplicate(consumer_data
.channels_by_session_id_ht
->ht
,
4565 consumer_data
.channels_by_session_id_ht
->hash_fct(
4566 &session_id
, lttng_ht_seed
),
4567 consumer_data
.channels_by_session_id_ht
->match_fct
,
4568 &session_id
, &iter
.iter
, channel
,
4569 channels_by_session_id_ht_node
.node
) {
4570 ret
= lttng_consumer_channel_set_trace_chunk(channel
,
4574 * Roll-back the creation of this chunk.
4576 * This is important since the session daemon will
4577 * assume that the creation of this chunk failed and
4578 * will never ask for it to be closed, resulting
4579 * in a leak and an inconsistent state for some
4582 enum lttcomm_return_code close_ret
;
4584 DBG("Failed to set new trace chunk on existing channels, rolling back");
4585 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4586 session_id
, chunk_id
,
4587 chunk_creation_timestamp
, NULL
);
4588 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4589 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4590 session_id
, chunk_id
);
4593 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4599 struct consumer_relayd_sock_pair
*relayd
;
4601 relayd
= consumer_find_relayd(*relayd_id
);
4603 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4604 ret
= relayd_create_trace_chunk(
4605 &relayd
->control_sock
, published_chunk
);
4606 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4608 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4611 if (!relayd
|| ret
) {
4612 enum lttcomm_return_code close_ret
;
4614 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4617 chunk_creation_timestamp
,
4619 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4620 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4625 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4631 /* Release the reference returned by the "publish" operation. */
4632 lttng_trace_chunk_put(published_chunk
);
4637 enum lttcomm_return_code
lttng_consumer_close_trace_chunk(
4638 const uint64_t *relayd_id
, uint64_t session_id
,
4639 uint64_t chunk_id
, time_t chunk_close_timestamp
,
4640 const enum lttng_trace_chunk_command_type
*close_command
)
4642 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4643 struct lttng_trace_chunk
*chunk
;
4644 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4645 const char *relayd_id_str
= "(none)";
4646 const char *close_command_name
= "none";
4647 struct lttng_ht_iter iter
;
4648 struct lttng_consumer_channel
*channel
;
4649 enum lttng_trace_chunk_status chunk_status
;
4654 /* Only used for logging purposes. */
4655 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4656 "%" PRIu64
, *relayd_id
);
4657 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4658 relayd_id_str
= relayd_id_buffer
;
4660 relayd_id_str
= "(formatting error)";
4663 if (close_command
) {
4664 close_command_name
= lttng_trace_chunk_command_type_get_name(
4668 DBG("Consumer close trace chunk command: relayd_id = %s"
4669 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4670 ", close command = %s",
4671 relayd_id_str
, session_id
, chunk_id
,
4672 close_command_name
);
4674 chunk
= lttng_trace_chunk_registry_find_chunk(
4675 consumer_data
.chunk_registry
, session_id
, chunk_id
);
4677 ERR("Failed to find chunk: session_id = %" PRIu64
4678 ", chunk_id = %" PRIu64
,
4679 session_id
, chunk_id
);
4680 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4684 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
,
4685 chunk_close_timestamp
);
4686 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4687 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4691 if (close_command
) {
4692 chunk_status
= lttng_trace_chunk_set_close_command(
4693 chunk
, *close_command
);
4694 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4695 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4701 * chunk is now invalid to access as we no longer hold a reference to
4702 * it; it is only kept around to compare it (by address) to the
4703 * current chunk found in the session's channels.
4706 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
,
4707 channel
, node
.node
) {
4711 * Only change the channel's chunk to NULL if it still
4712 * references the chunk being closed. The channel may
4713 * reference a newer channel in the case of a session
4714 * rotation. When a session rotation occurs, the "next"
4715 * chunk is created before the "current" chunk is closed.
4717 if (channel
->trace_chunk
!= chunk
) {
4720 ret
= lttng_consumer_channel_set_trace_chunk(channel
, NULL
);
4723 * Attempt to close the chunk on as many channels as
4726 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4732 struct consumer_relayd_sock_pair
*relayd
;
4734 relayd
= consumer_find_relayd(*relayd_id
);
4736 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4737 ret
= relayd_close_trace_chunk(
4738 &relayd
->control_sock
, chunk
);
4739 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4741 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
,
4745 if (!relayd
|| ret
) {
4746 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4754 * Release the reference returned by the "find" operation and
4755 * the session daemon's implicit reference to the chunk.
4757 lttng_trace_chunk_put(chunk
);
4758 lttng_trace_chunk_put(chunk
);
4763 enum lttcomm_return_code
lttng_consumer_trace_chunk_exists(
4764 const uint64_t *relayd_id
, uint64_t session_id
,
4768 enum lttcomm_return_code ret_code
;
4769 struct lttng_trace_chunk
*chunk
;
4770 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4771 const char *relayd_id_str
= "(none)";
4772 const bool is_local_trace
= !relayd_id
;
4773 struct consumer_relayd_sock_pair
*relayd
= NULL
;
4774 bool chunk_exists_remote
;
4779 /* Only used for logging purposes. */
4780 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4781 "%" PRIu64
, *relayd_id
);
4782 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4783 relayd_id_str
= relayd_id_buffer
;
4785 relayd_id_str
= "(formatting error)";
4789 DBG("Consumer trace chunk exists command: relayd_id = %s"
4790 ", chunk_id = %" PRIu64
, relayd_id_str
,
4792 chunk
= lttng_trace_chunk_registry_find_chunk(
4793 consumer_data
.chunk_registry
, session_id
,
4795 DBG("Trace chunk %s locally", chunk
? "exists" : "does not exist");
4797 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
4798 lttng_trace_chunk_put(chunk
);
4800 } else if (is_local_trace
) {
4801 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4806 relayd
= consumer_find_relayd(*relayd_id
);
4808 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
4809 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
4810 goto end_rcu_unlock
;
4812 DBG("Looking up existence of trace chunk on relay daemon");
4813 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4814 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
,
4815 &chunk_exists_remote
);
4816 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4818 ERR("Failed to look-up the existence of trace chunk on relay daemon");
4819 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
4820 goto end_rcu_unlock
;
4823 ret_code
= chunk_exists_remote
?
4824 LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
4825 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4826 DBG("Trace chunk %s on relay daemon",
4827 chunk_exists_remote
? "exists" : "does not exist");