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
15 #include <babeltrace2/babeltrace.h>
17 #define BT_COMP_LOG_SELF_COMP (self_comp)
18 #define BT_LOG_OUTPUT_LEVEL (log_level)
19 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/DS"
22 #include "logging/comp-logging.h"
24 #include "common/assert.h"
25 #include "compat/endian.h" /* IWYU pragma: keep */
26 #include "compat/mman.h" /* IWYU pragma: keep */
28 #include "../common/src/msg-iter/msg-iter.hpp"
29 #include "data-stream-file.hpp"
32 #include "lttng-index.hpp"
34 static inline size_t remaining_mmap_bytes(struct ctf_fs_ds_file
*ds_file
)
36 BT_ASSERT_DBG(ds_file
->mmap_len
>= ds_file
->request_offset_in_mapping
);
37 return ds_file
->mmap_len
- ds_file
->request_offset_in_mapping
;
41 * Return true if `offset_in_file` is in the current mapping.
44 static bool offset_ist_mapped(struct ctf_fs_ds_file
*ds_file
, off_t offset_in_file
)
46 return offset_in_file
>= ds_file
->mmap_offset_in_file
&&
47 offset_in_file
< (ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
);
50 static enum ctf_msg_iter_medium_status
ds_file_munmap(struct ctf_fs_ds_file
*ds_file
)
52 enum ctf_msg_iter_medium_status status
;
53 bt_self_component
*self_comp
= ds_file
->self_comp
;
54 bt_logging_level log_level
= ds_file
->log_level
;
58 if (!ds_file
->mmap_addr
) {
59 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
63 if (bt_munmap(ds_file
->mmap_addr
, ds_file
->mmap_len
)) {
64 BT_COMP_LOGE_ERRNO("Cannot memory-unmap file",
65 ": address=%p, size=%zu, file_path=\"%s\", file=%p", ds_file
->mmap_addr
,
66 ds_file
->mmap_len
, ds_file
->file
? ds_file
->file
->path
->str
: "NULL",
67 ds_file
->file
? ds_file
->file
->fp
: NULL
);
68 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
72 ds_file
->mmap_addr
= NULL
;
74 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
80 * mmap a region of `ds_file` such that `requested_offset_in_file` is in the
81 * mapping. If the currently mmap-ed region already contains
82 * `requested_offset_in_file`, the mapping is kept.
84 * Set `ds_file->requested_offset_in_mapping` based on `request_offset_in_file`,
85 * such that the next call to `request_bytes` will return bytes starting at that
88 * `requested_offset_in_file` must be a valid offset in the file.
90 static enum ctf_msg_iter_medium_status
ds_file_mmap(struct ctf_fs_ds_file
*ds_file
,
91 off_t requested_offset_in_file
)
93 enum ctf_msg_iter_medium_status status
;
94 bt_self_component
*self_comp
= ds_file
->self_comp
;
95 bt_logging_level log_level
= ds_file
->log_level
;
97 /* Ensure the requested offset is in the file range. */
98 BT_ASSERT(requested_offset_in_file
>= 0);
99 BT_ASSERT(requested_offset_in_file
< ds_file
->file
->size
);
102 * If the mapping already contains the requested offset, just adjust
103 * requested_offset_in_mapping.
105 if (offset_ist_mapped(ds_file
, requested_offset_in_file
)) {
106 ds_file
->request_offset_in_mapping
=
107 requested_offset_in_file
- ds_file
->mmap_offset_in_file
;
108 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
112 /* Unmap old region */
113 status
= ds_file_munmap(ds_file
);
114 if (status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
119 * Compute a mapping that has the required alignment properties and
120 * contains `requested_offset_in_file`.
122 ds_file
->request_offset_in_mapping
=
123 requested_offset_in_file
% bt_mmap_get_offset_align_size(ds_file
->log_level
);
124 ds_file
->mmap_offset_in_file
= requested_offset_in_file
- ds_file
->request_offset_in_mapping
;
126 MIN(ds_file
->file
->size
- ds_file
->mmap_offset_in_file
, ds_file
->mmap_max_len
);
128 BT_ASSERT(ds_file
->mmap_len
> 0);
131 bt_mmap(ds_file
->mmap_len
, PROT_READ
, MAP_PRIVATE
, fileno(ds_file
->file
->fp
),
132 ds_file
->mmap_offset_in_file
, ds_file
->log_level
);
133 if (ds_file
->mmap_addr
== MAP_FAILED
) {
134 BT_COMP_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s",
135 ds_file
->mmap_len
, ds_file
->file
->path
->str
, ds_file
->file
->fp
,
136 (intmax_t) ds_file
->mmap_offset_in_file
, strerror(errno
));
137 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
141 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
148 * Change the mapping of the file to read the region that follows the current
151 * If the file hasn't been mapped yet, then everything (mmap_offset_in_file,
152 * mmap_len, request_offset_in_mapping) should have the value 0, which will
153 * result in the beginning of the file getting mapped.
155 * return _EOF if the current mapping is the end of the file.
158 static enum ctf_msg_iter_medium_status
ds_file_mmap_next(struct ctf_fs_ds_file
*ds_file
)
160 enum ctf_msg_iter_medium_status status
;
163 * If we're called, it's because more bytes are requested but we have
164 * given all the bytes of the current mapping.
166 BT_ASSERT(ds_file
->request_offset_in_mapping
== ds_file
->mmap_len
);
169 * If the current mapping coincides with the end of the file, there is
172 if (ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
== ds_file
->file
->size
) {
173 status
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
177 status
= ds_file_mmap(ds_file
, ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
);
183 static enum ctf_msg_iter_medium_status
medop_request_bytes(size_t request_sz
, uint8_t **buffer_addr
,
184 size_t *buffer_sz
, void *data
)
186 enum ctf_msg_iter_medium_status status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
187 struct ctf_fs_ds_file
*ds_file
= (struct ctf_fs_ds_file
*) data
;
188 bt_self_component
*self_comp
= ds_file
->self_comp
;
189 bt_logging_level log_level
= ds_file
->log_level
;
191 BT_ASSERT(request_sz
> 0);
194 * Check if we have at least one memory-mapped byte left. If we don't,
195 * mmap the next file.
197 if (remaining_mmap_bytes(ds_file
) == 0) {
198 /* Are we at the end of the file? */
199 if (ds_file
->mmap_offset_in_file
>= ds_file
->file
->size
) {
200 BT_COMP_LOGD("Reached end of file \"%s\" (%p)", ds_file
->file
->path
->str
,
202 status
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
206 status
= ds_file_mmap_next(ds_file
);
208 case CTF_MSG_ITER_MEDIUM_STATUS_OK
:
210 case CTF_MSG_ITER_MEDIUM_STATUS_EOF
:
213 BT_COMP_LOGE("Cannot memory-map next region of file \"%s\" (%p)",
214 ds_file
->file
->path
->str
, ds_file
->file
->fp
);
219 BT_ASSERT(remaining_mmap_bytes(ds_file
) > 0);
220 *buffer_sz
= MIN(remaining_mmap_bytes(ds_file
), request_sz
);
222 BT_ASSERT(ds_file
->mmap_addr
);
223 *buffer_addr
= ((uint8_t *) ds_file
->mmap_addr
) + ds_file
->request_offset_in_mapping
;
225 ds_file
->request_offset_in_mapping
+= *buffer_sz
;
229 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
235 static bt_stream
*medop_borrow_stream(bt_stream_class
*stream_class
, int64_t, void *data
)
237 struct ctf_fs_ds_file
*ds_file
= (struct ctf_fs_ds_file
*) data
;
238 bt_stream_class
*ds_file_stream_class
;
239 bt_stream
*stream
= NULL
;
241 ds_file_stream_class
= bt_stream_borrow_class(ds_file
->stream
);
243 if (stream_class
!= ds_file_stream_class
) {
245 * Not supported: two packets described by two different
246 * stream classes within the same data stream file.
251 stream
= ds_file
->stream
;
257 static enum ctf_msg_iter_medium_status
medop_seek(off_t offset
, void *data
)
259 struct ctf_fs_ds_file
*ds_file
= (struct ctf_fs_ds_file
*) data
;
261 BT_ASSERT(offset
>= 0);
262 BT_ASSERT(offset
< ds_file
->file
->size
);
264 return ds_file_mmap(ds_file
, offset
);
267 struct ctf_msg_iter_medium_ops ctf_fs_ds_file_medops
= {
274 struct ctf_fs_ds_group_medops_data
276 /* Weak, set once at creation time. */
277 struct ctf_fs_ds_file_group
*ds_file_group
= nullptr;
280 * Index (as in element rank) of the index entry of ds_file_groups'
281 * index we will read next (so, the one after the one we are reading
284 guint next_index_entry_index
= 0;
287 * File we are currently reading. Changes whenever we switch to
288 * reading another data file.
292 struct ctf_fs_ds_file
*file
= nullptr;
294 /* Weak, for context / logging / appending causes. */
295 bt_self_message_iterator
*self_msg_iter
= nullptr;
296 bt_logging_level log_level
= (bt_logging_level
) 0;
299 static enum ctf_msg_iter_medium_status
medop_group_request_bytes(size_t request_sz
,
300 uint8_t **buffer_addr
,
301 size_t *buffer_sz
, void *void_data
)
303 struct ctf_fs_ds_group_medops_data
*data
= (struct ctf_fs_ds_group_medops_data
*) void_data
;
305 /* Return bytes from the current file. */
306 return medop_request_bytes(request_sz
, buffer_addr
, buffer_sz
, data
->file
);
309 static bt_stream
*medop_group_borrow_stream(bt_stream_class
*stream_class
, int64_t stream_id
,
312 struct ctf_fs_ds_group_medops_data
*data
= (struct ctf_fs_ds_group_medops_data
*) void_data
;
314 return medop_borrow_stream(stream_class
, stream_id
, data
->file
);
318 * Set `data->file` to prepare it to read the packet described
322 static enum ctf_msg_iter_medium_status
323 ctf_fs_ds_group_medops_set_file(struct ctf_fs_ds_group_medops_data
*data
,
324 struct ctf_fs_ds_index_entry
*index_entry
,
325 bt_self_message_iterator
*self_msg_iter
, bt_logging_level log_level
)
327 enum ctf_msg_iter_medium_status status
;
330 BT_ASSERT(index_entry
);
332 /* Check if that file is already the one mapped. */
333 if (!data
->file
|| strcmp(index_entry
->path
, data
->file
->file
->path
->str
) != 0) {
334 /* Destroy the previously used file. */
335 ctf_fs_ds_file_destroy(data
->file
);
337 /* Create the new file. */
339 ctf_fs_ds_file_create(data
->ds_file_group
->ctf_fs_trace
, data
->ds_file_group
->stream
,
340 index_entry
->path
, log_level
);
342 BT_MSG_ITER_LOGE_APPEND_CAUSE(self_msg_iter
, "failed to create ctf_fs_ds_file.");
343 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
349 * Ensure the right portion of the file will be returned on the next
350 * request_bytes call.
352 status
= ds_file_mmap(data
->file
, index_entry
->offset
);
353 if (status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
357 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
363 static enum ctf_msg_iter_medium_status
medop_group_switch_packet(void *void_data
)
365 struct ctf_fs_ds_group_medops_data
*data
= (struct ctf_fs_ds_group_medops_data
*) void_data
;
366 struct ctf_fs_ds_index_entry
*index_entry
;
367 enum ctf_msg_iter_medium_status status
;
369 /* If we have gone through all index entries, we are done. */
370 if (data
->next_index_entry_index
>= data
->ds_file_group
->index
->entries
->len
) {
371 status
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
376 * Otherwise, look up the next index entry / packet and prepare it
379 index_entry
= (struct ctf_fs_ds_index_entry
*) g_ptr_array_index(
380 data
->ds_file_group
->index
->entries
, data
->next_index_entry_index
);
383 ctf_fs_ds_group_medops_set_file(data
, index_entry
, data
->self_msg_iter
, data
->log_level
);
384 if (status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
388 data
->next_index_entry_index
++;
390 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
395 void ctf_fs_ds_group_medops_data_destroy(struct ctf_fs_ds_group_medops_data
*data
)
401 ctf_fs_ds_file_destroy(data
->file
);
409 enum ctf_msg_iter_medium_status
ctf_fs_ds_group_medops_data_create(
410 struct ctf_fs_ds_file_group
*ds_file_group
, bt_self_message_iterator
*self_msg_iter
,
411 bt_logging_level log_level
, struct ctf_fs_ds_group_medops_data
**out
)
413 BT_ASSERT(self_msg_iter
);
414 BT_ASSERT(ds_file_group
);
415 BT_ASSERT(ds_file_group
->index
);
416 BT_ASSERT(ds_file_group
->index
->entries
->len
> 0);
418 ctf_fs_ds_group_medops_data
*data
= new ctf_fs_ds_group_medops_data
;
419 data
->ds_file_group
= ds_file_group
;
420 data
->self_msg_iter
= self_msg_iter
;
421 data
->log_level
= log_level
;
424 * No need to prepare the first file. ctf_msg_iter will call
425 * switch_packet before reading the first packet, it will be
430 return CTF_MSG_ITER_MEDIUM_STATUS_OK
;
433 void ctf_fs_ds_group_medops_data_reset(struct ctf_fs_ds_group_medops_data
*data
)
435 data
->next_index_entry_index
= 0;
438 struct ctf_msg_iter_medium_ops ctf_fs_ds_group_medops
= {
439 .request_bytes
= medop_group_request_bytes
,
442 * We don't support seeking using this medops. It would probably be
443 * possible, but it's not needed at the moment.
447 .switch_packet
= medop_group_switch_packet
,
448 .borrow_stream
= medop_group_borrow_stream
,
451 static void ctf_fs_ds_index_entry_destroy(ctf_fs_ds_index_entry
*entry
)
456 static struct ctf_fs_ds_index_entry
*ctf_fs_ds_index_entry_create()
458 ctf_fs_ds_index_entry
*entry
= new ctf_fs_ds_index_entry
;
459 entry
->packet_seq_num
= UINT64_MAX
;
464 static int convert_cycles_to_ns(struct ctf_clock_class
*clock_class
, uint64_t cycles
, int64_t *ns
)
466 return bt_util_clock_cycles_to_ns_from_origin(cycles
, clock_class
->frequency
,
467 clock_class
->offset_seconds
,
468 clock_class
->offset_cycles
, ns
);
471 static struct ctf_fs_ds_index
*build_index_from_idx_file(struct ctf_fs_ds_file
*ds_file
,
472 struct ctf_fs_ds_file_info
*file_info
,
473 struct ctf_msg_iter
*msg_iter
)
476 gchar
*directory
= NULL
;
477 gchar
*basename
= NULL
;
478 GString
*index_basename
= NULL
;
479 gchar
*index_file_path
= NULL
;
480 GMappedFile
*mapped_file
= NULL
;
482 const char *mmap_begin
= NULL
, *file_pos
= NULL
;
483 const struct ctf_packet_index_file_hdr
*header
= NULL
;
484 struct ctf_fs_ds_index
*index
= NULL
;
485 struct ctf_fs_ds_index_entry
*index_entry
= NULL
, *prev_index_entry
= NULL
;
486 uint64_t total_packets_size
= 0;
487 size_t file_index_entry_size
;
488 size_t file_entry_count
;
490 struct ctf_stream_class
*sc
;
491 struct ctf_msg_iter_packet_properties props
;
492 uint32_t version_major
, version_minor
;
493 bt_self_component
*self_comp
= ds_file
->self_comp
;
494 bt_logging_level log_level
= ds_file
->log_level
;
496 BT_COMP_LOGI("Building index from .idx file of stream file %s", ds_file
->file
->path
->str
);
497 ret
= ctf_msg_iter_get_packet_properties(msg_iter
, &props
);
499 BT_COMP_LOGI_STR("Cannot read first packet's header and context fields.");
503 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
, props
.stream_class_id
);
505 if (!sc
->default_clock_class
) {
506 BT_COMP_LOGI_STR("Cannot find stream class's default clock class.");
510 /* Look for index file in relative path index/name.idx. */
511 basename
= g_path_get_basename(ds_file
->file
->path
->str
);
513 BT_COMP_LOGE("Cannot get the basename of datastream file %s", ds_file
->file
->path
->str
);
517 directory
= g_path_get_dirname(ds_file
->file
->path
->str
);
519 BT_COMP_LOGE("Cannot get dirname of datastream file %s", ds_file
->file
->path
->str
);
523 index_basename
= g_string_new(basename
);
524 if (!index_basename
) {
525 BT_COMP_LOGE_STR("Cannot allocate index file basename string");
529 g_string_append(index_basename
, ".idx");
530 index_file_path
= g_build_filename(directory
, "index", index_basename
->str
, NULL
);
531 mapped_file
= g_mapped_file_new(index_file_path
, FALSE
, NULL
);
533 BT_COMP_LOGD("Cannot create new mapped file %s", index_file_path
);
538 * The g_mapped_file API limits us to 4GB files on 32-bit.
539 * Traces with such large indexes have never been seen in the wild,
540 * but this would need to be adjusted to support them.
542 filesize
= g_mapped_file_get_length(mapped_file
);
543 if (filesize
< sizeof(*header
)) {
544 BT_COMP_LOGW("Invalid LTTng trace index file: "
545 "file size (%zu bytes) < header size (%zu bytes)",
546 filesize
, sizeof(*header
));
550 mmap_begin
= g_mapped_file_get_contents(mapped_file
);
551 header
= (struct ctf_packet_index_file_hdr
*) mmap_begin
;
553 file_pos
= g_mapped_file_get_contents(mapped_file
) + sizeof(*header
);
554 if (be32toh(header
->magic
) != CTF_INDEX_MAGIC
) {
555 BT_COMP_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
559 version_major
= be32toh(header
->index_major
);
560 version_minor
= be32toh(header
->index_minor
);
561 if (version_major
!= 1) {
562 BT_COMP_LOGW("Unknown LTTng trace index version: "
563 "major=%" PRIu32
", minor=%" PRIu32
,
564 version_major
, version_minor
);
568 file_index_entry_size
= be32toh(header
->packet_index_len
);
569 if (file_index_entry_size
< CTF_INDEX_1_0_SIZE
) {
571 "Invalid `packet_index_len` in LTTng trace index file (`packet_index_len` < CTF index 1.0 index entry size): "
572 "packet_index_len=%zu, CTF_INDEX_1_0_SIZE=%zu",
573 file_index_entry_size
, CTF_INDEX_1_0_SIZE
);
577 file_entry_count
= (filesize
- sizeof(*header
)) / file_index_entry_size
;
578 if ((filesize
- sizeof(*header
)) % file_index_entry_size
) {
579 BT_COMP_LOGW("Invalid LTTng trace index: the index's size after the header "
580 "(%zu bytes) is not a multiple of the index entry size "
582 (filesize
- sizeof(*header
)), sizeof(*header
));
586 index
= ctf_fs_ds_index_create(ds_file
->log_level
, ds_file
->self_comp
);
591 for (i
= 0; i
< file_entry_count
; i
++) {
592 struct ctf_packet_index
*file_index
= (struct ctf_packet_index
*) file_pos
;
593 uint64_t packet_size
= be64toh(file_index
->packet_size
);
595 if (packet_size
% CHAR_BIT
) {
596 BT_COMP_LOGW("Invalid packet size encountered in LTTng trace index file");
600 index_entry
= ctf_fs_ds_index_entry_create();
602 BT_COMP_LOGE_APPEND_CAUSE(ds_file
->self_comp
,
603 "Failed to create a ctf_fs_ds_index_entry.");
607 /* Set path to stream file. */
608 index_entry
->path
= file_info
->path
->str
;
610 /* Convert size in bits to bytes. */
611 packet_size
/= CHAR_BIT
;
612 index_entry
->packet_size
= packet_size
;
614 index_entry
->offset
= be64toh(file_index
->offset
);
615 if (i
!= 0 && index_entry
->offset
< prev_index_entry
->offset
) {
617 "Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
618 "previous offset=%" PRIu64
", current offset=%" PRIu64
,
619 prev_index_entry
->offset
, index_entry
->offset
);
623 index_entry
->timestamp_begin
= be64toh(file_index
->timestamp_begin
);
624 index_entry
->timestamp_end
= be64toh(file_index
->timestamp_end
);
625 if (index_entry
->timestamp_end
< index_entry
->timestamp_begin
) {
627 "Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
628 "timestamp_begin=%" PRIu64
"timestamp_end=%" PRIu64
,
629 index_entry
->timestamp_begin
, index_entry
->timestamp_end
);
633 /* Convert the packet's bound to nanoseconds since Epoch. */
634 ret
= convert_cycles_to_ns(sc
->default_clock_class
, index_entry
->timestamp_begin
,
635 &index_entry
->timestamp_begin_ns
);
638 "Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
641 ret
= convert_cycles_to_ns(sc
->default_clock_class
, index_entry
->timestamp_end
,
642 &index_entry
->timestamp_end_ns
);
645 "Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
649 if (version_minor
>= 1) {
650 index_entry
->packet_seq_num
= be64toh(file_index
->packet_seq_num
);
653 total_packets_size
+= packet_size
;
654 file_pos
+= file_index_entry_size
;
656 prev_index_entry
= index_entry
;
658 /* Give ownership of `index_entry` to `index->entries`. */
659 g_ptr_array_add(index
->entries
, index_entry
);
663 /* Validate that the index addresses the complete stream. */
664 if (ds_file
->file
->size
!= total_packets_size
) {
665 BT_COMP_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
666 "file-size=%" PRIu64
", total-packets-size=%" PRIu64
,
667 ds_file
->file
->size
, total_packets_size
);
673 g_free(index_file_path
);
674 if (index_basename
) {
675 g_string_free(index_basename
, TRUE
);
678 g_mapped_file_unref(mapped_file
);
682 ctf_fs_ds_index_destroy(index
);
683 ctf_fs_ds_index_entry_destroy(index_entry
);
688 static int init_index_entry(struct ctf_fs_ds_index_entry
*entry
, struct ctf_fs_ds_file
*ds_file
,
689 struct ctf_msg_iter_packet_properties
*props
, off_t packet_size
,
693 struct ctf_stream_class
*sc
;
694 bt_self_component
*self_comp
= ds_file
->self_comp
;
695 bt_logging_level log_level
= ds_file
->log_level
;
697 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
, props
->stream_class_id
);
699 BT_ASSERT(packet_offset
>= 0);
700 entry
->offset
= packet_offset
;
701 BT_ASSERT(packet_size
>= 0);
702 entry
->packet_size
= packet_size
;
704 if (props
->snapshots
.beginning_clock
!= UINT64_C(-1)) {
705 entry
->timestamp_begin
= props
->snapshots
.beginning_clock
;
707 /* Convert the packet's bound to nanoseconds since Epoch. */
708 ret
= convert_cycles_to_ns(sc
->default_clock_class
, props
->snapshots
.beginning_clock
,
709 &entry
->timestamp_begin_ns
);
711 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
715 entry
->timestamp_begin
= UINT64_C(-1);
716 entry
->timestamp_begin_ns
= UINT64_C(-1);
719 if (props
->snapshots
.end_clock
!= UINT64_C(-1)) {
720 entry
->timestamp_end
= props
->snapshots
.end_clock
;
722 /* Convert the packet's bound to nanoseconds since Epoch. */
723 ret
= convert_cycles_to_ns(sc
->default_clock_class
, props
->snapshots
.end_clock
,
724 &entry
->timestamp_end_ns
);
726 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
730 entry
->timestamp_end
= UINT64_C(-1);
731 entry
->timestamp_end_ns
= UINT64_C(-1);
738 static struct ctf_fs_ds_index
*build_index_from_stream_file(struct ctf_fs_ds_file
*ds_file
,
739 struct ctf_fs_ds_file_info
*file_info
,
740 struct ctf_msg_iter
*msg_iter
)
743 struct ctf_fs_ds_index
*index
= NULL
;
744 enum ctf_msg_iter_status iter_status
= CTF_MSG_ITER_STATUS_OK
;
745 off_t current_packet_offset_bytes
= 0;
746 bt_self_component
*self_comp
= ds_file
->self_comp
;
747 bt_logging_level log_level
= ds_file
->log_level
;
749 BT_COMP_LOGI("Indexing stream file %s", ds_file
->file
->path
->str
);
751 index
= ctf_fs_ds_index_create(ds_file
->log_level
, ds_file
->self_comp
);
757 off_t current_packet_size_bytes
;
758 struct ctf_fs_ds_index_entry
*index_entry
;
759 struct ctf_msg_iter_packet_properties props
;
761 if (current_packet_offset_bytes
< 0) {
762 BT_COMP_LOGE_STR("Cannot get the current packet's offset.");
764 } else if (current_packet_offset_bytes
> ds_file
->file
->size
) {
765 BT_COMP_LOGE_STR("Unexpected current packet's offset (larger than file).");
767 } else if (current_packet_offset_bytes
== ds_file
->file
->size
) {
772 iter_status
= ctf_msg_iter_seek(msg_iter
, current_packet_offset_bytes
);
773 if (iter_status
!= CTF_MSG_ITER_STATUS_OK
) {
777 iter_status
= ctf_msg_iter_get_packet_properties(msg_iter
, &props
);
778 if (iter_status
!= CTF_MSG_ITER_STATUS_OK
) {
782 if (props
.exp_packet_total_size
>= 0) {
783 current_packet_size_bytes
= (uint64_t) props
.exp_packet_total_size
/ 8;
785 current_packet_size_bytes
= ds_file
->file
->size
;
788 if (current_packet_offset_bytes
+ current_packet_size_bytes
> ds_file
->file
->size
) {
789 BT_COMP_LOGW("Invalid packet size reported in file: stream=\"%s\", "
790 "packet-offset=%jd, packet-size-bytes=%jd, "
792 ds_file
->file
->path
->str
, (intmax_t) current_packet_offset_bytes
,
793 (intmax_t) current_packet_size_bytes
, (intmax_t) ds_file
->file
->size
);
797 index_entry
= ctf_fs_ds_index_entry_create();
799 BT_COMP_LOGE_APPEND_CAUSE(ds_file
->self_comp
,
800 "Failed to create a ctf_fs_ds_index_entry.");
804 /* Set path to stream file. */
805 index_entry
->path
= file_info
->path
->str
;
807 ret
= init_index_entry(index_entry
, ds_file
, &props
, current_packet_size_bytes
,
808 current_packet_offset_bytes
);
810 ctf_fs_ds_index_entry_destroy(index_entry
);
814 g_ptr_array_add(index
->entries
, index_entry
);
816 current_packet_offset_bytes
+= current_packet_size_bytes
;
817 BT_COMP_LOGD("Seeking to next packet: current-packet-offset=%jd, "
818 "next-packet-offset=%jd",
819 (intmax_t) (current_packet_offset_bytes
- current_packet_size_bytes
),
820 (intmax_t) current_packet_offset_bytes
);
827 ctf_fs_ds_index_destroy(index
);
832 struct ctf_fs_ds_file
*ctf_fs_ds_file_create(struct ctf_fs_trace
*ctf_fs_trace
, bt_stream
*stream
,
833 const char *path
, bt_logging_level log_level
)
836 const size_t offset_align
= bt_mmap_get_offset_align_size(log_level
);
837 ctf_fs_ds_file
*ds_file
= new ctf_fs_ds_file
;
843 ds_file
->log_level
= log_level
;
844 ds_file
->self_comp
= ctf_fs_trace
->self_comp
;
845 ds_file
->file
= ctf_fs_file_create(log_level
, ds_file
->self_comp
);
846 if (!ds_file
->file
) {
850 ds_file
->stream
= stream
;
851 bt_stream_get_ref(ds_file
->stream
);
852 ds_file
->metadata
= ctf_fs_trace
->metadata
;
853 g_string_assign(ds_file
->file
->path
, path
);
854 ret
= ctf_fs_file_open(ds_file
->file
, "rb");
859 ds_file
->mmap_max_len
= offset_align
* 2048;
864 /* Do not touch "borrowed" file. */
865 ctf_fs_ds_file_destroy(ds_file
);
872 struct ctf_fs_ds_index
*ctf_fs_ds_file_build_index(struct ctf_fs_ds_file
*ds_file
,
873 struct ctf_fs_ds_file_info
*file_info
,
874 struct ctf_msg_iter
*msg_iter
)
876 struct ctf_fs_ds_index
*index
;
877 bt_self_component
*self_comp
= ds_file
->self_comp
;
878 bt_logging_level log_level
= ds_file
->log_level
;
880 index
= build_index_from_idx_file(ds_file
, file_info
, msg_iter
);
885 BT_COMP_LOGI("Failed to build index from .index file; "
886 "falling back to stream indexing.");
887 index
= build_index_from_stream_file(ds_file
, file_info
, msg_iter
);
892 struct ctf_fs_ds_index
*ctf_fs_ds_index_create(bt_logging_level log_level
,
893 bt_self_component
*self_comp
)
895 ctf_fs_ds_index
*index
= new ctf_fs_ds_index
;
896 index
->entries
= g_ptr_array_new_with_free_func((GDestroyNotify
) ctf_fs_ds_index_entry_destroy
);
897 if (!index
->entries
) {
898 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
899 "Failed to allocate index entries.");
906 ctf_fs_ds_index_destroy(index
);
912 void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file
*ds_file
)
918 bt_stream_put_ref(ds_file
->stream
);
919 (void) ds_file_munmap(ds_file
);
922 ctf_fs_file_destroy(ds_file
->file
);
928 void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index
*index
)
934 if (index
->entries
) {
935 g_ptr_array_free(index
->entries
, TRUE
);