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