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"
45 void ctf_fs_stream_destroy(struct ctf_fs_stream
*stream
)
48 ctf_fs_file_destroy(stream
->file
);
52 BT_PUT(stream
->stream
);
55 if (stream
->notif_iter
) {
56 bt_ctf_notif_iter_destroy(stream
->notif_iter
);
62 static size_t remaining_mmap_bytes(struct ctf_fs_stream
*stream
)
64 return stream
->mmap_offset
+ stream
->mmap_len
-
65 stream
->request_offset
;
68 static int stream_munmap(struct ctf_fs_stream
*stream
)
70 struct ctf_fs_component
*ctf_fs
= stream
->file
->ctf_fs
;
72 if (munmap(stream
->mmap_addr
, stream
->mmap_len
)) {
73 PERR("Cannot memory-unmap address %p (size %zu) of file \"%s\" (%p): %s\n",
74 stream
->mmap_addr
, stream
->mmap_len
,
75 stream
->file
->path
->str
, stream
->file
->fp
,
83 static int mmap_next(struct ctf_fs_stream
*stream
)
85 struct ctf_fs_component
*ctf_fs
= stream
->file
->ctf_fs
;
88 /* Unmap old region */
89 if (stream
->mmap_addr
) {
90 if (stream_munmap(stream
)) {
94 stream
->mmap_offset
+= stream
->mmap_len
;
95 stream
->request_offset
= stream
->mmap_offset
;
99 stream
->mmap_addr
= mmap((void *) 0, stream
->mmap_len
,
100 PROT_READ
, MAP_PRIVATE
, fileno(stream
->file
->fp
),
101 stream
->mmap_offset
);
102 if (stream
->mmap_addr
== MAP_FAILED
) {
103 PERR("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %zu: %s\n",
104 stream
->mmap_len
, stream
->file
->path
->str
,
105 stream
->file
->fp
, stream
->mmap_offset
,
112 stream_munmap(stream
);
118 static enum bt_ctf_notif_iter_medium_status
medop_request_bytes(
119 size_t request_sz
, uint8_t **buffer_addr
,
120 size_t *buffer_sz
, void *data
)
122 enum bt_ctf_notif_iter_medium_status status
=
123 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
;
124 struct ctf_fs_stream
*stream
= data
;
125 struct ctf_fs_component
*ctf_fs
= stream
->file
->ctf_fs
;
127 if (request_sz
== 0) {
131 /* Check if we need an initial memory map */
132 if (!stream
->mmap_addr
) {
133 if (mmap_next(stream
)) {
134 PERR("Cannot memory-map initial region of file \"%s\" (%p)\n",
135 stream
->file
->path
->str
, stream
->file
->fp
);
140 /* Check if we have at least one memory-mapped byte left */
141 if (remaining_mmap_bytes(stream
) == 0) {
142 /* Are we at the end of the file? */
143 if (stream
->request_offset
== stream
->file
->size
) {
144 PDBG("Reached end of file \"%s\" (%p)\n",
145 stream
->file
->path
->str
, stream
->file
->fp
);
146 status
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF
;
150 if (mmap_next(stream
)) {
151 PERR("Cannot memory-map next region of file \"%s\" (%p)\n",
152 stream
->file
->path
->str
, stream
->file
->fp
);
157 *buffer_sz
= MIN(remaining_mmap_bytes(stream
), request_sz
);
158 *buffer_addr
= ((uint8_t *) stream
->mmap_addr
) +
159 stream
->request_offset
- stream
->mmap_offset
;
160 stream
->request_offset
+= *buffer_sz
;
164 status
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
170 static struct bt_ctf_stream
*medop_get_stream(
171 struct bt_ctf_stream_class
*stream_class
, void *data
)
173 struct ctf_fs_stream
*fs_stream
= data
;
174 struct ctf_fs_component
*ctf_fs
= fs_stream
->file
->ctf_fs
;
176 if (!fs_stream
->stream
) {
177 int64_t id
= bt_ctf_stream_class_get_id(stream_class
);
179 PDBG("Creating stream out of stream class %" PRId64
"\n", id
);
180 fs_stream
->stream
= bt_ctf_stream_create(stream_class
,
181 fs_stream
->file
->path
->str
);
182 if (!fs_stream
->stream
) {
183 PERR("Cannot create stream (stream class %" PRId64
")\n",
188 return fs_stream
->stream
;
191 static struct bt_ctf_notif_iter_medium_ops medops
= {
192 .request_bytes
= medop_request_bytes
,
193 .get_stream
= medop_get_stream
,
197 struct ctf_fs_stream
*ctf_fs_stream_create(
198 struct ctf_fs_component
*ctf_fs
, struct ctf_fs_file
*file
)
200 struct ctf_fs_stream
*stream
= g_new0(struct ctf_fs_stream
, 1);
207 stream
->notif_iter
= bt_ctf_notif_iter_create(ctf_fs
->metadata
.trace
,
208 ctf_fs
->page_size
, medops
, stream
, ctf_fs
->error_fp
);
209 if (!stream
->notif_iter
) {
212 stream
->mmap_len
= ctf_fs
->page_size
* 2048;
215 /* Do not touch "borrowed" file. */
217 ctf_fs_stream_destroy(stream
);
223 enum bt_notification_iterator_status
ctf_fs_data_stream_get_next_notification(
224 struct ctf_fs_component
*ctf_fs
,
225 struct bt_notification
**notification
)
227 enum bt_ctf_notif_iter_status status
;
228 enum bt_notification_iterator_status ret
;
229 /* FIXME, only iterating on one stream for the moment. */
230 struct ctf_fs_stream
*stream
= g_ptr_array_index(ctf_fs
->streams
, 0);
232 if (stream
->end_reached
) {
233 status
= BT_CTF_NOTIF_ITER_STATUS_EOF
;
237 status
= bt_ctf_notif_iter_get_next_notification(stream
->notif_iter
,
239 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
&&
240 status
!= BT_CTF_NOTIF_ITER_STATUS_EOF
) {
244 /* Should be handled in bt_ctf_notif_iter_get_next_notification. */
245 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
246 *notification
= bt_notification_stream_end_create(
248 if (!*notification
) {
249 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
251 status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
252 stream
->end_reached
= true;
256 case BT_CTF_NOTIF_ITER_STATUS_EOF
:
257 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
259 case BT_CTF_NOTIF_ITER_STATUS_OK
:
260 ret
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
262 case BT_CTF_NOTIF_ITER_STATUS_AGAIN
:
264 * Should not make it this far as this is medium-specific;
265 * there is nothing for the user to do and it should have been
269 case BT_CTF_NOTIF_ITER_STATUS_INVAL
:
270 /* No argument provided by the user, so don't return INVAL. */
271 case BT_CTF_NOTIF_ITER_STATUS_ERROR
:
273 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;