Port: memstream compat for mingw
[babeltrace.git] / plugins / ctf / fs-src / data-stream-file.c
CommitLineData
e98a2d6e 1/*
94cf822e 2 * Copyright 2016-2017 - 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
0fbb9a9f 25#include <stdlib.h>
e98a2d6e
PP
26#include <stdio.h>
27#include <stdint.h>
28#include <stdlib.h>
29#include <stdbool.h>
30#include <glib.h>
31#include <inttypes.h>
8f76831a 32#include <babeltrace/compat/mman-internal.h>
e98a2d6e 33#include <babeltrace/ctf-ir/stream.h>
b2e0c907 34#include <babeltrace/graph/notification-iterator.h>
4f1f88a6
PP
35#include <babeltrace/graph/notification-stream.h>
36#include <babeltrace/graph/notification-event.h>
37#include <babeltrace/graph/notification-packet.h>
78586d8a
JG
38#include "file.h"
39#include "metadata.h"
40#include "../common/notif-iter/notif-iter.h"
41#include <assert.h>
94cf822e 42#include "data-stream-file.h"
e98a2d6e
PP
43
44#define PRINT_ERR_STREAM ctf_fs->error_fp
45#define PRINT_PREFIX "ctf-fs-data-stream"
cd00e1d3
MD
46#define PRINT_DBG_CHECK ctf_fs_debug
47#include "../print.h"
e98a2d6e 48
4f1f88a6 49static inline
94cf822e 50size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file)
e98a2d6e 51{
94cf822e 52 return ds_file->mmap_valid_len - ds_file->request_offset;
e98a2d6e
PP
53}
54
5b29e799 55static
94cf822e 56int ds_file_munmap(struct ctf_fs_ds_file *ds_file)
e98a2d6e 57{
fc9a526c 58 int ret = 0;
d238196d 59 struct ctf_fs_component *ctf_fs;
e98a2d6e 60
d238196d 61 if (!ds_file || !ds_file->mmap_addr) {
94cf822e
PP
62 goto end;
63 }
64
d238196d 65 ctf_fs = ds_file->file->ctf_fs;
94cf822e 66 if (munmap(ds_file->mmap_addr, ds_file->mmap_len)) {
e98a2d6e 67 PERR("Cannot memory-unmap address %p (size %zu) of file \"%s\" (%p): %s\n",
94cf822e
PP
68 ds_file->mmap_addr, ds_file->mmap_len,
69 ds_file->file->path->str, ds_file->file->fp,
e98a2d6e 70 strerror(errno));
fc9a526c
JG
71 ret = -1;
72 goto end;
e98a2d6e 73 }
94cf822e
PP
74
75 ds_file->mmap_addr = NULL;
76
fc9a526c
JG
77end:
78 return ret;
e98a2d6e
PP
79}
80
5b29e799 81static
94cf822e
PP
82enum bt_ctf_notif_iter_medium_status ds_file_mmap_next(
83 struct ctf_fs_ds_file *ds_file)
e98a2d6e 84{
e0f6a64a
JG
85 enum bt_ctf_notif_iter_medium_status ret =
86 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK;
94cf822e 87 struct ctf_fs_component *ctf_fs = ds_file->file->ctf_fs;
e98a2d6e
PP
88
89 /* Unmap old region */
94cf822e
PP
90 if (ds_file->mmap_addr) {
91 if (ds_file_munmap(ds_file)) {
e98a2d6e
PP
92 goto error;
93 }
94
94cf822e
PP
95 ds_file->mmap_offset += ds_file->mmap_valid_len;
96 ds_file->request_offset = 0;
e98a2d6e
PP
97 }
98
94cf822e
PP
99 ds_file->mmap_valid_len = MIN(ds_file->file->size - ds_file->mmap_offset,
100 ds_file->mmap_max_len);
101 if (ds_file->mmap_valid_len == 0) {
e0f6a64a
JG
102 ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF;
103 goto end;
104 }
fc9a526c 105 /* Round up to next page, assuming page size being a power of 2. */
94cf822e 106 ds_file->mmap_len = (ds_file->mmap_valid_len + ctf_fs->page_size - 1)
fc9a526c 107 & ~(ctf_fs->page_size - 1);
e98a2d6e 108 /* Map new region */
94cf822e
PP
109 assert(ds_file->mmap_len);
110 ds_file->mmap_addr = mmap((void *) 0, ds_file->mmap_len,
111 PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp),
112 ds_file->mmap_offset);
113 if (ds_file->mmap_addr == MAP_FAILED) {
e98a2d6e 114 PERR("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %zu: %s\n",
94cf822e
PP
115 ds_file->mmap_len, ds_file->file->path->str,
116 ds_file->file->fp, ds_file->mmap_offset,
78586d8a 117 strerror(errno));
e98a2d6e
PP
118 goto error;
119 }
120
121 goto end;
e98a2d6e 122error:
94cf822e 123 ds_file_munmap(ds_file);
e0f6a64a 124 ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
e98a2d6e
PP
125end:
126 return ret;
127}
128
5b29e799
JG
129static
130enum bt_ctf_notif_iter_medium_status medop_request_bytes(
e98a2d6e
PP
131 size_t request_sz, uint8_t **buffer_addr,
132 size_t *buffer_sz, void *data)
133{
134 enum bt_ctf_notif_iter_medium_status status =
135 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK;
94cf822e
PP
136 struct ctf_fs_ds_file *ds_file = data;
137 struct ctf_fs_component *ctf_fs = ds_file->file->ctf_fs;
e98a2d6e
PP
138
139 if (request_sz == 0) {
140 goto end;
141 }
142
e98a2d6e 143 /* Check if we have at least one memory-mapped byte left */
94cf822e 144 if (remaining_mmap_bytes(ds_file) == 0) {
e98a2d6e 145 /* Are we at the end of the file? */
94cf822e 146 if (ds_file->mmap_offset >= ds_file->file->size) {
e98a2d6e 147 PDBG("Reached end of file \"%s\" (%p)\n",
94cf822e 148 ds_file->file->path->str, ds_file->file->fp);
e98a2d6e
PP
149 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF;
150 goto end;
151 }
152
94cf822e 153 status = ds_file_mmap_next(ds_file);
e0f6a64a
JG
154 switch (status) {
155 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK:
156 break;
157 case BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF:
158 goto end;
159 default:
e98a2d6e 160 PERR("Cannot memory-map next region of file \"%s\" (%p)\n",
94cf822e
PP
161 ds_file->file->path->str,
162 ds_file->file->fp);
e98a2d6e
PP
163 goto error;
164 }
165 }
166
94cf822e
PP
167 *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz);
168 *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset;
169 ds_file->request_offset += *buffer_sz;
e98a2d6e
PP
170 goto end;
171
172error:
173 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
174
175end:
176 return status;
177}
178
5b29e799
JG
179static
180struct bt_ctf_stream *medop_get_stream(
e98a2d6e
PP
181 struct bt_ctf_stream_class *stream_class, void *data)
182{
94cf822e
PP
183 struct ctf_fs_ds_file *ds_file = data;
184 struct bt_ctf_stream_class *ds_file_stream_class;
185 struct bt_ctf_stream *stream = NULL;
186
187 ds_file_stream_class = bt_ctf_stream_get_class(ds_file->stream);
188 bt_put(ds_file_stream_class);
189
190 if (stream_class != ds_file_stream_class) {
191 /*
192 * Not supported: two packets described by two different
193 * stream classes within the same data stream file.
194 */
195 goto end;
e98a2d6e
PP
196 }
197
94cf822e
PP
198 stream = ds_file->stream;
199
200end:
201 return stream;
e98a2d6e
PP
202}
203
204static struct bt_ctf_notif_iter_medium_ops medops = {
205 .request_bytes = medop_request_bytes,
206 .get_stream = medop_get_stream,
207};
208
b6c3dcb2 209static
94cf822e 210int build_index_from_idx_file(struct ctf_fs_ds_file *ds_file)
b6c3dcb2
JG
211{
212 int ret = 0;
213 gchar *directory = NULL;
214 gchar *basename = NULL;
215 GString *index_basename = NULL;
216 gchar *index_file_path = NULL;
217 GMappedFile *mapped_file = NULL;
218 gsize filesize;
219 const struct ctf_packet_index_file_hdr *header;
220 const char *mmap_begin, *file_pos;
221 struct index_entry *index;
222 uint64_t total_packets_size = 0;
223 size_t file_index_entry_size;
224 size_t file_entry_count;
225 size_t i;
226
227 /* Look for index file in relative path index/name.idx. */
94cf822e 228 basename = g_path_get_basename(ds_file->file->path->str);
b6c3dcb2
JG
229 if (!basename) {
230 ret = -1;
231 goto end;
232 }
233
94cf822e 234 directory = g_path_get_dirname(ds_file->file->path->str);
b6c3dcb2
JG
235 if (!directory) {
236 ret = -1;
237 goto end;
238 }
239
240 index_basename = g_string_new(basename);
241 if (!index_basename) {
242 ret = -1;
243 goto end;
244 }
245
246 g_string_append(index_basename, ".idx");
247 index_file_path = g_build_filename(directory, "index",
248 index_basename->str, NULL);
249 mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL);
06367fa3
JG
250 if (!mapped_file) {
251 ret = -1;
252 goto end;
253 }
b6c3dcb2
JG
254 filesize = g_mapped_file_get_length(mapped_file);
255 if (filesize < sizeof(*header)) {
256 printf_error("Invalid LTTng trace index: file size < header size");
257 ret = -1;
258 goto end;
259 }
260
261 mmap_begin = g_mapped_file_get_contents(mapped_file);
262 header = (struct ctf_packet_index_file_hdr *) mmap_begin;
263
264 file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header);
265 if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
266 printf_error("Invalid LTTng trace index: \"magic\" validation failed");
267 ret = -1;
268 goto end;
269 }
b6c3dcb2
JG
270
271 file_index_entry_size = be32toh(header->packet_index_len);
272 file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
273 if ((filesize - sizeof(*header)) % (file_entry_count * file_index_entry_size)) {
274 printf_error("Invalid index file size; not a multiple of index entry size");
275 ret = -1;
276 goto end;
277 }
278
94cf822e 279 ds_file->index.entries = g_array_sized_new(FALSE, TRUE,
b6c3dcb2 280 sizeof(struct index_entry), file_entry_count);
94cf822e 281 if (!ds_file->index.entries) {
b6c3dcb2
JG
282 ret = -1;
283 goto end;
284 }
94cf822e 285 index = (struct index_entry *) ds_file->index.entries->data;
b6c3dcb2
JG
286 for (i = 0; i < file_entry_count; i++) {
287 struct ctf_packet_index *file_index =
288 (struct ctf_packet_index *) file_pos;
289 uint64_t packet_size = be64toh(file_index->packet_size);
290
291 if (packet_size % CHAR_BIT) {
292 ret = -1;
293 printf_error("Invalid packet size encountered in index file");
294 goto invalid_index;
295 }
296
297 /* Convert size in bits to bytes. */
298 packet_size /= CHAR_BIT;
299 index->packet_size = packet_size;
300
301 index->offset = be64toh(file_index->offset);
302 if (i != 0 && index->offset < (index - 1)->offset) {
303 printf_error("Invalid, non-monotonic, packet offset encountered in index file");
304 ret = -1;
305 goto invalid_index;
306 }
307
308 index->timestamp_begin = be64toh(file_index->timestamp_begin);
309 index->timestamp_end = be64toh(file_index->timestamp_end);
310 if (index->timestamp_end < index->timestamp_begin) {
311 printf_error("Invalid packet time bounds encountered in index file");
312 ret = -1;
313 goto invalid_index;
314 }
315
316 total_packets_size += packet_size;
317 file_pos += file_index_entry_size;
318 index++;
319 }
320
321 /* Validate that the index addresses the complete stream. */
94cf822e 322 if (ds_file->file->size != total_packets_size) {
b6c3dcb2
JG
323 printf_error("Invalid index; indexed size != stream file size");
324 ret = -1;
325 goto invalid_index;
326 }
327end:
328 g_free(directory);
329 g_free(basename);
330 g_free(index_file_path);
06367fa3
JG
331 if (index_basename) {
332 g_string_free(index_basename, TRUE);
333 }
334 if (mapped_file) {
335 g_mapped_file_unref(mapped_file);
336 }
b6c3dcb2
JG
337 return ret;
338invalid_index:
94cf822e 339 g_array_free(ds_file->index.entries, TRUE);
b6c3dcb2
JG
340 goto end;
341}
342
343static
94cf822e 344int build_index_from_data_stream_file(struct ctf_fs_ds_file *stream)
b6c3dcb2 345{
4f1f88a6 346 return 0;
b6c3dcb2
JG
347}
348
349static
94cf822e 350int init_stream_index(struct ctf_fs_ds_file *ds_file)
b6c3dcb2
JG
351{
352 int ret;
353
94cf822e 354 ret = build_index_from_idx_file(ds_file);
b6c3dcb2
JG
355 if (!ret) {
356 goto end;
357 }
358
94cf822e 359 ret = build_index_from_data_stream_file(ds_file);
b6c3dcb2
JG
360end:
361 return ret;
362}
363
e7a4393b 364BT_HIDDEN
94cf822e
PP
365struct ctf_fs_ds_file *ctf_fs_ds_file_create(
366 struct ctf_fs_trace *ctf_fs_trace,
367 struct bt_ctf_stream *stream, const char *path,
368 bool build_index)
e98a2d6e 369{
b6c3dcb2 370 int ret;
94cf822e 371 struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1);
e98a2d6e 372
94cf822e 373 if (!ds_file) {
e98a2d6e
PP
374 goto error;
375 }
376
94cf822e
PP
377 ds_file->file = ctf_fs_file_create(ctf_fs_trace->ctf_fs);
378 if (!ds_file->file) {
4f1f88a6
PP
379 goto error;
380 }
381
94cf822e
PP
382 ds_file->stream = bt_get(stream);
383 ds_file->cc_prio_map = bt_get(ctf_fs_trace->cc_prio_map);
384 g_string_assign(ds_file->file->path, path);
385 ret = ctf_fs_file_open(ctf_fs_trace->ctf_fs, ds_file->file, "rb");
4f1f88a6
PP
386 if (ret) {
387 goto error;
388 }
389
94cf822e 390 ds_file->notif_iter = bt_ctf_notif_iter_create(
1a9f7075 391 ctf_fs_trace->metadata->trace,
94cf822e 392 ctf_fs_trace->ctf_fs->page_size, medops, ds_file,
1a9f7075 393 ctf_fs_trace->ctf_fs->error_fp);
94cf822e 394 if (!ds_file->notif_iter) {
e98a2d6e
PP
395 goto error;
396 }
5b29e799 397
94cf822e
PP
398 ds_file->mmap_max_len = ctf_fs_trace->ctf_fs->page_size * 2048;
399
400 if (build_index) {
401 ret = init_stream_index(ds_file);
402 if (ret) {
403 goto error;
404 }
b6c3dcb2 405 }
1a9f7075 406
e98a2d6e 407 goto end;
1a9f7075 408
e98a2d6e 409error:
78586d8a 410 /* Do not touch "borrowed" file. */
94cf822e
PP
411 ctf_fs_ds_file_destroy(ds_file);
412 ds_file = NULL;
1a9f7075 413
e98a2d6e 414end:
94cf822e 415 return ds_file;
e98a2d6e
PP
416}
417
5b29e799 418BT_HIDDEN
94cf822e 419void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file)
e98a2d6e 420{
94cf822e 421 if (!ds_file) {
5b29e799 422 return;
043e2020
JG
423 }
424
94cf822e
PP
425 bt_put(ds_file->cc_prio_map);
426 bt_put(ds_file->stream);
427 (void) ds_file_munmap(ds_file);
0982a26d 428
94cf822e
PP
429 if (ds_file->file) {
430 ctf_fs_file_destroy(ds_file->file);
e98a2d6e
PP
431 }
432
94cf822e
PP
433 if (ds_file->notif_iter) {
434 bt_ctf_notif_iter_destroy(ds_file->notif_iter);
043e2020 435 }
5b29e799 436
94cf822e
PP
437 if (ds_file->index.entries) {
438 g_array_free(ds_file->index.entries, TRUE);
78586d8a 439 }
5b29e799 440
94cf822e 441 g_free(ds_file);
e98a2d6e 442}
4f1f88a6
PP
443
444BT_HIDDEN
94cf822e
PP
445struct bt_notification_iterator_next_return ctf_fs_ds_file_next(
446 struct ctf_fs_ds_file *ds_file)
4f1f88a6
PP
447{
448 enum bt_ctf_notif_iter_status notif_iter_status;
449 struct bt_notification_iterator_next_return ret = {
450 .status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR,
451 .notification = NULL,
452 };
453
8915b7a7 454 notif_iter_status = bt_ctf_notif_iter_get_next_notification(
94cf822e 455 ds_file->notif_iter, ds_file->cc_prio_map, &ret.notification);
4f1f88a6 456
4f1f88a6
PP
457 switch (notif_iter_status) {
458 case BT_CTF_NOTIF_ITER_STATUS_EOF:
459 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_END;
460 break;
461 case BT_CTF_NOTIF_ITER_STATUS_OK:
462 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
463 break;
464 case BT_CTF_NOTIF_ITER_STATUS_AGAIN:
465 /*
466 * Should not make it this far as this is
467 * medium-specific; there is nothing for the user to do
468 * and it should have been handled upstream.
469 */
0fbb9a9f 470 abort();
4f1f88a6
PP
471 case BT_CTF_NOTIF_ITER_STATUS_INVAL:
472 case BT_CTF_NOTIF_ITER_STATUS_ERROR:
473 default:
474 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
475 break;
476 }
477
478 return ret;
479}
94cf822e
PP
480
481BT_HIDDEN
482int ctf_fs_ds_file_get_packet_header_context_fields(
483 struct ctf_fs_trace *ctf_fs_trace, const char *path,
484 struct bt_ctf_field **packet_header_field,
485 struct bt_ctf_field **packet_context_field)
486{
487 enum bt_ctf_notif_iter_status notif_iter_status;
488 struct ctf_fs_ds_file *ds_file;
489 int ret = 0;
490
491 ds_file = ctf_fs_ds_file_create(ctf_fs_trace, NULL, path, false);
492 if (!ds_file) {
493 goto error;
494 }
495
496 notif_iter_status = bt_ctf_notif_iter_get_packet_header_context_fields(
497 ds_file->notif_iter, packet_header_field, packet_context_field);
498 switch (notif_iter_status) {
499 case BT_CTF_NOTIF_ITER_STATUS_EOF:
500 case BT_CTF_NOTIF_ITER_STATUS_OK:
501 break;
502 case BT_CTF_NOTIF_ITER_STATUS_AGAIN:
0fbb9a9f 503 abort();
94cf822e
PP
504 case BT_CTF_NOTIF_ITER_STATUS_INVAL:
505 case BT_CTF_NOTIF_ITER_STATUS_ERROR:
506 default:
507 goto error;
508 break;
509 }
510
511 goto end;
512
513error:
514 ret = -1;
515
516 if (packet_header_field) {
517 bt_put(*packet_header_field);
518 }
519
520 if (packet_context_field) {
521 bt_put(*packet_context_field);
522 }
523
524end:
525 ctf_fs_ds_file_destroy(ds_file);
526 return ret;
527}
This page took 0.053317 seconds and 4 git commands to generate.