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
)
471 struct lttng_consumer_stream
*stream
;
473 stream
= zmalloc(sizeof(*stream
));
474 if (stream
== NULL
) {
475 PERROR("malloc struct lttng_consumer_stream");
482 stream
->key
= stream_key
;
484 stream
->out_fd_offset
= 0;
485 stream
->state
= state
;
488 stream
->net_seq_idx
= relayd_id
;
489 stream
->session_id
= session_id
;
490 pthread_mutex_init(&stream
->lock
, NULL
);
492 /* If channel is the metadata, flag this stream as metadata. */
493 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
494 stream
->metadata_flag
= 1;
495 /* Metadata is flat out. */
496 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
498 /* Format stream name to <channel_name>_<cpu_number> */
499 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
502 PERROR("snprintf stream name");
507 /* Key is always the wait_fd for streams. */
508 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
510 /* Init node per channel id key */
511 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
513 /* Init session id node with the stream session id */
514 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
516 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
517 " relayd_id %" PRIu64
", session_id %" PRIu64
,
518 stream
->name
, stream
->key
, channel_key
,
519 stream
->net_seq_idx
, stream
->session_id
);
535 * Add a stream to the global list protected by a mutex.
537 static int add_stream(struct lttng_consumer_stream
*stream
,
541 struct consumer_relayd_sock_pair
*relayd
;
546 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
548 pthread_mutex_lock(&consumer_data
.lock
);
549 pthread_mutex_lock(&stream
->lock
);
552 /* Steal stream identifier to avoid having streams with the same key */
553 steal_stream_key(stream
->key
, ht
);
555 lttng_ht_add_unique_u64(ht
, &stream
->node
);
557 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
558 &stream
->node_channel_id
);
561 * Add stream to the stream_list_ht of the consumer data. No need to steal
562 * the key since the HT does not use it and we allow to add redundant keys
565 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
567 /* Check and cleanup relayd */
568 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
569 if (relayd
!= NULL
) {
570 uatomic_inc(&relayd
->refcount
);
574 * When nb_init_stream_left reaches 0, we don't need to trigger any action
575 * in terms of destroying the associated channel, because the action that
576 * causes the count to become 0 also causes a stream to be added. The
577 * channel deletion will thus be triggered by the following removal of this
580 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
581 /* Increment refcount before decrementing nb_init_stream_left */
583 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
586 /* Update consumer data once the node is inserted. */
587 consumer_data
.stream_count
++;
588 consumer_data
.need_update
= 1;
591 pthread_mutex_unlock(&stream
->lock
);
592 pthread_mutex_unlock(&consumer_data
.lock
);
598 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
599 * be acquired before calling this.
601 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
604 struct lttng_ht_node_u64
*node
;
605 struct lttng_ht_iter iter
;
609 lttng_ht_lookup(consumer_data
.relayd_ht
,
610 &relayd
->net_seq_idx
, &iter
);
611 node
= lttng_ht_iter_get_node_u64(&iter
);
615 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
622 * Allocate and return a consumer relayd socket.
624 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
625 uint64_t net_seq_idx
)
627 struct consumer_relayd_sock_pair
*obj
= NULL
;
629 /* net sequence index of -1 is a failure */
630 if (net_seq_idx
== (uint64_t) -1ULL) {
634 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
636 PERROR("zmalloc relayd sock");
640 obj
->net_seq_idx
= net_seq_idx
;
642 obj
->destroy_flag
= 0;
643 obj
->control_sock
.sock
.fd
= -1;
644 obj
->data_sock
.sock
.fd
= -1;
645 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
646 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
653 * Find a relayd socket pair in the global consumer data.
655 * Return the object if found else NULL.
656 * RCU read-side lock must be held across this call and while using the
659 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
661 struct lttng_ht_iter iter
;
662 struct lttng_ht_node_u64
*node
;
663 struct consumer_relayd_sock_pair
*relayd
= NULL
;
665 /* Negative keys are lookup failures */
666 if (key
== (uint64_t) -1ULL) {
670 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
672 node
= lttng_ht_iter_get_node_u64(&iter
);
674 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
682 * Find a relayd and send the stream
684 * Returns 0 on success, < 0 on error
686 int consumer_send_relayd_stream(struct lttng_consumer_stream
*stream
,
690 struct consumer_relayd_sock_pair
*relayd
;
693 assert(stream
->net_seq_idx
!= -1ULL);
696 /* The stream is not metadata. Get relayd reference if exists. */
698 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
699 if (relayd
!= NULL
) {
700 /* Add stream on the relayd */
701 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
702 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
703 path
, &stream
->relayd_stream_id
,
704 stream
->chan
->tracefile_size
, stream
->chan
->tracefile_count
);
705 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
709 uatomic_inc(&relayd
->refcount
);
711 ERR("Stream %" PRIu64
" relayd ID %" PRIu64
" unknown. Can't send it.",
712 stream
->key
, stream
->net_seq_idx
);
717 DBG("Stream %s with key %" PRIu64
" sent to relayd id %" PRIu64
,
718 stream
->name
, stream
->key
, stream
->net_seq_idx
);
726 * Find a relayd and close the stream
728 void close_relayd_stream(struct lttng_consumer_stream
*stream
)
730 struct consumer_relayd_sock_pair
*relayd
;
732 /* The stream is not metadata. Get relayd reference if exists. */
734 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
736 consumer_stream_relayd_close(stream
, relayd
);
742 * Handle stream for relayd transmission if the stream applies for network
743 * streaming where the net sequence index is set.
745 * Return destination file descriptor or negative value on error.
747 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
748 size_t data_size
, unsigned long padding
,
749 struct consumer_relayd_sock_pair
*relayd
)
752 struct lttcomm_relayd_data_hdr data_hdr
;
758 /* Reset data header */
759 memset(&data_hdr
, 0, sizeof(data_hdr
));
761 if (stream
->metadata_flag
) {
762 /* Caller MUST acquire the relayd control socket lock */
763 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
768 /* Metadata are always sent on the control socket. */
769 outfd
= relayd
->control_sock
.sock
.fd
;
771 /* Set header with stream information */
772 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
773 data_hdr
.data_size
= htobe32(data_size
);
774 data_hdr
.padding_size
= htobe32(padding
);
776 * Note that net_seq_num below is assigned with the *current* value of
777 * next_net_seq_num and only after that the next_net_seq_num will be
778 * increment. This is why when issuing a command on the relayd using
779 * this next value, 1 should always be substracted in order to compare
780 * the last seen sequence number on the relayd side to the last sent.
782 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
783 /* Other fields are zeroed previously */
785 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
791 ++stream
->next_net_seq_num
;
793 /* Set to go on data socket */
794 outfd
= relayd
->data_sock
.sock
.fd
;
802 * Allocate and return a new lttng_consumer_channel object using the given key
803 * to initialize the hash table node.
805 * On error, return NULL.
807 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
809 const char *pathname
,
814 enum lttng_event_output output
,
815 uint64_t tracefile_size
,
816 uint64_t tracefile_count
,
817 uint64_t session_id_per_pid
,
818 unsigned int monitor
)
820 struct lttng_consumer_channel
*channel
;
822 channel
= zmalloc(sizeof(*channel
));
823 if (channel
== NULL
) {
824 PERROR("malloc struct lttng_consumer_channel");
829 channel
->refcount
= 0;
830 channel
->session_id
= session_id
;
831 channel
->session_id_per_pid
= session_id_per_pid
;
834 channel
->relayd_id
= relayd_id
;
835 channel
->output
= output
;
836 channel
->tracefile_size
= tracefile_size
;
837 channel
->tracefile_count
= tracefile_count
;
838 channel
->monitor
= monitor
;
841 * In monitor mode, the streams associated with the channel will be put in
842 * a special list ONLY owned by this channel. So, the refcount is set to 1
843 * here meaning that the channel itself has streams that are referenced.
845 * On a channel deletion, once the channel is no longer visible, the
846 * refcount is decremented and checked for a zero value to delete it. With
847 * streams in no monitor mode, it will now be safe to destroy the channel.
849 if (!channel
->monitor
) {
850 channel
->refcount
= 1;
853 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
854 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
856 strncpy(channel
->name
, name
, sizeof(channel
->name
));
857 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
859 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
861 channel
->wait_fd
= -1;
863 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
865 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
872 * Add a channel to the global list protected by a mutex.
874 * On success 0 is returned else a negative value.
876 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
877 struct lttng_consumer_local_data
*ctx
)
880 struct lttng_ht_node_u64
*node
;
881 struct lttng_ht_iter iter
;
883 pthread_mutex_lock(&consumer_data
.lock
);
886 lttng_ht_lookup(consumer_data
.channel_ht
, &channel
->key
, &iter
);
887 node
= lttng_ht_iter_get_node_u64(&iter
);
889 /* Channel already exist. Ignore the insertion */
890 ERR("Consumer add channel key %" PRIu64
" already exists!",
896 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
900 pthread_mutex_unlock(&consumer_data
.lock
);
902 if (!ret
&& channel
->wait_fd
!= -1 &&
903 channel
->type
== CONSUMER_CHANNEL_TYPE_DATA
) {
904 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
910 * Allocate the pollfd structure and the local view of the out fds to avoid
911 * doing a lookup in the linked list and concurrency issues when writing is
912 * needed. Called with consumer_data.lock held.
914 * Returns the number of fds in the structures.
916 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
917 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
921 struct lttng_ht_iter iter
;
922 struct lttng_consumer_stream
*stream
;
927 assert(local_stream
);
929 DBG("Updating poll fd array");
931 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
933 * Only active streams with an active end point can be added to the
934 * poll set and local stream storage of the thread.
936 * There is a potential race here for endpoint_status to be updated
937 * just after the check. However, this is OK since the stream(s) will
938 * be deleted once the thread is notified that the end point state has
939 * changed where this function will be called back again.
941 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
942 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
946 * This clobbers way too much the debug output. Uncomment that if you
947 * need it for debugging purposes.
949 * DBG("Active FD %d", stream->wait_fd);
951 (*pollfd
)[i
].fd
= stream
->wait_fd
;
952 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
953 local_stream
[i
] = stream
;
959 * Insert the consumer_data_pipe at the end of the array and don't
960 * increment i so nb_fd is the number of real FD.
962 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
963 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
968 * Poll on the should_quit pipe and the command socket return -1 on error and
969 * should exit, 0 if data is available on the command socket
971 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
976 num_rdy
= poll(consumer_sockpoll
, 2, -1);
979 * Restart interrupted system call.
981 if (errno
== EINTR
) {
984 PERROR("Poll error");
987 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
988 DBG("consumer_should_quit wake up");
998 * Set the error socket.
1000 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1003 ctx
->consumer_error_socket
= sock
;
1007 * Set the command socket path.
1009 void lttng_consumer_set_command_sock_path(
1010 struct lttng_consumer_local_data
*ctx
, char *sock
)
1012 ctx
->consumer_command_sock_path
= sock
;
1016 * Send return code to the session daemon.
1017 * If the socket is not defined, we return 0, it is not a fatal error
1019 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1021 if (ctx
->consumer_error_socket
> 0) {
1022 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1023 sizeof(enum lttcomm_sessiond_command
));
1030 * Close all the tracefiles and stream fds and MUST be called when all
1031 * instances are destroyed i.e. when all threads were joined and are ended.
1033 void lttng_consumer_cleanup(void)
1035 struct lttng_ht_iter iter
;
1036 struct lttng_consumer_channel
*channel
;
1040 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1042 consumer_del_channel(channel
);
1047 lttng_ht_destroy(consumer_data
.channel_ht
);
1049 cleanup_relayd_ht();
1051 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1054 * This HT contains streams that are freed by either the metadata thread or
1055 * the data thread so we do *nothing* on the hash table and simply destroy
1058 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1062 * Called from signal handler.
1064 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1069 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1070 } while (ret
< 0 && errno
== EINTR
);
1071 if (ret
< 0 || ret
!= 1) {
1072 PERROR("write consumer quit");
1075 DBG("Consumer flag that it should quit");
1078 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1081 int outfd
= stream
->out_fd
;
1084 * This does a blocking write-and-wait on any page that belongs to the
1085 * subbuffer prior to the one we just wrote.
1086 * Don't care about error values, as these are just hints and ways to
1087 * limit the amount of page cache used.
1089 if (orig_offset
< stream
->max_sb_size
) {
1092 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1093 stream
->max_sb_size
,
1094 SYNC_FILE_RANGE_WAIT_BEFORE
1095 | SYNC_FILE_RANGE_WRITE
1096 | SYNC_FILE_RANGE_WAIT_AFTER
);
1098 * Give hints to the kernel about how we access the file:
1099 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1102 * We need to call fadvise again after the file grows because the
1103 * kernel does not seem to apply fadvise to non-existing parts of the
1106 * Call fadvise _after_ having waited for the page writeback to
1107 * complete because the dirty page writeback semantic is not well
1108 * defined. So it can be expected to lead to lower throughput in
1111 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1112 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1116 * Initialise the necessary environnement :
1117 * - create a new context
1118 * - create the poll_pipe
1119 * - create the should_quit pipe (for signal handler)
1120 * - create the thread pipe (for splice)
1122 * Takes a function pointer as argument, this function is called when data is
1123 * available on a buffer. This function is responsible to do the
1124 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1125 * buffer configuration and then kernctl_put_next_subbuf at the end.
1127 * Returns a pointer to the new context or NULL on error.
1129 struct lttng_consumer_local_data
*lttng_consumer_create(
1130 enum lttng_consumer_type type
,
1131 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1132 struct lttng_consumer_local_data
*ctx
),
1133 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1134 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1135 int (*update_stream
)(int stream_key
, uint32_t state
))
1138 struct lttng_consumer_local_data
*ctx
;
1140 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1141 consumer_data
.type
== type
);
1142 consumer_data
.type
= type
;
1144 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1146 PERROR("allocating context");
1150 ctx
->consumer_error_socket
= -1;
1151 ctx
->consumer_metadata_socket
= -1;
1152 /* assign the callbacks */
1153 ctx
->on_buffer_ready
= buffer_ready
;
1154 ctx
->on_recv_channel
= recv_channel
;
1155 ctx
->on_recv_stream
= recv_stream
;
1156 ctx
->on_update_stream
= update_stream
;
1158 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1159 if (!ctx
->consumer_data_pipe
) {
1160 goto error_poll_pipe
;
1163 ret
= pipe(ctx
->consumer_should_quit
);
1165 PERROR("Error creating recv pipe");
1166 goto error_quit_pipe
;
1169 ret
= pipe(ctx
->consumer_thread_pipe
);
1171 PERROR("Error creating thread pipe");
1172 goto error_thread_pipe
;
1175 ret
= pipe(ctx
->consumer_channel_pipe
);
1177 PERROR("Error creating channel pipe");
1178 goto error_channel_pipe
;
1181 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1182 if (!ctx
->consumer_metadata_pipe
) {
1183 goto error_metadata_pipe
;
1186 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1188 goto error_splice_pipe
;
1194 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1195 error_metadata_pipe
:
1196 utils_close_pipe(ctx
->consumer_channel_pipe
);
1198 utils_close_pipe(ctx
->consumer_thread_pipe
);
1200 utils_close_pipe(ctx
->consumer_should_quit
);
1202 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1210 * Close all fds associated with the instance and free the context.
1212 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1216 DBG("Consumer destroying it. Closing everything.");
1218 ret
= close(ctx
->consumer_error_socket
);
1222 ret
= close(ctx
->consumer_metadata_socket
);
1226 utils_close_pipe(ctx
->consumer_thread_pipe
);
1227 utils_close_pipe(ctx
->consumer_channel_pipe
);
1228 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1229 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1230 utils_close_pipe(ctx
->consumer_should_quit
);
1231 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1233 unlink(ctx
->consumer_command_sock_path
);
1238 * Write the metadata stream id on the specified file descriptor.
1240 static int write_relayd_metadata_id(int fd
,
1241 struct lttng_consumer_stream
*stream
,
1242 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1245 struct lttcomm_relayd_metadata_payload hdr
;
1247 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1248 hdr
.padding_size
= htobe32(padding
);
1250 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1251 } while (ret
< 0 && errno
== EINTR
);
1252 if (ret
< 0 || ret
!= sizeof(hdr
)) {
1254 * This error means that the fd's end is closed so ignore the perror
1255 * not to clubber the error output since this can happen in a normal
1258 if (errno
!= EPIPE
) {
1259 PERROR("write metadata stream id");
1261 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1263 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1264 * handle writting the missing part so report that as an error and
1265 * don't lie to the caller.
1270 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1271 stream
->relayd_stream_id
, padding
);
1278 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1279 * core function for writing trace buffers to either the local filesystem or
1282 * It must be called with the stream lock held.
1284 * Careful review MUST be put if any changes occur!
1286 * Returns the number of bytes written
1288 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1289 struct lttng_consumer_local_data
*ctx
,
1290 struct lttng_consumer_stream
*stream
, unsigned long len
,
1291 unsigned long padding
)
1293 unsigned long mmap_offset
;
1295 ssize_t ret
= 0, written
= 0;
1296 off_t orig_offset
= stream
->out_fd_offset
;
1297 /* Default is on the disk */
1298 int outfd
= stream
->out_fd
;
1299 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1300 unsigned int relayd_hang_up
= 0;
1302 /* RCU lock for the relayd pointer */
1305 /* Flag that the current stream if set for network streaming. */
1306 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1307 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1308 if (relayd
== NULL
) {
1313 /* get the offset inside the fd to mmap */
1314 switch (consumer_data
.type
) {
1315 case LTTNG_CONSUMER_KERNEL
:
1316 mmap_base
= stream
->mmap_base
;
1317 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1319 case LTTNG_CONSUMER32_UST
:
1320 case LTTNG_CONSUMER64_UST
:
1321 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1323 ERR("read mmap get mmap base for stream %s", stream
->name
);
1327 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1331 ERR("Unknown consumer_data type");
1336 PERROR("tracer ctl get_mmap_read_offset");
1341 /* Handle stream on the relayd if the output is on the network */
1343 unsigned long netlen
= len
;
1346 * Lock the control socket for the complete duration of the function
1347 * since from this point on we will use the socket.
1349 if (stream
->metadata_flag
) {
1350 /* Metadata requires the control socket. */
1351 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1352 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1355 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1357 /* Use the returned socket. */
1360 /* Write metadata stream id before payload */
1361 if (stream
->metadata_flag
) {
1362 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1365 /* Socket operation failed. We consider the relayd dead */
1366 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1374 /* Socket operation failed. We consider the relayd dead */
1375 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1379 /* Else, use the default set before which is the filesystem. */
1382 /* No streaming, we have to set the len with the full padding */
1386 * Check if we need to change the tracefile before writing the packet.
1388 if (stream
->chan
->tracefile_size
> 0 &&
1389 (stream
->tracefile_size_current
+ len
) >
1390 stream
->chan
->tracefile_size
) {
1391 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1392 stream
->name
, stream
->chan
->tracefile_size
,
1393 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1394 stream
->out_fd
, &(stream
->tracefile_count_current
));
1396 ERR("Rotating output file");
1399 outfd
= stream
->out_fd
= ret
;
1400 /* Reset current size because we just perform a rotation. */
1401 stream
->tracefile_size_current
= 0;
1403 stream
->tracefile_size_current
+= len
;
1408 ret
= write(outfd
, mmap_base
+ mmap_offset
, len
);
1409 } while (ret
< 0 && errno
== EINTR
);
1410 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1413 * This is possible if the fd is closed on the other side (outfd)
1414 * or any write problem. It can be verbose a bit for a normal
1415 * execution if for instance the relayd is stopped abruptly. This
1416 * can happen so set this to a DBG statement.
1418 DBG("Error in file write mmap");
1422 /* Socket operation failed. We consider the relayd dead */
1423 if (errno
== EPIPE
|| errno
== EINVAL
) {
1428 } else if (ret
> len
) {
1429 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1437 /* This call is useless on a socket so better save a syscall. */
1439 /* This won't block, but will start writeout asynchronously */
1440 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1441 SYNC_FILE_RANGE_WRITE
);
1442 stream
->out_fd_offset
+= ret
;
1446 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1450 * This is a special case that the relayd has closed its socket. Let's
1451 * cleanup the relayd object and all associated streams.
1453 if (relayd
&& relayd_hang_up
) {
1454 cleanup_relayd(relayd
, ctx
);
1458 /* Unlock only if ctrl socket used */
1459 if (relayd
&& stream
->metadata_flag
) {
1460 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1468 * Splice the data from the ring buffer to the tracefile.
1470 * It must be called with the stream lock held.
1472 * Returns the number of bytes spliced.
1474 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1475 struct lttng_consumer_local_data
*ctx
,
1476 struct lttng_consumer_stream
*stream
, unsigned long len
,
1477 unsigned long padding
)
1479 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1481 off_t orig_offset
= stream
->out_fd_offset
;
1482 int fd
= stream
->wait_fd
;
1483 /* Default is on the disk */
1484 int outfd
= stream
->out_fd
;
1485 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1487 unsigned int relayd_hang_up
= 0;
1489 switch (consumer_data
.type
) {
1490 case LTTNG_CONSUMER_KERNEL
:
1492 case LTTNG_CONSUMER32_UST
:
1493 case LTTNG_CONSUMER64_UST
:
1494 /* Not supported for user space tracing */
1497 ERR("Unknown consumer_data type");
1501 /* RCU lock for the relayd pointer */
1504 /* Flag that the current stream if set for network streaming. */
1505 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1506 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1507 if (relayd
== NULL
) {
1513 * Choose right pipe for splice. Metadata and trace data are handled by
1514 * different threads hence the use of two pipes in order not to race or
1515 * corrupt the written data.
1517 if (stream
->metadata_flag
) {
1518 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1520 splice_pipe
= ctx
->consumer_thread_pipe
;
1523 /* Write metadata stream id before payload */
1525 int total_len
= len
;
1527 if (stream
->metadata_flag
) {
1529 * Lock the control socket for the complete duration of the function
1530 * since from this point on we will use the socket.
1532 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1534 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1538 /* Socket operation failed. We consider the relayd dead */
1539 if (ret
== -EBADF
) {
1540 WARN("Remote relayd disconnected. Stopping");
1547 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1550 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1552 /* Use the returned socket. */
1555 /* Socket operation failed. We consider the relayd dead */
1556 if (ret
== -EBADF
) {
1557 WARN("Remote relayd disconnected. Stopping");
1564 /* No streaming, we have to set the len with the full padding */
1568 * Check if we need to change the tracefile before writing the packet.
1570 if (stream
->chan
->tracefile_size
> 0 &&
1571 (stream
->tracefile_size_current
+ len
) >
1572 stream
->chan
->tracefile_size
) {
1573 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1574 stream
->name
, stream
->chan
->tracefile_size
,
1575 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1576 stream
->out_fd
, &(stream
->tracefile_count_current
));
1578 ERR("Rotating output file");
1581 outfd
= stream
->out_fd
= ret
;
1582 /* Reset current size because we just perform a rotation. */
1583 stream
->tracefile_size_current
= 0;
1585 stream
->tracefile_size_current
+= len
;
1589 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1590 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1591 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1592 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1593 DBG("splice chan to pipe, ret %zd", ret_splice
);
1594 if (ret_splice
< 0) {
1595 PERROR("Error in relay splice");
1597 written
= ret_splice
;
1603 /* Handle stream on the relayd if the output is on the network */
1605 if (stream
->metadata_flag
) {
1606 size_t metadata_payload_size
=
1607 sizeof(struct lttcomm_relayd_metadata_payload
);
1609 /* Update counter to fit the spliced data */
1610 ret_splice
+= metadata_payload_size
;
1611 len
+= metadata_payload_size
;
1613 * We do this so the return value can match the len passed as
1614 * argument to this function.
1616 written
-= metadata_payload_size
;
1620 /* Splice data out */
1621 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1622 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1623 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1624 if (ret_splice
< 0) {
1625 PERROR("Error in file splice");
1627 written
= ret_splice
;
1629 /* Socket operation failed. We consider the relayd dead */
1630 if (errno
== EBADF
|| errno
== EPIPE
) {
1631 WARN("Remote relayd disconnected. Stopping");
1637 } else if (ret_splice
> len
) {
1639 PERROR("Wrote more data than requested %zd (len: %lu)",
1641 written
+= ret_splice
;
1647 /* This call is useless on a socket so better save a syscall. */
1649 /* This won't block, but will start writeout asynchronously */
1650 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1651 SYNC_FILE_RANGE_WRITE
);
1652 stream
->out_fd_offset
+= ret_splice
;
1654 written
+= ret_splice
;
1656 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1664 * This is a special case that the relayd has closed its socket. Let's
1665 * cleanup the relayd object and all associated streams.
1667 if (relayd
&& relayd_hang_up
) {
1668 cleanup_relayd(relayd
, ctx
);
1669 /* Skip splice error so the consumer does not fail */
1674 /* send the appropriate error description to sessiond */
1677 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1680 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1683 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1688 if (relayd
&& stream
->metadata_flag
) {
1689 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1697 * Take a snapshot for a specific fd
1699 * Returns 0 on success, < 0 on error
1701 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1703 switch (consumer_data
.type
) {
1704 case LTTNG_CONSUMER_KERNEL
:
1705 return lttng_kconsumer_take_snapshot(stream
);
1706 case LTTNG_CONSUMER32_UST
:
1707 case LTTNG_CONSUMER64_UST
:
1708 return lttng_ustconsumer_take_snapshot(stream
);
1710 ERR("Unknown consumer_data type");
1717 * Get the produced position
1719 * Returns 0 on success, < 0 on error
1721 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1724 switch (consumer_data
.type
) {
1725 case LTTNG_CONSUMER_KERNEL
:
1726 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1727 case LTTNG_CONSUMER32_UST
:
1728 case LTTNG_CONSUMER64_UST
:
1729 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1731 ERR("Unknown consumer_data type");
1737 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1738 int sock
, struct pollfd
*consumer_sockpoll
)
1740 switch (consumer_data
.type
) {
1741 case LTTNG_CONSUMER_KERNEL
:
1742 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1743 case LTTNG_CONSUMER32_UST
:
1744 case LTTNG_CONSUMER64_UST
:
1745 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1747 ERR("Unknown consumer_data type");
1754 * Iterate over all streams of the hashtable and free them properly.
1756 * WARNING: *MUST* be used with data stream only.
1758 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1760 struct lttng_ht_iter iter
;
1761 struct lttng_consumer_stream
*stream
;
1768 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1770 * Ignore return value since we are currently cleaning up so any error
1773 (void) consumer_del_stream(stream
, ht
);
1777 lttng_ht_destroy(ht
);
1781 * Iterate over all streams of the hashtable and free them properly.
1783 * XXX: Should not be only for metadata stream or else use an other name.
1785 static void destroy_stream_ht(struct lttng_ht
*ht
)
1787 struct lttng_ht_iter iter
;
1788 struct lttng_consumer_stream
*stream
;
1795 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1797 * Ignore return value since we are currently cleaning up so any error
1800 (void) consumer_del_metadata_stream(stream
, ht
);
1804 lttng_ht_destroy(ht
);
1807 void lttng_consumer_close_metadata(void)
1809 switch (consumer_data
.type
) {
1810 case LTTNG_CONSUMER_KERNEL
:
1812 * The Kernel consumer has a different metadata scheme so we don't
1813 * close anything because the stream will be closed by the session
1817 case LTTNG_CONSUMER32_UST
:
1818 case LTTNG_CONSUMER64_UST
:
1820 * Close all metadata streams. The metadata hash table is passed and
1821 * this call iterates over it by closing all wakeup fd. This is safe
1822 * because at this point we are sure that the metadata producer is
1823 * either dead or blocked.
1825 lttng_ustconsumer_close_metadata(metadata_ht
);
1828 ERR("Unknown consumer_data type");
1834 * Clean up a metadata stream and free its memory.
1836 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1837 struct lttng_ht
*ht
)
1840 struct lttng_ht_iter iter
;
1841 struct lttng_consumer_channel
*free_chan
= NULL
;
1842 struct consumer_relayd_sock_pair
*relayd
;
1846 * This call should NEVER receive regular stream. It must always be
1847 * metadata stream and this is crucial for data structure synchronization.
1849 assert(stream
->metadata_flag
);
1851 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1854 /* Means the stream was allocated but not successfully added */
1855 goto free_stream_rcu
;
1858 pthread_mutex_lock(&consumer_data
.lock
);
1859 pthread_mutex_lock(&stream
->lock
);
1861 switch (consumer_data
.type
) {
1862 case LTTNG_CONSUMER_KERNEL
:
1863 if (stream
->mmap_base
!= NULL
) {
1864 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1866 PERROR("munmap metadata stream");
1869 if (stream
->wait_fd
>= 0) {
1870 ret
= close(stream
->wait_fd
);
1872 PERROR("close kernel metadata wait_fd");
1876 case LTTNG_CONSUMER32_UST
:
1877 case LTTNG_CONSUMER64_UST
:
1878 lttng_ustconsumer_del_stream(stream
);
1881 ERR("Unknown consumer_data type");
1887 iter
.iter
.node
= &stream
->node
.node
;
1888 ret
= lttng_ht_del(ht
, &iter
);
1891 iter
.iter
.node
= &stream
->node_channel_id
.node
;
1892 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
1895 iter
.iter
.node
= &stream
->node_session_id
.node
;
1896 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1900 if (stream
->out_fd
>= 0) {
1901 ret
= close(stream
->out_fd
);
1907 /* Check and cleanup relayd */
1909 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1910 if (relayd
!= NULL
) {
1911 uatomic_dec(&relayd
->refcount
);
1912 assert(uatomic_read(&relayd
->refcount
) >= 0);
1914 /* Closing streams requires to lock the control socket. */
1915 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1916 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1917 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1918 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1920 DBG("Unable to close stream on the relayd. Continuing");
1922 * Continue here. There is nothing we can do for the relayd.
1923 * Chances are that the relayd has closed the socket so we just
1924 * continue cleaning up.
1928 /* Both conditions are met, we destroy the relayd. */
1929 if (uatomic_read(&relayd
->refcount
) == 0 &&
1930 uatomic_read(&relayd
->destroy_flag
)) {
1931 consumer_destroy_relayd(relayd
);
1936 /* Atomically decrement channel refcount since other threads can use it. */
1937 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
1938 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
1939 /* Go for channel deletion! */
1940 free_chan
= stream
->chan
;
1945 * Nullify the stream reference so it is not used after deletion. The
1946 * consumer data lock MUST be acquired before being able to check for a
1947 * NULL pointer value.
1949 stream
->chan
->metadata_stream
= NULL
;
1951 pthread_mutex_unlock(&stream
->lock
);
1952 pthread_mutex_unlock(&consumer_data
.lock
);
1955 consumer_del_channel(free_chan
);
1959 call_rcu(&stream
->node
.head
, free_stream_rcu
);
1963 * Action done with the metadata stream when adding it to the consumer internal
1964 * data structures to handle it.
1966 static int add_metadata_stream(struct lttng_consumer_stream
*stream
,
1967 struct lttng_ht
*ht
)
1970 struct consumer_relayd_sock_pair
*relayd
;
1971 struct lttng_ht_iter iter
;
1972 struct lttng_ht_node_u64
*node
;
1977 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
1979 pthread_mutex_lock(&consumer_data
.lock
);
1980 pthread_mutex_lock(&stream
->lock
);
1983 * From here, refcounts are updated so be _careful_ when returning an error
1990 * Lookup the stream just to make sure it does not exist in our internal
1991 * state. This should NEVER happen.
1993 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
1994 node
= lttng_ht_iter_get_node_u64(&iter
);
1997 /* Find relayd and, if one is found, increment refcount. */
1998 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1999 if (relayd
!= NULL
) {
2000 uatomic_inc(&relayd
->refcount
);
2004 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2005 * in terms of destroying the associated channel, because the action that
2006 * causes the count to become 0 also causes a stream to be added. The
2007 * channel deletion will thus be triggered by the following removal of this
2010 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2011 /* Increment refcount before decrementing nb_init_stream_left */
2013 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2016 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2018 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2019 &stream
->node_channel_id
);
2022 * Add stream to the stream_list_ht of the consumer data. No need to steal
2023 * the key since the HT does not use it and we allow to add redundant keys
2026 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2030 pthread_mutex_unlock(&stream
->lock
);
2031 pthread_mutex_unlock(&consumer_data
.lock
);
2036 * Delete data stream that are flagged for deletion (endpoint_status).
2038 static void validate_endpoint_status_data_stream(void)
2040 struct lttng_ht_iter iter
;
2041 struct lttng_consumer_stream
*stream
;
2043 DBG("Consumer delete flagged data stream");
2046 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2047 /* Validate delete flag of the stream */
2048 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2051 /* Delete it right now */
2052 consumer_del_stream(stream
, data_ht
);
2058 * Delete metadata stream that are flagged for deletion (endpoint_status).
2060 static void validate_endpoint_status_metadata_stream(
2061 struct lttng_poll_event
*pollset
)
2063 struct lttng_ht_iter iter
;
2064 struct lttng_consumer_stream
*stream
;
2066 DBG("Consumer delete flagged metadata stream");
2071 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2072 /* Validate delete flag of the stream */
2073 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2077 * Remove from pollset so the metadata thread can continue without
2078 * blocking on a deleted stream.
2080 lttng_poll_del(pollset
, stream
->wait_fd
);
2082 /* Delete it right now */
2083 consumer_del_metadata_stream(stream
, metadata_ht
);
2089 * Thread polls on metadata file descriptor and write them on disk or on the
2092 void *consumer_thread_metadata_poll(void *data
)
2095 uint32_t revents
, nb_fd
;
2096 struct lttng_consumer_stream
*stream
= NULL
;
2097 struct lttng_ht_iter iter
;
2098 struct lttng_ht_node_u64
*node
;
2099 struct lttng_poll_event events
;
2100 struct lttng_consumer_local_data
*ctx
= data
;
2103 rcu_register_thread();
2105 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2107 /* ENOMEM at this point. Better to bail out. */
2111 DBG("Thread metadata poll started");
2113 /* Size is set to 1 for the consumer_metadata pipe */
2114 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2116 ERR("Poll set creation failed");
2120 ret
= lttng_poll_add(&events
,
2121 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2127 DBG("Metadata main loop started");
2130 /* Only the metadata pipe is set */
2131 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2136 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2137 ret
= lttng_poll_wait(&events
, -1);
2138 DBG("Metadata event catched in thread");
2140 if (errno
== EINTR
) {
2141 ERR("Poll EINTR catched");
2149 /* From here, the event is a metadata wait fd */
2150 for (i
= 0; i
< nb_fd
; i
++) {
2151 revents
= LTTNG_POLL_GETEV(&events
, i
);
2152 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2154 /* Just don't waste time if no returned events for the fd */
2159 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2160 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2161 DBG("Metadata thread pipe hung up");
2163 * Remove the pipe from the poll set and continue the loop
2164 * since their might be data to consume.
2166 lttng_poll_del(&events
,
2167 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2168 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2170 } else if (revents
& LPOLLIN
) {
2173 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2174 &stream
, sizeof(stream
));
2176 ERR("read metadata stream, ret: %ld", pipe_len
);
2178 * Continue here to handle the rest of the streams.
2183 /* A NULL stream means that the state has changed. */
2184 if (stream
== NULL
) {
2185 /* Check for deleted streams. */
2186 validate_endpoint_status_metadata_stream(&events
);
2190 DBG("Adding metadata stream %d to poll set",
2193 ret
= add_metadata_stream(stream
, metadata_ht
);
2195 ERR("Unable to add metadata stream");
2196 /* Stream was not setup properly. Continuing. */
2197 consumer_del_metadata_stream(stream
, NULL
);
2201 /* Add metadata stream to the global poll events list */
2202 lttng_poll_add(&events
, stream
->wait_fd
,
2203 LPOLLIN
| LPOLLPRI
);
2206 /* Handle other stream */
2212 uint64_t tmp_id
= (uint64_t) pollfd
;
2214 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2216 node
= lttng_ht_iter_get_node_u64(&iter
);
2219 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2222 /* Check for error event */
2223 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2224 DBG("Metadata fd %d is hup|err.", pollfd
);
2225 if (!stream
->hangup_flush_done
2226 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2227 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2228 DBG("Attempting to flush and consume the UST buffers");
2229 lttng_ustconsumer_on_stream_hangup(stream
);
2231 /* We just flushed the stream now read it. */
2233 len
= ctx
->on_buffer_ready(stream
, ctx
);
2235 * We don't check the return value here since if we get
2236 * a negative len, it means an error occured thus we
2237 * simply remove it from the poll set and free the
2243 lttng_poll_del(&events
, stream
->wait_fd
);
2245 * This call update the channel states, closes file descriptors
2246 * and securely free the stream.
2248 consumer_del_metadata_stream(stream
, metadata_ht
);
2249 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2250 /* Get the data out of the metadata file descriptor */
2251 DBG("Metadata available on fd %d", pollfd
);
2252 assert(stream
->wait_fd
== pollfd
);
2254 len
= ctx
->on_buffer_ready(stream
, ctx
);
2255 /* It's ok to have an unavailable sub-buffer */
2256 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2257 /* Clean up stream from consumer and free it. */
2258 lttng_poll_del(&events
, stream
->wait_fd
);
2259 consumer_del_metadata_stream(stream
, metadata_ht
);
2260 } else if (len
> 0) {
2261 stream
->data_read
= 1;
2265 /* Release RCU lock for the stream looked up */
2272 DBG("Metadata poll thread exiting");
2274 lttng_poll_clean(&events
);
2276 destroy_stream_ht(metadata_ht
);
2278 rcu_unregister_thread();
2283 * This thread polls the fds in the set to consume the data and write
2284 * it to tracefile if necessary.
2286 void *consumer_thread_data_poll(void *data
)
2288 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2289 struct pollfd
*pollfd
= NULL
;
2290 /* local view of the streams */
2291 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2292 /* local view of consumer_data.fds_count */
2294 struct lttng_consumer_local_data
*ctx
= data
;
2297 rcu_register_thread();
2299 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2300 if (data_ht
== NULL
) {
2301 /* ENOMEM at this point. Better to bail out. */
2305 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
2312 * the fds set has been updated, we need to update our
2313 * local array as well
2315 pthread_mutex_lock(&consumer_data
.lock
);
2316 if (consumer_data
.need_update
) {
2321 local_stream
= NULL
;
2323 /* allocate for all fds + 1 for the consumer_data_pipe */
2324 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2325 if (pollfd
== NULL
) {
2326 PERROR("pollfd malloc");
2327 pthread_mutex_unlock(&consumer_data
.lock
);
2331 /* allocate for all fds + 1 for the consumer_data_pipe */
2332 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2333 sizeof(struct lttng_consumer_stream
*));
2334 if (local_stream
== NULL
) {
2335 PERROR("local_stream malloc");
2336 pthread_mutex_unlock(&consumer_data
.lock
);
2339 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2342 ERR("Error in allocating pollfd or local_outfds");
2343 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2344 pthread_mutex_unlock(&consumer_data
.lock
);
2348 consumer_data
.need_update
= 0;
2350 pthread_mutex_unlock(&consumer_data
.lock
);
2352 /* No FDs and consumer_quit, consumer_cleanup the thread */
2353 if (nb_fd
== 0 && consumer_quit
== 1) {
2356 /* poll on the array of fds */
2358 DBG("polling on %d fd", nb_fd
+ 1);
2359 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2360 DBG("poll num_rdy : %d", num_rdy
);
2361 if (num_rdy
== -1) {
2363 * Restart interrupted system call.
2365 if (errno
== EINTR
) {
2368 PERROR("Poll error");
2369 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2371 } else if (num_rdy
== 0) {
2372 DBG("Polling thread timed out");
2377 * If the consumer_data_pipe triggered poll go directly to the
2378 * beginning of the loop to update the array. We want to prioritize
2379 * array update over low-priority reads.
2381 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2382 ssize_t pipe_readlen
;
2384 DBG("consumer_data_pipe wake up");
2385 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2386 &new_stream
, sizeof(new_stream
));
2387 if (pipe_readlen
< 0) {
2388 ERR("Consumer data pipe ret %ld", pipe_readlen
);
2389 /* Continue so we can at least handle the current stream(s). */
2394 * If the stream is NULL, just ignore it. It's also possible that
2395 * the sessiond poll thread changed the consumer_quit state and is
2396 * waking us up to test it.
2398 if (new_stream
== NULL
) {
2399 validate_endpoint_status_data_stream();
2403 ret
= add_stream(new_stream
, data_ht
);
2405 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
2408 * At this point, if the add_stream fails, it is not in the
2409 * hash table thus passing the NULL value here.
2411 consumer_del_stream(new_stream
, NULL
);
2414 /* Continue to update the local streams and handle prio ones */
2418 /* Take care of high priority channels first. */
2419 for (i
= 0; i
< nb_fd
; i
++) {
2420 if (local_stream
[i
] == NULL
) {
2423 if (pollfd
[i
].revents
& POLLPRI
) {
2424 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2426 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2427 /* it's ok to have an unavailable sub-buffer */
2428 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2429 /* Clean the stream and free it. */
2430 consumer_del_stream(local_stream
[i
], data_ht
);
2431 local_stream
[i
] = NULL
;
2432 } else if (len
> 0) {
2433 local_stream
[i
]->data_read
= 1;
2439 * If we read high prio channel in this loop, try again
2440 * for more high prio data.
2446 /* Take care of low priority channels. */
2447 for (i
= 0; i
< nb_fd
; i
++) {
2448 if (local_stream
[i
] == NULL
) {
2451 if ((pollfd
[i
].revents
& POLLIN
) ||
2452 local_stream
[i
]->hangup_flush_done
) {
2453 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2454 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2455 /* it's ok to have an unavailable sub-buffer */
2456 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2457 /* Clean the stream and free it. */
2458 consumer_del_stream(local_stream
[i
], data_ht
);
2459 local_stream
[i
] = NULL
;
2460 } else if (len
> 0) {
2461 local_stream
[i
]->data_read
= 1;
2466 /* Handle hangup and errors */
2467 for (i
= 0; i
< nb_fd
; i
++) {
2468 if (local_stream
[i
] == NULL
) {
2471 if (!local_stream
[i
]->hangup_flush_done
2472 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2473 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2474 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2475 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2477 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2478 /* Attempt read again, for the data we just flushed. */
2479 local_stream
[i
]->data_read
= 1;
2482 * If the poll flag is HUP/ERR/NVAL and we have
2483 * read no data in this pass, we can remove the
2484 * stream from its hash table.
2486 if ((pollfd
[i
].revents
& POLLHUP
)) {
2487 DBG("Polling fd %d tells it has hung up.", 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
& POLLERR
) {
2494 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2495 if (!local_stream
[i
]->data_read
) {
2496 consumer_del_stream(local_stream
[i
], data_ht
);
2497 local_stream
[i
] = NULL
;
2500 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2501 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2502 if (!local_stream
[i
]->data_read
) {
2503 consumer_del_stream(local_stream
[i
], data_ht
);
2504 local_stream
[i
] = NULL
;
2508 if (local_stream
[i
] != NULL
) {
2509 local_stream
[i
]->data_read
= 0;
2514 DBG("polling thread exiting");
2519 * Close the write side of the pipe so epoll_wait() in
2520 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2521 * read side of the pipe. If we close them both, epoll_wait strangely does
2522 * not return and could create a endless wait period if the pipe is the
2523 * only tracked fd in the poll set. The thread will take care of closing
2526 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2528 destroy_data_stream_ht(data_ht
);
2530 rcu_unregister_thread();
2535 * Close wake-up end of each stream belonging to the channel. This will
2536 * allow the poll() on the stream read-side to detect when the
2537 * write-side (application) finally closes them.
2540 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2542 struct lttng_ht
*ht
;
2543 struct lttng_consumer_stream
*stream
;
2544 struct lttng_ht_iter iter
;
2546 ht
= consumer_data
.stream_per_chan_id_ht
;
2549 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2550 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2551 ht
->match_fct
, &channel
->key
,
2552 &iter
.iter
, stream
, node_channel_id
.node
) {
2554 * Protect against teardown with mutex.
2556 pthread_mutex_lock(&stream
->lock
);
2557 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2560 switch (consumer_data
.type
) {
2561 case LTTNG_CONSUMER_KERNEL
:
2563 case LTTNG_CONSUMER32_UST
:
2564 case LTTNG_CONSUMER64_UST
:
2566 * Note: a mutex is taken internally within
2567 * liblttng-ust-ctl to protect timer wakeup_fd
2568 * use from concurrent close.
2570 lttng_ustconsumer_close_stream_wakeup(stream
);
2573 ERR("Unknown consumer_data type");
2577 pthread_mutex_unlock(&stream
->lock
);
2582 static void destroy_channel_ht(struct lttng_ht
*ht
)
2584 struct lttng_ht_iter iter
;
2585 struct lttng_consumer_channel
*channel
;
2593 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2594 ret
= lttng_ht_del(ht
, &iter
);
2599 lttng_ht_destroy(ht
);
2603 * This thread polls the channel fds to detect when they are being
2604 * closed. It closes all related streams if the channel is detected as
2605 * closed. It is currently only used as a shim layer for UST because the
2606 * consumerd needs to keep the per-stream wakeup end of pipes open for
2609 void *consumer_thread_channel_poll(void *data
)
2612 uint32_t revents
, nb_fd
;
2613 struct lttng_consumer_channel
*chan
= NULL
;
2614 struct lttng_ht_iter iter
;
2615 struct lttng_ht_node_u64
*node
;
2616 struct lttng_poll_event events
;
2617 struct lttng_consumer_local_data
*ctx
= data
;
2618 struct lttng_ht
*channel_ht
;
2620 rcu_register_thread();
2622 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2624 /* ENOMEM at this point. Better to bail out. */
2628 DBG("Thread channel poll started");
2630 /* Size is set to 1 for the consumer_channel pipe */
2631 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2633 ERR("Poll set creation failed");
2637 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2643 DBG("Channel main loop started");
2646 /* Only the channel pipe is set */
2647 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2652 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2653 ret
= lttng_poll_wait(&events
, -1);
2654 DBG("Channel event catched in thread");
2656 if (errno
== EINTR
) {
2657 ERR("Poll EINTR catched");
2665 /* From here, the event is a channel wait fd */
2666 for (i
= 0; i
< nb_fd
; i
++) {
2667 revents
= LTTNG_POLL_GETEV(&events
, i
);
2668 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2670 /* Just don't waste time if no returned events for the fd */
2674 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2675 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2676 DBG("Channel thread pipe hung up");
2678 * Remove the pipe from the poll set and continue the loop
2679 * since their might be data to consume.
2681 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2683 } else if (revents
& LPOLLIN
) {
2684 enum consumer_channel_action action
;
2687 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2689 ERR("Error reading channel pipe");
2694 case CONSUMER_CHANNEL_ADD
:
2695 DBG("Adding channel %d to poll set",
2698 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2701 lttng_ht_add_unique_u64(channel_ht
,
2702 &chan
->wait_fd_node
);
2704 /* Add channel to the global poll events list */
2705 lttng_poll_add(&events
, chan
->wait_fd
,
2706 LPOLLIN
| LPOLLPRI
);
2708 case CONSUMER_CHANNEL_DEL
:
2710 struct lttng_consumer_stream
*stream
, *stmp
;
2713 chan
= consumer_find_channel(key
);
2716 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2719 lttng_poll_del(&events
, chan
->wait_fd
);
2720 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2721 ret
= lttng_ht_del(channel_ht
, &iter
);
2723 consumer_close_channel_streams(chan
);
2725 switch (consumer_data
.type
) {
2726 case LTTNG_CONSUMER_KERNEL
:
2728 case LTTNG_CONSUMER32_UST
:
2729 case LTTNG_CONSUMER64_UST
:
2730 /* Delete streams that might have been left in the stream list. */
2731 cds_list_for_each_entry_safe(stream
, stmp
, &chan
->streams
.head
,
2733 cds_list_del(&stream
->send_node
);
2734 lttng_ustconsumer_del_stream(stream
);
2735 uatomic_sub(&stream
->chan
->refcount
, 1);
2736 assert(&chan
->refcount
);
2741 ERR("Unknown consumer_data type");
2746 * Release our own refcount. Force channel deletion even if
2747 * streams were not initialized.
2749 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2750 consumer_del_channel(chan
);
2755 case CONSUMER_CHANNEL_QUIT
:
2757 * Remove the pipe from the poll set and continue the loop
2758 * since their might be data to consume.
2760 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2763 ERR("Unknown action");
2768 /* Handle other stream */
2774 uint64_t tmp_id
= (uint64_t) pollfd
;
2776 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2778 node
= lttng_ht_iter_get_node_u64(&iter
);
2781 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2784 /* Check for error event */
2785 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2786 DBG("Channel fd %d is hup|err.", pollfd
);
2788 lttng_poll_del(&events
, chan
->wait_fd
);
2789 ret
= lttng_ht_del(channel_ht
, &iter
);
2791 consumer_close_channel_streams(chan
);
2793 /* Release our own refcount */
2794 if (!uatomic_sub_return(&chan
->refcount
, 1)
2795 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2796 consumer_del_channel(chan
);
2800 /* Release RCU lock for the channel looked up */
2806 lttng_poll_clean(&events
);
2808 destroy_channel_ht(channel_ht
);
2810 DBG("Channel poll thread exiting");
2811 rcu_unregister_thread();
2815 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2816 struct pollfd
*sockpoll
, int client_socket
)
2823 if (lttng_consumer_poll_socket(sockpoll
) < 0) {
2827 DBG("Metadata connection on client_socket");
2829 /* Blocking call, waiting for transmission */
2830 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2831 if (ctx
->consumer_metadata_socket
< 0) {
2832 WARN("On accept metadata");
2843 * This thread listens on the consumerd socket and receives the file
2844 * descriptors from the session daemon.
2846 void *consumer_thread_sessiond_poll(void *data
)
2848 int sock
= -1, client_socket
, ret
;
2850 * structure to poll for incoming data on communication socket avoids
2851 * making blocking sockets.
2853 struct pollfd consumer_sockpoll
[2];
2854 struct lttng_consumer_local_data
*ctx
= data
;
2856 rcu_register_thread();
2858 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2859 unlink(ctx
->consumer_command_sock_path
);
2860 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2861 if (client_socket
< 0) {
2862 ERR("Cannot create command socket");
2866 ret
= lttcomm_listen_unix_sock(client_socket
);
2871 DBG("Sending ready command to lttng-sessiond");
2872 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2873 /* return < 0 on error, but == 0 is not fatal */
2875 ERR("Error sending ready command to lttng-sessiond");
2879 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2880 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2881 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2882 consumer_sockpoll
[1].fd
= client_socket
;
2883 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2885 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2888 DBG("Connection on client_socket");
2890 /* Blocking call, waiting for transmission */
2891 sock
= lttcomm_accept_unix_sock(client_socket
);
2898 * Setup metadata socket which is the second socket connection on the
2899 * command unix socket.
2901 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
2906 /* This socket is not useful anymore. */
2907 ret
= close(client_socket
);
2909 PERROR("close client_socket");
2913 /* update the polling structure to poll on the established socket */
2914 consumer_sockpoll
[1].fd
= sock
;
2915 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2918 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2921 DBG("Incoming command on sock");
2922 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2923 if (ret
== -ENOENT
) {
2924 DBG("Received STOP command");
2929 * This could simply be a session daemon quitting. Don't output
2932 DBG("Communication interrupted on command socket");
2935 if (consumer_quit
) {
2936 DBG("consumer_thread_receive_fds received quit from signal");
2939 DBG("received command on sock");
2942 DBG("Consumer thread sessiond poll exiting");
2945 * Close metadata streams since the producer is the session daemon which
2948 * NOTE: for now, this only applies to the UST tracer.
2950 lttng_consumer_close_metadata();
2953 * when all fds have hung up, the polling thread
2959 * Notify the data poll thread to poll back again and test the
2960 * consumer_quit state that we just set so to quit gracefully.
2962 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
2964 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
2966 /* Cleaning up possibly open sockets. */
2970 PERROR("close sock sessiond poll");
2973 if (client_socket
>= 0) {
2974 ret
= close(client_socket
);
2976 PERROR("close client_socket sessiond poll");
2980 rcu_unregister_thread();
2984 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2985 struct lttng_consumer_local_data
*ctx
)
2989 pthread_mutex_lock(&stream
->lock
);
2991 switch (consumer_data
.type
) {
2992 case LTTNG_CONSUMER_KERNEL
:
2993 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
2995 case LTTNG_CONSUMER32_UST
:
2996 case LTTNG_CONSUMER64_UST
:
2997 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3000 ERR("Unknown consumer_data type");
3006 pthread_mutex_unlock(&stream
->lock
);
3010 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3012 switch (consumer_data
.type
) {
3013 case LTTNG_CONSUMER_KERNEL
:
3014 return lttng_kconsumer_on_recv_stream(stream
);
3015 case LTTNG_CONSUMER32_UST
:
3016 case LTTNG_CONSUMER64_UST
:
3017 return lttng_ustconsumer_on_recv_stream(stream
);
3019 ERR("Unknown consumer_data type");
3026 * Allocate and set consumer data hash tables.
3028 void lttng_consumer_init(void)
3030 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3031 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3032 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3033 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3037 * Process the ADD_RELAYD command receive by a consumer.
3039 * This will create a relayd socket pair and add it to the relayd hash table.
3040 * The caller MUST acquire a RCU read side lock before calling it.
3042 int consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3043 struct lttng_consumer_local_data
*ctx
, int sock
,
3044 struct pollfd
*consumer_sockpoll
,
3045 struct lttcomm_relayd_sock
*relayd_sock
, unsigned int sessiond_id
)
3047 int fd
= -1, ret
= -1, relayd_created
= 0;
3048 enum lttng_error_code ret_code
= LTTNG_OK
;
3049 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3052 assert(relayd_sock
);
3054 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3056 /* Get relayd reference if exists. */
3057 relayd
= consumer_find_relayd(net_seq_idx
);
3058 if (relayd
== NULL
) {
3059 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3060 /* Not found. Allocate one. */
3061 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3062 if (relayd
== NULL
) {
3064 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3067 relayd
->sessiond_session_id
= (uint64_t) sessiond_id
;
3072 * This code path MUST continue to the consumer send status message to
3073 * we can notify the session daemon and continue our work without
3074 * killing everything.
3078 * relayd key should never be found for control socket.
3080 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3083 /* First send a status message before receiving the fds. */
3084 ret
= consumer_send_status_msg(sock
, LTTNG_OK
);
3086 /* Somehow, the session daemon is not responding anymore. */
3087 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3088 goto error_nosignal
;
3091 /* Poll on consumer socket. */
3092 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
3093 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3095 goto error_nosignal
;
3098 /* Get relayd socket from session daemon */
3099 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3100 if (ret
!= sizeof(fd
)) {
3102 fd
= -1; /* Just in case it gets set with an invalid value. */
3105 * Failing to receive FDs might indicate a major problem such as
3106 * reaching a fd limit during the receive where the kernel returns a
3107 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3108 * don't take any chances and stop everything.
3110 * XXX: Feature request #558 will fix that and avoid this possible
3111 * issue when reaching the fd limit.
3113 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3114 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3118 /* Copy socket information and received FD */
3119 switch (sock_type
) {
3120 case LTTNG_STREAM_CONTROL
:
3121 /* Copy received lttcomm socket */
3122 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3123 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3124 /* Handle create_sock error. */
3126 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3130 * Close the socket created internally by
3131 * lttcomm_create_sock, so we can replace it by the one
3132 * received from sessiond.
3134 if (close(relayd
->control_sock
.sock
.fd
)) {
3138 /* Assign new file descriptor */
3139 relayd
->control_sock
.sock
.fd
= fd
;
3140 fd
= -1; /* For error path */
3141 /* Assign version values. */
3142 relayd
->control_sock
.major
= relayd_sock
->major
;
3143 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3146 * Create a session on the relayd and store the returned id. Lock the
3147 * control socket mutex if the relayd was NOT created before.
3149 if (!relayd_created
) {
3150 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3152 ret
= relayd_create_session(&relayd
->control_sock
,
3153 &relayd
->relayd_session_id
);
3154 if (!relayd_created
) {
3155 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3159 * Close all sockets of a relayd object. It will be freed if it was
3160 * created at the error code path or else it will be garbage
3163 (void) relayd_close(&relayd
->control_sock
);
3164 (void) relayd_close(&relayd
->data_sock
);
3165 ret_code
= LTTCOMM_CONSUMERD_RELAYD_FAIL
;
3170 case LTTNG_STREAM_DATA
:
3171 /* Copy received lttcomm socket */
3172 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3173 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3174 /* Handle create_sock error. */
3176 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3180 * Close the socket created internally by
3181 * lttcomm_create_sock, so we can replace it by the one
3182 * received from sessiond.
3184 if (close(relayd
->data_sock
.sock
.fd
)) {
3188 /* Assign new file descriptor */
3189 relayd
->data_sock
.sock
.fd
= fd
;
3190 fd
= -1; /* for eventual error paths */
3191 /* Assign version values. */
3192 relayd
->data_sock
.major
= relayd_sock
->major
;
3193 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3196 ERR("Unknown relayd socket type (%d)", sock_type
);
3198 ret_code
= LTTCOMM_CONSUMERD_FATAL
;
3202 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3203 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3204 relayd
->net_seq_idx
, fd
);
3206 /* We successfully added the socket. Send status back. */
3207 ret
= consumer_send_status_msg(sock
, ret_code
);
3209 /* Somehow, the session daemon is not responding anymore. */
3210 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3211 goto error_nosignal
;
3215 * Add relayd socket pair to consumer data hashtable. If object already
3216 * exists or on error, the function gracefully returns.
3224 if (consumer_send_status_msg(sock
, ret_code
) < 0) {
3225 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_FATAL
);
3229 /* Close received socket if valid. */
3232 PERROR("close received socket");
3236 if (relayd_created
) {
3244 * Try to lock the stream mutex.
3246 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3248 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3255 * Try to lock the stream mutex. On failure, we know that the stream is
3256 * being used else where hence there is data still being extracted.
3258 ret
= pthread_mutex_trylock(&stream
->lock
);
3260 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3272 * Search for a relayd associated to the session id and return the reference.
3274 * A rcu read side lock MUST be acquire before calling this function and locked
3275 * until the relayd object is no longer necessary.
3277 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3279 struct lttng_ht_iter iter
;
3280 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3282 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3283 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3286 * Check by sessiond id which is unique here where the relayd session
3287 * id might not be when having multiple relayd.
3289 if (relayd
->sessiond_session_id
== id
) {
3290 /* Found the relayd. There can be only one per id. */
3302 * Check if for a given session id there is still data needed to be extract
3305 * Return 1 if data is pending or else 0 meaning ready to be read.
3307 int consumer_data_pending(uint64_t id
)
3310 struct lttng_ht_iter iter
;
3311 struct lttng_ht
*ht
;
3312 struct lttng_consumer_stream
*stream
;
3313 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3314 int (*data_pending
)(struct lttng_consumer_stream
*);
3316 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3319 pthread_mutex_lock(&consumer_data
.lock
);
3321 switch (consumer_data
.type
) {
3322 case LTTNG_CONSUMER_KERNEL
:
3323 data_pending
= lttng_kconsumer_data_pending
;
3325 case LTTNG_CONSUMER32_UST
:
3326 case LTTNG_CONSUMER64_UST
:
3327 data_pending
= lttng_ustconsumer_data_pending
;
3330 ERR("Unknown consumer data type");
3334 /* Ease our life a bit */
3335 ht
= consumer_data
.stream_list_ht
;
3337 relayd
= find_relayd_by_session_id(id
);
3339 /* Send init command for data pending. */
3340 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3341 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3342 relayd
->relayd_session_id
);
3343 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3345 /* Communication error thus the relayd so no data pending. */
3346 goto data_not_pending
;
3350 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3351 ht
->hash_fct(&id
, lttng_ht_seed
),
3353 &iter
.iter
, stream
, node_session_id
.node
) {
3354 /* If this call fails, the stream is being used hence data pending. */
3355 ret
= stream_try_lock(stream
);
3361 * A removed node from the hash table indicates that the stream has
3362 * been deleted thus having a guarantee that the buffers are closed
3363 * on the consumer side. However, data can still be transmitted
3364 * over the network so don't skip the relayd check.
3366 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3368 /* Check the stream if there is data in the buffers. */
3369 ret
= data_pending(stream
);
3371 pthread_mutex_unlock(&stream
->lock
);
3378 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3379 if (stream
->metadata_flag
) {
3380 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3381 stream
->relayd_stream_id
);
3383 ret
= relayd_data_pending(&relayd
->control_sock
,
3384 stream
->relayd_stream_id
,
3385 stream
->next_net_seq_num
- 1);
3387 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3389 pthread_mutex_unlock(&stream
->lock
);
3393 pthread_mutex_unlock(&stream
->lock
);
3397 unsigned int is_data_inflight
= 0;
3399 /* Send init command for data pending. */
3400 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3401 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3402 relayd
->relayd_session_id
, &is_data_inflight
);
3403 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3405 goto data_not_pending
;
3407 if (is_data_inflight
) {
3413 * Finding _no_ node in the hash table and no inflight data means that the
3414 * stream(s) have been removed thus data is guaranteed to be available for
3415 * analysis from the trace files.
3419 /* Data is available to be read by a viewer. */
3420 pthread_mutex_unlock(&consumer_data
.lock
);
3425 /* Data is still being extracted from buffers. */
3426 pthread_mutex_unlock(&consumer_data
.lock
);
3432 * Send a ret code status message to the sessiond daemon.
3434 * Return the sendmsg() return value.
3436 int consumer_send_status_msg(int sock
, int ret_code
)
3438 struct lttcomm_consumer_status_msg msg
;
3440 msg
.ret_code
= ret_code
;
3442 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3446 * Send a channel status message to the sessiond daemon.
3448 * Return the sendmsg() return value.
3450 int consumer_send_status_channel(int sock
,
3451 struct lttng_consumer_channel
*channel
)
3453 struct lttcomm_consumer_status_channel msg
;
3458 msg
.ret_code
= -LTTNG_ERR_UST_CHAN_FAIL
;
3460 msg
.ret_code
= LTTNG_OK
;
3461 msg
.key
= channel
->key
;
3462 msg
.stream_count
= channel
->streams
.count
;
3465 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));