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
);
460 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
462 enum lttng_consumer_stream_state state
,
463 const char *channel_name
,
470 enum consumer_channel_type type
,
471 unsigned int monitor
)
474 struct lttng_consumer_stream
*stream
;
476 stream
= zmalloc(sizeof(*stream
));
477 if (stream
== NULL
) {
478 PERROR("malloc struct lttng_consumer_stream");
485 stream
->key
= stream_key
;
487 stream
->out_fd_offset
= 0;
488 stream
->state
= state
;
491 stream
->net_seq_idx
= relayd_id
;
492 stream
->session_id
= session_id
;
493 stream
->monitor
= monitor
;
494 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
495 pthread_mutex_init(&stream
->lock
, NULL
);
497 /* If channel is the metadata, flag this stream as metadata. */
498 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
499 stream
->metadata_flag
= 1;
500 /* Metadata is flat out. */
501 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
503 /* Format stream name to <channel_name>_<cpu_number> */
504 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
507 PERROR("snprintf stream name");
512 /* Key is always the wait_fd for streams. */
513 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
515 /* Init node per channel id key */
516 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
518 /* Init session id node with the stream session id */
519 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
521 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
522 " relayd_id %" PRIu64
", session_id %" PRIu64
,
523 stream
->name
, stream
->key
, channel_key
,
524 stream
->net_seq_idx
, stream
->session_id
);
540 * Add a stream to the global list protected by a mutex.
542 static int add_stream(struct lttng_consumer_stream
*stream
,
550 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
552 pthread_mutex_lock(&consumer_data
.lock
);
553 pthread_mutex_lock(&stream
->chan
->lock
);
554 pthread_mutex_lock(&stream
->lock
);
557 /* Steal stream identifier to avoid having streams with the same key */
558 steal_stream_key(stream
->key
, ht
);
560 lttng_ht_add_unique_u64(ht
, &stream
->node
);
562 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
563 &stream
->node_channel_id
);
566 * Add stream to the stream_list_ht of the consumer data. No need to steal
567 * the key since the HT does not use it and we allow to add redundant keys
570 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
573 * When nb_init_stream_left reaches 0, we don't need to trigger any action
574 * in terms of destroying the associated channel, because the action that
575 * causes the count to become 0 also causes a stream to be added. The
576 * channel deletion will thus be triggered by the following removal of this
579 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
580 /* Increment refcount before decrementing nb_init_stream_left */
582 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
585 /* Update consumer data once the node is inserted. */
586 consumer_data
.stream_count
++;
587 consumer_data
.need_update
= 1;
590 pthread_mutex_unlock(&stream
->lock
);
591 pthread_mutex_unlock(&stream
->chan
->lock
);
592 pthread_mutex_unlock(&consumer_data
.lock
);
598 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
599 * be acquired before calling this.
601 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
604 struct lttng_ht_node_u64
*node
;
605 struct lttng_ht_iter iter
;
609 lttng_ht_lookup(consumer_data
.relayd_ht
,
610 &relayd
->net_seq_idx
, &iter
);
611 node
= lttng_ht_iter_get_node_u64(&iter
);
615 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
622 * Allocate and return a consumer relayd socket.
624 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
625 uint64_t net_seq_idx
)
627 struct consumer_relayd_sock_pair
*obj
= NULL
;
629 /* net sequence index of -1 is a failure */
630 if (net_seq_idx
== (uint64_t) -1ULL) {
634 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
636 PERROR("zmalloc relayd sock");
640 obj
->net_seq_idx
= net_seq_idx
;
642 obj
->destroy_flag
= 0;
643 obj
->control_sock
.sock
.fd
= -1;
644 obj
->data_sock
.sock
.fd
= -1;
645 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
646 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
653 * Find a relayd socket pair in the global consumer data.
655 * Return the object if found else NULL.
656 * RCU read-side lock must be held across this call and while using the
659 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
661 struct lttng_ht_iter iter
;
662 struct lttng_ht_node_u64
*node
;
663 struct consumer_relayd_sock_pair
*relayd
= NULL
;
665 /* Negative keys are lookup failures */
666 if (key
== (uint64_t) -1ULL) {
670 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
672 node
= lttng_ht_iter_get_node_u64(&iter
);
674 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
682 * Find a relayd and send the stream
684 * Returns 0 on success, < 0 on error
686 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
690 struct consumer_relayd_sock_pair
*relayd
;
693 assert(stream
->net_seq_idx
!= -1ULL);
696 /* The stream is not metadata. Get relayd reference if exists. */
698 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
699 if (relayd
!= NULL
) {
700 /* Add stream on the relayd */
701 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
702 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
703 path
, &stream
->relayd_stream_id
,
704 stream
->chan
->tracefile_size
, stream
->chan
->tracefile_count
);
705 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
709 uatomic_inc(&relayd
->refcount
);
710 stream
->sent_to_relayd
= 1;
712 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
713 stream
->key
, stream
->net_seq_idx
);
718 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
719 stream
->name
, stream
->key
, stream
->net_seq_idx
);
727 * Find a relayd and close the stream
729 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
731 struct consumer_relayd_sock_pair
*relayd
;
733 /* The stream is not metadata. Get relayd reference if exists. */
735 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
737 consumer_stream_relayd_close(stream
, relayd
);
743 * Handle stream for relayd transmission if the stream applies for network
744 * streaming where the net sequence index is set.
746 * Return destination file descriptor or negative value on error.
748 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
749 size_t data_size
, unsigned long padding
,
750 struct consumer_relayd_sock_pair
*relayd
)
753 struct lttcomm_relayd_data_hdr data_hdr
;
759 /* Reset data header */
760 memset(&data_hdr
, 0, sizeof(data_hdr
));
762 if (stream
->metadata_flag
) {
763 /* Caller MUST acquire the relayd control socket lock */
764 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
769 /* Metadata are always sent on the control socket. */
770 outfd
= relayd
->control_sock
.sock
.fd
;
772 /* Set header with stream information */
773 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
774 data_hdr
.data_size
= htobe32(data_size
);
775 data_hdr
.padding_size
= htobe32(padding
);
777 * Note that net_seq_num below is assigned with the *current* value of
778 * next_net_seq_num and only after that the next_net_seq_num will be
779 * increment. This is why when issuing a command on the relayd using
780 * this next value, 1 should always be substracted in order to compare
781 * the last seen sequence number on the relayd side to the last sent.
783 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
784 /* Other fields are zeroed previously */
786 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
792 ++stream
->next_net_seq_num
;
794 /* Set to go on data socket */
795 outfd
= relayd
->data_sock
.sock
.fd
;
803 * Allocate and return a new lttng_consumer_channel object using the given key
804 * to initialize the hash table node.
806 * On error, return NULL.
808 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
810 const char *pathname
,
815 enum lttng_event_output output
,
816 uint64_t tracefile_size
,
817 uint64_t tracefile_count
,
818 uint64_t session_id_per_pid
,
819 unsigned int monitor
)
821 struct lttng_consumer_channel
*channel
;
823 channel
= zmalloc(sizeof(*channel
));
824 if (channel
== NULL
) {
825 PERROR("malloc struct lttng_consumer_channel");
830 channel
->refcount
= 0;
831 channel
->session_id
= session_id
;
832 channel
->session_id_per_pid
= session_id_per_pid
;
835 channel
->relayd_id
= relayd_id
;
836 channel
->output
= output
;
837 channel
->tracefile_size
= tracefile_size
;
838 channel
->tracefile_count
= tracefile_count
;
839 channel
->monitor
= monitor
;
840 pthread_mutex_init(&channel
->lock
, NULL
);
843 * In monitor mode, the streams associated with the channel will be put in
844 * a special list ONLY owned by this channel. So, the refcount is set to 1
845 * here meaning that the channel itself has streams that are referenced.
847 * On a channel deletion, once the channel is no longer visible, the
848 * refcount is decremented and checked for a zero value to delete it. With
849 * streams in no monitor mode, it will now be safe to destroy the channel.
851 if (!channel
->monitor
) {
852 channel
->refcount
= 1;
855 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
856 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
858 strncpy(channel
->name
, name
, sizeof(channel
->name
));
859 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
861 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
863 channel
->wait_fd
= -1;
865 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
867 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
874 * Add a channel to the global list protected by a mutex.
876 * On success 0 is returned else a negative value.
878 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
879 struct lttng_consumer_local_data
*ctx
)
882 struct lttng_ht_node_u64
*node
;
883 struct lttng_ht_iter iter
;
885 pthread_mutex_lock(&consumer_data
.lock
);
886 pthread_mutex_lock(&channel
->lock
);
889 lttng_ht_lookup(consumer_data
.channel_ht
, &channel
->key
, &iter
);
890 node
= lttng_ht_iter_get_node_u64(&iter
);
892 /* Channel already exist. Ignore the insertion */
893 ERR("Consumer add channel key %" PRIu64
" already exists!",
899 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
903 pthread_mutex_unlock(&channel
->lock
);
904 pthread_mutex_unlock(&consumer_data
.lock
);
906 if (!ret
&& channel
->wait_fd
!= -1 &&
907 channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
908 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
914 * Allocate the pollfd structure and the local view of the out fds to avoid
915 * doing a lookup in the linked list and concurrency issues when writing is
916 * needed. Called with consumer_data.lock held.
918 * Returns the number of fds in the structures.
920 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
921 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
925 struct lttng_ht_iter iter
;
926 struct lttng_consumer_stream
*stream
;
931 assert(local_stream
);
933 DBG("Updating poll fd array");
935 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
937 * Only active streams with an active end point can be added to the
938 * poll set and local stream storage of the thread.
940 * There is a potential race here for endpoint_status to be updated
941 * just after the check. However, this is OK since the stream(s) will
942 * be deleted once the thread is notified that the end point state has
943 * changed where this function will be called back again.
945 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
946 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
950 * This clobbers way too much the debug output. Uncomment that if you
951 * need it for debugging purposes.
953 * DBG("Active FD %d", stream->wait_fd);
955 (*pollfd
)[i
].fd
= stream
->wait_fd
;
956 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
957 local_stream
[i
] = stream
;
963 * Insert the consumer_data_pipe at the end of the array and don't
964 * increment i so nb_fd is the number of real FD.
966 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
967 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
972 * Poll on the should_quit pipe and the command socket return -1 on error and
973 * should exit, 0 if data is available on the command socket
975 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
980 num_rdy
= poll(consumer_sockpoll
, 2, -1);
983 * Restart interrupted system call.
985 if (errno
== EINTR
) {
988 PERROR("Poll error");
991 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
992 DBG("consumer_should_quit wake up");
1002 * Set the error socket.
1004 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1007 ctx
->consumer_error_socket
= sock
;
1011 * Set the command socket path.
1013 void lttng_consumer_set_command_sock_path(
1014 struct lttng_consumer_local_data
*ctx
, char *sock
)
1016 ctx
->consumer_command_sock_path
= sock
;
1020 * Send return code to the session daemon.
1021 * If the socket is not defined, we return 0, it is not a fatal error
1023 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1025 if (ctx
->consumer_error_socket
> 0) {
1026 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1027 sizeof(enum lttcomm_sessiond_command
));
1034 * Close all the tracefiles and stream fds and MUST be called when all
1035 * instances are destroyed i.e. when all threads were joined and are ended.
1037 void lttng_consumer_cleanup(void)
1039 struct lttng_ht_iter iter
;
1040 struct lttng_consumer_channel
*channel
;
1044 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1046 consumer_del_channel(channel
);
1051 lttng_ht_destroy(consumer_data
.channel_ht
);
1053 cleanup_relayd_ht();
1055 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1058 * This HT contains streams that are freed by either the metadata thread or
1059 * the data thread so we do *nothing* on the hash table and simply destroy
1062 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1066 * Called from signal handler.
1068 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1073 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1074 } while (ret
< 0 && errno
== EINTR
);
1075 if (ret
< 0 || ret
!= 1) {
1076 PERROR("write consumer quit");
1079 DBG("Consumer flag that it should quit");
1082 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1085 int outfd
= stream
->out_fd
;
1088 * This does a blocking write-and-wait on any page that belongs to the
1089 * subbuffer prior to the one we just wrote.
1090 * Don't care about error values, as these are just hints and ways to
1091 * limit the amount of page cache used.
1093 if (orig_offset
< stream
->max_sb_size
) {
1096 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1097 stream
->max_sb_size
,
1098 SYNC_FILE_RANGE_WAIT_BEFORE
1099 | SYNC_FILE_RANGE_WRITE
1100 | SYNC_FILE_RANGE_WAIT_AFTER
);
1102 * Give hints to the kernel about how we access the file:
1103 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1106 * We need to call fadvise again after the file grows because the
1107 * kernel does not seem to apply fadvise to non-existing parts of the
1110 * Call fadvise _after_ having waited for the page writeback to
1111 * complete because the dirty page writeback semantic is not well
1112 * defined. So it can be expected to lead to lower throughput in
1115 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1116 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1120 * Initialise the necessary environnement :
1121 * - create a new context
1122 * - create the poll_pipe
1123 * - create the should_quit pipe (for signal handler)
1124 * - create the thread pipe (for splice)
1126 * Takes a function pointer as argument, this function is called when data is
1127 * available on a buffer. This function is responsible to do the
1128 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1129 * buffer configuration and then kernctl_put_next_subbuf at the end.
1131 * Returns a pointer to the new context or NULL on error.
1133 struct lttng_consumer_local_data
*lttng_consumer_create(
1134 enum lttng_consumer_type type
,
1135 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1136 struct lttng_consumer_local_data
*ctx
),
1137 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1138 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1139 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1142 struct lttng_consumer_local_data
*ctx
;
1144 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1145 consumer_data
.type
== type
);
1146 consumer_data
.type
= type
;
1148 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1150 PERROR("allocating context");
1154 ctx
->consumer_error_socket
= -1;
1155 ctx
->consumer_metadata_socket
= -1;
1156 /* assign the callbacks */
1157 ctx
->on_buffer_ready
= buffer_ready
;
1158 ctx
->on_recv_channel
= recv_channel
;
1159 ctx
->on_recv_stream
= recv_stream
;
1160 ctx
->on_update_stream
= update_stream
;
1162 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1163 if (!ctx
->consumer_data_pipe
) {
1164 goto error_poll_pipe
;
1167 ret
= pipe(ctx
->consumer_should_quit
);
1169 PERROR("Error creating recv pipe");
1170 goto error_quit_pipe
;
1173 ret
= pipe(ctx
->consumer_thread_pipe
);
1175 PERROR("Error creating thread pipe");
1176 goto error_thread_pipe
;
1179 ret
= pipe(ctx
->consumer_channel_pipe
);
1181 PERROR("Error creating channel pipe");
1182 goto error_channel_pipe
;
1185 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1186 if (!ctx
->consumer_metadata_pipe
) {
1187 goto error_metadata_pipe
;
1190 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1192 goto error_splice_pipe
;
1198 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1199 error_metadata_pipe
:
1200 utils_close_pipe(ctx
->consumer_channel_pipe
);
1202 utils_close_pipe(ctx
->consumer_thread_pipe
);
1204 utils_close_pipe(ctx
->consumer_should_quit
);
1206 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1214 * Close all fds associated with the instance and free the context.
1216 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1220 DBG("Consumer destroying it. Closing everything.");
1222 ret
= close(ctx
->consumer_error_socket
);
1226 ret
= close(ctx
->consumer_metadata_socket
);
1230 utils_close_pipe(ctx
->consumer_thread_pipe
);
1231 utils_close_pipe(ctx
->consumer_channel_pipe
);
1232 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1233 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1234 utils_close_pipe(ctx
->consumer_should_quit
);
1235 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1237 unlink(ctx
->consumer_command_sock_path
);
1242 * Write the metadata stream id on the specified file descriptor.
1244 static int write_relayd_metadata_id(int fd
,
1245 struct lttng_consumer_stream
*stream
,
1246 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1249 struct lttcomm_relayd_metadata_payload hdr
;
1251 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1252 hdr
.padding_size
= htobe32(padding
);
1254 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1255 } while (ret
< 0 && errno
== EINTR
);
1256 if (ret
< 0 || ret
!= sizeof(hdr
)) {
1258 * This error means that the fd's end is closed so ignore the perror
1259 * not to clubber the error output since this can happen in a normal
1262 if (errno
!= EPIPE
) {
1263 PERROR("write metadata stream id");
1265 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1267 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1268 * handle writting the missing part so report that as an error and
1269 * don't lie to the caller.
1274 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1275 stream
->relayd_stream_id
, padding
);
1282 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1283 * core function for writing trace buffers to either the local filesystem or
1286 * It must be called with the stream lock held.
1288 * Careful review MUST be put if any changes occur!
1290 * Returns the number of bytes written
1292 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1293 struct lttng_consumer_local_data
*ctx
,
1294 struct lttng_consumer_stream
*stream
, unsigned long len
,
1295 unsigned long padding
)
1297 unsigned long mmap_offset
;
1299 ssize_t ret
= 0, written
= 0;
1300 off_t orig_offset
= stream
->out_fd_offset
;
1301 /* Default is on the disk */
1302 int outfd
= stream
->out_fd
;
1303 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1304 unsigned int relayd_hang_up
= 0;
1306 /* RCU lock for the relayd pointer */
1309 /* Flag that the current stream if set for network streaming. */
1310 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1311 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1312 if (relayd
== NULL
) {
1317 /* get the offset inside the fd to mmap */
1318 switch (consumer_data
.type
) {
1319 case LTTNG_CONSUMER_KERNEL
:
1320 mmap_base
= stream
->mmap_base
;
1321 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1323 case LTTNG_CONSUMER32_UST
:
1324 case LTTNG_CONSUMER64_UST
:
1325 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1327 ERR("read mmap get mmap base for stream %s", stream
->name
);
1331 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1335 ERR("Unknown consumer_data type");
1340 PERROR("tracer ctl get_mmap_read_offset");
1345 /* Handle stream on the relayd if the output is on the network */
1347 unsigned long netlen
= len
;
1350 * Lock the control socket for the complete duration of the function
1351 * since from this point on we will use the socket.
1353 if (stream
->metadata_flag
) {
1354 /* Metadata requires the control socket. */
1355 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1356 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1359 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1361 /* Use the returned socket. */
1364 /* Write metadata stream id before payload */
1365 if (stream
->metadata_flag
) {
1366 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1369 /* Socket operation failed. We consider the relayd dead */
1370 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1378 /* Socket operation failed. We consider the relayd dead */
1379 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1383 /* Else, use the default set before which is the filesystem. */
1386 /* No streaming, we have to set the len with the full padding */
1390 * Check if we need to change the tracefile before writing the packet.
1392 if (stream
->chan
->tracefile_size
> 0 &&
1393 (stream
->tracefile_size_current
+ len
) >
1394 stream
->chan
->tracefile_size
) {
1395 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1396 stream
->name
, stream
->chan
->tracefile_size
,
1397 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1398 stream
->out_fd
, &(stream
->tracefile_count_current
));
1400 ERR("Rotating output file");
1403 outfd
= stream
->out_fd
= ret
;
1404 /* Reset current size because we just perform a rotation. */
1405 stream
->tracefile_size_current
= 0;
1407 stream
->tracefile_size_current
+= len
;
1412 ret
= write(outfd
, mmap_base
+ mmap_offset
, len
);
1413 } while (ret
< 0 && errno
== EINTR
);
1414 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1417 * This is possible if the fd is closed on the other side (outfd)
1418 * or any write problem. It can be verbose a bit for a normal
1419 * execution if for instance the relayd is stopped abruptly. This
1420 * can happen so set this to a DBG statement.
1422 DBG("Error in file write mmap");
1426 /* Socket operation failed. We consider the relayd dead */
1427 if (errno
== EPIPE
|| errno
== EINVAL
) {
1432 } else if (ret
> len
) {
1433 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1441 /* This call is useless on a socket so better save a syscall. */
1443 /* This won't block, but will start writeout asynchronously */
1444 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1445 SYNC_FILE_RANGE_WRITE
);
1446 stream
->out_fd_offset
+= ret
;
1450 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1454 * This is a special case that the relayd has closed its socket. Let's
1455 * cleanup the relayd object and all associated streams.
1457 if (relayd
&& relayd_hang_up
) {
1458 cleanup_relayd(relayd
, ctx
);
1462 /* Unlock only if ctrl socket used */
1463 if (relayd
&& stream
->metadata_flag
) {
1464 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1472 * Splice the data from the ring buffer to the tracefile.
1474 * It must be called with the stream lock held.
1476 * Returns the number of bytes spliced.
1478 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1479 struct lttng_consumer_local_data
*ctx
,
1480 struct lttng_consumer_stream
*stream
, unsigned long len
,
1481 unsigned long padding
)
1483 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1485 off_t orig_offset
= stream
->out_fd_offset
;
1486 int fd
= stream
->wait_fd
;
1487 /* Default is on the disk */
1488 int outfd
= stream
->out_fd
;
1489 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1491 unsigned int relayd_hang_up
= 0;
1493 switch (consumer_data
.type
) {
1494 case LTTNG_CONSUMER_KERNEL
:
1496 case LTTNG_CONSUMER32_UST
:
1497 case LTTNG_CONSUMER64_UST
:
1498 /* Not supported for user space tracing */
1501 ERR("Unknown consumer_data type");
1505 /* RCU lock for the relayd pointer */
1508 /* Flag that the current stream if set for network streaming. */
1509 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1510 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1511 if (relayd
== NULL
) {
1517 * Choose right pipe for splice. Metadata and trace data are handled by
1518 * different threads hence the use of two pipes in order not to race or
1519 * corrupt the written data.
1521 if (stream
->metadata_flag
) {
1522 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1524 splice_pipe
= ctx
->consumer_thread_pipe
;
1527 /* Write metadata stream id before payload */
1529 int total_len
= len
;
1531 if (stream
->metadata_flag
) {
1533 * Lock the control socket for the complete duration of the function
1534 * since from this point on we will use the socket.
1536 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1538 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1542 /* Socket operation failed. We consider the relayd dead */
1543 if (ret
== -EBADF
) {
1544 WARN("Remote relayd disconnected. Stopping");
1551 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1554 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1556 /* Use the returned socket. */
1559 /* Socket operation failed. We consider the relayd dead */
1560 if (ret
== -EBADF
) {
1561 WARN("Remote relayd disconnected. Stopping");
1568 /* No streaming, we have to set the len with the full padding */
1572 * Check if we need to change the tracefile before writing the packet.
1574 if (stream
->chan
->tracefile_size
> 0 &&
1575 (stream
->tracefile_size_current
+ len
) >
1576 stream
->chan
->tracefile_size
) {
1577 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1578 stream
->name
, stream
->chan
->tracefile_size
,
1579 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1580 stream
->out_fd
, &(stream
->tracefile_count_current
));
1582 ERR("Rotating output file");
1585 outfd
= stream
->out_fd
= ret
;
1586 /* Reset current size because we just perform a rotation. */
1587 stream
->tracefile_size_current
= 0;
1589 stream
->tracefile_size_current
+= len
;
1593 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1594 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1595 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1596 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1597 DBG("splice chan to pipe, ret %zd", ret_splice
);
1598 if (ret_splice
< 0) {
1599 PERROR("Error in relay splice");
1601 written
= ret_splice
;
1607 /* Handle stream on the relayd if the output is on the network */
1609 if (stream
->metadata_flag
) {
1610 size_t metadata_payload_size
=
1611 sizeof(struct lttcomm_relayd_metadata_payload
);
1613 /* Update counter to fit the spliced data */
1614 ret_splice
+= metadata_payload_size
;
1615 len
+= metadata_payload_size
;
1617 * We do this so the return value can match the len passed as
1618 * argument to this function.
1620 written
-= metadata_payload_size
;
1624 /* Splice data out */
1625 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1626 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1627 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1628 if (ret_splice
< 0) {
1629 PERROR("Error in file splice");
1631 written
= ret_splice
;
1633 /* Socket operation failed. We consider the relayd dead */
1634 if (errno
== EBADF
|| errno
== EPIPE
) {
1635 WARN("Remote relayd disconnected. Stopping");
1641 } else if (ret_splice
> len
) {
1643 PERROR("Wrote more data than requested %zd (len: %lu)",
1645 written
+= ret_splice
;
1651 /* This call is useless on a socket so better save a syscall. */
1653 /* This won't block, but will start writeout asynchronously */
1654 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1655 SYNC_FILE_RANGE_WRITE
);
1656 stream
->out_fd_offset
+= ret_splice
;
1658 written
+= ret_splice
;
1660 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1668 * This is a special case that the relayd has closed its socket. Let's
1669 * cleanup the relayd object and all associated streams.
1671 if (relayd
&& relayd_hang_up
) {
1672 cleanup_relayd(relayd
, ctx
);
1673 /* Skip splice error so the consumer does not fail */
1678 /* send the appropriate error description to sessiond */
1681 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1684 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1687 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1692 if (relayd
&& stream
->metadata_flag
) {
1693 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1701 * Take a snapshot for a specific fd
1703 * Returns 0 on success, < 0 on error
1705 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1707 switch (consumer_data
.type
) {
1708 case LTTNG_CONSUMER_KERNEL
:
1709 return lttng_kconsumer_take_snapshot(stream
);
1710 case LTTNG_CONSUMER32_UST
:
1711 case LTTNG_CONSUMER64_UST
:
1712 return lttng_ustconsumer_take_snapshot(stream
);
1714 ERR("Unknown consumer_data type");
1721 * Get the produced position
1723 * Returns 0 on success, < 0 on error
1725 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1728 switch (consumer_data
.type
) {
1729 case LTTNG_CONSUMER_KERNEL
:
1730 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1731 case LTTNG_CONSUMER32_UST
:
1732 case LTTNG_CONSUMER64_UST
:
1733 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1735 ERR("Unknown consumer_data type");
1741 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1742 int sock
, struct pollfd
*consumer_sockpoll
)
1744 switch (consumer_data
.type
) {
1745 case LTTNG_CONSUMER_KERNEL
:
1746 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1747 case LTTNG_CONSUMER32_UST
:
1748 case LTTNG_CONSUMER64_UST
:
1749 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1751 ERR("Unknown consumer_data type");
1758 * Iterate over all streams of the hashtable and free them properly.
1760 * WARNING: *MUST* be used with data stream only.
1762 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1764 struct lttng_ht_iter iter
;
1765 struct lttng_consumer_stream
*stream
;
1772 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1774 * Ignore return value since we are currently cleaning up so any error
1777 (void) consumer_del_stream(stream
, ht
);
1781 lttng_ht_destroy(ht
);
1785 * Iterate over all streams of the hashtable and free them properly.
1787 * XXX: Should not be only for metadata stream or else use an other name.
1789 static void destroy_stream_ht(struct lttng_ht
*ht
)
1791 struct lttng_ht_iter iter
;
1792 struct lttng_consumer_stream
*stream
;
1799 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1801 * Ignore return value since we are currently cleaning up so any error
1804 (void) consumer_del_metadata_stream(stream
, ht
);
1808 lttng_ht_destroy(ht
);
1811 void lttng_consumer_close_metadata(void)
1813 switch (consumer_data
.type
) {
1814 case LTTNG_CONSUMER_KERNEL
:
1816 * The Kernel consumer has a different metadata scheme so we don't
1817 * close anything because the stream will be closed by the session
1821 case LTTNG_CONSUMER32_UST
:
1822 case LTTNG_CONSUMER64_UST
:
1824 * Close all metadata streams. The metadata hash table is passed and
1825 * this call iterates over it by closing all wakeup fd. This is safe
1826 * because at this point we are sure that the metadata producer is
1827 * either dead or blocked.
1829 lttng_ustconsumer_close_metadata(metadata_ht
);
1832 ERR("Unknown consumer_data type");
1838 * Clean up a metadata stream and free its memory.
1840 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1841 struct lttng_ht
*ht
)
1844 struct lttng_ht_iter iter
;
1845 struct lttng_consumer_channel
*free_chan
= NULL
;
1846 struct consumer_relayd_sock_pair
*relayd
;
1850 * This call should NEVER receive regular stream. It must always be
1851 * metadata stream and this is crucial for data structure synchronization.
1853 assert(stream
->metadata_flag
);
1855 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1858 /* Means the stream was allocated but not successfully added */
1859 goto free_stream_rcu
;
1862 pthread_mutex_lock(&consumer_data
.lock
);
1863 pthread_mutex_lock(&stream
->chan
->lock
);
1864 pthread_mutex_lock(&stream
->lock
);
1866 switch (consumer_data
.type
) {
1867 case LTTNG_CONSUMER_KERNEL
:
1868 if (stream
->mmap_base
!= NULL
) {
1869 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1871 PERROR("munmap metadata stream");
1874 if (stream
->wait_fd
>= 0) {
1875 ret
= close(stream
->wait_fd
);
1877 PERROR("close kernel metadata wait_fd");
1881 case LTTNG_CONSUMER32_UST
:
1882 case LTTNG_CONSUMER64_UST
:
1883 lttng_ustconsumer_del_stream(stream
);
1886 ERR("Unknown consumer_data type");
1892 iter
.iter
.node
= &stream
->node
.node
;
1893 ret
= lttng_ht_del(ht
, &iter
);
1896 iter
.iter
.node
= &stream
->node_channel_id
.node
;
1897 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
1900 iter
.iter
.node
= &stream
->node_session_id
.node
;
1901 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1905 if (stream
->out_fd
>= 0) {
1906 ret
= close(stream
->out_fd
);
1912 /* Check and cleanup relayd */
1914 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1915 if (relayd
!= NULL
) {
1916 uatomic_dec(&relayd
->refcount
);
1917 assert(uatomic_read(&relayd
->refcount
) >= 0);
1919 /* Closing streams requires to lock the control socket. */
1920 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1921 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1922 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1923 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1925 DBG("Unable to close stream on the relayd. Continuing");
1927 * Continue here. There is nothing we can do for the relayd.
1928 * Chances are that the relayd has closed the socket so we just
1929 * continue cleaning up.
1933 /* Both conditions are met, we destroy the relayd. */
1934 if (uatomic_read(&relayd
->refcount
) == 0 &&
1935 uatomic_read(&relayd
->destroy_flag
)) {
1936 consumer_destroy_relayd(relayd
);
1941 /* Atomically decrement channel refcount since other threads can use it. */
1942 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
1943 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
1944 /* Go for channel deletion! */
1945 free_chan
= stream
->chan
;
1950 * Nullify the stream reference so it is not used after deletion. The
1951 * consumer data lock MUST be acquired before being able to check for a
1952 * NULL pointer value.
1954 stream
->chan
->metadata_stream
= NULL
;
1956 pthread_mutex_unlock(&stream
->lock
);
1957 pthread_mutex_unlock(&stream
->chan
->lock
);
1958 pthread_mutex_unlock(&consumer_data
.lock
);
1961 consumer_del_channel(free_chan
);
1965 call_rcu(&stream
->node
.head
, free_stream_rcu
);
1969 * Action done with the metadata stream when adding it to the consumer internal
1970 * data structures to handle it.
1972 static int add_metadata_stream(struct lttng_consumer_stream
*stream
,
1973 struct lttng_ht
*ht
)
1976 struct lttng_ht_iter iter
;
1977 struct lttng_ht_node_u64
*node
;
1982 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
1984 pthread_mutex_lock(&consumer_data
.lock
);
1985 pthread_mutex_lock(&stream
->chan
->lock
);
1986 pthread_mutex_lock(&stream
->lock
);
1989 * From here, refcounts are updated so be _careful_ when returning an error
1996 * Lookup the stream just to make sure it does not exist in our internal
1997 * state. This should NEVER happen.
1999 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2000 node
= lttng_ht_iter_get_node_u64(&iter
);
2004 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2005 * in terms of destroying the associated channel, because the action that
2006 * causes the count to become 0 also causes a stream to be added. The
2007 * channel deletion will thus be triggered by the following removal of this
2010 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2011 /* Increment refcount before decrementing nb_init_stream_left */
2013 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2016 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2018 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2019 &stream
->node_channel_id
);
2022 * Add stream to the stream_list_ht of the consumer data. No need to steal
2023 * the key since the HT does not use it and we allow to add redundant keys
2026 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2030 pthread_mutex_unlock(&stream
->lock
);
2031 pthread_mutex_unlock(&stream
->chan
->lock
);
2032 pthread_mutex_unlock(&consumer_data
.lock
);
2037 * Delete data stream that are flagged for deletion (endpoint_status).
2039 static void validate_endpoint_status_data_stream(void)
2041 struct lttng_ht_iter iter
;
2042 struct lttng_consumer_stream
*stream
;
2044 DBG("Consumer delete flagged data stream");
2047 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2048 /* Validate delete flag of the stream */
2049 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2052 /* Delete it right now */
2053 consumer_del_stream(stream
, data_ht
);
2059 * Delete metadata stream that are flagged for deletion (endpoint_status).
2061 static void validate_endpoint_status_metadata_stream(
2062 struct lttng_poll_event
*pollset
)
2064 struct lttng_ht_iter iter
;
2065 struct lttng_consumer_stream
*stream
;
2067 DBG("Consumer delete flagged metadata stream");
2072 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2073 /* Validate delete flag of the stream */
2074 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2078 * Remove from pollset so the metadata thread can continue without
2079 * blocking on a deleted stream.
2081 lttng_poll_del(pollset
, stream
->wait_fd
);
2083 /* Delete it right now */
2084 consumer_del_metadata_stream(stream
, metadata_ht
);
2090 * Thread polls on metadata file descriptor and write them on disk or on the
2093 void *consumer_thread_metadata_poll(void *data
)
2096 uint32_t revents
, nb_fd
;
2097 struct lttng_consumer_stream
*stream
= NULL
;
2098 struct lttng_ht_iter iter
;
2099 struct lttng_ht_node_u64
*node
;
2100 struct lttng_poll_event events
;
2101 struct lttng_consumer_local_data
*ctx
= data
;
2104 rcu_register_thread();
2106 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2108 /* ENOMEM at this point. Better to bail out. */
2112 DBG("Thread metadata poll started");
2114 /* Size is set to 1 for the consumer_metadata pipe */
2115 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2117 ERR("Poll set creation failed");
2121 ret
= lttng_poll_add(&events
,
2122 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2128 DBG("Metadata main loop started");
2131 /* Only the metadata pipe is set */
2132 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2137 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2138 ret
= lttng_poll_wait(&events
, -1);
2139 DBG("Metadata event catched in thread");
2141 if (errno
== EINTR
) {
2142 ERR("Poll EINTR catched");
2150 /* From here, the event is a metadata wait fd */
2151 for (i
= 0; i
< nb_fd
; i
++) {
2152 revents
= LTTNG_POLL_GETEV(&events
, i
);
2153 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2155 /* Just don't waste time if no returned events for the fd */
2160 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2161 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2162 DBG("Metadata thread pipe hung up");
2164 * Remove the pipe from the poll set and continue the loop
2165 * since their might be data to consume.
2167 lttng_poll_del(&events
,
2168 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2169 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2171 } else if (revents
& LPOLLIN
) {
2174 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2175 &stream
, sizeof(stream
));
2177 ERR("read metadata stream, ret: %ld", pipe_len
);
2179 * Continue here to handle the rest of the streams.
2184 /* A NULL stream means that the state has changed. */
2185 if (stream
== NULL
) {
2186 /* Check for deleted streams. */
2187 validate_endpoint_status_metadata_stream(&events
);
2191 DBG("Adding metadata stream %d to poll set",
2194 ret
= add_metadata_stream(stream
, metadata_ht
);
2196 ERR("Unable to add metadata stream");
2197 /* Stream was not setup properly. Continuing. */
2198 consumer_del_metadata_stream(stream
, NULL
);
2202 /* Add metadata stream to the global poll events list */
2203 lttng_poll_add(&events
, stream
->wait_fd
,
2204 LPOLLIN
| LPOLLPRI
);
2207 /* Handle other stream */
2213 uint64_t tmp_id
= (uint64_t) pollfd
;
2215 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2217 node
= lttng_ht_iter_get_node_u64(&iter
);
2220 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2223 /* Check for error event */
2224 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2225 DBG("Metadata fd %d is hup|err.", pollfd
);
2226 if (!stream
->hangup_flush_done
2227 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2228 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2229 DBG("Attempting to flush and consume the UST buffers");
2230 lttng_ustconsumer_on_stream_hangup(stream
);
2232 /* We just flushed the stream now read it. */
2234 len
= ctx
->on_buffer_ready(stream
, ctx
);
2236 * We don't check the return value here since if we get
2237 * a negative len, it means an error occured thus we
2238 * simply remove it from the poll set and free the
2244 lttng_poll_del(&events
, stream
->wait_fd
);
2246 * This call update the channel states, closes file descriptors
2247 * and securely free the stream.
2249 consumer_del_metadata_stream(stream
, metadata_ht
);
2250 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2251 /* Get the data out of the metadata file descriptor */
2252 DBG("Metadata available on fd %d", pollfd
);
2253 assert(stream
->wait_fd
== pollfd
);
2255 len
= ctx
->on_buffer_ready(stream
, ctx
);
2256 /* It's ok to have an unavailable sub-buffer */
2257 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2258 /* Clean up stream from consumer and free it. */
2259 lttng_poll_del(&events
, stream
->wait_fd
);
2260 consumer_del_metadata_stream(stream
, metadata_ht
);
2261 } else if (len
> 0) {
2262 stream
->data_read
= 1;
2266 /* Release RCU lock for the stream looked up */
2273 DBG("Metadata poll thread exiting");
2275 lttng_poll_clean(&events
);
2277 destroy_stream_ht(metadata_ht
);
2279 rcu_unregister_thread();
2284 * This thread polls the fds in the set to consume the data and write
2285 * it to tracefile if necessary.
2287 void *consumer_thread_data_poll(void *data
)
2289 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2290 struct pollfd
*pollfd
= NULL
;
2291 /* local view of the streams */
2292 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2293 /* local view of consumer_data.fds_count */
2295 struct lttng_consumer_local_data
*ctx
= data
;
2298 rcu_register_thread();
2300 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2301 if (data_ht
== NULL
) {
2302 /* ENOMEM at this point. Better to bail out. */
2306 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2307 if (local_stream
== NULL
) {
2308 PERROR("local_stream malloc");
2317 * the fds set has been updated, we need to update our
2318 * local array as well
2320 pthread_mutex_lock(&consumer_data
.lock
);
2321 if (consumer_data
.need_update
) {
2326 local_stream
= NULL
;
2328 /* allocate for all fds + 1 for the consumer_data_pipe */
2329 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2330 if (pollfd
== NULL
) {
2331 PERROR("pollfd malloc");
2332 pthread_mutex_unlock(&consumer_data
.lock
);
2336 /* allocate for all fds + 1 for the consumer_data_pipe */
2337 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2338 sizeof(struct lttng_consumer_stream
*));
2339 if (local_stream
== NULL
) {
2340 PERROR("local_stream malloc");
2341 pthread_mutex_unlock(&consumer_data
.lock
);
2344 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2347 ERR("Error in allocating pollfd or local_outfds");
2348 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2349 pthread_mutex_unlock(&consumer_data
.lock
);
2353 consumer_data
.need_update
= 0;
2355 pthread_mutex_unlock(&consumer_data
.lock
);
2357 /* No FDs and consumer_quit, consumer_cleanup the thread */
2358 if (nb_fd
== 0 && consumer_quit
== 1) {
2361 /* poll on the array of fds */
2363 DBG("polling on %d fd", nb_fd
+ 1);
2364 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2365 DBG("poll num_rdy : %d", num_rdy
);
2366 if (num_rdy
== -1) {
2368 * Restart interrupted system call.
2370 if (errno
== EINTR
) {
2373 PERROR("Poll error");
2374 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2376 } else if (num_rdy
== 0) {
2377 DBG("Polling thread timed out");
2382 * If the consumer_data_pipe triggered poll go directly to the
2383 * beginning of the loop to update the array. We want to prioritize
2384 * array update over low-priority reads.
2386 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2387 ssize_t pipe_readlen
;
2389 DBG("consumer_data_pipe wake up");
2390 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2391 &new_stream
, sizeof(new_stream
));
2392 if (pipe_readlen
< 0) {
2393 ERR("Consumer data pipe ret %ld", pipe_readlen
);
2394 /* Continue so we can at least handle the current stream(s). */
2399 * If the stream is NULL, just ignore it. It's also possible that
2400 * the sessiond poll thread changed the consumer_quit state and is
2401 * waking us up to test it.
2403 if (new_stream
== NULL
) {
2404 validate_endpoint_status_data_stream();
2408 ret
= add_stream(new_stream
, data_ht
);
2410 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
2413 * At this point, if the add_stream fails, it is not in the
2414 * hash table thus passing the NULL value here.
2416 consumer_del_stream(new_stream
, NULL
);
2419 /* Continue to update the local streams and handle prio ones */
2423 /* Take care of high priority channels first. */
2424 for (i
= 0; i
< nb_fd
; i
++) {
2425 if (local_stream
[i
] == NULL
) {
2428 if (pollfd
[i
].revents
& POLLPRI
) {
2429 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2431 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2432 /* it's ok to have an unavailable sub-buffer */
2433 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2434 /* Clean the stream and free it. */
2435 consumer_del_stream(local_stream
[i
], data_ht
);
2436 local_stream
[i
] = NULL
;
2437 } else if (len
> 0) {
2438 local_stream
[i
]->data_read
= 1;
2444 * If we read high prio channel in this loop, try again
2445 * for more high prio data.
2451 /* Take care of low priority channels. */
2452 for (i
= 0; i
< nb_fd
; i
++) {
2453 if (local_stream
[i
] == NULL
) {
2456 if ((pollfd
[i
].revents
& POLLIN
) ||
2457 local_stream
[i
]->hangup_flush_done
) {
2458 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2459 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2460 /* it's ok to have an unavailable sub-buffer */
2461 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2462 /* Clean the stream and free it. */
2463 consumer_del_stream(local_stream
[i
], data_ht
);
2464 local_stream
[i
] = NULL
;
2465 } else if (len
> 0) {
2466 local_stream
[i
]->data_read
= 1;
2471 /* Handle hangup and errors */
2472 for (i
= 0; i
< nb_fd
; i
++) {
2473 if (local_stream
[i
] == NULL
) {
2476 if (!local_stream
[i
]->hangup_flush_done
2477 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2478 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2479 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2480 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2482 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2483 /* Attempt read again, for the data we just flushed. */
2484 local_stream
[i
]->data_read
= 1;
2487 * If the poll flag is HUP/ERR/NVAL and we have
2488 * read no data in this pass, we can remove the
2489 * stream from its hash table.
2491 if ((pollfd
[i
].revents
& POLLHUP
)) {
2492 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2493 if (!local_stream
[i
]->data_read
) {
2494 consumer_del_stream(local_stream
[i
], data_ht
);
2495 local_stream
[i
] = NULL
;
2498 } else if (pollfd
[i
].revents
& POLLERR
) {
2499 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2500 if (!local_stream
[i
]->data_read
) {
2501 consumer_del_stream(local_stream
[i
], data_ht
);
2502 local_stream
[i
] = NULL
;
2505 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2506 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2507 if (!local_stream
[i
]->data_read
) {
2508 consumer_del_stream(local_stream
[i
], data_ht
);
2509 local_stream
[i
] = NULL
;
2513 if (local_stream
[i
] != NULL
) {
2514 local_stream
[i
]->data_read
= 0;
2519 DBG("polling thread exiting");
2524 * Close the write side of the pipe so epoll_wait() in
2525 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2526 * read side of the pipe. If we close them both, epoll_wait strangely does
2527 * not return and could create a endless wait period if the pipe is the
2528 * only tracked fd in the poll set. The thread will take care of closing
2531 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2533 destroy_data_stream_ht(data_ht
);
2535 rcu_unregister_thread();
2540 * Close wake-up end of each stream belonging to the channel. This will
2541 * allow the poll() on the stream read-side to detect when the
2542 * write-side (application) finally closes them.
2545 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2547 struct lttng_ht
*ht
;
2548 struct lttng_consumer_stream
*stream
;
2549 struct lttng_ht_iter iter
;
2551 ht
= consumer_data
.stream_per_chan_id_ht
;
2554 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2555 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2556 ht
->match_fct
, &channel
->key
,
2557 &iter
.iter
, stream
, node_channel_id
.node
) {
2559 * Protect against teardown with mutex.
2561 pthread_mutex_lock(&stream
->lock
);
2562 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2565 switch (consumer_data
.type
) {
2566 case LTTNG_CONSUMER_KERNEL
:
2568 case LTTNG_CONSUMER32_UST
:
2569 case LTTNG_CONSUMER64_UST
:
2571 * Note: a mutex is taken internally within
2572 * liblttng-ust-ctl to protect timer wakeup_fd
2573 * use from concurrent close.
2575 lttng_ustconsumer_close_stream_wakeup(stream
);
2578 ERR("Unknown consumer_data type");
2582 pthread_mutex_unlock(&stream
->lock
);
2587 static void destroy_channel_ht(struct lttng_ht
*ht
)
2589 struct lttng_ht_iter iter
;
2590 struct lttng_consumer_channel
*channel
;
2598 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2599 ret
= lttng_ht_del(ht
, &iter
);
2604 lttng_ht_destroy(ht
);
2608 * This thread polls the channel fds to detect when they are being
2609 * closed. It closes all related streams if the channel is detected as
2610 * closed. It is currently only used as a shim layer for UST because the
2611 * consumerd needs to keep the per-stream wakeup end of pipes open for
2614 void *consumer_thread_channel_poll(void *data
)
2617 uint32_t revents
, nb_fd
;
2618 struct lttng_consumer_channel
*chan
= NULL
;
2619 struct lttng_ht_iter iter
;
2620 struct lttng_ht_node_u64
*node
;
2621 struct lttng_poll_event events
;
2622 struct lttng_consumer_local_data
*ctx
= data
;
2623 struct lttng_ht
*channel_ht
;
2625 rcu_register_thread();
2627 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2629 /* ENOMEM at this point. Better to bail out. */
2633 DBG("Thread channel poll started");
2635 /* Size is set to 1 for the consumer_channel pipe */
2636 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2638 ERR("Poll set creation failed");
2642 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2648 DBG("Channel main loop started");
2651 /* Only the channel pipe is set */
2652 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2657 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2658 ret
= lttng_poll_wait(&events
, -1);
2659 DBG("Channel event catched in thread");
2661 if (errno
== EINTR
) {
2662 ERR("Poll EINTR catched");
2670 /* From here, the event is a channel wait fd */
2671 for (i
= 0; i
< nb_fd
; i
++) {
2672 revents
= LTTNG_POLL_GETEV(&events
, i
);
2673 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2675 /* Just don't waste time if no returned events for the fd */
2679 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2680 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2681 DBG("Channel thread pipe hung up");
2683 * Remove the pipe from the poll set and continue the loop
2684 * since their might be data to consume.
2686 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2688 } else if (revents
& LPOLLIN
) {
2689 enum consumer_channel_action action
;
2692 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2694 ERR("Error reading channel pipe");
2699 case CONSUMER_CHANNEL_ADD
:
2700 DBG("Adding channel %d to poll set",
2703 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2706 lttng_ht_add_unique_u64(channel_ht
,
2707 &chan
->wait_fd_node
);
2709 /* Add channel to the global poll events list */
2710 lttng_poll_add(&events
, chan
->wait_fd
,
2711 LPOLLIN
| LPOLLPRI
);
2713 case CONSUMER_CHANNEL_DEL
:
2715 struct lttng_consumer_stream
*stream
, *stmp
;
2718 chan
= consumer_find_channel(key
);
2721 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2724 lttng_poll_del(&events
, chan
->wait_fd
);
2725 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2726 ret
= lttng_ht_del(channel_ht
, &iter
);
2728 consumer_close_channel_streams(chan
);
2730 switch (consumer_data
.type
) {
2731 case LTTNG_CONSUMER_KERNEL
:
2733 case LTTNG_CONSUMER32_UST
:
2734 case LTTNG_CONSUMER64_UST
:
2735 /* Delete streams that might have been left in the stream list. */
2736 cds_list_for_each_entry_safe(stream
, stmp
, &chan
->streams
.head
,
2738 cds_list_del(&stream
->send_node
);
2739 lttng_ustconsumer_del_stream(stream
);
2740 uatomic_sub(&stream
->chan
->refcount
, 1);
2741 assert(&chan
->refcount
);
2746 ERR("Unknown consumer_data type");
2751 * Release our own refcount. Force channel deletion even if
2752 * streams were not initialized.
2754 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2755 consumer_del_channel(chan
);
2760 case CONSUMER_CHANNEL_QUIT
:
2762 * Remove the pipe from the poll set and continue the loop
2763 * since their might be data to consume.
2765 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2768 ERR("Unknown action");
2773 /* Handle other stream */
2779 uint64_t tmp_id
= (uint64_t) pollfd
;
2781 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2783 node
= lttng_ht_iter_get_node_u64(&iter
);
2786 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2789 /* Check for error event */
2790 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2791 DBG("Channel fd %d is hup|err.", pollfd
);
2793 lttng_poll_del(&events
, chan
->wait_fd
);
2794 ret
= lttng_ht_del(channel_ht
, &iter
);
2796 consumer_close_channel_streams(chan
);
2798 /* Release our own refcount */
2799 if (!uatomic_sub_return(&chan
->refcount
, 1)
2800 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2801 consumer_del_channel(chan
);
2805 /* Release RCU lock for the channel looked up */
2811 lttng_poll_clean(&events
);
2813 destroy_channel_ht(channel_ht
);
2815 DBG("Channel poll thread exiting");
2816 rcu_unregister_thread();
2820 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2821 struct pollfd
*sockpoll
, int client_socket
)
2828 if (lttng_consumer_poll_socket(sockpoll
) < 0) {
2832 DBG("Metadata connection on client_socket");
2834 /* Blocking call, waiting for transmission */
2835 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2836 if (ctx
->consumer_metadata_socket
< 0) {
2837 WARN("On accept metadata");
2848 * This thread listens on the consumerd socket and receives the file
2849 * descriptors from the session daemon.
2851 void *consumer_thread_sessiond_poll(void *data
)
2853 int sock
= -1, client_socket
, ret
;
2855 * structure to poll for incoming data on communication socket avoids
2856 * making blocking sockets.
2858 struct pollfd consumer_sockpoll
[2];
2859 struct lttng_consumer_local_data
*ctx
= data
;
2861 rcu_register_thread();
2863 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2864 unlink(ctx
->consumer_command_sock_path
);
2865 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2866 if (client_socket
< 0) {
2867 ERR("Cannot create command socket");
2871 ret
= lttcomm_listen_unix_sock(client_socket
);
2876 DBG("Sending ready command to lttng-sessiond");
2877 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2878 /* return < 0 on error, but == 0 is not fatal */
2880 ERR("Error sending ready command to lttng-sessiond");
2884 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2885 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2886 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2887 consumer_sockpoll
[1].fd
= client_socket
;
2888 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2890 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2893 DBG("Connection on client_socket");
2895 /* Blocking call, waiting for transmission */
2896 sock
= lttcomm_accept_unix_sock(client_socket
);
2903 * Setup metadata socket which is the second socket connection on the
2904 * command unix socket.
2906 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
2911 /* This socket is not useful anymore. */
2912 ret
= close(client_socket
);
2914 PERROR("close client_socket");
2918 /* update the polling structure to poll on the established socket */
2919 consumer_sockpoll
[1].fd
= sock
;
2920 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2923 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2926 DBG("Incoming command on sock");
2927 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2928 if (ret
== -ENOENT
) {
2929 DBG("Received STOP command");
2934 * This could simply be a session daemon quitting. Don't output
2937 DBG("Communication interrupted on command socket");
2940 if (consumer_quit
) {
2941 DBG("consumer_thread_receive_fds received quit from signal");
2944 DBG("received command on sock");
2947 DBG("Consumer thread sessiond poll exiting");
2950 * Close metadata streams since the producer is the session daemon which
2953 * NOTE: for now, this only applies to the UST tracer.
2955 lttng_consumer_close_metadata();
2958 * when all fds have hung up, the polling thread
2964 * Notify the data poll thread to poll back again and test the
2965 * consumer_quit state that we just set so to quit gracefully.
2967 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
2969 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
2971 /* Cleaning up possibly open sockets. */
2975 PERROR("close sock sessiond poll");
2978 if (client_socket
>= 0) {
2979 ret
= close(client_socket
);
2981 PERROR("close client_socket sessiond poll");
2985 rcu_unregister_thread();
2989 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2990 struct lttng_consumer_local_data
*ctx
)
2994 pthread_mutex_lock(&stream
->lock
);
2996 switch (consumer_data
.type
) {
2997 case LTTNG_CONSUMER_KERNEL
:
2998 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3000 case LTTNG_CONSUMER32_UST
:
3001 case LTTNG_CONSUMER64_UST
:
3002 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3005 ERR("Unknown consumer_data type");
3011 pthread_mutex_unlock(&stream
->lock
);
3015 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3017 switch (consumer_data
.type
) {
3018 case LTTNG_CONSUMER_KERNEL
:
3019 return lttng_kconsumer_on_recv_stream(stream
);
3020 case LTTNG_CONSUMER32_UST
:
3021 case LTTNG_CONSUMER64_UST
:
3022 return lttng_ustconsumer_on_recv_stream(stream
);
3024 ERR("Unknown consumer_data type");
3031 * Allocate and set consumer data hash tables.
3033 void lttng_consumer_init(void)
3035 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3036 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3037 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3038 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3042 * Process the ADD_RELAYD command receive by a consumer.
3044 * This will create a relayd socket pair and add it to the relayd hash table.
3045 * The caller MUST acquire a RCU read side lock before calling it.
3047 int consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3048 struct lttng_consumer_local_data
*ctx
, int sock
,
3049 struct pollfd
*consumer_sockpoll
,
3050 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
)
3052 int fd
= -1, ret
= -1, relayd_created
= 0;
3053 enum lttng_error_code ret_code
= LTTNG_OK
;
3054 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3057 assert(relayd_sock
);
3059 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3061 /* Get relayd reference if exists. */
3062 relayd
= consumer_find_relayd(net_seq_idx
);
3063 if (relayd
== NULL
) {
3064 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3065 /* Not found. Allocate one. */
3066 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3067 if (relayd
== NULL
) {
3069 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3072 relayd
->sessiond_session_id
= sessiond_id
;
3077 * This code path MUST continue to the consumer send status message to
3078 * we can notify the session daemon and continue our work without
3079 * killing everything.
3083 * relayd key should never be found for control socket.
3085 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3088 /* First send a status message before receiving the fds. */
3089 ret
= consumer_send_status_msg(sock
, LTTNG_OK
);
3091 /* Somehow, the session daemon is not responding anymore. */
3092 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3093 goto error_nosignal
;
3096 /* Poll on consumer socket. */
3097 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
3098 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3100 goto error_nosignal
;
3103 /* Get relayd socket from session daemon */
3104 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3105 if (ret
!= sizeof(fd
)) {
3107 fd
= -1; /* Just in case it gets set with an invalid value. */
3110 * Failing to receive FDs might indicate a major problem such as
3111 * reaching a fd limit during the receive where the kernel returns a
3112 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3113 * don't take any chances and stop everything.
3115 * XXX: Feature request #558 will fix that and avoid this possible
3116 * issue when reaching the fd limit.
3118 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3119 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3123 /* Copy socket information and received FD */
3124 switch (sock_type
) {
3125 case LTTNG_STREAM_CONTROL
:
3126 /* Copy received lttcomm socket */
3127 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3128 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3129 /* Handle create_sock error. */
3131 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3135 * Close the socket created internally by
3136 * lttcomm_create_sock, so we can replace it by the one
3137 * received from sessiond.
3139 if (close(relayd
->control_sock
.sock
.fd
)) {
3143 /* Assign new file descriptor */
3144 relayd
->control_sock
.sock
.fd
= fd
;
3145 fd
= -1; /* For error path */
3146 /* Assign version values. */
3147 relayd
->control_sock
.major
= relayd_sock
->major
;
3148 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3151 * Create a session on the relayd and store the returned id. Lock the
3152 * control socket mutex if the relayd was NOT created before.
3154 if (!relayd_created
) {
3155 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3157 ret
= relayd_create_session(&relayd
->control_sock
,
3158 &relayd
->relayd_session_id
);
3159 if (!relayd_created
) {
3160 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3164 * Close all sockets of a relayd object. It will be freed if it was
3165 * created at the error code path or else it will be garbage
3168 (void) relayd_close(&relayd
->control_sock
);
3169 (void) relayd_close(&relayd
->data_sock
);
3170 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
3175 case LTTNG_STREAM_DATA
:
3176 /* Copy received lttcomm socket */
3177 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3178 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3179 /* Handle create_sock error. */
3181 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3185 * Close the socket created internally by
3186 * lttcomm_create_sock, so we can replace it by the one
3187 * received from sessiond.
3189 if (close(relayd
->data_sock
.sock
.fd
)) {
3193 /* Assign new file descriptor */
3194 relayd
->data_sock
.sock
.fd
= fd
;
3195 fd
= -1; /* for eventual error paths */
3196 /* Assign version values. */
3197 relayd
->data_sock
.major
= relayd_sock
->major
;
3198 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3201 ERR("Unknown relayd socket type (%d)", sock_type
);
3203 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3207 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3208 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3209 relayd
->net_seq_idx
, fd
);
3211 /* We successfully added the socket. Send status back. */
3212 ret
= consumer_send_status_msg(sock
, ret_code
);
3214 /* Somehow, the session daemon is not responding anymore. */
3215 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3216 goto error_nosignal
;
3220 * Add relayd socket pair to consumer data hashtable. If object already
3221 * exists or on error, the function gracefully returns.
3229 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3230 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3234 /* Close received socket if valid. */
3237 PERROR("close received socket");
3241 if (relayd_created
) {
3249 * Try to lock the stream mutex.
3251 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3253 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3260 * Try to lock the stream mutex. On failure, we know that the stream is
3261 * being used else where hence there is data still being extracted.
3263 ret
= pthread_mutex_trylock(&stream
->lock
);
3265 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3277 * Search for a relayd associated to the session id and return the reference.
3279 * A rcu read side lock MUST be acquire before calling this function and locked
3280 * until the relayd object is no longer necessary.
3282 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3284 struct lttng_ht_iter iter
;
3285 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3287 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3288 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3291 * Check by sessiond id which is unique here where the relayd session
3292 * id might not be when having multiple relayd.
3294 if (relayd
->sessiond_session_id
== id
) {
3295 /* Found the relayd. There can be only one per id. */
3307 * Check if for a given session id there is still data needed to be extract
3310 * Return 1 if data is pending or else 0 meaning ready to be read.
3312 int consumer_data_pending(uint64_t id
)
3315 struct lttng_ht_iter iter
;
3316 struct lttng_ht
*ht
;
3317 struct lttng_consumer_stream
*stream
;
3318 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3319 int (*data_pending
)(struct lttng_consumer_stream
*);
3321 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3324 pthread_mutex_lock(&consumer_data
.lock
);
3326 switch (consumer_data
.type
) {
3327 case LTTNG_CONSUMER_KERNEL
:
3328 data_pending
= lttng_kconsumer_data_pending
;
3330 case LTTNG_CONSUMER32_UST
:
3331 case LTTNG_CONSUMER64_UST
:
3332 data_pending
= lttng_ustconsumer_data_pending
;
3335 ERR("Unknown consumer data type");
3339 /* Ease our life a bit */
3340 ht
= consumer_data
.stream_list_ht
;
3342 relayd
= find_relayd_by_session_id(id
);
3344 /* Send init command for data pending. */
3345 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3346 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3347 relayd
->relayd_session_id
);
3348 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3350 /* Communication error thus the relayd so no data pending. */
3351 goto data_not_pending
;
3355 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3356 ht
->hash_fct(&id
, lttng_ht_seed
),
3358 &iter
.iter
, stream
, node_session_id
.node
) {
3359 /* If this call fails, the stream is being used hence data pending. */
3360 ret
= stream_try_lock(stream
);
3366 * A removed node from the hash table indicates that the stream has
3367 * been deleted thus having a guarantee that the buffers are closed
3368 * on the consumer side. However, data can still be transmitted
3369 * over the network so don't skip the relayd check.
3371 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3373 /* Check the stream if there is data in the buffers. */
3374 ret
= data_pending(stream
);
3376 pthread_mutex_unlock(&stream
->lock
);
3383 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3384 if (stream
->metadata_flag
) {
3385 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3386 stream
->relayd_stream_id
);
3388 ret
= relayd_data_pending(&relayd
->control_sock
,
3389 stream
->relayd_stream_id
,
3390 stream
->next_net_seq_num
- 1);
3392 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3394 pthread_mutex_unlock(&stream
->lock
);
3398 pthread_mutex_unlock(&stream
->lock
);
3402 unsigned int is_data_inflight
= 0;
3404 /* Send init command for data pending. */
3405 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3406 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3407 relayd
->relayd_session_id
, &is_data_inflight
);
3408 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3410 goto data_not_pending
;
3412 if (is_data_inflight
) {
3418 * Finding _no_ node in the hash table and no inflight data means that the
3419 * stream(s) have been removed thus data is guaranteed to be available for
3420 * analysis from the trace files.
3424 /* Data is available to be read by a viewer. */
3425 pthread_mutex_unlock(&consumer_data
.lock
);
3430 /* Data is still being extracted from buffers. */
3431 pthread_mutex_unlock(&consumer_data
.lock
);
3437 * Send a ret code status message to the sessiond daemon.
3439 * Return the sendmsg() return value.
3441 int consumer_send_status_msg(int sock
, int ret_code
)
3443 struct lttcomm_consumer_status_msg msg
;
3445 msg
.ret_code
= ret_code
;
3447 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3451 * Send a channel status message to the sessiond daemon.
3453 * Return the sendmsg() return value.
3455 int consumer_send_status_channel(int sock
,
3456 struct lttng_consumer_channel
*channel
)
3458 struct lttcomm_consumer_status_channel msg
;
3463 msg
.ret_code
= -LTTNG_ERR_UST_CHAN_FAIL
;
3465 msg
.ret_code
= LTTNG_OK
;
3466 msg
.key
= channel
->key
;
3467 msg
.stream_count
= channel
->streams
.count
;
3470 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3474 * Using a maximum stream size with the produced and consumed position of a
3475 * stream, computes the new consumed position to be as close as possible to the
3476 * maximum possible stream size.
3478 * If maximum stream size is lower than the possible buffer size (produced -
3479 * consumed), the consumed_pos given is returned untouched else the new value
3482 unsigned long consumer_get_consumed_maxsize(unsigned long consumed_pos
,
3483 unsigned long produced_pos
, uint64_t max_stream_size
)
3485 if (max_stream_size
&& max_stream_size
< (produced_pos
- consumed_pos
)) {
3486 /* Offset from the produced position to get the latest buffers. */
3487 return produced_pos
- max_stream_size
;
3490 return consumed_pos
;