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