2 * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com>
3 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
14 #include <sys/types.h>
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>
22 #include <common/consumer/consumer.h>
24 #include "consumer-metadata-cache.h"
26 enum metadata_cache_update_version_status
{
27 METADATA_CACHE_UPDATE_STATUS_VERSION_UPDATED
,
28 METADATA_CACHE_UPDATE_STATUS_VERSION_NOT_UPDATED
,
31 extern struct lttng_consumer_global_data consumer_data
;
34 * Extend the allocated size of the metadata cache. Called only from
35 * lttng_ustconsumer_write_metadata_cache.
37 * Return 0 on success, a negative value on error.
39 static int extend_metadata_cache(struct lttng_consumer_channel
*channel
,
44 unsigned int new_size
, old_size
;
47 assert(channel
->metadata_cache
);
49 old_size
= channel
->metadata_cache
->cache_alloc_size
;
50 new_size
= max_t(unsigned int, old_size
+ size
, old_size
<< 1);
51 DBG("Extending metadata cache to %u", new_size
);
52 tmp_data_ptr
= realloc(channel
->metadata_cache
->data
, new_size
);
54 ERR("Reallocating metadata cache");
55 free(channel
->metadata_cache
->data
);
59 /* Zero newly allocated memory */
60 memset(tmp_data_ptr
+ old_size
, 0, new_size
- old_size
);
61 channel
->metadata_cache
->data
= tmp_data_ptr
;
62 channel
->metadata_cache
->cache_alloc_size
= new_size
;
69 * Reset the metadata cache.
72 void metadata_cache_reset(struct consumer_metadata_cache
*cache
)
74 memset(cache
->data
, 0, cache
->cache_alloc_size
);
75 cache
->max_offset
= 0;
79 * Check if the metadata cache version changed.
80 * If it did, reset the metadata cache.
81 * The metadata cache lock MUST be held.
83 static enum metadata_cache_update_version_status
metadata_cache_update_version(
84 struct consumer_metadata_cache
*cache
, uint64_t version
)
86 enum metadata_cache_update_version_status status
;
88 if (cache
->version
== version
) {
89 status
= METADATA_CACHE_UPDATE_STATUS_VERSION_NOT_UPDATED
;
93 DBG("Metadata cache version update to %" PRIu64
, version
);
94 cache
->version
= version
;
95 status
= METADATA_CACHE_UPDATE_STATUS_VERSION_UPDATED
;
102 * Write metadata to the cache, extend the cache if necessary. We support
103 * overlapping updates, but they need to be contiguous. Send the
104 * contiguous metadata in cache to the ring buffer. The metadata cache
105 * lock MUST be acquired to write in the cache.
107 * See `enum consumer_metadata_cache_write_status` for the meaning of the
108 * various returned status codes.
110 enum consumer_metadata_cache_write_status
111 consumer_metadata_cache_write(struct lttng_consumer_channel
*channel
,
112 unsigned int offset
, unsigned int len
, uint64_t version
,
116 struct consumer_metadata_cache
*cache
;
117 enum consumer_metadata_cache_write_status status
;
118 bool cache_is_invalidated
= false;
119 uint64_t original_max_offset
;
122 assert(channel
->metadata_cache
);
124 cache
= channel
->metadata_cache
;
125 ASSERT_LOCKED(cache
->lock
);
126 original_max_offset
= cache
->max_offset
;
128 if (metadata_cache_update_version(cache
, version
) ==
129 METADATA_CACHE_UPDATE_STATUS_VERSION_UPDATED
) {
130 metadata_cache_reset(cache
);
131 cache_is_invalidated
= true;
134 DBG("Writing %u bytes from offset %u in metadata cache", len
, offset
);
136 if (offset
+ len
> cache
->cache_alloc_size
) {
137 ret
= extend_metadata_cache(channel
,
138 len
- cache
->cache_alloc_size
+ offset
);
140 ERR("Extending metadata cache");
141 status
= CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR
;
146 memcpy(cache
->data
+ offset
, data
, len
);
147 cache
->max_offset
= max(cache
->max_offset
, offset
+ len
);
149 if (cache_is_invalidated
) {
150 status
= CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED
;
151 } else if (cache
->max_offset
> original_max_offset
) {
152 status
= CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT
;
154 status
= CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE
;
155 assert(cache
->max_offset
== original_max_offset
);
163 * Create the metadata cache, original allocated size: max_sb_size
165 * Return 0 on success, a negative value on error.
167 int consumer_metadata_cache_allocate(struct lttng_consumer_channel
*channel
)
173 channel
->metadata_cache
= zmalloc(
174 sizeof(struct consumer_metadata_cache
));
175 if (!channel
->metadata_cache
) {
176 PERROR("zmalloc metadata cache struct");
180 ret
= pthread_mutex_init(&channel
->metadata_cache
->lock
, NULL
);
182 PERROR("mutex init");
186 channel
->metadata_cache
->cache_alloc_size
= DEFAULT_METADATA_CACHE_SIZE
;
187 channel
->metadata_cache
->data
= zmalloc(
188 channel
->metadata_cache
->cache_alloc_size
* sizeof(char));
189 if (!channel
->metadata_cache
->data
) {
190 PERROR("zmalloc metadata cache data");
194 DBG("Allocated metadata cache of %" PRIu64
" bytes",
195 channel
->metadata_cache
->cache_alloc_size
);
201 pthread_mutex_destroy(&channel
->metadata_cache
->lock
);
203 free(channel
->metadata_cache
);
209 * Destroy and free the metadata cache
211 void consumer_metadata_cache_destroy(struct lttng_consumer_channel
*channel
)
213 if (!channel
|| !channel
->metadata_cache
) {
217 DBG("Destroying metadata cache");
219 pthread_mutex_destroy(&channel
->metadata_cache
->lock
);
220 free(channel
->metadata_cache
->data
);
221 free(channel
->metadata_cache
);
225 * Check if the cache is flushed up to the offset passed in parameter.
227 * Return 0 if everything has been flushed, 1 if there is data not flushed.
229 int consumer_metadata_cache_flushed(struct lttng_consumer_channel
*channel
,
230 uint64_t offset
, int timer
)
233 struct lttng_consumer_stream
*metadata_stream
;
236 assert(channel
->metadata_cache
);
239 * If not called from a timer handler, we have to take the
240 * channel lock to be mutually exclusive with channel teardown.
241 * Timer handler does not need to take this lock because it is
242 * already synchronized by timer stop (and, more importantly,
243 * taking this lock in a timer handler would cause a deadlock).
246 pthread_mutex_lock(&channel
->lock
);
248 pthread_mutex_lock(&channel
->timer_lock
);
249 metadata_stream
= channel
->metadata_stream
;
250 if (!metadata_stream
) {
252 * Having no metadata stream means the channel is being destroyed so there
253 * is no cache to flush anymore.
256 goto end_unlock_channel
;
259 pthread_mutex_lock(&metadata_stream
->lock
);
260 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
262 if (metadata_stream
->ust_metadata_pushed
>= offset
) {
264 } else if (channel
->metadata_stream
->endpoint_status
!=
265 CONSUMER_ENDPOINT_ACTIVE
) {
266 /* An inactive endpoint means we don't have to flush anymore. */
269 /* Still not completely flushed. */
273 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
274 pthread_mutex_unlock(&metadata_stream
->lock
);
276 pthread_mutex_unlock(&channel
->timer_lock
);
278 pthread_mutex_unlock(&channel
->lock
);