Add user data for private port
[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>
b2e0c907 33#include <babeltrace/graph/notification-iterator.h>
78586d8a
JG
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 68static
e0f6a64a 69enum bt_ctf_notif_iter_medium_status mmap_next(struct ctf_fs_stream *stream)
e98a2d6e 70{
e0f6a64a
JG
71 enum bt_ctf_notif_iter_medium_status ret =
72 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK;
fc9a526c 73 struct ctf_fs_component *ctf_fs = stream->file->ctf_fs;
e98a2d6e
PP
74
75 /* Unmap old region */
76 if (stream->mmap_addr) {
77 if (stream_munmap(stream)) {
78 goto error;
79 }
80
fc9a526c
JG
81 stream->mmap_offset += stream->mmap_valid_len;
82 stream->request_offset = 0;
e98a2d6e
PP
83 }
84
fc9a526c
JG
85 stream->mmap_valid_len = MIN(stream->file->size - stream->mmap_offset,
86 stream->mmap_max_len);
e0f6a64a
JG
87 if (stream->mmap_valid_len == 0) {
88 ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF;
89 goto end;
90 }
fc9a526c
JG
91 /* Round up to next page, assuming page size being a power of 2. */
92 stream->mmap_len = (stream->mmap_valid_len + ctf_fs->page_size - 1)
93 & ~(ctf_fs->page_size - 1);
e98a2d6e 94 /* Map new region */
e0f6a64a 95 assert(stream->mmap_len);
e98a2d6e 96 stream->mmap_addr = mmap((void *) 0, stream->mmap_len,
78586d8a
JG
97 PROT_READ, MAP_PRIVATE, fileno(stream->file->fp),
98 stream->mmap_offset);
e98a2d6e
PP
99 if (stream->mmap_addr == MAP_FAILED) {
100 PERR("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %zu: %s\n",
78586d8a
JG
101 stream->mmap_len, stream->file->path->str,
102 stream->file->fp, stream->mmap_offset,
103 strerror(errno));
e98a2d6e
PP
104 goto error;
105 }
106
107 goto end;
e98a2d6e
PP
108error:
109 stream_munmap(stream);
e0f6a64a 110 ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
e98a2d6e
PP
111end:
112 return ret;
113}
114
5b29e799
JG
115static
116enum bt_ctf_notif_iter_medium_status medop_request_bytes(
e98a2d6e
PP
117 size_t request_sz, uint8_t **buffer_addr,
118 size_t *buffer_sz, void *data)
119{
120 enum bt_ctf_notif_iter_medium_status status =
121 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK;
122 struct ctf_fs_stream *stream = data;
56a1cced 123 struct ctf_fs_component *ctf_fs = stream->file->ctf_fs;
e98a2d6e
PP
124
125 if (request_sz == 0) {
126 goto end;
127 }
128
e98a2d6e
PP
129 /* Check if we have at least one memory-mapped byte left */
130 if (remaining_mmap_bytes(stream) == 0) {
131 /* Are we at the end of the file? */
e0f6a64a 132 if (stream->mmap_offset >= stream->file->size) {
e98a2d6e
PP
133 PDBG("Reached end of file \"%s\" (%p)\n",
134 stream->file->path->str, stream->file->fp);
135 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF;
136 goto end;
137 }
138
e0f6a64a
JG
139 status = mmap_next(stream);
140 switch (status) {
141 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK:
142 break;
143 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF:
144 goto end;
145 default:
e98a2d6e 146 PERR("Cannot memory-map next region of file \"%s\" (%p)\n",
e0f6a64a
JG
147 stream->file->path->str,
148 stream->file->fp);
e98a2d6e
PP
149 goto error;
150 }
151 }
152
153 *buffer_sz = MIN(remaining_mmap_bytes(stream), request_sz);
fc9a526c 154 *buffer_addr = ((uint8_t *) stream->mmap_addr) + stream->request_offset;
e98a2d6e
PP
155 stream->request_offset += *buffer_sz;
156 goto end;
157
158error:
159 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
160
161end:
162 return status;
163}
164
5b29e799
JG
165static
166struct bt_ctf_stream *medop_get_stream(
e98a2d6e
PP
167 struct bt_ctf_stream_class *stream_class, void *data)
168{
169 struct ctf_fs_stream *fs_stream = data;
56a1cced 170 struct ctf_fs_component *ctf_fs = fs_stream->file->ctf_fs;
e98a2d6e
PP
171
172 if (!fs_stream->stream) {
173 int64_t id = bt_ctf_stream_class_get_id(stream_class);
174
175 PDBG("Creating stream out of stream class %" PRId64 "\n", id);
176 fs_stream->stream = bt_ctf_stream_create(stream_class,
fc9a526c 177 fs_stream->file->path->str);
e98a2d6e
PP
178 if (!fs_stream->stream) {
179 PERR("Cannot create stream (stream class %" PRId64 ")\n",
fc9a526c 180 id);
e98a2d6e
PP
181 }
182 }
183
184 return fs_stream->stream;
185}
186
187static struct bt_ctf_notif_iter_medium_ops medops = {
188 .request_bytes = medop_request_bytes,
189 .get_stream = medop_get_stream,
190};
191
b6c3dcb2
JG
192static
193int build_index_from_idx_file(struct ctf_fs_stream *stream)
194{
195 int ret = 0;
196 gchar *directory = NULL;
197 gchar *basename = NULL;
198 GString *index_basename = NULL;
199 gchar *index_file_path = NULL;
200 GMappedFile *mapped_file = NULL;
201 gsize filesize;
202 const struct ctf_packet_index_file_hdr *header;
203 const char *mmap_begin, *file_pos;
204 struct index_entry *index;
205 uint64_t total_packets_size = 0;
206 size_t file_index_entry_size;
207 size_t file_entry_count;
208 size_t i;
209
210 /* Look for index file in relative path index/name.idx. */
211 basename = g_path_get_basename(stream->file->path->str);
212 if (!basename) {
213 ret = -1;
214 goto end;
215 }
216
217 directory = g_path_get_dirname(stream->file->path->str);
218 if (!directory) {
219 ret = -1;
220 goto end;
221 }
222
223 index_basename = g_string_new(basename);
224 if (!index_basename) {
225 ret = -1;
226 goto end;
227 }
228
229 g_string_append(index_basename, ".idx");
230 index_file_path = g_build_filename(directory, "index",
231 index_basename->str, NULL);
232 mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL);
06367fa3
JG
233 if (!mapped_file) {
234 ret = -1;
235 goto end;
236 }
b6c3dcb2
JG
237 filesize = g_mapped_file_get_length(mapped_file);
238 if (filesize < sizeof(*header)) {
239 printf_error("Invalid LTTng trace index: file size < header size");
240 ret = -1;
241 goto end;
242 }
243
244 mmap_begin = g_mapped_file_get_contents(mapped_file);
245 header = (struct ctf_packet_index_file_hdr *) mmap_begin;
246
247 file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header);
248 if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
249 printf_error("Invalid LTTng trace index: \"magic\" validation failed");
250 ret = -1;
251 goto end;
252 }
b6c3dcb2
JG
253
254 file_index_entry_size = be32toh(header->packet_index_len);
255 file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
256 if ((filesize - sizeof(*header)) % (file_entry_count * file_index_entry_size)) {
257 printf_error("Invalid index file size; not a multiple of index entry size");
258 ret = -1;
259 goto end;
260 }
261
262 stream->index.entries = g_array_sized_new(FALSE, TRUE,
263 sizeof(struct index_entry), file_entry_count);
264 if (!stream->index.entries) {
265 ret = -1;
266 goto end;
267 }
268 index = (struct index_entry *) stream->index.entries->data;
269 for (i = 0; i < file_entry_count; i++) {
270 struct ctf_packet_index *file_index =
271 (struct ctf_packet_index *) file_pos;
272 uint64_t packet_size = be64toh(file_index->packet_size);
273
274 if (packet_size % CHAR_BIT) {
275 ret = -1;
276 printf_error("Invalid packet size encountered in index file");
277 goto invalid_index;
278 }
279
280 /* Convert size in bits to bytes. */
281 packet_size /= CHAR_BIT;
282 index->packet_size = packet_size;
283
284 index->offset = be64toh(file_index->offset);
285 if (i != 0 && index->offset < (index - 1)->offset) {
286 printf_error("Invalid, non-monotonic, packet offset encountered in index file");
287 ret = -1;
288 goto invalid_index;
289 }
290
291 index->timestamp_begin = be64toh(file_index->timestamp_begin);
292 index->timestamp_end = be64toh(file_index->timestamp_end);
293 if (index->timestamp_end < index->timestamp_begin) {
294 printf_error("Invalid packet time bounds encountered in index file");
295 ret = -1;
296 goto invalid_index;
297 }
298
299 total_packets_size += packet_size;
300 file_pos += file_index_entry_size;
301 index++;
302 }
303
304 /* Validate that the index addresses the complete stream. */
305 if (stream->file->size != total_packets_size) {
306 printf_error("Invalid index; indexed size != stream file size");
307 ret = -1;
308 goto invalid_index;
309 }
310end:
311 g_free(directory);
312 g_free(basename);
313 g_free(index_file_path);
06367fa3
JG
314 if (index_basename) {
315 g_string_free(index_basename, TRUE);
316 }
317 if (mapped_file) {
318 g_mapped_file_unref(mapped_file);
319 }
b6c3dcb2
JG
320 return ret;
321invalid_index:
322 g_array_free(stream->index.entries, TRUE);
323 goto end;
324}
325
326static
327int build_index_from_stream(struct ctf_fs_stream *stream)
328{
329 int ret = 0;
330end:
331 return ret;
332}
333
334static
335int init_stream_index(struct ctf_fs_stream *stream)
336{
337 int ret;
338
339 ret = build_index_from_idx_file(stream);
340 if (!ret) {
341 goto end;
342 }
343
344 ret = build_index_from_stream(stream);
345end:
346 return ret;
347}
348
e7a4393b
JG
349BT_HIDDEN
350struct ctf_fs_stream *ctf_fs_stream_create(
56a1cced 351 struct ctf_fs_component *ctf_fs, struct ctf_fs_file *file)
e98a2d6e 352{
b6c3dcb2 353 int ret;
e98a2d6e
PP
354 struct ctf_fs_stream *stream = g_new0(struct ctf_fs_stream, 1);
355
356 if (!stream) {
357 goto error;
358 }
359
360 stream->file = file;
5b29e799 361 stream->notif_iter = bt_ctf_notif_iter_create(ctf_fs->metadata->trace,
e7a4393b 362 ctf_fs->page_size, medops, stream, ctf_fs->error_fp);
e98a2d6e
PP
363 if (!stream->notif_iter) {
364 goto error;
365 }
5b29e799 366
fc9a526c 367 stream->mmap_max_len = ctf_fs->page_size * 2048;
b6c3dcb2
JG
368 ret = init_stream_index(stream);
369 if (ret) {
370 goto error;
371 }
e98a2d6e 372 goto end;
e98a2d6e 373error:
78586d8a 374 /* Do not touch "borrowed" file. */
e98a2d6e
PP
375 stream->file = NULL;
376 ctf_fs_stream_destroy(stream);
377 stream = NULL;
e98a2d6e
PP
378end:
379 return stream;
380}
381
5b29e799
JG
382BT_HIDDEN
383void ctf_fs_stream_destroy(struct ctf_fs_stream *stream)
e98a2d6e 384{
5b29e799
JG
385 if (!stream) {
386 return;
043e2020
JG
387 }
388
5b29e799
JG
389 if (stream->file) {
390 ctf_fs_file_destroy(stream->file);
e98a2d6e
PP
391 }
392
5b29e799
JG
393 if (stream->stream) {
394 BT_PUT(stream->stream);
043e2020 395 }
5b29e799
JG
396
397 if (stream->notif_iter) {
398 bt_ctf_notif_iter_destroy(stream->notif_iter);
78586d8a 399 }
5b29e799 400
b6c3dcb2
JG
401 if (stream->index.entries) {
402 g_array_free(stream->index.entries, TRUE);
403 }
404
5b29e799 405 g_free(stream);
e98a2d6e 406}
This page took 0.042582 seconds and 4 git commands to generate.