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>
45 struct lttng_consumer_global_data consumer_data
= {
48 .type
= LTTNG_CONSUMER_UNKNOWN
,
51 enum consumer_channel_action
{
54 CONSUMER_CHANNEL_QUIT
,
57 struct consumer_channel_msg
{
58 enum consumer_channel_action action
;
59 struct lttng_consumer_channel
*chan
; /* add */
60 uint64_t key
; /* del */
64 * Flag to inform the polling thread to quit when all fd hung up. Updated by
65 * the consumer_thread_receive_fds when it notices that all fds has hung up.
66 * Also updated by the signal handler (consumer_should_exit()). Read by the
69 volatile int consumer_quit
;
72 * Global hash table containing respectively metadata and data streams. The
73 * stream element in this ht should only be updated by the metadata poll thread
74 * for the metadata and the data poll thread for the data.
76 static struct lttng_ht
*metadata_ht
;
77 static struct lttng_ht
*data_ht
;
80 * Notify a thread lttng pipe to poll back again. This usually means that some
81 * global state has changed so we just send back the thread in a poll wait
84 static void notify_thread_lttng_pipe(struct lttng_pipe
*pipe
)
86 struct lttng_consumer_stream
*null_stream
= NULL
;
90 (void) lttng_pipe_write(pipe
, &null_stream
, sizeof(null_stream
));
93 static void notify_channel_pipe(struct lttng_consumer_local_data
*ctx
,
94 struct lttng_consumer_channel
*chan
,
96 enum consumer_channel_action action
)
98 struct consumer_channel_msg msg
;
101 memset(&msg
, 0, sizeof(msg
));
107 ret
= write(ctx
->consumer_channel_pipe
[1], &msg
, sizeof(msg
));
108 } while (ret
< 0 && errno
== EINTR
);
111 void notify_thread_del_channel(struct lttng_consumer_local_data
*ctx
,
114 notify_channel_pipe(ctx
, NULL
, key
, CONSUMER_CHANNEL_DEL
);
117 static int read_channel_pipe(struct lttng_consumer_local_data
*ctx
,
118 struct lttng_consumer_channel
**chan
,
120 enum consumer_channel_action
*action
)
122 struct consumer_channel_msg msg
;
126 ret
= read(ctx
->consumer_channel_pipe
[0], &msg
, sizeof(msg
));
127 } while (ret
< 0 && errno
== EINTR
);
129 *action
= msg
.action
;
137 * Find a stream. The consumer_data.lock must be locked during this
140 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
143 struct lttng_ht_iter iter
;
144 struct lttng_ht_node_u64
*node
;
145 struct lttng_consumer_stream
*stream
= NULL
;
149 /* -1ULL keys are lookup failures */
150 if (key
== (uint64_t) -1ULL) {
156 lttng_ht_lookup(ht
, &key
, &iter
);
157 node
= lttng_ht_iter_get_node_u64(&iter
);
159 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
167 static void steal_stream_key(uint64_t key
, struct lttng_ht
*ht
)
169 struct lttng_consumer_stream
*stream
;
172 stream
= find_stream(key
, ht
);
174 stream
->key
= (uint64_t) -1ULL;
176 * We don't want the lookup to match, but we still need
177 * to iterate on this stream when iterating over the hash table. Just
178 * change the node key.
180 stream
->node
.key
= (uint64_t) -1ULL;
186 * Return a channel object for the given key.
188 * RCU read side lock MUST be acquired before calling this function and
189 * protects the channel ptr.
191 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
193 struct lttng_ht_iter iter
;
194 struct lttng_ht_node_u64
*node
;
195 struct lttng_consumer_channel
*channel
= NULL
;
197 /* -1ULL keys are lookup failures */
198 if (key
== (uint64_t) -1ULL) {
202 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
203 node
= lttng_ht_iter_get_node_u64(&iter
);
205 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
211 static void free_stream_rcu(struct rcu_head
*head
)
213 struct lttng_ht_node_u64
*node
=
214 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
215 struct lttng_consumer_stream
*stream
=
216 caa_container_of(node
, struct lttng_consumer_stream
, node
);
221 static void free_channel_rcu(struct rcu_head
*head
)
223 struct lttng_ht_node_u64
*node
=
224 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
225 struct lttng_consumer_channel
*channel
=
226 caa_container_of(node
, struct lttng_consumer_channel
, node
);
232 * RCU protected relayd socket pair free.
234 static void free_relayd_rcu(struct rcu_head
*head
)
236 struct lttng_ht_node_u64
*node
=
237 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
238 struct consumer_relayd_sock_pair
*relayd
=
239 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
242 * Close all sockets. This is done in the call RCU since we don't want the
243 * socket fds to be reassigned thus potentially creating bad state of the
246 * We do not have to lock the control socket mutex here since at this stage
247 * there is no one referencing to this relayd object.
249 (void) relayd_close(&relayd
->control_sock
);
250 (void) relayd_close(&relayd
->data_sock
);
256 * Destroy and free relayd socket pair object.
258 * This function MUST be called with the consumer_data lock acquired.
260 static void destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
263 struct lttng_ht_iter iter
;
265 if (relayd
== NULL
) {
269 DBG("Consumer destroy and close relayd socket pair");
271 iter
.iter
.node
= &relayd
->node
.node
;
272 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
274 /* We assume the relayd is being or is destroyed */
278 /* RCU free() call */
279 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
283 * Remove a channel from the global list protected by a mutex. This function is
284 * also responsible for freeing its data structures.
286 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
289 struct lttng_ht_iter iter
;
290 struct lttng_consumer_stream
*stream
, *stmp
;
292 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
294 pthread_mutex_lock(&consumer_data
.lock
);
296 switch (consumer_data
.type
) {
297 case LTTNG_CONSUMER_KERNEL
:
299 case LTTNG_CONSUMER32_UST
:
300 case LTTNG_CONSUMER64_UST
:
301 /* Delete streams that might have been left in the stream list. */
302 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
304 cds_list_del(&stream
->send_node
);
305 lttng_ustconsumer_del_stream(stream
);
308 lttng_ustconsumer_del_channel(channel
);
311 ERR("Unknown consumer_data type");
317 iter
.iter
.node
= &channel
->node
.node
;
318 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
322 call_rcu(&channel
->node
.head
, free_channel_rcu
);
324 pthread_mutex_unlock(&consumer_data
.lock
);
328 * Iterate over the relayd hash table and destroy each element. Finally,
329 * destroy the whole hash table.
331 static void cleanup_relayd_ht(void)
333 struct lttng_ht_iter iter
;
334 struct consumer_relayd_sock_pair
*relayd
;
338 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
340 destroy_relayd(relayd
);
345 lttng_ht_destroy(consumer_data
.relayd_ht
);
349 * Update the end point status of all streams having the given network sequence
350 * index (relayd index).
352 * It's atomically set without having the stream mutex locked which is fine
353 * because we handle the write/read race with a pipe wakeup for each thread.
355 static void update_endpoint_status_by_netidx(uint64_t net_seq_idx
,
356 enum consumer_endpoint_status status
)
358 struct lttng_ht_iter iter
;
359 struct lttng_consumer_stream
*stream
;
361 DBG("Consumer set delete flag on stream by idx %" PRIu64
, net_seq_idx
);
365 /* Let's begin with metadata */
366 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
367 if (stream
->net_seq_idx
== net_seq_idx
) {
368 uatomic_set(&stream
->endpoint_status
, status
);
369 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
373 /* Follow up by the data streams */
374 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
375 if (stream
->net_seq_idx
== net_seq_idx
) {
376 uatomic_set(&stream
->endpoint_status
, status
);
377 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
384 * Cleanup a relayd object by flagging every associated streams for deletion,
385 * destroying the object meaning removing it from the relayd hash table,
386 * closing the sockets and freeing the memory in a RCU call.
388 * If a local data context is available, notify the threads that the streams'
389 * state have changed.
391 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
392 struct lttng_consumer_local_data
*ctx
)
398 DBG("Cleaning up relayd sockets");
400 /* Save the net sequence index before destroying the object */
401 netidx
= relayd
->net_seq_idx
;
404 * Delete the relayd from the relayd hash table, close the sockets and free
405 * the object in a RCU call.
407 destroy_relayd(relayd
);
409 /* Set inactive endpoint to all streams */
410 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
413 * With a local data context, notify the threads that the streams' state
414 * have changed. The write() action on the pipe acts as an "implicit"
415 * memory barrier ordering the updates of the end point status from the
416 * read of this status which happens AFTER receiving this notify.
419 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
420 notify_thread_lttng_pipe(ctx
->consumer_metadata_pipe
);
425 * Flag a relayd socket pair for destruction. Destroy it if the refcount
428 * RCU read side lock MUST be aquired before calling this function.
430 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
434 /* Set destroy flag for this object */
435 uatomic_set(&relayd
->destroy_flag
, 1);
437 /* Destroy the relayd if refcount is 0 */
438 if (uatomic_read(&relayd
->refcount
) == 0) {
439 destroy_relayd(relayd
);
444 * Remove a stream from the global list protected by a mutex. This
445 * function is also responsible for freeing its data structures.
447 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
451 struct lttng_ht_iter iter
;
452 struct lttng_consumer_channel
*free_chan
= NULL
;
453 struct consumer_relayd_sock_pair
*relayd
;
457 DBG("Consumer del stream %d", stream
->wait_fd
);
460 /* Means the stream was allocated but not successfully added */
461 goto free_stream_rcu
;
464 pthread_mutex_lock(&consumer_data
.lock
);
465 pthread_mutex_lock(&stream
->lock
);
467 switch (consumer_data
.type
) {
468 case LTTNG_CONSUMER_KERNEL
:
469 if (stream
->mmap_base
!= NULL
) {
470 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
476 if (stream
->wait_fd
>= 0) {
477 ret
= close(stream
->wait_fd
);
483 case LTTNG_CONSUMER32_UST
:
484 case LTTNG_CONSUMER64_UST
:
485 lttng_ustconsumer_del_stream(stream
);
488 ERR("Unknown consumer_data type");
494 iter
.iter
.node
= &stream
->node
.node
;
495 ret
= lttng_ht_del(ht
, &iter
);
498 iter
.iter
.node
= &stream
->node_channel_id
.node
;
499 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
502 iter
.iter
.node
= &stream
->node_session_id
.node
;
503 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
507 assert(consumer_data
.stream_count
> 0);
508 consumer_data
.stream_count
--;
510 if (stream
->out_fd
>= 0) {
511 ret
= close(stream
->out_fd
);
517 /* Check and cleanup relayd */
519 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
520 if (relayd
!= NULL
) {
521 uatomic_dec(&relayd
->refcount
);
522 assert(uatomic_read(&relayd
->refcount
) >= 0);
524 /* Closing streams requires to lock the control socket. */
525 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
526 ret
= relayd_send_close_stream(&relayd
->control_sock
,
527 stream
->relayd_stream_id
,
528 stream
->next_net_seq_num
- 1);
529 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
531 DBG("Unable to close stream on the relayd. Continuing");
533 * Continue here. There is nothing we can do for the relayd.
534 * Chances are that the relayd has closed the socket so we just
535 * continue cleaning up.
539 /* Both conditions are met, we destroy the relayd. */
540 if (uatomic_read(&relayd
->refcount
) == 0 &&
541 uatomic_read(&relayd
->destroy_flag
)) {
542 destroy_relayd(relayd
);
547 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
548 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
549 free_chan
= stream
->chan
;
553 consumer_data
.need_update
= 1;
554 pthread_mutex_unlock(&stream
->lock
);
555 pthread_mutex_unlock(&consumer_data
.lock
);
558 consumer_del_channel(free_chan
);
562 call_rcu(&stream
->node
.head
, free_stream_rcu
);
565 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
567 enum lttng_consumer_stream_state state
,
568 const char *channel_name
,
575 enum consumer_channel_type type
)
578 struct lttng_consumer_stream
*stream
;
580 stream
= zmalloc(sizeof(*stream
));
581 if (stream
== NULL
) {
582 PERROR("malloc struct lttng_consumer_stream");
589 stream
->key
= stream_key
;
591 stream
->out_fd_offset
= 0;
592 stream
->state
= state
;
595 stream
->net_seq_idx
= relayd_id
;
596 stream
->session_id
= session_id
;
597 pthread_mutex_init(&stream
->lock
, NULL
);
599 /* If channel is the metadata, flag this stream as metadata. */
600 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
601 stream
->metadata_flag
= 1;
602 /* Metadata is flat out. */
603 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
605 /* Format stream name to <channel_name>_<cpu_number> */
606 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
609 PERROR("snprintf stream name");
614 /* Key is always the wait_fd for streams. */
615 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
617 /* Init node per channel id key */
618 lttng_ht_node_init_u64(&stream
->node_channel_id
, channel_key
);
620 /* Init session id node with the stream session id */
621 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
623 DBG3("Allocated stream %s (key %" PRIu64
", chan_key %" PRIu64
" relayd_id %" PRIu64
", session_id %" PRIu64
,
624 stream
->name
, stream
->key
, channel_key
, stream
->net_seq_idx
, stream
->session_id
);
640 * Add a stream to the global list protected by a mutex.
642 static int add_stream(struct lttng_consumer_stream
*stream
,
646 struct consumer_relayd_sock_pair
*relayd
;
651 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
653 pthread_mutex_lock(&consumer_data
.lock
);
654 pthread_mutex_lock(&stream
->lock
);
657 /* Steal stream identifier to avoid having streams with the same key */
658 steal_stream_key(stream
->key
, ht
);
660 lttng_ht_add_unique_u64(ht
, &stream
->node
);
662 lttng_ht_add_u64(consumer_data
.stream_per_chan_id_ht
,
663 &stream
->node_channel_id
);
666 * Add stream to the stream_list_ht of the consumer data. No need to steal
667 * the key since the HT does not use it and we allow to add redundant keys
670 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
672 /* Check and cleanup relayd */
673 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
674 if (relayd
!= NULL
) {
675 uatomic_inc(&relayd
->refcount
);
679 * When nb_init_stream_left reaches 0, we don't need to trigger any action
680 * in terms of destroying the associated channel, because the action that
681 * causes the count to become 0 also causes a stream to be added. The
682 * channel deletion will thus be triggered by the following removal of this
685 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
686 /* Increment refcount before decrementing nb_init_stream_left */
688 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
691 /* Update consumer data once the node is inserted. */
692 consumer_data
.stream_count
++;
693 consumer_data
.need_update
= 1;
696 pthread_mutex_unlock(&stream
->lock
);
697 pthread_mutex_unlock(&consumer_data
.lock
);
703 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
704 * be acquired before calling this.
706 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
709 struct lttng_ht_node_u64
*node
;
710 struct lttng_ht_iter iter
;
714 lttng_ht_lookup(consumer_data
.relayd_ht
,
715 &relayd
->net_seq_idx
, &iter
);
716 node
= lttng_ht_iter_get_node_u64(&iter
);
720 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
727 * Allocate and return a consumer relayd socket.
729 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
730 uint64_t net_seq_idx
)
732 struct consumer_relayd_sock_pair
*obj
= NULL
;
734 /* net sequence index of -1 is a failure */
735 if (net_seq_idx
== (uint64_t) -1ULL) {
739 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
741 PERROR("zmalloc relayd sock");
745 obj
->net_seq_idx
= net_seq_idx
;
747 obj
->destroy_flag
= 0;
748 obj
->control_sock
.sock
.fd
= -1;
749 obj
->data_sock
.sock
.fd
= -1;
750 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
751 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
758 * Find a relayd socket pair in the global consumer data.
760 * Return the object if found else NULL.
761 * RCU read-side lock must be held across this call and while using the
764 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
766 struct lttng_ht_iter iter
;
767 struct lttng_ht_node_u64
*node
;
768 struct consumer_relayd_sock_pair
*relayd
= NULL
;
770 /* Negative keys are lookup failures */
771 if (key
== (uint64_t) -1ULL) {
775 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
777 node
= lttng_ht_iter_get_node_u64(&iter
);
779 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
787 * Handle stream for relayd transmission if the stream applies for network
788 * streaming where the net sequence index is set.
790 * Return destination file descriptor or negative value on error.
792 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
793 size_t data_size
, unsigned long padding
,
794 struct consumer_relayd_sock_pair
*relayd
)
797 struct lttcomm_relayd_data_hdr data_hdr
;
803 /* Reset data header */
804 memset(&data_hdr
, 0, sizeof(data_hdr
));
806 if (stream
->metadata_flag
) {
807 /* Caller MUST acquire the relayd control socket lock */
808 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
813 /* Metadata are always sent on the control socket. */
814 outfd
= relayd
->control_sock
.sock
.fd
;
816 /* Set header with stream information */
817 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
818 data_hdr
.data_size
= htobe32(data_size
);
819 data_hdr
.padding_size
= htobe32(padding
);
821 * Note that net_seq_num below is assigned with the *current* value of
822 * next_net_seq_num and only after that the next_net_seq_num will be
823 * increment. This is why when issuing a command on the relayd using
824 * this next value, 1 should always be substracted in order to compare
825 * the last seen sequence number on the relayd side to the last sent.
827 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
828 /* Other fields are zeroed previously */
830 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
836 ++stream
->next_net_seq_num
;
838 /* Set to go on data socket */
839 outfd
= relayd
->data_sock
.sock
.fd
;
847 * Allocate and return a new lttng_consumer_channel object using the given key
848 * to initialize the hash table node.
850 * On error, return NULL.
852 struct lttng_consumer_channel
*consumer_allocate_channel(uint64_t key
,
854 const char *pathname
,
859 enum lttng_event_output output
,
860 uint64_t tracefile_size
,
861 uint64_t tracefile_count
,
862 uint64_t session_id_per_pid
)
864 struct lttng_consumer_channel
*channel
;
866 channel
= zmalloc(sizeof(*channel
));
867 if (channel
== NULL
) {
868 PERROR("malloc struct lttng_consumer_channel");
873 channel
->refcount
= 0;
874 channel
->session_id
= session_id
;
875 channel
->session_id_per_pid
= session_id_per_pid
;
878 channel
->relayd_id
= relayd_id
;
879 channel
->output
= output
;
880 channel
->tracefile_size
= tracefile_size
;
881 channel
->tracefile_count
= tracefile_count
;
883 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
884 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
886 strncpy(channel
->name
, name
, sizeof(channel
->name
));
887 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
889 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
891 channel
->wait_fd
= -1;
893 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
895 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
902 * Add a channel to the global list protected by a mutex.
904 * On success 0 is returned else a negative value.
906 int consumer_add_channel(struct lttng_consumer_channel
*channel
,
907 struct lttng_consumer_local_data
*ctx
)
910 struct lttng_ht_node_u64
*node
;
911 struct lttng_ht_iter iter
;
913 pthread_mutex_lock(&consumer_data
.lock
);
916 lttng_ht_lookup(consumer_data
.channel_ht
, &channel
->key
, &iter
);
917 node
= lttng_ht_iter_get_node_u64(&iter
);
919 /* Channel already exist. Ignore the insertion */
920 ERR("Consumer add channel key %" PRIu64
" already exists!",
926 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
930 pthread_mutex_unlock(&consumer_data
.lock
);
932 if (!ret
&& channel
->wait_fd
!= -1 &&
933 channel
->metadata_stream
== NULL
) {
934 notify_channel_pipe(ctx
, channel
, -1, CONSUMER_CHANNEL_ADD
);
940 * Allocate the pollfd structure and the local view of the out fds to avoid
941 * doing a lookup in the linked list and concurrency issues when writing is
942 * needed. Called with consumer_data.lock held.
944 * Returns the number of fds in the structures.
946 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
947 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
951 struct lttng_ht_iter iter
;
952 struct lttng_consumer_stream
*stream
;
957 assert(local_stream
);
959 DBG("Updating poll fd array");
961 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
963 * Only active streams with an active end point can be added to the
964 * poll set and local stream storage of the thread.
966 * There is a potential race here for endpoint_status to be updated
967 * just after the check. However, this is OK since the stream(s) will
968 * be deleted once the thread is notified that the end point state has
969 * changed where this function will be called back again.
971 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
972 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
976 * This clobbers way too much the debug output. Uncomment that if you
977 * need it for debugging purposes.
979 * DBG("Active FD %d", stream->wait_fd);
981 (*pollfd
)[i
].fd
= stream
->wait_fd
;
982 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
983 local_stream
[i
] = stream
;
989 * Insert the consumer_data_pipe at the end of the array and don't
990 * increment i so nb_fd is the number of real FD.
992 (*pollfd
)[i
].fd
= lttng_pipe_get_readfd(ctx
->consumer_data_pipe
);
993 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
998 * Poll on the should_quit pipe and the command socket return -1 on error and
999 * should exit, 0 if data is available on the command socket
1001 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1006 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1007 if (num_rdy
== -1) {
1009 * Restart interrupted system call.
1011 if (errno
== EINTR
) {
1014 PERROR("Poll error");
1017 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1018 DBG("consumer_should_quit wake up");
1028 * Set the error socket.
1030 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
1033 ctx
->consumer_error_socket
= sock
;
1037 * Set the command socket path.
1039 void lttng_consumer_set_command_sock_path(
1040 struct lttng_consumer_local_data
*ctx
, char *sock
)
1042 ctx
->consumer_command_sock_path
= sock
;
1046 * Send return code to the session daemon.
1047 * If the socket is not defined, we return 0, it is not a fatal error
1049 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
1051 if (ctx
->consumer_error_socket
> 0) {
1052 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1053 sizeof(enum lttcomm_sessiond_command
));
1060 * Close all the tracefiles and stream fds and MUST be called when all
1061 * instances are destroyed i.e. when all threads were joined and are ended.
1063 void lttng_consumer_cleanup(void)
1065 struct lttng_ht_iter iter
;
1066 struct lttng_consumer_channel
*channel
;
1070 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
1072 consumer_del_channel(channel
);
1077 lttng_ht_destroy(consumer_data
.channel_ht
);
1079 cleanup_relayd_ht();
1081 lttng_ht_destroy(consumer_data
.stream_per_chan_id_ht
);
1084 * This HT contains streams that are freed by either the metadata thread or
1085 * the data thread so we do *nothing* on the hash table and simply destroy
1088 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1092 * Called from signal handler.
1094 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1099 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1100 } while (ret
< 0 && errno
== EINTR
);
1101 if (ret
< 0 || ret
!= 1) {
1102 PERROR("write consumer quit");
1105 DBG("Consumer flag that it should quit");
1108 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1111 int outfd
= stream
->out_fd
;
1114 * This does a blocking write-and-wait on any page that belongs to the
1115 * subbuffer prior to the one we just wrote.
1116 * Don't care about error values, as these are just hints and ways to
1117 * limit the amount of page cache used.
1119 if (orig_offset
< stream
->max_sb_size
) {
1122 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1123 stream
->max_sb_size
,
1124 SYNC_FILE_RANGE_WAIT_BEFORE
1125 | SYNC_FILE_RANGE_WRITE
1126 | SYNC_FILE_RANGE_WAIT_AFTER
);
1128 * Give hints to the kernel about how we access the file:
1129 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1132 * We need to call fadvise again after the file grows because the
1133 * kernel does not seem to apply fadvise to non-existing parts of the
1136 * Call fadvise _after_ having waited for the page writeback to
1137 * complete because the dirty page writeback semantic is not well
1138 * defined. So it can be expected to lead to lower throughput in
1141 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1142 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1146 * Initialise the necessary environnement :
1147 * - create a new context
1148 * - create the poll_pipe
1149 * - create the should_quit pipe (for signal handler)
1150 * - create the thread pipe (for splice)
1152 * Takes a function pointer as argument, this function is called when data is
1153 * available on a buffer. This function is responsible to do the
1154 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1155 * buffer configuration and then kernctl_put_next_subbuf at the end.
1157 * Returns a pointer to the new context or NULL on error.
1159 struct lttng_consumer_local_data
*lttng_consumer_create(
1160 enum lttng_consumer_type type
,
1161 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1162 struct lttng_consumer_local_data
*ctx
),
1163 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1164 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1165 int (*update_stream
)(uint64_t stream_key
, uint32_t state
))
1168 struct lttng_consumer_local_data
*ctx
;
1170 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1171 consumer_data
.type
== type
);
1172 consumer_data
.type
= type
;
1174 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1176 PERROR("allocating context");
1180 ctx
->consumer_error_socket
= -1;
1181 ctx
->consumer_metadata_socket
= -1;
1182 /* assign the callbacks */
1183 ctx
->on_buffer_ready
= buffer_ready
;
1184 ctx
->on_recv_channel
= recv_channel
;
1185 ctx
->on_recv_stream
= recv_stream
;
1186 ctx
->on_update_stream
= update_stream
;
1188 ctx
->consumer_data_pipe
= lttng_pipe_open(0);
1189 if (!ctx
->consumer_data_pipe
) {
1190 goto error_poll_pipe
;
1193 ret
= pipe(ctx
->consumer_should_quit
);
1195 PERROR("Error creating recv pipe");
1196 goto error_quit_pipe
;
1199 ret
= pipe(ctx
->consumer_thread_pipe
);
1201 PERROR("Error creating thread pipe");
1202 goto error_thread_pipe
;
1205 ret
= pipe(ctx
->consumer_channel_pipe
);
1207 PERROR("Error creating channel pipe");
1208 goto error_channel_pipe
;
1211 ctx
->consumer_metadata_pipe
= lttng_pipe_open(0);
1212 if (!ctx
->consumer_metadata_pipe
) {
1213 goto error_metadata_pipe
;
1216 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1218 goto error_splice_pipe
;
1224 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1225 error_metadata_pipe
:
1226 utils_close_pipe(ctx
->consumer_channel_pipe
);
1228 utils_close_pipe(ctx
->consumer_thread_pipe
);
1230 utils_close_pipe(ctx
->consumer_should_quit
);
1232 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1240 * Close all fds associated with the instance and free the context.
1242 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1246 DBG("Consumer destroying it. Closing everything.");
1248 ret
= close(ctx
->consumer_error_socket
);
1252 ret
= close(ctx
->consumer_metadata_socket
);
1256 utils_close_pipe(ctx
->consumer_thread_pipe
);
1257 utils_close_pipe(ctx
->consumer_channel_pipe
);
1258 lttng_pipe_destroy(ctx
->consumer_data_pipe
);
1259 lttng_pipe_destroy(ctx
->consumer_metadata_pipe
);
1260 utils_close_pipe(ctx
->consumer_should_quit
);
1261 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1263 unlink(ctx
->consumer_command_sock_path
);
1268 * Write the metadata stream id on the specified file descriptor.
1270 static int write_relayd_metadata_id(int fd
,
1271 struct lttng_consumer_stream
*stream
,
1272 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1275 struct lttcomm_relayd_metadata_payload hdr
;
1277 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1278 hdr
.padding_size
= htobe32(padding
);
1280 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1281 } while (ret
< 0 && errno
== EINTR
);
1282 if (ret
< 0 || ret
!= sizeof(hdr
)) {
1284 * This error means that the fd's end is closed so ignore the perror
1285 * not to clubber the error output since this can happen in a normal
1288 if (errno
!= EPIPE
) {
1289 PERROR("write metadata stream id");
1291 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1293 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1294 * handle writting the missing part so report that as an error and
1295 * don't lie to the caller.
1300 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1301 stream
->relayd_stream_id
, padding
);
1308 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1309 * core function for writing trace buffers to either the local filesystem or
1312 * It must be called with the stream lock held.
1314 * Careful review MUST be put if any changes occur!
1316 * Returns the number of bytes written
1318 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1319 struct lttng_consumer_local_data
*ctx
,
1320 struct lttng_consumer_stream
*stream
, unsigned long len
,
1321 unsigned long padding
)
1323 unsigned long mmap_offset
;
1325 ssize_t ret
= 0, written
= 0;
1326 off_t orig_offset
= stream
->out_fd_offset
;
1327 /* Default is on the disk */
1328 int outfd
= stream
->out_fd
;
1329 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1330 unsigned int relayd_hang_up
= 0;
1332 /* RCU lock for the relayd pointer */
1335 /* Flag that the current stream if set for network streaming. */
1336 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1337 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1338 if (relayd
== NULL
) {
1343 /* get the offset inside the fd to mmap */
1344 switch (consumer_data
.type
) {
1345 case LTTNG_CONSUMER_KERNEL
:
1346 mmap_base
= stream
->mmap_base
;
1347 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1349 case LTTNG_CONSUMER32_UST
:
1350 case LTTNG_CONSUMER64_UST
:
1351 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1353 ERR("read mmap get mmap base for stream %s", stream
->name
);
1357 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1361 ERR("Unknown consumer_data type");
1366 PERROR("tracer ctl get_mmap_read_offset");
1371 /* Handle stream on the relayd if the output is on the network */
1373 unsigned long netlen
= len
;
1376 * Lock the control socket for the complete duration of the function
1377 * since from this point on we will use the socket.
1379 if (stream
->metadata_flag
) {
1380 /* Metadata requires the control socket. */
1381 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1382 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1385 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1387 /* Use the returned socket. */
1390 /* Write metadata stream id before payload */
1391 if (stream
->metadata_flag
) {
1392 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1395 /* Socket operation failed. We consider the relayd dead */
1396 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1404 /* Socket operation failed. We consider the relayd dead */
1405 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1409 /* Else, use the default set before which is the filesystem. */
1412 /* No streaming, we have to set the len with the full padding */
1416 * Check if we need to change the tracefile before writing the packet.
1418 if (stream
->chan
->tracefile_size
> 0 &&
1419 (stream
->tracefile_size_current
+ len
) >
1420 stream
->chan
->tracefile_size
) {
1421 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1422 stream
->name
, stream
->chan
->tracefile_size
,
1423 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1424 stream
->out_fd
, &(stream
->tracefile_count_current
));
1426 ERR("Rotating output file");
1429 outfd
= stream
->out_fd
= ret
;
1430 /* Reset current size because we just perform a rotation. */
1431 stream
->tracefile_size_current
= 0;
1433 stream
->tracefile_size_current
+= len
;
1438 ret
= write(outfd
, mmap_base
+ mmap_offset
, len
);
1439 } while (ret
< 0 && errno
== EINTR
);
1440 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1443 * This is possible if the fd is closed on the other side (outfd)
1444 * or any write problem. It can be verbose a bit for a normal
1445 * execution if for instance the relayd is stopped abruptly. This
1446 * can happen so set this to a DBG statement.
1448 DBG("Error in file write mmap");
1452 /* Socket operation failed. We consider the relayd dead */
1453 if (errno
== EPIPE
|| errno
== EINVAL
) {
1458 } else if (ret
> len
) {
1459 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1467 /* This call is useless on a socket so better save a syscall. */
1469 /* This won't block, but will start writeout asynchronously */
1470 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1471 SYNC_FILE_RANGE_WRITE
);
1472 stream
->out_fd_offset
+= ret
;
1476 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1480 * This is a special case that the relayd has closed its socket. Let's
1481 * cleanup the relayd object and all associated streams.
1483 if (relayd
&& relayd_hang_up
) {
1484 cleanup_relayd(relayd
, ctx
);
1488 /* Unlock only if ctrl socket used */
1489 if (relayd
&& stream
->metadata_flag
) {
1490 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1498 * Splice the data from the ring buffer to the tracefile.
1500 * It must be called with the stream lock held.
1502 * Returns the number of bytes spliced.
1504 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1505 struct lttng_consumer_local_data
*ctx
,
1506 struct lttng_consumer_stream
*stream
, unsigned long len
,
1507 unsigned long padding
)
1509 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1511 off_t orig_offset
= stream
->out_fd_offset
;
1512 int fd
= stream
->wait_fd
;
1513 /* Default is on the disk */
1514 int outfd
= stream
->out_fd
;
1515 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1517 unsigned int relayd_hang_up
= 0;
1519 switch (consumer_data
.type
) {
1520 case LTTNG_CONSUMER_KERNEL
:
1522 case LTTNG_CONSUMER32_UST
:
1523 case LTTNG_CONSUMER64_UST
:
1524 /* Not supported for user space tracing */
1527 ERR("Unknown consumer_data type");
1531 /* RCU lock for the relayd pointer */
1534 /* Flag that the current stream if set for network streaming. */
1535 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1536 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1537 if (relayd
== NULL
) {
1543 * Choose right pipe for splice. Metadata and trace data are handled by
1544 * different threads hence the use of two pipes in order not to race or
1545 * corrupt the written data.
1547 if (stream
->metadata_flag
) {
1548 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1550 splice_pipe
= ctx
->consumer_thread_pipe
;
1553 /* Write metadata stream id before payload */
1555 int total_len
= len
;
1557 if (stream
->metadata_flag
) {
1559 * Lock the control socket for the complete duration of the function
1560 * since from this point on we will use the socket.
1562 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1564 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1568 /* Socket operation failed. We consider the relayd dead */
1569 if (ret
== -EBADF
) {
1570 WARN("Remote relayd disconnected. Stopping");
1577 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1580 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1582 /* Use the returned socket. */
1585 /* Socket operation failed. We consider the relayd dead */
1586 if (ret
== -EBADF
) {
1587 WARN("Remote relayd disconnected. Stopping");
1594 /* No streaming, we have to set the len with the full padding */
1598 * Check if we need to change the tracefile before writing the packet.
1600 if (stream
->chan
->tracefile_size
> 0 &&
1601 (stream
->tracefile_size_current
+ len
) >
1602 stream
->chan
->tracefile_size
) {
1603 ret
= utils_rotate_stream_file(stream
->chan
->pathname
,
1604 stream
->name
, stream
->chan
->tracefile_size
,
1605 stream
->chan
->tracefile_count
, stream
->uid
, stream
->gid
,
1606 stream
->out_fd
, &(stream
->tracefile_count_current
));
1608 ERR("Rotating output file");
1611 outfd
= stream
->out_fd
= ret
;
1612 /* Reset current size because we just perform a rotation. */
1613 stream
->tracefile_size_current
= 0;
1615 stream
->tracefile_size_current
+= len
;
1619 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1620 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1621 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1622 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1623 DBG("splice chan to pipe, ret %zd", ret_splice
);
1624 if (ret_splice
< 0) {
1625 PERROR("Error in relay splice");
1627 written
= ret_splice
;
1633 /* Handle stream on the relayd if the output is on the network */
1635 if (stream
->metadata_flag
) {
1636 size_t metadata_payload_size
=
1637 sizeof(struct lttcomm_relayd_metadata_payload
);
1639 /* Update counter to fit the spliced data */
1640 ret_splice
+= metadata_payload_size
;
1641 len
+= metadata_payload_size
;
1643 * We do this so the return value can match the len passed as
1644 * argument to this function.
1646 written
-= metadata_payload_size
;
1650 /* Splice data out */
1651 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1652 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1653 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1654 if (ret_splice
< 0) {
1655 PERROR("Error in file splice");
1657 written
= ret_splice
;
1659 /* Socket operation failed. We consider the relayd dead */
1660 if (errno
== EBADF
|| errno
== EPIPE
) {
1661 WARN("Remote relayd disconnected. Stopping");
1667 } else if (ret_splice
> len
) {
1669 PERROR("Wrote more data than requested %zd (len: %lu)",
1671 written
+= ret_splice
;
1677 /* This call is useless on a socket so better save a syscall. */
1679 /* This won't block, but will start writeout asynchronously */
1680 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1681 SYNC_FILE_RANGE_WRITE
);
1682 stream
->out_fd_offset
+= ret_splice
;
1684 written
+= ret_splice
;
1686 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1694 * This is a special case that the relayd has closed its socket. Let's
1695 * cleanup the relayd object and all associated streams.
1697 if (relayd
&& relayd_hang_up
) {
1698 cleanup_relayd(relayd
, ctx
);
1699 /* Skip splice error so the consumer does not fail */
1704 /* send the appropriate error description to sessiond */
1707 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1710 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1713 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1718 if (relayd
&& stream
->metadata_flag
) {
1719 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1727 * Take a snapshot for a specific fd
1729 * Returns 0 on success, < 0 on error
1731 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1733 switch (consumer_data
.type
) {
1734 case LTTNG_CONSUMER_KERNEL
:
1735 return lttng_kconsumer_take_snapshot(stream
);
1736 case LTTNG_CONSUMER32_UST
:
1737 case LTTNG_CONSUMER64_UST
:
1738 return lttng_ustconsumer_take_snapshot(stream
);
1740 ERR("Unknown consumer_data type");
1747 * Get the produced position
1749 * Returns 0 on success, < 0 on error
1751 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1754 switch (consumer_data
.type
) {
1755 case LTTNG_CONSUMER_KERNEL
:
1756 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1757 case LTTNG_CONSUMER32_UST
:
1758 case LTTNG_CONSUMER64_UST
:
1759 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1761 ERR("Unknown consumer_data type");
1767 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1768 int sock
, struct pollfd
*consumer_sockpoll
)
1770 switch (consumer_data
.type
) {
1771 case LTTNG_CONSUMER_KERNEL
:
1772 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1773 case LTTNG_CONSUMER32_UST
:
1774 case LTTNG_CONSUMER64_UST
:
1775 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1777 ERR("Unknown consumer_data type");
1784 * Iterate over all streams of the hashtable and free them properly.
1786 * WARNING: *MUST* be used with data stream only.
1788 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1790 struct lttng_ht_iter iter
;
1791 struct lttng_consumer_stream
*stream
;
1798 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1800 * Ignore return value since we are currently cleaning up so any error
1803 (void) consumer_del_stream(stream
, ht
);
1807 lttng_ht_destroy(ht
);
1811 * Iterate over all streams of the hashtable and free them properly.
1813 * XXX: Should not be only for metadata stream or else use an other name.
1815 static void destroy_stream_ht(struct lttng_ht
*ht
)
1817 struct lttng_ht_iter iter
;
1818 struct lttng_consumer_stream
*stream
;
1825 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1827 * Ignore return value since we are currently cleaning up so any error
1830 (void) consumer_del_metadata_stream(stream
, ht
);
1834 lttng_ht_destroy(ht
);
1837 void lttng_consumer_close_metadata(void)
1839 switch (consumer_data
.type
) {
1840 case LTTNG_CONSUMER_KERNEL
:
1842 * The Kernel consumer has a different metadata scheme so we don't
1843 * close anything because the stream will be closed by the session
1847 case LTTNG_CONSUMER32_UST
:
1848 case LTTNG_CONSUMER64_UST
:
1850 * Close all metadata streams. The metadata hash table is passed and
1851 * this call iterates over it by closing all wakeup fd. This is safe
1852 * because at this point we are sure that the metadata producer is
1853 * either dead or blocked.
1855 lttng_ustconsumer_close_metadata(metadata_ht
);
1858 ERR("Unknown consumer_data type");
1864 * Clean up a metadata stream and free its memory.
1866 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1867 struct lttng_ht
*ht
)
1870 struct lttng_ht_iter iter
;
1871 struct lttng_consumer_channel
*free_chan
= NULL
;
1872 struct consumer_relayd_sock_pair
*relayd
;
1876 * This call should NEVER receive regular stream. It must always be
1877 * metadata stream and this is crucial for data structure synchronization.
1879 assert(stream
->metadata_flag
);
1881 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1884 /* Means the stream was allocated but not successfully added */
1885 goto free_stream_rcu
;
1888 pthread_mutex_lock(&consumer_data
.lock
);
1889 pthread_mutex_lock(&stream
->lock
);
1891 switch (consumer_data
.type
) {
1892 case LTTNG_CONSUMER_KERNEL
:
1893 if (stream
->mmap_base
!= NULL
) {
1894 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1896 PERROR("munmap metadata stream");
1900 if (stream
->wait_fd
>= 0) {
1901 ret
= close(stream
->wait_fd
);
1903 PERROR("close kernel metadata wait_fd");
1907 case LTTNG_CONSUMER32_UST
:
1908 case LTTNG_CONSUMER64_UST
:
1909 lttng_ustconsumer_del_stream(stream
);
1912 ERR("Unknown consumer_data type");
1918 iter
.iter
.node
= &stream
->node
.node
;
1919 ret
= lttng_ht_del(ht
, &iter
);
1922 iter
.iter
.node
= &stream
->node_channel_id
.node
;
1923 ret
= lttng_ht_del(consumer_data
.stream_per_chan_id_ht
, &iter
);
1926 iter
.iter
.node
= &stream
->node_session_id
.node
;
1927 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1931 if (stream
->out_fd
>= 0) {
1932 ret
= close(stream
->out_fd
);
1938 /* Check and cleanup relayd */
1940 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1941 if (relayd
!= NULL
) {
1942 uatomic_dec(&relayd
->refcount
);
1943 assert(uatomic_read(&relayd
->refcount
) >= 0);
1945 /* Closing streams requires to lock the control socket. */
1946 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1947 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1948 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1949 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1951 DBG("Unable to close stream on the relayd. Continuing");
1953 * Continue here. There is nothing we can do for the relayd.
1954 * Chances are that the relayd has closed the socket so we just
1955 * continue cleaning up.
1959 /* Both conditions are met, we destroy the relayd. */
1960 if (uatomic_read(&relayd
->refcount
) == 0 &&
1961 uatomic_read(&relayd
->destroy_flag
)) {
1962 destroy_relayd(relayd
);
1967 /* Atomically decrement channel refcount since other threads can use it. */
1968 if (!uatomic_sub_return(&stream
->chan
->refcount
, 1)
1969 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
1970 /* Go for channel deletion! */
1971 free_chan
= stream
->chan
;
1976 * Nullify the stream reference so it is not used after deletion. The
1977 * consumer data lock MUST be acquired before being able to check for a
1978 * NULL pointer value.
1980 stream
->chan
->metadata_stream
= NULL
;
1982 pthread_mutex_unlock(&stream
->lock
);
1983 pthread_mutex_unlock(&consumer_data
.lock
);
1986 consumer_del_channel(free_chan
);
1990 call_rcu(&stream
->node
.head
, free_stream_rcu
);
1994 * Action done with the metadata stream when adding it to the consumer internal
1995 * data structures to handle it.
1997 static int add_metadata_stream(struct lttng_consumer_stream
*stream
,
1998 struct lttng_ht
*ht
)
2001 struct consumer_relayd_sock_pair
*relayd
;
2002 struct lttng_ht_iter iter
;
2003 struct lttng_ht_node_u64
*node
;
2008 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
2010 pthread_mutex_lock(&consumer_data
.lock
);
2011 pthread_mutex_lock(&stream
->lock
);
2014 * From here, refcounts are updated so be _careful_ when returning an error
2021 * Lookup the stream just to make sure it does not exist in our internal
2022 * state. This should NEVER happen.
2024 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
2025 node
= lttng_ht_iter_get_node_u64(&iter
);
2028 /* Find relayd and, if one is found, increment refcount. */
2029 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
2030 if (relayd
!= NULL
) {
2031 uatomic_inc(&relayd
->refcount
);
2035 * When nb_init_stream_left reaches 0, we don't need to trigger any action
2036 * in terms of destroying the associated channel, because the action that
2037 * causes the count to become 0 also causes a stream to be added. The
2038 * channel deletion will thus be triggered by the following removal of this
2041 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
2042 /* Increment refcount before decrementing nb_init_stream_left */
2044 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
2047 lttng_ht_add_unique_u64(ht
, &stream
->node
);
2049 lttng_ht_add_unique_u64(consumer_data
.stream_per_chan_id_ht
,
2050 &stream
->node_channel_id
);
2053 * Add stream to the stream_list_ht of the consumer data. No need to steal
2054 * the key since the HT does not use it and we allow to add redundant keys
2057 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2061 pthread_mutex_unlock(&stream
->lock
);
2062 pthread_mutex_unlock(&consumer_data
.lock
);
2067 * Delete data stream that are flagged for deletion (endpoint_status).
2069 static void validate_endpoint_status_data_stream(void)
2071 struct lttng_ht_iter iter
;
2072 struct lttng_consumer_stream
*stream
;
2074 DBG("Consumer delete flagged data stream");
2077 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2078 /* Validate delete flag of the stream */
2079 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2082 /* Delete it right now */
2083 consumer_del_stream(stream
, data_ht
);
2089 * Delete metadata stream that are flagged for deletion (endpoint_status).
2091 static void validate_endpoint_status_metadata_stream(
2092 struct lttng_poll_event
*pollset
)
2094 struct lttng_ht_iter iter
;
2095 struct lttng_consumer_stream
*stream
;
2097 DBG("Consumer delete flagged metadata stream");
2102 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2103 /* Validate delete flag of the stream */
2104 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2108 * Remove from pollset so the metadata thread can continue without
2109 * blocking on a deleted stream.
2111 lttng_poll_del(pollset
, stream
->wait_fd
);
2113 /* Delete it right now */
2114 consumer_del_metadata_stream(stream
, metadata_ht
);
2120 * Thread polls on metadata file descriptor and write them on disk or on the
2123 void *consumer_thread_metadata_poll(void *data
)
2126 uint32_t revents
, nb_fd
;
2127 struct lttng_consumer_stream
*stream
= NULL
;
2128 struct lttng_ht_iter iter
;
2129 struct lttng_ht_node_u64
*node
;
2130 struct lttng_poll_event events
;
2131 struct lttng_consumer_local_data
*ctx
= data
;
2134 rcu_register_thread();
2136 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2138 /* ENOMEM at this point. Better to bail out. */
2142 DBG("Thread metadata poll started");
2144 /* Size is set to 1 for the consumer_metadata pipe */
2145 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2147 ERR("Poll set creation failed");
2151 ret
= lttng_poll_add(&events
,
2152 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
), LPOLLIN
);
2158 DBG("Metadata main loop started");
2161 /* Only the metadata pipe is set */
2162 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2167 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2168 ret
= lttng_poll_wait(&events
, -1);
2169 DBG("Metadata event catched in thread");
2171 if (errno
== EINTR
) {
2172 ERR("Poll EINTR catched");
2180 /* From here, the event is a metadata wait fd */
2181 for (i
= 0; i
< nb_fd
; i
++) {
2182 revents
= LTTNG_POLL_GETEV(&events
, i
);
2183 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2185 /* Just don't waste time if no returned events for the fd */
2190 if (pollfd
== lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
)) {
2191 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2192 DBG("Metadata thread pipe hung up");
2194 * Remove the pipe from the poll set and continue the loop
2195 * since their might be data to consume.
2197 lttng_poll_del(&events
,
2198 lttng_pipe_get_readfd(ctx
->consumer_metadata_pipe
));
2199 lttng_pipe_read_close(ctx
->consumer_metadata_pipe
);
2201 } else if (revents
& LPOLLIN
) {
2204 pipe_len
= lttng_pipe_read(ctx
->consumer_metadata_pipe
,
2205 &stream
, sizeof(stream
));
2207 ERR("read metadata stream, ret: %ld", pipe_len
);
2209 * Continue here to handle the rest of the streams.
2214 /* A NULL stream means that the state has changed. */
2215 if (stream
== NULL
) {
2216 /* Check for deleted streams. */
2217 validate_endpoint_status_metadata_stream(&events
);
2221 DBG("Adding metadata stream %d to poll set",
2224 ret
= add_metadata_stream(stream
, metadata_ht
);
2226 ERR("Unable to add metadata stream");
2227 /* Stream was not setup properly. Continuing. */
2228 consumer_del_metadata_stream(stream
, NULL
);
2232 /* Add metadata stream to the global poll events list */
2233 lttng_poll_add(&events
, stream
->wait_fd
,
2234 LPOLLIN
| LPOLLPRI
);
2237 /* Handle other stream */
2243 uint64_t tmp_id
= (uint64_t) pollfd
;
2245 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2247 node
= lttng_ht_iter_get_node_u64(&iter
);
2250 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2253 /* Check for error event */
2254 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2255 DBG("Metadata fd %d is hup|err.", pollfd
);
2256 if (!stream
->hangup_flush_done
2257 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2258 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2259 DBG("Attempting to flush and consume the UST buffers");
2260 lttng_ustconsumer_on_stream_hangup(stream
);
2262 /* We just flushed the stream now read it. */
2264 len
= ctx
->on_buffer_ready(stream
, ctx
);
2266 * We don't check the return value here since if we get
2267 * a negative len, it means an error occured thus we
2268 * simply remove it from the poll set and free the
2274 lttng_poll_del(&events
, stream
->wait_fd
);
2276 * This call update the channel states, closes file descriptors
2277 * and securely free the stream.
2279 consumer_del_metadata_stream(stream
, metadata_ht
);
2280 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2281 /* Get the data out of the metadata file descriptor */
2282 DBG("Metadata available on fd %d", pollfd
);
2283 assert(stream
->wait_fd
== pollfd
);
2285 len
= ctx
->on_buffer_ready(stream
, ctx
);
2286 /* It's ok to have an unavailable sub-buffer */
2287 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2288 /* Clean up stream from consumer and free it. */
2289 lttng_poll_del(&events
, stream
->wait_fd
);
2290 consumer_del_metadata_stream(stream
, metadata_ht
);
2291 } else if (len
> 0) {
2292 stream
->data_read
= 1;
2296 /* Release RCU lock for the stream looked up */
2303 DBG("Metadata poll thread exiting");
2305 lttng_poll_clean(&events
);
2307 destroy_stream_ht(metadata_ht
);
2309 rcu_unregister_thread();
2314 * This thread polls the fds in the set to consume the data and write
2315 * it to tracefile if necessary.
2317 void *consumer_thread_data_poll(void *data
)
2319 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2320 struct pollfd
*pollfd
= NULL
;
2321 /* local view of the streams */
2322 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2323 /* local view of consumer_data.fds_count */
2325 struct lttng_consumer_local_data
*ctx
= data
;
2328 rcu_register_thread();
2330 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2331 if (data_ht
== NULL
) {
2332 /* ENOMEM at this point. Better to bail out. */
2336 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
2343 * the fds set has been updated, we need to update our
2344 * local array as well
2346 pthread_mutex_lock(&consumer_data
.lock
);
2347 if (consumer_data
.need_update
) {
2352 local_stream
= NULL
;
2354 /* allocate for all fds + 1 for the consumer_data_pipe */
2355 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2356 if (pollfd
== NULL
) {
2357 PERROR("pollfd malloc");
2358 pthread_mutex_unlock(&consumer_data
.lock
);
2362 /* allocate for all fds + 1 for the consumer_data_pipe */
2363 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2364 sizeof(struct lttng_consumer_stream
*));
2365 if (local_stream
== NULL
) {
2366 PERROR("local_stream malloc");
2367 pthread_mutex_unlock(&consumer_data
.lock
);
2370 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2373 ERR("Error in allocating pollfd or local_outfds");
2374 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2375 pthread_mutex_unlock(&consumer_data
.lock
);
2379 consumer_data
.need_update
= 0;
2381 pthread_mutex_unlock(&consumer_data
.lock
);
2383 /* No FDs and consumer_quit, consumer_cleanup the thread */
2384 if (nb_fd
== 0 && consumer_quit
== 1) {
2387 /* poll on the array of fds */
2389 DBG("polling on %d fd", nb_fd
+ 1);
2390 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2391 DBG("poll num_rdy : %d", num_rdy
);
2392 if (num_rdy
== -1) {
2394 * Restart interrupted system call.
2396 if (errno
== EINTR
) {
2399 PERROR("Poll error");
2400 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2402 } else if (num_rdy
== 0) {
2403 DBG("Polling thread timed out");
2408 * If the consumer_data_pipe triggered poll go directly to the
2409 * beginning of the loop to update the array. We want to prioritize
2410 * array update over low-priority reads.
2412 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2413 ssize_t pipe_readlen
;
2415 DBG("consumer_data_pipe wake up");
2416 pipe_readlen
= lttng_pipe_read(ctx
->consumer_data_pipe
,
2417 &new_stream
, sizeof(new_stream
));
2418 if (pipe_readlen
< 0) {
2419 ERR("Consumer data pipe ret %ld", pipe_readlen
);
2420 /* Continue so we can at least handle the current stream(s). */
2425 * If the stream is NULL, just ignore it. It's also possible that
2426 * the sessiond poll thread changed the consumer_quit state and is
2427 * waking us up to test it.
2429 if (new_stream
== NULL
) {
2430 validate_endpoint_status_data_stream();
2434 ret
= add_stream(new_stream
, data_ht
);
2436 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
2439 * At this point, if the add_stream fails, it is not in the
2440 * hash table thus passing the NULL value here.
2442 consumer_del_stream(new_stream
, NULL
);
2445 /* Continue to update the local streams and handle prio ones */
2449 /* Take care of high priority channels first. */
2450 for (i
= 0; i
< nb_fd
; i
++) {
2451 if (local_stream
[i
] == NULL
) {
2454 if (pollfd
[i
].revents
& POLLPRI
) {
2455 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2457 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2458 /* it's ok to have an unavailable sub-buffer */
2459 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2460 /* Clean the stream and free it. */
2461 consumer_del_stream(local_stream
[i
], data_ht
);
2462 local_stream
[i
] = NULL
;
2463 } else if (len
> 0) {
2464 local_stream
[i
]->data_read
= 1;
2470 * If we read high prio channel in this loop, try again
2471 * for more high prio data.
2477 /* Take care of low priority channels. */
2478 for (i
= 0; i
< nb_fd
; i
++) {
2479 if (local_stream
[i
] == NULL
) {
2482 if ((pollfd
[i
].revents
& POLLIN
) ||
2483 local_stream
[i
]->hangup_flush_done
) {
2484 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2485 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2486 /* it's ok to have an unavailable sub-buffer */
2487 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2488 /* Clean the stream and free it. */
2489 consumer_del_stream(local_stream
[i
], data_ht
);
2490 local_stream
[i
] = NULL
;
2491 } else if (len
> 0) {
2492 local_stream
[i
]->data_read
= 1;
2497 /* Handle hangup and errors */
2498 for (i
= 0; i
< nb_fd
; i
++) {
2499 if (local_stream
[i
] == NULL
) {
2502 if (!local_stream
[i
]->hangup_flush_done
2503 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2504 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2505 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2506 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2508 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2509 /* Attempt read again, for the data we just flushed. */
2510 local_stream
[i
]->data_read
= 1;
2513 * If the poll flag is HUP/ERR/NVAL and we have
2514 * read no data in this pass, we can remove the
2515 * stream from its hash table.
2517 if ((pollfd
[i
].revents
& POLLHUP
)) {
2518 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2519 if (!local_stream
[i
]->data_read
) {
2520 consumer_del_stream(local_stream
[i
], data_ht
);
2521 local_stream
[i
] = NULL
;
2524 } else if (pollfd
[i
].revents
& POLLERR
) {
2525 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2526 if (!local_stream
[i
]->data_read
) {
2527 consumer_del_stream(local_stream
[i
], data_ht
);
2528 local_stream
[i
] = NULL
;
2531 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2532 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2533 if (!local_stream
[i
]->data_read
) {
2534 consumer_del_stream(local_stream
[i
], data_ht
);
2535 local_stream
[i
] = NULL
;
2539 if (local_stream
[i
] != NULL
) {
2540 local_stream
[i
]->data_read
= 0;
2545 DBG("polling thread exiting");
2550 * Close the write side of the pipe so epoll_wait() in
2551 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2552 * read side of the pipe. If we close them both, epoll_wait strangely does
2553 * not return and could create a endless wait period if the pipe is the
2554 * only tracked fd in the poll set. The thread will take care of closing
2557 (void) lttng_pipe_write_close(ctx
->consumer_metadata_pipe
);
2559 destroy_data_stream_ht(data_ht
);
2561 rcu_unregister_thread();
2566 * Close wake-up end of each stream belonging to the channel. This will
2567 * allow the poll() on the stream read-side to detect when the
2568 * write-side (application) finally closes them.
2571 void consumer_close_channel_streams(struct lttng_consumer_channel
*channel
)
2573 struct lttng_ht
*ht
;
2574 struct lttng_consumer_stream
*stream
;
2575 struct lttng_ht_iter iter
;
2577 ht
= consumer_data
.stream_per_chan_id_ht
;
2580 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2581 ht
->hash_fct(&channel
->key
, lttng_ht_seed
),
2582 ht
->match_fct
, &channel
->key
,
2583 &iter
.iter
, stream
, node_channel_id
.node
) {
2585 * Protect against teardown with mutex.
2587 pthread_mutex_lock(&stream
->lock
);
2588 if (cds_lfht_is_node_deleted(&stream
->node
.node
)) {
2591 switch (consumer_data
.type
) {
2592 case LTTNG_CONSUMER_KERNEL
:
2594 case LTTNG_CONSUMER32_UST
:
2595 case LTTNG_CONSUMER64_UST
:
2597 * Note: a mutex is taken internally within
2598 * liblttng-ust-ctl to protect timer wakeup_fd
2599 * use from concurrent close.
2601 lttng_ustconsumer_close_stream_wakeup(stream
);
2604 ERR("Unknown consumer_data type");
2608 pthread_mutex_unlock(&stream
->lock
);
2613 static void destroy_channel_ht(struct lttng_ht
*ht
)
2615 struct lttng_ht_iter iter
;
2616 struct lttng_consumer_channel
*channel
;
2624 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, channel
, wait_fd_node
.node
) {
2625 ret
= lttng_ht_del(ht
, &iter
);
2630 lttng_ht_destroy(ht
);
2634 * This thread polls the channel fds to detect when they are being
2635 * closed. It closes all related streams if the channel is detected as
2636 * closed. It is currently only used as a shim layer for UST because the
2637 * consumerd needs to keep the per-stream wakeup end of pipes open for
2640 void *consumer_thread_channel_poll(void *data
)
2643 uint32_t revents
, nb_fd
;
2644 struct lttng_consumer_channel
*chan
= NULL
;
2645 struct lttng_ht_iter iter
;
2646 struct lttng_ht_node_u64
*node
;
2647 struct lttng_poll_event events
;
2648 struct lttng_consumer_local_data
*ctx
= data
;
2649 struct lttng_ht
*channel_ht
;
2651 rcu_register_thread();
2653 channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2655 /* ENOMEM at this point. Better to bail out. */
2659 DBG("Thread channel poll started");
2661 /* Size is set to 1 for the consumer_channel pipe */
2662 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2664 ERR("Poll set creation failed");
2668 ret
= lttng_poll_add(&events
, ctx
->consumer_channel_pipe
[0], LPOLLIN
);
2674 DBG("Channel main loop started");
2677 /* Only the channel pipe is set */
2678 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2683 DBG("Channel poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2684 ret
= lttng_poll_wait(&events
, -1);
2685 DBG("Channel event catched in thread");
2687 if (errno
== EINTR
) {
2688 ERR("Poll EINTR catched");
2696 /* From here, the event is a channel wait fd */
2697 for (i
= 0; i
< nb_fd
; i
++) {
2698 revents
= LTTNG_POLL_GETEV(&events
, i
);
2699 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2701 /* Just don't waste time if no returned events for the fd */
2705 if (pollfd
== ctx
->consumer_channel_pipe
[0]) {
2706 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2707 DBG("Channel thread pipe hung up");
2709 * Remove the pipe from the poll set and continue the loop
2710 * since their might be data to consume.
2712 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2714 } else if (revents
& LPOLLIN
) {
2715 enum consumer_channel_action action
;
2718 ret
= read_channel_pipe(ctx
, &chan
, &key
, &action
);
2720 ERR("Error reading channel pipe");
2725 case CONSUMER_CHANNEL_ADD
:
2726 DBG("Adding channel %d to poll set",
2729 lttng_ht_node_init_u64(&chan
->wait_fd_node
,
2732 lttng_ht_add_unique_u64(channel_ht
,
2733 &chan
->wait_fd_node
);
2735 /* Add channel to the global poll events list */
2736 lttng_poll_add(&events
, chan
->wait_fd
,
2737 LPOLLIN
| LPOLLPRI
);
2739 case CONSUMER_CHANNEL_DEL
:
2741 struct lttng_consumer_stream
*stream
, *stmp
;
2744 chan
= consumer_find_channel(key
);
2747 ERR("UST consumer get channel key %" PRIu64
" not found for del channel", key
);
2750 lttng_poll_del(&events
, chan
->wait_fd
);
2751 iter
.iter
.node
= &chan
->wait_fd_node
.node
;
2752 ret
= lttng_ht_del(channel_ht
, &iter
);
2754 consumer_close_channel_streams(chan
);
2756 switch (consumer_data
.type
) {
2757 case LTTNG_CONSUMER_KERNEL
:
2759 case LTTNG_CONSUMER32_UST
:
2760 case LTTNG_CONSUMER64_UST
:
2761 /* Delete streams that might have been left in the stream list. */
2762 cds_list_for_each_entry_safe(stream
, stmp
, &chan
->streams
.head
,
2764 cds_list_del(&stream
->send_node
);
2765 lttng_ustconsumer_del_stream(stream
);
2766 uatomic_sub(&stream
->chan
->refcount
, 1);
2767 assert(&chan
->refcount
);
2772 ERR("Unknown consumer_data type");
2777 * Release our own refcount. Force channel deletion even if
2778 * streams were not initialized.
2780 if (!uatomic_sub_return(&chan
->refcount
, 1)) {
2781 consumer_del_channel(chan
);
2786 case CONSUMER_CHANNEL_QUIT
:
2788 * Remove the pipe from the poll set and continue the loop
2789 * since their might be data to consume.
2791 lttng_poll_del(&events
, ctx
->consumer_channel_pipe
[0]);
2794 ERR("Unknown action");
2799 /* Handle other stream */
2805 uint64_t tmp_id
= (uint64_t) pollfd
;
2807 lttng_ht_lookup(channel_ht
, &tmp_id
, &iter
);
2809 node
= lttng_ht_iter_get_node_u64(&iter
);
2812 chan
= caa_container_of(node
, struct lttng_consumer_channel
,
2815 /* Check for error event */
2816 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2817 DBG("Channel fd %d is hup|err.", pollfd
);
2819 lttng_poll_del(&events
, chan
->wait_fd
);
2820 ret
= lttng_ht_del(channel_ht
, &iter
);
2822 assert(cds_list_empty(&chan
->streams
.head
));
2823 consumer_close_channel_streams(chan
);
2825 /* Release our own refcount */
2826 if (!uatomic_sub_return(&chan
->refcount
, 1)
2827 && !uatomic_read(&chan
->nb_init_stream_left
)) {
2828 consumer_del_channel(chan
);
2832 /* Release RCU lock for the channel looked up */
2838 lttng_poll_clean(&events
);
2840 destroy_channel_ht(channel_ht
);
2842 DBG("Channel poll thread exiting");
2843 rcu_unregister_thread();
2847 static int set_metadata_socket(struct lttng_consumer_local_data
*ctx
,
2848 struct pollfd
*sockpoll
, int client_socket
)
2855 if (lttng_consumer_poll_socket(sockpoll
) < 0) {
2859 DBG("Metadata connection on client_socket");
2861 /* Blocking call, waiting for transmission */
2862 ctx
->consumer_metadata_socket
= lttcomm_accept_unix_sock(client_socket
);
2863 if (ctx
->consumer_metadata_socket
< 0) {
2864 WARN("On accept metadata");
2875 * This thread listens on the consumerd socket and receives the file
2876 * descriptors from the session daemon.
2878 void *consumer_thread_sessiond_poll(void *data
)
2880 int sock
= -1, client_socket
, ret
;
2882 * structure to poll for incoming data on communication socket avoids
2883 * making blocking sockets.
2885 struct pollfd consumer_sockpoll
[2];
2886 struct lttng_consumer_local_data
*ctx
= data
;
2888 rcu_register_thread();
2890 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2891 unlink(ctx
->consumer_command_sock_path
);
2892 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2893 if (client_socket
< 0) {
2894 ERR("Cannot create command socket");
2898 ret
= lttcomm_listen_unix_sock(client_socket
);
2903 DBG("Sending ready command to lttng-sessiond");
2904 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2905 /* return < 0 on error, but == 0 is not fatal */
2907 ERR("Error sending ready command to lttng-sessiond");
2911 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2912 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2913 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2914 consumer_sockpoll
[1].fd
= client_socket
;
2915 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2917 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2920 DBG("Connection on client_socket");
2922 /* Blocking call, waiting for transmission */
2923 sock
= lttcomm_accept_unix_sock(client_socket
);
2930 * Setup metadata socket which is the second socket connection on the
2931 * command unix socket.
2933 ret
= set_metadata_socket(ctx
, consumer_sockpoll
, client_socket
);
2938 /* This socket is not useful anymore. */
2939 ret
= close(client_socket
);
2941 PERROR("close client_socket");
2945 /* update the polling structure to poll on the established socket */
2946 consumer_sockpoll
[1].fd
= sock
;
2947 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2950 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2953 DBG("Incoming command on sock");
2954 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2955 if (ret
== -ENOENT
) {
2956 DBG("Received STOP command");
2961 * This could simply be a session daemon quitting. Don't output
2964 DBG("Communication interrupted on command socket");
2967 if (consumer_quit
) {
2968 DBG("consumer_thread_receive_fds received quit from signal");
2971 DBG("received command on sock");
2974 DBG("Consumer thread sessiond poll exiting");
2977 * Close metadata streams since the producer is the session daemon which
2980 * NOTE: for now, this only applies to the UST tracer.
2982 lttng_consumer_close_metadata();
2985 * when all fds have hung up, the polling thread
2991 * Notify the data poll thread to poll back again and test the
2992 * consumer_quit state that we just set so to quit gracefully.
2994 notify_thread_lttng_pipe(ctx
->consumer_data_pipe
);
2996 notify_channel_pipe(ctx
, NULL
, -1, CONSUMER_CHANNEL_QUIT
);
2998 /* Cleaning up possibly open sockets. */
3002 PERROR("close sock sessiond poll");
3005 if (client_socket
>= 0) {
3006 ret
= close(client_socket
);
3008 PERROR("close client_socket sessiond poll");
3012 rcu_unregister_thread();
3016 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
3017 struct lttng_consumer_local_data
*ctx
)
3021 pthread_mutex_lock(&stream
->lock
);
3023 switch (consumer_data
.type
) {
3024 case LTTNG_CONSUMER_KERNEL
:
3025 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
3027 case LTTNG_CONSUMER32_UST
:
3028 case LTTNG_CONSUMER64_UST
:
3029 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
3032 ERR("Unknown consumer_data type");
3038 pthread_mutex_unlock(&stream
->lock
);
3042 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
3044 switch (consumer_data
.type
) {
3045 case LTTNG_CONSUMER_KERNEL
:
3046 return lttng_kconsumer_on_recv_stream(stream
);
3047 case LTTNG_CONSUMER32_UST
:
3048 case LTTNG_CONSUMER64_UST
:
3049 return lttng_ustconsumer_on_recv_stream(stream
);
3051 ERR("Unknown consumer_data type");
3058 * Allocate and set consumer data hash tables.
3060 void lttng_consumer_init(void)
3062 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3063 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3064 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3065 consumer_data
.stream_per_chan_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
3069 * Process the ADD_RELAYD command receive by a consumer.
3071 * This will create a relayd socket pair and add it to the relayd hash table.
3072 * The caller MUST acquire a RCU read side lock before calling it.
3074 int consumer_add_relayd_socket(uint64_t net_seq_idx
, int sock_type
,
3075 struct lttng_consumer_local_data
*ctx
, int sock
,
3076 struct pollfd
*consumer_sockpoll
,
3077 struct lttcomm_relayd_sock
*relayd_sock
, uint64_t sessiond_id
)
3079 int fd
= -1, ret
= -1, relayd_created
= 0;
3080 enum lttng_error_code ret_code
= LTTNG_OK
;
3081 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3084 assert(relayd_sock
);
3086 DBG("Consumer adding relayd socket (idx: %" PRIu64
")", net_seq_idx
);
3088 /* Get relayd reference if exists. */
3089 relayd
= consumer_find_relayd(net_seq_idx
);
3090 if (relayd
== NULL
) {
3091 assert(sock_type
== LTTNG_STREAM_CONTROL
);
3092 /* Not found. Allocate one. */
3093 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
3094 if (relayd
== NULL
) {
3095 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
3098 relayd
->sessiond_session_id
= sessiond_id
;
3103 * This code path MUST continue to the consumer send status message to
3104 * we can notify the session daemon and continue our work without
3105 * killing everything.
3109 * relayd key should never be found for control socket.
3111 assert(sock_type
!= LTTNG_STREAM_CONTROL
);
3114 /* First send a status message before receiving the fds. */
3115 ret
= consumer_send_status_msg(sock
, ret_code
);
3116 if (ret
< 0 || ret_code
!= LTTNG_OK
) {
3117 /* Somehow, the session daemon is not responding anymore. */
3121 /* Poll on consumer socket. */
3122 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
3123 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
3128 /* Get relayd socket from session daemon */
3129 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
3130 if (ret
!= sizeof(fd
)) {
3131 ret_code
= LTTCOMM_CONSUMERD_ERROR_RECV_FD
;
3133 fd
= -1; /* Just in case it gets set with an invalid value. */
3136 * Failing to receive FDs might indicate a major problem such as
3137 * reaching a fd limit during the receive where the kernel returns a
3138 * MSG_CTRUNC and fails to cleanup the fd in the queue. Any case, we
3139 * don't take any chances and stop everything.
3141 * XXX: Feature request #558 will fix that and avoid this possible
3142 * issue when reaching the fd limit.
3144 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
3147 * This code path MUST continue to the consumer send status message so
3148 * we can send the error to the thread expecting a reply. The above
3149 * call will make everything stop.
3153 /* We have the fds without error. Send status back. */
3154 ret
= consumer_send_status_msg(sock
, ret_code
);
3155 if (ret
< 0 || ret_code
!= LTTNG_OK
) {
3156 /* Somehow, the session daemon is not responding anymore. */
3160 /* Copy socket information and received FD */
3161 switch (sock_type
) {
3162 case LTTNG_STREAM_CONTROL
:
3163 /* Copy received lttcomm socket */
3164 lttcomm_copy_sock(&relayd
->control_sock
.sock
, &relayd_sock
->sock
);
3165 ret
= lttcomm_create_sock(&relayd
->control_sock
.sock
);
3166 /* Handle create_sock error. */
3171 * Close the socket created internally by
3172 * lttcomm_create_sock, so we can replace it by the one
3173 * received from sessiond.
3175 if (close(relayd
->control_sock
.sock
.fd
)) {
3179 /* Assign new file descriptor */
3180 relayd
->control_sock
.sock
.fd
= fd
;
3181 fd
= -1; /* For error path */
3182 /* Assign version values. */
3183 relayd
->control_sock
.major
= relayd_sock
->major
;
3184 relayd
->control_sock
.minor
= relayd_sock
->minor
;
3187 * Create a session on the relayd and store the returned id. Lock the
3188 * control socket mutex if the relayd was NOT created before.
3190 if (!relayd_created
) {
3191 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3193 ret
= relayd_create_session(&relayd
->control_sock
,
3194 &relayd
->relayd_session_id
);
3195 if (!relayd_created
) {
3196 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3200 * Close all sockets of a relayd object. It will be freed if it was
3201 * created at the error code path or else it will be garbage
3204 (void) relayd_close(&relayd
->control_sock
);
3205 (void) relayd_close(&relayd
->data_sock
);
3210 case LTTNG_STREAM_DATA
:
3211 /* Copy received lttcomm socket */
3212 lttcomm_copy_sock(&relayd
->data_sock
.sock
, &relayd_sock
->sock
);
3213 ret
= lttcomm_create_sock(&relayd
->data_sock
.sock
);
3214 /* Handle create_sock error. */
3219 * Close the socket created internally by
3220 * lttcomm_create_sock, so we can replace it by the one
3221 * received from sessiond.
3223 if (close(relayd
->data_sock
.sock
.fd
)) {
3227 /* Assign new file descriptor */
3228 relayd
->data_sock
.sock
.fd
= fd
;
3229 fd
= -1; /* for eventual error paths */
3230 /* Assign version values. */
3231 relayd
->data_sock
.major
= relayd_sock
->major
;
3232 relayd
->data_sock
.minor
= relayd_sock
->minor
;
3235 ERR("Unknown relayd socket type (%d)", sock_type
);
3240 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
3241 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
3242 relayd
->net_seq_idx
, fd
);
3245 * Add relayd socket pair to consumer data hashtable. If object already
3246 * exists or on error, the function gracefully returns.
3254 /* Close received socket if valid. */
3257 PERROR("close received socket");
3261 if (relayd_created
) {
3269 * Try to lock the stream mutex.
3271 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
3273 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
3280 * Try to lock the stream mutex. On failure, we know that the stream is
3281 * being used else where hence there is data still being extracted.
3283 ret
= pthread_mutex_trylock(&stream
->lock
);
3285 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
3297 * Search for a relayd associated to the session id and return the reference.
3299 * A rcu read side lock MUST be acquire before calling this function and locked
3300 * until the relayd object is no longer necessary.
3302 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
3304 struct lttng_ht_iter iter
;
3305 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3307 /* Iterate over all relayd since they are indexed by net_seq_idx. */
3308 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
3311 * Check by sessiond id which is unique here where the relayd session
3312 * id might not be when having multiple relayd.
3314 if (relayd
->sessiond_session_id
== id
) {
3315 /* Found the relayd. There can be only one per id. */
3327 * Check if for a given session id there is still data needed to be extract
3330 * Return 1 if data is pending or else 0 meaning ready to be read.
3332 int consumer_data_pending(uint64_t id
)
3335 struct lttng_ht_iter iter
;
3336 struct lttng_ht
*ht
;
3337 struct lttng_consumer_stream
*stream
;
3338 struct consumer_relayd_sock_pair
*relayd
= NULL
;
3339 int (*data_pending
)(struct lttng_consumer_stream
*);
3341 DBG("Consumer data pending command on session id %" PRIu64
, id
);
3344 pthread_mutex_lock(&consumer_data
.lock
);
3346 switch (consumer_data
.type
) {
3347 case LTTNG_CONSUMER_KERNEL
:
3348 data_pending
= lttng_kconsumer_data_pending
;
3350 case LTTNG_CONSUMER32_UST
:
3351 case LTTNG_CONSUMER64_UST
:
3352 data_pending
= lttng_ustconsumer_data_pending
;
3355 ERR("Unknown consumer data type");
3359 /* Ease our life a bit */
3360 ht
= consumer_data
.stream_list_ht
;
3362 relayd
= find_relayd_by_session_id(id
);
3364 /* Send init command for data pending. */
3365 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3366 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
3367 relayd
->relayd_session_id
);
3368 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3370 /* Communication error thus the relayd so no data pending. */
3371 goto data_not_pending
;
3375 cds_lfht_for_each_entry_duplicate(ht
->ht
,
3376 ht
->hash_fct(&id
, lttng_ht_seed
),
3378 &iter
.iter
, stream
, node_session_id
.node
) {
3379 /* If this call fails, the stream is being used hence data pending. */
3380 ret
= stream_try_lock(stream
);
3386 * A removed node from the hash table indicates that the stream has
3387 * been deleted thus having a guarantee that the buffers are closed
3388 * on the consumer side. However, data can still be transmitted
3389 * over the network so don't skip the relayd check.
3391 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
3393 /* Check the stream if there is data in the buffers. */
3394 ret
= data_pending(stream
);
3396 pthread_mutex_unlock(&stream
->lock
);
3403 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3404 if (stream
->metadata_flag
) {
3405 ret
= relayd_quiescent_control(&relayd
->control_sock
,
3406 stream
->relayd_stream_id
);
3408 ret
= relayd_data_pending(&relayd
->control_sock
,
3409 stream
->relayd_stream_id
,
3410 stream
->next_net_seq_num
- 1);
3412 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3414 pthread_mutex_unlock(&stream
->lock
);
3418 pthread_mutex_unlock(&stream
->lock
);
3422 unsigned int is_data_inflight
= 0;
3424 /* Send init command for data pending. */
3425 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
3426 ret
= relayd_end_data_pending(&relayd
->control_sock
,
3427 relayd
->relayd_session_id
, &is_data_inflight
);
3428 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
3430 goto data_not_pending
;
3432 if (is_data_inflight
) {
3438 * Finding _no_ node in the hash table and no inflight data means that the
3439 * stream(s) have been removed thus data is guaranteed to be available for
3440 * analysis from the trace files.
3444 /* Data is available to be read by a viewer. */
3445 pthread_mutex_unlock(&consumer_data
.lock
);
3450 /* Data is still being extracted from buffers. */
3451 pthread_mutex_unlock(&consumer_data
.lock
);
3457 * Send a ret code status message to the sessiond daemon.
3459 * Return the sendmsg() return value.
3461 int consumer_send_status_msg(int sock
, int ret_code
)
3463 struct lttcomm_consumer_status_msg msg
;
3465 msg
.ret_code
= ret_code
;
3467 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3471 * Send a channel status message to the sessiond daemon.
3473 * Return the sendmsg() return value.
3475 int consumer_send_status_channel(int sock
,
3476 struct lttng_consumer_channel
*channel
)
3478 struct lttcomm_consumer_status_channel msg
;
3483 msg
.ret_code
= -LTTNG_ERR_UST_CHAN_FAIL
;
3485 msg
.ret_code
= LTTNG_OK
;
3486 msg
.key
= channel
->key
;
3487 msg
.stream_count
= channel
->streams
.count
;
3490 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));