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