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
);
295 /* Delete streams that might have been left in the stream list. */
296 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
298 cds_list_del(&stream
->send_node
);
300 * Once a stream is added to this list, the buffers were created so
301 * we have a guarantee that this call will succeed.
303 consumer_stream_destroy(stream
, NULL
);
306 switch (consumer_data
.type
) {
307 case LTTNG_CONSUMER_KERNEL
:
309 case LTTNG_CONSUMER32_UST
:
310 case LTTNG_CONSUMER64_UST
:
311 lttng_ustconsumer_del_channel(channel
);
314 ERR("Unknown consumer_data type");
320 iter
.iter
.node
= &channel
->node
.node
;
321 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
325 call_rcu(&channel
->node
.head
, free_channel_rcu
);
327 pthread_mutex_unlock(&consumer_data
.lock
);
331 * Iterate over the relayd hash table and destroy each element. Finally,
332 * destroy the whole hash table.
334 static void cleanup_relayd_ht(void)
336 struct lttng_ht_iter iter
;
337 struct consumer_relayd_sock_pair
*relayd
;
341 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
343 consumer_destroy_relayd(relayd
);
348 lttng_ht_destroy(consumer_data
.relayd_ht
);
352 * Update the end point status of all streams having the given network sequence
353 * index (relayd index).
355 * It's atomically set without having the stream mutex locked which is fine
356 * because we handle the write/read race with a pipe wakeup for each thread.
358 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
359 enum consumer_endpoint_status status
)
361 struct lttng_ht_iter iter
;
362 struct lttng_consumer_stream
*stream
;
364 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
368 /* Let's begin with metadata */
369 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
370 if (stream
->net_seq_idx
== net_seq_idx
) {
371 uatomic_set(&stream
->endpoint_status
, status
);
372 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
376 /* Follow up by the data streams */
377 cds_lfht_for_each_entry(data_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 data stream %d", stream
->wait_fd
);
387 * Cleanup a relayd object by flagging every associated streams for deletion,
388 * destroying the object meaning removing it from the relayd hash table,
389 * closing the sockets and freeing the memory in a RCU call.
391 * If a local data context is available, notify the threads that the streams'
392 * state have changed.
394 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
395 struct lttng_consumer_local_data
*ctx
)
401 DBG("Cleaning up relayd sockets");
403 /* Save the net sequence index before destroying the object */
404 netidx
= relayd
->net_seq_idx
;
407 * Delete the relayd from the relayd hash table, close the sockets and free
408 * the object in a RCU call.
410 consumer_destroy_relayd(relayd
);
412 /* Set inactive endpoint to all streams */
413 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
416 * With a local data context, notify the threads that the streams' state
417 * have changed. The write() action on the pipe acts as an "implicit"
418 * memory barrier ordering the updates of the end point status from the
419 * read of this status which happens AFTER receiving this notify.
422 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
423 notify_thread_lttng_pipe(ctx
->consumer_metadata_pipe
);
428 * Flag a relayd socket pair for destruction. Destroy it if the refcount
431 * RCU read side lock MUST be aquired before calling this function.
433 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
437 /* Set destroy flag for this object */
438 uatomic_set(&relayd
->destroy_flag
, 1);
440 /* Destroy the relayd if refcount is 0 */
441 if (uatomic_read(&relayd
->refcount
) == 0) {
442 consumer_destroy_relayd(relayd
);
447 * Completly destroy stream from every visiable data structure and the given
450 * One this call returns, the stream object is not longer usable nor visible.
452 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
455 consumer_stream_destroy(stream
, ht
);
458 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
460 enum lttng_consumer_stream_state state
,
461 const char *channel_name
,
468 enum consumer_channel_type type
,
469 unsigned int monitor
)
472 struct lttng_consumer_stream
*stream
;
474 stream
= zmalloc(sizeof(*stream
));
475 if (stream
== NULL
) {
476 PERROR("malloc struct lttng_consumer_stream");
483 stream
->key
= stream_key
;
485 stream
->out_fd_offset
= 0;
486 stream
->state
= state
;
489 stream
->net_seq_idx
= relayd_id
;
490 stream
->session_id
= session_id
;
491 stream
->monitor
= monitor
;
492 pthread_mutex_init(&stream
->lock
, NULL
);
494 /* If channel is the metadata, flag this stream as metadata. */
495 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
496 stream
->metadata_flag
= 1;
497 /* Metadata is flat out. */
498 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
500 /* Format stream name to <channel_name>_<cpu_number> */
501 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
504 PERROR("snprintf stream name");
509 /* Key is always the wait_fd for streams. */
510 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
512 /* Init node per channel id key */
513 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
515 /* Init session id node with the stream session id */
516 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
518 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
519 " relayd_id %" PRIu64
", session_id %" PRIu64
,
520 stream
->name
, stream
->key
, channel_key
,
521 stream
->net_seq_idx
, stream
->session_id
);
537 * Add a stream to the global list protected by a mutex.
539 static int add_stream(struct lttng_consumer_stream
*stream
,
547 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
549 pthread_mutex_lock(&consumer_data
.lock
);
550 pthread_mutex_lock(&stream
->lock
);
553 /* Steal stream identifier to avoid having streams with the same key */
554 steal_stream_key(stream
->key
, ht
);
556 lttng_ht_add_unique_u64(ht
, &stream
->node
);
558 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
559 &stream
->node_channel_id
);
562 * Add stream to the stream_list_ht of the consumer data. No need to steal
563 * the key since the HT does not use it and we allow to add redundant keys
566 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
569 * When nb_init_stream_left reaches 0, we don't need to trigger any action
570 * in terms of destroying the associated channel, because the action that
571 * causes the count to become 0 also causes a stream to be added. The
572 * channel deletion will thus be triggered by the following removal of this
575 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
576 /* Increment refcount before decrementing nb_init_stream_left */
578 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
581 /* Update consumer data once the node is inserted. */
582 consumer_data
.stream_count
++;
583 consumer_data
.need_update
= 1;
586 pthread_mutex_unlock(&stream
->lock
);
587 pthread_mutex_unlock(&consumer_data
.lock
);
593 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
594 * be acquired before calling this.
596 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
599 struct lttng_ht_node_u64
*node
;
600 struct lttng_ht_iter iter
;
604 lttng_ht_lookup(consumer_data
.relayd_ht
,
605 &relayd
->net_seq_idx
, &iter
);
606 node
= lttng_ht_iter_get_node_u64(&iter
);
610 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
617 * Allocate and return a consumer relayd socket.
619 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
620 uint64_t net_seq_idx
)
622 struct consumer_relayd_sock_pair
*obj
= NULL
;
624 /* net sequence index of -1 is a failure */
625 if (net_seq_idx
== (uint64_t) -1ULL) {
629 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
631 PERROR("zmalloc relayd sock");
635 obj
->net_seq_idx
= net_seq_idx
;
637 obj
->destroy_flag
= 0;
638 obj
->control_sock
.sock
.fd
= -1;
639 obj
->data_sock
.sock
.fd
= -1;
640 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
641 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
648 * Find a relayd socket pair in the global consumer data.
650 * Return the object if found else NULL.
651 * RCU read-side lock must be held across this call and while using the
654 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
656 struct lttng_ht_iter iter
;
657 struct lttng_ht_node_u64
*node
;
658 struct consumer_relayd_sock_pair
*relayd
= NULL
;
660 /* Negative keys are lookup failures */
661 if (key
== (uint64_t) -1ULL) {
665 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
667 node
= lttng_ht_iter_get_node_u64(&iter
);
669 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
677 * Find a relayd and send the stream
679 * Returns 0 on success, < 0 on error
681 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
685 struct consumer_relayd_sock_pair
*relayd
;
688 assert(stream
->net_seq_idx
!= -1ULL);
691 /* The stream is not metadata. Get relayd reference if exists. */
693 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
694 if (relayd
!= NULL
) {
695 /* Add stream on the relayd */
696 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
697 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
698 path
, &stream
->relayd_stream_id
,
699 stream
->chan
->tracefile_size
, stream
->chan
->tracefile_count
);
700 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
704 uatomic_inc(&relayd
->refcount
);
705 stream
->sent_to_relayd
= 1;
707 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
708 stream
->key
, stream
->net_seq_idx
);
713 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
714 stream
->name
, stream
->key
, stream
->net_seq_idx
);
722 * Find a relayd and close the stream
724 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
726 struct consumer_relayd_sock_pair
*relayd
;
728 /* The stream is not metadata. Get relayd reference if exists. */
730 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
732 consumer_stream_relayd_close(stream
, relayd
);
738 * Handle stream for relayd transmission if the stream applies for network
739 * streaming where the net sequence index is set.
741 * Return destination file descriptor or negative value on error.
743 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
744 size_t data_size
, unsigned long padding
,
745 struct consumer_relayd_sock_pair
*relayd
)
748 struct lttcomm_relayd_data_hdr data_hdr
;
754 /* Reset data header */
755 memset(&data_hdr
, 0, sizeof(data_hdr
));
757 if (stream
->metadata_flag
) {
758 /* Caller MUST acquire the relayd control socket lock */
759 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
764 /* Metadata are always sent on the control socket. */
765 outfd
= relayd
->control_sock
.sock
.fd
;
767 /* Set header with stream information */
768 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
769 data_hdr
.data_size
= htobe32(data_size
);
770 data_hdr
.padding_size
= htobe32(padding
);
772 * Note that net_seq_num below is assigned with the *current* value of
773 * next_net_seq_num and only after that the next_net_seq_num will be
774 * increment. This is why when issuing a command on the relayd using
775 * this next value, 1 should always be substracted in order to compare
776 * the last seen sequence number on the relayd side to the last sent.
778 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
779 /* Other fields are zeroed previously */
781 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
787 ++stream
->next_net_seq_num
;
789 /* Set to go on data socket */
790 outfd
= relayd
->data_sock
.sock
.fd
;
798 * Allocate and return a new lttng_consumer_channel object using the given key
799 * to initialize the hash table node.
801 * On error, return NULL.
803 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
805 const char *pathname
,
810 enum lttng_event_output output
,
811 uint64_t tracefile_size
,
812 uint64_t tracefile_count
,
813 uint64_t session_id_per_pid
,
814 unsigned int monitor
)
816 struct lttng_consumer_channel
*channel
;
818 channel
= zmalloc(sizeof(*channel
));
819 if (channel
== NULL
) {
820 PERROR("malloc struct lttng_consumer_channel");
825 channel
->refcount
= 0;
826 channel
->session_id
= session_id
;
827 channel
->session_id_per_pid
= session_id_per_pid
;
830 channel
->relayd_id
= relayd_id
;
831 channel
->output
= output
;
832 channel
->tracefile_size
= tracefile_size
;
833 channel
->tracefile_count
= tracefile_count
;
834 channel
->monitor
= monitor
;
837 * In monitor mode, the streams associated with the channel will be put in
838 * a special list ONLY owned by this channel. So, the refcount is set to 1
839 * here meaning that the channel itself has streams that are referenced.
841 * On a channel deletion, once the channel is no longer visible, the
842 * refcount is decremented and checked for a zero value to delete it. With
843 * streams in no monitor mode, it will now be safe to destroy the channel.
845 if (!channel
->monitor
) {
846 channel
->refcount
= 1;
849 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
850 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
852 strncpy(channel
->name
, name
, sizeof(channel
->name
));
853 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
855 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
857 channel
->wait_fd
= -1;
859 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
861 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
868 * Add a channel to the global list protected by a mutex.
870 * On success 0 is returned else a negative value.
872 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
873 struct lttng_consumer_local_data
*ctx
)
876 struct lttng_ht_node_u64
*node
;
877 struct lttng_ht_iter iter
;
879 pthread_mutex_lock(&consumer_data
.lock
);
882 lttng_ht_lookup(consumer_data
.channel_ht
, &channel
->key
, &iter
);
883 node
= lttng_ht_iter_get_node_u64(&iter
);
885 /* Channel already exist. Ignore the insertion */
886 ERR("Consumer add channel key %" PRIu64
" already exists!",
892 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
896 pthread_mutex_unlock(&consumer_data
.lock
);
898 if (!ret
&& channel
->wait_fd
!= -1 &&
899 channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
900 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
906 * Allocate the pollfd structure and the local view of the out fds to avoid
907 * doing a lookup in the linked list and concurrency issues when writing is
908 * needed. Called with consumer_data.lock held.
910 * Returns the number of fds in the structures.
912 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
913 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
917 struct lttng_ht_iter iter
;
918 struct lttng_consumer_stream
*stream
;
923 assert(local_stream
);
925 DBG("Updating poll fd array");
927 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
929 * Only active streams with an active end point can be added to the
930 * poll set and local stream storage of the thread.
932 * There is a potential race here for endpoint_status to be updated
933 * just after the check. However, this is OK since the stream(s) will
934 * be deleted once the thread is notified that the end point state has
935 * changed where this function will be called back again.
937 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
938 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
942 * This clobbers way too much the debug output. Uncomment that if you
943 * need it for debugging purposes.
945 * DBG("Active FD %d", stream->wait_fd);
947 (*pollfd
)[i
].fd
= stream
->wait_fd
;
948 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
949 local_stream
[i
] = stream
;
955 * Insert the consumer_data_pipe at the end of the array and don't
956 * increment i so nb_fd is the number of real FD.
958 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
959 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
964 * Poll on the should_quit pipe and the command socket return -1 on error and
965 * should exit, 0 if data is available on the command socket
967 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
972 num_rdy
= poll(consumer_sockpoll
, 2, -1);
975 * Restart interrupted system call.
977 if (errno
== EINTR
) {
980 PERROR("Poll error");
983 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
984 DBG("consumer_should_quit wake up");
994 * Set the error socket.
996 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
999 ctx
->consumer_error_socket
= sock
;
1003 * Set the command socket path.
1005 void lttng_consumer_set_command_sock_path(
1006 struct lttng_consumer_local_data
*ctx
, char *sock
)
1008 ctx
->consumer_command_sock_path
= sock
;
1012 * Send return code to the session daemon.
1013 * If the socket is not defined, we return 0, it is not a fatal error
1015 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1017 if (ctx
->consumer_error_socket
> 0) {
1018 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1019 sizeof(enum lttcomm_sessiond_command
));
1026 * Close all the tracefiles and stream fds and MUST be called when all
1027 * instances are destroyed i.e. when all threads were joined and are ended.
1029 void lttng_consumer_cleanup(void)
1031 struct lttng_ht_iter iter
;
1032 struct lttng_consumer_channel
*channel
;
1036 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1038 consumer_del_channel(channel
);
1043 lttng_ht_destroy(consumer_data
.channel_ht
);
1045 cleanup_relayd_ht();
1047 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1050 * This HT contains streams that are freed by either the metadata thread or
1051 * the data thread so we do *nothing* on the hash table and simply destroy
1054 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1058 * Called from signal handler.
1060 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1065 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1066 } while (ret
< 0 && errno
== EINTR
);
1067 if (ret
< 0 || ret
!= 1) {
1068 PERROR("write consumer quit");
1071 DBG("Consumer flag that it should quit");
1074 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1077 int outfd
= stream
->out_fd
;
1080 * This does a blocking write-and-wait on any page that belongs to the
1081 * subbuffer prior to the one we just wrote.
1082 * Don't care about error values, as these are just hints and ways to
1083 * limit the amount of page cache used.
1085 if (orig_offset
< stream
->max_sb_size
) {
1088 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1089 stream
->max_sb_size
,
1090 SYNC_FILE_RANGE_WAIT_BEFORE
1091 | SYNC_FILE_RANGE_WRITE
1092 | SYNC_FILE_RANGE_WAIT_AFTER
);
1094 * Give hints to the kernel about how we access the file:
1095 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1098 * We need to call fadvise again after the file grows because the
1099 * kernel does not seem to apply fadvise to non-existing parts of the
1102 * Call fadvise _after_ having waited for the page writeback to
1103 * complete because the dirty page writeback semantic is not well
1104 * defined. So it can be expected to lead to lower throughput in
1107 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1108 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1112 * Initialise the necessary environnement :
1113 * - create a new context
1114 * - create the poll_pipe
1115 * - create the should_quit pipe (for signal handler)
1116 * - create the thread pipe (for splice)
1118 * Takes a function pointer as argument, this function is called when data is
1119 * available on a buffer. This function is responsible to do the
1120 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1121 * buffer configuration and then kernctl_put_next_subbuf at the end.
1123 * Returns a pointer to the new context or NULL on error.
1125 struct lttng_consumer_local_data
*lttng_consumer_create(
1126 enum lttng_consumer_type type
,
1127 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1128 struct lttng_consumer_local_data
*ctx
),
1129 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1130 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1131 int (*update_stream
)(int stream_key
, uint32_t state
))
1134 struct lttng_consumer_local_data
*ctx
;
1136 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1137 consumer_data
.type
== type
);
1138 consumer_data
.type
= type
;
1140 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1142 PERROR("allocating context");
1146 ctx
->consumer_error_socket
= -1;
1147 ctx
->consumer_metadata_socket
= -1;
1148 /* assign the callbacks */
1149 ctx
->on_buffer_ready
= buffer_ready
;
1150 ctx
->on_recv_channel
= recv_channel
;
1151 ctx
->on_recv_stream
= recv_stream
;
1152 ctx
->on_update_stream
= update_stream
;
1154 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1155 if (!ctx
->consumer_data_pipe
) {
1156 goto error_poll_pipe
;
1159 ret
= pipe(ctx
->consumer_should_quit
);
1161 PERROR("Error creating recv pipe");
1162 goto error_quit_pipe
;
1165 ret
= pipe(ctx
->consumer_thread_pipe
);
1167 PERROR("Error creating thread pipe");
1168 goto error_thread_pipe
;
1171 ret
= pipe(ctx
->consumer_channel_pipe
);
1173 PERROR("Error creating channel pipe");
1174 goto error_channel_pipe
;
1177 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1178 if (!ctx
->consumer_metadata_pipe
) {
1179 goto error_metadata_pipe
;
1182 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1184 goto error_splice_pipe
;
1190 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1191 error_metadata_pipe
:
1192 utils_close_pipe(ctx
->consumer_channel_pipe
);
1194 utils_close_pipe(ctx
->consumer_thread_pipe
);
1196 utils_close_pipe(ctx
->consumer_should_quit
);
1198 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1206 * Close all fds associated with the instance and free the context.
1208 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1212 DBG("Consumer destroying it. Closing everything.");
1214 ret
= close(ctx
->consumer_error_socket
);
1218 ret
= close(ctx
->consumer_metadata_socket
);
1222 utils_close_pipe(ctx
->consumer_thread_pipe
);
1223 utils_close_pipe(ctx
->consumer_channel_pipe
);
1224 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1225 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1226 utils_close_pipe(ctx
->consumer_should_quit
);
1227 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1229 unlink(ctx
->consumer_command_sock_path
);
1234 * Write the metadata stream id on the specified file descriptor.
1236 static int write_relayd_metadata_id(int fd
,
1237 struct lttng_consumer_stream
*stream
,
1238 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1241 struct lttcomm_relayd_metadata_payload hdr
;
1243 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1244 hdr
.padding_size
= htobe32(padding
);
1246 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1247 } while (ret
< 0 && errno
== EINTR
);
1248 if (ret
< 0 || ret
!= sizeof(hdr
)) {
1250 * This error means that the fd's end is closed so ignore the perror
1251 * not to clubber the error output since this can happen in a normal
1254 if (errno
!= EPIPE
) {
1255 PERROR("write metadata stream id");
1257 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1259 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1260 * handle writting the missing part so report that as an error and
1261 * don't lie to the caller.
1266 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1267 stream
->relayd_stream_id
, padding
);
1274 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1275 * core function for writing trace buffers to either the local filesystem or
1278 * It must be called with the stream lock held.
1280 * Careful review MUST be put if any changes occur!
1282 * Returns the number of bytes written
1284 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1285 struct lttng_consumer_local_data
*ctx
,
1286 struct lttng_consumer_stream
*stream
, unsigned long len
,
1287 unsigned long padding
)
1289 unsigned long mmap_offset
;
1291 ssize_t ret
= 0, written
= 0;
1292 off_t orig_offset
= stream
->out_fd_offset
;
1293 /* Default is on the disk */
1294 int outfd
= stream
->out_fd
;
1295 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1296 unsigned int relayd_hang_up
= 0;
1298 /* RCU lock for the relayd pointer */
1301 /* Flag that the current stream if set for network streaming. */
1302 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1303 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1304 if (relayd
== NULL
) {
1309 /* get the offset inside the fd to mmap */
1310 switch (consumer_data
.type
) {
1311 case LTTNG_CONSUMER_KERNEL
:
1312 mmap_base
= stream
->mmap_base
;
1313 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1315 case LTTNG_CONSUMER32_UST
:
1316 case LTTNG_CONSUMER64_UST
:
1317 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1319 ERR("read mmap get mmap base for stream %s", stream
->name
);
1323 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1327 ERR("Unknown consumer_data type");
1332 PERROR("tracer ctl get_mmap_read_offset");
1337 /* Handle stream on the relayd if the output is on the network */
1339 unsigned long netlen
= len
;
1342 * Lock the control socket for the complete duration of the function
1343 * since from this point on we will use the socket.
1345 if (stream
->metadata_flag
) {
1346 /* Metadata requires the control socket. */
1347 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1348 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1351 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1353 /* Use the returned socket. */
1356 /* Write metadata stream id before payload */
1357 if (stream
->metadata_flag
) {
1358 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1361 /* Socket operation failed. We consider the relayd dead */
1362 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1370 /* Socket operation failed. We consider the relayd dead */
1371 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1375 /* Else, use the default set before which is the filesystem. */
1378 /* No streaming, we have to set the len with the full padding */
1382 * Check if we need to change the tracefile before writing the packet.
1384 if (stream
->chan
->tracefile_size
> 0 &&
1385 (stream
->tracefile_size_current
+ len
) >
1386 stream
->chan
->tracefile_size
) {
1387 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1388 stream
->name
, stream
->chan
->tracefile_size
,
1389 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1390 stream
->out_fd
, &(stream
->tracefile_count_current
));
1392 ERR("Rotating output file");
1395 outfd
= stream
->out_fd
= ret
;
1396 /* Reset current size because we just perform a rotation. */
1397 stream
->tracefile_size_current
= 0;
1399 stream
->tracefile_size_current
+= len
;
1404 ret
= write(outfd
, mmap_base
+ mmap_offset
, len
);
1405 } while (ret
< 0 && errno
== EINTR
);
1406 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1409 * This is possible if the fd is closed on the other side (outfd)
1410 * or any write problem. It can be verbose a bit for a normal
1411 * execution if for instance the relayd is stopped abruptly. This
1412 * can happen so set this to a DBG statement.
1414 DBG("Error in file write mmap");
1418 /* Socket operation failed. We consider the relayd dead */
1419 if (errno
== EPIPE
|| errno
== EINVAL
) {
1424 } else if (ret
> len
) {
1425 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1433 /* This call is useless on a socket so better save a syscall. */
1435 /* This won't block, but will start writeout asynchronously */
1436 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1437 SYNC_FILE_RANGE_WRITE
);
1438 stream
->out_fd_offset
+= ret
;
1442 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1446 * This is a special case that the relayd has closed its socket. Let's
1447 * cleanup the relayd object and all associated streams.
1449 if (relayd
&& relayd_hang_up
) {
1450 cleanup_relayd(relayd
, ctx
);
1454 /* Unlock only if ctrl socket used */
1455 if (relayd
&& stream
->metadata_flag
) {
1456 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1464 * Splice the data from the ring buffer to the tracefile.
1466 * It must be called with the stream lock held.
1468 * Returns the number of bytes spliced.
1470 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1471 struct lttng_consumer_local_data
*ctx
,
1472 struct lttng_consumer_stream
*stream
, unsigned long len
,
1473 unsigned long padding
)
1475 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1477 off_t orig_offset
= stream
->out_fd_offset
;
1478 int fd
= stream
->wait_fd
;
1479 /* Default is on the disk */
1480 int outfd
= stream
->out_fd
;
1481 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1483 unsigned int relayd_hang_up
= 0;
1485 switch (consumer_data
.type
) {
1486 case LTTNG_CONSUMER_KERNEL
:
1488 case LTTNG_CONSUMER32_UST
:
1489 case LTTNG_CONSUMER64_UST
:
1490 /* Not supported for user space tracing */
1493 ERR("Unknown consumer_data type");
1497 /* RCU lock for the relayd pointer */
1500 /* Flag that the current stream if set for network streaming. */
1501 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1502 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1503 if (relayd
== NULL
) {
1509 * Choose right pipe for splice. Metadata and trace data are handled by
1510 * different threads hence the use of two pipes in order not to race or
1511 * corrupt the written data.
1513 if (stream
->metadata_flag
) {
1514 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1516 splice_pipe
= ctx
->consumer_thread_pipe
;
1519 /* Write metadata stream id before payload */
1521 int total_len
= len
;
1523 if (stream
->metadata_flag
) {
1525 * Lock the control socket for the complete duration of the function
1526 * since from this point on we will use the socket.
1528 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1530 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1534 /* Socket operation failed. We consider the relayd dead */
1535 if (ret
== -EBADF
) {
1536 WARN("Remote relayd disconnected. Stopping");
1543 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1546 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1548 /* Use the returned socket. */
1551 /* Socket operation failed. We consider the relayd dead */
1552 if (ret
== -EBADF
) {
1553 WARN("Remote relayd disconnected. Stopping");
1560 /* No streaming, we have to set the len with the full padding */
1564 * Check if we need to change the tracefile before writing the packet.
1566 if (stream
->chan
->tracefile_size
> 0 &&
1567 (stream
->tracefile_size_current
+ len
) >
1568 stream
->chan
->tracefile_size
) {
1569 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1570 stream
->name
, stream
->chan
->tracefile_size
,
1571 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1572 stream
->out_fd
, &(stream
->tracefile_count_current
));
1574 ERR("Rotating output file");
1577 outfd
= stream
->out_fd
= ret
;
1578 /* Reset current size because we just perform a rotation. */
1579 stream
->tracefile_size_current
= 0;
1581 stream
->tracefile_size_current
+= len
;
1585 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1586 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1587 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1588 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1589 DBG("splice chan to pipe, ret %zd", ret_splice
);
1590 if (ret_splice
< 0) {
1591 PERROR("Error in relay splice");
1593 written
= ret_splice
;
1599 /* Handle stream on the relayd if the output is on the network */
1601 if (stream
->metadata_flag
) {
1602 size_t metadata_payload_size
=
1603 sizeof(struct lttcomm_relayd_metadata_payload
);
1605 /* Update counter to fit the spliced data */
1606 ret_splice
+= metadata_payload_size
;
1607 len
+= metadata_payload_size
;
1609 * We do this so the return value can match the len passed as
1610 * argument to this function.
1612 written
-= metadata_payload_size
;
1616 /* Splice data out */
1617 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1618 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1619 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1620 if (ret_splice
< 0) {
1621 PERROR("Error in file splice");
1623 written
= ret_splice
;
1625 /* Socket operation failed. We consider the relayd dead */
1626 if (errno
== EBADF
|| errno
== EPIPE
) {
1627 WARN("Remote relayd disconnected. Stopping");
1633 } else if (ret_splice
> len
) {
1635 PERROR("Wrote more data than requested %zd (len: %lu)",
1637 written
+= ret_splice
;
1643 /* This call is useless on a socket so better save a syscall. */
1645 /* This won't block, but will start writeout asynchronously */
1646 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1647 SYNC_FILE_RANGE_WRITE
);
1648 stream
->out_fd_offset
+= ret_splice
;
1650 written
+= ret_splice
;
1652 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1660 * This is a special case that the relayd has closed its socket. Let's
1661 * cleanup the relayd object and all associated streams.
1663 if (relayd
&& relayd_hang_up
) {
1664 cleanup_relayd(relayd
, ctx
);
1665 /* Skip splice error so the consumer does not fail */
1670 /* send the appropriate error description to sessiond */
1673 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1676 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1679 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1684 if (relayd
&& stream
->metadata_flag
) {
1685 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1693 * Take a snapshot for a specific fd
1695 * Returns 0 on success, < 0 on error
1697 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1699 switch (consumer_data
.type
) {
1700 case LTTNG_CONSUMER_KERNEL
:
1701 return lttng_kconsumer_take_snapshot(stream
);
1702 case LTTNG_CONSUMER32_UST
:
1703 case LTTNG_CONSUMER64_UST
:
1704 return lttng_ustconsumer_take_snapshot(stream
);
1706 ERR("Unknown consumer_data type");
1713 * Get the produced position
1715 * Returns 0 on success, < 0 on error
1717 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1720 switch (consumer_data
.type
) {
1721 case LTTNG_CONSUMER_KERNEL
:
1722 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1723 case LTTNG_CONSUMER32_UST
:
1724 case LTTNG_CONSUMER64_UST
:
1725 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1727 ERR("Unknown consumer_data type");
1733 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1734 int sock
, struct pollfd
*consumer_sockpoll
)
1736 switch (consumer_data
.type
) {
1737 case LTTNG_CONSUMER_KERNEL
:
1738 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1739 case LTTNG_CONSUMER32_UST
:
1740 case LTTNG_CONSUMER64_UST
:
1741 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1743 ERR("Unknown consumer_data type");
1750 * Iterate over all streams of the hashtable and free them properly.
1752 * WARNING: *MUST* be used with data stream only.
1754 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1756 struct lttng_ht_iter iter
;
1757 struct lttng_consumer_stream
*stream
;
1764 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1766 * Ignore return value since we are currently cleaning up so any error
1769 (void) consumer_del_stream(stream
, ht
);
1773 lttng_ht_destroy(ht
);
1777 * Iterate over all streams of the hashtable and free them properly.
1779 * XXX: Should not be only for metadata stream or else use an other name.
1781 static void destroy_stream_ht(struct lttng_ht
*ht
)
1783 struct lttng_ht_iter iter
;
1784 struct lttng_consumer_stream
*stream
;
1791 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1793 * Ignore return value since we are currently cleaning up so any error
1796 (void) consumer_del_metadata_stream(stream
, ht
);
1800 lttng_ht_destroy(ht
);
1803 void lttng_consumer_close_metadata(void)
1805 switch (consumer_data
.type
) {
1806 case LTTNG_CONSUMER_KERNEL
:
1808 * The Kernel consumer has a different metadata scheme so we don't
1809 * close anything because the stream will be closed by the session
1813 case LTTNG_CONSUMER32_UST
:
1814 case LTTNG_CONSUMER64_UST
:
1816 * Close all metadata streams. The metadata hash table is passed and
1817 * this call iterates over it by closing all wakeup fd. This is safe
1818 * because at this point we are sure that the metadata producer is
1819 * either dead or blocked.
1821 lttng_ustconsumer_close_metadata(metadata_ht
);
1824 ERR("Unknown consumer_data type");
1830 * Clean up a metadata stream and free its memory.
1832 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1833 struct lttng_ht
*ht
)
1836 struct lttng_ht_iter iter
;
1837 struct lttng_consumer_channel
*free_chan
= NULL
;
1838 struct consumer_relayd_sock_pair
*relayd
;
1842 * This call should NEVER receive regular stream. It must always be
1843 * metadata stream and this is crucial for data structure synchronization.
1845 assert(stream
->metadata_flag
);
1847 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1850 /* Means the stream was allocated but not successfully added */
1851 goto free_stream_rcu
;
1854 pthread_mutex_lock(&consumer_data
.lock
);
1855 pthread_mutex_lock(&stream
->lock
);
1857 switch (consumer_data
.type
) {
1858 case LTTNG_CONSUMER_KERNEL
:
1859 if (stream
->mmap_base
!= NULL
) {
1860 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1862 PERROR("munmap metadata stream");
1865 if (stream
->wait_fd
>= 0) {
1866 ret
= close(stream
->wait_fd
);
1868 PERROR("close kernel metadata wait_fd");
1872 case LTTNG_CONSUMER32_UST
:
1873 case LTTNG_CONSUMER64_UST
:
1874 lttng_ustconsumer_del_stream(stream
);
1877 ERR("Unknown consumer_data type");
1883 iter
.iter
.node
= &stream
->node
.node
;
1884 ret
= lttng_ht_del(ht
, &iter
);
1887 iter
.iter
.node
= &stream
->node_channel_id
.node
;
1888 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
1891 iter
.iter
.node
= &stream
->node_session_id
.node
;
1892 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1896 if (stream
->out_fd
>= 0) {
1897 ret
= close(stream
->out_fd
);
1903 /* Check and cleanup relayd */
1905 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1906 if (relayd
!= NULL
) {
1907 uatomic_dec(&relayd
->refcount
);
1908 assert(uatomic_read(&relayd
->refcount
) >= 0);
1910 /* Closing streams requires to lock the control socket. */
1911 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1912 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1913 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1914 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1916 DBG("Unable to close stream on the relayd. Continuing");
1918 * Continue here. There is nothing we can do for the relayd.
1919 * Chances are that the relayd has closed the socket so we just
1920 * continue cleaning up.
1924 /* Both conditions are met, we destroy the relayd. */
1925 if (uatomic_read(&relayd
->refcount
) == 0 &&
1926 uatomic_read(&relayd
->destroy_flag
)) {
1927 consumer_destroy_relayd(relayd
);
1932 /* Atomically decrement channel refcount since other threads can use it. */
1933 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
1934 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
1935 /* Go for channel deletion! */
1936 free_chan
= stream
->chan
;
1941 * Nullify the stream reference so it is not used after deletion. The
1942 * consumer data lock MUST be acquired before being able to check for a
1943 * NULL pointer value.
1945 stream
->chan
->metadata_stream
= NULL
;
1947 pthread_mutex_unlock(&stream
->lock
);
1948 pthread_mutex_unlock(&consumer_data
.lock
);
1951 consumer_del_channel(free_chan
);
1955 call_rcu(&stream
->node
.head
, free_stream_rcu
);
1959 * Action done with the metadata stream when adding it to the consumer internal
1960 * data structures to handle it.
1962 static int add_metadata_stream(struct lttng_consumer_stream
*stream
,
1963 struct lttng_ht
*ht
)
1966 struct lttng_ht_iter iter
;
1967 struct lttng_ht_node_u64
*node
;
1972 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
1974 pthread_mutex_lock(&consumer_data
.lock
);
1975 pthread_mutex_lock(&stream
->lock
);
1978 * From here, refcounts are updated so be _careful_ when returning an error
1985 * Lookup the stream just to make sure it does not exist in our internal
1986 * state. This should NEVER happen.
1988 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
1989 node
= lttng_ht_iter_get_node_u64(&iter
);
1993 * When nb_init_stream_left reaches 0, we don't need to trigger any action
1994 * in terms of destroying the associated channel, because the action that
1995 * causes the count to become 0 also causes a stream to be added. The
1996 * channel deletion will thus be triggered by the following removal of this
1999 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2000 /* Increment refcount before decrementing nb_init_stream_left */
2002 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2005 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2007 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2008 &stream
->node_channel_id
);
2011 * Add stream to the stream_list_ht of the consumer data. No need to steal
2012 * the key since the HT does not use it and we allow to add redundant keys
2015 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2019 pthread_mutex_unlock(&stream
->lock
);
2020 pthread_mutex_unlock(&consumer_data
.lock
);
2025 * Delete data stream that are flagged for deletion (endpoint_status).
2027 static void validate_endpoint_status_data_stream(void)
2029 struct lttng_ht_iter iter
;
2030 struct lttng_consumer_stream
*stream
;
2032 DBG("Consumer delete flagged data stream");
2035 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2036 /* Validate delete flag of the stream */
2037 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2040 /* Delete it right now */
2041 consumer_del_stream(stream
, data_ht
);
2047 * Delete metadata stream that are flagged for deletion (endpoint_status).
2049 static void validate_endpoint_status_metadata_stream(
2050 struct lttng_poll_event
*pollset
)
2052 struct lttng_ht_iter iter
;
2053 struct lttng_consumer_stream
*stream
;
2055 DBG("Consumer delete flagged metadata stream");
2060 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2061 /* Validate delete flag of the stream */
2062 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2066 * Remove from pollset so the metadata thread can continue without
2067 * blocking on a deleted stream.
2069 lttng_poll_del(pollset
, stream
->wait_fd
);
2071 /* Delete it right now */
2072 consumer_del_metadata_stream(stream
, metadata_ht
);
2078 * Thread polls on metadata file descriptor and write them on disk or on the
2081 void *consumer_thread_metadata_poll(void *data
)
2084 uint32_t revents
, nb_fd
;
2085 struct lttng_consumer_stream
*stream
= NULL
;
2086 struct lttng_ht_iter iter
;
2087 struct lttng_ht_node_u64
*node
;
2088 struct lttng_poll_event events
;
2089 struct lttng_consumer_local_data
*ctx
= data
;
2092 rcu_register_thread();
2094 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2096 /* ENOMEM at this point. Better to bail out. */
2100 DBG("Thread metadata poll started");
2102 /* Size is set to 1 for the consumer_metadata pipe */
2103 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2105 ERR("Poll set creation failed");
2109 ret
= lttng_poll_add(&events
,
2110 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2116 DBG("Metadata main loop started");
2119 /* Only the metadata pipe is set */
2120 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2125 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2126 ret
= lttng_poll_wait(&events
, -1);
2127 DBG("Metadata event catched in thread");
2129 if (errno
== EINTR
) {
2130 ERR("Poll EINTR catched");
2138 /* From here, the event is a metadata wait fd */
2139 for (i
= 0; i
< nb_fd
; i
++) {
2140 revents
= LTTNG_POLL_GETEV(&events
, i
);
2141 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2143 /* Just don't waste time if no returned events for the fd */
2148 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2149 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2150 DBG("Metadata thread pipe hung up");
2152 * Remove the pipe from the poll set and continue the loop
2153 * since their might be data to consume.
2155 lttng_poll_del(&events
,
2156 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2157 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2159 } else if (revents
& LPOLLIN
) {
2162 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2163 &stream
, sizeof(stream
));
2165 ERR("read metadata stream, ret: %ld", pipe_len
);
2167 * Continue here to handle the rest of the streams.
2172 /* A NULL stream means that the state has changed. */
2173 if (stream
== NULL
) {
2174 /* Check for deleted streams. */
2175 validate_endpoint_status_metadata_stream(&events
);
2179 DBG("Adding metadata stream %d to poll set",
2182 ret
= add_metadata_stream(stream
, metadata_ht
);
2184 ERR("Unable to add metadata stream");
2185 /* Stream was not setup properly. Continuing. */
2186 consumer_del_metadata_stream(stream
, NULL
);
2190 /* Add metadata stream to the global poll events list */
2191 lttng_poll_add(&events
, stream
->wait_fd
,
2192 LPOLLIN
| LPOLLPRI
);
2195 /* Handle other stream */
2201 uint64_t tmp_id
= (uint64_t) pollfd
;
2203 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2205 node
= lttng_ht_iter_get_node_u64(&iter
);
2208 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2211 /* Check for error event */
2212 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2213 DBG("Metadata fd %d is hup|err.", pollfd
);
2214 if (!stream
->hangup_flush_done
2215 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2216 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2217 DBG("Attempting to flush and consume the UST buffers");
2218 lttng_ustconsumer_on_stream_hangup(stream
);
2220 /* We just flushed the stream now read it. */
2222 len
= ctx
->on_buffer_ready(stream
, ctx
);
2224 * We don't check the return value here since if we get
2225 * a negative len, it means an error occured thus we
2226 * simply remove it from the poll set and free the
2232 lttng_poll_del(&events
, stream
->wait_fd
);
2234 * This call update the channel states, closes file descriptors
2235 * and securely free the stream.
2237 consumer_del_metadata_stream(stream
, metadata_ht
);
2238 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2239 /* Get the data out of the metadata file descriptor */
2240 DBG("Metadata available on fd %d", pollfd
);
2241 assert(stream
->wait_fd
== pollfd
);
2243 len
= ctx
->on_buffer_ready(stream
, ctx
);
2244 /* It's ok to have an unavailable sub-buffer */
2245 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2246 /* Clean up stream from consumer and free it. */
2247 lttng_poll_del(&events
, stream
->wait_fd
);
2248 consumer_del_metadata_stream(stream
, metadata_ht
);
2249 } else if (len
> 0) {
2250 stream
->data_read
= 1;
2254 /* Release RCU lock for the stream looked up */
2261 DBG("Metadata poll thread exiting");
2263 lttng_poll_clean(&events
);
2265 destroy_stream_ht(metadata_ht
);
2267 rcu_unregister_thread();
2272 * This thread polls the fds in the set to consume the data and write
2273 * it to tracefile if necessary.
2275 void *consumer_thread_data_poll(void *data
)
2277 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2278 struct pollfd
*pollfd
= NULL
;
2279 /* local view of the streams */
2280 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2281 /* local view of consumer_data.fds_count */
2283 struct lttng_consumer_local_data
*ctx
= data
;
2286 rcu_register_thread();
2288 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2289 if (data_ht
== NULL
) {
2290 /* ENOMEM at this point. Better to bail out. */
2294 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
*));
2295 if (local_stream
== NULL
) {
2296 PERROR("local_stream malloc");
2305 * the fds set has been updated, we need to update our
2306 * local array as well
2308 pthread_mutex_lock(&consumer_data
.lock
);
2309 if (consumer_data
.need_update
) {
2314 local_stream
= NULL
;
2316 /* allocate for all fds + 1 for the consumer_data_pipe */
2317 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2318 if (pollfd
== NULL
) {
2319 PERROR("pollfd malloc");
2320 pthread_mutex_unlock(&consumer_data
.lock
);
2324 /* allocate for all fds + 1 for the consumer_data_pipe */
2325 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2326 sizeof(struct lttng_consumer_stream
*));
2327 if (local_stream
== NULL
) {
2328 PERROR("local_stream malloc");
2329 pthread_mutex_unlock(&consumer_data
.lock
);
2332 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2335 ERR("Error in allocating pollfd or local_outfds");
2336 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2337 pthread_mutex_unlock(&consumer_data
.lock
);
2341 consumer_data
.need_update
= 0;
2343 pthread_mutex_unlock(&consumer_data
.lock
);
2345 /* No FDs and consumer_quit, consumer_cleanup the thread */
2346 if (nb_fd
== 0 && consumer_quit
== 1) {
2349 /* poll on the array of fds */
2351 DBG("polling on %d fd", nb_fd
+ 1);
2352 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2353 DBG("poll num_rdy : %d", num_rdy
);
2354 if (num_rdy
== -1) {
2356 * Restart interrupted system call.
2358 if (errno
== EINTR
) {
2361 PERROR("Poll error");
2362 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2364 } else if (num_rdy
== 0) {
2365 DBG("Polling thread timed out");
2370 * If the consumer_data_pipe triggered poll go directly to the
2371 * beginning of the loop to update the array. We want to prioritize
2372 * array update over low-priority reads.
2374 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2375 ssize_t pipe_readlen
;
2377 DBG("consumer_data_pipe wake up");
2378 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2379 &new_stream
, sizeof(new_stream
));
2380 if (pipe_readlen
< 0) {
2381 ERR("Consumer data pipe ret %ld", pipe_readlen
);
2382 /* Continue so we can at least handle the current stream(s). */
2387 * If the stream is NULL, just ignore it. It's also possible that
2388 * the sessiond poll thread changed the consumer_quit state and is
2389 * waking us up to test it.
2391 if (new_stream
== NULL
) {
2392 validate_endpoint_status_data_stream();
2396 ret
= add_stream(new_stream
, data_ht
);
2398 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
2401 * At this point, if the add_stream fails, it is not in the
2402 * hash table thus passing the NULL value here.
2404 consumer_del_stream(new_stream
, NULL
);
2407 /* Continue to update the local streams and handle prio ones */
2411 /* Take care of high priority channels first. */
2412 for (i
= 0; i
< nb_fd
; i
++) {
2413 if (local_stream
[i
] == NULL
) {
2416 if (pollfd
[i
].revents
& POLLPRI
) {
2417 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2419 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2420 /* it's ok to have an unavailable sub-buffer */
2421 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2422 /* Clean the stream and free it. */
2423 consumer_del_stream(local_stream
[i
], data_ht
);
2424 local_stream
[i
] = NULL
;
2425 } else if (len
> 0) {
2426 local_stream
[i
]->data_read
= 1;
2432 * If we read high prio channel in this loop, try again
2433 * for more high prio data.
2439 /* Take care of low priority channels. */
2440 for (i
= 0; i
< nb_fd
; i
++) {
2441 if (local_stream
[i
] == NULL
) {
2444 if ((pollfd
[i
].revents
& POLLIN
) ||
2445 local_stream
[i
]->hangup_flush_done
) {
2446 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2447 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2448 /* it's ok to have an unavailable sub-buffer */
2449 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2450 /* Clean the stream and free it. */
2451 consumer_del_stream(local_stream
[i
], data_ht
);
2452 local_stream
[i
] = NULL
;
2453 } else if (len
> 0) {
2454 local_stream
[i
]->data_read
= 1;
2459 /* Handle hangup and errors */
2460 for (i
= 0; i
< nb_fd
; i
++) {
2461 if (local_stream
[i
] == NULL
) {
2464 if (!local_stream
[i
]->hangup_flush_done
2465 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2466 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2467 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2468 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2470 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2471 /* Attempt read again, for the data we just flushed. */
2472 local_stream
[i
]->data_read
= 1;
2475 * If the poll flag is HUP/ERR/NVAL and we have
2476 * read no data in this pass, we can remove the
2477 * stream from its hash table.
2479 if ((pollfd
[i
].revents
& POLLHUP
)) {
2480 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2481 if (!local_stream
[i
]->data_read
) {
2482 consumer_del_stream(local_stream
[i
], data_ht
);
2483 local_stream
[i
] = NULL
;
2486 } else if (pollfd
[i
].revents
& POLLERR
) {
2487 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2488 if (!local_stream
[i
]->data_read
) {
2489 consumer_del_stream(local_stream
[i
], data_ht
);
2490 local_stream
[i
] = NULL
;
2493 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2494 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2495 if (!local_stream
[i
]->data_read
) {
2496 consumer_del_stream(local_stream
[i
], data_ht
);
2497 local_stream
[i
] = NULL
;
2501 if (local_stream
[i
] != NULL
) {
2502 local_stream
[i
]->data_read
= 0;
2507 DBG("polling thread exiting");
2512 * Close the write side of the pipe so epoll_wait() in
2513 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2514 * read side of the pipe. If we close them both, epoll_wait strangely does
2515 * not return and could create a endless wait period if the pipe is the
2516 * only tracked fd in the poll set. The thread will take care of closing
2519 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2521 destroy_data_stream_ht(data_ht
);
2523 rcu_unregister_thread();
2528 * Close wake-up end of each stream belonging to the channel. This will
2529 * allow the poll() on the stream read-side to detect when the
2530 * write-side (application) finally closes them.
2533 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2535 struct lttng_ht
*ht
;
2536 struct lttng_consumer_stream
*stream
;
2537 struct lttng_ht_iter iter
;
2539 ht
= consumer_data
.stream_per_chan_id_ht
;
2542 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2543 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2544 ht
->match_fct
, &channel
->key
,
2545 &iter
.iter
, stream
, node_channel_id
.node
) {
2547 * Protect against teardown with mutex.
2549 pthread_mutex_lock(&stream
->lock
);
2550 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2553 switch (consumer_data
.type
) {
2554 case LTTNG_CONSUMER_KERNEL
:
2556 case LTTNG_CONSUMER32_UST
:
2557 case LTTNG_CONSUMER64_UST
:
2559 * Note: a mutex is taken internally within
2560 * liblttng-ust-ctl to protect timer wakeup_fd
2561 * use from concurrent close.
2563 lttng_ustconsumer_close_stream_wakeup(stream
);
2566 ERR("Unknown consumer_data type");
2570 pthread_mutex_unlock(&stream
->lock
);
2575 static void destroy_channel_ht(struct lttng_ht
*ht
)
2577 struct lttng_ht_iter iter
;
2578 struct lttng_consumer_channel
*channel
;
2586 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2587 ret
= lttng_ht_del(ht
, &iter
);
2592 lttng_ht_destroy(ht
);
2596 * This thread polls the channel fds to detect when they are being
2597 * closed. It closes all related streams if the channel is detected as
2598 * closed. It is currently only used as a shim layer for UST because the
2599 * consumerd needs to keep the per-stream wakeup end of pipes open for
2602 void *consumer_thread_channel_poll(void *data
)
2605 uint32_t revents
, nb_fd
;
2606 struct lttng_consumer_channel
*chan
= NULL
;
2607 struct lttng_ht_iter iter
;
2608 struct lttng_ht_node_u64
*node
;
2609 struct lttng_poll_event events
;
2610 struct lttng_consumer_local_data
*ctx
= data
;
2611 struct lttng_ht
*channel_ht
;
2613 rcu_register_thread();
2615 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2617 /* ENOMEM at this point. Better to bail out. */
2621 DBG("Thread channel poll started");
2623 /* Size is set to 1 for the consumer_channel pipe */
2624 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2626 ERR("Poll set creation failed");
2630 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2636 DBG("Channel main loop started");
2639 /* Only the channel pipe is set */
2640 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2645 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2646 ret
= lttng_poll_wait(&events
, -1);
2647 DBG("Channel event catched in thread");
2649 if (errno
== EINTR
) {
2650 ERR("Poll EINTR catched");
2658 /* From here, the event is a channel wait fd */
2659 for (i
= 0; i
< nb_fd
; i
++) {
2660 revents
= LTTNG_POLL_GETEV(&events
, i
);
2661 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2663 /* Just don't waste time if no returned events for the fd */
2667 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2668 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2669 DBG("Channel thread pipe hung up");
2671 * Remove the pipe from the poll set and continue the loop
2672 * since their might be data to consume.
2674 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2676 } else if (revents
& LPOLLIN
) {
2677 enum consumer_channel_action action
;
2680 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2682 ERR("Error reading channel pipe");
2687 case CONSUMER_CHANNEL_ADD
:
2688 DBG("Adding channel %d to poll set",
2691 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2694 lttng_ht_add_unique_u64(channel_ht
,
2695 &chan
->wait_fd_node
);
2697 /* Add channel to the global poll events list */
2698 lttng_poll_add(&events
, chan
->wait_fd
,
2699 LPOLLIN
| LPOLLPRI
);
2701 case CONSUMER_CHANNEL_DEL
:
2703 struct lttng_consumer_stream
*stream
, *stmp
;
2706 chan
= consumer_find_channel(key
);
2709 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2712 lttng_poll_del(&events
, chan
->wait_fd
);
2713 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2714 ret
= lttng_ht_del(channel_ht
, &iter
);
2716 consumer_close_channel_streams(chan
);
2718 switch (consumer_data
.type
) {
2719 case LTTNG_CONSUMER_KERNEL
:
2721 case LTTNG_CONSUMER32_UST
:
2722 case LTTNG_CONSUMER64_UST
:
2723 /* Delete streams that might have been left in the stream list. */
2724 cds_list_for_each_entry_safe(stream
, stmp
, &chan
->streams
.head
,
2726 cds_list_del(&stream
->send_node
);
2727 lttng_ustconsumer_del_stream(stream
);
2728 uatomic_sub(&stream
->chan
->refcount
, 1);
2729 assert(&chan
->refcount
);
2734 ERR("Unknown consumer_data type");
2739 * Release our own refcount. Force channel deletion even if
2740 * streams were not initialized.
2742 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2743 consumer_del_channel(chan
);
2748 case CONSUMER_CHANNEL_QUIT
:
2750 * Remove the pipe from the poll set and continue the loop
2751 * since their might be data to consume.
2753 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2756 ERR("Unknown action");
2761 /* Handle other stream */
2767 uint64_t tmp_id
= (uint64_t) pollfd
;
2769 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2771 node
= lttng_ht_iter_get_node_u64(&iter
);
2774 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2777 /* Check for error event */
2778 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2779 DBG("Channel fd %d is hup|err.", pollfd
);
2781 lttng_poll_del(&events
, chan
->wait_fd
);
2782 ret
= lttng_ht_del(channel_ht
, &iter
);
2784 consumer_close_channel_streams(chan
);
2786 /* Release our own refcount */
2787 if (!uatomic_sub_return(&chan
->refcount
, 1)
2788 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2789 consumer_del_channel(chan
);
2793 /* Release RCU lock for the channel looked up */
2799 lttng_poll_clean(&events
);
2801 destroy_channel_ht(channel_ht
);
2803 DBG("Channel poll thread exiting");
2804 rcu_unregister_thread();
2808 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2809 struct pollfd
*sockpoll
, int client_socket
)
2816 if (lttng_consumer_poll_socket(sockpoll
) < 0) {
2820 DBG("Metadata connection on client_socket");
2822 /* Blocking call, waiting for transmission */
2823 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2824 if (ctx
->consumer_metadata_socket
< 0) {
2825 WARN("On accept metadata");
2836 * This thread listens on the consumerd socket and receives the file
2837 * descriptors from the session daemon.
2839 void *consumer_thread_sessiond_poll(void *data
)
2841 int sock
= -1, client_socket
, ret
;
2843 * structure to poll for incoming data on communication socket avoids
2844 * making blocking sockets.
2846 struct pollfd consumer_sockpoll
[2];
2847 struct lttng_consumer_local_data
*ctx
= data
;
2849 rcu_register_thread();
2851 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2852 unlink(ctx
->consumer_command_sock_path
);
2853 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2854 if (client_socket
< 0) {
2855 ERR("Cannot create command socket");
2859 ret
= lttcomm_listen_unix_sock(client_socket
);
2864 DBG("Sending ready command to lttng-sessiond");
2865 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2866 /* return < 0 on error, but == 0 is not fatal */
2868 ERR("Error sending ready command to lttng-sessiond");
2872 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2873 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2874 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2875 consumer_sockpoll
[1].fd
= client_socket
;
2876 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2878 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2881 DBG("Connection on client_socket");
2883 /* Blocking call, waiting for transmission */
2884 sock
= lttcomm_accept_unix_sock(client_socket
);
2891 * Setup metadata socket which is the second socket connection on the
2892 * command unix socket.
2894 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
2899 /* This socket is not useful anymore. */
2900 ret
= close(client_socket
);
2902 PERROR("close client_socket");
2906 /* update the polling structure to poll on the established socket */
2907 consumer_sockpoll
[1].fd
= sock
;
2908 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2911 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2914 DBG("Incoming command on sock");
2915 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2916 if (ret
== -ENOENT
) {
2917 DBG("Received STOP command");
2922 * This could simply be a session daemon quitting. Don't output
2925 DBG("Communication interrupted on command socket");
2928 if (consumer_quit
) {
2929 DBG("consumer_thread_receive_fds received quit from signal");
2932 DBG("received command on sock");
2935 DBG("Consumer thread sessiond poll exiting");
2938 * Close metadata streams since the producer is the session daemon which
2941 * NOTE: for now, this only applies to the UST tracer.
2943 lttng_consumer_close_metadata();
2946 * when all fds have hung up, the polling thread
2952 * Notify the data poll thread to poll back again and test the
2953 * consumer_quit state that we just set so to quit gracefully.
2955 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
2957 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
2959 /* Cleaning up possibly open sockets. */
2963 PERROR("close sock sessiond poll");
2966 if (client_socket
>= 0) {
2967 ret
= close(client_socket
);
2969 PERROR("close client_socket sessiond poll");
2973 rcu_unregister_thread();
2977 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2978 struct lttng_consumer_local_data
*ctx
)
2982 pthread_mutex_lock(&stream
->lock
);
2984 switch (consumer_data
.type
) {
2985 case LTTNG_CONSUMER_KERNEL
:
2986 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
2988 case LTTNG_CONSUMER32_UST
:
2989 case LTTNG_CONSUMER64_UST
:
2990 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2993 ERR("Unknown consumer_data type");
2999 pthread_mutex_unlock(&stream
->lock
);
3003 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3005 switch (consumer_data
.type
) {
3006 case LTTNG_CONSUMER_KERNEL
:
3007 return lttng_kconsumer_on_recv_stream(stream
);
3008 case LTTNG_CONSUMER32_UST
:
3009 case LTTNG_CONSUMER64_UST
:
3010 return lttng_ustconsumer_on_recv_stream(stream
);
3012 ERR("Unknown consumer_data type");
3019 * Allocate and set consumer data hash tables.
3021 void lttng_consumer_init(void)
3023 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3024 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3025 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3026 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3030 * Process the ADD_RELAYD command receive by a consumer.
3032 * This will create a relayd socket pair and add it to the relayd hash table.
3033 * The caller MUST acquire a RCU read side lock before calling it.
3035 int consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3036 struct lttng_consumer_local_data
*ctx
, int sock
,
3037 struct pollfd
*consumer_sockpoll
,
3038 struct lttcomm_relayd_sock
*relayd_sock
, unsigned int sessiond_id
)
3040 int fd
= -1, ret
= -1, relayd_created
= 0;
3041 enum lttng_error_code ret_code
= LTTNG_OK
;
3042 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3045 assert(relayd_sock
);
3047 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3049 /* Get relayd reference if exists. */
3050 relayd
= consumer_find_relayd(net_seq_idx
);
3051 if (relayd
== NULL
) {
3052 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3053 /* Not found. Allocate one. */
3054 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3055 if (relayd
== NULL
) {
3057 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3060 relayd
->sessiond_session_id
= (uint64_t) sessiond_id
;
3065 * This code path MUST continue to the consumer send status message to
3066 * we can notify the session daemon and continue our work without
3067 * killing everything.
3071 * relayd key should never be found for control socket.
3073 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3076 /* First send a status message before receiving the fds. */
3077 ret
= consumer_send_status_msg(sock
, LTTNG_OK
);
3079 /* Somehow, the session daemon is not responding anymore. */
3080 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3081 goto error_nosignal
;
3084 /* Poll on consumer socket. */
3085 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
3086 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3088 goto error_nosignal
;
3091 /* Get relayd socket from session daemon */
3092 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3093 if (ret
!= sizeof(fd
)) {
3095 fd
= -1; /* Just in case it gets set with an invalid value. */
3098 * Failing to receive FDs might indicate a major problem such as
3099 * reaching a fd limit during the receive where the kernel returns a
3100 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3101 * don't take any chances and stop everything.
3103 * XXX: Feature request #558 will fix that and avoid this possible
3104 * issue when reaching the fd limit.
3106 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3107 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3111 /* Copy socket information and received FD */
3112 switch (sock_type
) {
3113 case LTTNG_STREAM_CONTROL
:
3114 /* Copy received lttcomm socket */
3115 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3116 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3117 /* Handle create_sock error. */
3119 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3123 * Close the socket created internally by
3124 * lttcomm_create_sock, so we can replace it by the one
3125 * received from sessiond.
3127 if (close(relayd
->control_sock
.sock
.fd
)) {
3131 /* Assign new file descriptor */
3132 relayd
->control_sock
.sock
.fd
= fd
;
3133 fd
= -1; /* For error path */
3134 /* Assign version values. */
3135 relayd
->control_sock
.major
= relayd_sock
->major
;
3136 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3139 * Create a session on the relayd and store the returned id. Lock the
3140 * control socket mutex if the relayd was NOT created before.
3142 if (!relayd_created
) {
3143 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3145 ret
= relayd_create_session(&relayd
->control_sock
,
3146 &relayd
->relayd_session_id
);
3147 if (!relayd_created
) {
3148 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3152 * Close all sockets of a relayd object. It will be freed if it was
3153 * created at the error code path or else it will be garbage
3156 (void) relayd_close(&relayd
->control_sock
);
3157 (void) relayd_close(&relayd
->data_sock
);
3158 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
3163 case LTTNG_STREAM_DATA
:
3164 /* Copy received lttcomm socket */
3165 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3166 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3167 /* Handle create_sock error. */
3169 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3173 * Close the socket created internally by
3174 * lttcomm_create_sock, so we can replace it by the one
3175 * received from sessiond.
3177 if (close(relayd
->data_sock
.sock
.fd
)) {
3181 /* Assign new file descriptor */
3182 relayd
->data_sock
.sock
.fd
= fd
;
3183 fd
= -1; /* for eventual error paths */
3184 /* Assign version values. */
3185 relayd
->data_sock
.major
= relayd_sock
->major
;
3186 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3189 ERR("Unknown relayd socket type (%d)", sock_type
);
3191 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3195 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3196 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3197 relayd
->net_seq_idx
, fd
);
3199 /* We successfully added the socket. Send status back. */
3200 ret
= consumer_send_status_msg(sock
, ret_code
);
3202 /* Somehow, the session daemon is not responding anymore. */
3203 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3204 goto error_nosignal
;
3208 * Add relayd socket pair to consumer data hashtable. If object already
3209 * exists or on error, the function gracefully returns.
3217 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3218 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3222 /* Close received socket if valid. */
3225 PERROR("close received socket");
3229 if (relayd_created
) {
3237 * Try to lock the stream mutex.
3239 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3241 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3248 * Try to lock the stream mutex. On failure, we know that the stream is
3249 * being used else where hence there is data still being extracted.
3251 ret
= pthread_mutex_trylock(&stream
->lock
);
3253 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3265 * Search for a relayd associated to the session id and return the reference.
3267 * A rcu read side lock MUST be acquire before calling this function and locked
3268 * until the relayd object is no longer necessary.
3270 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3272 struct lttng_ht_iter iter
;
3273 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3275 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3276 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3279 * Check by sessiond id which is unique here where the relayd session
3280 * id might not be when having multiple relayd.
3282 if (relayd
->sessiond_session_id
== id
) {
3283 /* Found the relayd. There can be only one per id. */
3295 * Check if for a given session id there is still data needed to be extract
3298 * Return 1 if data is pending or else 0 meaning ready to be read.
3300 int consumer_data_pending(uint64_t id
)
3303 struct lttng_ht_iter iter
;
3304 struct lttng_ht
*ht
;
3305 struct lttng_consumer_stream
*stream
;
3306 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3307 int (*data_pending
)(struct lttng_consumer_stream
*);
3309 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3312 pthread_mutex_lock(&consumer_data
.lock
);
3314 switch (consumer_data
.type
) {
3315 case LTTNG_CONSUMER_KERNEL
:
3316 data_pending
= lttng_kconsumer_data_pending
;
3318 case LTTNG_CONSUMER32_UST
:
3319 case LTTNG_CONSUMER64_UST
:
3320 data_pending
= lttng_ustconsumer_data_pending
;
3323 ERR("Unknown consumer data type");
3327 /* Ease our life a bit */
3328 ht
= consumer_data
.stream_list_ht
;
3330 relayd
= find_relayd_by_session_id(id
);
3332 /* Send init command for data pending. */
3333 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3334 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3335 relayd
->relayd_session_id
);
3336 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3338 /* Communication error thus the relayd so no data pending. */
3339 goto data_not_pending
;
3343 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3344 ht
->hash_fct(&id
, lttng_ht_seed
),
3346 &iter
.iter
, stream
, node_session_id
.node
) {
3347 /* If this call fails, the stream is being used hence data pending. */
3348 ret
= stream_try_lock(stream
);
3354 * A removed node from the hash table indicates that the stream has
3355 * been deleted thus having a guarantee that the buffers are closed
3356 * on the consumer side. However, data can still be transmitted
3357 * over the network so don't skip the relayd check.
3359 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3361 /* Check the stream if there is data in the buffers. */
3362 ret
= data_pending(stream
);
3364 pthread_mutex_unlock(&stream
->lock
);
3371 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3372 if (stream
->metadata_flag
) {
3373 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3374 stream
->relayd_stream_id
);
3376 ret
= relayd_data_pending(&relayd
->control_sock
,
3377 stream
->relayd_stream_id
,
3378 stream
->next_net_seq_num
- 1);
3380 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3382 pthread_mutex_unlock(&stream
->lock
);
3386 pthread_mutex_unlock(&stream
->lock
);
3390 unsigned int is_data_inflight
= 0;
3392 /* Send init command for data pending. */
3393 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3394 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3395 relayd
->relayd_session_id
, &is_data_inflight
);
3396 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3398 goto data_not_pending
;
3400 if (is_data_inflight
) {
3406 * Finding _no_ node in the hash table and no inflight data means that the
3407 * stream(s) have been removed thus data is guaranteed to be available for
3408 * analysis from the trace files.
3412 /* Data is available to be read by a viewer. */
3413 pthread_mutex_unlock(&consumer_data
.lock
);
3418 /* Data is still being extracted from buffers. */
3419 pthread_mutex_unlock(&consumer_data
.lock
);
3425 * Send a ret code status message to the sessiond daemon.
3427 * Return the sendmsg() return value.
3429 int consumer_send_status_msg(int sock
, int ret_code
)
3431 struct lttcomm_consumer_status_msg msg
;
3433 msg
.ret_code
= ret_code
;
3435 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3439 * Send a channel status message to the sessiond daemon.
3441 * Return the sendmsg() return value.
3443 int consumer_send_status_channel(int sock
,
3444 struct lttng_consumer_channel
*channel
)
3446 struct lttcomm_consumer_status_channel msg
;
3451 msg
.ret_code
= -LTTNG_ERR_UST_CHAN_FAIL
;
3453 msg
.ret_code
= LTTNG_OK
;
3454 msg
.key
= channel
->key
;
3455 msg
.stream_count
= channel
->streams
.count
;
3458 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));