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.
28 #include <sys/socket.h>
29 #include <sys/types.h>
34 #include <bin/lttng-consumerd/health-consumerd.h>
35 #include <common/common.h>
36 #include <common/utils.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-timer.h>
49 #include "consumer-stream.h"
50 #include "consumer-testpoint.h"
53 struct lttng_consumer_global_data consumer_data
= {
56 .type
= LTTNG_CONSUMER_UNKNOWN
,
59 enum consumer_channel_action
{
62 CONSUMER_CHANNEL_QUIT
,
65 struct consumer_channel_msg
{
66 enum consumer_channel_action action
;
67 struct lttng_consumer_channel
*chan
; /* add */
68 uint64_t key
; /* del */
72 * Flag to inform the polling thread to quit when all fd hung up. Updated by
73 * the consumer_thread_receive_fds when it notices that all fds has hung up.
74 * Also updated by the signal handler (consumer_should_exit()). Read by the
77 volatile int consumer_quit
;
80 * Global hash table containing respectively metadata and data streams. The
81 * stream element in this ht should only be updated by the metadata poll thread
82 * for the metadata and the data poll thread for the data.
84 static struct lttng_ht
*metadata_ht
;
85 static struct lttng_ht
*data_ht
;
88 * Notify a thread lttng pipe to poll back again. This usually means that some
89 * global state has changed so we just send back the thread in a poll wait
92 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
94 struct lttng_consumer_stream
*null_stream
= NULL
;
98 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
101 static void notify_health_quit_pipe(int *pipe
)
105 ret
= lttng_write(pipe
[1], "4", 1);
107 PERROR("write consumer health quit");
111 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
112 struct lttng_consumer_channel
*chan
,
114 enum consumer_channel_action action
)
116 struct consumer_channel_msg msg
;
119 memset(&msg
, 0, sizeof(msg
));
124 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
125 if (ret
< sizeof(msg
)) {
126 PERROR("notify_channel_pipe write error");
130 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
133 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
136 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
137 struct lttng_consumer_channel
**chan
,
139 enum consumer_channel_action
*action
)
141 struct consumer_channel_msg msg
;
144 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
145 if (ret
< sizeof(msg
)) {
149 *action
= msg
.action
;
157 * Cleanup the stream list of a channel. Those streams are not yet globally
160 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
162 struct lttng_consumer_stream
*stream
, *stmp
;
166 /* Delete streams that might have been left in the stream list. */
167 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
169 cds_list_del(&stream
->send_node
);
171 * Once a stream is added to this list, the buffers were created so we
172 * have a guarantee that this call will succeed. Setting the monitor
173 * mode to 0 so we don't lock nor try to delete the stream from the
177 consumer_stream_destroy(stream
, NULL
);
182 * Find a stream. The consumer_data.lock must be locked during this
185 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
188 struct lttng_ht_iter iter
;
189 struct lttng_ht_node_u64
*node
;
190 struct lttng_consumer_stream
*stream
= NULL
;
194 /* -1ULL keys are lookup failures */
195 if (key
== (uint64_t) -1ULL) {
201 lttng_ht_lookup(ht
, &key
, &iter
);
202 node
= lttng_ht_iter_get_node_u64(&iter
);
204 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
212 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
214 struct lttng_consumer_stream
*stream
;
217 stream
= find_stream(key
, ht
);
219 stream
->key
= (uint64_t) -1ULL;
221 * We don't want the lookup to match, but we still need
222 * to iterate on this stream when iterating over the hash table. Just
223 * change the node key.
225 stream
->node
.key
= (uint64_t) -1ULL;
231 * Return a channel object for the given key.
233 * RCU read side lock MUST be acquired before calling this function and
234 * protects the channel ptr.
236 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
238 struct lttng_ht_iter iter
;
239 struct lttng_ht_node_u64
*node
;
240 struct lttng_consumer_channel
*channel
= NULL
;
242 /* -1ULL keys are lookup failures */
243 if (key
== (uint64_t) -1ULL) {
247 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
248 node
= lttng_ht_iter_get_node_u64(&iter
);
250 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
257 * There is a possibility that the consumer does not have enough time between
258 * the close of the channel on the session daemon and the cleanup in here thus
259 * once we have a channel add with an existing key, we know for sure that this
260 * channel will eventually get cleaned up by all streams being closed.
262 * This function just nullifies the already existing channel key.
264 static void steal_channel_key(uint64_t key
)
266 struct lttng_consumer_channel
*channel
;
269 channel
= consumer_find_channel(key
);
271 channel
->key
= (uint64_t) -1ULL;
273 * We don't want the lookup to match, but we still need to iterate on
274 * this channel when iterating over the hash table. Just change the
277 channel
->node
.key
= (uint64_t) -1ULL;
282 static void free_channel_rcu(struct rcu_head
*head
)
284 struct lttng_ht_node_u64
*node
=
285 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
286 struct lttng_consumer_channel
*channel
=
287 caa_container_of(node
, struct lttng_consumer_channel
, node
);
293 * RCU protected relayd socket pair free.
295 static void free_relayd_rcu(struct rcu_head
*head
)
297 struct lttng_ht_node_u64
*node
=
298 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
299 struct consumer_relayd_sock_pair
*relayd
=
300 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
303 * Close all sockets. This is done in the call RCU since we don't want the
304 * socket fds to be reassigned thus potentially creating bad state of the
307 * We do not have to lock the control socket mutex here since at this stage
308 * there is no one referencing to this relayd object.
310 (void) relayd_close(&relayd
->control_sock
);
311 (void) relayd_close(&relayd
->data_sock
);
317 * Destroy and free relayd socket pair object.
319 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
322 struct lttng_ht_iter iter
;
324 if (relayd
== NULL
) {
328 DBG("Consumer destroy and close relayd socket pair");
330 iter
.iter
.node
= &relayd
->node
.node
;
331 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
333 /* We assume the relayd is being or is destroyed */
337 /* RCU free() call */
338 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
342 * Remove a channel from the global list protected by a mutex. This function is
343 * also responsible for freeing its data structures.
345 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
348 struct lttng_ht_iter iter
;
350 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
352 pthread_mutex_lock(&consumer_data
.lock
);
353 pthread_mutex_lock(&channel
->lock
);
355 /* Destroy streams that might have been left in the stream list. */
356 clean_channel_stream_list(channel
);
358 if (channel
->live_timer_enabled
== 1) {
359 consumer_timer_live_stop(channel
);
362 switch (consumer_data
.type
) {
363 case LTTNG_CONSUMER_KERNEL
:
365 case LTTNG_CONSUMER32_UST
:
366 case LTTNG_CONSUMER64_UST
:
367 lttng_ustconsumer_del_channel(channel
);
370 ERR("Unknown consumer_data type");
376 iter
.iter
.node
= &channel
->node
.node
;
377 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
381 call_rcu(&channel
->node
.head
, free_channel_rcu
);
383 pthread_mutex_unlock(&channel
->lock
);
384 pthread_mutex_unlock(&consumer_data
.lock
);
388 * Iterate over the relayd hash table and destroy each element. Finally,
389 * destroy the whole hash table.
391 static void cleanup_relayd_ht(void)
393 struct lttng_ht_iter iter
;
394 struct consumer_relayd_sock_pair
*relayd
;
398 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
400 consumer_destroy_relayd(relayd
);
405 lttng_ht_destroy(consumer_data
.relayd_ht
);
409 * Update the end point status of all streams having the given network sequence
410 * index (relayd index).
412 * It's atomically set without having the stream mutex locked which is fine
413 * because we handle the write/read race with a pipe wakeup for each thread.
415 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
416 enum consumer_endpoint_status status
)
418 struct lttng_ht_iter iter
;
419 struct lttng_consumer_stream
*stream
;
421 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
425 /* Let's begin with metadata */
426 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
427 if (stream
->net_seq_idx
== net_seq_idx
) {
428 uatomic_set(&stream
->endpoint_status
, status
);
429 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
433 /* Follow up by the data streams */
434 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
435 if (stream
->net_seq_idx
== net_seq_idx
) {
436 uatomic_set(&stream
->endpoint_status
, status
);
437 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
444 * Cleanup a relayd object by flagging every associated streams for deletion,
445 * destroying the object meaning removing it from the relayd hash table,
446 * closing the sockets and freeing the memory in a RCU call.
448 * If a local data context is available, notify the threads that the streams'
449 * state have changed.
451 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
452 struct lttng_consumer_local_data
*ctx
)
458 DBG("Cleaning up relayd sockets");
460 /* Save the net sequence index before destroying the object */
461 netidx
= relayd
->net_seq_idx
;
464 * Delete the relayd from the relayd hash table, close the sockets and free
465 * the object in a RCU call.
467 consumer_destroy_relayd(relayd
);
469 /* Set inactive endpoint to all streams */
470 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
473 * With a local data context, notify the threads that the streams' state
474 * have changed. The write() action on the pipe acts as an "implicit"
475 * memory barrier ordering the updates of the end point status from the
476 * read of this status which happens AFTER receiving this notify.
479 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
480 notify_thread_lttng_pipe(ctx
->consumer_metadata_pipe
);
485 * Flag a relayd socket pair for destruction. Destroy it if the refcount
488 * RCU read side lock MUST be aquired before calling this function.
490 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
494 /* Set destroy flag for this object */
495 uatomic_set(&relayd
->destroy_flag
, 1);
497 /* Destroy the relayd if refcount is 0 */
498 if (uatomic_read(&relayd
->refcount
) == 0) {
499 consumer_destroy_relayd(relayd
);
504 * Completly destroy stream from every visiable data structure and the given
507 * One this call returns, the stream object is not longer usable nor visible.
509 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
512 consumer_stream_destroy(stream
, ht
);
516 * XXX naming of del vs destroy is all mixed up.
518 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
520 consumer_stream_destroy(stream
, data_ht
);
523 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
525 consumer_stream_destroy(stream
, metadata_ht
);
528 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
530 enum lttng_consumer_stream_state state
,
531 const char *channel_name
,
538 enum consumer_channel_type type
,
539 unsigned int monitor
)
542 struct lttng_consumer_stream
*stream
;
544 stream
= zmalloc(sizeof(*stream
));
545 if (stream
== NULL
) {
546 PERROR("malloc struct lttng_consumer_stream");
553 stream
->key
= stream_key
;
555 stream
->out_fd_offset
= 0;
556 stream
->output_written
= 0;
557 stream
->state
= state
;
560 stream
->net_seq_idx
= relayd_id
;
561 stream
->session_id
= session_id
;
562 stream
->monitor
= monitor
;
563 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
564 stream
->index_fd
= -1;
565 pthread_mutex_init(&stream
->lock
, NULL
);
566 pthread_mutex_init(&stream
->metadata_timer_lock
, NULL
);
568 /* If channel is the metadata, flag this stream as metadata. */
569 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
570 stream
->metadata_flag
= 1;
571 /* Metadata is flat out. */
572 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
573 /* Live rendez-vous point. */
574 pthread_cond_init(&stream
->metadata_rdv
, NULL
);
575 pthread_mutex_init(&stream
->metadata_rdv_lock
, NULL
);
577 /* Format stream name to <channel_name>_<cpu_number> */
578 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
581 PERROR("snprintf stream name");
586 /* Key is always the wait_fd for streams. */
587 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
589 /* Init node per channel id key */
590 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
592 /* Init session id node with the stream session id */
593 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
595 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
596 " relayd_id %" PRIu64
", session_id %" PRIu64
,
597 stream
->name
, stream
->key
, channel_key
,
598 stream
->net_seq_idx
, stream
->session_id
);
614 * Add a stream to the global list protected by a mutex.
616 int consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
618 struct lttng_ht
*ht
= data_ht
;
624 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
626 pthread_mutex_lock(&consumer_data
.lock
);
627 pthread_mutex_lock(&stream
->chan
->lock
);
628 pthread_mutex_lock(&stream
->chan
->timer_lock
);
629 pthread_mutex_lock(&stream
->lock
);
632 /* Steal stream identifier to avoid having streams with the same key */
633 steal_stream_key(stream
->key
, ht
);
635 lttng_ht_add_unique_u64(ht
, &stream
->node
);
637 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
638 &stream
->node_channel_id
);
641 * Add stream to the stream_list_ht of the consumer data. No need to steal
642 * the key since the HT does not use it and we allow to add redundant keys
645 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
648 * When nb_init_stream_left reaches 0, we don't need to trigger any action
649 * in terms of destroying the associated channel, because the action that
650 * causes the count to become 0 also causes a stream to be added. The
651 * channel deletion will thus be triggered by the following removal of this
654 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
655 /* Increment refcount before decrementing nb_init_stream_left */
657 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
660 /* Update consumer data once the node is inserted. */
661 consumer_data
.stream_count
++;
662 consumer_data
.need_update
= 1;
665 pthread_mutex_unlock(&stream
->lock
);
666 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
667 pthread_mutex_unlock(&stream
->chan
->lock
);
668 pthread_mutex_unlock(&consumer_data
.lock
);
673 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
675 consumer_del_stream(stream
, data_ht
);
679 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
680 * be acquired before calling this.
682 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
685 struct lttng_ht_node_u64
*node
;
686 struct lttng_ht_iter iter
;
690 lttng_ht_lookup(consumer_data
.relayd_ht
,
691 &relayd
->net_seq_idx
, &iter
);
692 node
= lttng_ht_iter_get_node_u64(&iter
);
696 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
703 * Allocate and return a consumer relayd socket.
705 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
706 uint64_t net_seq_idx
)
708 struct consumer_relayd_sock_pair
*obj
= NULL
;
710 /* net sequence index of -1 is a failure */
711 if (net_seq_idx
== (uint64_t) -1ULL) {
715 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
717 PERROR("zmalloc relayd sock");
721 obj
->net_seq_idx
= net_seq_idx
;
723 obj
->destroy_flag
= 0;
724 obj
->control_sock
.sock
.fd
= -1;
725 obj
->data_sock
.sock
.fd
= -1;
726 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
727 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
734 * Find a relayd socket pair in the global consumer data.
736 * Return the object if found else NULL.
737 * RCU read-side lock must be held across this call and while using the
740 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
742 struct lttng_ht_iter iter
;
743 struct lttng_ht_node_u64
*node
;
744 struct consumer_relayd_sock_pair
*relayd
= NULL
;
746 /* Negative keys are lookup failures */
747 if (key
== (uint64_t) -1ULL) {
751 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
753 node
= lttng_ht_iter_get_node_u64(&iter
);
755 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
763 * Find a relayd and send the stream
765 * Returns 0 on success, < 0 on error
767 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
771 struct consumer_relayd_sock_pair
*relayd
;
774 assert(stream
->net_seq_idx
!= -1ULL);
777 /* The stream is not metadata. Get relayd reference if exists. */
779 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
780 if (relayd
!= NULL
) {
781 /* Add stream on the relayd */
782 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
783 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
784 path
, &stream
->relayd_stream_id
,
785 stream
->chan
->tracefile_size
, stream
->chan
->tracefile_count
);
786 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
791 uatomic_inc(&relayd
->refcount
);
792 stream
->sent_to_relayd
= 1;
794 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
795 stream
->key
, stream
->net_seq_idx
);
800 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
801 stream
->name
, stream
->key
, stream
->net_seq_idx
);
809 * Find a relayd and send the streams sent message
811 * Returns 0 on success, < 0 on error
813 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
816 struct consumer_relayd_sock_pair
*relayd
;
818 assert(net_seq_idx
!= -1ULL);
820 /* The stream is not metadata. Get relayd reference if exists. */
822 relayd
= consumer_find_relayd(net_seq_idx
);
823 if (relayd
!= NULL
) {
824 /* Add stream on the relayd */
825 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
826 ret
= relayd_streams_sent(&relayd
->control_sock
);
827 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
832 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
839 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
847 * Find a relayd and close the stream
849 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
851 struct consumer_relayd_sock_pair
*relayd
;
853 /* The stream is not metadata. Get relayd reference if exists. */
855 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
857 consumer_stream_relayd_close(stream
, relayd
);
863 * Handle stream for relayd transmission if the stream applies for network
864 * streaming where the net sequence index is set.
866 * Return destination file descriptor or negative value on error.
868 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
869 size_t data_size
, unsigned long padding
,
870 struct consumer_relayd_sock_pair
*relayd
)
873 struct lttcomm_relayd_data_hdr data_hdr
;
879 /* Reset data header */
880 memset(&data_hdr
, 0, sizeof(data_hdr
));
882 if (stream
->metadata_flag
) {
883 /* Caller MUST acquire the relayd control socket lock */
884 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
889 /* Metadata are always sent on the control socket. */
890 outfd
= relayd
->control_sock
.sock
.fd
;
892 /* Set header with stream information */
893 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
894 data_hdr
.data_size
= htobe32(data_size
);
895 data_hdr
.padding_size
= htobe32(padding
);
897 * Note that net_seq_num below is assigned with the *current* value of
898 * next_net_seq_num and only after that the next_net_seq_num will be
899 * increment. This is why when issuing a command on the relayd using
900 * this next value, 1 should always be substracted in order to compare
901 * the last seen sequence number on the relayd side to the last sent.
903 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
904 /* Other fields are zeroed previously */
906 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
912 ++stream
->next_net_seq_num
;
914 /* Set to go on data socket */
915 outfd
= relayd
->data_sock
.sock
.fd
;
923 * Allocate and return a new lttng_consumer_channel object using the given key
924 * to initialize the hash table node.
926 * On error, return NULL.
928 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
930 const char *pathname
,
935 enum lttng_event_output output
,
936 uint64_t tracefile_size
,
937 uint64_t tracefile_count
,
938 uint64_t session_id_per_pid
,
939 unsigned int monitor
,
940 unsigned int live_timer_interval
,
941 const char *root_shm_path
,
942 const char *shm_path
)
944 struct lttng_consumer_channel
*channel
;
946 channel
= zmalloc(sizeof(*channel
));
947 if (channel
== NULL
) {
948 PERROR("malloc struct lttng_consumer_channel");
953 channel
->refcount
= 0;
954 channel
->session_id
= session_id
;
955 channel
->session_id_per_pid
= session_id_per_pid
;
958 channel
->relayd_id
= relayd_id
;
959 channel
->tracefile_size
= tracefile_size
;
960 channel
->tracefile_count
= tracefile_count
;
961 channel
->monitor
= monitor
;
962 channel
->live_timer_interval
= live_timer_interval
;
963 pthread_mutex_init(&channel
->lock
, NULL
);
964 pthread_mutex_init(&channel
->timer_lock
, NULL
);
967 case LTTNG_EVENT_SPLICE
:
968 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
970 case LTTNG_EVENT_MMAP
:
971 channel
->output
= CONSUMER_CHANNEL_MMAP
;
981 * In monitor mode, the streams associated with the channel will be put in
982 * a special list ONLY owned by this channel. So, the refcount is set to 1
983 * here meaning that the channel itself has streams that are referenced.
985 * On a channel deletion, once the channel is no longer visible, the
986 * refcount is decremented and checked for a zero value to delete it. With
987 * streams in no monitor mode, it will now be safe to destroy the channel.
989 if (!channel
->monitor
) {
990 channel
->refcount
= 1;
993 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
994 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
996 strncpy(channel
->name
, name
, sizeof(channel
->name
));
997 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1000 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1001 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1004 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1005 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1008 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1010 channel
->wait_fd
= -1;
1012 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1014 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
1021 * Add a channel to the global list protected by a mutex.
1023 * Always return 0 indicating success.
1025 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1026 struct lttng_consumer_local_data
*ctx
)
1028 pthread_mutex_lock(&consumer_data
.lock
);
1029 pthread_mutex_lock(&channel
->lock
);
1030 pthread_mutex_lock(&channel
->timer_lock
);
1033 * This gives us a guarantee that the channel we are about to add to the
1034 * channel hash table will be unique. See this function comment on the why
1035 * we need to steel the channel key at this stage.
1037 steal_channel_key(channel
->key
);
1040 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1043 pthread_mutex_unlock(&channel
->timer_lock
);
1044 pthread_mutex_unlock(&channel
->lock
);
1045 pthread_mutex_unlock(&consumer_data
.lock
);
1047 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1048 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1055 * Allocate the pollfd structure and the local view of the out fds to avoid
1056 * doing a lookup in the linked list and concurrency issues when writing is
1057 * needed. Called with consumer_data.lock held.
1059 * Returns the number of fds in the structures.
1061 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1062 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1063 struct lttng_ht
*ht
)
1066 struct lttng_ht_iter iter
;
1067 struct lttng_consumer_stream
*stream
;
1072 assert(local_stream
);
1074 DBG("Updating poll fd array");
1076 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1078 * Only active streams with an active end point can be added to the
1079 * poll set and local stream storage of the thread.
1081 * There is a potential race here for endpoint_status to be updated
1082 * just after the check. However, this is OK since the stream(s) will
1083 * be deleted once the thread is notified that the end point state has
1084 * changed where this function will be called back again.
1086 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
1087 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1091 * This clobbers way too much the debug output. Uncomment that if you
1092 * need it for debugging purposes.
1094 * DBG("Active FD %d", stream->wait_fd);
1096 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1097 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1098 local_stream
[i
] = stream
;
1104 * Insert the consumer_data_pipe at the end of the array and don't
1105 * increment i so nb_fd is the number of real FD.
1107 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1108 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1110 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1111 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1116 * Poll on the should_quit pipe and the command socket return -1 on
1117 * error, 1 if should exit, 0 if data is available on the command socket
1119 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1124 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1125 if (num_rdy
== -1) {
1127 * Restart interrupted system call.
1129 if (errno
== EINTR
) {
1132 PERROR("Poll error");
1135 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1136 DBG("consumer_should_quit wake up");
1143 * Set the error socket.
1145 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1148 ctx
->consumer_error_socket
= sock
;
1152 * Set the command socket path.
1154 void lttng_consumer_set_command_sock_path(
1155 struct lttng_consumer_local_data
*ctx
, char *sock
)
1157 ctx
->consumer_command_sock_path
= sock
;
1161 * Send return code to the session daemon.
1162 * If the socket is not defined, we return 0, it is not a fatal error
1164 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1166 if (ctx
->consumer_error_socket
> 0) {
1167 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1168 sizeof(enum lttcomm_sessiond_command
));
1175 * Close all the tracefiles and stream fds and MUST be called when all
1176 * instances are destroyed i.e. when all threads were joined and are ended.
1178 void lttng_consumer_cleanup(void)
1180 struct lttng_ht_iter iter
;
1181 struct lttng_consumer_channel
*channel
;
1185 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1187 consumer_del_channel(channel
);
1192 lttng_ht_destroy(consumer_data
.channel_ht
);
1194 cleanup_relayd_ht();
1196 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1199 * This HT contains streams that are freed by either the metadata thread or
1200 * the data thread so we do *nothing* on the hash table and simply destroy
1203 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1207 * Called from signal handler.
1209 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1214 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1216 PERROR("write consumer quit");
1219 DBG("Consumer flag that it should quit");
1222 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1225 int outfd
= stream
->out_fd
;
1228 * This does a blocking write-and-wait on any page that belongs to the
1229 * subbuffer prior to the one we just wrote.
1230 * Don't care about error values, as these are just hints and ways to
1231 * limit the amount of page cache used.
1233 if (orig_offset
< stream
->max_sb_size
) {
1236 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1237 stream
->max_sb_size
,
1238 SYNC_FILE_RANGE_WAIT_BEFORE
1239 | SYNC_FILE_RANGE_WRITE
1240 | SYNC_FILE_RANGE_WAIT_AFTER
);
1242 * Give hints to the kernel about how we access the file:
1243 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1246 * We need to call fadvise again after the file grows because the
1247 * kernel does not seem to apply fadvise to non-existing parts of the
1250 * Call fadvise _after_ having waited for the page writeback to
1251 * complete because the dirty page writeback semantic is not well
1252 * defined. So it can be expected to lead to lower throughput in
1255 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1256 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1260 * Initialise the necessary environnement :
1261 * - create a new context
1262 * - create the poll_pipe
1263 * - create the should_quit pipe (for signal handler)
1264 * - create the thread pipe (for splice)
1266 * Takes a function pointer as argument, this function is called when data is
1267 * available on a buffer. This function is responsible to do the
1268 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1269 * buffer configuration and then kernctl_put_next_subbuf at the end.
1271 * Returns a pointer to the new context or NULL on error.
1273 struct lttng_consumer_local_data
*lttng_consumer_create(
1274 enum lttng_consumer_type type
,
1275 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1276 struct lttng_consumer_local_data
*ctx
),
1277 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1278 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1279 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1282 struct lttng_consumer_local_data
*ctx
;
1284 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1285 consumer_data
.type
== type
);
1286 consumer_data
.type
= type
;
1288 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1290 PERROR("allocating context");
1294 ctx
->consumer_error_socket
= -1;
1295 ctx
->consumer_metadata_socket
= -1;
1296 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1297 /* assign the callbacks */
1298 ctx
->on_buffer_ready
= buffer_ready
;
1299 ctx
->on_recv_channel
= recv_channel
;
1300 ctx
->on_recv_stream
= recv_stream
;
1301 ctx
->on_update_stream
= update_stream
;
1303 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1304 if (!ctx
->consumer_data_pipe
) {
1305 goto error_poll_pipe
;
1308 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1309 if (!ctx
->consumer_wakeup_pipe
) {
1310 goto error_wakeup_pipe
;
1313 ret
= pipe(ctx
->consumer_should_quit
);
1315 PERROR("Error creating recv pipe");
1316 goto error_quit_pipe
;
1319 ret
= pipe(ctx
->consumer_channel_pipe
);
1321 PERROR("Error creating channel pipe");
1322 goto error_channel_pipe
;
1325 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1326 if (!ctx
->consumer_metadata_pipe
) {
1327 goto error_metadata_pipe
;
1332 error_metadata_pipe
:
1333 utils_close_pipe(ctx
->consumer_channel_pipe
);
1335 utils_close_pipe(ctx
->consumer_should_quit
);
1337 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1339 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1347 * Iterate over all streams of the hashtable and free them properly.
1349 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1351 struct lttng_ht_iter iter
;
1352 struct lttng_consumer_stream
*stream
;
1359 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1361 * Ignore return value since we are currently cleaning up so any error
1364 (void) consumer_del_stream(stream
, ht
);
1368 lttng_ht_destroy(ht
);
1372 * Iterate over all streams of the metadata hashtable and free them
1375 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1377 struct lttng_ht_iter iter
;
1378 struct lttng_consumer_stream
*stream
;
1385 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1387 * Ignore return value since we are currently cleaning up so any error
1390 (void) consumer_del_metadata_stream(stream
, ht
);
1394 lttng_ht_destroy(ht
);
1398 * Close all fds associated with the instance and free the context.
1400 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1404 DBG("Consumer destroying it. Closing everything.");
1410 destroy_data_stream_ht(data_ht
);
1411 destroy_metadata_stream_ht(metadata_ht
);
1413 ret
= close(ctx
->consumer_error_socket
);
1417 ret
= close(ctx
->consumer_metadata_socket
);
1421 utils_close_pipe(ctx
->consumer_channel_pipe
);
1422 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1423 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1424 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1425 utils_close_pipe(ctx
->consumer_should_quit
);
1427 unlink(ctx
->consumer_command_sock_path
);
1432 * Write the metadata stream id on the specified file descriptor.
1434 static int write_relayd_metadata_id(int fd
,
1435 struct lttng_consumer_stream
*stream
,
1436 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1439 struct lttcomm_relayd_metadata_payload hdr
;
1441 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1442 hdr
.padding_size
= htobe32(padding
);
1443 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1444 if (ret
< sizeof(hdr
)) {
1446 * This error means that the fd's end is closed so ignore the PERROR
1447 * not to clubber the error output since this can happen in a normal
1450 if (errno
!= EPIPE
) {
1451 PERROR("write metadata stream id");
1453 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1455 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1456 * handle writting the missing part so report that as an error and
1457 * don't lie to the caller.
1462 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1463 stream
->relayd_stream_id
, padding
);
1470 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1471 * core function for writing trace buffers to either the local filesystem or
1474 * It must be called with the stream lock held.
1476 * Careful review MUST be put if any changes occur!
1478 * Returns the number of bytes written
1480 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1481 struct lttng_consumer_local_data
*ctx
,
1482 struct lttng_consumer_stream
*stream
, unsigned long len
,
1483 unsigned long padding
,
1484 struct ctf_packet_index
*index
)
1486 unsigned long mmap_offset
;
1489 off_t orig_offset
= stream
->out_fd_offset
;
1490 /* Default is on the disk */
1491 int outfd
= stream
->out_fd
;
1492 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1493 unsigned int relayd_hang_up
= 0;
1495 /* RCU lock for the relayd pointer */
1498 /* Flag that the current stream if set for network streaming. */
1499 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1500 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1501 if (relayd
== NULL
) {
1507 /* get the offset inside the fd to mmap */
1508 switch (consumer_data
.type
) {
1509 case LTTNG_CONSUMER_KERNEL
:
1510 mmap_base
= stream
->mmap_base
;
1511 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1514 PERROR("tracer ctl get_mmap_read_offset");
1518 case LTTNG_CONSUMER32_UST
:
1519 case LTTNG_CONSUMER64_UST
:
1520 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1522 ERR("read mmap get mmap base for stream %s", stream
->name
);
1526 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1528 PERROR("tracer ctl get_mmap_read_offset");
1534 ERR("Unknown consumer_data type");
1538 /* Handle stream on the relayd if the output is on the network */
1540 unsigned long netlen
= len
;
1543 * Lock the control socket for the complete duration of the function
1544 * since from this point on we will use the socket.
1546 if (stream
->metadata_flag
) {
1547 /* Metadata requires the control socket. */
1548 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1549 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1552 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1557 /* Use the returned socket. */
1560 /* Write metadata stream id before payload */
1561 if (stream
->metadata_flag
) {
1562 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1569 /* No streaming, we have to set the len with the full padding */
1573 * Check if we need to change the tracefile before writing the packet.
1575 if (stream
->chan
->tracefile_size
> 0 &&
1576 (stream
->tracefile_size_current
+ len
) >
1577 stream
->chan
->tracefile_size
) {
1578 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1579 stream
->name
, stream
->chan
->tracefile_size
,
1580 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1581 stream
->out_fd
, &(stream
->tracefile_count_current
),
1584 ERR("Rotating output file");
1587 outfd
= stream
->out_fd
;
1589 if (stream
->index_fd
>= 0) {
1590 ret
= index_create_file(stream
->chan
->pathname
,
1591 stream
->name
, stream
->uid
, stream
->gid
,
1592 stream
->chan
->tracefile_size
,
1593 stream
->tracefile_count_current
);
1597 stream
->index_fd
= ret
;
1600 /* Reset current size because we just perform a rotation. */
1601 stream
->tracefile_size_current
= 0;
1602 stream
->out_fd_offset
= 0;
1605 stream
->tracefile_size_current
+= len
;
1607 index
->offset
= htobe64(stream
->out_fd_offset
);
1612 * This call guarantee that len or less is returned. It's impossible to
1613 * receive a ret value that is bigger than len.
1615 ret
= lttng_write(outfd
, mmap_base
+ mmap_offset
, len
);
1616 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1617 if (ret
< 0 || ((size_t) ret
!= len
)) {
1619 * Report error to caller if nothing was written else at least send the
1627 /* Socket operation failed. We consider the relayd dead */
1628 if (errno
== EPIPE
|| errno
== EINVAL
|| errno
== EBADF
) {
1630 * This is possible if the fd is closed on the other side
1631 * (outfd) or any write problem. It can be verbose a bit for a
1632 * normal execution if for instance the relayd is stopped
1633 * abruptly. This can happen so set this to a DBG statement.
1635 DBG("Consumer mmap write detected relayd hang up");
1637 /* Unhandled error, print it and stop function right now. */
1638 PERROR("Error in write mmap (ret %zd != len %lu)", ret
, len
);
1642 stream
->output_written
+= ret
;
1644 /* This call is useless on a socket so better save a syscall. */
1646 /* This won't block, but will start writeout asynchronously */
1647 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, len
,
1648 SYNC_FILE_RANGE_WRITE
);
1649 stream
->out_fd_offset
+= len
;
1651 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1655 * This is a special case that the relayd has closed its socket. Let's
1656 * cleanup the relayd object and all associated streams.
1658 if (relayd
&& relayd_hang_up
) {
1659 cleanup_relayd(relayd
, ctx
);
1663 /* Unlock only if ctrl socket used */
1664 if (relayd
&& stream
->metadata_flag
) {
1665 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1673 * Splice the data from the ring buffer to the tracefile.
1675 * It must be called with the stream lock held.
1677 * Returns the number of bytes spliced.
1679 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1680 struct lttng_consumer_local_data
*ctx
,
1681 struct lttng_consumer_stream
*stream
, unsigned long len
,
1682 unsigned long padding
,
1683 struct ctf_packet_index
*index
)
1685 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1687 off_t orig_offset
= stream
->out_fd_offset
;
1688 int fd
= stream
->wait_fd
;
1689 /* Default is on the disk */
1690 int outfd
= stream
->out_fd
;
1691 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1693 unsigned int relayd_hang_up
= 0;
1695 switch (consumer_data
.type
) {
1696 case LTTNG_CONSUMER_KERNEL
:
1698 case LTTNG_CONSUMER32_UST
:
1699 case LTTNG_CONSUMER64_UST
:
1700 /* Not supported for user space tracing */
1703 ERR("Unknown consumer_data type");
1707 /* RCU lock for the relayd pointer */
1710 /* Flag that the current stream if set for network streaming. */
1711 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1712 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1713 if (relayd
== NULL
) {
1718 splice_pipe
= stream
->splice_pipe
;
1720 /* Write metadata stream id before payload */
1722 unsigned long total_len
= len
;
1724 if (stream
->metadata_flag
) {
1726 * Lock the control socket for the complete duration of the function
1727 * since from this point on we will use the socket.
1729 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1731 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1739 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1742 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1748 /* Use the returned socket. */
1751 /* No streaming, we have to set the len with the full padding */
1755 * Check if we need to change the tracefile before writing the packet.
1757 if (stream
->chan
->tracefile_size
> 0 &&
1758 (stream
->tracefile_size_current
+ len
) >
1759 stream
->chan
->tracefile_size
) {
1760 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1761 stream
->name
, stream
->chan
->tracefile_size
,
1762 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1763 stream
->out_fd
, &(stream
->tracefile_count_current
),
1767 ERR("Rotating output file");
1770 outfd
= stream
->out_fd
;
1772 if (stream
->index_fd
>= 0) {
1773 ret
= index_create_file(stream
->chan
->pathname
,
1774 stream
->name
, stream
->uid
, stream
->gid
,
1775 stream
->chan
->tracefile_size
,
1776 stream
->tracefile_count_current
);
1781 stream
->index_fd
= ret
;
1784 /* Reset current size because we just perform a rotation. */
1785 stream
->tracefile_size_current
= 0;
1786 stream
->out_fd_offset
= 0;
1789 stream
->tracefile_size_current
+= len
;
1790 index
->offset
= htobe64(stream
->out_fd_offset
);
1794 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1795 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1796 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1797 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1798 DBG("splice chan to pipe, ret %zd", ret_splice
);
1799 if (ret_splice
< 0) {
1802 PERROR("Error in relay splice");
1806 /* Handle stream on the relayd if the output is on the network */
1807 if (relayd
&& stream
->metadata_flag
) {
1808 size_t metadata_payload_size
=
1809 sizeof(struct lttcomm_relayd_metadata_payload
);
1811 /* Update counter to fit the spliced data */
1812 ret_splice
+= metadata_payload_size
;
1813 len
+= metadata_payload_size
;
1815 * We do this so the return value can match the len passed as
1816 * argument to this function.
1818 written
-= metadata_payload_size
;
1821 /* Splice data out */
1822 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1823 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1824 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1826 if (ret_splice
< 0) {
1831 } else if (ret_splice
> len
) {
1833 * We don't expect this code path to be executed but you never know
1834 * so this is an extra protection agains a buggy splice().
1837 written
+= ret_splice
;
1838 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1842 /* All good, update current len and continue. */
1846 /* This call is useless on a socket so better save a syscall. */
1848 /* This won't block, but will start writeout asynchronously */
1849 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1850 SYNC_FILE_RANGE_WRITE
);
1851 stream
->out_fd_offset
+= ret_splice
;
1853 stream
->output_written
+= ret_splice
;
1854 written
+= ret_splice
;
1856 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1861 * This is a special case that the relayd has closed its socket. Let's
1862 * cleanup the relayd object and all associated streams.
1864 if (relayd
&& relayd_hang_up
) {
1865 cleanup_relayd(relayd
, ctx
);
1866 /* Skip splice error so the consumer does not fail */
1871 /* send the appropriate error description to sessiond */
1874 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1877 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1880 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1885 if (relayd
&& stream
->metadata_flag
) {
1886 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1894 * Take a snapshot for a specific fd
1896 * Returns 0 on success, < 0 on error
1898 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1900 switch (consumer_data
.type
) {
1901 case LTTNG_CONSUMER_KERNEL
:
1902 return lttng_kconsumer_take_snapshot(stream
);
1903 case LTTNG_CONSUMER32_UST
:
1904 case LTTNG_CONSUMER64_UST
:
1905 return lttng_ustconsumer_take_snapshot(stream
);
1907 ERR("Unknown consumer_data type");
1914 * Get the produced position
1916 * Returns 0 on success, < 0 on error
1918 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1921 switch (consumer_data
.type
) {
1922 case LTTNG_CONSUMER_KERNEL
:
1923 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1924 case LTTNG_CONSUMER32_UST
:
1925 case LTTNG_CONSUMER64_UST
:
1926 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1928 ERR("Unknown consumer_data type");
1934 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1935 int sock
, struct pollfd
*consumer_sockpoll
)
1937 switch (consumer_data
.type
) {
1938 case LTTNG_CONSUMER_KERNEL
:
1939 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1940 case LTTNG_CONSUMER32_UST
:
1941 case LTTNG_CONSUMER64_UST
:
1942 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1944 ERR("Unknown consumer_data type");
1950 void lttng_consumer_close_all_metadata(void)
1952 switch (consumer_data
.type
) {
1953 case LTTNG_CONSUMER_KERNEL
:
1955 * The Kernel consumer has a different metadata scheme so we don't
1956 * close anything because the stream will be closed by the session
1960 case LTTNG_CONSUMER32_UST
:
1961 case LTTNG_CONSUMER64_UST
:
1963 * Close all metadata streams. The metadata hash table is passed and
1964 * this call iterates over it by closing all wakeup fd. This is safe
1965 * because at this point we are sure that the metadata producer is
1966 * either dead or blocked.
1968 lttng_ustconsumer_close_all_metadata(metadata_ht
);
1971 ERR("Unknown consumer_data type");
1977 * Clean up a metadata stream and free its memory.
1979 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1980 struct lttng_ht
*ht
)
1982 struct lttng_consumer_channel
*free_chan
= NULL
;
1986 * This call should NEVER receive regular stream. It must always be
1987 * metadata stream and this is crucial for data structure synchronization.
1989 assert(stream
->metadata_flag
);
1991 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1993 pthread_mutex_lock(&consumer_data
.lock
);
1994 pthread_mutex_lock(&stream
->chan
->lock
);
1995 pthread_mutex_lock(&stream
->lock
);
1997 /* Remove any reference to that stream. */
1998 consumer_stream_delete(stream
, ht
);
2000 /* Close down everything including the relayd if one. */
2001 consumer_stream_close(stream
);
2002 /* Destroy tracer buffers of the stream. */
2003 consumer_stream_destroy_buffers(stream
);
2005 /* Atomically decrement channel refcount since other threads can use it. */
2006 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
2007 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
2008 /* Go for channel deletion! */
2009 free_chan
= stream
->chan
;
2013 * Nullify the stream reference so it is not used after deletion. The
2014 * channel lock MUST be acquired before being able to check for a NULL
2017 stream
->chan
->metadata_stream
= NULL
;
2019 pthread_mutex_unlock(&stream
->lock
);
2020 pthread_mutex_unlock(&stream
->chan
->lock
);
2021 pthread_mutex_unlock(&consumer_data
.lock
);
2024 consumer_del_channel(free_chan
);
2027 consumer_stream_free(stream
);
2031 * Action done with the metadata stream when adding it to the consumer internal
2032 * data structures to handle it.
2034 int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2036 struct lttng_ht
*ht
= metadata_ht
;
2038 struct lttng_ht_iter iter
;
2039 struct lttng_ht_node_u64
*node
;
2044 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2046 pthread_mutex_lock(&consumer_data
.lock
);
2047 pthread_mutex_lock(&stream
->chan
->lock
);
2048 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2049 pthread_mutex_lock(&stream
->lock
);
2052 * From here, refcounts are updated so be _careful_ when returning an error
2059 * Lookup the stream just to make sure it does not exist in our internal
2060 * state. This should NEVER happen.
2062 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2063 node
= lttng_ht_iter_get_node_u64(&iter
);
2067 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2068 * in terms of destroying the associated channel, because the action that
2069 * causes the count to become 0 also causes a stream to be added. The
2070 * channel deletion will thus be triggered by the following removal of this
2073 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2074 /* Increment refcount before decrementing nb_init_stream_left */
2076 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2079 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2081 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2082 &stream
->node_channel_id
);
2085 * Add stream to the stream_list_ht of the consumer data. No need to steal
2086 * the key since the HT does not use it and we allow to add redundant keys
2089 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2093 pthread_mutex_unlock(&stream
->lock
);
2094 pthread_mutex_unlock(&stream
->chan
->lock
);
2095 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2096 pthread_mutex_unlock(&consumer_data
.lock
);
2101 * Delete data stream that are flagged for deletion (endpoint_status).
2103 static void validate_endpoint_status_data_stream(void)
2105 struct lttng_ht_iter iter
;
2106 struct lttng_consumer_stream
*stream
;
2108 DBG("Consumer delete flagged data stream");
2111 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2112 /* Validate delete flag of the stream */
2113 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2116 /* Delete it right now */
2117 consumer_del_stream(stream
, data_ht
);
2123 * Delete metadata stream that are flagged for deletion (endpoint_status).
2125 static void validate_endpoint_status_metadata_stream(
2126 struct lttng_poll_event
*pollset
)
2128 struct lttng_ht_iter iter
;
2129 struct lttng_consumer_stream
*stream
;
2131 DBG("Consumer delete flagged metadata stream");
2136 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2137 /* Validate delete flag of the stream */
2138 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2142 * Remove from pollset so the metadata thread can continue without
2143 * blocking on a deleted stream.
2145 lttng_poll_del(pollset
, stream
->wait_fd
);
2147 /* Delete it right now */
2148 consumer_del_metadata_stream(stream
, metadata_ht
);
2154 * Thread polls on metadata file descriptor and write them on disk or on the
2157 void *consumer_thread_metadata_poll(void *data
)
2159 int ret
, i
, pollfd
, err
= -1;
2160 uint32_t revents
, nb_fd
;
2161 struct lttng_consumer_stream
*stream
= NULL
;
2162 struct lttng_ht_iter iter
;
2163 struct lttng_ht_node_u64
*node
;
2164 struct lttng_poll_event events
;
2165 struct lttng_consumer_local_data
*ctx
= data
;
2168 rcu_register_thread();
2170 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2172 if (testpoint(consumerd_thread_metadata
)) {
2173 goto error_testpoint
;
2176 health_code_update();
2178 DBG("Thread metadata poll started");
2180 /* Size is set to 1 for the consumer_metadata pipe */
2181 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2183 ERR("Poll set creation failed");
2187 ret
= lttng_poll_add(&events
,
2188 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2194 DBG("Metadata main loop started");
2198 health_code_update();
2199 health_poll_entry();
2200 DBG("Metadata poll wait");
2201 ret
= lttng_poll_wait(&events
, -1);
2202 DBG("Metadata poll return from wait with %d fd(s)",
2203 LTTNG_POLL_GETNB(&events
));
2205 DBG("Metadata event catched in thread");
2207 if (errno
== EINTR
) {
2208 ERR("Poll EINTR catched");
2211 if (LTTNG_POLL_GETNB(&events
) == 0) {
2212 err
= 0; /* All is OK */
2219 /* From here, the event is a metadata wait fd */
2220 for (i
= 0; i
< nb_fd
; i
++) {
2221 health_code_update();
2223 revents
= LTTNG_POLL_GETEV(&events
, i
);
2224 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2227 /* No activity for this FD (poll implementation). */
2231 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2232 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2233 DBG("Metadata thread pipe hung up");
2235 * Remove the pipe from the poll set and continue the loop
2236 * since their might be data to consume.
2238 lttng_poll_del(&events
,
2239 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2240 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2242 } else if (revents
& LPOLLIN
) {
2245 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2246 &stream
, sizeof(stream
));
2247 if (pipe_len
< sizeof(stream
)) {
2248 PERROR("read metadata stream");
2250 * Continue here to handle the rest of the streams.
2255 /* A NULL stream means that the state has changed. */
2256 if (stream
== NULL
) {
2257 /* Check for deleted streams. */
2258 validate_endpoint_status_metadata_stream(&events
);
2262 DBG("Adding metadata stream %d to poll set",
2265 /* Add metadata stream to the global poll events list */
2266 lttng_poll_add(&events
, stream
->wait_fd
,
2267 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2270 /* Handle other stream */
2276 uint64_t tmp_id
= (uint64_t) pollfd
;
2278 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2280 node
= lttng_ht_iter_get_node_u64(&iter
);
2283 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2286 /* Check for error event */
2287 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2288 DBG("Metadata fd %d is hup|err.", pollfd
);
2289 if (!stream
->hangup_flush_done
2290 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2291 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2292 DBG("Attempting to flush and consume the UST buffers");
2293 lttng_ustconsumer_on_stream_hangup(stream
);
2295 /* We just flushed the stream now read it. */
2297 health_code_update();
2299 len
= ctx
->on_buffer_ready(stream
, ctx
);
2301 * We don't check the return value here since if we get
2302 * a negative len, it means an error occured thus we
2303 * simply remove it from the poll set and free the
2309 lttng_poll_del(&events
, stream
->wait_fd
);
2311 * This call update the channel states, closes file descriptors
2312 * and securely free the stream.
2314 consumer_del_metadata_stream(stream
, metadata_ht
);
2315 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2316 /* Get the data out of the metadata file descriptor */
2317 DBG("Metadata available on fd %d", pollfd
);
2318 assert(stream
->wait_fd
== pollfd
);
2321 health_code_update();
2323 len
= ctx
->on_buffer_ready(stream
, ctx
);
2325 * We don't check the return value here since if we get
2326 * a negative len, it means an error occured thus we
2327 * simply remove it from the poll set and free the
2332 /* It's ok to have an unavailable sub-buffer */
2333 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2334 /* Clean up stream from consumer and free it. */
2335 lttng_poll_del(&events
, stream
->wait_fd
);
2336 consumer_del_metadata_stream(stream
, metadata_ht
);
2340 /* Release RCU lock for the stream looked up */
2348 DBG("Metadata poll thread exiting");
2350 lttng_poll_clean(&events
);
2355 ERR("Health error occurred in %s", __func__
);
2357 health_unregister(health_consumerd
);
2358 rcu_unregister_thread();
2363 * This thread polls the fds in the set to consume the data and write
2364 * it to tracefile if necessary.
2366 void *consumer_thread_data_poll(void *data
)
2368 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2369 struct pollfd
*pollfd
= NULL
;
2370 /* local view of the streams */
2371 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2372 /* local view of consumer_data.fds_count */
2374 struct lttng_consumer_local_data
*ctx
= data
;
2377 rcu_register_thread();
2379 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2381 if (testpoint(consumerd_thread_data
)) {
2382 goto error_testpoint
;
2385 health_code_update();
2387 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2388 if (local_stream
== NULL
) {
2389 PERROR("local_stream malloc");
2394 health_code_update();
2400 * the fds set has been updated, we need to update our
2401 * local array as well
2403 pthread_mutex_lock(&consumer_data
.lock
);
2404 if (consumer_data
.need_update
) {
2409 local_stream
= NULL
;
2412 * Allocate for all fds +1 for the consumer_data_pipe and +1 for
2415 pollfd
= zmalloc((consumer_data
.stream_count
+ 2) * sizeof(struct pollfd
));
2416 if (pollfd
== NULL
) {
2417 PERROR("pollfd malloc");
2418 pthread_mutex_unlock(&consumer_data
.lock
);
2422 local_stream
= zmalloc((consumer_data
.stream_count
+ 2) *
2423 sizeof(struct lttng_consumer_stream
*));
2424 if (local_stream
== NULL
) {
2425 PERROR("local_stream malloc");
2426 pthread_mutex_unlock(&consumer_data
.lock
);
2429 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2432 ERR("Error in allocating pollfd or local_outfds");
2433 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2434 pthread_mutex_unlock(&consumer_data
.lock
);
2438 consumer_data
.need_update
= 0;
2440 pthread_mutex_unlock(&consumer_data
.lock
);
2442 /* No FDs and consumer_quit, consumer_cleanup the thread */
2443 if (nb_fd
== 0 && consumer_quit
== 1) {
2444 err
= 0; /* All is OK */
2447 /* poll on the array of fds */
2449 DBG("polling on %d fd", nb_fd
+ 2);
2450 health_poll_entry();
2451 num_rdy
= poll(pollfd
, nb_fd
+ 2, -1);
2453 DBG("poll num_rdy : %d", num_rdy
);
2454 if (num_rdy
== -1) {
2456 * Restart interrupted system call.
2458 if (errno
== EINTR
) {
2461 PERROR("Poll error");
2462 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2464 } else if (num_rdy
== 0) {
2465 DBG("Polling thread timed out");
2470 * If the consumer_data_pipe triggered poll go directly to the
2471 * beginning of the loop to update the array. We want to prioritize
2472 * array update over low-priority reads.
2474 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2475 ssize_t pipe_readlen
;
2477 DBG("consumer_data_pipe wake up");
2478 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2479 &new_stream
, sizeof(new_stream
));
2480 if (pipe_readlen
< sizeof(new_stream
)) {
2481 PERROR("Consumer data pipe");
2482 /* Continue so we can at least handle the current stream(s). */
2487 * If the stream is NULL, just ignore it. It's also possible that
2488 * the sessiond poll thread changed the consumer_quit state and is
2489 * waking us up to test it.
2491 if (new_stream
== NULL
) {
2492 validate_endpoint_status_data_stream();
2496 /* Continue to update the local streams and handle prio ones */
2500 /* Handle wakeup pipe. */
2501 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2503 ssize_t pipe_readlen
;
2505 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2507 if (pipe_readlen
< 0) {
2508 PERROR("Consumer data wakeup pipe");
2510 /* We've been awakened to handle stream(s). */
2511 ctx
->has_wakeup
= 0;
2514 /* Take care of high priority channels first. */
2515 for (i
= 0; i
< nb_fd
; i
++) {
2516 health_code_update();
2518 if (local_stream
[i
] == NULL
) {
2521 if (pollfd
[i
].revents
& POLLPRI
) {
2522 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2524 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2525 /* it's ok to have an unavailable sub-buffer */
2526 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2527 /* Clean the stream and free it. */
2528 consumer_del_stream(local_stream
[i
], data_ht
);
2529 local_stream
[i
] = NULL
;
2530 } else if (len
> 0) {
2531 local_stream
[i
]->data_read
= 1;
2537 * If we read high prio channel in this loop, try again
2538 * for more high prio data.
2544 /* Take care of low priority channels. */
2545 for (i
= 0; i
< nb_fd
; i
++) {
2546 health_code_update();
2548 if (local_stream
[i
] == NULL
) {
2551 if ((pollfd
[i
].revents
& POLLIN
) ||
2552 local_stream
[i
]->hangup_flush_done
||
2553 local_stream
[i
]->has_data
) {
2554 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2555 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2556 /* it's ok to have an unavailable sub-buffer */
2557 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2558 /* Clean the stream and free it. */
2559 consumer_del_stream(local_stream
[i
], data_ht
);
2560 local_stream
[i
] = NULL
;
2561 } else if (len
> 0) {
2562 local_stream
[i
]->data_read
= 1;
2567 /* Handle hangup and errors */
2568 for (i
= 0; i
< nb_fd
; i
++) {
2569 health_code_update();
2571 if (local_stream
[i
] == NULL
) {
2574 if (!local_stream
[i
]->hangup_flush_done
2575 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2576 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2577 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2578 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2580 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2581 /* Attempt read again, for the data we just flushed. */
2582 local_stream
[i
]->data_read
= 1;
2585 * If the poll flag is HUP/ERR/NVAL and we have
2586 * read no data in this pass, we can remove the
2587 * stream from its hash table.
2589 if ((pollfd
[i
].revents
& POLLHUP
)) {
2590 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2591 if (!local_stream
[i
]->data_read
) {
2592 consumer_del_stream(local_stream
[i
], data_ht
);
2593 local_stream
[i
] = NULL
;
2596 } else if (pollfd
[i
].revents
& POLLERR
) {
2597 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2598 if (!local_stream
[i
]->data_read
) {
2599 consumer_del_stream(local_stream
[i
], data_ht
);
2600 local_stream
[i
] = NULL
;
2603 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2604 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2605 if (!local_stream
[i
]->data_read
) {
2606 consumer_del_stream(local_stream
[i
], data_ht
);
2607 local_stream
[i
] = NULL
;
2611 if (local_stream
[i
] != NULL
) {
2612 local_stream
[i
]->data_read
= 0;
2619 DBG("polling thread exiting");
2624 * Close the write side of the pipe so epoll_wait() in
2625 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2626 * read side of the pipe. If we close them both, epoll_wait strangely does
2627 * not return and could create a endless wait period if the pipe is the
2628 * only tracked fd in the poll set. The thread will take care of closing
2631 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2636 ERR("Health error occurred in %s", __func__
);
2638 health_unregister(health_consumerd
);
2640 rcu_unregister_thread();
2645 * Close wake-up end of each stream belonging to the channel. This will
2646 * allow the poll() on the stream read-side to detect when the
2647 * write-side (application) finally closes them.
2650 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2652 struct lttng_ht
*ht
;
2653 struct lttng_consumer_stream
*stream
;
2654 struct lttng_ht_iter iter
;
2656 ht
= consumer_data
.stream_per_chan_id_ht
;
2659 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2660 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2661 ht
->match_fct
, &channel
->key
,
2662 &iter
.iter
, stream
, node_channel_id
.node
) {
2664 * Protect against teardown with mutex.
2666 pthread_mutex_lock(&stream
->lock
);
2667 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2670 switch (consumer_data
.type
) {
2671 case LTTNG_CONSUMER_KERNEL
:
2673 case LTTNG_CONSUMER32_UST
:
2674 case LTTNG_CONSUMER64_UST
:
2675 if (stream
->metadata_flag
) {
2676 /* Safe and protected by the stream lock. */
2677 lttng_ustconsumer_close_metadata(stream
->chan
);
2680 * Note: a mutex is taken internally within
2681 * liblttng-ust-ctl to protect timer wakeup_fd
2682 * use from concurrent close.
2684 lttng_ustconsumer_close_stream_wakeup(stream
);
2688 ERR("Unknown consumer_data type");
2692 pthread_mutex_unlock(&stream
->lock
);
2697 static void destroy_channel_ht(struct lttng_ht
*ht
)
2699 struct lttng_ht_iter iter
;
2700 struct lttng_consumer_channel
*channel
;
2708 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2709 ret
= lttng_ht_del(ht
, &iter
);
2714 lttng_ht_destroy(ht
);
2718 * This thread polls the channel fds to detect when they are being
2719 * closed. It closes all related streams if the channel is detected as
2720 * closed. It is currently only used as a shim layer for UST because the
2721 * consumerd needs to keep the per-stream wakeup end of pipes open for
2724 void *consumer_thread_channel_poll(void *data
)
2726 int ret
, i
, pollfd
, err
= -1;
2727 uint32_t revents
, nb_fd
;
2728 struct lttng_consumer_channel
*chan
= NULL
;
2729 struct lttng_ht_iter iter
;
2730 struct lttng_ht_node_u64
*node
;
2731 struct lttng_poll_event events
;
2732 struct lttng_consumer_local_data
*ctx
= data
;
2733 struct lttng_ht
*channel_ht
;
2735 rcu_register_thread();
2737 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2739 if (testpoint(consumerd_thread_channel
)) {
2740 goto error_testpoint
;
2743 health_code_update();
2745 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2747 /* ENOMEM at this point. Better to bail out. */
2751 DBG("Thread channel poll started");
2753 /* Size is set to 1 for the consumer_channel pipe */
2754 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2756 ERR("Poll set creation failed");
2760 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2766 DBG("Channel main loop started");
2770 health_code_update();
2771 DBG("Channel poll wait");
2772 health_poll_entry();
2773 ret
= lttng_poll_wait(&events
, -1);
2774 DBG("Channel poll return from wait with %d fd(s)",
2775 LTTNG_POLL_GETNB(&events
));
2777 DBG("Channel event catched in thread");
2779 if (errno
== EINTR
) {
2780 ERR("Poll EINTR catched");
2783 if (LTTNG_POLL_GETNB(&events
) == 0) {
2784 err
= 0; /* All is OK */
2791 /* From here, the event is a channel wait fd */
2792 for (i
= 0; i
< nb_fd
; i
++) {
2793 health_code_update();
2795 revents
= LTTNG_POLL_GETEV(&events
, i
);
2796 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2799 /* No activity for this FD (poll implementation). */
2803 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2804 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2805 DBG("Channel thread pipe hung up");
2807 * Remove the pipe from the poll set and continue the loop
2808 * since their might be data to consume.
2810 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2812 } else if (revents
& LPOLLIN
) {
2813 enum consumer_channel_action action
;
2816 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2818 ERR("Error reading channel pipe");
2823 case CONSUMER_CHANNEL_ADD
:
2824 DBG("Adding channel %d to poll set",
2827 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2830 lttng_ht_add_unique_u64(channel_ht
,
2831 &chan
->wait_fd_node
);
2833 /* Add channel to the global poll events list */
2834 lttng_poll_add(&events
, chan
->wait_fd
,
2835 LPOLLIN
| LPOLLPRI
);
2837 case CONSUMER_CHANNEL_DEL
:
2840 * This command should never be called if the channel
2841 * has streams monitored by either the data or metadata
2842 * thread. The consumer only notify this thread with a
2843 * channel del. command if it receives a destroy
2844 * channel command from the session daemon that send it
2845 * if a command prior to the GET_CHANNEL failed.
2849 chan
= consumer_find_channel(key
);
2852 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2855 lttng_poll_del(&events
, chan
->wait_fd
);
2856 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2857 ret
= lttng_ht_del(channel_ht
, &iter
);
2860 switch (consumer_data
.type
) {
2861 case LTTNG_CONSUMER_KERNEL
:
2863 case LTTNG_CONSUMER32_UST
:
2864 case LTTNG_CONSUMER64_UST
:
2865 health_code_update();
2866 /* Destroy streams that might have been left in the stream list. */
2867 clean_channel_stream_list(chan
);
2870 ERR("Unknown consumer_data type");
2875 * Release our own refcount. Force channel deletion even if
2876 * streams were not initialized.
2878 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2879 consumer_del_channel(chan
);
2884 case CONSUMER_CHANNEL_QUIT
:
2886 * Remove the pipe from the poll set and continue the loop
2887 * since their might be data to consume.
2889 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2892 ERR("Unknown action");
2897 /* Handle other stream */
2903 uint64_t tmp_id
= (uint64_t) pollfd
;
2905 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2907 node
= lttng_ht_iter_get_node_u64(&iter
);
2910 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2913 /* Check for error event */
2914 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2915 DBG("Channel fd %d is hup|err.", pollfd
);
2917 lttng_poll_del(&events
, chan
->wait_fd
);
2918 ret
= lttng_ht_del(channel_ht
, &iter
);
2922 * This will close the wait fd for each stream associated to
2923 * this channel AND monitored by the data/metadata thread thus
2924 * will be clean by the right thread.
2926 consumer_close_channel_streams(chan
);
2928 /* Release our own refcount */
2929 if (!uatomic_sub_return(&chan
->refcount
, 1)
2930 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2931 consumer_del_channel(chan
);
2935 /* Release RCU lock for the channel looked up */
2943 lttng_poll_clean(&events
);
2945 destroy_channel_ht(channel_ht
);
2948 DBG("Channel poll thread exiting");
2951 ERR("Health error occurred in %s", __func__
);
2953 health_unregister(health_consumerd
);
2954 rcu_unregister_thread();
2958 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2959 struct pollfd
*sockpoll
, int client_socket
)
2966 ret
= lttng_consumer_poll_socket(sockpoll
);
2970 DBG("Metadata connection on client_socket");
2972 /* Blocking call, waiting for transmission */
2973 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2974 if (ctx
->consumer_metadata_socket
< 0) {
2975 WARN("On accept metadata");
2986 * This thread listens on the consumerd socket and receives the file
2987 * descriptors from the session daemon.
2989 void *consumer_thread_sessiond_poll(void *data
)
2991 int sock
= -1, client_socket
, ret
, err
= -1;
2993 * structure to poll for incoming data on communication socket avoids
2994 * making blocking sockets.
2996 struct pollfd consumer_sockpoll
[2];
2997 struct lttng_consumer_local_data
*ctx
= data
;
2999 rcu_register_thread();
3001 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3003 if (testpoint(consumerd_thread_sessiond
)) {
3004 goto error_testpoint
;
3007 health_code_update();
3009 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3010 unlink(ctx
->consumer_command_sock_path
);
3011 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3012 if (client_socket
< 0) {
3013 ERR("Cannot create command socket");
3017 ret
= lttcomm_listen_unix_sock(client_socket
);
3022 DBG("Sending ready command to lttng-sessiond");
3023 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3024 /* return < 0 on error, but == 0 is not fatal */
3026 ERR("Error sending ready command to lttng-sessiond");
3030 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3031 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3032 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3033 consumer_sockpoll
[1].fd
= client_socket
;
3034 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3036 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3044 DBG("Connection on client_socket");
3046 /* Blocking call, waiting for transmission */
3047 sock
= lttcomm_accept_unix_sock(client_socket
);
3054 * Setup metadata socket which is the second socket connection on the
3055 * command unix socket.
3057 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3066 /* This socket is not useful anymore. */
3067 ret
= close(client_socket
);
3069 PERROR("close client_socket");
3073 /* update the polling structure to poll on the established socket */
3074 consumer_sockpoll
[1].fd
= sock
;
3075 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3078 health_code_update();
3080 health_poll_entry();
3081 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3090 DBG("Incoming command on sock");
3091 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3094 * This could simply be a session daemon quitting. Don't output
3097 DBG("Communication interrupted on command socket");
3101 if (consumer_quit
) {
3102 DBG("consumer_thread_receive_fds received quit from signal");
3103 err
= 0; /* All is OK */
3106 DBG("received command on sock");
3112 DBG("Consumer thread sessiond poll exiting");
3115 * Close metadata streams since the producer is the session daemon which
3118 * NOTE: for now, this only applies to the UST tracer.
3120 lttng_consumer_close_all_metadata();
3123 * when all fds have hung up, the polling thread
3129 * Notify the data poll thread to poll back again and test the
3130 * consumer_quit state that we just set so to quit gracefully.
3132 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3134 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3136 notify_health_quit_pipe(health_quit_pipe
);
3138 /* Cleaning up possibly open sockets. */
3142 PERROR("close sock sessiond poll");
3145 if (client_socket
>= 0) {
3146 ret
= close(client_socket
);
3148 PERROR("close client_socket sessiond poll");
3155 ERR("Health error occurred in %s", __func__
);
3157 health_unregister(health_consumerd
);
3159 rcu_unregister_thread();
3163 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3164 struct lttng_consumer_local_data
*ctx
)
3168 pthread_mutex_lock(&stream
->lock
);
3169 if (stream
->metadata_flag
) {
3170 pthread_mutex_lock(&stream
->metadata_rdv_lock
);
3173 switch (consumer_data
.type
) {
3174 case LTTNG_CONSUMER_KERNEL
:
3175 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3177 case LTTNG_CONSUMER32_UST
:
3178 case LTTNG_CONSUMER64_UST
:
3179 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3182 ERR("Unknown consumer_data type");
3188 if (stream
->metadata_flag
) {
3189 pthread_cond_broadcast(&stream
->metadata_rdv
);
3190 pthread_mutex_unlock(&stream
->metadata_rdv_lock
);
3192 pthread_mutex_unlock(&stream
->lock
);
3196 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3198 switch (consumer_data
.type
) {
3199 case LTTNG_CONSUMER_KERNEL
:
3200 return lttng_kconsumer_on_recv_stream(stream
);
3201 case LTTNG_CONSUMER32_UST
:
3202 case LTTNG_CONSUMER64_UST
:
3203 return lttng_ustconsumer_on_recv_stream(stream
);
3205 ERR("Unknown consumer_data type");
3212 * Allocate and set consumer data hash tables.
3214 int lttng_consumer_init(void)
3216 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3217 if (!consumer_data
.channel_ht
) {
3221 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3222 if (!consumer_data
.relayd_ht
) {
3226 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3227 if (!consumer_data
.stream_list_ht
) {
3231 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3232 if (!consumer_data
.stream_per_chan_id_ht
) {
3236 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3241 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3253 * Process the ADD_RELAYD command receive by a consumer.
3255 * This will create a relayd socket pair and add it to the relayd hash table.
3256 * The caller MUST acquire a RCU read side lock before calling it.
3258 int consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3259 struct lttng_consumer_local_data
*ctx
, int sock
,
3260 struct pollfd
*consumer_sockpoll
,
3261 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3262 uint64_t relayd_session_id
)
3264 int fd
= -1, ret
= -1, relayd_created
= 0;
3265 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3266 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3269 assert(relayd_sock
);
3271 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3273 /* Get relayd reference if exists. */
3274 relayd
= consumer_find_relayd(net_seq_idx
);
3275 if (relayd
== NULL
) {
3276 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3277 /* Not found. Allocate one. */
3278 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3279 if (relayd
== NULL
) {
3281 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3284 relayd
->sessiond_session_id
= sessiond_id
;
3289 * This code path MUST continue to the consumer send status message to
3290 * we can notify the session daemon and continue our work without
3291 * killing everything.
3295 * relayd key should never be found for control socket.
3297 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3300 /* First send a status message before receiving the fds. */
3301 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3303 /* Somehow, the session daemon is not responding anymore. */
3304 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3305 goto error_nosignal
;
3308 /* Poll on consumer socket. */
3309 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3311 /* Needing to exit in the middle of a command: error. */
3312 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3314 goto error_nosignal
;
3317 /* Get relayd socket from session daemon */
3318 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3319 if (ret
!= sizeof(fd
)) {
3321 fd
= -1; /* Just in case it gets set with an invalid value. */
3324 * Failing to receive FDs might indicate a major problem such as
3325 * reaching a fd limit during the receive where the kernel returns a
3326 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3327 * don't take any chances and stop everything.
3329 * XXX: Feature request #558 will fix that and avoid this possible
3330 * issue when reaching the fd limit.
3332 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3333 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3337 /* Copy socket information and received FD */
3338 switch (sock_type
) {
3339 case LTTNG_STREAM_CONTROL
:
3340 /* Copy received lttcomm socket */
3341 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3342 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3343 /* Handle create_sock error. */
3345 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3349 * Close the socket created internally by
3350 * lttcomm_create_sock, so we can replace it by the one
3351 * received from sessiond.
3353 if (close(relayd
->control_sock
.sock
.fd
)) {
3357 /* Assign new file descriptor */
3358 relayd
->control_sock
.sock
.fd
= fd
;
3359 fd
= -1; /* For error path */
3360 /* Assign version values. */
3361 relayd
->control_sock
.major
= relayd_sock
->major
;
3362 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3364 relayd
->relayd_session_id
= relayd_session_id
;
3367 case LTTNG_STREAM_DATA
:
3368 /* Copy received lttcomm socket */
3369 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3370 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3371 /* Handle create_sock error. */
3373 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3377 * Close the socket created internally by
3378 * lttcomm_create_sock, so we can replace it by the one
3379 * received from sessiond.
3381 if (close(relayd
->data_sock
.sock
.fd
)) {
3385 /* Assign new file descriptor */
3386 relayd
->data_sock
.sock
.fd
= fd
;
3387 fd
= -1; /* for eventual error paths */
3388 /* Assign version values. */
3389 relayd
->data_sock
.major
= relayd_sock
->major
;
3390 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3393 ERR("Unknown relayd socket type (%d)", sock_type
);
3395 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3399 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3400 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3401 relayd
->net_seq_idx
, fd
);
3403 /* We successfully added the socket. Send status back. */
3404 ret
= consumer_send_status_msg(sock
, ret_code
);
3406 /* Somehow, the session daemon is not responding anymore. */
3407 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3408 goto error_nosignal
;
3412 * Add relayd socket pair to consumer data hashtable. If object already
3413 * exists or on error, the function gracefully returns.
3421 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3422 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3426 /* Close received socket if valid. */
3429 PERROR("close received socket");
3433 if (relayd_created
) {
3441 * Try to lock the stream mutex.
3443 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3445 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3452 * Try to lock the stream mutex. On failure, we know that the stream is
3453 * being used else where hence there is data still being extracted.
3455 ret
= pthread_mutex_trylock(&stream
->lock
);
3457 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3469 * Search for a relayd associated to the session id and return the reference.
3471 * A rcu read side lock MUST be acquire before calling this function and locked
3472 * until the relayd object is no longer necessary.
3474 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3476 struct lttng_ht_iter iter
;
3477 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3479 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3480 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3483 * Check by sessiond id which is unique here where the relayd session
3484 * id might not be when having multiple relayd.
3486 if (relayd
->sessiond_session_id
== id
) {
3487 /* Found the relayd. There can be only one per id. */
3499 * Check if for a given session id there is still data needed to be extract
3502 * Return 1 if data is pending or else 0 meaning ready to be read.
3504 int consumer_data_pending(uint64_t id
)
3507 struct lttng_ht_iter iter
;
3508 struct lttng_ht
*ht
;
3509 struct lttng_consumer_stream
*stream
;
3510 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3511 int (*data_pending
)(struct lttng_consumer_stream
*);
3513 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3516 pthread_mutex_lock(&consumer_data
.lock
);
3518 switch (consumer_data
.type
) {
3519 case LTTNG_CONSUMER_KERNEL
:
3520 data_pending
= lttng_kconsumer_data_pending
;
3522 case LTTNG_CONSUMER32_UST
:
3523 case LTTNG_CONSUMER64_UST
:
3524 data_pending
= lttng_ustconsumer_data_pending
;
3527 ERR("Unknown consumer data type");
3531 /* Ease our life a bit */
3532 ht
= consumer_data
.stream_list_ht
;
3534 relayd
= find_relayd_by_session_id(id
);
3536 /* Send init command for data pending. */
3537 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3538 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3539 relayd
->relayd_session_id
);
3540 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3542 /* Communication error thus the relayd so no data pending. */
3543 goto data_not_pending
;
3547 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3548 ht
->hash_fct(&id
, lttng_ht_seed
),
3550 &iter
.iter
, stream
, node_session_id
.node
) {
3551 /* If this call fails, the stream is being used hence data pending. */
3552 ret
= stream_try_lock(stream
);
3558 * A removed node from the hash table indicates that the stream has
3559 * been deleted thus having a guarantee that the buffers are closed
3560 * on the consumer side. However, data can still be transmitted
3561 * over the network so don't skip the relayd check.
3563 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3565 /* Check the stream if there is data in the buffers. */
3566 ret
= data_pending(stream
);
3568 pthread_mutex_unlock(&stream
->lock
);
3575 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3576 if (stream
->metadata_flag
) {
3577 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3578 stream
->relayd_stream_id
);
3580 ret
= relayd_data_pending(&relayd
->control_sock
,
3581 stream
->relayd_stream_id
,
3582 stream
->next_net_seq_num
- 1);
3584 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3586 pthread_mutex_unlock(&stream
->lock
);
3590 pthread_mutex_unlock(&stream
->lock
);
3594 unsigned int is_data_inflight
= 0;
3596 /* Send init command for data pending. */
3597 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3598 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3599 relayd
->relayd_session_id
, &is_data_inflight
);
3600 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3602 goto data_not_pending
;
3604 if (is_data_inflight
) {
3610 * Finding _no_ node in the hash table and no inflight data means that the
3611 * stream(s) have been removed thus data is guaranteed to be available for
3612 * analysis from the trace files.
3616 /* Data is available to be read by a viewer. */
3617 pthread_mutex_unlock(&consumer_data
.lock
);
3622 /* Data is still being extracted from buffers. */
3623 pthread_mutex_unlock(&consumer_data
.lock
);
3629 * Send a ret code status message to the sessiond daemon.
3631 * Return the sendmsg() return value.
3633 int consumer_send_status_msg(int sock
, int ret_code
)
3635 struct lttcomm_consumer_status_msg msg
;
3637 memset(&msg
, 0, sizeof(msg
));
3638 msg
.ret_code
= ret_code
;
3640 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3644 * Send a channel status message to the sessiond daemon.
3646 * Return the sendmsg() return value.
3648 int consumer_send_status_channel(int sock
,
3649 struct lttng_consumer_channel
*channel
)
3651 struct lttcomm_consumer_status_channel msg
;
3655 memset(&msg
, 0, sizeof(msg
));
3657 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3659 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3660 msg
.key
= channel
->key
;
3661 msg
.stream_count
= channel
->streams
.count
;
3664 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3667 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3668 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3669 uint64_t max_sb_size
)
3671 unsigned long start_pos
;
3673 if (!nb_packets_per_stream
) {
3674 return consumed_pos
; /* Grab everything */
3676 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
3677 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3678 if ((long) (start_pos
- consumed_pos
) < 0) {
3679 return consumed_pos
; /* Grab everything */