2 * Copyright (c) 2019 EfficiOS Inc. and Linux Foundation
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #define BT_LOG_TAG "CLI-CFG-SRC-AUTO-DISC"
26 #include "babeltrace2-cfg-src-auto-disc.h"
27 #include "babeltrace2-plugins.h"
28 #include "babeltrace2-query.h"
29 #include "common/common.h"
31 /* Finalize and free a `struct auto_source_discovery_result`. */
34 void auto_source_discovery_result_destroy(struct auto_source_discovery_result
*res
)
38 bt_value_put_ref(res
->inputs
);
39 bt_value_put_ref(res
->original_input_indices
);
44 /* Allocate and initialize a `struct auto_source_discovery_result`. */
47 struct auto_source_discovery_result
*auto_source_discovery_result_create(
48 const char *plugin_name
, const char *source_cc_name
,
51 struct auto_source_discovery_result
*res
;
53 res
= g_new0(struct auto_source_discovery_result
, 1);
55 BT_CLI_LOGE_APPEND_CAUSE(
56 "Failed to allocate a auto_source_discovery_result structure.");
60 res
->plugin_name
= plugin_name
;
61 res
->source_cc_name
= source_cc_name
;
62 res
->group
= g_strdup(group
);
63 if (group
&& !res
->group
) {
64 BT_CLI_LOGE_APPEND_CAUSE("Failed to allocate a string.");
68 res
->inputs
= bt_value_array_create();
70 BT_CLI_LOGE_APPEND_CAUSE("Failed to allocate an array value.");
74 res
->original_input_indices
= bt_value_array_create();
75 if (!res
->original_input_indices
) {
76 BT_CLI_LOGE_APPEND_CAUSE("Failed to allocate an array value.");
82 auto_source_discovery_result_destroy(res
);
88 /* Finalize a `struct auto_source_discovery`. */
90 void auto_source_discovery_fini(struct auto_source_discovery
*auto_disc
)
92 if (auto_disc
->results
) {
93 g_ptr_array_free(auto_disc
->results
, TRUE
);
97 /* Initialize an already allocated `struct auto_source_discovery`. */
99 int auto_source_discovery_init(struct auto_source_discovery
*auto_disc
)
103 auto_disc
->results
= g_ptr_array_new_with_free_func(
104 (GDestroyNotify
) auto_source_discovery_result_destroy
);
106 if (!auto_disc
->results
) {
114 auto_source_discovery_fini(auto_disc
);
123 const bt_value
*borrow_array_value_last_element_const(const bt_value
*array
)
125 uint64_t last_index
= bt_value_array_get_size(array
) - 1;
127 return bt_value_array_borrow_element_by_index_const(array
, last_index
);
131 * Assign `input` to source component class `source_cc_name` of plugin
132 * `plugin_name`, in the group with key `group`.
136 int auto_source_discovery_add(struct auto_source_discovery
*auto_disc
,
137 const char *plugin_name
,
138 const char *source_cc_name
,
141 uint64_t original_input_index
)
144 bt_value_array_append_element_status append_status
;
147 struct auto_source_discovery_result
*res
= NULL
;
150 len
= auto_disc
->results
->len
;
154 for (i
= 0; i
< len
; i
++) {
155 res
= g_ptr_array_index(auto_disc
->results
, i
);
157 if (strcmp(res
->plugin_name
, plugin_name
) != 0) {
161 if (strcmp(res
->source_cc_name
, source_cc_name
) != 0) {
165 if (g_strcmp0(res
->group
, group
) != 0) {
174 /* Add a new result entry. */
175 res
= auto_source_discovery_result_create(plugin_name
,
176 source_cc_name
, group
);
181 g_ptr_array_add(auto_disc
->results
, res
);
184 append_status
= bt_value_array_append_string_element(res
->inputs
, input
);
185 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
186 BT_CLI_LOGE_APPEND_CAUSE("Failed to append a string value.");
191 * Append `original_input_index` to `original_input_indices` if not
192 * there already. We process the `inputs` array in order, so if it is
193 * present, it has to be the last element.
195 if (bt_value_array_is_empty(res
->original_input_indices
)) {
198 const bt_value
*last_index_value
;
202 borrow_array_value_last_element_const(res
->original_input_indices
);
203 last_index
= bt_value_integer_unsigned_get(last_index_value
);
205 BT_ASSERT(last_index
<= original_input_index
);
207 append_index
= (last_index
!= original_input_index
);
211 append_status
= bt_value_array_append_unsigned_integer_element(
212 res
->original_input_indices
, original_input_index
);
214 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
215 BT_CLI_LOGE_APPEND_CAUSE("Failed to append an unsigned integer value.");
231 int convert_weight_value(const bt_value
*weight_value
, double *weight
,
232 const char *plugin_name
, const char *source_cc_name
,
233 const char *input
, const char *input_type
)
235 enum bt_value_type weight_value_type
;
238 weight_value_type
= bt_value_get_type(weight_value
);
240 if (weight_value_type
== BT_VALUE_TYPE_REAL
) {
241 *weight
= bt_value_real_get(weight_value
);
242 } else if (weight_value_type
== BT_VALUE_TYPE_SIGNED_INTEGER
) {
243 /* Accept signed integer as a convenience for "return 0" or "return 1" in Python. */
244 *weight
= bt_value_integer_signed_get(weight_value
);
246 BT_LOGW("babeltrace.support-info query: unexpected type for weight: "
247 "component-class-name=source.%s.%s, input=%s, input-type=%s, "
248 "expected-entry-type=%s, actual-entry-type=%s",
249 plugin_name
, source_cc_name
, input
, input_type
,
250 bt_common_value_type_string(BT_VALUE_TYPE_REAL
),
251 bt_common_value_type_string(bt_value_get_type(weight_value
)));
255 if (*weight
< 0.0 || *weight
> 1.0) {
256 BT_LOGW("babeltrace.support-info query: weight value is out of range [0.0, 1.0]: "
257 "component-class-name=source.%s.%s, input=%s, input-type=%s, "
259 plugin_name
, source_cc_name
, input
, input_type
, *weight
);
274 * Query all known source components to see if any of them can handle `input`
275 * as the given `type`(arbitrary string, directory or file).
277 * If `plugin_restrict` is non-NULL, only query source component classes provided
278 * by the plugin with that name.
280 * If `component_class_restrict` is non-NULL, only query source component classes
285 * - > 0 on success, if no source component class has reported that it handles `input`
286 * - 0 on success, if a source component class has reported that it handles `input`
287 * - < 0 on failure (e.g. memory error)
290 int support_info_query_all_sources(const char *input
,
291 const char *input_type
,
292 uint64_t original_input_index
,
294 const char *plugin_restrict
,
295 const char *component_class_restrict
,
296 enum bt_logging_level log_level
,
297 struct auto_source_discovery
*auto_disc
)
299 bt_value_map_insert_entry_status insert_status
;
300 bt_value
*query_params
= NULL
;
303 const struct bt_value
*query_result
= NULL
;
305 const bt_component_class_source
*source
;
306 const bt_plugin
*plugin
;
307 const bt_value
*group
;
309 } winner
= { NULL
, NULL
, NULL
, 0 };
311 query_params
= bt_value_map_create();
313 BT_CLI_LOGE_APPEND_CAUSE("Failed to allocate a map value.");
317 insert_status
= bt_value_map_insert_string_entry(query_params
, "input", input
);
318 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
319 BT_CLI_LOGE_APPEND_CAUSE("Failed to insert a map entry.");
323 insert_status
= bt_value_map_insert_string_entry(query_params
, "type", input_type
);
324 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
325 BT_CLI_LOGE_APPEND_CAUSE("Failed to insert a map entry.");
329 for (i_plugins
= 0; i_plugins
< plugin_count
; i_plugins
++) {
330 const bt_plugin
*plugin
;
331 const char *plugin_name
;
332 uint64_t source_count
;
335 plugin
= borrow_loaded_plugin(i_plugins
);
336 plugin_name
= bt_plugin_get_name(plugin
);
339 * If the search is restricted to a specific plugin, only consider
340 * the plugin with that name.
342 if (plugin_restrict
&& strcmp(plugin_restrict
, plugin_name
) != 0) {
346 source_count
= bt_plugin_get_source_component_class_count(plugin
);
348 for (i_sources
= 0; i_sources
< source_count
; i_sources
++) {
349 const bt_component_class_source
*source_cc
;
350 const bt_component_class
*cc
;
351 const char *source_cc_name
;
352 bt_query_executor_query_status query_status
;
354 source_cc
= bt_plugin_borrow_source_component_class_by_index_const(plugin
, i_sources
);
355 cc
= bt_component_class_source_as_component_class_const(source_cc
);
356 source_cc_name
= bt_component_class_get_name(cc
);
359 * If the search is restricted to a specific component class, only consider the
360 * component classes with that name.
362 if (component_class_restrict
&& strcmp(component_class_restrict
, source_cc_name
) != 0) {
366 BT_LOGD("babeltrace.support-info query: before: component-class-name=source.%s.%s, input=%s, "
367 "type=%s", plugin_name
, source_cc_name
, input
, input_type
);
369 BT_VALUE_PUT_REF_AND_RESET(query_result
);
370 query_status
= cli_query(cc
, "babeltrace.support-info",
371 query_params
, log_level
, NULL
, &query_result
,
374 if (query_status
== BT_QUERY_EXECUTOR_QUERY_STATUS_OK
) {
376 const bt_value
*group_value
= NULL
;
377 enum bt_value_type query_result_type
;
379 BT_ASSERT(query_result
);
381 query_result_type
= bt_value_get_type(query_result
);
383 if (query_result_type
== BT_VALUE_TYPE_REAL
|| query_result_type
== BT_VALUE_TYPE_SIGNED_INTEGER
) {
384 if (convert_weight_value(query_result
, &weight
, plugin_name
, source_cc_name
, input
, input_type
) != 0) {
385 /* convert_weight_value has already warned. */
388 } else if (query_result_type
== BT_VALUE_TYPE_MAP
) {
389 const bt_value
*weight_value
;
391 if (!bt_value_map_has_entry(query_result
, "weight")) {
392 BT_LOGW("babeltrace.support-info query: result is missing `weight` entry: "
393 "component-class-name=source.%s.%s, input=%s, input-type=%s",
394 bt_plugin_get_name(plugin
),
395 bt_component_class_get_name(cc
), input
,
400 weight_value
= bt_value_map_borrow_entry_value_const(query_result
, "weight");
401 BT_ASSERT(weight_value
);
403 if (convert_weight_value(weight_value
, &weight
, plugin_name
, source_cc_name
, input
, input_type
) != 0) {
404 /* convert_weight_value has already warned. */
408 if (bt_value_map_has_entry(query_result
, "group")) {
409 enum bt_value_type group_value_type
;
411 group_value
= bt_value_map_borrow_entry_value_const(query_result
, "group");
412 BT_ASSERT(group_value
);
414 group_value_type
= bt_value_get_type(group_value
);
416 if (group_value_type
== BT_VALUE_TYPE_NULL
) {
417 /* Do as if no value was passed. */
419 } else if (bt_value_get_type(group_value
) != BT_VALUE_TYPE_STRING
) {
420 BT_LOGW("babeltrace.support-info query: unexpected type for entry `group`: "
421 "component-class-name=source.%s.%s, input=%s, input-type=%s, "
422 "expected-entry-type=%s,%s, actual-entry-type=%s",
423 bt_plugin_get_name(plugin
),
424 bt_component_class_get_name(cc
), input
,
426 bt_common_value_type_string(BT_VALUE_TYPE_NULL
),
427 bt_common_value_type_string(BT_VALUE_TYPE_STRING
),
428 bt_common_value_type_string(bt_value_get_type(group_value
)));
433 BT_LOGW("babeltrace.support-info query: unexpected result type: "
434 "component-class-name=source.%s.%s, input=%s, input-type=%s, "
435 "expected-types=%s,%s,%s, actual-type=%s",
436 bt_plugin_get_name(plugin
),
437 bt_component_class_get_name(cc
), input
,
439 bt_common_value_type_string(BT_VALUE_TYPE_REAL
),
440 bt_common_value_type_string(BT_VALUE_TYPE_MAP
),
441 bt_common_value_type_string(BT_VALUE_TYPE_SIGNED_INTEGER
),
442 bt_common_value_type_string(bt_value_get_type(query_result
)));
446 BT_LOGD("babeltrace.support-info query: success: component-class-name=source.%s.%s, input=%s, "
447 "type=%s, weight=%f\n",
448 bt_plugin_get_name(plugin
), bt_component_class_get_name(cc
), input
,
451 if (weight
> winner
.weigth
) {
452 winner
.source
= source_cc
;
453 winner
.plugin
= plugin
;
455 bt_value_put_ref(winner
.group
);
456 winner
.group
= group_value
;
457 bt_value_get_ref(winner
.group
);
459 winner
.weigth
= weight
;
461 } else if (query_status
== BT_QUERY_EXECUTOR_QUERY_STATUS_ERROR
) {
462 BT_CLI_LOGE_APPEND_CAUSE("babeltrace.support-info query failed.");
464 } else if (query_status
== BT_QUERY_EXECUTOR_QUERY_STATUS_MEMORY_ERROR
) {
465 BT_CLI_LOGE_APPEND_CAUSE("Memory error.");
468 BT_LOGD("babeltrace.support-info query: failure: component-class-name=source.%s.%s, input=%s, "
469 "type=%s, status=%s\n",
470 bt_plugin_get_name(plugin
), bt_component_class_get_name(cc
), input
,
472 bt_common_func_status_string(query_status
));
478 const char *source_name
;
479 const char *plugin_name
;
482 source_name
= bt_component_class_get_name(
483 bt_component_class_source_as_component_class_const(winner
.source
));
484 plugin_name
= bt_plugin_get_name(winner
.plugin
);
485 group
= winner
.group
? bt_value_string_get(winner
.group
) : NULL
;
487 BT_LOGI("Input %s is awarded to component class source.%s.%s with weight %f",
488 input
, plugin_name
, source_name
, winner
.weigth
);
490 status
= auto_source_discovery_add(auto_disc
, plugin_name
,
491 source_name
, group
, input
, original_input_index
);
496 BT_LOGI("Input %s (%s) was not recognized by any source component class.",
507 bt_value_put_ref(query_result
);
508 bt_value_put_ref(query_params
);
509 bt_value_put_ref(winner
.group
);
515 * Look for a source component class that recognizes `input` as an arbitrary
518 * Same return value semantic as `support_info_query_all_sources`.
522 int auto_discover_source_for_input_as_string(const char *input
,
523 uint64_t original_input_index
,
524 size_t plugin_count
, const char *plugin_restrict
,
525 const char *component_class_restrict
,
526 enum bt_logging_level log_level
,
527 struct auto_source_discovery
*auto_disc
)
529 return support_info_query_all_sources(input
, "string",
530 original_input_index
, plugin_count
, plugin_restrict
,
531 component_class_restrict
, log_level
, auto_disc
);
535 int auto_discover_source_for_input_as_dir_or_file_rec(GString
*input
,
536 uint64_t original_input_index
,
537 size_t plugin_count
, const char *plugin_restrict
,
538 const char *component_class_restrict
,
539 enum bt_logging_level log_level
,
540 struct auto_source_discovery
*auto_disc
)
543 GError
*error
= NULL
;
545 if (g_file_test(input
->str
, G_FILE_TEST_IS_REGULAR
)) {
547 status
= support_info_query_all_sources(input
->str
,
548 "file", original_input_index
, plugin_count
,
549 plugin_restrict
, component_class_restrict
, log_level
, auto_disc
);
550 } else if (g_file_test(input
->str
, G_FILE_TEST_IS_DIR
)) {
553 gsize saved_input_len
;
556 /* It's a directory. */
557 status
= support_info_query_all_sources(input
->str
,
558 "directory", original_input_index
, plugin_count
,
559 plugin_restrict
, component_class_restrict
, log_level
,
565 } else if (status
== 0) {
567 * A component class claimed this input as a directory,
573 dir
= g_dir_open(input
->str
, 0, &error
);
575 const char *fmt
= "Failed to open directory %s: %s";
576 BT_LOGW(fmt
, input
->str
, error
->message
);
578 if (error
->code
== G_FILE_ERROR_ACCES
) {
579 /* This is not a fatal error, we just skip it. */
583 BT_CLI_LOGE_APPEND_CAUSE(fmt
, input
->str
,
589 saved_input_len
= input
->len
;
593 dirent
= g_dir_read_name(dir
);
595 g_string_append_c_inline(input
, G_DIR_SEPARATOR
);
596 g_string_append(input
, dirent
);
598 status
= auto_discover_source_for_input_as_dir_or_file_rec(
599 input
, original_input_index
, plugin_count
,
600 plugin_restrict
, component_class_restrict
,
601 log_level
, auto_disc
);
603 g_string_truncate(input
, saved_input_len
);
608 } else if (status
== 0) {
611 } else if (errno
!= 0) {
612 BT_LOGW_ERRNO("Failed to read directory entry", ": dir=%s", input
->str
);
621 BT_LOGD("Skipping %s, not a file or directory", input
->str
);
640 * Look for a source component class that recognizes `input` as a directory or
641 * file. If `input` is a directory and is not directly recognized, recurse and
642 * apply the same logic to children nodes.
644 * Same return value semantic as `support_info_query_all_sources`.
648 int auto_discover_source_for_input_as_dir_or_file(const char *input
,
649 uint64_t original_input_index
,
650 size_t plugin_count
, const char *plugin_restrict
,
651 const char *component_class_restrict
,
652 enum bt_logging_level log_level
,
653 struct auto_source_discovery
*auto_disc
)
655 GString
*mutable_input
;
658 mutable_input
= g_string_new(input
);
659 if (!mutable_input
) {
664 status
= auto_discover_source_for_input_as_dir_or_file_rec(
665 mutable_input
, original_input_index
, plugin_count
, plugin_restrict
,
666 component_class_restrict
, log_level
, auto_disc
);
668 g_string_free(mutable_input
, TRUE
);
673 int auto_discover_source_components(
674 const bt_value
*plugin_paths
,
675 const bt_value
*inputs
,
676 const char *plugin_restrict
,
677 const char *component_class_restrict
,
678 enum bt_logging_level log_level
,
679 struct auto_source_discovery
*auto_disc
)
681 uint64_t i_inputs
, input_count
;
685 input_count
= bt_value_array_get_size(inputs
);
687 status
= require_loaded_plugins(plugin_paths
);
692 plugin_count
= get_loaded_plugins_count();
694 for (i_inputs
= 0; i_inputs
< input_count
; i_inputs
++) {
695 const bt_value
*input_value
;
698 input_value
= bt_value_array_borrow_element_by_index_const(inputs
, i_inputs
);
699 input
= bt_value_string_get(input_value
);
700 status
= auto_discover_source_for_input_as_string(input
, i_inputs
,
701 plugin_count
, plugin_restrict
, component_class_restrict
,
702 log_level
, auto_disc
);
706 } else if (status
== 0) {
707 /* A component class has claimed this input as an arbitrary string. */
711 status
= auto_discover_source_for_input_as_dir_or_file(input
,
712 i_inputs
, plugin_count
, plugin_restrict
,
713 component_class_restrict
, log_level
, auto_disc
);
717 } else if (status
== 0) {
719 * This input (or something under it) was recognized.
724 BT_LOGW("No trace was found based on input `%s`.", input
);