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
9 #define BT_CLOG_CFG (logCfg)
10 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/DS"
18 #include "compat/mman.h"
19 #include "compat/endian.h"
20 #include <babeltrace2/babeltrace.h>
21 #include "common/common.h"
23 #include "metadata.hpp"
24 #include "../common/src/msg-iter/msg-iter.hpp"
25 #include "common/assert.h"
26 #include "data-stream-file.hpp"
28 #include "cpp-common/cfg-logging.hpp"
29 #include "cpp-common/cfg-logging-error-reporting.hpp"
30 #include "cpp-common/make-unique.hpp"
33 static inline size_t remaining_mmap_bytes(struct ctf_fs_ds_file
*ds_file
)
35 BT_ASSERT_DBG(ds_file
->mmap_len
>= ds_file
->request_offset_in_mapping
);
36 return ds_file
->mmap_len
- ds_file
->request_offset_in_mapping
;
40 * Return true if `offset_in_file` is in the current mapping.
43 static bool offset_ist_mapped(struct ctf_fs_ds_file
*ds_file
, off_t offset_in_file
)
45 return offset_in_file
>= ds_file
->mmap_offset_in_file
&&
46 offset_in_file
< (ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
);
49 static enum ctf_msg_iter_medium_status
ds_file_munmap(struct ctf_fs_ds_file
*ds_file
)
51 const bt2_common::LogCfg
& logCfg
= ds_file
->logCfg
;
55 if (!ds_file
->mmap_addr
) {
56 return CTF_MSG_ITER_MEDIUM_STATUS_OK
;
59 if (bt_munmap(ds_file
->mmap_addr
, ds_file
->mmap_len
)) {
60 BT_CLOGE_ERRNO("Cannot memory-unmap file",
61 ": address=%p, size=%zu, file_path=\"%s\", file=%p", ds_file
->mmap_addr
,
62 ds_file
->mmap_len
, ds_file
->file
? ds_file
->file
->path
.c_str() : "NULL",
63 ds_file
->file
? ds_file
->file
->fp
.get() : NULL
);
64 return CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
67 ds_file
->mmap_addr
= NULL
;
69 return CTF_MSG_ITER_MEDIUM_STATUS_OK
;
73 * mmap a region of `ds_file` such that `requested_offset_in_file` is in the
74 * mapping. If the currently mmap-ed region already contains
75 * `requested_offset_in_file`, the mapping is kept.
77 * Set `ds_file->requested_offset_in_mapping` based on `request_offset_in_file`,
78 * such that the next call to `request_bytes` will return bytes starting at that
81 * `requested_offset_in_file` must be a valid offset in the file.
83 static enum ctf_msg_iter_medium_status
ds_file_mmap(struct ctf_fs_ds_file
*ds_file
,
84 off_t requested_offset_in_file
)
86 const bt2_common::LogCfg
& logCfg
= ds_file
->logCfg
;
88 /* Ensure the requested offset is in the file range. */
89 BT_ASSERT(requested_offset_in_file
>= 0);
90 BT_ASSERT(requested_offset_in_file
< ds_file
->file
->size
);
93 * If the mapping already contains the requested offset, just adjust
94 * requested_offset_in_mapping.
96 if (offset_ist_mapped(ds_file
, requested_offset_in_file
)) {
97 ds_file
->request_offset_in_mapping
=
98 requested_offset_in_file
- ds_file
->mmap_offset_in_file
;
99 return CTF_MSG_ITER_MEDIUM_STATUS_OK
;
102 /* Unmap old region */
103 ctf_msg_iter_medium_status status
= ds_file_munmap(ds_file
);
104 if (status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
109 * Compute a mapping that has the required alignment properties and
110 * contains `requested_offset_in_file`.
112 ds_file
->request_offset_in_mapping
=
113 requested_offset_in_file
% bt_mmap_get_offset_align_size(logCfg
.logLevel());
114 ds_file
->mmap_offset_in_file
= requested_offset_in_file
- ds_file
->request_offset_in_mapping
;
116 MIN(ds_file
->file
->size
- ds_file
->mmap_offset_in_file
, ds_file
->mmap_max_len
);
118 BT_ASSERT(ds_file
->mmap_len
> 0);
121 bt_mmap((void *) 0, ds_file
->mmap_len
, PROT_READ
, MAP_PRIVATE
,
122 fileno(ds_file
->file
->fp
.get()), ds_file
->mmap_offset_in_file
, logCfg
.logLevel());
123 if (ds_file
->mmap_addr
== MAP_FAILED
) {
124 BT_CLOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s",
125 ds_file
->mmap_len
, ds_file
->file
->path
.c_str(), ds_file
->file
->fp
.get(),
126 (intmax_t) ds_file
->mmap_offset_in_file
, strerror(errno
));
127 return CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
130 return CTF_MSG_ITER_MEDIUM_STATUS_OK
;
134 * Change the mapping of the file to read the region that follows the current
137 * If the file hasn't been mapped yet, then everything (mmap_offset_in_file,
138 * mmap_len, request_offset_in_mapping) should have the value 0, which will
139 * result in the beginning of the file getting mapped.
141 * return _EOF if the current mapping is the end of the file.
144 static enum ctf_msg_iter_medium_status
ds_file_mmap_next(struct ctf_fs_ds_file
*ds_file
)
147 * If we're called, it's because more bytes are requested but we have
148 * given all the bytes of the current mapping.
150 BT_ASSERT(ds_file
->request_offset_in_mapping
== ds_file
->mmap_len
);
153 * If the current mapping coincides with the end of the file, there is
156 if (ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
== ds_file
->file
->size
) {
157 return CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
160 return ds_file_mmap(ds_file
, ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
);
163 static enum ctf_msg_iter_medium_status
medop_request_bytes(size_t request_sz
, uint8_t **buffer_addr
,
164 size_t *buffer_sz
, void *data
)
166 struct ctf_fs_ds_file
*ds_file
= (struct ctf_fs_ds_file
*) data
;
167 const bt2_common::LogCfg
& logCfg
= ds_file
->logCfg
;
169 BT_ASSERT(request_sz
> 0);
172 * Check if we have at least one memory-mapped byte left. If we don't,
173 * mmap the next file.
175 if (remaining_mmap_bytes(ds_file
) == 0) {
176 /* Are we at the end of the file? */
177 if (ds_file
->mmap_offset_in_file
>= ds_file
->file
->size
) {
178 BT_CLOGD("Reached end of file \"%s\" (%p)", ds_file
->file
->path
.c_str(),
179 ds_file
->file
->fp
.get());
180 return CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
183 ctf_msg_iter_medium_status status
= ds_file_mmap_next(ds_file
);
185 case CTF_MSG_ITER_MEDIUM_STATUS_OK
:
187 case CTF_MSG_ITER_MEDIUM_STATUS_EOF
:
188 return CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
190 BT_CLOGE("Cannot memory-map next region of file \"%s\" (%p)",
191 ds_file
->file
->path
.c_str(), ds_file
->file
->fp
.get());
196 BT_ASSERT(remaining_mmap_bytes(ds_file
) > 0);
197 *buffer_sz
= MIN(remaining_mmap_bytes(ds_file
), request_sz
);
199 BT_ASSERT(ds_file
->mmap_addr
);
200 *buffer_addr
= ((uint8_t *) ds_file
->mmap_addr
) + ds_file
->request_offset_in_mapping
;
202 ds_file
->request_offset_in_mapping
+= *buffer_sz
;
204 return CTF_MSG_ITER_MEDIUM_STATUS_OK
;
207 static bt_stream
*medop_borrow_stream(bt_stream_class
*stream_class
, int64_t stream_id
, void *data
)
209 struct ctf_fs_ds_file
*ds_file
= (struct ctf_fs_ds_file
*) data
;
210 bt_stream_class
*ds_file_stream_class
;
212 ds_file_stream_class
= (*ds_file
->stream
)->cls().libObjPtr();
214 if (stream_class
!= ds_file_stream_class
) {
216 * Not supported: two packets described by two different
217 * stream classes within the same data stream file.
222 return (*ds_file
->stream
)->libObjPtr();
225 static enum ctf_msg_iter_medium_status
medop_seek(off_t offset
, void *data
)
227 struct ctf_fs_ds_file
*ds_file
= (struct ctf_fs_ds_file
*) data
;
229 BT_ASSERT(offset
>= 0);
230 BT_ASSERT(offset
< ds_file
->file
->size
);
232 return ds_file_mmap(ds_file
, offset
);
236 struct ctf_msg_iter_medium_ops ctf_fs_ds_file_medops
= {
243 struct ctf_fs_ds_group_medops_data
245 explicit ctf_fs_ds_group_medops_data(const bt2_common::LogCfg
& logCfgParam
) noexcept
:
250 /* Weak, set once at creation time. */
251 struct ctf_fs_ds_file_group
*ds_file_group
= nullptr;
254 * Index (as in element rank) of the index entry of ds_file_groups'
255 * index we will read next (so, the one after the one we are reading
258 guint next_index_entry_index
= 0;
261 * File we are currently reading. Changes whenever we switch to
262 * reading another data file.
266 ctf_fs_ds_file::UP file
;
268 /* Weak, for context / logging / appending causes. */
269 bt_self_message_iterator
*self_msg_iter
= nullptr;
270 const bt2_common::LogCfg logCfg
;
273 static enum ctf_msg_iter_medium_status
medop_group_request_bytes(size_t request_sz
,
274 uint8_t **buffer_addr
,
275 size_t *buffer_sz
, void *void_data
)
277 struct ctf_fs_ds_group_medops_data
*data
= (struct ctf_fs_ds_group_medops_data
*) void_data
;
279 /* Return bytes from the current file. */
280 return medop_request_bytes(request_sz
, buffer_addr
, buffer_sz
, data
->file
.get());
283 static bt_stream
*medop_group_borrow_stream(bt_stream_class
*stream_class
, int64_t stream_id
,
286 struct ctf_fs_ds_group_medops_data
*data
= (struct ctf_fs_ds_group_medops_data
*) void_data
;
288 return medop_borrow_stream(stream_class
, stream_id
, data
->file
.get());
292 * Set `data->file` to prepare it to read the packet described
296 static enum ctf_msg_iter_medium_status
297 ctf_fs_ds_group_medops_set_file(struct ctf_fs_ds_group_medops_data
*data
,
298 struct ctf_fs_ds_index_entry
*index_entry
)
301 BT_ASSERT(index_entry
);
303 /* Check if that file is already the one mapped. */
304 if (!data
->file
|| strcmp(index_entry
->path
, data
->file
->file
->path
.c_str()) != 0) {
305 /* Create the new file. */
307 ctf_fs_ds_file_create(data
->ds_file_group
->ctf_fs_trace
, data
->ds_file_group
->stream
,
308 index_entry
->path
, data
->logCfg
);
311 const bt2_common::LogCfg
& logCfg
= data
->logCfg
;
312 BT_CLOGE_APPEND_CAUSE("failed to create ctf_fs_ds_file.");
313 return CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
318 * Ensure the right portion of the file will be returned on the next
319 * request_bytes call.
321 return ds_file_mmap(data
->file
.get(), index_entry
->offset
.bytes());
324 static enum ctf_msg_iter_medium_status
medop_group_switch_packet(void *void_data
)
326 struct ctf_fs_ds_group_medops_data
*data
= (struct ctf_fs_ds_group_medops_data
*) void_data
;
328 /* If we have gone through all index entries, we are done. */
329 if (data
->next_index_entry_index
>= data
->ds_file_group
->index
.entries
.size()) {
330 return CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
334 * Otherwise, look up the next index entry / packet and prepare it
337 ctf_fs_ds_index_entry
& index_entry
=
338 data
->ds_file_group
->index
.entries
[data
->next_index_entry_index
];
340 ctf_msg_iter_medium_status status
= ctf_fs_ds_group_medops_set_file(data
, &index_entry
);
341 if (status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
345 data
->next_index_entry_index
++;
347 return CTF_MSG_ITER_MEDIUM_STATUS_OK
;
350 void ctf_fs_ds_group_medops_data_deleter::operator()(ctf_fs_ds_group_medops_data
*data
)
355 enum ctf_msg_iter_medium_status
ctf_fs_ds_group_medops_data_create(
356 struct ctf_fs_ds_file_group
*ds_file_group
, bt_self_message_iterator
*self_msg_iter
,
357 const bt2_common::LogCfg
& logCfg
, ctf_fs_ds_group_medops_data_up
& out
)
359 BT_ASSERT(self_msg_iter
);
360 BT_ASSERT(ds_file_group
);
361 BT_ASSERT(!ds_file_group
->index
.entries
.empty());
363 out
.reset(new ctf_fs_ds_group_medops_data
{logCfg
});
365 out
->ds_file_group
= ds_file_group
;
366 out
->self_msg_iter
= self_msg_iter
;
369 * No need to prepare the first file. ctf_msg_iter will call
370 * switch_packet before reading the first packet, it will be
374 return CTF_MSG_ITER_MEDIUM_STATUS_OK
;
377 void ctf_fs_ds_group_medops_data_reset(struct ctf_fs_ds_group_medops_data
*data
)
379 data
->next_index_entry_index
= 0;
382 struct ctf_msg_iter_medium_ops ctf_fs_ds_group_medops
= {
383 .request_bytes
= medop_group_request_bytes
,
386 * We don't support seeking using this medops. It would probably be
387 * possible, but it's not needed at the moment.
391 .switch_packet
= medop_group_switch_packet
,
392 .borrow_stream
= medop_group_borrow_stream
,
395 static int convert_cycles_to_ns(struct ctf_clock_class
*clock_class
, uint64_t cycles
, int64_t *ns
)
397 return bt_util_clock_cycles_to_ns_from_origin(cycles
, clock_class
->frequency
,
398 clock_class
->offset_seconds
,
399 clock_class
->offset_cycles
, ns
);
402 static nonstd::optional
<ctf_fs_ds_index
>
403 build_index_from_idx_file(struct ctf_fs_ds_file
*ds_file
, struct ctf_fs_ds_file_info
*file_info
,
404 struct ctf_msg_iter
*msg_iter
)
406 const bt2_common::LogCfg
& logCfg
= ds_file
->logCfg
;
408 BT_CLOGI("Building index from .idx file of stream file %s", ds_file
->file
->path
.c_str());
409 ctf_msg_iter_packet_properties props
;
410 int ret
= ctf_msg_iter_get_packet_properties(msg_iter
, &props
);
412 BT_CLOGI_STR("Cannot read first packet's header and context fields.");
413 return nonstd::nullopt
;
416 ctf_stream_class
*sc
=
417 ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
, props
.stream_class_id
);
419 if (!sc
->default_clock_class
) {
420 BT_CLOGI_STR("Cannot find stream class's default clock class.");
421 return nonstd::nullopt
;
424 /* Look for index file in relative path index/name.idx. */
425 bt2_common::GCharUP basename
{g_path_get_basename(ds_file
->file
->path
.c_str())};
427 BT_CLOGE("Cannot get the basename of datastream file %s", ds_file
->file
->path
.c_str());
428 return nonstd::nullopt
;
431 bt2_common::GCharUP directory
{g_path_get_dirname(ds_file
->file
->path
.c_str())};
433 BT_CLOGE("Cannot get dirname of datastream file %s", ds_file
->file
->path
.c_str());
434 return nonstd::nullopt
;
437 std::string index_basename
= basename
.get();
438 index_basename
+= ".idx";
440 bt2_common::GCharUP index_file_path
{
441 g_build_filename(directory
.get(), "index", index_basename
.c_str(), NULL
)};
442 bt2_common::GMappedFileUP mapped_file
{g_mapped_file_new(index_file_path
.get(), FALSE
, NULL
)};
444 BT_CLOGD("Cannot create new mapped file %s", index_file_path
.get());
445 return nonstd::nullopt
;
449 * The g_mapped_file API limits us to 4GB files on 32-bit.
450 * Traces with such large indexes have never been seen in the wild,
451 * but this would need to be adjusted to support them.
453 gsize filesize
= g_mapped_file_get_length(mapped_file
.get());
454 if (filesize
< sizeof(ctf_packet_index_file_hdr
)) {
455 BT_CLOGW("Invalid LTTng trace index file: "
456 "file size (%zu bytes) < header size (%zu bytes)",
457 filesize
, sizeof(ctf_packet_index_file_hdr
));
458 return nonstd::nullopt
;
461 const char *mmap_begin
= g_mapped_file_get_contents(mapped_file
.get());
462 const ctf_packet_index_file_hdr
*header
= (const ctf_packet_index_file_hdr
*) mmap_begin
;
464 const char *file_pos
= g_mapped_file_get_contents(mapped_file
.get()) + sizeof(*header
);
465 if (be32toh(header
->magic
) != CTF_INDEX_MAGIC
) {
466 BT_CLOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
467 return nonstd::nullopt
;
470 uint32_t version_major
= be32toh(header
->index_major
);
471 uint32_t version_minor
= be32toh(header
->index_minor
);
472 if (version_major
!= 1) {
473 BT_CLOGW("Unknown LTTng trace index version: "
474 "major=%" PRIu32
", minor=%" PRIu32
,
475 version_major
, version_minor
);
476 return nonstd::nullopt
;
479 size_t file_index_entry_size
= be32toh(header
->packet_index_len
);
480 if (file_index_entry_size
< CTF_INDEX_1_0_SIZE
) {
482 "Invalid `packet_index_len` in LTTng trace index file (`packet_index_len` < CTF index 1.0 index entry size): "
483 "packet_index_len=%zu, CTF_INDEX_1_0_SIZE=%zu",
484 file_index_entry_size
, CTF_INDEX_1_0_SIZE
);
485 return nonstd::nullopt
;
488 size_t file_entry_count
= (filesize
- sizeof(*header
)) / file_index_entry_size
;
489 if ((filesize
- sizeof(*header
)) % file_index_entry_size
) {
490 BT_CLOGW("Invalid LTTng trace index: the index's size after the header "
491 "(%zu bytes) is not a multiple of the index entry size "
493 (filesize
- sizeof(*header
)), sizeof(*header
));
494 return nonstd::nullopt
;
497 ctf_fs_ds_index index
;
498 ctf_fs_ds_index_entry
*prev_index_entry
= nullptr;
499 bt2_common::DataLen totalPacketsSize
= bt2_common::DataLen::fromBytes(0);
501 for (size_t i
= 0; i
< file_entry_count
; i
++) {
502 struct ctf_packet_index
*file_index
= (struct ctf_packet_index
*) file_pos
;
503 bt2_common::DataLen packetSize
=
504 bt2_common::DataLen::fromBits(be64toh(file_index
->packet_size
));
506 if (packetSize
.hasExtraBits()) {
507 BT_CLOGW("Invalid packet size encountered in LTTng trace index file");
508 return nonstd::nullopt
;
511 bt2_common::DataLen offset
= bt2_common::DataLen::fromBytes(be64toh(file_index
->offset
));
512 if (i
!= 0 && offset
< prev_index_entry
->offset
) {
513 BT_CLOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
514 "previous offset=%llu bytes, current offset=%llu bytes",
515 prev_index_entry
->offset
.bytes(), offset
.bytes());
516 return nonstd::nullopt
;
519 ctf_fs_ds_index_entry index_entry
{offset
, packetSize
};
521 /* Set path to stream file. */
522 index_entry
.path
= file_info
->path
.c_str();
524 index_entry
.timestamp_begin
= be64toh(file_index
->timestamp_begin
);
525 index_entry
.timestamp_end
= be64toh(file_index
->timestamp_end
);
526 if (index_entry
.timestamp_end
< index_entry
.timestamp_begin
) {
528 "Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
529 "timestamp_begin=%" PRIu64
"timestamp_end=%" PRIu64
,
530 index_entry
.timestamp_begin
, index_entry
.timestamp_end
);
531 return nonstd::nullopt
;
534 /* Convert the packet's bound to nanoseconds since Epoch. */
535 ret
= convert_cycles_to_ns(sc
->default_clock_class
, index_entry
.timestamp_begin
,
536 &index_entry
.timestamp_begin_ns
);
539 "Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
540 return nonstd::nullopt
;
542 ret
= convert_cycles_to_ns(sc
->default_clock_class
, index_entry
.timestamp_end
,
543 &index_entry
.timestamp_end_ns
);
546 "Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
547 return nonstd::nullopt
;
550 if (version_minor
>= 1) {
551 index_entry
.packet_seq_num
= be64toh(file_index
->packet_seq_num
);
554 totalPacketsSize
+= packetSize
;
555 file_pos
+= file_index_entry_size
;
557 index
.entries
.emplace_back(index_entry
);
559 prev_index_entry
= &index
.entries
.back();
562 /* Validate that the index addresses the complete stream. */
563 if (ds_file
->file
->size
!= totalPacketsSize
.bytes()) {
564 BT_CLOGW("Invalid LTTng trace index file; indexed size != stream file size: "
565 "file-size=%" PRIu64
" bytes, total-packets-size=%llu bytes",
566 ds_file
->file
->size
, totalPacketsSize
.bytes());
567 return nonstd::nullopt
;
573 static int init_index_entry(ctf_fs_ds_index_entry
& entry
, struct ctf_fs_ds_file
*ds_file
,
574 struct ctf_msg_iter_packet_properties
*props
)
576 ctf_stream_class
*sc
=
577 ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
, props
->stream_class_id
);
579 const bt2_common::LogCfg
& logCfg
= ds_file
->logCfg
;
581 if (props
->snapshots
.beginning_clock
!= UINT64_C(-1)) {
582 entry
.timestamp_begin
= props
->snapshots
.beginning_clock
;
584 /* Convert the packet's bound to nanoseconds since Epoch. */
585 int ret
= convert_cycles_to_ns(sc
->default_clock_class
, props
->snapshots
.beginning_clock
,
586 &entry
.timestamp_begin_ns
);
588 BT_CLOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
592 entry
.timestamp_begin
= UINT64_C(-1);
593 entry
.timestamp_begin_ns
= UINT64_C(-1);
596 if (props
->snapshots
.end_clock
!= UINT64_C(-1)) {
597 entry
.timestamp_end
= props
->snapshots
.end_clock
;
599 /* Convert the packet's bound to nanoseconds since Epoch. */
600 int ret
= convert_cycles_to_ns(sc
->default_clock_class
, props
->snapshots
.end_clock
,
601 &entry
.timestamp_end_ns
);
603 BT_CLOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
607 entry
.timestamp_end
= UINT64_C(-1);
608 entry
.timestamp_end_ns
= UINT64_C(-1);
614 static nonstd::optional
<ctf_fs_ds_index
>
615 build_index_from_stream_file(struct ctf_fs_ds_file
*ds_file
, struct ctf_fs_ds_file_info
*file_info
,
616 struct ctf_msg_iter
*msg_iter
)
618 const bt2_common::LogCfg
& logCfg
= ds_file
->logCfg
;
620 BT_CLOGI("Indexing stream file %s", ds_file
->file
->path
.c_str());
622 ctf_fs_ds_index index
;
623 bt2_common::DataLen currentPacketOffset
= bt2_common::DataLen::fromBytes(0);
626 struct ctf_msg_iter_packet_properties props
;
628 if (currentPacketOffset
.bytes() > ds_file
->file
->size
) {
629 BT_CLOGE_STR("Unexpected current packet's offset (larger than file).");
630 return nonstd::nullopt
;
631 } else if (currentPacketOffset
.bytes() == ds_file
->file
->size
) {
636 ctf_msg_iter_status iter_status
= ctf_msg_iter_seek(msg_iter
, currentPacketOffset
.bytes());
637 if (iter_status
!= CTF_MSG_ITER_STATUS_OK
) {
638 return nonstd::nullopt
;
641 iter_status
= ctf_msg_iter_get_packet_properties(msg_iter
, &props
);
642 if (iter_status
!= CTF_MSG_ITER_STATUS_OK
) {
643 return nonstd::nullopt
;
647 * Get the current packet size from the packet header, if set. Else,
648 * assume there is a single packet in the file, so take the file size
649 * as the packet size.
651 bt2_common::DataLen currentPacketSize
=
652 props
.exp_packet_total_size
>= 0 ?
653 bt2_common::DataLen::fromBits(props
.exp_packet_total_size
) :
654 bt2_common::DataLen::fromBytes(ds_file
->file
->size
);
656 if ((currentPacketOffset
+ currentPacketSize
).bytes() > ds_file
->file
->size
) {
657 BT_CLOGW("Invalid packet size reported in file: stream=\"%s\", "
658 "packet-offset-bytes=%llu, packet-size-bytes=%llu, "
659 "file-size-bytes=%jd",
660 ds_file
->file
->path
.c_str(), currentPacketOffset
.bytes(),
661 currentPacketSize
.bytes(), (intmax_t) ds_file
->file
->size
);
662 return nonstd::nullopt
;
665 ctf_fs_ds_index_entry index_entry
{currentPacketOffset
, currentPacketSize
};
667 /* Set path to stream file. */
668 index_entry
.path
= file_info
->path
.c_str();
670 int ret
= init_index_entry(index_entry
, ds_file
, &props
);
672 return nonstd::nullopt
;
675 index
.entries
.emplace_back(std::move(index_entry
));
677 currentPacketOffset
+= currentPacketSize
;
678 BT_CLOGD("Seeking to next packet: current-packet-offset-bytes=%llu, "
679 "next-packet-offset-bytes=%llu",
680 (currentPacketOffset
- currentPacketSize
).bytes(), currentPacketOffset
.bytes());
687 ctf_fs_ds_file::UP
ctf_fs_ds_file_create(struct ctf_fs_trace
*ctf_fs_trace
,
688 nonstd::optional
<bt2::Stream::Shared
> stream
,
689 const char *path
, const bt2_common::LogCfg
& logCfg
)
691 const size_t offset_align
= bt_mmap_get_offset_align_size(logCfg
.logLevel());
692 ctf_fs_ds_file::UP ds_file
= bt2_common::makeUnique
<ctf_fs_ds_file
>(logCfg
);
694 ds_file
->file
= bt2_common::makeUnique
<ctf_fs_file
>(logCfg
);
695 ds_file
->stream
= std::move(stream
);
696 ds_file
->metadata
= ctf_fs_trace
->metadata
.get();
697 ds_file
->file
->path
= path
;
698 int ret
= ctf_fs_file_open(ds_file
->file
.get(), "rb");
703 ds_file
->mmap_max_len
= offset_align
* 2048;
709 nonstd::optional
<ctf_fs_ds_index
> ctf_fs_ds_file_build_index(struct ctf_fs_ds_file
*ds_file
,
710 struct ctf_fs_ds_file_info
*file_info
,
711 struct ctf_msg_iter
*msg_iter
)
713 const bt2_common::LogCfg
& logCfg
= ds_file
->logCfg
;
715 nonstd::optional
<ctf_fs_ds_index
> index
=
716 build_index_from_idx_file(ds_file
, file_info
, msg_iter
);
721 BT_CLOGI("Failed to build index from .index file; "
722 "falling back to stream indexing.");
723 return build_index_from_stream_file(ds_file
, file_info
, msg_iter
);
726 ctf_fs_ds_file::~ctf_fs_ds_file()
728 (void) ds_file_munmap(this);
731 BT_HIDDEN
ctf_fs_ds_file_info::UP
ctf_fs_ds_file_info_create(const char *path
, int64_t begin_ns
)
733 ctf_fs_ds_file_info::UP ds_file_info
= bt2_common::makeUnique
<ctf_fs_ds_file_info
>();
735 ds_file_info
->path
= path
;
736 ds_file_info
->begin_ns
= begin_ns
;
741 BT_HIDDEN
ctf_fs_ds_file_group::UP
ctf_fs_ds_file_group_create(struct ctf_fs_trace
*ctf_fs_trace
,
742 struct ctf_stream_class
*sc
,
743 uint64_t stream_instance_id
,
744 ctf_fs_ds_index index
)
746 ctf_fs_ds_file_group::UP ds_file_group
{new ctf_fs_ds_file_group
};
748 ds_file_group
->index
= std::move(index
);
750 ds_file_group
->stream_id
= stream_instance_id
;
752 ds_file_group
->sc
= sc
;
753 ds_file_group
->ctf_fs_trace
= ctf_fs_trace
;
755 return ds_file_group
;