Fix: ust-consumer: metadata thread not woken-up after version change
[lttng-tools.git] / src / common / consumer / consumer-metadata-cache.c
1 /*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _LGPL_SOURCE
20 #include <assert.h>
21 #include <pthread.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include <inttypes.h>
27
28 #include <common/common.h>
29 #include <common/utils.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
31 #include <common/ust-consumer/ust-consumer.h>
32 #include <common/consumer/consumer.h>
33
34 #include "consumer-metadata-cache.h"
35
36 enum metadata_cache_update_version_status {
37 METADATA_CACHE_UPDATE_STATUS_VERSION_UPDATED,
38 METADATA_CACHE_UPDATE_STATUS_VERSION_NOT_UPDATED,
39 };
40
41 extern struct lttng_consumer_global_data consumer_data;
42
43 /*
44 * Extend the allocated size of the metadata cache. Called only from
45 * lttng_ustconsumer_write_metadata_cache.
46 *
47 * Return 0 on success, a negative value on error.
48 */
49 static int extend_metadata_cache(struct lttng_consumer_channel *channel,
50 unsigned int size)
51 {
52 int ret = 0;
53 char *tmp_data_ptr;
54 unsigned int new_size, old_size;
55
56 assert(channel);
57 assert(channel->metadata_cache);
58
59 old_size = channel->metadata_cache->cache_alloc_size;
60 new_size = max_t(unsigned int, old_size + size, old_size << 1);
61 DBG("Extending metadata cache to %u", new_size);
62 tmp_data_ptr = realloc(channel->metadata_cache->data, new_size);
63 if (!tmp_data_ptr) {
64 ERR("Reallocating metadata cache");
65 free(channel->metadata_cache->data);
66 ret = -1;
67 goto end;
68 }
69 /* Zero newly allocated memory */
70 memset(tmp_data_ptr + old_size, 0, new_size - old_size);
71 channel->metadata_cache->data = tmp_data_ptr;
72 channel->metadata_cache->cache_alloc_size = new_size;
73
74 end:
75 return ret;
76 }
77
78 /*
79 * Reset the metadata cache.
80 */
81 static
82 void metadata_cache_reset(struct consumer_metadata_cache *cache)
83 {
84 memset(cache->data, 0, cache->cache_alloc_size);
85 cache->max_offset = 0;
86 }
87
88 /*
89 * Check if the metadata cache version changed.
90 * If it did, reset the metadata cache.
91 * The metadata cache lock MUST be held.
92 */
93 static enum metadata_cache_update_version_status metadata_cache_update_version(
94 struct consumer_metadata_cache *cache, uint64_t version)
95 {
96 enum metadata_cache_update_version_status status;
97
98 if (cache->version == version) {
99 status = METADATA_CACHE_UPDATE_STATUS_VERSION_NOT_UPDATED;
100 goto end;
101 }
102
103 DBG("Metadata cache version update to %" PRIu64, version);
104 cache->version = version;
105 status = METADATA_CACHE_UPDATE_STATUS_VERSION_UPDATED;
106
107 end:
108 return status;
109 }
110
111 /*
112 * Write metadata to the cache, extend the cache if necessary. We support
113 * overlapping updates, but they need to be contiguous. Send the
114 * contiguous metadata in cache to the ring buffer. The metadata cache
115 * lock MUST be acquired to write in the cache.
116 *
117 * See `enum consumer_metadata_cache_write_status` for the meaning of the
118 * various returned status codes.
119 */
120 enum consumer_metadata_cache_write_status
121 consumer_metadata_cache_write(struct lttng_consumer_channel *channel,
122 unsigned int offset, unsigned int len, uint64_t version,
123 char *data)
124 {
125 int ret = 0;
126 struct consumer_metadata_cache *cache;
127 enum consumer_metadata_cache_write_status status;
128 bool cache_is_invalidated = false;
129 uint64_t original_max_offset;
130
131 assert(channel);
132 assert(channel->metadata_cache);
133
134 cache = channel->metadata_cache;
135 ASSERT_LOCKED(cache->lock);
136 original_max_offset = cache->max_offset;
137
138 if (metadata_cache_update_version(cache, version) ==
139 METADATA_CACHE_UPDATE_STATUS_VERSION_UPDATED) {
140 metadata_cache_reset(cache);
141 cache_is_invalidated = true;
142 }
143
144 DBG("Writing %u bytes from offset %u in metadata cache", len, offset);
145
146 if (offset + len > cache->cache_alloc_size) {
147 ret = extend_metadata_cache(channel,
148 len - cache->cache_alloc_size + offset);
149 if (ret < 0) {
150 ERR("Extending metadata cache");
151 status = CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR;
152 goto end;
153 }
154 }
155
156 memcpy(cache->data + offset, data, len);
157 cache->max_offset = max(cache->max_offset, offset + len);
158
159 if (cache_is_invalidated) {
160 status = CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED;
161 } else if (cache->max_offset > original_max_offset) {
162 status = CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT;
163 } else {
164 status = CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE;
165 assert(cache->max_offset == original_max_offset);
166 }
167
168 end:
169 return status;
170 }
171
172 /*
173 * Create the metadata cache, original allocated size: max_sb_size
174 *
175 * Return 0 on success, a negative value on error.
176 */
177 int consumer_metadata_cache_allocate(struct lttng_consumer_channel *channel)
178 {
179 int ret;
180
181 assert(channel);
182
183 channel->metadata_cache = zmalloc(
184 sizeof(struct consumer_metadata_cache));
185 if (!channel->metadata_cache) {
186 PERROR("zmalloc metadata cache struct");
187 ret = -1;
188 goto end;
189 }
190 ret = pthread_mutex_init(&channel->metadata_cache->lock, NULL);
191 if (ret != 0) {
192 PERROR("mutex init");
193 goto end_free_cache;
194 }
195
196 channel->metadata_cache->cache_alloc_size = DEFAULT_METADATA_CACHE_SIZE;
197 channel->metadata_cache->data = zmalloc(
198 channel->metadata_cache->cache_alloc_size * sizeof(char));
199 if (!channel->metadata_cache->data) {
200 PERROR("zmalloc metadata cache data");
201 ret = -1;
202 goto end_free_mutex;
203 }
204 DBG("Allocated metadata cache of %" PRIu64 " bytes",
205 channel->metadata_cache->cache_alloc_size);
206
207 ret = 0;
208 goto end;
209
210 end_free_mutex:
211 pthread_mutex_destroy(&channel->metadata_cache->lock);
212 end_free_cache:
213 free(channel->metadata_cache);
214 end:
215 return ret;
216 }
217
218 /*
219 * Destroy and free the metadata cache
220 */
221 void consumer_metadata_cache_destroy(struct lttng_consumer_channel *channel)
222 {
223 if (!channel || !channel->metadata_cache) {
224 return;
225 }
226
227 DBG("Destroying metadata cache");
228
229 pthread_mutex_destroy(&channel->metadata_cache->lock);
230 free(channel->metadata_cache->data);
231 free(channel->metadata_cache);
232 }
233
234 /*
235 * Check if the cache is flushed up to the offset passed in parameter.
236 *
237 * Return 0 if everything has been flushed, 1 if there is data not flushed.
238 */
239 int consumer_metadata_cache_flushed(struct lttng_consumer_channel *channel,
240 uint64_t offset, int timer)
241 {
242 int ret = 0;
243 struct lttng_consumer_stream *metadata_stream;
244
245 assert(channel);
246 assert(channel->metadata_cache);
247
248 /*
249 * If not called from a timer handler, we have to take the
250 * channel lock to be mutually exclusive with channel teardown.
251 * Timer handler does not need to take this lock because it is
252 * already synchronized by timer stop (and, more importantly,
253 * taking this lock in a timer handler would cause a deadlock).
254 */
255 if (!timer) {
256 pthread_mutex_lock(&channel->lock);
257 }
258 pthread_mutex_lock(&channel->timer_lock);
259 metadata_stream = channel->metadata_stream;
260 if (!metadata_stream) {
261 /*
262 * Having no metadata stream means the channel is being destroyed so there
263 * is no cache to flush anymore.
264 */
265 ret = 0;
266 goto end_unlock_channel;
267 }
268
269 pthread_mutex_lock(&metadata_stream->lock);
270 pthread_mutex_lock(&channel->metadata_cache->lock);
271
272 if (metadata_stream->ust_metadata_pushed >= offset) {
273 ret = 0;
274 } else if (channel->metadata_stream->endpoint_status !=
275 CONSUMER_ENDPOINT_ACTIVE) {
276 /* An inactive endpoint means we don't have to flush anymore. */
277 ret = 0;
278 } else {
279 /* Still not completely flushed. */
280 ret = 1;
281 }
282
283 pthread_mutex_unlock(&channel->metadata_cache->lock);
284 pthread_mutex_unlock(&metadata_stream->lock);
285 end_unlock_channel:
286 pthread_mutex_unlock(&channel->timer_lock);
287 if (!timer) {
288 pthread_mutex_unlock(&channel->lock);
289 }
290
291 return ret;
292 }
This page took 0.041883 seconds and 4 git commands to generate.