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