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