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