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