ctf: add weak ref to stream file in index entry
[babeltrace.git] / src / plugins / ctf / fs-src / data-stream-file.c
1 /*
2 * Copyright 2016-2017 - 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 #define BT_COMP_LOG_SELF_COMP (ds_file->self_comp)
26 #define BT_LOG_OUTPUT_LEVEL (ds_file->log_level)
27 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/DS"
28 #include "logging/comp-logging.h"
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <stdbool.h>
35 #include <glib.h>
36 #include <inttypes.h>
37 #include "compat/mman.h"
38 #include "compat/endian.h"
39 #include <babeltrace2/babeltrace.h>
40 #include "common/common.h"
41 #include "file.h"
42 #include "metadata.h"
43 #include "../common/msg-iter/msg-iter.h"
44 #include "common/assert.h"
45 #include "data-stream-file.h"
46 #include <string.h>
47
48 static inline
49 size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file)
50 {
51 BT_ASSERT(ds_file->mmap_len >= ds_file->request_offset);
52 return ds_file->mmap_len - ds_file->request_offset;
53 }
54
55 static
56 int ds_file_munmap(struct ctf_fs_ds_file *ds_file)
57 {
58 int ret = 0;
59
60 if (!ds_file || !ds_file->mmap_addr) {
61 goto end;
62 }
63
64 if (bt_munmap(ds_file->mmap_addr, ds_file->mmap_len)) {
65 BT_COMP_LOGE_ERRNO("Cannot memory-unmap file",
66 ": address=%p, size=%zu, file_path=\"%s\", file=%p",
67 ds_file->mmap_addr, ds_file->mmap_len,
68 ds_file->file ? ds_file->file->path->str : "NULL",
69 ds_file->file ? ds_file->file->fp : NULL);
70 ret = -1;
71 goto end;
72 }
73
74 ds_file->mmap_addr = NULL;
75
76 end:
77 return ret;
78 }
79
80 static
81 enum bt_msg_iter_medium_status ds_file_mmap_next(
82 struct ctf_fs_ds_file *ds_file)
83 {
84 enum bt_msg_iter_medium_status ret =
85 BT_MSG_ITER_MEDIUM_STATUS_OK;
86
87 /* Unmap old region */
88 if (ds_file->mmap_addr) {
89 if (ds_file_munmap(ds_file)) {
90 goto error;
91 }
92
93 /*
94 * mmap_len is guaranteed to be page-aligned except on the
95 * last mapping where it may not be possible (since the file's
96 * size itself may not be a page multiple).
97 */
98 ds_file->mmap_offset += ds_file->mmap_len;
99 ds_file->request_offset = 0;
100 }
101
102 ds_file->mmap_len = MIN(ds_file->file->size - ds_file->mmap_offset,
103 ds_file->mmap_max_len);
104 if (ds_file->mmap_len == 0) {
105 ret = BT_MSG_ITER_MEDIUM_STATUS_EOF;
106 goto end;
107 }
108 /* Map new region */
109 BT_ASSERT(ds_file->mmap_len);
110 ds_file->mmap_addr = bt_mmap((void *) 0, ds_file->mmap_len,
111 PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp),
112 ds_file->mmap_offset, ds_file->log_level);
113 if (ds_file->mmap_addr == MAP_FAILED) {
114 BT_COMP_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s",
115 ds_file->mmap_len, ds_file->file->path->str,
116 ds_file->file->fp, (intmax_t) ds_file->mmap_offset,
117 strerror(errno));
118 goto error;
119 }
120
121 goto end;
122 error:
123 ds_file_munmap(ds_file);
124 ret = BT_MSG_ITER_MEDIUM_STATUS_ERROR;
125 end:
126 return ret;
127 }
128
129 static
130 enum bt_msg_iter_medium_status medop_request_bytes(
131 size_t request_sz, uint8_t **buffer_addr,
132 size_t *buffer_sz, void *data)
133 {
134 enum bt_msg_iter_medium_status status =
135 BT_MSG_ITER_MEDIUM_STATUS_OK;
136 struct ctf_fs_ds_file *ds_file = data;
137
138 if (request_sz == 0) {
139 goto end;
140 }
141
142 /*
143 * Check if we have at least one memory-mapped byte left. If we don't,
144 * mmap the next file.
145 */
146 if (remaining_mmap_bytes(ds_file) == 0) {
147 /* Are we at the end of the file? */
148 if (ds_file->mmap_offset >= ds_file->file->size) {
149 BT_COMP_LOGD("Reached end of file \"%s\" (%p)",
150 ds_file->file->path->str, ds_file->file->fp);
151 status = BT_MSG_ITER_MEDIUM_STATUS_EOF;
152 goto end;
153 }
154
155 status = ds_file_mmap_next(ds_file);
156 switch (status) {
157 case BT_MSG_ITER_MEDIUM_STATUS_OK:
158 break;
159 case BT_MSG_ITER_MEDIUM_STATUS_EOF:
160 goto end;
161 default:
162 BT_COMP_LOGE("Cannot memory-map next region of file \"%s\" (%p)",
163 ds_file->file->path->str,
164 ds_file->file->fp);
165 goto error;
166 }
167 }
168
169 *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz);
170 BT_ASSERT(ds_file->mmap_addr);
171 *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset;
172 ds_file->request_offset += *buffer_sz;
173 goto end;
174
175 error:
176 status = BT_MSG_ITER_MEDIUM_STATUS_ERROR;
177
178 end:
179 return status;
180 }
181
182 static
183 bt_stream *medop_borrow_stream(bt_stream_class *stream_class, int64_t stream_id,
184 void *data)
185 {
186 struct ctf_fs_ds_file *ds_file = data;
187 bt_stream_class *ds_file_stream_class;
188 bt_stream *stream = NULL;
189
190 ds_file_stream_class = bt_stream_borrow_class(
191 ds_file->stream);
192
193 if (stream_class != ds_file_stream_class) {
194 /*
195 * Not supported: two packets described by two different
196 * stream classes within the same data stream file.
197 */
198 goto end;
199 }
200
201 stream = ds_file->stream;
202
203 end:
204 return stream;
205 }
206
207 static
208 enum bt_msg_iter_medium_status medop_seek(enum bt_msg_iter_seek_whence whence,
209 off_t offset, void *data)
210 {
211 enum bt_msg_iter_medium_status ret =
212 BT_MSG_ITER_MEDIUM_STATUS_OK;
213 struct ctf_fs_ds_file *ds_file = data;
214 off_t offset_in_mapping, file_size = ds_file->file->size;
215
216 if (whence != BT_MSG_ITER_SEEK_WHENCE_SET ||
217 offset < 0 || offset > file_size) {
218 BT_COMP_LOGE("Invalid medium seek request: whence=%d, offset=%jd, "
219 "file-size=%jd", (int) whence, (intmax_t) offset,
220 (intmax_t) file_size);
221 ret = BT_MSG_ITER_MEDIUM_STATUS_INVAL;
222 goto end;
223 }
224
225 /* If there is no current mapping, map the right file directly. */
226 if (!ds_file->mmap_addr) {
227 goto map_requested_offset;
228 }
229
230 /*
231 * Determine whether or not the destination is contained within the
232 * current mapping.
233 */
234 if (offset < ds_file->mmap_offset ||
235 offset >= ds_file->mmap_offset + ds_file->mmap_len) {
236 int unmap_ret;
237 BT_COMP_LOGD("Medium seek request cannot be accomodated by the current "
238 "file mapping: offset=%jd, mmap-offset=%jd, "
239 "mmap-len=%zu", (intmax_t) offset, (intmax_t) ds_file->mmap_offset,
240 ds_file->mmap_len);
241 unmap_ret = ds_file_munmap(ds_file);
242 if (unmap_ret) {
243 ret = BT_MSG_ITER_MEDIUM_STATUS_ERROR;
244 goto end;
245 }
246 goto map_requested_offset;
247 } else {
248 ds_file->request_offset = offset - ds_file->mmap_offset;
249 goto test_end;
250 }
251
252 map_requested_offset:
253 offset_in_mapping = offset %
254 bt_common_get_page_size(ds_file->log_level);
255
256 ds_file->mmap_offset = offset - offset_in_mapping;
257 ds_file->request_offset = offset_in_mapping;
258 ret = ds_file_mmap_next(ds_file);
259 if (ret != BT_MSG_ITER_MEDIUM_STATUS_OK) {
260 goto end;
261 }
262
263 test_end:
264 ds_file->end_reached = (offset == file_size);
265 end:
266 return ret;
267 }
268
269 BT_HIDDEN
270 struct bt_msg_iter_medium_ops ctf_fs_ds_file_medops = {
271 .request_bytes = medop_request_bytes,
272 .borrow_stream = medop_borrow_stream,
273 .seek = medop_seek,
274 };
275
276 static
277 int convert_cycles_to_ns(struct ctf_clock_class *clock_class,
278 uint64_t cycles, int64_t *ns)
279 {
280 return bt_util_clock_cycles_to_ns_from_origin(cycles,
281 clock_class->frequency, clock_class->offset_seconds,
282 clock_class->offset_cycles, ns);
283 }
284
285 static
286 struct ctf_fs_ds_index *build_index_from_idx_file(
287 struct ctf_fs_ds_file *ds_file,
288 struct ctf_fs_ds_file_info *file_info)
289 {
290 int ret;
291 gchar *directory = NULL;
292 gchar *basename = NULL;
293 GString *index_basename = NULL;
294 gchar *index_file_path = NULL;
295 GMappedFile *mapped_file = NULL;
296 gsize filesize;
297 const char *mmap_begin = NULL, *file_pos = NULL;
298 const struct ctf_packet_index_file_hdr *header = NULL;
299 struct ctf_fs_ds_index *index = NULL;
300 struct ctf_fs_ds_index_entry *index_entry = NULL, *prev_index_entry = NULL;
301 uint64_t total_packets_size = 0;
302 size_t file_index_entry_size;
303 size_t file_entry_count;
304 size_t i;
305 struct ctf_stream_class *sc;
306 struct bt_msg_iter_packet_properties props;
307
308 BT_COMP_LOGI("Building index from .idx file of stream file %s",
309 ds_file->file->path->str);
310 ret = bt_msg_iter_get_packet_properties(ds_file->msg_iter, &props);
311 if (ret) {
312 BT_COMP_LOGI_STR("Cannot read first packet's header and context fields.");
313 goto error;
314 }
315
316 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
317 props.stream_class_id);
318 BT_ASSERT(sc);
319 if (!sc->default_clock_class) {
320 BT_COMP_LOGI_STR("Cannot find stream class's default clock class.");
321 goto error;
322 }
323
324 /* Look for index file in relative path index/name.idx. */
325 basename = g_path_get_basename(ds_file->file->path->str);
326 if (!basename) {
327 BT_COMP_LOGE("Cannot get the basename of datastream file %s",
328 ds_file->file->path->str);
329 goto error;
330 }
331
332 directory = g_path_get_dirname(ds_file->file->path->str);
333 if (!directory) {
334 BT_COMP_LOGE("Cannot get dirname of datastream file %s",
335 ds_file->file->path->str);
336 goto error;
337 }
338
339 index_basename = g_string_new(basename);
340 if (!index_basename) {
341 BT_COMP_LOGE_STR("Cannot allocate index file basename string");
342 goto error;
343 }
344
345 g_string_append(index_basename, ".idx");
346 index_file_path = g_build_filename(directory, "index",
347 index_basename->str, NULL);
348 mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL);
349 if (!mapped_file) {
350 BT_COMP_LOGD("Cannot create new mapped file %s",
351 index_file_path);
352 goto error;
353 }
354
355 /*
356 * The g_mapped_file API limits us to 4GB files on 32-bit.
357 * Traces with such large indexes have never been seen in the wild,
358 * but this would need to be adjusted to support them.
359 */
360 filesize = g_mapped_file_get_length(mapped_file);
361 if (filesize < sizeof(*header)) {
362 BT_COMP_LOGW("Invalid LTTng trace index file: "
363 "file size (%zu bytes) < header size (%zu bytes)",
364 filesize, sizeof(*header));
365 goto error;
366 }
367
368 mmap_begin = g_mapped_file_get_contents(mapped_file);
369 header = (struct ctf_packet_index_file_hdr *) mmap_begin;
370
371 file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header);
372 if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
373 BT_COMP_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
374 goto error;
375 }
376
377 file_index_entry_size = be32toh(header->packet_index_len);
378 file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
379 if ((filesize - sizeof(*header)) % file_index_entry_size) {
380 BT_COMP_LOGW("Invalid LTTng trace index: the index's size after the header "
381 "(%zu bytes) is not a multiple of the index entry size "
382 "(%zu bytes)", (filesize - sizeof(*header)),
383 sizeof(*header));
384 goto error;
385 }
386
387 index = ctf_fs_ds_index_create(ds_file->log_level, ds_file->self_comp);
388 if (!index) {
389 goto error;
390 }
391
392 for (i = 0; i < file_entry_count; i++) {
393 struct ctf_packet_index *file_index =
394 (struct ctf_packet_index *) file_pos;
395 uint64_t packet_size = be64toh(file_index->packet_size);
396
397 if (packet_size % CHAR_BIT) {
398 BT_COMP_LOGW("Invalid packet size encountered in LTTng trace index file");
399 goto error;
400 }
401
402 index_entry = g_new0(struct ctf_fs_ds_index_entry, 1);
403 if (!index_entry) {
404 goto error;
405 }
406
407 /* Set path to stream file. */
408 index_entry->path = file_info->path->str;
409
410 /* Convert size in bits to bytes. */
411 packet_size /= CHAR_BIT;
412 index_entry->packet_size = packet_size;
413
414 index_entry->offset = be64toh(file_index->offset);
415 if (i != 0 && index_entry->offset < prev_index_entry->offset) {
416 BT_COMP_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
417 "previous offset=%" PRIu64 ", current offset=%" PRIu64,
418 prev_index_entry->offset, index_entry->offset);
419 goto error;
420 }
421
422 index_entry->timestamp_begin = be64toh(file_index->timestamp_begin);
423 index_entry->timestamp_end = be64toh(file_index->timestamp_end);
424 if (index_entry->timestamp_end < index_entry->timestamp_begin) {
425 BT_COMP_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
426 "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64,
427 index_entry->timestamp_begin,
428 index_entry->timestamp_end);
429 goto error;
430 }
431
432 /* Convert the packet's bound to nanoseconds since Epoch. */
433 ret = convert_cycles_to_ns(sc->default_clock_class,
434 index_entry->timestamp_begin,
435 &index_entry->timestamp_begin_ns);
436 if (ret) {
437 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
438 goto error;
439 }
440 ret = convert_cycles_to_ns(sc->default_clock_class,
441 index_entry->timestamp_end,
442 &index_entry->timestamp_end_ns);
443 if (ret) {
444 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
445 goto error;
446 }
447
448 total_packets_size += packet_size;
449 file_pos += file_index_entry_size;
450
451 g_ptr_array_add(index->entries, index_entry);
452 prev_index_entry = index_entry;
453 }
454
455 /* Validate that the index addresses the complete stream. */
456 if (ds_file->file->size != total_packets_size) {
457 BT_COMP_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
458 "file-size=%" PRIu64 ", total-packets-size=%" PRIu64,
459 ds_file->file->size, total_packets_size);
460 goto error;
461 }
462 end:
463 g_free(directory);
464 g_free(basename);
465 g_free(index_file_path);
466 if (index_basename) {
467 g_string_free(index_basename, TRUE);
468 }
469 if (mapped_file) {
470 g_mapped_file_unref(mapped_file);
471 }
472 return index;
473 error:
474 ctf_fs_ds_index_destroy(index);
475 g_free(index_entry);
476 index = NULL;
477 goto end;
478 }
479
480 static
481 int init_index_entry(struct ctf_fs_ds_index_entry *entry,
482 struct ctf_fs_ds_file *ds_file,
483 struct bt_msg_iter_packet_properties *props,
484 off_t packet_size, off_t packet_offset)
485 {
486 int ret = 0;
487 struct ctf_stream_class *sc;
488
489 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
490 props->stream_class_id);
491 BT_ASSERT(sc);
492 BT_ASSERT(packet_offset >= 0);
493 entry->offset = packet_offset;
494 BT_ASSERT(packet_size >= 0);
495 entry->packet_size = packet_size;
496
497 if (props->snapshots.beginning_clock != UINT64_C(-1)) {
498 /* Convert the packet's bound to nanoseconds since Epoch. */
499 ret = convert_cycles_to_ns(sc->default_clock_class,
500 props->snapshots.beginning_clock,
501 &entry->timestamp_begin_ns);
502 if (ret) {
503 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
504 goto end;
505 }
506 } else {
507 entry->timestamp_begin_ns = UINT64_C(-1);
508 }
509
510 if (props->snapshots.end_clock != UINT64_C(-1)) {
511 ret = convert_cycles_to_ns(sc->default_clock_class,
512 props->snapshots.end_clock,
513 &entry->timestamp_end_ns);
514 if (ret) {
515 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
516 goto end;
517 }
518 } else {
519 entry->timestamp_end_ns = UINT64_C(-1);
520 }
521
522 end:
523 return ret;
524 }
525
526 static
527 struct ctf_fs_ds_index *build_index_from_stream_file(
528 struct ctf_fs_ds_file *ds_file,
529 struct ctf_fs_ds_file_info *file_info)
530 {
531 int ret;
532 struct ctf_fs_ds_index *index = NULL;
533 enum bt_msg_iter_status iter_status = BT_MSG_ITER_STATUS_OK;
534 off_t current_packet_offset_bytes = 0;
535
536 BT_COMP_LOGI("Indexing stream file %s", ds_file->file->path->str);
537
538 index = ctf_fs_ds_index_create(ds_file->log_level, ds_file->self_comp);
539 if (!index) {
540 goto error;
541 }
542
543 do {
544 off_t current_packet_size_bytes;
545 struct ctf_fs_ds_index_entry *index_entry;
546 struct bt_msg_iter_packet_properties props;
547
548 if (current_packet_offset_bytes < 0) {
549 BT_COMP_LOGE_STR("Cannot get the current packet's offset.");
550 goto error;
551 } else if (current_packet_offset_bytes > ds_file->file->size) {
552 BT_COMP_LOGE_STR("Unexpected current packet's offset (larger than file).");
553 goto error;
554 } else if (current_packet_offset_bytes == ds_file->file->size) {
555 /* No more data */
556 break;
557 }
558
559 iter_status = bt_msg_iter_seek(ds_file->msg_iter,
560 current_packet_offset_bytes);
561 if (iter_status != BT_MSG_ITER_STATUS_OK) {
562 goto error;
563 }
564
565 iter_status = bt_msg_iter_get_packet_properties(
566 ds_file->msg_iter, &props);
567 if (iter_status != BT_MSG_ITER_STATUS_OK) {
568 goto error;
569 }
570
571 if (props.exp_packet_total_size >= 0) {
572 current_packet_size_bytes =
573 (uint64_t) props.exp_packet_total_size / 8;
574 } else {
575 current_packet_size_bytes = ds_file->file->size;
576 }
577
578 if (current_packet_offset_bytes + current_packet_size_bytes >
579 ds_file->file->size) {
580 BT_COMP_LOGW("Invalid packet size reported in file: stream=\"%s\", "
581 "packet-offset=%jd, packet-size-bytes=%jd, "
582 "file-size=%jd",
583 ds_file->file->path->str,
584 (intmax_t) current_packet_offset_bytes,
585 (intmax_t) current_packet_size_bytes,
586 (intmax_t) ds_file->file->size);
587 goto error;
588 }
589
590 index_entry = g_new0(struct ctf_fs_ds_index_entry, 1);
591 if (!index_entry) {
592 BT_COMP_LOGE_STR("Failed to allocate a new index entry.");
593 goto error;
594 }
595
596 /* Set path to stream file. */
597 index_entry->path = file_info->path->str;
598
599 ret = init_index_entry(index_entry, ds_file, &props,
600 current_packet_size_bytes, current_packet_offset_bytes);
601 if (ret) {
602 g_free(index_entry);
603 goto error;
604 }
605
606 g_ptr_array_add(index->entries, index_entry);
607
608 current_packet_offset_bytes += current_packet_size_bytes;
609 BT_COMP_LOGD("Seeking to next packet: current-packet-offset=%jd, "
610 "next-packet-offset=%jd",
611 (intmax_t) (current_packet_offset_bytes - current_packet_size_bytes),
612 (intmax_t) current_packet_offset_bytes);
613
614 } while (iter_status == BT_MSG_ITER_STATUS_OK);
615
616 if (iter_status != BT_MSG_ITER_STATUS_OK) {
617 goto error;
618 }
619
620 end:
621 return index;
622
623 error:
624 ctf_fs_ds_index_destroy(index);
625 index = NULL;
626 goto end;
627 }
628
629 BT_HIDDEN
630 struct ctf_fs_ds_file *ctf_fs_ds_file_create(
631 struct ctf_fs_trace *ctf_fs_trace,
632 bt_self_message_iterator *pc_msg_iter,
633 struct bt_msg_iter *msg_iter,
634 bt_stream *stream, const char *path,
635 bt_logging_level log_level)
636 {
637 int ret;
638 const size_t page_size = bt_common_get_page_size(log_level);
639 struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1);
640
641 if (!ds_file) {
642 goto error;
643 }
644
645 ds_file->log_level = log_level;
646 ds_file->self_comp = ctf_fs_trace->self_comp;
647 ds_file->pc_msg_iter = pc_msg_iter;
648 ds_file->file = ctf_fs_file_create(log_level, ds_file->self_comp);
649 if (!ds_file->file) {
650 goto error;
651 }
652
653 ds_file->stream = stream;
654 bt_stream_get_ref(ds_file->stream);
655 ds_file->metadata = ctf_fs_trace->metadata;
656 g_string_assign(ds_file->file->path, path);
657 ret = ctf_fs_file_open(ds_file->file, "rb");
658 if (ret) {
659 goto error;
660 }
661
662 ds_file->msg_iter = msg_iter;
663 bt_msg_iter_set_medops_data(ds_file->msg_iter, ds_file);
664 if (!ds_file->msg_iter) {
665 goto error;
666 }
667
668 ds_file->mmap_max_len = page_size * 2048;
669
670 goto end;
671
672 error:
673 /* Do not touch "borrowed" file. */
674 ctf_fs_ds_file_destroy(ds_file);
675 ds_file = NULL;
676
677 end:
678 return ds_file;
679 }
680
681 BT_HIDDEN
682 struct ctf_fs_ds_index *ctf_fs_ds_file_build_index(
683 struct ctf_fs_ds_file *ds_file,
684 struct ctf_fs_ds_file_info *file_info)
685 {
686 struct ctf_fs_ds_index *index;
687
688 index = build_index_from_idx_file(ds_file, file_info);
689 if (index) {
690 goto end;
691 }
692
693 BT_COMP_LOGI("Failed to build index from .index file; "
694 "falling back to stream indexing.");
695 index = build_index_from_stream_file(ds_file, file_info);
696 end:
697 return index;
698 }
699
700 BT_HIDDEN
701 struct ctf_fs_ds_index *ctf_fs_ds_index_create(bt_logging_level log_level,
702 bt_self_component *self_comp)
703 {
704 struct ctf_fs_ds_index *index = g_new0(struct ctf_fs_ds_index, 1);
705
706 if (!index) {
707 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
708 "Failed to allocate index");
709 goto error;
710 }
711
712 index->entries = g_ptr_array_new_with_free_func((GDestroyNotify) g_free);
713 if (!index->entries) {
714 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
715 "Failed to allocate index entries.");
716 goto error;
717 }
718
719 goto end;
720
721 error:
722 ctf_fs_ds_index_destroy(index);
723 index = NULL;
724 end:
725 return index;
726 }
727
728 BT_HIDDEN
729 void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file)
730 {
731 if (!ds_file) {
732 return;
733 }
734
735 bt_stream_put_ref(ds_file->stream);
736 (void) ds_file_munmap(ds_file);
737
738 if (ds_file->file) {
739 ctf_fs_file_destroy(ds_file->file);
740 }
741
742 g_free(ds_file);
743 }
744
745 BT_HIDDEN
746 bt_component_class_message_iterator_next_method_status ctf_fs_ds_file_next(
747 struct ctf_fs_ds_file *ds_file,
748 bt_message **msg)
749 {
750 enum bt_msg_iter_status msg_iter_status;
751 bt_component_class_message_iterator_next_method_status status;
752
753 msg_iter_status = bt_msg_iter_get_next_message(
754 ds_file->msg_iter, ds_file->pc_msg_iter, msg);
755
756 switch (msg_iter_status) {
757 case BT_MSG_ITER_STATUS_EOF:
758 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END;
759 break;
760 case BT_MSG_ITER_STATUS_OK:
761 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
762 break;
763 case BT_MSG_ITER_STATUS_AGAIN:
764 /*
765 * Should not make it this far as this is
766 * medium-specific; there is nothing for the user to do
767 * and it should have been handled upstream.
768 */
769 abort();
770 case BT_MSG_ITER_STATUS_INVAL:
771 case BT_MSG_ITER_STATUS_ERROR:
772 default:
773 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
774 break;
775 }
776 return status;
777 }
778
779 BT_HIDDEN
780 void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index)
781 {
782 if (!index) {
783 return;
784 }
785
786 if (index->entries) {
787 g_ptr_array_free(index->entries, TRUE);
788 }
789 g_free(index);
790 }
This page took 0.045921 seconds and 5 git commands to generate.