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>
41 #include "../common/notif-iter/notif-iter.h"
43 #include "data-stream-file.h"
46 #define PRINT_ERR_STREAM ctf_fs->error_fp
47 #define PRINT_PREFIX "ctf-fs-data-stream"
48 #define PRINT_DBG_CHECK ctf_fs_debug
52 size_t remaining_mmap_bytes(struct ctf_fs_ds_file
*ds_file
)
54 return ds_file
->mmap_valid_len
- ds_file
->request_offset
;
58 int ds_file_munmap(struct ctf_fs_ds_file
*ds_file
)
61 struct ctf_fs_component
*ctf_fs
;
63 if (!ds_file
|| !ds_file
->mmap_addr
) {
67 ctf_fs
= ds_file
->file
->ctf_fs
;
68 if (munmap(ds_file
->mmap_addr
, ds_file
->mmap_len
)) {
69 PERR("Cannot memory-unmap address %p (size %zu) of file \"%s\" (%p): %s\n",
70 ds_file
->mmap_addr
, ds_file
->mmap_len
,
71 ds_file
->file
->path
->str
, ds_file
->file
->fp
,
77 ds_file
->mmap_addr
= NULL
;
84 enum bt_ctf_notif_iter_medium_status
ds_file_mmap_next(
85 struct ctf_fs_ds_file
*ds_file
)
87 enum bt_ctf_notif_iter_medium_status ret
=
88 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
;
89 struct ctf_fs_component
*ctf_fs
= ds_file
->file
->ctf_fs
;
91 /* Unmap old region */
92 if (ds_file
->mmap_addr
) {
93 if (ds_file_munmap(ds_file
)) {
97 ds_file
->mmap_offset
+= ds_file
->mmap_valid_len
;
98 ds_file
->request_offset
= 0;
101 ds_file
->mmap_valid_len
= MIN(ds_file
->file
->size
- ds_file
->mmap_offset
,
102 ds_file
->mmap_max_len
);
103 if (ds_file
->mmap_valid_len
== 0) {
104 ret
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF
;
107 /* Round up to next page, assuming page size being a power of 2. */
108 ds_file
->mmap_len
= (ds_file
->mmap_valid_len
+ ctf_fs
->page_size
- 1)
109 & ~(ctf_fs
->page_size
- 1);
111 assert(ds_file
->mmap_len
);
112 ds_file
->mmap_addr
= mmap((void *) 0, ds_file
->mmap_len
,
113 PROT_READ
, MAP_PRIVATE
, fileno(ds_file
->file
->fp
),
114 ds_file
->mmap_offset
);
115 if (ds_file
->mmap_addr
== MAP_FAILED
) {
116 PERR("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %zu: %s\n",
117 ds_file
->mmap_len
, ds_file
->file
->path
->str
,
118 ds_file
->file
->fp
, ds_file
->mmap_offset
,
125 ds_file_munmap(ds_file
);
126 ret
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
132 enum bt_ctf_notif_iter_medium_status
medop_request_bytes(
133 size_t request_sz
, uint8_t **buffer_addr
,
134 size_t *buffer_sz
, void *data
)
136 enum bt_ctf_notif_iter_medium_status status
=
137 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
;
138 struct ctf_fs_ds_file
*ds_file
= data
;
139 struct ctf_fs_component
*ctf_fs
= ds_file
->file
->ctf_fs
;
141 if (request_sz
== 0) {
145 /* Check if we have at least one memory-mapped byte left */
146 if (remaining_mmap_bytes(ds_file
) == 0) {
147 /* Are we at the end of the file? */
148 if (ds_file
->mmap_offset
>= ds_file
->file
->size
) {
149 PDBG("Reached end of file \"%s\" (%p)\n",
150 ds_file
->file
->path
->str
, ds_file
->file
->fp
);
151 status
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF
;
155 status
= ds_file_mmap_next(ds_file
);
157 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
:
159 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF
:
162 PERR("Cannot memory-map next region of file \"%s\" (%p)\n",
163 ds_file
->file
->path
->str
,
169 *buffer_sz
= MIN(remaining_mmap_bytes(ds_file
), request_sz
);
170 *buffer_addr
= ((uint8_t *) ds_file
->mmap_addr
) + ds_file
->request_offset
;
171 ds_file
->request_offset
+= *buffer_sz
;
175 status
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
182 struct bt_ctf_stream
*medop_get_stream(
183 struct bt_ctf_stream_class
*stream_class
, void *data
)
185 struct ctf_fs_ds_file
*ds_file
= data
;
186 struct bt_ctf_stream_class
*ds_file_stream_class
;
187 struct bt_ctf_stream
*stream
= NULL
;
189 ds_file_stream_class
= bt_ctf_stream_get_class(ds_file
->stream
);
190 bt_put(ds_file_stream_class
);
192 if (stream_class
!= ds_file_stream_class
) {
194 * Not supported: two packets described by two different
195 * stream classes within the same data stream file.
200 stream
= ds_file
->stream
;
206 static struct bt_ctf_notif_iter_medium_ops medops
= {
207 .request_bytes
= medop_request_bytes
,
208 .get_stream
= medop_get_stream
,
212 int build_index_from_idx_file(struct ctf_fs_ds_file
*ds_file
)
215 gchar
*directory
= NULL
;
216 gchar
*basename
= NULL
;
217 GString
*index_basename
= NULL
;
218 gchar
*index_file_path
= NULL
;
219 GMappedFile
*mapped_file
= NULL
;
221 const struct ctf_packet_index_file_hdr
*header
;
222 const char *mmap_begin
, *file_pos
;
223 struct index_entry
*index
;
224 uint64_t total_packets_size
= 0;
225 size_t file_index_entry_size
;
226 size_t file_entry_count
;
229 /* Look for index file in relative path index/name.idx. */
230 basename
= g_path_get_basename(ds_file
->file
->path
->str
);
236 directory
= g_path_get_dirname(ds_file
->file
->path
->str
);
242 index_basename
= g_string_new(basename
);
243 if (!index_basename
) {
248 g_string_append(index_basename
, ".idx");
249 index_file_path
= g_build_filename(directory
, "index",
250 index_basename
->str
, NULL
);
251 mapped_file
= g_mapped_file_new(index_file_path
, FALSE
, NULL
);
256 filesize
= g_mapped_file_get_length(mapped_file
);
257 if (filesize
< sizeof(*header
)) {
258 printf_error("Invalid LTTng trace index: file size < header size");
263 mmap_begin
= g_mapped_file_get_contents(mapped_file
);
264 header
= (struct ctf_packet_index_file_hdr
*) mmap_begin
;
266 file_pos
= g_mapped_file_get_contents(mapped_file
) + sizeof(*header
);
267 if (be32toh(header
->magic
) != CTF_INDEX_MAGIC
) {
268 printf_error("Invalid LTTng trace index: \"magic\" validation failed");
273 file_index_entry_size
= be32toh(header
->packet_index_len
);
274 file_entry_count
= (filesize
- sizeof(*header
)) / file_index_entry_size
;
275 if ((filesize
- sizeof(*header
)) % (file_entry_count
* file_index_entry_size
)) {
276 printf_error("Invalid index file size; not a multiple of index entry size");
281 ds_file
->index
.entries
= g_array_sized_new(FALSE
, TRUE
,
282 sizeof(struct index_entry
), file_entry_count
);
283 if (!ds_file
->index
.entries
) {
287 index
= (struct index_entry
*) ds_file
->index
.entries
->data
;
288 for (i
= 0; i
< file_entry_count
; i
++) {
289 struct ctf_packet_index
*file_index
=
290 (struct ctf_packet_index
*) file_pos
;
291 uint64_t packet_size
= be64toh(file_index
->packet_size
);
293 if (packet_size
% CHAR_BIT
) {
295 printf_error("Invalid packet size encountered in index file");
299 /* Convert size in bits to bytes. */
300 packet_size
/= CHAR_BIT
;
301 index
->packet_size
= packet_size
;
303 index
->offset
= be64toh(file_index
->offset
);
304 if (i
!= 0 && index
->offset
< (index
- 1)->offset
) {
305 printf_error("Invalid, non-monotonic, packet offset encountered in index file");
310 index
->timestamp_begin
= be64toh(file_index
->timestamp_begin
);
311 index
->timestamp_end
= be64toh(file_index
->timestamp_end
);
312 if (index
->timestamp_end
< index
->timestamp_begin
) {
313 printf_error("Invalid packet time bounds encountered in index file");
318 total_packets_size
+= packet_size
;
319 file_pos
+= file_index_entry_size
;
323 /* Validate that the index addresses the complete stream. */
324 if (ds_file
->file
->size
!= total_packets_size
) {
325 printf_error("Invalid index; indexed size != stream file size");
332 g_free(index_file_path
);
333 if (index_basename
) {
334 g_string_free(index_basename
, TRUE
);
337 g_mapped_file_unref(mapped_file
);
341 g_array_free(ds_file
->index
.entries
, TRUE
);
346 int build_index_from_data_stream_file(struct ctf_fs_ds_file
*stream
)
352 int init_stream_index(struct ctf_fs_ds_file
*ds_file
)
356 ret
= build_index_from_idx_file(ds_file
);
361 ret
= build_index_from_data_stream_file(ds_file
);
367 struct ctf_fs_ds_file
*ctf_fs_ds_file_create(
368 struct ctf_fs_trace
*ctf_fs_trace
,
369 struct bt_ctf_stream
*stream
, const char *path
,
373 struct ctf_fs_ds_file
*ds_file
= g_new0(struct ctf_fs_ds_file
, 1);
379 ds_file
->file
= ctf_fs_file_create(ctf_fs_trace
->ctf_fs
);
380 if (!ds_file
->file
) {
384 ds_file
->stream
= bt_get(stream
);
385 ds_file
->cc_prio_map
= bt_get(ctf_fs_trace
->cc_prio_map
);
386 g_string_assign(ds_file
->file
->path
, path
);
387 ret
= ctf_fs_file_open(ctf_fs_trace
->ctf_fs
, ds_file
->file
, "rb");
392 ds_file
->notif_iter
= bt_ctf_notif_iter_create(
393 ctf_fs_trace
->metadata
->trace
,
394 ctf_fs_trace
->ctf_fs
->page_size
, medops
, ds_file
,
395 ctf_fs_trace
->ctf_fs
->error_fp
);
396 if (!ds_file
->notif_iter
) {
400 ds_file
->mmap_max_len
= ctf_fs_trace
->ctf_fs
->page_size
* 2048;
403 ret
= init_stream_index(ds_file
);
412 /* Do not touch "borrowed" file. */
413 ctf_fs_ds_file_destroy(ds_file
);
421 void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file
*ds_file
)
427 bt_put(ds_file
->cc_prio_map
);
428 bt_put(ds_file
->stream
);
429 (void) ds_file_munmap(ds_file
);
432 ctf_fs_file_destroy(ds_file
->file
);
435 if (ds_file
->notif_iter
) {
436 bt_ctf_notif_iter_destroy(ds_file
->notif_iter
);
439 if (ds_file
->index
.entries
) {
440 g_array_free(ds_file
->index
.entries
, TRUE
);
447 struct bt_notification_iterator_next_return
ctf_fs_ds_file_next(
448 struct ctf_fs_ds_file
*ds_file
)
450 enum bt_ctf_notif_iter_status notif_iter_status
;
451 struct bt_notification_iterator_next_return ret
= {
452 .status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
,
453 .notification
= NULL
,
456 notif_iter_status
= bt_ctf_notif_iter_get_next_notification(
457 ds_file
->notif_iter
, ds_file
->cc_prio_map
, &ret
.notification
);
459 switch (notif_iter_status
) {
460 case BT_CTF_NOTIF_ITER_STATUS_EOF
:
461 ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
463 case BT_CTF_NOTIF_ITER_STATUS_OK
:
464 ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
466 case BT_CTF_NOTIF_ITER_STATUS_AGAIN
:
468 * Should not make it this far as this is
469 * medium-specific; there is nothing for the user to do
470 * and it should have been handled upstream.
473 case BT_CTF_NOTIF_ITER_STATUS_INVAL
:
474 case BT_CTF_NOTIF_ITER_STATUS_ERROR
:
476 ret
.status
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;
484 int ctf_fs_ds_file_get_packet_header_context_fields(
485 struct ctf_fs_trace
*ctf_fs_trace
, const char *path
,
486 struct bt_ctf_field
**packet_header_field
,
487 struct bt_ctf_field
**packet_context_field
)
489 enum bt_ctf_notif_iter_status notif_iter_status
;
490 struct ctf_fs_ds_file
*ds_file
;
493 ds_file
= ctf_fs_ds_file_create(ctf_fs_trace
, NULL
, path
, false);
498 notif_iter_status
= bt_ctf_notif_iter_get_packet_header_context_fields(
499 ds_file
->notif_iter
, packet_header_field
, packet_context_field
);
500 switch (notif_iter_status
) {
501 case BT_CTF_NOTIF_ITER_STATUS_EOF
:
502 case BT_CTF_NOTIF_ITER_STATUS_OK
:
504 case BT_CTF_NOTIF_ITER_STATUS_AGAIN
:
506 case BT_CTF_NOTIF_ITER_STATUS_INVAL
:
507 case BT_CTF_NOTIF_ITER_STATUS_ERROR
:
518 if (packet_header_field
) {
519 bt_put(*packet_header_field
);
522 if (packet_context_field
) {
523 bt_put(*packet_context_field
);
527 ctf_fs_ds_file_destroy(ds_file
);