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 lttng_consumer_channel *channel,
- unsigned int size)
-{
- int ret = 0;
- char *tmp_data_ptr;
- unsigned int new_size, old_size;
-
- assert(channel);
- assert(channel->metadata_cache);
-
- old_size = channel->metadata_cache->cache_alloc_size;
- new_size = max_t(unsigned int, old_size + size, old_size << 1);
- DBG("Extending metadata cache to %u", new_size);
- tmp_data_ptr = realloc(channel->metadata_cache->data, new_size);
- if (!tmp_data_ptr) {
- ERR("Reallocating metadata cache");
- free(channel->metadata_cache->data);
- ret = -1;
- goto end;
- }
- /* Zero newly allocated memory */
- memset(tmp_data_ptr + old_size, 0, new_size - old_size);
- channel->metadata_cache->data = tmp_data_ptr;
- channel->metadata_cache->cache_alloc_size = new_size;
-
-end:
- return ret;
-}
+extern struct lttng_consumer_global_data the_consumer_data;
/*
* 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);
}
/*
* various returned status codes.
*/
enum consumer_metadata_cache_write_status
-consumer_metadata_cache_write(struct lttng_consumer_channel *channel,
+consumer_metadata_cache_write(struct consumer_metadata_cache *cache,
unsigned int offset, unsigned int len, uint64_t version,
const char *data)
{
int ret = 0;
- struct consumer_metadata_cache *cache;
enum consumer_metadata_cache_write_status status;
bool cache_is_invalidated = false;
- uint64_t original_max_offset;
+ uint64_t original_size;
- assert(channel);
- assert(channel->metadata_cache);
-
- cache = channel->metadata_cache;
+ 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(channel,
- 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);
}
/*
* 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.
* 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;
}
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);
+ }
}