Rename correlator to muxer
[babeltrace.git] / plugins / ctf / fs / data-stream.c
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>
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>
39
40 #define PRINT_ERR_STREAM ctf_fs->error_fp
41 #define PRINT_PREFIX "ctf-fs-data-stream"
42 #include "print.h"
43
44 static void ctf_fs_stream_destroy(struct ctf_fs_stream *stream)
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 {
63 return stream->mmap_offset + stream->mmap_len -
64 stream->request_offset;
65 }
66
67 static int stream_munmap(struct ctf_fs_stream *stream)
68 {
69 struct ctf_fs_component *ctf_fs = stream->file->ctf_fs;
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));
76 return -1;
77 }
78
79 return 0;
80 }
81
82 static int mmap_next(struct ctf_fs_stream *stream)
83 {
84 struct ctf_fs_component *ctf_fs = stream->file->ctf_fs;
85 int ret = 0;
86
87 /* Unmap old region */
88 if (stream->mmap_addr) {
89 if (stream_munmap(stream)) {
90 goto error;
91 }
92
93 stream->mmap_offset += stream->mmap_len;
94 stream->request_offset = stream->mmap_offset;
95 }
96
97 /* Map new region */
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,
105 strerror(errno));
106 goto error;
107 }
108
109 goto end;
110 error:
111 stream_munmap(stream);
112 ret = -1;
113 end:
114 return ret;
115 }
116
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)
120 {
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;
125
126 if (request_sz == 0) {
127 goto end;
128 }
129
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);
135 goto error;
136 }
137 }
138
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;
146 goto end;
147 }
148
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);
152 goto error;
153 }
154 }
155
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;
160 goto end;
161
162 error:
163 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
164
165 end:
166 return status;
167 }
168
169 static struct bt_ctf_stream *medop_get_stream(
170 struct bt_ctf_stream_class *stream_class, void *data)
171 {
172 struct ctf_fs_stream *fs_stream = data;
173 struct ctf_fs_component *ctf_fs = fs_stream->file->ctf_fs;
174
175 if (!fs_stream->stream) {
176 int64_t id = bt_ctf_stream_class_get_id(stream_class);
177
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",
183 id);
184 }
185 }
186
187 return fs_stream->stream;
188 }
189
190 static struct bt_ctf_notif_iter_medium_ops medops = {
191 .request_bytes = medop_request_bytes,
192 .get_stream = medop_get_stream,
193 };
194
195 static struct ctf_fs_stream *ctf_fs_stream_create(
196 struct ctf_fs_component *ctf_fs, struct ctf_fs_file *file)
197 {
198 struct ctf_fs_stream *stream = g_new0(struct ctf_fs_stream, 1);
199
200 if (!stream) {
201 goto error;
202 }
203
204 stream->file = file;
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) {
208 goto error;
209 }
210 stream->mmap_len = ctf_fs->page_size;
211 goto end;
212 error:
213 /* Do not touch "borrowed" file. */
214 stream->file = NULL;
215 ctf_fs_stream_destroy(stream);
216 stream = NULL;
217 end:
218 return stream;
219 }
220
221 int ctf_fs_data_stream_open_streams(struct ctf_fs_component *ctf_fs)
222 {
223 int ret = 0;
224 const char *name;
225 GError *error = NULL;
226 GDir *dir = g_dir_open(ctf_fs->trace_path->str, 0, &error);
227
228 if (!dir) {
229 PERR("Cannot open directory \"%s\": %s (code %d)\n",
230 ctf_fs->trace_path->str, error->message,
231 error->code);
232 goto error;
233 }
234
235 while ((name = g_dir_read_name(dir))) {
236 struct ctf_fs_file *file = NULL;
237 struct ctf_fs_stream *stream = NULL;
238
239 if (!strcmp(name, CTF_FS_METADATA_FILENAME)) {
240 /* Ignore the metadata stream. */
241 PDBG("Ignoring metadata file \"%s\"\n",
242 name);
243 continue;
244 }
245
246 if (name[0] == '.') {
247 PDBG("Ignoring hidden file \"%s\"\n",
248 name);
249 continue;
250 }
251
252 /* Create the file. */
253 file = ctf_fs_file_create(ctf_fs);
254 if (!file) {
255 PERR("Cannot create stream file object\n");
256 goto error;
257 }
258
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);
265 continue;
266 }
267
268 /* Open the file. */
269 if (ctf_fs_file_open(ctf_fs, file, "rb")) {
270 ctf_fs_file_destroy(file);
271 goto error;
272 }
273
274 /* Create a private stream. */
275 stream = ctf_fs_stream_create(ctf_fs, file);
276 if (!stream) {
277 ctf_fs_file_destroy(file);
278 goto error;
279 }
280
281 /* Append file to the array of files. */
282 g_ptr_array_add(ctf_fs->data_stream.streams, stream);
283 }
284
285 goto end;
286 error:
287 ret = -1;
288 end:
289 if (dir) {
290 g_dir_close(dir);
291 dir = NULL;
292 }
293 if (error) {
294 g_error_free(error);
295 }
296 return ret;
297 }
298
299 int ctf_fs_data_stream_init(struct ctf_fs_component *ctf_fs,
300 struct ctf_fs_data_stream *data_stream)
301 {
302 int ret = 0;
303
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");
308 goto error;
309 }
310
311 goto end;
312 error:
313 ret = -1;
314 end:
315 return ret;
316 }
317
318 void ctf_fs_data_stream_fini(struct ctf_fs_data_stream *data_stream)
319 {
320 g_ptr_array_free(data_stream->streams, TRUE);
321 }
322
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)
326 {
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);
332
333 if (stream->end_reached) {
334 status = BT_CTF_NOTIF_ITER_STATUS_EOF;
335 goto end;
336 }
337
338 status = bt_ctf_notif_iter_get_next_notification(stream->notif_iter,
339 notification);
340 if (status != BT_CTF_NOTIF_ITER_STATUS_OK &&
341 status != BT_CTF_NOTIF_ITER_STATUS_EOF) {
342 goto end;
343 }
344
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(
348 stream->stream);
349 if (!*notification) {
350 status = BT_CTF_NOTIF_ITER_STATUS_ERROR;
351 }
352 status = BT_CTF_NOTIF_ITER_STATUS_OK;
353 stream->end_reached = true;
354 }
355 end:
356 switch (status) {
357 case BT_CTF_NOTIF_ITER_STATUS_EOF:
358 ret = BT_NOTIFICATION_ITERATOR_STATUS_END;
359 break;
360 case BT_CTF_NOTIF_ITER_STATUS_OK:
361 ret = BT_NOTIFICATION_ITERATOR_STATUS_OK;
362 break;
363 case BT_CTF_NOTIF_ITER_STATUS_AGAIN:
364 /*
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
367 * handled upstream.
368 */
369 assert(0);
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:
373 default:
374 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
375 break;
376 }
377 return ret;
378 }
This page took 0.053615 seconds and 5 git commands to generate.