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_COMP_LOG_SELF_COMP (self_comp)
10 #define BT_LOG_OUTPUT_LEVEL (log_level)
11 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/DS"
12 #include "logging/comp-logging.h"
20 #include "compat/mman.h"
21 #include "compat/endian.h"
22 #include <babeltrace2/babeltrace.h>
23 #include "common/common.h"
25 #include "metadata.hpp"
26 #include "../common/msg-iter/msg-iter.hpp"
27 #include "common/assert.h"
28 #include "data-stream-file.hpp"
31 static inline size_t remaining_mmap_bytes(struct ctf_fs_ds_file
*ds_file
)
33 BT_ASSERT_DBG(ds_file
->mmap_len
>= ds_file
->request_offset_in_mapping
);
34 return ds_file
->mmap_len
- ds_file
->request_offset_in_mapping
;
38 * Return true if `offset_in_file` is in the current mapping.
41 static bool offset_ist_mapped(struct ctf_fs_ds_file
*ds_file
, off_t offset_in_file
)
43 return offset_in_file
>= ds_file
->mmap_offset_in_file
&&
44 offset_in_file
< (ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
);
47 static enum ctf_msg_iter_medium_status
ds_file_munmap(struct ctf_fs_ds_file
*ds_file
)
49 enum ctf_msg_iter_medium_status status
;
50 bt_self_component
*self_comp
= ds_file
->self_comp
;
51 bt_logging_level log_level
= ds_file
->log_level
;
55 if (!ds_file
->mmap_addr
) {
56 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
60 if (bt_munmap(ds_file
->mmap_addr
, ds_file
->mmap_len
)) {
61 BT_COMP_LOGE_ERRNO("Cannot memory-unmap file",
62 ": address=%p, size=%zu, file_path=\"%s\", file=%p", ds_file
->mmap_addr
,
63 ds_file
->mmap_len
, ds_file
->file
? ds_file
->file
->path
->str
: "NULL",
64 ds_file
->file
? ds_file
->file
->fp
: NULL
);
65 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
69 ds_file
->mmap_addr
= NULL
;
71 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
77 * mmap a region of `ds_file` such that `requested_offset_in_file` is in the
78 * mapping. If the currently mmap-ed region already contains
79 * `requested_offset_in_file`, the mapping is kept.
81 * Set `ds_file->requested_offset_in_mapping` based on `request_offset_in_file`,
82 * such that the next call to `request_bytes` will return bytes starting at that
85 * `requested_offset_in_file` must be a valid offset in the file.
87 static enum ctf_msg_iter_medium_status
ds_file_mmap(struct ctf_fs_ds_file
*ds_file
,
88 off_t requested_offset_in_file
)
90 enum ctf_msg_iter_medium_status status
;
91 bt_self_component
*self_comp
= ds_file
->self_comp
;
92 bt_logging_level log_level
= ds_file
->log_level
;
94 /* Ensure the requested offset is in the file range. */
95 BT_ASSERT(requested_offset_in_file
>= 0);
96 BT_ASSERT(requested_offset_in_file
< ds_file
->file
->size
);
99 * If the mapping already contains the requested offset, just adjust
100 * requested_offset_in_mapping.
102 if (offset_ist_mapped(ds_file
, requested_offset_in_file
)) {
103 ds_file
->request_offset_in_mapping
=
104 requested_offset_in_file
- ds_file
->mmap_offset_in_file
;
105 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
109 /* Unmap old region */
110 status
= ds_file_munmap(ds_file
);
111 if (status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
116 * Compute a mapping that has the required alignment properties and
117 * contains `requested_offset_in_file`.
119 ds_file
->request_offset_in_mapping
=
120 requested_offset_in_file
% bt_mmap_get_offset_align_size(ds_file
->log_level
);
121 ds_file
->mmap_offset_in_file
= requested_offset_in_file
- ds_file
->request_offset_in_mapping
;
123 MIN(ds_file
->file
->size
- ds_file
->mmap_offset_in_file
, ds_file
->mmap_max_len
);
125 BT_ASSERT(ds_file
->mmap_len
> 0);
128 bt_mmap((void *) 0, ds_file
->mmap_len
, PROT_READ
, MAP_PRIVATE
, fileno(ds_file
->file
->fp
),
129 ds_file
->mmap_offset_in_file
, ds_file
->log_level
);
130 if (ds_file
->mmap_addr
== MAP_FAILED
) {
131 BT_COMP_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s",
132 ds_file
->mmap_len
, ds_file
->file
->path
->str
, ds_file
->file
->fp
,
133 (intmax_t) ds_file
->mmap_offset_in_file
, strerror(errno
));
134 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
138 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
145 * Change the mapping of the file to read the region that follows the current
148 * If the file hasn't been mapped yet, then everything (mmap_offset_in_file,
149 * mmap_len, request_offset_in_mapping) should have the value 0, which will
150 * result in the beginning of the file getting mapped.
152 * return _EOF if the current mapping is the end of the file.
155 static enum ctf_msg_iter_medium_status
ds_file_mmap_next(struct ctf_fs_ds_file
*ds_file
)
157 enum ctf_msg_iter_medium_status status
;
160 * If we're called, it's because more bytes are requested but we have
161 * given all the bytes of the current mapping.
163 BT_ASSERT(ds_file
->request_offset_in_mapping
== ds_file
->mmap_len
);
166 * If the current mapping coincides with the end of the file, there is
169 if (ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
== ds_file
->file
->size
) {
170 status
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
174 status
= ds_file_mmap(ds_file
, ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
);
180 static enum ctf_msg_iter_medium_status
medop_request_bytes(size_t request_sz
, uint8_t **buffer_addr
,
181 size_t *buffer_sz
, void *data
)
183 enum ctf_msg_iter_medium_status status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
184 struct ctf_fs_ds_file
*ds_file
= (struct ctf_fs_ds_file
*) data
;
185 bt_self_component
*self_comp
= ds_file
->self_comp
;
186 bt_logging_level log_level
= ds_file
->log_level
;
188 BT_ASSERT(request_sz
> 0);
191 * Check if we have at least one memory-mapped byte left. If we don't,
192 * mmap the next file.
194 if (remaining_mmap_bytes(ds_file
) == 0) {
195 /* Are we at the end of the file? */
196 if (ds_file
->mmap_offset_in_file
>= ds_file
->file
->size
) {
197 BT_COMP_LOGD("Reached end of file \"%s\" (%p)", ds_file
->file
->path
->str
,
199 status
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
203 status
= ds_file_mmap_next(ds_file
);
205 case CTF_MSG_ITER_MEDIUM_STATUS_OK
:
207 case CTF_MSG_ITER_MEDIUM_STATUS_EOF
:
210 BT_COMP_LOGE("Cannot memory-map next region of file \"%s\" (%p)",
211 ds_file
->file
->path
->str
, ds_file
->file
->fp
);
216 BT_ASSERT(remaining_mmap_bytes(ds_file
) > 0);
217 *buffer_sz
= MIN(remaining_mmap_bytes(ds_file
), request_sz
);
219 BT_ASSERT(ds_file
->mmap_addr
);
220 *buffer_addr
= ((uint8_t *) ds_file
->mmap_addr
) + ds_file
->request_offset_in_mapping
;
222 ds_file
->request_offset_in_mapping
+= *buffer_sz
;
226 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
232 static bt_stream
*medop_borrow_stream(bt_stream_class
*stream_class
, int64_t, void *data
)
234 struct ctf_fs_ds_file
*ds_file
= (struct ctf_fs_ds_file
*) data
;
235 bt_stream_class
*ds_file_stream_class
;
236 bt_stream
*stream
= NULL
;
238 ds_file_stream_class
= bt_stream_borrow_class(ds_file
->stream
);
240 if (stream_class
!= ds_file_stream_class
) {
242 * Not supported: two packets described by two different
243 * stream classes within the same data stream file.
248 stream
= ds_file
->stream
;
254 static enum ctf_msg_iter_medium_status
medop_seek(off_t offset
, void *data
)
256 struct ctf_fs_ds_file
*ds_file
= (struct ctf_fs_ds_file
*) data
;
258 BT_ASSERT(offset
>= 0);
259 BT_ASSERT(offset
< ds_file
->file
->size
);
261 return ds_file_mmap(ds_file
, offset
);
264 struct ctf_msg_iter_medium_ops ctf_fs_ds_file_medops
= {
271 struct ctf_fs_ds_group_medops_data
273 /* Weak, set once at creation time. */
274 struct ctf_fs_ds_file_group
*ds_file_group
;
277 * Index (as in element rank) of the index entry of ds_file_groups'
278 * index we will read next (so, the one after the one we are reading
281 guint next_index_entry_index
;
284 * File we are currently reading. Changes whenever we switch to
285 * reading another data file.
289 struct ctf_fs_ds_file
*file
;
291 /* Weak, for context / logging / appending causes. */
292 bt_self_message_iterator
*self_msg_iter
;
293 bt_logging_level log_level
;
296 static enum ctf_msg_iter_medium_status
medop_group_request_bytes(size_t request_sz
,
297 uint8_t **buffer_addr
,
298 size_t *buffer_sz
, void *void_data
)
300 struct ctf_fs_ds_group_medops_data
*data
= (struct ctf_fs_ds_group_medops_data
*) void_data
;
302 /* Return bytes from the current file. */
303 return medop_request_bytes(request_sz
, buffer_addr
, buffer_sz
, data
->file
);
306 static bt_stream
*medop_group_borrow_stream(bt_stream_class
*stream_class
, int64_t stream_id
,
309 struct ctf_fs_ds_group_medops_data
*data
= (struct ctf_fs_ds_group_medops_data
*) void_data
;
311 return medop_borrow_stream(stream_class
, stream_id
, data
->file
);
315 * Set `data->file` to prepare it to read the packet described
319 static enum ctf_msg_iter_medium_status
320 ctf_fs_ds_group_medops_set_file(struct ctf_fs_ds_group_medops_data
*data
,
321 struct ctf_fs_ds_index_entry
*index_entry
,
322 bt_self_message_iterator
*self_msg_iter
, bt_logging_level log_level
)
324 enum ctf_msg_iter_medium_status status
;
327 BT_ASSERT(index_entry
);
329 /* Check if that file is already the one mapped. */
330 if (!data
->file
|| strcmp(index_entry
->path
, data
->file
->file
->path
->str
) != 0) {
331 /* Destroy the previously used file. */
332 ctf_fs_ds_file_destroy(data
->file
);
334 /* Create the new file. */
336 ctf_fs_ds_file_create(data
->ds_file_group
->ctf_fs_trace
, data
->ds_file_group
->stream
,
337 index_entry
->path
, log_level
);
339 BT_MSG_ITER_LOGE_APPEND_CAUSE(self_msg_iter
, "failed to create ctf_fs_ds_file.");
340 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
346 * Ensure the right portion of the file will be returned on the next
347 * request_bytes call.
349 status
= ds_file_mmap(data
->file
, index_entry
->offset
);
350 if (status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
354 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
360 static enum ctf_msg_iter_medium_status
medop_group_switch_packet(void *void_data
)
362 struct ctf_fs_ds_group_medops_data
*data
= (struct ctf_fs_ds_group_medops_data
*) void_data
;
363 struct ctf_fs_ds_index_entry
*index_entry
;
364 enum ctf_msg_iter_medium_status status
;
366 /* If we have gone through all index entries, we are done. */
367 if (data
->next_index_entry_index
>= data
->ds_file_group
->index
->entries
->len
) {
368 status
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
373 * Otherwise, look up the next index entry / packet and prepare it
376 index_entry
= (struct ctf_fs_ds_index_entry
*) g_ptr_array_index(
377 data
->ds_file_group
->index
->entries
, data
->next_index_entry_index
);
380 ctf_fs_ds_group_medops_set_file(data
, index_entry
, data
->self_msg_iter
, data
->log_level
);
381 if (status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
385 data
->next_index_entry_index
++;
387 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
392 void ctf_fs_ds_group_medops_data_destroy(struct ctf_fs_ds_group_medops_data
*data
)
398 ctf_fs_ds_file_destroy(data
->file
);
406 enum ctf_msg_iter_medium_status
ctf_fs_ds_group_medops_data_create(
407 struct ctf_fs_ds_file_group
*ds_file_group
, bt_self_message_iterator
*self_msg_iter
,
408 bt_logging_level log_level
, struct ctf_fs_ds_group_medops_data
**out
)
410 struct ctf_fs_ds_group_medops_data
*data
;
411 enum ctf_msg_iter_medium_status status
;
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 data
= g_new0(struct ctf_fs_ds_group_medops_data
, 1);
420 BT_MSG_ITER_LOGE_APPEND_CAUSE(self_msg_iter
,
421 "Failed to allocate a struct ctf_fs_ds_group_medops_data");
422 status
= CTF_MSG_ITER_MEDIUM_STATUS_MEMORY_ERROR
;
426 data
->ds_file_group
= ds_file_group
;
427 data
->self_msg_iter
= self_msg_iter
;
428 data
->log_level
= log_level
;
431 * No need to prepare the first file. ctf_msg_iter will call
432 * switch_packet before reading the first packet, it will be
437 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
441 ctf_fs_ds_group_medops_data_destroy(data
);
447 void ctf_fs_ds_group_medops_data_reset(struct ctf_fs_ds_group_medops_data
*data
)
449 data
->next_index_entry_index
= 0;
452 struct ctf_msg_iter_medium_ops ctf_fs_ds_group_medops
= {
453 .request_bytes
= medop_group_request_bytes
,
456 * We don't support seeking using this medops. It would probably be
457 * possible, but it's not needed at the moment.
461 .switch_packet
= medop_group_switch_packet
,
462 .borrow_stream
= medop_group_borrow_stream
,
465 static struct ctf_fs_ds_index_entry
*ctf_fs_ds_index_entry_create(bt_self_component
*self_comp
,
466 bt_logging_level log_level
)
468 struct ctf_fs_ds_index_entry
*entry
;
470 entry
= g_new0(struct ctf_fs_ds_index_entry
, 1);
472 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to allocate a ctf_fs_ds_index_entry.");
476 entry
->packet_seq_num
= UINT64_MAX
;
482 static int convert_cycles_to_ns(struct ctf_clock_class
*clock_class
, uint64_t cycles
, int64_t *ns
)
484 return bt_util_clock_cycles_to_ns_from_origin(cycles
, clock_class
->frequency
,
485 clock_class
->offset_seconds
,
486 clock_class
->offset_cycles
, ns
);
489 static struct ctf_fs_ds_index
*build_index_from_idx_file(struct ctf_fs_ds_file
*ds_file
,
490 struct ctf_fs_ds_file_info
*file_info
,
491 struct ctf_msg_iter
*msg_iter
)
494 gchar
*directory
= NULL
;
495 gchar
*basename
= NULL
;
496 GString
*index_basename
= NULL
;
497 gchar
*index_file_path
= NULL
;
498 GMappedFile
*mapped_file
= NULL
;
500 const char *mmap_begin
= NULL
, *file_pos
= NULL
;
501 const struct ctf_packet_index_file_hdr
*header
= NULL
;
502 struct ctf_fs_ds_index
*index
= NULL
;
503 struct ctf_fs_ds_index_entry
*index_entry
= NULL
, *prev_index_entry
= NULL
;
504 uint64_t total_packets_size
= 0;
505 size_t file_index_entry_size
;
506 size_t file_entry_count
;
508 struct ctf_stream_class
*sc
;
509 struct ctf_msg_iter_packet_properties props
;
510 uint32_t version_major
, version_minor
;
511 bt_self_component
*self_comp
= ds_file
->self_comp
;
512 bt_logging_level log_level
= ds_file
->log_level
;
514 BT_COMP_LOGI("Building index from .idx file of stream file %s", ds_file
->file
->path
->str
);
515 ret
= ctf_msg_iter_get_packet_properties(msg_iter
, &props
);
517 BT_COMP_LOGI_STR("Cannot read first packet's header and context fields.");
521 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
, props
.stream_class_id
);
523 if (!sc
->default_clock_class
) {
524 BT_COMP_LOGI_STR("Cannot find stream class's default clock class.");
528 /* Look for index file in relative path index/name.idx. */
529 basename
= g_path_get_basename(ds_file
->file
->path
->str
);
531 BT_COMP_LOGE("Cannot get the basename of datastream file %s", ds_file
->file
->path
->str
);
535 directory
= g_path_get_dirname(ds_file
->file
->path
->str
);
537 BT_COMP_LOGE("Cannot get dirname of datastream file %s", ds_file
->file
->path
->str
);
541 index_basename
= g_string_new(basename
);
542 if (!index_basename
) {
543 BT_COMP_LOGE_STR("Cannot allocate index file basename string");
547 g_string_append(index_basename
, ".idx");
548 index_file_path
= g_build_filename(directory
, "index", index_basename
->str
, NULL
);
549 mapped_file
= g_mapped_file_new(index_file_path
, FALSE
, NULL
);
551 BT_COMP_LOGD("Cannot create new mapped file %s", index_file_path
);
556 * The g_mapped_file API limits us to 4GB files on 32-bit.
557 * Traces with such large indexes have never been seen in the wild,
558 * but this would need to be adjusted to support them.
560 filesize
= g_mapped_file_get_length(mapped_file
);
561 if (filesize
< sizeof(*header
)) {
562 BT_COMP_LOGW("Invalid LTTng trace index file: "
563 "file size (%zu bytes) < header size (%zu bytes)",
564 filesize
, sizeof(*header
));
568 mmap_begin
= g_mapped_file_get_contents(mapped_file
);
569 header
= (struct ctf_packet_index_file_hdr
*) mmap_begin
;
571 file_pos
= g_mapped_file_get_contents(mapped_file
) + sizeof(*header
);
572 if (be32toh(header
->magic
) != CTF_INDEX_MAGIC
) {
573 BT_COMP_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
577 version_major
= be32toh(header
->index_major
);
578 version_minor
= be32toh(header
->index_minor
);
579 if (version_major
!= 1) {
580 BT_COMP_LOGW("Unknown LTTng trace index version: "
581 "major=%" PRIu32
", minor=%" PRIu32
,
582 version_major
, version_minor
);
586 file_index_entry_size
= be32toh(header
->packet_index_len
);
587 if (file_index_entry_size
< CTF_INDEX_1_0_SIZE
) {
589 "Invalid `packet_index_len` in LTTng trace index file (`packet_index_len` < CTF index 1.0 index entry size): "
590 "packet_index_len=%zu, CTF_INDEX_1_0_SIZE=%zu",
591 file_index_entry_size
, CTF_INDEX_1_0_SIZE
);
595 file_entry_count
= (filesize
- sizeof(*header
)) / file_index_entry_size
;
596 if ((filesize
- sizeof(*header
)) % file_index_entry_size
) {
597 BT_COMP_LOGW("Invalid LTTng trace index: the index's size after the header "
598 "(%zu bytes) is not a multiple of the index entry size "
600 (filesize
- sizeof(*header
)), sizeof(*header
));
604 index
= ctf_fs_ds_index_create(ds_file
->log_level
, ds_file
->self_comp
);
609 for (i
= 0; i
< file_entry_count
; i
++) {
610 struct ctf_packet_index
*file_index
= (struct ctf_packet_index
*) file_pos
;
611 uint64_t packet_size
= be64toh(file_index
->packet_size
);
613 if (packet_size
% CHAR_BIT
) {
614 BT_COMP_LOGW("Invalid packet size encountered in LTTng trace index file");
618 index_entry
= ctf_fs_ds_index_entry_create(ds_file
->self_comp
, ds_file
->log_level
);
620 BT_COMP_LOGE_APPEND_CAUSE(ds_file
->self_comp
,
621 "Failed to create a ctf_fs_ds_index_entry.");
625 /* Set path to stream file. */
626 index_entry
->path
= file_info
->path
->str
;
628 /* Convert size in bits to bytes. */
629 packet_size
/= CHAR_BIT
;
630 index_entry
->packet_size
= packet_size
;
632 index_entry
->offset
= be64toh(file_index
->offset
);
633 if (i
!= 0 && index_entry
->offset
< prev_index_entry
->offset
) {
635 "Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
636 "previous offset=%" PRIu64
", current offset=%" PRIu64
,
637 prev_index_entry
->offset
, index_entry
->offset
);
641 index_entry
->timestamp_begin
= be64toh(file_index
->timestamp_begin
);
642 index_entry
->timestamp_end
= be64toh(file_index
->timestamp_end
);
643 if (index_entry
->timestamp_end
< index_entry
->timestamp_begin
) {
645 "Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
646 "timestamp_begin=%" PRIu64
"timestamp_end=%" PRIu64
,
647 index_entry
->timestamp_begin
, index_entry
->timestamp_end
);
651 /* Convert the packet's bound to nanoseconds since Epoch. */
652 ret
= convert_cycles_to_ns(sc
->default_clock_class
, index_entry
->timestamp_begin
,
653 &index_entry
->timestamp_begin_ns
);
656 "Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
659 ret
= convert_cycles_to_ns(sc
->default_clock_class
, index_entry
->timestamp_end
,
660 &index_entry
->timestamp_end_ns
);
663 "Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
667 if (version_minor
>= 1) {
668 index_entry
->packet_seq_num
= be64toh(file_index
->packet_seq_num
);
671 total_packets_size
+= packet_size
;
672 file_pos
+= file_index_entry_size
;
674 prev_index_entry
= index_entry
;
676 /* Give ownership of `index_entry` to `index->entries`. */
677 g_ptr_array_add(index
->entries
, index_entry
);
681 /* Validate that the index addresses the complete stream. */
682 if (ds_file
->file
->size
!= total_packets_size
) {
683 BT_COMP_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
684 "file-size=%" PRIu64
", total-packets-size=%" PRIu64
,
685 ds_file
->file
->size
, total_packets_size
);
691 g_free(index_file_path
);
692 if (index_basename
) {
693 g_string_free(index_basename
, TRUE
);
696 g_mapped_file_unref(mapped_file
);
700 ctf_fs_ds_index_destroy(index
);
706 static int init_index_entry(struct ctf_fs_ds_index_entry
*entry
, struct ctf_fs_ds_file
*ds_file
,
707 struct ctf_msg_iter_packet_properties
*props
, off_t packet_size
,
711 struct ctf_stream_class
*sc
;
712 bt_self_component
*self_comp
= ds_file
->self_comp
;
713 bt_logging_level log_level
= ds_file
->log_level
;
715 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
, props
->stream_class_id
);
717 BT_ASSERT(packet_offset
>= 0);
718 entry
->offset
= packet_offset
;
719 BT_ASSERT(packet_size
>= 0);
720 entry
->packet_size
= packet_size
;
722 if (props
->snapshots
.beginning_clock
!= UINT64_C(-1)) {
723 entry
->timestamp_begin
= props
->snapshots
.beginning_clock
;
725 /* Convert the packet's bound to nanoseconds since Epoch. */
726 ret
= convert_cycles_to_ns(sc
->default_clock_class
, props
->snapshots
.beginning_clock
,
727 &entry
->timestamp_begin_ns
);
729 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
733 entry
->timestamp_begin
= UINT64_C(-1);
734 entry
->timestamp_begin_ns
= UINT64_C(-1);
737 if (props
->snapshots
.end_clock
!= UINT64_C(-1)) {
738 entry
->timestamp_end
= props
->snapshots
.end_clock
;
740 /* Convert the packet's bound to nanoseconds since Epoch. */
741 ret
= convert_cycles_to_ns(sc
->default_clock_class
, props
->snapshots
.end_clock
,
742 &entry
->timestamp_end_ns
);
744 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
748 entry
->timestamp_end
= UINT64_C(-1);
749 entry
->timestamp_end_ns
= UINT64_C(-1);
756 static struct ctf_fs_ds_index
*build_index_from_stream_file(struct ctf_fs_ds_file
*ds_file
,
757 struct ctf_fs_ds_file_info
*file_info
,
758 struct ctf_msg_iter
*msg_iter
)
761 struct ctf_fs_ds_index
*index
= NULL
;
762 enum ctf_msg_iter_status iter_status
= CTF_MSG_ITER_STATUS_OK
;
763 off_t current_packet_offset_bytes
= 0;
764 bt_self_component
*self_comp
= ds_file
->self_comp
;
765 bt_logging_level log_level
= ds_file
->log_level
;
767 BT_COMP_LOGI("Indexing stream file %s", ds_file
->file
->path
->str
);
769 index
= ctf_fs_ds_index_create(ds_file
->log_level
, ds_file
->self_comp
);
775 off_t current_packet_size_bytes
;
776 struct ctf_fs_ds_index_entry
*index_entry
;
777 struct ctf_msg_iter_packet_properties props
;
779 if (current_packet_offset_bytes
< 0) {
780 BT_COMP_LOGE_STR("Cannot get the current packet's offset.");
782 } else if (current_packet_offset_bytes
> ds_file
->file
->size
) {
783 BT_COMP_LOGE_STR("Unexpected current packet's offset (larger than file).");
785 } else if (current_packet_offset_bytes
== ds_file
->file
->size
) {
790 iter_status
= ctf_msg_iter_seek(msg_iter
, current_packet_offset_bytes
);
791 if (iter_status
!= CTF_MSG_ITER_STATUS_OK
) {
795 iter_status
= ctf_msg_iter_get_packet_properties(msg_iter
, &props
);
796 if (iter_status
!= CTF_MSG_ITER_STATUS_OK
) {
800 if (props
.exp_packet_total_size
>= 0) {
801 current_packet_size_bytes
= (uint64_t) props
.exp_packet_total_size
/ 8;
803 current_packet_size_bytes
= ds_file
->file
->size
;
806 if (current_packet_offset_bytes
+ current_packet_size_bytes
> ds_file
->file
->size
) {
807 BT_COMP_LOGW("Invalid packet size reported in file: stream=\"%s\", "
808 "packet-offset=%jd, packet-size-bytes=%jd, "
810 ds_file
->file
->path
->str
, (intmax_t) current_packet_offset_bytes
,
811 (intmax_t) current_packet_size_bytes
, (intmax_t) ds_file
->file
->size
);
815 index_entry
= ctf_fs_ds_index_entry_create(ds_file
->self_comp
, ds_file
->log_level
);
817 BT_COMP_LOGE_APPEND_CAUSE(ds_file
->self_comp
,
818 "Failed to create a ctf_fs_ds_index_entry.");
822 /* Set path to stream file. */
823 index_entry
->path
= file_info
->path
->str
;
825 ret
= init_index_entry(index_entry
, ds_file
, &props
, current_packet_size_bytes
,
826 current_packet_offset_bytes
);
832 g_ptr_array_add(index
->entries
, index_entry
);
834 current_packet_offset_bytes
+= current_packet_size_bytes
;
835 BT_COMP_LOGD("Seeking to next packet: current-packet-offset=%jd, "
836 "next-packet-offset=%jd",
837 (intmax_t) (current_packet_offset_bytes
- current_packet_size_bytes
),
838 (intmax_t) current_packet_offset_bytes
);
845 ctf_fs_ds_index_destroy(index
);
850 struct ctf_fs_ds_file
*ctf_fs_ds_file_create(struct ctf_fs_trace
*ctf_fs_trace
, bt_stream
*stream
,
851 const char *path
, bt_logging_level log_level
)
854 const size_t offset_align
= bt_mmap_get_offset_align_size(log_level
);
855 struct ctf_fs_ds_file
*ds_file
= g_new0(struct ctf_fs_ds_file
, 1);
861 ds_file
->log_level
= log_level
;
862 ds_file
->self_comp
= ctf_fs_trace
->self_comp
;
863 ds_file
->file
= ctf_fs_file_create(log_level
, ds_file
->self_comp
);
864 if (!ds_file
->file
) {
868 ds_file
->stream
= stream
;
869 bt_stream_get_ref(ds_file
->stream
);
870 ds_file
->metadata
= ctf_fs_trace
->metadata
;
871 g_string_assign(ds_file
->file
->path
, path
);
872 ret
= ctf_fs_file_open(ds_file
->file
, "rb");
877 ds_file
->mmap_max_len
= offset_align
* 2048;
882 /* Do not touch "borrowed" file. */
883 ctf_fs_ds_file_destroy(ds_file
);
890 struct ctf_fs_ds_index
*ctf_fs_ds_file_build_index(struct ctf_fs_ds_file
*ds_file
,
891 struct ctf_fs_ds_file_info
*file_info
,
892 struct ctf_msg_iter
*msg_iter
)
894 struct ctf_fs_ds_index
*index
;
895 bt_self_component
*self_comp
= ds_file
->self_comp
;
896 bt_logging_level log_level
= ds_file
->log_level
;
898 index
= build_index_from_idx_file(ds_file
, file_info
, msg_iter
);
903 BT_COMP_LOGI("Failed to build index from .index file; "
904 "falling back to stream indexing.");
905 index
= build_index_from_stream_file(ds_file
, file_info
, msg_iter
);
910 struct ctf_fs_ds_index
*ctf_fs_ds_index_create(bt_logging_level log_level
,
911 bt_self_component
*self_comp
)
913 struct ctf_fs_ds_index
*index
= g_new0(struct ctf_fs_ds_index
, 1);
916 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
, "Failed to allocate index");
920 index
->entries
= g_ptr_array_new_with_free_func((GDestroyNotify
) g_free
);
921 if (!index
->entries
) {
922 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
923 "Failed to allocate index entries.");
930 ctf_fs_ds_index_destroy(index
);
936 void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file
*ds_file
)
942 bt_stream_put_ref(ds_file
->stream
);
943 (void) ds_file_munmap(ds_file
);
946 ctf_fs_file_destroy(ds_file
->file
);
952 void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index
*index
)
958 if (index
->entries
) {
959 g_ptr_array_free(index
->entries
, TRUE
);