Clean-up: ust-consumer: replace manual metadata cache buffer allocation
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 9 Feb 2021 00:01:15 +0000 (19:01 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 9 Feb 2021 03:03:43 +0000 (22:03 -0500)
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 <jeremie.galarneau@efficios.com>
Change-Id: I793243f2e8f5f17a4a6c0abdbafb7295ae90cf60

src/common/consumer/consumer-metadata-cache.c
src/common/consumer/consumer-metadata-cache.h
src/common/ust-consumer/ust-consumer.c

index 732a4687b82cd335284402cd4b5108be9d4e346b..976c83e54fd5dc6904cb6e72f21ea4af81426d9f 100644 (file)
@@ -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);
 }
 
index 303f5cd2a1aeb7d71968b74c7c13876a82b32992..b207cc79c233df23fb926aec7382b83094700a0d 100644 (file)
@@ -10,6 +10,7 @@
 #define CONSUMER_METADATA_CACHE_H
 
 #include <common/consumer/consumer.h>
+#include <common/dynamic-buffer.h>
 
 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).
index 10c8f4e62c5c343984260a72a9e29b6b9b3976e7..6b195c77ddae86a3e2d1ca27d6ce92d821819c3d 100644 (file)
@@ -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;
 
This page took 0.028971 seconds and 4 git commands to generate.