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/plugin/notification/iterator.h>
36 #include "../common/notif-iter/notif-iter.h"
39 #define PRINT_ERR_STREAM ctf_fs->error_fp
40 #define PRINT_PREFIX "ctf-fs-data-stream"
44 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_valid_len
- stream
->request_offset
;
66 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
,
83 static int mmap_next(struct ctf_fs_stream
*stream
)
86 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_valid_len
;
95 stream
->request_offset
= 0;
98 stream
->mmap_valid_len
= MIN(stream
->file
->size
- stream
->mmap_offset
,
99 stream
->mmap_max_len
);
100 /* Round up to next page, assuming page size being a power of 2. */
101 stream
->mmap_len
= (stream
->mmap_valid_len
+ ctf_fs
->page_size
- 1)
102 & ~(ctf_fs
->page_size
- 1);
104 stream
->mmap_addr
= mmap((void *) 0, stream
->mmap_len
,
105 PROT_READ
, MAP_PRIVATE
, fileno(stream
->file
->fp
),
106 stream
->mmap_offset
);
107 if (stream
->mmap_addr
== MAP_FAILED
) {
108 PERR("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %zu: %s\n",
109 stream
->mmap_len
, stream
->file
->path
->str
,
110 stream
->file
->fp
, stream
->mmap_offset
,
117 stream_munmap(stream
);
123 static enum bt_ctf_notif_iter_medium_status
medop_request_bytes(
124 size_t request_sz
, uint8_t **buffer_addr
,
125 size_t *buffer_sz
, void *data
)
127 enum bt_ctf_notif_iter_medium_status status
=
128 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK
;
129 struct ctf_fs_stream
*stream
= data
;
130 struct ctf_fs_component
*ctf_fs
= stream
->file
->ctf_fs
;
132 if (request_sz
== 0) {
136 /* Check if we need an initial memory map */
137 if (!stream
->mmap_addr
) {
138 if (mmap_next(stream
)) {
139 PERR("Cannot memory-map initial region of file \"%s\" (%p)\n",
140 stream
->file
->path
->str
, stream
->file
->fp
);
145 /* Check if we have at least one memory-mapped byte left */
146 if (remaining_mmap_bytes(stream
) == 0) {
147 /* Are we at the end of the file? */
148 if (stream
->request_offset
>= stream
->file
->size
) {
149 PDBG("Reached end of file \"%s\" (%p)\n",
150 stream
->file
->path
->str
, stream
->file
->fp
);
151 status
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF
;
155 if (mmap_next(stream
)) {
156 PERR("Cannot memory-map next region of file \"%s\" (%p)\n",
157 stream
->file
->path
->str
, stream
->file
->fp
);
162 *buffer_sz
= MIN(remaining_mmap_bytes(stream
), request_sz
);
163 *buffer_addr
= ((uint8_t *) stream
->mmap_addr
) + stream
->request_offset
;
164 stream
->request_offset
+= *buffer_sz
;
168 status
= BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR
;
174 static struct bt_ctf_stream
*medop_get_stream(
175 struct bt_ctf_stream_class
*stream_class
, void *data
)
177 struct ctf_fs_stream
*fs_stream
= data
;
178 struct ctf_fs_component
*ctf_fs
= fs_stream
->file
->ctf_fs
;
180 if (!fs_stream
->stream
) {
181 int64_t id
= bt_ctf_stream_class_get_id(stream_class
);
183 PDBG("Creating stream out of stream class %" PRId64
"\n", id
);
184 fs_stream
->stream
= bt_ctf_stream_create(stream_class
,
185 fs_stream
->file
->path
->str
);
186 if (!fs_stream
->stream
) {
187 PERR("Cannot create stream (stream class %" PRId64
")\n",
192 return fs_stream
->stream
;
195 static struct bt_ctf_notif_iter_medium_ops medops
= {
196 .request_bytes
= medop_request_bytes
,
197 .get_stream
= medop_get_stream
,
201 struct ctf_fs_stream
*ctf_fs_stream_create(
202 struct ctf_fs_component
*ctf_fs
, struct ctf_fs_file
*file
)
204 struct ctf_fs_stream
*stream
= g_new0(struct ctf_fs_stream
, 1);
211 stream
->notif_iter
= bt_ctf_notif_iter_create(ctf_fs
->metadata
.trace
,
212 ctf_fs
->page_size
, medops
, stream
, ctf_fs
->error_fp
);
213 if (!stream
->notif_iter
) {
216 stream
->mmap_max_len
= ctf_fs
->page_size
* 2048;
219 /* Do not touch "borrowed" file. */
221 ctf_fs_stream_destroy(stream
);
227 enum bt_notification_iterator_status
ctf_fs_data_stream_get_next_notification(
228 struct ctf_fs_component
*ctf_fs
,
229 struct bt_notification
**notification
,
232 enum bt_ctf_notif_iter_status status
;
233 enum bt_notification_iterator_status ret
;
234 /* FIXME, only iterating on one stream for the moment. */
235 struct ctf_fs_stream
*stream
= g_ptr_array_index(ctf_fs
->streams
,
238 if (stream
->end_reached
) {
239 status
= BT_CTF_NOTIF_ITER_STATUS_EOF
;
243 status
= bt_ctf_notif_iter_get_next_notification(stream
->notif_iter
,
245 if (status
!= BT_CTF_NOTIF_ITER_STATUS_OK
&&
246 status
!= BT_CTF_NOTIF_ITER_STATUS_EOF
) {
250 /* Should be handled in bt_ctf_notif_iter_get_next_notification. */
251 if (status
== BT_CTF_NOTIF_ITER_STATUS_EOF
) {
252 *notification
= bt_notification_stream_end_create(
254 if (!*notification
) {
255 status
= BT_CTF_NOTIF_ITER_STATUS_ERROR
;
257 status
= BT_CTF_NOTIF_ITER_STATUS_OK
;
258 stream
->end_reached
= true;
262 case BT_CTF_NOTIF_ITER_STATUS_EOF
:
263 ret
= BT_NOTIFICATION_ITERATOR_STATUS_END
;
265 case BT_CTF_NOTIF_ITER_STATUS_OK
:
266 ret
= BT_NOTIFICATION_ITERATOR_STATUS_OK
;
268 case BT_CTF_NOTIF_ITER_STATUS_AGAIN
:
270 * Should not make it this far as this is medium-specific;
271 * there is nothing for the user to do and it should have been
275 case BT_CTF_NOTIF_ITER_STATUS_INVAL
:
276 /* No argument provided by the user, so don't return INVAL. */
277 case BT_CTF_NOTIF_ITER_STATUS_ERROR
:
279 ret
= BT_NOTIFICATION_ITERATOR_STATUS_ERROR
;