Commit | Line | Data |
---|---|---|
04c0ba87 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
04c0ba87 JG |
3 | * |
4 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
5 | * | |
0235b0db | 6 | * Babeltrace CTF file system Reader Component queries |
04c0ba87 JG |
7 | */ |
8 | ||
c802cacb SM |
9 | #include <glib.h> |
10 | #include <glib/gstdio.h> | |
c802cacb SM |
11 | #include <sys/types.h> |
12 | ||
13 | #include <babeltrace2/babeltrace.h> | |
14 | ||
c02af779 | 15 | #include "cpp-common/bt2/exc.hpp" |
69d91bb3 | 16 | #include "cpp-common/bt2c/glib-up.hpp" |
8d049eb2 SM |
17 | #include "cpp-common/bt2c/libc-up.hpp" |
18 | ||
69d91bb3 SM |
19 | #include "plugins/common/param-validation/param-validation.h" |
20 | ||
5656cea5 | 21 | #include "../common/src/metadata/tsdl/decoder.hpp" |
873c329a | 22 | #include "data-stream-file.hpp" |
087cd0f5 | 23 | #include "fs.hpp" |
cdf7de78 | 24 | #include "metadata.hpp" |
c802cacb | 25 | #include "query.hpp" |
55314f2a | 26 | |
4164020e | 27 | #define METADATA_TEXT_SIG "/* CTF 1.8" |
04c0ba87 | 28 | |
4164020e SM |
29 | struct range |
30 | { | |
31 | int64_t begin_ns = 0; | |
32 | int64_t end_ns = 0; | |
33 | bool set = false; | |
97ade20b JG |
34 | }; |
35 | ||
69d91bb3 SM |
36 | static bt_param_validation_map_value_entry_descr metadataInfoQueryParamsDesc[] = { |
37 | {"path", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, | |
38 | bt_param_validation_value_descr::makeString()}, | |
39 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END}; | |
40 | ||
c02af779 | 41 | bt2::Value::Shared metadata_info_query(const bt2::ConstMapValue params, const bt2c::Logger& logger) |
04c0ba87 | 42 | { |
69d91bb3 SM |
43 | gchar *validateError = nullptr; |
44 | const auto validationStatus = bt_param_validation_validate( | |
45 | params.libObjPtr(), metadataInfoQueryParamsDesc, &validateError); | |
4164020e | 46 | |
69d91bb3 SM |
47 | if (validationStatus == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) { |
48 | throw bt2::MemoryError {}; | |
49 | } else if (validationStatus == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) { | |
50 | const bt2c::GCharUP deleter {validateError}; | |
51 | ||
52 | BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger, bt2::Error, "{}", validateError); | |
4164020e SM |
53 | } |
54 | ||
69d91bb3 | 55 | const auto path = params["path"]->asString().value(); |
c02af779 SM |
56 | bt2c::FileUP metadataFp {ctf_fs_metadata_open_file(path, logger)}; |
57 | if (!metadataFp) { | |
58 | BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger, bt2::Error, | |
59 | "Cannot open trace metadata: path=\"{}\".", path); | |
4164020e SM |
60 | } |
61 | ||
c02af779 SM |
62 | bool is_packetized; |
63 | int bo; | |
64 | int ret = ctf_metadata_decoder_is_packetized(metadataFp.get(), &is_packetized, &bo, logger); | |
4164020e | 65 | |
4164020e | 66 | if (ret) { |
c02af779 SM |
67 | BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC( |
68 | logger, bt2::Error, | |
69 | "Cannot check whether or not the metadata stream is packetized: path=\"{}\".", path); | |
4164020e SM |
70 | } |
71 | ||
c02af779 | 72 | ctf_metadata_decoder_config decoder_cfg {logger}; |
4164020e | 73 | decoder_cfg.keep_plain_text = true; |
c02af779 | 74 | ctf_metadata_decoder_up decoder = ctf_metadata_decoder_create(&decoder_cfg); |
4164020e | 75 | if (!decoder) { |
c02af779 SM |
76 | BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC( |
77 | logger, bt2::Error, "Cannot create metadata decoder: path=\"{}}\".", path); | |
4164020e SM |
78 | } |
79 | ||
c02af779 SM |
80 | rewind(metadataFp.get()); |
81 | ctf_metadata_decoder_status decoder_status = | |
82 | ctf_metadata_decoder_append_content(decoder.get(), metadataFp.get()); | |
4164020e | 83 | if (decoder_status) { |
c02af779 SM |
84 | BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC( |
85 | logger, bt2::Error, "Cannot update metadata decoder's content: path=\"{}\".", path); | |
4164020e SM |
86 | } |
87 | ||
c02af779 SM |
88 | const char *plain_text = ctf_metadata_decoder_get_text(decoder.get()); |
89 | std::string metadata_text; | |
c719eabb | 90 | |
c02af779 SM |
91 | if (strncmp(plain_text, METADATA_TEXT_SIG, sizeof(METADATA_TEXT_SIG) - 1) != 0) { |
92 | metadata_text = METADATA_TEXT_SIG; | |
93 | metadata_text += " */\n\n"; | |
4164020e SM |
94 | } |
95 | ||
c02af779 | 96 | metadata_text += plain_text; |
04c0ba87 | 97 | |
c02af779 SM |
98 | const auto result = bt2::MapValue::create(); |
99 | result->insert("text", metadata_text); | |
100 | result->insert("is-packetized", is_packetized); | |
c7eee084 | 101 | |
c02af779 | 102 | return result; |
97ade20b | 103 | } |
9ec238a8 | 104 | |
c02af779 | 105 | static void add_range(const bt2::MapValue info, struct range *range, const char *range_name) |
97ade20b | 106 | { |
4164020e SM |
107 | if (!range->set) { |
108 | /* Not an error. */ | |
c02af779 | 109 | return; |
4164020e SM |
110 | } |
111 | ||
c02af779 SM |
112 | const auto rangeMap = info.insertEmptyMap(range_name); |
113 | rangeMap.insert("begin", range->begin_ns); | |
114 | rangeMap.insert("end", range->end_ns); | |
97ade20b JG |
115 | } |
116 | ||
c02af779 | 117 | static void populate_stream_info(struct ctf_fs_ds_file_group *group, const bt2::MapValue groupInfo, |
3045bbcb | 118 | struct range *stream_range) |
97ade20b | 119 | { |
4164020e SM |
120 | /* |
121 | * Since each `struct ctf_fs_ds_file_group` has a sorted array of | |
122 | * `struct ctf_fs_ds_index_entry`, we can compute the stream range from | |
123 | * the timestamp_begin of the first index entry and the timestamp_end | |
124 | * of the last index entry. | |
125 | */ | |
c46b32d8 | 126 | BT_ASSERT(!group->index.entries.empty()); |
4164020e SM |
127 | |
128 | /* First entry. */ | |
c46b32d8 | 129 | const auto& first_ds_index_entry = group->index.entries.front(); |
4164020e SM |
130 | |
131 | /* Last entry. */ | |
c46b32d8 | 132 | const auto& last_ds_index_entry = group->index.entries.back(); |
4164020e | 133 | |
0011731e SM |
134 | stream_range->begin_ns = first_ds_index_entry.timestamp_begin_ns; |
135 | stream_range->end_ns = last_ds_index_entry.timestamp_end_ns; | |
4164020e SM |
136 | |
137 | /* | |
138 | * If any of the begin and end timestamps is not set it means that | |
139 | * packets don't include `timestamp_begin` _and_ `timestamp_end` fields | |
140 | * in their packet context so we can't set the range. | |
141 | */ | |
142 | stream_range->set = | |
143 | stream_range->begin_ns != UINT64_C(-1) && stream_range->end_ns != UINT64_C(-1); | |
144 | ||
c02af779 | 145 | add_range(groupInfo, stream_range, "range-ns"); |
3045bbcb | 146 | groupInfo.insert("port-name", ctf_fs_make_port_name(group)); |
97ade20b JG |
147 | } |
148 | ||
c02af779 SM |
149 | static void populate_trace_info(const struct ctf_fs_trace *trace, const bt2::MapValue traceInfo, |
150 | const bt2c::Logger& logger) | |
97ade20b | 151 | { |
4164020e | 152 | /* Add trace range info only if it contains streams. */ |
cdf7de78 | 153 | if (trace->ds_file_groups.empty()) { |
cee8a466 SM |
154 | BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger, bt2::Error, |
155 | "Trace has no streams: trace-path={}", trace->path); | |
4164020e SM |
156 | } |
157 | ||
c02af779 | 158 | const auto fileGroups = traceInfo.insertEmptyArray("stream-infos"); |
4164020e SM |
159 | |
160 | /* Find range of all stream groups, and of the trace. */ | |
cdf7de78 | 161 | for (const auto& group : trace->ds_file_groups) { |
4164020e | 162 | range group_range; |
c02af779 | 163 | const auto groupInfo = fileGroups.appendEmptyMap(); |
3045bbcb | 164 | populate_stream_info(group.get(), groupInfo, &group_range); |
4164020e | 165 | } |
97ade20b JG |
166 | } |
167 | ||
c02af779 | 168 | bt2::Value::Shared trace_infos_query(const bt2::ConstMapValue params, const bt2c::Logger& logger) |
97ade20b | 169 | { |
5e502e99 | 170 | ctf_fs_component ctf_fs {logger}; |
c02af779 SM |
171 | const bt_value *inputs_value = NULL; |
172 | const bt_value *trace_name_value; | |
4164020e | 173 | |
5e502e99 | 174 | if (!read_src_fs_parameters(params.libObjPtr(), &inputs_value, &trace_name_value, &ctf_fs)) { |
c02af779 | 175 | BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger, bt2::Error, "Failed to read parameters"); |
4164020e SM |
176 | } |
177 | ||
5e502e99 | 178 | if (ctf_fs_component_create_ctf_fs_trace(&ctf_fs, inputs_value, trace_name_value, NULL)) { |
c02af779 | 179 | BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger, bt2::Error, "Failed to create trace"); |
4164020e SM |
180 | } |
181 | ||
c02af779 SM |
182 | const auto result = bt2::ArrayValue::create(); |
183 | const auto traceInfo = result->appendEmptyMap(); | |
5e502e99 | 184 | populate_trace_info(ctf_fs.trace.get(), traceInfo, logger); |
c7eee084 | 185 | |
c02af779 | 186 | return result; |
04c0ba87 | 187 | } |
73760435 | 188 | |
69d91bb3 SM |
189 | static bt_param_validation_map_value_entry_descr supportInfoQueryParamsDesc[] = { |
190 | {"type", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, | |
191 | bt_param_validation_value_descr::makeString()}, | |
192 | {"input", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY, | |
193 | bt_param_validation_value_descr::makeString()}, | |
194 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END}; | |
195 | ||
c02af779 | 196 | bt2::Value::Shared support_info_query(const bt2::ConstMapValue params, const bt2c::Logger& logger) |
73760435 | 197 | { |
69d91bb3 SM |
198 | gchar *validateError = NULL; |
199 | const auto validationStatus = bt_param_validation_validate( | |
200 | params.libObjPtr(), supportInfoQueryParamsDesc, &validateError); | |
201 | ||
202 | if (validationStatus == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) { | |
203 | throw bt2::MemoryError {}; | |
204 | } else if (validationStatus == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) { | |
205 | const bt2c::GCharUP deleter {validateError}; | |
206 | ||
207 | BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger, bt2::Error, "{}", validateError); | |
208 | } | |
209 | ||
210 | const auto type = params["type"]->asString().value(); | |
4164020e | 211 | |
c02af779 | 212 | if (strcmp(type, "directory") != 0) { |
69d91bb3 SM |
213 | /* |
214 | * The input type is not a directory so we are 100% sure it's not a CTF | |
215 | * 1.8 trace as it would need a directory with at least 1 metadata file | |
216 | * and 1 data stream file. | |
217 | */ | |
c02af779 SM |
218 | const auto result = bt2::MapValue::create(); |
219 | result->insert("weight", 0.0f); | |
220 | return result; | |
4164020e SM |
221 | } |
222 | ||
69d91bb3 | 223 | const auto input = params["input"]->asString().value(); |
4164020e | 224 | |
c02af779 SM |
225 | bt2c::GCharUP metadataPath {g_build_filename(input, CTF_FS_METADATA_FILENAME, NULL)}; |
226 | if (!metadataPath) { | |
227 | BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger, bt2::Error, "Failed to read parameters"); | |
4164020e SM |
228 | } |
229 | ||
c02af779 SM |
230 | double weight = 0; |
231 | char uuid_str[BT_UUID_STR_LEN + 1]; | |
232 | bool has_uuid = false; | |
233 | bt2c::FileUP metadataFile {g_fopen(metadataPath.get(), "rb")}; | |
234 | if (metadataFile) { | |
4164020e SM |
235 | enum ctf_metadata_decoder_status decoder_status; |
236 | bt_uuid_t uuid; | |
237 | ||
0f5c5d5c | 238 | ctf_metadata_decoder_config metadata_decoder_config {logger}; |
4164020e | 239 | |
c02af779 SM |
240 | ctf_metadata_decoder_up metadata_decoder = |
241 | ctf_metadata_decoder_create(&metadata_decoder_config); | |
4164020e | 242 | if (!metadata_decoder) { |
c02af779 SM |
243 | BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger, bt2::Error, |
244 | "Failed to create metadata decoder"); | |
4164020e SM |
245 | } |
246 | ||
c02af779 SM |
247 | decoder_status = |
248 | ctf_metadata_decoder_append_content(metadata_decoder.get(), metadataFile.get()); | |
4164020e | 249 | if (decoder_status != CTF_METADATA_DECODER_STATUS_OK) { |
c02af779 SM |
250 | BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC( |
251 | logger, bt2::Error, "Failed to append metadata content: metadata-decoder-status={}", | |
252 | decoder_status); | |
4164020e SM |
253 | } |
254 | ||
255 | /* | |
256 | * We were able to parse the metadata file, so we are | |
257 | * confident it's a CTF trace. | |
258 | */ | |
259 | weight = 0.75; | |
260 | ||
261 | /* If the trace has a UUID, return the stringified UUID as the group. */ | |
1fa280c9 | 262 | if (ctf_metadata_decoder_get_trace_class_uuid(metadata_decoder.get(), uuid) == 0) { |
4164020e SM |
263 | bt_uuid_to_str(uuid, uuid_str); |
264 | has_uuid = true; | |
265 | } | |
266 | } | |
73760435 | 267 | |
c02af779 SM |
268 | const auto result = bt2::MapValue::create(); |
269 | result->insert("weight", weight); | |
4164020e SM |
270 | |
271 | /* We are not supposed to have weight == 0 and a UUID. */ | |
272 | BT_ASSERT(weight > 0 || !has_uuid); | |
273 | ||
274 | if (weight > 0 && has_uuid) { | |
c02af779 | 275 | result->insert("group", uuid_str); |
4164020e SM |
276 | } |
277 | ||
c02af779 | 278 | return result; |
73760435 | 279 | } |