Fix: removed useless key from relay_index
[lttng-tools.git] / src / common / consumer-metadata-cache.c
CommitLineData
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
19#define _GNU_SOURCE
6c1c0768 20#define _LGPL_SOURCE
331744e3
JD
21#include <assert.h>
22#include <pthread.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/types.h>
26#include <unistd.h>
27#include <inttypes.h>
28
29#include <common/common.h>
30#include <common/utils.h>
31#include <common/sessiond-comm/sessiond-comm.h>
32#include <common/ust-consumer/ust-consumer.h>
33#include <common/consumer.h>
34
35#include "consumer-metadata-cache.h"
36
fe81e5c9
DG
37extern struct lttng_consumer_global_data consumer_data;
38
331744e3
JD
39/*
40 * Extend the allocated size of the metadata cache. Called only from
41 * lttng_ustconsumer_write_metadata_cache.
42 *
43 * Return 0 on success, a negative value on error.
44 */
45static int extend_metadata_cache(struct lttng_consumer_channel *channel,
46 unsigned int size)
47{
48 int ret = 0;
49 char *tmp_data_ptr;
53efb85a 50 unsigned int new_size, old_size;
331744e3
JD
51
52 assert(channel);
53 assert(channel->metadata_cache);
54
53efb85a
MD
55 old_size = channel->metadata_cache->cache_alloc_size;
56 new_size = max_t(unsigned int, old_size + size, old_size << 1);
331744e3
JD
57 DBG("Extending metadata cache to %u", new_size);
58 tmp_data_ptr = realloc(channel->metadata_cache->data, new_size);
59 if (!tmp_data_ptr) {
60 ERR("Reallocating metadata cache");
61 free(channel->metadata_cache->data);
62 ret = -1;
63 goto end;
64 }
53efb85a
MD
65 /* Zero newly allocated memory */
66 memset(tmp_data_ptr + old_size, 0, new_size - old_size);
331744e3
JD
67 channel->metadata_cache->data = tmp_data_ptr;
68 channel->metadata_cache->cache_alloc_size = new_size;
69
70end:
71 return ret;
72}
73
74/*
75 * Write metadata to the cache, extend the cache if necessary. We support
76 * non-contiguous updates but not overlapping ones. If there is contiguous
77 * metadata in the cache, we send it to the ring buffer. The metadata cache
78 * lock MUST be acquired to write in the cache.
79 *
80 * Return 0 on success, a negative value on error.
81 */
82int consumer_metadata_cache_write(struct lttng_consumer_channel *channel,
83 unsigned int offset, unsigned int len, char *data)
84{
85 int ret = 0;
6cd525e8 86 int size_ret;
331744e3
JD
87 struct consumer_metadata_cache *cache;
88
89 assert(channel);
90 assert(channel->metadata_cache);
91
92 cache = channel->metadata_cache;
93 DBG("Writing %u bytes from offset %u in metadata cache", len, offset);
94
95 if (offset + len > cache->cache_alloc_size) {
96 ret = extend_metadata_cache(channel,
97 len - cache->cache_alloc_size + offset);
98 if (ret < 0) {
99 ERR("Extending metadata cache");
100 goto end;
101 }
102 }
103
104 memcpy(cache->data + offset, data, len);
105 cache->total_bytes_written += len;
106 if (offset + len > cache->max_offset) {
107 cache->max_offset = offset + len;
108 }
109
110 if (cache->max_offset == cache->total_bytes_written) {
04ef1097
MD
111 char dummy = 'c';
112
113 cache->contiguous = cache->max_offset;
114 if (channel->monitor) {
6cd525e8 115 size_ret = lttng_write(channel->metadata_stream->ust_metadata_poll_pipe[1],
04ef1097 116 &dummy, 1);
6cd525e8 117 if (size_ret < 1) {
04ef1097 118 ERR("Wakeup UST metadata pipe");
6cd525e8 119 ret = -1;
04ef1097
MD
120 goto end;
121 }
331744e3 122 }
331744e3
JD
123 }
124
125end:
126 return ret;
127}
128
129/*
130 * Create the metadata cache, original allocated size: max_sb_size
131 *
132 * Return 0 on success, a negative value on error.
133 */
134int consumer_metadata_cache_allocate(struct lttng_consumer_channel *channel)
135{
136 int ret;
137
138 assert(channel);
139
140 channel->metadata_cache = zmalloc(
141 sizeof(struct consumer_metadata_cache));
142 if (!channel->metadata_cache) {
143 PERROR("zmalloc metadata cache struct");
144 ret = -1;
145 goto end;
146 }
147 ret = pthread_mutex_init(&channel->metadata_cache->lock, NULL);
148 if (ret != 0) {
149 PERROR("mutex init");
150 goto end_free_cache;
151 }
152
153 channel->metadata_cache->cache_alloc_size = DEFAULT_METADATA_CACHE_SIZE;
154 channel->metadata_cache->data = zmalloc(
155 channel->metadata_cache->cache_alloc_size * sizeof(char));
156 if (!channel->metadata_cache->data) {
157 PERROR("zmalloc metadata cache data");
158 ret = -1;
159 goto end_free_mutex;
160 }
161 DBG("Allocated metadata cache of %" PRIu64 " bytes",
162 channel->metadata_cache->cache_alloc_size);
163
164 ret = 0;
165 goto end;
166
167end_free_mutex:
168 pthread_mutex_destroy(&channel->metadata_cache->lock);
169end_free_cache:
170 free(channel->metadata_cache);
171end:
172 return ret;
173}
174
175/*
176 * Destroy and free the metadata cache
177 */
178void consumer_metadata_cache_destroy(struct lttng_consumer_channel *channel)
179{
180 if (!channel || !channel->metadata_cache) {
181 return;
182 }
183
184 DBG("Destroying metadata cache");
185
331744e3
JD
186 pthread_mutex_destroy(&channel->metadata_cache->lock);
187 free(channel->metadata_cache->data);
188 free(channel->metadata_cache);
189}
190
191/*
192 * Check if the cache is flushed up to the offset passed in parameter.
193 *
194 * Return 0 if everything has been flushed, 1 if there is data not flushed.
195 */
196int consumer_metadata_cache_flushed(struct lttng_consumer_channel *channel,
5e41ebe1 197 uint64_t offset, int timer)
331744e3 198{
04ef1097
MD
199 int ret = 0;
200 struct lttng_consumer_stream *metadata_stream;
331744e3
JD
201
202 assert(channel);
203 assert(channel->metadata_cache);
204
7f725ec5 205 /*
5e41ebe1
MD
206 * If not called from a timer handler, we have to take the
207 * channel lock to be mutually exclusive with channel teardown.
208 * Timer handler does not need to take this lock because it is
209 * already synchronized by timer stop (and, more importantly,
210 * taking this lock in a timer handler would cause a deadlock).
7f725ec5 211 */
5e41ebe1
MD
212 if (!timer) {
213 pthread_mutex_lock(&channel->lock);
214 }
ec6ea7d0 215 pthread_mutex_lock(&channel->timer_lock);
331744e3 216 pthread_mutex_lock(&channel->metadata_cache->lock);
fe81e5c9 217
04ef1097
MD
218 metadata_stream = channel->metadata_stream;
219
220 if (!metadata_stream) {
fe81e5c9
DG
221 /*
222 * Having no metadata stream means the channel is being destroyed so there
223 * is no cache to flush anymore.
224 */
225 ret = 0;
04ef1097
MD
226 } else if (metadata_stream->ust_metadata_pushed >= offset) {
227 ret = 0;
fe81e5c9
DG
228 } else if (channel->metadata_stream->endpoint_status !=
229 CONSUMER_ENDPOINT_ACTIVE) {
230 /* An inactive endpoint means we don't have to flush anymore. */
231 ret = 0;
331744e3 232 } else {
fe81e5c9 233 /* Still not completely flushed. */
331744e3
JD
234 ret = 1;
235 }
fe81e5c9 236
331744e3 237 pthread_mutex_unlock(&channel->metadata_cache->lock);
ec6ea7d0 238 pthread_mutex_unlock(&channel->timer_lock);
5e41ebe1
MD
239 if (!timer) {
240 pthread_mutex_unlock(&channel->lock);
241 }
331744e3
JD
242
243 return ret;
244}
This page took 0.037696 seconds and 4 git commands to generate.