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