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);
}
/*
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) {
}
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:
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;
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);
}
#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,
};
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).
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:
}
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");
}
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;
}
} 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);
/* 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;