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