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