1a841dd67317f791a3d27c682d51698499fa3d16
[babeltrace.git] / plugins / ctf / fs-src / data-stream-file.c
1 /*
2 * Copyright 2016-2017 - Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation
5 *
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 <stdlib.h>
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 <babeltrace/compat/mman-internal.h>
33 #include <babeltrace/endian-internal.h>
34 #include <babeltrace/ctf-ir/stream.h>
35 #include <babeltrace/graph/notification-iterator.h>
36 #include <babeltrace/graph/notification-stream.h>
37 #include <babeltrace/graph/notification-event.h>
38 #include <babeltrace/graph/notification-packet.h>
39 #include <babeltrace/common-internal.h>
40 #include "file.h"
41 #include "metadata.h"
42 #include "../common/notif-iter/notif-iter.h"
43 #include <assert.h>
44 #include "data-stream-file.h"
45 #include <string.h>
46
47 #define BT_LOG_TAG "PLUGIN-CTF-FS-SRC-DS"
48 #include "logging.h"
49
50 static inline
51 size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file)
52 {
53 return ds_file->mmap_len - ds_file->request_offset;
54 }
55
56 static
57 int ds_file_munmap(struct ctf_fs_ds_file *ds_file)
58 {
59 int ret = 0;
60
61 if (!ds_file || !ds_file->mmap_addr) {
62 goto end;
63 }
64
65 if (bt_munmap(ds_file->mmap_addr, ds_file->mmap_len)) {
66 BT_LOGE_ERRNO("Cannot memory-unmap file",
67 ": address=%p, size=%zu, file_path=\"%s\", file=%p",
68 ds_file->mmap_addr, ds_file->mmap_len,
69 ds_file->file ? ds_file->file->path->str : "NULL",
70 ds_file->file ? ds_file->file->fp : NULL);
71 ret = -1;
72 goto end;
73 }
74
75 ds_file->mmap_addr = NULL;
76
77 end:
78 return ret;
79 }
80
81 static
82 enum bt_ctf_notif_iter_medium_status ds_file_mmap_next(
83 struct ctf_fs_ds_file *ds_file)
84 {
85 enum bt_ctf_notif_iter_medium_status ret =
86 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK;
87
88 /* Unmap old region */
89 if (ds_file->mmap_addr) {
90 if (ds_file_munmap(ds_file)) {
91 goto error;
92 }
93
94 /*
95 * mmap_len is guaranteed to be page-aligned except on the
96 * last mapping where it may not be possible (since the file's
97 * size itself may not be a page multiple).
98 */
99 ds_file->mmap_offset += ds_file->mmap_len;
100 ds_file->request_offset = 0;
101 }
102
103 ds_file->mmap_len = MIN(ds_file->file->size - ds_file->mmap_offset,
104 ds_file->mmap_max_len);
105 if (ds_file->mmap_len == 0) {
106 ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF;
107 goto end;
108 }
109 /* Map new region */
110 assert(ds_file->mmap_len);
111 ds_file->mmap_addr = bt_mmap((void *) 0, ds_file->mmap_len,
112 PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp),
113 ds_file->mmap_offset);
114 if (ds_file->mmap_addr == MAP_FAILED) {
115 BT_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s",
116 ds_file->mmap_len, ds_file->file->path->str,
117 ds_file->file->fp, (intmax_t) ds_file->mmap_offset,
118 strerror(errno));
119 goto error;
120 }
121
122 goto end;
123 error:
124 ds_file_munmap(ds_file);
125 ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
126 end:
127 return ret;
128 }
129
130 static
131 enum bt_ctf_notif_iter_medium_status medop_request_bytes(
132 size_t request_sz, uint8_t **buffer_addr,
133 size_t *buffer_sz, void *data)
134 {
135 enum bt_ctf_notif_iter_medium_status status =
136 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK;
137 struct ctf_fs_ds_file *ds_file = data;
138
139 if (request_sz == 0) {
140 goto end;
141 }
142
143 /* Check if we have at least one memory-mapped byte left */
144 if (remaining_mmap_bytes(ds_file) == 0) {
145 /* Are we at the end of the file? */
146 if (ds_file->mmap_offset >= ds_file->file->size) {
147 BT_LOGD("Reached end of file \"%s\" (%p)",
148 ds_file->file->path->str, ds_file->file->fp);
149 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_EOF;
150 goto end;
151 }
152
153 status = ds_file_mmap_next(ds_file);
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:
160 BT_LOGE("Cannot memory-map next region of file \"%s\" (%p)",
161 ds_file->file->path->str,
162 ds_file->file->fp);
163 goto error;
164 }
165 }
166
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;
170 goto end;
171
172 error:
173 status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
174
175 end:
176 return status;
177 }
178
179 static
180 struct bt_ctf_stream *medop_get_stream(
181 struct bt_ctf_stream_class *stream_class, uint64_t stream_id,
182 void *data)
183 {
184 struct ctf_fs_ds_file *ds_file = data;
185 struct bt_ctf_stream_class *ds_file_stream_class;
186 struct bt_ctf_stream *stream = NULL;
187
188 ds_file_stream_class = bt_ctf_stream_get_class(ds_file->stream);
189 bt_put(ds_file_stream_class);
190
191 if (stream_class != ds_file_stream_class) {
192 /*
193 * Not supported: two packets described by two different
194 * stream classes within the same data stream file.
195 */
196 goto end;
197 }
198
199 stream = ds_file->stream;
200
201 end:
202 return stream;
203 }
204
205 static
206 enum bt_ctf_notif_iter_medium_status medop_seek(
207 enum bt_ctf_notif_iter_seek_whence whence, off_t offset,
208 void *data)
209 {
210 enum bt_ctf_notif_iter_medium_status ret =
211 BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK;
212 struct ctf_fs_ds_file *ds_file = data;
213 off_t file_size = ds_file->file->size;
214
215 if (whence != BT_CTF_NOTIF_ITER_SEEK_WHENCE_SET ||
216 offset < 0 || offset > file_size) {
217 BT_LOGE("Invalid medium seek request: whence=%d, offset=%jd, "
218 "file-size=%jd", (int) whence, offset,
219 file_size);
220 ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_INVAL;
221 goto end;
222 }
223
224 /*
225 * Determine whether or not the destination is contained within the
226 * current mapping.
227 */
228 if (ds_file->mmap_addr && (offset < ds_file->mmap_offset ||
229 offset >= ds_file->mmap_offset + ds_file->mmap_len)) {
230 int unmap_ret;
231 off_t offset_in_mapping = offset % bt_common_get_page_size();
232
233 BT_LOGD("Medium seek request cannot be accomodated by the current "
234 "file mapping: offset=%jd, mmap-offset=%zu, "
235 "mmap-len=%zu", offset, ds_file->mmap_offset,
236 ds_file->mmap_len);
237 unmap_ret = ds_file_munmap(ds_file);
238 if (unmap_ret) {
239 ret = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_ERROR;
240 goto end;
241 }
242
243 ds_file->mmap_offset = offset - offset_in_mapping;
244 ds_file->request_offset = offset_in_mapping;
245 ret = ds_file_mmap_next(ds_file);
246 if (ret != BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK) {
247 goto end;
248 }
249 } else {
250 ds_file->request_offset = offset - ds_file->mmap_offset;
251 }
252
253 ds_file->end_reached = (offset == file_size);
254 end:
255 return ret;
256 }
257
258 BT_HIDDEN
259 struct bt_ctf_notif_iter_medium_ops ctf_fs_ds_file_medops = {
260 .request_bytes = medop_request_bytes,
261 .get_stream = medop_get_stream,
262 .seek = medop_seek,
263 };
264
265 static
266 struct ctf_fs_ds_index *ctf_fs_ds_index_create(size_t length)
267 {
268 struct ctf_fs_ds_index *index = g_new0(struct ctf_fs_ds_index, 1);
269
270 if (!index) {
271 BT_LOGE_STR("Failed to allocate index");
272 goto error;
273 }
274
275 index->entries = g_array_sized_new(FALSE, TRUE,
276 sizeof(struct ctf_fs_ds_index_entry), length);
277 if (!index->entries) {
278 BT_LOGE("Failed to allocate %zu index entries.", length);
279 goto error;
280 }
281 g_array_set_size(index->entries, length);
282 end:
283 return index;
284 error:
285 ctf_fs_ds_index_destroy(index);
286 goto end;
287 }
288
289 /* Returns a new, zeroed, index entry. */
290 static
291 struct ctf_fs_ds_index_entry *ctf_fs_ds_index_add_new_entry(
292 struct ctf_fs_ds_index *index)
293 {
294 g_array_set_size(index->entries, index->entries->len + 1);
295 return &g_array_index(index->entries, struct ctf_fs_ds_index_entry,
296 index->entries->len - 1);
297 }
298
299 static
300 struct bt_ctf_clock_class *get_field_mapped_clock_class(
301 struct bt_ctf_field *field)
302 {
303 struct bt_ctf_field_type *field_type;
304 struct bt_ctf_clock_class *clock_class = NULL;
305
306 field_type = bt_ctf_field_get_type(field);
307 if (!field_type) {
308 goto end;
309 }
310
311 clock_class = bt_ctf_field_type_integer_get_mapped_clock_class(
312 field_type);
313 if (!clock_class) {
314 goto end;
315 }
316 end:
317 bt_put(field_type);
318 return clock_class;
319 }
320
321 static
322 int get_packet_bounds_from_packet_context(
323 struct bt_ctf_field *packet_context,
324 struct bt_ctf_clock_class **_timestamp_begin_cc,
325 struct bt_ctf_field **_timestamp_begin,
326 struct bt_ctf_clock_class **_timestamp_end_cc,
327 struct bt_ctf_field **_timestamp_end)
328 {
329 int ret = 0;
330 struct bt_ctf_clock_class *timestamp_begin_cc = NULL;
331 struct bt_ctf_clock_class *timestamp_end_cc = NULL;
332 struct bt_ctf_field *timestamp_begin = NULL;
333 struct bt_ctf_field *timestamp_end = NULL;
334
335 timestamp_begin = bt_ctf_field_structure_get_field_by_name(
336 packet_context, "timestamp_begin");
337 if (!timestamp_begin) {
338 BT_LOGD_STR("Cannot retrieve timestamp_begin field in packet context.");
339 ret = -1;
340 goto end;
341 }
342
343 timestamp_begin_cc = get_field_mapped_clock_class(timestamp_begin);
344 if (!timestamp_begin_cc) {
345 BT_LOGD_STR("Cannot retrieve the clock mapped to timestamp_begin.");
346 }
347
348 timestamp_end = bt_ctf_field_structure_get_field_by_name(
349 packet_context, "timestamp_end");
350 if (!timestamp_end) {
351 BT_LOGD_STR("Cannot retrieve timestamp_end field in packet context.");
352 ret = -1;
353 goto end;
354 }
355
356 timestamp_end_cc = get_field_mapped_clock_class(timestamp_end);
357 if (!timestamp_end_cc) {
358 BT_LOGD_STR("Cannot retrieve the clock mapped to timestamp_end.");
359 }
360
361 if (_timestamp_begin_cc) {
362 *_timestamp_begin_cc = bt_get(timestamp_begin_cc);
363 }
364 if (_timestamp_begin) {
365 *_timestamp_begin = bt_get(timestamp_begin);
366 }
367 if (_timestamp_end_cc) {
368 *_timestamp_end_cc = bt_get(timestamp_end_cc);
369 }
370 if (_timestamp_end) {
371 *_timestamp_end = bt_get(timestamp_end);
372 }
373 end:
374 bt_put(timestamp_begin_cc);
375 bt_put(timestamp_end_cc);
376 bt_put(timestamp_begin);
377 bt_put(timestamp_end);
378 return ret;
379 }
380
381 static
382 int get_ds_file_packet_bounds_clock_classes(struct ctf_fs_ds_file *ds_file,
383 struct bt_ctf_clock_class **timestamp_begin_cc,
384 struct bt_ctf_clock_class **timestamp_end_cc)
385 {
386 struct bt_ctf_field *packet_context = NULL;
387 int ret = ctf_fs_ds_file_get_packet_header_context_fields(ds_file,
388 NULL, &packet_context);
389
390 if (ret || !packet_context) {
391 BT_LOGD("Cannot retrieve packet context field of stream \'%s\'",
392 ds_file->file->path->str);
393 ret = -1;
394 goto end;
395 }
396
397 ret = get_packet_bounds_from_packet_context(packet_context,
398 timestamp_begin_cc, NULL,
399 timestamp_end_cc, NULL);
400 end:
401 bt_put(packet_context);
402 return ret;
403 }
404
405 static
406 int convert_cycles_to_ns(struct bt_ctf_clock_class *clock_class,
407 uint64_t cycles, int64_t *ns)
408 {
409 int ret = 0;
410 struct bt_ctf_clock_value *clock_value;
411
412 assert(ns);
413 clock_value = bt_ctf_clock_value_create(clock_class, cycles);
414 if (!clock_value) {
415 ret = -1;
416 goto end;
417 }
418
419 ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, ns);
420 if (ret) {
421 goto end;
422 }
423 end:
424 bt_put(clock_value);
425 return ret;
426 }
427
428 static
429 struct ctf_fs_ds_index *build_index_from_idx_file(
430 struct ctf_fs_ds_file *ds_file)
431 {
432 int ret;
433 gchar *directory = NULL;
434 gchar *basename = NULL;
435 GString *index_basename = NULL;
436 gchar *index_file_path = NULL;
437 GMappedFile *mapped_file = NULL;
438 gsize filesize;
439 const char *mmap_begin = NULL, *file_pos = NULL;
440 const struct ctf_packet_index_file_hdr *header = NULL;
441 struct ctf_fs_ds_index *index = NULL;
442 struct ctf_fs_ds_index_entry *index_entry = NULL;
443 uint64_t total_packets_size = 0;
444 size_t file_index_entry_size;
445 size_t file_entry_count;
446 size_t i;
447 struct bt_ctf_clock_class *timestamp_begin_cc = NULL;
448 struct bt_ctf_clock_class *timestamp_end_cc = NULL;
449
450 BT_LOGD("Building index from .idx file of stream file %s",
451 ds_file->file->path->str);
452
453 ret = get_ds_file_packet_bounds_clock_classes(ds_file,
454 &timestamp_begin_cc, &timestamp_end_cc);
455 if (ret) {
456 BT_LOGD_STR("Cannot get clock classes of \"timestamp_begin\" "
457 "and \"timestamp_end\" fields");
458 goto error;
459 }
460
461 /* Look for index file in relative path index/name.idx. */
462 basename = g_path_get_basename(ds_file->file->path->str);
463 if (!basename) {
464 BT_LOGE("Cannot get the basename of datastream file %s",
465 ds_file->file->path->str);
466 goto error;
467 }
468
469 directory = g_path_get_dirname(ds_file->file->path->str);
470 if (!directory) {
471 BT_LOGE("Cannot get dirname of datastream file %s",
472 ds_file->file->path->str);
473 goto error;
474 }
475
476 index_basename = g_string_new(basename);
477 if (!index_basename) {
478 BT_LOGE_STR("Cannot allocate index file basename string");
479 goto error;
480 }
481
482 g_string_append(index_basename, ".idx");
483 index_file_path = g_build_filename(directory, "index",
484 index_basename->str, NULL);
485 mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL);
486 if (!mapped_file) {
487 BT_LOGD("Cannot create new mapped file %s",
488 index_file_path);
489 goto error;
490 }
491
492 /*
493 * The g_mapped_file API limits us to 4GB files on 32-bit.
494 * Traces with such large indexes have never been seen in the wild,
495 * but this would need to be adjusted to support them.
496 */
497 filesize = g_mapped_file_get_length(mapped_file);
498 if (filesize < sizeof(*header)) {
499 BT_LOGW("Invalid LTTng trace index file: "
500 "file size (%zu bytes) < header size (%zu bytes)",
501 filesize, sizeof(*header));
502 goto error;
503 }
504
505 mmap_begin = g_mapped_file_get_contents(mapped_file);
506 header = (struct ctf_packet_index_file_hdr *) mmap_begin;
507
508 file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header);
509 if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
510 BT_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
511 goto error;
512 }
513
514 file_index_entry_size = be32toh(header->packet_index_len);
515 file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
516 if ((filesize - sizeof(*header)) % file_index_entry_size) {
517 BT_LOGW("Invalid LTTng trace index: the index's size after the header "
518 "(%zu bytes) is not a multiple of the index entry size "
519 "(%zu bytes)", (filesize - sizeof(*header)),
520 sizeof(*header));
521 goto error;
522 }
523
524 index = ctf_fs_ds_index_create(file_entry_count);
525 if (!index) {
526 goto error;
527 }
528
529 index_entry = (struct ctf_fs_ds_index_entry *) &g_array_index(
530 index->entries, struct ctf_fs_ds_index_entry, 0);
531 for (i = 0; i < file_entry_count; i++) {
532 struct ctf_packet_index *file_index =
533 (struct ctf_packet_index *) file_pos;
534 uint64_t packet_size = be64toh(file_index->packet_size);
535
536 if (packet_size % CHAR_BIT) {
537 BT_LOGW("Invalid packet size encountered in LTTng trace index file");
538 goto error;
539 }
540
541 /* Convert size in bits to bytes. */
542 packet_size /= CHAR_BIT;
543 index_entry->packet_size = packet_size;
544
545 index_entry->offset = be64toh(file_index->offset);
546 if (i != 0 && index_entry->offset < (index_entry - 1)->offset) {
547 BT_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
548 "previous offset=%" PRIu64 ", current offset=%" PRIu64,
549 (index_entry - 1)->offset, index_entry->offset);
550 goto error;
551 }
552
553 index_entry->timestamp_begin = be64toh(file_index->timestamp_begin);
554 index_entry->timestamp_end = be64toh(file_index->timestamp_end);
555 if (index_entry->timestamp_end < index_entry->timestamp_begin) {
556 BT_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
557 "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64,
558 index_entry->timestamp_begin,
559 index_entry->timestamp_end);
560 goto error;
561 }
562
563 /* Convert the packet's bound to nanoseconds since Epoch. */
564 ret = convert_cycles_to_ns(timestamp_begin_cc,
565 index_entry->timestamp_begin,
566 &index_entry->timestamp_begin_ns);
567 if (ret) {
568 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
569 goto error;
570 }
571 ret = convert_cycles_to_ns(timestamp_end_cc,
572 index_entry->timestamp_end,
573 &index_entry->timestamp_end_ns);
574 if (ret) {
575 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
576 goto error;
577 }
578
579 total_packets_size += packet_size;
580 file_pos += file_index_entry_size;
581 index_entry++;
582 }
583
584 /* Validate that the index addresses the complete stream. */
585 if (ds_file->file->size != total_packets_size) {
586 BT_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
587 "file-size=%" PRIu64 ", total-packets-size=%" PRIu64,
588 ds_file->file->size, total_packets_size);
589 goto error;
590 }
591 end:
592 g_free(directory);
593 g_free(basename);
594 g_free(index_file_path);
595 if (index_basename) {
596 g_string_free(index_basename, TRUE);
597 }
598 if (mapped_file) {
599 g_mapped_file_unref(mapped_file);
600 }
601 bt_put(timestamp_begin_cc);
602 bt_put(timestamp_end_cc);
603 return index;
604 error:
605 ctf_fs_ds_index_destroy(index);
606 index = NULL;
607 goto end;
608 }
609
610 static
611 int init_index_entry(struct ctf_fs_ds_index_entry *entry,
612 struct bt_ctf_field *packet_context, off_t packet_size,
613 off_t packet_offset)
614 {
615 int ret;
616 struct bt_ctf_field *timestamp_begin = NULL;
617 struct bt_ctf_field *timestamp_end = NULL;
618 struct bt_ctf_clock_class *timestamp_begin_cc = NULL;
619 struct bt_ctf_clock_class *timestamp_end_cc = NULL;
620
621 ret = get_packet_bounds_from_packet_context(packet_context,
622 &timestamp_begin_cc, &timestamp_begin,
623 &timestamp_end_cc, &timestamp_end);
624 if (ret || !timestamp_begin_cc || !timestamp_begin ||
625 !timestamp_end_cc || ! timestamp_end) {
626 BT_LOGD_STR("Failed to determine time bound fields of packet.");
627 goto end;
628 }
629
630 assert(packet_offset >= 0);
631 entry->offset = packet_offset;
632
633 assert(packet_size >= 0);
634 entry->packet_size = packet_size;
635
636 ret = bt_ctf_field_unsigned_integer_get_value(timestamp_begin,
637 &entry->timestamp_begin);
638 if (ret) {
639 goto end;
640 }
641 ret = bt_ctf_field_unsigned_integer_get_value(timestamp_end,
642 &entry->timestamp_end);
643 if (ret) {
644 goto end;
645 }
646
647 /* Convert the packet's bound to nanoseconds since Epoch. */
648 ret = convert_cycles_to_ns(timestamp_begin_cc,
649 entry->timestamp_begin,
650 &entry->timestamp_begin_ns);
651 if (ret) {
652 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
653 goto end;
654 }
655
656 ret = convert_cycles_to_ns(timestamp_end_cc,
657 entry->timestamp_end,
658 &entry->timestamp_end_ns);
659 if (ret) {
660 BT_LOGD_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
661 goto end;
662 }
663 end:
664 bt_put(timestamp_begin);
665 bt_put(timestamp_begin_cc);
666 bt_put(timestamp_end);
667 bt_put(timestamp_end_cc);
668 return ret;
669 }
670
671 static
672 struct ctf_fs_ds_index *build_index_from_stream_file(
673 struct ctf_fs_ds_file *ds_file)
674 {
675 int ret;
676 struct ctf_fs_ds_index *index = NULL;
677 enum bt_ctf_notif_iter_status iter_status;
678 struct bt_ctf_field *packet_context = NULL;
679
680 BT_LOGD("Indexing stream file %s", ds_file->file->path->str);
681
682 index = ctf_fs_ds_index_create(0);
683 if (!index) {
684 goto error;
685 }
686
687 do {
688 off_t current_packet_offset;
689 off_t next_packet_offset;
690 off_t current_packet_size, current_packet_size_bytes;
691 struct ctf_fs_ds_index_entry *entry;
692
693 iter_status = bt_ctf_notif_iter_get_packet_header_context_fields(
694 ds_file->notif_iter, NULL, &packet_context);
695 if (iter_status != BT_CTF_NOTIF_ITER_STATUS_OK) {
696 if (iter_status == BT_CTF_NOTIF_ITER_STATUS_EOF) {
697 break;
698 }
699 goto error;
700 }
701 current_packet_offset =
702 bt_ctf_notif_iter_get_current_packet_offset(
703 ds_file->notif_iter);
704 if (current_packet_offset < 0) {
705 BT_LOGE_STR("Cannot get the current packet's offset.");
706 goto error;
707 }
708
709 current_packet_size = bt_ctf_notif_iter_get_current_packet_size(
710 ds_file->notif_iter);
711 if (current_packet_size < 0) {
712 BT_LOGE("Cannot get packet size: packet-offset=%jd",
713 current_packet_offset);
714 goto error;
715 }
716 current_packet_size_bytes =
717 ((current_packet_size + 7) & ~7) / CHAR_BIT;
718
719 if (current_packet_offset + current_packet_size_bytes >
720 ds_file->file->size) {
721 BT_LOGW("Invalid packet size reported in file: stream=\"%s\", "
722 "packet-offset=%jd, packet-size-bytes=%jd, "
723 "file-size=%jd",
724 ds_file->file->path->str,
725 current_packet_offset,
726 current_packet_size_bytes,
727 ds_file->file->size);
728 goto error;
729 }
730
731 next_packet_offset = current_packet_offset +
732 current_packet_size_bytes;
733 BT_LOGD("Seeking to next packet: current-packet-offset=%jd, "
734 "next-packet-offset=%jd", current_packet_offset,
735 next_packet_offset);
736
737 entry = ctf_fs_ds_index_add_new_entry(index);
738 if (!entry) {
739 BT_LOGE_STR("Failed to allocate a new index entry.");
740 goto error;
741 }
742
743 ret = init_index_entry(entry, packet_context,
744 current_packet_size_bytes,
745 current_packet_offset);
746 if (ret) {
747 goto error;
748 }
749
750 iter_status = bt_ctf_notif_iter_seek(ds_file->notif_iter,
751 next_packet_offset);
752 BT_PUT(packet_context);
753 } while (iter_status == BT_CTF_NOTIF_ITER_STATUS_OK);
754
755 if (iter_status != BT_CTF_NOTIF_ITER_STATUS_EOF) {
756 goto error;
757 }
758 end:
759 bt_put(packet_context);
760 return index;
761 error:
762 ctf_fs_ds_index_destroy(index);
763 index = NULL;
764 goto end;
765 }
766
767 BT_HIDDEN
768 struct ctf_fs_ds_file *ctf_fs_ds_file_create(
769 struct ctf_fs_trace *ctf_fs_trace,
770 struct bt_ctf_notif_iter *notif_iter,
771 struct bt_ctf_stream *stream, const char *path)
772 {
773 int ret;
774 const size_t page_size = bt_common_get_page_size();
775 struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1);
776
777 if (!ds_file) {
778 goto error;
779 }
780
781 ds_file->file = ctf_fs_file_create();
782 if (!ds_file->file) {
783 goto error;
784 }
785
786 ds_file->stream = bt_get(stream);
787 ds_file->cc_prio_map = bt_get(ctf_fs_trace->cc_prio_map);
788 g_string_assign(ds_file->file->path, path);
789 ret = ctf_fs_file_open(ds_file->file, "rb");
790 if (ret) {
791 goto error;
792 }
793
794 ds_file->notif_iter = notif_iter;
795 bt_ctf_notif_iter_set_medops_data(ds_file->notif_iter, ds_file);
796 if (!ds_file->notif_iter) {
797 goto error;
798 }
799
800 ds_file->mmap_max_len = page_size * 2048;
801
802 goto end;
803
804 error:
805 /* Do not touch "borrowed" file. */
806 ctf_fs_ds_file_destroy(ds_file);
807 ds_file = NULL;
808
809 end:
810 return ds_file;
811 }
812
813 BT_HIDDEN
814 struct ctf_fs_ds_index *ctf_fs_ds_file_build_index(
815 struct ctf_fs_ds_file *ds_file)
816 {
817 struct ctf_fs_ds_index *index;
818
819 index = build_index_from_idx_file(ds_file);
820 if (index) {
821 goto end;
822 }
823
824 BT_LOGD("Failed to build index from .index file; "
825 "falling back to stream indexing.");
826 index = build_index_from_stream_file(ds_file);
827 end:
828 return index;
829 }
830
831 BT_HIDDEN
832 void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file)
833 {
834 if (!ds_file) {
835 return;
836 }
837
838 bt_put(ds_file->cc_prio_map);
839 bt_put(ds_file->stream);
840 (void) ds_file_munmap(ds_file);
841
842 if (ds_file->file) {
843 ctf_fs_file_destroy(ds_file->file);
844 }
845
846 g_free(ds_file);
847 }
848
849 BT_HIDDEN
850 struct bt_notification_iterator_next_return ctf_fs_ds_file_next(
851 struct ctf_fs_ds_file *ds_file)
852 {
853 enum bt_ctf_notif_iter_status notif_iter_status;
854 struct bt_notification_iterator_next_return ret = {
855 .status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR,
856 .notification = NULL,
857 };
858
859 notif_iter_status = bt_ctf_notif_iter_get_next_notification(
860 ds_file->notif_iter, ds_file->cc_prio_map, &ret.notification);
861
862 switch (notif_iter_status) {
863 case BT_CTF_NOTIF_ITER_STATUS_EOF:
864 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_END;
865 break;
866 case BT_CTF_NOTIF_ITER_STATUS_OK:
867 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
868 break;
869 case BT_CTF_NOTIF_ITER_STATUS_AGAIN:
870 /*
871 * Should not make it this far as this is
872 * medium-specific; there is nothing for the user to do
873 * and it should have been handled upstream.
874 */
875 abort();
876 case BT_CTF_NOTIF_ITER_STATUS_INVAL:
877 case BT_CTF_NOTIF_ITER_STATUS_ERROR:
878 default:
879 ret.status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
880 break;
881 }
882
883 return ret;
884 }
885
886 BT_HIDDEN
887 int ctf_fs_ds_file_get_packet_header_context_fields(
888 struct ctf_fs_ds_file *ds_file,
889 struct bt_ctf_field **packet_header_field,
890 struct bt_ctf_field **packet_context_field)
891 {
892 enum bt_ctf_notif_iter_status notif_iter_status;
893 int ret = 0;
894
895 assert(ds_file);
896 notif_iter_status = bt_ctf_notif_iter_get_packet_header_context_fields(
897 ds_file->notif_iter, packet_header_field, packet_context_field);
898 switch (notif_iter_status) {
899 case BT_CTF_NOTIF_ITER_STATUS_EOF:
900 case BT_CTF_NOTIF_ITER_STATUS_OK:
901 break;
902 case BT_CTF_NOTIF_ITER_STATUS_AGAIN:
903 abort();
904 case BT_CTF_NOTIF_ITER_STATUS_INVAL:
905 case BT_CTF_NOTIF_ITER_STATUS_ERROR:
906 default:
907 goto error;
908 break;
909 }
910
911 goto end;
912
913 error:
914 ret = -1;
915
916 if (packet_header_field) {
917 bt_put(*packet_header_field);
918 }
919
920 if (packet_context_field) {
921 bt_put(*packet_context_field);
922 }
923
924 end:
925 return ret;
926 }
927
928 BT_HIDDEN
929 void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index)
930 {
931 if (!index) {
932 return;
933 }
934
935 if (index->entries) {
936 g_array_free(index->entries, TRUE);
937 }
938 g_free(index);
939 }
This page took 0.046802 seconds and 3 git commands to generate.