Commit | Line | Data |
---|---|---|
f3c9a159 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
f3c9a159 SM |
3 | * |
4 | * Copyright (c) 2016 Philippe Proulx <pproulx@efficios.com> | |
f3c9a159 SM |
5 | */ |
6 | ||
7 | #include <autodisc/autodisc.h> | |
8 | #include <common/common.h> | |
9 | ||
10 | /* | |
11 | * Interface between the Python bindings and the auto source discovery system: | |
12 | * input strings go in, specs for components to instantiate go out. | |
13 | * | |
14 | * `inputs` must be an array of strings, the list of inputs in which to look | |
15 | * for traces. `plugin_set` is the set of plugins to query. | |
16 | * | |
17 | * Returns a map with the following entries: | |
18 | * | |
19 | * - status: signed integer, return status of this function | |
20 | * - results: array, each element is an array describing one auto | |
21 | * source discovery result (see `struct | |
22 | * auto_source_discovery_result` for more details about these): | |
23 | * | |
24 | * - 0: plugin name, string | |
25 | * - 1: class name, string | |
26 | * - 2: inputs, array of strings | |
27 | * - 3: original input indices, array of unsigned integers | |
28 | * | |
29 | * This function can also return None, if it failed to allocate memory | |
30 | * for the return value and status code. | |
31 | */ | |
7c7301d5 | 32 | static |
f3c9a159 SM |
33 | bt_value *bt_bt2_auto_discover_source_components(const bt_value *inputs, |
34 | const bt_plugin_set *plugin_set) | |
35 | { | |
36 | uint64_t i; | |
37 | int status = 0; | |
38 | static const char * const module_name = "Automatic source discovery"; | |
39 | const bt_plugin **plugins = NULL; | |
40 | const uint64_t plugin_count = bt_plugin_set_get_plugin_count(plugin_set); | |
41 | struct auto_source_discovery auto_disc = { 0 }; | |
42 | bt_value *result = NULL; | |
43 | bt_value *components_list = NULL; | |
44 | bt_value *component_info = NULL; | |
45 | bt_value_map_insert_entry_status insert_entry_status; | |
46 | ||
47 | BT_ASSERT(bt_value_get_type(inputs) == BT_VALUE_TYPE_ARRAY); | |
393729a6 | 48 | for (i = 0; i < bt_value_array_get_length(inputs); i++) { |
f3c9a159 SM |
49 | const bt_value *elem = bt_value_array_borrow_element_by_index_const(inputs, i); |
50 | BT_ASSERT(bt_value_get_type(elem) == BT_VALUE_TYPE_STRING); | |
51 | } | |
52 | ||
53 | result = bt_value_map_create(); | |
54 | if (!result) { | |
8b305066 SM |
55 | #define BT_FMT "Failed to create a map value." |
56 | BT_LOGE_STR(BT_FMT); | |
57 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, BT_FMT); | |
58 | #undef BT_FMT | |
f3c9a159 SM |
59 | PyErr_NoMemory(); |
60 | goto end; | |
61 | } | |
62 | ||
63 | status = auto_source_discovery_init(&auto_disc); | |
64 | if (status != 0) { | |
65 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
66 | "Failed to initialize auto source discovery structure."); | |
67 | goto error; | |
68 | } | |
69 | ||
70 | /* Create array of bt_plugin pointers from plugin set. */ | |
71 | plugins = g_new(const bt_plugin *, plugin_count); | |
72 | if (!plugins) { | |
73 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
74 | "Failed to allocate plugins array."); | |
75 | status = __BT_FUNC_STATUS_MEMORY_ERROR; | |
76 | goto error; | |
77 | } | |
78 | ||
79 | for (i = 0; i < plugin_count; i++) { | |
80 | plugins[i] = bt_plugin_set_borrow_plugin_by_index_const(plugin_set, i); | |
81 | } | |
82 | ||
83 | status = auto_discover_source_components( | |
84 | inputs, | |
85 | plugins, | |
86 | plugin_count, | |
87 | NULL, | |
88 | bt_python_bindings_bt2_log_level, | |
3dae1685 SM |
89 | &auto_disc, |
90 | NULL); | |
f3c9a159 SM |
91 | if (status != 0) { |
92 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
93 | "Failed to auto discover sources."); | |
94 | goto error; | |
95 | } | |
96 | ||
97 | components_list = bt_value_array_create(); | |
98 | if (!components_list) { | |
99 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
100 | "Failed to allocate one array value."); | |
101 | status = __BT_FUNC_STATUS_MEMORY_ERROR; | |
102 | goto error; | |
103 | } | |
104 | ||
105 | insert_entry_status = bt_value_map_insert_entry(result, "results", components_list); | |
106 | if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { | |
107 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
108 | "Failed to insert a map entry."); | |
109 | status = (int) insert_entry_status; | |
110 | goto error; | |
111 | } | |
112 | ||
113 | for (i = 0; i < auto_disc.results->len; i++) { | |
50233236 | 114 | struct auto_source_discovery_result *autodisc_result = |
f3c9a159 SM |
115 | g_ptr_array_index(auto_disc.results, i); |
116 | bt_value_array_append_element_status append_element_status; | |
117 | ||
118 | component_info = bt_value_array_create(); | |
119 | if (!component_info) { | |
120 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
121 | "Failed to allocate one array value."); | |
122 | status = __BT_FUNC_STATUS_MEMORY_ERROR; | |
123 | goto error; | |
124 | } | |
125 | ||
126 | append_element_status = bt_value_array_append_string_element( | |
50233236 | 127 | component_info, autodisc_result->plugin_name); |
f3c9a159 SM |
128 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
129 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
130 | "Failed to append one array element."); | |
131 | status = (int) append_element_status; | |
132 | goto error; | |
133 | } | |
134 | ||
135 | append_element_status = bt_value_array_append_string_element( | |
50233236 | 136 | component_info, autodisc_result->source_cc_name); |
f3c9a159 SM |
137 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
138 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
139 | "Failed to append one array element."); | |
140 | status = (int) append_element_status; | |
141 | goto error; | |
142 | } | |
143 | ||
144 | append_element_status = bt_value_array_append_element( | |
50233236 | 145 | component_info, autodisc_result->inputs); |
f3c9a159 SM |
146 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
147 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
148 | "Failed to append one array element."); | |
149 | status = (int) append_element_status; | |
150 | goto error; | |
151 | } | |
152 | ||
153 | append_element_status = bt_value_array_append_element( | |
50233236 | 154 | component_info, autodisc_result->original_input_indices); |
f3c9a159 SM |
155 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
156 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
157 | "Failed to append one array element."); | |
158 | status = (int) append_element_status; | |
159 | goto error; | |
160 | } | |
161 | ||
162 | append_element_status = bt_value_array_append_element(components_list, | |
163 | component_info); | |
164 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { | |
165 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
166 | "Failed to append one array element."); | |
167 | status = (int) append_element_status; | |
168 | goto error; | |
169 | } | |
170 | ||
171 | BT_VALUE_PUT_REF_AND_RESET(component_info); | |
172 | } | |
173 | ||
174 | status = 0; | |
175 | goto end; | |
176 | error: | |
177 | BT_ASSERT(status != 0); | |
178 | ||
179 | end: | |
180 | if (result) { | |
181 | insert_entry_status = bt_value_map_insert_signed_integer_entry(result, "status", status); | |
182 | if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { | |
183 | BT_VALUE_PUT_REF_AND_RESET(result); | |
184 | PyErr_NoMemory(); | |
185 | } | |
186 | } | |
187 | ||
188 | auto_source_discovery_fini(&auto_disc); | |
189 | g_free(plugins); | |
190 | bt_value_put_ref(components_list); | |
191 | bt_value_put_ref(component_info); | |
192 | ||
193 | return result; | |
194 | } |