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