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 * Global hash table containing respectively metadata and data streams. The
60 * stream element in this ht should only be updated by the metadata poll thread
61 * for the metadata and the data poll thread for the data.
63 static struct lttng_ht
*metadata_ht
;
64 static struct lttng_ht
*data_ht
;
67 * This hash table contains the mapping between the session id of the sessiond
68 * and the relayd session id. Element of the ht are indexed by sessiond_id.
70 * Node can be added when a relayd communication is opened in the sessiond
73 * Note that a session id of the session daemon is unique to a tracing session
74 * and not to a domain session. However, a domain session has one consumer
75 * which forces the 1-1 mapping between a consumer and a domain session (ex:
76 * UST). This means that we can't have duplicate in this ht.
78 static struct lttng_ht
*relayd_session_id_ht
;
81 * Notify a thread pipe to poll back again. This usually means that some global
82 * state has changed so we just send back the thread in a poll wait call.
84 static void notify_thread_pipe(int wpipe
)
89 struct lttng_consumer_stream
*null_stream
= NULL
;
91 ret
= write(wpipe
, &null_stream
, sizeof(null_stream
));
92 } while (ret
< 0 && errno
== EINTR
);
96 * Find a stream. The consumer_data.lock must be locked during this
99 static struct lttng_consumer_stream
*consumer_find_stream(int key
,
102 struct lttng_ht_iter iter
;
103 struct lttng_ht_node_ulong
*node
;
104 struct lttng_consumer_stream
*stream
= NULL
;
108 /* Negative keys are lookup failures */
115 lttng_ht_lookup(ht
, (void *)((unsigned long) key
), &iter
);
116 node
= lttng_ht_iter_get_node_ulong(&iter
);
118 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
126 void consumer_steal_stream_key(int key
, struct lttng_ht
*ht
)
128 struct lttng_consumer_stream
*stream
;
131 stream
= consumer_find_stream(key
, ht
);
135 * We don't want the lookup to match, but we still need
136 * to iterate on this stream when iterating over the hash table. Just
137 * change the node key.
139 stream
->node
.key
= -1;
145 * Return a channel object for the given key.
147 * RCU read side lock MUST be acquired before calling this function and
148 * protects the channel ptr.
150 static struct lttng_consumer_channel
*consumer_find_channel(int key
)
152 struct lttng_ht_iter iter
;
153 struct lttng_ht_node_ulong
*node
;
154 struct lttng_consumer_channel
*channel
= NULL
;
156 /* Negative keys are lookup failures */
161 lttng_ht_lookup(consumer_data
.channel_ht
, (void *)((unsigned long) key
),
163 node
= lttng_ht_iter_get_node_ulong(&iter
);
165 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
171 static void consumer_steal_channel_key(int key
)
173 struct lttng_consumer_channel
*channel
;
176 channel
= consumer_find_channel(key
);
180 * We don't want the lookup to match, but we still need
181 * to iterate on this channel when iterating over the hash table. Just
182 * change the node key.
184 channel
->node
.key
= -1;
190 void consumer_free_stream(struct rcu_head
*head
)
192 struct lttng_ht_node_ulong
*node
=
193 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
194 struct lttng_consumer_stream
*stream
=
195 caa_container_of(node
, struct lttng_consumer_stream
, node
);
201 * RCU protected relayd socket pair free.
203 static void consumer_rcu_free_relayd(struct rcu_head
*head
)
205 struct lttng_ht_node_ulong
*node
=
206 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
207 struct consumer_relayd_sock_pair
*relayd
=
208 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
211 * Close all sockets. This is done in the call RCU since we don't want the
212 * socket fds to be reassigned thus potentially creating bad state of the
215 * We do not have to lock the control socket mutex here since at this stage
216 * there is no one referencing to this relayd object.
218 (void) relayd_close(&relayd
->control_sock
);
219 (void) relayd_close(&relayd
->data_sock
);
225 * Destroy and free relayd socket pair object.
227 * This function MUST be called with the consumer_data lock acquired.
229 static void destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
232 struct lttng_ht_iter iter
;
233 struct lttng_ht_node_ulong
*node
;
235 if (relayd
== NULL
) {
239 DBG("Consumer destroy and close relayd socket pair");
241 lttng_ht_lookup(relayd_session_id_ht
,
242 (void *)((unsigned long) relayd
->session_id
), &iter
);
243 node
= lttng_ht_iter_get_node_ulong(&iter
);
245 /* We assume the relayd is being or is destroyed */
249 ret
= lttng_ht_del(relayd_session_id_ht
, &iter
);
251 /* We assume the relayd is being or is destroyed */
255 iter
.iter
.node
= &relayd
->node
.node
;
256 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
258 /* We assume the relayd is being or is destroyed */
262 /* RCU free() call */
263 call_rcu(&relayd
->node
.head
, consumer_rcu_free_relayd
);
267 * Iterate over the relayd hash table and destroy each element. Finally,
268 * destroy the whole hash table.
270 static void cleanup_relayd_ht(void)
272 struct lttng_ht_iter iter
;
273 struct consumer_relayd_sock_pair
*relayd
;
277 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
279 destroy_relayd(relayd
);
282 lttng_ht_destroy(consumer_data
.relayd_ht
);
288 * Update the end point status of all streams having the given network sequence
289 * index (relayd index).
291 * It's atomically set without having the stream mutex locked which is fine
292 * because we handle the write/read race with a pipe wakeup for each thread.
294 static void update_endpoint_status_by_netidx(int net_seq_idx
,
295 enum consumer_endpoint_status status
)
297 struct lttng_ht_iter iter
;
298 struct lttng_consumer_stream
*stream
;
300 DBG("Consumer set delete flag on stream by idx %d", net_seq_idx
);
304 /* Let's begin with metadata */
305 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
306 if (stream
->net_seq_idx
== net_seq_idx
) {
307 uatomic_set(&stream
->endpoint_status
, status
);
308 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
312 /* Follow up by the data streams */
313 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
314 if (stream
->net_seq_idx
== net_seq_idx
) {
315 uatomic_set(&stream
->endpoint_status
, status
);
316 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
323 * Cleanup a relayd object by flagging every associated streams for deletion,
324 * destroying the object meaning removing it from the relayd hash table,
325 * closing the sockets and freeing the memory in a RCU call.
327 * If a local data context is available, notify the threads that the streams'
328 * state have changed.
330 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
331 struct lttng_consumer_local_data
*ctx
)
337 DBG("Cleaning up relayd sockets");
339 /* Save the net sequence index before destroying the object */
340 netidx
= relayd
->net_seq_idx
;
343 * Delete the relayd from the relayd hash table, close the sockets and free
344 * the object in a RCU call.
346 destroy_relayd(relayd
);
348 /* Set inactive endpoint to all streams */
349 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
352 * With a local data context, notify the threads that the streams' state
353 * have changed. The write() action on the pipe acts as an "implicit"
354 * memory barrier ordering the updates of the end point status from the
355 * read of this status which happens AFTER receiving this notify.
358 notify_thread_pipe(ctx
->consumer_data_pipe
[1]);
359 notify_thread_pipe(ctx
->consumer_metadata_pipe
[1]);
364 * Flag a relayd socket pair for destruction. Destroy it if the refcount
367 * RCU read side lock MUST be aquired before calling this function.
369 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
373 /* Set destroy flag for this object */
374 uatomic_set(&relayd
->destroy_flag
, 1);
376 /* Destroy the relayd if refcount is 0 */
377 if (uatomic_read(&relayd
->refcount
) == 0) {
378 destroy_relayd(relayd
);
383 * Remove a stream from the global list protected by a mutex. This
384 * function is also responsible for freeing its data structures.
386 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
390 struct lttng_ht_iter iter
;
391 struct lttng_consumer_channel
*free_chan
= NULL
;
392 struct consumer_relayd_sock_pair
*relayd
;
396 DBG("Consumer del stream %d", stream
->wait_fd
);
399 /* Means the stream was allocated but not successfully added */
403 pthread_mutex_lock(&consumer_data
.lock
);
404 pthread_mutex_lock(&stream
->lock
);
406 switch (consumer_data
.type
) {
407 case LTTNG_CONSUMER_KERNEL
:
408 if (stream
->mmap_base
!= NULL
) {
409 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
415 case LTTNG_CONSUMER32_UST
:
416 case LTTNG_CONSUMER64_UST
:
417 lttng_ustconsumer_del_stream(stream
);
420 ERR("Unknown consumer_data type");
426 iter
.iter
.node
= &stream
->node
.node
;
427 ret
= lttng_ht_del(ht
, &iter
);
430 /* Remove node session id from the consumer_data stream ht */
431 iter
.iter
.node
= &stream
->node_session_id
.node
;
432 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
436 assert(consumer_data
.stream_count
> 0);
437 consumer_data
.stream_count
--;
439 if (stream
->out_fd
>= 0) {
440 ret
= close(stream
->out_fd
);
445 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
446 ret
= close(stream
->wait_fd
);
451 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
452 ret
= close(stream
->shm_fd
);
458 /* Check and cleanup relayd */
460 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
461 if (relayd
!= NULL
) {
462 uatomic_dec(&relayd
->refcount
);
463 assert(uatomic_read(&relayd
->refcount
) >= 0);
465 /* Closing streams requires to lock the control socket. */
466 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
467 ret
= relayd_send_close_stream(&relayd
->control_sock
,
468 stream
->relayd_stream_id
,
469 stream
->next_net_seq_num
- 1);
470 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
472 DBG("Unable to close stream on the relayd. Continuing");
474 * Continue here. There is nothing we can do for the relayd.
475 * Chances are that the relayd has closed the socket so we just
476 * continue cleaning up.
480 /* Both conditions are met, we destroy the relayd. */
481 if (uatomic_read(&relayd
->refcount
) == 0 &&
482 uatomic_read(&relayd
->destroy_flag
)) {
483 destroy_relayd(relayd
);
488 uatomic_dec(&stream
->chan
->refcount
);
489 if (!uatomic_read(&stream
->chan
->refcount
)
490 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
491 free_chan
= stream
->chan
;
495 consumer_data
.need_update
= 1;
496 pthread_mutex_unlock(&stream
->lock
);
497 pthread_mutex_unlock(&consumer_data
.lock
);
500 consumer_del_channel(free_chan
);
504 call_rcu(&stream
->node
.head
, consumer_free_stream
);
507 struct lttng_consumer_stream
*consumer_allocate_stream(
508 int channel_key
, int stream_key
,
509 int shm_fd
, int wait_fd
,
510 enum lttng_consumer_stream_state state
,
512 enum lttng_event_output output
,
513 const char *path_name
,
521 struct lttng_consumer_stream
*stream
;
523 stream
= zmalloc(sizeof(*stream
));
524 if (stream
== NULL
) {
525 PERROR("malloc struct lttng_consumer_stream");
526 *alloc_ret
= -ENOMEM
;
533 * Get stream's channel reference. Needed when adding the stream to the
536 stream
->chan
= consumer_find_channel(channel_key
);
538 *alloc_ret
= -ENOENT
;
539 ERR("Unable to find channel for stream %d", stream_key
);
543 stream
->key
= stream_key
;
544 stream
->shm_fd
= shm_fd
;
545 stream
->wait_fd
= wait_fd
;
547 stream
->out_fd_offset
= 0;
548 stream
->state
= state
;
549 stream
->mmap_len
= mmap_len
;
550 stream
->mmap_base
= NULL
;
551 stream
->output
= output
;
554 stream
->net_seq_idx
= net_index
;
555 stream
->metadata_flag
= metadata_flag
;
556 stream
->session_id
= session_id
;
557 strncpy(stream
->path_name
, path_name
, sizeof(stream
->path_name
));
558 stream
->path_name
[sizeof(stream
->path_name
) - 1] = '\0';
559 pthread_mutex_init(&stream
->lock
, NULL
);
562 * Index differently the metadata node because the thread is using an
563 * internal hash table to match streams in the metadata_ht to the epoll set
567 lttng_ht_node_init_ulong(&stream
->node
, stream
->wait_fd
);
569 lttng_ht_node_init_ulong(&stream
->node
, stream
->key
);
572 /* Init session id node with the stream session id */
573 lttng_ht_node_init_ulong(&stream
->node_session_id
, stream
->session_id
);
576 * The cpu number is needed before using any ustctl_* actions. Ignored for
577 * the kernel so the value does not matter.
579 pthread_mutex_lock(&consumer_data
.lock
);
580 stream
->cpu
= stream
->chan
->cpucount
++;
581 pthread_mutex_unlock(&consumer_data
.lock
);
583 DBG3("Allocated stream %s (key %d, shm_fd %d, wait_fd %d, mmap_len %llu,"
584 " out_fd %d, net_seq_idx %d, session_id %" PRIu64
,
585 stream
->path_name
, stream
->key
, stream
->shm_fd
, stream
->wait_fd
,
586 (unsigned long long) stream
->mmap_len
, stream
->out_fd
,
587 stream
->net_seq_idx
, stream
->session_id
);
600 * Add a stream to the global list protected by a mutex.
602 static int consumer_add_stream(struct lttng_consumer_stream
*stream
,
606 struct consumer_relayd_sock_pair
*relayd
;
611 DBG3("Adding consumer stream %d", stream
->key
);
613 pthread_mutex_lock(&consumer_data
.lock
);
614 pthread_mutex_lock(&stream
->lock
);
617 /* Steal stream identifier to avoid having streams with the same key */
618 consumer_steal_stream_key(stream
->key
, ht
);
620 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
623 * Add stream to the stream_list_ht of the consumer data. No need to steal
624 * the key since the HT does not use it and we allow to add redundant keys
627 lttng_ht_add_ulong(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
629 /* Check and cleanup relayd */
630 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
631 if (relayd
!= NULL
) {
632 uatomic_inc(&relayd
->refcount
);
635 /* Update channel refcount once added without error(s). */
636 uatomic_inc(&stream
->chan
->refcount
);
639 * When nb_init_streams reaches 0, we don't need to trigger any action in
640 * terms of destroying the associated channel, because the action that
641 * causes the count to become 0 also causes a stream to be added. The
642 * channel deletion will thus be triggered by the following removal of this
645 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
646 uatomic_dec(&stream
->chan
->nb_init_streams
);
649 /* Update consumer data once the node is inserted. */
650 consumer_data
.stream_count
++;
651 consumer_data
.need_update
= 1;
654 pthread_mutex_unlock(&stream
->lock
);
655 pthread_mutex_unlock(&consumer_data
.lock
);
661 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
662 * be acquired before calling this.
664 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
667 struct lttng_ht_node_ulong
*node
;
668 struct lttng_ht_iter iter
;
670 if (relayd
== NULL
) {
675 lttng_ht_lookup(consumer_data
.relayd_ht
,
676 (void *)((unsigned long) relayd
->net_seq_idx
), &iter
);
677 node
= lttng_ht_iter_get_node_ulong(&iter
);
679 /* Relayd already exist. Ignore the insertion */
682 lttng_ht_add_unique_ulong(consumer_data
.relayd_ht
, &relayd
->node
);
689 * Allocate and return a consumer relayd socket.
691 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
694 struct consumer_relayd_sock_pair
*obj
= NULL
;
696 /* Negative net sequence index is a failure */
697 if (net_seq_idx
< 0) {
701 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
703 PERROR("zmalloc relayd sock");
707 obj
->net_seq_idx
= net_seq_idx
;
709 obj
->destroy_flag
= 0;
710 lttng_ht_node_init_ulong(&obj
->node
, obj
->net_seq_idx
);
711 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
718 * Find a relayd socket pair in the global consumer data.
720 * Return the object if found else NULL.
721 * RCU read-side lock must be held across this call and while using the
724 struct consumer_relayd_sock_pair
*consumer_find_relayd(int key
)
726 struct lttng_ht_iter iter
;
727 struct lttng_ht_node_ulong
*node
;
728 struct consumer_relayd_sock_pair
*relayd
= NULL
;
730 /* Negative keys are lookup failures */
735 lttng_ht_lookup(consumer_data
.relayd_ht
, (void *)((unsigned long) key
),
737 node
= lttng_ht_iter_get_node_ulong(&iter
);
739 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
747 * Handle stream for relayd transmission if the stream applies for network
748 * streaming where the net sequence index is set.
750 * Return destination file descriptor or negative value on error.
752 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
753 size_t data_size
, unsigned long padding
,
754 struct consumer_relayd_sock_pair
*relayd
)
757 struct lttcomm_relayd_data_hdr data_hdr
;
763 /* Reset data header */
764 memset(&data_hdr
, 0, sizeof(data_hdr
));
766 if (stream
->metadata_flag
) {
767 /* Caller MUST acquire the relayd control socket lock */
768 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
773 /* Metadata are always sent on the control socket. */
774 outfd
= relayd
->control_sock
.fd
;
776 /* Set header with stream information */
777 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
778 data_hdr
.data_size
= htobe32(data_size
);
779 data_hdr
.padding_size
= htobe32(padding
);
780 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
++);
781 /* Other fields are zeroed previously */
783 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
789 /* Set to go on data socket */
790 outfd
= relayd
->data_sock
.fd
;
798 void consumer_free_channel(struct rcu_head
*head
)
800 struct lttng_ht_node_ulong
*node
=
801 caa_container_of(head
, struct lttng_ht_node_ulong
, head
);
802 struct lttng_consumer_channel
*channel
=
803 caa_container_of(node
, struct lttng_consumer_channel
, node
);
809 * Remove a channel from the global list protected by a mutex. This
810 * function is also responsible for freeing its data structures.
812 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
815 struct lttng_ht_iter iter
;
817 DBG("Consumer delete channel key %d", channel
->key
);
819 pthread_mutex_lock(&consumer_data
.lock
);
821 switch (consumer_data
.type
) {
822 case LTTNG_CONSUMER_KERNEL
:
824 case LTTNG_CONSUMER32_UST
:
825 case LTTNG_CONSUMER64_UST
:
826 lttng_ustconsumer_del_channel(channel
);
829 ERR("Unknown consumer_data type");
835 iter
.iter
.node
= &channel
->node
.node
;
836 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
840 if (channel
->mmap_base
!= NULL
) {
841 ret
= munmap(channel
->mmap_base
, channel
->mmap_len
);
846 if (channel
->wait_fd
>= 0 && !channel
->wait_fd_is_copy
) {
847 ret
= close(channel
->wait_fd
);
852 if (channel
->shm_fd
>= 0 && channel
->wait_fd
!= channel
->shm_fd
) {
853 ret
= close(channel
->shm_fd
);
859 call_rcu(&channel
->node
.head
, consumer_free_channel
);
861 pthread_mutex_unlock(&consumer_data
.lock
);
864 struct lttng_consumer_channel
*consumer_allocate_channel(
866 int shm_fd
, int wait_fd
,
868 uint64_t max_sb_size
,
869 unsigned int nb_init_streams
)
871 struct lttng_consumer_channel
*channel
;
874 channel
= zmalloc(sizeof(*channel
));
875 if (channel
== NULL
) {
876 PERROR("malloc struct lttng_consumer_channel");
879 channel
->key
= channel_key
;
880 channel
->shm_fd
= shm_fd
;
881 channel
->wait_fd
= wait_fd
;
882 channel
->mmap_len
= mmap_len
;
883 channel
->max_sb_size
= max_sb_size
;
884 channel
->refcount
= 0;
885 channel
->nb_init_streams
= nb_init_streams
;
886 lttng_ht_node_init_ulong(&channel
->node
, channel
->key
);
888 switch (consumer_data
.type
) {
889 case LTTNG_CONSUMER_KERNEL
:
890 channel
->mmap_base
= NULL
;
891 channel
->mmap_len
= 0;
893 case LTTNG_CONSUMER32_UST
:
894 case LTTNG_CONSUMER64_UST
:
895 ret
= lttng_ustconsumer_allocate_channel(channel
);
902 ERR("Unknown consumer_data type");
906 DBG("Allocated channel (key %d, shm_fd %d, wait_fd %d, mmap_len %llu, max_sb_size %llu)",
907 channel
->key
, channel
->shm_fd
, channel
->wait_fd
,
908 (unsigned long long) channel
->mmap_len
,
909 (unsigned long long) channel
->max_sb_size
);
915 * Add a channel to the global list protected by a mutex.
917 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
919 struct lttng_ht_node_ulong
*node
;
920 struct lttng_ht_iter iter
;
922 pthread_mutex_lock(&consumer_data
.lock
);
923 /* Steal channel identifier, for UST */
924 consumer_steal_channel_key(channel
->key
);
927 lttng_ht_lookup(consumer_data
.channel_ht
,
928 (void *)((unsigned long) channel
->key
), &iter
);
929 node
= lttng_ht_iter_get_node_ulong(&iter
);
931 /* Channel already exist. Ignore the insertion */
935 lttng_ht_add_unique_ulong(consumer_data
.channel_ht
, &channel
->node
);
939 pthread_mutex_unlock(&consumer_data
.lock
);
945 * Allocate the pollfd structure and the local view of the out fds to avoid
946 * doing a lookup in the linked list and concurrency issues when writing is
947 * needed. Called with consumer_data.lock held.
949 * Returns the number of fds in the structures.
951 static int consumer_update_poll_array(
952 struct lttng_consumer_local_data
*ctx
, struct pollfd
**pollfd
,
953 struct lttng_consumer_stream
**local_stream
, struct lttng_ht
*ht
)
956 struct lttng_ht_iter iter
;
957 struct lttng_consumer_stream
*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
) {
975 DBG("Active FD %d", stream
->wait_fd
);
976 (*pollfd
)[i
].fd
= stream
->wait_fd
;
977 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
978 local_stream
[i
] = stream
;
984 * Insert the consumer_data_pipe at the end of the array and don't
985 * increment i so nb_fd is the number of real FD.
987 (*pollfd
)[i
].fd
= ctx
->consumer_data_pipe
[0];
988 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
993 * Poll on the should_quit pipe and the command socket return -1 on error and
994 * should exit, 0 if data is available on the command socket
996 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
1001 num_rdy
= poll(consumer_sockpoll
, 2, -1);
1002 if (num_rdy
== -1) {
1004 * Restart interrupted system call.
1006 if (errno
== EINTR
) {
1009 PERROR("Poll error");
1012 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
1013 DBG("consumer_should_quit wake up");
1023 * Set the error socket.
1025 void lttng_consumer_set_error_sock(
1026 struct lttng_consumer_local_data
*ctx
, int sock
)
1028 ctx
->consumer_error_socket
= sock
;
1032 * Set the command socket path.
1034 void lttng_consumer_set_command_sock_path(
1035 struct lttng_consumer_local_data
*ctx
, char *sock
)
1037 ctx
->consumer_command_sock_path
= sock
;
1041 * Send return code to the session daemon.
1042 * If the socket is not defined, we return 0, it is not a fatal error
1044 int lttng_consumer_send_error(
1045 struct lttng_consumer_local_data
*ctx
, int cmd
)
1047 if (ctx
->consumer_error_socket
> 0) {
1048 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
1049 sizeof(enum lttcomm_sessiond_command
));
1056 * Close all the tracefiles and stream fds and MUST be called when all
1057 * instances are destroyed i.e. when all threads were joined and are ended.
1059 void lttng_consumer_cleanup(void)
1061 struct lttng_ht_iter iter
;
1062 struct lttng_ht_node_ulong
*node
;
1066 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, node
,
1068 struct lttng_consumer_channel
*channel
=
1069 caa_container_of(node
, struct lttng_consumer_channel
, node
);
1070 consumer_del_channel(channel
);
1075 lttng_ht_destroy(consumer_data
.channel_ht
);
1077 cleanup_relayd_ht();
1080 * This HT contains streams that are freed by either the metadata thread or
1081 * the data thread so we do *nothing* on the hash table and simply destroy
1084 lttng_ht_destroy(consumer_data
.stream_list_ht
);
1088 * Called from signal handler.
1090 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
1095 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
1096 } while (ret
< 0 && errno
== EINTR
);
1098 PERROR("write consumer quit");
1101 DBG("Consumer flag that it should quit");
1104 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1107 int outfd
= stream
->out_fd
;
1110 * This does a blocking write-and-wait on any page that belongs to the
1111 * subbuffer prior to the one we just wrote.
1112 * Don't care about error values, as these are just hints and ways to
1113 * limit the amount of page cache used.
1115 if (orig_offset
< stream
->chan
->max_sb_size
) {
1118 lttng_sync_file_range(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
1119 stream
->chan
->max_sb_size
,
1120 SYNC_FILE_RANGE_WAIT_BEFORE
1121 | SYNC_FILE_RANGE_WRITE
1122 | SYNC_FILE_RANGE_WAIT_AFTER
);
1124 * Give hints to the kernel about how we access the file:
1125 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1128 * We need to call fadvise again after the file grows because the
1129 * kernel does not seem to apply fadvise to non-existing parts of the
1132 * Call fadvise _after_ having waited for the page writeback to
1133 * complete because the dirty page writeback semantic is not well
1134 * defined. So it can be expected to lead to lower throughput in
1137 posix_fadvise(outfd
, orig_offset
- stream
->chan
->max_sb_size
,
1138 stream
->chan
->max_sb_size
, POSIX_FADV_DONTNEED
);
1142 * Initialise the necessary environnement :
1143 * - create a new context
1144 * - create the poll_pipe
1145 * - create the should_quit pipe (for signal handler)
1146 * - create the thread pipe (for splice)
1148 * Takes a function pointer as argument, this function is called when data is
1149 * available on a buffer. This function is responsible to do the
1150 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1151 * buffer configuration and then kernctl_put_next_subbuf at the end.
1153 * Returns a pointer to the new context or NULL on error.
1155 struct lttng_consumer_local_data
*lttng_consumer_create(
1156 enum lttng_consumer_type type
,
1157 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1158 struct lttng_consumer_local_data
*ctx
),
1159 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1160 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1161 int (*update_stream
)(int stream_key
, uint32_t state
))
1164 struct lttng_consumer_local_data
*ctx
;
1166 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1167 consumer_data
.type
== type
);
1168 consumer_data
.type
= type
;
1170 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1172 PERROR("allocating context");
1176 ctx
->consumer_error_socket
= -1;
1177 /* assign the callbacks */
1178 ctx
->on_buffer_ready
= buffer_ready
;
1179 ctx
->on_recv_channel
= recv_channel
;
1180 ctx
->on_recv_stream
= recv_stream
;
1181 ctx
->on_update_stream
= update_stream
;
1183 ret
= pipe(ctx
->consumer_data_pipe
);
1185 PERROR("Error creating poll pipe");
1186 goto error_poll_pipe
;
1189 /* set read end of the pipe to non-blocking */
1190 ret
= fcntl(ctx
->consumer_data_pipe
[0], F_SETFL
, O_NONBLOCK
);
1192 PERROR("fcntl O_NONBLOCK");
1193 goto error_poll_fcntl
;
1196 /* set write end of the pipe to non-blocking */
1197 ret
= fcntl(ctx
->consumer_data_pipe
[1], F_SETFL
, O_NONBLOCK
);
1199 PERROR("fcntl O_NONBLOCK");
1200 goto error_poll_fcntl
;
1203 ret
= pipe(ctx
->consumer_should_quit
);
1205 PERROR("Error creating recv pipe");
1206 goto error_quit_pipe
;
1209 ret
= pipe(ctx
->consumer_thread_pipe
);
1211 PERROR("Error creating thread pipe");
1212 goto error_thread_pipe
;
1215 ret
= utils_create_pipe(ctx
->consumer_metadata_pipe
);
1217 goto error_metadata_pipe
;
1220 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1222 goto error_splice_pipe
;
1228 utils_close_pipe(ctx
->consumer_metadata_pipe
);
1229 error_metadata_pipe
:
1230 utils_close_pipe(ctx
->consumer_thread_pipe
);
1232 for (i
= 0; i
< 2; i
++) {
1235 err
= close(ctx
->consumer_should_quit
[i
]);
1242 for (i
= 0; i
< 2; i
++) {
1245 err
= close(ctx
->consumer_data_pipe
[i
]);
1257 * Close all fds associated with the instance and free the context.
1259 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1263 DBG("Consumer destroying it. Closing everything.");
1265 ret
= close(ctx
->consumer_error_socket
);
1269 ret
= close(ctx
->consumer_thread_pipe
[0]);
1273 ret
= close(ctx
->consumer_thread_pipe
[1]);
1277 ret
= close(ctx
->consumer_data_pipe
[0]);
1281 ret
= close(ctx
->consumer_data_pipe
[1]);
1285 ret
= close(ctx
->consumer_should_quit
[0]);
1289 ret
= close(ctx
->consumer_should_quit
[1]);
1293 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1295 unlink(ctx
->consumer_command_sock_path
);
1300 * Write the metadata stream id on the specified file descriptor.
1302 static int write_relayd_metadata_id(int fd
,
1303 struct lttng_consumer_stream
*stream
,
1304 struct consumer_relayd_sock_pair
*relayd
,
1305 unsigned long padding
)
1308 struct lttcomm_relayd_metadata_payload hdr
;
1310 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1311 hdr
.padding_size
= htobe32(padding
);
1313 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1314 } while (ret
< 0 && errno
== EINTR
);
1316 PERROR("write metadata stream id");
1319 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1320 stream
->relayd_stream_id
, padding
);
1327 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1328 * core function for writing trace buffers to either the local filesystem or
1331 * It must be called with the stream lock held.
1333 * Careful review MUST be put if any changes occur!
1335 * Returns the number of bytes written
1337 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1338 struct lttng_consumer_local_data
*ctx
,
1339 struct lttng_consumer_stream
*stream
, unsigned long len
,
1340 unsigned long padding
)
1342 unsigned long mmap_offset
;
1343 ssize_t ret
= 0, written
= 0;
1344 off_t orig_offset
= stream
->out_fd_offset
;
1345 /* Default is on the disk */
1346 int outfd
= stream
->out_fd
;
1347 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1348 unsigned int relayd_hang_up
= 0;
1350 /* RCU lock for the relayd pointer */
1353 /* Flag that the current stream if set for network streaming. */
1354 if (stream
->net_seq_idx
!= -1) {
1355 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1356 if (relayd
== NULL
) {
1361 /* get the offset inside the fd to mmap */
1362 switch (consumer_data
.type
) {
1363 case LTTNG_CONSUMER_KERNEL
:
1364 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1366 case LTTNG_CONSUMER32_UST
:
1367 case LTTNG_CONSUMER64_UST
:
1368 ret
= lttng_ustctl_get_mmap_read_offset(stream
->chan
->handle
,
1369 stream
->buf
, &mmap_offset
);
1372 ERR("Unknown consumer_data type");
1377 PERROR("tracer ctl get_mmap_read_offset");
1382 /* Handle stream on the relayd if the output is on the network */
1384 unsigned long netlen
= len
;
1387 * Lock the control socket for the complete duration of the function
1388 * since from this point on we will use the socket.
1390 if (stream
->metadata_flag
) {
1391 /* Metadata requires the control socket. */
1392 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1393 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1396 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1398 /* Use the returned socket. */
1401 /* Write metadata stream id before payload */
1402 if (stream
->metadata_flag
) {
1403 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1406 /* Socket operation failed. We consider the relayd dead */
1407 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1415 /* Socket operation failed. We consider the relayd dead */
1416 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1420 /* Else, use the default set before which is the filesystem. */
1423 /* No streaming, we have to set the len with the full padding */
1429 ret
= write(outfd
, stream
->mmap_base
+ mmap_offset
, len
);
1430 } while (ret
< 0 && errno
== EINTR
);
1431 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1433 PERROR("Error in file write");
1437 /* Socket operation failed. We consider the relayd dead */
1438 if (errno
== EPIPE
|| errno
== EINVAL
) {
1443 } else if (ret
> len
) {
1444 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1452 /* This call is useless on a socket so better save a syscall. */
1454 /* This won't block, but will start writeout asynchronously */
1455 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1456 SYNC_FILE_RANGE_WRITE
);
1457 stream
->out_fd_offset
+= ret
;
1461 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1465 * This is a special case that the relayd has closed its socket. Let's
1466 * cleanup the relayd object and all associated streams.
1468 if (relayd
&& relayd_hang_up
) {
1469 cleanup_relayd(relayd
, ctx
);
1473 /* Unlock only if ctrl socket used */
1474 if (relayd
&& stream
->metadata_flag
) {
1475 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1483 * Splice the data from the ring buffer to the tracefile.
1485 * It must be called with the stream lock held.
1487 * Returns the number of bytes spliced.
1489 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1490 struct lttng_consumer_local_data
*ctx
,
1491 struct lttng_consumer_stream
*stream
, unsigned long len
,
1492 unsigned long padding
)
1494 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1496 off_t orig_offset
= stream
->out_fd_offset
;
1497 int fd
= stream
->wait_fd
;
1498 /* Default is on the disk */
1499 int outfd
= stream
->out_fd
;
1500 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1502 unsigned int relayd_hang_up
= 0;
1504 switch (consumer_data
.type
) {
1505 case LTTNG_CONSUMER_KERNEL
:
1507 case LTTNG_CONSUMER32_UST
:
1508 case LTTNG_CONSUMER64_UST
:
1509 /* Not supported for user space tracing */
1512 ERR("Unknown consumer_data type");
1516 /* RCU lock for the relayd pointer */
1519 /* Flag that the current stream if set for network streaming. */
1520 if (stream
->net_seq_idx
!= -1) {
1521 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1522 if (relayd
== NULL
) {
1528 * Choose right pipe for splice. Metadata and trace data are handled by
1529 * different threads hence the use of two pipes in order not to race or
1530 * corrupt the written data.
1532 if (stream
->metadata_flag
) {
1533 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1535 splice_pipe
= ctx
->consumer_thread_pipe
;
1538 /* Write metadata stream id before payload */
1540 int total_len
= len
;
1542 if (stream
->metadata_flag
) {
1544 * Lock the control socket for the complete duration of the function
1545 * since from this point on we will use the socket.
1547 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1549 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1553 /* Socket operation failed. We consider the relayd dead */
1554 if (ret
== -EBADF
) {
1555 WARN("Remote relayd disconnected. Stopping");
1562 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1565 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1567 /* Use the returned socket. */
1570 /* Socket operation failed. We consider the relayd dead */
1571 if (ret
== -EBADF
) {
1572 WARN("Remote relayd disconnected. Stopping");
1579 /* No streaming, we have to set the len with the full padding */
1584 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1585 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1586 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1587 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1588 DBG("splice chan to pipe, ret %zd", ret_splice
);
1589 if (ret_splice
< 0) {
1590 PERROR("Error in relay splice");
1592 written
= ret_splice
;
1598 /* Handle stream on the relayd if the output is on the network */
1600 if (stream
->metadata_flag
) {
1601 size_t metadata_payload_size
=
1602 sizeof(struct lttcomm_relayd_metadata_payload
);
1604 /* Update counter to fit the spliced data */
1605 ret_splice
+= metadata_payload_size
;
1606 len
+= metadata_payload_size
;
1608 * We do this so the return value can match the len passed as
1609 * argument to this function.
1611 written
-= metadata_payload_size
;
1615 /* Splice data out */
1616 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1617 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1618 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1619 if (ret_splice
< 0) {
1620 PERROR("Error in file splice");
1622 written
= ret_splice
;
1624 /* Socket operation failed. We consider the relayd dead */
1625 if (errno
== EBADF
|| errno
== EPIPE
) {
1626 WARN("Remote relayd disconnected. Stopping");
1632 } else if (ret_splice
> len
) {
1634 PERROR("Wrote more data than requested %zd (len: %lu)",
1636 written
+= ret_splice
;
1642 /* This call is useless on a socket so better save a syscall. */
1644 /* This won't block, but will start writeout asynchronously */
1645 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1646 SYNC_FILE_RANGE_WRITE
);
1647 stream
->out_fd_offset
+= ret_splice
;
1649 written
+= ret_splice
;
1651 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1659 * This is a special case that the relayd has closed its socket. Let's
1660 * cleanup the relayd object and all associated streams.
1662 if (relayd
&& relayd_hang_up
) {
1663 cleanup_relayd(relayd
, ctx
);
1664 /* Skip splice error so the consumer does not fail */
1669 /* send the appropriate error description to sessiond */
1672 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1675 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1678 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1683 if (relayd
&& stream
->metadata_flag
) {
1684 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1692 * Take a snapshot for a specific fd
1694 * Returns 0 on success, < 0 on error
1696 int lttng_consumer_take_snapshot(struct lttng_consumer_local_data
*ctx
,
1697 struct lttng_consumer_stream
*stream
)
1699 switch (consumer_data
.type
) {
1700 case LTTNG_CONSUMER_KERNEL
:
1701 return lttng_kconsumer_take_snapshot(ctx
, stream
);
1702 case LTTNG_CONSUMER32_UST
:
1703 case LTTNG_CONSUMER64_UST
:
1704 return lttng_ustconsumer_take_snapshot(ctx
, stream
);
1706 ERR("Unknown consumer_data type");
1714 * Get the produced position
1716 * Returns 0 on success, < 0 on error
1718 int lttng_consumer_get_produced_snapshot(
1719 struct lttng_consumer_local_data
*ctx
,
1720 struct lttng_consumer_stream
*stream
,
1723 switch (consumer_data
.type
) {
1724 case LTTNG_CONSUMER_KERNEL
:
1725 return lttng_kconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1726 case LTTNG_CONSUMER32_UST
:
1727 case LTTNG_CONSUMER64_UST
:
1728 return lttng_ustconsumer_get_produced_snapshot(ctx
, stream
, pos
);
1730 ERR("Unknown consumer_data type");
1736 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1737 int sock
, struct pollfd
*consumer_sockpoll
)
1739 switch (consumer_data
.type
) {
1740 case LTTNG_CONSUMER_KERNEL
:
1741 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1742 case LTTNG_CONSUMER32_UST
:
1743 case LTTNG_CONSUMER64_UST
:
1744 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1746 ERR("Unknown consumer_data type");
1753 * Iterate over all streams of the hashtable and free them properly.
1755 * WARNING: *MUST* be used with data stream only.
1757 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1759 struct lttng_ht_iter iter
;
1760 struct lttng_consumer_stream
*stream
;
1767 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1769 * Ignore return value since we are currently cleaning up so any error
1772 (void) consumer_del_stream(stream
, ht
);
1776 lttng_ht_destroy(ht
);
1780 * Iterate over all streams of the hashtable and free them properly.
1782 * XXX: Should not be only for metadata stream or else use an other name.
1784 static void destroy_stream_ht(struct lttng_ht
*ht
)
1786 struct lttng_ht_iter iter
;
1787 struct lttng_consumer_stream
*stream
;
1794 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1796 * Ignore return value since we are currently cleaning up so any error
1799 (void) consumer_del_metadata_stream(stream
, ht
);
1803 lttng_ht_destroy(ht
);
1807 * Clean up a metadata stream and free its memory.
1809 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1810 struct lttng_ht
*ht
)
1813 struct lttng_ht_iter iter
;
1814 struct lttng_consumer_channel
*free_chan
= NULL
;
1815 struct consumer_relayd_sock_pair
*relayd
;
1819 * This call should NEVER receive regular stream. It must always be
1820 * metadata stream and this is crucial for data structure synchronization.
1822 assert(stream
->metadata_flag
);
1824 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1827 /* Means the stream was allocated but not successfully added */
1831 pthread_mutex_lock(&consumer_data
.lock
);
1832 pthread_mutex_lock(&stream
->lock
);
1834 switch (consumer_data
.type
) {
1835 case LTTNG_CONSUMER_KERNEL
:
1836 if (stream
->mmap_base
!= NULL
) {
1837 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1839 PERROR("munmap metadata stream");
1843 case LTTNG_CONSUMER32_UST
:
1844 case LTTNG_CONSUMER64_UST
:
1845 lttng_ustconsumer_del_stream(stream
);
1848 ERR("Unknown consumer_data type");
1854 iter
.iter
.node
= &stream
->node
.node
;
1855 ret
= lttng_ht_del(ht
, &iter
);
1858 /* Remove node session id from the consumer_data stream ht */
1859 iter
.iter
.node
= &stream
->node_session_id
.node
;
1860 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1864 if (stream
->out_fd
>= 0) {
1865 ret
= close(stream
->out_fd
);
1871 if (stream
->wait_fd
>= 0 && !stream
->wait_fd_is_copy
) {
1872 ret
= close(stream
->wait_fd
);
1878 if (stream
->shm_fd
>= 0 && stream
->wait_fd
!= stream
->shm_fd
) {
1879 ret
= close(stream
->shm_fd
);
1885 /* Check and cleanup relayd */
1887 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1888 if (relayd
!= NULL
) {
1889 uatomic_dec(&relayd
->refcount
);
1890 assert(uatomic_read(&relayd
->refcount
) >= 0);
1892 /* Closing streams requires to lock the control socket. */
1893 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1894 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1895 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1896 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1898 DBG("Unable to close stream on the relayd. Continuing");
1900 * Continue here. There is nothing we can do for the relayd.
1901 * Chances are that the relayd has closed the socket so we just
1902 * continue cleaning up.
1906 /* Both conditions are met, we destroy the relayd. */
1907 if (uatomic_read(&relayd
->refcount
) == 0 &&
1908 uatomic_read(&relayd
->destroy_flag
)) {
1909 destroy_relayd(relayd
);
1914 /* Atomically decrement channel refcount since other threads can use it. */
1915 uatomic_dec(&stream
->chan
->refcount
);
1916 if (!uatomic_read(&stream
->chan
->refcount
)
1917 && !uatomic_read(&stream
->chan
->nb_init_streams
)) {
1918 /* Go for channel deletion! */
1919 free_chan
= stream
->chan
;
1923 pthread_mutex_unlock(&stream
->lock
);
1924 pthread_mutex_unlock(&consumer_data
.lock
);
1927 consumer_del_channel(free_chan
);
1931 call_rcu(&stream
->node
.head
, consumer_free_stream
);
1935 * Action done with the metadata stream when adding it to the consumer internal
1936 * data structures to handle it.
1938 static int consumer_add_metadata_stream(struct lttng_consumer_stream
*stream
,
1939 struct lttng_ht
*ht
)
1942 struct consumer_relayd_sock_pair
*relayd
;
1943 struct lttng_ht_iter iter
;
1944 struct lttng_ht_node_ulong
*node
;
1949 DBG3("Adding metadata stream %d to hash table", stream
->wait_fd
);
1951 pthread_mutex_lock(&consumer_data
.lock
);
1952 pthread_mutex_lock(&stream
->lock
);
1955 * From here, refcounts are updated so be _careful_ when returning an error
1962 * Lookup the stream just to make sure it does not exist in our internal
1963 * state. This should NEVER happen.
1965 lttng_ht_lookup(ht
, (void *)((unsigned long) stream
->wait_fd
), &iter
);
1966 node
= lttng_ht_iter_get_node_ulong(&iter
);
1969 /* Find relayd and, if one is found, increment refcount. */
1970 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1971 if (relayd
!= NULL
) {
1972 uatomic_inc(&relayd
->refcount
);
1975 /* Update channel refcount once added without error(s). */
1976 uatomic_inc(&stream
->chan
->refcount
);
1979 * When nb_init_streams reaches 0, we don't need to trigger any action in
1980 * terms of destroying the associated channel, because the action that
1981 * causes the count to become 0 also causes a stream to be added. The
1982 * channel deletion will thus be triggered by the following removal of this
1985 if (uatomic_read(&stream
->chan
->nb_init_streams
) > 0) {
1986 uatomic_dec(&stream
->chan
->nb_init_streams
);
1989 lttng_ht_add_unique_ulong(ht
, &stream
->node
);
1992 * Add stream to the stream_list_ht of the consumer data. No need to steal
1993 * the key since the HT does not use it and we allow to add redundant keys
1996 lttng_ht_add_ulong(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
2000 pthread_mutex_unlock(&stream
->lock
);
2001 pthread_mutex_unlock(&consumer_data
.lock
);
2006 * Delete data stream that are flagged for deletion (endpoint_status).
2008 static void validate_endpoint_status_data_stream(void)
2010 struct lttng_ht_iter iter
;
2011 struct lttng_consumer_stream
*stream
;
2013 DBG("Consumer delete flagged data stream");
2016 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2017 /* Validate delete flag of the stream */
2018 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2021 /* Delete it right now */
2022 consumer_del_stream(stream
, data_ht
);
2028 * Delete metadata stream that are flagged for deletion (endpoint_status).
2030 static void validate_endpoint_status_metadata_stream(
2031 struct lttng_poll_event
*pollset
)
2033 struct lttng_ht_iter iter
;
2034 struct lttng_consumer_stream
*stream
;
2036 DBG("Consumer delete flagged metadata stream");
2041 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
2042 /* Validate delete flag of the stream */
2043 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
2047 * Remove from pollset so the metadata thread can continue without
2048 * blocking on a deleted stream.
2050 lttng_poll_del(pollset
, stream
->wait_fd
);
2052 /* Delete it right now */
2053 consumer_del_metadata_stream(stream
, metadata_ht
);
2059 * Thread polls on metadata file descriptor and write them on disk or on the
2062 void *consumer_thread_metadata_poll(void *data
)
2065 uint32_t revents
, nb_fd
;
2066 struct lttng_consumer_stream
*stream
= NULL
;
2067 struct lttng_ht_iter iter
;
2068 struct lttng_ht_node_ulong
*node
;
2069 struct lttng_poll_event events
;
2070 struct lttng_consumer_local_data
*ctx
= data
;
2073 rcu_register_thread();
2075 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2077 /* ENOMEM at this point. Better to bail out. */
2081 DBG("Thread metadata poll started");
2083 /* Size is set to 1 for the consumer_metadata pipe */
2084 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2086 ERR("Poll set creation failed");
2090 ret
= lttng_poll_add(&events
, ctx
->consumer_metadata_pipe
[0], LPOLLIN
);
2096 DBG("Metadata main loop started");
2099 lttng_poll_reset(&events
);
2101 nb_fd
= LTTNG_POLL_GETNB(&events
);
2103 /* Only the metadata pipe is set */
2104 if (nb_fd
== 0 && consumer_quit
== 1) {
2109 DBG("Metadata poll wait with %d fd(s)", nb_fd
);
2110 ret
= lttng_poll_wait(&events
, -1);
2111 DBG("Metadata event catched in thread");
2113 if (errno
== EINTR
) {
2114 ERR("Poll EINTR catched");
2120 /* From here, the event is a metadata wait fd */
2121 for (i
= 0; i
< nb_fd
; i
++) {
2122 revents
= LTTNG_POLL_GETEV(&events
, i
);
2123 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2125 /* Just don't waste time if no returned events for the fd */
2130 if (pollfd
== ctx
->consumer_metadata_pipe
[0]) {
2131 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2132 DBG("Metadata thread pipe hung up");
2134 * Remove the pipe from the poll set and continue the loop
2135 * since their might be data to consume.
2137 lttng_poll_del(&events
, ctx
->consumer_metadata_pipe
[0]);
2138 ret
= close(ctx
->consumer_metadata_pipe
[0]);
2140 PERROR("close metadata pipe");
2143 } else if (revents
& LPOLLIN
) {
2145 /* Get the stream pointer received */
2146 ret
= read(pollfd
, &stream
, sizeof(stream
));
2147 } while (ret
< 0 && errno
== EINTR
);
2149 ret
< sizeof(struct lttng_consumer_stream
*)) {
2150 PERROR("read metadata stream");
2152 * Let's continue here and hope we can still work
2153 * without stopping the consumer. XXX: Should we?
2158 /* A NULL stream means that the state has changed. */
2159 if (stream
== NULL
) {
2160 /* Check for deleted streams. */
2161 validate_endpoint_status_metadata_stream(&events
);
2165 DBG("Adding metadata stream %d to poll set",
2168 ret
= consumer_add_metadata_stream(stream
, metadata_ht
);
2170 ERR("Unable to add metadata stream");
2171 /* Stream was not setup properly. Continuing. */
2172 consumer_del_metadata_stream(stream
, NULL
);
2176 /* Add metadata stream to the global poll events list */
2177 lttng_poll_add(&events
, stream
->wait_fd
,
2178 LPOLLIN
| LPOLLPRI
);
2181 /* Handle other stream */
2186 lttng_ht_lookup(metadata_ht
, (void *)((unsigned long) pollfd
),
2188 node
= lttng_ht_iter_get_node_ulong(&iter
);
2191 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2194 /* Check for error event */
2195 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2196 DBG("Metadata fd %d is hup|err.", pollfd
);
2197 if (!stream
->hangup_flush_done
2198 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2199 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2200 DBG("Attempting to flush and consume the UST buffers");
2201 lttng_ustconsumer_on_stream_hangup(stream
);
2203 /* We just flushed the stream now read it. */
2205 len
= ctx
->on_buffer_ready(stream
, ctx
);
2207 * We don't check the return value here since if we get
2208 * a negative len, it means an error occured thus we
2209 * simply remove it from the poll set and free the
2215 lttng_poll_del(&events
, stream
->wait_fd
);
2217 * This call update the channel states, closes file descriptors
2218 * and securely free the stream.
2220 consumer_del_metadata_stream(stream
, metadata_ht
);
2221 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2222 /* Get the data out of the metadata file descriptor */
2223 DBG("Metadata available on fd %d", pollfd
);
2224 assert(stream
->wait_fd
== pollfd
);
2226 len
= ctx
->on_buffer_ready(stream
, ctx
);
2227 /* It's ok to have an unavailable sub-buffer */
2228 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2229 /* Clean up stream from consumer and free it. */
2230 lttng_poll_del(&events
, stream
->wait_fd
);
2231 consumer_del_metadata_stream(stream
, metadata_ht
);
2232 } else if (len
> 0) {
2233 stream
->data_read
= 1;
2237 /* Release RCU lock for the stream looked up */
2244 DBG("Metadata poll thread exiting");
2245 lttng_poll_clean(&events
);
2247 destroy_stream_ht(metadata_ht
);
2249 rcu_unregister_thread();
2254 * This thread polls the fds in the set to consume the data and write
2255 * it to tracefile if necessary.
2257 void *consumer_thread_data_poll(void *data
)
2259 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2260 struct pollfd
*pollfd
= NULL
;
2261 /* local view of the streams */
2262 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2263 /* local view of consumer_data.fds_count */
2265 struct lttng_consumer_local_data
*ctx
= data
;
2268 rcu_register_thread();
2270 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2271 if (data_ht
== NULL
) {
2272 /* ENOMEM at this point. Better to bail out. */
2276 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
2283 * the fds set has been updated, we need to update our
2284 * local array as well
2286 pthread_mutex_lock(&consumer_data
.lock
);
2287 if (consumer_data
.need_update
) {
2288 if (pollfd
!= NULL
) {
2292 if (local_stream
!= NULL
) {
2294 local_stream
= NULL
;
2297 /* allocate for all fds + 1 for the consumer_data_pipe */
2298 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2299 if (pollfd
== NULL
) {
2300 PERROR("pollfd malloc");
2301 pthread_mutex_unlock(&consumer_data
.lock
);
2305 /* allocate for all fds + 1 for the consumer_data_pipe */
2306 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2307 sizeof(struct lttng_consumer_stream
));
2308 if (local_stream
== NULL
) {
2309 PERROR("local_stream malloc");
2310 pthread_mutex_unlock(&consumer_data
.lock
);
2313 ret
= consumer_update_poll_array(ctx
, &pollfd
, local_stream
,
2316 ERR("Error in allocating pollfd or local_outfds");
2317 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2318 pthread_mutex_unlock(&consumer_data
.lock
);
2322 consumer_data
.need_update
= 0;
2324 pthread_mutex_unlock(&consumer_data
.lock
);
2326 /* No FDs and consumer_quit, consumer_cleanup the thread */
2327 if (nb_fd
== 0 && consumer_quit
== 1) {
2330 /* poll on the array of fds */
2332 DBG("polling on %d fd", nb_fd
+ 1);
2333 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2334 DBG("poll num_rdy : %d", num_rdy
);
2335 if (num_rdy
== -1) {
2337 * Restart interrupted system call.
2339 if (errno
== EINTR
) {
2342 PERROR("Poll error");
2343 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2345 } else if (num_rdy
== 0) {
2346 DBG("Polling thread timed out");
2351 * If the consumer_data_pipe triggered poll go directly to the
2352 * beginning of the loop to update the array. We want to prioritize
2353 * array update over low-priority reads.
2355 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2356 size_t pipe_readlen
;
2358 DBG("consumer_data_pipe wake up");
2359 /* Consume 1 byte of pipe data */
2361 pipe_readlen
= read(ctx
->consumer_data_pipe
[0], &new_stream
,
2362 sizeof(new_stream
));
2363 } while (pipe_readlen
== -1 && errno
== EINTR
);
2366 * If the stream is NULL, just ignore it. It's also possible that
2367 * the sessiond poll thread changed the consumer_quit state and is
2368 * waking us up to test it.
2370 if (new_stream
== NULL
) {
2371 validate_endpoint_status_data_stream();
2375 ret
= consumer_add_stream(new_stream
, data_ht
);
2377 ERR("Consumer add stream %d failed. Continuing",
2380 * At this point, if the add_stream fails, it is not in the
2381 * hash table thus passing the NULL value here.
2383 consumer_del_stream(new_stream
, NULL
);
2386 /* Continue to update the local streams and handle prio ones */
2390 /* Take care of high priority channels first. */
2391 for (i
= 0; i
< nb_fd
; i
++) {
2392 if (local_stream
[i
] == NULL
) {
2395 if (pollfd
[i
].revents
& POLLPRI
) {
2396 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2398 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2399 /* it's ok to have an unavailable sub-buffer */
2400 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2401 /* Clean the stream and free it. */
2402 consumer_del_stream(local_stream
[i
], data_ht
);
2403 local_stream
[i
] = NULL
;
2404 } else if (len
> 0) {
2405 local_stream
[i
]->data_read
= 1;
2411 * If we read high prio channel in this loop, try again
2412 * for more high prio data.
2418 /* Take care of low priority channels. */
2419 for (i
= 0; i
< nb_fd
; i
++) {
2420 if (local_stream
[i
] == NULL
) {
2423 if ((pollfd
[i
].revents
& POLLIN
) ||
2424 local_stream
[i
]->hangup_flush_done
) {
2425 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2426 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2427 /* it's ok to have an unavailable sub-buffer */
2428 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2429 /* Clean the stream and free it. */
2430 consumer_del_stream(local_stream
[i
], data_ht
);
2431 local_stream
[i
] = NULL
;
2432 } else if (len
> 0) {
2433 local_stream
[i
]->data_read
= 1;
2438 /* Handle hangup and errors */
2439 for (i
= 0; i
< nb_fd
; i
++) {
2440 if (local_stream
[i
] == NULL
) {
2443 if (!local_stream
[i
]->hangup_flush_done
2444 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2445 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2446 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2447 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2449 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2450 /* Attempt read again, for the data we just flushed. */
2451 local_stream
[i
]->data_read
= 1;
2454 * If the poll flag is HUP/ERR/NVAL and we have
2455 * read no data in this pass, we can remove the
2456 * stream from its hash table.
2458 if ((pollfd
[i
].revents
& POLLHUP
)) {
2459 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2460 if (!local_stream
[i
]->data_read
) {
2461 consumer_del_stream(local_stream
[i
], data_ht
);
2462 local_stream
[i
] = NULL
;
2465 } else if (pollfd
[i
].revents
& POLLERR
) {
2466 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2467 if (!local_stream
[i
]->data_read
) {
2468 consumer_del_stream(local_stream
[i
], data_ht
);
2469 local_stream
[i
] = NULL
;
2472 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2473 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2474 if (!local_stream
[i
]->data_read
) {
2475 consumer_del_stream(local_stream
[i
], data_ht
);
2476 local_stream
[i
] = NULL
;
2480 if (local_stream
[i
] != NULL
) {
2481 local_stream
[i
]->data_read
= 0;
2486 DBG("polling thread exiting");
2487 if (pollfd
!= NULL
) {
2491 if (local_stream
!= NULL
) {
2493 local_stream
= NULL
;
2497 * Close the write side of the pipe so epoll_wait() in
2498 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2499 * read side of the pipe. If we close them both, epoll_wait strangely does
2500 * not return and could create a endless wait period if the pipe is the
2501 * only tracked fd in the poll set. The thread will take care of closing
2504 ret
= close(ctx
->consumer_metadata_pipe
[1]);
2506 PERROR("close data pipe");
2509 destroy_data_stream_ht(data_ht
);
2511 rcu_unregister_thread();
2516 * This thread listens on the consumerd socket and receives the file
2517 * descriptors from the session daemon.
2519 void *consumer_thread_sessiond_poll(void *data
)
2521 int sock
= -1, client_socket
, ret
;
2523 * structure to poll for incoming data on communication socket avoids
2524 * making blocking sockets.
2526 struct pollfd consumer_sockpoll
[2];
2527 struct lttng_consumer_local_data
*ctx
= data
;
2529 rcu_register_thread();
2531 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2532 unlink(ctx
->consumer_command_sock_path
);
2533 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2534 if (client_socket
< 0) {
2535 ERR("Cannot create command socket");
2539 ret
= lttcomm_listen_unix_sock(client_socket
);
2544 DBG("Sending ready command to lttng-sessiond");
2545 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2546 /* return < 0 on error, but == 0 is not fatal */
2548 ERR("Error sending ready command to lttng-sessiond");
2552 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2554 PERROR("fcntl O_NONBLOCK");
2558 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2559 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2560 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2561 consumer_sockpoll
[1].fd
= client_socket
;
2562 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2564 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2567 DBG("Connection on client_socket");
2569 /* Blocking call, waiting for transmission */
2570 sock
= lttcomm_accept_unix_sock(client_socket
);
2575 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2577 PERROR("fcntl O_NONBLOCK");
2581 /* This socket is not useful anymore. */
2582 ret
= close(client_socket
);
2584 PERROR("close client_socket");
2588 /* update the polling structure to poll on the established socket */
2589 consumer_sockpoll
[1].fd
= sock
;
2590 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2593 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2596 DBG("Incoming command on sock");
2597 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2598 if (ret
== -ENOENT
) {
2599 DBG("Received STOP command");
2604 * This could simply be a session daemon quitting. Don't output
2607 DBG("Communication interrupted on command socket");
2610 if (consumer_quit
) {
2611 DBG("consumer_thread_receive_fds received quit from signal");
2614 DBG("received fds on sock");
2617 DBG("consumer_thread_receive_fds exiting");
2620 * when all fds have hung up, the polling thread
2626 * Notify the data poll thread to poll back again and test the
2627 * consumer_quit state that we just set so to quit gracefully.
2629 notify_thread_pipe(ctx
->consumer_data_pipe
[1]);
2631 /* Cleaning up possibly open sockets. */
2635 PERROR("close sock sessiond poll");
2638 if (client_socket
>= 0) {
2641 PERROR("close client_socket sessiond poll");
2645 rcu_unregister_thread();
2649 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2650 struct lttng_consumer_local_data
*ctx
)
2654 pthread_mutex_lock(&stream
->lock
);
2656 switch (consumer_data
.type
) {
2657 case LTTNG_CONSUMER_KERNEL
:
2658 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
2660 case LTTNG_CONSUMER32_UST
:
2661 case LTTNG_CONSUMER64_UST
:
2662 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2665 ERR("Unknown consumer_data type");
2671 pthread_mutex_unlock(&stream
->lock
);
2675 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2677 switch (consumer_data
.type
) {
2678 case LTTNG_CONSUMER_KERNEL
:
2679 return lttng_kconsumer_on_recv_stream(stream
);
2680 case LTTNG_CONSUMER32_UST
:
2681 case LTTNG_CONSUMER64_UST
:
2682 return lttng_ustconsumer_on_recv_stream(stream
);
2684 ERR("Unknown consumer_data type");
2691 * Allocate and set consumer data hash tables.
2693 void lttng_consumer_init(void)
2695 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2696 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2697 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2698 relayd_session_id_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
2702 * Process the ADD_RELAYD command receive by a consumer.
2704 * This will create a relayd socket pair and add it to the relayd hash table.
2705 * The caller MUST acquire a RCU read side lock before calling it.
2707 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
2708 struct lttng_consumer_local_data
*ctx
, int sock
,
2709 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
,
2710 unsigned int sessiond_id
)
2712 int fd
= -1, ret
= -1;
2713 enum lttng_error_code ret_code
= LTTNG_OK
;
2714 struct consumer_relayd_sock_pair
*relayd
;
2715 struct consumer_relayd_session_id
*relayd_id_node
;
2717 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
2719 /* First send a status message before receiving the fds. */
2720 ret
= consumer_send_status_msg(sock
, ret_code
);
2722 /* Somehow, the session daemon is not responding anymore. */
2726 /* Get relayd reference if exists. */
2727 relayd
= consumer_find_relayd(net_seq_idx
);
2728 if (relayd
== NULL
) {
2729 /* Not found. Allocate one. */
2730 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
2731 if (relayd
== NULL
) {
2732 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
2737 /* Poll on consumer socket. */
2738 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2743 /* Get relayd socket from session daemon */
2744 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2745 if (ret
!= sizeof(fd
)) {
2746 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
2748 fd
= -1; /* Just in case it gets set with an invalid value. */
2752 /* We have the fds without error. Send status back. */
2753 ret
= consumer_send_status_msg(sock
, ret_code
);
2755 /* Somehow, the session daemon is not responding anymore. */
2759 /* Copy socket information and received FD */
2760 switch (sock_type
) {
2761 case LTTNG_STREAM_CONTROL
:
2762 /* Copy received lttcomm socket */
2763 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
2764 ret
= lttcomm_create_sock(&relayd
->control_sock
);
2765 /* Immediately try to close the created socket if valid. */
2766 if (relayd
->control_sock
.fd
>= 0) {
2767 if (close(relayd
->control_sock
.fd
)) {
2768 PERROR("close relayd control socket");
2771 /* Handle create_sock error. */
2776 /* Assign new file descriptor */
2777 relayd
->control_sock
.fd
= fd
;
2780 * Create a session on the relayd and store the returned id. No need to
2781 * grab the socket lock since the relayd object is not yet visible.
2783 ret
= relayd_create_session(&relayd
->control_sock
,
2784 &relayd
->session_id
);
2789 /* Set up a relayd session id node. */
2790 relayd_id_node
= zmalloc(sizeof(struct consumer_relayd_session_id
));
2791 if (!relayd_id_node
) {
2792 PERROR("zmalloc relayd id node");
2796 relayd_id_node
->relayd_id
= relayd
->session_id
;
2797 relayd_id_node
->sessiond_id
= (uint64_t) sessiond_id
;
2799 /* Indexed by session id of the session daemon. */
2800 lttng_ht_node_init_ulong(&relayd_id_node
->node
,
2801 relayd_id_node
->sessiond_id
);
2803 lttng_ht_add_unique_ulong(relayd_session_id_ht
, &relayd_id_node
->node
);
2807 case LTTNG_STREAM_DATA
:
2808 /* Copy received lttcomm socket */
2809 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
2810 ret
= lttcomm_create_sock(&relayd
->data_sock
);
2811 /* Immediately try to close the created socket if valid. */
2812 if (relayd
->data_sock
.fd
>= 0) {
2813 if (close(relayd
->data_sock
.fd
)) {
2814 PERROR("close relayd data socket");
2817 /* Handle create_sock error. */
2822 /* Assign new file descriptor */
2823 relayd
->data_sock
.fd
= fd
;
2826 ERR("Unknown relayd socket type (%d)", sock_type
);
2830 DBG("Consumer %s socket created successfully with net idx %d (fd: %d)",
2831 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
2832 relayd
->net_seq_idx
, fd
);
2835 * Add relayd socket pair to consumer data hashtable. If object already
2836 * exists or on error, the function gracefully returns.
2844 /* Close received socket if valid. */
2847 PERROR("close received socket");
2854 * Try to lock the stream mutex.
2856 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
2858 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
2865 * Try to lock the stream mutex. On failure, we know that the stream is
2866 * being used else where hence there is data still being extracted.
2868 ret
= pthread_mutex_trylock(&stream
->lock
);
2870 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
2882 * Check if for a given session id there is still data needed to be extract
2885 * Return 1 if data is pending or else 0 meaning ready to be read.
2887 int consumer_data_pending(uint64_t id
)
2890 struct lttng_ht_iter iter
;
2891 struct lttng_ht
*ht
;
2892 struct lttng_consumer_stream
*stream
;
2893 struct consumer_relayd_sock_pair
*relayd
;
2894 int (*data_pending
)(struct lttng_consumer_stream
*);
2896 DBG("Consumer data pending command on session id %" PRIu64
, id
);
2899 pthread_mutex_lock(&consumer_data
.lock
);
2901 switch (consumer_data
.type
) {
2902 case LTTNG_CONSUMER_KERNEL
:
2903 data_pending
= lttng_kconsumer_data_pending
;
2905 case LTTNG_CONSUMER32_UST
:
2906 case LTTNG_CONSUMER64_UST
:
2907 data_pending
= lttng_ustconsumer_data_pending
;
2910 ERR("Unknown consumer data type");
2914 /* Ease our life a bit */
2915 ht
= consumer_data
.stream_list_ht
;
2917 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2918 ht
->hash_fct((void *)((unsigned long) id
), lttng_ht_seed
),
2919 ht
->match_fct
, (void *)((unsigned long) id
),
2920 &iter
.iter
, stream
, node_session_id
.node
) {
2921 /* If this call fails, the stream is being used hence data pending. */
2922 ret
= stream_try_lock(stream
);
2924 goto data_not_pending
;
2928 * A removed node from the hash table indicates that the stream has
2929 * been deleted thus having a guarantee that the buffers are closed
2930 * on the consumer side. However, data can still be transmitted
2931 * over the network so don't skip the relayd check.
2933 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
2935 /* Check the stream if there is data in the buffers. */
2936 ret
= data_pending(stream
);
2938 pthread_mutex_unlock(&stream
->lock
);
2939 goto data_not_pending
;
2944 if (stream
->net_seq_idx
!= -1) {
2945 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
2948 * At this point, if the relayd object is not available for the
2949 * given stream, it is because the relayd is being cleaned up
2950 * so every stream associated with it (for a session id value)
2951 * are or will be marked for deletion hence no data pending.
2953 pthread_mutex_unlock(&stream
->lock
);
2954 goto data_not_pending
;
2957 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
2958 if (stream
->metadata_flag
) {
2959 ret
= relayd_quiescent_control(&relayd
->control_sock
);
2961 ret
= relayd_data_pending(&relayd
->control_sock
,
2962 stream
->relayd_stream_id
, stream
->next_net_seq_num
);
2964 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2966 pthread_mutex_unlock(&stream
->lock
);
2967 goto data_not_pending
;
2970 pthread_mutex_unlock(&stream
->lock
);
2974 * Finding _no_ node in the hash table means that the stream(s) have been
2975 * removed thus data is guaranteed to be available for analysis from the
2976 * trace files. This is *only* true for local consumer and not network
2980 /* Data is available to be read by a viewer. */
2981 pthread_mutex_unlock(&consumer_data
.lock
);
2986 /* Data is still being extracted from buffers. */
2987 pthread_mutex_unlock(&consumer_data
.lock
);
2993 * Send a ret code status message to the sessiond daemon.
2995 * Return the sendmsg() return value.
2997 int consumer_send_status_msg(int sock
, int ret_code
)
2999 struct lttcomm_consumer_status_msg msg
;
3001 msg
.ret_code
= ret_code
;
3003 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));