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