cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / plugins / ctf / fs-src / query.cpp
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * Babeltrace CTF file system Reader Component queries
7 */
8
9 #include <glib.h>
10 #include <glib/gstdio.h>
11 #include <sys/types.h>
12
13 #include <babeltrace2/babeltrace.h>
14
15 #include "cpp-common/bt2/exc.hpp"
16 #include "cpp-common/bt2c/glib-up.hpp"
17 #include "cpp-common/bt2c/libc-up.hpp"
18
19 #include "plugins/common/param-validation/param-validation.h"
20
21 #include "../common/src/metadata/tsdl/decoder.hpp"
22 #include "data-stream-file.hpp"
23 #include "fs.hpp"
24 #include "metadata.hpp"
25 #include "query.hpp"
26
27 #define METADATA_TEXT_SIG "/* CTF 1.8"
28
29 struct range
30 {
31 int64_t begin_ns = 0;
32 int64_t end_ns = 0;
33 bool set = false;
34 };
35
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
41 bt2::Value::Shared metadata_info_query(const bt2::ConstMapValue params, const bt2c::Logger& logger)
42 {
43 gchar *validateError = nullptr;
44 const auto validationStatus = bt_param_validation_validate(
45 params.libObjPtr(), metadataInfoQueryParamsDesc, &validateError);
46
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);
53 }
54
55 const auto path = params["path"]->asString().value();
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);
60 }
61
62 bool is_packetized;
63 int bo;
64 int ret = ctf_metadata_decoder_is_packetized(metadataFp.get(), &is_packetized, &bo, logger);
65
66 if (ret) {
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);
70 }
71
72 ctf_metadata_decoder_config decoder_cfg {logger};
73 decoder_cfg.keep_plain_text = true;
74 ctf_metadata_decoder_up decoder = ctf_metadata_decoder_create(&decoder_cfg);
75 if (!decoder) {
76 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(
77 logger, bt2::Error, "Cannot create metadata decoder: path=\"{}}\".", path);
78 }
79
80 rewind(metadataFp.get());
81 ctf_metadata_decoder_status decoder_status =
82 ctf_metadata_decoder_append_content(decoder.get(), metadataFp.get());
83 if (decoder_status) {
84 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(
85 logger, bt2::Error, "Cannot update metadata decoder's content: path=\"{}\".", path);
86 }
87
88 const char *plain_text = ctf_metadata_decoder_get_text(decoder.get());
89 std::string metadata_text;
90
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";
94 }
95
96 metadata_text += plain_text;
97
98 const auto result = bt2::MapValue::create();
99 result->insert("text", metadata_text);
100 result->insert("is-packetized", is_packetized);
101
102 return result;
103 }
104
105 static void add_range(const bt2::MapValue info, const range& range, const char *range_name)
106 {
107 if (!range.set) {
108 /* Not an error. */
109 return;
110 }
111
112 const auto rangeMap = info.insertEmptyMap(range_name);
113 rangeMap.insert("begin", range.begin_ns);
114 rangeMap.insert("end", range.end_ns);
115 }
116
117 static void populate_stream_info(struct ctf_fs_ds_file_group *group, const bt2::MapValue groupInfo)
118 {
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.entries.empty());
126
127 /* First entry. */
128 const auto& first_ds_index_entry = group->index.entries.front();
129
130 /* Last entry. */
131 const auto& last_ds_index_entry = group->index.entries.back();
132
133 range stream_range;
134 stream_range.begin_ns = first_ds_index_entry.timestamp_begin_ns;
135 stream_range.end_ns = last_ds_index_entry.timestamp_end_ns;
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 = stream_range.begin_ns != UINT64_C(-1) && stream_range.end_ns != UINT64_C(-1);
143
144 add_range(groupInfo, stream_range, "range-ns");
145 groupInfo.insert("port-name", ctf_fs_make_port_name(group));
146 }
147
148 static void populate_trace_info(const struct ctf_fs_trace *trace, const bt2::MapValue traceInfo,
149 const bt2c::Logger& logger)
150 {
151 /* Add trace range info only if it contains streams. */
152 if (trace->ds_file_groups.empty()) {
153 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger, bt2::Error,
154 "Trace has no streams: trace-path={}", trace->path);
155 }
156
157 const auto fileGroups = traceInfo.insertEmptyArray("stream-infos");
158
159 /* Find range of all stream groups, and of the trace. */
160 for (const auto& group : trace->ds_file_groups) {
161 const auto groupInfo = fileGroups.appendEmptyMap();
162 populate_stream_info(group.get(), groupInfo);
163 }
164 }
165
166 bt2::Value::Shared trace_infos_query(const bt2::ConstMapValue params, const bt2c::Logger& logger)
167 {
168 const auto parameters = read_src_fs_parameters(params, logger);
169 ctf_fs_component ctf_fs {parameters.clkClsCfg, logger};
170
171 if (ctf_fs_component_create_ctf_fs_trace(
172 &ctf_fs, parameters.inputs,
173 parameters.traceName ? parameters.traceName->c_str() : nullptr, nullptr)) {
174 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger, bt2::Error, "Failed to create trace");
175 }
176
177 const auto result = bt2::ArrayValue::create();
178 const auto traceInfo = result->appendEmptyMap();
179 populate_trace_info(ctf_fs.trace.get(), traceInfo, logger);
180
181 return result;
182 }
183
184 static bt_param_validation_map_value_entry_descr supportInfoQueryParamsDesc[] = {
185 {"type", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY,
186 bt_param_validation_value_descr::makeString()},
187 {"input", BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY,
188 bt_param_validation_value_descr::makeString()},
189 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END};
190
191 bt2::Value::Shared support_info_query(const bt2::ConstMapValue params, const bt2c::Logger& logger)
192 {
193 gchar *validateError = NULL;
194 const auto validationStatus = bt_param_validation_validate(
195 params.libObjPtr(), supportInfoQueryParamsDesc, &validateError);
196
197 if (validationStatus == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) {
198 throw bt2::MemoryError {};
199 } else if (validationStatus == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) {
200 const bt2c::GCharUP deleter {validateError};
201
202 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger, bt2::Error, "{}", validateError);
203 }
204
205 const auto type = params["type"]->asString().value();
206
207 if (strcmp(type, "directory") != 0) {
208 /*
209 * The input type is not a directory so we are 100% sure it's not a CTF
210 * 1.8 trace as it would need a directory with at least 1 metadata file
211 * and 1 data stream file.
212 */
213 const auto result = bt2::MapValue::create();
214 result->insert("weight", 0.0f);
215 return result;
216 }
217
218 const auto input = params["input"]->asString().value();
219
220 bt2c::GCharUP metadataPath {g_build_filename(input, CTF_FS_METADATA_FILENAME, NULL)};
221 if (!metadataPath) {
222 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger, bt2::Error, "Failed to read parameters");
223 }
224
225 double weight = 0;
226 char uuid_str[BT_UUID_STR_LEN + 1];
227 bool has_uuid = false;
228 bt2c::FileUP metadataFile {g_fopen(metadataPath.get(), "rb")};
229 if (metadataFile) {
230 enum ctf_metadata_decoder_status decoder_status;
231 bt_uuid_t uuid;
232
233 ctf_metadata_decoder_config metadata_decoder_config {logger};
234
235 ctf_metadata_decoder_up metadata_decoder =
236 ctf_metadata_decoder_create(&metadata_decoder_config);
237 if (!metadata_decoder) {
238 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(logger, bt2::Error,
239 "Failed to create metadata decoder");
240 }
241
242 decoder_status =
243 ctf_metadata_decoder_append_content(metadata_decoder.get(), metadataFile.get());
244 if (decoder_status != CTF_METADATA_DECODER_STATUS_OK) {
245 BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(
246 logger, bt2::Error, "Failed to append metadata content: metadata-decoder-status={}",
247 decoder_status);
248 }
249
250 /*
251 * We were able to parse the metadata file, so we are
252 * confident it's a CTF trace.
253 */
254 weight = 0.75;
255
256 /* If the trace has a UUID, return the stringified UUID as the group. */
257 if (ctf_metadata_decoder_get_trace_class_uuid(metadata_decoder.get(), uuid) == 0) {
258 bt_uuid_to_str(uuid, uuid_str);
259 has_uuid = true;
260 }
261 }
262
263 const auto result = bt2::MapValue::create();
264 result->insert("weight", weight);
265
266 /* We are not supposed to have weight == 0 and a UUID. */
267 BT_ASSERT(weight > 0 || !has_uuid);
268
269 if (weight > 0 && has_uuid) {
270 result->insert("group", uuid_str);
271 }
272
273 return result;
274 }
This page took 0.035045 seconds and 4 git commands to generate.