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
;
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
;
287 * File we are currently reading. Changes whenever we switch to
288 * reading another data file.
292 struct ctf_fs_ds_file
*file
;
294 /* Weak, for context / logging / appending causes. */
295 bt_self_message_iterator
*self_msg_iter
;
296 bt_logging_level log_level
;
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 struct ctf_fs_ds_group_medops_data
*data
;
414 enum ctf_msg_iter_medium_status status
;
416 BT_ASSERT(self_msg_iter
);
417 BT_ASSERT(ds_file_group
);
418 BT_ASSERT(ds_file_group
->index
);
419 BT_ASSERT(ds_file_group
->index
->entries
->len
> 0);
421 data
= g_new0(struct ctf_fs_ds_group_medops_data
, 1);
423 BT_MSG_ITER_LOGE_APPEND_CAUSE(self_msg_iter
,
424 "Failed to allocate a struct ctf_fs_ds_group_medops_data");
425 status
= CTF_MSG_ITER_MEDIUM_STATUS_MEMORY_ERROR
;
429 data
->ds_file_group
= ds_file_group
;
430 data
->self_msg_iter
= self_msg_iter
;
431 data
->log_level
= log_level
;
434 * No need to prepare the first file. ctf_msg_iter will call
435 * switch_packet before reading the first packet, it will be
440 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
444 ctf_fs_ds_group_medops_data_destroy(data
);
450 void ctf_fs_ds_group_medops_data_reset(struct ctf_fs_ds_group_medops_data
*data
)
452 data
->next_index_entry_index
= 0;
455 struct ctf_msg_iter_medium_ops ctf_fs_ds_group_medops
= {
456 .request_bytes
= medop_group_request_bytes
,
459 * We don't support seeking using this medops. It would probably be
460 * possible, but it's not needed at the moment.
464 .switch_packet
= medop_group_switch_packet
,
465 .borrow_stream
= medop_group_borrow_stream
,
468 static struct ctf_fs_ds_index_entry
*ctf_fs_ds_index_entry_create(bt_self_component
*self_comp
,
469 bt_logging_level log_level
)
471 struct ctf_fs_ds_index_entry
*entry
;
473 entry
= g_new0(struct ctf_fs_ds_index_entry
, 1);
475 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to allocate a ctf_fs_ds_index_entry.");
479 entry
->packet_seq_num
= UINT64_MAX
;
485 static int convert_cycles_to_ns(struct ctf_clock_class
*clock_class
, uint64_t cycles
, int64_t *ns
)
487 return bt_util_clock_cycles_to_ns_from_origin(cycles
, clock_class
->frequency
,
488 clock_class
->offset_seconds
,
489 clock_class
->offset_cycles
, ns
);
492 static struct ctf_fs_ds_index
*build_index_from_idx_file(struct ctf_fs_ds_file
*ds_file
,
493 struct ctf_fs_ds_file_info
*file_info
,
494 struct ctf_msg_iter
*msg_iter
)
497 gchar
*directory
= NULL
;
498 gchar
*basename
= NULL
;
499 GString
*index_basename
= NULL
;
500 gchar
*index_file_path
= NULL
;
501 GMappedFile
*mapped_file
= NULL
;
503 const char *mmap_begin
= NULL
, *file_pos
= NULL
;
504 const struct ctf_packet_index_file_hdr
*header
= NULL
;
505 struct ctf_fs_ds_index
*index
= NULL
;
506 struct ctf_fs_ds_index_entry
*index_entry
= NULL
, *prev_index_entry
= NULL
;
507 uint64_t total_packets_size
= 0;
508 size_t file_index_entry_size
;
509 size_t file_entry_count
;
511 struct ctf_stream_class
*sc
;
512 struct ctf_msg_iter_packet_properties props
;
513 uint32_t version_major
, version_minor
;
514 bt_self_component
*self_comp
= ds_file
->self_comp
;
515 bt_logging_level log_level
= ds_file
->log_level
;
517 BT_COMP_LOGI("Building index from .idx file of stream file %s", ds_file
->file
->path
->str
);
518 ret
= ctf_msg_iter_get_packet_properties(msg_iter
, &props
);
520 BT_COMP_LOGI_STR("Cannot read first packet's header and context fields.");
524 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
, props
.stream_class_id
);
526 if (!sc
->default_clock_class
) {
527 BT_COMP_LOGI_STR("Cannot find stream class's default clock class.");
531 /* Look for index file in relative path index/name.idx. */
532 basename
= g_path_get_basename(ds_file
->file
->path
->str
);
534 BT_COMP_LOGE("Cannot get the basename of datastream file %s", ds_file
->file
->path
->str
);
538 directory
= g_path_get_dirname(ds_file
->file
->path
->str
);
540 BT_COMP_LOGE("Cannot get dirname of datastream file %s", ds_file
->file
->path
->str
);
544 index_basename
= g_string_new(basename
);
545 if (!index_basename
) {
546 BT_COMP_LOGE_STR("Cannot allocate index file basename string");
550 g_string_append(index_basename
, ".idx");
551 index_file_path
= g_build_filename(directory
, "index", index_basename
->str
, NULL
);
552 mapped_file
= g_mapped_file_new(index_file_path
, FALSE
, NULL
);
554 BT_COMP_LOGD("Cannot create new mapped file %s", index_file_path
);
559 * The g_mapped_file API limits us to 4GB files on 32-bit.
560 * Traces with such large indexes have never been seen in the wild,
561 * but this would need to be adjusted to support them.
563 filesize
= g_mapped_file_get_length(mapped_file
);
564 if (filesize
< sizeof(*header
)) {
565 BT_COMP_LOGW("Invalid LTTng trace index file: "
566 "file size (%zu bytes) < header size (%zu bytes)",
567 filesize
, sizeof(*header
));
571 mmap_begin
= g_mapped_file_get_contents(mapped_file
);
572 header
= (struct ctf_packet_index_file_hdr
*) mmap_begin
;
574 file_pos
= g_mapped_file_get_contents(mapped_file
) + sizeof(*header
);
575 if (be32toh(header
->magic
) != CTF_INDEX_MAGIC
) {
576 BT_COMP_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
580 version_major
= be32toh(header
->index_major
);
581 version_minor
= be32toh(header
->index_minor
);
582 if (version_major
!= 1) {
583 BT_COMP_LOGW("Unknown LTTng trace index version: "
584 "major=%" PRIu32
", minor=%" PRIu32
,
585 version_major
, version_minor
);
589 file_index_entry_size
= be32toh(header
->packet_index_len
);
590 if (file_index_entry_size
< CTF_INDEX_1_0_SIZE
) {
592 "Invalid `packet_index_len` in LTTng trace index file (`packet_index_len` < CTF index 1.0 index entry size): "
593 "packet_index_len=%zu, CTF_INDEX_1_0_SIZE=%zu",
594 file_index_entry_size
, CTF_INDEX_1_0_SIZE
);
598 file_entry_count
= (filesize
- sizeof(*header
)) / file_index_entry_size
;
599 if ((filesize
- sizeof(*header
)) % file_index_entry_size
) {
600 BT_COMP_LOGW("Invalid LTTng trace index: the index's size after the header "
601 "(%zu bytes) is not a multiple of the index entry size "
603 (filesize
- sizeof(*header
)), sizeof(*header
));
607 index
= ctf_fs_ds_index_create(ds_file
->log_level
, ds_file
->self_comp
);
612 for (i
= 0; i
< file_entry_count
; i
++) {
613 struct ctf_packet_index
*file_index
= (struct ctf_packet_index
*) file_pos
;
614 uint64_t packet_size
= be64toh(file_index
->packet_size
);
616 if (packet_size
% CHAR_BIT
) {
617 BT_COMP_LOGW("Invalid packet size encountered in LTTng trace index file");
621 index_entry
= ctf_fs_ds_index_entry_create(ds_file
->self_comp
, ds_file
->log_level
);
623 BT_COMP_LOGE_APPEND_CAUSE(ds_file
->self_comp
,
624 "Failed to create a ctf_fs_ds_index_entry.");
628 /* Set path to stream file. */
629 index_entry
->path
= file_info
->path
->str
;
631 /* Convert size in bits to bytes. */
632 packet_size
/= CHAR_BIT
;
633 index_entry
->packet_size
= packet_size
;
635 index_entry
->offset
= be64toh(file_index
->offset
);
636 if (i
!= 0 && index_entry
->offset
< prev_index_entry
->offset
) {
638 "Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
639 "previous offset=%" PRIu64
", current offset=%" PRIu64
,
640 prev_index_entry
->offset
, index_entry
->offset
);
644 index_entry
->timestamp_begin
= be64toh(file_index
->timestamp_begin
);
645 index_entry
->timestamp_end
= be64toh(file_index
->timestamp_end
);
646 if (index_entry
->timestamp_end
< index_entry
->timestamp_begin
) {
648 "Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
649 "timestamp_begin=%" PRIu64
"timestamp_end=%" PRIu64
,
650 index_entry
->timestamp_begin
, index_entry
->timestamp_end
);
654 /* Convert the packet's bound to nanoseconds since Epoch. */
655 ret
= convert_cycles_to_ns(sc
->default_clock_class
, index_entry
->timestamp_begin
,
656 &index_entry
->timestamp_begin_ns
);
659 "Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
662 ret
= convert_cycles_to_ns(sc
->default_clock_class
, index_entry
->timestamp_end
,
663 &index_entry
->timestamp_end_ns
);
666 "Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
670 if (version_minor
>= 1) {
671 index_entry
->packet_seq_num
= be64toh(file_index
->packet_seq_num
);
674 total_packets_size
+= packet_size
;
675 file_pos
+= file_index_entry_size
;
677 prev_index_entry
= index_entry
;
679 /* Give ownership of `index_entry` to `index->entries`. */
680 g_ptr_array_add(index
->entries
, index_entry
);
684 /* Validate that the index addresses the complete stream. */
685 if (ds_file
->file
->size
!= total_packets_size
) {
686 BT_COMP_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
687 "file-size=%" PRIu64
", total-packets-size=%" PRIu64
,
688 ds_file
->file
->size
, total_packets_size
);
694 g_free(index_file_path
);
695 if (index_basename
) {
696 g_string_free(index_basename
, TRUE
);
699 g_mapped_file_unref(mapped_file
);
703 ctf_fs_ds_index_destroy(index
);
709 static int init_index_entry(struct ctf_fs_ds_index_entry
*entry
, struct ctf_fs_ds_file
*ds_file
,
710 struct ctf_msg_iter_packet_properties
*props
, off_t packet_size
,
714 struct ctf_stream_class
*sc
;
715 bt_self_component
*self_comp
= ds_file
->self_comp
;
716 bt_logging_level log_level
= ds_file
->log_level
;
718 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
, props
->stream_class_id
);
720 BT_ASSERT(packet_offset
>= 0);
721 entry
->offset
= packet_offset
;
722 BT_ASSERT(packet_size
>= 0);
723 entry
->packet_size
= packet_size
;
725 if (props
->snapshots
.beginning_clock
!= UINT64_C(-1)) {
726 entry
->timestamp_begin
= props
->snapshots
.beginning_clock
;
728 /* Convert the packet's bound to nanoseconds since Epoch. */
729 ret
= convert_cycles_to_ns(sc
->default_clock_class
, props
->snapshots
.beginning_clock
,
730 &entry
->timestamp_begin_ns
);
732 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
736 entry
->timestamp_begin
= UINT64_C(-1);
737 entry
->timestamp_begin_ns
= UINT64_C(-1);
740 if (props
->snapshots
.end_clock
!= UINT64_C(-1)) {
741 entry
->timestamp_end
= props
->snapshots
.end_clock
;
743 /* Convert the packet's bound to nanoseconds since Epoch. */
744 ret
= convert_cycles_to_ns(sc
->default_clock_class
, props
->snapshots
.end_clock
,
745 &entry
->timestamp_end_ns
);
747 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
751 entry
->timestamp_end
= UINT64_C(-1);
752 entry
->timestamp_end_ns
= UINT64_C(-1);
759 static struct ctf_fs_ds_index
*build_index_from_stream_file(struct ctf_fs_ds_file
*ds_file
,
760 struct ctf_fs_ds_file_info
*file_info
,
761 struct ctf_msg_iter
*msg_iter
)
764 struct ctf_fs_ds_index
*index
= NULL
;
765 enum ctf_msg_iter_status iter_status
= CTF_MSG_ITER_STATUS_OK
;
766 off_t current_packet_offset_bytes
= 0;
767 bt_self_component
*self_comp
= ds_file
->self_comp
;
768 bt_logging_level log_level
= ds_file
->log_level
;
770 BT_COMP_LOGI("Indexing stream file %s", ds_file
->file
->path
->str
);
772 index
= ctf_fs_ds_index_create(ds_file
->log_level
, ds_file
->self_comp
);
778 off_t current_packet_size_bytes
;
779 struct ctf_fs_ds_index_entry
*index_entry
;
780 struct ctf_msg_iter_packet_properties props
;
782 if (current_packet_offset_bytes
< 0) {
783 BT_COMP_LOGE_STR("Cannot get the current packet's offset.");
785 } else if (current_packet_offset_bytes
> ds_file
->file
->size
) {
786 BT_COMP_LOGE_STR("Unexpected current packet's offset (larger than file).");
788 } else if (current_packet_offset_bytes
== ds_file
->file
->size
) {
793 iter_status
= ctf_msg_iter_seek(msg_iter
, current_packet_offset_bytes
);
794 if (iter_status
!= CTF_MSG_ITER_STATUS_OK
) {
798 iter_status
= ctf_msg_iter_get_packet_properties(msg_iter
, &props
);
799 if (iter_status
!= CTF_MSG_ITER_STATUS_OK
) {
803 if (props
.exp_packet_total_size
>= 0) {
804 current_packet_size_bytes
= (uint64_t) props
.exp_packet_total_size
/ 8;
806 current_packet_size_bytes
= ds_file
->file
->size
;
809 if (current_packet_offset_bytes
+ current_packet_size_bytes
> ds_file
->file
->size
) {
810 BT_COMP_LOGW("Invalid packet size reported in file: stream=\"%s\", "
811 "packet-offset=%jd, packet-size-bytes=%jd, "
813 ds_file
->file
->path
->str
, (intmax_t) current_packet_offset_bytes
,
814 (intmax_t) current_packet_size_bytes
, (intmax_t) ds_file
->file
->size
);
818 index_entry
= ctf_fs_ds_index_entry_create(ds_file
->self_comp
, ds_file
->log_level
);
820 BT_COMP_LOGE_APPEND_CAUSE(ds_file
->self_comp
,
821 "Failed to create a ctf_fs_ds_index_entry.");
825 /* Set path to stream file. */
826 index_entry
->path
= file_info
->path
->str
;
828 ret
= init_index_entry(index_entry
, ds_file
, &props
, current_packet_size_bytes
,
829 current_packet_offset_bytes
);
835 g_ptr_array_add(index
->entries
, index_entry
);
837 current_packet_offset_bytes
+= current_packet_size_bytes
;
838 BT_COMP_LOGD("Seeking to next packet: current-packet-offset=%jd, "
839 "next-packet-offset=%jd",
840 (intmax_t) (current_packet_offset_bytes
- current_packet_size_bytes
),
841 (intmax_t) current_packet_offset_bytes
);
848 ctf_fs_ds_index_destroy(index
);
853 struct ctf_fs_ds_file
*ctf_fs_ds_file_create(struct ctf_fs_trace
*ctf_fs_trace
, bt_stream
*stream
,
854 const char *path
, bt_logging_level log_level
)
857 const size_t offset_align
= bt_mmap_get_offset_align_size(log_level
);
858 struct ctf_fs_ds_file
*ds_file
= g_new0(struct ctf_fs_ds_file
, 1);
864 ds_file
->log_level
= log_level
;
865 ds_file
->self_comp
= ctf_fs_trace
->self_comp
;
866 ds_file
->file
= ctf_fs_file_create(log_level
, ds_file
->self_comp
);
867 if (!ds_file
->file
) {
871 ds_file
->stream
= stream
;
872 bt_stream_get_ref(ds_file
->stream
);
873 ds_file
->metadata
= ctf_fs_trace
->metadata
;
874 g_string_assign(ds_file
->file
->path
, path
);
875 ret
= ctf_fs_file_open(ds_file
->file
, "rb");
880 ds_file
->mmap_max_len
= offset_align
* 2048;
885 /* Do not touch "borrowed" file. */
886 ctf_fs_ds_file_destroy(ds_file
);
893 struct ctf_fs_ds_index
*ctf_fs_ds_file_build_index(struct ctf_fs_ds_file
*ds_file
,
894 struct ctf_fs_ds_file_info
*file_info
,
895 struct ctf_msg_iter
*msg_iter
)
897 struct ctf_fs_ds_index
*index
;
898 bt_self_component
*self_comp
= ds_file
->self_comp
;
899 bt_logging_level log_level
= ds_file
->log_level
;
901 index
= build_index_from_idx_file(ds_file
, file_info
, msg_iter
);
906 BT_COMP_LOGI("Failed to build index from .index file; "
907 "falling back to stream indexing.");
908 index
= build_index_from_stream_file(ds_file
, file_info
, msg_iter
);
913 struct ctf_fs_ds_index
*ctf_fs_ds_index_create(bt_logging_level log_level
,
914 bt_self_component
*self_comp
)
916 struct ctf_fs_ds_index
*index
= g_new0(struct ctf_fs_ds_index
, 1);
919 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
, "Failed to allocate index");
923 index
->entries
= g_ptr_array_new_with_free_func((GDestroyNotify
) g_free
);
924 if (!index
->entries
) {
925 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
926 "Failed to allocate index entries.");
933 ctf_fs_ds_index_destroy(index
);
939 void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file
*ds_file
)
945 bt_stream_put_ref(ds_file
->stream
);
946 (void) ds_file_munmap(ds_file
);
949 ctf_fs_file_destroy(ds_file
->file
);
955 void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index
*index
)
961 if (index
->entries
) {
962 g_ptr_array_free(index
->entries
, TRUE
);