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 * Notify a thread pipe to poll back again. This usually means that some global
68 * state has changed so we just send back the thread in a poll wait call.
70 static void notify_thread_pipe(int wpipe
)
75 struct lttng_consumer_stream
*null_stream
= NULL
;
77 ret
= write(wpipe
, &null_stream
, sizeof(null_stream
));
78 } while (ret
< 0 && errno
== EINTR
);
82 * Find a stream. The consumer_data.lock must be locked during this
85 static struct lttng_consumer_stream
*find_stream(uint64_t key
,
88 struct lttng_ht_iter iter
;
89 struct lttng_ht_node_u64
*node
;
90 struct lttng_consumer_stream
*stream
= NULL
;
94 /* -1ULL keys are lookup failures */
95 if (key
== (uint64_t) -1ULL) {
101 lttng_ht_lookup(ht
, &key
, &iter
);
102 node
= lttng_ht_iter_get_node_u64(&iter
);
104 stream
= caa_container_of(node
, struct lttng_consumer_stream
, node
);
112 static void steal_stream_key(int key
, struct lttng_ht
*ht
)
114 struct lttng_consumer_stream
*stream
;
117 stream
= find_stream(key
, ht
);
121 * We don't want the lookup to match, but we still need
122 * to iterate on this stream when iterating over the hash table. Just
123 * change the node key.
125 stream
->node
.key
= -1ULL;
131 * Return a channel object for the given key.
133 * RCU read side lock MUST be acquired before calling this function and
134 * protects the channel ptr.
136 struct lttng_consumer_channel
*consumer_find_channel(uint64_t key
)
138 struct lttng_ht_iter iter
;
139 struct lttng_ht_node_u64
*node
;
140 struct lttng_consumer_channel
*channel
= NULL
;
142 /* -1ULL keys are lookup failures */
143 if (key
== (uint64_t) -1ULL) {
147 lttng_ht_lookup(consumer_data
.channel_ht
, &key
, &iter
);
148 node
= lttng_ht_iter_get_node_u64(&iter
);
150 channel
= caa_container_of(node
, struct lttng_consumer_channel
, node
);
156 static void free_stream_rcu(struct rcu_head
*head
)
158 struct lttng_ht_node_u64
*node
=
159 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
160 struct lttng_consumer_stream
*stream
=
161 caa_container_of(node
, struct lttng_consumer_stream
, node
);
166 static void free_channel_rcu(struct rcu_head
*head
)
168 struct lttng_ht_node_u64
*node
=
169 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
170 struct lttng_consumer_channel
*channel
=
171 caa_container_of(node
, struct lttng_consumer_channel
, node
);
177 * RCU protected relayd socket pair free.
179 static void free_relayd_rcu(struct rcu_head
*head
)
181 struct lttng_ht_node_u64
*node
=
182 caa_container_of(head
, struct lttng_ht_node_u64
, head
);
183 struct consumer_relayd_sock_pair
*relayd
=
184 caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
187 * Close all sockets. This is done in the call RCU since we don't want the
188 * socket fds to be reassigned thus potentially creating bad state of the
191 * We do not have to lock the control socket mutex here since at this stage
192 * there is no one referencing to this relayd object.
194 (void) relayd_close(&relayd
->control_sock
);
195 (void) relayd_close(&relayd
->data_sock
);
201 * Destroy and free relayd socket pair object.
203 * This function MUST be called with the consumer_data lock acquired.
205 static void destroy_relayd(struct consumer_relayd_sock_pair
*relayd
)
208 struct lttng_ht_iter iter
;
210 if (relayd
== NULL
) {
214 DBG("Consumer destroy and close relayd socket pair");
216 iter
.iter
.node
= &relayd
->node
.node
;
217 ret
= lttng_ht_del(consumer_data
.relayd_ht
, &iter
);
219 /* We assume the relayd is being or is destroyed */
223 /* RCU free() call */
224 call_rcu(&relayd
->node
.head
, free_relayd_rcu
);
228 * Remove a channel from the global list protected by a mutex. This function is
229 * also responsible for freeing its data structures.
231 void consumer_del_channel(struct lttng_consumer_channel
*channel
)
234 struct lttng_ht_iter iter
;
236 DBG("Consumer delete channel key %" PRIu64
, channel
->key
);
238 pthread_mutex_lock(&consumer_data
.lock
);
240 switch (consumer_data
.type
) {
241 case LTTNG_CONSUMER_KERNEL
:
243 case LTTNG_CONSUMER32_UST
:
244 case LTTNG_CONSUMER64_UST
:
245 lttng_ustconsumer_del_channel(channel
);
248 ERR("Unknown consumer_data type");
254 iter
.iter
.node
= &channel
->node
.node
;
255 ret
= lttng_ht_del(consumer_data
.channel_ht
, &iter
);
259 call_rcu(&channel
->node
.head
, free_channel_rcu
);
261 pthread_mutex_unlock(&consumer_data
.lock
);
265 * Iterate over the relayd hash table and destroy each element. Finally,
266 * destroy the whole hash table.
268 static void cleanup_relayd_ht(void)
270 struct lttng_ht_iter iter
;
271 struct consumer_relayd_sock_pair
*relayd
;
275 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
277 destroy_relayd(relayd
);
280 lttng_ht_destroy(consumer_data
.relayd_ht
);
286 * Update the end point status of all streams having the given network sequence
287 * index (relayd index).
289 * It's atomically set without having the stream mutex locked which is fine
290 * because we handle the write/read race with a pipe wakeup for each thread.
292 static void update_endpoint_status_by_netidx(int net_seq_idx
,
293 enum consumer_endpoint_status status
)
295 struct lttng_ht_iter iter
;
296 struct lttng_consumer_stream
*stream
;
298 DBG("Consumer set delete flag on stream by idx %d", net_seq_idx
);
302 /* Let's begin with metadata */
303 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
304 if (stream
->net_seq_idx
== net_seq_idx
) {
305 uatomic_set(&stream
->endpoint_status
, status
);
306 DBG("Delete flag set to metadata stream %d", stream
->wait_fd
);
310 /* Follow up by the data streams */
311 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
312 if (stream
->net_seq_idx
== net_seq_idx
) {
313 uatomic_set(&stream
->endpoint_status
, status
);
314 DBG("Delete flag set to data stream %d", stream
->wait_fd
);
321 * Cleanup a relayd object by flagging every associated streams for deletion,
322 * destroying the object meaning removing it from the relayd hash table,
323 * closing the sockets and freeing the memory in a RCU call.
325 * If a local data context is available, notify the threads that the streams'
326 * state have changed.
328 static void cleanup_relayd(struct consumer_relayd_sock_pair
*relayd
,
329 struct lttng_consumer_local_data
*ctx
)
335 DBG("Cleaning up relayd sockets");
337 /* Save the net sequence index before destroying the object */
338 netidx
= relayd
->net_seq_idx
;
341 * Delete the relayd from the relayd hash table, close the sockets and free
342 * the object in a RCU call.
344 destroy_relayd(relayd
);
346 /* Set inactive endpoint to all streams */
347 update_endpoint_status_by_netidx(netidx
, CONSUMER_ENDPOINT_INACTIVE
);
350 * With a local data context, notify the threads that the streams' state
351 * have changed. The write() action on the pipe acts as an "implicit"
352 * memory barrier ordering the updates of the end point status from the
353 * read of this status which happens AFTER receiving this notify.
356 notify_thread_pipe(ctx
->consumer_data_pipe
[1]);
357 notify_thread_pipe(ctx
->consumer_metadata_pipe
[1]);
362 * Flag a relayd socket pair for destruction. Destroy it if the refcount
365 * RCU read side lock MUST be aquired before calling this function.
367 void consumer_flag_relayd_for_destroy(struct consumer_relayd_sock_pair
*relayd
)
371 /* Set destroy flag for this object */
372 uatomic_set(&relayd
->destroy_flag
, 1);
374 /* Destroy the relayd if refcount is 0 */
375 if (uatomic_read(&relayd
->refcount
) == 0) {
376 destroy_relayd(relayd
);
381 * Remove a stream from the global list protected by a mutex. This
382 * function is also responsible for freeing its data structures.
384 void consumer_del_stream(struct lttng_consumer_stream
*stream
,
388 struct lttng_ht_iter iter
;
389 struct lttng_consumer_channel
*free_chan
= NULL
;
390 struct consumer_relayd_sock_pair
*relayd
;
394 DBG("Consumer del stream %d", stream
->wait_fd
);
397 /* Means the stream was allocated but not successfully added */
398 goto free_stream_rcu
;
401 pthread_mutex_lock(&consumer_data
.lock
);
402 pthread_mutex_lock(&stream
->lock
);
404 switch (consumer_data
.type
) {
405 case LTTNG_CONSUMER_KERNEL
:
406 if (stream
->mmap_base
!= NULL
) {
407 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
413 case LTTNG_CONSUMER32_UST
:
414 case LTTNG_CONSUMER64_UST
:
415 lttng_ustconsumer_del_stream(stream
);
418 ERR("Unknown consumer_data type");
424 iter
.iter
.node
= &stream
->node
.node
;
425 ret
= lttng_ht_del(ht
, &iter
);
428 /* Remove node session id from the consumer_data stream ht */
429 iter
.iter
.node
= &stream
->node_session_id
.node
;
430 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
434 assert(consumer_data
.stream_count
> 0);
435 consumer_data
.stream_count
--;
437 if (stream
->out_fd
>= 0) {
438 ret
= close(stream
->out_fd
);
444 /* Check and cleanup relayd */
446 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
447 if (relayd
!= NULL
) {
448 uatomic_dec(&relayd
->refcount
);
449 assert(uatomic_read(&relayd
->refcount
) >= 0);
451 /* Closing streams requires to lock the control socket. */
452 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
453 ret
= relayd_send_close_stream(&relayd
->control_sock
,
454 stream
->relayd_stream_id
,
455 stream
->next_net_seq_num
- 1);
456 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
458 DBG("Unable to close stream on the relayd. Continuing");
460 * Continue here. There is nothing we can do for the relayd.
461 * Chances are that the relayd has closed the socket so we just
462 * continue cleaning up.
466 /* Both conditions are met, we destroy the relayd. */
467 if (uatomic_read(&relayd
->refcount
) == 0 &&
468 uatomic_read(&relayd
->destroy_flag
)) {
469 destroy_relayd(relayd
);
474 uatomic_dec(&stream
->chan
->refcount
);
475 if (!uatomic_read(&stream
->chan
->refcount
)
476 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
477 free_chan
= stream
->chan
;
481 consumer_data
.need_update
= 1;
482 pthread_mutex_unlock(&stream
->lock
);
483 pthread_mutex_unlock(&consumer_data
.lock
);
486 consumer_del_channel(free_chan
);
490 call_rcu(&stream
->node
.head
, free_stream_rcu
);
493 struct lttng_consumer_stream
*consumer_allocate_stream(uint64_t channel_key
,
495 enum lttng_consumer_stream_state state
,
496 const char *channel_name
,
503 enum consumer_channel_type type
)
506 struct lttng_consumer_stream
*stream
;
508 stream
= zmalloc(sizeof(*stream
));
509 if (stream
== NULL
) {
510 PERROR("malloc struct lttng_consumer_stream");
517 stream
->key
= stream_key
;
519 stream
->out_fd_offset
= 0;
520 stream
->state
= state
;
523 stream
->net_seq_idx
= relayd_id
;
524 stream
->session_id
= session_id
;
525 pthread_mutex_init(&stream
->lock
, NULL
);
527 /* If channel is the metadata, flag this stream as metadata. */
528 if (type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
529 stream
->metadata_flag
= 1;
530 /* Metadata is flat out. */
531 strncpy(stream
->name
, DEFAULT_METADATA_NAME
, sizeof(stream
->name
));
533 /* Format stream name to <channel_name>_<cpu_number> */
534 ret
= snprintf(stream
->name
, sizeof(stream
->name
), "%s_%d",
537 PERROR("snprintf stream name");
542 /* Key is always the wait_fd for streams. */
543 lttng_ht_node_init_u64(&stream
->node
, stream
->key
);
545 /* Init session id node with the stream session id */
546 lttng_ht_node_init_u64(&stream
->node_session_id
, stream
->session_id
);
548 DBG3("Allocated stream %s (key %" PRIu64
", relayd_id %" PRIu64
", session_id %" PRIu64
,
549 stream
->name
, stream
->key
, stream
->net_seq_idx
, stream
->session_id
);
565 * Add a stream to the global list protected by a mutex.
567 static int add_stream(struct lttng_consumer_stream
*stream
,
571 struct consumer_relayd_sock_pair
*relayd
;
576 DBG3("Adding consumer stream %" PRIu64
, stream
->key
);
578 pthread_mutex_lock(&consumer_data
.lock
);
579 pthread_mutex_lock(&stream
->lock
);
582 /* Steal stream identifier to avoid having streams with the same key */
583 steal_stream_key(stream
->key
, ht
);
585 lttng_ht_add_unique_u64(ht
, &stream
->node
);
588 * Add stream to the stream_list_ht of the consumer data. No need to steal
589 * the key since the HT does not use it and we allow to add redundant keys
592 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
594 /* Check and cleanup relayd */
595 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
596 if (relayd
!= NULL
) {
597 uatomic_inc(&relayd
->refcount
);
600 /* Update channel refcount once added without error(s). */
601 uatomic_inc(&stream
->chan
->refcount
);
604 * When nb_init_stream_left reaches 0, we don't need to trigger any action
605 * in terms of destroying the associated channel, because the action that
606 * causes the count to become 0 also causes a stream to be added. The
607 * channel deletion will thus be triggered by the following removal of this
610 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
611 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
614 /* Update consumer data once the node is inserted. */
615 consumer_data
.stream_count
++;
616 consumer_data
.need_update
= 1;
619 pthread_mutex_unlock(&stream
->lock
);
620 pthread_mutex_unlock(&consumer_data
.lock
);
626 * Add relayd socket to global consumer data hashtable. RCU read side lock MUST
627 * be acquired before calling this.
629 static int add_relayd(struct consumer_relayd_sock_pair
*relayd
)
632 struct lttng_ht_node_u64
*node
;
633 struct lttng_ht_iter iter
;
637 lttng_ht_lookup(consumer_data
.relayd_ht
,
638 &relayd
->net_seq_idx
, &iter
);
639 node
= lttng_ht_iter_get_node_u64(&iter
);
643 lttng_ht_add_unique_u64(consumer_data
.relayd_ht
, &relayd
->node
);
650 * Allocate and return a consumer relayd socket.
652 struct consumer_relayd_sock_pair
*consumer_allocate_relayd_sock_pair(
655 struct consumer_relayd_sock_pair
*obj
= NULL
;
657 /* Negative net sequence index is a failure */
658 if (net_seq_idx
< 0) {
662 obj
= zmalloc(sizeof(struct consumer_relayd_sock_pair
));
664 PERROR("zmalloc relayd sock");
668 obj
->net_seq_idx
= net_seq_idx
;
670 obj
->destroy_flag
= 0;
671 lttng_ht_node_init_u64(&obj
->node
, obj
->net_seq_idx
);
672 pthread_mutex_init(&obj
->ctrl_sock_mutex
, NULL
);
679 * Find a relayd socket pair in the global consumer data.
681 * Return the object if found else NULL.
682 * RCU read-side lock must be held across this call and while using the
685 struct consumer_relayd_sock_pair
*consumer_find_relayd(uint64_t key
)
687 struct lttng_ht_iter iter
;
688 struct lttng_ht_node_u64
*node
;
689 struct consumer_relayd_sock_pair
*relayd
= NULL
;
691 /* Negative keys are lookup failures */
692 if (key
== (uint64_t) -1ULL) {
696 lttng_ht_lookup(consumer_data
.relayd_ht
, &key
,
698 node
= lttng_ht_iter_get_node_u64(&iter
);
700 relayd
= caa_container_of(node
, struct consumer_relayd_sock_pair
, node
);
708 * Handle stream for relayd transmission if the stream applies for network
709 * streaming where the net sequence index is set.
711 * Return destination file descriptor or negative value on error.
713 static int write_relayd_stream_header(struct lttng_consumer_stream
*stream
,
714 size_t data_size
, unsigned long padding
,
715 struct consumer_relayd_sock_pair
*relayd
)
718 struct lttcomm_relayd_data_hdr data_hdr
;
724 /* Reset data header */
725 memset(&data_hdr
, 0, sizeof(data_hdr
));
727 if (stream
->metadata_flag
) {
728 /* Caller MUST acquire the relayd control socket lock */
729 ret
= relayd_send_metadata(&relayd
->control_sock
, data_size
);
734 /* Metadata are always sent on the control socket. */
735 outfd
= relayd
->control_sock
.fd
;
737 /* Set header with stream information */
738 data_hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
739 data_hdr
.data_size
= htobe32(data_size
);
740 data_hdr
.padding_size
= htobe32(padding
);
742 * Note that net_seq_num below is assigned with the *current* value of
743 * next_net_seq_num and only after that the next_net_seq_num will be
744 * increment. This is why when issuing a command on the relayd using
745 * this next value, 1 should always be substracted in order to compare
746 * the last seen sequence number on the relayd side to the last sent.
748 data_hdr
.net_seq_num
= htobe64(stream
->next_net_seq_num
);
749 /* Other fields are zeroed previously */
751 ret
= relayd_send_data_hdr(&relayd
->data_sock
, &data_hdr
,
757 ++stream
->next_net_seq_num
;
759 /* Set to go on data socket */
760 outfd
= relayd
->data_sock
.fd
;
768 * Allocate and return a new lttng_consumer_channel object using the given key
769 * to initialize the hash table node.
771 * On error, return NULL.
773 struct lttng_consumer_channel
*consumer_allocate_channel(unsigned long key
,
775 const char *pathname
,
780 enum lttng_event_output output
)
782 struct lttng_consumer_channel
*channel
;
784 channel
= zmalloc(sizeof(*channel
));
785 if (channel
== NULL
) {
786 PERROR("malloc struct lttng_consumer_channel");
791 channel
->refcount
= 0;
792 channel
->session_id
= session_id
;
795 channel
->relayd_id
= relayd_id
;
796 channel
->output
= output
;
798 strncpy(channel
->pathname
, pathname
, sizeof(channel
->pathname
));
799 channel
->pathname
[sizeof(channel
->pathname
) - 1] = '\0';
801 strncpy(channel
->name
, name
, sizeof(channel
->name
));
802 channel
->name
[sizeof(channel
->name
) - 1] = '\0';
804 lttng_ht_node_init_u64(&channel
->node
, channel
->key
);
805 CDS_INIT_LIST_HEAD(&channel
->streams
.head
);
807 DBG("Allocated channel (key %" PRIu64
")", channel
->key
)
814 * Add a channel to the global list protected by a mutex.
816 int consumer_add_channel(struct lttng_consumer_channel
*channel
)
819 struct lttng_ht_node_u64
*node
;
820 struct lttng_ht_iter iter
;
822 pthread_mutex_lock(&consumer_data
.lock
);
825 lttng_ht_lookup(consumer_data
.channel_ht
,
826 &channel
->key
, &iter
);
827 node
= lttng_ht_iter_get_node_u64(&iter
);
829 /* Channel already exist. Ignore the insertion */
830 ERR("Consumer add channel key %" PRIu64
" already exists!",
836 lttng_ht_add_unique_u64(consumer_data
.channel_ht
, &channel
->node
);
840 pthread_mutex_unlock(&consumer_data
.lock
);
846 * Allocate the pollfd structure and the local view of the out fds to avoid
847 * doing a lookup in the linked list and concurrency issues when writing is
848 * needed. Called with consumer_data.lock held.
850 * Returns the number of fds in the structures.
852 static int update_poll_array(struct lttng_consumer_local_data
*ctx
,
853 struct pollfd
**pollfd
, struct lttng_consumer_stream
**local_stream
,
857 struct lttng_ht_iter iter
;
858 struct lttng_consumer_stream
*stream
;
863 assert(local_stream
);
865 DBG("Updating poll fd array");
867 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
869 * Only active streams with an active end point can be added to the
870 * poll set and local stream storage of the thread.
872 * There is a potential race here for endpoint_status to be updated
873 * just after the check. However, this is OK since the stream(s) will
874 * be deleted once the thread is notified that the end point state has
875 * changed where this function will be called back again.
877 if (stream
->state
!= LTTNG_CONSUMER_ACTIVE_STREAM
||
878 stream
->endpoint_status
== CONSUMER_ENDPOINT_INACTIVE
) {
881 DBG("Active FD %d", stream
->wait_fd
);
882 (*pollfd
)[i
].fd
= stream
->wait_fd
;
883 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
884 local_stream
[i
] = stream
;
890 * Insert the consumer_data_pipe at the end of the array and don't
891 * increment i so nb_fd is the number of real FD.
893 (*pollfd
)[i
].fd
= ctx
->consumer_data_pipe
[0];
894 (*pollfd
)[i
].events
= POLLIN
| POLLPRI
;
899 * Poll on the should_quit pipe and the command socket return -1 on error and
900 * should exit, 0 if data is available on the command socket
902 int lttng_consumer_poll_socket(struct pollfd
*consumer_sockpoll
)
907 num_rdy
= poll(consumer_sockpoll
, 2, -1);
910 * Restart interrupted system call.
912 if (errno
== EINTR
) {
915 PERROR("Poll error");
918 if (consumer_sockpoll
[0].revents
& (POLLIN
| POLLPRI
)) {
919 DBG("consumer_should_quit wake up");
929 * Set the error socket.
931 void lttng_consumer_set_error_sock(struct lttng_consumer_local_data
*ctx
,
934 ctx
->consumer_error_socket
= sock
;
938 * Set the command socket path.
940 void lttng_consumer_set_command_sock_path(
941 struct lttng_consumer_local_data
*ctx
, char *sock
)
943 ctx
->consumer_command_sock_path
= sock
;
947 * Send return code to the session daemon.
948 * If the socket is not defined, we return 0, it is not a fatal error
950 int lttng_consumer_send_error(struct lttng_consumer_local_data
*ctx
, int cmd
)
952 if (ctx
->consumer_error_socket
> 0) {
953 return lttcomm_send_unix_sock(ctx
->consumer_error_socket
, &cmd
,
954 sizeof(enum lttcomm_sessiond_command
));
961 * Close all the tracefiles and stream fds and MUST be called when all
962 * instances are destroyed i.e. when all threads were joined and are ended.
964 void lttng_consumer_cleanup(void)
966 struct lttng_ht_iter iter
;
967 struct lttng_consumer_channel
*channel
;
971 cds_lfht_for_each_entry(consumer_data
.channel_ht
->ht
, &iter
.iter
, channel
,
973 consumer_del_channel(channel
);
978 lttng_ht_destroy(consumer_data
.channel_ht
);
983 * This HT contains streams that are freed by either the metadata thread or
984 * the data thread so we do *nothing* on the hash table and simply destroy
987 lttng_ht_destroy(consumer_data
.stream_list_ht
);
991 * Called from signal handler.
993 void lttng_consumer_should_exit(struct lttng_consumer_local_data
*ctx
)
998 ret
= write(ctx
->consumer_should_quit
[1], "4", 1);
999 } while (ret
< 0 && errno
== EINTR
);
1000 if (ret
< 0 || ret
!= 1) {
1001 PERROR("write consumer quit");
1004 DBG("Consumer flag that it should quit");
1007 void lttng_consumer_sync_trace_file(struct lttng_consumer_stream
*stream
,
1010 int outfd
= stream
->out_fd
;
1013 * This does a blocking write-and-wait on any page that belongs to the
1014 * subbuffer prior to the one we just wrote.
1015 * Don't care about error values, as these are just hints and ways to
1016 * limit the amount of page cache used.
1018 if (orig_offset
< stream
->max_sb_size
) {
1021 lttng_sync_file_range(outfd
, orig_offset
- stream
->max_sb_size
,
1022 stream
->max_sb_size
,
1023 SYNC_FILE_RANGE_WAIT_BEFORE
1024 | SYNC_FILE_RANGE_WRITE
1025 | SYNC_FILE_RANGE_WAIT_AFTER
);
1027 * Give hints to the kernel about how we access the file:
1028 * POSIX_FADV_DONTNEED : we won't re-access data in a near future after
1031 * We need to call fadvise again after the file grows because the
1032 * kernel does not seem to apply fadvise to non-existing parts of the
1035 * Call fadvise _after_ having waited for the page writeback to
1036 * complete because the dirty page writeback semantic is not well
1037 * defined. So it can be expected to lead to lower throughput in
1040 posix_fadvise(outfd
, orig_offset
- stream
->max_sb_size
,
1041 stream
->max_sb_size
, POSIX_FADV_DONTNEED
);
1045 * Initialise the necessary environnement :
1046 * - create a new context
1047 * - create the poll_pipe
1048 * - create the should_quit pipe (for signal handler)
1049 * - create the thread pipe (for splice)
1051 * Takes a function pointer as argument, this function is called when data is
1052 * available on a buffer. This function is responsible to do the
1053 * kernctl_get_next_subbuf, read the data with mmap or splice depending on the
1054 * buffer configuration and then kernctl_put_next_subbuf at the end.
1056 * Returns a pointer to the new context or NULL on error.
1058 struct lttng_consumer_local_data
*lttng_consumer_create(
1059 enum lttng_consumer_type type
,
1060 ssize_t (*buffer_ready
)(struct lttng_consumer_stream
*stream
,
1061 struct lttng_consumer_local_data
*ctx
),
1062 int (*recv_channel
)(struct lttng_consumer_channel
*channel
),
1063 int (*recv_stream
)(struct lttng_consumer_stream
*stream
),
1064 int (*update_stream
)(int stream_key
, uint32_t state
))
1067 struct lttng_consumer_local_data
*ctx
;
1069 assert(consumer_data
.type
== LTTNG_CONSUMER_UNKNOWN
||
1070 consumer_data
.type
== type
);
1071 consumer_data
.type
= type
;
1073 ctx
= zmalloc(sizeof(struct lttng_consumer_local_data
));
1075 PERROR("allocating context");
1079 ctx
->consumer_error_socket
= -1;
1080 /* assign the callbacks */
1081 ctx
->on_buffer_ready
= buffer_ready
;
1082 ctx
->on_recv_channel
= recv_channel
;
1083 ctx
->on_recv_stream
= recv_stream
;
1084 ctx
->on_update_stream
= update_stream
;
1086 ret
= pipe(ctx
->consumer_data_pipe
);
1088 PERROR("Error creating poll pipe");
1089 goto error_poll_pipe
;
1092 /* set read end of the pipe to non-blocking */
1093 ret
= fcntl(ctx
->consumer_data_pipe
[0], F_SETFL
, O_NONBLOCK
);
1095 PERROR("fcntl O_NONBLOCK");
1096 goto error_poll_fcntl
;
1099 /* set write end of the pipe to non-blocking */
1100 ret
= fcntl(ctx
->consumer_data_pipe
[1], F_SETFL
, O_NONBLOCK
);
1102 PERROR("fcntl O_NONBLOCK");
1103 goto error_poll_fcntl
;
1106 ret
= pipe(ctx
->consumer_should_quit
);
1108 PERROR("Error creating recv pipe");
1109 goto error_quit_pipe
;
1112 ret
= pipe(ctx
->consumer_thread_pipe
);
1114 PERROR("Error creating thread pipe");
1115 goto error_thread_pipe
;
1118 ret
= utils_create_pipe(ctx
->consumer_metadata_pipe
);
1120 goto error_metadata_pipe
;
1123 ret
= utils_create_pipe(ctx
->consumer_splice_metadata_pipe
);
1125 goto error_splice_pipe
;
1131 utils_close_pipe(ctx
->consumer_metadata_pipe
);
1132 error_metadata_pipe
:
1133 utils_close_pipe(ctx
->consumer_thread_pipe
);
1135 for (i
= 0; i
< 2; i
++) {
1138 err
= close(ctx
->consumer_should_quit
[i
]);
1145 for (i
= 0; i
< 2; i
++) {
1148 err
= close(ctx
->consumer_data_pipe
[i
]);
1160 * Close all fds associated with the instance and free the context.
1162 void lttng_consumer_destroy(struct lttng_consumer_local_data
*ctx
)
1166 DBG("Consumer destroying it. Closing everything.");
1168 ret
= close(ctx
->consumer_error_socket
);
1172 ret
= close(ctx
->consumer_thread_pipe
[0]);
1176 ret
= close(ctx
->consumer_thread_pipe
[1]);
1180 ret
= close(ctx
->consumer_data_pipe
[0]);
1184 ret
= close(ctx
->consumer_data_pipe
[1]);
1188 ret
= close(ctx
->consumer_should_quit
[0]);
1192 ret
= close(ctx
->consumer_should_quit
[1]);
1196 utils_close_pipe(ctx
->consumer_splice_metadata_pipe
);
1198 unlink(ctx
->consumer_command_sock_path
);
1203 * Write the metadata stream id on the specified file descriptor.
1205 static int write_relayd_metadata_id(int fd
,
1206 struct lttng_consumer_stream
*stream
,
1207 struct consumer_relayd_sock_pair
*relayd
, unsigned long padding
)
1210 struct lttcomm_relayd_metadata_payload hdr
;
1212 hdr
.stream_id
= htobe64(stream
->relayd_stream_id
);
1213 hdr
.padding_size
= htobe32(padding
);
1215 ret
= write(fd
, (void *) &hdr
, sizeof(hdr
));
1216 } while (ret
< 0 && errno
== EINTR
);
1217 if (ret
< 0 || ret
!= sizeof(hdr
)) {
1219 * This error means that the fd's end is closed so ignore the perror
1220 * not to clubber the error output since this can happen in a normal
1223 if (errno
!= EPIPE
) {
1224 PERROR("write metadata stream id");
1226 DBG3("Consumer failed to write relayd metadata id (errno: %d)", errno
);
1228 * Set ret to a negative value because if ret != sizeof(hdr), we don't
1229 * handle writting the missing part so report that as an error and
1230 * don't lie to the caller.
1235 DBG("Metadata stream id %" PRIu64
" with padding %lu written before data",
1236 stream
->relayd_stream_id
, padding
);
1243 * Mmap the ring buffer, read it and write the data to the tracefile. This is a
1244 * core function for writing trace buffers to either the local filesystem or
1247 * It must be called with the stream lock held.
1249 * Careful review MUST be put if any changes occur!
1251 * Returns the number of bytes written
1253 ssize_t
lttng_consumer_on_read_subbuffer_mmap(
1254 struct lttng_consumer_local_data
*ctx
,
1255 struct lttng_consumer_stream
*stream
, unsigned long len
,
1256 unsigned long padding
)
1258 unsigned long mmap_offset
;
1260 ssize_t ret
= 0, written
= 0;
1261 off_t orig_offset
= stream
->out_fd_offset
;
1262 /* Default is on the disk */
1263 int outfd
= stream
->out_fd
;
1264 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1265 unsigned int relayd_hang_up
= 0;
1267 /* RCU lock for the relayd pointer */
1270 /* Flag that the current stream if set for network streaming. */
1271 if (stream
->net_seq_idx
!= -1) {
1272 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1273 if (relayd
== NULL
) {
1278 /* get the offset inside the fd to mmap */
1279 switch (consumer_data
.type
) {
1280 case LTTNG_CONSUMER_KERNEL
:
1281 mmap_base
= stream
->mmap_base
;
1282 ret
= kernctl_get_mmap_read_offset(stream
->wait_fd
, &mmap_offset
);
1284 case LTTNG_CONSUMER32_UST
:
1285 case LTTNG_CONSUMER64_UST
:
1286 mmap_base
= lttng_ustctl_get_mmap_base(stream
);
1288 ERR("read mmap get mmap base for stream %s", stream
->name
);
1292 ret
= lttng_ustctl_get_mmap_read_offset(stream
, &mmap_offset
);
1295 ERR("Unknown consumer_data type");
1300 PERROR("tracer ctl get_mmap_read_offset");
1305 /* Handle stream on the relayd if the output is on the network */
1307 unsigned long netlen
= len
;
1310 * Lock the control socket for the complete duration of the function
1311 * since from this point on we will use the socket.
1313 if (stream
->metadata_flag
) {
1314 /* Metadata requires the control socket. */
1315 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1316 netlen
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1319 ret
= write_relayd_stream_header(stream
, netlen
, padding
, relayd
);
1321 /* Use the returned socket. */
1324 /* Write metadata stream id before payload */
1325 if (stream
->metadata_flag
) {
1326 ret
= write_relayd_metadata_id(outfd
, stream
, relayd
, padding
);
1329 /* Socket operation failed. We consider the relayd dead */
1330 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1338 /* Socket operation failed. We consider the relayd dead */
1339 if (ret
== -EPIPE
|| ret
== -EINVAL
) {
1343 /* Else, use the default set before which is the filesystem. */
1346 /* No streaming, we have to set the len with the full padding */
1352 ret
= write(outfd
, mmap_base
+ mmap_offset
, len
);
1353 } while (ret
< 0 && errno
== EINTR
);
1354 DBG("Consumer mmap write() ret %zd (len %lu)", ret
, len
);
1357 * This is possible if the fd is closed on the other side (outfd)
1358 * or any write problem. It can be verbose a bit for a normal
1359 * execution if for instance the relayd is stopped abruptly. This
1360 * can happen so set this to a DBG statement.
1362 DBG("Error in file write mmap");
1366 /* Socket operation failed. We consider the relayd dead */
1367 if (errno
== EPIPE
|| errno
== EINVAL
) {
1372 } else if (ret
> len
) {
1373 PERROR("Error in file write (ret %zd > len %lu)", ret
, len
);
1381 /* This call is useless on a socket so better save a syscall. */
1383 /* This won't block, but will start writeout asynchronously */
1384 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret
,
1385 SYNC_FILE_RANGE_WRITE
);
1386 stream
->out_fd_offset
+= ret
;
1390 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1394 * This is a special case that the relayd has closed its socket. Let's
1395 * cleanup the relayd object and all associated streams.
1397 if (relayd
&& relayd_hang_up
) {
1398 cleanup_relayd(relayd
, ctx
);
1402 /* Unlock only if ctrl socket used */
1403 if (relayd
&& stream
->metadata_flag
) {
1404 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1412 * Splice the data from the ring buffer to the tracefile.
1414 * It must be called with the stream lock held.
1416 * Returns the number of bytes spliced.
1418 ssize_t
lttng_consumer_on_read_subbuffer_splice(
1419 struct lttng_consumer_local_data
*ctx
,
1420 struct lttng_consumer_stream
*stream
, unsigned long len
,
1421 unsigned long padding
)
1423 ssize_t ret
= 0, written
= 0, ret_splice
= 0;
1425 off_t orig_offset
= stream
->out_fd_offset
;
1426 int fd
= stream
->wait_fd
;
1427 /* Default is on the disk */
1428 int outfd
= stream
->out_fd
;
1429 struct consumer_relayd_sock_pair
*relayd
= NULL
;
1431 unsigned int relayd_hang_up
= 0;
1433 switch (consumer_data
.type
) {
1434 case LTTNG_CONSUMER_KERNEL
:
1436 case LTTNG_CONSUMER32_UST
:
1437 case LTTNG_CONSUMER64_UST
:
1438 /* Not supported for user space tracing */
1441 ERR("Unknown consumer_data type");
1445 /* RCU lock for the relayd pointer */
1448 /* Flag that the current stream if set for network streaming. */
1449 if (stream
->net_seq_idx
!= -1) {
1450 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1451 if (relayd
== NULL
) {
1457 * Choose right pipe for splice. Metadata and trace data are handled by
1458 * different threads hence the use of two pipes in order not to race or
1459 * corrupt the written data.
1461 if (stream
->metadata_flag
) {
1462 splice_pipe
= ctx
->consumer_splice_metadata_pipe
;
1464 splice_pipe
= ctx
->consumer_thread_pipe
;
1467 /* Write metadata stream id before payload */
1469 int total_len
= len
;
1471 if (stream
->metadata_flag
) {
1473 * Lock the control socket for the complete duration of the function
1474 * since from this point on we will use the socket.
1476 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1478 ret
= write_relayd_metadata_id(splice_pipe
[1], stream
, relayd
,
1482 /* Socket operation failed. We consider the relayd dead */
1483 if (ret
== -EBADF
) {
1484 WARN("Remote relayd disconnected. Stopping");
1491 total_len
+= sizeof(struct lttcomm_relayd_metadata_payload
);
1494 ret
= write_relayd_stream_header(stream
, total_len
, padding
, relayd
);
1496 /* Use the returned socket. */
1499 /* Socket operation failed. We consider the relayd dead */
1500 if (ret
== -EBADF
) {
1501 WARN("Remote relayd disconnected. Stopping");
1508 /* No streaming, we have to set the len with the full padding */
1513 DBG("splice chan to pipe offset %lu of len %lu (fd : %d, pipe: %d)",
1514 (unsigned long)offset
, len
, fd
, splice_pipe
[1]);
1515 ret_splice
= splice(fd
, &offset
, splice_pipe
[1], NULL
, len
,
1516 SPLICE_F_MOVE
| SPLICE_F_MORE
);
1517 DBG("splice chan to pipe, ret %zd", ret_splice
);
1518 if (ret_splice
< 0) {
1519 PERROR("Error in relay splice");
1521 written
= ret_splice
;
1527 /* Handle stream on the relayd if the output is on the network */
1529 if (stream
->metadata_flag
) {
1530 size_t metadata_payload_size
=
1531 sizeof(struct lttcomm_relayd_metadata_payload
);
1533 /* Update counter to fit the spliced data */
1534 ret_splice
+= metadata_payload_size
;
1535 len
+= metadata_payload_size
;
1537 * We do this so the return value can match the len passed as
1538 * argument to this function.
1540 written
-= metadata_payload_size
;
1544 /* Splice data out */
1545 ret_splice
= splice(splice_pipe
[0], NULL
, outfd
, NULL
,
1546 ret_splice
, SPLICE_F_MOVE
| SPLICE_F_MORE
);
1547 DBG("Consumer splice pipe to file, ret %zd", ret_splice
);
1548 if (ret_splice
< 0) {
1549 PERROR("Error in file splice");
1551 written
= ret_splice
;
1553 /* Socket operation failed. We consider the relayd dead */
1554 if (errno
== EBADF
|| errno
== EPIPE
) {
1555 WARN("Remote relayd disconnected. Stopping");
1561 } else if (ret_splice
> len
) {
1563 PERROR("Wrote more data than requested %zd (len: %lu)",
1565 written
+= ret_splice
;
1571 /* This call is useless on a socket so better save a syscall. */
1573 /* This won't block, but will start writeout asynchronously */
1574 lttng_sync_file_range(outfd
, stream
->out_fd_offset
, ret_splice
,
1575 SYNC_FILE_RANGE_WRITE
);
1576 stream
->out_fd_offset
+= ret_splice
;
1578 written
+= ret_splice
;
1580 lttng_consumer_sync_trace_file(stream
, orig_offset
);
1588 * This is a special case that the relayd has closed its socket. Let's
1589 * cleanup the relayd object and all associated streams.
1591 if (relayd
&& relayd_hang_up
) {
1592 cleanup_relayd(relayd
, ctx
);
1593 /* Skip splice error so the consumer does not fail */
1598 /* send the appropriate error description to sessiond */
1601 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_EINVAL
);
1604 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ENOMEM
);
1607 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_SPLICE_ESPIPE
);
1612 if (relayd
&& stream
->metadata_flag
) {
1613 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1621 * Take a snapshot for a specific fd
1623 * Returns 0 on success, < 0 on error
1625 int lttng_consumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1627 switch (consumer_data
.type
) {
1628 case LTTNG_CONSUMER_KERNEL
:
1629 return lttng_kconsumer_take_snapshot(stream
);
1630 case LTTNG_CONSUMER32_UST
:
1631 case LTTNG_CONSUMER64_UST
:
1632 return lttng_ustconsumer_take_snapshot(stream
);
1634 ERR("Unknown consumer_data type");
1641 * Get the produced position
1643 * Returns 0 on success, < 0 on error
1645 int lttng_consumer_get_produced_snapshot(struct lttng_consumer_stream
*stream
,
1648 switch (consumer_data
.type
) {
1649 case LTTNG_CONSUMER_KERNEL
:
1650 return lttng_kconsumer_get_produced_snapshot(stream
, pos
);
1651 case LTTNG_CONSUMER32_UST
:
1652 case LTTNG_CONSUMER64_UST
:
1653 return lttng_ustconsumer_get_produced_snapshot(stream
, pos
);
1655 ERR("Unknown consumer_data type");
1661 int lttng_consumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
1662 int sock
, struct pollfd
*consumer_sockpoll
)
1664 switch (consumer_data
.type
) {
1665 case LTTNG_CONSUMER_KERNEL
:
1666 return lttng_kconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1667 case LTTNG_CONSUMER32_UST
:
1668 case LTTNG_CONSUMER64_UST
:
1669 return lttng_ustconsumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
1671 ERR("Unknown consumer_data type");
1678 * Iterate over all streams of the hashtable and free them properly.
1680 * WARNING: *MUST* be used with data stream only.
1682 static void destroy_data_stream_ht(struct lttng_ht
*ht
)
1684 struct lttng_ht_iter iter
;
1685 struct lttng_consumer_stream
*stream
;
1692 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1694 * Ignore return value since we are currently cleaning up so any error
1697 (void) consumer_del_stream(stream
, ht
);
1701 lttng_ht_destroy(ht
);
1705 * Iterate over all streams of the hashtable and free them properly.
1707 * XXX: Should not be only for metadata stream or else use an other name.
1709 static void destroy_stream_ht(struct lttng_ht
*ht
)
1711 struct lttng_ht_iter iter
;
1712 struct lttng_consumer_stream
*stream
;
1719 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1721 * Ignore return value since we are currently cleaning up so any error
1724 (void) consumer_del_metadata_stream(stream
, ht
);
1728 lttng_ht_destroy(ht
);
1731 void lttng_consumer_close_metadata(void)
1733 switch (consumer_data
.type
) {
1734 case LTTNG_CONSUMER_KERNEL
:
1736 * The Kernel consumer has a different metadata scheme so we don't
1737 * close anything because the stream will be closed by the session
1741 case LTTNG_CONSUMER32_UST
:
1742 case LTTNG_CONSUMER64_UST
:
1744 * Close all metadata streams. The metadata hash table is passed and
1745 * this call iterates over it by closing all wakeup fd. This is safe
1746 * because at this point we are sure that the metadata producer is
1747 * either dead or blocked.
1749 lttng_ustconsumer_close_metadata(metadata_ht
);
1752 ERR("Unknown consumer_data type");
1758 * Clean up a metadata stream and free its memory.
1760 void consumer_del_metadata_stream(struct lttng_consumer_stream
*stream
,
1761 struct lttng_ht
*ht
)
1764 struct lttng_ht_iter iter
;
1765 struct lttng_consumer_channel
*free_chan
= NULL
;
1766 struct consumer_relayd_sock_pair
*relayd
;
1770 * This call should NEVER receive regular stream. It must always be
1771 * metadata stream and this is crucial for data structure synchronization.
1773 assert(stream
->metadata_flag
);
1775 DBG3("Consumer delete metadata stream %d", stream
->wait_fd
);
1778 /* Means the stream was allocated but not successfully added */
1779 goto free_stream_rcu
;
1782 pthread_mutex_lock(&consumer_data
.lock
);
1783 pthread_mutex_lock(&stream
->lock
);
1785 switch (consumer_data
.type
) {
1786 case LTTNG_CONSUMER_KERNEL
:
1787 if (stream
->mmap_base
!= NULL
) {
1788 ret
= munmap(stream
->mmap_base
, stream
->mmap_len
);
1790 PERROR("munmap metadata stream");
1794 case LTTNG_CONSUMER32_UST
:
1795 case LTTNG_CONSUMER64_UST
:
1796 lttng_ustconsumer_del_stream(stream
);
1799 ERR("Unknown consumer_data type");
1805 iter
.iter
.node
= &stream
->node
.node
;
1806 ret
= lttng_ht_del(ht
, &iter
);
1809 /* Remove node session id from the consumer_data stream ht */
1810 iter
.iter
.node
= &stream
->node_session_id
.node
;
1811 ret
= lttng_ht_del(consumer_data
.stream_list_ht
, &iter
);
1815 if (stream
->out_fd
>= 0) {
1816 ret
= close(stream
->out_fd
);
1822 /* Check and cleanup relayd */
1824 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1825 if (relayd
!= NULL
) {
1826 uatomic_dec(&relayd
->refcount
);
1827 assert(uatomic_read(&relayd
->refcount
) >= 0);
1829 /* Closing streams requires to lock the control socket. */
1830 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
1831 ret
= relayd_send_close_stream(&relayd
->control_sock
,
1832 stream
->relayd_stream_id
, stream
->next_net_seq_num
- 1);
1833 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
1835 DBG("Unable to close stream on the relayd. Continuing");
1837 * Continue here. There is nothing we can do for the relayd.
1838 * Chances are that the relayd has closed the socket so we just
1839 * continue cleaning up.
1843 /* Both conditions are met, we destroy the relayd. */
1844 if (uatomic_read(&relayd
->refcount
) == 0 &&
1845 uatomic_read(&relayd
->destroy_flag
)) {
1846 destroy_relayd(relayd
);
1851 /* Atomically decrement channel refcount since other threads can use it. */
1852 uatomic_dec(&stream
->chan
->refcount
);
1853 if (!uatomic_read(&stream
->chan
->refcount
)
1854 && !uatomic_read(&stream
->chan
->nb_init_stream_left
)) {
1855 /* Go for channel deletion! */
1856 free_chan
= stream
->chan
;
1860 pthread_mutex_unlock(&stream
->lock
);
1861 pthread_mutex_unlock(&consumer_data
.lock
);
1864 consumer_del_channel(free_chan
);
1868 call_rcu(&stream
->node
.head
, free_stream_rcu
);
1872 * Action done with the metadata stream when adding it to the consumer internal
1873 * data structures to handle it.
1875 static int add_metadata_stream(struct lttng_consumer_stream
*stream
,
1876 struct lttng_ht
*ht
)
1879 struct consumer_relayd_sock_pair
*relayd
;
1880 struct lttng_ht_iter iter
;
1881 struct lttng_ht_node_u64
*node
;
1886 DBG3("Adding metadata stream %" PRIu64
" to hash table", stream
->key
);
1888 pthread_mutex_lock(&consumer_data
.lock
);
1889 pthread_mutex_lock(&stream
->lock
);
1892 * From here, refcounts are updated so be _careful_ when returning an error
1899 * Lookup the stream just to make sure it does not exist in our internal
1900 * state. This should NEVER happen.
1902 lttng_ht_lookup(ht
, &stream
->key
, &iter
);
1903 node
= lttng_ht_iter_get_node_u64(&iter
);
1906 /* Find relayd and, if one is found, increment refcount. */
1907 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
1908 if (relayd
!= NULL
) {
1909 uatomic_inc(&relayd
->refcount
);
1912 /* Update channel refcount once added without error(s). */
1913 uatomic_inc(&stream
->chan
->refcount
);
1916 * When nb_init_stream_left reaches 0, we don't need to trigger any action
1917 * in terms of destroying the associated channel, because the action that
1918 * causes the count to become 0 also causes a stream to be added. The
1919 * channel deletion will thus be triggered by the following removal of this
1922 if (uatomic_read(&stream
->chan
->nb_init_stream_left
) > 0) {
1923 uatomic_dec(&stream
->chan
->nb_init_stream_left
);
1926 lttng_ht_add_unique_u64(ht
, &stream
->node
);
1929 * Add stream to the stream_list_ht of the consumer data. No need to steal
1930 * the key since the HT does not use it and we allow to add redundant keys
1933 lttng_ht_add_u64(consumer_data
.stream_list_ht
, &stream
->node_session_id
);
1937 pthread_mutex_unlock(&stream
->lock
);
1938 pthread_mutex_unlock(&consumer_data
.lock
);
1943 * Delete data stream that are flagged for deletion (endpoint_status).
1945 static void validate_endpoint_status_data_stream(void)
1947 struct lttng_ht_iter iter
;
1948 struct lttng_consumer_stream
*stream
;
1950 DBG("Consumer delete flagged data stream");
1953 cds_lfht_for_each_entry(data_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1954 /* Validate delete flag of the stream */
1955 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
1958 /* Delete it right now */
1959 consumer_del_stream(stream
, data_ht
);
1965 * Delete metadata stream that are flagged for deletion (endpoint_status).
1967 static void validate_endpoint_status_metadata_stream(
1968 struct lttng_poll_event
*pollset
)
1970 struct lttng_ht_iter iter
;
1971 struct lttng_consumer_stream
*stream
;
1973 DBG("Consumer delete flagged metadata stream");
1978 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
, node
.node
) {
1979 /* Validate delete flag of the stream */
1980 if (stream
->endpoint_status
== CONSUMER_ENDPOINT_ACTIVE
) {
1984 * Remove from pollset so the metadata thread can continue without
1985 * blocking on a deleted stream.
1987 lttng_poll_del(pollset
, stream
->wait_fd
);
1989 /* Delete it right now */
1990 consumer_del_metadata_stream(stream
, metadata_ht
);
1996 * Thread polls on metadata file descriptor and write them on disk or on the
1999 void *consumer_thread_metadata_poll(void *data
)
2002 uint32_t revents
, nb_fd
;
2003 struct lttng_consumer_stream
*stream
= NULL
;
2004 struct lttng_ht_iter iter
;
2005 struct lttng_ht_node_u64
*node
;
2006 struct lttng_poll_event events
;
2007 struct lttng_consumer_local_data
*ctx
= data
;
2010 rcu_register_thread();
2012 metadata_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2014 /* ENOMEM at this point. Better to bail out. */
2018 DBG("Thread metadata poll started");
2020 /* Size is set to 1 for the consumer_metadata pipe */
2021 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
2023 ERR("Poll set creation failed");
2027 ret
= lttng_poll_add(&events
, ctx
->consumer_metadata_pipe
[0], LPOLLIN
);
2033 DBG("Metadata main loop started");
2036 /* Only the metadata pipe is set */
2037 if (LTTNG_POLL_GETNB(&events
) == 0 && consumer_quit
== 1) {
2042 DBG("Metadata poll wait with %d fd(s)", LTTNG_POLL_GETNB(&events
));
2043 ret
= lttng_poll_wait(&events
, -1);
2044 DBG("Metadata event catched in thread");
2046 if (errno
== EINTR
) {
2047 ERR("Poll EINTR catched");
2055 /* From here, the event is a metadata wait fd */
2056 for (i
= 0; i
< nb_fd
; i
++) {
2057 revents
= LTTNG_POLL_GETEV(&events
, i
);
2058 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2060 /* Just don't waste time if no returned events for the fd */
2065 if (pollfd
== ctx
->consumer_metadata_pipe
[0]) {
2066 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2067 DBG("Metadata thread pipe hung up");
2069 * Remove the pipe from the poll set and continue the loop
2070 * since their might be data to consume.
2072 lttng_poll_del(&events
, ctx
->consumer_metadata_pipe
[0]);
2073 ret
= close(ctx
->consumer_metadata_pipe
[0]);
2075 PERROR("close metadata pipe");
2078 } else if (revents
& LPOLLIN
) {
2080 /* Get the stream pointer received */
2081 ret
= read(pollfd
, &stream
, sizeof(stream
));
2082 } while (ret
< 0 && errno
== EINTR
);
2084 ret
< sizeof(struct lttng_consumer_stream
*)) {
2085 PERROR("read metadata stream");
2087 * Let's continue here and hope we can still work
2088 * without stopping the consumer. XXX: Should we?
2093 /* A NULL stream means that the state has changed. */
2094 if (stream
== NULL
) {
2095 /* Check for deleted streams. */
2096 validate_endpoint_status_metadata_stream(&events
);
2100 DBG("Adding metadata stream %d to poll set",
2103 ret
= add_metadata_stream(stream
, metadata_ht
);
2105 ERR("Unable to add metadata stream");
2106 /* Stream was not setup properly. Continuing. */
2107 consumer_del_metadata_stream(stream
, NULL
);
2111 /* Add metadata stream to the global poll events list */
2112 lttng_poll_add(&events
, stream
->wait_fd
,
2113 LPOLLIN
| LPOLLPRI
);
2116 /* Handle other stream */
2122 uint64_t tmp_id
= (uint64_t) pollfd
;
2124 lttng_ht_lookup(metadata_ht
, &tmp_id
, &iter
);
2126 node
= lttng_ht_iter_get_node_u64(&iter
);
2129 stream
= caa_container_of(node
, struct lttng_consumer_stream
,
2132 /* Check for error event */
2133 if (revents
& (LPOLLERR
| LPOLLHUP
)) {
2134 DBG("Metadata fd %d is hup|err.", pollfd
);
2135 if (!stream
->hangup_flush_done
2136 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2137 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2138 DBG("Attempting to flush and consume the UST buffers");
2139 lttng_ustconsumer_on_stream_hangup(stream
);
2141 /* We just flushed the stream now read it. */
2143 len
= ctx
->on_buffer_ready(stream
, ctx
);
2145 * We don't check the return value here since if we get
2146 * a negative len, it means an error occured thus we
2147 * simply remove it from the poll set and free the
2153 lttng_poll_del(&events
, stream
->wait_fd
);
2155 * This call update the channel states, closes file descriptors
2156 * and securely free the stream.
2158 consumer_del_metadata_stream(stream
, metadata_ht
);
2159 } else if (revents
& (LPOLLIN
| LPOLLPRI
)) {
2160 /* Get the data out of the metadata file descriptor */
2161 DBG("Metadata available on fd %d", pollfd
);
2162 assert(stream
->wait_fd
== pollfd
);
2164 len
= ctx
->on_buffer_ready(stream
, ctx
);
2165 /* It's ok to have an unavailable sub-buffer */
2166 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2167 /* Clean up stream from consumer and free it. */
2168 lttng_poll_del(&events
, stream
->wait_fd
);
2169 consumer_del_metadata_stream(stream
, metadata_ht
);
2170 } else if (len
> 0) {
2171 stream
->data_read
= 1;
2175 /* Release RCU lock for the stream looked up */
2182 DBG("Metadata poll thread exiting");
2183 lttng_poll_clean(&events
);
2185 destroy_stream_ht(metadata_ht
);
2187 rcu_unregister_thread();
2192 * This thread polls the fds in the set to consume the data and write
2193 * it to tracefile if necessary.
2195 void *consumer_thread_data_poll(void *data
)
2197 int num_rdy
, num_hup
, high_prio
, ret
, i
;
2198 struct pollfd
*pollfd
= NULL
;
2199 /* local view of the streams */
2200 struct lttng_consumer_stream
**local_stream
= NULL
, *new_stream
= NULL
;
2201 /* local view of consumer_data.fds_count */
2203 struct lttng_consumer_local_data
*ctx
= data
;
2206 rcu_register_thread();
2208 data_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2209 if (data_ht
== NULL
) {
2210 /* ENOMEM at this point. Better to bail out. */
2214 local_stream
= zmalloc(sizeof(struct lttng_consumer_stream
));
2221 * the fds set has been updated, we need to update our
2222 * local array as well
2224 pthread_mutex_lock(&consumer_data
.lock
);
2225 if (consumer_data
.need_update
) {
2230 local_stream
= NULL
;
2232 /* allocate for all fds + 1 for the consumer_data_pipe */
2233 pollfd
= zmalloc((consumer_data
.stream_count
+ 1) * sizeof(struct pollfd
));
2234 if (pollfd
== NULL
) {
2235 PERROR("pollfd malloc");
2236 pthread_mutex_unlock(&consumer_data
.lock
);
2240 /* allocate for all fds + 1 for the consumer_data_pipe */
2241 local_stream
= zmalloc((consumer_data
.stream_count
+ 1) *
2242 sizeof(struct lttng_consumer_stream
));
2243 if (local_stream
== NULL
) {
2244 PERROR("local_stream malloc");
2245 pthread_mutex_unlock(&consumer_data
.lock
);
2248 ret
= update_poll_array(ctx
, &pollfd
, local_stream
,
2251 ERR("Error in allocating pollfd or local_outfds");
2252 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2253 pthread_mutex_unlock(&consumer_data
.lock
);
2257 consumer_data
.need_update
= 0;
2259 pthread_mutex_unlock(&consumer_data
.lock
);
2261 /* No FDs and consumer_quit, consumer_cleanup the thread */
2262 if (nb_fd
== 0 && consumer_quit
== 1) {
2265 /* poll on the array of fds */
2267 DBG("polling on %d fd", nb_fd
+ 1);
2268 num_rdy
= poll(pollfd
, nb_fd
+ 1, -1);
2269 DBG("poll num_rdy : %d", num_rdy
);
2270 if (num_rdy
== -1) {
2272 * Restart interrupted system call.
2274 if (errno
== EINTR
) {
2277 PERROR("Poll error");
2278 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_POLL_ERROR
);
2280 } else if (num_rdy
== 0) {
2281 DBG("Polling thread timed out");
2286 * If the consumer_data_pipe triggered poll go directly to the
2287 * beginning of the loop to update the array. We want to prioritize
2288 * array update over low-priority reads.
2290 if (pollfd
[nb_fd
].revents
& (POLLIN
| POLLPRI
)) {
2291 ssize_t pipe_readlen
;
2293 DBG("consumer_data_pipe wake up");
2294 /* Consume 1 byte of pipe data */
2296 pipe_readlen
= read(ctx
->consumer_data_pipe
[0], &new_stream
,
2297 sizeof(new_stream
));
2298 } while (pipe_readlen
== -1 && errno
== EINTR
);
2299 if (pipe_readlen
< 0) {
2300 PERROR("read consumer data pipe");
2301 /* Continue so we can at least handle the current stream(s). */
2306 * If the stream is NULL, just ignore it. It's also possible that
2307 * the sessiond poll thread changed the consumer_quit state and is
2308 * waking us up to test it.
2310 if (new_stream
== NULL
) {
2311 validate_endpoint_status_data_stream();
2315 ret
= add_stream(new_stream
, data_ht
);
2317 ERR("Consumer add stream %" PRIu64
" failed. Continuing",
2320 * At this point, if the add_stream fails, it is not in the
2321 * hash table thus passing the NULL value here.
2323 consumer_del_stream(new_stream
, NULL
);
2326 /* Continue to update the local streams and handle prio ones */
2330 /* Take care of high priority channels first. */
2331 for (i
= 0; i
< nb_fd
; i
++) {
2332 if (local_stream
[i
] == NULL
) {
2335 if (pollfd
[i
].revents
& POLLPRI
) {
2336 DBG("Urgent read on fd %d", pollfd
[i
].fd
);
2338 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2339 /* it's ok to have an unavailable sub-buffer */
2340 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2341 /* Clean the stream and free it. */
2342 consumer_del_stream(local_stream
[i
], data_ht
);
2343 local_stream
[i
] = NULL
;
2344 } else if (len
> 0) {
2345 local_stream
[i
]->data_read
= 1;
2351 * If we read high prio channel in this loop, try again
2352 * for more high prio data.
2358 /* Take care of low priority channels. */
2359 for (i
= 0; i
< nb_fd
; i
++) {
2360 if (local_stream
[i
] == NULL
) {
2363 if ((pollfd
[i
].revents
& POLLIN
) ||
2364 local_stream
[i
]->hangup_flush_done
) {
2365 DBG("Normal read on fd %d", pollfd
[i
].fd
);
2366 len
= ctx
->on_buffer_ready(local_stream
[i
], ctx
);
2367 /* it's ok to have an unavailable sub-buffer */
2368 if (len
< 0 && len
!= -EAGAIN
&& len
!= -ENODATA
) {
2369 /* Clean the stream and free it. */
2370 consumer_del_stream(local_stream
[i
], data_ht
);
2371 local_stream
[i
] = NULL
;
2372 } else if (len
> 0) {
2373 local_stream
[i
]->data_read
= 1;
2378 /* Handle hangup and errors */
2379 for (i
= 0; i
< nb_fd
; i
++) {
2380 if (local_stream
[i
] == NULL
) {
2383 if (!local_stream
[i
]->hangup_flush_done
2384 && (pollfd
[i
].revents
& (POLLHUP
| POLLERR
| POLLNVAL
))
2385 && (consumer_data
.type
== LTTNG_CONSUMER32_UST
2386 || consumer_data
.type
== LTTNG_CONSUMER64_UST
)) {
2387 DBG("fd %d is hup|err|nval. Attempting flush and read.",
2389 lttng_ustconsumer_on_stream_hangup(local_stream
[i
]);
2390 /* Attempt read again, for the data we just flushed. */
2391 local_stream
[i
]->data_read
= 1;
2394 * If the poll flag is HUP/ERR/NVAL and we have
2395 * read no data in this pass, we can remove the
2396 * stream from its hash table.
2398 if ((pollfd
[i
].revents
& POLLHUP
)) {
2399 DBG("Polling fd %d tells it has hung up.", pollfd
[i
].fd
);
2400 if (!local_stream
[i
]->data_read
) {
2401 consumer_del_stream(local_stream
[i
], data_ht
);
2402 local_stream
[i
] = NULL
;
2405 } else if (pollfd
[i
].revents
& POLLERR
) {
2406 ERR("Error returned in polling fd %d.", pollfd
[i
].fd
);
2407 if (!local_stream
[i
]->data_read
) {
2408 consumer_del_stream(local_stream
[i
], data_ht
);
2409 local_stream
[i
] = NULL
;
2412 } else if (pollfd
[i
].revents
& POLLNVAL
) {
2413 ERR("Polling fd %d tells fd is not open.", pollfd
[i
].fd
);
2414 if (!local_stream
[i
]->data_read
) {
2415 consumer_del_stream(local_stream
[i
], data_ht
);
2416 local_stream
[i
] = NULL
;
2420 if (local_stream
[i
] != NULL
) {
2421 local_stream
[i
]->data_read
= 0;
2426 DBG("polling thread exiting");
2431 * Close the write side of the pipe so epoll_wait() in
2432 * consumer_thread_metadata_poll can catch it. The thread is monitoring the
2433 * read side of the pipe. If we close them both, epoll_wait strangely does
2434 * not return and could create a endless wait period if the pipe is the
2435 * only tracked fd in the poll set. The thread will take care of closing
2438 ret
= close(ctx
->consumer_metadata_pipe
[1]);
2440 PERROR("close data pipe");
2443 destroy_data_stream_ht(data_ht
);
2445 rcu_unregister_thread();
2450 * This thread listens on the consumerd socket and receives the file
2451 * descriptors from the session daemon.
2453 void *consumer_thread_sessiond_poll(void *data
)
2455 int sock
= -1, client_socket
, ret
;
2457 * structure to poll for incoming data on communication socket avoids
2458 * making blocking sockets.
2460 struct pollfd consumer_sockpoll
[2];
2461 struct lttng_consumer_local_data
*ctx
= data
;
2463 rcu_register_thread();
2465 DBG("Creating command socket %s", ctx
->consumer_command_sock_path
);
2466 unlink(ctx
->consumer_command_sock_path
);
2467 client_socket
= lttcomm_create_unix_sock(ctx
->consumer_command_sock_path
);
2468 if (client_socket
< 0) {
2469 ERR("Cannot create command socket");
2473 ret
= lttcomm_listen_unix_sock(client_socket
);
2478 DBG("Sending ready command to lttng-sessiond");
2479 ret
= lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_COMMAND_SOCK_READY
);
2480 /* return < 0 on error, but == 0 is not fatal */
2482 ERR("Error sending ready command to lttng-sessiond");
2486 ret
= fcntl(client_socket
, F_SETFL
, O_NONBLOCK
);
2488 PERROR("fcntl O_NONBLOCK");
2492 /* prepare the FDs to poll : to client socket and the should_quit pipe */
2493 consumer_sockpoll
[0].fd
= ctx
->consumer_should_quit
[0];
2494 consumer_sockpoll
[0].events
= POLLIN
| POLLPRI
;
2495 consumer_sockpoll
[1].fd
= client_socket
;
2496 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2498 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2501 DBG("Connection on client_socket");
2503 /* Blocking call, waiting for transmission */
2504 sock
= lttcomm_accept_unix_sock(client_socket
);
2509 ret
= fcntl(sock
, F_SETFL
, O_NONBLOCK
);
2511 PERROR("fcntl O_NONBLOCK");
2515 /* This socket is not useful anymore. */
2516 ret
= close(client_socket
);
2518 PERROR("close client_socket");
2522 /* update the polling structure to poll on the established socket */
2523 consumer_sockpoll
[1].fd
= sock
;
2524 consumer_sockpoll
[1].events
= POLLIN
| POLLPRI
;
2527 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2530 DBG("Incoming command on sock");
2531 ret
= lttng_consumer_recv_cmd(ctx
, sock
, consumer_sockpoll
);
2532 if (ret
== -ENOENT
) {
2533 DBG("Received STOP command");
2538 * This could simply be a session daemon quitting. Don't output
2541 DBG("Communication interrupted on command socket");
2544 if (consumer_quit
) {
2545 DBG("consumer_thread_receive_fds received quit from signal");
2548 DBG("received command on sock");
2551 DBG("Consumer thread sessiond poll exiting");
2554 * Close metadata streams since the producer is the session daemon which
2557 * NOTE: for now, this only applies to the UST tracer.
2559 lttng_consumer_close_metadata();
2562 * when all fds have hung up, the polling thread
2568 * Notify the data poll thread to poll back again and test the
2569 * consumer_quit state that we just set so to quit gracefully.
2571 notify_thread_pipe(ctx
->consumer_data_pipe
[1]);
2573 /* Cleaning up possibly open sockets. */
2577 PERROR("close sock sessiond poll");
2580 if (client_socket
>= 0) {
2583 PERROR("close client_socket sessiond poll");
2587 rcu_unregister_thread();
2591 ssize_t
lttng_consumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
2592 struct lttng_consumer_local_data
*ctx
)
2596 pthread_mutex_lock(&stream
->lock
);
2598 switch (consumer_data
.type
) {
2599 case LTTNG_CONSUMER_KERNEL
:
2600 ret
= lttng_kconsumer_read_subbuffer(stream
, ctx
);
2602 case LTTNG_CONSUMER32_UST
:
2603 case LTTNG_CONSUMER64_UST
:
2604 ret
= lttng_ustconsumer_read_subbuffer(stream
, ctx
);
2607 ERR("Unknown consumer_data type");
2613 pthread_mutex_unlock(&stream
->lock
);
2617 int lttng_consumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
2619 switch (consumer_data
.type
) {
2620 case LTTNG_CONSUMER_KERNEL
:
2621 return lttng_kconsumer_on_recv_stream(stream
);
2622 case LTTNG_CONSUMER32_UST
:
2623 case LTTNG_CONSUMER64_UST
:
2624 return lttng_ustconsumer_on_recv_stream(stream
);
2626 ERR("Unknown consumer_data type");
2633 * Allocate and set consumer data hash tables.
2635 void lttng_consumer_init(void)
2637 consumer_data
.channel_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2638 consumer_data
.relayd_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2639 consumer_data
.stream_list_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
2643 * Process the ADD_RELAYD command receive by a consumer.
2645 * This will create a relayd socket pair and add it to the relayd hash table.
2646 * The caller MUST acquire a RCU read side lock before calling it.
2648 int consumer_add_relayd_socket(int net_seq_idx
, int sock_type
,
2649 struct lttng_consumer_local_data
*ctx
, int sock
,
2650 struct pollfd
*consumer_sockpoll
, struct lttcomm_sock
*relayd_sock
,
2651 unsigned int sessiond_id
)
2653 int fd
= -1, ret
= -1, relayd_created
= 0;
2654 enum lttng_error_code ret_code
= LTTNG_OK
;
2655 struct consumer_relayd_sock_pair
*relayd
;
2657 DBG("Consumer adding relayd socket (idx: %d)", net_seq_idx
);
2659 /* First send a status message before receiving the fds. */
2660 ret
= consumer_send_status_msg(sock
, ret_code
);
2662 /* Somehow, the session daemon is not responding anymore. */
2666 /* Get relayd reference if exists. */
2667 relayd
= consumer_find_relayd(net_seq_idx
);
2668 if (relayd
== NULL
) {
2669 /* Not found. Allocate one. */
2670 relayd
= consumer_allocate_relayd_sock_pair(net_seq_idx
);
2671 if (relayd
== NULL
) {
2672 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
2676 relayd
->sessiond_session_id
= (uint64_t) sessiond_id
;
2680 /* Poll on consumer socket. */
2681 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
2686 /* Get relayd socket from session daemon */
2687 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
2688 if (ret
!= sizeof(fd
)) {
2689 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_FD
);
2691 fd
= -1; /* Just in case it gets set with an invalid value. */
2695 /* We have the fds without error. Send status back. */
2696 ret
= consumer_send_status_msg(sock
, ret_code
);
2698 /* Somehow, the session daemon is not responding anymore. */
2702 /* Copy socket information and received FD */
2703 switch (sock_type
) {
2704 case LTTNG_STREAM_CONTROL
:
2705 /* Copy received lttcomm socket */
2706 lttcomm_copy_sock(&relayd
->control_sock
, relayd_sock
);
2707 ret
= lttcomm_create_sock(&relayd
->control_sock
);
2708 /* Immediately try to close the created socket if valid. */
2709 if (relayd
->control_sock
.fd
>= 0) {
2710 if (close(relayd
->control_sock
.fd
)) {
2711 PERROR("close relayd control socket");
2714 /* Handle create_sock error. */
2719 /* Assign new file descriptor */
2720 relayd
->control_sock
.fd
= fd
;
2723 * Create a session on the relayd and store the returned id. Lock the
2724 * control socket mutex if the relayd was NOT created before.
2726 if (!relayd_created
) {
2727 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
2729 ret
= relayd_create_session(&relayd
->control_sock
,
2730 &relayd
->relayd_session_id
);
2731 if (!relayd_created
) {
2732 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2736 * Close all sockets of a relayd object. It will be freed if it was
2737 * created at the error code path or else it will be garbage
2740 (void) relayd_close(&relayd
->control_sock
);
2741 (void) relayd_close(&relayd
->data_sock
);
2746 case LTTNG_STREAM_DATA
:
2747 /* Copy received lttcomm socket */
2748 lttcomm_copy_sock(&relayd
->data_sock
, relayd_sock
);
2749 ret
= lttcomm_create_sock(&relayd
->data_sock
);
2750 /* Immediately try to close the created socket if valid. */
2751 if (relayd
->data_sock
.fd
>= 0) {
2752 if (close(relayd
->data_sock
.fd
)) {
2753 PERROR("close relayd data socket");
2756 /* Handle create_sock error. */
2761 /* Assign new file descriptor */
2762 relayd
->data_sock
.fd
= fd
;
2765 ERR("Unknown relayd socket type (%d)", sock_type
);
2770 DBG("Consumer %s socket created successfully with net idx %" PRIu64
" (fd: %d)",
2771 sock_type
== LTTNG_STREAM_CONTROL
? "control" : "data",
2772 relayd
->net_seq_idx
, fd
);
2775 * Add relayd socket pair to consumer data hashtable. If object already
2776 * exists or on error, the function gracefully returns.
2784 /* Close received socket if valid. */
2787 PERROR("close received socket");
2792 if (relayd_created
) {
2800 * Try to lock the stream mutex.
2802 * On success, 1 is returned else 0 indicating that the mutex is NOT lock.
2804 static int stream_try_lock(struct lttng_consumer_stream
*stream
)
2811 * Try to lock the stream mutex. On failure, we know that the stream is
2812 * being used else where hence there is data still being extracted.
2814 ret
= pthread_mutex_trylock(&stream
->lock
);
2816 /* For both EBUSY and EINVAL error, the mutex is NOT locked. */
2828 * Search for a relayd associated to the session id and return the reference.
2830 * A rcu read side lock MUST be acquire before calling this function and locked
2831 * until the relayd object is no longer necessary.
2833 static struct consumer_relayd_sock_pair
*find_relayd_by_session_id(uint64_t id
)
2835 struct lttng_ht_iter iter
;
2836 struct consumer_relayd_sock_pair
*relayd
= NULL
;
2838 /* Iterate over all relayd since they are indexed by net_seq_idx. */
2839 cds_lfht_for_each_entry(consumer_data
.relayd_ht
->ht
, &iter
.iter
, relayd
,
2842 * Check by sessiond id which is unique here where the relayd session
2843 * id might not be when having multiple relayd.
2845 if (relayd
->sessiond_session_id
== id
) {
2846 /* Found the relayd. There can be only one per id. */
2858 * Check if for a given session id there is still data needed to be extract
2861 * Return 1 if data is pending or else 0 meaning ready to be read.
2863 int consumer_data_pending(uint64_t id
)
2866 struct lttng_ht_iter iter
;
2867 struct lttng_ht
*ht
;
2868 struct lttng_consumer_stream
*stream
;
2869 struct consumer_relayd_sock_pair
*relayd
= NULL
;
2870 int (*data_pending
)(struct lttng_consumer_stream
*);
2872 DBG("Consumer data pending command on session id %" PRIu64
, id
);
2875 pthread_mutex_lock(&consumer_data
.lock
);
2877 switch (consumer_data
.type
) {
2878 case LTTNG_CONSUMER_KERNEL
:
2879 data_pending
= lttng_kconsumer_data_pending
;
2881 case LTTNG_CONSUMER32_UST
:
2882 case LTTNG_CONSUMER64_UST
:
2883 data_pending
= lttng_ustconsumer_data_pending
;
2886 ERR("Unknown consumer data type");
2890 /* Ease our life a bit */
2891 ht
= consumer_data
.stream_list_ht
;
2893 relayd
= find_relayd_by_session_id(id
);
2895 /* Send init command for data pending. */
2896 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
2897 ret
= relayd_begin_data_pending(&relayd
->control_sock
,
2898 relayd
->relayd_session_id
);
2899 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2901 /* Communication error thus the relayd so no data pending. */
2902 goto data_not_pending
;
2906 cds_lfht_for_each_entry_duplicate(ht
->ht
,
2907 ht
->hash_fct(&id
, lttng_ht_seed
),
2909 &iter
.iter
, stream
, node_session_id
.node
) {
2910 /* If this call fails, the stream is being used hence data pending. */
2911 ret
= stream_try_lock(stream
);
2917 * A removed node from the hash table indicates that the stream has
2918 * been deleted thus having a guarantee that the buffers are closed
2919 * on the consumer side. However, data can still be transmitted
2920 * over the network so don't skip the relayd check.
2922 ret
= cds_lfht_is_node_deleted(&stream
->node
.node
);
2924 /* Check the stream if there is data in the buffers. */
2925 ret
= data_pending(stream
);
2927 pthread_mutex_unlock(&stream
->lock
);
2934 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
2935 if (stream
->metadata_flag
) {
2936 ret
= relayd_quiescent_control(&relayd
->control_sock
,
2937 stream
->relayd_stream_id
);
2939 ret
= relayd_data_pending(&relayd
->control_sock
,
2940 stream
->relayd_stream_id
,
2941 stream
->next_net_seq_num
- 1);
2943 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2945 pthread_mutex_unlock(&stream
->lock
);
2949 pthread_mutex_unlock(&stream
->lock
);
2953 unsigned int is_data_inflight
= 0;
2955 /* Send init command for data pending. */
2956 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
2957 ret
= relayd_end_data_pending(&relayd
->control_sock
,
2958 relayd
->relayd_session_id
, &is_data_inflight
);
2959 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
2961 goto data_not_pending
;
2963 if (is_data_inflight
) {
2969 * Finding _no_ node in the hash table and no inflight data means that the
2970 * stream(s) have been removed thus data is guaranteed to be available for
2971 * analysis from the trace files.
2975 /* Data is available to be read by a viewer. */
2976 pthread_mutex_unlock(&consumer_data
.lock
);
2981 /* Data is still being extracted from buffers. */
2982 pthread_mutex_unlock(&consumer_data
.lock
);
2988 * Send a ret code status message to the sessiond daemon.
2990 * Return the sendmsg() return value.
2992 int consumer_send_status_msg(int sock
, int ret_code
)
2994 struct lttcomm_consumer_status_msg msg
;
2996 msg
.ret_code
= ret_code
;
2998 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));
3002 * Send a channel status message to the sessiond daemon.
3004 * Return the sendmsg() return value.
3006 int consumer_send_status_channel(int sock
,
3007 struct lttng_consumer_channel
*channel
)
3009 struct lttcomm_consumer_status_channel msg
;
3014 msg
.ret_code
= -LTTNG_ERR_UST_CHAN_FAIL
;
3016 msg
.ret_code
= LTTNG_OK
;
3017 msg
.key
= channel
->key
;
3018 msg
.stream_count
= channel
->streams
.count
;
3021 return lttcomm_send_unix_sock(sock
, &msg
, sizeof(msg
));