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/bt2s/make-unique.hpp"
16 #include "cpp-common/vendor/fmt/format.h"
18 #include "../common/src/msg-iter/msg-iter.hpp"
19 #include "data-stream-file.hpp"
22 #include "lttng-index.hpp"
24 static inline size_t remaining_mmap_bytes(struct ctf_fs_ds_file
*ds_file
)
26 BT_ASSERT_DBG(ds_file
->mmap_len
>= ds_file
->request_offset_in_mapping
);
27 return ds_file
->mmap_len
- ds_file
->request_offset_in_mapping
;
31 * Return true if `offset_in_file` is in the current mapping.
34 static bool offset_ist_mapped(struct ctf_fs_ds_file
*ds_file
, off_t offset_in_file
)
36 return offset_in_file
>= ds_file
->mmap_offset_in_file
&&
37 offset_in_file
< (ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
);
40 static enum ctf_msg_iter_medium_status
ds_file_munmap(struct ctf_fs_ds_file
*ds_file
)
42 enum ctf_msg_iter_medium_status status
;
46 if (!ds_file
->mmap_addr
) {
47 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
51 if (bt_munmap(ds_file
->mmap_addr
, ds_file
->mmap_len
)) {
52 BT_CPPLOGE_ERRNO_SPEC(ds_file
->logger
, "Cannot memory-unmap file",
53 ": address={}, size={}, file_path=\"{}\", file={}",
54 fmt::ptr(ds_file
->mmap_addr
), ds_file
->mmap_len
,
55 ds_file
->file
? ds_file
->file
->path
->str
: "NULL",
56 ds_file
->file
? fmt::ptr(ds_file
->file
->fp
) : NULL
);
57 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
61 ds_file
->mmap_addr
= NULL
;
63 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
69 * mmap a region of `ds_file` such that `requested_offset_in_file` is in the
70 * mapping. If the currently mmap-ed region already contains
71 * `requested_offset_in_file`, the mapping is kept.
73 * Set `ds_file->requested_offset_in_mapping` based on `request_offset_in_file`,
74 * such that the next call to `request_bytes` will return bytes starting at that
77 * `requested_offset_in_file` must be a valid offset in the file.
79 static enum ctf_msg_iter_medium_status
ds_file_mmap(struct ctf_fs_ds_file
*ds_file
,
80 off_t requested_offset_in_file
)
82 enum ctf_msg_iter_medium_status status
;
84 /* Ensure the requested offset is in the file range. */
85 BT_ASSERT(requested_offset_in_file
>= 0);
86 BT_ASSERT(requested_offset_in_file
< ds_file
->file
->size
);
89 * If the mapping already contains the requested offset, just adjust
90 * requested_offset_in_mapping.
92 if (offset_ist_mapped(ds_file
, requested_offset_in_file
)) {
93 ds_file
->request_offset_in_mapping
=
94 requested_offset_in_file
- ds_file
->mmap_offset_in_file
;
95 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
99 /* Unmap old region */
100 status
= ds_file_munmap(ds_file
);
101 if (status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
106 * Compute a mapping that has the required alignment properties and
107 * contains `requested_offset_in_file`.
109 ds_file
->request_offset_in_mapping
=
110 requested_offset_in_file
%
111 bt_mmap_get_offset_align_size(static_cast<int>(ds_file
->logger
.level()));
112 ds_file
->mmap_offset_in_file
= requested_offset_in_file
- ds_file
->request_offset_in_mapping
;
114 MIN(ds_file
->file
->size
- ds_file
->mmap_offset_in_file
, ds_file
->mmap_max_len
);
116 BT_ASSERT(ds_file
->mmap_len
> 0);
119 bt_mmap(ds_file
->mmap_len
, PROT_READ
, MAP_PRIVATE
, fileno(ds_file
->file
->fp
),
120 ds_file
->mmap_offset_in_file
, static_cast<int>(ds_file
->logger
.level()));
121 if (ds_file
->mmap_addr
== MAP_FAILED
) {
122 BT_CPPLOGE_SPEC(ds_file
->logger
,
123 "Cannot memory-map address (size {}) of file \"{}\" ({}) at offset {}: {}",
124 ds_file
->mmap_len
, ds_file
->file
->path
->str
, fmt::ptr(ds_file
->file
->fp
),
125 (intmax_t) ds_file
->mmap_offset_in_file
, strerror(errno
));
126 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
130 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
137 * Change the mapping of the file to read the region that follows the current
140 * If the file hasn't been mapped yet, then everything (mmap_offset_in_file,
141 * mmap_len, request_offset_in_mapping) should have the value 0, which will
142 * result in the beginning of the file getting mapped.
144 * return _EOF if the current mapping is the end of the file.
147 static enum ctf_msg_iter_medium_status
ds_file_mmap_next(struct ctf_fs_ds_file
*ds_file
)
149 enum ctf_msg_iter_medium_status status
;
152 * If we're called, it's because more bytes are requested but we have
153 * given all the bytes of the current mapping.
155 BT_ASSERT(ds_file
->request_offset_in_mapping
== ds_file
->mmap_len
);
158 * If the current mapping coincides with the end of the file, there is
161 if (ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
== ds_file
->file
->size
) {
162 status
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
166 status
= ds_file_mmap(ds_file
, ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
);
172 static enum ctf_msg_iter_medium_status
medop_request_bytes(size_t request_sz
, uint8_t **buffer_addr
,
173 size_t *buffer_sz
, void *data
)
175 enum ctf_msg_iter_medium_status status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
176 struct ctf_fs_ds_file
*ds_file
= (struct ctf_fs_ds_file
*) data
;
178 BT_ASSERT(request_sz
> 0);
181 * Check if we have at least one memory-mapped byte left. If we don't,
182 * mmap the next file.
184 if (remaining_mmap_bytes(ds_file
) == 0) {
185 /* Are we at the end of the file? */
186 if (ds_file
->mmap_offset_in_file
>= ds_file
->file
->size
) {
187 BT_CPPLOGD_SPEC(ds_file
->logger
, "Reached end of file \"{}\" ({})",
188 ds_file
->file
->path
->str
, fmt::ptr(ds_file
->file
->fp
));
189 status
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
193 status
= ds_file_mmap_next(ds_file
);
195 case CTF_MSG_ITER_MEDIUM_STATUS_OK
:
197 case CTF_MSG_ITER_MEDIUM_STATUS_EOF
:
200 BT_CPPLOGE_SPEC(ds_file
->logger
, "Cannot memory-map next region of file \"{}\" ({})",
201 ds_file
->file
->path
->str
, fmt::ptr(ds_file
->file
->fp
));
206 BT_ASSERT(remaining_mmap_bytes(ds_file
) > 0);
207 *buffer_sz
= MIN(remaining_mmap_bytes(ds_file
), request_sz
);
209 BT_ASSERT(ds_file
->mmap_addr
);
210 *buffer_addr
= ((uint8_t *) ds_file
->mmap_addr
) + ds_file
->request_offset_in_mapping
;
212 ds_file
->request_offset_in_mapping
+= *buffer_sz
;
216 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
222 static bt_stream
*medop_borrow_stream(bt_stream_class
*stream_class
, int64_t, void *data
)
224 struct ctf_fs_ds_file
*ds_file
= (struct ctf_fs_ds_file
*) data
;
225 bt_stream_class
*ds_file_stream_class
;
226 bt_stream
*stream
= NULL
;
228 ds_file_stream_class
= ds_file
->stream
->cls().libObjPtr();
230 if (stream_class
!= ds_file_stream_class
) {
232 * Not supported: two packets described by two different
233 * stream classes within the same data stream file.
238 stream
= ds_file
->stream
->libObjPtr();
244 static enum ctf_msg_iter_medium_status
medop_seek(off_t offset
, void *data
)
246 struct ctf_fs_ds_file
*ds_file
= (struct ctf_fs_ds_file
*) data
;
248 BT_ASSERT(offset
>= 0);
249 BT_ASSERT(offset
< ds_file
->file
->size
);
251 return ds_file_mmap(ds_file
, offset
);
254 struct ctf_msg_iter_medium_ops ctf_fs_ds_file_medops
= {
261 struct ctf_fs_ds_group_medops_data
263 explicit ctf_fs_ds_group_medops_data(const bt2c::Logger
& parentLogger
) :
264 logger
{parentLogger
, "PLUGIN/SRC.CTF.FS/DS-GROUP-MEDOPS"}
270 /* Weak, set once at creation time. */
271 struct ctf_fs_ds_file_group
*ds_file_group
= nullptr;
274 * Index (as in element rank) of the index entry of ds_file_groups'
275 * index we will read next (so, the one after the one we are reading
278 guint next_index_entry_index
= 0;
281 * File we are currently reading. Changes whenever we switch to
282 * reading another data file.
286 struct ctf_fs_ds_file
*file
= nullptr;
288 /* Weak, for context / logging / appending causes. */
289 bt_self_message_iterator
*self_msg_iter
= nullptr;
292 static enum ctf_msg_iter_medium_status
medop_group_request_bytes(size_t request_sz
,
293 uint8_t **buffer_addr
,
294 size_t *buffer_sz
, void *void_data
)
296 struct ctf_fs_ds_group_medops_data
*data
= (struct ctf_fs_ds_group_medops_data
*) void_data
;
298 /* Return bytes from the current file. */
299 return medop_request_bytes(request_sz
, buffer_addr
, buffer_sz
, data
->file
);
302 static bt_stream
*medop_group_borrow_stream(bt_stream_class
*stream_class
, int64_t stream_id
,
305 struct ctf_fs_ds_group_medops_data
*data
= (struct ctf_fs_ds_group_medops_data
*) void_data
;
307 return medop_borrow_stream(stream_class
, stream_id
, data
->file
);
311 * Set `data->file` to prepare it to read the packet described
315 static enum ctf_msg_iter_medium_status
316 ctf_fs_ds_group_medops_set_file(struct ctf_fs_ds_group_medops_data
*data
,
317 struct ctf_fs_ds_index_entry
*index_entry
)
319 enum ctf_msg_iter_medium_status status
;
322 BT_ASSERT(index_entry
);
324 /* Check if that file is already the one mapped. */
325 if (!data
->file
|| strcmp(index_entry
->path
, data
->file
->file
->path
->str
) != 0) {
326 /* Destroy the previously used file. */
327 ctf_fs_ds_file_destroy(data
->file
);
329 /* Create the new file. */
331 ctf_fs_ds_file_create(data
->ds_file_group
->ctf_fs_trace
, data
->ds_file_group
->stream
,
332 index_entry
->path
, data
->logger
);
334 BT_CPPLOGE_APPEND_CAUSE_SPEC(data
->logger
, "failed to create ctf_fs_ds_file.");
335 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
341 * Ensure the right portion of the file will be returned on the next
342 * request_bytes call.
344 status
= ds_file_mmap(data
->file
, index_entry
->offset
.bytes());
345 if (status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
349 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
355 static enum ctf_msg_iter_medium_status
medop_group_switch_packet(void *void_data
)
357 struct ctf_fs_ds_group_medops_data
*data
= (struct ctf_fs_ds_group_medops_data
*) void_data
;
358 struct ctf_fs_ds_index_entry
*index_entry
;
359 enum ctf_msg_iter_medium_status status
;
361 /* If we have gone through all index entries, we are done. */
362 if (data
->next_index_entry_index
>= data
->ds_file_group
->index
->entries
.size()) {
363 status
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
368 * Otherwise, look up the next index entry / packet and prepare it
371 index_entry
= data
->ds_file_group
->index
->entries
[data
->next_index_entry_index
].get();
373 status
= ctf_fs_ds_group_medops_set_file(data
, index_entry
);
374 if (status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
378 data
->next_index_entry_index
++;
380 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
385 void ctf_fs_ds_group_medops_data_destroy(struct ctf_fs_ds_group_medops_data
*data
)
391 ctf_fs_ds_file_destroy(data
->file
);
399 enum ctf_msg_iter_medium_status
ctf_fs_ds_group_medops_data_create(
400 struct ctf_fs_ds_file_group
*ds_file_group
, bt_self_message_iterator
*self_msg_iter
,
401 const bt2c::Logger
& parentLogger
, struct ctf_fs_ds_group_medops_data
**out
)
403 BT_ASSERT(self_msg_iter
);
404 BT_ASSERT(ds_file_group
);
405 BT_ASSERT(ds_file_group
->index
);
406 BT_ASSERT(!ds_file_group
->index
->entries
.empty());
408 ctf_fs_ds_group_medops_data
*data
= new ctf_fs_ds_group_medops_data
{parentLogger
};
409 data
->ds_file_group
= ds_file_group
;
410 data
->self_msg_iter
= self_msg_iter
;
413 * No need to prepare the first file. ctf_msg_iter will call
414 * switch_packet before reading the first packet, it will be
419 return CTF_MSG_ITER_MEDIUM_STATUS_OK
;
422 void ctf_fs_ds_group_medops_data_reset(struct ctf_fs_ds_group_medops_data
*data
)
424 data
->next_index_entry_index
= 0;
427 struct ctf_msg_iter_medium_ops ctf_fs_ds_group_medops
= {
428 .request_bytes
= medop_group_request_bytes
,
431 * We don't support seeking using this medops. It would probably be
432 * possible, but it's not needed at the moment.
436 .switch_packet
= medop_group_switch_packet
,
437 .borrow_stream
= medop_group_borrow_stream
,
440 static ctf_fs_ds_index_entry::UP
ctf_fs_ds_index_entry_create(const bt2c::DataLen offset
,
441 const bt2c::DataLen packetSize
)
443 ctf_fs_ds_index_entry::UP entry
= bt2s::make_unique
<ctf_fs_ds_index_entry
>(offset
, packetSize
);
445 entry
->packet_seq_num
= UINT64_MAX
;
450 static int convert_cycles_to_ns(struct ctf_clock_class
*clock_class
, uint64_t cycles
, int64_t *ns
)
452 return bt_util_clock_cycles_to_ns_from_origin(cycles
, clock_class
->frequency
,
453 clock_class
->offset_seconds
,
454 clock_class
->offset_cycles
, ns
);
457 static ctf_fs_ds_index::UP
build_index_from_idx_file(struct ctf_fs_ds_file
*ds_file
,
458 struct ctf_fs_ds_file_info
*file_info
,
459 struct ctf_msg_iter
*msg_iter
)
462 gchar
*directory
= NULL
;
463 gchar
*basename
= NULL
;
464 GString
*index_basename
= NULL
;
465 gchar
*index_file_path
= NULL
;
466 GMappedFile
*mapped_file
= NULL
;
468 const char *mmap_begin
= NULL
, *file_pos
= NULL
;
469 const struct ctf_packet_index_file_hdr
*header
= NULL
;
470 ctf_fs_ds_index::UP index
;
471 ctf_fs_ds_index_entry::UP index_entry
;
472 ctf_fs_ds_index_entry
*prev_index_entry
= NULL
;
473 auto totalPacketsSize
= bt2c::DataLen::fromBytes(0);
474 size_t file_index_entry_size
;
475 size_t file_entry_count
;
477 struct ctf_stream_class
*sc
;
478 struct ctf_msg_iter_packet_properties props
;
479 uint32_t version_major
, version_minor
;
481 BT_CPPLOGI_SPEC(ds_file
->logger
, "Building index from .idx file of stream file {}",
482 ds_file
->file
->path
->str
);
483 ret
= ctf_msg_iter_get_packet_properties(msg_iter
, &props
);
485 BT_CPPLOGI_STR_SPEC(ds_file
->logger
,
486 "Cannot read first packet's header and context fields.");
490 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
, props
.stream_class_id
);
492 if (!sc
->default_clock_class
) {
493 BT_CPPLOGI_STR_SPEC(ds_file
->logger
, "Cannot find stream class's default clock class.");
497 /* Look for index file in relative path index/name.idx. */
498 basename
= g_path_get_basename(ds_file
->file
->path
->str
);
500 BT_CPPLOGE_SPEC(ds_file
->logger
, "Cannot get the basename of datastream file {}",
501 ds_file
->file
->path
->str
);
505 directory
= g_path_get_dirname(ds_file
->file
->path
->str
);
507 BT_CPPLOGE_SPEC(ds_file
->logger
, "Cannot get dirname of datastream file {}",
508 ds_file
->file
->path
->str
);
512 index_basename
= g_string_new(basename
);
513 if (!index_basename
) {
514 BT_CPPLOGE_STR_SPEC(ds_file
->logger
, "Cannot allocate index file basename string");
518 g_string_append(index_basename
, ".idx");
519 index_file_path
= g_build_filename(directory
, "index", index_basename
->str
, NULL
);
520 mapped_file
= g_mapped_file_new(index_file_path
, FALSE
, NULL
);
522 BT_CPPLOGD_SPEC(ds_file
->logger
, "Cannot create new mapped file {}", index_file_path
);
527 * The g_mapped_file API limits us to 4GB files on 32-bit.
528 * Traces with such large indexes have never been seen in the wild,
529 * but this would need to be adjusted to support them.
531 filesize
= g_mapped_file_get_length(mapped_file
);
532 if (filesize
< sizeof(*header
)) {
533 BT_CPPLOGW_SPEC(ds_file
->logger
,
534 "Invalid LTTng trace index file: "
535 "file size ({} bytes) < header size ({} bytes)",
536 filesize
, sizeof(*header
));
540 mmap_begin
= g_mapped_file_get_contents(mapped_file
);
541 header
= (struct ctf_packet_index_file_hdr
*) mmap_begin
;
543 file_pos
= g_mapped_file_get_contents(mapped_file
) + sizeof(*header
);
544 if (be32toh(header
->magic
) != CTF_INDEX_MAGIC
) {
545 BT_CPPLOGW_STR_SPEC(ds_file
->logger
,
546 "Invalid LTTng trace index: \"magic\" field validation failed");
550 version_major
= be32toh(header
->index_major
);
551 version_minor
= be32toh(header
->index_minor
);
552 if (version_major
!= 1) {
553 BT_CPPLOGW_SPEC(ds_file
->logger
, "Unknown LTTng trace index version: major={}, minor={}",
554 version_major
, version_minor
);
558 file_index_entry_size
= be32toh(header
->packet_index_len
);
559 if (file_index_entry_size
< CTF_INDEX_1_0_SIZE
) {
562 "Invalid `packet_index_len` in LTTng trace index file (`packet_index_len` < CTF index 1.0 index entry size): "
563 "packet_index_len={}, CTF_INDEX_1_0_SIZE={}",
564 file_index_entry_size
, CTF_INDEX_1_0_SIZE
);
568 file_entry_count
= (filesize
- sizeof(*header
)) / file_index_entry_size
;
569 if ((filesize
- sizeof(*header
)) % file_index_entry_size
) {
570 BT_CPPLOGW_SPEC(ds_file
->logger
,
571 "Invalid LTTng trace index: the index's size after the header "
572 "({} bytes) is not a multiple of the index entry size "
574 (filesize
- sizeof(*header
)), sizeof(*header
));
578 index
= ctf_fs_ds_index_create();
583 for (i
= 0; i
< file_entry_count
; i
++) {
584 struct ctf_packet_index
*file_index
= (struct ctf_packet_index
*) file_pos
;
585 const auto packetSize
= bt2c::DataLen::fromBits(be64toh(file_index
->packet_size
));
587 if (packetSize
.hasExtraBits()) {
588 BT_CPPLOGW_SPEC(ds_file
->logger
,
589 "Invalid packet size encountered in LTTng trace index file");
593 const auto offset
= bt2c::DataLen::fromBytes(be64toh(file_index
->offset
));
595 if (i
!= 0 && offset
< prev_index_entry
->offset
) {
598 "Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
599 "previous offset={} bytes, current offset={} bytes",
600 prev_index_entry
->offset
.bytes(), offset
.bytes());
604 index_entry
= ctf_fs_ds_index_entry_create(offset
, packetSize
);
606 BT_CPPLOGE_APPEND_CAUSE_SPEC(ds_file
->logger
,
607 "Failed to create a ctf_fs_ds_index_entry.");
611 /* Set path to stream file. */
612 index_entry
->path
= file_info
->path
.c_str();
614 index_entry
->timestamp_begin
= be64toh(file_index
->timestamp_begin
);
615 index_entry
->timestamp_end
= be64toh(file_index
->timestamp_end
);
616 if (index_entry
->timestamp_end
< index_entry
->timestamp_begin
) {
619 "Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
620 "timestamp_begin={}, timestamp_end={}",
621 index_entry
->timestamp_begin
, index_entry
->timestamp_end
);
625 /* Convert the packet's bound to nanoseconds since Epoch. */
626 ret
= convert_cycles_to_ns(sc
->default_clock_class
, index_entry
->timestamp_begin
,
627 &index_entry
->timestamp_begin_ns
);
631 "Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
634 ret
= convert_cycles_to_ns(sc
->default_clock_class
, index_entry
->timestamp_end
,
635 &index_entry
->timestamp_end_ns
);
639 "Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
643 if (version_minor
>= 1) {
644 index_entry
->packet_seq_num
= be64toh(file_index
->packet_seq_num
);
647 totalPacketsSize
+= packetSize
;
648 file_pos
+= file_index_entry_size
;
650 prev_index_entry
= index_entry
.get();
652 index
->entries
.emplace_back(std::move(index_entry
));
655 /* Validate that the index addresses the complete stream. */
656 if (ds_file
->file
->size
!= totalPacketsSize
.bytes()) {
657 BT_CPPLOGW_SPEC(ds_file
->logger
,
658 "Invalid LTTng trace index file; indexed size != stream file size: "
659 "file-size={} bytes, total-packets-size={} bytes",
660 ds_file
->file
->size
, totalPacketsSize
.bytes());
666 g_free(index_file_path
);
667 if (index_basename
) {
668 g_string_free(index_basename
, TRUE
);
671 g_mapped_file_unref(mapped_file
);
679 static int init_index_entry(struct ctf_fs_ds_index_entry
*entry
, struct ctf_fs_ds_file
*ds_file
,
680 struct ctf_msg_iter_packet_properties
*props
)
683 struct ctf_stream_class
*sc
;
685 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
, props
->stream_class_id
);
688 if (props
->snapshots
.beginning_clock
!= UINT64_C(-1)) {
689 entry
->timestamp_begin
= props
->snapshots
.beginning_clock
;
691 /* Convert the packet's bound to nanoseconds since Epoch. */
692 ret
= convert_cycles_to_ns(sc
->default_clock_class
, props
->snapshots
.beginning_clock
,
693 &entry
->timestamp_begin_ns
);
695 BT_CPPLOGI_STR_SPEC(ds_file
->logger
,
696 "Failed to convert raw timestamp to nanoseconds since Epoch.");
700 entry
->timestamp_begin
= UINT64_C(-1);
701 entry
->timestamp_begin_ns
= UINT64_C(-1);
704 if (props
->snapshots
.end_clock
!= UINT64_C(-1)) {
705 entry
->timestamp_end
= props
->snapshots
.end_clock
;
707 /* Convert the packet's bound to nanoseconds since Epoch. */
708 ret
= convert_cycles_to_ns(sc
->default_clock_class
, props
->snapshots
.end_clock
,
709 &entry
->timestamp_end_ns
);
711 BT_CPPLOGI_STR_SPEC(ds_file
->logger
,
712 "Failed to convert raw timestamp to nanoseconds since Epoch.");
716 entry
->timestamp_end
= UINT64_C(-1);
717 entry
->timestamp_end_ns
= UINT64_C(-1);
724 static ctf_fs_ds_index::UP
build_index_from_stream_file(struct ctf_fs_ds_file
*ds_file
,
725 struct ctf_fs_ds_file_info
*file_info
,
726 struct ctf_msg_iter
*msg_iter
)
729 ctf_fs_ds_index::UP index
;
730 enum ctf_msg_iter_status iter_status
= CTF_MSG_ITER_STATUS_OK
;
731 auto currentPacketOffset
= bt2c::DataLen::fromBytes(0);
733 BT_CPPLOGI_SPEC(ds_file
->logger
, "Indexing stream file {}", ds_file
->file
->path
->str
);
735 index
= ctf_fs_ds_index_create();
741 struct ctf_msg_iter_packet_properties props
;
743 if (currentPacketOffset
.bytes() > ds_file
->file
->size
) {
744 BT_CPPLOGE_STR_SPEC(ds_file
->logger
,
745 "Unexpected current packet's offset (larger than file).");
747 } else if (currentPacketOffset
.bytes() == ds_file
->file
->size
) {
752 iter_status
= ctf_msg_iter_seek(msg_iter
, currentPacketOffset
.bytes());
753 if (iter_status
!= CTF_MSG_ITER_STATUS_OK
) {
757 iter_status
= ctf_msg_iter_get_packet_properties(msg_iter
, &props
);
758 if (iter_status
!= CTF_MSG_ITER_STATUS_OK
) {
763 * Get the current packet size from the packet header, if set. Else,
764 * assume there is a single packet in the file, so take the file size
765 * as the packet size.
767 const auto currentPacketSize
= props
.exp_packet_total_size
>= 0 ?
768 bt2c::DataLen::fromBits(props
.exp_packet_total_size
) :
769 bt2c::DataLen::fromBytes(ds_file
->file
->size
);
771 if ((currentPacketOffset
+ currentPacketSize
).bytes() > ds_file
->file
->size
) {
772 BT_CPPLOGW_SPEC(ds_file
->logger
,
773 "Invalid packet size reported in file: stream=\"{}\", "
774 "packet-offset-bytes={}, packet-size-bytes={}, "
775 "file-size-bytes={}",
776 ds_file
->file
->path
->str
, currentPacketOffset
.bytes(),
777 currentPacketSize
.bytes(), ds_file
->file
->size
);
781 auto index_entry
= ctf_fs_ds_index_entry_create(currentPacketOffset
, currentPacketSize
);
783 BT_CPPLOGE_APPEND_CAUSE_SPEC(ds_file
->logger
,
784 "Failed to create a ctf_fs_ds_index_entry.");
788 /* Set path to stream file. */
789 index_entry
->path
= file_info
->path
.c_str();
791 ret
= init_index_entry(index_entry
.get(), ds_file
, &props
);
796 index
->entries
.emplace_back(std::move(index_entry
));
798 currentPacketOffset
+= currentPacketSize
;
799 BT_CPPLOGD_SPEC(ds_file
->logger
,
800 "Seeking to next packet: current-packet-offset-bytes={}, "
801 "next-packet-offset-bytes={}",
802 (currentPacketOffset
- currentPacketSize
).bytes(),
803 currentPacketOffset
.bytes());
814 struct ctf_fs_ds_file
*ctf_fs_ds_file_create(struct ctf_fs_trace
*ctf_fs_trace
,
815 bt2::Stream::Shared stream
, const char *path
,
816 const bt2c::Logger
& parentLogger
)
820 ctf_fs_ds_file
*ds_file
= new ctf_fs_ds_file
{parentLogger
};
826 ds_file
->file
= ctf_fs_file_create(parentLogger
);
827 if (!ds_file
->file
) {
831 ds_file
->stream
= std::move(stream
);
832 ds_file
->metadata
= ctf_fs_trace
->metadata
;
833 g_string_assign(ds_file
->file
->path
, path
);
834 ret
= ctf_fs_file_open(ds_file
->file
, "rb");
839 offset_align
= bt_mmap_get_offset_align_size(static_cast<int>(ds_file
->logger
.level()));
840 ds_file
->mmap_max_len
= offset_align
* 2048;
845 /* Do not touch "borrowed" file. */
846 ctf_fs_ds_file_destroy(ds_file
);
853 ctf_fs_ds_index::UP
ctf_fs_ds_file_build_index(struct ctf_fs_ds_file
*ds_file
,
854 struct ctf_fs_ds_file_info
*file_info
,
855 struct ctf_msg_iter
*msg_iter
)
857 auto index
= build_index_from_idx_file(ds_file
, file_info
, msg_iter
);
862 BT_CPPLOGI_SPEC(ds_file
->logger
, "Failed to build index from .index file; "
863 "falling back to stream indexing.");
864 index
= build_index_from_stream_file(ds_file
, file_info
, msg_iter
);
869 ctf_fs_ds_index::UP
ctf_fs_ds_index_create()
871 return bt2s::make_unique
<ctf_fs_ds_index
>();
874 void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file
*ds_file
)
880 (void) ds_file_munmap(ds_file
);
883 ctf_fs_file_destroy(ds_file
->file
);
889 ctf_fs_ds_file_info::UP
ctf_fs_ds_file_info_create(const char *path
, int64_t begin_ns
)
891 ctf_fs_ds_file_info::UP ds_file_info
= bt2s::make_unique
<ctf_fs_ds_file_info
>();
893 ds_file_info
->path
= path
;
894 ds_file_info
->begin_ns
= begin_ns
;
898 ctf_fs_ds_file_group::UP
ctf_fs_ds_file_group_create(struct ctf_fs_trace
*ctf_fs_trace
,
899 struct ctf_stream_class
*sc
,
900 uint64_t stream_instance_id
,
901 ctf_fs_ds_index::UP index
)
903 ctf_fs_ds_file_group::UP ds_file_group
{new ctf_fs_ds_file_group
};
905 ds_file_group
->index
= std::move(index
);
907 ds_file_group
->stream_id
= stream_instance_id
;
909 ds_file_group
->sc
= sc
;
910 ds_file_group
->ctf_fs_trace
= ctf_fs_trace
;
912 return ds_file_group
;