src.ctf.fs: make ctf_fs_ds_file_create return a unique_ptr
[babeltrace.git] / src / plugins / ctf / fs-src / data-stream-file.cpp
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2016-2017 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 */
8
9 #include <glib.h>
10 #include <stdint.h>
11 #include <stdio.h>
12
13 #include "compat/endian.h" /* IWYU pragma: keep */
14 #include "compat/mman.h" /* IWYU: pragma keep */
15 #include "cpp-common/bt2s/make-unique.hpp"
16 #include "cpp-common/vendor/fmt/format.h"
17
18 #include "../common/src/msg-iter/msg-iter.hpp"
19 #include "data-stream-file.hpp"
20 #include "file.hpp"
21 #include "fs.hpp"
22 #include "lttng-index.hpp"
23
24 static inline size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file)
25 {
26 BT_ASSERT_DBG(ds_file->mmap_len >= ds_file->request_offset_in_mapping);
27 return ds_file->mmap_len - ds_file->request_offset_in_mapping;
28 }
29
30 /*
31 * Return true if `offset_in_file` is in the current mapping.
32 */
33
34 static bool offset_ist_mapped(struct ctf_fs_ds_file *ds_file, off_t offset_in_file)
35 {
36 return offset_in_file >= ds_file->mmap_offset_in_file &&
37 offset_in_file < (ds_file->mmap_offset_in_file + ds_file->mmap_len);
38 }
39
40 static enum ctf_msg_iter_medium_status ds_file_munmap(struct ctf_fs_ds_file *ds_file)
41 {
42 enum ctf_msg_iter_medium_status status;
43
44 BT_ASSERT(ds_file);
45
46 if (!ds_file->mmap_addr) {
47 status = CTF_MSG_ITER_MEDIUM_STATUS_OK;
48 goto end;
49 }
50
51 if (bt_munmap(ds_file->mmap_addr, ds_file->mmap_len)) {
52 BT_CPPLOGE_ERRNO_SPEC(ds_file->logger, "Cannot memory-unmap file",
53 ": address={}, size={}, file_path=\"{}\", file={}",
54 fmt::ptr(ds_file->mmap_addr), ds_file->mmap_len,
55 ds_file->file ? ds_file->file->path : "NULL",
56 ds_file->file ? fmt::ptr(ds_file->file->fp) : NULL);
57 status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
58 goto end;
59 }
60
61 ds_file->mmap_addr = NULL;
62
63 status = CTF_MSG_ITER_MEDIUM_STATUS_OK;
64 end:
65 return status;
66 }
67
68 /*
69 * mmap a region of `ds_file` such that `requested_offset_in_file` is in the
70 * mapping. If the currently mmap-ed region already contains
71 * `requested_offset_in_file`, the mapping is kept.
72 *
73 * Set `ds_file->requested_offset_in_mapping` based on `request_offset_in_file`,
74 * such that the next call to `request_bytes` will return bytes starting at that
75 * position.
76 *
77 * `requested_offset_in_file` must be a valid offset in the file.
78 */
79 static enum ctf_msg_iter_medium_status ds_file_mmap(struct ctf_fs_ds_file *ds_file,
80 off_t requested_offset_in_file)
81 {
82 enum ctf_msg_iter_medium_status status;
83
84 /* Ensure the requested offset is in the file range. */
85 BT_ASSERT(requested_offset_in_file >= 0);
86 BT_ASSERT(requested_offset_in_file < ds_file->file->size);
87
88 /*
89 * If the mapping already contains the requested offset, just adjust
90 * requested_offset_in_mapping.
91 */
92 if (offset_ist_mapped(ds_file, requested_offset_in_file)) {
93 ds_file->request_offset_in_mapping =
94 requested_offset_in_file - ds_file->mmap_offset_in_file;
95 status = CTF_MSG_ITER_MEDIUM_STATUS_OK;
96 goto end;
97 }
98
99 /* Unmap old region */
100 status = ds_file_munmap(ds_file);
101 if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) {
102 goto end;
103 }
104
105 /*
106 * Compute a mapping that has the required alignment properties and
107 * contains `requested_offset_in_file`.
108 */
109 ds_file->request_offset_in_mapping =
110 requested_offset_in_file %
111 bt_mmap_get_offset_align_size(static_cast<int>(ds_file->logger.level()));
112 ds_file->mmap_offset_in_file = requested_offset_in_file - ds_file->request_offset_in_mapping;
113 ds_file->mmap_len =
114 MIN(ds_file->file->size - ds_file->mmap_offset_in_file, ds_file->mmap_max_len);
115
116 BT_ASSERT(ds_file->mmap_len > 0);
117
118 ds_file->mmap_addr =
119 bt_mmap(ds_file->mmap_len, PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp.get()),
120 ds_file->mmap_offset_in_file, static_cast<int>(ds_file->logger.level()));
121 if (ds_file->mmap_addr == MAP_FAILED) {
122 BT_CPPLOGE_SPEC(ds_file->logger,
123 "Cannot memory-map address (size {}) of file \"{}\" ({}) at offset {}: {}",
124 ds_file->mmap_len, ds_file->file->path, fmt::ptr(ds_file->file->fp),
125 (intmax_t) ds_file->mmap_offset_in_file, strerror(errno));
126 status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
127 goto end;
128 }
129
130 status = CTF_MSG_ITER_MEDIUM_STATUS_OK;
131
132 end:
133 return status;
134 }
135
136 /*
137 * Change the mapping of the file to read the region that follows the current
138 * mapping.
139 *
140 * If the file hasn't been mapped yet, then everything (mmap_offset_in_file,
141 * mmap_len, request_offset_in_mapping) should have the value 0, which will
142 * result in the beginning of the file getting mapped.
143 *
144 * return _EOF if the current mapping is the end of the file.
145 */
146
147 static enum ctf_msg_iter_medium_status ds_file_mmap_next(struct ctf_fs_ds_file *ds_file)
148 {
149 enum ctf_msg_iter_medium_status status;
150
151 /*
152 * If we're called, it's because more bytes are requested but we have
153 * given all the bytes of the current mapping.
154 */
155 BT_ASSERT(ds_file->request_offset_in_mapping == ds_file->mmap_len);
156
157 /*
158 * If the current mapping coincides with the end of the file, there is
159 * no next mapping.
160 */
161 if (ds_file->mmap_offset_in_file + ds_file->mmap_len == ds_file->file->size) {
162 status = CTF_MSG_ITER_MEDIUM_STATUS_EOF;
163 goto end;
164 }
165
166 status = ds_file_mmap(ds_file, ds_file->mmap_offset_in_file + ds_file->mmap_len);
167
168 end:
169 return status;
170 }
171
172 static enum ctf_msg_iter_medium_status medop_request_bytes(size_t request_sz, uint8_t **buffer_addr,
173 size_t *buffer_sz, void *data)
174 {
175 enum ctf_msg_iter_medium_status status = CTF_MSG_ITER_MEDIUM_STATUS_OK;
176 struct ctf_fs_ds_file *ds_file = (struct ctf_fs_ds_file *) data;
177
178 BT_ASSERT(request_sz > 0);
179
180 /*
181 * Check if we have at least one memory-mapped byte left. If we don't,
182 * mmap the next file.
183 */
184 if (remaining_mmap_bytes(ds_file) == 0) {
185 /* Are we at the end of the file? */
186 if (ds_file->mmap_offset_in_file >= ds_file->file->size) {
187 BT_CPPLOGD_SPEC(ds_file->logger, "Reached end of file \"{}\" ({})", ds_file->file->path,
188 fmt::ptr(ds_file->file->fp));
189 status = CTF_MSG_ITER_MEDIUM_STATUS_EOF;
190 goto end;
191 }
192
193 status = ds_file_mmap_next(ds_file);
194 switch (status) {
195 case CTF_MSG_ITER_MEDIUM_STATUS_OK:
196 break;
197 case CTF_MSG_ITER_MEDIUM_STATUS_EOF:
198 goto end;
199 default:
200 BT_CPPLOGE_SPEC(ds_file->logger, "Cannot memory-map next region of file \"{}\" ({})",
201 ds_file->file->path, fmt::ptr(ds_file->file->fp));
202 goto error;
203 }
204 }
205
206 BT_ASSERT(remaining_mmap_bytes(ds_file) > 0);
207 *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz);
208
209 BT_ASSERT(ds_file->mmap_addr);
210 *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset_in_mapping;
211
212 ds_file->request_offset_in_mapping += *buffer_sz;
213 goto end;
214
215 error:
216 status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
217
218 end:
219 return status;
220 }
221
222 static bt_stream *medop_borrow_stream(bt_stream_class *stream_class, int64_t, void *data)
223 {
224 struct ctf_fs_ds_file *ds_file = (struct ctf_fs_ds_file *) data;
225 bt_stream_class *ds_file_stream_class;
226 bt_stream *stream = NULL;
227
228 ds_file_stream_class = ds_file->stream->cls().libObjPtr();
229
230 if (stream_class != ds_file_stream_class) {
231 /*
232 * Not supported: two packets described by two different
233 * stream classes within the same data stream file.
234 */
235 goto end;
236 }
237
238 stream = ds_file->stream->libObjPtr();
239
240 end:
241 return stream;
242 }
243
244 static enum ctf_msg_iter_medium_status medop_seek(off_t offset, void *data)
245 {
246 struct ctf_fs_ds_file *ds_file = (struct ctf_fs_ds_file *) data;
247
248 BT_ASSERT(offset >= 0);
249 BT_ASSERT(offset < ds_file->file->size);
250
251 return ds_file_mmap(ds_file, offset);
252 }
253
254 struct ctf_msg_iter_medium_ops ctf_fs_ds_file_medops = {
255 medop_request_bytes,
256 medop_seek,
257 nullptr,
258 medop_borrow_stream,
259 };
260
261 struct ctf_fs_ds_group_medops_data
262 {
263 explicit ctf_fs_ds_group_medops_data(const bt2c::Logger& parentLogger) :
264 logger {parentLogger, "PLUGIN/SRC.CTF.FS/DS-GROUP-MEDOPS"}
265 {
266 }
267
268 bt2c::Logger logger;
269
270 /* Weak, set once at creation time. */
271 struct ctf_fs_ds_file_group *ds_file_group = nullptr;
272
273 /*
274 * Index (as in element rank) of the index entry of ds_file_groups'
275 * index we will read next (so, the one after the one we are reading
276 * right now).
277 */
278 guint next_index_entry_index = 0;
279
280 /*
281 * File we are currently reading. Changes whenever we switch to
282 * reading another data file.
283 *
284 * Owned by this.
285 */
286 struct ctf_fs_ds_file *file = nullptr;
287
288 /* Weak, for context / logging / appending causes. */
289 bt_self_message_iterator *self_msg_iter = nullptr;
290 };
291
292 static enum ctf_msg_iter_medium_status medop_group_request_bytes(size_t request_sz,
293 uint8_t **buffer_addr,
294 size_t *buffer_sz, void *void_data)
295 {
296 struct ctf_fs_ds_group_medops_data *data = (struct ctf_fs_ds_group_medops_data *) void_data;
297
298 /* Return bytes from the current file. */
299 return medop_request_bytes(request_sz, buffer_addr, buffer_sz, data->file);
300 }
301
302 static bt_stream *medop_group_borrow_stream(bt_stream_class *stream_class, int64_t stream_id,
303 void *void_data)
304 {
305 struct ctf_fs_ds_group_medops_data *data = (struct ctf_fs_ds_group_medops_data *) void_data;
306
307 return medop_borrow_stream(stream_class, stream_id, data->file);
308 }
309
310 /*
311 * Set `data->file` to prepare it to read the packet described
312 * by `index_entry`.
313 */
314
315 static enum ctf_msg_iter_medium_status
316 ctf_fs_ds_group_medops_set_file(struct ctf_fs_ds_group_medops_data *data,
317 struct ctf_fs_ds_index_entry *index_entry)
318 {
319 enum ctf_msg_iter_medium_status status;
320
321 BT_ASSERT(data);
322 BT_ASSERT(index_entry);
323
324 /* Check if that file is already the one mapped. */
325 if (!data->file || data->file->file->path != index_entry->path) {
326 /* Destroy the previously used file. */
327 delete data->file;
328
329 /* Create the new file. */
330 data->file =
331 ctf_fs_ds_file_create(data->ds_file_group->ctf_fs_trace, data->ds_file_group->stream,
332 index_entry->path, data->logger)
333 .release();
334 if (!data->file) {
335 BT_CPPLOGE_APPEND_CAUSE_SPEC(data->logger, "failed to create ctf_fs_ds_file.");
336 status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
337 goto end;
338 }
339 }
340
341 /*
342 * Ensure the right portion of the file will be returned on the next
343 * request_bytes call.
344 */
345 status = ds_file_mmap(data->file, index_entry->offset.bytes());
346 if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) {
347 goto end;
348 }
349
350 status = CTF_MSG_ITER_MEDIUM_STATUS_OK;
351
352 end:
353 return status;
354 }
355
356 static enum ctf_msg_iter_medium_status medop_group_switch_packet(void *void_data)
357 {
358 struct ctf_fs_ds_group_medops_data *data = (struct ctf_fs_ds_group_medops_data *) void_data;
359 struct ctf_fs_ds_index_entry *index_entry;
360 enum ctf_msg_iter_medium_status status;
361
362 /* If we have gone through all index entries, we are done. */
363 if (data->next_index_entry_index >= data->ds_file_group->index->entries.size()) {
364 status = CTF_MSG_ITER_MEDIUM_STATUS_EOF;
365 goto end;
366 }
367
368 /*
369 * Otherwise, look up the next index entry / packet and prepare it
370 * for reading.
371 */
372 index_entry = data->ds_file_group->index->entries[data->next_index_entry_index].get();
373
374 status = ctf_fs_ds_group_medops_set_file(data, index_entry);
375 if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) {
376 goto end;
377 }
378
379 data->next_index_entry_index++;
380
381 status = CTF_MSG_ITER_MEDIUM_STATUS_OK;
382 end:
383 return status;
384 }
385
386 void ctf_fs_ds_group_medops_data_destroy(struct ctf_fs_ds_group_medops_data *data)
387 {
388 if (!data) {
389 goto end;
390 }
391
392 delete data->file;
393
394 delete data;
395
396 end:
397 return;
398 }
399
400 enum ctf_msg_iter_medium_status ctf_fs_ds_group_medops_data_create(
401 struct ctf_fs_ds_file_group *ds_file_group, bt_self_message_iterator *self_msg_iter,
402 const bt2c::Logger& parentLogger, struct ctf_fs_ds_group_medops_data **out)
403 {
404 BT_ASSERT(self_msg_iter);
405 BT_ASSERT(ds_file_group);
406 BT_ASSERT(ds_file_group->index);
407 BT_ASSERT(!ds_file_group->index->entries.empty());
408
409 ctf_fs_ds_group_medops_data *data = new ctf_fs_ds_group_medops_data {parentLogger};
410 data->ds_file_group = ds_file_group;
411 data->self_msg_iter = self_msg_iter;
412
413 /*
414 * No need to prepare the first file. ctf_msg_iter will call
415 * switch_packet before reading the first packet, it will be
416 * done then.
417 */
418
419 *out = data;
420 return CTF_MSG_ITER_MEDIUM_STATUS_OK;
421 }
422
423 void ctf_fs_ds_group_medops_data_reset(struct ctf_fs_ds_group_medops_data *data)
424 {
425 data->next_index_entry_index = 0;
426 }
427
428 struct ctf_msg_iter_medium_ops ctf_fs_ds_group_medops = {
429 .request_bytes = medop_group_request_bytes,
430
431 /*
432 * We don't support seeking using this medops. It would probably be
433 * possible, but it's not needed at the moment.
434 */
435 .seek = NULL,
436
437 .switch_packet = medop_group_switch_packet,
438 .borrow_stream = medop_group_borrow_stream,
439 };
440
441 static ctf_fs_ds_index_entry::UP ctf_fs_ds_index_entry_create(const bt2c::DataLen offset,
442 const bt2c::DataLen packetSize)
443 {
444 ctf_fs_ds_index_entry::UP entry = bt2s::make_unique<ctf_fs_ds_index_entry>(offset, packetSize);
445
446 entry->packet_seq_num = UINT64_MAX;
447
448 return entry;
449 }
450
451 static int convert_cycles_to_ns(struct ctf_clock_class *clock_class, uint64_t cycles, int64_t *ns)
452 {
453 return bt_util_clock_cycles_to_ns_from_origin(cycles, clock_class->frequency,
454 clock_class->offset_seconds,
455 clock_class->offset_cycles, ns);
456 }
457
458 static ctf_fs_ds_index::UP build_index_from_idx_file(struct ctf_fs_ds_file *ds_file,
459 struct ctf_fs_ds_file_info *file_info,
460 struct ctf_msg_iter *msg_iter)
461 {
462 int ret;
463 gchar *directory = NULL;
464 gchar *basename = NULL;
465 GString *index_basename = NULL;
466 gchar *index_file_path = NULL;
467 GMappedFile *mapped_file = NULL;
468 gsize filesize;
469 const char *mmap_begin = NULL, *file_pos = NULL;
470 const struct ctf_packet_index_file_hdr *header = NULL;
471 ctf_fs_ds_index::UP index;
472 ctf_fs_ds_index_entry::UP index_entry;
473 ctf_fs_ds_index_entry *prev_index_entry = NULL;
474 auto totalPacketsSize = bt2c::DataLen::fromBytes(0);
475 size_t file_index_entry_size;
476 size_t file_entry_count;
477 size_t i;
478 struct ctf_stream_class *sc;
479 struct ctf_msg_iter_packet_properties props;
480 uint32_t version_major, version_minor;
481
482 BT_CPPLOGI_SPEC(ds_file->logger, "Building index from .idx file of stream file {}",
483 ds_file->file->path);
484 ret = ctf_msg_iter_get_packet_properties(msg_iter, &props);
485 if (ret) {
486 BT_CPPLOGI_STR_SPEC(ds_file->logger,
487 "Cannot read first packet's header and context fields.");
488 goto error;
489 }
490
491 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, props.stream_class_id);
492 BT_ASSERT(sc);
493 if (!sc->default_clock_class) {
494 BT_CPPLOGI_STR_SPEC(ds_file->logger, "Cannot find stream class's default clock class.");
495 goto error;
496 }
497
498 /* Look for index file in relative path index/name.idx. */
499 basename = g_path_get_basename(ds_file->file->path.c_str());
500 if (!basename) {
501 BT_CPPLOGE_SPEC(ds_file->logger, "Cannot get the basename of datastream file {}",
502 ds_file->file->path);
503 goto error;
504 }
505
506 directory = g_path_get_dirname(ds_file->file->path.c_str());
507 if (!directory) {
508 BT_CPPLOGE_SPEC(ds_file->logger, "Cannot get dirname of datastream file {}",
509 ds_file->file->path);
510 goto error;
511 }
512
513 index_basename = g_string_new(basename);
514 if (!index_basename) {
515 BT_CPPLOGE_STR_SPEC(ds_file->logger, "Cannot allocate index file basename string");
516 goto error;
517 }
518
519 g_string_append(index_basename, ".idx");
520 index_file_path = g_build_filename(directory, "index", index_basename->str, NULL);
521 mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL);
522 if (!mapped_file) {
523 BT_CPPLOGD_SPEC(ds_file->logger, "Cannot create new mapped file {}", index_file_path);
524 goto error;
525 }
526
527 /*
528 * The g_mapped_file API limits us to 4GB files on 32-bit.
529 * Traces with such large indexes have never been seen in the wild,
530 * but this would need to be adjusted to support them.
531 */
532 filesize = g_mapped_file_get_length(mapped_file);
533 if (filesize < sizeof(*header)) {
534 BT_CPPLOGW_SPEC(ds_file->logger,
535 "Invalid LTTng trace index file: "
536 "file size ({} bytes) < header size ({} bytes)",
537 filesize, sizeof(*header));
538 goto error;
539 }
540
541 mmap_begin = g_mapped_file_get_contents(mapped_file);
542 header = (struct ctf_packet_index_file_hdr *) mmap_begin;
543
544 file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header);
545 if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
546 BT_CPPLOGW_STR_SPEC(ds_file->logger,
547 "Invalid LTTng trace index: \"magic\" field validation failed");
548 goto error;
549 }
550
551 version_major = be32toh(header->index_major);
552 version_minor = be32toh(header->index_minor);
553 if (version_major != 1) {
554 BT_CPPLOGW_SPEC(ds_file->logger, "Unknown LTTng trace index version: major={}, minor={}",
555 version_major, version_minor);
556 goto error;
557 }
558
559 file_index_entry_size = be32toh(header->packet_index_len);
560 if (file_index_entry_size < CTF_INDEX_1_0_SIZE) {
561 BT_CPPLOGW_SPEC(
562 ds_file->logger,
563 "Invalid `packet_index_len` in LTTng trace index file (`packet_index_len` < CTF index 1.0 index entry size): "
564 "packet_index_len={}, CTF_INDEX_1_0_SIZE={}",
565 file_index_entry_size, CTF_INDEX_1_0_SIZE);
566 goto error;
567 }
568
569 file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
570 if ((filesize - sizeof(*header)) % file_index_entry_size) {
571 BT_CPPLOGW_SPEC(ds_file->logger,
572 "Invalid LTTng trace index: the index's size after the header "
573 "({} bytes) is not a multiple of the index entry size "
574 "({} bytes)",
575 (filesize - sizeof(*header)), sizeof(*header));
576 goto error;
577 }
578
579 index = ctf_fs_ds_index_create();
580 if (!index) {
581 goto error;
582 }
583
584 for (i = 0; i < file_entry_count; i++) {
585 struct ctf_packet_index *file_index = (struct ctf_packet_index *) file_pos;
586 const auto packetSize = bt2c::DataLen::fromBits(be64toh(file_index->packet_size));
587
588 if (packetSize.hasExtraBits()) {
589 BT_CPPLOGW_SPEC(ds_file->logger,
590 "Invalid packet size encountered in LTTng trace index file");
591 goto error;
592 }
593
594 const auto offset = bt2c::DataLen::fromBytes(be64toh(file_index->offset));
595
596 if (i != 0 && offset < prev_index_entry->offset) {
597 BT_CPPLOGW_SPEC(
598 ds_file->logger,
599 "Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
600 "previous offset={} bytes, current offset={} bytes",
601 prev_index_entry->offset.bytes(), offset.bytes());
602 goto error;
603 }
604
605 index_entry = ctf_fs_ds_index_entry_create(offset, packetSize);
606 if (!index_entry) {
607 BT_CPPLOGE_APPEND_CAUSE_SPEC(ds_file->logger,
608 "Failed to create a ctf_fs_ds_index_entry.");
609 goto error;
610 }
611
612 /* Set path to stream file. */
613 index_entry->path = file_info->path.c_str();
614
615 index_entry->timestamp_begin = be64toh(file_index->timestamp_begin);
616 index_entry->timestamp_end = be64toh(file_index->timestamp_end);
617 if (index_entry->timestamp_end < index_entry->timestamp_begin) {
618 BT_CPPLOGW_SPEC(
619 ds_file->logger,
620 "Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
621 "timestamp_begin={}, timestamp_end={}",
622 index_entry->timestamp_begin, index_entry->timestamp_end);
623 goto error;
624 }
625
626 /* Convert the packet's bound to nanoseconds since Epoch. */
627 ret = convert_cycles_to_ns(sc->default_clock_class, index_entry->timestamp_begin,
628 &index_entry->timestamp_begin_ns);
629 if (ret) {
630 BT_CPPLOGI_STR_SPEC(
631 ds_file->logger,
632 "Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
633 goto error;
634 }
635 ret = convert_cycles_to_ns(sc->default_clock_class, index_entry->timestamp_end,
636 &index_entry->timestamp_end_ns);
637 if (ret) {
638 BT_CPPLOGI_STR_SPEC(
639 ds_file->logger,
640 "Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
641 goto error;
642 }
643
644 if (version_minor >= 1) {
645 index_entry->packet_seq_num = be64toh(file_index->packet_seq_num);
646 }
647
648 totalPacketsSize += packetSize;
649 file_pos += file_index_entry_size;
650
651 prev_index_entry = index_entry.get();
652
653 index->entries.emplace_back(std::move(index_entry));
654 }
655
656 /* Validate that the index addresses the complete stream. */
657 if (ds_file->file->size != totalPacketsSize.bytes()) {
658 BT_CPPLOGW_SPEC(ds_file->logger,
659 "Invalid LTTng trace index file; indexed size != stream file size: "
660 "file-size={} bytes, total-packets-size={} bytes",
661 ds_file->file->size, totalPacketsSize.bytes());
662 goto error;
663 }
664 end:
665 g_free(directory);
666 g_free(basename);
667 g_free(index_file_path);
668 if (index_basename) {
669 g_string_free(index_basename, TRUE);
670 }
671 if (mapped_file) {
672 g_mapped_file_unref(mapped_file);
673 }
674 return index;
675 error:
676 index.reset();
677 goto end;
678 }
679
680 static int init_index_entry(struct ctf_fs_ds_index_entry *entry, struct ctf_fs_ds_file *ds_file,
681 struct ctf_msg_iter_packet_properties *props)
682 {
683 int ret = 0;
684 struct ctf_stream_class *sc;
685
686 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, props->stream_class_id);
687 BT_ASSERT(sc);
688
689 if (props->snapshots.beginning_clock != UINT64_C(-1)) {
690 entry->timestamp_begin = props->snapshots.beginning_clock;
691
692 /* Convert the packet's bound to nanoseconds since Epoch. */
693 ret = convert_cycles_to_ns(sc->default_clock_class, props->snapshots.beginning_clock,
694 &entry->timestamp_begin_ns);
695 if (ret) {
696 BT_CPPLOGI_STR_SPEC(ds_file->logger,
697 "Failed to convert raw timestamp to nanoseconds since Epoch.");
698 goto end;
699 }
700 } else {
701 entry->timestamp_begin = UINT64_C(-1);
702 entry->timestamp_begin_ns = UINT64_C(-1);
703 }
704
705 if (props->snapshots.end_clock != UINT64_C(-1)) {
706 entry->timestamp_end = props->snapshots.end_clock;
707
708 /* Convert the packet's bound to nanoseconds since Epoch. */
709 ret = convert_cycles_to_ns(sc->default_clock_class, props->snapshots.end_clock,
710 &entry->timestamp_end_ns);
711 if (ret) {
712 BT_CPPLOGI_STR_SPEC(ds_file->logger,
713 "Failed to convert raw timestamp to nanoseconds since Epoch.");
714 goto end;
715 }
716 } else {
717 entry->timestamp_end = UINT64_C(-1);
718 entry->timestamp_end_ns = UINT64_C(-1);
719 }
720
721 end:
722 return ret;
723 }
724
725 static ctf_fs_ds_index::UP build_index_from_stream_file(struct ctf_fs_ds_file *ds_file,
726 struct ctf_fs_ds_file_info *file_info,
727 struct ctf_msg_iter *msg_iter)
728 {
729 int ret;
730 ctf_fs_ds_index::UP index;
731 enum ctf_msg_iter_status iter_status = CTF_MSG_ITER_STATUS_OK;
732 auto currentPacketOffset = bt2c::DataLen::fromBytes(0);
733
734 BT_CPPLOGI_SPEC(ds_file->logger, "Indexing stream file {}", ds_file->file->path);
735
736 index = ctf_fs_ds_index_create();
737 if (!index) {
738 goto error;
739 }
740
741 while (true) {
742 struct ctf_msg_iter_packet_properties props;
743
744 if (currentPacketOffset.bytes() > ds_file->file->size) {
745 BT_CPPLOGE_STR_SPEC(ds_file->logger,
746 "Unexpected current packet's offset (larger than file).");
747 goto error;
748 } else if (currentPacketOffset.bytes() == ds_file->file->size) {
749 /* No more data */
750 break;
751 }
752
753 iter_status = ctf_msg_iter_seek(msg_iter, currentPacketOffset.bytes());
754 if (iter_status != CTF_MSG_ITER_STATUS_OK) {
755 goto error;
756 }
757
758 iter_status = ctf_msg_iter_get_packet_properties(msg_iter, &props);
759 if (iter_status != CTF_MSG_ITER_STATUS_OK) {
760 goto error;
761 }
762
763 /*
764 * Get the current packet size from the packet header, if set. Else,
765 * assume there is a single packet in the file, so take the file size
766 * as the packet size.
767 */
768 const auto currentPacketSize = props.exp_packet_total_size >= 0 ?
769 bt2c::DataLen::fromBits(props.exp_packet_total_size) :
770 bt2c::DataLen::fromBytes(ds_file->file->size);
771
772 if ((currentPacketOffset + currentPacketSize).bytes() > ds_file->file->size) {
773 BT_CPPLOGW_SPEC(ds_file->logger,
774 "Invalid packet size reported in file: stream=\"{}\", "
775 "packet-offset-bytes={}, packet-size-bytes={}, "
776 "file-size-bytes={}",
777 ds_file->file->path, currentPacketOffset.bytes(),
778 currentPacketSize.bytes(), ds_file->file->size);
779 goto error;
780 }
781
782 auto index_entry = ctf_fs_ds_index_entry_create(currentPacketOffset, currentPacketSize);
783 if (!index_entry) {
784 BT_CPPLOGE_APPEND_CAUSE_SPEC(ds_file->logger,
785 "Failed to create a ctf_fs_ds_index_entry.");
786 goto error;
787 }
788
789 /* Set path to stream file. */
790 index_entry->path = file_info->path.c_str();
791
792 ret = init_index_entry(index_entry.get(), ds_file, &props);
793 if (ret) {
794 goto error;
795 }
796
797 index->entries.emplace_back(std::move(index_entry));
798
799 currentPacketOffset += currentPacketSize;
800 BT_CPPLOGD_SPEC(ds_file->logger,
801 "Seeking to next packet: current-packet-offset-bytes={}, "
802 "next-packet-offset-bytes={}",
803 (currentPacketOffset - currentPacketSize).bytes(),
804 currentPacketOffset.bytes());
805 }
806
807 end:
808 return index;
809
810 error:
811 index.reset();
812 goto end;
813 }
814
815 ctf_fs_ds_file::UP ctf_fs_ds_file_create(struct ctf_fs_trace *ctf_fs_trace,
816 bt2::Stream::Shared stream, const char *path,
817 const bt2c::Logger& parentLogger)
818 {
819 int ret;
820 auto ds_file = bt2s::make_unique<ctf_fs_ds_file>(parentLogger);
821 size_t offset_align;
822
823 ds_file->file = ctf_fs_file_create(parentLogger);
824 if (!ds_file->file) {
825 goto error;
826 }
827
828 ds_file->stream = std::move(stream);
829 ds_file->metadata = ctf_fs_trace->metadata;
830 ds_file->file->path = path;
831 ret = ctf_fs_file_open(ds_file->file.get(), "rb");
832 if (ret) {
833 goto error;
834 }
835
836 offset_align = bt_mmap_get_offset_align_size(static_cast<int>(ds_file->logger.level()));
837 ds_file->mmap_max_len = offset_align * 2048;
838
839 goto end;
840
841 error:
842 /* Do not touch "borrowed" file. */
843 ds_file.reset();
844
845 end:
846 return ds_file;
847 }
848
849 ctf_fs_ds_index::UP ctf_fs_ds_file_build_index(struct ctf_fs_ds_file *ds_file,
850 struct ctf_fs_ds_file_info *file_info,
851 struct ctf_msg_iter *msg_iter)
852 {
853 auto index = build_index_from_idx_file(ds_file, file_info, msg_iter);
854 if (index) {
855 goto end;
856 }
857
858 BT_CPPLOGI_SPEC(ds_file->logger, "Failed to build index from .index file; "
859 "falling back to stream indexing.");
860 index = build_index_from_stream_file(ds_file, file_info, msg_iter);
861 end:
862 return index;
863 }
864
865 ctf_fs_ds_index::UP ctf_fs_ds_index_create()
866 {
867 return bt2s::make_unique<ctf_fs_ds_index>();
868 }
869
870 ctf_fs_ds_file::~ctf_fs_ds_file()
871 {
872 (void) ds_file_munmap(this);
873 }
874
875 ctf_fs_ds_file_info::UP ctf_fs_ds_file_info_create(const char *path, int64_t begin_ns)
876 {
877 ctf_fs_ds_file_info::UP ds_file_info = bt2s::make_unique<ctf_fs_ds_file_info>();
878
879 ds_file_info->path = path;
880 ds_file_info->begin_ns = begin_ns;
881 return ds_file_info;
882 }
883
884 ctf_fs_ds_file_group::UP ctf_fs_ds_file_group_create(struct ctf_fs_trace *ctf_fs_trace,
885 struct ctf_stream_class *sc,
886 uint64_t stream_instance_id,
887 ctf_fs_ds_index::UP index)
888 {
889 ctf_fs_ds_file_group::UP ds_file_group {new ctf_fs_ds_file_group};
890
891 ds_file_group->index = std::move(index);
892
893 ds_file_group->stream_id = stream_instance_id;
894 BT_ASSERT(sc);
895 ds_file_group->sc = sc;
896 ds_file_group->ctf_fs_trace = ctf_fs_trace;
897
898 return ds_file_group;
899 }
This page took 0.061086 seconds and 4 git commands to generate.