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");
316 iter
.iter
.node
= &channel
->node
.node
;
317 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
321 call_rcu(&channel
->node
.head
, free_channel_rcu
);
323 pthread_mutex_unlock(&consumer_data
.lock
);
327 * Iterate over the relayd hash table and destroy each element. Finally,
328 * destroy the whole hash table.
330 static void cleanup_relayd_ht(void)
332 struct lttng_ht_iter iter
;
333 struct consumer_relayd_sock_pair
*relayd
;
337 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
339 consumer_destroy_relayd(relayd
);
344 lttng_ht_destroy(consumer_data
.relayd_ht
);
348 * Update the end point status of all streams having the given network sequence
349 * index (relayd index).
351 * It's atomically set without having the stream mutex locked which is fine
352 * because we handle the write/read race with a pipe wakeup for each thread.
354 static void update_endpoint_status_by_netidx(int net_seq_idx
,
355 enum consumer_endpoint_status status
)
357 struct lttng_ht_iter iter
;
358 struct lttng_consumer_stream
*stream
;
360 DBG("Consumer set delete flag on stream by idx %d", net_seq_idx
);
364 /* Let's begin with metadata */
365 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
366 if (stream
->net_seq_idx
== net_seq_idx
) {
367 uatomic_set(&stream
->endpoint_status
, status
);
368 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
372 /* Follow up by the data streams */
373 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
374 if (stream
->net_seq_idx
== net_seq_idx
) {
375 uatomic_set(&stream
->endpoint_status
, status
);
376 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
383 * Cleanup a relayd object by flagging every associated streams for deletion,
384 * destroying the object meaning removing it from the relayd hash table,
385 * closing the sockets and freeing the memory in a RCU call.
387 * If a local data context is available, notify the threads that the streams'
388 * state have changed.
390 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
391 struct lttng_consumer_local_data
*ctx
)
397 DBG("Cleaning up relayd sockets");
399 /* Save the net sequence index before destroying the object */
400 netidx
= relayd
->net_seq_idx
;
403 * Delete the relayd from the relayd hash table, close the sockets and free
404 * the object in a RCU call.
406 consumer_destroy_relayd(relayd
);
408 /* Set inactive endpoint to all streams */
409 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
412 * With a local data context, notify the threads that the streams' state
413 * have changed. The write() action on the pipe acts as an "implicit"
414 * memory barrier ordering the updates of the end point status from the
415 * read of this status which happens AFTER receiving this notify.
418 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
419 notify_thread_lttng_pipe(ctx
->consumer_metadata_pipe
);
424 * Flag a relayd socket pair for destruction. Destroy it if the refcount
427 * RCU read side lock MUST be aquired before calling this function.
429 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
433 /* Set destroy flag for this object */
434 uatomic_set(&relayd
->destroy_flag
, 1);
436 /* Destroy the relayd if refcount is 0 */
437 if (uatomic_read(&relayd
->refcount
) == 0) {
438 consumer_destroy_relayd(relayd
);
443 * Completly destroy stream from every visiable data structure and the given
446 * One this call returns, the stream object is not longer usable nor visible.
448 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
451 consumer_stream_destroy(stream
, ht
);
454 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
456 enum lttng_consumer_stream_state state
,
457 const char *channel_name
,
464 enum consumer_channel_type type
)
467 struct lttng_consumer_stream
*stream
;
469 stream
= zmalloc(sizeof(*stream
));
470 if (stream
== NULL
) {
471 PERROR("malloc struct lttng_consumer_stream");
478 stream
->key
= stream_key
;
480 stream
->out_fd_offset
= 0;
481 stream
->state
= state
;
484 stream
->net_seq_idx
= relayd_id
;
485 stream
->session_id
= session_id
;
486 pthread_mutex_init(&stream
->lock
, NULL
);
488 /* If channel is the metadata, flag this stream as metadata. */
489 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
490 stream
->metadata_flag
= 1;
491 /* Metadata is flat out. */
492 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
494 /* Format stream name to <channel_name>_<cpu_number> */
495 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
498 PERROR("snprintf stream name");
503 /* Key is always the wait_fd for streams. */
504 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
506 /* Init node per channel id key */
507 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
509 /* Init session id node with the stream session id */
510 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
512 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
" relayd_id %" PRIu64
", session_id %" PRIu64
,
513 stream
->name
, stream
->key
, channel_key
, stream
->net_seq_idx
, stream
->session_id
);
529 * Add a stream to the global list protected by a mutex.
531 static int add_stream(struct lttng_consumer_stream
*stream
,
535 struct consumer_relayd_sock_pair
*relayd
;
540 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
542 pthread_mutex_lock(&consumer_data
.lock
);
543 pthread_mutex_lock(&stream
->lock
);
546 /* Steal stream identifier to avoid having streams with the same key */
547 steal_stream_key(stream
->key
, ht
);
549 lttng_ht_add_unique_u64(ht
, &stream
->node
);
551 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
552 &stream
->node_channel_id
);
555 * Add stream to the stream_list_ht of the consumer data. No need to steal
556 * the key since the HT does not use it and we allow to add redundant keys
559 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
561 /* Check and cleanup relayd */
562 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
563 if (relayd
!= NULL
) {
564 uatomic_inc(&relayd
->refcount
);
568 * When nb_init_stream_left reaches 0, we don't need to trigger any action
569 * in terms of destroying the associated channel, because the action that
570 * causes the count to become 0 also causes a stream to be added. The
571 * channel deletion will thus be triggered by the following removal of this
574 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
575 /* Increment refcount before decrementing nb_init_stream_left */
577 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
580 /* Update consumer data once the node is inserted. */
581 consumer_data
.stream_count
++;
582 consumer_data
.need_update
= 1;
585 pthread_mutex_unlock(&stream
->lock
);
586 pthread_mutex_unlock(&consumer_data
.lock
);
592 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
593 * be acquired before calling this.
595 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
598 struct lttng_ht_node_u64
*node
;
599 struct lttng_ht_iter iter
;
603 lttng_ht_lookup(consumer_data
.relayd_ht
,
604 &relayd
->net_seq_idx
, &iter
);
605 node
= lttng_ht_iter_get_node_u64(&iter
);
609 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
616 * Allocate and return a consumer relayd socket.
618 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
621 struct consumer_relayd_sock_pair
*obj
= NULL
;
623 /* Negative net sequence index is a failure */
624 if (net_seq_idx
< 0) {
628 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
630 PERROR("zmalloc relayd sock");
634 obj
->net_seq_idx
= net_seq_idx
;
636 obj
->destroy_flag
= 0;
637 obj
->control_sock
.sock
.fd
= -1;
638 obj
->data_sock
.sock
.fd
= -1;
639 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
640 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
647 * Find a relayd socket pair in the global consumer data.
649 * Return the object if found else NULL.
650 * RCU read-side lock must be held across this call and while using the
653 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
655 struct lttng_ht_iter iter
;
656 struct lttng_ht_node_u64
*node
;
657 struct consumer_relayd_sock_pair
*relayd
= NULL
;
659 /* Negative keys are lookup failures */
660 if (key
== (uint64_t) -1ULL) {
664 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
666 node
= lttng_ht_iter_get_node_u64(&iter
);
668 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
676 * Handle stream for relayd transmission if the stream applies for network
677 * streaming where the net sequence index is set.
679 * Return destination file descriptor or negative value on error.
681 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
682 size_t data_size
, unsigned long padding
,
683 struct consumer_relayd_sock_pair
*relayd
)
686 struct lttcomm_relayd_data_hdr data_hdr
;
692 /* Reset data header */
693 memset(&data_hdr
, 0, sizeof(data_hdr
));
695 if (stream
->metadata_flag
) {
696 /* Caller MUST acquire the relayd control socket lock */
697 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
702 /* Metadata are always sent on the control socket. */
703 outfd
= relayd
->control_sock
.sock
.fd
;
705 /* Set header with stream information */
706 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
707 data_hdr
.data_size
= htobe32(data_size
);
708 data_hdr
.padding_size
= htobe32(padding
);
710 * Note that net_seq_num below is assigned with the *current* value of
711 * next_net_seq_num and only after that the next_net_seq_num will be
712 * increment. This is why when issuing a command on the relayd using
713 * this next value, 1 should always be substracted in order to compare
714 * the last seen sequence number on the relayd side to the last sent.
716 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
717 /* Other fields are zeroed previously */
719 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
725 ++stream
->next_net_seq_num
;
727 /* Set to go on data socket */
728 outfd
= relayd
->data_sock
.sock
.fd
;
736 * Allocate and return a new lttng_consumer_channel object using the given key
737 * to initialize the hash table node.
739 * On error, return NULL.
741 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
743 const char *pathname
,
748 enum lttng_event_output output
,
749 uint64_t tracefile_size
,
750 uint64_t tracefile_count
)
752 struct lttng_consumer_channel
*channel
;
754 channel
= zmalloc(sizeof(*channel
));
755 if (channel
== NULL
) {
756 PERROR("malloc struct lttng_consumer_channel");
761 channel
->refcount
= 0;
762 channel
->session_id
= session_id
;
765 channel
->relayd_id
= relayd_id
;
766 channel
->output
= output
;
767 channel
->tracefile_size
= tracefile_size
;
768 channel
->tracefile_count
= tracefile_count
;
770 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
771 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
773 strncpy(channel
->name
, name
, sizeof(channel
->name
));
774 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
776 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
778 channel
->wait_fd
= -1;
780 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
782 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
789 * Add a channel to the global list protected by a mutex.
791 * On success 0 is returned else a negative value.
793 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
794 struct lttng_consumer_local_data
*ctx
)
797 struct lttng_ht_node_u64
*node
;
798 struct lttng_ht_iter iter
;
800 pthread_mutex_lock(&consumer_data
.lock
);
803 lttng_ht_lookup(consumer_data
.channel_ht
, &channel
->key
, &iter
);
804 node
= lttng_ht_iter_get_node_u64(&iter
);
806 /* Channel already exist. Ignore the insertion */
807 ERR("Consumer add channel key %" PRIu64
" already exists!",
813 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
817 pthread_mutex_unlock(&consumer_data
.lock
);
819 if (!ret
&& channel
->wait_fd
!= -1 &&
820 channel
->metadata_stream
== NULL
) {
821 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
827 * Allocate the pollfd structure and the local view of the out fds to avoid
828 * doing a lookup in the linked list and concurrency issues when writing is
829 * needed. Called with consumer_data.lock held.
831 * Returns the number of fds in the structures.
833 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
834 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
838 struct lttng_ht_iter iter
;
839 struct lttng_consumer_stream
*stream
;
844 assert(local_stream
);
846 DBG("Updating poll fd array");
848 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
850 * Only active streams with an active end point can be added to the
851 * poll set and local stream storage of the thread.
853 * There is a potential race here for endpoint_status to be updated
854 * just after the check. However, this is OK since the stream(s) will
855 * be deleted once the thread is notified that the end point state has
856 * changed where this function will be called back again.
858 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
859 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
863 * This clobbers way too much the debug output. Uncomment that if you
864 * need it for debugging purposes.
866 * DBG("Active FD %d", stream->wait_fd);
868 (*pollfd
)[i
].fd
= stream
->wait_fd
;
869 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
870 local_stream
[i
] = stream
;
876 * Insert the consumer_data_pipe at the end of the array and don't
877 * increment i so nb_fd is the number of real FD.
879 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
880 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
885 * Poll on the should_quit pipe and the command socket return -1 on error and
886 * should exit, 0 if data is available on the command socket
888 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
893 num_rdy
= poll(consumer_sockpoll
, 2, -1);
896 * Restart interrupted system call.
898 if (errno
== EINTR
) {
901 PERROR("Poll error");
904 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
905 DBG("consumer_should_quit wake up");
915 * Set the error socket.
917 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
920 ctx
->consumer_error_socket
= sock
;
924 * Set the command socket path.
926 void lttng_consumer_set_command_sock_path(
927 struct lttng_consumer_local_data
*ctx
, char *sock
)
929 ctx
->consumer_command_sock_path
= sock
;
933 * Send return code to the session daemon.
934 * If the socket is not defined, we return 0, it is not a fatal error
936 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
938 if (ctx
->consumer_error_socket
> 0) {
939 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
940 sizeof(enum lttcomm_sessiond_command
));
947 * Close all the tracefiles and stream fds and MUST be called when all
948 * instances are destroyed i.e. when all threads were joined and are ended.
950 void lttng_consumer_cleanup(void)
952 struct lttng_ht_iter iter
;
953 struct lttng_consumer_channel
*channel
;
957 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
959 consumer_del_channel(channel
);
964 lttng_ht_destroy(consumer_data
.channel_ht
);
968 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
971 * This HT contains streams that are freed by either the metadata thread or
972 * the data thread so we do *nothing* on the hash table and simply destroy
975 lttng_ht_destroy(consumer_data
.stream_list_ht
);
979 * Called from signal handler.
981 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
986 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
987 } while (ret
< 0 && errno
== EINTR
);
988 if (ret
< 0 || ret
!= 1) {
989 PERROR("write consumer quit");
992 DBG("Consumer flag that it should quit");
995 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
998 int outfd
= stream
->out_fd
;
1001 * This does a blocking write-and-wait on any page that belongs to the
1002 * subbuffer prior to the one we just wrote.
1003 * Don't care about error values, as these are just hints and ways to
1004 * limit the amount of page cache used.
1006 if (orig_offset
< stream
->max_sb_size
) {
1009 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1010 stream
->max_sb_size
,
1011 SYNC_FILE_RANGE_WAIT_BEFORE
1012 | SYNC_FILE_RANGE_WRITE
1013 | SYNC_FILE_RANGE_WAIT_AFTER
);
1015 * Give hints to the kernel about how we access the file:
1016 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1019 * We need to call fadvise again after the file grows because the
1020 * kernel does not seem to apply fadvise to non-existing parts of the
1023 * Call fadvise _after_ having waited for the page writeback to
1024 * complete because the dirty page writeback semantic is not well
1025 * defined. So it can be expected to lead to lower throughput in
1028 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1029 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1033 * Initialise the necessary environnement :
1034 * - create a new context
1035 * - create the poll_pipe
1036 * - create the should_quit pipe (for signal handler)
1037 * - create the thread pipe (for splice)
1039 * Takes a function pointer as argument, this function is called when data is
1040 * available on a buffer. This function is responsible to do the
1041 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1042 * buffer configuration and then kernctl_put_next_subbuf at the end.
1044 * Returns a pointer to the new context or NULL on error.
1046 struct lttng_consumer_local_data
*lttng_consumer_create(
1047 enum lttng_consumer_type type
,
1048 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1049 struct lttng_consumer_local_data
*ctx
),
1050 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1051 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1052 int (*update_stream
)(int stream_key
, uint32_t state
))
1055 struct lttng_consumer_local_data
*ctx
;
1057 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1058 consumer_data
.type
== type
);
1059 consumer_data
.type
= type
;
1061 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1063 PERROR("allocating context");
1067 ctx
->consumer_error_socket
= -1;
1068 ctx
->consumer_metadata_socket
= -1;
1069 /* assign the callbacks */
1070 ctx
->on_buffer_ready
= buffer_ready
;
1071 ctx
->on_recv_channel
= recv_channel
;
1072 ctx
->on_recv_stream
= recv_stream
;
1073 ctx
->on_update_stream
= update_stream
;
1075 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1076 if (!ctx
->consumer_data_pipe
) {
1077 goto error_poll_pipe
;
1080 ret
= pipe(ctx
->consumer_should_quit
);
1082 PERROR("Error creating recv pipe");
1083 goto error_quit_pipe
;
1086 ret
= pipe(ctx
->consumer_thread_pipe
);
1088 PERROR("Error creating thread pipe");
1089 goto error_thread_pipe
;
1092 ret
= pipe(ctx
->consumer_channel_pipe
);
1094 PERROR("Error creating channel pipe");
1095 goto error_channel_pipe
;
1098 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1099 if (!ctx
->consumer_metadata_pipe
) {
1100 goto error_metadata_pipe
;
1103 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1105 goto error_splice_pipe
;
1111 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1112 error_metadata_pipe
:
1113 utils_close_pipe(ctx
->consumer_channel_pipe
);
1115 utils_close_pipe(ctx
->consumer_thread_pipe
);
1117 utils_close_pipe(ctx
->consumer_should_quit
);
1119 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1127 * Close all fds associated with the instance and free the context.
1129 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1133 DBG("Consumer destroying it. Closing everything.");
1135 ret
= close(ctx
->consumer_error_socket
);
1139 ret
= close(ctx
->consumer_metadata_socket
);
1143 utils_close_pipe(ctx
->consumer_thread_pipe
);
1144 utils_close_pipe(ctx
->consumer_channel_pipe
);
1145 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1146 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1147 utils_close_pipe(ctx
->consumer_should_quit
);
1148 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1150 unlink(ctx
->consumer_command_sock_path
);
1155 * Write the metadata stream id on the specified file descriptor.
1157 static int write_relayd_metadata_id(int fd
,
1158 struct lttng_consumer_stream
*stream
,
1159 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1162 struct lttcomm_relayd_metadata_payload hdr
;
1164 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1165 hdr
.padding_size
= htobe32(padding
);
1167 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1168 } while (ret
< 0 && errno
== EINTR
);
1169 if (ret
< 0 || ret
!= sizeof(hdr
)) {
1171 * This error means that the fd's end is closed so ignore the perror
1172 * not to clubber the error output since this can happen in a normal
1175 if (errno
!= EPIPE
) {
1176 PERROR("write metadata stream id");
1178 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1180 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1181 * handle writting the missing part so report that as an error and
1182 * don't lie to the caller.
1187 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1188 stream
->relayd_stream_id
, padding
);
1195 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1196 * core function for writing trace buffers to either the local filesystem or
1199 * It must be called with the stream lock held.
1201 * Careful review MUST be put if any changes occur!
1203 * Returns the number of bytes written
1205 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1206 struct lttng_consumer_local_data
*ctx
,
1207 struct lttng_consumer_stream
*stream
, unsigned long len
,
1208 unsigned long padding
)
1210 unsigned long mmap_offset
;
1212 ssize_t ret
= 0, written
= 0;
1213 off_t orig_offset
= stream
->out_fd_offset
;
1214 /* Default is on the disk */
1215 int outfd
= stream
->out_fd
;
1216 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1217 unsigned int relayd_hang_up
= 0;
1219 /* RCU lock for the relayd pointer */
1222 /* Flag that the current stream if set for network streaming. */
1223 if (stream
->net_seq_idx
!= -1) {
1224 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1225 if (relayd
== NULL
) {
1230 /* get the offset inside the fd to mmap */
1231 switch (consumer_data
.type
) {
1232 case LTTNG_CONSUMER_KERNEL
:
1233 mmap_base
= stream
->mmap_base
;
1234 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1236 case LTTNG_CONSUMER32_UST
:
1237 case LTTNG_CONSUMER64_UST
:
1238 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1240 ERR("read mmap get mmap base for stream %s", stream
->name
);
1244 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1248 ERR("Unknown consumer_data type");
1253 PERROR("tracer ctl get_mmap_read_offset");
1258 /* Handle stream on the relayd if the output is on the network */
1260 unsigned long netlen
= len
;
1263 * Lock the control socket for the complete duration of the function
1264 * since from this point on we will use the socket.
1266 if (stream
->metadata_flag
) {
1267 /* Metadata requires the control socket. */
1268 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1269 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1272 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1274 /* Use the returned socket. */
1277 /* Write metadata stream id before payload */
1278 if (stream
->metadata_flag
) {
1279 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1282 /* Socket operation failed. We consider the relayd dead */
1283 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1291 /* Socket operation failed. We consider the relayd dead */
1292 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1296 /* Else, use the default set before which is the filesystem. */
1299 /* No streaming, we have to set the len with the full padding */
1303 * Check if we need to change the tracefile before writing the packet.
1305 if (stream
->chan
->tracefile_size
> 0 &&
1306 (stream
->tracefile_size_current
+ len
) >
1307 stream
->chan
->tracefile_size
) {
1308 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1309 stream
->name
, stream
->chan
->tracefile_size
,
1310 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1311 stream
->out_fd
, &(stream
->tracefile_count_current
));
1313 ERR("Rotating output file");
1316 outfd
= stream
->out_fd
= ret
;
1317 /* Reset current size because we just perform a rotation. */
1318 stream
->tracefile_size_current
= 0;
1320 stream
->tracefile_size_current
+= len
;
1325 ret
= write(outfd
, mmap_base
+ mmap_offset
, len
);
1326 } while (ret
< 0 && errno
== EINTR
);
1327 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1330 * This is possible if the fd is closed on the other side (outfd)
1331 * or any write problem. It can be verbose a bit for a normal
1332 * execution if for instance the relayd is stopped abruptly. This
1333 * can happen so set this to a DBG statement.
1335 DBG("Error in file write mmap");
1339 /* Socket operation failed. We consider the relayd dead */
1340 if (errno
== EPIPE
|| errno
== EINVAL
) {
1345 } else if (ret
> len
) {
1346 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1354 /* This call is useless on a socket so better save a syscall. */
1356 /* This won't block, but will start writeout asynchronously */
1357 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1358 SYNC_FILE_RANGE_WRITE
);
1359 stream
->out_fd_offset
+= ret
;
1363 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1367 * This is a special case that the relayd has closed its socket. Let's
1368 * cleanup the relayd object and all associated streams.
1370 if (relayd
&& relayd_hang_up
) {
1371 cleanup_relayd(relayd
, ctx
);
1375 /* Unlock only if ctrl socket used */
1376 if (relayd
&& stream
->metadata_flag
) {
1377 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1385 * Splice the data from the ring buffer to the tracefile.
1387 * It must be called with the stream lock held.
1389 * Returns the number of bytes spliced.
1391 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1392 struct lttng_consumer_local_data
*ctx
,
1393 struct lttng_consumer_stream
*stream
, unsigned long len
,
1394 unsigned long padding
)
1396 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1398 off_t orig_offset
= stream
->out_fd_offset
;
1399 int fd
= stream
->wait_fd
;
1400 /* Default is on the disk */
1401 int outfd
= stream
->out_fd
;
1402 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1404 unsigned int relayd_hang_up
= 0;
1406 switch (consumer_data
.type
) {
1407 case LTTNG_CONSUMER_KERNEL
:
1409 case LTTNG_CONSUMER32_UST
:
1410 case LTTNG_CONSUMER64_UST
:
1411 /* Not supported for user space tracing */
1414 ERR("Unknown consumer_data type");
1418 /* RCU lock for the relayd pointer */
1421 /* Flag that the current stream if set for network streaming. */
1422 if (stream
->net_seq_idx
!= -1) {
1423 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1424 if (relayd
== NULL
) {
1430 * Choose right pipe for splice. Metadata and trace data are handled by
1431 * different threads hence the use of two pipes in order not to race or
1432 * corrupt the written data.
1434 if (stream
->metadata_flag
) {
1435 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1437 splice_pipe
= ctx
->consumer_thread_pipe
;
1440 /* Write metadata stream id before payload */
1442 int total_len
= len
;
1444 if (stream
->metadata_flag
) {
1446 * Lock the control socket for the complete duration of the function
1447 * since from this point on we will use the socket.
1449 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1451 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1455 /* Socket operation failed. We consider the relayd dead */
1456 if (ret
== -EBADF
) {
1457 WARN("Remote relayd disconnected. Stopping");
1464 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1467 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1469 /* Use the returned socket. */
1472 /* Socket operation failed. We consider the relayd dead */
1473 if (ret
== -EBADF
) {
1474 WARN("Remote relayd disconnected. Stopping");
1481 /* No streaming, we have to set the len with the full padding */
1485 * Check if we need to change the tracefile before writing the packet.
1487 if (stream
->chan
->tracefile_size
> 0 &&
1488 (stream
->tracefile_size_current
+ len
) >
1489 stream
->chan
->tracefile_size
) {
1490 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1491 stream
->name
, stream
->chan
->tracefile_size
,
1492 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1493 stream
->out_fd
, &(stream
->tracefile_count_current
));
1495 ERR("Rotating output file");
1498 outfd
= stream
->out_fd
= ret
;
1499 /* Reset current size because we just perform a rotation. */
1500 stream
->tracefile_size_current
= 0;
1502 stream
->tracefile_size_current
+= len
;
1506 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1507 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1508 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1509 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1510 DBG("splice chan to pipe, ret %zd", ret_splice
);
1511 if (ret_splice
< 0) {
1512 PERROR("Error in relay splice");
1514 written
= ret_splice
;
1520 /* Handle stream on the relayd if the output is on the network */
1522 if (stream
->metadata_flag
) {
1523 size_t metadata_payload_size
=
1524 sizeof(struct lttcomm_relayd_metadata_payload
);
1526 /* Update counter to fit the spliced data */
1527 ret_splice
+= metadata_payload_size
;
1528 len
+= metadata_payload_size
;
1530 * We do this so the return value can match the len passed as
1531 * argument to this function.
1533 written
-= metadata_payload_size
;
1537 /* Splice data out */
1538 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1539 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1540 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1541 if (ret_splice
< 0) {
1542 PERROR("Error in file splice");
1544 written
= ret_splice
;
1546 /* Socket operation failed. We consider the relayd dead */
1547 if (errno
== EBADF
|| errno
== EPIPE
) {
1548 WARN("Remote relayd disconnected. Stopping");
1554 } else if (ret_splice
> len
) {
1556 PERROR("Wrote more data than requested %zd (len: %lu)",
1558 written
+= ret_splice
;
1564 /* This call is useless on a socket so better save a syscall. */
1566 /* This won't block, but will start writeout asynchronously */
1567 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1568 SYNC_FILE_RANGE_WRITE
);
1569 stream
->out_fd_offset
+= ret_splice
;
1571 written
+= ret_splice
;
1573 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1581 * This is a special case that the relayd has closed its socket. Let's
1582 * cleanup the relayd object and all associated streams.
1584 if (relayd
&& relayd_hang_up
) {
1585 cleanup_relayd(relayd
, ctx
);
1586 /* Skip splice error so the consumer does not fail */
1591 /* send the appropriate error description to sessiond */
1594 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1597 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1600 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1605 if (relayd
&& stream
->metadata_flag
) {
1606 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1614 * Take a snapshot for a specific fd
1616 * Returns 0 on success, < 0 on error
1618 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1620 switch (consumer_data
.type
) {
1621 case LTTNG_CONSUMER_KERNEL
:
1622 return lttng_kconsumer_take_snapshot(stream
);
1623 case LTTNG_CONSUMER32_UST
:
1624 case LTTNG_CONSUMER64_UST
:
1625 return lttng_ustconsumer_take_snapshot(stream
);
1627 ERR("Unknown consumer_data type");
1634 * Get the produced position
1636 * Returns 0 on success, < 0 on error
1638 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1641 switch (consumer_data
.type
) {
1642 case LTTNG_CONSUMER_KERNEL
:
1643 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1644 case LTTNG_CONSUMER32_UST
:
1645 case LTTNG_CONSUMER64_UST
:
1646 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1648 ERR("Unknown consumer_data type");
1654 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1655 int sock
, struct pollfd
*consumer_sockpoll
)
1657 switch (consumer_data
.type
) {
1658 case LTTNG_CONSUMER_KERNEL
:
1659 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1660 case LTTNG_CONSUMER32_UST
:
1661 case LTTNG_CONSUMER64_UST
:
1662 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1664 ERR("Unknown consumer_data type");
1671 * Iterate over all streams of the hashtable and free them properly.
1673 * WARNING: *MUST* be used with data stream only.
1675 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1677 struct lttng_ht_iter iter
;
1678 struct lttng_consumer_stream
*stream
;
1685 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1687 * Ignore return value since we are currently cleaning up so any error
1690 (void) consumer_del_stream(stream
, ht
);
1694 lttng_ht_destroy(ht
);
1698 * Iterate over all streams of the hashtable and free them properly.
1700 * XXX: Should not be only for metadata stream or else use an other name.
1702 static void destroy_stream_ht(struct lttng_ht
*ht
)
1704 struct lttng_ht_iter iter
;
1705 struct lttng_consumer_stream
*stream
;
1712 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1714 * Ignore return value since we are currently cleaning up so any error
1717 (void) consumer_del_metadata_stream(stream
, ht
);
1721 lttng_ht_destroy(ht
);
1724 void lttng_consumer_close_metadata(void)
1726 switch (consumer_data
.type
) {
1727 case LTTNG_CONSUMER_KERNEL
:
1729 * The Kernel consumer has a different metadata scheme so we don't
1730 * close anything because the stream will be closed by the session
1734 case LTTNG_CONSUMER32_UST
:
1735 case LTTNG_CONSUMER64_UST
:
1737 * Close all metadata streams. The metadata hash table is passed and
1738 * this call iterates over it by closing all wakeup fd. This is safe
1739 * because at this point we are sure that the metadata producer is
1740 * either dead or blocked.
1742 lttng_ustconsumer_close_metadata(metadata_ht
);
1745 ERR("Unknown consumer_data type");
1751 * Clean up a metadata stream and free its memory.
1753 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1754 struct lttng_ht
*ht
)
1757 struct lttng_ht_iter iter
;
1758 struct lttng_consumer_channel
*free_chan
= NULL
;
1759 struct consumer_relayd_sock_pair
*relayd
;
1763 * This call should NEVER receive regular stream. It must always be
1764 * metadata stream and this is crucial for data structure synchronization.
1766 assert(stream
->metadata_flag
);
1768 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1771 /* Means the stream was allocated but not successfully added */
1772 goto free_stream_rcu
;
1775 pthread_mutex_lock(&consumer_data
.lock
);
1776 pthread_mutex_lock(&stream
->lock
);
1778 switch (consumer_data
.type
) {
1779 case LTTNG_CONSUMER_KERNEL
:
1780 if (stream
->mmap_base
!= NULL
) {
1781 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1783 PERROR("munmap metadata stream");
1787 if (stream
->wait_fd
>= 0) {
1788 ret
= close(stream
->wait_fd
);
1790 PERROR("close kernel metadata wait_fd");
1794 case LTTNG_CONSUMER32_UST
:
1795 case LTTNG_CONSUMER64_UST
:
1796 lttng_ustconsumer_del_stream(stream
);
1799 ERR("Unknown consumer_data type");
1805 iter
.iter
.node
= &stream
->node
.node
;
1806 ret
= lttng_ht_del(ht
, &iter
);
1809 iter
.iter
.node
= &stream
->node_channel_id
.node
;
1810 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
1813 iter
.iter
.node
= &stream
->node_session_id
.node
;
1814 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1818 if (stream
->out_fd
>= 0) {
1819 ret
= close(stream
->out_fd
);
1825 /* Check and cleanup relayd */
1827 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1828 if (relayd
!= NULL
) {
1829 uatomic_dec(&relayd
->refcount
);
1830 assert(uatomic_read(&relayd
->refcount
) >= 0);
1832 /* Closing streams requires to lock the control socket. */
1833 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1834 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1835 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1836 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1838 DBG("Unable to close stream on the relayd. Continuing");
1840 * Continue here. There is nothing we can do for the relayd.
1841 * Chances are that the relayd has closed the socket so we just
1842 * continue cleaning up.
1846 /* Both conditions are met, we destroy the relayd. */
1847 if (uatomic_read(&relayd
->refcount
) == 0 &&
1848 uatomic_read(&relayd
->destroy_flag
)) {
1849 consumer_destroy_relayd(relayd
);
1854 /* Atomically decrement channel refcount since other threads can use it. */
1855 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
1856 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
1857 /* Go for channel deletion! */
1858 free_chan
= stream
->chan
;
1863 * Nullify the stream reference so it is not used after deletion. The
1864 * consumer data lock MUST be acquired before being able to check for a
1865 * NULL pointer value.
1867 stream
->chan
->metadata_stream
= NULL
;
1869 pthread_mutex_unlock(&stream
->lock
);
1870 pthread_mutex_unlock(&consumer_data
.lock
);
1873 consumer_del_channel(free_chan
);
1877 call_rcu(&stream
->node
.head
, free_stream_rcu
);
1881 * Action done with the metadata stream when adding it to the consumer internal
1882 * data structures to handle it.
1884 static int add_metadata_stream(struct lttng_consumer_stream
*stream
,
1885 struct lttng_ht
*ht
)
1888 struct consumer_relayd_sock_pair
*relayd
;
1889 struct lttng_ht_iter iter
;
1890 struct lttng_ht_node_u64
*node
;
1895 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
1897 pthread_mutex_lock(&consumer_data
.lock
);
1898 pthread_mutex_lock(&stream
->lock
);
1901 * From here, refcounts are updated so be _careful_ when returning an error
1908 * Lookup the stream just to make sure it does not exist in our internal
1909 * state. This should NEVER happen.
1911 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
1912 node
= lttng_ht_iter_get_node_u64(&iter
);
1915 /* Find relayd and, if one is found, increment refcount. */
1916 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1917 if (relayd
!= NULL
) {
1918 uatomic_inc(&relayd
->refcount
);
1922 * When nb_init_stream_left reaches 0, we don't need to trigger any action
1923 * in terms of destroying the associated channel, because the action that
1924 * causes the count to become 0 also causes a stream to be added. The
1925 * channel deletion will thus be triggered by the following removal of this
1928 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
1929 /* Increment refcount before decrementing nb_init_stream_left */
1931 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
1934 lttng_ht_add_unique_u64(ht
, &stream
->node
);
1936 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
1937 &stream
->node_channel_id
);
1940 * Add stream to the stream_list_ht of the consumer data. No need to steal
1941 * the key since the HT does not use it and we allow to add redundant keys
1944 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
1948 pthread_mutex_unlock(&stream
->lock
);
1949 pthread_mutex_unlock(&consumer_data
.lock
);
1954 * Delete data stream that are flagged for deletion (endpoint_status).
1956 static void validate_endpoint_status_data_stream(void)
1958 struct lttng_ht_iter iter
;
1959 struct lttng_consumer_stream
*stream
;
1961 DBG("Consumer delete flagged data stream");
1964 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1965 /* Validate delete flag of the stream */
1966 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
1969 /* Delete it right now */
1970 consumer_del_stream(stream
, data_ht
);
1976 * Delete metadata stream that are flagged for deletion (endpoint_status).
1978 static void validate_endpoint_status_metadata_stream(
1979 struct lttng_poll_event
*pollset
)
1981 struct lttng_ht_iter iter
;
1982 struct lttng_consumer_stream
*stream
;
1984 DBG("Consumer delete flagged metadata stream");
1989 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1990 /* Validate delete flag of the stream */
1991 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
1995 * Remove from pollset so the metadata thread can continue without
1996 * blocking on a deleted stream.
1998 lttng_poll_del(pollset
, stream
->wait_fd
);
2000 /* Delete it right now */
2001 consumer_del_metadata_stream(stream
, metadata_ht
);
2007 * Thread polls on metadata file descriptor and write them on disk or on the
2010 void *consumer_thread_metadata_poll(void *data
)
2013 uint32_t revents
, nb_fd
;
2014 struct lttng_consumer_stream
*stream
= NULL
;
2015 struct lttng_ht_iter iter
;
2016 struct lttng_ht_node_u64
*node
;
2017 struct lttng_poll_event events
;
2018 struct lttng_consumer_local_data
*ctx
= data
;
2021 rcu_register_thread();
2023 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2025 /* ENOMEM at this point. Better to bail out. */
2029 DBG("Thread metadata poll started");
2031 /* Size is set to 1 for the consumer_metadata pipe */
2032 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2034 ERR("Poll set creation failed");
2038 ret
= lttng_poll_add(&events
,
2039 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2045 DBG("Metadata main loop started");
2048 /* Only the metadata pipe is set */
2049 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2054 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2055 ret
= lttng_poll_wait(&events
, -1);
2056 DBG("Metadata event catched in thread");
2058 if (errno
== EINTR
) {
2059 ERR("Poll EINTR catched");
2067 /* From here, the event is a metadata wait fd */
2068 for (i
= 0; i
< nb_fd
; i
++) {
2069 revents
= LTTNG_POLL_GETEV(&events
, i
);
2070 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2072 /* Just don't waste time if no returned events for the fd */
2077 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2078 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2079 DBG("Metadata thread pipe hung up");
2081 * Remove the pipe from the poll set and continue the loop
2082 * since their might be data to consume.
2084 lttng_poll_del(&events
,
2085 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2086 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2088 } else if (revents
& LPOLLIN
) {
2091 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2092 &stream
, sizeof(stream
));
2094 ERR("read metadata stream, ret: %ld", pipe_len
);
2096 * Continue here to handle the rest of the streams.
2101 /* A NULL stream means that the state has changed. */
2102 if (stream
== NULL
) {
2103 /* Check for deleted streams. */
2104 validate_endpoint_status_metadata_stream(&events
);
2108 DBG("Adding metadata stream %d to poll set",
2111 ret
= add_metadata_stream(stream
, metadata_ht
);
2113 ERR("Unable to add metadata stream");
2114 /* Stream was not setup properly. Continuing. */
2115 consumer_del_metadata_stream(stream
, NULL
);
2119 /* Add metadata stream to the global poll events list */
2120 lttng_poll_add(&events
, stream
->wait_fd
,
2121 LPOLLIN
| LPOLLPRI
);
2124 /* Handle other stream */
2130 uint64_t tmp_id
= (uint64_t) pollfd
;
2132 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2134 node
= lttng_ht_iter_get_node_u64(&iter
);
2137 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2140 /* Check for error event */
2141 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2142 DBG("Metadata fd %d is hup|err.", pollfd
);
2143 if (!stream
->hangup_flush_done
2144 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2145 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2146 DBG("Attempting to flush and consume the UST buffers");
2147 lttng_ustconsumer_on_stream_hangup(stream
);
2149 /* We just flushed the stream now read it. */
2151 len
= ctx
->on_buffer_ready(stream
, ctx
);
2153 * We don't check the return value here since if we get
2154 * a negative len, it means an error occured thus we
2155 * simply remove it from the poll set and free the
2161 lttng_poll_del(&events
, stream
->wait_fd
);
2163 * This call update the channel states, closes file descriptors
2164 * and securely free the stream.
2166 consumer_del_metadata_stream(stream
, metadata_ht
);
2167 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2168 /* Get the data out of the metadata file descriptor */
2169 DBG("Metadata available on fd %d", pollfd
);
2170 assert(stream
->wait_fd
== pollfd
);
2172 len
= ctx
->on_buffer_ready(stream
, ctx
);
2173 /* It's ok to have an unavailable sub-buffer */
2174 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2175 /* Clean up stream from consumer and free it. */
2176 lttng_poll_del(&events
, stream
->wait_fd
);
2177 consumer_del_metadata_stream(stream
, metadata_ht
);
2178 } else if (len
> 0) {
2179 stream
->data_read
= 1;
2183 /* Release RCU lock for the stream looked up */
2190 DBG("Metadata poll thread exiting");
2192 lttng_poll_clean(&events
);
2194 destroy_stream_ht(metadata_ht
);
2196 rcu_unregister_thread();
2201 * This thread polls the fds in the set to consume the data and write
2202 * it to tracefile if necessary.
2204 void *consumer_thread_data_poll(void *data
)
2206 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2207 struct pollfd
*pollfd
= NULL
;
2208 /* local view of the streams */
2209 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2210 /* local view of consumer_data.fds_count */
2212 struct lttng_consumer_local_data
*ctx
= data
;
2215 rcu_register_thread();
2217 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2218 if (data_ht
== NULL
) {
2219 /* ENOMEM at this point. Better to bail out. */
2223 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
2230 * the fds set has been updated, we need to update our
2231 * local array as well
2233 pthread_mutex_lock(&consumer_data
.lock
);
2234 if (consumer_data
.need_update
) {
2239 local_stream
= NULL
;
2241 /* allocate for all fds + 1 for the consumer_data_pipe */
2242 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2243 if (pollfd
== NULL
) {
2244 PERROR("pollfd malloc");
2245 pthread_mutex_unlock(&consumer_data
.lock
);
2249 /* allocate for all fds + 1 for the consumer_data_pipe */
2250 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2251 sizeof(struct lttng_consumer_stream
*));
2252 if (local_stream
== NULL
) {
2253 PERROR("local_stream malloc");
2254 pthread_mutex_unlock(&consumer_data
.lock
);
2257 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2260 ERR("Error in allocating pollfd or local_outfds");
2261 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2262 pthread_mutex_unlock(&consumer_data
.lock
);
2266 consumer_data
.need_update
= 0;
2268 pthread_mutex_unlock(&consumer_data
.lock
);
2270 /* No FDs and consumer_quit, consumer_cleanup the thread */
2271 if (nb_fd
== 0 && consumer_quit
== 1) {
2274 /* poll on the array of fds */
2276 DBG("polling on %d fd", nb_fd
+ 1);
2277 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2278 DBG("poll num_rdy : %d", num_rdy
);
2279 if (num_rdy
== -1) {
2281 * Restart interrupted system call.
2283 if (errno
== EINTR
) {
2286 PERROR("Poll error");
2287 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2289 } else if (num_rdy
== 0) {
2290 DBG("Polling thread timed out");
2295 * If the consumer_data_pipe triggered poll go directly to the
2296 * beginning of the loop to update the array. We want to prioritize
2297 * array update over low-priority reads.
2299 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2300 ssize_t pipe_readlen
;
2302 DBG("consumer_data_pipe wake up");
2303 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2304 &new_stream
, sizeof(new_stream
));
2305 if (pipe_readlen
< 0) {
2306 ERR("Consumer data pipe ret %ld", pipe_readlen
);
2307 /* Continue so we can at least handle the current stream(s). */
2312 * If the stream is NULL, just ignore it. It's also possible that
2313 * the sessiond poll thread changed the consumer_quit state and is
2314 * waking us up to test it.
2316 if (new_stream
== NULL
) {
2317 validate_endpoint_status_data_stream();
2321 ret
= add_stream(new_stream
, data_ht
);
2323 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
2326 * At this point, if the add_stream fails, it is not in the
2327 * hash table thus passing the NULL value here.
2329 consumer_del_stream(new_stream
, NULL
);
2332 /* Continue to update the local streams and handle prio ones */
2336 /* Take care of high priority channels first. */
2337 for (i
= 0; i
< nb_fd
; i
++) {
2338 if (local_stream
[i
] == NULL
) {
2341 if (pollfd
[i
].revents
& POLLPRI
) {
2342 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2344 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2345 /* it's ok to have an unavailable sub-buffer */
2346 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2347 /* Clean the stream and free it. */
2348 consumer_del_stream(local_stream
[i
], data_ht
);
2349 local_stream
[i
] = NULL
;
2350 } else if (len
> 0) {
2351 local_stream
[i
]->data_read
= 1;
2357 * If we read high prio channel in this loop, try again
2358 * for more high prio data.
2364 /* Take care of low priority channels. */
2365 for (i
= 0; i
< nb_fd
; i
++) {
2366 if (local_stream
[i
] == NULL
) {
2369 if ((pollfd
[i
].revents
& POLLIN
) ||
2370 local_stream
[i
]->hangup_flush_done
) {
2371 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2372 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2373 /* it's ok to have an unavailable sub-buffer */
2374 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2375 /* Clean the stream and free it. */
2376 consumer_del_stream(local_stream
[i
], data_ht
);
2377 local_stream
[i
] = NULL
;
2378 } else if (len
> 0) {
2379 local_stream
[i
]->data_read
= 1;
2384 /* Handle hangup and errors */
2385 for (i
= 0; i
< nb_fd
; i
++) {
2386 if (local_stream
[i
] == NULL
) {
2389 if (!local_stream
[i
]->hangup_flush_done
2390 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2391 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2392 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2393 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2395 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2396 /* Attempt read again, for the data we just flushed. */
2397 local_stream
[i
]->data_read
= 1;
2400 * If the poll flag is HUP/ERR/NVAL and we have
2401 * read no data in this pass, we can remove the
2402 * stream from its hash table.
2404 if ((pollfd
[i
].revents
& POLLHUP
)) {
2405 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2406 if (!local_stream
[i
]->data_read
) {
2407 consumer_del_stream(local_stream
[i
], data_ht
);
2408 local_stream
[i
] = NULL
;
2411 } else if (pollfd
[i
].revents
& POLLERR
) {
2412 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2413 if (!local_stream
[i
]->data_read
) {
2414 consumer_del_stream(local_stream
[i
], data_ht
);
2415 local_stream
[i
] = NULL
;
2418 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2419 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2420 if (!local_stream
[i
]->data_read
) {
2421 consumer_del_stream(local_stream
[i
], data_ht
);
2422 local_stream
[i
] = NULL
;
2426 if (local_stream
[i
] != NULL
) {
2427 local_stream
[i
]->data_read
= 0;
2432 DBG("polling thread exiting");
2437 * Close the write side of the pipe so epoll_wait() in
2438 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2439 * read side of the pipe. If we close them both, epoll_wait strangely does
2440 * not return and could create a endless wait period if the pipe is the
2441 * only tracked fd in the poll set. The thread will take care of closing
2444 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2446 destroy_data_stream_ht(data_ht
);
2448 rcu_unregister_thread();
2453 * Close wake-up end of each stream belonging to the channel. This will
2454 * allow the poll() on the stream read-side to detect when the
2455 * write-side (application) finally closes them.
2458 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2460 struct lttng_ht
*ht
;
2461 struct lttng_consumer_stream
*stream
;
2462 struct lttng_ht_iter iter
;
2464 ht
= consumer_data
.stream_per_chan_id_ht
;
2467 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2468 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2469 ht
->match_fct
, &channel
->key
,
2470 &iter
.iter
, stream
, node_channel_id
.node
) {
2472 * Protect against teardown with mutex.
2474 pthread_mutex_lock(&stream
->lock
);
2475 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2478 switch (consumer_data
.type
) {
2479 case LTTNG_CONSUMER_KERNEL
:
2481 case LTTNG_CONSUMER32_UST
:
2482 case LTTNG_CONSUMER64_UST
:
2484 * Note: a mutex is taken internally within
2485 * liblttng-ust-ctl to protect timer wakeup_fd
2486 * use from concurrent close.
2488 lttng_ustconsumer_close_stream_wakeup(stream
);
2491 ERR("Unknown consumer_data type");
2495 pthread_mutex_unlock(&stream
->lock
);
2500 static void destroy_channel_ht(struct lttng_ht
*ht
)
2502 struct lttng_ht_iter iter
;
2503 struct lttng_consumer_channel
*channel
;
2511 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2512 ret
= lttng_ht_del(ht
, &iter
);
2517 lttng_ht_destroy(ht
);
2521 * This thread polls the channel fds to detect when they are being
2522 * closed. It closes all related streams if the channel is detected as
2523 * closed. It is currently only used as a shim layer for UST because the
2524 * consumerd needs to keep the per-stream wakeup end of pipes open for
2527 void *consumer_thread_channel_poll(void *data
)
2530 uint32_t revents
, nb_fd
;
2531 struct lttng_consumer_channel
*chan
= NULL
;
2532 struct lttng_ht_iter iter
;
2533 struct lttng_ht_node_u64
*node
;
2534 struct lttng_poll_event events
;
2535 struct lttng_consumer_local_data
*ctx
= data
;
2536 struct lttng_ht
*channel_ht
;
2538 rcu_register_thread();
2540 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2542 /* ENOMEM at this point. Better to bail out. */
2546 DBG("Thread channel poll started");
2548 /* Size is set to 1 for the consumer_channel pipe */
2549 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2551 ERR("Poll set creation failed");
2555 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2561 DBG("Channel main loop started");
2564 /* Only the channel pipe is set */
2565 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2570 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2571 ret
= lttng_poll_wait(&events
, -1);
2572 DBG("Channel event catched in thread");
2574 if (errno
== EINTR
) {
2575 ERR("Poll EINTR catched");
2583 /* From here, the event is a channel wait fd */
2584 for (i
= 0; i
< nb_fd
; i
++) {
2585 revents
= LTTNG_POLL_GETEV(&events
, i
);
2586 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2588 /* Just don't waste time if no returned events for the fd */
2592 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2593 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2594 DBG("Channel thread pipe hung up");
2596 * Remove the pipe from the poll set and continue the loop
2597 * since their might be data to consume.
2599 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2601 } else if (revents
& LPOLLIN
) {
2602 enum consumer_channel_action action
;
2605 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2607 ERR("Error reading channel pipe");
2612 case CONSUMER_CHANNEL_ADD
:
2613 DBG("Adding channel %d to poll set",
2616 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2619 lttng_ht_add_unique_u64(channel_ht
,
2620 &chan
->wait_fd_node
);
2622 /* Add channel to the global poll events list */
2623 lttng_poll_add(&events
, chan
->wait_fd
,
2624 LPOLLIN
| LPOLLPRI
);
2626 case CONSUMER_CHANNEL_DEL
:
2628 struct lttng_consumer_stream
*stream
, *stmp
;
2631 chan
= consumer_find_channel(key
);
2634 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2637 lttng_poll_del(&events
, chan
->wait_fd
);
2638 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2639 ret
= lttng_ht_del(channel_ht
, &iter
);
2641 consumer_close_channel_streams(chan
);
2643 switch (consumer_data
.type
) {
2644 case LTTNG_CONSUMER_KERNEL
:
2646 case LTTNG_CONSUMER32_UST
:
2647 case LTTNG_CONSUMER64_UST
:
2648 /* Delete streams that might have been left in the stream list. */
2649 cds_list_for_each_entry_safe(stream
, stmp
, &chan
->streams
.head
,
2651 cds_list_del(&stream
->send_node
);
2652 lttng_ustconsumer_del_stream(stream
);
2653 uatomic_sub(&stream
->chan
->refcount
, 1);
2654 assert(&chan
->refcount
);
2659 ERR("Unknown consumer_data type");
2664 * Release our own refcount. Force channel deletion even if
2665 * streams were not initialized.
2667 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2668 consumer_del_channel(chan
);
2673 case CONSUMER_CHANNEL_QUIT
:
2675 * Remove the pipe from the poll set and continue the loop
2676 * since their might be data to consume.
2678 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2681 ERR("Unknown action");
2686 /* Handle other stream */
2692 uint64_t tmp_id
= (uint64_t) pollfd
;
2694 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2696 node
= lttng_ht_iter_get_node_u64(&iter
);
2699 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2702 /* Check for error event */
2703 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2704 DBG("Channel fd %d is hup|err.", pollfd
);
2706 lttng_poll_del(&events
, chan
->wait_fd
);
2707 ret
= lttng_ht_del(channel_ht
, &iter
);
2709 assert(cds_list_empty(&chan
->streams
.head
));
2710 consumer_close_channel_streams(chan
);
2712 /* Release our own refcount */
2713 if (!uatomic_sub_return(&chan
->refcount
, 1)
2714 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2715 consumer_del_channel(chan
);
2719 /* Release RCU lock for the channel looked up */
2725 lttng_poll_clean(&events
);
2727 destroy_channel_ht(channel_ht
);
2729 DBG("Channel poll thread exiting");
2730 rcu_unregister_thread();
2734 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2735 struct pollfd
*sockpoll
, int client_socket
)
2742 if (lttng_consumer_poll_socket(sockpoll
) < 0) {
2746 DBG("Metadata connection on client_socket");
2748 /* Blocking call, waiting for transmission */
2749 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2750 if (ctx
->consumer_metadata_socket
< 0) {
2751 WARN("On accept metadata");
2762 * This thread listens on the consumerd socket and receives the file
2763 * descriptors from the session daemon.
2765 void *consumer_thread_sessiond_poll(void *data
)
2767 int sock
= -1, client_socket
, ret
;
2769 * structure to poll for incoming data on communication socket avoids
2770 * making blocking sockets.
2772 struct pollfd consumer_sockpoll
[2];
2773 struct lttng_consumer_local_data
*ctx
= data
;
2775 rcu_register_thread();
2777 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2778 unlink(ctx
->consumer_command_sock_path
);
2779 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2780 if (client_socket
< 0) {
2781 ERR("Cannot create command socket");
2785 ret
= lttcomm_listen_unix_sock(client_socket
);
2790 DBG("Sending ready command to lttng-sessiond");
2791 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2792 /* return < 0 on error, but == 0 is not fatal */
2794 ERR("Error sending ready command to lttng-sessiond");
2798 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2799 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2800 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2801 consumer_sockpoll
[1].fd
= client_socket
;
2802 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2804 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2807 DBG("Connection on client_socket");
2809 /* Blocking call, waiting for transmission */
2810 sock
= lttcomm_accept_unix_sock(client_socket
);
2817 * Setup metadata socket which is the second socket connection on the
2818 * command unix socket.
2820 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
2825 /* This socket is not useful anymore. */
2826 ret
= close(client_socket
);
2828 PERROR("close client_socket");
2832 /* update the polling structure to poll on the established socket */
2833 consumer_sockpoll
[1].fd
= sock
;
2834 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2837 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2840 DBG("Incoming command on sock");
2841 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2842 if (ret
== -ENOENT
) {
2843 DBG("Received STOP command");
2848 * This could simply be a session daemon quitting. Don't output
2851 DBG("Communication interrupted on command socket");
2854 if (consumer_quit
) {
2855 DBG("consumer_thread_receive_fds received quit from signal");
2858 DBG("received command on sock");
2861 DBG("Consumer thread sessiond poll exiting");
2864 * Close metadata streams since the producer is the session daemon which
2867 * NOTE: for now, this only applies to the UST tracer.
2869 lttng_consumer_close_metadata();
2872 * when all fds have hung up, the polling thread
2878 * Notify the data poll thread to poll back again and test the
2879 * consumer_quit state that we just set so to quit gracefully.
2881 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
2883 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
2885 /* Cleaning up possibly open sockets. */
2889 PERROR("close sock sessiond poll");
2892 if (client_socket
>= 0) {
2893 ret
= close(client_socket
);
2895 PERROR("close client_socket sessiond poll");
2899 rcu_unregister_thread();
2903 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2904 struct lttng_consumer_local_data
*ctx
)
2908 pthread_mutex_lock(&stream
->lock
);
2910 switch (consumer_data
.type
) {
2911 case LTTNG_CONSUMER_KERNEL
:
2912 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
2914 case LTTNG_CONSUMER32_UST
:
2915 case LTTNG_CONSUMER64_UST
:
2916 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2919 ERR("Unknown consumer_data type");
2925 pthread_mutex_unlock(&stream
->lock
);
2929 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2931 switch (consumer_data
.type
) {
2932 case LTTNG_CONSUMER_KERNEL
:
2933 return lttng_kconsumer_on_recv_stream(stream
);
2934 case LTTNG_CONSUMER32_UST
:
2935 case LTTNG_CONSUMER64_UST
:
2936 return lttng_ustconsumer_on_recv_stream(stream
);
2938 ERR("Unknown consumer_data type");
2945 * Allocate and set consumer data hash tables.
2947 void lttng_consumer_init(void)
2949 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2950 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2951 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2952 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2956 * Process the ADD_RELAYD command receive by a consumer.
2958 * This will create a relayd socket pair and add it to the relayd hash table.
2959 * The caller MUST acquire a RCU read side lock before calling it.
2961 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
2962 struct lttng_consumer_local_data
*ctx
, int sock
,
2963 struct pollfd
*consumer_sockpoll
,
2964 struct lttcomm_relayd_sock
*relayd_sock
, unsigned int sessiond_id
)
2966 int fd
= -1, ret
= -1, relayd_created
= 0;
2967 enum lttng_error_code ret_code
= LTTNG_OK
;
2968 struct consumer_relayd_sock_pair
*relayd
= NULL
;
2971 assert(relayd_sock
);
2973 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
2975 /* Get relayd reference if exists. */
2976 relayd
= consumer_find_relayd(net_seq_idx
);
2977 if (relayd
== NULL
) {
2978 /* Not found. Allocate one. */
2979 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
2980 if (relayd
== NULL
) {
2981 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
2984 relayd
->sessiond_session_id
= (uint64_t) sessiond_id
;
2989 * This code path MUST continue to the consumer send status message to
2990 * we can notify the session daemon and continue our work without
2991 * killing everything.
2995 /* First send a status message before receiving the fds. */
2996 ret
= consumer_send_status_msg(sock
, ret_code
);
2997 if (ret
< 0 || ret_code
!= LTTNG_OK
) {
2998 /* Somehow, the session daemon is not responding anymore. */
3002 /* Poll on consumer socket. */
3003 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
3004 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3009 /* Get relayd socket from session daemon */
3010 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3011 if (ret
!= sizeof(fd
)) {
3012 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3014 fd
= -1; /* Just in case it gets set with an invalid value. */
3017 * Failing to receive FDs might indicate a major problem such as
3018 * reaching a fd limit during the receive where the kernel returns a
3019 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3020 * don't take any chances and stop everything.
3022 * XXX: Feature request #558 will fix that and avoid this possible
3023 * issue when reaching the fd limit.
3025 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3028 * This code path MUST continue to the consumer send status message so
3029 * we can send the error to the thread expecting a reply. The above
3030 * call will make everything stop.
3034 /* We have the fds without error. Send status back. */
3035 ret
= consumer_send_status_msg(sock
, ret_code
);
3036 if (ret
< 0 || ret_code
!= LTTNG_OK
) {
3037 /* Somehow, the session daemon is not responding anymore. */
3041 /* Copy socket information and received FD */
3042 switch (sock_type
) {
3043 case LTTNG_STREAM_CONTROL
:
3044 /* Copy received lttcomm socket */
3045 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3046 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3047 /* Immediately try to close the created socket if valid. */
3048 if (relayd
->control_sock
.sock
.fd
>= 0) {
3049 if (close(relayd
->control_sock
.sock
.fd
)) {
3050 PERROR("close relayd control socket");
3053 /* Handle create_sock error. */
3058 /* Assign new file descriptor */
3059 relayd
->control_sock
.sock
.fd
= fd
;
3060 /* Assign version values. */
3061 relayd
->control_sock
.major
= relayd_sock
->major
;
3062 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3065 * Create a session on the relayd and store the returned id. Lock the
3066 * control socket mutex if the relayd was NOT created before.
3068 if (!relayd_created
) {
3069 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3071 ret
= relayd_create_session(&relayd
->control_sock
,
3072 &relayd
->relayd_session_id
);
3073 if (!relayd_created
) {
3074 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3078 * Close all sockets of a relayd object. It will be freed if it was
3079 * created at the error code path or else it will be garbage
3082 (void) relayd_close(&relayd
->control_sock
);
3083 (void) relayd_close(&relayd
->data_sock
);
3088 case LTTNG_STREAM_DATA
:
3089 /* Copy received lttcomm socket */
3090 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3091 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3092 /* Immediately try to close the created socket if valid. */
3093 if (relayd
->data_sock
.sock
.fd
>= 0) {
3094 if (close(relayd
->data_sock
.sock
.fd
)) {
3095 PERROR("close relayd data socket");
3098 /* Handle create_sock error. */
3103 /* Assign new file descriptor */
3104 relayd
->data_sock
.sock
.fd
= fd
;
3105 /* Assign version values. */
3106 relayd
->data_sock
.major
= relayd_sock
->major
;
3107 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3110 ERR("Unknown relayd socket type (%d)", sock_type
);
3115 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3116 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3117 relayd
->net_seq_idx
, fd
);
3120 * Add relayd socket pair to consumer data hashtable. If object already
3121 * exists or on error, the function gracefully returns.
3129 /* Close received socket if valid. */
3132 PERROR("close received socket");
3136 if (relayd_created
) {
3144 * Try to lock the stream mutex.
3146 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3148 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3155 * Try to lock the stream mutex. On failure, we know that the stream is
3156 * being used else where hence there is data still being extracted.
3158 ret
= pthread_mutex_trylock(&stream
->lock
);
3160 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3172 * Search for a relayd associated to the session id and return the reference.
3174 * A rcu read side lock MUST be acquire before calling this function and locked
3175 * until the relayd object is no longer necessary.
3177 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3179 struct lttng_ht_iter iter
;
3180 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3182 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3183 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3186 * Check by sessiond id which is unique here where the relayd session
3187 * id might not be when having multiple relayd.
3189 if (relayd
->sessiond_session_id
== id
) {
3190 /* Found the relayd. There can be only one per id. */
3202 * Check if for a given session id there is still data needed to be extract
3205 * Return 1 if data is pending or else 0 meaning ready to be read.
3207 int consumer_data_pending(uint64_t id
)
3210 struct lttng_ht_iter iter
;
3211 struct lttng_ht
*ht
;
3212 struct lttng_consumer_stream
*stream
;
3213 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3214 int (*data_pending
)(struct lttng_consumer_stream
*);
3216 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3219 pthread_mutex_lock(&consumer_data
.lock
);
3221 switch (consumer_data
.type
) {
3222 case LTTNG_CONSUMER_KERNEL
:
3223 data_pending
= lttng_kconsumer_data_pending
;
3225 case LTTNG_CONSUMER32_UST
:
3226 case LTTNG_CONSUMER64_UST
:
3227 data_pending
= lttng_ustconsumer_data_pending
;
3230 ERR("Unknown consumer data type");
3234 /* Ease our life a bit */
3235 ht
= consumer_data
.stream_list_ht
;
3237 relayd
= find_relayd_by_session_id(id
);
3239 /* Send init command for data pending. */
3240 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3241 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3242 relayd
->relayd_session_id
);
3243 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3245 /* Communication error thus the relayd so no data pending. */
3246 goto data_not_pending
;
3250 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3251 ht
->hash_fct(&id
, lttng_ht_seed
),
3253 &iter
.iter
, stream
, node_session_id
.node
) {
3254 /* If this call fails, the stream is being used hence data pending. */
3255 ret
= stream_try_lock(stream
);
3261 * A removed node from the hash table indicates that the stream has
3262 * been deleted thus having a guarantee that the buffers are closed
3263 * on the consumer side. However, data can still be transmitted
3264 * over the network so don't skip the relayd check.
3266 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3268 /* Check the stream if there is data in the buffers. */
3269 ret
= data_pending(stream
);
3271 pthread_mutex_unlock(&stream
->lock
);
3278 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3279 if (stream
->metadata_flag
) {
3280 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3281 stream
->relayd_stream_id
);
3283 ret
= relayd_data_pending(&relayd
->control_sock
,
3284 stream
->relayd_stream_id
,
3285 stream
->next_net_seq_num
- 1);
3287 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3289 pthread_mutex_unlock(&stream
->lock
);
3293 pthread_mutex_unlock(&stream
->lock
);
3297 unsigned int is_data_inflight
= 0;
3299 /* Send init command for data pending. */
3300 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3301 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3302 relayd
->relayd_session_id
, &is_data_inflight
);
3303 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3305 goto data_not_pending
;
3307 if (is_data_inflight
) {
3313 * Finding _no_ node in the hash table and no inflight data means that the
3314 * stream(s) have been removed thus data is guaranteed to be available for
3315 * analysis from the trace files.
3319 /* Data is available to be read by a viewer. */
3320 pthread_mutex_unlock(&consumer_data
.lock
);
3325 /* Data is still being extracted from buffers. */
3326 pthread_mutex_unlock(&consumer_data
.lock
);
3332 * Send a ret code status message to the sessiond daemon.
3334 * Return the sendmsg() return value.
3336 int consumer_send_status_msg(int sock
, int ret_code
)
3338 struct lttcomm_consumer_status_msg msg
;
3340 msg
.ret_code
= ret_code
;
3342 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3346 * Send a channel status message to the sessiond daemon.
3348 * Return the sendmsg() return value.
3350 int consumer_send_status_channel(int sock
,
3351 struct lttng_consumer_channel
*channel
)
3353 struct lttcomm_consumer_status_channel msg
;
3358 msg
.ret_code
= -LTTNG_ERR_UST_CHAN_FAIL
;
3360 msg
.ret_code
= LTTNG_OK
;
3361 msg
.key
= channel
->key
;
3362 msg
.stream_count
= channel
->streams
.count
;
3365 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));