Decouple component class from plugin subsystem, remove component factory
[babeltrace.git] / plugins / ctf / fs / data-stream.c
1 /*
2 * Copyright 2016 - Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <stdbool.h>
29 #include <glib.h>
30 #include <inttypes.h>
31 #include <sys/mman.h>
32 #include <babeltrace/ctf-ir/stream.h>
33 #include <babeltrace/component/notification/iterator.h>
34 #include "file.h"
35 #include "metadata.h"
36 #include "../common/notif-iter/notif-iter.h"
37 #include <assert.h>
38 #include "data-stream.h"
39
40 #define PRINT_ERR_STREAM ctf_fs->error_fp
41 #define PRINT_PREFIX "ctf-fs-data-stream"
42 #include "print.h"
43
44 static
45 size_t remaining_mmap_bytes(struct ctf_fs_stream *stream)
46 {
47 return stream->mmap_valid_len - stream->request_offset;
48 }
49
50 static
51 int stream_munmap(struct ctf_fs_stream *stream)
52 {
53 int ret = 0;
54 struct ctf_fs_component *ctf_fs = stream->file->ctf_fs;
55
56 if (munmap(stream->mmap_addr, stream->mmap_len)) {
57 PERR("Cannot memory-unmap address %p (size %zu) of file \"%s\" (%p): %s\n",
58 stream->mmap_addr, stream->mmap_len,
59 stream->file->path->str, stream->file->fp,
60 strerror(errno));
61 ret = -1;
62 goto end;
63 }
64 end:
65 return ret;
66 }
67
68 static
69 enum bt_ctf_notif_iter_medium_status mmap_next(struct ctf_fs_stream *stream)
70 {
71 enum bt_ctf_notif_iter_medium_status ret =
72 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK;
73 struct ctf_fs_component *ctf_fs = stream->file->ctf_fs;
74
75 /* Unmap old region */
76 if (stream->mmap_addr) {
77 if (stream_munmap(stream)) {
78 goto error;
79 }
80
81 stream->mmap_offset += stream->mmap_valid_len;
82 stream->request_offset = 0;
83 }
84
85 stream->mmap_valid_len = MIN(stream->file->size - stream->mmap_offset,
86 stream->mmap_max_len);
87 if (stream->mmap_valid_len == 0) {
88 ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF;
89 goto end;
90 }
91 /* Round up to next page, assuming page size being a power of 2. */
92 stream->mmap_len = (stream->mmap_valid_len + ctf_fs->page_size - 1)
93 & ~(ctf_fs->page_size - 1);
94 /* Map new region */
95 assert(stream->mmap_len);
96 stream->mmap_addr = mmap((void *) 0, stream->mmap_len,
97 PROT_READ, MAP_PRIVATE, fileno(stream->file->fp),
98 stream->mmap_offset);
99 if (stream->mmap_addr == MAP_FAILED) {
100 PERR("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %zu: %s\n",
101 stream->mmap_len, stream->file->path->str,
102 stream->file->fp, stream->mmap_offset,
103 strerror(errno));
104 goto error;
105 }
106
107 goto end;
108 error:
109 stream_munmap(stream);
110 ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
111 end:
112 return ret;
113 }
114
115 static
116 enum bt_ctf_notif_iter_medium_status medop_request_bytes(
117 size_t request_sz, uint8_t **buffer_addr,
118 size_t *buffer_sz, void *data)
119 {
120 enum bt_ctf_notif_iter_medium_status status =
121 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK;
122 struct ctf_fs_stream *stream = data;
123 struct ctf_fs_component *ctf_fs = stream->file->ctf_fs;
124
125 if (request_sz == 0) {
126 goto end;
127 }
128
129 /* Check if we have at least one memory-mapped byte left */
130 if (remaining_mmap_bytes(stream) == 0) {
131 /* Are we at the end of the file? */
132 if (stream->mmap_offset >= stream->file->size) {
133 PDBG("Reached end of file \"%s\" (%p)\n",
134 stream->file->path->str, stream->file->fp);
135 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF;
136 goto end;
137 }
138
139 status = mmap_next(stream);
140 switch (status) {
141 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK:
142 break;
143 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF:
144 goto end;
145 default:
146 PERR("Cannot memory-map next region of file \"%s\" (%p)\n",
147 stream->file->path->str,
148 stream->file->fp);
149 goto error;
150 }
151 }
152
153 *buffer_sz = MIN(remaining_mmap_bytes(stream), request_sz);
154 *buffer_addr = ((uint8_t *) stream->mmap_addr) + stream->request_offset;
155 stream->request_offset += *buffer_sz;
156 goto end;
157
158 error:
159 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
160
161 end:
162 return status;
163 }
164
165 static
166 struct bt_ctf_stream *medop_get_stream(
167 struct bt_ctf_stream_class *stream_class, void *data)
168 {
169 struct ctf_fs_stream *fs_stream = data;
170 struct ctf_fs_component *ctf_fs = fs_stream->file->ctf_fs;
171
172 if (!fs_stream->stream) {
173 int64_t id = bt_ctf_stream_class_get_id(stream_class);
174
175 PDBG("Creating stream out of stream class %" PRId64 "\n", id);
176 fs_stream->stream = bt_ctf_stream_create(stream_class,
177 fs_stream->file->path->str);
178 if (!fs_stream->stream) {
179 PERR("Cannot create stream (stream class %" PRId64 ")\n",
180 id);
181 }
182 }
183
184 return fs_stream->stream;
185 }
186
187 static struct bt_ctf_notif_iter_medium_ops medops = {
188 .request_bytes = medop_request_bytes,
189 .get_stream = medop_get_stream,
190 };
191
192 static
193 int build_index_from_idx_file(struct ctf_fs_stream *stream)
194 {
195 int ret = 0;
196 gchar *directory = NULL;
197 gchar *basename = NULL;
198 GString *index_basename = NULL;
199 gchar *index_file_path = NULL;
200 GMappedFile *mapped_file = NULL;
201 gsize filesize;
202 const struct ctf_packet_index_file_hdr *header;
203 const char *mmap_begin, *file_pos;
204 struct index_entry *index;
205 uint64_t total_packets_size = 0;
206 size_t file_index_entry_size;
207 size_t file_entry_count;
208 size_t i;
209
210 /* Look for index file in relative path index/name.idx. */
211 basename = g_path_get_basename(stream->file->path->str);
212 if (!basename) {
213 ret = -1;
214 goto end;
215 }
216
217 directory = g_path_get_dirname(stream->file->path->str);
218 if (!directory) {
219 ret = -1;
220 goto end;
221 }
222
223 index_basename = g_string_new(basename);
224 if (!index_basename) {
225 ret = -1;
226 goto end;
227 }
228
229 g_string_append(index_basename, ".idx");
230 index_file_path = g_build_filename(directory, "index",
231 index_basename->str, NULL);
232 mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL);
233 if (!mapped_file) {
234 ret = -1;
235 goto end;
236 }
237 filesize = g_mapped_file_get_length(mapped_file);
238 if (filesize < sizeof(*header)) {
239 printf_error("Invalid LTTng trace index: file size < header size");
240 ret = -1;
241 goto end;
242 }
243
244 mmap_begin = g_mapped_file_get_contents(mapped_file);
245 header = (struct ctf_packet_index_file_hdr *) mmap_begin;
246
247 file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header);
248 if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
249 printf_error("Invalid LTTng trace index: \"magic\" validation failed");
250 ret = -1;
251 goto end;
252 }
253
254 file_index_entry_size = be32toh(header->packet_index_len);
255 file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
256 if ((filesize - sizeof(*header)) % (file_entry_count * file_index_entry_size)) {
257 printf_error("Invalid index file size; not a multiple of index entry size");
258 ret = -1;
259 goto end;
260 }
261
262 stream->index.entries = g_array_sized_new(FALSE, TRUE,
263 sizeof(struct index_entry), file_entry_count);
264 if (!stream->index.entries) {
265 ret = -1;
266 goto end;
267 }
268 index = (struct index_entry *) stream->index.entries->data;
269 for (i = 0; i < file_entry_count; i++) {
270 struct ctf_packet_index *file_index =
271 (struct ctf_packet_index *) file_pos;
272 uint64_t packet_size = be64toh(file_index->packet_size);
273
274 if (packet_size % CHAR_BIT) {
275 ret = -1;
276 printf_error("Invalid packet size encountered in index file");
277 goto invalid_index;
278 }
279
280 /* Convert size in bits to bytes. */
281 packet_size /= CHAR_BIT;
282 index->packet_size = packet_size;
283
284 index->offset = be64toh(file_index->offset);
285 if (i != 0 && index->offset < (index - 1)->offset) {
286 printf_error("Invalid, non-monotonic, packet offset encountered in index file");
287 ret = -1;
288 goto invalid_index;
289 }
290
291 index->timestamp_begin = be64toh(file_index->timestamp_begin);
292 index->timestamp_end = be64toh(file_index->timestamp_end);
293 if (index->timestamp_end < index->timestamp_begin) {
294 printf_error("Invalid packet time bounds encountered in index file");
295 ret = -1;
296 goto invalid_index;
297 }
298
299 total_packets_size += packet_size;
300 file_pos += file_index_entry_size;
301 index++;
302 }
303
304 /* Validate that the index addresses the complete stream. */
305 if (stream->file->size != total_packets_size) {
306 printf_error("Invalid index; indexed size != stream file size");
307 ret = -1;
308 goto invalid_index;
309 }
310 end:
311 g_free(directory);
312 g_free(basename);
313 g_free(index_file_path);
314 if (index_basename) {
315 g_string_free(index_basename, TRUE);
316 }
317 if (mapped_file) {
318 g_mapped_file_unref(mapped_file);
319 }
320 return ret;
321 invalid_index:
322 g_array_free(stream->index.entries, TRUE);
323 goto end;
324 }
325
326 static
327 int build_index_from_stream(struct ctf_fs_stream *stream)
328 {
329 int ret = 0;
330 end:
331 return ret;
332 }
333
334 static
335 int init_stream_index(struct ctf_fs_stream *stream)
336 {
337 int ret;
338
339 ret = build_index_from_idx_file(stream);
340 if (!ret) {
341 goto end;
342 }
343
344 ret = build_index_from_stream(stream);
345 end:
346 return ret;
347 }
348
349 BT_HIDDEN
350 struct ctf_fs_stream *ctf_fs_stream_create(
351 struct ctf_fs_component *ctf_fs, struct ctf_fs_file *file)
352 {
353 int ret;
354 struct ctf_fs_stream *stream = g_new0(struct ctf_fs_stream, 1);
355
356 if (!stream) {
357 goto error;
358 }
359
360 stream->file = file;
361 stream->notif_iter = bt_ctf_notif_iter_create(ctf_fs->metadata->trace,
362 ctf_fs->page_size, medops, stream, ctf_fs->error_fp);
363 if (!stream->notif_iter) {
364 goto error;
365 }
366
367 stream->mmap_max_len = ctf_fs->page_size * 2048;
368 ret = init_stream_index(stream);
369 if (ret) {
370 goto error;
371 }
372 goto end;
373 error:
374 /* Do not touch "borrowed" file. */
375 stream->file = NULL;
376 ctf_fs_stream_destroy(stream);
377 stream = NULL;
378 end:
379 return stream;
380 }
381
382 BT_HIDDEN
383 void ctf_fs_stream_destroy(struct ctf_fs_stream *stream)
384 {
385 if (!stream) {
386 return;
387 }
388
389 if (stream->file) {
390 ctf_fs_file_destroy(stream->file);
391 }
392
393 if (stream->stream) {
394 BT_PUT(stream->stream);
395 }
396
397 if (stream->notif_iter) {
398 bt_ctf_notif_iter_destroy(stream->notif_iter);
399 }
400
401 if (stream->index.entries) {
402 g_array_free(stream->index.entries, TRUE);
403 }
404
405 g_free(stream);
406 }
This page took 0.037036 seconds and 4 git commands to generate.