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 | ||
c124970b JG |
36 | enum metadata_cache_update_version_status { |
37 | METADATA_CACHE_UPDATE_STATUS_VERSION_UPDATED, | |
38 | METADATA_CACHE_UPDATE_STATUS_VERSION_NOT_UPDATED, | |
39 | }; | |
40 | ||
fe81e5c9 DG |
41 | extern struct lttng_consumer_global_data consumer_data; |
42 | ||
331744e3 JD |
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; | |
53efb85a | 54 | unsigned int new_size, old_size; |
331744e3 JD |
55 | |
56 | assert(channel); | |
57 | assert(channel->metadata_cache); | |
58 | ||
53efb85a MD |
59 | old_size = channel->metadata_cache->cache_alloc_size; |
60 | new_size = max_t(unsigned int, old_size + size, old_size << 1); | |
331744e3 JD |
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 | } | |
53efb85a MD |
69 | /* Zero newly allocated memory */ |
70 | memset(tmp_data_ptr + old_size, 0, new_size - old_size); | |
331744e3 JD |
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 | ||
93ec662e JD |
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. | |
93ec662e | 92 | */ |
c124970b JG |
93 | static enum metadata_cache_update_version_status metadata_cache_update_version( |
94 | struct consumer_metadata_cache *cache, uint64_t version) | |
93ec662e | 95 | { |
c124970b | 96 | enum metadata_cache_update_version_status status; |
93ec662e JD |
97 | |
98 | if (cache->version == version) { | |
c124970b | 99 | status = METADATA_CACHE_UPDATE_STATUS_VERSION_NOT_UPDATED; |
93ec662e JD |
100 | goto end; |
101 | } | |
102 | ||
103 | DBG("Metadata cache version update to %" PRIu64, version); | |
93ec662e | 104 | cache->version = version; |
c124970b | 105 | status = METADATA_CACHE_UPDATE_STATUS_VERSION_UPDATED; |
93ec662e JD |
106 | |
107 | end: | |
c124970b | 108 | return status; |
5b8eb761 JD |
109 | } |
110 | ||
331744e3 JD |
111 | /* |
112 | * Write metadata to the cache, extend the cache if necessary. We support | |
c585821b MD |
113 | * overlapping updates, but they need to be contiguous. Send the |
114 | * contiguous metadata in cache to the ring buffer. The metadata cache | |
331744e3 JD |
115 | * lock MUST be acquired to write in the cache. |
116 | * | |
c124970b JG |
117 | * See `enum consumer_metadata_cache_write_status` for the meaning of the |
118 | * various returned status codes. | |
331744e3 | 119 | */ |
c124970b JG |
120 | enum consumer_metadata_cache_write_status |
121 | consumer_metadata_cache_write(struct lttng_consumer_channel *channel, | |
93ec662e JD |
122 | unsigned int offset, unsigned int len, uint64_t version, |
123 | char *data) | |
331744e3 JD |
124 | { |
125 | int ret = 0; | |
126 | struct consumer_metadata_cache *cache; | |
c124970b JG |
127 | enum consumer_metadata_cache_write_status status; |
128 | bool cache_is_invalidated = false; | |
129 | uint64_t original_max_offset; | |
331744e3 JD |
130 | |
131 | assert(channel); | |
132 | assert(channel->metadata_cache); | |
133 | ||
134 | cache = channel->metadata_cache; | |
c124970b JG |
135 | ASSERT_LOCKED(cache->lock); |
136 | original_max_offset = cache->max_offset; | |
93ec662e | 137 | |
c124970b JG |
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; | |
93ec662e JD |
142 | } |
143 | ||
331744e3 JD |
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"); | |
c124970b | 151 | status = CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR; |
331744e3 JD |
152 | goto end; |
153 | } | |
154 | } | |
155 | ||
156 | memcpy(cache->data + offset, data, len); | |
c124970b JG |
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); | |
331744e3 JD |
166 | } |
167 | ||
168 | end: | |
c124970b | 169 | return status; |
331744e3 JD |
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 | ||
331744e3 JD |
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, | |
5e41ebe1 | 240 | uint64_t offset, int timer) |
331744e3 | 241 | { |
04ef1097 MD |
242 | int ret = 0; |
243 | struct lttng_consumer_stream *metadata_stream; | |
331744e3 JD |
244 | |
245 | assert(channel); | |
246 | assert(channel->metadata_cache); | |
247 | ||
7f725ec5 | 248 | /* |
5e41ebe1 MD |
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). | |
7f725ec5 | 254 | */ |
5e41ebe1 MD |
255 | if (!timer) { |
256 | pthread_mutex_lock(&channel->lock); | |
257 | } | |
ec6ea7d0 | 258 | pthread_mutex_lock(&channel->timer_lock); |
04ef1097 | 259 | metadata_stream = channel->metadata_stream; |
04ef1097 | 260 | if (!metadata_stream) { |
fe81e5c9 DG |
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; | |
c9b95901 JG |
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) { | |
04ef1097 | 273 | ret = 0; |
fe81e5c9 DG |
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; | |
331744e3 | 278 | } else { |
fe81e5c9 | 279 | /* Still not completely flushed. */ |
331744e3 JD |
280 | ret = 1; |
281 | } | |
fe81e5c9 | 282 | |
331744e3 | 283 | pthread_mutex_unlock(&channel->metadata_cache->lock); |
fdb1cb82 | 284 | pthread_mutex_unlock(&metadata_stream->lock); |
c9b95901 | 285 | end_unlock_channel: |
ec6ea7d0 | 286 | pthread_mutex_unlock(&channel->timer_lock); |
5e41ebe1 MD |
287 | if (!timer) { |
288 | pthread_mutex_unlock(&channel->lock); | |
289 | } | |
331744e3 JD |
290 | |
291 | return ret; | |
292 | } |