2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2012 - David Goulet <dgoulet@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/socket.h>
28 #include <sys/types.h>
33 #include <bin/lttng-consumerd/health-consumerd.h>
34 #include <common/common.h>
35 #include <common/utils.h>
36 #include <common/compat/poll.h>
37 #include <common/compat/endian.h>
38 #include <common/index/index.h>
39 #include <common/kernel-ctl/kernel-ctl.h>
40 #include <common/sessiond-comm/relayd.h>
41 #include <common/sessiond-comm/sessiond-comm.h>
42 #include <common/kernel-consumer/kernel-consumer.h>
43 #include <common/relayd/relayd.h>
44 #include <common/ust-consumer/ust-consumer.h>
45 #include <common/consumer/consumer-timer.h>
46 #include <common/consumer/consumer.h>
47 #include <common/consumer/consumer-stream.h>
48 #include <common/consumer/consumer-testpoint.h>
49 #include <common/align.h>
50 #include <common/consumer/consumer-metadata-cache.h>
52 struct lttng_consumer_global_data consumer_data
= {
55 .type
= LTTNG_CONSUMER_UNKNOWN
,
58 enum consumer_channel_action
{
61 CONSUMER_CHANNEL_QUIT
,
64 struct consumer_channel_msg
{
65 enum consumer_channel_action action
;
66 struct lttng_consumer_channel
*chan
; /* add */
67 uint64_t key
; /* del */
71 * Flag to inform the polling thread to quit when all fd hung up. Updated by
72 * the consumer_thread_receive_fds when it notices that all fds has hung up.
73 * Also updated by the signal handler (consumer_should_exit()). Read by the
76 volatile int consumer_quit
;
79 * Global hash table containing respectively metadata and data streams. The
80 * stream element in this ht should only be updated by the metadata poll thread
81 * for the metadata and the data poll thread for the data.
83 static struct lttng_ht
*metadata_ht
;
84 static struct lttng_ht
*data_ht
;
87 * Notify a thread lttng pipe to poll back again. This usually means that some
88 * global state has changed so we just send back the thread in a poll wait
91 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
93 struct lttng_consumer_stream
*null_stream
= NULL
;
97 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
100 static void notify_health_quit_pipe(int *pipe
)
104 ret
= lttng_write(pipe
[1], "4", 1);
106 PERROR("write consumer health quit");
110 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
111 struct lttng_consumer_channel
*chan
,
113 enum consumer_channel_action action
)
115 struct consumer_channel_msg msg
;
118 memset(&msg
, 0, sizeof(msg
));
123 ret
= lttng_write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
124 if (ret
< sizeof(msg
)) {
125 PERROR("notify_channel_pipe write error");
129 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
132 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
135 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
136 struct lttng_consumer_channel
**chan
,
138 enum consumer_channel_action
*action
)
140 struct consumer_channel_msg msg
;
143 ret
= lttng_read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
144 if (ret
< sizeof(msg
)) {
148 *action
= msg
.action
;
156 * Cleanup the stream list of a channel. Those streams are not yet globally
159 static void clean_channel_stream_list(struct lttng_consumer_channel
*channel
)
161 struct lttng_consumer_stream
*stream
, *stmp
;
165 /* Delete streams that might have been left in the stream list. */
166 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
168 cds_list_del(&stream
->send_node
);
170 * Once a stream is added to this list, the buffers were created so we
171 * have a guarantee that this call will succeed. Setting the monitor
172 * mode to 0 so we don't lock nor try to delete the stream from the
176 consumer_stream_destroy(stream
, NULL
);
181 * Find a stream. The consumer_data.lock must be locked during this
184 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
187 struct lttng_ht_iter iter
;
188 struct lttng_ht_node_u64
*node
;
189 struct lttng_consumer_stream
*stream
= NULL
;
193 /* -1ULL keys are lookup failures */
194 if (key
== (uint64_t) -1ULL) {
200 lttng_ht_lookup(ht
, &key
, &iter
);
201 node
= lttng_ht_iter_get_node_u64(&iter
);
203 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
211 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
213 struct lttng_consumer_stream
*stream
;
216 stream
= find_stream(key
, ht
);
218 stream
->key
= (uint64_t) -1ULL;
220 * We don't want the lookup to match, but we still need
221 * to iterate on this stream when iterating over the hash table. Just
222 * change the node key.
224 stream
->node
.key
= (uint64_t) -1ULL;
230 * Return a channel object for the given key.
232 * RCU read side lock MUST be acquired before calling this function and
233 * protects the channel ptr.
235 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
237 struct lttng_ht_iter iter
;
238 struct lttng_ht_node_u64
*node
;
239 struct lttng_consumer_channel
*channel
= NULL
;
241 /* -1ULL keys are lookup failures */
242 if (key
== (uint64_t) -1ULL) {
246 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
247 node
= lttng_ht_iter_get_node_u64(&iter
);
249 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
256 * There is a possibility that the consumer does not have enough time between
257 * the close of the channel on the session daemon and the cleanup in here thus
258 * once we have a channel add with an existing key, we know for sure that this
259 * channel will eventually get cleaned up by all streams being closed.
261 * This function just nullifies the already existing channel key.
263 static void steal_channel_key(uint64_t key
)
265 struct lttng_consumer_channel
*channel
;
268 channel
= consumer_find_channel(key
);
270 channel
->key
= (uint64_t) -1ULL;
272 * We don't want the lookup to match, but we still need to iterate on
273 * this channel when iterating over the hash table. Just change the
276 channel
->node
.key
= (uint64_t) -1ULL;
281 static void free_channel_rcu(struct rcu_head
*head
)
283 struct lttng_ht_node_u64
*node
=
284 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
285 struct lttng_consumer_channel
*channel
=
286 caa_container_of(node
, struct lttng_consumer_channel
, node
);
288 switch (consumer_data
.type
) {
289 case LTTNG_CONSUMER_KERNEL
:
291 case LTTNG_CONSUMER32_UST
:
292 case LTTNG_CONSUMER64_UST
:
293 lttng_ustconsumer_free_channel(channel
);
296 ERR("Unknown consumer_data type");
303 * RCU protected relayd socket pair free.
305 static void free_relayd_rcu(struct rcu_head
*head
)
307 struct lttng_ht_node_u64
*node
=
308 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
309 struct consumer_relayd_sock_pair
*relayd
=
310 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
313 * Close all sockets. This is done in the call RCU since we don't want the
314 * socket fds to be reassigned thus potentially creating bad state of the
317 * We do not have to lock the control socket mutex here since at this stage
318 * there is no one referencing to this relayd object.
320 (void) relayd_close(&relayd
->control_sock
);
321 (void) relayd_close(&relayd
->data_sock
);
323 pthread_mutex_destroy(&relayd
->ctrl_sock_mutex
);
328 * Destroy and free relayd socket pair object.
330 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
333 struct lttng_ht_iter iter
;
335 if (relayd
== NULL
) {
339 DBG("Consumer destroy and close relayd socket pair");
341 iter
.iter
.node
= &relayd
->node
.node
;
342 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
344 /* We assume the relayd is being or is destroyed */
348 /* RCU free() call */
349 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
353 * Remove a channel from the global list protected by a mutex. This function is
354 * also responsible for freeing its data structures.
356 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
359 struct lttng_ht_iter iter
;
361 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
363 pthread_mutex_lock(&consumer_data
.lock
);
364 pthread_mutex_lock(&channel
->lock
);
366 /* Destroy streams that might have been left in the stream list. */
367 clean_channel_stream_list(channel
);
369 if (channel
->live_timer_enabled
== 1) {
370 consumer_timer_live_stop(channel
);
373 switch (consumer_data
.type
) {
374 case LTTNG_CONSUMER_KERNEL
:
376 case LTTNG_CONSUMER32_UST
:
377 case LTTNG_CONSUMER64_UST
:
378 lttng_ustconsumer_del_channel(channel
);
381 ERR("Unknown consumer_data type");
387 iter
.iter
.node
= &channel
->node
.node
;
388 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
392 call_rcu(&channel
->node
.head
, free_channel_rcu
);
394 pthread_mutex_unlock(&channel
->lock
);
395 pthread_mutex_unlock(&consumer_data
.lock
);
399 * Iterate over the relayd hash table and destroy each element. Finally,
400 * destroy the whole hash table.
402 static void cleanup_relayd_ht(void)
404 struct lttng_ht_iter iter
;
405 struct consumer_relayd_sock_pair
*relayd
;
409 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
411 consumer_destroy_relayd(relayd
);
416 lttng_ht_destroy(consumer_data
.relayd_ht
);
420 * Update the end point status of all streams having the given network sequence
421 * index (relayd index).
423 * It's atomically set without having the stream mutex locked which is fine
424 * because we handle the write/read race with a pipe wakeup for each thread.
426 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
427 enum consumer_endpoint_status status
)
429 struct lttng_ht_iter iter
;
430 struct lttng_consumer_stream
*stream
;
432 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
436 /* Let's begin with metadata */
437 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
438 if (stream
->net_seq_idx
== net_seq_idx
) {
439 uatomic_set(&stream
->endpoint_status
, status
);
440 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
444 /* Follow up by the data streams */
445 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
446 if (stream
->net_seq_idx
== net_seq_idx
) {
447 uatomic_set(&stream
->endpoint_status
, status
);
448 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
455 * Cleanup a relayd object by flagging every associated streams for deletion,
456 * destroying the object meaning removing it from the relayd hash table,
457 * closing the sockets and freeing the memory in a RCU call.
459 * If a local data context is available, notify the threads that the streams'
460 * state have changed.
462 void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
)
468 DBG("Cleaning up relayd object ID %"PRIu64
, relayd
->net_seq_idx
);
470 /* Save the net sequence index before destroying the object */
471 netidx
= relayd
->net_seq_idx
;
474 * Delete the relayd from the relayd hash table, close the sockets and free
475 * the object in a RCU call.
477 consumer_destroy_relayd(relayd
);
479 /* Set inactive endpoint to all streams */
480 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
483 * With a local data context, notify the threads that the streams' state
484 * have changed. The write() action on the pipe acts as an "implicit"
485 * memory barrier ordering the updates of the end point status from the
486 * read of this status which happens AFTER receiving this notify.
488 notify_thread_lttng_pipe(relayd
->ctx
->consumer_data_pipe
);
489 notify_thread_lttng_pipe(relayd
->ctx
->consumer_metadata_pipe
);
493 * Flag a relayd socket pair for destruction. Destroy it if the refcount
496 * RCU read side lock MUST be aquired before calling this function.
498 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
502 /* Set destroy flag for this object */
503 uatomic_set(&relayd
->destroy_flag
, 1);
505 /* Destroy the relayd if refcount is 0 */
506 if (uatomic_read(&relayd
->refcount
) == 0) {
507 consumer_destroy_relayd(relayd
);
512 * Completly destroy stream from every visiable data structure and the given
515 * One this call returns, the stream object is not longer usable nor visible.
517 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
520 consumer_stream_destroy(stream
, ht
);
524 * XXX naming of del vs destroy is all mixed up.
526 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
528 consumer_stream_destroy(stream
, data_ht
);
531 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
533 consumer_stream_destroy(stream
, metadata_ht
);
536 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
538 enum lttng_consumer_stream_state state
,
539 const char *channel_name
,
546 enum consumer_channel_type type
,
547 unsigned int monitor
)
550 struct lttng_consumer_stream
*stream
;
552 stream
= zmalloc(sizeof(*stream
));
553 if (stream
== NULL
) {
554 PERROR("malloc struct lttng_consumer_stream");
561 stream
->key
= stream_key
;
563 stream
->out_fd_offset
= 0;
564 stream
->output_written
= 0;
565 stream
->state
= state
;
568 stream
->net_seq_idx
= relayd_id
;
569 stream
->session_id
= session_id
;
570 stream
->monitor
= monitor
;
571 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
572 stream
->index_file
= NULL
;
573 stream
->last_sequence_number
= -1ULL;
574 pthread_mutex_init(&stream
->lock
, NULL
);
575 pthread_mutex_init(&stream
->metadata_timer_lock
, NULL
);
577 /* If channel is the metadata, flag this stream as metadata. */
578 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
579 stream
->metadata_flag
= 1;
580 /* Metadata is flat out. */
581 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
582 /* Live rendez-vous point. */
583 pthread_cond_init(&stream
->metadata_rdv
, NULL
);
584 pthread_mutex_init(&stream
->metadata_rdv_lock
, NULL
);
586 /* Format stream name to <channel_name>_<cpu_number> */
587 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
590 PERROR("snprintf stream name");
595 /* Key is always the wait_fd for streams. */
596 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
598 /* Init node per channel id key */
599 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
601 /* Init session id node with the stream session id */
602 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
604 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
605 " relayd_id %" PRIu64
", session_id %" PRIu64
,
606 stream
->name
, stream
->key
, channel_key
,
607 stream
->net_seq_idx
, stream
->session_id
);
623 * Add a stream to the global list protected by a mutex.
625 int consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
627 struct lttng_ht
*ht
= data_ht
;
633 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
635 pthread_mutex_lock(&consumer_data
.lock
);
636 pthread_mutex_lock(&stream
->chan
->lock
);
637 pthread_mutex_lock(&stream
->chan
->timer_lock
);
638 pthread_mutex_lock(&stream
->lock
);
641 /* Steal stream identifier to avoid having streams with the same key */
642 steal_stream_key(stream
->key
, ht
);
644 lttng_ht_add_unique_u64(ht
, &stream
->node
);
646 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
647 &stream
->node_channel_id
);
650 * Add stream to the stream_list_ht of the consumer data. No need to steal
651 * the key since the HT does not use it and we allow to add redundant keys
654 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
657 * When nb_init_stream_left reaches 0, we don't need to trigger any action
658 * in terms of destroying the associated channel, because the action that
659 * causes the count to become 0 also causes a stream to be added. The
660 * channel deletion will thus be triggered by the following removal of this
663 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
664 /* Increment refcount before decrementing nb_init_stream_left */
666 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
669 /* Update consumer data once the node is inserted. */
670 consumer_data
.stream_count
++;
671 consumer_data
.need_update
= 1;
674 pthread_mutex_unlock(&stream
->lock
);
675 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
676 pthread_mutex_unlock(&stream
->chan
->lock
);
677 pthread_mutex_unlock(&consumer_data
.lock
);
682 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
684 consumer_del_stream(stream
, data_ht
);
688 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
689 * be acquired before calling this.
691 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
694 struct lttng_ht_node_u64
*node
;
695 struct lttng_ht_iter iter
;
699 lttng_ht_lookup(consumer_data
.relayd_ht
,
700 &relayd
->net_seq_idx
, &iter
);
701 node
= lttng_ht_iter_get_node_u64(&iter
);
705 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
712 * Allocate and return a consumer relayd socket.
714 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
715 uint64_t net_seq_idx
)
717 struct consumer_relayd_sock_pair
*obj
= NULL
;
719 /* net sequence index of -1 is a failure */
720 if (net_seq_idx
== (uint64_t) -1ULL) {
724 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
726 PERROR("zmalloc relayd sock");
730 obj
->net_seq_idx
= net_seq_idx
;
732 obj
->destroy_flag
= 0;
733 obj
->control_sock
.sock
.fd
= -1;
734 obj
->data_sock
.sock
.fd
= -1;
735 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
736 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
743 * Find a relayd socket pair in the global consumer data.
745 * Return the object if found else NULL.
746 * RCU read-side lock must be held across this call and while using the
749 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
751 struct lttng_ht_iter iter
;
752 struct lttng_ht_node_u64
*node
;
753 struct consumer_relayd_sock_pair
*relayd
= NULL
;
755 /* Negative keys are lookup failures */
756 if (key
== (uint64_t) -1ULL) {
760 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
762 node
= lttng_ht_iter_get_node_u64(&iter
);
764 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
772 * Find a relayd and send the stream
774 * Returns 0 on success, < 0 on error
776 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
780 struct consumer_relayd_sock_pair
*relayd
;
783 assert(stream
->net_seq_idx
!= -1ULL);
786 /* The stream is not metadata. Get relayd reference if exists. */
788 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
789 if (relayd
!= NULL
) {
790 /* Add stream on the relayd */
791 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
792 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
793 path
, &stream
->relayd_stream_id
,
794 stream
->chan
->tracefile_size
, stream
->chan
->tracefile_count
);
795 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
797 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
798 lttng_consumer_cleanup_relayd(relayd
);
802 uatomic_inc(&relayd
->refcount
);
803 stream
->sent_to_relayd
= 1;
805 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
806 stream
->key
, stream
->net_seq_idx
);
811 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
812 stream
->name
, stream
->key
, stream
->net_seq_idx
);
820 * Find a relayd and send the streams sent message
822 * Returns 0 on success, < 0 on error
824 int consumer_send_relayd_streams_sent(uint64_t net_seq_idx
)
827 struct consumer_relayd_sock_pair
*relayd
;
829 assert(net_seq_idx
!= -1ULL);
831 /* The stream is not metadata. Get relayd reference if exists. */
833 relayd
= consumer_find_relayd(net_seq_idx
);
834 if (relayd
!= NULL
) {
835 /* Add stream on the relayd */
836 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
837 ret
= relayd_streams_sent(&relayd
->control_sock
);
838 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
840 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
841 lttng_consumer_cleanup_relayd(relayd
);
845 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
852 DBG("All streams sent relayd id %" PRIu64
, net_seq_idx
);
860 * Find a relayd and close the stream
862 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
864 struct consumer_relayd_sock_pair
*relayd
;
866 /* The stream is not metadata. Get relayd reference if exists. */
868 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
870 consumer_stream_relayd_close(stream
, relayd
);
876 * Handle stream for relayd transmission if the stream applies for network
877 * streaming where the net sequence index is set.
879 * Return destination file descriptor or negative value on error.
881 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
882 size_t data_size
, unsigned long padding
,
883 struct consumer_relayd_sock_pair
*relayd
)
886 struct lttcomm_relayd_data_hdr data_hdr
;
892 /* Reset data header */
893 memset(&data_hdr
, 0, sizeof(data_hdr
));
895 if (stream
->metadata_flag
) {
896 /* Caller MUST acquire the relayd control socket lock */
897 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
902 /* Metadata are always sent on the control socket. */
903 outfd
= relayd
->control_sock
.sock
.fd
;
905 /* Set header with stream information */
906 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
907 data_hdr
.data_size
= htobe32(data_size
);
908 data_hdr
.padding_size
= htobe32(padding
);
910 * Note that net_seq_num below is assigned with the *current* value of
911 * next_net_seq_num and only after that the next_net_seq_num will be
912 * increment. This is why when issuing a command on the relayd using
913 * this next value, 1 should always be substracted in order to compare
914 * the last seen sequence number on the relayd side to the last sent.
916 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
917 /* Other fields are zeroed previously */
919 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
925 ++stream
->next_net_seq_num
;
927 /* Set to go on data socket */
928 outfd
= relayd
->data_sock
.sock
.fd
;
936 * Allocate and return a new lttng_consumer_channel object using the given key
937 * to initialize the hash table node.
939 * On error, return NULL.
941 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
943 const char *pathname
,
948 enum lttng_event_output output
,
949 uint64_t tracefile_size
,
950 uint64_t tracefile_count
,
951 uint64_t session_id_per_pid
,
952 unsigned int monitor
,
953 unsigned int live_timer_interval
,
954 const char *root_shm_path
,
955 const char *shm_path
)
957 struct lttng_consumer_channel
*channel
;
959 channel
= zmalloc(sizeof(*channel
));
960 if (channel
== NULL
) {
961 PERROR("malloc struct lttng_consumer_channel");
966 channel
->refcount
= 0;
967 channel
->session_id
= session_id
;
968 channel
->session_id_per_pid
= session_id_per_pid
;
971 channel
->relayd_id
= relayd_id
;
972 channel
->tracefile_size
= tracefile_size
;
973 channel
->tracefile_count
= tracefile_count
;
974 channel
->monitor
= monitor
;
975 channel
->live_timer_interval
= live_timer_interval
;
976 pthread_mutex_init(&channel
->lock
, NULL
);
977 pthread_mutex_init(&channel
->timer_lock
, NULL
);
980 case LTTNG_EVENT_SPLICE
:
981 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
983 case LTTNG_EVENT_MMAP
:
984 channel
->output
= CONSUMER_CHANNEL_MMAP
;
994 * In monitor mode, the streams associated with the channel will be put in
995 * a special list ONLY owned by this channel. So, the refcount is set to 1
996 * here meaning that the channel itself has streams that are referenced.
998 * On a channel deletion, once the channel is no longer visible, the
999 * refcount is decremented and checked for a zero value to delete it. With
1000 * streams in no monitor mode, it will now be safe to destroy the channel.
1002 if (!channel
->monitor
) {
1003 channel
->refcount
= 1;
1006 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1007 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1009 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1010 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1012 if (root_shm_path
) {
1013 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1014 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1017 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1018 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1021 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1023 channel
->wait_fd
= -1;
1025 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1027 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1034 * Add a channel to the global list protected by a mutex.
1036 * Always return 0 indicating success.
1038 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1039 struct lttng_consumer_local_data
*ctx
)
1041 pthread_mutex_lock(&consumer_data
.lock
);
1042 pthread_mutex_lock(&channel
->lock
);
1043 pthread_mutex_lock(&channel
->timer_lock
);
1046 * This gives us a guarantee that the channel we are about to add to the
1047 * channel hash table will be unique. See this function comment on the why
1048 * we need to steel the channel key at this stage.
1050 steal_channel_key(channel
->key
);
1053 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1056 pthread_mutex_unlock(&channel
->timer_lock
);
1057 pthread_mutex_unlock(&channel
->lock
);
1058 pthread_mutex_unlock(&consumer_data
.lock
);
1060 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1061 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1068 * Allocate the pollfd structure and the local view of the out fds to avoid
1069 * doing a lookup in the linked list and concurrency issues when writing is
1070 * needed. Called with consumer_data.lock held.
1072 * Returns the number of fds in the structures.
1074 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1075 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1076 struct lttng_ht
*ht
, int *nb_inactive_fd
)
1079 struct lttng_ht_iter iter
;
1080 struct lttng_consumer_stream
*stream
;
1085 assert(local_stream
);
1087 DBG("Updating poll fd array");
1088 *nb_inactive_fd
= 0;
1090 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1092 * Only active streams with an active end point can be added to the
1093 * poll set and local stream storage of the thread.
1095 * There is a potential race here for endpoint_status to be updated
1096 * just after the check. However, this is OK since the stream(s) will
1097 * be deleted once the thread is notified that the end point state has
1098 * changed where this function will be called back again.
1100 * We track the number of inactive FDs because they still need to be
1101 * closed by the polling thread after a wakeup on the data_pipe or
1104 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
1105 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1106 (*nb_inactive_fd
)++;
1110 * This clobbers way too much the debug output. Uncomment that if you
1111 * need it for debugging purposes.
1113 * DBG("Active FD %d", stream->wait_fd);
1115 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1116 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1117 local_stream
[i
] = stream
;
1123 * Insert the consumer_data_pipe at the end of the array and don't
1124 * increment i so nb_fd is the number of real FD.
1126 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1127 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1129 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1130 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1135 * Poll on the should_quit pipe and the command socket return -1 on
1136 * error, 1 if should exit, 0 if data is available on the command socket
1138 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1143 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1144 if (num_rdy
== -1) {
1146 * Restart interrupted system call.
1148 if (errno
== EINTR
) {
1151 PERROR("Poll error");
1154 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1155 DBG("consumer_should_quit wake up");
1162 * Set the error socket.
1164 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1167 ctx
->consumer_error_socket
= sock
;
1171 * Set the command socket path.
1173 void lttng_consumer_set_command_sock_path(
1174 struct lttng_consumer_local_data
*ctx
, char *sock
)
1176 ctx
->consumer_command_sock_path
= sock
;
1180 * Send return code to the session daemon.
1181 * If the socket is not defined, we return 0, it is not a fatal error
1183 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1185 if (ctx
->consumer_error_socket
> 0) {
1186 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1187 sizeof(enum lttcomm_sessiond_command
));
1194 * Close all the tracefiles and stream fds and MUST be called when all
1195 * instances are destroyed i.e. when all threads were joined and are ended.
1197 void lttng_consumer_cleanup(void)
1199 struct lttng_ht_iter iter
;
1200 struct lttng_consumer_channel
*channel
;
1204 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1206 consumer_del_channel(channel
);
1211 lttng_ht_destroy(consumer_data
.channel_ht
);
1213 cleanup_relayd_ht();
1215 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1218 * This HT contains streams that are freed by either the metadata thread or
1219 * the data thread so we do *nothing* on the hash table and simply destroy
1222 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1226 * Called from signal handler.
1228 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1233 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1235 PERROR("write consumer quit");
1238 DBG("Consumer flag that it should quit");
1243 * Flush pending writes to trace output disk file.
1246 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1250 int outfd
= stream
->out_fd
;
1253 * This does a blocking write-and-wait on any page that belongs to the
1254 * subbuffer prior to the one we just wrote.
1255 * Don't care about error values, as these are just hints and ways to
1256 * limit the amount of page cache used.
1258 if (orig_offset
< stream
->max_sb_size
) {
1261 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1262 stream
->max_sb_size
,
1263 SYNC_FILE_RANGE_WAIT_BEFORE
1264 | SYNC_FILE_RANGE_WRITE
1265 | SYNC_FILE_RANGE_WAIT_AFTER
);
1267 * Give hints to the kernel about how we access the file:
1268 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1271 * We need to call fadvise again after the file grows because the
1272 * kernel does not seem to apply fadvise to non-existing parts of the
1275 * Call fadvise _after_ having waited for the page writeback to
1276 * complete because the dirty page writeback semantic is not well
1277 * defined. So it can be expected to lead to lower throughput in
1280 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1281 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1282 if (ret
&& ret
!= -ENOSYS
) {
1284 PERROR("posix_fadvise on fd %i", outfd
);
1289 * Initialise the necessary environnement :
1290 * - create a new context
1291 * - create the poll_pipe
1292 * - create the should_quit pipe (for signal handler)
1293 * - create the thread pipe (for splice)
1295 * Takes a function pointer as argument, this function is called when data is
1296 * available on a buffer. This function is responsible to do the
1297 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1298 * buffer configuration and then kernctl_put_next_subbuf at the end.
1300 * Returns a pointer to the new context or NULL on error.
1302 struct lttng_consumer_local_data
*lttng_consumer_create(
1303 enum lttng_consumer_type type
,
1304 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1305 struct lttng_consumer_local_data
*ctx
),
1306 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1307 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1308 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1311 struct lttng_consumer_local_data
*ctx
;
1313 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1314 consumer_data
.type
== type
);
1315 consumer_data
.type
= type
;
1317 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1319 PERROR("allocating context");
1323 ctx
->consumer_error_socket
= -1;
1324 ctx
->consumer_metadata_socket
= -1;
1325 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1326 /* assign the callbacks */
1327 ctx
->on_buffer_ready
= buffer_ready
;
1328 ctx
->on_recv_channel
= recv_channel
;
1329 ctx
->on_recv_stream
= recv_stream
;
1330 ctx
->on_update_stream
= update_stream
;
1332 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1333 if (!ctx
->consumer_data_pipe
) {
1334 goto error_poll_pipe
;
1337 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1338 if (!ctx
->consumer_wakeup_pipe
) {
1339 goto error_wakeup_pipe
;
1342 ret
= pipe(ctx
->consumer_should_quit
);
1344 PERROR("Error creating recv pipe");
1345 goto error_quit_pipe
;
1348 ret
= pipe(ctx
->consumer_channel_pipe
);
1350 PERROR("Error creating channel pipe");
1351 goto error_channel_pipe
;
1354 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1355 if (!ctx
->consumer_metadata_pipe
) {
1356 goto error_metadata_pipe
;
1361 error_metadata_pipe
:
1362 utils_close_pipe(ctx
->consumer_channel_pipe
);
1364 utils_close_pipe(ctx
->consumer_should_quit
);
1366 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1368 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1376 * Iterate over all streams of the hashtable and free them properly.
1378 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1380 struct lttng_ht_iter iter
;
1381 struct lttng_consumer_stream
*stream
;
1388 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1390 * Ignore return value since we are currently cleaning up so any error
1393 (void) consumer_del_stream(stream
, ht
);
1397 lttng_ht_destroy(ht
);
1401 * Iterate over all streams of the metadata hashtable and free them
1404 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1406 struct lttng_ht_iter iter
;
1407 struct lttng_consumer_stream
*stream
;
1414 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1416 * Ignore return value since we are currently cleaning up so any error
1419 (void) consumer_del_metadata_stream(stream
, ht
);
1423 lttng_ht_destroy(ht
);
1427 * Close all fds associated with the instance and free the context.
1429 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1433 DBG("Consumer destroying it. Closing everything.");
1439 destroy_data_stream_ht(data_ht
);
1440 destroy_metadata_stream_ht(metadata_ht
);
1442 ret
= close(ctx
->consumer_error_socket
);
1446 ret
= close(ctx
->consumer_metadata_socket
);
1450 utils_close_pipe(ctx
->consumer_channel_pipe
);
1451 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1452 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1453 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1454 utils_close_pipe(ctx
->consumer_should_quit
);
1456 unlink(ctx
->consumer_command_sock_path
);
1461 * Write the metadata stream id on the specified file descriptor.
1463 static int write_relayd_metadata_id(int fd
,
1464 struct lttng_consumer_stream
*stream
,
1465 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1468 struct lttcomm_relayd_metadata_payload hdr
;
1470 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1471 hdr
.padding_size
= htobe32(padding
);
1472 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1473 if (ret
< sizeof(hdr
)) {
1475 * This error means that the fd's end is closed so ignore the PERROR
1476 * not to clubber the error output since this can happen in a normal
1479 if (errno
!= EPIPE
) {
1480 PERROR("write metadata stream id");
1482 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1484 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1485 * handle writting the missing part so report that as an error and
1486 * don't lie to the caller.
1491 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1492 stream
->relayd_stream_id
, padding
);
1499 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1500 * core function for writing trace buffers to either the local filesystem or
1503 * It must be called with the stream lock held.
1505 * Careful review MUST be put if any changes occur!
1507 * Returns the number of bytes written
1509 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1510 struct lttng_consumer_local_data
*ctx
,
1511 struct lttng_consumer_stream
*stream
, unsigned long len
,
1512 unsigned long padding
,
1513 struct ctf_packet_index
*index
)
1515 unsigned long mmap_offset
;
1518 off_t orig_offset
= stream
->out_fd_offset
;
1519 /* Default is on the disk */
1520 int outfd
= stream
->out_fd
;
1521 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1522 unsigned int relayd_hang_up
= 0;
1524 /* RCU lock for the relayd pointer */
1527 /* Flag that the current stream if set for network streaming. */
1528 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1529 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1530 if (relayd
== NULL
) {
1536 /* get the offset inside the fd to mmap */
1537 switch (consumer_data
.type
) {
1538 case LTTNG_CONSUMER_KERNEL
:
1539 mmap_base
= stream
->mmap_base
;
1540 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1542 PERROR("tracer ctl get_mmap_read_offset");
1546 case LTTNG_CONSUMER32_UST
:
1547 case LTTNG_CONSUMER64_UST
:
1548 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1550 ERR("read mmap get mmap base for stream %s", stream
->name
);
1554 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1556 PERROR("tracer ctl get_mmap_read_offset");
1562 ERR("Unknown consumer_data type");
1566 /* Handle stream on the relayd if the output is on the network */
1568 unsigned long netlen
= len
;
1571 * Lock the control socket for the complete duration of the function
1572 * since from this point on we will use the socket.
1574 if (stream
->metadata_flag
) {
1575 /* Metadata requires the control socket. */
1576 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1577 if (stream
->reset_metadata_flag
) {
1578 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1579 stream
->relayd_stream_id
,
1580 stream
->metadata_version
);
1585 stream
->reset_metadata_flag
= 0;
1587 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1590 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1595 /* Use the returned socket. */
1598 /* Write metadata stream id before payload */
1599 if (stream
->metadata_flag
) {
1600 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1607 /* No streaming, we have to set the len with the full padding */
1610 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1611 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1613 ERR("Reset metadata file");
1616 stream
->reset_metadata_flag
= 0;
1620 * Check if we need to change the tracefile before writing the packet.
1622 if (stream
->chan
->tracefile_size
> 0 &&
1623 (stream
->tracefile_size_current
+ len
) >
1624 stream
->chan
->tracefile_size
) {
1625 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1626 stream
->name
, stream
->chan
->tracefile_size
,
1627 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1628 stream
->out_fd
, &(stream
->tracefile_count_current
),
1631 ERR("Rotating output file");
1634 outfd
= stream
->out_fd
;
1636 if (stream
->index_file
) {
1637 lttng_index_file_put(stream
->index_file
);
1638 stream
->index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1639 stream
->name
, stream
->uid
, stream
->gid
,
1640 stream
->chan
->tracefile_size
,
1641 stream
->tracefile_count_current
,
1642 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1643 if (!stream
->index_file
) {
1648 /* Reset current size because we just perform a rotation. */
1649 stream
->tracefile_size_current
= 0;
1650 stream
->out_fd_offset
= 0;
1653 stream
->tracefile_size_current
+= len
;
1655 index
->offset
= htobe64(stream
->out_fd_offset
);
1660 * This call guarantee that len or less is returned. It's impossible to
1661 * receive a ret value that is bigger than len.
1663 ret
= lttng_write(outfd
, mmap_base
+ mmap_offset
, len
);
1664 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1665 if (ret
< 0 || ((size_t) ret
!= len
)) {
1667 * Report error to caller if nothing was written else at least send the
1675 /* Socket operation failed. We consider the relayd dead */
1676 if (errno
== EPIPE
|| errno
== EINVAL
|| errno
== EBADF
) {
1678 * This is possible if the fd is closed on the other side
1679 * (outfd) or any write problem. It can be verbose a bit for a
1680 * normal execution if for instance the relayd is stopped
1681 * abruptly. This can happen so set this to a DBG statement.
1683 DBG("Consumer mmap write detected relayd hang up");
1685 /* Unhandled error, print it and stop function right now. */
1686 PERROR("Error in write mmap (ret %zd != len %lu)", ret
, len
);
1690 stream
->output_written
+= ret
;
1692 /* This call is useless on a socket so better save a syscall. */
1694 /* This won't block, but will start writeout asynchronously */
1695 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, len
,
1696 SYNC_FILE_RANGE_WRITE
);
1697 stream
->out_fd_offset
+= len
;
1698 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1703 * This is a special case that the relayd has closed its socket. Let's
1704 * cleanup the relayd object and all associated streams.
1706 if (relayd
&& relayd_hang_up
) {
1707 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1708 lttng_consumer_cleanup_relayd(relayd
);
1712 /* Unlock only if ctrl socket used */
1713 if (relayd
&& stream
->metadata_flag
) {
1714 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1722 * Splice the data from the ring buffer to the tracefile.
1724 * It must be called with the stream lock held.
1726 * Returns the number of bytes spliced.
1728 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1729 struct lttng_consumer_local_data
*ctx
,
1730 struct lttng_consumer_stream
*stream
, unsigned long len
,
1731 unsigned long padding
,
1732 struct ctf_packet_index
*index
)
1734 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1736 off_t orig_offset
= stream
->out_fd_offset
;
1737 int fd
= stream
->wait_fd
;
1738 /* Default is on the disk */
1739 int outfd
= stream
->out_fd
;
1740 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1742 unsigned int relayd_hang_up
= 0;
1744 switch (consumer_data
.type
) {
1745 case LTTNG_CONSUMER_KERNEL
:
1747 case LTTNG_CONSUMER32_UST
:
1748 case LTTNG_CONSUMER64_UST
:
1749 /* Not supported for user space tracing */
1752 ERR("Unknown consumer_data type");
1756 /* RCU lock for the relayd pointer */
1759 /* Flag that the current stream if set for network streaming. */
1760 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1761 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1762 if (relayd
== NULL
) {
1767 splice_pipe
= stream
->splice_pipe
;
1769 /* Write metadata stream id before payload */
1771 unsigned long total_len
= len
;
1773 if (stream
->metadata_flag
) {
1775 * Lock the control socket for the complete duration of the function
1776 * since from this point on we will use the socket.
1778 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1780 if (stream
->reset_metadata_flag
) {
1781 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1782 stream
->relayd_stream_id
,
1783 stream
->metadata_version
);
1788 stream
->reset_metadata_flag
= 0;
1790 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1798 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1801 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1807 /* Use the returned socket. */
1810 /* No streaming, we have to set the len with the full padding */
1813 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1814 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1816 ERR("Reset metadata file");
1819 stream
->reset_metadata_flag
= 0;
1822 * Check if we need to change the tracefile before writing the packet.
1824 if (stream
->chan
->tracefile_size
> 0 &&
1825 (stream
->tracefile_size_current
+ len
) >
1826 stream
->chan
->tracefile_size
) {
1827 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1828 stream
->name
, stream
->chan
->tracefile_size
,
1829 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1830 stream
->out_fd
, &(stream
->tracefile_count_current
),
1834 ERR("Rotating output file");
1837 outfd
= stream
->out_fd
;
1839 if (stream
->index_file
) {
1840 lttng_index_file_put(stream
->index_file
);
1841 stream
->index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1842 stream
->name
, stream
->uid
, stream
->gid
,
1843 stream
->chan
->tracefile_size
,
1844 stream
->tracefile_count_current
,
1845 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1846 if (!stream
->index_file
) {
1851 /* Reset current size because we just perform a rotation. */
1852 stream
->tracefile_size_current
= 0;
1853 stream
->out_fd_offset
= 0;
1856 stream
->tracefile_size_current
+= len
;
1857 index
->offset
= htobe64(stream
->out_fd_offset
);
1861 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1862 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1863 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1864 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1865 DBG("splice chan to pipe, ret %zd", ret_splice
);
1866 if (ret_splice
< 0) {
1869 PERROR("Error in relay splice");
1873 /* Handle stream on the relayd if the output is on the network */
1874 if (relayd
&& stream
->metadata_flag
) {
1875 size_t metadata_payload_size
=
1876 sizeof(struct lttcomm_relayd_metadata_payload
);
1878 /* Update counter to fit the spliced data */
1879 ret_splice
+= metadata_payload_size
;
1880 len
+= metadata_payload_size
;
1882 * We do this so the return value can match the len passed as
1883 * argument to this function.
1885 written
-= metadata_payload_size
;
1888 /* Splice data out */
1889 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1890 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1891 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1893 if (ret_splice
< 0) {
1898 } else if (ret_splice
> len
) {
1900 * We don't expect this code path to be executed but you never know
1901 * so this is an extra protection agains a buggy splice().
1904 written
+= ret_splice
;
1905 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1909 /* All good, update current len and continue. */
1913 /* This call is useless on a socket so better save a syscall. */
1915 /* This won't block, but will start writeout asynchronously */
1916 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1917 SYNC_FILE_RANGE_WRITE
);
1918 stream
->out_fd_offset
+= ret_splice
;
1920 stream
->output_written
+= ret_splice
;
1921 written
+= ret_splice
;
1924 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1930 * This is a special case that the relayd has closed its socket. Let's
1931 * cleanup the relayd object and all associated streams.
1933 if (relayd
&& relayd_hang_up
) {
1934 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
1935 lttng_consumer_cleanup_relayd(relayd
);
1936 /* Skip splice error so the consumer does not fail */
1941 /* send the appropriate error description to sessiond */
1944 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1947 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1950 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1955 if (relayd
&& stream
->metadata_flag
) {
1956 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1964 * Take a snapshot for a specific fd
1966 * Returns 0 on success, < 0 on error
1968 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1970 switch (consumer_data
.type
) {
1971 case LTTNG_CONSUMER_KERNEL
:
1972 return lttng_kconsumer_take_snapshot(stream
);
1973 case LTTNG_CONSUMER32_UST
:
1974 case LTTNG_CONSUMER64_UST
:
1975 return lttng_ustconsumer_take_snapshot(stream
);
1977 ERR("Unknown consumer_data type");
1984 * Get the produced position
1986 * Returns 0 on success, < 0 on error
1988 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1991 switch (consumer_data
.type
) {
1992 case LTTNG_CONSUMER_KERNEL
:
1993 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1994 case LTTNG_CONSUMER32_UST
:
1995 case LTTNG_CONSUMER64_UST
:
1996 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1998 ERR("Unknown consumer_data type");
2004 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2005 int sock
, struct pollfd
*consumer_sockpoll
)
2007 switch (consumer_data
.type
) {
2008 case LTTNG_CONSUMER_KERNEL
:
2009 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2010 case LTTNG_CONSUMER32_UST
:
2011 case LTTNG_CONSUMER64_UST
:
2012 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2014 ERR("Unknown consumer_data type");
2020 void lttng_consumer_close_all_metadata(void)
2022 switch (consumer_data
.type
) {
2023 case LTTNG_CONSUMER_KERNEL
:
2025 * The Kernel consumer has a different metadata scheme so we don't
2026 * close anything because the stream will be closed by the session
2030 case LTTNG_CONSUMER32_UST
:
2031 case LTTNG_CONSUMER64_UST
:
2033 * Close all metadata streams. The metadata hash table is passed and
2034 * this call iterates over it by closing all wakeup fd. This is safe
2035 * because at this point we are sure that the metadata producer is
2036 * either dead or blocked.
2038 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2041 ERR("Unknown consumer_data type");
2047 * Clean up a metadata stream and free its memory.
2049 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2050 struct lttng_ht
*ht
)
2052 struct lttng_consumer_channel
*free_chan
= NULL
;
2056 * This call should NEVER receive regular stream. It must always be
2057 * metadata stream and this is crucial for data structure synchronization.
2059 assert(stream
->metadata_flag
);
2061 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2063 pthread_mutex_lock(&consumer_data
.lock
);
2064 pthread_mutex_lock(&stream
->chan
->lock
);
2065 pthread_mutex_lock(&stream
->lock
);
2066 if (stream
->chan
->metadata_cache
) {
2067 /* Only applicable to userspace consumers. */
2068 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2071 /* Remove any reference to that stream. */
2072 consumer_stream_delete(stream
, ht
);
2074 /* Close down everything including the relayd if one. */
2075 consumer_stream_close(stream
);
2076 /* Destroy tracer buffers of the stream. */
2077 consumer_stream_destroy_buffers(stream
);
2079 /* Atomically decrement channel refcount since other threads can use it. */
2080 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
2081 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
2082 /* Go for channel deletion! */
2083 free_chan
= stream
->chan
;
2087 * Nullify the stream reference so it is not used after deletion. The
2088 * channel lock MUST be acquired before being able to check for a NULL
2091 stream
->chan
->metadata_stream
= NULL
;
2093 if (stream
->chan
->metadata_cache
) {
2094 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2096 pthread_mutex_unlock(&stream
->lock
);
2097 pthread_mutex_unlock(&stream
->chan
->lock
);
2098 pthread_mutex_unlock(&consumer_data
.lock
);
2101 consumer_del_channel(free_chan
);
2104 consumer_stream_free(stream
);
2108 * Action done with the metadata stream when adding it to the consumer internal
2109 * data structures to handle it.
2111 int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2113 struct lttng_ht
*ht
= metadata_ht
;
2115 struct lttng_ht_iter iter
;
2116 struct lttng_ht_node_u64
*node
;
2121 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2123 pthread_mutex_lock(&consumer_data
.lock
);
2124 pthread_mutex_lock(&stream
->chan
->lock
);
2125 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2126 pthread_mutex_lock(&stream
->lock
);
2129 * From here, refcounts are updated so be _careful_ when returning an error
2136 * Lookup the stream just to make sure it does not exist in our internal
2137 * state. This should NEVER happen.
2139 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2140 node
= lttng_ht_iter_get_node_u64(&iter
);
2144 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2145 * in terms of destroying the associated channel, because the action that
2146 * causes the count to become 0 also causes a stream to be added. The
2147 * channel deletion will thus be triggered by the following removal of this
2150 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2151 /* Increment refcount before decrementing nb_init_stream_left */
2153 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2156 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2158 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
2159 &stream
->node_channel_id
);
2162 * Add stream to the stream_list_ht of the consumer data. No need to steal
2163 * the key since the HT does not use it and we allow to add redundant keys
2166 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2170 pthread_mutex_unlock(&stream
->lock
);
2171 pthread_mutex_unlock(&stream
->chan
->lock
);
2172 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2173 pthread_mutex_unlock(&consumer_data
.lock
);
2178 * Delete data stream that are flagged for deletion (endpoint_status).
2180 static void validate_endpoint_status_data_stream(void)
2182 struct lttng_ht_iter iter
;
2183 struct lttng_consumer_stream
*stream
;
2185 DBG("Consumer delete flagged data stream");
2188 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2189 /* Validate delete flag of the stream */
2190 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2193 /* Delete it right now */
2194 consumer_del_stream(stream
, data_ht
);
2200 * Delete metadata stream that are flagged for deletion (endpoint_status).
2202 static void validate_endpoint_status_metadata_stream(
2203 struct lttng_poll_event
*pollset
)
2205 struct lttng_ht_iter iter
;
2206 struct lttng_consumer_stream
*stream
;
2208 DBG("Consumer delete flagged metadata stream");
2213 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2214 /* Validate delete flag of the stream */
2215 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2219 * Remove from pollset so the metadata thread can continue without
2220 * blocking on a deleted stream.
2222 lttng_poll_del(pollset
, stream
->wait_fd
);
2224 /* Delete it right now */
2225 consumer_del_metadata_stream(stream
, metadata_ht
);
2231 * Thread polls on metadata file descriptor and write them on disk or on the
2234 void *consumer_thread_metadata_poll(void *data
)
2236 int ret
, i
, pollfd
, err
= -1;
2237 uint32_t revents
, nb_fd
;
2238 struct lttng_consumer_stream
*stream
= NULL
;
2239 struct lttng_ht_iter iter
;
2240 struct lttng_ht_node_u64
*node
;
2241 struct lttng_poll_event events
;
2242 struct lttng_consumer_local_data
*ctx
= data
;
2245 rcu_register_thread();
2247 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2249 if (testpoint(consumerd_thread_metadata
)) {
2250 goto error_testpoint
;
2253 health_code_update();
2255 DBG("Thread metadata poll started");
2257 /* Size is set to 1 for the consumer_metadata pipe */
2258 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2260 ERR("Poll set creation failed");
2264 ret
= lttng_poll_add(&events
,
2265 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2271 DBG("Metadata main loop started");
2275 health_code_update();
2276 health_poll_entry();
2277 DBG("Metadata poll wait");
2278 ret
= lttng_poll_wait(&events
, -1);
2279 DBG("Metadata poll return from wait with %d fd(s)",
2280 LTTNG_POLL_GETNB(&events
));
2282 DBG("Metadata event caught in thread");
2284 if (errno
== EINTR
) {
2285 ERR("Poll EINTR caught");
2288 if (LTTNG_POLL_GETNB(&events
) == 0) {
2289 err
= 0; /* All is OK */
2296 /* From here, the event is a metadata wait fd */
2297 for (i
= 0; i
< nb_fd
; i
++) {
2298 health_code_update();
2300 revents
= LTTNG_POLL_GETEV(&events
, i
);
2301 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2304 /* No activity for this FD (poll implementation). */
2308 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2309 if (revents
& LPOLLIN
) {
2312 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2313 &stream
, sizeof(stream
));
2314 if (pipe_len
< sizeof(stream
)) {
2316 PERROR("read metadata stream");
2319 * Remove the pipe from the poll set and continue the loop
2320 * since their might be data to consume.
2322 lttng_poll_del(&events
,
2323 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2324 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2328 /* A NULL stream means that the state has changed. */
2329 if (stream
== NULL
) {
2330 /* Check for deleted streams. */
2331 validate_endpoint_status_metadata_stream(&events
);
2335 DBG("Adding metadata stream %d to poll set",
2338 /* Add metadata stream to the global poll events list */
2339 lttng_poll_add(&events
, stream
->wait_fd
,
2340 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2341 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2342 DBG("Metadata thread pipe hung up");
2344 * Remove the pipe from the poll set and continue the loop
2345 * since their might be data to consume.
2347 lttng_poll_del(&events
,
2348 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2349 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2352 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2356 /* Handle other stream */
2362 uint64_t tmp_id
= (uint64_t) pollfd
;
2364 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2366 node
= lttng_ht_iter_get_node_u64(&iter
);
2369 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2372 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2373 /* Get the data out of the metadata file descriptor */
2374 DBG("Metadata available on fd %d", pollfd
);
2375 assert(stream
->wait_fd
== pollfd
);
2378 health_code_update();
2380 len
= ctx
->on_buffer_ready(stream
, ctx
);
2382 * We don't check the return value here since if we get
2383 * a negative len, it means an error occurred thus we
2384 * simply remove it from the poll set and free the
2389 /* It's ok to have an unavailable sub-buffer */
2390 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2391 /* Clean up stream from consumer and free it. */
2392 lttng_poll_del(&events
, stream
->wait_fd
);
2393 consumer_del_metadata_stream(stream
, metadata_ht
);
2395 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2396 DBG("Metadata fd %d is hup|err.", pollfd
);
2397 if (!stream
->hangup_flush_done
2398 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2399 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2400 DBG("Attempting to flush and consume the UST buffers");
2401 lttng_ustconsumer_on_stream_hangup(stream
);
2403 /* We just flushed the stream now read it. */
2405 health_code_update();
2407 len
= ctx
->on_buffer_ready(stream
, ctx
);
2409 * We don't check the return value here since if we get
2410 * a negative len, it means an error occurred thus we
2411 * simply remove it from the poll set and free the
2417 lttng_poll_del(&events
, stream
->wait_fd
);
2419 * This call update the channel states, closes file descriptors
2420 * and securely free the stream.
2422 consumer_del_metadata_stream(stream
, metadata_ht
);
2424 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2428 /* Release RCU lock for the stream looked up */
2436 DBG("Metadata poll thread exiting");
2438 lttng_poll_clean(&events
);
2443 ERR("Health error occurred in %s", __func__
);
2445 health_unregister(health_consumerd
);
2446 rcu_unregister_thread();
2451 * This thread polls the fds in the set to consume the data and write
2452 * it to tracefile if necessary.
2454 void *consumer_thread_data_poll(void *data
)
2456 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2457 struct pollfd
*pollfd
= NULL
;
2458 /* local view of the streams */
2459 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2460 /* local view of consumer_data.fds_count */
2462 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2463 int nb_inactive_fd
= 0;
2464 struct lttng_consumer_local_data
*ctx
= data
;
2467 rcu_register_thread();
2469 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2471 if (testpoint(consumerd_thread_data
)) {
2472 goto error_testpoint
;
2475 health_code_update();
2477 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2478 if (local_stream
== NULL
) {
2479 PERROR("local_stream malloc");
2484 health_code_update();
2490 * the fds set has been updated, we need to update our
2491 * local array as well
2493 pthread_mutex_lock(&consumer_data
.lock
);
2494 if (consumer_data
.need_update
) {
2499 local_stream
= NULL
;
2502 * Allocate for all fds +1 for the consumer_data_pipe and +1 for
2505 pollfd
= zmalloc((consumer_data
.stream_count
+ 2) * sizeof(struct pollfd
));
2506 if (pollfd
== NULL
) {
2507 PERROR("pollfd malloc");
2508 pthread_mutex_unlock(&consumer_data
.lock
);
2512 local_stream
= zmalloc((consumer_data
.stream_count
+ 2) *
2513 sizeof(struct lttng_consumer_stream
*));
2514 if (local_stream
== NULL
) {
2515 PERROR("local_stream malloc");
2516 pthread_mutex_unlock(&consumer_data
.lock
);
2519 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2520 data_ht
, &nb_inactive_fd
);
2522 ERR("Error in allocating pollfd or local_outfds");
2523 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2524 pthread_mutex_unlock(&consumer_data
.lock
);
2528 consumer_data
.need_update
= 0;
2530 pthread_mutex_unlock(&consumer_data
.lock
);
2532 /* No FDs and consumer_quit, consumer_cleanup the thread */
2533 if (nb_fd
== 0 && consumer_quit
== 1 && nb_inactive_fd
== 0) {
2534 err
= 0; /* All is OK */
2537 /* poll on the array of fds */
2539 DBG("polling on %d fd", nb_fd
+ 2);
2540 health_poll_entry();
2541 num_rdy
= poll(pollfd
, nb_fd
+ 2, -1);
2543 DBG("poll num_rdy : %d", num_rdy
);
2544 if (num_rdy
== -1) {
2546 * Restart interrupted system call.
2548 if (errno
== EINTR
) {
2551 PERROR("Poll error");
2552 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2554 } else if (num_rdy
== 0) {
2555 DBG("Polling thread timed out");
2560 * If the consumer_data_pipe triggered poll go directly to the
2561 * beginning of the loop to update the array. We want to prioritize
2562 * array update over low-priority reads.
2564 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2565 ssize_t pipe_readlen
;
2567 DBG("consumer_data_pipe wake up");
2568 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2569 &new_stream
, sizeof(new_stream
));
2570 if (pipe_readlen
< sizeof(new_stream
)) {
2571 PERROR("Consumer data pipe");
2572 /* Continue so we can at least handle the current stream(s). */
2577 * If the stream is NULL, just ignore it. It's also possible that
2578 * the sessiond poll thread changed the consumer_quit state and is
2579 * waking us up to test it.
2581 if (new_stream
== NULL
) {
2582 validate_endpoint_status_data_stream();
2586 /* Continue to update the local streams and handle prio ones */
2590 /* Handle wakeup pipe. */
2591 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2593 ssize_t pipe_readlen
;
2595 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2597 if (pipe_readlen
< 0) {
2598 PERROR("Consumer data wakeup pipe");
2600 /* We've been awakened to handle stream(s). */
2601 ctx
->has_wakeup
= 0;
2604 /* Take care of high priority channels first. */
2605 for (i
= 0; i
< nb_fd
; i
++) {
2606 health_code_update();
2608 if (local_stream
[i
] == NULL
) {
2611 if (pollfd
[i
].revents
& POLLPRI
) {
2612 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2614 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2615 /* it's ok to have an unavailable sub-buffer */
2616 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2617 /* Clean the stream and free it. */
2618 consumer_del_stream(local_stream
[i
], data_ht
);
2619 local_stream
[i
] = NULL
;
2620 } else if (len
> 0) {
2621 local_stream
[i
]->data_read
= 1;
2627 * If we read high prio channel in this loop, try again
2628 * for more high prio data.
2634 /* Take care of low priority channels. */
2635 for (i
= 0; i
< nb_fd
; i
++) {
2636 health_code_update();
2638 if (local_stream
[i
] == NULL
) {
2641 if ((pollfd
[i
].revents
& POLLIN
) ||
2642 local_stream
[i
]->hangup_flush_done
||
2643 local_stream
[i
]->has_data
) {
2644 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2645 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2646 /* it's ok to have an unavailable sub-buffer */
2647 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2648 /* Clean the stream and free it. */
2649 consumer_del_stream(local_stream
[i
], data_ht
);
2650 local_stream
[i
] = NULL
;
2651 } else if (len
> 0) {
2652 local_stream
[i
]->data_read
= 1;
2657 /* Handle hangup and errors */
2658 for (i
= 0; i
< nb_fd
; i
++) {
2659 health_code_update();
2661 if (local_stream
[i
] == NULL
) {
2664 if (!local_stream
[i
]->hangup_flush_done
2665 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2666 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2667 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2668 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2670 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2671 /* Attempt read again, for the data we just flushed. */
2672 local_stream
[i
]->data_read
= 1;
2675 * If the poll flag is HUP/ERR/NVAL and we have
2676 * read no data in this pass, we can remove the
2677 * stream from its hash table.
2679 if ((pollfd
[i
].revents
& POLLHUP
)) {
2680 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2681 if (!local_stream
[i
]->data_read
) {
2682 consumer_del_stream(local_stream
[i
], data_ht
);
2683 local_stream
[i
] = NULL
;
2686 } else if (pollfd
[i
].revents
& POLLERR
) {
2687 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2688 if (!local_stream
[i
]->data_read
) {
2689 consumer_del_stream(local_stream
[i
], data_ht
);
2690 local_stream
[i
] = NULL
;
2693 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2694 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2695 if (!local_stream
[i
]->data_read
) {
2696 consumer_del_stream(local_stream
[i
], data_ht
);
2697 local_stream
[i
] = NULL
;
2701 if (local_stream
[i
] != NULL
) {
2702 local_stream
[i
]->data_read
= 0;
2709 DBG("polling thread exiting");
2714 * Close the write side of the pipe so epoll_wait() in
2715 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2716 * read side of the pipe. If we close them both, epoll_wait strangely does
2717 * not return and could create a endless wait period if the pipe is the
2718 * only tracked fd in the poll set. The thread will take care of closing
2721 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2726 ERR("Health error occurred in %s", __func__
);
2728 health_unregister(health_consumerd
);
2730 rcu_unregister_thread();
2735 * Close wake-up end of each stream belonging to the channel. This will
2736 * allow the poll() on the stream read-side to detect when the
2737 * write-side (application) finally closes them.
2740 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2742 struct lttng_ht
*ht
;
2743 struct lttng_consumer_stream
*stream
;
2744 struct lttng_ht_iter iter
;
2746 ht
= consumer_data
.stream_per_chan_id_ht
;
2749 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2750 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2751 ht
->match_fct
, &channel
->key
,
2752 &iter
.iter
, stream
, node_channel_id
.node
) {
2754 * Protect against teardown with mutex.
2756 pthread_mutex_lock(&stream
->lock
);
2757 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2760 switch (consumer_data
.type
) {
2761 case LTTNG_CONSUMER_KERNEL
:
2763 case LTTNG_CONSUMER32_UST
:
2764 case LTTNG_CONSUMER64_UST
:
2765 if (stream
->metadata_flag
) {
2766 /* Safe and protected by the stream lock. */
2767 lttng_ustconsumer_close_metadata(stream
->chan
);
2770 * Note: a mutex is taken internally within
2771 * liblttng-ust-ctl to protect timer wakeup_fd
2772 * use from concurrent close.
2774 lttng_ustconsumer_close_stream_wakeup(stream
);
2778 ERR("Unknown consumer_data type");
2782 pthread_mutex_unlock(&stream
->lock
);
2787 static void destroy_channel_ht(struct lttng_ht
*ht
)
2789 struct lttng_ht_iter iter
;
2790 struct lttng_consumer_channel
*channel
;
2798 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2799 ret
= lttng_ht_del(ht
, &iter
);
2804 lttng_ht_destroy(ht
);
2808 * This thread polls the channel fds to detect when they are being
2809 * closed. It closes all related streams if the channel is detected as
2810 * closed. It is currently only used as a shim layer for UST because the
2811 * consumerd needs to keep the per-stream wakeup end of pipes open for
2814 void *consumer_thread_channel_poll(void *data
)
2816 int ret
, i
, pollfd
, err
= -1;
2817 uint32_t revents
, nb_fd
;
2818 struct lttng_consumer_channel
*chan
= NULL
;
2819 struct lttng_ht_iter iter
;
2820 struct lttng_ht_node_u64
*node
;
2821 struct lttng_poll_event events
;
2822 struct lttng_consumer_local_data
*ctx
= data
;
2823 struct lttng_ht
*channel_ht
;
2825 rcu_register_thread();
2827 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2829 if (testpoint(consumerd_thread_channel
)) {
2830 goto error_testpoint
;
2833 health_code_update();
2835 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2837 /* ENOMEM at this point. Better to bail out. */
2841 DBG("Thread channel poll started");
2843 /* Size is set to 1 for the consumer_channel pipe */
2844 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2846 ERR("Poll set creation failed");
2850 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2856 DBG("Channel main loop started");
2860 health_code_update();
2861 DBG("Channel poll wait");
2862 health_poll_entry();
2863 ret
= lttng_poll_wait(&events
, -1);
2864 DBG("Channel poll return from wait with %d fd(s)",
2865 LTTNG_POLL_GETNB(&events
));
2867 DBG("Channel event caught in thread");
2869 if (errno
== EINTR
) {
2870 ERR("Poll EINTR caught");
2873 if (LTTNG_POLL_GETNB(&events
) == 0) {
2874 err
= 0; /* All is OK */
2881 /* From here, the event is a channel wait fd */
2882 for (i
= 0; i
< nb_fd
; i
++) {
2883 health_code_update();
2885 revents
= LTTNG_POLL_GETEV(&events
, i
);
2886 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2889 /* No activity for this FD (poll implementation). */
2893 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2894 if (revents
& LPOLLIN
) {
2895 enum consumer_channel_action action
;
2898 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2901 ERR("Error reading channel pipe");
2903 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2908 case CONSUMER_CHANNEL_ADD
:
2909 DBG("Adding channel %d to poll set",
2912 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2915 lttng_ht_add_unique_u64(channel_ht
,
2916 &chan
->wait_fd_node
);
2918 /* Add channel to the global poll events list */
2919 lttng_poll_add(&events
, chan
->wait_fd
,
2920 LPOLLERR
| LPOLLHUP
);
2922 case CONSUMER_CHANNEL_DEL
:
2925 * This command should never be called if the channel
2926 * has streams monitored by either the data or metadata
2927 * thread. The consumer only notify this thread with a
2928 * channel del. command if it receives a destroy
2929 * channel command from the session daemon that send it
2930 * if a command prior to the GET_CHANNEL failed.
2934 chan
= consumer_find_channel(key
);
2937 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2940 lttng_poll_del(&events
, chan
->wait_fd
);
2941 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2942 ret
= lttng_ht_del(channel_ht
, &iter
);
2945 switch (consumer_data
.type
) {
2946 case LTTNG_CONSUMER_KERNEL
:
2948 case LTTNG_CONSUMER32_UST
:
2949 case LTTNG_CONSUMER64_UST
:
2950 health_code_update();
2951 /* Destroy streams that might have been left in the stream list. */
2952 clean_channel_stream_list(chan
);
2955 ERR("Unknown consumer_data type");
2960 * Release our own refcount. Force channel deletion even if
2961 * streams were not initialized.
2963 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2964 consumer_del_channel(chan
);
2969 case CONSUMER_CHANNEL_QUIT
:
2971 * Remove the pipe from the poll set and continue the loop
2972 * since their might be data to consume.
2974 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2977 ERR("Unknown action");
2980 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2981 DBG("Channel thread pipe hung up");
2983 * Remove the pipe from the poll set and continue the loop
2984 * since their might be data to consume.
2986 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2989 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2993 /* Handle other stream */
2999 uint64_t tmp_id
= (uint64_t) pollfd
;
3001 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3003 node
= lttng_ht_iter_get_node_u64(&iter
);
3006 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
3009 /* Check for error event */
3010 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3011 DBG("Channel fd %d is hup|err.", pollfd
);
3013 lttng_poll_del(&events
, chan
->wait_fd
);
3014 ret
= lttng_ht_del(channel_ht
, &iter
);
3018 * This will close the wait fd for each stream associated to
3019 * this channel AND monitored by the data/metadata thread thus
3020 * will be clean by the right thread.
3022 consumer_close_channel_streams(chan
);
3024 /* Release our own refcount */
3025 if (!uatomic_sub_return(&chan
->refcount
, 1)
3026 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3027 consumer_del_channel(chan
);
3030 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3035 /* Release RCU lock for the channel looked up */
3043 lttng_poll_clean(&events
);
3045 destroy_channel_ht(channel_ht
);
3048 DBG("Channel poll thread exiting");
3051 ERR("Health error occurred in %s", __func__
);
3053 health_unregister(health_consumerd
);
3054 rcu_unregister_thread();
3058 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3059 struct pollfd
*sockpoll
, int client_socket
)
3066 ret
= lttng_consumer_poll_socket(sockpoll
);
3070 DBG("Metadata connection on client_socket");
3072 /* Blocking call, waiting for transmission */
3073 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3074 if (ctx
->consumer_metadata_socket
< 0) {
3075 WARN("On accept metadata");
3086 * This thread listens on the consumerd socket and receives the file
3087 * descriptors from the session daemon.
3089 void *consumer_thread_sessiond_poll(void *data
)
3091 int sock
= -1, client_socket
, ret
, err
= -1;
3093 * structure to poll for incoming data on communication socket avoids
3094 * making blocking sockets.
3096 struct pollfd consumer_sockpoll
[2];
3097 struct lttng_consumer_local_data
*ctx
= data
;
3099 rcu_register_thread();
3101 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3103 if (testpoint(consumerd_thread_sessiond
)) {
3104 goto error_testpoint
;
3107 health_code_update();
3109 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3110 unlink(ctx
->consumer_command_sock_path
);
3111 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3112 if (client_socket
< 0) {
3113 ERR("Cannot create command socket");
3117 ret
= lttcomm_listen_unix_sock(client_socket
);
3122 DBG("Sending ready command to lttng-sessiond");
3123 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3124 /* return < 0 on error, but == 0 is not fatal */
3126 ERR("Error sending ready command to lttng-sessiond");
3130 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3131 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3132 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3133 consumer_sockpoll
[1].fd
= client_socket
;
3134 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3136 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3144 DBG("Connection on client_socket");
3146 /* Blocking call, waiting for transmission */
3147 sock
= lttcomm_accept_unix_sock(client_socket
);
3154 * Setup metadata socket which is the second socket connection on the
3155 * command unix socket.
3157 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3166 /* This socket is not useful anymore. */
3167 ret
= close(client_socket
);
3169 PERROR("close client_socket");
3173 /* update the polling structure to poll on the established socket */
3174 consumer_sockpoll
[1].fd
= sock
;
3175 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3178 health_code_update();
3180 health_poll_entry();
3181 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3190 DBG("Incoming command on sock");
3191 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3194 * This could simply be a session daemon quitting. Don't output
3197 DBG("Communication interrupted on command socket");
3201 if (consumer_quit
) {
3202 DBG("consumer_thread_receive_fds received quit from signal");
3203 err
= 0; /* All is OK */
3206 DBG("received command on sock");
3212 DBG("Consumer thread sessiond poll exiting");
3215 * Close metadata streams since the producer is the session daemon which
3218 * NOTE: for now, this only applies to the UST tracer.
3220 lttng_consumer_close_all_metadata();
3223 * when all fds have hung up, the polling thread
3229 * Notify the data poll thread to poll back again and test the
3230 * consumer_quit state that we just set so to quit gracefully.
3232 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3234 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3236 notify_health_quit_pipe(health_quit_pipe
);
3238 /* Cleaning up possibly open sockets. */
3242 PERROR("close sock sessiond poll");
3245 if (client_socket
>= 0) {
3246 ret
= close(client_socket
);
3248 PERROR("close client_socket sessiond poll");
3255 ERR("Health error occurred in %s", __func__
);
3257 health_unregister(health_consumerd
);
3259 rcu_unregister_thread();
3263 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3264 struct lttng_consumer_local_data
*ctx
)
3268 pthread_mutex_lock(&stream
->lock
);
3269 if (stream
->metadata_flag
) {
3270 pthread_mutex_lock(&stream
->metadata_rdv_lock
);
3273 switch (consumer_data
.type
) {
3274 case LTTNG_CONSUMER_KERNEL
:
3275 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3277 case LTTNG_CONSUMER32_UST
:
3278 case LTTNG_CONSUMER64_UST
:
3279 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3282 ERR("Unknown consumer_data type");
3288 if (stream
->metadata_flag
) {
3289 pthread_cond_broadcast(&stream
->metadata_rdv
);
3290 pthread_mutex_unlock(&stream
->metadata_rdv_lock
);
3292 pthread_mutex_unlock(&stream
->lock
);
3296 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3298 switch (consumer_data
.type
) {
3299 case LTTNG_CONSUMER_KERNEL
:
3300 return lttng_kconsumer_on_recv_stream(stream
);
3301 case LTTNG_CONSUMER32_UST
:
3302 case LTTNG_CONSUMER64_UST
:
3303 return lttng_ustconsumer_on_recv_stream(stream
);
3305 ERR("Unknown consumer_data type");
3312 * Allocate and set consumer data hash tables.
3314 int lttng_consumer_init(void)
3316 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3317 if (!consumer_data
.channel_ht
) {
3321 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3322 if (!consumer_data
.relayd_ht
) {
3326 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3327 if (!consumer_data
.stream_list_ht
) {
3331 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3332 if (!consumer_data
.stream_per_chan_id_ht
) {
3336 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3341 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3353 * Process the ADD_RELAYD command receive by a consumer.
3355 * This will create a relayd socket pair and add it to the relayd hash table.
3356 * The caller MUST acquire a RCU read side lock before calling it.
3358 int consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3359 struct lttng_consumer_local_data
*ctx
, int sock
,
3360 struct pollfd
*consumer_sockpoll
,
3361 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3362 uint64_t relayd_session_id
)
3364 int fd
= -1, ret
= -1, relayd_created
= 0;
3365 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3366 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3369 assert(relayd_sock
);
3371 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3373 /* Get relayd reference if exists. */
3374 relayd
= consumer_find_relayd(net_seq_idx
);
3375 if (relayd
== NULL
) {
3376 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3377 /* Not found. Allocate one. */
3378 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3379 if (relayd
== NULL
) {
3381 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3384 relayd
->sessiond_session_id
= sessiond_id
;
3389 * This code path MUST continue to the consumer send status message to
3390 * we can notify the session daemon and continue our work without
3391 * killing everything.
3395 * relayd key should never be found for control socket.
3397 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3400 /* First send a status message before receiving the fds. */
3401 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3403 /* Somehow, the session daemon is not responding anymore. */
3404 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3405 goto error_nosignal
;
3408 /* Poll on consumer socket. */
3409 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3411 /* Needing to exit in the middle of a command: error. */
3412 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3414 goto error_nosignal
;
3417 /* Get relayd socket from session daemon */
3418 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3419 if (ret
!= sizeof(fd
)) {
3421 fd
= -1; /* Just in case it gets set with an invalid value. */
3424 * Failing to receive FDs might indicate a major problem such as
3425 * reaching a fd limit during the receive where the kernel returns a
3426 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3427 * don't take any chances and stop everything.
3429 * XXX: Feature request #558 will fix that and avoid this possible
3430 * issue when reaching the fd limit.
3432 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3433 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3437 /* Copy socket information and received FD */
3438 switch (sock_type
) {
3439 case LTTNG_STREAM_CONTROL
:
3440 /* Copy received lttcomm socket */
3441 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3442 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3443 /* Handle create_sock error. */
3445 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3449 * Close the socket created internally by
3450 * lttcomm_create_sock, so we can replace it by the one
3451 * received from sessiond.
3453 if (close(relayd
->control_sock
.sock
.fd
)) {
3457 /* Assign new file descriptor */
3458 relayd
->control_sock
.sock
.fd
= fd
;
3459 fd
= -1; /* For error path */
3460 /* Assign version values. */
3461 relayd
->control_sock
.major
= relayd_sock
->major
;
3462 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3464 relayd
->relayd_session_id
= relayd_session_id
;
3467 case LTTNG_STREAM_DATA
:
3468 /* Copy received lttcomm socket */
3469 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3470 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3471 /* Handle create_sock error. */
3473 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3477 * Close the socket created internally by
3478 * lttcomm_create_sock, so we can replace it by the one
3479 * received from sessiond.
3481 if (close(relayd
->data_sock
.sock
.fd
)) {
3485 /* Assign new file descriptor */
3486 relayd
->data_sock
.sock
.fd
= fd
;
3487 fd
= -1; /* for eventual error paths */
3488 /* Assign version values. */
3489 relayd
->data_sock
.major
= relayd_sock
->major
;
3490 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3493 ERR("Unknown relayd socket type (%d)", sock_type
);
3495 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3499 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3500 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3501 relayd
->net_seq_idx
, fd
);
3503 /* We successfully added the socket. Send status back. */
3504 ret
= consumer_send_status_msg(sock
, ret_code
);
3506 /* Somehow, the session daemon is not responding anymore. */
3507 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3508 goto error_nosignal
;
3512 * Add relayd socket pair to consumer data hashtable. If object already
3513 * exists or on error, the function gracefully returns.
3522 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3523 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3527 /* Close received socket if valid. */
3530 PERROR("close received socket");
3534 if (relayd_created
) {
3542 * Try to lock the stream mutex.
3544 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3546 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3553 * Try to lock the stream mutex. On failure, we know that the stream is
3554 * being used else where hence there is data still being extracted.
3556 ret
= pthread_mutex_trylock(&stream
->lock
);
3558 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3570 * Search for a relayd associated to the session id and return the reference.
3572 * A rcu read side lock MUST be acquire before calling this function and locked
3573 * until the relayd object is no longer necessary.
3575 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3577 struct lttng_ht_iter iter
;
3578 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3580 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3581 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3584 * Check by sessiond id which is unique here where the relayd session
3585 * id might not be when having multiple relayd.
3587 if (relayd
->sessiond_session_id
== id
) {
3588 /* Found the relayd. There can be only one per id. */
3600 * Check if for a given session id there is still data needed to be extract
3603 * Return 1 if data is pending or else 0 meaning ready to be read.
3605 int consumer_data_pending(uint64_t id
)
3608 struct lttng_ht_iter iter
;
3609 struct lttng_ht
*ht
;
3610 struct lttng_consumer_stream
*stream
;
3611 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3612 int (*data_pending
)(struct lttng_consumer_stream
*);
3614 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3617 pthread_mutex_lock(&consumer_data
.lock
);
3619 switch (consumer_data
.type
) {
3620 case LTTNG_CONSUMER_KERNEL
:
3621 data_pending
= lttng_kconsumer_data_pending
;
3623 case LTTNG_CONSUMER32_UST
:
3624 case LTTNG_CONSUMER64_UST
:
3625 data_pending
= lttng_ustconsumer_data_pending
;
3628 ERR("Unknown consumer data type");
3632 /* Ease our life a bit */
3633 ht
= consumer_data
.stream_list_ht
;
3635 relayd
= find_relayd_by_session_id(id
);
3637 /* Send init command for data pending. */
3638 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3639 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3640 relayd
->relayd_session_id
);
3641 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3643 /* Communication error thus the relayd so no data pending. */
3644 ERR("Relayd begin data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3645 lttng_consumer_cleanup_relayd(relayd
);
3646 goto data_not_pending
;
3650 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3651 ht
->hash_fct(&id
, lttng_ht_seed
),
3653 &iter
.iter
, stream
, node_session_id
.node
) {
3654 /* If this call fails, the stream is being used hence data pending. */
3655 ret
= stream_try_lock(stream
);
3661 * A removed node from the hash table indicates that the stream has
3662 * been deleted thus having a guarantee that the buffers are closed
3663 * on the consumer side. However, data can still be transmitted
3664 * over the network so don't skip the relayd check.
3666 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3668 /* Check the stream if there is data in the buffers. */
3669 ret
= data_pending(stream
);
3671 pthread_mutex_unlock(&stream
->lock
);
3678 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3679 if (stream
->metadata_flag
) {
3680 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3681 stream
->relayd_stream_id
);
3683 ret
= relayd_data_pending(&relayd
->control_sock
,
3684 stream
->relayd_stream_id
,
3685 stream
->next_net_seq_num
- 1);
3688 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3689 lttng_consumer_cleanup_relayd(relayd
);
3690 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3691 pthread_mutex_unlock(&stream
->lock
);
3692 goto data_not_pending
;
3694 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3696 pthread_mutex_unlock(&stream
->lock
);
3700 pthread_mutex_unlock(&stream
->lock
);
3704 unsigned int is_data_inflight
= 0;
3706 /* Send init command for data pending. */
3707 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3708 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3709 relayd
->relayd_session_id
, &is_data_inflight
);
3710 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3712 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->net_seq_idx
);
3713 lttng_consumer_cleanup_relayd(relayd
);
3714 goto data_not_pending
;
3716 if (is_data_inflight
) {
3722 * Finding _no_ node in the hash table and no inflight data means that the
3723 * stream(s) have been removed thus data is guaranteed to be available for
3724 * analysis from the trace files.
3728 /* Data is available to be read by a viewer. */
3729 pthread_mutex_unlock(&consumer_data
.lock
);
3734 /* Data is still being extracted from buffers. */
3735 pthread_mutex_unlock(&consumer_data
.lock
);
3741 * Send a ret code status message to the sessiond daemon.
3743 * Return the sendmsg() return value.
3745 int consumer_send_status_msg(int sock
, int ret_code
)
3747 struct lttcomm_consumer_status_msg msg
;
3749 memset(&msg
, 0, sizeof(msg
));
3750 msg
.ret_code
= ret_code
;
3752 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3756 * Send a channel status message to the sessiond daemon.
3758 * Return the sendmsg() return value.
3760 int consumer_send_status_channel(int sock
,
3761 struct lttng_consumer_channel
*channel
)
3763 struct lttcomm_consumer_status_channel msg
;
3767 memset(&msg
, 0, sizeof(msg
));
3769 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3771 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3772 msg
.key
= channel
->key
;
3773 msg
.stream_count
= channel
->streams
.count
;
3776 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3779 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3780 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3781 uint64_t max_sb_size
)
3783 unsigned long start_pos
;
3785 if (!nb_packets_per_stream
) {
3786 return consumed_pos
; /* Grab everything */
3788 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
3789 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3790 if ((long) (start_pos
- consumed_pos
) < 0) {
3791 return consumed_pos
; /* Grab everything */