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