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/compat/poll.h>
37 #include <common/compat/endian.h>
38 #include <common/index/index.h>
39 #include <common/kernel-ctl/kernel-ctl.h>
40 #include <common/sessiond-comm/relayd.h>
41 #include <common/sessiond-comm/sessiond-comm.h>
42 #include <common/kernel-consumer/kernel-consumer.h>
43 #include <common/relayd/relayd.h>
44 #include <common/ust-consumer/ust-consumer.h>
45 #include <common/consumer/consumer-timer.h>
46 #include <common/consumer/consumer.h>
47 #include <common/consumer/consumer-stream.h>
48 #include <common/consumer/consumer-testpoint.h>
49 #include <common/align.h>
50 #include <common/consumer/consumer-metadata-cache.h>
52 struct lttng_consumer_global_data consumer_data
= {
55 .type
= LTTNG_CONSUMER_UNKNOWN
,
58 enum consumer_channel_action
{
61 CONSUMER_CHANNEL_QUIT
,
64 struct consumer_channel_msg
{
65 enum consumer_channel_action action
;
66 struct lttng_consumer_channel
*chan
; /* add */
67 uint64_t key
; /* del */
71 * Flag to inform the polling thread to quit when all fd hung up. Updated by
72 * the consumer_thread_receive_fds when it notices that all fds has hung up.
73 * Also updated by the signal handler (consumer_should_exit()). Read by the
76 volatile int consumer_quit
;
79 * Global hash table containing respectively metadata and data streams. The
80 * stream element in this ht should only be updated by the metadata poll thread
81 * for the metadata and the data poll thread for the data.
83 static struct lttng_ht
*metadata_ht
;
84 static struct lttng_ht
*data_ht
;
87 * Notify a thread lttng pipe to poll back again. This usually means that some
88 * global state has changed so we just send back the thread in a poll wait
91 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
93 struct lttng_consumer_stream
*null_stream
= NULL
;
97 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
100 static void notify_health_quit_pipe(int *pipe
)
104 ret
= lttng_write(pipe
[1], "4", 1);
106 PERROR("write consumer health quit");
110 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
111 struct lttng_consumer_channel
*chan
,
113 enum consumer_channel_action action
)
115 struct consumer_channel_msg msg
;
118 memset(&msg
, 0, sizeof(msg
));
123 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
124 if (ret
< sizeof(msg
)) {
125 PERROR("notify_channel_pipe write error");
129 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
132 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
135 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
136 struct lttng_consumer_channel
**chan
,
138 enum consumer_channel_action
*action
)
140 struct consumer_channel_msg msg
;
143 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
144 if (ret
< sizeof(msg
)) {
148 *action
= msg
.action
;
156 * Cleanup the stream list of a channel. Those streams are not yet globally
159 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
161 struct lttng_consumer_stream
*stream
, *stmp
;
165 /* Delete streams that might have been left in the stream list. */
166 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
168 cds_list_del(&stream
->send_node
);
170 * Once a stream is added to this list, the buffers were created so we
171 * have a guarantee that this call will succeed. Setting the monitor
172 * mode to 0 so we don't lock nor try to delete the stream from the
176 consumer_stream_destroy(stream
, NULL
);
181 * Find a stream. The consumer_data.lock must be locked during this
184 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
187 struct lttng_ht_iter iter
;
188 struct lttng_ht_node_u64
*node
;
189 struct lttng_consumer_stream
*stream
= NULL
;
193 /* -1ULL keys are lookup failures */
194 if (key
== (uint64_t) -1ULL) {
200 lttng_ht_lookup(ht
, &key
, &iter
);
201 node
= lttng_ht_iter_get_node_u64(&iter
);
203 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
211 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
213 struct lttng_consumer_stream
*stream
;
216 stream
= find_stream(key
, ht
);
218 stream
->key
= (uint64_t) -1ULL;
220 * We don't want the lookup to match, but we still need
221 * to iterate on this stream when iterating over the hash table. Just
222 * change the node key.
224 stream
->node
.key
= (uint64_t) -1ULL;
230 * Return a channel object for the given key.
232 * RCU read side lock MUST be acquired before calling this function and
233 * protects the channel ptr.
235 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
237 struct lttng_ht_iter iter
;
238 struct lttng_ht_node_u64
*node
;
239 struct lttng_consumer_channel
*channel
= NULL
;
241 /* -1ULL keys are lookup failures */
242 if (key
== (uint64_t) -1ULL) {
246 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
247 node
= lttng_ht_iter_get_node_u64(&iter
);
249 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
256 * There is a possibility that the consumer does not have enough time between
257 * the close of the channel on the session daemon and the cleanup in here thus
258 * once we have a channel add with an existing key, we know for sure that this
259 * channel will eventually get cleaned up by all streams being closed.
261 * This function just nullifies the already existing channel key.
263 static void steal_channel_key(uint64_t key
)
265 struct lttng_consumer_channel
*channel
;
268 channel
= consumer_find_channel(key
);
270 channel
->key
= (uint64_t) -1ULL;
272 * We don't want the lookup to match, but we still need to iterate on
273 * this channel when iterating over the hash table. Just change the
276 channel
->node
.key
= (uint64_t) -1ULL;
281 static void free_channel_rcu(struct rcu_head
*head
)
283 struct lttng_ht_node_u64
*node
=
284 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
285 struct lttng_consumer_channel
*channel
=
286 caa_container_of(node
, struct lttng_consumer_channel
, node
);
288 switch (consumer_data
.type
) {
289 case LTTNG_CONSUMER_KERNEL
:
291 case LTTNG_CONSUMER32_UST
:
292 case LTTNG_CONSUMER64_UST
:
293 lttng_ustconsumer_free_channel(channel
);
296 ERR("Unknown consumer_data type");
303 * RCU protected relayd socket pair free.
305 static void free_relayd_rcu(struct rcu_head
*head
)
307 struct lttng_ht_node_u64
*node
=
308 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
309 struct consumer_relayd_sock_pair
*relayd
=
310 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
313 * Close all sockets. This is done in the call RCU since we don't want the
314 * socket fds to be reassigned thus potentially creating bad state of the
317 * We do not have to lock the control socket mutex here since at this stage
318 * there is no one referencing to this relayd object.
320 (void) relayd_close(&relayd
->control_sock
);
321 (void) relayd_close(&relayd
->data_sock
);
327 * Destroy and free relayd socket pair object.
329 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
332 struct lttng_ht_iter iter
;
334 if (relayd
== NULL
) {
338 DBG("Consumer destroy and close relayd socket pair");
340 iter
.iter
.node
= &relayd
->node
.node
;
341 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
343 /* We assume the relayd is being or is destroyed */
347 /* RCU free() call */
348 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
352 * Remove a channel from the global list protected by a mutex. This function is
353 * also responsible for freeing its data structures.
355 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
358 struct lttng_ht_iter iter
;
360 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
362 pthread_mutex_lock(&consumer_data
.lock
);
363 pthread_mutex_lock(&channel
->lock
);
365 /* Destroy streams that might have been left in the stream list. */
366 clean_channel_stream_list(channel
);
368 if (channel
->live_timer_enabled
== 1) {
369 consumer_timer_live_stop(channel
);
371 if (channel
->monitor_timer_enabled
== 1) {
372 consumer_timer_monitor_stop(channel
);
375 switch (consumer_data
.type
) {
376 case LTTNG_CONSUMER_KERNEL
:
378 case LTTNG_CONSUMER32_UST
:
379 case LTTNG_CONSUMER64_UST
:
380 lttng_ustconsumer_del_channel(channel
);
383 ERR("Unknown consumer_data type");
389 iter
.iter
.node
= &channel
->node
.node
;
390 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
394 call_rcu(&channel
->node
.head
, free_channel_rcu
);
396 pthread_mutex_unlock(&channel
->lock
);
397 pthread_mutex_unlock(&consumer_data
.lock
);
401 * Iterate over the relayd hash table and destroy each element. Finally,
402 * destroy the whole hash table.
404 static void cleanup_relayd_ht(void)
406 struct lttng_ht_iter iter
;
407 struct consumer_relayd_sock_pair
*relayd
;
411 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
413 consumer_destroy_relayd(relayd
);
418 lttng_ht_destroy(consumer_data
.relayd_ht
);
422 * Update the end point status of all streams having the given network sequence
423 * index (relayd index).
425 * It's atomically set without having the stream mutex locked which is fine
426 * because we handle the write/read race with a pipe wakeup for each thread.
428 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
429 enum consumer_endpoint_status status
)
431 struct lttng_ht_iter iter
;
432 struct lttng_consumer_stream
*stream
;
434 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
438 /* Let's begin with metadata */
439 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
440 if (stream
->net_seq_idx
== net_seq_idx
) {
441 uatomic_set(&stream
->endpoint_status
, status
);
442 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
446 /* Follow up by the data streams */
447 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
448 if (stream
->net_seq_idx
== net_seq_idx
) {
449 uatomic_set(&stream
->endpoint_status
, status
);
450 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
457 * Cleanup a relayd object by flagging every associated streams for deletion,
458 * destroying the object meaning removing it from the relayd hash table,
459 * closing the sockets and freeing the memory in a RCU call.
461 * If a local data context is available, notify the threads that the streams'
462 * state have changed.
464 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
465 struct lttng_consumer_local_data
*ctx
)
471 DBG("Cleaning up relayd sockets");
473 /* Save the net sequence index before destroying the object */
474 netidx
= relayd
->net_seq_idx
;
477 * Delete the relayd from the relayd hash table, close the sockets and free
478 * the object in a RCU call.
480 consumer_destroy_relayd(relayd
);
482 /* Set inactive endpoint to all streams */
483 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
486 * With a local data context, notify the threads that the streams' state
487 * have changed. The write() action on the pipe acts as an "implicit"
488 * memory barrier ordering the updates of the end point status from the
489 * read of this status which happens AFTER receiving this notify.
492 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
493 notify_thread_lttng_pipe(ctx
->consumer_metadata_pipe
);
498 * Flag a relayd socket pair for destruction. Destroy it if the refcount
501 * RCU read side lock MUST be aquired before calling this function.
503 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
507 /* Set destroy flag for this object */
508 uatomic_set(&relayd
->destroy_flag
, 1);
510 /* Destroy the relayd if refcount is 0 */
511 if (uatomic_read(&relayd
->refcount
) == 0) {
512 consumer_destroy_relayd(relayd
);
517 * Completly destroy stream from every visiable data structure and the given
520 * One this call returns, the stream object is not longer usable nor visible.
522 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
525 consumer_stream_destroy(stream
, ht
);
529 * XXX naming of del vs destroy is all mixed up.
531 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
533 consumer_stream_destroy(stream
, data_ht
);
536 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
538 consumer_stream_destroy(stream
, metadata_ht
);
541 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
543 enum lttng_consumer_stream_state state
,
544 const char *channel_name
,
551 enum consumer_channel_type type
,
552 unsigned int monitor
)
555 struct lttng_consumer_stream
*stream
;
557 stream
= zmalloc(sizeof(*stream
));
558 if (stream
== NULL
) {
559 PERROR("malloc struct lttng_consumer_stream");
566 stream
->key
= stream_key
;
568 stream
->out_fd_offset
= 0;
569 stream
->output_written
= 0;
570 stream
->state
= state
;
573 stream
->net_seq_idx
= relayd_id
;
574 stream
->session_id
= session_id
;
575 stream
->monitor
= monitor
;
576 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
577 stream
->index_file
= NULL
;
578 stream
->last_sequence_number
= -1ULL;
579 pthread_mutex_init(&stream
->lock
, NULL
);
580 pthread_mutex_init(&stream
->metadata_timer_lock
, NULL
);
582 /* If channel is the metadata, flag this stream as metadata. */
583 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
584 stream
->metadata_flag
= 1;
585 /* Metadata is flat out. */
586 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
587 /* Live rendez-vous point. */
588 pthread_cond_init(&stream
->metadata_rdv
, NULL
);
589 pthread_mutex_init(&stream
->metadata_rdv_lock
, NULL
);
591 /* Format stream name to <channel_name>_<cpu_number> */
592 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
595 PERROR("snprintf stream name");
600 /* Key is always the wait_fd for streams. */
601 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
603 /* Init node per channel id key */
604 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
606 /* Init session id node with the stream session id */
607 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
609 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
610 " relayd_id %" PRIu64
", session_id %" PRIu64
,
611 stream
->name
, stream
->key
, channel_key
,
612 stream
->net_seq_idx
, stream
->session_id
);
628 * Add a stream to the global list protected by a mutex.
630 int consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
632 struct lttng_ht
*ht
= data_ht
;
638 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
640 pthread_mutex_lock(&consumer_data
.lock
);
641 pthread_mutex_lock(&stream
->chan
->lock
);
642 pthread_mutex_lock(&stream
->chan
->timer_lock
);
643 pthread_mutex_lock(&stream
->lock
);
646 /* Steal stream identifier to avoid having streams with the same key */
647 steal_stream_key(stream
->key
, ht
);
649 lttng_ht_add_unique_u64(ht
, &stream
->node
);
651 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
652 &stream
->node_channel_id
);
655 * Add stream to the stream_list_ht of the consumer data. No need to steal
656 * the key since the HT does not use it and we allow to add redundant keys
659 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
662 * When nb_init_stream_left reaches 0, we don't need to trigger any action
663 * in terms of destroying the associated channel, because the action that
664 * causes the count to become 0 also causes a stream to be added. The
665 * channel deletion will thus be triggered by the following removal of this
668 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
669 /* Increment refcount before decrementing nb_init_stream_left */
671 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
674 /* Update consumer data once the node is inserted. */
675 consumer_data
.stream_count
++;
676 consumer_data
.need_update
= 1;
679 pthread_mutex_unlock(&stream
->lock
);
680 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
681 pthread_mutex_unlock(&stream
->chan
->lock
);
682 pthread_mutex_unlock(&consumer_data
.lock
);
687 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
689 consumer_del_stream(stream
, data_ht
);
693 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
694 * be acquired before calling this.
696 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
699 struct lttng_ht_node_u64
*node
;
700 struct lttng_ht_iter iter
;
704 lttng_ht_lookup(consumer_data
.relayd_ht
,
705 &relayd
->net_seq_idx
, &iter
);
706 node
= lttng_ht_iter_get_node_u64(&iter
);
710 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
717 * Allocate and return a consumer relayd socket.
719 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
720 uint64_t net_seq_idx
)
722 struct consumer_relayd_sock_pair
*obj
= NULL
;
724 /* net sequence index of -1 is a failure */
725 if (net_seq_idx
== (uint64_t) -1ULL) {
729 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
731 PERROR("zmalloc relayd sock");
735 obj
->net_seq_idx
= net_seq_idx
;
737 obj
->destroy_flag
= 0;
738 obj
->control_sock
.sock
.fd
= -1;
739 obj
->data_sock
.sock
.fd
= -1;
740 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
741 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
748 * Find a relayd socket pair in the global consumer data.
750 * Return the object if found else NULL.
751 * RCU read-side lock must be held across this call and while using the
754 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
756 struct lttng_ht_iter iter
;
757 struct lttng_ht_node_u64
*node
;
758 struct consumer_relayd_sock_pair
*relayd
= NULL
;
760 /* Negative keys are lookup failures */
761 if (key
== (uint64_t) -1ULL) {
765 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
767 node
= lttng_ht_iter_get_node_u64(&iter
);
769 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
777 * Find a relayd and send the stream
779 * Returns 0 on success, < 0 on error
781 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
785 struct consumer_relayd_sock_pair
*relayd
;
788 assert(stream
->net_seq_idx
!= -1ULL);
791 /* The stream is not metadata. Get relayd reference if exists. */
793 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
794 if (relayd
!= NULL
) {
795 /* Add stream on the relayd */
796 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
797 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
798 path
, &stream
->relayd_stream_id
,
799 stream
->chan
->tracefile_size
, stream
->chan
->tracefile_count
);
800 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
805 uatomic_inc(&relayd
->refcount
);
806 stream
->sent_to_relayd
= 1;
808 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
809 stream
->key
, stream
->net_seq_idx
);
814 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
815 stream
->name
, stream
->key
, stream
->net_seq_idx
);
823 * Find a relayd and send the streams sent message
825 * Returns 0 on success, < 0 on error
827 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
830 struct consumer_relayd_sock_pair
*relayd
;
832 assert(net_seq_idx
!= -1ULL);
834 /* The stream is not metadata. Get relayd reference if exists. */
836 relayd
= consumer_find_relayd(net_seq_idx
);
837 if (relayd
!= NULL
) {
838 /* Add stream on the relayd */
839 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
840 ret
= relayd_streams_sent(&relayd
->control_sock
);
841 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
846 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
853 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
861 * Find a relayd and close the stream
863 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
865 struct consumer_relayd_sock_pair
*relayd
;
867 /* The stream is not metadata. Get relayd reference if exists. */
869 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
871 consumer_stream_relayd_close(stream
, relayd
);
877 * Handle stream for relayd transmission if the stream applies for network
878 * streaming where the net sequence index is set.
880 * Return destination file descriptor or negative value on error.
882 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
883 size_t data_size
, unsigned long padding
,
884 struct consumer_relayd_sock_pair
*relayd
)
887 struct lttcomm_relayd_data_hdr data_hdr
;
893 /* Reset data header */
894 memset(&data_hdr
, 0, sizeof(data_hdr
));
896 if (stream
->metadata_flag
) {
897 /* Caller MUST acquire the relayd control socket lock */
898 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
903 /* Metadata are always sent on the control socket. */
904 outfd
= relayd
->control_sock
.sock
.fd
;
906 /* Set header with stream information */
907 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
908 data_hdr
.data_size
= htobe32(data_size
);
909 data_hdr
.padding_size
= htobe32(padding
);
911 * Note that net_seq_num below is assigned with the *current* value of
912 * next_net_seq_num and only after that the next_net_seq_num will be
913 * increment. This is why when issuing a command on the relayd using
914 * this next value, 1 should always be substracted in order to compare
915 * the last seen sequence number on the relayd side to the last sent.
917 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
918 /* Other fields are zeroed previously */
920 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
926 ++stream
->next_net_seq_num
;
928 /* Set to go on data socket */
929 outfd
= relayd
->data_sock
.sock
.fd
;
937 * Allocate and return a new lttng_consumer_channel object using the given key
938 * to initialize the hash table node.
940 * On error, return NULL.
942 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
944 const char *pathname
,
949 enum lttng_event_output output
,
950 uint64_t tracefile_size
,
951 uint64_t tracefile_count
,
952 uint64_t session_id_per_pid
,
953 unsigned int monitor
,
954 unsigned int live_timer_interval
,
955 const char *root_shm_path
,
956 const char *shm_path
)
958 struct lttng_consumer_channel
*channel
;
960 channel
= zmalloc(sizeof(*channel
));
961 if (channel
== NULL
) {
962 PERROR("malloc struct lttng_consumer_channel");
967 channel
->refcount
= 0;
968 channel
->session_id
= session_id
;
969 channel
->session_id_per_pid
= session_id_per_pid
;
972 channel
->relayd_id
= relayd_id
;
973 channel
->tracefile_size
= tracefile_size
;
974 channel
->tracefile_count
= tracefile_count
;
975 channel
->monitor
= monitor
;
976 channel
->live_timer_interval
= live_timer_interval
;
977 pthread_mutex_init(&channel
->lock
, NULL
);
978 pthread_mutex_init(&channel
->timer_lock
, NULL
);
981 case LTTNG_EVENT_SPLICE
:
982 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
984 case LTTNG_EVENT_MMAP
:
985 channel
->output
= CONSUMER_CHANNEL_MMAP
;
995 * In monitor mode, the streams associated with the channel will be put in
996 * a special list ONLY owned by this channel. So, the refcount is set to 1
997 * here meaning that the channel itself has streams that are referenced.
999 * On a channel deletion, once the channel is no longer visible, the
1000 * refcount is decremented and checked for a zero value to delete it. With
1001 * streams in no monitor mode, it will now be safe to destroy the channel.
1003 if (!channel
->monitor
) {
1004 channel
->refcount
= 1;
1007 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1008 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1010 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1011 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1013 if (root_shm_path
) {
1014 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1015 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1018 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1019 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1022 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1024 channel
->wait_fd
= -1;
1026 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1028 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1035 * Add a channel to the global list protected by a mutex.
1037 * Always return 0 indicating success.
1039 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1040 struct lttng_consumer_local_data
*ctx
)
1042 pthread_mutex_lock(&consumer_data
.lock
);
1043 pthread_mutex_lock(&channel
->lock
);
1044 pthread_mutex_lock(&channel
->timer_lock
);
1047 * This gives us a guarantee that the channel we are about to add to the
1048 * channel hash table will be unique. See this function comment on the why
1049 * we need to steel the channel key at this stage.
1051 steal_channel_key(channel
->key
);
1054 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1057 pthread_mutex_unlock(&channel
->timer_lock
);
1058 pthread_mutex_unlock(&channel
->lock
);
1059 pthread_mutex_unlock(&consumer_data
.lock
);
1061 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1062 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1069 * Allocate the pollfd structure and the local view of the out fds to avoid
1070 * doing a lookup in the linked list and concurrency issues when writing is
1071 * needed. Called with consumer_data.lock held.
1073 * Returns the number of fds in the structures.
1075 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1076 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1077 struct lttng_ht
*ht
)
1080 struct lttng_ht_iter iter
;
1081 struct lttng_consumer_stream
*stream
;
1086 assert(local_stream
);
1088 DBG("Updating poll fd array");
1090 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1092 * Only active streams with an active end point can be added to the
1093 * poll set and local stream storage of the thread.
1095 * There is a potential race here for endpoint_status to be updated
1096 * just after the check. However, this is OK since the stream(s) will
1097 * be deleted once the thread is notified that the end point state has
1098 * changed where this function will be called back again.
1100 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
1101 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1105 * This clobbers way too much the debug output. Uncomment that if you
1106 * need it for debugging purposes.
1108 * DBG("Active FD %d", stream->wait_fd);
1110 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1111 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1112 local_stream
[i
] = stream
;
1118 * Insert the consumer_data_pipe at the end of the array and don't
1119 * increment i so nb_fd is the number of real FD.
1121 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1122 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1124 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1125 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1130 * Poll on the should_quit pipe and the command socket return -1 on
1131 * error, 1 if should exit, 0 if data is available on the command socket
1133 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1138 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1139 if (num_rdy
== -1) {
1141 * Restart interrupted system call.
1143 if (errno
== EINTR
) {
1146 PERROR("Poll error");
1149 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1150 DBG("consumer_should_quit wake up");
1157 * Set the error socket.
1159 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1162 ctx
->consumer_error_socket
= sock
;
1166 * Set the command socket path.
1168 void lttng_consumer_set_command_sock_path(
1169 struct lttng_consumer_local_data
*ctx
, char *sock
)
1171 ctx
->consumer_command_sock_path
= sock
;
1175 * Send return code to the session daemon.
1176 * If the socket is not defined, we return 0, it is not a fatal error
1178 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1180 if (ctx
->consumer_error_socket
> 0) {
1181 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1182 sizeof(enum lttcomm_sessiond_command
));
1189 * Close all the tracefiles and stream fds and MUST be called when all
1190 * instances are destroyed i.e. when all threads were joined and are ended.
1192 void lttng_consumer_cleanup(void)
1194 struct lttng_ht_iter iter
;
1195 struct lttng_consumer_channel
*channel
;
1199 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1201 consumer_del_channel(channel
);
1206 lttng_ht_destroy(consumer_data
.channel_ht
);
1208 cleanup_relayd_ht();
1210 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1213 * This HT contains streams that are freed by either the metadata thread or
1214 * the data thread so we do *nothing* on the hash table and simply destroy
1217 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1221 * Called from signal handler.
1223 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1228 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1230 PERROR("write consumer quit");
1233 DBG("Consumer flag that it should quit");
1238 * Flush pending writes to trace output disk file.
1241 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1245 int outfd
= stream
->out_fd
;
1248 * This does a blocking write-and-wait on any page that belongs to the
1249 * subbuffer prior to the one we just wrote.
1250 * Don't care about error values, as these are just hints and ways to
1251 * limit the amount of page cache used.
1253 if (orig_offset
< stream
->max_sb_size
) {
1256 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1257 stream
->max_sb_size
,
1258 SYNC_FILE_RANGE_WAIT_BEFORE
1259 | SYNC_FILE_RANGE_WRITE
1260 | SYNC_FILE_RANGE_WAIT_AFTER
);
1262 * Give hints to the kernel about how we access the file:
1263 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1266 * We need to call fadvise again after the file grows because the
1267 * kernel does not seem to apply fadvise to non-existing parts of the
1270 * Call fadvise _after_ having waited for the page writeback to
1271 * complete because the dirty page writeback semantic is not well
1272 * defined. So it can be expected to lead to lower throughput in
1275 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1276 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1277 if (ret
&& ret
!= -ENOSYS
) {
1279 PERROR("posix_fadvise on fd %i", outfd
);
1284 * Initialise the necessary environnement :
1285 * - create a new context
1286 * - create the poll_pipe
1287 * - create the should_quit pipe (for signal handler)
1288 * - create the thread pipe (for splice)
1290 * Takes a function pointer as argument, this function is called when data is
1291 * available on a buffer. This function is responsible to do the
1292 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1293 * buffer configuration and then kernctl_put_next_subbuf at the end.
1295 * Returns a pointer to the new context or NULL on error.
1297 struct lttng_consumer_local_data
*lttng_consumer_create(
1298 enum lttng_consumer_type type
,
1299 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1300 struct lttng_consumer_local_data
*ctx
),
1301 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1302 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1303 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1306 struct lttng_consumer_local_data
*ctx
;
1308 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1309 consumer_data
.type
== type
);
1310 consumer_data
.type
= type
;
1312 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1314 PERROR("allocating context");
1318 ctx
->consumer_error_socket
= -1;
1319 ctx
->consumer_metadata_socket
= -1;
1320 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1321 /* assign the callbacks */
1322 ctx
->on_buffer_ready
= buffer_ready
;
1323 ctx
->on_recv_channel
= recv_channel
;
1324 ctx
->on_recv_stream
= recv_stream
;
1325 ctx
->on_update_stream
= update_stream
;
1327 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1328 if (!ctx
->consumer_data_pipe
) {
1329 goto error_poll_pipe
;
1332 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1333 if (!ctx
->consumer_wakeup_pipe
) {
1334 goto error_wakeup_pipe
;
1337 ret
= pipe(ctx
->consumer_should_quit
);
1339 PERROR("Error creating recv pipe");
1340 goto error_quit_pipe
;
1343 ret
= pipe(ctx
->consumer_channel_pipe
);
1345 PERROR("Error creating channel pipe");
1346 goto error_channel_pipe
;
1349 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1350 if (!ctx
->consumer_metadata_pipe
) {
1351 goto error_metadata_pipe
;
1354 ctx
->channel_monitor_pipe
= -1;
1358 error_metadata_pipe
:
1359 utils_close_pipe(ctx
->consumer_channel_pipe
);
1361 utils_close_pipe(ctx
->consumer_should_quit
);
1363 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1365 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1373 * Iterate over all streams of the hashtable and free them properly.
1375 static void destroy_data_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_stream(stream
, ht
);
1394 lttng_ht_destroy(ht
);
1398 * Iterate over all streams of the metadata hashtable and free them
1401 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1403 struct lttng_ht_iter iter
;
1404 struct lttng_consumer_stream
*stream
;
1411 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1413 * Ignore return value since we are currently cleaning up so any error
1416 (void) consumer_del_metadata_stream(stream
, ht
);
1420 lttng_ht_destroy(ht
);
1424 * Close all fds associated with the instance and free the context.
1426 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1430 DBG("Consumer destroying it. Closing everything.");
1436 destroy_data_stream_ht(data_ht
);
1437 destroy_metadata_stream_ht(metadata_ht
);
1439 ret
= close(ctx
->consumer_error_socket
);
1443 ret
= close(ctx
->consumer_metadata_socket
);
1447 utils_close_pipe(ctx
->consumer_channel_pipe
);
1448 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1449 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1450 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1451 utils_close_pipe(ctx
->consumer_should_quit
);
1453 unlink(ctx
->consumer_command_sock_path
);
1458 * Write the metadata stream id on the specified file descriptor.
1460 static int write_relayd_metadata_id(int fd
,
1461 struct lttng_consumer_stream
*stream
,
1462 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1465 struct lttcomm_relayd_metadata_payload hdr
;
1467 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1468 hdr
.padding_size
= htobe32(padding
);
1469 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1470 if (ret
< sizeof(hdr
)) {
1472 * This error means that the fd's end is closed so ignore the PERROR
1473 * not to clubber the error output since this can happen in a normal
1476 if (errno
!= EPIPE
) {
1477 PERROR("write metadata stream id");
1479 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1481 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1482 * handle writting the missing part so report that as an error and
1483 * don't lie to the caller.
1488 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1489 stream
->relayd_stream_id
, padding
);
1496 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1497 * core function for writing trace buffers to either the local filesystem or
1500 * It must be called with the stream lock held.
1502 * Careful review MUST be put if any changes occur!
1504 * Returns the number of bytes written
1506 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1507 struct lttng_consumer_local_data
*ctx
,
1508 struct lttng_consumer_stream
*stream
, unsigned long len
,
1509 unsigned long padding
,
1510 struct ctf_packet_index
*index
)
1512 unsigned long mmap_offset
;
1515 off_t orig_offset
= stream
->out_fd_offset
;
1516 /* Default is on the disk */
1517 int outfd
= stream
->out_fd
;
1518 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1519 unsigned int relayd_hang_up
= 0;
1521 /* RCU lock for the relayd pointer */
1524 /* Flag that the current stream if set for network streaming. */
1525 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1526 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1527 if (relayd
== NULL
) {
1533 /* get the offset inside the fd to mmap */
1534 switch (consumer_data
.type
) {
1535 case LTTNG_CONSUMER_KERNEL
:
1536 mmap_base
= stream
->mmap_base
;
1537 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1539 PERROR("tracer ctl get_mmap_read_offset");
1543 case LTTNG_CONSUMER32_UST
:
1544 case LTTNG_CONSUMER64_UST
:
1545 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1547 ERR("read mmap get mmap base for stream %s", stream
->name
);
1551 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1553 PERROR("tracer ctl get_mmap_read_offset");
1559 ERR("Unknown consumer_data type");
1563 /* Handle stream on the relayd if the output is on the network */
1565 unsigned long netlen
= len
;
1568 * Lock the control socket for the complete duration of the function
1569 * since from this point on we will use the socket.
1571 if (stream
->metadata_flag
) {
1572 /* Metadata requires the control socket. */
1573 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1574 if (stream
->reset_metadata_flag
) {
1575 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1576 stream
->relayd_stream_id
,
1577 stream
->metadata_version
);
1582 stream
->reset_metadata_flag
= 0;
1584 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1587 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1592 /* Use the returned socket. */
1595 /* Write metadata stream id before payload */
1596 if (stream
->metadata_flag
) {
1597 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1604 /* No streaming, we have to set the len with the full padding */
1607 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1608 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1610 ERR("Reset metadata file");
1613 stream
->reset_metadata_flag
= 0;
1617 * Check if we need to change the tracefile before writing the packet.
1619 if (stream
->chan
->tracefile_size
> 0 &&
1620 (stream
->tracefile_size_current
+ len
) >
1621 stream
->chan
->tracefile_size
) {
1622 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1623 stream
->name
, stream
->chan
->tracefile_size
,
1624 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1625 stream
->out_fd
, &(stream
->tracefile_count_current
),
1628 ERR("Rotating output file");
1631 outfd
= stream
->out_fd
;
1633 if (stream
->index_file
) {
1634 lttng_index_file_put(stream
->index_file
);
1635 stream
->index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1636 stream
->name
, stream
->uid
, stream
->gid
,
1637 stream
->chan
->tracefile_size
,
1638 stream
->tracefile_count_current
,
1639 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1640 if (!stream
->index_file
) {
1645 /* Reset current size because we just perform a rotation. */
1646 stream
->tracefile_size_current
= 0;
1647 stream
->out_fd_offset
= 0;
1650 stream
->tracefile_size_current
+= len
;
1652 index
->offset
= htobe64(stream
->out_fd_offset
);
1657 * This call guarantee that len or less is returned. It's impossible to
1658 * receive a ret value that is bigger than len.
1660 ret
= lttng_write(outfd
, mmap_base
+ mmap_offset
, len
);
1661 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1662 if (ret
< 0 || ((size_t) ret
!= len
)) {
1664 * Report error to caller if nothing was written else at least send the
1672 /* Socket operation failed. We consider the relayd dead */
1673 if (errno
== EPIPE
|| errno
== EINVAL
|| errno
== EBADF
) {
1675 * This is possible if the fd is closed on the other side
1676 * (outfd) or any write problem. It can be verbose a bit for a
1677 * normal execution if for instance the relayd is stopped
1678 * abruptly. This can happen so set this to a DBG statement.
1680 DBG("Consumer mmap write detected relayd hang up");
1682 /* Unhandled error, print it and stop function right now. */
1683 PERROR("Error in write mmap (ret %zd != len %lu)", ret
, len
);
1687 stream
->output_written
+= ret
;
1689 /* This call is useless on a socket so better save a syscall. */
1691 /* This won't block, but will start writeout asynchronously */
1692 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, len
,
1693 SYNC_FILE_RANGE_WRITE
);
1694 stream
->out_fd_offset
+= len
;
1695 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1700 * This is a special case that the relayd has closed its socket. Let's
1701 * cleanup the relayd object and all associated streams.
1703 if (relayd
&& relayd_hang_up
) {
1704 cleanup_relayd(relayd
, ctx
);
1708 /* Unlock only if ctrl socket used */
1709 if (relayd
&& stream
->metadata_flag
) {
1710 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1718 * Splice the data from the ring buffer to the tracefile.
1720 * It must be called with the stream lock held.
1722 * Returns the number of bytes spliced.
1724 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1725 struct lttng_consumer_local_data
*ctx
,
1726 struct lttng_consumer_stream
*stream
, unsigned long len
,
1727 unsigned long padding
,
1728 struct ctf_packet_index
*index
)
1730 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1732 off_t orig_offset
= stream
->out_fd_offset
;
1733 int fd
= stream
->wait_fd
;
1734 /* Default is on the disk */
1735 int outfd
= stream
->out_fd
;
1736 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1738 unsigned int relayd_hang_up
= 0;
1740 switch (consumer_data
.type
) {
1741 case LTTNG_CONSUMER_KERNEL
:
1743 case LTTNG_CONSUMER32_UST
:
1744 case LTTNG_CONSUMER64_UST
:
1745 /* Not supported for user space tracing */
1748 ERR("Unknown consumer_data type");
1752 /* RCU lock for the relayd pointer */
1755 /* Flag that the current stream if set for network streaming. */
1756 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1757 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1758 if (relayd
== NULL
) {
1763 splice_pipe
= stream
->splice_pipe
;
1765 /* Write metadata stream id before payload */
1767 unsigned long total_len
= len
;
1769 if (stream
->metadata_flag
) {
1771 * Lock the control socket for the complete duration of the function
1772 * since from this point on we will use the socket.
1774 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1776 if (stream
->reset_metadata_flag
) {
1777 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1778 stream
->relayd_stream_id
,
1779 stream
->metadata_version
);
1784 stream
->reset_metadata_flag
= 0;
1786 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1794 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1797 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1803 /* Use the returned socket. */
1806 /* No streaming, we have to set the len with the full padding */
1809 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1810 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1812 ERR("Reset metadata file");
1815 stream
->reset_metadata_flag
= 0;
1818 * Check if we need to change the tracefile before writing the packet.
1820 if (stream
->chan
->tracefile_size
> 0 &&
1821 (stream
->tracefile_size_current
+ len
) >
1822 stream
->chan
->tracefile_size
) {
1823 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1824 stream
->name
, stream
->chan
->tracefile_size
,
1825 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1826 stream
->out_fd
, &(stream
->tracefile_count_current
),
1830 ERR("Rotating output file");
1833 outfd
= stream
->out_fd
;
1835 if (stream
->index_file
) {
1836 lttng_index_file_put(stream
->index_file
);
1837 stream
->index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1838 stream
->name
, stream
->uid
, stream
->gid
,
1839 stream
->chan
->tracefile_size
,
1840 stream
->tracefile_count_current
,
1841 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1842 if (!stream
->index_file
) {
1847 /* Reset current size because we just perform a rotation. */
1848 stream
->tracefile_size_current
= 0;
1849 stream
->out_fd_offset
= 0;
1852 stream
->tracefile_size_current
+= len
;
1853 index
->offset
= htobe64(stream
->out_fd_offset
);
1857 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1858 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1859 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1860 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1861 DBG("splice chan to pipe, ret %zd", ret_splice
);
1862 if (ret_splice
< 0) {
1865 PERROR("Error in relay splice");
1869 /* Handle stream on the relayd if the output is on the network */
1870 if (relayd
&& stream
->metadata_flag
) {
1871 size_t metadata_payload_size
=
1872 sizeof(struct lttcomm_relayd_metadata_payload
);
1874 /* Update counter to fit the spliced data */
1875 ret_splice
+= metadata_payload_size
;
1876 len
+= metadata_payload_size
;
1878 * We do this so the return value can match the len passed as
1879 * argument to this function.
1881 written
-= metadata_payload_size
;
1884 /* Splice data out */
1885 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1886 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1887 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1889 if (ret_splice
< 0) {
1894 } else if (ret_splice
> len
) {
1896 * We don't expect this code path to be executed but you never know
1897 * so this is an extra protection agains a buggy splice().
1900 written
+= ret_splice
;
1901 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1905 /* All good, update current len and continue. */
1909 /* This call is useless on a socket so better save a syscall. */
1911 /* This won't block, but will start writeout asynchronously */
1912 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1913 SYNC_FILE_RANGE_WRITE
);
1914 stream
->out_fd_offset
+= ret_splice
;
1916 stream
->output_written
+= ret_splice
;
1917 written
+= ret_splice
;
1920 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1926 * This is a special case that the relayd has closed its socket. Let's
1927 * cleanup the relayd object and all associated streams.
1929 if (relayd
&& relayd_hang_up
) {
1930 cleanup_relayd(relayd
, ctx
);
1931 /* Skip splice error so the consumer does not fail */
1936 /* send the appropriate error description to sessiond */
1939 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1942 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1945 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1950 if (relayd
&& stream
->metadata_flag
) {
1951 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1959 * Take a snapshot for a specific fd
1961 * Returns 0 on success, < 0 on error
1963 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1965 switch (consumer_data
.type
) {
1966 case LTTNG_CONSUMER_KERNEL
:
1967 return lttng_kconsumer_take_snapshot(stream
);
1968 case LTTNG_CONSUMER32_UST
:
1969 case LTTNG_CONSUMER64_UST
:
1970 return lttng_ustconsumer_take_snapshot(stream
);
1972 ERR("Unknown consumer_data type");
1979 * Get the produced position
1981 * Returns 0 on success, < 0 on error
1983 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1986 switch (consumer_data
.type
) {
1987 case LTTNG_CONSUMER_KERNEL
:
1988 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1989 case LTTNG_CONSUMER32_UST
:
1990 case LTTNG_CONSUMER64_UST
:
1991 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1993 ERR("Unknown consumer_data type");
1999 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2000 int sock
, struct pollfd
*consumer_sockpoll
)
2002 switch (consumer_data
.type
) {
2003 case LTTNG_CONSUMER_KERNEL
:
2004 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2005 case LTTNG_CONSUMER32_UST
:
2006 case LTTNG_CONSUMER64_UST
:
2007 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2009 ERR("Unknown consumer_data type");
2015 void lttng_consumer_close_all_metadata(void)
2017 switch (consumer_data
.type
) {
2018 case LTTNG_CONSUMER_KERNEL
:
2020 * The Kernel consumer has a different metadata scheme so we don't
2021 * close anything because the stream will be closed by the session
2025 case LTTNG_CONSUMER32_UST
:
2026 case LTTNG_CONSUMER64_UST
:
2028 * Close all metadata streams. The metadata hash table is passed and
2029 * this call iterates over it by closing all wakeup fd. This is safe
2030 * because at this point we are sure that the metadata producer is
2031 * either dead or blocked.
2033 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2036 ERR("Unknown consumer_data type");
2042 * Clean up a metadata stream and free its memory.
2044 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2045 struct lttng_ht
*ht
)
2047 struct lttng_consumer_channel
*free_chan
= NULL
;
2051 * This call should NEVER receive regular stream. It must always be
2052 * metadata stream and this is crucial for data structure synchronization.
2054 assert(stream
->metadata_flag
);
2056 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2058 pthread_mutex_lock(&consumer_data
.lock
);
2059 pthread_mutex_lock(&stream
->chan
->lock
);
2060 pthread_mutex_lock(&stream
->lock
);
2061 if (stream
->chan
->metadata_cache
) {
2062 /* Only applicable to userspace consumers. */
2063 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2066 /* Remove any reference to that stream. */
2067 consumer_stream_delete(stream
, ht
);
2069 /* Close down everything including the relayd if one. */
2070 consumer_stream_close(stream
);
2071 /* Destroy tracer buffers of the stream. */
2072 consumer_stream_destroy_buffers(stream
);
2074 /* Atomically decrement channel refcount since other threads can use it. */
2075 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
2076 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
2077 /* Go for channel deletion! */
2078 free_chan
= stream
->chan
;
2082 * Nullify the stream reference so it is not used after deletion. The
2083 * channel lock MUST be acquired before being able to check for a NULL
2086 stream
->chan
->metadata_stream
= NULL
;
2088 if (stream
->chan
->metadata_cache
) {
2089 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2091 pthread_mutex_unlock(&stream
->lock
);
2092 pthread_mutex_unlock(&stream
->chan
->lock
);
2093 pthread_mutex_unlock(&consumer_data
.lock
);
2096 consumer_del_channel(free_chan
);
2099 consumer_stream_free(stream
);
2103 * Action done with the metadata stream when adding it to the consumer internal
2104 * data structures to handle it.
2106 int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2108 struct lttng_ht
*ht
= metadata_ht
;
2110 struct lttng_ht_iter iter
;
2111 struct lttng_ht_node_u64
*node
;
2116 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2118 pthread_mutex_lock(&consumer_data
.lock
);
2119 pthread_mutex_lock(&stream
->chan
->lock
);
2120 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2121 pthread_mutex_lock(&stream
->lock
);
2124 * From here, refcounts are updated so be _careful_ when returning an error
2131 * Lookup the stream just to make sure it does not exist in our internal
2132 * state. This should NEVER happen.
2134 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2135 node
= lttng_ht_iter_get_node_u64(&iter
);
2139 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2140 * in terms of destroying the associated channel, because the action that
2141 * causes the count to become 0 also causes a stream to be added. The
2142 * channel deletion will thus be triggered by the following removal of this
2145 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2146 /* Increment refcount before decrementing nb_init_stream_left */
2148 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2151 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2153 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2154 &stream
->node_channel_id
);
2157 * Add stream to the stream_list_ht of the consumer data. No need to steal
2158 * the key since the HT does not use it and we allow to add redundant keys
2161 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2165 pthread_mutex_unlock(&stream
->lock
);
2166 pthread_mutex_unlock(&stream
->chan
->lock
);
2167 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2168 pthread_mutex_unlock(&consumer_data
.lock
);
2173 * Delete data stream that are flagged for deletion (endpoint_status).
2175 static void validate_endpoint_status_data_stream(void)
2177 struct lttng_ht_iter iter
;
2178 struct lttng_consumer_stream
*stream
;
2180 DBG("Consumer delete flagged data stream");
2183 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2184 /* Validate delete flag of the stream */
2185 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2188 /* Delete it right now */
2189 consumer_del_stream(stream
, data_ht
);
2195 * Delete metadata stream that are flagged for deletion (endpoint_status).
2197 static void validate_endpoint_status_metadata_stream(
2198 struct lttng_poll_event
*pollset
)
2200 struct lttng_ht_iter iter
;
2201 struct lttng_consumer_stream
*stream
;
2203 DBG("Consumer delete flagged metadata stream");
2208 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2209 /* Validate delete flag of the stream */
2210 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2214 * Remove from pollset so the metadata thread can continue without
2215 * blocking on a deleted stream.
2217 lttng_poll_del(pollset
, stream
->wait_fd
);
2219 /* Delete it right now */
2220 consumer_del_metadata_stream(stream
, metadata_ht
);
2226 * Thread polls on metadata file descriptor and write them on disk or on the
2229 void *consumer_thread_metadata_poll(void *data
)
2231 int ret
, i
, pollfd
, err
= -1;
2232 uint32_t revents
, nb_fd
;
2233 struct lttng_consumer_stream
*stream
= NULL
;
2234 struct lttng_ht_iter iter
;
2235 struct lttng_ht_node_u64
*node
;
2236 struct lttng_poll_event events
;
2237 struct lttng_consumer_local_data
*ctx
= data
;
2240 rcu_register_thread();
2242 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2244 if (testpoint(consumerd_thread_metadata
)) {
2245 goto error_testpoint
;
2248 health_code_update();
2250 DBG("Thread metadata poll started");
2252 /* Size is set to 1 for the consumer_metadata pipe */
2253 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2255 ERR("Poll set creation failed");
2259 ret
= lttng_poll_add(&events
,
2260 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2266 DBG("Metadata main loop started");
2270 health_code_update();
2271 health_poll_entry();
2272 DBG("Metadata poll wait");
2273 ret
= lttng_poll_wait(&events
, -1);
2274 DBG("Metadata poll return from wait with %d fd(s)",
2275 LTTNG_POLL_GETNB(&events
));
2277 DBG("Metadata event caught in thread");
2279 if (errno
== EINTR
) {
2280 ERR("Poll EINTR caught");
2283 if (LTTNG_POLL_GETNB(&events
) == 0) {
2284 err
= 0; /* All is OK */
2291 /* From here, the event is a metadata wait fd */
2292 for (i
= 0; i
< nb_fd
; i
++) {
2293 health_code_update();
2295 revents
= LTTNG_POLL_GETEV(&events
, i
);
2296 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2299 /* No activity for this FD (poll implementation). */
2303 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2304 if (revents
& LPOLLIN
) {
2307 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2308 &stream
, sizeof(stream
));
2309 if (pipe_len
< sizeof(stream
)) {
2311 PERROR("read metadata stream");
2314 * Remove the pipe from the poll set and continue the loop
2315 * since their might be data to consume.
2317 lttng_poll_del(&events
,
2318 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2319 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2323 /* A NULL stream means that the state has changed. */
2324 if (stream
== NULL
) {
2325 /* Check for deleted streams. */
2326 validate_endpoint_status_metadata_stream(&events
);
2330 DBG("Adding metadata stream %d to poll set",
2333 /* Add metadata stream to the global poll events list */
2334 lttng_poll_add(&events
, stream
->wait_fd
,
2335 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2336 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2337 DBG("Metadata thread pipe hung up");
2339 * Remove the pipe from the poll set and continue the loop
2340 * since their might be data to consume.
2342 lttng_poll_del(&events
,
2343 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2344 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2347 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2351 /* Handle other stream */
2357 uint64_t tmp_id
= (uint64_t) pollfd
;
2359 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2361 node
= lttng_ht_iter_get_node_u64(&iter
);
2364 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2367 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2368 /* Get the data out of the metadata file descriptor */
2369 DBG("Metadata available on fd %d", pollfd
);
2370 assert(stream
->wait_fd
== pollfd
);
2373 health_code_update();
2375 len
= ctx
->on_buffer_ready(stream
, ctx
);
2377 * We don't check the return value here since if we get
2378 * a negative len, it means an error occurred thus we
2379 * simply remove it from the poll set and free the
2384 /* It's ok to have an unavailable sub-buffer */
2385 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2386 /* Clean up stream from consumer and free it. */
2387 lttng_poll_del(&events
, stream
->wait_fd
);
2388 consumer_del_metadata_stream(stream
, metadata_ht
);
2390 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2391 DBG("Metadata fd %d is hup|err.", pollfd
);
2392 if (!stream
->hangup_flush_done
2393 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2394 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2395 DBG("Attempting to flush and consume the UST buffers");
2396 lttng_ustconsumer_on_stream_hangup(stream
);
2398 /* We just flushed the stream now read it. */
2400 health_code_update();
2402 len
= ctx
->on_buffer_ready(stream
, ctx
);
2404 * We don't check the return value here since if we get
2405 * a negative len, it means an error occurred thus we
2406 * simply remove it from the poll set and free the
2412 lttng_poll_del(&events
, stream
->wait_fd
);
2414 * This call update the channel states, closes file descriptors
2415 * and securely free the stream.
2417 consumer_del_metadata_stream(stream
, metadata_ht
);
2419 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2423 /* Release RCU lock for the stream looked up */
2431 DBG("Metadata poll thread exiting");
2433 lttng_poll_clean(&events
);
2438 ERR("Health error occurred in %s", __func__
);
2440 health_unregister(health_consumerd
);
2441 rcu_unregister_thread();
2446 * This thread polls the fds in the set to consume the data and write
2447 * it to tracefile if necessary.
2449 void *consumer_thread_data_poll(void *data
)
2451 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2452 struct pollfd
*pollfd
= NULL
;
2453 /* local view of the streams */
2454 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2455 /* local view of consumer_data.fds_count */
2457 struct lttng_consumer_local_data
*ctx
= data
;
2460 rcu_register_thread();
2462 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2464 if (testpoint(consumerd_thread_data
)) {
2465 goto error_testpoint
;
2468 health_code_update();
2470 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2471 if (local_stream
== NULL
) {
2472 PERROR("local_stream malloc");
2477 health_code_update();
2483 * the fds set has been updated, we need to update our
2484 * local array as well
2486 pthread_mutex_lock(&consumer_data
.lock
);
2487 if (consumer_data
.need_update
) {
2492 local_stream
= NULL
;
2495 * Allocate for all fds +1 for the consumer_data_pipe and +1 for
2498 pollfd
= zmalloc((consumer_data
.stream_count
+ 2) * sizeof(struct pollfd
));
2499 if (pollfd
== NULL
) {
2500 PERROR("pollfd malloc");
2501 pthread_mutex_unlock(&consumer_data
.lock
);
2505 local_stream
= zmalloc((consumer_data
.stream_count
+ 2) *
2506 sizeof(struct lttng_consumer_stream
*));
2507 if (local_stream
== NULL
) {
2508 PERROR("local_stream malloc");
2509 pthread_mutex_unlock(&consumer_data
.lock
);
2512 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2515 ERR("Error in allocating pollfd or local_outfds");
2516 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2517 pthread_mutex_unlock(&consumer_data
.lock
);
2521 consumer_data
.need_update
= 0;
2523 pthread_mutex_unlock(&consumer_data
.lock
);
2525 /* No FDs and consumer_quit, consumer_cleanup the thread */
2526 if (nb_fd
== 0 && consumer_quit
== 1) {
2527 err
= 0; /* All is OK */
2530 /* poll on the array of fds */
2532 DBG("polling on %d fd", nb_fd
+ 2);
2533 health_poll_entry();
2534 num_rdy
= poll(pollfd
, nb_fd
+ 2, -1);
2536 DBG("poll num_rdy : %d", num_rdy
);
2537 if (num_rdy
== -1) {
2539 * Restart interrupted system call.
2541 if (errno
== EINTR
) {
2544 PERROR("Poll error");
2545 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2547 } else if (num_rdy
== 0) {
2548 DBG("Polling thread timed out");
2553 * If the consumer_data_pipe triggered poll go directly to the
2554 * beginning of the loop to update the array. We want to prioritize
2555 * array update over low-priority reads.
2557 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2558 ssize_t pipe_readlen
;
2560 DBG("consumer_data_pipe wake up");
2561 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2562 &new_stream
, sizeof(new_stream
));
2563 if (pipe_readlen
< sizeof(new_stream
)) {
2564 PERROR("Consumer data pipe");
2565 /* Continue so we can at least handle the current stream(s). */
2570 * If the stream is NULL, just ignore it. It's also possible that
2571 * the sessiond poll thread changed the consumer_quit state and is
2572 * waking us up to test it.
2574 if (new_stream
== NULL
) {
2575 validate_endpoint_status_data_stream();
2579 /* Continue to update the local streams and handle prio ones */
2583 /* Handle wakeup pipe. */
2584 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2586 ssize_t pipe_readlen
;
2588 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2590 if (pipe_readlen
< 0) {
2591 PERROR("Consumer data wakeup pipe");
2593 /* We've been awakened to handle stream(s). */
2594 ctx
->has_wakeup
= 0;
2597 /* Take care of high priority channels first. */
2598 for (i
= 0; i
< nb_fd
; i
++) {
2599 health_code_update();
2601 if (local_stream
[i
] == NULL
) {
2604 if (pollfd
[i
].revents
& POLLPRI
) {
2605 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2607 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2608 /* it's ok to have an unavailable sub-buffer */
2609 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2610 /* Clean the stream and free it. */
2611 consumer_del_stream(local_stream
[i
], data_ht
);
2612 local_stream
[i
] = NULL
;
2613 } else if (len
> 0) {
2614 local_stream
[i
]->data_read
= 1;
2620 * If we read high prio channel in this loop, try again
2621 * for more high prio data.
2627 /* Take care of low priority channels. */
2628 for (i
= 0; i
< nb_fd
; i
++) {
2629 health_code_update();
2631 if (local_stream
[i
] == NULL
) {
2634 if ((pollfd
[i
].revents
& POLLIN
) ||
2635 local_stream
[i
]->hangup_flush_done
||
2636 local_stream
[i
]->has_data
) {
2637 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2638 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2639 /* it's ok to have an unavailable sub-buffer */
2640 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2641 /* Clean the stream and free it. */
2642 consumer_del_stream(local_stream
[i
], data_ht
);
2643 local_stream
[i
] = NULL
;
2644 } else if (len
> 0) {
2645 local_stream
[i
]->data_read
= 1;
2650 /* Handle hangup and errors */
2651 for (i
= 0; i
< nb_fd
; i
++) {
2652 health_code_update();
2654 if (local_stream
[i
] == NULL
) {
2657 if (!local_stream
[i
]->hangup_flush_done
2658 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2659 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2660 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2661 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2663 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2664 /* Attempt read again, for the data we just flushed. */
2665 local_stream
[i
]->data_read
= 1;
2668 * If the poll flag is HUP/ERR/NVAL and we have
2669 * read no data in this pass, we can remove the
2670 * stream from its hash table.
2672 if ((pollfd
[i
].revents
& POLLHUP
)) {
2673 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2674 if (!local_stream
[i
]->data_read
) {
2675 consumer_del_stream(local_stream
[i
], data_ht
);
2676 local_stream
[i
] = NULL
;
2679 } else if (pollfd
[i
].revents
& POLLERR
) {
2680 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2681 if (!local_stream
[i
]->data_read
) {
2682 consumer_del_stream(local_stream
[i
], data_ht
);
2683 local_stream
[i
] = NULL
;
2686 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2687 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2688 if (!local_stream
[i
]->data_read
) {
2689 consumer_del_stream(local_stream
[i
], data_ht
);
2690 local_stream
[i
] = NULL
;
2694 if (local_stream
[i
] != NULL
) {
2695 local_stream
[i
]->data_read
= 0;
2702 DBG("polling thread exiting");
2707 * Close the write side of the pipe so epoll_wait() in
2708 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2709 * read side of the pipe. If we close them both, epoll_wait strangely does
2710 * not return and could create a endless wait period if the pipe is the
2711 * only tracked fd in the poll set. The thread will take care of closing
2714 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2719 ERR("Health error occurred in %s", __func__
);
2721 health_unregister(health_consumerd
);
2723 rcu_unregister_thread();
2728 * Close wake-up end of each stream belonging to the channel. This will
2729 * allow the poll() on the stream read-side to detect when the
2730 * write-side (application) finally closes them.
2733 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2735 struct lttng_ht
*ht
;
2736 struct lttng_consumer_stream
*stream
;
2737 struct lttng_ht_iter iter
;
2739 ht
= consumer_data
.stream_per_chan_id_ht
;
2742 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2743 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2744 ht
->match_fct
, &channel
->key
,
2745 &iter
.iter
, stream
, node_channel_id
.node
) {
2747 * Protect against teardown with mutex.
2749 pthread_mutex_lock(&stream
->lock
);
2750 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2753 switch (consumer_data
.type
) {
2754 case LTTNG_CONSUMER_KERNEL
:
2756 case LTTNG_CONSUMER32_UST
:
2757 case LTTNG_CONSUMER64_UST
:
2758 if (stream
->metadata_flag
) {
2759 /* Safe and protected by the stream lock. */
2760 lttng_ustconsumer_close_metadata(stream
->chan
);
2763 * Note: a mutex is taken internally within
2764 * liblttng-ust-ctl to protect timer wakeup_fd
2765 * use from concurrent close.
2767 lttng_ustconsumer_close_stream_wakeup(stream
);
2771 ERR("Unknown consumer_data type");
2775 pthread_mutex_unlock(&stream
->lock
);
2780 static void destroy_channel_ht(struct lttng_ht
*ht
)
2782 struct lttng_ht_iter iter
;
2783 struct lttng_consumer_channel
*channel
;
2791 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2792 ret
= lttng_ht_del(ht
, &iter
);
2797 lttng_ht_destroy(ht
);
2801 * This thread polls the channel fds to detect when they are being
2802 * closed. It closes all related streams if the channel is detected as
2803 * closed. It is currently only used as a shim layer for UST because the
2804 * consumerd needs to keep the per-stream wakeup end of pipes open for
2807 void *consumer_thread_channel_poll(void *data
)
2809 int ret
, i
, pollfd
, err
= -1;
2810 uint32_t revents
, nb_fd
;
2811 struct lttng_consumer_channel
*chan
= NULL
;
2812 struct lttng_ht_iter iter
;
2813 struct lttng_ht_node_u64
*node
;
2814 struct lttng_poll_event events
;
2815 struct lttng_consumer_local_data
*ctx
= data
;
2816 struct lttng_ht
*channel_ht
;
2818 rcu_register_thread();
2820 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2822 if (testpoint(consumerd_thread_channel
)) {
2823 goto error_testpoint
;
2826 health_code_update();
2828 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2830 /* ENOMEM at this point. Better to bail out. */
2834 DBG("Thread channel poll started");
2836 /* Size is set to 1 for the consumer_channel pipe */
2837 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2839 ERR("Poll set creation failed");
2843 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2849 DBG("Channel main loop started");
2853 health_code_update();
2854 DBG("Channel poll wait");
2855 health_poll_entry();
2856 ret
= lttng_poll_wait(&events
, -1);
2857 DBG("Channel poll return from wait with %d fd(s)",
2858 LTTNG_POLL_GETNB(&events
));
2860 DBG("Channel event caught in thread");
2862 if (errno
== EINTR
) {
2863 ERR("Poll EINTR caught");
2866 if (LTTNG_POLL_GETNB(&events
) == 0) {
2867 err
= 0; /* All is OK */
2874 /* From here, the event is a channel wait fd */
2875 for (i
= 0; i
< nb_fd
; i
++) {
2876 health_code_update();
2878 revents
= LTTNG_POLL_GETEV(&events
, i
);
2879 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2882 /* No activity for this FD (poll implementation). */
2886 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2887 if (revents
& LPOLLIN
) {
2888 enum consumer_channel_action action
;
2891 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2894 ERR("Error reading channel pipe");
2896 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2901 case CONSUMER_CHANNEL_ADD
:
2902 DBG("Adding channel %d to poll set",
2905 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2908 lttng_ht_add_unique_u64(channel_ht
,
2909 &chan
->wait_fd_node
);
2911 /* Add channel to the global poll events list */
2912 lttng_poll_add(&events
, chan
->wait_fd
,
2913 LPOLLERR
| LPOLLHUP
);
2915 case CONSUMER_CHANNEL_DEL
:
2918 * This command should never be called if the channel
2919 * has streams monitored by either the data or metadata
2920 * thread. The consumer only notify this thread with a
2921 * channel del. command if it receives a destroy
2922 * channel command from the session daemon that send it
2923 * if a command prior to the GET_CHANNEL failed.
2927 chan
= consumer_find_channel(key
);
2930 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2933 lttng_poll_del(&events
, chan
->wait_fd
);
2934 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2935 ret
= lttng_ht_del(channel_ht
, &iter
);
2938 switch (consumer_data
.type
) {
2939 case LTTNG_CONSUMER_KERNEL
:
2941 case LTTNG_CONSUMER32_UST
:
2942 case LTTNG_CONSUMER64_UST
:
2943 health_code_update();
2944 /* Destroy streams that might have been left in the stream list. */
2945 clean_channel_stream_list(chan
);
2948 ERR("Unknown consumer_data type");
2953 * Release our own refcount. Force channel deletion even if
2954 * streams were not initialized.
2956 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2957 consumer_del_channel(chan
);
2962 case CONSUMER_CHANNEL_QUIT
:
2964 * Remove the pipe from the poll set and continue the loop
2965 * since their might be data to consume.
2967 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2970 ERR("Unknown action");
2973 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2974 DBG("Channel thread pipe hung up");
2976 * Remove the pipe from the poll set and continue the loop
2977 * since their might be data to consume.
2979 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2982 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2986 /* Handle other stream */
2992 uint64_t tmp_id
= (uint64_t) pollfd
;
2994 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2996 node
= lttng_ht_iter_get_node_u64(&iter
);
2999 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
3002 /* Check for error event */
3003 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3004 DBG("Channel fd %d is hup|err.", pollfd
);
3006 lttng_poll_del(&events
, chan
->wait_fd
);
3007 ret
= lttng_ht_del(channel_ht
, &iter
);
3011 * This will close the wait fd for each stream associated to
3012 * this channel AND monitored by the data/metadata thread thus
3013 * will be clean by the right thread.
3015 consumer_close_channel_streams(chan
);
3017 /* Release our own refcount */
3018 if (!uatomic_sub_return(&chan
->refcount
, 1)
3019 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3020 consumer_del_channel(chan
);
3023 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3028 /* Release RCU lock for the channel looked up */
3036 lttng_poll_clean(&events
);
3038 destroy_channel_ht(channel_ht
);
3041 DBG("Channel poll thread exiting");
3044 ERR("Health error occurred in %s", __func__
);
3046 health_unregister(health_consumerd
);
3047 rcu_unregister_thread();
3051 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3052 struct pollfd
*sockpoll
, int client_socket
)
3059 ret
= lttng_consumer_poll_socket(sockpoll
);
3063 DBG("Metadata connection on client_socket");
3065 /* Blocking call, waiting for transmission */
3066 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3067 if (ctx
->consumer_metadata_socket
< 0) {
3068 WARN("On accept metadata");
3079 * This thread listens on the consumerd socket and receives the file
3080 * descriptors from the session daemon.
3082 void *consumer_thread_sessiond_poll(void *data
)
3084 int sock
= -1, client_socket
, ret
, err
= -1;
3086 * structure to poll for incoming data on communication socket avoids
3087 * making blocking sockets.
3089 struct pollfd consumer_sockpoll
[2];
3090 struct lttng_consumer_local_data
*ctx
= data
;
3092 rcu_register_thread();
3094 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3096 if (testpoint(consumerd_thread_sessiond
)) {
3097 goto error_testpoint
;
3100 health_code_update();
3102 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3103 unlink(ctx
->consumer_command_sock_path
);
3104 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3105 if (client_socket
< 0) {
3106 ERR("Cannot create command socket");
3110 ret
= lttcomm_listen_unix_sock(client_socket
);
3115 DBG("Sending ready command to lttng-sessiond");
3116 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3117 /* return < 0 on error, but == 0 is not fatal */
3119 ERR("Error sending ready command to lttng-sessiond");
3123 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3124 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3125 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3126 consumer_sockpoll
[1].fd
= client_socket
;
3127 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3129 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3137 DBG("Connection on client_socket");
3139 /* Blocking call, waiting for transmission */
3140 sock
= lttcomm_accept_unix_sock(client_socket
);
3147 * Setup metadata socket which is the second socket connection on the
3148 * command unix socket.
3150 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3159 /* This socket is not useful anymore. */
3160 ret
= close(client_socket
);
3162 PERROR("close client_socket");
3166 /* update the polling structure to poll on the established socket */
3167 consumer_sockpoll
[1].fd
= sock
;
3168 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3171 health_code_update();
3173 health_poll_entry();
3174 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3183 DBG("Incoming command on sock");
3184 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3187 * This could simply be a session daemon quitting. Don't output
3190 DBG("Communication interrupted on command socket");
3194 if (consumer_quit
) {
3195 DBG("consumer_thread_receive_fds received quit from signal");
3196 err
= 0; /* All is OK */
3199 DBG("received command on sock");
3205 DBG("Consumer thread sessiond poll exiting");
3208 * Close metadata streams since the producer is the session daemon which
3211 * NOTE: for now, this only applies to the UST tracer.
3213 lttng_consumer_close_all_metadata();
3216 * when all fds have hung up, the polling thread
3222 * Notify the data poll thread to poll back again and test the
3223 * consumer_quit state that we just set so to quit gracefully.
3225 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3227 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3229 notify_health_quit_pipe(health_quit_pipe
);
3231 /* Cleaning up possibly open sockets. */
3235 PERROR("close sock sessiond poll");
3238 if (client_socket
>= 0) {
3239 ret
= close(client_socket
);
3241 PERROR("close client_socket sessiond poll");
3248 ERR("Health error occurred in %s", __func__
);
3250 health_unregister(health_consumerd
);
3252 rcu_unregister_thread();
3256 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3257 struct lttng_consumer_local_data
*ctx
)
3261 pthread_mutex_lock(&stream
->lock
);
3262 if (stream
->metadata_flag
) {
3263 pthread_mutex_lock(&stream
->metadata_rdv_lock
);
3266 switch (consumer_data
.type
) {
3267 case LTTNG_CONSUMER_KERNEL
:
3268 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3270 case LTTNG_CONSUMER32_UST
:
3271 case LTTNG_CONSUMER64_UST
:
3272 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3275 ERR("Unknown consumer_data type");
3281 if (stream
->metadata_flag
) {
3282 pthread_cond_broadcast(&stream
->metadata_rdv
);
3283 pthread_mutex_unlock(&stream
->metadata_rdv_lock
);
3285 pthread_mutex_unlock(&stream
->lock
);
3289 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3291 switch (consumer_data
.type
) {
3292 case LTTNG_CONSUMER_KERNEL
:
3293 return lttng_kconsumer_on_recv_stream(stream
);
3294 case LTTNG_CONSUMER32_UST
:
3295 case LTTNG_CONSUMER64_UST
:
3296 return lttng_ustconsumer_on_recv_stream(stream
);
3298 ERR("Unknown consumer_data type");
3305 * Allocate and set consumer data hash tables.
3307 int lttng_consumer_init(void)
3309 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3310 if (!consumer_data
.channel_ht
) {
3314 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3315 if (!consumer_data
.relayd_ht
) {
3319 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3320 if (!consumer_data
.stream_list_ht
) {
3324 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3325 if (!consumer_data
.stream_per_chan_id_ht
) {
3329 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3334 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3346 * Process the ADD_RELAYD command receive by a consumer.
3348 * This will create a relayd socket pair and add it to the relayd hash table.
3349 * The caller MUST acquire a RCU read side lock before calling it.
3351 int consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3352 struct lttng_consumer_local_data
*ctx
, int sock
,
3353 struct pollfd
*consumer_sockpoll
,
3354 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3355 uint64_t relayd_session_id
)
3357 int fd
= -1, ret
= -1, relayd_created
= 0;
3358 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3359 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3362 assert(relayd_sock
);
3364 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3366 /* Get relayd reference if exists. */
3367 relayd
= consumer_find_relayd(net_seq_idx
);
3368 if (relayd
== NULL
) {
3369 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3370 /* Not found. Allocate one. */
3371 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3372 if (relayd
== NULL
) {
3374 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3377 relayd
->sessiond_session_id
= sessiond_id
;
3382 * This code path MUST continue to the consumer send status message to
3383 * we can notify the session daemon and continue our work without
3384 * killing everything.
3388 * relayd key should never be found for control socket.
3390 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3393 /* First send a status message before receiving the fds. */
3394 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3396 /* Somehow, the session daemon is not responding anymore. */
3397 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3398 goto error_nosignal
;
3401 /* Poll on consumer socket. */
3402 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3404 /* Needing to exit in the middle of a command: error. */
3405 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3407 goto error_nosignal
;
3410 /* Get relayd socket from session daemon */
3411 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3412 if (ret
!= sizeof(fd
)) {
3414 fd
= -1; /* Just in case it gets set with an invalid value. */
3417 * Failing to receive FDs might indicate a major problem such as
3418 * reaching a fd limit during the receive where the kernel returns a
3419 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3420 * don't take any chances and stop everything.
3422 * XXX: Feature request #558 will fix that and avoid this possible
3423 * issue when reaching the fd limit.
3425 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3426 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3430 /* Copy socket information and received FD */
3431 switch (sock_type
) {
3432 case LTTNG_STREAM_CONTROL
:
3433 /* Copy received lttcomm socket */
3434 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3435 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3436 /* Handle create_sock error. */
3438 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3442 * Close the socket created internally by
3443 * lttcomm_create_sock, so we can replace it by the one
3444 * received from sessiond.
3446 if (close(relayd
->control_sock
.sock
.fd
)) {
3450 /* Assign new file descriptor */
3451 relayd
->control_sock
.sock
.fd
= fd
;
3452 fd
= -1; /* For error path */
3453 /* Assign version values. */
3454 relayd
->control_sock
.major
= relayd_sock
->major
;
3455 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3457 relayd
->relayd_session_id
= relayd_session_id
;
3460 case LTTNG_STREAM_DATA
:
3461 /* Copy received lttcomm socket */
3462 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3463 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3464 /* Handle create_sock error. */
3466 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3470 * Close the socket created internally by
3471 * lttcomm_create_sock, so we can replace it by the one
3472 * received from sessiond.
3474 if (close(relayd
->data_sock
.sock
.fd
)) {
3478 /* Assign new file descriptor */
3479 relayd
->data_sock
.sock
.fd
= fd
;
3480 fd
= -1; /* for eventual error paths */
3481 /* Assign version values. */
3482 relayd
->data_sock
.major
= relayd_sock
->major
;
3483 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3486 ERR("Unknown relayd socket type (%d)", sock_type
);
3488 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3492 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3493 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3494 relayd
->net_seq_idx
, fd
);
3496 /* We successfully added the socket. Send status back. */
3497 ret
= consumer_send_status_msg(sock
, ret_code
);
3499 /* Somehow, the session daemon is not responding anymore. */
3500 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3501 goto error_nosignal
;
3505 * Add relayd socket pair to consumer data hashtable. If object already
3506 * exists or on error, the function gracefully returns.
3514 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3515 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3519 /* Close received socket if valid. */
3522 PERROR("close received socket");
3526 if (relayd_created
) {
3534 * Try to lock the stream mutex.
3536 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3538 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3545 * Try to lock the stream mutex. On failure, we know that the stream is
3546 * being used else where hence there is data still being extracted.
3548 ret
= pthread_mutex_trylock(&stream
->lock
);
3550 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3562 * Search for a relayd associated to the session id and return the reference.
3564 * A rcu read side lock MUST be acquire before calling this function and locked
3565 * until the relayd object is no longer necessary.
3567 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3569 struct lttng_ht_iter iter
;
3570 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3572 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3573 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3576 * Check by sessiond id which is unique here where the relayd session
3577 * id might not be when having multiple relayd.
3579 if (relayd
->sessiond_session_id
== id
) {
3580 /* Found the relayd. There can be only one per id. */
3592 * Check if for a given session id there is still data needed to be extract
3595 * Return 1 if data is pending or else 0 meaning ready to be read.
3597 int consumer_data_pending(uint64_t id
)
3600 struct lttng_ht_iter iter
;
3601 struct lttng_ht
*ht
;
3602 struct lttng_consumer_stream
*stream
;
3603 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3604 int (*data_pending
)(struct lttng_consumer_stream
*);
3606 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3609 pthread_mutex_lock(&consumer_data
.lock
);
3611 switch (consumer_data
.type
) {
3612 case LTTNG_CONSUMER_KERNEL
:
3613 data_pending
= lttng_kconsumer_data_pending
;
3615 case LTTNG_CONSUMER32_UST
:
3616 case LTTNG_CONSUMER64_UST
:
3617 data_pending
= lttng_ustconsumer_data_pending
;
3620 ERR("Unknown consumer data type");
3624 /* Ease our life a bit */
3625 ht
= consumer_data
.stream_list_ht
;
3627 relayd
= find_relayd_by_session_id(id
);
3629 /* Send init command for data pending. */
3630 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3631 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3632 relayd
->relayd_session_id
);
3633 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3635 /* Communication error thus the relayd so no data pending. */
3636 goto data_not_pending
;
3640 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3641 ht
->hash_fct(&id
, lttng_ht_seed
),
3643 &iter
.iter
, stream
, node_session_id
.node
) {
3644 /* If this call fails, the stream is being used hence data pending. */
3645 ret
= stream_try_lock(stream
);
3651 * A removed node from the hash table indicates that the stream has
3652 * been deleted thus having a guarantee that the buffers are closed
3653 * on the consumer side. However, data can still be transmitted
3654 * over the network so don't skip the relayd check.
3656 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3658 /* Check the stream if there is data in the buffers. */
3659 ret
= data_pending(stream
);
3661 pthread_mutex_unlock(&stream
->lock
);
3668 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3669 if (stream
->metadata_flag
) {
3670 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3671 stream
->relayd_stream_id
);
3673 ret
= relayd_data_pending(&relayd
->control_sock
,
3674 stream
->relayd_stream_id
,
3675 stream
->next_net_seq_num
- 1);
3677 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3679 pthread_mutex_unlock(&stream
->lock
);
3683 pthread_mutex_unlock(&stream
->lock
);
3687 unsigned int is_data_inflight
= 0;
3689 /* Send init command for data pending. */
3690 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3691 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3692 relayd
->relayd_session_id
, &is_data_inflight
);
3693 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3695 goto data_not_pending
;
3697 if (is_data_inflight
) {
3703 * Finding _no_ node in the hash table and no inflight data means that the
3704 * stream(s) have been removed thus data is guaranteed to be available for
3705 * analysis from the trace files.
3709 /* Data is available to be read by a viewer. */
3710 pthread_mutex_unlock(&consumer_data
.lock
);
3715 /* Data is still being extracted from buffers. */
3716 pthread_mutex_unlock(&consumer_data
.lock
);
3722 * Send a ret code status message to the sessiond daemon.
3724 * Return the sendmsg() return value.
3726 int consumer_send_status_msg(int sock
, int ret_code
)
3728 struct lttcomm_consumer_status_msg msg
;
3730 memset(&msg
, 0, sizeof(msg
));
3731 msg
.ret_code
= ret_code
;
3733 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3737 * Send a channel status message to the sessiond daemon.
3739 * Return the sendmsg() return value.
3741 int consumer_send_status_channel(int sock
,
3742 struct lttng_consumer_channel
*channel
)
3744 struct lttcomm_consumer_status_channel msg
;
3748 memset(&msg
, 0, sizeof(msg
));
3750 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3752 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3753 msg
.key
= channel
->key
;
3754 msg
.stream_count
= channel
->streams
.count
;
3757 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3760 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3761 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3762 uint64_t max_sb_size
)
3764 unsigned long start_pos
;
3766 if (!nb_packets_per_stream
) {
3767 return consumed_pos
; /* Grab everything */
3769 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
3770 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3771 if ((long) (start_pos
- consumed_pos
) < 0) {
3772 return consumed_pos
; /* Grab everything */