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 <common/common.h>
34 #include <common/utils.h>
35 #include <common/compat/poll.h>
36 #include <common/kernel-ctl/kernel-ctl.h>
37 #include <common/sessiond-comm/relayd.h>
38 #include <common/sessiond-comm/sessiond-comm.h>
39 #include <common/kernel-consumer/kernel-consumer.h>
40 #include <common/relayd/relayd.h>
41 #include <common/ust-consumer/ust-consumer.h>
44 #include "consumer-stream.h"
46 struct lttng_consumer_global_data consumer_data
= {
49 .type
= LTTNG_CONSUMER_UNKNOWN
,
52 enum consumer_channel_action
{
55 CONSUMER_CHANNEL_QUIT
,
58 struct consumer_channel_msg
{
59 enum consumer_channel_action action
;
60 struct lttng_consumer_channel
*chan
; /* add */
61 uint64_t key
; /* del */
65 * Flag to inform the polling thread to quit when all fd hung up. Updated by
66 * the consumer_thread_receive_fds when it notices that all fds has hung up.
67 * Also updated by the signal handler (consumer_should_exit()). Read by the
70 volatile int consumer_quit
;
73 * Global hash table containing respectively metadata and data streams. The
74 * stream element in this ht should only be updated by the metadata poll thread
75 * for the metadata and the data poll thread for the data.
77 static struct lttng_ht
*metadata_ht
;
78 static struct lttng_ht
*data_ht
;
81 * Notify a thread lttng pipe to poll back again. This usually means that some
82 * global state has changed so we just send back the thread in a poll wait
85 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
87 struct lttng_consumer_stream
*null_stream
= NULL
;
91 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
94 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
95 struct lttng_consumer_channel
*chan
,
97 enum consumer_channel_action action
)
99 struct consumer_channel_msg msg
;
102 memset(&msg
, 0, sizeof(msg
));
108 ret
= write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
109 } while (ret
< 0 && errno
== EINTR
);
112 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
115 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
118 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
119 struct lttng_consumer_channel
**chan
,
121 enum consumer_channel_action
*action
)
123 struct consumer_channel_msg msg
;
127 ret
= read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
128 } while (ret
< 0 && errno
== EINTR
);
130 *action
= msg
.action
;
138 * Find a stream. The consumer_data.lock must be locked during this
141 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
144 struct lttng_ht_iter iter
;
145 struct lttng_ht_node_u64
*node
;
146 struct lttng_consumer_stream
*stream
= NULL
;
150 /* -1ULL keys are lookup failures */
151 if (key
== (uint64_t) -1ULL) {
157 lttng_ht_lookup(ht
, &key
, &iter
);
158 node
= lttng_ht_iter_get_node_u64(&iter
);
160 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
168 static void steal_stream_key(int key
, struct lttng_ht
*ht
)
170 struct lttng_consumer_stream
*stream
;
173 stream
= find_stream(key
, ht
);
177 * We don't want the lookup to match, but we still need
178 * to iterate on this stream when iterating over the hash table. Just
179 * change the node key.
181 stream
->node
.key
= -1ULL;
187 * Return a channel object for the given key.
189 * RCU read side lock MUST be acquired before calling this function and
190 * protects the channel ptr.
192 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
194 struct lttng_ht_iter iter
;
195 struct lttng_ht_node_u64
*node
;
196 struct lttng_consumer_channel
*channel
= NULL
;
198 /* -1ULL keys are lookup failures */
199 if (key
== (uint64_t) -1ULL) {
203 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
204 node
= lttng_ht_iter_get_node_u64(&iter
);
206 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
212 static void free_stream_rcu(struct rcu_head
*head
)
214 struct lttng_ht_node_u64
*node
=
215 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
216 struct lttng_consumer_stream
*stream
=
217 caa_container_of(node
, struct lttng_consumer_stream
, node
);
222 static void free_channel_rcu(struct rcu_head
*head
)
224 struct lttng_ht_node_u64
*node
=
225 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
226 struct lttng_consumer_channel
*channel
=
227 caa_container_of(node
, struct lttng_consumer_channel
, node
);
233 * RCU protected relayd socket pair free.
235 static void free_relayd_rcu(struct rcu_head
*head
)
237 struct lttng_ht_node_u64
*node
=
238 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
239 struct consumer_relayd_sock_pair
*relayd
=
240 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
243 * Close all sockets. This is done in the call RCU since we don't want the
244 * socket fds to be reassigned thus potentially creating bad state of the
247 * We do not have to lock the control socket mutex here since at this stage
248 * there is no one referencing to this relayd object.
250 (void) relayd_close(&relayd
->control_sock
);
251 (void) relayd_close(&relayd
->data_sock
);
257 * Destroy and free relayd socket pair object.
259 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
262 struct lttng_ht_iter iter
;
264 if (relayd
== NULL
) {
268 DBG("Consumer destroy and close relayd socket pair");
270 iter
.iter
.node
= &relayd
->node
.node
;
271 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
273 /* We assume the relayd is being or is destroyed */
277 /* RCU free() call */
278 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
282 * Remove a channel from the global list protected by a mutex. This function is
283 * also responsible for freeing its data structures.
285 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
288 struct lttng_ht_iter iter
;
289 struct lttng_consumer_stream
*stream
, *stmp
;
291 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
293 pthread_mutex_lock(&consumer_data
.lock
);
295 switch (consumer_data
.type
) {
296 case LTTNG_CONSUMER_KERNEL
:
298 case LTTNG_CONSUMER32_UST
:
299 case LTTNG_CONSUMER64_UST
:
300 /* Delete streams that might have been left in the stream list. */
301 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
303 cds_list_del(&stream
->send_node
);
304 lttng_ustconsumer_del_stream(stream
);
307 lttng_ustconsumer_del_channel(channel
);
310 ERR("Unknown consumer_data type");
315 /* Empty no monitor streams list. */
316 if (!channel
->monitor
) {
317 struct lttng_consumer_stream
*stream
, *stmp
;
320 * So, these streams are not visible to any data thread. This is why we
321 * close and free them because they were never added to any data
322 * structure apart from this one.
324 cds_list_for_each_entry_safe(stream
, stmp
,
325 &channel
->stream_no_monitor_list
.head
, no_monitor_node
) {
326 cds_list_del(&stream
->no_monitor_node
);
327 /* Close everything in that stream. */
328 consumer_stream_close(stream
);
329 /* Free the ressource. */
330 consumer_stream_free(stream
);
335 iter
.iter
.node
= &channel
->node
.node
;
336 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
340 call_rcu(&channel
->node
.head
, free_channel_rcu
);
342 pthread_mutex_unlock(&consumer_data
.lock
);
346 * Iterate over the relayd hash table and destroy each element. Finally,
347 * destroy the whole hash table.
349 static void cleanup_relayd_ht(void)
351 struct lttng_ht_iter iter
;
352 struct consumer_relayd_sock_pair
*relayd
;
356 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
358 consumer_destroy_relayd(relayd
);
363 lttng_ht_destroy(consumer_data
.relayd_ht
);
367 * Update the end point status of all streams having the given network sequence
368 * index (relayd index).
370 * It's atomically set without having the stream mutex locked which is fine
371 * because we handle the write/read race with a pipe wakeup for each thread.
373 static void update_endpoint_status_by_netidx(int net_seq_idx
,
374 enum consumer_endpoint_status status
)
376 struct lttng_ht_iter iter
;
377 struct lttng_consumer_stream
*stream
;
379 DBG("Consumer set delete flag on stream by idx %d", net_seq_idx
);
383 /* Let's begin with metadata */
384 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
385 if (stream
->net_seq_idx
== net_seq_idx
) {
386 uatomic_set(&stream
->endpoint_status
, status
);
387 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
391 /* Follow up by the data streams */
392 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
393 if (stream
->net_seq_idx
== net_seq_idx
) {
394 uatomic_set(&stream
->endpoint_status
, status
);
395 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
402 * Cleanup a relayd object by flagging every associated streams for deletion,
403 * destroying the object meaning removing it from the relayd hash table,
404 * closing the sockets and freeing the memory in a RCU call.
406 * If a local data context is available, notify the threads that the streams'
407 * state have changed.
409 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
410 struct lttng_consumer_local_data
*ctx
)
416 DBG("Cleaning up relayd sockets");
418 /* Save the net sequence index before destroying the object */
419 netidx
= relayd
->net_seq_idx
;
422 * Delete the relayd from the relayd hash table, close the sockets and free
423 * the object in a RCU call.
425 consumer_destroy_relayd(relayd
);
427 /* Set inactive endpoint to all streams */
428 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
431 * With a local data context, notify the threads that the streams' state
432 * have changed. The write() action on the pipe acts as an "implicit"
433 * memory barrier ordering the updates of the end point status from the
434 * read of this status which happens AFTER receiving this notify.
437 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
438 notify_thread_lttng_pipe(ctx
->consumer_metadata_pipe
);
443 * Flag a relayd socket pair for destruction. Destroy it if the refcount
446 * RCU read side lock MUST be aquired before calling this function.
448 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
452 /* Set destroy flag for this object */
453 uatomic_set(&relayd
->destroy_flag
, 1);
455 /* Destroy the relayd if refcount is 0 */
456 if (uatomic_read(&relayd
->refcount
) == 0) {
457 consumer_destroy_relayd(relayd
);
462 * Completly destroy stream from every visiable data structure and the given
465 * One this call returns, the stream object is not longer usable nor visible.
467 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
470 consumer_stream_destroy(stream
, ht
);
473 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
475 enum lttng_consumer_stream_state state
,
476 const char *channel_name
,
483 enum consumer_channel_type type
)
486 struct lttng_consumer_stream
*stream
;
488 stream
= zmalloc(sizeof(*stream
));
489 if (stream
== NULL
) {
490 PERROR("malloc struct lttng_consumer_stream");
497 stream
->key
= stream_key
;
499 stream
->out_fd_offset
= 0;
500 stream
->state
= state
;
503 stream
->net_seq_idx
= relayd_id
;
504 stream
->session_id
= session_id
;
505 pthread_mutex_init(&stream
->lock
, NULL
);
507 /* If channel is the metadata, flag this stream as metadata. */
508 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
509 stream
->metadata_flag
= 1;
510 /* Metadata is flat out. */
511 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
513 /* Format stream name to <channel_name>_<cpu_number> */
514 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
517 PERROR("snprintf stream name");
522 /* Key is always the wait_fd for streams. */
523 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
525 /* Init node per channel id key */
526 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
528 /* Init session id node with the stream session id */
529 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
531 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
532 " relayd_id %" PRIu64
", session_id %" PRIu64
,
533 stream
->name
, stream
->key
, channel_key
,
534 stream
->net_seq_idx
, stream
->session_id
);
550 * Add a stream to the global list protected by a mutex.
552 static int add_stream(struct lttng_consumer_stream
*stream
,
556 struct consumer_relayd_sock_pair
*relayd
;
561 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
563 pthread_mutex_lock(&consumer_data
.lock
);
564 pthread_mutex_lock(&stream
->lock
);
567 /* Steal stream identifier to avoid having streams with the same key */
568 steal_stream_key(stream
->key
, ht
);
570 lttng_ht_add_unique_u64(ht
, &stream
->node
);
572 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
573 &stream
->node_channel_id
);
576 * Add stream to the stream_list_ht of the consumer data. No need to steal
577 * the key since the HT does not use it and we allow to add redundant keys
580 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
582 /* Check and cleanup relayd */
583 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
584 if (relayd
!= NULL
) {
585 uatomic_inc(&relayd
->refcount
);
589 * When nb_init_stream_left reaches 0, we don't need to trigger any action
590 * in terms of destroying the associated channel, because the action that
591 * causes the count to become 0 also causes a stream to be added. The
592 * channel deletion will thus be triggered by the following removal of this
595 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
596 /* Increment refcount before decrementing nb_init_stream_left */
598 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
601 /* Update consumer data once the node is inserted. */
602 consumer_data
.stream_count
++;
603 consumer_data
.need_update
= 1;
606 pthread_mutex_unlock(&stream
->lock
);
607 pthread_mutex_unlock(&consumer_data
.lock
);
613 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
614 * be acquired before calling this.
616 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
619 struct lttng_ht_node_u64
*node
;
620 struct lttng_ht_iter iter
;
624 lttng_ht_lookup(consumer_data
.relayd_ht
,
625 &relayd
->net_seq_idx
, &iter
);
626 node
= lttng_ht_iter_get_node_u64(&iter
);
630 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
637 * Allocate and return a consumer relayd socket.
639 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
642 struct consumer_relayd_sock_pair
*obj
= NULL
;
644 /* Negative net sequence index is a failure */
645 if (net_seq_idx
< 0) {
649 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
651 PERROR("zmalloc relayd sock");
655 obj
->net_seq_idx
= net_seq_idx
;
657 obj
->destroy_flag
= 0;
658 obj
->control_sock
.sock
.fd
= -1;
659 obj
->data_sock
.sock
.fd
= -1;
660 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
661 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
668 * Find a relayd socket pair in the global consumer data.
670 * Return the object if found else NULL.
671 * RCU read-side lock must be held across this call and while using the
674 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
676 struct lttng_ht_iter iter
;
677 struct lttng_ht_node_u64
*node
;
678 struct consumer_relayd_sock_pair
*relayd
= NULL
;
680 /* Negative keys are lookup failures */
681 if (key
== (uint64_t) -1ULL) {
685 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
687 node
= lttng_ht_iter_get_node_u64(&iter
);
689 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
697 * Handle stream for relayd transmission if the stream applies for network
698 * streaming where the net sequence index is set.
700 * Return destination file descriptor or negative value on error.
702 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
703 size_t data_size
, unsigned long padding
,
704 struct consumer_relayd_sock_pair
*relayd
)
707 struct lttcomm_relayd_data_hdr data_hdr
;
713 /* Reset data header */
714 memset(&data_hdr
, 0, sizeof(data_hdr
));
716 if (stream
->metadata_flag
) {
717 /* Caller MUST acquire the relayd control socket lock */
718 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
723 /* Metadata are always sent on the control socket. */
724 outfd
= relayd
->control_sock
.sock
.fd
;
726 /* Set header with stream information */
727 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
728 data_hdr
.data_size
= htobe32(data_size
);
729 data_hdr
.padding_size
= htobe32(padding
);
731 * Note that net_seq_num below is assigned with the *current* value of
732 * next_net_seq_num and only after that the next_net_seq_num will be
733 * increment. This is why when issuing a command on the relayd using
734 * this next value, 1 should always be substracted in order to compare
735 * the last seen sequence number on the relayd side to the last sent.
737 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
738 /* Other fields are zeroed previously */
740 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
746 ++stream
->next_net_seq_num
;
748 /* Set to go on data socket */
749 outfd
= relayd
->data_sock
.sock
.fd
;
757 * Allocate and return a new lttng_consumer_channel object using the given key
758 * to initialize the hash table node.
760 * On error, return NULL.
762 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
764 const char *pathname
,
769 enum lttng_event_output output
,
770 uint64_t tracefile_size
,
771 uint64_t tracefile_count
,
772 uint64_t session_id_per_pid
,
773 unsigned int monitor
)
775 struct lttng_consumer_channel
*channel
;
777 channel
= zmalloc(sizeof(*channel
));
778 if (channel
== NULL
) {
779 PERROR("malloc struct lttng_consumer_channel");
784 channel
->refcount
= 0;
785 channel
->session_id
= session_id
;
786 channel
->session_id_per_pid
= session_id_per_pid
;
789 channel
->relayd_id
= relayd_id
;
790 channel
->output
= output
;
791 channel
->tracefile_size
= tracefile_size
;
792 channel
->tracefile_count
= tracefile_count
;
793 channel
->monitor
= monitor
;
796 * In monitor mode, the streams associated with the channel will be put in
797 * a special list ONLY owned by this channel. So, the refcount is set to 1
798 * here meaning that the channel itself has streams that are referenced.
800 * On a channel deletion, once the channel is no longer visible, the
801 * refcount is decremented and checked for a zero value to delete it. With
802 * streams in no monitor mode, it will now be safe to destroy the channel.
804 if (!channel
->monitor
) {
805 channel
->refcount
= 1;
808 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
809 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
811 strncpy(channel
->name
, name
, sizeof(channel
->name
));
812 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
814 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
816 channel
->wait_fd
= -1;
818 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
819 CDS_INIT_LIST_HEAD(&channel
->stream_no_monitor_list
.head
);
821 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
828 * Add a channel to the global list protected by a mutex.
830 * On success 0 is returned else a negative value.
832 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
833 struct lttng_consumer_local_data
*ctx
)
836 struct lttng_ht_node_u64
*node
;
837 struct lttng_ht_iter iter
;
839 pthread_mutex_lock(&consumer_data
.lock
);
842 lttng_ht_lookup(consumer_data
.channel_ht
, &channel
->key
, &iter
);
843 node
= lttng_ht_iter_get_node_u64(&iter
);
845 /* Channel already exist. Ignore the insertion */
846 ERR("Consumer add channel key %" PRIu64
" already exists!",
852 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
856 pthread_mutex_unlock(&consumer_data
.lock
);
858 if (!ret
&& channel
->wait_fd
!= -1 &&
859 channel
->metadata_stream
== NULL
) {
860 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
866 * Allocate the pollfd structure and the local view of the out fds to avoid
867 * doing a lookup in the linked list and concurrency issues when writing is
868 * needed. Called with consumer_data.lock held.
870 * Returns the number of fds in the structures.
872 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
873 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
877 struct lttng_ht_iter iter
;
878 struct lttng_consumer_stream
*stream
;
883 assert(local_stream
);
885 DBG("Updating poll fd array");
887 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
889 * Only active streams with an active end point can be added to the
890 * poll set and local stream storage of the thread.
892 * There is a potential race here for endpoint_status to be updated
893 * just after the check. However, this is OK since the stream(s) will
894 * be deleted once the thread is notified that the end point state has
895 * changed where this function will be called back again.
897 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
898 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
902 * This clobbers way too much the debug output. Uncomment that if you
903 * need it for debugging purposes.
905 * DBG("Active FD %d", stream->wait_fd);
907 (*pollfd
)[i
].fd
= stream
->wait_fd
;
908 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
909 local_stream
[i
] = stream
;
915 * Insert the consumer_data_pipe at the end of the array and don't
916 * increment i so nb_fd is the number of real FD.
918 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
919 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
924 * Poll on the should_quit pipe and the command socket return -1 on error and
925 * should exit, 0 if data is available on the command socket
927 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
932 num_rdy
= poll(consumer_sockpoll
, 2, -1);
935 * Restart interrupted system call.
937 if (errno
== EINTR
) {
940 PERROR("Poll error");
943 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
944 DBG("consumer_should_quit wake up");
954 * Set the error socket.
956 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
959 ctx
->consumer_error_socket
= sock
;
963 * Set the command socket path.
965 void lttng_consumer_set_command_sock_path(
966 struct lttng_consumer_local_data
*ctx
, char *sock
)
968 ctx
->consumer_command_sock_path
= sock
;
972 * Send return code to the session daemon.
973 * If the socket is not defined, we return 0, it is not a fatal error
975 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
977 if (ctx
->consumer_error_socket
> 0) {
978 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
979 sizeof(enum lttcomm_sessiond_command
));
986 * Close all the tracefiles and stream fds and MUST be called when all
987 * instances are destroyed i.e. when all threads were joined and are ended.
989 void lttng_consumer_cleanup(void)
991 struct lttng_ht_iter iter
;
992 struct lttng_consumer_channel
*channel
;
996 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
998 consumer_del_channel(channel
);
1003 lttng_ht_destroy(consumer_data
.channel_ht
);
1005 cleanup_relayd_ht();
1007 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1010 * This HT contains streams that are freed by either the metadata thread or
1011 * the data thread so we do *nothing* on the hash table and simply destroy
1014 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1018 * Called from signal handler.
1020 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1025 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1026 } while (ret
< 0 && errno
== EINTR
);
1027 if (ret
< 0 || ret
!= 1) {
1028 PERROR("write consumer quit");
1031 DBG("Consumer flag that it should quit");
1034 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1037 int outfd
= stream
->out_fd
;
1040 * This does a blocking write-and-wait on any page that belongs to the
1041 * subbuffer prior to the one we just wrote.
1042 * Don't care about error values, as these are just hints and ways to
1043 * limit the amount of page cache used.
1045 if (orig_offset
< stream
->max_sb_size
) {
1048 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1049 stream
->max_sb_size
,
1050 SYNC_FILE_RANGE_WAIT_BEFORE
1051 | SYNC_FILE_RANGE_WRITE
1052 | SYNC_FILE_RANGE_WAIT_AFTER
);
1054 * Give hints to the kernel about how we access the file:
1055 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1058 * We need to call fadvise again after the file grows because the
1059 * kernel does not seem to apply fadvise to non-existing parts of the
1062 * Call fadvise _after_ having waited for the page writeback to
1063 * complete because the dirty page writeback semantic is not well
1064 * defined. So it can be expected to lead to lower throughput in
1067 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1068 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1072 * Initialise the necessary environnement :
1073 * - create a new context
1074 * - create the poll_pipe
1075 * - create the should_quit pipe (for signal handler)
1076 * - create the thread pipe (for splice)
1078 * Takes a function pointer as argument, this function is called when data is
1079 * available on a buffer. This function is responsible to do the
1080 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1081 * buffer configuration and then kernctl_put_next_subbuf at the end.
1083 * Returns a pointer to the new context or NULL on error.
1085 struct lttng_consumer_local_data
*lttng_consumer_create(
1086 enum lttng_consumer_type type
,
1087 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1088 struct lttng_consumer_local_data
*ctx
),
1089 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1090 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1091 int (*update_stream
)(int stream_key
, uint32_t state
))
1094 struct lttng_consumer_local_data
*ctx
;
1096 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1097 consumer_data
.type
== type
);
1098 consumer_data
.type
= type
;
1100 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1102 PERROR("allocating context");
1106 ctx
->consumer_error_socket
= -1;
1107 ctx
->consumer_metadata_socket
= -1;
1108 /* assign the callbacks */
1109 ctx
->on_buffer_ready
= buffer_ready
;
1110 ctx
->on_recv_channel
= recv_channel
;
1111 ctx
->on_recv_stream
= recv_stream
;
1112 ctx
->on_update_stream
= update_stream
;
1114 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1115 if (!ctx
->consumer_data_pipe
) {
1116 goto error_poll_pipe
;
1119 ret
= pipe(ctx
->consumer_should_quit
);
1121 PERROR("Error creating recv pipe");
1122 goto error_quit_pipe
;
1125 ret
= pipe(ctx
->consumer_thread_pipe
);
1127 PERROR("Error creating thread pipe");
1128 goto error_thread_pipe
;
1131 ret
= pipe(ctx
->consumer_channel_pipe
);
1133 PERROR("Error creating channel pipe");
1134 goto error_channel_pipe
;
1137 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1138 if (!ctx
->consumer_metadata_pipe
) {
1139 goto error_metadata_pipe
;
1142 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1144 goto error_splice_pipe
;
1150 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1151 error_metadata_pipe
:
1152 utils_close_pipe(ctx
->consumer_channel_pipe
);
1154 utils_close_pipe(ctx
->consumer_thread_pipe
);
1156 utils_close_pipe(ctx
->consumer_should_quit
);
1158 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1166 * Close all fds associated with the instance and free the context.
1168 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1172 DBG("Consumer destroying it. Closing everything.");
1174 ret
= close(ctx
->consumer_error_socket
);
1178 ret
= close(ctx
->consumer_metadata_socket
);
1182 utils_close_pipe(ctx
->consumer_thread_pipe
);
1183 utils_close_pipe(ctx
->consumer_channel_pipe
);
1184 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1185 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1186 utils_close_pipe(ctx
->consumer_should_quit
);
1187 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1189 unlink(ctx
->consumer_command_sock_path
);
1194 * Write the metadata stream id on the specified file descriptor.
1196 static int write_relayd_metadata_id(int fd
,
1197 struct lttng_consumer_stream
*stream
,
1198 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1201 struct lttcomm_relayd_metadata_payload hdr
;
1203 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1204 hdr
.padding_size
= htobe32(padding
);
1206 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1207 } while (ret
< 0 && errno
== EINTR
);
1208 if (ret
< 0 || ret
!= sizeof(hdr
)) {
1210 * This error means that the fd's end is closed so ignore the perror
1211 * not to clubber the error output since this can happen in a normal
1214 if (errno
!= EPIPE
) {
1215 PERROR("write metadata stream id");
1217 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1219 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1220 * handle writting the missing part so report that as an error and
1221 * don't lie to the caller.
1226 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1227 stream
->relayd_stream_id
, padding
);
1234 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1235 * core function for writing trace buffers to either the local filesystem or
1238 * It must be called with the stream lock held.
1240 * Careful review MUST be put if any changes occur!
1242 * Returns the number of bytes written
1244 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1245 struct lttng_consumer_local_data
*ctx
,
1246 struct lttng_consumer_stream
*stream
, unsigned long len
,
1247 unsigned long padding
)
1249 unsigned long mmap_offset
;
1251 ssize_t ret
= 0, written
= 0;
1252 off_t orig_offset
= stream
->out_fd_offset
;
1253 /* Default is on the disk */
1254 int outfd
= stream
->out_fd
;
1255 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1256 unsigned int relayd_hang_up
= 0;
1258 /* RCU lock for the relayd pointer */
1261 /* Flag that the current stream if set for network streaming. */
1262 if (stream
->net_seq_idx
!= -1) {
1263 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1264 if (relayd
== NULL
) {
1269 /* get the offset inside the fd to mmap */
1270 switch (consumer_data
.type
) {
1271 case LTTNG_CONSUMER_KERNEL
:
1272 mmap_base
= stream
->mmap_base
;
1273 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1275 case LTTNG_CONSUMER32_UST
:
1276 case LTTNG_CONSUMER64_UST
:
1277 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1279 ERR("read mmap get mmap base for stream %s", stream
->name
);
1283 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1287 ERR("Unknown consumer_data type");
1292 PERROR("tracer ctl get_mmap_read_offset");
1297 /* Handle stream on the relayd if the output is on the network */
1299 unsigned long netlen
= len
;
1302 * Lock the control socket for the complete duration of the function
1303 * since from this point on we will use the socket.
1305 if (stream
->metadata_flag
) {
1306 /* Metadata requires the control socket. */
1307 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1308 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1311 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1313 /* Use the returned socket. */
1316 /* Write metadata stream id before payload */
1317 if (stream
->metadata_flag
) {
1318 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1321 /* Socket operation failed. We consider the relayd dead */
1322 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1330 /* Socket operation failed. We consider the relayd dead */
1331 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1335 /* Else, use the default set before which is the filesystem. */
1338 /* No streaming, we have to set the len with the full padding */
1342 * Check if we need to change the tracefile before writing the packet.
1344 if (stream
->chan
->tracefile_size
> 0 &&
1345 (stream
->tracefile_size_current
+ len
) >
1346 stream
->chan
->tracefile_size
) {
1347 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1348 stream
->name
, stream
->chan
->tracefile_size
,
1349 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1350 stream
->out_fd
, &(stream
->tracefile_count_current
));
1352 ERR("Rotating output file");
1355 outfd
= stream
->out_fd
= ret
;
1356 /* Reset current size because we just perform a rotation. */
1357 stream
->tracefile_size_current
= 0;
1359 stream
->tracefile_size_current
+= len
;
1364 ret
= write(outfd
, mmap_base
+ mmap_offset
, len
);
1365 } while (ret
< 0 && errno
== EINTR
);
1366 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1369 * This is possible if the fd is closed on the other side (outfd)
1370 * or any write problem. It can be verbose a bit for a normal
1371 * execution if for instance the relayd is stopped abruptly. This
1372 * can happen so set this to a DBG statement.
1374 DBG("Error in file write mmap");
1378 /* Socket operation failed. We consider the relayd dead */
1379 if (errno
== EPIPE
|| errno
== EINVAL
) {
1384 } else if (ret
> len
) {
1385 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1393 /* This call is useless on a socket so better save a syscall. */
1395 /* This won't block, but will start writeout asynchronously */
1396 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1397 SYNC_FILE_RANGE_WRITE
);
1398 stream
->out_fd_offset
+= ret
;
1402 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1406 * This is a special case that the relayd has closed its socket. Let's
1407 * cleanup the relayd object and all associated streams.
1409 if (relayd
&& relayd_hang_up
) {
1410 cleanup_relayd(relayd
, ctx
);
1414 /* Unlock only if ctrl socket used */
1415 if (relayd
&& stream
->metadata_flag
) {
1416 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1424 * Splice the data from the ring buffer to the tracefile.
1426 * It must be called with the stream lock held.
1428 * Returns the number of bytes spliced.
1430 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1431 struct lttng_consumer_local_data
*ctx
,
1432 struct lttng_consumer_stream
*stream
, unsigned long len
,
1433 unsigned long padding
)
1435 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1437 off_t orig_offset
= stream
->out_fd_offset
;
1438 int fd
= stream
->wait_fd
;
1439 /* Default is on the disk */
1440 int outfd
= stream
->out_fd
;
1441 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1443 unsigned int relayd_hang_up
= 0;
1445 switch (consumer_data
.type
) {
1446 case LTTNG_CONSUMER_KERNEL
:
1448 case LTTNG_CONSUMER32_UST
:
1449 case LTTNG_CONSUMER64_UST
:
1450 /* Not supported for user space tracing */
1453 ERR("Unknown consumer_data type");
1457 /* RCU lock for the relayd pointer */
1460 /* Flag that the current stream if set for network streaming. */
1461 if (stream
->net_seq_idx
!= -1) {
1462 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1463 if (relayd
== NULL
) {
1469 * Choose right pipe for splice. Metadata and trace data are handled by
1470 * different threads hence the use of two pipes in order not to race or
1471 * corrupt the written data.
1473 if (stream
->metadata_flag
) {
1474 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1476 splice_pipe
= ctx
->consumer_thread_pipe
;
1479 /* Write metadata stream id before payload */
1481 int total_len
= len
;
1483 if (stream
->metadata_flag
) {
1485 * Lock the control socket for the complete duration of the function
1486 * since from this point on we will use the socket.
1488 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1490 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1494 /* Socket operation failed. We consider the relayd dead */
1495 if (ret
== -EBADF
) {
1496 WARN("Remote relayd disconnected. Stopping");
1503 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1506 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1508 /* Use the returned socket. */
1511 /* Socket operation failed. We consider the relayd dead */
1512 if (ret
== -EBADF
) {
1513 WARN("Remote relayd disconnected. Stopping");
1520 /* No streaming, we have to set the len with the full padding */
1524 * Check if we need to change the tracefile before writing the packet.
1526 if (stream
->chan
->tracefile_size
> 0 &&
1527 (stream
->tracefile_size_current
+ len
) >
1528 stream
->chan
->tracefile_size
) {
1529 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1530 stream
->name
, stream
->chan
->tracefile_size
,
1531 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1532 stream
->out_fd
, &(stream
->tracefile_count_current
));
1534 ERR("Rotating output file");
1537 outfd
= stream
->out_fd
= ret
;
1538 /* Reset current size because we just perform a rotation. */
1539 stream
->tracefile_size_current
= 0;
1541 stream
->tracefile_size_current
+= len
;
1545 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1546 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1547 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1548 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1549 DBG("splice chan to pipe, ret %zd", ret_splice
);
1550 if (ret_splice
< 0) {
1551 PERROR("Error in relay splice");
1553 written
= ret_splice
;
1559 /* Handle stream on the relayd if the output is on the network */
1561 if (stream
->metadata_flag
) {
1562 size_t metadata_payload_size
=
1563 sizeof(struct lttcomm_relayd_metadata_payload
);
1565 /* Update counter to fit the spliced data */
1566 ret_splice
+= metadata_payload_size
;
1567 len
+= metadata_payload_size
;
1569 * We do this so the return value can match the len passed as
1570 * argument to this function.
1572 written
-= metadata_payload_size
;
1576 /* Splice data out */
1577 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1578 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1579 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1580 if (ret_splice
< 0) {
1581 PERROR("Error in file splice");
1583 written
= ret_splice
;
1585 /* Socket operation failed. We consider the relayd dead */
1586 if (errno
== EBADF
|| errno
== EPIPE
) {
1587 WARN("Remote relayd disconnected. Stopping");
1593 } else if (ret_splice
> len
) {
1595 PERROR("Wrote more data than requested %zd (len: %lu)",
1597 written
+= ret_splice
;
1603 /* This call is useless on a socket so better save a syscall. */
1605 /* This won't block, but will start writeout asynchronously */
1606 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1607 SYNC_FILE_RANGE_WRITE
);
1608 stream
->out_fd_offset
+= ret_splice
;
1610 written
+= ret_splice
;
1612 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1620 * This is a special case that the relayd has closed its socket. Let's
1621 * cleanup the relayd object and all associated streams.
1623 if (relayd
&& relayd_hang_up
) {
1624 cleanup_relayd(relayd
, ctx
);
1625 /* Skip splice error so the consumer does not fail */
1630 /* send the appropriate error description to sessiond */
1633 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1636 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1639 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1644 if (relayd
&& stream
->metadata_flag
) {
1645 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1653 * Take a snapshot for a specific fd
1655 * Returns 0 on success, < 0 on error
1657 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1659 switch (consumer_data
.type
) {
1660 case LTTNG_CONSUMER_KERNEL
:
1661 return lttng_kconsumer_take_snapshot(stream
);
1662 case LTTNG_CONSUMER32_UST
:
1663 case LTTNG_CONSUMER64_UST
:
1664 return lttng_ustconsumer_take_snapshot(stream
);
1666 ERR("Unknown consumer_data type");
1673 * Get the produced position
1675 * Returns 0 on success, < 0 on error
1677 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1680 switch (consumer_data
.type
) {
1681 case LTTNG_CONSUMER_KERNEL
:
1682 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1683 case LTTNG_CONSUMER32_UST
:
1684 case LTTNG_CONSUMER64_UST
:
1685 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1687 ERR("Unknown consumer_data type");
1693 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1694 int sock
, struct pollfd
*consumer_sockpoll
)
1696 switch (consumer_data
.type
) {
1697 case LTTNG_CONSUMER_KERNEL
:
1698 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1699 case LTTNG_CONSUMER32_UST
:
1700 case LTTNG_CONSUMER64_UST
:
1701 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1703 ERR("Unknown consumer_data type");
1710 * Iterate over all streams of the hashtable and free them properly.
1712 * WARNING: *MUST* be used with data stream only.
1714 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1716 struct lttng_ht_iter iter
;
1717 struct lttng_consumer_stream
*stream
;
1724 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1726 * Ignore return value since we are currently cleaning up so any error
1729 (void) consumer_del_stream(stream
, ht
);
1733 lttng_ht_destroy(ht
);
1737 * Iterate over all streams of the hashtable and free them properly.
1739 * XXX: Should not be only for metadata stream or else use an other name.
1741 static void destroy_stream_ht(struct lttng_ht
*ht
)
1743 struct lttng_ht_iter iter
;
1744 struct lttng_consumer_stream
*stream
;
1751 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1753 * Ignore return value since we are currently cleaning up so any error
1756 (void) consumer_del_metadata_stream(stream
, ht
);
1760 lttng_ht_destroy(ht
);
1763 void lttng_consumer_close_metadata(void)
1765 switch (consumer_data
.type
) {
1766 case LTTNG_CONSUMER_KERNEL
:
1768 * The Kernel consumer has a different metadata scheme so we don't
1769 * close anything because the stream will be closed by the session
1773 case LTTNG_CONSUMER32_UST
:
1774 case LTTNG_CONSUMER64_UST
:
1776 * Close all metadata streams. The metadata hash table is passed and
1777 * this call iterates over it by closing all wakeup fd. This is safe
1778 * because at this point we are sure that the metadata producer is
1779 * either dead or blocked.
1781 lttng_ustconsumer_close_metadata(metadata_ht
);
1784 ERR("Unknown consumer_data type");
1790 * Clean up a metadata stream and free its memory.
1792 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1793 struct lttng_ht
*ht
)
1796 struct lttng_ht_iter iter
;
1797 struct lttng_consumer_channel
*free_chan
= NULL
;
1798 struct consumer_relayd_sock_pair
*relayd
;
1802 * This call should NEVER receive regular stream. It must always be
1803 * metadata stream and this is crucial for data structure synchronization.
1805 assert(stream
->metadata_flag
);
1807 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1810 /* Means the stream was allocated but not successfully added */
1811 goto free_stream_rcu
;
1814 pthread_mutex_lock(&consumer_data
.lock
);
1815 pthread_mutex_lock(&stream
->lock
);
1817 switch (consumer_data
.type
) {
1818 case LTTNG_CONSUMER_KERNEL
:
1819 if (stream
->mmap_base
!= NULL
) {
1820 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1822 PERROR("munmap metadata stream");
1825 if (stream
->wait_fd
>= 0) {
1826 ret
= close(stream
->wait_fd
);
1828 PERROR("close kernel metadata wait_fd");
1832 case LTTNG_CONSUMER32_UST
:
1833 case LTTNG_CONSUMER64_UST
:
1834 lttng_ustconsumer_del_stream(stream
);
1837 ERR("Unknown consumer_data type");
1843 iter
.iter
.node
= &stream
->node
.node
;
1844 ret
= lttng_ht_del(ht
, &iter
);
1847 iter
.iter
.node
= &stream
->node_channel_id
.node
;
1848 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
1851 iter
.iter
.node
= &stream
->node_session_id
.node
;
1852 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1856 if (stream
->out_fd
>= 0) {
1857 ret
= close(stream
->out_fd
);
1863 /* Check and cleanup relayd */
1865 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1866 if (relayd
!= NULL
) {
1867 uatomic_dec(&relayd
->refcount
);
1868 assert(uatomic_read(&relayd
->refcount
) >= 0);
1870 /* Closing streams requires to lock the control socket. */
1871 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1872 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1873 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1874 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1876 DBG("Unable to close stream on the relayd. Continuing");
1878 * Continue here. There is nothing we can do for the relayd.
1879 * Chances are that the relayd has closed the socket so we just
1880 * continue cleaning up.
1884 /* Both conditions are met, we destroy the relayd. */
1885 if (uatomic_read(&relayd
->refcount
) == 0 &&
1886 uatomic_read(&relayd
->destroy_flag
)) {
1887 consumer_destroy_relayd(relayd
);
1892 /* Atomically decrement channel refcount since other threads can use it. */
1893 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
1894 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
1895 /* Go for channel deletion! */
1896 free_chan
= stream
->chan
;
1901 * Nullify the stream reference so it is not used after deletion. The
1902 * consumer data lock MUST be acquired before being able to check for a
1903 * NULL pointer value.
1905 stream
->chan
->metadata_stream
= NULL
;
1907 pthread_mutex_unlock(&stream
->lock
);
1908 pthread_mutex_unlock(&consumer_data
.lock
);
1911 consumer_del_channel(free_chan
);
1915 call_rcu(&stream
->node
.head
, free_stream_rcu
);
1919 * Action done with the metadata stream when adding it to the consumer internal
1920 * data structures to handle it.
1922 static int add_metadata_stream(struct lttng_consumer_stream
*stream
,
1923 struct lttng_ht
*ht
)
1926 struct consumer_relayd_sock_pair
*relayd
;
1927 struct lttng_ht_iter iter
;
1928 struct lttng_ht_node_u64
*node
;
1933 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
1935 pthread_mutex_lock(&consumer_data
.lock
);
1936 pthread_mutex_lock(&stream
->lock
);
1939 * From here, refcounts are updated so be _careful_ when returning an error
1946 * Lookup the stream just to make sure it does not exist in our internal
1947 * state. This should NEVER happen.
1949 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
1950 node
= lttng_ht_iter_get_node_u64(&iter
);
1953 /* Find relayd and, if one is found, increment refcount. */
1954 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1955 if (relayd
!= NULL
) {
1956 uatomic_inc(&relayd
->refcount
);
1960 * When nb_init_stream_left reaches 0, we don't need to trigger any action
1961 * in terms of destroying the associated channel, because the action that
1962 * causes the count to become 0 also causes a stream to be added. The
1963 * channel deletion will thus be triggered by the following removal of this
1966 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
1967 /* Increment refcount before decrementing nb_init_stream_left */
1969 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
1972 lttng_ht_add_unique_u64(ht
, &stream
->node
);
1974 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
1975 &stream
->node_channel_id
);
1978 * Add stream to the stream_list_ht of the consumer data. No need to steal
1979 * the key since the HT does not use it and we allow to add redundant keys
1982 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
1986 pthread_mutex_unlock(&stream
->lock
);
1987 pthread_mutex_unlock(&consumer_data
.lock
);
1992 * Delete data stream that are flagged for deletion (endpoint_status).
1994 static void validate_endpoint_status_data_stream(void)
1996 struct lttng_ht_iter iter
;
1997 struct lttng_consumer_stream
*stream
;
1999 DBG("Consumer delete flagged data stream");
2002 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2003 /* Validate delete flag of the stream */
2004 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2007 /* Delete it right now */
2008 consumer_del_stream(stream
, data_ht
);
2014 * Delete metadata stream that are flagged for deletion (endpoint_status).
2016 static void validate_endpoint_status_metadata_stream(
2017 struct lttng_poll_event
*pollset
)
2019 struct lttng_ht_iter iter
;
2020 struct lttng_consumer_stream
*stream
;
2022 DBG("Consumer delete flagged metadata stream");
2027 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2028 /* Validate delete flag of the stream */
2029 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2033 * Remove from pollset so the metadata thread can continue without
2034 * blocking on a deleted stream.
2036 lttng_poll_del(pollset
, stream
->wait_fd
);
2038 /* Delete it right now */
2039 consumer_del_metadata_stream(stream
, metadata_ht
);
2045 * Thread polls on metadata file descriptor and write them on disk or on the
2048 void *consumer_thread_metadata_poll(void *data
)
2051 uint32_t revents
, nb_fd
;
2052 struct lttng_consumer_stream
*stream
= NULL
;
2053 struct lttng_ht_iter iter
;
2054 struct lttng_ht_node_u64
*node
;
2055 struct lttng_poll_event events
;
2056 struct lttng_consumer_local_data
*ctx
= data
;
2059 rcu_register_thread();
2061 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2063 /* ENOMEM at this point. Better to bail out. */
2067 DBG("Thread metadata poll started");
2069 /* Size is set to 1 for the consumer_metadata pipe */
2070 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2072 ERR("Poll set creation failed");
2076 ret
= lttng_poll_add(&events
,
2077 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2083 DBG("Metadata main loop started");
2086 /* Only the metadata pipe is set */
2087 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2092 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2093 ret
= lttng_poll_wait(&events
, -1);
2094 DBG("Metadata event catched in thread");
2096 if (errno
== EINTR
) {
2097 ERR("Poll EINTR catched");
2105 /* From here, the event is a metadata wait fd */
2106 for (i
= 0; i
< nb_fd
; i
++) {
2107 revents
= LTTNG_POLL_GETEV(&events
, i
);
2108 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2110 /* Just don't waste time if no returned events for the fd */
2115 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2116 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2117 DBG("Metadata thread pipe hung up");
2119 * Remove the pipe from the poll set and continue the loop
2120 * since their might be data to consume.
2122 lttng_poll_del(&events
,
2123 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2124 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2126 } else if (revents
& LPOLLIN
) {
2129 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2130 &stream
, sizeof(stream
));
2132 ERR("read metadata stream, ret: %ld", pipe_len
);
2134 * Continue here to handle the rest of the streams.
2139 /* A NULL stream means that the state has changed. */
2140 if (stream
== NULL
) {
2141 /* Check for deleted streams. */
2142 validate_endpoint_status_metadata_stream(&events
);
2146 DBG("Adding metadata stream %d to poll set",
2149 ret
= add_metadata_stream(stream
, metadata_ht
);
2151 ERR("Unable to add metadata stream");
2152 /* Stream was not setup properly. Continuing. */
2153 consumer_del_metadata_stream(stream
, NULL
);
2157 /* Add metadata stream to the global poll events list */
2158 lttng_poll_add(&events
, stream
->wait_fd
,
2159 LPOLLIN
| LPOLLPRI
);
2162 /* Handle other stream */
2168 uint64_t tmp_id
= (uint64_t) pollfd
;
2170 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2172 node
= lttng_ht_iter_get_node_u64(&iter
);
2175 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2178 /* Check for error event */
2179 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2180 DBG("Metadata fd %d is hup|err.", pollfd
);
2181 if (!stream
->hangup_flush_done
2182 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2183 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2184 DBG("Attempting to flush and consume the UST buffers");
2185 lttng_ustconsumer_on_stream_hangup(stream
);
2187 /* We just flushed the stream now read it. */
2189 len
= ctx
->on_buffer_ready(stream
, ctx
);
2191 * We don't check the return value here since if we get
2192 * a negative len, it means an error occured thus we
2193 * simply remove it from the poll set and free the
2199 lttng_poll_del(&events
, stream
->wait_fd
);
2201 * This call update the channel states, closes file descriptors
2202 * and securely free the stream.
2204 consumer_del_metadata_stream(stream
, metadata_ht
);
2205 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2206 /* Get the data out of the metadata file descriptor */
2207 DBG("Metadata available on fd %d", pollfd
);
2208 assert(stream
->wait_fd
== pollfd
);
2210 len
= ctx
->on_buffer_ready(stream
, ctx
);
2211 /* It's ok to have an unavailable sub-buffer */
2212 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2213 /* Clean up stream from consumer and free it. */
2214 lttng_poll_del(&events
, stream
->wait_fd
);
2215 consumer_del_metadata_stream(stream
, metadata_ht
);
2216 } else if (len
> 0) {
2217 stream
->data_read
= 1;
2221 /* Release RCU lock for the stream looked up */
2228 DBG("Metadata poll thread exiting");
2230 lttng_poll_clean(&events
);
2232 destroy_stream_ht(metadata_ht
);
2234 rcu_unregister_thread();
2239 * This thread polls the fds in the set to consume the data and write
2240 * it to tracefile if necessary.
2242 void *consumer_thread_data_poll(void *data
)
2244 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2245 struct pollfd
*pollfd
= NULL
;
2246 /* local view of the streams */
2247 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2248 /* local view of consumer_data.fds_count */
2250 struct lttng_consumer_local_data
*ctx
= data
;
2253 rcu_register_thread();
2255 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2256 if (data_ht
== NULL
) {
2257 /* ENOMEM at this point. Better to bail out. */
2261 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
2268 * the fds set has been updated, we need to update our
2269 * local array as well
2271 pthread_mutex_lock(&consumer_data
.lock
);
2272 if (consumer_data
.need_update
) {
2277 local_stream
= NULL
;
2279 /* allocate for all fds + 1 for the consumer_data_pipe */
2280 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2281 if (pollfd
== NULL
) {
2282 PERROR("pollfd malloc");
2283 pthread_mutex_unlock(&consumer_data
.lock
);
2287 /* allocate for all fds + 1 for the consumer_data_pipe */
2288 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2289 sizeof(struct lttng_consumer_stream
*));
2290 if (local_stream
== NULL
) {
2291 PERROR("local_stream malloc");
2292 pthread_mutex_unlock(&consumer_data
.lock
);
2295 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2298 ERR("Error in allocating pollfd or local_outfds");
2299 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2300 pthread_mutex_unlock(&consumer_data
.lock
);
2304 consumer_data
.need_update
= 0;
2306 pthread_mutex_unlock(&consumer_data
.lock
);
2308 /* No FDs and consumer_quit, consumer_cleanup the thread */
2309 if (nb_fd
== 0 && consumer_quit
== 1) {
2312 /* poll on the array of fds */
2314 DBG("polling on %d fd", nb_fd
+ 1);
2315 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2316 DBG("poll num_rdy : %d", num_rdy
);
2317 if (num_rdy
== -1) {
2319 * Restart interrupted system call.
2321 if (errno
== EINTR
) {
2324 PERROR("Poll error");
2325 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2327 } else if (num_rdy
== 0) {
2328 DBG("Polling thread timed out");
2333 * If the consumer_data_pipe triggered poll go directly to the
2334 * beginning of the loop to update the array. We want to prioritize
2335 * array update over low-priority reads.
2337 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2338 ssize_t pipe_readlen
;
2340 DBG("consumer_data_pipe wake up");
2341 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2342 &new_stream
, sizeof(new_stream
));
2343 if (pipe_readlen
< 0) {
2344 ERR("Consumer data pipe ret %ld", pipe_readlen
);
2345 /* Continue so we can at least handle the current stream(s). */
2350 * If the stream is NULL, just ignore it. It's also possible that
2351 * the sessiond poll thread changed the consumer_quit state and is
2352 * waking us up to test it.
2354 if (new_stream
== NULL
) {
2355 validate_endpoint_status_data_stream();
2359 ret
= add_stream(new_stream
, data_ht
);
2361 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
2364 * At this point, if the add_stream fails, it is not in the
2365 * hash table thus passing the NULL value here.
2367 consumer_del_stream(new_stream
, NULL
);
2370 /* Continue to update the local streams and handle prio ones */
2374 /* Take care of high priority channels first. */
2375 for (i
= 0; i
< nb_fd
; i
++) {
2376 if (local_stream
[i
] == NULL
) {
2379 if (pollfd
[i
].revents
& POLLPRI
) {
2380 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2382 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2383 /* it's ok to have an unavailable sub-buffer */
2384 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2385 /* Clean the stream and free it. */
2386 consumer_del_stream(local_stream
[i
], data_ht
);
2387 local_stream
[i
] = NULL
;
2388 } else if (len
> 0) {
2389 local_stream
[i
]->data_read
= 1;
2395 * If we read high prio channel in this loop, try again
2396 * for more high prio data.
2402 /* Take care of low priority channels. */
2403 for (i
= 0; i
< nb_fd
; i
++) {
2404 if (local_stream
[i
] == NULL
) {
2407 if ((pollfd
[i
].revents
& POLLIN
) ||
2408 local_stream
[i
]->hangup_flush_done
) {
2409 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2410 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2411 /* it's ok to have an unavailable sub-buffer */
2412 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2413 /* Clean the stream and free it. */
2414 consumer_del_stream(local_stream
[i
], data_ht
);
2415 local_stream
[i
] = NULL
;
2416 } else if (len
> 0) {
2417 local_stream
[i
]->data_read
= 1;
2422 /* Handle hangup and errors */
2423 for (i
= 0; i
< nb_fd
; i
++) {
2424 if (local_stream
[i
] == NULL
) {
2427 if (!local_stream
[i
]->hangup_flush_done
2428 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2429 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2430 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2431 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2433 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2434 /* Attempt read again, for the data we just flushed. */
2435 local_stream
[i
]->data_read
= 1;
2438 * If the poll flag is HUP/ERR/NVAL and we have
2439 * read no data in this pass, we can remove the
2440 * stream from its hash table.
2442 if ((pollfd
[i
].revents
& POLLHUP
)) {
2443 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2444 if (!local_stream
[i
]->data_read
) {
2445 consumer_del_stream(local_stream
[i
], data_ht
);
2446 local_stream
[i
] = NULL
;
2449 } else if (pollfd
[i
].revents
& POLLERR
) {
2450 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2451 if (!local_stream
[i
]->data_read
) {
2452 consumer_del_stream(local_stream
[i
], data_ht
);
2453 local_stream
[i
] = NULL
;
2456 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2457 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2458 if (!local_stream
[i
]->data_read
) {
2459 consumer_del_stream(local_stream
[i
], data_ht
);
2460 local_stream
[i
] = NULL
;
2464 if (local_stream
[i
] != NULL
) {
2465 local_stream
[i
]->data_read
= 0;
2470 DBG("polling thread exiting");
2475 * Close the write side of the pipe so epoll_wait() in
2476 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2477 * read side of the pipe. If we close them both, epoll_wait strangely does
2478 * not return and could create a endless wait period if the pipe is the
2479 * only tracked fd in the poll set. The thread will take care of closing
2482 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2484 destroy_data_stream_ht(data_ht
);
2486 rcu_unregister_thread();
2491 * Close wake-up end of each stream belonging to the channel. This will
2492 * allow the poll() on the stream read-side to detect when the
2493 * write-side (application) finally closes them.
2496 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2498 struct lttng_ht
*ht
;
2499 struct lttng_consumer_stream
*stream
;
2500 struct lttng_ht_iter iter
;
2502 ht
= consumer_data
.stream_per_chan_id_ht
;
2505 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2506 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2507 ht
->match_fct
, &channel
->key
,
2508 &iter
.iter
, stream
, node_channel_id
.node
) {
2510 * Protect against teardown with mutex.
2512 pthread_mutex_lock(&stream
->lock
);
2513 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2516 switch (consumer_data
.type
) {
2517 case LTTNG_CONSUMER_KERNEL
:
2519 case LTTNG_CONSUMER32_UST
:
2520 case LTTNG_CONSUMER64_UST
:
2522 * Note: a mutex is taken internally within
2523 * liblttng-ust-ctl to protect timer wakeup_fd
2524 * use from concurrent close.
2526 lttng_ustconsumer_close_stream_wakeup(stream
);
2529 ERR("Unknown consumer_data type");
2533 pthread_mutex_unlock(&stream
->lock
);
2538 static void destroy_channel_ht(struct lttng_ht
*ht
)
2540 struct lttng_ht_iter iter
;
2541 struct lttng_consumer_channel
*channel
;
2549 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2550 ret
= lttng_ht_del(ht
, &iter
);
2555 lttng_ht_destroy(ht
);
2559 * This thread polls the channel fds to detect when they are being
2560 * closed. It closes all related streams if the channel is detected as
2561 * closed. It is currently only used as a shim layer for UST because the
2562 * consumerd needs to keep the per-stream wakeup end of pipes open for
2565 void *consumer_thread_channel_poll(void *data
)
2568 uint32_t revents
, nb_fd
;
2569 struct lttng_consumer_channel
*chan
= NULL
;
2570 struct lttng_ht_iter iter
;
2571 struct lttng_ht_node_u64
*node
;
2572 struct lttng_poll_event events
;
2573 struct lttng_consumer_local_data
*ctx
= data
;
2574 struct lttng_ht
*channel_ht
;
2576 rcu_register_thread();
2578 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2580 /* ENOMEM at this point. Better to bail out. */
2584 DBG("Thread channel poll started");
2586 /* Size is set to 1 for the consumer_channel pipe */
2587 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2589 ERR("Poll set creation failed");
2593 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2599 DBG("Channel main loop started");
2602 /* Only the channel pipe is set */
2603 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2608 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2609 ret
= lttng_poll_wait(&events
, -1);
2610 DBG("Channel event catched in thread");
2612 if (errno
== EINTR
) {
2613 ERR("Poll EINTR catched");
2621 /* From here, the event is a channel wait fd */
2622 for (i
= 0; i
< nb_fd
; i
++) {
2623 revents
= LTTNG_POLL_GETEV(&events
, i
);
2624 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2626 /* Just don't waste time if no returned events for the fd */
2630 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2631 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2632 DBG("Channel thread pipe hung up");
2634 * Remove the pipe from the poll set and continue the loop
2635 * since their might be data to consume.
2637 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2639 } else if (revents
& LPOLLIN
) {
2640 enum consumer_channel_action action
;
2643 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2645 ERR("Error reading channel pipe");
2650 case CONSUMER_CHANNEL_ADD
:
2651 DBG("Adding channel %d to poll set",
2654 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2657 lttng_ht_add_unique_u64(channel_ht
,
2658 &chan
->wait_fd_node
);
2660 /* Add channel to the global poll events list */
2661 lttng_poll_add(&events
, chan
->wait_fd
,
2662 LPOLLIN
| LPOLLPRI
);
2664 case CONSUMER_CHANNEL_DEL
:
2666 struct lttng_consumer_stream
*stream
, *stmp
;
2669 chan
= consumer_find_channel(key
);
2672 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2675 lttng_poll_del(&events
, chan
->wait_fd
);
2676 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2677 ret
= lttng_ht_del(channel_ht
, &iter
);
2679 consumer_close_channel_streams(chan
);
2681 switch (consumer_data
.type
) {
2682 case LTTNG_CONSUMER_KERNEL
:
2684 case LTTNG_CONSUMER32_UST
:
2685 case LTTNG_CONSUMER64_UST
:
2686 /* Delete streams that might have been left in the stream list. */
2687 cds_list_for_each_entry_safe(stream
, stmp
, &chan
->streams
.head
,
2689 cds_list_del(&stream
->send_node
);
2690 lttng_ustconsumer_del_stream(stream
);
2691 uatomic_sub(&stream
->chan
->refcount
, 1);
2692 assert(&chan
->refcount
);
2697 ERR("Unknown consumer_data type");
2702 * Release our own refcount. Force channel deletion even if
2703 * streams were not initialized.
2705 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2706 consumer_del_channel(chan
);
2711 case CONSUMER_CHANNEL_QUIT
:
2713 * Remove the pipe from the poll set and continue the loop
2714 * since their might be data to consume.
2716 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2719 ERR("Unknown action");
2724 /* Handle other stream */
2730 uint64_t tmp_id
= (uint64_t) pollfd
;
2732 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2734 node
= lttng_ht_iter_get_node_u64(&iter
);
2737 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2740 /* Check for error event */
2741 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2742 DBG("Channel fd %d is hup|err.", pollfd
);
2744 lttng_poll_del(&events
, chan
->wait_fd
);
2745 ret
= lttng_ht_del(channel_ht
, &iter
);
2747 assert(cds_list_empty(&chan
->streams
.head
));
2748 consumer_close_channel_streams(chan
);
2750 /* Release our own refcount */
2751 if (!uatomic_sub_return(&chan
->refcount
, 1)
2752 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2753 consumer_del_channel(chan
);
2757 /* Release RCU lock for the channel looked up */
2763 lttng_poll_clean(&events
);
2765 destroy_channel_ht(channel_ht
);
2767 DBG("Channel poll thread exiting");
2768 rcu_unregister_thread();
2772 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2773 struct pollfd
*sockpoll
, int client_socket
)
2780 if (lttng_consumer_poll_socket(sockpoll
) < 0) {
2784 DBG("Metadata connection on client_socket");
2786 /* Blocking call, waiting for transmission */
2787 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2788 if (ctx
->consumer_metadata_socket
< 0) {
2789 WARN("On accept metadata");
2800 * This thread listens on the consumerd socket and receives the file
2801 * descriptors from the session daemon.
2803 void *consumer_thread_sessiond_poll(void *data
)
2805 int sock
= -1, client_socket
, ret
;
2807 * structure to poll for incoming data on communication socket avoids
2808 * making blocking sockets.
2810 struct pollfd consumer_sockpoll
[2];
2811 struct lttng_consumer_local_data
*ctx
= data
;
2813 rcu_register_thread();
2815 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2816 unlink(ctx
->consumer_command_sock_path
);
2817 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2818 if (client_socket
< 0) {
2819 ERR("Cannot create command socket");
2823 ret
= lttcomm_listen_unix_sock(client_socket
);
2828 DBG("Sending ready command to lttng-sessiond");
2829 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2830 /* return < 0 on error, but == 0 is not fatal */
2832 ERR("Error sending ready command to lttng-sessiond");
2836 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2837 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2838 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2839 consumer_sockpoll
[1].fd
= client_socket
;
2840 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2842 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2845 DBG("Connection on client_socket");
2847 /* Blocking call, waiting for transmission */
2848 sock
= lttcomm_accept_unix_sock(client_socket
);
2855 * Setup metadata socket which is the second socket connection on the
2856 * command unix socket.
2858 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
2863 /* This socket is not useful anymore. */
2864 ret
= close(client_socket
);
2866 PERROR("close client_socket");
2870 /* update the polling structure to poll on the established socket */
2871 consumer_sockpoll
[1].fd
= sock
;
2872 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2875 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2878 DBG("Incoming command on sock");
2879 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2880 if (ret
== -ENOENT
) {
2881 DBG("Received STOP command");
2886 * This could simply be a session daemon quitting. Don't output
2889 DBG("Communication interrupted on command socket");
2892 if (consumer_quit
) {
2893 DBG("consumer_thread_receive_fds received quit from signal");
2896 DBG("received command on sock");
2899 DBG("Consumer thread sessiond poll exiting");
2902 * Close metadata streams since the producer is the session daemon which
2905 * NOTE: for now, this only applies to the UST tracer.
2907 lttng_consumer_close_metadata();
2910 * when all fds have hung up, the polling thread
2916 * Notify the data poll thread to poll back again and test the
2917 * consumer_quit state that we just set so to quit gracefully.
2919 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
2921 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
2923 /* Cleaning up possibly open sockets. */
2927 PERROR("close sock sessiond poll");
2930 if (client_socket
>= 0) {
2931 ret
= close(client_socket
);
2933 PERROR("close client_socket sessiond poll");
2937 rcu_unregister_thread();
2941 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2942 struct lttng_consumer_local_data
*ctx
)
2946 pthread_mutex_lock(&stream
->lock
);
2948 switch (consumer_data
.type
) {
2949 case LTTNG_CONSUMER_KERNEL
:
2950 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
2952 case LTTNG_CONSUMER32_UST
:
2953 case LTTNG_CONSUMER64_UST
:
2954 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2957 ERR("Unknown consumer_data type");
2963 pthread_mutex_unlock(&stream
->lock
);
2967 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2969 switch (consumer_data
.type
) {
2970 case LTTNG_CONSUMER_KERNEL
:
2971 return lttng_kconsumer_on_recv_stream(stream
);
2972 case LTTNG_CONSUMER32_UST
:
2973 case LTTNG_CONSUMER64_UST
:
2974 return lttng_ustconsumer_on_recv_stream(stream
);
2976 ERR("Unknown consumer_data type");
2983 * Allocate and set consumer data hash tables.
2985 void lttng_consumer_init(void)
2987 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2988 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2989 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2990 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2994 * Process the ADD_RELAYD command receive by a consumer.
2996 * This will create a relayd socket pair and add it to the relayd hash table.
2997 * The caller MUST acquire a RCU read side lock before calling it.
2999 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
3000 struct lttng_consumer_local_data
*ctx
, int sock
,
3001 struct pollfd
*consumer_sockpoll
,
3002 struct lttcomm_relayd_sock
*relayd_sock
, unsigned int sessiond_id
)
3004 int fd
= -1, ret
= -1, relayd_created
= 0;
3005 enum lttng_error_code ret_code
= LTTNG_OK
;
3006 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3009 assert(relayd_sock
);
3011 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
3013 /* Get relayd reference if exists. */
3014 relayd
= consumer_find_relayd(net_seq_idx
);
3015 if (relayd
== NULL
) {
3016 /* Not found. Allocate one. */
3017 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3018 if (relayd
== NULL
) {
3019 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3022 relayd
->sessiond_session_id
= (uint64_t) sessiond_id
;
3027 * This code path MUST continue to the consumer send status message to
3028 * we can notify the session daemon and continue our work without
3029 * killing everything.
3033 /* First send a status message before receiving the fds. */
3034 ret
= consumer_send_status_msg(sock
, ret_code
);
3035 if (ret
< 0 || ret_code
!= LTTNG_OK
) {
3036 /* Somehow, the session daemon is not responding anymore. */
3040 /* Poll on consumer socket. */
3041 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
3042 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3047 /* Get relayd socket from session daemon */
3048 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3049 if (ret
!= sizeof(fd
)) {
3050 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3052 fd
= -1; /* Just in case it gets set with an invalid value. */
3055 * Failing to receive FDs might indicate a major problem such as
3056 * reaching a fd limit during the receive where the kernel returns a
3057 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3058 * don't take any chances and stop everything.
3060 * XXX: Feature request #558 will fix that and avoid this possible
3061 * issue when reaching the fd limit.
3063 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3066 * This code path MUST continue to the consumer send status message so
3067 * we can send the error to the thread expecting a reply. The above
3068 * call will make everything stop.
3072 /* We have the fds without error. Send status back. */
3073 ret
= consumer_send_status_msg(sock
, ret_code
);
3074 if (ret
< 0 || ret_code
!= LTTNG_OK
) {
3075 /* Somehow, the session daemon is not responding anymore. */
3079 /* Copy socket information and received FD */
3080 switch (sock_type
) {
3081 case LTTNG_STREAM_CONTROL
:
3082 /* Copy received lttcomm socket */
3083 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3084 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3085 /* Immediately try to close the created socket if valid. */
3086 if (relayd
->control_sock
.sock
.fd
>= 0) {
3087 if (close(relayd
->control_sock
.sock
.fd
)) {
3088 PERROR("close relayd control socket");
3091 /* Handle create_sock error. */
3096 /* Assign new file descriptor */
3097 relayd
->control_sock
.sock
.fd
= fd
;
3098 fd
= -1; /* For error path */
3099 /* Assign version values. */
3100 relayd
->control_sock
.major
= relayd_sock
->major
;
3101 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3104 * Create a session on the relayd and store the returned id. Lock the
3105 * control socket mutex if the relayd was NOT created before.
3107 if (!relayd_created
) {
3108 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3110 ret
= relayd_create_session(&relayd
->control_sock
,
3111 &relayd
->relayd_session_id
);
3112 if (!relayd_created
) {
3113 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3117 * Close all sockets of a relayd object. It will be freed if it was
3118 * created at the error code path or else it will be garbage
3121 (void) relayd_close(&relayd
->control_sock
);
3122 (void) relayd_close(&relayd
->data_sock
);
3127 case LTTNG_STREAM_DATA
:
3128 /* Copy received lttcomm socket */
3129 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3130 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3131 /* Immediately try to close the created socket if valid. */
3132 if (relayd
->data_sock
.sock
.fd
>= 0) {
3133 if (close(relayd
->data_sock
.sock
.fd
)) {
3134 PERROR("close relayd data socket");
3137 /* Handle create_sock error. */
3142 /* Assign new file descriptor */
3143 relayd
->data_sock
.sock
.fd
= fd
;
3144 fd
= -1; /* for eventual error paths */
3145 /* Assign version values. */
3146 relayd
->data_sock
.major
= relayd_sock
->major
;
3147 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3150 ERR("Unknown relayd socket type (%d)", sock_type
);
3155 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3156 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3157 relayd
->net_seq_idx
, fd
);
3160 * Add relayd socket pair to consumer data hashtable. If object already
3161 * exists or on error, the function gracefully returns.
3169 /* Close received socket if valid. */
3172 PERROR("close received socket");
3176 if (relayd_created
) {
3184 * Try to lock the stream mutex.
3186 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3188 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3195 * Try to lock the stream mutex. On failure, we know that the stream is
3196 * being used else where hence there is data still being extracted.
3198 ret
= pthread_mutex_trylock(&stream
->lock
);
3200 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3212 * Search for a relayd associated to the session id and return the reference.
3214 * A rcu read side lock MUST be acquire before calling this function and locked
3215 * until the relayd object is no longer necessary.
3217 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3219 struct lttng_ht_iter iter
;
3220 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3222 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3223 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3226 * Check by sessiond id which is unique here where the relayd session
3227 * id might not be when having multiple relayd.
3229 if (relayd
->sessiond_session_id
== id
) {
3230 /* Found the relayd. There can be only one per id. */
3242 * Check if for a given session id there is still data needed to be extract
3245 * Return 1 if data is pending or else 0 meaning ready to be read.
3247 int consumer_data_pending(uint64_t id
)
3250 struct lttng_ht_iter iter
;
3251 struct lttng_ht
*ht
;
3252 struct lttng_consumer_stream
*stream
;
3253 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3254 int (*data_pending
)(struct lttng_consumer_stream
*);
3256 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3259 pthread_mutex_lock(&consumer_data
.lock
);
3261 switch (consumer_data
.type
) {
3262 case LTTNG_CONSUMER_KERNEL
:
3263 data_pending
= lttng_kconsumer_data_pending
;
3265 case LTTNG_CONSUMER32_UST
:
3266 case LTTNG_CONSUMER64_UST
:
3267 data_pending
= lttng_ustconsumer_data_pending
;
3270 ERR("Unknown consumer data type");
3274 /* Ease our life a bit */
3275 ht
= consumer_data
.stream_list_ht
;
3277 relayd
= find_relayd_by_session_id(id
);
3279 /* Send init command for data pending. */
3280 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3281 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3282 relayd
->relayd_session_id
);
3283 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3285 /* Communication error thus the relayd so no data pending. */
3286 goto data_not_pending
;
3290 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3291 ht
->hash_fct(&id
, lttng_ht_seed
),
3293 &iter
.iter
, stream
, node_session_id
.node
) {
3294 /* If this call fails, the stream is being used hence data pending. */
3295 ret
= stream_try_lock(stream
);
3301 * A removed node from the hash table indicates that the stream has
3302 * been deleted thus having a guarantee that the buffers are closed
3303 * on the consumer side. However, data can still be transmitted
3304 * over the network so don't skip the relayd check.
3306 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3308 /* Check the stream if there is data in the buffers. */
3309 ret
= data_pending(stream
);
3311 pthread_mutex_unlock(&stream
->lock
);
3318 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3319 if (stream
->metadata_flag
) {
3320 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3321 stream
->relayd_stream_id
);
3323 ret
= relayd_data_pending(&relayd
->control_sock
,
3324 stream
->relayd_stream_id
,
3325 stream
->next_net_seq_num
- 1);
3327 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3329 pthread_mutex_unlock(&stream
->lock
);
3333 pthread_mutex_unlock(&stream
->lock
);
3337 unsigned int is_data_inflight
= 0;
3339 /* Send init command for data pending. */
3340 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3341 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3342 relayd
->relayd_session_id
, &is_data_inflight
);
3343 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3345 goto data_not_pending
;
3347 if (is_data_inflight
) {
3353 * Finding _no_ node in the hash table and no inflight data means that the
3354 * stream(s) have been removed thus data is guaranteed to be available for
3355 * analysis from the trace files.
3359 /* Data is available to be read by a viewer. */
3360 pthread_mutex_unlock(&consumer_data
.lock
);
3365 /* Data is still being extracted from buffers. */
3366 pthread_mutex_unlock(&consumer_data
.lock
);
3372 * Send a ret code status message to the sessiond daemon.
3374 * Return the sendmsg() return value.
3376 int consumer_send_status_msg(int sock
, int ret_code
)
3378 struct lttcomm_consumer_status_msg msg
;
3380 msg
.ret_code
= ret_code
;
3382 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3386 * Send a channel status message to the sessiond daemon.
3388 * Return the sendmsg() return value.
3390 int consumer_send_status_channel(int sock
,
3391 struct lttng_consumer_channel
*channel
)
3393 struct lttcomm_consumer_status_channel msg
;
3398 msg
.ret_code
= -LTTNG_ERR_UST_CHAN_FAIL
;
3400 msg
.ret_code
= LTTNG_OK
;
3401 msg
.key
= channel
->key
;
3402 msg
.stream_count
= channel
->streams
.count
;
3405 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));