Plugins are alive!
[babeltrace.git] / plugins / ctf / fs / data-stream.c
CommitLineData
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
e98a2d6e
PP
44static 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
61static 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
67static int stream_munmap(struct ctf_fs_stream *stream)
68{
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));
76 return -1;
77 }
78
79 return 0;
80}
81
82static int mmap_next(struct ctf_fs_stream *stream)
83{
56a1cced 84 struct ctf_fs_component *ctf_fs = stream->file->ctf_fs;
e98a2d6e
PP
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,
78586d8a
JG
99 PROT_READ, MAP_PRIVATE, fileno(stream->file->fp),
100 stream->mmap_offset);
e98a2d6e
PP
101 if (stream->mmap_addr == MAP_FAILED) {
102 PERR("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %zu: %s\n",
78586d8a
JG
103 stream->mmap_len, stream->file->path->str,
104 stream->file->fp, stream->mmap_offset,
105 strerror(errno));
e98a2d6e
PP
106 goto error;
107 }
108
109 goto end;
e98a2d6e
PP
110error:
111 stream_munmap(stream);
112 ret = -1;
e98a2d6e
PP
113end:
114 return ret;
115}
116
117static 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;
56a1cced 124 struct ctf_fs_component *ctf_fs = stream->file->ctf_fs;
e98a2d6e
PP
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
162error:
163 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
164
165end:
166 return status;
167}
168
169static 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;
56a1cced 173 struct ctf_fs_component *ctf_fs = fs_stream->file->ctf_fs;
e98a2d6e
PP
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
190static struct bt_ctf_notif_iter_medium_ops medops = {
191 .request_bytes = medop_request_bytes,
192 .get_stream = medop_get_stream,
193};
194
56a1cced
JG
195static struct ctf_fs_stream *ctf_fs_stream_create(
196 struct ctf_fs_component *ctf_fs, struct ctf_fs_file *file)
e98a2d6e
PP
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;
e98a2d6e 211 goto end;
e98a2d6e 212error:
78586d8a 213 /* Do not touch "borrowed" file. */
e98a2d6e
PP
214 stream->file = NULL;
215 ctf_fs_stream_destroy(stream);
216 stream = NULL;
e98a2d6e
PP
217end:
218 return stream;
219}
220
56a1cced 221int ctf_fs_data_stream_open_streams(struct ctf_fs_component *ctf_fs)
e98a2d6e
PP
222{
223 int ret = 0;
78586d8a 224 const char *name;
e98a2d6e
PP
225 GError *error = NULL;
226 GDir *dir = g_dir_open(ctf_fs->trace_path->str, 0, &error);
e98a2d6e
PP
227
228 if (!dir) {
229 PERR("Cannot open directory \"%s\": %s (code %d)\n",
78586d8a
JG
230 ctf_fs->trace_path->str, error->message,
231 error->code);
e98a2d6e
PP
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
78586d8a
JG
239 if (!strcmp(name, CTF_FS_METADATA_FILENAME)) {
240 /* Ignore the metadata stream. */
e98a2d6e 241 PDBG("Ignoring metadata file \"%s\"\n",
78586d8a 242 name);
e98a2d6e
PP
243 continue;
244 }
245
246 if (name[0] == '.') {
247 PDBG("Ignoring hidden file \"%s\"\n",
78586d8a 248 name);
e98a2d6e
PP
249 continue;
250 }
251
78586d8a 252 /* Create the file. */
e98a2d6e
PP
253 file = ctf_fs_file_create(ctf_fs);
254 if (!file) {
255 PERR("Cannot create stream file object\n");
256 goto error;
257 }
258
78586d8a
JG
259 /* Create full path string. */
260 g_string_append_printf(file->path, "%s/%s",
261 ctf_fs->trace_path->str, name);
e98a2d6e
PP
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
78586d8a 268 /* Open the file. */
e98a2d6e
PP
269 if (ctf_fs_file_open(ctf_fs, file, "rb")) {
270 ctf_fs_file_destroy(file);
271 goto error;
272 }
273
78586d8a 274 /* Create a private stream. */
e98a2d6e
PP
275 stream = ctf_fs_stream_create(ctf_fs, file);
276 if (!stream) {
277 ctf_fs_file_destroy(file);
278 goto error;
279 }
280
78586d8a 281 /* Append file to the array of files. */
e98a2d6e
PP
282 g_ptr_array_add(ctf_fs->data_stream.streams, stream);
283 }
284
285 goto end;
e98a2d6e
PP
286error:
287 ret = -1;
e98a2d6e
PP
288end:
289 if (dir) {
290 g_dir_close(dir);
291 dir = NULL;
292 }
e98a2d6e
PP
293 if (error) {
294 g_error_free(error);
295 }
e98a2d6e
PP
296 return ret;
297}
298
56a1cced 299int ctf_fs_data_stream_init(struct ctf_fs_component *ctf_fs,
e98a2d6e
PP
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(
78586d8a 305 (GDestroyNotify) ctf_fs_stream_destroy);
e98a2d6e
PP
306 if (!data_stream->streams) {
307 PERR("Cannot allocate array of streams\n");
308 goto error;
309 }
310
311 goto end;
e98a2d6e
PP
312error:
313 ret = -1;
e98a2d6e
PP
314end:
315 return ret;
316}
317
413bc2c4 318void ctf_fs_data_stream_fini(struct ctf_fs_data_stream *data_stream)
e98a2d6e
PP
319{
320 g_ptr_array_free(data_stream->streams, TRUE);
321}
322
78586d8a 323enum bt_notification_iterator_status ctf_fs_data_stream_get_next_notification(
56a1cced 324 struct ctf_fs_component *ctf_fs,
78586d8a 325 struct bt_notification **notification)
e98a2d6e 326{
e98a2d6e 327 enum bt_ctf_notif_iter_status status;
78586d8a
JG
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);
e98a2d6e 332
78586d8a
JG
333 status = bt_ctf_notif_iter_get_next_notification(stream->notif_iter,
334 notification);
e98a2d6e
PP
335 if (status != BT_CTF_NOTIF_ITER_STATUS_OK &&
336 status != BT_CTF_NOTIF_ITER_STATUS_EOF) {
78586d8a 337 goto end;
e98a2d6e
PP
338 }
339
e98a2d6e 340end:
78586d8a
JG
341 switch (status) {
342 case BT_CTF_NOTIF_ITER_STATUS_EOF:
343 /* Not an error, send end of stream notification. */
344 /* Subsequent calls to "next" should return BT_NOTIFICATION_STATUS_END */
345 /* TODO */
346 case BT_CTF_NOTIF_ITER_STATUS_OK:
347 ret = BT_NOTIFICATION_ITERATOR_STATUS_OK;
348 break;
349 case BT_CTF_NOTIF_ITER_STATUS_AGAIN:
350 /*
351 * Should not make it this far as this is medium-specific;
352 * there is nothing for the user to do and it should have been
353 * handled upstream.
354 */
355 assert(0);
356 case BT_CTF_NOTIF_ITER_STATUS_INVAL:
357 /* No argument provided by the user, so don't return INVAL. */
358 case BT_CTF_NOTIF_ITER_STATUS_ERROR:
359 ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
360 break;
361 }
e98a2d6e
PP
362 return ret;
363}
This page took 0.036735 seconds and 4 git commands to generate.