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