| 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 | */ |
| 50 | static |
| 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); |
| 66 | for (i = 0; i < bt_value_array_get_length(inputs); i++) { |
| 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 | #define BT_FMT "Failed to create a map value." |
| 74 | BT_LOGE_STR(BT_FMT); |
| 75 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, BT_FMT); |
| 76 | #undef BT_FMT |
| 77 | PyErr_NoMemory(); |
| 78 | goto end; |
| 79 | } |
| 80 | |
| 81 | status = auto_source_discovery_init(&auto_disc); |
| 82 | if (status != 0) { |
| 83 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 84 | "Failed to initialize auto source discovery structure."); |
| 85 | goto error; |
| 86 | } |
| 87 | |
| 88 | /* Create array of bt_plugin pointers from plugin set. */ |
| 89 | plugins = g_new(const bt_plugin *, plugin_count); |
| 90 | if (!plugins) { |
| 91 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 92 | "Failed to allocate plugins array."); |
| 93 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
| 94 | goto error; |
| 95 | } |
| 96 | |
| 97 | for (i = 0; i < plugin_count; i++) { |
| 98 | plugins[i] = bt_plugin_set_borrow_plugin_by_index_const(plugin_set, i); |
| 99 | } |
| 100 | |
| 101 | status = auto_discover_source_components( |
| 102 | inputs, |
| 103 | plugins, |
| 104 | plugin_count, |
| 105 | NULL, |
| 106 | bt_python_bindings_bt2_log_level, |
| 107 | &auto_disc, |
| 108 | NULL); |
| 109 | if (status != 0) { |
| 110 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 111 | "Failed to auto discover sources."); |
| 112 | goto error; |
| 113 | } |
| 114 | |
| 115 | components_list = bt_value_array_create(); |
| 116 | if (!components_list) { |
| 117 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 118 | "Failed to allocate one array value."); |
| 119 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
| 120 | goto error; |
| 121 | } |
| 122 | |
| 123 | insert_entry_status = bt_value_map_insert_entry(result, "results", components_list); |
| 124 | if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 125 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 126 | "Failed to insert a map entry."); |
| 127 | status = (int) insert_entry_status; |
| 128 | goto error; |
| 129 | } |
| 130 | |
| 131 | for (i = 0; i < auto_disc.results->len; i++) { |
| 132 | struct auto_source_discovery_result *autodisc_result = |
| 133 | g_ptr_array_index(auto_disc.results, i); |
| 134 | bt_value_array_append_element_status append_element_status; |
| 135 | |
| 136 | component_info = bt_value_array_create(); |
| 137 | if (!component_info) { |
| 138 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 139 | "Failed to allocate one array value."); |
| 140 | status = __BT_FUNC_STATUS_MEMORY_ERROR; |
| 141 | goto error; |
| 142 | } |
| 143 | |
| 144 | append_element_status = bt_value_array_append_string_element( |
| 145 | component_info, autodisc_result->plugin_name); |
| 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_string_element( |
| 154 | component_info, autodisc_result->source_cc_name); |
| 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( |
| 163 | component_info, autodisc_result->inputs); |
| 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 | append_element_status = bt_value_array_append_element( |
| 172 | component_info, autodisc_result->original_input_indices); |
| 173 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
| 174 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 175 | "Failed to append one array element."); |
| 176 | status = (int) append_element_status; |
| 177 | goto error; |
| 178 | } |
| 179 | |
| 180 | append_element_status = bt_value_array_append_element(components_list, |
| 181 | component_info); |
| 182 | if (append_element_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { |
| 183 | BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(module_name, |
| 184 | "Failed to append one array element."); |
| 185 | status = (int) append_element_status; |
| 186 | goto error; |
| 187 | } |
| 188 | |
| 189 | BT_VALUE_PUT_REF_AND_RESET(component_info); |
| 190 | } |
| 191 | |
| 192 | status = 0; |
| 193 | goto end; |
| 194 | error: |
| 195 | BT_ASSERT(status != 0); |
| 196 | |
| 197 | end: |
| 198 | if (result) { |
| 199 | insert_entry_status = bt_value_map_insert_signed_integer_entry(result, "status", status); |
| 200 | if (insert_entry_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { |
| 201 | BT_VALUE_PUT_REF_AND_RESET(result); |
| 202 | PyErr_NoMemory(); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | auto_source_discovery_fini(&auto_disc); |
| 207 | g_free(plugins); |
| 208 | bt_value_put_ref(components_list); |
| 209 | bt_value_put_ref(component_info); |
| 210 | |
| 211 | return result; |
| 212 | } |