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
);
713 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
714 * be acquired before calling this.
716 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
719 struct lttng_ht_node_u64
*node
;
720 struct lttng_ht_iter iter
;
724 lttng_ht_lookup(consumer_data
.relayd_ht
,
725 &relayd
->net_seq_idx
, &iter
);
726 node
= lttng_ht_iter_get_node_u64(&iter
);
730 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
737 * Allocate and return a consumer relayd socket.
739 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
740 uint64_t net_seq_idx
)
742 struct consumer_relayd_sock_pair
*obj
= NULL
;
744 /* net sequence index of -1 is a failure */
745 if (net_seq_idx
== (uint64_t) -1ULL) {
749 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
751 PERROR("zmalloc relayd sock");
755 obj
->net_seq_idx
= net_seq_idx
;
757 obj
->destroy_flag
= 0;
758 obj
->control_sock
.sock
.fd
= -1;
759 obj
->data_sock
.sock
.fd
= -1;
760 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
761 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
768 * Find a relayd socket pair in the global consumer data.
770 * Return the object if found else NULL.
771 * RCU read-side lock must be held across this call and while using the
774 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
776 struct lttng_ht_iter iter
;
777 struct lttng_ht_node_u64
*node
;
778 struct consumer_relayd_sock_pair
*relayd
= NULL
;
780 /* Negative keys are lookup failures */
781 if (key
== (uint64_t) -1ULL) {
785 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
787 node
= lttng_ht_iter_get_node_u64(&iter
);
789 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
797 * Find a relayd and send the stream
799 * Returns 0 on success, < 0 on error
801 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
805 struct consumer_relayd_sock_pair
*relayd
;
808 assert(stream
->net_seq_idx
!= -1ULL);
811 /* The stream is not metadata. Get relayd reference if exists. */
813 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
814 if (relayd
!= NULL
) {
815 /* Add stream on the relayd */
816 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
817 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
818 path
, &stream
->relayd_stream_id
,
819 stream
->chan
->tracefile_size
,
820 stream
->chan
->tracefile_count
,
821 stream
->trace_chunk
);
822 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
824 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
825 lttng_consumer_cleanup_relayd(relayd
);
829 uatomic_inc(&relayd
->refcount
);
830 stream
->sent_to_relayd
= 1;
832 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
833 stream
->key
, stream
->net_seq_idx
);
838 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
839 stream
->name
, stream
->key
, stream
->net_seq_idx
);
847 * Find a relayd and send the streams sent message
849 * Returns 0 on success, < 0 on error
851 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
854 struct consumer_relayd_sock_pair
*relayd
;
856 assert(net_seq_idx
!= -1ULL);
858 /* The stream is not metadata. Get relayd reference if exists. */
860 relayd
= consumer_find_relayd(net_seq_idx
);
861 if (relayd
!= NULL
) {
862 /* Add stream on the relayd */
863 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
864 ret
= relayd_streams_sent(&relayd
->control_sock
);
865 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
867 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
868 lttng_consumer_cleanup_relayd(relayd
);
872 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
879 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
887 * Find a relayd and close the stream
889 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
891 struct consumer_relayd_sock_pair
*relayd
;
893 /* The stream is not metadata. Get relayd reference if exists. */
895 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
897 consumer_stream_relayd_close(stream
, relayd
);
903 * Handle stream for relayd transmission if the stream applies for network
904 * streaming where the net sequence index is set.
906 * Return destination file descriptor or negative value on error.
908 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
909 size_t data_size
, unsigned long padding
,
910 struct consumer_relayd_sock_pair
*relayd
)
913 struct lttcomm_relayd_data_hdr data_hdr
;
919 /* Reset data header */
920 memset(&data_hdr
, 0, sizeof(data_hdr
));
922 if (stream
->metadata_flag
) {
923 /* Caller MUST acquire the relayd control socket lock */
924 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
929 /* Metadata are always sent on the control socket. */
930 outfd
= relayd
->control_sock
.sock
.fd
;
932 /* Set header with stream information */
933 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
934 data_hdr
.data_size
= htobe32(data_size
);
935 data_hdr
.padding_size
= htobe32(padding
);
938 * Note that net_seq_num below is assigned with the *current* value of
939 * next_net_seq_num and only after that the next_net_seq_num will be
940 * increment. This is why when issuing a command on the relayd using
941 * this next value, 1 should always be substracted in order to compare
942 * the last seen sequence number on the relayd side to the last sent.
944 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
945 /* Other fields are zeroed previously */
947 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
953 ++stream
->next_net_seq_num
;
955 /* Set to go on data socket */
956 outfd
= relayd
->data_sock
.sock
.fd
;
964 * Trigger a dump of the metadata content. Following/during the succesful
965 * completion of this call, the metadata poll thread will start receiving
966 * metadata packets to consume.
968 * The caller must hold the channel and stream locks.
971 int consumer_metadata_stream_dump(struct lttng_consumer_stream
*stream
)
975 ASSERT_LOCKED(stream
->chan
->lock
);
976 ASSERT_LOCKED(stream
->lock
);
977 assert(stream
->metadata_flag
);
978 assert(stream
->chan
->trace_chunk
);
980 switch (consumer_data
.type
) {
981 case LTTNG_CONSUMER_KERNEL
:
983 * Reset the position of what has been read from the
984 * metadata cache to 0 so we can dump it again.
986 ret
= kernctl_metadata_cache_dump(stream
->wait_fd
);
988 case LTTNG_CONSUMER32_UST
:
989 case LTTNG_CONSUMER64_UST
:
991 * Reset the position pushed from the metadata cache so it
992 * will write from the beginning on the next push.
994 stream
->ust_metadata_pushed
= 0;
995 ret
= consumer_metadata_wakeup_pipe(stream
->chan
);
998 ERR("Unknown consumer_data type");
1002 ERR("Failed to dump the metadata cache");
1008 int lttng_consumer_channel_set_trace_chunk(
1009 struct lttng_consumer_channel
*channel
,
1010 struct lttng_trace_chunk
*new_trace_chunk
)
1012 pthread_mutex_lock(&channel
->lock
);
1013 if (channel
->is_deleted
) {
1015 * The channel has been logically deleted and should no longer
1016 * be used. It has released its reference to its current trace
1017 * chunk and should not acquire a new one.
1019 * Return success as there is nothing for the caller to do.
1025 * The acquisition of the reference cannot fail (barring
1026 * a severe internal error) since a reference to the published
1027 * chunk is already held by the caller.
1029 if (new_trace_chunk
) {
1030 const bool acquired_reference
= lttng_trace_chunk_get(
1033 assert(acquired_reference
);
1036 lttng_trace_chunk_put(channel
->trace_chunk
);
1037 channel
->trace_chunk
= new_trace_chunk
;
1039 pthread_mutex_unlock(&channel
->lock
);
1044 * Allocate and return a new lttng_consumer_channel object using the given key
1045 * to initialize the hash table node.
1047 * On error, return NULL.
1049 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
1050 uint64_t session_id
,
1051 const uint64_t *chunk_id
,
1052 const char *pathname
,
1055 enum lttng_event_output output
,
1056 uint64_t tracefile_size
,
1057 uint64_t tracefile_count
,
1058 uint64_t session_id_per_pid
,
1059 unsigned int monitor
,
1060 unsigned int live_timer_interval
,
1061 const char *root_shm_path
,
1062 const char *shm_path
)
1064 struct lttng_consumer_channel
*channel
= NULL
;
1065 struct lttng_trace_chunk
*trace_chunk
= NULL
;
1068 trace_chunk
= lttng_trace_chunk_registry_find_chunk(
1069 consumer_data
.chunk_registry
, session_id
,
1072 ERR("Failed to find trace chunk reference during creation of channel");
1077 channel
= zmalloc(sizeof(*channel
));
1078 if (channel
== NULL
) {
1079 PERROR("malloc struct lttng_consumer_channel");
1084 channel
->refcount
= 0;
1085 channel
->session_id
= session_id
;
1086 channel
->session_id_per_pid
= session_id_per_pid
;
1087 channel
->relayd_id
= relayd_id
;
1088 channel
->tracefile_size
= tracefile_size
;
1089 channel
->tracefile_count
= tracefile_count
;
1090 channel
->monitor
= monitor
;
1091 channel
->live_timer_interval
= live_timer_interval
;
1092 pthread_mutex_init(&channel
->lock
, NULL
);
1093 pthread_mutex_init(&channel
->timer_lock
, NULL
);
1096 case LTTNG_EVENT_SPLICE
:
1097 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
1099 case LTTNG_EVENT_MMAP
:
1100 channel
->output
= CONSUMER_CHANNEL_MMAP
;
1110 * In monitor mode, the streams associated with the channel will be put in
1111 * a special list ONLY owned by this channel. So, the refcount is set to 1
1112 * here meaning that the channel itself has streams that are referenced.
1114 * On a channel deletion, once the channel is no longer visible, the
1115 * refcount is decremented and checked for a zero value to delete it. With
1116 * streams in no monitor mode, it will now be safe to destroy the channel.
1118 if (!channel
->monitor
) {
1119 channel
->refcount
= 1;
1122 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1123 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1125 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1126 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1128 if (root_shm_path
) {
1129 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1130 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1133 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1134 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1137 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1138 lttng_ht_node_init_u64(&channel
->channels_by_session_id_ht_node
,
1139 channel
->session_id
);
1141 channel
->wait_fd
= -1;
1142 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1145 int ret
= lttng_consumer_channel_set_trace_chunk(channel
,
1152 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1155 lttng_trace_chunk_put(trace_chunk
);
1158 consumer_del_channel(channel
);
1164 * Add a channel to the global list protected by a mutex.
1166 * Always return 0 indicating success.
1168 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1169 struct lttng_consumer_local_data
*ctx
)
1171 pthread_mutex_lock(&consumer_data
.lock
);
1172 pthread_mutex_lock(&channel
->lock
);
1173 pthread_mutex_lock(&channel
->timer_lock
);
1176 * This gives us a guarantee that the channel we are about to add to the
1177 * channel hash table will be unique. See this function comment on the why
1178 * we need to steel the channel key at this stage.
1180 steal_channel_key(channel
->key
);
1183 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1184 lttng_ht_add_u64(consumer_data
.channels_by_session_id_ht
,
1185 &channel
->channels_by_session_id_ht_node
);
1187 channel
->is_published
= true;
1189 pthread_mutex_unlock(&channel
->timer_lock
);
1190 pthread_mutex_unlock(&channel
->lock
);
1191 pthread_mutex_unlock(&consumer_data
.lock
);
1193 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1194 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1201 * Allocate the pollfd structure and the local view of the out fds to avoid
1202 * doing a lookup in the linked list and concurrency issues when writing is
1203 * needed. Called with consumer_data.lock held.
1205 * Returns the number of fds in the structures.
1207 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1208 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1209 struct lttng_ht
*ht
, int *nb_inactive_fd
)
1212 struct lttng_ht_iter iter
;
1213 struct lttng_consumer_stream
*stream
;
1218 assert(local_stream
);
1220 DBG("Updating poll fd array");
1221 *nb_inactive_fd
= 0;
1223 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1225 * Only active streams with an active end point can be added to the
1226 * poll set and local stream storage of the thread.
1228 * There is a potential race here for endpoint_status to be updated
1229 * just after the check. However, this is OK since the stream(s) will
1230 * be deleted once the thread is notified that the end point state has
1231 * changed where this function will be called back again.
1233 * We track the number of inactive FDs because they still need to be
1234 * closed by the polling thread after a wakeup on the data_pipe or
1237 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1238 (*nb_inactive_fd
)++;
1242 * This clobbers way too much the debug output. Uncomment that if you
1243 * need it for debugging purposes.
1245 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1246 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1247 local_stream
[i
] = stream
;
1253 * Insert the consumer_data_pipe at the end of the array and don't
1254 * increment i so nb_fd is the number of real FD.
1256 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1257 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1259 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1260 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1265 * Poll on the should_quit pipe and the command socket return -1 on
1266 * error, 1 if should exit, 0 if data is available on the command socket
1268 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1273 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1274 if (num_rdy
== -1) {
1276 * Restart interrupted system call.
1278 if (errno
== EINTR
) {
1281 PERROR("Poll error");
1284 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1285 DBG("consumer_should_quit wake up");
1292 * Set the error socket.
1294 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1297 ctx
->consumer_error_socket
= sock
;
1301 * Set the command socket path.
1303 void lttng_consumer_set_command_sock_path(
1304 struct lttng_consumer_local_data
*ctx
, char *sock
)
1306 ctx
->consumer_command_sock_path
= sock
;
1310 * Send return code to the session daemon.
1311 * If the socket is not defined, we return 0, it is not a fatal error
1313 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1315 if (ctx
->consumer_error_socket
> 0) {
1316 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1317 sizeof(enum lttcomm_sessiond_command
));
1324 * Close all the tracefiles and stream fds and MUST be called when all
1325 * instances are destroyed i.e. when all threads were joined and are ended.
1327 void lttng_consumer_cleanup(void)
1329 struct lttng_ht_iter iter
;
1330 struct lttng_consumer_channel
*channel
;
1331 unsigned int trace_chunks_left
;
1335 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1337 consumer_del_channel(channel
);
1342 lttng_ht_destroy(consumer_data
.channel_ht
);
1343 lttng_ht_destroy(consumer_data
.channels_by_session_id_ht
);
1345 cleanup_relayd_ht();
1347 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1350 * This HT contains streams that are freed by either the metadata thread or
1351 * the data thread so we do *nothing* on the hash table and simply destroy
1354 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1357 * Trace chunks in the registry may still exist if the session
1358 * daemon has encountered an internal error and could not
1359 * tear down its sessions and/or trace chunks properly.
1361 * Release the session daemon's implicit reference to any remaining
1362 * trace chunk and print an error if any trace chunk was found. Note
1363 * that there are _no_ legitimate cases for trace chunks to be left,
1364 * it is a leak. However, it can happen following a crash of the
1365 * session daemon and not emptying the registry would cause an assertion
1368 trace_chunks_left
= lttng_trace_chunk_registry_put_each_chunk(
1369 consumer_data
.chunk_registry
);
1370 if (trace_chunks_left
) {
1371 ERR("%u trace chunks are leaked by lttng-consumerd. "
1372 "This can be caused by an internal error of the session daemon.",
1375 /* Run all callbacks freeing each chunk. */
1377 lttng_trace_chunk_registry_destroy(consumer_data
.chunk_registry
);
1381 * Called from signal handler.
1383 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1387 CMM_STORE_SHARED(consumer_quit
, 1);
1388 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1390 PERROR("write consumer quit");
1393 DBG("Consumer flag that it should quit");
1398 * Flush pending writes to trace output disk file.
1401 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1405 int outfd
= stream
->out_fd
;
1408 * This does a blocking write-and-wait on any page that belongs to the
1409 * subbuffer prior to the one we just wrote.
1410 * Don't care about error values, as these are just hints and ways to
1411 * limit the amount of page cache used.
1413 if (orig_offset
< stream
->max_sb_size
) {
1416 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1417 stream
->max_sb_size
,
1418 SYNC_FILE_RANGE_WAIT_BEFORE
1419 | SYNC_FILE_RANGE_WRITE
1420 | SYNC_FILE_RANGE_WAIT_AFTER
);
1422 * Give hints to the kernel about how we access the file:
1423 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1426 * We need to call fadvise again after the file grows because the
1427 * kernel does not seem to apply fadvise to non-existing parts of the
1430 * Call fadvise _after_ having waited for the page writeback to
1431 * complete because the dirty page writeback semantic is not well
1432 * defined. So it can be expected to lead to lower throughput in
1435 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1436 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1437 if (ret
&& ret
!= -ENOSYS
) {
1439 PERROR("posix_fadvise on fd %i", outfd
);
1444 * Initialise the necessary environnement :
1445 * - create a new context
1446 * - create the poll_pipe
1447 * - create the should_quit pipe (for signal handler)
1448 * - create the thread pipe (for splice)
1450 * Takes a function pointer as argument, this function is called when data is
1451 * available on a buffer. This function is responsible to do the
1452 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1453 * buffer configuration and then kernctl_put_next_subbuf at the end.
1455 * Returns a pointer to the new context or NULL on error.
1457 struct lttng_consumer_local_data
*lttng_consumer_create(
1458 enum lttng_consumer_type type
,
1459 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1460 struct lttng_consumer_local_data
*ctx
),
1461 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1462 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1463 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1466 struct lttng_consumer_local_data
*ctx
;
1468 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1469 consumer_data
.type
== type
);
1470 consumer_data
.type
= type
;
1472 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1474 PERROR("allocating context");
1478 ctx
->consumer_error_socket
= -1;
1479 ctx
->consumer_metadata_socket
= -1;
1480 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1481 /* assign the callbacks */
1482 ctx
->on_buffer_ready
= buffer_ready
;
1483 ctx
->on_recv_channel
= recv_channel
;
1484 ctx
->on_recv_stream
= recv_stream
;
1485 ctx
->on_update_stream
= update_stream
;
1487 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1488 if (!ctx
->consumer_data_pipe
) {
1489 goto error_poll_pipe
;
1492 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1493 if (!ctx
->consumer_wakeup_pipe
) {
1494 goto error_wakeup_pipe
;
1497 ret
= pipe(ctx
->consumer_should_quit
);
1499 PERROR("Error creating recv pipe");
1500 goto error_quit_pipe
;
1503 ret
= pipe(ctx
->consumer_channel_pipe
);
1505 PERROR("Error creating channel pipe");
1506 goto error_channel_pipe
;
1509 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1510 if (!ctx
->consumer_metadata_pipe
) {
1511 goto error_metadata_pipe
;
1514 ctx
->channel_monitor_pipe
= -1;
1518 error_metadata_pipe
:
1519 utils_close_pipe(ctx
->consumer_channel_pipe
);
1521 utils_close_pipe(ctx
->consumer_should_quit
);
1523 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1525 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1533 * Iterate over all streams of the hashtable and free them properly.
1535 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1537 struct lttng_ht_iter iter
;
1538 struct lttng_consumer_stream
*stream
;
1545 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1547 * Ignore return value since we are currently cleaning up so any error
1550 (void) consumer_del_stream(stream
, ht
);
1554 lttng_ht_destroy(ht
);
1558 * Iterate over all streams of the metadata hashtable and free them
1561 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1563 struct lttng_ht_iter iter
;
1564 struct lttng_consumer_stream
*stream
;
1571 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1573 * Ignore return value since we are currently cleaning up so any error
1576 (void) consumer_del_metadata_stream(stream
, ht
);
1580 lttng_ht_destroy(ht
);
1584 * Close all fds associated with the instance and free the context.
1586 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1590 DBG("Consumer destroying it. Closing everything.");
1596 destroy_data_stream_ht(data_ht
);
1597 destroy_metadata_stream_ht(metadata_ht
);
1599 ret
= close(ctx
->consumer_error_socket
);
1603 ret
= close(ctx
->consumer_metadata_socket
);
1607 utils_close_pipe(ctx
->consumer_channel_pipe
);
1608 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1609 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1610 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1611 utils_close_pipe(ctx
->consumer_should_quit
);
1613 unlink(ctx
->consumer_command_sock_path
);
1618 * Write the metadata stream id on the specified file descriptor.
1620 static int write_relayd_metadata_id(int fd
,
1621 struct lttng_consumer_stream
*stream
,
1622 unsigned long padding
)
1625 struct lttcomm_relayd_metadata_payload hdr
;
1627 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1628 hdr
.padding_size
= htobe32(padding
);
1629 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1630 if (ret
< sizeof(hdr
)) {
1632 * This error means that the fd's end is closed so ignore the PERROR
1633 * not to clubber the error output since this can happen in a normal
1636 if (errno
!= EPIPE
) {
1637 PERROR("write metadata stream id");
1639 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1641 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1642 * handle writting the missing part so report that as an error and
1643 * don't lie to the caller.
1648 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1649 stream
->relayd_stream_id
, padding
);
1656 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1657 * core function for writing trace buffers to either the local filesystem or
1660 * It must be called with the stream and the channel lock held.
1662 * Careful review MUST be put if any changes occur!
1664 * Returns the number of bytes written
1666 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1667 struct lttng_consumer_local_data
*ctx
,
1668 struct lttng_consumer_stream
*stream
, unsigned long len
,
1669 unsigned long padding
,
1670 struct ctf_packet_index
*index
)
1672 unsigned long mmap_offset
;
1675 off_t orig_offset
= stream
->out_fd_offset
;
1676 /* Default is on the disk */
1677 int outfd
= stream
->out_fd
;
1678 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1679 unsigned int relayd_hang_up
= 0;
1681 /* RCU lock for the relayd pointer */
1683 assert(stream
->net_seq_idx
!= (uint64_t) -1ULL ||
1684 stream
->trace_chunk
);
1686 /* Flag that the current stream if set for network streaming. */
1687 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1688 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1689 if (relayd
== NULL
) {
1695 /* get the offset inside the fd to mmap */
1696 switch (consumer_data
.type
) {
1697 case LTTNG_CONSUMER_KERNEL
:
1698 mmap_base
= stream
->mmap_base
;
1699 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1701 PERROR("tracer ctl get_mmap_read_offset");
1705 case LTTNG_CONSUMER32_UST
:
1706 case LTTNG_CONSUMER64_UST
:
1707 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1709 ERR("read mmap get mmap base for stream %s", stream
->name
);
1713 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1715 PERROR("tracer ctl get_mmap_read_offset");
1721 ERR("Unknown consumer_data type");
1725 /* Handle stream on the relayd if the output is on the network */
1727 unsigned long netlen
= len
;
1730 * Lock the control socket for the complete duration of the function
1731 * since from this point on we will use the socket.
1733 if (stream
->metadata_flag
) {
1734 /* Metadata requires the control socket. */
1735 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1736 if (stream
->reset_metadata_flag
) {
1737 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1738 stream
->relayd_stream_id
,
1739 stream
->metadata_version
);
1744 stream
->reset_metadata_flag
= 0;
1746 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1749 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1754 /* Use the returned socket. */
1757 /* Write metadata stream id before payload */
1758 if (stream
->metadata_flag
) {
1759 ret
= write_relayd_metadata_id(outfd
, stream
, padding
);
1766 /* No streaming, we have to set the len with the full padding */
1769 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1770 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1772 ERR("Reset metadata file");
1775 stream
->reset_metadata_flag
= 0;
1779 * Check if we need to change the tracefile before writing the packet.
1781 if (stream
->chan
->tracefile_size
> 0 &&
1782 (stream
->tracefile_size_current
+ len
) >
1783 stream
->chan
->tracefile_size
) {
1784 ret
= consumer_stream_rotate_output_files(stream
);
1788 outfd
= stream
->out_fd
;
1791 stream
->tracefile_size_current
+= len
;
1793 index
->offset
= htobe64(stream
->out_fd_offset
);
1798 * This call guarantee that len or less is returned. It's impossible to
1799 * receive a ret value that is bigger than len.
1801 ret
= lttng_write(outfd
, mmap_base
+ mmap_offset
, len
);
1802 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1803 if (ret
< 0 || ((size_t) ret
!= len
)) {
1805 * Report error to caller if nothing was written else at least send the
1813 /* Socket operation failed. We consider the relayd dead */
1814 if (errno
== EPIPE
) {
1816 * This is possible if the fd is closed on the other side
1817 * (outfd) or any write problem. It can be verbose a bit for a
1818 * normal execution if for instance the relayd is stopped
1819 * abruptly. This can happen so set this to a DBG statement.
1821 DBG("Consumer mmap write detected relayd hang up");
1823 /* Unhandled error, print it and stop function right now. */
1824 PERROR("Error in write mmap (ret %zd != len %lu)", ret
, len
);
1828 stream
->output_written
+= ret
;
1830 /* This call is useless on a socket so better save a syscall. */
1832 /* This won't block, but will start writeout asynchronously */
1833 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, len
,
1834 SYNC_FILE_RANGE_WRITE
);
1835 stream
->out_fd_offset
+= len
;
1836 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1841 * This is a special case that the relayd has closed its socket. Let's
1842 * cleanup the relayd object and all associated streams.
1844 if (relayd
&& relayd_hang_up
) {
1845 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1846 lttng_consumer_cleanup_relayd(relayd
);
1850 /* Unlock only if ctrl socket used */
1851 if (relayd
&& stream
->metadata_flag
) {
1852 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1860 * Splice the data from the ring buffer to the tracefile.
1862 * It must be called with the stream lock held.
1864 * Returns the number of bytes spliced.
1866 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1867 struct lttng_consumer_local_data
*ctx
,
1868 struct lttng_consumer_stream
*stream
, unsigned long len
,
1869 unsigned long padding
,
1870 struct ctf_packet_index
*index
)
1872 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1874 off_t orig_offset
= stream
->out_fd_offset
;
1875 int fd
= stream
->wait_fd
;
1876 /* Default is on the disk */
1877 int outfd
= stream
->out_fd
;
1878 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1880 unsigned int relayd_hang_up
= 0;
1882 switch (consumer_data
.type
) {
1883 case LTTNG_CONSUMER_KERNEL
:
1885 case LTTNG_CONSUMER32_UST
:
1886 case LTTNG_CONSUMER64_UST
:
1887 /* Not supported for user space tracing */
1890 ERR("Unknown consumer_data type");
1894 /* RCU lock for the relayd pointer */
1897 /* Flag that the current stream if set for network streaming. */
1898 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1899 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1900 if (relayd
== NULL
) {
1905 splice_pipe
= stream
->splice_pipe
;
1907 /* Write metadata stream id before payload */
1909 unsigned long total_len
= len
;
1911 if (stream
->metadata_flag
) {
1913 * Lock the control socket for the complete duration of the function
1914 * since from this point on we will use the socket.
1916 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1918 if (stream
->reset_metadata_flag
) {
1919 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1920 stream
->relayd_stream_id
,
1921 stream
->metadata_version
);
1926 stream
->reset_metadata_flag
= 0;
1928 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
,
1936 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1939 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1945 /* Use the returned socket. */
1948 /* No streaming, we have to set the len with the full padding */
1951 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1952 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1954 ERR("Reset metadata file");
1957 stream
->reset_metadata_flag
= 0;
1960 * Check if we need to change the tracefile before writing the packet.
1962 if (stream
->chan
->tracefile_size
> 0 &&
1963 (stream
->tracefile_size_current
+ len
) >
1964 stream
->chan
->tracefile_size
) {
1965 ret
= consumer_stream_rotate_output_files(stream
);
1970 outfd
= stream
->out_fd
;
1973 stream
->tracefile_size_current
+= len
;
1974 index
->offset
= htobe64(stream
->out_fd_offset
);
1978 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1979 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1980 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1981 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1982 DBG("splice chan to pipe, ret %zd", ret_splice
);
1983 if (ret_splice
< 0) {
1986 PERROR("Error in relay splice");
1990 /* Handle stream on the relayd if the output is on the network */
1991 if (relayd
&& stream
->metadata_flag
) {
1992 size_t metadata_payload_size
=
1993 sizeof(struct lttcomm_relayd_metadata_payload
);
1995 /* Update counter to fit the spliced data */
1996 ret_splice
+= metadata_payload_size
;
1997 len
+= metadata_payload_size
;
1999 * We do this so the return value can match the len passed as
2000 * argument to this function.
2002 written
-= metadata_payload_size
;
2005 /* Splice data out */
2006 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
2007 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
2008 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
2010 if (ret_splice
< 0) {
2015 } else if (ret_splice
> len
) {
2017 * We don't expect this code path to be executed but you never know
2018 * so this is an extra protection agains a buggy splice().
2021 written
+= ret_splice
;
2022 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
2026 /* All good, update current len and continue. */
2030 /* This call is useless on a socket so better save a syscall. */
2032 /* This won't block, but will start writeout asynchronously */
2033 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
2034 SYNC_FILE_RANGE_WRITE
);
2035 stream
->out_fd_offset
+= ret_splice
;
2037 stream
->output_written
+= ret_splice
;
2038 written
+= ret_splice
;
2041 lttng_consumer_sync_trace_file(stream
, orig_offset
);
2047 * This is a special case that the relayd has closed its socket. Let's
2048 * cleanup the relayd object and all associated streams.
2050 if (relayd
&& relayd_hang_up
) {
2051 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
2052 lttng_consumer_cleanup_relayd(relayd
);
2053 /* Skip splice error so the consumer does not fail */
2058 /* send the appropriate error description to sessiond */
2061 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
2064 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
2067 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
2072 if (relayd
&& stream
->metadata_flag
) {
2073 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2081 * Sample the snapshot positions for a specific fd
2083 * Returns 0 on success, < 0 on error
2085 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
2087 switch (consumer_data
.type
) {
2088 case LTTNG_CONSUMER_KERNEL
:
2089 return lttng_kconsumer_sample_snapshot_positions(stream
);
2090 case LTTNG_CONSUMER32_UST
:
2091 case LTTNG_CONSUMER64_UST
:
2092 return lttng_ustconsumer_sample_snapshot_positions(stream
);
2094 ERR("Unknown consumer_data type");
2100 * Take a snapshot for a specific fd
2102 * Returns 0 on success, < 0 on error
2104 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2106 switch (consumer_data
.type
) {
2107 case LTTNG_CONSUMER_KERNEL
:
2108 return lttng_kconsumer_take_snapshot(stream
);
2109 case LTTNG_CONSUMER32_UST
:
2110 case LTTNG_CONSUMER64_UST
:
2111 return lttng_ustconsumer_take_snapshot(stream
);
2113 ERR("Unknown consumer_data type");
2120 * Get the produced position
2122 * Returns 0 on success, < 0 on error
2124 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
2127 switch (consumer_data
.type
) {
2128 case LTTNG_CONSUMER_KERNEL
:
2129 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
2130 case LTTNG_CONSUMER32_UST
:
2131 case LTTNG_CONSUMER64_UST
:
2132 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2134 ERR("Unknown consumer_data type");
2141 * Get the consumed position (free-running counter position in bytes).
2143 * Returns 0 on success, < 0 on error
2145 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
2148 switch (consumer_data
.type
) {
2149 case LTTNG_CONSUMER_KERNEL
:
2150 return lttng_kconsumer_get_consumed_snapshot(stream
, pos
);
2151 case LTTNG_CONSUMER32_UST
:
2152 case LTTNG_CONSUMER64_UST
:
2153 return lttng_ustconsumer_get_consumed_snapshot(stream
, pos
);
2155 ERR("Unknown consumer_data type");
2161 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2162 int sock
, struct pollfd
*consumer_sockpoll
)
2164 switch (consumer_data
.type
) {
2165 case LTTNG_CONSUMER_KERNEL
:
2166 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2167 case LTTNG_CONSUMER32_UST
:
2168 case LTTNG_CONSUMER64_UST
:
2169 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2171 ERR("Unknown consumer_data type");
2178 void lttng_consumer_close_all_metadata(void)
2180 switch (consumer_data
.type
) {
2181 case LTTNG_CONSUMER_KERNEL
:
2183 * The Kernel consumer has a different metadata scheme so we don't
2184 * close anything because the stream will be closed by the session
2188 case LTTNG_CONSUMER32_UST
:
2189 case LTTNG_CONSUMER64_UST
:
2191 * Close all metadata streams. The metadata hash table is passed and
2192 * this call iterates over it by closing all wakeup fd. This is safe
2193 * because at this point we are sure that the metadata producer is
2194 * either dead or blocked.
2196 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2199 ERR("Unknown consumer_data type");
2205 * Clean up a metadata stream and free its memory.
2207 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2208 struct lttng_ht
*ht
)
2210 struct lttng_consumer_channel
*channel
= NULL
;
2211 bool free_channel
= false;
2215 * This call should NEVER receive regular stream. It must always be
2216 * metadata stream and this is crucial for data structure synchronization.
2218 assert(stream
->metadata_flag
);
2220 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2222 pthread_mutex_lock(&consumer_data
.lock
);
2224 * Note that this assumes that a stream's channel is never changed and
2225 * that the stream's lock doesn't need to be taken to sample its
2228 channel
= stream
->chan
;
2229 pthread_mutex_lock(&channel
->lock
);
2230 pthread_mutex_lock(&stream
->lock
);
2231 if (channel
->metadata_cache
) {
2232 /* Only applicable to userspace consumers. */
2233 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
2236 /* Remove any reference to that stream. */
2237 consumer_stream_delete(stream
, ht
);
2239 /* Close down everything including the relayd if one. */
2240 consumer_stream_close(stream
);
2241 /* Destroy tracer buffers of the stream. */
2242 consumer_stream_destroy_buffers(stream
);
2244 /* Atomically decrement channel refcount since other threads can use it. */
2245 if (!uatomic_sub_return(&channel
->refcount
, 1)
2246 && !uatomic_read(&channel
->nb_init_stream_left
)) {
2247 /* Go for channel deletion! */
2248 free_channel
= true;
2250 stream
->chan
= NULL
;
2253 * Nullify the stream reference so it is not used after deletion. The
2254 * channel lock MUST be acquired before being able to check for a NULL
2257 channel
->metadata_stream
= NULL
;
2259 if (channel
->metadata_cache
) {
2260 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
2262 pthread_mutex_unlock(&stream
->lock
);
2263 pthread_mutex_unlock(&channel
->lock
);
2264 pthread_mutex_unlock(&consumer_data
.lock
);
2267 consumer_del_channel(channel
);
2270 lttng_trace_chunk_put(stream
->trace_chunk
);
2271 stream
->trace_chunk
= NULL
;
2272 consumer_stream_free(stream
);
2276 * Action done with the metadata stream when adding it to the consumer internal
2277 * data structures to handle it.
2279 void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2281 struct lttng_ht
*ht
= metadata_ht
;
2282 struct lttng_ht_iter iter
;
2283 struct lttng_ht_node_u64
*node
;
2288 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2290 pthread_mutex_lock(&consumer_data
.lock
);
2291 pthread_mutex_lock(&stream
->chan
->lock
);
2292 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2293 pthread_mutex_lock(&stream
->lock
);
2296 * From here, refcounts are updated so be _careful_ when returning an error
2303 * Lookup the stream just to make sure it does not exist in our internal
2304 * state. This should NEVER happen.
2306 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2307 node
= lttng_ht_iter_get_node_u64(&iter
);
2311 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2312 * in terms of destroying the associated channel, because the action that
2313 * causes the count to become 0 also causes a stream to be added. The
2314 * channel deletion will thus be triggered by the following removal of this
2317 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2318 /* Increment refcount before decrementing nb_init_stream_left */
2320 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2323 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2325 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
2326 &stream
->node_channel_id
);
2329 * Add stream to the stream_list_ht of the consumer data. No need to steal
2330 * the key since the HT does not use it and we allow to add redundant keys
2333 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2337 pthread_mutex_unlock(&stream
->lock
);
2338 pthread_mutex_unlock(&stream
->chan
->lock
);
2339 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2340 pthread_mutex_unlock(&consumer_data
.lock
);
2344 * Delete data stream that are flagged for deletion (endpoint_status).
2346 static void validate_endpoint_status_data_stream(void)
2348 struct lttng_ht_iter iter
;
2349 struct lttng_consumer_stream
*stream
;
2351 DBG("Consumer delete flagged data stream");
2354 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2355 /* Validate delete flag of the stream */
2356 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2359 /* Delete it right now */
2360 consumer_del_stream(stream
, data_ht
);
2366 * Delete metadata stream that are flagged for deletion (endpoint_status).
2368 static void validate_endpoint_status_metadata_stream(
2369 struct lttng_poll_event
*pollset
)
2371 struct lttng_ht_iter iter
;
2372 struct lttng_consumer_stream
*stream
;
2374 DBG("Consumer delete flagged metadata stream");
2379 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2380 /* Validate delete flag of the stream */
2381 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2385 * Remove from pollset so the metadata thread can continue without
2386 * blocking on a deleted stream.
2388 lttng_poll_del(pollset
, stream
->wait_fd
);
2390 /* Delete it right now */
2391 consumer_del_metadata_stream(stream
, metadata_ht
);
2397 * Thread polls on metadata file descriptor and write them on disk or on the
2400 void *consumer_thread_metadata_poll(void *data
)
2402 int ret
, i
, pollfd
, err
= -1;
2403 uint32_t revents
, nb_fd
;
2404 struct lttng_consumer_stream
*stream
= NULL
;
2405 struct lttng_ht_iter iter
;
2406 struct lttng_ht_node_u64
*node
;
2407 struct lttng_poll_event events
;
2408 struct lttng_consumer_local_data
*ctx
= data
;
2411 rcu_register_thread();
2413 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2415 if (testpoint(consumerd_thread_metadata
)) {
2416 goto error_testpoint
;
2419 health_code_update();
2421 DBG("Thread metadata poll started");
2423 /* Size is set to 1 for the consumer_metadata pipe */
2424 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2426 ERR("Poll set creation failed");
2430 ret
= lttng_poll_add(&events
,
2431 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2437 DBG("Metadata main loop started");
2441 health_code_update();
2442 health_poll_entry();
2443 DBG("Metadata poll wait");
2444 ret
= lttng_poll_wait(&events
, -1);
2445 DBG("Metadata poll return from wait with %d fd(s)",
2446 LTTNG_POLL_GETNB(&events
));
2448 DBG("Metadata event caught in thread");
2450 if (errno
== EINTR
) {
2451 ERR("Poll EINTR caught");
2454 if (LTTNG_POLL_GETNB(&events
) == 0) {
2455 err
= 0; /* All is OK */
2462 /* From here, the event is a metadata wait fd */
2463 for (i
= 0; i
< nb_fd
; i
++) {
2464 health_code_update();
2466 revents
= LTTNG_POLL_GETEV(&events
, i
);
2467 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2469 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2470 if (revents
& LPOLLIN
) {
2473 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2474 &stream
, sizeof(stream
));
2475 if (pipe_len
< sizeof(stream
)) {
2477 PERROR("read metadata stream");
2480 * Remove the pipe from the poll set and continue the loop
2481 * since their might be data to consume.
2483 lttng_poll_del(&events
,
2484 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2485 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2489 /* A NULL stream means that the state has changed. */
2490 if (stream
== NULL
) {
2491 /* Check for deleted streams. */
2492 validate_endpoint_status_metadata_stream(&events
);
2496 DBG("Adding metadata stream %d to poll set",
2499 /* Add metadata stream to the global poll events list */
2500 lttng_poll_add(&events
, stream
->wait_fd
,
2501 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2502 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2503 DBG("Metadata thread pipe hung up");
2505 * Remove the pipe from the poll set and continue the loop
2506 * since their might be data to consume.
2508 lttng_poll_del(&events
,
2509 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2510 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2513 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2517 /* Handle other stream */
2523 uint64_t tmp_id
= (uint64_t) pollfd
;
2525 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2527 node
= lttng_ht_iter_get_node_u64(&iter
);
2530 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2533 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2534 /* Get the data out of the metadata file descriptor */
2535 DBG("Metadata available on fd %d", pollfd
);
2536 assert(stream
->wait_fd
== pollfd
);
2539 health_code_update();
2541 len
= ctx
->on_buffer_ready(stream
, ctx
);
2543 * We don't check the return value here since if we get
2544 * a negative len, it means an error occurred thus we
2545 * simply remove it from the poll set and free the
2550 /* It's ok to have an unavailable sub-buffer */
2551 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2552 /* Clean up stream from consumer and free it. */
2553 lttng_poll_del(&events
, stream
->wait_fd
);
2554 consumer_del_metadata_stream(stream
, metadata_ht
);
2556 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2557 DBG("Metadata fd %d is hup|err.", pollfd
);
2558 if (!stream
->hangup_flush_done
2559 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2560 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2561 DBG("Attempting to flush and consume the UST buffers");
2562 lttng_ustconsumer_on_stream_hangup(stream
);
2564 /* We just flushed the stream now read it. */
2566 health_code_update();
2568 len
= ctx
->on_buffer_ready(stream
, ctx
);
2570 * We don't check the return value here since if we get
2571 * a negative len, it means an error occurred thus we
2572 * simply remove it from the poll set and free the
2578 lttng_poll_del(&events
, stream
->wait_fd
);
2580 * This call update the channel states, closes file descriptors
2581 * and securely free the stream.
2583 consumer_del_metadata_stream(stream
, metadata_ht
);
2585 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2589 /* Release RCU lock for the stream looked up */
2597 DBG("Metadata poll thread exiting");
2599 lttng_poll_clean(&events
);
2604 ERR("Health error occurred in %s", __func__
);
2606 health_unregister(health_consumerd
);
2607 rcu_unregister_thread();
2612 * This thread polls the fds in the set to consume the data and write
2613 * it to tracefile if necessary.
2615 void *consumer_thread_data_poll(void *data
)
2617 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2618 struct pollfd
*pollfd
= NULL
;
2619 /* local view of the streams */
2620 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2621 /* local view of consumer_data.fds_count */
2623 /* 2 for the consumer_data_pipe and wake up pipe */
2624 const int nb_pipes_fd
= 2;
2625 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2626 int nb_inactive_fd
= 0;
2627 struct lttng_consumer_local_data
*ctx
= data
;
2630 rcu_register_thread();
2632 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2634 if (testpoint(consumerd_thread_data
)) {
2635 goto error_testpoint
;
2638 health_code_update();
2640 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2641 if (local_stream
== NULL
) {
2642 PERROR("local_stream malloc");
2647 health_code_update();
2653 * the fds set has been updated, we need to update our
2654 * local array as well
2656 pthread_mutex_lock(&consumer_data
.lock
);
2657 if (consumer_data
.need_update
) {
2662 local_stream
= NULL
;
2664 /* Allocate for all fds */
2665 pollfd
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) * sizeof(struct pollfd
));
2666 if (pollfd
== NULL
) {
2667 PERROR("pollfd malloc");
2668 pthread_mutex_unlock(&consumer_data
.lock
);
2672 local_stream
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) *
2673 sizeof(struct lttng_consumer_stream
*));
2674 if (local_stream
== NULL
) {
2675 PERROR("local_stream malloc");
2676 pthread_mutex_unlock(&consumer_data
.lock
);
2679 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2680 data_ht
, &nb_inactive_fd
);
2682 ERR("Error in allocating pollfd or local_outfds");
2683 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2684 pthread_mutex_unlock(&consumer_data
.lock
);
2688 consumer_data
.need_update
= 0;
2690 pthread_mutex_unlock(&consumer_data
.lock
);
2692 /* No FDs and consumer_quit, consumer_cleanup the thread */
2693 if (nb_fd
== 0 && nb_inactive_fd
== 0 &&
2694 CMM_LOAD_SHARED(consumer_quit
) == 1) {
2695 err
= 0; /* All is OK */
2698 /* poll on the array of fds */
2700 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2701 if (testpoint(consumerd_thread_data_poll
)) {
2704 health_poll_entry();
2705 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2707 DBG("poll num_rdy : %d", num_rdy
);
2708 if (num_rdy
== -1) {
2710 * Restart interrupted system call.
2712 if (errno
== EINTR
) {
2715 PERROR("Poll error");
2716 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2718 } else if (num_rdy
== 0) {
2719 DBG("Polling thread timed out");
2723 if (caa_unlikely(data_consumption_paused
)) {
2724 DBG("Data consumption paused, sleeping...");
2730 * If the consumer_data_pipe triggered poll go directly to the
2731 * beginning of the loop to update the array. We want to prioritize
2732 * array update over low-priority reads.
2734 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2735 ssize_t pipe_readlen
;
2737 DBG("consumer_data_pipe wake up");
2738 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2739 &new_stream
, sizeof(new_stream
));
2740 if (pipe_readlen
< sizeof(new_stream
)) {
2741 PERROR("Consumer data pipe");
2742 /* Continue so we can at least handle the current stream(s). */
2747 * If the stream is NULL, just ignore it. It's also possible that
2748 * the sessiond poll thread changed the consumer_quit state and is
2749 * waking us up to test it.
2751 if (new_stream
== NULL
) {
2752 validate_endpoint_status_data_stream();
2756 /* Continue to update the local streams and handle prio ones */
2760 /* Handle wakeup pipe. */
2761 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2763 ssize_t pipe_readlen
;
2765 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2767 if (pipe_readlen
< 0) {
2768 PERROR("Consumer data wakeup pipe");
2770 /* We've been awakened to handle stream(s). */
2771 ctx
->has_wakeup
= 0;
2774 /* Take care of high priority channels first. */
2775 for (i
= 0; i
< nb_fd
; i
++) {
2776 health_code_update();
2778 if (local_stream
[i
] == NULL
) {
2781 if (pollfd
[i
].revents
& POLLPRI
) {
2782 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2784 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2785 /* it's ok to have an unavailable sub-buffer */
2786 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2787 /* Clean the stream and free it. */
2788 consumer_del_stream(local_stream
[i
], data_ht
);
2789 local_stream
[i
] = NULL
;
2790 } else if (len
> 0) {
2791 local_stream
[i
]->data_read
= 1;
2797 * If we read high prio channel in this loop, try again
2798 * for more high prio data.
2804 /* Take care of low priority channels. */
2805 for (i
= 0; i
< nb_fd
; i
++) {
2806 health_code_update();
2808 if (local_stream
[i
] == NULL
) {
2811 if ((pollfd
[i
].revents
& POLLIN
) ||
2812 local_stream
[i
]->hangup_flush_done
||
2813 local_stream
[i
]->has_data
) {
2814 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2815 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2816 /* it's ok to have an unavailable sub-buffer */
2817 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2818 /* Clean the stream and free it. */
2819 consumer_del_stream(local_stream
[i
], data_ht
);
2820 local_stream
[i
] = NULL
;
2821 } else if (len
> 0) {
2822 local_stream
[i
]->data_read
= 1;
2827 /* Handle hangup and errors */
2828 for (i
= 0; i
< nb_fd
; i
++) {
2829 health_code_update();
2831 if (local_stream
[i
] == NULL
) {
2834 if (!local_stream
[i
]->hangup_flush_done
2835 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2836 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2837 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2838 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2840 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2841 /* Attempt read again, for the data we just flushed. */
2842 local_stream
[i
]->data_read
= 1;
2845 * If the poll flag is HUP/ERR/NVAL and we have
2846 * read no data in this pass, we can remove the
2847 * stream from its hash table.
2849 if ((pollfd
[i
].revents
& POLLHUP
)) {
2850 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2851 if (!local_stream
[i
]->data_read
) {
2852 consumer_del_stream(local_stream
[i
], data_ht
);
2853 local_stream
[i
] = NULL
;
2856 } else if (pollfd
[i
].revents
& POLLERR
) {
2857 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2858 if (!local_stream
[i
]->data_read
) {
2859 consumer_del_stream(local_stream
[i
], data_ht
);
2860 local_stream
[i
] = NULL
;
2863 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2864 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2865 if (!local_stream
[i
]->data_read
) {
2866 consumer_del_stream(local_stream
[i
], data_ht
);
2867 local_stream
[i
] = NULL
;
2871 if (local_stream
[i
] != NULL
) {
2872 local_stream
[i
]->data_read
= 0;
2879 DBG("polling thread exiting");
2884 * Close the write side of the pipe so epoll_wait() in
2885 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2886 * read side of the pipe. If we close them both, epoll_wait strangely does
2887 * not return and could create a endless wait period if the pipe is the
2888 * only tracked fd in the poll set. The thread will take care of closing
2891 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2896 ERR("Health error occurred in %s", __func__
);
2898 health_unregister(health_consumerd
);
2900 rcu_unregister_thread();
2905 * Close wake-up end of each stream belonging to the channel. This will
2906 * allow the poll() on the stream read-side to detect when the
2907 * write-side (application) finally closes them.
2910 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2912 struct lttng_ht
*ht
;
2913 struct lttng_consumer_stream
*stream
;
2914 struct lttng_ht_iter iter
;
2916 ht
= consumer_data
.stream_per_chan_id_ht
;
2919 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2920 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2921 ht
->match_fct
, &channel
->key
,
2922 &iter
.iter
, stream
, node_channel_id
.node
) {
2924 * Protect against teardown with mutex.
2926 pthread_mutex_lock(&stream
->lock
);
2927 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2930 switch (consumer_data
.type
) {
2931 case LTTNG_CONSUMER_KERNEL
:
2933 case LTTNG_CONSUMER32_UST
:
2934 case LTTNG_CONSUMER64_UST
:
2935 if (stream
->metadata_flag
) {
2936 /* Safe and protected by the stream lock. */
2937 lttng_ustconsumer_close_metadata(stream
->chan
);
2940 * Note: a mutex is taken internally within
2941 * liblttng-ust-ctl to protect timer wakeup_fd
2942 * use from concurrent close.
2944 lttng_ustconsumer_close_stream_wakeup(stream
);
2948 ERR("Unknown consumer_data type");
2952 pthread_mutex_unlock(&stream
->lock
);
2957 static void destroy_channel_ht(struct lttng_ht
*ht
)
2959 struct lttng_ht_iter iter
;
2960 struct lttng_consumer_channel
*channel
;
2968 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2969 ret
= lttng_ht_del(ht
, &iter
);
2974 lttng_ht_destroy(ht
);
2978 * This thread polls the channel fds to detect when they are being
2979 * closed. It closes all related streams if the channel is detected as
2980 * closed. It is currently only used as a shim layer for UST because the
2981 * consumerd needs to keep the per-stream wakeup end of pipes open for
2984 void *consumer_thread_channel_poll(void *data
)
2986 int ret
, i
, pollfd
, err
= -1;
2987 uint32_t revents
, nb_fd
;
2988 struct lttng_consumer_channel
*chan
= NULL
;
2989 struct lttng_ht_iter iter
;
2990 struct lttng_ht_node_u64
*node
;
2991 struct lttng_poll_event events
;
2992 struct lttng_consumer_local_data
*ctx
= data
;
2993 struct lttng_ht
*channel_ht
;
2995 rcu_register_thread();
2997 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2999 if (testpoint(consumerd_thread_channel
)) {
3000 goto error_testpoint
;
3003 health_code_update();
3005 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3007 /* ENOMEM at this point. Better to bail out. */
3011 DBG("Thread channel poll started");
3013 /* Size is set to 1 for the consumer_channel pipe */
3014 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
3016 ERR("Poll set creation failed");
3020 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
3026 DBG("Channel main loop started");
3030 health_code_update();
3031 DBG("Channel poll wait");
3032 health_poll_entry();
3033 ret
= lttng_poll_wait(&events
, -1);
3034 DBG("Channel poll return from wait with %d fd(s)",
3035 LTTNG_POLL_GETNB(&events
));
3037 DBG("Channel event caught in thread");
3039 if (errno
== EINTR
) {
3040 ERR("Poll EINTR caught");
3043 if (LTTNG_POLL_GETNB(&events
) == 0) {
3044 err
= 0; /* All is OK */
3051 /* From here, the event is a channel wait fd */
3052 for (i
= 0; i
< nb_fd
; i
++) {
3053 health_code_update();
3055 revents
= LTTNG_POLL_GETEV(&events
, i
);
3056 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3058 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
3059 if (revents
& LPOLLIN
) {
3060 enum consumer_channel_action action
;
3063 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
3066 ERR("Error reading channel pipe");
3068 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3073 case CONSUMER_CHANNEL_ADD
:
3074 DBG("Adding channel %d to poll set",
3077 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
3080 lttng_ht_add_unique_u64(channel_ht
,
3081 &chan
->wait_fd_node
);
3083 /* Add channel to the global poll events list */
3084 lttng_poll_add(&events
, chan
->wait_fd
,
3085 LPOLLERR
| LPOLLHUP
);
3087 case CONSUMER_CHANNEL_DEL
:
3090 * This command should never be called if the channel
3091 * has streams monitored by either the data or metadata
3092 * thread. The consumer only notify this thread with a
3093 * channel del. command if it receives a destroy
3094 * channel command from the session daemon that send it
3095 * if a command prior to the GET_CHANNEL failed.
3099 chan
= consumer_find_channel(key
);
3102 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
3105 lttng_poll_del(&events
, chan
->wait_fd
);
3106 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
3107 ret
= lttng_ht_del(channel_ht
, &iter
);
3110 switch (consumer_data
.type
) {
3111 case LTTNG_CONSUMER_KERNEL
:
3113 case LTTNG_CONSUMER32_UST
:
3114 case LTTNG_CONSUMER64_UST
:
3115 health_code_update();
3116 /* Destroy streams that might have been left in the stream list. */
3117 clean_channel_stream_list(chan
);
3120 ERR("Unknown consumer_data type");
3125 * Release our own refcount. Force channel deletion even if
3126 * streams were not initialized.
3128 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3129 consumer_del_channel(chan
);
3134 case CONSUMER_CHANNEL_QUIT
:
3136 * Remove the pipe from the poll set and continue the loop
3137 * since their might be data to consume.
3139 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3142 ERR("Unknown action");
3145 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3146 DBG("Channel thread pipe hung up");
3148 * Remove the pipe from the poll set and continue the loop
3149 * since their might be data to consume.
3151 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3154 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3158 /* Handle other stream */
3164 uint64_t tmp_id
= (uint64_t) pollfd
;
3166 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3168 node
= lttng_ht_iter_get_node_u64(&iter
);
3171 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
3174 /* Check for error event */
3175 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3176 DBG("Channel fd %d is hup|err.", pollfd
);
3178 lttng_poll_del(&events
, chan
->wait_fd
);
3179 ret
= lttng_ht_del(channel_ht
, &iter
);
3183 * This will close the wait fd for each stream associated to
3184 * this channel AND monitored by the data/metadata thread thus
3185 * will be clean by the right thread.
3187 consumer_close_channel_streams(chan
);
3189 /* Release our own refcount */
3190 if (!uatomic_sub_return(&chan
->refcount
, 1)
3191 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3192 consumer_del_channel(chan
);
3195 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3200 /* Release RCU lock for the channel looked up */
3208 lttng_poll_clean(&events
);
3210 destroy_channel_ht(channel_ht
);
3213 DBG("Channel poll thread exiting");
3216 ERR("Health error occurred in %s", __func__
);
3218 health_unregister(health_consumerd
);
3219 rcu_unregister_thread();
3223 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3224 struct pollfd
*sockpoll
, int client_socket
)
3231 ret
= lttng_consumer_poll_socket(sockpoll
);
3235 DBG("Metadata connection on client_socket");
3237 /* Blocking call, waiting for transmission */
3238 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3239 if (ctx
->consumer_metadata_socket
< 0) {
3240 WARN("On accept metadata");
3251 * This thread listens on the consumerd socket and receives the file
3252 * descriptors from the session daemon.
3254 void *consumer_thread_sessiond_poll(void *data
)
3256 int sock
= -1, client_socket
, ret
, err
= -1;
3258 * structure to poll for incoming data on communication socket avoids
3259 * making blocking sockets.
3261 struct pollfd consumer_sockpoll
[2];
3262 struct lttng_consumer_local_data
*ctx
= data
;
3264 rcu_register_thread();
3266 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3268 if (testpoint(consumerd_thread_sessiond
)) {
3269 goto error_testpoint
;
3272 health_code_update();
3274 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3275 unlink(ctx
->consumer_command_sock_path
);
3276 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3277 if (client_socket
< 0) {
3278 ERR("Cannot create command socket");
3282 ret
= lttcomm_listen_unix_sock(client_socket
);
3287 DBG("Sending ready command to lttng-sessiond");
3288 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3289 /* return < 0 on error, but == 0 is not fatal */
3291 ERR("Error sending ready command to lttng-sessiond");
3295 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3296 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3297 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3298 consumer_sockpoll
[1].fd
= client_socket
;
3299 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3301 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3309 DBG("Connection on client_socket");
3311 /* Blocking call, waiting for transmission */
3312 sock
= lttcomm_accept_unix_sock(client_socket
);
3319 * Setup metadata socket which is the second socket connection on the
3320 * command unix socket.
3322 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3331 /* This socket is not useful anymore. */
3332 ret
= close(client_socket
);
3334 PERROR("close client_socket");
3338 /* update the polling structure to poll on the established socket */
3339 consumer_sockpoll
[1].fd
= sock
;
3340 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3343 health_code_update();
3345 health_poll_entry();
3346 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3355 DBG("Incoming command on sock");
3356 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3359 * This could simply be a session daemon quitting. Don't output
3362 DBG("Communication interrupted on command socket");
3366 if (CMM_LOAD_SHARED(consumer_quit
)) {
3367 DBG("consumer_thread_receive_fds received quit from signal");
3368 err
= 0; /* All is OK */
3371 DBG("received command on sock");
3377 DBG("Consumer thread sessiond poll exiting");
3380 * Close metadata streams since the producer is the session daemon which
3383 * NOTE: for now, this only applies to the UST tracer.
3385 lttng_consumer_close_all_metadata();
3388 * when all fds have hung up, the polling thread
3391 CMM_STORE_SHARED(consumer_quit
, 1);
3394 * Notify the data poll thread to poll back again and test the
3395 * consumer_quit state that we just set so to quit gracefully.
3397 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3399 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3401 notify_health_quit_pipe(health_quit_pipe
);
3403 /* Cleaning up possibly open sockets. */
3407 PERROR("close sock sessiond poll");
3410 if (client_socket
>= 0) {
3411 ret
= close(client_socket
);
3413 PERROR("close client_socket sessiond poll");
3420 ERR("Health error occurred in %s", __func__
);
3422 health_unregister(health_consumerd
);
3424 rcu_unregister_thread();
3428 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3429 struct lttng_consumer_local_data
*ctx
)
3433 pthread_mutex_lock(&stream
->chan
->lock
);
3434 pthread_mutex_lock(&stream
->lock
);
3435 if (stream
->metadata_flag
) {
3436 pthread_mutex_lock(&stream
->metadata_rdv_lock
);
3439 switch (consumer_data
.type
) {
3440 case LTTNG_CONSUMER_KERNEL
:
3441 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3443 case LTTNG_CONSUMER32_UST
:
3444 case LTTNG_CONSUMER64_UST
:
3445 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3448 ERR("Unknown consumer_data type");
3454 if (stream
->metadata_flag
) {
3455 pthread_cond_broadcast(&stream
->metadata_rdv
);
3456 pthread_mutex_unlock(&stream
->metadata_rdv_lock
);
3458 pthread_mutex_unlock(&stream
->lock
);
3459 pthread_mutex_unlock(&stream
->chan
->lock
);
3464 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3466 switch (consumer_data
.type
) {
3467 case LTTNG_CONSUMER_KERNEL
:
3468 return lttng_kconsumer_on_recv_stream(stream
);
3469 case LTTNG_CONSUMER32_UST
:
3470 case LTTNG_CONSUMER64_UST
:
3471 return lttng_ustconsumer_on_recv_stream(stream
);
3473 ERR("Unknown consumer_data type");
3480 * Allocate and set consumer data hash tables.
3482 int lttng_consumer_init(void)
3484 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3485 if (!consumer_data
.channel_ht
) {
3489 consumer_data
.channels_by_session_id_ht
=
3490 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3491 if (!consumer_data
.channels_by_session_id_ht
) {
3495 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3496 if (!consumer_data
.relayd_ht
) {
3500 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3501 if (!consumer_data
.stream_list_ht
) {
3505 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3506 if (!consumer_data
.stream_per_chan_id_ht
) {
3510 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3515 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3520 consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3521 if (!consumer_data
.chunk_registry
) {
3532 * Process the ADD_RELAYD command receive by a consumer.
3534 * This will create a relayd socket pair and add it to the relayd hash table.
3535 * The caller MUST acquire a RCU read side lock before calling it.
3537 void consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3538 struct lttng_consumer_local_data
*ctx
, int sock
,
3539 struct pollfd
*consumer_sockpoll
,
3540 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3541 uint64_t relayd_session_id
)
3543 int fd
= -1, ret
= -1, relayd_created
= 0;
3544 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3545 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3548 assert(relayd_sock
);
3550 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3552 /* Get relayd reference if exists. */
3553 relayd
= consumer_find_relayd(net_seq_idx
);
3554 if (relayd
== NULL
) {
3555 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3556 /* Not found. Allocate one. */
3557 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3558 if (relayd
== NULL
) {
3559 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3562 relayd
->sessiond_session_id
= sessiond_id
;
3567 * This code path MUST continue to the consumer send status message to
3568 * we can notify the session daemon and continue our work without
3569 * killing everything.
3573 * relayd key should never be found for control socket.
3575 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3578 /* First send a status message before receiving the fds. */
3579 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3581 /* Somehow, the session daemon is not responding anymore. */
3582 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3583 goto error_nosignal
;
3586 /* Poll on consumer socket. */
3587 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3589 /* Needing to exit in the middle of a command: error. */
3590 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3591 goto error_nosignal
;
3594 /* Get relayd socket from session daemon */
3595 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3596 if (ret
!= sizeof(fd
)) {
3597 fd
= -1; /* Just in case it gets set with an invalid value. */
3600 * Failing to receive FDs might indicate a major problem such as
3601 * reaching a fd limit during the receive where the kernel returns a
3602 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3603 * don't take any chances and stop everything.
3605 * XXX: Feature request #558 will fix that and avoid this possible
3606 * issue when reaching the fd limit.
3608 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3609 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3613 /* Copy socket information and received FD */
3614 switch (sock_type
) {
3615 case LTTNG_STREAM_CONTROL
:
3616 /* Copy received lttcomm socket */
3617 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3618 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3619 /* Handle create_sock error. */
3621 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3625 * Close the socket created internally by
3626 * lttcomm_create_sock, so we can replace it by the one
3627 * received from sessiond.
3629 if (close(relayd
->control_sock
.sock
.fd
)) {
3633 /* Assign new file descriptor */
3634 relayd
->control_sock
.sock
.fd
= fd
;
3635 /* Assign version values. */
3636 relayd
->control_sock
.major
= relayd_sock
->major
;
3637 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3639 relayd
->relayd_session_id
= relayd_session_id
;
3642 case LTTNG_STREAM_DATA
:
3643 /* Copy received lttcomm socket */
3644 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3645 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3646 /* Handle create_sock error. */
3648 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3652 * Close the socket created internally by
3653 * lttcomm_create_sock, so we can replace it by the one
3654 * received from sessiond.
3656 if (close(relayd
->data_sock
.sock
.fd
)) {
3660 /* Assign new file descriptor */
3661 relayd
->data_sock
.sock
.fd
= fd
;
3662 /* Assign version values. */
3663 relayd
->data_sock
.major
= relayd_sock
->major
;
3664 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3667 ERR("Unknown relayd socket type (%d)", sock_type
);
3668 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3672 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3673 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3674 relayd
->net_seq_idx
, fd
);
3676 * We gave the ownership of the fd to the relayd structure. Set the
3677 * fd to -1 so we don't call close() on it in the error path below.
3681 /* We successfully added the socket. Send status back. */
3682 ret
= consumer_send_status_msg(sock
, ret_code
);
3684 /* Somehow, the session daemon is not responding anymore. */
3685 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3686 goto error_nosignal
;
3690 * Add relayd socket pair to consumer data hashtable. If object already
3691 * exists or on error, the function gracefully returns.
3700 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3701 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3705 /* Close received socket if valid. */
3708 PERROR("close received socket");
3712 if (relayd_created
) {
3718 * Search for a relayd associated to the session id and return the reference.
3720 * A rcu read side lock MUST be acquire before calling this function and locked
3721 * until the relayd object is no longer necessary.
3723 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3725 struct lttng_ht_iter iter
;
3726 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3728 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3729 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3732 * Check by sessiond id which is unique here where the relayd session
3733 * id might not be when having multiple relayd.
3735 if (relayd
->sessiond_session_id
== id
) {
3736 /* Found the relayd. There can be only one per id. */
3748 * Check if for a given session id there is still data needed to be extract
3751 * Return 1 if data is pending or else 0 meaning ready to be read.
3753 int consumer_data_pending(uint64_t id
)
3756 struct lttng_ht_iter iter
;
3757 struct lttng_ht
*ht
;
3758 struct lttng_consumer_stream
*stream
;
3759 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3760 int (*data_pending
)(struct lttng_consumer_stream
*);
3762 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3765 pthread_mutex_lock(&consumer_data
.lock
);
3767 switch (consumer_data
.type
) {
3768 case LTTNG_CONSUMER_KERNEL
:
3769 data_pending
= lttng_kconsumer_data_pending
;
3771 case LTTNG_CONSUMER32_UST
:
3772 case LTTNG_CONSUMER64_UST
:
3773 data_pending
= lttng_ustconsumer_data_pending
;
3776 ERR("Unknown consumer data type");
3780 /* Ease our life a bit */
3781 ht
= consumer_data
.stream_list_ht
;
3783 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3784 ht
->hash_fct(&id
, lttng_ht_seed
),
3786 &iter
.iter
, stream
, node_session_id
.node
) {
3787 pthread_mutex_lock(&stream
->lock
);
3790 * A removed node from the hash table indicates that the stream has
3791 * been deleted thus having a guarantee that the buffers are closed
3792 * on the consumer side. However, data can still be transmitted
3793 * over the network so don't skip the relayd check.
3795 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3797 /* Check the stream if there is data in the buffers. */
3798 ret
= data_pending(stream
);
3800 pthread_mutex_unlock(&stream
->lock
);
3805 pthread_mutex_unlock(&stream
->lock
);
3808 relayd
= find_relayd_by_session_id(id
);
3810 unsigned int is_data_inflight
= 0;
3812 /* Send init command for data pending. */
3813 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3814 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3815 relayd
->relayd_session_id
);
3817 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3818 /* Communication error thus the relayd so no data pending. */
3819 goto data_not_pending
;
3822 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3823 ht
->hash_fct(&id
, lttng_ht_seed
),
3825 &iter
.iter
, stream
, node_session_id
.node
) {
3826 if (stream
->metadata_flag
) {
3827 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3828 stream
->relayd_stream_id
);
3830 ret
= relayd_data_pending(&relayd
->control_sock
,
3831 stream
->relayd_stream_id
,
3832 stream
->next_net_seq_num
- 1);
3836 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3838 } else if (ret
< 0) {
3839 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3840 lttng_consumer_cleanup_relayd(relayd
);
3841 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3842 goto data_not_pending
;
3846 /* Send end command for data pending. */
3847 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3848 relayd
->relayd_session_id
, &is_data_inflight
);
3849 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3851 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3852 lttng_consumer_cleanup_relayd(relayd
);
3853 goto data_not_pending
;
3855 if (is_data_inflight
) {
3861 * Finding _no_ node in the hash table and no inflight data means that the
3862 * stream(s) have been removed thus data is guaranteed to be available for
3863 * analysis from the trace files.
3867 /* Data is available to be read by a viewer. */
3868 pthread_mutex_unlock(&consumer_data
.lock
);
3873 /* Data is still being extracted from buffers. */
3874 pthread_mutex_unlock(&consumer_data
.lock
);
3880 * Send a ret code status message to the sessiond daemon.
3882 * Return the sendmsg() return value.
3884 int consumer_send_status_msg(int sock
, int ret_code
)
3886 struct lttcomm_consumer_status_msg msg
;
3888 memset(&msg
, 0, sizeof(msg
));
3889 msg
.ret_code
= ret_code
;
3891 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3895 * Send a channel status message to the sessiond daemon.
3897 * Return the sendmsg() return value.
3899 int consumer_send_status_channel(int sock
,
3900 struct lttng_consumer_channel
*channel
)
3902 struct lttcomm_consumer_status_channel msg
;
3906 memset(&msg
, 0, sizeof(msg
));
3908 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3910 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3911 msg
.key
= channel
->key
;
3912 msg
.stream_count
= channel
->streams
.count
;
3915 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3918 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3919 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3920 uint64_t max_sb_size
)
3922 unsigned long start_pos
;
3924 if (!nb_packets_per_stream
) {
3925 return consumed_pos
; /* Grab everything */
3927 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
3928 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3929 if ((long) (start_pos
- consumed_pos
) < 0) {
3930 return consumed_pos
; /* Grab everything */
3936 int consumer_flush_buffer(struct lttng_consumer_stream
*stream
, int producer_active
)
3940 switch (consumer_data
.type
) {
3941 case LTTNG_CONSUMER_KERNEL
:
3942 ret
= kernctl_buffer_flush(stream
->wait_fd
);
3944 ERR("Failed to flush kernel stream");
3948 case LTTNG_CONSUMER32_UST
:
3949 case LTTNG_CONSUMER64_UST
:
3950 lttng_ustctl_flush_buffer(stream
, producer_active
);
3953 ERR("Unknown consumer_data type");
3962 * Sample the rotate position for all the streams of a channel. If a stream
3963 * is already at the rotate position (produced == consumed), we flag it as
3964 * ready for rotation. The rotation of ready streams occurs after we have
3965 * replied to the session daemon that we have finished sampling the positions.
3966 * Must be called with RCU read-side lock held to ensure existence of channel.
3968 * Returns 0 on success, < 0 on error
3970 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
3971 uint64_t key
, uint64_t relayd_id
, uint32_t metadata
,
3972 struct lttng_consumer_local_data
*ctx
)
3975 struct lttng_consumer_stream
*stream
;
3976 struct lttng_ht_iter iter
;
3977 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
3978 struct lttng_dynamic_array stream_rotation_positions
;
3979 uint64_t next_chunk_id
, stream_count
= 0;
3980 enum lttng_trace_chunk_status chunk_status
;
3981 const bool is_local_trace
= relayd_id
== -1ULL;
3982 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3983 bool rotating_to_new_chunk
= true;
3985 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
3987 lttng_dynamic_array_init(&stream_rotation_positions
,
3988 sizeof(struct relayd_stream_rotation_position
), NULL
);
3992 pthread_mutex_lock(&channel
->lock
);
3993 assert(channel
->trace_chunk
);
3994 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
,
3996 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
3998 goto end_unlock_channel
;
4001 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4002 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4003 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4004 stream
, node_channel_id
.node
) {
4005 unsigned long consumed_pos
;
4007 health_code_update();
4010 * Lock stream because we are about to change its state.
4012 pthread_mutex_lock(&stream
->lock
);
4014 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
4015 rotating_to_new_chunk
= false;
4018 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4020 ERR("Failed to sample snapshot position during channel rotation");
4021 goto end_unlock_stream
;
4024 ret
= lttng_consumer_get_produced_snapshot(stream
,
4025 &stream
->rotate_position
);
4027 ERR("Failed to sample produced position during channel rotation");
4028 goto end_unlock_stream
;
4031 lttng_consumer_get_consumed_snapshot(stream
,
4033 if (consumed_pos
== stream
->rotate_position
) {
4034 stream
->rotate_ready
= true;
4038 * Active flush; has no effect if the production position
4039 * is at a packet boundary.
4041 ret
= consumer_flush_buffer(stream
, 1);
4043 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4045 goto end_unlock_stream
;
4048 if (!is_local_trace
) {
4050 * The relay daemon control protocol expects a rotation
4051 * position as "the sequence number of the first packet
4052 * _after_ the current trace chunk.
4054 * At the moment when the positions of the buffers are
4055 * sampled, the production position does not necessarily
4056 * sit at a packet boundary. The 'active' flush
4057 * operation above will push the production position to
4058 * the next packet boundary _if_ it is not already
4059 * sitting at such a boundary.
4061 * Assuming a current production position that is not
4062 * on the bound of a packet, the 'target' sequence
4064 * (consumed_pos / subbuffer_size) + 1
4065 * Note the '+ 1' to ensure the current packet is
4066 * part of the current trace chunk.
4068 * However, if the production position is already at
4069 * a packet boundary, the '+ 1' is not necessary as the
4070 * last packet of the current chunk is already
4073 const struct relayd_stream_rotation_position position
= {
4074 .stream_id
= stream
->relayd_stream_id
,
4075 .rotate_at_seq_num
= (stream
->rotate_position
/ stream
->max_sb_size
) +
4076 !!(stream
->rotate_position
% stream
->max_sb_size
),
4079 ret
= lttng_dynamic_array_add_element(
4080 &stream_rotation_positions
,
4083 ERR("Failed to allocate stream rotation position");
4084 goto end_unlock_stream
;
4088 pthread_mutex_unlock(&stream
->lock
);
4091 pthread_mutex_unlock(&channel
->lock
);
4093 if (is_local_trace
) {
4098 relayd
= consumer_find_relayd(relayd_id
);
4100 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4105 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4106 ret
= relayd_rotate_streams(&relayd
->control_sock
, stream_count
,
4107 rotating_to_new_chunk
? &next_chunk_id
: NULL
,
4108 (const struct relayd_stream_rotation_position
*)
4109 stream_rotation_positions
.buffer
.data
);
4110 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4112 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4113 relayd
->net_seq_idx
);
4114 lttng_consumer_cleanup_relayd(relayd
);
4122 pthread_mutex_unlock(&stream
->lock
);
4124 pthread_mutex_unlock(&channel
->lock
);
4127 lttng_dynamic_array_reset(&stream_rotation_positions
);
4132 * Check if a stream is ready to be rotated after extracting it.
4134 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4135 * error. Stream lock must be held.
4137 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4140 unsigned long consumed_pos
;
4142 if (!stream
->rotate_position
&& !stream
->rotate_ready
) {
4147 if (stream
->rotate_ready
) {
4153 * If we don't have the rotate_ready flag, check the consumed position
4154 * to determine if we need to rotate.
4156 ret
= lttng_consumer_sample_snapshot_positions(stream
);
4158 ERR("Taking snapshot positions");
4162 ret
= lttng_consumer_get_consumed_snapshot(stream
, &consumed_pos
);
4164 ERR("Consumed snapshot position");
4168 /* Rotate position not reached yet (with check for overflow). */
4169 if ((long) (consumed_pos
- stream
->rotate_position
) < 0) {
4180 * Reset the state for a stream after a rotation occurred.
4182 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4184 stream
->rotate_position
= 0;
4185 stream
->rotate_ready
= false;
4189 * Perform the rotation a local stream file.
4192 int rotate_local_stream(struct lttng_consumer_local_data
*ctx
,
4193 struct lttng_consumer_stream
*stream
)
4197 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4200 stream
->tracefile_size_current
= 0;
4201 stream
->tracefile_count_current
= 0;
4203 if (stream
->out_fd
>= 0) {
4204 ret
= close(stream
->out_fd
);
4206 PERROR("Failed to close stream out_fd of channel \"%s\"",
4207 stream
->chan
->name
);
4209 stream
->out_fd
= -1;
4212 if (stream
->index_file
) {
4213 lttng_index_file_put(stream
->index_file
);
4214 stream
->index_file
= NULL
;
4217 if (!stream
->trace_chunk
) {
4221 ret
= consumer_stream_create_output_files(stream
, true);
4227 * Performs the stream rotation for the rotate session feature if needed.
4228 * It must be called with the channel and stream locks held.
4230 * Return 0 on success, a negative number of error.
4232 int lttng_consumer_rotate_stream(struct lttng_consumer_local_data
*ctx
,
4233 struct lttng_consumer_stream
*stream
)
4237 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4240 * Update the stream's 'current' chunk to the session's (channel)
4241 * now-current chunk.
4243 lttng_trace_chunk_put(stream
->trace_chunk
);
4244 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4246 * A channel can be rotated and not have a "next" chunk
4247 * to transition to. In that case, the channel's "current chunk"
4248 * has not been closed yet, but it has not been updated to
4249 * a "next" trace chunk either. Hence, the stream, like its
4250 * parent channel, becomes part of no chunk and can't output
4251 * anything until a new trace chunk is created.
4253 stream
->trace_chunk
= NULL
;
4254 } else if (stream
->chan
->trace_chunk
&&
4255 !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4256 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4261 * Update the stream's trace chunk to its parent channel's
4262 * current trace chunk.
4264 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4267 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4268 ret
= rotate_local_stream(ctx
, stream
);
4270 ERR("Failed to rotate stream, ret = %i", ret
);
4275 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4277 * If the stream has transitioned to a new trace
4278 * chunk, the metadata should be re-dumped to the
4281 * However, it is possible for a stream to transition to
4282 * a "no-chunk" state. This can happen if a rotation
4283 * occurs on an inactive session. In such cases, the metadata
4284 * regeneration will happen when the next trace chunk is
4287 ret
= consumer_metadata_stream_dump(stream
);
4292 lttng_consumer_reset_stream_rotate_state(stream
);
4301 * Rotate all the ready streams now.
4303 * This is especially important for low throughput streams that have already
4304 * been consumed, we cannot wait for their next packet to perform the
4306 * Need to be called with RCU read-side lock held to ensure existence of
4309 * Returns 0 on success, < 0 on error
4311 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
,
4312 uint64_t key
, struct lttng_consumer_local_data
*ctx
)
4315 struct lttng_consumer_stream
*stream
;
4316 struct lttng_ht_iter iter
;
4317 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
4321 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4323 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4324 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4325 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4326 stream
, node_channel_id
.node
) {
4327 health_code_update();
4329 pthread_mutex_lock(&stream
->chan
->lock
);
4330 pthread_mutex_lock(&stream
->lock
);
4332 if (!stream
->rotate_ready
) {
4333 pthread_mutex_unlock(&stream
->lock
);
4334 pthread_mutex_unlock(&stream
->chan
->lock
);
4337 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4339 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
4340 pthread_mutex_unlock(&stream
->lock
);
4341 pthread_mutex_unlock(&stream
->chan
->lock
);
4354 enum lttcomm_return_code
lttng_consumer_init_command(
4355 struct lttng_consumer_local_data
*ctx
,
4356 const lttng_uuid sessiond_uuid
)
4358 enum lttcomm_return_code ret
;
4359 char uuid_str
[LTTNG_UUID_STR_LEN
];
4361 if (ctx
->sessiond_uuid
.is_set
) {
4362 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4366 ctx
->sessiond_uuid
.is_set
= true;
4367 memcpy(ctx
->sessiond_uuid
.value
, sessiond_uuid
, sizeof(lttng_uuid
));
4368 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4369 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4370 DBG("Received session daemon UUID: %s", uuid_str
);
4375 enum lttcomm_return_code
lttng_consumer_create_trace_chunk(
4376 const uint64_t *relayd_id
, uint64_t session_id
,
4378 time_t chunk_creation_timestamp
,
4379 const char *chunk_override_name
,
4380 const struct lttng_credentials
*credentials
,
4381 struct lttng_directory_handle
*chunk_directory_handle
)
4384 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4385 struct lttng_trace_chunk
*created_chunk
= NULL
, *published_chunk
= NULL
;
4386 enum lttng_trace_chunk_status chunk_status
;
4387 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4388 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4389 const char *relayd_id_str
= "(none)";
4390 const char *creation_timestamp_str
;
4391 struct lttng_ht_iter iter
;
4392 struct lttng_consumer_channel
*channel
;
4395 /* Only used for logging purposes. */
4396 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4397 "%" PRIu64
, *relayd_id
);
4398 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4399 relayd_id_str
= relayd_id_buffer
;
4401 relayd_id_str
= "(formatting error)";
4405 /* Local protocol error. */
4406 assert(chunk_creation_timestamp
);
4407 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4408 creation_timestamp_buffer
,
4409 sizeof(creation_timestamp_buffer
));
4410 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
4411 "(formatting error)";
4413 DBG("Consumer create trace chunk command: relay_id = %s"
4414 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4415 ", chunk_override_name = %s"
4416 ", chunk_creation_timestamp = %s",
4417 relayd_id_str
, session_id
, chunk_id
,
4418 chunk_override_name
? : "(none)",
4419 creation_timestamp_str
);
4422 * The trace chunk registry, as used by the consumer daemon, implicitly
4423 * owns the trace chunks. This is only needed in the consumer since
4424 * the consumer has no notion of a session beyond session IDs being
4425 * used to identify other objects.
4427 * The lttng_trace_chunk_registry_publish() call below provides a
4428 * reference which is not released; it implicitly becomes the session
4429 * daemon's reference to the chunk in the consumer daemon.
4431 * The lifetime of trace chunks in the consumer daemon is managed by
4432 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4433 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4435 created_chunk
= lttng_trace_chunk_create(chunk_id
,
4436 chunk_creation_timestamp
);
4437 if (!created_chunk
) {
4438 ERR("Failed to create trace chunk");
4439 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4443 if (chunk_override_name
) {
4444 chunk_status
= lttng_trace_chunk_override_name(created_chunk
,
4445 chunk_override_name
);
4446 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4447 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4452 if (chunk_directory_handle
) {
4453 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
,
4455 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4456 ERR("Failed to set trace chunk credentials");
4457 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4461 * The consumer daemon has no ownership of the chunk output
4464 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
,
4465 chunk_directory_handle
);
4466 chunk_directory_handle
= NULL
;
4467 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4468 ERR("Failed to set trace chunk's directory handle");
4469 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4474 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4475 consumer_data
.chunk_registry
, session_id
,
4477 lttng_trace_chunk_put(created_chunk
);
4478 created_chunk
= NULL
;
4479 if (!published_chunk
) {
4480 ERR("Failed to publish trace chunk");
4481 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4486 cds_lfht_for_each_entry_duplicate(consumer_data
.channels_by_session_id_ht
->ht
,
4487 consumer_data
.channels_by_session_id_ht
->hash_fct(
4488 &session_id
, lttng_ht_seed
),
4489 consumer_data
.channels_by_session_id_ht
->match_fct
,
4490 &session_id
, &iter
.iter
, channel
,
4491 channels_by_session_id_ht_node
.node
) {
4492 ret
= lttng_consumer_channel_set_trace_chunk(channel
,
4496 * Roll-back the creation of this chunk.
4498 * This is important since the session daemon will
4499 * assume that the creation of this chunk failed and
4500 * will never ask for it to be closed, resulting
4501 * in a leak and an inconsistent state for some
4504 enum lttcomm_return_code close_ret
;
4505 char path
[LTTNG_PATH_MAX
];
4507 DBG("Failed to set new trace chunk on existing channels, rolling back");
4508 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4509 session_id
, chunk_id
,
4510 chunk_creation_timestamp
, NULL
,
4512 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4513 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4514 session_id
, chunk_id
);
4517 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4523 struct consumer_relayd_sock_pair
*relayd
;
4525 relayd
= consumer_find_relayd(*relayd_id
);
4527 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4528 ret
= relayd_create_trace_chunk(
4529 &relayd
->control_sock
, published_chunk
);
4530 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4532 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4535 if (!relayd
|| ret
) {
4536 enum lttcomm_return_code close_ret
;
4537 char path
[LTTNG_PATH_MAX
];
4539 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4542 chunk_creation_timestamp
,
4544 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4545 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4550 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4557 /* Release the reference returned by the "publish" operation. */
4558 lttng_trace_chunk_put(published_chunk
);
4559 lttng_trace_chunk_put(created_chunk
);
4563 enum lttcomm_return_code
lttng_consumer_close_trace_chunk(
4564 const uint64_t *relayd_id
, uint64_t session_id
,
4565 uint64_t chunk_id
, time_t chunk_close_timestamp
,
4566 const enum lttng_trace_chunk_command_type
*close_command
,
4569 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4570 struct lttng_trace_chunk
*chunk
;
4571 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4572 const char *relayd_id_str
= "(none)";
4573 const char *close_command_name
= "none";
4574 struct lttng_ht_iter iter
;
4575 struct lttng_consumer_channel
*channel
;
4576 enum lttng_trace_chunk_status chunk_status
;
4581 /* Only used for logging purposes. */
4582 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4583 "%" PRIu64
, *relayd_id
);
4584 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4585 relayd_id_str
= relayd_id_buffer
;
4587 relayd_id_str
= "(formatting error)";
4590 if (close_command
) {
4591 close_command_name
= lttng_trace_chunk_command_type_get_name(
4595 DBG("Consumer close trace chunk command: relayd_id = %s"
4596 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4597 ", close command = %s",
4598 relayd_id_str
, session_id
, chunk_id
,
4599 close_command_name
);
4601 chunk
= lttng_trace_chunk_registry_find_chunk(
4602 consumer_data
.chunk_registry
, session_id
, chunk_id
);
4604 ERR("Failed to find chunk: session_id = %" PRIu64
4605 ", chunk_id = %" PRIu64
,
4606 session_id
, chunk_id
);
4607 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4611 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
,
4612 chunk_close_timestamp
);
4613 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4614 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4618 if (close_command
) {
4619 chunk_status
= lttng_trace_chunk_set_close_command(
4620 chunk
, *close_command
);
4621 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4622 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4628 * chunk is now invalid to access as we no longer hold a reference to
4629 * it; it is only kept around to compare it (by address) to the
4630 * current chunk found in the session's channels.
4633 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
,
4634 channel
, node
.node
) {
4638 * Only change the channel's chunk to NULL if it still
4639 * references the chunk being closed. The channel may
4640 * reference a newer channel in the case of a session
4641 * rotation. When a session rotation occurs, the "next"
4642 * chunk is created before the "current" chunk is closed.
4644 if (channel
->trace_chunk
!= chunk
) {
4647 ret
= lttng_consumer_channel_set_trace_chunk(channel
, NULL
);
4650 * Attempt to close the chunk on as many channels as
4653 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4659 struct consumer_relayd_sock_pair
*relayd
;
4661 relayd
= consumer_find_relayd(*relayd_id
);
4663 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4664 ret
= relayd_close_trace_chunk(
4665 &relayd
->control_sock
, chunk
,
4667 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4669 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
,
4673 if (!relayd
|| ret
) {
4674 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4682 * Release the reference returned by the "find" operation and
4683 * the session daemon's implicit reference to the chunk.
4685 lttng_trace_chunk_put(chunk
);
4686 lttng_trace_chunk_put(chunk
);
4691 enum lttcomm_return_code
lttng_consumer_trace_chunk_exists(
4692 const uint64_t *relayd_id
, uint64_t session_id
,
4696 enum lttcomm_return_code ret_code
;
4697 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4698 const char *relayd_id_str
= "(none)";
4699 const bool is_local_trace
= !relayd_id
;
4700 struct consumer_relayd_sock_pair
*relayd
= NULL
;
4701 bool chunk_exists_local
, chunk_exists_remote
;
4706 /* Only used for logging purposes. */
4707 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4708 "%" PRIu64
, *relayd_id
);
4709 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4710 relayd_id_str
= relayd_id_buffer
;
4712 relayd_id_str
= "(formatting error)";
4716 DBG("Consumer trace chunk exists command: relayd_id = %s"
4717 ", chunk_id = %" PRIu64
, relayd_id_str
,
4719 ret
= lttng_trace_chunk_registry_chunk_exists(
4720 consumer_data
.chunk_registry
, session_id
,
4721 chunk_id
, &chunk_exists_local
);
4723 /* Internal error. */
4724 ERR("Failed to query the existence of a trace chunk");
4725 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
4728 DBG("Trace chunk %s locally",
4729 chunk_exists_local
? "exists" : "does not exist");
4730 if (chunk_exists_local
) {
4731 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
4733 } else if (is_local_trace
) {
4734 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4739 relayd
= consumer_find_relayd(*relayd_id
);
4741 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
4742 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
4743 goto end_rcu_unlock
;
4745 DBG("Looking up existence of trace chunk on relay daemon");
4746 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4747 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
,
4748 &chunk_exists_remote
);
4749 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4751 ERR("Failed to look-up the existence of trace chunk on relay daemon");
4752 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
4753 goto end_rcu_unlock
;
4756 ret_code
= chunk_exists_remote
?
4757 LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
4758 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4759 DBG("Trace chunk %s on relay daemon",
4760 chunk_exists_remote
? "exists" : "does not exist");