src.ctf.fs: remove uneeded check in create_streams_for_trace
[deliverable/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 #define BT_CLOG_CFG logCfg
10 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/QUERY"
11
12 #include "query.hpp"
13 #include <stdbool.h>
14 #include <glib.h>
15 #include <glib/gstdio.h>
16 #include <fcntl.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include "common/assert.h"
20 #include "metadata.hpp"
21 #include "../common/src/metadata/tsdl/decoder.hpp"
22 #include "common/common.h"
23 #include "common/macros.h"
24 #include "plugins/common/param-validation/param-validation.h"
25 #include <babeltrace2/babeltrace.h>
26 #include "fs.hpp"
27 #include "cpp-common/cfg-logging-error-reporting.hpp"
28 #include "cpp-common/cfg-logging-error-reporting-throw.hpp"
29 #include "cpp-common/libc-up.hpp"
30 #include "cpp-common/exc.hpp"
31
32 #define METADATA_TEXT_SIG "/* CTF 1.8"
33
34 struct range
35 {
36 int64_t begin_ns = 0;
37 int64_t end_ns = 0;
38 bool set = false;
39 };
40
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
46 BT_HIDDEN
47 bt2::Value::Shared metadata_info_query(bt2::ConstMapValue params, const bt2_common::LogCfg& logCfg)
48 {
49 gchar *validateError = NULL;
50 auto validationStatus = bt_param_validation_validate(
51 params.libObjPtr(), metadataInfoQueryParamsDesc, &validateError);
52
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());
60 }
61
62 const char *path = params["path"]->asString().value().data();
63
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);
68 }
69
70 int bo;
71 bool is_packetized;
72 int ret = ctf_metadata_decoder_is_packetized(metadataFp.get(), &is_packetized, &bo, logCfg);
73 if (ret) {
74 BT_CLOGE_APPEND_CAUSE_AND_THROW(
75 bt2_common::Error,
76 "Cannot check whether or not the metadata stream is packetized: path=\"%s\".", path);
77 }
78
79 ctf_metadata_decoder_config decoder_cfg(logCfg);
80 decoder_cfg.keep_plain_text = true;
81 ctf_metadata_decoder_up decoder = ctf_metadata_decoder_create(&decoder_cfg);
82 if (!decoder) {
83 BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2_common::Error,
84 "Cannot create metadata decoder: path=\"%s\".", path);
85 }
86
87 rewind(metadataFp.get());
88 ctf_metadata_decoder_status decoder_status =
89 ctf_metadata_decoder_append_content(decoder.get(), metadataFp.get());
90 if (decoder_status) {
91 BT_CLOGE_APPEND_CAUSE_AND_THROW(
92 bt2_common::Error, "Cannot update metadata decoder's content: path=\"%s\".", path);
93 }
94
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);
98
99 return result;
100 }
101
102 static void add_range(bt2::MapValue info, struct range *range, const char *range_name)
103 {
104 if (!range->set) {
105 /* Not an error. */
106 return;
107 }
108
109 bt2::MapValue rangeMap = info.insertEmptyMap(range_name);
110 rangeMap.insert("begin", range->begin_ns);
111 rangeMap.insert("end", range->end_ns);
112 }
113
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)
116 {
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. */
128 ctf_fs_ds_index_entry *first_ds_index_entry =
129 (struct ctf_fs_ds_index_entry *) g_ptr_array_index(group->index->entries, 0);
130
131 /* Last entry. */
132 ctf_fs_ds_index_entry *last_ds_index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index(
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
146 add_range(groupInfo, stream_range, "range-ns");
147
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");
151 }
152
153 groupInfo.insert("port-name", portName.get());
154 }
155
156 static void populate_trace_info(const struct ctf_fs_trace *trace, bt2::MapValue traceInfo,
157 const bt2_common::LogCfg& logCfg)
158 {
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) {
162 BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2_common::Error, "Trace has no streams: trace-path=%s",
163 trace->path->str);
164 }
165
166 bt2::ArrayValue fileGroups = traceInfo.insertEmptyArray("stream-infos");
167
168 /* Find range of all stream groups, and of the trace. */
169 for (size_t group_idx = 0; group_idx < trace->ds_file_groups->len; group_idx++) {
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
174 bt2::MapValue groupInfo = fileGroups.appendEmptyMap();
175 populate_stream_info(group, groupInfo, &group_range, logCfg);
176 }
177 }
178
179 BT_HIDDEN
180 bt2::Value::Shared trace_infos_query(bt2::ConstMapValue params, const bt2_common::LogCfg& logCfg)
181 {
182 ctf_fs_component::UP ctf_fs = ctf_fs_component_create(logCfg);
183 if (!ctf_fs) {
184 BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2_common::Error, "Cannot create ctf_fs_component");
185 }
186
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");
193 }
194
195 if (ctf_fs_component_create_ctf_fs_trace(ctf_fs.get(), inputs_value, trace_name_value,
196 nullptr)) {
197 BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2_common::Error, "Failed to create trace");
198 }
199
200 bt2::ArrayValue::Shared result = bt2::ArrayValue::create();
201 bt2::MapValue traceInfo = result->appendEmptyMap();
202 populate_trace_info(ctf_fs->trace, traceInfo, logCfg);
203
204 return result;
205 }
206
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
214 BT_HIDDEN
215 bt2::Value::Shared support_info_query(bt2::ConstMapValue params, const bt2_common::LogCfg& logCfg)
216 {
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();
231
232 if (type != "directory") {
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 */
238 bt2::MapValue::Shared result = bt2::MapValue::create();
239 result->insert("weight", 0.0f);
240 return result;
241 }
242
243 bpstd::string_view input = params["input"]->asString().value();
244
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");
249 }
250
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) {
256 enum ctf_metadata_decoder_status decoder_status;
257 bt_uuid_t uuid;
258
259 ctf_metadata_decoder_config metadata_decoder_config(logCfg);
260
261 ctf_metadata_decoder_up metadata_decoder =
262 ctf_metadata_decoder_create(&metadata_decoder_config);
263 if (!metadata_decoder) {
264 BT_CLOGE_APPEND_CAUSE_AND_THROW(bt2_common::Error, "Failed to create metadata decoder");
265 }
266
267 decoder_status =
268 ctf_metadata_decoder_append_content(metadata_decoder.get(), metadataFile.get());
269 if (decoder_status != CTF_METADATA_DECODER_STATUS_OK) {
270 BT_CLOGE_APPEND_CAUSE_AND_THROW(
271 bt2_common::Error, "Failed to append metadata content: metadata-decoder-status=%d",
272 decoder_status);
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. */
282 if (ctf_metadata_decoder_get_trace_class_uuid(metadata_decoder.get(), uuid) == 0) {
283 bt_uuid_to_str(uuid, uuid_str);
284 has_uuid = true;
285 }
286 }
287
288 bt2::MapValue::Shared result = bt2::MapValue::create();
289 result->insert("weight", weight);
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) {
295 result->insert("group", uuid_str);
296 }
297
298 return result;
299 }
This page took 0.036343 seconds and 5 git commands to generate.