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
->state
= state
;
504 stream
->net_seq_idx
= relayd_id
;
505 stream
->session_id
= session_id
;
506 stream
->monitor
= monitor
;
507 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
508 pthread_mutex_init(&stream
->lock
, NULL
);
510 /* If channel is the metadata, flag this stream as metadata. */
511 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
512 stream
->metadata_flag
= 1;
513 /* Metadata is flat out. */
514 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
516 /* Format stream name to <channel_name>_<cpu_number> */
517 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
520 PERROR("snprintf stream name");
525 /* Key is always the wait_fd for streams. */
526 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
528 /* Init node per channel id key */
529 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
531 /* Init session id node with the stream session id */
532 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
534 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
535 " relayd_id %" PRIu64
", session_id %" PRIu64
,
536 stream
->name
, stream
->key
, channel_key
,
537 stream
->net_seq_idx
, stream
->session_id
);
553 * Add a stream to the global list protected by a mutex.
555 int consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
557 struct lttng_ht
*ht
= data_ht
;
563 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
565 pthread_mutex_lock(&consumer_data
.lock
);
566 pthread_mutex_lock(&stream
->chan
->lock
);
567 pthread_mutex_lock(&stream
->chan
->timer_lock
);
568 pthread_mutex_lock(&stream
->lock
);
571 /* Steal stream identifier to avoid having streams with the same key */
572 steal_stream_key(stream
->key
, ht
);
574 lttng_ht_add_unique_u64(ht
, &stream
->node
);
576 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
577 &stream
->node_channel_id
);
580 * Add stream to the stream_list_ht of the consumer data. No need to steal
581 * the key since the HT does not use it and we allow to add redundant keys
584 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
587 * When nb_init_stream_left reaches 0, we don't need to trigger any action
588 * in terms of destroying the associated channel, because the action that
589 * causes the count to become 0 also causes a stream to be added. The
590 * channel deletion will thus be triggered by the following removal of this
593 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
594 /* Increment refcount before decrementing nb_init_stream_left */
596 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
599 /* Update consumer data once the node is inserted. */
600 consumer_data
.stream_count
++;
601 consumer_data
.need_update
= 1;
604 pthread_mutex_unlock(&stream
->lock
);
605 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
606 pthread_mutex_unlock(&stream
->chan
->lock
);
607 pthread_mutex_unlock(&consumer_data
.lock
);
612 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
614 consumer_del_stream(stream
, data_ht
);
618 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
619 * be acquired before calling this.
621 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
624 struct lttng_ht_node_u64
*node
;
625 struct lttng_ht_iter iter
;
629 lttng_ht_lookup(consumer_data
.relayd_ht
,
630 &relayd
->net_seq_idx
, &iter
);
631 node
= lttng_ht_iter_get_node_u64(&iter
);
635 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
642 * Allocate and return a consumer relayd socket.
644 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
645 uint64_t net_seq_idx
)
647 struct consumer_relayd_sock_pair
*obj
= NULL
;
649 /* net sequence index of -1 is a failure */
650 if (net_seq_idx
== (uint64_t) -1ULL) {
654 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
656 PERROR("zmalloc relayd sock");
660 obj
->net_seq_idx
= net_seq_idx
;
662 obj
->destroy_flag
= 0;
663 obj
->control_sock
.sock
.fd
= -1;
664 obj
->data_sock
.sock
.fd
= -1;
665 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
666 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
673 * Find a relayd socket pair in the global consumer data.
675 * Return the object if found else NULL.
676 * RCU read-side lock must be held across this call and while using the
679 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
681 struct lttng_ht_iter iter
;
682 struct lttng_ht_node_u64
*node
;
683 struct consumer_relayd_sock_pair
*relayd
= NULL
;
685 /* Negative keys are lookup failures */
686 if (key
== (uint64_t) -1ULL) {
690 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
692 node
= lttng_ht_iter_get_node_u64(&iter
);
694 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
702 * Find a relayd and send the stream
704 * Returns 0 on success, < 0 on error
706 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
710 struct consumer_relayd_sock_pair
*relayd
;
713 assert(stream
->net_seq_idx
!= -1ULL);
716 /* The stream is not metadata. Get relayd reference if exists. */
718 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
719 if (relayd
!= NULL
) {
720 /* Add stream on the relayd */
721 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
722 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
723 path
, &stream
->relayd_stream_id
,
724 stream
->chan
->tracefile_size
, stream
->chan
->tracefile_count
);
725 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
729 uatomic_inc(&relayd
->refcount
);
730 stream
->sent_to_relayd
= 1;
732 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
733 stream
->key
, stream
->net_seq_idx
);
738 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
739 stream
->name
, stream
->key
, stream
->net_seq_idx
);
747 * Find a relayd and close the stream
749 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
751 struct consumer_relayd_sock_pair
*relayd
;
753 /* The stream is not metadata. Get relayd reference if exists. */
755 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
757 consumer_stream_relayd_close(stream
, relayd
);
763 * Handle stream for relayd transmission if the stream applies for network
764 * streaming where the net sequence index is set.
766 * Return destination file descriptor or negative value on error.
768 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
769 size_t data_size
, unsigned long padding
,
770 struct consumer_relayd_sock_pair
*relayd
)
773 struct lttcomm_relayd_data_hdr data_hdr
;
779 /* Reset data header */
780 memset(&data_hdr
, 0, sizeof(data_hdr
));
782 if (stream
->metadata_flag
) {
783 /* Caller MUST acquire the relayd control socket lock */
784 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
789 /* Metadata are always sent on the control socket. */
790 outfd
= relayd
->control_sock
.sock
.fd
;
792 /* Set header with stream information */
793 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
794 data_hdr
.data_size
= htobe32(data_size
);
795 data_hdr
.padding_size
= htobe32(padding
);
797 * Note that net_seq_num below is assigned with the *current* value of
798 * next_net_seq_num and only after that the next_net_seq_num will be
799 * increment. This is why when issuing a command on the relayd using
800 * this next value, 1 should always be substracted in order to compare
801 * the last seen sequence number on the relayd side to the last sent.
803 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
804 /* Other fields are zeroed previously */
806 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
812 ++stream
->next_net_seq_num
;
814 /* Set to go on data socket */
815 outfd
= relayd
->data_sock
.sock
.fd
;
823 * Allocate and return a new lttng_consumer_channel object using the given key
824 * to initialize the hash table node.
826 * On error, return NULL.
828 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
830 const char *pathname
,
835 enum lttng_event_output output
,
836 uint64_t tracefile_size
,
837 uint64_t tracefile_count
,
838 uint64_t session_id_per_pid
,
839 unsigned int monitor
)
841 struct lttng_consumer_channel
*channel
;
843 channel
= zmalloc(sizeof(*channel
));
844 if (channel
== NULL
) {
845 PERROR("malloc struct lttng_consumer_channel");
850 channel
->refcount
= 0;
851 channel
->session_id
= session_id
;
852 channel
->session_id_per_pid
= session_id_per_pid
;
855 channel
->relayd_id
= relayd_id
;
856 channel
->output
= output
;
857 channel
->tracefile_size
= tracefile_size
;
858 channel
->tracefile_count
= tracefile_count
;
859 channel
->monitor
= monitor
;
860 pthread_mutex_init(&channel
->lock
, NULL
);
861 pthread_mutex_init(&channel
->timer_lock
, NULL
);
864 * In monitor mode, the streams associated with the channel will be put in
865 * a special list ONLY owned by this channel. So, the refcount is set to 1
866 * here meaning that the channel itself has streams that are referenced.
868 * On a channel deletion, once the channel is no longer visible, the
869 * refcount is decremented and checked for a zero value to delete it. With
870 * streams in no monitor mode, it will now be safe to destroy the channel.
872 if (!channel
->monitor
) {
873 channel
->refcount
= 1;
876 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
877 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
879 strncpy(channel
->name
, name
, sizeof(channel
->name
));
880 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
882 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
884 channel
->wait_fd
= -1;
886 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
888 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
895 * Add a channel to the global list protected by a mutex.
897 * On success 0 is returned else a negative value.
899 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
900 struct lttng_consumer_local_data
*ctx
)
903 struct lttng_ht_node_u64
*node
;
904 struct lttng_ht_iter iter
;
906 pthread_mutex_lock(&consumer_data
.lock
);
907 pthread_mutex_lock(&channel
->lock
);
908 pthread_mutex_lock(&channel
->timer_lock
);
911 lttng_ht_lookup(consumer_data
.channel_ht
, &channel
->key
, &iter
);
912 node
= lttng_ht_iter_get_node_u64(&iter
);
914 /* Channel already exist. Ignore the insertion */
915 ERR("Consumer add channel key %" PRIu64
" already exists!",
921 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
925 pthread_mutex_unlock(&channel
->timer_lock
);
926 pthread_mutex_unlock(&channel
->lock
);
927 pthread_mutex_unlock(&consumer_data
.lock
);
929 if (!ret
&& channel
->wait_fd
!= -1 &&
930 channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
931 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
937 * Allocate the pollfd structure and the local view of the out fds to avoid
938 * doing a lookup in the linked list and concurrency issues when writing is
939 * needed. Called with consumer_data.lock held.
941 * Returns the number of fds in the structures.
943 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
944 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
948 struct lttng_ht_iter iter
;
949 struct lttng_consumer_stream
*stream
;
954 assert(local_stream
);
956 DBG("Updating poll fd array");
958 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
960 * Only active streams with an active end point can be added to the
961 * poll set and local stream storage of the thread.
963 * There is a potential race here for endpoint_status to be updated
964 * just after the check. However, this is OK since the stream(s) will
965 * be deleted once the thread is notified that the end point state has
966 * changed where this function will be called back again.
968 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
969 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
973 * This clobbers way too much the debug output. Uncomment that if you
974 * need it for debugging purposes.
976 * DBG("Active FD %d", stream->wait_fd);
978 (*pollfd
)[i
].fd
= stream
->wait_fd
;
979 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
980 local_stream
[i
] = stream
;
986 * Insert the consumer_data_pipe at the end of the array and don't
987 * increment i so nb_fd is the number of real FD.
989 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
990 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
995 * Poll on the should_quit pipe and the command socket return -1 on error and
996 * should exit, 0 if data is available on the command socket
998 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1003 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1004 if (num_rdy
== -1) {
1006 * Restart interrupted system call.
1008 if (errno
== EINTR
) {
1011 PERROR("Poll error");
1014 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1015 DBG("consumer_should_quit wake up");
1025 * Set the error socket.
1027 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1030 ctx
->consumer_error_socket
= sock
;
1034 * Set the command socket path.
1036 void lttng_consumer_set_command_sock_path(
1037 struct lttng_consumer_local_data
*ctx
, char *sock
)
1039 ctx
->consumer_command_sock_path
= sock
;
1043 * Send return code to the session daemon.
1044 * If the socket is not defined, we return 0, it is not a fatal error
1046 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1048 if (ctx
->consumer_error_socket
> 0) {
1049 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1050 sizeof(enum lttcomm_sessiond_command
));
1057 * Close all the tracefiles and stream fds and MUST be called when all
1058 * instances are destroyed i.e. when all threads were joined and are ended.
1060 void lttng_consumer_cleanup(void)
1062 struct lttng_ht_iter iter
;
1063 struct lttng_consumer_channel
*channel
;
1067 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1069 consumer_del_channel(channel
);
1074 lttng_ht_destroy(consumer_data
.channel_ht
);
1076 cleanup_relayd_ht();
1078 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1081 * This HT contains streams that are freed by either the metadata thread or
1082 * the data thread so we do *nothing* on the hash table and simply destroy
1085 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1089 * Called from signal handler.
1091 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1096 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1097 } while (ret
< 0 && errno
== EINTR
);
1098 if (ret
< 0 || ret
!= 1) {
1099 PERROR("write consumer quit");
1102 DBG("Consumer flag that it should quit");
1105 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1108 int outfd
= stream
->out_fd
;
1111 * This does a blocking write-and-wait on any page that belongs to the
1112 * subbuffer prior to the one we just wrote.
1113 * Don't care about error values, as these are just hints and ways to
1114 * limit the amount of page cache used.
1116 if (orig_offset
< stream
->max_sb_size
) {
1119 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1120 stream
->max_sb_size
,
1121 SYNC_FILE_RANGE_WAIT_BEFORE
1122 | SYNC_FILE_RANGE_WRITE
1123 | SYNC_FILE_RANGE_WAIT_AFTER
);
1125 * Give hints to the kernel about how we access the file:
1126 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1129 * We need to call fadvise again after the file grows because the
1130 * kernel does not seem to apply fadvise to non-existing parts of the
1133 * Call fadvise _after_ having waited for the page writeback to
1134 * complete because the dirty page writeback semantic is not well
1135 * defined. So it can be expected to lead to lower throughput in
1138 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1139 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1143 * Initialise the necessary environnement :
1144 * - create a new context
1145 * - create the poll_pipe
1146 * - create the should_quit pipe (for signal handler)
1147 * - create the thread pipe (for splice)
1149 * Takes a function pointer as argument, this function is called when data is
1150 * available on a buffer. This function is responsible to do the
1151 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1152 * buffer configuration and then kernctl_put_next_subbuf at the end.
1154 * Returns a pointer to the new context or NULL on error.
1156 struct lttng_consumer_local_data
*lttng_consumer_create(
1157 enum lttng_consumer_type type
,
1158 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1159 struct lttng_consumer_local_data
*ctx
),
1160 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1161 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1162 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1165 struct lttng_consumer_local_data
*ctx
;
1167 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1168 consumer_data
.type
== type
);
1169 consumer_data
.type
= type
;
1171 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1173 PERROR("allocating context");
1177 ctx
->consumer_error_socket
= -1;
1178 ctx
->consumer_metadata_socket
= -1;
1179 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1180 /* assign the callbacks */
1181 ctx
->on_buffer_ready
= buffer_ready
;
1182 ctx
->on_recv_channel
= recv_channel
;
1183 ctx
->on_recv_stream
= recv_stream
;
1184 ctx
->on_update_stream
= update_stream
;
1186 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1187 if (!ctx
->consumer_data_pipe
) {
1188 goto error_poll_pipe
;
1191 ret
= pipe(ctx
->consumer_should_quit
);
1193 PERROR("Error creating recv pipe");
1194 goto error_quit_pipe
;
1197 ret
= pipe(ctx
->consumer_thread_pipe
);
1199 PERROR("Error creating thread pipe");
1200 goto error_thread_pipe
;
1203 ret
= pipe(ctx
->consumer_channel_pipe
);
1205 PERROR("Error creating channel pipe");
1206 goto error_channel_pipe
;
1209 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1210 if (!ctx
->consumer_metadata_pipe
) {
1211 goto error_metadata_pipe
;
1214 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1216 goto error_splice_pipe
;
1222 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1223 error_metadata_pipe
:
1224 utils_close_pipe(ctx
->consumer_channel_pipe
);
1226 utils_close_pipe(ctx
->consumer_thread_pipe
);
1228 utils_close_pipe(ctx
->consumer_should_quit
);
1230 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1238 * Close all fds associated with the instance and free the context.
1240 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1244 DBG("Consumer destroying it. Closing everything.");
1246 ret
= close(ctx
->consumer_error_socket
);
1250 ret
= close(ctx
->consumer_metadata_socket
);
1254 utils_close_pipe(ctx
->consumer_thread_pipe
);
1255 utils_close_pipe(ctx
->consumer_channel_pipe
);
1256 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1257 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1258 utils_close_pipe(ctx
->consumer_should_quit
);
1259 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1261 unlink(ctx
->consumer_command_sock_path
);
1266 * Write the metadata stream id on the specified file descriptor.
1268 static int write_relayd_metadata_id(int fd
,
1269 struct lttng_consumer_stream
*stream
,
1270 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1273 struct lttcomm_relayd_metadata_payload hdr
;
1275 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1276 hdr
.padding_size
= htobe32(padding
);
1278 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1279 } while (ret
< 0 && errno
== EINTR
);
1280 if (ret
< 0 || ret
!= sizeof(hdr
)) {
1282 * This error means that the fd's end is closed so ignore the perror
1283 * not to clubber the error output since this can happen in a normal
1286 if (errno
!= EPIPE
) {
1287 PERROR("write metadata stream id");
1289 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1291 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1292 * handle writting the missing part so report that as an error and
1293 * don't lie to the caller.
1298 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1299 stream
->relayd_stream_id
, padding
);
1306 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1307 * core function for writing trace buffers to either the local filesystem or
1310 * It must be called with the stream lock held.
1312 * Careful review MUST be put if any changes occur!
1314 * Returns the number of bytes written
1316 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1317 struct lttng_consumer_local_data
*ctx
,
1318 struct lttng_consumer_stream
*stream
, unsigned long len
,
1319 unsigned long padding
)
1321 unsigned long mmap_offset
;
1323 ssize_t ret
= 0, written
= 0;
1324 off_t orig_offset
= stream
->out_fd_offset
;
1325 /* Default is on the disk */
1326 int outfd
= stream
->out_fd
;
1327 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1328 unsigned int relayd_hang_up
= 0;
1330 /* RCU lock for the relayd pointer */
1333 /* Flag that the current stream if set for network streaming. */
1334 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1335 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1336 if (relayd
== NULL
) {
1342 /* get the offset inside the fd to mmap */
1343 switch (consumer_data
.type
) {
1344 case LTTNG_CONSUMER_KERNEL
:
1345 mmap_base
= stream
->mmap_base
;
1346 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1348 PERROR("tracer ctl get_mmap_read_offset");
1353 case LTTNG_CONSUMER32_UST
:
1354 case LTTNG_CONSUMER64_UST
:
1355 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1357 ERR("read mmap get mmap base for stream %s", stream
->name
);
1361 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1363 PERROR("tracer ctl get_mmap_read_offset");
1369 ERR("Unknown consumer_data type");
1373 /* Handle stream on the relayd if the output is on the network */
1375 unsigned long netlen
= len
;
1378 * Lock the control socket for the complete duration of the function
1379 * since from this point on we will use the socket.
1381 if (stream
->metadata_flag
) {
1382 /* Metadata requires the control socket. */
1383 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1384 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1387 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1389 /* Use the returned socket. */
1392 /* Write metadata stream id before payload */
1393 if (stream
->metadata_flag
) {
1394 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1397 /* Socket operation failed. We consider the relayd dead */
1398 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1406 /* Socket operation failed. We consider the relayd dead */
1407 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1411 /* Else, use the default set before which is the filesystem. */
1414 /* No streaming, we have to set the len with the full padding */
1418 * Check if we need to change the tracefile before writing the packet.
1420 if (stream
->chan
->tracefile_size
> 0 &&
1421 (stream
->tracefile_size_current
+ len
) >
1422 stream
->chan
->tracefile_size
) {
1423 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1424 stream
->name
, stream
->chan
->tracefile_size
,
1425 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1426 stream
->out_fd
, &(stream
->tracefile_count_current
));
1428 ERR("Rotating output file");
1431 outfd
= stream
->out_fd
= ret
;
1432 /* Reset current size because we just perform a rotation. */
1433 stream
->tracefile_size_current
= 0;
1435 stream
->tracefile_size_current
+= len
;
1440 ret
= write(outfd
, mmap_base
+ mmap_offset
, len
);
1441 } while (ret
< 0 && errno
== EINTR
);
1442 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1445 * This is possible if the fd is closed on the other side (outfd)
1446 * or any write problem. It can be verbose a bit for a normal
1447 * execution if for instance the relayd is stopped abruptly. This
1448 * can happen so set this to a DBG statement.
1450 DBG("Error in file write mmap");
1454 /* Socket operation failed. We consider the relayd dead */
1455 if (errno
== EPIPE
|| errno
== EINVAL
) {
1460 } else if (ret
> len
) {
1461 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1469 /* This call is useless on a socket so better save a syscall. */
1471 /* This won't block, but will start writeout asynchronously */
1472 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1473 SYNC_FILE_RANGE_WRITE
);
1474 stream
->out_fd_offset
+= ret
;
1478 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1482 * This is a special case that the relayd has closed its socket. Let's
1483 * cleanup the relayd object and all associated streams.
1485 if (relayd
&& relayd_hang_up
) {
1486 cleanup_relayd(relayd
, ctx
);
1490 /* Unlock only if ctrl socket used */
1491 if (relayd
&& stream
->metadata_flag
) {
1492 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1500 * Splice the data from the ring buffer to the tracefile.
1502 * It must be called with the stream lock held.
1504 * Returns the number of bytes spliced.
1506 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1507 struct lttng_consumer_local_data
*ctx
,
1508 struct lttng_consumer_stream
*stream
, unsigned long len
,
1509 unsigned long padding
)
1511 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1513 off_t orig_offset
= stream
->out_fd_offset
;
1514 int fd
= stream
->wait_fd
;
1515 /* Default is on the disk */
1516 int outfd
= stream
->out_fd
;
1517 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1519 unsigned int relayd_hang_up
= 0;
1521 switch (consumer_data
.type
) {
1522 case LTTNG_CONSUMER_KERNEL
:
1524 case LTTNG_CONSUMER32_UST
:
1525 case LTTNG_CONSUMER64_UST
:
1526 /* Not supported for user space tracing */
1529 ERR("Unknown consumer_data type");
1533 /* RCU lock for the relayd pointer */
1536 /* Flag that the current stream if set for network streaming. */
1537 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1538 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1539 if (relayd
== NULL
) {
1546 * Choose right pipe for splice. Metadata and trace data are handled by
1547 * different threads hence the use of two pipes in order not to race or
1548 * corrupt the written data.
1550 if (stream
->metadata_flag
) {
1551 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1553 splice_pipe
= ctx
->consumer_thread_pipe
;
1556 /* Write metadata stream id before payload */
1558 int total_len
= len
;
1560 if (stream
->metadata_flag
) {
1562 * Lock the control socket for the complete duration of the function
1563 * since from this point on we will use the socket.
1565 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1567 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1571 /* Socket operation failed. We consider the relayd dead */
1572 if (ret
== -EBADF
) {
1573 WARN("Remote relayd disconnected. Stopping");
1580 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1583 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1585 /* Use the returned socket. */
1588 /* Socket operation failed. We consider the relayd dead */
1589 if (ret
== -EBADF
) {
1590 WARN("Remote relayd disconnected. Stopping");
1597 /* No streaming, we have to set the len with the full padding */
1601 * Check if we need to change the tracefile before writing the packet.
1603 if (stream
->chan
->tracefile_size
> 0 &&
1604 (stream
->tracefile_size_current
+ len
) >
1605 stream
->chan
->tracefile_size
) {
1606 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1607 stream
->name
, stream
->chan
->tracefile_size
,
1608 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1609 stream
->out_fd
, &(stream
->tracefile_count_current
));
1611 ERR("Rotating output file");
1614 outfd
= stream
->out_fd
= ret
;
1615 /* Reset current size because we just perform a rotation. */
1616 stream
->tracefile_size_current
= 0;
1618 stream
->tracefile_size_current
+= len
;
1622 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1623 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1624 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1625 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1626 DBG("splice chan to pipe, ret %zd", ret_splice
);
1627 if (ret_splice
< 0) {
1628 PERROR("Error in relay splice");
1630 written
= ret_splice
;
1636 /* Handle stream on the relayd if the output is on the network */
1638 if (stream
->metadata_flag
) {
1639 size_t metadata_payload_size
=
1640 sizeof(struct lttcomm_relayd_metadata_payload
);
1642 /* Update counter to fit the spliced data */
1643 ret_splice
+= metadata_payload_size
;
1644 len
+= metadata_payload_size
;
1646 * We do this so the return value can match the len passed as
1647 * argument to this function.
1649 written
-= metadata_payload_size
;
1653 /* Splice data out */
1654 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1655 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1656 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1657 if (ret_splice
< 0) {
1658 PERROR("Error in file splice");
1660 written
= ret_splice
;
1662 /* Socket operation failed. We consider the relayd dead */
1663 if (errno
== EBADF
|| errno
== EPIPE
) {
1664 WARN("Remote relayd disconnected. Stopping");
1670 } else if (ret_splice
> len
) {
1672 PERROR("Wrote more data than requested %zd (len: %lu)",
1674 written
+= ret_splice
;
1680 /* This call is useless on a socket so better save a syscall. */
1682 /* This won't block, but will start writeout asynchronously */
1683 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1684 SYNC_FILE_RANGE_WRITE
);
1685 stream
->out_fd_offset
+= ret_splice
;
1687 written
+= ret_splice
;
1689 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1697 * This is a special case that the relayd has closed its socket. Let's
1698 * cleanup the relayd object and all associated streams.
1700 if (relayd
&& relayd_hang_up
) {
1701 cleanup_relayd(relayd
, ctx
);
1702 /* Skip splice error so the consumer does not fail */
1707 /* send the appropriate error description to sessiond */
1710 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1713 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1716 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1721 if (relayd
&& stream
->metadata_flag
) {
1722 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1730 * Take a snapshot for a specific fd
1732 * Returns 0 on success, < 0 on error
1734 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1736 switch (consumer_data
.type
) {
1737 case LTTNG_CONSUMER_KERNEL
:
1738 return lttng_kconsumer_take_snapshot(stream
);
1739 case LTTNG_CONSUMER32_UST
:
1740 case LTTNG_CONSUMER64_UST
:
1741 return lttng_ustconsumer_take_snapshot(stream
);
1743 ERR("Unknown consumer_data type");
1750 * Get the produced position
1752 * Returns 0 on success, < 0 on error
1754 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1757 switch (consumer_data
.type
) {
1758 case LTTNG_CONSUMER_KERNEL
:
1759 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1760 case LTTNG_CONSUMER32_UST
:
1761 case LTTNG_CONSUMER64_UST
:
1762 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1764 ERR("Unknown consumer_data type");
1770 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1771 int sock
, struct pollfd
*consumer_sockpoll
)
1773 switch (consumer_data
.type
) {
1774 case LTTNG_CONSUMER_KERNEL
:
1775 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1776 case LTTNG_CONSUMER32_UST
:
1777 case LTTNG_CONSUMER64_UST
:
1778 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1780 ERR("Unknown consumer_data type");
1787 * Iterate over all streams of the hashtable and free them properly.
1789 * WARNING: *MUST* be used with data stream only.
1791 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1793 struct lttng_ht_iter iter
;
1794 struct lttng_consumer_stream
*stream
;
1801 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1803 * Ignore return value since we are currently cleaning up so any error
1806 (void) consumer_del_stream(stream
, ht
);
1810 lttng_ht_destroy(ht
);
1814 * Iterate over all streams of the hashtable and free them properly.
1816 * XXX: Should not be only for metadata stream or else use an other name.
1818 static void destroy_stream_ht(struct lttng_ht
*ht
)
1820 struct lttng_ht_iter iter
;
1821 struct lttng_consumer_stream
*stream
;
1828 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1830 * Ignore return value since we are currently cleaning up so any error
1833 (void) consumer_del_metadata_stream(stream
, ht
);
1837 lttng_ht_destroy(ht
);
1840 void lttng_consumer_close_metadata(void)
1842 switch (consumer_data
.type
) {
1843 case LTTNG_CONSUMER_KERNEL
:
1845 * The Kernel consumer has a different metadata scheme so we don't
1846 * close anything because the stream will be closed by the session
1850 case LTTNG_CONSUMER32_UST
:
1851 case LTTNG_CONSUMER64_UST
:
1853 * Close all metadata streams. The metadata hash table is passed and
1854 * this call iterates over it by closing all wakeup fd. This is safe
1855 * because at this point we are sure that the metadata producer is
1856 * either dead or blocked.
1858 lttng_ustconsumer_close_metadata(metadata_ht
);
1861 ERR("Unknown consumer_data type");
1867 * Clean up a metadata stream and free its memory.
1869 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1870 struct lttng_ht
*ht
)
1873 struct lttng_ht_iter iter
;
1874 struct lttng_consumer_channel
*free_chan
= NULL
;
1875 struct consumer_relayd_sock_pair
*relayd
;
1879 * This call should NEVER receive regular stream. It must always be
1880 * metadata stream and this is crucial for data structure synchronization.
1882 assert(stream
->metadata_flag
);
1884 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1887 /* Means the stream was allocated but not successfully added */
1888 goto free_stream_rcu
;
1891 pthread_mutex_lock(&consumer_data
.lock
);
1892 pthread_mutex_lock(&stream
->chan
->lock
);
1893 pthread_mutex_lock(&stream
->lock
);
1895 switch (consumer_data
.type
) {
1896 case LTTNG_CONSUMER_KERNEL
:
1897 if (stream
->mmap_base
!= NULL
) {
1898 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1900 PERROR("munmap metadata stream");
1903 if (stream
->wait_fd
>= 0) {
1904 ret
= close(stream
->wait_fd
);
1906 PERROR("close kernel metadata wait_fd");
1910 case LTTNG_CONSUMER32_UST
:
1911 case LTTNG_CONSUMER64_UST
:
1912 if (stream
->monitor
) {
1913 /* close the write-side in close_metadata */
1914 ret
= close(stream
->ust_metadata_poll_pipe
[0]);
1916 PERROR("Close UST metadata read-side poll pipe");
1919 lttng_ustconsumer_del_stream(stream
);
1922 ERR("Unknown consumer_data type");
1928 iter
.iter
.node
= &stream
->node
.node
;
1929 ret
= lttng_ht_del(ht
, &iter
);
1932 iter
.iter
.node
= &stream
->node_channel_id
.node
;
1933 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
1936 iter
.iter
.node
= &stream
->node_session_id
.node
;
1937 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1941 if (stream
->out_fd
>= 0) {
1942 ret
= close(stream
->out_fd
);
1948 /* Check and cleanup relayd */
1950 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1951 if (relayd
!= NULL
) {
1952 uatomic_dec(&relayd
->refcount
);
1953 assert(uatomic_read(&relayd
->refcount
) >= 0);
1955 /* Closing streams requires to lock the control socket. */
1956 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1957 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1958 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1959 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1961 DBG("Unable to close stream on the relayd. Continuing");
1963 * Continue here. There is nothing we can do for the relayd.
1964 * Chances are that the relayd has closed the socket so we just
1965 * continue cleaning up.
1969 /* Both conditions are met, we destroy the relayd. */
1970 if (uatomic_read(&relayd
->refcount
) == 0 &&
1971 uatomic_read(&relayd
->destroy_flag
)) {
1972 consumer_destroy_relayd(relayd
);
1977 /* Atomically decrement channel refcount since other threads can use it. */
1978 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
1979 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
1980 /* Go for channel deletion! */
1981 free_chan
= stream
->chan
;
1986 * Nullify the stream reference so it is not used after deletion. The
1987 * channel lock MUST be acquired before being able to check for
1988 * a NULL pointer value.
1990 stream
->chan
->metadata_stream
= NULL
;
1992 pthread_mutex_unlock(&stream
->lock
);
1993 pthread_mutex_unlock(&stream
->chan
->lock
);
1994 pthread_mutex_unlock(&consumer_data
.lock
);
1997 consumer_del_channel(free_chan
);
2001 call_rcu(&stream
->node
.head
, free_stream_rcu
);
2005 * Action done with the metadata stream when adding it to the consumer internal
2006 * data structures to handle it.
2008 int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2010 struct lttng_ht
*ht
= metadata_ht
;
2012 struct lttng_ht_iter iter
;
2013 struct lttng_ht_node_u64
*node
;
2018 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2020 pthread_mutex_lock(&consumer_data
.lock
);
2021 pthread_mutex_lock(&stream
->chan
->lock
);
2022 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2023 pthread_mutex_lock(&stream
->lock
);
2026 * From here, refcounts are updated so be _careful_ when returning an error
2033 * Lookup the stream just to make sure it does not exist in our internal
2034 * state. This should NEVER happen.
2036 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2037 node
= lttng_ht_iter_get_node_u64(&iter
);
2041 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2042 * in terms of destroying the associated channel, because the action that
2043 * causes the count to become 0 also causes a stream to be added. The
2044 * channel deletion will thus be triggered by the following removal of this
2047 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2048 /* Increment refcount before decrementing nb_init_stream_left */
2050 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2053 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2055 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2056 &stream
->node_channel_id
);
2059 * Add stream to the stream_list_ht of the consumer data. No need to steal
2060 * the key since the HT does not use it and we allow to add redundant keys
2063 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2067 pthread_mutex_unlock(&stream
->lock
);
2068 pthread_mutex_unlock(&stream
->chan
->lock
);
2069 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2070 pthread_mutex_unlock(&consumer_data
.lock
);
2075 * Delete data stream that are flagged for deletion (endpoint_status).
2077 static void validate_endpoint_status_data_stream(void)
2079 struct lttng_ht_iter iter
;
2080 struct lttng_consumer_stream
*stream
;
2082 DBG("Consumer delete flagged data stream");
2085 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2086 /* Validate delete flag of the stream */
2087 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2090 /* Delete it right now */
2091 consumer_del_stream(stream
, data_ht
);
2097 * Delete metadata stream that are flagged for deletion (endpoint_status).
2099 static void validate_endpoint_status_metadata_stream(
2100 struct lttng_poll_event
*pollset
)
2102 struct lttng_ht_iter iter
;
2103 struct lttng_consumer_stream
*stream
;
2105 DBG("Consumer delete flagged metadata stream");
2110 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2111 /* Validate delete flag of the stream */
2112 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2116 * Remove from pollset so the metadata thread can continue without
2117 * blocking on a deleted stream.
2119 lttng_poll_del(pollset
, stream
->wait_fd
);
2121 /* Delete it right now */
2122 consumer_del_metadata_stream(stream
, metadata_ht
);
2128 * Thread polls on metadata file descriptor and write them on disk or on the
2131 void *consumer_thread_metadata_poll(void *data
)
2134 uint32_t revents
, nb_fd
;
2135 struct lttng_consumer_stream
*stream
= NULL
;
2136 struct lttng_ht_iter iter
;
2137 struct lttng_ht_node_u64
*node
;
2138 struct lttng_poll_event events
;
2139 struct lttng_consumer_local_data
*ctx
= data
;
2142 rcu_register_thread();
2144 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2146 /* ENOMEM at this point. Better to bail out. */
2150 DBG("Thread metadata poll started");
2152 /* Size is set to 1 for the consumer_metadata pipe */
2153 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2155 ERR("Poll set creation failed");
2159 ret
= lttng_poll_add(&events
,
2160 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2166 DBG("Metadata main loop started");
2169 /* Only the metadata pipe is set */
2170 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2175 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2176 ret
= lttng_poll_wait(&events
, -1);
2177 DBG("Metadata event catched in thread");
2179 if (errno
== EINTR
) {
2180 ERR("Poll EINTR catched");
2188 /* From here, the event is a metadata wait fd */
2189 for (i
= 0; i
< nb_fd
; i
++) {
2190 revents
= LTTNG_POLL_GETEV(&events
, i
);
2191 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2193 /* Just don't waste time if no returned events for the fd */
2198 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2199 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2200 DBG("Metadata thread pipe hung up");
2202 * Remove the pipe from the poll set and continue the loop
2203 * since their might be data to consume.
2205 lttng_poll_del(&events
,
2206 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2207 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2209 } else if (revents
& LPOLLIN
) {
2212 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2213 &stream
, sizeof(stream
));
2215 ERR("read metadata stream, ret: %zd", pipe_len
);
2217 * Continue here to handle the rest of the streams.
2222 /* A NULL stream means that the state has changed. */
2223 if (stream
== NULL
) {
2224 /* Check for deleted streams. */
2225 validate_endpoint_status_metadata_stream(&events
);
2229 DBG("Adding metadata stream %d to poll set",
2232 /* Add metadata stream to the global poll events list */
2233 lttng_poll_add(&events
, stream
->wait_fd
,
2234 LPOLLIN
| LPOLLPRI
);
2237 /* Handle other stream */
2243 uint64_t tmp_id
= (uint64_t) pollfd
;
2245 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2247 node
= lttng_ht_iter_get_node_u64(&iter
);
2250 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2253 /* Check for error event */
2254 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2255 DBG("Metadata fd %d is hup|err.", pollfd
);
2256 if (!stream
->hangup_flush_done
2257 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2258 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2259 DBG("Attempting to flush and consume the UST buffers");
2260 lttng_ustconsumer_on_stream_hangup(stream
);
2262 /* We just flushed the stream now read it. */
2264 len
= ctx
->on_buffer_ready(stream
, ctx
);
2266 * We don't check the return value here since if we get
2267 * a negative len, it means an error occured thus we
2268 * simply remove it from the poll set and free the
2274 lttng_poll_del(&events
, stream
->wait_fd
);
2276 * This call update the channel states, closes file descriptors
2277 * and securely free the stream.
2279 consumer_del_metadata_stream(stream
, metadata_ht
);
2280 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2281 /* Get the data out of the metadata file descriptor */
2282 DBG("Metadata available on fd %d", pollfd
);
2283 assert(stream
->wait_fd
== pollfd
);
2286 len
= ctx
->on_buffer_ready(stream
, ctx
);
2288 * We don't check the return value here since if we get
2289 * a negative len, it means an error occured thus we
2290 * simply remove it from the poll set and free the
2295 /* It's ok to have an unavailable sub-buffer */
2296 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2297 /* Clean up stream from consumer and free it. */
2298 lttng_poll_del(&events
, stream
->wait_fd
);
2299 consumer_del_metadata_stream(stream
, metadata_ht
);
2303 /* Release RCU lock for the stream looked up */
2310 DBG("Metadata poll thread exiting");
2312 lttng_poll_clean(&events
);
2314 destroy_stream_ht(metadata_ht
);
2316 rcu_unregister_thread();
2321 * This thread polls the fds in the set to consume the data and write
2322 * it to tracefile if necessary.
2324 void *consumer_thread_data_poll(void *data
)
2326 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2327 struct pollfd
*pollfd
= NULL
;
2328 /* local view of the streams */
2329 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2330 /* local view of consumer_data.fds_count */
2332 struct lttng_consumer_local_data
*ctx
= data
;
2335 rcu_register_thread();
2337 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2338 if (data_ht
== NULL
) {
2339 /* ENOMEM at this point. Better to bail out. */
2343 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2344 if (local_stream
== NULL
) {
2345 PERROR("local_stream malloc");
2354 * the fds set has been updated, we need to update our
2355 * local array as well
2357 pthread_mutex_lock(&consumer_data
.lock
);
2358 if (consumer_data
.need_update
) {
2363 local_stream
= NULL
;
2365 /* allocate for all fds + 1 for the consumer_data_pipe */
2366 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2367 if (pollfd
== NULL
) {
2368 PERROR("pollfd malloc");
2369 pthread_mutex_unlock(&consumer_data
.lock
);
2373 /* allocate for all fds + 1 for the consumer_data_pipe */
2374 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2375 sizeof(struct lttng_consumer_stream
*));
2376 if (local_stream
== NULL
) {
2377 PERROR("local_stream malloc");
2378 pthread_mutex_unlock(&consumer_data
.lock
);
2381 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2384 ERR("Error in allocating pollfd or local_outfds");
2385 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2386 pthread_mutex_unlock(&consumer_data
.lock
);
2390 consumer_data
.need_update
= 0;
2392 pthread_mutex_unlock(&consumer_data
.lock
);
2394 /* No FDs and consumer_quit, consumer_cleanup the thread */
2395 if (nb_fd
== 0 && consumer_quit
== 1) {
2398 /* poll on the array of fds */
2400 DBG("polling on %d fd", nb_fd
+ 1);
2401 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2402 DBG("poll num_rdy : %d", num_rdy
);
2403 if (num_rdy
== -1) {
2405 * Restart interrupted system call.
2407 if (errno
== EINTR
) {
2410 PERROR("Poll error");
2411 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2413 } else if (num_rdy
== 0) {
2414 DBG("Polling thread timed out");
2419 * If the consumer_data_pipe triggered poll go directly to the
2420 * beginning of the loop to update the array. We want to prioritize
2421 * array update over low-priority reads.
2423 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2424 ssize_t pipe_readlen
;
2426 DBG("consumer_data_pipe wake up");
2427 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2428 &new_stream
, sizeof(new_stream
));
2429 if (pipe_readlen
< 0) {
2430 ERR("Consumer data pipe ret %zd", pipe_readlen
);
2431 /* Continue so we can at least handle the current stream(s). */
2436 * If the stream is NULL, just ignore it. It's also possible that
2437 * the sessiond poll thread changed the consumer_quit state and is
2438 * waking us up to test it.
2440 if (new_stream
== NULL
) {
2441 validate_endpoint_status_data_stream();
2445 /* Continue to update the local streams and handle prio ones */
2449 /* Take care of high priority channels first. */
2450 for (i
= 0; i
< nb_fd
; i
++) {
2451 if (local_stream
[i
] == NULL
) {
2454 if (pollfd
[i
].revents
& POLLPRI
) {
2455 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2457 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2458 /* it's ok to have an unavailable sub-buffer */
2459 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2460 /* Clean the stream and free it. */
2461 consumer_del_stream(local_stream
[i
], data_ht
);
2462 local_stream
[i
] = NULL
;
2463 } else if (len
> 0) {
2464 local_stream
[i
]->data_read
= 1;
2470 * If we read high prio channel in this loop, try again
2471 * for more high prio data.
2477 /* Take care of low priority channels. */
2478 for (i
= 0; i
< nb_fd
; i
++) {
2479 if (local_stream
[i
] == NULL
) {
2482 if ((pollfd
[i
].revents
& POLLIN
) ||
2483 local_stream
[i
]->hangup_flush_done
) {
2484 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2485 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2486 /* it's ok to have an unavailable sub-buffer */
2487 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2488 /* Clean the stream and free it. */
2489 consumer_del_stream(local_stream
[i
], data_ht
);
2490 local_stream
[i
] = NULL
;
2491 } else if (len
> 0) {
2492 local_stream
[i
]->data_read
= 1;
2497 /* Handle hangup and errors */
2498 for (i
= 0; i
< nb_fd
; i
++) {
2499 if (local_stream
[i
] == NULL
) {
2502 if (!local_stream
[i
]->hangup_flush_done
2503 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2504 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2505 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2506 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2508 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2509 /* Attempt read again, for the data we just flushed. */
2510 local_stream
[i
]->data_read
= 1;
2513 * If the poll flag is HUP/ERR/NVAL and we have
2514 * read no data in this pass, we can remove the
2515 * stream from its hash table.
2517 if ((pollfd
[i
].revents
& POLLHUP
)) {
2518 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2519 if (!local_stream
[i
]->data_read
) {
2520 consumer_del_stream(local_stream
[i
], data_ht
);
2521 local_stream
[i
] = NULL
;
2524 } else if (pollfd
[i
].revents
& POLLERR
) {
2525 ERR("Error returned in polling fd %d.", 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
& POLLNVAL
) {
2532 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2533 if (!local_stream
[i
]->data_read
) {
2534 consumer_del_stream(local_stream
[i
], data_ht
);
2535 local_stream
[i
] = NULL
;
2539 if (local_stream
[i
] != NULL
) {
2540 local_stream
[i
]->data_read
= 0;
2545 DBG("polling thread exiting");
2550 * Close the write side of the pipe so epoll_wait() in
2551 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2552 * read side of the pipe. If we close them both, epoll_wait strangely does
2553 * not return and could create a endless wait period if the pipe is the
2554 * only tracked fd in the poll set. The thread will take care of closing
2557 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2559 destroy_data_stream_ht(data_ht
);
2561 rcu_unregister_thread();
2566 * Close wake-up end of each stream belonging to the channel. This will
2567 * allow the poll() on the stream read-side to detect when the
2568 * write-side (application) finally closes them.
2571 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2573 struct lttng_ht
*ht
;
2574 struct lttng_consumer_stream
*stream
;
2575 struct lttng_ht_iter iter
;
2577 ht
= consumer_data
.stream_per_chan_id_ht
;
2580 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2581 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2582 ht
->match_fct
, &channel
->key
,
2583 &iter
.iter
, stream
, node_channel_id
.node
) {
2585 * Protect against teardown with mutex.
2587 pthread_mutex_lock(&stream
->lock
);
2588 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2591 switch (consumer_data
.type
) {
2592 case LTTNG_CONSUMER_KERNEL
:
2594 case LTTNG_CONSUMER32_UST
:
2595 case LTTNG_CONSUMER64_UST
:
2597 * Note: a mutex is taken internally within
2598 * liblttng-ust-ctl to protect timer wakeup_fd
2599 * use from concurrent close.
2601 lttng_ustconsumer_close_stream_wakeup(stream
);
2604 ERR("Unknown consumer_data type");
2608 pthread_mutex_unlock(&stream
->lock
);
2613 static void destroy_channel_ht(struct lttng_ht
*ht
)
2615 struct lttng_ht_iter iter
;
2616 struct lttng_consumer_channel
*channel
;
2624 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2625 ret
= lttng_ht_del(ht
, &iter
);
2630 lttng_ht_destroy(ht
);
2634 * This thread polls the channel fds to detect when they are being
2635 * closed. It closes all related streams if the channel is detected as
2636 * closed. It is currently only used as a shim layer for UST because the
2637 * consumerd needs to keep the per-stream wakeup end of pipes open for
2640 void *consumer_thread_channel_poll(void *data
)
2643 uint32_t revents
, nb_fd
;
2644 struct lttng_consumer_channel
*chan
= NULL
;
2645 struct lttng_ht_iter iter
;
2646 struct lttng_ht_node_u64
*node
;
2647 struct lttng_poll_event events
;
2648 struct lttng_consumer_local_data
*ctx
= data
;
2649 struct lttng_ht
*channel_ht
;
2651 rcu_register_thread();
2653 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2655 /* ENOMEM at this point. Better to bail out. */
2659 DBG("Thread channel poll started");
2661 /* Size is set to 1 for the consumer_channel pipe */
2662 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2664 ERR("Poll set creation failed");
2668 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2674 DBG("Channel main loop started");
2677 /* Only the channel pipe is set */
2678 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2683 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2684 ret
= lttng_poll_wait(&events
, -1);
2685 DBG("Channel event catched in thread");
2687 if (errno
== EINTR
) {
2688 ERR("Poll EINTR catched");
2696 /* From here, the event is a channel wait fd */
2697 for (i
= 0; i
< nb_fd
; i
++) {
2698 revents
= LTTNG_POLL_GETEV(&events
, i
);
2699 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2701 /* Just don't waste time if no returned events for the fd */
2705 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2706 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2707 DBG("Channel thread pipe hung up");
2709 * Remove the pipe from the poll set and continue the loop
2710 * since their might be data to consume.
2712 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2714 } else if (revents
& LPOLLIN
) {
2715 enum consumer_channel_action action
;
2718 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2720 ERR("Error reading channel pipe");
2725 case CONSUMER_CHANNEL_ADD
:
2726 DBG("Adding channel %d to poll set",
2729 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2732 lttng_ht_add_unique_u64(channel_ht
,
2733 &chan
->wait_fd_node
);
2735 /* Add channel to the global poll events list */
2736 lttng_poll_add(&events
, chan
->wait_fd
,
2737 LPOLLIN
| LPOLLPRI
);
2739 case CONSUMER_CHANNEL_DEL
:
2741 struct lttng_consumer_stream
*stream
, *stmp
;
2744 chan
= consumer_find_channel(key
);
2747 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2750 lttng_poll_del(&events
, chan
->wait_fd
);
2751 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2752 ret
= lttng_ht_del(channel_ht
, &iter
);
2754 consumer_close_channel_streams(chan
);
2756 switch (consumer_data
.type
) {
2757 case LTTNG_CONSUMER_KERNEL
:
2759 case LTTNG_CONSUMER32_UST
:
2760 case LTTNG_CONSUMER64_UST
:
2761 /* Delete streams that might have been left in the stream list. */
2762 cds_list_for_each_entry_safe(stream
, stmp
, &chan
->streams
.head
,
2764 cds_list_del(&stream
->send_node
);
2765 lttng_ustconsumer_del_stream(stream
);
2766 uatomic_sub(&stream
->chan
->refcount
, 1);
2767 assert(&chan
->refcount
);
2772 ERR("Unknown consumer_data type");
2777 * Release our own refcount. Force channel deletion even if
2778 * streams were not initialized.
2780 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2781 consumer_del_channel(chan
);
2786 case CONSUMER_CHANNEL_QUIT
:
2788 * Remove the pipe from the poll set and continue the loop
2789 * since their might be data to consume.
2791 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2794 ERR("Unknown action");
2799 /* Handle other stream */
2805 uint64_t tmp_id
= (uint64_t) pollfd
;
2807 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2809 node
= lttng_ht_iter_get_node_u64(&iter
);
2812 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2815 /* Check for error event */
2816 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2817 DBG("Channel fd %d is hup|err.", pollfd
);
2819 lttng_poll_del(&events
, chan
->wait_fd
);
2820 ret
= lttng_ht_del(channel_ht
, &iter
);
2822 consumer_close_channel_streams(chan
);
2824 /* Release our own refcount */
2825 if (!uatomic_sub_return(&chan
->refcount
, 1)
2826 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2827 consumer_del_channel(chan
);
2831 /* Release RCU lock for the channel looked up */
2837 lttng_poll_clean(&events
);
2839 destroy_channel_ht(channel_ht
);
2841 DBG("Channel poll thread exiting");
2842 rcu_unregister_thread();
2846 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2847 struct pollfd
*sockpoll
, int client_socket
)
2854 if (lttng_consumer_poll_socket(sockpoll
) < 0) {
2858 DBG("Metadata connection on client_socket");
2860 /* Blocking call, waiting for transmission */
2861 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2862 if (ctx
->consumer_metadata_socket
< 0) {
2863 WARN("On accept metadata");
2874 * This thread listens on the consumerd socket and receives the file
2875 * descriptors from the session daemon.
2877 void *consumer_thread_sessiond_poll(void *data
)
2879 int sock
= -1, client_socket
, ret
;
2881 * structure to poll for incoming data on communication socket avoids
2882 * making blocking sockets.
2884 struct pollfd consumer_sockpoll
[2];
2885 struct lttng_consumer_local_data
*ctx
= data
;
2887 rcu_register_thread();
2889 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2890 unlink(ctx
->consumer_command_sock_path
);
2891 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2892 if (client_socket
< 0) {
2893 ERR("Cannot create command socket");
2897 ret
= lttcomm_listen_unix_sock(client_socket
);
2902 DBG("Sending ready command to lttng-sessiond");
2903 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2904 /* return < 0 on error, but == 0 is not fatal */
2906 ERR("Error sending ready command to lttng-sessiond");
2910 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2911 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2912 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2913 consumer_sockpoll
[1].fd
= client_socket
;
2914 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2916 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2919 DBG("Connection on client_socket");
2921 /* Blocking call, waiting for transmission */
2922 sock
= lttcomm_accept_unix_sock(client_socket
);
2929 * Setup metadata socket which is the second socket connection on the
2930 * command unix socket.
2932 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
2937 /* This socket is not useful anymore. */
2938 ret
= close(client_socket
);
2940 PERROR("close client_socket");
2944 /* update the polling structure to poll on the established socket */
2945 consumer_sockpoll
[1].fd
= sock
;
2946 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2949 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2952 DBG("Incoming command on sock");
2953 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2954 if (ret
== -ENOENT
) {
2955 DBG("Received STOP command");
2960 * This could simply be a session daemon quitting. Don't output
2963 DBG("Communication interrupted on command socket");
2966 if (consumer_quit
) {
2967 DBG("consumer_thread_receive_fds received quit from signal");
2970 DBG("received command on sock");
2973 DBG("Consumer thread sessiond poll exiting");
2976 * Close metadata streams since the producer is the session daemon which
2979 * NOTE: for now, this only applies to the UST tracer.
2981 lttng_consumer_close_metadata();
2984 * when all fds have hung up, the polling thread
2990 * Notify the data poll thread to poll back again and test the
2991 * consumer_quit state that we just set so to quit gracefully.
2993 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
2995 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
2997 /* Cleaning up possibly open sockets. */
3001 PERROR("close sock sessiond poll");
3004 if (client_socket
>= 0) {
3005 ret
= close(client_socket
);
3007 PERROR("close client_socket sessiond poll");
3011 rcu_unregister_thread();
3015 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3016 struct lttng_consumer_local_data
*ctx
)
3020 pthread_mutex_lock(&stream
->lock
);
3022 switch (consumer_data
.type
) {
3023 case LTTNG_CONSUMER_KERNEL
:
3024 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3026 case LTTNG_CONSUMER32_UST
:
3027 case LTTNG_CONSUMER64_UST
:
3028 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3031 ERR("Unknown consumer_data type");
3037 pthread_mutex_unlock(&stream
->lock
);
3041 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3043 switch (consumer_data
.type
) {
3044 case LTTNG_CONSUMER_KERNEL
:
3045 return lttng_kconsumer_on_recv_stream(stream
);
3046 case LTTNG_CONSUMER32_UST
:
3047 case LTTNG_CONSUMER64_UST
:
3048 return lttng_ustconsumer_on_recv_stream(stream
);
3050 ERR("Unknown consumer_data type");
3057 * Allocate and set consumer data hash tables.
3059 void lttng_consumer_init(void)
3061 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3062 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3063 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3064 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3068 * Process the ADD_RELAYD command receive by a consumer.
3070 * This will create a relayd socket pair and add it to the relayd hash table.
3071 * The caller MUST acquire a RCU read side lock before calling it.
3073 int consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3074 struct lttng_consumer_local_data
*ctx
, int sock
,
3075 struct pollfd
*consumer_sockpoll
,
3076 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
)
3078 int fd
= -1, ret
= -1, relayd_created
= 0;
3079 enum lttng_error_code ret_code
= LTTNG_OK
;
3080 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3083 assert(relayd_sock
);
3085 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3087 /* Get relayd reference if exists. */
3088 relayd
= consumer_find_relayd(net_seq_idx
);
3089 if (relayd
== NULL
) {
3090 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3091 /* Not found. Allocate one. */
3092 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3093 if (relayd
== NULL
) {
3095 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3098 relayd
->sessiond_session_id
= sessiond_id
;
3103 * This code path MUST continue to the consumer send status message to
3104 * we can notify the session daemon and continue our work without
3105 * killing everything.
3109 * relayd key should never be found for control socket.
3111 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3114 /* First send a status message before receiving the fds. */
3115 ret
= consumer_send_status_msg(sock
, LTTNG_OK
);
3117 /* Somehow, the session daemon is not responding anymore. */
3118 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3119 goto error_nosignal
;
3122 /* Poll on consumer socket. */
3123 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
3124 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3126 goto error_nosignal
;
3129 /* Get relayd socket from session daemon */
3130 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3131 if (ret
!= sizeof(fd
)) {
3133 fd
= -1; /* Just in case it gets set with an invalid value. */
3136 * Failing to receive FDs might indicate a major problem such as
3137 * reaching a fd limit during the receive where the kernel returns a
3138 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3139 * don't take any chances and stop everything.
3141 * XXX: Feature request #558 will fix that and avoid this possible
3142 * issue when reaching the fd limit.
3144 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3145 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3149 /* Copy socket information and received FD */
3150 switch (sock_type
) {
3151 case LTTNG_STREAM_CONTROL
:
3152 /* Copy received lttcomm socket */
3153 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3154 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3155 /* Handle create_sock error. */
3157 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3161 * Close the socket created internally by
3162 * lttcomm_create_sock, so we can replace it by the one
3163 * received from sessiond.
3165 if (close(relayd
->control_sock
.sock
.fd
)) {
3169 /* Assign new file descriptor */
3170 relayd
->control_sock
.sock
.fd
= fd
;
3171 fd
= -1; /* For error path */
3172 /* Assign version values. */
3173 relayd
->control_sock
.major
= relayd_sock
->major
;
3174 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3177 * Create a session on the relayd and store the returned id. Lock the
3178 * control socket mutex if the relayd was NOT created before.
3180 if (!relayd_created
) {
3181 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3183 ret
= relayd_create_session(&relayd
->control_sock
,
3184 &relayd
->relayd_session_id
);
3185 if (!relayd_created
) {
3186 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3190 * Close all sockets of a relayd object. It will be freed if it was
3191 * created at the error code path or else it will be garbage
3194 (void) relayd_close(&relayd
->control_sock
);
3195 (void) relayd_close(&relayd
->data_sock
);
3196 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
3201 case LTTNG_STREAM_DATA
:
3202 /* Copy received lttcomm socket */
3203 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3204 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3205 /* Handle create_sock error. */
3207 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3211 * Close the socket created internally by
3212 * lttcomm_create_sock, so we can replace it by the one
3213 * received from sessiond.
3215 if (close(relayd
->data_sock
.sock
.fd
)) {
3219 /* Assign new file descriptor */
3220 relayd
->data_sock
.sock
.fd
= fd
;
3221 fd
= -1; /* for eventual error paths */
3222 /* Assign version values. */
3223 relayd
->data_sock
.major
= relayd_sock
->major
;
3224 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3227 ERR("Unknown relayd socket type (%d)", sock_type
);
3229 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3233 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3234 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3235 relayd
->net_seq_idx
, fd
);
3237 /* We successfully added the socket. Send status back. */
3238 ret
= consumer_send_status_msg(sock
, ret_code
);
3240 /* Somehow, the session daemon is not responding anymore. */
3241 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3242 goto error_nosignal
;
3246 * Add relayd socket pair to consumer data hashtable. If object already
3247 * exists or on error, the function gracefully returns.
3255 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3256 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3260 /* Close received socket if valid. */
3263 PERROR("close received socket");
3267 if (relayd_created
) {
3275 * Try to lock the stream mutex.
3277 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3279 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3286 * Try to lock the stream mutex. On failure, we know that the stream is
3287 * being used else where hence there is data still being extracted.
3289 ret
= pthread_mutex_trylock(&stream
->lock
);
3291 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3303 * Search for a relayd associated to the session id and return the reference.
3305 * A rcu read side lock MUST be acquire before calling this function and locked
3306 * until the relayd object is no longer necessary.
3308 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3310 struct lttng_ht_iter iter
;
3311 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3313 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3314 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3317 * Check by sessiond id which is unique here where the relayd session
3318 * id might not be when having multiple relayd.
3320 if (relayd
->sessiond_session_id
== id
) {
3321 /* Found the relayd. There can be only one per id. */
3333 * Check if for a given session id there is still data needed to be extract
3336 * Return 1 if data is pending or else 0 meaning ready to be read.
3338 int consumer_data_pending(uint64_t id
)
3341 struct lttng_ht_iter iter
;
3342 struct lttng_ht
*ht
;
3343 struct lttng_consumer_stream
*stream
;
3344 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3345 int (*data_pending
)(struct lttng_consumer_stream
*);
3347 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3350 pthread_mutex_lock(&consumer_data
.lock
);
3352 switch (consumer_data
.type
) {
3353 case LTTNG_CONSUMER_KERNEL
:
3354 data_pending
= lttng_kconsumer_data_pending
;
3356 case LTTNG_CONSUMER32_UST
:
3357 case LTTNG_CONSUMER64_UST
:
3358 data_pending
= lttng_ustconsumer_data_pending
;
3361 ERR("Unknown consumer data type");
3365 /* Ease our life a bit */
3366 ht
= consumer_data
.stream_list_ht
;
3368 relayd
= find_relayd_by_session_id(id
);
3370 /* Send init command for data pending. */
3371 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3372 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3373 relayd
->relayd_session_id
);
3374 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3376 /* Communication error thus the relayd so no data pending. */
3377 goto data_not_pending
;
3381 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3382 ht
->hash_fct(&id
, lttng_ht_seed
),
3384 &iter
.iter
, stream
, node_session_id
.node
) {
3385 /* If this call fails, the stream is being used hence data pending. */
3386 ret
= stream_try_lock(stream
);
3392 * A removed node from the hash table indicates that the stream has
3393 * been deleted thus having a guarantee that the buffers are closed
3394 * on the consumer side. However, data can still be transmitted
3395 * over the network so don't skip the relayd check.
3397 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3399 /* Check the stream if there is data in the buffers. */
3400 ret
= data_pending(stream
);
3402 pthread_mutex_unlock(&stream
->lock
);
3409 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3410 if (stream
->metadata_flag
) {
3411 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3412 stream
->relayd_stream_id
);
3414 ret
= relayd_data_pending(&relayd
->control_sock
,
3415 stream
->relayd_stream_id
,
3416 stream
->next_net_seq_num
- 1);
3418 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3420 pthread_mutex_unlock(&stream
->lock
);
3424 pthread_mutex_unlock(&stream
->lock
);
3428 unsigned int is_data_inflight
= 0;
3430 /* Send init command for data pending. */
3431 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3432 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3433 relayd
->relayd_session_id
, &is_data_inflight
);
3434 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3436 goto data_not_pending
;
3438 if (is_data_inflight
) {
3444 * Finding _no_ node in the hash table and no inflight data means that the
3445 * stream(s) have been removed thus data is guaranteed to be available for
3446 * analysis from the trace files.
3450 /* Data is available to be read by a viewer. */
3451 pthread_mutex_unlock(&consumer_data
.lock
);
3456 /* Data is still being extracted from buffers. */
3457 pthread_mutex_unlock(&consumer_data
.lock
);
3463 * Send a ret code status message to the sessiond daemon.
3465 * Return the sendmsg() return value.
3467 int consumer_send_status_msg(int sock
, int ret_code
)
3469 struct lttcomm_consumer_status_msg msg
;
3471 msg
.ret_code
= ret_code
;
3473 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3477 * Send a channel status message to the sessiond daemon.
3479 * Return the sendmsg() return value.
3481 int consumer_send_status_channel(int sock
,
3482 struct lttng_consumer_channel
*channel
)
3484 struct lttcomm_consumer_status_channel msg
;
3489 msg
.ret_code
= -LTTNG_ERR_UST_CHAN_FAIL
;
3491 msg
.ret_code
= LTTNG_OK
;
3492 msg
.key
= channel
->key
;
3493 msg
.stream_count
= channel
->streams
.count
;
3496 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3500 * Using a maximum stream size with the produced and consumed position of a
3501 * stream, computes the new consumed position to be as close as possible to the
3502 * maximum possible stream size.
3504 * If maximum stream size is lower than the possible buffer size (produced -
3505 * consumed), the consumed_pos given is returned untouched else the new value
3508 unsigned long consumer_get_consumed_maxsize(unsigned long consumed_pos
,
3509 unsigned long produced_pos
, uint64_t max_stream_size
)
3511 if (max_stream_size
&& max_stream_size
< (produced_pos
- consumed_pos
)) {
3512 /* Offset from the produced position to get the latest buffers. */
3513 return produced_pos
- max_stream_size
;
3516 return consumed_pos
;