X-Git-Url: http://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Fconsumer%2Fconsumer-metadata-cache.c;h=976c83e54fd5dc6904cb6e72f21ea4af81426d9f;hp=48257c299750aa0953016808766c6a634a17c65e;hb=9eac98281fd4923ea52b94abffd0cf8cfa27588e;hpb=c8fea79c745d42ea8143b7020ae11b4fc2da0d8a diff --git a/src/common/consumer/consumer-metadata-cache.c b/src/common/consumer/consumer-metadata-cache.c index 48257c299..976c83e54 100644 --- a/src/common/consumer/consumer-metadata-cache.c +++ b/src/common/consumer/consumer-metadata-cache.c @@ -1,19 +1,9 @@ /* - * Copyright (C) 2013 - Julien Desfossez - * David Goulet + * Copyright (C) 2013 Julien Desfossez + * Copyright (C) 2013 David Goulet * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License, version 2 only, - * as published by the Free Software Foundation. + * SPDX-License-Identifier: GPL-2.0-only * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #define _LGPL_SOURCE @@ -33,41 +23,45 @@ #include "consumer-metadata-cache.h" +enum metadata_cache_update_version_status { + METADATA_CACHE_UPDATE_STATUS_VERSION_UPDATED, + 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. + * Reset the metadata cache. */ -static int extend_metadata_cache(struct lttng_consumer_channel *channel, - unsigned int size) +static +void metadata_cache_reset(struct consumer_metadata_cache *cache) { - int ret = 0; - char *tmp_data_ptr; - unsigned int new_size, old_size; + const int ret = lttng_dynamic_buffer_set_size(&cache->contents, 0); - assert(channel); - assert(channel->metadata_cache); + assert(ret == 0); +} - 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; +/* + * Check if the metadata cache version changed. + * If it did, reset the metadata cache. + * The metadata cache lock MUST be held. + */ +static enum metadata_cache_update_version_status metadata_cache_update_version( + struct consumer_metadata_cache *cache, uint64_t version) +{ + enum metadata_cache_update_version_status status; + + if (cache->version == version) { + status = METADATA_CACHE_UPDATE_STATUS_VERSION_NOT_UPDATED; 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; + + DBG("Metadata cache version update to %" PRIu64, version); + cache->version = version; + status = METADATA_CACHE_UPDATE_STATUS_VERSION_UPDATED; end: - return ret; + return status; } /* @@ -76,48 +70,53 @@ end: * contiguous metadata in cache to the ring buffer. The metadata cache * lock MUST be acquired to write in the cache. * - * Return 0 on success, a negative value on error. + * See `enum consumer_metadata_cache_write_status` for the meaning of the + * various returned status codes. */ -int consumer_metadata_cache_write(struct lttng_consumer_channel *channel, - unsigned int offset, unsigned int len, char *data) +enum consumer_metadata_cache_write_status +consumer_metadata_cache_write(struct consumer_metadata_cache *cache, + unsigned int offset, unsigned int len, uint64_t version, + const char *data) { int ret = 0; - int size_ret; - struct consumer_metadata_cache *cache; - - assert(channel); - assert(channel->metadata_cache); + enum consumer_metadata_cache_write_status status; + bool cache_is_invalidated = false; + uint64_t original_size; + + assert(cache); + ASSERT_LOCKED(cache->lock); + original_size = cache->contents.size; + + if (metadata_cache_update_version(cache, version) == + METADATA_CACHE_UPDATE_STATUS_VERSION_UPDATED) { + metadata_cache_reset(cache); + cache_is_invalidated = true; + } - cache = channel->metadata_cache; 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); - if (offset + len > cache->max_offset) { - char dummy = 'c'; - - cache->max_offset = offset + len; - if (channel->monitor) { - size_ret = lttng_write(channel->metadata_stream->ust_metadata_poll_pipe[1], - &dummy, 1); - if (size_ret < 1) { - ERR("Wakeup UST metadata pipe"); - ret = -1; - goto end; - } - } + memcpy(cache->contents.data + offset, data, len); + + if (cache_is_invalidated) { + status = CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED; + } 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->contents.size == original_size); } end: - return ret; + return status; } /* @@ -144,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; @@ -178,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); } @@ -207,17 +210,20 @@ int consumer_metadata_cache_flushed(struct lttng_consumer_channel *channel, pthread_mutex_lock(&channel->lock); } pthread_mutex_lock(&channel->timer_lock); - pthread_mutex_lock(&channel->metadata_cache->lock); - metadata_stream = channel->metadata_stream; - if (!metadata_stream) { /* * Having no metadata stream means the channel is being destroyed so there * is no cache to flush anymore. */ ret = 0; - } else if (metadata_stream->ust_metadata_pushed >= offset) { + goto end_unlock_channel; + } + + pthread_mutex_lock(&metadata_stream->lock); + pthread_mutex_lock(&channel->metadata_cache->lock); + + if (metadata_stream->ust_metadata_pushed >= offset) { ret = 0; } else if (channel->metadata_stream->endpoint_status != CONSUMER_ENDPOINT_ACTIVE) { @@ -229,6 +235,8 @@ int consumer_metadata_cache_flushed(struct lttng_consumer_channel *channel, } 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);