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