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