src.ctf.fs: data-stream-file.cpp: declare variables on first use
[deliverable/babeltrace.git] / src / plugins / ctf / fs-src / data-stream-file.cpp
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2016-2017 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 */
8
9 #define BT_CLOG_CFG (logCfg)
10 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/DS"
11
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <stdint.h>
15 #include <stdlib.h>
16 #include <glib.h>
17 #include <inttypes.h>
18 #include "compat/mman.h"
19 #include "compat/endian.h"
20 #include <babeltrace2/babeltrace.h>
21 #include "common/common.h"
22 #include "file.hpp"
23 #include "metadata.hpp"
24 #include "../common/src/msg-iter/msg-iter.hpp"
25 #include "common/assert.h"
26 #include "data-stream-file.hpp"
27 #include <string.h>
28 #include "cpp-common/cfg-logging.hpp"
29 #include "cpp-common/cfg-logging-error-reporting.hpp"
30 #include "cpp-common/make-unique.hpp"
31 #include "fs.hpp"
32
33 static inline size_t remaining_mmap_bytes(struct ctf_fs_ds_file *ds_file)
34 {
35 BT_ASSERT_DBG(ds_file->mmap_len >= ds_file->request_offset_in_mapping);
36 return ds_file->mmap_len - ds_file->request_offset_in_mapping;
37 }
38
39 /*
40 * Return true if `offset_in_file` is in the current mapping.
41 */
42
43 static bool offset_ist_mapped(struct ctf_fs_ds_file *ds_file, off_t offset_in_file)
44 {
45 return offset_in_file >= ds_file->mmap_offset_in_file &&
46 offset_in_file < (ds_file->mmap_offset_in_file + ds_file->mmap_len);
47 }
48
49 static enum ctf_msg_iter_medium_status ds_file_munmap(struct ctf_fs_ds_file *ds_file)
50 {
51 const bt2_common::LogCfg& logCfg = ds_file->logCfg;
52
53 BT_ASSERT(ds_file);
54
55 if (!ds_file->mmap_addr) {
56 return CTF_MSG_ITER_MEDIUM_STATUS_OK;
57 }
58
59 if (bt_munmap(ds_file->mmap_addr, ds_file->mmap_len)) {
60 BT_CLOGE_ERRNO("Cannot memory-unmap file",
61 ": address=%p, size=%zu, file_path=\"%s\", file=%p", ds_file->mmap_addr,
62 ds_file->mmap_len, ds_file->file ? ds_file->file->path.c_str() : "NULL",
63 ds_file->file ? ds_file->file->fp.get() : NULL);
64 return CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
65 }
66
67 ds_file->mmap_addr = NULL;
68
69 return CTF_MSG_ITER_MEDIUM_STATUS_OK;
70 }
71
72 /*
73 * mmap a region of `ds_file` such that `requested_offset_in_file` is in the
74 * mapping. If the currently mmap-ed region already contains
75 * `requested_offset_in_file`, the mapping is kept.
76 *
77 * Set `ds_file->requested_offset_in_mapping` based on `request_offset_in_file`,
78 * such that the next call to `request_bytes` will return bytes starting at that
79 * position.
80 *
81 * `requested_offset_in_file` must be a valid offset in the file.
82 */
83 static enum ctf_msg_iter_medium_status ds_file_mmap(struct ctf_fs_ds_file *ds_file,
84 off_t requested_offset_in_file)
85 {
86 const bt2_common::LogCfg& logCfg = ds_file->logCfg;
87
88 /* Ensure the requested offset is in the file range. */
89 BT_ASSERT(requested_offset_in_file >= 0);
90 BT_ASSERT(requested_offset_in_file < ds_file->file->size);
91
92 /*
93 * If the mapping already contains the requested offset, just adjust
94 * requested_offset_in_mapping.
95 */
96 if (offset_ist_mapped(ds_file, requested_offset_in_file)) {
97 ds_file->request_offset_in_mapping =
98 requested_offset_in_file - ds_file->mmap_offset_in_file;
99 return CTF_MSG_ITER_MEDIUM_STATUS_OK;
100 }
101
102 /* Unmap old region */
103 ctf_msg_iter_medium_status status = ds_file_munmap(ds_file);
104 if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) {
105 return status;
106 }
107
108 /*
109 * Compute a mapping that has the required alignment properties and
110 * contains `requested_offset_in_file`.
111 */
112 ds_file->request_offset_in_mapping =
113 requested_offset_in_file % bt_mmap_get_offset_align_size(logCfg.logLevel());
114 ds_file->mmap_offset_in_file = requested_offset_in_file - ds_file->request_offset_in_mapping;
115 ds_file->mmap_len =
116 MIN(ds_file->file->size - ds_file->mmap_offset_in_file, ds_file->mmap_max_len);
117
118 BT_ASSERT(ds_file->mmap_len > 0);
119
120 ds_file->mmap_addr =
121 bt_mmap((void *) 0, ds_file->mmap_len, PROT_READ, MAP_PRIVATE,
122 fileno(ds_file->file->fp.get()), ds_file->mmap_offset_in_file, logCfg.logLevel());
123 if (ds_file->mmap_addr == MAP_FAILED) {
124 BT_CLOGE("Cannot memory-map address (size %zu) of file \"%s\" (%p) at offset %jd: %s",
125 ds_file->mmap_len, ds_file->file->path.c_str(), ds_file->file->fp.get(),
126 (intmax_t) ds_file->mmap_offset_in_file, strerror(errno));
127 return CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
128 }
129
130 return CTF_MSG_ITER_MEDIUM_STATUS_OK;
131 }
132
133 /*
134 * Change the mapping of the file to read the region that follows the current
135 * mapping.
136 *
137 * If the file hasn't been mapped yet, then everything (mmap_offset_in_file,
138 * mmap_len, request_offset_in_mapping) should have the value 0, which will
139 * result in the beginning of the file getting mapped.
140 *
141 * return _EOF if the current mapping is the end of the file.
142 */
143
144 static enum ctf_msg_iter_medium_status ds_file_mmap_next(struct ctf_fs_ds_file *ds_file)
145 {
146 /*
147 * If we're called, it's because more bytes are requested but we have
148 * given all the bytes of the current mapping.
149 */
150 BT_ASSERT(ds_file->request_offset_in_mapping == ds_file->mmap_len);
151
152 /*
153 * If the current mapping coincides with the end of the file, there is
154 * no next mapping.
155 */
156 if (ds_file->mmap_offset_in_file + ds_file->mmap_len == ds_file->file->size) {
157 return CTF_MSG_ITER_MEDIUM_STATUS_EOF;
158 }
159
160 return ds_file_mmap(ds_file, ds_file->mmap_offset_in_file + ds_file->mmap_len);
161 }
162
163 static enum ctf_msg_iter_medium_status medop_request_bytes(size_t request_sz, uint8_t **buffer_addr,
164 size_t *buffer_sz, void *data)
165 {
166 struct ctf_fs_ds_file *ds_file = (struct ctf_fs_ds_file *) data;
167 const bt2_common::LogCfg& logCfg = ds_file->logCfg;
168
169 BT_ASSERT(request_sz > 0);
170
171 /*
172 * Check if we have at least one memory-mapped byte left. If we don't,
173 * mmap the next file.
174 */
175 if (remaining_mmap_bytes(ds_file) == 0) {
176 /* Are we at the end of the file? */
177 if (ds_file->mmap_offset_in_file >= ds_file->file->size) {
178 BT_CLOGD("Reached end of file \"%s\" (%p)", ds_file->file->path.c_str(),
179 ds_file->file->fp.get());
180 return CTF_MSG_ITER_MEDIUM_STATUS_EOF;
181 }
182
183 ctf_msg_iter_medium_status status = ds_file_mmap_next(ds_file);
184 switch (status) {
185 case CTF_MSG_ITER_MEDIUM_STATUS_OK:
186 break;
187 case CTF_MSG_ITER_MEDIUM_STATUS_EOF:
188 return CTF_MSG_ITER_MEDIUM_STATUS_EOF;
189 default:
190 BT_CLOGE("Cannot memory-map next region of file \"%s\" (%p)",
191 ds_file->file->path.c_str(), ds_file->file->fp.get());
192 return status;
193 }
194 }
195
196 BT_ASSERT(remaining_mmap_bytes(ds_file) > 0);
197 *buffer_sz = MIN(remaining_mmap_bytes(ds_file), request_sz);
198
199 BT_ASSERT(ds_file->mmap_addr);
200 *buffer_addr = ((uint8_t *) ds_file->mmap_addr) + ds_file->request_offset_in_mapping;
201
202 ds_file->request_offset_in_mapping += *buffer_sz;
203
204 return CTF_MSG_ITER_MEDIUM_STATUS_OK;
205 }
206
207 static bt_stream *medop_borrow_stream(bt_stream_class *stream_class, int64_t stream_id, void *data)
208 {
209 struct ctf_fs_ds_file *ds_file = (struct ctf_fs_ds_file *) data;
210 bt_stream_class *ds_file_stream_class;
211
212 ds_file_stream_class = (*ds_file->stream)->cls().libObjPtr();
213
214 if (stream_class != ds_file_stream_class) {
215 /*
216 * Not supported: two packets described by two different
217 * stream classes within the same data stream file.
218 */
219 return nullptr;
220 }
221
222 return (*ds_file->stream)->libObjPtr();
223 }
224
225 static enum ctf_msg_iter_medium_status medop_seek(off_t offset, void *data)
226 {
227 struct ctf_fs_ds_file *ds_file = (struct ctf_fs_ds_file *) data;
228
229 BT_ASSERT(offset >= 0);
230 BT_ASSERT(offset < ds_file->file->size);
231
232 return ds_file_mmap(ds_file, offset);
233 }
234
235 BT_HIDDEN
236 struct ctf_msg_iter_medium_ops ctf_fs_ds_file_medops = {
237 medop_request_bytes,
238 medop_seek,
239 nullptr,
240 medop_borrow_stream,
241 };
242
243 struct ctf_fs_ds_group_medops_data
244 {
245 explicit ctf_fs_ds_group_medops_data(const bt2_common::LogCfg& logCfgParam) noexcept :
246 logCfg {logCfgParam}
247 {
248 }
249
250 /* Weak, set once at creation time. */
251 struct ctf_fs_ds_file_group *ds_file_group = nullptr;
252
253 /*
254 * Index (as in element rank) of the index entry of ds_file_groups'
255 * index we will read next (so, the one after the one we are reading
256 * right now).
257 */
258 guint next_index_entry_index = 0;
259
260 /*
261 * File we are currently reading. Changes whenever we switch to
262 * reading another data file.
263 *
264 * Owned by this.
265 */
266 ctf_fs_ds_file::UP file;
267
268 /* Weak, for context / logging / appending causes. */
269 bt_self_message_iterator *self_msg_iter = nullptr;
270 const bt2_common::LogCfg logCfg;
271 };
272
273 static enum ctf_msg_iter_medium_status medop_group_request_bytes(size_t request_sz,
274 uint8_t **buffer_addr,
275 size_t *buffer_sz, void *void_data)
276 {
277 struct ctf_fs_ds_group_medops_data *data = (struct ctf_fs_ds_group_medops_data *) void_data;
278
279 /* Return bytes from the current file. */
280 return medop_request_bytes(request_sz, buffer_addr, buffer_sz, data->file.get());
281 }
282
283 static bt_stream *medop_group_borrow_stream(bt_stream_class *stream_class, int64_t stream_id,
284 void *void_data)
285 {
286 struct ctf_fs_ds_group_medops_data *data = (struct ctf_fs_ds_group_medops_data *) void_data;
287
288 return medop_borrow_stream(stream_class, stream_id, data->file.get());
289 }
290
291 /*
292 * Set `data->file` to prepare it to read the packet described
293 * by `index_entry`.
294 */
295
296 static enum ctf_msg_iter_medium_status
297 ctf_fs_ds_group_medops_set_file(struct ctf_fs_ds_group_medops_data *data,
298 struct ctf_fs_ds_index_entry *index_entry)
299 {
300 BT_ASSERT(data);
301 BT_ASSERT(index_entry);
302
303 /* Check if that file is already the one mapped. */
304 if (!data->file || strcmp(index_entry->path, data->file->file->path.c_str()) != 0) {
305 /* Create the new file. */
306 data->file =
307 ctf_fs_ds_file_create(data->ds_file_group->ctf_fs_trace, data->ds_file_group->stream,
308 index_entry->path, data->logCfg);
309
310 if (!data->file) {
311 const bt2_common::LogCfg& logCfg = data->logCfg;
312 BT_CLOGE_APPEND_CAUSE("failed to create ctf_fs_ds_file.");
313 return CTF_MSG_ITER_MEDIUM_STATUS_ERROR;
314 }
315 }
316
317 /*
318 * Ensure the right portion of the file will be returned on the next
319 * request_bytes call.
320 */
321 return ds_file_mmap(data->file.get(), index_entry->offset.bytes());
322 }
323
324 static enum ctf_msg_iter_medium_status medop_group_switch_packet(void *void_data)
325 {
326 struct ctf_fs_ds_group_medops_data *data = (struct ctf_fs_ds_group_medops_data *) void_data;
327
328 /* If we have gone through all index entries, we are done. */
329 if (data->next_index_entry_index >= data->ds_file_group->index.entries.size()) {
330 return CTF_MSG_ITER_MEDIUM_STATUS_EOF;
331 }
332
333 /*
334 * Otherwise, look up the next index entry / packet and prepare it
335 * for reading.
336 */
337 ctf_fs_ds_index_entry& index_entry =
338 data->ds_file_group->index.entries[data->next_index_entry_index];
339
340 ctf_msg_iter_medium_status status = ctf_fs_ds_group_medops_set_file(data, &index_entry);
341 if (status != CTF_MSG_ITER_MEDIUM_STATUS_OK) {
342 return status;
343 }
344
345 data->next_index_entry_index++;
346
347 return CTF_MSG_ITER_MEDIUM_STATUS_OK;
348 }
349
350 void ctf_fs_ds_group_medops_data_deleter::operator()(ctf_fs_ds_group_medops_data *data)
351 {
352 delete data;
353 }
354
355 enum ctf_msg_iter_medium_status ctf_fs_ds_group_medops_data_create(
356 struct ctf_fs_ds_file_group *ds_file_group, bt_self_message_iterator *self_msg_iter,
357 const bt2_common::LogCfg& logCfg, ctf_fs_ds_group_medops_data_up& out)
358 {
359 BT_ASSERT(self_msg_iter);
360 BT_ASSERT(ds_file_group);
361 BT_ASSERT(!ds_file_group->index.entries.empty());
362
363 out.reset(new ctf_fs_ds_group_medops_data {logCfg});
364
365 out->ds_file_group = ds_file_group;
366 out->self_msg_iter = self_msg_iter;
367
368 /*
369 * No need to prepare the first file. ctf_msg_iter will call
370 * switch_packet before reading the first packet, it will be
371 * done then.
372 */
373
374 return CTF_MSG_ITER_MEDIUM_STATUS_OK;
375 }
376
377 void ctf_fs_ds_group_medops_data_reset(struct ctf_fs_ds_group_medops_data *data)
378 {
379 data->next_index_entry_index = 0;
380 }
381
382 struct ctf_msg_iter_medium_ops ctf_fs_ds_group_medops = {
383 .request_bytes = medop_group_request_bytes,
384
385 /*
386 * We don't support seeking using this medops. It would probably be
387 * possible, but it's not needed at the moment.
388 */
389 .seek = NULL,
390
391 .switch_packet = medop_group_switch_packet,
392 .borrow_stream = medop_group_borrow_stream,
393 };
394
395 static int convert_cycles_to_ns(struct ctf_clock_class *clock_class, uint64_t cycles, int64_t *ns)
396 {
397 return bt_util_clock_cycles_to_ns_from_origin(cycles, clock_class->frequency,
398 clock_class->offset_seconds,
399 clock_class->offset_cycles, ns);
400 }
401
402 static nonstd::optional<ctf_fs_ds_index>
403 build_index_from_idx_file(struct ctf_fs_ds_file *ds_file, struct ctf_fs_ds_file_info *file_info,
404 struct ctf_msg_iter *msg_iter)
405 {
406 const bt2_common::LogCfg& logCfg = ds_file->logCfg;
407
408 BT_CLOGI("Building index from .idx file of stream file %s", ds_file->file->path.c_str());
409 ctf_msg_iter_packet_properties props;
410 int ret = ctf_msg_iter_get_packet_properties(msg_iter, &props);
411 if (ret) {
412 BT_CLOGI_STR("Cannot read first packet's header and context fields.");
413 return nonstd::nullopt;
414 }
415
416 ctf_stream_class *sc =
417 ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, props.stream_class_id);
418 BT_ASSERT(sc);
419 if (!sc->default_clock_class) {
420 BT_CLOGI_STR("Cannot find stream class's default clock class.");
421 return nonstd::nullopt;
422 }
423
424 /* Look for index file in relative path index/name.idx. */
425 bt2_common::GCharUP basename {g_path_get_basename(ds_file->file->path.c_str())};
426 if (!basename) {
427 BT_CLOGE("Cannot get the basename of datastream file %s", ds_file->file->path.c_str());
428 return nonstd::nullopt;
429 }
430
431 bt2_common::GCharUP directory {g_path_get_dirname(ds_file->file->path.c_str())};
432 if (!directory) {
433 BT_CLOGE("Cannot get dirname of datastream file %s", ds_file->file->path.c_str());
434 return nonstd::nullopt;
435 }
436
437 std::string index_basename = basename.get();
438 index_basename += ".idx";
439
440 bt2_common::GCharUP index_file_path {
441 g_build_filename(directory.get(), "index", index_basename.c_str(), NULL)};
442 bt2_common::GMappedFileUP mapped_file {g_mapped_file_new(index_file_path.get(), FALSE, NULL)};
443 if (!mapped_file) {
444 BT_CLOGD("Cannot create new mapped file %s", index_file_path.get());
445 return nonstd::nullopt;
446 }
447
448 /*
449 * The g_mapped_file API limits us to 4GB files on 32-bit.
450 * Traces with such large indexes have never been seen in the wild,
451 * but this would need to be adjusted to support them.
452 */
453 gsize filesize = g_mapped_file_get_length(mapped_file.get());
454 if (filesize < sizeof(ctf_packet_index_file_hdr)) {
455 BT_CLOGW("Invalid LTTng trace index file: "
456 "file size (%zu bytes) < header size (%zu bytes)",
457 filesize, sizeof(ctf_packet_index_file_hdr));
458 return nonstd::nullopt;
459 }
460
461 const char *mmap_begin = g_mapped_file_get_contents(mapped_file.get());
462 const ctf_packet_index_file_hdr *header = (const ctf_packet_index_file_hdr *) mmap_begin;
463
464 const char *file_pos = g_mapped_file_get_contents(mapped_file.get()) + sizeof(*header);
465 if (be32toh(header->magic) != CTF_INDEX_MAGIC) {
466 BT_CLOGW_STR("Invalid LTTng trace index: \"magic\" field validation failed");
467 return nonstd::nullopt;
468 }
469
470 uint32_t version_major = be32toh(header->index_major);
471 uint32_t version_minor = be32toh(header->index_minor);
472 if (version_major != 1) {
473 BT_CLOGW("Unknown LTTng trace index version: "
474 "major=%" PRIu32 ", minor=%" PRIu32,
475 version_major, version_minor);
476 return nonstd::nullopt;
477 }
478
479 size_t file_index_entry_size = be32toh(header->packet_index_len);
480 if (file_index_entry_size < CTF_INDEX_1_0_SIZE) {
481 BT_CLOGW(
482 "Invalid `packet_index_len` in LTTng trace index file (`packet_index_len` < CTF index 1.0 index entry size): "
483 "packet_index_len=%zu, CTF_INDEX_1_0_SIZE=%zu",
484 file_index_entry_size, CTF_INDEX_1_0_SIZE);
485 return nonstd::nullopt;
486 }
487
488 size_t file_entry_count = (filesize - sizeof(*header)) / file_index_entry_size;
489 if ((filesize - sizeof(*header)) % file_index_entry_size) {
490 BT_CLOGW("Invalid LTTng trace index: the index's size after the header "
491 "(%zu bytes) is not a multiple of the index entry size "
492 "(%zu bytes)",
493 (filesize - sizeof(*header)), sizeof(*header));
494 return nonstd::nullopt;
495 }
496
497 ctf_fs_ds_index index;
498 ctf_fs_ds_index_entry *prev_index_entry = nullptr;
499 bt2_common::DataLen totalPacketsSize = bt2_common::DataLen::fromBytes(0);
500
501 for (size_t i = 0; i < file_entry_count; i++) {
502 struct ctf_packet_index *file_index = (struct ctf_packet_index *) file_pos;
503 bt2_common::DataLen packetSize =
504 bt2_common::DataLen::fromBits(be64toh(file_index->packet_size));
505
506 if (packetSize.hasExtraBits()) {
507 BT_CLOGW("Invalid packet size encountered in LTTng trace index file");
508 return nonstd::nullopt;
509 }
510
511 bt2_common::DataLen offset = bt2_common::DataLen::fromBytes(be64toh(file_index->offset));
512 if (i != 0 && offset < prev_index_entry->offset) {
513 BT_CLOGW("Invalid, non-monotonic, packet offset encountered in LTTng trace index file: "
514 "previous offset=%llu bytes, current offset=%llu bytes",
515 prev_index_entry->offset.bytes(), offset.bytes());
516 return nonstd::nullopt;
517 }
518
519 ctf_fs_ds_index_entry index_entry {offset, packetSize};
520
521 /* Set path to stream file. */
522 index_entry.path = file_info->path.c_str();
523
524 index_entry.timestamp_begin = be64toh(file_index->timestamp_begin);
525 index_entry.timestamp_end = be64toh(file_index->timestamp_end);
526 if (index_entry.timestamp_end < index_entry.timestamp_begin) {
527 BT_CLOGW(
528 "Invalid packet time bounds encountered in LTTng trace index file (begin > end): "
529 "timestamp_begin=%" PRIu64 "timestamp_end=%" PRIu64,
530 index_entry.timestamp_begin, index_entry.timestamp_end);
531 return nonstd::nullopt;
532 }
533
534 /* Convert the packet's bound to nanoseconds since Epoch. */
535 ret = convert_cycles_to_ns(sc->default_clock_class, index_entry.timestamp_begin,
536 &index_entry.timestamp_begin_ns);
537 if (ret) {
538 BT_CLOGI_STR(
539 "Failed to convert raw timestamp to nanoseconds since Epoch during index parsing");
540 return nonstd::nullopt;
541 }
542 ret = convert_cycles_to_ns(sc->default_clock_class, index_entry.timestamp_end,
543 &index_entry.timestamp_end_ns);
544 if (ret) {
545 BT_CLOGI_STR(
546 "Failed to convert raw timestamp to nanoseconds since Epoch during LTTng trace index parsing");
547 return nonstd::nullopt;
548 }
549
550 if (version_minor >= 1) {
551 index_entry.packet_seq_num = be64toh(file_index->packet_seq_num);
552 }
553
554 totalPacketsSize += packetSize;
555 file_pos += file_index_entry_size;
556
557 index.entries.emplace_back(index_entry);
558
559 prev_index_entry = &index.entries.back();
560 }
561
562 /* Validate that the index addresses the complete stream. */
563 if (ds_file->file->size != totalPacketsSize.bytes()) {
564 BT_CLOGW("Invalid LTTng trace index file; indexed size != stream file size: "
565 "file-size=%" PRIu64 " bytes, total-packets-size=%llu bytes",
566 ds_file->file->size, totalPacketsSize.bytes());
567 return nonstd::nullopt;
568 }
569
570 return index;
571 }
572
573 static int init_index_entry(ctf_fs_ds_index_entry& entry, struct ctf_fs_ds_file *ds_file,
574 struct ctf_msg_iter_packet_properties *props)
575 {
576 ctf_stream_class *sc =
577 ctf_trace_class_borrow_stream_class_by_id(ds_file->metadata->tc, props->stream_class_id);
578 BT_ASSERT(sc);
579 const bt2_common::LogCfg& logCfg = ds_file->logCfg;
580
581 if (props->snapshots.beginning_clock != UINT64_C(-1)) {
582 entry.timestamp_begin = props->snapshots.beginning_clock;
583
584 /* Convert the packet's bound to nanoseconds since Epoch. */
585 int ret = convert_cycles_to_ns(sc->default_clock_class, props->snapshots.beginning_clock,
586 &entry.timestamp_begin_ns);
587 if (ret) {
588 BT_CLOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
589 return ret;
590 }
591 } else {
592 entry.timestamp_begin = UINT64_C(-1);
593 entry.timestamp_begin_ns = UINT64_C(-1);
594 }
595
596 if (props->snapshots.end_clock != UINT64_C(-1)) {
597 entry.timestamp_end = props->snapshots.end_clock;
598
599 /* Convert the packet's bound to nanoseconds since Epoch. */
600 int ret = convert_cycles_to_ns(sc->default_clock_class, props->snapshots.end_clock,
601 &entry.timestamp_end_ns);
602 if (ret) {
603 BT_CLOGI_STR("Failed to convert raw timestamp to nanoseconds since Epoch.");
604 return ret;
605 }
606 } else {
607 entry.timestamp_end = UINT64_C(-1);
608 entry.timestamp_end_ns = UINT64_C(-1);
609 }
610
611 return 0;
612 }
613
614 static nonstd::optional<ctf_fs_ds_index>
615 build_index_from_stream_file(struct ctf_fs_ds_file *ds_file, struct ctf_fs_ds_file_info *file_info,
616 struct ctf_msg_iter *msg_iter)
617 {
618 const bt2_common::LogCfg& logCfg = ds_file->logCfg;
619
620 BT_CLOGI("Indexing stream file %s", ds_file->file->path.c_str());
621
622 ctf_fs_ds_index index;
623 bt2_common::DataLen currentPacketOffset = bt2_common::DataLen::fromBytes(0);
624
625 while (true) {
626 struct ctf_msg_iter_packet_properties props;
627
628 if (currentPacketOffset.bytes() > ds_file->file->size) {
629 BT_CLOGE_STR("Unexpected current packet's offset (larger than file).");
630 return nonstd::nullopt;
631 } else if (currentPacketOffset.bytes() == ds_file->file->size) {
632 /* No more data */
633 break;
634 }
635
636 ctf_msg_iter_status iter_status = ctf_msg_iter_seek(msg_iter, currentPacketOffset.bytes());
637 if (iter_status != CTF_MSG_ITER_STATUS_OK) {
638 return nonstd::nullopt;
639 }
640
641 iter_status = ctf_msg_iter_get_packet_properties(msg_iter, &props);
642 if (iter_status != CTF_MSG_ITER_STATUS_OK) {
643 return nonstd::nullopt;
644 }
645
646 /*
647 * Get the current packet size from the packet header, if set. Else,
648 * assume there is a single packet in the file, so take the file size
649 * as the packet size.
650 */
651 bt2_common::DataLen currentPacketSize =
652 props.exp_packet_total_size >= 0 ?
653 bt2_common::DataLen::fromBits(props.exp_packet_total_size) :
654 bt2_common::DataLen::fromBytes(ds_file->file->size);
655
656 if ((currentPacketOffset + currentPacketSize).bytes() > ds_file->file->size) {
657 BT_CLOGW("Invalid packet size reported in file: stream=\"%s\", "
658 "packet-offset-bytes=%llu, packet-size-bytes=%llu, "
659 "file-size-bytes=%jd",
660 ds_file->file->path.c_str(), currentPacketOffset.bytes(),
661 currentPacketSize.bytes(), (intmax_t) ds_file->file->size);
662 return nonstd::nullopt;
663 }
664
665 ctf_fs_ds_index_entry index_entry {currentPacketOffset, currentPacketSize};
666
667 /* Set path to stream file. */
668 index_entry.path = file_info->path.c_str();
669
670 int ret = init_index_entry(index_entry, ds_file, &props);
671 if (ret) {
672 return nonstd::nullopt;
673 }
674
675 index.entries.emplace_back(std::move(index_entry));
676
677 currentPacketOffset += currentPacketSize;
678 BT_CLOGD("Seeking to next packet: current-packet-offset-bytes=%llu, "
679 "next-packet-offset-bytes=%llu",
680 (currentPacketOffset - currentPacketSize).bytes(), currentPacketOffset.bytes());
681 }
682
683 return index;
684 }
685
686 BT_HIDDEN
687 ctf_fs_ds_file::UP ctf_fs_ds_file_create(struct ctf_fs_trace *ctf_fs_trace,
688 nonstd::optional<bt2::Stream::Shared> stream,
689 const char *path, const bt2_common::LogCfg& logCfg)
690 {
691 const size_t offset_align = bt_mmap_get_offset_align_size(logCfg.logLevel());
692 ctf_fs_ds_file::UP ds_file = bt2_common::makeUnique<ctf_fs_ds_file>(logCfg);
693
694 ds_file->file = bt2_common::makeUnique<ctf_fs_file>(logCfg);
695 ds_file->stream = std::move(stream);
696 ds_file->metadata = ctf_fs_trace->metadata.get();
697 ds_file->file->path = path;
698 int ret = ctf_fs_file_open(ds_file->file.get(), "rb");
699 if (ret) {
700 return nullptr;
701 }
702
703 ds_file->mmap_max_len = offset_align * 2048;
704
705 return ds_file;
706 }
707
708 BT_HIDDEN
709 nonstd::optional<ctf_fs_ds_index> ctf_fs_ds_file_build_index(struct ctf_fs_ds_file *ds_file,
710 struct ctf_fs_ds_file_info *file_info,
711 struct ctf_msg_iter *msg_iter)
712 {
713 const bt2_common::LogCfg& logCfg = ds_file->logCfg;
714
715 nonstd::optional<ctf_fs_ds_index> index =
716 build_index_from_idx_file(ds_file, file_info, msg_iter);
717 if (index) {
718 return index;
719 }
720
721 BT_CLOGI("Failed to build index from .index file; "
722 "falling back to stream indexing.");
723 return build_index_from_stream_file(ds_file, file_info, msg_iter);
724 }
725
726 ctf_fs_ds_file::~ctf_fs_ds_file()
727 {
728 (void) ds_file_munmap(this);
729 }
730
731 BT_HIDDEN ctf_fs_ds_file_info::UP ctf_fs_ds_file_info_create(const char *path, int64_t begin_ns)
732 {
733 ctf_fs_ds_file_info::UP ds_file_info = bt2_common::makeUnique<ctf_fs_ds_file_info>();
734
735 ds_file_info->path = path;
736 ds_file_info->begin_ns = begin_ns;
737
738 return ds_file_info;
739 }
740
741 BT_HIDDEN ctf_fs_ds_file_group::UP ctf_fs_ds_file_group_create(struct ctf_fs_trace *ctf_fs_trace,
742 struct ctf_stream_class *sc,
743 uint64_t stream_instance_id,
744 ctf_fs_ds_index index)
745 {
746 ctf_fs_ds_file_group::UP ds_file_group {new ctf_fs_ds_file_group};
747
748 ds_file_group->index = std::move(index);
749
750 ds_file_group->stream_id = stream_instance_id;
751 BT_ASSERT(sc);
752 ds_file_group->sc = sc;
753 ds_file_group->ctf_fs_trace = ctf_fs_trace;
754
755 return ds_file_group;
756 }
This page took 0.046973 seconds and 5 git commands to generate.