Merge streams in ctf fs component
[babeltrace.git] / plugins / ctf / fs / data-stream.c
CommitLineData
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>
5b29e799 38#include "data-stream.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
5b29e799
JG
44static
45size_t remaining_mmap_bytes(struct ctf_fs_stream *stream)
e98a2d6e 46{
fc9a526c 47 return stream->mmap_valid_len - stream->request_offset;
e98a2d6e
PP
48}
49
5b29e799
JG
50static
51int stream_munmap(struct ctf_fs_stream *stream)
e98a2d6e 52{
fc9a526c 53 int ret = 0;
56a1cced 54 struct ctf_fs_component *ctf_fs = stream->file->ctf_fs;
e98a2d6e
PP
55
56 if (munmap(stream->mmap_addr, stream->mmap_len)) {
57 PERR("Cannot memory-unmap address %p (size %zu) of file \"%s\" (%p): %s\n",
58 stream->mmap_addr, stream->mmap_len,
59 stream->file->path->str, stream->file->fp,
60 strerror(errno));
fc9a526c
JG
61 ret = -1;
62 goto end;
e98a2d6e 63 }
fc9a526c
JG
64end:
65 return ret;
e98a2d6e
PP
66}
67
5b29e799
JG
68static
69int mmap_next(struct ctf_fs_stream *stream)
e98a2d6e 70{
e98a2d6e 71 int ret = 0;
fc9a526c 72 struct ctf_fs_component *ctf_fs = stream->file->ctf_fs;
e98a2d6e
PP
73
74 /* Unmap old region */
75 if (stream->mmap_addr) {
76 if (stream_munmap(stream)) {
77 goto error;
78 }
79
fc9a526c
JG
80 stream->mmap_offset += stream->mmap_valid_len;
81 stream->request_offset = 0;
e98a2d6e
PP
82 }
83
fc9a526c
JG
84 stream->mmap_valid_len = MIN(stream->file->size - stream->mmap_offset,
85 stream->mmap_max_len);
86 /* Round up to next page, assuming page size being a power of 2. */
87 stream->mmap_len = (stream->mmap_valid_len + ctf_fs->page_size - 1)
88 & ~(ctf_fs->page_size - 1);
e98a2d6e
PP
89 /* Map new region */
90 stream->mmap_addr = mmap((void *) 0, stream->mmap_len,
78586d8a
JG
91 PROT_READ, MAP_PRIVATE, fileno(stream->file->fp),
92 stream->mmap_offset);
e98a2d6e
PP
93 if (stream->mmap_addr == MAP_FAILED) {
94 PERR("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %zu: %s\n",
78586d8a
JG
95 stream->mmap_len, stream->file->path->str,
96 stream->file->fp, stream->mmap_offset,
97 strerror(errno));
e98a2d6e
PP
98 goto error;
99 }
100
101 goto end;
e98a2d6e
PP
102error:
103 stream_munmap(stream);
104 ret = -1;
e98a2d6e
PP
105end:
106 return ret;
107}
108
5b29e799
JG
109static
110enum bt_ctf_notif_iter_medium_status medop_request_bytes(
e98a2d6e
PP
111 size_t request_sz, uint8_t **buffer_addr,
112 size_t *buffer_sz, void *data)
113{
114 enum bt_ctf_notif_iter_medium_status status =
115 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK;
116 struct ctf_fs_stream *stream = data;
56a1cced 117 struct ctf_fs_component *ctf_fs = stream->file->ctf_fs;
e98a2d6e
PP
118
119 if (request_sz == 0) {
120 goto end;
121 }
122
123 /* Check if we need an initial memory map */
124 if (!stream->mmap_addr) {
125 if (mmap_next(stream)) {
126 PERR("Cannot memory-map initial region of file \"%s\" (%p)\n",
127 stream->file->path->str, stream->file->fp);
128 goto error;
129 }
130 }
131
132 /* Check if we have at least one memory-mapped byte left */
133 if (remaining_mmap_bytes(stream) == 0) {
134 /* Are we at the end of the file? */
fc9a526c 135 if (stream->request_offset >= stream->file->size) {
e98a2d6e
PP
136 PDBG("Reached end of file \"%s\" (%p)\n",
137 stream->file->path->str, stream->file->fp);
138 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF;
139 goto end;
140 }
141
142 if (mmap_next(stream)) {
143 PERR("Cannot memory-map next region of file \"%s\" (%p)\n",
144 stream->file->path->str, stream->file->fp);
145 goto error;
146 }
147 }
148
149 *buffer_sz = MIN(remaining_mmap_bytes(stream), request_sz);
fc9a526c 150 *buffer_addr = ((uint8_t *) stream->mmap_addr) + stream->request_offset;
e98a2d6e
PP
151 stream->request_offset += *buffer_sz;
152 goto end;
153
154error:
155 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
156
157end:
158 return status;
159}
160
5b29e799
JG
161static
162struct bt_ctf_stream *medop_get_stream(
e98a2d6e
PP
163 struct bt_ctf_stream_class *stream_class, void *data)
164{
165 struct ctf_fs_stream *fs_stream = data;
56a1cced 166 struct ctf_fs_component *ctf_fs = fs_stream->file->ctf_fs;
e98a2d6e
PP
167
168 if (!fs_stream->stream) {
169 int64_t id = bt_ctf_stream_class_get_id(stream_class);
170
171 PDBG("Creating stream out of stream class %" PRId64 "\n", id);
172 fs_stream->stream = bt_ctf_stream_create(stream_class,
fc9a526c 173 fs_stream->file->path->str);
e98a2d6e
PP
174 if (!fs_stream->stream) {
175 PERR("Cannot create stream (stream class %" PRId64 ")\n",
fc9a526c 176 id);
e98a2d6e
PP
177 }
178 }
179
180 return fs_stream->stream;
181}
182
183static struct bt_ctf_notif_iter_medium_ops medops = {
184 .request_bytes = medop_request_bytes,
185 .get_stream = medop_get_stream,
186};
187
e7a4393b
JG
188BT_HIDDEN
189struct ctf_fs_stream *ctf_fs_stream_create(
56a1cced 190 struct ctf_fs_component *ctf_fs, struct ctf_fs_file *file)
e98a2d6e
PP
191{
192 struct ctf_fs_stream *stream = g_new0(struct ctf_fs_stream, 1);
193
194 if (!stream) {
195 goto error;
196 }
197
198 stream->file = file;
5b29e799 199 stream->notif_iter = bt_ctf_notif_iter_create(ctf_fs->metadata->trace,
e7a4393b 200 ctf_fs->page_size, medops, stream, ctf_fs->error_fp);
e98a2d6e
PP
201 if (!stream->notif_iter) {
202 goto error;
203 }
5b29e799 204
fc9a526c 205 stream->mmap_max_len = ctf_fs->page_size * 2048;
e98a2d6e 206 goto end;
e98a2d6e 207error:
78586d8a 208 /* Do not touch "borrowed" file. */
e98a2d6e
PP
209 stream->file = NULL;
210 ctf_fs_stream_destroy(stream);
211 stream = NULL;
e98a2d6e
PP
212end:
213 return stream;
214}
215
5b29e799
JG
216BT_HIDDEN
217void ctf_fs_stream_destroy(struct ctf_fs_stream *stream)
e98a2d6e 218{
5b29e799
JG
219 if (!stream) {
220 return;
043e2020
JG
221 }
222
5b29e799
JG
223 if (stream->file) {
224 ctf_fs_file_destroy(stream->file);
e98a2d6e
PP
225 }
226
5b29e799
JG
227 if (stream->stream) {
228 BT_PUT(stream->stream);
043e2020 229 }
5b29e799
JG
230
231 if (stream->notif_iter) {
232 bt_ctf_notif_iter_destroy(stream->notif_iter);
78586d8a 233 }
5b29e799
JG
234
235 g_free(stream);
e98a2d6e 236}
This page took 0.033833 seconds and 4 git commands to generate.