2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2012 - David Goulet <dgoulet@efficios.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/socket.h>
28 #include <sys/types.h>
33 #include <common/common.h>
34 #include <common/utils.h>
35 #include <common/compat/poll.h>
36 #include <common/kernel-ctl/kernel-ctl.h>
37 #include <common/sessiond-comm/relayd.h>
38 #include <common/sessiond-comm/sessiond-comm.h>
39 #include <common/kernel-consumer/kernel-consumer.h>
40 #include <common/relayd/relayd.h>
41 #include <common/ust-consumer/ust-consumer.h>
44 #include "consumer-stream.h"
46 struct lttng_consumer_global_data consumer_data
= {
49 .type
= LTTNG_CONSUMER_UNKNOWN
,
52 enum consumer_channel_action
{
55 CONSUMER_CHANNEL_QUIT
,
58 struct consumer_channel_msg
{
59 enum consumer_channel_action action
;
60 struct lttng_consumer_channel
*chan
; /* add */
61 uint64_t key
; /* del */
65 * Flag to inform the polling thread to quit when all fd hung up. Updated by
66 * the consumer_thread_receive_fds when it notices that all fds has hung up.
67 * Also updated by the signal handler (consumer_should_exit()). Read by the
70 volatile int consumer_quit
;
73 * Global hash table containing respectively metadata and data streams. The
74 * stream element in this ht should only be updated by the metadata poll thread
75 * for the metadata and the data poll thread for the data.
77 static struct lttng_ht
*metadata_ht
;
78 static struct lttng_ht
*data_ht
;
81 * Notify a thread lttng pipe to poll back again. This usually means that some
82 * global state has changed so we just send back the thread in a poll wait
85 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
87 struct lttng_consumer_stream
*null_stream
= NULL
;
91 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
94 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
95 struct lttng_consumer_channel
*chan
,
97 enum consumer_channel_action action
)
99 struct consumer_channel_msg msg
;
102 memset(&msg
, 0, sizeof(msg
));
108 ret
= write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
109 } while (ret
< 0 && errno
== EINTR
);
112 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
115 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
118 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
119 struct lttng_consumer_channel
**chan
,
121 enum consumer_channel_action
*action
)
123 struct consumer_channel_msg msg
;
127 ret
= read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
128 } while (ret
< 0 && errno
== EINTR
);
130 *action
= msg
.action
;
138 * Find a stream. The consumer_data.lock must be locked during this
141 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
144 struct lttng_ht_iter iter
;
145 struct lttng_ht_node_u64
*node
;
146 struct lttng_consumer_stream
*stream
= NULL
;
150 /* -1ULL keys are lookup failures */
151 if (key
== (uint64_t) -1ULL) {
157 lttng_ht_lookup(ht
, &key
, &iter
);
158 node
= lttng_ht_iter_get_node_u64(&iter
);
160 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
168 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
170 struct lttng_consumer_stream
*stream
;
173 stream
= find_stream(key
, ht
);
175 stream
->key
= (uint64_t) -1ULL;
177 * We don't want the lookup to match, but we still need
178 * to iterate on this stream when iterating over the hash table. Just
179 * change the node key.
181 stream
->node
.key
= (uint64_t) -1ULL;
187 * Return a channel object for the given key.
189 * RCU read side lock MUST be acquired before calling this function and
190 * protects the channel ptr.
192 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
194 struct lttng_ht_iter iter
;
195 struct lttng_ht_node_u64
*node
;
196 struct lttng_consumer_channel
*channel
= NULL
;
198 /* -1ULL keys are lookup failures */
199 if (key
== (uint64_t) -1ULL) {
203 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
204 node
= lttng_ht_iter_get_node_u64(&iter
);
206 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
212 static void free_stream_rcu(struct rcu_head
*head
)
214 struct lttng_ht_node_u64
*node
=
215 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
216 struct lttng_consumer_stream
*stream
=
217 caa_container_of(node
, struct lttng_consumer_stream
, node
);
222 static void free_channel_rcu(struct rcu_head
*head
)
224 struct lttng_ht_node_u64
*node
=
225 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
226 struct lttng_consumer_channel
*channel
=
227 caa_container_of(node
, struct lttng_consumer_channel
, node
);
233 * RCU protected relayd socket pair free.
235 static void free_relayd_rcu(struct rcu_head
*head
)
237 struct lttng_ht_node_u64
*node
=
238 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
239 struct consumer_relayd_sock_pair
*relayd
=
240 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
243 * Close all sockets. This is done in the call RCU since we don't want the
244 * socket fds to be reassigned thus potentially creating bad state of the
247 * We do not have to lock the control socket mutex here since at this stage
248 * there is no one referencing to this relayd object.
250 (void) relayd_close(&relayd
->control_sock
);
251 (void) relayd_close(&relayd
->data_sock
);
257 * Destroy and free relayd socket pair object.
259 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
262 struct lttng_ht_iter iter
;
264 if (relayd
== NULL
) {
268 DBG("Consumer destroy and close relayd socket pair");
270 iter
.iter
.node
= &relayd
->node
.node
;
271 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
273 /* We assume the relayd is being or is destroyed */
277 /* RCU free() call */
278 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
282 * Remove a channel from the global list protected by a mutex. This function is
283 * also responsible for freeing its data structures.
285 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
288 struct lttng_ht_iter iter
;
289 struct lttng_consumer_stream
*stream
, *stmp
;
291 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
293 pthread_mutex_lock(&consumer_data
.lock
);
294 pthread_mutex_lock(&channel
->lock
);
296 /* Delete streams that might have been left in the stream list. */
297 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
299 cds_list_del(&stream
->send_node
);
301 * Once a stream is added to this list, the buffers were created so
302 * we have a guarantee that this call will succeed.
304 consumer_stream_destroy(stream
, NULL
);
307 switch (consumer_data
.type
) {
308 case LTTNG_CONSUMER_KERNEL
:
310 case LTTNG_CONSUMER32_UST
:
311 case LTTNG_CONSUMER64_UST
:
312 lttng_ustconsumer_del_channel(channel
);
315 ERR("Unknown consumer_data type");
321 iter
.iter
.node
= &channel
->node
.node
;
322 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
326 call_rcu(&channel
->node
.head
, free_channel_rcu
);
328 pthread_mutex_unlock(&channel
->lock
);
329 pthread_mutex_unlock(&consumer_data
.lock
);
333 * Iterate over the relayd hash table and destroy each element. Finally,
334 * destroy the whole hash table.
336 static void cleanup_relayd_ht(void)
338 struct lttng_ht_iter iter
;
339 struct consumer_relayd_sock_pair
*relayd
;
343 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
345 consumer_destroy_relayd(relayd
);
350 lttng_ht_destroy(consumer_data
.relayd_ht
);
354 * Update the end point status of all streams having the given network sequence
355 * index (relayd index).
357 * It's atomically set without having the stream mutex locked which is fine
358 * because we handle the write/read race with a pipe wakeup for each thread.
360 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
361 enum consumer_endpoint_status status
)
363 struct lttng_ht_iter iter
;
364 struct lttng_consumer_stream
*stream
;
366 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
370 /* Let's begin with metadata */
371 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
372 if (stream
->net_seq_idx
== net_seq_idx
) {
373 uatomic_set(&stream
->endpoint_status
, status
);
374 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
378 /* Follow up by the data streams */
379 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
380 if (stream
->net_seq_idx
== net_seq_idx
) {
381 uatomic_set(&stream
->endpoint_status
, status
);
382 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
389 * Cleanup a relayd object by flagging every associated streams for deletion,
390 * destroying the object meaning removing it from the relayd hash table,
391 * closing the sockets and freeing the memory in a RCU call.
393 * If a local data context is available, notify the threads that the streams'
394 * state have changed.
396 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
397 struct lttng_consumer_local_data
*ctx
)
403 DBG("Cleaning up relayd sockets");
405 /* Save the net sequence index before destroying the object */
406 netidx
= relayd
->net_seq_idx
;
409 * Delete the relayd from the relayd hash table, close the sockets and free
410 * the object in a RCU call.
412 consumer_destroy_relayd(relayd
);
414 /* Set inactive endpoint to all streams */
415 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
418 * With a local data context, notify the threads that the streams' state
419 * have changed. The write() action on the pipe acts as an "implicit"
420 * memory barrier ordering the updates of the end point status from the
421 * read of this status which happens AFTER receiving this notify.
424 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
425 notify_thread_lttng_pipe(ctx
->consumer_metadata_pipe
);
430 * Flag a relayd socket pair for destruction. Destroy it if the refcount
433 * RCU read side lock MUST be aquired before calling this function.
435 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
439 /* Set destroy flag for this object */
440 uatomic_set(&relayd
->destroy_flag
, 1);
442 /* Destroy the relayd if refcount is 0 */
443 if (uatomic_read(&relayd
->refcount
) == 0) {
444 consumer_destroy_relayd(relayd
);
449 * Completly destroy stream from every visiable data structure and the given
452 * One this call returns, the stream object is not longer usable nor visible.
454 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
457 consumer_stream_destroy(stream
, ht
);
461 * XXX naming of del vs destroy is all mixed up.
463 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
465 consumer_stream_destroy(stream
, data_ht
);
468 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
470 consumer_stream_destroy(stream
, metadata_ht
);
473 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
475 enum lttng_consumer_stream_state state
,
476 const char *channel_name
,
483 enum consumer_channel_type type
,
484 unsigned int monitor
)
487 struct lttng_consumer_stream
*stream
;
489 stream
= zmalloc(sizeof(*stream
));
490 if (stream
== NULL
) {
491 PERROR("malloc struct lttng_consumer_stream");
498 stream
->key
= stream_key
;
500 stream
->out_fd_offset
= 0;
501 stream
->output_written
= 0;
502 stream
->state
= state
;
505 stream
->net_seq_idx
= relayd_id
;
506 stream
->session_id
= session_id
;
507 stream
->monitor
= monitor
;
508 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
509 pthread_mutex_init(&stream
->lock
, NULL
);
511 /* If channel is the metadata, flag this stream as metadata. */
512 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
513 stream
->metadata_flag
= 1;
514 /* Metadata is flat out. */
515 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
517 /* Format stream name to <channel_name>_<cpu_number> */
518 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
521 PERROR("snprintf stream name");
526 /* Key is always the wait_fd for streams. */
527 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
529 /* Init node per channel id key */
530 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
532 /* Init session id node with the stream session id */
533 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
535 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
536 " relayd_id %" PRIu64
", session_id %" PRIu64
,
537 stream
->name
, stream
->key
, channel_key
,
538 stream
->net_seq_idx
, stream
->session_id
);
554 * Add a stream to the global list protected by a mutex.
556 int consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
558 struct lttng_ht
*ht
= data_ht
;
564 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
566 pthread_mutex_lock(&consumer_data
.lock
);
567 pthread_mutex_lock(&stream
->chan
->lock
);
568 pthread_mutex_lock(&stream
->chan
->timer_lock
);
569 pthread_mutex_lock(&stream
->lock
);
572 /* Steal stream identifier to avoid having streams with the same key */
573 steal_stream_key(stream
->key
, ht
);
575 lttng_ht_add_unique_u64(ht
, &stream
->node
);
577 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
578 &stream
->node_channel_id
);
581 * Add stream to the stream_list_ht of the consumer data. No need to steal
582 * the key since the HT does not use it and we allow to add redundant keys
585 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
588 * When nb_init_stream_left reaches 0, we don't need to trigger any action
589 * in terms of destroying the associated channel, because the action that
590 * causes the count to become 0 also causes a stream to be added. The
591 * channel deletion will thus be triggered by the following removal of this
594 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
595 /* Increment refcount before decrementing nb_init_stream_left */
597 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
600 /* Update consumer data once the node is inserted. */
601 consumer_data
.stream_count
++;
602 consumer_data
.need_update
= 1;
605 pthread_mutex_unlock(&stream
->lock
);
606 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
607 pthread_mutex_unlock(&stream
->chan
->lock
);
608 pthread_mutex_unlock(&consumer_data
.lock
);
613 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
615 consumer_del_stream(stream
, data_ht
);
619 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
620 * be acquired before calling this.
622 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
625 struct lttng_ht_node_u64
*node
;
626 struct lttng_ht_iter iter
;
630 lttng_ht_lookup(consumer_data
.relayd_ht
,
631 &relayd
->net_seq_idx
, &iter
);
632 node
= lttng_ht_iter_get_node_u64(&iter
);
636 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
643 * Allocate and return a consumer relayd socket.
645 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
646 uint64_t net_seq_idx
)
648 struct consumer_relayd_sock_pair
*obj
= NULL
;
650 /* net sequence index of -1 is a failure */
651 if (net_seq_idx
== (uint64_t) -1ULL) {
655 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
657 PERROR("zmalloc relayd sock");
661 obj
->net_seq_idx
= net_seq_idx
;
663 obj
->destroy_flag
= 0;
664 obj
->control_sock
.sock
.fd
= -1;
665 obj
->data_sock
.sock
.fd
= -1;
666 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
667 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
674 * Find a relayd socket pair in the global consumer data.
676 * Return the object if found else NULL.
677 * RCU read-side lock must be held across this call and while using the
680 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
682 struct lttng_ht_iter iter
;
683 struct lttng_ht_node_u64
*node
;
684 struct consumer_relayd_sock_pair
*relayd
= NULL
;
686 /* Negative keys are lookup failures */
687 if (key
== (uint64_t) -1ULL) {
691 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
693 node
= lttng_ht_iter_get_node_u64(&iter
);
695 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
703 * Find a relayd and send the stream
705 * Returns 0 on success, < 0 on error
707 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
711 struct consumer_relayd_sock_pair
*relayd
;
714 assert(stream
->net_seq_idx
!= -1ULL);
717 /* The stream is not metadata. Get relayd reference if exists. */
719 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
720 if (relayd
!= NULL
) {
721 /* Add stream on the relayd */
722 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
723 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
724 path
, &stream
->relayd_stream_id
,
725 stream
->chan
->tracefile_size
, stream
->chan
->tracefile_count
);
726 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
730 uatomic_inc(&relayd
->refcount
);
731 stream
->sent_to_relayd
= 1;
733 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
734 stream
->key
, stream
->net_seq_idx
);
739 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
740 stream
->name
, stream
->key
, stream
->net_seq_idx
);
748 * Find a relayd and close the stream
750 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
752 struct consumer_relayd_sock_pair
*relayd
;
754 /* The stream is not metadata. Get relayd reference if exists. */
756 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
758 consumer_stream_relayd_close(stream
, relayd
);
764 * Handle stream for relayd transmission if the stream applies for network
765 * streaming where the net sequence index is set.
767 * Return destination file descriptor or negative value on error.
769 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
770 size_t data_size
, unsigned long padding
,
771 struct consumer_relayd_sock_pair
*relayd
)
774 struct lttcomm_relayd_data_hdr data_hdr
;
780 /* Reset data header */
781 memset(&data_hdr
, 0, sizeof(data_hdr
));
783 if (stream
->metadata_flag
) {
784 /* Caller MUST acquire the relayd control socket lock */
785 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
790 /* Metadata are always sent on the control socket. */
791 outfd
= relayd
->control_sock
.sock
.fd
;
793 /* Set header with stream information */
794 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
795 data_hdr
.data_size
= htobe32(data_size
);
796 data_hdr
.padding_size
= htobe32(padding
);
798 * Note that net_seq_num below is assigned with the *current* value of
799 * next_net_seq_num and only after that the next_net_seq_num will be
800 * increment. This is why when issuing a command on the relayd using
801 * this next value, 1 should always be substracted in order to compare
802 * the last seen sequence number on the relayd side to the last sent.
804 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
805 /* Other fields are zeroed previously */
807 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
813 ++stream
->next_net_seq_num
;
815 /* Set to go on data socket */
816 outfd
= relayd
->data_sock
.sock
.fd
;
824 * Allocate and return a new lttng_consumer_channel object using the given key
825 * to initialize the hash table node.
827 * On error, return NULL.
829 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
831 const char *pathname
,
836 enum lttng_event_output output
,
837 uint64_t tracefile_size
,
838 uint64_t tracefile_count
,
839 uint64_t session_id_per_pid
,
840 unsigned int monitor
)
842 struct lttng_consumer_channel
*channel
;
844 channel
= zmalloc(sizeof(*channel
));
845 if (channel
== NULL
) {
846 PERROR("malloc struct lttng_consumer_channel");
851 channel
->refcount
= 0;
852 channel
->session_id
= session_id
;
853 channel
->session_id_per_pid
= session_id_per_pid
;
856 channel
->relayd_id
= relayd_id
;
857 channel
->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 case LTTNG_EVENT_SPLICE
:
865 channel
->output
= CONSUMER_CHANNEL_SPLICE
;
867 case LTTNG_EVENT_MMAP
:
868 channel
->output
= CONSUMER_CHANNEL_MMAP
;
878 * In monitor mode, the streams associated with the channel will be put in
879 * a special list ONLY owned by this channel. So, the refcount is set to 1
880 * here meaning that the channel itself has streams that are referenced.
882 * On a channel deletion, once the channel is no longer visible, the
883 * refcount is decremented and checked for a zero value to delete it. With
884 * streams in no monitor mode, it will now be safe to destroy the channel.
886 if (!channel
->monitor
) {
887 channel
->refcount
= 1;
890 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
891 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
893 strncpy(channel
->name
, name
, sizeof(channel
->name
));
894 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
896 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
898 channel
->wait_fd
= -1;
900 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
902 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
909 * Add a channel to the global list protected by a mutex.
911 * On success 0 is returned else a negative value.
913 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
914 struct lttng_consumer_local_data
*ctx
)
917 struct lttng_ht_node_u64
*node
;
918 struct lttng_ht_iter iter
;
920 pthread_mutex_lock(&consumer_data
.lock
);
921 pthread_mutex_lock(&channel
->lock
);
922 pthread_mutex_lock(&channel
->timer_lock
);
925 lttng_ht_lookup(consumer_data
.channel_ht
, &channel
->key
, &iter
);
926 node
= lttng_ht_iter_get_node_u64(&iter
);
928 /* Channel already exist. Ignore the insertion */
929 ERR("Consumer add channel key %" PRIu64
" already exists!",
935 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
939 pthread_mutex_unlock(&channel
->timer_lock
);
940 pthread_mutex_unlock(&channel
->lock
);
941 pthread_mutex_unlock(&consumer_data
.lock
);
943 if (!ret
&& channel
->wait_fd
!= -1 &&
944 channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
945 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
951 * Allocate the pollfd structure and the local view of the out fds to avoid
952 * doing a lookup in the linked list and concurrency issues when writing is
953 * needed. Called with consumer_data.lock held.
955 * Returns the number of fds in the structures.
957 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
958 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
962 struct lttng_ht_iter iter
;
963 struct lttng_consumer_stream
*stream
;
968 assert(local_stream
);
970 DBG("Updating poll fd array");
972 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
974 * Only active streams with an active end point can be added to the
975 * poll set and local stream storage of the thread.
977 * There is a potential race here for endpoint_status to be updated
978 * just after the check. However, this is OK since the stream(s) will
979 * be deleted once the thread is notified that the end point state has
980 * changed where this function will be called back again.
982 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
983 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
987 * This clobbers way too much the debug output. Uncomment that if you
988 * need it for debugging purposes.
990 * DBG("Active FD %d", stream->wait_fd);
992 (*pollfd
)[i
].fd
= stream
->wait_fd
;
993 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
994 local_stream
[i
] = stream
;
1000 * Insert the consumer_data_pipe at the end of the array and don't
1001 * increment i so nb_fd is the number of real FD.
1003 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1004 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1009 * Poll on the should_quit pipe and the command socket return -1 on error and
1010 * should exit, 0 if data is available on the command socket
1012 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1017 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1018 if (num_rdy
== -1) {
1020 * Restart interrupted system call.
1022 if (errno
== EINTR
) {
1025 PERROR("Poll error");
1028 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1029 DBG("consumer_should_quit wake up");
1039 * Set the error socket.
1041 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1044 ctx
->consumer_error_socket
= sock
;
1048 * Set the command socket path.
1050 void lttng_consumer_set_command_sock_path(
1051 struct lttng_consumer_local_data
*ctx
, char *sock
)
1053 ctx
->consumer_command_sock_path
= sock
;
1057 * Send return code to the session daemon.
1058 * If the socket is not defined, we return 0, it is not a fatal error
1060 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1062 if (ctx
->consumer_error_socket
> 0) {
1063 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1064 sizeof(enum lttcomm_sessiond_command
));
1071 * Close all the tracefiles and stream fds and MUST be called when all
1072 * instances are destroyed i.e. when all threads were joined and are ended.
1074 void lttng_consumer_cleanup(void)
1076 struct lttng_ht_iter iter
;
1077 struct lttng_consumer_channel
*channel
;
1081 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1083 consumer_del_channel(channel
);
1088 lttng_ht_destroy(consumer_data
.channel_ht
);
1090 cleanup_relayd_ht();
1092 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1095 * This HT contains streams that are freed by either the metadata thread or
1096 * the data thread so we do *nothing* on the hash table and simply destroy
1099 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1103 * Called from signal handler.
1105 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1110 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1111 } while (ret
< 0 && errno
== EINTR
);
1112 if (ret
< 0 || ret
!= 1) {
1113 PERROR("write consumer quit");
1116 DBG("Consumer flag that it should quit");
1119 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1122 int outfd
= stream
->out_fd
;
1125 * This does a blocking write-and-wait on any page that belongs to the
1126 * subbuffer prior to the one we just wrote.
1127 * Don't care about error values, as these are just hints and ways to
1128 * limit the amount of page cache used.
1130 if (orig_offset
< stream
->max_sb_size
) {
1133 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1134 stream
->max_sb_size
,
1135 SYNC_FILE_RANGE_WAIT_BEFORE
1136 | SYNC_FILE_RANGE_WRITE
1137 | SYNC_FILE_RANGE_WAIT_AFTER
);
1139 * Give hints to the kernel about how we access the file:
1140 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1143 * We need to call fadvise again after the file grows because the
1144 * kernel does not seem to apply fadvise to non-existing parts of the
1147 * Call fadvise _after_ having waited for the page writeback to
1148 * complete because the dirty page writeback semantic is not well
1149 * defined. So it can be expected to lead to lower throughput in
1152 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1153 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1157 * Initialise the necessary environnement :
1158 * - create a new context
1159 * - create the poll_pipe
1160 * - create the should_quit pipe (for signal handler)
1161 * - create the thread pipe (for splice)
1163 * Takes a function pointer as argument, this function is called when data is
1164 * available on a buffer. This function is responsible to do the
1165 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1166 * buffer configuration and then kernctl_put_next_subbuf at the end.
1168 * Returns a pointer to the new context or NULL on error.
1170 struct lttng_consumer_local_data
*lttng_consumer_create(
1171 enum lttng_consumer_type type
,
1172 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1173 struct lttng_consumer_local_data
*ctx
),
1174 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1175 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1176 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1179 struct lttng_consumer_local_data
*ctx
;
1181 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1182 consumer_data
.type
== type
);
1183 consumer_data
.type
= type
;
1185 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1187 PERROR("allocating context");
1191 ctx
->consumer_error_socket
= -1;
1192 ctx
->consumer_metadata_socket
= -1;
1193 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1194 /* assign the callbacks */
1195 ctx
->on_buffer_ready
= buffer_ready
;
1196 ctx
->on_recv_channel
= recv_channel
;
1197 ctx
->on_recv_stream
= recv_stream
;
1198 ctx
->on_update_stream
= update_stream
;
1200 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1201 if (!ctx
->consumer_data_pipe
) {
1202 goto error_poll_pipe
;
1205 ret
= pipe(ctx
->consumer_should_quit
);
1207 PERROR("Error creating recv pipe");
1208 goto error_quit_pipe
;
1211 ret
= pipe(ctx
->consumer_thread_pipe
);
1213 PERROR("Error creating thread pipe");
1214 goto error_thread_pipe
;
1217 ret
= pipe(ctx
->consumer_channel_pipe
);
1219 PERROR("Error creating channel pipe");
1220 goto error_channel_pipe
;
1223 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1224 if (!ctx
->consumer_metadata_pipe
) {
1225 goto error_metadata_pipe
;
1228 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1230 goto error_splice_pipe
;
1236 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1237 error_metadata_pipe
:
1238 utils_close_pipe(ctx
->consumer_channel_pipe
);
1240 utils_close_pipe(ctx
->consumer_thread_pipe
);
1242 utils_close_pipe(ctx
->consumer_should_quit
);
1244 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1252 * Iterate over all streams of the hashtable and free them properly.
1254 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1256 struct lttng_ht_iter iter
;
1257 struct lttng_consumer_stream
*stream
;
1264 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1266 * Ignore return value since we are currently cleaning up so any error
1269 (void) consumer_del_stream(stream
, ht
);
1273 lttng_ht_destroy(ht
);
1277 * Iterate over all streams of the metadata hashtable and free them
1280 static void destroy_metadata_stream_ht(struct lttng_ht
*ht
)
1282 struct lttng_ht_iter iter
;
1283 struct lttng_consumer_stream
*stream
;
1290 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1292 * Ignore return value since we are currently cleaning up so any error
1295 (void) consumer_del_metadata_stream(stream
, ht
);
1299 lttng_ht_destroy(ht
);
1303 * Close all fds associated with the instance and free the context.
1305 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1309 DBG("Consumer destroying it. Closing everything.");
1311 destroy_data_stream_ht(data_ht
);
1312 destroy_metadata_stream_ht(metadata_ht
);
1314 ret
= close(ctx
->consumer_error_socket
);
1318 ret
= close(ctx
->consumer_metadata_socket
);
1322 utils_close_pipe(ctx
->consumer_thread_pipe
);
1323 utils_close_pipe(ctx
->consumer_channel_pipe
);
1324 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1325 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1326 utils_close_pipe(ctx
->consumer_should_quit
);
1327 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1329 unlink(ctx
->consumer_command_sock_path
);
1334 * Write the metadata stream id on the specified file descriptor.
1336 static int write_relayd_metadata_id(int fd
,
1337 struct lttng_consumer_stream
*stream
,
1338 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1341 struct lttcomm_relayd_metadata_payload hdr
;
1343 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1344 hdr
.padding_size
= htobe32(padding
);
1346 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1347 } while (ret
< 0 && errno
== EINTR
);
1348 if (ret
< 0 || ret
!= sizeof(hdr
)) {
1350 * This error means that the fd's end is closed so ignore the perror
1351 * not to clubber the error output since this can happen in a normal
1354 if (errno
!= EPIPE
) {
1355 PERROR("write metadata stream id");
1357 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1359 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1360 * handle writting the missing part so report that as an error and
1361 * don't lie to the caller.
1366 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1367 stream
->relayd_stream_id
, padding
);
1374 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1375 * core function for writing trace buffers to either the local filesystem or
1378 * It must be called with the stream lock held.
1380 * Careful review MUST be put if any changes occur!
1382 * Returns the number of bytes written
1384 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1385 struct lttng_consumer_local_data
*ctx
,
1386 struct lttng_consumer_stream
*stream
, unsigned long len
,
1387 unsigned long padding
)
1389 unsigned long mmap_offset
;
1391 ssize_t ret
= 0, written
= 0;
1392 off_t orig_offset
= stream
->out_fd_offset
;
1393 /* Default is on the disk */
1394 int outfd
= stream
->out_fd
;
1395 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1396 unsigned int relayd_hang_up
= 0;
1398 /* RCU lock for the relayd pointer */
1401 /* Flag that the current stream if set for network streaming. */
1402 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1403 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1404 if (relayd
== NULL
) {
1410 /* get the offset inside the fd to mmap */
1411 switch (consumer_data
.type
) {
1412 case LTTNG_CONSUMER_KERNEL
:
1413 mmap_base
= stream
->mmap_base
;
1414 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1416 PERROR("tracer ctl get_mmap_read_offset");
1421 case LTTNG_CONSUMER32_UST
:
1422 case LTTNG_CONSUMER64_UST
:
1423 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1425 ERR("read mmap get mmap base for stream %s", stream
->name
);
1429 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1431 PERROR("tracer ctl get_mmap_read_offset");
1437 ERR("Unknown consumer_data type");
1441 /* Handle stream on the relayd if the output is on the network */
1443 unsigned long netlen
= len
;
1446 * Lock the control socket for the complete duration of the function
1447 * since from this point on we will use the socket.
1449 if (stream
->metadata_flag
) {
1450 /* Metadata requires the control socket. */
1451 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1452 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1455 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1457 /* Use the returned socket. */
1460 /* Write metadata stream id before payload */
1461 if (stream
->metadata_flag
) {
1462 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1465 /* Socket operation failed. We consider the relayd dead */
1466 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1474 /* Socket operation failed. We consider the relayd dead */
1475 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1479 /* Else, use the default set before which is the filesystem. */
1482 /* No streaming, we have to set the len with the full padding */
1486 * Check if we need to change the tracefile before writing the packet.
1488 if (stream
->chan
->tracefile_size
> 0 &&
1489 (stream
->tracefile_size_current
+ len
) >
1490 stream
->chan
->tracefile_size
) {
1491 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1492 stream
->name
, stream
->chan
->tracefile_size
,
1493 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1494 stream
->out_fd
, &(stream
->tracefile_count_current
));
1496 ERR("Rotating output file");
1499 outfd
= stream
->out_fd
= ret
;
1500 /* Reset current size because we just perform a rotation. */
1501 stream
->tracefile_size_current
= 0;
1502 stream
->out_fd_offset
= 0;
1505 stream
->tracefile_size_current
+= len
;
1510 ret
= write(outfd
, mmap_base
+ mmap_offset
, len
);
1511 } while (ret
< 0 && errno
== EINTR
);
1512 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1515 * This is possible if the fd is closed on the other side (outfd)
1516 * or any write problem. It can be verbose a bit for a normal
1517 * execution if for instance the relayd is stopped abruptly. This
1518 * can happen so set this to a DBG statement.
1520 DBG("Error in file write mmap");
1524 /* Socket operation failed. We consider the relayd dead */
1525 if (errno
== EPIPE
|| errno
== EINVAL
) {
1530 } else if (ret
> len
) {
1531 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1539 /* This call is useless on a socket so better save a syscall. */
1541 /* This won't block, but will start writeout asynchronously */
1542 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1543 SYNC_FILE_RANGE_WRITE
);
1544 stream
->out_fd_offset
+= ret
;
1546 stream
->output_written
+= ret
;
1549 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1553 * This is a special case that the relayd has closed its socket. Let's
1554 * cleanup the relayd object and all associated streams.
1556 if (relayd
&& relayd_hang_up
) {
1557 cleanup_relayd(relayd
, ctx
);
1561 /* Unlock only if ctrl socket used */
1562 if (relayd
&& stream
->metadata_flag
) {
1563 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1571 * Splice the data from the ring buffer to the tracefile.
1573 * It must be called with the stream lock held.
1575 * Returns the number of bytes spliced.
1577 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1578 struct lttng_consumer_local_data
*ctx
,
1579 struct lttng_consumer_stream
*stream
, unsigned long len
,
1580 unsigned long padding
)
1582 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1584 off_t orig_offset
= stream
->out_fd_offset
;
1585 int fd
= stream
->wait_fd
;
1586 /* Default is on the disk */
1587 int outfd
= stream
->out_fd
;
1588 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1590 unsigned int relayd_hang_up
= 0;
1592 switch (consumer_data
.type
) {
1593 case LTTNG_CONSUMER_KERNEL
:
1595 case LTTNG_CONSUMER32_UST
:
1596 case LTTNG_CONSUMER64_UST
:
1597 /* Not supported for user space tracing */
1600 ERR("Unknown consumer_data type");
1604 /* RCU lock for the relayd pointer */
1607 /* Flag that the current stream if set for network streaming. */
1608 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1609 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1610 if (relayd
== NULL
) {
1617 * Choose right pipe for splice. Metadata and trace data are handled by
1618 * different threads hence the use of two pipes in order not to race or
1619 * corrupt the written data.
1621 if (stream
->metadata_flag
) {
1622 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1624 splice_pipe
= ctx
->consumer_thread_pipe
;
1627 /* Write metadata stream id before payload */
1629 int total_len
= len
;
1631 if (stream
->metadata_flag
) {
1633 * Lock the control socket for the complete duration of the function
1634 * since from this point on we will use the socket.
1636 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1638 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1642 /* Socket operation failed. We consider the relayd dead */
1643 if (ret
== -EBADF
) {
1644 WARN("Remote relayd disconnected. Stopping");
1651 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1654 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1656 /* Use the returned socket. */
1659 /* Socket operation failed. We consider the relayd dead */
1660 if (ret
== -EBADF
) {
1661 WARN("Remote relayd disconnected. Stopping");
1668 /* No streaming, we have to set the len with the full padding */
1672 * Check if we need to change the tracefile before writing the packet.
1674 if (stream
->chan
->tracefile_size
> 0 &&
1675 (stream
->tracefile_size_current
+ len
) >
1676 stream
->chan
->tracefile_size
) {
1677 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1678 stream
->name
, stream
->chan
->tracefile_size
,
1679 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1680 stream
->out_fd
, &(stream
->tracefile_count_current
));
1682 ERR("Rotating output file");
1685 outfd
= stream
->out_fd
= ret
;
1686 /* Reset current size because we just perform a rotation. */
1687 stream
->tracefile_size_current
= 0;
1688 stream
->out_fd_offset
= 0;
1691 stream
->tracefile_size_current
+= len
;
1695 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1696 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1697 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1698 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1699 DBG("splice chan to pipe, ret %zd", ret_splice
);
1700 if (ret_splice
< 0) {
1701 PERROR("Error in relay splice");
1703 written
= ret_splice
;
1709 /* Handle stream on the relayd if the output is on the network */
1711 if (stream
->metadata_flag
) {
1712 size_t metadata_payload_size
=
1713 sizeof(struct lttcomm_relayd_metadata_payload
);
1715 /* Update counter to fit the spliced data */
1716 ret_splice
+= metadata_payload_size
;
1717 len
+= metadata_payload_size
;
1719 * We do this so the return value can match the len passed as
1720 * argument to this function.
1722 written
-= metadata_payload_size
;
1726 /* Splice data out */
1727 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1728 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1729 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1730 if (ret_splice
< 0) {
1731 PERROR("Error in file splice");
1733 written
= ret_splice
;
1735 /* Socket operation failed. We consider the relayd dead */
1736 if (errno
== EBADF
|| errno
== EPIPE
) {
1737 WARN("Remote relayd disconnected. Stopping");
1743 } else if (ret_splice
> len
) {
1745 PERROR("Wrote more data than requested %zd (len: %lu)",
1747 written
+= ret_splice
;
1753 /* This call is useless on a socket so better save a syscall. */
1755 /* This won't block, but will start writeout asynchronously */
1756 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1757 SYNC_FILE_RANGE_WRITE
);
1758 stream
->out_fd_offset
+= ret_splice
;
1760 stream
->output_written
+= ret_splice
;
1761 written
+= ret_splice
;
1763 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1771 * This is a special case that the relayd has closed its socket. Let's
1772 * cleanup the relayd object and all associated streams.
1774 if (relayd
&& relayd_hang_up
) {
1775 cleanup_relayd(relayd
, ctx
);
1776 /* Skip splice error so the consumer does not fail */
1781 /* send the appropriate error description to sessiond */
1784 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1787 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1790 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1795 if (relayd
&& stream
->metadata_flag
) {
1796 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1804 * Take a snapshot for a specific fd
1806 * Returns 0 on success, < 0 on error
1808 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1810 switch (consumer_data
.type
) {
1811 case LTTNG_CONSUMER_KERNEL
:
1812 return lttng_kconsumer_take_snapshot(stream
);
1813 case LTTNG_CONSUMER32_UST
:
1814 case LTTNG_CONSUMER64_UST
:
1815 return lttng_ustconsumer_take_snapshot(stream
);
1817 ERR("Unknown consumer_data type");
1824 * Get the produced position
1826 * Returns 0 on success, < 0 on error
1828 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1831 switch (consumer_data
.type
) {
1832 case LTTNG_CONSUMER_KERNEL
:
1833 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1834 case LTTNG_CONSUMER32_UST
:
1835 case LTTNG_CONSUMER64_UST
:
1836 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1838 ERR("Unknown consumer_data type");
1844 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1845 int sock
, struct pollfd
*consumer_sockpoll
)
1847 switch (consumer_data
.type
) {
1848 case LTTNG_CONSUMER_KERNEL
:
1849 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1850 case LTTNG_CONSUMER32_UST
:
1851 case LTTNG_CONSUMER64_UST
:
1852 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1854 ERR("Unknown consumer_data type");
1860 void lttng_consumer_close_metadata(void)
1862 switch (consumer_data
.type
) {
1863 case LTTNG_CONSUMER_KERNEL
:
1865 * The Kernel consumer has a different metadata scheme so we don't
1866 * close anything because the stream will be closed by the session
1870 case LTTNG_CONSUMER32_UST
:
1871 case LTTNG_CONSUMER64_UST
:
1873 * Close all metadata streams. The metadata hash table is passed and
1874 * this call iterates over it by closing all wakeup fd. This is safe
1875 * because at this point we are sure that the metadata producer is
1876 * either dead or blocked.
1878 lttng_ustconsumer_close_metadata(metadata_ht
);
1881 ERR("Unknown consumer_data type");
1887 * Clean up a metadata stream and free its memory.
1889 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1890 struct lttng_ht
*ht
)
1893 struct lttng_ht_iter iter
;
1894 struct lttng_consumer_channel
*free_chan
= NULL
;
1895 struct consumer_relayd_sock_pair
*relayd
;
1899 * This call should NEVER receive regular stream. It must always be
1900 * metadata stream and this is crucial for data structure synchronization.
1902 assert(stream
->metadata_flag
);
1904 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1907 /* Means the stream was allocated but not successfully added */
1908 goto free_stream_rcu
;
1911 pthread_mutex_lock(&consumer_data
.lock
);
1912 pthread_mutex_lock(&stream
->chan
->lock
);
1913 pthread_mutex_lock(&stream
->lock
);
1915 switch (consumer_data
.type
) {
1916 case LTTNG_CONSUMER_KERNEL
:
1917 if (stream
->mmap_base
!= NULL
) {
1918 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1920 PERROR("munmap metadata stream");
1923 if (stream
->wait_fd
>= 0) {
1924 ret
= close(stream
->wait_fd
);
1926 PERROR("close kernel metadata wait_fd");
1930 case LTTNG_CONSUMER32_UST
:
1931 case LTTNG_CONSUMER64_UST
:
1932 if (stream
->monitor
) {
1933 /* close the write-side in close_metadata */
1934 ret
= close(stream
->ust_metadata_poll_pipe
[0]);
1936 PERROR("Close UST metadata read-side poll pipe");
1939 lttng_ustconsumer_del_stream(stream
);
1942 ERR("Unknown consumer_data type");
1948 iter
.iter
.node
= &stream
->node
.node
;
1949 ret
= lttng_ht_del(ht
, &iter
);
1952 iter
.iter
.node
= &stream
->node_channel_id
.node
;
1953 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
1956 iter
.iter
.node
= &stream
->node_session_id
.node
;
1957 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1961 if (stream
->out_fd
>= 0) {
1962 ret
= close(stream
->out_fd
);
1968 /* Check and cleanup relayd */
1970 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1971 if (relayd
!= NULL
) {
1972 uatomic_dec(&relayd
->refcount
);
1973 assert(uatomic_read(&relayd
->refcount
) >= 0);
1975 /* Closing streams requires to lock the control socket. */
1976 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1977 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1978 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1979 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1981 DBG("Unable to close stream on the relayd. Continuing");
1983 * Continue here. There is nothing we can do for the relayd.
1984 * Chances are that the relayd has closed the socket so we just
1985 * continue cleaning up.
1989 /* Both conditions are met, we destroy the relayd. */
1990 if (uatomic_read(&relayd
->refcount
) == 0 &&
1991 uatomic_read(&relayd
->destroy_flag
)) {
1992 consumer_destroy_relayd(relayd
);
1997 /* Atomically decrement channel refcount since other threads can use it. */
1998 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
1999 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
2000 /* Go for channel deletion! */
2001 free_chan
= stream
->chan
;
2006 * Nullify the stream reference so it is not used after deletion. The
2007 * channel lock MUST be acquired before being able to check for
2008 * a NULL pointer value.
2010 stream
->chan
->metadata_stream
= NULL
;
2012 pthread_mutex_unlock(&stream
->lock
);
2013 pthread_mutex_unlock(&stream
->chan
->lock
);
2014 pthread_mutex_unlock(&consumer_data
.lock
);
2017 consumer_del_channel(free_chan
);
2021 call_rcu(&stream
->node
.head
, free_stream_rcu
);
2025 * Action done with the metadata stream when adding it to the consumer internal
2026 * data structures to handle it.
2028 int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2030 struct lttng_ht
*ht
= metadata_ht
;
2032 struct lttng_ht_iter iter
;
2033 struct lttng_ht_node_u64
*node
;
2038 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2040 pthread_mutex_lock(&consumer_data
.lock
);
2041 pthread_mutex_lock(&stream
->chan
->lock
);
2042 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2043 pthread_mutex_lock(&stream
->lock
);
2046 * From here, refcounts are updated so be _careful_ when returning an error
2053 * Lookup the stream just to make sure it does not exist in our internal
2054 * state. This should NEVER happen.
2056 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2057 node
= lttng_ht_iter_get_node_u64(&iter
);
2061 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2062 * in terms of destroying the associated channel, because the action that
2063 * causes the count to become 0 also causes a stream to be added. The
2064 * channel deletion will thus be triggered by the following removal of this
2067 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2068 /* Increment refcount before decrementing nb_init_stream_left */
2070 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2073 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2075 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2076 &stream
->node_channel_id
);
2079 * Add stream to the stream_list_ht of the consumer data. No need to steal
2080 * the key since the HT does not use it and we allow to add redundant keys
2083 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2087 pthread_mutex_unlock(&stream
->lock
);
2088 pthread_mutex_unlock(&stream
->chan
->lock
);
2089 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2090 pthread_mutex_unlock(&consumer_data
.lock
);
2095 * Delete data stream that are flagged for deletion (endpoint_status).
2097 static void validate_endpoint_status_data_stream(void)
2099 struct lttng_ht_iter iter
;
2100 struct lttng_consumer_stream
*stream
;
2102 DBG("Consumer delete flagged data stream");
2105 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2106 /* Validate delete flag of the stream */
2107 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2110 /* Delete it right now */
2111 consumer_del_stream(stream
, data_ht
);
2117 * Delete metadata stream that are flagged for deletion (endpoint_status).
2119 static void validate_endpoint_status_metadata_stream(
2120 struct lttng_poll_event
*pollset
)
2122 struct lttng_ht_iter iter
;
2123 struct lttng_consumer_stream
*stream
;
2125 DBG("Consumer delete flagged metadata stream");
2130 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2131 /* Validate delete flag of the stream */
2132 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2136 * Remove from pollset so the metadata thread can continue without
2137 * blocking on a deleted stream.
2139 lttng_poll_del(pollset
, stream
->wait_fd
);
2141 /* Delete it right now */
2142 consumer_del_metadata_stream(stream
, metadata_ht
);
2148 * Thread polls on metadata file descriptor and write them on disk or on the
2151 void *consumer_thread_metadata_poll(void *data
)
2154 uint32_t revents
, nb_fd
;
2155 struct lttng_consumer_stream
*stream
= NULL
;
2156 struct lttng_ht_iter iter
;
2157 struct lttng_ht_node_u64
*node
;
2158 struct lttng_poll_event events
;
2159 struct lttng_consumer_local_data
*ctx
= data
;
2162 rcu_register_thread();
2164 DBG("Thread metadata poll started");
2166 /* Size is set to 1 for the consumer_metadata pipe */
2167 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2169 ERR("Poll set creation failed");
2173 ret
= lttng_poll_add(&events
,
2174 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2180 DBG("Metadata main loop started");
2183 /* Only the metadata pipe is set */
2184 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2189 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2190 ret
= lttng_poll_wait(&events
, -1);
2191 DBG("Metadata event catched in thread");
2193 if (errno
== EINTR
) {
2194 ERR("Poll EINTR catched");
2202 /* From here, the event is a metadata wait fd */
2203 for (i
= 0; i
< nb_fd
; i
++) {
2204 revents
= LTTNG_POLL_GETEV(&events
, i
);
2205 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2207 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2208 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2209 DBG("Metadata thread pipe hung up");
2211 * Remove the pipe from the poll set and continue the loop
2212 * since their might be data to consume.
2214 lttng_poll_del(&events
,
2215 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2216 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2218 } else if (revents
& LPOLLIN
) {
2221 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2222 &stream
, sizeof(stream
));
2224 ERR("read metadata stream, ret: %zd", pipe_len
);
2226 * Continue here to handle the rest of the streams.
2231 /* A NULL stream means that the state has changed. */
2232 if (stream
== NULL
) {
2233 /* Check for deleted streams. */
2234 validate_endpoint_status_metadata_stream(&events
);
2238 DBG("Adding metadata stream %d to poll set",
2241 /* Add metadata stream to the global poll events list */
2242 lttng_poll_add(&events
, stream
->wait_fd
,
2243 LPOLLIN
| LPOLLPRI
);
2246 /* Handle other stream */
2252 uint64_t tmp_id
= (uint64_t) pollfd
;
2254 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2256 node
= lttng_ht_iter_get_node_u64(&iter
);
2259 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2262 /* Check for error event */
2263 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2264 DBG("Metadata fd %d is hup|err.", pollfd
);
2265 if (!stream
->hangup_flush_done
2266 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2267 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2268 DBG("Attempting to flush and consume the UST buffers");
2269 lttng_ustconsumer_on_stream_hangup(stream
);
2271 /* We just flushed the stream now read it. */
2273 len
= ctx
->on_buffer_ready(stream
, ctx
);
2275 * We don't check the return value here since if we get
2276 * a negative len, it means an error occured thus we
2277 * simply remove it from the poll set and free the
2283 lttng_poll_del(&events
, stream
->wait_fd
);
2285 * This call update the channel states, closes file descriptors
2286 * and securely free the stream.
2288 consumer_del_metadata_stream(stream
, metadata_ht
);
2289 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2290 /* Get the data out of the metadata file descriptor */
2291 DBG("Metadata available on fd %d", pollfd
);
2292 assert(stream
->wait_fd
== pollfd
);
2295 len
= ctx
->on_buffer_ready(stream
, ctx
);
2297 * We don't check the return value here since if we get
2298 * a negative len, it means an error occured thus we
2299 * simply remove it from the poll set and free the
2304 /* It's ok to have an unavailable sub-buffer */
2305 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2306 /* Clean up stream from consumer and free it. */
2307 lttng_poll_del(&events
, stream
->wait_fd
);
2308 consumer_del_metadata_stream(stream
, metadata_ht
);
2312 /* Release RCU lock for the stream looked up */
2319 DBG("Metadata poll thread exiting");
2321 lttng_poll_clean(&events
);
2323 rcu_unregister_thread();
2328 * This thread polls the fds in the set to consume the data and write
2329 * it to tracefile if necessary.
2331 void *consumer_thread_data_poll(void *data
)
2333 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2334 struct pollfd
*pollfd
= NULL
;
2335 /* local view of the streams */
2336 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2337 /* local view of consumer_data.fds_count */
2339 struct lttng_consumer_local_data
*ctx
= data
;
2342 rcu_register_thread();
2344 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2345 if (local_stream
== NULL
) {
2346 PERROR("local_stream malloc");
2355 * the fds set has been updated, we need to update our
2356 * local array as well
2358 pthread_mutex_lock(&consumer_data
.lock
);
2359 if (consumer_data
.need_update
) {
2364 local_stream
= NULL
;
2366 /* allocate for all fds + 1 for the consumer_data_pipe */
2367 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2368 if (pollfd
== NULL
) {
2369 PERROR("pollfd malloc");
2370 pthread_mutex_unlock(&consumer_data
.lock
);
2374 /* allocate for all fds + 1 for the consumer_data_pipe */
2375 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2376 sizeof(struct lttng_consumer_stream
*));
2377 if (local_stream
== NULL
) {
2378 PERROR("local_stream malloc");
2379 pthread_mutex_unlock(&consumer_data
.lock
);
2382 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2385 ERR("Error in allocating pollfd or local_outfds");
2386 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2387 pthread_mutex_unlock(&consumer_data
.lock
);
2391 consumer_data
.need_update
= 0;
2393 pthread_mutex_unlock(&consumer_data
.lock
);
2395 /* No FDs and consumer_quit, consumer_cleanup the thread */
2396 if (nb_fd
== 0 && consumer_quit
== 1) {
2399 /* poll on the array of fds */
2401 DBG("polling on %d fd", nb_fd
+ 1);
2402 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2403 DBG("poll num_rdy : %d", num_rdy
);
2404 if (num_rdy
== -1) {
2406 * Restart interrupted system call.
2408 if (errno
== EINTR
) {
2411 PERROR("Poll error");
2412 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2414 } else if (num_rdy
== 0) {
2415 DBG("Polling thread timed out");
2420 * If the consumer_data_pipe triggered poll go directly to the
2421 * beginning of the loop to update the array. We want to prioritize
2422 * array update over low-priority reads.
2424 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2425 ssize_t pipe_readlen
;
2427 DBG("consumer_data_pipe wake up");
2428 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2429 &new_stream
, sizeof(new_stream
));
2430 if (pipe_readlen
< 0) {
2431 ERR("Consumer data pipe ret %zd", pipe_readlen
);
2432 /* Continue so we can at least handle the current stream(s). */
2437 * If the stream is NULL, just ignore it. It's also possible that
2438 * the sessiond poll thread changed the consumer_quit state and is
2439 * waking us up to test it.
2441 if (new_stream
== NULL
) {
2442 validate_endpoint_status_data_stream();
2446 /* Continue to update the local streams and handle prio ones */
2450 /* Take care of high priority channels first. */
2451 for (i
= 0; i
< nb_fd
; i
++) {
2452 if (local_stream
[i
] == NULL
) {
2455 if (pollfd
[i
].revents
& POLLPRI
) {
2456 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2458 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2459 /* it's ok to have an unavailable sub-buffer */
2460 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2461 /* Clean the stream and free it. */
2462 consumer_del_stream(local_stream
[i
], data_ht
);
2463 local_stream
[i
] = NULL
;
2464 } else if (len
> 0) {
2465 local_stream
[i
]->data_read
= 1;
2471 * If we read high prio channel in this loop, try again
2472 * for more high prio data.
2478 /* Take care of low priority channels. */
2479 for (i
= 0; i
< nb_fd
; i
++) {
2480 if (local_stream
[i
] == NULL
) {
2483 if ((pollfd
[i
].revents
& POLLIN
) ||
2484 local_stream
[i
]->hangup_flush_done
) {
2485 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2486 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2487 /* it's ok to have an unavailable sub-buffer */
2488 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2489 /* Clean the stream and free it. */
2490 consumer_del_stream(local_stream
[i
], data_ht
);
2491 local_stream
[i
] = NULL
;
2492 } else if (len
> 0) {
2493 local_stream
[i
]->data_read
= 1;
2498 /* Handle hangup and errors */
2499 for (i
= 0; i
< nb_fd
; i
++) {
2500 if (local_stream
[i
] == NULL
) {
2503 if (!local_stream
[i
]->hangup_flush_done
2504 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2505 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2506 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2507 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2509 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2510 /* Attempt read again, for the data we just flushed. */
2511 local_stream
[i
]->data_read
= 1;
2514 * If the poll flag is HUP/ERR/NVAL and we have
2515 * read no data in this pass, we can remove the
2516 * stream from its hash table.
2518 if ((pollfd
[i
].revents
& POLLHUP
)) {
2519 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2520 if (!local_stream
[i
]->data_read
) {
2521 consumer_del_stream(local_stream
[i
], data_ht
);
2522 local_stream
[i
] = NULL
;
2525 } else if (pollfd
[i
].revents
& POLLERR
) {
2526 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2527 if (!local_stream
[i
]->data_read
) {
2528 consumer_del_stream(local_stream
[i
], data_ht
);
2529 local_stream
[i
] = NULL
;
2532 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2533 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2534 if (!local_stream
[i
]->data_read
) {
2535 consumer_del_stream(local_stream
[i
], data_ht
);
2536 local_stream
[i
] = NULL
;
2540 if (local_stream
[i
] != NULL
) {
2541 local_stream
[i
]->data_read
= 0;
2546 DBG("polling thread exiting");
2551 * Close the write side of the pipe so epoll_wait() in
2552 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2553 * read side of the pipe. If we close them both, epoll_wait strangely does
2554 * not return and could create a endless wait period if the pipe is the
2555 * only tracked fd in the poll set. The thread will take care of closing
2558 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2560 rcu_unregister_thread();
2565 * Close wake-up end of each stream belonging to the channel. This will
2566 * allow the poll() on the stream read-side to detect when the
2567 * write-side (application) finally closes them.
2570 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2572 struct lttng_ht
*ht
;
2573 struct lttng_consumer_stream
*stream
;
2574 struct lttng_ht_iter iter
;
2576 ht
= consumer_data
.stream_per_chan_id_ht
;
2579 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2580 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2581 ht
->match_fct
, &channel
->key
,
2582 &iter
.iter
, stream
, node_channel_id
.node
) {
2584 * Protect against teardown with mutex.
2586 pthread_mutex_lock(&stream
->lock
);
2587 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2590 switch (consumer_data
.type
) {
2591 case LTTNG_CONSUMER_KERNEL
:
2593 case LTTNG_CONSUMER32_UST
:
2594 case LTTNG_CONSUMER64_UST
:
2596 * Note: a mutex is taken internally within
2597 * liblttng-ust-ctl to protect timer wakeup_fd
2598 * use from concurrent close.
2600 lttng_ustconsumer_close_stream_wakeup(stream
);
2603 ERR("Unknown consumer_data type");
2607 pthread_mutex_unlock(&stream
->lock
);
2612 static void destroy_channel_ht(struct lttng_ht
*ht
)
2614 struct lttng_ht_iter iter
;
2615 struct lttng_consumer_channel
*channel
;
2623 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2624 ret
= lttng_ht_del(ht
, &iter
);
2629 lttng_ht_destroy(ht
);
2633 * This thread polls the channel fds to detect when they are being
2634 * closed. It closes all related streams if the channel is detected as
2635 * closed. It is currently only used as a shim layer for UST because the
2636 * consumerd needs to keep the per-stream wakeup end of pipes open for
2639 void *consumer_thread_channel_poll(void *data
)
2642 uint32_t revents
, nb_fd
;
2643 struct lttng_consumer_channel
*chan
= NULL
;
2644 struct lttng_ht_iter iter
;
2645 struct lttng_ht_node_u64
*node
;
2646 struct lttng_poll_event events
;
2647 struct lttng_consumer_local_data
*ctx
= data
;
2648 struct lttng_ht
*channel_ht
;
2650 rcu_register_thread();
2652 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2654 /* ENOMEM at this point. Better to bail out. */
2658 DBG("Thread channel poll started");
2660 /* Size is set to 1 for the consumer_channel pipe */
2661 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2663 ERR("Poll set creation failed");
2667 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2673 DBG("Channel main loop started");
2676 /* Only the channel pipe is set */
2677 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2682 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2683 ret
= lttng_poll_wait(&events
, -1);
2684 DBG("Channel event catched in thread");
2686 if (errno
== EINTR
) {
2687 ERR("Poll EINTR catched");
2695 /* From here, the event is a channel wait fd */
2696 for (i
= 0; i
< nb_fd
; i
++) {
2697 revents
= LTTNG_POLL_GETEV(&events
, i
);
2698 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2700 /* Just don't waste time if no returned events for the fd */
2704 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2705 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2706 DBG("Channel thread pipe hung up");
2708 * Remove the pipe from the poll set and continue the loop
2709 * since their might be data to consume.
2711 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2713 } else if (revents
& LPOLLIN
) {
2714 enum consumer_channel_action action
;
2717 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2719 ERR("Error reading channel pipe");
2724 case CONSUMER_CHANNEL_ADD
:
2725 DBG("Adding channel %d to poll set",
2728 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2731 lttng_ht_add_unique_u64(channel_ht
,
2732 &chan
->wait_fd_node
);
2734 /* Add channel to the global poll events list */
2735 lttng_poll_add(&events
, chan
->wait_fd
,
2736 LPOLLIN
| LPOLLPRI
);
2738 case CONSUMER_CHANNEL_DEL
:
2740 struct lttng_consumer_stream
*stream
, *stmp
;
2743 chan
= consumer_find_channel(key
);
2746 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2749 lttng_poll_del(&events
, chan
->wait_fd
);
2750 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2751 ret
= lttng_ht_del(channel_ht
, &iter
);
2753 consumer_close_channel_streams(chan
);
2755 switch (consumer_data
.type
) {
2756 case LTTNG_CONSUMER_KERNEL
:
2758 case LTTNG_CONSUMER32_UST
:
2759 case LTTNG_CONSUMER64_UST
:
2760 /* Delete streams that might have been left in the stream list. */
2761 cds_list_for_each_entry_safe(stream
, stmp
, &chan
->streams
.head
,
2763 cds_list_del(&stream
->send_node
);
2764 lttng_ustconsumer_del_stream(stream
);
2765 uatomic_sub(&stream
->chan
->refcount
, 1);
2766 assert(&chan
->refcount
);
2771 ERR("Unknown consumer_data type");
2776 * Release our own refcount. Force channel deletion even if
2777 * streams were not initialized.
2779 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2780 consumer_del_channel(chan
);
2785 case CONSUMER_CHANNEL_QUIT
:
2787 * Remove the pipe from the poll set and continue the loop
2788 * since their might be data to consume.
2790 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2793 ERR("Unknown action");
2798 /* Handle other stream */
2804 uint64_t tmp_id
= (uint64_t) pollfd
;
2806 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2808 node
= lttng_ht_iter_get_node_u64(&iter
);
2811 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2814 /* Check for error event */
2815 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2816 DBG("Channel fd %d is hup|err.", pollfd
);
2818 lttng_poll_del(&events
, chan
->wait_fd
);
2819 ret
= lttng_ht_del(channel_ht
, &iter
);
2821 consumer_close_channel_streams(chan
);
2823 /* Release our own refcount */
2824 if (!uatomic_sub_return(&chan
->refcount
, 1)
2825 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2826 consumer_del_channel(chan
);
2830 /* Release RCU lock for the channel looked up */
2836 lttng_poll_clean(&events
);
2838 destroy_channel_ht(channel_ht
);
2840 DBG("Channel poll thread exiting");
2841 rcu_unregister_thread();
2845 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2846 struct pollfd
*sockpoll
, int client_socket
)
2853 if (lttng_consumer_poll_socket(sockpoll
) < 0) {
2857 DBG("Metadata connection on client_socket");
2859 /* Blocking call, waiting for transmission */
2860 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2861 if (ctx
->consumer_metadata_socket
< 0) {
2862 WARN("On accept metadata");
2873 * This thread listens on the consumerd socket and receives the file
2874 * descriptors from the session daemon.
2876 void *consumer_thread_sessiond_poll(void *data
)
2878 int sock
= -1, client_socket
, ret
;
2880 * structure to poll for incoming data on communication socket avoids
2881 * making blocking sockets.
2883 struct pollfd consumer_sockpoll
[2];
2884 struct lttng_consumer_local_data
*ctx
= data
;
2886 rcu_register_thread();
2888 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2889 unlink(ctx
->consumer_command_sock_path
);
2890 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2891 if (client_socket
< 0) {
2892 ERR("Cannot create command socket");
2896 ret
= lttcomm_listen_unix_sock(client_socket
);
2901 DBG("Sending ready command to lttng-sessiond");
2902 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2903 /* return < 0 on error, but == 0 is not fatal */
2905 ERR("Error sending ready command to lttng-sessiond");
2909 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2910 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2911 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2912 consumer_sockpoll
[1].fd
= client_socket
;
2913 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2915 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2918 DBG("Connection on client_socket");
2920 /* Blocking call, waiting for transmission */
2921 sock
= lttcomm_accept_unix_sock(client_socket
);
2928 * Setup metadata socket which is the second socket connection on the
2929 * command unix socket.
2931 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
2936 /* This socket is not useful anymore. */
2937 ret
= close(client_socket
);
2939 PERROR("close client_socket");
2943 /* update the polling structure to poll on the established socket */
2944 consumer_sockpoll
[1].fd
= sock
;
2945 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2948 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2951 DBG("Incoming command on sock");
2952 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2953 if (ret
== -ENOENT
) {
2954 DBG("Received STOP command");
2959 * This could simply be a session daemon quitting. Don't output
2962 DBG("Communication interrupted on command socket");
2965 if (consumer_quit
) {
2966 DBG("consumer_thread_receive_fds received quit from signal");
2969 DBG("received command on sock");
2972 DBG("Consumer thread sessiond poll exiting");
2975 * Close metadata streams since the producer is the session daemon which
2978 * NOTE: for now, this only applies to the UST tracer.
2980 lttng_consumer_close_metadata();
2983 * when all fds have hung up, the polling thread
2989 * Notify the data poll thread to poll back again and test the
2990 * consumer_quit state that we just set so to quit gracefully.
2992 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
2994 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
2996 /* Cleaning up possibly open sockets. */
3000 PERROR("close sock sessiond poll");
3003 if (client_socket
>= 0) {
3004 ret
= close(client_socket
);
3006 PERROR("close client_socket sessiond poll");
3010 rcu_unregister_thread();
3014 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3015 struct lttng_consumer_local_data
*ctx
)
3019 pthread_mutex_lock(&stream
->lock
);
3021 switch (consumer_data
.type
) {
3022 case LTTNG_CONSUMER_KERNEL
:
3023 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3025 case LTTNG_CONSUMER32_UST
:
3026 case LTTNG_CONSUMER64_UST
:
3027 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3030 ERR("Unknown consumer_data type");
3036 pthread_mutex_unlock(&stream
->lock
);
3040 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3042 switch (consumer_data
.type
) {
3043 case LTTNG_CONSUMER_KERNEL
:
3044 return lttng_kconsumer_on_recv_stream(stream
);
3045 case LTTNG_CONSUMER32_UST
:
3046 case LTTNG_CONSUMER64_UST
:
3047 return lttng_ustconsumer_on_recv_stream(stream
);
3049 ERR("Unknown consumer_data type");
3056 * Allocate and set consumer data hash tables.
3058 int lttng_consumer_init(void)
3060 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3061 if (!consumer_data
.channel_ht
) {
3065 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3066 if (!consumer_data
.relayd_ht
) {
3070 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3071 if (!consumer_data
.stream_list_ht
) {
3075 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3076 if (!consumer_data
.stream_per_chan_id_ht
) {
3080 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3085 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3097 * Process the ADD_RELAYD command receive by a consumer.
3099 * This will create a relayd socket pair and add it to the relayd hash table.
3100 * The caller MUST acquire a RCU read side lock before calling it.
3102 int consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3103 struct lttng_consumer_local_data
*ctx
, int sock
,
3104 struct pollfd
*consumer_sockpoll
,
3105 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
)
3107 int fd
= -1, ret
= -1, relayd_created
= 0;
3108 enum lttcomm_return_code ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3109 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3112 assert(relayd_sock
);
3114 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3116 /* Get relayd reference if exists. */
3117 relayd
= consumer_find_relayd(net_seq_idx
);
3118 if (relayd
== NULL
) {
3119 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3120 /* Not found. Allocate one. */
3121 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3122 if (relayd
== NULL
) {
3124 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3127 relayd
->sessiond_session_id
= sessiond_id
;
3132 * This code path MUST continue to the consumer send status message to
3133 * we can notify the session daemon and continue our work without
3134 * killing everything.
3138 * relayd key should never be found for control socket.
3140 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3143 /* First send a status message before receiving the fds. */
3144 ret
= consumer_send_status_msg(sock
, LTTCOMM_CONSUMERD_SUCCESS
);
3146 /* Somehow, the session daemon is not responding anymore. */
3147 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3148 goto error_nosignal
;
3151 /* Poll on consumer socket. */
3152 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
3153 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3155 goto error_nosignal
;
3158 /* Get relayd socket from session daemon */
3159 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3160 if (ret
!= sizeof(fd
)) {
3162 fd
= -1; /* Just in case it gets set with an invalid value. */
3165 * Failing to receive FDs might indicate a major problem such as
3166 * reaching a fd limit during the receive where the kernel returns a
3167 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3168 * don't take any chances and stop everything.
3170 * XXX: Feature request #558 will fix that and avoid this possible
3171 * issue when reaching the fd limit.
3173 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3174 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3178 /* Copy socket information and received FD */
3179 switch (sock_type
) {
3180 case LTTNG_STREAM_CONTROL
:
3181 /* Copy received lttcomm socket */
3182 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3183 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3184 /* Handle create_sock error. */
3186 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3190 * Close the socket created internally by
3191 * lttcomm_create_sock, so we can replace it by the one
3192 * received from sessiond.
3194 if (close(relayd
->control_sock
.sock
.fd
)) {
3198 /* Assign new file descriptor */
3199 relayd
->control_sock
.sock
.fd
= fd
;
3200 fd
= -1; /* For error path */
3201 /* Assign version values. */
3202 relayd
->control_sock
.major
= relayd_sock
->major
;
3203 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3206 * Create a session on the relayd and store the returned id. Lock the
3207 * control socket mutex if the relayd was NOT created before.
3209 if (!relayd_created
) {
3210 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3212 ret
= relayd_create_session(&relayd
->control_sock
,
3213 &relayd
->relayd_session_id
);
3214 if (!relayd_created
) {
3215 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3219 * Close all sockets of a relayd object. It will be freed if it was
3220 * created at the error code path or else it will be garbage
3223 (void) relayd_close(&relayd
->control_sock
);
3224 (void) relayd_close(&relayd
->data_sock
);
3225 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
3230 case LTTNG_STREAM_DATA
:
3231 /* Copy received lttcomm socket */
3232 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3233 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3234 /* Handle create_sock error. */
3236 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3240 * Close the socket created internally by
3241 * lttcomm_create_sock, so we can replace it by the one
3242 * received from sessiond.
3244 if (close(relayd
->data_sock
.sock
.fd
)) {
3248 /* Assign new file descriptor */
3249 relayd
->data_sock
.sock
.fd
= fd
;
3250 fd
= -1; /* for eventual error paths */
3251 /* Assign version values. */
3252 relayd
->data_sock
.major
= relayd_sock
->major
;
3253 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3256 ERR("Unknown relayd socket type (%d)", sock_type
);
3258 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3262 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3263 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3264 relayd
->net_seq_idx
, fd
);
3266 /* We successfully added the socket. Send status back. */
3267 ret
= consumer_send_status_msg(sock
, ret_code
);
3269 /* Somehow, the session daemon is not responding anymore. */
3270 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3271 goto error_nosignal
;
3275 * Add relayd socket pair to consumer data hashtable. If object already
3276 * exists or on error, the function gracefully returns.
3284 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3285 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3289 /* Close received socket if valid. */
3292 PERROR("close received socket");
3296 if (relayd_created
) {
3304 * Try to lock the stream mutex.
3306 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3308 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3315 * Try to lock the stream mutex. On failure, we know that the stream is
3316 * being used else where hence there is data still being extracted.
3318 ret
= pthread_mutex_trylock(&stream
->lock
);
3320 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3332 * Search for a relayd associated to the session id and return the reference.
3334 * A rcu read side lock MUST be acquire before calling this function and locked
3335 * until the relayd object is no longer necessary.
3337 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3339 struct lttng_ht_iter iter
;
3340 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3342 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3343 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3346 * Check by sessiond id which is unique here where the relayd session
3347 * id might not be when having multiple relayd.
3349 if (relayd
->sessiond_session_id
== id
) {
3350 /* Found the relayd. There can be only one per id. */
3362 * Check if for a given session id there is still data needed to be extract
3365 * Return 1 if data is pending or else 0 meaning ready to be read.
3367 int consumer_data_pending(uint64_t id
)
3370 struct lttng_ht_iter iter
;
3371 struct lttng_ht
*ht
;
3372 struct lttng_consumer_stream
*stream
;
3373 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3374 int (*data_pending
)(struct lttng_consumer_stream
*);
3376 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3379 pthread_mutex_lock(&consumer_data
.lock
);
3381 switch (consumer_data
.type
) {
3382 case LTTNG_CONSUMER_KERNEL
:
3383 data_pending
= lttng_kconsumer_data_pending
;
3385 case LTTNG_CONSUMER32_UST
:
3386 case LTTNG_CONSUMER64_UST
:
3387 data_pending
= lttng_ustconsumer_data_pending
;
3390 ERR("Unknown consumer data type");
3394 /* Ease our life a bit */
3395 ht
= consumer_data
.stream_list_ht
;
3397 relayd
= find_relayd_by_session_id(id
);
3399 /* Send init command for data pending. */
3400 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3401 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3402 relayd
->relayd_session_id
);
3403 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3405 /* Communication error thus the relayd so no data pending. */
3406 goto data_not_pending
;
3410 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3411 ht
->hash_fct(&id
, lttng_ht_seed
),
3413 &iter
.iter
, stream
, node_session_id
.node
) {
3414 /* If this call fails, the stream is being used hence data pending. */
3415 ret
= stream_try_lock(stream
);
3421 * A removed node from the hash table indicates that the stream has
3422 * been deleted thus having a guarantee that the buffers are closed
3423 * on the consumer side. However, data can still be transmitted
3424 * over the network so don't skip the relayd check.
3426 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3429 * An empty output file is not valid. We need at least one packet
3430 * generated per stream, even if it contains no event, so it
3431 * contains at least one packet header.
3433 if (stream
->output_written
== 0) {
3434 pthread_mutex_unlock(&stream
->lock
);
3437 /* Check the stream if there is data in the buffers. */
3438 ret
= data_pending(stream
);
3440 pthread_mutex_unlock(&stream
->lock
);
3447 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3448 if (stream
->metadata_flag
) {
3449 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3450 stream
->relayd_stream_id
);
3452 ret
= relayd_data_pending(&relayd
->control_sock
,
3453 stream
->relayd_stream_id
,
3454 stream
->next_net_seq_num
- 1);
3456 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3458 pthread_mutex_unlock(&stream
->lock
);
3462 pthread_mutex_unlock(&stream
->lock
);
3466 unsigned int is_data_inflight
= 0;
3468 /* Send init command for data pending. */
3469 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3470 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3471 relayd
->relayd_session_id
, &is_data_inflight
);
3472 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3474 goto data_not_pending
;
3476 if (is_data_inflight
) {
3482 * Finding _no_ node in the hash table and no inflight data means that the
3483 * stream(s) have been removed thus data is guaranteed to be available for
3484 * analysis from the trace files.
3488 /* Data is available to be read by a viewer. */
3489 pthread_mutex_unlock(&consumer_data
.lock
);
3494 /* Data is still being extracted from buffers. */
3495 pthread_mutex_unlock(&consumer_data
.lock
);
3501 * Send a ret code status message to the sessiond daemon.
3503 * Return the sendmsg() return value.
3505 int consumer_send_status_msg(int sock
, int ret_code
)
3507 struct lttcomm_consumer_status_msg msg
;
3509 msg
.ret_code
= ret_code
;
3511 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3515 * Send a channel status message to the sessiond daemon.
3517 * Return the sendmsg() return value.
3519 int consumer_send_status_channel(int sock
,
3520 struct lttng_consumer_channel
*channel
)
3522 struct lttcomm_consumer_status_channel msg
;
3527 msg
.ret_code
= LTTCOMM_CONSUMERD_CHANNEL_FAIL
;
3529 msg
.ret_code
= LTTCOMM_CONSUMERD_SUCCESS
;
3530 msg
.key
= channel
->key
;
3531 msg
.stream_count
= channel
->streams
.count
;
3534 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3538 * Using a maximum stream size with the produced and consumed position of a
3539 * stream, computes the new consumed position to be as close as possible to the
3540 * maximum possible stream size.
3542 * If maximum stream size is lower than the possible buffer size (produced -
3543 * consumed), the consumed_pos given is returned untouched else the new value
3546 unsigned long consumer_get_consumed_maxsize(unsigned long consumed_pos
,
3547 unsigned long produced_pos
, uint64_t max_stream_size
)
3549 if (max_stream_size
&& max_stream_size
< (produced_pos
- consumed_pos
)) {
3550 /* Offset from the produced position to get the latest buffers. */
3551 return produced_pos
- max_stream_size
;
3554 return consumed_pos
;