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
);
43 /* Allocate and initialize a `struct auto_source_discovery_result`. */
46 struct auto_source_discovery_result
*auto_source_discovery_result_create(
47 const char *plugin_name
, const char *source_cc_name
,
50 struct auto_source_discovery_result
*res
;
52 res
= g_new0(struct auto_source_discovery_result
, 1);
54 BT_CLI_LOGE_APPEND_CAUSE(
55 "Failed to allocate a auto_source_discovery_result structure.");
59 res
->plugin_name
= plugin_name
;
60 res
->source_cc_name
= source_cc_name
;
61 res
->group
= g_strdup(group
);
62 if (group
&& !res
->group
) {
63 BT_CLI_LOGE_APPEND_CAUSE("Failed to allocate a string.");
67 res
->inputs
= bt_value_array_create();
69 BT_CLI_LOGE_APPEND_CAUSE("Failed to allocate an array value.");
73 res
->original_input_indices
= bt_value_array_create();
74 if (!res
->original_input_indices
) {
75 BT_CLI_LOGE_APPEND_CAUSE("Failed to allocate an array value.");
81 auto_source_discovery_result_destroy(res
);
87 /* Finalize a `struct auto_source_discovery`. */
89 void auto_source_discovery_fini(struct auto_source_discovery
*auto_disc
)
91 if (auto_disc
->results
) {
92 g_ptr_array_free(auto_disc
->results
, TRUE
);
96 /* Initialize an already allocated `struct auto_source_discovery`. */
98 int auto_source_discovery_init(struct auto_source_discovery
*auto_disc
)
102 auto_disc
->results
= g_ptr_array_new_with_free_func(
103 (GDestroyNotify
) auto_source_discovery_result_destroy
);
105 if (!auto_disc
->results
) {
113 auto_source_discovery_fini(auto_disc
);
122 const bt_value
*borrow_array_value_last_element_const(const bt_value
*array
)
124 uint64_t last_index
= bt_value_array_get_size(array
) - 1;
126 return bt_value_array_borrow_element_by_index_const(array
, last_index
);
130 * Assign `input` to source component class `source_cc_name` of plugin
131 * `plugin_name`, in the group with key `group`.
135 int auto_source_discovery_add(struct auto_source_discovery
*auto_disc
,
136 const char *plugin_name
,
137 const char *source_cc_name
,
140 uint64_t original_input_index
)
143 bt_value_array_append_element_status append_status
;
146 struct auto_source_discovery_result
*res
= NULL
;
149 len
= auto_disc
->results
->len
;
153 for (i
= 0; i
< len
; i
++) {
154 res
= g_ptr_array_index(auto_disc
->results
, i
);
156 if (strcmp(res
->plugin_name
, plugin_name
) != 0) {
160 if (strcmp(res
->source_cc_name
, source_cc_name
) != 0) {
164 if (g_strcmp0(res
->group
, group
) != 0) {
173 /* Add a new result entry. */
174 res
= auto_source_discovery_result_create(plugin_name
,
175 source_cc_name
, group
);
180 g_ptr_array_add(auto_disc
->results
, res
);
183 append_status
= bt_value_array_append_string_element(res
->inputs
, input
);
184 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
185 BT_CLI_LOGE_APPEND_CAUSE("Failed to append a string value.");
190 * Append `original_input_index` to `original_input_indices` if not
191 * there already. We process the `inputs` array in order, so if it is
192 * present, it has to be the last element.
194 if (bt_value_array_is_empty(res
->original_input_indices
)) {
197 const bt_value
*last_index_value
;
201 borrow_array_value_last_element_const(res
->original_input_indices
);
202 last_index
= bt_value_integer_unsigned_get(last_index_value
);
204 BT_ASSERT(last_index
<= original_input_index
);
206 append_index
= (last_index
!= original_input_index
);
210 append_status
= bt_value_array_append_unsigned_integer_element(
211 res
->original_input_indices
, original_input_index
);
213 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
214 BT_CLI_LOGE_APPEND_CAUSE("Failed to append an unsigned integer value.");
230 int convert_weight_value(const bt_value
*weight_value
, double *weight
,
231 const char *plugin_name
, const char *source_cc_name
,
232 const char *input
, const char *input_type
)
234 enum bt_value_type weight_value_type
;
237 weight_value_type
= bt_value_get_type(weight_value
);
239 if (weight_value_type
== BT_VALUE_TYPE_REAL
) {
240 *weight
= bt_value_real_get(weight_value
);
241 } else if (weight_value_type
== BT_VALUE_TYPE_SIGNED_INTEGER
) {
242 /* Accept signed integer as a convenience for "return 0" or "return 1" in Python. */
243 *weight
= bt_value_integer_signed_get(weight_value
);
245 BT_LOGW("babeltrace.support-info query: unexpected type for weight: "
246 "component-class-name=source.%s.%s, input=%s, input-type=%s, "
247 "expected-entry-type=%s, actual-entry-type=%s",
248 plugin_name
, source_cc_name
, input
, input_type
,
249 bt_common_value_type_string(BT_VALUE_TYPE_REAL
),
250 bt_common_value_type_string(bt_value_get_type(weight_value
)));
254 if (*weight
< 0.0 || *weight
> 1.0) {
255 BT_LOGW("babeltrace.support-info query: weight value is out of range [0.0, 1.0]: "
256 "component-class-name=source.%s.%s, input=%s, input-type=%s, "
258 plugin_name
, source_cc_name
, input
, input_type
, *weight
);
273 * Query all known source components to see if any of them can handle `input`
274 * as the given `type`(arbitrary string, directory or file).
276 * If `plugin_restrict` is non-NULL, only query source component classes provided
277 * by the plugin with that name.
279 * If `component_class_restrict` is non-NULL, only query source component classes
284 * - > 0 on success, if no source component class has reported that it handles `input`
285 * - 0 on success, if a source component class has reported that it handles `input`
286 * - < 0 on failure (e.g. memory error)
289 int support_info_query_all_sources(const char *input
,
290 const char *input_type
,
291 uint64_t original_input_index
,
293 const char *plugin_restrict
,
294 const char *component_class_restrict
,
295 enum bt_logging_level log_level
,
296 struct auto_source_discovery
*auto_disc
)
298 bt_value_map_insert_entry_status insert_status
;
299 bt_value
*query_params
= NULL
;
302 const struct bt_value
*query_result
= NULL
;
304 const bt_component_class_source
*source
;
305 const bt_plugin
*plugin
;
306 const bt_value
*group
;
308 } winner
= { NULL
, NULL
, NULL
, 0 };
310 query_params
= bt_value_map_create();
312 BT_CLI_LOGE_APPEND_CAUSE("Failed to allocate a map value.");
316 insert_status
= bt_value_map_insert_string_entry(query_params
, "input", input
);
317 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
318 BT_CLI_LOGE_APPEND_CAUSE("Failed to insert a map entry.");
322 insert_status
= bt_value_map_insert_string_entry(query_params
, "type", input_type
);
323 if (insert_status
!= BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK
) {
324 BT_CLI_LOGE_APPEND_CAUSE("Failed to insert a map entry.");
328 for (i_plugins
= 0; i_plugins
< plugin_count
; i_plugins
++) {
329 const bt_plugin
*plugin
;
330 const char *plugin_name
;
331 uint64_t source_count
;
334 plugin
= borrow_loaded_plugin(i_plugins
);
335 plugin_name
= bt_plugin_get_name(plugin
);
338 * If the search is restricted to a specific plugin, only consider
339 * the plugin with that name.
341 if (plugin_restrict
&& strcmp(plugin_restrict
, plugin_name
) != 0) {
345 source_count
= bt_plugin_get_source_component_class_count(plugin
);
347 for (i_sources
= 0; i_sources
< source_count
; i_sources
++) {
348 const bt_component_class_source
*source_cc
;
349 const bt_component_class
*cc
;
350 const char *source_cc_name
;
351 bt_query_executor_query_status query_status
;
353 source_cc
= bt_plugin_borrow_source_component_class_by_index_const(plugin
, i_sources
);
354 cc
= bt_component_class_source_as_component_class_const(source_cc
);
355 source_cc_name
= bt_component_class_get_name(cc
);
358 * If the search is restricted to a specific component class, only consider the
359 * component classes with that name.
361 if (component_class_restrict
&& strcmp(component_class_restrict
, source_cc_name
) != 0) {
365 BT_LOGD("babeltrace.support-info query: before: component-class-name=source.%s.%s, input=%s, "
366 "type=%s", plugin_name
, source_cc_name
, input
, input_type
);
368 BT_VALUE_PUT_REF_AND_RESET(query_result
);
369 query_status
= cli_query(cc
, "babeltrace.support-info",
370 query_params
, log_level
, NULL
, &query_result
,
373 if (query_status
== BT_QUERY_EXECUTOR_QUERY_STATUS_OK
) {
375 const bt_value
*group_value
= NULL
;
376 enum bt_value_type query_result_type
;
378 BT_ASSERT(query_result
);
380 query_result_type
= bt_value_get_type(query_result
);
382 if (query_result_type
== BT_VALUE_TYPE_REAL
|| query_result_type
== BT_VALUE_TYPE_SIGNED_INTEGER
) {
383 if (convert_weight_value(query_result
, &weight
, plugin_name
, source_cc_name
, input
, input_type
) != 0) {
384 /* convert_weight_value has already warned. */
387 } else if (query_result_type
== BT_VALUE_TYPE_MAP
) {
388 const bt_value
*weight_value
;
390 if (!bt_value_map_has_entry(query_result
, "weight")) {
391 BT_LOGW("babeltrace.support-info query: result is missing `weight` entry: "
392 "component-class-name=source.%s.%s, input=%s, input-type=%s",
393 bt_plugin_get_name(plugin
),
394 bt_component_class_get_name(cc
), input
,
399 weight_value
= bt_value_map_borrow_entry_value_const(query_result
, "weight");
400 BT_ASSERT(weight_value
);
402 if (convert_weight_value(weight_value
, &weight
, plugin_name
, source_cc_name
, input
, input_type
) != 0) {
403 /* convert_weight_value has already warned. */
407 if (bt_value_map_has_entry(query_result
, "group")) {
408 enum bt_value_type group_value_type
;
410 group_value
= bt_value_map_borrow_entry_value_const(query_result
, "group");
411 BT_ASSERT(group_value
);
413 group_value_type
= bt_value_get_type(group_value
);
415 if (group_value_type
== BT_VALUE_TYPE_NULL
) {
416 /* Do as if no value was passed. */
418 } else if (bt_value_get_type(group_value
) != BT_VALUE_TYPE_STRING
) {
419 BT_LOGW("babeltrace.support-info query: unexpected type for entry `group`: "
420 "component-class-name=source.%s.%s, input=%s, input-type=%s, "
421 "expected-entry-type=%s,%s, actual-entry-type=%s",
422 bt_plugin_get_name(plugin
),
423 bt_component_class_get_name(cc
), input
,
425 bt_common_value_type_string(BT_VALUE_TYPE_NULL
),
426 bt_common_value_type_string(BT_VALUE_TYPE_STRING
),
427 bt_common_value_type_string(bt_value_get_type(group_value
)));
432 BT_LOGW("babeltrace.support-info query: unexpected result type: "
433 "component-class-name=source.%s.%s, input=%s, input-type=%s, "
434 "expected-types=%s,%s,%s, actual-type=%s",
435 bt_plugin_get_name(plugin
),
436 bt_component_class_get_name(cc
), input
,
438 bt_common_value_type_string(BT_VALUE_TYPE_REAL
),
439 bt_common_value_type_string(BT_VALUE_TYPE_MAP
),
440 bt_common_value_type_string(BT_VALUE_TYPE_SIGNED_INTEGER
),
441 bt_common_value_type_string(bt_value_get_type(query_result
)));
445 BT_LOGD("babeltrace.support-info query: success: component-class-name=source.%s.%s, input=%s, "
446 "type=%s, weight=%f\n",
447 bt_plugin_get_name(plugin
), bt_component_class_get_name(cc
), input
,
450 if (weight
> winner
.weigth
) {
451 winner
.source
= source_cc
;
452 winner
.plugin
= plugin
;
454 bt_value_put_ref(winner
.group
);
455 winner
.group
= group_value
;
456 bt_value_get_ref(winner
.group
);
458 winner
.weigth
= weight
;
460 } else if (query_status
== BT_QUERY_EXECUTOR_QUERY_STATUS_ERROR
) {
461 BT_CLI_LOGE_APPEND_CAUSE("babeltrace.support-info query failed.");
463 } else if (query_status
== BT_QUERY_EXECUTOR_QUERY_STATUS_MEMORY_ERROR
) {
464 BT_CLI_LOGE_APPEND_CAUSE("Memory error.");
467 BT_LOGD("babeltrace.support-info query: failure: component-class-name=source.%s.%s, input=%s, "
468 "type=%s, status=%s\n",
469 bt_plugin_get_name(plugin
), bt_component_class_get_name(cc
), input
,
471 bt_common_func_status_string(query_status
));
477 const char *source_name
;
478 const char *plugin_name
;
481 source_name
= bt_component_class_get_name(
482 bt_component_class_source_as_component_class_const(winner
.source
));
483 plugin_name
= bt_plugin_get_name(winner
.plugin
);
484 group
= winner
.group
? bt_value_string_get(winner
.group
) : NULL
;
486 BT_LOGI("Input %s is awarded to component class source.%s.%s with weight %f",
487 input
, plugin_name
, source_name
, winner
.weigth
);
489 status
= auto_source_discovery_add(auto_disc
, plugin_name
,
490 source_name
, group
, input
, original_input_index
);
495 BT_LOGI("Input %s (%s) was not recognized by any source component class.",
506 bt_value_put_ref(query_result
);
507 bt_value_put_ref(query_params
);
508 bt_value_put_ref(winner
.group
);
514 * Look for a source component class that recognizes `input` as an arbitrary
517 * Same return value semantic as `support_info_query_all_sources`.
521 int auto_discover_source_for_input_as_string(const char *input
,
522 uint64_t original_input_index
,
523 size_t plugin_count
, const char *plugin_restrict
,
524 const char *component_class_restrict
,
525 enum bt_logging_level log_level
,
526 struct auto_source_discovery
*auto_disc
)
528 return support_info_query_all_sources(input
, "string",
529 original_input_index
, plugin_count
, plugin_restrict
,
530 component_class_restrict
, log_level
, auto_disc
);
534 int auto_discover_source_for_input_as_dir_or_file_rec(GString
*input
,
535 uint64_t original_input_index
,
536 size_t plugin_count
, const char *plugin_restrict
,
537 const char *component_class_restrict
,
538 enum bt_logging_level log_level
,
539 struct auto_source_discovery
*auto_disc
)
542 GError
*error
= NULL
;
544 if (g_file_test(input
->str
, G_FILE_TEST_IS_REGULAR
)) {
546 status
= support_info_query_all_sources(input
->str
,
547 "file", original_input_index
, plugin_count
,
548 plugin_restrict
, component_class_restrict
, log_level
, auto_disc
);
549 } else if (g_file_test(input
->str
, G_FILE_TEST_IS_DIR
)) {
552 gsize saved_input_len
;
555 /* It's a directory. */
556 status
= support_info_query_all_sources(input
->str
,
557 "directory", original_input_index
, plugin_count
,
558 plugin_restrict
, component_class_restrict
, log_level
,
564 } else if (status
== 0) {
566 * A component class claimed this input as a directory,
572 dir
= g_dir_open(input
->str
, 0, &error
);
574 const char *fmt
= "Failed to open directory %s: %s";
575 BT_LOGW(fmt
, input
->str
, error
->message
);
577 if (error
->code
== G_FILE_ERROR_ACCES
) {
578 /* This is not a fatal error, we just skip it. */
582 BT_CLI_LOGE_APPEND_CAUSE(fmt
, input
->str
,
588 saved_input_len
= input
->len
;
592 dirent
= g_dir_read_name(dir
);
594 g_string_append_c_inline(input
, G_DIR_SEPARATOR
);
595 g_string_append(input
, dirent
);
597 status
= auto_discover_source_for_input_as_dir_or_file_rec(
598 input
, original_input_index
, plugin_count
,
599 plugin_restrict
, component_class_restrict
,
600 log_level
, auto_disc
);
602 g_string_truncate(input
, saved_input_len
);
607 } else if (status
== 0) {
610 } else if (errno
!= 0) {
611 BT_LOGW_ERRNO("Failed to read directory entry", ": dir=%s", input
->str
);
620 BT_LOGD("Skipping %s, not a file or directory", input
->str
);
639 * Look for a source component class that recognizes `input` as a directory or
640 * file. If `input` is a directory and is not directly recognized, recurse and
641 * apply the same logic to children nodes.
643 * Same return value semantic as `support_info_query_all_sources`.
647 int auto_discover_source_for_input_as_dir_or_file(const char *input
,
648 uint64_t original_input_index
,
649 size_t plugin_count
, const char *plugin_restrict
,
650 const char *component_class_restrict
,
651 enum bt_logging_level log_level
,
652 struct auto_source_discovery
*auto_disc
)
654 GString
*mutable_input
;
657 mutable_input
= g_string_new(input
);
658 if (!mutable_input
) {
663 status
= auto_discover_source_for_input_as_dir_or_file_rec(
664 mutable_input
, original_input_index
, plugin_count
, plugin_restrict
,
665 component_class_restrict
, log_level
, auto_disc
);
667 g_string_free(mutable_input
, TRUE
);
672 int auto_discover_source_components(
673 const bt_value
*plugin_paths
,
674 const bt_value
*inputs
,
675 const char *plugin_restrict
,
676 const char *component_class_restrict
,
677 enum bt_logging_level log_level
,
678 struct auto_source_discovery
*auto_disc
)
680 uint64_t i_inputs
, input_count
;
684 input_count
= bt_value_array_get_size(inputs
);
686 status
= require_loaded_plugins(plugin_paths
);
691 plugin_count
= get_loaded_plugins_count();
693 for (i_inputs
= 0; i_inputs
< input_count
; i_inputs
++) {
694 const bt_value
*input_value
;
697 input_value
= bt_value_array_borrow_element_by_index_const(inputs
, i_inputs
);
698 input
= bt_value_string_get(input_value
);
699 status
= auto_discover_source_for_input_as_string(input
, i_inputs
,
700 plugin_count
, plugin_restrict
, component_class_restrict
,
701 log_level
, auto_disc
);
705 } else if (status
== 0) {
706 /* A component class has claimed this input as an arbitrary string. */
710 status
= auto_discover_source_for_input_as_dir_or_file(input
,
711 i_inputs
, plugin_count
, plugin_restrict
,
712 component_class_restrict
, log_level
, auto_disc
);
716 } else if (status
== 0) {
718 * This input (or something under it) was recognized.
723 BT_LOGW("No trace was found based on input `%s`.", input
);