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 relayd id.
422 * It's atomically set without having the stream mutex locked which is fine
423 * because we handle the write/read race with a pipe wakeup for each thread.
425 static void update_endpoint_status_by_netidx(uint64_t relayd_id
,
426 enum consumer_endpoint_status status
)
428 struct lttng_ht_iter iter
;
429 struct lttng_consumer_stream
*stream
;
431 DBG("Consumer set delete flag on stream by idx %" PRIu64
, relayd_id
);
435 /* Let's begin with metadata */
436 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
437 if (stream
->relayd_id
== relayd_id
) {
438 uatomic_set(&stream
->endpoint_status
, status
);
439 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
443 /* Follow up by the data streams */
444 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
445 if (stream
->relayd_id
== relayd_id
) {
446 uatomic_set(&stream
->endpoint_status
, status
);
447 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
454 * Cleanup a relayd object by flagging every associated streams for deletion,
455 * destroying the object meaning removing it from the relayd hash table,
456 * closing the sockets and freeing the memory in a RCU call.
458 * If a local data context is available, notify the threads that the streams'
459 * state have changed.
461 void lttng_consumer_cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
)
467 DBG("Cleaning up relayd object ID %"PRIu64
, relayd
->id
);
469 /* Save the net sequence index before destroying the object */
473 * Delete the relayd from the relayd hash table, close the sockets and free
474 * the object in a RCU call.
476 consumer_destroy_relayd(relayd
);
478 /* Set inactive endpoint to all streams */
479 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
482 * With a local data context, notify the threads that the streams' state
483 * have changed. The write() action on the pipe acts as an "implicit"
484 * memory barrier ordering the updates of the end point status from the
485 * read of this status which happens AFTER receiving this notify.
487 notify_thread_lttng_pipe(relayd
->ctx
->consumer_data_pipe
);
488 notify_thread_lttng_pipe(relayd
->ctx
->consumer_metadata_pipe
);
492 * Flag a relayd socket pair for destruction. Destroy it if the refcount
495 * RCU read side lock MUST be aquired before calling this function.
497 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
501 /* Set destroy flag for this object */
502 uatomic_set(&relayd
->destroy_flag
, 1);
504 /* Destroy the relayd if refcount is 0 */
505 if (uatomic_read(&relayd
->refcount
) == 0) {
506 consumer_destroy_relayd(relayd
);
511 * Completly destroy stream from every visiable data structure and the given
514 * One this call returns, the stream object is not longer usable nor visible.
516 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
519 consumer_stream_destroy(stream
, ht
);
523 * XXX naming of del vs destroy is all mixed up.
525 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
527 consumer_stream_destroy(stream
, data_ht
);
530 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
532 consumer_stream_destroy(stream
, metadata_ht
);
535 struct lttng_consumer_stream
*consumer_allocate_stream(
536 struct lttng_consumer_channel
*channel
,
537 uint64_t channel_key
,
539 enum lttng_consumer_stream_state state
,
540 const char *channel_name
,
547 enum consumer_channel_type type
,
548 unsigned int monitor
)
551 struct lttng_consumer_stream
*stream
;
553 stream
= zmalloc(sizeof(*stream
));
554 if (stream
== NULL
) {
555 PERROR("malloc struct lttng_consumer_stream");
561 stream
->chan
= channel
;
562 stream
->key
= stream_key
;
564 stream
->out_fd_offset
= 0;
565 stream
->output_written
= 0;
566 stream
->state
= state
;
569 stream
->relayd_id
= relayd_id
;
570 stream
->session_id
= session_id
;
571 stream
->monitor
= monitor
;
572 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
573 stream
->index_file
= NULL
;
574 stream
->last_sequence_number
= -1ULL;
575 pthread_mutex_init(&stream
->lock
, NULL
);
576 pthread_mutex_init(&stream
->metadata_timer_lock
, NULL
);
578 /* If channel is the metadata, flag this stream as metadata. */
579 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
580 stream
->metadata_flag
= 1;
581 /* Metadata is flat out. */
582 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
583 /* Live rendez-vous point. */
584 pthread_cond_init(&stream
->metadata_rdv
, NULL
);
585 pthread_mutex_init(&stream
->metadata_rdv_lock
, NULL
);
587 /* Format stream name to <channel_name>_<cpu_number> */
588 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
591 PERROR("snprintf stream name");
596 /* Key is always the wait_fd for streams. */
597 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
599 /* Init node per channel id key */
600 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
602 /* Init session id node with the stream session id */
603 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
605 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
606 " relayd_id %" PRIu64
", session_id %" PRIu64
,
607 stream
->name
, stream
->key
, channel_key
,
608 stream
->relayd_id
, stream
->session_id
);
624 * Add a stream to the global list protected by a mutex.
626 int consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
628 struct lttng_ht
*ht
= data_ht
;
634 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
636 pthread_mutex_lock(&consumer_data
.lock
);
637 pthread_mutex_lock(&stream
->chan
->lock
);
638 pthread_mutex_lock(&stream
->chan
->timer_lock
);
639 pthread_mutex_lock(&stream
->lock
);
642 /* Steal stream identifier to avoid having streams with the same key */
643 steal_stream_key(stream
->key
, ht
);
645 lttng_ht_add_unique_u64(ht
, &stream
->node
);
647 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
648 &stream
->node_channel_id
);
651 * Add stream to the stream_list_ht of the consumer data. No need to steal
652 * the key since the HT does not use it and we allow to add redundant keys
655 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
658 * When nb_init_stream_left reaches 0, we don't need to trigger any action
659 * in terms of destroying the associated channel, because the action that
660 * causes the count to become 0 also causes a stream to be added. The
661 * channel deletion will thus be triggered by the following removal of this
664 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
665 /* Increment refcount before decrementing nb_init_stream_left */
667 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
670 /* Update consumer data once the node is inserted. */
671 consumer_data
.stream_count
++;
672 consumer_data
.need_update
= 1;
675 pthread_mutex_unlock(&stream
->lock
);
676 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
677 pthread_mutex_unlock(&stream
->chan
->lock
);
678 pthread_mutex_unlock(&consumer_data
.lock
);
683 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
685 consumer_del_stream(stream
, data_ht
);
689 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
690 * be acquired before calling this.
692 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
695 struct lttng_ht_node_u64
*node
;
696 struct lttng_ht_iter iter
;
700 lttng_ht_lookup(consumer_data
.relayd_ht
,
702 node
= lttng_ht_iter_get_node_u64(&iter
);
706 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
713 * Allocate and return a consumer relayd socket.
715 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
718 struct consumer_relayd_sock_pair
*obj
= NULL
;
720 /* net sequence index of -1 is a failure */
721 if (relayd_id
== (uint64_t) -1ULL) {
725 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
727 PERROR("zmalloc relayd sock");
733 obj
->destroy_flag
= 0;
734 obj
->control_sock
.sock
.fd
= -1;
735 obj
->data_sock
.sock
.fd
= -1;
736 lttng_ht_node_init_u64(&obj
->node
, obj
->id
);
737 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
744 * Find a relayd socket pair in the global consumer data.
746 * Return the object if found else NULL.
747 * RCU read-side lock must be held across this call and while using the
750 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
752 struct lttng_ht_iter iter
;
753 struct lttng_ht_node_u64
*node
;
754 struct consumer_relayd_sock_pair
*relayd
= NULL
;
756 /* Negative keys are lookup failures */
757 if (key
== (uint64_t) -1ULL) {
761 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
763 node
= lttng_ht_iter_get_node_u64(&iter
);
765 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
773 * Find a relayd and send the stream
775 * Returns 0 on success, < 0 on error
777 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
781 struct consumer_relayd_sock_pair
*relayd
;
784 assert(stream
->relayd_id
!= -1ULL);
787 /* The stream is not metadata. Get relayd reference if exists. */
789 relayd
= consumer_find_relayd(stream
->relayd_id
);
790 if (relayd
!= NULL
) {
791 /* Add stream on the relayd */
792 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
793 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
794 path
, &stream
->relayd_stream_id
,
795 stream
->chan
->tracefile_size
, stream
->chan
->tracefile_count
);
796 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
798 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".", relayd
->id
);
799 lttng_consumer_cleanup_relayd(relayd
);
803 uatomic_inc(&relayd
->refcount
);
804 stream
->sent_to_relayd
= 1;
806 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
807 stream
->key
, stream
->relayd_id
);
812 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
813 stream
->name
, stream
->key
, stream
->relayd_id
);
821 * Find a relayd and send the streams sent message
823 * Returns 0 on success, < 0 on error
825 int consumer_send_relayd_streams_sent(uint64_t relayd_id
)
828 struct consumer_relayd_sock_pair
*relayd
;
830 assert(relayd_id
!= -1ULL);
832 /* The stream is not metadata. Get relayd reference if exists. */
834 relayd
= consumer_find_relayd(relayd_id
);
835 if (relayd
!= NULL
) {
836 /* Add stream on the relayd */
837 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
838 ret
= relayd_streams_sent(&relayd
->control_sock
);
839 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
841 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64
".", relayd
->id
);
842 lttng_consumer_cleanup_relayd(relayd
);
846 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
853 DBG("All streams sent relayd id %" PRIu64
, relayd_id
);
861 * Find a relayd and close the stream
863 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
865 struct consumer_relayd_sock_pair
*relayd
;
867 /* The stream is not metadata. Get relayd reference if exists. */
869 relayd
= consumer_find_relayd(stream
->relayd_id
);
871 consumer_stream_relayd_close(stream
, relayd
);
877 * Handle stream for relayd transmission if the stream applies for network
878 * streaming where the net sequence index is set.
880 * Return destination file descriptor or negative value on error.
882 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
883 size_t data_size
, unsigned long padding
,
884 struct consumer_relayd_sock_pair
*relayd
)
887 struct lttcomm_relayd_data_hdr data_hdr
;
893 /* Reset data header */
894 memset(&data_hdr
, 0, sizeof(data_hdr
));
896 if (stream
->metadata_flag
) {
897 /* Caller MUST acquire the relayd control socket lock */
898 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
903 /* Metadata are always sent on the control socket. */
904 outfd
= relayd
->control_sock
.sock
.fd
;
906 /* Set header with stream information */
907 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
908 data_hdr
.data_size
= htobe32(data_size
);
909 data_hdr
.padding_size
= htobe32(padding
);
911 * Note that net_seq_num below is assigned with the *current* value of
912 * next_net_seq_num and only after that the next_net_seq_num will be
913 * increment. This is why when issuing a command on the relayd using
914 * this next value, 1 should always be substracted in order to compare
915 * the last seen sequence number on the relayd side to the last sent.
917 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
918 /* Other fields are zeroed previously */
920 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
926 ++stream
->next_net_seq_num
;
928 /* Set to go on data socket */
929 outfd
= relayd
->data_sock
.sock
.fd
;
937 * Allocate and return a new lttng_consumer_channel object using the given key
938 * to initialize the hash table node.
940 * On error, return NULL.
942 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
944 const char *pathname
,
949 enum lttng_event_output output
,
950 uint64_t tracefile_size
,
951 uint64_t tracefile_count
,
952 uint64_t session_id_per_pid
,
953 unsigned int monitor
,
954 unsigned int live_timer_interval
,
955 bool is_in_live_session
,
956 const char *root_shm_path
,
957 const char *shm_path
)
959 struct lttng_consumer_channel
*channel
;
961 channel
= zmalloc(sizeof(*channel
));
962 if (channel
== NULL
) {
963 PERROR("malloc struct lttng_consumer_channel");
968 channel
->refcount
= 0;
969 channel
->session_id
= session_id
;
970 channel
->session_id_per_pid
= session_id_per_pid
;
973 channel
->relayd_id
= relayd_id
;
974 channel
->tracefile_size
= tracefile_size
;
975 channel
->tracefile_count
= tracefile_count
;
976 channel
->monitor
= monitor
;
977 channel
->live_timer_interval
= live_timer_interval
;
978 channel
->is_live
= is_in_live_session
;
979 pthread_mutex_init(&channel
->lock
, NULL
);
980 pthread_mutex_init(&channel
->timer_lock
, NULL
);
983 case LTTNG_EVENT_SPLICE
:
984 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
986 case LTTNG_EVENT_MMAP
:
987 channel
->output
= CONSUMER_CHANNEL_MMAP
;
997 * In monitor mode, the streams associated with the channel will be put in
998 * a special list ONLY owned by this channel. So, the refcount is set to 1
999 * here meaning that the channel itself has streams that are referenced.
1001 * On a channel deletion, once the channel is no longer visible, the
1002 * refcount is decremented and checked for a zero value to delete it. With
1003 * streams in no monitor mode, it will now be safe to destroy the channel.
1005 if (!channel
->monitor
) {
1006 channel
->refcount
= 1;
1009 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1010 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1012 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1013 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1015 if (root_shm_path
) {
1016 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1017 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1020 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1021 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1024 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1026 channel
->wait_fd
= -1;
1028 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1030 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1037 * Add a channel to the global list protected by a mutex.
1039 * Always return 0 indicating success.
1041 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1042 struct lttng_consumer_local_data
*ctx
)
1044 pthread_mutex_lock(&consumer_data
.lock
);
1045 pthread_mutex_lock(&channel
->lock
);
1046 pthread_mutex_lock(&channel
->timer_lock
);
1049 * This gives us a guarantee that the channel we are about to add to the
1050 * channel hash table will be unique. See this function comment on the why
1051 * we need to steel the channel key at this stage.
1053 steal_channel_key(channel
->key
);
1056 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1059 pthread_mutex_unlock(&channel
->timer_lock
);
1060 pthread_mutex_unlock(&channel
->lock
);
1061 pthread_mutex_unlock(&consumer_data
.lock
);
1063 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1064 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1071 * Allocate the pollfd structure and the local view of the out fds to avoid
1072 * doing a lookup in the linked list and concurrency issues when writing is
1073 * needed. Called with consumer_data.lock held.
1075 * Returns the number of fds in the structures.
1077 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1078 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1079 struct lttng_ht
*ht
, int *nb_inactive_fd
)
1082 struct lttng_ht_iter iter
;
1083 struct lttng_consumer_stream
*stream
;
1088 assert(local_stream
);
1090 DBG("Updating poll fd array");
1091 *nb_inactive_fd
= 0;
1093 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1095 * Only active streams with an active end point can be added to the
1096 * poll set and local stream storage of the thread.
1098 * There is a potential race here for endpoint_status to be updated
1099 * just after the check. However, this is OK since the stream(s) will
1100 * be deleted once the thread is notified that the end point state has
1101 * changed where this function will be called back again.
1103 * We track the number of inactive FDs because they still need to be
1104 * closed by the polling thread after a wakeup on the data_pipe or
1107 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
1108 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1109 (*nb_inactive_fd
)++;
1113 * This clobbers way too much the debug output. Uncomment that if you
1114 * need it for debugging purposes.
1116 * DBG("Active FD %d", stream->wait_fd);
1118 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1119 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1120 local_stream
[i
] = stream
;
1126 * Insert the consumer_data_pipe at the end of the array and don't
1127 * increment i so nb_fd is the number of real FD.
1129 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1130 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1132 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1133 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1138 * Poll on the should_quit pipe and the command socket return -1 on
1139 * error, 1 if should exit, 0 if data is available on the command socket
1141 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1146 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1147 if (num_rdy
== -1) {
1149 * Restart interrupted system call.
1151 if (errno
== EINTR
) {
1154 PERROR("Poll error");
1157 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1158 DBG("consumer_should_quit wake up");
1165 * Set the error socket.
1167 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1170 ctx
->consumer_error_socket
= sock
;
1174 * Set the command socket path.
1176 void lttng_consumer_set_command_sock_path(
1177 struct lttng_consumer_local_data
*ctx
, char *sock
)
1179 ctx
->consumer_command_sock_path
= sock
;
1183 * Send return code to the session daemon.
1184 * If the socket is not defined, we return 0, it is not a fatal error
1186 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1188 if (ctx
->consumer_error_socket
> 0) {
1189 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1190 sizeof(enum lttcomm_sessiond_command
));
1197 * Close all the tracefiles and stream fds and MUST be called when all
1198 * instances are destroyed i.e. when all threads were joined and are ended.
1200 void lttng_consumer_cleanup(void)
1202 struct lttng_ht_iter iter
;
1203 struct lttng_consumer_channel
*channel
;
1207 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1209 consumer_del_channel(channel
);
1214 lttng_ht_destroy(consumer_data
.channel_ht
);
1216 cleanup_relayd_ht();
1218 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1221 * This HT contains streams that are freed by either the metadata thread or
1222 * the data thread so we do *nothing* on the hash table and simply destroy
1225 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1229 * Called from signal handler.
1231 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1236 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1238 PERROR("write consumer quit");
1241 DBG("Consumer flag that it should quit");
1246 * Flush pending writes to trace output disk file.
1249 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1253 int outfd
= stream
->out_fd
;
1256 * This does a blocking write-and-wait on any page that belongs to the
1257 * subbuffer prior to the one we just wrote.
1258 * Don't care about error values, as these are just hints and ways to
1259 * limit the amount of page cache used.
1261 if (orig_offset
< stream
->max_sb_size
) {
1264 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1265 stream
->max_sb_size
,
1266 SYNC_FILE_RANGE_WAIT_BEFORE
1267 | SYNC_FILE_RANGE_WRITE
1268 | SYNC_FILE_RANGE_WAIT_AFTER
);
1270 * Give hints to the kernel about how we access the file:
1271 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1274 * We need to call fadvise again after the file grows because the
1275 * kernel does not seem to apply fadvise to non-existing parts of the
1278 * Call fadvise _after_ having waited for the page writeback to
1279 * complete because the dirty page writeback semantic is not well
1280 * defined. So it can be expected to lead to lower throughput in
1283 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1284 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1285 if (ret
&& ret
!= -ENOSYS
) {
1287 PERROR("posix_fadvise on fd %i", outfd
);
1292 * Initialise the necessary environnement :
1293 * - create a new context
1294 * - create the poll_pipe
1295 * - create the should_quit pipe (for signal handler)
1296 * - create the thread pipe (for splice)
1298 * Takes a function pointer as argument, this function is called when data is
1299 * available on a buffer. This function is responsible to do the
1300 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1301 * buffer configuration and then kernctl_put_next_subbuf at the end.
1303 * Returns a pointer to the new context or NULL on error.
1305 struct lttng_consumer_local_data
*lttng_consumer_create(
1306 enum lttng_consumer_type type
,
1307 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1308 struct lttng_consumer_local_data
*ctx
),
1309 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1310 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1311 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1314 struct lttng_consumer_local_data
*ctx
;
1316 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1317 consumer_data
.type
== type
);
1318 consumer_data
.type
= type
;
1320 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1322 PERROR("allocating context");
1326 ctx
->consumer_error_socket
= -1;
1327 ctx
->consumer_metadata_socket
= -1;
1328 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1329 /* assign the callbacks */
1330 ctx
->on_buffer_ready
= buffer_ready
;
1331 ctx
->on_recv_channel
= recv_channel
;
1332 ctx
->on_recv_stream
= recv_stream
;
1333 ctx
->on_update_stream
= update_stream
;
1335 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1336 if (!ctx
->consumer_data_pipe
) {
1337 goto error_poll_pipe
;
1340 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1341 if (!ctx
->consumer_wakeup_pipe
) {
1342 goto error_wakeup_pipe
;
1345 ret
= pipe(ctx
->consumer_should_quit
);
1347 PERROR("Error creating recv pipe");
1348 goto error_quit_pipe
;
1351 ret
= pipe(ctx
->consumer_channel_pipe
);
1353 PERROR("Error creating channel pipe");
1354 goto error_channel_pipe
;
1357 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1358 if (!ctx
->consumer_metadata_pipe
) {
1359 goto error_metadata_pipe
;
1364 error_metadata_pipe
:
1365 utils_close_pipe(ctx
->consumer_channel_pipe
);
1367 utils_close_pipe(ctx
->consumer_should_quit
);
1369 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1371 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1379 * Iterate over all streams of the hashtable and free them properly.
1381 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1383 struct lttng_ht_iter iter
;
1384 struct lttng_consumer_stream
*stream
;
1391 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1393 * Ignore return value since we are currently cleaning up so any error
1396 (void) consumer_del_stream(stream
, ht
);
1400 lttng_ht_destroy(ht
);
1404 * Iterate over all streams of the metadata hashtable and free them
1407 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1409 struct lttng_ht_iter iter
;
1410 struct lttng_consumer_stream
*stream
;
1417 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1419 * Ignore return value since we are currently cleaning up so any error
1422 (void) consumer_del_metadata_stream(stream
, ht
);
1426 lttng_ht_destroy(ht
);
1430 * Close all fds associated with the instance and free the context.
1432 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1436 DBG("Consumer destroying it. Closing everything.");
1442 destroy_data_stream_ht(data_ht
);
1443 destroy_metadata_stream_ht(metadata_ht
);
1445 ret
= close(ctx
->consumer_error_socket
);
1449 ret
= close(ctx
->consumer_metadata_socket
);
1453 utils_close_pipe(ctx
->consumer_channel_pipe
);
1454 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1455 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1456 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1457 utils_close_pipe(ctx
->consumer_should_quit
);
1459 unlink(ctx
->consumer_command_sock_path
);
1464 * Write the metadata stream id on the specified file descriptor.
1466 static int write_relayd_metadata_id(int fd
,
1467 struct lttng_consumer_stream
*stream
,
1468 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1471 struct lttcomm_relayd_metadata_payload hdr
;
1473 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1474 hdr
.padding_size
= htobe32(padding
);
1475 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1476 if (ret
< sizeof(hdr
)) {
1478 * This error means that the fd's end is closed so ignore the PERROR
1479 * not to clubber the error output since this can happen in a normal
1482 if (errno
!= EPIPE
) {
1483 PERROR("write metadata stream id");
1485 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1487 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1488 * handle writting the missing part so report that as an error and
1489 * don't lie to the caller.
1494 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1495 stream
->relayd_stream_id
, padding
);
1502 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1503 * core function for writing trace buffers to either the local filesystem or
1506 * It must be called with the stream lock held.
1508 * Careful review MUST be put if any changes occur!
1510 * Returns the number of bytes written
1512 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1513 struct lttng_consumer_local_data
*ctx
,
1514 struct lttng_consumer_stream
*stream
,
1515 const struct lttng_buffer_view
*buffer
,
1516 unsigned long padding
,
1517 struct ctf_packet_index
*index
)
1520 off_t orig_offset
= stream
->out_fd_offset
;
1521 /* Default is on the disk */
1522 int outfd
= stream
->out_fd
;
1523 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1524 unsigned int relayd_hang_up
= 0;
1525 const size_t subbuf_content_size
= buffer
->size
- padding
;
1528 /* RCU lock for the relayd pointer */
1531 /* Flag that the current stream if set for network streaming. */
1532 if (stream
->relayd_id
!= (uint64_t) -1ULL) {
1533 relayd
= consumer_find_relayd(stream
->relayd_id
);
1534 if (relayd
== NULL
) {
1540 /* Handle stream on the relayd if the output is on the network */
1542 unsigned long netlen
= subbuf_content_size
;
1545 * Lock the control socket for the complete duration of the function
1546 * since from this point on we will use the socket.
1548 if (stream
->metadata_flag
) {
1549 /* Metadata requires the control socket. */
1550 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1551 if (stream
->reset_metadata_flag
) {
1552 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1553 stream
->relayd_stream_id
,
1554 stream
->metadata_version
);
1559 stream
->reset_metadata_flag
= 0;
1561 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1564 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1569 /* Use the returned socket. */
1572 /* Write metadata stream id before payload */
1573 if (stream
->metadata_flag
) {
1574 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1581 write_len
= subbuf_content_size
;
1583 /* No streaming; we have to write the full padding. */
1584 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1585 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1587 ERR("Reset metadata file");
1590 stream
->reset_metadata_flag
= 0;
1594 * Check if we need to change the tracefile before writing the packet.
1596 if (stream
->chan
->tracefile_size
> 0 &&
1597 (stream
->tracefile_size_current
+ buffer
->size
) >
1598 stream
->chan
->tracefile_size
) {
1599 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1600 stream
->name
, stream
->chan
->tracefile_size
,
1601 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1602 stream
->out_fd
, &(stream
->tracefile_count_current
),
1605 ERR("Rotating output file");
1608 outfd
= stream
->out_fd
;
1610 if (stream
->index_file
) {
1611 lttng_index_file_put(stream
->index_file
);
1612 stream
->index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1613 stream
->name
, stream
->uid
, stream
->gid
,
1614 stream
->chan
->tracefile_size
,
1615 stream
->tracefile_count_current
,
1616 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1617 if (!stream
->index_file
) {
1622 /* Reset current size because we just perform a rotation. */
1623 stream
->tracefile_size_current
= 0;
1624 stream
->out_fd_offset
= 0;
1627 stream
->tracefile_size_current
+= buffer
->size
;
1629 index
->offset
= htobe64(stream
->out_fd_offset
);
1632 write_len
= buffer
->size
;
1636 * This call guarantee that len or less is returned. It's impossible to
1637 * receive a ret value that is bigger than len.
1639 ret
= lttng_write(outfd
, buffer
->data
, write_len
);
1640 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, write_len
);
1641 if (ret
< 0 || ((size_t) ret
!= write_len
)) {
1643 * Report error to caller if nothing was written else at least send the
1651 /* Socket operation failed. We consider the relayd dead */
1652 if (errno
== EPIPE
|| errno
== EINVAL
|| errno
== EBADF
) {
1654 * This is possible if the fd is closed on the other side
1655 * (outfd) or any write problem. It can be verbose a bit for a
1656 * normal execution if for instance the relayd is stopped
1657 * abruptly. This can happen so set this to a DBG statement.
1659 DBG("Consumer mmap write detected relayd hang up");
1661 /* Unhandled error, print it and stop function right now. */
1662 PERROR("Error in write mmap (ret %zd != write_len %zu)", ret
,
1667 stream
->output_written
+= ret
;
1669 /* This call is useless on a socket so better save a syscall. */
1671 /* This won't block, but will start writeout asynchronously */
1672 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, write_len
,
1673 SYNC_FILE_RANGE_WRITE
);
1674 stream
->out_fd_offset
+= write_len
;
1675 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1680 * This is a special case that the relayd has closed its socket. Let's
1681 * cleanup the relayd object and all associated streams.
1683 if (relayd
&& relayd_hang_up
) {
1684 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->id
);
1685 lttng_consumer_cleanup_relayd(relayd
);
1689 /* Unlock only if ctrl socket used */
1690 if (relayd
&& stream
->metadata_flag
) {
1691 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1699 * Splice the data from the ring buffer to the tracefile.
1701 * It must be called with the stream lock held.
1703 * Returns the number of bytes spliced.
1705 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1706 struct lttng_consumer_local_data
*ctx
,
1707 struct lttng_consumer_stream
*stream
, unsigned long len
,
1708 unsigned long padding
,
1709 struct ctf_packet_index
*index
)
1711 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1713 off_t orig_offset
= stream
->out_fd_offset
;
1714 int fd
= stream
->wait_fd
;
1715 /* Default is on the disk */
1716 int outfd
= stream
->out_fd
;
1717 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1719 unsigned int relayd_hang_up
= 0;
1721 switch (consumer_data
.type
) {
1722 case LTTNG_CONSUMER_KERNEL
:
1724 case LTTNG_CONSUMER32_UST
:
1725 case LTTNG_CONSUMER64_UST
:
1726 /* Not supported for user space tracing */
1729 ERR("Unknown consumer_data type");
1733 /* RCU lock for the relayd pointer */
1736 /* Flag that the current stream if set for network streaming. */
1737 if (stream
->relayd_id
!= (uint64_t) -1ULL) {
1738 relayd
= consumer_find_relayd(stream
->relayd_id
);
1739 if (relayd
== NULL
) {
1744 splice_pipe
= stream
->splice_pipe
;
1746 /* Write metadata stream id before payload */
1748 unsigned long total_len
= len
;
1750 if (stream
->metadata_flag
) {
1752 * Lock the control socket for the complete duration of the function
1753 * since from this point on we will use the socket.
1755 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1757 if (stream
->reset_metadata_flag
) {
1758 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1759 stream
->relayd_stream_id
,
1760 stream
->metadata_version
);
1765 stream
->reset_metadata_flag
= 0;
1767 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1775 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1778 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1784 /* Use the returned socket. */
1787 /* No streaming, we have to set the len with the full padding */
1790 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1791 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1793 ERR("Reset metadata file");
1796 stream
->reset_metadata_flag
= 0;
1799 * Check if we need to change the tracefile before writing the packet.
1801 if (stream
->chan
->tracefile_size
> 0 &&
1802 (stream
->tracefile_size_current
+ len
) >
1803 stream
->chan
->tracefile_size
) {
1804 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1805 stream
->name
, stream
->chan
->tracefile_size
,
1806 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1807 stream
->out_fd
, &(stream
->tracefile_count_current
),
1811 ERR("Rotating output file");
1814 outfd
= stream
->out_fd
;
1816 if (stream
->index_file
) {
1817 lttng_index_file_put(stream
->index_file
);
1818 stream
->index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1819 stream
->name
, stream
->uid
, stream
->gid
,
1820 stream
->chan
->tracefile_size
,
1821 stream
->tracefile_count_current
,
1822 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1823 if (!stream
->index_file
) {
1828 /* Reset current size because we just perform a rotation. */
1829 stream
->tracefile_size_current
= 0;
1830 stream
->out_fd_offset
= 0;
1833 stream
->tracefile_size_current
+= len
;
1834 index
->offset
= htobe64(stream
->out_fd_offset
);
1838 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1839 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1840 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1841 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1842 DBG("splice chan to pipe, ret %zd", ret_splice
);
1843 if (ret_splice
< 0) {
1846 PERROR("Error in relay splice");
1850 /* Handle stream on the relayd if the output is on the network */
1851 if (relayd
&& stream
->metadata_flag
) {
1852 size_t metadata_payload_size
=
1853 sizeof(struct lttcomm_relayd_metadata_payload
);
1855 /* Update counter to fit the spliced data */
1856 ret_splice
+= metadata_payload_size
;
1857 len
+= metadata_payload_size
;
1859 * We do this so the return value can match the len passed as
1860 * argument to this function.
1862 written
-= metadata_payload_size
;
1865 /* Splice data out */
1866 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1867 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1868 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1870 if (ret_splice
< 0) {
1875 } else if (ret_splice
> len
) {
1877 * We don't expect this code path to be executed but you never know
1878 * so this is an extra protection agains a buggy splice().
1881 written
+= ret_splice
;
1882 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1886 /* All good, update current len and continue. */
1890 /* This call is useless on a socket so better save a syscall. */
1892 /* This won't block, but will start writeout asynchronously */
1893 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1894 SYNC_FILE_RANGE_WRITE
);
1895 stream
->out_fd_offset
+= ret_splice
;
1897 stream
->output_written
+= ret_splice
;
1898 written
+= ret_splice
;
1901 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1907 * This is a special case that the relayd has closed its socket. Let's
1908 * cleanup the relayd object and all associated streams.
1910 if (relayd
&& relayd_hang_up
) {
1911 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->id
);
1912 lttng_consumer_cleanup_relayd(relayd
);
1913 /* Skip splice error so the consumer does not fail */
1918 /* send the appropriate error description to sessiond */
1921 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1924 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1927 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1932 if (relayd
&& stream
->metadata_flag
) {
1933 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1941 * Take a snapshot for a specific fd
1943 * Returns 0 on success, < 0 on error
1945 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1947 switch (consumer_data
.type
) {
1948 case LTTNG_CONSUMER_KERNEL
:
1949 return lttng_kconsumer_take_snapshot(stream
);
1950 case LTTNG_CONSUMER32_UST
:
1951 case LTTNG_CONSUMER64_UST
:
1952 return lttng_ustconsumer_take_snapshot(stream
);
1954 ERR("Unknown consumer_data type");
1961 * Get the produced position
1963 * Returns 0 on success, < 0 on error
1965 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1968 switch (consumer_data
.type
) {
1969 case LTTNG_CONSUMER_KERNEL
:
1970 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1971 case LTTNG_CONSUMER32_UST
:
1972 case LTTNG_CONSUMER64_UST
:
1973 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1975 ERR("Unknown consumer_data type");
1981 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1982 int sock
, struct pollfd
*consumer_sockpoll
)
1984 switch (consumer_data
.type
) {
1985 case LTTNG_CONSUMER_KERNEL
:
1986 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1987 case LTTNG_CONSUMER32_UST
:
1988 case LTTNG_CONSUMER64_UST
:
1989 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1991 ERR("Unknown consumer_data type");
1997 void lttng_consumer_close_all_metadata(void)
1999 switch (consumer_data
.type
) {
2000 case LTTNG_CONSUMER_KERNEL
:
2002 * The Kernel consumer has a different metadata scheme so we don't
2003 * close anything because the stream will be closed by the session
2007 case LTTNG_CONSUMER32_UST
:
2008 case LTTNG_CONSUMER64_UST
:
2010 * Close all metadata streams. The metadata hash table is passed and
2011 * this call iterates over it by closing all wakeup fd. This is safe
2012 * because at this point we are sure that the metadata producer is
2013 * either dead or blocked.
2015 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2018 ERR("Unknown consumer_data type");
2024 * Clean up a metadata stream and free its memory.
2026 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2027 struct lttng_ht
*ht
)
2029 struct lttng_consumer_channel
*free_chan
= NULL
;
2033 * This call should NEVER receive regular stream. It must always be
2034 * metadata stream and this is crucial for data structure synchronization.
2036 assert(stream
->metadata_flag
);
2038 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2040 pthread_mutex_lock(&consumer_data
.lock
);
2041 pthread_mutex_lock(&stream
->chan
->lock
);
2042 pthread_mutex_lock(&stream
->lock
);
2043 if (stream
->chan
->metadata_cache
) {
2044 /* Only applicable to userspace consumers. */
2045 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2048 /* Remove any reference to that stream. */
2049 consumer_stream_delete(stream
, ht
);
2051 /* Close down everything including the relayd if one. */
2052 consumer_stream_close(stream
);
2053 /* Destroy tracer buffers of the stream. */
2054 consumer_stream_destroy_buffers(stream
);
2056 /* Atomically decrement channel refcount since other threads can use it. */
2057 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
2058 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
2059 /* Go for channel deletion! */
2060 free_chan
= stream
->chan
;
2064 * Nullify the stream reference so it is not used after deletion. The
2065 * channel lock MUST be acquired before being able to check for a NULL
2068 stream
->chan
->metadata_stream
= NULL
;
2070 if (stream
->chan
->metadata_cache
) {
2071 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2073 pthread_mutex_unlock(&stream
->lock
);
2074 pthread_mutex_unlock(&stream
->chan
->lock
);
2075 pthread_mutex_unlock(&consumer_data
.lock
);
2078 consumer_del_channel(free_chan
);
2081 consumer_stream_free(stream
);
2085 * Action done with the metadata stream when adding it to the consumer internal
2086 * data structures to handle it.
2088 int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2090 struct lttng_ht
*ht
= metadata_ht
;
2092 struct lttng_ht_iter iter
;
2093 struct lttng_ht_node_u64
*node
;
2098 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2100 pthread_mutex_lock(&consumer_data
.lock
);
2101 pthread_mutex_lock(&stream
->chan
->lock
);
2102 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2103 pthread_mutex_lock(&stream
->lock
);
2106 * From here, refcounts are updated so be _careful_ when returning an error
2113 * Lookup the stream just to make sure it does not exist in our internal
2114 * state. This should NEVER happen.
2116 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2117 node
= lttng_ht_iter_get_node_u64(&iter
);
2121 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2122 * in terms of destroying the associated channel, because the action that
2123 * causes the count to become 0 also causes a stream to be added. The
2124 * channel deletion will thus be triggered by the following removal of this
2127 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2128 /* Increment refcount before decrementing nb_init_stream_left */
2130 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2133 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2135 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
2136 &stream
->node_channel_id
);
2139 * Add stream to the stream_list_ht of the consumer data. No need to steal
2140 * the key since the HT does not use it and we allow to add redundant keys
2143 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2147 pthread_mutex_unlock(&stream
->lock
);
2148 pthread_mutex_unlock(&stream
->chan
->lock
);
2149 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2150 pthread_mutex_unlock(&consumer_data
.lock
);
2155 * Delete data stream that are flagged for deletion (endpoint_status).
2157 static void validate_endpoint_status_data_stream(void)
2159 struct lttng_ht_iter iter
;
2160 struct lttng_consumer_stream
*stream
;
2162 DBG("Consumer delete flagged data stream");
2165 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2166 /* Validate delete flag of the stream */
2167 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2170 /* Delete it right now */
2171 consumer_del_stream(stream
, data_ht
);
2177 * Delete metadata stream that are flagged for deletion (endpoint_status).
2179 static void validate_endpoint_status_metadata_stream(
2180 struct lttng_poll_event
*pollset
)
2182 struct lttng_ht_iter iter
;
2183 struct lttng_consumer_stream
*stream
;
2185 DBG("Consumer delete flagged metadata stream");
2190 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2191 /* Validate delete flag of the stream */
2192 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2196 * Remove from pollset so the metadata thread can continue without
2197 * blocking on a deleted stream.
2199 lttng_poll_del(pollset
, stream
->wait_fd
);
2201 /* Delete it right now */
2202 consumer_del_metadata_stream(stream
, metadata_ht
);
2208 * Thread polls on metadata file descriptor and write them on disk or on the
2211 void *consumer_thread_metadata_poll(void *data
)
2213 int ret
, i
, pollfd
, err
= -1;
2214 uint32_t revents
, nb_fd
;
2215 struct lttng_consumer_stream
*stream
= NULL
;
2216 struct lttng_ht_iter iter
;
2217 struct lttng_ht_node_u64
*node
;
2218 struct lttng_poll_event events
;
2219 struct lttng_consumer_local_data
*ctx
= data
;
2222 rcu_register_thread();
2224 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2226 if (testpoint(consumerd_thread_metadata
)) {
2227 goto error_testpoint
;
2230 health_code_update();
2232 DBG("Thread metadata poll started");
2234 /* Size is set to 1 for the consumer_metadata pipe */
2235 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2237 ERR("Poll set creation failed");
2241 ret
= lttng_poll_add(&events
,
2242 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2248 DBG("Metadata main loop started");
2252 health_code_update();
2253 health_poll_entry();
2254 DBG("Metadata poll wait");
2255 ret
= lttng_poll_wait(&events
, -1);
2256 DBG("Metadata poll return from wait with %d fd(s)",
2257 LTTNG_POLL_GETNB(&events
));
2259 DBG("Metadata event caught in thread");
2261 if (errno
== EINTR
) {
2262 ERR("Poll EINTR caught");
2265 if (LTTNG_POLL_GETNB(&events
) == 0) {
2266 err
= 0; /* All is OK */
2273 /* From here, the event is a metadata wait fd */
2274 for (i
= 0; i
< nb_fd
; i
++) {
2275 health_code_update();
2277 revents
= LTTNG_POLL_GETEV(&events
, i
);
2278 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2281 /* No activity for this FD (poll implementation). */
2285 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2286 if (revents
& LPOLLIN
) {
2289 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2290 &stream
, sizeof(stream
));
2291 if (pipe_len
< sizeof(stream
)) {
2293 PERROR("read metadata stream");
2296 * Remove the pipe from the poll set and continue the loop
2297 * since their might be data to consume.
2299 lttng_poll_del(&events
,
2300 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2301 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2305 /* A NULL stream means that the state has changed. */
2306 if (stream
== NULL
) {
2307 /* Check for deleted streams. */
2308 validate_endpoint_status_metadata_stream(&events
);
2312 DBG("Adding metadata stream %d to poll set",
2315 /* Add metadata stream to the global poll events list */
2316 lttng_poll_add(&events
, stream
->wait_fd
,
2317 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2318 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2319 DBG("Metadata thread pipe hung up");
2321 * Remove the pipe from the poll set and continue the loop
2322 * since their might be data to consume.
2324 lttng_poll_del(&events
,
2325 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2326 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2329 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2333 /* Handle other stream */
2339 uint64_t tmp_id
= (uint64_t) pollfd
;
2341 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2343 node
= lttng_ht_iter_get_node_u64(&iter
);
2346 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2349 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2350 /* Get the data out of the metadata file descriptor */
2351 DBG("Metadata available on fd %d", pollfd
);
2352 assert(stream
->wait_fd
== pollfd
);
2355 health_code_update();
2357 len
= ctx
->on_buffer_ready(stream
, ctx
);
2359 * We don't check the return value here since if we get
2360 * a negative len, it means an error occurred thus we
2361 * simply remove it from the poll set and free the
2366 /* It's ok to have an unavailable sub-buffer */
2367 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2368 /* Clean up stream from consumer and free it. */
2369 lttng_poll_del(&events
, stream
->wait_fd
);
2370 consumer_del_metadata_stream(stream
, metadata_ht
);
2372 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2373 DBG("Metadata fd %d is hup|err.", pollfd
);
2374 if (!stream
->hangup_flush_done
2375 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2376 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2377 DBG("Attempting to flush and consume the UST buffers");
2378 lttng_ustconsumer_on_stream_hangup(stream
);
2380 /* We just flushed the stream now read it. */
2382 health_code_update();
2384 len
= ctx
->on_buffer_ready(stream
, ctx
);
2386 * We don't check the return value here since if we get
2387 * a negative len, it means an error occurred thus we
2388 * simply remove it from the poll set and free the
2394 lttng_poll_del(&events
, stream
->wait_fd
);
2396 * This call update the channel states, closes file descriptors
2397 * and securely free the stream.
2399 consumer_del_metadata_stream(stream
, metadata_ht
);
2401 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2405 /* Release RCU lock for the stream looked up */
2413 DBG("Metadata poll thread exiting");
2415 lttng_poll_clean(&events
);
2420 ERR("Health error occurred in %s", __func__
);
2422 health_unregister(health_consumerd
);
2423 rcu_unregister_thread();
2428 * This thread polls the fds in the set to consume the data and write
2429 * it to tracefile if necessary.
2431 void *consumer_thread_data_poll(void *data
)
2433 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2434 struct pollfd
*pollfd
= NULL
;
2435 /* local view of the streams */
2436 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2437 /* local view of consumer_data.fds_count */
2439 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2440 int nb_inactive_fd
= 0;
2441 struct lttng_consumer_local_data
*ctx
= data
;
2444 rcu_register_thread();
2446 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2448 if (testpoint(consumerd_thread_data
)) {
2449 goto error_testpoint
;
2452 health_code_update();
2454 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2455 if (local_stream
== NULL
) {
2456 PERROR("local_stream malloc");
2461 health_code_update();
2467 * the fds set has been updated, we need to update our
2468 * local array as well
2470 pthread_mutex_lock(&consumer_data
.lock
);
2471 if (consumer_data
.need_update
) {
2476 local_stream
= NULL
;
2479 * Allocate for all fds +1 for the consumer_data_pipe and +1 for
2482 pollfd
= zmalloc((consumer_data
.stream_count
+ 2) * sizeof(struct pollfd
));
2483 if (pollfd
== NULL
) {
2484 PERROR("pollfd malloc");
2485 pthread_mutex_unlock(&consumer_data
.lock
);
2489 local_stream
= zmalloc((consumer_data
.stream_count
+ 2) *
2490 sizeof(struct lttng_consumer_stream
*));
2491 if (local_stream
== NULL
) {
2492 PERROR("local_stream malloc");
2493 pthread_mutex_unlock(&consumer_data
.lock
);
2496 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2497 data_ht
, &nb_inactive_fd
);
2499 ERR("Error in allocating pollfd or local_outfds");
2500 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2501 pthread_mutex_unlock(&consumer_data
.lock
);
2505 consumer_data
.need_update
= 0;
2507 pthread_mutex_unlock(&consumer_data
.lock
);
2509 /* No FDs and consumer_quit, consumer_cleanup the thread */
2510 if (nb_fd
== 0 && consumer_quit
== 1 && nb_inactive_fd
== 0) {
2511 err
= 0; /* All is OK */
2514 /* poll on the array of fds */
2516 DBG("polling on %d fd", nb_fd
+ 2);
2517 health_poll_entry();
2518 num_rdy
= poll(pollfd
, nb_fd
+ 2, -1);
2520 DBG("poll num_rdy : %d", num_rdy
);
2521 if (num_rdy
== -1) {
2523 * Restart interrupted system call.
2525 if (errno
== EINTR
) {
2528 PERROR("Poll error");
2529 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2531 } else if (num_rdy
== 0) {
2532 DBG("Polling thread timed out");
2537 * If the consumer_data_pipe triggered poll go directly to the
2538 * beginning of the loop to update the array. We want to prioritize
2539 * array update over low-priority reads.
2541 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2542 ssize_t pipe_readlen
;
2544 DBG("consumer_data_pipe wake up");
2545 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2546 &new_stream
, sizeof(new_stream
));
2547 if (pipe_readlen
< sizeof(new_stream
)) {
2548 PERROR("Consumer data pipe");
2549 /* Continue so we can at least handle the current stream(s). */
2554 * If the stream is NULL, just ignore it. It's also possible that
2555 * the sessiond poll thread changed the consumer_quit state and is
2556 * waking us up to test it.
2558 if (new_stream
== NULL
) {
2559 validate_endpoint_status_data_stream();
2563 /* Continue to update the local streams and handle prio ones */
2567 /* Handle wakeup pipe. */
2568 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2570 ssize_t pipe_readlen
;
2572 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2574 if (pipe_readlen
< 0) {
2575 PERROR("Consumer data wakeup pipe");
2577 /* We've been awakened to handle stream(s). */
2578 ctx
->has_wakeup
= 0;
2581 /* Take care of high priority channels first. */
2582 for (i
= 0; i
< nb_fd
; i
++) {
2583 health_code_update();
2585 if (local_stream
[i
] == NULL
) {
2588 if (pollfd
[i
].revents
& POLLPRI
) {
2589 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2591 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2592 /* it's ok to have an unavailable sub-buffer */
2593 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2594 /* Clean the stream and free it. */
2595 consumer_del_stream(local_stream
[i
], data_ht
);
2596 local_stream
[i
] = NULL
;
2597 } else if (len
> 0) {
2598 local_stream
[i
]->data_read
= 1;
2604 * If we read high prio channel in this loop, try again
2605 * for more high prio data.
2611 /* Take care of low priority channels. */
2612 for (i
= 0; i
< nb_fd
; i
++) {
2613 health_code_update();
2615 if (local_stream
[i
] == NULL
) {
2618 if ((pollfd
[i
].revents
& POLLIN
) ||
2619 local_stream
[i
]->hangup_flush_done
||
2620 local_stream
[i
]->has_data
) {
2621 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2622 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2623 /* it's ok to have an unavailable sub-buffer */
2624 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2625 /* Clean the stream and free it. */
2626 consumer_del_stream(local_stream
[i
], data_ht
);
2627 local_stream
[i
] = NULL
;
2628 } else if (len
> 0) {
2629 local_stream
[i
]->data_read
= 1;
2634 /* Handle hangup and errors */
2635 for (i
= 0; i
< nb_fd
; i
++) {
2636 health_code_update();
2638 if (local_stream
[i
] == NULL
) {
2641 if (!local_stream
[i
]->hangup_flush_done
2642 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2643 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2644 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2645 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2647 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2648 /* Attempt read again, for the data we just flushed. */
2649 local_stream
[i
]->data_read
= 1;
2652 * If the poll flag is HUP/ERR/NVAL and we have
2653 * read no data in this pass, we can remove the
2654 * stream from its hash table.
2656 if ((pollfd
[i
].revents
& POLLHUP
)) {
2657 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2658 if (!local_stream
[i
]->data_read
) {
2659 consumer_del_stream(local_stream
[i
], data_ht
);
2660 local_stream
[i
] = NULL
;
2663 } else if (pollfd
[i
].revents
& POLLERR
) {
2664 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2665 if (!local_stream
[i
]->data_read
) {
2666 consumer_del_stream(local_stream
[i
], data_ht
);
2667 local_stream
[i
] = NULL
;
2670 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2671 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2672 if (!local_stream
[i
]->data_read
) {
2673 consumer_del_stream(local_stream
[i
], data_ht
);
2674 local_stream
[i
] = NULL
;
2678 if (local_stream
[i
] != NULL
) {
2679 local_stream
[i
]->data_read
= 0;
2686 DBG("polling thread exiting");
2691 * Close the write side of the pipe so epoll_wait() in
2692 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2693 * read side of the pipe. If we close them both, epoll_wait strangely does
2694 * not return and could create a endless wait period if the pipe is the
2695 * only tracked fd in the poll set. The thread will take care of closing
2698 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2703 ERR("Health error occurred in %s", __func__
);
2705 health_unregister(health_consumerd
);
2707 rcu_unregister_thread();
2712 * Close wake-up end of each stream belonging to the channel. This will
2713 * allow the poll() on the stream read-side to detect when the
2714 * write-side (application) finally closes them.
2717 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2719 struct lttng_ht
*ht
;
2720 struct lttng_consumer_stream
*stream
;
2721 struct lttng_ht_iter iter
;
2723 ht
= consumer_data
.stream_per_chan_id_ht
;
2726 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2727 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2728 ht
->match_fct
, &channel
->key
,
2729 &iter
.iter
, stream
, node_channel_id
.node
) {
2731 * Protect against teardown with mutex.
2733 pthread_mutex_lock(&stream
->lock
);
2734 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2737 switch (consumer_data
.type
) {
2738 case LTTNG_CONSUMER_KERNEL
:
2740 case LTTNG_CONSUMER32_UST
:
2741 case LTTNG_CONSUMER64_UST
:
2742 if (stream
->metadata_flag
) {
2743 /* Safe and protected by the stream lock. */
2744 lttng_ustconsumer_close_metadata(stream
->chan
);
2747 * Note: a mutex is taken internally within
2748 * liblttng-ust-ctl to protect timer wakeup_fd
2749 * use from concurrent close.
2751 lttng_ustconsumer_close_stream_wakeup(stream
);
2755 ERR("Unknown consumer_data type");
2759 pthread_mutex_unlock(&stream
->lock
);
2764 static void destroy_channel_ht(struct lttng_ht
*ht
)
2766 struct lttng_ht_iter iter
;
2767 struct lttng_consumer_channel
*channel
;
2775 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2776 ret
= lttng_ht_del(ht
, &iter
);
2781 lttng_ht_destroy(ht
);
2785 * This thread polls the channel fds to detect when they are being
2786 * closed. It closes all related streams if the channel is detected as
2787 * closed. It is currently only used as a shim layer for UST because the
2788 * consumerd needs to keep the per-stream wakeup end of pipes open for
2791 void *consumer_thread_channel_poll(void *data
)
2793 int ret
, i
, pollfd
, err
= -1;
2794 uint32_t revents
, nb_fd
;
2795 struct lttng_consumer_channel
*chan
= NULL
;
2796 struct lttng_ht_iter iter
;
2797 struct lttng_ht_node_u64
*node
;
2798 struct lttng_poll_event events
;
2799 struct lttng_consumer_local_data
*ctx
= data
;
2800 struct lttng_ht
*channel_ht
;
2802 rcu_register_thread();
2804 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2806 if (testpoint(consumerd_thread_channel
)) {
2807 goto error_testpoint
;
2810 health_code_update();
2812 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2814 /* ENOMEM at this point. Better to bail out. */
2818 DBG("Thread channel poll started");
2820 /* Size is set to 1 for the consumer_channel pipe */
2821 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2823 ERR("Poll set creation failed");
2827 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2833 DBG("Channel main loop started");
2837 health_code_update();
2838 DBG("Channel poll wait");
2839 health_poll_entry();
2840 ret
= lttng_poll_wait(&events
, -1);
2841 DBG("Channel poll return from wait with %d fd(s)",
2842 LTTNG_POLL_GETNB(&events
));
2844 DBG("Channel event caught in thread");
2846 if (errno
== EINTR
) {
2847 ERR("Poll EINTR caught");
2850 if (LTTNG_POLL_GETNB(&events
) == 0) {
2851 err
= 0; /* All is OK */
2858 /* From here, the event is a channel wait fd */
2859 for (i
= 0; i
< nb_fd
; i
++) {
2860 health_code_update();
2862 revents
= LTTNG_POLL_GETEV(&events
, i
);
2863 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2866 /* No activity for this FD (poll implementation). */
2870 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2871 if (revents
& LPOLLIN
) {
2872 enum consumer_channel_action action
;
2875 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2878 ERR("Error reading channel pipe");
2880 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2885 case CONSUMER_CHANNEL_ADD
:
2886 DBG("Adding channel %d to poll set",
2889 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2892 lttng_ht_add_unique_u64(channel_ht
,
2893 &chan
->wait_fd_node
);
2895 /* Add channel to the global poll events list */
2896 lttng_poll_add(&events
, chan
->wait_fd
,
2897 LPOLLERR
| LPOLLHUP
);
2899 case CONSUMER_CHANNEL_DEL
:
2902 * This command should never be called if the channel
2903 * has streams monitored by either the data or metadata
2904 * thread. The consumer only notify this thread with a
2905 * channel del. command if it receives a destroy
2906 * channel command from the session daemon that send it
2907 * if a command prior to the GET_CHANNEL failed.
2911 chan
= consumer_find_channel(key
);
2914 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2917 lttng_poll_del(&events
, chan
->wait_fd
);
2918 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2919 ret
= lttng_ht_del(channel_ht
, &iter
);
2922 switch (consumer_data
.type
) {
2923 case LTTNG_CONSUMER_KERNEL
:
2925 case LTTNG_CONSUMER32_UST
:
2926 case LTTNG_CONSUMER64_UST
:
2927 health_code_update();
2928 /* Destroy streams that might have been left in the stream list. */
2929 clean_channel_stream_list(chan
);
2932 ERR("Unknown consumer_data type");
2937 * Release our own refcount. Force channel deletion even if
2938 * streams were not initialized.
2940 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2941 consumer_del_channel(chan
);
2946 case CONSUMER_CHANNEL_QUIT
:
2948 * Remove the pipe from the poll set and continue the loop
2949 * since their might be data to consume.
2951 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2954 ERR("Unknown action");
2957 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2958 DBG("Channel thread pipe hung up");
2960 * Remove the pipe from the poll set and continue the loop
2961 * since their might be data to consume.
2963 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2966 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2970 /* Handle other stream */
2976 uint64_t tmp_id
= (uint64_t) pollfd
;
2978 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2980 node
= lttng_ht_iter_get_node_u64(&iter
);
2983 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2986 /* Check for error event */
2987 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2988 DBG("Channel fd %d is hup|err.", pollfd
);
2990 lttng_poll_del(&events
, chan
->wait_fd
);
2991 ret
= lttng_ht_del(channel_ht
, &iter
);
2995 * This will close the wait fd for each stream associated to
2996 * this channel AND monitored by the data/metadata thread thus
2997 * will be clean by the right thread.
2999 consumer_close_channel_streams(chan
);
3001 /* Release our own refcount */
3002 if (!uatomic_sub_return(&chan
->refcount
, 1)
3003 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3004 consumer_del_channel(chan
);
3007 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3012 /* Release RCU lock for the channel looked up */
3020 lttng_poll_clean(&events
);
3022 destroy_channel_ht(channel_ht
);
3025 DBG("Channel poll thread exiting");
3028 ERR("Health error occurred in %s", __func__
);
3030 health_unregister(health_consumerd
);
3031 rcu_unregister_thread();
3035 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3036 struct pollfd
*sockpoll
, int client_socket
)
3043 ret
= lttng_consumer_poll_socket(sockpoll
);
3047 DBG("Metadata connection on client_socket");
3049 /* Blocking call, waiting for transmission */
3050 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3051 if (ctx
->consumer_metadata_socket
< 0) {
3052 WARN("On accept metadata");
3063 * This thread listens on the consumerd socket and receives the file
3064 * descriptors from the session daemon.
3066 void *consumer_thread_sessiond_poll(void *data
)
3068 int sock
= -1, client_socket
, ret
, err
= -1;
3070 * structure to poll for incoming data on communication socket avoids
3071 * making blocking sockets.
3073 struct pollfd consumer_sockpoll
[2];
3074 struct lttng_consumer_local_data
*ctx
= data
;
3076 rcu_register_thread();
3078 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3080 if (testpoint(consumerd_thread_sessiond
)) {
3081 goto error_testpoint
;
3084 health_code_update();
3086 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3087 unlink(ctx
->consumer_command_sock_path
);
3088 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3089 if (client_socket
< 0) {
3090 ERR("Cannot create command socket");
3094 ret
= lttcomm_listen_unix_sock(client_socket
);
3099 DBG("Sending ready command to lttng-sessiond");
3100 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3101 /* return < 0 on error, but == 0 is not fatal */
3103 ERR("Error sending ready command to lttng-sessiond");
3107 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3108 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3109 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3110 consumer_sockpoll
[1].fd
= client_socket
;
3111 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3113 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3121 DBG("Connection on client_socket");
3123 /* Blocking call, waiting for transmission */
3124 sock
= lttcomm_accept_unix_sock(client_socket
);
3131 * Setup metadata socket which is the second socket connection on the
3132 * command unix socket.
3134 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3143 /* This socket is not useful anymore. */
3144 ret
= close(client_socket
);
3146 PERROR("close client_socket");
3150 /* update the polling structure to poll on the established socket */
3151 consumer_sockpoll
[1].fd
= sock
;
3152 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3155 health_code_update();
3157 health_poll_entry();
3158 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3167 DBG("Incoming command on sock");
3168 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3171 * This could simply be a session daemon quitting. Don't output
3174 DBG("Communication interrupted on command socket");
3178 if (consumer_quit
) {
3179 DBG("consumer_thread_receive_fds received quit from signal");
3180 err
= 0; /* All is OK */
3183 DBG("received command on sock");
3189 DBG("Consumer thread sessiond poll exiting");
3192 * Close metadata streams since the producer is the session daemon which
3195 * NOTE: for now, this only applies to the UST tracer.
3197 lttng_consumer_close_all_metadata();
3200 * when all fds have hung up, the polling thread
3206 * Notify the data poll thread to poll back again and test the
3207 * consumer_quit state that we just set so to quit gracefully.
3209 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3211 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3213 notify_health_quit_pipe(health_quit_pipe
);
3215 /* Cleaning up possibly open sockets. */
3219 PERROR("close sock sessiond poll");
3222 if (client_socket
>= 0) {
3223 ret
= close(client_socket
);
3225 PERROR("close client_socket sessiond poll");
3232 ERR("Health error occurred in %s", __func__
);
3234 health_unregister(health_consumerd
);
3236 rcu_unregister_thread();
3240 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3241 struct lttng_consumer_local_data
*ctx
)
3245 pthread_mutex_lock(&stream
->lock
);
3246 if (stream
->metadata_flag
) {
3247 pthread_mutex_lock(&stream
->metadata_rdv_lock
);
3250 switch (consumer_data
.type
) {
3251 case LTTNG_CONSUMER_KERNEL
:
3252 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3254 case LTTNG_CONSUMER32_UST
:
3255 case LTTNG_CONSUMER64_UST
:
3256 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3259 ERR("Unknown consumer_data type");
3265 if (stream
->metadata_flag
) {
3266 pthread_cond_broadcast(&stream
->metadata_rdv
);
3267 pthread_mutex_unlock(&stream
->metadata_rdv_lock
);
3269 pthread_mutex_unlock(&stream
->lock
);
3273 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3275 switch (consumer_data
.type
) {
3276 case LTTNG_CONSUMER_KERNEL
:
3277 return lttng_kconsumer_on_recv_stream(stream
);
3278 case LTTNG_CONSUMER32_UST
:
3279 case LTTNG_CONSUMER64_UST
:
3280 return lttng_ustconsumer_on_recv_stream(stream
);
3282 ERR("Unknown consumer_data type");
3289 * Allocate and set consumer data hash tables.
3291 int lttng_consumer_init(void)
3293 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3294 if (!consumer_data
.channel_ht
) {
3298 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3299 if (!consumer_data
.relayd_ht
) {
3303 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3304 if (!consumer_data
.stream_list_ht
) {
3308 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3309 if (!consumer_data
.stream_per_chan_id_ht
) {
3313 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3318 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3330 * Process the ADD_RELAYD command receive by a consumer.
3332 * This will create a relayd socket pair and add it to the relayd hash table.
3333 * The caller MUST acquire a RCU read side lock before calling it.
3335 void consumer_add_relayd_socket(uint64_t relayd_id
, int sock_type
,
3336 struct lttng_consumer_local_data
*ctx
, int sock
,
3337 struct pollfd
*consumer_sockpoll
,
3338 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3339 uint64_t relayd_session_id
)
3341 int fd
= -1, ret
= -1, relayd_created
= 0;
3342 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3343 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3346 assert(relayd_sock
);
3348 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", relayd_id
);
3350 /* Get relayd reference if exists. */
3351 relayd
= consumer_find_relayd(relayd_id
);
3352 if (relayd
== NULL
) {
3353 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3354 /* Not found. Allocate one. */
3355 relayd
= consumer_allocate_relayd_sock_pair(relayd_id
);
3356 if (relayd
== NULL
) {
3358 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3361 relayd
->sessiond_session_id
= sessiond_id
;
3366 * This code path MUST continue to the consumer send status message to
3367 * we can notify the session daemon and continue our work without
3368 * killing everything.
3372 * relayd key should never be found for control socket.
3374 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3377 /* First send a status message before receiving the fds. */
3378 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3380 /* Somehow, the session daemon is not responding anymore. */
3381 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3382 goto error_nosignal
;
3385 /* Poll on consumer socket. */
3386 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3388 /* Needing to exit in the middle of a command: error. */
3389 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3391 goto error_nosignal
;
3394 /* Get relayd socket from session daemon */
3395 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3396 if (ret
!= sizeof(fd
)) {
3398 fd
= -1; /* Just in case it gets set with an invalid value. */
3401 * Failing to receive FDs might indicate a major problem such as
3402 * reaching a fd limit during the receive where the kernel returns a
3403 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3404 * don't take any chances and stop everything.
3406 * XXX: Feature request #558 will fix that and avoid this possible
3407 * issue when reaching the fd limit.
3409 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3410 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3414 /* Copy socket information and received FD */
3415 switch (sock_type
) {
3416 case LTTNG_STREAM_CONTROL
:
3417 /* Copy received lttcomm socket */
3418 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3419 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3420 /* Handle create_sock error. */
3422 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3426 * Close the socket created internally by
3427 * lttcomm_create_sock, so we can replace it by the one
3428 * received from sessiond.
3430 if (close(relayd
->control_sock
.sock
.fd
)) {
3434 /* Assign new file descriptor */
3435 relayd
->control_sock
.sock
.fd
= fd
;
3436 fd
= -1; /* For error path */
3437 /* Assign version values. */
3438 relayd
->control_sock
.major
= relayd_sock
->major
;
3439 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3441 relayd
->relayd_session_id
= relayd_session_id
;
3444 case LTTNG_STREAM_DATA
:
3445 /* Copy received lttcomm socket */
3446 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3447 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3448 /* Handle create_sock error. */
3450 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3454 * Close the socket created internally by
3455 * lttcomm_create_sock, so we can replace it by the one
3456 * received from sessiond.
3458 if (close(relayd
->data_sock
.sock
.fd
)) {
3462 /* Assign new file descriptor */
3463 relayd
->data_sock
.sock
.fd
= fd
;
3464 fd
= -1; /* for eventual error paths */
3465 /* Assign version values. */
3466 relayd
->data_sock
.major
= relayd_sock
->major
;
3467 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3470 ERR("Unknown relayd socket type (%d)", sock_type
);
3472 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3476 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3477 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3480 /* We successfully added the socket. Send status back. */
3481 ret
= consumer_send_status_msg(sock
, ret_code
);
3483 /* Somehow, the session daemon is not responding anymore. */
3484 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3485 goto error_nosignal
;
3489 * Add relayd socket pair to consumer data hashtable. If object already
3490 * exists or on error, the function gracefully returns.
3499 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3500 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3504 /* Close received socket if valid. */
3507 PERROR("close received socket");
3511 if (relayd_created
) {
3517 * Search for a relayd associated to the session id and return the reference.
3519 * A rcu read side lock MUST be acquire before calling this function and locked
3520 * until the relayd object is no longer necessary.
3522 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3524 struct lttng_ht_iter iter
;
3525 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3527 /* Iterate over all relayd since they are indexed by relayd_id. */
3528 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3531 * Check by sessiond id which is unique here where the relayd session
3532 * id might not be when having multiple relayd.
3534 if (relayd
->sessiond_session_id
== id
) {
3535 /* Found the relayd. There can be only one per id. */
3547 * Check if for a given session id there is still data needed to be extract
3550 * Return 1 if data is pending or else 0 meaning ready to be read.
3552 int consumer_data_pending(uint64_t id
)
3555 struct lttng_ht_iter iter
;
3556 struct lttng_ht
*ht
;
3557 struct lttng_consumer_stream
*stream
;
3558 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3559 int (*data_pending
)(struct lttng_consumer_stream
*);
3561 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3564 pthread_mutex_lock(&consumer_data
.lock
);
3566 switch (consumer_data
.type
) {
3567 case LTTNG_CONSUMER_KERNEL
:
3568 data_pending
= lttng_kconsumer_data_pending
;
3570 case LTTNG_CONSUMER32_UST
:
3571 case LTTNG_CONSUMER64_UST
:
3572 data_pending
= lttng_ustconsumer_data_pending
;
3575 ERR("Unknown consumer data type");
3579 /* Ease our life a bit */
3580 ht
= consumer_data
.stream_list_ht
;
3582 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3583 ht
->hash_fct(&id
, lttng_ht_seed
),
3585 &iter
.iter
, stream
, node_session_id
.node
) {
3586 pthread_mutex_lock(&stream
->lock
);
3589 * A removed node from the hash table indicates that the stream has
3590 * been deleted thus having a guarantee that the buffers are closed
3591 * on the consumer side. However, data can still be transmitted
3592 * over the network so don't skip the relayd check.
3594 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3596 /* Check the stream if there is data in the buffers. */
3597 ret
= data_pending(stream
);
3599 DBG("Data is pending locally on stream %" PRIu64
, stream
->key
);
3600 pthread_mutex_unlock(&stream
->lock
);
3605 pthread_mutex_unlock(&stream
->lock
);
3608 relayd
= find_relayd_by_session_id(id
);
3610 unsigned int is_data_inflight
= 0;
3612 /* Send init command for data pending. */
3613 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3614 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3615 relayd
->relayd_session_id
);
3617 /* Communication error thus the relayd so no data pending. */
3618 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3619 ERR("Relayd begin data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->id
);
3620 lttng_consumer_cleanup_relayd(relayd
);
3621 goto data_not_pending
;
3624 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3625 ht
->hash_fct(&id
, lttng_ht_seed
),
3627 &iter
.iter
, stream
, node_session_id
.node
) {
3628 if (stream
->metadata_flag
) {
3629 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3630 stream
->relayd_stream_id
);
3632 ret
= relayd_data_pending(&relayd
->control_sock
,
3633 stream
->relayd_stream_id
,
3634 stream
->next_net_seq_num
- 1);
3637 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3641 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->id
);
3642 lttng_consumer_cleanup_relayd(relayd
);
3643 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3644 goto data_not_pending
;
3648 /* Send end command for data pending. */
3649 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3650 relayd
->relayd_session_id
, &is_data_inflight
);
3651 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3653 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->id
);
3654 lttng_consumer_cleanup_relayd(relayd
);
3655 goto data_not_pending
;
3657 if (is_data_inflight
) {
3658 DBG("Data is in flight on relayd %" PRIu64
, relayd
->id
);
3664 * Finding _no_ node in the hash table and no inflight data means that the
3665 * stream(s) have been removed thus data is guaranteed to be available for
3666 * analysis from the trace files.
3670 /* Data is available to be read by a viewer. */
3671 pthread_mutex_unlock(&consumer_data
.lock
);
3676 /* Data is still being extracted from buffers. */
3677 pthread_mutex_unlock(&consumer_data
.lock
);
3683 * Send a ret code status message to the sessiond daemon.
3685 * Return the sendmsg() return value.
3687 int consumer_send_status_msg(int sock
, int ret_code
)
3689 struct lttcomm_consumer_status_msg msg
;
3691 memset(&msg
, 0, sizeof(msg
));
3692 msg
.ret_code
= ret_code
;
3694 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3698 * Send a channel status message to the sessiond daemon.
3700 * Return the sendmsg() return value.
3702 int consumer_send_status_channel(int sock
,
3703 struct lttng_consumer_channel
*channel
)
3705 struct lttcomm_consumer_status_channel msg
;
3709 memset(&msg
, 0, sizeof(msg
));
3711 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3713 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3714 msg
.key
= channel
->key
;
3715 msg
.stream_count
= channel
->streams
.count
;
3718 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3721 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3722 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3723 uint64_t max_sb_size
)
3725 unsigned long start_pos
;
3727 if (!nb_packets_per_stream
) {
3728 return consumed_pos
; /* Grab everything */
3730 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
3731 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3732 if ((long) (start_pos
- consumed_pos
) < 0) {
3733 return consumed_pos
; /* Grab everything */