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>
32 #include <common/common.h>
33 #include <common/utils.h>
34 #include <common/compat/poll.h>
35 #include <common/kernel-ctl/kernel-ctl.h>
36 #include <common/sessiond-comm/relayd.h>
37 #include <common/sessiond-comm/sessiond-comm.h>
38 #include <common/kernel-consumer/kernel-consumer.h>
39 #include <common/relayd/relayd.h>
40 #include <common/ust-consumer/ust-consumer.h>
44 struct lttng_consumer_global_data consumer_data
= {
47 .type
= LTTNG_CONSUMER_UNKNOWN
,
51 * Flag to inform the polling thread to quit when all fd hung up. Updated by
52 * the consumer_thread_receive_fds when it notices that all fds has hung up.
53 * Also updated by the signal handler (consumer_should_exit()). Read by the
56 volatile int consumer_quit
;
59 * The following two hash tables are visible by all threads which are separated
60 * in different source files.
62 * Global hash table containing respectively metadata and data streams. The
63 * stream element in this ht should only be updated by the metadata poll thread
64 * for the metadata and the data poll thread for the data.
66 struct lttng_ht
*metadata_ht
;
67 struct lttng_ht
*data_ht
;
70 * Notify a thread pipe to poll back again. This usually means that some global
71 * state has changed so we just send back the thread in a poll wait call.
73 static void notify_thread_pipe(int wpipe
)
78 struct lttng_consumer_stream
*null_stream
= NULL
;
80 ret
= write(wpipe
, &null_stream
, sizeof(null_stream
));
81 } while (ret
< 0 && errno
== EINTR
);
85 * Find a stream. The consumer_data.lock must be locked during this
88 static struct lttng_consumer_stream
*consumer_find_stream(int key
,
91 struct lttng_ht_iter iter
;
92 struct lttng_ht_node_ulong
*node
;
93 struct lttng_consumer_stream
*stream
= NULL
;
97 /* Negative keys are lookup failures */
104 lttng_ht_lookup(ht
, (void *)((unsigned long) key
), &iter
);
105 node
= lttng_ht_iter_get_node_ulong(&iter
);
107 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
115 void consumer_steal_stream_key(int key
, struct lttng_ht
*ht
)
117 struct lttng_consumer_stream
*stream
;
120 stream
= consumer_find_stream(key
, ht
);
124 * We don't want the lookup to match, but we still need
125 * to iterate on this stream when iterating over the hash table. Just
126 * change the node key.
128 stream
->node
.key
= -1;
133 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
135 struct lttng_ht_iter iter
;
136 struct lttng_ht_node_ulong
*node
;
137 struct lttng_consumer_channel
*channel
= NULL
;
139 /* Negative keys are lookup failures */
146 lttng_ht_lookup(consumer_data
.channel_ht
, (void *)((unsigned long) key
),
148 node
= lttng_ht_iter_get_node_ulong(&iter
);
150 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
158 static void consumer_steal_channel_key(int key
)
160 struct lttng_consumer_channel
*channel
;
163 channel
= consumer_find_channel(key
);
167 * We don't want the lookup to match, but we still need
168 * to iterate on this channel when iterating over the hash table. Just
169 * change the node key.
171 channel
->node
.key
= -1;
177 void consumer_free_stream(struct rcu_head
*head
)
179 struct lttng_ht_node_ulong
*node
=
180 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
181 struct lttng_consumer_stream
*stream
=
182 caa_container_of(node
, struct lttng_consumer_stream
, node
);
188 * RCU protected relayd socket pair free.
190 static void consumer_rcu_free_relayd(struct rcu_head
*head
)
192 struct lttng_ht_node_ulong
*node
=
193 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
194 struct consumer_relayd_sock_pair
*relayd
=
195 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
198 * Close all sockets. This is done in the call RCU since we don't want the
199 * socket fds to be reassigned thus potentially creating bad state of the
202 * We do not have to lock the control socket mutex here since at this stage
203 * there is no one referencing to this relayd object.
205 (void) relayd_close(&relayd
->control_sock
);
206 (void) relayd_close(&relayd
->data_sock
);
212 * Destroy and free relayd socket pair object.
214 * This function MUST be called with the consumer_data lock acquired.
216 static void destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
219 struct lttng_ht_iter iter
;
221 if (relayd
== NULL
) {
225 DBG("Consumer destroy and close relayd socket pair");
227 iter
.iter
.node
= &relayd
->node
.node
;
228 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
230 /* We assume the relayd is being or is destroyed */
234 /* RCU free() call */
235 call_rcu(&relayd
->node
.head
, consumer_rcu_free_relayd
);
239 * Update the end point status of all streams having the given network sequence
240 * index (relayd index).
242 * It's atomically set without having the stream mutex locked which is fine
243 * because we handle the write/read race with a pipe wakeup for each thread.
245 static void update_endpoint_status_by_netidx(int net_seq_idx
,
246 enum consumer_endpoint_status status
)
248 struct lttng_ht_iter iter
;
249 struct lttng_consumer_stream
*stream
;
251 DBG("Consumer set delete flag on stream by idx %d", net_seq_idx
);
255 /* Let's begin with metadata */
256 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
257 if (stream
->net_seq_idx
== net_seq_idx
) {
258 uatomic_set(&stream
->endpoint_status
, status
);
259 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
263 /* Follow up by the data streams */
264 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
265 if (stream
->net_seq_idx
== net_seq_idx
) {
266 uatomic_set(&stream
->endpoint_status
, status
);
267 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
274 * Cleanup a relayd object by flagging every associated streams for deletion,
275 * destroying the object meaning removing it from the relayd hash table,
276 * closing the sockets and freeing the memory in a RCU call.
278 * If a local data context is available, notify the threads that the streams'
279 * state have changed.
281 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
282 struct lttng_consumer_local_data
*ctx
)
288 DBG("Cleaning up relayd sockets");
290 /* Save the net sequence index before destroying the object */
291 netidx
= relayd
->net_seq_idx
;
294 * Delete the relayd from the relayd hash table, close the sockets and free
295 * the object in a RCU call.
297 destroy_relayd(relayd
);
299 /* Set inactive endpoint to all streams */
300 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
303 * With a local data context, notify the threads that the streams' state
304 * have changed. The write() action on the pipe acts as an "implicit"
305 * memory barrier ordering the updates of the end point status from the
306 * read of this status which happens AFTER receiving this notify.
309 notify_thread_pipe(ctx
->consumer_data_pipe
[1]);
310 notify_thread_pipe(ctx
->consumer_metadata_pipe
[1]);
315 * Flag a relayd socket pair for destruction. Destroy it if the refcount
318 * RCU read side lock MUST be aquired before calling this function.
320 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
324 /* Set destroy flag for this object */
325 uatomic_set(&relayd
->destroy_flag
, 1);
327 /* Destroy the relayd if refcount is 0 */
328 if (uatomic_read(&relayd
->refcount
) == 0) {
329 destroy_relayd(relayd
);
334 * Remove a stream from the global list protected by a mutex. This
335 * function is also responsible for freeing its data structures.
337 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
341 struct lttng_ht_iter iter
;
342 struct lttng_consumer_channel
*free_chan
= NULL
;
343 struct consumer_relayd_sock_pair
*relayd
;
347 DBG("Consumer del stream %d", stream
->wait_fd
);
350 /* Means the stream was allocated but not successfully added */
354 pthread_mutex_lock(&stream
->lock
);
355 pthread_mutex_lock(&consumer_data
.lock
);
357 switch (consumer_data
.type
) {
358 case LTTNG_CONSUMER_KERNEL
:
359 if (stream
->mmap_base
!= NULL
) {
360 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
366 case LTTNG_CONSUMER32_UST
:
367 case LTTNG_CONSUMER64_UST
:
368 lttng_ustconsumer_del_stream(stream
);
371 ERR("Unknown consumer_data type");
377 iter
.iter
.node
= &stream
->node
.node
;
378 ret
= lttng_ht_del(ht
, &iter
);
381 /* Remove node session id from the consumer_data stream ht */
382 iter
.iter
.node
= &stream
->node_session_id
.node
;
383 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
387 assert(consumer_data
.stream_count
> 0);
388 consumer_data
.stream_count
--;
390 if (stream
->out_fd
>= 0) {
391 ret
= close(stream
->out_fd
);
396 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
397 ret
= close(stream
->wait_fd
);
402 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
403 ret
= close(stream
->shm_fd
);
409 /* Check and cleanup relayd */
411 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
412 if (relayd
!= NULL
) {
413 uatomic_dec(&relayd
->refcount
);
414 assert(uatomic_read(&relayd
->refcount
) >= 0);
416 /* Closing streams requires to lock the control socket. */
417 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
418 ret
= relayd_send_close_stream(&relayd
->control_sock
,
419 stream
->relayd_stream_id
,
420 stream
->next_net_seq_num
- 1);
421 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
423 DBG("Unable to close stream on the relayd. Continuing");
425 * Continue here. There is nothing we can do for the relayd.
426 * Chances are that the relayd has closed the socket so we just
427 * continue cleaning up.
431 /* Both conditions are met, we destroy the relayd. */
432 if (uatomic_read(&relayd
->refcount
) == 0 &&
433 uatomic_read(&relayd
->destroy_flag
)) {
434 destroy_relayd(relayd
);
439 uatomic_dec(&stream
->chan
->refcount
);
440 if (!uatomic_read(&stream
->chan
->refcount
)
441 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
442 free_chan
= stream
->chan
;
446 consumer_data
.need_update
= 1;
447 pthread_mutex_unlock(&consumer_data
.lock
);
448 pthread_mutex_unlock(&stream
->lock
);
451 consumer_del_channel(free_chan
);
455 call_rcu(&stream
->node
.head
, consumer_free_stream
);
458 struct lttng_consumer_stream
*consumer_allocate_stream(
459 int channel_key
, int stream_key
,
460 int shm_fd
, int wait_fd
,
461 enum lttng_consumer_stream_state state
,
463 enum lttng_event_output output
,
464 const char *path_name
,
472 struct lttng_consumer_stream
*stream
;
474 stream
= zmalloc(sizeof(*stream
));
475 if (stream
== NULL
) {
476 PERROR("malloc struct lttng_consumer_stream");
477 *alloc_ret
= -ENOMEM
;
482 * Get stream's channel reference. Needed when adding the stream to the
485 stream
->chan
= consumer_find_channel(channel_key
);
487 *alloc_ret
= -ENOENT
;
488 ERR("Unable to find channel for stream %d", stream_key
);
492 stream
->key
= stream_key
;
493 stream
->shm_fd
= shm_fd
;
494 stream
->wait_fd
= wait_fd
;
496 stream
->out_fd_offset
= 0;
497 stream
->state
= state
;
498 stream
->mmap_len
= mmap_len
;
499 stream
->mmap_base
= NULL
;
500 stream
->output
= output
;
503 stream
->net_seq_idx
= net_index
;
504 stream
->metadata_flag
= metadata_flag
;
505 stream
->session_id
= session_id
;
506 strncpy(stream
->path_name
, path_name
, sizeof(stream
->path_name
));
507 stream
->path_name
[sizeof(stream
->path_name
) - 1] = '\0';
508 pthread_mutex_init(&stream
->lock
, NULL
);
511 * Index differently the metadata node because the thread is using an
512 * internal hash table to match streams in the metadata_ht to the epoll set
516 lttng_ht_node_init_ulong(&stream
->node
, stream
->wait_fd
);
518 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
521 /* Init session id node with the stream session id */
522 lttng_ht_node_init_ulong(&stream
->node_session_id
, stream
->session_id
);
525 * The cpu number is needed before using any ustctl_* actions. Ignored for
526 * the kernel so the value does not matter.
528 pthread_mutex_lock(&consumer_data
.lock
);
529 stream
->cpu
= stream
->chan
->cpucount
++;
530 pthread_mutex_unlock(&consumer_data
.lock
);
532 DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu,"
533 " out_fd %d, net_seq_idx %d, session_id %" PRIu64
,
534 stream
->path_name
, stream
->key
, stream
->shm_fd
, stream
->wait_fd
,
535 (unsigned long long) stream
->mmap_len
, stream
->out_fd
,
536 stream
->net_seq_idx
, stream
->session_id
);
546 * Add a stream to the global list protected by a mutex.
548 static int consumer_add_stream(struct lttng_consumer_stream
*stream
,
552 struct consumer_relayd_sock_pair
*relayd
;
557 DBG3("Adding consumer stream %d", stream
->key
);
559 pthread_mutex_lock(&consumer_data
.lock
);
562 /* Steal stream identifier to avoid having streams with the same key */
563 consumer_steal_stream_key(stream
->key
, ht
);
565 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
568 * Add stream to the stream_list_ht of the consumer data. No need to steal
569 * the key since the HT does not use it and we allow to add redundant keys
572 lttng_ht_add_ulong(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
574 /* Check and cleanup relayd */
575 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
576 if (relayd
!= NULL
) {
577 uatomic_inc(&relayd
->refcount
);
580 /* Update channel refcount once added without error(s). */
581 uatomic_inc(&stream
->chan
->refcount
);
584 * When nb_init_streams reaches 0, we don't need to trigger any action in
585 * terms of destroying the associated channel, because the action that
586 * causes the count to become 0 also causes a stream to be added. The
587 * channel deletion will thus be triggered by the following removal of this
590 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
591 uatomic_dec(&stream
->chan
->nb_init_streams
);
594 /* Update consumer data once the node is inserted. */
595 consumer_data
.stream_count
++;
596 consumer_data
.need_update
= 1;
599 pthread_mutex_unlock(&consumer_data
.lock
);
605 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
606 * be acquired before calling this.
608 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
611 struct lttng_ht_node_ulong
*node
;
612 struct lttng_ht_iter iter
;
614 if (relayd
== NULL
) {
619 lttng_ht_lookup(consumer_data
.relayd_ht
,
620 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
621 node
= lttng_ht_iter_get_node_ulong(&iter
);
623 /* Relayd already exist. Ignore the insertion */
626 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
633 * Allocate and return a consumer relayd socket.
635 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
638 struct consumer_relayd_sock_pair
*obj
= NULL
;
640 /* Negative net sequence index is a failure */
641 if (net_seq_idx
< 0) {
645 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
647 PERROR("zmalloc relayd sock");
651 obj
->net_seq_idx
= net_seq_idx
;
653 obj
->destroy_flag
= 0;
654 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
655 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
662 * Find a relayd socket pair in the global consumer data.
664 * Return the object if found else NULL.
665 * RCU read-side lock must be held across this call and while using the
668 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
670 struct lttng_ht_iter iter
;
671 struct lttng_ht_node_ulong
*node
;
672 struct consumer_relayd_sock_pair
*relayd
= NULL
;
674 /* Negative keys are lookup failures */
679 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
681 node
= lttng_ht_iter_get_node_ulong(&iter
);
683 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
691 * Handle stream for relayd transmission if the stream applies for network
692 * streaming where the net sequence index is set.
694 * Return destination file descriptor or negative value on error.
696 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
697 size_t data_size
, unsigned long padding
,
698 struct consumer_relayd_sock_pair
*relayd
)
701 struct lttcomm_relayd_data_hdr data_hdr
;
707 /* Reset data header */
708 memset(&data_hdr
, 0, sizeof(data_hdr
));
710 if (stream
->metadata_flag
) {
711 /* Caller MUST acquire the relayd control socket lock */
712 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
717 /* Metadata are always sent on the control socket. */
718 outfd
= relayd
->control_sock
.fd
;
720 /* Set header with stream information */
721 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
722 data_hdr
.data_size
= htobe32(data_size
);
723 data_hdr
.padding_size
= htobe32(padding
);
724 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
725 /* Other fields are zeroed previously */
727 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
733 /* Set to go on data socket */
734 outfd
= relayd
->data_sock
.fd
;
742 void consumer_free_channel(struct rcu_head
*head
)
744 struct lttng_ht_node_ulong
*node
=
745 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
746 struct lttng_consumer_channel
*channel
=
747 caa_container_of(node
, struct lttng_consumer_channel
, node
);
753 * Remove a channel from the global list protected by a mutex. This
754 * function is also responsible for freeing its data structures.
756 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
759 struct lttng_ht_iter iter
;
761 pthread_mutex_lock(&consumer_data
.lock
);
763 switch (consumer_data
.type
) {
764 case LTTNG_CONSUMER_KERNEL
:
766 case LTTNG_CONSUMER32_UST
:
767 case LTTNG_CONSUMER64_UST
:
768 lttng_ustconsumer_del_channel(channel
);
771 ERR("Unknown consumer_data type");
777 iter
.iter
.node
= &channel
->node
.node
;
778 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
782 if (channel
->mmap_base
!= NULL
) {
783 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
788 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
789 ret
= close(channel
->wait_fd
);
794 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
795 ret
= close(channel
->shm_fd
);
801 call_rcu(&channel
->node
.head
, consumer_free_channel
);
803 pthread_mutex_unlock(&consumer_data
.lock
);
806 struct lttng_consumer_channel
*consumer_allocate_channel(
808 int shm_fd
, int wait_fd
,
810 uint64_t max_sb_size
,
811 unsigned int nb_init_streams
)
813 struct lttng_consumer_channel
*channel
;
816 channel
= zmalloc(sizeof(*channel
));
817 if (channel
== NULL
) {
818 PERROR("malloc struct lttng_consumer_channel");
821 channel
->key
= channel_key
;
822 channel
->shm_fd
= shm_fd
;
823 channel
->wait_fd
= wait_fd
;
824 channel
->mmap_len
= mmap_len
;
825 channel
->max_sb_size
= max_sb_size
;
826 channel
->refcount
= 0;
827 channel
->nb_init_streams
= nb_init_streams
;
828 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
830 switch (consumer_data
.type
) {
831 case LTTNG_CONSUMER_KERNEL
:
832 channel
->mmap_base
= NULL
;
833 channel
->mmap_len
= 0;
835 case LTTNG_CONSUMER32_UST
:
836 case LTTNG_CONSUMER64_UST
:
837 ret
= lttng_ustconsumer_allocate_channel(channel
);
844 ERR("Unknown consumer_data type");
848 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
849 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
850 (unsigned long long) channel
->mmap_len
,
851 (unsigned long long) channel
->max_sb_size
);
857 * Add a channel to the global list protected by a mutex.
859 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
861 struct lttng_ht_node_ulong
*node
;
862 struct lttng_ht_iter iter
;
864 pthread_mutex_lock(&consumer_data
.lock
);
865 /* Steal channel identifier, for UST */
866 consumer_steal_channel_key(channel
->key
);
869 lttng_ht_lookup(consumer_data
.channel_ht
,
870 (void *)((unsigned long) channel
->key
), &iter
);
871 node
= lttng_ht_iter_get_node_ulong(&iter
);
873 /* Channel already exist. Ignore the insertion */
877 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
881 pthread_mutex_unlock(&consumer_data
.lock
);
887 * Allocate the pollfd structure and the local view of the out fds to avoid
888 * doing a lookup in the linked list and concurrency issues when writing is
889 * needed. Called with consumer_data.lock held.
891 * Returns the number of fds in the structures.
893 static int consumer_update_poll_array(
894 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
895 struct lttng_consumer_stream
**local_stream
, struct lttng_ht
*ht
)
898 struct lttng_ht_iter iter
;
899 struct lttng_consumer_stream
*stream
;
901 DBG("Updating poll fd array");
903 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
905 * Only active streams with an active end point can be added to the
906 * poll set and local stream storage of the thread.
908 * There is a potential race here for endpoint_status to be updated
909 * just after the check. However, this is OK since the stream(s) will
910 * be deleted once the thread is notified that the end point state has
911 * changed where this function will be called back again.
913 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
914 stream
->endpoint_status
) {
917 DBG("Active FD %d", stream
->wait_fd
);
918 (*pollfd
)[i
].fd
= stream
->wait_fd
;
919 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
920 local_stream
[i
] = stream
;
926 * Insert the consumer_data_pipe at the end of the array and don't
927 * increment i so nb_fd is the number of real FD.
929 (*pollfd
)[i
].fd
= ctx
->consumer_data_pipe
[0];
930 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
935 * Poll on the should_quit pipe and the command socket return -1 on error and
936 * should exit, 0 if data is available on the command socket
938 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
943 num_rdy
= poll(consumer_sockpoll
, 2, -1);
946 * Restart interrupted system call.
948 if (errno
== EINTR
) {
951 PERROR("Poll error");
954 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
955 DBG("consumer_should_quit wake up");
965 * Set the error socket.
967 void lttng_consumer_set_error_sock(
968 struct lttng_consumer_local_data
*ctx
, int sock
)
970 ctx
->consumer_error_socket
= sock
;
974 * Set the command socket path.
976 void lttng_consumer_set_command_sock_path(
977 struct lttng_consumer_local_data
*ctx
, char *sock
)
979 ctx
->consumer_command_sock_path
= sock
;
983 * Send return code to the session daemon.
984 * If the socket is not defined, we return 0, it is not a fatal error
986 int lttng_consumer_send_error(
987 struct lttng_consumer_local_data
*ctx
, int cmd
)
989 if (ctx
->consumer_error_socket
> 0) {
990 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
991 sizeof(enum lttcomm_sessiond_command
));
998 * Close all the tracefiles and stream fds, should be called when all instances
1001 void lttng_consumer_cleanup(void)
1003 struct lttng_ht_iter iter
;
1004 struct lttng_ht_node_ulong
*node
;
1008 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
1010 struct lttng_consumer_channel
*channel
=
1011 caa_container_of(node
, struct lttng_consumer_channel
, node
);
1012 consumer_del_channel(channel
);
1017 lttng_ht_destroy(consumer_data
.channel_ht
);
1021 * Called from signal handler.
1023 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1028 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1029 } while (ret
< 0 && errno
== EINTR
);
1031 PERROR("write consumer quit");
1034 DBG("Consumer flag that it should quit");
1037 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1040 int outfd
= stream
->out_fd
;
1043 * This does a blocking write-and-wait on any page that belongs to the
1044 * subbuffer prior to the one we just wrote.
1045 * Don't care about error values, as these are just hints and ways to
1046 * limit the amount of page cache used.
1048 if (orig_offset
< stream
->chan
->max_sb_size
) {
1051 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
1052 stream
->chan
->max_sb_size
,
1053 SYNC_FILE_RANGE_WAIT_BEFORE
1054 | SYNC_FILE_RANGE_WRITE
1055 | SYNC_FILE_RANGE_WAIT_AFTER
);
1057 * Give hints to the kernel about how we access the file:
1058 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1061 * We need to call fadvise again after the file grows because the
1062 * kernel does not seem to apply fadvise to non-existing parts of the
1065 * Call fadvise _after_ having waited for the page writeback to
1066 * complete because the dirty page writeback semantic is not well
1067 * defined. So it can be expected to lead to lower throughput in
1070 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
1071 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
1075 * Initialise the necessary environnement :
1076 * - create a new context
1077 * - create the poll_pipe
1078 * - create the should_quit pipe (for signal handler)
1079 * - create the thread pipe (for splice)
1081 * Takes a function pointer as argument, this function is called when data is
1082 * available on a buffer. This function is responsible to do the
1083 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1084 * buffer configuration and then kernctl_put_next_subbuf at the end.
1086 * Returns a pointer to the new context or NULL on error.
1088 struct lttng_consumer_local_data
*lttng_consumer_create(
1089 enum lttng_consumer_type type
,
1090 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1091 struct lttng_consumer_local_data
*ctx
),
1092 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1093 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1094 int (*update_stream
)(int stream_key
, uint32_t state
))
1097 struct lttng_consumer_local_data
*ctx
;
1099 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1100 consumer_data
.type
== type
);
1101 consumer_data
.type
= type
;
1103 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1105 PERROR("allocating context");
1109 ctx
->consumer_error_socket
= -1;
1110 /* assign the callbacks */
1111 ctx
->on_buffer_ready
= buffer_ready
;
1112 ctx
->on_recv_channel
= recv_channel
;
1113 ctx
->on_recv_stream
= recv_stream
;
1114 ctx
->on_update_stream
= update_stream
;
1116 ret
= pipe(ctx
->consumer_data_pipe
);
1118 PERROR("Error creating poll pipe");
1119 goto error_poll_pipe
;
1122 /* set read end of the pipe to non-blocking */
1123 ret
= fcntl(ctx
->consumer_data_pipe
[0], F_SETFL
, O_NONBLOCK
);
1125 PERROR("fcntl O_NONBLOCK");
1126 goto error_poll_fcntl
;
1129 /* set write end of the pipe to non-blocking */
1130 ret
= fcntl(ctx
->consumer_data_pipe
[1], F_SETFL
, O_NONBLOCK
);
1132 PERROR("fcntl O_NONBLOCK");
1133 goto error_poll_fcntl
;
1136 ret
= pipe(ctx
->consumer_should_quit
);
1138 PERROR("Error creating recv pipe");
1139 goto error_quit_pipe
;
1142 ret
= pipe(ctx
->consumer_thread_pipe
);
1144 PERROR("Error creating thread pipe");
1145 goto error_thread_pipe
;
1148 ret
= utils_create_pipe(ctx
->consumer_metadata_pipe
);
1150 goto error_metadata_pipe
;
1153 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1155 goto error_splice_pipe
;
1161 utils_close_pipe(ctx
->consumer_metadata_pipe
);
1162 error_metadata_pipe
:
1163 utils_close_pipe(ctx
->consumer_thread_pipe
);
1165 for (i
= 0; i
< 2; i
++) {
1168 err
= close(ctx
->consumer_should_quit
[i
]);
1175 for (i
= 0; i
< 2; i
++) {
1178 err
= close(ctx
->consumer_data_pipe
[i
]);
1190 * Close all fds associated with the instance and free the context.
1192 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1196 DBG("Consumer destroying it. Closing everything.");
1198 ret
= close(ctx
->consumer_error_socket
);
1202 ret
= close(ctx
->consumer_thread_pipe
[0]);
1206 ret
= close(ctx
->consumer_thread_pipe
[1]);
1210 ret
= close(ctx
->consumer_data_pipe
[0]);
1214 ret
= close(ctx
->consumer_data_pipe
[1]);
1218 ret
= close(ctx
->consumer_should_quit
[0]);
1222 ret
= close(ctx
->consumer_should_quit
[1]);
1226 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1228 unlink(ctx
->consumer_command_sock_path
);
1233 * Write the metadata stream id on the specified file descriptor.
1235 static int write_relayd_metadata_id(int fd
,
1236 struct lttng_consumer_stream
*stream
,
1237 struct consumer_relayd_sock_pair
*relayd
,
1238 unsigned long padding
)
1241 struct lttcomm_relayd_metadata_payload hdr
;
1243 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1244 hdr
.padding_size
= htobe32(padding
);
1246 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1247 } while (ret
< 0 && errno
== EINTR
);
1249 PERROR("write metadata stream id");
1252 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1253 stream
->relayd_stream_id
, padding
);
1260 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1261 * core function for writing trace buffers to either the local filesystem or
1264 * Careful review MUST be put if any changes occur!
1266 * Returns the number of bytes written
1268 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1269 struct lttng_consumer_local_data
*ctx
,
1270 struct lttng_consumer_stream
*stream
, unsigned long len
,
1271 unsigned long padding
)
1273 unsigned long mmap_offset
;
1274 ssize_t ret
= 0, written
= 0;
1275 off_t orig_offset
= stream
->out_fd_offset
;
1276 /* Default is on the disk */
1277 int outfd
= stream
->out_fd
;
1278 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1279 unsigned int relayd_hang_up
= 0;
1281 /* RCU lock for the relayd pointer */
1284 pthread_mutex_lock(&stream
->lock
);
1286 /* Flag that the current stream if set for network streaming. */
1287 if (stream
->net_seq_idx
!= -1) {
1288 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1289 if (relayd
== NULL
) {
1294 /* get the offset inside the fd to mmap */
1295 switch (consumer_data
.type
) {
1296 case LTTNG_CONSUMER_KERNEL
:
1297 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1299 case LTTNG_CONSUMER32_UST
:
1300 case LTTNG_CONSUMER64_UST
:
1301 ret
= lttng_ustctl_get_mmap_read_offset(stream
->chan
->handle
,
1302 stream
->buf
, &mmap_offset
);
1305 ERR("Unknown consumer_data type");
1310 PERROR("tracer ctl get_mmap_read_offset");
1315 /* Handle stream on the relayd if the output is on the network */
1317 unsigned long netlen
= len
;
1320 * Lock the control socket for the complete duration of the function
1321 * since from this point on we will use the socket.
1323 if (stream
->metadata_flag
) {
1324 /* Metadata requires the control socket. */
1325 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1326 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1329 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1331 /* Use the returned socket. */
1334 /* Write metadata stream id before payload */
1335 if (stream
->metadata_flag
) {
1336 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1339 /* Socket operation failed. We consider the relayd dead */
1340 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1348 /* Socket operation failed. We consider the relayd dead */
1349 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1353 /* Else, use the default set before which is the filesystem. */
1356 /* No streaming, we have to set the len with the full padding */
1362 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
1363 } while (ret
< 0 && errno
== EINTR
);
1364 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1366 PERROR("Error in file write");
1370 /* Socket operation failed. We consider the relayd dead */
1371 if (errno
== EPIPE
|| errno
== EINVAL
) {
1376 } else if (ret
> len
) {
1377 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1385 /* This call is useless on a socket so better save a syscall. */
1387 /* This won't block, but will start writeout asynchronously */
1388 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1389 SYNC_FILE_RANGE_WRITE
);
1390 stream
->out_fd_offset
+= ret
;
1394 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1398 * This is a special case that the relayd has closed its socket. Let's
1399 * cleanup the relayd object and all associated streams.
1401 if (relayd
&& relayd_hang_up
) {
1402 cleanup_relayd(relayd
, ctx
);
1406 /* Unlock only if ctrl socket used */
1407 if (relayd
&& stream
->metadata_flag
) {
1408 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1410 pthread_mutex_unlock(&stream
->lock
);
1417 * Splice the data from the ring buffer to the tracefile.
1419 * Returns the number of bytes spliced.
1421 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1422 struct lttng_consumer_local_data
*ctx
,
1423 struct lttng_consumer_stream
*stream
, unsigned long len
,
1424 unsigned long padding
)
1426 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1428 off_t orig_offset
= stream
->out_fd_offset
;
1429 int fd
= stream
->wait_fd
;
1430 /* Default is on the disk */
1431 int outfd
= stream
->out_fd
;
1432 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1434 unsigned int relayd_hang_up
= 0;
1436 switch (consumer_data
.type
) {
1437 case LTTNG_CONSUMER_KERNEL
:
1439 case LTTNG_CONSUMER32_UST
:
1440 case LTTNG_CONSUMER64_UST
:
1441 /* Not supported for user space tracing */
1444 ERR("Unknown consumer_data type");
1448 /* RCU lock for the relayd pointer */
1451 pthread_mutex_lock(&stream
->lock
);
1453 /* Flag that the current stream if set for network streaming. */
1454 if (stream
->net_seq_idx
!= -1) {
1455 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1456 if (relayd
== NULL
) {
1462 * Choose right pipe for splice. Metadata and trace data are handled by
1463 * different threads hence the use of two pipes in order not to race or
1464 * corrupt the written data.
1466 if (stream
->metadata_flag
) {
1467 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1469 splice_pipe
= ctx
->consumer_thread_pipe
;
1472 /* Write metadata stream id before payload */
1474 int total_len
= len
;
1476 if (stream
->metadata_flag
) {
1478 * Lock the control socket for the complete duration of the function
1479 * since from this point on we will use the socket.
1481 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1483 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1487 /* Socket operation failed. We consider the relayd dead */
1488 if (ret
== -EBADF
) {
1489 WARN("Remote relayd disconnected. Stopping");
1496 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1499 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1501 /* Use the returned socket. */
1504 /* Socket operation failed. We consider the relayd dead */
1505 if (ret
== -EBADF
) {
1506 WARN("Remote relayd disconnected. Stopping");
1513 /* No streaming, we have to set the len with the full padding */
1518 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1519 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1520 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1521 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1522 DBG("splice chan to pipe, ret %zd", ret_splice
);
1523 if (ret_splice
< 0) {
1524 PERROR("Error in relay splice");
1526 written
= ret_splice
;
1532 /* Handle stream on the relayd if the output is on the network */
1534 if (stream
->metadata_flag
) {
1535 size_t metadata_payload_size
=
1536 sizeof(struct lttcomm_relayd_metadata_payload
);
1538 /* Update counter to fit the spliced data */
1539 ret_splice
+= metadata_payload_size
;
1540 len
+= metadata_payload_size
;
1542 * We do this so the return value can match the len passed as
1543 * argument to this function.
1545 written
-= metadata_payload_size
;
1549 /* Splice data out */
1550 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1551 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1552 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1553 if (ret_splice
< 0) {
1554 PERROR("Error in file splice");
1556 written
= ret_splice
;
1558 /* Socket operation failed. We consider the relayd dead */
1559 if (errno
== EBADF
|| errno
== EPIPE
) {
1560 WARN("Remote relayd disconnected. Stopping");
1566 } else if (ret_splice
> len
) {
1568 PERROR("Wrote more data than requested %zd (len: %lu)",
1570 written
+= ret_splice
;
1576 /* This call is useless on a socket so better save a syscall. */
1578 /* This won't block, but will start writeout asynchronously */
1579 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1580 SYNC_FILE_RANGE_WRITE
);
1581 stream
->out_fd_offset
+= ret_splice
;
1583 written
+= ret_splice
;
1585 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1593 * This is a special case that the relayd has closed its socket. Let's
1594 * cleanup the relayd object and all associated streams.
1596 if (relayd
&& relayd_hang_up
) {
1597 cleanup_relayd(relayd
, ctx
);
1598 /* Skip splice error so the consumer does not fail */
1603 /* send the appropriate error description to sessiond */
1606 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1609 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1612 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1617 if (relayd
&& stream
->metadata_flag
) {
1618 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1620 pthread_mutex_unlock(&stream
->lock
);
1627 * Take a snapshot for a specific fd
1629 * Returns 0 on success, < 0 on error
1631 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1632 struct lttng_consumer_stream
*stream
)
1634 switch (consumer_data
.type
) {
1635 case LTTNG_CONSUMER_KERNEL
:
1636 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1637 case LTTNG_CONSUMER32_UST
:
1638 case LTTNG_CONSUMER64_UST
:
1639 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1641 ERR("Unknown consumer_data type");
1649 * Get the produced position
1651 * Returns 0 on success, < 0 on error
1653 int lttng_consumer_get_produced_snapshot(
1654 struct lttng_consumer_local_data
*ctx
,
1655 struct lttng_consumer_stream
*stream
,
1658 switch (consumer_data
.type
) {
1659 case LTTNG_CONSUMER_KERNEL
:
1660 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1661 case LTTNG_CONSUMER32_UST
:
1662 case LTTNG_CONSUMER64_UST
:
1663 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1665 ERR("Unknown consumer_data type");
1671 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1672 int sock
, struct pollfd
*consumer_sockpoll
)
1674 switch (consumer_data
.type
) {
1675 case LTTNG_CONSUMER_KERNEL
:
1676 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1677 case LTTNG_CONSUMER32_UST
:
1678 case LTTNG_CONSUMER64_UST
:
1679 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1681 ERR("Unknown consumer_data type");
1688 * Iterate over all streams of the hashtable and free them properly.
1690 * WARNING: *MUST* be used with data stream only.
1692 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1695 struct lttng_ht_iter iter
;
1696 struct lttng_consumer_stream
*stream
;
1703 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1704 ret
= lttng_ht_del(ht
, &iter
);
1707 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1711 lttng_ht_destroy(ht
);
1715 * Iterate over all streams of the hashtable and free them properly.
1717 * XXX: Should not be only for metadata stream or else use an other name.
1719 static void destroy_stream_ht(struct lttng_ht
*ht
)
1722 struct lttng_ht_iter iter
;
1723 struct lttng_consumer_stream
*stream
;
1730 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1731 ret
= lttng_ht_del(ht
, &iter
);
1734 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1738 lttng_ht_destroy(ht
);
1742 * Clean up a metadata stream and free its memory.
1744 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1745 struct lttng_ht
*ht
)
1748 struct lttng_ht_iter iter
;
1749 struct lttng_consumer_channel
*free_chan
= NULL
;
1750 struct consumer_relayd_sock_pair
*relayd
;
1754 * This call should NEVER receive regular stream. It must always be
1755 * metadata stream and this is crucial for data structure synchronization.
1757 assert(stream
->metadata_flag
);
1759 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1762 /* Means the stream was allocated but not successfully added */
1766 pthread_mutex_lock(&stream
->lock
);
1768 pthread_mutex_lock(&consumer_data
.lock
);
1769 switch (consumer_data
.type
) {
1770 case LTTNG_CONSUMER_KERNEL
:
1771 if (stream
->mmap_base
!= NULL
) {
1772 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1774 PERROR("munmap metadata stream");
1778 case LTTNG_CONSUMER32_UST
:
1779 case LTTNG_CONSUMER64_UST
:
1780 lttng_ustconsumer_del_stream(stream
);
1783 ERR("Unknown consumer_data type");
1789 iter
.iter
.node
= &stream
->node
.node
;
1790 ret
= lttng_ht_del(ht
, &iter
);
1793 /* Remove node session id from the consumer_data stream ht */
1794 iter
.iter
.node
= &stream
->node_session_id
.node
;
1795 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1799 if (stream
->out_fd
>= 0) {
1800 ret
= close(stream
->out_fd
);
1806 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
1807 ret
= close(stream
->wait_fd
);
1813 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
1814 ret
= close(stream
->shm_fd
);
1820 /* Check and cleanup relayd */
1822 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1823 if (relayd
!= NULL
) {
1824 uatomic_dec(&relayd
->refcount
);
1825 assert(uatomic_read(&relayd
->refcount
) >= 0);
1827 /* Closing streams requires to lock the control socket. */
1828 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1829 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1830 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1831 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1833 DBG("Unable to close stream on the relayd. Continuing");
1835 * Continue here. There is nothing we can do for the relayd.
1836 * Chances are that the relayd has closed the socket so we just
1837 * continue cleaning up.
1841 /* Both conditions are met, we destroy the relayd. */
1842 if (uatomic_read(&relayd
->refcount
) == 0 &&
1843 uatomic_read(&relayd
->destroy_flag
)) {
1844 destroy_relayd(relayd
);
1849 /* Atomically decrement channel refcount since other threads can use it. */
1850 uatomic_dec(&stream
->chan
->refcount
);
1851 if (!uatomic_read(&stream
->chan
->refcount
)
1852 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
1853 /* Go for channel deletion! */
1854 free_chan
= stream
->chan
;
1858 pthread_mutex_unlock(&consumer_data
.lock
);
1859 pthread_mutex_unlock(&stream
->lock
);
1862 consumer_del_channel(free_chan
);
1866 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1870 * Action done with the metadata stream when adding it to the consumer internal
1871 * data structures to handle it.
1873 static int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
,
1874 struct lttng_ht
*ht
)
1877 struct consumer_relayd_sock_pair
*relayd
;
1882 DBG3("Adding metadata stream %d to hash table", stream
->wait_fd
);
1884 pthread_mutex_lock(&consumer_data
.lock
);
1887 * From here, refcounts are updated so be _careful_ when returning an error
1892 /* Find relayd and, if one is found, increment refcount. */
1893 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1894 if (relayd
!= NULL
) {
1895 uatomic_inc(&relayd
->refcount
);
1898 /* Update channel refcount once added without error(s). */
1899 uatomic_inc(&stream
->chan
->refcount
);
1902 * When nb_init_streams reaches 0, we don't need to trigger any action in
1903 * terms of destroying the associated channel, because the action that
1904 * causes the count to become 0 also causes a stream to be added. The
1905 * channel deletion will thus be triggered by the following removal of this
1908 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
1909 uatomic_dec(&stream
->chan
->nb_init_streams
);
1912 /* Steal stream identifier to avoid having streams with the same key */
1913 consumer_steal_stream_key(stream
->key
, ht
);
1915 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
1918 * Add stream to the stream_list_ht of the consumer data. No need to steal
1919 * the key since the HT does not use it and we allow to add redundant keys
1922 lttng_ht_add_ulong(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
1926 pthread_mutex_unlock(&consumer_data
.lock
);
1931 * Delete data stream that are flagged for deletion (endpoint_status).
1933 static void validate_endpoint_status_data_stream(void)
1935 struct lttng_ht_iter iter
;
1936 struct lttng_consumer_stream
*stream
;
1938 DBG("Consumer delete flagged data stream");
1941 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1942 /* Validate delete flag of the stream */
1943 if (stream
->endpoint_status
!= CONSUMER_ENDPOINT_INACTIVE
) {
1946 /* Delete it right now */
1947 consumer_del_stream(stream
, data_ht
);
1953 * Delete metadata stream that are flagged for deletion (endpoint_status).
1955 static void validate_endpoint_status_metadata_stream(
1956 struct lttng_poll_event
*pollset
)
1958 struct lttng_ht_iter iter
;
1959 struct lttng_consumer_stream
*stream
;
1961 DBG("Consumer delete flagged metadata stream");
1966 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1967 /* Validate delete flag of the stream */
1968 if (!stream
->endpoint_status
) {
1972 * Remove from pollset so the metadata thread can continue without
1973 * blocking on a deleted stream.
1975 lttng_poll_del(pollset
, stream
->wait_fd
);
1977 /* Delete it right now */
1978 consumer_del_metadata_stream(stream
, metadata_ht
);
1984 * Thread polls on metadata file descriptor and write them on disk or on the
1987 void *consumer_thread_metadata_poll(void *data
)
1990 uint32_t revents
, nb_fd
;
1991 struct lttng_consumer_stream
*stream
= NULL
;
1992 struct lttng_ht_iter iter
;
1993 struct lttng_ht_node_ulong
*node
;
1994 struct lttng_poll_event events
;
1995 struct lttng_consumer_local_data
*ctx
= data
;
1998 rcu_register_thread();
2000 DBG("Thread metadata poll started");
2002 /* Size is set to 1 for the consumer_metadata pipe */
2003 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2005 ERR("Poll set creation failed");
2009 ret
= lttng_poll_add(&events
, ctx
->consumer_metadata_pipe
[0], LPOLLIN
);
2015 DBG("Metadata main loop started");
2018 lttng_poll_reset(&events
);
2020 nb_fd
= LTTNG_POLL_GETNB(&events
);
2022 /* Only the metadata pipe is set */
2023 if (nb_fd
== 0 && consumer_quit
== 1) {
2028 DBG("Metadata poll wait with %d fd(s)", nb_fd
);
2029 ret
= lttng_poll_wait(&events
, -1);
2030 DBG("Metadata event catched in thread");
2032 if (errno
== EINTR
) {
2033 ERR("Poll EINTR catched");
2039 /* From here, the event is a metadata wait fd */
2040 for (i
= 0; i
< nb_fd
; i
++) {
2041 revents
= LTTNG_POLL_GETEV(&events
, i
);
2042 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2044 /* Just don't waste time if no returned events for the fd */
2049 if (pollfd
== ctx
->consumer_metadata_pipe
[0]) {
2050 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2051 DBG("Metadata thread pipe hung up");
2053 * Remove the pipe from the poll set and continue the loop
2054 * since their might be data to consume.
2056 lttng_poll_del(&events
, ctx
->consumer_metadata_pipe
[0]);
2057 close(ctx
->consumer_metadata_pipe
[0]);
2059 } else if (revents
& LPOLLIN
) {
2061 /* Get the stream pointer received */
2062 ret
= read(pollfd
, &stream
, sizeof(stream
));
2063 } while (ret
< 0 && errno
== EINTR
);
2065 ret
< sizeof(struct lttng_consumer_stream
*)) {
2066 PERROR("read metadata stream");
2068 * Let's continue here and hope we can still work
2069 * without stopping the consumer. XXX: Should we?
2074 /* A NULL stream means that the state has changed. */
2075 if (stream
== NULL
) {
2076 /* Check for deleted streams. */
2077 validate_endpoint_status_metadata_stream(&events
);
2081 DBG("Adding metadata stream %d to poll set",
2084 ret
= consumer_add_metadata_stream(stream
, metadata_ht
);
2086 ERR("Unable to add metadata stream");
2087 /* Stream was not setup properly. Continuing. */
2088 consumer_del_metadata_stream(stream
, NULL
);
2092 /* Add metadata stream to the global poll events list */
2093 lttng_poll_add(&events
, stream
->wait_fd
,
2094 LPOLLIN
| LPOLLPRI
);
2097 /* Handle other stream */
2102 lttng_ht_lookup(metadata_ht
, (void *)((unsigned long) pollfd
),
2104 node
= lttng_ht_iter_get_node_ulong(&iter
);
2107 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2110 /* Check for error event */
2111 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2112 DBG("Metadata fd %d is hup|err.", pollfd
);
2113 if (!stream
->hangup_flush_done
2114 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2115 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2116 DBG("Attempting to flush and consume the UST buffers");
2117 lttng_ustconsumer_on_stream_hangup(stream
);
2119 /* We just flushed the stream now read it. */
2121 len
= ctx
->on_buffer_ready(stream
, ctx
);
2123 * We don't check the return value here since if we get
2124 * a negative len, it means an error occured thus we
2125 * simply remove it from the poll set and free the
2131 lttng_poll_del(&events
, stream
->wait_fd
);
2133 * This call update the channel states, closes file descriptors
2134 * and securely free the stream.
2136 consumer_del_metadata_stream(stream
, metadata_ht
);
2137 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2138 /* Get the data out of the metadata file descriptor */
2139 DBG("Metadata available on fd %d", pollfd
);
2140 assert(stream
->wait_fd
== pollfd
);
2142 len
= ctx
->on_buffer_ready(stream
, ctx
);
2143 /* It's ok to have an unavailable sub-buffer */
2144 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2145 /* Clean up stream from consumer and free it. */
2146 lttng_poll_del(&events
, stream
->wait_fd
);
2147 consumer_del_metadata_stream(stream
, metadata_ht
);
2148 } else if (len
> 0) {
2149 stream
->data_read
= 1;
2153 /* Release RCU lock for the stream looked up */
2160 DBG("Metadata poll thread exiting");
2161 lttng_poll_clean(&events
);
2164 destroy_stream_ht(metadata_ht
);
2167 rcu_unregister_thread();
2172 * This thread polls the fds in the set to consume the data and write
2173 * it to tracefile if necessary.
2175 void *consumer_thread_data_poll(void *data
)
2177 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2178 struct pollfd
*pollfd
= NULL
;
2179 /* local view of the streams */
2180 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2181 /* local view of consumer_data.fds_count */
2183 struct lttng_consumer_local_data
*ctx
= data
;
2186 rcu_register_thread();
2188 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2189 if (data_ht
== NULL
) {
2193 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
2200 * the fds set has been updated, we need to update our
2201 * local array as well
2203 pthread_mutex_lock(&consumer_data
.lock
);
2204 if (consumer_data
.need_update
) {
2205 if (pollfd
!= NULL
) {
2209 if (local_stream
!= NULL
) {
2211 local_stream
= NULL
;
2214 /* allocate for all fds + 1 for the consumer_data_pipe */
2215 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2216 if (pollfd
== NULL
) {
2217 PERROR("pollfd malloc");
2218 pthread_mutex_unlock(&consumer_data
.lock
);
2222 /* allocate for all fds + 1 for the consumer_data_pipe */
2223 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2224 sizeof(struct lttng_consumer_stream
));
2225 if (local_stream
== NULL
) {
2226 PERROR("local_stream malloc");
2227 pthread_mutex_unlock(&consumer_data
.lock
);
2230 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
,
2233 ERR("Error in allocating pollfd or local_outfds");
2234 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2235 pthread_mutex_unlock(&consumer_data
.lock
);
2239 consumer_data
.need_update
= 0;
2241 pthread_mutex_unlock(&consumer_data
.lock
);
2243 /* No FDs and consumer_quit, consumer_cleanup the thread */
2244 if (nb_fd
== 0 && consumer_quit
== 1) {
2247 /* poll on the array of fds */
2249 DBG("polling on %d fd", nb_fd
+ 1);
2250 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2251 DBG("poll num_rdy : %d", num_rdy
);
2252 if (num_rdy
== -1) {
2254 * Restart interrupted system call.
2256 if (errno
== EINTR
) {
2259 PERROR("Poll error");
2260 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2262 } else if (num_rdy
== 0) {
2263 DBG("Polling thread timed out");
2268 * If the consumer_data_pipe triggered poll go directly to the
2269 * beginning of the loop to update the array. We want to prioritize
2270 * array update over low-priority reads.
2272 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2273 size_t pipe_readlen
;
2275 DBG("consumer_data_pipe wake up");
2276 /* Consume 1 byte of pipe data */
2278 pipe_readlen
= read(ctx
->consumer_data_pipe
[0], &new_stream
,
2279 sizeof(new_stream
));
2280 } while (pipe_readlen
== -1 && errno
== EINTR
);
2283 * If the stream is NULL, just ignore it. It's also possible that
2284 * the sessiond poll thread changed the consumer_quit state and is
2285 * waking us up to test it.
2287 if (new_stream
== NULL
) {
2288 validate_endpoint_status_data_stream();
2292 ret
= consumer_add_stream(new_stream
, data_ht
);
2294 ERR("Consumer add stream %d failed. Continuing",
2297 * At this point, if the add_stream fails, it is not in the
2298 * hash table thus passing the NULL value here.
2300 consumer_del_stream(new_stream
, NULL
);
2303 /* Continue to update the local streams and handle prio ones */
2307 /* Take care of high priority channels first. */
2308 for (i
= 0; i
< nb_fd
; i
++) {
2309 if (local_stream
[i
] == NULL
) {
2312 if (pollfd
[i
].revents
& POLLPRI
) {
2313 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2315 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2316 /* it's ok to have an unavailable sub-buffer */
2317 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2318 /* Clean the stream and free it. */
2319 consumer_del_stream(local_stream
[i
], data_ht
);
2320 local_stream
[i
] = NULL
;
2321 } else if (len
> 0) {
2322 local_stream
[i
]->data_read
= 1;
2328 * If we read high prio channel in this loop, try again
2329 * for more high prio data.
2335 /* Take care of low priority channels. */
2336 for (i
= 0; i
< nb_fd
; i
++) {
2337 if (local_stream
[i
] == NULL
) {
2340 if ((pollfd
[i
].revents
& POLLIN
) ||
2341 local_stream
[i
]->hangup_flush_done
) {
2342 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2343 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2344 /* it's ok to have an unavailable sub-buffer */
2345 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2346 /* Clean the stream and free it. */
2347 consumer_del_stream(local_stream
[i
], data_ht
);
2348 local_stream
[i
] = NULL
;
2349 } else if (len
> 0) {
2350 local_stream
[i
]->data_read
= 1;
2355 /* Handle hangup and errors */
2356 for (i
= 0; i
< nb_fd
; i
++) {
2357 if (local_stream
[i
] == NULL
) {
2360 if (!local_stream
[i
]->hangup_flush_done
2361 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2362 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2363 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2364 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2366 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2367 /* Attempt read again, for the data we just flushed. */
2368 local_stream
[i
]->data_read
= 1;
2371 * If the poll flag is HUP/ERR/NVAL and we have
2372 * read no data in this pass, we can remove the
2373 * stream from its hash table.
2375 if ((pollfd
[i
].revents
& POLLHUP
)) {
2376 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2377 if (!local_stream
[i
]->data_read
) {
2378 consumer_del_stream(local_stream
[i
], data_ht
);
2379 local_stream
[i
] = NULL
;
2382 } else if (pollfd
[i
].revents
& POLLERR
) {
2383 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2384 if (!local_stream
[i
]->data_read
) {
2385 consumer_del_stream(local_stream
[i
], data_ht
);
2386 local_stream
[i
] = NULL
;
2389 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2390 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2391 if (!local_stream
[i
]->data_read
) {
2392 consumer_del_stream(local_stream
[i
], data_ht
);
2393 local_stream
[i
] = NULL
;
2397 if (local_stream
[i
] != NULL
) {
2398 local_stream
[i
]->data_read
= 0;
2403 DBG("polling thread exiting");
2404 if (pollfd
!= NULL
) {
2408 if (local_stream
!= NULL
) {
2410 local_stream
= NULL
;
2414 * Close the write side of the pipe so epoll_wait() in
2415 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2416 * read side of the pipe. If we close them both, epoll_wait strangely does
2417 * not return and could create a endless wait period if the pipe is the
2418 * only tracked fd in the poll set. The thread will take care of closing
2421 close(ctx
->consumer_metadata_pipe
[1]);
2424 destroy_data_stream_ht(data_ht
);
2427 rcu_unregister_thread();
2432 * This thread listens on the consumerd socket and receives the file
2433 * descriptors from the session daemon.
2435 void *consumer_thread_sessiond_poll(void *data
)
2437 int sock
, client_socket
, ret
;
2439 * structure to poll for incoming data on communication socket avoids
2440 * making blocking sockets.
2442 struct pollfd consumer_sockpoll
[2];
2443 struct lttng_consumer_local_data
*ctx
= data
;
2445 rcu_register_thread();
2447 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2448 unlink(ctx
->consumer_command_sock_path
);
2449 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2450 if (client_socket
< 0) {
2451 ERR("Cannot create command socket");
2455 ret
= lttcomm_listen_unix_sock(client_socket
);
2460 DBG("Sending ready command to lttng-sessiond");
2461 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2462 /* return < 0 on error, but == 0 is not fatal */
2464 ERR("Error sending ready command to lttng-sessiond");
2468 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2470 PERROR("fcntl O_NONBLOCK");
2474 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2475 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2476 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2477 consumer_sockpoll
[1].fd
= client_socket
;
2478 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2480 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2483 DBG("Connection on client_socket");
2485 /* Blocking call, waiting for transmission */
2486 sock
= lttcomm_accept_unix_sock(client_socket
);
2491 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2493 PERROR("fcntl O_NONBLOCK");
2497 /* update the polling structure to poll on the established socket */
2498 consumer_sockpoll
[1].fd
= sock
;
2499 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2502 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2505 DBG("Incoming command on sock");
2506 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2507 if (ret
== -ENOENT
) {
2508 DBG("Received STOP command");
2513 * This could simply be a session daemon quitting. Don't output
2516 DBG("Communication interrupted on command socket");
2519 if (consumer_quit
) {
2520 DBG("consumer_thread_receive_fds received quit from signal");
2523 DBG("received fds on sock");
2526 DBG("consumer_thread_receive_fds exiting");
2529 * when all fds have hung up, the polling thread
2535 * Notify the data poll thread to poll back again and test the
2536 * consumer_quit state that we just set so to quit gracefully.
2538 notify_thread_pipe(ctx
->consumer_data_pipe
[1]);
2540 rcu_unregister_thread();
2544 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2545 struct lttng_consumer_local_data
*ctx
)
2547 switch (consumer_data
.type
) {
2548 case LTTNG_CONSUMER_KERNEL
:
2549 return lttng_kconsumer_read_subbuffer(stream
, ctx
);
2550 case LTTNG_CONSUMER32_UST
:
2551 case LTTNG_CONSUMER64_UST
:
2552 return lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2554 ERR("Unknown consumer_data type");
2560 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2562 switch (consumer_data
.type
) {
2563 case LTTNG_CONSUMER_KERNEL
:
2564 return lttng_kconsumer_on_recv_stream(stream
);
2565 case LTTNG_CONSUMER32_UST
:
2566 case LTTNG_CONSUMER64_UST
:
2567 return lttng_ustconsumer_on_recv_stream(stream
);
2569 ERR("Unknown consumer_data type");
2576 * Allocate and set consumer data hash tables.
2578 void lttng_consumer_init(void)
2580 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2581 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2582 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2584 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2585 assert(metadata_ht
);
2586 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2591 * Process the ADD_RELAYD command receive by a consumer.
2593 * This will create a relayd socket pair and add it to the relayd hash table.
2594 * The caller MUST acquire a RCU read side lock before calling it.
2596 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
2597 struct lttng_consumer_local_data
*ctx
, int sock
,
2598 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
)
2601 struct consumer_relayd_sock_pair
*relayd
;
2603 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
2605 /* Get relayd reference if exists. */
2606 relayd
= consumer_find_relayd(net_seq_idx
);
2607 if (relayd
== NULL
) {
2608 /* Not found. Allocate one. */
2609 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
2610 if (relayd
== NULL
) {
2611 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
2616 /* Poll on consumer socket. */
2617 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2622 /* Get relayd socket from session daemon */
2623 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2624 if (ret
!= sizeof(fd
)) {
2625 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
2630 /* Copy socket information and received FD */
2631 switch (sock_type
) {
2632 case LTTNG_STREAM_CONTROL
:
2633 /* Copy received lttcomm socket */
2634 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
2635 ret
= lttcomm_create_sock(&relayd
->control_sock
);
2640 /* Close the created socket fd which is useless */
2641 close(relayd
->control_sock
.fd
);
2643 /* Assign new file descriptor */
2644 relayd
->control_sock
.fd
= fd
;
2646 case LTTNG_STREAM_DATA
:
2647 /* Copy received lttcomm socket */
2648 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
2649 ret
= lttcomm_create_sock(&relayd
->data_sock
);
2654 /* Close the created socket fd which is useless */
2655 close(relayd
->data_sock
.fd
);
2657 /* Assign new file descriptor */
2658 relayd
->data_sock
.fd
= fd
;
2661 ERR("Unknown relayd socket type (%d)", sock_type
);
2665 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2666 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
2667 relayd
->net_seq_idx
, fd
);
2670 * Add relayd socket pair to consumer data hashtable. If object already
2671 * exists or on error, the function gracefully returns.
2683 * Try to lock the stream mutex.
2685 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
2687 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
2694 * Try to lock the stream mutex. On failure, we know that the stream is
2695 * being used else where hence there is data still being extracted.
2697 ret
= pthread_mutex_trylock(&stream
->lock
);
2699 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
2711 * Check if for a given session id there is still data needed to be extract
2714 * Return 1 if data is pending or else 0 meaning ready to be read.
2716 int consumer_data_pending(uint64_t id
)
2719 struct lttng_ht_iter iter
;
2720 struct lttng_ht
*ht
;
2721 struct lttng_consumer_stream
*stream
;
2722 struct consumer_relayd_sock_pair
*relayd
;
2723 int (*data_pending
)(struct lttng_consumer_stream
*);
2725 DBG("Consumer data pending command on session id %" PRIu64
, id
);
2728 pthread_mutex_lock(&consumer_data
.lock
);
2730 switch (consumer_data
.type
) {
2731 case LTTNG_CONSUMER_KERNEL
:
2732 data_pending
= lttng_kconsumer_data_pending
;
2734 case LTTNG_CONSUMER32_UST
:
2735 case LTTNG_CONSUMER64_UST
:
2736 data_pending
= lttng_ustconsumer_data_pending
;
2739 ERR("Unknown consumer data type");
2743 /* Ease our life a bit */
2744 ht
= consumer_data
.stream_list_ht
;
2746 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2747 ht
->hash_fct((void *)((unsigned long) id
), lttng_ht_seed
),
2748 ht
->match_fct
, (void *)((unsigned long) id
),
2749 &iter
.iter
, stream
, node_session_id
.node
) {
2750 /* If this call fails, the stream is being used hence data pending. */
2751 ret
= stream_try_lock(stream
);
2753 goto data_not_pending
;
2757 * A removed node from the hash table indicates that the stream has
2758 * been deleted thus having a guarantee that the buffers are closed
2759 * on the consumer side. However, data can still be transmitted
2760 * over the network so don't skip the relayd check.
2762 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
2764 /* Check the stream if there is data in the buffers. */
2765 ret
= data_pending(stream
);
2767 pthread_mutex_unlock(&stream
->lock
);
2768 goto data_not_pending
;
2773 if (stream
->net_seq_idx
!= -1) {
2774 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
2777 * At this point, if the relayd object is not available for the
2778 * given stream, it is because the relayd is being cleaned up
2779 * so every stream associated with it (for a session id value)
2780 * are or will be marked for deletion hence no data pending.
2782 pthread_mutex_unlock(&stream
->lock
);
2783 goto data_not_pending
;
2786 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
2787 if (stream
->metadata_flag
) {
2788 ret
= relayd_quiescent_control(&relayd
->control_sock
);
2790 ret
= relayd_data_pending(&relayd
->control_sock
,
2791 stream
->relayd_stream_id
, stream
->next_net_seq_num
);
2793 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2795 pthread_mutex_unlock(&stream
->lock
);
2796 goto data_not_pending
;
2799 pthread_mutex_unlock(&stream
->lock
);
2803 * Finding _no_ node in the hash table means that the stream(s) have been
2804 * removed thus data is guaranteed to be available for analysis from the
2805 * trace files. This is *only* true for local consumer and not network
2809 /* Data is available to be read by a viewer. */
2810 pthread_mutex_unlock(&consumer_data
.lock
);
2815 /* Data is still being extracted from buffers. */
2816 pthread_mutex_unlock(&consumer_data
.lock
);