From: Jérémie Galarneau Date: Tue, 9 Feb 2021 00:01:15 +0000 (-0500) Subject: Clean-up: ust-consumer: replace manual metadata cache buffer allocation X-Git-Tag: v2.13.0-rc1~348 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=9eac98281fd4923ea52b94abffd0cf8cfa27588e;p=lttng-tools.git Clean-up: ust-consumer: replace manual metadata cache buffer allocation Replace the hand-rolled buffer allocation management code of the metadata cache by the lttng_dynamic_buffer utility. Signed-off-by: Jérémie Galarneau Change-Id: I793243f2e8f5f17a4a6c0abdbafb7295ae90cf60 --- diff --git a/src/common/consumer/consumer-metadata-cache.c b/src/common/consumer/consumer-metadata-cache.c index 732a4687b..976c83e54 100644 --- a/src/common/consumer/consumer-metadata-cache.c +++ b/src/common/consumer/consumer-metadata-cache.c @@ -30,51 +30,15 @@ enum metadata_cache_update_version_status { extern struct lttng_consumer_global_data consumer_data; -/* - * Extend the allocated size of the metadata cache. Called only from - * lttng_ustconsumer_write_metadata_cache. - * - * Return 0 on success, a negative value on error. - */ -static int extend_metadata_cache(struct consumer_metadata_cache *cache, - unsigned int size) -{ - int ret = 0; - char *tmp_data_ptr; - unsigned int new_size, old_size; - - assert(cache); - - old_size = cache->cache_alloc_size; - new_size = max_t(unsigned int, old_size + size, old_size << 1); - DBG("Extending metadata cache: old size = %u, new size = %u", old_size, - new_size); - - tmp_data_ptr = realloc(cache->data, new_size); - if (!tmp_data_ptr) { - ERR("Failed to re-allocate metadata cache"); - free(cache->data); - ret = -1; - goto end; - } - - /* Zero newly allocated memory. */ - memset(tmp_data_ptr + old_size, 0, new_size - old_size); - cache->data = tmp_data_ptr; - cache->cache_alloc_size = new_size; - -end: - return ret; -} - /* * Reset the metadata cache. */ static void metadata_cache_reset(struct consumer_metadata_cache *cache) { - memset(cache->data, 0, cache->cache_alloc_size); - cache->max_offset = 0; + const int ret = lttng_dynamic_buffer_set_size(&cache->contents, 0); + + assert(ret == 0); } /* @@ -117,11 +81,11 @@ consumer_metadata_cache_write(struct consumer_metadata_cache *cache, int ret = 0; enum consumer_metadata_cache_write_status status; bool cache_is_invalidated = false; - uint64_t original_max_offset; + uint64_t original_size; assert(cache); ASSERT_LOCKED(cache->lock); - original_max_offset = cache->max_offset; + original_size = cache->contents.size; if (metadata_cache_update_version(cache, version) == METADATA_CACHE_UPDATE_STATUS_VERSION_UPDATED) { @@ -130,27 +94,25 @@ consumer_metadata_cache_write(struct consumer_metadata_cache *cache, } DBG("Writing %u bytes from offset %u in metadata cache", len, offset); - - if (offset + len > cache->cache_alloc_size) { - ret = extend_metadata_cache(cache, - len - cache->cache_alloc_size + offset); - if (ret < 0) { + if (offset + len > cache->contents.size) { + ret = lttng_dynamic_buffer_set_size( + &cache->contents, offset + len); + if (ret) { ERR("Extending metadata cache"); status = CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR; goto end; } } - memcpy(cache->data + offset, data, len); - cache->max_offset = max(cache->max_offset, offset + len); + memcpy(cache->contents.data + offset, data, len); if (cache_is_invalidated) { status = CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED; - } else if (cache->max_offset > original_max_offset) { + } else if (cache->contents.size > original_size) { status = CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT; } else { status = CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE; - assert(cache->max_offset == original_max_offset); + assert(cache->contents.size == original_size); } end: @@ -181,16 +143,20 @@ int consumer_metadata_cache_allocate(struct lttng_consumer_channel *channel) goto end_free_cache; } - channel->metadata_cache->cache_alloc_size = DEFAULT_METADATA_CACHE_SIZE; - channel->metadata_cache->data = zmalloc( - channel->metadata_cache->cache_alloc_size * sizeof(char)); - if (!channel->metadata_cache->data) { - PERROR("zmalloc metadata cache data"); + lttng_dynamic_buffer_init(&channel->metadata_cache->contents); + ret = lttng_dynamic_buffer_set_capacity( + &channel->metadata_cache->contents, + DEFAULT_METADATA_CACHE_SIZE); + if (ret) { + PERROR("Failed to pre-allocate metadata cache storage of %d bytes on creation", + DEFAULT_METADATA_CACHE_SIZE); ret = -1; goto end_free_mutex; } - DBG("Allocated metadata cache of %" PRIu64 " bytes", - channel->metadata_cache->cache_alloc_size); + + DBG("Allocated metadata cache: current capacity = %zu", + lttng_dynamic_buffer_get_capacity_left( + &channel->metadata_cache->contents)); ret = 0; goto end; @@ -215,7 +181,7 @@ void consumer_metadata_cache_destroy(struct lttng_consumer_channel *channel) DBG("Destroying metadata cache"); pthread_mutex_destroy(&channel->metadata_cache->lock); - free(channel->metadata_cache->data); + lttng_dynamic_buffer_reset(&channel->metadata_cache->contents); free(channel->metadata_cache); } diff --git a/src/common/consumer/consumer-metadata-cache.h b/src/common/consumer/consumer-metadata-cache.h index 303f5cd2a..b207cc79c 100644 --- a/src/common/consumer/consumer-metadata-cache.h +++ b/src/common/consumer/consumer-metadata-cache.h @@ -10,6 +10,7 @@ #define CONSUMER_METADATA_CACHE_H #include +#include enum consumer_metadata_cache_write_status { CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR = -1, @@ -33,20 +34,13 @@ enum consumer_metadata_cache_write_status { }; struct consumer_metadata_cache { - char *data; - uint64_t cache_alloc_size; - /* - * Current version of the metadata cache. - */ + /* Current version of the metadata cache. */ uint64_t version; /* - * The upper-limit of data written inside the buffer. - * - * With the total_bytes_written it allows us to keep track of when the - * cache contains contiguous metadata ready to be sent to the RB. + * Size is the upper-limit of data written inside the buffer. * All cached data is contiguous. */ - uint64_t max_offset; + struct lttng_dynamic_buffer contents; /* * Lock to update the metadata cache and push into the ring_buffer * (ustctl_write_metadata_to_channel). diff --git a/src/common/ust-consumer/ust-consumer.c b/src/common/ust-consumer/ust-consumer.c index 10c8f4e62..6b195c77d 100644 --- a/src/common/ust-consumer/ust-consumer.c +++ b/src/common/ust-consumer/ust-consumer.c @@ -2522,8 +2522,8 @@ int commit_one_metadata_packet(struct lttng_consumer_stream *stream) int ret; pthread_mutex_lock(&stream->chan->metadata_cache->lock); - if (stream->chan->metadata_cache->max_offset == - stream->ust_metadata_pushed) { + if (stream->chan->metadata_cache->contents.size == + stream->ust_metadata_pushed) { /* * In the context of a user space metadata channel, a * change in version can be detected in two ways: @@ -2560,9 +2560,9 @@ int commit_one_metadata_packet(struct lttng_consumer_stream *stream) } write_len = ustctl_write_one_packet_to_channel(stream->chan->uchan, - &stream->chan->metadata_cache->data[stream->ust_metadata_pushed], - stream->chan->metadata_cache->max_offset - - stream->ust_metadata_pushed); + &stream->chan->metadata_cache->contents.data[stream->ust_metadata_pushed], + stream->chan->metadata_cache->contents.size - + stream->ust_metadata_pushed); assert(write_len != 0); if (write_len < 0) { ERR("Writing one metadata packet"); @@ -2571,7 +2571,7 @@ int commit_one_metadata_packet(struct lttng_consumer_stream *stream) } stream->ust_metadata_pushed += write_len; - assert(stream->chan->metadata_cache->max_offset >= + assert(stream->chan->metadata_cache->contents.size >= stream->ust_metadata_pushed); ret = write_len; @@ -2939,8 +2939,8 @@ static int get_next_subbuffer_metadata(struct lttng_consumer_stream *stream, } } else { pthread_mutex_lock(&stream->chan->metadata_cache->lock); - cache_empty = stream->chan->metadata_cache->max_offset == - stream->ust_metadata_pushed; + cache_empty = stream->chan->metadata_cache->contents.size == + stream->ust_metadata_pushed; pthread_mutex_unlock(&stream->chan->metadata_cache->lock); } } while (!got_subbuffer); @@ -3101,7 +3101,7 @@ int lttng_ustconsumer_data_pending(struct lttng_consumer_stream *stream) /* Ease our life a bit. */ pthread_mutex_lock(&stream->chan->metadata_cache->lock); - contiguous = stream->chan->metadata_cache->max_offset; + contiguous = stream->chan->metadata_cache->contents.size; pthread_mutex_unlock(&stream->chan->metadata_cache->lock); pushed = stream->ust_metadata_pushed;