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/ctf-ir/stream.h>
35 #include <babeltrace/graph/notification-iterator.h>
36 #include <babeltrace/graph/notification-stream.h>
37 #include <babeltrace/graph/notification-event.h>
38 #include <babeltrace/graph/notification-packet.h>
39 #include <babeltrace/common-internal.h>
42 #include "../common/notif-iter/notif-iter.h"
44 #include "data-stream-file.h"
47 #define BT_LOG_TAG "PLUGIN-CTF-FS-SRC-DS"
51 size_t remaining_mmap_bytes(struct ctf_fs_ds_file
*ds_file
)
53 return ds_file
->mmap_len
- ds_file
->request_offset
;
57 int ds_file_munmap(struct ctf_fs_ds_file
*ds_file
)
61 if (!ds_file
|| !ds_file
->mmap_addr
) {
65 if (bt_munmap(ds_file
->mmap_addr
, ds_file
->mmap_len
)) {
66 BT_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 bt_ctf_notif_iter_medium_status
ds_file_mmap_next(
83 struct ctf_fs_ds_file
*ds_file
)
85 enum bt_ctf_notif_iter_medium_status ret
=
86 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
;
88 /* Unmap old region */
89 if (ds_file
->mmap_addr
) {
90 if (ds_file_munmap(ds_file
)) {
95 * mmap_len is guaranteed to be page-aligned except on the
96 * last mapping where it may not be possible (since the file's
97 * size itself may not be a page multiple).
99 ds_file
->mmap_offset
+= ds_file
->mmap_len
;
100 ds_file
->request_offset
= 0;
103 ds_file
->mmap_len
= MIN(ds_file
->file
->size
- ds_file
->mmap_offset
,
104 ds_file
->mmap_max_len
);
105 if (ds_file
->mmap_len
== 0) {
106 ret
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF
;
110 assert(ds_file
->mmap_len
);
111 ds_file
->mmap_addr
= bt_mmap((void *) 0, ds_file
->mmap_len
,
112 PROT_READ
, MAP_PRIVATE
, fileno(ds_file
->file
->fp
),
113 ds_file
->mmap_offset
);
114 if (ds_file
->mmap_addr
== MAP_FAILED
) {
115 BT_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s",
116 ds_file
->mmap_len
, ds_file
->file
->path
->str
,
117 ds_file
->file
->fp
, (intmax_t) ds_file
->mmap_offset
,
124 ds_file_munmap(ds_file
);
125 ret
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
131 enum bt_ctf_notif_iter_medium_status
medop_request_bytes(
132 size_t request_sz
, uint8_t **buffer_addr
,
133 size_t *buffer_sz
, void *data
)
135 enum bt_ctf_notif_iter_medium_status status
=
136 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
;
137 struct ctf_fs_ds_file
*ds_file
= data
;
139 if (request_sz
== 0) {
143 /* Check if we have at least one memory-mapped byte left */
144 if (remaining_mmap_bytes(ds_file
) == 0) {
145 /* Are we at the end of the file? */
146 if (ds_file
->mmap_offset
>= ds_file
->file
->size
) {
147 BT_LOGD("Reached end of file \"%s\" (%p)",
148 ds_file
->file
->path
->str
, ds_file
->file
->fp
);
149 status
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF
;
153 status
= ds_file_mmap_next(ds_file
);
155 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
:
157 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF
:
160 BT_LOGE("Cannot memory-map next region of file \"%s\" (%p)",
161 ds_file
->file
->path
->str
,
167 *buffer_sz
= MIN(remaining_mmap_bytes(ds_file
), request_sz
);
168 *buffer_addr
= ((uint8_t *) ds_file
->mmap_addr
) + ds_file
->request_offset
;
169 ds_file
->request_offset
+= *buffer_sz
;
173 status
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
180 struct bt_ctf_stream
*medop_get_stream(
181 struct bt_ctf_stream_class
*stream_class
, uint64_t stream_id
,
184 struct ctf_fs_ds_file
*ds_file
= data
;
185 struct bt_ctf_stream_class
*ds_file_stream_class
;
186 struct bt_ctf_stream
*stream
= NULL
;
188 ds_file_stream_class
= bt_ctf_stream_get_class(ds_file
->stream
);
189 bt_put(ds_file_stream_class
);
191 if (stream_class
!= ds_file_stream_class
) {
193 * Not supported: two packets described by two different
194 * stream classes within the same data stream file.
199 stream
= ds_file
->stream
;
206 enum bt_ctf_notif_iter_medium_status
medop_seek(
207 enum bt_ctf_notif_iter_seek_whence whence
, off_t offset
,
210 enum bt_ctf_notif_iter_medium_status ret
=
211 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
;
212 struct ctf_fs_ds_file
*ds_file
= data
;
213 off_t file_size
= ds_file
->file
->size
;
215 if (whence
!= BT_CTF_NOTIF_ITER_SEEK_WHENCE_SET
||
216 offset
< 0 || offset
> file_size
) {
217 BT_LOGE("Invalid medium seek request: whence=%d, offset=%jd, "
218 "file-size=%jd", (int) whence
, offset
,
220 ret
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_INVAL
;
225 * Determine whether or not the destination is contained within the
228 if (ds_file
->mmap_addr
&& (offset
< ds_file
->mmap_offset
||
229 offset
>= ds_file
->mmap_offset
+ ds_file
->mmap_len
)) {
231 off_t offset_in_mapping
= offset
% bt_common_get_page_size();
233 BT_LOGD("Medium seek request cannot be accomodated by the current "
234 "file mapping: offset=%jd, mmap-offset=%zu, "
235 "mmap-len=%zu", offset
, ds_file
->mmap_offset
,
237 unmap_ret
= ds_file_munmap(ds_file
);
239 ret
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
243 ds_file
->mmap_offset
= offset
- offset_in_mapping
;
244 ds_file
->request_offset
= offset_in_mapping
;
245 ret
= ds_file_mmap_next(ds_file
);
246 if (ret
!= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
) {
250 ds_file
->request_offset
= offset
- ds_file
->mmap_offset
;
253 ds_file
->end_reached
= (offset
== file_size
);
259 struct bt_ctf_notif_iter_medium_ops ctf_fs_ds_file_medops
= {
260 .request_bytes
= medop_request_bytes
,
261 .get_stream
= medop_get_stream
,
266 struct ctf_fs_ds_index
*ctf_fs_ds_index_create(size_t length
)
268 struct ctf_fs_ds_index
*index
= g_new0(struct ctf_fs_ds_index
, 1);
271 BT_LOGE_STR("Failed to allocate index");
275 index
->entries
= g_array_sized_new(FALSE
, TRUE
,
276 sizeof(struct ctf_fs_ds_index_entry
), length
);
277 if (!index
->entries
) {
278 BT_LOGE("Failed to allocate %zu index entries.", length
);
281 g_array_set_size(index
->entries
, length
);
285 ctf_fs_ds_index_destroy(index
);
289 /* Returns a new, zeroed, index entry. */
291 struct ctf_fs_ds_index_entry
*ctf_fs_ds_index_add_new_entry(
292 struct ctf_fs_ds_index
*index
)
294 g_array_set_size(index
->entries
, index
->entries
->len
+ 1);
295 return &g_array_index(index
->entries
, struct ctf_fs_ds_index_entry
,
296 index
->entries
->len
- 1);
300 struct bt_ctf_clock_class
*get_field_mapped_clock_class(
301 struct bt_ctf_field
*field
)
303 struct bt_ctf_field_type
*field_type
;
304 struct bt_ctf_clock_class
*clock_class
= NULL
;
306 field_type
= bt_ctf_field_get_type(field
);
311 clock_class
= bt_ctf_field_type_integer_get_mapped_clock_class(
322 int get_packet_bounds_from_packet_context(
323 struct bt_ctf_field
*packet_context
,
324 struct bt_ctf_clock_class
**_timestamp_begin_cc
,
325 struct bt_ctf_field
**_timestamp_begin
,
326 struct bt_ctf_clock_class
**_timestamp_end_cc
,
327 struct bt_ctf_field
**_timestamp_end
)
330 struct bt_ctf_clock_class
*timestamp_begin_cc
= NULL
;
331 struct bt_ctf_clock_class
*timestamp_end_cc
= NULL
;
332 struct bt_ctf_field
*timestamp_begin
= NULL
;
333 struct bt_ctf_field
*timestamp_end
= NULL
;
335 timestamp_begin
= bt_ctf_field_structure_get_field_by_name(
336 packet_context
, "timestamp_begin");
337 if (!timestamp_begin
) {
338 BT_LOGD_STR("Cannot retrieve timestamp_begin field in packet context.");
343 timestamp_begin_cc
= get_field_mapped_clock_class(timestamp_begin
);
344 if (!timestamp_begin_cc
) {
345 BT_LOGD_STR("Cannot retrieve the clock mapped to timestamp_begin.");
348 timestamp_end
= bt_ctf_field_structure_get_field_by_name(
349 packet_context
, "timestamp_end");
350 if (!timestamp_end
) {
351 BT_LOGD_STR("Cannot retrieve timestamp_end field in packet context.");
356 timestamp_end_cc
= get_field_mapped_clock_class(timestamp_end
);
357 if (!timestamp_end_cc
) {
358 BT_LOGD_STR("Cannot retrieve the clock mapped to timestamp_end.");
361 if (_timestamp_begin_cc
) {
362 *_timestamp_begin_cc
= bt_get(timestamp_begin_cc
);
364 if (_timestamp_begin
) {
365 *_timestamp_begin
= bt_get(timestamp_begin
);
367 if (_timestamp_end_cc
) {
368 *_timestamp_end_cc
= bt_get(timestamp_end_cc
);
370 if (_timestamp_end
) {
371 *_timestamp_end
= bt_get(timestamp_end
);
374 bt_put(timestamp_begin_cc
);
375 bt_put(timestamp_end_cc
);
376 bt_put(timestamp_begin
);
377 bt_put(timestamp_end
);
382 int get_ds_file_packet_bounds_clock_classes(struct ctf_fs_ds_file
*ds_file
,
383 struct bt_ctf_clock_class
**timestamp_begin_cc
,
384 struct bt_ctf_clock_class
**timestamp_end_cc
)
386 struct bt_ctf_field
*packet_context
= NULL
;
387 int ret
= ctf_fs_ds_file_get_packet_header_context_fields(ds_file
,
388 NULL
, &packet_context
);
390 if (ret
|| !packet_context
) {
391 BT_LOGD("Cannot retrieve packet context field of stream \'%s\'",
392 ds_file
->file
->path
->str
);
397 ret
= get_packet_bounds_from_packet_context(packet_context
,
398 timestamp_begin_cc
, NULL
,
399 timestamp_end_cc
, NULL
);
401 bt_put(packet_context
);
406 int convert_cycles_to_ns(struct bt_ctf_clock_class
*clock_class
,
407 uint64_t cycles
, int64_t *ns
)
410 struct bt_ctf_clock_value
*clock_value
;
413 clock_value
= bt_ctf_clock_value_create(clock_class
, cycles
);
419 ret
= bt_ctf_clock_value_get_value_ns_from_epoch(clock_value
, ns
);
429 struct ctf_fs_ds_index
*build_index_from_idx_file(
430 struct ctf_fs_ds_file
*ds_file
)
433 gchar
*directory
= NULL
;
434 gchar
*basename
= NULL
;
435 GString
*index_basename
= NULL
;
436 gchar
*index_file_path
= NULL
;
437 GMappedFile
*mapped_file
= NULL
;
439 const char *mmap_begin
= NULL
, *file_pos
= NULL
;
440 const struct ctf_packet_index_file_hdr
*header
= NULL
;
441 struct ctf_fs_ds_index
*index
= NULL
;
442 struct ctf_fs_ds_index_entry
*index_entry
= NULL
;
443 uint64_t total_packets_size
= 0;
444 size_t file_index_entry_size
;
445 size_t file_entry_count
;
447 struct bt_ctf_clock_class
*timestamp_begin_cc
= NULL
;
448 struct bt_ctf_clock_class
*timestamp_end_cc
= NULL
;
450 BT_LOGD("Building index from .idx file of stream file %s",
451 ds_file
->file
->path
->str
);
453 ret
= get_ds_file_packet_bounds_clock_classes(ds_file
,
454 ×tamp_begin_cc
, ×tamp_end_cc
);
456 BT_LOGD_STR("Cannot get clock classes of \"timestamp_begin\" "
457 "and \"timestamp_end\" fields");
461 /* Look for index file in relative path index/name.idx. */
462 basename
= g_path_get_basename(ds_file
->file
->path
->str
);
464 BT_LOGE("Cannot get the basename of datastream file %s",
465 ds_file
->file
->path
->str
);
469 directory
= g_path_get_dirname(ds_file
->file
->path
->str
);
471 BT_LOGE("Cannot get dirname of datastream file %s",
472 ds_file
->file
->path
->str
);
476 index_basename
= g_string_new(basename
);
477 if (!index_basename
) {
478 BT_LOGE_STR("Cannot allocate index file basename string");
482 g_string_append(index_basename
, ".idx");
483 index_file_path
= g_build_filename(directory
, "index",
484 index_basename
->str
, NULL
);
485 mapped_file
= g_mapped_file_new(index_file_path
, FALSE
, NULL
);
487 BT_LOGD("Cannot create new mapped file %s",
493 * The g_mapped_file API limits us to 4GB files on 32-bit.
494 * Traces with such large indexes have never been seen in the wild,
495 * but this would need to be adjusted to support them.
497 filesize
= g_mapped_file_get_length(mapped_file
);
498 if (filesize
< sizeof(*header
)) {
499 BT_LOGW("Invalid LTTng trace index file: "
500 "file size (%zu bytes) < header size (%zu bytes)",
501 filesize
, sizeof(*header
));
505 mmap_begin
= g_mapped_file_get_contents(mapped_file
);
506 header
= (struct ctf_packet_index_file_hdr
*) mmap_begin
;
508 file_pos
= g_mapped_file_get_contents(mapped_file
) + sizeof(*header
);
509 if (be32toh(header
->magic
) != CTF_INDEX_MAGIC
) {
510 BT_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
514 file_index_entry_size
= be32toh(header
->packet_index_len
);
515 file_entry_count
= (filesize
- sizeof(*header
)) / file_index_entry_size
;
516 if ((filesize
- sizeof(*header
)) % file_index_entry_size
) {
517 BT_LOGW("Invalid LTTng trace index: the index's size after the header "
518 "(%zu bytes) is not a multiple of the index entry size "
519 "(%zu bytes)", (filesize
- sizeof(*header
)),
524 index
= ctf_fs_ds_index_create(file_entry_count
);
529 index_entry
= (struct ctf_fs_ds_index_entry
*) &g_array_index(
530 index
->entries
, struct ctf_fs_ds_index_entry
, 0);
531 for (i
= 0; i
< file_entry_count
; i
++) {
532 struct ctf_packet_index
*file_index
=
533 (struct ctf_packet_index
*) file_pos
;
534 uint64_t packet_size
= be64toh(file_index
->packet_size
);
536 if (packet_size
% CHAR_BIT
) {
537 BT_LOGW("Invalid packet size encountered in LTTng trace index file");
541 /* Convert size in bits to bytes. */
542 packet_size
/= CHAR_BIT
;
543 index_entry
->packet_size
= packet_size
;
545 index_entry
->offset
= be64toh(file_index
->offset
);
546 if (i
!= 0 && index_entry
->offset
< (index_entry
- 1)->offset
) {
547 BT_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
548 "previous offset=%" PRIu64
", current offset=%" PRIu64
,
549 (index_entry
- 1)->offset
, index_entry
->offset
);
553 index_entry
->timestamp_begin
= be64toh(file_index
->timestamp_begin
);
554 index_entry
->timestamp_end
= be64toh(file_index
->timestamp_end
);
555 if (index_entry
->timestamp_end
< index_entry
->timestamp_begin
) {
556 BT_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
557 "timestamp_begin=%" PRIu64
"timestamp_end=%" PRIu64
,
558 index_entry
->timestamp_begin
,
559 index_entry
->timestamp_end
);
563 /* Convert the packet's bound to nanoseconds since Epoch. */
564 ret
= convert_cycles_to_ns(timestamp_begin_cc
,
565 index_entry
->timestamp_begin
,
566 &index_entry
->timestamp_begin_ns
);
568 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
571 ret
= convert_cycles_to_ns(timestamp_end_cc
,
572 index_entry
->timestamp_end
,
573 &index_entry
->timestamp_end_ns
);
575 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
579 total_packets_size
+= packet_size
;
580 file_pos
+= file_index_entry_size
;
584 /* Validate that the index addresses the complete stream. */
585 if (ds_file
->file
->size
!= total_packets_size
) {
586 BT_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
587 "file-size=%" PRIu64
", total-packets-size=%" PRIu64
,
588 ds_file
->file
->size
, total_packets_size
);
594 g_free(index_file_path
);
595 if (index_basename
) {
596 g_string_free(index_basename
, TRUE
);
599 g_mapped_file_unref(mapped_file
);
601 bt_put(timestamp_begin_cc
);
602 bt_put(timestamp_end_cc
);
605 ctf_fs_ds_index_destroy(index
);
611 int init_index_entry(struct ctf_fs_ds_index_entry
*entry
,
612 struct bt_ctf_field
*packet_context
, off_t packet_size
,
616 struct bt_ctf_field
*timestamp_begin
= NULL
;
617 struct bt_ctf_field
*timestamp_end
= NULL
;
618 struct bt_ctf_clock_class
*timestamp_begin_cc
= NULL
;
619 struct bt_ctf_clock_class
*timestamp_end_cc
= NULL
;
621 ret
= get_packet_bounds_from_packet_context(packet_context
,
622 ×tamp_begin_cc
, ×tamp_begin
,
623 ×tamp_end_cc
, ×tamp_end
);
624 if (ret
|| !timestamp_begin_cc
|| !timestamp_begin
||
625 !timestamp_end_cc
|| ! timestamp_end
) {
626 BT_LOGD_STR("Failed to determine time bound fields of packet.");
630 assert(packet_offset
>= 0);
631 entry
->offset
= packet_offset
;
633 assert(packet_size
>= 0);
634 entry
->packet_size
= packet_size
;
636 ret
= bt_ctf_field_unsigned_integer_get_value(timestamp_begin
,
637 &entry
->timestamp_begin
);
641 ret
= bt_ctf_field_unsigned_integer_get_value(timestamp_end
,
642 &entry
->timestamp_end
);
647 /* Convert the packet's bound to nanoseconds since Epoch. */
648 ret
= convert_cycles_to_ns(timestamp_begin_cc
,
649 entry
->timestamp_begin
,
650 &entry
->timestamp_begin_ns
);
652 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
656 ret
= convert_cycles_to_ns(timestamp_end_cc
,
657 entry
->timestamp_end
,
658 &entry
->timestamp_end_ns
);
660 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
664 bt_put(timestamp_begin
);
665 bt_put(timestamp_begin_cc
);
666 bt_put(timestamp_end
);
667 bt_put(timestamp_end_cc
);
672 struct ctf_fs_ds_index
*build_index_from_stream_file(
673 struct ctf_fs_ds_file
*ds_file
)
676 struct ctf_fs_ds_index
*index
= NULL
;
677 enum bt_ctf_notif_iter_status iter_status
;
678 struct bt_ctf_field
*packet_context
= NULL
;
680 BT_LOGD("Indexing stream file %s", ds_file
->file
->path
->str
);
682 index
= ctf_fs_ds_index_create(0);
688 off_t current_packet_offset
;
689 off_t next_packet_offset
;
690 off_t current_packet_size
, current_packet_size_bytes
;
691 struct ctf_fs_ds_index_entry
*entry
;
693 iter_status
= bt_ctf_notif_iter_get_packet_header_context_fields(
694 ds_file
->notif_iter
, NULL
, &packet_context
);
695 if (iter_status
!= BT_CTF_NOTIF_ITER_STATUS_OK
) {
696 if (iter_status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
701 current_packet_offset
=
702 bt_ctf_notif_iter_get_current_packet_offset(
703 ds_file
->notif_iter
);
704 if (current_packet_offset
< 0) {
705 BT_LOGE_STR("Cannot get the current packet's offset.");
709 current_packet_size
= bt_ctf_notif_iter_get_current_packet_size(
710 ds_file
->notif_iter
);
711 if (current_packet_size
< 0) {
712 BT_LOGE("Cannot get packet size: packet-offset=%jd",
713 current_packet_offset
);
716 current_packet_size_bytes
=
717 ((current_packet_size
+ 7) & ~7) / CHAR_BIT
;
719 if (current_packet_offset
+ current_packet_size_bytes
>
720 ds_file
->file
->size
) {
721 BT_LOGW("Invalid packet size reported in file: stream=\"%s\", "
722 "packet-offset=%jd, packet-size-bytes=%jd, "
724 ds_file
->file
->path
->str
,
725 current_packet_offset
,
726 current_packet_size_bytes
,
727 ds_file
->file
->size
);
731 next_packet_offset
= current_packet_offset
+
732 current_packet_size_bytes
;
733 BT_LOGD("Seeking to next packet: current-packet-offset=%jd, "
734 "next-packet-offset=%jd", current_packet_offset
,
737 entry
= ctf_fs_ds_index_add_new_entry(index
);
739 BT_LOGE_STR("Failed to allocate a new index entry.");
743 ret
= init_index_entry(entry
, packet_context
,
744 current_packet_size_bytes
,
745 current_packet_offset
);
750 iter_status
= bt_ctf_notif_iter_seek(ds_file
->notif_iter
,
752 BT_PUT(packet_context
);
753 } while (iter_status
== BT_CTF_NOTIF_ITER_STATUS_OK
);
755 if (iter_status
!= BT_CTF_NOTIF_ITER_STATUS_EOF
) {
759 bt_put(packet_context
);
762 ctf_fs_ds_index_destroy(index
);
768 struct ctf_fs_ds_file
*ctf_fs_ds_file_create(
769 struct ctf_fs_trace
*ctf_fs_trace
,
770 struct bt_ctf_notif_iter
*notif_iter
,
771 struct bt_ctf_stream
*stream
, const char *path
)
774 const size_t page_size
= bt_common_get_page_size();
775 struct ctf_fs_ds_file
*ds_file
= g_new0(struct ctf_fs_ds_file
, 1);
781 ds_file
->file
= ctf_fs_file_create();
782 if (!ds_file
->file
) {
786 ds_file
->stream
= bt_get(stream
);
787 ds_file
->cc_prio_map
= bt_get(ctf_fs_trace
->cc_prio_map
);
788 g_string_assign(ds_file
->file
->path
, path
);
789 ret
= ctf_fs_file_open(ds_file
->file
, "rb");
794 ds_file
->notif_iter
= notif_iter
;
795 bt_ctf_notif_iter_set_medops_data(ds_file
->notif_iter
, ds_file
);
796 if (!ds_file
->notif_iter
) {
800 ds_file
->mmap_max_len
= page_size
* 2048;
805 /* Do not touch "borrowed" file. */
806 ctf_fs_ds_file_destroy(ds_file
);
814 struct ctf_fs_ds_index
*ctf_fs_ds_file_build_index(
815 struct ctf_fs_ds_file
*ds_file
)
817 struct ctf_fs_ds_index
*index
;
819 index
= build_index_from_idx_file(ds_file
);
824 BT_LOGD("Failed to build index from .index file; "
825 "falling back to stream indexing.");
826 index
= build_index_from_stream_file(ds_file
);
832 void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file
*ds_file
)
838 bt_put(ds_file
->cc_prio_map
);
839 bt_put(ds_file
->stream
);
840 (void) ds_file_munmap(ds_file
);
843 ctf_fs_file_destroy(ds_file
->file
);
850 struct bt_notification_iterator_next_return
ctf_fs_ds_file_next(
851 struct ctf_fs_ds_file
*ds_file
)
853 enum bt_ctf_notif_iter_status notif_iter_status
;
854 struct bt_notification_iterator_next_return ret
= {
855 .status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
,
856 .notification
= NULL
,
859 notif_iter_status
= bt_ctf_notif_iter_get_next_notification(
860 ds_file
->notif_iter
, ds_file
->cc_prio_map
, &ret
.notification
);
862 switch (notif_iter_status
) {
863 case BT_CTF_NOTIF_ITER_STATUS_EOF
:
864 ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
866 case BT_CTF_NOTIF_ITER_STATUS_OK
:
867 ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
869 case BT_CTF_NOTIF_ITER_STATUS_AGAIN
:
871 * Should not make it this far as this is
872 * medium-specific; there is nothing for the user to do
873 * and it should have been handled upstream.
876 case BT_CTF_NOTIF_ITER_STATUS_INVAL
:
877 case BT_CTF_NOTIF_ITER_STATUS_ERROR
:
879 ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
887 int ctf_fs_ds_file_get_packet_header_context_fields(
888 struct ctf_fs_ds_file
*ds_file
,
889 struct bt_ctf_field
**packet_header_field
,
890 struct bt_ctf_field
**packet_context_field
)
892 enum bt_ctf_notif_iter_status notif_iter_status
;
896 notif_iter_status
= bt_ctf_notif_iter_get_packet_header_context_fields(
897 ds_file
->notif_iter
, packet_header_field
, packet_context_field
);
898 switch (notif_iter_status
) {
899 case BT_CTF_NOTIF_ITER_STATUS_EOF
:
900 case BT_CTF_NOTIF_ITER_STATUS_OK
:
902 case BT_CTF_NOTIF_ITER_STATUS_AGAIN
:
904 case BT_CTF_NOTIF_ITER_STATUS_INVAL
:
905 case BT_CTF_NOTIF_ITER_STATUS_ERROR
:
916 if (packet_header_field
) {
917 bt_put(*packet_header_field
);
920 if (packet_context_field
) {
921 bt_put(*packet_context_field
);
929 void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index
*index
)
935 if (index
->entries
) {
936 g_array_free(index
->entries
, TRUE
);