2 * Copyright 2016-2017 - Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_COMP_LOG_SELF_COMP (self_comp)
26 #define BT_LOG_OUTPUT_LEVEL (log_level)
27 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/DS"
28 #include "logging/comp-logging.h"
36 #include "compat/mman.h"
37 #include "compat/endian.h"
38 #include <babeltrace2/babeltrace.h>
39 #include "common/common.h"
42 #include "../common/msg-iter/msg-iter.h"
43 #include "common/assert.h"
44 #include "data-stream-file.h"
48 size_t remaining_mmap_bytes(struct ctf_fs_ds_file
*ds_file
)
50 BT_ASSERT_DBG(ds_file
->mmap_len
>= ds_file
->request_offset
);
51 return ds_file
->mmap_len
- ds_file
->request_offset
;
55 int ds_file_munmap(struct ctf_fs_ds_file
*ds_file
)
58 bt_self_component
*self_comp
= ds_file
->self_comp
;
59 bt_logging_level log_level
= ds_file
->log_level
;
61 if (!ds_file
|| !ds_file
->mmap_addr
) {
65 if (bt_munmap(ds_file
->mmap_addr
, ds_file
->mmap_len
)) {
66 BT_COMP_LOGE_ERRNO("Cannot memory-unmap file",
67 ": address=%p, size=%zu, file_path=\"%s\", file=%p",
68 ds_file
->mmap_addr
, ds_file
->mmap_len
,
69 ds_file
->file
? ds_file
->file
->path
->str
: "NULL",
70 ds_file
->file
? ds_file
->file
->fp
: NULL
);
75 ds_file
->mmap_addr
= NULL
;
82 enum ctf_msg_iter_medium_status
ds_file_mmap_next(
83 struct ctf_fs_ds_file
*ds_file
)
85 enum ctf_msg_iter_medium_status ret
=
86 CTF_MSG_ITER_MEDIUM_STATUS_OK
;
87 bt_self_component
*self_comp
= ds_file
->self_comp
;
88 bt_logging_level log_level
= ds_file
->log_level
;
90 /* Unmap old region */
91 if (ds_file
->mmap_addr
) {
92 if (ds_file_munmap(ds_file
)) {
97 * mmap_len is guaranteed to be page-aligned except on the
98 * last mapping where it may not be possible (since the file's
99 * size itself may not be a page multiple).
101 ds_file
->mmap_offset
+= ds_file
->mmap_len
;
102 ds_file
->request_offset
= 0;
105 ds_file
->mmap_len
= MIN(ds_file
->file
->size
- ds_file
->mmap_offset
,
106 ds_file
->mmap_max_len
);
107 if (ds_file
->mmap_len
== 0) {
108 ret
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
112 BT_ASSERT(ds_file
->mmap_len
);
113 ds_file
->mmap_addr
= bt_mmap((void *) 0, ds_file
->mmap_len
,
114 PROT_READ
, MAP_PRIVATE
, fileno(ds_file
->file
->fp
),
115 ds_file
->mmap_offset
, ds_file
->log_level
);
116 if (ds_file
->mmap_addr
== MAP_FAILED
) {
117 BT_COMP_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s",
118 ds_file
->mmap_len
, ds_file
->file
->path
->str
,
119 ds_file
->file
->fp
, (intmax_t) ds_file
->mmap_offset
,
126 ds_file_munmap(ds_file
);
127 ret
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
133 enum ctf_msg_iter_medium_status
medop_request_bytes(
134 size_t request_sz
, uint8_t **buffer_addr
,
135 size_t *buffer_sz
, void *data
)
137 enum ctf_msg_iter_medium_status status
=
138 CTF_MSG_ITER_MEDIUM_STATUS_OK
;
139 struct ctf_fs_ds_file
*ds_file
= data
;
140 bt_self_component
*self_comp
= ds_file
->self_comp
;
141 bt_logging_level log_level
= ds_file
->log_level
;
143 if (request_sz
== 0) {
148 * Check if we have at least one memory-mapped byte left. If we don't,
149 * mmap the next file.
151 if (remaining_mmap_bytes(ds_file
) == 0) {
152 /* Are we at the end of the file? */
153 if (ds_file
->mmap_offset
>= ds_file
->file
->size
) {
154 BT_COMP_LOGD("Reached end of file \"%s\" (%p)",
155 ds_file
->file
->path
->str
, ds_file
->file
->fp
);
156 status
= CTF_MSG_ITER_MEDIUM_STATUS_EOF
;
160 status
= ds_file_mmap_next(ds_file
);
162 case CTF_MSG_ITER_MEDIUM_STATUS_OK
:
164 case CTF_MSG_ITER_MEDIUM_STATUS_EOF
:
167 BT_COMP_LOGE("Cannot memory-map next region of file \"%s\" (%p)",
168 ds_file
->file
->path
->str
,
174 *buffer_sz
= MIN(remaining_mmap_bytes(ds_file
), request_sz
);
175 BT_ASSERT(ds_file
->mmap_addr
);
176 *buffer_addr
= ((uint8_t *) ds_file
->mmap_addr
) + ds_file
->request_offset
;
177 ds_file
->request_offset
+= *buffer_sz
;
181 status
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
188 bt_stream
*medop_borrow_stream(bt_stream_class
*stream_class
, int64_t stream_id
,
191 struct ctf_fs_ds_file
*ds_file
= data
;
192 bt_stream_class
*ds_file_stream_class
;
193 bt_stream
*stream
= NULL
;
195 ds_file_stream_class
= bt_stream_borrow_class(
198 if (stream_class
!= ds_file_stream_class
) {
200 * Not supported: two packets described by two different
201 * stream classes within the same data stream file.
206 stream
= ds_file
->stream
;
213 enum ctf_msg_iter_medium_status
medop_seek(enum ctf_msg_iter_seek_whence whence
,
214 off_t offset
, void *data
)
216 enum ctf_msg_iter_medium_status ret
=
217 CTF_MSG_ITER_MEDIUM_STATUS_OK
;
218 struct ctf_fs_ds_file
*ds_file
= data
;
219 off_t offset_in_mapping
, file_size
= ds_file
->file
->size
;
220 bt_self_component
*self_comp
= ds_file
->self_comp
;
221 bt_logging_level log_level
= ds_file
->log_level
;
223 if (whence
!= CTF_MSG_ITER_SEEK_WHENCE_SET
||
224 offset
< 0 || offset
> file_size
) {
225 BT_COMP_LOGE("Invalid medium seek request: whence=%d, offset=%jd, "
226 "file-size=%jd", (int) whence
, (intmax_t) offset
,
227 (intmax_t) file_size
);
228 ret
= CTF_MSG_ITER_MEDIUM_STATUS_INVAL
;
232 /* If there is no current mapping, map the right file directly. */
233 if (!ds_file
->mmap_addr
) {
234 goto map_requested_offset
;
238 * Determine whether or not the destination is contained within the
241 if (offset
< ds_file
->mmap_offset
||
242 offset
>= ds_file
->mmap_offset
+ ds_file
->mmap_len
) {
244 BT_COMP_LOGD("Medium seek request cannot be accomodated by the current "
245 "file mapping: offset=%jd, mmap-offset=%jd, "
246 "mmap-len=%zu", (intmax_t) offset
, (intmax_t) ds_file
->mmap_offset
,
248 unmap_ret
= ds_file_munmap(ds_file
);
250 ret
= CTF_MSG_ITER_MEDIUM_STATUS_ERROR
;
253 goto map_requested_offset
;
255 ds_file
->request_offset
= offset
- ds_file
->mmap_offset
;
259 map_requested_offset
:
260 offset_in_mapping
= offset
%
261 bt_mmap_get_offset_align_size(ds_file
->log_level
);
263 ds_file
->mmap_offset
= offset
- offset_in_mapping
;
264 ds_file
->request_offset
= offset_in_mapping
;
265 ret
= ds_file_mmap_next(ds_file
);
266 if (ret
!= CTF_MSG_ITER_MEDIUM_STATUS_OK
) {
271 ds_file
->end_reached
= (offset
== file_size
);
277 struct ctf_msg_iter_medium_ops ctf_fs_ds_file_medops
= {
278 .request_bytes
= medop_request_bytes
,
279 .borrow_stream
= medop_borrow_stream
,
284 int convert_cycles_to_ns(struct ctf_clock_class
*clock_class
,
285 uint64_t cycles
, int64_t *ns
)
287 return bt_util_clock_cycles_to_ns_from_origin(cycles
,
288 clock_class
->frequency
, clock_class
->offset_seconds
,
289 clock_class
->offset_cycles
, ns
);
293 struct ctf_fs_ds_index
*build_index_from_idx_file(
294 struct ctf_fs_ds_file
*ds_file
,
295 struct ctf_fs_ds_file_info
*file_info
)
298 gchar
*directory
= NULL
;
299 gchar
*basename
= NULL
;
300 GString
*index_basename
= NULL
;
301 gchar
*index_file_path
= NULL
;
302 GMappedFile
*mapped_file
= NULL
;
304 const char *mmap_begin
= NULL
, *file_pos
= NULL
;
305 const struct ctf_packet_index_file_hdr
*header
= NULL
;
306 struct ctf_fs_ds_index
*index
= NULL
;
307 struct ctf_fs_ds_index_entry
*index_entry
= NULL
, *prev_index_entry
= NULL
;
308 uint64_t total_packets_size
= 0;
309 size_t file_index_entry_size
;
310 size_t file_entry_count
;
312 struct ctf_stream_class
*sc
;
313 struct ctf_msg_iter_packet_properties props
;
314 uint32_t version_major
, version_minor
;
315 bt_self_component
*self_comp
= ds_file
->self_comp
;
316 bt_logging_level log_level
= ds_file
->log_level
;
318 BT_COMP_LOGI("Building index from .idx file of stream file %s",
319 ds_file
->file
->path
->str
);
320 ret
= ctf_msg_iter_get_packet_properties(ds_file
->msg_iter
, &props
);
322 BT_COMP_LOGI_STR("Cannot read first packet's header and context fields.");
326 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
,
327 props
.stream_class_id
);
329 if (!sc
->default_clock_class
) {
330 BT_COMP_LOGI_STR("Cannot find stream class's default clock class.");
334 /* Look for index file in relative path index/name.idx. */
335 basename
= g_path_get_basename(ds_file
->file
->path
->str
);
337 BT_COMP_LOGE("Cannot get the basename of datastream file %s",
338 ds_file
->file
->path
->str
);
342 directory
= g_path_get_dirname(ds_file
->file
->path
->str
);
344 BT_COMP_LOGE("Cannot get dirname of datastream file %s",
345 ds_file
->file
->path
->str
);
349 index_basename
= g_string_new(basename
);
350 if (!index_basename
) {
351 BT_COMP_LOGE_STR("Cannot allocate index file basename string");
355 g_string_append(index_basename
, ".idx");
356 index_file_path
= g_build_filename(directory
, "index",
357 index_basename
->str
, NULL
);
358 mapped_file
= g_mapped_file_new(index_file_path
, FALSE
, NULL
);
360 BT_COMP_LOGD("Cannot create new mapped file %s",
366 * The g_mapped_file API limits us to 4GB files on 32-bit.
367 * Traces with such large indexes have never been seen in the wild,
368 * but this would need to be adjusted to support them.
370 filesize
= g_mapped_file_get_length(mapped_file
);
371 if (filesize
< sizeof(*header
)) {
372 BT_COMP_LOGW("Invalid LTTng trace index file: "
373 "file size (%zu bytes) < header size (%zu bytes)",
374 filesize
, sizeof(*header
));
378 mmap_begin
= g_mapped_file_get_contents(mapped_file
);
379 header
= (struct ctf_packet_index_file_hdr
*) mmap_begin
;
381 file_pos
= g_mapped_file_get_contents(mapped_file
) + sizeof(*header
);
382 if (be32toh(header
->magic
) != CTF_INDEX_MAGIC
) {
383 BT_COMP_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
387 version_major
= be32toh(header
->index_major
);
388 version_minor
= be32toh(header
->index_minor
);
389 if (version_major
!= 1) {
391 "Unknown LTTng trace index version: "
392 "major=%" PRIu32
", minor=%" PRIu32
,
393 version_major
, version_minor
);
397 file_index_entry_size
= be32toh(header
->packet_index_len
);
398 file_entry_count
= (filesize
- sizeof(*header
)) / file_index_entry_size
;
399 if ((filesize
- sizeof(*header
)) % file_index_entry_size
) {
400 BT_COMP_LOGW("Invalid LTTng trace index: the index's size after the header "
401 "(%zu bytes) is not a multiple of the index entry size "
402 "(%zu bytes)", (filesize
- sizeof(*header
)),
407 index
= ctf_fs_ds_index_create(ds_file
->log_level
, ds_file
->self_comp
);
412 for (i
= 0; i
< file_entry_count
; i
++) {
413 struct ctf_packet_index
*file_index
=
414 (struct ctf_packet_index
*) file_pos
;
415 uint64_t packet_size
= be64toh(file_index
->packet_size
);
417 if (packet_size
% CHAR_BIT
) {
418 BT_COMP_LOGW("Invalid packet size encountered in LTTng trace index file");
422 index_entry
= g_new0(struct ctf_fs_ds_index_entry
, 1);
427 /* Set path to stream file. */
428 index_entry
->path
= file_info
->path
->str
;
430 /* Convert size in bits to bytes. */
431 packet_size
/= CHAR_BIT
;
432 index_entry
->packet_size
= packet_size
;
434 index_entry
->offset
= be64toh(file_index
->offset
);
435 if (i
!= 0 && index_entry
->offset
< prev_index_entry
->offset
) {
436 BT_COMP_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
437 "previous offset=%" PRIu64
", current offset=%" PRIu64
,
438 prev_index_entry
->offset
, index_entry
->offset
);
442 index_entry
->timestamp_begin
= be64toh(file_index
->timestamp_begin
);
443 index_entry
->timestamp_end
= be64toh(file_index
->timestamp_end
);
444 if (index_entry
->timestamp_end
< index_entry
->timestamp_begin
) {
445 BT_COMP_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
446 "timestamp_begin=%" PRIu64
"timestamp_end=%" PRIu64
,
447 index_entry
->timestamp_begin
,
448 index_entry
->timestamp_end
);
452 /* Convert the packet's bound to nanoseconds since Epoch. */
453 ret
= convert_cycles_to_ns(sc
->default_clock_class
,
454 index_entry
->timestamp_begin
,
455 &index_entry
->timestamp_begin_ns
);
457 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
460 ret
= convert_cycles_to_ns(sc
->default_clock_class
,
461 index_entry
->timestamp_end
,
462 &index_entry
->timestamp_end_ns
);
464 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
468 total_packets_size
+= packet_size
;
469 file_pos
+= file_index_entry_size
;
471 prev_index_entry
= index_entry
;
473 /* Give ownership of `index_entry` to `index->entries`. */
474 g_ptr_array_add(index
->entries
, index_entry
);
478 /* Validate that the index addresses the complete stream. */
479 if (ds_file
->file
->size
!= total_packets_size
) {
480 BT_COMP_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
481 "file-size=%" PRIu64
", total-packets-size=%" PRIu64
,
482 ds_file
->file
->size
, total_packets_size
);
488 g_free(index_file_path
);
489 if (index_basename
) {
490 g_string_free(index_basename
, TRUE
);
493 g_mapped_file_unref(mapped_file
);
497 ctf_fs_ds_index_destroy(index
);
504 int init_index_entry(struct ctf_fs_ds_index_entry
*entry
,
505 struct ctf_fs_ds_file
*ds_file
,
506 struct ctf_msg_iter_packet_properties
*props
,
507 off_t packet_size
, off_t packet_offset
)
510 struct ctf_stream_class
*sc
;
511 bt_self_component
*self_comp
= ds_file
->self_comp
;
512 bt_logging_level log_level
= ds_file
->log_level
;
514 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
,
515 props
->stream_class_id
);
517 BT_ASSERT(packet_offset
>= 0);
518 entry
->offset
= packet_offset
;
519 BT_ASSERT(packet_size
>= 0);
520 entry
->packet_size
= packet_size
;
522 if (props
->snapshots
.beginning_clock
!= UINT64_C(-1)) {
523 entry
->timestamp_begin
= props
->snapshots
.beginning_clock
;
525 /* Convert the packet's bound to nanoseconds since Epoch. */
526 ret
= convert_cycles_to_ns(sc
->default_clock_class
,
527 props
->snapshots
.beginning_clock
,
528 &entry
->timestamp_begin_ns
);
530 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
534 entry
->timestamp_begin
= UINT64_C(-1);
535 entry
->timestamp_begin_ns
= UINT64_C(-1);
538 if (props
->snapshots
.end_clock
!= UINT64_C(-1)) {
539 entry
->timestamp_end
= props
->snapshots
.end_clock
;
541 /* Convert the packet's bound to nanoseconds since Epoch. */
542 ret
= convert_cycles_to_ns(sc
->default_clock_class
,
543 props
->snapshots
.end_clock
,
544 &entry
->timestamp_end_ns
);
546 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
550 entry
->timestamp_end
= UINT64_C(-1);
551 entry
->timestamp_end_ns
= UINT64_C(-1);
559 struct ctf_fs_ds_index
*build_index_from_stream_file(
560 struct ctf_fs_ds_file
*ds_file
,
561 struct ctf_fs_ds_file_info
*file_info
)
564 struct ctf_fs_ds_index
*index
= NULL
;
565 enum ctf_msg_iter_status iter_status
= CTF_MSG_ITER_STATUS_OK
;
566 off_t current_packet_offset_bytes
= 0;
567 bt_self_component
*self_comp
= ds_file
->self_comp
;
568 bt_logging_level log_level
= ds_file
->log_level
;
570 BT_COMP_LOGI("Indexing stream file %s", ds_file
->file
->path
->str
);
572 index
= ctf_fs_ds_index_create(ds_file
->log_level
, ds_file
->self_comp
);
578 off_t current_packet_size_bytes
;
579 struct ctf_fs_ds_index_entry
*index_entry
;
580 struct ctf_msg_iter_packet_properties props
;
582 if (current_packet_offset_bytes
< 0) {
583 BT_COMP_LOGE_STR("Cannot get the current packet's offset.");
585 } else if (current_packet_offset_bytes
> ds_file
->file
->size
) {
586 BT_COMP_LOGE_STR("Unexpected current packet's offset (larger than file).");
588 } else if (current_packet_offset_bytes
== ds_file
->file
->size
) {
593 iter_status
= ctf_msg_iter_seek(ds_file
->msg_iter
,
594 current_packet_offset_bytes
);
595 if (iter_status
!= CTF_MSG_ITER_STATUS_OK
) {
599 iter_status
= ctf_msg_iter_get_packet_properties(
600 ds_file
->msg_iter
, &props
);
601 if (iter_status
!= CTF_MSG_ITER_STATUS_OK
) {
605 if (props
.exp_packet_total_size
>= 0) {
606 current_packet_size_bytes
=
607 (uint64_t) props
.exp_packet_total_size
/ 8;
609 current_packet_size_bytes
= ds_file
->file
->size
;
612 if (current_packet_offset_bytes
+ current_packet_size_bytes
>
613 ds_file
->file
->size
) {
614 BT_COMP_LOGW("Invalid packet size reported in file: stream=\"%s\", "
615 "packet-offset=%jd, packet-size-bytes=%jd, "
617 ds_file
->file
->path
->str
,
618 (intmax_t) current_packet_offset_bytes
,
619 (intmax_t) current_packet_size_bytes
,
620 (intmax_t) ds_file
->file
->size
);
624 index_entry
= g_new0(struct ctf_fs_ds_index_entry
, 1);
626 BT_COMP_LOGE_STR("Failed to allocate a new index entry.");
630 /* Set path to stream file. */
631 index_entry
->path
= file_info
->path
->str
;
633 ret
= init_index_entry(index_entry
, ds_file
, &props
,
634 current_packet_size_bytes
, current_packet_offset_bytes
);
640 g_ptr_array_add(index
->entries
, index_entry
);
642 current_packet_offset_bytes
+= current_packet_size_bytes
;
643 BT_COMP_LOGD("Seeking to next packet: current-packet-offset=%jd, "
644 "next-packet-offset=%jd",
645 (intmax_t) (current_packet_offset_bytes
- current_packet_size_bytes
),
646 (intmax_t) current_packet_offset_bytes
);
653 ctf_fs_ds_index_destroy(index
);
659 struct ctf_fs_ds_file
*ctf_fs_ds_file_create(
660 struct ctf_fs_trace
*ctf_fs_trace
,
661 bt_self_message_iterator
*self_msg_iter
,
662 struct ctf_msg_iter
*msg_iter
,
663 bt_stream
*stream
, const char *path
,
664 bt_logging_level log_level
)
667 const size_t offset_align
= bt_mmap_get_offset_align_size(log_level
);
668 struct ctf_fs_ds_file
*ds_file
= g_new0(struct ctf_fs_ds_file
, 1);
674 ds_file
->log_level
= log_level
;
675 ds_file
->self_comp
= ctf_fs_trace
->self_comp
;
676 ds_file
->self_msg_iter
= self_msg_iter
;
677 ds_file
->file
= ctf_fs_file_create(log_level
, ds_file
->self_comp
);
678 if (!ds_file
->file
) {
682 ds_file
->stream
= stream
;
683 bt_stream_get_ref(ds_file
->stream
);
684 ds_file
->metadata
= ctf_fs_trace
->metadata
;
685 g_string_assign(ds_file
->file
->path
, path
);
686 ret
= ctf_fs_file_open(ds_file
->file
, "rb");
691 ds_file
->msg_iter
= msg_iter
;
692 ctf_msg_iter_set_medops_data(ds_file
->msg_iter
, ds_file
);
693 if (!ds_file
->msg_iter
) {
697 ds_file
->mmap_max_len
= offset_align
* 2048;
702 /* Do not touch "borrowed" file. */
703 ctf_fs_ds_file_destroy(ds_file
);
711 struct ctf_fs_ds_index
*ctf_fs_ds_file_build_index(
712 struct ctf_fs_ds_file
*ds_file
,
713 struct ctf_fs_ds_file_info
*file_info
)
715 struct ctf_fs_ds_index
*index
;
716 bt_self_component
*self_comp
= ds_file
->self_comp
;
717 bt_logging_level log_level
= ds_file
->log_level
;
719 index
= build_index_from_idx_file(ds_file
, file_info
);
724 BT_COMP_LOGI("Failed to build index from .index file; "
725 "falling back to stream indexing.");
726 index
= build_index_from_stream_file(ds_file
, file_info
);
732 struct ctf_fs_ds_index
*ctf_fs_ds_index_create(bt_logging_level log_level
,
733 bt_self_component
*self_comp
)
735 struct ctf_fs_ds_index
*index
= g_new0(struct ctf_fs_ds_index
, 1);
738 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
739 "Failed to allocate index");
743 index
->entries
= g_ptr_array_new_with_free_func((GDestroyNotify
) g_free
);
744 if (!index
->entries
) {
745 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR
, log_level
, self_comp
,
746 "Failed to allocate index entries.");
753 ctf_fs_ds_index_destroy(index
);
760 void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file
*ds_file
)
766 bt_stream_put_ref(ds_file
->stream
);
767 (void) ds_file_munmap(ds_file
);
770 ctf_fs_file_destroy(ds_file
->file
);
777 bt_component_class_message_iterator_next_method_status
ctf_fs_ds_file_next(
778 struct ctf_fs_ds_file
*ds_file
,
781 enum ctf_msg_iter_status msg_iter_status
;
782 bt_component_class_message_iterator_next_method_status status
;
784 msg_iter_status
= ctf_msg_iter_get_next_message(
785 ds_file
->msg_iter
, msg
);
787 switch (msg_iter_status
) {
788 case CTF_MSG_ITER_STATUS_EOF
:
789 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_END
;
791 case CTF_MSG_ITER_STATUS_OK
:
792 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK
;
794 case CTF_MSG_ITER_STATUS_AGAIN
:
796 * Should not make it this far as this is
797 * medium-specific; there is nothing for the user to do
798 * and it should have been handled upstream.
801 case CTF_MSG_ITER_STATUS_INVAL
:
802 case CTF_MSG_ITER_STATUS_ERROR
:
804 status
= BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR
;
811 void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index
*index
)
817 if (index
->entries
) {
818 g_ptr_array_free(index
->entries
, TRUE
);