2 * SPDX-License-Identifier: MIT
4 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Babeltrace CTF file system Reader Component queries
9 #define BT_LOG_OUTPUT_LEVEL log_level
10 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/QUERY"
11 #include "logging/log.h"
16 #include <glib/gstdio.h>
18 #include <sys/types.h>
20 #include "common/assert.h"
22 #include "../common/metadata/decoder.h"
23 #include "common/common.h"
24 #include "common/macros.h"
25 #include <babeltrace2/babeltrace.h>
27 #include "logging/comp-logging.h"
29 #define METADATA_TEXT_SIG "/* CTF 1.8"
38 bt_component_class_query_method_status
metadata_info_query(
39 bt_self_component_class_source
*self_comp_class_src
,
40 const bt_value
*params
, bt_logging_level log_level
,
41 const bt_value
**user_result
)
43 bt_component_class_query_method_status status
=
44 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
45 bt_self_component_class
*self_comp_class
=
46 bt_self_component_class_source_as_self_component_class(self_comp_class_src
);
47 bt_value
*result
= NULL
;
48 const bt_value
*path_value
= NULL
;
49 FILE *metadata_fp
= NULL
;
54 struct ctf_metadata_decoder
*decoder
= NULL
;
55 struct ctf_metadata_decoder_config decoder_cfg
= { 0 };
56 enum ctf_metadata_decoder_status decoder_status
;
58 result
= bt_value_map_create();
60 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
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
;
73 path_value
= bt_value_map_borrow_entry_value_const(params
, "path");
75 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
76 "Mandatory `path` parameter missing");
77 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
81 if (!bt_value_is_string(path_value
)) {
82 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
83 "`path` parameter is required to be a string value");
84 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
88 path
= bt_value_string_get(path_value
);
91 metadata_fp
= ctf_fs_metadata_open_file(path
);
93 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
94 "Cannot open trace metadata: path=\"%s\".", path
);
98 ret
= ctf_metadata_decoder_is_packetized(metadata_fp
, &is_packetized
,
99 &bo
, log_level
, NULL
);
101 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
102 "Cannot check whether or not the metadata stream is packetized: path=\"%s\".",
107 decoder_cfg
.log_level
= log_level
;
108 decoder_cfg
.keep_plain_text
= true;
109 decoder
= ctf_metadata_decoder_create(&decoder_cfg
);
111 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
112 "Cannot create metadata decoder: path=\"%s\".", path
);
117 decoder_status
= ctf_metadata_decoder_append_content(decoder
,
119 if (decoder_status
) {
120 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
121 "Cannot update metadata decoder's content: path=\"%s\".",
126 ret
= bt_value_map_insert_string_entry(result
, "text",
127 ctf_metadata_decoder_get_text(decoder
));
129 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
130 "Cannot insert metadata text into query result.");
134 ret
= bt_value_map_insert_bool_entry(result
, "is-packetized",
137 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
138 "Cannot insert \"is-packetized\" attribute into query result.");
145 BT_VALUE_PUT_REF_AND_RESET(result
);
149 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
153 ctf_metadata_decoder_destroy(decoder
);
156 ret
= fclose(metadata_fp
);
158 BT_LOGE_ERRNO("Cannot close metatada file stream",
159 ": path=\"%s\"", path
);
163 *user_result
= result
;
168 int add_range(bt_value
*info
, struct range
*range
,
169 const char *range_name
)
172 bt_value_map_insert_entry_status status
;
180 status
= bt_value_map_insert_empty_map_entry(info
, range_name
,
182 if (status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
187 status
= bt_value_map_insert_signed_integer_entry(range_map
, "begin",
189 if (status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
194 status
= bt_value_map_insert_signed_integer_entry(range_map
, "end",
196 if (status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
206 int populate_stream_info(struct ctf_fs_ds_file_group
*group
,
207 bt_value
*group_info
, struct range
*stream_range
)
210 bt_value_map_insert_entry_status insert_status
;
211 struct ctf_fs_ds_index_entry
*first_ds_index_entry
, *last_ds_index_entry
;
212 gchar
*port_name
= NULL
;
215 * Since each `struct ctf_fs_ds_file_group` has a sorted array of
216 * `struct ctf_fs_ds_index_entry`, we can compute the stream range from
217 * the timestamp_begin of the first index entry and the timestamp_end
218 * of the last index entry.
220 BT_ASSERT(group
->index
);
221 BT_ASSERT(group
->index
->entries
);
222 BT_ASSERT(group
->index
->entries
->len
> 0);
225 first_ds_index_entry
= (struct ctf_fs_ds_index_entry
*) g_ptr_array_index(
226 group
->index
->entries
, 0);
229 last_ds_index_entry
= (struct ctf_fs_ds_index_entry
*) g_ptr_array_index(
230 group
->index
->entries
, group
->index
->entries
->len
- 1);
232 stream_range
->begin_ns
= first_ds_index_entry
->timestamp_begin_ns
;
233 stream_range
->end_ns
= last_ds_index_entry
->timestamp_end_ns
;
236 * If any of the begin and end timestamps is not set it means that
237 * packets don't include `timestamp_begin` _and_ `timestamp_end` fields
238 * in their packet context so we can't set the range.
240 stream_range
->set
= stream_range
->begin_ns
!= UINT64_C(-1) &&
241 stream_range
->end_ns
!= UINT64_C(-1);
243 ret
= add_range(group_info
, stream_range
, "range-ns");
248 port_name
= ctf_fs_make_port_name(group
);
254 insert_status
= bt_value_map_insert_string_entry(group_info
,
255 "port-name", port_name
);
256 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
267 int populate_trace_info(const struct ctf_fs_trace
*trace
, bt_value
*trace_info
,
268 bt_logging_level log_level
,
269 bt_self_component_class
*self_comp_class
)
273 bt_value_map_insert_entry_status insert_status
;
274 bt_value_array_append_element_status append_status
;
275 bt_value
*file_groups
= NULL
;
277 BT_ASSERT(trace
->ds_file_groups
);
278 /* Add trace range info only if it contains streams. */
279 if (trace
->ds_file_groups
->len
== 0) {
281 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
282 "Trace has no streams: trace-path=%s", trace
->path
->str
);
286 insert_status
= bt_value_map_insert_empty_array_entry(trace_info
,
287 "stream-infos", &file_groups
);
288 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
293 /* Find range of all stream groups, and of the trace. */
294 for (group_idx
= 0; group_idx
< trace
->ds_file_groups
->len
;
296 bt_value
*group_info
;
297 struct range group_range
= { .set
= false };
298 struct ctf_fs_ds_file_group
*group
= g_ptr_array_index(
299 trace
->ds_file_groups
, group_idx
);
301 append_status
= bt_value_array_append_empty_map_element(
302 file_groups
, &group_info
);
303 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
308 ret
= populate_stream_info(group
, group_info
, &group_range
);
319 bt_component_class_query_method_status
trace_infos_query(
320 bt_self_component_class_source
*self_comp_class_src
,
321 const bt_value
*params
, bt_logging_level log_level
,
322 const bt_value
**user_result
)
324 struct ctf_fs_component
*ctf_fs
= NULL
;
325 bt_component_class_query_method_status status
=
326 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
327 bt_self_component_class
*self_comp_class
=
328 bt_self_component_class_source_as_self_component_class(
329 self_comp_class_src
);
330 bt_value
*result
= NULL
;
331 const bt_value
*inputs_value
= NULL
;
332 const bt_value
*trace_name_value
;
334 bt_value
*trace_info
= NULL
;
335 bt_value_array_append_element_status append_status
;
339 if (!bt_value_is_map(params
)) {
340 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
341 "Query parameters is not a map value object.");
342 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
346 ctf_fs
= ctf_fs_component_create(log_level
, NULL
);
351 if (!read_src_fs_parameters(params
, &inputs_value
, &trace_name_value
,
352 ctf_fs
, NULL
, self_comp_class
)) {
353 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
357 if (ctf_fs_component_create_ctf_fs_trace(ctf_fs
, inputs_value
,
358 trace_name_value
, NULL
, self_comp_class
)) {
362 result
= bt_value_array_create();
364 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
368 append_status
= bt_value_array_append_empty_map_element(result
,
370 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
371 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
372 "Failed to create trace info map.");
376 ret
= populate_trace_info(ctf_fs
->trace
, trace_info
, log_level
,
385 BT_VALUE_PUT_REF_AND_RESET(result
);
388 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
393 ctf_fs_destroy(ctf_fs
);
397 *user_result
= result
;
402 bt_component_class_query_method_status
support_info_query(
403 bt_self_component_class_source
*comp_class
,
404 const bt_value
*params
, bt_logging_level log_level
,
405 const bt_value
**user_result
)
407 const bt_value
*input_type_value
;
408 const char *input_type
;
409 bt_component_class_query_method_status status
;
410 bt_value_map_insert_entry_status insert_entry_status
;
412 gchar
*metadata_path
= NULL
;
413 bt_value
*result
= NULL
;
414 struct ctf_metadata_decoder
*metadata_decoder
= NULL
;
415 FILE *metadata_file
= NULL
;
416 char uuid_str
[BT_UUID_STR_LEN
+ 1];
417 bool has_uuid
= false;
418 const bt_value
*input_value
;
421 input_type_value
= bt_value_map_borrow_entry_value_const(params
, "type");
422 BT_ASSERT(input_type_value
);
423 BT_ASSERT(bt_value_get_type(input_type_value
) == BT_VALUE_TYPE_STRING
);
424 input_type
= bt_value_string_get(input_type_value
);
426 if (strcmp(input_type
, "directory") != 0) {
430 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
431 BT_ASSERT(input_value
);
432 BT_ASSERT(bt_value_get_type(input_value
) == BT_VALUE_TYPE_STRING
);
433 input
= bt_value_string_get(input_value
);
435 metadata_path
= g_build_filename(input
, CTF_FS_METADATA_FILENAME
, NULL
);
436 if (!metadata_path
) {
437 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
441 metadata_file
= g_fopen(metadata_path
, "rb");
443 struct ctf_metadata_decoder_config metadata_decoder_config
= { 0 };
444 enum ctf_metadata_decoder_status decoder_status
;
447 metadata_decoder_config
.log_level
= log_level
;
449 metadata_decoder
= ctf_metadata_decoder_create(
450 &metadata_decoder_config
);
451 if (!metadata_decoder
) {
452 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
456 decoder_status
= ctf_metadata_decoder_append_content(
457 metadata_decoder
, metadata_file
);
458 if (decoder_status
!= CTF_METADATA_DECODER_STATUS_OK
) {
459 BT_LOGW("cannot append metadata content: metadata-decoder-status=%d",
461 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
466 * We were able to parse the metadata file, so we are
467 * confident it's a CTF trace.
471 /* If the trace has a UUID, return the stringified UUID as the group. */
472 if (ctf_metadata_decoder_get_trace_class_uuid(metadata_decoder
, uuid
) == 0) {
473 bt_uuid_to_str(uuid
, uuid_str
);
479 result
= bt_value_map_create();
481 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
485 insert_entry_status
= bt_value_map_insert_real_entry(result
, "weight", weight
);
486 if (insert_entry_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
487 status
= (int) insert_entry_status
;
491 /* We are not supposed to have weight == 0 and a UUID. */
492 BT_ASSERT(weight
> 0 || !has_uuid
);
494 if (weight
> 0 && has_uuid
) {
495 insert_entry_status
= bt_value_map_insert_string_entry(result
, "group", uuid_str
);
496 if (insert_entry_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
497 status
= (int) insert_entry_status
;
502 *user_result
= result
;
504 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
507 g_free(metadata_path
);
508 bt_value_put_ref(result
);
509 ctf_metadata_decoder_destroy(metadata_decoder
);