consumerd: clean-up: stream attribute accessed without locking stream
[lttng-tools.git] / src / common / consumer / consumer-metadata-cache.c
index 48257c299750aa0953016808766c6a634a17c65e..5eee4014265f42566776ea34645a0f08504fbde0 100644 (file)
@@ -70,6 +70,76 @@ 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;
+}
+
+/*
+ * Check if the metadata cache version changed.
+ * If it did, reset the metadata cache.
+ * The metadata cache lock MUST be held.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+static
+int metadata_cache_check_version(struct consumer_metadata_cache *cache,
+               uint64_t version)
+{
+       int ret = 0;
+
+       if (cache->version == version) {
+               goto end;
+       }
+
+       DBG("Metadata cache version update to %" PRIu64, version);
+       metadata_cache_reset(cache);
+       cache->version = version;
+
+end:
+       return ret;
+}
+
+/*
+ * Write a character on the metadata poll pipe to wake the metadata thread.
+ * Returns 0 on success, -1 on error.
+ */
+int consumer_metadata_wakeup_pipe(const struct lttng_consumer_channel *channel)
+{
+       int ret = 0;
+       const char dummy = 'c';
+
+       if (channel->monitor && channel->metadata_stream) {
+               ssize_t write_ret;
+
+               write_ret = lttng_write(channel->metadata_stream->ust_metadata_poll_pipe[1],
+                               &dummy, 1);
+               if (write_ret < 1) {
+                       if (errno == EWOULDBLOCK) {
+                               /*
+                                * This is fine, the metadata poll thread
+                                * is having a hard time keeping-up, but
+                                * it will eventually wake-up and consume
+                                * the available data.
+                                */
+                               ret = 0;
+                        } else {
+                               PERROR("Wake-up UST metadata pipe");
+                               ret = -1;
+                               goto end;
+                        }
+                }
+       }
+
+end:
+       return ret;
+}
+
 /*
  * Write metadata to the cache, extend the cache if necessary. We support
  * overlapping updates, but they need to be contiguous. Send the
@@ -79,16 +149,22 @@ end:
  * Return 0 on success, a negative value on error.
  */
 int consumer_metadata_cache_write(struct lttng_consumer_channel *channel,
-               unsigned int offset, unsigned int len, char *data)
+               unsigned int offset, unsigned int len, uint64_t version,
+               char *data)
 {
        int ret = 0;
-       int size_ret;
        struct consumer_metadata_cache *cache;
 
        assert(channel);
        assert(channel->metadata_cache);
 
        cache = channel->metadata_cache;
+
+       ret = metadata_cache_check_version(cache, version);
+       if (ret < 0) {
+               goto end;
+       }
+
        DBG("Writing %u bytes from offset %u in metadata cache", len, offset);
 
        if (offset + len > cache->cache_alloc_size) {
@@ -102,18 +178,8 @@ int consumer_metadata_cache_write(struct lttng_consumer_channel *channel,
 
        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;
-                       }
-               }
+               ret = consumer_metadata_wakeup_pipe(channel);
        }
 
 end:
@@ -207,9 +273,9 @@ 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;
+       pthread_mutex_lock(&metadata_stream->lock);
+       pthread_mutex_lock(&channel->metadata_cache->lock);
 
        if (!metadata_stream) {
                /*
@@ -229,6 +295,7 @@ int consumer_metadata_cache_flushed(struct lttng_consumer_channel *channel,
        }
 
        pthread_mutex_unlock(&channel->metadata_cache->lock);
+       pthread_mutex_unlock(&metadata_stream->lock);
        pthread_mutex_unlock(&channel->timer_lock);
        if (!timer) {
                pthread_mutex_unlock(&channel->lock);
This page took 0.038721 seconds and 4 git commands to generate.