src.ctf.fs: make ds_file_munmap assert that ds_file is not NULL
[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
2e42d046
SM
25#define BT_COMP_LOG_SELF_COMP (self_comp)
26#define BT_LOG_OUTPUT_LEVEL (log_level)
98903a3e 27#define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/DS"
d9c39b0a 28#include "logging/comp-logging.h"
98903a3e 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>
578e048b
MJ
36#include "compat/mman.h"
37#include "compat/endian.h"
3fadfbc0 38#include <babeltrace2/babeltrace.h>
578e048b 39#include "common/common.h"
78586d8a
JG
40#include "file.h"
41#include "metadata.h"
d6e69534 42#include "../common/msg-iter/msg-iter.h"
578e048b 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{
e9bfbfe0
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
5b29e799 54static
94cf822e 55int ds_file_munmap(struct ctf_fs_ds_file *ds_file)
e98a2d6e 56{
fc9a526c 57 int ret = 0;
2e42d046
SM
58 bt_self_component *self_comp = ds_file->self_comp;
59 bt_logging_level log_level = ds_file->log_level;
e98a2d6e 60
9f5571db
SM
61 BT_ASSERT(ds_file);
62
63 if (!ds_file->mmap_addr) {
94cf822e
PP
64 goto end;
65 }
66
04394229 67 if (bt_munmap(ds_file->mmap_addr, ds_file->mmap_len)) {
4c65a157 68 BT_COMP_LOGE_ERRNO("Cannot memory-unmap file",
cd232764 69 ": address=%p, size=%zu, file_path=\"%s\", file=%p",
94cf822e 70 ds_file->mmap_addr, ds_file->mmap_len,
9e0c8dbb 71 ds_file->file ? ds_file->file->path->str : "NULL",
cd232764 72 ds_file->file ? ds_file->file->fp : NULL);
fc9a526c
JG
73 ret = -1;
74 goto end;
e98a2d6e 75 }
94cf822e
PP
76
77 ds_file->mmap_addr = NULL;
78
fc9a526c
JG
79end:
80 return ret;
e98a2d6e
PP
81}
82
5b29e799 83static
18a1979b 84enum ctf_msg_iter_medium_status ds_file_mmap_next(
94cf822e 85 struct ctf_fs_ds_file *ds_file)
e98a2d6e 86{
18a1979b
SM
87 enum ctf_msg_iter_medium_status ret =
88 CTF_MSG_ITER_MEDIUM_STATUS_OK;
2e42d046
SM
89 bt_self_component *self_comp = ds_file->self_comp;
90 bt_logging_level log_level = ds_file->log_level;
e98a2d6e
PP
91
92 /* Unmap old region */
94cf822e
PP
93 if (ds_file->mmap_addr) {
94 if (ds_file_munmap(ds_file)) {
e98a2d6e
PP
95 goto error;
96 }
97
2f5a009b 98 /*
2c701ca6 99 * mmap_len is guaranteed to be page-aligned except on the
2f5a009b
JG
100 * last mapping where it may not be possible (since the file's
101 * size itself may not be a page multiple).
102 */
e9bfbfe0
SM
103 ds_file->mmap_offset_in_file += ds_file->mmap_len;
104 ds_file->request_offset_in_mapping = 0;
e98a2d6e
PP
105 }
106
e9bfbfe0 107 ds_file->mmap_len = MIN(ds_file->file->size - ds_file->mmap_offset_in_file,
94cf822e 108 ds_file->mmap_max_len);
2c701ca6 109 if (ds_file->mmap_len == 0) {
18a1979b 110 ret = CTF_MSG_ITER_MEDIUM_STATUS_EOF;
e0f6a64a
JG
111 goto end;
112 }
e98a2d6e 113 /* Map new region */
f6ccaed9 114 BT_ASSERT(ds_file->mmap_len);
04394229 115 ds_file->mmap_addr = bt_mmap((void *) 0, ds_file->mmap_len,
94cf822e 116 PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp),
e9bfbfe0 117 ds_file->mmap_offset_in_file, ds_file->log_level);
94cf822e 118 if (ds_file->mmap_addr == MAP_FAILED) {
4c65a157 119 BT_COMP_LOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s",
94cf822e 120 ds_file->mmap_len, ds_file->file->path->str,
e9bfbfe0 121 ds_file->file->fp, (intmax_t) ds_file->mmap_offset_in_file,
78586d8a 122 strerror(errno));
e98a2d6e
PP
123 goto error;
124 }
125
126 goto end;
e98a2d6e 127error:
94cf822e 128 ds_file_munmap(ds_file);
18a1979b 129 ret = CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
e98a2d6e
PP
130end:
131 return ret;
132}
133
5b29e799 134static
18a1979b 135enum ctf_msg_iter_medium_status medop_request_bytes(
e98a2d6e
PP
136 size_t request_sz, uint8_t **buffer_addr,
137 size_t *buffer_sz, void *data)
138{
18a1979b
SM
139 enum ctf_msg_iter_medium_status status =
140 CTF_MSG_ITER_MEDIUM_STATUS_OK;
94cf822e 141 struct ctf_fs_ds_file *ds_file = data;
2e42d046
SM
142 bt_self_component *self_comp = ds_file->self_comp;
143 bt_logging_level log_level = ds_file->log_level;
e98a2d6e
PP
144
145 if (request_sz == 0) {
146 goto end;
147 }
148
de2abea4
FD
149 /*
150 * Check if we have at least one memory-mapped byte left. If we don't,
151 * mmap the next file.
152 */
94cf822e 153 if (remaining_mmap_bytes(ds_file) == 0) {
e98a2d6e 154 /* Are we at the end of the file? */
e9bfbfe0 155 if (ds_file->mmap_offset_in_file >= ds_file->file->size) {
4c65a157 156 BT_COMP_LOGD("Reached end of file \"%s\" (%p)",
94cf822e 157 ds_file->file->path->str, ds_file->file->fp);
18a1979b 158 status = CTF_MSG_ITER_MEDIUM_STATUS_EOF;
e98a2d6e
PP
159 goto end;
160 }
161
94cf822e 162 status = ds_file_mmap_next(ds_file);
e0f6a64a 163 switch (status) {
18a1979b 164 case CTF_MSG_ITER_MEDIUM_STATUS_OK:
e0f6a64a 165 break;
18a1979b 166 case CTF_MSG_ITER_MEDIUM_STATUS_EOF:
e0f6a64a
JG
167 goto end;
168 default:
4c65a157 169 BT_COMP_LOGE("Cannot memory-map next region of file \"%s\" (%p)",
94cf822e
PP
170 ds_file->file->path->str,
171 ds_file->file->fp);
e98a2d6e
PP
172 goto error;
173 }
174 }
175
94cf822e 176 *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz);
de2abea4 177 BT_ASSERT(ds_file->mmap_addr);
e9bfbfe0
SM
178 *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset_in_mapping;
179 ds_file->request_offset_in_mapping += *buffer_sz;
e98a2d6e
PP
180 goto end;
181
182error:
18a1979b 183 status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
e98a2d6e
PP
184
185end:
186 return status;
187}
188
5b29e799 189static
fc917f65 190bt_stream *medop_borrow_stream(bt_stream_class *stream_class, int64_t stream_id,
b92735af 191 void *data)
e98a2d6e 192{
94cf822e 193 struct ctf_fs_ds_file *ds_file = data;
b19ff26f
PP
194 bt_stream_class *ds_file_stream_class;
195 bt_stream *stream = NULL;
e5be10ef 196
40f4ba76 197 ds_file_stream_class = bt_stream_borrow_class(
e5be10ef 198 ds_file->stream);
94cf822e 199
94cf822e
PP
200 if (stream_class != ds_file_stream_class) {
201 /*
202 * Not supported: two packets described by two different
203 * stream classes within the same data stream file.
204 */
205 goto end;
e98a2d6e
PP
206 }
207
94cf822e
PP
208 stream = ds_file->stream;
209
210end:
211 return stream;
e98a2d6e
PP
212}
213
9e0c8dbb 214static
18a1979b 215enum ctf_msg_iter_medium_status medop_seek(enum ctf_msg_iter_seek_whence whence,
fc917f65 216 off_t offset, void *data)
9e0c8dbb 217{
18a1979b
SM
218 enum ctf_msg_iter_medium_status ret =
219 CTF_MSG_ITER_MEDIUM_STATUS_OK;
9e0c8dbb 220 struct ctf_fs_ds_file *ds_file = data;
de2abea4 221 off_t offset_in_mapping, file_size = ds_file->file->size;
2e42d046
SM
222 bt_self_component *self_comp = ds_file->self_comp;
223 bt_logging_level log_level = ds_file->log_level;
9e0c8dbb 224
18a1979b 225 if (whence != CTF_MSG_ITER_SEEK_WHENCE_SET ||
9e0c8dbb 226 offset < 0 || offset > file_size) {
4c65a157 227 BT_COMP_LOGE("Invalid medium seek request: whence=%d, offset=%jd, "
df0fc0bf
JR
228 "file-size=%jd", (int) whence, (intmax_t) offset,
229 (intmax_t) file_size);
18a1979b 230 ret = CTF_MSG_ITER_MEDIUM_STATUS_INVAL;
9e0c8dbb
JG
231 goto end;
232 }
233
de2abea4
FD
234 /* If there is no current mapping, map the right file directly. */
235 if (!ds_file->mmap_addr) {
236 goto map_requested_offset;
237 }
238
9e0c8dbb
JG
239 /*
240 * Determine whether or not the destination is contained within the
241 * current mapping.
242 */
e9bfbfe0
SM
243 if (offset < ds_file->mmap_offset_in_file ||
244 offset >= ds_file->mmap_offset_in_file + ds_file->mmap_len) {
9e0c8dbb 245 int unmap_ret;
4c65a157 246 BT_COMP_LOGD("Medium seek request cannot be accomodated by the current "
63489ca2 247 "file mapping: offset=%jd, mmap-offset=%jd, "
e9bfbfe0 248 "mmap-len=%zu", (intmax_t) offset, (intmax_t) ds_file->mmap_offset_in_file,
9e0c8dbb
JG
249 ds_file->mmap_len);
250 unmap_ret = ds_file_munmap(ds_file);
251 if (unmap_ret) {
18a1979b 252 ret = CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
9e0c8dbb
JG
253 goto end;
254 }
de2abea4 255 goto map_requested_offset;
9e0c8dbb 256 } else {
e9bfbfe0 257 ds_file->request_offset_in_mapping = offset - ds_file->mmap_offset_in_file;
de2abea4
FD
258 goto test_end;
259 }
260
261map_requested_offset:
262 offset_in_mapping = offset %
3b16a19b 263 bt_mmap_get_offset_align_size(ds_file->log_level);
de2abea4 264
e9bfbfe0
SM
265 ds_file->mmap_offset_in_file = offset - offset_in_mapping;
266 ds_file->request_offset_in_mapping = offset_in_mapping;
de2abea4 267 ret = ds_file_mmap_next(ds_file);
18a1979b 268 if (ret != CTF_MSG_ITER_MEDIUM_STATUS_OK) {
de2abea4 269 goto end;
9e0c8dbb
JG
270 }
271
de2abea4 272test_end:
9e0c8dbb
JG
273 ds_file->end_reached = (offset == file_size);
274end:
275 return ret;
276}
277
6de92955 278BT_HIDDEN
18a1979b 279struct ctf_msg_iter_medium_ops ctf_fs_ds_file_medops = {
e98a2d6e 280 .request_bytes = medop_request_bytes,
312c056a 281 .borrow_stream = medop_borrow_stream,
9e0c8dbb 282 .seek = medop_seek,
e98a2d6e 283};
6de92955 284
6834784d
SM
285static
286struct ctf_fs_ds_index_entry *ctf_fs_ds_index_entry_create(
287 bt_self_component *self_comp, bt_logging_level log_level)
288{
289 struct ctf_fs_ds_index_entry *entry;
290
291 entry = g_new0(struct ctf_fs_ds_index_entry, 1);
292 if (!entry) {
293 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to allocate a ctf_fs_ds_index_entry.");
294 goto end;
295 }
296
297 entry->packet_seq_num = UINT64_MAX;
298
299end:
300 return entry;
301}
302
97ade20b 303static
0f2d58c9 304int convert_cycles_to_ns(struct ctf_clock_class *clock_class,
97ade20b 305 uint64_t cycles, int64_t *ns)
b6c3dcb2 306{
0f2d58c9
PP
307 return bt_util_clock_cycles_to_ns_from_origin(cycles,
308 clock_class->frequency, clock_class->offset_seconds,
309 clock_class->offset_cycles, ns);
97ade20b
JG
310}
311
312static
313struct ctf_fs_ds_index *build_index_from_idx_file(
bf012bde 314 struct ctf_fs_ds_file *ds_file,
6d54260a
SM
315 struct ctf_fs_ds_file_info *file_info,
316 struct ctf_msg_iter *msg_iter)
97ade20b
JG
317{
318 int ret;
b6c3dcb2
JG
319 gchar *directory = NULL;
320 gchar *basename = NULL;
321 GString *index_basename = NULL;
322 gchar *index_file_path = NULL;
323 GMappedFile *mapped_file = NULL;
324 gsize filesize;
97ade20b
JG
325 const char *mmap_begin = NULL, *file_pos = NULL;
326 const struct ctf_packet_index_file_hdr *header = NULL;
327 struct ctf_fs_ds_index *index = NULL;
de38c26a 328 struct ctf_fs_ds_index_entry *index_entry = NULL, *prev_index_entry = NULL;
b6c3dcb2
JG
329 uint64_t total_packets_size = 0;
330 size_t file_index_entry_size;
331 size_t file_entry_count;
332 size_t i;
44c440bc 333 struct ctf_stream_class *sc;
18a1979b 334 struct ctf_msg_iter_packet_properties props;
1984ac2b 335 uint32_t version_major, version_minor;
2e42d046
SM
336 bt_self_component *self_comp = ds_file->self_comp;
337 bt_logging_level log_level = ds_file->log_level;
97ade20b 338
4c65a157 339 BT_COMP_LOGI("Building index from .idx file of stream file %s",
97ade20b 340 ds_file->file->path->str);
6d54260a 341 ret = ctf_msg_iter_get_packet_properties(msg_iter, &props);
97ade20b 342 if (ret) {
4c65a157 343 BT_COMP_LOGI_STR("Cannot read first packet's header and context fields.");
44c440bc
PP
344 goto error;
345 }
346
44c440bc
PP
347 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
348 props.stream_class_id);
349 BT_ASSERT(sc);
350 if (!sc->default_clock_class) {
4c65a157 351 BT_COMP_LOGI_STR("Cannot find stream class's default clock class.");
97ade20b
JG
352 goto error;
353 }
b6c3dcb2
JG
354
355 /* Look for index file in relative path index/name.idx. */
94cf822e 356 basename = g_path_get_basename(ds_file->file->path->str);
b6c3dcb2 357 if (!basename) {
4c65a157 358 BT_COMP_LOGE("Cannot get the basename of datastream file %s",
55314f2a 359 ds_file->file->path->str);
97ade20b 360 goto error;
b6c3dcb2
JG
361 }
362
94cf822e 363 directory = g_path_get_dirname(ds_file->file->path->str);
b6c3dcb2 364 if (!directory) {
4c65a157 365 BT_COMP_LOGE("Cannot get dirname of datastream file %s",
55314f2a 366 ds_file->file->path->str);
97ade20b 367 goto error;
b6c3dcb2
JG
368 }
369
370 index_basename = g_string_new(basename);
371 if (!index_basename) {
4c65a157 372 BT_COMP_LOGE_STR("Cannot allocate index file basename string");
97ade20b 373 goto error;
b6c3dcb2
JG
374 }
375
376 g_string_append(index_basename, ".idx");
377 index_file_path = g_build_filename(directory, "index",
378 index_basename->str, NULL);
379 mapped_file = g_mapped_file_new(index_file_path, FALSE, NULL);
06367fa3 380 if (!mapped_file) {
4c65a157 381 BT_COMP_LOGD("Cannot create new mapped file %s",
55314f2a 382 index_file_path);
97ade20b 383 goto error;
06367fa3 384 }
d14940a7
JG
385
386 /*
387 * The g_mapped_file API limits us to 4GB files on 32-bit.
388 * Traces with such large indexes have never been seen in the wild,
389 * but this would need to be adjusted to support them.
390 */
b6c3dcb2
JG
391 filesize = g_mapped_file_get_length(mapped_file);
392 if (filesize < sizeof(*header)) {
4c65a157 393 BT_COMP_LOGW("Invalid LTTng trace index file: "
d14940a7
JG
394 "file size (%zu bytes) < header size (%zu bytes)",
395 filesize, sizeof(*header));
97ade20b 396 goto error;
b6c3dcb2
JG
397 }
398
399 mmap_begin = g_mapped_file_get_contents(mapped_file);
400 header = (struct ctf_packet_index_file_hdr *) mmap_begin;
401
402 file_pos = g_mapped_file_get_contents(mapped_file) + sizeof(*header);
403 if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
4c65a157 404 BT_COMP_LOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
97ade20b 405 goto error;
b6c3dcb2 406 }
b6c3dcb2 407
1984ac2b
SM
408 version_major = be32toh(header->index_major);
409 version_minor = be32toh(header->index_minor);
410 if (version_major != 1) {
411 BT_COMP_LOGW(
412 "Unknown LTTng trace index version: "
413 "major=%" PRIu32 ", minor=%" PRIu32,
414 version_major, version_minor);
415 goto error;
416 }
417
b6c3dcb2
JG
418 file_index_entry_size = be32toh(header->packet_index_len);
419 file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
97ade20b 420 if ((filesize - sizeof(*header)) % file_index_entry_size) {
4c65a157 421 BT_COMP_LOGW("Invalid LTTng trace index: the index's size after the header "
d14940a7
JG
422 "(%zu bytes) is not a multiple of the index entry size "
423 "(%zu bytes)", (filesize - sizeof(*header)),
424 sizeof(*header));
97ade20b 425 goto error;
b6c3dcb2
JG
426 }
427
4c65a157 428 index = ctf_fs_ds_index_create(ds_file->log_level, ds_file->self_comp);
97ade20b
JG
429 if (!index) {
430 goto error;
b6c3dcb2 431 }
97ade20b 432
b6c3dcb2
JG
433 for (i = 0; i < file_entry_count; i++) {
434 struct ctf_packet_index *file_index =
435 (struct ctf_packet_index *) file_pos;
436 uint64_t packet_size = be64toh(file_index->packet_size);
437
438 if (packet_size % CHAR_BIT) {
4c65a157 439 BT_COMP_LOGW("Invalid packet size encountered in LTTng trace index file");
97ade20b 440 goto error;
b6c3dcb2
JG
441 }
442
6834784d
SM
443 index_entry = ctf_fs_ds_index_entry_create(
444 ds_file->self_comp, ds_file->log_level);
7ed5243a 445 if (!index_entry) {
6834784d
SM
446 BT_COMP_LOGE_APPEND_CAUSE(ds_file->self_comp,
447 "Failed to create a ctf_fs_ds_index_entry.");
7ed5243a
FD
448 goto error;
449 }
450
bf012bde
FD
451 /* Set path to stream file. */
452 index_entry->path = file_info->path->str;
453
b6c3dcb2
JG
454 /* Convert size in bits to bytes. */
455 packet_size /= CHAR_BIT;
97ade20b 456 index_entry->packet_size = packet_size;
b6c3dcb2 457
97ade20b 458 index_entry->offset = be64toh(file_index->offset);
de38c26a 459 if (i != 0 && index_entry->offset < prev_index_entry->offset) {
4c65a157 460 BT_COMP_LOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
d14940a7 461 "previous offset=%" PRIu64 ", current offset=%" PRIu64,
9dd33658 462 prev_index_entry->offset, index_entry->offset);
97ade20b 463 goto error;
b6c3dcb2
JG
464 }
465
97ade20b
JG
466 index_entry->timestamp_begin = be64toh(file_index->timestamp_begin);
467 index_entry->timestamp_end = be64toh(file_index->timestamp_end);
468 if (index_entry->timestamp_end < index_entry->timestamp_begin) {
4c65a157 469 BT_COMP_LOGW("Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
d14940a7
JG
470 "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64,
471 index_entry->timestamp_begin,
472 index_entry->timestamp_end);
97ade20b
JG
473 goto error;
474 }
475
476 /* Convert the packet's bound to nanoseconds since Epoch. */
44c440bc 477 ret = convert_cycles_to_ns(sc->default_clock_class,
97ade20b
JG
478 index_entry->timestamp_begin,
479 &index_entry->timestamp_begin_ns);
480 if (ret) {
4c65a157 481 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
97ade20b
JG
482 goto error;
483 }
44c440bc 484 ret = convert_cycles_to_ns(sc->default_clock_class,
97ade20b
JG
485 index_entry->timestamp_end,
486 &index_entry->timestamp_end_ns);
487 if (ret) {
4c65a157 488 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
97ade20b 489 goto error;
b6c3dcb2
JG
490 }
491
6834784d
SM
492 if (version_minor >= 1) {
493 index_entry->packet_seq_num = be64toh(file_index->packet_seq_num);
494 }
495
b6c3dcb2
JG
496 total_packets_size += packet_size;
497 file_pos += file_index_entry_size;
7ed5243a 498
de38c26a 499 prev_index_entry = index_entry;
f35a48bc
SM
500
501 /* Give ownership of `index_entry` to `index->entries`. */
502 g_ptr_array_add(index->entries, index_entry);
503 index_entry = NULL;
b6c3dcb2
JG
504 }
505
506 /* Validate that the index addresses the complete stream. */
94cf822e 507 if (ds_file->file->size != total_packets_size) {
4c65a157 508 BT_COMP_LOGW("Invalid LTTng trace index file; indexed size != stream file size: "
9e0c8dbb 509 "file-size=%" PRIu64 ", total-packets-size=%" PRIu64,
d14940a7 510 ds_file->file->size, total_packets_size);
97ade20b 511 goto error;
b6c3dcb2
JG
512 }
513end:
514 g_free(directory);
515 g_free(basename);
516 g_free(index_file_path);
06367fa3
JG
517 if (index_basename) {
518 g_string_free(index_basename, TRUE);
519 }
520 if (mapped_file) {
521 g_mapped_file_unref(mapped_file);
522 }
97ade20b
JG
523 return index;
524error:
525 ctf_fs_ds_index_destroy(index);
7ed5243a 526 g_free(index_entry);
97ade20b 527 index = NULL;
b6c3dcb2
JG
528 goto end;
529}
530
9e0c8dbb
JG
531static
532int init_index_entry(struct ctf_fs_ds_index_entry *entry,
44c440bc 533 struct ctf_fs_ds_file *ds_file,
18a1979b 534 struct ctf_msg_iter_packet_properties *props,
44c440bc 535 off_t packet_size, off_t packet_offset)
9e0c8dbb 536{
ca79a87c 537 int ret = 0;
44c440bc 538 struct ctf_stream_class *sc;
2e42d046
SM
539 bt_self_component *self_comp = ds_file->self_comp;
540 bt_logging_level log_level = ds_file->log_level;
9e0c8dbb 541
44c440bc
PP
542 sc = ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc,
543 props->stream_class_id);
544 BT_ASSERT(sc);
f6ccaed9 545 BT_ASSERT(packet_offset >= 0);
9e0c8dbb 546 entry->offset = packet_offset;
f6ccaed9 547 BT_ASSERT(packet_size >= 0);
9e0c8dbb
JG
548 entry->packet_size = packet_size;
549
83ebb7f1 550 if (props->snapshots.beginning_clock != UINT64_C(-1)) {
41bd46eb
FD
551 entry->timestamp_begin = props->snapshots.beginning_clock;
552
83ebb7f1
PP
553 /* Convert the packet's bound to nanoseconds since Epoch. */
554 ret = convert_cycles_to_ns(sc->default_clock_class,
555 props->snapshots.beginning_clock,
556 &entry->timestamp_begin_ns);
557 if (ret) {
4c65a157 558 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
83ebb7f1
PP
559 goto end;
560 }
561 } else {
41bd46eb 562 entry->timestamp_begin = UINT64_C(-1);
83ebb7f1 563 entry->timestamp_begin_ns = UINT64_C(-1);
9e0c8dbb
JG
564 }
565
83ebb7f1 566 if (props->snapshots.end_clock != UINT64_C(-1)) {
41bd46eb
FD
567 entry->timestamp_end = props->snapshots.end_clock;
568
569 /* Convert the packet's bound to nanoseconds since Epoch. */
83ebb7f1
PP
570 ret = convert_cycles_to_ns(sc->default_clock_class,
571 props->snapshots.end_clock,
572 &entry->timestamp_end_ns);
573 if (ret) {
4c65a157 574 BT_COMP_LOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
83ebb7f1
PP
575 goto end;
576 }
577 } else {
41bd46eb 578 entry->timestamp_end = UINT64_C(-1);
83ebb7f1 579 entry->timestamp_end_ns = UINT64_C(-1);
9e0c8dbb 580 }
0b29603d 581
9e0c8dbb 582end:
9e0c8dbb
JG
583 return ret;
584}
585
586static
587struct ctf_fs_ds_index *build_index_from_stream_file(
bf012bde 588 struct ctf_fs_ds_file *ds_file,
6d54260a
SM
589 struct ctf_fs_ds_file_info *file_info,
590 struct ctf_msg_iter *msg_iter)
9e0c8dbb
JG
591{
592 int ret;
593 struct ctf_fs_ds_index *index = NULL;
18a1979b 594 enum ctf_msg_iter_status iter_status = CTF_MSG_ITER_STATUS_OK;
fc917f65 595 off_t current_packet_offset_bytes = 0;
2e42d046
SM
596 bt_self_component *self_comp = ds_file->self_comp;
597 bt_logging_level log_level = ds_file->log_level;
9e0c8dbb 598
4c65a157 599 BT_COMP_LOGI("Indexing stream file %s", ds_file->file->path->str);
9e0c8dbb 600
4c65a157 601 index = ctf_fs_ds_index_create(ds_file->log_level, ds_file->self_comp);
9e0c8dbb
JG
602 if (!index) {
603 goto error;
604 }
605
2b601d0c 606 while (true) {
44c440bc 607 off_t current_packet_size_bytes;
7ed5243a 608 struct ctf_fs_ds_index_entry *index_entry;
18a1979b 609 struct ctf_msg_iter_packet_properties props;
9e0c8dbb 610
fc917f65 611 if (current_packet_offset_bytes < 0) {
4c65a157 612 BT_COMP_LOGE_STR("Cannot get the current packet's offset.");
fc917f65
PP
613 goto error;
614 } else if (current_packet_offset_bytes > ds_file->file->size) {
4c65a157 615 BT_COMP_LOGE_STR("Unexpected current packet's offset (larger than file).");
fc917f65
PP
616 goto error;
617 } else if (current_packet_offset_bytes == ds_file->file->size) {
618 /* No more data */
619 break;
620 }
621
6d54260a 622 iter_status = ctf_msg_iter_seek(msg_iter,
fc917f65 623 current_packet_offset_bytes);
18a1979b 624 if (iter_status != CTF_MSG_ITER_STATUS_OK) {
9e0c8dbb
JG
625 goto error;
626 }
312c056a 627
18a1979b 628 iter_status = ctf_msg_iter_get_packet_properties(
6d54260a 629 msg_iter, &props);
18a1979b 630 if (iter_status != CTF_MSG_ITER_STATUS_OK) {
9e0c8dbb
JG
631 goto error;
632 }
633
fc917f65
PP
634 if (props.exp_packet_total_size >= 0) {
635 current_packet_size_bytes =
636 (uint64_t) props.exp_packet_total_size / 8;
637 } else {
638 current_packet_size_bytes = ds_file->file->size;
639 }
9e0c8dbb 640
fc917f65 641 if (current_packet_offset_bytes + current_packet_size_bytes >
9e0c8dbb 642 ds_file->file->size) {
4c65a157 643 BT_COMP_LOGW("Invalid packet size reported in file: stream=\"%s\", "
9e0c8dbb
JG
644 "packet-offset=%jd, packet-size-bytes=%jd, "
645 "file-size=%jd",
646 ds_file->file->path->str,
df0fc0bf
JR
647 (intmax_t) current_packet_offset_bytes,
648 (intmax_t) current_packet_size_bytes,
649 (intmax_t) ds_file->file->size);
9e0c8dbb
JG
650 goto error;
651 }
652
6834784d
SM
653 index_entry = ctf_fs_ds_index_entry_create(
654 ds_file->self_comp, ds_file->log_level);
7ed5243a 655 if (!index_entry) {
6834784d
SM
656 BT_COMP_LOGE_APPEND_CAUSE(ds_file->self_comp,
657 "Failed to create a ctf_fs_ds_index_entry.");
9e0c8dbb
JG
658 goto error;
659 }
660
bf012bde
FD
661 /* Set path to stream file. */
662 index_entry->path = file_info->path->str;
663
7ed5243a 664 ret = init_index_entry(index_entry, ds_file, &props,
fc917f65 665 current_packet_size_bytes, current_packet_offset_bytes);
9e0c8dbb 666 if (ret) {
7ed5243a 667 g_free(index_entry);
9e0c8dbb
JG
668 goto error;
669 }
7ed5243a
FD
670
671 g_ptr_array_add(index->entries, index_entry);
672
ca633588 673 current_packet_offset_bytes += current_packet_size_bytes;
4c65a157 674 BT_COMP_LOGD("Seeking to next packet: current-packet-offset=%jd, "
ca633588 675 "next-packet-offset=%jd",
df0fc0bf
JR
676 (intmax_t) (current_packet_offset_bytes - current_packet_size_bytes),
677 (intmax_t) current_packet_offset_bytes);
9e0c8dbb 678 }
312c056a 679
9e0c8dbb 680end:
9e0c8dbb 681 return index;
312c056a 682
9e0c8dbb
JG
683error:
684 ctf_fs_ds_index_destroy(index);
685 index = NULL;
686 goto end;
687}
688
e7a4393b 689BT_HIDDEN
94cf822e
PP
690struct ctf_fs_ds_file *ctf_fs_ds_file_create(
691 struct ctf_fs_trace *ctf_fs_trace,
f5f7e8df 692 bt_self_message_iterator *self_msg_iter,
98903a3e
PP
693 bt_stream *stream, const char *path,
694 bt_logging_level log_level)
e98a2d6e 695{
b6c3dcb2 696 int ret;
3b16a19b 697 const size_t offset_align = bt_mmap_get_offset_align_size(log_level);
94cf822e 698 struct ctf_fs_ds_file *ds_file = g_new0(struct ctf_fs_ds_file, 1);
e98a2d6e 699
94cf822e 700 if (!ds_file) {
e98a2d6e
PP
701 goto error;
702 }
703
98903a3e 704 ds_file->log_level = log_level;
4c65a157 705 ds_file->self_comp = ctf_fs_trace->self_comp;
f5f7e8df 706 ds_file->self_msg_iter = self_msg_iter;
4c65a157 707 ds_file->file = ctf_fs_file_create(log_level, ds_file->self_comp);
94cf822e 708 if (!ds_file->file) {
4f1f88a6
PP
709 goto error;
710 }
711
398454ed 712 ds_file->stream = stream;
c5b9b441 713 bt_stream_get_ref(ds_file->stream);
44c440bc 714 ds_file->metadata = ctf_fs_trace->metadata;
94cf822e 715 g_string_assign(ds_file->file->path, path);
55314f2a 716 ret = ctf_fs_file_open(ds_file->file, "rb");
4f1f88a6
PP
717 if (ret) {
718 goto error;
719 }
720
3b16a19b 721 ds_file->mmap_max_len = offset_align * 2048;
94cf822e 722
e98a2d6e 723 goto end;
1a9f7075 724
e98a2d6e 725error:
78586d8a 726 /* Do not touch "borrowed" file. */
94cf822e
PP
727 ctf_fs_ds_file_destroy(ds_file);
728 ds_file = NULL;
1a9f7075 729
e98a2d6e 730end:
94cf822e 731 return ds_file;
e98a2d6e
PP
732}
733
97ade20b
JG
734BT_HIDDEN
735struct ctf_fs_ds_index *ctf_fs_ds_file_build_index(
bf012bde 736 struct ctf_fs_ds_file *ds_file,
6d54260a
SM
737 struct ctf_fs_ds_file_info *file_info,
738 struct ctf_msg_iter *msg_iter)
97ade20b 739{
9e0c8dbb 740 struct ctf_fs_ds_index *index;
2e42d046
SM
741 bt_self_component *self_comp = ds_file->self_comp;
742 bt_logging_level log_level = ds_file->log_level;
9e0c8dbb 743
6d54260a 744 index = build_index_from_idx_file(ds_file, file_info, msg_iter);
9e0c8dbb
JG
745 if (index) {
746 goto end;
747 }
748
4c65a157 749 BT_COMP_LOGI("Failed to build index from .index file; "
9e0c8dbb 750 "falling back to stream indexing.");
6d54260a 751 index = build_index_from_stream_file(ds_file, file_info, msg_iter);
9e0c8dbb
JG
752end:
753 return index;
97ade20b
JG
754}
755
7ed5243a 756BT_HIDDEN
4c65a157
PP
757struct ctf_fs_ds_index *ctf_fs_ds_index_create(bt_logging_level log_level,
758 bt_self_component *self_comp)
7ed5243a
FD
759{
760 struct ctf_fs_ds_index *index = g_new0(struct ctf_fs_ds_index, 1);
761
762 if (!index) {
4c65a157 763 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
98903a3e 764 "Failed to allocate index");
7ed5243a
FD
765 goto error;
766 }
767
768 index->entries = g_ptr_array_new_with_free_func((GDestroyNotify) g_free);
769 if (!index->entries) {
4c65a157 770 BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, self_comp,
98903a3e 771 "Failed to allocate index entries.");
7ed5243a
FD
772 goto error;
773 }
774
775 goto end;
776
777error:
778 ctf_fs_ds_index_destroy(index);
779 index = NULL;
780end:
781 return index;
782}
783
5b29e799 784BT_HIDDEN
94cf822e 785void ctf_fs_ds_file_destroy(struct ctf_fs_ds_file *ds_file)
e98a2d6e 786{
94cf822e 787 if (!ds_file) {
5b29e799 788 return;
043e2020
JG
789 }
790
c5b9b441 791 bt_stream_put_ref(ds_file->stream);
94cf822e 792 (void) ds_file_munmap(ds_file);
0982a26d 793
94cf822e
PP
794 if (ds_file->file) {
795 ctf_fs_file_destroy(ds_file->file);
e98a2d6e
PP
796 }
797
94cf822e 798 g_free(ds_file);
e98a2d6e 799}
4f1f88a6 800
97ade20b
JG
801BT_HIDDEN
802void ctf_fs_ds_index_destroy(struct ctf_fs_ds_index *index)
803{
804 if (!index) {
805 return;
806 }
807
808 if (index->entries) {
7ed5243a 809 g_ptr_array_free(index->entries, TRUE);
97ade20b
JG
810 }
811 g_free(index);
812}
This page took 0.114385 seconds and 4 git commands to generate.