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 char *metadata_text
= NULL
;
63 FILE *metadata_fp
= NULL
;
64 GString
*g_metadata_text
= NULL
;
70 result
= bt_value_map_create();
72 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
78 if (!bt_value_is_map(params
)) {
79 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
80 "Query parameters is not a map value object.");
81 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
85 path_value
= bt_value_map_borrow_entry_value_const(params
, "path");
87 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
88 "Mandatory `path` parameter missing");
89 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
93 if (!bt_value_is_string(path_value
)) {
94 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
95 "`path` parameter is required to be a string value");
96 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
100 path
= bt_value_string_get(path_value
);
103 metadata_fp
= ctf_fs_metadata_open_file(path
);
105 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
106 "Cannot open trace metadata: path=\"%s\".", path
);
110 is_packetized
= ctf_metadata_decoder_is_packetized(metadata_fp
,
111 &bo
, log_level
, NULL
);
114 ret
= ctf_metadata_decoder_packetized_file_stream_to_buf(
115 metadata_fp
, &metadata_text
, bo
, NULL
, NULL
,
118 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
119 "Cannot decode packetized metadata file: path=\"%s\"",
126 ret
= fseek(metadata_fp
, 0, SEEK_END
);
128 BT_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(self_comp_class
,
129 "Failed to seek to the end of the metadata file",
130 ": path=\"%s\"", path
);
133 filesize
= ftell(metadata_fp
);
135 BT_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(self_comp_class
,
136 "Failed to get the current position in the metadata file",
137 ": path=\"%s\"", path
);
141 metadata_text
= malloc(filesize
+ 1);
142 if (!metadata_text
) {
143 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
144 "Cannot allocate buffer for metadata text.");
148 if (fread(metadata_text
, filesize
, 1, metadata_fp
) != 1) {
149 BT_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(self_comp_class
,
150 "Cannot read metadata file", ": path=\"%s\"",
155 metadata_text
[filesize
] = '\0';
158 g_metadata_text
= g_string_new(NULL
);
159 if (!g_metadata_text
) {
163 if (strncmp(metadata_text
, METADATA_TEXT_SIG
,
164 sizeof(METADATA_TEXT_SIG
) - 1) != 0) {
165 g_string_assign(g_metadata_text
, METADATA_TEXT_SIG
);
166 g_string_append(g_metadata_text
, " */\n\n");
169 g_string_append(g_metadata_text
, metadata_text
);
171 ret
= bt_value_map_insert_string_entry(result
, "text",
172 g_metadata_text
->str
);
174 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
175 "Cannot insert metadata text into query result.");
179 ret
= bt_value_map_insert_bool_entry(result
, "is-packetized",
182 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
183 "Cannot insert \"is-packetized\" attribute into query result.");
190 BT_VALUE_PUT_REF_AND_RESET(result
);
194 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
200 if (g_metadata_text
) {
201 g_string_free(g_metadata_text
, TRUE
);
208 *user_result
= result
;
213 int add_range(bt_value
*info
, struct range
*range
,
214 const char *range_name
)
217 bt_value_map_insert_entry_status status
;
225 status
= bt_value_map_insert_empty_map_entry(info
, range_name
,
227 if (status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
232 status
= bt_value_map_insert_signed_integer_entry(range_map
, "begin",
234 if (status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
239 status
= bt_value_map_insert_signed_integer_entry(range_map
, "end",
241 if (status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
251 int populate_stream_info(struct ctf_fs_ds_file_group
*group
,
252 bt_value
*group_info
, struct range
*stream_range
)
255 bt_value_map_insert_entry_status insert_status
;
256 struct ctf_fs_ds_index_entry
*first_ds_index_entry
, *last_ds_index_entry
;
257 gchar
*port_name
= NULL
;
260 * Since each `struct ctf_fs_ds_file_group` has a sorted array of
261 * `struct ctf_fs_ds_index_entry`, we can compute the stream range from
262 * the timestamp_begin of the first index entry and the timestamp_end
263 * of the last index entry.
265 BT_ASSERT(group
->index
);
266 BT_ASSERT(group
->index
->entries
);
267 BT_ASSERT(group
->index
->entries
->len
> 0);
270 first_ds_index_entry
= (struct ctf_fs_ds_index_entry
*) g_ptr_array_index(
271 group
->index
->entries
, 0);
274 last_ds_index_entry
= (struct ctf_fs_ds_index_entry
*) g_ptr_array_index(
275 group
->index
->entries
, group
->index
->entries
->len
- 1);
277 stream_range
->begin_ns
= first_ds_index_entry
->timestamp_begin_ns
;
278 stream_range
->end_ns
= last_ds_index_entry
->timestamp_end_ns
;
281 * If any of the begin and end timestamps is not set it means that
282 * packets don't include `timestamp_begin` _and_ `timestamp_end` fields
283 * in their packet context so we can't set the range.
285 stream_range
->set
= stream_range
->begin_ns
!= UINT64_C(-1) &&
286 stream_range
->end_ns
!= UINT64_C(-1);
288 ret
= add_range(group_info
, stream_range
, "range-ns");
293 port_name
= ctf_fs_make_port_name(group
);
299 insert_status
= bt_value_map_insert_string_entry(group_info
,
300 "port-name", port_name
);
301 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
312 int populate_trace_info(const struct ctf_fs_trace
*trace
, bt_value
*trace_info
)
316 bt_value_map_insert_entry_status insert_status
;
317 bt_value_array_append_element_status append_status
;
318 bt_value
*file_groups
= NULL
;
319 struct range trace_intersection
= {
325 BT_ASSERT(trace
->ds_file_groups
);
326 /* Add trace range info only if it contains streams. */
327 if (trace
->ds_file_groups
->len
== 0) {
332 insert_status
= bt_value_map_insert_empty_array_entry(trace_info
,
333 "streams", &file_groups
);
334 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
339 /* Find range of all stream groups, and of the trace. */
340 for (group_idx
= 0; group_idx
< trace
->ds_file_groups
->len
;
342 bt_value
*group_info
;
343 struct range group_range
= { .set
= false };
344 struct ctf_fs_ds_file_group
*group
= g_ptr_array_index(
345 trace
->ds_file_groups
, group_idx
);
347 append_status
= bt_value_array_append_empty_map_element(
348 file_groups
, &group_info
);
349 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
354 ret
= populate_stream_info(group
, group_info
, &group_range
);
359 if (group_range
.set
) {
360 trace_intersection
.begin_ns
= MAX(trace_intersection
.begin_ns
,
361 group_range
.begin_ns
);
362 trace_intersection
.end_ns
= MIN(trace_intersection
.end_ns
,
364 trace_intersection
.set
= true;
368 if (trace_intersection
.begin_ns
< trace_intersection
.end_ns
) {
369 ret
= add_range(trace_info
, &trace_intersection
,
370 "intersection-range-ns");
381 bt_component_class_query_method_status
trace_info_query(
382 bt_self_component_class_source
*self_comp_class_src
,
383 const bt_value
*params
, bt_logging_level log_level
,
384 const bt_value
**user_result
)
386 struct ctf_fs_component
*ctf_fs
= NULL
;
387 bt_component_class_query_method_status status
=
388 BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
389 bt_self_component_class
*self_comp_class
=
390 bt_self_component_class_source_as_self_component_class(
391 self_comp_class_src
);
392 bt_value
*result
= NULL
;
393 const bt_value
*inputs_value
= NULL
;
399 if (!bt_value_is_map(params
)) {
400 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
401 "Query parameters is not a map value object.");
402 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
406 ctf_fs
= ctf_fs_component_create(log_level
, NULL
);
411 if (!read_src_fs_parameters(params
, &inputs_value
, ctf_fs
, NULL
,
413 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
417 if (ctf_fs_component_create_ctf_fs_traces(ctf_fs
, inputs_value
, NULL
,
422 result
= bt_value_array_create();
424 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
428 for (i
= 0; i
< ctf_fs
->traces
->len
; i
++) {
429 struct ctf_fs_trace
*trace
;
430 bt_value
*trace_info
;
431 bt_value_array_append_element_status append_status
;
433 trace
= g_ptr_array_index(ctf_fs
->traces
, i
);
436 append_status
= bt_value_array_append_empty_map_element(result
,
438 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
439 BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class
,
440 "Failed to create trace info map.");
444 ret
= populate_trace_info(trace
, trace_info
);
453 BT_VALUE_PUT_REF_AND_RESET(result
);
457 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR
;
462 ctf_fs_destroy(ctf_fs
);
466 *user_result
= result
;
471 bt_component_class_query_method_status
support_info_query(
472 bt_self_component_class_source
*comp_class
,
473 const bt_value
*params
, bt_logging_level log_level
,
474 const bt_value
**user_result
)
476 const bt_value
*input_type_value
;
477 const char *input_type
;
478 bt_component_class_query_method_status status
;
480 gchar
*metadata_path
= NULL
;
481 bt_value
*result
= NULL
;
483 input_type_value
= bt_value_map_borrow_entry_value_const(params
, "type");
484 BT_ASSERT(input_type_value
);
485 BT_ASSERT(bt_value_get_type(input_type_value
) == BT_VALUE_TYPE_STRING
);
486 input_type
= bt_value_string_get(input_type_value
);
488 result
= bt_value_map_create();
490 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
494 if (strcmp(input_type
, "directory") == 0) {
495 const bt_value
*input_value
;
498 input_value
= bt_value_map_borrow_entry_value_const(params
, "input");
499 BT_ASSERT(input_value
);
500 BT_ASSERT(bt_value_get_type(input_value
) == BT_VALUE_TYPE_STRING
);
501 path
= bt_value_string_get(input_value
);
503 metadata_path
= g_build_filename(path
, CTF_FS_METADATA_FILENAME
, NULL
);
504 if (!metadata_path
) {
505 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
510 * If the metadata file exists in this directory, consider it to
513 if (g_file_test(metadata_path
, G_FILE_TEST_EXISTS
)) {
518 if (bt_value_map_insert_real_entry(result
, "weight", weight
) != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
519 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
524 * Use the arbitrary constant string "ctf" as the group, such that all
525 * found ctf traces are passed to the same instance of src.ctf.fs.
527 if (bt_value_map_insert_string_entry(result
, "group", "ctf") != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
528 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR
;
532 *user_result
= result
;
534 status
= BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK
;
537 g_free(metadata_path
);
538 bt_value_put_ref(result
);