Fix: consumerd: slow metadata push slows down application registration
[lttng-tools.git] / src / common / consumer / consumer-metadata-cache.c
index 732a4687b82cd335284402cd4b5108be9d4e346b..a5943fd33ae6f8f858af5c61e3504a70dbceb2e1 100644 (file)
@@ -28,44 +28,7 @@ enum metadata_cache_update_version_status {
        METADATA_CACHE_UPDATE_STATUS_VERSION_NOT_UPDATED,
 };
 
-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;
-}
+extern struct lttng_consumer_global_data the_consumer_data;
 
 /*
  * Reset the metadata cache.
@@ -73,8 +36,9 @@ end:
 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,24 +181,22 @@ 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);
 }
 
 /*
  * Check if the cache is flushed up to the offset passed in parameter.
  *
- * Return 0 if everything has been flushed, 1 if there is data not flushed.
+ * Return true if everything has been flushed, false if there is data not flushed.
  */
-int consumer_metadata_cache_flushed(struct lttng_consumer_channel *channel,
+static
+bool consumer_metadata_cache_is_flushed(struct lttng_consumer_channel *channel,
                uint64_t offset, int timer)
 {
-       int ret = 0;
+       bool done_flushing = false;
        struct lttng_consumer_stream *metadata_stream;
 
-       assert(channel);
-       assert(channel->metadata_cache);
-
        /*
         * If not called from a timer handler, we have to take the
         * channel lock to be mutually exclusive with channel teardown.
@@ -250,7 +214,7 @@ int consumer_metadata_cache_flushed(struct lttng_consumer_channel *channel,
                 * Having no metadata stream means the channel is being destroyed so there
                 * is no cache to flush anymore.
                 */
-               ret = 0;
+               done_flushing = true;
                goto end_unlock_channel;
        }
 
@@ -258,23 +222,56 @@ int consumer_metadata_cache_flushed(struct lttng_consumer_channel *channel,
        pthread_mutex_lock(&channel->metadata_cache->lock);
 
        if (metadata_stream->ust_metadata_pushed >= offset) {
-               ret = 0;
+               done_flushing = true;
        } else if (channel->metadata_stream->endpoint_status !=
                        CONSUMER_ENDPOINT_ACTIVE) {
                /* An inactive endpoint means we don't have to flush anymore. */
-               ret = 0;
+               done_flushing = true;
        } else {
                /* Still not completely flushed. */
-               ret = 1;
+               done_flushing = false;
        }
 
        pthread_mutex_unlock(&channel->metadata_cache->lock);
        pthread_mutex_unlock(&metadata_stream->lock);
+
 end_unlock_channel:
        pthread_mutex_unlock(&channel->timer_lock);
        if (!timer) {
                pthread_mutex_unlock(&channel->lock);
        }
 
-       return ret;
+       return done_flushing;
+}
+
+/*
+ * Wait until the cache is flushed up to the offset passed in parameter or the
+ * metadata stream has been destroyed.
+ */
+void consumer_wait_metadata_cache_flushed(struct lttng_consumer_channel *channel,
+               uint64_t offset, bool invoked_by_timer)
+{
+       assert(channel);
+       assert(channel->metadata_cache);
+
+       if (consumer_metadata_cache_is_flushed(channel, offset, invoked_by_timer)) {
+               return;
+       }
+
+       /* Metadata cache is not currently flushed, wait on wait queue. */
+       for (;;) {
+               struct lttng_waiter waiter;
+
+               lttng_waiter_init(&waiter);
+               lttng_wait_queue_add(&channel->metadata_pushed_wait_queue, &waiter);
+               if (consumer_metadata_cache_is_flushed(channel, offset, invoked_by_timer)) {
+                       /* Wake up all waiters, ourself included. */
+                       lttng_wait_queue_wake_all(&channel->metadata_pushed_wait_queue);
+                       /* Ensure proper teardown of waiter. */
+                       lttng_waiter_wait(&waiter);
+                       break;
+               }
+
+               lttng_waiter_wait(&waiter);
+       }
 }
This page took 0.026511 seconds and 4 git commands to generate.