2 * SPDX-License-Identifier: MIT
4 * Copyright 2016-2017 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
13 #include "compat/endian.h" /* IWYU pragma: keep */
14 #include "compat/mman.h" /* IWYU: pragma keep */
15 #include "cpp-common/bt2c/glib-up.hpp"
16 #include "cpp-common/bt2s/make-unique.hpp"
17 #include "cpp-common/vendor/fmt/format.h"
19 #include "../common/src/msg-iter/msg-iter.hpp"
20 #include "data-stream-file.hpp"
23 #include "lttng-index.hpp"
25 static inline size_t remaining_mmap_bytes(struct ctf_fs_ds_file
*ds_file
)
27 BT_ASSERT_DBG(ds_file
->mmap_len
>= ds_file
->request_offset_in_mapping
);
28 return ds_file
->mmap_len
- ds_file
->request_offset_in_mapping
;
32 * Return true if `offset_in_file` is in the current mapping.
35 static bool offset_ist_mapped(struct ctf_fs_ds_file
*ds_file
, off_t offset_in_file
)
37 return offset_in_file
>= ds_file
->mmap_offset_in_file
&&
38 offset_in_file
< (ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
);
41 static enum ctf_msg_iter_medium_status
ds_file_munmap(struct ctf_fs_ds_file
*ds_file
)
45 if (!ds_file
->mmap_addr
) {
46 return CTF_MSG_ITER_MEDIUM_STATUS_OK
;
49 if (bt_munmap(ds_file
->mmap_addr
, ds_file
->mmap_len
)) {
50 BT_CPPLOGE_ERRNO_SPEC(ds_file
->logger
, "Cannot memory-unmap file",
51 ": address={}, size={}, file_path=\"{}\", file={}",
52 fmt::ptr(ds_file
->mmap_addr
), ds_file
->mmap_len
,
53 ds_file
->file
? ds_file
->file
->path
: "NULL",
54 ds_file
->file
? fmt::ptr(ds_file
->file
->fp
) : NULL
);
55 return CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
58 ds_file
->mmap_addr
= NULL
;
60 return CTF_MSG_ITER_MEDIUM_STATUS_OK
;
64 * mmap a region of `ds_file` such that `requested_offset_in_file` is in the
65 * mapping. If the currently mmap-ed region already contains
66 * `requested_offset_in_file`, the mapping is kept.
68 * Set `ds_file->requested_offset_in_mapping` based on `request_offset_in_file`,
69 * such that the next call to `request_bytes` will return bytes starting at that
72 * `requested_offset_in_file` must be a valid offset in the file.
74 static enum ctf_msg_iter_medium_status
ds_file_mmap(struct ctf_fs_ds_file
*ds_file
,
75 off_t requested_offset_in_file
)
77 /* Ensure the requested offset is in the file range. */
78 BT_ASSERT(requested_offset_in_file
>= 0);
79 BT_ASSERT(requested_offset_in_file
< ds_file
->file
->size
);
82 * If the mapping already contains the requested offset, just adjust
83 * requested_offset_in_mapping.
85 if (offset_ist_mapped(ds_file
, requested_offset_in_file
)) {
86 ds_file
->request_offset_in_mapping
=
87 requested_offset_in_file
- ds_file
->mmap_offset_in_file
;
88 return CTF_MSG_ITER_MEDIUM_STATUS_OK
;
91 /* Unmap old region */
92 ctf_msg_iter_medium_status status
= ds_file_munmap(ds_file
);
93 if (status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
98 * Compute a mapping that has the required alignment properties and
99 * contains `requested_offset_in_file`.
101 ds_file
->request_offset_in_mapping
=
102 requested_offset_in_file
%
103 bt_mmap_get_offset_align_size(static_cast<int>(ds_file
->logger
.level()));
104 ds_file
->mmap_offset_in_file
= requested_offset_in_file
- ds_file
->request_offset_in_mapping
;
106 MIN(ds_file
->file
->size
- ds_file
->mmap_offset_in_file
, ds_file
->mmap_max_len
);
108 BT_ASSERT(ds_file
->mmap_len
> 0);
111 bt_mmap(ds_file
->mmap_len
, PROT_READ
, MAP_PRIVATE
, fileno(ds_file
->file
->fp
.get()),
112 ds_file
->mmap_offset_in_file
, static_cast<int>(ds_file
->logger
.level()));
113 if (ds_file
->mmap_addr
== MAP_FAILED
) {
114 BT_CPPLOGE_SPEC(ds_file
->logger
,
115 "Cannot memory-map address (size {}) of file \"{}\" ({}) at offset {}: {}",
116 ds_file
->mmap_len
, ds_file
->file
->path
, fmt::ptr(ds_file
->file
->fp
),
117 (intmax_t) ds_file
->mmap_offset_in_file
, strerror(errno
));
118 return CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
121 return CTF_MSG_ITER_MEDIUM_STATUS_OK
;
125 * Change the mapping of the file to read the region that follows the current
128 * If the file hasn't been mapped yet, then everything (mmap_offset_in_file,
129 * mmap_len, request_offset_in_mapping) should have the value 0, which will
130 * result in the beginning of the file getting mapped.
132 * return _EOF if the current mapping is the end of the file.
135 static enum ctf_msg_iter_medium_status
ds_file_mmap_next(struct ctf_fs_ds_file
*ds_file
)
138 * If we're called, it's because more bytes are requested but we have
139 * given all the bytes of the current mapping.
141 BT_ASSERT(ds_file
->request_offset_in_mapping
== ds_file
->mmap_len
);
144 * If the current mapping coincides with the end of the file, there is
147 if (ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
== ds_file
->file
->size
) {
148 return CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
151 return ds_file_mmap(ds_file
, ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
);
154 static enum ctf_msg_iter_medium_status
medop_request_bytes(size_t request_sz
, uint8_t **buffer_addr
,
155 size_t *buffer_sz
, void *data
)
157 struct ctf_fs_ds_file
*ds_file
= (struct ctf_fs_ds_file
*) data
;
159 BT_ASSERT(request_sz
> 0);
162 * Check if we have at least one memory-mapped byte left. If we don't,
163 * mmap the next file.
165 if (remaining_mmap_bytes(ds_file
) == 0) {
166 /* Are we at the end of the file? */
167 if (ds_file
->mmap_offset_in_file
>= ds_file
->file
->size
) {
168 BT_CPPLOGD_SPEC(ds_file
->logger
, "Reached end of file \"{}\" ({})", ds_file
->file
->path
,
169 fmt::ptr(ds_file
->file
->fp
));
170 return CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
173 ctf_msg_iter_medium_status status
= ds_file_mmap_next(ds_file
);
175 case CTF_MSG_ITER_MEDIUM_STATUS_OK
:
177 case CTF_MSG_ITER_MEDIUM_STATUS_EOF
:
178 return CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
180 BT_CPPLOGE_SPEC(ds_file
->logger
, "Cannot memory-map next region of file \"{}\" ({})",
181 ds_file
->file
->path
, fmt::ptr(ds_file
->file
->fp
));
186 BT_ASSERT(remaining_mmap_bytes(ds_file
) > 0);
187 *buffer_sz
= MIN(remaining_mmap_bytes(ds_file
), request_sz
);
189 BT_ASSERT(ds_file
->mmap_addr
);
190 *buffer_addr
= ((uint8_t *) ds_file
->mmap_addr
) + ds_file
->request_offset_in_mapping
;
192 ds_file
->request_offset_in_mapping
+= *buffer_sz
;
194 return CTF_MSG_ITER_MEDIUM_STATUS_OK
;
197 static bt_stream
*medop_borrow_stream(bt_stream_class
*stream_class
, int64_t, void *data
)
199 struct ctf_fs_ds_file
*ds_file
= (struct ctf_fs_ds_file
*) data
;
200 bt_stream_class
*ds_file_stream_class
;
202 ds_file_stream_class
= ds_file
->stream
->cls().libObjPtr();
204 if (stream_class
!= ds_file_stream_class
) {
206 * Not supported: two packets described by two different
207 * stream classes within the same data stream file.
212 return ds_file
->stream
->libObjPtr();
215 static enum ctf_msg_iter_medium_status
medop_seek(off_t offset
, void *data
)
217 struct ctf_fs_ds_file
*ds_file
= (struct ctf_fs_ds_file
*) data
;
219 BT_ASSERT(offset
>= 0);
220 BT_ASSERT(offset
< ds_file
->file
->size
);
222 return ds_file_mmap(ds_file
, offset
);
225 struct ctf_msg_iter_medium_ops ctf_fs_ds_file_medops
= {
232 struct ctf_fs_ds_group_medops_data
234 explicit ctf_fs_ds_group_medops_data(const bt2c::Logger
& parentLogger
) :
235 logger
{parentLogger
, "PLUGIN/SRC.CTF.FS/DS-GROUP-MEDOPS"}
241 /* Weak, set once at creation time. */
242 struct ctf_fs_ds_file_group
*ds_file_group
= nullptr;
245 * Index (as in element rank) of the index entry of ds_file_groups'
246 * index we will read next (so, the one after the one we are reading
249 guint next_index_entry_index
= 0;
252 * File we are currently reading. Changes whenever we switch to
253 * reading another data file.
255 ctf_fs_ds_file::UP file
;
257 /* Weak, for context / logging / appending causes. */
258 bt_self_message_iterator
*self_msg_iter
= nullptr;
261 static enum ctf_msg_iter_medium_status
medop_group_request_bytes(size_t request_sz
,
262 uint8_t **buffer_addr
,
263 size_t *buffer_sz
, void *void_data
)
265 struct ctf_fs_ds_group_medops_data
*data
= (struct ctf_fs_ds_group_medops_data
*) void_data
;
267 /* Return bytes from the current file. */
268 return medop_request_bytes(request_sz
, buffer_addr
, buffer_sz
, data
->file
.get());
271 static bt_stream
*medop_group_borrow_stream(bt_stream_class
*stream_class
, int64_t stream_id
,
274 struct ctf_fs_ds_group_medops_data
*data
= (struct ctf_fs_ds_group_medops_data
*) void_data
;
276 return medop_borrow_stream(stream_class
, stream_id
, data
->file
.get());
280 * Set `data->file` to prepare it to read the packet described
284 static enum ctf_msg_iter_medium_status
285 ctf_fs_ds_group_medops_set_file(struct ctf_fs_ds_group_medops_data
*data
,
286 struct ctf_fs_ds_index_entry
*index_entry
)
289 BT_ASSERT(index_entry
);
291 /* Check if that file is already the one mapped. */
292 if (!data
->file
|| data
->file
->file
->path
!= index_entry
->path
) {
293 /* Create the new file. */
295 ctf_fs_ds_file_create(data
->ds_file_group
->ctf_fs_trace
, data
->ds_file_group
->stream
,
296 index_entry
->path
, data
->logger
);
298 BT_CPPLOGE_APPEND_CAUSE_SPEC(data
->logger
, "failed to create ctf_fs_ds_file.");
299 return CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
304 * Ensure the right portion of the file will be returned on the next
305 * request_bytes call.
307 return ds_file_mmap(data
->file
.get(), index_entry
->offset
.bytes());
310 static enum ctf_msg_iter_medium_status
medop_group_switch_packet(void *void_data
)
312 struct ctf_fs_ds_group_medops_data
*data
= (struct ctf_fs_ds_group_medops_data
*) void_data
;
313 struct ctf_fs_ds_index_entry
*index_entry
;
315 /* If we have gone through all index entries, we are done. */
316 if (data
->next_index_entry_index
>= data
->ds_file_group
->index
->entries
.size()) {
317 return CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
321 * Otherwise, look up the next index entry / packet and prepare it
324 index_entry
= data
->ds_file_group
->index
->entries
[data
->next_index_entry_index
].get();
326 ctf_msg_iter_medium_status status
= ctf_fs_ds_group_medops_set_file(data
, index_entry
);
327 if (status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
331 data
->next_index_entry_index
++;
333 return CTF_MSG_ITER_MEDIUM_STATUS_OK
;
336 void ctf_fs_ds_group_medops_data_deleter::operator()(ctf_fs_ds_group_medops_data
*data
) noexcept
341 enum ctf_msg_iter_medium_status
ctf_fs_ds_group_medops_data_create(
342 struct ctf_fs_ds_file_group
*ds_file_group
, bt_self_message_iterator
*self_msg_iter
,
343 const bt2c::Logger
& parentLogger
, ctf_fs_ds_group_medops_data_up
& out
)
345 BT_ASSERT(self_msg_iter
);
346 BT_ASSERT(ds_file_group
);
347 BT_ASSERT(ds_file_group
->index
);
348 BT_ASSERT(!ds_file_group
->index
->entries
.empty());
350 out
.reset(new ctf_fs_ds_group_medops_data
{parentLogger
});
352 out
->ds_file_group
= ds_file_group
;
353 out
->self_msg_iter
= self_msg_iter
;
356 * No need to prepare the first file. ctf_msg_iter will call
357 * switch_packet before reading the first packet, it will be
361 return CTF_MSG_ITER_MEDIUM_STATUS_OK
;
364 void ctf_fs_ds_group_medops_data_reset(struct ctf_fs_ds_group_medops_data
*data
)
366 data
->next_index_entry_index
= 0;
369 struct ctf_msg_iter_medium_ops ctf_fs_ds_group_medops
= {
370 .request_bytes
= medop_group_request_bytes
,
373 * We don't support seeking using this medops. It would probably be
374 * possible, but it's not needed at the moment.
378 .switch_packet
= medop_group_switch_packet
,
379 .borrow_stream
= medop_group_borrow_stream
,
382 static ctf_fs_ds_index_entry::UP
ctf_fs_ds_index_entry_create(const bt2c::DataLen offset
,
383 const bt2c::DataLen packetSize
)
385 ctf_fs_ds_index_entry::UP entry
= bt2s::make_unique
<ctf_fs_ds_index_entry
>(offset
, packetSize
);
387 entry
->packet_seq_num
= UINT64_MAX
;
392 static int convert_cycles_to_ns(struct ctf_clock_class
*clock_class
, uint64_t cycles
, int64_t *ns
)
394 return bt_util_clock_cycles_to_ns_from_origin(cycles
, clock_class
->frequency
,
395 clock_class
->offset_seconds
,
396 clock_class
->offset_cycles
, ns
);
399 static ctf_fs_ds_index::UP
build_index_from_idx_file(struct ctf_fs_ds_file
*ds_file
,
400 struct ctf_fs_ds_file_info
*file_info
,
401 struct ctf_msg_iter
*msg_iter
)
403 bt2c::GCharUP directory
;
404 bt2c::GCharUP basename
;
405 std::string index_basename
;
406 bt2c::GCharUP index_file_path
;
407 bt2c::GMappedFileUP mapped_file
;
409 const char *mmap_begin
= NULL
, *file_pos
= NULL
;
410 const struct ctf_packet_index_file_hdr
*header
= NULL
;
411 ctf_fs_ds_index::UP index
;
412 ctf_fs_ds_index_entry::UP index_entry
;
413 ctf_fs_ds_index_entry
*prev_index_entry
= NULL
;
414 auto totalPacketsSize
= bt2c::DataLen::fromBytes(0);
415 size_t file_index_entry_size
;
416 size_t file_entry_count
;
418 struct ctf_stream_class
*sc
;
419 struct ctf_msg_iter_packet_properties props
;
420 uint32_t version_major
, version_minor
;
422 BT_CPPLOGI_SPEC(ds_file
->logger
, "Building index from .idx file of stream file {}",
423 ds_file
->file
->path
);
424 int ret
= ctf_msg_iter_get_packet_properties(msg_iter
, &props
);
426 BT_CPPLOGI_STR_SPEC(ds_file
->logger
,
427 "Cannot read first packet's header and context fields.");
431 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
, props
.stream_class_id
);
433 if (!sc
->default_clock_class
) {
434 BT_CPPLOGI_STR_SPEC(ds_file
->logger
, "Cannot find stream class's default clock class.");
438 /* Look for index file in relative path index/name.idx. */
439 basename
.reset(g_path_get_basename(ds_file
->file
->path
.c_str()));
441 BT_CPPLOGE_SPEC(ds_file
->logger
, "Cannot get the basename of datastream file {}",
442 ds_file
->file
->path
);
446 directory
.reset(g_path_get_dirname(ds_file
->file
->path
.c_str()));
448 BT_CPPLOGE_SPEC(ds_file
->logger
, "Cannot get dirname of datastream file {}",
449 ds_file
->file
->path
);
453 index_basename
= fmt::format("{}.idx", basename
.get());
454 index_file_path
.reset(g_build_filename(directory
.get(), "index", index_basename
.c_str(), NULL
));
455 mapped_file
.reset(g_mapped_file_new(index_file_path
.get(), FALSE
, NULL
));
457 BT_CPPLOGD_SPEC(ds_file
->logger
, "Cannot create new mapped file {}", index_file_path
.get());
462 * The g_mapped_file API limits us to 4GB files on 32-bit.
463 * Traces with such large indexes have never been seen in the wild,
464 * but this would need to be adjusted to support them.
466 filesize
= g_mapped_file_get_length(mapped_file
.get());
467 if (filesize
< sizeof(*header
)) {
468 BT_CPPLOGW_SPEC(ds_file
->logger
,
469 "Invalid LTTng trace index file: "
470 "file size ({} bytes) < header size ({} bytes)",
471 filesize
, sizeof(*header
));
475 mmap_begin
= g_mapped_file_get_contents(mapped_file
.get());
476 header
= (struct ctf_packet_index_file_hdr
*) mmap_begin
;
478 file_pos
= g_mapped_file_get_contents(mapped_file
.get()) + sizeof(*header
);
479 if (be32toh(header
->magic
) != CTF_INDEX_MAGIC
) {
480 BT_CPPLOGW_STR_SPEC(ds_file
->logger
,
481 "Invalid LTTng trace index: \"magic\" field validation failed");
485 version_major
= be32toh(header
->index_major
);
486 version_minor
= be32toh(header
->index_minor
);
487 if (version_major
!= 1) {
488 BT_CPPLOGW_SPEC(ds_file
->logger
, "Unknown LTTng trace index version: major={}, minor={}",
489 version_major
, version_minor
);
493 file_index_entry_size
= be32toh(header
->packet_index_len
);
494 if (file_index_entry_size
< CTF_INDEX_1_0_SIZE
) {
497 "Invalid `packet_index_len` in LTTng trace index file (`packet_index_len` < CTF index 1.0 index entry size): "
498 "packet_index_len={}, CTF_INDEX_1_0_SIZE={}",
499 file_index_entry_size
, CTF_INDEX_1_0_SIZE
);
503 file_entry_count
= (filesize
- sizeof(*header
)) / file_index_entry_size
;
504 if ((filesize
- sizeof(*header
)) % file_index_entry_size
) {
505 BT_CPPLOGW_SPEC(ds_file
->logger
,
506 "Invalid LTTng trace index: the index's size after the header "
507 "({} bytes) is not a multiple of the index entry size "
509 (filesize
- sizeof(*header
)), sizeof(*header
));
513 index
= bt2s::make_unique
<ctf_fs_ds_index
>();
515 for (i
= 0; i
< file_entry_count
; i
++) {
516 struct ctf_packet_index
*file_index
= (struct ctf_packet_index
*) file_pos
;
517 const auto packetSize
= bt2c::DataLen::fromBits(be64toh(file_index
->packet_size
));
519 if (packetSize
.hasExtraBits()) {
520 BT_CPPLOGW_SPEC(ds_file
->logger
,
521 "Invalid packet size encountered in LTTng trace index file");
525 const auto offset
= bt2c::DataLen::fromBytes(be64toh(file_index
->offset
));
527 if (i
!= 0 && offset
< prev_index_entry
->offset
) {
530 "Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
531 "previous offset={} bytes, current offset={} bytes",
532 prev_index_entry
->offset
.bytes(), offset
.bytes());
536 index_entry
= ctf_fs_ds_index_entry_create(offset
, packetSize
);
538 BT_CPPLOGE_APPEND_CAUSE_SPEC(ds_file
->logger
,
539 "Failed to create a ctf_fs_ds_index_entry.");
543 /* Set path to stream file. */
544 index_entry
->path
= file_info
->path
.c_str();
546 index_entry
->timestamp_begin
= be64toh(file_index
->timestamp_begin
);
547 index_entry
->timestamp_end
= be64toh(file_index
->timestamp_end
);
548 if (index_entry
->timestamp_end
< index_entry
->timestamp_begin
) {
551 "Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
552 "timestamp_begin={}, timestamp_end={}",
553 index_entry
->timestamp_begin
, index_entry
->timestamp_end
);
557 /* Convert the packet's bound to nanoseconds since Epoch. */
558 ret
= convert_cycles_to_ns(sc
->default_clock_class
, index_entry
->timestamp_begin
,
559 &index_entry
->timestamp_begin_ns
);
563 "Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
566 ret
= convert_cycles_to_ns(sc
->default_clock_class
, index_entry
->timestamp_end
,
567 &index_entry
->timestamp_end_ns
);
571 "Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
575 if (version_minor
>= 1) {
576 index_entry
->packet_seq_num
= be64toh(file_index
->packet_seq_num
);
579 totalPacketsSize
+= packetSize
;
580 file_pos
+= file_index_entry_size
;
582 prev_index_entry
= index_entry
.get();
584 index
->entries
.emplace_back(std::move(index_entry
));
587 /* Validate that the index addresses the complete stream. */
588 if (ds_file
->file
->size
!= totalPacketsSize
.bytes()) {
589 BT_CPPLOGW_SPEC(ds_file
->logger
,
590 "Invalid LTTng trace index file; indexed size != stream file size: "
591 "file-size={} bytes, total-packets-size={} bytes",
592 ds_file
->file
->size
, totalPacketsSize
.bytes());
599 static int init_index_entry(struct ctf_fs_ds_index_entry
*entry
, struct ctf_fs_ds_file
*ds_file
,
600 struct ctf_msg_iter_packet_properties
*props
)
602 struct ctf_stream_class
*sc
;
604 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
, props
->stream_class_id
);
607 if (props
->snapshots
.beginning_clock
!= UINT64_C(-1)) {
608 entry
->timestamp_begin
= props
->snapshots
.beginning_clock
;
610 /* Convert the packet's bound to nanoseconds since Epoch. */
611 int ret
= convert_cycles_to_ns(sc
->default_clock_class
, props
->snapshots
.beginning_clock
,
612 &entry
->timestamp_begin_ns
);
614 BT_CPPLOGI_STR_SPEC(ds_file
->logger
,
615 "Failed to convert raw timestamp to nanoseconds since Epoch.");
619 entry
->timestamp_begin
= UINT64_C(-1);
620 entry
->timestamp_begin_ns
= UINT64_C(-1);
623 if (props
->snapshots
.end_clock
!= UINT64_C(-1)) {
624 entry
->timestamp_end
= props
->snapshots
.end_clock
;
626 /* Convert the packet's bound to nanoseconds since Epoch. */
627 int ret
= convert_cycles_to_ns(sc
->default_clock_class
, props
->snapshots
.end_clock
,
628 &entry
->timestamp_end_ns
);
630 BT_CPPLOGI_STR_SPEC(ds_file
->logger
,
631 "Failed to convert raw timestamp to nanoseconds since Epoch.");
635 entry
->timestamp_end
= UINT64_C(-1);
636 entry
->timestamp_end_ns
= UINT64_C(-1);
642 static ctf_fs_ds_index::UP
build_index_from_stream_file(struct ctf_fs_ds_file
*ds_file
,
643 struct ctf_fs_ds_file_info
*file_info
,
644 struct ctf_msg_iter
*msg_iter
)
647 enum ctf_msg_iter_status iter_status
= CTF_MSG_ITER_STATUS_OK
;
648 auto currentPacketOffset
= bt2c::DataLen::fromBytes(0);
650 BT_CPPLOGI_SPEC(ds_file
->logger
, "Indexing stream file {}", ds_file
->file
->path
);
652 ctf_fs_ds_index::UP index
= bt2s::make_unique
<ctf_fs_ds_index
>();
655 struct ctf_msg_iter_packet_properties props
;
657 if (currentPacketOffset
.bytes() > ds_file
->file
->size
) {
658 BT_CPPLOGE_STR_SPEC(ds_file
->logger
,
659 "Unexpected current packet's offset (larger than file).");
661 } else if (currentPacketOffset
.bytes() == ds_file
->file
->size
) {
666 iter_status
= ctf_msg_iter_seek(msg_iter
, currentPacketOffset
.bytes());
667 if (iter_status
!= CTF_MSG_ITER_STATUS_OK
) {
671 iter_status
= ctf_msg_iter_get_packet_properties(msg_iter
, &props
);
672 if (iter_status
!= CTF_MSG_ITER_STATUS_OK
) {
677 * Get the current packet size from the packet header, if set. Else,
678 * assume there is a single packet in the file, so take the file size
679 * as the packet size.
681 const auto currentPacketSize
= props
.exp_packet_total_size
>= 0 ?
682 bt2c::DataLen::fromBits(props
.exp_packet_total_size
) :
683 bt2c::DataLen::fromBytes(ds_file
->file
->size
);
685 if ((currentPacketOffset
+ currentPacketSize
).bytes() > ds_file
->file
->size
) {
686 BT_CPPLOGW_SPEC(ds_file
->logger
,
687 "Invalid packet size reported in file: stream=\"{}\", "
688 "packet-offset-bytes={}, packet-size-bytes={}, "
689 "file-size-bytes={}",
690 ds_file
->file
->path
, currentPacketOffset
.bytes(),
691 currentPacketSize
.bytes(), ds_file
->file
->size
);
695 auto index_entry
= ctf_fs_ds_index_entry_create(currentPacketOffset
, currentPacketSize
);
697 BT_CPPLOGE_APPEND_CAUSE_SPEC(ds_file
->logger
,
698 "Failed to create a ctf_fs_ds_index_entry.");
702 /* Set path to stream file. */
703 index_entry
->path
= file_info
->path
.c_str();
705 ret
= init_index_entry(index_entry
.get(), ds_file
, &props
);
710 index
->entries
.emplace_back(std::move(index_entry
));
712 currentPacketOffset
+= currentPacketSize
;
713 BT_CPPLOGD_SPEC(ds_file
->logger
,
714 "Seeking to next packet: current-packet-offset-bytes={}, "
715 "next-packet-offset-bytes={}",
716 (currentPacketOffset
- currentPacketSize
).bytes(),
717 currentPacketOffset
.bytes());
723 ctf_fs_ds_file::UP
ctf_fs_ds_file_create(struct ctf_fs_trace
*ctf_fs_trace
,
724 bt2::Stream::Shared stream
, const char *path
,
725 const bt2c::Logger
& parentLogger
)
728 auto ds_file
= bt2s::make_unique
<ctf_fs_ds_file
>(parentLogger
);
731 ds_file
->file
= bt2s::make_unique
<ctf_fs_file
>(parentLogger
);
732 ds_file
->stream
= std::move(stream
);
733 ds_file
->metadata
= ctf_fs_trace
->metadata
.get();
734 ds_file
->file
->path
= path
;
735 ret
= ctf_fs_file_open(ds_file
->file
.get(), "rb");
740 offset_align
= bt_mmap_get_offset_align_size(static_cast<int>(ds_file
->logger
.level()));
741 ds_file
->mmap_max_len
= offset_align
* 2048;
746 ctf_fs_ds_index::UP
ctf_fs_ds_file_build_index(struct ctf_fs_ds_file
*ds_file
,
747 struct ctf_fs_ds_file_info
*file_info
,
748 struct ctf_msg_iter
*msg_iter
)
750 auto index
= build_index_from_idx_file(ds_file
, file_info
, msg_iter
);
755 BT_CPPLOGI_SPEC(ds_file
->logger
, "Failed to build index from .index file; "
756 "falling back to stream indexing.");
757 return build_index_from_stream_file(ds_file
, file_info
, msg_iter
);
760 ctf_fs_ds_file::~ctf_fs_ds_file()
762 (void) ds_file_munmap(this);
765 ctf_fs_ds_file_info::UP
ctf_fs_ds_file_info_create(const char *path
, int64_t begin_ns
)
767 ctf_fs_ds_file_info::UP ds_file_info
= bt2s::make_unique
<ctf_fs_ds_file_info
>();
769 ds_file_info
->path
= path
;
770 ds_file_info
->begin_ns
= begin_ns
;
774 ctf_fs_ds_file_group::UP
ctf_fs_ds_file_group_create(struct ctf_fs_trace
*ctf_fs_trace
,
775 struct ctf_stream_class
*sc
,
776 uint64_t stream_instance_id
,
777 ctf_fs_ds_index::UP index
)
779 ctf_fs_ds_file_group::UP ds_file_group
{new ctf_fs_ds_file_group
};
781 ds_file_group
->index
= std::move(index
);
783 ds_file_group
->stream_id
= stream_instance_id
;
785 ds_file_group
->sc
= sc
;
786 ds_file_group
->ctf_fs_trace
= ctf_fs_trace
;
788 return ds_file_group
;