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 * Return true if `offset_in_file` is in the current mapping.
59 bool offset_ist_mapped(struct ctf_fs_ds_file
*ds_file
, off_t offset_in_file
)
61 return offset_in_file
>= ds_file
->mmap_offset_in_file
&&
62 offset_in_file
< (ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
);
66 enum ctf_msg_iter_medium_status
ds_file_munmap(
67 struct ctf_fs_ds_file
*ds_file
)
69 enum ctf_msg_iter_medium_status status
;
70 bt_self_component
*self_comp
= ds_file
->self_comp
;
71 bt_logging_level log_level
= ds_file
->log_level
;
75 if (!ds_file
->mmap_addr
) {
76 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
80 if (bt_munmap(ds_file
->mmap_addr
, ds_file
->mmap_len
)) {
81 BT_COMP_LOGE_ERRNO("Cannot memory-unmap file",
82 ": address=%p, size=%zu, file_path=\"%s\", file=%p",
83 ds_file
->mmap_addr
, ds_file
->mmap_len
,
84 ds_file
->file
? ds_file
->file
->path
->str
: "NULL",
85 ds_file
->file
? ds_file
->file
->fp
: NULL
);
86 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
90 ds_file
->mmap_addr
= NULL
;
92 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
98 * mmap a region of `ds_file` such that `requested_offset_in_file` is in the
99 * mapping. If the currently mmap-ed region already contains
100 * `requested_offset_in_file`, the mapping is kept.
102 * Set `ds_file->requested_offset_in_mapping` based on `request_offset_in_file`
104 * `requested_offset_in_file` must be a valid offset in the file.
107 enum ctf_msg_iter_medium_status
ds_file_mmap(
108 struct ctf_fs_ds_file
*ds_file
, off_t requested_offset_in_file
)
110 enum ctf_msg_iter_medium_status status
;
111 bt_self_component
*self_comp
= ds_file
->self_comp
;
112 bt_logging_level log_level
= ds_file
->log_level
;
114 /* Ensure the requested offset is in the file range. */
115 BT_ASSERT(requested_offset_in_file
>= 0);
116 BT_ASSERT(requested_offset_in_file
< ds_file
->file
->size
);
119 * If the mapping already contains the requested offset, just adjust
120 * requested_offset_in_mapping.
122 if (offset_ist_mapped(ds_file
, requested_offset_in_file
)) {
123 ds_file
->request_offset_in_mapping
=
124 requested_offset_in_file
- ds_file
->mmap_offset_in_file
;
125 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
129 /* Unmap old region */
130 status
= ds_file_munmap(ds_file
);
131 if (status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
136 * Compute a mapping that has the required alignment properties and
137 * contains `requested_offset_in_file`.
139 ds_file
->request_offset_in_mapping
=
140 requested_offset_in_file
% bt_mmap_get_offset_align_size(ds_file
->log_level
);
141 ds_file
->mmap_offset_in_file
=
142 requested_offset_in_file
- ds_file
->request_offset_in_mapping
;
143 ds_file
->mmap_len
= MIN(ds_file
->file
->size
- ds_file
->mmap_offset_in_file
,
144 ds_file
->mmap_max_len
);
146 BT_ASSERT(ds_file
->mmap_len
> 0);
148 ds_file
->mmap_addr
= bt_mmap((void *) 0, ds_file
->mmap_len
,
149 PROT_READ
, MAP_PRIVATE
, fileno(ds_file
->file
->fp
),
150 ds_file
->mmap_offset_in_file
, ds_file
->log_level
);
151 if (ds_file
->mmap_addr
== MAP_FAILED
) {
152 BT_COMP_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s",
153 ds_file
->mmap_len
, ds_file
->file
->path
->str
,
154 ds_file
->file
->fp
, (intmax_t) ds_file
->mmap_offset_in_file
,
156 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
160 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
167 * Change the mapping of the file to read the region that follows the current
170 * If the file hasn't been mapped yet, then everything (mmap_offset_in_file,
171 * mmap_len, request_offset_in_mapping) should have the value 0, which will
172 * result in the beginning of the file getting mapped.
174 * return _EOF if the current mapping is the end of the file.
178 enum ctf_msg_iter_medium_status
ds_file_mmap_next(
179 struct ctf_fs_ds_file
*ds_file
)
181 enum ctf_msg_iter_medium_status status
;
184 * If we're called, it's because more bytes are requested but we have
185 * given all the bytes of the current mapping.
187 BT_ASSERT(ds_file
->request_offset_in_mapping
== ds_file
->mmap_len
);
190 * If the current mapping coincides with the end of the file, there is
193 if (ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
== ds_file
->file
->size
) {
194 status
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
198 status
= ds_file_mmap(ds_file
,
199 ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
);
206 enum ctf_msg_iter_medium_status
medop_request_bytes(
207 size_t request_sz
, uint8_t **buffer_addr
,
208 size_t *buffer_sz
, void *data
)
210 enum ctf_msg_iter_medium_status status
=
211 CTF_MSG_ITER_MEDIUM_STATUS_OK
;
212 struct ctf_fs_ds_file
*ds_file
= data
;
213 bt_self_component
*self_comp
= ds_file
->self_comp
;
214 bt_logging_level log_level
= ds_file
->log_level
;
216 BT_ASSERT(request_sz
> 0);
219 * Check if we have at least one memory-mapped byte left. If we don't,
220 * mmap the next file.
222 if (remaining_mmap_bytes(ds_file
) == 0) {
223 /* Are we at the end of the file? */
224 if (ds_file
->mmap_offset_in_file
>= ds_file
->file
->size
) {
225 BT_COMP_LOGD("Reached end of file \"%s\" (%p)",
226 ds_file
->file
->path
->str
, ds_file
->file
->fp
);
227 status
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
231 status
= ds_file_mmap_next(ds_file
);
233 case CTF_MSG_ITER_MEDIUM_STATUS_OK
:
235 case CTF_MSG_ITER_MEDIUM_STATUS_EOF
:
238 BT_COMP_LOGE("Cannot memory-map next region of file \"%s\" (%p)",
239 ds_file
->file
->path
->str
,
245 BT_ASSERT(remaining_mmap_bytes(ds_file
) > 0);
246 *buffer_sz
= MIN(remaining_mmap_bytes(ds_file
), request_sz
);
248 BT_ASSERT(ds_file
->mmap_addr
);
249 *buffer_addr
= ((uint8_t *) ds_file
->mmap_addr
) + ds_file
->request_offset_in_mapping
;
251 ds_file
->request_offset_in_mapping
+= *buffer_sz
;
255 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
262 bt_stream
*medop_borrow_stream(bt_stream_class
*stream_class
, int64_t stream_id
,
265 struct ctf_fs_ds_file
*ds_file
= data
;
266 bt_stream_class
*ds_file_stream_class
;
267 bt_stream
*stream
= NULL
;
269 ds_file_stream_class
= bt_stream_borrow_class(
272 if (stream_class
!= ds_file_stream_class
) {
274 * Not supported: two packets described by two different
275 * stream classes within the same data stream file.
280 stream
= ds_file
->stream
;
287 enum ctf_msg_iter_medium_status
medop_seek(off_t offset
, void *data
)
289 struct ctf_fs_ds_file
*ds_file
= data
;
291 BT_ASSERT(offset
>= 0);
292 BT_ASSERT(offset
< ds_file
->file
->size
);
294 return ds_file_mmap(ds_file
, offset
);
298 struct ctf_msg_iter_medium_ops ctf_fs_ds_file_medops
= {
299 .request_bytes
= medop_request_bytes
,
300 .borrow_stream
= medop_borrow_stream
,
305 struct ctf_fs_ds_index_entry
*ctf_fs_ds_index_entry_create(
306 bt_self_component
*self_comp
, bt_logging_level log_level
)
308 struct ctf_fs_ds_index_entry
*entry
;
310 entry
= g_new0(struct ctf_fs_ds_index_entry
, 1);
312 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to allocate a ctf_fs_ds_index_entry.");
316 entry
->packet_seq_num
= UINT64_MAX
;
323 int convert_cycles_to_ns(struct ctf_clock_class
*clock_class
,
324 uint64_t cycles
, int64_t *ns
)
326 return bt_util_clock_cycles_to_ns_from_origin(cycles
,
327 clock_class
->frequency
, clock_class
->offset_seconds
,
328 clock_class
->offset_cycles
, ns
);
332 struct ctf_fs_ds_index
*build_index_from_idx_file(
333 struct ctf_fs_ds_file
*ds_file
,
334 struct ctf_fs_ds_file_info
*file_info
,
335 struct ctf_msg_iter
*msg_iter
)
338 gchar
*directory
= NULL
;
339 gchar
*basename
= NULL
;
340 GString
*index_basename
= NULL
;
341 gchar
*index_file_path
= NULL
;
342 GMappedFile
*mapped_file
= NULL
;
344 const char *mmap_begin
= NULL
, *file_pos
= NULL
;
345 const struct ctf_packet_index_file_hdr
*header
= NULL
;
346 struct ctf_fs_ds_index
*index
= NULL
;
347 struct ctf_fs_ds_index_entry
*index_entry
= NULL
, *prev_index_entry
= NULL
;
348 uint64_t total_packets_size
= 0;
349 size_t file_index_entry_size
;
350 size_t file_entry_count
;
352 struct ctf_stream_class
*sc
;
353 struct ctf_msg_iter_packet_properties props
;
354 uint32_t version_major
, version_minor
;
355 bt_self_component
*self_comp
= ds_file
->self_comp
;
356 bt_logging_level log_level
= ds_file
->log_level
;
358 BT_COMP_LOGI("Building index from .idx file of stream file %s",
359 ds_file
->file
->path
->str
);
360 ret
= ctf_msg_iter_get_packet_properties(msg_iter
, &props
);
362 BT_COMP_LOGI_STR("Cannot read first packet's header and context fields.");
366 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
,
367 props
.stream_class_id
);
369 if (!sc
->default_clock_class
) {
370 BT_COMP_LOGI_STR("Cannot find stream class's default clock class.");
374 /* Look for index file in relative path index/name.idx. */
375 basename
= g_path_get_basename(ds_file
->file
->path
->str
);
377 BT_COMP_LOGE("Cannot get the basename of datastream file %s",
378 ds_file
->file
->path
->str
);
382 directory
= g_path_get_dirname(ds_file
->file
->path
->str
);
384 BT_COMP_LOGE("Cannot get dirname of datastream file %s",
385 ds_file
->file
->path
->str
);
389 index_basename
= g_string_new(basename
);
390 if (!index_basename
) {
391 BT_COMP_LOGE_STR("Cannot allocate index file basename string");
395 g_string_append(index_basename
, ".idx");
396 index_file_path
= g_build_filename(directory
, "index",
397 index_basename
->str
, NULL
);
398 mapped_file
= g_mapped_file_new(index_file_path
, FALSE
, NULL
);
400 BT_COMP_LOGD("Cannot create new mapped file %s",
406 * The g_mapped_file API limits us to 4GB files on 32-bit.
407 * Traces with such large indexes have never been seen in the wild,
408 * but this would need to be adjusted to support them.
410 filesize
= g_mapped_file_get_length(mapped_file
);
411 if (filesize
< sizeof(*header
)) {
412 BT_COMP_LOGW("Invalid LTTng trace index file: "
413 "file size (%zu bytes) < header size (%zu bytes)",
414 filesize
, sizeof(*header
));
418 mmap_begin
= g_mapped_file_get_contents(mapped_file
);
419 header
= (struct ctf_packet_index_file_hdr
*) mmap_begin
;
421 file_pos
= g_mapped_file_get_contents(mapped_file
) + sizeof(*header
);
422 if (be32toh(header
->magic
) != CTF_INDEX_MAGIC
) {
423 BT_COMP_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
427 version_major
= be32toh(header
->index_major
);
428 version_minor
= be32toh(header
->index_minor
);
429 if (version_major
!= 1) {
431 "Unknown LTTng trace index version: "
432 "major=%" PRIu32
", minor=%" PRIu32
,
433 version_major
, version_minor
);
437 file_index_entry_size
= be32toh(header
->packet_index_len
);
438 file_entry_count
= (filesize
- sizeof(*header
)) / file_index_entry_size
;
439 if ((filesize
- sizeof(*header
)) % file_index_entry_size
) {
440 BT_COMP_LOGW("Invalid LTTng trace index: the index's size after the header "
441 "(%zu bytes) is not a multiple of the index entry size "
442 "(%zu bytes)", (filesize
- sizeof(*header
)),
447 index
= ctf_fs_ds_index_create(ds_file
->log_level
, ds_file
->self_comp
);
452 for (i
= 0; i
< file_entry_count
; i
++) {
453 struct ctf_packet_index
*file_index
=
454 (struct ctf_packet_index
*) file_pos
;
455 uint64_t packet_size
= be64toh(file_index
->packet_size
);
457 if (packet_size
% CHAR_BIT
) {
458 BT_COMP_LOGW("Invalid packet size encountered in LTTng trace index file");
462 index_entry
= ctf_fs_ds_index_entry_create(
463 ds_file
->self_comp
, ds_file
->log_level
);
465 BT_COMP_LOGE_APPEND_CAUSE(ds_file
->self_comp
,
466 "Failed to create a ctf_fs_ds_index_entry.");
470 /* Set path to stream file. */
471 index_entry
->path
= file_info
->path
->str
;
473 /* Convert size in bits to bytes. */
474 packet_size
/= CHAR_BIT
;
475 index_entry
->packet_size
= packet_size
;
477 index_entry
->offset
= be64toh(file_index
->offset
);
478 if (i
!= 0 && index_entry
->offset
< prev_index_entry
->offset
) {
479 BT_COMP_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
480 "previous offset=%" PRIu64
", current offset=%" PRIu64
,
481 prev_index_entry
->offset
, index_entry
->offset
);
485 index_entry
->timestamp_begin
= be64toh(file_index
->timestamp_begin
);
486 index_entry
->timestamp_end
= be64toh(file_index
->timestamp_end
);
487 if (index_entry
->timestamp_end
< index_entry
->timestamp_begin
) {
488 BT_COMP_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
489 "timestamp_begin=%" PRIu64
"timestamp_end=%" PRIu64
,
490 index_entry
->timestamp_begin
,
491 index_entry
->timestamp_end
);
495 /* Convert the packet's bound to nanoseconds since Epoch. */
496 ret
= convert_cycles_to_ns(sc
->default_clock_class
,
497 index_entry
->timestamp_begin
,
498 &index_entry
->timestamp_begin_ns
);
500 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
503 ret
= convert_cycles_to_ns(sc
->default_clock_class
,
504 index_entry
->timestamp_end
,
505 &index_entry
->timestamp_end_ns
);
507 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
511 if (version_minor
>= 1) {
512 index_entry
->packet_seq_num
= be64toh(file_index
->packet_seq_num
);
515 total_packets_size
+= packet_size
;
516 file_pos
+= file_index_entry_size
;
518 prev_index_entry
= index_entry
;
520 /* Give ownership of `index_entry` to `index->entries`. */
521 g_ptr_array_add(index
->entries
, index_entry
);
525 /* Validate that the index addresses the complete stream. */
526 if (ds_file
->file
->size
!= total_packets_size
) {
527 BT_COMP_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
528 "file-size=%" PRIu64
", total-packets-size=%" PRIu64
,
529 ds_file
->file
->size
, total_packets_size
);
535 g_free(index_file_path
);
536 if (index_basename
) {
537 g_string_free(index_basename
, TRUE
);
540 g_mapped_file_unref(mapped_file
);
544 ctf_fs_ds_index_destroy(index
);
551 int init_index_entry(struct ctf_fs_ds_index_entry
*entry
,
552 struct ctf_fs_ds_file
*ds_file
,
553 struct ctf_msg_iter_packet_properties
*props
,
554 off_t packet_size
, off_t packet_offset
)
557 struct ctf_stream_class
*sc
;
558 bt_self_component
*self_comp
= ds_file
->self_comp
;
559 bt_logging_level log_level
= ds_file
->log_level
;
561 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
,
562 props
->stream_class_id
);
564 BT_ASSERT(packet_offset
>= 0);
565 entry
->offset
= packet_offset
;
566 BT_ASSERT(packet_size
>= 0);
567 entry
->packet_size
= packet_size
;
569 if (props
->snapshots
.beginning_clock
!= UINT64_C(-1)) {
570 entry
->timestamp_begin
= props
->snapshots
.beginning_clock
;
572 /* Convert the packet's bound to nanoseconds since Epoch. */
573 ret
= convert_cycles_to_ns(sc
->default_clock_class
,
574 props
->snapshots
.beginning_clock
,
575 &entry
->timestamp_begin_ns
);
577 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
581 entry
->timestamp_begin
= UINT64_C(-1);
582 entry
->timestamp_begin_ns
= UINT64_C(-1);
585 if (props
->snapshots
.end_clock
!= UINT64_C(-1)) {
586 entry
->timestamp_end
= props
->snapshots
.end_clock
;
588 /* Convert the packet's bound to nanoseconds since Epoch. */
589 ret
= convert_cycles_to_ns(sc
->default_clock_class
,
590 props
->snapshots
.end_clock
,
591 &entry
->timestamp_end_ns
);
593 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
597 entry
->timestamp_end
= UINT64_C(-1);
598 entry
->timestamp_end_ns
= UINT64_C(-1);
606 struct ctf_fs_ds_index
*build_index_from_stream_file(
607 struct ctf_fs_ds_file
*ds_file
,
608 struct ctf_fs_ds_file_info
*file_info
,
609 struct ctf_msg_iter
*msg_iter
)
612 struct ctf_fs_ds_index
*index
= NULL
;
613 enum ctf_msg_iter_status iter_status
= CTF_MSG_ITER_STATUS_OK
;
614 off_t current_packet_offset_bytes
= 0;
615 bt_self_component
*self_comp
= ds_file
->self_comp
;
616 bt_logging_level log_level
= ds_file
->log_level
;
618 BT_COMP_LOGI("Indexing stream file %s", ds_file
->file
->path
->str
);
620 index
= ctf_fs_ds_index_create(ds_file
->log_level
, ds_file
->self_comp
);
626 off_t current_packet_size_bytes
;
627 struct ctf_fs_ds_index_entry
*index_entry
;
628 struct ctf_msg_iter_packet_properties props
;
630 if (current_packet_offset_bytes
< 0) {
631 BT_COMP_LOGE_STR("Cannot get the current packet's offset.");
633 } else if (current_packet_offset_bytes
> ds_file
->file
->size
) {
634 BT_COMP_LOGE_STR("Unexpected current packet's offset (larger than file).");
636 } else if (current_packet_offset_bytes
== ds_file
->file
->size
) {
641 iter_status
= ctf_msg_iter_seek(msg_iter
,
642 current_packet_offset_bytes
);
643 if (iter_status
!= CTF_MSG_ITER_STATUS_OK
) {
647 iter_status
= ctf_msg_iter_get_packet_properties(
649 if (iter_status
!= CTF_MSG_ITER_STATUS_OK
) {
653 if (props
.exp_packet_total_size
>= 0) {
654 current_packet_size_bytes
=
655 (uint64_t) props
.exp_packet_total_size
/ 8;
657 current_packet_size_bytes
= ds_file
->file
->size
;
660 if (current_packet_offset_bytes
+ current_packet_size_bytes
>
661 ds_file
->file
->size
) {
662 BT_COMP_LOGW("Invalid packet size reported in file: stream=\"%s\", "
663 "packet-offset=%jd, packet-size-bytes=%jd, "
665 ds_file
->file
->path
->str
,
666 (intmax_t) current_packet_offset_bytes
,
667 (intmax_t) current_packet_size_bytes
,
668 (intmax_t) ds_file
->file
->size
);
672 index_entry
= ctf_fs_ds_index_entry_create(
673 ds_file
->self_comp
, ds_file
->log_level
);
675 BT_COMP_LOGE_APPEND_CAUSE(ds_file
->self_comp
,
676 "Failed to create a ctf_fs_ds_index_entry.");
680 /* Set path to stream file. */
681 index_entry
->path
= file_info
->path
->str
;
683 ret
= init_index_entry(index_entry
, ds_file
, &props
,
684 current_packet_size_bytes
, current_packet_offset_bytes
);
690 g_ptr_array_add(index
->entries
, index_entry
);
692 current_packet_offset_bytes
+= current_packet_size_bytes
;
693 BT_COMP_LOGD("Seeking to next packet: current-packet-offset=%jd, "
694 "next-packet-offset=%jd",
695 (intmax_t) (current_packet_offset_bytes
- current_packet_size_bytes
),
696 (intmax_t) current_packet_offset_bytes
);
703 ctf_fs_ds_index_destroy(index
);
709 struct ctf_fs_ds_file
*ctf_fs_ds_file_create(
710 struct ctf_fs_trace
*ctf_fs_trace
,
711 bt_self_message_iterator
*self_msg_iter
,
712 bt_stream
*stream
, const char *path
,
713 bt_logging_level log_level
)
716 const size_t offset_align
= bt_mmap_get_offset_align_size(log_level
);
717 struct ctf_fs_ds_file
*ds_file
= g_new0(struct ctf_fs_ds_file
, 1);
723 ds_file
->log_level
= log_level
;
724 ds_file
->self_comp
= ctf_fs_trace
->self_comp
;
725 ds_file
->self_msg_iter
= self_msg_iter
;
726 ds_file
->file
= ctf_fs_file_create(log_level
, ds_file
->self_comp
);
727 if (!ds_file
->file
) {
731 ds_file
->stream
= stream
;
732 bt_stream_get_ref(ds_file
->stream
);
733 ds_file
->metadata
= ctf_fs_trace
->metadata
;
734 g_string_assign(ds_file
->file
->path
, path
);
735 ret
= ctf_fs_file_open(ds_file
->file
, "rb");
740 ds_file
->mmap_max_len
= offset_align
* 2048;
745 /* Do not touch "borrowed" file. */
746 ctf_fs_ds_file_destroy(ds_file
);
754 struct ctf_fs_ds_index
*ctf_fs_ds_file_build_index(
755 struct ctf_fs_ds_file
*ds_file
,
756 struct ctf_fs_ds_file_info
*file_info
,
757 struct ctf_msg_iter
*msg_iter
)
759 struct ctf_fs_ds_index
*index
;
760 bt_self_component
*self_comp
= ds_file
->self_comp
;
761 bt_logging_level log_level
= ds_file
->log_level
;
763 index
= build_index_from_idx_file(ds_file
, file_info
, msg_iter
);
768 BT_COMP_LOGI("Failed to build index from .index file; "
769 "falling back to stream indexing.");
770 index
= build_index_from_stream_file(ds_file
, file_info
, msg_iter
);
776 struct ctf_fs_ds_index
*ctf_fs_ds_index_create(bt_logging_level log_level
,
777 bt_self_component
*self_comp
)
779 struct ctf_fs_ds_index
*index
= g_new0(struct ctf_fs_ds_index
, 1);
782 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
783 "Failed to allocate index");
787 index
->entries
= g_ptr_array_new_with_free_func((GDestroyNotify
) g_free
);
788 if (!index
->entries
) {
789 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
790 "Failed to allocate index entries.");
797 ctf_fs_ds_index_destroy(index
);
804 void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file
*ds_file
)
810 bt_stream_put_ref(ds_file
->stream
);
811 (void) ds_file_munmap(ds_file
);
814 ctf_fs_file_destroy(ds_file
->file
);
821 void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index
*index
)
827 if (index
->entries
) {
828 g_ptr_array_free(index
->entries
, TRUE
);