- ret = ustctl_get_timestamp_begin(ustream, &index->timestamp_begin);
- if (ret < 0) {
- PERROR("ustctl_get_timestamp_begin");
- goto error;
- }
- index->timestamp_begin = htobe64(index->timestamp_begin);
-
- ret = ustctl_get_timestamp_end(ustream, &index->timestamp_end);
- if (ret < 0) {
- PERROR("ustctl_get_timestamp_end");
- goto error;
- }
- index->timestamp_end = htobe64(index->timestamp_end);
-
- ret = ustctl_get_events_discarded(ustream, &index->events_discarded);
- if (ret < 0) {
- PERROR("ustctl_get_events_discarded");
- goto error;
- }
- index->events_discarded = htobe64(index->events_discarded);
-
- ret = ustctl_get_content_size(ustream, &index->content_size);
- if (ret < 0) {
- PERROR("ustctl_get_content_size");
- goto error;
- }
- index->content_size = htobe64(index->content_size);
-
- ret = ustctl_get_packet_size(ustream, &index->packet_size);
- if (ret < 0) {
- PERROR("ustctl_get_packet_size");
- goto error;
- }
- index->packet_size = htobe64(index->packet_size);
-
- ret = ustctl_get_stream_id(ustream, &index->stream_id);
- if (ret < 0) {
- PERROR("ustctl_get_stream_id");
- goto error;
- }
- index->stream_id = htobe64(index->stream_id);
-
- ret = ustctl_get_instance_id(ustream, &index->stream_instance_id);
- if (ret < 0) {
- PERROR("ustctl_get_instance_id");
- goto error;
- }
- index->stream_instance_id = htobe64(index->stream_instance_id);
-
- ret = ustctl_get_sequence_number(ustream, &index->packet_seq_num);
- if (ret < 0) {
- PERROR("ustctl_get_sequence_number");
- goto error;
- }
- index->packet_seq_num = htobe64(index->packet_seq_num);
-
-error:
- return ret;
-}
-
-static
-void metadata_stream_reset_cache(struct lttng_consumer_stream *stream,
- struct consumer_metadata_cache *cache)
-{
- DBG("Metadata stream update to version %" PRIu64,
- cache->version);
- stream->ust_metadata_pushed = 0;
- stream->metadata_version = cache->version;
- stream->reset_metadata_flag = 1;
-}
-
-/*
- * Check if the version of the metadata stream and metadata cache match.
- * If the cache got updated, reset the metadata stream.
- * The stream lock and metadata cache lock MUST be held.
- * Return 0 on success, a negative value on error.
- */
-static
-int metadata_stream_check_version(struct lttng_consumer_stream *stream)
-{
- int ret = 0;
- struct consumer_metadata_cache *cache = stream->chan->metadata_cache;
-
- if (cache->version == stream->metadata_version) {
- goto end;
- }
- metadata_stream_reset_cache(stream, cache);
-
-end:
- return ret;
-}
-
-/*
- * Write up to one packet from the metadata cache to the channel.
- *
- * Returns the number of bytes pushed in the cache, or a negative value
- * on error.
- */
-static
-int commit_one_metadata_packet(struct lttng_consumer_stream *stream)
-{
- ssize_t write_len;
- int ret;
-
- pthread_mutex_lock(&stream->chan->metadata_cache->lock);
- ret = metadata_stream_check_version(stream);
- if (ret < 0) {
- goto end;
- }
- if (stream->chan->metadata_cache->max_offset
- == stream->ust_metadata_pushed) {
- ret = 0;
- goto end;
+ pthread_mutex_lock(&stream->chan->metadata_cache->lock);
+ if (stream->chan->metadata_cache->max_offset ==
+ stream->ust_metadata_pushed) {
+ /*
+ * In the context of a user space metadata channel, a
+ * change in version can be detected in two ways:
+ * 1) During the pre-consume of the `read_subbuffer` loop,
+ * 2) When populating the metadata ring buffer (i.e. here).
+ *
+ * This function is invoked when there is no metadata
+ * available in the ring-buffer. If all data was consumed
+ * up to the size of the metadata cache, there is no metadata
+ * to insert in the ring-buffer.
+ *
+ * However, the metadata version could still have changed (a
+ * regeneration without any new data will yield the same cache
+ * size).
+ *
+ * The cache's version is checked for a version change and the
+ * consumed position is reset if one occurred.
+ *
+ * This check is only necessary for the user space domain as
+ * it has to manage the cache explicitly. If this reset was not
+ * performed, no metadata would be consumed (and no reset would
+ * occur as part of the pre-consume) until the metadata size
+ * exceeded the cache size.
+ */
+ if (stream->metadata_version !=
+ stream->chan->metadata_cache->version) {
+ metadata_stream_reset_cache_consumed_position(stream);
+ consumer_stream_metadata_set_version(stream,
+ stream->chan->metadata_cache->version);
+ } else {
+ ret = 0;
+ goto end;
+ }