Commit | Line | Data |
---|---|---|
e98a2d6e PP |
1 | /* |
2 | * Copyright 2016 - Philippe Proulx <pproulx@efficios.com> | |
fc9a526c | 3 | * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
e98a2d6e PP |
4 | * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation |
5 | * | |
e98a2d6e PP |
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: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
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 | |
22 | * SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include <stdio.h> | |
26 | #include <stdint.h> | |
27 | #include <stdlib.h> | |
28 | #include <stdbool.h> | |
29 | #include <glib.h> | |
30 | #include <inttypes.h> | |
31 | #include <sys/mman.h> | |
32 | #include <babeltrace/ctf-ir/stream.h> | |
78586d8a JG |
33 | #include <babeltrace/plugin/notification/iterator.h> |
34 | #include "file.h" | |
35 | #include "metadata.h" | |
36 | #include "../common/notif-iter/notif-iter.h" | |
37 | #include <assert.h> | |
e98a2d6e PP |
38 | |
39 | #define PRINT_ERR_STREAM ctf_fs->error_fp | |
40 | #define PRINT_PREFIX "ctf-fs-data-stream" | |
41 | #include "print.h" | |
42 | ||
e7a4393b JG |
43 | BT_HIDDEN |
44 | void ctf_fs_stream_destroy(struct ctf_fs_stream *stream) | |
e98a2d6e PP |
45 | { |
46 | if (stream->file) { | |
47 | ctf_fs_file_destroy(stream->file); | |
48 | } | |
49 | ||
50 | if (stream->stream) { | |
51 | BT_PUT(stream->stream); | |
52 | } | |
53 | ||
54 | if (stream->notif_iter) { | |
55 | bt_ctf_notif_iter_destroy(stream->notif_iter); | |
56 | } | |
57 | ||
58 | g_free(stream); | |
59 | } | |
60 | ||
61 | static size_t remaining_mmap_bytes(struct ctf_fs_stream *stream) | |
62 | { | |
fc9a526c | 63 | return stream->mmap_valid_len - stream->request_offset; |
e98a2d6e PP |
64 | } |
65 | ||
66 | static int stream_munmap(struct ctf_fs_stream *stream) | |
67 | { | |
fc9a526c | 68 | int ret = 0; |
56a1cced | 69 | struct ctf_fs_component *ctf_fs = stream->file->ctf_fs; |
e98a2d6e PP |
70 | |
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, | |
75 | strerror(errno)); | |
fc9a526c JG |
76 | ret = -1; |
77 | goto end; | |
e98a2d6e | 78 | } |
fc9a526c JG |
79 | end: |
80 | return ret; | |
e98a2d6e PP |
81 | } |
82 | ||
83 | static int mmap_next(struct ctf_fs_stream *stream) | |
84 | { | |
e98a2d6e | 85 | int ret = 0; |
fc9a526c | 86 | struct ctf_fs_component *ctf_fs = stream->file->ctf_fs; |
e98a2d6e PP |
87 | |
88 | /* Unmap old region */ | |
89 | if (stream->mmap_addr) { | |
90 | if (stream_munmap(stream)) { | |
91 | goto error; | |
92 | } | |
93 | ||
fc9a526c JG |
94 | stream->mmap_offset += stream->mmap_valid_len; |
95 | stream->request_offset = 0; | |
e98a2d6e PP |
96 | } |
97 | ||
fc9a526c JG |
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); | |
e98a2d6e PP |
103 | /* Map new region */ |
104 | stream->mmap_addr = mmap((void *) 0, stream->mmap_len, | |
78586d8a JG |
105 | PROT_READ, MAP_PRIVATE, fileno(stream->file->fp), |
106 | stream->mmap_offset); | |
e98a2d6e PP |
107 | if (stream->mmap_addr == MAP_FAILED) { |
108 | PERR("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %zu: %s\n", | |
78586d8a JG |
109 | stream->mmap_len, stream->file->path->str, |
110 | stream->file->fp, stream->mmap_offset, | |
111 | strerror(errno)); | |
e98a2d6e PP |
112 | goto error; |
113 | } | |
114 | ||
115 | goto end; | |
e98a2d6e PP |
116 | error: |
117 | stream_munmap(stream); | |
118 | ret = -1; | |
e98a2d6e PP |
119 | end: |
120 | return ret; | |
121 | } | |
122 | ||
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) | |
126 | { | |
127 | enum bt_ctf_notif_iter_medium_status status = | |
128 | BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK; | |
129 | struct ctf_fs_stream *stream = data; | |
56a1cced | 130 | struct ctf_fs_component *ctf_fs = stream->file->ctf_fs; |
e98a2d6e PP |
131 | |
132 | if (request_sz == 0) { | |
133 | goto end; | |
134 | } | |
135 | ||
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); | |
141 | goto error; | |
142 | } | |
143 | } | |
144 | ||
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? */ | |
fc9a526c | 148 | if (stream->request_offset >= stream->file->size) { |
e98a2d6e PP |
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; | |
152 | goto end; | |
153 | } | |
154 | ||
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); | |
158 | goto error; | |
159 | } | |
160 | } | |
161 | ||
162 | *buffer_sz = MIN(remaining_mmap_bytes(stream), request_sz); | |
fc9a526c | 163 | *buffer_addr = ((uint8_t *) stream->mmap_addr) + stream->request_offset; |
e98a2d6e PP |
164 | stream->request_offset += *buffer_sz; |
165 | goto end; | |
166 | ||
167 | error: | |
168 | status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR; | |
169 | ||
170 | end: | |
171 | return status; | |
172 | } | |
173 | ||
174 | static struct bt_ctf_stream *medop_get_stream( | |
175 | struct bt_ctf_stream_class *stream_class, void *data) | |
176 | { | |
177 | struct ctf_fs_stream *fs_stream = data; | |
56a1cced | 178 | struct ctf_fs_component *ctf_fs = fs_stream->file->ctf_fs; |
e98a2d6e PP |
179 | |
180 | if (!fs_stream->stream) { | |
181 | int64_t id = bt_ctf_stream_class_get_id(stream_class); | |
182 | ||
183 | PDBG("Creating stream out of stream class %" PRId64 "\n", id); | |
184 | fs_stream->stream = bt_ctf_stream_create(stream_class, | |
fc9a526c | 185 | fs_stream->file->path->str); |
e98a2d6e PP |
186 | if (!fs_stream->stream) { |
187 | PERR("Cannot create stream (stream class %" PRId64 ")\n", | |
fc9a526c | 188 | id); |
e98a2d6e PP |
189 | } |
190 | } | |
191 | ||
192 | return fs_stream->stream; | |
193 | } | |
194 | ||
195 | static struct bt_ctf_notif_iter_medium_ops medops = { | |
196 | .request_bytes = medop_request_bytes, | |
197 | .get_stream = medop_get_stream, | |
198 | }; | |
199 | ||
e7a4393b JG |
200 | BT_HIDDEN |
201 | struct ctf_fs_stream *ctf_fs_stream_create( | |
56a1cced | 202 | struct ctf_fs_component *ctf_fs, struct ctf_fs_file *file) |
e98a2d6e PP |
203 | { |
204 | struct ctf_fs_stream *stream = g_new0(struct ctf_fs_stream, 1); | |
205 | ||
206 | if (!stream) { | |
207 | goto error; | |
208 | } | |
209 | ||
210 | stream->file = file; | |
211 | stream->notif_iter = bt_ctf_notif_iter_create(ctf_fs->metadata.trace, | |
e7a4393b | 212 | ctf_fs->page_size, medops, stream, ctf_fs->error_fp); |
e98a2d6e PP |
213 | if (!stream->notif_iter) { |
214 | goto error; | |
215 | } | |
fc9a526c | 216 | stream->mmap_max_len = ctf_fs->page_size * 2048; |
e98a2d6e | 217 | goto end; |
e98a2d6e | 218 | error: |
78586d8a | 219 | /* Do not touch "borrowed" file. */ |
e98a2d6e PP |
220 | stream->file = NULL; |
221 | ctf_fs_stream_destroy(stream); | |
222 | stream = NULL; | |
e98a2d6e PP |
223 | end: |
224 | return stream; | |
225 | } | |
226 | ||
78586d8a | 227 | enum bt_notification_iterator_status ctf_fs_data_stream_get_next_notification( |
56a1cced | 228 | struct ctf_fs_component *ctf_fs, |
fc9a526c JG |
229 | struct bt_notification **notification, |
230 | size_t stream_id) | |
e98a2d6e | 231 | { |
e98a2d6e | 232 | enum bt_ctf_notif_iter_status status; |
78586d8a JG |
233 | enum bt_notification_iterator_status ret; |
234 | /* FIXME, only iterating on one stream for the moment. */ | |
fc9a526c JG |
235 | struct ctf_fs_stream *stream = g_ptr_array_index(ctf_fs->streams, |
236 | stream_id); | |
e98a2d6e | 237 | |
043e2020 JG |
238 | if (stream->end_reached) { |
239 | status = BT_CTF_NOTIF_ITER_STATUS_EOF; | |
240 | goto end; | |
241 | } | |
242 | ||
78586d8a JG |
243 | status = bt_ctf_notif_iter_get_next_notification(stream->notif_iter, |
244 | notification); | |
e98a2d6e PP |
245 | if (status != BT_CTF_NOTIF_ITER_STATUS_OK && |
246 | status != BT_CTF_NOTIF_ITER_STATUS_EOF) { | |
78586d8a | 247 | goto end; |
e98a2d6e PP |
248 | } |
249 | ||
043e2020 JG |
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( | |
253 | stream->stream); | |
254 | if (!*notification) { | |
255 | status = BT_CTF_NOTIF_ITER_STATUS_ERROR; | |
256 | } | |
257 | status = BT_CTF_NOTIF_ITER_STATUS_OK; | |
258 | stream->end_reached = true; | |
259 | } | |
e98a2d6e | 260 | end: |
78586d8a JG |
261 | switch (status) { |
262 | case BT_CTF_NOTIF_ITER_STATUS_EOF: | |
043e2020 JG |
263 | ret = BT_NOTIFICATION_ITERATOR_STATUS_END; |
264 | break; | |
78586d8a JG |
265 | case BT_CTF_NOTIF_ITER_STATUS_OK: |
266 | ret = BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
267 | break; | |
268 | case BT_CTF_NOTIF_ITER_STATUS_AGAIN: | |
269 | /* | |
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 | |
272 | * handled upstream. | |
273 | */ | |
274 | assert(0); | |
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: | |
043e2020 | 278 | default: |
78586d8a JG |
279 | ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; |
280 | break; | |
281 | } | |
e98a2d6e PP |
282 | return ret; |
283 | } |