Commit | Line | Data |
---|---|---|
e98a2d6e | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
e98a2d6e | 3 | * |
0235b0db | 4 | * Copyright (C) 2016 Philippe Proulx <pproulx@efficios.com> |
e98a2d6e PP |
5 | */ |
6 | ||
ae2be88d SM |
7 | #ifndef BABELTRACE_PLUGINS_CTF_FS_SRC_DATA_STREAM_FILE_HPP |
8 | #define BABELTRACE_PLUGINS_CTF_FS_SRC_DATA_STREAM_FILE_HPP | |
0235b0db | 9 | |
fe2e19c4 | 10 | #include <memory> |
4d199954 | 11 | #include <string> |
f3d74124 | 12 | #include <vector> |
fe2e19c4 | 13 | |
e98a2d6e | 14 | #include <glib.h> |
c802cacb SM |
15 | #include <stdio.h> |
16 | ||
3fadfbc0 | 17 | #include <babeltrace2/babeltrace.h> |
e98a2d6e | 18 | |
be215bcd | 19 | #include "cpp-common/bt2/trace-ir.hpp" |
873c329a | 20 | #include "cpp-common/bt2c/data-len.hpp" |
0f5c5d5c SM |
21 | #include "cpp-common/bt2c/logging.hpp" |
22 | ||
9328f86b SM |
23 | #include "../common/src/item-seq/medium.hpp" |
24 | #include "../common/src/metadata/ctf-ir.hpp" | |
c44dc433 | 25 | #include "file.hpp" |
b6c3dcb2 | 26 | |
4164020e SM |
27 | struct ctf_fs_ds_file_info |
28 | { | |
2cef6403 SM |
29 | using UP = std::unique_ptr<ctf_fs_ds_file_info>; |
30 | ||
9328f86b SM |
31 | ctf_fs_ds_file_info(std::string pathParam, const bt2c::Logger& parentLogger); |
32 | ||
33 | bt2c::Logger logger; | |
4d199954 | 34 | std::string path; |
9328f86b | 35 | bt2c::DataLen size; |
97ade20b | 36 | |
4164020e | 37 | /* Guaranteed to be set, as opposed to the index. */ |
afb0f12b | 38 | int64_t begin_ns = 0; |
97ade20b JG |
39 | }; |
40 | ||
4164020e SM |
41 | struct ctf_fs_ds_file |
42 | { | |
89f88383 SM |
43 | using UP = std::unique_ptr<ctf_fs_ds_file>; |
44 | ||
9328f86b SM |
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} | |
0f5c5d5c SM |
47 | { |
48 | } | |
98903a3e | 49 | |
3199f1ba SM |
50 | ctf_fs_ds_file(const ctf_fs_ds_file&) = delete; |
51 | ctf_fs_ds_file& operator=(const ctf_fs_ds_file&) = delete; | |
52 | ~ctf_fs_ds_file(); | |
53 | ||
0f5c5d5c | 54 | bt2c::Logger logger; |
4c65a157 | 55 | |
c44dc433 | 56 | ctf_fs_file::UP file; |
97ade20b | 57 | |
afb0f12b | 58 | void *mmap_addr = nullptr; |
97ade20b | 59 | |
4164020e SM |
60 | /* |
61 | * Max length of chunk to mmap() when updating the current mapping. | |
62 | * This value must be page-aligned. | |
63 | */ | |
afb0f12b | 64 | size_t mmap_max_len = 0; |
97ade20b | 65 | |
4164020e | 66 | /* Length of the current mapping. Never exceeds the file's length. */ |
afb0f12b | 67 | size_t mmap_len = 0; |
97ade20b | 68 | |
4164020e | 69 | /* Offset in the file where the current mapping starts. */ |
afb0f12b | 70 | off_t mmap_offset_in_file = 0; |
b6c3dcb2 | 71 | }; |
e98a2d6e | 72 | |
873c329a SM |
73 | struct ctf_fs_ds_index_entry |
74 | { | |
9328f86b SM |
75 | ctf_fs_ds_index_entry(const bt2c::CStringView pathParam, const bt2c::DataLen offsetInFileParam, |
76 | const bt2c::DataLen packetSizeParam) : | |
f9b673b0 | 77 | path {pathParam}, |
9328f86b SM |
78 | offsetInFile {offsetInFileParam}, offsetInStream {offsetInFileParam}, |
79 | packetSize {packetSizeParam} | |
873c329a | 80 | { |
9328f86b | 81 | BT_ASSERT(path); |
873c329a SM |
82 | } |
83 | ||
84 | /* Weak, belongs to ctf_fs_ds_file_info. */ | |
f9b673b0 | 85 | const char *path; |
873c329a SM |
86 | |
87 | /* Position of the packet from the beginning of the file. */ | |
9328f86b SM |
88 | bt2c::DataLen offsetInFile; |
89 | ||
90 | /* | |
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. | |
94 | */ | |
95 | bt2c::DataLen offsetInStream; | |
873c329a SM |
96 | |
97 | /* Size of the packet. */ | |
98 | bt2c::DataLen packetSize; | |
99 | ||
100 | /* | |
101 | * Extracted from the packet context, relative to the respective fields' | |
102 | * mapped clock classes (in cycles). | |
103 | */ | |
104 | uint64_t timestamp_begin = 0, timestamp_end = 0; | |
105 | ||
106 | /* | |
107 | * Converted from the packet context, relative to the trace's EPOCH | |
108 | * (in ns since EPOCH). | |
109 | */ | |
110 | int64_t timestamp_begin_ns = 0, timestamp_end_ns = 0; | |
111 | ||
112 | /* | |
113 | * Packet sequence number, or UINT64_MAX if not present in the index. | |
114 | */ | |
8bcfc7d7 | 115 | uint64_t packet_seq_num = UINT64_MAX; |
873c329a SM |
116 | }; |
117 | ||
118 | struct ctf_fs_ds_index | |
119 | { | |
9328f86b SM |
120 | using EntriesT = std::vector<ctf_fs_ds_index_entry>; |
121 | ||
122 | EntriesT entries; | |
123 | ||
124 | void updateOffsetsInStream(); | |
873c329a SM |
125 | }; |
126 | ||
127 | struct ctf_fs_ds_file_group | |
128 | { | |
235f9fd9 | 129 | using UP = std::unique_ptr<ctf_fs_ds_file_group>; |
fe2e19c4 | 130 | |
9328f86b SM |
131 | explicit ctf_fs_ds_file_group(struct ctf_fs_trace * const ctfFsTrace, |
132 | const ctf::src::DataStreamCls& dataStreamClsParam, | |
133 | const uint64_t streamInstanceId, | |
86c82636 | 134 | ctf_fs_ds_index indexParam) noexcept : |
9328f86b SM |
135 | dataStreamCls {&dataStreamClsParam}, |
136 | stream_id {streamInstanceId}, ctf_fs_trace {ctfFsTrace}, | |
70b3e6e5 SM |
137 | /* Don't use brace initialization, because of gcc 4.8. */ |
138 | index(std::move(indexParam)) | |
86c82636 SM |
139 | { |
140 | } | |
141 | ||
1cb63a52 SM |
142 | /* |
143 | * Insert ds_file_info in the list of ds_file_infos at the right | |
144 | * place to keep it sorted. | |
145 | */ | |
146 | void insert_ds_file_info_sorted(ctf_fs_ds_file_info::UP ds_file_info); | |
147 | ||
873c329a | 148 | /* |
873c329a SM |
149 | * This is an _ordered_ array of data stream file infos which |
150 | * belong to this group (a single stream instance). | |
151 | * | |
152 | * You can call ctf_fs_ds_file_create() with one of those paths | |
153 | * and the trace IR stream below. | |
154 | */ | |
f3d74124 | 155 | std::vector<ctf_fs_ds_file_info::UP> ds_file_infos; |
873c329a | 156 | |
9328f86b | 157 | const ctf::src::DataStreamCls *dataStreamCls; |
873c329a | 158 | |
be215bcd | 159 | bt2::Stream::Shared stream; |
873c329a SM |
160 | |
161 | /* Stream (instance) ID; -1ULL means none */ | |
162 | uint64_t stream_id = 0; | |
163 | ||
164 | /* Weak, belongs to component */ | |
165 | struct ctf_fs_trace *ctf_fs_trace = nullptr; | |
166 | ||
c46b32d8 | 167 | ctf_fs_ds_index index; |
873c329a SM |
168 | }; |
169 | ||
9328f86b | 170 | ctf_fs_ds_file::UP ctf_fs_ds_file_create(const char *path, const bt2c::Logger& parentLogger); |
e98a2d6e | 171 | |
9328f86b SM |
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); | |
97ade20b | 174 | |
9328f86b SM |
175 | namespace ctf { |
176 | namespace src { | |
177 | namespace fs { | |
873c329a | 178 | |
9328f86b SM |
179 | struct Medium : public ctf::src::Medium |
180 | { | |
181 | explicit Medium(const ctf_fs_ds_index& index, const bt2c::Logger& parentLogger); | |
6de92955 | 182 | |
9328f86b SM |
183 | ~Medium() = default; |
184 | Medium(const Medium&) = delete; | |
185 | Medium& operator=(const Medium&) = delete; | |
f6e68e70 | 186 | |
9328f86b | 187 | ctf::src::Buf buf(bt2c::DataLen offset, bt2c::DataLen minSize) override; |
3cf88182 | 188 | |
9328f86b SM |
189 | private: |
190 | ctf_fs_ds_index::EntriesT::const_iterator | |
191 | _mFindIndexEntryForOffset(bt2c::DataLen offsetInStream) const noexcept; | |
3cf88182 | 192 | |
9328f86b SM |
193 | const ctf_fs_ds_index& _mIndex; |
194 | bt2c::Logger _mLogger; | |
195 | ctf_fs_ds_file::UP _mCurrentDsFile; | |
196 | }; | |
f6e68e70 | 197 | |
9328f86b SM |
198 | } /* namespace fs */ |
199 | } /* namespace src */ | |
200 | } /* namespace ctf */ | |
f6e68e70 | 201 | |
ae2be88d | 202 | #endif /* BABELTRACE_PLUGINS_CTF_FS_SRC_DATA_STREAM_FILE_HPP */ |