ctf: assert that msg iter and medium seek offset is valid
[babeltrace.git] / src / 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 #define BT_COMP_LOG_SELF_COMP (self_comp)
26 #define BT_LOG_OUTPUT_LEVEL (log_level)
27 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/DS"
28 #include "logging/comp-logging.h"
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <glib.h>
35 #include <inttypes.h>
36 #include "compat/mman.h"
37 #include "compat/endian.h"
38 #include <babeltrace2/babeltrace.h>
39 #include "common/common.h"
40 #include "file.h"
41 #include "metadata.h"
42 #include "../common/msg-iter/msg-iter.h"
43 #include "common/assert.h"
44 #include "data-stream-file.h"
45 #include <string.h>
46
47 static inline
48 size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file)
49 {
50 BT_ASSERT_DBG(ds_file->mmap_len >= ds_file->request_offset_in_mapping);
51 return ds_file->mmap_len - ds_file->request_offset_in_mapping;
52 }
53
54 static
55 enum ctf_msg_iter_medium_status ds_file_munmap(
56 struct ctf_fs_ds_file *ds_file)
57 {
58 enum ctf_msg_iter_medium_status status;
59 bt_self_component *self_comp = ds_file->self_comp;
60 bt_logging_level log_level = ds_file->log_level;
61
62 BT_ASSERT(ds_file);
63
64 if (!ds_file->mmap_addr) {
65 status = CTF_MSG_ITER_MEDIUM_STATUS_OK;
66 goto end;
67 }
68
69 if (bt_munmap(ds_file->mmap_addr, ds_file->mmap_len)) {
70 BT_COMP_LOGE_ERRNO("Cannot memory-unmap file",
71 ": address=%p, size=%zu, file_path=\"%s\", file=%p",
72 ds_file->mmap_addr, ds_file->mmap_len,
73 ds_file->file ? ds_file->file->path->str : "NULL",
74 ds_file->file ? ds_file->file->fp : NULL);
75 status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
76 goto end;
77 }
78
79 ds_file->mmap_addr = NULL;
80
81 status = CTF_MSG_ITER_MEDIUM_STATUS_OK;
82 end:
83 return status;
84 }
85
86 static
87 enum ctf_msg_iter_medium_status ds_file_mmap_next(
88 struct ctf_fs_ds_file *ds_file)
89 {
90 enum ctf_msg_iter_medium_status status;
91 bt_self_component *self_comp = ds_file->self_comp;
92 bt_logging_level log_level = ds_file->log_level;
93
94 /* Unmap old region */
95 if (ds_file->mmap_addr) {
96 status = ds_file_munmap(ds_file);
97 if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) {
98 goto end;
99 }
100
101 /*
102 * mmap_len is guaranteed to be page-aligned except on the
103 * last mapping where it may not be possible (since the file's
104 * size itself may not be a page multiple).
105 */
106 ds_file->mmap_offset_in_file += ds_file->mmap_len;
107 ds_file->request_offset_in_mapping = 0;
108 }
109
110 ds_file->mmap_len = MIN(ds_file->file->size - ds_file->mmap_offset_in_file,
111 ds_file->mmap_max_len);
112 if (ds_file->mmap_len == 0) {
113 status = CTF_MSG_ITER_MEDIUM_STATUS_EOF;
114 goto end;
115 }
116 /* Map new region */
117 BT_ASSERT(ds_file->mmap_len);
118 ds_file->mmap_addr = bt_mmap((void *) 0, ds_file->mmap_len,
119 PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp),
120 ds_file->mmap_offset_in_file, ds_file->log_level);
121 if (ds_file->mmap_addr == MAP_FAILED) {
122 BT_COMP_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s",
123 ds_file->mmap_len, ds_file->file->path->str,
124 ds_file->file->fp, (intmax_t) ds_file->mmap_offset_in_file,
125 strerror(errno));
126 status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
127 goto end;
128 }
129
130 status = CTF_MSG_ITER_MEDIUM_STATUS_OK;
131 end:
132 return status;
133 }
134
135 static
136 enum ctf_msg_iter_medium_status medop_request_bytes(
137 size_t request_sz, uint8_t **buffer_addr,
138 size_t *buffer_sz, void *data)
139 {
140 enum ctf_msg_iter_medium_status status =
141 CTF_MSG_ITER_MEDIUM_STATUS_OK;
142 struct ctf_fs_ds_file *ds_file = data;
143 bt_self_component *self_comp = ds_file->self_comp;
144 bt_logging_level log_level = ds_file->log_level;
145
146 if (request_sz == 0) {
147 goto end;
148 }
149
150 /*
151 * Check if we have at least one memory-mapped byte left. If we don't,
152 * mmap the next file.
153 */
154 if (remaining_mmap_bytes(ds_file) == 0) {
155 /* Are we at the end of the file? */
156 if (ds_file->mmap_offset_in_file >= ds_file->file->size) {
157 BT_COMP_LOGD("Reached end of file \"%s\" (%p)",
158 ds_file->file->path->str, ds_file->file->fp);
159 status = CTF_MSG_ITER_MEDIUM_STATUS_EOF;
160 goto end;
161 }
162
163 status = ds_file_mmap_next(ds_file);
164 switch (status) {
165 case CTF_MSG_ITER_MEDIUM_STATUS_OK:
166 break;
167 case CTF_MSG_ITER_MEDIUM_STATUS_EOF:
168 goto end;
169 default:
170 BT_COMP_LOGE("Cannot memory-map next region of file \"%s\" (%p)",
171 ds_file->file->path->str,
172 ds_file->file->fp);
173 goto error;
174 }
175 }
176
177 *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz);
178 BT_ASSERT(ds_file->mmap_addr);
179 *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset_in_mapping;
180 ds_file->request_offset_in_mapping += *buffer_sz;
181 goto end;
182
183 error:
184 status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
185
186 end:
187 return status;
188 }
189
190 static
191 bt_stream *medop_borrow_stream(bt_stream_class *stream_class, int64_t stream_id,
192 void *data)
193 {
194 struct ctf_fs_ds_file *ds_file = data;
195 bt_stream_class *ds_file_stream_class;
196 bt_stream *stream = NULL;
197
198 ds_file_stream_class = bt_stream_borrow_class(
199 ds_file->stream);
200
201 if (stream_class != ds_file_stream_class) {
202 /*
203 * Not supported: two packets described by two different
204 * stream classes within the same data stream file.
205 */
206 goto end;
207 }
208
209 stream = ds_file->stream;
210
211 end:
212 return stream;
213 }
214
215 static
216 enum ctf_msg_iter_medium_status medop_seek(off_t offset, void *data)
217 {
218 enum ctf_msg_iter_medium_status status;
219 struct ctf_fs_ds_file *ds_file = data;
220 off_t offset_in_mapping, file_size = ds_file->file->size;
221 bt_self_component *self_comp = ds_file->self_comp;
222 bt_logging_level log_level = ds_file->log_level;
223
224 BT_ASSERT(offset >= 0);
225 BT_ASSERT(offset < file_size);
226
227 /* If there is no current mapping, map the right file directly. */
228 if (!ds_file->mmap_addr) {
229 goto map_requested_offset;
230 }
231
232 /*
233 * Determine whether or not the destination is contained within the
234 * current mapping.
235 */
236 if (offset < ds_file->mmap_offset_in_file ||
237 offset >= ds_file->mmap_offset_in_file + ds_file->mmap_len) {
238 BT_COMP_LOGD("Medium seek request cannot be accomodated by the current "
239 "file mapping: offset=%jd, mmap-offset=%jd, "
240 "mmap-len=%zu", (intmax_t) offset, (intmax_t) ds_file->mmap_offset_in_file,
241 ds_file->mmap_len);
242 status = ds_file_munmap(ds_file);
243 if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) {
244 goto end;
245 }
246 goto map_requested_offset;
247 } else {
248 ds_file->request_offset_in_mapping = offset - ds_file->mmap_offset_in_file;
249 goto test_end;
250 }
251
252 map_requested_offset:
253 offset_in_mapping = offset %
254 bt_mmap_get_offset_align_size(ds_file->log_level);
255
256 ds_file->mmap_offset_in_file = offset - offset_in_mapping;
257 ds_file->request_offset_in_mapping = offset_in_mapping;
258 status = ds_file_mmap_next(ds_file);
259 if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) {
260 goto end;
261 }
262
263 test_end:
264 ds_file->end_reached = (offset == file_size);
265
266 status = CTF_MSG_ITER_MEDIUM_STATUS_OK;
267 end:
268 return status;
269 }
270
271 BT_HIDDEN
272 struct ctf_msg_iter_medium_ops ctf_fs_ds_file_medops = {
273 .request_bytes = medop_request_bytes,
274 .borrow_stream = medop_borrow_stream,
275 .seek = medop_seek,
276 };
277
278 static
279 struct ctf_fs_ds_index_entry *ctf_fs_ds_index_entry_create(
280 bt_self_component *self_comp, bt_logging_level log_level)
281 {
282 struct ctf_fs_ds_index_entry *entry;
283
284 entry = g_new0(struct ctf_fs_ds_index_entry, 1);
285 if (!entry) {
286 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to allocate a ctf_fs_ds_index_entry.");
287 goto end;
288 }
289
290 entry->packet_seq_num = UINT64_MAX;
291
292 end:
293 return entry;
294 }
295
296 static
297 int convert_cycles_to_ns(struct ctf_clock_class *clock_class,
298 uint64_t cycles, int64_t *ns)
299 {
300 return bt_util_clock_cycles_to_ns_from_origin(cycles,
301 clock_class->frequency, clock_class->offset_seconds,
302 clock_class->offset_cycles, ns);
303 }
304
305 static
306 struct ctf_fs_ds_index *build_index_from_idx_file(
307 struct ctf_fs_ds_file *ds_file,
308 struct ctf_fs_ds_file_info *file_info,
309 struct ctf_msg_iter *msg_iter)
310 {
311 int ret;
312 gchar *directory = NULL;
313 gchar *basename = NULL;
314 GString *index_basename = NULL;
315 gchar *index_file_path = NULL;
316 GMappedFile *mapped_file = NULL;
317 gsize filesize;
318 const char *mmap_begin = NULL, *file_pos = NULL;
319 const struct ctf_packet_index_file_hdr *header = NULL;
320 struct ctf_fs_ds_index *index = NULL;
321 struct ctf_fs_ds_index_entry *index_entry = NULL, *prev_index_entry = NULL;
322 uint64_t total_packets_size = 0;
323 size_t file_index_entry_size;
324 size_t file_entry_count;
325 size_t i;
326 struct ctf_stream_class *sc;
327 struct ctf_msg_iter_packet_properties props;
328 uint32_t version_major, version_minor;
329 bt_self_component *self_comp = ds_file->self_comp;
330 bt_logging_level log_level = ds_file->log_level;
331
332 BT_COMP_LOGI("Building index from .idx file of stream file %s",
333 ds_file->file->path->str);
334 ret = ctf_msg_iter_get_packet_properties(msg_iter, &props);
335 if (ret) {
336 BT_COMP_LOGI_STR("Cannot read first packet's header and context fields.");
337 goto error;
338 }
339
340 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
341 props.stream_class_id);
342 BT_ASSERT(sc);
343 if (!sc->default_clock_class) {
344 BT_COMP_LOGI_STR("Cannot find stream class's default clock class.");
345 goto error;
346 }
347
348 /* Look for index file in relative path index/name.idx. */
349 basename = g_path_get_basename(ds_file->file->path->str);
350 if (!basename) {
351 BT_COMP_LOGE("Cannot get the basename of datastream file %s",
352 ds_file->file->path->str);
353 goto error;
354 }
355
356 directory = g_path_get_dirname(ds_file->file->path->str);
357 if (!directory) {
358 BT_COMP_LOGE("Cannot get dirname of datastream file %s",
359 ds_file->file->path->str);
360 goto error;
361 }
362
363 index_basename = g_string_new(basename);
364 if (!index_basename) {
365 BT_COMP_LOGE_STR("Cannot allocate index file basename string");
366 goto error;
367 }
368
369 g_string_append(index_basename, ".idx");
370 index_file_path = g_build_filename(directory, "index",
371 index_basename->str, NULL);
372 mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL);
373 if (!mapped_file) {
374 BT_COMP_LOGD("Cannot create new mapped file %s",
375 index_file_path);
376 goto error;
377 }
378
379 /*
380 * The g_mapped_file API limits us to 4GB files on 32-bit.
381 * Traces with such large indexes have never been seen in the wild,
382 * but this would need to be adjusted to support them.
383 */
384 filesize = g_mapped_file_get_length(mapped_file);
385 if (filesize < sizeof(*header)) {
386 BT_COMP_LOGW("Invalid LTTng trace index file: "
387 "file size (%zu bytes) < header size (%zu bytes)",
388 filesize, sizeof(*header));
389 goto error;
390 }
391
392 mmap_begin = g_mapped_file_get_contents(mapped_file);
393 header = (struct ctf_packet_index_file_hdr *) mmap_begin;
394
395 file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header);
396 if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
397 BT_COMP_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
398 goto error;
399 }
400
401 version_major = be32toh(header->index_major);
402 version_minor = be32toh(header->index_minor);
403 if (version_major != 1) {
404 BT_COMP_LOGW(
405 "Unknown LTTng trace index version: "
406 "major=%" PRIu32 ", minor=%" PRIu32,
407 version_major, version_minor);
408 goto error;
409 }
410
411 file_index_entry_size = be32toh(header->packet_index_len);
412 file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
413 if ((filesize - sizeof(*header)) % file_index_entry_size) {
414 BT_COMP_LOGW("Invalid LTTng trace index: the index's size after the header "
415 "(%zu bytes) is not a multiple of the index entry size "
416 "(%zu bytes)", (filesize - sizeof(*header)),
417 sizeof(*header));
418 goto error;
419 }
420
421 index = ctf_fs_ds_index_create(ds_file->log_level, ds_file->self_comp);
422 if (!index) {
423 goto error;
424 }
425
426 for (i = 0; i < file_entry_count; i++) {
427 struct ctf_packet_index *file_index =
428 (struct ctf_packet_index *) file_pos;
429 uint64_t packet_size = be64toh(file_index->packet_size);
430
431 if (packet_size % CHAR_BIT) {
432 BT_COMP_LOGW("Invalid packet size encountered in LTTng trace index file");
433 goto error;
434 }
435
436 index_entry = ctf_fs_ds_index_entry_create(
437 ds_file->self_comp, ds_file->log_level);
438 if (!index_entry) {
439 BT_COMP_LOGE_APPEND_CAUSE(ds_file->self_comp,
440 "Failed to create a ctf_fs_ds_index_entry.");
441 goto error;
442 }
443
444 /* Set path to stream file. */
445 index_entry->path = file_info->path->str;
446
447 /* Convert size in bits to bytes. */
448 packet_size /= CHAR_BIT;
449 index_entry->packet_size = packet_size;
450
451 index_entry->offset = be64toh(file_index->offset);
452 if (i != 0 && index_entry->offset < prev_index_entry->offset) {
453 BT_COMP_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
454 "previous offset=%" PRIu64 ", current offset=%" PRIu64,
455 prev_index_entry->offset, index_entry->offset);
456 goto error;
457 }
458
459 index_entry->timestamp_begin = be64toh(file_index->timestamp_begin);
460 index_entry->timestamp_end = be64toh(file_index->timestamp_end);
461 if (index_entry->timestamp_end < index_entry->timestamp_begin) {
462 BT_COMP_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
463 "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64,
464 index_entry->timestamp_begin,
465 index_entry->timestamp_end);
466 goto error;
467 }
468
469 /* Convert the packet's bound to nanoseconds since Epoch. */
470 ret = convert_cycles_to_ns(sc->default_clock_class,
471 index_entry->timestamp_begin,
472 &index_entry->timestamp_begin_ns);
473 if (ret) {
474 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
475 goto error;
476 }
477 ret = convert_cycles_to_ns(sc->default_clock_class,
478 index_entry->timestamp_end,
479 &index_entry->timestamp_end_ns);
480 if (ret) {
481 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
482 goto error;
483 }
484
485 if (version_minor >= 1) {
486 index_entry->packet_seq_num = be64toh(file_index->packet_seq_num);
487 }
488
489 total_packets_size += packet_size;
490 file_pos += file_index_entry_size;
491
492 prev_index_entry = index_entry;
493
494 /* Give ownership of `index_entry` to `index->entries`. */
495 g_ptr_array_add(index->entries, index_entry);
496 index_entry = NULL;
497 }
498
499 /* Validate that the index addresses the complete stream. */
500 if (ds_file->file->size != total_packets_size) {
501 BT_COMP_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
502 "file-size=%" PRIu64 ", total-packets-size=%" PRIu64,
503 ds_file->file->size, total_packets_size);
504 goto error;
505 }
506 end:
507 g_free(directory);
508 g_free(basename);
509 g_free(index_file_path);
510 if (index_basename) {
511 g_string_free(index_basename, TRUE);
512 }
513 if (mapped_file) {
514 g_mapped_file_unref(mapped_file);
515 }
516 return index;
517 error:
518 ctf_fs_ds_index_destroy(index);
519 g_free(index_entry);
520 index = NULL;
521 goto end;
522 }
523
524 static
525 int init_index_entry(struct ctf_fs_ds_index_entry *entry,
526 struct ctf_fs_ds_file *ds_file,
527 struct ctf_msg_iter_packet_properties *props,
528 off_t packet_size, off_t packet_offset)
529 {
530 int ret = 0;
531 struct ctf_stream_class *sc;
532 bt_self_component *self_comp = ds_file->self_comp;
533 bt_logging_level log_level = ds_file->log_level;
534
535 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
536 props->stream_class_id);
537 BT_ASSERT(sc);
538 BT_ASSERT(packet_offset >= 0);
539 entry->offset = packet_offset;
540 BT_ASSERT(packet_size >= 0);
541 entry->packet_size = packet_size;
542
543 if (props->snapshots.beginning_clock != UINT64_C(-1)) {
544 entry->timestamp_begin = props->snapshots.beginning_clock;
545
546 /* Convert the packet's bound to nanoseconds since Epoch. */
547 ret = convert_cycles_to_ns(sc->default_clock_class,
548 props->snapshots.beginning_clock,
549 &entry->timestamp_begin_ns);
550 if (ret) {
551 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
552 goto end;
553 }
554 } else {
555 entry->timestamp_begin = UINT64_C(-1);
556 entry->timestamp_begin_ns = UINT64_C(-1);
557 }
558
559 if (props->snapshots.end_clock != UINT64_C(-1)) {
560 entry->timestamp_end = props->snapshots.end_clock;
561
562 /* Convert the packet's bound to nanoseconds since Epoch. */
563 ret = convert_cycles_to_ns(sc->default_clock_class,
564 props->snapshots.end_clock,
565 &entry->timestamp_end_ns);
566 if (ret) {
567 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
568 goto end;
569 }
570 } else {
571 entry->timestamp_end = UINT64_C(-1);
572 entry->timestamp_end_ns = UINT64_C(-1);
573 }
574
575 end:
576 return ret;
577 }
578
579 static
580 struct ctf_fs_ds_index *build_index_from_stream_file(
581 struct ctf_fs_ds_file *ds_file,
582 struct ctf_fs_ds_file_info *file_info,
583 struct ctf_msg_iter *msg_iter)
584 {
585 int ret;
586 struct ctf_fs_ds_index *index = NULL;
587 enum ctf_msg_iter_status iter_status = CTF_MSG_ITER_STATUS_OK;
588 off_t current_packet_offset_bytes = 0;
589 bt_self_component *self_comp = ds_file->self_comp;
590 bt_logging_level log_level = ds_file->log_level;
591
592 BT_COMP_LOGI("Indexing stream file %s", ds_file->file->path->str);
593
594 index = ctf_fs_ds_index_create(ds_file->log_level, ds_file->self_comp);
595 if (!index) {
596 goto error;
597 }
598
599 while (true) {
600 off_t current_packet_size_bytes;
601 struct ctf_fs_ds_index_entry *index_entry;
602 struct ctf_msg_iter_packet_properties props;
603
604 if (current_packet_offset_bytes < 0) {
605 BT_COMP_LOGE_STR("Cannot get the current packet's offset.");
606 goto error;
607 } else if (current_packet_offset_bytes > ds_file->file->size) {
608 BT_COMP_LOGE_STR("Unexpected current packet's offset (larger than file).");
609 goto error;
610 } else if (current_packet_offset_bytes == ds_file->file->size) {
611 /* No more data */
612 break;
613 }
614
615 iter_status = ctf_msg_iter_seek(msg_iter,
616 current_packet_offset_bytes);
617 if (iter_status != CTF_MSG_ITER_STATUS_OK) {
618 goto error;
619 }
620
621 iter_status = ctf_msg_iter_get_packet_properties(
622 msg_iter, &props);
623 if (iter_status != CTF_MSG_ITER_STATUS_OK) {
624 goto error;
625 }
626
627 if (props.exp_packet_total_size >= 0) {
628 current_packet_size_bytes =
629 (uint64_t) props.exp_packet_total_size / 8;
630 } else {
631 current_packet_size_bytes = ds_file->file->size;
632 }
633
634 if (current_packet_offset_bytes + current_packet_size_bytes >
635 ds_file->file->size) {
636 BT_COMP_LOGW("Invalid packet size reported in file: stream=\"%s\", "
637 "packet-offset=%jd, packet-size-bytes=%jd, "
638 "file-size=%jd",
639 ds_file->file->path->str,
640 (intmax_t) current_packet_offset_bytes,
641 (intmax_t) current_packet_size_bytes,
642 (intmax_t) ds_file->file->size);
643 goto error;
644 }
645
646 index_entry = ctf_fs_ds_index_entry_create(
647 ds_file->self_comp, ds_file->log_level);
648 if (!index_entry) {
649 BT_COMP_LOGE_APPEND_CAUSE(ds_file->self_comp,
650 "Failed to create a ctf_fs_ds_index_entry.");
651 goto error;
652 }
653
654 /* Set path to stream file. */
655 index_entry->path = file_info->path->str;
656
657 ret = init_index_entry(index_entry, ds_file, &props,
658 current_packet_size_bytes, current_packet_offset_bytes);
659 if (ret) {
660 g_free(index_entry);
661 goto error;
662 }
663
664 g_ptr_array_add(index->entries, index_entry);
665
666 current_packet_offset_bytes += current_packet_size_bytes;
667 BT_COMP_LOGD("Seeking to next packet: current-packet-offset=%jd, "
668 "next-packet-offset=%jd",
669 (intmax_t) (current_packet_offset_bytes - current_packet_size_bytes),
670 (intmax_t) current_packet_offset_bytes);
671 }
672
673 end:
674 return index;
675
676 error:
677 ctf_fs_ds_index_destroy(index);
678 index = NULL;
679 goto end;
680 }
681
682 BT_HIDDEN
683 struct ctf_fs_ds_file *ctf_fs_ds_file_create(
684 struct ctf_fs_trace *ctf_fs_trace,
685 bt_self_message_iterator *self_msg_iter,
686 bt_stream *stream, const char *path,
687 bt_logging_level log_level)
688 {
689 int ret;
690 const size_t offset_align = bt_mmap_get_offset_align_size(log_level);
691 struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1);
692
693 if (!ds_file) {
694 goto error;
695 }
696
697 ds_file->log_level = log_level;
698 ds_file->self_comp = ctf_fs_trace->self_comp;
699 ds_file->self_msg_iter = self_msg_iter;
700 ds_file->file = ctf_fs_file_create(log_level, ds_file->self_comp);
701 if (!ds_file->file) {
702 goto error;
703 }
704
705 ds_file->stream = stream;
706 bt_stream_get_ref(ds_file->stream);
707 ds_file->metadata = ctf_fs_trace->metadata;
708 g_string_assign(ds_file->file->path, path);
709 ret = ctf_fs_file_open(ds_file->file, "rb");
710 if (ret) {
711 goto error;
712 }
713
714 ds_file->mmap_max_len = offset_align * 2048;
715
716 goto end;
717
718 error:
719 /* Do not touch "borrowed" file. */
720 ctf_fs_ds_file_destroy(ds_file);
721 ds_file = NULL;
722
723 end:
724 return ds_file;
725 }
726
727 BT_HIDDEN
728 struct ctf_fs_ds_index *ctf_fs_ds_file_build_index(
729 struct ctf_fs_ds_file *ds_file,
730 struct ctf_fs_ds_file_info *file_info,
731 struct ctf_msg_iter *msg_iter)
732 {
733 struct ctf_fs_ds_index *index;
734 bt_self_component *self_comp = ds_file->self_comp;
735 bt_logging_level log_level = ds_file->log_level;
736
737 index = build_index_from_idx_file(ds_file, file_info, msg_iter);
738 if (index) {
739 goto end;
740 }
741
742 BT_COMP_LOGI("Failed to build index from .index file; "
743 "falling back to stream indexing.");
744 index = build_index_from_stream_file(ds_file, file_info, msg_iter);
745 end:
746 return index;
747 }
748
749 BT_HIDDEN
750 struct ctf_fs_ds_index *ctf_fs_ds_index_create(bt_logging_level log_level,
751 bt_self_component *self_comp)
752 {
753 struct ctf_fs_ds_index *index = g_new0(struct ctf_fs_ds_index, 1);
754
755 if (!index) {
756 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
757 "Failed to allocate index");
758 goto error;
759 }
760
761 index->entries = g_ptr_array_new_with_free_func((GDestroyNotify) g_free);
762 if (!index->entries) {
763 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
764 "Failed to allocate index entries.");
765 goto error;
766 }
767
768 goto end;
769
770 error:
771 ctf_fs_ds_index_destroy(index);
772 index = NULL;
773 end:
774 return index;
775 }
776
777 BT_HIDDEN
778 void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file)
779 {
780 if (!ds_file) {
781 return;
782 }
783
784 bt_stream_put_ref(ds_file->stream);
785 (void) ds_file_munmap(ds_file);
786
787 if (ds_file->file) {
788 ctf_fs_file_destroy(ds_file->file);
789 }
790
791 g_free(ds_file);
792 }
793
794 BT_HIDDEN
795 void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index)
796 {
797 if (!index) {
798 return;
799 }
800
801 if (index->entries) {
802 g_ptr_array_free(index->entries, TRUE);
803 }
804 g_free(index);
805 }
This page took 0.046169 seconds and 4 git commands to generate.