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(uint64_t key
, struct lttng_ht
*ht
)
170 struct lttng_consumer_stream
*stream
;
173 stream
= find_stream(key
, ht
);
175 stream
->key
= (uint64_t) -1ULL;
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
= (uint64_t) -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
);
294 pthread_mutex_lock(&channel
->lock
);
296 /* Delete streams that might have been left in the stream list. */
297 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
299 cds_list_del(&stream
->send_node
);
301 * Once a stream is added to this list, the buffers were created so
302 * we have a guarantee that this call will succeed.
304 consumer_stream_destroy(stream
, NULL
);
307 switch (consumer_data
.type
) {
308 case LTTNG_CONSUMER_KERNEL
:
310 case LTTNG_CONSUMER32_UST
:
311 case LTTNG_CONSUMER64_UST
:
312 lttng_ustconsumer_del_channel(channel
);
315 ERR("Unknown consumer_data type");
321 iter
.iter
.node
= &channel
->node
.node
;
322 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
326 call_rcu(&channel
->node
.head
, free_channel_rcu
);
328 pthread_mutex_unlock(&channel
->lock
);
329 pthread_mutex_unlock(&consumer_data
.lock
);
333 * Iterate over the relayd hash table and destroy each element. Finally,
334 * destroy the whole hash table.
336 static void cleanup_relayd_ht(void)
338 struct lttng_ht_iter iter
;
339 struct consumer_relayd_sock_pair
*relayd
;
343 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
345 consumer_destroy_relayd(relayd
);
350 lttng_ht_destroy(consumer_data
.relayd_ht
);
354 * Update the end point status of all streams having the given network sequence
355 * index (relayd index).
357 * It's atomically set without having the stream mutex locked which is fine
358 * because we handle the write/read race with a pipe wakeup for each thread.
360 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
361 enum consumer_endpoint_status status
)
363 struct lttng_ht_iter iter
;
364 struct lttng_consumer_stream
*stream
;
366 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
370 /* Let's begin with metadata */
371 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
372 if (stream
->net_seq_idx
== net_seq_idx
) {
373 uatomic_set(&stream
->endpoint_status
, status
);
374 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
378 /* Follow up by the data streams */
379 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
380 if (stream
->net_seq_idx
== net_seq_idx
) {
381 uatomic_set(&stream
->endpoint_status
, status
);
382 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
389 * Cleanup a relayd object by flagging every associated streams for deletion,
390 * destroying the object meaning removing it from the relayd hash table,
391 * closing the sockets and freeing the memory in a RCU call.
393 * If a local data context is available, notify the threads that the streams'
394 * state have changed.
396 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
397 struct lttng_consumer_local_data
*ctx
)
403 DBG("Cleaning up relayd sockets");
405 /* Save the net sequence index before destroying the object */
406 netidx
= relayd
->net_seq_idx
;
409 * Delete the relayd from the relayd hash table, close the sockets and free
410 * the object in a RCU call.
412 consumer_destroy_relayd(relayd
);
414 /* Set inactive endpoint to all streams */
415 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
418 * With a local data context, notify the threads that the streams' state
419 * have changed. The write() action on the pipe acts as an "implicit"
420 * memory barrier ordering the updates of the end point status from the
421 * read of this status which happens AFTER receiving this notify.
424 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
425 notify_thread_lttng_pipe(ctx
->consumer_metadata_pipe
);
430 * Flag a relayd socket pair for destruction. Destroy it if the refcount
433 * RCU read side lock MUST be aquired before calling this function.
435 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
439 /* Set destroy flag for this object */
440 uatomic_set(&relayd
->destroy_flag
, 1);
442 /* Destroy the relayd if refcount is 0 */
443 if (uatomic_read(&relayd
->refcount
) == 0) {
444 consumer_destroy_relayd(relayd
);
449 * Completly destroy stream from every visiable data structure and the given
452 * One this call returns, the stream object is not longer usable nor visible.
454 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
457 consumer_stream_destroy(stream
, ht
);
461 * XXX naming of del vs destroy is all mixed up.
463 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
465 consumer_stream_destroy(stream
, data_ht
);
468 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
470 consumer_stream_destroy(stream
, metadata_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
,
484 unsigned int monitor
)
487 struct lttng_consumer_stream
*stream
;
489 stream
= zmalloc(sizeof(*stream
));
490 if (stream
== NULL
) {
491 PERROR("malloc struct lttng_consumer_stream");
498 stream
->key
= stream_key
;
500 stream
->out_fd_offset
= 0;
501 stream
->output_written
= 0;
502 stream
->state
= state
;
505 stream
->net_seq_idx
= relayd_id
;
506 stream
->session_id
= session_id
;
507 stream
->monitor
= monitor
;
508 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
509 pthread_mutex_init(&stream
->lock
, NULL
);
511 /* If channel is the metadata, flag this stream as metadata. */
512 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
513 stream
->metadata_flag
= 1;
514 /* Metadata is flat out. */
515 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
517 /* Format stream name to <channel_name>_<cpu_number> */
518 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
521 PERROR("snprintf stream name");
526 /* Key is always the wait_fd for streams. */
527 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
529 /* Init node per channel id key */
530 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
532 /* Init session id node with the stream session id */
533 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
535 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
536 " relayd_id %" PRIu64
", session_id %" PRIu64
,
537 stream
->name
, stream
->key
, channel_key
,
538 stream
->net_seq_idx
, stream
->session_id
);
554 * Add a stream to the global list protected by a mutex.
556 int consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
558 struct lttng_ht
*ht
= data_ht
;
564 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
566 pthread_mutex_lock(&consumer_data
.lock
);
567 pthread_mutex_lock(&stream
->chan
->lock
);
568 pthread_mutex_lock(&stream
->chan
->timer_lock
);
569 pthread_mutex_lock(&stream
->lock
);
572 /* Steal stream identifier to avoid having streams with the same key */
573 steal_stream_key(stream
->key
, ht
);
575 lttng_ht_add_unique_u64(ht
, &stream
->node
);
577 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
578 &stream
->node_channel_id
);
581 * Add stream to the stream_list_ht of the consumer data. No need to steal
582 * the key since the HT does not use it and we allow to add redundant keys
585 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
588 * When nb_init_stream_left reaches 0, we don't need to trigger any action
589 * in terms of destroying the associated channel, because the action that
590 * causes the count to become 0 also causes a stream to be added. The
591 * channel deletion will thus be triggered by the following removal of this
594 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
595 /* Increment refcount before decrementing nb_init_stream_left */
597 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
600 /* Update consumer data once the node is inserted. */
601 consumer_data
.stream_count
++;
602 consumer_data
.need_update
= 1;
605 pthread_mutex_unlock(&stream
->lock
);
606 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
607 pthread_mutex_unlock(&stream
->chan
->lock
);
608 pthread_mutex_unlock(&consumer_data
.lock
);
613 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
615 consumer_del_stream(stream
, data_ht
);
619 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
620 * be acquired before calling this.
622 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
625 struct lttng_ht_node_u64
*node
;
626 struct lttng_ht_iter iter
;
630 lttng_ht_lookup(consumer_data
.relayd_ht
,
631 &relayd
->net_seq_idx
, &iter
);
632 node
= lttng_ht_iter_get_node_u64(&iter
);
636 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
643 * Allocate and return a consumer relayd socket.
645 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
646 uint64_t net_seq_idx
)
648 struct consumer_relayd_sock_pair
*obj
= NULL
;
650 /* net sequence index of -1 is a failure */
651 if (net_seq_idx
== (uint64_t) -1ULL) {
655 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
657 PERROR("zmalloc relayd sock");
661 obj
->net_seq_idx
= net_seq_idx
;
663 obj
->destroy_flag
= 0;
664 obj
->control_sock
.sock
.fd
= -1;
665 obj
->data_sock
.sock
.fd
= -1;
666 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
667 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
674 * Find a relayd socket pair in the global consumer data.
676 * Return the object if found else NULL.
677 * RCU read-side lock must be held across this call and while using the
680 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
682 struct lttng_ht_iter iter
;
683 struct lttng_ht_node_u64
*node
;
684 struct consumer_relayd_sock_pair
*relayd
= NULL
;
686 /* Negative keys are lookup failures */
687 if (key
== (uint64_t) -1ULL) {
691 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
693 node
= lttng_ht_iter_get_node_u64(&iter
);
695 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
703 * Find a relayd and send the stream
705 * Returns 0 on success, < 0 on error
707 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
711 struct consumer_relayd_sock_pair
*relayd
;
714 assert(stream
->net_seq_idx
!= -1ULL);
717 /* The stream is not metadata. Get relayd reference if exists. */
719 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
720 if (relayd
!= NULL
) {
721 /* Add stream on the relayd */
722 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
723 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
724 path
, &stream
->relayd_stream_id
,
725 stream
->chan
->tracefile_size
, stream
->chan
->tracefile_count
);
726 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
730 uatomic_inc(&relayd
->refcount
);
731 stream
->sent_to_relayd
= 1;
733 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
734 stream
->key
, stream
->net_seq_idx
);
739 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
740 stream
->name
, stream
->key
, stream
->net_seq_idx
);
748 * Find a relayd and close the stream
750 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
752 struct consumer_relayd_sock_pair
*relayd
;
754 /* The stream is not metadata. Get relayd reference if exists. */
756 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
758 consumer_stream_relayd_close(stream
, relayd
);
764 * Handle stream for relayd transmission if the stream applies for network
765 * streaming where the net sequence index is set.
767 * Return destination file descriptor or negative value on error.
769 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
770 size_t data_size
, unsigned long padding
,
771 struct consumer_relayd_sock_pair
*relayd
)
774 struct lttcomm_relayd_data_hdr data_hdr
;
780 /* Reset data header */
781 memset(&data_hdr
, 0, sizeof(data_hdr
));
783 if (stream
->metadata_flag
) {
784 /* Caller MUST acquire the relayd control socket lock */
785 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
790 /* Metadata are always sent on the control socket. */
791 outfd
= relayd
->control_sock
.sock
.fd
;
793 /* Set header with stream information */
794 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
795 data_hdr
.data_size
= htobe32(data_size
);
796 data_hdr
.padding_size
= htobe32(padding
);
798 * Note that net_seq_num below is assigned with the *current* value of
799 * next_net_seq_num and only after that the next_net_seq_num will be
800 * increment. This is why when issuing a command on the relayd using
801 * this next value, 1 should always be substracted in order to compare
802 * the last seen sequence number on the relayd side to the last sent.
804 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
805 /* Other fields are zeroed previously */
807 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
813 ++stream
->next_net_seq_num
;
815 /* Set to go on data socket */
816 outfd
= relayd
->data_sock
.sock
.fd
;
824 * Allocate and return a new lttng_consumer_channel object using the given key
825 * to initialize the hash table node.
827 * On error, return NULL.
829 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
831 const char *pathname
,
836 enum lttng_event_output output
,
837 uint64_t tracefile_size
,
838 uint64_t tracefile_count
,
839 uint64_t session_id_per_pid
,
840 unsigned int monitor
)
842 struct lttng_consumer_channel
*channel
;
844 channel
= zmalloc(sizeof(*channel
));
845 if (channel
== NULL
) {
846 PERROR("malloc struct lttng_consumer_channel");
851 channel
->refcount
= 0;
852 channel
->session_id
= session_id
;
853 channel
->session_id_per_pid
= session_id_per_pid
;
856 channel
->relayd_id
= relayd_id
;
857 channel
->output
= output
;
858 channel
->tracefile_size
= tracefile_size
;
859 channel
->tracefile_count
= tracefile_count
;
860 channel
->monitor
= monitor
;
861 pthread_mutex_init(&channel
->lock
, NULL
);
862 pthread_mutex_init(&channel
->timer_lock
, NULL
);
865 * In monitor mode, the streams associated with the channel will be put in
866 * a special list ONLY owned by this channel. So, the refcount is set to 1
867 * here meaning that the channel itself has streams that are referenced.
869 * On a channel deletion, once the channel is no longer visible, the
870 * refcount is decremented and checked for a zero value to delete it. With
871 * streams in no monitor mode, it will now be safe to destroy the channel.
873 if (!channel
->monitor
) {
874 channel
->refcount
= 1;
877 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
878 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
880 strncpy(channel
->name
, name
, sizeof(channel
->name
));
881 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
883 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
885 channel
->wait_fd
= -1;
887 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
889 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
896 * Add a channel to the global list protected by a mutex.
898 * On success 0 is returned else a negative value.
900 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
901 struct lttng_consumer_local_data
*ctx
)
904 struct lttng_ht_node_u64
*node
;
905 struct lttng_ht_iter iter
;
907 pthread_mutex_lock(&consumer_data
.lock
);
908 pthread_mutex_lock(&channel
->lock
);
909 pthread_mutex_lock(&channel
->timer_lock
);
912 lttng_ht_lookup(consumer_data
.channel_ht
, &channel
->key
, &iter
);
913 node
= lttng_ht_iter_get_node_u64(&iter
);
915 /* Channel already exist. Ignore the insertion */
916 ERR("Consumer add channel key %" PRIu64
" already exists!",
922 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
926 pthread_mutex_unlock(&channel
->timer_lock
);
927 pthread_mutex_unlock(&channel
->lock
);
928 pthread_mutex_unlock(&consumer_data
.lock
);
930 if (!ret
&& channel
->wait_fd
!= -1 &&
931 channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
932 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
938 * Allocate the pollfd structure and the local view of the out fds to avoid
939 * doing a lookup in the linked list and concurrency issues when writing is
940 * needed. Called with consumer_data.lock held.
942 * Returns the number of fds in the structures.
944 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
945 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
949 struct lttng_ht_iter iter
;
950 struct lttng_consumer_stream
*stream
;
955 assert(local_stream
);
957 DBG("Updating poll fd array");
959 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
961 * Only active streams with an active end point can be added to the
962 * poll set and local stream storage of the thread.
964 * There is a potential race here for endpoint_status to be updated
965 * just after the check. However, this is OK since the stream(s) will
966 * be deleted once the thread is notified that the end point state has
967 * changed where this function will be called back again.
969 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
970 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
974 * This clobbers way too much the debug output. Uncomment that if you
975 * need it for debugging purposes.
977 * DBG("Active FD %d", stream->wait_fd);
979 (*pollfd
)[i
].fd
= stream
->wait_fd
;
980 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
981 local_stream
[i
] = stream
;
987 * Insert the consumer_data_pipe at the end of the array and don't
988 * increment i so nb_fd is the number of real FD.
990 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
991 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
996 * Poll on the should_quit pipe and the command socket return -1 on error and
997 * should exit, 0 if data is available on the command socket
999 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1004 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1005 if (num_rdy
== -1) {
1007 * Restart interrupted system call.
1009 if (errno
== EINTR
) {
1012 PERROR("Poll error");
1015 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1016 DBG("consumer_should_quit wake up");
1026 * Set the error socket.
1028 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1031 ctx
->consumer_error_socket
= sock
;
1035 * Set the command socket path.
1037 void lttng_consumer_set_command_sock_path(
1038 struct lttng_consumer_local_data
*ctx
, char *sock
)
1040 ctx
->consumer_command_sock_path
= sock
;
1044 * Send return code to the session daemon.
1045 * If the socket is not defined, we return 0, it is not a fatal error
1047 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1049 if (ctx
->consumer_error_socket
> 0) {
1050 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1051 sizeof(enum lttcomm_sessiond_command
));
1058 * Close all the tracefiles and stream fds and MUST be called when all
1059 * instances are destroyed i.e. when all threads were joined and are ended.
1061 void lttng_consumer_cleanup(void)
1063 struct lttng_ht_iter iter
;
1064 struct lttng_consumer_channel
*channel
;
1068 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1070 consumer_del_channel(channel
);
1075 lttng_ht_destroy(consumer_data
.channel_ht
);
1077 cleanup_relayd_ht();
1079 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1082 * This HT contains streams that are freed by either the metadata thread or
1083 * the data thread so we do *nothing* on the hash table and simply destroy
1086 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1090 * Called from signal handler.
1092 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1097 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1098 } while (ret
< 0 && errno
== EINTR
);
1099 if (ret
< 0 || ret
!= 1) {
1100 PERROR("write consumer quit");
1103 DBG("Consumer flag that it should quit");
1106 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1109 int outfd
= stream
->out_fd
;
1112 * This does a blocking write-and-wait on any page that belongs to the
1113 * subbuffer prior to the one we just wrote.
1114 * Don't care about error values, as these are just hints and ways to
1115 * limit the amount of page cache used.
1117 if (orig_offset
< stream
->max_sb_size
) {
1120 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1121 stream
->max_sb_size
,
1122 SYNC_FILE_RANGE_WAIT_BEFORE
1123 | SYNC_FILE_RANGE_WRITE
1124 | SYNC_FILE_RANGE_WAIT_AFTER
);
1126 * Give hints to the kernel about how we access the file:
1127 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1130 * We need to call fadvise again after the file grows because the
1131 * kernel does not seem to apply fadvise to non-existing parts of the
1134 * Call fadvise _after_ having waited for the page writeback to
1135 * complete because the dirty page writeback semantic is not well
1136 * defined. So it can be expected to lead to lower throughput in
1139 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1140 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1144 * Initialise the necessary environnement :
1145 * - create a new context
1146 * - create the poll_pipe
1147 * - create the should_quit pipe (for signal handler)
1148 * - create the thread pipe (for splice)
1150 * Takes a function pointer as argument, this function is called when data is
1151 * available on a buffer. This function is responsible to do the
1152 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1153 * buffer configuration and then kernctl_put_next_subbuf at the end.
1155 * Returns a pointer to the new context or NULL on error.
1157 struct lttng_consumer_local_data
*lttng_consumer_create(
1158 enum lttng_consumer_type type
,
1159 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1160 struct lttng_consumer_local_data
*ctx
),
1161 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1162 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1163 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1166 struct lttng_consumer_local_data
*ctx
;
1168 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1169 consumer_data
.type
== type
);
1170 consumer_data
.type
= type
;
1172 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1174 PERROR("allocating context");
1178 ctx
->consumer_error_socket
= -1;
1179 ctx
->consumer_metadata_socket
= -1;
1180 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1181 /* assign the callbacks */
1182 ctx
->on_buffer_ready
= buffer_ready
;
1183 ctx
->on_recv_channel
= recv_channel
;
1184 ctx
->on_recv_stream
= recv_stream
;
1185 ctx
->on_update_stream
= update_stream
;
1187 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1188 if (!ctx
->consumer_data_pipe
) {
1189 goto error_poll_pipe
;
1192 ret
= pipe(ctx
->consumer_should_quit
);
1194 PERROR("Error creating recv pipe");
1195 goto error_quit_pipe
;
1198 ret
= pipe(ctx
->consumer_thread_pipe
);
1200 PERROR("Error creating thread pipe");
1201 goto error_thread_pipe
;
1204 ret
= pipe(ctx
->consumer_channel_pipe
);
1206 PERROR("Error creating channel pipe");
1207 goto error_channel_pipe
;
1210 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1211 if (!ctx
->consumer_metadata_pipe
) {
1212 goto error_metadata_pipe
;
1215 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1217 goto error_splice_pipe
;
1223 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1224 error_metadata_pipe
:
1225 utils_close_pipe(ctx
->consumer_channel_pipe
);
1227 utils_close_pipe(ctx
->consumer_thread_pipe
);
1229 utils_close_pipe(ctx
->consumer_should_quit
);
1231 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1239 * Close all fds associated with the instance and free the context.
1241 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1245 DBG("Consumer destroying it. Closing everything.");
1247 ret
= close(ctx
->consumer_error_socket
);
1251 ret
= close(ctx
->consumer_metadata_socket
);
1255 utils_close_pipe(ctx
->consumer_thread_pipe
);
1256 utils_close_pipe(ctx
->consumer_channel_pipe
);
1257 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1258 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1259 utils_close_pipe(ctx
->consumer_should_quit
);
1260 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1262 unlink(ctx
->consumer_command_sock_path
);
1267 * Write the metadata stream id on the specified file descriptor.
1269 static int write_relayd_metadata_id(int fd
,
1270 struct lttng_consumer_stream
*stream
,
1271 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1274 struct lttcomm_relayd_metadata_payload hdr
;
1276 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1277 hdr
.padding_size
= htobe32(padding
);
1279 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1280 } while (ret
< 0 && errno
== EINTR
);
1281 if (ret
< 0 || ret
!= sizeof(hdr
)) {
1283 * This error means that the fd's end is closed so ignore the perror
1284 * not to clubber the error output since this can happen in a normal
1287 if (errno
!= EPIPE
) {
1288 PERROR("write metadata stream id");
1290 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1292 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1293 * handle writting the missing part so report that as an error and
1294 * don't lie to the caller.
1299 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1300 stream
->relayd_stream_id
, padding
);
1307 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1308 * core function for writing trace buffers to either the local filesystem or
1311 * It must be called with the stream lock held.
1313 * Careful review MUST be put if any changes occur!
1315 * Returns the number of bytes written
1317 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1318 struct lttng_consumer_local_data
*ctx
,
1319 struct lttng_consumer_stream
*stream
, unsigned long len
,
1320 unsigned long padding
)
1322 unsigned long mmap_offset
;
1324 ssize_t ret
= 0, written
= 0;
1325 off_t orig_offset
= stream
->out_fd_offset
;
1326 /* Default is on the disk */
1327 int outfd
= stream
->out_fd
;
1328 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1329 unsigned int relayd_hang_up
= 0;
1331 /* RCU lock for the relayd pointer */
1334 /* Flag that the current stream if set for network streaming. */
1335 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1336 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1337 if (relayd
== NULL
) {
1343 /* get the offset inside the fd to mmap */
1344 switch (consumer_data
.type
) {
1345 case LTTNG_CONSUMER_KERNEL
:
1346 mmap_base
= stream
->mmap_base
;
1347 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1349 PERROR("tracer ctl get_mmap_read_offset");
1354 case LTTNG_CONSUMER32_UST
:
1355 case LTTNG_CONSUMER64_UST
:
1356 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1358 ERR("read mmap get mmap base for stream %s", stream
->name
);
1362 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1364 PERROR("tracer ctl get_mmap_read_offset");
1370 ERR("Unknown consumer_data type");
1374 /* Handle stream on the relayd if the output is on the network */
1376 unsigned long netlen
= len
;
1379 * Lock the control socket for the complete duration of the function
1380 * since from this point on we will use the socket.
1382 if (stream
->metadata_flag
) {
1383 /* Metadata requires the control socket. */
1384 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1385 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1388 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1390 /* Use the returned socket. */
1393 /* Write metadata stream id before payload */
1394 if (stream
->metadata_flag
) {
1395 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1398 /* Socket operation failed. We consider the relayd dead */
1399 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1407 /* Socket operation failed. We consider the relayd dead */
1408 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1412 /* Else, use the default set before which is the filesystem. */
1415 /* No streaming, we have to set the len with the full padding */
1419 * Check if we need to change the tracefile before writing the packet.
1421 if (stream
->chan
->tracefile_size
> 0 &&
1422 (stream
->tracefile_size_current
+ len
) >
1423 stream
->chan
->tracefile_size
) {
1424 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1425 stream
->name
, stream
->chan
->tracefile_size
,
1426 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1427 stream
->out_fd
, &(stream
->tracefile_count_current
));
1429 ERR("Rotating output file");
1432 outfd
= stream
->out_fd
= ret
;
1433 /* Reset current size because we just perform a rotation. */
1434 stream
->tracefile_size_current
= 0;
1435 stream
->out_fd_offset
= 0;
1438 stream
->tracefile_size_current
+= len
;
1443 ret
= write(outfd
, mmap_base
+ mmap_offset
, len
);
1444 } while (ret
< 0 && errno
== EINTR
);
1445 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1448 * This is possible if the fd is closed on the other side (outfd)
1449 * or any write problem. It can be verbose a bit for a normal
1450 * execution if for instance the relayd is stopped abruptly. This
1451 * can happen so set this to a DBG statement.
1453 DBG("Error in file write mmap");
1457 /* Socket operation failed. We consider the relayd dead */
1458 if (errno
== EPIPE
|| errno
== EINVAL
) {
1463 } else if (ret
> len
) {
1464 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1472 /* This call is useless on a socket so better save a syscall. */
1474 /* This won't block, but will start writeout asynchronously */
1475 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1476 SYNC_FILE_RANGE_WRITE
);
1477 stream
->out_fd_offset
+= ret
;
1479 stream
->output_written
+= ret
;
1482 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1486 * This is a special case that the relayd has closed its socket. Let's
1487 * cleanup the relayd object and all associated streams.
1489 if (relayd
&& relayd_hang_up
) {
1490 cleanup_relayd(relayd
, ctx
);
1494 /* Unlock only if ctrl socket used */
1495 if (relayd
&& stream
->metadata_flag
) {
1496 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1504 * Splice the data from the ring buffer to the tracefile.
1506 * It must be called with the stream lock held.
1508 * Returns the number of bytes spliced.
1510 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1511 struct lttng_consumer_local_data
*ctx
,
1512 struct lttng_consumer_stream
*stream
, unsigned long len
,
1513 unsigned long padding
)
1515 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1517 off_t orig_offset
= stream
->out_fd_offset
;
1518 int fd
= stream
->wait_fd
;
1519 /* Default is on the disk */
1520 int outfd
= stream
->out_fd
;
1521 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1523 unsigned int relayd_hang_up
= 0;
1525 switch (consumer_data
.type
) {
1526 case LTTNG_CONSUMER_KERNEL
:
1528 case LTTNG_CONSUMER32_UST
:
1529 case LTTNG_CONSUMER64_UST
:
1530 /* Not supported for user space tracing */
1533 ERR("Unknown consumer_data type");
1537 /* RCU lock for the relayd pointer */
1540 /* Flag that the current stream if set for network streaming. */
1541 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1542 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1543 if (relayd
== NULL
) {
1550 * Choose right pipe for splice. Metadata and trace data are handled by
1551 * different threads hence the use of two pipes in order not to race or
1552 * corrupt the written data.
1554 if (stream
->metadata_flag
) {
1555 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1557 splice_pipe
= ctx
->consumer_thread_pipe
;
1560 /* Write metadata stream id before payload */
1562 int total_len
= len
;
1564 if (stream
->metadata_flag
) {
1566 * Lock the control socket for the complete duration of the function
1567 * since from this point on we will use the socket.
1569 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1571 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1575 /* Socket operation failed. We consider the relayd dead */
1576 if (ret
== -EBADF
) {
1577 WARN("Remote relayd disconnected. Stopping");
1584 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1587 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1589 /* Use the returned socket. */
1592 /* Socket operation failed. We consider the relayd dead */
1593 if (ret
== -EBADF
) {
1594 WARN("Remote relayd disconnected. Stopping");
1601 /* No streaming, we have to set the len with the full padding */
1605 * Check if we need to change the tracefile before writing the packet.
1607 if (stream
->chan
->tracefile_size
> 0 &&
1608 (stream
->tracefile_size_current
+ len
) >
1609 stream
->chan
->tracefile_size
) {
1610 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1611 stream
->name
, stream
->chan
->tracefile_size
,
1612 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1613 stream
->out_fd
, &(stream
->tracefile_count_current
));
1615 ERR("Rotating output file");
1618 outfd
= stream
->out_fd
= ret
;
1619 /* Reset current size because we just perform a rotation. */
1620 stream
->tracefile_size_current
= 0;
1621 stream
->out_fd_offset
= 0;
1624 stream
->tracefile_size_current
+= len
;
1628 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1629 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1630 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1631 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1632 DBG("splice chan to pipe, ret %zd", ret_splice
);
1633 if (ret_splice
< 0) {
1634 PERROR("Error in relay splice");
1636 written
= ret_splice
;
1642 /* Handle stream on the relayd if the output is on the network */
1644 if (stream
->metadata_flag
) {
1645 size_t metadata_payload_size
=
1646 sizeof(struct lttcomm_relayd_metadata_payload
);
1648 /* Update counter to fit the spliced data */
1649 ret_splice
+= metadata_payload_size
;
1650 len
+= metadata_payload_size
;
1652 * We do this so the return value can match the len passed as
1653 * argument to this function.
1655 written
-= metadata_payload_size
;
1659 /* Splice data out */
1660 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1661 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1662 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1663 if (ret_splice
< 0) {
1664 PERROR("Error in file splice");
1666 written
= ret_splice
;
1668 /* Socket operation failed. We consider the relayd dead */
1669 if (errno
== EBADF
|| errno
== EPIPE
) {
1670 WARN("Remote relayd disconnected. Stopping");
1676 } else if (ret_splice
> len
) {
1678 PERROR("Wrote more data than requested %zd (len: %lu)",
1680 written
+= ret_splice
;
1686 /* This call is useless on a socket so better save a syscall. */
1688 /* This won't block, but will start writeout asynchronously */
1689 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1690 SYNC_FILE_RANGE_WRITE
);
1691 stream
->out_fd_offset
+= ret_splice
;
1693 stream
->output_written
+= ret_splice
;
1694 written
+= ret_splice
;
1696 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1704 * This is a special case that the relayd has closed its socket. Let's
1705 * cleanup the relayd object and all associated streams.
1707 if (relayd
&& relayd_hang_up
) {
1708 cleanup_relayd(relayd
, ctx
);
1709 /* Skip splice error so the consumer does not fail */
1714 /* send the appropriate error description to sessiond */
1717 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1720 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1723 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1728 if (relayd
&& stream
->metadata_flag
) {
1729 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1737 * Take a snapshot for a specific fd
1739 * Returns 0 on success, < 0 on error
1741 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1743 switch (consumer_data
.type
) {
1744 case LTTNG_CONSUMER_KERNEL
:
1745 return lttng_kconsumer_take_snapshot(stream
);
1746 case LTTNG_CONSUMER32_UST
:
1747 case LTTNG_CONSUMER64_UST
:
1748 return lttng_ustconsumer_take_snapshot(stream
);
1750 ERR("Unknown consumer_data type");
1757 * Get the produced position
1759 * Returns 0 on success, < 0 on error
1761 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1764 switch (consumer_data
.type
) {
1765 case LTTNG_CONSUMER_KERNEL
:
1766 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1767 case LTTNG_CONSUMER32_UST
:
1768 case LTTNG_CONSUMER64_UST
:
1769 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1771 ERR("Unknown consumer_data type");
1777 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1778 int sock
, struct pollfd
*consumer_sockpoll
)
1780 switch (consumer_data
.type
) {
1781 case LTTNG_CONSUMER_KERNEL
:
1782 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1783 case LTTNG_CONSUMER32_UST
:
1784 case LTTNG_CONSUMER64_UST
:
1785 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1787 ERR("Unknown consumer_data type");
1794 * Iterate over all streams of the hashtable and free them properly.
1796 * WARNING: *MUST* be used with data stream only.
1798 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1800 struct lttng_ht_iter iter
;
1801 struct lttng_consumer_stream
*stream
;
1808 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1810 * Ignore return value since we are currently cleaning up so any error
1813 (void) consumer_del_stream(stream
, ht
);
1817 lttng_ht_destroy(ht
);
1821 * Iterate over all streams of the hashtable and free them properly.
1823 * XXX: Should not be only for metadata stream or else use an other name.
1825 static void destroy_stream_ht(struct lttng_ht
*ht
)
1827 struct lttng_ht_iter iter
;
1828 struct lttng_consumer_stream
*stream
;
1835 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1837 * Ignore return value since we are currently cleaning up so any error
1840 (void) consumer_del_metadata_stream(stream
, ht
);
1844 lttng_ht_destroy(ht
);
1847 void lttng_consumer_close_metadata(void)
1849 switch (consumer_data
.type
) {
1850 case LTTNG_CONSUMER_KERNEL
:
1852 * The Kernel consumer has a different metadata scheme so we don't
1853 * close anything because the stream will be closed by the session
1857 case LTTNG_CONSUMER32_UST
:
1858 case LTTNG_CONSUMER64_UST
:
1860 * Close all metadata streams. The metadata hash table is passed and
1861 * this call iterates over it by closing all wakeup fd. This is safe
1862 * because at this point we are sure that the metadata producer is
1863 * either dead or blocked.
1865 lttng_ustconsumer_close_metadata(metadata_ht
);
1868 ERR("Unknown consumer_data type");
1874 * Clean up a metadata stream and free its memory.
1876 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1877 struct lttng_ht
*ht
)
1880 struct lttng_ht_iter iter
;
1881 struct lttng_consumer_channel
*free_chan
= NULL
;
1882 struct consumer_relayd_sock_pair
*relayd
;
1886 * This call should NEVER receive regular stream. It must always be
1887 * metadata stream and this is crucial for data structure synchronization.
1889 assert(stream
->metadata_flag
);
1891 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1894 /* Means the stream was allocated but not successfully added */
1895 goto free_stream_rcu
;
1898 pthread_mutex_lock(&consumer_data
.lock
);
1899 pthread_mutex_lock(&stream
->chan
->lock
);
1900 pthread_mutex_lock(&stream
->lock
);
1902 switch (consumer_data
.type
) {
1903 case LTTNG_CONSUMER_KERNEL
:
1904 if (stream
->mmap_base
!= NULL
) {
1905 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1907 PERROR("munmap metadata stream");
1910 if (stream
->wait_fd
>= 0) {
1911 ret
= close(stream
->wait_fd
);
1913 PERROR("close kernel metadata wait_fd");
1917 case LTTNG_CONSUMER32_UST
:
1918 case LTTNG_CONSUMER64_UST
:
1919 if (stream
->monitor
) {
1920 /* close the write-side in close_metadata */
1921 ret
= close(stream
->ust_metadata_poll_pipe
[0]);
1923 PERROR("Close UST metadata read-side poll pipe");
1926 lttng_ustconsumer_del_stream(stream
);
1929 ERR("Unknown consumer_data type");
1935 iter
.iter
.node
= &stream
->node
.node
;
1936 ret
= lttng_ht_del(ht
, &iter
);
1939 iter
.iter
.node
= &stream
->node_channel_id
.node
;
1940 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
1943 iter
.iter
.node
= &stream
->node_session_id
.node
;
1944 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1948 if (stream
->out_fd
>= 0) {
1949 ret
= close(stream
->out_fd
);
1955 /* Check and cleanup relayd */
1957 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1958 if (relayd
!= NULL
) {
1959 uatomic_dec(&relayd
->refcount
);
1960 assert(uatomic_read(&relayd
->refcount
) >= 0);
1962 /* Closing streams requires to lock the control socket. */
1963 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1964 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1965 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1966 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1968 DBG("Unable to close stream on the relayd. Continuing");
1970 * Continue here. There is nothing we can do for the relayd.
1971 * Chances are that the relayd has closed the socket so we just
1972 * continue cleaning up.
1976 /* Both conditions are met, we destroy the relayd. */
1977 if (uatomic_read(&relayd
->refcount
) == 0 &&
1978 uatomic_read(&relayd
->destroy_flag
)) {
1979 consumer_destroy_relayd(relayd
);
1984 /* Atomically decrement channel refcount since other threads can use it. */
1985 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
1986 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
1987 /* Go for channel deletion! */
1988 free_chan
= stream
->chan
;
1993 * Nullify the stream reference so it is not used after deletion. The
1994 * channel lock MUST be acquired before being able to check for
1995 * a NULL pointer value.
1997 stream
->chan
->metadata_stream
= NULL
;
1999 pthread_mutex_unlock(&stream
->lock
);
2000 pthread_mutex_unlock(&stream
->chan
->lock
);
2001 pthread_mutex_unlock(&consumer_data
.lock
);
2004 consumer_del_channel(free_chan
);
2008 call_rcu(&stream
->node
.head
, free_stream_rcu
);
2012 * Action done with the metadata stream when adding it to the consumer internal
2013 * data structures to handle it.
2015 int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2017 struct lttng_ht
*ht
= metadata_ht
;
2019 struct lttng_ht_iter iter
;
2020 struct lttng_ht_node_u64
*node
;
2025 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2027 pthread_mutex_lock(&consumer_data
.lock
);
2028 pthread_mutex_lock(&stream
->chan
->lock
);
2029 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2030 pthread_mutex_lock(&stream
->lock
);
2033 * From here, refcounts are updated so be _careful_ when returning an error
2040 * Lookup the stream just to make sure it does not exist in our internal
2041 * state. This should NEVER happen.
2043 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2044 node
= lttng_ht_iter_get_node_u64(&iter
);
2048 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2049 * in terms of destroying the associated channel, because the action that
2050 * causes the count to become 0 also causes a stream to be added. The
2051 * channel deletion will thus be triggered by the following removal of this
2054 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2055 /* Increment refcount before decrementing nb_init_stream_left */
2057 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2060 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2062 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2063 &stream
->node_channel_id
);
2066 * Add stream to the stream_list_ht of the consumer data. No need to steal
2067 * the key since the HT does not use it and we allow to add redundant keys
2070 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2074 pthread_mutex_unlock(&stream
->lock
);
2075 pthread_mutex_unlock(&stream
->chan
->lock
);
2076 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2077 pthread_mutex_unlock(&consumer_data
.lock
);
2082 * Delete data stream that are flagged for deletion (endpoint_status).
2084 static void validate_endpoint_status_data_stream(void)
2086 struct lttng_ht_iter iter
;
2087 struct lttng_consumer_stream
*stream
;
2089 DBG("Consumer delete flagged data stream");
2092 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2093 /* Validate delete flag of the stream */
2094 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2097 /* Delete it right now */
2098 consumer_del_stream(stream
, data_ht
);
2104 * Delete metadata stream that are flagged for deletion (endpoint_status).
2106 static void validate_endpoint_status_metadata_stream(
2107 struct lttng_poll_event
*pollset
)
2109 struct lttng_ht_iter iter
;
2110 struct lttng_consumer_stream
*stream
;
2112 DBG("Consumer delete flagged metadata stream");
2117 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2118 /* Validate delete flag of the stream */
2119 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2123 * Remove from pollset so the metadata thread can continue without
2124 * blocking on a deleted stream.
2126 lttng_poll_del(pollset
, stream
->wait_fd
);
2128 /* Delete it right now */
2129 consumer_del_metadata_stream(stream
, metadata_ht
);
2135 * Thread polls on metadata file descriptor and write them on disk or on the
2138 void *consumer_thread_metadata_poll(void *data
)
2141 uint32_t revents
, nb_fd
;
2142 struct lttng_consumer_stream
*stream
= NULL
;
2143 struct lttng_ht_iter iter
;
2144 struct lttng_ht_node_u64
*node
;
2145 struct lttng_poll_event events
;
2146 struct lttng_consumer_local_data
*ctx
= data
;
2149 rcu_register_thread();
2151 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2153 /* ENOMEM at this point. Better to bail out. */
2157 DBG("Thread metadata poll started");
2159 /* Size is set to 1 for the consumer_metadata pipe */
2160 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2162 ERR("Poll set creation failed");
2166 ret
= lttng_poll_add(&events
,
2167 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2173 DBG("Metadata main loop started");
2176 /* Only the metadata pipe is set */
2177 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2182 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2183 ret
= lttng_poll_wait(&events
, -1);
2184 DBG("Metadata event catched in thread");
2186 if (errno
== EINTR
) {
2187 ERR("Poll EINTR catched");
2195 /* From here, the event is a metadata wait fd */
2196 for (i
= 0; i
< nb_fd
; i
++) {
2197 revents
= LTTNG_POLL_GETEV(&events
, i
);
2198 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2200 /* Just don't waste time if no returned events for the fd */
2205 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2206 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2207 DBG("Metadata thread pipe hung up");
2209 * Remove the pipe from the poll set and continue the loop
2210 * since their might be data to consume.
2212 lttng_poll_del(&events
,
2213 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2214 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2216 } else if (revents
& LPOLLIN
) {
2219 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2220 &stream
, sizeof(stream
));
2222 ERR("read metadata stream, ret: %zd", pipe_len
);
2224 * Continue here to handle the rest of the streams.
2229 /* A NULL stream means that the state has changed. */
2230 if (stream
== NULL
) {
2231 /* Check for deleted streams. */
2232 validate_endpoint_status_metadata_stream(&events
);
2236 DBG("Adding metadata stream %d to poll set",
2239 /* Add metadata stream to the global poll events list */
2240 lttng_poll_add(&events
, stream
->wait_fd
,
2241 LPOLLIN
| LPOLLPRI
);
2244 /* Handle other stream */
2250 uint64_t tmp_id
= (uint64_t) pollfd
;
2252 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2254 node
= lttng_ht_iter_get_node_u64(&iter
);
2257 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2260 /* Check for error event */
2261 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2262 DBG("Metadata fd %d is hup|err.", pollfd
);
2263 if (!stream
->hangup_flush_done
2264 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2265 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2266 DBG("Attempting to flush and consume the UST buffers");
2267 lttng_ustconsumer_on_stream_hangup(stream
);
2269 /* We just flushed the stream now read it. */
2271 len
= ctx
->on_buffer_ready(stream
, ctx
);
2273 * We don't check the return value here since if we get
2274 * a negative len, it means an error occured thus we
2275 * simply remove it from the poll set and free the
2281 lttng_poll_del(&events
, stream
->wait_fd
);
2283 * This call update the channel states, closes file descriptors
2284 * and securely free the stream.
2286 consumer_del_metadata_stream(stream
, metadata_ht
);
2287 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2288 /* Get the data out of the metadata file descriptor */
2289 DBG("Metadata available on fd %d", pollfd
);
2290 assert(stream
->wait_fd
== pollfd
);
2293 len
= ctx
->on_buffer_ready(stream
, ctx
);
2295 * We don't check the return value here since if we get
2296 * a negative len, it means an error occured thus we
2297 * simply remove it from the poll set and free the
2302 /* It's ok to have an unavailable sub-buffer */
2303 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2304 /* Clean up stream from consumer and free it. */
2305 lttng_poll_del(&events
, stream
->wait_fd
);
2306 consumer_del_metadata_stream(stream
, metadata_ht
);
2310 /* Release RCU lock for the stream looked up */
2317 DBG("Metadata poll thread exiting");
2319 lttng_poll_clean(&events
);
2321 destroy_stream_ht(metadata_ht
);
2323 rcu_unregister_thread();
2328 * This thread polls the fds in the set to consume the data and write
2329 * it to tracefile if necessary.
2331 void *consumer_thread_data_poll(void *data
)
2333 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2334 struct pollfd
*pollfd
= NULL
;
2335 /* local view of the streams */
2336 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2337 /* local view of consumer_data.fds_count */
2339 struct lttng_consumer_local_data
*ctx
= data
;
2342 rcu_register_thread();
2344 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2345 if (data_ht
== NULL
) {
2346 /* ENOMEM at this point. Better to bail out. */
2350 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2351 if (local_stream
== NULL
) {
2352 PERROR("local_stream malloc");
2361 * the fds set has been updated, we need to update our
2362 * local array as well
2364 pthread_mutex_lock(&consumer_data
.lock
);
2365 if (consumer_data
.need_update
) {
2370 local_stream
= NULL
;
2372 /* allocate for all fds + 1 for the consumer_data_pipe */
2373 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2374 if (pollfd
== NULL
) {
2375 PERROR("pollfd malloc");
2376 pthread_mutex_unlock(&consumer_data
.lock
);
2380 /* allocate for all fds + 1 for the consumer_data_pipe */
2381 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2382 sizeof(struct lttng_consumer_stream
*));
2383 if (local_stream
== NULL
) {
2384 PERROR("local_stream malloc");
2385 pthread_mutex_unlock(&consumer_data
.lock
);
2388 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2391 ERR("Error in allocating pollfd or local_outfds");
2392 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2393 pthread_mutex_unlock(&consumer_data
.lock
);
2397 consumer_data
.need_update
= 0;
2399 pthread_mutex_unlock(&consumer_data
.lock
);
2401 /* No FDs and consumer_quit, consumer_cleanup the thread */
2402 if (nb_fd
== 0 && consumer_quit
== 1) {
2405 /* poll on the array of fds */
2407 DBG("polling on %d fd", nb_fd
+ 1);
2408 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2409 DBG("poll num_rdy : %d", num_rdy
);
2410 if (num_rdy
== -1) {
2412 * Restart interrupted system call.
2414 if (errno
== EINTR
) {
2417 PERROR("Poll error");
2418 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2420 } else if (num_rdy
== 0) {
2421 DBG("Polling thread timed out");
2426 * If the consumer_data_pipe triggered poll go directly to the
2427 * beginning of the loop to update the array. We want to prioritize
2428 * array update over low-priority reads.
2430 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2431 ssize_t pipe_readlen
;
2433 DBG("consumer_data_pipe wake up");
2434 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2435 &new_stream
, sizeof(new_stream
));
2436 if (pipe_readlen
< 0) {
2437 ERR("Consumer data pipe ret %zd", pipe_readlen
);
2438 /* Continue so we can at least handle the current stream(s). */
2443 * If the stream is NULL, just ignore it. It's also possible that
2444 * the sessiond poll thread changed the consumer_quit state and is
2445 * waking us up to test it.
2447 if (new_stream
== NULL
) {
2448 validate_endpoint_status_data_stream();
2452 /* Continue to update the local streams and handle prio ones */
2456 /* Take care of high priority channels first. */
2457 for (i
= 0; i
< nb_fd
; i
++) {
2458 if (local_stream
[i
] == NULL
) {
2461 if (pollfd
[i
].revents
& POLLPRI
) {
2462 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2464 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2465 /* it's ok to have an unavailable sub-buffer */
2466 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2467 /* Clean the stream and free it. */
2468 consumer_del_stream(local_stream
[i
], data_ht
);
2469 local_stream
[i
] = NULL
;
2470 } else if (len
> 0) {
2471 local_stream
[i
]->data_read
= 1;
2477 * If we read high prio channel in this loop, try again
2478 * for more high prio data.
2484 /* Take care of low priority channels. */
2485 for (i
= 0; i
< nb_fd
; i
++) {
2486 if (local_stream
[i
] == NULL
) {
2489 if ((pollfd
[i
].revents
& POLLIN
) ||
2490 local_stream
[i
]->hangup_flush_done
) {
2491 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2492 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2493 /* it's ok to have an unavailable sub-buffer */
2494 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2495 /* Clean the stream and free it. */
2496 consumer_del_stream(local_stream
[i
], data_ht
);
2497 local_stream
[i
] = NULL
;
2498 } else if (len
> 0) {
2499 local_stream
[i
]->data_read
= 1;
2504 /* Handle hangup and errors */
2505 for (i
= 0; i
< nb_fd
; i
++) {
2506 if (local_stream
[i
] == NULL
) {
2509 if (!local_stream
[i
]->hangup_flush_done
2510 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2511 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2512 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2513 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2515 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2516 /* Attempt read again, for the data we just flushed. */
2517 local_stream
[i
]->data_read
= 1;
2520 * If the poll flag is HUP/ERR/NVAL and we have
2521 * read no data in this pass, we can remove the
2522 * stream from its hash table.
2524 if ((pollfd
[i
].revents
& POLLHUP
)) {
2525 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2526 if (!local_stream
[i
]->data_read
) {
2527 consumer_del_stream(local_stream
[i
], data_ht
);
2528 local_stream
[i
] = NULL
;
2531 } else if (pollfd
[i
].revents
& POLLERR
) {
2532 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2533 if (!local_stream
[i
]->data_read
) {
2534 consumer_del_stream(local_stream
[i
], data_ht
);
2535 local_stream
[i
] = NULL
;
2538 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2539 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2540 if (!local_stream
[i
]->data_read
) {
2541 consumer_del_stream(local_stream
[i
], data_ht
);
2542 local_stream
[i
] = NULL
;
2546 if (local_stream
[i
] != NULL
) {
2547 local_stream
[i
]->data_read
= 0;
2552 DBG("polling thread exiting");
2557 * Close the write side of the pipe so epoll_wait() in
2558 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2559 * read side of the pipe. If we close them both, epoll_wait strangely does
2560 * not return and could create a endless wait period if the pipe is the
2561 * only tracked fd in the poll set. The thread will take care of closing
2564 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2566 destroy_data_stream_ht(data_ht
);
2568 rcu_unregister_thread();
2573 * Close wake-up end of each stream belonging to the channel. This will
2574 * allow the poll() on the stream read-side to detect when the
2575 * write-side (application) finally closes them.
2578 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2580 struct lttng_ht
*ht
;
2581 struct lttng_consumer_stream
*stream
;
2582 struct lttng_ht_iter iter
;
2584 ht
= consumer_data
.stream_per_chan_id_ht
;
2587 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2588 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2589 ht
->match_fct
, &channel
->key
,
2590 &iter
.iter
, stream
, node_channel_id
.node
) {
2592 * Protect against teardown with mutex.
2594 pthread_mutex_lock(&stream
->lock
);
2595 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2598 switch (consumer_data
.type
) {
2599 case LTTNG_CONSUMER_KERNEL
:
2601 case LTTNG_CONSUMER32_UST
:
2602 case LTTNG_CONSUMER64_UST
:
2604 * Note: a mutex is taken internally within
2605 * liblttng-ust-ctl to protect timer wakeup_fd
2606 * use from concurrent close.
2608 lttng_ustconsumer_close_stream_wakeup(stream
);
2611 ERR("Unknown consumer_data type");
2615 pthread_mutex_unlock(&stream
->lock
);
2620 static void destroy_channel_ht(struct lttng_ht
*ht
)
2622 struct lttng_ht_iter iter
;
2623 struct lttng_consumer_channel
*channel
;
2631 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2632 ret
= lttng_ht_del(ht
, &iter
);
2637 lttng_ht_destroy(ht
);
2641 * This thread polls the channel fds to detect when they are being
2642 * closed. It closes all related streams if the channel is detected as
2643 * closed. It is currently only used as a shim layer for UST because the
2644 * consumerd needs to keep the per-stream wakeup end of pipes open for
2647 void *consumer_thread_channel_poll(void *data
)
2650 uint32_t revents
, nb_fd
;
2651 struct lttng_consumer_channel
*chan
= NULL
;
2652 struct lttng_ht_iter iter
;
2653 struct lttng_ht_node_u64
*node
;
2654 struct lttng_poll_event events
;
2655 struct lttng_consumer_local_data
*ctx
= data
;
2656 struct lttng_ht
*channel_ht
;
2658 rcu_register_thread();
2660 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2662 /* ENOMEM at this point. Better to bail out. */
2666 DBG("Thread channel poll started");
2668 /* Size is set to 1 for the consumer_channel pipe */
2669 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2671 ERR("Poll set creation failed");
2675 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2681 DBG("Channel main loop started");
2684 /* Only the channel pipe is set */
2685 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2690 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2691 ret
= lttng_poll_wait(&events
, -1);
2692 DBG("Channel event catched in thread");
2694 if (errno
== EINTR
) {
2695 ERR("Poll EINTR catched");
2703 /* From here, the event is a channel wait fd */
2704 for (i
= 0; i
< nb_fd
; i
++) {
2705 revents
= LTTNG_POLL_GETEV(&events
, i
);
2706 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2708 /* Just don't waste time if no returned events for the fd */
2712 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2713 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2714 DBG("Channel thread pipe hung up");
2716 * Remove the pipe from the poll set and continue the loop
2717 * since their might be data to consume.
2719 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2721 } else if (revents
& LPOLLIN
) {
2722 enum consumer_channel_action action
;
2725 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2727 ERR("Error reading channel pipe");
2732 case CONSUMER_CHANNEL_ADD
:
2733 DBG("Adding channel %d to poll set",
2736 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2739 lttng_ht_add_unique_u64(channel_ht
,
2740 &chan
->wait_fd_node
);
2742 /* Add channel to the global poll events list */
2743 lttng_poll_add(&events
, chan
->wait_fd
,
2744 LPOLLIN
| LPOLLPRI
);
2746 case CONSUMER_CHANNEL_DEL
:
2748 struct lttng_consumer_stream
*stream
, *stmp
;
2751 chan
= consumer_find_channel(key
);
2754 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2757 lttng_poll_del(&events
, chan
->wait_fd
);
2758 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2759 ret
= lttng_ht_del(channel_ht
, &iter
);
2761 consumer_close_channel_streams(chan
);
2763 switch (consumer_data
.type
) {
2764 case LTTNG_CONSUMER_KERNEL
:
2766 case LTTNG_CONSUMER32_UST
:
2767 case LTTNG_CONSUMER64_UST
:
2768 /* Delete streams that might have been left in the stream list. */
2769 cds_list_for_each_entry_safe(stream
, stmp
, &chan
->streams
.head
,
2771 cds_list_del(&stream
->send_node
);
2772 lttng_ustconsumer_del_stream(stream
);
2773 uatomic_sub(&stream
->chan
->refcount
, 1);
2774 assert(&chan
->refcount
);
2779 ERR("Unknown consumer_data type");
2784 * Release our own refcount. Force channel deletion even if
2785 * streams were not initialized.
2787 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2788 consumer_del_channel(chan
);
2793 case CONSUMER_CHANNEL_QUIT
:
2795 * Remove the pipe from the poll set and continue the loop
2796 * since their might be data to consume.
2798 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2801 ERR("Unknown action");
2806 /* Handle other stream */
2812 uint64_t tmp_id
= (uint64_t) pollfd
;
2814 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2816 node
= lttng_ht_iter_get_node_u64(&iter
);
2819 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2822 /* Check for error event */
2823 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2824 DBG("Channel fd %d is hup|err.", pollfd
);
2826 lttng_poll_del(&events
, chan
->wait_fd
);
2827 ret
= lttng_ht_del(channel_ht
, &iter
);
2829 consumer_close_channel_streams(chan
);
2831 /* Release our own refcount */
2832 if (!uatomic_sub_return(&chan
->refcount
, 1)
2833 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2834 consumer_del_channel(chan
);
2838 /* Release RCU lock for the channel looked up */
2844 lttng_poll_clean(&events
);
2846 destroy_channel_ht(channel_ht
);
2848 DBG("Channel poll thread exiting");
2849 rcu_unregister_thread();
2853 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2854 struct pollfd
*sockpoll
, int client_socket
)
2861 if (lttng_consumer_poll_socket(sockpoll
) < 0) {
2865 DBG("Metadata connection on client_socket");
2867 /* Blocking call, waiting for transmission */
2868 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2869 if (ctx
->consumer_metadata_socket
< 0) {
2870 WARN("On accept metadata");
2881 * This thread listens on the consumerd socket and receives the file
2882 * descriptors from the session daemon.
2884 void *consumer_thread_sessiond_poll(void *data
)
2886 int sock
= -1, client_socket
, ret
;
2888 * structure to poll for incoming data on communication socket avoids
2889 * making blocking sockets.
2891 struct pollfd consumer_sockpoll
[2];
2892 struct lttng_consumer_local_data
*ctx
= data
;
2894 rcu_register_thread();
2896 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2897 unlink(ctx
->consumer_command_sock_path
);
2898 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2899 if (client_socket
< 0) {
2900 ERR("Cannot create command socket");
2904 ret
= lttcomm_listen_unix_sock(client_socket
);
2909 DBG("Sending ready command to lttng-sessiond");
2910 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2911 /* return < 0 on error, but == 0 is not fatal */
2913 ERR("Error sending ready command to lttng-sessiond");
2917 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2918 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2919 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2920 consumer_sockpoll
[1].fd
= client_socket
;
2921 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2923 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2926 DBG("Connection on client_socket");
2928 /* Blocking call, waiting for transmission */
2929 sock
= lttcomm_accept_unix_sock(client_socket
);
2936 * Setup metadata socket which is the second socket connection on the
2937 * command unix socket.
2939 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
2944 /* This socket is not useful anymore. */
2945 ret
= close(client_socket
);
2947 PERROR("close client_socket");
2951 /* update the polling structure to poll on the established socket */
2952 consumer_sockpoll
[1].fd
= sock
;
2953 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2956 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2959 DBG("Incoming command on sock");
2960 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2961 if (ret
== -ENOENT
) {
2962 DBG("Received STOP command");
2967 * This could simply be a session daemon quitting. Don't output
2970 DBG("Communication interrupted on command socket");
2973 if (consumer_quit
) {
2974 DBG("consumer_thread_receive_fds received quit from signal");
2977 DBG("received command on sock");
2980 DBG("Consumer thread sessiond poll exiting");
2983 * Close metadata streams since the producer is the session daemon which
2986 * NOTE: for now, this only applies to the UST tracer.
2988 lttng_consumer_close_metadata();
2991 * when all fds have hung up, the polling thread
2997 * Notify the data poll thread to poll back again and test the
2998 * consumer_quit state that we just set so to quit gracefully.
3000 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3002 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3004 /* Cleaning up possibly open sockets. */
3008 PERROR("close sock sessiond poll");
3011 if (client_socket
>= 0) {
3012 ret
= close(client_socket
);
3014 PERROR("close client_socket sessiond poll");
3018 rcu_unregister_thread();
3022 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3023 struct lttng_consumer_local_data
*ctx
)
3027 pthread_mutex_lock(&stream
->lock
);
3029 switch (consumer_data
.type
) {
3030 case LTTNG_CONSUMER_KERNEL
:
3031 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3033 case LTTNG_CONSUMER32_UST
:
3034 case LTTNG_CONSUMER64_UST
:
3035 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3038 ERR("Unknown consumer_data type");
3044 pthread_mutex_unlock(&stream
->lock
);
3048 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3050 switch (consumer_data
.type
) {
3051 case LTTNG_CONSUMER_KERNEL
:
3052 return lttng_kconsumer_on_recv_stream(stream
);
3053 case LTTNG_CONSUMER32_UST
:
3054 case LTTNG_CONSUMER64_UST
:
3055 return lttng_ustconsumer_on_recv_stream(stream
);
3057 ERR("Unknown consumer_data type");
3064 * Allocate and set consumer data hash tables.
3066 void lttng_consumer_init(void)
3068 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3069 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3070 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3071 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3075 * Process the ADD_RELAYD command receive by a consumer.
3077 * This will create a relayd socket pair and add it to the relayd hash table.
3078 * The caller MUST acquire a RCU read side lock before calling it.
3080 int consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3081 struct lttng_consumer_local_data
*ctx
, int sock
,
3082 struct pollfd
*consumer_sockpoll
,
3083 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
)
3085 int fd
= -1, ret
= -1, relayd_created
= 0;
3086 enum lttng_error_code ret_code
= LTTNG_OK
;
3087 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3090 assert(relayd_sock
);
3092 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3094 /* Get relayd reference if exists. */
3095 relayd
= consumer_find_relayd(net_seq_idx
);
3096 if (relayd
== NULL
) {
3097 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3098 /* Not found. Allocate one. */
3099 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3100 if (relayd
== NULL
) {
3102 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3105 relayd
->sessiond_session_id
= sessiond_id
;
3110 * This code path MUST continue to the consumer send status message to
3111 * we can notify the session daemon and continue our work without
3112 * killing everything.
3116 * relayd key should never be found for control socket.
3118 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3121 /* First send a status message before receiving the fds. */
3122 ret
= consumer_send_status_msg(sock
, LTTNG_OK
);
3124 /* Somehow, the session daemon is not responding anymore. */
3125 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3126 goto error_nosignal
;
3129 /* Poll on consumer socket. */
3130 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
3131 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3133 goto error_nosignal
;
3136 /* Get relayd socket from session daemon */
3137 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3138 if (ret
!= sizeof(fd
)) {
3140 fd
= -1; /* Just in case it gets set with an invalid value. */
3143 * Failing to receive FDs might indicate a major problem such as
3144 * reaching a fd limit during the receive where the kernel returns a
3145 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3146 * don't take any chances and stop everything.
3148 * XXX: Feature request #558 will fix that and avoid this possible
3149 * issue when reaching the fd limit.
3151 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3152 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3156 /* Copy socket information and received FD */
3157 switch (sock_type
) {
3158 case LTTNG_STREAM_CONTROL
:
3159 /* Copy received lttcomm socket */
3160 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3161 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3162 /* Handle create_sock error. */
3164 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3168 * Close the socket created internally by
3169 * lttcomm_create_sock, so we can replace it by the one
3170 * received from sessiond.
3172 if (close(relayd
->control_sock
.sock
.fd
)) {
3176 /* Assign new file descriptor */
3177 relayd
->control_sock
.sock
.fd
= fd
;
3178 fd
= -1; /* For error path */
3179 /* Assign version values. */
3180 relayd
->control_sock
.major
= relayd_sock
->major
;
3181 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3184 * Create a session on the relayd and store the returned id. Lock the
3185 * control socket mutex if the relayd was NOT created before.
3187 if (!relayd_created
) {
3188 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3190 ret
= relayd_create_session(&relayd
->control_sock
,
3191 &relayd
->relayd_session_id
);
3192 if (!relayd_created
) {
3193 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3197 * Close all sockets of a relayd object. It will be freed if it was
3198 * created at the error code path or else it will be garbage
3201 (void) relayd_close(&relayd
->control_sock
);
3202 (void) relayd_close(&relayd
->data_sock
);
3203 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
3208 case LTTNG_STREAM_DATA
:
3209 /* Copy received lttcomm socket */
3210 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3211 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3212 /* Handle create_sock error. */
3214 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3218 * Close the socket created internally by
3219 * lttcomm_create_sock, so we can replace it by the one
3220 * received from sessiond.
3222 if (close(relayd
->data_sock
.sock
.fd
)) {
3226 /* Assign new file descriptor */
3227 relayd
->data_sock
.sock
.fd
= fd
;
3228 fd
= -1; /* for eventual error paths */
3229 /* Assign version values. */
3230 relayd
->data_sock
.major
= relayd_sock
->major
;
3231 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3234 ERR("Unknown relayd socket type (%d)", sock_type
);
3236 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3240 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3241 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3242 relayd
->net_seq_idx
, fd
);
3244 /* We successfully added the socket. Send status back. */
3245 ret
= consumer_send_status_msg(sock
, ret_code
);
3247 /* Somehow, the session daemon is not responding anymore. */
3248 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3249 goto error_nosignal
;
3253 * Add relayd socket pair to consumer data hashtable. If object already
3254 * exists or on error, the function gracefully returns.
3262 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3263 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3267 /* Close received socket if valid. */
3270 PERROR("close received socket");
3274 if (relayd_created
) {
3282 * Try to lock the stream mutex.
3284 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3286 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3293 * Try to lock the stream mutex. On failure, we know that the stream is
3294 * being used else where hence there is data still being extracted.
3296 ret
= pthread_mutex_trylock(&stream
->lock
);
3298 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3310 * Search for a relayd associated to the session id and return the reference.
3312 * A rcu read side lock MUST be acquire before calling this function and locked
3313 * until the relayd object is no longer necessary.
3315 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3317 struct lttng_ht_iter iter
;
3318 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3320 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3321 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3324 * Check by sessiond id which is unique here where the relayd session
3325 * id might not be when having multiple relayd.
3327 if (relayd
->sessiond_session_id
== id
) {
3328 /* Found the relayd. There can be only one per id. */
3340 * Check if for a given session id there is still data needed to be extract
3343 * Return 1 if data is pending or else 0 meaning ready to be read.
3345 int consumer_data_pending(uint64_t id
)
3348 struct lttng_ht_iter iter
;
3349 struct lttng_ht
*ht
;
3350 struct lttng_consumer_stream
*stream
;
3351 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3352 int (*data_pending
)(struct lttng_consumer_stream
*);
3354 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3357 pthread_mutex_lock(&consumer_data
.lock
);
3359 switch (consumer_data
.type
) {
3360 case LTTNG_CONSUMER_KERNEL
:
3361 data_pending
= lttng_kconsumer_data_pending
;
3363 case LTTNG_CONSUMER32_UST
:
3364 case LTTNG_CONSUMER64_UST
:
3365 data_pending
= lttng_ustconsumer_data_pending
;
3368 ERR("Unknown consumer data type");
3372 /* Ease our life a bit */
3373 ht
= consumer_data
.stream_list_ht
;
3375 relayd
= find_relayd_by_session_id(id
);
3377 /* Send init command for data pending. */
3378 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3379 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3380 relayd
->relayd_session_id
);
3381 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3383 /* Communication error thus the relayd so no data pending. */
3384 goto data_not_pending
;
3388 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3389 ht
->hash_fct(&id
, lttng_ht_seed
),
3391 &iter
.iter
, stream
, node_session_id
.node
) {
3392 /* If this call fails, the stream is being used hence data pending. */
3393 ret
= stream_try_lock(stream
);
3399 * A removed node from the hash table indicates that the stream has
3400 * been deleted thus having a guarantee that the buffers are closed
3401 * on the consumer side. However, data can still be transmitted
3402 * over the network so don't skip the relayd check.
3404 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3407 * An empty output file is not valid. We need at least one packet
3408 * generated per stream, even if it contains no event, so it
3409 * contains at least one packet header.
3411 if (stream
->output_written
== 0) {
3412 pthread_mutex_unlock(&stream
->lock
);
3415 /* Check the stream if there is data in the buffers. */
3416 ret
= data_pending(stream
);
3418 pthread_mutex_unlock(&stream
->lock
);
3425 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3426 if (stream
->metadata_flag
) {
3427 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3428 stream
->relayd_stream_id
);
3430 ret
= relayd_data_pending(&relayd
->control_sock
,
3431 stream
->relayd_stream_id
,
3432 stream
->next_net_seq_num
- 1);
3434 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3436 pthread_mutex_unlock(&stream
->lock
);
3440 pthread_mutex_unlock(&stream
->lock
);
3444 unsigned int is_data_inflight
= 0;
3446 /* Send init command for data pending. */
3447 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3448 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3449 relayd
->relayd_session_id
, &is_data_inflight
);
3450 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3452 goto data_not_pending
;
3454 if (is_data_inflight
) {
3460 * Finding _no_ node in the hash table and no inflight data means that the
3461 * stream(s) have been removed thus data is guaranteed to be available for
3462 * analysis from the trace files.
3466 /* Data is available to be read by a viewer. */
3467 pthread_mutex_unlock(&consumer_data
.lock
);
3472 /* Data is still being extracted from buffers. */
3473 pthread_mutex_unlock(&consumer_data
.lock
);
3479 * Send a ret code status message to the sessiond daemon.
3481 * Return the sendmsg() return value.
3483 int consumer_send_status_msg(int sock
, int ret_code
)
3485 struct lttcomm_consumer_status_msg msg
;
3487 msg
.ret_code
= ret_code
;
3489 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3493 * Send a channel status message to the sessiond daemon.
3495 * Return the sendmsg() return value.
3497 int consumer_send_status_channel(int sock
,
3498 struct lttng_consumer_channel
*channel
)
3500 struct lttcomm_consumer_status_channel msg
;
3505 msg
.ret_code
= -LTTNG_ERR_UST_CHAN_FAIL
;
3507 msg
.ret_code
= LTTNG_OK
;
3508 msg
.key
= channel
->key
;
3509 msg
.stream_count
= channel
->streams
.count
;
3512 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3516 * Using a maximum stream size with the produced and consumed position of a
3517 * stream, computes the new consumed position to be as close as possible to the
3518 * maximum possible stream size.
3520 * If maximum stream size is lower than the possible buffer size (produced -
3521 * consumed), the consumed_pos given is returned untouched else the new value
3524 unsigned long consumer_get_consumed_maxsize(unsigned long consumed_pos
,
3525 unsigned long produced_pos
, uint64_t max_stream_size
)
3527 if (max_stream_size
&& max_stream_size
< (produced_pos
- consumed_pos
)) {
3528 /* Offset from the produced position to get the latest buffers. */
3529 return produced_pos
- max_stream_size
;
3532 return consumed_pos
;