4 * Babeltrace CTF file system Reader Component queries
6 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 #define BT_LOG_OUTPUT_LEVEL log_level
28 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/QUERY"
29 #include "logging/log.h"
33 #include "common/assert.h"
35 #include "../common/metadata/decoder.h"
36 #include "common/common.h"
37 #include "common/macros.h"
38 #include <babeltrace2/babeltrace.h>
40 #include "logging/comp-logging.h"
42 #define METADATA_TEXT_SIG "/* CTF 1.8"
51 bt_component_class_query_method_status
metadata_info_query(
52 bt_self_component_class_source
*self_comp_class_src
,
53 const bt_value
*params
, bt_logging_level log_level
,
54 const bt_value
**user_result
)
56 bt_component_class_query_method_status status
=
57 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
58 bt_self_component_class
*self_comp_class
=
59 bt_self_component_class_source_as_self_component_class(self_comp_class_src
);
60 bt_value
*result
= NULL
;
61 const bt_value
*path_value
= NULL
;
62 FILE *metadata_fp
= NULL
;
67 struct ctf_metadata_decoder
*decoder
= NULL
;
68 struct ctf_metadata_decoder_config decoder_cfg
= { 0 };
69 enum ctf_metadata_decoder_status decoder_status
;
71 result
= bt_value_map_create();
73 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
79 if (!bt_value_is_map(params
)) {
80 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
81 "Query parameters is not a map value object.");
82 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
86 path_value
= bt_value_map_borrow_entry_value_const(params
, "path");
88 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
89 "Mandatory `path` parameter missing");
90 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
94 if (!bt_value_is_string(path_value
)) {
95 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
96 "`path` parameter is required to be a string value");
97 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
101 path
= bt_value_string_get(path_value
);
104 metadata_fp
= ctf_fs_metadata_open_file(path
);
106 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
107 "Cannot open trace metadata: path=\"%s\".", path
);
111 ret
= ctf_metadata_decoder_is_packetized(metadata_fp
, &is_packetized
,
112 &bo
, log_level
, NULL
);
114 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
115 "Cannot check whether or not the metadata stream is packetized: path=\"%s\".",
120 decoder_cfg
.log_level
= log_level
;
121 decoder_cfg
.keep_plain_text
= true;
122 decoder
= ctf_metadata_decoder_create(&decoder_cfg
);
124 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
125 "Cannot create metadata decoder: path=\"%s\".", path
);
130 decoder_status
= ctf_metadata_decoder_append_content(decoder
,
132 if (decoder_status
) {
133 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
134 "Cannot update metadata decoder's content: path=\"%s\".",
139 ret
= bt_value_map_insert_string_entry(result
, "text",
140 ctf_metadata_decoder_get_text(decoder
));
142 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
143 "Cannot insert metadata text into query result.");
147 ret
= bt_value_map_insert_bool_entry(result
, "is-packetized",
150 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
151 "Cannot insert \"is-packetized\" attribute into query result.");
158 BT_VALUE_PUT_REF_AND_RESET(result
);
162 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
166 ctf_metadata_decoder_destroy(decoder
);
172 *user_result
= result
;
177 int add_range(bt_value
*info
, struct range
*range
,
178 const char *range_name
)
181 bt_value_map_insert_entry_status status
;
189 status
= bt_value_map_insert_empty_map_entry(info
, range_name
,
191 if (status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
196 status
= bt_value_map_insert_signed_integer_entry(range_map
, "begin",
198 if (status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
203 status
= bt_value_map_insert_signed_integer_entry(range_map
, "end",
205 if (status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
215 int populate_stream_info(struct ctf_fs_ds_file_group
*group
,
216 bt_value
*group_info
, struct range
*stream_range
)
219 bt_value_map_insert_entry_status insert_status
;
220 struct ctf_fs_ds_index_entry
*first_ds_index_entry
, *last_ds_index_entry
;
221 gchar
*port_name
= NULL
;
224 * Since each `struct ctf_fs_ds_file_group` has a sorted array of
225 * `struct ctf_fs_ds_index_entry`, we can compute the stream range from
226 * the timestamp_begin of the first index entry and the timestamp_end
227 * of the last index entry.
229 BT_ASSERT(group
->index
);
230 BT_ASSERT(group
->index
->entries
);
231 BT_ASSERT(group
->index
->entries
->len
> 0);
234 first_ds_index_entry
= (struct ctf_fs_ds_index_entry
*) g_ptr_array_index(
235 group
->index
->entries
, 0);
238 last_ds_index_entry
= (struct ctf_fs_ds_index_entry
*) g_ptr_array_index(
239 group
->index
->entries
, group
->index
->entries
->len
- 1);
241 stream_range
->begin_ns
= first_ds_index_entry
->timestamp_begin_ns
;
242 stream_range
->end_ns
= last_ds_index_entry
->timestamp_end_ns
;
245 * If any of the begin and end timestamps is not set it means that
246 * packets don't include `timestamp_begin` _and_ `timestamp_end` fields
247 * in their packet context so we can't set the range.
249 stream_range
->set
= stream_range
->begin_ns
!= UINT64_C(-1) &&
250 stream_range
->end_ns
!= UINT64_C(-1);
252 ret
= add_range(group_info
, stream_range
, "range-ns");
257 port_name
= ctf_fs_make_port_name(group
);
263 insert_status
= bt_value_map_insert_string_entry(group_info
,
264 "port-name", port_name
);
265 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
276 int populate_trace_info(const struct ctf_fs_trace
*trace
, bt_value
*trace_info
)
280 bt_value_map_insert_entry_status insert_status
;
281 bt_value_array_append_element_status append_status
;
282 bt_value
*file_groups
= NULL
;
283 struct range trace_intersection
= {
289 BT_ASSERT(trace
->ds_file_groups
);
290 /* Add trace range info only if it contains streams. */
291 if (trace
->ds_file_groups
->len
== 0) {
296 insert_status
= bt_value_map_insert_empty_array_entry(trace_info
,
297 "streams", &file_groups
);
298 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
303 /* Find range of all stream groups, and of the trace. */
304 for (group_idx
= 0; group_idx
< trace
->ds_file_groups
->len
;
306 bt_value
*group_info
;
307 struct range group_range
= { .set
= false };
308 struct ctf_fs_ds_file_group
*group
= g_ptr_array_index(
309 trace
->ds_file_groups
, group_idx
);
311 append_status
= bt_value_array_append_empty_map_element(
312 file_groups
, &group_info
);
313 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
318 ret
= populate_stream_info(group
, group_info
, &group_range
);
323 if (group_range
.set
) {
324 trace_intersection
.begin_ns
= MAX(trace_intersection
.begin_ns
,
325 group_range
.begin_ns
);
326 trace_intersection
.end_ns
= MIN(trace_intersection
.end_ns
,
328 trace_intersection
.set
= true;
332 if (trace_intersection
.begin_ns
< trace_intersection
.end_ns
) {
333 ret
= add_range(trace_info
, &trace_intersection
,
334 "intersection-range-ns");
345 bt_component_class_query_method_status
trace_info_query(
346 bt_self_component_class_source
*self_comp_class_src
,
347 const bt_value
*params
, bt_logging_level log_level
,
348 const bt_value
**user_result
)
350 struct ctf_fs_component
*ctf_fs
= NULL
;
351 bt_component_class_query_method_status status
=
352 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
353 bt_self_component_class
*self_comp_class
=
354 bt_self_component_class_source_as_self_component_class(
355 self_comp_class_src
);
356 bt_value
*result
= NULL
;
357 const bt_value
*inputs_value
= NULL
;
363 if (!bt_value_is_map(params
)) {
364 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
365 "Query parameters is not a map value object.");
366 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
370 ctf_fs
= ctf_fs_component_create(log_level
, NULL
);
375 if (!read_src_fs_parameters(params
, &inputs_value
, ctf_fs
, NULL
,
377 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
381 if (ctf_fs_component_create_ctf_fs_traces(ctf_fs
, inputs_value
, NULL
,
386 result
= bt_value_array_create();
388 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
392 for (i
= 0; i
< ctf_fs
->traces
->len
; i
++) {
393 struct ctf_fs_trace
*trace
;
394 bt_value
*trace_info
;
395 bt_value_array_append_element_status append_status
;
397 trace
= g_ptr_array_index(ctf_fs
->traces
, i
);
400 append_status
= bt_value_array_append_empty_map_element(result
,
402 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
403 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
404 "Failed to create trace info map.");
408 ret
= populate_trace_info(trace
, trace_info
);
417 BT_VALUE_PUT_REF_AND_RESET(result
);
421 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
426 ctf_fs_destroy(ctf_fs
);
430 *user_result
= result
;
435 bt_component_class_query_method_status
support_info_query(
436 bt_self_component_class_source
*comp_class
,
437 const bt_value
*params
, bt_logging_level log_level
,
438 const bt_value
**user_result
)
440 const bt_value
*input_type_value
;
441 const char *input_type
;
442 bt_component_class_query_method_status status
;
444 gchar
*metadata_path
= NULL
;
445 bt_value
*result
= NULL
;
447 input_type_value
= bt_value_map_borrow_entry_value_const(params
, "type");
448 BT_ASSERT(input_type_value
);
449 BT_ASSERT(bt_value_get_type(input_type_value
) == BT_VALUE_TYPE_STRING
);
450 input_type
= bt_value_string_get(input_type_value
);
452 result
= bt_value_map_create();
454 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
458 if (strcmp(input_type
, "directory") == 0) {
459 const bt_value
*input_value
;
462 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
463 BT_ASSERT(input_value
);
464 BT_ASSERT(bt_value_get_type(input_value
) == BT_VALUE_TYPE_STRING
);
465 path
= bt_value_string_get(input_value
);
467 metadata_path
= g_build_filename(path
, CTF_FS_METADATA_FILENAME
, NULL
);
468 if (!metadata_path
) {
469 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
474 * If the metadata file exists in this directory, consider it to
477 if (g_file_test(metadata_path
, G_FILE_TEST_EXISTS
)) {
482 if (bt_value_map_insert_real_entry(result
, "weight", weight
) != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
483 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
488 * Use the arbitrary constant string "ctf" as the group, such that all
489 * found ctf traces are passed to the same instance of src.ctf.fs.
491 if (bt_value_map_insert_string_entry(result
, "group", "ctf") != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
492 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
496 *user_result
= result
;
498 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
501 g_free(metadata_path
);
502 bt_value_put_ref(result
);