document lttng_ustconsumer_request_metadata locking constraints
[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
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
fe81e5c9
DG
36extern struct lttng_consumer_global_data consumer_data;
37
331744e3
JD
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 */
44static 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;
50
51 assert(channel);
52 assert(channel->metadata_cache);
53
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);
59 if (!tmp_data_ptr) {
60 ERR("Reallocating metadata cache");
61 free(channel->metadata_cache->data);
62 ret = -1;
63 goto end;
64 }
65 channel->metadata_cache->data = tmp_data_ptr;
66 channel->metadata_cache->cache_alloc_size = new_size;
67
68end:
69 return ret;
70}
71
72/*
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.
77 *
78 * Return 0 on success, a negative value on error.
79 */
80int consumer_metadata_cache_write(struct lttng_consumer_channel *channel,
81 unsigned int offset, unsigned int len, char *data)
82{
83 int ret = 0;
84 struct consumer_metadata_cache *cache;
85
86 assert(channel);
87 assert(channel->metadata_cache);
88
89 cache = channel->metadata_cache;
90 DBG("Writing %u bytes from offset %u in metadata cache", len, offset);
91
92 if (offset + len > cache->cache_alloc_size) {
93 ret = extend_metadata_cache(channel,
94 len - cache->cache_alloc_size + offset);
95 if (ret < 0) {
96 ERR("Extending metadata cache");
97 goto end;
98 }
99 }
100
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;
105 }
106
107 if (cache->max_offset == cache->total_bytes_written) {
04ef1097
MD
108 char dummy = 'c';
109
110 cache->contiguous = cache->max_offset;
111 if (channel->monitor) {
112 ret = write(channel->metadata_stream->ust_metadata_poll_pipe[1],
113 &dummy, 1);
114 if (ret < 1) {
115 ERR("Wakeup UST metadata pipe");
116 goto end;
117 }
331744e3 118 }
331744e3
JD
119 }
120
121end:
122 return ret;
123}
124
125/*
126 * Create the metadata cache, original allocated size: max_sb_size
127 *
128 * Return 0 on success, a negative value on error.
129 */
130int consumer_metadata_cache_allocate(struct lttng_consumer_channel *channel)
131{
132 int ret;
133
134 assert(channel);
135
136 channel->metadata_cache = zmalloc(
137 sizeof(struct consumer_metadata_cache));
138 if (!channel->metadata_cache) {
139 PERROR("zmalloc metadata cache struct");
140 ret = -1;
141 goto end;
142 }
143 ret = pthread_mutex_init(&channel->metadata_cache->lock, NULL);
144 if (ret != 0) {
145 PERROR("mutex init");
146 goto end_free_cache;
147 }
148
149 channel->metadata_cache->cache_alloc_size = DEFAULT_METADATA_CACHE_SIZE;
150 channel->metadata_cache->data = zmalloc(
151 channel->metadata_cache->cache_alloc_size * sizeof(char));
152 if (!channel->metadata_cache->data) {
153 PERROR("zmalloc metadata cache data");
154 ret = -1;
155 goto end_free_mutex;
156 }
157 DBG("Allocated metadata cache of %" PRIu64 " bytes",
158 channel->metadata_cache->cache_alloc_size);
159
160 ret = 0;
161 goto end;
162
163end_free_mutex:
164 pthread_mutex_destroy(&channel->metadata_cache->lock);
165end_free_cache:
166 free(channel->metadata_cache);
167end:
168 return ret;
169}
170
171/*
172 * Destroy and free the metadata cache
173 */
174void consumer_metadata_cache_destroy(struct lttng_consumer_channel *channel)
175{
176 if (!channel || !channel->metadata_cache) {
177 return;
178 }
179
180 DBG("Destroying metadata cache");
181
331744e3
JD
182 pthread_mutex_destroy(&channel->metadata_cache->lock);
183 free(channel->metadata_cache->data);
184 free(channel->metadata_cache);
185}
186
187/*
188 * Check if the cache is flushed up to the offset passed in parameter.
189 *
190 * Return 0 if everything has been flushed, 1 if there is data not flushed.
191 */
192int consumer_metadata_cache_flushed(struct lttng_consumer_channel *channel,
193 uint64_t offset)
194{
04ef1097
MD
195 int ret = 0;
196 struct lttng_consumer_stream *metadata_stream;
331744e3
JD
197
198 assert(channel);
199 assert(channel->metadata_cache);
200
7f725ec5
MD
201 /*
202 * XXX This consumer_data.lock should eventually be replaced by
203 * a channel lock. It protects metadata_stream read and endpoint
204 * status check.
205 */
fe81e5c9 206 pthread_mutex_lock(&consumer_data.lock);
a9838785 207 pthread_mutex_lock(&channel->lock);
331744e3 208 pthread_mutex_lock(&channel->metadata_cache->lock);
fe81e5c9 209
04ef1097
MD
210 metadata_stream = channel->metadata_stream;
211
212 if (!metadata_stream) {
fe81e5c9
DG
213 /*
214 * Having no metadata stream means the channel is being destroyed so there
215 * is no cache to flush anymore.
216 */
217 ret = 0;
04ef1097
MD
218 } else if (metadata_stream->ust_metadata_pushed >= offset) {
219 ret = 0;
fe81e5c9
DG
220 } else if (channel->metadata_stream->endpoint_status !=
221 CONSUMER_ENDPOINT_ACTIVE) {
222 /* An inactive endpoint means we don't have to flush anymore. */
223 ret = 0;
331744e3 224 } else {
fe81e5c9 225 /* Still not completely flushed. */
331744e3
JD
226 ret = 1;
227 }
fe81e5c9 228
331744e3 229 pthread_mutex_unlock(&channel->metadata_cache->lock);
a9838785 230 pthread_mutex_unlock(&channel->lock);
fe81e5c9 231 pthread_mutex_unlock(&consumer_data.lock);
331744e3
JD
232
233 return ret;
234}
This page took 0.034394 seconds and 5 git commands to generate.