2 * Copyright 2016 - 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/ctf-ir/stream.h>
33 #include <babeltrace/component/notification/iterator.h>
36 #include "../common/notif-iter/notif-iter.h"
38 #include "data-stream.h"
40 #define PRINT_ERR_STREAM ctf_fs->error_fp
41 #define PRINT_PREFIX "ctf-fs-data-stream"
45 size_t remaining_mmap_bytes(struct ctf_fs_stream
*stream
)
47 return stream
->mmap_valid_len
- stream
->request_offset
;
51 int stream_munmap(struct ctf_fs_stream
*stream
)
54 struct ctf_fs_component
*ctf_fs
= stream
->file
->ctf_fs
;
56 if (munmap(stream
->mmap_addr
, stream
->mmap_len
)) {
57 PERR("Cannot memory-unmap address %p (size %zu) of file \"%s\" (%p): %s\n",
58 stream
->mmap_addr
, stream
->mmap_len
,
59 stream
->file
->path
->str
, stream
->file
->fp
,
69 enum bt_ctf_notif_iter_medium_status
mmap_next(struct ctf_fs_stream
*stream
)
71 enum bt_ctf_notif_iter_medium_status ret
=
72 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
;
73 struct ctf_fs_component
*ctf_fs
= stream
->file
->ctf_fs
;
75 /* Unmap old region */
76 if (stream
->mmap_addr
) {
77 if (stream_munmap(stream
)) {
81 stream
->mmap_offset
+= stream
->mmap_valid_len
;
82 stream
->request_offset
= 0;
85 stream
->mmap_valid_len
= MIN(stream
->file
->size
- stream
->mmap_offset
,
86 stream
->mmap_max_len
);
87 if (stream
->mmap_valid_len
== 0) {
88 ret
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF
;
91 /* Round up to next page, assuming page size being a power of 2. */
92 stream
->mmap_len
= (stream
->mmap_valid_len
+ ctf_fs
->page_size
- 1)
93 & ~(ctf_fs
->page_size
- 1);
95 assert(stream
->mmap_len
);
96 stream
->mmap_addr
= mmap((void *) 0, stream
->mmap_len
,
97 PROT_READ
, MAP_PRIVATE
, fileno(stream
->file
->fp
),
99 if (stream
->mmap_addr
== MAP_FAILED
) {
100 PERR("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %zu: %s\n",
101 stream
->mmap_len
, stream
->file
->path
->str
,
102 stream
->file
->fp
, stream
->mmap_offset
,
109 stream_munmap(stream
);
110 ret
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
116 enum bt_ctf_notif_iter_medium_status
medop_request_bytes(
117 size_t request_sz
, uint8_t **buffer_addr
,
118 size_t *buffer_sz
, void *data
)
120 enum bt_ctf_notif_iter_medium_status status
=
121 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
;
122 struct ctf_fs_stream
*stream
= data
;
123 struct ctf_fs_component
*ctf_fs
= stream
->file
->ctf_fs
;
125 if (request_sz
== 0) {
129 /* Check if we have at least one memory-mapped byte left */
130 if (remaining_mmap_bytes(stream
) == 0) {
131 /* Are we at the end of the file? */
132 if (stream
->mmap_offset
>= stream
->file
->size
) {
133 PDBG("Reached end of file \"%s\" (%p)\n",
134 stream
->file
->path
->str
, stream
->file
->fp
);
135 status
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF
;
139 status
= mmap_next(stream
);
141 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
:
143 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF
:
146 PERR("Cannot memory-map next region of file \"%s\" (%p)\n",
147 stream
->file
->path
->str
,
153 *buffer_sz
= MIN(remaining_mmap_bytes(stream
), request_sz
);
154 *buffer_addr
= ((uint8_t *) stream
->mmap_addr
) + stream
->request_offset
;
155 stream
->request_offset
+= *buffer_sz
;
159 status
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
166 struct bt_ctf_stream
*medop_get_stream(
167 struct bt_ctf_stream_class
*stream_class
, void *data
)
169 struct ctf_fs_stream
*fs_stream
= data
;
170 struct ctf_fs_component
*ctf_fs
= fs_stream
->file
->ctf_fs
;
172 if (!fs_stream
->stream
) {
173 int64_t id
= bt_ctf_stream_class_get_id(stream_class
);
175 PDBG("Creating stream out of stream class %" PRId64
"\n", id
);
176 fs_stream
->stream
= bt_ctf_stream_create(stream_class
,
177 fs_stream
->file
->path
->str
);
178 if (!fs_stream
->stream
) {
179 PERR("Cannot create stream (stream class %" PRId64
")\n",
184 return fs_stream
->stream
;
187 static struct bt_ctf_notif_iter_medium_ops medops
= {
188 .request_bytes
= medop_request_bytes
,
189 .get_stream
= medop_get_stream
,
193 int build_index_from_idx_file(struct ctf_fs_stream
*stream
)
196 gchar
*directory
= NULL
;
197 gchar
*basename
= NULL
;
198 GString
*index_basename
= NULL
;
199 gchar
*index_file_path
= NULL
;
200 GMappedFile
*mapped_file
= NULL
;
202 const struct ctf_packet_index_file_hdr
*header
;
203 const char *mmap_begin
, *file_pos
;
204 struct index_entry
*index
;
205 uint64_t total_packets_size
= 0;
206 size_t file_index_entry_size
;
207 size_t file_entry_count
;
210 /* Look for index file in relative path index/name.idx. */
211 basename
= g_path_get_basename(stream
->file
->path
->str
);
217 directory
= g_path_get_dirname(stream
->file
->path
->str
);
223 index_basename
= g_string_new(basename
);
224 if (!index_basename
) {
229 g_string_append(index_basename
, ".idx");
230 index_file_path
= g_build_filename(directory
, "index",
231 index_basename
->str
, NULL
);
232 mapped_file
= g_mapped_file_new(index_file_path
, FALSE
, NULL
);
237 filesize
= g_mapped_file_get_length(mapped_file
);
238 if (filesize
< sizeof(*header
)) {
239 printf_error("Invalid LTTng trace index: file size < header size");
244 mmap_begin
= g_mapped_file_get_contents(mapped_file
);
245 header
= (struct ctf_packet_index_file_hdr
*) mmap_begin
;
247 file_pos
= g_mapped_file_get_contents(mapped_file
) + sizeof(*header
);
248 if (be32toh(header
->magic
) != CTF_INDEX_MAGIC
) {
249 printf_error("Invalid LTTng trace index: \"magic\" validation failed");
254 file_index_entry_size
= be32toh(header
->packet_index_len
);
255 file_entry_count
= (filesize
- sizeof(*header
)) / file_index_entry_size
;
256 if ((filesize
- sizeof(*header
)) % (file_entry_count
* file_index_entry_size
)) {
257 printf_error("Invalid index file size; not a multiple of index entry size");
262 stream
->index
.entries
= g_array_sized_new(FALSE
, TRUE
,
263 sizeof(struct index_entry
), file_entry_count
);
264 if (!stream
->index
.entries
) {
268 index
= (struct index_entry
*) stream
->index
.entries
->data
;
269 for (i
= 0; i
< file_entry_count
; i
++) {
270 struct ctf_packet_index
*file_index
=
271 (struct ctf_packet_index
*) file_pos
;
272 uint64_t packet_size
= be64toh(file_index
->packet_size
);
274 if (packet_size
% CHAR_BIT
) {
276 printf_error("Invalid packet size encountered in index file");
280 /* Convert size in bits to bytes. */
281 packet_size
/= CHAR_BIT
;
282 index
->packet_size
= packet_size
;
284 index
->offset
= be64toh(file_index
->offset
);
285 if (i
!= 0 && index
->offset
< (index
- 1)->offset
) {
286 printf_error("Invalid, non-monotonic, packet offset encountered in index file");
291 index
->timestamp_begin
= be64toh(file_index
->timestamp_begin
);
292 index
->timestamp_end
= be64toh(file_index
->timestamp_end
);
293 if (index
->timestamp_end
< index
->timestamp_begin
) {
294 printf_error("Invalid packet time bounds encountered in index file");
299 total_packets_size
+= packet_size
;
300 file_pos
+= file_index_entry_size
;
304 /* Validate that the index addresses the complete stream. */
305 if (stream
->file
->size
!= total_packets_size
) {
306 printf_error("Invalid index; indexed size != stream file size");
313 g_free(index_file_path
);
314 if (index_basename
) {
315 g_string_free(index_basename
, TRUE
);
318 g_mapped_file_unref(mapped_file
);
322 g_array_free(stream
->index
.entries
, TRUE
);
327 int build_index_from_stream(struct ctf_fs_stream
*stream
)
335 int init_stream_index(struct ctf_fs_stream
*stream
)
339 ret
= build_index_from_idx_file(stream
);
344 ret
= build_index_from_stream(stream
);
350 struct ctf_fs_stream
*ctf_fs_stream_create(
351 struct ctf_fs_component
*ctf_fs
, struct ctf_fs_file
*file
)
354 struct ctf_fs_stream
*stream
= g_new0(struct ctf_fs_stream
, 1);
361 stream
->notif_iter
= bt_ctf_notif_iter_create(ctf_fs
->metadata
->trace
,
362 ctf_fs
->page_size
, medops
, stream
, ctf_fs
->error_fp
);
363 if (!stream
->notif_iter
) {
367 stream
->mmap_max_len
= ctf_fs
->page_size
* 2048;
368 ret
= init_stream_index(stream
);
374 /* Do not touch "borrowed" file. */
376 ctf_fs_stream_destroy(stream
);
383 void ctf_fs_stream_destroy(struct ctf_fs_stream
*stream
)
390 ctf_fs_file_destroy(stream
->file
);
393 if (stream
->stream
) {
394 BT_PUT(stream
->stream
);
397 if (stream
->notif_iter
) {
398 bt_ctf_notif_iter_destroy(stream
->notif_iter
);
401 if (stream
->index
.entries
) {
402 g_array_free(stream
->index
.entries
, TRUE
);