compat: fix compilation with !BABELTRACE_HAVE_OPEN_MEMSTREAM
[babeltrace.git] / src / plugins / ctf / fs-src / data-stream-file.c
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"
78586d8a
JG
24#include "file.h"
25#include "metadata.h"
d6e69534 26#include "../common/msg-iter/msg-iter.h"
578e048b 27#include "common/assert.h"
94cf822e 28#include "data-stream-file.h"
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;
94cf822e 198 struct ctf_fs_ds_file *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{
94cf822e 251 struct ctf_fs_ds_file *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{
9e0c8dbb 275 struct ctf_fs_ds_file *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 = {
e98a2d6e 285 .request_bytes = medop_request_bytes,
312c056a 286 .borrow_stream = medop_borrow_stream,
9e0c8dbb 287 .seek = medop_seek,
e98a2d6e 288};
6de92955 289
f6e68e70
SM
290struct ctf_fs_ds_group_medops_data {
291 /* Weak, set once at creation time. */
292 struct ctf_fs_ds_file_group *ds_file_group;
293
294 /*
295 * Index (as in element rank) of the index entry of ds_file_groups'
296 * index we will read next (so, the one after the one we are reading
297 * right now).
298 */
299 guint next_index_entry_index;
300
301 /*
302 * File we are currently reading. Changes whenever we switch to
303 * reading another data file.
304 *
305 * Owned by this.
306 */
307 struct ctf_fs_ds_file *file;
308
309 /* Weak, for context / logging / appending causes. */
310 bt_self_message_iterator *self_msg_iter;
311 bt_logging_level log_level;
312};
313
314static
315enum ctf_msg_iter_medium_status medop_group_request_bytes(
316 size_t request_sz,
317 uint8_t **buffer_addr,
318 size_t *buffer_sz,
319 void *void_data)
320{
321 struct ctf_fs_ds_group_medops_data *data = void_data;
322
323 /* Return bytes from the current file. */
324 return medop_request_bytes(request_sz, buffer_addr, buffer_sz, data->file);
325}
326
327static
328bt_stream *medop_group_borrow_stream(
329 bt_stream_class *stream_class,
330 int64_t stream_id,
331 void *void_data)
332{
333 struct ctf_fs_ds_group_medops_data *data = void_data;
334
335 return medop_borrow_stream(stream_class, stream_id, data->file);
336}
337
338/*
339 * Set `data->file` to prepare it to read the packet described
340 * by `index_entry`.
341 */
342
343static
344enum ctf_msg_iter_medium_status ctf_fs_ds_group_medops_set_file(
345 struct ctf_fs_ds_group_medops_data *data,
346 struct ctf_fs_ds_index_entry *index_entry,
347 bt_self_message_iterator *self_msg_iter,
348 bt_logging_level log_level)
349{
350 enum ctf_msg_iter_medium_status status;
351
352 BT_ASSERT(data);
353 BT_ASSERT(index_entry);
354
355 /* Check if that file is already the one mapped. */
356 if (!data->file || strcmp(index_entry->path, data->file->file->path->str) != 0) {
357 /* Destroy the previously used file. */
358 ctf_fs_ds_file_destroy(data->file);
359
360 /* Create the new file. */
361 data->file = ctf_fs_ds_file_create(
362 data->ds_file_group->ctf_fs_trace,
363 self_msg_iter,
364 data->ds_file_group->stream,
365 index_entry->path,
366 log_level);
367 if (!data->file) {
368 BT_MSG_ITER_LOGE_APPEND_CAUSE(self_msg_iter,
369 "failed to create ctf_fs_ds_file.");
370 status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
371 goto end;
372 }
373 }
374
375 /*
376 * Ensure the right portion of the file will be returned on the next
377 * request_bytes call.
378 */
379 status = ds_file_mmap(data->file, index_entry->offset);
380 if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) {
381 goto end;
382 }
383
384 status = CTF_MSG_ITER_MEDIUM_STATUS_OK;
385
386end:
387 return status;
388}
389
390static
391enum ctf_msg_iter_medium_status medop_group_switch_packet(void *void_data)
392{
393 struct ctf_fs_ds_group_medops_data *data = void_data;
394 struct ctf_fs_ds_index_entry *index_entry;
395 enum ctf_msg_iter_medium_status status;
396
397 /* If we have gone through all index entries, we are done. */
398 if (data->next_index_entry_index >=
399 data->ds_file_group->index->entries->len) {
400 status = CTF_MSG_ITER_MEDIUM_STATUS_EOF;
401 goto end;
402 }
403
404 /*
405 * Otherwise, look up the next index entry / packet and prepare it
406 * for reading.
407 */
408 index_entry = g_ptr_array_index(
409 data->ds_file_group->index->entries,
410 data->next_index_entry_index);
411
412 status = ctf_fs_ds_group_medops_set_file(
413 data, index_entry, data->self_msg_iter, data->log_level);
414 if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) {
415 goto end;
416 }
417
418 data->next_index_entry_index++;
419
420 status = CTF_MSG_ITER_MEDIUM_STATUS_OK;
421end:
422 return status;
423}
424
425BT_HIDDEN
426void ctf_fs_ds_group_medops_data_destroy(
427 struct ctf_fs_ds_group_medops_data *data)
428{
429 if (!data) {
430 goto end;
431 }
432
433 ctf_fs_ds_file_destroy(data->file);
434
435 g_free(data);
436
437end:
438 return;
439}
440
441enum ctf_msg_iter_medium_status ctf_fs_ds_group_medops_data_create(
442 struct ctf_fs_ds_file_group *ds_file_group,
443 bt_self_message_iterator *self_msg_iter,
444 bt_logging_level log_level,
445 struct ctf_fs_ds_group_medops_data **out)
446{
447 struct ctf_fs_ds_group_medops_data *data;
448 enum ctf_msg_iter_medium_status status;
449
450 BT_ASSERT(self_msg_iter);
451 BT_ASSERT(ds_file_group);
452 BT_ASSERT(ds_file_group->index);
453 BT_ASSERT(ds_file_group->index->entries->len > 0);
454
455 data = g_new0(struct ctf_fs_ds_group_medops_data, 1);
456 if (!data) {
457 BT_MSG_ITER_LOGE_APPEND_CAUSE(self_msg_iter,
458 "Failed to allocate a struct ctf_fs_ds_group_medops_data");
459 status = CTF_MSG_ITER_MEDIUM_STATUS_MEMORY_ERROR;
460 goto error;
461 }
462
463 data->ds_file_group = ds_file_group;
464 data->self_msg_iter = self_msg_iter;
465 data->log_level = log_level;
466
467 /*
468 * No need to prepare the first file. ctf_msg_iter will call
469 * switch_packet before reading the first packet, it will be
470 * done then.
471 */
472
473 *out = data;
474 status = CTF_MSG_ITER_MEDIUM_STATUS_OK;
475 goto end;
476
477error:
478 ctf_fs_ds_group_medops_data_destroy(data);
479
480end:
481 return status;
482}
483
484void ctf_fs_ds_group_medops_data_reset(struct ctf_fs_ds_group_medops_data *data)
485{
486 data->next_index_entry_index = 0;
487}
488
489struct ctf_msg_iter_medium_ops ctf_fs_ds_group_medops = {
490 .request_bytes = medop_group_request_bytes,
491 .borrow_stream = medop_group_borrow_stream,
492 .switch_packet = medop_group_switch_packet,
493
494 /*
495 * We don't support seeking using this medops. It would probably be
496 * possible, but it's not needed at the moment.
497 */
498 .seek = NULL,
499};
500
6834784d
SM
501static
502struct ctf_fs_ds_index_entry *ctf_fs_ds_index_entry_create(
503 bt_self_component *self_comp, bt_logging_level log_level)
504{
505 struct ctf_fs_ds_index_entry *entry;
506
507 entry = g_new0(struct ctf_fs_ds_index_entry, 1);
508 if (!entry) {
509 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to allocate a ctf_fs_ds_index_entry.");
510 goto end;
511 }
512
513 entry->packet_seq_num = UINT64_MAX;
514
515end:
516 return entry;
517}
518
97ade20b 519static
0f2d58c9 520int convert_cycles_to_ns(struct ctf_clock_class *clock_class,
97ade20b 521 uint64_t cycles, int64_t *ns)
b6c3dcb2 522{
0f2d58c9
PP
523 return bt_util_clock_cycles_to_ns_from_origin(cycles,
524 clock_class->frequency, clock_class->offset_seconds,
525 clock_class->offset_cycles, ns);
97ade20b
JG
526}
527
528static
529struct ctf_fs_ds_index *build_index_from_idx_file(
bf012bde 530 struct ctf_fs_ds_file *ds_file,
6d54260a
SM
531 struct ctf_fs_ds_file_info *file_info,
532 struct ctf_msg_iter *msg_iter)
97ade20b
JG
533{
534 int ret;
b6c3dcb2
JG
535 gchar *directory = NULL;
536 gchar *basename = NULL;
537 GString *index_basename = NULL;
538 gchar *index_file_path = NULL;
539 GMappedFile *mapped_file = NULL;
540 gsize filesize;
97ade20b
JG
541 const char *mmap_begin = NULL, *file_pos = NULL;
542 const struct ctf_packet_index_file_hdr *header = NULL;
543 struct ctf_fs_ds_index *index = NULL;
de38c26a 544 struct ctf_fs_ds_index_entry *index_entry = NULL, *prev_index_entry = NULL;
b6c3dcb2
JG
545 uint64_t total_packets_size = 0;
546 size_t file_index_entry_size;
547 size_t file_entry_count;
548 size_t i;
44c440bc 549 struct ctf_stream_class *sc;
18a1979b 550 struct ctf_msg_iter_packet_properties props;
1984ac2b 551 uint32_t version_major, version_minor;
2e42d046
SM
552 bt_self_component *self_comp = ds_file->self_comp;
553 bt_logging_level log_level = ds_file->log_level;
97ade20b 554
4c65a157 555 BT_COMP_LOGI("Building index from .idx file of stream file %s",
97ade20b 556 ds_file->file->path->str);
6d54260a 557 ret = ctf_msg_iter_get_packet_properties(msg_iter, &props);
97ade20b 558 if (ret) {
4c65a157 559 BT_COMP_LOGI_STR("Cannot read first packet's header and context fields.");
44c440bc
PP
560 goto error;
561 }
562
44c440bc
PP
563 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
564 props.stream_class_id);
565 BT_ASSERT(sc);
566 if (!sc->default_clock_class) {
4c65a157 567 BT_COMP_LOGI_STR("Cannot find stream class's default clock class.");
97ade20b
JG
568 goto error;
569 }
b6c3dcb2
JG
570
571 /* Look for index file in relative path index/name.idx. */
94cf822e 572 basename = g_path_get_basename(ds_file->file->path->str);
b6c3dcb2 573 if (!basename) {
4c65a157 574 BT_COMP_LOGE("Cannot get the basename of datastream file %s",
55314f2a 575 ds_file->file->path->str);
97ade20b 576 goto error;
b6c3dcb2
JG
577 }
578
94cf822e 579 directory = g_path_get_dirname(ds_file->file->path->str);
b6c3dcb2 580 if (!directory) {
4c65a157 581 BT_COMP_LOGE("Cannot get dirname of datastream file %s",
55314f2a 582 ds_file->file->path->str);
97ade20b 583 goto error;
b6c3dcb2
JG
584 }
585
586 index_basename = g_string_new(basename);
587 if (!index_basename) {
4c65a157 588 BT_COMP_LOGE_STR("Cannot allocate index file basename string");
97ade20b 589 goto error;
b6c3dcb2
JG
590 }
591
592 g_string_append(index_basename, ".idx");
593 index_file_path = g_build_filename(directory, "index",
594 index_basename->str, NULL);
595 mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL);
06367fa3 596 if (!mapped_file) {
4c65a157 597 BT_COMP_LOGD("Cannot create new mapped file %s",
55314f2a 598 index_file_path);
97ade20b 599 goto error;
06367fa3 600 }
d14940a7
JG
601
602 /*
603 * The g_mapped_file API limits us to 4GB files on 32-bit.
604 * Traces with such large indexes have never been seen in the wild,
605 * but this would need to be adjusted to support them.
606 */
b6c3dcb2
JG
607 filesize = g_mapped_file_get_length(mapped_file);
608 if (filesize < sizeof(*header)) {
4c65a157 609 BT_COMP_LOGW("Invalid LTTng trace index file: "
d14940a7
JG
610 "file size (%zu bytes) < header size (%zu bytes)",
611 filesize, sizeof(*header));
97ade20b 612 goto error;
b6c3dcb2
JG
613 }
614
615 mmap_begin = g_mapped_file_get_contents(mapped_file);
616 header = (struct ctf_packet_index_file_hdr *) mmap_begin;
617
618 file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header);
619 if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
4c65a157 620 BT_COMP_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
97ade20b 621 goto error;
b6c3dcb2 622 }
b6c3dcb2 623
1984ac2b
SM
624 version_major = be32toh(header->index_major);
625 version_minor = be32toh(header->index_minor);
626 if (version_major != 1) {
627 BT_COMP_LOGW(
628 "Unknown LTTng trace index version: "
629 "major=%" PRIu32 ", minor=%" PRIu32,
630 version_major, version_minor);
631 goto error;
632 }
633
b6c3dcb2 634 file_index_entry_size = be32toh(header->packet_index_len);
ef4ef756
JG
635 if (file_index_entry_size < CTF_INDEX_1_0_SIZE) {
636 BT_COMP_LOGW("Invalid `packet_index_len` in LTTng trace index file (`packet_index_len` < CTF index 1.0 index entry size): "
637 "packet_index_len=%zu, CTF_INDEX_1_0_SIZE=%zu",
638 file_index_entry_size, CTF_INDEX_1_0_SIZE);
639 goto error;
640 }
641
b6c3dcb2 642 file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
97ade20b 643 if ((filesize - sizeof(*header)) % file_index_entry_size) {
4c65a157 644 BT_COMP_LOGW("Invalid LTTng trace index: the index's size after the header "
d14940a7
JG
645 "(%zu bytes) is not a multiple of the index entry size "
646 "(%zu bytes)", (filesize - sizeof(*header)),
647 sizeof(*header));
97ade20b 648 goto error;
b6c3dcb2
JG
649 }
650
4c65a157 651 index = ctf_fs_ds_index_create(ds_file->log_level, ds_file->self_comp);
97ade20b
JG
652 if (!index) {
653 goto error;
b6c3dcb2 654 }
97ade20b 655
b6c3dcb2
JG
656 for (i = 0; i < file_entry_count; i++) {
657 struct ctf_packet_index *file_index =
658 (struct ctf_packet_index *) file_pos;
659 uint64_t packet_size = be64toh(file_index->packet_size);
660
661 if (packet_size % CHAR_BIT) {
4c65a157 662 BT_COMP_LOGW("Invalid packet size encountered in LTTng trace index file");
97ade20b 663 goto error;
b6c3dcb2
JG
664 }
665
6834784d
SM
666 index_entry = ctf_fs_ds_index_entry_create(
667 ds_file->self_comp, ds_file->log_level);
7ed5243a 668 if (!index_entry) {
6834784d
SM
669 BT_COMP_LOGE_APPEND_CAUSE(ds_file->self_comp,
670 "Failed to create a ctf_fs_ds_index_entry.");
7ed5243a
FD
671 goto error;
672 }
673
bf012bde
FD
674 /* Set path to stream file. */
675 index_entry->path = file_info->path->str;
676
b6c3dcb2
JG
677 /* Convert size in bits to bytes. */
678 packet_size /= CHAR_BIT;
97ade20b 679 index_entry->packet_size = packet_size;
b6c3dcb2 680
97ade20b 681 index_entry->offset = be64toh(file_index->offset);
de38c26a 682 if (i != 0 && index_entry->offset < prev_index_entry->offset) {
4c65a157 683 BT_COMP_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
d14940a7 684 "previous offset=%" PRIu64 ", current offset=%" PRIu64,
9dd33658 685 prev_index_entry->offset, index_entry->offset);
97ade20b 686 goto error;
b6c3dcb2
JG
687 }
688
97ade20b
JG
689 index_entry->timestamp_begin = be64toh(file_index->timestamp_begin);
690 index_entry->timestamp_end = be64toh(file_index->timestamp_end);
691 if (index_entry->timestamp_end < index_entry->timestamp_begin) {
4c65a157 692 BT_COMP_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
d14940a7
JG
693 "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64,
694 index_entry->timestamp_begin,
695 index_entry->timestamp_end);
97ade20b
JG
696 goto error;
697 }
698
699 /* Convert the packet's bound to nanoseconds since Epoch. */
44c440bc 700 ret = convert_cycles_to_ns(sc->default_clock_class,
97ade20b
JG
701 index_entry->timestamp_begin,
702 &index_entry->timestamp_begin_ns);
703 if (ret) {
4c65a157 704 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
97ade20b
JG
705 goto error;
706 }
44c440bc 707 ret = convert_cycles_to_ns(sc->default_clock_class,
97ade20b
JG
708 index_entry->timestamp_end,
709 &index_entry->timestamp_end_ns);
710 if (ret) {
4c65a157 711 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
97ade20b 712 goto error;
b6c3dcb2
JG
713 }
714
6834784d
SM
715 if (version_minor >= 1) {
716 index_entry->packet_seq_num = be64toh(file_index->packet_seq_num);
717 }
718
b6c3dcb2
JG
719 total_packets_size += packet_size;
720 file_pos += file_index_entry_size;
7ed5243a 721
de38c26a 722 prev_index_entry = index_entry;
f35a48bc
SM
723
724 /* Give ownership of `index_entry` to `index->entries`. */
725 g_ptr_array_add(index->entries, index_entry);
726 index_entry = NULL;
b6c3dcb2
JG
727 }
728
729 /* Validate that the index addresses the complete stream. */
94cf822e 730 if (ds_file->file->size != total_packets_size) {
4c65a157 731 BT_COMP_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
9e0c8dbb 732 "file-size=%" PRIu64 ", total-packets-size=%" PRIu64,
d14940a7 733 ds_file->file->size, total_packets_size);
97ade20b 734 goto error;
b6c3dcb2
JG
735 }
736end:
737 g_free(directory);
738 g_free(basename);
739 g_free(index_file_path);
06367fa3
JG
740 if (index_basename) {
741 g_string_free(index_basename, TRUE);
742 }
743 if (mapped_file) {
744 g_mapped_file_unref(mapped_file);
745 }
97ade20b
JG
746 return index;
747error:
748 ctf_fs_ds_index_destroy(index);
7ed5243a 749 g_free(index_entry);
97ade20b 750 index = NULL;
b6c3dcb2
JG
751 goto end;
752}
753
9e0c8dbb
JG
754static
755int init_index_entry(struct ctf_fs_ds_index_entry *entry,
44c440bc 756 struct ctf_fs_ds_file *ds_file,
18a1979b 757 struct ctf_msg_iter_packet_properties *props,
44c440bc 758 off_t packet_size, off_t packet_offset)
9e0c8dbb 759{
ca79a87c 760 int ret = 0;
44c440bc 761 struct ctf_stream_class *sc;
2e42d046
SM
762 bt_self_component *self_comp = ds_file->self_comp;
763 bt_logging_level log_level = ds_file->log_level;
9e0c8dbb 764
44c440bc
PP
765 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
766 props->stream_class_id);
767 BT_ASSERT(sc);
f6ccaed9 768 BT_ASSERT(packet_offset >= 0);
9e0c8dbb 769 entry->offset = packet_offset;
f6ccaed9 770 BT_ASSERT(packet_size >= 0);
9e0c8dbb
JG
771 entry->packet_size = packet_size;
772
83ebb7f1 773 if (props->snapshots.beginning_clock != UINT64_C(-1)) {
41bd46eb
FD
774 entry->timestamp_begin = props->snapshots.beginning_clock;
775
83ebb7f1
PP
776 /* Convert the packet's bound to nanoseconds since Epoch. */
777 ret = convert_cycles_to_ns(sc->default_clock_class,
778 props->snapshots.beginning_clock,
779 &entry->timestamp_begin_ns);
780 if (ret) {
4c65a157 781 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
83ebb7f1
PP
782 goto end;
783 }
784 } else {
41bd46eb 785 entry->timestamp_begin = UINT64_C(-1);
83ebb7f1 786 entry->timestamp_begin_ns = UINT64_C(-1);
9e0c8dbb
JG
787 }
788
83ebb7f1 789 if (props->snapshots.end_clock != UINT64_C(-1)) {
41bd46eb
FD
790 entry->timestamp_end = props->snapshots.end_clock;
791
792 /* Convert the packet's bound to nanoseconds since Epoch. */
83ebb7f1
PP
793 ret = convert_cycles_to_ns(sc->default_clock_class,
794 props->snapshots.end_clock,
795 &entry->timestamp_end_ns);
796 if (ret) {
4c65a157 797 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
83ebb7f1
PP
798 goto end;
799 }
800 } else {
41bd46eb 801 entry->timestamp_end = UINT64_C(-1);
83ebb7f1 802 entry->timestamp_end_ns = UINT64_C(-1);
9e0c8dbb 803 }
0b29603d 804
9e0c8dbb 805end:
9e0c8dbb
JG
806 return ret;
807}
808
809static
810struct ctf_fs_ds_index *build_index_from_stream_file(
bf012bde 811 struct ctf_fs_ds_file *ds_file,
6d54260a
SM
812 struct ctf_fs_ds_file_info *file_info,
813 struct ctf_msg_iter *msg_iter)
9e0c8dbb
JG
814{
815 int ret;
816 struct ctf_fs_ds_index *index = NULL;
18a1979b 817 enum ctf_msg_iter_status iter_status = CTF_MSG_ITER_STATUS_OK;
fc917f65 818 off_t current_packet_offset_bytes = 0;
2e42d046
SM
819 bt_self_component *self_comp = ds_file->self_comp;
820 bt_logging_level log_level = ds_file->log_level;
9e0c8dbb 821
4c65a157 822 BT_COMP_LOGI("Indexing stream file %s", ds_file->file->path->str);
9e0c8dbb 823
4c65a157 824 index = ctf_fs_ds_index_create(ds_file->log_level, ds_file->self_comp);
9e0c8dbb
JG
825 if (!index) {
826 goto error;
827 }
828
2b601d0c 829 while (true) {
44c440bc 830 off_t current_packet_size_bytes;
7ed5243a 831 struct ctf_fs_ds_index_entry *index_entry;
18a1979b 832 struct ctf_msg_iter_packet_properties props;
9e0c8dbb 833
fc917f65 834 if (current_packet_offset_bytes < 0) {
4c65a157 835 BT_COMP_LOGE_STR("Cannot get the current packet's offset.");
fc917f65
PP
836 goto error;
837 } else if (current_packet_offset_bytes > ds_file->file->size) {
4c65a157 838 BT_COMP_LOGE_STR("Unexpected current packet's offset (larger than file).");
fc917f65
PP
839 goto error;
840 } else if (current_packet_offset_bytes == ds_file->file->size) {
841 /* No more data */
842 break;
843 }
844
6d54260a 845 iter_status = ctf_msg_iter_seek(msg_iter,
fc917f65 846 current_packet_offset_bytes);
18a1979b 847 if (iter_status != CTF_MSG_ITER_STATUS_OK) {
9e0c8dbb
JG
848 goto error;
849 }
312c056a 850
18a1979b 851 iter_status = ctf_msg_iter_get_packet_properties(
6d54260a 852 msg_iter, &props);
18a1979b 853 if (iter_status != CTF_MSG_ITER_STATUS_OK) {
9e0c8dbb
JG
854 goto error;
855 }
856
fc917f65
PP
857 if (props.exp_packet_total_size >= 0) {
858 current_packet_size_bytes =
859 (uint64_t) props.exp_packet_total_size / 8;
860 } else {
861 current_packet_size_bytes = ds_file->file->size;
862 }
9e0c8dbb 863
fc917f65 864 if (current_packet_offset_bytes + current_packet_size_bytes >
9e0c8dbb 865 ds_file->file->size) {
4c65a157 866 BT_COMP_LOGW("Invalid packet size reported in file: stream=\"%s\", "
9e0c8dbb
JG
867 "packet-offset=%jd, packet-size-bytes=%jd, "
868 "file-size=%jd",
869 ds_file->file->path->str,
df0fc0bf
JR
870 (intmax_t) current_packet_offset_bytes,
871 (intmax_t) current_packet_size_bytes,
872 (intmax_t) ds_file->file->size);
9e0c8dbb
JG
873 goto error;
874 }
875
6834784d
SM
876 index_entry = ctf_fs_ds_index_entry_create(
877 ds_file->self_comp, ds_file->log_level);
7ed5243a 878 if (!index_entry) {
6834784d
SM
879 BT_COMP_LOGE_APPEND_CAUSE(ds_file->self_comp,
880 "Failed to create a ctf_fs_ds_index_entry.");
9e0c8dbb
JG
881 goto error;
882 }
883
bf012bde
FD
884 /* Set path to stream file. */
885 index_entry->path = file_info->path->str;
886
7ed5243a 887 ret = init_index_entry(index_entry, ds_file, &props,
fc917f65 888 current_packet_size_bytes, current_packet_offset_bytes);
9e0c8dbb 889 if (ret) {
7ed5243a 890 g_free(index_entry);
9e0c8dbb
JG
891 goto error;
892 }
7ed5243a
FD
893
894 g_ptr_array_add(index->entries, index_entry);
895
ca633588 896 current_packet_offset_bytes += current_packet_size_bytes;
4c65a157 897 BT_COMP_LOGD("Seeking to next packet: current-packet-offset=%jd, "
ca633588 898 "next-packet-offset=%jd",
df0fc0bf
JR
899 (intmax_t) (current_packet_offset_bytes - current_packet_size_bytes),
900 (intmax_t) current_packet_offset_bytes);
9e0c8dbb 901 }
312c056a 902
9e0c8dbb 903end:
9e0c8dbb 904 return index;
312c056a 905
9e0c8dbb
JG
906error:
907 ctf_fs_ds_index_destroy(index);
908 index = NULL;
909 goto end;
910}
911
e7a4393b 912BT_HIDDEN
94cf822e
PP
913struct ctf_fs_ds_file *ctf_fs_ds_file_create(
914 struct ctf_fs_trace *ctf_fs_trace,
f5f7e8df 915 bt_self_message_iterator *self_msg_iter,
98903a3e
PP
916 bt_stream *stream, const char *path,
917 bt_logging_level log_level)
e98a2d6e 918{
b6c3dcb2 919 int ret;
3b16a19b 920 const size_t offset_align = bt_mmap_get_offset_align_size(log_level);
94cf822e 921 struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1);
e98a2d6e 922
94cf822e 923 if (!ds_file) {
e98a2d6e
PP
924 goto error;
925 }
926
98903a3e 927 ds_file->log_level = log_level;
4c65a157 928 ds_file->self_comp = ctf_fs_trace->self_comp;
f5f7e8df 929 ds_file->self_msg_iter = self_msg_iter;
4c65a157 930 ds_file->file = ctf_fs_file_create(log_level, ds_file->self_comp);
94cf822e 931 if (!ds_file->file) {
4f1f88a6
PP
932 goto error;
933 }
934
398454ed 935 ds_file->stream = stream;
c5b9b441 936 bt_stream_get_ref(ds_file->stream);
44c440bc 937 ds_file->metadata = ctf_fs_trace->metadata;
94cf822e 938 g_string_assign(ds_file->file->path, path);
55314f2a 939 ret = ctf_fs_file_open(ds_file->file, "rb");
4f1f88a6
PP
940 if (ret) {
941 goto error;
942 }
943
3b16a19b 944 ds_file->mmap_max_len = offset_align * 2048;
94cf822e 945
e98a2d6e 946 goto end;
1a9f7075 947
e98a2d6e 948error:
78586d8a 949 /* Do not touch "borrowed" file. */
94cf822e
PP
950 ctf_fs_ds_file_destroy(ds_file);
951 ds_file = NULL;
1a9f7075 952
e98a2d6e 953end:
94cf822e 954 return ds_file;
e98a2d6e
PP
955}
956
97ade20b
JG
957BT_HIDDEN
958struct ctf_fs_ds_index *ctf_fs_ds_file_build_index(
bf012bde 959 struct ctf_fs_ds_file *ds_file,
6d54260a
SM
960 struct ctf_fs_ds_file_info *file_info,
961 struct ctf_msg_iter *msg_iter)
97ade20b 962{
9e0c8dbb 963 struct ctf_fs_ds_index *index;
2e42d046
SM
964 bt_self_component *self_comp = ds_file->self_comp;
965 bt_logging_level log_level = ds_file->log_level;
9e0c8dbb 966
6d54260a 967 index = build_index_from_idx_file(ds_file, file_info, msg_iter);
9e0c8dbb
JG
968 if (index) {
969 goto end;
970 }
971
4c65a157 972 BT_COMP_LOGI("Failed to build index from .index file; "
9e0c8dbb 973 "falling back to stream indexing.");
6d54260a 974 index = build_index_from_stream_file(ds_file, file_info, msg_iter);
9e0c8dbb
JG
975end:
976 return index;
97ade20b
JG
977}
978
7ed5243a 979BT_HIDDEN
4c65a157
PP
980struct ctf_fs_ds_index *ctf_fs_ds_index_create(bt_logging_level log_level,
981 bt_self_component *self_comp)
7ed5243a
FD
982{
983 struct ctf_fs_ds_index *index = g_new0(struct ctf_fs_ds_index, 1);
984
985 if (!index) {
4c65a157 986 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
98903a3e 987 "Failed to allocate index");
7ed5243a
FD
988 goto error;
989 }
990
991 index->entries = g_ptr_array_new_with_free_func((GDestroyNotify) g_free);
992 if (!index->entries) {
4c65a157 993 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
98903a3e 994 "Failed to allocate index entries.");
7ed5243a
FD
995 goto error;
996 }
997
998 goto end;
999
1000error:
1001 ctf_fs_ds_index_destroy(index);
1002 index = NULL;
1003end:
1004 return index;
1005}
1006
5b29e799 1007BT_HIDDEN
94cf822e 1008void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file)
e98a2d6e 1009{
94cf822e 1010 if (!ds_file) {
5b29e799 1011 return;
043e2020
JG
1012 }
1013
c5b9b441 1014 bt_stream_put_ref(ds_file->stream);
94cf822e 1015 (void) ds_file_munmap(ds_file);
0982a26d 1016
94cf822e
PP
1017 if (ds_file->file) {
1018 ctf_fs_file_destroy(ds_file->file);
e98a2d6e
PP
1019 }
1020
94cf822e 1021 g_free(ds_file);
e98a2d6e 1022}
4f1f88a6 1023
97ade20b
JG
1024BT_HIDDEN
1025void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index)
1026{
1027 if (!index) {
1028 return;
1029 }
1030
1031 if (index->entries) {
7ed5243a 1032 g_ptr_array_free(index->entries, TRUE);
97ade20b
JG
1033 }
1034 g_free(index);
1035}
This page took 0.132528 seconds and 4 git commands to generate.