Fix: sessiond: kernel: leak of notifier rule on failure to init notifier
[lttng-tools.git] / src / common / consumer / consumer-metadata-cache.c
CommitLineData
331744e3 1/*
ab5be9fa
MJ
2 * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com>
3 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
331744e3 4 *
ab5be9fa 5 * SPDX-License-Identifier: GPL-2.0-only
331744e3 6 *
331744e3
JD
7 */
8
6c1c0768 9#define _LGPL_SOURCE
331744e3
JD
10#include <assert.h>
11#include <pthread.h>
12#include <stdlib.h>
13#include <string.h>
14#include <sys/types.h>
15#include <unistd.h>
16#include <inttypes.h>
17
18#include <common/common.h>
19#include <common/utils.h>
20#include <common/sessiond-comm/sessiond-comm.h>
21#include <common/ust-consumer/ust-consumer.h>
c8fea79c 22#include <common/consumer/consumer.h>
331744e3
JD
23
24#include "consumer-metadata-cache.h"
25
b1316da1
JG
26enum metadata_cache_update_version_status {
27 METADATA_CACHE_UPDATE_STATUS_VERSION_UPDATED,
28 METADATA_CACHE_UPDATE_STATUS_VERSION_NOT_UPDATED,
29};
30
fe81e5c9
DG
31extern struct lttng_consumer_global_data consumer_data;
32
93ec662e
JD
33/*
34 * Reset the metadata cache.
35 */
36static
37void metadata_cache_reset(struct consumer_metadata_cache *cache)
38{
9eac9828
JG
39 const int ret = lttng_dynamic_buffer_set_size(&cache->contents, 0);
40
41 assert(ret == 0);
93ec662e
JD
42}
43
44/*
45 * Check if the metadata cache version changed.
46 * If it did, reset the metadata cache.
47 * The metadata cache lock MUST be held.
93ec662e 48 */
b1316da1
JG
49static enum metadata_cache_update_version_status metadata_cache_update_version(
50 struct consumer_metadata_cache *cache, uint64_t version)
93ec662e 51{
b1316da1 52 enum metadata_cache_update_version_status status;
93ec662e
JD
53
54 if (cache->version == version) {
b1316da1 55 status = METADATA_CACHE_UPDATE_STATUS_VERSION_NOT_UPDATED;
93ec662e
JD
56 goto end;
57 }
58
59 DBG("Metadata cache version update to %" PRIu64, version);
93ec662e 60 cache->version = version;
b1316da1 61 status = METADATA_CACHE_UPDATE_STATUS_VERSION_UPDATED;
93ec662e
JD
62
63end:
b1316da1 64 return status;
5b8eb761
JD
65}
66
331744e3
JD
67/*
68 * Write metadata to the cache, extend the cache if necessary. We support
c585821b
MD
69 * overlapping updates, but they need to be contiguous. Send the
70 * contiguous metadata in cache to the ring buffer. The metadata cache
331744e3
JD
71 * lock MUST be acquired to write in the cache.
72 *
b1316da1
JG
73 * See `enum consumer_metadata_cache_write_status` for the meaning of the
74 * various returned status codes.
331744e3 75 */
b1316da1 76enum consumer_metadata_cache_write_status
a25d34bc 77consumer_metadata_cache_write(struct consumer_metadata_cache *cache,
93ec662e 78 unsigned int offset, unsigned int len, uint64_t version,
d44f9a18 79 const char *data)
331744e3
JD
80{
81 int ret = 0;
b1316da1
JG
82 enum consumer_metadata_cache_write_status status;
83 bool cache_is_invalidated = false;
9eac9828 84 uint64_t original_size;
331744e3 85
a25d34bc 86 assert(cache);
b1316da1 87 ASSERT_LOCKED(cache->lock);
9eac9828 88 original_size = cache->contents.size;
93ec662e 89
b1316da1
JG
90 if (metadata_cache_update_version(cache, version) ==
91 METADATA_CACHE_UPDATE_STATUS_VERSION_UPDATED) {
92 metadata_cache_reset(cache);
93 cache_is_invalidated = true;
93ec662e
JD
94 }
95
331744e3 96 DBG("Writing %u bytes from offset %u in metadata cache", len, offset);
9eac9828
JG
97 if (offset + len > cache->contents.size) {
98 ret = lttng_dynamic_buffer_set_size(
99 &cache->contents, offset + len);
100 if (ret) {
331744e3 101 ERR("Extending metadata cache");
b1316da1 102 status = CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR;
331744e3
JD
103 goto end;
104 }
105 }
106
9eac9828 107 memcpy(cache->contents.data + offset, data, len);
b1316da1
JG
108
109 if (cache_is_invalidated) {
110 status = CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED;
9eac9828 111 } else if (cache->contents.size > original_size) {
b1316da1
JG
112 status = CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT;
113 } else {
114 status = CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE;
9eac9828 115 assert(cache->contents.size == original_size);
331744e3
JD
116 }
117
118end:
b1316da1 119 return status;
331744e3
JD
120}
121
122/*
123 * Create the metadata cache, original allocated size: max_sb_size
124 *
125 * Return 0 on success, a negative value on error.
126 */
127int consumer_metadata_cache_allocate(struct lttng_consumer_channel *channel)
128{
129 int ret;
130
131 assert(channel);
132
133 channel->metadata_cache = zmalloc(
134 sizeof(struct consumer_metadata_cache));
135 if (!channel->metadata_cache) {
136 PERROR("zmalloc metadata cache struct");
137 ret = -1;
138 goto end;
139 }
140 ret = pthread_mutex_init(&channel->metadata_cache->lock, NULL);
141 if (ret != 0) {
142 PERROR("mutex init");
143 goto end_free_cache;
144 }
145
9eac9828
JG
146 lttng_dynamic_buffer_init(&channel->metadata_cache->contents);
147 ret = lttng_dynamic_buffer_set_capacity(
148 &channel->metadata_cache->contents,
149 DEFAULT_METADATA_CACHE_SIZE);
150 if (ret) {
151 PERROR("Failed to pre-allocate metadata cache storage of %d bytes on creation",
152 DEFAULT_METADATA_CACHE_SIZE);
331744e3
JD
153 ret = -1;
154 goto end_free_mutex;
155 }
9eac9828
JG
156
157 DBG("Allocated metadata cache: current capacity = %zu",
158 lttng_dynamic_buffer_get_capacity_left(
159 &channel->metadata_cache->contents));
331744e3
JD
160
161 ret = 0;
162 goto end;
163
164end_free_mutex:
165 pthread_mutex_destroy(&channel->metadata_cache->lock);
166end_free_cache:
167 free(channel->metadata_cache);
168end:
169 return ret;
170}
171
172/*
173 * Destroy and free the metadata cache
174 */
175void consumer_metadata_cache_destroy(struct lttng_consumer_channel *channel)
176{
177 if (!channel || !channel->metadata_cache) {
178 return;
179 }
180
181 DBG("Destroying metadata cache");
182
331744e3 183 pthread_mutex_destroy(&channel->metadata_cache->lock);
9eac9828 184 lttng_dynamic_buffer_reset(&channel->metadata_cache->contents);
331744e3
JD
185 free(channel->metadata_cache);
186}
187
188/*
189 * Check if the cache is flushed up to the offset passed in parameter.
190 *
191 * Return 0 if everything has been flushed, 1 if there is data not flushed.
192 */
193int consumer_metadata_cache_flushed(struct lttng_consumer_channel *channel,
5e41ebe1 194 uint64_t offset, int timer)
331744e3 195{
04ef1097
MD
196 int ret = 0;
197 struct lttng_consumer_stream *metadata_stream;
331744e3
JD
198
199 assert(channel);
200 assert(channel->metadata_cache);
201
7f725ec5 202 /*
5e41ebe1
MD
203 * If not called from a timer handler, we have to take the
204 * channel lock to be mutually exclusive with channel teardown.
205 * Timer handler does not need to take this lock because it is
206 * already synchronized by timer stop (and, more importantly,
207 * taking this lock in a timer handler would cause a deadlock).
7f725ec5 208 */
5e41ebe1
MD
209 if (!timer) {
210 pthread_mutex_lock(&channel->lock);
211 }
ec6ea7d0 212 pthread_mutex_lock(&channel->timer_lock);
04ef1097 213 metadata_stream = channel->metadata_stream;
04ef1097 214 if (!metadata_stream) {
fe81e5c9
DG
215 /*
216 * Having no metadata stream means the channel is being destroyed so there
217 * is no cache to flush anymore.
218 */
219 ret = 0;
e524139e
JG
220 goto end_unlock_channel;
221 }
222
223 pthread_mutex_lock(&metadata_stream->lock);
224 pthread_mutex_lock(&channel->metadata_cache->lock);
225
226 if (metadata_stream->ust_metadata_pushed >= offset) {
04ef1097 227 ret = 0;
fe81e5c9
DG
228 } else if (channel->metadata_stream->endpoint_status !=
229 CONSUMER_ENDPOINT_ACTIVE) {
230 /* An inactive endpoint means we don't have to flush anymore. */
231 ret = 0;
331744e3 232 } else {
fe81e5c9 233 /* Still not completely flushed. */
331744e3
JD
234 ret = 1;
235 }
fe81e5c9 236
331744e3 237 pthread_mutex_unlock(&channel->metadata_cache->lock);
8e1ef46e 238 pthread_mutex_unlock(&metadata_stream->lock);
e524139e 239end_unlock_channel:
ec6ea7d0 240 pthread_mutex_unlock(&channel->timer_lock);
5e41ebe1
MD
241 if (!timer) {
242 pthread_mutex_unlock(&channel->lock);
243 }
331744e3
JD
244
245 return ret;
246}
This page took 0.080658 seconds and 4 git commands to generate.