Commit | Line | Data |
---|---|---|
94e72386 SM |
1 | /* |
2 | * The MIT License (MIT) | |
3 | * | |
4 | * Copyright (c) 2016 Philippe Proulx <pproulx@efficios.com> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include <autodisc/autodisc.h> | |
26 | #include <common/common.h> | |
27 | ||
28 | /* | |
29 | * Interface between the Python bindings and the auto source discovery system: | |
30 | * input strings go in, specs for components to instantiate go out. | |
31 | * | |
32 | * `inputs` must be an array of strings, the list of inputs in which to look | |
33 | * for traces. `plugin_set` is the set of plugins to query. | |
34 | * | |
35 | * Returns a map with the following entries: | |
36 | * | |
37 | * - status: signed integer, return status of this function | |
38 | * - results: array, each element is an array describing one auto | |
39 | * source discovery result (see `struct | |
40 | * auto_source_discovery_result` for more details about these): | |
41 | * | |
42 | * - 0: plugin name, string | |
43 | * - 1: class name, string | |
44 | * - 2: inputs, array of strings | |
45 | * - 3: original input indices, array of unsigned integers | |
46 | * | |
47 | * This function can also return None, if it failed to allocate memory | |
48 | * for the return value and status code. | |
49 | */ | |
f3847c75 | 50 | static |
94e72386 SM |
51 | bt_value *bt_bt2_auto_discover_source_components(const bt_value *inputs, |
52 | const bt_plugin_set *plugin_set) | |
53 | { | |
54 | uint64_t i; | |
55 | int status = 0; | |
56 | static const char * const module_name = "Automatic source discovery"; | |
57 | const bt_plugin **plugins = NULL; | |
58 | const uint64_t plugin_count = bt_plugin_set_get_plugin_count(plugin_set); | |
59 | struct auto_source_discovery auto_disc = { 0 }; | |
60 | bt_value *result = NULL; | |
61 | bt_value *components_list = NULL; | |
62 | bt_value *component_info = NULL; | |
63 | bt_value_map_insert_entry_status insert_entry_status; | |
64 | ||
65 | BT_ASSERT(bt_value_get_type(inputs) == BT_VALUE_TYPE_ARRAY); | |
1270d0e9 | 66 | for (i = 0; i < bt_value_array_get_length(inputs); i++) { |
94e72386 SM |
67 | const bt_value *elem = bt_value_array_borrow_element_by_index_const(inputs, i); |
68 | BT_ASSERT(bt_value_get_type(elem) == BT_VALUE_TYPE_STRING); | |
69 | } | |
70 | ||
71 | result = bt_value_map_create(); | |
72 | if (!result) { | |
73 | static const char * const err = "Failed to create a map value."; | |
74 | BT_LOGE_STR(err); | |
75 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, err); | |
76 | PyErr_NoMemory(); | |
77 | goto end; | |
78 | } | |
79 | ||
80 | status = auto_source_discovery_init(&auto_disc); | |
81 | if (status != 0) { | |
82 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
83 | "Failed to initialize auto source discovery structure."); | |
84 | goto error; | |
85 | } | |
86 | ||
87 | /* Create array of bt_plugin pointers from plugin set. */ | |
88 | plugins = g_new(const bt_plugin *, plugin_count); | |
89 | if (!plugins) { | |
90 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
91 | "Failed to allocate plugins array."); | |
92 | status = __BT_FUNC_STATUS_MEMORY_ERROR; | |
93 | goto error; | |
94 | } | |
95 | ||
96 | for (i = 0; i < plugin_count; i++) { | |
97 | plugins[i] = bt_plugin_set_borrow_plugin_by_index_const(plugin_set, i); | |
98 | } | |
99 | ||
100 | status = auto_discover_source_components( | |
101 | inputs, | |
102 | plugins, | |
103 | plugin_count, | |
104 | NULL, | |
105 | bt_python_bindings_bt2_log_level, | |
3f8644ec SM |
106 | &auto_disc, |
107 | NULL); | |
94e72386 SM |
108 | if (status != 0) { |
109 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
110 | "Failed to auto discover sources."); | |
111 | goto error; | |
112 | } | |
113 | ||
114 | components_list = bt_value_array_create(); | |
115 | if (!components_list) { | |
116 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
117 | "Failed to allocate one array value."); | |
118 | status = __BT_FUNC_STATUS_MEMORY_ERROR; | |
119 | goto error; | |
120 | } | |
121 | ||
122 | insert_entry_status = bt_value_map_insert_entry(result, "results", components_list); | |
123 | if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { | |
124 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
125 | "Failed to insert a map entry."); | |
126 | status = (int) insert_entry_status; | |
127 | goto error; | |
128 | } | |
129 | ||
130 | for (i = 0; i < auto_disc.results->len; i++) { | |
96ec9323 | 131 | struct auto_source_discovery_result *autodisc_result = |
94e72386 SM |
132 | g_ptr_array_index(auto_disc.results, i); |
133 | bt_value_array_append_element_status append_element_status; | |
134 | ||
135 | component_info = bt_value_array_create(); | |
136 | if (!component_info) { | |
137 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
138 | "Failed to allocate one array value."); | |
139 | status = __BT_FUNC_STATUS_MEMORY_ERROR; | |
140 | goto error; | |
141 | } | |
142 | ||
143 | append_element_status = bt_value_array_append_string_element( | |
96ec9323 | 144 | component_info, autodisc_result->plugin_name); |
94e72386 SM |
145 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
146 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
147 | "Failed to append one array element."); | |
148 | status = (int) append_element_status; | |
149 | goto error; | |
150 | } | |
151 | ||
152 | append_element_status = bt_value_array_append_string_element( | |
96ec9323 | 153 | component_info, autodisc_result->source_cc_name); |
94e72386 SM |
154 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
155 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
156 | "Failed to append one array element."); | |
157 | status = (int) append_element_status; | |
158 | goto error; | |
159 | } | |
160 | ||
161 | append_element_status = bt_value_array_append_element( | |
96ec9323 | 162 | component_info, autodisc_result->inputs); |
94e72386 SM |
163 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
164 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
165 | "Failed to append one array element."); | |
166 | status = (int) append_element_status; | |
167 | goto error; | |
168 | } | |
169 | ||
170 | append_element_status = bt_value_array_append_element( | |
96ec9323 | 171 | component_info, autodisc_result->original_input_indices); |
94e72386 SM |
172 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
173 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
174 | "Failed to append one array element."); | |
175 | status = (int) append_element_status; | |
176 | goto error; | |
177 | } | |
178 | ||
179 | append_element_status = bt_value_array_append_element(components_list, | |
180 | component_info); | |
181 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { | |
182 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, | |
183 | "Failed to append one array element."); | |
184 | status = (int) append_element_status; | |
185 | goto error; | |
186 | } | |
187 | ||
188 | BT_VALUE_PUT_REF_AND_RESET(component_info); | |
189 | } | |
190 | ||
191 | status = 0; | |
192 | goto end; | |
193 | error: | |
194 | BT_ASSERT(status != 0); | |
195 | ||
196 | end: | |
197 | if (result) { | |
198 | insert_entry_status = bt_value_map_insert_signed_integer_entry(result, "status", status); | |
199 | if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { | |
200 | BT_VALUE_PUT_REF_AND_RESET(result); | |
201 | PyErr_NoMemory(); | |
202 | } | |
203 | } | |
204 | ||
205 | auto_source_discovery_fini(&auto_disc); | |
206 | g_free(plugins); | |
207 | bt_value_put_ref(components_list); | |
208 | bt_value_put_ref(component_info); | |
209 | ||
210 | return result; | |
211 | } |