2 * SPDX-License-Identifier: MIT
4 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Babeltrace CTF file system Reader Component queries
10 #include <glib/gstdio.h>
11 #include <sys/types.h>
13 #include <babeltrace2/babeltrace.h>
15 #include "cpp-common/bt2c/libc-up.hpp"
17 #include "../common/src/metadata/tsdl/decoder.hpp"
21 #define METADATA_TEXT_SIG "/* CTF 1.8"
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
)
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
;
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
;
48 result
= bt_value_map_create();
50 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
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
;
62 path_value
= bt_value_map_borrow_entry_value_const(params
, "path");
64 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Mandatory `path` parameter missing");
65 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
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
;
75 path
= bt_value_string_get(path_value
);
78 metadata_fp
.reset(ctf_fs_metadata_open_file(path
, logger
));
80 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Cannot open trace metadata: path=\"{}\".", path
);
84 ret
= ctf_metadata_decoder_is_packetized(metadata_fp
.get(), &is_packetized
, &bo
, logger
);
86 BT_CPPLOGE_APPEND_CAUSE_SPEC(
87 logger
, "Cannot check whether or not the metadata stream is packetized: path=\"{}\".",
92 decoder_cfg
.keep_plain_text
= true;
93 decoder
= ctf_metadata_decoder_create(&decoder_cfg
);
95 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Cannot create metadata decoder: path=\"{}\".", path
);
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
);
107 plaintext
= ctf_metadata_decoder_get_text(decoder
.get());
108 g_metadata_text
= g_string_new(NULL
);
110 if (!g_metadata_text
) {
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");
119 g_string_append(g_metadata_text
, plaintext
);
120 ret
= bt_value_map_insert_string_entry(result
, "text", g_metadata_text
->str
);
122 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Cannot insert metadata text into query result.");
126 ret
= bt_value_map_insert_bool_entry(result
, "is-packetized", is_packetized
);
128 BT_CPPLOGE_APPEND_CAUSE_SPEC(
129 logger
, "Cannot insert \"is-packetized\" attribute into query result.");
136 BT_VALUE_PUT_REF_AND_RESET(result
);
140 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
144 if (g_metadata_text
) {
145 g_string_free(g_metadata_text
, TRUE
);
148 *user_result
= result
;
152 static int add_range(bt_value
*info
, struct range
*range
, const char *range_name
)
155 bt_value_map_insert_entry_status status
;
163 status
= bt_value_map_insert_empty_map_entry(info
, range_name
, &range_map
);
164 if (status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
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
) {
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
) {
185 static int populate_stream_info(struct ctf_fs_ds_file_group
*group
, bt_value
*group_info
,
186 struct range
*stream_range
)
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
;
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.
199 BT_ASSERT(group
->index
);
200 BT_ASSERT(group
->index
->entries
);
201 BT_ASSERT(group
->index
->entries
->len
> 0);
204 first_ds_index_entry
=
205 (struct ctf_fs_ds_index_entry
*) g_ptr_array_index(group
->index
->entries
, 0);
208 last_ds_index_entry
= (struct ctf_fs_ds_index_entry
*) g_ptr_array_index(
209 group
->index
->entries
, group
->index
->entries
->len
- 1);
211 stream_range
->begin_ns
= first_ds_index_entry
->timestamp_begin_ns
;
212 stream_range
->end_ns
= last_ds_index_entry
->timestamp_end_ns
;
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.
220 stream_range
->begin_ns
!= UINT64_C(-1) && stream_range
->end_ns
!= UINT64_C(-1);
222 ret
= add_range(group_info
, stream_range
, "range-ns");
227 port_name
= ctf_fs_make_port_name(group
);
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
) {
243 static int populate_trace_info(const struct ctf_fs_trace
*trace
, bt_value
*trace_info
,
244 const bt2c::Logger
& logger
)
248 bt_value_map_insert_entry_status insert_status
;
249 bt_value_array_append_element_status append_status
;
250 bt_value
*file_groups
= NULL
;
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) {
256 BT_CPPLOGE_APPEND_CAUSE_SPEC(logger
, "Trace has no streams: trace-path={}",
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
) {
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
;
271 ctf_fs_ds_file_group
*group
=
272 (ctf_fs_ds_file_group
*) g_ptr_array_index(trace
->ds_file_groups
, group_idx
);
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
) {
280 ret
= populate_stream_info(group
, group_info
, &group_range
);
290 bt_component_class_query_method_status
291 trace_infos_query(const bt_value
*params
, const bt2c::Logger
& logger
, const bt_value
**user_result
)
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
;
299 bt_value
*trace_info
= NULL
;
300 bt_value_array_append_element_status append_status
;
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
;
310 ctf_fs
= ctf_fs_component_create(logger
);
315 if (!read_src_fs_parameters(params
, &inputs_value
, &trace_name_value
, ctf_fs
.get())) {
316 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
320 if (ctf_fs_component_create_ctf_fs_trace(ctf_fs
.get(), inputs_value
, trace_name_value
, NULL
)) {
324 result
= bt_value_array_create();
326 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
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.");
336 ret
= populate_trace_info(ctf_fs
->trace
, trace_info
, logger
);
344 BT_VALUE_PUT_REF_AND_RESET(result
);
347 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
351 *user_result
= result
;
355 bt_component_class_query_method_status
356 support_info_query(const bt_value
*params
, const bt2c::Logger
& logger
, const bt_value
**user_result
)
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
;
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
;
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
);
377 if (strcmp(input_type
, "directory") != 0) {
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
);
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
;
392 metadata_file
= g_fopen(metadata_path
.get(), "rb");
394 enum ctf_metadata_decoder_status decoder_status
;
397 ctf_metadata_decoder_config metadata_decoder_config
{logger
};
399 metadata_decoder
= ctf_metadata_decoder_create(&metadata_decoder_config
);
400 if (!metadata_decoder
) {
401 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
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={}",
409 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
414 * We were able to parse the metadata file, so we are
415 * confident it's a CTF trace.
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
);
427 result
= bt_value_map_create();
429 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
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
;
439 /* We are not supposed to have weight == 0 and a UUID. */
440 BT_ASSERT(weight
> 0 || !has_uuid
);
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
;
450 *user_result
= result
;
452 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
455 bt_value_put_ref(result
);