2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * SPDX-License-Identifier: GPL-2.0-only
12 #include <lttng/ust-ctl.h>
13 #include <lttng/ust-sigbus.h>
19 #include <sys/socket.h>
21 #include <sys/types.h>
24 #include <urcu/list.h>
29 #include <bin/lttng-consumerd/health-consumerd.h>
30 #include <common/common.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
32 #include <common/relayd/relayd.h>
33 #include <common/compat/fcntl.h>
34 #include <common/compat/endian.h>
35 #include <common/consumer/consumer-metadata-cache.h>
36 #include <common/consumer/consumer-stream.h>
37 #include <common/consumer/consumer-timer.h>
38 #include <common/utils.h>
39 #include <common/index/index.h>
40 #include <common/consumer/consumer.h>
41 #include <common/shm.h>
42 #include <common/optional.h>
44 #include "ust-consumer.h"
46 #define INT_MAX_STR_LEN 12 /* includes \0 */
48 extern struct lttng_consumer_global_data the_consumer_data;
49 extern int consumer_poll_timeout;
51 DEFINE_LTTNG_UST_SIGBUS_STATE();
54 * Free channel object and all streams associated with it. This MUST be used
55 * only and only if the channel has _NEVER_ been added to the global channel
58 static void destroy_channel(struct lttng_consumer_channel *channel)
60 struct lttng_consumer_stream *stream, *stmp;
64 DBG("UST consumer cleaning stream list");
66 cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head,
71 cds_list_del(&stream->send_node);
72 lttng_ust_ctl_destroy_stream(stream->ustream);
73 lttng_trace_chunk_put(stream->trace_chunk);
78 * If a channel is available meaning that was created before the streams
82 lttng_ustconsumer_del_channel(channel);
83 lttng_ustconsumer_free_channel(channel);
86 if (channel->trace_chunk) {
87 lttng_trace_chunk_put(channel->trace_chunk);
94 * Add channel to internal consumer state.
96 * Returns 0 on success or else a negative value.
98 static int add_channel(struct lttng_consumer_channel *channel,
99 struct lttng_consumer_local_data *ctx)
106 if (ctx->on_recv_channel != NULL) {
107 ret = ctx->on_recv_channel(channel);
109 ret = consumer_add_channel(channel, ctx);
110 } else if (ret < 0) {
111 /* Most likely an ENOMEM. */
112 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
116 ret = consumer_add_channel(channel, ctx);
119 DBG("UST consumer channel added (key: %" PRIu64 ")", channel->key);
126 * Allocate and return a consumer stream object. If _alloc_ret is not NULL, the
127 * error value if applicable is set in it else it is kept untouched.
129 * Return NULL on error else the newly allocated stream object.
131 static struct lttng_consumer_stream *allocate_stream(int cpu, int key,
132 struct lttng_consumer_channel *channel,
133 struct lttng_consumer_local_data *ctx, int *_alloc_ret)
136 struct lttng_consumer_stream *stream = NULL;
141 stream = consumer_stream_create(
148 channel->trace_chunk,
153 if (stream == NULL) {
157 * We could not find the channel. Can happen if cpu hotplug
158 * happens while tearing down.
160 DBG3("Could not find channel");
165 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_OUTFD_ERROR);
171 consumer_stream_update_channel_attributes(stream, channel);
175 *_alloc_ret = alloc_ret;
181 * Send the given stream pointer to the corresponding thread.
183 * Returns 0 on success else a negative value.
185 static int send_stream_to_thread(struct lttng_consumer_stream *stream,
186 struct lttng_consumer_local_data *ctx)
189 struct lttng_pipe *stream_pipe;
191 /* Get the right pipe where the stream will be sent. */
192 if (stream->metadata_flag) {
193 consumer_add_metadata_stream(stream);
194 stream_pipe = ctx->consumer_metadata_pipe;
196 consumer_add_data_stream(stream);
197 stream_pipe = ctx->consumer_data_pipe;
201 * From this point on, the stream's ownership has been moved away from
202 * the channel and it becomes globally visible. Hence, remove it from
203 * the local stream list to prevent the stream from being both local and
206 stream->globally_visible = 1;
207 cds_list_del(&stream->send_node);
209 ret = lttng_pipe_write(stream_pipe, &stream, sizeof(stream));
211 ERR("Consumer write %s stream to pipe %d",
212 stream->metadata_flag ? "metadata" : "data",
213 lttng_pipe_get_writefd(stream_pipe));
214 if (stream->metadata_flag) {
215 consumer_del_stream_for_metadata(stream);
217 consumer_del_stream_for_data(stream);
227 int get_stream_shm_path(char *stream_shm_path, const char *shm_path, int cpu)
229 char cpu_nr[INT_MAX_STR_LEN]; /* int max len */
232 strncpy(stream_shm_path, shm_path, PATH_MAX);
233 stream_shm_path[PATH_MAX - 1] = '\0';
234 ret = snprintf(cpu_nr, INT_MAX_STR_LEN, "%i", cpu);
239 strncat(stream_shm_path, cpu_nr,
240 PATH_MAX - strlen(stream_shm_path) - 1);
247 * Create streams for the given channel using liblttng-ust-ctl.
248 * The channel lock must be acquired by the caller.
250 * Return 0 on success else a negative value.
252 static int create_ust_streams(struct lttng_consumer_channel *channel,
253 struct lttng_consumer_local_data *ctx)
256 struct lttng_ust_ctl_consumer_stream *ustream;
257 struct lttng_consumer_stream *stream;
258 pthread_mutex_t *current_stream_lock = NULL;
264 * While a stream is available from ustctl. When NULL is returned, we've
265 * reached the end of the possible stream for the channel.
267 while ((ustream = lttng_ust_ctl_create_stream(channel->uchan, cpu))) {
269 int ust_metadata_pipe[2];
271 health_code_update();
273 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA && channel->monitor) {
274 ret = utils_create_pipe_cloexec_nonblock(ust_metadata_pipe);
276 ERR("Create ust metadata poll pipe");
279 wait_fd = ust_metadata_pipe[0];
281 wait_fd = lttng_ust_ctl_stream_get_wait_fd(ustream);
284 /* Allocate consumer stream object. */
285 stream = allocate_stream(cpu, wait_fd, channel, ctx, &ret);
289 stream->ustream = ustream;
291 * Store it so we can save multiple function calls afterwards since
292 * this value is used heavily in the stream threads. This is UST
293 * specific so this is why it's done after allocation.
295 stream->wait_fd = wait_fd;
298 * Increment channel refcount since the channel reference has now been
299 * assigned in the allocation process above.
301 if (stream->chan->monitor) {
302 uatomic_inc(&stream->chan->refcount);
305 pthread_mutex_lock(&stream->lock);
306 current_stream_lock = &stream->lock;
308 * Order is important this is why a list is used. On error, the caller
309 * should clean this list.
311 cds_list_add_tail(&stream->send_node, &channel->streams.head);
313 ret = lttng_ust_ctl_get_max_subbuf_size(stream->ustream,
314 &stream->max_sb_size);
316 ERR("lttng_ust_ctl_get_max_subbuf_size failed for stream %s",
321 /* Do actions once stream has been received. */
322 if (ctx->on_recv_stream) {
323 ret = ctx->on_recv_stream(stream);
329 DBG("UST consumer add stream %s (key: %" PRIu64 ") with relayd id %" PRIu64,
330 stream->name, stream->key, stream->relayd_stream_id);
332 /* Set next CPU stream. */
333 channel->streams.count = ++cpu;
335 /* Keep stream reference when creating metadata. */
336 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA) {
337 channel->metadata_stream = stream;
338 if (channel->monitor) {
339 /* Set metadata poll pipe if we created one */
340 memcpy(stream->ust_metadata_poll_pipe,
342 sizeof(ust_metadata_pipe));
345 pthread_mutex_unlock(&stream->lock);
346 current_stream_lock = NULL;
353 if (current_stream_lock) {
354 pthread_mutex_unlock(current_stream_lock);
359 static int open_ust_stream_fd(struct lttng_consumer_channel *channel, int cpu,
360 const struct lttng_credentials *session_credentials)
362 char shm_path[PATH_MAX];
365 if (!channel->shm_path[0]) {
366 return shm_create_anonymous("ust-consumer");
368 ret = get_stream_shm_path(shm_path, channel->shm_path, cpu);
372 return run_as_open(shm_path,
373 O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR,
374 lttng_credentials_get_uid(session_credentials),
375 lttng_credentials_get_gid(session_credentials));
382 * Create an UST channel with the given attributes and send it to the session
383 * daemon using the ust ctl API.
385 * Return 0 on success or else a negative value.
387 static int create_ust_channel(struct lttng_consumer_channel *channel,
388 struct lttng_ust_ctl_consumer_channel_attr *attr,
389 struct lttng_ust_ctl_consumer_channel **ust_chanp)
391 int ret, nr_stream_fds, i, j;
393 struct lttng_ust_ctl_consumer_channel *ust_channel;
398 assert(channel->buffer_credentials.is_set);
400 DBG3("Creating channel to ustctl with attr: [overwrite: %d, "
401 "subbuf_size: %" PRIu64 ", num_subbuf: %" PRIu64 ", "
402 "switch_timer_interval: %u, read_timer_interval: %u, "
403 "output: %d, type: %d", attr->overwrite, attr->subbuf_size,
404 attr->num_subbuf, attr->switch_timer_interval,
405 attr->read_timer_interval, attr->output, attr->type);
407 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA)
410 nr_stream_fds = lttng_ust_ctl_get_nr_stream_per_channel();
411 stream_fds = zmalloc(nr_stream_fds * sizeof(*stream_fds));
416 for (i = 0; i < nr_stream_fds; i++) {
417 stream_fds[i] = open_ust_stream_fd(channel, i,
418 &channel->buffer_credentials.value);
419 if (stream_fds[i] < 0) {
424 ust_channel = lttng_ust_ctl_create_channel(attr, stream_fds, nr_stream_fds);
429 channel->nr_stream_fds = nr_stream_fds;
430 channel->stream_fds = stream_fds;
431 *ust_chanp = ust_channel;
437 for (j = i - 1; j >= 0; j--) {
440 closeret = close(stream_fds[j]);
444 if (channel->shm_path[0]) {
445 char shm_path[PATH_MAX];
447 closeret = get_stream_shm_path(shm_path,
448 channel->shm_path, j);
450 ERR("Cannot get stream shm path");
452 closeret = run_as_unlink(shm_path,
453 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
454 channel->buffer_credentials)),
455 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
456 channel->buffer_credentials)));
458 PERROR("unlink %s", shm_path);
462 /* Try to rmdir all directories under shm_path root. */
463 if (channel->root_shm_path[0]) {
464 (void) run_as_rmdir_recursive(channel->root_shm_path,
465 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
466 channel->buffer_credentials)),
467 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
468 channel->buffer_credentials)),
469 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
477 * Send a single given stream to the session daemon using the sock.
479 * Return 0 on success else a negative value.
481 static int send_sessiond_stream(int sock, struct lttng_consumer_stream *stream)
488 DBG("UST consumer sending stream %" PRIu64 " to sessiond", stream->key);
490 /* Send stream to session daemon. */
491 ret = lttng_ust_ctl_send_stream_to_sessiond(sock, stream->ustream);
501 * Send channel to sessiond and relayd if applicable.
503 * Return 0 on success or else a negative value.
505 static int send_channel_to_sessiond_and_relayd(int sock,
506 struct lttng_consumer_channel *channel,
507 struct lttng_consumer_local_data *ctx, int *relayd_error)
509 int ret, ret_code = LTTCOMM_CONSUMERD_SUCCESS;
510 struct lttng_consumer_stream *stream;
511 uint64_t net_seq_idx = -1ULL;
517 DBG("UST consumer sending channel %s to sessiond", channel->name);
519 if (channel->relayd_id != (uint64_t) -1ULL) {
520 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
522 health_code_update();
524 /* Try to send the stream to the relayd if one is available. */
525 DBG("Sending stream %" PRIu64 " of channel \"%s\" to relayd",
526 stream->key, channel->name);
527 ret = consumer_send_relayd_stream(stream, stream->chan->pathname);
530 * Flag that the relayd was the problem here probably due to a
531 * communicaton error on the socket.
536 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
538 if (net_seq_idx == -1ULL) {
539 net_seq_idx = stream->net_seq_idx;
544 /* Inform sessiond that we are about to send channel and streams. */
545 ret = consumer_send_status_msg(sock, ret_code);
546 if (ret < 0 || ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
548 * Either the session daemon is not responding or the relayd died so we
554 /* Send channel to sessiond. */
555 ret = lttng_ust_ctl_send_channel_to_sessiond(sock, channel->uchan);
560 ret = lttng_ust_ctl_channel_close_wakeup_fd(channel->uchan);
565 /* The channel was sent successfully to the sessiond at this point. */
566 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
568 health_code_update();
570 /* Send stream to session daemon. */
571 ret = send_sessiond_stream(sock, stream);
577 /* Tell sessiond there is no more stream. */
578 ret = lttng_ust_ctl_send_stream_to_sessiond(sock, NULL);
583 DBG("UST consumer NULL stream sent to sessiond");
588 if (ret_code != LTTCOMM_CONSUMERD_SUCCESS) {
595 * Creates a channel and streams and add the channel it to the channel internal
596 * state. The created stream must ONLY be sent once the GET_CHANNEL command is
599 * Return 0 on success or else, a negative value is returned and the channel
600 * MUST be destroyed by consumer_del_channel().
602 static int ask_channel(struct lttng_consumer_local_data *ctx,
603 struct lttng_consumer_channel *channel,
604 struct lttng_ust_ctl_consumer_channel_attr *attr)
613 * This value is still used by the kernel consumer since for the kernel,
614 * the stream ownership is not IN the consumer so we need to have the
615 * number of left stream that needs to be initialized so we can know when
616 * to delete the channel (see consumer.c).
618 * As for the user space tracer now, the consumer creates and sends the
619 * stream to the session daemon which only sends them to the application
620 * once every stream of a channel is received making this value useless
621 * because we they will be added to the poll thread before the application
622 * receives them. This ensures that a stream can not hang up during
623 * initilization of a channel.
625 channel->nb_init_stream_left = 0;
627 /* The reply msg status is handled in the following call. */
628 ret = create_ust_channel(channel, attr, &channel->uchan);
633 channel->wait_fd = lttng_ust_ctl_channel_get_wait_fd(channel->uchan);
636 * For the snapshots (no monitor), we create the metadata streams
637 * on demand, not during the channel creation.
639 if (channel->type == CONSUMER_CHANNEL_TYPE_METADATA && !channel->monitor) {
644 /* Open all streams for this channel. */
645 pthread_mutex_lock(&channel->lock);
646 ret = create_ust_streams(channel, ctx);
647 pthread_mutex_unlock(&channel->lock);
657 * Send all stream of a channel to the right thread handling it.
659 * On error, return a negative value else 0 on success.
661 static int send_streams_to_thread(struct lttng_consumer_channel *channel,
662 struct lttng_consumer_local_data *ctx)
665 struct lttng_consumer_stream *stream, *stmp;
670 /* Send streams to the corresponding thread. */
671 cds_list_for_each_entry_safe(stream, stmp, &channel->streams.head,
674 health_code_update();
676 /* Sending the stream to the thread. */
677 ret = send_stream_to_thread(stream, ctx);
680 * If we are unable to send the stream to the thread, there is
681 * a big problem so just stop everything.
692 * Flush channel's streams using the given key to retrieve the channel.
694 * Return 0 on success else an LTTng error code.
696 static int flush_channel(uint64_t chan_key)
699 struct lttng_consumer_channel *channel;
700 struct lttng_consumer_stream *stream;
702 struct lttng_ht_iter iter;
704 DBG("UST consumer flush channel key %" PRIu64, chan_key);
707 channel = consumer_find_channel(chan_key);
709 ERR("UST consumer flush channel %" PRIu64 " not found", chan_key);
710 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
714 ht = the_consumer_data.stream_per_chan_id_ht;
716 /* For each stream of the channel id, flush it. */
717 cds_lfht_for_each_entry_duplicate(ht->ht,
718 ht->hash_fct(&channel->key, lttng_ht_seed), ht->match_fct,
719 &channel->key, &iter.iter, stream, node_channel_id.node) {
721 health_code_update();
723 pthread_mutex_lock(&stream->lock);
726 * Protect against concurrent teardown of a stream.
728 if (cds_lfht_is_node_deleted(&stream->node.node)) {
732 if (!stream->quiescent) {
733 ret = lttng_ust_ctl_flush_buffer(stream->ustream, 0);
735 ERR("Failed to flush buffer while flushing channel: channel key = %" PRIu64 ", channel name = '%s'",
736 chan_key, channel->name);
737 ret = LTTNG_ERR_BUFFER_FLUSH_FAILED;
738 pthread_mutex_unlock(&stream->lock);
741 stream->quiescent = true;
744 pthread_mutex_unlock(&stream->lock);
752 * Clear quiescent state from channel's streams using the given key to
753 * retrieve the channel.
755 * Return 0 on success else an LTTng error code.
757 static int clear_quiescent_channel(uint64_t chan_key)
760 struct lttng_consumer_channel *channel;
761 struct lttng_consumer_stream *stream;
763 struct lttng_ht_iter iter;
765 DBG("UST consumer clear quiescent channel key %" PRIu64, chan_key);
768 channel = consumer_find_channel(chan_key);
770 ERR("UST consumer clear quiescent channel %" PRIu64 " not found", chan_key);
771 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
775 ht = the_consumer_data.stream_per_chan_id_ht;
777 /* For each stream of the channel id, clear quiescent state. */
778 cds_lfht_for_each_entry_duplicate(ht->ht,
779 ht->hash_fct(&channel->key, lttng_ht_seed), ht->match_fct,
780 &channel->key, &iter.iter, stream, node_channel_id.node) {
782 health_code_update();
784 pthread_mutex_lock(&stream->lock);
785 stream->quiescent = false;
786 pthread_mutex_unlock(&stream->lock);
794 * Close metadata stream wakeup_fd using the given key to retrieve the channel.
796 * Return 0 on success else an LTTng error code.
798 static int close_metadata(uint64_t chan_key)
801 struct lttng_consumer_channel *channel;
802 unsigned int channel_monitor;
804 DBG("UST consumer close metadata key %" PRIu64, chan_key);
806 channel = consumer_find_channel(chan_key);
809 * This is possible if the metadata thread has issue a delete because
810 * the endpoint point of the stream hung up. There is no way the
811 * session daemon can know about it thus use a DBG instead of an actual
814 DBG("UST consumer close metadata %" PRIu64 " not found", chan_key);
815 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
819 pthread_mutex_lock(&the_consumer_data.lock);
820 pthread_mutex_lock(&channel->lock);
821 channel_monitor = channel->monitor;
822 if (cds_lfht_is_node_deleted(&channel->node.node)) {
826 lttng_ustconsumer_close_metadata(channel);
827 pthread_mutex_unlock(&channel->lock);
828 pthread_mutex_unlock(&the_consumer_data.lock);
831 * The ownership of a metadata channel depends on the type of
832 * session to which it belongs. In effect, the monitor flag is checked
833 * to determine if this metadata channel is in "snapshot" mode or not.
835 * In the non-snapshot case, the metadata channel is created along with
836 * a single stream which will remain present until the metadata channel
837 * is destroyed (on the destruction of its session). In this case, the
838 * metadata stream in "monitored" by the metadata poll thread and holds
839 * the ownership of its channel.
841 * Closing the metadata will cause the metadata stream's "metadata poll
842 * pipe" to be closed. Closing this pipe will wake-up the metadata poll
843 * thread which will teardown the metadata stream which, in return,
844 * deletes the metadata channel.
846 * In the snapshot case, the metadata stream is created and destroyed
847 * on every snapshot record. Since the channel doesn't have an owner
848 * other than the session daemon, it is safe to destroy it immediately
849 * on reception of the CLOSE_METADATA command.
851 if (!channel_monitor) {
853 * The channel and consumer_data locks must be
854 * released before this call since consumer_del_channel
855 * re-acquires the channel and consumer_data locks to teardown
856 * the channel and queue its reclamation by the "call_rcu"
859 consumer_del_channel(channel);
864 pthread_mutex_unlock(&channel->lock);
865 pthread_mutex_unlock(&the_consumer_data.lock);
871 * RCU read side lock MUST be acquired before calling this function.
873 * Return 0 on success else an LTTng error code.
875 static int setup_metadata(struct lttng_consumer_local_data *ctx, uint64_t key)
878 struct lttng_consumer_channel *metadata;
880 DBG("UST consumer setup metadata key %" PRIu64, key);
882 metadata = consumer_find_channel(key);
884 ERR("UST consumer push metadata %" PRIu64 " not found", key);
885 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
890 * In no monitor mode, the metadata channel has no stream(s) so skip the
891 * ownership transfer to the metadata thread.
893 if (!metadata->monitor) {
894 DBG("Metadata channel in no monitor");
900 * Send metadata stream to relayd if one available. Availability is
901 * known if the stream is still in the list of the channel.
903 if (cds_list_empty(&metadata->streams.head)) {
904 ERR("Metadata channel key %" PRIu64 ", no stream available.", key);
905 ret = LTTCOMM_CONSUMERD_ERROR_METADATA;
906 goto error_no_stream;
909 /* Send metadata stream to relayd if needed. */
910 if (metadata->metadata_stream->net_seq_idx != (uint64_t) -1ULL) {
911 ret = consumer_send_relayd_stream(metadata->metadata_stream,
914 ret = LTTCOMM_CONSUMERD_ERROR_METADATA;
917 ret = consumer_send_relayd_streams_sent(
918 metadata->metadata_stream->net_seq_idx);
920 ret = LTTCOMM_CONSUMERD_RELAYD_FAIL;
926 * Ownership of metadata stream is passed along. Freeing is handled by
929 ret = send_streams_to_thread(metadata, ctx);
932 * If we are unable to send the stream to the thread, there is
933 * a big problem so just stop everything.
935 ret = LTTCOMM_CONSUMERD_FATAL;
936 goto send_streams_error;
938 /* List MUST be empty after or else it could be reused. */
939 assert(cds_list_empty(&metadata->streams.head));
946 * Delete metadata channel on error. At this point, the metadata stream can
947 * NOT be monitored by the metadata thread thus having the guarantee that
948 * the stream is still in the local stream list of the channel. This call
949 * will make sure to clean that list.
951 consumer_stream_destroy(metadata->metadata_stream, NULL);
952 cds_list_del(&metadata->metadata_stream->send_node);
953 metadata->metadata_stream = NULL;
961 * Snapshot the whole metadata.
962 * RCU read-side lock must be held by the caller.
964 * Returns 0 on success, < 0 on error
966 static int snapshot_metadata(struct lttng_consumer_channel *metadata_channel,
967 uint64_t key, char *path, uint64_t relayd_id,
968 struct lttng_consumer_local_data *ctx)
971 struct lttng_consumer_stream *metadata_stream;
976 DBG("UST consumer snapshot metadata with key %" PRIu64 " at path %s",
981 assert(!metadata_channel->monitor);
983 health_code_update();
986 * Ask the sessiond if we have new metadata waiting and update the
987 * consumer metadata cache.
989 ret = lttng_ustconsumer_request_metadata(ctx, metadata_channel, 0, 1);
994 health_code_update();
997 * The metadata stream is NOT created in no monitor mode when the channel
998 * is created on a sessiond ask channel command.
1000 ret = create_ust_streams(metadata_channel, ctx);
1005 metadata_stream = metadata_channel->metadata_stream;
1006 assert(metadata_stream);
1008 metadata_stream->read_subbuffer_ops.lock(metadata_stream);
1009 if (relayd_id != (uint64_t) -1ULL) {
1010 metadata_stream->net_seq_idx = relayd_id;
1011 ret = consumer_send_relayd_stream(metadata_stream, path);
1013 ret = consumer_stream_create_output_files(metadata_stream,
1021 health_code_update();
1022 ret = lttng_consumer_read_subbuffer(metadata_stream, ctx, true);
1029 metadata_stream->read_subbuffer_ops.unlock(metadata_stream);
1031 * Clean up the stream completely because the next snapshot will use a
1032 * new metadata stream.
1034 consumer_stream_destroy(metadata_stream, NULL);
1035 cds_list_del(&metadata_stream->send_node);
1036 metadata_channel->metadata_stream = NULL;
1044 int get_current_subbuf_addr(struct lttng_consumer_stream *stream,
1048 unsigned long mmap_offset;
1049 const char *mmap_base;
1051 mmap_base = lttng_ust_ctl_get_mmap_base(stream->ustream);
1053 ERR("Failed to get mmap base for stream `%s`",
1059 ret = lttng_ust_ctl_get_mmap_read_offset(stream->ustream, &mmap_offset);
1061 ERR("Failed to get mmap offset for stream `%s`", stream->name);
1066 *addr = mmap_base + mmap_offset;
1073 * Take a snapshot of all the stream of a channel.
1074 * RCU read-side lock and the channel lock must be held by the caller.
1076 * Returns 0 on success, < 0 on error
1078 static int snapshot_channel(struct lttng_consumer_channel *channel,
1079 uint64_t key, char *path, uint64_t relayd_id,
1080 uint64_t nb_packets_per_stream,
1081 struct lttng_consumer_local_data *ctx)
1084 unsigned use_relayd = 0;
1085 unsigned long consumed_pos, produced_pos;
1086 struct lttng_consumer_stream *stream;
1093 if (relayd_id != (uint64_t) -1ULL) {
1097 assert(!channel->monitor);
1098 DBG("UST consumer snapshot channel %" PRIu64, key);
1100 cds_list_for_each_entry(stream, &channel->streams.head, send_node) {
1101 health_code_update();
1103 /* Lock stream because we are about to change its state. */
1104 pthread_mutex_lock(&stream->lock);
1105 assert(channel->trace_chunk);
1106 if (!lttng_trace_chunk_get(channel->trace_chunk)) {
1108 * Can't happen barring an internal error as the channel
1109 * holds a reference to the trace chunk.
1111 ERR("Failed to acquire reference to channel's trace chunk");
1115 assert(!stream->trace_chunk);
1116 stream->trace_chunk = channel->trace_chunk;
1118 stream->net_seq_idx = relayd_id;
1121 ret = consumer_send_relayd_stream(stream, path);
1126 ret = consumer_stream_create_output_files(stream,
1131 DBG("UST consumer snapshot stream (%" PRIu64 ")",
1136 * If tracing is active, we want to perform a "full" buffer flush.
1137 * Else, if quiescent, it has already been done by the prior stop.
1139 if (!stream->quiescent) {
1140 ret = lttng_ust_ctl_flush_buffer(stream->ustream, 0);
1142 ERR("Failed to flush buffer during snapshot of channel: channel key = %" PRIu64 ", channel name = '%s'",
1143 channel->key, channel->name);
1148 ret = lttng_ustconsumer_take_snapshot(stream);
1150 ERR("Taking UST snapshot");
1154 ret = lttng_ustconsumer_get_produced_snapshot(stream, &produced_pos);
1156 ERR("Produced UST snapshot position");
1160 ret = lttng_ustconsumer_get_consumed_snapshot(stream, &consumed_pos);
1162 ERR("Consumerd UST snapshot position");
1167 * The original value is sent back if max stream size is larger than
1168 * the possible size of the snapshot. Also, we assume that the session
1169 * daemon should never send a maximum stream size that is lower than
1172 consumed_pos = consumer_get_consume_start_pos(consumed_pos,
1173 produced_pos, nb_packets_per_stream,
1174 stream->max_sb_size);
1176 while ((long) (consumed_pos - produced_pos) < 0) {
1178 unsigned long len, padded_len;
1179 const char *subbuf_addr;
1180 struct lttng_buffer_view subbuf_view;
1182 health_code_update();
1184 DBG("UST consumer taking snapshot at pos %lu", consumed_pos);
1186 ret = lttng_ust_ctl_get_subbuf(stream->ustream, &consumed_pos);
1188 if (ret != -EAGAIN) {
1189 PERROR("lttng_ust_ctl_get_subbuf snapshot");
1190 goto error_close_stream;
1192 DBG("UST consumer get subbuf failed. Skipping it.");
1193 consumed_pos += stream->max_sb_size;
1194 stream->chan->lost_packets++;
1198 ret = lttng_ust_ctl_get_subbuf_size(stream->ustream, &len);
1200 ERR("Snapshot lttng_ust_ctl_get_subbuf_size");
1201 goto error_put_subbuf;
1204 ret = lttng_ust_ctl_get_padded_subbuf_size(stream->ustream, &padded_len);
1206 ERR("Snapshot lttng_ust_ctl_get_padded_subbuf_size");
1207 goto error_put_subbuf;
1210 ret = get_current_subbuf_addr(stream, &subbuf_addr);
1212 goto error_put_subbuf;
1215 subbuf_view = lttng_buffer_view_init(
1216 subbuf_addr, 0, padded_len);
1217 read_len = lttng_consumer_on_read_subbuffer_mmap(
1218 stream, &subbuf_view, padded_len - len);
1220 if (read_len != len) {
1222 goto error_put_subbuf;
1225 if (read_len != padded_len) {
1227 goto error_put_subbuf;
1231 ret = lttng_ust_ctl_put_subbuf(stream->ustream);
1233 ERR("Snapshot lttng_ust_ctl_put_subbuf");
1234 goto error_close_stream;
1236 consumed_pos += stream->max_sb_size;
1239 /* Simply close the stream so we can use it on the next snapshot. */
1240 consumer_stream_close(stream);
1241 pthread_mutex_unlock(&stream->lock);
1248 if (lttng_ust_ctl_put_subbuf(stream->ustream) < 0) {
1249 ERR("Snapshot lttng_ust_ctl_put_subbuf");
1252 consumer_stream_close(stream);
1254 pthread_mutex_unlock(&stream->lock);
1260 void metadata_stream_reset_cache_consumed_position(
1261 struct lttng_consumer_stream *stream)
1263 ASSERT_LOCKED(stream->lock);
1265 DBG("Reset metadata cache of session %" PRIu64,
1266 stream->chan->session_id);
1267 stream->ust_metadata_pushed = 0;
1271 * Receive the metadata updates from the sessiond. Supports receiving
1272 * overlapping metadata, but is needs to always belong to a contiguous
1273 * range starting from 0.
1274 * Be careful about the locks held when calling this function: it needs
1275 * the metadata cache flush to concurrently progress in order to
1278 int lttng_ustconsumer_recv_metadata(int sock, uint64_t key, uint64_t offset,
1279 uint64_t len, uint64_t version,
1280 struct lttng_consumer_channel *channel, int timer, int wait)
1282 int ret, ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1284 enum consumer_metadata_cache_write_status cache_write_status;
1286 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key, len);
1288 metadata_str = zmalloc(len * sizeof(char));
1289 if (!metadata_str) {
1290 PERROR("zmalloc metadata string");
1291 ret_code = LTTCOMM_CONSUMERD_ENOMEM;
1295 health_code_update();
1297 /* Receive metadata string. */
1298 ret = lttcomm_recv_unix_sock(sock, metadata_str, len);
1300 /* Session daemon is dead so return gracefully. */
1305 health_code_update();
1307 pthread_mutex_lock(&channel->metadata_cache->lock);
1308 cache_write_status = consumer_metadata_cache_write(
1309 channel->metadata_cache, offset, len, version,
1311 pthread_mutex_unlock(&channel->metadata_cache->lock);
1312 switch (cache_write_status) {
1313 case CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE:
1315 * The write entirely overlapped with existing contents of the
1316 * same metadata version (same content); there is nothing to do.
1319 case CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED:
1321 * The metadata cache was invalidated (previously pushed
1322 * content has been overwritten). Reset the stream's consumed
1323 * metadata position to ensure the metadata poll thread consumes
1328 * channel::metadata_stream can be null when the metadata
1329 * channel is under a snapshot session type. No need to update
1330 * the stream position in that scenario.
1332 if (channel->metadata_stream != NULL) {
1333 pthread_mutex_lock(&channel->metadata_stream->lock);
1334 metadata_stream_reset_cache_consumed_position(
1335 channel->metadata_stream);
1336 pthread_mutex_unlock(&channel->metadata_stream->lock);
1338 /* Validate we are in snapshot mode. */
1339 assert(!channel->monitor);
1342 case CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT:
1344 * In both cases, the metadata poll thread has new data to
1347 ret = consumer_metadata_wakeup_pipe(channel);
1349 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
1353 case CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR:
1354 /* Unable to handle metadata. Notify session daemon. */
1355 ret_code = LTTCOMM_CONSUMERD_ERROR_METADATA;
1357 * Skip metadata flush on write error since the offset and len might
1358 * not have been updated which could create an infinite loop below when
1359 * waiting for the metadata cache to be flushed.
1369 while (consumer_metadata_cache_flushed(channel, offset + len, timer)) {
1370 DBG("Waiting for metadata to be flushed");
1372 health_code_update();
1374 usleep(DEFAULT_METADATA_AVAILABILITY_WAIT_TIME);
1384 * Receive command from session daemon and process it.
1386 * Return 1 on success else a negative value or 0.
1388 int lttng_ustconsumer_recv_cmd(struct lttng_consumer_local_data *ctx,
1389 int sock, struct pollfd *consumer_sockpoll)
1392 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1393 struct lttcomm_consumer_msg msg;
1394 struct lttng_consumer_channel *channel = NULL;
1396 health_code_update();
1401 ret_recv = lttcomm_recv_unix_sock(sock, &msg, sizeof(msg));
1402 if (ret_recv != sizeof(msg)) {
1403 DBG("Consumer received unexpected message size %zd (expects %zu)",
1404 ret_recv, sizeof(msg));
1406 * The ret value might 0 meaning an orderly shutdown but this is ok
1407 * since the caller handles this.
1410 lttng_consumer_send_error(ctx,
1411 LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
1418 health_code_update();
1421 assert(msg.cmd_type != LTTNG_CONSUMER_STOP);
1423 health_code_update();
1425 /* relayd needs RCU read-side lock */
1428 switch (msg.cmd_type) {
1429 case LTTNG_CONSUMER_ADD_RELAYD_SOCKET:
1431 /* Session daemon status message are handled in the following call. */
1432 consumer_add_relayd_socket(msg.u.relayd_sock.net_index,
1433 msg.u.relayd_sock.type, ctx, sock, consumer_sockpoll,
1434 &msg.u.relayd_sock.sock, msg.u.relayd_sock.session_id,
1435 msg.u.relayd_sock.relayd_session_id);
1438 case LTTNG_CONSUMER_DESTROY_RELAYD:
1440 uint64_t index = msg.u.destroy_relayd.net_seq_idx;
1441 struct consumer_relayd_sock_pair *relayd;
1443 DBG("UST consumer destroying relayd %" PRIu64, index);
1445 /* Get relayd reference if exists. */
1446 relayd = consumer_find_relayd(index);
1447 if (relayd == NULL) {
1448 DBG("Unable to find relayd %" PRIu64, index);
1449 ret_code = LTTCOMM_CONSUMERD_RELAYD_FAIL;
1453 * Each relayd socket pair has a refcount of stream attached to it
1454 * which tells if the relayd is still active or not depending on the
1457 * This will set the destroy flag of the relayd object and destroy it
1458 * if the refcount reaches zero when called.
1460 * The destroy can happen either here or when a stream fd hangs up.
1463 consumer_flag_relayd_for_destroy(relayd);
1466 goto end_msg_sessiond;
1468 case LTTNG_CONSUMER_UPDATE_STREAM:
1473 case LTTNG_CONSUMER_DATA_PENDING:
1475 int is_data_pending;
1477 uint64_t id = msg.u.data_pending.session_id;
1479 DBG("UST consumer data pending command for id %" PRIu64, id);
1481 is_data_pending = consumer_data_pending(id);
1483 /* Send back returned value to session daemon */
1484 ret_send = lttcomm_send_unix_sock(sock, &is_data_pending,
1485 sizeof(is_data_pending));
1487 DBG("Error when sending the data pending ret code: %zd",
1493 * No need to send back a status message since the data pending
1494 * returned value is the response.
1498 case LTTNG_CONSUMER_ASK_CHANNEL_CREATION:
1500 int ret_ask_channel, ret_add_channel, ret_send;
1501 struct lttng_ust_ctl_consumer_channel_attr attr;
1502 const uint64_t chunk_id = msg.u.ask_channel.chunk_id.value;
1503 const struct lttng_credentials buffer_credentials = {
1504 .uid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.ask_channel.buffer_credentials.uid),
1505 .gid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.ask_channel.buffer_credentials.gid),
1508 /* Create a plain object and reserve a channel key. */
1509 channel = consumer_allocate_channel(
1510 msg.u.ask_channel.key,
1511 msg.u.ask_channel.session_id,
1512 msg.u.ask_channel.chunk_id.is_set ?
1514 msg.u.ask_channel.pathname,
1515 msg.u.ask_channel.name,
1516 msg.u.ask_channel.relayd_id,
1517 (enum lttng_event_output) msg.u.ask_channel.output,
1518 msg.u.ask_channel.tracefile_size,
1519 msg.u.ask_channel.tracefile_count,
1520 msg.u.ask_channel.session_id_per_pid,
1521 msg.u.ask_channel.monitor,
1522 msg.u.ask_channel.live_timer_interval,
1523 msg.u.ask_channel.is_live,
1524 msg.u.ask_channel.root_shm_path,
1525 msg.u.ask_channel.shm_path);
1527 goto end_channel_error;
1530 LTTNG_OPTIONAL_SET(&channel->buffer_credentials,
1531 buffer_credentials);
1534 * Assign UST application UID to the channel. This value is ignored for
1535 * per PID buffers. This is specific to UST thus setting this after the
1538 channel->ust_app_uid = msg.u.ask_channel.ust_app_uid;
1540 /* Build channel attributes from received message. */
1541 attr.subbuf_size = msg.u.ask_channel.subbuf_size;
1542 attr.num_subbuf = msg.u.ask_channel.num_subbuf;
1543 attr.overwrite = msg.u.ask_channel.overwrite;
1544 attr.switch_timer_interval = msg.u.ask_channel.switch_timer_interval;
1545 attr.read_timer_interval = msg.u.ask_channel.read_timer_interval;
1546 attr.chan_id = msg.u.ask_channel.chan_id;
1547 memcpy(attr.uuid, msg.u.ask_channel.uuid, sizeof(attr.uuid));
1548 attr.blocking_timeout= msg.u.ask_channel.blocking_timeout;
1550 /* Match channel buffer type to the UST abi. */
1551 switch (msg.u.ask_channel.output) {
1552 case LTTNG_EVENT_MMAP:
1554 attr.output = LTTNG_UST_ABI_MMAP;
1558 /* Translate and save channel type. */
1559 switch (msg.u.ask_channel.type) {
1560 case LTTNG_UST_ABI_CHAN_PER_CPU:
1561 channel->type = CONSUMER_CHANNEL_TYPE_DATA;
1562 attr.type = LTTNG_UST_ABI_CHAN_PER_CPU;
1564 * Set refcount to 1 for owner. Below, we will
1565 * pass ownership to the
1566 * consumer_thread_channel_poll() thread.
1568 channel->refcount = 1;
1570 case LTTNG_UST_ABI_CHAN_METADATA:
1571 channel->type = CONSUMER_CHANNEL_TYPE_METADATA;
1572 attr.type = LTTNG_UST_ABI_CHAN_METADATA;
1579 health_code_update();
1581 ret_ask_channel = ask_channel(ctx, channel, &attr);
1582 if (ret_ask_channel < 0) {
1583 goto end_channel_error;
1586 if (msg.u.ask_channel.type == LTTNG_UST_ABI_CHAN_METADATA) {
1589 ret_allocate = consumer_metadata_cache_allocate(
1591 if (ret_allocate < 0) {
1592 ERR("Allocating metadata cache");
1593 goto end_channel_error;
1595 consumer_timer_switch_start(channel, attr.switch_timer_interval);
1596 attr.switch_timer_interval = 0;
1598 int monitor_start_ret;
1600 consumer_timer_live_start(channel,
1601 msg.u.ask_channel.live_timer_interval);
1602 monitor_start_ret = consumer_timer_monitor_start(
1604 msg.u.ask_channel.monitor_timer_interval);
1605 if (monitor_start_ret < 0) {
1606 ERR("Starting channel monitoring timer failed");
1607 goto end_channel_error;
1611 health_code_update();
1614 * Add the channel to the internal state AFTER all streams were created
1615 * and successfully sent to session daemon. This way, all streams must
1616 * be ready before this channel is visible to the threads.
1617 * If add_channel succeeds, ownership of the channel is
1618 * passed to consumer_thread_channel_poll().
1620 ret_add_channel = add_channel(channel, ctx);
1621 if (ret_add_channel < 0) {
1622 if (msg.u.ask_channel.type == LTTNG_UST_ABI_CHAN_METADATA) {
1623 if (channel->switch_timer_enabled == 1) {
1624 consumer_timer_switch_stop(channel);
1626 consumer_metadata_cache_destroy(channel);
1628 if (channel->live_timer_enabled == 1) {
1629 consumer_timer_live_stop(channel);
1631 if (channel->monitor_timer_enabled == 1) {
1632 consumer_timer_monitor_stop(channel);
1634 goto end_channel_error;
1637 health_code_update();
1640 * Channel and streams are now created. Inform the session daemon that
1641 * everything went well and should wait to receive the channel and
1642 * streams with ustctl API.
1644 ret_send = consumer_send_status_channel(sock, channel);
1647 * There is probably a problem on the socket.
1654 case LTTNG_CONSUMER_GET_CHANNEL:
1656 int ret, relayd_err = 0;
1657 uint64_t key = msg.u.get_channel.key;
1658 struct lttng_consumer_channel *found_channel;
1660 found_channel = consumer_find_channel(key);
1661 if (!found_channel) {
1662 ERR("UST consumer get channel key %" PRIu64 " not found", key);
1663 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1664 goto end_get_channel;
1667 health_code_update();
1669 /* Send the channel to sessiond (and relayd, if applicable). */
1670 ret = send_channel_to_sessiond_and_relayd(
1671 sock, found_channel, ctx, &relayd_err);
1675 * We were unable to send to the relayd the stream so avoid
1676 * sending back a fatal error to the thread since this is OK
1677 * and the consumer can continue its work. The above call
1678 * has sent the error status message to the sessiond.
1680 goto end_get_channel_nosignal;
1683 * The communicaton was broken hence there is a bad state between
1684 * the consumer and sessiond so stop everything.
1686 goto error_get_channel_fatal;
1689 health_code_update();
1692 * In no monitor mode, the streams ownership is kept inside the channel
1693 * so don't send them to the data thread.
1695 if (!found_channel->monitor) {
1696 goto end_get_channel;
1699 ret = send_streams_to_thread(found_channel, ctx);
1702 * If we are unable to send the stream to the thread, there is
1703 * a big problem so just stop everything.
1705 goto error_get_channel_fatal;
1707 /* List MUST be empty after or else it could be reused. */
1708 assert(cds_list_empty(&found_channel->streams.head));
1710 goto end_msg_sessiond;
1711 error_get_channel_fatal:
1713 end_get_channel_nosignal:
1716 case LTTNG_CONSUMER_DESTROY_CHANNEL:
1718 uint64_t key = msg.u.destroy_channel.key;
1721 * Only called if streams have not been sent to stream
1722 * manager thread. However, channel has been sent to
1723 * channel manager thread.
1725 notify_thread_del_channel(ctx, key);
1726 goto end_msg_sessiond;
1728 case LTTNG_CONSUMER_CLOSE_METADATA:
1732 ret = close_metadata(msg.u.close_metadata.key);
1737 goto end_msg_sessiond;
1739 case LTTNG_CONSUMER_FLUSH_CHANNEL:
1743 ret = flush_channel(msg.u.flush_channel.key);
1748 goto end_msg_sessiond;
1750 case LTTNG_CONSUMER_CLEAR_QUIESCENT_CHANNEL:
1754 ret = clear_quiescent_channel(
1755 msg.u.clear_quiescent_channel.key);
1760 goto end_msg_sessiond;
1762 case LTTNG_CONSUMER_PUSH_METADATA:
1765 uint64_t len = msg.u.push_metadata.len;
1766 uint64_t key = msg.u.push_metadata.key;
1767 uint64_t offset = msg.u.push_metadata.target_offset;
1768 uint64_t version = msg.u.push_metadata.version;
1769 struct lttng_consumer_channel *found_channel;
1771 DBG("UST consumer push metadata key %" PRIu64 " of len %" PRIu64, key,
1774 found_channel = consumer_find_channel(key);
1775 if (!found_channel) {
1777 * This is possible if the metadata creation on the consumer side
1778 * is in flight vis-a-vis a concurrent push metadata from the
1779 * session daemon. Simply return that the channel failed and the
1780 * session daemon will handle that message correctly considering
1781 * that this race is acceptable thus the DBG() statement here.
1783 DBG("UST consumer push metadata %" PRIu64 " not found", key);
1784 ret_code = LTTCOMM_CONSUMERD_CHANNEL_FAIL;
1785 goto end_push_metadata_msg_sessiond;
1788 health_code_update();
1792 * There is nothing to receive. We have simply
1793 * checked whether the channel can be found.
1795 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1796 goto end_push_metadata_msg_sessiond;
1799 /* Tell session daemon we are ready to receive the metadata. */
1800 ret = consumer_send_status_msg(sock, LTTCOMM_CONSUMERD_SUCCESS);
1802 /* Somehow, the session daemon is not responding anymore. */
1803 goto error_push_metadata_fatal;
1806 health_code_update();
1808 /* Wait for more data. */
1809 health_poll_entry();
1810 ret = lttng_consumer_poll_socket(consumer_sockpoll);
1813 goto error_push_metadata_fatal;
1816 health_code_update();
1818 ret = lttng_ustconsumer_recv_metadata(sock, key, offset, len,
1819 version, found_channel, 0, 1);
1821 /* error receiving from sessiond */
1822 goto error_push_metadata_fatal;
1825 goto end_push_metadata_msg_sessiond;
1827 end_push_metadata_msg_sessiond:
1828 goto end_msg_sessiond;
1829 error_push_metadata_fatal:
1832 case LTTNG_CONSUMER_SETUP_METADATA:
1836 ret = setup_metadata(ctx, msg.u.setup_metadata.key);
1840 goto end_msg_sessiond;
1842 case LTTNG_CONSUMER_SNAPSHOT_CHANNEL:
1844 struct lttng_consumer_channel *found_channel;
1845 uint64_t key = msg.u.snapshot_channel.key;
1848 found_channel = consumer_find_channel(key);
1849 if (!found_channel) {
1850 DBG("UST snapshot channel not found for key %" PRIu64, key);
1851 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
1853 if (msg.u.snapshot_channel.metadata) {
1856 ret_snapshot = snapshot_metadata(found_channel,
1858 msg.u.snapshot_channel.pathname,
1859 msg.u.snapshot_channel.relayd_id,
1861 if (ret_snapshot < 0) {
1862 ERR("Snapshot metadata failed");
1863 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
1868 ret_snapshot = snapshot_channel(found_channel,
1870 msg.u.snapshot_channel.pathname,
1871 msg.u.snapshot_channel.relayd_id,
1872 msg.u.snapshot_channel
1873 .nb_packets_per_stream,
1875 if (ret_snapshot < 0) {
1876 ERR("Snapshot channel failed");
1877 ret_code = LTTCOMM_CONSUMERD_SNAPSHOT_FAILED;
1881 health_code_update();
1882 ret_send = consumer_send_status_msg(sock, ret_code);
1884 /* Somehow, the session daemon is not responding anymore. */
1887 health_code_update();
1890 case LTTNG_CONSUMER_DISCARDED_EVENTS:
1893 uint64_t discarded_events;
1894 struct lttng_ht_iter iter;
1895 struct lttng_ht *ht;
1896 struct lttng_consumer_stream *stream;
1897 uint64_t id = msg.u.discarded_events.session_id;
1898 uint64_t key = msg.u.discarded_events.channel_key;
1900 DBG("UST consumer discarded events command for session id %"
1903 pthread_mutex_lock(&the_consumer_data.lock);
1905 ht = the_consumer_data.stream_list_ht;
1908 * We only need a reference to the channel, but they are not
1909 * directly indexed, so we just use the first matching stream
1910 * to extract the information we need, we default to 0 if not
1911 * found (no events are dropped if the channel is not yet in
1914 discarded_events = 0;
1915 cds_lfht_for_each_entry_duplicate(ht->ht,
1916 ht->hash_fct(&id, lttng_ht_seed),
1918 &iter.iter, stream, node_session_id.node) {
1919 if (stream->chan->key == key) {
1920 discarded_events = stream->chan->discarded_events;
1924 pthread_mutex_unlock(&the_consumer_data.lock);
1927 DBG("UST consumer discarded events command for session id %"
1928 PRIu64 ", channel key %" PRIu64, id, key);
1930 health_code_update();
1932 /* Send back returned value to session daemon */
1933 ret = lttcomm_send_unix_sock(sock, &discarded_events, sizeof(discarded_events));
1935 PERROR("send discarded events");
1941 case LTTNG_CONSUMER_LOST_PACKETS:
1944 uint64_t lost_packets;
1945 struct lttng_ht_iter iter;
1946 struct lttng_ht *ht;
1947 struct lttng_consumer_stream *stream;
1948 uint64_t id = msg.u.lost_packets.session_id;
1949 uint64_t key = msg.u.lost_packets.channel_key;
1951 DBG("UST consumer lost packets command for session id %"
1954 pthread_mutex_lock(&the_consumer_data.lock);
1956 ht = the_consumer_data.stream_list_ht;
1959 * We only need a reference to the channel, but they are not
1960 * directly indexed, so we just use the first matching stream
1961 * to extract the information we need, we default to 0 if not
1962 * found (no packets lost if the channel is not yet in use).
1965 cds_lfht_for_each_entry_duplicate(ht->ht,
1966 ht->hash_fct(&id, lttng_ht_seed),
1968 &iter.iter, stream, node_session_id.node) {
1969 if (stream->chan->key == key) {
1970 lost_packets = stream->chan->lost_packets;
1974 pthread_mutex_unlock(&the_consumer_data.lock);
1977 DBG("UST consumer lost packets command for session id %"
1978 PRIu64 ", channel key %" PRIu64, id, key);
1980 health_code_update();
1982 /* Send back returned value to session daemon */
1983 ret = lttcomm_send_unix_sock(sock, &lost_packets,
1984 sizeof(lost_packets));
1986 PERROR("send lost packets");
1992 case LTTNG_CONSUMER_SET_CHANNEL_MONITOR_PIPE:
1994 int channel_monitor_pipe, ret_send,
1995 ret_set_channel_monitor_pipe;
1998 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
1999 /* Successfully received the command's type. */
2000 ret_send = consumer_send_status_msg(sock, ret_code);
2005 ret_recv = lttcomm_recv_fds_unix_sock(
2006 sock, &channel_monitor_pipe, 1);
2007 if (ret_recv != sizeof(channel_monitor_pipe)) {
2008 ERR("Failed to receive channel monitor pipe");
2012 DBG("Received channel monitor pipe (%d)", channel_monitor_pipe);
2013 ret_set_channel_monitor_pipe =
2014 consumer_timer_thread_set_channel_monitor_pipe(
2015 channel_monitor_pipe);
2016 if (!ret_set_channel_monitor_pipe) {
2020 ret_code = LTTCOMM_CONSUMERD_SUCCESS;
2021 /* Set the pipe as non-blocking. */
2022 ret_fcntl = fcntl(channel_monitor_pipe, F_GETFL, 0);
2023 if (ret_fcntl == -1) {
2024 PERROR("fcntl get flags of the channel monitoring pipe");
2029 ret_fcntl = fcntl(channel_monitor_pipe, F_SETFL,
2030 flags | O_NONBLOCK);
2031 if (ret_fcntl == -1) {
2032 PERROR("fcntl set O_NONBLOCK flag of the channel monitoring pipe");
2035 DBG("Channel monitor pipe set as non-blocking");
2037 ret_code = LTTCOMM_CONSUMERD_ALREADY_SET;
2039 goto end_msg_sessiond;
2041 case LTTNG_CONSUMER_ROTATE_CHANNEL:
2043 struct lttng_consumer_channel *found_channel;
2044 uint64_t key = msg.u.rotate_channel.key;
2045 int ret_send_status;
2047 found_channel = consumer_find_channel(key);
2048 if (!found_channel) {
2049 DBG("Channel %" PRIu64 " not found", key);
2050 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2055 * Sample the rotate position of all the streams in
2058 rotate_channel = lttng_consumer_rotate_channel(
2060 msg.u.rotate_channel.relayd_id,
2061 msg.u.rotate_channel.metadata, ctx);
2062 if (rotate_channel < 0) {
2063 ERR("Rotate channel failed");
2064 ret_code = LTTCOMM_CONSUMERD_ROTATION_FAIL;
2067 health_code_update();
2070 ret_send_status = consumer_send_status_msg(sock, ret_code);
2071 if (ret_send_status < 0) {
2072 /* Somehow, the session daemon is not responding anymore. */
2073 goto end_rotate_channel_nosignal;
2077 * Rotate the streams that are ready right now.
2078 * FIXME: this is a second consecutive iteration over the
2079 * streams in a channel, there is probably a better way to
2080 * handle this, but it needs to be after the
2081 * consumer_send_status_msg() call.
2083 if (found_channel) {
2084 int ret_rotate_read_streams;
2086 ret_rotate_read_streams =
2087 lttng_consumer_rotate_ready_streams(
2090 if (ret_rotate_read_streams < 0) {
2091 ERR("Rotate channel failed");
2095 end_rotate_channel_nosignal:
2098 case LTTNG_CONSUMER_CLEAR_CHANNEL:
2100 struct lttng_consumer_channel *found_channel;
2101 uint64_t key = msg.u.clear_channel.key;
2102 int ret_send_status;
2104 found_channel = consumer_find_channel(key);
2105 if (!found_channel) {
2106 DBG("Channel %" PRIu64 " not found", key);
2107 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2109 int ret_clear_channel;
2111 ret_clear_channel = lttng_consumer_clear_channel(
2113 if (ret_clear_channel) {
2114 ERR("Clear channel failed key %" PRIu64, key);
2115 ret_code = ret_clear_channel;
2118 health_code_update();
2120 ret_send_status = consumer_send_status_msg(sock, ret_code);
2121 if (ret_send_status < 0) {
2122 /* Somehow, the session daemon is not responding anymore. */
2127 case LTTNG_CONSUMER_INIT:
2129 int ret_send_status;
2131 ret_code = lttng_consumer_init_command(ctx,
2132 msg.u.init.sessiond_uuid);
2133 health_code_update();
2134 ret_send_status = consumer_send_status_msg(sock, ret_code);
2135 if (ret_send_status < 0) {
2136 /* Somehow, the session daemon is not responding anymore. */
2141 case LTTNG_CONSUMER_CREATE_TRACE_CHUNK:
2143 const struct lttng_credentials credentials = {
2144 .uid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.create_trace_chunk.credentials.value.uid),
2145 .gid = LTTNG_OPTIONAL_INIT_VALUE(msg.u.create_trace_chunk.credentials.value.gid),
2147 const bool is_local_trace =
2148 !msg.u.create_trace_chunk.relayd_id.is_set;
2149 const uint64_t relayd_id =
2150 msg.u.create_trace_chunk.relayd_id.value;
2151 const char *chunk_override_name =
2152 *msg.u.create_trace_chunk.override_name ?
2153 msg.u.create_trace_chunk.override_name :
2155 struct lttng_directory_handle *chunk_directory_handle = NULL;
2158 * The session daemon will only provide a chunk directory file
2159 * descriptor for local traces.
2161 if (is_local_trace) {
2163 int ret_send_status;
2166 /* Acnowledge the reception of the command. */
2167 ret_send_status = consumer_send_status_msg(
2168 sock, LTTCOMM_CONSUMERD_SUCCESS);
2169 if (ret_send_status < 0) {
2170 /* Somehow, the session daemon is not responding anymore. */
2175 * Receive trace chunk domain dirfd.
2177 ret_recv = lttcomm_recv_fds_unix_sock(
2178 sock, &chunk_dirfd, 1);
2179 if (ret_recv != sizeof(chunk_dirfd)) {
2180 ERR("Failed to receive trace chunk domain directory file descriptor");
2184 DBG("Received trace chunk domain directory fd (%d)",
2186 chunk_directory_handle = lttng_directory_handle_create_from_dirfd(
2188 if (!chunk_directory_handle) {
2189 ERR("Failed to initialize chunk domain directory handle from directory file descriptor");
2190 if (close(chunk_dirfd)) {
2191 PERROR("Failed to close chunk directory file descriptor");
2197 ret_code = lttng_consumer_create_trace_chunk(
2198 !is_local_trace ? &relayd_id : NULL,
2199 msg.u.create_trace_chunk.session_id,
2200 msg.u.create_trace_chunk.chunk_id,
2201 (time_t) msg.u.create_trace_chunk
2202 .creation_timestamp,
2203 chunk_override_name,
2204 msg.u.create_trace_chunk.credentials.is_set ?
2207 chunk_directory_handle);
2208 lttng_directory_handle_put(chunk_directory_handle);
2209 goto end_msg_sessiond;
2211 case LTTNG_CONSUMER_CLOSE_TRACE_CHUNK:
2213 enum lttng_trace_chunk_command_type close_command =
2214 msg.u.close_trace_chunk.close_command.value;
2215 const uint64_t relayd_id =
2216 msg.u.close_trace_chunk.relayd_id.value;
2217 struct lttcomm_consumer_close_trace_chunk_reply reply;
2218 char closed_trace_chunk_path[LTTNG_PATH_MAX] = {};
2221 ret_code = lttng_consumer_close_trace_chunk(
2222 msg.u.close_trace_chunk.relayd_id.is_set ?
2225 msg.u.close_trace_chunk.session_id,
2226 msg.u.close_trace_chunk.chunk_id,
2227 (time_t) msg.u.close_trace_chunk.close_timestamp,
2228 msg.u.close_trace_chunk.close_command.is_set ?
2230 NULL, closed_trace_chunk_path);
2231 reply.ret_code = ret_code;
2232 reply.path_length = strlen(closed_trace_chunk_path) + 1;
2233 ret = lttcomm_send_unix_sock(sock, &reply, sizeof(reply));
2234 if (ret != sizeof(reply)) {
2237 ret = lttcomm_send_unix_sock(sock, closed_trace_chunk_path,
2239 if (ret != reply.path_length) {
2244 case LTTNG_CONSUMER_TRACE_CHUNK_EXISTS:
2246 const uint64_t relayd_id =
2247 msg.u.trace_chunk_exists.relayd_id.value;
2249 ret_code = lttng_consumer_trace_chunk_exists(
2250 msg.u.trace_chunk_exists.relayd_id.is_set ?
2252 msg.u.trace_chunk_exists.session_id,
2253 msg.u.trace_chunk_exists.chunk_id);
2254 goto end_msg_sessiond;
2256 case LTTNG_CONSUMER_OPEN_CHANNEL_PACKETS:
2258 const uint64_t key = msg.u.open_channel_packets.key;
2259 struct lttng_consumer_channel *found_channel =
2260 consumer_find_channel(key);
2262 if (found_channel) {
2263 pthread_mutex_lock(&found_channel->lock);
2264 ret_code = lttng_consumer_open_channel_packets(
2266 pthread_mutex_unlock(&found_channel->lock);
2269 * The channel could have disappeared in per-pid
2272 DBG("Channel %" PRIu64 " not found", key);
2273 ret_code = LTTCOMM_CONSUMERD_CHAN_NOT_FOUND;
2276 health_code_update();
2277 goto end_msg_sessiond;
2285 * Return 1 to indicate success since the 0 value can be a socket
2286 * shutdown during the recv() or send() call.
2293 * The returned value here is not useful since either way we'll return 1 to
2294 * the caller because the session daemon socket management is done
2295 * elsewhere. Returning a negative code or 0 will shutdown the consumer.
2298 int ret_send_status;
2300 ret_send_status = consumer_send_status_msg(sock, ret_code);
2301 if (ret_send_status < 0) {
2312 * Free channel here since no one has a reference to it. We don't
2313 * free after that because a stream can store this pointer.
2315 destroy_channel(channel);
2317 /* We have to send a status channel message indicating an error. */
2319 int ret_send_status;
2321 ret_send_status = consumer_send_status_channel(sock, NULL);
2322 if (ret_send_status < 0) {
2323 /* Stop everything if session daemon can not be notified. */
2332 /* This will issue a consumer stop. */
2338 health_code_update();
2342 int lttng_ust_flush_buffer(struct lttng_consumer_stream *stream,
2343 int producer_active)
2346 assert(stream->ustream);
2348 return lttng_ust_ctl_flush_buffer(stream->ustream, producer_active);
2352 * Take a snapshot for a specific stream.
2354 * Returns 0 on success, < 0 on error
2356 int lttng_ustconsumer_take_snapshot(struct lttng_consumer_stream *stream)
2359 assert(stream->ustream);
2361 return lttng_ust_ctl_snapshot(stream->ustream);
2365 * Sample consumed and produced positions for a specific stream.
2367 * Returns 0 on success, < 0 on error.
2369 int lttng_ustconsumer_sample_snapshot_positions(
2370 struct lttng_consumer_stream *stream)
2373 assert(stream->ustream);
2375 return lttng_ust_ctl_snapshot_sample_positions(stream->ustream);
2379 * Get the produced position
2381 * Returns 0 on success, < 0 on error
2383 int lttng_ustconsumer_get_produced_snapshot(
2384 struct lttng_consumer_stream *stream, unsigned long *pos)
2387 assert(stream->ustream);
2390 return lttng_ust_ctl_snapshot_get_produced(stream->ustream, pos);
2394 * Get the consumed position
2396 * Returns 0 on success, < 0 on error
2398 int lttng_ustconsumer_get_consumed_snapshot(
2399 struct lttng_consumer_stream *stream, unsigned long *pos)
2402 assert(stream->ustream);
2405 return lttng_ust_ctl_snapshot_get_consumed(stream->ustream, pos);
2408 int lttng_ustconsumer_flush_buffer(struct lttng_consumer_stream *stream,
2412 assert(stream->ustream);
2414 return lttng_ust_ctl_flush_buffer(stream->ustream, producer);
2417 int lttng_ustconsumer_clear_buffer(struct lttng_consumer_stream *stream)
2420 assert(stream->ustream);
2422 return lttng_ust_ctl_clear_buffer(stream->ustream);
2425 int lttng_ustconsumer_get_current_timestamp(
2426 struct lttng_consumer_stream *stream, uint64_t *ts)
2429 assert(stream->ustream);
2432 return lttng_ust_ctl_get_current_timestamp(stream->ustream, ts);
2435 int lttng_ustconsumer_get_sequence_number(
2436 struct lttng_consumer_stream *stream, uint64_t *seq)
2439 assert(stream->ustream);
2442 return lttng_ust_ctl_get_sequence_number(stream->ustream, seq);
2446 * Called when the stream signals the consumer that it has hung up.
2448 void lttng_ustconsumer_on_stream_hangup(struct lttng_consumer_stream *stream)
2451 assert(stream->ustream);
2453 pthread_mutex_lock(&stream->lock);
2454 if (!stream->quiescent) {
2455 if (lttng_ust_ctl_flush_buffer(stream->ustream, 0) < 0) {
2456 ERR("Failed to flush buffer on stream hang-up");
2458 stream->quiescent = true;
2461 pthread_mutex_unlock(&stream->lock);
2462 stream->hangup_flush_done = 1;
2465 void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan)
2470 assert(chan->uchan);
2471 assert(chan->buffer_credentials.is_set);
2473 if (chan->switch_timer_enabled == 1) {
2474 consumer_timer_switch_stop(chan);
2476 for (i = 0; i < chan->nr_stream_fds; i++) {
2479 ret = close(chan->stream_fds[i]);
2483 if (chan->shm_path[0]) {
2484 char shm_path[PATH_MAX];
2486 ret = get_stream_shm_path(shm_path, chan->shm_path, i);
2488 ERR("Cannot get stream shm path");
2490 ret = run_as_unlink(shm_path,
2491 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2492 chan->buffer_credentials)),
2493 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2494 chan->buffer_credentials)));
2496 PERROR("unlink %s", shm_path);
2502 void lttng_ustconsumer_free_channel(struct lttng_consumer_channel *chan)
2505 assert(chan->uchan);
2506 assert(chan->buffer_credentials.is_set);
2508 consumer_metadata_cache_destroy(chan);
2509 lttng_ust_ctl_destroy_channel(chan->uchan);
2510 /* Try to rmdir all directories under shm_path root. */
2511 if (chan->root_shm_path[0]) {
2512 (void) run_as_rmdir_recursive(chan->root_shm_path,
2513 lttng_credentials_get_uid(LTTNG_OPTIONAL_GET_PTR(
2514 chan->buffer_credentials)),
2515 lttng_credentials_get_gid(LTTNG_OPTIONAL_GET_PTR(
2516 chan->buffer_credentials)),
2517 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
2519 free(chan->stream_fds);
2522 void lttng_ustconsumer_del_stream(struct lttng_consumer_stream *stream)
2525 assert(stream->ustream);
2527 if (stream->chan->switch_timer_enabled == 1) {
2528 consumer_timer_switch_stop(stream->chan);
2530 lttng_ust_ctl_destroy_stream(stream->ustream);
2533 int lttng_ustconsumer_get_wakeup_fd(struct lttng_consumer_stream *stream)
2536 assert(stream->ustream);
2538 return lttng_ust_ctl_stream_get_wakeup_fd(stream->ustream);
2541 int lttng_ustconsumer_close_wakeup_fd(struct lttng_consumer_stream *stream)
2544 assert(stream->ustream);
2546 return lttng_ust_ctl_stream_close_wakeup_fd(stream->ustream);
2550 * Write up to one packet from the metadata cache to the channel.
2552 * Returns the number of bytes pushed from the cache into the ring buffer, or a
2553 * negative value on error.
2556 int commit_one_metadata_packet(struct lttng_consumer_stream *stream)
2561 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
2562 if (stream->chan->metadata_cache->contents.size ==
2563 stream->ust_metadata_pushed) {
2565 * In the context of a user space metadata channel, a
2566 * change in version can be detected in two ways:
2567 * 1) During the pre-consume of the `read_subbuffer` loop,
2568 * 2) When populating the metadata ring buffer (i.e. here).
2570 * This function is invoked when there is no metadata
2571 * available in the ring-buffer. If all data was consumed
2572 * up to the size of the metadata cache, there is no metadata
2573 * to insert in the ring-buffer.
2575 * However, the metadata version could still have changed (a
2576 * regeneration without any new data will yield the same cache
2579 * The cache's version is checked for a version change and the
2580 * consumed position is reset if one occurred.
2582 * This check is only necessary for the user space domain as
2583 * it has to manage the cache explicitly. If this reset was not
2584 * performed, no metadata would be consumed (and no reset would
2585 * occur as part of the pre-consume) until the metadata size
2586 * exceeded the cache size.
2588 if (stream->metadata_version !=
2589 stream->chan->metadata_cache->version) {
2590 metadata_stream_reset_cache_consumed_position(stream);
2591 consumer_stream_metadata_set_version(stream,
2592 stream->chan->metadata_cache->version);
2599 write_len = lttng_ust_ctl_write_one_packet_to_channel(stream->chan->uchan,
2600 &stream->chan->metadata_cache->contents.data[stream->ust_metadata_pushed],
2601 stream->chan->metadata_cache->contents.size -
2602 stream->ust_metadata_pushed);
2603 assert(write_len != 0);
2604 if (write_len < 0) {
2605 ERR("Writing one metadata packet");
2609 stream->ust_metadata_pushed += write_len;
2611 assert(stream->chan->metadata_cache->contents.size >=
2612 stream->ust_metadata_pushed);
2616 * Switch packet (but don't open the next one) on every commit of
2617 * a metadata packet. Since the subbuffer is fully filled (with padding,
2618 * if needed), the stream is "quiescent" after this commit.
2620 if (lttng_ust_ctl_flush_buffer(stream->ustream, 1)) {
2621 ERR("Failed to flush buffer while commiting one metadata packet");
2624 stream->quiescent = true;
2627 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
2633 * Sync metadata meaning request them to the session daemon and snapshot to the
2634 * metadata thread can consumer them.
2636 * Metadata stream lock is held here, but we need to release it when
2637 * interacting with sessiond, else we cause a deadlock with live
2638 * awaiting on metadata to be pushed out.
2640 * The RCU read side lock must be held by the caller.
2642 enum sync_metadata_status lttng_ustconsumer_sync_metadata(
2643 struct lttng_consumer_local_data *ctx,
2644 struct lttng_consumer_stream *metadata_stream)
2647 enum sync_metadata_status status;
2648 struct lttng_consumer_channel *metadata_channel;
2651 assert(metadata_stream);
2653 metadata_channel = metadata_stream->chan;
2654 pthread_mutex_unlock(&metadata_stream->lock);
2656 * Request metadata from the sessiond, but don't wait for the flush
2657 * because we locked the metadata thread.
2659 ret = lttng_ustconsumer_request_metadata(ctx, metadata_channel, 0, 0);
2660 pthread_mutex_lock(&metadata_stream->lock);
2662 status = SYNC_METADATA_STATUS_ERROR;
2667 * The metadata stream and channel can be deleted while the
2668 * metadata stream lock was released. The streamed is checked
2669 * for deletion before we use it further.
2671 * Note that it is safe to access a logically-deleted stream since its
2672 * existence is still guaranteed by the RCU read side lock. However,
2673 * it should no longer be used. The close/deletion of the metadata
2674 * channel and stream already guarantees that all metadata has been
2675 * consumed. Therefore, there is nothing left to do in this function.
2677 if (consumer_stream_is_deleted(metadata_stream)) {
2678 DBG("Metadata stream %" PRIu64 " was deleted during the metadata synchronization",
2679 metadata_stream->key);
2680 status = SYNC_METADATA_STATUS_NO_DATA;
2684 ret = commit_one_metadata_packet(metadata_stream);
2686 status = SYNC_METADATA_STATUS_ERROR;
2688 } else if (ret > 0) {
2689 status = SYNC_METADATA_STATUS_NEW_DATA;
2690 } else /* ret == 0 */ {
2691 status = SYNC_METADATA_STATUS_NO_DATA;
2695 ret = lttng_ust_ctl_snapshot(metadata_stream->ustream);
2697 ERR("Failed to take a snapshot of the metadata ring-buffer positions, ret = %d", ret);
2698 status = SYNC_METADATA_STATUS_ERROR;
2707 * Return 0 on success else a negative value.
2709 static int notify_if_more_data(struct lttng_consumer_stream *stream,
2710 struct lttng_consumer_local_data *ctx)
2713 struct lttng_ust_ctl_consumer_stream *ustream;
2718 ustream = stream->ustream;
2721 * First, we are going to check if there is a new subbuffer available
2722 * before reading the stream wait_fd.
2724 /* Get the next subbuffer */
2725 ret = lttng_ust_ctl_get_next_subbuf(ustream);
2727 /* No more data found, flag the stream. */
2728 stream->has_data = 0;
2733 ret = lttng_ust_ctl_put_subbuf(ustream);
2736 /* This stream still has data. Flag it and wake up the data thread. */
2737 stream->has_data = 1;
2739 if (stream->monitor && !stream->hangup_flush_done && !ctx->has_wakeup) {
2742 writelen = lttng_pipe_write(ctx->consumer_wakeup_pipe, "!", 1);
2743 if (writelen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
2748 /* The wake up pipe has been notified. */
2749 ctx->has_wakeup = 1;
2757 static int consumer_stream_ust_on_wake_up(struct lttng_consumer_stream *stream)
2762 * We can consume the 1 byte written into the wait_fd by
2763 * UST. Don't trigger error if we cannot read this one byte
2764 * (read returns 0), or if the error is EAGAIN or EWOULDBLOCK.
2766 * This is only done when the stream is monitored by a thread,
2767 * before the flush is done after a hangup and if the stream
2768 * is not flagged with data since there might be nothing to
2769 * consume in the wait fd but still have data available
2770 * flagged by the consumer wake up pipe.
2772 if (stream->monitor && !stream->hangup_flush_done && !stream->has_data) {
2776 readlen = lttng_read(stream->wait_fd, &dummy, 1);
2777 if (readlen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
2785 static int extract_common_subbuffer_info(struct lttng_consumer_stream *stream,
2786 struct stream_subbuffer *subbuf)
2790 ret = lttng_ust_ctl_get_subbuf_size(
2791 stream->ustream, &subbuf->info.data.subbuf_size);
2796 ret = lttng_ust_ctl_get_padded_subbuf_size(
2797 stream->ustream, &subbuf->info.data.padded_subbuf_size);
2806 static int extract_metadata_subbuffer_info(struct lttng_consumer_stream *stream,
2807 struct stream_subbuffer *subbuf)
2811 ret = extract_common_subbuffer_info(stream, subbuf);
2816 subbuf->info.metadata.version = stream->metadata_version;
2822 static int extract_data_subbuffer_info(struct lttng_consumer_stream *stream,
2823 struct stream_subbuffer *subbuf)
2827 ret = extract_common_subbuffer_info(stream, subbuf);
2832 ret = lttng_ust_ctl_get_packet_size(
2833 stream->ustream, &subbuf->info.data.packet_size);
2835 PERROR("Failed to get sub-buffer packet size");
2839 ret = lttng_ust_ctl_get_content_size(
2840 stream->ustream, &subbuf->info.data.content_size);
2842 PERROR("Failed to get sub-buffer content size");
2846 ret = lttng_ust_ctl_get_timestamp_begin(
2847 stream->ustream, &subbuf->info.data.timestamp_begin);
2849 PERROR("Failed to get sub-buffer begin timestamp");
2853 ret = lttng_ust_ctl_get_timestamp_end(
2854 stream->ustream, &subbuf->info.data.timestamp_end);
2856 PERROR("Failed to get sub-buffer end timestamp");
2860 ret = lttng_ust_ctl_get_events_discarded(
2861 stream->ustream, &subbuf->info.data.events_discarded);
2863 PERROR("Failed to get sub-buffer events discarded count");
2867 ret = lttng_ust_ctl_get_sequence_number(stream->ustream,
2868 &subbuf->info.data.sequence_number.value);
2870 /* May not be supported by older LTTng-modules. */
2871 if (ret != -ENOTTY) {
2872 PERROR("Failed to get sub-buffer sequence number");
2876 subbuf->info.data.sequence_number.is_set = true;
2879 ret = lttng_ust_ctl_get_stream_id(
2880 stream->ustream, &subbuf->info.data.stream_id);
2882 PERROR("Failed to get stream id");
2886 ret = lttng_ust_ctl_get_instance_id(stream->ustream,
2887 &subbuf->info.data.stream_instance_id.value);
2889 /* May not be supported by older LTTng-modules. */
2890 if (ret != -ENOTTY) {
2891 PERROR("Failed to get stream instance id");
2895 subbuf->info.data.stream_instance_id.is_set = true;
2901 static int get_next_subbuffer_common(struct lttng_consumer_stream *stream,
2902 struct stream_subbuffer *subbuffer)
2907 ret = stream->read_subbuffer_ops.extract_subbuffer_info(
2913 ret = get_current_subbuf_addr(stream, &addr);
2918 subbuffer->buffer.buffer = lttng_buffer_view_init(
2919 addr, 0, subbuffer->info.data.padded_subbuf_size);
2920 assert(subbuffer->buffer.buffer.data != NULL);
2925 static enum get_next_subbuffer_status get_next_subbuffer(
2926 struct lttng_consumer_stream *stream,
2927 struct stream_subbuffer *subbuffer)
2930 enum get_next_subbuffer_status status;
2932 ret = lttng_ust_ctl_get_next_subbuf(stream->ustream);
2935 status = GET_NEXT_SUBBUFFER_STATUS_OK;
2940 * The caller only expects -ENODATA when there is no data to
2941 * read, but the kernel tracer returns -EAGAIN when there is
2942 * currently no data for a non-finalized stream, and -ENODATA
2943 * when there is no data for a finalized stream. Those can be
2944 * combined into a -ENODATA return value.
2946 status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA;
2949 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
2953 ret = get_next_subbuffer_common(stream, subbuffer);
2955 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
2962 static enum get_next_subbuffer_status get_next_subbuffer_metadata(
2963 struct lttng_consumer_stream *stream,
2964 struct stream_subbuffer *subbuffer)
2971 unsigned long consumed_pos, produced_pos;
2972 enum get_next_subbuffer_status status;
2975 ret = lttng_ust_ctl_get_next_subbuf(stream->ustream);
2977 got_subbuffer = true;
2979 got_subbuffer = false;
2980 if (ret != -EAGAIN) {
2982 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
2988 * Determine if the cache is empty and ensure that a sub-buffer
2989 * is made available if the cache is not empty.
2991 if (!got_subbuffer) {
2992 ret = commit_one_metadata_packet(stream);
2993 if (ret < 0 && ret != -ENOBUFS) {
2994 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
2996 } else if (ret == 0) {
2997 /* Not an error, the cache is empty. */
2999 status = GET_NEXT_SUBBUFFER_STATUS_NO_DATA;
3002 cache_empty = false;
3005 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
3006 cache_empty = stream->chan->metadata_cache->contents.size ==
3007 stream->ust_metadata_pushed;
3008 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
3010 } while (!got_subbuffer);
3012 /* Populate sub-buffer infos and view. */
3013 ret = get_next_subbuffer_common(stream, subbuffer);
3015 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
3019 ret = lttng_ustconsumer_sample_snapshot_positions(stream);
3022 * -EAGAIN is not expected since we got a sub-buffer and haven't
3023 * pushed the consumption position yet (on put_next).
3025 PERROR("Failed to take a snapshot of metadata buffer positions");
3026 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
3030 ret = lttng_ustconsumer_get_consumed_snapshot(stream, &consumed_pos);
3032 PERROR("Failed to get metadata consumed position");
3033 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
3037 ret = lttng_ustconsumer_get_produced_snapshot(stream, &produced_pos);
3039 PERROR("Failed to get metadata produced position");
3040 status = GET_NEXT_SUBBUFFER_STATUS_ERROR;
3044 /* Last sub-buffer of the ring buffer ? */
3045 buffer_empty = (consumed_pos + stream->max_sb_size) == produced_pos;
3048 * The sessiond registry lock ensures that coherent units of metadata
3049 * are pushed to the consumer daemon at once. Hence, if a sub-buffer is
3050 * acquired, the cache is empty, and it is the only available sub-buffer
3051 * available, it is safe to assume that it is "coherent".
3053 coherent = got_subbuffer && cache_empty && buffer_empty;
3055 LTTNG_OPTIONAL_SET(&subbuffer->info.metadata.coherent, coherent);
3056 status = GET_NEXT_SUBBUFFER_STATUS_OK;
3061 static int put_next_subbuffer(struct lttng_consumer_stream *stream,
3062 struct stream_subbuffer *subbuffer)
3064 const int ret = lttng_ust_ctl_put_next_subbuf(stream->ustream);
3070 static int signal_metadata(struct lttng_consumer_stream *stream,
3071 struct lttng_consumer_local_data *ctx)
3073 ASSERT_LOCKED(stream->metadata_rdv_lock);
3074 return pthread_cond_broadcast(&stream->metadata_rdv) ? -errno : 0;
3077 static int lttng_ustconsumer_set_stream_ops(
3078 struct lttng_consumer_stream *stream)
3082 stream->read_subbuffer_ops.on_wake_up = consumer_stream_ust_on_wake_up;
3083 if (stream->metadata_flag) {
3084 stream->read_subbuffer_ops.get_next_subbuffer =
3085 get_next_subbuffer_metadata;
3086 stream->read_subbuffer_ops.extract_subbuffer_info =
3087 extract_metadata_subbuffer_info;
3088 stream->read_subbuffer_ops.reset_metadata =
3089 metadata_stream_reset_cache_consumed_position;
3090 if (stream->chan->is_live) {
3091 stream->read_subbuffer_ops.on_sleep = signal_metadata;
3092 ret = consumer_stream_enable_metadata_bucketization(
3099 stream->read_subbuffer_ops.get_next_subbuffer =
3101 stream->read_subbuffer_ops.extract_subbuffer_info =
3102 extract_data_subbuffer_info;
3103 stream->read_subbuffer_ops.on_sleep = notify_if_more_data;
3104 if (stream->chan->is_live) {
3105 stream->read_subbuffer_ops.send_live_beacon =
3106 consumer_flush_ust_index;
3110 stream->read_subbuffer_ops.put_next_subbuffer = put_next_subbuffer;
3116 * Called when a stream is created.
3118 * Return 0 on success or else a negative value.
3120 int lttng_ustconsumer_on_recv_stream(struct lttng_consumer_stream *stream)
3127 * Don't create anything if this is set for streaming or if there is
3128 * no current trace chunk on the parent channel.
3130 if (stream->net_seq_idx == (uint64_t) -1ULL && stream->chan->monitor &&
3131 stream->chan->trace_chunk) {
3132 ret = consumer_stream_create_output_files(stream, true);
3138 lttng_ustconsumer_set_stream_ops(stream);
3146 * Check if data is still being extracted from the buffers for a specific
3147 * stream. Consumer data lock MUST be acquired before calling this function
3148 * and the stream lock.
3150 * Return 1 if the traced data are still getting read else 0 meaning that the
3151 * data is available for trace viewer reading.
3153 int lttng_ustconsumer_data_pending(struct lttng_consumer_stream *stream)
3158 assert(stream->ustream);
3159 ASSERT_LOCKED(stream->lock);
3161 DBG("UST consumer checking data pending");
3163 if (stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) {
3168 if (stream->chan->type == CONSUMER_CHANNEL_TYPE_METADATA) {
3169 uint64_t contiguous, pushed;
3171 /* Ease our life a bit. */
3172 pthread_mutex_lock(&stream->chan->metadata_cache->lock);
3173 contiguous = stream->chan->metadata_cache->contents.size;
3174 pthread_mutex_unlock(&stream->chan->metadata_cache->lock);
3175 pushed = stream->ust_metadata_pushed;
3178 * We can simply check whether all contiguously available data
3179 * has been pushed to the ring buffer, since the push operation
3180 * is performed within get_next_subbuf(), and because both
3181 * get_next_subbuf() and put_next_subbuf() are issued atomically
3182 * thanks to the stream lock within
3183 * lttng_ustconsumer_read_subbuffer(). This basically means that
3184 * whetnever ust_metadata_pushed is incremented, the associated
3185 * metadata has been consumed from the metadata stream.
3187 DBG("UST consumer metadata pending check: contiguous %" PRIu64 " vs pushed %" PRIu64,
3188 contiguous, pushed);
3189 assert(((int64_t) (contiguous - pushed)) >= 0);
3190 if ((contiguous != pushed) ||
3191 (((int64_t) contiguous - pushed) > 0 || contiguous == 0)) {
3192 ret = 1; /* Data is pending */
3196 ret = lttng_ust_ctl_get_next_subbuf(stream->ustream);
3199 * There is still data so let's put back this
3202 ret = lttng_ust_ctl_put_subbuf(stream->ustream);
3204 ret = 1; /* Data is pending */
3209 /* Data is NOT pending so ready to be read. */
3217 * Stop a given metadata channel timer if enabled and close the wait fd which
3218 * is the poll pipe of the metadata stream.
3220 * This MUST be called with the metadata channel lock acquired.
3222 void lttng_ustconsumer_close_metadata(struct lttng_consumer_channel *metadata)
3227 assert(metadata->type == CONSUMER_CHANNEL_TYPE_METADATA);
3229 DBG("Closing metadata channel key %" PRIu64, metadata->key);
3231 if (metadata->switch_timer_enabled == 1) {
3232 consumer_timer_switch_stop(metadata);
3235 if (!metadata->metadata_stream) {
3240 * Closing write side so the thread monitoring the stream wakes up if any
3241 * and clean the metadata stream.
3243 if (metadata->metadata_stream->ust_metadata_poll_pipe[1] >= 0) {
3244 ret = close(metadata->metadata_stream->ust_metadata_poll_pipe[1]);
3246 PERROR("closing metadata pipe write side");
3248 metadata->metadata_stream->ust_metadata_poll_pipe[1] = -1;
3256 * Close every metadata stream wait fd of the metadata hash table. This
3257 * function MUST be used very carefully so not to run into a race between the
3258 * metadata thread handling streams and this function closing their wait fd.
3260 * For UST, this is used when the session daemon hangs up. Its the metadata
3261 * producer so calling this is safe because we are assured that no state change
3262 * can occur in the metadata thread for the streams in the hash table.
3264 void lttng_ustconsumer_close_all_metadata(struct lttng_ht *metadata_ht)
3266 struct lttng_ht_iter iter;
3267 struct lttng_consumer_stream *stream;
3269 assert(metadata_ht);
3270 assert(metadata_ht->ht);
3272 DBG("UST consumer closing all metadata streams");
3275 cds_lfht_for_each_entry(metadata_ht->ht, &iter.iter, stream,
3278 health_code_update();
3280 pthread_mutex_lock(&stream->chan->lock);
3281 lttng_ustconsumer_close_metadata(stream->chan);
3282 pthread_mutex_unlock(&stream->chan->lock);
3288 void lttng_ustconsumer_close_stream_wakeup(struct lttng_consumer_stream *stream)
3292 ret = lttng_ust_ctl_stream_close_wakeup_fd(stream->ustream);
3294 ERR("Unable to close wakeup fd");
3299 * Please refer to consumer-timer.c before adding any lock within this
3300 * function or any of its callees. Timers have a very strict locking
3301 * semantic with respect to teardown. Failure to respect this semantic
3302 * introduces deadlocks.
3304 * DON'T hold the metadata lock when calling this function, else this
3305 * can cause deadlock involving consumer awaiting for metadata to be
3306 * pushed out due to concurrent interaction with the session daemon.
3308 int lttng_ustconsumer_request_metadata(struct lttng_consumer_local_data *ctx,
3309 struct lttng_consumer_channel *channel, int timer, int wait)
3311 struct lttcomm_metadata_request_msg request;
3312 struct lttcomm_consumer_msg msg;
3313 enum lttcomm_return_code ret_code = LTTCOMM_CONSUMERD_SUCCESS;
3314 uint64_t len, key, offset, version;
3318 assert(channel->metadata_cache);
3320 memset(&request, 0, sizeof(request));
3322 /* send the metadata request to sessiond */
3323 switch (the_consumer_data.type) {
3324 case LTTNG_CONSUMER64_UST:
3325 request.bits_per_long = 64;
3327 case LTTNG_CONSUMER32_UST:
3328 request.bits_per_long = 32;
3331 request.bits_per_long = 0;
3335 request.session_id = channel->session_id;
3336 request.session_id_per_pid = channel->session_id_per_pid;
3338 * Request the application UID here so the metadata of that application can
3339 * be sent back. The channel UID corresponds to the user UID of the session
3340 * used for the rights on the stream file(s).
3342 request.uid = channel->ust_app_uid;
3343 request.key = channel->key;
3345 DBG("Sending metadata request to sessiond, session id %" PRIu64
3346 ", per-pid %" PRIu64 ", app UID %u and channel key %" PRIu64,
3347 request.session_id, request.session_id_per_pid, request.uid,
3350 pthread_mutex_lock(&ctx->metadata_socket_lock);
3352 health_code_update();
3354 ret = lttcomm_send_unix_sock(ctx->consumer_metadata_socket, &request,
3357 ERR("Asking metadata to sessiond");
3361 health_code_update();
3363 /* Receive the metadata from sessiond */
3364 ret = lttcomm_recv_unix_sock(ctx->consumer_metadata_socket, &msg,
3366 if (ret != sizeof(msg)) {
3367 DBG("Consumer received unexpected message size %d (expects %zu)",
3369 lttng_consumer_send_error(ctx, LTTCOMM_CONSUMERD_ERROR_RECV_CMD);
3371 * The ret value might 0 meaning an orderly shutdown but this is ok
3372 * since the caller handles this.
3377 health_code_update();
3379 if (msg.cmd_type == LTTNG_ERR_UND) {
3380 /* No registry found */
3381 (void) consumer_send_status_msg(ctx->consumer_metadata_socket,
3385 } else if (msg.cmd_type != LTTNG_CONSUMER_PUSH_METADATA) {
3386 ERR("Unexpected cmd_type received %d", msg.cmd_type);
3391 len = msg.u.push_metadata.len;
3392 key = msg.u.push_metadata.key;
3393 offset = msg.u.push_metadata.target_offset;
3394 version = msg.u.push_metadata.version;
3396 assert(key == channel->key);
3398 DBG("No new metadata to receive for key %" PRIu64, key);
3401 health_code_update();
3403 /* Tell session daemon we are ready to receive the metadata. */
3404 ret = consumer_send_status_msg(ctx->consumer_metadata_socket,
3405 LTTCOMM_CONSUMERD_SUCCESS);
3406 if (ret < 0 || len == 0) {
3408 * Somehow, the session daemon is not responding anymore or there is
3409 * nothing to receive.
3414 health_code_update();
3416 ret = lttng_ustconsumer_recv_metadata(ctx->consumer_metadata_socket,
3417 key, offset, len, version, channel, timer, wait);
3420 * Only send the status msg if the sessiond is alive meaning a positive
3423 (void) consumer_send_status_msg(ctx->consumer_metadata_socket, ret);
3428 health_code_update();
3430 pthread_mutex_unlock(&ctx->metadata_socket_lock);
3435 * Return the ustctl call for the get stream id.
3437 int lttng_ustconsumer_get_stream_id(struct lttng_consumer_stream *stream,
3438 uint64_t *stream_id)
3443 return lttng_ust_ctl_get_stream_id(stream->ustream, stream_id);
3446 void lttng_ustconsumer_sigbus_handle(void *addr)
3448 lttng_ust_ctl_sigbus_handle(addr);