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