| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright (C) 2016 Philippe Proulx <pproulx@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #ifndef CTF_FS_DS_FILE_H |
| 8 | #define CTF_FS_DS_FILE_H |
| 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/msg-iter/msg-iter.hpp" |
| 24 | #include "file.hpp" |
| 25 | |
| 26 | struct ctf_fs_ds_file_info |
| 27 | { |
| 28 | using UP = std::unique_ptr<ctf_fs_ds_file_info>; |
| 29 | |
| 30 | std::string path; |
| 31 | |
| 32 | /* Guaranteed to be set, as opposed to the index. */ |
| 33 | int64_t begin_ns = 0; |
| 34 | }; |
| 35 | |
| 36 | struct ctf_fs_ds_file |
| 37 | { |
| 38 | using UP = std::unique_ptr<ctf_fs_ds_file>; |
| 39 | |
| 40 | explicit ctf_fs_ds_file(const bt2c::Logger& parentLogger) : |
| 41 | logger {parentLogger, "PLUGIN/SRC.CTF.FS/DS"} |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | ctf_fs_ds_file(const ctf_fs_ds_file&) = delete; |
| 46 | ctf_fs_ds_file& operator=(const ctf_fs_ds_file&) = delete; |
| 47 | ~ctf_fs_ds_file(); |
| 48 | |
| 49 | bt2c::Logger logger; |
| 50 | |
| 51 | /* Weak */ |
| 52 | struct ctf_fs_metadata *metadata = nullptr; |
| 53 | |
| 54 | ctf_fs_file::UP file; |
| 55 | |
| 56 | bt2::Stream::Shared stream; |
| 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 | * Offset, in the current mapping, of the address to return on the next |
| 74 | * request. |
| 75 | */ |
| 76 | off_t request_offset_in_mapping = 0; |
| 77 | }; |
| 78 | |
| 79 | struct ctf_fs_ds_index_entry |
| 80 | { |
| 81 | ctf_fs_ds_index_entry(const bt2c::CStringView pathParam, const bt2c::DataLen offsetParam, |
| 82 | const bt2c::DataLen packetSizeParam) noexcept : |
| 83 | path {pathParam}, |
| 84 | offset {offsetParam}, packetSize {packetSizeParam} |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | /* Weak, belongs to ctf_fs_ds_file_info. */ |
| 89 | const char *path; |
| 90 | |
| 91 | /* Position of the packet from the beginning of the file. */ |
| 92 | bt2c::DataLen offset; |
| 93 | |
| 94 | /* Size of the packet. */ |
| 95 | bt2c::DataLen packetSize; |
| 96 | |
| 97 | /* |
| 98 | * Extracted from the packet context, relative to the respective fields' |
| 99 | * mapped clock classes (in cycles). |
| 100 | */ |
| 101 | uint64_t timestamp_begin = 0, timestamp_end = 0; |
| 102 | |
| 103 | /* |
| 104 | * Converted from the packet context, relative to the trace's EPOCH |
| 105 | * (in ns since EPOCH). |
| 106 | */ |
| 107 | int64_t timestamp_begin_ns = 0, timestamp_end_ns = 0; |
| 108 | |
| 109 | /* |
| 110 | * Packet sequence number, or UINT64_MAX if not present in the index. |
| 111 | */ |
| 112 | uint64_t packet_seq_num = UINT64_MAX; |
| 113 | }; |
| 114 | |
| 115 | struct ctf_fs_ds_index |
| 116 | { |
| 117 | std::vector<ctf_fs_ds_index_entry> entries; |
| 118 | }; |
| 119 | |
| 120 | struct ctf_fs_ds_file_group |
| 121 | { |
| 122 | using UP = std::unique_ptr<ctf_fs_ds_file_group>; |
| 123 | |
| 124 | explicit ctf_fs_ds_file_group(struct ctf_fs_trace * const trace, |
| 125 | ctf_stream_class * const scParam, const uint64_t streamInstanceId, |
| 126 | ctf_fs_ds_index indexParam) noexcept : |
| 127 | |
| 128 | sc {scParam}, |
| 129 | stream_id(streamInstanceId), ctf_fs_trace {trace}, index {std::move(indexParam)} |
| 130 | |
| 131 | { |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Insert ds_file_info in the list of ds_file_infos at the right |
| 136 | * place to keep it sorted. |
| 137 | */ |
| 138 | void insert_ds_file_info_sorted(ctf_fs_ds_file_info::UP ds_file_info); |
| 139 | |
| 140 | /* |
| 141 | * This is an _ordered_ array of data stream file infos which |
| 142 | * belong to this group (a single stream instance). |
| 143 | * |
| 144 | * You can call ctf_fs_ds_file_create() with one of those paths |
| 145 | * and the trace IR stream below. |
| 146 | */ |
| 147 | std::vector<ctf_fs_ds_file_info::UP> ds_file_infos; |
| 148 | |
| 149 | /* Owned by this */ |
| 150 | struct ctf_stream_class *sc = nullptr; |
| 151 | |
| 152 | bt2::Stream::Shared stream; |
| 153 | |
| 154 | /* Stream (instance) ID; -1ULL means none */ |
| 155 | uint64_t stream_id = 0; |
| 156 | |
| 157 | /* Weak, belongs to component */ |
| 158 | struct ctf_fs_trace *ctf_fs_trace = nullptr; |
| 159 | |
| 160 | ctf_fs_ds_index index; |
| 161 | }; |
| 162 | |
| 163 | ctf_fs_ds_file::UP ctf_fs_ds_file_create(ctf_fs_trace *ctf_fs_trace, bt2::Stream::Shared stream, |
| 164 | const char *path, const bt2c::Logger& logger); |
| 165 | |
| 166 | bt2s::optional<ctf_fs_ds_index> ctf_fs_ds_file_build_index(struct ctf_fs_ds_file *ds_file, |
| 167 | struct ctf_fs_ds_file_info *ds_file_info, |
| 168 | struct ctf_msg_iter *msg_iter); |
| 169 | |
| 170 | ctf_fs_ds_file_info::UP ctf_fs_ds_file_info_create(const char *path, int64_t begin_ns); |
| 171 | |
| 172 | /* |
| 173 | * Medium operations to iterate on a single ctf_fs_ds_file. |
| 174 | * |
| 175 | * The data pointer when using this must be a pointer to the ctf_fs_ds_file. |
| 176 | */ |
| 177 | extern struct ctf_msg_iter_medium_ops ctf_fs_ds_file_medops; |
| 178 | |
| 179 | /* |
| 180 | * Medium operations to iterate on the packet of a ctf_fs_ds_group. |
| 181 | * |
| 182 | * The iteration is done based on the index of the group. |
| 183 | * |
| 184 | * The data pointer when using these medops must be a pointer to a ctf_fs_ds |
| 185 | * group_medops_data structure. |
| 186 | */ |
| 187 | extern struct ctf_msg_iter_medium_ops ctf_fs_ds_group_medops; |
| 188 | |
| 189 | struct ctf_fs_ds_group_medops_data_deleter |
| 190 | { |
| 191 | void operator()(struct ctf_fs_ds_group_medops_data *data) noexcept; |
| 192 | }; |
| 193 | |
| 194 | using ctf_fs_ds_group_medops_data_up = |
| 195 | std::unique_ptr<ctf_fs_ds_group_medops_data, ctf_fs_ds_group_medops_data_deleter>; |
| 196 | |
| 197 | enum ctf_msg_iter_medium_status |
| 198 | ctf_fs_ds_group_medops_data_create(struct ctf_fs_ds_file_group *ds_file_group, |
| 199 | bt_self_message_iterator *self_msg_iter, |
| 200 | const bt2c::Logger& logger, ctf_fs_ds_group_medops_data_up& out); |
| 201 | |
| 202 | void ctf_fs_ds_group_medops_data_reset(struct ctf_fs_ds_group_medops_data *data); |
| 203 | |
| 204 | #endif /* CTF_FS_DS_FILE_H */ |