Use lttng_read/lttng_write wrappers
[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;
6cd525e8 84 int size_ret;
331744e3
JD
85 struct consumer_metadata_cache *cache;
86
87 assert(channel);
88 assert(channel->metadata_cache);
89
90 cache = channel->metadata_cache;
91 DBG("Writing %u bytes from offset %u in metadata cache", len, offset);
92
93 if (offset + len > cache->cache_alloc_size) {
94 ret = extend_metadata_cache(channel,
95 len - cache->cache_alloc_size + offset);
96 if (ret < 0) {
97 ERR("Extending metadata cache");
98 goto end;
99 }
100 }
101
102 memcpy(cache->data + offset, data, len);
103 cache->total_bytes_written += len;
104 if (offset + len > cache->max_offset) {
105 cache->max_offset = offset + len;
106 }
107
108 if (cache->max_offset == cache->total_bytes_written) {
04ef1097
MD
109 char dummy = 'c';
110
111 cache->contiguous = cache->max_offset;
112 if (channel->monitor) {
6cd525e8 113 size_ret = lttng_write(channel->metadata_stream->ust_metadata_poll_pipe[1],
04ef1097 114 &dummy, 1);
6cd525e8 115 if (size_ret < 1) {
04ef1097 116 ERR("Wakeup UST metadata pipe");
6cd525e8 117 ret = -1;
04ef1097
MD
118 goto end;
119 }
331744e3 120 }
331744e3
JD
121 }
122
123end:
124 return ret;
125}
126
127/*
128 * Create the metadata cache, original allocated size: max_sb_size
129 *
130 * Return 0 on success, a negative value on error.
131 */
132int consumer_metadata_cache_allocate(struct lttng_consumer_channel *channel)
133{
134 int ret;
135
136 assert(channel);
137
138 channel->metadata_cache = zmalloc(
139 sizeof(struct consumer_metadata_cache));
140 if (!channel->metadata_cache) {
141 PERROR("zmalloc metadata cache struct");
142 ret = -1;
143 goto end;
144 }
145 ret = pthread_mutex_init(&channel->metadata_cache->lock, NULL);
146 if (ret != 0) {
147 PERROR("mutex init");
148 goto end_free_cache;
149 }
150
151 channel->metadata_cache->cache_alloc_size = DEFAULT_METADATA_CACHE_SIZE;
152 channel->metadata_cache->data = zmalloc(
153 channel->metadata_cache->cache_alloc_size * sizeof(char));
154 if (!channel->metadata_cache->data) {
155 PERROR("zmalloc metadata cache data");
156 ret = -1;
157 goto end_free_mutex;
158 }
159 DBG("Allocated metadata cache of %" PRIu64 " bytes",
160 channel->metadata_cache->cache_alloc_size);
161
162 ret = 0;
163 goto end;
164
165end_free_mutex:
166 pthread_mutex_destroy(&channel->metadata_cache->lock);
167end_free_cache:
168 free(channel->metadata_cache);
169end:
170 return ret;
171}
172
173/*
174 * Destroy and free the metadata cache
175 */
176void consumer_metadata_cache_destroy(struct lttng_consumer_channel *channel)
177{
178 if (!channel || !channel->metadata_cache) {
179 return;
180 }
181
182 DBG("Destroying metadata cache");
183
331744e3
JD
184 pthread_mutex_destroy(&channel->metadata_cache->lock);
185 free(channel->metadata_cache->data);
186 free(channel->metadata_cache);
187}
188
189/*
190 * Check if the cache is flushed up to the offset passed in parameter.
191 *
192 * Return 0 if everything has been flushed, 1 if there is data not flushed.
193 */
194int consumer_metadata_cache_flushed(struct lttng_consumer_channel *channel,
5e41ebe1 195 uint64_t offset, int timer)
331744e3 196{
04ef1097
MD
197 int ret = 0;
198 struct lttng_consumer_stream *metadata_stream;
331744e3
JD
199
200 assert(channel);
201 assert(channel->metadata_cache);
202
7f725ec5 203 /*
5e41ebe1
MD
204 * If not called from a timer handler, we have to take the
205 * channel lock to be mutually exclusive with channel teardown.
206 * Timer handler does not need to take this lock because it is
207 * already synchronized by timer stop (and, more importantly,
208 * taking this lock in a timer handler would cause a deadlock).
7f725ec5 209 */
5e41ebe1
MD
210 if (!timer) {
211 pthread_mutex_lock(&channel->lock);
212 }
ec6ea7d0 213 pthread_mutex_lock(&channel->timer_lock);
331744e3 214 pthread_mutex_lock(&channel->metadata_cache->lock);
fe81e5c9 215
04ef1097
MD
216 metadata_stream = channel->metadata_stream;
217
218 if (!metadata_stream) {
fe81e5c9
DG
219 /*
220 * Having no metadata stream means the channel is being destroyed so there
221 * is no cache to flush anymore.
222 */
223 ret = 0;
04ef1097
MD
224 } else if (metadata_stream->ust_metadata_pushed >= offset) {
225 ret = 0;
fe81e5c9
DG
226 } else if (channel->metadata_stream->endpoint_status !=
227 CONSUMER_ENDPOINT_ACTIVE) {
228 /* An inactive endpoint means we don't have to flush anymore. */
229 ret = 0;
331744e3 230 } else {
fe81e5c9 231 /* Still not completely flushed. */
331744e3
JD
232 ret = 1;
233 }
fe81e5c9 234
331744e3 235 pthread_mutex_unlock(&channel->metadata_cache->lock);
ec6ea7d0 236 pthread_mutex_unlock(&channel->timer_lock);
5e41ebe1
MD
237 if (!timer) {
238 pthread_mutex_unlock(&channel->lock);
239 }
331744e3
JD
240
241 return ret;
242}
This page took 0.036291 seconds and 4 git commands to generate.