src.ctf.fs: factor out "ds_file_mmap" from "ds_file_mmap_next"
[babeltrace.git] / src / plugins / ctf / fs-src / data-stream-file.c
CommitLineData
e98a2d6e 1/*
94cf822e 2 * Copyright 2016-2017 - Philippe Proulx <pproulx@efficios.com>
fc9a526c 3 * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
e98a2d6e
PP
4 * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation
5 *
e98a2d6e
PP
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
d7bc0973
SM
25#define BT_COMP_LOG_SELF_COMP (self_comp)
26#define BT_LOG_OUTPUT_LEVEL (log_level)
3d731ea9 27#define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/DS"
3fa1b6a3 28#include "logging/comp-logging.h"
3d731ea9 29
0fbb9a9f 30#include <stdlib.h>
e98a2d6e
PP
31#include <stdio.h>
32#include <stdint.h>
33#include <stdlib.h>
e98a2d6e
PP
34#include <glib.h>
35#include <inttypes.h>
57952005
MJ
36#include "compat/mman.h"
37#include "compat/endian.h"
71c5da58 38#include <babeltrace2/babeltrace.h>
57952005 39#include "common/common.h"
78586d8a
JG
40#include "file.h"
41#include "metadata.h"
b09a5592 42#include "../common/msg-iter/msg-iter.h"
57952005 43#include "common/assert.h"
94cf822e 44#include "data-stream-file.h"
e9383dfd 45#include <string.h>
e98a2d6e 46
4f1f88a6 47static inline
94cf822e 48size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file)
e98a2d6e 49{
43b07461
SM
50 BT_ASSERT_DBG(ds_file->mmap_len >= ds_file->request_offset_in_mapping);
51 return ds_file->mmap_len - ds_file->request_offset_in_mapping;
e98a2d6e
PP
52}
53
0eb6c8ee
SM
54/*
55 * Return true if `offset_in_file` is in the current mapping.
56 */
57
58static
59bool offset_ist_mapped(struct ctf_fs_ds_file *ds_file, off_t offset_in_file)
60{
61 return offset_in_file >= ds_file->mmap_offset_in_file &&
62 offset_in_file < (ds_file->mmap_offset_in_file + ds_file->mmap_len);
63}
64
5b29e799 65static
3baf7da0
SM
66enum ctf_msg_iter_medium_status ds_file_munmap(
67 struct ctf_fs_ds_file *ds_file)
e98a2d6e 68{
3baf7da0 69 enum ctf_msg_iter_medium_status status;
d7bc0973
SM
70 bt_self_component *self_comp = ds_file->self_comp;
71 bt_logging_level log_level = ds_file->log_level;
e98a2d6e 72
4bbea805
SM
73 BT_ASSERT(ds_file);
74
75 if (!ds_file->mmap_addr) {
3baf7da0 76 status = CTF_MSG_ITER_MEDIUM_STATUS_OK;
94cf822e
PP
77 goto end;
78 }
79
a555e971 80 if (bt_munmap(ds_file->mmap_addr, ds_file->mmap_len)) {
f67894f9 81 BT_COMP_LOGE_ERRNO("Cannot memory-unmap file",
2a48e5ae 82 ": address=%p, size=%zu, file_path=\"%s\", file=%p",
94cf822e 83 ds_file->mmap_addr, ds_file->mmap_len,
bb230c9b 84 ds_file->file ? ds_file->file->path->str : "NULL",
2a48e5ae 85 ds_file->file ? ds_file->file->fp : NULL);
3baf7da0 86 status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
fc9a526c 87 goto end;
e98a2d6e 88 }
94cf822e
PP
89
90 ds_file->mmap_addr = NULL;
91
3baf7da0 92 status = CTF_MSG_ITER_MEDIUM_STATUS_OK;
fc9a526c 93end:
3baf7da0 94 return status;
e98a2d6e
PP
95}
96
0eb6c8ee
SM
97/*
98 * mmap a region of `ds_file` such that `requested_offset_in_file` is in the
99 * mapping. If the currently mmap-ed region already contains
100 * `requested_offset_in_file`, the mapping is kept.
101 *
102 * Set `ds_file->requested_offset_in_mapping` based on `request_offset_in_file`
103 *
104 * `requested_offset_in_file` must be a valid offset in the file.
105 */
5b29e799 106static
0eb6c8ee
SM
107enum ctf_msg_iter_medium_status ds_file_mmap(
108 struct ctf_fs_ds_file *ds_file, off_t requested_offset_in_file)
e98a2d6e 109{
3baf7da0 110 enum ctf_msg_iter_medium_status status;
d7bc0973
SM
111 bt_self_component *self_comp = ds_file->self_comp;
112 bt_logging_level log_level = ds_file->log_level;
e98a2d6e 113
0eb6c8ee
SM
114 /* Ensure the requested offset is in the file range. */
115 BT_ASSERT(requested_offset_in_file >= 0);
116 BT_ASSERT(requested_offset_in_file < ds_file->file->size);
e98a2d6e 117
0eb6c8ee
SM
118 /*
119 * If the mapping already contains the requested offset, just adjust
120 * requested_offset_in_mapping.
121 */
122 if (offset_ist_mapped(ds_file, requested_offset_in_file)) {
123 ds_file->request_offset_in_mapping =
124 requested_offset_in_file - ds_file->mmap_offset_in_file;
125 status = CTF_MSG_ITER_MEDIUM_STATUS_OK;
126 goto end;
e98a2d6e
PP
127 }
128
0eb6c8ee
SM
129 /* Unmap old region */
130 status = ds_file_munmap(ds_file);
131 if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) {
e0f6a64a
JG
132 goto end;
133 }
0eb6c8ee
SM
134
135 /*
136 * Compute a mapping that has the required alignment properties and
137 * contains `requested_offset_in_file`.
138 */
139 ds_file->request_offset_in_mapping =
140 requested_offset_in_file % bt_mmap_get_offset_align_size(ds_file->log_level);
141 ds_file->mmap_offset_in_file =
142 requested_offset_in_file - ds_file->request_offset_in_mapping;
143 ds_file->mmap_len = MIN(ds_file->file->size - ds_file->mmap_offset_in_file,
144 ds_file->mmap_max_len);
145
146 BT_ASSERT(ds_file->mmap_len > 0);
147
a555e971 148 ds_file->mmap_addr = bt_mmap((void *) 0, ds_file->mmap_len,
94cf822e 149 PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp),
43b07461 150 ds_file->mmap_offset_in_file, ds_file->log_level);
94cf822e 151 if (ds_file->mmap_addr == MAP_FAILED) {
f67894f9 152 BT_COMP_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s",
94cf822e 153 ds_file->mmap_len, ds_file->file->path->str,
43b07461 154 ds_file->file->fp, (intmax_t) ds_file->mmap_offset_in_file,
78586d8a 155 strerror(errno));
3baf7da0
SM
156 status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
157 goto end;
e98a2d6e
PP
158 }
159
3baf7da0 160 status = CTF_MSG_ITER_MEDIUM_STATUS_OK;
0eb6c8ee
SM
161
162end:
163 return status;
164}
165
166/*
167 * Change the mapping of the file to read the region that follows the current
168 * mapping.
169 *
170 * If the file hasn't been mapped yet, then everything (mmap_offset_in_file,
171 * mmap_len, request_offset_in_mapping) should have the value 0, which will
172 * result in the beginning of the file getting mapped.
173 *
174 * return _EOF if the current mapping is the end of the file.
175 */
176
177static
178enum ctf_msg_iter_medium_status ds_file_mmap_next(
179 struct ctf_fs_ds_file *ds_file)
180{
181 enum ctf_msg_iter_medium_status status;
182
183 /*
184 * If we're called, it's because more bytes are requested but we have
185 * given all the bytes of the current mapping.
186 */
187 BT_ASSERT(ds_file->request_offset_in_mapping == ds_file->mmap_len);
188
189 /*
190 * If the current mapping coincides with the end of the file, there is
191 * no next mapping.
192 */
193 if (ds_file->mmap_offset_in_file + ds_file->mmap_len == ds_file->file->size) {
194 status = CTF_MSG_ITER_MEDIUM_STATUS_EOF;
195 goto end;
196 }
197
198 status = ds_file_mmap(ds_file,
199 ds_file->mmap_offset_in_file + ds_file->mmap_len);
200
e98a2d6e 201end:
3baf7da0 202 return status;
e98a2d6e
PP
203}
204
5b29e799 205static
5d452e1f 206enum ctf_msg_iter_medium_status medop_request_bytes(
e98a2d6e
PP
207 size_t request_sz, uint8_t **buffer_addr,
208 size_t *buffer_sz, void *data)
209{
5d452e1f
SM
210 enum ctf_msg_iter_medium_status status =
211 CTF_MSG_ITER_MEDIUM_STATUS_OK;
94cf822e 212 struct ctf_fs_ds_file *ds_file = data;
d7bc0973
SM
213 bt_self_component *self_comp = ds_file->self_comp;
214 bt_logging_level log_level = ds_file->log_level;
e98a2d6e 215
640a46fd 216 BT_ASSERT(request_sz > 0);
e98a2d6e 217
f102bfaa
FD
218 /*
219 * Check if we have at least one memory-mapped byte left. If we don't,
220 * mmap the next file.
221 */
94cf822e 222 if (remaining_mmap_bytes(ds_file) == 0) {
e98a2d6e 223 /* Are we at the end of the file? */
43b07461 224 if (ds_file->mmap_offset_in_file >= ds_file->file->size) {
f67894f9 225 BT_COMP_LOGD("Reached end of file \"%s\" (%p)",
94cf822e 226 ds_file->file->path->str, ds_file->file->fp);
5d452e1f 227 status = CTF_MSG_ITER_MEDIUM_STATUS_EOF;
e98a2d6e
PP
228 goto end;
229 }
230
94cf822e 231 status = ds_file_mmap_next(ds_file);
e0f6a64a 232 switch (status) {
5d452e1f 233 case CTF_MSG_ITER_MEDIUM_STATUS_OK:
e0f6a64a 234 break;
5d452e1f 235 case CTF_MSG_ITER_MEDIUM_STATUS_EOF:
e0f6a64a
JG
236 goto end;
237 default:
f67894f9 238 BT_COMP_LOGE("Cannot memory-map next region of file \"%s\" (%p)",
94cf822e
PP
239 ds_file->file->path->str,
240 ds_file->file->fp);
e98a2d6e
PP
241 goto error;
242 }
243 }
244
640a46fd 245 BT_ASSERT(remaining_mmap_bytes(ds_file) > 0);
94cf822e 246 *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz);
640a46fd 247
f102bfaa 248 BT_ASSERT(ds_file->mmap_addr);
43b07461 249 *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset_in_mapping;
640a46fd 250
43b07461 251 ds_file->request_offset_in_mapping += *buffer_sz;
e98a2d6e
PP
252 goto end;
253
254error:
5d452e1f 255 status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
e98a2d6e
PP
256
257end:
258 return status;
259}
260
5b29e799 261static
bbbd35bf 262bt_stream *medop_borrow_stream(bt_stream_class *stream_class, int64_t stream_id,
0659c536 263 void *data)
e98a2d6e 264{
94cf822e 265 struct ctf_fs_ds_file *ds_file = data;
8eee8ea2
PP
266 bt_stream_class *ds_file_stream_class;
267 bt_stream *stream = NULL;
9e550e5f 268
78cf9df6 269 ds_file_stream_class = bt_stream_borrow_class(
9e550e5f 270 ds_file->stream);
94cf822e 271
94cf822e
PP
272 if (stream_class != ds_file_stream_class) {
273 /*
274 * Not supported: two packets described by two different
275 * stream classes within the same data stream file.
276 */
277 goto end;
e98a2d6e
PP
278 }
279
94cf822e
PP
280 stream = ds_file->stream;
281
282end:
283 return stream;
e98a2d6e
PP
284}
285
bb230c9b 286static
9a4c10a7 287enum ctf_msg_iter_medium_status medop_seek(off_t offset, void *data)
bb230c9b 288{
bb230c9b 289 struct ctf_fs_ds_file *ds_file = data;
bb230c9b 290
ebe4c684 291 BT_ASSERT(offset >= 0);
0eb6c8ee 292 BT_ASSERT(offset < ds_file->file->size);
bb230c9b 293
0eb6c8ee 294 return ds_file_mmap(ds_file, offset);
bb230c9b
JG
295}
296
55fa6491 297BT_HIDDEN
5d452e1f 298struct ctf_msg_iter_medium_ops ctf_fs_ds_file_medops = {
e98a2d6e 299 .request_bytes = medop_request_bytes,
a6918753 300 .borrow_stream = medop_borrow_stream,
bb230c9b 301 .seek = medop_seek,
e98a2d6e 302};
55fa6491 303
408c8c19
SM
304static
305struct ctf_fs_ds_index_entry *ctf_fs_ds_index_entry_create(
306 bt_self_component *self_comp, bt_logging_level log_level)
307{
308 struct ctf_fs_ds_index_entry *entry;
309
310 entry = g_new0(struct ctf_fs_ds_index_entry, 1);
311 if (!entry) {
312 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to allocate a ctf_fs_ds_index_entry.");
313 goto end;
314 }
315
316 entry->packet_seq_num = UINT64_MAX;
317
318end:
319 return entry;
320}
321
97ade20b 322static
35fa110e 323int convert_cycles_to_ns(struct ctf_clock_class *clock_class,
97ade20b 324 uint64_t cycles, int64_t *ns)
b6c3dcb2 325{
35fa110e
PP
326 return bt_util_clock_cycles_to_ns_from_origin(cycles,
327 clock_class->frequency, clock_class->offset_seconds,
328 clock_class->offset_cycles, ns);
97ade20b
JG
329}
330
331static
332struct ctf_fs_ds_index *build_index_from_idx_file(
13772304 333 struct ctf_fs_ds_file *ds_file,
3b89958b
SM
334 struct ctf_fs_ds_file_info *file_info,
335 struct ctf_msg_iter *msg_iter)
97ade20b
JG
336{
337 int ret;
b6c3dcb2
JG
338 gchar *directory = NULL;
339 gchar *basename = NULL;
340 GString *index_basename = NULL;
341 gchar *index_file_path = NULL;
342 GMappedFile *mapped_file = NULL;
343 gsize filesize;
97ade20b
JG
344 const char *mmap_begin = NULL, *file_pos = NULL;
345 const struct ctf_packet_index_file_hdr *header = NULL;
346 struct ctf_fs_ds_index *index = NULL;
e58dfd9c 347 struct ctf_fs_ds_index_entry *index_entry = NULL, *prev_index_entry = NULL;
b6c3dcb2
JG
348 uint64_t total_packets_size = 0;
349 size_t file_index_entry_size;
350 size_t file_entry_count;
351 size_t i;
7b33a0e0 352 struct ctf_stream_class *sc;
5d452e1f 353 struct ctf_msg_iter_packet_properties props;
2068d50b 354 uint32_t version_major, version_minor;
d7bc0973
SM
355 bt_self_component *self_comp = ds_file->self_comp;
356 bt_logging_level log_level = ds_file->log_level;
97ade20b 357
f67894f9 358 BT_COMP_LOGI("Building index from .idx file of stream file %s",
97ade20b 359 ds_file->file->path->str);
3b89958b 360 ret = ctf_msg_iter_get_packet_properties(msg_iter, &props);
97ade20b 361 if (ret) {
f67894f9 362 BT_COMP_LOGI_STR("Cannot read first packet's header and context fields.");
7b33a0e0
PP
363 goto error;
364 }
365
7b33a0e0
PP
366 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
367 props.stream_class_id);
368 BT_ASSERT(sc);
369 if (!sc->default_clock_class) {
f67894f9 370 BT_COMP_LOGI_STR("Cannot find stream class's default clock class.");
97ade20b
JG
371 goto error;
372 }
b6c3dcb2
JG
373
374 /* Look for index file in relative path index/name.idx. */
94cf822e 375 basename = g_path_get_basename(ds_file->file->path->str);
b6c3dcb2 376 if (!basename) {
f67894f9 377 BT_COMP_LOGE("Cannot get the basename of datastream file %s",
55314f2a 378 ds_file->file->path->str);
97ade20b 379 goto error;
b6c3dcb2
JG
380 }
381
94cf822e 382 directory = g_path_get_dirname(ds_file->file->path->str);
b6c3dcb2 383 if (!directory) {
f67894f9 384 BT_COMP_LOGE("Cannot get dirname of datastream file %s",
55314f2a 385 ds_file->file->path->str);
97ade20b 386 goto error;
b6c3dcb2
JG
387 }
388
389 index_basename = g_string_new(basename);
390 if (!index_basename) {
f67894f9 391 BT_COMP_LOGE_STR("Cannot allocate index file basename string");
97ade20b 392 goto error;
b6c3dcb2
JG
393 }
394
395 g_string_append(index_basename, ".idx");
396 index_file_path = g_build_filename(directory, "index",
397 index_basename->str, NULL);
398 mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL);
06367fa3 399 if (!mapped_file) {
f67894f9 400 BT_COMP_LOGD("Cannot create new mapped file %s",
55314f2a 401 index_file_path);
97ade20b 402 goto error;
06367fa3 403 }
65cbebef
JG
404
405 /*
406 * The g_mapped_file API limits us to 4GB files on 32-bit.
407 * Traces with such large indexes have never been seen in the wild,
408 * but this would need to be adjusted to support them.
409 */
b6c3dcb2
JG
410 filesize = g_mapped_file_get_length(mapped_file);
411 if (filesize < sizeof(*header)) {
f67894f9 412 BT_COMP_LOGW("Invalid LTTng trace index file: "
65cbebef
JG
413 "file size (%zu bytes) < header size (%zu bytes)",
414 filesize, sizeof(*header));
97ade20b 415 goto error;
b6c3dcb2
JG
416 }
417
418 mmap_begin = g_mapped_file_get_contents(mapped_file);
419 header = (struct ctf_packet_index_file_hdr *) mmap_begin;
420
421 file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header);
422 if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
f67894f9 423 BT_COMP_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
97ade20b 424 goto error;
b6c3dcb2 425 }
b6c3dcb2 426
2068d50b
SM
427 version_major = be32toh(header->index_major);
428 version_minor = be32toh(header->index_minor);
429 if (version_major != 1) {
430 BT_COMP_LOGW(
431 "Unknown LTTng trace index version: "
432 "major=%" PRIu32 ", minor=%" PRIu32,
433 version_major, version_minor);
434 goto error;
435 }
436
b6c3dcb2
JG
437 file_index_entry_size = be32toh(header->packet_index_len);
438 file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
97ade20b 439 if ((filesize - sizeof(*header)) % file_index_entry_size) {
f67894f9 440 BT_COMP_LOGW("Invalid LTTng trace index: the index's size after the header "
65cbebef
JG
441 "(%zu bytes) is not a multiple of the index entry size "
442 "(%zu bytes)", (filesize - sizeof(*header)),
443 sizeof(*header));
97ade20b 444 goto error;
b6c3dcb2
JG
445 }
446
f67894f9 447 index = ctf_fs_ds_index_create(ds_file->log_level, ds_file->self_comp);
97ade20b
JG
448 if (!index) {
449 goto error;
b6c3dcb2 450 }
97ade20b 451
b6c3dcb2
JG
452 for (i = 0; i < file_entry_count; i++) {
453 struct ctf_packet_index *file_index =
454 (struct ctf_packet_index *) file_pos;
455 uint64_t packet_size = be64toh(file_index->packet_size);
456
457 if (packet_size % CHAR_BIT) {
f67894f9 458 BT_COMP_LOGW("Invalid packet size encountered in LTTng trace index file");
97ade20b 459 goto error;
b6c3dcb2
JG
460 }
461
408c8c19
SM
462 index_entry = ctf_fs_ds_index_entry_create(
463 ds_file->self_comp, ds_file->log_level);
779f4a23 464 if (!index_entry) {
408c8c19
SM
465 BT_COMP_LOGE_APPEND_CAUSE(ds_file->self_comp,
466 "Failed to create a ctf_fs_ds_index_entry.");
779f4a23
FD
467 goto error;
468 }
469
13772304
FD
470 /* Set path to stream file. */
471 index_entry->path = file_info->path->str;
472
b6c3dcb2
JG
473 /* Convert size in bits to bytes. */
474 packet_size /= CHAR_BIT;
97ade20b 475 index_entry->packet_size = packet_size;
b6c3dcb2 476
97ade20b 477 index_entry->offset = be64toh(file_index->offset);
e58dfd9c 478 if (i != 0 && index_entry->offset < prev_index_entry->offset) {
f67894f9 479 BT_COMP_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
65cbebef 480 "previous offset=%" PRIu64 ", current offset=%" PRIu64,
6cef4a7d 481 prev_index_entry->offset, index_entry->offset);
97ade20b 482 goto error;
b6c3dcb2
JG
483 }
484
97ade20b
JG
485 index_entry->timestamp_begin = be64toh(file_index->timestamp_begin);
486 index_entry->timestamp_end = be64toh(file_index->timestamp_end);
487 if (index_entry->timestamp_end < index_entry->timestamp_begin) {
f67894f9 488 BT_COMP_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
65cbebef
JG
489 "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64,
490 index_entry->timestamp_begin,
491 index_entry->timestamp_end);
97ade20b
JG
492 goto error;
493 }
494
495 /* Convert the packet's bound to nanoseconds since Epoch. */
7b33a0e0 496 ret = convert_cycles_to_ns(sc->default_clock_class,
97ade20b
JG
497 index_entry->timestamp_begin,
498 &index_entry->timestamp_begin_ns);
499 if (ret) {
f67894f9 500 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
97ade20b
JG
501 goto error;
502 }
7b33a0e0 503 ret = convert_cycles_to_ns(sc->default_clock_class,
97ade20b
JG
504 index_entry->timestamp_end,
505 &index_entry->timestamp_end_ns);
506 if (ret) {
f67894f9 507 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
97ade20b 508 goto error;
b6c3dcb2
JG
509 }
510
408c8c19
SM
511 if (version_minor >= 1) {
512 index_entry->packet_seq_num = be64toh(file_index->packet_seq_num);
513 }
514
b6c3dcb2
JG
515 total_packets_size += packet_size;
516 file_pos += file_index_entry_size;
779f4a23 517
e58dfd9c 518 prev_index_entry = index_entry;
c0ba90e9
SM
519
520 /* Give ownership of `index_entry` to `index->entries`. */
521 g_ptr_array_add(index->entries, index_entry);
522 index_entry = NULL;
b6c3dcb2
JG
523 }
524
525 /* Validate that the index addresses the complete stream. */
94cf822e 526 if (ds_file->file->size != total_packets_size) {
f67894f9 527 BT_COMP_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
bb230c9b 528 "file-size=%" PRIu64 ", total-packets-size=%" PRIu64,
65cbebef 529 ds_file->file->size, total_packets_size);
97ade20b 530 goto error;
b6c3dcb2
JG
531 }
532end:
533 g_free(directory);
534 g_free(basename);
535 g_free(index_file_path);
06367fa3
JG
536 if (index_basename) {
537 g_string_free(index_basename, TRUE);
538 }
539 if (mapped_file) {
540 g_mapped_file_unref(mapped_file);
541 }
97ade20b
JG
542 return index;
543error:
544 ctf_fs_ds_index_destroy(index);
779f4a23 545 g_free(index_entry);
97ade20b 546 index = NULL;
b6c3dcb2
JG
547 goto end;
548}
549
bb230c9b
JG
550static
551int init_index_entry(struct ctf_fs_ds_index_entry *entry,
7b33a0e0 552 struct ctf_fs_ds_file *ds_file,
5d452e1f 553 struct ctf_msg_iter_packet_properties *props,
7b33a0e0 554 off_t packet_size, off_t packet_offset)
bb230c9b 555{
414ee0f4 556 int ret = 0;
7b33a0e0 557 struct ctf_stream_class *sc;
d7bc0973
SM
558 bt_self_component *self_comp = ds_file->self_comp;
559 bt_logging_level log_level = ds_file->log_level;
bb230c9b 560
7b33a0e0
PP
561 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
562 props->stream_class_id);
563 BT_ASSERT(sc);
8b45963b 564 BT_ASSERT(packet_offset >= 0);
bb230c9b 565 entry->offset = packet_offset;
8b45963b 566 BT_ASSERT(packet_size >= 0);
bb230c9b
JG
567 entry->packet_size = packet_size;
568
0476f6f7 569 if (props->snapshots.beginning_clock != UINT64_C(-1)) {
c2f37699
FD
570 entry->timestamp_begin = props->snapshots.beginning_clock;
571
0476f6f7
PP
572 /* Convert the packet's bound to nanoseconds since Epoch. */
573 ret = convert_cycles_to_ns(sc->default_clock_class,
574 props->snapshots.beginning_clock,
575 &entry->timestamp_begin_ns);
576 if (ret) {
f67894f9 577 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
0476f6f7
PP
578 goto end;
579 }
580 } else {
c2f37699 581 entry->timestamp_begin = UINT64_C(-1);
0476f6f7 582 entry->timestamp_begin_ns = UINT64_C(-1);
bb230c9b
JG
583 }
584
0476f6f7 585 if (props->snapshots.end_clock != UINT64_C(-1)) {
c2f37699
FD
586 entry->timestamp_end = props->snapshots.end_clock;
587
588 /* Convert the packet's bound to nanoseconds since Epoch. */
0476f6f7
PP
589 ret = convert_cycles_to_ns(sc->default_clock_class,
590 props->snapshots.end_clock,
591 &entry->timestamp_end_ns);
592 if (ret) {
f67894f9 593 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
0476f6f7
PP
594 goto end;
595 }
596 } else {
c2f37699 597 entry->timestamp_end = UINT64_C(-1);
0476f6f7 598 entry->timestamp_end_ns = UINT64_C(-1);
bb230c9b 599 }
c43f6ee2 600
bb230c9b 601end:
bb230c9b
JG
602 return ret;
603}
604
605static
606struct ctf_fs_ds_index *build_index_from_stream_file(
13772304 607 struct ctf_fs_ds_file *ds_file,
3b89958b
SM
608 struct ctf_fs_ds_file_info *file_info,
609 struct ctf_msg_iter *msg_iter)
bb230c9b
JG
610{
611 int ret;
612 struct ctf_fs_ds_index *index = NULL;
5d452e1f 613 enum ctf_msg_iter_status iter_status = CTF_MSG_ITER_STATUS_OK;
bbbd35bf 614 off_t current_packet_offset_bytes = 0;
d7bc0973
SM
615 bt_self_component *self_comp = ds_file->self_comp;
616 bt_logging_level log_level = ds_file->log_level;
bb230c9b 617
f67894f9 618 BT_COMP_LOGI("Indexing stream file %s", ds_file->file->path->str);
bb230c9b 619
f67894f9 620 index = ctf_fs_ds_index_create(ds_file->log_level, ds_file->self_comp);
bb230c9b
JG
621 if (!index) {
622 goto error;
623 }
624
5eb3f5e6 625 while (true) {
7b33a0e0 626 off_t current_packet_size_bytes;
779f4a23 627 struct ctf_fs_ds_index_entry *index_entry;
5d452e1f 628 struct ctf_msg_iter_packet_properties props;
bb230c9b 629
bbbd35bf 630 if (current_packet_offset_bytes < 0) {
f67894f9 631 BT_COMP_LOGE_STR("Cannot get the current packet's offset.");
bbbd35bf
PP
632 goto error;
633 } else if (current_packet_offset_bytes > ds_file->file->size) {
f67894f9 634 BT_COMP_LOGE_STR("Unexpected current packet's offset (larger than file).");
bbbd35bf
PP
635 goto error;
636 } else if (current_packet_offset_bytes == ds_file->file->size) {
637 /* No more data */
638 break;
639 }
640
3b89958b 641 iter_status = ctf_msg_iter_seek(msg_iter,
bbbd35bf 642 current_packet_offset_bytes);
5d452e1f 643 if (iter_status != CTF_MSG_ITER_STATUS_OK) {
bb230c9b
JG
644 goto error;
645 }
a6918753 646
5d452e1f 647 iter_status = ctf_msg_iter_get_packet_properties(
3b89958b 648 msg_iter, &props);
5d452e1f 649 if (iter_status != CTF_MSG_ITER_STATUS_OK) {
bb230c9b
JG
650 goto error;
651 }
652
bbbd35bf
PP
653 if (props.exp_packet_total_size >= 0) {
654 current_packet_size_bytes =
655 (uint64_t) props.exp_packet_total_size / 8;
656 } else {
657 current_packet_size_bytes = ds_file->file->size;
658 }
bb230c9b 659
bbbd35bf 660 if (current_packet_offset_bytes + current_packet_size_bytes >
bb230c9b 661 ds_file->file->size) {
f67894f9 662 BT_COMP_LOGW("Invalid packet size reported in file: stream=\"%s\", "
bb230c9b
JG
663 "packet-offset=%jd, packet-size-bytes=%jd, "
664 "file-size=%jd",
665 ds_file->file->path->str,
2655783b
JR
666 (intmax_t) current_packet_offset_bytes,
667 (intmax_t) current_packet_size_bytes,
668 (intmax_t) ds_file->file->size);
bb230c9b
JG
669 goto error;
670 }
671
408c8c19
SM
672 index_entry = ctf_fs_ds_index_entry_create(
673 ds_file->self_comp, ds_file->log_level);
779f4a23 674 if (!index_entry) {
408c8c19
SM
675 BT_COMP_LOGE_APPEND_CAUSE(ds_file->self_comp,
676 "Failed to create a ctf_fs_ds_index_entry.");
bb230c9b
JG
677 goto error;
678 }
679
13772304
FD
680 /* Set path to stream file. */
681 index_entry->path = file_info->path->str;
682
779f4a23 683 ret = init_index_entry(index_entry, ds_file, &props,
bbbd35bf 684 current_packet_size_bytes, current_packet_offset_bytes);
bb230c9b 685 if (ret) {
779f4a23 686 g_free(index_entry);
bb230c9b
JG
687 goto error;
688 }
779f4a23
FD
689
690 g_ptr_array_add(index->entries, index_entry);
691
9502dc00 692 current_packet_offset_bytes += current_packet_size_bytes;
f67894f9 693 BT_COMP_LOGD("Seeking to next packet: current-packet-offset=%jd, "
9502dc00 694 "next-packet-offset=%jd",
2655783b
JR
695 (intmax_t) (current_packet_offset_bytes - current_packet_size_bytes),
696 (intmax_t) current_packet_offset_bytes);
bb230c9b 697 }
a6918753 698
bb230c9b 699end:
bb230c9b 700 return index;
a6918753 701
bb230c9b
JG
702error:
703 ctf_fs_ds_index_destroy(index);
704 index = NULL;
705 goto end;
706}
707
e7a4393b 708BT_HIDDEN
94cf822e
PP
709struct ctf_fs_ds_file *ctf_fs_ds_file_create(
710 struct ctf_fs_trace *ctf_fs_trace,
c7d8bfa1 711 bt_self_message_iterator *self_msg_iter,
3d731ea9
PP
712 bt_stream *stream, const char *path,
713 bt_logging_level log_level)
e98a2d6e 714{
b6c3dcb2 715 int ret;
c8ebf034 716 const size_t offset_align = bt_mmap_get_offset_align_size(log_level);
94cf822e 717 struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1);
e98a2d6e 718
94cf822e 719 if (!ds_file) {
e98a2d6e
PP
720 goto error;
721 }
722
3d731ea9 723 ds_file->log_level = log_level;
f67894f9 724 ds_file->self_comp = ctf_fs_trace->self_comp;
c7d8bfa1 725 ds_file->self_msg_iter = self_msg_iter;
f67894f9 726 ds_file->file = ctf_fs_file_create(log_level, ds_file->self_comp);
94cf822e 727 if (!ds_file->file) {
4f1f88a6
PP
728 goto error;
729 }
730
4b70020d 731 ds_file->stream = stream;
8c6884d9 732 bt_stream_get_ref(ds_file->stream);
7b33a0e0 733 ds_file->metadata = ctf_fs_trace->metadata;
94cf822e 734 g_string_assign(ds_file->file->path, path);
55314f2a 735 ret = ctf_fs_file_open(ds_file->file, "rb");
4f1f88a6
PP
736 if (ret) {
737 goto error;
738 }
739
c8ebf034 740 ds_file->mmap_max_len = offset_align * 2048;
94cf822e 741
e98a2d6e 742 goto end;
1a9f7075 743
e98a2d6e 744error:
78586d8a 745 /* Do not touch "borrowed" file. */
94cf822e
PP
746 ctf_fs_ds_file_destroy(ds_file);
747 ds_file = NULL;
1a9f7075 748
e98a2d6e 749end:
94cf822e 750 return ds_file;
e98a2d6e
PP
751}
752
97ade20b
JG
753BT_HIDDEN
754struct ctf_fs_ds_index *ctf_fs_ds_file_build_index(
13772304 755 struct ctf_fs_ds_file *ds_file,
3b89958b
SM
756 struct ctf_fs_ds_file_info *file_info,
757 struct ctf_msg_iter *msg_iter)
97ade20b 758{
bb230c9b 759 struct ctf_fs_ds_index *index;
d7bc0973
SM
760 bt_self_component *self_comp = ds_file->self_comp;
761 bt_logging_level log_level = ds_file->log_level;
bb230c9b 762
3b89958b 763 index = build_index_from_idx_file(ds_file, file_info, msg_iter);
bb230c9b
JG
764 if (index) {
765 goto end;
766 }
767
f67894f9 768 BT_COMP_LOGI("Failed to build index from .index file; "
bb230c9b 769 "falling back to stream indexing.");
3b89958b 770 index = build_index_from_stream_file(ds_file, file_info, msg_iter);
bb230c9b
JG
771end:
772 return index;
97ade20b
JG
773}
774
779f4a23 775BT_HIDDEN
f67894f9
PP
776struct ctf_fs_ds_index *ctf_fs_ds_index_create(bt_logging_level log_level,
777 bt_self_component *self_comp)
779f4a23
FD
778{
779 struct ctf_fs_ds_index *index = g_new0(struct ctf_fs_ds_index, 1);
780
781 if (!index) {
f67894f9 782 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
3d731ea9 783 "Failed to allocate index");
779f4a23
FD
784 goto error;
785 }
786
787 index->entries = g_ptr_array_new_with_free_func((GDestroyNotify) g_free);
788 if (!index->entries) {
f67894f9 789 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
3d731ea9 790 "Failed to allocate index entries.");
779f4a23
FD
791 goto error;
792 }
793
794 goto end;
795
796error:
797 ctf_fs_ds_index_destroy(index);
798 index = NULL;
799end:
800 return index;
801}
802
5b29e799 803BT_HIDDEN
94cf822e 804void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file)
e98a2d6e 805{
94cf822e 806 if (!ds_file) {
5b29e799 807 return;
043e2020
JG
808 }
809
8c6884d9 810 bt_stream_put_ref(ds_file->stream);
94cf822e 811 (void) ds_file_munmap(ds_file);
0982a26d 812
94cf822e
PP
813 if (ds_file->file) {
814 ctf_fs_file_destroy(ds_file->file);
e98a2d6e
PP
815 }
816
94cf822e 817 g_free(ds_file);
e98a2d6e 818}
4f1f88a6 819
97ade20b
JG
820BT_HIDDEN
821void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index)
822{
823 if (!index) {
824 return;
825 }
826
827 if (index->entries) {
779f4a23 828 g_ptr_array_free(index->entries, TRUE);
97ade20b
JG
829 }
830 g_free(index);
831}
This page took 0.10892 seconds and 4 git commands to generate.