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(uint64_t channel_key
,
537 enum lttng_consumer_stream_state state
,
538 const char *channel_name
,
545 enum consumer_channel_type type
,
546 unsigned int monitor
)
549 struct lttng_consumer_stream
*stream
;
551 stream
= zmalloc(sizeof(*stream
));
552 if (stream
== NULL
) {
553 PERROR("malloc struct lttng_consumer_stream");
560 stream
->key
= stream_key
;
562 stream
->out_fd_offset
= 0;
563 stream
->output_written
= 0;
564 stream
->state
= state
;
567 stream
->relayd_id
= relayd_id
;
568 stream
->session_id
= session_id
;
569 stream
->monitor
= monitor
;
570 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
571 stream
->index_file
= NULL
;
572 stream
->last_sequence_number
= -1ULL;
573 pthread_mutex_init(&stream
->lock
, NULL
);
574 pthread_mutex_init(&stream
->metadata_timer_lock
, NULL
);
576 /* If channel is the metadata, flag this stream as metadata. */
577 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
578 stream
->metadata_flag
= 1;
579 /* Metadata is flat out. */
580 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
581 /* Live rendez-vous point. */
582 pthread_cond_init(&stream
->metadata_rdv
, NULL
);
583 pthread_mutex_init(&stream
->metadata_rdv_lock
, NULL
);
585 /* Format stream name to <channel_name>_<cpu_number> */
586 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
589 PERROR("snprintf stream name");
594 /* Key is always the wait_fd for streams. */
595 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
597 /* Init node per channel id key */
598 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
600 /* Init session id node with the stream session id */
601 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
603 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
604 " relayd_id %" PRIu64
", session_id %" PRIu64
,
605 stream
->name
, stream
->key
, channel_key
,
606 stream
->relayd_id
, stream
->session_id
);
622 * Add a stream to the global list protected by a mutex.
624 int consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
626 struct lttng_ht
*ht
= data_ht
;
632 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
634 pthread_mutex_lock(&consumer_data
.lock
);
635 pthread_mutex_lock(&stream
->chan
->lock
);
636 pthread_mutex_lock(&stream
->chan
->timer_lock
);
637 pthread_mutex_lock(&stream
->lock
);
640 /* Steal stream identifier to avoid having streams with the same key */
641 steal_stream_key(stream
->key
, ht
);
643 lttng_ht_add_unique_u64(ht
, &stream
->node
);
645 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
646 &stream
->node_channel_id
);
649 * Add stream to the stream_list_ht of the consumer data. No need to steal
650 * the key since the HT does not use it and we allow to add redundant keys
653 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
656 * When nb_init_stream_left reaches 0, we don't need to trigger any action
657 * in terms of destroying the associated channel, because the action that
658 * causes the count to become 0 also causes a stream to be added. The
659 * channel deletion will thus be triggered by the following removal of this
662 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
663 /* Increment refcount before decrementing nb_init_stream_left */
665 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
668 /* Update consumer data once the node is inserted. */
669 consumer_data
.stream_count
++;
670 consumer_data
.need_update
= 1;
673 pthread_mutex_unlock(&stream
->lock
);
674 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
675 pthread_mutex_unlock(&stream
->chan
->lock
);
676 pthread_mutex_unlock(&consumer_data
.lock
);
681 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
683 consumer_del_stream(stream
, data_ht
);
687 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
688 * be acquired before calling this.
690 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
693 struct lttng_ht_node_u64
*node
;
694 struct lttng_ht_iter iter
;
698 lttng_ht_lookup(consumer_data
.relayd_ht
,
700 node
= lttng_ht_iter_get_node_u64(&iter
);
704 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
711 * Allocate and return a consumer relayd socket.
713 static struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
716 struct consumer_relayd_sock_pair
*obj
= NULL
;
718 /* net sequence index of -1 is a failure */
719 if (relayd_id
== (uint64_t) -1ULL) {
723 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
725 PERROR("zmalloc relayd sock");
731 obj
->destroy_flag
= 0;
732 obj
->control_sock
.sock
.fd
= -1;
733 obj
->data_sock
.sock
.fd
= -1;
734 lttng_ht_node_init_u64(&obj
->node
, obj
->id
);
735 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
742 * Find a relayd socket pair in the global consumer data.
744 * Return the object if found else NULL.
745 * RCU read-side lock must be held across this call and while using the
748 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
750 struct lttng_ht_iter iter
;
751 struct lttng_ht_node_u64
*node
;
752 struct consumer_relayd_sock_pair
*relayd
= NULL
;
754 /* Negative keys are lookup failures */
755 if (key
== (uint64_t) -1ULL) {
759 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
761 node
= lttng_ht_iter_get_node_u64(&iter
);
763 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
771 * Find a relayd and send the stream
773 * Returns 0 on success, < 0 on error
775 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
779 struct consumer_relayd_sock_pair
*relayd
;
782 assert(stream
->relayd_id
!= -1ULL);
785 /* The stream is not metadata. Get relayd reference if exists. */
787 relayd
= consumer_find_relayd(stream
->relayd_id
);
788 if (relayd
!= NULL
) {
789 /* Add stream on the relayd */
790 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
791 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
792 path
, &stream
->relayd_stream_id
,
793 stream
->chan
->tracefile_size
, stream
->chan
->tracefile_count
);
794 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
796 ERR("Relayd add stream failed. Cleaning up relayd %" PRIu64
".", relayd
->id
);
797 lttng_consumer_cleanup_relayd(relayd
);
801 uatomic_inc(&relayd
->refcount
);
802 stream
->sent_to_relayd
= 1;
804 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
805 stream
->key
, stream
->relayd_id
);
810 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
811 stream
->name
, stream
->key
, stream
->relayd_id
);
819 * Find a relayd and send the streams sent message
821 * Returns 0 on success, < 0 on error
823 int consumer_send_relayd_streams_sent(uint64_t relayd_id
)
826 struct consumer_relayd_sock_pair
*relayd
;
828 assert(relayd_id
!= -1ULL);
830 /* The stream is not metadata. Get relayd reference if exists. */
832 relayd
= consumer_find_relayd(relayd_id
);
833 if (relayd
!= NULL
) {
834 /* Add stream on the relayd */
835 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
836 ret
= relayd_streams_sent(&relayd
->control_sock
);
837 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
839 ERR("Relayd streams sent failed. Cleaning up relayd %" PRIu64
".", relayd
->id
);
840 lttng_consumer_cleanup_relayd(relayd
);
844 ERR("Relayd ID %" PRIu64
" unknown. Can't send streams_sent.",
851 DBG("All streams sent relayd id %" PRIu64
, relayd_id
);
859 * Find a relayd and close the stream
861 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
863 struct consumer_relayd_sock_pair
*relayd
;
865 /* The stream is not metadata. Get relayd reference if exists. */
867 relayd
= consumer_find_relayd(stream
->relayd_id
);
869 consumer_stream_relayd_close(stream
, relayd
);
875 * Handle stream for relayd transmission if the stream applies for network
876 * streaming where the net sequence index is set.
878 * Return destination file descriptor or negative value on error.
880 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
881 size_t data_size
, unsigned long padding
,
882 struct consumer_relayd_sock_pair
*relayd
)
885 struct lttcomm_relayd_data_hdr data_hdr
;
891 /* Reset data header */
892 memset(&data_hdr
, 0, sizeof(data_hdr
));
894 if (stream
->metadata_flag
) {
895 /* Caller MUST acquire the relayd control socket lock */
896 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
901 /* Metadata are always sent on the control socket. */
902 outfd
= relayd
->control_sock
.sock
.fd
;
904 /* Set header with stream information */
905 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
906 data_hdr
.data_size
= htobe32(data_size
);
907 data_hdr
.padding_size
= htobe32(padding
);
909 * Note that net_seq_num below is assigned with the *current* value of
910 * next_net_seq_num and only after that the next_net_seq_num will be
911 * increment. This is why when issuing a command on the relayd using
912 * this next value, 1 should always be substracted in order to compare
913 * the last seen sequence number on the relayd side to the last sent.
915 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
916 /* Other fields are zeroed previously */
918 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
924 ++stream
->next_net_seq_num
;
926 /* Set to go on data socket */
927 outfd
= relayd
->data_sock
.sock
.fd
;
935 * Allocate and return a new lttng_consumer_channel object using the given key
936 * to initialize the hash table node.
938 * On error, return NULL.
940 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
942 const char *pathname
,
947 enum lttng_event_output output
,
948 uint64_t tracefile_size
,
949 uint64_t tracefile_count
,
950 uint64_t session_id_per_pid
,
951 unsigned int monitor
,
952 unsigned int live_timer_interval
,
953 const char *root_shm_path
,
954 const char *shm_path
)
956 struct lttng_consumer_channel
*channel
;
958 channel
= zmalloc(sizeof(*channel
));
959 if (channel
== NULL
) {
960 PERROR("malloc struct lttng_consumer_channel");
965 channel
->refcount
= 0;
966 channel
->session_id
= session_id
;
967 channel
->session_id_per_pid
= session_id_per_pid
;
970 channel
->relayd_id
= relayd_id
;
971 channel
->tracefile_size
= tracefile_size
;
972 channel
->tracefile_count
= tracefile_count
;
973 channel
->monitor
= monitor
;
974 channel
->live_timer_interval
= live_timer_interval
;
975 pthread_mutex_init(&channel
->lock
, NULL
);
976 pthread_mutex_init(&channel
->timer_lock
, NULL
);
979 case LTTNG_EVENT_SPLICE
:
980 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
982 case LTTNG_EVENT_MMAP
:
983 channel
->output
= CONSUMER_CHANNEL_MMAP
;
993 * In monitor mode, the streams associated with the channel will be put in
994 * a special list ONLY owned by this channel. So, the refcount is set to 1
995 * here meaning that the channel itself has streams that are referenced.
997 * On a channel deletion, once the channel is no longer visible, the
998 * refcount is decremented and checked for a zero value to delete it. With
999 * streams in no monitor mode, it will now be safe to destroy the channel.
1001 if (!channel
->monitor
) {
1002 channel
->refcount
= 1;
1005 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
1006 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
1008 strncpy(channel
->name
, name
, sizeof(channel
->name
));
1009 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
1011 if (root_shm_path
) {
1012 strncpy(channel
->root_shm_path
, root_shm_path
, sizeof(channel
->root_shm_path
));
1013 channel
->root_shm_path
[sizeof(channel
->root_shm_path
) - 1] = '\0';
1016 strncpy(channel
->shm_path
, shm_path
, sizeof(channel
->shm_path
));
1017 channel
->shm_path
[sizeof(channel
->shm_path
) - 1] = '\0';
1020 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
1022 channel
->wait_fd
= -1;
1024 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
1026 DBG("Allocated channel (key %" PRIu64
")", channel
->key
);
1033 * Add a channel to the global list protected by a mutex.
1035 * Always return 0 indicating success.
1037 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
1038 struct lttng_consumer_local_data
*ctx
)
1040 pthread_mutex_lock(&consumer_data
.lock
);
1041 pthread_mutex_lock(&channel
->lock
);
1042 pthread_mutex_lock(&channel
->timer_lock
);
1045 * This gives us a guarantee that the channel we are about to add to the
1046 * channel hash table will be unique. See this function comment on the why
1047 * we need to steel the channel key at this stage.
1049 steal_channel_key(channel
->key
);
1052 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
1055 pthread_mutex_unlock(&channel
->timer_lock
);
1056 pthread_mutex_unlock(&channel
->lock
);
1057 pthread_mutex_unlock(&consumer_data
.lock
);
1059 if (channel
->wait_fd
!= -1 && channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
1060 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
1067 * Allocate the pollfd structure and the local view of the out fds to avoid
1068 * doing a lookup in the linked list and concurrency issues when writing is
1069 * needed. Called with consumer_data.lock held.
1071 * Returns the number of fds in the structures.
1073 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
1074 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
1075 struct lttng_ht
*ht
, int *nb_inactive_fd
)
1078 struct lttng_ht_iter iter
;
1079 struct lttng_consumer_stream
*stream
;
1084 assert(local_stream
);
1086 DBG("Updating poll fd array");
1087 *nb_inactive_fd
= 0;
1089 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1091 * Only active streams with an active end point can be added to the
1092 * poll set and local stream storage of the thread.
1094 * There is a potential race here for endpoint_status to be updated
1095 * just after the check. However, this is OK since the stream(s) will
1096 * be deleted once the thread is notified that the end point state has
1097 * changed where this function will be called back again.
1099 * We track the number of inactive FDs because they still need to be
1100 * closed by the polling thread after a wakeup on the data_pipe or
1103 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
1104 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
1105 (*nb_inactive_fd
)++;
1109 * This clobbers way too much the debug output. Uncomment that if you
1110 * need it for debugging purposes.
1112 * DBG("Active FD %d", stream->wait_fd);
1114 (*pollfd
)[i
].fd
= stream
->wait_fd
;
1115 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1116 local_stream
[i
] = stream
;
1122 * Insert the consumer_data_pipe at the end of the array and don't
1123 * increment i so nb_fd is the number of real FD.
1125 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1126 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1128 (*pollfd
)[i
+ 1].fd
= lttng_pipe_get_readfd(ctx
->consumer_wakeup_pipe
);
1129 (*pollfd
)[i
+ 1].events
= POLLIN
| POLLPRI
;
1134 * Poll on the should_quit pipe and the command socket return -1 on
1135 * error, 1 if should exit, 0 if data is available on the command socket
1137 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1142 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1143 if (num_rdy
== -1) {
1145 * Restart interrupted system call.
1147 if (errno
== EINTR
) {
1150 PERROR("Poll error");
1153 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1154 DBG("consumer_should_quit wake up");
1161 * Set the error socket.
1163 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1166 ctx
->consumer_error_socket
= sock
;
1170 * Set the command socket path.
1172 void lttng_consumer_set_command_sock_path(
1173 struct lttng_consumer_local_data
*ctx
, char *sock
)
1175 ctx
->consumer_command_sock_path
= sock
;
1179 * Send return code to the session daemon.
1180 * If the socket is not defined, we return 0, it is not a fatal error
1182 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1184 if (ctx
->consumer_error_socket
> 0) {
1185 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1186 sizeof(enum lttcomm_sessiond_command
));
1193 * Close all the tracefiles and stream fds and MUST be called when all
1194 * instances are destroyed i.e. when all threads were joined and are ended.
1196 void lttng_consumer_cleanup(void)
1198 struct lttng_ht_iter iter
;
1199 struct lttng_consumer_channel
*channel
;
1203 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1205 consumer_del_channel(channel
);
1210 lttng_ht_destroy(consumer_data
.channel_ht
);
1212 cleanup_relayd_ht();
1214 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1217 * This HT contains streams that are freed by either the metadata thread or
1218 * the data thread so we do *nothing* on the hash table and simply destroy
1221 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1225 * Called from signal handler.
1227 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1232 ret
= lttng_write(ctx
->consumer_should_quit
[1], "4", 1);
1234 PERROR("write consumer quit");
1237 DBG("Consumer flag that it should quit");
1242 * Flush pending writes to trace output disk file.
1245 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1249 int outfd
= stream
->out_fd
;
1252 * This does a blocking write-and-wait on any page that belongs to the
1253 * subbuffer prior to the one we just wrote.
1254 * Don't care about error values, as these are just hints and ways to
1255 * limit the amount of page cache used.
1257 if (orig_offset
< stream
->max_sb_size
) {
1260 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1261 stream
->max_sb_size
,
1262 SYNC_FILE_RANGE_WAIT_BEFORE
1263 | SYNC_FILE_RANGE_WRITE
1264 | SYNC_FILE_RANGE_WAIT_AFTER
);
1266 * Give hints to the kernel about how we access the file:
1267 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1270 * We need to call fadvise again after the file grows because the
1271 * kernel does not seem to apply fadvise to non-existing parts of the
1274 * Call fadvise _after_ having waited for the page writeback to
1275 * complete because the dirty page writeback semantic is not well
1276 * defined. So it can be expected to lead to lower throughput in
1279 ret
= posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1280 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1281 if (ret
&& ret
!= -ENOSYS
) {
1283 PERROR("posix_fadvise on fd %i", outfd
);
1288 * Initialise the necessary environnement :
1289 * - create a new context
1290 * - create the poll_pipe
1291 * - create the should_quit pipe (for signal handler)
1292 * - create the thread pipe (for splice)
1294 * Takes a function pointer as argument, this function is called when data is
1295 * available on a buffer. This function is responsible to do the
1296 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1297 * buffer configuration and then kernctl_put_next_subbuf at the end.
1299 * Returns a pointer to the new context or NULL on error.
1301 struct lttng_consumer_local_data
*lttng_consumer_create(
1302 enum lttng_consumer_type type
,
1303 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1304 struct lttng_consumer_local_data
*ctx
),
1305 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1306 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1307 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1310 struct lttng_consumer_local_data
*ctx
;
1312 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1313 consumer_data
.type
== type
);
1314 consumer_data
.type
= type
;
1316 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1318 PERROR("allocating context");
1322 ctx
->consumer_error_socket
= -1;
1323 ctx
->consumer_metadata_socket
= -1;
1324 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1325 /* assign the callbacks */
1326 ctx
->on_buffer_ready
= buffer_ready
;
1327 ctx
->on_recv_channel
= recv_channel
;
1328 ctx
->on_recv_stream
= recv_stream
;
1329 ctx
->on_update_stream
= update_stream
;
1331 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1332 if (!ctx
->consumer_data_pipe
) {
1333 goto error_poll_pipe
;
1336 ctx
->consumer_wakeup_pipe
= lttng_pipe_open(0);
1337 if (!ctx
->consumer_wakeup_pipe
) {
1338 goto error_wakeup_pipe
;
1341 ret
= pipe(ctx
->consumer_should_quit
);
1343 PERROR("Error creating recv pipe");
1344 goto error_quit_pipe
;
1347 ret
= pipe(ctx
->consumer_channel_pipe
);
1349 PERROR("Error creating channel pipe");
1350 goto error_channel_pipe
;
1353 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1354 if (!ctx
->consumer_metadata_pipe
) {
1355 goto error_metadata_pipe
;
1360 error_metadata_pipe
:
1361 utils_close_pipe(ctx
->consumer_channel_pipe
);
1363 utils_close_pipe(ctx
->consumer_should_quit
);
1365 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1367 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1375 * Iterate over all streams of the hashtable and free them properly.
1377 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1379 struct lttng_ht_iter iter
;
1380 struct lttng_consumer_stream
*stream
;
1387 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1389 * Ignore return value since we are currently cleaning up so any error
1392 (void) consumer_del_stream(stream
, ht
);
1396 lttng_ht_destroy(ht
);
1400 * Iterate over all streams of the metadata hashtable and free them
1403 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1405 struct lttng_ht_iter iter
;
1406 struct lttng_consumer_stream
*stream
;
1413 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1415 * Ignore return value since we are currently cleaning up so any error
1418 (void) consumer_del_metadata_stream(stream
, ht
);
1422 lttng_ht_destroy(ht
);
1426 * Close all fds associated with the instance and free the context.
1428 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1432 DBG("Consumer destroying it. Closing everything.");
1438 destroy_data_stream_ht(data_ht
);
1439 destroy_metadata_stream_ht(metadata_ht
);
1441 ret
= close(ctx
->consumer_error_socket
);
1445 ret
= close(ctx
->consumer_metadata_socket
);
1449 utils_close_pipe(ctx
->consumer_channel_pipe
);
1450 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1451 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1452 lttng_pipe_destroy(ctx
->consumer_wakeup_pipe
);
1453 utils_close_pipe(ctx
->consumer_should_quit
);
1455 unlink(ctx
->consumer_command_sock_path
);
1460 * Write the metadata stream id on the specified file descriptor.
1462 static int write_relayd_metadata_id(int fd
,
1463 struct lttng_consumer_stream
*stream
,
1464 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1467 struct lttcomm_relayd_metadata_payload hdr
;
1469 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1470 hdr
.padding_size
= htobe32(padding
);
1471 ret
= lttng_write(fd
, (void *) &hdr
, sizeof(hdr
));
1472 if (ret
< sizeof(hdr
)) {
1474 * This error means that the fd's end is closed so ignore the PERROR
1475 * not to clubber the error output since this can happen in a normal
1478 if (errno
!= EPIPE
) {
1479 PERROR("write metadata stream id");
1481 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1483 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1484 * handle writting the missing part so report that as an error and
1485 * don't lie to the caller.
1490 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1491 stream
->relayd_stream_id
, padding
);
1498 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1499 * core function for writing trace buffers to either the local filesystem or
1502 * It must be called with the stream lock held.
1504 * Careful review MUST be put if any changes occur!
1506 * Returns the number of bytes written
1508 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1509 struct lttng_consumer_local_data
*ctx
,
1510 struct lttng_consumer_stream
*stream
, unsigned long len
,
1511 unsigned long padding
,
1512 struct ctf_packet_index
*index
)
1514 unsigned long mmap_offset
;
1517 off_t orig_offset
= stream
->out_fd_offset
;
1518 /* Default is on the disk */
1519 int outfd
= stream
->out_fd
;
1520 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1521 unsigned int relayd_hang_up
= 0;
1523 /* RCU lock for the relayd pointer */
1526 /* Flag that the current stream if set for network streaming. */
1527 if (stream
->relayd_id
!= (uint64_t) -1ULL) {
1528 relayd
= consumer_find_relayd(stream
->relayd_id
);
1529 if (relayd
== NULL
) {
1535 /* get the offset inside the fd to mmap */
1536 switch (consumer_data
.type
) {
1537 case LTTNG_CONSUMER_KERNEL
:
1538 mmap_base
= stream
->mmap_base
;
1539 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1541 PERROR("tracer ctl get_mmap_read_offset");
1545 case LTTNG_CONSUMER32_UST
:
1546 case LTTNG_CONSUMER64_UST
:
1547 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1549 ERR("read mmap get mmap base for stream %s", stream
->name
);
1553 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1555 PERROR("tracer ctl get_mmap_read_offset");
1561 ERR("Unknown consumer_data type");
1565 /* Handle stream on the relayd if the output is on the network */
1567 unsigned long netlen
= len
;
1570 * Lock the control socket for the complete duration of the function
1571 * since from this point on we will use the socket.
1573 if (stream
->metadata_flag
) {
1574 /* Metadata requires the control socket. */
1575 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1576 if (stream
->reset_metadata_flag
) {
1577 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1578 stream
->relayd_stream_id
,
1579 stream
->metadata_version
);
1584 stream
->reset_metadata_flag
= 0;
1586 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1589 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1594 /* Use the returned socket. */
1597 /* Write metadata stream id before payload */
1598 if (stream
->metadata_flag
) {
1599 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1606 /* No streaming, we have to set the len with the full padding */
1609 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1610 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1612 ERR("Reset metadata file");
1615 stream
->reset_metadata_flag
= 0;
1619 * Check if we need to change the tracefile before writing the packet.
1621 if (stream
->chan
->tracefile_size
> 0 &&
1622 (stream
->tracefile_size_current
+ len
) >
1623 stream
->chan
->tracefile_size
) {
1624 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1625 stream
->name
, stream
->chan
->tracefile_size
,
1626 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1627 stream
->out_fd
, &(stream
->tracefile_count_current
),
1630 ERR("Rotating output file");
1633 outfd
= stream
->out_fd
;
1635 if (stream
->index_file
) {
1636 lttng_index_file_put(stream
->index_file
);
1637 stream
->index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1638 stream
->name
, stream
->uid
, stream
->gid
,
1639 stream
->chan
->tracefile_size
,
1640 stream
->tracefile_count_current
,
1641 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1642 if (!stream
->index_file
) {
1647 /* Reset current size because we just perform a rotation. */
1648 stream
->tracefile_size_current
= 0;
1649 stream
->out_fd_offset
= 0;
1652 stream
->tracefile_size_current
+= len
;
1654 index
->offset
= htobe64(stream
->out_fd_offset
);
1659 * This call guarantee that len or less is returned. It's impossible to
1660 * receive a ret value that is bigger than len.
1662 ret
= lttng_write(outfd
, mmap_base
+ mmap_offset
, len
);
1663 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1664 if (ret
< 0 || ((size_t) ret
!= len
)) {
1666 * Report error to caller if nothing was written else at least send the
1674 /* Socket operation failed. We consider the relayd dead */
1675 if (errno
== EPIPE
|| errno
== EINVAL
|| errno
== EBADF
) {
1677 * This is possible if the fd is closed on the other side
1678 * (outfd) or any write problem. It can be verbose a bit for a
1679 * normal execution if for instance the relayd is stopped
1680 * abruptly. This can happen so set this to a DBG statement.
1682 DBG("Consumer mmap write detected relayd hang up");
1684 /* Unhandled error, print it and stop function right now. */
1685 PERROR("Error in write mmap (ret %zd != len %lu)", ret
, len
);
1689 stream
->output_written
+= ret
;
1691 /* This call is useless on a socket so better save a syscall. */
1693 /* This won't block, but will start writeout asynchronously */
1694 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, len
,
1695 SYNC_FILE_RANGE_WRITE
);
1696 stream
->out_fd_offset
+= len
;
1697 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1702 * This is a special case that the relayd has closed its socket. Let's
1703 * cleanup the relayd object and all associated streams.
1705 if (relayd
&& relayd_hang_up
) {
1706 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->id
);
1707 lttng_consumer_cleanup_relayd(relayd
);
1711 /* Unlock only if ctrl socket used */
1712 if (relayd
&& stream
->metadata_flag
) {
1713 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1721 * Splice the data from the ring buffer to the tracefile.
1723 * It must be called with the stream lock held.
1725 * Returns the number of bytes spliced.
1727 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1728 struct lttng_consumer_local_data
*ctx
,
1729 struct lttng_consumer_stream
*stream
, unsigned long len
,
1730 unsigned long padding
,
1731 struct ctf_packet_index
*index
)
1733 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1735 off_t orig_offset
= stream
->out_fd_offset
;
1736 int fd
= stream
->wait_fd
;
1737 /* Default is on the disk */
1738 int outfd
= stream
->out_fd
;
1739 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1741 unsigned int relayd_hang_up
= 0;
1743 switch (consumer_data
.type
) {
1744 case LTTNG_CONSUMER_KERNEL
:
1746 case LTTNG_CONSUMER32_UST
:
1747 case LTTNG_CONSUMER64_UST
:
1748 /* Not supported for user space tracing */
1751 ERR("Unknown consumer_data type");
1755 /* RCU lock for the relayd pointer */
1758 /* Flag that the current stream if set for network streaming. */
1759 if (stream
->relayd_id
!= (uint64_t) -1ULL) {
1760 relayd
= consumer_find_relayd(stream
->relayd_id
);
1761 if (relayd
== NULL
) {
1766 splice_pipe
= stream
->splice_pipe
;
1768 /* Write metadata stream id before payload */
1770 unsigned long total_len
= len
;
1772 if (stream
->metadata_flag
) {
1774 * Lock the control socket for the complete duration of the function
1775 * since from this point on we will use the socket.
1777 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1779 if (stream
->reset_metadata_flag
) {
1780 ret
= relayd_reset_metadata(&relayd
->control_sock
,
1781 stream
->relayd_stream_id
,
1782 stream
->metadata_version
);
1787 stream
->reset_metadata_flag
= 0;
1789 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1797 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1800 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1806 /* Use the returned socket. */
1809 /* No streaming, we have to set the len with the full padding */
1812 if (stream
->metadata_flag
&& stream
->reset_metadata_flag
) {
1813 ret
= utils_truncate_stream_file(stream
->out_fd
, 0);
1815 ERR("Reset metadata file");
1818 stream
->reset_metadata_flag
= 0;
1821 * Check if we need to change the tracefile before writing the packet.
1823 if (stream
->chan
->tracefile_size
> 0 &&
1824 (stream
->tracefile_size_current
+ len
) >
1825 stream
->chan
->tracefile_size
) {
1826 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1827 stream
->name
, stream
->chan
->tracefile_size
,
1828 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1829 stream
->out_fd
, &(stream
->tracefile_count_current
),
1833 ERR("Rotating output file");
1836 outfd
= stream
->out_fd
;
1838 if (stream
->index_file
) {
1839 lttng_index_file_put(stream
->index_file
);
1840 stream
->index_file
= lttng_index_file_create(stream
->chan
->pathname
,
1841 stream
->name
, stream
->uid
, stream
->gid
,
1842 stream
->chan
->tracefile_size
,
1843 stream
->tracefile_count_current
,
1844 CTF_INDEX_MAJOR
, CTF_INDEX_MINOR
);
1845 if (!stream
->index_file
) {
1850 /* Reset current size because we just perform a rotation. */
1851 stream
->tracefile_size_current
= 0;
1852 stream
->out_fd_offset
= 0;
1855 stream
->tracefile_size_current
+= len
;
1856 index
->offset
= htobe64(stream
->out_fd_offset
);
1860 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1861 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1862 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1863 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1864 DBG("splice chan to pipe, ret %zd", ret_splice
);
1865 if (ret_splice
< 0) {
1868 PERROR("Error in relay splice");
1872 /* Handle stream on the relayd if the output is on the network */
1873 if (relayd
&& stream
->metadata_flag
) {
1874 size_t metadata_payload_size
=
1875 sizeof(struct lttcomm_relayd_metadata_payload
);
1877 /* Update counter to fit the spliced data */
1878 ret_splice
+= metadata_payload_size
;
1879 len
+= metadata_payload_size
;
1881 * We do this so the return value can match the len passed as
1882 * argument to this function.
1884 written
-= metadata_payload_size
;
1887 /* Splice data out */
1888 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1889 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1890 DBG("Consumer splice pipe to file (out_fd: %d), ret %zd",
1892 if (ret_splice
< 0) {
1897 } else if (ret_splice
> len
) {
1899 * We don't expect this code path to be executed but you never know
1900 * so this is an extra protection agains a buggy splice().
1903 written
+= ret_splice
;
1904 PERROR("Wrote more data than requested %zd (len: %lu)", ret_splice
,
1908 /* All good, update current len and continue. */
1912 /* This call is useless on a socket so better save a syscall. */
1914 /* This won't block, but will start writeout asynchronously */
1915 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1916 SYNC_FILE_RANGE_WRITE
);
1917 stream
->out_fd_offset
+= ret_splice
;
1919 stream
->output_written
+= ret_splice
;
1920 written
+= ret_splice
;
1923 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1929 * This is a special case that the relayd has closed its socket. Let's
1930 * cleanup the relayd object and all associated streams.
1932 if (relayd
&& relayd_hang_up
) {
1933 ERR("Relayd hangup. Cleaning up relayd %" PRIu64
".", relayd
->id
);
1934 lttng_consumer_cleanup_relayd(relayd
);
1935 /* Skip splice error so the consumer does not fail */
1940 /* send the appropriate error description to sessiond */
1943 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1946 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1949 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1954 if (relayd
&& stream
->metadata_flag
) {
1955 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1963 * Take a snapshot for a specific fd
1965 * Returns 0 on success, < 0 on error
1967 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1969 switch (consumer_data
.type
) {
1970 case LTTNG_CONSUMER_KERNEL
:
1971 return lttng_kconsumer_take_snapshot(stream
);
1972 case LTTNG_CONSUMER32_UST
:
1973 case LTTNG_CONSUMER64_UST
:
1974 return lttng_ustconsumer_take_snapshot(stream
);
1976 ERR("Unknown consumer_data type");
1983 * Get the produced position
1985 * Returns 0 on success, < 0 on error
1987 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1990 switch (consumer_data
.type
) {
1991 case LTTNG_CONSUMER_KERNEL
:
1992 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1993 case LTTNG_CONSUMER32_UST
:
1994 case LTTNG_CONSUMER64_UST
:
1995 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1997 ERR("Unknown consumer_data type");
2003 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
2004 int sock
, struct pollfd
*consumer_sockpoll
)
2006 switch (consumer_data
.type
) {
2007 case LTTNG_CONSUMER_KERNEL
:
2008 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2009 case LTTNG_CONSUMER32_UST
:
2010 case LTTNG_CONSUMER64_UST
:
2011 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2013 ERR("Unknown consumer_data type");
2019 void lttng_consumer_close_all_metadata(void)
2021 switch (consumer_data
.type
) {
2022 case LTTNG_CONSUMER_KERNEL
:
2024 * The Kernel consumer has a different metadata scheme so we don't
2025 * close anything because the stream will be closed by the session
2029 case LTTNG_CONSUMER32_UST
:
2030 case LTTNG_CONSUMER64_UST
:
2032 * Close all metadata streams. The metadata hash table is passed and
2033 * this call iterates over it by closing all wakeup fd. This is safe
2034 * because at this point we are sure that the metadata producer is
2035 * either dead or blocked.
2037 lttng_ustconsumer_close_all_metadata(metadata_ht
);
2040 ERR("Unknown consumer_data type");
2046 * Clean up a metadata stream and free its memory.
2048 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
2049 struct lttng_ht
*ht
)
2051 struct lttng_consumer_channel
*free_chan
= NULL
;
2055 * This call should NEVER receive regular stream. It must always be
2056 * metadata stream and this is crucial for data structure synchronization.
2058 assert(stream
->metadata_flag
);
2060 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
2062 pthread_mutex_lock(&consumer_data
.lock
);
2063 pthread_mutex_lock(&stream
->chan
->lock
);
2064 pthread_mutex_lock(&stream
->lock
);
2065 if (stream
->chan
->metadata_cache
) {
2066 /* Only applicable to userspace consumers. */
2067 pthread_mutex_lock(&stream
->chan
->metadata_cache
->lock
);
2070 /* Remove any reference to that stream. */
2071 consumer_stream_delete(stream
, ht
);
2073 /* Close down everything including the relayd if one. */
2074 consumer_stream_close(stream
);
2075 /* Destroy tracer buffers of the stream. */
2076 consumer_stream_destroy_buffers(stream
);
2078 /* Atomically decrement channel refcount since other threads can use it. */
2079 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
2080 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
2081 /* Go for channel deletion! */
2082 free_chan
= stream
->chan
;
2086 * Nullify the stream reference so it is not used after deletion. The
2087 * channel lock MUST be acquired before being able to check for a NULL
2090 stream
->chan
->metadata_stream
= NULL
;
2092 if (stream
->chan
->metadata_cache
) {
2093 pthread_mutex_unlock(&stream
->chan
->metadata_cache
->lock
);
2095 pthread_mutex_unlock(&stream
->lock
);
2096 pthread_mutex_unlock(&stream
->chan
->lock
);
2097 pthread_mutex_unlock(&consumer_data
.lock
);
2100 consumer_del_channel(free_chan
);
2103 consumer_stream_free(stream
);
2107 * Action done with the metadata stream when adding it to the consumer internal
2108 * data structures to handle it.
2110 int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2112 struct lttng_ht
*ht
= metadata_ht
;
2114 struct lttng_ht_iter iter
;
2115 struct lttng_ht_node_u64
*node
;
2120 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2122 pthread_mutex_lock(&consumer_data
.lock
);
2123 pthread_mutex_lock(&stream
->chan
->lock
);
2124 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2125 pthread_mutex_lock(&stream
->lock
);
2128 * From here, refcounts are updated so be _careful_ when returning an error
2135 * Lookup the stream just to make sure it does not exist in our internal
2136 * state. This should NEVER happen.
2138 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2139 node
= lttng_ht_iter_get_node_u64(&iter
);
2143 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2144 * in terms of destroying the associated channel, because the action that
2145 * causes the count to become 0 also causes a stream to be added. The
2146 * channel deletion will thus be triggered by the following removal of this
2149 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2150 /* Increment refcount before decrementing nb_init_stream_left */
2152 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2155 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2157 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
2158 &stream
->node_channel_id
);
2161 * Add stream to the stream_list_ht of the consumer data. No need to steal
2162 * the key since the HT does not use it and we allow to add redundant keys
2165 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2169 pthread_mutex_unlock(&stream
->lock
);
2170 pthread_mutex_unlock(&stream
->chan
->lock
);
2171 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2172 pthread_mutex_unlock(&consumer_data
.lock
);
2177 * Delete data stream that are flagged for deletion (endpoint_status).
2179 static void validate_endpoint_status_data_stream(void)
2181 struct lttng_ht_iter iter
;
2182 struct lttng_consumer_stream
*stream
;
2184 DBG("Consumer delete flagged data stream");
2187 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2188 /* Validate delete flag of the stream */
2189 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2192 /* Delete it right now */
2193 consumer_del_stream(stream
, data_ht
);
2199 * Delete metadata stream that are flagged for deletion (endpoint_status).
2201 static void validate_endpoint_status_metadata_stream(
2202 struct lttng_poll_event
*pollset
)
2204 struct lttng_ht_iter iter
;
2205 struct lttng_consumer_stream
*stream
;
2207 DBG("Consumer delete flagged metadata stream");
2212 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2213 /* Validate delete flag of the stream */
2214 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2218 * Remove from pollset so the metadata thread can continue without
2219 * blocking on a deleted stream.
2221 lttng_poll_del(pollset
, stream
->wait_fd
);
2223 /* Delete it right now */
2224 consumer_del_metadata_stream(stream
, metadata_ht
);
2230 * Thread polls on metadata file descriptor and write them on disk or on the
2233 void *consumer_thread_metadata_poll(void *data
)
2235 int ret
, i
, pollfd
, err
= -1;
2236 uint32_t revents
, nb_fd
;
2237 struct lttng_consumer_stream
*stream
= NULL
;
2238 struct lttng_ht_iter iter
;
2239 struct lttng_ht_node_u64
*node
;
2240 struct lttng_poll_event events
;
2241 struct lttng_consumer_local_data
*ctx
= data
;
2244 rcu_register_thread();
2246 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_METADATA
);
2248 if (testpoint(consumerd_thread_metadata
)) {
2249 goto error_testpoint
;
2252 health_code_update();
2254 DBG("Thread metadata poll started");
2256 /* Size is set to 1 for the consumer_metadata pipe */
2257 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2259 ERR("Poll set creation failed");
2263 ret
= lttng_poll_add(&events
,
2264 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2270 DBG("Metadata main loop started");
2274 health_code_update();
2275 health_poll_entry();
2276 DBG("Metadata poll wait");
2277 ret
= lttng_poll_wait(&events
, -1);
2278 DBG("Metadata poll return from wait with %d fd(s)",
2279 LTTNG_POLL_GETNB(&events
));
2281 DBG("Metadata event caught in thread");
2283 if (errno
== EINTR
) {
2284 ERR("Poll EINTR caught");
2287 if (LTTNG_POLL_GETNB(&events
) == 0) {
2288 err
= 0; /* All is OK */
2295 /* From here, the event is a metadata wait fd */
2296 for (i
= 0; i
< nb_fd
; i
++) {
2297 health_code_update();
2299 revents
= LTTNG_POLL_GETEV(&events
, i
);
2300 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2303 /* No activity for this FD (poll implementation). */
2307 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2308 if (revents
& LPOLLIN
) {
2311 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2312 &stream
, sizeof(stream
));
2313 if (pipe_len
< sizeof(stream
)) {
2315 PERROR("read metadata stream");
2318 * Remove the pipe from the poll set and continue the loop
2319 * since their might be data to consume.
2321 lttng_poll_del(&events
,
2322 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2323 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2327 /* A NULL stream means that the state has changed. */
2328 if (stream
== NULL
) {
2329 /* Check for deleted streams. */
2330 validate_endpoint_status_metadata_stream(&events
);
2334 DBG("Adding metadata stream %d to poll set",
2337 /* Add metadata stream to the global poll events list */
2338 lttng_poll_add(&events
, stream
->wait_fd
,
2339 LPOLLIN
| LPOLLPRI
| LPOLLHUP
);
2340 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2341 DBG("Metadata thread pipe hung up");
2343 * Remove the pipe from the poll set and continue the loop
2344 * since their might be data to consume.
2346 lttng_poll_del(&events
,
2347 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2348 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2351 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2355 /* Handle other stream */
2361 uint64_t tmp_id
= (uint64_t) pollfd
;
2363 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2365 node
= lttng_ht_iter_get_node_u64(&iter
);
2368 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2371 if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2372 /* Get the data out of the metadata file descriptor */
2373 DBG("Metadata available on fd %d", pollfd
);
2374 assert(stream
->wait_fd
== pollfd
);
2377 health_code_update();
2379 len
= ctx
->on_buffer_ready(stream
, ctx
);
2381 * We don't check the return value here since if we get
2382 * a negative len, it means an error occurred thus we
2383 * simply remove it from the poll set and free the
2388 /* It's ok to have an unavailable sub-buffer */
2389 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2390 /* Clean up stream from consumer and free it. */
2391 lttng_poll_del(&events
, stream
->wait_fd
);
2392 consumer_del_metadata_stream(stream
, metadata_ht
);
2394 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2395 DBG("Metadata fd %d is hup|err.", pollfd
);
2396 if (!stream
->hangup_flush_done
2397 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2398 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2399 DBG("Attempting to flush and consume the UST buffers");
2400 lttng_ustconsumer_on_stream_hangup(stream
);
2402 /* We just flushed the stream now read it. */
2404 health_code_update();
2406 len
= ctx
->on_buffer_ready(stream
, ctx
);
2408 * We don't check the return value here since if we get
2409 * a negative len, it means an error occurred thus we
2410 * simply remove it from the poll set and free the
2416 lttng_poll_del(&events
, stream
->wait_fd
);
2418 * This call update the channel states, closes file descriptors
2419 * and securely free the stream.
2421 consumer_del_metadata_stream(stream
, metadata_ht
);
2423 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2427 /* Release RCU lock for the stream looked up */
2435 DBG("Metadata poll thread exiting");
2437 lttng_poll_clean(&events
);
2442 ERR("Health error occurred in %s", __func__
);
2444 health_unregister(health_consumerd
);
2445 rcu_unregister_thread();
2450 * This thread polls the fds in the set to consume the data and write
2451 * it to tracefile if necessary.
2453 void *consumer_thread_data_poll(void *data
)
2455 int num_rdy
, num_hup
, high_prio
, ret
, i
, err
= -1;
2456 struct pollfd
*pollfd
= NULL
;
2457 /* local view of the streams */
2458 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2459 /* local view of consumer_data.fds_count */
2461 /* Number of FDs with CONSUMER_ENDPOINT_INACTIVE but still open. */
2462 int nb_inactive_fd
= 0;
2463 struct lttng_consumer_local_data
*ctx
= data
;
2466 rcu_register_thread();
2468 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_DATA
);
2470 if (testpoint(consumerd_thread_data
)) {
2471 goto error_testpoint
;
2474 health_code_update();
2476 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2477 if (local_stream
== NULL
) {
2478 PERROR("local_stream malloc");
2483 health_code_update();
2489 * the fds set has been updated, we need to update our
2490 * local array as well
2492 pthread_mutex_lock(&consumer_data
.lock
);
2493 if (consumer_data
.need_update
) {
2498 local_stream
= NULL
;
2501 * Allocate for all fds +1 for the consumer_data_pipe and +1 for
2504 pollfd
= zmalloc((consumer_data
.stream_count
+ 2) * sizeof(struct pollfd
));
2505 if (pollfd
== NULL
) {
2506 PERROR("pollfd malloc");
2507 pthread_mutex_unlock(&consumer_data
.lock
);
2511 local_stream
= zmalloc((consumer_data
.stream_count
+ 2) *
2512 sizeof(struct lttng_consumer_stream
*));
2513 if (local_stream
== NULL
) {
2514 PERROR("local_stream malloc");
2515 pthread_mutex_unlock(&consumer_data
.lock
);
2518 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2519 data_ht
, &nb_inactive_fd
);
2521 ERR("Error in allocating pollfd or local_outfds");
2522 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2523 pthread_mutex_unlock(&consumer_data
.lock
);
2527 consumer_data
.need_update
= 0;
2529 pthread_mutex_unlock(&consumer_data
.lock
);
2531 /* No FDs and consumer_quit, consumer_cleanup the thread */
2532 if (nb_fd
== 0 && consumer_quit
== 1 && nb_inactive_fd
== 0) {
2533 err
= 0; /* All is OK */
2536 /* poll on the array of fds */
2538 DBG("polling on %d fd", nb_fd
+ 2);
2539 health_poll_entry();
2540 num_rdy
= poll(pollfd
, nb_fd
+ 2, -1);
2542 DBG("poll num_rdy : %d", num_rdy
);
2543 if (num_rdy
== -1) {
2545 * Restart interrupted system call.
2547 if (errno
== EINTR
) {
2550 PERROR("Poll error");
2551 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2553 } else if (num_rdy
== 0) {
2554 DBG("Polling thread timed out");
2559 * If the consumer_data_pipe triggered poll go directly to the
2560 * beginning of the loop to update the array. We want to prioritize
2561 * array update over low-priority reads.
2563 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2564 ssize_t pipe_readlen
;
2566 DBG("consumer_data_pipe wake up");
2567 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2568 &new_stream
, sizeof(new_stream
));
2569 if (pipe_readlen
< sizeof(new_stream
)) {
2570 PERROR("Consumer data pipe");
2571 /* Continue so we can at least handle the current stream(s). */
2576 * If the stream is NULL, just ignore it. It's also possible that
2577 * the sessiond poll thread changed the consumer_quit state and is
2578 * waking us up to test it.
2580 if (new_stream
== NULL
) {
2581 validate_endpoint_status_data_stream();
2585 /* Continue to update the local streams and handle prio ones */
2589 /* Handle wakeup pipe. */
2590 if (pollfd
[nb_fd
+ 1].revents
& (POLLIN
| POLLPRI
)) {
2592 ssize_t pipe_readlen
;
2594 pipe_readlen
= lttng_pipe_read(ctx
->consumer_wakeup_pipe
, &dummy
,
2596 if (pipe_readlen
< 0) {
2597 PERROR("Consumer data wakeup pipe");
2599 /* We've been awakened to handle stream(s). */
2600 ctx
->has_wakeup
= 0;
2603 /* Take care of high priority channels first. */
2604 for (i
= 0; i
< nb_fd
; i
++) {
2605 health_code_update();
2607 if (local_stream
[i
] == NULL
) {
2610 if (pollfd
[i
].revents
& POLLPRI
) {
2611 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2613 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2614 /* it's ok to have an unavailable sub-buffer */
2615 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2616 /* Clean the stream and free it. */
2617 consumer_del_stream(local_stream
[i
], data_ht
);
2618 local_stream
[i
] = NULL
;
2619 } else if (len
> 0) {
2620 local_stream
[i
]->data_read
= 1;
2626 * If we read high prio channel in this loop, try again
2627 * for more high prio data.
2633 /* Take care of low priority channels. */
2634 for (i
= 0; i
< nb_fd
; i
++) {
2635 health_code_update();
2637 if (local_stream
[i
] == NULL
) {
2640 if ((pollfd
[i
].revents
& POLLIN
) ||
2641 local_stream
[i
]->hangup_flush_done
||
2642 local_stream
[i
]->has_data
) {
2643 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2644 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2645 /* it's ok to have an unavailable sub-buffer */
2646 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2647 /* Clean the stream and free it. */
2648 consumer_del_stream(local_stream
[i
], data_ht
);
2649 local_stream
[i
] = NULL
;
2650 } else if (len
> 0) {
2651 local_stream
[i
]->data_read
= 1;
2656 /* Handle hangup and errors */
2657 for (i
= 0; i
< nb_fd
; i
++) {
2658 health_code_update();
2660 if (local_stream
[i
] == NULL
) {
2663 if (!local_stream
[i
]->hangup_flush_done
2664 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2665 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2666 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2667 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2669 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2670 /* Attempt read again, for the data we just flushed. */
2671 local_stream
[i
]->data_read
= 1;
2674 * If the poll flag is HUP/ERR/NVAL and we have
2675 * read no data in this pass, we can remove the
2676 * stream from its hash table.
2678 if ((pollfd
[i
].revents
& POLLHUP
)) {
2679 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2680 if (!local_stream
[i
]->data_read
) {
2681 consumer_del_stream(local_stream
[i
], data_ht
);
2682 local_stream
[i
] = NULL
;
2685 } else if (pollfd
[i
].revents
& POLLERR
) {
2686 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2687 if (!local_stream
[i
]->data_read
) {
2688 consumer_del_stream(local_stream
[i
], data_ht
);
2689 local_stream
[i
] = NULL
;
2692 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2693 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2694 if (!local_stream
[i
]->data_read
) {
2695 consumer_del_stream(local_stream
[i
], data_ht
);
2696 local_stream
[i
] = NULL
;
2700 if (local_stream
[i
] != NULL
) {
2701 local_stream
[i
]->data_read
= 0;
2708 DBG("polling thread exiting");
2713 * Close the write side of the pipe so epoll_wait() in
2714 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2715 * read side of the pipe. If we close them both, epoll_wait strangely does
2716 * not return and could create a endless wait period if the pipe is the
2717 * only tracked fd in the poll set. The thread will take care of closing
2720 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2725 ERR("Health error occurred in %s", __func__
);
2727 health_unregister(health_consumerd
);
2729 rcu_unregister_thread();
2734 * Close wake-up end of each stream belonging to the channel. This will
2735 * allow the poll() on the stream read-side to detect when the
2736 * write-side (application) finally closes them.
2739 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2741 struct lttng_ht
*ht
;
2742 struct lttng_consumer_stream
*stream
;
2743 struct lttng_ht_iter iter
;
2745 ht
= consumer_data
.stream_per_chan_id_ht
;
2748 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2749 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2750 ht
->match_fct
, &channel
->key
,
2751 &iter
.iter
, stream
, node_channel_id
.node
) {
2753 * Protect against teardown with mutex.
2755 pthread_mutex_lock(&stream
->lock
);
2756 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2759 switch (consumer_data
.type
) {
2760 case LTTNG_CONSUMER_KERNEL
:
2762 case LTTNG_CONSUMER32_UST
:
2763 case LTTNG_CONSUMER64_UST
:
2764 if (stream
->metadata_flag
) {
2765 /* Safe and protected by the stream lock. */
2766 lttng_ustconsumer_close_metadata(stream
->chan
);
2769 * Note: a mutex is taken internally within
2770 * liblttng-ust-ctl to protect timer wakeup_fd
2771 * use from concurrent close.
2773 lttng_ustconsumer_close_stream_wakeup(stream
);
2777 ERR("Unknown consumer_data type");
2781 pthread_mutex_unlock(&stream
->lock
);
2786 static void destroy_channel_ht(struct lttng_ht
*ht
)
2788 struct lttng_ht_iter iter
;
2789 struct lttng_consumer_channel
*channel
;
2797 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2798 ret
= lttng_ht_del(ht
, &iter
);
2803 lttng_ht_destroy(ht
);
2807 * This thread polls the channel fds to detect when they are being
2808 * closed. It closes all related streams if the channel is detected as
2809 * closed. It is currently only used as a shim layer for UST because the
2810 * consumerd needs to keep the per-stream wakeup end of pipes open for
2813 void *consumer_thread_channel_poll(void *data
)
2815 int ret
, i
, pollfd
, err
= -1;
2816 uint32_t revents
, nb_fd
;
2817 struct lttng_consumer_channel
*chan
= NULL
;
2818 struct lttng_ht_iter iter
;
2819 struct lttng_ht_node_u64
*node
;
2820 struct lttng_poll_event events
;
2821 struct lttng_consumer_local_data
*ctx
= data
;
2822 struct lttng_ht
*channel_ht
;
2824 rcu_register_thread();
2826 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_CHANNEL
);
2828 if (testpoint(consumerd_thread_channel
)) {
2829 goto error_testpoint
;
2832 health_code_update();
2834 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2836 /* ENOMEM at this point. Better to bail out. */
2840 DBG("Thread channel poll started");
2842 /* Size is set to 1 for the consumer_channel pipe */
2843 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2845 ERR("Poll set creation failed");
2849 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2855 DBG("Channel main loop started");
2859 health_code_update();
2860 DBG("Channel poll wait");
2861 health_poll_entry();
2862 ret
= lttng_poll_wait(&events
, -1);
2863 DBG("Channel poll return from wait with %d fd(s)",
2864 LTTNG_POLL_GETNB(&events
));
2866 DBG("Channel event caught in thread");
2868 if (errno
== EINTR
) {
2869 ERR("Poll EINTR caught");
2872 if (LTTNG_POLL_GETNB(&events
) == 0) {
2873 err
= 0; /* All is OK */
2880 /* From here, the event is a channel wait fd */
2881 for (i
= 0; i
< nb_fd
; i
++) {
2882 health_code_update();
2884 revents
= LTTNG_POLL_GETEV(&events
, i
);
2885 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2888 /* No activity for this FD (poll implementation). */
2892 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2893 if (revents
& LPOLLIN
) {
2894 enum consumer_channel_action action
;
2897 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2900 ERR("Error reading channel pipe");
2902 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2907 case CONSUMER_CHANNEL_ADD
:
2908 DBG("Adding channel %d to poll set",
2911 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2914 lttng_ht_add_unique_u64(channel_ht
,
2915 &chan
->wait_fd_node
);
2917 /* Add channel to the global poll events list */
2918 lttng_poll_add(&events
, chan
->wait_fd
,
2919 LPOLLERR
| LPOLLHUP
);
2921 case CONSUMER_CHANNEL_DEL
:
2924 * This command should never be called if the channel
2925 * has streams monitored by either the data or metadata
2926 * thread. The consumer only notify this thread with a
2927 * channel del. command if it receives a destroy
2928 * channel command from the session daemon that send it
2929 * if a command prior to the GET_CHANNEL failed.
2933 chan
= consumer_find_channel(key
);
2936 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2939 lttng_poll_del(&events
, chan
->wait_fd
);
2940 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2941 ret
= lttng_ht_del(channel_ht
, &iter
);
2944 switch (consumer_data
.type
) {
2945 case LTTNG_CONSUMER_KERNEL
:
2947 case LTTNG_CONSUMER32_UST
:
2948 case LTTNG_CONSUMER64_UST
:
2949 health_code_update();
2950 /* Destroy streams that might have been left in the stream list. */
2951 clean_channel_stream_list(chan
);
2954 ERR("Unknown consumer_data type");
2959 * Release our own refcount. Force channel deletion even if
2960 * streams were not initialized.
2962 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2963 consumer_del_channel(chan
);
2968 case CONSUMER_CHANNEL_QUIT
:
2970 * Remove the pipe from the poll set and continue the loop
2971 * since their might be data to consume.
2973 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2976 ERR("Unknown action");
2979 } else if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2980 DBG("Channel thread pipe hung up");
2982 * Remove the pipe from the poll set and continue the loop
2983 * since their might be data to consume.
2985 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2988 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
2992 /* Handle other stream */
2998 uint64_t tmp_id
= (uint64_t) pollfd
;
3000 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
3002 node
= lttng_ht_iter_get_node_u64(&iter
);
3005 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
3008 /* Check for error event */
3009 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
3010 DBG("Channel fd %d is hup|err.", pollfd
);
3012 lttng_poll_del(&events
, chan
->wait_fd
);
3013 ret
= lttng_ht_del(channel_ht
, &iter
);
3017 * This will close the wait fd for each stream associated to
3018 * this channel AND monitored by the data/metadata thread thus
3019 * will be clean by the right thread.
3021 consumer_close_channel_streams(chan
);
3023 /* Release our own refcount */
3024 if (!uatomic_sub_return(&chan
->refcount
, 1)
3025 && !uatomic_read(&chan
->nb_init_stream_left
)) {
3026 consumer_del_channel(chan
);
3029 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
3034 /* Release RCU lock for the channel looked up */
3042 lttng_poll_clean(&events
);
3044 destroy_channel_ht(channel_ht
);
3047 DBG("Channel poll thread exiting");
3050 ERR("Health error occurred in %s", __func__
);
3052 health_unregister(health_consumerd
);
3053 rcu_unregister_thread();
3057 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
3058 struct pollfd
*sockpoll
, int client_socket
)
3065 ret
= lttng_consumer_poll_socket(sockpoll
);
3069 DBG("Metadata connection on client_socket");
3071 /* Blocking call, waiting for transmission */
3072 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
3073 if (ctx
->consumer_metadata_socket
< 0) {
3074 WARN("On accept metadata");
3085 * This thread listens on the consumerd socket and receives the file
3086 * descriptors from the session daemon.
3088 void *consumer_thread_sessiond_poll(void *data
)
3090 int sock
= -1, client_socket
, ret
, err
= -1;
3092 * structure to poll for incoming data on communication socket avoids
3093 * making blocking sockets.
3095 struct pollfd consumer_sockpoll
[2];
3096 struct lttng_consumer_local_data
*ctx
= data
;
3098 rcu_register_thread();
3100 health_register(health_consumerd
, HEALTH_CONSUMERD_TYPE_SESSIOND
);
3102 if (testpoint(consumerd_thread_sessiond
)) {
3103 goto error_testpoint
;
3106 health_code_update();
3108 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
3109 unlink(ctx
->consumer_command_sock_path
);
3110 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
3111 if (client_socket
< 0) {
3112 ERR("Cannot create command socket");
3116 ret
= lttcomm_listen_unix_sock(client_socket
);
3121 DBG("Sending ready command to lttng-sessiond");
3122 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
3123 /* return < 0 on error, but == 0 is not fatal */
3125 ERR("Error sending ready command to lttng-sessiond");
3129 /* prepare the FDs to poll : to client socket and the should_quit pipe */
3130 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
3131 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
3132 consumer_sockpoll
[1].fd
= client_socket
;
3133 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3135 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3143 DBG("Connection on client_socket");
3145 /* Blocking call, waiting for transmission */
3146 sock
= lttcomm_accept_unix_sock(client_socket
);
3153 * Setup metadata socket which is the second socket connection on the
3154 * command unix socket.
3156 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
3165 /* This socket is not useful anymore. */
3166 ret
= close(client_socket
);
3168 PERROR("close client_socket");
3172 /* update the polling structure to poll on the established socket */
3173 consumer_sockpoll
[1].fd
= sock
;
3174 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
3177 health_code_update();
3179 health_poll_entry();
3180 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3189 DBG("Incoming command on sock");
3190 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
3193 * This could simply be a session daemon quitting. Don't output
3196 DBG("Communication interrupted on command socket");
3200 if (consumer_quit
) {
3201 DBG("consumer_thread_receive_fds received quit from signal");
3202 err
= 0; /* All is OK */
3205 DBG("received command on sock");
3211 DBG("Consumer thread sessiond poll exiting");
3214 * Close metadata streams since the producer is the session daemon which
3217 * NOTE: for now, this only applies to the UST tracer.
3219 lttng_consumer_close_all_metadata();
3222 * when all fds have hung up, the polling thread
3228 * Notify the data poll thread to poll back again and test the
3229 * consumer_quit state that we just set so to quit gracefully.
3231 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3233 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3235 notify_health_quit_pipe(health_quit_pipe
);
3237 /* Cleaning up possibly open sockets. */
3241 PERROR("close sock sessiond poll");
3244 if (client_socket
>= 0) {
3245 ret
= close(client_socket
);
3247 PERROR("close client_socket sessiond poll");
3254 ERR("Health error occurred in %s", __func__
);
3256 health_unregister(health_consumerd
);
3258 rcu_unregister_thread();
3262 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3263 struct lttng_consumer_local_data
*ctx
)
3267 pthread_mutex_lock(&stream
->lock
);
3268 if (stream
->metadata_flag
) {
3269 pthread_mutex_lock(&stream
->metadata_rdv_lock
);
3272 switch (consumer_data
.type
) {
3273 case LTTNG_CONSUMER_KERNEL
:
3274 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3276 case LTTNG_CONSUMER32_UST
:
3277 case LTTNG_CONSUMER64_UST
:
3278 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3281 ERR("Unknown consumer_data type");
3287 if (stream
->metadata_flag
) {
3288 pthread_cond_broadcast(&stream
->metadata_rdv
);
3289 pthread_mutex_unlock(&stream
->metadata_rdv_lock
);
3291 pthread_mutex_unlock(&stream
->lock
);
3295 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3297 switch (consumer_data
.type
) {
3298 case LTTNG_CONSUMER_KERNEL
:
3299 return lttng_kconsumer_on_recv_stream(stream
);
3300 case LTTNG_CONSUMER32_UST
:
3301 case LTTNG_CONSUMER64_UST
:
3302 return lttng_ustconsumer_on_recv_stream(stream
);
3304 ERR("Unknown consumer_data type");
3311 * Allocate and set consumer data hash tables.
3313 int lttng_consumer_init(void)
3315 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3316 if (!consumer_data
.channel_ht
) {
3320 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3321 if (!consumer_data
.relayd_ht
) {
3325 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3326 if (!consumer_data
.stream_list_ht
) {
3330 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3331 if (!consumer_data
.stream_per_chan_id_ht
) {
3335 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3340 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3352 * Process the ADD_RELAYD command receive by a consumer.
3354 * This will create a relayd socket pair and add it to the relayd hash table.
3355 * The caller MUST acquire a RCU read side lock before calling it.
3357 void consumer_add_relayd_socket(uint64_t relayd_id
, int sock_type
,
3358 struct lttng_consumer_local_data
*ctx
, int sock
,
3359 struct pollfd
*consumer_sockpoll
,
3360 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3361 uint64_t relayd_session_id
)
3363 int fd
= -1, ret
= -1, relayd_created
= 0;
3364 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3365 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3368 assert(relayd_sock
);
3370 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", relayd_id
);
3372 /* Get relayd reference if exists. */
3373 relayd
= consumer_find_relayd(relayd_id
);
3374 if (relayd
== NULL
) {
3375 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3376 /* Not found. Allocate one. */
3377 relayd
= consumer_allocate_relayd_sock_pair(relayd_id
);
3378 if (relayd
== NULL
) {
3380 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3383 relayd
->sessiond_session_id
= sessiond_id
;
3388 * This code path MUST continue to the consumer send status message to
3389 * we can notify the session daemon and continue our work without
3390 * killing everything.
3394 * relayd key should never be found for control socket.
3396 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3399 /* First send a status message before receiving the fds. */
3400 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3402 /* Somehow, the session daemon is not responding anymore. */
3403 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3404 goto error_nosignal
;
3407 /* Poll on consumer socket. */
3408 ret
= lttng_consumer_poll_socket(consumer_sockpoll
);
3410 /* Needing to exit in the middle of a command: error. */
3411 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3413 goto error_nosignal
;
3416 /* Get relayd socket from session daemon */
3417 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3418 if (ret
!= sizeof(fd
)) {
3420 fd
= -1; /* Just in case it gets set with an invalid value. */
3423 * Failing to receive FDs might indicate a major problem such as
3424 * reaching a fd limit during the receive where the kernel returns a
3425 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3426 * don't take any chances and stop everything.
3428 * XXX: Feature request #558 will fix that and avoid this possible
3429 * issue when reaching the fd limit.
3431 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3432 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3436 /* Copy socket information and received FD */
3437 switch (sock_type
) {
3438 case LTTNG_STREAM_CONTROL
:
3439 /* Copy received lttcomm socket */
3440 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3441 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3442 /* Handle create_sock error. */
3444 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3448 * Close the socket created internally by
3449 * lttcomm_create_sock, so we can replace it by the one
3450 * received from sessiond.
3452 if (close(relayd
->control_sock
.sock
.fd
)) {
3456 /* Assign new file descriptor */
3457 relayd
->control_sock
.sock
.fd
= fd
;
3458 fd
= -1; /* For error path */
3459 /* Assign version values. */
3460 relayd
->control_sock
.major
= relayd_sock
->major
;
3461 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3463 relayd
->relayd_session_id
= relayd_session_id
;
3466 case LTTNG_STREAM_DATA
:
3467 /* Copy received lttcomm socket */
3468 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3469 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3470 /* Handle create_sock error. */
3472 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3476 * Close the socket created internally by
3477 * lttcomm_create_sock, so we can replace it by the one
3478 * received from sessiond.
3480 if (close(relayd
->data_sock
.sock
.fd
)) {
3484 /* Assign new file descriptor */
3485 relayd
->data_sock
.sock
.fd
= fd
;
3486 fd
= -1; /* for eventual error paths */
3487 /* Assign version values. */
3488 relayd
->data_sock
.major
= relayd_sock
->major
;
3489 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3492 ERR("Unknown relayd socket type (%d)", sock_type
);
3494 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3498 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3499 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3502 /* We successfully added the socket. Send status back. */
3503 ret
= consumer_send_status_msg(sock
, ret_code
);
3505 /* Somehow, the session daemon is not responding anymore. */
3506 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3507 goto error_nosignal
;
3511 * Add relayd socket pair to consumer data hashtable. If object already
3512 * exists or on error, the function gracefully returns.
3521 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3522 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3526 /* Close received socket if valid. */
3529 PERROR("close received socket");
3533 if (relayd_created
) {
3539 * Search for a relayd associated to the session id and return the reference.
3541 * A rcu read side lock MUST be acquire before calling this function and locked
3542 * until the relayd object is no longer necessary.
3544 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3546 struct lttng_ht_iter iter
;
3547 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3549 /* Iterate over all relayd since they are indexed by relayd_id. */
3550 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3553 * Check by sessiond id which is unique here where the relayd session
3554 * id might not be when having multiple relayd.
3556 if (relayd
->sessiond_session_id
== id
) {
3557 /* Found the relayd. There can be only one per id. */
3569 * Check if for a given session id there is still data needed to be extract
3572 * Return 1 if data is pending or else 0 meaning ready to be read.
3574 int consumer_data_pending(uint64_t id
)
3577 struct lttng_ht_iter iter
;
3578 struct lttng_ht
*ht
;
3579 struct lttng_consumer_stream
*stream
;
3580 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3581 int (*data_pending
)(struct lttng_consumer_stream
*);
3583 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3586 pthread_mutex_lock(&consumer_data
.lock
);
3588 switch (consumer_data
.type
) {
3589 case LTTNG_CONSUMER_KERNEL
:
3590 data_pending
= lttng_kconsumer_data_pending
;
3592 case LTTNG_CONSUMER32_UST
:
3593 case LTTNG_CONSUMER64_UST
:
3594 data_pending
= lttng_ustconsumer_data_pending
;
3597 ERR("Unknown consumer data type");
3601 /* Ease our life a bit */
3602 ht
= consumer_data
.stream_list_ht
;
3604 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3605 ht
->hash_fct(&id
, lttng_ht_seed
),
3607 &iter
.iter
, stream
, node_session_id
.node
) {
3608 pthread_mutex_lock(&stream
->lock
);
3611 * A removed node from the hash table indicates that the stream has
3612 * been deleted thus having a guarantee that the buffers are closed
3613 * on the consumer side. However, data can still be transmitted
3614 * over the network so don't skip the relayd check.
3616 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3618 /* Check the stream if there is data in the buffers. */
3619 ret
= data_pending(stream
);
3621 pthread_mutex_unlock(&stream
->lock
);
3626 pthread_mutex_unlock(&stream
->lock
);
3629 relayd
= find_relayd_by_session_id(id
);
3631 unsigned int is_data_inflight
= 0;
3633 /* Send init command for data pending. */
3634 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3635 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3636 relayd
->relayd_session_id
);
3638 /* Communication error thus the relayd so no data pending. */
3639 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3640 ERR("Relayd begin data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->id
);
3641 lttng_consumer_cleanup_relayd(relayd
);
3642 goto data_not_pending
;
3645 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3646 ht
->hash_fct(&id
, lttng_ht_seed
),
3648 &iter
.iter
, stream
, node_session_id
.node
) {
3649 if (stream
->metadata_flag
) {
3650 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3651 stream
->relayd_stream_id
);
3653 ret
= relayd_data_pending(&relayd
->control_sock
,
3654 stream
->relayd_stream_id
,
3655 stream
->next_net_seq_num
- 1);
3658 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3659 pthread_mutex_unlock(&stream
->lock
);
3663 ERR("Relayd data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->id
);
3664 lttng_consumer_cleanup_relayd(relayd
);
3665 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3666 pthread_mutex_unlock(&stream
->lock
);
3667 goto data_not_pending
;
3671 /* Send end command for data pending. */
3672 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3673 relayd
->relayd_session_id
, &is_data_inflight
);
3674 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3676 ERR("Relayd end data pending failed. Cleaning up relayd %" PRIu64
".", relayd
->id
);
3677 lttng_consumer_cleanup_relayd(relayd
);
3678 goto data_not_pending
;
3680 if (is_data_inflight
) {
3686 * Finding _no_ node in the hash table and no inflight data means that the
3687 * stream(s) have been removed thus data is guaranteed to be available for
3688 * analysis from the trace files.
3692 /* Data is available to be read by a viewer. */
3693 pthread_mutex_unlock(&consumer_data
.lock
);
3698 /* Data is still being extracted from buffers. */
3699 pthread_mutex_unlock(&consumer_data
.lock
);
3705 * Send a ret code status message to the sessiond daemon.
3707 * Return the sendmsg() return value.
3709 int consumer_send_status_msg(int sock
, int ret_code
)
3711 struct lttcomm_consumer_status_msg msg
;
3713 memset(&msg
, 0, sizeof(msg
));
3714 msg
.ret_code
= ret_code
;
3716 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3720 * Send a channel status message to the sessiond daemon.
3722 * Return the sendmsg() return value.
3724 int consumer_send_status_channel(int sock
,
3725 struct lttng_consumer_channel
*channel
)
3727 struct lttcomm_consumer_status_channel msg
;
3731 memset(&msg
, 0, sizeof(msg
));
3733 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3735 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3736 msg
.key
= channel
->key
;
3737 msg
.stream_count
= channel
->streams
.count
;
3740 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3743 unsigned long consumer_get_consume_start_pos(unsigned long consumed_pos
,
3744 unsigned long produced_pos
, uint64_t nb_packets_per_stream
,
3745 uint64_t max_sb_size
)
3747 unsigned long start_pos
;
3749 if (!nb_packets_per_stream
) {
3750 return consumed_pos
; /* Grab everything */
3752 start_pos
= produced_pos
- offset_align_floor(produced_pos
, max_sb_size
);
3753 start_pos
-= max_sb_size
* nb_packets_per_stream
;
3754 if ((long) (start_pos
- consumed_pos
) < 0) {
3755 return consumed_pos
; /* Grab everything */