2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
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.
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
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.
24 #include <sys/types.h>
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.h>
34 #include "consumer-metadata-cache.h"
36 extern struct lttng_consumer_global_data consumer_data
;
39 * Extend the allocated size of the metadata cache. Called only from
40 * lttng_ustconsumer_write_metadata_cache.
42 * Return 0 on success, a negative value on error.
44 static int extend_metadata_cache(struct lttng_consumer_channel
*channel
,
49 unsigned int new_size
;
52 assert(channel
->metadata_cache
);
54 new_size
= max_t(unsigned int,
55 channel
->metadata_cache
->cache_alloc_size
+ size
,
56 channel
->metadata_cache
->cache_alloc_size
<< 1);
57 DBG("Extending metadata cache to %u", new_size
);
58 tmp_data_ptr
= realloc(channel
->metadata_cache
->data
, new_size
);
60 ERR("Reallocating metadata cache");
61 free(channel
->metadata_cache
->data
);
65 channel
->metadata_cache
->data
= tmp_data_ptr
;
66 channel
->metadata_cache
->cache_alloc_size
= new_size
;
73 * Write metadata to the cache, extend the cache if necessary. We support
74 * non-contiguous updates but not overlapping ones. If there is contiguous
75 * metadata in the cache, we send it to the ring buffer. The metadata cache
76 * lock MUST be acquired to write in the cache.
78 * Return 0 on success, a negative value on error.
80 int consumer_metadata_cache_write(struct lttng_consumer_channel
*channel
,
81 unsigned int offset
, unsigned int len
, char *data
)
84 struct consumer_metadata_cache
*cache
;
87 assert(channel
->metadata_cache
);
89 cache
= channel
->metadata_cache
;
90 DBG("Writing %u bytes from offset %u in metadata cache", len
, offset
);
92 if (offset
+ len
> cache
->cache_alloc_size
) {
93 ret
= extend_metadata_cache(channel
,
94 len
- cache
->cache_alloc_size
+ offset
);
96 ERR("Extending metadata cache");
101 memcpy(cache
->data
+ offset
, data
, len
);
102 cache
->total_bytes_written
+= len
;
103 if (offset
+ len
> cache
->max_offset
) {
104 cache
->max_offset
= offset
+ len
;
107 if (cache
->max_offset
== cache
->total_bytes_written
) {
108 offset
= cache
->rb_pushed
;
109 len
= cache
->total_bytes_written
- cache
->rb_pushed
;
110 ret
= lttng_ustconsumer_push_metadata(channel
, cache
->data
, offset
,
113 ERR("Pushing metadata");
116 cache
->rb_pushed
+= len
;
124 * Create the metadata cache, original allocated size: max_sb_size
126 * Return 0 on success, a negative value on error.
128 int consumer_metadata_cache_allocate(struct lttng_consumer_channel
*channel
)
134 channel
->metadata_cache
= zmalloc(
135 sizeof(struct consumer_metadata_cache
));
136 if (!channel
->metadata_cache
) {
137 PERROR("zmalloc metadata cache struct");
141 ret
= pthread_mutex_init(&channel
->metadata_cache
->lock
, NULL
);
143 PERROR("mutex init");
147 channel
->metadata_cache
->cache_alloc_size
= DEFAULT_METADATA_CACHE_SIZE
;
148 channel
->metadata_cache
->data
= zmalloc(
149 channel
->metadata_cache
->cache_alloc_size
* sizeof(char));
150 if (!channel
->metadata_cache
->data
) {
151 PERROR("zmalloc metadata cache data");
155 DBG("Allocated metadata cache of %" PRIu64
" bytes",
156 channel
->metadata_cache
->cache_alloc_size
);
162 pthread_mutex_destroy(&channel
->metadata_cache
->lock
);
164 free(channel
->metadata_cache
);
170 * Destroy and free the metadata cache
172 void consumer_metadata_cache_destroy(struct lttng_consumer_channel
*channel
)
174 if (!channel
|| !channel
->metadata_cache
) {
178 DBG("Destroying metadata cache");
180 if (channel
->metadata_cache
->max_offset
>
181 channel
->metadata_cache
->rb_pushed
) {
182 ERR("Destroying a cache not entirely commited");
185 pthread_mutex_destroy(&channel
->metadata_cache
->lock
);
186 free(channel
->metadata_cache
->data
);
187 free(channel
->metadata_cache
);
191 * Check if the cache is flushed up to the offset passed in parameter.
193 * Return 0 if everything has been flushed, 1 if there is data not flushed.
195 int consumer_metadata_cache_flushed(struct lttng_consumer_channel
*channel
,
199 struct consumer_metadata_cache
*cache
;
202 assert(channel
->metadata_cache
);
204 cache
= channel
->metadata_cache
;
206 pthread_mutex_lock(&consumer_data
.lock
);
207 pthread_mutex_lock(&channel
->metadata_cache
->lock
);
209 if (cache
->rb_pushed
>= offset
) {
211 } else if (!channel
->metadata_stream
) {
213 * Having no metadata stream means the channel is being destroyed so there
214 * is no cache to flush anymore.
217 } else if (channel
->metadata_stream
->endpoint_status
!=
218 CONSUMER_ENDPOINT_ACTIVE
) {
219 /* An inactive endpoint means we don't have to flush anymore. */
222 /* Still not completely flushed. */
226 pthread_mutex_unlock(&channel
->metadata_cache
->lock
);
227 pthread_mutex_unlock(&consumer_data
.lock
);