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 */
70 /* Flag used to temporarily pause data consumption from testpoints. */
71 int data_consumption_paused
;
74 * Flag to inform the polling thread to quit when all fd hung up. Updated by
75 * the consumer_thread_receive_fds when it notices that all fds has hung up.
76 * Also updated by the signal handler (consumer_should_exit()). Read by the
82 * Global hash table containing respectively metadata and data streams. The
83 * stream element in this ht should only be updated by the metadata poll thread
84 * for the metadata and the data poll thread for the data.
86 static struct lttng_ht
*metadata_ht
;
87 static struct lttng_ht
*data_ht
;
90 * Notify a thread lttng pipe to poll back again. This usually means that some
91 * global state has changed so we just send back the thread in a poll wait
94 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
96 struct lttng_consumer_stream
*null_stream
= NULL
;
100 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
103 static void notify_health_quit_pipe(int *pipe
)
107 ret
= lttng_write(pipe
[1], "4", 1);
109 PERROR("write consumer health quit");
113 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
114 struct lttng_consumer_channel
*chan
,
116 enum consumer_channel_action action
)
118 struct consumer_channel_msg msg
;
121 memset(&msg
, 0, sizeof(msg
));
126 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
127 if (ret
< sizeof(msg
)) {
128 PERROR("notify_channel_pipe write error");
132 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
135 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
138 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
139 struct lttng_consumer_channel
**chan
,
141 enum consumer_channel_action
*action
)
143 struct consumer_channel_msg msg
;
146 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
147 if (ret
< sizeof(msg
)) {
151 *action
= msg
.action
;
159 * Cleanup the stream list of a channel. Those streams are not yet globally
162 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
164 struct lttng_consumer_stream
*stream
, *stmp
;
168 /* Delete streams that might have been left in the stream list. */
169 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
171 cds_list_del(&stream
->send_node
);
173 * Once a stream is added to this list, the buffers were created so we
174 * have a guarantee that this call will succeed. Setting the monitor
175 * mode to 0 so we don't lock nor try to delete the stream from the
179 consumer_stream_destroy(stream
, NULL
);
184 * Find a stream. The consumer_data.lock must be locked during this
187 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
190 struct lttng_ht_iter iter
;
191 struct lttng_ht_node_u64
*node
;
192 struct lttng_consumer_stream
*stream
= NULL
;
196 /* -1ULL keys are lookup failures */
197 if (key
== (uint64_t) -1ULL) {
203 lttng_ht_lookup(ht
, &key
, &iter
);
204 node
= lttng_ht_iter_get_node_u64(&iter
);
206 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
214 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
216 struct lttng_consumer_stream
*stream
;
219 stream
= find_stream(key
, ht
);
221 stream
->key
= (uint64_t) -1ULL;
223 * We don't want the lookup to match, but we still need
224 * to iterate on this stream when iterating over the hash table. Just
225 * change the node key.
227 stream
->node
.key
= (uint64_t) -1ULL;
233 * Return a channel object for the given key.
235 * RCU read side lock MUST be acquired before calling this function and
236 * protects the channel ptr.
238 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
240 struct lttng_ht_iter iter
;
241 struct lttng_ht_node_u64
*node
;
242 struct lttng_consumer_channel
*channel
= NULL
;
244 /* -1ULL keys are lookup failures */
245 if (key
== (uint64_t) -1ULL) {
249 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
250 node
= lttng_ht_iter_get_node_u64(&iter
);
252 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
259 * There is a possibility that the consumer does not have enough time between
260 * the close of the channel on the session daemon and the cleanup in here thus
261 * once we have a channel add with an existing key, we know for sure that this
262 * channel will eventually get cleaned up by all streams being closed.
264 * This function just nullifies the already existing channel key.
266 static void steal_channel_key(uint64_t key
)
268 struct lttng_consumer_channel
*channel
;
271 channel
= consumer_find_channel(key
);
273 channel
->key
= (uint64_t) -1ULL;
275 * We don't want the lookup to match, but we still need to iterate on
276 * this channel when iterating over the hash table. Just change the
279 channel
->node
.key
= (uint64_t) -1ULL;
284 static void free_channel_rcu(struct rcu_head
*head
)
286 struct lttng_ht_node_u64
*node
=
287 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
288 struct lttng_consumer_channel
*channel
=
289 caa_container_of(node
, struct lttng_consumer_channel
, node
);
291 switch (consumer_data
.type
) {
292 case LTTNG_CONSUMER_KERNEL
:
294 case LTTNG_CONSUMER32_UST
:
295 case LTTNG_CONSUMER64_UST
:
296 lttng_ustconsumer_free_channel(channel
);
299 ERR("Unknown consumer_data type");
306 * RCU protected relayd socket pair free.
308 static void free_relayd_rcu(struct rcu_head
*head
)
310 struct lttng_ht_node_u64
*node
=
311 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
312 struct consumer_relayd_sock_pair
*relayd
=
313 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
316 * Close all sockets. This is done in the call RCU since we don't want the
317 * socket fds to be reassigned thus potentially creating bad state of the
320 * We do not have to lock the control socket mutex here since at this stage
321 * there is no one referencing to this relayd object.
323 (void) relayd_close(&relayd
->control_sock
);
324 (void) relayd_close(&relayd
->data_sock
);
330 * Destroy and free relayd socket pair object.
332 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
335 struct lttng_ht_iter iter
;
337 if (relayd
== NULL
) {
341 DBG("Consumer destroy and close relayd socket pair");
343 iter
.iter
.node
= &relayd
->node
.node
;
344 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
346 /* We assume the relayd is being or is destroyed */
350 /* RCU free() call */
351 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
355 * Remove a channel from the global list protected by a mutex. This function is
356 * also responsible for freeing its data structures.
358 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
361 struct lttng_ht_iter iter
;
363 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
365 pthread_mutex_lock(&consumer_data
.lock
);
366 pthread_mutex_lock(&channel
->lock
);
368 /* Destroy streams that might have been left in the stream list. */
369 clean_channel_stream_list(channel
);
371 if (channel
->live_timer_enabled
== 1) {
372 consumer_timer_live_stop(channel
);
374 if (channel
->monitor_timer_enabled
== 1) {
375 consumer_timer_monitor_stop(channel
);
378 switch (consumer_data
.type
) {
379 case LTTNG_CONSUMER_KERNEL
:
381 case LTTNG_CONSUMER32_UST
:
382 case LTTNG_CONSUMER64_UST
:
383 lttng_ustconsumer_del_channel(channel
);
386 ERR("Unknown consumer_data type");
392 iter
.iter
.node
= &channel
->node
.node
;
393 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
397 call_rcu(&channel
->node
.head
, free_channel_rcu
);
399 pthread_mutex_unlock(&channel
->lock
);
400 pthread_mutex_unlock(&consumer_data
.lock
);
404 * Iterate over the relayd hash table and destroy each element. Finally,
405 * destroy the whole hash table.
407 static void cleanup_relayd_ht(void)
409 struct lttng_ht_iter iter
;
410 struct consumer_relayd_sock_pair
*relayd
;
414 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
416 consumer_destroy_relayd(relayd
);
421 lttng_ht_destroy(consumer_data
.relayd_ht
);
425 * Update the end point status of all streams having the given network sequence
426 * index (relayd index).
428 * It's atomically set without having the stream mutex locked which is fine
429 * because we handle the write/read race with a pipe wakeup for each thread.
431 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
432 enum consumer_endpoint_status status
)
434 struct lttng_ht_iter iter
;
435 struct lttng_consumer_stream
*stream
;
437 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
441 /* Let's begin with metadata */
442 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
443 if (stream
->net_seq_idx
== net_seq_idx
) {
444 uatomic_set(&stream
->endpoint_status
, status
);
445 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
449 /* Follow up by the data streams */
450 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
451 if (stream
->net_seq_idx
== net_seq_idx
) {
452 uatomic_set(&stream
->endpoint_status
, status
);
453 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
460 * Cleanup a relayd object by flagging every associated streams for deletion,
461 * destroying the object meaning removing it from the relayd hash table,
462 * closing the sockets and freeing the memory in a RCU call.
464 * If a local data context is available, notify the threads that the streams'
465 * state have changed.
467 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
468 struct lttng_consumer_local_data
*ctx
)
474 DBG("Cleaning up relayd sockets");
476 /* Save the net sequence index before destroying the object */
477 netidx
= relayd
->net_seq_idx
;
480 * Delete the relayd from the relayd hash table, close the sockets and free
481 * the object in a RCU call.
483 consumer_destroy_relayd(relayd
);
485 /* Set inactive endpoint to all streams */
486 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
489 * With a local data context, notify the threads that the streams' state
490 * have changed. The write() action on the pipe acts as an "implicit"
491 * memory barrier ordering the updates of the end point status from the
492 * read of this status which happens AFTER receiving this notify.
495 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
496 notify_thread_lttng_pipe(ctx
->consumer_metadata_pipe
);
501 * Flag a relayd socket pair for destruction. Destroy it if the refcount
504 * RCU read side lock MUST be aquired before calling this function.
506 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
510 /* Set destroy flag for this object */
511 uatomic_set(&relayd
->destroy_flag
, 1);
513 /* Destroy the relayd if refcount is 0 */
514 if (uatomic_read(&relayd
->refcount
) == 0) {
515 consumer_destroy_relayd(relayd
);
520 * Completly destroy stream from every visiable data structure and the given
523 * One this call returns, the stream object is not longer usable nor visible.
525 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
528 consumer_stream_destroy(stream
, ht
);
532 * XXX naming of del vs destroy is all mixed up.
534 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
536 consumer_stream_destroy(stream
, data_ht
);
539 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
541 consumer_stream_destroy(stream
, metadata_ht
);
544 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
546 enum lttng_consumer_stream_state state
,
547 const char *channel_name
,
554 enum consumer_channel_type type
,
555 unsigned int monitor
)
558 struct lttng_consumer_stream
*stream
;
560 stream
= zmalloc(sizeof(*stream
));
561 if (stream
== NULL
) {
562 PERROR("malloc struct lttng_consumer_stream");
569 stream
->key
= stream_key
;
571 stream
->out_fd_offset
= 0;
572 stream
->output_written
= 0;
573 stream
->state
= state
;
576 stream
->net_seq_idx
= relayd_id
;
577 stream
->session_id
= session_id
;
578 stream
->monitor
= monitor
;
579 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
580 stream
->index_file
= NULL
;
581 stream
->last_sequence_number
= -1ULL;
582 pthread_mutex_init(&stream
->lock
, NULL
);
583 pthread_mutex_init(&stream
->metadata_timer_lock
, NULL
);
585 /* If channel is the metadata, flag this stream as metadata. */
586 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
587 stream
->metadata_flag
= 1;
588 /* Metadata is flat out. */
589 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
590 /* Live rendez-vous point. */
591 pthread_cond_init(&stream
->metadata_rdv
, NULL
);
592 pthread_mutex_init(&stream
->metadata_rdv_lock
, NULL
);
594 /* Format stream name to <channel_name>_<cpu_number> */
595 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
598 PERROR("snprintf stream name");
603 /* Key is always the wait_fd for streams. */
604 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
606 /* Init node per channel id key */
607 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
609 /* Init session id node with the stream session id */
610 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
612 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
613 " relayd_id %" PRIu64
", session_id %" PRIu64
,
614 stream
->name
, stream
->key
, channel_key
,
615 stream
->net_seq_idx
, stream
->session_id
);
631 * Add a stream to the global list protected by a mutex.
633 int consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
635 struct lttng_ht
*ht
= data_ht
;
641 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
643 pthread_mutex_lock(&consumer_data
.lock
);
644 pthread_mutex_lock(&stream
->chan
->lock
);
645 pthread_mutex_lock(&stream
->chan
->timer_lock
);
646 pthread_mutex_lock(&stream
->lock
);
649 /* Steal stream identifier to avoid having streams with the same key */
650 steal_stream_key(stream
->key
, ht
);
652 lttng_ht_add_unique_u64(ht
, &stream
->node
);
654 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
655 &stream
->node_channel_id
);
658 * Add stream to the stream_list_ht of the consumer data. No need to steal
659 * the key since the HT does not use it and we allow to add redundant keys
662 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
665 * When nb_init_stream_left reaches 0, we don't need to trigger any action
666 * in terms of destroying the associated channel, because the action that
667 * causes the count to become 0 also causes a stream to be added. The
668 * channel deletion will thus be triggered by the following removal of this
671 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
672 /* Increment refcount before decrementing nb_init_stream_left */
674 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
677 /* Update consumer data once the node is inserted. */
678 consumer_data
.stream_count
++;
679 consumer_data
.need_update
= 1;
682 pthread_mutex_unlock(&stream
->lock
);
683 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
684 pthread_mutex_unlock(&stream
->chan
->lock
);
685 pthread_mutex_unlock(&consumer_data
.lock
);
690 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
692 consumer_del_stream(stream
, data_ht
);
696 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
697 * be acquired before calling this.
699 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
702 struct lttng_ht_node_u64
*node
;
703 struct lttng_ht_iter iter
;
707 lttng_ht_lookup(consumer_data
.relayd_ht
,
708 &relayd
->net_seq_idx
, &iter
);
709 node
= lttng_ht_iter_get_node_u64(&iter
);
713 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
720 * Allocate and return a consumer relayd socket.
722 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
723 uint64_t net_seq_idx
)
725 struct consumer_relayd_sock_pair
*obj
= NULL
;
727 /* net sequence index of -1 is a failure */
728 if (net_seq_idx
== (uint64_t) -1ULL) {
732 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
734 PERROR("zmalloc relayd sock");
738 obj
->net_seq_idx
= net_seq_idx
;
740 obj
->destroy_flag
= 0;
741 obj
->control_sock
.sock
.fd
= -1;
742 obj
->data_sock
.sock
.fd
= -1;
743 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
744 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
751 * Find a relayd socket pair in the global consumer data.
753 * Return the object if found else NULL.
754 * RCU read-side lock must be held across this call and while using the
757 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
759 struct lttng_ht_iter iter
;
760 struct lttng_ht_node_u64
*node
;
761 struct consumer_relayd_sock_pair
*relayd
= NULL
;
763 /* Negative keys are lookup failures */
764 if (key
== (uint64_t) -1ULL) {
768 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
770 node
= lttng_ht_iter_get_node_u64(&iter
);
772 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
780 * Find a relayd and send the stream
782 * Returns 0 on success, < 0 on error
784 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
788 struct consumer_relayd_sock_pair
*relayd
;
791 assert(stream
->net_seq_idx
!= -1ULL);
794 /* The stream is not metadata. Get relayd reference if exists. */
796 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
797 if (relayd
!= NULL
) {
798 /* Add stream on the relayd */
799 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
800 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
801 path
, &stream
->relayd_stream_id
,
802 stream
->chan
->tracefile_size
, stream
->chan
->tracefile_count
);
803 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
808 uatomic_inc(&relayd
->refcount
);
809 stream
->sent_to_relayd
= 1;
811 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
812 stream
->key
, stream
->net_seq_idx
);
817 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
818 stream
->name
, stream
->key
, stream
->net_seq_idx
);
826 * Find a relayd and send the streams sent message
828 * Returns 0 on success, < 0 on error
830 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
833 struct consumer_relayd_sock_pair
*relayd
;
835 assert(net_seq_idx
!= -1ULL);
837 /* The stream is not metadata. Get relayd reference if exists. */
839 relayd
= consumer_find_relayd(net_seq_idx
);
840 if (relayd
!= NULL
) {
841 /* Add stream on the relayd */
842 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
843 ret
= relayd_streams_sent(&relayd
->control_sock
);
844 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
849 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
856 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
864 * Find a relayd and close the stream
866 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
868 struct consumer_relayd_sock_pair
*relayd
;
870 /* The stream is not metadata. Get relayd reference if exists. */
872 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
874 consumer_stream_relayd_close(stream
, relayd
);
880 * Handle stream for relayd transmission if the stream applies for network
881 * streaming where the net sequence index is set.
883 * Return destination file descriptor or negative value on error.
885 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
886 size_t data_size
, unsigned long padding
,
887 struct consumer_relayd_sock_pair
*relayd
)
890 struct lttcomm_relayd_data_hdr data_hdr
;
896 /* Reset data header */
897 memset(&data_hdr
, 0, sizeof(data_hdr
));
899 if (stream
->metadata_flag
) {
900 /* Caller MUST acquire the relayd control socket lock */
901 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
906 /* Metadata are always sent on the control socket. */
907 outfd
= relayd
->control_sock
.sock
.fd
;
909 /* Set header with stream information */
910 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
911 data_hdr
.data_size
= htobe32(data_size
);
912 data_hdr
.padding_size
= htobe32(padding
);
914 * Note that net_seq_num below is assigned with the *current* value of
915 * next_net_seq_num and only after that the next_net_seq_num will be
916 * increment. This is why when issuing a command on the relayd using
917 * this next value, 1 should always be substracted in order to compare
918 * the last seen sequence number on the relayd side to the last sent.
920 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
921 /* Other fields are zeroed previously */
923 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
929 ++stream
->next_net_seq_num
;
931 /* Set to go on data socket */
932 outfd
= relayd
->data_sock
.sock
.fd
;
940 * Allocate and return a new lttng_consumer_channel object using the given key
941 * to initialize the hash table node.
943 * On error, return NULL.
945 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
947 const char *pathname
,
952 enum lttng_event_output output
,
953 uint64_t tracefile_size
,
954 uint64_t tracefile_count
,
955 uint64_t session_id_per_pid
,
956 unsigned int monitor
,
957 unsigned int live_timer_interval
,
958 const char *root_shm_path
,
959 const char *shm_path
)
961 struct lttng_consumer_channel
*channel
;
963 channel
= zmalloc(sizeof(*channel
));
964 if (channel
== NULL
) {
965 PERROR("malloc struct lttng_consumer_channel");
970 channel
->refcount
= 0;
971 channel
->session_id
= session_id
;
972 channel
->session_id_per_pid
= session_id_per_pid
;
975 channel
->relayd_id
= relayd_id
;
976 channel
->tracefile_size
= tracefile_size
;
977 channel
->tracefile_count
= tracefile_count
;
978 channel
->monitor
= monitor
;
979 channel
->live_timer_interval
= live_timer_interval
;
980 pthread_mutex_init(&channel
->lock
, NULL
);
981 pthread_mutex_init(&channel
->timer_lock
, NULL
);
984 case LTTNG_EVENT_SPLICE
:
985 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
987 case LTTNG_EVENT_MMAP
:
988 channel
->output
= CONSUMER_CHANNEL_MMAP
;
998 * In monitor mode, the streams associated with the channel will be put in
999 * a special list ONLY owned by this channel. So, the refcount is set to 1
1000 * here meaning that the channel itself has streams that are referenced.
1002 * On a channel deletion, once the channel is no longer visible, the
1003 * refcount is decremented and checked for a zero value to delete it. With
1004 * streams in no monitor mode, it will now be safe to destroy the channel.
1006 if (!channel
->monitor
) {
1007 channel
->refcount
= 1;
1010 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1011 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1013 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1014 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1016 if (root_shm_path
) {
1017 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1018 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1021 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1022 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1025 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1027 channel
->wait_fd
= -1;
1029 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1031 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1038 * Add a channel to the global list protected by a mutex.
1040 * Always return 0 indicating success.
1042 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1043 struct lttng_consumer_local_data
*ctx
)
1045 pthread_mutex_lock(&consumer_data
.lock
);
1046 pthread_mutex_lock(&channel
->lock
);
1047 pthread_mutex_lock(&channel
->timer_lock
);
1050 * This gives us a guarantee that the channel we are about to add to the
1051 * channel hash table will be unique. See this function comment on the why
1052 * we need to steel the channel key at this stage.
1054 steal_channel_key(channel
->key
);
1057 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1060 pthread_mutex_unlock(&channel
->timer_lock
);
1061 pthread_mutex_unlock(&channel
->lock
);
1062 pthread_mutex_unlock(&consumer_data
.lock
);
1064 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1065 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1072 * Allocate the pollfd structure and the local view of the out fds to avoid
1073 * doing a lookup in the linked list and concurrency issues when writing is
1074 * needed. Called with consumer_data.lock held.
1076 * Returns the number of fds in the structures.
1078 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1079 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1080 struct lttng_ht
*ht
, int *nb_inactive_fd
)
1083 struct lttng_ht_iter iter
;
1084 struct lttng_consumer_stream
*stream
;
1089 assert(local_stream
);
1091 DBG("Updating poll fd array");
1092 *nb_inactive_fd
= 0;
1094 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1096 * Only active streams with an active end point can be added to the
1097 * poll set and local stream storage of the thread.
1099 * There is a potential race here for endpoint_status to be updated
1100 * just after the check. However, this is OK since the stream(s) will
1101 * be deleted once the thread is notified that the end point state has
1102 * changed where this function will be called back again.
1104 * We track the number of inactive FDs because they still need to be
1105 * closed by the polling thread after a wakeup on the data_pipe or
1108 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
1109 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1110 (*nb_inactive_fd
)++;
1114 * This clobbers way too much the debug output. Uncomment that if you
1115 * need it for debugging purposes.
1117 * DBG("Active FD %d", stream->wait_fd);
1119 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1120 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1121 local_stream
[i
] = stream
;
1127 * Insert the consumer_data_pipe at the end of the array and don't
1128 * increment i so nb_fd is the number of real FD.
1130 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1131 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1133 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1134 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1139 * Poll on the should_quit pipe and the command socket return -1 on
1140 * error, 1 if should exit, 0 if data is available on the command socket
1142 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1147 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1148 if (num_rdy
== -1) {
1150 * Restart interrupted system call.
1152 if (errno
== EINTR
) {
1155 PERROR("Poll error");
1158 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1159 DBG("consumer_should_quit wake up");
1166 * Set the error socket.
1168 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1171 ctx
->consumer_error_socket
= sock
;
1175 * Set the command socket path.
1177 void lttng_consumer_set_command_sock_path(
1178 struct lttng_consumer_local_data
*ctx
, char *sock
)
1180 ctx
->consumer_command_sock_path
= sock
;
1184 * Send return code to the session daemon.
1185 * If the socket is not defined, we return 0, it is not a fatal error
1187 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1189 if (ctx
->consumer_error_socket
> 0) {
1190 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1191 sizeof(enum lttcomm_sessiond_command
));
1198 * Close all the tracefiles and stream fds and MUST be called when all
1199 * instances are destroyed i.e. when all threads were joined and are ended.
1201 void lttng_consumer_cleanup(void)
1203 struct lttng_ht_iter iter
;
1204 struct lttng_consumer_channel
*channel
;
1208 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1210 consumer_del_channel(channel
);
1215 lttng_ht_destroy(consumer_data
.channel_ht
);
1217 cleanup_relayd_ht();
1219 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1222 * This HT contains streams that are freed by either the metadata thread or
1223 * the data thread so we do *nothing* on the hash table and simply destroy
1226 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1230 * Called from signal handler.
1232 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1236 CMM_STORE_SHARED(consumer_quit
, 1);
1237 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1239 PERROR("write consumer quit");
1242 DBG("Consumer flag that it should quit");
1247 * Flush pending writes to trace output disk file.
1250 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1254 int outfd
= stream
->out_fd
;
1257 * This does a blocking write-and-wait on any page that belongs to the
1258 * subbuffer prior to the one we just wrote.
1259 * Don't care about error values, as these are just hints and ways to
1260 * limit the amount of page cache used.
1262 if (orig_offset
< stream
->max_sb_size
) {
1265 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1266 stream
->max_sb_size
,
1267 SYNC_FILE_RANGE_WAIT_BEFORE
1268 | SYNC_FILE_RANGE_WRITE
1269 | SYNC_FILE_RANGE_WAIT_AFTER
);
1271 * Give hints to the kernel about how we access the file:
1272 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1275 * We need to call fadvise again after the file grows because the
1276 * kernel does not seem to apply fadvise to non-existing parts of the
1279 * Call fadvise _after_ having waited for the page writeback to
1280 * complete because the dirty page writeback semantic is not well
1281 * defined. So it can be expected to lead to lower throughput in
1284 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1285 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1286 if (ret
&& ret
!= -ENOSYS
) {
1288 PERROR("posix_fadvise on fd %i", outfd
);
1293 * Initialise the necessary environnement :
1294 * - create a new context
1295 * - create the poll_pipe
1296 * - create the should_quit pipe (for signal handler)
1297 * - create the thread pipe (for splice)
1299 * Takes a function pointer as argument, this function is called when data is
1300 * available on a buffer. This function is responsible to do the
1301 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1302 * buffer configuration and then kernctl_put_next_subbuf at the end.
1304 * Returns a pointer to the new context or NULL on error.
1306 struct lttng_consumer_local_data
*lttng_consumer_create(
1307 enum lttng_consumer_type type
,
1308 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1309 struct lttng_consumer_local_data
*ctx
),
1310 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1311 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1312 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1315 struct lttng_consumer_local_data
*ctx
;
1317 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1318 consumer_data
.type
== type
);
1319 consumer_data
.type
= type
;
1321 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1323 PERROR("allocating context");
1327 ctx
->consumer_error_socket
= -1;
1328 ctx
->consumer_metadata_socket
= -1;
1329 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1330 /* assign the callbacks */
1331 ctx
->on_buffer_ready
= buffer_ready
;
1332 ctx
->on_recv_channel
= recv_channel
;
1333 ctx
->on_recv_stream
= recv_stream
;
1334 ctx
->on_update_stream
= update_stream
;
1336 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1337 if (!ctx
->consumer_data_pipe
) {
1338 goto error_poll_pipe
;
1341 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1342 if (!ctx
->consumer_wakeup_pipe
) {
1343 goto error_wakeup_pipe
;
1346 ret
= pipe(ctx
->consumer_should_quit
);
1348 PERROR("Error creating recv pipe");
1349 goto error_quit_pipe
;
1352 ret
= pipe(ctx
->consumer_channel_pipe
);
1354 PERROR("Error creating channel pipe");
1355 goto error_channel_pipe
;
1358 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1359 if (!ctx
->consumer_metadata_pipe
) {
1360 goto error_metadata_pipe
;
1363 ctx
->channel_monitor_pipe
= -1;
1367 error_metadata_pipe
:
1368 utils_close_pipe(ctx
->consumer_channel_pipe
);
1370 utils_close_pipe(ctx
->consumer_should_quit
);
1372 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1374 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1382 * Iterate over all streams of the hashtable and free them properly.
1384 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1386 struct lttng_ht_iter iter
;
1387 struct lttng_consumer_stream
*stream
;
1394 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1396 * Ignore return value since we are currently cleaning up so any error
1399 (void) consumer_del_stream(stream
, ht
);
1403 lttng_ht_destroy(ht
);
1407 * Iterate over all streams of the metadata hashtable and free them
1410 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1412 struct lttng_ht_iter iter
;
1413 struct lttng_consumer_stream
*stream
;
1420 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1422 * Ignore return value since we are currently cleaning up so any error
1425 (void) consumer_del_metadata_stream(stream
, ht
);
1429 lttng_ht_destroy(ht
);
1433 * Close all fds associated with the instance and free the context.
1435 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1439 DBG("Consumer destroying it. Closing everything.");
1445 destroy_data_stream_ht(data_ht
);
1446 destroy_metadata_stream_ht(metadata_ht
);
1448 ret
= close(ctx
->consumer_error_socket
);
1452 ret
= close(ctx
->consumer_metadata_socket
);
1456 utils_close_pipe(ctx
->consumer_channel_pipe
);
1457 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1458 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1459 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1460 utils_close_pipe(ctx
->consumer_should_quit
);
1462 unlink(ctx
->consumer_command_sock_path
);
1467 * Write the metadata stream id on the specified file descriptor.
1469 static int write_relayd_metadata_id(int fd
,
1470 struct lttng_consumer_stream
*stream
,
1471 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1474 struct lttcomm_relayd_metadata_payload hdr
;
1476 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1477 hdr
.padding_size
= htobe32(padding
);
1478 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1479 if (ret
< sizeof(hdr
)) {
1481 * This error means that the fd's end is closed so ignore the PERROR
1482 * not to clubber the error output since this can happen in a normal
1485 if (errno
!= EPIPE
) {
1486 PERROR("write metadata stream id");
1488 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1490 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1491 * handle writting the missing part so report that as an error and
1492 * don't lie to the caller.
1497 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1498 stream
->relayd_stream_id
, padding
);
1505 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1506 * core function for writing trace buffers to either the local filesystem or
1509 * It must be called with the stream lock held.
1511 * Careful review MUST be put if any changes occur!
1513 * Returns the number of bytes written
1515 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1516 struct lttng_consumer_local_data
*ctx
,
1517 struct lttng_consumer_stream
*stream
, unsigned long len
,
1518 unsigned long padding
,
1519 struct ctf_packet_index
*index
)
1521 unsigned long mmap_offset
;
1524 off_t orig_offset
= stream
->out_fd_offset
;
1525 /* Default is on the disk */
1526 int outfd
= stream
->out_fd
;
1527 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1528 unsigned int relayd_hang_up
= 0;
1530 /* RCU lock for the relayd pointer */
1533 /* Flag that the current stream if set for network streaming. */
1534 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1535 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1536 if (relayd
== NULL
) {
1542 /* get the offset inside the fd to mmap */
1543 switch (consumer_data
.type
) {
1544 case LTTNG_CONSUMER_KERNEL
:
1545 mmap_base
= stream
->mmap_base
;
1546 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1548 PERROR("tracer ctl get_mmap_read_offset");
1552 case LTTNG_CONSUMER32_UST
:
1553 case LTTNG_CONSUMER64_UST
:
1554 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1556 ERR("read mmap get mmap base for stream %s", stream
->name
);
1560 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1562 PERROR("tracer ctl get_mmap_read_offset");
1568 ERR("Unknown consumer_data type");
1572 /* Handle stream on the relayd if the output is on the network */
1574 unsigned long netlen
= len
;
1577 * Lock the control socket for the complete duration of the function
1578 * since from this point on we will use the socket.
1580 if (stream
->metadata_flag
) {
1581 /* Metadata requires the control socket. */
1582 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1583 if (stream
->reset_metadata_flag
) {
1584 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1585 stream
->relayd_stream_id
,
1586 stream
->metadata_version
);
1591 stream
->reset_metadata_flag
= 0;
1593 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1596 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1601 /* Use the returned socket. */
1604 /* Write metadata stream id before payload */
1605 if (stream
->metadata_flag
) {
1606 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1613 /* No streaming, we have to set the len with the full padding */
1616 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1617 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1619 ERR("Reset metadata file");
1622 stream
->reset_metadata_flag
= 0;
1626 * Check if we need to change the tracefile before writing the packet.
1628 if (stream
->chan
->tracefile_size
> 0 &&
1629 (stream
->tracefile_size_current
+ len
) >
1630 stream
->chan
->tracefile_size
) {
1631 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1632 stream
->name
, stream
->chan
->tracefile_size
,
1633 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1634 stream
->out_fd
, &(stream
->tracefile_count_current
),
1637 ERR("Rotating output file");
1640 outfd
= stream
->out_fd
;
1642 if (stream
->index_file
) {
1643 lttng_index_file_put(stream
->index_file
);
1644 stream
->index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1645 stream
->name
, stream
->uid
, stream
->gid
,
1646 stream
->chan
->tracefile_size
,
1647 stream
->tracefile_count_current
,
1648 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1649 if (!stream
->index_file
) {
1654 /* Reset current size because we just perform a rotation. */
1655 stream
->tracefile_size_current
= 0;
1656 stream
->out_fd_offset
= 0;
1659 stream
->tracefile_size_current
+= len
;
1661 index
->offset
= htobe64(stream
->out_fd_offset
);
1666 * This call guarantee that len or less is returned. It's impossible to
1667 * receive a ret value that is bigger than len.
1669 ret
= lttng_write(outfd
, mmap_base
+ mmap_offset
, len
);
1670 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1671 if (ret
< 0 || ((size_t) ret
!= len
)) {
1673 * Report error to caller if nothing was written else at least send the
1681 /* Socket operation failed. We consider the relayd dead */
1682 if (errno
== EPIPE
|| errno
== EINVAL
|| errno
== EBADF
) {
1684 * This is possible if the fd is closed on the other side
1685 * (outfd) or any write problem. It can be verbose a bit for a
1686 * normal execution if for instance the relayd is stopped
1687 * abruptly. This can happen so set this to a DBG statement.
1689 DBG("Consumer mmap write detected relayd hang up");
1691 /* Unhandled error, print it and stop function right now. */
1692 PERROR("Error in write mmap (ret %zd != len %lu)", ret
, len
);
1696 stream
->output_written
+= ret
;
1698 /* This call is useless on a socket so better save a syscall. */
1700 /* This won't block, but will start writeout asynchronously */
1701 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, len
,
1702 SYNC_FILE_RANGE_WRITE
);
1703 stream
->out_fd_offset
+= len
;
1704 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1709 * This is a special case that the relayd has closed its socket. Let's
1710 * cleanup the relayd object and all associated streams.
1712 if (relayd
&& relayd_hang_up
) {
1713 cleanup_relayd(relayd
, ctx
);
1717 /* Unlock only if ctrl socket used */
1718 if (relayd
&& stream
->metadata_flag
) {
1719 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1727 * Splice the data from the ring buffer to the tracefile.
1729 * It must be called with the stream lock held.
1731 * Returns the number of bytes spliced.
1733 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1734 struct lttng_consumer_local_data
*ctx
,
1735 struct lttng_consumer_stream
*stream
, unsigned long len
,
1736 unsigned long padding
,
1737 struct ctf_packet_index
*index
)
1739 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1741 off_t orig_offset
= stream
->out_fd_offset
;
1742 int fd
= stream
->wait_fd
;
1743 /* Default is on the disk */
1744 int outfd
= stream
->out_fd
;
1745 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1747 unsigned int relayd_hang_up
= 0;
1749 switch (consumer_data
.type
) {
1750 case LTTNG_CONSUMER_KERNEL
:
1752 case LTTNG_CONSUMER32_UST
:
1753 case LTTNG_CONSUMER64_UST
:
1754 /* Not supported for user space tracing */
1757 ERR("Unknown consumer_data type");
1761 /* RCU lock for the relayd pointer */
1764 /* Flag that the current stream if set for network streaming. */
1765 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1766 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1767 if (relayd
== NULL
) {
1772 splice_pipe
= stream
->splice_pipe
;
1774 /* Write metadata stream id before payload */
1776 unsigned long total_len
= len
;
1778 if (stream
->metadata_flag
) {
1780 * Lock the control socket for the complete duration of the function
1781 * since from this point on we will use the socket.
1783 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1785 if (stream
->reset_metadata_flag
) {
1786 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1787 stream
->relayd_stream_id
,
1788 stream
->metadata_version
);
1793 stream
->reset_metadata_flag
= 0;
1795 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1803 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1806 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1812 /* Use the returned socket. */
1815 /* No streaming, we have to set the len with the full padding */
1818 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1819 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1821 ERR("Reset metadata file");
1824 stream
->reset_metadata_flag
= 0;
1827 * Check if we need to change the tracefile before writing the packet.
1829 if (stream
->chan
->tracefile_size
> 0 &&
1830 (stream
->tracefile_size_current
+ len
) >
1831 stream
->chan
->tracefile_size
) {
1832 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1833 stream
->name
, stream
->chan
->tracefile_size
,
1834 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1835 stream
->out_fd
, &(stream
->tracefile_count_current
),
1839 ERR("Rotating output file");
1842 outfd
= stream
->out_fd
;
1844 if (stream
->index_file
) {
1845 lttng_index_file_put(stream
->index_file
);
1846 stream
->index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1847 stream
->name
, stream
->uid
, stream
->gid
,
1848 stream
->chan
->tracefile_size
,
1849 stream
->tracefile_count_current
,
1850 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1851 if (!stream
->index_file
) {
1856 /* Reset current size because we just perform a rotation. */
1857 stream
->tracefile_size_current
= 0;
1858 stream
->out_fd_offset
= 0;
1861 stream
->tracefile_size_current
+= len
;
1862 index
->offset
= htobe64(stream
->out_fd_offset
);
1866 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1867 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1868 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1869 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1870 DBG("splice chan to pipe, ret %zd", ret_splice
);
1871 if (ret_splice
< 0) {
1874 PERROR("Error in relay splice");
1878 /* Handle stream on the relayd if the output is on the network */
1879 if (relayd
&& stream
->metadata_flag
) {
1880 size_t metadata_payload_size
=
1881 sizeof(struct lttcomm_relayd_metadata_payload
);
1883 /* Update counter to fit the spliced data */
1884 ret_splice
+= metadata_payload_size
;
1885 len
+= metadata_payload_size
;
1887 * We do this so the return value can match the len passed as
1888 * argument to this function.
1890 written
-= metadata_payload_size
;
1893 /* Splice data out */
1894 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1895 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1896 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1898 if (ret_splice
< 0) {
1903 } else if (ret_splice
> len
) {
1905 * We don't expect this code path to be executed but you never know
1906 * so this is an extra protection agains a buggy splice().
1909 written
+= ret_splice
;
1910 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1914 /* All good, update current len and continue. */
1918 /* This call is useless on a socket so better save a syscall. */
1920 /* This won't block, but will start writeout asynchronously */
1921 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1922 SYNC_FILE_RANGE_WRITE
);
1923 stream
->out_fd_offset
+= ret_splice
;
1925 stream
->output_written
+= ret_splice
;
1926 written
+= ret_splice
;
1929 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1935 * This is a special case that the relayd has closed its socket. Let's
1936 * cleanup the relayd object and all associated streams.
1938 if (relayd
&& relayd_hang_up
) {
1939 cleanup_relayd(relayd
, ctx
);
1940 /* Skip splice error so the consumer does not fail */
1945 /* send the appropriate error description to sessiond */
1948 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1951 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1954 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1959 if (relayd
&& stream
->metadata_flag
) {
1960 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1968 * Take a snapshot for a specific fd
1970 * Returns 0 on success, < 0 on error
1972 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1974 switch (consumer_data
.type
) {
1975 case LTTNG_CONSUMER_KERNEL
:
1976 return lttng_kconsumer_take_snapshot(stream
);
1977 case LTTNG_CONSUMER32_UST
:
1978 case LTTNG_CONSUMER64_UST
:
1979 return lttng_ustconsumer_take_snapshot(stream
);
1981 ERR("Unknown consumer_data type");
1988 * Get the produced position
1990 * Returns 0 on success, < 0 on error
1992 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1995 switch (consumer_data
.type
) {
1996 case LTTNG_CONSUMER_KERNEL
:
1997 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1998 case LTTNG_CONSUMER32_UST
:
1999 case LTTNG_CONSUMER64_UST
:
2000 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
2002 ERR("Unknown consumer_data type");
2008 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2009 int sock
, struct pollfd
*consumer_sockpoll
)
2011 switch (consumer_data
.type
) {
2012 case LTTNG_CONSUMER_KERNEL
:
2013 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2014 case LTTNG_CONSUMER32_UST
:
2015 case LTTNG_CONSUMER64_UST
:
2016 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2018 ERR("Unknown consumer_data type");
2024 void lttng_consumer_close_all_metadata(void)
2026 switch (consumer_data
.type
) {
2027 case LTTNG_CONSUMER_KERNEL
:
2029 * The Kernel consumer has a different metadata scheme so we don't
2030 * close anything because the stream will be closed by the session
2034 case LTTNG_CONSUMER32_UST
:
2035 case LTTNG_CONSUMER64_UST
:
2037 * Close all metadata streams. The metadata hash table is passed and
2038 * this call iterates over it by closing all wakeup fd. This is safe
2039 * because at this point we are sure that the metadata producer is
2040 * either dead or blocked.
2042 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2045 ERR("Unknown consumer_data type");
2051 * Clean up a metadata stream and free its memory.
2053 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2054 struct lttng_ht
*ht
)
2056 struct lttng_consumer_channel
*free_chan
= NULL
;
2060 * This call should NEVER receive regular stream. It must always be
2061 * metadata stream and this is crucial for data structure synchronization.
2063 assert(stream
->metadata_flag
);
2065 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2067 pthread_mutex_lock(&consumer_data
.lock
);
2068 pthread_mutex_lock(&stream
->chan
->lock
);
2069 pthread_mutex_lock(&stream
->lock
);
2070 if (stream
->chan
->metadata_cache
) {
2071 /* Only applicable to userspace consumers. */
2072 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2075 /* Remove any reference to that stream. */
2076 consumer_stream_delete(stream
, ht
);
2078 /* Close down everything including the relayd if one. */
2079 consumer_stream_close(stream
);
2080 /* Destroy tracer buffers of the stream. */
2081 consumer_stream_destroy_buffers(stream
);
2083 /* Atomically decrement channel refcount since other threads can use it. */
2084 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
2085 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
2086 /* Go for channel deletion! */
2087 free_chan
= stream
->chan
;
2091 * Nullify the stream reference so it is not used after deletion. The
2092 * channel lock MUST be acquired before being able to check for a NULL
2095 stream
->chan
->metadata_stream
= NULL
;
2097 if (stream
->chan
->metadata_cache
) {
2098 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2100 pthread_mutex_unlock(&stream
->lock
);
2101 pthread_mutex_unlock(&stream
->chan
->lock
);
2102 pthread_mutex_unlock(&consumer_data
.lock
);
2105 consumer_del_channel(free_chan
);
2108 consumer_stream_free(stream
);
2112 * Action done with the metadata stream when adding it to the consumer internal
2113 * data structures to handle it.
2115 int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2117 struct lttng_ht
*ht
= metadata_ht
;
2119 struct lttng_ht_iter iter
;
2120 struct lttng_ht_node_u64
*node
;
2125 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2127 pthread_mutex_lock(&consumer_data
.lock
);
2128 pthread_mutex_lock(&stream
->chan
->lock
);
2129 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2130 pthread_mutex_lock(&stream
->lock
);
2133 * From here, refcounts are updated so be _careful_ when returning an error
2140 * Lookup the stream just to make sure it does not exist in our internal
2141 * state. This should NEVER happen.
2143 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2144 node
= lttng_ht_iter_get_node_u64(&iter
);
2148 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2149 * in terms of destroying the associated channel, because the action that
2150 * causes the count to become 0 also causes a stream to be added. The
2151 * channel deletion will thus be triggered by the following removal of this
2154 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2155 /* Increment refcount before decrementing nb_init_stream_left */
2157 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2160 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2162 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2163 &stream
->node_channel_id
);
2166 * Add stream to the stream_list_ht of the consumer data. No need to steal
2167 * the key since the HT does not use it and we allow to add redundant keys
2170 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2174 pthread_mutex_unlock(&stream
->lock
);
2175 pthread_mutex_unlock(&stream
->chan
->lock
);
2176 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2177 pthread_mutex_unlock(&consumer_data
.lock
);
2182 * Delete data stream that are flagged for deletion (endpoint_status).
2184 static void validate_endpoint_status_data_stream(void)
2186 struct lttng_ht_iter iter
;
2187 struct lttng_consumer_stream
*stream
;
2189 DBG("Consumer delete flagged data stream");
2192 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2193 /* Validate delete flag of the stream */
2194 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2197 /* Delete it right now */
2198 consumer_del_stream(stream
, data_ht
);
2204 * Delete metadata stream that are flagged for deletion (endpoint_status).
2206 static void validate_endpoint_status_metadata_stream(
2207 struct lttng_poll_event
*pollset
)
2209 struct lttng_ht_iter iter
;
2210 struct lttng_consumer_stream
*stream
;
2212 DBG("Consumer delete flagged metadata stream");
2217 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2218 /* Validate delete flag of the stream */
2219 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2223 * Remove from pollset so the metadata thread can continue without
2224 * blocking on a deleted stream.
2226 lttng_poll_del(pollset
, stream
->wait_fd
);
2228 /* Delete it right now */
2229 consumer_del_metadata_stream(stream
, metadata_ht
);
2235 * Thread polls on metadata file descriptor and write them on disk or on the
2238 void *consumer_thread_metadata_poll(void *data
)
2240 int ret
, i
, pollfd
, err
= -1;
2241 uint32_t revents
, nb_fd
;
2242 struct lttng_consumer_stream
*stream
= NULL
;
2243 struct lttng_ht_iter iter
;
2244 struct lttng_ht_node_u64
*node
;
2245 struct lttng_poll_event events
;
2246 struct lttng_consumer_local_data
*ctx
= data
;
2249 rcu_register_thread();
2251 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2253 if (testpoint(consumerd_thread_metadata
)) {
2254 goto error_testpoint
;
2257 health_code_update();
2259 DBG("Thread metadata poll started");
2261 /* Size is set to 1 for the consumer_metadata pipe */
2262 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2264 ERR("Poll set creation failed");
2268 ret
= lttng_poll_add(&events
,
2269 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2275 DBG("Metadata main loop started");
2279 health_code_update();
2280 health_poll_entry();
2281 DBG("Metadata poll wait");
2282 ret
= lttng_poll_wait(&events
, -1);
2283 DBG("Metadata poll return from wait with %d fd(s)",
2284 LTTNG_POLL_GETNB(&events
));
2286 DBG("Metadata event caught in thread");
2288 if (errno
== EINTR
) {
2289 ERR("Poll EINTR caught");
2292 if (LTTNG_POLL_GETNB(&events
) == 0) {
2293 err
= 0; /* All is OK */
2300 /* From here, the event is a metadata wait fd */
2301 for (i
= 0; i
< nb_fd
; i
++) {
2302 health_code_update();
2304 revents
= LTTNG_POLL_GETEV(&events
, i
);
2305 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2308 /* No activity for this FD (poll implementation). */
2312 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2313 if (revents
& LPOLLIN
) {
2316 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2317 &stream
, sizeof(stream
));
2318 if (pipe_len
< sizeof(stream
)) {
2320 PERROR("read metadata stream");
2323 * Remove the pipe from the poll set and continue the loop
2324 * since their might be data to consume.
2326 lttng_poll_del(&events
,
2327 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2328 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2332 /* A NULL stream means that the state has changed. */
2333 if (stream
== NULL
) {
2334 /* Check for deleted streams. */
2335 validate_endpoint_status_metadata_stream(&events
);
2339 DBG("Adding metadata stream %d to poll set",
2342 /* Add metadata stream to the global poll events list */
2343 lttng_poll_add(&events
, stream
->wait_fd
,
2344 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2345 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2346 DBG("Metadata thread pipe hung up");
2348 * Remove the pipe from the poll set and continue the loop
2349 * since their might be data to consume.
2351 lttng_poll_del(&events
,
2352 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2353 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2356 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2360 /* Handle other stream */
2366 uint64_t tmp_id
= (uint64_t) pollfd
;
2368 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2370 node
= lttng_ht_iter_get_node_u64(&iter
);
2373 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2376 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2377 /* Get the data out of the metadata file descriptor */
2378 DBG("Metadata available on fd %d", pollfd
);
2379 assert(stream
->wait_fd
== pollfd
);
2382 health_code_update();
2384 len
= ctx
->on_buffer_ready(stream
, ctx
);
2386 * We don't check the return value here since if we get
2387 * a negative len, it means an error occurred thus we
2388 * simply remove it from the poll set and free the
2393 /* It's ok to have an unavailable sub-buffer */
2394 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2395 /* Clean up stream from consumer and free it. */
2396 lttng_poll_del(&events
, stream
->wait_fd
);
2397 consumer_del_metadata_stream(stream
, metadata_ht
);
2399 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2400 DBG("Metadata fd %d is hup|err.", pollfd
);
2401 if (!stream
->hangup_flush_done
2402 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2403 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2404 DBG("Attempting to flush and consume the UST buffers");
2405 lttng_ustconsumer_on_stream_hangup(stream
);
2407 /* We just flushed the stream now read it. */
2409 health_code_update();
2411 len
= ctx
->on_buffer_ready(stream
, ctx
);
2413 * We don't check the return value here since if we get
2414 * a negative len, it means an error occurred thus we
2415 * simply remove it from the poll set and free the
2421 lttng_poll_del(&events
, stream
->wait_fd
);
2423 * This call update the channel states, closes file descriptors
2424 * and securely free the stream.
2426 consumer_del_metadata_stream(stream
, metadata_ht
);
2428 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2432 /* Release RCU lock for the stream looked up */
2440 DBG("Metadata poll thread exiting");
2442 lttng_poll_clean(&events
);
2447 ERR("Health error occurred in %s", __func__
);
2449 health_unregister(health_consumerd
);
2450 rcu_unregister_thread();
2455 * This thread polls the fds in the set to consume the data and write
2456 * it to tracefile if necessary.
2458 void *consumer_thread_data_poll(void *data
)
2460 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2461 struct pollfd
*pollfd
= NULL
;
2462 /* local view of the streams */
2463 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2464 /* local view of consumer_data.fds_count */
2466 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2467 int nb_inactive_fd
= 0;
2468 struct lttng_consumer_local_data
*ctx
= data
;
2471 rcu_register_thread();
2473 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2475 if (testpoint(consumerd_thread_data
)) {
2476 goto error_testpoint
;
2479 health_code_update();
2481 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2482 if (local_stream
== NULL
) {
2483 PERROR("local_stream malloc");
2488 health_code_update();
2494 * the fds set has been updated, we need to update our
2495 * local array as well
2497 pthread_mutex_lock(&consumer_data
.lock
);
2498 if (consumer_data
.need_update
) {
2503 local_stream
= NULL
;
2506 * Allocate for all fds +1 for the consumer_data_pipe and +1 for
2509 pollfd
= zmalloc((consumer_data
.stream_count
+ 2) * sizeof(struct pollfd
));
2510 if (pollfd
== NULL
) {
2511 PERROR("pollfd malloc");
2512 pthread_mutex_unlock(&consumer_data
.lock
);
2516 local_stream
= zmalloc((consumer_data
.stream_count
+ 2) *
2517 sizeof(struct lttng_consumer_stream
*));
2518 if (local_stream
== NULL
) {
2519 PERROR("local_stream malloc");
2520 pthread_mutex_unlock(&consumer_data
.lock
);
2523 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2524 data_ht
, &nb_inactive_fd
);
2526 ERR("Error in allocating pollfd or local_outfds");
2527 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2528 pthread_mutex_unlock(&consumer_data
.lock
);
2532 consumer_data
.need_update
= 0;
2534 pthread_mutex_unlock(&consumer_data
.lock
);
2536 /* No FDs and consumer_quit, consumer_cleanup the thread */
2537 if (nb_fd
== 0 && nb_inactive_fd
== 0 &&
2538 CMM_LOAD_SHARED(consumer_quit
) == 1) {
2539 err
= 0; /* All is OK */
2542 /* poll on the array of fds */
2544 DBG("polling on %d fd", nb_fd
+ 2);
2545 if (testpoint(consumerd_thread_data_poll
)) {
2548 health_poll_entry();
2549 num_rdy
= poll(pollfd
, nb_fd
+ 2, -1);
2551 DBG("poll num_rdy : %d", num_rdy
);
2552 if (num_rdy
== -1) {
2554 * Restart interrupted system call.
2556 if (errno
== EINTR
) {
2559 PERROR("Poll error");
2560 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2562 } else if (num_rdy
== 0) {
2563 DBG("Polling thread timed out");
2567 if (caa_unlikely(data_consumption_paused
)) {
2568 DBG("Data consumption paused, sleeping...");
2574 * If the consumer_data_pipe triggered poll go directly to the
2575 * beginning of the loop to update the array. We want to prioritize
2576 * array update over low-priority reads.
2578 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2579 ssize_t pipe_readlen
;
2581 DBG("consumer_data_pipe wake up");
2582 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2583 &new_stream
, sizeof(new_stream
));
2584 if (pipe_readlen
< sizeof(new_stream
)) {
2585 PERROR("Consumer data pipe");
2586 /* Continue so we can at least handle the current stream(s). */
2591 * If the stream is NULL, just ignore it. It's also possible that
2592 * the sessiond poll thread changed the consumer_quit state and is
2593 * waking us up to test it.
2595 if (new_stream
== NULL
) {
2596 validate_endpoint_status_data_stream();
2600 /* Continue to update the local streams and handle prio ones */
2604 /* Handle wakeup pipe. */
2605 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2607 ssize_t pipe_readlen
;
2609 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2611 if (pipe_readlen
< 0) {
2612 PERROR("Consumer data wakeup pipe");
2614 /* We've been awakened to handle stream(s). */
2615 ctx
->has_wakeup
= 0;
2618 /* Take care of high priority channels first. */
2619 for (i
= 0; i
< nb_fd
; i
++) {
2620 health_code_update();
2622 if (local_stream
[i
] == NULL
) {
2625 if (pollfd
[i
].revents
& POLLPRI
) {
2626 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2628 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2629 /* it's ok to have an unavailable sub-buffer */
2630 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2631 /* Clean the stream and free it. */
2632 consumer_del_stream(local_stream
[i
], data_ht
);
2633 local_stream
[i
] = NULL
;
2634 } else if (len
> 0) {
2635 local_stream
[i
]->data_read
= 1;
2641 * If we read high prio channel in this loop, try again
2642 * for more high prio data.
2648 /* Take care of low priority channels. */
2649 for (i
= 0; i
< nb_fd
; i
++) {
2650 health_code_update();
2652 if (local_stream
[i
] == NULL
) {
2655 if ((pollfd
[i
].revents
& POLLIN
) ||
2656 local_stream
[i
]->hangup_flush_done
||
2657 local_stream
[i
]->has_data
) {
2658 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2659 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2660 /* it's ok to have an unavailable sub-buffer */
2661 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2662 /* Clean the stream and free it. */
2663 consumer_del_stream(local_stream
[i
], data_ht
);
2664 local_stream
[i
] = NULL
;
2665 } else if (len
> 0) {
2666 local_stream
[i
]->data_read
= 1;
2671 /* Handle hangup and errors */
2672 for (i
= 0; i
< nb_fd
; i
++) {
2673 health_code_update();
2675 if (local_stream
[i
] == NULL
) {
2678 if (!local_stream
[i
]->hangup_flush_done
2679 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2680 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2681 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2682 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2684 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2685 /* Attempt read again, for the data we just flushed. */
2686 local_stream
[i
]->data_read
= 1;
2689 * If the poll flag is HUP/ERR/NVAL and we have
2690 * read no data in this pass, we can remove the
2691 * stream from its hash table.
2693 if ((pollfd
[i
].revents
& POLLHUP
)) {
2694 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2695 if (!local_stream
[i
]->data_read
) {
2696 consumer_del_stream(local_stream
[i
], data_ht
);
2697 local_stream
[i
] = NULL
;
2700 } else if (pollfd
[i
].revents
& POLLERR
) {
2701 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2702 if (!local_stream
[i
]->data_read
) {
2703 consumer_del_stream(local_stream
[i
], data_ht
);
2704 local_stream
[i
] = NULL
;
2707 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2708 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2709 if (!local_stream
[i
]->data_read
) {
2710 consumer_del_stream(local_stream
[i
], data_ht
);
2711 local_stream
[i
] = NULL
;
2715 if (local_stream
[i
] != NULL
) {
2716 local_stream
[i
]->data_read
= 0;
2723 DBG("polling thread exiting");
2728 * Close the write side of the pipe so epoll_wait() in
2729 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2730 * read side of the pipe. If we close them both, epoll_wait strangely does
2731 * not return and could create a endless wait period if the pipe is the
2732 * only tracked fd in the poll set. The thread will take care of closing
2735 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2740 ERR("Health error occurred in %s", __func__
);
2742 health_unregister(health_consumerd
);
2744 rcu_unregister_thread();
2749 * Close wake-up end of each stream belonging to the channel. This will
2750 * allow the poll() on the stream read-side to detect when the
2751 * write-side (application) finally closes them.
2754 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2756 struct lttng_ht
*ht
;
2757 struct lttng_consumer_stream
*stream
;
2758 struct lttng_ht_iter iter
;
2760 ht
= consumer_data
.stream_per_chan_id_ht
;
2763 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2764 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2765 ht
->match_fct
, &channel
->key
,
2766 &iter
.iter
, stream
, node_channel_id
.node
) {
2768 * Protect against teardown with mutex.
2770 pthread_mutex_lock(&stream
->lock
);
2771 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2774 switch (consumer_data
.type
) {
2775 case LTTNG_CONSUMER_KERNEL
:
2777 case LTTNG_CONSUMER32_UST
:
2778 case LTTNG_CONSUMER64_UST
:
2779 if (stream
->metadata_flag
) {
2780 /* Safe and protected by the stream lock. */
2781 lttng_ustconsumer_close_metadata(stream
->chan
);
2784 * Note: a mutex is taken internally within
2785 * liblttng-ust-ctl to protect timer wakeup_fd
2786 * use from concurrent close.
2788 lttng_ustconsumer_close_stream_wakeup(stream
);
2792 ERR("Unknown consumer_data type");
2796 pthread_mutex_unlock(&stream
->lock
);
2801 static void destroy_channel_ht(struct lttng_ht
*ht
)
2803 struct lttng_ht_iter iter
;
2804 struct lttng_consumer_channel
*channel
;
2812 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2813 ret
= lttng_ht_del(ht
, &iter
);
2818 lttng_ht_destroy(ht
);
2822 * This thread polls the channel fds to detect when they are being
2823 * closed. It closes all related streams if the channel is detected as
2824 * closed. It is currently only used as a shim layer for UST because the
2825 * consumerd needs to keep the per-stream wakeup end of pipes open for
2828 void *consumer_thread_channel_poll(void *data
)
2830 int ret
, i
, pollfd
, err
= -1;
2831 uint32_t revents
, nb_fd
;
2832 struct lttng_consumer_channel
*chan
= NULL
;
2833 struct lttng_ht_iter iter
;
2834 struct lttng_ht_node_u64
*node
;
2835 struct lttng_poll_event events
;
2836 struct lttng_consumer_local_data
*ctx
= data
;
2837 struct lttng_ht
*channel_ht
;
2839 rcu_register_thread();
2841 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2843 if (testpoint(consumerd_thread_channel
)) {
2844 goto error_testpoint
;
2847 health_code_update();
2849 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2851 /* ENOMEM at this point. Better to bail out. */
2855 DBG("Thread channel poll started");
2857 /* Size is set to 1 for the consumer_channel pipe */
2858 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2860 ERR("Poll set creation failed");
2864 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2870 DBG("Channel main loop started");
2874 health_code_update();
2875 DBG("Channel poll wait");
2876 health_poll_entry();
2877 ret
= lttng_poll_wait(&events
, -1);
2878 DBG("Channel poll return from wait with %d fd(s)",
2879 LTTNG_POLL_GETNB(&events
));
2881 DBG("Channel event caught in thread");
2883 if (errno
== EINTR
) {
2884 ERR("Poll EINTR caught");
2887 if (LTTNG_POLL_GETNB(&events
) == 0) {
2888 err
= 0; /* All is OK */
2895 /* From here, the event is a channel wait fd */
2896 for (i
= 0; i
< nb_fd
; i
++) {
2897 health_code_update();
2899 revents
= LTTNG_POLL_GETEV(&events
, i
);
2900 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2903 /* No activity for this FD (poll implementation). */
2907 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2908 if (revents
& LPOLLIN
) {
2909 enum consumer_channel_action action
;
2912 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2915 ERR("Error reading channel pipe");
2917 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2922 case CONSUMER_CHANNEL_ADD
:
2923 DBG("Adding channel %d to poll set",
2926 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2929 lttng_ht_add_unique_u64(channel_ht
,
2930 &chan
->wait_fd_node
);
2932 /* Add channel to the global poll events list */
2933 lttng_poll_add(&events
, chan
->wait_fd
,
2934 LPOLLERR
| LPOLLHUP
);
2936 case CONSUMER_CHANNEL_DEL
:
2939 * This command should never be called if the channel
2940 * has streams monitored by either the data or metadata
2941 * thread. The consumer only notify this thread with a
2942 * channel del. command if it receives a destroy
2943 * channel command from the session daemon that send it
2944 * if a command prior to the GET_CHANNEL failed.
2948 chan
= consumer_find_channel(key
);
2951 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2954 lttng_poll_del(&events
, chan
->wait_fd
);
2955 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2956 ret
= lttng_ht_del(channel_ht
, &iter
);
2959 switch (consumer_data
.type
) {
2960 case LTTNG_CONSUMER_KERNEL
:
2962 case LTTNG_CONSUMER32_UST
:
2963 case LTTNG_CONSUMER64_UST
:
2964 health_code_update();
2965 /* Destroy streams that might have been left in the stream list. */
2966 clean_channel_stream_list(chan
);
2969 ERR("Unknown consumer_data type");
2974 * Release our own refcount. Force channel deletion even if
2975 * streams were not initialized.
2977 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2978 consumer_del_channel(chan
);
2983 case CONSUMER_CHANNEL_QUIT
:
2985 * Remove the pipe from the poll set and continue the loop
2986 * since their might be data to consume.
2988 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2991 ERR("Unknown action");
2994 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2995 DBG("Channel thread pipe hung up");
2997 * Remove the pipe from the poll set and continue the loop
2998 * since their might be data to consume.
3000 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
3003 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3007 /* Handle other stream */
3013 uint64_t tmp_id
= (uint64_t) pollfd
;
3015 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3017 node
= lttng_ht_iter_get_node_u64(&iter
);
3020 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
3023 /* Check for error event */
3024 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3025 DBG("Channel fd %d is hup|err.", pollfd
);
3027 lttng_poll_del(&events
, chan
->wait_fd
);
3028 ret
= lttng_ht_del(channel_ht
, &iter
);
3032 * This will close the wait fd for each stream associated to
3033 * this channel AND monitored by the data/metadata thread thus
3034 * will be clean by the right thread.
3036 consumer_close_channel_streams(chan
);
3038 /* Release our own refcount */
3039 if (!uatomic_sub_return(&chan
->refcount
, 1)
3040 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3041 consumer_del_channel(chan
);
3044 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3049 /* Release RCU lock for the channel looked up */
3057 lttng_poll_clean(&events
);
3059 destroy_channel_ht(channel_ht
);
3062 DBG("Channel poll thread exiting");
3065 ERR("Health error occurred in %s", __func__
);
3067 health_unregister(health_consumerd
);
3068 rcu_unregister_thread();
3072 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3073 struct pollfd
*sockpoll
, int client_socket
)
3080 ret
= lttng_consumer_poll_socket(sockpoll
);
3084 DBG("Metadata connection on client_socket");
3086 /* Blocking call, waiting for transmission */
3087 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3088 if (ctx
->consumer_metadata_socket
< 0) {
3089 WARN("On accept metadata");
3100 * This thread listens on the consumerd socket and receives the file
3101 * descriptors from the session daemon.
3103 void *consumer_thread_sessiond_poll(void *data
)
3105 int sock
= -1, client_socket
, ret
, err
= -1;
3107 * structure to poll for incoming data on communication socket avoids
3108 * making blocking sockets.
3110 struct pollfd consumer_sockpoll
[2];
3111 struct lttng_consumer_local_data
*ctx
= data
;
3113 rcu_register_thread();
3115 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3117 if (testpoint(consumerd_thread_sessiond
)) {
3118 goto error_testpoint
;
3121 health_code_update();
3123 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3124 unlink(ctx
->consumer_command_sock_path
);
3125 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3126 if (client_socket
< 0) {
3127 ERR("Cannot create command socket");
3131 ret
= lttcomm_listen_unix_sock(client_socket
);
3136 DBG("Sending ready command to lttng-sessiond");
3137 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3138 /* return < 0 on error, but == 0 is not fatal */
3140 ERR("Error sending ready command to lttng-sessiond");
3144 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3145 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3146 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3147 consumer_sockpoll
[1].fd
= client_socket
;
3148 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3150 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3158 DBG("Connection on client_socket");
3160 /* Blocking call, waiting for transmission */
3161 sock
= lttcomm_accept_unix_sock(client_socket
);
3168 * Setup metadata socket which is the second socket connection on the
3169 * command unix socket.
3171 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3180 /* This socket is not useful anymore. */
3181 ret
= close(client_socket
);
3183 PERROR("close client_socket");
3187 /* update the polling structure to poll on the established socket */
3188 consumer_sockpoll
[1].fd
= sock
;
3189 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3192 health_code_update();
3194 health_poll_entry();
3195 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3204 DBG("Incoming command on sock");
3205 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3208 * This could simply be a session daemon quitting. Don't output
3211 DBG("Communication interrupted on command socket");
3215 if (CMM_LOAD_SHARED(consumer_quit
)) {
3216 DBG("consumer_thread_receive_fds received quit from signal");
3217 err
= 0; /* All is OK */
3220 DBG("received command on sock");
3226 DBG("Consumer thread sessiond poll exiting");
3229 * Close metadata streams since the producer is the session daemon which
3232 * NOTE: for now, this only applies to the UST tracer.
3234 lttng_consumer_close_all_metadata();
3237 * when all fds have hung up, the polling thread
3240 CMM_STORE_SHARED(consumer_quit
, 1);
3243 * Notify the data poll thread to poll back again and test the
3244 * consumer_quit state that we just set so to quit gracefully.
3246 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3248 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3250 notify_health_quit_pipe(health_quit_pipe
);
3252 /* Cleaning up possibly open sockets. */
3256 PERROR("close sock sessiond poll");
3259 if (client_socket
>= 0) {
3260 ret
= close(client_socket
);
3262 PERROR("close client_socket sessiond poll");
3269 ERR("Health error occurred in %s", __func__
);
3271 health_unregister(health_consumerd
);
3273 rcu_unregister_thread();
3277 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3278 struct lttng_consumer_local_data
*ctx
)
3282 pthread_mutex_lock(&stream
->lock
);
3283 if (stream
->metadata_flag
) {
3284 pthread_mutex_lock(&stream
->metadata_rdv_lock
);
3287 switch (consumer_data
.type
) {
3288 case LTTNG_CONSUMER_KERNEL
:
3289 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3291 case LTTNG_CONSUMER32_UST
:
3292 case LTTNG_CONSUMER64_UST
:
3293 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3296 ERR("Unknown consumer_data type");
3302 if (stream
->metadata_flag
) {
3303 pthread_cond_broadcast(&stream
->metadata_rdv
);
3304 pthread_mutex_unlock(&stream
->metadata_rdv_lock
);
3306 pthread_mutex_unlock(&stream
->lock
);
3310 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3312 switch (consumer_data
.type
) {
3313 case LTTNG_CONSUMER_KERNEL
:
3314 return lttng_kconsumer_on_recv_stream(stream
);
3315 case LTTNG_CONSUMER32_UST
:
3316 case LTTNG_CONSUMER64_UST
:
3317 return lttng_ustconsumer_on_recv_stream(stream
);
3319 ERR("Unknown consumer_data type");
3326 * Allocate and set consumer data hash tables.
3328 int lttng_consumer_init(void)
3330 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3331 if (!consumer_data
.channel_ht
) {
3335 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3336 if (!consumer_data
.relayd_ht
) {
3340 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3341 if (!consumer_data
.stream_list_ht
) {
3345 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3346 if (!consumer_data
.stream_per_chan_id_ht
) {
3350 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3355 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3367 * Process the ADD_RELAYD command receive by a consumer.
3369 * This will create a relayd socket pair and add it to the relayd hash table.
3370 * The caller MUST acquire a RCU read side lock before calling it.
3372 void consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3373 struct lttng_consumer_local_data
*ctx
, int sock
,
3374 struct pollfd
*consumer_sockpoll
,
3375 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3376 uint64_t relayd_session_id
)
3378 int fd
= -1, ret
= -1, relayd_created
= 0;
3379 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3380 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3383 assert(relayd_sock
);
3385 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3387 /* Get relayd reference if exists. */
3388 relayd
= consumer_find_relayd(net_seq_idx
);
3389 if (relayd
== NULL
) {
3390 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3391 /* Not found. Allocate one. */
3392 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3393 if (relayd
== NULL
) {
3394 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3397 relayd
->sessiond_session_id
= sessiond_id
;
3402 * This code path MUST continue to the consumer send status message to
3403 * we can notify the session daemon and continue our work without
3404 * killing everything.
3408 * relayd key should never be found for control socket.
3410 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3413 /* First send a status message before receiving the fds. */
3414 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3416 /* Somehow, the session daemon is not responding anymore. */
3417 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3418 goto error_nosignal
;
3421 /* Poll on consumer socket. */
3422 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3424 /* Needing to exit in the middle of a command: error. */
3425 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3426 goto error_nosignal
;
3429 /* Get relayd socket from session daemon */
3430 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3431 if (ret
!= sizeof(fd
)) {
3432 fd
= -1; /* Just in case it gets set with an invalid value. */
3435 * Failing to receive FDs might indicate a major problem such as
3436 * reaching a fd limit during the receive where the kernel returns a
3437 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3438 * don't take any chances and stop everything.
3440 * XXX: Feature request #558 will fix that and avoid this possible
3441 * issue when reaching the fd limit.
3443 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3444 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3448 /* Copy socket information and received FD */
3449 switch (sock_type
) {
3450 case LTTNG_STREAM_CONTROL
:
3451 /* Copy received lttcomm socket */
3452 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3453 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3454 /* Handle create_sock error. */
3456 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3460 * Close the socket created internally by
3461 * lttcomm_create_sock, so we can replace it by the one
3462 * received from sessiond.
3464 if (close(relayd
->control_sock
.sock
.fd
)) {
3468 /* Assign new file descriptor */
3469 relayd
->control_sock
.sock
.fd
= fd
;
3470 fd
= -1; /* For error path */
3471 /* Assign version values. */
3472 relayd
->control_sock
.major
= relayd_sock
->major
;
3473 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3475 relayd
->relayd_session_id
= relayd_session_id
;
3478 case LTTNG_STREAM_DATA
:
3479 /* Copy received lttcomm socket */
3480 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3481 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3482 /* Handle create_sock error. */
3484 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3488 * Close the socket created internally by
3489 * lttcomm_create_sock, so we can replace it by the one
3490 * received from sessiond.
3492 if (close(relayd
->data_sock
.sock
.fd
)) {
3496 /* Assign new file descriptor */
3497 relayd
->data_sock
.sock
.fd
= fd
;
3498 fd
= -1; /* for eventual error paths */
3499 /* Assign version values. */
3500 relayd
->data_sock
.major
= relayd_sock
->major
;
3501 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3504 ERR("Unknown relayd socket type (%d)", sock_type
);
3505 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3509 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3510 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3511 relayd
->net_seq_idx
, fd
);
3513 /* We successfully added the socket. Send status back. */
3514 ret
= consumer_send_status_msg(sock
, ret_code
);
3516 /* Somehow, the session daemon is not responding anymore. */
3517 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3518 goto error_nosignal
;
3522 * Add relayd socket pair to consumer data hashtable. If object already
3523 * exists or on error, the function gracefully returns.
3531 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3532 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3536 /* Close received socket if valid. */
3539 PERROR("close received socket");
3543 if (relayd_created
) {
3549 * Try to lock the stream mutex.
3551 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3553 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3560 * Try to lock the stream mutex. On failure, we know that the stream is
3561 * being used else where hence there is data still being extracted.
3563 ret
= pthread_mutex_trylock(&stream
->lock
);
3565 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3577 * Search for a relayd associated to the session id and return the reference.
3579 * A rcu read side lock MUST be acquire before calling this function and locked
3580 * until the relayd object is no longer necessary.
3582 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3584 struct lttng_ht_iter iter
;
3585 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3587 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3588 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3591 * Check by sessiond id which is unique here where the relayd session
3592 * id might not be when having multiple relayd.
3594 if (relayd
->sessiond_session_id
== id
) {
3595 /* Found the relayd. There can be only one per id. */
3607 * Check if for a given session id there is still data needed to be extract
3610 * Return 1 if data is pending or else 0 meaning ready to be read.
3612 int consumer_data_pending(uint64_t id
)
3615 struct lttng_ht_iter iter
;
3616 struct lttng_ht
*ht
;
3617 struct lttng_consumer_stream
*stream
;
3618 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3619 int (*data_pending
)(struct lttng_consumer_stream
*);
3621 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3624 pthread_mutex_lock(&consumer_data
.lock
);
3626 switch (consumer_data
.type
) {
3627 case LTTNG_CONSUMER_KERNEL
:
3628 data_pending
= lttng_kconsumer_data_pending
;
3630 case LTTNG_CONSUMER32_UST
:
3631 case LTTNG_CONSUMER64_UST
:
3632 data_pending
= lttng_ustconsumer_data_pending
;
3635 ERR("Unknown consumer data type");
3639 /* Ease our life a bit */
3640 ht
= consumer_data
.stream_list_ht
;
3642 relayd
= find_relayd_by_session_id(id
);
3644 /* Send init command for data pending. */
3645 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3646 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3647 relayd
->relayd_session_id
);
3648 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3650 /* Communication error thus the relayd so no data pending. */
3651 goto data_not_pending
;
3655 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3656 ht
->hash_fct(&id
, lttng_ht_seed
),
3658 &iter
.iter
, stream
, node_session_id
.node
) {
3659 /* If this call fails, the stream is being used hence data pending. */
3660 ret
= stream_try_lock(stream
);
3666 * A removed node from the hash table indicates that the stream has
3667 * been deleted thus having a guarantee that the buffers are closed
3668 * on the consumer side. However, data can still be transmitted
3669 * over the network so don't skip the relayd check.
3671 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3673 /* Check the stream if there is data in the buffers. */
3674 ret
= data_pending(stream
);
3676 pthread_mutex_unlock(&stream
->lock
);
3683 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3684 if (stream
->metadata_flag
) {
3685 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3686 stream
->relayd_stream_id
);
3688 ret
= relayd_data_pending(&relayd
->control_sock
,
3689 stream
->relayd_stream_id
,
3690 stream
->next_net_seq_num
- 1);
3692 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3694 pthread_mutex_unlock(&stream
->lock
);
3698 pthread_mutex_unlock(&stream
->lock
);
3702 unsigned int is_data_inflight
= 0;
3704 /* Send init command for data pending. */
3705 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3706 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3707 relayd
->relayd_session_id
, &is_data_inflight
);
3708 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3710 goto data_not_pending
;
3712 if (is_data_inflight
) {
3718 * Finding _no_ node in the hash table and no inflight data means that the
3719 * stream(s) have been removed thus data is guaranteed to be available for
3720 * analysis from the trace files.
3724 /* Data is available to be read by a viewer. */
3725 pthread_mutex_unlock(&consumer_data
.lock
);
3730 /* Data is still being extracted from buffers. */
3731 pthread_mutex_unlock(&consumer_data
.lock
);
3737 * Send a ret code status message to the sessiond daemon.
3739 * Return the sendmsg() return value.
3741 int consumer_send_status_msg(int sock
, int ret_code
)
3743 struct lttcomm_consumer_status_msg msg
;
3745 memset(&msg
, 0, sizeof(msg
));
3746 msg
.ret_code
= ret_code
;
3748 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3752 * Send a channel status message to the sessiond daemon.
3754 * Return the sendmsg() return value.
3756 int consumer_send_status_channel(int sock
,
3757 struct lttng_consumer_channel
*channel
)
3759 struct lttcomm_consumer_status_channel msg
;
3763 memset(&msg
, 0, sizeof(msg
));
3765 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3767 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3768 msg
.key
= channel
->key
;
3769 msg
.stream_count
= channel
->streams
.count
;
3772 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3775 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3776 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3777 uint64_t max_sb_size
)
3779 unsigned long start_pos
;
3781 if (!nb_packets_per_stream
) {
3782 return consumed_pos
; /* Grab everything */
3784 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
3785 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3786 if ((long) (start_pos
- consumed_pos
) < 0) {
3787 return consumed_pos
; /* Grab everything */