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>
43 #include <common/consumer-timer.h>
46 #include "consumer-stream.h"
48 struct lttng_consumer_global_data consumer_data
= {
51 .type
= LTTNG_CONSUMER_UNKNOWN
,
54 enum consumer_channel_action
{
57 CONSUMER_CHANNEL_QUIT
,
60 struct consumer_channel_msg
{
61 enum consumer_channel_action action
;
62 struct lttng_consumer_channel
*chan
; /* add */
63 uint64_t key
; /* del */
67 * Flag to inform the polling thread to quit when all fd hung up. Updated by
68 * the consumer_thread_receive_fds when it notices that all fds has hung up.
69 * Also updated by the signal handler (consumer_should_exit()). Read by the
72 volatile int consumer_quit
;
75 * Global hash table containing respectively metadata and data streams. The
76 * stream element in this ht should only be updated by the metadata poll thread
77 * for the metadata and the data poll thread for the data.
79 static struct lttng_ht
*metadata_ht
;
80 static struct lttng_ht
*data_ht
;
83 * Notify a thread lttng pipe to poll back again. This usually means that some
84 * global state has changed so we just send back the thread in a poll wait
87 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
89 struct lttng_consumer_stream
*null_stream
= NULL
;
93 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
96 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
97 struct lttng_consumer_channel
*chan
,
99 enum consumer_channel_action action
)
101 struct consumer_channel_msg msg
;
104 memset(&msg
, 0, sizeof(msg
));
110 ret
= write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
111 } while (ret
< 0 && errno
== EINTR
);
114 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
117 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
120 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
121 struct lttng_consumer_channel
**chan
,
123 enum consumer_channel_action
*action
)
125 struct consumer_channel_msg msg
;
129 ret
= read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
130 } while (ret
< 0 && errno
== EINTR
);
132 *action
= msg
.action
;
140 * Find a stream. The consumer_data.lock must be locked during this
143 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
146 struct lttng_ht_iter iter
;
147 struct lttng_ht_node_u64
*node
;
148 struct lttng_consumer_stream
*stream
= NULL
;
152 /* -1ULL keys are lookup failures */
153 if (key
== (uint64_t) -1ULL) {
159 lttng_ht_lookup(ht
, &key
, &iter
);
160 node
= lttng_ht_iter_get_node_u64(&iter
);
162 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
170 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
172 struct lttng_consumer_stream
*stream
;
175 stream
= find_stream(key
, ht
);
177 stream
->key
= (uint64_t) -1ULL;
179 * We don't want the lookup to match, but we still need
180 * to iterate on this stream when iterating over the hash table. Just
181 * change the node key.
183 stream
->node
.key
= (uint64_t) -1ULL;
189 * Return a channel object for the given key.
191 * RCU read side lock MUST be acquired before calling this function and
192 * protects the channel ptr.
194 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
196 struct lttng_ht_iter iter
;
197 struct lttng_ht_node_u64
*node
;
198 struct lttng_consumer_channel
*channel
= NULL
;
200 /* -1ULL keys are lookup failures */
201 if (key
== (uint64_t) -1ULL) {
205 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
206 node
= lttng_ht_iter_get_node_u64(&iter
);
208 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
214 static void free_stream_rcu(struct rcu_head
*head
)
216 struct lttng_ht_node_u64
*node
=
217 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
218 struct lttng_consumer_stream
*stream
=
219 caa_container_of(node
, struct lttng_consumer_stream
, node
);
224 static void free_channel_rcu(struct rcu_head
*head
)
226 struct lttng_ht_node_u64
*node
=
227 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
228 struct lttng_consumer_channel
*channel
=
229 caa_container_of(node
, struct lttng_consumer_channel
, node
);
235 * RCU protected relayd socket pair free.
237 static void free_relayd_rcu(struct rcu_head
*head
)
239 struct lttng_ht_node_u64
*node
=
240 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
241 struct consumer_relayd_sock_pair
*relayd
=
242 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
245 * Close all sockets. This is done in the call RCU since we don't want the
246 * socket fds to be reassigned thus potentially creating bad state of the
249 * We do not have to lock the control socket mutex here since at this stage
250 * there is no one referencing to this relayd object.
252 (void) relayd_close(&relayd
->control_sock
);
253 (void) relayd_close(&relayd
->data_sock
);
259 * Destroy and free relayd socket pair object.
261 void consumer_destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
264 struct lttng_ht_iter iter
;
266 if (relayd
== NULL
) {
270 DBG("Consumer destroy and close relayd socket pair");
272 iter
.iter
.node
= &relayd
->node
.node
;
273 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
275 /* We assume the relayd is being or is destroyed */
279 /* RCU free() call */
280 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
284 * Remove a channel from the global list protected by a mutex. This function is
285 * also responsible for freeing its data structures.
287 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
290 struct lttng_ht_iter iter
;
291 struct lttng_consumer_stream
*stream
, *stmp
;
293 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
295 pthread_mutex_lock(&consumer_data
.lock
);
296 pthread_mutex_lock(&channel
->lock
);
298 /* Delete streams that might have been left in the stream list. */
299 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
301 cds_list_del(&stream
->send_node
);
303 * Once a stream is added to this list, the buffers were created so
304 * we have a guarantee that this call will succeed.
306 consumer_stream_destroy(stream
, NULL
);
309 if (channel
->live_timer_enabled
== 1) {
310 consumer_timer_live_stop(channel
);
313 switch (consumer_data
.type
) {
314 case LTTNG_CONSUMER_KERNEL
:
316 case LTTNG_CONSUMER32_UST
:
317 case LTTNG_CONSUMER64_UST
:
318 lttng_ustconsumer_del_channel(channel
);
321 ERR("Unknown consumer_data type");
327 iter
.iter
.node
= &channel
->node
.node
;
328 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
332 call_rcu(&channel
->node
.head
, free_channel_rcu
);
334 pthread_mutex_unlock(&channel
->lock
);
335 pthread_mutex_unlock(&consumer_data
.lock
);
339 * Iterate over the relayd hash table and destroy each element. Finally,
340 * destroy the whole hash table.
342 static void cleanup_relayd_ht(void)
344 struct lttng_ht_iter iter
;
345 struct consumer_relayd_sock_pair
*relayd
;
349 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
351 consumer_destroy_relayd(relayd
);
356 lttng_ht_destroy(consumer_data
.relayd_ht
);
360 * Update the end point status of all streams having the given network sequence
361 * index (relayd index).
363 * It's atomically set without having the stream mutex locked which is fine
364 * because we handle the write/read race with a pipe wakeup for each thread.
366 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
367 enum consumer_endpoint_status status
)
369 struct lttng_ht_iter iter
;
370 struct lttng_consumer_stream
*stream
;
372 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
376 /* Let's begin with metadata */
377 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
378 if (stream
->net_seq_idx
== net_seq_idx
) {
379 uatomic_set(&stream
->endpoint_status
, status
);
380 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
384 /* Follow up by the data streams */
385 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
386 if (stream
->net_seq_idx
== net_seq_idx
) {
387 uatomic_set(&stream
->endpoint_status
, status
);
388 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
395 * Cleanup a relayd object by flagging every associated streams for deletion,
396 * destroying the object meaning removing it from the relayd hash table,
397 * closing the sockets and freeing the memory in a RCU call.
399 * If a local data context is available, notify the threads that the streams'
400 * state have changed.
402 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
403 struct lttng_consumer_local_data
*ctx
)
409 DBG("Cleaning up relayd sockets");
411 /* Save the net sequence index before destroying the object */
412 netidx
= relayd
->net_seq_idx
;
415 * Delete the relayd from the relayd hash table, close the sockets and free
416 * the object in a RCU call.
418 consumer_destroy_relayd(relayd
);
420 /* Set inactive endpoint to all streams */
421 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
424 * With a local data context, notify the threads that the streams' state
425 * have changed. The write() action on the pipe acts as an "implicit"
426 * memory barrier ordering the updates of the end point status from the
427 * read of this status which happens AFTER receiving this notify.
430 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
431 notify_thread_lttng_pipe(ctx
->consumer_metadata_pipe
);
436 * Flag a relayd socket pair for destruction. Destroy it if the refcount
439 * RCU read side lock MUST be aquired before calling this function.
441 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
445 /* Set destroy flag for this object */
446 uatomic_set(&relayd
->destroy_flag
, 1);
448 /* Destroy the relayd if refcount is 0 */
449 if (uatomic_read(&relayd
->refcount
) == 0) {
450 consumer_destroy_relayd(relayd
);
455 * Completly destroy stream from every visiable data structure and the given
458 * One this call returns, the stream object is not longer usable nor visible.
460 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
463 consumer_stream_destroy(stream
, ht
);
467 * XXX naming of del vs destroy is all mixed up.
469 void consumer_del_stream_for_data(struct lttng_consumer_stream
*stream
)
471 consumer_stream_destroy(stream
, data_ht
);
474 void consumer_del_stream_for_metadata(struct lttng_consumer_stream
*stream
)
476 consumer_stream_destroy(stream
, metadata_ht
);
479 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
481 enum lttng_consumer_stream_state state
,
482 const char *channel_name
,
489 enum consumer_channel_type type
,
490 unsigned int monitor
)
493 struct lttng_consumer_stream
*stream
;
495 stream
= zmalloc(sizeof(*stream
));
496 if (stream
== NULL
) {
497 PERROR("malloc struct lttng_consumer_stream");
504 stream
->key
= stream_key
;
506 stream
->out_fd_offset
= 0;
507 stream
->output_written
= 0;
508 stream
->state
= state
;
511 stream
->net_seq_idx
= relayd_id
;
512 stream
->session_id
= session_id
;
513 stream
->monitor
= monitor
;
514 stream
->endpoint_status
= CONSUMER_ENDPOINT_ACTIVE
;
515 stream
->index_fd
= -1;
516 pthread_mutex_init(&stream
->lock
, NULL
);
518 /* If channel is the metadata, flag this stream as metadata. */
519 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
520 stream
->metadata_flag
= 1;
521 /* Metadata is flat out. */
522 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
524 /* Format stream name to <channel_name>_<cpu_number> */
525 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
528 PERROR("snprintf stream name");
533 /* Key is always the wait_fd for streams. */
534 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
536 /* Init node per channel id key */
537 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
539 /* Init session id node with the stream session id */
540 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
542 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
543 " relayd_id %" PRIu64
", session_id %" PRIu64
,
544 stream
->name
, stream
->key
, channel_key
,
545 stream
->net_seq_idx
, stream
->session_id
);
561 * Add a stream to the global list protected by a mutex.
563 int consumer_add_data_stream(struct lttng_consumer_stream
*stream
)
565 struct lttng_ht
*ht
= data_ht
;
571 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
573 pthread_mutex_lock(&consumer_data
.lock
);
574 pthread_mutex_lock(&stream
->chan
->lock
);
575 pthread_mutex_lock(&stream
->chan
->timer_lock
);
576 pthread_mutex_lock(&stream
->lock
);
579 /* Steal stream identifier to avoid having streams with the same key */
580 steal_stream_key(stream
->key
, ht
);
582 lttng_ht_add_unique_u64(ht
, &stream
->node
);
584 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
585 &stream
->node_channel_id
);
588 * Add stream to the stream_list_ht of the consumer data. No need to steal
589 * the key since the HT does not use it and we allow to add redundant keys
592 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
595 * When nb_init_stream_left reaches 0, we don't need to trigger any action
596 * in terms of destroying the associated channel, because the action that
597 * causes the count to become 0 also causes a stream to be added. The
598 * channel deletion will thus be triggered by the following removal of this
601 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
602 /* Increment refcount before decrementing nb_init_stream_left */
604 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
607 /* Update consumer data once the node is inserted. */
608 consumer_data
.stream_count
++;
609 consumer_data
.need_update
= 1;
612 pthread_mutex_unlock(&stream
->lock
);
613 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
614 pthread_mutex_unlock(&stream
->chan
->lock
);
615 pthread_mutex_unlock(&consumer_data
.lock
);
620 void consumer_del_data_stream(struct lttng_consumer_stream
*stream
)
622 consumer_del_stream(stream
, data_ht
);
626 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
627 * be acquired before calling this.
629 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
632 struct lttng_ht_node_u64
*node
;
633 struct lttng_ht_iter iter
;
637 lttng_ht_lookup(consumer_data
.relayd_ht
,
638 &relayd
->net_seq_idx
, &iter
);
639 node
= lttng_ht_iter_get_node_u64(&iter
);
643 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
650 * Allocate and return a consumer relayd socket.
652 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
653 uint64_t net_seq_idx
)
655 struct consumer_relayd_sock_pair
*obj
= NULL
;
657 /* net sequence index of -1 is a failure */
658 if (net_seq_idx
== (uint64_t) -1ULL) {
662 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
664 PERROR("zmalloc relayd sock");
668 obj
->net_seq_idx
= net_seq_idx
;
670 obj
->destroy_flag
= 0;
671 obj
->control_sock
.sock
.fd
= -1;
672 obj
->data_sock
.sock
.fd
= -1;
673 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
674 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
681 * Find a relayd socket pair in the global consumer data.
683 * Return the object if found else NULL.
684 * RCU read-side lock must be held across this call and while using the
687 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
689 struct lttng_ht_iter iter
;
690 struct lttng_ht_node_u64
*node
;
691 struct consumer_relayd_sock_pair
*relayd
= NULL
;
693 /* Negative keys are lookup failures */
694 if (key
== (uint64_t) -1ULL) {
698 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
700 node
= lttng_ht_iter_get_node_u64(&iter
);
702 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
710 * Find a relayd and send the stream
712 * Returns 0 on success, < 0 on error
714 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
718 struct consumer_relayd_sock_pair
*relayd
;
721 assert(stream
->net_seq_idx
!= -1ULL);
724 /* The stream is not metadata. Get relayd reference if exists. */
726 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
727 if (relayd
!= NULL
) {
728 /* Add stream on the relayd */
729 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
730 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
731 path
, &stream
->relayd_stream_id
,
732 stream
->chan
->tracefile_size
, stream
->chan
->tracefile_count
);
733 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
738 uatomic_inc(&relayd
->refcount
);
739 stream
->sent_to_relayd
= 1;
741 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
742 stream
->key
, stream
->net_seq_idx
);
747 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
748 stream
->name
, stream
->key
, stream
->net_seq_idx
);
756 * Find a relayd and close the stream
758 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
760 struct consumer_relayd_sock_pair
*relayd
;
762 /* The stream is not metadata. Get relayd reference if exists. */
764 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
766 consumer_stream_relayd_close(stream
, relayd
);
772 * Handle stream for relayd transmission if the stream applies for network
773 * streaming where the net sequence index is set.
775 * Return destination file descriptor or negative value on error.
777 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
778 size_t data_size
, unsigned long padding
,
779 struct consumer_relayd_sock_pair
*relayd
)
782 struct lttcomm_relayd_data_hdr data_hdr
;
788 /* Reset data header */
789 memset(&data_hdr
, 0, sizeof(data_hdr
));
791 if (stream
->metadata_flag
) {
792 /* Caller MUST acquire the relayd control socket lock */
793 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
798 /* Metadata are always sent on the control socket. */
799 outfd
= relayd
->control_sock
.sock
.fd
;
801 /* Set header with stream information */
802 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
803 data_hdr
.data_size
= htobe32(data_size
);
804 data_hdr
.padding_size
= htobe32(padding
);
806 * Note that net_seq_num below is assigned with the *current* value of
807 * next_net_seq_num and only after that the next_net_seq_num will be
808 * increment. This is why when issuing a command on the relayd using
809 * this next value, 1 should always be substracted in order to compare
810 * the last seen sequence number on the relayd side to the last sent.
812 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
813 /* Other fields are zeroed previously */
815 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
821 ++stream
->next_net_seq_num
;
823 /* Set to go on data socket */
824 outfd
= relayd
->data_sock
.sock
.fd
;
832 * Allocate and return a new lttng_consumer_channel object using the given key
833 * to initialize the hash table node.
835 * On error, return NULL.
837 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
839 const char *pathname
,
844 enum lttng_event_output output
,
845 uint64_t tracefile_size
,
846 uint64_t tracefile_count
,
847 uint64_t session_id_per_pid
,
848 unsigned int monitor
,
849 unsigned int live_timer_interval
)
851 struct lttng_consumer_channel
*channel
;
853 channel
= zmalloc(sizeof(*channel
));
854 if (channel
== NULL
) {
855 PERROR("malloc struct lttng_consumer_channel");
860 channel
->refcount
= 0;
861 channel
->session_id
= session_id
;
862 channel
->session_id_per_pid
= session_id_per_pid
;
865 channel
->relayd_id
= relayd_id
;
866 channel
->output
= output
;
867 channel
->tracefile_size
= tracefile_size
;
868 channel
->tracefile_count
= tracefile_count
;
869 channel
->monitor
= monitor
;
870 channel
->live_timer_interval
= live_timer_interval
;
871 pthread_mutex_init(&channel
->lock
, NULL
);
872 pthread_mutex_init(&channel
->timer_lock
, NULL
);
875 * In monitor mode, the streams associated with the channel will be put in
876 * a special list ONLY owned by this channel. So, the refcount is set to 1
877 * here meaning that the channel itself has streams that are referenced.
879 * On a channel deletion, once the channel is no longer visible, the
880 * refcount is decremented and checked for a zero value to delete it. With
881 * streams in no monitor mode, it will now be safe to destroy the channel.
883 if (!channel
->monitor
) {
884 channel
->refcount
= 1;
887 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
888 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
890 strncpy(channel
->name
, name
, sizeof(channel
->name
));
891 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
893 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
895 channel
->wait_fd
= -1;
897 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
899 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
906 * Add a channel to the global list protected by a mutex.
908 * On success 0 is returned else a negative value.
910 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
911 struct lttng_consumer_local_data
*ctx
)
914 struct lttng_ht_node_u64
*node
;
915 struct lttng_ht_iter iter
;
917 pthread_mutex_lock(&consumer_data
.lock
);
918 pthread_mutex_lock(&channel
->lock
);
919 pthread_mutex_lock(&channel
->timer_lock
);
922 lttng_ht_lookup(consumer_data
.channel_ht
, &channel
->key
, &iter
);
923 node
= lttng_ht_iter_get_node_u64(&iter
);
925 /* Channel already exist. Ignore the insertion */
926 ERR("Consumer add channel key %" PRIu64
" already exists!",
932 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
936 pthread_mutex_unlock(&channel
->timer_lock
);
937 pthread_mutex_unlock(&channel
->lock
);
938 pthread_mutex_unlock(&consumer_data
.lock
);
940 if (!ret
&& channel
->wait_fd
!= -1 &&
941 channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
942 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
948 * Allocate the pollfd structure and the local view of the out fds to avoid
949 * doing a lookup in the linked list and concurrency issues when writing is
950 * needed. Called with consumer_data.lock held.
952 * Returns the number of fds in the structures.
954 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
955 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
959 struct lttng_ht_iter iter
;
960 struct lttng_consumer_stream
*stream
;
965 assert(local_stream
);
967 DBG("Updating poll fd array");
969 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
971 * Only active streams with an active end point can be added to the
972 * poll set and local stream storage of the thread.
974 * There is a potential race here for endpoint_status to be updated
975 * just after the check. However, this is OK since the stream(s) will
976 * be deleted once the thread is notified that the end point state has
977 * changed where this function will be called back again.
979 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
980 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
984 * This clobbers way too much the debug output. Uncomment that if you
985 * need it for debugging purposes.
987 * DBG("Active FD %d", stream->wait_fd);
989 (*pollfd
)[i
].fd
= stream
->wait_fd
;
990 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
991 local_stream
[i
] = stream
;
997 * Insert the consumer_data_pipe at the end of the array and don't
998 * increment i so nb_fd is the number of real FD.
1000 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
1001 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
1006 * Poll on the should_quit pipe and the command socket return -1 on error and
1007 * should exit, 0 if data is available on the command socket
1009 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1014 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1015 if (num_rdy
== -1) {
1017 * Restart interrupted system call.
1019 if (errno
== EINTR
) {
1022 PERROR("Poll error");
1025 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1026 DBG("consumer_should_quit wake up");
1036 * Set the error socket.
1038 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1041 ctx
->consumer_error_socket
= sock
;
1045 * Set the command socket path.
1047 void lttng_consumer_set_command_sock_path(
1048 struct lttng_consumer_local_data
*ctx
, char *sock
)
1050 ctx
->consumer_command_sock_path
= sock
;
1054 * Send return code to the session daemon.
1055 * If the socket is not defined, we return 0, it is not a fatal error
1057 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1059 if (ctx
->consumer_error_socket
> 0) {
1060 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1061 sizeof(enum lttcomm_sessiond_command
));
1068 * Close all the tracefiles and stream fds and MUST be called when all
1069 * instances are destroyed i.e. when all threads were joined and are ended.
1071 void lttng_consumer_cleanup(void)
1073 struct lttng_ht_iter iter
;
1074 struct lttng_consumer_channel
*channel
;
1078 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1080 consumer_del_channel(channel
);
1085 lttng_ht_destroy(consumer_data
.channel_ht
);
1087 cleanup_relayd_ht();
1089 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1092 * This HT contains streams that are freed by either the metadata thread or
1093 * the data thread so we do *nothing* on the hash table and simply destroy
1096 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1100 * Called from signal handler.
1102 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1107 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1108 } while (ret
< 0 && errno
== EINTR
);
1109 if (ret
< 0 || ret
!= 1) {
1110 PERROR("write consumer quit");
1113 DBG("Consumer flag that it should quit");
1116 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1119 int outfd
= stream
->out_fd
;
1122 * This does a blocking write-and-wait on any page that belongs to the
1123 * subbuffer prior to the one we just wrote.
1124 * Don't care about error values, as these are just hints and ways to
1125 * limit the amount of page cache used.
1127 if (orig_offset
< stream
->max_sb_size
) {
1130 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1131 stream
->max_sb_size
,
1132 SYNC_FILE_RANGE_WAIT_BEFORE
1133 | SYNC_FILE_RANGE_WRITE
1134 | SYNC_FILE_RANGE_WAIT_AFTER
);
1136 * Give hints to the kernel about how we access the file:
1137 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1140 * We need to call fadvise again after the file grows because the
1141 * kernel does not seem to apply fadvise to non-existing parts of the
1144 * Call fadvise _after_ having waited for the page writeback to
1145 * complete because the dirty page writeback semantic is not well
1146 * defined. So it can be expected to lead to lower throughput in
1149 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1150 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1154 * Initialise the necessary environnement :
1155 * - create a new context
1156 * - create the poll_pipe
1157 * - create the should_quit pipe (for signal handler)
1158 * - create the thread pipe (for splice)
1160 * Takes a function pointer as argument, this function is called when data is
1161 * available on a buffer. This function is responsible to do the
1162 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1163 * buffer configuration and then kernctl_put_next_subbuf at the end.
1165 * Returns a pointer to the new context or NULL on error.
1167 struct lttng_consumer_local_data
*lttng_consumer_create(
1168 enum lttng_consumer_type type
,
1169 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1170 struct lttng_consumer_local_data
*ctx
),
1171 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1172 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1173 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1176 struct lttng_consumer_local_data
*ctx
;
1178 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1179 consumer_data
.type
== type
);
1180 consumer_data
.type
= type
;
1182 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1184 PERROR("allocating context");
1188 ctx
->consumer_error_socket
= -1;
1189 ctx
->consumer_metadata_socket
= -1;
1190 pthread_mutex_init(&ctx
->metadata_socket_lock
, NULL
);
1191 /* assign the callbacks */
1192 ctx
->on_buffer_ready
= buffer_ready
;
1193 ctx
->on_recv_channel
= recv_channel
;
1194 ctx
->on_recv_stream
= recv_stream
;
1195 ctx
->on_update_stream
= update_stream
;
1197 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1198 if (!ctx
->consumer_data_pipe
) {
1199 goto error_poll_pipe
;
1202 ret
= pipe(ctx
->consumer_should_quit
);
1204 PERROR("Error creating recv pipe");
1205 goto error_quit_pipe
;
1208 ret
= pipe(ctx
->consumer_thread_pipe
);
1210 PERROR("Error creating thread pipe");
1211 goto error_thread_pipe
;
1214 ret
= pipe(ctx
->consumer_channel_pipe
);
1216 PERROR("Error creating channel pipe");
1217 goto error_channel_pipe
;
1220 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1221 if (!ctx
->consumer_metadata_pipe
) {
1222 goto error_metadata_pipe
;
1225 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1227 goto error_splice_pipe
;
1233 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1234 error_metadata_pipe
:
1235 utils_close_pipe(ctx
->consumer_channel_pipe
);
1237 utils_close_pipe(ctx
->consumer_thread_pipe
);
1239 utils_close_pipe(ctx
->consumer_should_quit
);
1241 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1249 * Close all fds associated with the instance and free the context.
1251 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1255 DBG("Consumer destroying it. Closing everything.");
1257 ret
= close(ctx
->consumer_error_socket
);
1261 ret
= close(ctx
->consumer_metadata_socket
);
1265 utils_close_pipe(ctx
->consumer_thread_pipe
);
1266 utils_close_pipe(ctx
->consumer_channel_pipe
);
1267 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1268 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1269 utils_close_pipe(ctx
->consumer_should_quit
);
1270 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1272 unlink(ctx
->consumer_command_sock_path
);
1277 * Write the metadata stream id on the specified file descriptor.
1279 static int write_relayd_metadata_id(int fd
,
1280 struct lttng_consumer_stream
*stream
,
1281 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1284 struct lttcomm_relayd_metadata_payload hdr
;
1286 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1287 hdr
.padding_size
= htobe32(padding
);
1289 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1290 } while (ret
< 0 && errno
== EINTR
);
1291 if (ret
< 0 || ret
!= sizeof(hdr
)) {
1293 * This error means that the fd's end is closed so ignore the perror
1294 * not to clubber the error output since this can happen in a normal
1297 if (errno
!= EPIPE
) {
1298 PERROR("write metadata stream id");
1300 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1302 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1303 * handle writting the missing part so report that as an error and
1304 * don't lie to the caller.
1309 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1310 stream
->relayd_stream_id
, padding
);
1317 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1318 * core function for writing trace buffers to either the local filesystem or
1321 * It must be called with the stream lock held.
1323 * Careful review MUST be put if any changes occur!
1325 * Returns the number of bytes written
1327 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1328 struct lttng_consumer_local_data
*ctx
,
1329 struct lttng_consumer_stream
*stream
, unsigned long len
,
1330 unsigned long padding
,
1331 struct lttng_packet_index
*index
)
1333 unsigned long mmap_offset
;
1335 ssize_t ret
= 0, written
= 0;
1336 off_t orig_offset
= stream
->out_fd_offset
;
1337 /* Default is on the disk */
1338 int outfd
= stream
->out_fd
;
1339 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1340 unsigned int relayd_hang_up
= 0;
1342 /* RCU lock for the relayd pointer */
1345 /* Flag that the current stream if set for network streaming. */
1346 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1347 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1348 if (relayd
== NULL
) {
1354 /* get the offset inside the fd to mmap */
1355 switch (consumer_data
.type
) {
1356 case LTTNG_CONSUMER_KERNEL
:
1357 mmap_base
= stream
->mmap_base
;
1358 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1360 PERROR("tracer ctl get_mmap_read_offset");
1365 case LTTNG_CONSUMER32_UST
:
1366 case LTTNG_CONSUMER64_UST
:
1367 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1369 ERR("read mmap get mmap base for stream %s", stream
->name
);
1373 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1375 PERROR("tracer ctl get_mmap_read_offset");
1381 ERR("Unknown consumer_data type");
1385 /* Handle stream on the relayd if the output is on the network */
1387 unsigned long netlen
= len
;
1390 * Lock the control socket for the complete duration of the function
1391 * since from this point on we will use the socket.
1393 if (stream
->metadata_flag
) {
1394 /* Metadata requires the control socket. */
1395 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1396 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1399 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1401 /* Use the returned socket. */
1404 /* Write metadata stream id before payload */
1405 if (stream
->metadata_flag
) {
1406 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1409 /* Socket operation failed. We consider the relayd dead */
1410 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1418 /* Socket operation failed. We consider the relayd dead */
1419 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1423 /* Else, use the default set before which is the filesystem. */
1426 /* No streaming, we have to set the len with the full padding */
1430 * Check if we need to change the tracefile before writing the packet.
1432 if (stream
->chan
->tracefile_size
> 0 &&
1433 (stream
->tracefile_size_current
+ len
) >
1434 stream
->chan
->tracefile_size
) {
1435 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1436 stream
->name
, stream
->chan
->tracefile_size
,
1437 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1438 stream
->out_fd
, &(stream
->tracefile_count_current
),
1441 ERR("Rotating output file");
1444 outfd
= stream
->out_fd
;
1446 if (stream
->index_fd
>= 0) {
1447 ret
= index_create_file(stream
->chan
->pathname
,
1448 stream
->name
, stream
->uid
, stream
->gid
,
1449 stream
->chan
->tracefile_size
,
1450 stream
->tracefile_count_current
);
1454 stream
->index_fd
= ret
;
1457 /* Reset current size because we just perform a rotation. */
1458 stream
->tracefile_size_current
= 0;
1459 stream
->out_fd_offset
= 0;
1462 stream
->tracefile_size_current
+= len
;
1464 index
->offset
= htobe64(stream
->out_fd_offset
);
1470 ret
= write(outfd
, mmap_base
+ mmap_offset
, len
);
1471 } while (ret
< 0 && errno
== EINTR
);
1472 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1475 * This is possible if the fd is closed on the other side (outfd)
1476 * or any write problem. It can be verbose a bit for a normal
1477 * execution if for instance the relayd is stopped abruptly. This
1478 * can happen so set this to a DBG statement.
1480 DBG("Error in file write mmap");
1484 /* Socket operation failed. We consider the relayd dead */
1485 if (errno
== EPIPE
|| errno
== EINVAL
) {
1490 } else if (ret
> len
) {
1491 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1499 /* This call is useless on a socket so better save a syscall. */
1501 /* This won't block, but will start writeout asynchronously */
1502 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1503 SYNC_FILE_RANGE_WRITE
);
1504 stream
->out_fd_offset
+= ret
;
1506 stream
->output_written
+= ret
;
1509 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1513 * This is a special case that the relayd has closed its socket. Let's
1514 * cleanup the relayd object and all associated streams.
1516 if (relayd
&& relayd_hang_up
) {
1517 cleanup_relayd(relayd
, ctx
);
1521 /* Unlock only if ctrl socket used */
1522 if (relayd
&& stream
->metadata_flag
) {
1523 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1531 * Splice the data from the ring buffer to the tracefile.
1533 * It must be called with the stream lock held.
1535 * Returns the number of bytes spliced.
1537 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1538 struct lttng_consumer_local_data
*ctx
,
1539 struct lttng_consumer_stream
*stream
, unsigned long len
,
1540 unsigned long padding
,
1541 struct lttng_packet_index
*index
)
1543 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1545 off_t orig_offset
= stream
->out_fd_offset
;
1546 int fd
= stream
->wait_fd
;
1547 /* Default is on the disk */
1548 int outfd
= stream
->out_fd
;
1549 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1551 unsigned int relayd_hang_up
= 0;
1553 switch (consumer_data
.type
) {
1554 case LTTNG_CONSUMER_KERNEL
:
1556 case LTTNG_CONSUMER32_UST
:
1557 case LTTNG_CONSUMER64_UST
:
1558 /* Not supported for user space tracing */
1561 ERR("Unknown consumer_data type");
1565 /* RCU lock for the relayd pointer */
1568 /* Flag that the current stream if set for network streaming. */
1569 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1570 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1571 if (relayd
== NULL
) {
1578 * Choose right pipe for splice. Metadata and trace data are handled by
1579 * different threads hence the use of two pipes in order not to race or
1580 * corrupt the written data.
1582 if (stream
->metadata_flag
) {
1583 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1585 splice_pipe
= ctx
->consumer_thread_pipe
;
1588 /* Write metadata stream id before payload */
1590 int total_len
= len
;
1592 if (stream
->metadata_flag
) {
1594 * Lock the control socket for the complete duration of the function
1595 * since from this point on we will use the socket.
1597 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1599 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1603 /* Socket operation failed. We consider the relayd dead */
1604 if (ret
== -EBADF
) {
1605 WARN("Remote relayd disconnected. Stopping");
1612 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1615 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1617 /* Use the returned socket. */
1620 /* Socket operation failed. We consider the relayd dead */
1621 if (ret
== -EBADF
) {
1622 WARN("Remote relayd disconnected. Stopping");
1629 /* No streaming, we have to set the len with the full padding */
1633 * Check if we need to change the tracefile before writing the packet.
1635 if (stream
->chan
->tracefile_size
> 0 &&
1636 (stream
->tracefile_size_current
+ len
) >
1637 stream
->chan
->tracefile_size
) {
1638 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1639 stream
->name
, stream
->chan
->tracefile_size
,
1640 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1641 stream
->out_fd
, &(stream
->tracefile_count_current
),
1644 ERR("Rotating output file");
1647 outfd
= stream
->out_fd
;
1649 if (stream
->index_fd
>= 0) {
1650 ret
= index_create_file(stream
->chan
->pathname
,
1651 stream
->name
, stream
->uid
, stream
->gid
,
1652 stream
->chan
->tracefile_size
,
1653 stream
->tracefile_count_current
);
1657 stream
->index_fd
= ret
;
1660 /* Reset current size because we just perform a rotation. */
1661 stream
->tracefile_size_current
= 0;
1662 stream
->out_fd_offset
= 0;
1665 stream
->tracefile_size_current
+= len
;
1666 index
->offset
= htobe64(stream
->out_fd_offset
);
1670 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1671 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1672 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1673 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1674 DBG("splice chan to pipe, ret %zd", ret_splice
);
1675 if (ret_splice
< 0) {
1676 PERROR("Error in relay splice");
1678 written
= ret_splice
;
1684 /* Handle stream on the relayd if the output is on the network */
1686 if (stream
->metadata_flag
) {
1687 size_t metadata_payload_size
=
1688 sizeof(struct lttcomm_relayd_metadata_payload
);
1690 /* Update counter to fit the spliced data */
1691 ret_splice
+= metadata_payload_size
;
1692 len
+= metadata_payload_size
;
1694 * We do this so the return value can match the len passed as
1695 * argument to this function.
1697 written
-= metadata_payload_size
;
1701 /* Splice data out */
1702 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1703 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1704 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1705 if (ret_splice
< 0) {
1706 PERROR("Error in file splice");
1708 written
= ret_splice
;
1710 /* Socket operation failed. We consider the relayd dead */
1711 if (errno
== EBADF
|| errno
== EPIPE
) {
1712 WARN("Remote relayd disconnected. Stopping");
1718 } else if (ret_splice
> len
) {
1720 PERROR("Wrote more data than requested %zd (len: %lu)",
1722 written
+= ret_splice
;
1728 /* This call is useless on a socket so better save a syscall. */
1730 /* This won't block, but will start writeout asynchronously */
1731 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1732 SYNC_FILE_RANGE_WRITE
);
1733 stream
->out_fd_offset
+= ret_splice
;
1735 stream
->output_written
+= ret_splice
;
1736 written
+= ret_splice
;
1738 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1746 * This is a special case that the relayd has closed its socket. Let's
1747 * cleanup the relayd object and all associated streams.
1749 if (relayd
&& relayd_hang_up
) {
1750 cleanup_relayd(relayd
, ctx
);
1751 /* Skip splice error so the consumer does not fail */
1756 /* send the appropriate error description to sessiond */
1759 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1762 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1765 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1770 if (relayd
&& stream
->metadata_flag
) {
1771 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1779 * Take a snapshot for a specific fd
1781 * Returns 0 on success, < 0 on error
1783 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1785 switch (consumer_data
.type
) {
1786 case LTTNG_CONSUMER_KERNEL
:
1787 return lttng_kconsumer_take_snapshot(stream
);
1788 case LTTNG_CONSUMER32_UST
:
1789 case LTTNG_CONSUMER64_UST
:
1790 return lttng_ustconsumer_take_snapshot(stream
);
1792 ERR("Unknown consumer_data type");
1799 * Get the produced position
1801 * Returns 0 on success, < 0 on error
1803 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1806 switch (consumer_data
.type
) {
1807 case LTTNG_CONSUMER_KERNEL
:
1808 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1809 case LTTNG_CONSUMER32_UST
:
1810 case LTTNG_CONSUMER64_UST
:
1811 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1813 ERR("Unknown consumer_data type");
1819 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1820 int sock
, struct pollfd
*consumer_sockpoll
)
1822 switch (consumer_data
.type
) {
1823 case LTTNG_CONSUMER_KERNEL
:
1824 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1825 case LTTNG_CONSUMER32_UST
:
1826 case LTTNG_CONSUMER64_UST
:
1827 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1829 ERR("Unknown consumer_data type");
1836 * Iterate over all streams of the hashtable and free them properly.
1838 * WARNING: *MUST* be used with data stream only.
1840 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1842 struct lttng_ht_iter iter
;
1843 struct lttng_consumer_stream
*stream
;
1850 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1852 * Ignore return value since we are currently cleaning up so any error
1855 (void) consumer_del_stream(stream
, ht
);
1859 lttng_ht_destroy(ht
);
1863 * Iterate over all streams of the hashtable and free them properly.
1865 * XXX: Should not be only for metadata stream or else use an other name.
1867 static void destroy_stream_ht(struct lttng_ht
*ht
)
1869 struct lttng_ht_iter iter
;
1870 struct lttng_consumer_stream
*stream
;
1877 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1879 * Ignore return value since we are currently cleaning up so any error
1882 (void) consumer_del_metadata_stream(stream
, ht
);
1886 lttng_ht_destroy(ht
);
1889 void lttng_consumer_close_metadata(void)
1891 switch (consumer_data
.type
) {
1892 case LTTNG_CONSUMER_KERNEL
:
1894 * The Kernel consumer has a different metadata scheme so we don't
1895 * close anything because the stream will be closed by the session
1899 case LTTNG_CONSUMER32_UST
:
1900 case LTTNG_CONSUMER64_UST
:
1902 * Close all metadata streams. The metadata hash table is passed and
1903 * this call iterates over it by closing all wakeup fd. This is safe
1904 * because at this point we are sure that the metadata producer is
1905 * either dead or blocked.
1907 lttng_ustconsumer_close_metadata(metadata_ht
);
1910 ERR("Unknown consumer_data type");
1916 * Clean up a metadata stream and free its memory.
1918 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1919 struct lttng_ht
*ht
)
1922 struct lttng_ht_iter iter
;
1923 struct lttng_consumer_channel
*free_chan
= NULL
;
1924 struct consumer_relayd_sock_pair
*relayd
;
1928 * This call should NEVER receive regular stream. It must always be
1929 * metadata stream and this is crucial for data structure synchronization.
1931 assert(stream
->metadata_flag
);
1933 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1936 /* Means the stream was allocated but not successfully added */
1937 goto free_stream_rcu
;
1940 pthread_mutex_lock(&consumer_data
.lock
);
1941 pthread_mutex_lock(&stream
->chan
->lock
);
1942 pthread_mutex_lock(&stream
->lock
);
1944 switch (consumer_data
.type
) {
1945 case LTTNG_CONSUMER_KERNEL
:
1946 if (stream
->mmap_base
!= NULL
) {
1947 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1949 PERROR("munmap metadata stream");
1952 if (stream
->wait_fd
>= 0) {
1953 ret
= close(stream
->wait_fd
);
1955 PERROR("close kernel metadata wait_fd");
1959 case LTTNG_CONSUMER32_UST
:
1960 case LTTNG_CONSUMER64_UST
:
1961 if (stream
->monitor
) {
1962 /* close the write-side in close_metadata */
1963 ret
= close(stream
->ust_metadata_poll_pipe
[0]);
1965 PERROR("Close UST metadata read-side poll pipe");
1968 lttng_ustconsumer_del_stream(stream
);
1971 ERR("Unknown consumer_data type");
1977 iter
.iter
.node
= &stream
->node
.node
;
1978 ret
= lttng_ht_del(ht
, &iter
);
1981 iter
.iter
.node
= &stream
->node_channel_id
.node
;
1982 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
1985 iter
.iter
.node
= &stream
->node_session_id
.node
;
1986 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1990 if (stream
->out_fd
>= 0) {
1991 ret
= close(stream
->out_fd
);
1997 /* Check and cleanup relayd */
1999 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
2000 if (relayd
!= NULL
) {
2001 uatomic_dec(&relayd
->refcount
);
2002 assert(uatomic_read(&relayd
->refcount
) >= 0);
2004 /* Closing streams requires to lock the control socket. */
2005 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
2006 ret
= relayd_send_close_stream(&relayd
->control_sock
,
2007 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
2008 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2010 DBG("Unable to close stream on the relayd. Continuing");
2012 * Continue here. There is nothing we can do for the relayd.
2013 * Chances are that the relayd has closed the socket so we just
2014 * continue cleaning up.
2018 /* Both conditions are met, we destroy the relayd. */
2019 if (uatomic_read(&relayd
->refcount
) == 0 &&
2020 uatomic_read(&relayd
->destroy_flag
)) {
2021 consumer_destroy_relayd(relayd
);
2026 /* Atomically decrement channel refcount since other threads can use it. */
2027 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
2028 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
2029 /* Go for channel deletion! */
2030 free_chan
= stream
->chan
;
2035 * Nullify the stream reference so it is not used after deletion. The
2036 * channel lock MUST be acquired before being able to check for
2037 * a NULL pointer value.
2039 stream
->chan
->metadata_stream
= NULL
;
2041 pthread_mutex_unlock(&stream
->lock
);
2042 pthread_mutex_unlock(&stream
->chan
->lock
);
2043 pthread_mutex_unlock(&consumer_data
.lock
);
2046 consumer_del_channel(free_chan
);
2050 call_rcu(&stream
->node
.head
, free_stream_rcu
);
2054 * Action done with the metadata stream when adding it to the consumer internal
2055 * data structures to handle it.
2057 int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
)
2059 struct lttng_ht
*ht
= metadata_ht
;
2061 struct lttng_ht_iter iter
;
2062 struct lttng_ht_node_u64
*node
;
2067 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2069 pthread_mutex_lock(&consumer_data
.lock
);
2070 pthread_mutex_lock(&stream
->chan
->lock
);
2071 pthread_mutex_lock(&stream
->chan
->timer_lock
);
2072 pthread_mutex_lock(&stream
->lock
);
2075 * From here, refcounts are updated so be _careful_ when returning an error
2082 * Lookup the stream just to make sure it does not exist in our internal
2083 * state. This should NEVER happen.
2085 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2086 node
= lttng_ht_iter_get_node_u64(&iter
);
2090 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2091 * in terms of destroying the associated channel, because the action that
2092 * causes the count to become 0 also causes a stream to be added. The
2093 * channel deletion will thus be triggered by the following removal of this
2096 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2097 /* Increment refcount before decrementing nb_init_stream_left */
2099 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2102 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2104 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2105 &stream
->node_channel_id
);
2108 * Add stream to the stream_list_ht of the consumer data. No need to steal
2109 * the key since the HT does not use it and we allow to add redundant keys
2112 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2116 pthread_mutex_unlock(&stream
->lock
);
2117 pthread_mutex_unlock(&stream
->chan
->lock
);
2118 pthread_mutex_unlock(&stream
->chan
->timer_lock
);
2119 pthread_mutex_unlock(&consumer_data
.lock
);
2124 * Delete data stream that are flagged for deletion (endpoint_status).
2126 static void validate_endpoint_status_data_stream(void)
2128 struct lttng_ht_iter iter
;
2129 struct lttng_consumer_stream
*stream
;
2131 DBG("Consumer delete flagged data stream");
2134 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2135 /* Validate delete flag of the stream */
2136 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2139 /* Delete it right now */
2140 consumer_del_stream(stream
, data_ht
);
2146 * Delete metadata stream that are flagged for deletion (endpoint_status).
2148 static void validate_endpoint_status_metadata_stream(
2149 struct lttng_poll_event
*pollset
)
2151 struct lttng_ht_iter iter
;
2152 struct lttng_consumer_stream
*stream
;
2154 DBG("Consumer delete flagged metadata stream");
2159 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2160 /* Validate delete flag of the stream */
2161 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2165 * Remove from pollset so the metadata thread can continue without
2166 * blocking on a deleted stream.
2168 lttng_poll_del(pollset
, stream
->wait_fd
);
2170 /* Delete it right now */
2171 consumer_del_metadata_stream(stream
, metadata_ht
);
2177 * Thread polls on metadata file descriptor and write them on disk or on the
2180 void *consumer_thread_metadata_poll(void *data
)
2183 uint32_t revents
, nb_fd
;
2184 struct lttng_consumer_stream
*stream
= NULL
;
2185 struct lttng_ht_iter iter
;
2186 struct lttng_ht_node_u64
*node
;
2187 struct lttng_poll_event events
;
2188 struct lttng_consumer_local_data
*ctx
= data
;
2191 rcu_register_thread();
2193 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2195 /* ENOMEM at this point. Better to bail out. */
2199 DBG("Thread metadata poll started");
2201 /* Size is set to 1 for the consumer_metadata pipe */
2202 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2204 ERR("Poll set creation failed");
2208 ret
= lttng_poll_add(&events
,
2209 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2215 DBG("Metadata main loop started");
2218 /* Only the metadata pipe is set */
2219 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2224 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2225 ret
= lttng_poll_wait(&events
, -1);
2226 DBG("Metadata event catched in thread");
2228 if (errno
== EINTR
) {
2229 ERR("Poll EINTR catched");
2237 /* From here, the event is a metadata wait fd */
2238 for (i
= 0; i
< nb_fd
; i
++) {
2239 revents
= LTTNG_POLL_GETEV(&events
, i
);
2240 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2242 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2243 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2244 DBG("Metadata thread pipe hung up");
2246 * Remove the pipe from the poll set and continue the loop
2247 * since their might be data to consume.
2249 lttng_poll_del(&events
,
2250 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2251 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2253 } else if (revents
& LPOLLIN
) {
2256 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2257 &stream
, sizeof(stream
));
2259 ERR("read metadata stream, ret: %zd", pipe_len
);
2261 * Continue here to handle the rest of the streams.
2266 /* A NULL stream means that the state has changed. */
2267 if (stream
== NULL
) {
2268 /* Check for deleted streams. */
2269 validate_endpoint_status_metadata_stream(&events
);
2273 DBG("Adding metadata stream %d to poll set",
2276 /* Add metadata stream to the global poll events list */
2277 lttng_poll_add(&events
, stream
->wait_fd
,
2278 LPOLLIN
| LPOLLPRI
);
2281 /* Handle other stream */
2287 uint64_t tmp_id
= (uint64_t) pollfd
;
2289 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2291 node
= lttng_ht_iter_get_node_u64(&iter
);
2294 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2297 /* Check for error event */
2298 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2299 DBG("Metadata fd %d is hup|err.", pollfd
);
2300 if (!stream
->hangup_flush_done
2301 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2302 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2303 DBG("Attempting to flush and consume the UST buffers");
2304 lttng_ustconsumer_on_stream_hangup(stream
);
2306 /* We just flushed the stream now read it. */
2308 len
= ctx
->on_buffer_ready(stream
, ctx
);
2310 * We don't check the return value here since if we get
2311 * a negative len, it means an error occured thus we
2312 * simply remove it from the poll set and free the
2318 lttng_poll_del(&events
, stream
->wait_fd
);
2320 * This call update the channel states, closes file descriptors
2321 * and securely free the stream.
2323 consumer_del_metadata_stream(stream
, metadata_ht
);
2324 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2325 /* Get the data out of the metadata file descriptor */
2326 DBG("Metadata available on fd %d", pollfd
);
2327 assert(stream
->wait_fd
== pollfd
);
2330 len
= ctx
->on_buffer_ready(stream
, ctx
);
2332 * We don't check the return value here since if we get
2333 * a negative len, it means an error occured thus we
2334 * simply remove it from the poll set and free the
2339 /* It's ok to have an unavailable sub-buffer */
2340 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2341 /* Clean up stream from consumer and free it. */
2342 lttng_poll_del(&events
, stream
->wait_fd
);
2343 consumer_del_metadata_stream(stream
, metadata_ht
);
2347 /* Release RCU lock for the stream looked up */
2354 DBG("Metadata poll thread exiting");
2356 lttng_poll_clean(&events
);
2358 destroy_stream_ht(metadata_ht
);
2360 rcu_unregister_thread();
2365 * This thread polls the fds in the set to consume the data and write
2366 * it to tracefile if necessary.
2368 void *consumer_thread_data_poll(void *data
)
2370 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2371 struct pollfd
*pollfd
= NULL
;
2372 /* local view of the streams */
2373 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2374 /* local view of consumer_data.fds_count */
2376 struct lttng_consumer_local_data
*ctx
= data
;
2379 rcu_register_thread();
2381 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2382 if (data_ht
== NULL
) {
2383 /* ENOMEM at this point. Better to bail out. */
2387 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2388 if (local_stream
== NULL
) {
2389 PERROR("local_stream malloc");
2398 * the fds set has been updated, we need to update our
2399 * local array as well
2401 pthread_mutex_lock(&consumer_data
.lock
);
2402 if (consumer_data
.need_update
) {
2407 local_stream
= NULL
;
2409 /* allocate for all fds + 1 for the consumer_data_pipe */
2410 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2411 if (pollfd
== NULL
) {
2412 PERROR("pollfd malloc");
2413 pthread_mutex_unlock(&consumer_data
.lock
);
2417 /* allocate for all fds + 1 for the consumer_data_pipe */
2418 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2419 sizeof(struct lttng_consumer_stream
*));
2420 if (local_stream
== NULL
) {
2421 PERROR("local_stream malloc");
2422 pthread_mutex_unlock(&consumer_data
.lock
);
2425 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2428 ERR("Error in allocating pollfd or local_outfds");
2429 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2430 pthread_mutex_unlock(&consumer_data
.lock
);
2434 consumer_data
.need_update
= 0;
2436 pthread_mutex_unlock(&consumer_data
.lock
);
2438 /* No FDs and consumer_quit, consumer_cleanup the thread */
2439 if (nb_fd
== 0 && consumer_quit
== 1) {
2442 /* poll on the array of fds */
2444 DBG("polling on %d fd", nb_fd
+ 1);
2445 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2446 DBG("poll num_rdy : %d", num_rdy
);
2447 if (num_rdy
== -1) {
2449 * Restart interrupted system call.
2451 if (errno
== EINTR
) {
2454 PERROR("Poll error");
2455 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2457 } else if (num_rdy
== 0) {
2458 DBG("Polling thread timed out");
2463 * If the consumer_data_pipe triggered poll go directly to the
2464 * beginning of the loop to update the array. We want to prioritize
2465 * array update over low-priority reads.
2467 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2468 ssize_t pipe_readlen
;
2470 DBG("consumer_data_pipe wake up");
2471 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2472 &new_stream
, sizeof(new_stream
));
2473 if (pipe_readlen
< 0) {
2474 ERR("Consumer data pipe ret %zd", pipe_readlen
);
2475 /* Continue so we can at least handle the current stream(s). */
2480 * If the stream is NULL, just ignore it. It's also possible that
2481 * the sessiond poll thread changed the consumer_quit state and is
2482 * waking us up to test it.
2484 if (new_stream
== NULL
) {
2485 validate_endpoint_status_data_stream();
2489 /* Continue to update the local streams and handle prio ones */
2493 /* Take care of high priority channels first. */
2494 for (i
= 0; i
< nb_fd
; i
++) {
2495 if (local_stream
[i
] == NULL
) {
2498 if (pollfd
[i
].revents
& POLLPRI
) {
2499 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2501 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2502 /* it's ok to have an unavailable sub-buffer */
2503 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2504 /* Clean the stream and free it. */
2505 consumer_del_stream(local_stream
[i
], data_ht
);
2506 local_stream
[i
] = NULL
;
2507 } else if (len
> 0) {
2508 local_stream
[i
]->data_read
= 1;
2514 * If we read high prio channel in this loop, try again
2515 * for more high prio data.
2521 /* Take care of low priority channels. */
2522 for (i
= 0; i
< nb_fd
; i
++) {
2523 if (local_stream
[i
] == NULL
) {
2526 if ((pollfd
[i
].revents
& POLLIN
) ||
2527 local_stream
[i
]->hangup_flush_done
) {
2528 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2529 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2530 /* it's ok to have an unavailable sub-buffer */
2531 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2532 /* Clean the stream and free it. */
2533 consumer_del_stream(local_stream
[i
], data_ht
);
2534 local_stream
[i
] = NULL
;
2535 } else if (len
> 0) {
2536 local_stream
[i
]->data_read
= 1;
2541 /* Handle hangup and errors */
2542 for (i
= 0; i
< nb_fd
; i
++) {
2543 if (local_stream
[i
] == NULL
) {
2546 if (!local_stream
[i
]->hangup_flush_done
2547 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2548 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2549 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2550 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2552 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2553 /* Attempt read again, for the data we just flushed. */
2554 local_stream
[i
]->data_read
= 1;
2557 * If the poll flag is HUP/ERR/NVAL and we have
2558 * read no data in this pass, we can remove the
2559 * stream from its hash table.
2561 if ((pollfd
[i
].revents
& POLLHUP
)) {
2562 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2563 if (!local_stream
[i
]->data_read
) {
2564 consumer_del_stream(local_stream
[i
], data_ht
);
2565 local_stream
[i
] = NULL
;
2568 } else if (pollfd
[i
].revents
& POLLERR
) {
2569 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2570 if (!local_stream
[i
]->data_read
) {
2571 consumer_del_stream(local_stream
[i
], data_ht
);
2572 local_stream
[i
] = NULL
;
2575 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2576 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2577 if (!local_stream
[i
]->data_read
) {
2578 consumer_del_stream(local_stream
[i
], data_ht
);
2579 local_stream
[i
] = NULL
;
2583 if (local_stream
[i
] != NULL
) {
2584 local_stream
[i
]->data_read
= 0;
2589 DBG("polling thread exiting");
2594 * Close the write side of the pipe so epoll_wait() in
2595 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2596 * read side of the pipe. If we close them both, epoll_wait strangely does
2597 * not return and could create a endless wait period if the pipe is the
2598 * only tracked fd in the poll set. The thread will take care of closing
2601 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2603 destroy_data_stream_ht(data_ht
);
2605 rcu_unregister_thread();
2610 * Close wake-up end of each stream belonging to the channel. This will
2611 * allow the poll() on the stream read-side to detect when the
2612 * write-side (application) finally closes them.
2615 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2617 struct lttng_ht
*ht
;
2618 struct lttng_consumer_stream
*stream
;
2619 struct lttng_ht_iter iter
;
2621 ht
= consumer_data
.stream_per_chan_id_ht
;
2624 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2625 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2626 ht
->match_fct
, &channel
->key
,
2627 &iter
.iter
, stream
, node_channel_id
.node
) {
2629 * Protect against teardown with mutex.
2631 pthread_mutex_lock(&stream
->lock
);
2632 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2635 switch (consumer_data
.type
) {
2636 case LTTNG_CONSUMER_KERNEL
:
2638 case LTTNG_CONSUMER32_UST
:
2639 case LTTNG_CONSUMER64_UST
:
2641 * Note: a mutex is taken internally within
2642 * liblttng-ust-ctl to protect timer wakeup_fd
2643 * use from concurrent close.
2645 lttng_ustconsumer_close_stream_wakeup(stream
);
2648 ERR("Unknown consumer_data type");
2652 pthread_mutex_unlock(&stream
->lock
);
2657 static void destroy_channel_ht(struct lttng_ht
*ht
)
2659 struct lttng_ht_iter iter
;
2660 struct lttng_consumer_channel
*channel
;
2668 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2669 ret
= lttng_ht_del(ht
, &iter
);
2674 lttng_ht_destroy(ht
);
2678 * This thread polls the channel fds to detect when they are being
2679 * closed. It closes all related streams if the channel is detected as
2680 * closed. It is currently only used as a shim layer for UST because the
2681 * consumerd needs to keep the per-stream wakeup end of pipes open for
2684 void *consumer_thread_channel_poll(void *data
)
2687 uint32_t revents
, nb_fd
;
2688 struct lttng_consumer_channel
*chan
= NULL
;
2689 struct lttng_ht_iter iter
;
2690 struct lttng_ht_node_u64
*node
;
2691 struct lttng_poll_event events
;
2692 struct lttng_consumer_local_data
*ctx
= data
;
2693 struct lttng_ht
*channel_ht
;
2695 rcu_register_thread();
2697 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2699 /* ENOMEM at this point. Better to bail out. */
2703 DBG("Thread channel poll started");
2705 /* Size is set to 1 for the consumer_channel pipe */
2706 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2708 ERR("Poll set creation failed");
2712 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2718 DBG("Channel main loop started");
2721 /* Only the channel pipe is set */
2722 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2727 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2728 ret
= lttng_poll_wait(&events
, -1);
2729 DBG("Channel event catched in thread");
2731 if (errno
== EINTR
) {
2732 ERR("Poll EINTR catched");
2740 /* From here, the event is a channel wait fd */
2741 for (i
= 0; i
< nb_fd
; i
++) {
2742 revents
= LTTNG_POLL_GETEV(&events
, i
);
2743 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2745 /* Just don't waste time if no returned events for the fd */
2749 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2750 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2751 DBG("Channel thread pipe hung up");
2753 * Remove the pipe from the poll set and continue the loop
2754 * since their might be data to consume.
2756 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2758 } else if (revents
& LPOLLIN
) {
2759 enum consumer_channel_action action
;
2762 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2764 ERR("Error reading channel pipe");
2769 case CONSUMER_CHANNEL_ADD
:
2770 DBG("Adding channel %d to poll set",
2773 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2776 lttng_ht_add_unique_u64(channel_ht
,
2777 &chan
->wait_fd_node
);
2779 /* Add channel to the global poll events list */
2780 lttng_poll_add(&events
, chan
->wait_fd
,
2781 LPOLLIN
| LPOLLPRI
);
2783 case CONSUMER_CHANNEL_DEL
:
2785 struct lttng_consumer_stream
*stream
, *stmp
;
2788 chan
= consumer_find_channel(key
);
2791 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2794 lttng_poll_del(&events
, chan
->wait_fd
);
2795 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2796 ret
= lttng_ht_del(channel_ht
, &iter
);
2798 consumer_close_channel_streams(chan
);
2800 switch (consumer_data
.type
) {
2801 case LTTNG_CONSUMER_KERNEL
:
2803 case LTTNG_CONSUMER32_UST
:
2804 case LTTNG_CONSUMER64_UST
:
2805 /* Delete streams that might have been left in the stream list. */
2806 cds_list_for_each_entry_safe(stream
, stmp
, &chan
->streams
.head
,
2808 cds_list_del(&stream
->send_node
);
2809 lttng_ustconsumer_del_stream(stream
);
2810 uatomic_sub(&stream
->chan
->refcount
, 1);
2811 assert(&chan
->refcount
);
2816 ERR("Unknown consumer_data type");
2821 * Release our own refcount. Force channel deletion even if
2822 * streams were not initialized.
2824 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2825 consumer_del_channel(chan
);
2830 case CONSUMER_CHANNEL_QUIT
:
2832 * Remove the pipe from the poll set and continue the loop
2833 * since their might be data to consume.
2835 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2838 ERR("Unknown action");
2843 /* Handle other stream */
2849 uint64_t tmp_id
= (uint64_t) pollfd
;
2851 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2853 node
= lttng_ht_iter_get_node_u64(&iter
);
2856 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2859 /* Check for error event */
2860 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2861 DBG("Channel fd %d is hup|err.", pollfd
);
2863 lttng_poll_del(&events
, chan
->wait_fd
);
2864 ret
= lttng_ht_del(channel_ht
, &iter
);
2866 consumer_close_channel_streams(chan
);
2868 /* Release our own refcount */
2869 if (!uatomic_sub_return(&chan
->refcount
, 1)
2870 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2871 consumer_del_channel(chan
);
2875 /* Release RCU lock for the channel looked up */
2881 lttng_poll_clean(&events
);
2883 destroy_channel_ht(channel_ht
);
2885 DBG("Channel poll thread exiting");
2886 rcu_unregister_thread();
2890 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2891 struct pollfd
*sockpoll
, int client_socket
)
2898 if (lttng_consumer_poll_socket(sockpoll
) < 0) {
2902 DBG("Metadata connection on client_socket");
2904 /* Blocking call, waiting for transmission */
2905 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2906 if (ctx
->consumer_metadata_socket
< 0) {
2907 WARN("On accept metadata");
2918 * This thread listens on the consumerd socket and receives the file
2919 * descriptors from the session daemon.
2921 void *consumer_thread_sessiond_poll(void *data
)
2923 int sock
= -1, client_socket
, ret
;
2925 * structure to poll for incoming data on communication socket avoids
2926 * making blocking sockets.
2928 struct pollfd consumer_sockpoll
[2];
2929 struct lttng_consumer_local_data
*ctx
= data
;
2931 rcu_register_thread();
2933 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2934 unlink(ctx
->consumer_command_sock_path
);
2935 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2936 if (client_socket
< 0) {
2937 ERR("Cannot create command socket");
2941 ret
= lttcomm_listen_unix_sock(client_socket
);
2946 DBG("Sending ready command to lttng-sessiond");
2947 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2948 /* return < 0 on error, but == 0 is not fatal */
2950 ERR("Error sending ready command to lttng-sessiond");
2954 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2955 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2956 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2957 consumer_sockpoll
[1].fd
= client_socket
;
2958 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2960 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2963 DBG("Connection on client_socket");
2965 /* Blocking call, waiting for transmission */
2966 sock
= lttcomm_accept_unix_sock(client_socket
);
2973 * Setup metadata socket which is the second socket connection on the
2974 * command unix socket.
2976 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
2981 /* This socket is not useful anymore. */
2982 ret
= close(client_socket
);
2984 PERROR("close client_socket");
2988 /* update the polling structure to poll on the established socket */
2989 consumer_sockpoll
[1].fd
= sock
;
2990 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2993 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2996 DBG("Incoming command on sock");
2997 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2998 if (ret
== -ENOENT
) {
2999 DBG("Received STOP command");
3004 * This could simply be a session daemon quitting. Don't output
3007 DBG("Communication interrupted on command socket");
3010 if (consumer_quit
) {
3011 DBG("consumer_thread_receive_fds received quit from signal");
3014 DBG("received command on sock");
3017 DBG("Consumer thread sessiond poll exiting");
3020 * Close metadata streams since the producer is the session daemon which
3023 * NOTE: for now, this only applies to the UST tracer.
3025 lttng_consumer_close_metadata();
3028 * when all fds have hung up, the polling thread
3034 * Notify the data poll thread to poll back again and test the
3035 * consumer_quit state that we just set so to quit gracefully.
3037 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
3039 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
3041 /* Cleaning up possibly open sockets. */
3045 PERROR("close sock sessiond poll");
3048 if (client_socket
>= 0) {
3049 ret
= close(client_socket
);
3051 PERROR("close client_socket sessiond poll");
3055 rcu_unregister_thread();
3059 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3060 struct lttng_consumer_local_data
*ctx
)
3064 pthread_mutex_lock(&stream
->lock
);
3066 switch (consumer_data
.type
) {
3067 case LTTNG_CONSUMER_KERNEL
:
3068 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3070 case LTTNG_CONSUMER32_UST
:
3071 case LTTNG_CONSUMER64_UST
:
3072 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3075 ERR("Unknown consumer_data type");
3081 pthread_mutex_unlock(&stream
->lock
);
3085 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3087 switch (consumer_data
.type
) {
3088 case LTTNG_CONSUMER_KERNEL
:
3089 return lttng_kconsumer_on_recv_stream(stream
);
3090 case LTTNG_CONSUMER32_UST
:
3091 case LTTNG_CONSUMER64_UST
:
3092 return lttng_ustconsumer_on_recv_stream(stream
);
3094 ERR("Unknown consumer_data type");
3101 * Allocate and set consumer data hash tables.
3103 void lttng_consumer_init(void)
3105 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3106 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3107 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3108 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3112 * Process the ADD_RELAYD command receive by a consumer.
3114 * This will create a relayd socket pair and add it to the relayd hash table.
3115 * The caller MUST acquire a RCU read side lock before calling it.
3117 int consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3118 struct lttng_consumer_local_data
*ctx
, int sock
,
3119 struct pollfd
*consumer_sockpoll
,
3120 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
,
3121 uint64_t relayd_session_id
)
3123 int fd
= -1, ret
= -1, relayd_created
= 0;
3124 enum lttng_error_code ret_code
= LTTNG_OK
;
3125 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3128 assert(relayd_sock
);
3130 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3132 /* Get relayd reference if exists. */
3133 relayd
= consumer_find_relayd(net_seq_idx
);
3134 if (relayd
== NULL
) {
3135 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3136 /* Not found. Allocate one. */
3137 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3138 if (relayd
== NULL
) {
3140 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3143 relayd
->sessiond_session_id
= sessiond_id
;
3148 * This code path MUST continue to the consumer send status message to
3149 * we can notify the session daemon and continue our work without
3150 * killing everything.
3154 * relayd key should never be found for control socket.
3156 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3159 /* First send a status message before receiving the fds. */
3160 ret
= consumer_send_status_msg(sock
, LTTNG_OK
);
3162 /* Somehow, the session daemon is not responding anymore. */
3163 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3164 goto error_nosignal
;
3167 /* Poll on consumer socket. */
3168 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
3169 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3171 goto error_nosignal
;
3174 /* Get relayd socket from session daemon */
3175 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3176 if (ret
!= sizeof(fd
)) {
3178 fd
= -1; /* Just in case it gets set with an invalid value. */
3181 * Failing to receive FDs might indicate a major problem such as
3182 * reaching a fd limit during the receive where the kernel returns a
3183 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3184 * don't take any chances and stop everything.
3186 * XXX: Feature request #558 will fix that and avoid this possible
3187 * issue when reaching the fd limit.
3189 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3190 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3194 /* Copy socket information and received FD */
3195 switch (sock_type
) {
3196 case LTTNG_STREAM_CONTROL
:
3197 /* Copy received lttcomm socket */
3198 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3199 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3200 /* Handle create_sock error. */
3202 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3206 * Close the socket created internally by
3207 * lttcomm_create_sock, so we can replace it by the one
3208 * received from sessiond.
3210 if (close(relayd
->control_sock
.sock
.fd
)) {
3214 /* Assign new file descriptor */
3215 relayd
->control_sock
.sock
.fd
= fd
;
3216 fd
= -1; /* For error path */
3217 /* Assign version values. */
3218 relayd
->control_sock
.major
= relayd_sock
->major
;
3219 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3221 relayd
->relayd_session_id
= relayd_session_id
;
3224 case LTTNG_STREAM_DATA
:
3225 /* Copy received lttcomm socket */
3226 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3227 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3228 /* Handle create_sock error. */
3230 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3234 * Close the socket created internally by
3235 * lttcomm_create_sock, so we can replace it by the one
3236 * received from sessiond.
3238 if (close(relayd
->data_sock
.sock
.fd
)) {
3242 /* Assign new file descriptor */
3243 relayd
->data_sock
.sock
.fd
= fd
;
3244 fd
= -1; /* for eventual error paths */
3245 /* Assign version values. */
3246 relayd
->data_sock
.major
= relayd_sock
->major
;
3247 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3250 ERR("Unknown relayd socket type (%d)", sock_type
);
3252 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3256 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3257 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3258 relayd
->net_seq_idx
, fd
);
3260 /* We successfully added the socket. Send status back. */
3261 ret
= consumer_send_status_msg(sock
, ret_code
);
3263 /* Somehow, the session daemon is not responding anymore. */
3264 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3265 goto error_nosignal
;
3269 * Add relayd socket pair to consumer data hashtable. If object already
3270 * exists or on error, the function gracefully returns.
3278 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3279 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3283 /* Close received socket if valid. */
3286 PERROR("close received socket");
3290 if (relayd_created
) {
3298 * Try to lock the stream mutex.
3300 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3302 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3309 * Try to lock the stream mutex. On failure, we know that the stream is
3310 * being used else where hence there is data still being extracted.
3312 ret
= pthread_mutex_trylock(&stream
->lock
);
3314 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3326 * Search for a relayd associated to the session id and return the reference.
3328 * A rcu read side lock MUST be acquire before calling this function and locked
3329 * until the relayd object is no longer necessary.
3331 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3333 struct lttng_ht_iter iter
;
3334 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3336 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3337 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3340 * Check by sessiond id which is unique here where the relayd session
3341 * id might not be when having multiple relayd.
3343 if (relayd
->sessiond_session_id
== id
) {
3344 /* Found the relayd. There can be only one per id. */
3356 * Check if for a given session id there is still data needed to be extract
3359 * Return 1 if data is pending or else 0 meaning ready to be read.
3361 int consumer_data_pending(uint64_t id
)
3364 struct lttng_ht_iter iter
;
3365 struct lttng_ht
*ht
;
3366 struct lttng_consumer_stream
*stream
;
3367 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3368 int (*data_pending
)(struct lttng_consumer_stream
*);
3370 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3373 pthread_mutex_lock(&consumer_data
.lock
);
3375 switch (consumer_data
.type
) {
3376 case LTTNG_CONSUMER_KERNEL
:
3377 data_pending
= lttng_kconsumer_data_pending
;
3379 case LTTNG_CONSUMER32_UST
:
3380 case LTTNG_CONSUMER64_UST
:
3381 data_pending
= lttng_ustconsumer_data_pending
;
3384 ERR("Unknown consumer data type");
3388 /* Ease our life a bit */
3389 ht
= consumer_data
.stream_list_ht
;
3391 relayd
= find_relayd_by_session_id(id
);
3393 /* Send init command for data pending. */
3394 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3395 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3396 relayd
->relayd_session_id
);
3397 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3399 /* Communication error thus the relayd so no data pending. */
3400 goto data_not_pending
;
3404 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3405 ht
->hash_fct(&id
, lttng_ht_seed
),
3407 &iter
.iter
, stream
, node_session_id
.node
) {
3408 /* If this call fails, the stream is being used hence data pending. */
3409 ret
= stream_try_lock(stream
);
3415 * A removed node from the hash table indicates that the stream has
3416 * been deleted thus having a guarantee that the buffers are closed
3417 * on the consumer side. However, data can still be transmitted
3418 * over the network so don't skip the relayd check.
3420 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3423 * An empty output file is not valid. We need at least one packet
3424 * generated per stream, even if it contains no event, so it
3425 * contains at least one packet header.
3427 if (stream
->output_written
== 0) {
3428 pthread_mutex_unlock(&stream
->lock
);
3431 /* Check the stream if there is data in the buffers. */
3432 ret
= data_pending(stream
);
3434 pthread_mutex_unlock(&stream
->lock
);
3441 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3442 if (stream
->metadata_flag
) {
3443 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3444 stream
->relayd_stream_id
);
3446 ret
= relayd_data_pending(&relayd
->control_sock
,
3447 stream
->relayd_stream_id
,
3448 stream
->next_net_seq_num
- 1);
3450 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3452 pthread_mutex_unlock(&stream
->lock
);
3456 pthread_mutex_unlock(&stream
->lock
);
3460 unsigned int is_data_inflight
= 0;
3462 /* Send init command for data pending. */
3463 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3464 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3465 relayd
->relayd_session_id
, &is_data_inflight
);
3466 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3468 goto data_not_pending
;
3470 if (is_data_inflight
) {
3476 * Finding _no_ node in the hash table and no inflight data means that the
3477 * stream(s) have been removed thus data is guaranteed to be available for
3478 * analysis from the trace files.
3482 /* Data is available to be read by a viewer. */
3483 pthread_mutex_unlock(&consumer_data
.lock
);
3488 /* Data is still being extracted from buffers. */
3489 pthread_mutex_unlock(&consumer_data
.lock
);
3495 * Send a ret code status message to the sessiond daemon.
3497 * Return the sendmsg() return value.
3499 int consumer_send_status_msg(int sock
, int ret_code
)
3501 struct lttcomm_consumer_status_msg msg
;
3503 msg
.ret_code
= ret_code
;
3505 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3509 * Send a channel status message to the sessiond daemon.
3511 * Return the sendmsg() return value.
3513 int consumer_send_status_channel(int sock
,
3514 struct lttng_consumer_channel
*channel
)
3516 struct lttcomm_consumer_status_channel msg
;
3521 msg
.ret_code
= -LTTNG_ERR_UST_CHAN_FAIL
;
3523 msg
.ret_code
= LTTNG_OK
;
3524 msg
.key
= channel
->key
;
3525 msg
.stream_count
= channel
->streams
.count
;
3528 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3532 * Using a maximum stream size with the produced and consumed position of a
3533 * stream, computes the new consumed position to be as close as possible to the
3534 * maximum possible stream size.
3536 * If maximum stream size is lower than the possible buffer size (produced -
3537 * consumed), the consumed_pos given is returned untouched else the new value
3540 unsigned long consumer_get_consumed_maxsize(unsigned long consumed_pos
,
3541 unsigned long produced_pos
, uint64_t max_stream_size
)
3543 if (max_stream_size
&& max_stream_size
< (produced_pos
- consumed_pos
)) {
3544 /* Offset from the produced position to get the latest buffers. */
3545 return produced_pos
- max_stream_size
;
3548 return consumed_pos
;