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