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/msg-iter/msg-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_msg_iter_medium_status
ds_file_mmap_next(
79 struct ctf_fs_ds_file
*ds_file
)
81 enum bt_msg_iter_medium_status ret
=
82 BT_MSG_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_MSG_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_MSG_ITER_MEDIUM_STATUS_ERROR
;
127 enum bt_msg_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_msg_iter_medium_status status
=
132 BT_MSG_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_MSG_ITER_MEDIUM_STATUS_EOF
;
149 status
= ds_file_mmap_next(ds_file
);
151 case BT_MSG_ITER_MEDIUM_STATUS_OK
:
153 case BT_MSG_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_MSG_ITER_MEDIUM_STATUS_ERROR
;
176 bt_stream
*medop_borrow_stream(
177 bt_stream_class
*stream_class
, int64_t stream_id
,
180 struct ctf_fs_ds_file
*ds_file
= data
;
181 bt_stream_class
*ds_file_stream_class
;
182 bt_stream
*stream
= NULL
;
184 ds_file_stream_class
= bt_stream_borrow_class(
187 if (stream_class
!= ds_file_stream_class
) {
189 * Not supported: two packets described by two different
190 * stream classes within the same data stream file.
195 stream
= ds_file
->stream
;
202 enum bt_msg_iter_medium_status
medop_seek(
203 enum bt_msg_iter_seek_whence whence
, off_t offset
,
206 enum bt_msg_iter_medium_status ret
=
207 BT_MSG_ITER_MEDIUM_STATUS_OK
;
208 struct ctf_fs_ds_file
*ds_file
= data
;
209 off_t file_size
= ds_file
->file
->size
;
211 if (whence
!= BT_MSG_ITER_SEEK_WHENCE_SET
||
212 offset
< 0 || offset
> file_size
) {
213 BT_LOGE("Invalid medium seek request: whence=%d, offset=%jd, "
214 "file-size=%jd", (int) whence
, offset
,
216 ret
= BT_MSG_ITER_MEDIUM_STATUS_INVAL
;
221 * Determine whether or not the destination is contained within the
224 if (ds_file
->mmap_addr
&& (offset
< ds_file
->mmap_offset
||
225 offset
>= ds_file
->mmap_offset
+ ds_file
->mmap_len
)) {
227 off_t offset_in_mapping
= offset
% bt_common_get_page_size();
229 BT_LOGD("Medium seek request cannot be accomodated by the current "
230 "file mapping: offset=%jd, mmap-offset=%jd, "
231 "mmap-len=%zu", offset
, ds_file
->mmap_offset
,
233 unmap_ret
= ds_file_munmap(ds_file
);
235 ret
= BT_MSG_ITER_MEDIUM_STATUS_ERROR
;
239 ds_file
->mmap_offset
= offset
- offset_in_mapping
;
240 ds_file
->request_offset
= offset_in_mapping
;
241 ret
= ds_file_mmap_next(ds_file
);
242 if (ret
!= BT_MSG_ITER_MEDIUM_STATUS_OK
) {
246 ds_file
->request_offset
= offset
- ds_file
->mmap_offset
;
249 ds_file
->end_reached
= (offset
== file_size
);
255 struct bt_msg_iter_medium_ops ctf_fs_ds_file_medops
= {
256 .request_bytes
= medop_request_bytes
,
257 .borrow_stream
= medop_borrow_stream
,
262 struct ctf_fs_ds_index
*ctf_fs_ds_index_create(size_t length
)
264 struct ctf_fs_ds_index
*index
= g_new0(struct ctf_fs_ds_index
, 1);
267 BT_LOGE_STR("Failed to allocate index");
271 index
->entries
= g_array_sized_new(FALSE
, TRUE
,
272 sizeof(struct ctf_fs_ds_index_entry
), length
);
273 if (!index
->entries
) {
274 BT_LOGE("Failed to allocate %zu index entries.", length
);
277 g_array_set_size(index
->entries
, length
);
281 ctf_fs_ds_index_destroy(index
);
285 /* Returns a new, zeroed, index entry. */
287 struct ctf_fs_ds_index_entry
*ctf_fs_ds_index_add_new_entry(
288 struct ctf_fs_ds_index
*index
)
290 g_array_set_size(index
->entries
, index
->entries
->len
+ 1);
291 return &g_array_index(index
->entries
, struct ctf_fs_ds_index_entry
,
292 index
->entries
->len
- 1);
296 int convert_cycles_to_ns(bt_clock_class
*clock_class
,
297 uint64_t cycles
, int64_t *ns
)
299 return bt_clock_class_cycles_to_ns_from_origin(clock_class
, cycles
,
304 struct ctf_fs_ds_index
*build_index_from_idx_file(
305 struct ctf_fs_ds_file
*ds_file
)
308 gchar
*directory
= NULL
;
309 gchar
*basename
= NULL
;
310 GString
*index_basename
= NULL
;
311 gchar
*index_file_path
= NULL
;
312 GMappedFile
*mapped_file
= NULL
;
314 const char *mmap_begin
= NULL
, *file_pos
= NULL
;
315 const struct ctf_packet_index_file_hdr
*header
= NULL
;
316 struct ctf_fs_ds_index
*index
= NULL
;
317 struct ctf_fs_ds_index_entry
*index_entry
= NULL
;
318 uint64_t total_packets_size
= 0;
319 size_t file_index_entry_size
;
320 size_t file_entry_count
;
322 struct ctf_stream_class
*sc
;
323 struct bt_msg_iter_packet_properties props
;
325 BT_LOGD("Building index from .idx file of stream file %s",
326 ds_file
->file
->path
->str
);
328 ret
= bt_msg_iter_borrow_packet_header_context_fields(
329 ds_file
->msg_iter
, NULL
, NULL
);
331 BT_LOGD_STR("Cannot borrow first packet's header and context "
336 ret
= bt_msg_iter_get_packet_properties(ds_file
->msg_iter
, &props
);
338 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
,
339 props
.stream_class_id
);
341 if (!sc
->default_clock_class
) {
342 BT_LOGD_STR("Cannot find stream class's default clock class.");
346 /* Look for index file in relative path index/name.idx. */
347 basename
= g_path_get_basename(ds_file
->file
->path
->str
);
349 BT_LOGE("Cannot get the basename of datastream file %s",
350 ds_file
->file
->path
->str
);
354 directory
= g_path_get_dirname(ds_file
->file
->path
->str
);
356 BT_LOGE("Cannot get dirname of datastream file %s",
357 ds_file
->file
->path
->str
);
361 index_basename
= g_string_new(basename
);
362 if (!index_basename
) {
363 BT_LOGE_STR("Cannot allocate index file basename string");
367 g_string_append(index_basename
, ".idx");
368 index_file_path
= g_build_filename(directory
, "index",
369 index_basename
->str
, NULL
);
370 mapped_file
= g_mapped_file_new(index_file_path
, FALSE
, NULL
);
372 BT_LOGD("Cannot create new mapped file %s",
378 * The g_mapped_file API limits us to 4GB files on 32-bit.
379 * Traces with such large indexes have never been seen in the wild,
380 * but this would need to be adjusted to support them.
382 filesize
= g_mapped_file_get_length(mapped_file
);
383 if (filesize
< sizeof(*header
)) {
384 BT_LOGW("Invalid LTTng trace index file: "
385 "file size (%zu bytes) < header size (%zu bytes)",
386 filesize
, sizeof(*header
));
390 mmap_begin
= g_mapped_file_get_contents(mapped_file
);
391 header
= (struct ctf_packet_index_file_hdr
*) mmap_begin
;
393 file_pos
= g_mapped_file_get_contents(mapped_file
) + sizeof(*header
);
394 if (be32toh(header
->magic
) != CTF_INDEX_MAGIC
) {
395 BT_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
399 file_index_entry_size
= be32toh(header
->packet_index_len
);
400 file_entry_count
= (filesize
- sizeof(*header
)) / file_index_entry_size
;
401 if ((filesize
- sizeof(*header
)) % file_index_entry_size
) {
402 BT_LOGW("Invalid LTTng trace index: the index's size after the header "
403 "(%zu bytes) is not a multiple of the index entry size "
404 "(%zu bytes)", (filesize
- sizeof(*header
)),
409 index
= ctf_fs_ds_index_create(file_entry_count
);
414 index_entry
= (struct ctf_fs_ds_index_entry
*) &g_array_index(
415 index
->entries
, struct ctf_fs_ds_index_entry
, 0);
416 for (i
= 0; i
< file_entry_count
; i
++) {
417 struct ctf_packet_index
*file_index
=
418 (struct ctf_packet_index
*) file_pos
;
419 uint64_t packet_size
= be64toh(file_index
->packet_size
);
421 if (packet_size
% CHAR_BIT
) {
422 BT_LOGW("Invalid packet size encountered in LTTng trace index file");
426 /* Convert size in bits to bytes. */
427 packet_size
/= CHAR_BIT
;
428 index_entry
->packet_size
= packet_size
;
430 index_entry
->offset
= be64toh(file_index
->offset
);
431 if (i
!= 0 && index_entry
->offset
< (index_entry
- 1)->offset
) {
432 BT_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
433 "previous offset=%" PRIu64
", current offset=%" PRIu64
,
434 (index_entry
- 1)->offset
, index_entry
->offset
);
438 index_entry
->timestamp_begin
= be64toh(file_index
->timestamp_begin
);
439 index_entry
->timestamp_end
= be64toh(file_index
->timestamp_end
);
440 if (index_entry
->timestamp_end
< index_entry
->timestamp_begin
) {
441 BT_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
442 "timestamp_begin=%" PRIu64
"timestamp_end=%" PRIu64
,
443 index_entry
->timestamp_begin
,
444 index_entry
->timestamp_end
);
448 /* Convert the packet's bound to nanoseconds since Epoch. */
449 ret
= convert_cycles_to_ns(sc
->default_clock_class
,
450 index_entry
->timestamp_begin
,
451 &index_entry
->timestamp_begin_ns
);
453 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
456 ret
= convert_cycles_to_ns(sc
->default_clock_class
,
457 index_entry
->timestamp_end
,
458 &index_entry
->timestamp_end_ns
);
460 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
464 total_packets_size
+= packet_size
;
465 file_pos
+= file_index_entry_size
;
469 /* Validate that the index addresses the complete stream. */
470 if (ds_file
->file
->size
!= total_packets_size
) {
471 BT_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
472 "file-size=%" PRIu64
", total-packets-size=%" PRIu64
,
473 ds_file
->file
->size
, total_packets_size
);
479 g_free(index_file_path
);
480 if (index_basename
) {
481 g_string_free(index_basename
, TRUE
);
484 g_mapped_file_unref(mapped_file
);
488 ctf_fs_ds_index_destroy(index
);
494 int init_index_entry(struct ctf_fs_ds_index_entry
*entry
,
495 struct ctf_fs_ds_file
*ds_file
,
496 struct bt_msg_iter_packet_properties
*props
,
497 off_t packet_size
, off_t packet_offset
)
500 struct ctf_stream_class
*sc
;
502 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
,
503 props
->stream_class_id
);
505 BT_ASSERT(packet_offset
>= 0);
506 entry
->offset
= packet_offset
;
507 BT_ASSERT(packet_size
>= 0);
508 entry
->packet_size
= packet_size
;
510 /* Convert the packet's bound to nanoseconds since Epoch. */
511 ret
= convert_cycles_to_ns(sc
->default_clock_class
,
512 props
->snapshots
.beginning_clock
,
513 &entry
->timestamp_begin_ns
);
515 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
519 ret
= convert_cycles_to_ns(sc
->default_clock_class
,
520 props
->snapshots
.end_clock
,
521 &entry
->timestamp_end_ns
);
523 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
532 struct ctf_fs_ds_index
*build_index_from_stream_file(
533 struct ctf_fs_ds_file
*ds_file
)
536 struct ctf_fs_ds_index
*index
= NULL
;
537 enum bt_msg_iter_status iter_status
;
539 BT_LOGD("Indexing stream file %s", ds_file
->file
->path
->str
);
541 index
= ctf_fs_ds_index_create(0);
547 off_t current_packet_offset
;
548 off_t next_packet_offset
;
549 off_t current_packet_size_bytes
;
550 struct ctf_fs_ds_index_entry
*entry
;
551 struct bt_msg_iter_packet_properties props
;
553 iter_status
= bt_msg_iter_borrow_packet_header_context_fields(
554 ds_file
->msg_iter
, NULL
, NULL
);
555 if (iter_status
!= BT_MSG_ITER_STATUS_OK
) {
556 if (iter_status
== BT_MSG_ITER_STATUS_EOF
) {
562 ret
= bt_msg_iter_get_packet_properties(ds_file
->msg_iter
,
566 current_packet_offset
=
567 bt_msg_iter_get_current_packet_offset(
569 if (current_packet_offset
< 0) {
570 BT_LOGE_STR("Cannot get the current packet's offset.");
574 current_packet_size_bytes
=
575 ((props
.exp_packet_total_size
+ 7) & ~7) / CHAR_BIT
;
577 if (current_packet_offset
+ current_packet_size_bytes
>
578 ds_file
->file
->size
) {
579 BT_LOGW("Invalid packet size reported in file: stream=\"%s\", "
580 "packet-offset=%jd, packet-size-bytes=%jd, "
582 ds_file
->file
->path
->str
,
583 current_packet_offset
,
584 current_packet_size_bytes
,
585 ds_file
->file
->size
);
589 next_packet_offset
= current_packet_offset
+
590 current_packet_size_bytes
;
591 BT_LOGD("Seeking to next packet: current-packet-offset=%jd, "
592 "next-packet-offset=%jd", current_packet_offset
,
595 entry
= ctf_fs_ds_index_add_new_entry(index
);
597 BT_LOGE_STR("Failed to allocate a new index entry.");
601 ret
= init_index_entry(entry
, ds_file
, &props
,
602 current_packet_size_bytes
, current_packet_offset
);
607 iter_status
= bt_msg_iter_seek(ds_file
->msg_iter
,
609 } while (iter_status
== BT_MSG_ITER_STATUS_OK
);
611 if (iter_status
!= BT_MSG_ITER_STATUS_EOF
) {
619 ctf_fs_ds_index_destroy(index
);
625 struct ctf_fs_ds_file
*ctf_fs_ds_file_create(
626 struct ctf_fs_trace
*ctf_fs_trace
,
627 bt_self_message_iterator
*pc_msg_iter
,
628 struct bt_msg_iter
*msg_iter
,
629 bt_stream
*stream
, const char *path
)
632 const size_t page_size
= bt_common_get_page_size();
633 struct ctf_fs_ds_file
*ds_file
= g_new0(struct ctf_fs_ds_file
, 1);
639 ds_file
->pc_msg_iter
= pc_msg_iter
;
640 ds_file
->file
= ctf_fs_file_create();
641 if (!ds_file
->file
) {
645 ds_file
->stream
= stream
;
646 bt_stream_get_ref(ds_file
->stream
);
647 ds_file
->metadata
= ctf_fs_trace
->metadata
;
648 g_string_assign(ds_file
->file
->path
, path
);
649 ret
= ctf_fs_file_open(ds_file
->file
, "rb");
654 ds_file
->msg_iter
= msg_iter
;
655 bt_msg_iter_set_medops_data(ds_file
->msg_iter
, ds_file
);
656 if (!ds_file
->msg_iter
) {
660 ds_file
->mmap_max_len
= page_size
* 2048;
665 /* Do not touch "borrowed" file. */
666 ctf_fs_ds_file_destroy(ds_file
);
674 struct ctf_fs_ds_index
*ctf_fs_ds_file_build_index(
675 struct ctf_fs_ds_file
*ds_file
)
677 struct ctf_fs_ds_index
*index
;
679 index
= build_index_from_idx_file(ds_file
);
684 BT_LOGD("Failed to build index from .index file; "
685 "falling back to stream indexing.");
686 index
= build_index_from_stream_file(ds_file
);
692 void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file
*ds_file
)
698 bt_stream_put_ref(ds_file
->stream
);
699 (void) ds_file_munmap(ds_file
);
702 ctf_fs_file_destroy(ds_file
->file
);
709 enum bt_message_iterator_status
ctf_fs_ds_file_next(
710 struct ctf_fs_ds_file
*ds_file
,
713 enum bt_msg_iter_status msg_iter_status
;
714 enum bt_message_iterator_status status
;
716 msg_iter_status
= bt_msg_iter_get_next_message(
717 ds_file
->msg_iter
, ds_file
->pc_msg_iter
, msg
);
719 switch (msg_iter_status
) {
720 case BT_MSG_ITER_STATUS_EOF
:
721 status
= BT_MESSAGE_ITERATOR_STATUS_END
;
723 case BT_MSG_ITER_STATUS_OK
:
724 status
= BT_MESSAGE_ITERATOR_STATUS_OK
;
726 case BT_MSG_ITER_STATUS_AGAIN
:
728 * Should not make it this far as this is
729 * medium-specific; there is nothing for the user to do
730 * and it should have been handled upstream.
733 case BT_MSG_ITER_STATUS_INVAL
:
734 case BT_MSG_ITER_STATUS_ERROR
:
736 status
= BT_MESSAGE_ITERATOR_STATUS_ERROR
;
743 int ctf_fs_ds_file_borrow_packet_header_context_fields(
744 struct ctf_fs_ds_file
*ds_file
,
745 bt_field
**packet_header_field
,
746 bt_field
**packet_context_field
)
748 enum bt_msg_iter_status msg_iter_status
;
752 msg_iter_status
= bt_msg_iter_borrow_packet_header_context_fields(
753 ds_file
->msg_iter
, packet_header_field
, packet_context_field
);
754 switch (msg_iter_status
) {
755 case BT_MSG_ITER_STATUS_EOF
:
756 case BT_MSG_ITER_STATUS_OK
:
758 case BT_MSG_ITER_STATUS_AGAIN
:
760 case BT_MSG_ITER_STATUS_INVAL
:
761 case BT_MSG_ITER_STATUS_ERROR
:
777 void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index
*index
)
783 if (index
->entries
) {
784 g_array_free(index
->entries
, TRUE
);