Common code to wakeup the metadata pipe
[deliverable/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 extern struct lttng_consumer_global_data consumer_data;
37
38 /*
39 * Extend the allocated size of the metadata cache. Called only from
40 * lttng_ustconsumer_write_metadata_cache.
41 *
42 * Return 0 on success, a negative value on error.
43 */
44 static int extend_metadata_cache(struct lttng_consumer_channel *channel,
45 unsigned int size)
46 {
47 int ret = 0;
48 char *tmp_data_ptr;
49 unsigned int new_size, old_size;
50
51 assert(channel);
52 assert(channel->metadata_cache);
53
54 old_size = channel->metadata_cache->cache_alloc_size;
55 new_size = max_t(unsigned int, old_size + size, old_size << 1);
56 DBG("Extending metadata cache to %u", new_size);
57 tmp_data_ptr = realloc(channel->metadata_cache->data, new_size);
58 if (!tmp_data_ptr) {
59 ERR("Reallocating metadata cache");
60 free(channel->metadata_cache->data);
61 ret = -1;
62 goto end;
63 }
64 /* Zero newly allocated memory */
65 memset(tmp_data_ptr + old_size, 0, new_size - old_size);
66 channel->metadata_cache->data = tmp_data_ptr;
67 channel->metadata_cache->cache_alloc_size = new_size;
68
69 end:
70 return ret;
71 }
72
73 /*
74 * Reset the metadata cache.
75 */
76 static
77 void metadata_cache_reset(struct consumer_metadata_cache *cache)
78 {
79 memset(cache->data, 0, cache->cache_alloc_size);
80 cache->max_offset = 0;
81 }
82
83 /*
84 * Check if the metadata cache version changed.
85 * If it did, reset the metadata cache.
86 * The metadata cache lock MUST be held.
87 *
88 * Returns 0 on success, a negative value on error.
89 */
90 static
91 int metadata_cache_check_version(struct consumer_metadata_cache *cache,
92 struct lttng_consumer_channel *channel, uint64_t version)
93 {
94 int ret = 0;
95
96 if (cache->version == version) {
97 goto end;
98 }
99
100 DBG("Metadata cache version update to %" PRIu64, version);
101 metadata_cache_reset(cache);
102 cache->version = version;
103
104 end:
105 return ret;
106 }
107
108 /*
109 * Write a character on the metadata poll pipe to wake the metadata thread.
110 * Returns 0 on success, -1 on error.
111 */
112 int consumer_metadata_wakeup_pipe(struct lttng_consumer_channel *channel)
113 {
114 char dummy = 'c';
115 int ret = 0;
116 int size_ret;
117
118 if (channel->monitor && channel->metadata_stream) {
119 size_ret = lttng_write(channel->metadata_stream->ust_metadata_poll_pipe[1],
120 &dummy, 1);
121 if (size_ret < 1) {
122 ERR("Wakeup UST metadata pipe");
123 ret = -1;
124 goto end;
125 }
126 }
127
128 end:
129 return ret;
130 }
131
132 /*
133 * Write metadata to the cache, extend the cache if necessary. We support
134 * overlapping updates, but they need to be contiguous. Send the
135 * contiguous metadata in cache to the ring buffer. The metadata cache
136 * lock MUST be acquired to write in the cache.
137 *
138 * Return 0 on success, a negative value on error.
139 */
140 int consumer_metadata_cache_write(struct lttng_consumer_channel *channel,
141 unsigned int offset, unsigned int len, uint64_t version,
142 char *data)
143 {
144 int ret = 0;
145 struct consumer_metadata_cache *cache;
146
147 assert(channel);
148 assert(channel->metadata_cache);
149
150 cache = channel->metadata_cache;
151
152 ret = metadata_cache_check_version(cache, channel, version);
153 if (ret < 0) {
154 goto end;
155 }
156
157 DBG("Writing %u bytes from offset %u in metadata cache", len, offset);
158
159 if (offset + len > cache->cache_alloc_size) {
160 ret = extend_metadata_cache(channel,
161 len - cache->cache_alloc_size + offset);
162 if (ret < 0) {
163 ERR("Extending metadata cache");
164 goto end;
165 }
166 }
167
168 memcpy(cache->data + offset, data, len);
169 if (offset + len > cache->max_offset) {
170 cache->max_offset = offset + len;
171 ret = consumer_metadata_wakeup_pipe(channel);
172 }
173
174 end:
175 return ret;
176 }
177
178 /*
179 * Create the metadata cache, original allocated size: max_sb_size
180 *
181 * Return 0 on success, a negative value on error.
182 */
183 int consumer_metadata_cache_allocate(struct lttng_consumer_channel *channel)
184 {
185 int ret;
186
187 assert(channel);
188
189 channel->metadata_cache = zmalloc(
190 sizeof(struct consumer_metadata_cache));
191 if (!channel->metadata_cache) {
192 PERROR("zmalloc metadata cache struct");
193 ret = -1;
194 goto end;
195 }
196 ret = pthread_mutex_init(&channel->metadata_cache->lock, NULL);
197 if (ret != 0) {
198 PERROR("mutex init");
199 goto end_free_cache;
200 }
201
202 channel->metadata_cache->cache_alloc_size = DEFAULT_METADATA_CACHE_SIZE;
203 channel->metadata_cache->data = zmalloc(
204 channel->metadata_cache->cache_alloc_size * sizeof(char));
205 if (!channel->metadata_cache->data) {
206 PERROR("zmalloc metadata cache data");
207 ret = -1;
208 goto end_free_mutex;
209 }
210 DBG("Allocated metadata cache of %" PRIu64 " bytes",
211 channel->metadata_cache->cache_alloc_size);
212
213 ret = 0;
214 goto end;
215
216 end_free_mutex:
217 pthread_mutex_destroy(&channel->metadata_cache->lock);
218 end_free_cache:
219 free(channel->metadata_cache);
220 end:
221 return ret;
222 }
223
224 /*
225 * Destroy and free the metadata cache
226 */
227 void consumer_metadata_cache_destroy(struct lttng_consumer_channel *channel)
228 {
229 if (!channel || !channel->metadata_cache) {
230 return;
231 }
232
233 DBG("Destroying metadata cache");
234
235 pthread_mutex_destroy(&channel->metadata_cache->lock);
236 free(channel->metadata_cache->data);
237 free(channel->metadata_cache);
238 }
239
240 /*
241 * Check if the cache is flushed up to the offset passed in parameter.
242 *
243 * Return 0 if everything has been flushed, 1 if there is data not flushed.
244 */
245 int consumer_metadata_cache_flushed(struct lttng_consumer_channel *channel,
246 uint64_t offset, int timer)
247 {
248 int ret = 0;
249 struct lttng_consumer_stream *metadata_stream;
250
251 assert(channel);
252 assert(channel->metadata_cache);
253
254 /*
255 * If not called from a timer handler, we have to take the
256 * channel lock to be mutually exclusive with channel teardown.
257 * Timer handler does not need to take this lock because it is
258 * already synchronized by timer stop (and, more importantly,
259 * taking this lock in a timer handler would cause a deadlock).
260 */
261 if (!timer) {
262 pthread_mutex_lock(&channel->lock);
263 }
264 pthread_mutex_lock(&channel->timer_lock);
265 pthread_mutex_lock(&channel->metadata_cache->lock);
266
267 metadata_stream = channel->metadata_stream;
268
269 if (!metadata_stream) {
270 /*
271 * Having no metadata stream means the channel is being destroyed so there
272 * is no cache to flush anymore.
273 */
274 ret = 0;
275 } else if (metadata_stream->ust_metadata_pushed >= offset) {
276 ret = 0;
277 } else if (channel->metadata_stream->endpoint_status !=
278 CONSUMER_ENDPOINT_ACTIVE) {
279 /* An inactive endpoint means we don't have to flush anymore. */
280 ret = 0;
281 } else {
282 /* Still not completely flushed. */
283 ret = 1;
284 }
285
286 pthread_mutex_unlock(&channel->metadata_cache->lock);
287 pthread_mutex_unlock(&channel->timer_lock);
288 if (!timer) {
289 pthread_mutex_unlock(&channel->lock);
290 }
291
292 return ret;
293 }
This page took 0.035925 seconds and 5 git commands to generate.