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