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"
20 #include "logging/comp-logging.h"
22 #include "common/assert.h"
23 #include "compat/endian.h"
24 #include "compat/mman.h"
26 #include "../common/msg-iter/msg-iter.hpp"
27 #include "data-stream-file.hpp"
30 #include "lttng-index.hpp"
31 #include "plugins/ctf/common/metadata/ctf-meta.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 enum ctf_msg_iter_medium_status status
;
52 bt_self_component
*self_comp
= ds_file
->self_comp
;
53 bt_logging_level log_level
= ds_file
->log_level
;
57 if (!ds_file
->mmap_addr
) {
58 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
62 if (bt_munmap(ds_file
->mmap_addr
, ds_file
->mmap_len
)) {
63 BT_COMP_LOGE_ERRNO("Cannot memory-unmap file",
64 ": address=%p, size=%zu, file_path=\"%s\", file=%p", ds_file
->mmap_addr
,
65 ds_file
->mmap_len
, ds_file
->file
? ds_file
->file
->path
->str
: "NULL",
66 ds_file
->file
? ds_file
->file
->fp
: NULL
);
67 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
71 ds_file
->mmap_addr
= NULL
;
73 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
79 * mmap a region of `ds_file` such that `requested_offset_in_file` is in the
80 * mapping. If the currently mmap-ed region already contains
81 * `requested_offset_in_file`, the mapping is kept.
83 * Set `ds_file->requested_offset_in_mapping` based on `request_offset_in_file`,
84 * such that the next call to `request_bytes` will return bytes starting at that
87 * `requested_offset_in_file` must be a valid offset in the file.
89 static enum ctf_msg_iter_medium_status
ds_file_mmap(struct ctf_fs_ds_file
*ds_file
,
90 off_t requested_offset_in_file
)
92 enum ctf_msg_iter_medium_status status
;
93 bt_self_component
*self_comp
= ds_file
->self_comp
;
94 bt_logging_level log_level
= ds_file
->log_level
;
96 /* Ensure the requested offset is in the file range. */
97 BT_ASSERT(requested_offset_in_file
>= 0);
98 BT_ASSERT(requested_offset_in_file
< ds_file
->file
->size
);
101 * If the mapping already contains the requested offset, just adjust
102 * requested_offset_in_mapping.
104 if (offset_ist_mapped(ds_file
, requested_offset_in_file
)) {
105 ds_file
->request_offset_in_mapping
=
106 requested_offset_in_file
- ds_file
->mmap_offset_in_file
;
107 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
111 /* Unmap old region */
112 status
= ds_file_munmap(ds_file
);
113 if (status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
118 * Compute a mapping that has the required alignment properties and
119 * contains `requested_offset_in_file`.
121 ds_file
->request_offset_in_mapping
=
122 requested_offset_in_file
% bt_mmap_get_offset_align_size(ds_file
->log_level
);
123 ds_file
->mmap_offset_in_file
= requested_offset_in_file
- ds_file
->request_offset_in_mapping
;
125 MIN(ds_file
->file
->size
- ds_file
->mmap_offset_in_file
, ds_file
->mmap_max_len
);
127 BT_ASSERT(ds_file
->mmap_len
> 0);
130 bt_mmap(ds_file
->mmap_len
, PROT_READ
, MAP_PRIVATE
, fileno(ds_file
->file
->fp
),
131 ds_file
->mmap_offset_in_file
, ds_file
->log_level
);
132 if (ds_file
->mmap_addr
== MAP_FAILED
) {
133 BT_COMP_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s",
134 ds_file
->mmap_len
, ds_file
->file
->path
->str
, ds_file
->file
->fp
,
135 (intmax_t) ds_file
->mmap_offset_in_file
, strerror(errno
));
136 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
140 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
147 * Change the mapping of the file to read the region that follows the current
150 * If the file hasn't been mapped yet, then everything (mmap_offset_in_file,
151 * mmap_len, request_offset_in_mapping) should have the value 0, which will
152 * result in the beginning of the file getting mapped.
154 * return _EOF if the current mapping is the end of the file.
157 static enum ctf_msg_iter_medium_status
ds_file_mmap_next(struct ctf_fs_ds_file
*ds_file
)
159 enum ctf_msg_iter_medium_status status
;
162 * If we're called, it's because more bytes are requested but we have
163 * given all the bytes of the current mapping.
165 BT_ASSERT(ds_file
->request_offset_in_mapping
== ds_file
->mmap_len
);
168 * If the current mapping coincides with the end of the file, there is
171 if (ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
== ds_file
->file
->size
) {
172 status
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
176 status
= ds_file_mmap(ds_file
, ds_file
->mmap_offset_in_file
+ ds_file
->mmap_len
);
182 static enum ctf_msg_iter_medium_status
medop_request_bytes(size_t request_sz
, uint8_t **buffer_addr
,
183 size_t *buffer_sz
, void *data
)
185 enum ctf_msg_iter_medium_status status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
186 struct ctf_fs_ds_file
*ds_file
= (struct ctf_fs_ds_file
*) data
;
187 bt_self_component
*self_comp
= ds_file
->self_comp
;
188 bt_logging_level log_level
= ds_file
->log_level
;
190 BT_ASSERT(request_sz
> 0);
193 * Check if we have at least one memory-mapped byte left. If we don't,
194 * mmap the next file.
196 if (remaining_mmap_bytes(ds_file
) == 0) {
197 /* Are we at the end of the file? */
198 if (ds_file
->mmap_offset_in_file
>= ds_file
->file
->size
) {
199 BT_COMP_LOGD("Reached end of file \"%s\" (%p)", ds_file
->file
->path
->str
,
201 status
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
205 status
= ds_file_mmap_next(ds_file
);
207 case CTF_MSG_ITER_MEDIUM_STATUS_OK
:
209 case CTF_MSG_ITER_MEDIUM_STATUS_EOF
:
212 BT_COMP_LOGE("Cannot memory-map next region of file \"%s\" (%p)",
213 ds_file
->file
->path
->str
, ds_file
->file
->fp
);
218 BT_ASSERT(remaining_mmap_bytes(ds_file
) > 0);
219 *buffer_sz
= MIN(remaining_mmap_bytes(ds_file
), request_sz
);
221 BT_ASSERT(ds_file
->mmap_addr
);
222 *buffer_addr
= ((uint8_t *) ds_file
->mmap_addr
) + ds_file
->request_offset_in_mapping
;
224 ds_file
->request_offset_in_mapping
+= *buffer_sz
;
228 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
234 static bt_stream
*medop_borrow_stream(bt_stream_class
*stream_class
, int64_t, void *data
)
236 struct ctf_fs_ds_file
*ds_file
= (struct ctf_fs_ds_file
*) data
;
237 bt_stream_class
*ds_file_stream_class
;
238 bt_stream
*stream
= NULL
;
240 ds_file_stream_class
= bt_stream_borrow_class(ds_file
->stream
);
242 if (stream_class
!= ds_file_stream_class
) {
244 * Not supported: two packets described by two different
245 * stream classes within the same data stream file.
250 stream
= ds_file
->stream
;
256 static enum ctf_msg_iter_medium_status
medop_seek(off_t offset
, void *data
)
258 struct ctf_fs_ds_file
*ds_file
= (struct ctf_fs_ds_file
*) data
;
260 BT_ASSERT(offset
>= 0);
261 BT_ASSERT(offset
< ds_file
->file
->size
);
263 return ds_file_mmap(ds_file
, offset
);
266 struct ctf_msg_iter_medium_ops ctf_fs_ds_file_medops
= {
273 struct ctf_fs_ds_group_medops_data
275 /* Weak, set once at creation time. */
276 struct ctf_fs_ds_file_group
*ds_file_group
;
279 * Index (as in element rank) of the index entry of ds_file_groups'
280 * index we will read next (so, the one after the one we are reading
283 guint next_index_entry_index
;
286 * File we are currently reading. Changes whenever we switch to
287 * reading another data file.
291 struct ctf_fs_ds_file
*file
;
293 /* Weak, for context / logging / appending causes. */
294 bt_self_message_iterator
*self_msg_iter
;
295 bt_logging_level log_level
;
298 static enum ctf_msg_iter_medium_status
medop_group_request_bytes(size_t request_sz
,
299 uint8_t **buffer_addr
,
300 size_t *buffer_sz
, void *void_data
)
302 struct ctf_fs_ds_group_medops_data
*data
= (struct ctf_fs_ds_group_medops_data
*) void_data
;
304 /* Return bytes from the current file. */
305 return medop_request_bytes(request_sz
, buffer_addr
, buffer_sz
, data
->file
);
308 static bt_stream
*medop_group_borrow_stream(bt_stream_class
*stream_class
, int64_t stream_id
,
311 struct ctf_fs_ds_group_medops_data
*data
= (struct ctf_fs_ds_group_medops_data
*) void_data
;
313 return medop_borrow_stream(stream_class
, stream_id
, data
->file
);
317 * Set `data->file` to prepare it to read the packet described
321 static enum ctf_msg_iter_medium_status
322 ctf_fs_ds_group_medops_set_file(struct ctf_fs_ds_group_medops_data
*data
,
323 struct ctf_fs_ds_index_entry
*index_entry
,
324 bt_self_message_iterator
*self_msg_iter
, bt_logging_level log_level
)
326 enum ctf_msg_iter_medium_status status
;
329 BT_ASSERT(index_entry
);
331 /* Check if that file is already the one mapped. */
332 if (!data
->file
|| strcmp(index_entry
->path
, data
->file
->file
->path
->str
) != 0) {
333 /* Destroy the previously used file. */
334 ctf_fs_ds_file_destroy(data
->file
);
336 /* Create the new file. */
338 ctf_fs_ds_file_create(data
->ds_file_group
->ctf_fs_trace
, data
->ds_file_group
->stream
,
339 index_entry
->path
, log_level
);
341 BT_MSG_ITER_LOGE_APPEND_CAUSE(self_msg_iter
, "failed to create ctf_fs_ds_file.");
342 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
348 * Ensure the right portion of the file will be returned on the next
349 * request_bytes call.
351 status
= ds_file_mmap(data
->file
, index_entry
->offset
);
352 if (status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
356 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
362 static enum ctf_msg_iter_medium_status
medop_group_switch_packet(void *void_data
)
364 struct ctf_fs_ds_group_medops_data
*data
= (struct ctf_fs_ds_group_medops_data
*) void_data
;
365 struct ctf_fs_ds_index_entry
*index_entry
;
366 enum ctf_msg_iter_medium_status status
;
368 /* If we have gone through all index entries, we are done. */
369 if (data
->next_index_entry_index
>= data
->ds_file_group
->index
->entries
->len
) {
370 status
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
375 * Otherwise, look up the next index entry / packet and prepare it
378 index_entry
= (struct ctf_fs_ds_index_entry
*) g_ptr_array_index(
379 data
->ds_file_group
->index
->entries
, data
->next_index_entry_index
);
382 ctf_fs_ds_group_medops_set_file(data
, index_entry
, data
->self_msg_iter
, data
->log_level
);
383 if (status
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
387 data
->next_index_entry_index
++;
389 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
394 void ctf_fs_ds_group_medops_data_destroy(struct ctf_fs_ds_group_medops_data
*data
)
400 ctf_fs_ds_file_destroy(data
->file
);
408 enum ctf_msg_iter_medium_status
ctf_fs_ds_group_medops_data_create(
409 struct ctf_fs_ds_file_group
*ds_file_group
, bt_self_message_iterator
*self_msg_iter
,
410 bt_logging_level log_level
, struct ctf_fs_ds_group_medops_data
**out
)
412 struct ctf_fs_ds_group_medops_data
*data
;
413 enum ctf_msg_iter_medium_status status
;
415 BT_ASSERT(self_msg_iter
);
416 BT_ASSERT(ds_file_group
);
417 BT_ASSERT(ds_file_group
->index
);
418 BT_ASSERT(ds_file_group
->index
->entries
->len
> 0);
420 data
= g_new0(struct ctf_fs_ds_group_medops_data
, 1);
422 BT_MSG_ITER_LOGE_APPEND_CAUSE(self_msg_iter
,
423 "Failed to allocate a struct ctf_fs_ds_group_medops_data");
424 status
= CTF_MSG_ITER_MEDIUM_STATUS_MEMORY_ERROR
;
428 data
->ds_file_group
= ds_file_group
;
429 data
->self_msg_iter
= self_msg_iter
;
430 data
->log_level
= log_level
;
433 * No need to prepare the first file. ctf_msg_iter will call
434 * switch_packet before reading the first packet, it will be
439 status
= CTF_MSG_ITER_MEDIUM_STATUS_OK
;
443 ctf_fs_ds_group_medops_data_destroy(data
);
449 void ctf_fs_ds_group_medops_data_reset(struct ctf_fs_ds_group_medops_data
*data
)
451 data
->next_index_entry_index
= 0;
454 struct ctf_msg_iter_medium_ops ctf_fs_ds_group_medops
= {
455 .request_bytes
= medop_group_request_bytes
,
458 * We don't support seeking using this medops. It would probably be
459 * possible, but it's not needed at the moment.
463 .switch_packet
= medop_group_switch_packet
,
464 .borrow_stream
= medop_group_borrow_stream
,
467 static struct ctf_fs_ds_index_entry
*ctf_fs_ds_index_entry_create(bt_self_component
*self_comp
,
468 bt_logging_level log_level
)
470 struct ctf_fs_ds_index_entry
*entry
;
472 entry
= g_new0(struct ctf_fs_ds_index_entry
, 1);
474 BT_COMP_LOGE_APPEND_CAUSE(self_comp
, "Failed to allocate a ctf_fs_ds_index_entry.");
478 entry
->packet_seq_num
= UINT64_MAX
;
484 static int convert_cycles_to_ns(struct ctf_clock_class
*clock_class
, uint64_t cycles
, int64_t *ns
)
486 return bt_util_clock_cycles_to_ns_from_origin(cycles
, clock_class
->frequency
,
487 clock_class
->offset_seconds
,
488 clock_class
->offset_cycles
, ns
);
491 static struct ctf_fs_ds_index
*build_index_from_idx_file(struct ctf_fs_ds_file
*ds_file
,
492 struct ctf_fs_ds_file_info
*file_info
,
493 struct ctf_msg_iter
*msg_iter
)
496 gchar
*directory
= NULL
;
497 gchar
*basename
= NULL
;
498 GString
*index_basename
= NULL
;
499 gchar
*index_file_path
= NULL
;
500 GMappedFile
*mapped_file
= NULL
;
502 const char *mmap_begin
= NULL
, *file_pos
= NULL
;
503 const struct ctf_packet_index_file_hdr
*header
= NULL
;
504 struct ctf_fs_ds_index
*index
= NULL
;
505 struct ctf_fs_ds_index_entry
*index_entry
= NULL
, *prev_index_entry
= NULL
;
506 uint64_t total_packets_size
= 0;
507 size_t file_index_entry_size
;
508 size_t file_entry_count
;
510 struct ctf_stream_class
*sc
;
511 struct ctf_msg_iter_packet_properties props
;
512 uint32_t version_major
, version_minor
;
513 bt_self_component
*self_comp
= ds_file
->self_comp
;
514 bt_logging_level log_level
= ds_file
->log_level
;
516 BT_COMP_LOGI("Building index from .idx file of stream file %s", ds_file
->file
->path
->str
);
517 ret
= ctf_msg_iter_get_packet_properties(msg_iter
, &props
);
519 BT_COMP_LOGI_STR("Cannot read first packet's header and context fields.");
523 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
, props
.stream_class_id
);
525 if (!sc
->default_clock_class
) {
526 BT_COMP_LOGI_STR("Cannot find stream class's default clock class.");
530 /* Look for index file in relative path index/name.idx. */
531 basename
= g_path_get_basename(ds_file
->file
->path
->str
);
533 BT_COMP_LOGE("Cannot get the basename of datastream file %s", ds_file
->file
->path
->str
);
537 directory
= g_path_get_dirname(ds_file
->file
->path
->str
);
539 BT_COMP_LOGE("Cannot get dirname of datastream file %s", ds_file
->file
->path
->str
);
543 index_basename
= g_string_new(basename
);
544 if (!index_basename
) {
545 BT_COMP_LOGE_STR("Cannot allocate index file basename string");
549 g_string_append(index_basename
, ".idx");
550 index_file_path
= g_build_filename(directory
, "index", index_basename
->str
, NULL
);
551 mapped_file
= g_mapped_file_new(index_file_path
, FALSE
, NULL
);
553 BT_COMP_LOGD("Cannot create new mapped file %s", index_file_path
);
558 * The g_mapped_file API limits us to 4GB files on 32-bit.
559 * Traces with such large indexes have never been seen in the wild,
560 * but this would need to be adjusted to support them.
562 filesize
= g_mapped_file_get_length(mapped_file
);
563 if (filesize
< sizeof(*header
)) {
564 BT_COMP_LOGW("Invalid LTTng trace index file: "
565 "file size (%zu bytes) < header size (%zu bytes)",
566 filesize
, sizeof(*header
));
570 mmap_begin
= g_mapped_file_get_contents(mapped_file
);
571 header
= (struct ctf_packet_index_file_hdr
*) mmap_begin
;
573 file_pos
= g_mapped_file_get_contents(mapped_file
) + sizeof(*header
);
574 if (be32toh(header
->magic
) != CTF_INDEX_MAGIC
) {
575 BT_COMP_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
579 version_major
= be32toh(header
->index_major
);
580 version_minor
= be32toh(header
->index_minor
);
581 if (version_major
!= 1) {
582 BT_COMP_LOGW("Unknown LTTng trace index version: "
583 "major=%" PRIu32
", minor=%" PRIu32
,
584 version_major
, version_minor
);
588 file_index_entry_size
= be32toh(header
->packet_index_len
);
589 if (file_index_entry_size
< CTF_INDEX_1_0_SIZE
) {
591 "Invalid `packet_index_len` in LTTng trace index file (`packet_index_len` < CTF index 1.0 index entry size): "
592 "packet_index_len=%zu, CTF_INDEX_1_0_SIZE=%zu",
593 file_index_entry_size
, CTF_INDEX_1_0_SIZE
);
597 file_entry_count
= (filesize
- sizeof(*header
)) / file_index_entry_size
;
598 if ((filesize
- sizeof(*header
)) % file_index_entry_size
) {
599 BT_COMP_LOGW("Invalid LTTng trace index: the index's size after the header "
600 "(%zu bytes) is not a multiple of the index entry size "
602 (filesize
- sizeof(*header
)), sizeof(*header
));
606 index
= ctf_fs_ds_index_create(ds_file
->log_level
, ds_file
->self_comp
);
611 for (i
= 0; i
< file_entry_count
; i
++) {
612 struct ctf_packet_index
*file_index
= (struct ctf_packet_index
*) file_pos
;
613 uint64_t packet_size
= be64toh(file_index
->packet_size
);
615 if (packet_size
% CHAR_BIT
) {
616 BT_COMP_LOGW("Invalid packet size encountered in LTTng trace index file");
620 index_entry
= ctf_fs_ds_index_entry_create(ds_file
->self_comp
, ds_file
->log_level
);
622 BT_COMP_LOGE_APPEND_CAUSE(ds_file
->self_comp
,
623 "Failed to create a ctf_fs_ds_index_entry.");
627 /* Set path to stream file. */
628 index_entry
->path
= file_info
->path
->str
;
630 /* Convert size in bits to bytes. */
631 packet_size
/= CHAR_BIT
;
632 index_entry
->packet_size
= packet_size
;
634 index_entry
->offset
= be64toh(file_index
->offset
);
635 if (i
!= 0 && index_entry
->offset
< prev_index_entry
->offset
) {
637 "Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
638 "previous offset=%" PRIu64
", current offset=%" PRIu64
,
639 prev_index_entry
->offset
, index_entry
->offset
);
643 index_entry
->timestamp_begin
= be64toh(file_index
->timestamp_begin
);
644 index_entry
->timestamp_end
= be64toh(file_index
->timestamp_end
);
645 if (index_entry
->timestamp_end
< index_entry
->timestamp_begin
) {
647 "Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
648 "timestamp_begin=%" PRIu64
"timestamp_end=%" PRIu64
,
649 index_entry
->timestamp_begin
, index_entry
->timestamp_end
);
653 /* Convert the packet's bound to nanoseconds since Epoch. */
654 ret
= convert_cycles_to_ns(sc
->default_clock_class
, index_entry
->timestamp_begin
,
655 &index_entry
->timestamp_begin_ns
);
658 "Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
661 ret
= convert_cycles_to_ns(sc
->default_clock_class
, index_entry
->timestamp_end
,
662 &index_entry
->timestamp_end_ns
);
665 "Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
669 if (version_minor
>= 1) {
670 index_entry
->packet_seq_num
= be64toh(file_index
->packet_seq_num
);
673 total_packets_size
+= packet_size
;
674 file_pos
+= file_index_entry_size
;
676 prev_index_entry
= index_entry
;
678 /* Give ownership of `index_entry` to `index->entries`. */
679 g_ptr_array_add(index
->entries
, index_entry
);
683 /* Validate that the index addresses the complete stream. */
684 if (ds_file
->file
->size
!= total_packets_size
) {
685 BT_COMP_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
686 "file-size=%" PRIu64
", total-packets-size=%" PRIu64
,
687 ds_file
->file
->size
, total_packets_size
);
693 g_free(index_file_path
);
694 if (index_basename
) {
695 g_string_free(index_basename
, TRUE
);
698 g_mapped_file_unref(mapped_file
);
702 ctf_fs_ds_index_destroy(index
);
708 static int init_index_entry(struct ctf_fs_ds_index_entry
*entry
, struct ctf_fs_ds_file
*ds_file
,
709 struct ctf_msg_iter_packet_properties
*props
, off_t packet_size
,
713 struct ctf_stream_class
*sc
;
714 bt_self_component
*self_comp
= ds_file
->self_comp
;
715 bt_logging_level log_level
= ds_file
->log_level
;
717 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
, props
->stream_class_id
);
719 BT_ASSERT(packet_offset
>= 0);
720 entry
->offset
= packet_offset
;
721 BT_ASSERT(packet_size
>= 0);
722 entry
->packet_size
= packet_size
;
724 if (props
->snapshots
.beginning_clock
!= UINT64_C(-1)) {
725 entry
->timestamp_begin
= props
->snapshots
.beginning_clock
;
727 /* Convert the packet's bound to nanoseconds since Epoch. */
728 ret
= convert_cycles_to_ns(sc
->default_clock_class
, props
->snapshots
.beginning_clock
,
729 &entry
->timestamp_begin_ns
);
731 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
735 entry
->timestamp_begin
= UINT64_C(-1);
736 entry
->timestamp_begin_ns
= UINT64_C(-1);
739 if (props
->snapshots
.end_clock
!= UINT64_C(-1)) {
740 entry
->timestamp_end
= props
->snapshots
.end_clock
;
742 /* Convert the packet's bound to nanoseconds since Epoch. */
743 ret
= convert_cycles_to_ns(sc
->default_clock_class
, props
->snapshots
.end_clock
,
744 &entry
->timestamp_end_ns
);
746 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
750 entry
->timestamp_end
= UINT64_C(-1);
751 entry
->timestamp_end_ns
= UINT64_C(-1);
758 static struct ctf_fs_ds_index
*build_index_from_stream_file(struct ctf_fs_ds_file
*ds_file
,
759 struct ctf_fs_ds_file_info
*file_info
,
760 struct ctf_msg_iter
*msg_iter
)
763 struct ctf_fs_ds_index
*index
= NULL
;
764 enum ctf_msg_iter_status iter_status
= CTF_MSG_ITER_STATUS_OK
;
765 off_t current_packet_offset_bytes
= 0;
766 bt_self_component
*self_comp
= ds_file
->self_comp
;
767 bt_logging_level log_level
= ds_file
->log_level
;
769 BT_COMP_LOGI("Indexing stream file %s", ds_file
->file
->path
->str
);
771 index
= ctf_fs_ds_index_create(ds_file
->log_level
, ds_file
->self_comp
);
777 off_t current_packet_size_bytes
;
778 struct ctf_fs_ds_index_entry
*index_entry
;
779 struct ctf_msg_iter_packet_properties props
;
781 if (current_packet_offset_bytes
< 0) {
782 BT_COMP_LOGE_STR("Cannot get the current packet's offset.");
784 } else if (current_packet_offset_bytes
> ds_file
->file
->size
) {
785 BT_COMP_LOGE_STR("Unexpected current packet's offset (larger than file).");
787 } else if (current_packet_offset_bytes
== ds_file
->file
->size
) {
792 iter_status
= ctf_msg_iter_seek(msg_iter
, current_packet_offset_bytes
);
793 if (iter_status
!= CTF_MSG_ITER_STATUS_OK
) {
797 iter_status
= ctf_msg_iter_get_packet_properties(msg_iter
, &props
);
798 if (iter_status
!= CTF_MSG_ITER_STATUS_OK
) {
802 if (props
.exp_packet_total_size
>= 0) {
803 current_packet_size_bytes
= (uint64_t) props
.exp_packet_total_size
/ 8;
805 current_packet_size_bytes
= ds_file
->file
->size
;
808 if (current_packet_offset_bytes
+ current_packet_size_bytes
> ds_file
->file
->size
) {
809 BT_COMP_LOGW("Invalid packet size reported in file: stream=\"%s\", "
810 "packet-offset=%jd, packet-size-bytes=%jd, "
812 ds_file
->file
->path
->str
, (intmax_t) current_packet_offset_bytes
,
813 (intmax_t) current_packet_size_bytes
, (intmax_t) ds_file
->file
->size
);
817 index_entry
= ctf_fs_ds_index_entry_create(ds_file
->self_comp
, ds_file
->log_level
);
819 BT_COMP_LOGE_APPEND_CAUSE(ds_file
->self_comp
,
820 "Failed to create a ctf_fs_ds_index_entry.");
824 /* Set path to stream file. */
825 index_entry
->path
= file_info
->path
->str
;
827 ret
= init_index_entry(index_entry
, ds_file
, &props
, current_packet_size_bytes
,
828 current_packet_offset_bytes
);
834 g_ptr_array_add(index
->entries
, index_entry
);
836 current_packet_offset_bytes
+= current_packet_size_bytes
;
837 BT_COMP_LOGD("Seeking to next packet: current-packet-offset=%jd, "
838 "next-packet-offset=%jd",
839 (intmax_t) (current_packet_offset_bytes
- current_packet_size_bytes
),
840 (intmax_t) current_packet_offset_bytes
);
847 ctf_fs_ds_index_destroy(index
);
852 struct ctf_fs_ds_file
*ctf_fs_ds_file_create(struct ctf_fs_trace
*ctf_fs_trace
, bt_stream
*stream
,
853 const char *path
, bt_logging_level log_level
)
856 const size_t offset_align
= bt_mmap_get_offset_align_size(log_level
);
857 struct ctf_fs_ds_file
*ds_file
= g_new0(struct ctf_fs_ds_file
, 1);
863 ds_file
->log_level
= log_level
;
864 ds_file
->self_comp
= ctf_fs_trace
->self_comp
;
865 ds_file
->file
= ctf_fs_file_create(log_level
, ds_file
->self_comp
);
866 if (!ds_file
->file
) {
870 ds_file
->stream
= stream
;
871 bt_stream_get_ref(ds_file
->stream
);
872 ds_file
->metadata
= ctf_fs_trace
->metadata
;
873 g_string_assign(ds_file
->file
->path
, path
);
874 ret
= ctf_fs_file_open(ds_file
->file
, "rb");
879 ds_file
->mmap_max_len
= offset_align
* 2048;
884 /* Do not touch "borrowed" file. */
885 ctf_fs_ds_file_destroy(ds_file
);
892 struct ctf_fs_ds_index
*ctf_fs_ds_file_build_index(struct ctf_fs_ds_file
*ds_file
,
893 struct ctf_fs_ds_file_info
*file_info
,
894 struct ctf_msg_iter
*msg_iter
)
896 struct ctf_fs_ds_index
*index
;
897 bt_self_component
*self_comp
= ds_file
->self_comp
;
898 bt_logging_level log_level
= ds_file
->log_level
;
900 index
= build_index_from_idx_file(ds_file
, file_info
, msg_iter
);
905 BT_COMP_LOGI("Failed to build index from .index file; "
906 "falling back to stream indexing.");
907 index
= build_index_from_stream_file(ds_file
, file_info
, msg_iter
);
912 struct ctf_fs_ds_index
*ctf_fs_ds_index_create(bt_logging_level log_level
,
913 bt_self_component
*self_comp
)
915 struct ctf_fs_ds_index
*index
= g_new0(struct ctf_fs_ds_index
, 1);
918 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
, "Failed to allocate index");
922 index
->entries
= g_ptr_array_new_with_free_func((GDestroyNotify
) g_free
);
923 if (!index
->entries
) {
924 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
925 "Failed to allocate index entries.");
932 ctf_fs_ds_index_destroy(index
);
938 void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file
*ds_file
)
944 bt_stream_put_ref(ds_file
->stream
);
945 (void) ds_file_munmap(ds_file
);
948 ctf_fs_file_destroy(ds_file
->file
);
954 void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index
*index
)
960 if (index
->entries
) {
961 g_ptr_array_free(index
->entries
, TRUE
);