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/index/index.h>
37 #include <common/kernel-ctl/kernel-ctl.h>
38 #include <common/sessiond-comm/relayd.h>
39 #include <common/sessiond-comm/sessiond-comm.h>
40 #include <common/kernel-consumer/kernel-consumer.h>
41 #include <common/relayd/relayd.h>
42 #include <common/ust-consumer/ust-consumer.h>
45 #include "consumer-stream.h"
47 struct lttng_consumer_global_data consumer_data
= {
50 .type
= LTTNG_CONSUMER_UNKNOWN
,
53 enum consumer_channel_action
{
56 CONSUMER_CHANNEL_QUIT
,
59 struct consumer_channel_msg
{
60 enum consumer_channel_action action
;
61 struct lttng_consumer_channel
*chan
; /* add */
62 uint64_t key
; /* del */
66 * Flag to inform the polling thread to quit when all fd hung up. Updated by
67 * the consumer_thread_receive_fds when it notices that all fds has hung up.
68 * Also updated by the signal handler (consumer_should_exit()). Read by the
71 volatile int consumer_quit
;
74 * Global hash table containing respectively metadata and data streams. The
75 * stream element in this ht should only be updated by the metadata poll thread
76 * for the metadata and the data poll thread for the data.
78 static struct lttng_ht
*metadata_ht
;
79 static struct lttng_ht
*data_ht
;
82 * Notify a thread lttng pipe to poll back again. This usually means that some
83 * global state has changed so we just send back the thread in a poll wait
86 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
88 struct lttng_consumer_stream
*null_stream
= NULL
;
92 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
95 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
96 struct lttng_consumer_channel
*chan
,
98 enum consumer_channel_action action
)
100 struct consumer_channel_msg msg
;
103 memset(&msg
, 0, sizeof(msg
));
109 ret
= write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
110 } while (ret
< 0 && errno
== EINTR
);
113 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
116 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
119 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
120 struct lttng_consumer_channel
**chan
,
122 enum consumer_channel_action
*action
)
124 struct consumer_channel_msg msg
;
128 ret
= read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
129 } while (ret
< 0 && errno
== EINTR
);
131 *action
= msg
.action
;
139 * Find a stream. The consumer_data.lock must be locked during this
142 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
145 struct lttng_ht_iter iter
;
146 struct lttng_ht_node_u64
*node
;
147 struct lttng_consumer_stream
*stream
= NULL
;
151 /* -1ULL keys are lookup failures */
152 if (key
== (uint64_t) -1ULL) {
158 lttng_ht_lookup(ht
, &key
, &iter
);
159 node
= lttng_ht_iter_get_node_u64(&iter
);
161 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
169 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
171 struct lttng_consumer_stream
*stream
;
174 stream
= find_stream(key
, ht
);
176 stream
->key
= (uint64_t) -1ULL;
178 * We don't want the lookup to match, but we still need
179 * to iterate on this stream when iterating over the hash table. Just
180 * change the node key.
182 stream
->node
.key
= (uint64_t) -1ULL;
188 * Return a channel object for the given key.
190 * RCU read side lock MUST be acquired before calling this function and
191 * protects the channel ptr.
193 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
195 struct lttng_ht_iter iter
;
196 struct lttng_ht_node_u64
*node
;
197 struct lttng_consumer_channel
*channel
= NULL
;
199 /* -1ULL keys are lookup failures */
200 if (key
== (uint64_t) -1ULL) {
204 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
205 node
= lttng_ht_iter_get_node_u64(&iter
);
207 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
213 static void free_stream_rcu(struct rcu_head
*head
)
215 struct lttng_ht_node_u64
*node
=
216 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
217 struct lttng_consumer_stream
*stream
=
218 caa_container_of(node
, struct lttng_consumer_stream
, node
);
223 static void free_channel_rcu(struct rcu_head
*head
)
225 struct lttng_ht_node_u64
*node
=
226 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
227 struct lttng_consumer_channel
*channel
=
228 caa_container_of(node
, struct lttng_consumer_channel
, node
);
234 * RCU protected relayd socket pair free.
236 static void free_relayd_rcu(struct rcu_head
*head
)
238 struct lttng_ht_node_u64
*node
=
239 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
240 struct consumer_relayd_sock_pair
*relayd
=
241 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
244 * Close all sockets. This is done in the call RCU since we don't want the
245 * socket fds to be reassigned thus potentially creating bad state of the
248 * We do not have to lock the control socket mutex here since at this stage
249 * there is no one referencing to this relayd object.
251 (void) relayd_close(&relayd
->control_sock
);
252 (void) relayd_close(&relayd
->data_sock
);
258 * Destroy and free relayd socket pair object.
260 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
263 struct lttng_ht_iter iter
;
265 if (relayd
== NULL
) {
269 DBG("Consumer destroy and close relayd socket pair");
271 iter
.iter
.node
= &relayd
->node
.node
;
272 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
274 /* We assume the relayd is being or is destroyed */
278 /* RCU free() call */
279 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
283 * Remove a channel from the global list protected by a mutex. This function is
284 * also responsible for freeing its data structures.
286 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
289 struct lttng_ht_iter iter
;
290 struct lttng_consumer_stream
*stream
, *stmp
;
292 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
294 pthread_mutex_lock(&consumer_data
.lock
);
295 pthread_mutex_lock(&channel
->lock
);
297 /* Delete streams that might have been left in the stream list. */
298 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
300 cds_list_del(&stream
->send_node
);
302 * Once a stream is added to this list, the buffers were created so
303 * we have a guarantee that this call will succeed.
305 consumer_stream_destroy(stream
, NULL
);
308 switch (consumer_data
.type
) {
309 case LTTNG_CONSUMER_KERNEL
:
311 case LTTNG_CONSUMER32_UST
:
312 case LTTNG_CONSUMER64_UST
:
313 lttng_ustconsumer_del_channel(channel
);
316 ERR("Unknown consumer_data type");
322 iter
.iter
.node
= &channel
->node
.node
;
323 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
327 call_rcu(&channel
->node
.head
, free_channel_rcu
);
329 pthread_mutex_unlock(&channel
->lock
);
330 pthread_mutex_unlock(&consumer_data
.lock
);
334 * Iterate over the relayd hash table and destroy each element. Finally,
335 * destroy the whole hash table.
337 static void cleanup_relayd_ht(void)
339 struct lttng_ht_iter iter
;
340 struct consumer_relayd_sock_pair
*relayd
;
344 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
346 consumer_destroy_relayd(relayd
);
351 lttng_ht_destroy(consumer_data
.relayd_ht
);
355 * Update the end point status of all streams having the given network sequence
356 * index (relayd index).
358 * It's atomically set without having the stream mutex locked which is fine
359 * because we handle the write/read race with a pipe wakeup for each thread.
361 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
362 enum consumer_endpoint_status status
)
364 struct lttng_ht_iter iter
;
365 struct lttng_consumer_stream
*stream
;
367 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
371 /* Let's begin with metadata */
372 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
373 if (stream
->net_seq_idx
== net_seq_idx
) {
374 uatomic_set(&stream
->endpoint_status
, status
);
375 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
379 /* Follow up by the data streams */
380 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
381 if (stream
->net_seq_idx
== net_seq_idx
) {
382 uatomic_set(&stream
->endpoint_status
, status
);
383 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
390 * Cleanup a relayd object by flagging every associated streams for deletion,
391 * destroying the object meaning removing it from the relayd hash table,
392 * closing the sockets and freeing the memory in a RCU call.
394 * If a local data context is available, notify the threads that the streams'
395 * state have changed.
397 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
398 struct lttng_consumer_local_data
*ctx
)
404 DBG("Cleaning up relayd sockets");
406 /* Save the net sequence index before destroying the object */
407 netidx
= relayd
->net_seq_idx
;
410 * Delete the relayd from the relayd hash table, close the sockets and free
411 * the object in a RCU call.
413 consumer_destroy_relayd(relayd
);
415 /* Set inactive endpoint to all streams */
416 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
419 * With a local data context, notify the threads that the streams' state
420 * have changed. The write() action on the pipe acts as an "implicit"
421 * memory barrier ordering the updates of the end point status from the
422 * read of this status which happens AFTER receiving this notify.
425 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
426 notify_thread_lttng_pipe(ctx
->consumer_metadata_pipe
);
431 * Flag a relayd socket pair for destruction. Destroy it if the refcount
434 * RCU read side lock MUST be aquired before calling this function.
436 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
440 /* Set destroy flag for this object */
441 uatomic_set(&relayd
->destroy_flag
, 1);
443 /* Destroy the relayd if refcount is 0 */
444 if (uatomic_read(&relayd
->refcount
) == 0) {
445 consumer_destroy_relayd(relayd
);
450 * Completly destroy stream from every visiable data structure and the given
453 * One this call returns, the stream object is not longer usable nor visible.
455 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
458 consumer_stream_destroy(stream
, ht
);
462 * XXX naming of del vs destroy is all mixed up.
464 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
466 consumer_stream_destroy(stream
, data_ht
);
469 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
471 consumer_stream_destroy(stream
, metadata_ht
);
474 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
476 enum lttng_consumer_stream_state state
,
477 const char *channel_name
,
484 enum consumer_channel_type type
,
485 unsigned int monitor
)
488 struct lttng_consumer_stream
*stream
;
490 stream
= zmalloc(sizeof(*stream
));
491 if (stream
== NULL
) {
492 PERROR("malloc struct lttng_consumer_stream");
499 stream
->key
= stream_key
;
501 stream
->out_fd_offset
= 0;
502 stream
->output_written
= 0;
503 stream
->state
= state
;
506 stream
->net_seq_idx
= relayd_id
;
507 stream
->session_id
= session_id
;
508 stream
->monitor
= monitor
;
509 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
510 stream
->index_fd
= -1;
511 pthread_mutex_init(&stream
->lock
, NULL
);
513 /* If channel is the metadata, flag this stream as metadata. */
514 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
515 stream
->metadata_flag
= 1;
516 /* Metadata is flat out. */
517 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
519 /* Format stream name to <channel_name>_<cpu_number> */
520 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
523 PERROR("snprintf stream name");
528 /* Key is always the wait_fd for streams. */
529 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
531 /* Init node per channel id key */
532 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
534 /* Init session id node with the stream session id */
535 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
537 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
538 " relayd_id %" PRIu64
", session_id %" PRIu64
,
539 stream
->name
, stream
->key
, channel_key
,
540 stream
->net_seq_idx
, stream
->session_id
);
556 * Add a stream to the global list protected by a mutex.
558 int consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
560 struct lttng_ht
*ht
= data_ht
;
566 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
568 pthread_mutex_lock(&consumer_data
.lock
);
569 pthread_mutex_lock(&stream
->chan
->lock
);
570 pthread_mutex_lock(&stream
->chan
->timer_lock
);
571 pthread_mutex_lock(&stream
->lock
);
574 /* Steal stream identifier to avoid having streams with the same key */
575 steal_stream_key(stream
->key
, ht
);
577 lttng_ht_add_unique_u64(ht
, &stream
->node
);
579 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
580 &stream
->node_channel_id
);
583 * Add stream to the stream_list_ht of the consumer data. No need to steal
584 * the key since the HT does not use it and we allow to add redundant keys
587 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
590 * When nb_init_stream_left reaches 0, we don't need to trigger any action
591 * in terms of destroying the associated channel, because the action that
592 * causes the count to become 0 also causes a stream to be added. The
593 * channel deletion will thus be triggered by the following removal of this
596 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
597 /* Increment refcount before decrementing nb_init_stream_left */
599 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
602 /* Update consumer data once the node is inserted. */
603 consumer_data
.stream_count
++;
604 consumer_data
.need_update
= 1;
607 pthread_mutex_unlock(&stream
->lock
);
608 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
609 pthread_mutex_unlock(&stream
->chan
->lock
);
610 pthread_mutex_unlock(&consumer_data
.lock
);
615 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
617 consumer_del_stream(stream
, data_ht
);
621 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
622 * be acquired before calling this.
624 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
627 struct lttng_ht_node_u64
*node
;
628 struct lttng_ht_iter iter
;
632 lttng_ht_lookup(consumer_data
.relayd_ht
,
633 &relayd
->net_seq_idx
, &iter
);
634 node
= lttng_ht_iter_get_node_u64(&iter
);
638 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
645 * Allocate and return a consumer relayd socket.
647 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
648 uint64_t net_seq_idx
)
650 struct consumer_relayd_sock_pair
*obj
= NULL
;
652 /* net sequence index of -1 is a failure */
653 if (net_seq_idx
== (uint64_t) -1ULL) {
657 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
659 PERROR("zmalloc relayd sock");
663 obj
->net_seq_idx
= net_seq_idx
;
665 obj
->destroy_flag
= 0;
666 obj
->control_sock
.sock
.fd
= -1;
667 obj
->data_sock
.sock
.fd
= -1;
668 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
669 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
676 * Find a relayd socket pair in the global consumer data.
678 * Return the object if found else NULL.
679 * RCU read-side lock must be held across this call and while using the
682 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
684 struct lttng_ht_iter iter
;
685 struct lttng_ht_node_u64
*node
;
686 struct consumer_relayd_sock_pair
*relayd
= NULL
;
688 /* Negative keys are lookup failures */
689 if (key
== (uint64_t) -1ULL) {
693 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
695 node
= lttng_ht_iter_get_node_u64(&iter
);
697 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
705 * Find a relayd and send the stream
707 * Returns 0 on success, < 0 on error
709 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
713 struct consumer_relayd_sock_pair
*relayd
;
716 assert(stream
->net_seq_idx
!= -1ULL);
719 /* The stream is not metadata. Get relayd reference if exists. */
721 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
722 if (relayd
!= NULL
) {
723 /* Add stream on the relayd */
724 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
725 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
726 path
, &stream
->relayd_stream_id
,
727 stream
->chan
->tracefile_size
, stream
->chan
->tracefile_count
);
728 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
733 uatomic_inc(&relayd
->refcount
);
734 stream
->sent_to_relayd
= 1;
736 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
737 stream
->key
, stream
->net_seq_idx
);
742 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
743 stream
->name
, stream
->key
, stream
->net_seq_idx
);
751 * Find a relayd and close the stream
753 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
755 struct consumer_relayd_sock_pair
*relayd
;
757 /* The stream is not metadata. Get relayd reference if exists. */
759 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
761 consumer_stream_relayd_close(stream
, relayd
);
767 * Handle stream for relayd transmission if the stream applies for network
768 * streaming where the net sequence index is set.
770 * Return destination file descriptor or negative value on error.
772 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
773 size_t data_size
, unsigned long padding
,
774 struct consumer_relayd_sock_pair
*relayd
)
777 struct lttcomm_relayd_data_hdr data_hdr
;
783 /* Reset data header */
784 memset(&data_hdr
, 0, sizeof(data_hdr
));
786 if (stream
->metadata_flag
) {
787 /* Caller MUST acquire the relayd control socket lock */
788 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
793 /* Metadata are always sent on the control socket. */
794 outfd
= relayd
->control_sock
.sock
.fd
;
796 /* Set header with stream information */
797 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
798 data_hdr
.data_size
= htobe32(data_size
);
799 data_hdr
.padding_size
= htobe32(padding
);
801 * Note that net_seq_num below is assigned with the *current* value of
802 * next_net_seq_num and only after that the next_net_seq_num will be
803 * increment. This is why when issuing a command on the relayd using
804 * this next value, 1 should always be substracted in order to compare
805 * the last seen sequence number on the relayd side to the last sent.
807 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
808 /* Other fields are zeroed previously */
810 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
816 ++stream
->next_net_seq_num
;
818 /* Set to go on data socket */
819 outfd
= relayd
->data_sock
.sock
.fd
;
827 * Allocate and return a new lttng_consumer_channel object using the given key
828 * to initialize the hash table node.
830 * On error, return NULL.
832 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
834 const char *pathname
,
839 enum lttng_event_output output
,
840 uint64_t tracefile_size
,
841 uint64_t tracefile_count
,
842 uint64_t session_id_per_pid
,
843 unsigned int monitor
,
844 unsigned int live_timer_interval
)
846 struct lttng_consumer_channel
*channel
;
848 channel
= zmalloc(sizeof(*channel
));
849 if (channel
== NULL
) {
850 PERROR("malloc struct lttng_consumer_channel");
855 channel
->refcount
= 0;
856 channel
->session_id
= session_id
;
857 channel
->session_id_per_pid
= session_id_per_pid
;
860 channel
->relayd_id
= relayd_id
;
861 channel
->output
= output
;
862 channel
->tracefile_size
= tracefile_size
;
863 channel
->tracefile_count
= tracefile_count
;
864 channel
->monitor
= monitor
;
865 channel
->live_timer_interval
= live_timer_interval
;
866 pthread_mutex_init(&channel
->lock
, NULL
);
867 pthread_mutex_init(&channel
->timer_lock
, NULL
);
870 * In monitor mode, the streams associated with the channel will be put in
871 * a special list ONLY owned by this channel. So, the refcount is set to 1
872 * here meaning that the channel itself has streams that are referenced.
874 * On a channel deletion, once the channel is no longer visible, the
875 * refcount is decremented and checked for a zero value to delete it. With
876 * streams in no monitor mode, it will now be safe to destroy the channel.
878 if (!channel
->monitor
) {
879 channel
->refcount
= 1;
882 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
883 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
885 strncpy(channel
->name
, name
, sizeof(channel
->name
));
886 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
888 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
890 channel
->wait_fd
= -1;
892 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
894 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
901 * Add a channel to the global list protected by a mutex.
903 * On success 0 is returned else a negative value.
905 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
906 struct lttng_consumer_local_data
*ctx
)
909 struct lttng_ht_node_u64
*node
;
910 struct lttng_ht_iter iter
;
912 pthread_mutex_lock(&consumer_data
.lock
);
913 pthread_mutex_lock(&channel
->lock
);
914 pthread_mutex_lock(&channel
->timer_lock
);
917 lttng_ht_lookup(consumer_data
.channel_ht
, &channel
->key
, &iter
);
918 node
= lttng_ht_iter_get_node_u64(&iter
);
920 /* Channel already exist. Ignore the insertion */
921 ERR("Consumer add channel key %" PRIu64
" already exists!",
927 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
931 pthread_mutex_unlock(&channel
->timer_lock
);
932 pthread_mutex_unlock(&channel
->lock
);
933 pthread_mutex_unlock(&consumer_data
.lock
);
935 if (!ret
&& channel
->wait_fd
!= -1 &&
936 channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
937 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
943 * Allocate the pollfd structure and the local view of the out fds to avoid
944 * doing a lookup in the linked list and concurrency issues when writing is
945 * needed. Called with consumer_data.lock held.
947 * Returns the number of fds in the structures.
949 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
950 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
954 struct lttng_ht_iter iter
;
955 struct lttng_consumer_stream
*stream
;
960 assert(local_stream
);
962 DBG("Updating poll fd array");
964 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
966 * Only active streams with an active end point can be added to the
967 * poll set and local stream storage of the thread.
969 * There is a potential race here for endpoint_status to be updated
970 * just after the check. However, this is OK since the stream(s) will
971 * be deleted once the thread is notified that the end point state has
972 * changed where this function will be called back again.
974 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
975 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
979 * This clobbers way too much the debug output. Uncomment that if you
980 * need it for debugging purposes.
982 * DBG("Active FD %d", stream->wait_fd);
984 (*pollfd
)[i
].fd
= stream
->wait_fd
;
985 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
986 local_stream
[i
] = stream
;
992 * Insert the consumer_data_pipe at the end of the array and don't
993 * increment i so nb_fd is the number of real FD.
995 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
996 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1001 * Poll on the should_quit pipe and the command socket return -1 on error and
1002 * should exit, 0 if data is available on the command socket
1004 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1009 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1010 if (num_rdy
== -1) {
1012 * Restart interrupted system call.
1014 if (errno
== EINTR
) {
1017 PERROR("Poll error");
1020 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1021 DBG("consumer_should_quit wake up");
1031 * Set the error socket.
1033 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1036 ctx
->consumer_error_socket
= sock
;
1040 * Set the command socket path.
1042 void lttng_consumer_set_command_sock_path(
1043 struct lttng_consumer_local_data
*ctx
, char *sock
)
1045 ctx
->consumer_command_sock_path
= sock
;
1049 * Send return code to the session daemon.
1050 * If the socket is not defined, we return 0, it is not a fatal error
1052 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1054 if (ctx
->consumer_error_socket
> 0) {
1055 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1056 sizeof(enum lttcomm_sessiond_command
));
1063 * Close all the tracefiles and stream fds and MUST be called when all
1064 * instances are destroyed i.e. when all threads were joined and are ended.
1066 void lttng_consumer_cleanup(void)
1068 struct lttng_ht_iter iter
;
1069 struct lttng_consumer_channel
*channel
;
1073 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1075 consumer_del_channel(channel
);
1080 lttng_ht_destroy(consumer_data
.channel_ht
);
1082 cleanup_relayd_ht();
1084 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1087 * This HT contains streams that are freed by either the metadata thread or
1088 * the data thread so we do *nothing* on the hash table and simply destroy
1091 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1095 * Called from signal handler.
1097 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1102 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1103 } while (ret
< 0 && errno
== EINTR
);
1104 if (ret
< 0 || ret
!= 1) {
1105 PERROR("write consumer quit");
1108 DBG("Consumer flag that it should quit");
1111 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1114 int outfd
= stream
->out_fd
;
1117 * This does a blocking write-and-wait on any page that belongs to the
1118 * subbuffer prior to the one we just wrote.
1119 * Don't care about error values, as these are just hints and ways to
1120 * limit the amount of page cache used.
1122 if (orig_offset
< stream
->max_sb_size
) {
1125 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1126 stream
->max_sb_size
,
1127 SYNC_FILE_RANGE_WAIT_BEFORE
1128 | SYNC_FILE_RANGE_WRITE
1129 | SYNC_FILE_RANGE_WAIT_AFTER
);
1131 * Give hints to the kernel about how we access the file:
1132 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1135 * We need to call fadvise again after the file grows because the
1136 * kernel does not seem to apply fadvise to non-existing parts of the
1139 * Call fadvise _after_ having waited for the page writeback to
1140 * complete because the dirty page writeback semantic is not well
1141 * defined. So it can be expected to lead to lower throughput in
1144 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1145 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1149 * Initialise the necessary environnement :
1150 * - create a new context
1151 * - create the poll_pipe
1152 * - create the should_quit pipe (for signal handler)
1153 * - create the thread pipe (for splice)
1155 * Takes a function pointer as argument, this function is called when data is
1156 * available on a buffer. This function is responsible to do the
1157 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1158 * buffer configuration and then kernctl_put_next_subbuf at the end.
1160 * Returns a pointer to the new context or NULL on error.
1162 struct lttng_consumer_local_data
*lttng_consumer_create(
1163 enum lttng_consumer_type type
,
1164 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1165 struct lttng_consumer_local_data
*ctx
),
1166 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1167 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1168 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1171 struct lttng_consumer_local_data
*ctx
;
1173 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1174 consumer_data
.type
== type
);
1175 consumer_data
.type
= type
;
1177 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1179 PERROR("allocating context");
1183 ctx
->consumer_error_socket
= -1;
1184 ctx
->consumer_metadata_socket
= -1;
1185 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1186 /* assign the callbacks */
1187 ctx
->on_buffer_ready
= buffer_ready
;
1188 ctx
->on_recv_channel
= recv_channel
;
1189 ctx
->on_recv_stream
= recv_stream
;
1190 ctx
->on_update_stream
= update_stream
;
1192 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1193 if (!ctx
->consumer_data_pipe
) {
1194 goto error_poll_pipe
;
1197 ret
= pipe(ctx
->consumer_should_quit
);
1199 PERROR("Error creating recv pipe");
1200 goto error_quit_pipe
;
1203 ret
= pipe(ctx
->consumer_thread_pipe
);
1205 PERROR("Error creating thread pipe");
1206 goto error_thread_pipe
;
1209 ret
= pipe(ctx
->consumer_channel_pipe
);
1211 PERROR("Error creating channel pipe");
1212 goto error_channel_pipe
;
1215 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1216 if (!ctx
->consumer_metadata_pipe
) {
1217 goto error_metadata_pipe
;
1220 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1222 goto error_splice_pipe
;
1228 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1229 error_metadata_pipe
:
1230 utils_close_pipe(ctx
->consumer_channel_pipe
);
1232 utils_close_pipe(ctx
->consumer_thread_pipe
);
1234 utils_close_pipe(ctx
->consumer_should_quit
);
1236 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1244 * Close all fds associated with the instance and free the context.
1246 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1250 DBG("Consumer destroying it. Closing everything.");
1252 ret
= close(ctx
->consumer_error_socket
);
1256 ret
= close(ctx
->consumer_metadata_socket
);
1260 utils_close_pipe(ctx
->consumer_thread_pipe
);
1261 utils_close_pipe(ctx
->consumer_channel_pipe
);
1262 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1263 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1264 utils_close_pipe(ctx
->consumer_should_quit
);
1265 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1267 unlink(ctx
->consumer_command_sock_path
);
1272 * Write the metadata stream id on the specified file descriptor.
1274 static int write_relayd_metadata_id(int fd
,
1275 struct lttng_consumer_stream
*stream
,
1276 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1279 struct lttcomm_relayd_metadata_payload hdr
;
1281 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1282 hdr
.padding_size
= htobe32(padding
);
1284 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1285 } while (ret
< 0 && errno
== EINTR
);
1286 if (ret
< 0 || ret
!= sizeof(hdr
)) {
1288 * This error means that the fd's end is closed so ignore the perror
1289 * not to clubber the error output since this can happen in a normal
1292 if (errno
!= EPIPE
) {
1293 PERROR("write metadata stream id");
1295 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1297 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1298 * handle writting the missing part so report that as an error and
1299 * don't lie to the caller.
1304 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1305 stream
->relayd_stream_id
, padding
);
1312 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1313 * core function for writing trace buffers to either the local filesystem or
1316 * It must be called with the stream lock held.
1318 * Careful review MUST be put if any changes occur!
1320 * Returns the number of bytes written
1322 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1323 struct lttng_consumer_local_data
*ctx
,
1324 struct lttng_consumer_stream
*stream
, unsigned long len
,
1325 unsigned long padding
,
1326 struct lttng_packet_index
*index
)
1328 unsigned long mmap_offset
;
1330 ssize_t ret
= 0, written
= 0;
1331 off_t orig_offset
= stream
->out_fd_offset
;
1332 /* Default is on the disk */
1333 int outfd
= stream
->out_fd
;
1334 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1335 unsigned int relayd_hang_up
= 0;
1337 /* RCU lock for the relayd pointer */
1340 /* Flag that the current stream if set for network streaming. */
1341 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1342 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1343 if (relayd
== NULL
) {
1349 /* get the offset inside the fd to mmap */
1350 switch (consumer_data
.type
) {
1351 case LTTNG_CONSUMER_KERNEL
:
1352 mmap_base
= stream
->mmap_base
;
1353 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1355 PERROR("tracer ctl get_mmap_read_offset");
1360 case LTTNG_CONSUMER32_UST
:
1361 case LTTNG_CONSUMER64_UST
:
1362 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1364 ERR("read mmap get mmap base for stream %s", stream
->name
);
1368 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1370 PERROR("tracer ctl get_mmap_read_offset");
1376 ERR("Unknown consumer_data type");
1380 /* Handle stream on the relayd if the output is on the network */
1382 unsigned long netlen
= len
;
1385 * Lock the control socket for the complete duration of the function
1386 * since from this point on we will use the socket.
1388 if (stream
->metadata_flag
) {
1389 /* Metadata requires the control socket. */
1390 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1391 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1394 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1396 /* Use the returned socket. */
1399 /* Write metadata stream id before payload */
1400 if (stream
->metadata_flag
) {
1401 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1404 /* Socket operation failed. We consider the relayd dead */
1405 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1413 /* Socket operation failed. We consider the relayd dead */
1414 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1418 /* Else, use the default set before which is the filesystem. */
1421 /* No streaming, we have to set the len with the full padding */
1425 * Check if we need to change the tracefile before writing the packet.
1427 if (stream
->chan
->tracefile_size
> 0 &&
1428 (stream
->tracefile_size_current
+ len
) >
1429 stream
->chan
->tracefile_size
) {
1430 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1431 stream
->name
, stream
->chan
->tracefile_size
,
1432 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1433 stream
->out_fd
, &(stream
->tracefile_count_current
),
1436 ERR("Rotating output file");
1439 outfd
= stream
->out_fd
;
1441 if (stream
->index_fd
>= 0) {
1442 ret
= index_create_file(stream
->chan
->pathname
,
1443 stream
->name
, stream
->uid
, stream
->gid
,
1444 stream
->chan
->tracefile_size
,
1445 stream
->tracefile_count_current
);
1449 stream
->index_fd
= ret
;
1452 /* Reset current size because we just perform a rotation. */
1453 stream
->tracefile_size_current
= 0;
1454 stream
->out_fd_offset
= 0;
1457 stream
->tracefile_size_current
+= len
;
1459 index
->offset
= htobe64(stream
->out_fd_offset
);
1465 ret
= write(outfd
, mmap_base
+ mmap_offset
, len
);
1466 } while (ret
< 0 && errno
== EINTR
);
1467 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1470 * This is possible if the fd is closed on the other side (outfd)
1471 * or any write problem. It can be verbose a bit for a normal
1472 * execution if for instance the relayd is stopped abruptly. This
1473 * can happen so set this to a DBG statement.
1475 DBG("Error in file write mmap");
1479 /* Socket operation failed. We consider the relayd dead */
1480 if (errno
== EPIPE
|| errno
== EINVAL
) {
1485 } else if (ret
> len
) {
1486 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1494 /* This call is useless on a socket so better save a syscall. */
1496 /* This won't block, but will start writeout asynchronously */
1497 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1498 SYNC_FILE_RANGE_WRITE
);
1499 stream
->out_fd_offset
+= ret
;
1501 stream
->output_written
+= ret
;
1504 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1508 * This is a special case that the relayd has closed its socket. Let's
1509 * cleanup the relayd object and all associated streams.
1511 if (relayd
&& relayd_hang_up
) {
1512 cleanup_relayd(relayd
, ctx
);
1516 /* Unlock only if ctrl socket used */
1517 if (relayd
&& stream
->metadata_flag
) {
1518 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1526 * Splice the data from the ring buffer to the tracefile.
1528 * It must be called with the stream lock held.
1530 * Returns the number of bytes spliced.
1532 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1533 struct lttng_consumer_local_data
*ctx
,
1534 struct lttng_consumer_stream
*stream
, unsigned long len
,
1535 unsigned long padding
,
1536 struct lttng_packet_index
*index
)
1538 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1540 off_t orig_offset
= stream
->out_fd_offset
;
1541 int fd
= stream
->wait_fd
;
1542 /* Default is on the disk */
1543 int outfd
= stream
->out_fd
;
1544 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1546 unsigned int relayd_hang_up
= 0;
1548 switch (consumer_data
.type
) {
1549 case LTTNG_CONSUMER_KERNEL
:
1551 case LTTNG_CONSUMER32_UST
:
1552 case LTTNG_CONSUMER64_UST
:
1553 /* Not supported for user space tracing */
1556 ERR("Unknown consumer_data type");
1560 /* RCU lock for the relayd pointer */
1563 /* Flag that the current stream if set for network streaming. */
1564 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1565 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1566 if (relayd
== NULL
) {
1573 * Choose right pipe for splice. Metadata and trace data are handled by
1574 * different threads hence the use of two pipes in order not to race or
1575 * corrupt the written data.
1577 if (stream
->metadata_flag
) {
1578 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1580 splice_pipe
= ctx
->consumer_thread_pipe
;
1583 /* Write metadata stream id before payload */
1585 int total_len
= len
;
1587 if (stream
->metadata_flag
) {
1589 * Lock the control socket for the complete duration of the function
1590 * since from this point on we will use the socket.
1592 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1594 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1598 /* Socket operation failed. We consider the relayd dead */
1599 if (ret
== -EBADF
) {
1600 WARN("Remote relayd disconnected. Stopping");
1607 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1610 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1612 /* Use the returned socket. */
1615 /* Socket operation failed. We consider the relayd dead */
1616 if (ret
== -EBADF
) {
1617 WARN("Remote relayd disconnected. Stopping");
1624 /* No streaming, we have to set the len with the full padding */
1628 * Check if we need to change the tracefile before writing the packet.
1630 if (stream
->chan
->tracefile_size
> 0 &&
1631 (stream
->tracefile_size_current
+ len
) >
1632 stream
->chan
->tracefile_size
) {
1633 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1634 stream
->name
, stream
->chan
->tracefile_size
,
1635 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1636 stream
->out_fd
, &(stream
->tracefile_count_current
),
1639 ERR("Rotating output file");
1642 outfd
= stream
->out_fd
;
1644 if (stream
->index_fd
>= 0) {
1645 ret
= index_create_file(stream
->chan
->pathname
,
1646 stream
->name
, stream
->uid
, stream
->gid
,
1647 stream
->chan
->tracefile_size
,
1648 stream
->tracefile_count_current
);
1652 stream
->index_fd
= ret
;
1655 /* Reset current size because we just perform a rotation. */
1656 stream
->tracefile_size_current
= 0;
1657 stream
->out_fd_offset
= 0;
1660 stream
->tracefile_size_current
+= len
;
1661 index
->offset
= htobe64(stream
->out_fd_offset
);
1665 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1666 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1667 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1668 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1669 DBG("splice chan to pipe, ret %zd", ret_splice
);
1670 if (ret_splice
< 0) {
1671 PERROR("Error in relay splice");
1673 written
= ret_splice
;
1679 /* Handle stream on the relayd if the output is on the network */
1681 if (stream
->metadata_flag
) {
1682 size_t metadata_payload_size
=
1683 sizeof(struct lttcomm_relayd_metadata_payload
);
1685 /* Update counter to fit the spliced data */
1686 ret_splice
+= metadata_payload_size
;
1687 len
+= metadata_payload_size
;
1689 * We do this so the return value can match the len passed as
1690 * argument to this function.
1692 written
-= metadata_payload_size
;
1696 /* Splice data out */
1697 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1698 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1699 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1700 if (ret_splice
< 0) {
1701 PERROR("Error in file splice");
1703 written
= ret_splice
;
1705 /* Socket operation failed. We consider the relayd dead */
1706 if (errno
== EBADF
|| errno
== EPIPE
) {
1707 WARN("Remote relayd disconnected. Stopping");
1713 } else if (ret_splice
> len
) {
1715 PERROR("Wrote more data than requested %zd (len: %lu)",
1717 written
+= ret_splice
;
1723 /* This call is useless on a socket so better save a syscall. */
1725 /* This won't block, but will start writeout asynchronously */
1726 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1727 SYNC_FILE_RANGE_WRITE
);
1728 stream
->out_fd_offset
+= ret_splice
;
1730 stream
->output_written
+= ret_splice
;
1731 written
+= ret_splice
;
1733 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1741 * This is a special case that the relayd has closed its socket. Let's
1742 * cleanup the relayd object and all associated streams.
1744 if (relayd
&& relayd_hang_up
) {
1745 cleanup_relayd(relayd
, ctx
);
1746 /* Skip splice error so the consumer does not fail */
1751 /* send the appropriate error description to sessiond */
1754 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1757 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1760 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1765 if (relayd
&& stream
->metadata_flag
) {
1766 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1774 * Take a snapshot for a specific fd
1776 * Returns 0 on success, < 0 on error
1778 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1780 switch (consumer_data
.type
) {
1781 case LTTNG_CONSUMER_KERNEL
:
1782 return lttng_kconsumer_take_snapshot(stream
);
1783 case LTTNG_CONSUMER32_UST
:
1784 case LTTNG_CONSUMER64_UST
:
1785 return lttng_ustconsumer_take_snapshot(stream
);
1787 ERR("Unknown consumer_data type");
1794 * Get the produced position
1796 * Returns 0 on success, < 0 on error
1798 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1801 switch (consumer_data
.type
) {
1802 case LTTNG_CONSUMER_KERNEL
:
1803 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1804 case LTTNG_CONSUMER32_UST
:
1805 case LTTNG_CONSUMER64_UST
:
1806 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1808 ERR("Unknown consumer_data type");
1814 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1815 int sock
, struct pollfd
*consumer_sockpoll
)
1817 switch (consumer_data
.type
) {
1818 case LTTNG_CONSUMER_KERNEL
:
1819 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1820 case LTTNG_CONSUMER32_UST
:
1821 case LTTNG_CONSUMER64_UST
:
1822 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1824 ERR("Unknown consumer_data type");
1831 * Iterate over all streams of the hashtable and free them properly.
1833 * WARNING: *MUST* be used with data stream only.
1835 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1837 struct lttng_ht_iter iter
;
1838 struct lttng_consumer_stream
*stream
;
1845 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1847 * Ignore return value since we are currently cleaning up so any error
1850 (void) consumer_del_stream(stream
, ht
);
1854 lttng_ht_destroy(ht
);
1858 * Iterate over all streams of the hashtable and free them properly.
1860 * XXX: Should not be only for metadata stream or else use an other name.
1862 static void destroy_stream_ht(struct lttng_ht
*ht
)
1864 struct lttng_ht_iter iter
;
1865 struct lttng_consumer_stream
*stream
;
1872 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1874 * Ignore return value since we are currently cleaning up so any error
1877 (void) consumer_del_metadata_stream(stream
, ht
);
1881 lttng_ht_destroy(ht
);
1884 void lttng_consumer_close_metadata(void)
1886 switch (consumer_data
.type
) {
1887 case LTTNG_CONSUMER_KERNEL
:
1889 * The Kernel consumer has a different metadata scheme so we don't
1890 * close anything because the stream will be closed by the session
1894 case LTTNG_CONSUMER32_UST
:
1895 case LTTNG_CONSUMER64_UST
:
1897 * Close all metadata streams. The metadata hash table is passed and
1898 * this call iterates over it by closing all wakeup fd. This is safe
1899 * because at this point we are sure that the metadata producer is
1900 * either dead or blocked.
1902 lttng_ustconsumer_close_metadata(metadata_ht
);
1905 ERR("Unknown consumer_data type");
1911 * Clean up a metadata stream and free its memory.
1913 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1914 struct lttng_ht
*ht
)
1917 struct lttng_ht_iter iter
;
1918 struct lttng_consumer_channel
*free_chan
= NULL
;
1919 struct consumer_relayd_sock_pair
*relayd
;
1923 * This call should NEVER receive regular stream. It must always be
1924 * metadata stream and this is crucial for data structure synchronization.
1926 assert(stream
->metadata_flag
);
1928 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1931 /* Means the stream was allocated but not successfully added */
1932 goto free_stream_rcu
;
1935 pthread_mutex_lock(&consumer_data
.lock
);
1936 pthread_mutex_lock(&stream
->chan
->lock
);
1937 pthread_mutex_lock(&stream
->lock
);
1939 switch (consumer_data
.type
) {
1940 case LTTNG_CONSUMER_KERNEL
:
1941 if (stream
->mmap_base
!= NULL
) {
1942 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1944 PERROR("munmap metadata stream");
1947 if (stream
->wait_fd
>= 0) {
1948 ret
= close(stream
->wait_fd
);
1950 PERROR("close kernel metadata wait_fd");
1954 case LTTNG_CONSUMER32_UST
:
1955 case LTTNG_CONSUMER64_UST
:
1956 if (stream
->monitor
) {
1957 /* close the write-side in close_metadata */
1958 ret
= close(stream
->ust_metadata_poll_pipe
[0]);
1960 PERROR("Close UST metadata read-side poll pipe");
1963 lttng_ustconsumer_del_stream(stream
);
1966 ERR("Unknown consumer_data type");
1972 iter
.iter
.node
= &stream
->node
.node
;
1973 ret
= lttng_ht_del(ht
, &iter
);
1976 iter
.iter
.node
= &stream
->node_channel_id
.node
;
1977 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
1980 iter
.iter
.node
= &stream
->node_session_id
.node
;
1981 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1985 if (stream
->out_fd
>= 0) {
1986 ret
= close(stream
->out_fd
);
1992 /* Check and cleanup relayd */
1994 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1995 if (relayd
!= NULL
) {
1996 uatomic_dec(&relayd
->refcount
);
1997 assert(uatomic_read(&relayd
->refcount
) >= 0);
1999 /* Closing streams requires to lock the control socket. */
2000 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
2001 ret
= relayd_send_close_stream(&relayd
->control_sock
,
2002 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
2003 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2005 DBG("Unable to close stream on the relayd. Continuing");
2007 * Continue here. There is nothing we can do for the relayd.
2008 * Chances are that the relayd has closed the socket so we just
2009 * continue cleaning up.
2013 /* Both conditions are met, we destroy the relayd. */
2014 if (uatomic_read(&relayd
->refcount
) == 0 &&
2015 uatomic_read(&relayd
->destroy_flag
)) {
2016 consumer_destroy_relayd(relayd
);
2021 /* Atomically decrement channel refcount since other threads can use it. */
2022 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
2023 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
2024 /* Go for channel deletion! */
2025 free_chan
= stream
->chan
;
2030 * Nullify the stream reference so it is not used after deletion. The
2031 * channel lock MUST be acquired before being able to check for
2032 * a NULL pointer value.
2034 stream
->chan
->metadata_stream
= NULL
;
2036 pthread_mutex_unlock(&stream
->lock
);
2037 pthread_mutex_unlock(&stream
->chan
->lock
);
2038 pthread_mutex_unlock(&consumer_data
.lock
);
2041 consumer_del_channel(free_chan
);
2045 call_rcu(&stream
->node
.head
, free_stream_rcu
);
2049 * Action done with the metadata stream when adding it to the consumer internal
2050 * data structures to handle it.
2052 int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2054 struct lttng_ht
*ht
= metadata_ht
;
2056 struct lttng_ht_iter iter
;
2057 struct lttng_ht_node_u64
*node
;
2062 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2064 pthread_mutex_lock(&consumer_data
.lock
);
2065 pthread_mutex_lock(&stream
->chan
->lock
);
2066 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2067 pthread_mutex_lock(&stream
->lock
);
2070 * From here, refcounts are updated so be _careful_ when returning an error
2077 * Lookup the stream just to make sure it does not exist in our internal
2078 * state. This should NEVER happen.
2080 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2081 node
= lttng_ht_iter_get_node_u64(&iter
);
2085 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2086 * in terms of destroying the associated channel, because the action that
2087 * causes the count to become 0 also causes a stream to be added. The
2088 * channel deletion will thus be triggered by the following removal of this
2091 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2092 /* Increment refcount before decrementing nb_init_stream_left */
2094 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2097 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2099 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2100 &stream
->node_channel_id
);
2103 * Add stream to the stream_list_ht of the consumer data. No need to steal
2104 * the key since the HT does not use it and we allow to add redundant keys
2107 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2111 pthread_mutex_unlock(&stream
->lock
);
2112 pthread_mutex_unlock(&stream
->chan
->lock
);
2113 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2114 pthread_mutex_unlock(&consumer_data
.lock
);
2119 * Delete data stream that are flagged for deletion (endpoint_status).
2121 static void validate_endpoint_status_data_stream(void)
2123 struct lttng_ht_iter iter
;
2124 struct lttng_consumer_stream
*stream
;
2126 DBG("Consumer delete flagged data stream");
2129 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2130 /* Validate delete flag of the stream */
2131 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2134 /* Delete it right now */
2135 consumer_del_stream(stream
, data_ht
);
2141 * Delete metadata stream that are flagged for deletion (endpoint_status).
2143 static void validate_endpoint_status_metadata_stream(
2144 struct lttng_poll_event
*pollset
)
2146 struct lttng_ht_iter iter
;
2147 struct lttng_consumer_stream
*stream
;
2149 DBG("Consumer delete flagged metadata stream");
2154 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2155 /* Validate delete flag of the stream */
2156 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2160 * Remove from pollset so the metadata thread can continue without
2161 * blocking on a deleted stream.
2163 lttng_poll_del(pollset
, stream
->wait_fd
);
2165 /* Delete it right now */
2166 consumer_del_metadata_stream(stream
, metadata_ht
);
2172 * Thread polls on metadata file descriptor and write them on disk or on the
2175 void *consumer_thread_metadata_poll(void *data
)
2178 uint32_t revents
, nb_fd
;
2179 struct lttng_consumer_stream
*stream
= NULL
;
2180 struct lttng_ht_iter iter
;
2181 struct lttng_ht_node_u64
*node
;
2182 struct lttng_poll_event events
;
2183 struct lttng_consumer_local_data
*ctx
= data
;
2186 rcu_register_thread();
2188 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2190 /* ENOMEM at this point. Better to bail out. */
2194 DBG("Thread metadata poll started");
2196 /* Size is set to 1 for the consumer_metadata pipe */
2197 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2199 ERR("Poll set creation failed");
2203 ret
= lttng_poll_add(&events
,
2204 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2210 DBG("Metadata main loop started");
2213 /* Only the metadata pipe is set */
2214 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2219 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2220 ret
= lttng_poll_wait(&events
, -1);
2221 DBG("Metadata event catched in thread");
2223 if (errno
== EINTR
) {
2224 ERR("Poll EINTR catched");
2232 /* From here, the event is a metadata wait fd */
2233 for (i
= 0; i
< nb_fd
; i
++) {
2234 revents
= LTTNG_POLL_GETEV(&events
, i
);
2235 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2237 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2238 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2239 DBG("Metadata thread pipe hung up");
2241 * Remove the pipe from the poll set and continue the loop
2242 * since their might be data to consume.
2244 lttng_poll_del(&events
,
2245 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2246 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2248 } else if (revents
& LPOLLIN
) {
2251 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2252 &stream
, sizeof(stream
));
2254 ERR("read metadata stream, ret: %zd", pipe_len
);
2256 * Continue here to handle the rest of the streams.
2261 /* A NULL stream means that the state has changed. */
2262 if (stream
== NULL
) {
2263 /* Check for deleted streams. */
2264 validate_endpoint_status_metadata_stream(&events
);
2268 DBG("Adding metadata stream %d to poll set",
2271 /* Add metadata stream to the global poll events list */
2272 lttng_poll_add(&events
, stream
->wait_fd
,
2273 LPOLLIN
| LPOLLPRI
);
2276 /* Handle other stream */
2282 uint64_t tmp_id
= (uint64_t) pollfd
;
2284 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2286 node
= lttng_ht_iter_get_node_u64(&iter
);
2289 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2292 /* Check for error event */
2293 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2294 DBG("Metadata fd %d is hup|err.", pollfd
);
2295 if (!stream
->hangup_flush_done
2296 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2297 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2298 DBG("Attempting to flush and consume the UST buffers");
2299 lttng_ustconsumer_on_stream_hangup(stream
);
2301 /* We just flushed the stream now read it. */
2303 len
= ctx
->on_buffer_ready(stream
, ctx
);
2305 * We don't check the return value here since if we get
2306 * a negative len, it means an error occured thus we
2307 * simply remove it from the poll set and free the
2313 lttng_poll_del(&events
, stream
->wait_fd
);
2315 * This call update the channel states, closes file descriptors
2316 * and securely free the stream.
2318 consumer_del_metadata_stream(stream
, metadata_ht
);
2319 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2320 /* Get the data out of the metadata file descriptor */
2321 DBG("Metadata available on fd %d", pollfd
);
2322 assert(stream
->wait_fd
== pollfd
);
2325 len
= ctx
->on_buffer_ready(stream
, ctx
);
2327 * We don't check the return value here since if we get
2328 * a negative len, it means an error occured thus we
2329 * simply remove it from the poll set and free the
2334 /* It's ok to have an unavailable sub-buffer */
2335 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2336 /* Clean up stream from consumer and free it. */
2337 lttng_poll_del(&events
, stream
->wait_fd
);
2338 consumer_del_metadata_stream(stream
, metadata_ht
);
2342 /* Release RCU lock for the stream looked up */
2349 DBG("Metadata poll thread exiting");
2351 lttng_poll_clean(&events
);
2353 destroy_stream_ht(metadata_ht
);
2355 rcu_unregister_thread();
2360 * This thread polls the fds in the set to consume the data and write
2361 * it to tracefile if necessary.
2363 void *consumer_thread_data_poll(void *data
)
2365 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2366 struct pollfd
*pollfd
= NULL
;
2367 /* local view of the streams */
2368 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2369 /* local view of consumer_data.fds_count */
2371 struct lttng_consumer_local_data
*ctx
= data
;
2374 rcu_register_thread();
2376 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2377 if (data_ht
== NULL
) {
2378 /* ENOMEM at this point. Better to bail out. */
2382 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2383 if (local_stream
== NULL
) {
2384 PERROR("local_stream malloc");
2393 * the fds set has been updated, we need to update our
2394 * local array as well
2396 pthread_mutex_lock(&consumer_data
.lock
);
2397 if (consumer_data
.need_update
) {
2402 local_stream
= NULL
;
2404 /* allocate for all fds + 1 for the consumer_data_pipe */
2405 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2406 if (pollfd
== NULL
) {
2407 PERROR("pollfd malloc");
2408 pthread_mutex_unlock(&consumer_data
.lock
);
2412 /* allocate for all fds + 1 for the consumer_data_pipe */
2413 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2414 sizeof(struct lttng_consumer_stream
*));
2415 if (local_stream
== NULL
) {
2416 PERROR("local_stream malloc");
2417 pthread_mutex_unlock(&consumer_data
.lock
);
2420 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2423 ERR("Error in allocating pollfd or local_outfds");
2424 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2425 pthread_mutex_unlock(&consumer_data
.lock
);
2429 consumer_data
.need_update
= 0;
2431 pthread_mutex_unlock(&consumer_data
.lock
);
2433 /* No FDs and consumer_quit, consumer_cleanup the thread */
2434 if (nb_fd
== 0 && consumer_quit
== 1) {
2437 /* poll on the array of fds */
2439 DBG("polling on %d fd", nb_fd
+ 1);
2440 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2441 DBG("poll num_rdy : %d", num_rdy
);
2442 if (num_rdy
== -1) {
2444 * Restart interrupted system call.
2446 if (errno
== EINTR
) {
2449 PERROR("Poll error");
2450 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2452 } else if (num_rdy
== 0) {
2453 DBG("Polling thread timed out");
2458 * If the consumer_data_pipe triggered poll go directly to the
2459 * beginning of the loop to update the array. We want to prioritize
2460 * array update over low-priority reads.
2462 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2463 ssize_t pipe_readlen
;
2465 DBG("consumer_data_pipe wake up");
2466 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2467 &new_stream
, sizeof(new_stream
));
2468 if (pipe_readlen
< 0) {
2469 ERR("Consumer data pipe ret %zd", pipe_readlen
);
2470 /* Continue so we can at least handle the current stream(s). */
2475 * If the stream is NULL, just ignore it. It's also possible that
2476 * the sessiond poll thread changed the consumer_quit state and is
2477 * waking us up to test it.
2479 if (new_stream
== NULL
) {
2480 validate_endpoint_status_data_stream();
2484 /* Continue to update the local streams and handle prio ones */
2488 /* Take care of high priority channels first. */
2489 for (i
= 0; i
< nb_fd
; i
++) {
2490 if (local_stream
[i
] == NULL
) {
2493 if (pollfd
[i
].revents
& POLLPRI
) {
2494 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2496 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2497 /* it's ok to have an unavailable sub-buffer */
2498 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2499 /* Clean the stream and free it. */
2500 consumer_del_stream(local_stream
[i
], data_ht
);
2501 local_stream
[i
] = NULL
;
2502 } else if (len
> 0) {
2503 local_stream
[i
]->data_read
= 1;
2509 * If we read high prio channel in this loop, try again
2510 * for more high prio data.
2516 /* Take care of low priority channels. */
2517 for (i
= 0; i
< nb_fd
; i
++) {
2518 if (local_stream
[i
] == NULL
) {
2521 if ((pollfd
[i
].revents
& POLLIN
) ||
2522 local_stream
[i
]->hangup_flush_done
) {
2523 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2524 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2525 /* it's ok to have an unavailable sub-buffer */
2526 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2527 /* Clean the stream and free it. */
2528 consumer_del_stream(local_stream
[i
], data_ht
);
2529 local_stream
[i
] = NULL
;
2530 } else if (len
> 0) {
2531 local_stream
[i
]->data_read
= 1;
2536 /* Handle hangup and errors */
2537 for (i
= 0; i
< nb_fd
; i
++) {
2538 if (local_stream
[i
] == NULL
) {
2541 if (!local_stream
[i
]->hangup_flush_done
2542 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2543 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2544 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2545 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2547 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2548 /* Attempt read again, for the data we just flushed. */
2549 local_stream
[i
]->data_read
= 1;
2552 * If the poll flag is HUP/ERR/NVAL and we have
2553 * read no data in this pass, we can remove the
2554 * stream from its hash table.
2556 if ((pollfd
[i
].revents
& POLLHUP
)) {
2557 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2558 if (!local_stream
[i
]->data_read
) {
2559 consumer_del_stream(local_stream
[i
], data_ht
);
2560 local_stream
[i
] = NULL
;
2563 } else if (pollfd
[i
].revents
& POLLERR
) {
2564 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2565 if (!local_stream
[i
]->data_read
) {
2566 consumer_del_stream(local_stream
[i
], data_ht
);
2567 local_stream
[i
] = NULL
;
2570 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2571 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2572 if (!local_stream
[i
]->data_read
) {
2573 consumer_del_stream(local_stream
[i
], data_ht
);
2574 local_stream
[i
] = NULL
;
2578 if (local_stream
[i
] != NULL
) {
2579 local_stream
[i
]->data_read
= 0;
2584 DBG("polling thread exiting");
2589 * Close the write side of the pipe so epoll_wait() in
2590 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2591 * read side of the pipe. If we close them both, epoll_wait strangely does
2592 * not return and could create a endless wait period if the pipe is the
2593 * only tracked fd in the poll set. The thread will take care of closing
2596 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2598 destroy_data_stream_ht(data_ht
);
2600 rcu_unregister_thread();
2605 * Close wake-up end of each stream belonging to the channel. This will
2606 * allow the poll() on the stream read-side to detect when the
2607 * write-side (application) finally closes them.
2610 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2612 struct lttng_ht
*ht
;
2613 struct lttng_consumer_stream
*stream
;
2614 struct lttng_ht_iter iter
;
2616 ht
= consumer_data
.stream_per_chan_id_ht
;
2619 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2620 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2621 ht
->match_fct
, &channel
->key
,
2622 &iter
.iter
, stream
, node_channel_id
.node
) {
2624 * Protect against teardown with mutex.
2626 pthread_mutex_lock(&stream
->lock
);
2627 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2630 switch (consumer_data
.type
) {
2631 case LTTNG_CONSUMER_KERNEL
:
2633 case LTTNG_CONSUMER32_UST
:
2634 case LTTNG_CONSUMER64_UST
:
2636 * Note: a mutex is taken internally within
2637 * liblttng-ust-ctl to protect timer wakeup_fd
2638 * use from concurrent close.
2640 lttng_ustconsumer_close_stream_wakeup(stream
);
2643 ERR("Unknown consumer_data type");
2647 pthread_mutex_unlock(&stream
->lock
);
2652 static void destroy_channel_ht(struct lttng_ht
*ht
)
2654 struct lttng_ht_iter iter
;
2655 struct lttng_consumer_channel
*channel
;
2663 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2664 ret
= lttng_ht_del(ht
, &iter
);
2669 lttng_ht_destroy(ht
);
2673 * This thread polls the channel fds to detect when they are being
2674 * closed. It closes all related streams if the channel is detected as
2675 * closed. It is currently only used as a shim layer for UST because the
2676 * consumerd needs to keep the per-stream wakeup end of pipes open for
2679 void *consumer_thread_channel_poll(void *data
)
2682 uint32_t revents
, nb_fd
;
2683 struct lttng_consumer_channel
*chan
= NULL
;
2684 struct lttng_ht_iter iter
;
2685 struct lttng_ht_node_u64
*node
;
2686 struct lttng_poll_event events
;
2687 struct lttng_consumer_local_data
*ctx
= data
;
2688 struct lttng_ht
*channel_ht
;
2690 rcu_register_thread();
2692 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2694 /* ENOMEM at this point. Better to bail out. */
2698 DBG("Thread channel poll started");
2700 /* Size is set to 1 for the consumer_channel pipe */
2701 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2703 ERR("Poll set creation failed");
2707 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2713 DBG("Channel main loop started");
2716 /* Only the channel pipe is set */
2717 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2722 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2723 ret
= lttng_poll_wait(&events
, -1);
2724 DBG("Channel event catched in thread");
2726 if (errno
== EINTR
) {
2727 ERR("Poll EINTR catched");
2735 /* From here, the event is a channel wait fd */
2736 for (i
= 0; i
< nb_fd
; i
++) {
2737 revents
= LTTNG_POLL_GETEV(&events
, i
);
2738 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2740 /* Just don't waste time if no returned events for the fd */
2744 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2745 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2746 DBG("Channel thread pipe hung up");
2748 * Remove the pipe from the poll set and continue the loop
2749 * since their might be data to consume.
2751 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2753 } else if (revents
& LPOLLIN
) {
2754 enum consumer_channel_action action
;
2757 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2759 ERR("Error reading channel pipe");
2764 case CONSUMER_CHANNEL_ADD
:
2765 DBG("Adding channel %d to poll set",
2768 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2771 lttng_ht_add_unique_u64(channel_ht
,
2772 &chan
->wait_fd_node
);
2774 /* Add channel to the global poll events list */
2775 lttng_poll_add(&events
, chan
->wait_fd
,
2776 LPOLLIN
| LPOLLPRI
);
2778 case CONSUMER_CHANNEL_DEL
:
2780 struct lttng_consumer_stream
*stream
, *stmp
;
2783 chan
= consumer_find_channel(key
);
2786 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2789 lttng_poll_del(&events
, chan
->wait_fd
);
2790 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2791 ret
= lttng_ht_del(channel_ht
, &iter
);
2793 consumer_close_channel_streams(chan
);
2795 switch (consumer_data
.type
) {
2796 case LTTNG_CONSUMER_KERNEL
:
2798 case LTTNG_CONSUMER32_UST
:
2799 case LTTNG_CONSUMER64_UST
:
2800 /* Delete streams that might have been left in the stream list. */
2801 cds_list_for_each_entry_safe(stream
, stmp
, &chan
->streams
.head
,
2803 cds_list_del(&stream
->send_node
);
2804 lttng_ustconsumer_del_stream(stream
);
2805 uatomic_sub(&stream
->chan
->refcount
, 1);
2806 assert(&chan
->refcount
);
2811 ERR("Unknown consumer_data type");
2816 * Release our own refcount. Force channel deletion even if
2817 * streams were not initialized.
2819 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2820 consumer_del_channel(chan
);
2825 case CONSUMER_CHANNEL_QUIT
:
2827 * Remove the pipe from the poll set and continue the loop
2828 * since their might be data to consume.
2830 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2833 ERR("Unknown action");
2838 /* Handle other stream */
2844 uint64_t tmp_id
= (uint64_t) pollfd
;
2846 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2848 node
= lttng_ht_iter_get_node_u64(&iter
);
2851 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2854 /* Check for error event */
2855 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2856 DBG("Channel fd %d is hup|err.", pollfd
);
2858 lttng_poll_del(&events
, chan
->wait_fd
);
2859 ret
= lttng_ht_del(channel_ht
, &iter
);
2861 consumer_close_channel_streams(chan
);
2863 /* Release our own refcount */
2864 if (!uatomic_sub_return(&chan
->refcount
, 1)
2865 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2866 consumer_del_channel(chan
);
2870 /* Release RCU lock for the channel looked up */
2876 lttng_poll_clean(&events
);
2878 destroy_channel_ht(channel_ht
);
2880 DBG("Channel poll thread exiting");
2881 rcu_unregister_thread();
2885 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2886 struct pollfd
*sockpoll
, int client_socket
)
2893 if (lttng_consumer_poll_socket(sockpoll
) < 0) {
2897 DBG("Metadata connection on client_socket");
2899 /* Blocking call, waiting for transmission */
2900 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2901 if (ctx
->consumer_metadata_socket
< 0) {
2902 WARN("On accept metadata");
2913 * This thread listens on the consumerd socket and receives the file
2914 * descriptors from the session daemon.
2916 void *consumer_thread_sessiond_poll(void *data
)
2918 int sock
= -1, client_socket
, ret
;
2920 * structure to poll for incoming data on communication socket avoids
2921 * making blocking sockets.
2923 struct pollfd consumer_sockpoll
[2];
2924 struct lttng_consumer_local_data
*ctx
= data
;
2926 rcu_register_thread();
2928 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2929 unlink(ctx
->consumer_command_sock_path
);
2930 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2931 if (client_socket
< 0) {
2932 ERR("Cannot create command socket");
2936 ret
= lttcomm_listen_unix_sock(client_socket
);
2941 DBG("Sending ready command to lttng-sessiond");
2942 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2943 /* return < 0 on error, but == 0 is not fatal */
2945 ERR("Error sending ready command to lttng-sessiond");
2949 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2950 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2951 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2952 consumer_sockpoll
[1].fd
= client_socket
;
2953 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2955 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2958 DBG("Connection on client_socket");
2960 /* Blocking call, waiting for transmission */
2961 sock
= lttcomm_accept_unix_sock(client_socket
);
2968 * Setup metadata socket which is the second socket connection on the
2969 * command unix socket.
2971 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
2976 /* This socket is not useful anymore. */
2977 ret
= close(client_socket
);
2979 PERROR("close client_socket");
2983 /* update the polling structure to poll on the established socket */
2984 consumer_sockpoll
[1].fd
= sock
;
2985 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2988 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2991 DBG("Incoming command on sock");
2992 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2993 if (ret
== -ENOENT
) {
2994 DBG("Received STOP command");
2999 * This could simply be a session daemon quitting. Don't output
3002 DBG("Communication interrupted on command socket");
3005 if (consumer_quit
) {
3006 DBG("consumer_thread_receive_fds received quit from signal");
3009 DBG("received command on sock");
3012 DBG("Consumer thread sessiond poll exiting");
3015 * Close metadata streams since the producer is the session daemon which
3018 * NOTE: for now, this only applies to the UST tracer.
3020 lttng_consumer_close_metadata();
3023 * when all fds have hung up, the polling thread
3029 * Notify the data poll thread to poll back again and test the
3030 * consumer_quit state that we just set so to quit gracefully.
3032 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3034 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3036 /* Cleaning up possibly open sockets. */
3040 PERROR("close sock sessiond poll");
3043 if (client_socket
>= 0) {
3044 ret
= close(client_socket
);
3046 PERROR("close client_socket sessiond poll");
3050 rcu_unregister_thread();
3054 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3055 struct lttng_consumer_local_data
*ctx
)
3059 pthread_mutex_lock(&stream
->lock
);
3061 switch (consumer_data
.type
) {
3062 case LTTNG_CONSUMER_KERNEL
:
3063 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3065 case LTTNG_CONSUMER32_UST
:
3066 case LTTNG_CONSUMER64_UST
:
3067 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3070 ERR("Unknown consumer_data type");
3076 pthread_mutex_unlock(&stream
->lock
);
3080 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3082 switch (consumer_data
.type
) {
3083 case LTTNG_CONSUMER_KERNEL
:
3084 return lttng_kconsumer_on_recv_stream(stream
);
3085 case LTTNG_CONSUMER32_UST
:
3086 case LTTNG_CONSUMER64_UST
:
3087 return lttng_ustconsumer_on_recv_stream(stream
);
3089 ERR("Unknown consumer_data type");
3096 * Allocate and set consumer data hash tables.
3098 void lttng_consumer_init(void)
3100 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3101 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3102 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3103 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3107 * Process the ADD_RELAYD command receive by a consumer.
3109 * This will create a relayd socket pair and add it to the relayd hash table.
3110 * The caller MUST acquire a RCU read side lock before calling it.
3112 int consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3113 struct lttng_consumer_local_data
*ctx
, int sock
,
3114 struct pollfd
*consumer_sockpoll
,
3115 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
)
3117 int fd
= -1, ret
= -1, relayd_created
= 0;
3118 enum lttng_error_code ret_code
= LTTNG_OK
;
3119 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3122 assert(relayd_sock
);
3124 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3126 /* Get relayd reference if exists. */
3127 relayd
= consumer_find_relayd(net_seq_idx
);
3128 if (relayd
== NULL
) {
3129 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3130 /* Not found. Allocate one. */
3131 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3132 if (relayd
== NULL
) {
3134 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3137 relayd
->sessiond_session_id
= sessiond_id
;
3142 * This code path MUST continue to the consumer send status message to
3143 * we can notify the session daemon and continue our work without
3144 * killing everything.
3148 * relayd key should never be found for control socket.
3150 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3153 /* First send a status message before receiving the fds. */
3154 ret
= consumer_send_status_msg(sock
, LTTNG_OK
);
3156 /* Somehow, the session daemon is not responding anymore. */
3157 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3158 goto error_nosignal
;
3161 /* Poll on consumer socket. */
3162 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
3163 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3165 goto error_nosignal
;
3168 /* Get relayd socket from session daemon */
3169 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3170 if (ret
!= sizeof(fd
)) {
3172 fd
= -1; /* Just in case it gets set with an invalid value. */
3175 * Failing to receive FDs might indicate a major problem such as
3176 * reaching a fd limit during the receive where the kernel returns a
3177 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3178 * don't take any chances and stop everything.
3180 * XXX: Feature request #558 will fix that and avoid this possible
3181 * issue when reaching the fd limit.
3183 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3184 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3188 /* Copy socket information and received FD */
3189 switch (sock_type
) {
3190 case LTTNG_STREAM_CONTROL
:
3191 /* Copy received lttcomm socket */
3192 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3193 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3194 /* Handle create_sock error. */
3196 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3200 * Close the socket created internally by
3201 * lttcomm_create_sock, so we can replace it by the one
3202 * received from sessiond.
3204 if (close(relayd
->control_sock
.sock
.fd
)) {
3208 /* Assign new file descriptor */
3209 relayd
->control_sock
.sock
.fd
= fd
;
3210 fd
= -1; /* For error path */
3211 /* Assign version values. */
3212 relayd
->control_sock
.major
= relayd_sock
->major
;
3213 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3216 * Create a session on the relayd and store the returned id. Lock the
3217 * control socket mutex if the relayd was NOT created before.
3219 if (!relayd_created
) {
3220 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3222 ret
= relayd_create_session(&relayd
->control_sock
,
3223 &relayd
->relayd_session_id
);
3224 if (!relayd_created
) {
3225 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3229 * Close all sockets of a relayd object. It will be freed if it was
3230 * created at the error code path or else it will be garbage
3233 (void) relayd_close(&relayd
->control_sock
);
3234 (void) relayd_close(&relayd
->data_sock
);
3235 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
3240 case LTTNG_STREAM_DATA
:
3241 /* Copy received lttcomm socket */
3242 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3243 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3244 /* Handle create_sock error. */
3246 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3250 * Close the socket created internally by
3251 * lttcomm_create_sock, so we can replace it by the one
3252 * received from sessiond.
3254 if (close(relayd
->data_sock
.sock
.fd
)) {
3258 /* Assign new file descriptor */
3259 relayd
->data_sock
.sock
.fd
= fd
;
3260 fd
= -1; /* for eventual error paths */
3261 /* Assign version values. */
3262 relayd
->data_sock
.major
= relayd_sock
->major
;
3263 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3266 ERR("Unknown relayd socket type (%d)", sock_type
);
3268 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3272 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3273 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3274 relayd
->net_seq_idx
, fd
);
3276 /* We successfully added the socket. Send status back. */
3277 ret
= consumer_send_status_msg(sock
, ret_code
);
3279 /* Somehow, the session daemon is not responding anymore. */
3280 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3281 goto error_nosignal
;
3285 * Add relayd socket pair to consumer data hashtable. If object already
3286 * exists or on error, the function gracefully returns.
3294 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3295 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3299 /* Close received socket if valid. */
3302 PERROR("close received socket");
3306 if (relayd_created
) {
3314 * Try to lock the stream mutex.
3316 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3318 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3325 * Try to lock the stream mutex. On failure, we know that the stream is
3326 * being used else where hence there is data still being extracted.
3328 ret
= pthread_mutex_trylock(&stream
->lock
);
3330 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3342 * Search for a relayd associated to the session id and return the reference.
3344 * A rcu read side lock MUST be acquire before calling this function and locked
3345 * until the relayd object is no longer necessary.
3347 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3349 struct lttng_ht_iter iter
;
3350 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3352 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3353 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3356 * Check by sessiond id which is unique here where the relayd session
3357 * id might not be when having multiple relayd.
3359 if (relayd
->sessiond_session_id
== id
) {
3360 /* Found the relayd. There can be only one per id. */
3372 * Check if for a given session id there is still data needed to be extract
3375 * Return 1 if data is pending or else 0 meaning ready to be read.
3377 int consumer_data_pending(uint64_t id
)
3380 struct lttng_ht_iter iter
;
3381 struct lttng_ht
*ht
;
3382 struct lttng_consumer_stream
*stream
;
3383 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3384 int (*data_pending
)(struct lttng_consumer_stream
*);
3386 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3389 pthread_mutex_lock(&consumer_data
.lock
);
3391 switch (consumer_data
.type
) {
3392 case LTTNG_CONSUMER_KERNEL
:
3393 data_pending
= lttng_kconsumer_data_pending
;
3395 case LTTNG_CONSUMER32_UST
:
3396 case LTTNG_CONSUMER64_UST
:
3397 data_pending
= lttng_ustconsumer_data_pending
;
3400 ERR("Unknown consumer data type");
3404 /* Ease our life a bit */
3405 ht
= consumer_data
.stream_list_ht
;
3407 relayd
= find_relayd_by_session_id(id
);
3409 /* Send init command for data pending. */
3410 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3411 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3412 relayd
->relayd_session_id
);
3413 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3415 /* Communication error thus the relayd so no data pending. */
3416 goto data_not_pending
;
3420 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3421 ht
->hash_fct(&id
, lttng_ht_seed
),
3423 &iter
.iter
, stream
, node_session_id
.node
) {
3424 /* If this call fails, the stream is being used hence data pending. */
3425 ret
= stream_try_lock(stream
);
3431 * A removed node from the hash table indicates that the stream has
3432 * been deleted thus having a guarantee that the buffers are closed
3433 * on the consumer side. However, data can still be transmitted
3434 * over the network so don't skip the relayd check.
3436 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3439 * An empty output file is not valid. We need at least one packet
3440 * generated per stream, even if it contains no event, so it
3441 * contains at least one packet header.
3443 if (stream
->output_written
== 0) {
3444 pthread_mutex_unlock(&stream
->lock
);
3447 /* Check the stream if there is data in the buffers. */
3448 ret
= data_pending(stream
);
3450 pthread_mutex_unlock(&stream
->lock
);
3457 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3458 if (stream
->metadata_flag
) {
3459 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3460 stream
->relayd_stream_id
);
3462 ret
= relayd_data_pending(&relayd
->control_sock
,
3463 stream
->relayd_stream_id
,
3464 stream
->next_net_seq_num
- 1);
3466 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3468 pthread_mutex_unlock(&stream
->lock
);
3472 pthread_mutex_unlock(&stream
->lock
);
3476 unsigned int is_data_inflight
= 0;
3478 /* Send init command for data pending. */
3479 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3480 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3481 relayd
->relayd_session_id
, &is_data_inflight
);
3482 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3484 goto data_not_pending
;
3486 if (is_data_inflight
) {
3492 * Finding _no_ node in the hash table and no inflight data means that the
3493 * stream(s) have been removed thus data is guaranteed to be available for
3494 * analysis from the trace files.
3498 /* Data is available to be read by a viewer. */
3499 pthread_mutex_unlock(&consumer_data
.lock
);
3504 /* Data is still being extracted from buffers. */
3505 pthread_mutex_unlock(&consumer_data
.lock
);
3511 * Send a ret code status message to the sessiond daemon.
3513 * Return the sendmsg() return value.
3515 int consumer_send_status_msg(int sock
, int ret_code
)
3517 struct lttcomm_consumer_status_msg msg
;
3519 msg
.ret_code
= ret_code
;
3521 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3525 * Send a channel status message to the sessiond daemon.
3527 * Return the sendmsg() return value.
3529 int consumer_send_status_channel(int sock
,
3530 struct lttng_consumer_channel
*channel
)
3532 struct lttcomm_consumer_status_channel msg
;
3537 msg
.ret_code
= -LTTNG_ERR_UST_CHAN_FAIL
;
3539 msg
.ret_code
= LTTNG_OK
;
3540 msg
.key
= channel
->key
;
3541 msg
.stream_count
= channel
->streams
.count
;
3544 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3548 * Using a maximum stream size with the produced and consumed position of a
3549 * stream, computes the new consumed position to be as close as possible to the
3550 * maximum possible stream size.
3552 * If maximum stream size is lower than the possible buffer size (produced -
3553 * consumed), the consumed_pos given is returned untouched else the new value
3556 unsigned long consumer_get_consumed_maxsize(unsigned long consumed_pos
,
3557 unsigned long produced_pos
, uint64_t max_stream_size
)
3559 if (max_stream_size
&& max_stream_size
< (produced_pos
- consumed_pos
)) {
3560 /* Offset from the produced position to get the latest buffers. */
3561 return produced_pos
- max_stream_size
;
3564 return consumed_pos
;