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 stream
->rotate_position
= -1ULL;
607 pthread_mutex_init(&stream
->lock
, NULL
);
608 pthread_mutex_init(&stream
->metadata_timer_lock
, NULL
);
610 /* If channel is the metadata, flag this stream as metadata. */
611 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
612 stream
->metadata_flag
= 1;
613 /* Metadata is flat out. */
614 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
615 /* Live rendez-vous point. */
616 pthread_cond_init(&stream
->metadata_rdv
, NULL
);
617 pthread_mutex_init(&stream
->metadata_rdv_lock
, NULL
);
619 /* Format stream name to <channel_name>_<cpu_number> */
620 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
623 PERROR("snprintf stream name");
628 /* Key is always the wait_fd for streams. */
629 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
631 /* Init node per channel id key */
632 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
634 /* Init session id node with the stream session id */
635 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
637 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
638 " relayd_id %" PRIu64
", session_id %" PRIu64
,
639 stream
->name
, stream
->key
, channel_key
,
640 stream
->net_seq_idx
, stream
->session_id
);
647 lttng_trace_chunk_put(stream
->trace_chunk
);
657 * Add a stream to the global list protected by a mutex.
659 void consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
661 struct lttng_ht
*ht
= data_ht
;
666 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
668 pthread_mutex_lock(&consumer_data
.lock
);
669 pthread_mutex_lock(&stream
->chan
->lock
);
670 pthread_mutex_lock(&stream
->chan
->timer_lock
);
671 pthread_mutex_lock(&stream
->lock
);
674 /* Steal stream identifier to avoid having streams with the same key */
675 steal_stream_key(stream
->key
, ht
);
677 lttng_ht_add_unique_u64(ht
, &stream
->node
);
679 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
680 &stream
->node_channel_id
);
683 * Add stream to the stream_list_ht of the consumer data. No need to steal
684 * the key since the HT does not use it and we allow to add redundant keys
687 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
690 * When nb_init_stream_left reaches 0, we don't need to trigger any action
691 * in terms of destroying the associated channel, because the action that
692 * causes the count to become 0 also causes a stream to be added. The
693 * channel deletion will thus be triggered by the following removal of this
696 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
697 /* Increment refcount before decrementing nb_init_stream_left */
699 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
702 /* Update consumer data once the node is inserted. */
703 consumer_data
.stream_count
++;
704 consumer_data
.need_update
= 1;
707 pthread_mutex_unlock(&stream
->lock
);
708 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
709 pthread_mutex_unlock(&stream
->chan
->lock
);
710 pthread_mutex_unlock(&consumer_data
.lock
);
713 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
715 consumer_del_stream(stream
, data_ht
);
719 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
720 * be acquired before calling this.
722 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
725 struct lttng_ht_node_u64
*node
;
726 struct lttng_ht_iter iter
;
730 lttng_ht_lookup(consumer_data
.relayd_ht
,
731 &relayd
->net_seq_idx
, &iter
);
732 node
= lttng_ht_iter_get_node_u64(&iter
);
736 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
743 * Allocate and return a consumer relayd socket.
745 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
746 uint64_t net_seq_idx
)
748 struct consumer_relayd_sock_pair
*obj
= NULL
;
750 /* net sequence index of -1 is a failure */
751 if (net_seq_idx
== (uint64_t) -1ULL) {
755 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
757 PERROR("zmalloc relayd sock");
761 obj
->net_seq_idx
= net_seq_idx
;
763 obj
->destroy_flag
= 0;
764 obj
->control_sock
.sock
.fd
= -1;
765 obj
->data_sock
.sock
.fd
= -1;
766 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
767 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
774 * Find a relayd socket pair in the global consumer data.
776 * Return the object if found else NULL.
777 * RCU read-side lock must be held across this call and while using the
780 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
782 struct lttng_ht_iter iter
;
783 struct lttng_ht_node_u64
*node
;
784 struct consumer_relayd_sock_pair
*relayd
= NULL
;
786 /* Negative keys are lookup failures */
787 if (key
== (uint64_t) -1ULL) {
791 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
793 node
= lttng_ht_iter_get_node_u64(&iter
);
795 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
803 * Find a relayd and send the stream
805 * Returns 0 on success, < 0 on error
807 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
811 struct consumer_relayd_sock_pair
*relayd
;
814 assert(stream
->net_seq_idx
!= -1ULL);
817 /* The stream is not metadata. Get relayd reference if exists. */
819 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
820 if (relayd
!= NULL
) {
821 /* Add stream on the relayd */
822 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
823 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
824 path
, &stream
->relayd_stream_id
,
825 stream
->chan
->tracefile_size
,
826 stream
->chan
->tracefile_count
,
827 stream
->trace_chunk
);
828 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
830 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
831 lttng_consumer_cleanup_relayd(relayd
);
835 uatomic_inc(&relayd
->refcount
);
836 stream
->sent_to_relayd
= 1;
838 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
839 stream
->key
, stream
->net_seq_idx
);
844 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
845 stream
->name
, stream
->key
, stream
->net_seq_idx
);
853 * Find a relayd and send the streams sent message
855 * Returns 0 on success, < 0 on error
857 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
860 struct consumer_relayd_sock_pair
*relayd
;
862 assert(net_seq_idx
!= -1ULL);
864 /* The stream is not metadata. Get relayd reference if exists. */
866 relayd
= consumer_find_relayd(net_seq_idx
);
867 if (relayd
!= NULL
) {
868 /* Add stream on the relayd */
869 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
870 ret
= relayd_streams_sent(&relayd
->control_sock
);
871 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
873 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
874 lttng_consumer_cleanup_relayd(relayd
);
878 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
885 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
893 * Find a relayd and close the stream
895 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
897 struct consumer_relayd_sock_pair
*relayd
;
899 /* The stream is not metadata. Get relayd reference if exists. */
901 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
903 consumer_stream_relayd_close(stream
, relayd
);
909 * Handle stream for relayd transmission if the stream applies for network
910 * streaming where the net sequence index is set.
912 * Return destination file descriptor or negative value on error.
914 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
915 size_t data_size
, unsigned long padding
,
916 struct consumer_relayd_sock_pair
*relayd
)
919 struct lttcomm_relayd_data_hdr data_hdr
;
925 /* Reset data header */
926 memset(&data_hdr
, 0, sizeof(data_hdr
));
928 if (stream
->metadata_flag
) {
929 /* Caller MUST acquire the relayd control socket lock */
930 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
935 /* Metadata are always sent on the control socket. */
936 outfd
= relayd
->control_sock
.sock
.fd
;
938 /* Set header with stream information */
939 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
940 data_hdr
.data_size
= htobe32(data_size
);
941 data_hdr
.padding_size
= htobe32(padding
);
944 * Note that net_seq_num below is assigned with the *current* value of
945 * next_net_seq_num and only after that the next_net_seq_num will be
946 * increment. This is why when issuing a command on the relayd using
947 * this next value, 1 should always be substracted in order to compare
948 * the last seen sequence number on the relayd side to the last sent.
950 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
951 /* Other fields are zeroed previously */
953 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
959 ++stream
->next_net_seq_num
;
961 /* Set to go on data socket */
962 outfd
= relayd
->data_sock
.sock
.fd
;
970 * Trigger a dump of the metadata content. Following/during the succesful
971 * completion of this call, the metadata poll thread will start receiving
972 * metadata packets to consume.
974 * The caller must hold the channel and stream locks.
977 int consumer_metadata_stream_dump(struct lttng_consumer_stream
*stream
)
981 ASSERT_LOCKED(stream
->chan
->lock
);
982 ASSERT_LOCKED(stream
->lock
);
983 assert(stream
->metadata_flag
);
984 assert(stream
->chan
->trace_chunk
);
986 switch (consumer_data
.type
) {
987 case LTTNG_CONSUMER_KERNEL
:
989 * Reset the position of what has been read from the
990 * metadata cache to 0 so we can dump it again.
992 ret
= kernctl_metadata_cache_dump(stream
->wait_fd
);
994 case LTTNG_CONSUMER32_UST
:
995 case LTTNG_CONSUMER64_UST
:
997 * Reset the position pushed from the metadata cache so it
998 * will write from the beginning on the next push.
1000 stream
->ust_metadata_pushed
= 0;
1001 ret
= consumer_metadata_wakeup_pipe(stream
->chan
);
1004 ERR("Unknown consumer_data type");
1008 ERR("Failed to dump the metadata cache");
1014 int lttng_consumer_channel_set_trace_chunk(
1015 struct lttng_consumer_channel
*channel
,
1016 struct lttng_trace_chunk
*new_trace_chunk
)
1018 pthread_mutex_lock(&channel
->lock
);
1019 if (channel
->is_deleted
) {
1021 * The channel has been logically deleted and should no longer
1022 * be used. It has released its reference to its current trace
1023 * chunk and should not acquire a new one.
1025 * Return success as there is nothing for the caller to do.
1031 * The acquisition of the reference cannot fail (barring
1032 * a severe internal error) since a reference to the published
1033 * chunk is already held by the caller.
1035 if (new_trace_chunk
) {
1036 const bool acquired_reference
= lttng_trace_chunk_get(
1039 assert(acquired_reference
);
1042 lttng_trace_chunk_put(channel
->trace_chunk
);
1043 channel
->trace_chunk
= new_trace_chunk
;
1045 pthread_mutex_unlock(&channel
->lock
);
1050 * Allocate and return a new lttng_consumer_channel object using the given key
1051 * to initialize the hash table node.
1053 * On error, return NULL.
1055 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
1056 uint64_t session_id
,
1057 const uint64_t *chunk_id
,
1058 const char *pathname
,
1061 enum lttng_event_output output
,
1062 uint64_t tracefile_size
,
1063 uint64_t tracefile_count
,
1064 uint64_t session_id_per_pid
,
1065 unsigned int monitor
,
1066 unsigned int live_timer_interval
,
1067 const char *root_shm_path
,
1068 const char *shm_path
)
1070 struct lttng_consumer_channel
*channel
= NULL
;
1071 struct lttng_trace_chunk
*trace_chunk
= NULL
;
1074 trace_chunk
= lttng_trace_chunk_registry_find_chunk(
1075 consumer_data
.chunk_registry
, session_id
,
1078 ERR("Failed to find trace chunk reference during creation of channel");
1083 channel
= zmalloc(sizeof(*channel
));
1084 if (channel
== NULL
) {
1085 PERROR("malloc struct lttng_consumer_channel");
1090 channel
->refcount
= 0;
1091 channel
->session_id
= session_id
;
1092 channel
->session_id_per_pid
= session_id_per_pid
;
1093 channel
->relayd_id
= relayd_id
;
1094 channel
->tracefile_size
= tracefile_size
;
1095 channel
->tracefile_count
= tracefile_count
;
1096 channel
->monitor
= monitor
;
1097 channel
->live_timer_interval
= live_timer_interval
;
1098 pthread_mutex_init(&channel
->lock
, NULL
);
1099 pthread_mutex_init(&channel
->timer_lock
, NULL
);
1102 case LTTNG_EVENT_SPLICE
:
1103 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
1105 case LTTNG_EVENT_MMAP
:
1106 channel
->output
= CONSUMER_CHANNEL_MMAP
;
1116 * In monitor mode, the streams associated with the channel will be put in
1117 * a special list ONLY owned by this channel. So, the refcount is set to 1
1118 * here meaning that the channel itself has streams that are referenced.
1120 * On a channel deletion, once the channel is no longer visible, the
1121 * refcount is decremented and checked for a zero value to delete it. With
1122 * streams in no monitor mode, it will now be safe to destroy the channel.
1124 if (!channel
->monitor
) {
1125 channel
->refcount
= 1;
1128 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1129 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1131 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1132 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1134 if (root_shm_path
) {
1135 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1136 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1139 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1140 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1143 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1144 lttng_ht_node_init_u64(&channel
->channels_by_session_id_ht_node
,
1145 channel
->session_id
);
1147 channel
->wait_fd
= -1;
1148 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1151 int ret
= lttng_consumer_channel_set_trace_chunk(channel
,
1158 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1161 lttng_trace_chunk_put(trace_chunk
);
1164 consumer_del_channel(channel
);
1170 * Add a channel to the global list protected by a mutex.
1172 * Always return 0 indicating success.
1174 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1175 struct lttng_consumer_local_data
*ctx
)
1177 pthread_mutex_lock(&consumer_data
.lock
);
1178 pthread_mutex_lock(&channel
->lock
);
1179 pthread_mutex_lock(&channel
->timer_lock
);
1182 * This gives us a guarantee that the channel we are about to add to the
1183 * channel hash table will be unique. See this function comment on the why
1184 * we need to steel the channel key at this stage.
1186 steal_channel_key(channel
->key
);
1189 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1190 lttng_ht_add_u64(consumer_data
.channels_by_session_id_ht
,
1191 &channel
->channels_by_session_id_ht_node
);
1193 channel
->is_published
= true;
1195 pthread_mutex_unlock(&channel
->timer_lock
);
1196 pthread_mutex_unlock(&channel
->lock
);
1197 pthread_mutex_unlock(&consumer_data
.lock
);
1199 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1200 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1207 * Allocate the pollfd structure and the local view of the out fds to avoid
1208 * doing a lookup in the linked list and concurrency issues when writing is
1209 * needed. Called with consumer_data.lock held.
1211 * Returns the number of fds in the structures.
1213 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1214 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1215 struct lttng_ht
*ht
, int *nb_inactive_fd
)
1218 struct lttng_ht_iter iter
;
1219 struct lttng_consumer_stream
*stream
;
1224 assert(local_stream
);
1226 DBG("Updating poll fd array");
1227 *nb_inactive_fd
= 0;
1229 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1231 * Only active streams with an active end point can be added to the
1232 * poll set and local stream storage of the thread.
1234 * There is a potential race here for endpoint_status to be updated
1235 * just after the check. However, this is OK since the stream(s) will
1236 * be deleted once the thread is notified that the end point state has
1237 * changed where this function will be called back again.
1239 * We track the number of inactive FDs because they still need to be
1240 * closed by the polling thread after a wakeup on the data_pipe or
1243 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1244 (*nb_inactive_fd
)++;
1248 * This clobbers way too much the debug output. Uncomment that if you
1249 * need it for debugging purposes.
1251 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1252 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1253 local_stream
[i
] = stream
;
1259 * Insert the consumer_data_pipe at the end of the array and don't
1260 * increment i so nb_fd is the number of real FD.
1262 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1263 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1265 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1266 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1271 * Poll on the should_quit pipe and the command socket return -1 on
1272 * error, 1 if should exit, 0 if data is available on the command socket
1274 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1279 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1280 if (num_rdy
== -1) {
1282 * Restart interrupted system call.
1284 if (errno
== EINTR
) {
1287 PERROR("Poll error");
1290 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1291 DBG("consumer_should_quit wake up");
1298 * Set the error socket.
1300 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1303 ctx
->consumer_error_socket
= sock
;
1307 * Set the command socket path.
1309 void lttng_consumer_set_command_sock_path(
1310 struct lttng_consumer_local_data
*ctx
, char *sock
)
1312 ctx
->consumer_command_sock_path
= sock
;
1316 * Send return code to the session daemon.
1317 * If the socket is not defined, we return 0, it is not a fatal error
1319 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1321 if (ctx
->consumer_error_socket
> 0) {
1322 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1323 sizeof(enum lttcomm_sessiond_command
));
1330 * Close all the tracefiles and stream fds and MUST be called when all
1331 * instances are destroyed i.e. when all threads were joined and are ended.
1333 void lttng_consumer_cleanup(void)
1335 struct lttng_ht_iter iter
;
1336 struct lttng_consumer_channel
*channel
;
1337 unsigned int trace_chunks_left
;
1341 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1343 consumer_del_channel(channel
);
1348 lttng_ht_destroy(consumer_data
.channel_ht
);
1349 lttng_ht_destroy(consumer_data
.channels_by_session_id_ht
);
1351 cleanup_relayd_ht();
1353 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1356 * This HT contains streams that are freed by either the metadata thread or
1357 * the data thread so we do *nothing* on the hash table and simply destroy
1360 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1363 * Trace chunks in the registry may still exist if the session
1364 * daemon has encountered an internal error and could not
1365 * tear down its sessions and/or trace chunks properly.
1367 * Release the session daemon's implicit reference to any remaining
1368 * trace chunk and print an error if any trace chunk was found. Note
1369 * that there are _no_ legitimate cases for trace chunks to be left,
1370 * it is a leak. However, it can happen following a crash of the
1371 * session daemon and not emptying the registry would cause an assertion
1374 trace_chunks_left
= lttng_trace_chunk_registry_put_each_chunk(
1375 consumer_data
.chunk_registry
);
1376 if (trace_chunks_left
) {
1377 ERR("%u trace chunks are leaked by lttng-consumerd. "
1378 "This can be caused by an internal error of the session daemon.",
1381 /* Run all callbacks freeing each chunk. */
1383 lttng_trace_chunk_registry_destroy(consumer_data
.chunk_registry
);
1387 * Called from signal handler.
1389 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1393 CMM_STORE_SHARED(consumer_quit
, 1);
1394 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1396 PERROR("write consumer quit");
1399 DBG("Consumer flag that it should quit");
1404 * Flush pending writes to trace output disk file.
1407 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1411 int outfd
= stream
->out_fd
;
1414 * This does a blocking write-and-wait on any page that belongs to the
1415 * subbuffer prior to the one we just wrote.
1416 * Don't care about error values, as these are just hints and ways to
1417 * limit the amount of page cache used.
1419 if (orig_offset
< stream
->max_sb_size
) {
1422 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1423 stream
->max_sb_size
,
1424 SYNC_FILE_RANGE_WAIT_BEFORE
1425 | SYNC_FILE_RANGE_WRITE
1426 | SYNC_FILE_RANGE_WAIT_AFTER
);
1428 * Give hints to the kernel about how we access the file:
1429 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1432 * We need to call fadvise again after the file grows because the
1433 * kernel does not seem to apply fadvise to non-existing parts of the
1436 * Call fadvise _after_ having waited for the page writeback to
1437 * complete because the dirty page writeback semantic is not well
1438 * defined. So it can be expected to lead to lower throughput in
1441 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1442 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1443 if (ret
&& ret
!= -ENOSYS
) {
1445 PERROR("posix_fadvise on fd %i", outfd
);
1450 * Initialise the necessary environnement :
1451 * - create a new context
1452 * - create the poll_pipe
1453 * - create the should_quit pipe (for signal handler)
1454 * - create the thread pipe (for splice)
1456 * Takes a function pointer as argument, this function is called when data is
1457 * available on a buffer. This function is responsible to do the
1458 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1459 * buffer configuration and then kernctl_put_next_subbuf at the end.
1461 * Returns a pointer to the new context or NULL on error.
1463 struct lttng_consumer_local_data
*lttng_consumer_create(
1464 enum lttng_consumer_type type
,
1465 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1466 struct lttng_consumer_local_data
*ctx
),
1467 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1468 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1469 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1472 struct lttng_consumer_local_data
*ctx
;
1474 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1475 consumer_data
.type
== type
);
1476 consumer_data
.type
= type
;
1478 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1480 PERROR("allocating context");
1484 ctx
->consumer_error_socket
= -1;
1485 ctx
->consumer_metadata_socket
= -1;
1486 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1487 /* assign the callbacks */
1488 ctx
->on_buffer_ready
= buffer_ready
;
1489 ctx
->on_recv_channel
= recv_channel
;
1490 ctx
->on_recv_stream
= recv_stream
;
1491 ctx
->on_update_stream
= update_stream
;
1493 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1494 if (!ctx
->consumer_data_pipe
) {
1495 goto error_poll_pipe
;
1498 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1499 if (!ctx
->consumer_wakeup_pipe
) {
1500 goto error_wakeup_pipe
;
1503 ret
= pipe(ctx
->consumer_should_quit
);
1505 PERROR("Error creating recv pipe");
1506 goto error_quit_pipe
;
1509 ret
= pipe(ctx
->consumer_channel_pipe
);
1511 PERROR("Error creating channel pipe");
1512 goto error_channel_pipe
;
1515 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1516 if (!ctx
->consumer_metadata_pipe
) {
1517 goto error_metadata_pipe
;
1520 ctx
->channel_monitor_pipe
= -1;
1524 error_metadata_pipe
:
1525 utils_close_pipe(ctx
->consumer_channel_pipe
);
1527 utils_close_pipe(ctx
->consumer_should_quit
);
1529 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1531 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1539 * Iterate over all streams of the hashtable and free them properly.
1541 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1543 struct lttng_ht_iter iter
;
1544 struct lttng_consumer_stream
*stream
;
1551 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1553 * Ignore return value since we are currently cleaning up so any error
1556 (void) consumer_del_stream(stream
, ht
);
1560 lttng_ht_destroy(ht
);
1564 * Iterate over all streams of the metadata hashtable and free them
1567 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1569 struct lttng_ht_iter iter
;
1570 struct lttng_consumer_stream
*stream
;
1577 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1579 * Ignore return value since we are currently cleaning up so any error
1582 (void) consumer_del_metadata_stream(stream
, ht
);
1586 lttng_ht_destroy(ht
);
1590 * Close all fds associated with the instance and free the context.
1592 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1596 DBG("Consumer destroying it. Closing everything.");
1602 destroy_data_stream_ht(data_ht
);
1603 destroy_metadata_stream_ht(metadata_ht
);
1605 ret
= close(ctx
->consumer_error_socket
);
1609 ret
= close(ctx
->consumer_metadata_socket
);
1613 utils_close_pipe(ctx
->consumer_channel_pipe
);
1614 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1615 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1616 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1617 utils_close_pipe(ctx
->consumer_should_quit
);
1619 unlink(ctx
->consumer_command_sock_path
);
1624 * Write the metadata stream id on the specified file descriptor.
1626 static int write_relayd_metadata_id(int fd
,
1627 struct lttng_consumer_stream
*stream
,
1628 unsigned long padding
)
1631 struct lttcomm_relayd_metadata_payload hdr
;
1633 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1634 hdr
.padding_size
= htobe32(padding
);
1635 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1636 if (ret
< sizeof(hdr
)) {
1638 * This error means that the fd's end is closed so ignore the PERROR
1639 * not to clubber the error output since this can happen in a normal
1642 if (errno
!= EPIPE
) {
1643 PERROR("write metadata stream id");
1645 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1647 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1648 * handle writting the missing part so report that as an error and
1649 * don't lie to the caller.
1654 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1655 stream
->relayd_stream_id
, padding
);
1662 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1663 * core function for writing trace buffers to either the local filesystem or
1666 * It must be called with the stream and the channel lock held.
1668 * Careful review MUST be put if any changes occur!
1670 * Returns the number of bytes written
1672 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1673 struct lttng_consumer_local_data
*ctx
,
1674 struct lttng_consumer_stream
*stream
, unsigned long len
,
1675 unsigned long padding
,
1676 struct ctf_packet_index
*index
)
1678 unsigned long mmap_offset
;
1681 off_t orig_offset
= stream
->out_fd_offset
;
1682 /* Default is on the disk */
1683 int outfd
= stream
->out_fd
;
1684 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1685 unsigned int relayd_hang_up
= 0;
1687 /* RCU lock for the relayd pointer */
1689 assert(stream
->net_seq_idx
!= (uint64_t) -1ULL ||
1690 stream
->trace_chunk
);
1692 /* Flag that the current stream if set for network streaming. */
1693 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1694 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1695 if (relayd
== NULL
) {
1701 /* get the offset inside the fd to mmap */
1702 switch (consumer_data
.type
) {
1703 case LTTNG_CONSUMER_KERNEL
:
1704 mmap_base
= stream
->mmap_base
;
1705 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1707 PERROR("tracer ctl get_mmap_read_offset");
1711 case LTTNG_CONSUMER32_UST
:
1712 case LTTNG_CONSUMER64_UST
:
1713 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1715 ERR("read mmap get mmap base for stream %s", stream
->name
);
1719 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1721 PERROR("tracer ctl get_mmap_read_offset");
1727 ERR("Unknown consumer_data type");
1731 /* Handle stream on the relayd if the output is on the network */
1733 unsigned long netlen
= len
;
1736 * Lock the control socket for the complete duration of the function
1737 * since from this point on we will use the socket.
1739 if (stream
->metadata_flag
) {
1740 /* Metadata requires the control socket. */
1741 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1742 if (stream
->reset_metadata_flag
) {
1743 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1744 stream
->relayd_stream_id
,
1745 stream
->metadata_version
);
1750 stream
->reset_metadata_flag
= 0;
1752 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1755 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1760 /* Use the returned socket. */
1763 /* Write metadata stream id before payload */
1764 if (stream
->metadata_flag
) {
1765 ret
= write_relayd_metadata_id(outfd
, stream
, padding
);
1772 /* No streaming, we have to set the len with the full padding */
1775 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1776 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1778 ERR("Reset metadata file");
1781 stream
->reset_metadata_flag
= 0;
1785 * Check if we need to change the tracefile before writing the packet.
1787 if (stream
->chan
->tracefile_size
> 0 &&
1788 (stream
->tracefile_size_current
+ len
) >
1789 stream
->chan
->tracefile_size
) {
1790 ret
= consumer_stream_rotate_output_files(stream
);
1794 outfd
= stream
->out_fd
;
1797 stream
->tracefile_size_current
+= len
;
1799 index
->offset
= htobe64(stream
->out_fd_offset
);
1804 * This call guarantee that len or less is returned. It's impossible to
1805 * receive a ret value that is bigger than len.
1807 ret
= lttng_write(outfd
, mmap_base
+ mmap_offset
, len
);
1808 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1809 if (ret
< 0 || ((size_t) ret
!= len
)) {
1811 * Report error to caller if nothing was written else at least send the
1819 /* Socket operation failed. We consider the relayd dead */
1820 if (errno
== EPIPE
) {
1822 * This is possible if the fd is closed on the other side
1823 * (outfd) or any write problem. It can be verbose a bit for a
1824 * normal execution if for instance the relayd is stopped
1825 * abruptly. This can happen so set this to a DBG statement.
1827 DBG("Consumer mmap write detected relayd hang up");
1829 /* Unhandled error, print it and stop function right now. */
1830 PERROR("Error in write mmap (ret %zd != len %lu)", ret
, len
);
1834 stream
->output_written
+= ret
;
1836 /* This call is useless on a socket so better save a syscall. */
1838 /* This won't block, but will start writeout asynchronously */
1839 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, len
,
1840 SYNC_FILE_RANGE_WRITE
);
1841 stream
->out_fd_offset
+= len
;
1842 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1847 * This is a special case that the relayd has closed its socket. Let's
1848 * cleanup the relayd object and all associated streams.
1850 if (relayd
&& relayd_hang_up
) {
1851 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1852 lttng_consumer_cleanup_relayd(relayd
);
1856 /* Unlock only if ctrl socket used */
1857 if (relayd
&& stream
->metadata_flag
) {
1858 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1866 * Splice the data from the ring buffer to the tracefile.
1868 * It must be called with the stream lock held.
1870 * Returns the number of bytes spliced.
1872 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1873 struct lttng_consumer_local_data
*ctx
,
1874 struct lttng_consumer_stream
*stream
, unsigned long len
,
1875 unsigned long padding
,
1876 struct ctf_packet_index
*index
)
1878 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1880 off_t orig_offset
= stream
->out_fd_offset
;
1881 int fd
= stream
->wait_fd
;
1882 /* Default is on the disk */
1883 int outfd
= stream
->out_fd
;
1884 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1886 unsigned int relayd_hang_up
= 0;
1888 switch (consumer_data
.type
) {
1889 case LTTNG_CONSUMER_KERNEL
:
1891 case LTTNG_CONSUMER32_UST
:
1892 case LTTNG_CONSUMER64_UST
:
1893 /* Not supported for user space tracing */
1896 ERR("Unknown consumer_data type");
1900 /* RCU lock for the relayd pointer */
1903 /* Flag that the current stream if set for network streaming. */
1904 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1905 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1906 if (relayd
== NULL
) {
1911 splice_pipe
= stream
->splice_pipe
;
1913 /* Write metadata stream id before payload */
1915 unsigned long total_len
= len
;
1917 if (stream
->metadata_flag
) {
1919 * Lock the control socket for the complete duration of the function
1920 * since from this point on we will use the socket.
1922 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1924 if (stream
->reset_metadata_flag
) {
1925 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1926 stream
->relayd_stream_id
,
1927 stream
->metadata_version
);
1932 stream
->reset_metadata_flag
= 0;
1934 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
,
1942 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1945 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1951 /* Use the returned socket. */
1954 /* No streaming, we have to set the len with the full padding */
1957 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1958 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1960 ERR("Reset metadata file");
1963 stream
->reset_metadata_flag
= 0;
1966 * Check if we need to change the tracefile before writing the packet.
1968 if (stream
->chan
->tracefile_size
> 0 &&
1969 (stream
->tracefile_size_current
+ len
) >
1970 stream
->chan
->tracefile_size
) {
1971 ret
= consumer_stream_rotate_output_files(stream
);
1976 outfd
= stream
->out_fd
;
1979 stream
->tracefile_size_current
+= len
;
1980 index
->offset
= htobe64(stream
->out_fd_offset
);
1984 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1985 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1986 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1987 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1988 DBG("splice chan to pipe, ret %zd", ret_splice
);
1989 if (ret_splice
< 0) {
1992 PERROR("Error in relay splice");
1996 /* Handle stream on the relayd if the output is on the network */
1997 if (relayd
&& stream
->metadata_flag
) {
1998 size_t metadata_payload_size
=
1999 sizeof(struct lttcomm_relayd_metadata_payload
);
2001 /* Update counter to fit the spliced data */
2002 ret_splice
+= metadata_payload_size
;
2003 len
+= metadata_payload_size
;
2005 * We do this so the return value can match the len passed as
2006 * argument to this function.
2008 written
-= metadata_payload_size
;
2011 /* Splice data out */
2012 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
2013 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
2014 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
2016 if (ret_splice
< 0) {
2021 } else if (ret_splice
> len
) {
2023 * We don't expect this code path to be executed but you never know
2024 * so this is an extra protection agains a buggy splice().
2027 written
+= ret_splice
;
2028 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
2032 /* All good, update current len and continue. */
2036 /* This call is useless on a socket so better save a syscall. */
2038 /* This won't block, but will start writeout asynchronously */
2039 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
2040 SYNC_FILE_RANGE_WRITE
);
2041 stream
->out_fd_offset
+= ret_splice
;
2043 stream
->output_written
+= ret_splice
;
2044 written
+= ret_splice
;
2047 lttng_consumer_sync_trace_file(stream
, orig_offset
);
2053 * This is a special case that the relayd has closed its socket. Let's
2054 * cleanup the relayd object and all associated streams.
2056 if (relayd
&& relayd_hang_up
) {
2057 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
2058 lttng_consumer_cleanup_relayd(relayd
);
2059 /* Skip splice error so the consumer does not fail */
2064 /* send the appropriate error description to sessiond */
2067 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
2070 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
2073 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
2078 if (relayd
&& stream
->metadata_flag
) {
2079 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2087 * Sample the snapshot positions for a specific fd
2089 * Returns 0 on success, < 0 on error
2091 int lttng_consumer_sample_snapshot_positions(struct lttng_consumer_stream
*stream
)
2093 switch (consumer_data
.type
) {
2094 case LTTNG_CONSUMER_KERNEL
:
2095 return lttng_kconsumer_sample_snapshot_positions(stream
);
2096 case LTTNG_CONSUMER32_UST
:
2097 case LTTNG_CONSUMER64_UST
:
2098 return lttng_ustconsumer_sample_snapshot_positions(stream
);
2100 ERR("Unknown consumer_data type");
2106 * Take a snapshot for a specific fd
2108 * Returns 0 on success, < 0 on error
2110 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
2112 switch (consumer_data
.type
) {
2113 case LTTNG_CONSUMER_KERNEL
:
2114 return lttng_kconsumer_take_snapshot(stream
);
2115 case LTTNG_CONSUMER32_UST
:
2116 case LTTNG_CONSUMER64_UST
:
2117 return lttng_ustconsumer_take_snapshot(stream
);
2119 ERR("Unknown consumer_data type");
2126 * Get the produced position
2128 * Returns 0 on success, < 0 on error
2130 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
2133 switch (consumer_data
.type
) {
2134 case LTTNG_CONSUMER_KERNEL
:
2135 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
2136 case LTTNG_CONSUMER32_UST
:
2137 case LTTNG_CONSUMER64_UST
:
2138 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2140 ERR("Unknown consumer_data type");
2147 * Get the consumed position (free-running counter position in bytes).
2149 * Returns 0 on success, < 0 on error
2151 int lttng_consumer_get_consumed_snapshot(struct lttng_consumer_stream
*stream
,
2154 switch (consumer_data
.type
) {
2155 case LTTNG_CONSUMER_KERNEL
:
2156 return lttng_kconsumer_get_consumed_snapshot(stream
, pos
);
2157 case LTTNG_CONSUMER32_UST
:
2158 case LTTNG_CONSUMER64_UST
:
2159 return lttng_ustconsumer_get_consumed_snapshot(stream
, pos
);
2161 ERR("Unknown consumer_data type");
2167 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2168 int sock
, struct pollfd
*consumer_sockpoll
)
2170 switch (consumer_data
.type
) {
2171 case LTTNG_CONSUMER_KERNEL
:
2172 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2173 case LTTNG_CONSUMER32_UST
:
2174 case LTTNG_CONSUMER64_UST
:
2175 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2177 ERR("Unknown consumer_data type");
2183 void lttng_consumer_close_all_metadata(void)
2185 switch (consumer_data
.type
) {
2186 case LTTNG_CONSUMER_KERNEL
:
2188 * The Kernel consumer has a different metadata scheme so we don't
2189 * close anything because the stream will be closed by the session
2193 case LTTNG_CONSUMER32_UST
:
2194 case LTTNG_CONSUMER64_UST
:
2196 * Close all metadata streams. The metadata hash table is passed and
2197 * this call iterates over it by closing all wakeup fd. This is safe
2198 * because at this point we are sure that the metadata producer is
2199 * either dead or blocked.
2201 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2204 ERR("Unknown consumer_data type");
2210 * Clean up a metadata stream and free its memory.
2212 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2213 struct lttng_ht
*ht
)
2215 struct lttng_consumer_channel
*channel
= NULL
;
2216 bool free_channel
= false;
2220 * This call should NEVER receive regular stream. It must always be
2221 * metadata stream and this is crucial for data structure synchronization.
2223 assert(stream
->metadata_flag
);
2225 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2227 pthread_mutex_lock(&consumer_data
.lock
);
2229 * Note that this assumes that a stream's channel is never changed and
2230 * that the stream's lock doesn't need to be taken to sample its
2233 channel
= stream
->chan
;
2234 pthread_mutex_lock(&channel
->lock
);
2235 pthread_mutex_lock(&stream
->lock
);
2236 if (channel
->metadata_cache
) {
2237 /* Only applicable to userspace consumers. */
2238 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
2241 /* Remove any reference to that stream. */
2242 consumer_stream_delete(stream
, ht
);
2244 /* Close down everything including the relayd if one. */
2245 consumer_stream_close(stream
);
2246 /* Destroy tracer buffers of the stream. */
2247 consumer_stream_destroy_buffers(stream
);
2249 /* Atomically decrement channel refcount since other threads can use it. */
2250 if (!uatomic_sub_return(&channel
->refcount
, 1)
2251 && !uatomic_read(&channel
->nb_init_stream_left
)) {
2252 /* Go for channel deletion! */
2253 free_channel
= true;
2255 stream
->chan
= NULL
;
2258 * Nullify the stream reference so it is not used after deletion. The
2259 * channel lock MUST be acquired before being able to check for a NULL
2262 channel
->metadata_stream
= NULL
;
2264 if (channel
->metadata_cache
) {
2265 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
2267 pthread_mutex_unlock(&stream
->lock
);
2268 pthread_mutex_unlock(&channel
->lock
);
2269 pthread_mutex_unlock(&consumer_data
.lock
);
2272 consumer_del_channel(channel
);
2275 lttng_trace_chunk_put(stream
->trace_chunk
);
2276 stream
->trace_chunk
= NULL
;
2277 consumer_stream_free(stream
);
2281 * Action done with the metadata stream when adding it to the consumer internal
2282 * data structures to handle it.
2284 void consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2286 struct lttng_ht
*ht
= metadata_ht
;
2287 struct lttng_ht_iter iter
;
2288 struct lttng_ht_node_u64
*node
;
2293 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2295 pthread_mutex_lock(&consumer_data
.lock
);
2296 pthread_mutex_lock(&stream
->chan
->lock
);
2297 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2298 pthread_mutex_lock(&stream
->lock
);
2301 * From here, refcounts are updated so be _careful_ when returning an error
2308 * Lookup the stream just to make sure it does not exist in our internal
2309 * state. This should NEVER happen.
2311 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2312 node
= lttng_ht_iter_get_node_u64(&iter
);
2316 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2317 * in terms of destroying the associated channel, because the action that
2318 * causes the count to become 0 also causes a stream to be added. The
2319 * channel deletion will thus be triggered by the following removal of this
2322 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2323 /* Increment refcount before decrementing nb_init_stream_left */
2325 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2328 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2330 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
2331 &stream
->node_channel_id
);
2334 * Add stream to the stream_list_ht of the consumer data. No need to steal
2335 * the key since the HT does not use it and we allow to add redundant keys
2338 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2342 pthread_mutex_unlock(&stream
->lock
);
2343 pthread_mutex_unlock(&stream
->chan
->lock
);
2344 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2345 pthread_mutex_unlock(&consumer_data
.lock
);
2349 * Delete data stream that are flagged for deletion (endpoint_status).
2351 static void validate_endpoint_status_data_stream(void)
2353 struct lttng_ht_iter iter
;
2354 struct lttng_consumer_stream
*stream
;
2356 DBG("Consumer delete flagged data stream");
2359 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2360 /* Validate delete flag of the stream */
2361 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2364 /* Delete it right now */
2365 consumer_del_stream(stream
, data_ht
);
2371 * Delete metadata stream that are flagged for deletion (endpoint_status).
2373 static void validate_endpoint_status_metadata_stream(
2374 struct lttng_poll_event
*pollset
)
2376 struct lttng_ht_iter iter
;
2377 struct lttng_consumer_stream
*stream
;
2379 DBG("Consumer delete flagged metadata stream");
2384 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2385 /* Validate delete flag of the stream */
2386 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2390 * Remove from pollset so the metadata thread can continue without
2391 * blocking on a deleted stream.
2393 lttng_poll_del(pollset
, stream
->wait_fd
);
2395 /* Delete it right now */
2396 consumer_del_metadata_stream(stream
, metadata_ht
);
2402 * Thread polls on metadata file descriptor and write them on disk or on the
2405 void *consumer_thread_metadata_poll(void *data
)
2407 int ret
, i
, pollfd
, err
= -1;
2408 uint32_t revents
, nb_fd
;
2409 struct lttng_consumer_stream
*stream
= NULL
;
2410 struct lttng_ht_iter iter
;
2411 struct lttng_ht_node_u64
*node
;
2412 struct lttng_poll_event events
;
2413 struct lttng_consumer_local_data
*ctx
= data
;
2416 rcu_register_thread();
2418 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2420 if (testpoint(consumerd_thread_metadata
)) {
2421 goto error_testpoint
;
2424 health_code_update();
2426 DBG("Thread metadata poll started");
2428 /* Size is set to 1 for the consumer_metadata pipe */
2429 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2431 ERR("Poll set creation failed");
2435 ret
= lttng_poll_add(&events
,
2436 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2442 DBG("Metadata main loop started");
2446 health_code_update();
2447 health_poll_entry();
2448 DBG("Metadata poll wait");
2449 ret
= lttng_poll_wait(&events
, -1);
2450 DBG("Metadata poll return from wait with %d fd(s)",
2451 LTTNG_POLL_GETNB(&events
));
2453 DBG("Metadata event caught in thread");
2455 if (errno
== EINTR
) {
2456 ERR("Poll EINTR caught");
2459 if (LTTNG_POLL_GETNB(&events
) == 0) {
2460 err
= 0; /* All is OK */
2467 /* From here, the event is a metadata wait fd */
2468 for (i
= 0; i
< nb_fd
; i
++) {
2469 health_code_update();
2471 revents
= LTTNG_POLL_GETEV(&events
, i
);
2472 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2474 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2475 if (revents
& LPOLLIN
) {
2478 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2479 &stream
, sizeof(stream
));
2480 if (pipe_len
< sizeof(stream
)) {
2482 PERROR("read metadata stream");
2485 * Remove the pipe from the poll set and continue the loop
2486 * since their might be data to consume.
2488 lttng_poll_del(&events
,
2489 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2490 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2494 /* A NULL stream means that the state has changed. */
2495 if (stream
== NULL
) {
2496 /* Check for deleted streams. */
2497 validate_endpoint_status_metadata_stream(&events
);
2501 DBG("Adding metadata stream %d to poll set",
2504 /* Add metadata stream to the global poll events list */
2505 lttng_poll_add(&events
, stream
->wait_fd
,
2506 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2507 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2508 DBG("Metadata thread pipe hung up");
2510 * Remove the pipe from the poll set and continue the loop
2511 * since their might be data to consume.
2513 lttng_poll_del(&events
,
2514 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2515 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2518 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2522 /* Handle other stream */
2528 uint64_t tmp_id
= (uint64_t) pollfd
;
2530 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2532 node
= lttng_ht_iter_get_node_u64(&iter
);
2535 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2538 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2539 /* Get the data out of the metadata file descriptor */
2540 DBG("Metadata available on fd %d", pollfd
);
2541 assert(stream
->wait_fd
== pollfd
);
2544 health_code_update();
2546 len
= ctx
->on_buffer_ready(stream
, ctx
);
2548 * We don't check the return value here since if we get
2549 * a negative len, it means an error occurred thus we
2550 * simply remove it from the poll set and free the
2555 /* It's ok to have an unavailable sub-buffer */
2556 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2557 /* Clean up stream from consumer and free it. */
2558 lttng_poll_del(&events
, stream
->wait_fd
);
2559 consumer_del_metadata_stream(stream
, metadata_ht
);
2561 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2562 DBG("Metadata fd %d is hup|err.", pollfd
);
2563 if (!stream
->hangup_flush_done
2564 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2565 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2566 DBG("Attempting to flush and consume the UST buffers");
2567 lttng_ustconsumer_on_stream_hangup(stream
);
2569 /* We just flushed the stream now read it. */
2571 health_code_update();
2573 len
= ctx
->on_buffer_ready(stream
, ctx
);
2575 * We don't check the return value here since if we get
2576 * a negative len, it means an error occurred thus we
2577 * simply remove it from the poll set and free the
2583 lttng_poll_del(&events
, stream
->wait_fd
);
2585 * This call update the channel states, closes file descriptors
2586 * and securely free the stream.
2588 consumer_del_metadata_stream(stream
, metadata_ht
);
2590 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2594 /* Release RCU lock for the stream looked up */
2602 DBG("Metadata poll thread exiting");
2604 lttng_poll_clean(&events
);
2609 ERR("Health error occurred in %s", __func__
);
2611 health_unregister(health_consumerd
);
2612 rcu_unregister_thread();
2617 * This thread polls the fds in the set to consume the data and write
2618 * it to tracefile if necessary.
2620 void *consumer_thread_data_poll(void *data
)
2622 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2623 struct pollfd
*pollfd
= NULL
;
2624 /* local view of the streams */
2625 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2626 /* local view of consumer_data.fds_count */
2628 /* 2 for the consumer_data_pipe and wake up pipe */
2629 const int nb_pipes_fd
= 2;
2630 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2631 int nb_inactive_fd
= 0;
2632 struct lttng_consumer_local_data
*ctx
= data
;
2635 rcu_register_thread();
2637 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2639 if (testpoint(consumerd_thread_data
)) {
2640 goto error_testpoint
;
2643 health_code_update();
2645 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2646 if (local_stream
== NULL
) {
2647 PERROR("local_stream malloc");
2652 health_code_update();
2658 * the fds set has been updated, we need to update our
2659 * local array as well
2661 pthread_mutex_lock(&consumer_data
.lock
);
2662 if (consumer_data
.need_update
) {
2667 local_stream
= NULL
;
2669 /* Allocate for all fds */
2670 pollfd
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) * sizeof(struct pollfd
));
2671 if (pollfd
== NULL
) {
2672 PERROR("pollfd malloc");
2673 pthread_mutex_unlock(&consumer_data
.lock
);
2677 local_stream
= zmalloc((consumer_data
.stream_count
+ nb_pipes_fd
) *
2678 sizeof(struct lttng_consumer_stream
*));
2679 if (local_stream
== NULL
) {
2680 PERROR("local_stream malloc");
2681 pthread_mutex_unlock(&consumer_data
.lock
);
2684 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2685 data_ht
, &nb_inactive_fd
);
2687 ERR("Error in allocating pollfd or local_outfds");
2688 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2689 pthread_mutex_unlock(&consumer_data
.lock
);
2693 consumer_data
.need_update
= 0;
2695 pthread_mutex_unlock(&consumer_data
.lock
);
2697 /* No FDs and consumer_quit, consumer_cleanup the thread */
2698 if (nb_fd
== 0 && nb_inactive_fd
== 0 &&
2699 CMM_LOAD_SHARED(consumer_quit
) == 1) {
2700 err
= 0; /* All is OK */
2703 /* poll on the array of fds */
2705 DBG("polling on %d fd", nb_fd
+ nb_pipes_fd
);
2706 if (testpoint(consumerd_thread_data_poll
)) {
2709 health_poll_entry();
2710 num_rdy
= poll(pollfd
, nb_fd
+ nb_pipes_fd
, -1);
2712 DBG("poll num_rdy : %d", num_rdy
);
2713 if (num_rdy
== -1) {
2715 * Restart interrupted system call.
2717 if (errno
== EINTR
) {
2720 PERROR("Poll error");
2721 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2723 } else if (num_rdy
== 0) {
2724 DBG("Polling thread timed out");
2728 if (caa_unlikely(data_consumption_paused
)) {
2729 DBG("Data consumption paused, sleeping...");
2735 * If the consumer_data_pipe triggered poll go directly to the
2736 * beginning of the loop to update the array. We want to prioritize
2737 * array update over low-priority reads.
2739 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2740 ssize_t pipe_readlen
;
2742 DBG("consumer_data_pipe wake up");
2743 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2744 &new_stream
, sizeof(new_stream
));
2745 if (pipe_readlen
< sizeof(new_stream
)) {
2746 PERROR("Consumer data pipe");
2747 /* Continue so we can at least handle the current stream(s). */
2752 * If the stream is NULL, just ignore it. It's also possible that
2753 * the sessiond poll thread changed the consumer_quit state and is
2754 * waking us up to test it.
2756 if (new_stream
== NULL
) {
2757 validate_endpoint_status_data_stream();
2761 /* Continue to update the local streams and handle prio ones */
2765 /* Handle wakeup pipe. */
2766 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2768 ssize_t pipe_readlen
;
2770 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2772 if (pipe_readlen
< 0) {
2773 PERROR("Consumer data wakeup pipe");
2775 /* We've been awakened to handle stream(s). */
2776 ctx
->has_wakeup
= 0;
2779 /* Take care of high priority channels first. */
2780 for (i
= 0; i
< nb_fd
; i
++) {
2781 health_code_update();
2783 if (local_stream
[i
] == NULL
) {
2786 if (pollfd
[i
].revents
& POLLPRI
) {
2787 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2789 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2790 /* it's ok to have an unavailable sub-buffer */
2791 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2792 /* Clean the stream and free it. */
2793 consumer_del_stream(local_stream
[i
], data_ht
);
2794 local_stream
[i
] = NULL
;
2795 } else if (len
> 0) {
2796 local_stream
[i
]->data_read
= 1;
2802 * If we read high prio channel in this loop, try again
2803 * for more high prio data.
2809 /* Take care of low priority channels. */
2810 for (i
= 0; i
< nb_fd
; i
++) {
2811 health_code_update();
2813 if (local_stream
[i
] == NULL
) {
2816 if ((pollfd
[i
].revents
& POLLIN
) ||
2817 local_stream
[i
]->hangup_flush_done
||
2818 local_stream
[i
]->has_data
) {
2819 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2820 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2821 /* it's ok to have an unavailable sub-buffer */
2822 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2823 /* Clean the stream and free it. */
2824 consumer_del_stream(local_stream
[i
], data_ht
);
2825 local_stream
[i
] = NULL
;
2826 } else if (len
> 0) {
2827 local_stream
[i
]->data_read
= 1;
2832 /* Handle hangup and errors */
2833 for (i
= 0; i
< nb_fd
; i
++) {
2834 health_code_update();
2836 if (local_stream
[i
] == NULL
) {
2839 if (!local_stream
[i
]->hangup_flush_done
2840 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2841 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2842 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2843 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2845 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2846 /* Attempt read again, for the data we just flushed. */
2847 local_stream
[i
]->data_read
= 1;
2850 * If the poll flag is HUP/ERR/NVAL and we have
2851 * read no data in this pass, we can remove the
2852 * stream from its hash table.
2854 if ((pollfd
[i
].revents
& POLLHUP
)) {
2855 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2856 if (!local_stream
[i
]->data_read
) {
2857 consumer_del_stream(local_stream
[i
], data_ht
);
2858 local_stream
[i
] = NULL
;
2861 } else if (pollfd
[i
].revents
& POLLERR
) {
2862 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2863 if (!local_stream
[i
]->data_read
) {
2864 consumer_del_stream(local_stream
[i
], data_ht
);
2865 local_stream
[i
] = NULL
;
2868 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2869 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2870 if (!local_stream
[i
]->data_read
) {
2871 consumer_del_stream(local_stream
[i
], data_ht
);
2872 local_stream
[i
] = NULL
;
2876 if (local_stream
[i
] != NULL
) {
2877 local_stream
[i
]->data_read
= 0;
2884 DBG("polling thread exiting");
2889 * Close the write side of the pipe so epoll_wait() in
2890 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2891 * read side of the pipe. If we close them both, epoll_wait strangely does
2892 * not return and could create a endless wait period if the pipe is the
2893 * only tracked fd in the poll set. The thread will take care of closing
2896 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2901 ERR("Health error occurred in %s", __func__
);
2903 health_unregister(health_consumerd
);
2905 rcu_unregister_thread();
2910 * Close wake-up end of each stream belonging to the channel. This will
2911 * allow the poll() on the stream read-side to detect when the
2912 * write-side (application) finally closes them.
2915 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2917 struct lttng_ht
*ht
;
2918 struct lttng_consumer_stream
*stream
;
2919 struct lttng_ht_iter iter
;
2921 ht
= consumer_data
.stream_per_chan_id_ht
;
2924 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2925 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2926 ht
->match_fct
, &channel
->key
,
2927 &iter
.iter
, stream
, node_channel_id
.node
) {
2929 * Protect against teardown with mutex.
2931 pthread_mutex_lock(&stream
->lock
);
2932 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2935 switch (consumer_data
.type
) {
2936 case LTTNG_CONSUMER_KERNEL
:
2938 case LTTNG_CONSUMER32_UST
:
2939 case LTTNG_CONSUMER64_UST
:
2940 if (stream
->metadata_flag
) {
2941 /* Safe and protected by the stream lock. */
2942 lttng_ustconsumer_close_metadata(stream
->chan
);
2945 * Note: a mutex is taken internally within
2946 * liblttng-ust-ctl to protect timer wakeup_fd
2947 * use from concurrent close.
2949 lttng_ustconsumer_close_stream_wakeup(stream
);
2953 ERR("Unknown consumer_data type");
2957 pthread_mutex_unlock(&stream
->lock
);
2962 static void destroy_channel_ht(struct lttng_ht
*ht
)
2964 struct lttng_ht_iter iter
;
2965 struct lttng_consumer_channel
*channel
;
2973 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2974 ret
= lttng_ht_del(ht
, &iter
);
2979 lttng_ht_destroy(ht
);
2983 * This thread polls the channel fds to detect when they are being
2984 * closed. It closes all related streams if the channel is detected as
2985 * closed. It is currently only used as a shim layer for UST because the
2986 * consumerd needs to keep the per-stream wakeup end of pipes open for
2989 void *consumer_thread_channel_poll(void *data
)
2991 int ret
, i
, pollfd
, err
= -1;
2992 uint32_t revents
, nb_fd
;
2993 struct lttng_consumer_channel
*chan
= NULL
;
2994 struct lttng_ht_iter iter
;
2995 struct lttng_ht_node_u64
*node
;
2996 struct lttng_poll_event events
;
2997 struct lttng_consumer_local_data
*ctx
= data
;
2998 struct lttng_ht
*channel_ht
;
3000 rcu_register_thread();
3002 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
3004 if (testpoint(consumerd_thread_channel
)) {
3005 goto error_testpoint
;
3008 health_code_update();
3010 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3012 /* ENOMEM at this point. Better to bail out. */
3016 DBG("Thread channel poll started");
3018 /* Size is set to 1 for the consumer_channel pipe */
3019 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
3021 ERR("Poll set creation failed");
3025 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
3031 DBG("Channel main loop started");
3035 health_code_update();
3036 DBG("Channel poll wait");
3037 health_poll_entry();
3038 ret
= lttng_poll_wait(&events
, -1);
3039 DBG("Channel poll return from wait with %d fd(s)",
3040 LTTNG_POLL_GETNB(&events
));
3042 DBG("Channel event caught in thread");
3044 if (errno
== EINTR
) {
3045 ERR("Poll EINTR caught");
3048 if (LTTNG_POLL_GETNB(&events
) == 0) {
3049 err
= 0; /* All is OK */
3056 /* From here, the event is a channel wait fd */
3057 for (i
= 0; i
< nb_fd
; i
++) {
3058 health_code_update();
3060 revents
= LTTNG_POLL_GETEV(&events
, i
);
3061 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
3063 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
3064 if (revents
& LPOLLIN
) {
3065 enum consumer_channel_action action
;
3068 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
3071 ERR("Error reading channel pipe");
3073 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3078 case CONSUMER_CHANNEL_ADD
:
3079 DBG("Adding channel %d to poll set",
3082 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
3085 lttng_ht_add_unique_u64(channel_ht
,
3086 &chan
->wait_fd_node
);
3088 /* Add channel to the global poll events list */
3089 lttng_poll_add(&events
, chan
->wait_fd
,
3090 LPOLLERR
| LPOLLHUP
);
3092 case CONSUMER_CHANNEL_DEL
:
3095 * This command should never be called if the channel
3096 * has streams monitored by either the data or metadata
3097 * thread. The consumer only notify this thread with a
3098 * channel del. command if it receives a destroy
3099 * channel command from the session daemon that send it
3100 * if a command prior to the GET_CHANNEL failed.
3104 chan
= consumer_find_channel(key
);
3107 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
3110 lttng_poll_del(&events
, chan
->wait_fd
);
3111 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
3112 ret
= lttng_ht_del(channel_ht
, &iter
);
3115 switch (consumer_data
.type
) {
3116 case LTTNG_CONSUMER_KERNEL
:
3118 case LTTNG_CONSUMER32_UST
:
3119 case LTTNG_CONSUMER64_UST
:
3120 health_code_update();
3121 /* Destroy streams that might have been left in the stream list. */
3122 clean_channel_stream_list(chan
);
3125 ERR("Unknown consumer_data type");
3130 * Release our own refcount. Force channel deletion even if
3131 * streams were not initialized.
3133 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
3134 consumer_del_channel(chan
);
3139 case CONSUMER_CHANNEL_QUIT
:
3141 * Remove the pipe from the poll set and continue the loop
3142 * since their might be data to consume.
3144 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3147 ERR("Unknown action");
3150 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3151 DBG("Channel thread pipe hung up");
3153 * Remove the pipe from the poll set and continue the loop
3154 * since their might be data to consume.
3156 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3159 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3163 /* Handle other stream */
3169 uint64_t tmp_id
= (uint64_t) pollfd
;
3171 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3173 node
= lttng_ht_iter_get_node_u64(&iter
);
3176 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
3179 /* Check for error event */
3180 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3181 DBG("Channel fd %d is hup|err.", pollfd
);
3183 lttng_poll_del(&events
, chan
->wait_fd
);
3184 ret
= lttng_ht_del(channel_ht
, &iter
);
3188 * This will close the wait fd for each stream associated to
3189 * this channel AND monitored by the data/metadata thread thus
3190 * will be clean by the right thread.
3192 consumer_close_channel_streams(chan
);
3194 /* Release our own refcount */
3195 if (!uatomic_sub_return(&chan
->refcount
, 1)
3196 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3197 consumer_del_channel(chan
);
3200 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3205 /* Release RCU lock for the channel looked up */
3213 lttng_poll_clean(&events
);
3215 destroy_channel_ht(channel_ht
);
3218 DBG("Channel poll thread exiting");
3221 ERR("Health error occurred in %s", __func__
);
3223 health_unregister(health_consumerd
);
3224 rcu_unregister_thread();
3228 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3229 struct pollfd
*sockpoll
, int client_socket
)
3236 ret
= lttng_consumer_poll_socket(sockpoll
);
3240 DBG("Metadata connection on client_socket");
3242 /* Blocking call, waiting for transmission */
3243 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3244 if (ctx
->consumer_metadata_socket
< 0) {
3245 WARN("On accept metadata");
3256 * This thread listens on the consumerd socket and receives the file
3257 * descriptors from the session daemon.
3259 void *consumer_thread_sessiond_poll(void *data
)
3261 int sock
= -1, client_socket
, ret
, err
= -1;
3263 * structure to poll for incoming data on communication socket avoids
3264 * making blocking sockets.
3266 struct pollfd consumer_sockpoll
[2];
3267 struct lttng_consumer_local_data
*ctx
= data
;
3269 rcu_register_thread();
3271 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3273 if (testpoint(consumerd_thread_sessiond
)) {
3274 goto error_testpoint
;
3277 health_code_update();
3279 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3280 unlink(ctx
->consumer_command_sock_path
);
3281 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3282 if (client_socket
< 0) {
3283 ERR("Cannot create command socket");
3287 ret
= lttcomm_listen_unix_sock(client_socket
);
3292 DBG("Sending ready command to lttng-sessiond");
3293 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3294 /* return < 0 on error, but == 0 is not fatal */
3296 ERR("Error sending ready command to lttng-sessiond");
3300 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3301 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3302 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3303 consumer_sockpoll
[1].fd
= client_socket
;
3304 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3306 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3314 DBG("Connection on client_socket");
3316 /* Blocking call, waiting for transmission */
3317 sock
= lttcomm_accept_unix_sock(client_socket
);
3324 * Setup metadata socket which is the second socket connection on the
3325 * command unix socket.
3327 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3336 /* This socket is not useful anymore. */
3337 ret
= close(client_socket
);
3339 PERROR("close client_socket");
3343 /* update the polling structure to poll on the established socket */
3344 consumer_sockpoll
[1].fd
= sock
;
3345 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3348 health_code_update();
3350 health_poll_entry();
3351 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3360 DBG("Incoming command on sock");
3361 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3364 * This could simply be a session daemon quitting. Don't output
3367 DBG("Communication interrupted on command socket");
3371 if (CMM_LOAD_SHARED(consumer_quit
)) {
3372 DBG("consumer_thread_receive_fds received quit from signal");
3373 err
= 0; /* All is OK */
3376 DBG("received command on sock");
3382 DBG("Consumer thread sessiond poll exiting");
3385 * Close metadata streams since the producer is the session daemon which
3388 * NOTE: for now, this only applies to the UST tracer.
3390 lttng_consumer_close_all_metadata();
3393 * when all fds have hung up, the polling thread
3396 CMM_STORE_SHARED(consumer_quit
, 1);
3399 * Notify the data poll thread to poll back again and test the
3400 * consumer_quit state that we just set so to quit gracefully.
3402 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3404 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3406 notify_health_quit_pipe(health_quit_pipe
);
3408 /* Cleaning up possibly open sockets. */
3412 PERROR("close sock sessiond poll");
3415 if (client_socket
>= 0) {
3416 ret
= close(client_socket
);
3418 PERROR("close client_socket sessiond poll");
3425 ERR("Health error occurred in %s", __func__
);
3427 health_unregister(health_consumerd
);
3429 rcu_unregister_thread();
3433 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3434 struct lttng_consumer_local_data
*ctx
)
3438 pthread_mutex_lock(&stream
->chan
->lock
);
3439 pthread_mutex_lock(&stream
->lock
);
3440 if (stream
->metadata_flag
) {
3441 pthread_mutex_lock(&stream
->metadata_rdv_lock
);
3444 switch (consumer_data
.type
) {
3445 case LTTNG_CONSUMER_KERNEL
:
3446 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3448 case LTTNG_CONSUMER32_UST
:
3449 case LTTNG_CONSUMER64_UST
:
3450 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3453 ERR("Unknown consumer_data type");
3459 if (stream
->metadata_flag
) {
3460 pthread_cond_broadcast(&stream
->metadata_rdv
);
3461 pthread_mutex_unlock(&stream
->metadata_rdv_lock
);
3463 pthread_mutex_unlock(&stream
->lock
);
3464 pthread_mutex_unlock(&stream
->chan
->lock
);
3469 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3471 switch (consumer_data
.type
) {
3472 case LTTNG_CONSUMER_KERNEL
:
3473 return lttng_kconsumer_on_recv_stream(stream
);
3474 case LTTNG_CONSUMER32_UST
:
3475 case LTTNG_CONSUMER64_UST
:
3476 return lttng_ustconsumer_on_recv_stream(stream
);
3478 ERR("Unknown consumer_data type");
3485 * Allocate and set consumer data hash tables.
3487 int lttng_consumer_init(void)
3489 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3490 if (!consumer_data
.channel_ht
) {
3494 consumer_data
.channels_by_session_id_ht
=
3495 lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3496 if (!consumer_data
.channels_by_session_id_ht
) {
3500 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3501 if (!consumer_data
.relayd_ht
) {
3505 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3506 if (!consumer_data
.stream_list_ht
) {
3510 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3511 if (!consumer_data
.stream_per_chan_id_ht
) {
3515 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3520 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3525 consumer_data
.chunk_registry
= lttng_trace_chunk_registry_create();
3526 if (!consumer_data
.chunk_registry
) {
3537 * Process the ADD_RELAYD command receive by a consumer.
3539 * This will create a relayd socket pair and add it to the relayd hash table.
3540 * The caller MUST acquire a RCU read side lock before calling it.
3542 void consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3543 struct lttng_consumer_local_data
*ctx
, int sock
,
3544 struct pollfd
*consumer_sockpoll
,
3545 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3546 uint64_t relayd_session_id
)
3548 int fd
= -1, ret
= -1, relayd_created
= 0;
3549 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3550 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3553 assert(relayd_sock
);
3555 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3557 /* Get relayd reference if exists. */
3558 relayd
= consumer_find_relayd(net_seq_idx
);
3559 if (relayd
== NULL
) {
3560 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3561 /* Not found. Allocate one. */
3562 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3563 if (relayd
== NULL
) {
3564 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3567 relayd
->sessiond_session_id
= sessiond_id
;
3572 * This code path MUST continue to the consumer send status message to
3573 * we can notify the session daemon and continue our work without
3574 * killing everything.
3578 * relayd key should never be found for control socket.
3580 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3583 /* First send a status message before receiving the fds. */
3584 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3586 /* Somehow, the session daemon is not responding anymore. */
3587 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3588 goto error_nosignal
;
3591 /* Poll on consumer socket. */
3592 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3594 /* Needing to exit in the middle of a command: error. */
3595 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3596 goto error_nosignal
;
3599 /* Get relayd socket from session daemon */
3600 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3601 if (ret
!= sizeof(fd
)) {
3602 fd
= -1; /* Just in case it gets set with an invalid value. */
3605 * Failing to receive FDs might indicate a major problem such as
3606 * reaching a fd limit during the receive where the kernel returns a
3607 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3608 * don't take any chances and stop everything.
3610 * XXX: Feature request #558 will fix that and avoid this possible
3611 * issue when reaching the fd limit.
3613 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3614 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3618 /* Copy socket information and received FD */
3619 switch (sock_type
) {
3620 case LTTNG_STREAM_CONTROL
:
3621 /* Copy received lttcomm socket */
3622 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3623 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3624 /* Handle create_sock error. */
3626 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3630 * Close the socket created internally by
3631 * lttcomm_create_sock, so we can replace it by the one
3632 * received from sessiond.
3634 if (close(relayd
->control_sock
.sock
.fd
)) {
3638 /* Assign new file descriptor */
3639 relayd
->control_sock
.sock
.fd
= fd
;
3640 /* Assign version values. */
3641 relayd
->control_sock
.major
= relayd_sock
->major
;
3642 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3644 relayd
->relayd_session_id
= relayd_session_id
;
3647 case LTTNG_STREAM_DATA
:
3648 /* Copy received lttcomm socket */
3649 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3650 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3651 /* Handle create_sock error. */
3653 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3657 * Close the socket created internally by
3658 * lttcomm_create_sock, so we can replace it by the one
3659 * received from sessiond.
3661 if (close(relayd
->data_sock
.sock
.fd
)) {
3665 /* Assign new file descriptor */
3666 relayd
->data_sock
.sock
.fd
= fd
;
3667 /* Assign version values. */
3668 relayd
->data_sock
.major
= relayd_sock
->major
;
3669 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3672 ERR("Unknown relayd socket type (%d)", sock_type
);
3673 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3677 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3678 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3679 relayd
->net_seq_idx
, fd
);
3681 * We gave the ownership of the fd to the relayd structure. Set the
3682 * fd to -1 so we don't call close() on it in the error path below.
3686 /* We successfully added the socket. Send status back. */
3687 ret
= consumer_send_status_msg(sock
, ret_code
);
3689 /* Somehow, the session daemon is not responding anymore. */
3690 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3691 goto error_nosignal
;
3695 * Add relayd socket pair to consumer data hashtable. If object already
3696 * exists or on error, the function gracefully returns.
3705 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3706 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3710 /* Close received socket if valid. */
3713 PERROR("close received socket");
3717 if (relayd_created
) {
3723 * Search for a relayd associated to the session id and return the reference.
3725 * A rcu read side lock MUST be acquire before calling this function and locked
3726 * until the relayd object is no longer necessary.
3728 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3730 struct lttng_ht_iter iter
;
3731 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3733 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3734 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3737 * Check by sessiond id which is unique here where the relayd session
3738 * id might not be when having multiple relayd.
3740 if (relayd
->sessiond_session_id
== id
) {
3741 /* Found the relayd. There can be only one per id. */
3753 * Check if for a given session id there is still data needed to be extract
3756 * Return 1 if data is pending or else 0 meaning ready to be read.
3758 int consumer_data_pending(uint64_t id
)
3761 struct lttng_ht_iter iter
;
3762 struct lttng_ht
*ht
;
3763 struct lttng_consumer_stream
*stream
;
3764 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3765 int (*data_pending
)(struct lttng_consumer_stream
*);
3767 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3770 pthread_mutex_lock(&consumer_data
.lock
);
3772 switch (consumer_data
.type
) {
3773 case LTTNG_CONSUMER_KERNEL
:
3774 data_pending
= lttng_kconsumer_data_pending
;
3776 case LTTNG_CONSUMER32_UST
:
3777 case LTTNG_CONSUMER64_UST
:
3778 data_pending
= lttng_ustconsumer_data_pending
;
3781 ERR("Unknown consumer data type");
3785 /* Ease our life a bit */
3786 ht
= consumer_data
.stream_list_ht
;
3788 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3789 ht
->hash_fct(&id
, lttng_ht_seed
),
3791 &iter
.iter
, stream
, node_session_id
.node
) {
3792 pthread_mutex_lock(&stream
->lock
);
3795 * A removed node from the hash table indicates that the stream has
3796 * been deleted thus having a guarantee that the buffers are closed
3797 * on the consumer side. However, data can still be transmitted
3798 * over the network so don't skip the relayd check.
3800 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3802 /* Check the stream if there is data in the buffers. */
3803 ret
= data_pending(stream
);
3805 pthread_mutex_unlock(&stream
->lock
);
3810 pthread_mutex_unlock(&stream
->lock
);
3813 relayd
= find_relayd_by_session_id(id
);
3815 unsigned int is_data_inflight
= 0;
3817 /* Send init command for data pending. */
3818 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3819 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3820 relayd
->relayd_session_id
);
3822 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3823 /* Communication error thus the relayd so no data pending. */
3824 goto data_not_pending
;
3827 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3828 ht
->hash_fct(&id
, lttng_ht_seed
),
3830 &iter
.iter
, stream
, node_session_id
.node
) {
3831 if (stream
->metadata_flag
) {
3832 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3833 stream
->relayd_stream_id
);
3835 ret
= relayd_data_pending(&relayd
->control_sock
,
3836 stream
->relayd_stream_id
,
3837 stream
->next_net_seq_num
- 1);
3841 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3843 } else if (ret
< 0) {
3844 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3845 lttng_consumer_cleanup_relayd(relayd
);
3846 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3847 goto data_not_pending
;
3851 /* Send end command for data pending. */
3852 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3853 relayd
->relayd_session_id
, &is_data_inflight
);
3854 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3856 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3857 lttng_consumer_cleanup_relayd(relayd
);
3858 goto data_not_pending
;
3860 if (is_data_inflight
) {
3866 * Finding _no_ node in the hash table and no inflight data means that the
3867 * stream(s) have been removed thus data is guaranteed to be available for
3868 * analysis from the trace files.
3872 /* Data is available to be read by a viewer. */
3873 pthread_mutex_unlock(&consumer_data
.lock
);
3878 /* Data is still being extracted from buffers. */
3879 pthread_mutex_unlock(&consumer_data
.lock
);
3885 * Send a ret code status message to the sessiond daemon.
3887 * Return the sendmsg() return value.
3889 int consumer_send_status_msg(int sock
, int ret_code
)
3891 struct lttcomm_consumer_status_msg msg
;
3893 memset(&msg
, 0, sizeof(msg
));
3894 msg
.ret_code
= ret_code
;
3896 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3900 * Send a channel status message to the sessiond daemon.
3902 * Return the sendmsg() return value.
3904 int consumer_send_status_channel(int sock
,
3905 struct lttng_consumer_channel
*channel
)
3907 struct lttcomm_consumer_status_channel msg
;
3911 memset(&msg
, 0, sizeof(msg
));
3913 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3915 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3916 msg
.key
= channel
->key
;
3917 msg
.stream_count
= channel
->streams
.count
;
3920 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3923 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3924 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3925 uint64_t max_sb_size
)
3927 unsigned long start_pos
;
3929 if (!nb_packets_per_stream
) {
3930 return consumed_pos
; /* Grab everything */
3932 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
3933 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3934 if ((long) (start_pos
- consumed_pos
) < 0) {
3935 return consumed_pos
; /* Grab everything */
3941 int consumer_flush_buffer(struct lttng_consumer_stream
*stream
, int producer_active
)
3945 switch (consumer_data
.type
) {
3946 case LTTNG_CONSUMER_KERNEL
:
3947 if (producer_active
) {
3948 ret
= kernctl_buffer_flush(stream
->wait_fd
);
3950 ERR("Failed to flush kernel stream");
3954 ret
= kernctl_buffer_flush_empty(stream
->wait_fd
);
3956 ERR("Failed to flush kernel stream");
3961 case LTTNG_CONSUMER32_UST
:
3962 case LTTNG_CONSUMER64_UST
:
3963 lttng_ustconsumer_flush_buffer(stream
, producer_active
);
3966 ERR("Unknown consumer_data type");
3975 * Sample the rotate position for all the streams of a channel. If a stream
3976 * is already at the rotate position (produced == consumed), we flag it as
3977 * ready for rotation. The rotation of ready streams occurs after we have
3978 * replied to the session daemon that we have finished sampling the positions.
3979 * Must be called with RCU read-side lock held to ensure existence of channel.
3981 * Returns 0 on success, < 0 on error
3983 int lttng_consumer_rotate_channel(struct lttng_consumer_channel
*channel
,
3984 uint64_t key
, uint64_t relayd_id
, uint32_t metadata
,
3985 struct lttng_consumer_local_data
*ctx
)
3988 struct lttng_consumer_stream
*stream
;
3989 struct lttng_ht_iter iter
;
3990 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
3991 struct lttng_dynamic_array stream_rotation_positions
;
3992 uint64_t next_chunk_id
, stream_count
= 0;
3993 enum lttng_trace_chunk_status chunk_status
;
3994 const bool is_local_trace
= relayd_id
== -1ULL;
3995 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3996 bool rotating_to_new_chunk
= true;
3998 DBG("Consumer sample rotate position for channel %" PRIu64
, key
);
4000 lttng_dynamic_array_init(&stream_rotation_positions
,
4001 sizeof(struct relayd_stream_rotation_position
), NULL
);
4005 pthread_mutex_lock(&channel
->lock
);
4006 assert(channel
->trace_chunk
);
4007 chunk_status
= lttng_trace_chunk_get_id(channel
->trace_chunk
,
4009 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4011 goto end_unlock_channel
;
4014 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4015 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4016 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4017 stream
, node_channel_id
.node
) {
4018 unsigned long produced_pos
= 0, consumed_pos
= 0;
4020 health_code_update();
4023 * Lock stream because we are about to change its state.
4025 pthread_mutex_lock(&stream
->lock
);
4027 if (stream
->trace_chunk
== stream
->chan
->trace_chunk
) {
4028 rotating_to_new_chunk
= false;
4032 * Do not flush an empty packet when rotating from a NULL trace
4033 * chunk. The stream has no means to output data, and the prior
4034 * rotation which rotated to NULL performed that side-effect already.
4036 if (stream
->trace_chunk
) {
4038 * For metadata stream, do an active flush, which does not
4039 * produce empty packets. For data streams, empty-flush;
4040 * ensures we have at least one packet in each stream per trace
4041 * chunk, even if no data was produced.
4043 ret
= consumer_flush_buffer(stream
, stream
->metadata_flag
? 1 : 0);
4045 ERR("Failed to flush stream %" PRIu64
" during channel rotation",
4047 goto end_unlock_stream
;
4051 ret
= lttng_consumer_take_snapshot(stream
);
4052 if (ret
< 0 && ret
!= -ENODATA
&& ret
!= -EAGAIN
) {
4053 ERR("Failed to sample snapshot position during channel rotation");
4054 goto end_unlock_stream
;
4057 ret
= lttng_consumer_get_produced_snapshot(stream
,
4060 ERR("Failed to sample produced position during channel rotation");
4061 goto end_unlock_stream
;
4064 ret
= lttng_consumer_get_consumed_snapshot(stream
,
4067 ERR("Failed to sample consumed position during channel rotation");
4068 goto end_unlock_stream
;
4072 * Align produced position on the start-of-packet boundary of the first
4073 * packet going into the next trace chunk.
4075 produced_pos
= ALIGN_FLOOR(produced_pos
, stream
->max_sb_size
);
4076 if (consumed_pos
== produced_pos
) {
4077 stream
->rotate_ready
= true;
4080 * The rotation position is based on the packet_seq_num of the
4081 * packet following the last packet that was consumed for this
4082 * stream, incremented by the offset between produced and
4083 * consumed positions. This rotation position is a lower bound
4084 * (inclusive) at which the next trace chunk starts. Since it
4085 * is a lower bound, it is OK if the packet_seq_num does not
4086 * correspond exactly to the same packet identified by the
4087 * consumed_pos, which can happen in overwrite mode.
4089 if (stream
->sequence_number_unavailable
) {
4091 * Rotation should never be performed on a session which
4092 * interacts with a pre-2.8 lttng-modules, which does
4093 * not implement packet sequence number.
4095 ERR("Failure to rotate stream %" PRIu64
": sequence number unavailable",
4098 goto end_unlock_stream
;
4100 stream
->rotate_position
= stream
->last_sequence_number
+ 1 +
4101 ((produced_pos
- consumed_pos
) / stream
->max_sb_size
);
4103 if (!is_local_trace
) {
4105 * The relay daemon control protocol expects a rotation
4106 * position as "the sequence number of the first packet
4107 * _after_ the current trace chunk".
4109 const struct relayd_stream_rotation_position position
= {
4110 .stream_id
= stream
->relayd_stream_id
,
4111 .rotate_at_seq_num
= stream
->rotate_position
,
4114 ret
= lttng_dynamic_array_add_element(
4115 &stream_rotation_positions
,
4118 ERR("Failed to allocate stream rotation position");
4119 goto end_unlock_stream
;
4123 pthread_mutex_unlock(&stream
->lock
);
4126 pthread_mutex_unlock(&channel
->lock
);
4128 if (is_local_trace
) {
4133 relayd
= consumer_find_relayd(relayd_id
);
4135 ERR("Failed to find relayd %" PRIu64
, relayd_id
);
4140 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4141 ret
= relayd_rotate_streams(&relayd
->control_sock
, stream_count
,
4142 rotating_to_new_chunk
? &next_chunk_id
: NULL
,
4143 (const struct relayd_stream_rotation_position
*)
4144 stream_rotation_positions
.buffer
.data
);
4145 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4147 ERR("Relayd rotate stream failed. Cleaning up relayd %" PRIu64
,
4148 relayd
->net_seq_idx
);
4149 lttng_consumer_cleanup_relayd(relayd
);
4157 pthread_mutex_unlock(&stream
->lock
);
4159 pthread_mutex_unlock(&channel
->lock
);
4162 lttng_dynamic_array_reset(&stream_rotation_positions
);
4167 * Check if a stream is ready to be rotated after extracting it.
4169 * Return 1 if it is ready for rotation, 0 if it is not, a negative value on
4170 * error. Stream lock must be held.
4172 int lttng_consumer_stream_is_rotate_ready(struct lttng_consumer_stream
*stream
)
4174 if (stream
->rotate_ready
) {
4179 * If packet seq num is unavailable, it means we are interacting
4180 * with a pre-2.8 lttng-modules which does not implement the
4181 * sequence number. Rotation should never be used by sessiond in this
4184 if (stream
->sequence_number_unavailable
) {
4185 ERR("Internal error: rotation used on stream %" PRIu64
4186 " with unavailable sequence number",
4191 if (stream
->rotate_position
== -1ULL ||
4192 stream
->last_sequence_number
== -1ULL) {
4197 * Rotate position not reached yet. The stream rotate position is
4198 * the position of the next packet belonging to the next trace chunk,
4199 * but consumerd considers rotation ready when reaching the last
4200 * packet of the current chunk, hence the "rotate_position - 1".
4202 if (stream
->last_sequence_number
>= stream
->rotate_position
- 1) {
4210 * Reset the state for a stream after a rotation occurred.
4212 void lttng_consumer_reset_stream_rotate_state(struct lttng_consumer_stream
*stream
)
4214 stream
->rotate_position
= -1ULL;
4215 stream
->rotate_ready
= false;
4219 * Perform the rotation a local stream file.
4222 int rotate_local_stream(struct lttng_consumer_local_data
*ctx
,
4223 struct lttng_consumer_stream
*stream
)
4227 DBG("Rotate local stream: stream key %" PRIu64
", channel key %" PRIu64
,
4230 stream
->tracefile_size_current
= 0;
4231 stream
->tracefile_count_current
= 0;
4233 if (stream
->out_fd
>= 0) {
4234 ret
= close(stream
->out_fd
);
4236 PERROR("Failed to close stream out_fd of channel \"%s\"",
4237 stream
->chan
->name
);
4239 stream
->out_fd
= -1;
4242 if (stream
->index_file
) {
4243 lttng_index_file_put(stream
->index_file
);
4244 stream
->index_file
= NULL
;
4247 if (!stream
->trace_chunk
) {
4251 ret
= consumer_stream_create_output_files(stream
, true);
4257 * Performs the stream rotation for the rotate session feature if needed.
4258 * It must be called with the channel and stream locks held.
4260 * Return 0 on success, a negative number of error.
4262 int lttng_consumer_rotate_stream(struct lttng_consumer_local_data
*ctx
,
4263 struct lttng_consumer_stream
*stream
)
4267 DBG("Consumer rotate stream %" PRIu64
, stream
->key
);
4270 * Update the stream's 'current' chunk to the session's (channel)
4271 * now-current chunk.
4273 lttng_trace_chunk_put(stream
->trace_chunk
);
4274 if (stream
->chan
->trace_chunk
== stream
->trace_chunk
) {
4276 * A channel can be rotated and not have a "next" chunk
4277 * to transition to. In that case, the channel's "current chunk"
4278 * has not been closed yet, but it has not been updated to
4279 * a "next" trace chunk either. Hence, the stream, like its
4280 * parent channel, becomes part of no chunk and can't output
4281 * anything until a new trace chunk is created.
4283 stream
->trace_chunk
= NULL
;
4284 } else if (stream
->chan
->trace_chunk
&&
4285 !lttng_trace_chunk_get(stream
->chan
->trace_chunk
)) {
4286 ERR("Failed to acquire a reference to channel's trace chunk during stream rotation");
4291 * Update the stream's trace chunk to its parent channel's
4292 * current trace chunk.
4294 stream
->trace_chunk
= stream
->chan
->trace_chunk
;
4297 if (stream
->net_seq_idx
== (uint64_t) -1ULL) {
4298 ret
= rotate_local_stream(ctx
, stream
);
4300 ERR("Failed to rotate stream, ret = %i", ret
);
4305 if (stream
->metadata_flag
&& stream
->trace_chunk
) {
4307 * If the stream has transitioned to a new trace
4308 * chunk, the metadata should be re-dumped to the
4311 * However, it is possible for a stream to transition to
4312 * a "no-chunk" state. This can happen if a rotation
4313 * occurs on an inactive session. In such cases, the metadata
4314 * regeneration will happen when the next trace chunk is
4317 ret
= consumer_metadata_stream_dump(stream
);
4322 lttng_consumer_reset_stream_rotate_state(stream
);
4331 * Rotate all the ready streams now.
4333 * This is especially important for low throughput streams that have already
4334 * been consumed, we cannot wait for their next packet to perform the
4336 * Need to be called with RCU read-side lock held to ensure existence of
4339 * Returns 0 on success, < 0 on error
4341 int lttng_consumer_rotate_ready_streams(struct lttng_consumer_channel
*channel
,
4342 uint64_t key
, struct lttng_consumer_local_data
*ctx
)
4345 struct lttng_consumer_stream
*stream
;
4346 struct lttng_ht_iter iter
;
4347 struct lttng_ht
*ht
= consumer_data
.stream_per_chan_id_ht
;
4351 DBG("Consumer rotate ready streams in channel %" PRIu64
, key
);
4353 cds_lfht_for_each_entry_duplicate(ht
->ht
,
4354 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
4355 ht
->match_fct
, &channel
->key
, &iter
.iter
,
4356 stream
, node_channel_id
.node
) {
4357 health_code_update();
4359 pthread_mutex_lock(&stream
->chan
->lock
);
4360 pthread_mutex_lock(&stream
->lock
);
4362 if (!stream
->rotate_ready
) {
4363 pthread_mutex_unlock(&stream
->lock
);
4364 pthread_mutex_unlock(&stream
->chan
->lock
);
4367 DBG("Consumer rotate ready stream %" PRIu64
, stream
->key
);
4369 ret
= lttng_consumer_rotate_stream(ctx
, stream
);
4370 pthread_mutex_unlock(&stream
->lock
);
4371 pthread_mutex_unlock(&stream
->chan
->lock
);
4384 enum lttcomm_return_code
lttng_consumer_init_command(
4385 struct lttng_consumer_local_data
*ctx
,
4386 const lttng_uuid sessiond_uuid
)
4388 enum lttcomm_return_code ret
;
4389 char uuid_str
[UUID_STR_LEN
];
4391 if (ctx
->sessiond_uuid
.is_set
) {
4392 ret
= LTTCOMM_CONSUMERD_ALREADY_SET
;
4396 ctx
->sessiond_uuid
.is_set
= true;
4397 memcpy(ctx
->sessiond_uuid
.value
, sessiond_uuid
, sizeof(lttng_uuid
));
4398 ret
= LTTCOMM_CONSUMERD_SUCCESS
;
4399 lttng_uuid_to_str(sessiond_uuid
, uuid_str
);
4400 DBG("Received session daemon UUID: %s", uuid_str
);
4405 enum lttcomm_return_code
lttng_consumer_create_trace_chunk(
4406 const uint64_t *relayd_id
, uint64_t session_id
,
4408 time_t chunk_creation_timestamp
,
4409 const char *chunk_override_name
,
4410 const struct lttng_credentials
*credentials
,
4411 struct lttng_directory_handle
*chunk_directory_handle
)
4414 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4415 struct lttng_trace_chunk
*created_chunk
= NULL
, *published_chunk
= NULL
;
4416 enum lttng_trace_chunk_status chunk_status
;
4417 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4418 char creation_timestamp_buffer
[ISO8601_STR_LEN
];
4419 const char *relayd_id_str
= "(none)";
4420 const char *creation_timestamp_str
;
4421 struct lttng_ht_iter iter
;
4422 struct lttng_consumer_channel
*channel
;
4425 /* Only used for logging purposes. */
4426 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4427 "%" PRIu64
, *relayd_id
);
4428 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4429 relayd_id_str
= relayd_id_buffer
;
4431 relayd_id_str
= "(formatting error)";
4435 /* Local protocol error. */
4436 assert(chunk_creation_timestamp
);
4437 ret
= time_to_iso8601_str(chunk_creation_timestamp
,
4438 creation_timestamp_buffer
,
4439 sizeof(creation_timestamp_buffer
));
4440 creation_timestamp_str
= !ret
? creation_timestamp_buffer
:
4441 "(formatting error)";
4443 DBG("Consumer create trace chunk command: relay_id = %s"
4444 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4445 ", chunk_override_name = %s"
4446 ", chunk_creation_timestamp = %s",
4447 relayd_id_str
, session_id
, chunk_id
,
4448 chunk_override_name
? : "(none)",
4449 creation_timestamp_str
);
4452 * The trace chunk registry, as used by the consumer daemon, implicitly
4453 * owns the trace chunks. This is only needed in the consumer since
4454 * the consumer has no notion of a session beyond session IDs being
4455 * used to identify other objects.
4457 * The lttng_trace_chunk_registry_publish() call below provides a
4458 * reference which is not released; it implicitly becomes the session
4459 * daemon's reference to the chunk in the consumer daemon.
4461 * The lifetime of trace chunks in the consumer daemon is managed by
4462 * the session daemon through the LTTNG_CONSUMER_CREATE_TRACE_CHUNK
4463 * and LTTNG_CONSUMER_DESTROY_TRACE_CHUNK commands.
4465 created_chunk
= lttng_trace_chunk_create(chunk_id
,
4466 chunk_creation_timestamp
);
4467 if (!created_chunk
) {
4468 ERR("Failed to create trace chunk");
4469 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4473 if (chunk_override_name
) {
4474 chunk_status
= lttng_trace_chunk_override_name(created_chunk
,
4475 chunk_override_name
);
4476 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4477 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4482 if (chunk_directory_handle
) {
4483 chunk_status
= lttng_trace_chunk_set_credentials(created_chunk
,
4485 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4486 ERR("Failed to set trace chunk credentials");
4487 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4491 * The consumer daemon has no ownership of the chunk output
4494 chunk_status
= lttng_trace_chunk_set_as_user(created_chunk
,
4495 chunk_directory_handle
);
4496 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4497 ERR("Failed to set trace chunk's directory handle");
4498 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4503 published_chunk
= lttng_trace_chunk_registry_publish_chunk(
4504 consumer_data
.chunk_registry
, session_id
,
4506 lttng_trace_chunk_put(created_chunk
);
4507 created_chunk
= NULL
;
4508 if (!published_chunk
) {
4509 ERR("Failed to publish trace chunk");
4510 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4515 cds_lfht_for_each_entry_duplicate(consumer_data
.channels_by_session_id_ht
->ht
,
4516 consumer_data
.channels_by_session_id_ht
->hash_fct(
4517 &session_id
, lttng_ht_seed
),
4518 consumer_data
.channels_by_session_id_ht
->match_fct
,
4519 &session_id
, &iter
.iter
, channel
,
4520 channels_by_session_id_ht_node
.node
) {
4521 ret
= lttng_consumer_channel_set_trace_chunk(channel
,
4525 * Roll-back the creation of this chunk.
4527 * This is important since the session daemon will
4528 * assume that the creation of this chunk failed and
4529 * will never ask for it to be closed, resulting
4530 * in a leak and an inconsistent state for some
4533 enum lttcomm_return_code close_ret
;
4534 char path
[LTTNG_PATH_MAX
];
4536 DBG("Failed to set new trace chunk on existing channels, rolling back");
4537 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4538 session_id
, chunk_id
,
4539 chunk_creation_timestamp
, NULL
,
4541 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4542 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4543 session_id
, chunk_id
);
4546 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4552 struct consumer_relayd_sock_pair
*relayd
;
4554 relayd
= consumer_find_relayd(*relayd_id
);
4556 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4557 ret
= relayd_create_trace_chunk(
4558 &relayd
->control_sock
, published_chunk
);
4559 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4561 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
, *relayd_id
);
4564 if (!relayd
|| ret
) {
4565 enum lttcomm_return_code close_ret
;
4566 char path
[LTTNG_PATH_MAX
];
4568 close_ret
= lttng_consumer_close_trace_chunk(relayd_id
,
4571 chunk_creation_timestamp
,
4573 if (close_ret
!= LTTCOMM_CONSUMERD_SUCCESS
) {
4574 ERR("Failed to roll-back the creation of new chunk: session_id = %" PRIu64
", chunk_id = %" PRIu64
,
4579 ret_code
= LTTCOMM_CONSUMERD_CREATE_TRACE_CHUNK_FAILED
;
4586 /* Release the reference returned by the "publish" operation. */
4587 lttng_trace_chunk_put(published_chunk
);
4588 lttng_trace_chunk_put(created_chunk
);
4592 enum lttcomm_return_code
lttng_consumer_close_trace_chunk(
4593 const uint64_t *relayd_id
, uint64_t session_id
,
4594 uint64_t chunk_id
, time_t chunk_close_timestamp
,
4595 const enum lttng_trace_chunk_command_type
*close_command
,
4598 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
4599 struct lttng_trace_chunk
*chunk
;
4600 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4601 const char *relayd_id_str
= "(none)";
4602 const char *close_command_name
= "none";
4603 struct lttng_ht_iter iter
;
4604 struct lttng_consumer_channel
*channel
;
4605 enum lttng_trace_chunk_status chunk_status
;
4610 /* Only used for logging purposes. */
4611 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4612 "%" PRIu64
, *relayd_id
);
4613 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4614 relayd_id_str
= relayd_id_buffer
;
4616 relayd_id_str
= "(formatting error)";
4619 if (close_command
) {
4620 close_command_name
= lttng_trace_chunk_command_type_get_name(
4624 DBG("Consumer close trace chunk command: relayd_id = %s"
4625 ", session_id = %" PRIu64
", chunk_id = %" PRIu64
4626 ", close command = %s",
4627 relayd_id_str
, session_id
, chunk_id
,
4628 close_command_name
);
4630 chunk
= lttng_trace_chunk_registry_find_chunk(
4631 consumer_data
.chunk_registry
, session_id
, chunk_id
);
4633 ERR("Failed to find chunk: session_id = %" PRIu64
4634 ", chunk_id = %" PRIu64
,
4635 session_id
, chunk_id
);
4636 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4640 chunk_status
= lttng_trace_chunk_set_close_timestamp(chunk
,
4641 chunk_close_timestamp
);
4642 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4643 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4647 if (close_command
) {
4648 chunk_status
= lttng_trace_chunk_set_close_command(
4649 chunk
, *close_command
);
4650 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4651 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4657 * chunk is now invalid to access as we no longer hold a reference to
4658 * it; it is only kept around to compare it (by address) to the
4659 * current chunk found in the session's channels.
4662 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
,
4663 channel
, node
.node
) {
4667 * Only change the channel's chunk to NULL if it still
4668 * references the chunk being closed. The channel may
4669 * reference a newer channel in the case of a session
4670 * rotation. When a session rotation occurs, the "next"
4671 * chunk is created before the "current" chunk is closed.
4673 if (channel
->trace_chunk
!= chunk
) {
4676 ret
= lttng_consumer_channel_set_trace_chunk(channel
, NULL
);
4679 * Attempt to close the chunk on as many channels as
4682 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4688 struct consumer_relayd_sock_pair
*relayd
;
4690 relayd
= consumer_find_relayd(*relayd_id
);
4692 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4693 ret
= relayd_close_trace_chunk(
4694 &relayd
->control_sock
, chunk
,
4696 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4698 ERR("Failed to find relay daemon socket: relayd_id = %" PRIu64
,
4702 if (!relayd
|| ret
) {
4703 ret_code
= LTTCOMM_CONSUMERD_CLOSE_TRACE_CHUNK_FAILED
;
4711 * Release the reference returned by the "find" operation and
4712 * the session daemon's implicit reference to the chunk.
4714 lttng_trace_chunk_put(chunk
);
4715 lttng_trace_chunk_put(chunk
);
4720 enum lttcomm_return_code
lttng_consumer_trace_chunk_exists(
4721 const uint64_t *relayd_id
, uint64_t session_id
,
4725 enum lttcomm_return_code ret_code
;
4726 char relayd_id_buffer
[MAX_INT_DEC_LEN(*relayd_id
)];
4727 const char *relayd_id_str
= "(none)";
4728 const bool is_local_trace
= !relayd_id
;
4729 struct consumer_relayd_sock_pair
*relayd
= NULL
;
4730 bool chunk_exists_local
, chunk_exists_remote
;
4735 /* Only used for logging purposes. */
4736 ret
= snprintf(relayd_id_buffer
, sizeof(relayd_id_buffer
),
4737 "%" PRIu64
, *relayd_id
);
4738 if (ret
> 0 && ret
< sizeof(relayd_id_buffer
)) {
4739 relayd_id_str
= relayd_id_buffer
;
4741 relayd_id_str
= "(formatting error)";
4745 DBG("Consumer trace chunk exists command: relayd_id = %s"
4746 ", chunk_id = %" PRIu64
, relayd_id_str
,
4748 ret
= lttng_trace_chunk_registry_chunk_exists(
4749 consumer_data
.chunk_registry
, session_id
,
4750 chunk_id
, &chunk_exists_local
);
4752 /* Internal error. */
4753 ERR("Failed to query the existence of a trace chunk");
4754 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
4757 DBG("Trace chunk %s locally",
4758 chunk_exists_local
? "exists" : "does not exist");
4759 if (chunk_exists_local
) {
4760 ret_code
= LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_LOCAL
;
4762 } else if (is_local_trace
) {
4763 ret_code
= LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4768 relayd
= consumer_find_relayd(*relayd_id
);
4770 ERR("Failed to find relayd %" PRIu64
, *relayd_id
);
4771 ret_code
= LTTCOMM_CONSUMERD_INVALID_PARAMETERS
;
4772 goto end_rcu_unlock
;
4774 DBG("Looking up existence of trace chunk on relay daemon");
4775 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
4776 ret
= relayd_trace_chunk_exists(&relayd
->control_sock
, chunk_id
,
4777 &chunk_exists_remote
);
4778 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
4780 ERR("Failed to look-up the existence of trace chunk on relay daemon");
4781 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
4782 goto end_rcu_unlock
;
4785 ret_code
= chunk_exists_remote
?
4786 LTTCOMM_CONSUMERD_TRACE_CHUNK_EXISTS_REMOTE
:
4787 LTTCOMM_CONSUMERD_UNKNOWN_TRACE_CHUNK
;
4788 DBG("Trace chunk %s on relay daemon",
4789 chunk_exists_remote
? "exists" : "does not exist");