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
10 #include "consumer-metadata-cache.hpp"
12 #include <common/common.hpp>
13 #include <common/consumer/consumer.hpp>
14 #include <common/sessiond-comm/sessiond-comm.hpp>
15 #include <common/ust-consumer/ust-consumer.hpp>
16 #include <common/utils.hpp>
22 #include <sys/types.h>
25 enum metadata_cache_update_version_status
{
26 METADATA_CACHE_UPDATE_STATUS_VERSION_UPDATED
,
27 METADATA_CACHE_UPDATE_STATUS_VERSION_NOT_UPDATED
,
30 extern struct lttng_consumer_global_data the_consumer_data
;
33 * Reset the metadata cache.
35 static void metadata_cache_reset(struct consumer_metadata_cache
*cache
)
37 const int ret
= lttng_dynamic_buffer_set_size(&cache
->contents
, 0);
39 LTTNG_ASSERT(ret
== 0);
43 * Check if the metadata cache version changed.
44 * If it did, reset the metadata cache.
45 * The metadata cache lock MUST be held.
47 static enum metadata_cache_update_version_status
48 metadata_cache_update_version(struct consumer_metadata_cache
*cache
, uint64_t version
)
50 enum metadata_cache_update_version_status status
;
52 if (cache
->version
== version
) {
53 status
= METADATA_CACHE_UPDATE_STATUS_VERSION_NOT_UPDATED
;
57 DBG("Metadata cache version update to %" PRIu64
, version
);
58 cache
->version
= version
;
59 status
= METADATA_CACHE_UPDATE_STATUS_VERSION_UPDATED
;
66 * Write metadata to the cache, extend the cache if necessary. We support
67 * overlapping updates, but they need to be contiguous. Send the
68 * contiguous metadata in cache to the ring buffer. The metadata cache
69 * lock MUST be acquired to write in the cache.
71 * See `enum consumer_metadata_cache_write_status` for the meaning of the
72 * various returned status codes.
74 enum consumer_metadata_cache_write_status
75 consumer_metadata_cache_write(struct consumer_metadata_cache
*cache
,
82 enum consumer_metadata_cache_write_status status
;
83 bool cache_is_invalidated
= false;
84 uint64_t original_size
;
87 ASSERT_LOCKED(cache
->lock
);
88 original_size
= cache
->contents
.size
;
90 if (metadata_cache_update_version(cache
, version
) ==
91 METADATA_CACHE_UPDATE_STATUS_VERSION_UPDATED
) {
92 metadata_cache_reset(cache
);
93 cache_is_invalidated
= true;
96 DBG("Writing %u bytes from offset %u in metadata cache", len
, offset
);
97 if (offset
+ len
> cache
->contents
.size
) {
98 ret
= lttng_dynamic_buffer_set_size(&cache
->contents
, offset
+ len
);
100 ERR("Extending metadata cache");
101 status
= CONSUMER_METADATA_CACHE_WRITE_STATUS_ERROR
;
106 memcpy(cache
->contents
.data
+ offset
, data
, len
);
108 if (cache_is_invalidated
) {
109 status
= CONSUMER_METADATA_CACHE_WRITE_STATUS_INVALIDATED
;
110 } else if (cache
->contents
.size
> original_size
) {
111 status
= CONSUMER_METADATA_CACHE_WRITE_STATUS_APPENDED_CONTENT
;
113 status
= CONSUMER_METADATA_CACHE_WRITE_STATUS_NO_CHANGE
;
114 LTTNG_ASSERT(cache
->contents
.size
== original_size
);
122 * Create the metadata cache, original allocated size: max_sb_size
124 * Return 0 on success, a negative value on error.
126 int consumer_metadata_cache_allocate(struct lttng_consumer_channel
*channel
)
130 LTTNG_ASSERT(channel
);
132 channel
->metadata_cache
= zmalloc
<consumer_metadata_cache
>();
133 if (!channel
->metadata_cache
) {
134 PERROR("zmalloc metadata cache struct");
138 ret
= pthread_mutex_init(&channel
->metadata_cache
->lock
, nullptr);
140 PERROR("mutex init");
144 lttng_dynamic_buffer_init(&channel
->metadata_cache
->contents
);
145 ret
= lttng_dynamic_buffer_set_capacity(&channel
->metadata_cache
->contents
,
146 DEFAULT_METADATA_CACHE_SIZE
);
148 PERROR("Failed to pre-allocate metadata cache storage of %d bytes on creation",
149 DEFAULT_METADATA_CACHE_SIZE
);
154 DBG("Allocated metadata cache: current capacity = %zu",
155 lttng_dynamic_buffer_get_capacity_left(&channel
->metadata_cache
->contents
));
161 pthread_mutex_destroy(&channel
->metadata_cache
->lock
);
163 free(channel
->metadata_cache
);
169 * Destroy and free the metadata cache
171 void consumer_metadata_cache_destroy(struct lttng_consumer_channel
*channel
)
173 if (!channel
|| !channel
->metadata_cache
) {
177 DBG("Destroying metadata cache");
179 pthread_mutex_destroy(&channel
->metadata_cache
->lock
);
180 lttng_dynamic_buffer_reset(&channel
->metadata_cache
->contents
);
181 free(channel
->metadata_cache
);
185 * Check if the cache is flushed up to the offset passed in parameter.
187 * Return true if everything has been flushed, false if there is data not flushed.
190 bool consumer_metadata_cache_is_flushed(struct lttng_consumer_channel
*channel
,
194 bool done_flushing
= false;
195 struct lttng_consumer_stream
*metadata_stream
;
198 * If not called from a timer handler, we have to take the
199 * channel lock to be mutually exclusive with channel teardown.
200 * Timer handler does not need to take this lock because it is
201 * already synchronized by timer stop (and, more importantly,
202 * taking this lock in a timer handler would cause a deadlock).
205 pthread_mutex_lock(&channel
->lock
);
207 pthread_mutex_lock(&channel
->timer_lock
);
208 metadata_stream
= channel
->metadata_stream
;
209 if (!metadata_stream
) {
211 * Having no metadata stream means the channel is being destroyed so there
212 * is no cache to flush anymore.
214 done_flushing
= true;
215 goto end_unlock_channel
;
218 pthread_mutex_lock(&metadata_stream
->lock
);
219 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
221 if (metadata_stream
->ust_metadata_pushed
>= offset
) {
222 done_flushing
= true;
223 } else if (channel
->metadata_stream
->endpoint_status
!= CONSUMER_ENDPOINT_ACTIVE
) {
224 /* An inactive endpoint means we don't have to flush anymore. */
225 done_flushing
= true;
227 /* Still not completely flushed. */
228 done_flushing
= false;
231 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
232 pthread_mutex_unlock(&metadata_stream
->lock
);
235 pthread_mutex_unlock(&channel
->timer_lock
);
237 pthread_mutex_unlock(&channel
->lock
);
240 return done_flushing
;
245 * Wait until the cache is flushed up to the offset passed in parameter or the
246 * metadata stream has been destroyed.
248 void consumer_wait_metadata_cache_flushed(struct lttng_consumer_channel
*channel
,
250 bool invoked_by_timer
)
253 assert(channel
->metadata_cache
);
255 if (consumer_metadata_cache_is_flushed(channel
, offset
, invoked_by_timer
)) {
259 /* Metadata cache is not currently flushed, wait on wait queue. */
261 lttng::synchro::waiter waiter
;
263 channel
->metadata_pushed_wait_queue
.add(waiter
);
264 if (consumer_metadata_cache_is_flushed(channel
, offset
, invoked_by_timer
)) {
265 /* Wake up all waiters, ourself included. */
266 channel
->metadata_pushed_wait_queue
.wake_all();
267 /* Ensure proper teardown of waiter. */