2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <lttng/ust-ctl.h>
27 #include <sys/socket.h>
29 #include <sys/types.h>
32 #include <urcu/list.h>
35 #include <common/common.h>
36 #include <common/sessiond-comm/sessiond-comm.h>
37 #include <common/relayd/relayd.h>
38 #include <common/compat/fcntl.h>
39 #include <common/consumer-metadata-cache.h>
40 #include <common/consumer-timer.h>
42 #include "ust-consumer.h"
44 extern struct lttng_consumer_global_data consumer_data
;
45 extern int consumer_poll_timeout
;
46 extern volatile int consumer_quit
;
49 * Free channel object and all streams associated with it. This MUST be used
50 * only and only if the channel has _NEVER_ been added to the global channel
53 static void destroy_channel(struct lttng_consumer_channel
*channel
)
55 struct lttng_consumer_stream
*stream
, *stmp
;
59 DBG("UST consumer cleaning stream list");
61 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
63 cds_list_del(&stream
->send_node
);
64 ustctl_destroy_stream(stream
->ustream
);
69 * If a channel is available meaning that was created before the streams
73 lttng_ustconsumer_del_channel(channel
);
79 * Add channel to internal consumer state.
81 * Returns 0 on success or else a negative value.
83 static int add_channel(struct lttng_consumer_channel
*channel
,
84 struct lttng_consumer_local_data
*ctx
)
91 if (ctx
->on_recv_channel
!= NULL
) {
92 ret
= ctx
->on_recv_channel(channel
);
94 ret
= consumer_add_channel(channel
, ctx
);
96 /* Most likely an ENOMEM. */
97 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
101 ret
= consumer_add_channel(channel
, ctx
);
104 DBG("UST consumer channel added (key: %" PRIu64
")", channel
->key
);
111 * Allocate and return a consumer channel object.
113 static struct lttng_consumer_channel
*allocate_channel(uint64_t session_id
,
114 const char *pathname
, const char *name
, uid_t uid
, gid_t gid
,
115 int relayd_id
, uint64_t key
, enum lttng_event_output output
)
120 return consumer_allocate_channel(key
, session_id
, pathname
, name
, uid
, gid
,
125 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
126 * error value if applicable is set in it else it is kept untouched.
128 * Return NULL on error else the newly allocated stream object.
130 static struct lttng_consumer_stream
*allocate_stream(int cpu
, int key
,
131 struct lttng_consumer_channel
*channel
,
132 struct lttng_consumer_local_data
*ctx
, int *_alloc_ret
)
135 struct lttng_consumer_stream
*stream
= NULL
;
140 stream
= consumer_allocate_stream(channel
->key
,
142 LTTNG_CONSUMER_ACTIVE_STREAM
,
151 if (stream
== NULL
) {
155 * We could not find the channel. Can happen if cpu hotplug
156 * happens while tearing down.
158 DBG3("Could not find channel");
163 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_OUTFD_ERROR
);
169 stream
->chan
= channel
;
173 *_alloc_ret
= alloc_ret
;
179 * Send the given stream pointer to the corresponding thread.
181 * Returns 0 on success else a negative value.
183 static int send_stream_to_thread(struct lttng_consumer_stream
*stream
,
184 struct lttng_consumer_local_data
*ctx
)
186 int ret
, stream_pipe
;
188 /* Get the right pipe where the stream will be sent. */
189 if (stream
->metadata_flag
) {
190 stream_pipe
= ctx
->consumer_metadata_pipe
[1];
192 stream_pipe
= ctx
->consumer_data_pipe
[1];
196 ret
= write(stream_pipe
, &stream
, sizeof(stream
));
197 } while (ret
< 0 && errno
== EINTR
);
199 PERROR("Consumer write %s stream to pipe %d",
200 stream
->metadata_flag
? "metadata" : "data", stream_pipe
);
207 * Search for a relayd object related to the stream. If found, send the stream
210 * On success, returns 0 else a negative value.
212 static int send_stream_to_relayd(struct lttng_consumer_stream
*stream
)
215 struct consumer_relayd_sock_pair
*relayd
;
219 relayd
= consumer_find_relayd(stream
->net_seq_idx
);
220 if (relayd
!= NULL
) {
221 pthread_mutex_lock(&relayd
->ctrl_sock_mutex
);
222 /* Add stream on the relayd */
223 ret
= relayd_add_stream(&relayd
->control_sock
, stream
->name
,
224 stream
->chan
->pathname
, &stream
->relayd_stream_id
);
225 pthread_mutex_unlock(&relayd
->ctrl_sock_mutex
);
229 } else if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
230 ERR("Network sequence index %" PRIu64
" unknown. Not adding stream.",
231 stream
->net_seq_idx
);
241 * Create streams for the given channel using liblttng-ust-ctl.
243 * Return 0 on success else a negative value.
245 static int create_ust_streams(struct lttng_consumer_channel
*channel
,
246 struct lttng_consumer_local_data
*ctx
)
249 struct ustctl_consumer_stream
*ustream
;
250 struct lttng_consumer_stream
*stream
;
256 * While a stream is available from ustctl. When NULL is returned, we've
257 * reached the end of the possible stream for the channel.
259 while ((ustream
= ustctl_create_stream(channel
->uchan
, cpu
))) {
262 wait_fd
= ustctl_stream_get_wait_fd(ustream
);
264 /* Allocate consumer stream object. */
265 stream
= allocate_stream(cpu
, wait_fd
, channel
, ctx
, &ret
);
269 stream
->ustream
= ustream
;
271 * Store it so we can save multiple function calls afterwards since
272 * this value is used heavily in the stream threads. This is UST
273 * specific so this is why it's done after allocation.
275 stream
->wait_fd
= wait_fd
;
278 * Order is important this is why a list is used. On error, the caller
279 * should clean this list.
281 cds_list_add_tail(&stream
->send_node
, &channel
->streams
.head
);
283 ret
= ustctl_get_max_subbuf_size(stream
->ustream
,
284 &stream
->max_sb_size
);
286 ERR("ustctl_get_max_subbuf_size failed for stream %s",
291 /* Do actions once stream has been received. */
292 if (ctx
->on_recv_stream
) {
293 ret
= ctx
->on_recv_stream(stream
);
299 DBG("UST consumer add stream %s (key: %" PRIu64
") with relayd id %" PRIu64
,
300 stream
->name
, stream
->key
, stream
->relayd_stream_id
);
302 /* Set next CPU stream. */
303 channel
->streams
.count
= ++cpu
;
305 /* Keep stream reference when creating metadata. */
306 if (channel
->type
== CONSUMER_CHANNEL_TYPE_METADATA
) {
307 channel
->metadata_stream
= stream
;
319 * Create an UST channel with the given attributes and send it to the session
320 * daemon using the ust ctl API.
322 * Return 0 on success or else a negative value.
324 static int create_ust_channel(struct ustctl_consumer_channel_attr
*attr
,
325 struct ustctl_consumer_channel
**chanp
)
328 struct ustctl_consumer_channel
*channel
;
333 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
334 "subbuf_size: %" PRIu64
", num_subbuf: %" PRIu64
", "
335 "switch_timer_interval: %u, read_timer_interval: %u, "
336 "output: %d, type: %d", attr
->overwrite
, attr
->subbuf_size
,
337 attr
->num_subbuf
, attr
->switch_timer_interval
,
338 attr
->read_timer_interval
, attr
->output
, attr
->type
);
340 channel
= ustctl_create_channel(attr
);
355 * Send a single given stream to the session daemon using the sock.
357 * Return 0 on success else a negative value.
359 static int send_sessiond_stream(int sock
, struct lttng_consumer_stream
*stream
)
366 DBG2("UST consumer sending stream %" PRIu64
" to sessiond", stream
->key
);
368 /* Send stream to session daemon. */
369 ret
= ustctl_send_stream_to_sessiond(sock
, stream
->ustream
);
379 * Send channel to sessiond.
381 * Return 0 on success or else a negative value.
383 static int send_sessiond_channel(int sock
,
384 struct lttng_consumer_channel
*channel
,
385 struct lttng_consumer_local_data
*ctx
, int *relayd_error
)
388 struct lttng_consumer_stream
*stream
;
394 DBG("UST consumer sending channel %s to sessiond", channel
->name
);
396 /* Send channel to sessiond. */
397 ret
= ustctl_send_channel_to_sessiond(sock
, channel
->uchan
);
402 ret
= ustctl_channel_close_wakeup_fd(channel
->uchan
);
407 /* The channel was sent successfully to the sessiond at this point. */
408 cds_list_for_each_entry(stream
, &channel
->streams
.head
, send_node
) {
409 /* Try to send the stream to the relayd if one is available. */
410 ret
= send_stream_to_relayd(stream
);
413 * Flag that the relayd was the problem here probably due to a
414 * communicaton error on the socket.
422 /* Send stream to session daemon. */
423 ret
= send_sessiond_stream(sock
, stream
);
429 /* Tell sessiond there is no more stream. */
430 ret
= ustctl_send_stream_to_sessiond(sock
, NULL
);
435 DBG("UST consumer NULL stream sent to sessiond");
444 * Creates a channel and streams and add the channel it to the channel internal
445 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
448 * Return 0 on success or else, a negative value is returned and the channel
449 * MUST be destroyed by consumer_del_channel().
451 static int ask_channel(struct lttng_consumer_local_data
*ctx
, int sock
,
452 struct lttng_consumer_channel
*channel
,
453 struct ustctl_consumer_channel_attr
*attr
)
462 * This value is still used by the kernel consumer since for the kernel,
463 * the stream ownership is not IN the consumer so we need to have the
464 * number of left stream that needs to be initialized so we can know when
465 * to delete the channel (see consumer.c).
467 * As for the user space tracer now, the consumer creates and sends the
468 * stream to the session daemon which only sends them to the application
469 * once every stream of a channel is received making this value useless
470 * because we they will be added to the poll thread before the application
471 * receives them. This ensures that a stream can not hang up during
472 * initilization of a channel.
474 channel
->nb_init_stream_left
= 0;
476 /* The reply msg status is handled in the following call. */
477 ret
= create_ust_channel(attr
, &channel
->uchan
);
482 channel
->wait_fd
= ustctl_channel_get_wait_fd(channel
->uchan
);
488 /* Open all streams for this channel. */
489 ret
= create_ust_streams(channel
, ctx
);
499 * Send all stream of a channel to the right thread handling it.
501 * On error, return a negative value else 0 on success.
503 static int send_streams_to_thread(struct lttng_consumer_channel
*channel
,
504 struct lttng_consumer_local_data
*ctx
)
507 struct lttng_consumer_stream
*stream
, *stmp
;
512 /* Send streams to the corresponding thread. */
513 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->streams
.head
,
515 /* Sending the stream to the thread. */
516 ret
= send_stream_to_thread(stream
, ctx
);
519 * If we are unable to send the stream to the thread, there is
520 * a big problem so just stop everything.
525 /* Remove node from the channel stream list. */
526 cds_list_del(&stream
->send_node
);
534 * Write metadata to the given channel using ustctl to convert the string to
536 * Called only from consumer_metadata_cache_write.
537 * The metadata cache lock MUST be acquired to write in the cache.
539 * Return 0 on success else a negative value.
541 int lttng_ustconsumer_push_metadata(struct lttng_consumer_channel
*metadata
,
542 const char *metadata_str
, uint64_t target_offset
, uint64_t len
)
547 assert(metadata_str
);
549 DBG("UST consumer writing metadata to channel %s", metadata
->name
);
551 assert(target_offset
<= metadata
->metadata_cache
->max_offset
);
552 ret
= ustctl_write_metadata_to_channel(metadata
->uchan
,
553 metadata_str
+ target_offset
, len
);
555 ERR("ustctl write metadata fail with ret %d, len %ld", ret
, len
);
559 ustctl_flush_buffer(metadata
->metadata_stream
->ustream
, 1);
566 * Flush channel's streams using the given key to retrieve the channel.
568 * Return 0 on success else an LTTng error code.
570 static int flush_channel(uint64_t chan_key
)
573 struct lttng_consumer_channel
*channel
;
574 struct lttng_consumer_stream
*stream
;
576 struct lttng_ht_iter iter
;
578 DBG("UST consumer flush channel key %lu", chan_key
);
580 channel
= consumer_find_channel(chan_key
);
582 ERR("UST consumer flush channel %lu not found", chan_key
);
583 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
587 ht
= consumer_data
.stream_per_chan_id_ht
;
589 /* For each stream of the channel id, flush it. */
591 cds_lfht_for_each_entry_duplicate(ht
->ht
,
592 ht
->hash_fct(&channel
->key
, lttng_ht_seed
), ht
->match_fct
,
593 &channel
->key
, &iter
.iter
, stream
, node_channel_id
.node
) {
594 ustctl_flush_buffer(stream
->ustream
, 1);
603 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
605 * Return 0 on success else an LTTng error code.
607 static int close_metadata(uint64_t chan_key
)
610 struct lttng_consumer_channel
*channel
;
612 DBG("UST consumer close metadata key %lu", chan_key
);
614 channel
= consumer_find_channel(chan_key
);
616 ERR("UST consumer close metadata %lu not found", chan_key
);
617 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
621 ret
= ustctl_stream_close_wakeup_fd(channel
->metadata_stream
->ustream
);
623 ERR("UST consumer unable to close fd of metadata (ret: %d)", ret
);
624 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
627 if (channel
->switch_timer_enabled
== 1) {
628 DBG("Deleting timer on metadata channel");
629 consumer_timer_switch_stop(channel
);
631 consumer_metadata_cache_destroy(channel
);
638 * RCU read side lock MUST be acquired before calling this function.
640 * Return 0 on success else an LTTng error code.
642 static int setup_metadata(struct lttng_consumer_local_data
*ctx
, uint64_t key
)
645 struct lttng_consumer_channel
*metadata
;
647 DBG("UST consumer setup metadata key %lu", key
);
649 metadata
= consumer_find_channel(key
);
651 ERR("UST consumer push metadata %" PRIu64
" not found", key
);
652 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
657 * Send metadata stream to relayd if one available. Availability is
658 * known if the stream is still in the list of the channel.
660 if (cds_list_empty(&metadata
->streams
.head
)) {
661 ERR("Metadata channel key %" PRIu64
", no stream available.", key
);
662 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
666 /* Send metadata stream to relayd if needed. */
667 ret
= send_stream_to_relayd(metadata
->metadata_stream
);
669 ret
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
673 ret
= send_streams_to_thread(metadata
, ctx
);
676 * If we are unable to send the stream to the thread, there is
677 * a big problem so just stop everything.
679 ret
= LTTCOMM_CONSUMERD_FATAL
;
682 /* List MUST be empty after or else it could be reused. */
683 assert(cds_list_empty(&metadata
->streams
.head
));
692 * Receive the metadata updates from the sessiond.
694 int lttng_ustconsumer_recv_metadata(int sock
, uint64_t key
, uint64_t offset
,
695 uint64_t len
, struct lttng_consumer_channel
*channel
)
697 int ret
, ret_code
= LTTNG_OK
;
700 DBG("UST consumer push metadata key %lu of len %lu", key
, len
);
702 metadata_str
= zmalloc(len
* sizeof(char));
704 PERROR("zmalloc metadata string");
705 ret_code
= LTTCOMM_CONSUMERD_ENOMEM
;
709 /* Receive metadata string. */
710 ret
= lttcomm_recv_unix_sock(sock
, metadata_str
, len
);
712 /* Session daemon is dead so return gracefully. */
717 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
718 ret
= consumer_metadata_cache_write(channel
, offset
, len
, metadata_str
);
720 /* Unable to handle metadata. Notify session daemon. */
721 ret_code
= LTTCOMM_CONSUMERD_ERROR_METADATA
;
723 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
725 while (consumer_metadata_cache_flushed(channel
, offset
+ len
)) {
726 DBG("Waiting for metadata to be flushed");
727 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME
);
737 * Receive command from session daemon and process it.
739 * Return 1 on success else a negative value or 0.
741 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data
*ctx
,
742 int sock
, struct pollfd
*consumer_sockpoll
)
745 enum lttng_error_code ret_code
= LTTNG_OK
;
746 struct lttcomm_consumer_msg msg
;
747 struct lttng_consumer_channel
*channel
= NULL
;
749 ret
= lttcomm_recv_unix_sock(sock
, &msg
, sizeof(msg
));
750 if (ret
!= sizeof(msg
)) {
751 DBG("Consumer received unexpected message size %zd (expects %zu)",
753 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
755 * The ret value might 0 meaning an orderly shutdown but this is ok
756 * since the caller handles this.
760 if (msg
.cmd_type
== LTTNG_CONSUMER_STOP
) {
762 * Notify the session daemon that the command is completed.
764 * On transport layer error, the function call will print an error
765 * message so handling the returned code is a bit useless since we
766 * return an error code anyway.
768 (void) consumer_send_status_msg(sock
, ret_code
);
772 /* relayd needs RCU read-side lock */
775 switch (msg
.cmd_type
) {
776 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET
:
778 /* Session daemon status message are handled in the following call. */
779 ret
= consumer_add_relayd_socket(msg
.u
.relayd_sock
.net_index
,
780 msg
.u
.relayd_sock
.type
, ctx
, sock
, consumer_sockpoll
,
781 &msg
.u
.relayd_sock
.sock
, msg
.u
.relayd_sock
.session_id
);
784 case LTTNG_CONSUMER_DESTROY_RELAYD
:
786 uint64_t index
= msg
.u
.destroy_relayd
.net_seq_idx
;
787 struct consumer_relayd_sock_pair
*relayd
;
789 DBG("UST consumer destroying relayd %" PRIu64
, index
);
791 /* Get relayd reference if exists. */
792 relayd
= consumer_find_relayd(index
);
793 if (relayd
== NULL
) {
794 DBG("Unable to find relayd %" PRIu64
, index
);
795 ret_code
= LTTNG_ERR_NO_CONSUMER
;
799 * Each relayd socket pair has a refcount of stream attached to it
800 * which tells if the relayd is still active or not depending on the
803 * This will set the destroy flag of the relayd object and destroy it
804 * if the refcount reaches zero when called.
806 * The destroy can happen either here or when a stream fd hangs up.
809 consumer_flag_relayd_for_destroy(relayd
);
812 goto end_msg_sessiond
;
814 case LTTNG_CONSUMER_UPDATE_STREAM
:
819 case LTTNG_CONSUMER_DATA_PENDING
:
821 int ret
, is_data_pending
;
822 uint64_t id
= msg
.u
.data_pending
.session_id
;
824 DBG("UST consumer data pending command for id %" PRIu64
, id
);
826 is_data_pending
= consumer_data_pending(id
);
828 /* Send back returned value to session daemon */
829 ret
= lttcomm_send_unix_sock(sock
, &is_data_pending
,
830 sizeof(is_data_pending
));
832 DBG("Error when sending the data pending ret code: %d", ret
);
836 * No need to send back a status message since the data pending
837 * returned value is the response.
841 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION
:
844 struct ustctl_consumer_channel_attr attr
;
846 /* Create a plain object and reserve a channel key. */
847 channel
= allocate_channel(msg
.u
.ask_channel
.session_id
,
848 msg
.u
.ask_channel
.pathname
, msg
.u
.ask_channel
.name
,
849 msg
.u
.ask_channel
.uid
, msg
.u
.ask_channel
.gid
,
850 msg
.u
.ask_channel
.relayd_id
, msg
.u
.ask_channel
.key
,
851 (enum lttng_event_output
) msg
.u
.ask_channel
.output
);
853 goto end_channel_error
;
856 /* Build channel attributes from received message. */
857 attr
.subbuf_size
= msg
.u
.ask_channel
.subbuf_size
;
858 attr
.num_subbuf
= msg
.u
.ask_channel
.num_subbuf
;
859 attr
.overwrite
= msg
.u
.ask_channel
.overwrite
;
860 attr
.switch_timer_interval
= msg
.u
.ask_channel
.switch_timer_interval
;
861 attr
.read_timer_interval
= msg
.u
.ask_channel
.read_timer_interval
;
862 attr
.chan_id
= msg
.u
.ask_channel
.chan_id
;
863 memcpy(attr
.uuid
, msg
.u
.ask_channel
.uuid
, sizeof(attr
.uuid
));
865 /* Translate the event output type to UST. */
866 switch (channel
->output
) {
867 case LTTNG_EVENT_SPLICE
:
868 /* Splice not supported so fallback on mmap(). */
869 case LTTNG_EVENT_MMAP
:
871 attr
.output
= CONSUMER_CHANNEL_MMAP
;
875 /* Translate and save channel type. */
876 switch (msg
.u
.ask_channel
.type
) {
877 case LTTNG_UST_CHAN_PER_CPU
:
878 channel
->type
= CONSUMER_CHANNEL_TYPE_DATA
;
879 attr
.type
= LTTNG_UST_CHAN_PER_CPU
;
881 case LTTNG_UST_CHAN_METADATA
:
882 channel
->type
= CONSUMER_CHANNEL_TYPE_METADATA
;
883 attr
.type
= LTTNG_UST_CHAN_METADATA
;
890 ret
= ask_channel(ctx
, sock
, channel
, &attr
);
892 goto end_channel_error
;
896 * Add the channel to the internal state AFTER all streams were created
897 * and successfully sent to session daemon. This way, all streams must
898 * be ready before this channel is visible to the threads.
900 ret
= add_channel(channel
, ctx
);
902 goto end_channel_error
;
907 * Channel and streams are now created. Inform the session daemon that
908 * everything went well and should wait to receive the channel and
909 * streams with ustctl API.
911 ret
= consumer_send_status_channel(sock
, channel
);
914 * There is probably a problem on the socket so the poll will get
915 * it and clean everything up.
920 if (msg
.u
.ask_channel
.type
== LTTNG_UST_CHAN_METADATA
) {
921 ret
= consumer_metadata_cache_allocate(channel
);
923 ERR("Allocating metadata cache");
924 goto end_channel_error
;
926 consumer_timer_switch_start(channel
, attr
.switch_timer_interval
);
927 attr
.switch_timer_interval
= 0;
932 case LTTNG_CONSUMER_GET_CHANNEL
:
934 int ret
, relayd_err
= 0;
935 uint64_t key
= msg
.u
.get_channel
.key
;
936 struct lttng_consumer_channel
*channel
;
938 channel
= consumer_find_channel(key
);
940 ERR("UST consumer get channel key %lu not found", key
);
941 ret_code
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
942 goto end_msg_sessiond
;
945 /* Inform sessiond that we are about to send channel and streams. */
946 ret
= consumer_send_status_msg(sock
, LTTNG_OK
);
948 /* Somehow, the session daemon is not responding anymore. */
952 /* Send everything to sessiond. */
953 ret
= send_sessiond_channel(sock
, channel
, ctx
, &relayd_err
);
957 * We were unable to send to the relayd the stream so avoid
958 * sending back a fatal error to the thread since this is OK
959 * and the consumer can continue its work.
961 ret_code
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
962 goto end_msg_sessiond
;
965 * The communicaton was broken hence there is a bad state between
966 * the consumer and sessiond so stop everything.
971 ret
= send_streams_to_thread(channel
, ctx
);
974 * If we are unable to send the stream to the thread, there is
975 * a big problem so just stop everything.
979 /* List MUST be empty after or else it could be reused. */
980 assert(cds_list_empty(&channel
->streams
.head
));
982 goto end_msg_sessiond
;
984 case LTTNG_CONSUMER_DESTROY_CHANNEL
:
986 uint64_t key
= msg
.u
.destroy_channel
.key
;
987 struct lttng_consumer_channel
*channel
;
989 channel
= consumer_find_channel(key
);
991 ERR("UST consumer get channel key %lu not found", key
);
992 ret_code
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
993 goto end_msg_sessiond
;
996 destroy_channel(channel
);
998 goto end_msg_sessiond
;
1000 case LTTNG_CONSUMER_CLOSE_METADATA
:
1004 ret
= close_metadata(msg
.u
.close_metadata
.key
);
1009 goto end_msg_sessiond
;
1011 case LTTNG_CONSUMER_FLUSH_CHANNEL
:
1015 ret
= flush_channel(msg
.u
.flush_channel
.key
);
1020 goto end_msg_sessiond
;
1022 case LTTNG_CONSUMER_PUSH_METADATA
:
1025 uint64_t len
= msg
.u
.push_metadata
.len
;
1026 uint64_t key
= msg
.u
.push_metadata
.key
;
1027 uint64_t offset
= msg
.u
.push_metadata
.target_offset
;
1028 struct lttng_consumer_channel
*channel
;
1030 DBG("UST consumer push metadata key %lu of len %lu", key
, len
);
1032 channel
= consumer_find_channel(key
);
1034 ERR("UST consumer push metadata %lu not found", key
);
1035 ret_code
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1038 /* Tell session daemon we are ready to receive the metadata. */
1039 ret
= consumer_send_status_msg(sock
, LTTNG_OK
);
1041 /* Somehow, the session daemon is not responding anymore. */
1045 /* Wait for more data. */
1046 if (lttng_consumer_poll_socket(consumer_sockpoll
) < 0) {
1050 ret
= lttng_ustconsumer_recv_metadata(sock
, key
, offset
,
1053 /* error receiving from sessiond */
1057 goto end_msg_sessiond
;
1060 case LTTNG_CONSUMER_SETUP_METADATA
:
1064 ret
= setup_metadata(ctx
, msg
.u
.setup_metadata
.key
);
1068 goto end_msg_sessiond
;
1078 * Return 1 to indicate success since the 0 value can be a socket
1079 * shutdown during the recv() or send() call.
1085 * The returned value here is not useful since either way we'll return 1 to
1086 * the caller because the session daemon socket management is done
1087 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
1089 (void) consumer_send_status_msg(sock
, ret_code
);
1095 * Free channel here since no one has a reference to it. We don't
1096 * free after that because a stream can store this pointer.
1098 destroy_channel(channel
);
1100 /* We have to send a status channel message indicating an error. */
1101 ret
= consumer_send_status_channel(sock
, NULL
);
1103 /* Stop everything if session daemon can not be notified. */
1110 /* This will issue a consumer stop. */
1115 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
1116 * compiled out, we isolate it in this library.
1118 int lttng_ustctl_get_mmap_read_offset(struct lttng_consumer_stream
*stream
,
1122 assert(stream
->ustream
);
1124 return ustctl_get_mmap_read_offset(stream
->ustream
, off
);
1128 * Wrapper over the mmap() read offset from ust-ctl library. Since this can be
1129 * compiled out, we isolate it in this library.
1131 void *lttng_ustctl_get_mmap_base(struct lttng_consumer_stream
*stream
)
1134 assert(stream
->ustream
);
1136 return ustctl_get_mmap_base(stream
->ustream
);
1140 * Take a snapshot for a specific fd
1142 * Returns 0 on success, < 0 on error
1144 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream
*stream
)
1147 assert(stream
->ustream
);
1149 return ustctl_snapshot(stream
->ustream
);
1153 * Get the produced position
1155 * Returns 0 on success, < 0 on error
1157 int lttng_ustconsumer_get_produced_snapshot(
1158 struct lttng_consumer_stream
*stream
, unsigned long *pos
)
1161 assert(stream
->ustream
);
1164 return ustctl_snapshot_get_produced(stream
->ustream
, pos
);
1168 * Called when the stream signal the consumer that it has hang up.
1170 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream
*stream
)
1173 assert(stream
->ustream
);
1175 ustctl_flush_buffer(stream
->ustream
, 0);
1176 stream
->hangup_flush_done
= 1;
1179 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel
*chan
)
1182 assert(chan
->uchan
);
1184 ustctl_destroy_channel(chan
->uchan
);
1187 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream
*stream
)
1190 assert(stream
->ustream
);
1192 ustctl_destroy_stream(stream
->ustream
);
1195 int lttng_ustconsumer_read_subbuffer(struct lttng_consumer_stream
*stream
,
1196 struct lttng_consumer_local_data
*ctx
)
1198 unsigned long len
, subbuf_size
, padding
;
1202 struct ustctl_consumer_stream
*ustream
;
1205 assert(stream
->ustream
);
1208 DBG2("In UST read_subbuffer (wait_fd: %d, name: %s)", stream
->wait_fd
,
1211 /* Ease our life for what's next. */
1212 ustream
= stream
->ustream
;
1214 /* We can consume the 1 byte written into the wait_fd by UST */
1215 if (!stream
->hangup_flush_done
) {
1219 readlen
= read(stream
->wait_fd
, &dummy
, 1);
1220 } while (readlen
== -1 && errno
== EINTR
);
1221 if (readlen
== -1) {
1227 /* Get the next subbuffer */
1228 err
= ustctl_get_next_subbuf(ustream
);
1230 ret
= err
; /* ustctl_get_next_subbuf returns negative, caller expect positive. */
1232 * This is a debug message even for single-threaded consumer,
1233 * because poll() have more relaxed criterions than get subbuf,
1234 * so get_subbuf may fail for short race windows where poll()
1235 * would issue wakeups.
1237 DBG("Reserving sub buffer failed (everything is normal, "
1238 "it is due to concurrency) [ret: %d]", err
);
1241 assert(stream
->chan
->output
== CONSUMER_CHANNEL_MMAP
);
1242 /* Get the full padded subbuffer size */
1243 err
= ustctl_get_padded_subbuf_size(ustream
, &len
);
1246 /* Get subbuffer data size (without padding) */
1247 err
= ustctl_get_subbuf_size(ustream
, &subbuf_size
);
1250 /* Make sure we don't get a subbuffer size bigger than the padded */
1251 assert(len
>= subbuf_size
);
1253 padding
= len
- subbuf_size
;
1254 /* write the subbuffer to the tracefile */
1255 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, subbuf_size
, padding
);
1257 * The mmap operation should write subbuf_size amount of data when network
1258 * streaming or the full padding (len) size when we are _not_ streaming.
1260 if ((ret
!= subbuf_size
&& stream
->net_seq_idx
!= (uint64_t) -1ULL) ||
1261 (ret
!= len
&& stream
->net_seq_idx
== (uint64_t) -1ULL)) {
1263 * Display the error but continue processing to try to release the
1264 * subbuffer. This is a DBG statement since any unexpected kill or
1265 * signal, the application gets unregistered, relayd gets closed or
1266 * anything that affects the buffer lifetime will trigger this error.
1267 * So, for the sake of the user, don't print this error since it can
1268 * happen and it is OK with the code flow.
1270 DBG("Error writing to tracefile "
1271 "(ret: %zd != len: %lu != subbuf_size: %lu)",
1272 ret
, len
, subbuf_size
);
1274 err
= ustctl_put_next_subbuf(ustream
);
1282 * Called when a stream is created.
1284 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream
*stream
)
1287 char full_path
[PATH_MAX
];
1289 /* Opening the tracefile in write mode */
1290 if (stream
->net_seq_idx
!= (uint64_t) -1ULL) {
1294 ret
= snprintf(full_path
, sizeof(full_path
), "%s/%s",
1295 stream
->chan
->pathname
, stream
->name
);
1297 PERROR("snprintf on_recv_stream");
1301 ret
= run_as_open(full_path
, O_WRONLY
| O_CREAT
| O_TRUNC
,
1302 S_IRWXU
| S_IRWXG
| S_IRWXO
, stream
->uid
, stream
->gid
);
1304 PERROR("open stream path %s", full_path
);
1307 stream
->out_fd
= ret
;
1310 /* we return 0 to let the library handle the FD internally */
1318 * Check if data is still being extracted from the buffers for a specific
1319 * stream. Consumer data lock MUST be acquired before calling this function
1320 * and the stream lock.
1322 * Return 1 if the traced data are still getting read else 0 meaning that the
1323 * data is available for trace viewer reading.
1325 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream
*stream
)
1330 assert(stream
->ustream
);
1332 DBG("UST consumer checking data pending");
1334 ret
= ustctl_get_next_subbuf(stream
->ustream
);
1336 /* There is still data so let's put back this subbuffer. */
1337 ret
= ustctl_put_subbuf(stream
->ustream
);
1339 ret
= 1; /* Data is pending */
1343 /* Data is NOT pending so ready to be read. */
1351 * Close every metadata stream wait fd of the metadata hash table. This
1352 * function MUST be used very carefully so not to run into a race between the
1353 * metadata thread handling streams and this function closing their wait fd.
1355 * For UST, this is used when the session daemon hangs up. Its the metadata
1356 * producer so calling this is safe because we are assured that no state change
1357 * can occur in the metadata thread for the streams in the hash table.
1359 void lttng_ustconsumer_close_metadata(struct lttng_ht
*metadata_ht
)
1362 struct lttng_ht_iter iter
;
1363 struct lttng_consumer_stream
*stream
;
1365 assert(metadata_ht
);
1366 assert(metadata_ht
->ht
);
1368 DBG("UST consumer closing all metadata streams");
1371 cds_lfht_for_each_entry(metadata_ht
->ht
, &iter
.iter
, stream
,
1373 int fd
= stream
->wait_fd
;
1376 * Whatever happens here we have to continue to try to close every
1377 * streams. Let's report at least the error on failure.
1379 ret
= ustctl_stream_close_wakeup_fd(stream
->ustream
);
1381 ERR("Unable to close metadata stream fd %d ret %d", fd
, ret
);
1383 DBG("Metadata wait fd %d closed", fd
);
1388 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream
*stream
)
1392 ret
= ustctl_stream_close_wakeup_fd(stream
->ustream
);
1394 ERR("Unable to close wakeup fd");
1398 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data
*ctx
,
1399 struct lttng_consumer_channel
*channel
)
1401 struct lttcomm_metadata_request_msg request
;
1402 struct lttcomm_consumer_msg msg
;
1403 enum lttng_error_code ret_code
= LTTNG_OK
;
1404 uint64_t len
, key
, offset
;
1408 assert(channel
->metadata_cache
);
1410 /* send the metadata request to sessiond */
1411 switch (consumer_data
.type
) {
1412 case LTTNG_CONSUMER64_UST
:
1413 request
.bits_per_long
= 64;
1415 case LTTNG_CONSUMER32_UST
:
1416 request
.bits_per_long
= 32;
1419 request
.bits_per_long
= 0;
1423 request
.session_id
= channel
->session_id
;
1424 request
.uid
= channel
->uid
;
1425 request
.key
= channel
->key
;
1426 DBG("Sending metadata request to sessiond, session %" PRIu64
,
1427 channel
->session_id
);
1429 ret
= lttcomm_send_unix_sock(ctx
->consumer_metadata_socket
, &request
,
1432 ERR("Asking metadata to sessiond");
1436 /* Receive the metadata from sessiond */
1437 ret
= lttcomm_recv_unix_sock(ctx
->consumer_metadata_socket
, &msg
,
1439 if (ret
!= sizeof(msg
)) {
1440 DBG("Consumer received unexpected message size %d (expects %lu)",
1442 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_ERROR_RECV_CMD
);
1444 * The ret value might 0 meaning an orderly shutdown but this is ok
1445 * since the caller handles this.
1450 if (msg
.cmd_type
== LTTNG_ERR_UND
) {
1451 /* No registry found */
1452 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
,
1456 } else if (msg
.cmd_type
!= LTTNG_CONSUMER_PUSH_METADATA
) {
1457 ERR("Unexpected cmd_type received %d", msg
.cmd_type
);
1462 len
= msg
.u
.push_metadata
.len
;
1463 key
= msg
.u
.push_metadata
.key
;
1464 offset
= msg
.u
.push_metadata
.target_offset
;
1466 assert(key
== channel
->key
);
1468 DBG("No new metadata to receive for key %" PRIu64
, key
);
1471 /* Tell session daemon we are ready to receive the metadata. */
1472 ret
= consumer_send_status_msg(ctx
->consumer_metadata_socket
,
1474 if (ret
< 0 || len
== 0) {
1476 * Somehow, the session daemon is not responding anymore or there is
1477 * nothing to receive.
1482 ret_code
= lttng_ustconsumer_recv_metadata(ctx
->consumer_metadata_socket
,
1483 key
, offset
, len
, channel
);
1484 (void) consumer_send_status_msg(ctx
->consumer_metadata_socket
, ret_code
);