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 <babeltrace2/compat/mman-internal.h>
33 #include <babeltrace2/endian-internal.h>
34 #include <babeltrace2/babeltrace.h>
35 #include <babeltrace2/common-internal.h>
38 #include "../common/msg-iter/msg-iter.h"
39 #include <babeltrace2/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(bt_stream_class
*stream_class
, int64_t stream_id
,
179 struct ctf_fs_ds_file
*ds_file
= data
;
180 bt_stream_class
*ds_file_stream_class
;
181 bt_stream
*stream
= NULL
;
183 ds_file_stream_class
= bt_stream_borrow_class(
186 if (stream_class
!= ds_file_stream_class
) {
188 * Not supported: two packets described by two different
189 * stream classes within the same data stream file.
194 stream
= ds_file
->stream
;
201 enum bt_msg_iter_medium_status
medop_seek(enum bt_msg_iter_seek_whence whence
,
202 off_t offset
, void *data
)
204 enum bt_msg_iter_medium_status ret
=
205 BT_MSG_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_MSG_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_MSG_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_MSG_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_MSG_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_msg_iter_medium_ops ctf_fs_ds_file_medops
= {
254 .request_bytes
= medop_request_bytes
,
255 .borrow_stream
= medop_borrow_stream
,
260 int convert_cycles_to_ns(struct ctf_clock_class
*clock_class
,
261 uint64_t cycles
, int64_t *ns
)
263 return bt_util_clock_cycles_to_ns_from_origin(cycles
,
264 clock_class
->frequency
, clock_class
->offset_seconds
,
265 clock_class
->offset_cycles
, ns
);
269 struct ctf_fs_ds_index
*build_index_from_idx_file(
270 struct ctf_fs_ds_file
*ds_file
)
273 gchar
*directory
= NULL
;
274 gchar
*basename
= NULL
;
275 GString
*index_basename
= NULL
;
276 gchar
*index_file_path
= NULL
;
277 GMappedFile
*mapped_file
= NULL
;
279 const char *mmap_begin
= NULL
, *file_pos
= NULL
;
280 const struct ctf_packet_index_file_hdr
*header
= NULL
;
281 struct ctf_fs_ds_index
*index
= NULL
;
282 struct ctf_fs_ds_index_entry
*index_entry
= NULL
;
283 uint64_t total_packets_size
= 0;
284 size_t file_index_entry_size
;
285 size_t file_entry_count
;
287 struct ctf_stream_class
*sc
;
288 struct bt_msg_iter_packet_properties props
;
290 BT_LOGD("Building index from .idx file of stream file %s",
291 ds_file
->file
->path
->str
);
292 ret
= bt_msg_iter_get_packet_properties(ds_file
->msg_iter
, &props
);
294 BT_LOGD_STR("Cannot read first packet's header and context fields.");
298 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
,
299 props
.stream_class_id
);
301 if (!sc
->default_clock_class
) {
302 BT_LOGD_STR("Cannot find stream class's default clock class.");
306 /* Look for index file in relative path index/name.idx. */
307 basename
= g_path_get_basename(ds_file
->file
->path
->str
);
309 BT_LOGE("Cannot get the basename of datastream file %s",
310 ds_file
->file
->path
->str
);
314 directory
= g_path_get_dirname(ds_file
->file
->path
->str
);
316 BT_LOGE("Cannot get dirname of datastream file %s",
317 ds_file
->file
->path
->str
);
321 index_basename
= g_string_new(basename
);
322 if (!index_basename
) {
323 BT_LOGE_STR("Cannot allocate index file basename string");
327 g_string_append(index_basename
, ".idx");
328 index_file_path
= g_build_filename(directory
, "index",
329 index_basename
->str
, NULL
);
330 mapped_file
= g_mapped_file_new(index_file_path
, FALSE
, NULL
);
332 BT_LOGD("Cannot create new mapped file %s",
338 * The g_mapped_file API limits us to 4GB files on 32-bit.
339 * Traces with such large indexes have never been seen in the wild,
340 * but this would need to be adjusted to support them.
342 filesize
= g_mapped_file_get_length(mapped_file
);
343 if (filesize
< sizeof(*header
)) {
344 BT_LOGW("Invalid LTTng trace index file: "
345 "file size (%zu bytes) < header size (%zu bytes)",
346 filesize
, sizeof(*header
));
350 mmap_begin
= g_mapped_file_get_contents(mapped_file
);
351 header
= (struct ctf_packet_index_file_hdr
*) mmap_begin
;
353 file_pos
= g_mapped_file_get_contents(mapped_file
) + sizeof(*header
);
354 if (be32toh(header
->magic
) != CTF_INDEX_MAGIC
) {
355 BT_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
359 file_index_entry_size
= be32toh(header
->packet_index_len
);
360 file_entry_count
= (filesize
- sizeof(*header
)) / file_index_entry_size
;
361 if ((filesize
- sizeof(*header
)) % file_index_entry_size
) {
362 BT_LOGW("Invalid LTTng trace index: the index's size after the header "
363 "(%zu bytes) is not a multiple of the index entry size "
364 "(%zu bytes)", (filesize
- sizeof(*header
)),
369 index
= ctf_fs_ds_index_create();
374 for (i
= 0; i
< file_entry_count
; i
++) {
375 struct ctf_packet_index
*file_index
=
376 (struct ctf_packet_index
*) file_pos
;
377 uint64_t packet_size
= be64toh(file_index
->packet_size
);
379 if (packet_size
% CHAR_BIT
) {
380 BT_LOGW("Invalid packet size encountered in LTTng trace index file");
384 index_entry
= g_new0(struct ctf_fs_ds_index_entry
, 1);
389 /* Convert size in bits to bytes. */
390 packet_size
/= CHAR_BIT
;
391 index_entry
->packet_size
= packet_size
;
393 index_entry
->offset
= be64toh(file_index
->offset
);
394 if (i
!= 0 && index_entry
->offset
< (index_entry
- 1)->offset
) {
395 BT_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
396 "previous offset=%" PRIu64
", current offset=%" PRIu64
,
397 (index_entry
- 1)->offset
, index_entry
->offset
);
401 index_entry
->timestamp_begin
= be64toh(file_index
->timestamp_begin
);
402 index_entry
->timestamp_end
= be64toh(file_index
->timestamp_end
);
403 if (index_entry
->timestamp_end
< index_entry
->timestamp_begin
) {
404 BT_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
405 "timestamp_begin=%" PRIu64
"timestamp_end=%" PRIu64
,
406 index_entry
->timestamp_begin
,
407 index_entry
->timestamp_end
);
411 /* Convert the packet's bound to nanoseconds since Epoch. */
412 ret
= convert_cycles_to_ns(sc
->default_clock_class
,
413 index_entry
->timestamp_begin
,
414 &index_entry
->timestamp_begin_ns
);
416 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
419 ret
= convert_cycles_to_ns(sc
->default_clock_class
,
420 index_entry
->timestamp_end
,
421 &index_entry
->timestamp_end_ns
);
423 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
427 total_packets_size
+= packet_size
;
428 file_pos
+= file_index_entry_size
;
430 g_ptr_array_add(index
->entries
, index_entry
);
433 /* Validate that the index addresses the complete stream. */
434 if (ds_file
->file
->size
!= total_packets_size
) {
435 BT_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
436 "file-size=%" PRIu64
", total-packets-size=%" PRIu64
,
437 ds_file
->file
->size
, total_packets_size
);
443 g_free(index_file_path
);
444 if (index_basename
) {
445 g_string_free(index_basename
, TRUE
);
448 g_mapped_file_unref(mapped_file
);
452 ctf_fs_ds_index_destroy(index
);
459 int init_index_entry(struct ctf_fs_ds_index_entry
*entry
,
460 struct ctf_fs_ds_file
*ds_file
,
461 struct bt_msg_iter_packet_properties
*props
,
462 off_t packet_size
, off_t packet_offset
)
465 struct ctf_stream_class
*sc
;
467 sc
= ctf_trace_class_borrow_stream_class_by_id(ds_file
->metadata
->tc
,
468 props
->stream_class_id
);
470 BT_ASSERT(packet_offset
>= 0);
471 entry
->offset
= packet_offset
;
472 BT_ASSERT(packet_size
>= 0);
473 entry
->packet_size
= packet_size
;
475 if (props
->snapshots
.beginning_clock
!= UINT64_C(-1)) {
476 /* Convert the packet's bound to nanoseconds since Epoch. */
477 ret
= convert_cycles_to_ns(sc
->default_clock_class
,
478 props
->snapshots
.beginning_clock
,
479 &entry
->timestamp_begin_ns
);
481 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
485 entry
->timestamp_begin_ns
= UINT64_C(-1);
488 if (props
->snapshots
.end_clock
!= UINT64_C(-1)) {
489 ret
= convert_cycles_to_ns(sc
->default_clock_class
,
490 props
->snapshots
.end_clock
,
491 &entry
->timestamp_end_ns
);
493 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
497 entry
->timestamp_end_ns
= UINT64_C(-1);
505 struct ctf_fs_ds_index
*build_index_from_stream_file(
506 struct ctf_fs_ds_file
*ds_file
)
509 struct ctf_fs_ds_index
*index
= NULL
;
510 enum bt_msg_iter_status iter_status
= BT_MSG_ITER_STATUS_OK
;
511 off_t current_packet_offset_bytes
= 0;
513 BT_LOGD("Indexing stream file %s", ds_file
->file
->path
->str
);
515 index
= ctf_fs_ds_index_create();
521 off_t current_packet_size_bytes
;
522 struct ctf_fs_ds_index_entry
*index_entry
;
523 struct bt_msg_iter_packet_properties props
;
525 if (current_packet_offset_bytes
< 0) {
526 BT_LOGE_STR("Cannot get the current packet's offset.");
528 } else if (current_packet_offset_bytes
> ds_file
->file
->size
) {
529 BT_LOGE_STR("Unexpected current packet's offset (larger than file).");
531 } else if (current_packet_offset_bytes
== ds_file
->file
->size
) {
536 iter_status
= bt_msg_iter_seek(ds_file
->msg_iter
,
537 current_packet_offset_bytes
);
538 if (iter_status
!= BT_MSG_ITER_STATUS_OK
) {
542 iter_status
= bt_msg_iter_get_packet_properties(
543 ds_file
->msg_iter
, &props
);
544 if (iter_status
!= BT_MSG_ITER_STATUS_OK
) {
548 if (props
.exp_packet_total_size
>= 0) {
549 current_packet_size_bytes
=
550 (uint64_t) props
.exp_packet_total_size
/ 8;
552 current_packet_size_bytes
= ds_file
->file
->size
;
555 if (current_packet_offset_bytes
+ current_packet_size_bytes
>
556 ds_file
->file
->size
) {
557 BT_LOGW("Invalid packet size reported in file: stream=\"%s\", "
558 "packet-offset=%jd, packet-size-bytes=%jd, "
560 ds_file
->file
->path
->str
,
561 current_packet_offset_bytes
,
562 current_packet_size_bytes
,
563 ds_file
->file
->size
);
567 current_packet_offset_bytes
+= current_packet_size_bytes
;
568 BT_LOGD("Seeking to next packet: current-packet-offset=%jd, "
569 "next-packet-offset=%jd",
570 current_packet_offset_bytes
- current_packet_size_bytes
,
571 current_packet_offset_bytes
);
573 index_entry
= g_new0(struct ctf_fs_ds_index_entry
, 1);
575 BT_LOGE_STR("Failed to allocate a new index entry.");
579 ret
= init_index_entry(index_entry
, ds_file
, &props
,
580 current_packet_size_bytes
, current_packet_offset_bytes
);
586 g_ptr_array_add(index
->entries
, index_entry
);
588 } while (iter_status
== BT_MSG_ITER_STATUS_OK
);
590 if (iter_status
!= BT_MSG_ITER_STATUS_OK
) {
598 ctf_fs_ds_index_destroy(index
);
604 struct ctf_fs_ds_file
*ctf_fs_ds_file_create(
605 struct ctf_fs_trace
*ctf_fs_trace
,
606 bt_self_message_iterator
*pc_msg_iter
,
607 struct bt_msg_iter
*msg_iter
,
608 bt_stream
*stream
, const char *path
)
611 const size_t page_size
= bt_common_get_page_size();
612 struct ctf_fs_ds_file
*ds_file
= g_new0(struct ctf_fs_ds_file
, 1);
618 ds_file
->pc_msg_iter
= pc_msg_iter
;
619 ds_file
->file
= ctf_fs_file_create();
620 if (!ds_file
->file
) {
624 ds_file
->stream
= stream
;
625 bt_stream_get_ref(ds_file
->stream
);
626 ds_file
->metadata
= ctf_fs_trace
->metadata
;
627 g_string_assign(ds_file
->file
->path
, path
);
628 ret
= ctf_fs_file_open(ds_file
->file
, "rb");
633 ds_file
->msg_iter
= msg_iter
;
634 bt_msg_iter_set_medops_data(ds_file
->msg_iter
, ds_file
);
635 if (!ds_file
->msg_iter
) {
639 ds_file
->mmap_max_len
= page_size
* 2048;
644 /* Do not touch "borrowed" file. */
645 ctf_fs_ds_file_destroy(ds_file
);
653 struct ctf_fs_ds_index
*ctf_fs_ds_file_build_index(
654 struct ctf_fs_ds_file
*ds_file
)
656 struct ctf_fs_ds_index
*index
;
658 index
= build_index_from_idx_file(ds_file
);
663 BT_LOGD("Failed to build index from .index file; "
664 "falling back to stream indexing.");
665 index
= build_index_from_stream_file(ds_file
);
671 struct ctf_fs_ds_index
*ctf_fs_ds_index_create()
673 struct ctf_fs_ds_index
*index
= g_new0(struct ctf_fs_ds_index
, 1);
676 BT_LOGE_STR("Failed to allocate index");
680 index
->entries
= g_ptr_array_new_with_free_func((GDestroyNotify
) g_free
);
681 if (!index
->entries
) {
682 BT_LOGE("Failed to allocate index entries.");
689 ctf_fs_ds_index_destroy(index
);
696 void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file
*ds_file
)
702 bt_stream_put_ref(ds_file
->stream
);
703 (void) ds_file_munmap(ds_file
);
706 ctf_fs_file_destroy(ds_file
->file
);
713 bt_self_message_iterator_status
ctf_fs_ds_file_next(
714 struct ctf_fs_ds_file
*ds_file
,
717 enum bt_msg_iter_status msg_iter_status
;
718 bt_self_message_iterator_status status
;
720 msg_iter_status
= bt_msg_iter_get_next_message(
721 ds_file
->msg_iter
, ds_file
->pc_msg_iter
, msg
);
723 switch (msg_iter_status
) {
724 case BT_MSG_ITER_STATUS_EOF
:
725 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_END
;
727 case BT_MSG_ITER_STATUS_OK
:
728 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_OK
;
730 case BT_MSG_ITER_STATUS_AGAIN
:
732 * Should not make it this far as this is
733 * medium-specific; there is nothing for the user to do
734 * and it should have been handled upstream.
737 case BT_MSG_ITER_STATUS_INVAL
:
738 case BT_MSG_ITER_STATUS_ERROR
:
740 status
= BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
;
747 void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index
*index
)
753 if (index
->entries
) {
754 g_ptr_array_free(index
->entries
, TRUE
);