| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright (C) 2016 Philippe Proulx <pproulx@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #ifndef BABELTRACE_PLUGINS_CTF_FS_SRC_DATA_STREAM_FILE_HPP |
| 8 | #define BABELTRACE_PLUGINS_CTF_FS_SRC_DATA_STREAM_FILE_HPP |
| 9 | |
| 10 | #include <memory> |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include <glib.h> |
| 15 | #include <stdio.h> |
| 16 | |
| 17 | #include <babeltrace2/babeltrace.h> |
| 18 | |
| 19 | #include "cpp-common/bt2/trace-ir.hpp" |
| 20 | #include "cpp-common/bt2c/data-len.hpp" |
| 21 | #include "cpp-common/bt2c/logging.hpp" |
| 22 | |
| 23 | #include "../common/src/item-seq/medium.hpp" |
| 24 | #include "../common/src/metadata/ctf-ir.hpp" |
| 25 | #include "file.hpp" |
| 26 | |
| 27 | struct ctf_fs_ds_file_info |
| 28 | { |
| 29 | using UP = std::unique_ptr<ctf_fs_ds_file_info>; |
| 30 | |
| 31 | ctf_fs_ds_file_info(std::string pathParam, const bt2c::Logger& parentLogger); |
| 32 | |
| 33 | bt2c::Logger logger; |
| 34 | std::string path; |
| 35 | bt2c::DataLen size; |
| 36 | |
| 37 | /* Guaranteed to be set, as opposed to the index. */ |
| 38 | int64_t begin_ns = 0; |
| 39 | }; |
| 40 | |
| 41 | struct ctf_fs_ds_file |
| 42 | { |
| 43 | using UP = std::unique_ptr<ctf_fs_ds_file>; |
| 44 | |
| 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} |
| 47 | { |
| 48 | } |
| 49 | |
| 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 | |
| 54 | bt2c::Logger logger; |
| 55 | |
| 56 | ctf_fs_file::UP file; |
| 57 | |
| 58 | void *mmap_addr = nullptr; |
| 59 | |
| 60 | /* |
| 61 | * Max length of chunk to mmap() when updating the current mapping. |
| 62 | * This value must be page-aligned. |
| 63 | */ |
| 64 | size_t mmap_max_len = 0; |
| 65 | |
| 66 | /* Length of the current mapping. Never exceeds the file's length. */ |
| 67 | size_t mmap_len = 0; |
| 68 | |
| 69 | /* Offset in the file where the current mapping starts. */ |
| 70 | off_t mmap_offset_in_file = 0; |
| 71 | }; |
| 72 | |
| 73 | struct ctf_fs_ds_index_entry |
| 74 | { |
| 75 | ctf_fs_ds_index_entry(const bt2c::CStringView pathParam, const bt2c::DataLen offsetInFileParam, |
| 76 | const bt2c::DataLen packetSizeParam) : |
| 77 | path {pathParam}, |
| 78 | offsetInFile {offsetInFileParam}, offsetInStream {offsetInFileParam}, |
| 79 | packetSize {packetSizeParam} |
| 80 | { |
| 81 | BT_ASSERT(path); |
| 82 | } |
| 83 | |
| 84 | /* Weak, belongs to ctf_fs_ds_file_info. */ |
| 85 | const char *path; |
| 86 | |
| 87 | /* Position of the packet from the beginning of the file. */ |
| 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; |
| 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 | */ |
| 115 | uint64_t packet_seq_num = UINT64_MAX; |
| 116 | }; |
| 117 | |
| 118 | struct ctf_fs_ds_index |
| 119 | { |
| 120 | using EntriesT = std::vector<ctf_fs_ds_index_entry>; |
| 121 | |
| 122 | EntriesT entries; |
| 123 | |
| 124 | void updateOffsetsInStream(); |
| 125 | }; |
| 126 | |
| 127 | struct ctf_fs_ds_file_group |
| 128 | { |
| 129 | using UP = std::unique_ptr<ctf_fs_ds_file_group>; |
| 130 | |
| 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)) |
| 139 | { |
| 140 | } |
| 141 | |
| 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 | |
| 148 | /* |
| 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 | */ |
| 155 | std::vector<ctf_fs_ds_file_info::UP> ds_file_infos; |
| 156 | |
| 157 | const ctf::src::DataStreamCls *dataStreamCls; |
| 158 | |
| 159 | bt2::Stream::Shared stream; |
| 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 | |
| 167 | ctf_fs_ds_index index; |
| 168 | }; |
| 169 | |
| 170 | ctf_fs_ds_file::UP ctf_fs_ds_file_create(const char *path, const bt2c::Logger& parentLogger); |
| 171 | |
| 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); |
| 174 | |
| 175 | namespace ctf { |
| 176 | namespace src { |
| 177 | namespace fs { |
| 178 | |
| 179 | struct Medium : public ctf::src::Medium |
| 180 | { |
| 181 | explicit Medium(const ctf_fs_ds_index& index, const bt2c::Logger& parentLogger); |
| 182 | |
| 183 | ~Medium() = default; |
| 184 | Medium(const Medium&) = delete; |
| 185 | Medium& operator=(const Medium&) = delete; |
| 186 | |
| 187 | ctf::src::Buf buf(bt2c::DataLen offset, bt2c::DataLen minSize) override; |
| 188 | |
| 189 | private: |
| 190 | ctf_fs_ds_index::EntriesT::const_iterator |
| 191 | _mFindIndexEntryForOffset(bt2c::DataLen offsetInStream) const noexcept; |
| 192 | |
| 193 | const ctf_fs_ds_index& _mIndex; |
| 194 | bt2c::Logger _mLogger; |
| 195 | ctf_fs_ds_file::UP _mCurrentDsFile; |
| 196 | }; |
| 197 | |
| 198 | } /* namespace fs */ |
| 199 | } /* namespace src */ |
| 200 | } /* namespace ctf */ |
| 201 | |
| 202 | #endif /* BABELTRACE_PLUGINS_CTF_FS_SRC_DATA_STREAM_FILE_HPP */ |