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
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #define BT_COMP_LOG_SELF_COMP (self_comp)
26 #define BT_LOG_OUTPUT_LEVEL (log_level)
27 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/DS"
28 #include "logging/comp-logging.h"
36 #include "compat/mman.h"
37 #include "compat/endian.h"
38 #include <babeltrace2/babeltrace.h>
39 #include "common/common.h"
42 #include "../common/msg-iter/msg-iter.h"
43 #include "common/assert.h"
44 #include "data-stream-file.h"
48 size_t remaining_mmap_bytes(struct ctf_fs_ds_file
*ds_file
)
50 BT_ASSERT_DBG(ds_file
->mmap_len
>= ds_file
->request_offset_in_mapping
);
51 return ds_file
->mmap_len
- ds_file
->request_offset_in_mapping
;
55 int ds_file_munmap(struct ctf_fs_ds_file
*ds_file
)
58 bt_self_component
*self_comp
= ds_file
->self_comp
;
59 bt_logging_level log_level
= ds_file
->log_level
;
63 if (!ds_file
->mmap_addr
) {
67 if (bt_munmap(ds_file
->mmap_addr
, ds_file
->mmap_len
)) {
68 BT_COMP_LOGE_ERRNO("Cannot memory-unmap file",
69 ": address=%p, size=%zu, file_path=\"%s\", file=%p",
70 ds_file
->mmap_addr
, ds_file
->mmap_len
,
71 ds_file
->file
? ds_file
->file
->path
->str
: "NULL",
72 ds_file
->file
? ds_file
->file
->fp
: NULL
);
77 ds_file
->mmap_addr
= NULL
;
84 enum ctf_msg_iter_medium_status
ds_file_mmap_next(
85 struct ctf_fs_ds_file
*ds_file
)
87 enum ctf_msg_iter_medium_status ret
=
88 CTF_MSG_ITER_MEDIUM_STATUS_OK
;
89 bt_self_component
*self_comp
= ds_file
->self_comp
;
90 bt_logging_level log_level
= ds_file
->log_level
;
92 /* Unmap old region */
93 if (ds_file
->mmap_addr
) {
94 if (ds_file_munmap(ds_file
)) {
99 * mmap_len is guaranteed to be page-aligned except on the
100 * last mapping where it may not be possible (since the file's
101 * size itself may not be a page multiple).
103 ds_file
->mmap_offset_in_file
+= ds_file
->mmap_len
;
104 ds_file
->request_offset_in_mapping
= 0;
107 ds_file
->mmap_len
= MIN(ds_file
->file
->size
- ds_file
->mmap_offset_in_file
,
108 ds_file
->mmap_max_len
);
109 if (ds_file
->mmap_len
== 0) {
110 ret
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
114 BT_ASSERT(ds_file
->mmap_len
);
115 ds_file
->mmap_addr
= bt_mmap((void *) 0, ds_file
->mmap_len
,
116 PROT_READ
, MAP_PRIVATE
, fileno(ds_file
->file
->fp
),
117 ds_file
->mmap_offset_in_file
, ds_file
->log_level
);
118 if (ds_file
->mmap_addr
== MAP_FAILED
) {
119 BT_COMP_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s",
120 ds_file
->mmap_len
, ds_file
->file
->path
->str
,
121 ds_file
->file
->fp
, (intmax_t) ds_file
->mmap_offset_in_file
,
128 ret
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
134 enum ctf_msg_iter_medium_status
medop_request_bytes(
135 size_t request_sz
, uint8_t **buffer_addr
,
136 size_t *buffer_sz
, void *data
)
138 enum ctf_msg_iter_medium_status status
=
139 CTF_MSG_ITER_MEDIUM_STATUS_OK
;
140 struct ctf_fs_ds_file
*ds_file
= data
;
141 bt_self_component
*self_comp
= ds_file
->self_comp
;
142 bt_logging_level log_level
= ds_file
->log_level
;
144 if (request_sz
== 0) {
149 * Check if we have at least one memory-mapped byte left. If we don't,
150 * mmap the next file.
152 if (remaining_mmap_bytes(ds_file
) == 0) {
153 /* Are we at the end of the file? */
154 if (ds_file
->mmap_offset_in_file
>= ds_file
->file
->size
) {
155 BT_COMP_LOGD("Reached end of file \"%s\" (%p)",
156 ds_file
->file
->path
->str
, ds_file
->file
->fp
);
157 status
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
161 status
= ds_file_mmap_next(ds_file
);
163 case CTF_MSG_ITER_MEDIUM_STATUS_OK
:
165 case CTF_MSG_ITER_MEDIUM_STATUS_EOF
:
168 BT_COMP_LOGE("Cannot memory-map next region of file \"%s\" (%p)",
169 ds_file
->file
->path
->str
,
175 *buffer_sz
= MIN(remaining_mmap_bytes(ds_file
), request_sz
);
176 BT_ASSERT(ds_file
->mmap_addr
);
177 *buffer_addr
= ((uint8_t *) ds_file
->mmap_addr
) + ds_file
->request_offset_in_mapping
;
178 ds_file
->request_offset_in_mapping
+= *buffer_sz
;
182 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
189 bt_stream
*medop_borrow_stream(bt_stream_class
*stream_class
, int64_t stream_id
,
192 struct ctf_fs_ds_file
*ds_file
= data
;
193 bt_stream_class
*ds_file_stream_class
;
194 bt_stream
*stream
= NULL
;
196 ds_file_stream_class
= bt_stream_borrow_class(
199 if (stream_class
!= ds_file_stream_class
) {
201 * Not supported: two packets described by two different
202 * stream classes within the same data stream file.
207 stream
= ds_file
->stream
;
214 enum ctf_msg_iter_medium_status
medop_seek(enum ctf_msg_iter_seek_whence whence
,
215 off_t offset
, void *data
)
217 enum ctf_msg_iter_medium_status ret
=
218 CTF_MSG_ITER_MEDIUM_STATUS_OK
;
219 struct ctf_fs_ds_file
*ds_file
= data
;
220 off_t offset_in_mapping
, file_size
= ds_file
->file
->size
;
221 bt_self_component
*self_comp
= ds_file
->self_comp
;
222 bt_logging_level log_level
= ds_file
->log_level
;
224 if (whence
!= CTF_MSG_ITER_SEEK_WHENCE_SET
||
225 offset
< 0 || offset
> file_size
) {
226 BT_COMP_LOGE("Invalid medium seek request: whence=%d, offset=%jd, "
227 "file-size=%jd", (int) whence
, (intmax_t) offset
,
228 (intmax_t) file_size
);
229 ret
= CTF_MSG_ITER_MEDIUM_STATUS_INVAL
;
233 /* If there is no current mapping, map the right file directly. */
234 if (!ds_file
->mmap_addr
) {
235 goto map_requested_offset
;
239 * Determine whether or not the destination is contained within the
242 if (offset
< ds_file
->mmap_offset_in_file
||
243 offset
>= ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
) {
245 BT_COMP_LOGD("Medium seek request cannot be accomodated by the current "
246 "file mapping: offset=%jd, mmap-offset=%jd, "
247 "mmap-len=%zu", (intmax_t) offset
, (intmax_t) ds_file
->mmap_offset_in_file
,
249 unmap_ret
= ds_file_munmap(ds_file
);
251 ret
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
254 goto map_requested_offset
;
256 ds_file
->request_offset_in_mapping
= offset
- ds_file
->mmap_offset_in_file
;
260 map_requested_offset
:
261 offset_in_mapping
= offset
%
262 bt_mmap_get_offset_align_size(ds_file
->log_level
);
264 ds_file
->mmap_offset_in_file
= offset
- offset_in_mapping
;
265 ds_file
->request_offset_in_mapping
= offset_in_mapping
;
266 ret
= ds_file_mmap_next(ds_file
);
267 if (ret
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
272 ds_file
->end_reached
= (offset
== file_size
);
278 struct ctf_msg_iter_medium_ops ctf_fs_ds_file_medops
= {
279 .request_bytes
= medop_request_bytes
,
280 .borrow_stream
= medop_borrow_stream
,
285 struct ctf_fs_ds_index_entry
*ctf_fs_ds_index_entry_create(
286 bt_self_component
*self_comp
, bt_logging_level log_level
)
288 struct ctf_fs_ds_index_entry
*entry
;
290 entry
= g_new0(struct ctf_fs_ds_index_entry
, 1);
292 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to allocate a ctf_fs_ds_index_entry.");
296 entry
->packet_seq_num
= UINT64_MAX
;
303 int convert_cycles_to_ns(struct ctf_clock_class
*clock_class
,
304 uint64_t cycles
, int64_t *ns
)
306 return bt_util_clock_cycles_to_ns_from_origin(cycles
,
307 clock_class
->frequency
, clock_class
->offset_seconds
,
308 clock_class
->offset_cycles
, ns
);
312 struct ctf_fs_ds_index
*build_index_from_idx_file(
313 struct ctf_fs_ds_file
*ds_file
,
314 struct ctf_fs_ds_file_info
*file_info
,
315 struct ctf_msg_iter
*msg_iter
)
318 gchar
*directory
= NULL
;
319 gchar
*basename
= NULL
;
320 GString
*index_basename
= NULL
;
321 gchar
*index_file_path
= NULL
;
322 GMappedFile
*mapped_file
= NULL
;
324 const char *mmap_begin
= NULL
, *file_pos
= NULL
;
325 const struct ctf_packet_index_file_hdr
*header
= NULL
;
326 struct ctf_fs_ds_index
*index
= NULL
;
327 struct ctf_fs_ds_index_entry
*index_entry
= NULL
, *prev_index_entry
= NULL
;
328 uint64_t total_packets_size
= 0;
329 size_t file_index_entry_size
;
330 size_t file_entry_count
;
332 struct ctf_stream_class
*sc
;
333 struct ctf_msg_iter_packet_properties props
;
334 uint32_t version_major
, version_minor
;
335 bt_self_component
*self_comp
= ds_file
->self_comp
;
336 bt_logging_level log_level
= ds_file
->log_level
;
338 BT_COMP_LOGI("Building index from .idx file of stream file %s",
339 ds_file
->file
->path
->str
);
340 ret
= ctf_msg_iter_get_packet_properties(msg_iter
, &props
);
342 BT_COMP_LOGI_STR("Cannot read first packet's header and context fields.");
346 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
,
347 props
.stream_class_id
);
349 if (!sc
->default_clock_class
) {
350 BT_COMP_LOGI_STR("Cannot find stream class's default clock class.");
354 /* Look for index file in relative path index/name.idx. */
355 basename
= g_path_get_basename(ds_file
->file
->path
->str
);
357 BT_COMP_LOGE("Cannot get the basename of datastream file %s",
358 ds_file
->file
->path
->str
);
362 directory
= g_path_get_dirname(ds_file
->file
->path
->str
);
364 BT_COMP_LOGE("Cannot get dirname of datastream file %s",
365 ds_file
->file
->path
->str
);
369 index_basename
= g_string_new(basename
);
370 if (!index_basename
) {
371 BT_COMP_LOGE_STR("Cannot allocate index file basename string");
375 g_string_append(index_basename
, ".idx");
376 index_file_path
= g_build_filename(directory
, "index",
377 index_basename
->str
, NULL
);
378 mapped_file
= g_mapped_file_new(index_file_path
, FALSE
, NULL
);
380 BT_COMP_LOGD("Cannot create new mapped file %s",
386 * The g_mapped_file API limits us to 4GB files on 32-bit.
387 * Traces with such large indexes have never been seen in the wild,
388 * but this would need to be adjusted to support them.
390 filesize
= g_mapped_file_get_length(mapped_file
);
391 if (filesize
< sizeof(*header
)) {
392 BT_COMP_LOGW("Invalid LTTng trace index file: "
393 "file size (%zu bytes) < header size (%zu bytes)",
394 filesize
, sizeof(*header
));
398 mmap_begin
= g_mapped_file_get_contents(mapped_file
);
399 header
= (struct ctf_packet_index_file_hdr
*) mmap_begin
;
401 file_pos
= g_mapped_file_get_contents(mapped_file
) + sizeof(*header
);
402 if (be32toh(header
->magic
) != CTF_INDEX_MAGIC
) {
403 BT_COMP_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
407 version_major
= be32toh(header
->index_major
);
408 version_minor
= be32toh(header
->index_minor
);
409 if (version_major
!= 1) {
411 "Unknown LTTng trace index version: "
412 "major=%" PRIu32
", minor=%" PRIu32
,
413 version_major
, version_minor
);
417 file_index_entry_size
= be32toh(header
->packet_index_len
);
418 file_entry_count
= (filesize
- sizeof(*header
)) / file_index_entry_size
;
419 if ((filesize
- sizeof(*header
)) % file_index_entry_size
) {
420 BT_COMP_LOGW("Invalid LTTng trace index: the index's size after the header "
421 "(%zu bytes) is not a multiple of the index entry size "
422 "(%zu bytes)", (filesize
- sizeof(*header
)),
427 index
= ctf_fs_ds_index_create(ds_file
->log_level
, ds_file
->self_comp
);
432 for (i
= 0; i
< file_entry_count
; i
++) {
433 struct ctf_packet_index
*file_index
=
434 (struct ctf_packet_index
*) file_pos
;
435 uint64_t packet_size
= be64toh(file_index
->packet_size
);
437 if (packet_size
% CHAR_BIT
) {
438 BT_COMP_LOGW("Invalid packet size encountered in LTTng trace index file");
442 index_entry
= ctf_fs_ds_index_entry_create(
443 ds_file
->self_comp
, ds_file
->log_level
);
445 BT_COMP_LOGE_APPEND_CAUSE(ds_file
->self_comp
,
446 "Failed to create a ctf_fs_ds_index_entry.");
450 /* Set path to stream file. */
451 index_entry
->path
= file_info
->path
->str
;
453 /* Convert size in bits to bytes. */
454 packet_size
/= CHAR_BIT
;
455 index_entry
->packet_size
= packet_size
;
457 index_entry
->offset
= be64toh(file_index
->offset
);
458 if (i
!= 0 && index_entry
->offset
< prev_index_entry
->offset
) {
459 BT_COMP_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
460 "previous offset=%" PRIu64
", current offset=%" PRIu64
,
461 prev_index_entry
->offset
, index_entry
->offset
);
465 index_entry
->timestamp_begin
= be64toh(file_index
->timestamp_begin
);
466 index_entry
->timestamp_end
= be64toh(file_index
->timestamp_end
);
467 if (index_entry
->timestamp_end
< index_entry
->timestamp_begin
) {
468 BT_COMP_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
469 "timestamp_begin=%" PRIu64
"timestamp_end=%" PRIu64
,
470 index_entry
->timestamp_begin
,
471 index_entry
->timestamp_end
);
475 /* Convert the packet's bound to nanoseconds since Epoch. */
476 ret
= convert_cycles_to_ns(sc
->default_clock_class
,
477 index_entry
->timestamp_begin
,
478 &index_entry
->timestamp_begin_ns
);
480 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
483 ret
= convert_cycles_to_ns(sc
->default_clock_class
,
484 index_entry
->timestamp_end
,
485 &index_entry
->timestamp_end_ns
);
487 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
491 if (version_minor
>= 1) {
492 index_entry
->packet_seq_num
= be64toh(file_index
->packet_seq_num
);
495 total_packets_size
+= packet_size
;
496 file_pos
+= file_index_entry_size
;
498 prev_index_entry
= index_entry
;
500 /* Give ownership of `index_entry` to `index->entries`. */
501 g_ptr_array_add(index
->entries
, index_entry
);
505 /* Validate that the index addresses the complete stream. */
506 if (ds_file
->file
->size
!= total_packets_size
) {
507 BT_COMP_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
508 "file-size=%" PRIu64
", total-packets-size=%" PRIu64
,
509 ds_file
->file
->size
, total_packets_size
);
515 g_free(index_file_path
);
516 if (index_basename
) {
517 g_string_free(index_basename
, TRUE
);
520 g_mapped_file_unref(mapped_file
);
524 ctf_fs_ds_index_destroy(index
);
531 int init_index_entry(struct ctf_fs_ds_index_entry
*entry
,
532 struct ctf_fs_ds_file
*ds_file
,
533 struct ctf_msg_iter_packet_properties
*props
,
534 off_t packet_size
, off_t packet_offset
)
537 struct ctf_stream_class
*sc
;
538 bt_self_component
*self_comp
= ds_file
->self_comp
;
539 bt_logging_level log_level
= ds_file
->log_level
;
541 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
,
542 props
->stream_class_id
);
544 BT_ASSERT(packet_offset
>= 0);
545 entry
->offset
= packet_offset
;
546 BT_ASSERT(packet_size
>= 0);
547 entry
->packet_size
= packet_size
;
549 if (props
->snapshots
.beginning_clock
!= UINT64_C(-1)) {
550 entry
->timestamp_begin
= props
->snapshots
.beginning_clock
;
552 /* Convert the packet's bound to nanoseconds since Epoch. */
553 ret
= convert_cycles_to_ns(sc
->default_clock_class
,
554 props
->snapshots
.beginning_clock
,
555 &entry
->timestamp_begin_ns
);
557 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
561 entry
->timestamp_begin
= UINT64_C(-1);
562 entry
->timestamp_begin_ns
= UINT64_C(-1);
565 if (props
->snapshots
.end_clock
!= UINT64_C(-1)) {
566 entry
->timestamp_end
= props
->snapshots
.end_clock
;
568 /* Convert the packet's bound to nanoseconds since Epoch. */
569 ret
= convert_cycles_to_ns(sc
->default_clock_class
,
570 props
->snapshots
.end_clock
,
571 &entry
->timestamp_end_ns
);
573 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
577 entry
->timestamp_end
= UINT64_C(-1);
578 entry
->timestamp_end_ns
= UINT64_C(-1);
586 struct ctf_fs_ds_index
*build_index_from_stream_file(
587 struct ctf_fs_ds_file
*ds_file
,
588 struct ctf_fs_ds_file_info
*file_info
,
589 struct ctf_msg_iter
*msg_iter
)
592 struct ctf_fs_ds_index
*index
= NULL
;
593 enum ctf_msg_iter_status iter_status
= CTF_MSG_ITER_STATUS_OK
;
594 off_t current_packet_offset_bytes
= 0;
595 bt_self_component
*self_comp
= ds_file
->self_comp
;
596 bt_logging_level log_level
= ds_file
->log_level
;
598 BT_COMP_LOGI("Indexing stream file %s", ds_file
->file
->path
->str
);
600 index
= ctf_fs_ds_index_create(ds_file
->log_level
, ds_file
->self_comp
);
606 off_t current_packet_size_bytes
;
607 struct ctf_fs_ds_index_entry
*index_entry
;
608 struct ctf_msg_iter_packet_properties props
;
610 if (current_packet_offset_bytes
< 0) {
611 BT_COMP_LOGE_STR("Cannot get the current packet's offset.");
613 } else if (current_packet_offset_bytes
> ds_file
->file
->size
) {
614 BT_COMP_LOGE_STR("Unexpected current packet's offset (larger than file).");
616 } else if (current_packet_offset_bytes
== ds_file
->file
->size
) {
621 iter_status
= ctf_msg_iter_seek(msg_iter
,
622 current_packet_offset_bytes
);
623 if (iter_status
!= CTF_MSG_ITER_STATUS_OK
) {
627 iter_status
= ctf_msg_iter_get_packet_properties(
629 if (iter_status
!= CTF_MSG_ITER_STATUS_OK
) {
633 if (props
.exp_packet_total_size
>= 0) {
634 current_packet_size_bytes
=
635 (uint64_t) props
.exp_packet_total_size
/ 8;
637 current_packet_size_bytes
= ds_file
->file
->size
;
640 if (current_packet_offset_bytes
+ current_packet_size_bytes
>
641 ds_file
->file
->size
) {
642 BT_COMP_LOGW("Invalid packet size reported in file: stream=\"%s\", "
643 "packet-offset=%jd, packet-size-bytes=%jd, "
645 ds_file
->file
->path
->str
,
646 (intmax_t) current_packet_offset_bytes
,
647 (intmax_t) current_packet_size_bytes
,
648 (intmax_t) ds_file
->file
->size
);
652 index_entry
= ctf_fs_ds_index_entry_create(
653 ds_file
->self_comp
, ds_file
->log_level
);
655 BT_COMP_LOGE_APPEND_CAUSE(ds_file
->self_comp
,
656 "Failed to create a ctf_fs_ds_index_entry.");
660 /* Set path to stream file. */
661 index_entry
->path
= file_info
->path
->str
;
663 ret
= init_index_entry(index_entry
, ds_file
, &props
,
664 current_packet_size_bytes
, current_packet_offset_bytes
);
670 g_ptr_array_add(index
->entries
, index_entry
);
672 current_packet_offset_bytes
+= current_packet_size_bytes
;
673 BT_COMP_LOGD("Seeking to next packet: current-packet-offset=%jd, "
674 "next-packet-offset=%jd",
675 (intmax_t) (current_packet_offset_bytes
- current_packet_size_bytes
),
676 (intmax_t) current_packet_offset_bytes
);
683 ctf_fs_ds_index_destroy(index
);
689 struct ctf_fs_ds_file
*ctf_fs_ds_file_create(
690 struct ctf_fs_trace
*ctf_fs_trace
,
691 bt_self_message_iterator
*self_msg_iter
,
692 bt_stream
*stream
, const char *path
,
693 bt_logging_level log_level
)
696 const size_t offset_align
= bt_mmap_get_offset_align_size(log_level
);
697 struct ctf_fs_ds_file
*ds_file
= g_new0(struct ctf_fs_ds_file
, 1);
703 ds_file
->log_level
= log_level
;
704 ds_file
->self_comp
= ctf_fs_trace
->self_comp
;
705 ds_file
->self_msg_iter
= self_msg_iter
;
706 ds_file
->file
= ctf_fs_file_create(log_level
, ds_file
->self_comp
);
707 if (!ds_file
->file
) {
711 ds_file
->stream
= stream
;
712 bt_stream_get_ref(ds_file
->stream
);
713 ds_file
->metadata
= ctf_fs_trace
->metadata
;
714 g_string_assign(ds_file
->file
->path
, path
);
715 ret
= ctf_fs_file_open(ds_file
->file
, "rb");
720 ds_file
->mmap_max_len
= offset_align
* 2048;
725 /* Do not touch "borrowed" file. */
726 ctf_fs_ds_file_destroy(ds_file
);
734 struct ctf_fs_ds_index
*ctf_fs_ds_file_build_index(
735 struct ctf_fs_ds_file
*ds_file
,
736 struct ctf_fs_ds_file_info
*file_info
,
737 struct ctf_msg_iter
*msg_iter
)
739 struct ctf_fs_ds_index
*index
;
740 bt_self_component
*self_comp
= ds_file
->self_comp
;
741 bt_logging_level log_level
= ds_file
->log_level
;
743 index
= build_index_from_idx_file(ds_file
, file_info
, msg_iter
);
748 BT_COMP_LOGI("Failed to build index from .index file; "
749 "falling back to stream indexing.");
750 index
= build_index_from_stream_file(ds_file
, file_info
, msg_iter
);
756 struct ctf_fs_ds_index
*ctf_fs_ds_index_create(bt_logging_level log_level
,
757 bt_self_component
*self_comp
)
759 struct ctf_fs_ds_index
*index
= g_new0(struct ctf_fs_ds_index
, 1);
762 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
763 "Failed to allocate index");
767 index
->entries
= g_ptr_array_new_with_free_func((GDestroyNotify
) g_free
);
768 if (!index
->entries
) {
769 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
770 "Failed to allocate index entries.");
777 ctf_fs_ds_index_destroy(index
);
784 void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file
*ds_file
)
790 bt_stream_put_ref(ds_file
->stream
);
791 (void) ds_file_munmap(ds_file
);
794 ctf_fs_file_destroy(ds_file
->file
);
801 void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index
*index
)
807 if (index
->entries
) {
808 g_ptr_array_free(index
->entries
, TRUE
);