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;
809 case LTTNG_EVENT_SPLICE
:
810 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
812 case LTTNG_EVENT_MMAP
:
813 channel
->output
= CONSUMER_CHANNEL_MMAP
;
816 ERR("Allocate channel output unknown %d", output
);
822 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
823 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
825 strncpy(channel
->name
, name
, sizeof(channel
->name
));
826 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
828 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
830 channel
->wait_fd
= -1;
832 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
833 CDS_INIT_LIST_HEAD(&channel
->stream_no_monitor_list
.head
);
835 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
842 * Add a channel to the global list protected by a mutex.
844 * On success 0 is returned else a negative value.
846 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
847 struct lttng_consumer_local_data
*ctx
)
850 struct lttng_ht_node_u64
*node
;
851 struct lttng_ht_iter iter
;
853 pthread_mutex_lock(&consumer_data
.lock
);
856 lttng_ht_lookup(consumer_data
.channel_ht
, &channel
->key
, &iter
);
857 node
= lttng_ht_iter_get_node_u64(&iter
);
859 /* Channel already exist. Ignore the insertion */
860 ERR("Consumer add channel key %" PRIu64
" already exists!",
866 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
870 pthread_mutex_unlock(&consumer_data
.lock
);
872 if (!ret
&& channel
->wait_fd
!= -1 &&
873 channel
->metadata_stream
== NULL
) {
874 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
880 * Allocate the pollfd structure and the local view of the out fds to avoid
881 * doing a lookup in the linked list and concurrency issues when writing is
882 * needed. Called with consumer_data.lock held.
884 * Returns the number of fds in the structures.
886 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
887 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
891 struct lttng_ht_iter iter
;
892 struct lttng_consumer_stream
*stream
;
897 assert(local_stream
);
899 DBG("Updating poll fd array");
901 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
903 * Only active streams with an active end point can be added to the
904 * poll set and local stream storage of the thread.
906 * There is a potential race here for endpoint_status to be updated
907 * just after the check. However, this is OK since the stream(s) will
908 * be deleted once the thread is notified that the end point state has
909 * changed where this function will be called back again.
911 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
912 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
916 * This clobbers way too much the debug output. Uncomment that if you
917 * need it for debugging purposes.
919 * DBG("Active FD %d", stream->wait_fd);
921 (*pollfd
)[i
].fd
= stream
->wait_fd
;
922 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
923 local_stream
[i
] = stream
;
929 * Insert the consumer_data_pipe at the end of the array and don't
930 * increment i so nb_fd is the number of real FD.
932 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
933 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
938 * Poll on the should_quit pipe and the command socket return -1 on error and
939 * should exit, 0 if data is available on the command socket
941 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
946 num_rdy
= poll(consumer_sockpoll
, 2, -1);
949 * Restart interrupted system call.
951 if (errno
== EINTR
) {
954 PERROR("Poll error");
957 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
958 DBG("consumer_should_quit wake up");
968 * Set the error socket.
970 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
973 ctx
->consumer_error_socket
= sock
;
977 * Set the command socket path.
979 void lttng_consumer_set_command_sock_path(
980 struct lttng_consumer_local_data
*ctx
, char *sock
)
982 ctx
->consumer_command_sock_path
= sock
;
986 * Send return code to the session daemon.
987 * If the socket is not defined, we return 0, it is not a fatal error
989 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
991 if (ctx
->consumer_error_socket
> 0) {
992 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
993 sizeof(enum lttcomm_sessiond_command
));
1000 * Close all the tracefiles and stream fds and MUST be called when all
1001 * instances are destroyed i.e. when all threads were joined and are ended.
1003 void lttng_consumer_cleanup(void)
1005 struct lttng_ht_iter iter
;
1006 struct lttng_consumer_channel
*channel
;
1010 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1012 consumer_del_channel(channel
);
1017 lttng_ht_destroy(consumer_data
.channel_ht
);
1019 cleanup_relayd_ht();
1021 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1024 * This HT contains streams that are freed by either the metadata thread or
1025 * the data thread so we do *nothing* on the hash table and simply destroy
1028 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1032 * Called from signal handler.
1034 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1039 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1040 } while (ret
< 0 && errno
== EINTR
);
1041 if (ret
< 0 || ret
!= 1) {
1042 PERROR("write consumer quit");
1045 DBG("Consumer flag that it should quit");
1048 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1051 int outfd
= stream
->out_fd
;
1054 * This does a blocking write-and-wait on any page that belongs to the
1055 * subbuffer prior to the one we just wrote.
1056 * Don't care about error values, as these are just hints and ways to
1057 * limit the amount of page cache used.
1059 if (orig_offset
< stream
->max_sb_size
) {
1062 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1063 stream
->max_sb_size
,
1064 SYNC_FILE_RANGE_WAIT_BEFORE
1065 | SYNC_FILE_RANGE_WRITE
1066 | SYNC_FILE_RANGE_WAIT_AFTER
);
1068 * Give hints to the kernel about how we access the file:
1069 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1072 * We need to call fadvise again after the file grows because the
1073 * kernel does not seem to apply fadvise to non-existing parts of the
1076 * Call fadvise _after_ having waited for the page writeback to
1077 * complete because the dirty page writeback semantic is not well
1078 * defined. So it can be expected to lead to lower throughput in
1081 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1082 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1086 * Initialise the necessary environnement :
1087 * - create a new context
1088 * - create the poll_pipe
1089 * - create the should_quit pipe (for signal handler)
1090 * - create the thread pipe (for splice)
1092 * Takes a function pointer as argument, this function is called when data is
1093 * available on a buffer. This function is responsible to do the
1094 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1095 * buffer configuration and then kernctl_put_next_subbuf at the end.
1097 * Returns a pointer to the new context or NULL on error.
1099 struct lttng_consumer_local_data
*lttng_consumer_create(
1100 enum lttng_consumer_type type
,
1101 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1102 struct lttng_consumer_local_data
*ctx
),
1103 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1104 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1105 int (*update_stream
)(int stream_key
, uint32_t state
))
1108 struct lttng_consumer_local_data
*ctx
;
1110 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1111 consumer_data
.type
== type
);
1112 consumer_data
.type
= type
;
1114 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1116 PERROR("allocating context");
1120 ctx
->consumer_error_socket
= -1;
1121 ctx
->consumer_metadata_socket
= -1;
1122 /* assign the callbacks */
1123 ctx
->on_buffer_ready
= buffer_ready
;
1124 ctx
->on_recv_channel
= recv_channel
;
1125 ctx
->on_recv_stream
= recv_stream
;
1126 ctx
->on_update_stream
= update_stream
;
1128 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1129 if (!ctx
->consumer_data_pipe
) {
1130 goto error_poll_pipe
;
1133 ret
= pipe(ctx
->consumer_should_quit
);
1135 PERROR("Error creating recv pipe");
1136 goto error_quit_pipe
;
1139 ret
= pipe(ctx
->consumer_thread_pipe
);
1141 PERROR("Error creating thread pipe");
1142 goto error_thread_pipe
;
1145 ret
= pipe(ctx
->consumer_channel_pipe
);
1147 PERROR("Error creating channel pipe");
1148 goto error_channel_pipe
;
1151 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1152 if (!ctx
->consumer_metadata_pipe
) {
1153 goto error_metadata_pipe
;
1156 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1158 goto error_splice_pipe
;
1164 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1165 error_metadata_pipe
:
1166 utils_close_pipe(ctx
->consumer_channel_pipe
);
1168 utils_close_pipe(ctx
->consumer_thread_pipe
);
1170 utils_close_pipe(ctx
->consumer_should_quit
);
1172 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1180 * Close all fds associated with the instance and free the context.
1182 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1186 DBG("Consumer destroying it. Closing everything.");
1188 ret
= close(ctx
->consumer_error_socket
);
1192 ret
= close(ctx
->consumer_metadata_socket
);
1196 utils_close_pipe(ctx
->consumer_thread_pipe
);
1197 utils_close_pipe(ctx
->consumer_channel_pipe
);
1198 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1199 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1200 utils_close_pipe(ctx
->consumer_should_quit
);
1201 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1203 unlink(ctx
->consumer_command_sock_path
);
1208 * Write the metadata stream id on the specified file descriptor.
1210 static int write_relayd_metadata_id(int fd
,
1211 struct lttng_consumer_stream
*stream
,
1212 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1215 struct lttcomm_relayd_metadata_payload hdr
;
1217 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1218 hdr
.padding_size
= htobe32(padding
);
1220 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1221 } while (ret
< 0 && errno
== EINTR
);
1222 if (ret
< 0 || ret
!= sizeof(hdr
)) {
1224 * This error means that the fd's end is closed so ignore the perror
1225 * not to clubber the error output since this can happen in a normal
1228 if (errno
!= EPIPE
) {
1229 PERROR("write metadata stream id");
1231 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1233 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1234 * handle writting the missing part so report that as an error and
1235 * don't lie to the caller.
1240 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1241 stream
->relayd_stream_id
, padding
);
1248 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1249 * core function for writing trace buffers to either the local filesystem or
1252 * It must be called with the stream lock held.
1254 * Careful review MUST be put if any changes occur!
1256 * Returns the number of bytes written
1258 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1259 struct lttng_consumer_local_data
*ctx
,
1260 struct lttng_consumer_stream
*stream
, unsigned long len
,
1261 unsigned long padding
)
1263 unsigned long mmap_offset
;
1265 ssize_t ret
= 0, written
= 0;
1266 off_t orig_offset
= stream
->out_fd_offset
;
1267 /* Default is on the disk */
1268 int outfd
= stream
->out_fd
;
1269 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1270 unsigned int relayd_hang_up
= 0;
1272 /* RCU lock for the relayd pointer */
1275 /* Flag that the current stream if set for network streaming. */
1276 if (stream
->net_seq_idx
!= -1) {
1277 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1278 if (relayd
== NULL
) {
1283 /* get the offset inside the fd to mmap */
1284 switch (consumer_data
.type
) {
1285 case LTTNG_CONSUMER_KERNEL
:
1286 mmap_base
= stream
->mmap_base
;
1287 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1289 case LTTNG_CONSUMER32_UST
:
1290 case LTTNG_CONSUMER64_UST
:
1291 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1293 ERR("read mmap get mmap base for stream %s", stream
->name
);
1297 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1301 ERR("Unknown consumer_data type");
1306 PERROR("tracer ctl get_mmap_read_offset");
1311 /* Handle stream on the relayd if the output is on the network */
1313 unsigned long netlen
= len
;
1316 * Lock the control socket for the complete duration of the function
1317 * since from this point on we will use the socket.
1319 if (stream
->metadata_flag
) {
1320 /* Metadata requires the control socket. */
1321 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1322 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1325 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1327 /* Use the returned socket. */
1330 /* Write metadata stream id before payload */
1331 if (stream
->metadata_flag
) {
1332 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1335 /* Socket operation failed. We consider the relayd dead */
1336 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1344 /* Socket operation failed. We consider the relayd dead */
1345 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1349 /* Else, use the default set before which is the filesystem. */
1352 /* No streaming, we have to set the len with the full padding */
1356 * Check if we need to change the tracefile before writing the packet.
1358 if (stream
->chan
->tracefile_size
> 0 &&
1359 (stream
->tracefile_size_current
+ len
) >
1360 stream
->chan
->tracefile_size
) {
1361 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1362 stream
->name
, stream
->chan
->tracefile_size
,
1363 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1364 stream
->out_fd
, &(stream
->tracefile_count_current
));
1366 ERR("Rotating output file");
1369 outfd
= stream
->out_fd
= ret
;
1370 /* Reset current size because we just perform a rotation. */
1371 stream
->tracefile_size_current
= 0;
1373 stream
->tracefile_size_current
+= len
;
1378 ret
= write(outfd
, mmap_base
+ mmap_offset
, len
);
1379 } while (ret
< 0 && errno
== EINTR
);
1380 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1383 * This is possible if the fd is closed on the other side (outfd)
1384 * or any write problem. It can be verbose a bit for a normal
1385 * execution if for instance the relayd is stopped abruptly. This
1386 * can happen so set this to a DBG statement.
1388 DBG("Error in file write mmap");
1392 /* Socket operation failed. We consider the relayd dead */
1393 if (errno
== EPIPE
|| errno
== EINVAL
) {
1398 } else if (ret
> len
) {
1399 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1407 /* This call is useless on a socket so better save a syscall. */
1409 /* This won't block, but will start writeout asynchronously */
1410 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1411 SYNC_FILE_RANGE_WRITE
);
1412 stream
->out_fd_offset
+= ret
;
1416 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1420 * This is a special case that the relayd has closed its socket. Let's
1421 * cleanup the relayd object and all associated streams.
1423 if (relayd
&& relayd_hang_up
) {
1424 cleanup_relayd(relayd
, ctx
);
1428 /* Unlock only if ctrl socket used */
1429 if (relayd
&& stream
->metadata_flag
) {
1430 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1438 * Splice the data from the ring buffer to the tracefile.
1440 * It must be called with the stream lock held.
1442 * Returns the number of bytes spliced.
1444 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1445 struct lttng_consumer_local_data
*ctx
,
1446 struct lttng_consumer_stream
*stream
, unsigned long len
,
1447 unsigned long padding
)
1449 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1451 off_t orig_offset
= stream
->out_fd_offset
;
1452 int fd
= stream
->wait_fd
;
1453 /* Default is on the disk */
1454 int outfd
= stream
->out_fd
;
1455 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1457 unsigned int relayd_hang_up
= 0;
1459 switch (consumer_data
.type
) {
1460 case LTTNG_CONSUMER_KERNEL
:
1462 case LTTNG_CONSUMER32_UST
:
1463 case LTTNG_CONSUMER64_UST
:
1464 /* Not supported for user space tracing */
1467 ERR("Unknown consumer_data type");
1471 /* RCU lock for the relayd pointer */
1474 /* Flag that the current stream if set for network streaming. */
1475 if (stream
->net_seq_idx
!= -1) {
1476 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1477 if (relayd
== NULL
) {
1483 * Choose right pipe for splice. Metadata and trace data are handled by
1484 * different threads hence the use of two pipes in order not to race or
1485 * corrupt the written data.
1487 if (stream
->metadata_flag
) {
1488 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1490 splice_pipe
= ctx
->consumer_thread_pipe
;
1493 /* Write metadata stream id before payload */
1495 int total_len
= len
;
1497 if (stream
->metadata_flag
) {
1499 * Lock the control socket for the complete duration of the function
1500 * since from this point on we will use the socket.
1502 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1504 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1508 /* Socket operation failed. We consider the relayd dead */
1509 if (ret
== -EBADF
) {
1510 WARN("Remote relayd disconnected. Stopping");
1517 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1520 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1522 /* Use the returned socket. */
1525 /* Socket operation failed. We consider the relayd dead */
1526 if (ret
== -EBADF
) {
1527 WARN("Remote relayd disconnected. Stopping");
1534 /* No streaming, we have to set the len with the full padding */
1538 * Check if we need to change the tracefile before writing the packet.
1540 if (stream
->chan
->tracefile_size
> 0 &&
1541 (stream
->tracefile_size_current
+ len
) >
1542 stream
->chan
->tracefile_size
) {
1543 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1544 stream
->name
, stream
->chan
->tracefile_size
,
1545 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1546 stream
->out_fd
, &(stream
->tracefile_count_current
));
1548 ERR("Rotating output file");
1551 outfd
= stream
->out_fd
= ret
;
1552 /* Reset current size because we just perform a rotation. */
1553 stream
->tracefile_size_current
= 0;
1555 stream
->tracefile_size_current
+= len
;
1559 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1560 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1561 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1562 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1563 DBG("splice chan to pipe, ret %zd", ret_splice
);
1564 if (ret_splice
< 0) {
1565 PERROR("Error in relay splice");
1567 written
= ret_splice
;
1573 /* Handle stream on the relayd if the output is on the network */
1575 if (stream
->metadata_flag
) {
1576 size_t metadata_payload_size
=
1577 sizeof(struct lttcomm_relayd_metadata_payload
);
1579 /* Update counter to fit the spliced data */
1580 ret_splice
+= metadata_payload_size
;
1581 len
+= metadata_payload_size
;
1583 * We do this so the return value can match the len passed as
1584 * argument to this function.
1586 written
-= metadata_payload_size
;
1590 /* Splice data out */
1591 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1592 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1593 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1594 if (ret_splice
< 0) {
1595 PERROR("Error in file splice");
1597 written
= ret_splice
;
1599 /* Socket operation failed. We consider the relayd dead */
1600 if (errno
== EBADF
|| errno
== EPIPE
) {
1601 WARN("Remote relayd disconnected. Stopping");
1607 } else if (ret_splice
> len
) {
1609 PERROR("Wrote more data than requested %zd (len: %lu)",
1611 written
+= ret_splice
;
1617 /* This call is useless on a socket so better save a syscall. */
1619 /* This won't block, but will start writeout asynchronously */
1620 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1621 SYNC_FILE_RANGE_WRITE
);
1622 stream
->out_fd_offset
+= ret_splice
;
1624 written
+= ret_splice
;
1626 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1634 * This is a special case that the relayd has closed its socket. Let's
1635 * cleanup the relayd object and all associated streams.
1637 if (relayd
&& relayd_hang_up
) {
1638 cleanup_relayd(relayd
, ctx
);
1639 /* Skip splice error so the consumer does not fail */
1644 /* send the appropriate error description to sessiond */
1647 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1650 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1653 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1658 if (relayd
&& stream
->metadata_flag
) {
1659 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1667 * Take a snapshot for a specific fd
1669 * Returns 0 on success, < 0 on error
1671 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1673 switch (consumer_data
.type
) {
1674 case LTTNG_CONSUMER_KERNEL
:
1675 return lttng_kconsumer_take_snapshot(stream
);
1676 case LTTNG_CONSUMER32_UST
:
1677 case LTTNG_CONSUMER64_UST
:
1678 return lttng_ustconsumer_take_snapshot(stream
);
1680 ERR("Unknown consumer_data type");
1687 * Get the produced position
1689 * Returns 0 on success, < 0 on error
1691 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1694 switch (consumer_data
.type
) {
1695 case LTTNG_CONSUMER_KERNEL
:
1696 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1697 case LTTNG_CONSUMER32_UST
:
1698 case LTTNG_CONSUMER64_UST
:
1699 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1701 ERR("Unknown consumer_data type");
1707 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1708 int sock
, struct pollfd
*consumer_sockpoll
)
1710 switch (consumer_data
.type
) {
1711 case LTTNG_CONSUMER_KERNEL
:
1712 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1713 case LTTNG_CONSUMER32_UST
:
1714 case LTTNG_CONSUMER64_UST
:
1715 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1717 ERR("Unknown consumer_data type");
1724 * Iterate over all streams of the hashtable and free them properly.
1726 * WARNING: *MUST* be used with data stream only.
1728 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1730 struct lttng_ht_iter iter
;
1731 struct lttng_consumer_stream
*stream
;
1738 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1740 * Ignore return value since we are currently cleaning up so any error
1743 (void) consumer_del_stream(stream
, ht
);
1747 lttng_ht_destroy(ht
);
1751 * Iterate over all streams of the hashtable and free them properly.
1753 * XXX: Should not be only for metadata stream or else use an other name.
1755 static void destroy_stream_ht(struct lttng_ht
*ht
)
1757 struct lttng_ht_iter iter
;
1758 struct lttng_consumer_stream
*stream
;
1765 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1767 * Ignore return value since we are currently cleaning up so any error
1770 (void) consumer_del_metadata_stream(stream
, ht
);
1774 lttng_ht_destroy(ht
);
1777 void lttng_consumer_close_metadata(void)
1779 switch (consumer_data
.type
) {
1780 case LTTNG_CONSUMER_KERNEL
:
1782 * The Kernel consumer has a different metadata scheme so we don't
1783 * close anything because the stream will be closed by the session
1787 case LTTNG_CONSUMER32_UST
:
1788 case LTTNG_CONSUMER64_UST
:
1790 * Close all metadata streams. The metadata hash table is passed and
1791 * this call iterates over it by closing all wakeup fd. This is safe
1792 * because at this point we are sure that the metadata producer is
1793 * either dead or blocked.
1795 lttng_ustconsumer_close_metadata(metadata_ht
);
1798 ERR("Unknown consumer_data type");
1804 * Clean up a metadata stream and free its memory.
1806 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1807 struct lttng_ht
*ht
)
1810 struct lttng_ht_iter iter
;
1811 struct lttng_consumer_channel
*free_chan
= NULL
;
1812 struct consumer_relayd_sock_pair
*relayd
;
1816 * This call should NEVER receive regular stream. It must always be
1817 * metadata stream and this is crucial for data structure synchronization.
1819 assert(stream
->metadata_flag
);
1821 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1824 /* Means the stream was allocated but not successfully added */
1825 goto free_stream_rcu
;
1828 pthread_mutex_lock(&consumer_data
.lock
);
1829 pthread_mutex_lock(&stream
->lock
);
1831 switch (consumer_data
.type
) {
1832 case LTTNG_CONSUMER_KERNEL
:
1833 if (stream
->mmap_base
!= NULL
) {
1834 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1836 PERROR("munmap metadata stream");
1839 if (stream
->wait_fd
>= 0) {
1840 ret
= close(stream
->wait_fd
);
1842 PERROR("close kernel metadata wait_fd");
1846 case LTTNG_CONSUMER32_UST
:
1847 case LTTNG_CONSUMER64_UST
:
1848 lttng_ustconsumer_del_stream(stream
);
1851 ERR("Unknown consumer_data type");
1857 iter
.iter
.node
= &stream
->node
.node
;
1858 ret
= lttng_ht_del(ht
, &iter
);
1861 iter
.iter
.node
= &stream
->node_channel_id
.node
;
1862 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
1865 iter
.iter
.node
= &stream
->node_session_id
.node
;
1866 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1870 if (stream
->out_fd
>= 0) {
1871 ret
= close(stream
->out_fd
);
1877 /* Check and cleanup relayd */
1879 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1880 if (relayd
!= NULL
) {
1881 uatomic_dec(&relayd
->refcount
);
1882 assert(uatomic_read(&relayd
->refcount
) >= 0);
1884 /* Closing streams requires to lock the control socket. */
1885 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1886 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1887 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1888 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1890 DBG("Unable to close stream on the relayd. Continuing");
1892 * Continue here. There is nothing we can do for the relayd.
1893 * Chances are that the relayd has closed the socket so we just
1894 * continue cleaning up.
1898 /* Both conditions are met, we destroy the relayd. */
1899 if (uatomic_read(&relayd
->refcount
) == 0 &&
1900 uatomic_read(&relayd
->destroy_flag
)) {
1901 consumer_destroy_relayd(relayd
);
1906 /* Atomically decrement channel refcount since other threads can use it. */
1907 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
1908 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
1909 /* Go for channel deletion! */
1910 free_chan
= stream
->chan
;
1915 * Nullify the stream reference so it is not used after deletion. The
1916 * consumer data lock MUST be acquired before being able to check for a
1917 * NULL pointer value.
1919 stream
->chan
->metadata_stream
= NULL
;
1921 pthread_mutex_unlock(&stream
->lock
);
1922 pthread_mutex_unlock(&consumer_data
.lock
);
1925 consumer_del_channel(free_chan
);
1929 call_rcu(&stream
->node
.head
, free_stream_rcu
);
1933 * Action done with the metadata stream when adding it to the consumer internal
1934 * data structures to handle it.
1936 static int add_metadata_stream(struct lttng_consumer_stream
*stream
,
1937 struct lttng_ht
*ht
)
1940 struct consumer_relayd_sock_pair
*relayd
;
1941 struct lttng_ht_iter iter
;
1942 struct lttng_ht_node_u64
*node
;
1947 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
1949 pthread_mutex_lock(&consumer_data
.lock
);
1950 pthread_mutex_lock(&stream
->lock
);
1953 * From here, refcounts are updated so be _careful_ when returning an error
1960 * Lookup the stream just to make sure it does not exist in our internal
1961 * state. This should NEVER happen.
1963 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
1964 node
= lttng_ht_iter_get_node_u64(&iter
);
1967 /* Find relayd and, if one is found, increment refcount. */
1968 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1969 if (relayd
!= NULL
) {
1970 uatomic_inc(&relayd
->refcount
);
1974 * When nb_init_stream_left reaches 0, we don't need to trigger any action
1975 * in terms of destroying the associated channel, because the action that
1976 * causes the count to become 0 also causes a stream to be added. The
1977 * channel deletion will thus be triggered by the following removal of this
1980 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
1981 /* Increment refcount before decrementing nb_init_stream_left */
1983 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
1986 lttng_ht_add_unique_u64(ht
, &stream
->node
);
1988 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
1989 &stream
->node_channel_id
);
1992 * Add stream to the stream_list_ht of the consumer data. No need to steal
1993 * the key since the HT does not use it and we allow to add redundant keys
1996 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2000 pthread_mutex_unlock(&stream
->lock
);
2001 pthread_mutex_unlock(&consumer_data
.lock
);
2006 * Delete data stream that are flagged for deletion (endpoint_status).
2008 static void validate_endpoint_status_data_stream(void)
2010 struct lttng_ht_iter iter
;
2011 struct lttng_consumer_stream
*stream
;
2013 DBG("Consumer delete flagged data stream");
2016 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2017 /* Validate delete flag of the stream */
2018 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2021 /* Delete it right now */
2022 consumer_del_stream(stream
, data_ht
);
2028 * Delete metadata stream that are flagged for deletion (endpoint_status).
2030 static void validate_endpoint_status_metadata_stream(
2031 struct lttng_poll_event
*pollset
)
2033 struct lttng_ht_iter iter
;
2034 struct lttng_consumer_stream
*stream
;
2036 DBG("Consumer delete flagged metadata stream");
2041 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2042 /* Validate delete flag of the stream */
2043 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2047 * Remove from pollset so the metadata thread can continue without
2048 * blocking on a deleted stream.
2050 lttng_poll_del(pollset
, stream
->wait_fd
);
2052 /* Delete it right now */
2053 consumer_del_metadata_stream(stream
, metadata_ht
);
2059 * Thread polls on metadata file descriptor and write them on disk or on the
2062 void *consumer_thread_metadata_poll(void *data
)
2065 uint32_t revents
, nb_fd
;
2066 struct lttng_consumer_stream
*stream
= NULL
;
2067 struct lttng_ht_iter iter
;
2068 struct lttng_ht_node_u64
*node
;
2069 struct lttng_poll_event events
;
2070 struct lttng_consumer_local_data
*ctx
= data
;
2073 rcu_register_thread();
2075 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2077 /* ENOMEM at this point. Better to bail out. */
2081 DBG("Thread metadata poll started");
2083 /* Size is set to 1 for the consumer_metadata pipe */
2084 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2086 ERR("Poll set creation failed");
2090 ret
= lttng_poll_add(&events
,
2091 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2097 DBG("Metadata main loop started");
2100 /* Only the metadata pipe is set */
2101 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2106 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2107 ret
= lttng_poll_wait(&events
, -1);
2108 DBG("Metadata event catched in thread");
2110 if (errno
== EINTR
) {
2111 ERR("Poll EINTR catched");
2119 /* From here, the event is a metadata wait fd */
2120 for (i
= 0; i
< nb_fd
; i
++) {
2121 revents
= LTTNG_POLL_GETEV(&events
, i
);
2122 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2124 /* Just don't waste time if no returned events for the fd */
2129 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2130 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2131 DBG("Metadata thread pipe hung up");
2133 * Remove the pipe from the poll set and continue the loop
2134 * since their might be data to consume.
2136 lttng_poll_del(&events
,
2137 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2138 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2140 } else if (revents
& LPOLLIN
) {
2143 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2144 &stream
, sizeof(stream
));
2146 ERR("read metadata stream, ret: %ld", pipe_len
);
2148 * Continue here to handle the rest of the streams.
2153 /* A NULL stream means that the state has changed. */
2154 if (stream
== NULL
) {
2155 /* Check for deleted streams. */
2156 validate_endpoint_status_metadata_stream(&events
);
2160 DBG("Adding metadata stream %d to poll set",
2163 ret
= add_metadata_stream(stream
, metadata_ht
);
2165 ERR("Unable to add metadata stream");
2166 /* Stream was not setup properly. Continuing. */
2167 consumer_del_metadata_stream(stream
, NULL
);
2171 /* Add metadata stream to the global poll events list */
2172 lttng_poll_add(&events
, stream
->wait_fd
,
2173 LPOLLIN
| LPOLLPRI
);
2176 /* Handle other stream */
2182 uint64_t tmp_id
= (uint64_t) pollfd
;
2184 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2186 node
= lttng_ht_iter_get_node_u64(&iter
);
2189 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2192 /* Check for error event */
2193 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2194 DBG("Metadata fd %d is hup|err.", pollfd
);
2195 if (!stream
->hangup_flush_done
2196 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2197 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2198 DBG("Attempting to flush and consume the UST buffers");
2199 lttng_ustconsumer_on_stream_hangup(stream
);
2201 /* We just flushed the stream now read it. */
2203 len
= ctx
->on_buffer_ready(stream
, ctx
);
2205 * We don't check the return value here since if we get
2206 * a negative len, it means an error occured thus we
2207 * simply remove it from the poll set and free the
2213 lttng_poll_del(&events
, stream
->wait_fd
);
2215 * This call update the channel states, closes file descriptors
2216 * and securely free the stream.
2218 consumer_del_metadata_stream(stream
, metadata_ht
);
2219 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2220 /* Get the data out of the metadata file descriptor */
2221 DBG("Metadata available on fd %d", pollfd
);
2222 assert(stream
->wait_fd
== pollfd
);
2224 len
= ctx
->on_buffer_ready(stream
, ctx
);
2225 /* It's ok to have an unavailable sub-buffer */
2226 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2227 /* Clean up stream from consumer and free it. */
2228 lttng_poll_del(&events
, stream
->wait_fd
);
2229 consumer_del_metadata_stream(stream
, metadata_ht
);
2230 } else if (len
> 0) {
2231 stream
->data_read
= 1;
2235 /* Release RCU lock for the stream looked up */
2242 DBG("Metadata poll thread exiting");
2244 lttng_poll_clean(&events
);
2246 destroy_stream_ht(metadata_ht
);
2248 rcu_unregister_thread();
2253 * This thread polls the fds in the set to consume the data and write
2254 * it to tracefile if necessary.
2256 void *consumer_thread_data_poll(void *data
)
2258 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2259 struct pollfd
*pollfd
= NULL
;
2260 /* local view of the streams */
2261 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2262 /* local view of consumer_data.fds_count */
2264 struct lttng_consumer_local_data
*ctx
= data
;
2267 rcu_register_thread();
2269 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2270 if (data_ht
== NULL
) {
2271 /* ENOMEM at this point. Better to bail out. */
2275 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
2282 * the fds set has been updated, we need to update our
2283 * local array as well
2285 pthread_mutex_lock(&consumer_data
.lock
);
2286 if (consumer_data
.need_update
) {
2291 local_stream
= NULL
;
2293 /* allocate for all fds + 1 for the consumer_data_pipe */
2294 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2295 if (pollfd
== NULL
) {
2296 PERROR("pollfd malloc");
2297 pthread_mutex_unlock(&consumer_data
.lock
);
2301 /* allocate for all fds + 1 for the consumer_data_pipe */
2302 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2303 sizeof(struct lttng_consumer_stream
*));
2304 if (local_stream
== NULL
) {
2305 PERROR("local_stream malloc");
2306 pthread_mutex_unlock(&consumer_data
.lock
);
2309 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2312 ERR("Error in allocating pollfd or local_outfds");
2313 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2314 pthread_mutex_unlock(&consumer_data
.lock
);
2318 consumer_data
.need_update
= 0;
2320 pthread_mutex_unlock(&consumer_data
.lock
);
2322 /* No FDs and consumer_quit, consumer_cleanup the thread */
2323 if (nb_fd
== 0 && consumer_quit
== 1) {
2326 /* poll on the array of fds */
2328 DBG("polling on %d fd", nb_fd
+ 1);
2329 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2330 DBG("poll num_rdy : %d", num_rdy
);
2331 if (num_rdy
== -1) {
2333 * Restart interrupted system call.
2335 if (errno
== EINTR
) {
2338 PERROR("Poll error");
2339 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2341 } else if (num_rdy
== 0) {
2342 DBG("Polling thread timed out");
2347 * If the consumer_data_pipe triggered poll go directly to the
2348 * beginning of the loop to update the array. We want to prioritize
2349 * array update over low-priority reads.
2351 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2352 ssize_t pipe_readlen
;
2354 DBG("consumer_data_pipe wake up");
2355 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2356 &new_stream
, sizeof(new_stream
));
2357 if (pipe_readlen
< 0) {
2358 ERR("Consumer data pipe ret %ld", pipe_readlen
);
2359 /* Continue so we can at least handle the current stream(s). */
2364 * If the stream is NULL, just ignore it. It's also possible that
2365 * the sessiond poll thread changed the consumer_quit state and is
2366 * waking us up to test it.
2368 if (new_stream
== NULL
) {
2369 validate_endpoint_status_data_stream();
2373 ret
= add_stream(new_stream
, data_ht
);
2375 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
2378 * At this point, if the add_stream fails, it is not in the
2379 * hash table thus passing the NULL value here.
2381 consumer_del_stream(new_stream
, NULL
);
2384 /* Continue to update the local streams and handle prio ones */
2388 /* Take care of high priority channels first. */
2389 for (i
= 0; i
< nb_fd
; i
++) {
2390 if (local_stream
[i
] == NULL
) {
2393 if (pollfd
[i
].revents
& POLLPRI
) {
2394 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2396 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2397 /* it's ok to have an unavailable sub-buffer */
2398 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2399 /* Clean the stream and free it. */
2400 consumer_del_stream(local_stream
[i
], data_ht
);
2401 local_stream
[i
] = NULL
;
2402 } else if (len
> 0) {
2403 local_stream
[i
]->data_read
= 1;
2409 * If we read high prio channel in this loop, try again
2410 * for more high prio data.
2416 /* Take care of low priority channels. */
2417 for (i
= 0; i
< nb_fd
; i
++) {
2418 if (local_stream
[i
] == NULL
) {
2421 if ((pollfd
[i
].revents
& POLLIN
) ||
2422 local_stream
[i
]->hangup_flush_done
) {
2423 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2424 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2425 /* it's ok to have an unavailable sub-buffer */
2426 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2427 /* Clean the stream and free it. */
2428 consumer_del_stream(local_stream
[i
], data_ht
);
2429 local_stream
[i
] = NULL
;
2430 } else if (len
> 0) {
2431 local_stream
[i
]->data_read
= 1;
2436 /* Handle hangup and errors */
2437 for (i
= 0; i
< nb_fd
; i
++) {
2438 if (local_stream
[i
] == NULL
) {
2441 if (!local_stream
[i
]->hangup_flush_done
2442 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2443 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2444 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2445 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2447 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2448 /* Attempt read again, for the data we just flushed. */
2449 local_stream
[i
]->data_read
= 1;
2452 * If the poll flag is HUP/ERR/NVAL and we have
2453 * read no data in this pass, we can remove the
2454 * stream from its hash table.
2456 if ((pollfd
[i
].revents
& POLLHUP
)) {
2457 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2458 if (!local_stream
[i
]->data_read
) {
2459 consumer_del_stream(local_stream
[i
], data_ht
);
2460 local_stream
[i
] = NULL
;
2463 } else if (pollfd
[i
].revents
& POLLERR
) {
2464 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2465 if (!local_stream
[i
]->data_read
) {
2466 consumer_del_stream(local_stream
[i
], data_ht
);
2467 local_stream
[i
] = NULL
;
2470 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2471 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2472 if (!local_stream
[i
]->data_read
) {
2473 consumer_del_stream(local_stream
[i
], data_ht
);
2474 local_stream
[i
] = NULL
;
2478 if (local_stream
[i
] != NULL
) {
2479 local_stream
[i
]->data_read
= 0;
2484 DBG("polling thread exiting");
2489 * Close the write side of the pipe so epoll_wait() in
2490 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2491 * read side of the pipe. If we close them both, epoll_wait strangely does
2492 * not return and could create a endless wait period if the pipe is the
2493 * only tracked fd in the poll set. The thread will take care of closing
2496 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2498 destroy_data_stream_ht(data_ht
);
2500 rcu_unregister_thread();
2505 * Close wake-up end of each stream belonging to the channel. This will
2506 * allow the poll() on the stream read-side to detect when the
2507 * write-side (application) finally closes them.
2510 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2512 struct lttng_ht
*ht
;
2513 struct lttng_consumer_stream
*stream
;
2514 struct lttng_ht_iter iter
;
2516 ht
= consumer_data
.stream_per_chan_id_ht
;
2519 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2520 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2521 ht
->match_fct
, &channel
->key
,
2522 &iter
.iter
, stream
, node_channel_id
.node
) {
2524 * Protect against teardown with mutex.
2526 pthread_mutex_lock(&stream
->lock
);
2527 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2530 switch (consumer_data
.type
) {
2531 case LTTNG_CONSUMER_KERNEL
:
2533 case LTTNG_CONSUMER32_UST
:
2534 case LTTNG_CONSUMER64_UST
:
2536 * Note: a mutex is taken internally within
2537 * liblttng-ust-ctl to protect timer wakeup_fd
2538 * use from concurrent close.
2540 lttng_ustconsumer_close_stream_wakeup(stream
);
2543 ERR("Unknown consumer_data type");
2547 pthread_mutex_unlock(&stream
->lock
);
2552 static void destroy_channel_ht(struct lttng_ht
*ht
)
2554 struct lttng_ht_iter iter
;
2555 struct lttng_consumer_channel
*channel
;
2563 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2564 ret
= lttng_ht_del(ht
, &iter
);
2569 lttng_ht_destroy(ht
);
2573 * This thread polls the channel fds to detect when they are being
2574 * closed. It closes all related streams if the channel is detected as
2575 * closed. It is currently only used as a shim layer for UST because the
2576 * consumerd needs to keep the per-stream wakeup end of pipes open for
2579 void *consumer_thread_channel_poll(void *data
)
2582 uint32_t revents
, nb_fd
;
2583 struct lttng_consumer_channel
*chan
= NULL
;
2584 struct lttng_ht_iter iter
;
2585 struct lttng_ht_node_u64
*node
;
2586 struct lttng_poll_event events
;
2587 struct lttng_consumer_local_data
*ctx
= data
;
2588 struct lttng_ht
*channel_ht
;
2590 rcu_register_thread();
2592 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2594 /* ENOMEM at this point. Better to bail out. */
2598 DBG("Thread channel poll started");
2600 /* Size is set to 1 for the consumer_channel pipe */
2601 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2603 ERR("Poll set creation failed");
2607 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2613 DBG("Channel main loop started");
2616 /* Only the channel pipe is set */
2617 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2622 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2623 ret
= lttng_poll_wait(&events
, -1);
2624 DBG("Channel event catched in thread");
2626 if (errno
== EINTR
) {
2627 ERR("Poll EINTR catched");
2635 /* From here, the event is a channel wait fd */
2636 for (i
= 0; i
< nb_fd
; i
++) {
2637 revents
= LTTNG_POLL_GETEV(&events
, i
);
2638 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2640 /* Just don't waste time if no returned events for the fd */
2644 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2645 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2646 DBG("Channel thread pipe hung up");
2648 * Remove the pipe from the poll set and continue the loop
2649 * since their might be data to consume.
2651 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2653 } else if (revents
& LPOLLIN
) {
2654 enum consumer_channel_action action
;
2657 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2659 ERR("Error reading channel pipe");
2664 case CONSUMER_CHANNEL_ADD
:
2665 DBG("Adding channel %d to poll set",
2668 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2671 lttng_ht_add_unique_u64(channel_ht
,
2672 &chan
->wait_fd_node
);
2674 /* Add channel to the global poll events list */
2675 lttng_poll_add(&events
, chan
->wait_fd
,
2676 LPOLLIN
| LPOLLPRI
);
2678 case CONSUMER_CHANNEL_DEL
:
2680 struct lttng_consumer_stream
*stream
, *stmp
;
2683 chan
= consumer_find_channel(key
);
2686 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2689 lttng_poll_del(&events
, chan
->wait_fd
);
2690 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2691 ret
= lttng_ht_del(channel_ht
, &iter
);
2693 consumer_close_channel_streams(chan
);
2695 switch (consumer_data
.type
) {
2696 case LTTNG_CONSUMER_KERNEL
:
2698 case LTTNG_CONSUMER32_UST
:
2699 case LTTNG_CONSUMER64_UST
:
2700 /* Delete streams that might have been left in the stream list. */
2701 cds_list_for_each_entry_safe(stream
, stmp
, &chan
->streams
.head
,
2703 cds_list_del(&stream
->send_node
);
2704 lttng_ustconsumer_del_stream(stream
);
2705 uatomic_sub(&stream
->chan
->refcount
, 1);
2706 assert(&chan
->refcount
);
2711 ERR("Unknown consumer_data type");
2716 * Release our own refcount. Force channel deletion even if
2717 * streams were not initialized.
2719 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2720 consumer_del_channel(chan
);
2725 case CONSUMER_CHANNEL_QUIT
:
2727 * Remove the pipe from the poll set and continue the loop
2728 * since their might be data to consume.
2730 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2733 ERR("Unknown action");
2738 /* Handle other stream */
2744 uint64_t tmp_id
= (uint64_t) pollfd
;
2746 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2748 node
= lttng_ht_iter_get_node_u64(&iter
);
2751 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2754 /* Check for error event */
2755 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2756 DBG("Channel fd %d is hup|err.", pollfd
);
2758 lttng_poll_del(&events
, chan
->wait_fd
);
2759 ret
= lttng_ht_del(channel_ht
, &iter
);
2761 assert(cds_list_empty(&chan
->streams
.head
));
2762 consumer_close_channel_streams(chan
);
2764 /* Release our own refcount */
2765 if (!uatomic_sub_return(&chan
->refcount
, 1)
2766 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2767 consumer_del_channel(chan
);
2771 /* Release RCU lock for the channel looked up */
2777 lttng_poll_clean(&events
);
2779 destroy_channel_ht(channel_ht
);
2781 DBG("Channel poll thread exiting");
2782 rcu_unregister_thread();
2786 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2787 struct pollfd
*sockpoll
, int client_socket
)
2794 if (lttng_consumer_poll_socket(sockpoll
) < 0) {
2798 DBG("Metadata connection on client_socket");
2800 /* Blocking call, waiting for transmission */
2801 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2802 if (ctx
->consumer_metadata_socket
< 0) {
2803 WARN("On accept metadata");
2814 * This thread listens on the consumerd socket and receives the file
2815 * descriptors from the session daemon.
2817 void *consumer_thread_sessiond_poll(void *data
)
2819 int sock
= -1, client_socket
, ret
;
2821 * structure to poll for incoming data on communication socket avoids
2822 * making blocking sockets.
2824 struct pollfd consumer_sockpoll
[2];
2825 struct lttng_consumer_local_data
*ctx
= data
;
2827 rcu_register_thread();
2829 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2830 unlink(ctx
->consumer_command_sock_path
);
2831 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2832 if (client_socket
< 0) {
2833 ERR("Cannot create command socket");
2837 ret
= lttcomm_listen_unix_sock(client_socket
);
2842 DBG("Sending ready command to lttng-sessiond");
2843 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2844 /* return < 0 on error, but == 0 is not fatal */
2846 ERR("Error sending ready command to lttng-sessiond");
2850 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2851 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2852 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2853 consumer_sockpoll
[1].fd
= client_socket
;
2854 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2856 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2859 DBG("Connection on client_socket");
2861 /* Blocking call, waiting for transmission */
2862 sock
= lttcomm_accept_unix_sock(client_socket
);
2869 * Setup metadata socket which is the second socket connection on the
2870 * command unix socket.
2872 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
2877 /* This socket is not useful anymore. */
2878 ret
= close(client_socket
);
2880 PERROR("close client_socket");
2884 /* update the polling structure to poll on the established socket */
2885 consumer_sockpoll
[1].fd
= sock
;
2886 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2889 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2892 DBG("Incoming command on sock");
2893 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2894 if (ret
== -ENOENT
) {
2895 DBG("Received STOP command");
2900 * This could simply be a session daemon quitting. Don't output
2903 DBG("Communication interrupted on command socket");
2906 if (consumer_quit
) {
2907 DBG("consumer_thread_receive_fds received quit from signal");
2910 DBG("received command on sock");
2913 DBG("Consumer thread sessiond poll exiting");
2916 * Close metadata streams since the producer is the session daemon which
2919 * NOTE: for now, this only applies to the UST tracer.
2921 lttng_consumer_close_metadata();
2924 * when all fds have hung up, the polling thread
2930 * Notify the data poll thread to poll back again and test the
2931 * consumer_quit state that we just set so to quit gracefully.
2933 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
2935 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
2937 /* Cleaning up possibly open sockets. */
2941 PERROR("close sock sessiond poll");
2944 if (client_socket
>= 0) {
2945 ret
= close(client_socket
);
2947 PERROR("close client_socket sessiond poll");
2951 rcu_unregister_thread();
2955 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2956 struct lttng_consumer_local_data
*ctx
)
2960 pthread_mutex_lock(&stream
->lock
);
2962 switch (consumer_data
.type
) {
2963 case LTTNG_CONSUMER_KERNEL
:
2964 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
2966 case LTTNG_CONSUMER32_UST
:
2967 case LTTNG_CONSUMER64_UST
:
2968 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2971 ERR("Unknown consumer_data type");
2977 pthread_mutex_unlock(&stream
->lock
);
2981 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2983 switch (consumer_data
.type
) {
2984 case LTTNG_CONSUMER_KERNEL
:
2985 return lttng_kconsumer_on_recv_stream(stream
);
2986 case LTTNG_CONSUMER32_UST
:
2987 case LTTNG_CONSUMER64_UST
:
2988 return lttng_ustconsumer_on_recv_stream(stream
);
2990 ERR("Unknown consumer_data type");
2997 * Allocate and set consumer data hash tables.
2999 void lttng_consumer_init(void)
3001 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3002 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3003 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3004 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3008 * Process the ADD_RELAYD command receive by a consumer.
3010 * This will create a relayd socket pair and add it to the relayd hash table.
3011 * The caller MUST acquire a RCU read side lock before calling it.
3013 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
3014 struct lttng_consumer_local_data
*ctx
, int sock
,
3015 struct pollfd
*consumer_sockpoll
,
3016 struct lttcomm_relayd_sock
*relayd_sock
, unsigned int sessiond_id
)
3018 int fd
= -1, ret
= -1, relayd_created
= 0;
3019 enum lttng_error_code ret_code
= LTTNG_OK
;
3020 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3023 assert(relayd_sock
);
3025 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
3027 /* Get relayd reference if exists. */
3028 relayd
= consumer_find_relayd(net_seq_idx
);
3029 if (relayd
== NULL
) {
3030 /* Not found. Allocate one. */
3031 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3032 if (relayd
== NULL
) {
3033 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3036 relayd
->sessiond_session_id
= (uint64_t) sessiond_id
;
3041 * This code path MUST continue to the consumer send status message to
3042 * we can notify the session daemon and continue our work without
3043 * killing everything.
3047 /* First send a status message before receiving the fds. */
3048 ret
= consumer_send_status_msg(sock
, ret_code
);
3049 if (ret
< 0 || ret_code
!= LTTNG_OK
) {
3050 /* Somehow, the session daemon is not responding anymore. */
3054 /* Poll on consumer socket. */
3055 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
3056 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3061 /* Get relayd socket from session daemon */
3062 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3063 if (ret
!= sizeof(fd
)) {
3064 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3066 fd
= -1; /* Just in case it gets set with an invalid value. */
3069 * Failing to receive FDs might indicate a major problem such as
3070 * reaching a fd limit during the receive where the kernel returns a
3071 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3072 * don't take any chances and stop everything.
3074 * XXX: Feature request #558 will fix that and avoid this possible
3075 * issue when reaching the fd limit.
3077 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3080 * This code path MUST continue to the consumer send status message so
3081 * we can send the error to the thread expecting a reply. The above
3082 * call will make everything stop.
3086 /* We have the fds without error. Send status back. */
3087 ret
= consumer_send_status_msg(sock
, ret_code
);
3088 if (ret
< 0 || ret_code
!= LTTNG_OK
) {
3089 /* Somehow, the session daemon is not responding anymore. */
3093 /* Copy socket information and received FD */
3094 switch (sock_type
) {
3095 case LTTNG_STREAM_CONTROL
:
3096 /* Copy received lttcomm socket */
3097 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3098 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3099 /* Immediately try to close the created socket if valid. */
3100 if (relayd
->control_sock
.sock
.fd
>= 0) {
3101 if (close(relayd
->control_sock
.sock
.fd
)) {
3102 PERROR("close relayd control socket");
3105 /* Handle create_sock error. */
3110 /* Assign new file descriptor */
3111 relayd
->control_sock
.sock
.fd
= fd
;
3112 fd
= -1; /* For error path */
3113 /* Assign version values. */
3114 relayd
->control_sock
.major
= relayd_sock
->major
;
3115 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3118 * Create a session on the relayd and store the returned id. Lock the
3119 * control socket mutex if the relayd was NOT created before.
3121 if (!relayd_created
) {
3122 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3124 ret
= relayd_create_session(&relayd
->control_sock
,
3125 &relayd
->relayd_session_id
);
3126 if (!relayd_created
) {
3127 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3131 * Close all sockets of a relayd object. It will be freed if it was
3132 * created at the error code path or else it will be garbage
3135 (void) relayd_close(&relayd
->control_sock
);
3136 (void) relayd_close(&relayd
->data_sock
);
3141 case LTTNG_STREAM_DATA
:
3142 /* Copy received lttcomm socket */
3143 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3144 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3145 /* Immediately try to close the created socket if valid. */
3146 if (relayd
->data_sock
.sock
.fd
>= 0) {
3147 if (close(relayd
->data_sock
.sock
.fd
)) {
3148 PERROR("close relayd data socket");
3151 /* Handle create_sock error. */
3156 /* Assign new file descriptor */
3157 relayd
->data_sock
.sock
.fd
= fd
;
3158 fd
= -1; /* for eventual error paths */
3159 /* Assign version values. */
3160 relayd
->data_sock
.major
= relayd_sock
->major
;
3161 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3164 ERR("Unknown relayd socket type (%d)", sock_type
);
3169 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3170 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3171 relayd
->net_seq_idx
, fd
);
3174 * Add relayd socket pair to consumer data hashtable. If object already
3175 * exists or on error, the function gracefully returns.
3183 /* Close received socket if valid. */
3186 PERROR("close received socket");
3190 if (relayd_created
) {
3198 * Try to lock the stream mutex.
3200 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3202 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3209 * Try to lock the stream mutex. On failure, we know that the stream is
3210 * being used else where hence there is data still being extracted.
3212 ret
= pthread_mutex_trylock(&stream
->lock
);
3214 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3226 * Search for a relayd associated to the session id and return the reference.
3228 * A rcu read side lock MUST be acquire before calling this function and locked
3229 * until the relayd object is no longer necessary.
3231 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3233 struct lttng_ht_iter iter
;
3234 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3236 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3237 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3240 * Check by sessiond id which is unique here where the relayd session
3241 * id might not be when having multiple relayd.
3243 if (relayd
->sessiond_session_id
== id
) {
3244 /* Found the relayd. There can be only one per id. */
3256 * Check if for a given session id there is still data needed to be extract
3259 * Return 1 if data is pending or else 0 meaning ready to be read.
3261 int consumer_data_pending(uint64_t id
)
3264 struct lttng_ht_iter iter
;
3265 struct lttng_ht
*ht
;
3266 struct lttng_consumer_stream
*stream
;
3267 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3268 int (*data_pending
)(struct lttng_consumer_stream
*);
3270 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3273 pthread_mutex_lock(&consumer_data
.lock
);
3275 switch (consumer_data
.type
) {
3276 case LTTNG_CONSUMER_KERNEL
:
3277 data_pending
= lttng_kconsumer_data_pending
;
3279 case LTTNG_CONSUMER32_UST
:
3280 case LTTNG_CONSUMER64_UST
:
3281 data_pending
= lttng_ustconsumer_data_pending
;
3284 ERR("Unknown consumer data type");
3288 /* Ease our life a bit */
3289 ht
= consumer_data
.stream_list_ht
;
3291 relayd
= find_relayd_by_session_id(id
);
3293 /* Send init command for data pending. */
3294 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3295 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3296 relayd
->relayd_session_id
);
3297 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3299 /* Communication error thus the relayd so no data pending. */
3300 goto data_not_pending
;
3304 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3305 ht
->hash_fct(&id
, lttng_ht_seed
),
3307 &iter
.iter
, stream
, node_session_id
.node
) {
3308 /* If this call fails, the stream is being used hence data pending. */
3309 ret
= stream_try_lock(stream
);
3315 * A removed node from the hash table indicates that the stream has
3316 * been deleted thus having a guarantee that the buffers are closed
3317 * on the consumer side. However, data can still be transmitted
3318 * over the network so don't skip the relayd check.
3320 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3322 /* Check the stream if there is data in the buffers. */
3323 ret
= data_pending(stream
);
3325 pthread_mutex_unlock(&stream
->lock
);
3332 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3333 if (stream
->metadata_flag
) {
3334 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3335 stream
->relayd_stream_id
);
3337 ret
= relayd_data_pending(&relayd
->control_sock
,
3338 stream
->relayd_stream_id
,
3339 stream
->next_net_seq_num
- 1);
3341 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3343 pthread_mutex_unlock(&stream
->lock
);
3347 pthread_mutex_unlock(&stream
->lock
);
3351 unsigned int is_data_inflight
= 0;
3353 /* Send init command for data pending. */
3354 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3355 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3356 relayd
->relayd_session_id
, &is_data_inflight
);
3357 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3359 goto data_not_pending
;
3361 if (is_data_inflight
) {
3367 * Finding _no_ node in the hash table and no inflight data means that the
3368 * stream(s) have been removed thus data is guaranteed to be available for
3369 * analysis from the trace files.
3373 /* Data is available to be read by a viewer. */
3374 pthread_mutex_unlock(&consumer_data
.lock
);
3379 /* Data is still being extracted from buffers. */
3380 pthread_mutex_unlock(&consumer_data
.lock
);
3386 * Send a ret code status message to the sessiond daemon.
3388 * Return the sendmsg() return value.
3390 int consumer_send_status_msg(int sock
, int ret_code
)
3392 struct lttcomm_consumer_status_msg msg
;
3394 msg
.ret_code
= ret_code
;
3396 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3400 * Send a channel status message to the sessiond daemon.
3402 * Return the sendmsg() return value.
3404 int consumer_send_status_channel(int sock
,
3405 struct lttng_consumer_channel
*channel
)
3407 struct lttcomm_consumer_status_channel msg
;
3412 msg
.ret_code
= -LTTNG_ERR_UST_CHAN_FAIL
;
3414 msg
.ret_code
= LTTNG_OK
;
3415 msg
.key
= channel
->key
;
3416 msg
.stream_count
= channel
->streams
.count
;
3419 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));