2 * SPDX-License-Identifier: MIT
4 * Copyright (C) 2016 Philippe Proulx <pproulx@efficios.com>
7 #ifndef BABELTRACE_PLUGINS_CTF_FS_SRC_DATA_STREAM_FILE_HPP
8 #define BABELTRACE_PLUGINS_CTF_FS_SRC_DATA_STREAM_FILE_HPP
17 #include <babeltrace2/babeltrace.h>
19 #include "cpp-common/bt2/trace-ir.hpp"
20 #include "cpp-common/bt2c/data-len.hpp"
21 #include "cpp-common/bt2c/logging.hpp"
23 #include "../common/src/item-seq/medium.hpp"
24 #include "../common/src/metadata/ctf-ir.hpp"
27 struct ctf_fs_ds_file_info
29 using UP = std::unique_ptr<ctf_fs_ds_file_info>;
31 ctf_fs_ds_file_info(std::string pathParam, const bt2c::Logger& parentLogger);
37 /* Guaranteed to be set, as opposed to the index. */
43 using UP = std::unique_ptr<ctf_fs_ds_file>;
45 explicit ctf_fs_ds_file(const bt2c::Logger& parentLogger, const size_t mmapMaxLenParam) :
46 logger {parentLogger, "PLUGIN/SRC.CTF.FS/DS"}, mmap_max_len {mmapMaxLenParam}
50 ctf_fs_ds_file(const ctf_fs_ds_file&) = delete;
51 ctf_fs_ds_file& operator=(const ctf_fs_ds_file&) = delete;
58 void *mmap_addr = nullptr;
61 * Max length of chunk to mmap() when updating the current mapping.
62 * This value must be page-aligned.
64 size_t mmap_max_len = 0;
66 /* Length of the current mapping. Never exceeds the file's length. */
69 /* Offset in the file where the current mapping starts. */
70 off_t mmap_offset_in_file = 0;
73 struct ctf_fs_ds_index_entry
75 ctf_fs_ds_index_entry(const bt2c::CStringView pathParam, const bt2c::DataLen offsetInFileParam,
76 const bt2c::DataLen packetSizeParam) :
78 offsetInFile {offsetInFileParam}, offsetInStream {offsetInFileParam},
79 packetSize {packetSizeParam}
84 /* Weak, belongs to ctf_fs_ds_file_info. */
87 /* Position of the packet from the beginning of the file. */
88 bt2c::DataLen offsetInFile;
91 * Position of the packet from the beginning of the stream. Starts equal
92 * to `offsetInFile`, but can change when multiple data stream files
93 * belonging to the same stream are merged.
95 bt2c::DataLen offsetInStream;
97 /* Size of the packet. */
98 bt2c::DataLen packetSize;
101 * Extracted from the packet context, relative to the respective fields'
102 * mapped clock classes (in cycles).
104 uint64_t timestamp_begin = 0, timestamp_end = 0;
107 * Converted from the packet context, relative to the trace's EPOCH
108 * (in ns since EPOCH).
110 int64_t timestamp_begin_ns = 0, timestamp_end_ns = 0;
113 * Packet sequence number, or UINT64_MAX if not present in the index.
115 uint64_t packet_seq_num = UINT64_MAX;
118 struct ctf_fs_ds_index
120 using EntriesT = std::vector<ctf_fs_ds_index_entry>;
124 void updateOffsetsInStream();
127 struct ctf_fs_ds_file_group
129 using UP = std::unique_ptr<ctf_fs_ds_file_group>;
131 explicit ctf_fs_ds_file_group(struct ctf_fs_trace * const ctfFsTrace,
132 const ctf::src::DataStreamCls& dataStreamClsParam,
133 const uint64_t streamInstanceId,
134 ctf_fs_ds_index indexParam) noexcept :
135 dataStreamCls {&dataStreamClsParam},
136 stream_id {streamInstanceId}, ctf_fs_trace {ctfFsTrace},
137 /* Don't use brace initialization, because of gcc 4.8. */
138 index(std::move(indexParam))
143 * Insert ds_file_info in the list of ds_file_infos at the right
144 * place to keep it sorted.
146 void insert_ds_file_info_sorted(ctf_fs_ds_file_info::UP ds_file_info);
149 * This is an _ordered_ array of data stream file infos which
150 * belong to this group (a single stream instance).
152 * You can call ctf_fs_ds_file_create() with one of those paths
153 * and the trace IR stream below.
155 std::vector<ctf_fs_ds_file_info::UP> ds_file_infos;
157 const ctf::src::DataStreamCls *dataStreamCls;
159 bt2::Stream::Shared stream;
161 /* Stream (instance) ID; -1ULL means none */
162 uint64_t stream_id = 0;
164 /* Weak, belongs to component */
165 struct ctf_fs_trace *ctf_fs_trace = nullptr;
167 ctf_fs_ds_index index;
170 ctf_fs_ds_file::UP ctf_fs_ds_file_create(const char *path, const bt2c::Logger& parentLogger);
172 bt2s::optional<ctf_fs_ds_index> ctf_fs_ds_file_build_index(const ctf_fs_ds_file_info& file_info,
173 const ctf::src::TraceCls& traceCls);
179 struct Medium : public ctf::src::Medium
181 explicit Medium(const ctf_fs_ds_index& index, const bt2c::Logger& parentLogger);
184 Medium(const Medium&) = delete;
185 Medium& operator=(const Medium&) = delete;
187 ctf::src::Buf buf(bt2c::DataLen offset, bt2c::DataLen minSize) override;
190 ctf_fs_ds_index::EntriesT::const_iterator
191 _mFindIndexEntryForOffset(bt2c::DataLen offsetInStream) const noexcept;
193 const ctf_fs_ds_index& _mIndex;
194 bt2c::Logger _mLogger;
195 ctf_fs_ds_file::UP _mCurrentDsFile;
199 } /* namespace src */
200 } /* namespace ctf */
202 #endif /* BABELTRACE_PLUGINS_CTF_FS_SRC_DATA_STREAM_FILE_HPP */