2 * Copyright 2016 - Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation
5 * Some functions are based on older functions written by Mathieu Desnoyers.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <babeltrace/ctf-ir/stream.h>
34 #include <babeltrace/plugin/notification/iterator.h>
37 #include "../common/notif-iter/notif-iter.h"
40 #define PRINT_ERR_STREAM ctf_fs->error_fp
41 #define PRINT_PREFIX "ctf-fs-data-stream"
44 static void ctf_fs_stream_destroy(struct ctf_fs_stream
*stream
)
47 ctf_fs_file_destroy(stream
->file
);
51 BT_PUT(stream
->stream
);
54 if (stream
->notif_iter
) {
55 bt_ctf_notif_iter_destroy(stream
->notif_iter
);
61 static size_t remaining_mmap_bytes(struct ctf_fs_stream
*stream
)
63 return stream
->mmap_offset
+ stream
->mmap_len
-
64 stream
->request_offset
;
67 static int stream_munmap(struct ctf_fs_stream
*stream
)
69 struct ctf_fs_component
*ctf_fs
= stream
->file
->ctf_fs
;
71 if (munmap(stream
->mmap_addr
, stream
->mmap_len
)) {
72 PERR("Cannot memory-unmap address %p (size %zu) of file \"%s\" (%p): %s\n",
73 stream
->mmap_addr
, stream
->mmap_len
,
74 stream
->file
->path
->str
, stream
->file
->fp
,
82 static int mmap_next(struct ctf_fs_stream
*stream
)
84 struct ctf_fs_component
*ctf_fs
= stream
->file
->ctf_fs
;
87 /* Unmap old region */
88 if (stream
->mmap_addr
) {
89 if (stream_munmap(stream
)) {
93 stream
->mmap_offset
+= stream
->mmap_len
;
94 stream
->request_offset
= stream
->mmap_offset
;
98 stream
->mmap_addr
= mmap((void *) 0, stream
->mmap_len
,
99 PROT_READ
, MAP_PRIVATE
, fileno(stream
->file
->fp
),
100 stream
->mmap_offset
);
101 if (stream
->mmap_addr
== MAP_FAILED
) {
102 PERR("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %zu: %s\n",
103 stream
->mmap_len
, stream
->file
->path
->str
,
104 stream
->file
->fp
, stream
->mmap_offset
,
111 stream_munmap(stream
);
117 static enum bt_ctf_notif_iter_medium_status
medop_request_bytes(
118 size_t request_sz
, uint8_t **buffer_addr
,
119 size_t *buffer_sz
, void *data
)
121 enum bt_ctf_notif_iter_medium_status status
=
122 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
;
123 struct ctf_fs_stream
*stream
= data
;
124 struct ctf_fs_component
*ctf_fs
= stream
->file
->ctf_fs
;
126 if (request_sz
== 0) {
130 /* Check if we need an initial memory map */
131 if (!stream
->mmap_addr
) {
132 if (mmap_next(stream
)) {
133 PERR("Cannot memory-map initial region of file \"%s\" (%p)\n",
134 stream
->file
->path
->str
, stream
->file
->fp
);
139 /* Check if we have at least one memory-mapped byte left */
140 if (remaining_mmap_bytes(stream
) == 0) {
141 /* Are we at the end of the file? */
142 if (stream
->request_offset
== stream
->file
->size
) {
143 PDBG("Reached end of file \"%s\" (%p)\n",
144 stream
->file
->path
->str
, stream
->file
->fp
);
145 status
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF
;
149 if (mmap_next(stream
)) {
150 PERR("Cannot memory-map next region of file \"%s\" (%p)\n",
151 stream
->file
->path
->str
, stream
->file
->fp
);
156 *buffer_sz
= MIN(remaining_mmap_bytes(stream
), request_sz
);
157 *buffer_addr
= ((uint8_t *) stream
->mmap_addr
) +
158 stream
->request_offset
- stream
->mmap_offset
;
159 stream
->request_offset
+= *buffer_sz
;
163 status
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
169 static struct bt_ctf_stream
*medop_get_stream(
170 struct bt_ctf_stream_class
*stream_class
, void *data
)
172 struct ctf_fs_stream
*fs_stream
= data
;
173 struct ctf_fs_component
*ctf_fs
= fs_stream
->file
->ctf_fs
;
175 if (!fs_stream
->stream
) {
176 int64_t id
= bt_ctf_stream_class_get_id(stream_class
);
178 PDBG("Creating stream out of stream class %" PRId64
"\n", id
);
179 fs_stream
->stream
= bt_ctf_stream_create(stream_class
,
180 fs_stream
->file
->path
->str
);
181 if (!fs_stream
->stream
) {
182 PERR("Cannot create stream (stream class %" PRId64
")\n",
187 return fs_stream
->stream
;
190 static struct bt_ctf_notif_iter_medium_ops medops
= {
191 .request_bytes
= medop_request_bytes
,
192 .get_stream
= medop_get_stream
,
195 static struct ctf_fs_stream
*ctf_fs_stream_create(
196 struct ctf_fs_component
*ctf_fs
, struct ctf_fs_file
*file
)
198 struct ctf_fs_stream
*stream
= g_new0(struct ctf_fs_stream
, 1);
205 stream
->notif_iter
= bt_ctf_notif_iter_create(ctf_fs
->metadata
.trace
,
206 12, medops
, stream
, ctf_fs
->error_fp
);
207 if (!stream
->notif_iter
) {
210 stream
->mmap_len
= ctf_fs
->page_size
;
213 /* Do not touch "borrowed" file. */
215 ctf_fs_stream_destroy(stream
);
221 int ctf_fs_data_stream_open_streams(struct ctf_fs_component
*ctf_fs
)
225 GError
*error
= NULL
;
226 GDir
*dir
= g_dir_open(ctf_fs
->trace_path
->str
, 0, &error
);
229 PERR("Cannot open directory \"%s\": %s (code %d)\n",
230 ctf_fs
->trace_path
->str
, error
->message
,
235 while ((name
= g_dir_read_name(dir
))) {
236 struct ctf_fs_file
*file
= NULL
;
237 struct ctf_fs_stream
*stream
= NULL
;
239 if (!strcmp(name
, CTF_FS_METADATA_FILENAME
)) {
240 /* Ignore the metadata stream. */
241 PDBG("Ignoring metadata file \"%s\"\n",
246 if (name
[0] == '.') {
247 PDBG("Ignoring hidden file \"%s\"\n",
252 /* Create the file. */
253 file
= ctf_fs_file_create(ctf_fs
);
255 PERR("Cannot create stream file object\n");
259 /* Create full path string. */
260 g_string_append_printf(file
->path
, "%s/%s",
261 ctf_fs
->trace_path
->str
, name
);
262 if (!g_file_test(file
->path
->str
, G_FILE_TEST_IS_REGULAR
)) {
263 PDBG("Ignoring non-regular file \"%s\"\n", name
);
264 ctf_fs_file_destroy(file
);
269 if (ctf_fs_file_open(ctf_fs
, file
, "rb")) {
270 ctf_fs_file_destroy(file
);
274 /* Create a private stream. */
275 stream
= ctf_fs_stream_create(ctf_fs
, file
);
277 ctf_fs_file_destroy(file
);
281 /* Append file to the array of files. */
282 g_ptr_array_add(ctf_fs
->data_stream
.streams
, stream
);
299 int ctf_fs_data_stream_init(struct ctf_fs_component
*ctf_fs
,
300 struct ctf_fs_data_stream
*data_stream
)
304 data_stream
->streams
= g_ptr_array_new_with_free_func(
305 (GDestroyNotify
) ctf_fs_stream_destroy
);
306 if (!data_stream
->streams
) {
307 PERR("Cannot allocate array of streams\n");
318 void ctf_fs_data_stream_fini(struct ctf_fs_data_stream
*data_stream
)
320 g_ptr_array_free(data_stream
->streams
, TRUE
);
323 enum bt_notification_iterator_status
ctf_fs_data_stream_get_next_notification(
324 struct ctf_fs_component
*ctf_fs
,
325 struct bt_notification
**notification
)
327 enum bt_ctf_notif_iter_status status
;
328 enum bt_notification_iterator_status ret
;
329 /* FIXME, only iterating on one stream for the moment. */
330 struct ctf_fs_stream
*stream
= g_ptr_array_index(
331 ctf_fs
->data_stream
.streams
, 0);
333 if (stream
->end_reached
) {
334 status
= BT_CTF_NOTIF_ITER_STATUS_EOF
;
338 status
= bt_ctf_notif_iter_get_next_notification(stream
->notif_iter
,
340 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
&&
341 status
!= BT_CTF_NOTIF_ITER_STATUS_EOF
) {
345 /* Should be handled in bt_ctf_notif_iter_get_next_notification. */
346 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
347 *notification
= bt_notification_stream_end_create(
349 if (!*notification
) {
350 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
352 status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
353 stream
->end_reached
= true;
357 case BT_CTF_NOTIF_ITER_STATUS_EOF
:
358 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
360 case BT_CTF_NOTIF_ITER_STATUS_OK
:
361 ret
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
363 case BT_CTF_NOTIF_ITER_STATUS_AGAIN
:
365 * Should not make it this far as this is medium-specific;
366 * there is nothing for the user to do and it should have been
370 case BT_CTF_NOTIF_ITER_STATUS_INVAL
:
371 /* No argument provided by the user, so don't return INVAL. */
372 case BT_CTF_NOTIF_ITER_STATUS_ERROR
:
374 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;