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