Commit | Line | Data |
---|---|---|
331744e3 JD |
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 | ||
6c1c0768 | 19 | #define _LGPL_SOURCE |
331744e3 JD |
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> | |
c8fea79c | 32 | #include <common/consumer/consumer.h> |
331744e3 JD |
33 | |
34 | #include "consumer-metadata-cache.h" | |
35 | ||
fe81e5c9 DG |
36 | extern struct lttng_consumer_global_data consumer_data; |
37 | ||
331744e3 JD |
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; | |
53efb85a | 49 | unsigned int new_size, old_size; |
331744e3 JD |
50 | |
51 | assert(channel); | |
52 | assert(channel->metadata_cache); | |
53 | ||
53efb85a MD |
54 | old_size = channel->metadata_cache->cache_alloc_size; |
55 | new_size = max_t(unsigned int, old_size + size, old_size << 1); | |
331744e3 JD |
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 | } | |
53efb85a MD |
64 | /* Zero newly allocated memory */ |
65 | memset(tmp_data_ptr + old_size, 0, new_size - old_size); | |
331744e3 JD |
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 | ||
93ec662e JD |
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, | |
9dd04ae3 | 92 | uint64_t version) |
93ec662e JD |
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 | ||
5b8eb761 JD |
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(const struct lttng_consumer_channel *channel) | |
113 | { | |
114 | int ret = 0; | |
115 | const char dummy = 'c'; | |
116 | ||
117 | if (channel->monitor && channel->metadata_stream) { | |
118 | ssize_t write_ret; | |
119 | ||
120 | write_ret = lttng_write(channel->metadata_stream->ust_metadata_poll_pipe[1], | |
121 | &dummy, 1); | |
122 | if (write_ret < 1) { | |
d2956687 JG |
123 | if (errno == EWOULDBLOCK) { |
124 | /* | |
125 | * This is fine, the metadata poll thread | |
126 | * is having a hard time keeping-up, but | |
127 | * it will eventually wake-up and consume | |
128 | * the available data. | |
129 | */ | |
130 | ret = 0; | |
131 | } else { | |
132 | PERROR("Wake-up UST metadata pipe"); | |
133 | ret = -1; | |
134 | goto end; | |
135 | } | |
136 | } | |
5b8eb761 JD |
137 | } |
138 | ||
139 | end: | |
140 | return ret; | |
141 | } | |
142 | ||
331744e3 JD |
143 | /* |
144 | * Write metadata to the cache, extend the cache if necessary. We support | |
c585821b MD |
145 | * overlapping updates, but they need to be contiguous. Send the |
146 | * contiguous metadata in cache to the ring buffer. The metadata cache | |
331744e3 JD |
147 | * lock MUST be acquired to write in the cache. |
148 | * | |
149 | * Return 0 on success, a negative value on error. | |
150 | */ | |
151 | int consumer_metadata_cache_write(struct lttng_consumer_channel *channel, | |
93ec662e JD |
152 | unsigned int offset, unsigned int len, uint64_t version, |
153 | char *data) | |
331744e3 JD |
154 | { |
155 | int ret = 0; | |
156 | struct consumer_metadata_cache *cache; | |
157 | ||
158 | assert(channel); | |
159 | assert(channel->metadata_cache); | |
160 | ||
161 | cache = channel->metadata_cache; | |
93ec662e | 162 | |
9dd04ae3 | 163 | ret = metadata_cache_check_version(cache, version); |
93ec662e JD |
164 | if (ret < 0) { |
165 | goto end; | |
166 | } | |
167 | ||
331744e3 JD |
168 | DBG("Writing %u bytes from offset %u in metadata cache", len, offset); |
169 | ||
170 | if (offset + len > cache->cache_alloc_size) { | |
171 | ret = extend_metadata_cache(channel, | |
172 | len - cache->cache_alloc_size + offset); | |
173 | if (ret < 0) { | |
174 | ERR("Extending metadata cache"); | |
175 | goto end; | |
176 | } | |
177 | } | |
178 | ||
179 | memcpy(cache->data + offset, data, len); | |
331744e3 | 180 | if (offset + len > cache->max_offset) { |
c585821b | 181 | cache->max_offset = offset + len; |
5b8eb761 | 182 | ret = consumer_metadata_wakeup_pipe(channel); |
331744e3 JD |
183 | } |
184 | ||
185 | end: | |
186 | return ret; | |
187 | } | |
188 | ||
189 | /* | |
190 | * Create the metadata cache, original allocated size: max_sb_size | |
191 | * | |
192 | * Return 0 on success, a negative value on error. | |
193 | */ | |
194 | int consumer_metadata_cache_allocate(struct lttng_consumer_channel *channel) | |
195 | { | |
196 | int ret; | |
197 | ||
198 | assert(channel); | |
199 | ||
200 | channel->metadata_cache = zmalloc( | |
201 | sizeof(struct consumer_metadata_cache)); | |
202 | if (!channel->metadata_cache) { | |
203 | PERROR("zmalloc metadata cache struct"); | |
204 | ret = -1; | |
205 | goto end; | |
206 | } | |
207 | ret = pthread_mutex_init(&channel->metadata_cache->lock, NULL); | |
208 | if (ret != 0) { | |
209 | PERROR("mutex init"); | |
210 | goto end_free_cache; | |
211 | } | |
212 | ||
213 | channel->metadata_cache->cache_alloc_size = DEFAULT_METADATA_CACHE_SIZE; | |
214 | channel->metadata_cache->data = zmalloc( | |
215 | channel->metadata_cache->cache_alloc_size * sizeof(char)); | |
216 | if (!channel->metadata_cache->data) { | |
217 | PERROR("zmalloc metadata cache data"); | |
218 | ret = -1; | |
219 | goto end_free_mutex; | |
220 | } | |
221 | DBG("Allocated metadata cache of %" PRIu64 " bytes", | |
222 | channel->metadata_cache->cache_alloc_size); | |
223 | ||
224 | ret = 0; | |
225 | goto end; | |
226 | ||
227 | end_free_mutex: | |
228 | pthread_mutex_destroy(&channel->metadata_cache->lock); | |
229 | end_free_cache: | |
230 | free(channel->metadata_cache); | |
231 | end: | |
232 | return ret; | |
233 | } | |
234 | ||
235 | /* | |
236 | * Destroy and free the metadata cache | |
237 | */ | |
238 | void consumer_metadata_cache_destroy(struct lttng_consumer_channel *channel) | |
239 | { | |
240 | if (!channel || !channel->metadata_cache) { | |
241 | return; | |
242 | } | |
243 | ||
244 | DBG("Destroying metadata cache"); | |
245 | ||
331744e3 JD |
246 | pthread_mutex_destroy(&channel->metadata_cache->lock); |
247 | free(channel->metadata_cache->data); | |
248 | free(channel->metadata_cache); | |
249 | } | |
250 | ||
251 | /* | |
252 | * Check if the cache is flushed up to the offset passed in parameter. | |
253 | * | |
254 | * Return 0 if everything has been flushed, 1 if there is data not flushed. | |
255 | */ | |
256 | int consumer_metadata_cache_flushed(struct lttng_consumer_channel *channel, | |
5e41ebe1 | 257 | uint64_t offset, int timer) |
331744e3 | 258 | { |
04ef1097 MD |
259 | int ret = 0; |
260 | struct lttng_consumer_stream *metadata_stream; | |
331744e3 JD |
261 | |
262 | assert(channel); | |
263 | assert(channel->metadata_cache); | |
264 | ||
7f725ec5 | 265 | /* |
5e41ebe1 MD |
266 | * If not called from a timer handler, we have to take the |
267 | * channel lock to be mutually exclusive with channel teardown. | |
268 | * Timer handler does not need to take this lock because it is | |
269 | * already synchronized by timer stop (and, more importantly, | |
270 | * taking this lock in a timer handler would cause a deadlock). | |
7f725ec5 | 271 | */ |
5e41ebe1 MD |
272 | if (!timer) { |
273 | pthread_mutex_lock(&channel->lock); | |
274 | } | |
ec6ea7d0 | 275 | pthread_mutex_lock(&channel->timer_lock); |
04ef1097 | 276 | metadata_stream = channel->metadata_stream; |
04ef1097 | 277 | if (!metadata_stream) { |
fe81e5c9 DG |
278 | /* |
279 | * Having no metadata stream means the channel is being destroyed so there | |
280 | * is no cache to flush anymore. | |
281 | */ | |
282 | ret = 0; | |
e524139e JG |
283 | goto end_unlock_channel; |
284 | } | |
285 | ||
286 | pthread_mutex_lock(&metadata_stream->lock); | |
287 | pthread_mutex_lock(&channel->metadata_cache->lock); | |
288 | ||
289 | if (metadata_stream->ust_metadata_pushed >= offset) { | |
04ef1097 | 290 | ret = 0; |
fe81e5c9 DG |
291 | } else if (channel->metadata_stream->endpoint_status != |
292 | CONSUMER_ENDPOINT_ACTIVE) { | |
293 | /* An inactive endpoint means we don't have to flush anymore. */ | |
294 | ret = 0; | |
331744e3 | 295 | } else { |
fe81e5c9 | 296 | /* Still not completely flushed. */ |
331744e3 JD |
297 | ret = 1; |
298 | } | |
fe81e5c9 | 299 | |
331744e3 | 300 | pthread_mutex_unlock(&channel->metadata_cache->lock); |
8e1ef46e | 301 | pthread_mutex_unlock(&metadata_stream->lock); |
e524139e | 302 | end_unlock_channel: |
ec6ea7d0 | 303 | pthread_mutex_unlock(&channel->timer_lock); |
5e41ebe1 MD |
304 | if (!timer) { |
305 | pthread_mutex_unlock(&channel->lock); | |
306 | } | |
331744e3 JD |
307 | |
308 | return ret; | |
309 | } |