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