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
32 #include <babeltrace/compat/mman-internal.h>
33 #include <babeltrace/endian-internal.h>
34 #include <babeltrace/babeltrace.h>
35 #include <babeltrace/common-internal.h>
38 #include "../common/notif-iter/notif-iter.h"
39 #include <babeltrace/assert-internal.h>
40 #include "data-stream-file.h"
43 #define BT_LOG_TAG "PLUGIN-CTF-FS-SRC-DS"
47 size_t remaining_mmap_bytes(struct ctf_fs_ds_file
*ds_file
)
49 return ds_file
->mmap_len
- ds_file
->request_offset
;
53 int ds_file_munmap(struct ctf_fs_ds_file
*ds_file
)
57 if (!ds_file
|| !ds_file
->mmap_addr
) {
61 if (bt_munmap(ds_file
->mmap_addr
, ds_file
->mmap_len
)) {
62 BT_LOGE_ERRNO("Cannot memory-unmap file",
63 ": address=%p, size=%zu, file_path=\"%s\", file=%p",
64 ds_file
->mmap_addr
, ds_file
->mmap_len
,
65 ds_file
->file
? ds_file
->file
->path
->str
: "NULL",
66 ds_file
->file
? ds_file
->file
->fp
: NULL
);
71 ds_file
->mmap_addr
= NULL
;
78 enum bt_notif_iter_medium_status
ds_file_mmap_next(
79 struct ctf_fs_ds_file
*ds_file
)
81 enum bt_notif_iter_medium_status ret
=
82 BT_NOTIF_ITER_MEDIUM_STATUS_OK
;
84 /* Unmap old region */
85 if (ds_file
->mmap_addr
) {
86 if (ds_file_munmap(ds_file
)) {
91 * mmap_len is guaranteed to be page-aligned except on the
92 * last mapping where it may not be possible (since the file's
93 * size itself may not be a page multiple).
95 ds_file
->mmap_offset
+= ds_file
->mmap_len
;
96 ds_file
->request_offset
= 0;
99 ds_file
->mmap_len
= MIN(ds_file
->file
->size
- ds_file
->mmap_offset
,
100 ds_file
->mmap_max_len
);
101 if (ds_file
->mmap_len
== 0) {
102 ret
= BT_NOTIF_ITER_MEDIUM_STATUS_EOF
;
106 BT_ASSERT(ds_file
->mmap_len
);
107 ds_file
->mmap_addr
= bt_mmap((void *) 0, ds_file
->mmap_len
,
108 PROT_READ
, MAP_PRIVATE
, fileno(ds_file
->file
->fp
),
109 ds_file
->mmap_offset
);
110 if (ds_file
->mmap_addr
== MAP_FAILED
) {
111 BT_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s",
112 ds_file
->mmap_len
, ds_file
->file
->path
->str
,
113 ds_file
->file
->fp
, (intmax_t) ds_file
->mmap_offset
,
120 ds_file_munmap(ds_file
);
121 ret
= BT_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
127 enum bt_notif_iter_medium_status
medop_request_bytes(
128 size_t request_sz
, uint8_t **buffer_addr
,
129 size_t *buffer_sz
, void *data
)
131 enum bt_notif_iter_medium_status status
=
132 BT_NOTIF_ITER_MEDIUM_STATUS_OK
;
133 struct ctf_fs_ds_file
*ds_file
= data
;
135 if (request_sz
== 0) {
139 /* Check if we have at least one memory-mapped byte left */
140 if (remaining_mmap_bytes(ds_file
) == 0) {
141 /* Are we at the end of the file? */
142 if (ds_file
->mmap_offset
>= ds_file
->file
->size
) {
143 BT_LOGD("Reached end of file \"%s\" (%p)",
144 ds_file
->file
->path
->str
, ds_file
->file
->fp
);
145 status
= BT_NOTIF_ITER_MEDIUM_STATUS_EOF
;
149 status
= ds_file_mmap_next(ds_file
);
151 case BT_NOTIF_ITER_MEDIUM_STATUS_OK
:
153 case BT_NOTIF_ITER_MEDIUM_STATUS_EOF
:
156 BT_LOGE("Cannot memory-map next region of file \"%s\" (%p)",
157 ds_file
->file
->path
->str
,
163 *buffer_sz
= MIN(remaining_mmap_bytes(ds_file
), request_sz
);
164 *buffer_addr
= ((uint8_t *) ds_file
->mmap_addr
) + ds_file
->request_offset
;
165 ds_file
->request_offset
+= *buffer_sz
;
169 status
= BT_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
176 struct bt_stream
*medop_borrow_stream(
177 struct bt_stream_class
*stream_class
, uint64_t stream_id
,
180 struct ctf_fs_ds_file
*ds_file
= data
;
181 struct bt_stream_class
*ds_file_stream_class
;
182 struct bt_stream
*stream
= NULL
;
184 ds_file_stream_class
= bt_stream_borrow_class(ds_file
->stream
);
185 if (stream_class
!= ds_file_stream_class
) {
187 * Not supported: two packets described by two different
188 * stream classes within the same data stream file.
193 stream
= ds_file
->stream
;
200 enum bt_notif_iter_medium_status
medop_seek(
201 enum bt_notif_iter_seek_whence whence
, off_t offset
,
204 enum bt_notif_iter_medium_status ret
=
205 BT_NOTIF_ITER_MEDIUM_STATUS_OK
;
206 struct ctf_fs_ds_file
*ds_file
= data
;
207 off_t file_size
= ds_file
->file
->size
;
209 if (whence
!= BT_NOTIF_ITER_SEEK_WHENCE_SET
||
210 offset
< 0 || offset
> file_size
) {
211 BT_LOGE("Invalid medium seek request: whence=%d, offset=%jd, "
212 "file-size=%jd", (int) whence
, offset
,
214 ret
= BT_NOTIF_ITER_MEDIUM_STATUS_INVAL
;
219 * Determine whether or not the destination is contained within the
222 if (ds_file
->mmap_addr
&& (offset
< ds_file
->mmap_offset
||
223 offset
>= ds_file
->mmap_offset
+ ds_file
->mmap_len
)) {
225 off_t offset_in_mapping
= offset
% bt_common_get_page_size();
227 BT_LOGD("Medium seek request cannot be accomodated by the current "
228 "file mapping: offset=%jd, mmap-offset=%jd, "
229 "mmap-len=%zu", offset
, ds_file
->mmap_offset
,
231 unmap_ret
= ds_file_munmap(ds_file
);
233 ret
= BT_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
237 ds_file
->mmap_offset
= offset
- offset_in_mapping
;
238 ds_file
->request_offset
= offset_in_mapping
;
239 ret
= ds_file_mmap_next(ds_file
);
240 if (ret
!= BT_NOTIF_ITER_MEDIUM_STATUS_OK
) {
244 ds_file
->request_offset
= offset
- ds_file
->mmap_offset
;
247 ds_file
->end_reached
= (offset
== file_size
);
253 struct bt_notif_iter_medium_ops ctf_fs_ds_file_medops
= {
254 .request_bytes
= medop_request_bytes
,
255 .borrow_stream
= medop_borrow_stream
,
260 struct ctf_fs_ds_index
*ctf_fs_ds_index_create(size_t length
)
262 struct ctf_fs_ds_index
*index
= g_new0(struct ctf_fs_ds_index
, 1);
265 BT_LOGE_STR("Failed to allocate index");
269 index
->entries
= g_array_sized_new(FALSE
, TRUE
,
270 sizeof(struct ctf_fs_ds_index_entry
), length
);
271 if (!index
->entries
) {
272 BT_LOGE("Failed to allocate %zu index entries.", length
);
275 g_array_set_size(index
->entries
, length
);
279 ctf_fs_ds_index_destroy(index
);
283 /* Returns a new, zeroed, index entry. */
285 struct ctf_fs_ds_index_entry
*ctf_fs_ds_index_add_new_entry(
286 struct ctf_fs_ds_index
*index
)
288 g_array_set_size(index
->entries
, index
->entries
->len
+ 1);
289 return &g_array_index(index
->entries
, struct ctf_fs_ds_index_entry
,
290 index
->entries
->len
- 1);
294 struct bt_clock_class
*borrow_field_mapped_clock_class(
295 struct bt_field
*field
)
297 struct bt_field_type
*field_type
;
298 struct bt_clock_class
*clock_class
= NULL
;
300 field_type
= bt_field_borrow_type(field
);
305 clock_class
= bt_field_type_integer_borrow_mapped_clock_class(
316 int borrow_packet_bounds_from_packet_context(
317 struct bt_field
*packet_context
,
318 struct bt_clock_class
**_timestamp_begin_cc
,
319 struct bt_field
**_timestamp_begin
,
320 struct bt_clock_class
**_timestamp_end_cc
,
321 struct bt_field
**_timestamp_end
)
324 struct bt_clock_class
*timestamp_begin_cc
= NULL
;
325 struct bt_clock_class
*timestamp_end_cc
= NULL
;
326 struct bt_field
*timestamp_begin
= NULL
;
327 struct bt_field
*timestamp_end
= NULL
;
329 timestamp_begin
= bt_field_structure_borrow_field_by_name(
330 packet_context
, "timestamp_begin");
331 if (!timestamp_begin
) {
332 BT_LOGD_STR("Cannot retrieve timestamp_begin field in packet context.");
337 timestamp_begin_cc
= borrow_field_mapped_clock_class(timestamp_begin
);
338 if (!timestamp_begin_cc
) {
339 BT_LOGD_STR("Cannot retrieve the clock mapped to timestamp_begin.");
342 timestamp_end
= bt_field_structure_borrow_field_by_name(
343 packet_context
, "timestamp_end");
344 if (!timestamp_end
) {
345 BT_LOGD_STR("Cannot retrieve timestamp_end field in packet context.");
350 timestamp_end_cc
= borrow_field_mapped_clock_class(timestamp_end
);
351 if (!timestamp_end_cc
) {
352 BT_LOGD_STR("Cannot retrieve the clock mapped to timestamp_end.");
355 if (_timestamp_begin_cc
) {
356 *_timestamp_begin_cc
= timestamp_begin_cc
;
358 if (_timestamp_begin
) {
359 *_timestamp_begin
= timestamp_begin
;
361 if (_timestamp_end_cc
) {
362 *_timestamp_end_cc
= timestamp_end_cc
;
364 if (_timestamp_end
) {
365 *_timestamp_end
= timestamp_end
;
372 int borrow_ds_file_packet_bounds_clock_classes(struct ctf_fs_ds_file
*ds_file
,
373 struct bt_clock_class
**timestamp_begin_cc
,
374 struct bt_clock_class
**timestamp_end_cc
)
376 struct bt_field
*packet_context
= NULL
;
377 int ret
= ctf_fs_ds_file_borrow_packet_header_context_fields(ds_file
,
378 NULL
, &packet_context
);
380 if (ret
|| !packet_context
) {
381 BT_LOGD("Cannot retrieve packet context field: ds-file-path=\"%s\"",
382 ds_file
->file
->path
->str
);
387 ret
= borrow_packet_bounds_from_packet_context(packet_context
,
388 timestamp_begin_cc
, NULL
,
389 timestamp_end_cc
, NULL
);
396 int convert_cycles_to_ns(struct bt_clock_class
*clock_class
,
397 uint64_t cycles
, int64_t *ns
)
399 return bt_clock_class_cycles_to_ns(clock_class
, cycles
, ns
);
403 struct ctf_fs_ds_index
*build_index_from_idx_file(
404 struct ctf_fs_ds_file
*ds_file
)
407 gchar
*directory
= NULL
;
408 gchar
*basename
= NULL
;
409 GString
*index_basename
= NULL
;
410 gchar
*index_file_path
= NULL
;
411 GMappedFile
*mapped_file
= NULL
;
413 const char *mmap_begin
= NULL
, *file_pos
= NULL
;
414 const struct ctf_packet_index_file_hdr
*header
= NULL
;
415 struct ctf_fs_ds_index
*index
= NULL
;
416 struct ctf_fs_ds_index_entry
*index_entry
= NULL
;
417 uint64_t total_packets_size
= 0;
418 size_t file_index_entry_size
;
419 size_t file_entry_count
;
421 struct bt_clock_class
*timestamp_begin_cc
= NULL
;
422 struct bt_clock_class
*timestamp_end_cc
= NULL
;
424 BT_LOGD("Building index from .idx file of stream file %s",
425 ds_file
->file
->path
->str
);
427 ret
= borrow_ds_file_packet_bounds_clock_classes(ds_file
,
428 ×tamp_begin_cc
, ×tamp_end_cc
);
430 BT_LOGD_STR("Cannot get clock classes of \"timestamp_begin\" "
431 "and \"timestamp_end\" fields");
435 /* Look for index file in relative path index/name.idx. */
436 basename
= g_path_get_basename(ds_file
->file
->path
->str
);
438 BT_LOGE("Cannot get the basename of datastream file %s",
439 ds_file
->file
->path
->str
);
443 directory
= g_path_get_dirname(ds_file
->file
->path
->str
);
445 BT_LOGE("Cannot get dirname of datastream file %s",
446 ds_file
->file
->path
->str
);
450 index_basename
= g_string_new(basename
);
451 if (!index_basename
) {
452 BT_LOGE_STR("Cannot allocate index file basename string");
456 g_string_append(index_basename
, ".idx");
457 index_file_path
= g_build_filename(directory
, "index",
458 index_basename
->str
, NULL
);
459 mapped_file
= g_mapped_file_new(index_file_path
, FALSE
, NULL
);
461 BT_LOGD("Cannot create new mapped file %s",
467 * The g_mapped_file API limits us to 4GB files on 32-bit.
468 * Traces with such large indexes have never been seen in the wild,
469 * but this would need to be adjusted to support them.
471 filesize
= g_mapped_file_get_length(mapped_file
);
472 if (filesize
< sizeof(*header
)) {
473 BT_LOGW("Invalid LTTng trace index file: "
474 "file size (%zu bytes) < header size (%zu bytes)",
475 filesize
, sizeof(*header
));
479 mmap_begin
= g_mapped_file_get_contents(mapped_file
);
480 header
= (struct ctf_packet_index_file_hdr
*) mmap_begin
;
482 file_pos
= g_mapped_file_get_contents(mapped_file
) + sizeof(*header
);
483 if (be32toh(header
->magic
) != CTF_INDEX_MAGIC
) {
484 BT_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
488 file_index_entry_size
= be32toh(header
->packet_index_len
);
489 file_entry_count
= (filesize
- sizeof(*header
)) / file_index_entry_size
;
490 if ((filesize
- sizeof(*header
)) % file_index_entry_size
) {
491 BT_LOGW("Invalid LTTng trace index: the index's size after the header "
492 "(%zu bytes) is not a multiple of the index entry size "
493 "(%zu bytes)", (filesize
- sizeof(*header
)),
498 index
= ctf_fs_ds_index_create(file_entry_count
);
503 index_entry
= (struct ctf_fs_ds_index_entry
*) &g_array_index(
504 index
->entries
, struct ctf_fs_ds_index_entry
, 0);
505 for (i
= 0; i
< file_entry_count
; i
++) {
506 struct ctf_packet_index
*file_index
=
507 (struct ctf_packet_index
*) file_pos
;
508 uint64_t packet_size
= be64toh(file_index
->packet_size
);
510 if (packet_size
% CHAR_BIT
) {
511 BT_LOGW("Invalid packet size encountered in LTTng trace index file");
515 /* Convert size in bits to bytes. */
516 packet_size
/= CHAR_BIT
;
517 index_entry
->packet_size
= packet_size
;
519 index_entry
->offset
= be64toh(file_index
->offset
);
520 if (i
!= 0 && index_entry
->offset
< (index_entry
- 1)->offset
) {
521 BT_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
522 "previous offset=%" PRIu64
", current offset=%" PRIu64
,
523 (index_entry
- 1)->offset
, index_entry
->offset
);
527 index_entry
->timestamp_begin
= be64toh(file_index
->timestamp_begin
);
528 index_entry
->timestamp_end
= be64toh(file_index
->timestamp_end
);
529 if (index_entry
->timestamp_end
< index_entry
->timestamp_begin
) {
530 BT_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
531 "timestamp_begin=%" PRIu64
"timestamp_end=%" PRIu64
,
532 index_entry
->timestamp_begin
,
533 index_entry
->timestamp_end
);
537 /* Convert the packet's bound to nanoseconds since Epoch. */
538 ret
= convert_cycles_to_ns(timestamp_begin_cc
,
539 index_entry
->timestamp_begin
,
540 &index_entry
->timestamp_begin_ns
);
542 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
545 ret
= convert_cycles_to_ns(timestamp_end_cc
,
546 index_entry
->timestamp_end
,
547 &index_entry
->timestamp_end_ns
);
549 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
553 total_packets_size
+= packet_size
;
554 file_pos
+= file_index_entry_size
;
558 /* Validate that the index addresses the complete stream. */
559 if (ds_file
->file
->size
!= total_packets_size
) {
560 BT_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
561 "file-size=%" PRIu64
", total-packets-size=%" PRIu64
,
562 ds_file
->file
->size
, total_packets_size
);
568 g_free(index_file_path
);
569 if (index_basename
) {
570 g_string_free(index_basename
, TRUE
);
573 g_mapped_file_unref(mapped_file
);
577 ctf_fs_ds_index_destroy(index
);
583 int init_index_entry(struct ctf_fs_ds_index_entry
*entry
,
584 struct bt_field
*packet_context
, off_t packet_size
,
588 struct bt_field
*timestamp_begin
= NULL
;
589 struct bt_field
*timestamp_end
= NULL
;
590 struct bt_clock_class
*timestamp_begin_cc
= NULL
;
591 struct bt_clock_class
*timestamp_end_cc
= NULL
;
593 ret
= borrow_packet_bounds_from_packet_context(packet_context
,
594 ×tamp_begin_cc
, ×tamp_begin
,
595 ×tamp_end_cc
, ×tamp_end
);
596 if (ret
|| !timestamp_begin_cc
|| !timestamp_begin
||
597 !timestamp_end_cc
|| ! timestamp_end
) {
598 BT_LOGD_STR("Failed to determine time bound fields of packet.");
602 BT_ASSERT(packet_offset
>= 0);
603 entry
->offset
= packet_offset
;
605 BT_ASSERT(packet_size
>= 0);
606 entry
->packet_size
= packet_size
;
608 ret
= bt_field_integer_unsigned_get_value(timestamp_begin
,
609 &entry
->timestamp_begin
);
613 ret
= bt_field_integer_unsigned_get_value(timestamp_end
,
614 &entry
->timestamp_end
);
619 /* Convert the packet's bound to nanoseconds since Epoch. */
620 ret
= convert_cycles_to_ns(timestamp_begin_cc
,
621 entry
->timestamp_begin
,
622 &entry
->timestamp_begin_ns
);
624 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
628 ret
= convert_cycles_to_ns(timestamp_end_cc
,
629 entry
->timestamp_end
,
630 &entry
->timestamp_end_ns
);
632 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
641 struct ctf_fs_ds_index
*build_index_from_stream_file(
642 struct ctf_fs_ds_file
*ds_file
)
645 struct ctf_fs_ds_index
*index
= NULL
;
646 enum bt_notif_iter_status iter_status
;
648 BT_LOGD("Indexing stream file %s", ds_file
->file
->path
->str
);
650 index
= ctf_fs_ds_index_create(0);
656 off_t current_packet_offset
;
657 off_t next_packet_offset
;
658 off_t current_packet_size
, current_packet_size_bytes
;
659 struct ctf_fs_ds_index_entry
*entry
;
660 struct bt_field
*packet_context
= NULL
;
662 iter_status
= bt_notif_iter_borrow_packet_header_context_fields(
663 ds_file
->notif_iter
, NULL
, &packet_context
);
664 if (iter_status
!= BT_NOTIF_ITER_STATUS_OK
) {
665 if (iter_status
== BT_NOTIF_ITER_STATUS_EOF
) {
671 current_packet_offset
=
672 bt_notif_iter_get_current_packet_offset(
673 ds_file
->notif_iter
);
674 if (current_packet_offset
< 0) {
675 BT_LOGE_STR("Cannot get the current packet's offset.");
679 current_packet_size
= bt_notif_iter_get_current_packet_size(
680 ds_file
->notif_iter
);
681 if (current_packet_size
< 0) {
682 BT_LOGE("Cannot get packet size: packet-offset=%jd",
683 current_packet_offset
);
686 current_packet_size_bytes
=
687 ((current_packet_size
+ 7) & ~7) / CHAR_BIT
;
689 if (current_packet_offset
+ current_packet_size_bytes
>
690 ds_file
->file
->size
) {
691 BT_LOGW("Invalid packet size reported in file: stream=\"%s\", "
692 "packet-offset=%jd, packet-size-bytes=%jd, "
694 ds_file
->file
->path
->str
,
695 current_packet_offset
,
696 current_packet_size_bytes
,
697 ds_file
->file
->size
);
701 next_packet_offset
= current_packet_offset
+
702 current_packet_size_bytes
;
703 BT_LOGD("Seeking to next packet: current-packet-offset=%jd, "
704 "next-packet-offset=%jd", current_packet_offset
,
707 entry
= ctf_fs_ds_index_add_new_entry(index
);
709 BT_LOGE_STR("Failed to allocate a new index entry.");
713 ret
= init_index_entry(entry
, packet_context
,
714 current_packet_size_bytes
,
715 current_packet_offset
);
720 iter_status
= bt_notif_iter_seek(ds_file
->notif_iter
,
722 } while (iter_status
== BT_NOTIF_ITER_STATUS_OK
);
724 if (iter_status
!= BT_NOTIF_ITER_STATUS_EOF
) {
732 ctf_fs_ds_index_destroy(index
);
738 struct ctf_fs_ds_file
*ctf_fs_ds_file_create(
739 struct ctf_fs_trace
*ctf_fs_trace
,
740 struct bt_private_connection_private_notification_iterator
*pc_notif_iter
,
741 struct bt_notif_iter
*notif_iter
,
742 struct bt_stream
*stream
, const char *path
)
745 const size_t page_size
= bt_common_get_page_size();
746 struct ctf_fs_ds_file
*ds_file
= g_new0(struct ctf_fs_ds_file
, 1);
752 ds_file
->pc_notif_iter
= pc_notif_iter
;
753 ds_file
->file
= ctf_fs_file_create();
754 if (!ds_file
->file
) {
758 ds_file
->stream
= bt_get(stream
);
759 ds_file
->cc_prio_map
= bt_get(ctf_fs_trace
->cc_prio_map
);
760 g_string_assign(ds_file
->file
->path
, path
);
761 ret
= ctf_fs_file_open(ds_file
->file
, "rb");
766 ds_file
->notif_iter
= notif_iter
;
767 bt_notif_iter_set_medops_data(ds_file
->notif_iter
, ds_file
);
768 if (!ds_file
->notif_iter
) {
772 ds_file
->mmap_max_len
= page_size
* 2048;
777 /* Do not touch "borrowed" file. */
778 ctf_fs_ds_file_destroy(ds_file
);
786 struct ctf_fs_ds_index
*ctf_fs_ds_file_build_index(
787 struct ctf_fs_ds_file
*ds_file
)
789 struct ctf_fs_ds_index
*index
;
791 index
= build_index_from_idx_file(ds_file
);
796 BT_LOGD("Failed to build index from .index file; "
797 "falling back to stream indexing.");
798 index
= build_index_from_stream_file(ds_file
);
804 void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file
*ds_file
)
810 bt_put(ds_file
->cc_prio_map
);
811 bt_put(ds_file
->stream
);
812 (void) ds_file_munmap(ds_file
);
815 ctf_fs_file_destroy(ds_file
->file
);
822 enum bt_notification_iterator_status
ctf_fs_ds_file_next(
823 struct ctf_fs_ds_file
*ds_file
,
824 struct bt_notification
**notif
)
826 enum bt_notif_iter_status notif_iter_status
;
827 enum bt_notification_iterator_status status
;
829 notif_iter_status
= bt_notif_iter_get_next_notification(
830 ds_file
->notif_iter
, ds_file
->pc_notif_iter
, notif
);
832 switch (notif_iter_status
) {
833 case BT_NOTIF_ITER_STATUS_EOF
:
834 status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
836 case BT_NOTIF_ITER_STATUS_OK
:
837 status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
839 case BT_NOTIF_ITER_STATUS_AGAIN
:
841 * Should not make it this far as this is
842 * medium-specific; there is nothing for the user to do
843 * and it should have been handled upstream.
846 case BT_NOTIF_ITER_STATUS_INVAL
:
847 case BT_NOTIF_ITER_STATUS_ERROR
:
849 status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
857 int ctf_fs_ds_file_borrow_packet_header_context_fields(
858 struct ctf_fs_ds_file
*ds_file
,
859 struct bt_field
**packet_header_field
,
860 struct bt_field
**packet_context_field
)
862 enum bt_notif_iter_status notif_iter_status
;
866 notif_iter_status
= bt_notif_iter_borrow_packet_header_context_fields(
867 ds_file
->notif_iter
, packet_header_field
, packet_context_field
);
868 switch (notif_iter_status
) {
869 case BT_NOTIF_ITER_STATUS_EOF
:
870 case BT_NOTIF_ITER_STATUS_OK
:
872 case BT_NOTIF_ITER_STATUS_AGAIN
:
874 case BT_NOTIF_ITER_STATUS_INVAL
:
875 case BT_NOTIF_ITER_STATUS_ERROR
:
891 void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index
*index
)
897 if (index
->entries
) {
898 g_array_free(index
->entries
, TRUE
);