6625ec7d403e7fdbd1bd6508b253d1ff47d76372
[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/bt2c/libc-up.hpp"
16
17 #include "../common/src/metadata/tsdl/decoder.hpp"
18 #include "fs.hpp"
19 #include "query.hpp"
20
21 #define METADATA_TEXT_SIG "/* CTF 1.8"
22
23 struct range
24 {
25 int64_t begin_ns = 0;
26 int64_t end_ns = 0;
27 bool set = false;
28 };
29
30 bt_component_class_query_method_status metadata_info_query(const bt_value *params,
31 const bt2c::Logger& logger,
32 const bt_value **user_result)
33 {
34 bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
35 bt_value *result = NULL;
36 const bt_value *path_value = NULL;
37 bt2c::FileUP metadata_fp;
38 int ret;
39 int bo;
40 const char *path;
41 bool is_packetized;
42 ctf_metadata_decoder_up decoder;
43 ctf_metadata_decoder_config decoder_cfg {logger};
44 enum ctf_metadata_decoder_status decoder_status;
45 GString *g_metadata_text = NULL;
46 const char *plaintext;
47
48 result = bt_value_map_create();
49 if (!result) {
50 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
51 goto error;
52 }
53
54 BT_ASSERT(params);
55
56 if (!bt_value_is_map(params)) {
57 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Query parameters is not a map value object.");
58 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
59 goto error;
60 }
61
62 path_value = bt_value_map_borrow_entry_value_const(params, "path");
63 if (!path_value) {
64 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Mandatory `path` parameter missing");
65 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
66 goto error;
67 }
68
69 if (!bt_value_is_string(path_value)) {
70 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "`path` parameter is required to be a string value");
71 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
72 goto error;
73 }
74
75 path = bt_value_string_get(path_value);
76
77 BT_ASSERT(path);
78 metadata_fp.reset(ctf_fs_metadata_open_file(path, logger));
79 if (!metadata_fp) {
80 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot open trace metadata: path=\"{}\".", path);
81 goto error;
82 }
83
84 ret = ctf_metadata_decoder_is_packetized(metadata_fp.get(), &is_packetized, &bo, logger);
85 if (ret) {
86 BT_CPPLOGE_APPEND_CAUSE_SPEC(
87 logger, "Cannot check whether or not the metadata stream is packetized: path=\"{}\".",
88 path);
89 goto error;
90 }
91
92 decoder_cfg.keep_plain_text = true;
93 decoder = ctf_metadata_decoder_create(&decoder_cfg);
94 if (!decoder) {
95 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot create metadata decoder: path=\"{}\".", path);
96 goto error;
97 }
98
99 rewind(metadata_fp.get());
100 decoder_status = ctf_metadata_decoder_append_content(decoder.get(), metadata_fp.get());
101 if (decoder_status) {
102 BT_CPPLOGE_APPEND_CAUSE_SPEC(
103 logger, "Cannot update metadata decoder's content: path=\"{}\".", path);
104 goto error;
105 }
106
107 plaintext = ctf_metadata_decoder_get_text(decoder.get());
108 g_metadata_text = g_string_new(NULL);
109
110 if (!g_metadata_text) {
111 goto error;
112 }
113
114 if (strncmp(plaintext, METADATA_TEXT_SIG, sizeof(METADATA_TEXT_SIG) - 1) != 0) {
115 g_string_assign(g_metadata_text, METADATA_TEXT_SIG);
116 g_string_append(g_metadata_text, " */\n\n");
117 }
118
119 g_string_append(g_metadata_text, plaintext);
120 ret = bt_value_map_insert_string_entry(result, "text", g_metadata_text->str);
121 if (ret) {
122 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Cannot insert metadata text into query result.");
123 goto error;
124 }
125
126 ret = bt_value_map_insert_bool_entry(result, "is-packetized", is_packetized);
127 if (ret) {
128 BT_CPPLOGE_APPEND_CAUSE_SPEC(
129 logger, "Cannot insert \"is-packetized\" attribute into query result.");
130 goto error;
131 }
132
133 goto end;
134
135 error:
136 BT_VALUE_PUT_REF_AND_RESET(result);
137 result = NULL;
138
139 if (status >= 0) {
140 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
141 }
142
143 end:
144 if (g_metadata_text) {
145 g_string_free(g_metadata_text, TRUE);
146 }
147
148 *user_result = result;
149 return status;
150 }
151
152 static int add_range(bt_value *info, struct range *range, const char *range_name)
153 {
154 int ret = 0;
155 bt_value_map_insert_entry_status status;
156 bt_value *range_map;
157
158 if (!range->set) {
159 /* Not an error. */
160 goto end;
161 }
162
163 status = bt_value_map_insert_empty_map_entry(info, range_name, &range_map);
164 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
165 ret = -1;
166 goto end;
167 }
168
169 status = bt_value_map_insert_signed_integer_entry(range_map, "begin", range->begin_ns);
170 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
171 ret = -1;
172 goto end;
173 }
174
175 status = bt_value_map_insert_signed_integer_entry(range_map, "end", range->end_ns);
176 if (status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
177 ret = -1;
178 goto end;
179 }
180
181 end:
182 return ret;
183 }
184
185 static int populate_stream_info(struct ctf_fs_ds_file_group *group, bt_value *group_info,
186 struct range *stream_range)
187 {
188 int ret = 0;
189 bt_value_map_insert_entry_status insert_status;
190 struct ctf_fs_ds_index_entry *first_ds_index_entry, *last_ds_index_entry;
191 bt2c::GCharUP port_name;
192
193 /*
194 * Since each `struct ctf_fs_ds_file_group` has a sorted array of
195 * `struct ctf_fs_ds_index_entry`, we can compute the stream range from
196 * the timestamp_begin of the first index entry and the timestamp_end
197 * of the last index entry.
198 */
199 BT_ASSERT(group->index);
200 BT_ASSERT(group->index->entries);
201 BT_ASSERT(group->index->entries->len > 0);
202
203 /* First entry. */
204 first_ds_index_entry =
205 (struct ctf_fs_ds_index_entry *) g_ptr_array_index(group->index->entries, 0);
206
207 /* Last entry. */
208 last_ds_index_entry = (struct ctf_fs_ds_index_entry *) g_ptr_array_index(
209 group->index->entries, group->index->entries->len - 1);
210
211 stream_range->begin_ns = first_ds_index_entry->timestamp_begin_ns;
212 stream_range->end_ns = last_ds_index_entry->timestamp_end_ns;
213
214 /*
215 * If any of the begin and end timestamps is not set it means that
216 * packets don't include `timestamp_begin` _and_ `timestamp_end` fields
217 * in their packet context so we can't set the range.
218 */
219 stream_range->set =
220 stream_range->begin_ns != UINT64_C(-1) && stream_range->end_ns != UINT64_C(-1);
221
222 ret = add_range(group_info, stream_range, "range-ns");
223 if (ret) {
224 goto end;
225 }
226
227 port_name = ctf_fs_make_port_name(group);
228 if (!port_name) {
229 ret = -1;
230 goto end;
231 }
232
233 insert_status = bt_value_map_insert_string_entry(group_info, "port-name", port_name.get());
234 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
235 ret = -1;
236 goto end;
237 }
238
239 end:
240 return ret;
241 }
242
243 static int populate_trace_info(const struct ctf_fs_trace *trace, bt_value *trace_info,
244 const bt2c::Logger& logger)
245 {
246 int ret = 0;
247 size_t group_idx;
248 bt_value_map_insert_entry_status insert_status;
249 bt_value_array_append_element_status append_status;
250 bt_value *file_groups = NULL;
251
252 BT_ASSERT(trace->ds_file_groups);
253 /* Add trace range info only if it contains streams. */
254 if (trace->ds_file_groups->len == 0) {
255 ret = -1;
256 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Trace has no streams: trace-path={}",
257 trace->path->str);
258 goto end;
259 }
260
261 insert_status = bt_value_map_insert_empty_array_entry(trace_info, "stream-infos", &file_groups);
262 if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
263 ret = -1;
264 goto end;
265 }
266
267 /* Find range of all stream groups, and of the trace. */
268 for (group_idx = 0; group_idx < trace->ds_file_groups->len; group_idx++) {
269 bt_value *group_info;
270 range group_range;
271 ctf_fs_ds_file_group *group =
272 (ctf_fs_ds_file_group *) g_ptr_array_index(trace->ds_file_groups, group_idx);
273
274 append_status = bt_value_array_append_empty_map_element(file_groups, &group_info);
275 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
276 ret = -1;
277 goto end;
278 }
279
280 ret = populate_stream_info(group, group_info, &group_range);
281 if (ret) {
282 goto end;
283 }
284 }
285
286 end:
287 return ret;
288 }
289
290 bt_component_class_query_method_status
291 trace_infos_query(const bt_value *params, const bt2c::Logger& logger, const bt_value **user_result)
292 {
293 ctf_fs_component::UP ctf_fs;
294 bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
295 bt_value *result = NULL;
296 const bt_value *inputs_value = NULL;
297 const bt_value *trace_name_value;
298 int ret = 0;
299 bt_value *trace_info = NULL;
300 bt_value_array_append_element_status append_status;
301
302 BT_ASSERT(params);
303
304 if (!bt_value_is_map(params)) {
305 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Query parameters is not a map value object.");
306 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
307 goto error;
308 }
309
310 ctf_fs = ctf_fs_component_create(logger);
311 if (!ctf_fs) {
312 goto error;
313 }
314
315 if (!read_src_fs_parameters(params, &inputs_value, &trace_name_value, ctf_fs.get())) {
316 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
317 goto error;
318 }
319
320 if (ctf_fs_component_create_ctf_fs_trace(ctf_fs.get(), inputs_value, trace_name_value, NULL)) {
321 goto error;
322 }
323
324 result = bt_value_array_create();
325 if (!result) {
326 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
327 goto error;
328 }
329
330 append_status = bt_value_array_append_empty_map_element(result, &trace_info);
331 if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) {
332 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger, "Failed to create trace info map.");
333 goto error;
334 }
335
336 ret = populate_trace_info(ctf_fs->trace, trace_info, logger);
337 if (ret) {
338 goto error;
339 }
340
341 goto end;
342
343 error:
344 BT_VALUE_PUT_REF_AND_RESET(result);
345
346 if (status >= 0) {
347 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
348 }
349
350 end:
351 *user_result = result;
352 return status;
353 }
354
355 bt_component_class_query_method_status
356 support_info_query(const bt_value *params, const bt2c::Logger& logger, const bt_value **user_result)
357 {
358 const bt_value *input_type_value;
359 const char *input_type;
360 bt_component_class_query_method_status status;
361 bt_value_map_insert_entry_status insert_entry_status;
362 double weight = 0;
363 bt2c::GCharUP metadata_path;
364 bt_value *result = NULL;
365 ctf_metadata_decoder_up metadata_decoder;
366 FILE *metadata_file = NULL;
367 char uuid_str[BT_UUID_STR_LEN + 1];
368 bool has_uuid = false;
369 const bt_value *input_value;
370 const char *input;
371
372 input_type_value = bt_value_map_borrow_entry_value_const(params, "type");
373 BT_ASSERT(input_type_value);
374 BT_ASSERT(bt_value_get_type(input_type_value) == BT_VALUE_TYPE_STRING);
375 input_type = bt_value_string_get(input_type_value);
376
377 if (strcmp(input_type, "directory") != 0) {
378 goto create_result;
379 }
380
381 input_value = bt_value_map_borrow_entry_value_const(params, "input");
382 BT_ASSERT(input_value);
383 BT_ASSERT(bt_value_get_type(input_value) == BT_VALUE_TYPE_STRING);
384 input = bt_value_string_get(input_value);
385
386 metadata_path.reset(g_build_filename(input, CTF_FS_METADATA_FILENAME, NULL));
387 if (!metadata_path) {
388 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
389 goto end;
390 }
391
392 metadata_file = g_fopen(metadata_path.get(), "rb");
393 if (metadata_file) {
394 enum ctf_metadata_decoder_status decoder_status;
395 bt_uuid_t uuid;
396
397 ctf_metadata_decoder_config metadata_decoder_config {logger};
398
399 metadata_decoder = ctf_metadata_decoder_create(&metadata_decoder_config);
400 if (!metadata_decoder) {
401 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
402 goto end;
403 }
404
405 decoder_status = ctf_metadata_decoder_append_content(metadata_decoder.get(), metadata_file);
406 if (decoder_status != CTF_METADATA_DECODER_STATUS_OK) {
407 BT_CPPLOGW_SPEC(logger, "cannot append metadata content: metadata-decoder-status={}",
408 decoder_status);
409 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR;
410 goto end;
411 }
412
413 /*
414 * We were able to parse the metadata file, so we are
415 * confident it's a CTF trace.
416 */
417 weight = 0.75;
418
419 /* If the trace has a UUID, return the stringified UUID as the group. */
420 if (ctf_metadata_decoder_get_trace_class_uuid(metadata_decoder.get(), uuid) == 0) {
421 bt_uuid_to_str(uuid, uuid_str);
422 has_uuid = true;
423 }
424 }
425
426 create_result:
427 result = bt_value_map_create();
428 if (!result) {
429 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR;
430 goto end;
431 }
432
433 insert_entry_status = bt_value_map_insert_real_entry(result, "weight", weight);
434 if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
435 status = (bt_component_class_query_method_status) insert_entry_status;
436 goto end;
437 }
438
439 /* We are not supposed to have weight == 0 and a UUID. */
440 BT_ASSERT(weight > 0 || !has_uuid);
441
442 if (weight > 0 && has_uuid) {
443 insert_entry_status = bt_value_map_insert_string_entry(result, "group", uuid_str);
444 if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) {
445 status = (bt_component_class_query_method_status) insert_entry_status;
446 goto end;
447 }
448 }
449
450 *user_result = result;
451 result = NULL;
452 status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK;
453
454 end:
455 bt_value_put_ref(result);
456
457 return status;
458 }
This page took 0.037876 seconds and 3 git commands to generate.