src.ctf.fs: make ctf_fs_ds_index_entry_create return a unique_ptr
[babeltrace.git] / src / plugins / ctf / fs-src / data-stream-file.cpp
CommitLineData
e98a2d6e 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
e98a2d6e 3 *
0235b0db
MJ
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
e98a2d6e
PP
7 */
8
c802cacb 9#include <glib.h>
c802cacb
SM
10#include <stdint.h>
11#include <stdio.h>
c802cacb 12
83ad336c 13#include "compat/endian.h" /* IWYU pragma: keep */
0f5c5d5c 14#include "compat/mman.h" /* IWYU: pragma keep */
2cef6403 15#include "cpp-common/bt2s/make-unique.hpp"
0f5c5d5c 16#include "cpp-common/vendor/fmt/format.h"
c802cacb 17
5656cea5 18#include "../common/src/msg-iter/msg-iter.hpp"
087cd0f5 19#include "data-stream-file.hpp"
c802cacb 20#include "file.hpp"
c7e1be4b
SM
21#include "fs.hpp"
22#include "lttng-index.hpp"
e98a2d6e 23
4164020e 24static inline size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file)
e98a2d6e 25{
4164020e
SM
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;
e98a2d6e
PP
28}
29
127e2341
SM
30/*
31 * Return true if `offset_in_file` is in the current mapping.
32 */
33
4164020e 34static bool offset_ist_mapped(struct ctf_fs_ds_file *ds_file, off_t offset_in_file)
127e2341 35{
4164020e
SM
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);
127e2341
SM
38}
39
4164020e 40static enum ctf_msg_iter_medium_status ds_file_munmap(struct ctf_fs_ds_file *ds_file)
e98a2d6e 41{
4164020e 42 enum ctf_msg_iter_medium_status status;
4164020e
SM
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)) {
0f5c5d5c
SM
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->str : "NULL",
56 ds_file->file ? fmt::ptr(ds_file->file->fp) : NULL);
4164020e
SM
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;
fc9a526c 64end:
4164020e 65 return status;
e98a2d6e
PP
66}
67
127e2341
SM
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 *
f6e68e70
SM
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.
127e2341
SM
76 *
77 * `requested_offset_in_file` must be a valid offset in the file.
78 */
4164020e
SM
79static enum ctf_msg_iter_medium_status ds_file_mmap(struct ctf_fs_ds_file *ds_file,
80 off_t requested_offset_in_file)
e98a2d6e 81{
4164020e 82 enum ctf_msg_iter_medium_status status;
4164020e
SM
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 =
0f5c5d5c
SM
110 requested_offset_in_file %
111 bt_mmap_get_offset_align_size(static_cast<int>(ds_file->logger.level()));
4164020e
SM
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 =
31cc85a3 119 bt_mmap(ds_file->mmap_len, PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp),
0f5c5d5c 120 ds_file->mmap_offset_in_file, static_cast<int>(ds_file->logger.level()));
4164020e 121 if (ds_file->mmap_addr == MAP_FAILED) {
0f5c5d5c
SM
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->str, fmt::ptr(ds_file->file->fp),
125 (intmax_t) ds_file->mmap_offset_in_file, strerror(errno));
4164020e
SM
126 status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
127 goto end;
128 }
129
130 status = CTF_MSG_ITER_MEDIUM_STATUS_OK;
127e2341
SM
131
132end:
4164020e 133 return status;
127e2341
SM
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
4164020e 147static enum ctf_msg_iter_medium_status ds_file_mmap_next(struct ctf_fs_ds_file *ds_file)
127e2341 148{
4164020e
SM
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);
127e2341 167
e98a2d6e 168end:
4164020e 169 return status;
e98a2d6e
PP
170}
171
4164020e
SM
172static enum ctf_msg_iter_medium_status medop_request_bytes(size_t request_sz, uint8_t **buffer_addr,
173 size_t *buffer_sz, void *data)
e98a2d6e 174{
4164020e
SM
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;
4164020e
SM
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) {
0f5c5d5c
SM
187 BT_CPPLOGD_SPEC(ds_file->logger, "Reached end of file \"{}\" ({})",
188 ds_file->file->path->str, fmt::ptr(ds_file->file->fp));
4164020e
SM
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:
0f5c5d5c
SM
200 BT_CPPLOGE_SPEC(ds_file->logger, "Cannot memory-map next region of file \"{}\" ({})",
201 ds_file->file->path->str, fmt::ptr(ds_file->file->fp));
4164020e
SM
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;
e98a2d6e
PP
214
215error:
4164020e 216 status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
e98a2d6e
PP
217
218end:
4164020e 219 return status;
e98a2d6e
PP
220}
221
ecd7492f 222static bt_stream *medop_borrow_stream(bt_stream_class *stream_class, int64_t, void *data)
e98a2d6e 223{
4164020e
SM
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;
e5be10ef 227
4164020e 228 ds_file_stream_class = bt_stream_borrow_class(ds_file->stream);
94cf822e 229
4164020e
SM
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 }
e98a2d6e 237
4164020e 238 stream = ds_file->stream;
94cf822e
PP
239
240end:
4164020e 241 return stream;
e98a2d6e
PP
242}
243
4164020e 244static enum ctf_msg_iter_medium_status medop_seek(off_t offset, void *data)
9e0c8dbb 245{
4164020e 246 struct ctf_fs_ds_file *ds_file = (struct ctf_fs_ds_file *) data;
9e0c8dbb 247
4164020e
SM
248 BT_ASSERT(offset >= 0);
249 BT_ASSERT(offset < ds_file->file->size);
9e0c8dbb 250
4164020e 251 return ds_file_mmap(ds_file, offset);
9e0c8dbb
JG
252}
253
18a1979b 254struct ctf_msg_iter_medium_ops ctf_fs_ds_file_medops = {
4164020e
SM
255 medop_request_bytes,
256 medop_seek,
257 nullptr,
258 medop_borrow_stream,
e98a2d6e 259};
6de92955 260
4164020e
SM
261struct ctf_fs_ds_group_medops_data
262{
0f5c5d5c
SM
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
4164020e 270 /* Weak, set once at creation time. */
afb0f12b 271 struct ctf_fs_ds_file_group *ds_file_group = nullptr;
4164020e
SM
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 */
afb0f12b 278 guint next_index_entry_index = 0;
4164020e
SM
279
280 /*
281 * File we are currently reading. Changes whenever we switch to
282 * reading another data file.
283 *
284 * Owned by this.
285 */
afb0f12b 286 struct ctf_fs_ds_file *file = nullptr;
4164020e
SM
287
288 /* Weak, for context / logging / appending causes. */
afb0f12b 289 bt_self_message_iterator *self_msg_iter = nullptr;
f6e68e70
SM
290};
291
4164020e
SM
292static 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)
f6e68e70 295{
4164020e 296 struct ctf_fs_ds_group_medops_data *data = (struct ctf_fs_ds_group_medops_data *) void_data;
f6e68e70 297
4164020e
SM
298 /* Return bytes from the current file. */
299 return medop_request_bytes(request_sz, buffer_addr, buffer_sz, data->file);
f6e68e70
SM
300}
301
4164020e
SM
302static bt_stream *medop_group_borrow_stream(bt_stream_class *stream_class, int64_t stream_id,
303 void *void_data)
f6e68e70 304{
4164020e 305 struct ctf_fs_ds_group_medops_data *data = (struct ctf_fs_ds_group_medops_data *) void_data;
f6e68e70 306
4164020e 307 return medop_borrow_stream(stream_class, stream_id, data->file);
f6e68e70
SM
308}
309
310/*
311 * Set `data->file` to prepare it to read the packet described
312 * by `index_entry`.
313 */
314
4164020e
SM
315static enum ctf_msg_iter_medium_status
316ctf_fs_ds_group_medops_set_file(struct ctf_fs_ds_group_medops_data *data,
0f5c5d5c 317 struct ctf_fs_ds_index_entry *index_entry)
f6e68e70 318{
4164020e
SM
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 || strcmp(index_entry->path, data->file->file->path->str) != 0) {
326 /* Destroy the previously used file. */
327 ctf_fs_ds_file_destroy(data->file);
328
329 /* Create the new file. */
330 data->file =
76edbcf1 331 ctf_fs_ds_file_create(data->ds_file_group->ctf_fs_trace, data->ds_file_group->stream,
0f5c5d5c 332 index_entry->path, data->logger);
4164020e 333 if (!data->file) {
0f5c5d5c 334 BT_CPPLOGE_APPEND_CAUSE_SPEC(data->logger, "failed to create ctf_fs_ds_file.");
4164020e
SM
335 status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
336 goto end;
337 }
338 }
339
340 /*
341 * Ensure the right portion of the file will be returned on the next
342 * request_bytes call.
343 */
ef7d7ac2 344 status = ds_file_mmap(data->file, index_entry->offset.bytes());
4164020e
SM
345 if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) {
346 goto end;
347 }
348
349 status = CTF_MSG_ITER_MEDIUM_STATUS_OK;
f6e68e70
SM
350
351end:
4164020e 352 return status;
f6e68e70
SM
353}
354
4164020e 355static enum ctf_msg_iter_medium_status medop_group_switch_packet(void *void_data)
f6e68e70 356{
4164020e
SM
357 struct ctf_fs_ds_group_medops_data *data = (struct ctf_fs_ds_group_medops_data *) void_data;
358 struct ctf_fs_ds_index_entry *index_entry;
359 enum ctf_msg_iter_medium_status status;
360
361 /* If we have gone through all index entries, we are done. */
362 if (data->next_index_entry_index >= data->ds_file_group->index->entries->len) {
363 status = CTF_MSG_ITER_MEDIUM_STATUS_EOF;
364 goto end;
365 }
366
367 /*
368 * Otherwise, look up the next index entry / packet and prepare it
369 * for reading.
370 */
371 index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index(
372 data->ds_file_group->index->entries, data->next_index_entry_index);
373
0f5c5d5c 374 status = ctf_fs_ds_group_medops_set_file(data, index_entry);
4164020e
SM
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;
f6e68e70 382end:
4164020e 383 return status;
f6e68e70
SM
384}
385
4164020e 386void ctf_fs_ds_group_medops_data_destroy(struct ctf_fs_ds_group_medops_data *data)
f6e68e70 387{
4164020e
SM
388 if (!data) {
389 goto end;
390 }
f6e68e70 391
4164020e 392 ctf_fs_ds_file_destroy(data->file);
f6e68e70 393
afb0f12b 394 delete data;
f6e68e70
SM
395
396end:
4164020e 397 return;
f6e68e70
SM
398}
399
400enum ctf_msg_iter_medium_status ctf_fs_ds_group_medops_data_create(
4164020e 401 struct ctf_fs_ds_file_group *ds_file_group, bt_self_message_iterator *self_msg_iter,
0f5c5d5c 402 const bt2c::Logger& parentLogger, struct ctf_fs_ds_group_medops_data **out)
f6e68e70 403{
4164020e
SM
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->len > 0);
408
0f5c5d5c 409 ctf_fs_ds_group_medops_data *data = new ctf_fs_ds_group_medops_data {parentLogger};
4164020e
SM
410 data->ds_file_group = ds_file_group;
411 data->self_msg_iter = self_msg_iter;
4164020e
SM
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;
afb0f12b 420 return CTF_MSG_ITER_MEDIUM_STATUS_OK;
f6e68e70
SM
421}
422
423void ctf_fs_ds_group_medops_data_reset(struct ctf_fs_ds_group_medops_data *data)
424{
4164020e 425 data->next_index_entry_index = 0;
f6e68e70
SM
426}
427
428struct ctf_msg_iter_medium_ops ctf_fs_ds_group_medops = {
4164020e 429 .request_bytes = medop_group_request_bytes,
f6e68e70 430
4164020e
SM
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,
087cd0f5 436
4164020e
SM
437 .switch_packet = medop_group_switch_packet,
438 .borrow_stream = medop_group_borrow_stream,
f6e68e70
SM
439};
440
afb0f12b 441static void ctf_fs_ds_index_entry_destroy(ctf_fs_ds_index_entry *entry)
6834784d 442{
afb0f12b
SM
443 delete entry;
444}
6834784d 445
c05e1405
SM
446static ctf_fs_ds_index_entry::UP ctf_fs_ds_index_entry_create(const bt2c::DataLen offset,
447 const bt2c::DataLen packetSize)
afb0f12b 448{
c05e1405 449 ctf_fs_ds_index_entry::UP entry = bt2s::make_unique<ctf_fs_ds_index_entry>(offset, packetSize);
ef7d7ac2 450
4164020e 451 entry->packet_seq_num = UINT64_MAX;
6834784d 452
4164020e 453 return entry;
6834784d
SM
454}
455
4164020e 456static int convert_cycles_to_ns(struct ctf_clock_class *clock_class, uint64_t cycles, int64_t *ns)
b6c3dcb2 457{
4164020e
SM
458 return bt_util_clock_cycles_to_ns_from_origin(cycles, clock_class->frequency,
459 clock_class->offset_seconds,
460 clock_class->offset_cycles, ns);
97ade20b
JG
461}
462
441fa755
SM
463static ctf_fs_ds_index::UP build_index_from_idx_file(struct ctf_fs_ds_file *ds_file,
464 struct ctf_fs_ds_file_info *file_info,
465 struct ctf_msg_iter *msg_iter)
97ade20b 466{
4164020e
SM
467 int ret;
468 gchar *directory = NULL;
469 gchar *basename = NULL;
470 GString *index_basename = NULL;
471 gchar *index_file_path = NULL;
472 GMappedFile *mapped_file = NULL;
473 gsize filesize;
474 const char *mmap_begin = NULL, *file_pos = NULL;
475 const struct ctf_packet_index_file_hdr *header = NULL;
441fa755 476 ctf_fs_ds_index::UP index;
c05e1405
SM
477 ctf_fs_ds_index_entry::UP index_entry;
478 ctf_fs_ds_index_entry *prev_index_entry = NULL;
ef7d7ac2 479 auto totalPacketsSize = bt2c::DataLen::fromBytes(0);
4164020e
SM
480 size_t file_index_entry_size;
481 size_t file_entry_count;
482 size_t i;
483 struct ctf_stream_class *sc;
484 struct ctf_msg_iter_packet_properties props;
485 uint32_t version_major, version_minor;
4164020e 486
0f5c5d5c
SM
487 BT_CPPLOGI_SPEC(ds_file->logger, "Building index from .idx file of stream file {}",
488 ds_file->file->path->str);
4164020e
SM
489 ret = ctf_msg_iter_get_packet_properties(msg_iter, &props);
490 if (ret) {
0f5c5d5c
SM
491 BT_CPPLOGI_STR_SPEC(ds_file->logger,
492 "Cannot read first packet's header and context fields.");
4164020e
SM
493 goto error;
494 }
495
496 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, props.stream_class_id);
497 BT_ASSERT(sc);
498 if (!sc->default_clock_class) {
0f5c5d5c 499 BT_CPPLOGI_STR_SPEC(ds_file->logger, "Cannot find stream class's default clock class.");
4164020e
SM
500 goto error;
501 }
502
503 /* Look for index file in relative path index/name.idx. */
504 basename = g_path_get_basename(ds_file->file->path->str);
505 if (!basename) {
0f5c5d5c
SM
506 BT_CPPLOGE_SPEC(ds_file->logger, "Cannot get the basename of datastream file {}",
507 ds_file->file->path->str);
4164020e
SM
508 goto error;
509 }
510
511 directory = g_path_get_dirname(ds_file->file->path->str);
512 if (!directory) {
0f5c5d5c
SM
513 BT_CPPLOGE_SPEC(ds_file->logger, "Cannot get dirname of datastream file {}",
514 ds_file->file->path->str);
4164020e
SM
515 goto error;
516 }
517
518 index_basename = g_string_new(basename);
519 if (!index_basename) {
0f5c5d5c 520 BT_CPPLOGE_STR_SPEC(ds_file->logger, "Cannot allocate index file basename string");
4164020e
SM
521 goto error;
522 }
523
524 g_string_append(index_basename, ".idx");
525 index_file_path = g_build_filename(directory, "index", index_basename->str, NULL);
526 mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL);
527 if (!mapped_file) {
0f5c5d5c 528 BT_CPPLOGD_SPEC(ds_file->logger, "Cannot create new mapped file {}", index_file_path);
4164020e
SM
529 goto error;
530 }
531
532 /*
533 * The g_mapped_file API limits us to 4GB files on 32-bit.
534 * Traces with such large indexes have never been seen in the wild,
535 * but this would need to be adjusted to support them.
536 */
537 filesize = g_mapped_file_get_length(mapped_file);
538 if (filesize < sizeof(*header)) {
0f5c5d5c
SM
539 BT_CPPLOGW_SPEC(ds_file->logger,
540 "Invalid LTTng trace index file: "
541 "file size ({} bytes) < header size ({} bytes)",
542 filesize, sizeof(*header));
4164020e
SM
543 goto error;
544 }
545
546 mmap_begin = g_mapped_file_get_contents(mapped_file);
547 header = (struct ctf_packet_index_file_hdr *) mmap_begin;
548
549 file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header);
550 if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
0f5c5d5c
SM
551 BT_CPPLOGW_STR_SPEC(ds_file->logger,
552 "Invalid LTTng trace index: \"magic\" field validation failed");
4164020e
SM
553 goto error;
554 }
555
556 version_major = be32toh(header->index_major);
557 version_minor = be32toh(header->index_minor);
558 if (version_major != 1) {
0f5c5d5c
SM
559 BT_CPPLOGW_SPEC(ds_file->logger, "Unknown LTTng trace index version: major={}, minor={}",
560 version_major, version_minor);
4164020e
SM
561 goto error;
562 }
563
564 file_index_entry_size = be32toh(header->packet_index_len);
565 if (file_index_entry_size < CTF_INDEX_1_0_SIZE) {
0f5c5d5c
SM
566 BT_CPPLOGW_SPEC(
567 ds_file->logger,
4164020e 568 "Invalid `packet_index_len` in LTTng trace index file (`packet_index_len` < CTF index 1.0 index entry size): "
0f5c5d5c 569 "packet_index_len={}, CTF_INDEX_1_0_SIZE={}",
4164020e
SM
570 file_index_entry_size, CTF_INDEX_1_0_SIZE);
571 goto error;
572 }
573
574 file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
575 if ((filesize - sizeof(*header)) % file_index_entry_size) {
0f5c5d5c
SM
576 BT_CPPLOGW_SPEC(ds_file->logger,
577 "Invalid LTTng trace index: the index's size after the header "
578 "({} bytes) is not a multiple of the index entry size "
579 "({} bytes)",
580 (filesize - sizeof(*header)), sizeof(*header));
4164020e
SM
581 goto error;
582 }
583
0f5c5d5c 584 index = ctf_fs_ds_index_create(ds_file->logger);
4164020e
SM
585 if (!index) {
586 goto error;
587 }
588
589 for (i = 0; i < file_entry_count; i++) {
590 struct ctf_packet_index *file_index = (struct ctf_packet_index *) file_pos;
ef7d7ac2 591 const auto packetSize = bt2c::DataLen::fromBits(be64toh(file_index->packet_size));
4164020e 592
ef7d7ac2 593 if (packetSize.hasExtraBits()) {
0f5c5d5c
SM
594 BT_CPPLOGW_SPEC(ds_file->logger,
595 "Invalid packet size encountered in LTTng trace index file");
4164020e
SM
596 goto error;
597 }
598
ef7d7ac2
SM
599 const auto offset = bt2c::DataLen::fromBytes(be64toh(file_index->offset));
600
601 if (i != 0 && offset < prev_index_entry->offset) {
602 BT_CPPLOGW_SPEC(
603 ds_file->logger,
604 "Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
605 "previous offset={} bytes, current offset={} bytes",
606 prev_index_entry->offset.bytes(), offset.bytes());
607 goto error;
608 }
609
610 index_entry = ctf_fs_ds_index_entry_create(offset, packetSize);
4164020e 611 if (!index_entry) {
0f5c5d5c
SM
612 BT_CPPLOGE_APPEND_CAUSE_SPEC(ds_file->logger,
613 "Failed to create a ctf_fs_ds_index_entry.");
4164020e
SM
614 goto error;
615 }
616
617 /* Set path to stream file. */
4d199954 618 index_entry->path = file_info->path.c_str();
4164020e 619
4164020e
SM
620 index_entry->timestamp_begin = be64toh(file_index->timestamp_begin);
621 index_entry->timestamp_end = be64toh(file_index->timestamp_end);
622 if (index_entry->timestamp_end < index_entry->timestamp_begin) {
0f5c5d5c
SM
623 BT_CPPLOGW_SPEC(
624 ds_file->logger,
4164020e 625 "Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
0f5c5d5c 626 "timestamp_begin={}, timestamp_end={}",
4164020e
SM
627 index_entry->timestamp_begin, index_entry->timestamp_end);
628 goto error;
629 }
630
631 /* Convert the packet's bound to nanoseconds since Epoch. */
632 ret = convert_cycles_to_ns(sc->default_clock_class, index_entry->timestamp_begin,
633 &index_entry->timestamp_begin_ns);
634 if (ret) {
0f5c5d5c
SM
635 BT_CPPLOGI_STR_SPEC(
636 ds_file->logger,
4164020e
SM
637 "Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
638 goto error;
639 }
640 ret = convert_cycles_to_ns(sc->default_clock_class, index_entry->timestamp_end,
641 &index_entry->timestamp_end_ns);
642 if (ret) {
0f5c5d5c
SM
643 BT_CPPLOGI_STR_SPEC(
644 ds_file->logger,
4164020e
SM
645 "Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
646 goto error;
647 }
648
649 if (version_minor >= 1) {
650 index_entry->packet_seq_num = be64toh(file_index->packet_seq_num);
651 }
652
ef7d7ac2 653 totalPacketsSize += packetSize;
4164020e
SM
654 file_pos += file_index_entry_size;
655
c05e1405 656 prev_index_entry = index_entry.get();
4164020e
SM
657
658 /* Give ownership of `index_entry` to `index->entries`. */
c05e1405 659 g_ptr_array_add(index->entries, index_entry.release());
4164020e
SM
660 }
661
662 /* Validate that the index addresses the complete stream. */
ef7d7ac2 663 if (ds_file->file->size != totalPacketsSize.bytes()) {
0f5c5d5c
SM
664 BT_CPPLOGW_SPEC(ds_file->logger,
665 "Invalid LTTng trace index file; indexed size != stream file size: "
ef7d7ac2
SM
666 "file-size={} bytes, total-packets-size={} bytes",
667 ds_file->file->size, totalPacketsSize.bytes());
4164020e
SM
668 goto error;
669 }
b6c3dcb2 670end:
4164020e
SM
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;
97ade20b 681error:
441fa755 682 index.reset();
4164020e 683 goto end;
b6c3dcb2
JG
684}
685
4164020e 686static int init_index_entry(struct ctf_fs_ds_index_entry *entry, struct ctf_fs_ds_file *ds_file,
ef7d7ac2 687 struct ctf_msg_iter_packet_properties *props)
9e0c8dbb 688{
4164020e
SM
689 int ret = 0;
690 struct ctf_stream_class *sc;
4164020e
SM
691
692 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, props->stream_class_id);
693 BT_ASSERT(sc);
4164020e
SM
694
695 if (props->snapshots.beginning_clock != UINT64_C(-1)) {
696 entry->timestamp_begin = props->snapshots.beginning_clock;
697
698 /* Convert the packet's bound to nanoseconds since Epoch. */
699 ret = convert_cycles_to_ns(sc->default_clock_class, props->snapshots.beginning_clock,
700 &entry->timestamp_begin_ns);
701 if (ret) {
0f5c5d5c
SM
702 BT_CPPLOGI_STR_SPEC(ds_file->logger,
703 "Failed to convert raw timestamp to nanoseconds since Epoch.");
4164020e
SM
704 goto end;
705 }
706 } else {
707 entry->timestamp_begin = UINT64_C(-1);
708 entry->timestamp_begin_ns = UINT64_C(-1);
709 }
710
711 if (props->snapshots.end_clock != UINT64_C(-1)) {
712 entry->timestamp_end = props->snapshots.end_clock;
713
714 /* Convert the packet's bound to nanoseconds since Epoch. */
715 ret = convert_cycles_to_ns(sc->default_clock_class, props->snapshots.end_clock,
716 &entry->timestamp_end_ns);
717 if (ret) {
0f5c5d5c
SM
718 BT_CPPLOGI_STR_SPEC(ds_file->logger,
719 "Failed to convert raw timestamp to nanoseconds since Epoch.");
4164020e
SM
720 goto end;
721 }
722 } else {
723 entry->timestamp_end = UINT64_C(-1);
724 entry->timestamp_end_ns = UINT64_C(-1);
725 }
0b29603d 726
9e0c8dbb 727end:
4164020e 728 return ret;
9e0c8dbb
JG
729}
730
441fa755
SM
731static ctf_fs_ds_index::UP build_index_from_stream_file(struct ctf_fs_ds_file *ds_file,
732 struct ctf_fs_ds_file_info *file_info,
733 struct ctf_msg_iter *msg_iter)
9e0c8dbb 734{
4164020e 735 int ret;
441fa755 736 ctf_fs_ds_index::UP index;
4164020e 737 enum ctf_msg_iter_status iter_status = CTF_MSG_ITER_STATUS_OK;
ef7d7ac2 738 auto currentPacketOffset = bt2c::DataLen::fromBytes(0);
4164020e 739
0f5c5d5c 740 BT_CPPLOGI_SPEC(ds_file->logger, "Indexing stream file {}", ds_file->file->path->str);
4164020e 741
0f5c5d5c 742 index = ctf_fs_ds_index_create(ds_file->logger);
4164020e
SM
743 if (!index) {
744 goto error;
745 }
746
747 while (true) {
4164020e
SM
748 struct ctf_msg_iter_packet_properties props;
749
ef7d7ac2 750 if (currentPacketOffset.bytes() > ds_file->file->size) {
0f5c5d5c
SM
751 BT_CPPLOGE_STR_SPEC(ds_file->logger,
752 "Unexpected current packet's offset (larger than file).");
4164020e 753 goto error;
ef7d7ac2 754 } else if (currentPacketOffset.bytes() == ds_file->file->size) {
4164020e
SM
755 /* No more data */
756 break;
757 }
758
ef7d7ac2 759 iter_status = ctf_msg_iter_seek(msg_iter, currentPacketOffset.bytes());
4164020e
SM
760 if (iter_status != CTF_MSG_ITER_STATUS_OK) {
761 goto error;
762 }
763
764 iter_status = ctf_msg_iter_get_packet_properties(msg_iter, &props);
765 if (iter_status != CTF_MSG_ITER_STATUS_OK) {
766 goto error;
767 }
768
ef7d7ac2
SM
769 /*
770 * Get the current packet size from the packet header, if set. Else,
771 * assume there is a single packet in the file, so take the file size
772 * as the packet size.
773 */
774 const auto currentPacketSize = props.exp_packet_total_size >= 0 ?
775 bt2c::DataLen::fromBits(props.exp_packet_total_size) :
776 bt2c::DataLen::fromBytes(ds_file->file->size);
4164020e 777
ef7d7ac2 778 if ((currentPacketOffset + currentPacketSize).bytes() > ds_file->file->size) {
0f5c5d5c
SM
779 BT_CPPLOGW_SPEC(ds_file->logger,
780 "Invalid packet size reported in file: stream=\"{}\", "
ef7d7ac2
SM
781 "packet-offset-bytes={}, packet-size-bytes={}, "
782 "file-size-bytes={}",
783 ds_file->file->path->str, currentPacketOffset.bytes(),
784 currentPacketSize.bytes(), ds_file->file->size);
4164020e
SM
785 goto error;
786 }
787
c05e1405 788 auto index_entry = ctf_fs_ds_index_entry_create(currentPacketOffset, currentPacketSize);
4164020e 789 if (!index_entry) {
0f5c5d5c
SM
790 BT_CPPLOGE_APPEND_CAUSE_SPEC(ds_file->logger,
791 "Failed to create a ctf_fs_ds_index_entry.");
4164020e
SM
792 goto error;
793 }
794
795 /* Set path to stream file. */
4d199954 796 index_entry->path = file_info->path.c_str();
4164020e 797
c05e1405 798 ret = init_index_entry(index_entry.get(), ds_file, &props);
4164020e 799 if (ret) {
4164020e
SM
800 goto error;
801 }
802
c05e1405 803 g_ptr_array_add(index->entries, index_entry.release());
4164020e 804
ef7d7ac2 805 currentPacketOffset += currentPacketSize;
0f5c5d5c 806 BT_CPPLOGD_SPEC(ds_file->logger,
ef7d7ac2
SM
807 "Seeking to next packet: current-packet-offset-bytes={}, "
808 "next-packet-offset-bytes={}",
809 (currentPacketOffset - currentPacketSize).bytes(),
810 currentPacketOffset.bytes());
4164020e 811 }
312c056a 812
9e0c8dbb 813end:
4164020e 814 return index;
312c056a 815
9e0c8dbb 816error:
441fa755 817 index.reset();
4164020e 818 goto end;
9e0c8dbb
JG
819}
820
76edbcf1 821struct ctf_fs_ds_file *ctf_fs_ds_file_create(struct ctf_fs_trace *ctf_fs_trace, bt_stream *stream,
0f5c5d5c 822 const char *path, const bt2c::Logger& parentLogger)
e98a2d6e 823{
4164020e 824 int ret;
0f5c5d5c
SM
825 size_t offset_align;
826 ctf_fs_ds_file *ds_file = new ctf_fs_ds_file {parentLogger};
4164020e
SM
827
828 if (!ds_file) {
829 goto error;
830 }
831
0f5c5d5c 832 ds_file->file = ctf_fs_file_create(parentLogger);
4164020e
SM
833 if (!ds_file->file) {
834 goto error;
835 }
836
837 ds_file->stream = stream;
838 bt_stream_get_ref(ds_file->stream);
839 ds_file->metadata = ctf_fs_trace->metadata;
840 g_string_assign(ds_file->file->path, path);
841 ret = ctf_fs_file_open(ds_file->file, "rb");
842 if (ret) {
843 goto error;
844 }
845
0f5c5d5c 846 offset_align = bt_mmap_get_offset_align_size(static_cast<int>(ds_file->logger.level()));
4164020e
SM
847 ds_file->mmap_max_len = offset_align * 2048;
848
849 goto end;
1a9f7075 850
e98a2d6e 851error:
4164020e
SM
852 /* Do not touch "borrowed" file. */
853 ctf_fs_ds_file_destroy(ds_file);
854 ds_file = NULL;
1a9f7075 855
e98a2d6e 856end:
4164020e 857 return ds_file;
e98a2d6e
PP
858}
859
441fa755
SM
860ctf_fs_ds_index::UP ctf_fs_ds_file_build_index(struct ctf_fs_ds_file *ds_file,
861 struct ctf_fs_ds_file_info *file_info,
862 struct ctf_msg_iter *msg_iter)
97ade20b 863{
441fa755 864 auto index = build_index_from_idx_file(ds_file, file_info, msg_iter);
4164020e
SM
865 if (index) {
866 goto end;
867 }
868
0f5c5d5c
SM
869 BT_CPPLOGI_SPEC(ds_file->logger, "Failed to build index from .index file; "
870 "falling back to stream indexing.");
4164020e 871 index = build_index_from_stream_file(ds_file, file_info, msg_iter);
9e0c8dbb 872end:
4164020e 873 return index;
97ade20b
JG
874}
875
441fa755 876ctf_fs_ds_index::UP ctf_fs_ds_index_create(const bt2c::Logger& logger)
7ed5243a 877{
441fa755
SM
878 ctf_fs_ds_index::UP index {new ctf_fs_ds_index};
879
afb0f12b 880 index->entries = g_ptr_array_new_with_free_func((GDestroyNotify) ctf_fs_ds_index_entry_destroy);
4164020e 881 if (!index->entries) {
0f5c5d5c 882 BT_CPPLOGE_SPEC(logger, "Failed to allocate index entries.");
4164020e
SM
883 goto error;
884 }
7ed5243a 885
4164020e 886 goto end;
7ed5243a
FD
887
888error:
441fa755
SM
889 index.reset();
890
7ed5243a 891end:
4164020e 892 return index;
7ed5243a
FD
893}
894
94cf822e 895void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file)
e98a2d6e 896{
4164020e
SM
897 if (!ds_file) {
898 return;
899 }
043e2020 900
4164020e
SM
901 bt_stream_put_ref(ds_file->stream);
902 (void) ds_file_munmap(ds_file);
0982a26d 903
4164020e
SM
904 if (ds_file->file) {
905 ctf_fs_file_destroy(ds_file->file);
906 }
e98a2d6e 907
afb0f12b 908 delete ds_file;
e98a2d6e 909}
4f1f88a6 910
97ade20b
JG
911void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index)
912{
4164020e
SM
913 if (!index) {
914 return;
915 }
916
917 if (index->entries) {
918 g_ptr_array_free(index->entries, TRUE);
919 }
afb0f12b
SM
920
921 delete index;
97ade20b 922}
873c329a 923
441fa755
SM
924void ctf_fs_ds_index_deleter::operator()(ctf_fs_ds_index * const index) noexcept
925{
926 ctf_fs_ds_index_destroy(index);
927}
928
2cef6403 929ctf_fs_ds_file_info::UP ctf_fs_ds_file_info_create(const char *path, int64_t begin_ns)
873c329a 930{
2cef6403 931 ctf_fs_ds_file_info::UP ds_file_info = bt2s::make_unique<ctf_fs_ds_file_info>();
873c329a 932
4d199954 933 ds_file_info->path = path;
873c329a 934 ds_file_info->begin_ns = begin_ns;
873c329a
SM
935 return ds_file_info;
936}
937
cdf7de78 938static void ctf_fs_ds_file_group_destroy(struct ctf_fs_ds_file_group *ds_file_group)
873c329a
SM
939{
940 if (!ds_file_group) {
941 return;
942 }
943
873c329a
SM
944 bt_stream_put_ref(ds_file_group->stream);
945 delete ds_file_group;
946}
947
fe2e19c4 948void ctf_fs_ds_file_group_deleter::operator()(ctf_fs_ds_file_group *group) noexcept
873c329a 949{
fe2e19c4
SM
950 ctf_fs_ds_file_group_destroy(group);
951}
952
953ctf_fs_ds_file_group::UP ctf_fs_ds_file_group_create(struct ctf_fs_trace *ctf_fs_trace,
954 struct ctf_stream_class *sc,
955 uint64_t stream_instance_id,
fe2f9cda 956 ctf_fs_ds_index::UP index)
fe2e19c4
SM
957{
958 ctf_fs_ds_file_group::UP ds_file_group {new ctf_fs_ds_file_group};
959
fe2f9cda 960 ds_file_group->index = std::move(index);
873c329a
SM
961
962 ds_file_group->stream_id = stream_instance_id;
963 BT_ASSERT(sc);
964 ds_file_group->sc = sc;
965 ds_file_group->ctf_fs_trace = ctf_fs_trace;
873c329a 966
873c329a
SM
967 return ds_file_group;
968}
This page took 0.185524 seconds and 4 git commands to generate.