| 1 | /* |
| 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
| 3 | * |
| 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: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 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 |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #define BT_LOG_TAG "LIB/QUERY-EXECUTOR" |
| 24 | #include "lib/logging.h" |
| 25 | |
| 26 | #include "common/assert.h" |
| 27 | #include "common/common.h" |
| 28 | #include "lib/assert-pre.h" |
| 29 | #include <babeltrace2/graph/query-executor-const.h> |
| 30 | #include <babeltrace2/graph/query-executor.h> |
| 31 | #include <babeltrace2/graph/component-class.h> |
| 32 | #include <babeltrace2/value.h> |
| 33 | #include <babeltrace2/value-const.h> |
| 34 | #include "lib/object.h" |
| 35 | #include "compat/compiler.h" |
| 36 | |
| 37 | #include "component-class.h" |
| 38 | #include "query-executor.h" |
| 39 | |
| 40 | static |
| 41 | void bt_query_executor_destroy(struct bt_object *obj) |
| 42 | { |
| 43 | struct bt_query_executor *query_exec = |
| 44 | container_of(obj, struct bt_query_executor, base); |
| 45 | |
| 46 | BT_LOGD("Destroying query executor: addr=%p", query_exec); |
| 47 | g_free(query_exec); |
| 48 | } |
| 49 | |
| 50 | struct bt_query_executor *bt_query_executor_create(void) |
| 51 | { |
| 52 | struct bt_query_executor *query_exec; |
| 53 | |
| 54 | BT_LOGD_STR("Creating query executor."); |
| 55 | query_exec = g_new0(struct bt_query_executor, 1); |
| 56 | if (!query_exec) { |
| 57 | BT_LOGE_STR("Failed to allocate one query executor."); |
| 58 | goto end; |
| 59 | } |
| 60 | |
| 61 | bt_object_init_shared(&query_exec->base, |
| 62 | bt_query_executor_destroy); |
| 63 | BT_LOGD("Created query executor: addr=%p", query_exec); |
| 64 | |
| 65 | end: |
| 66 | return (void *) query_exec; |
| 67 | } |
| 68 | |
| 69 | enum bt_query_executor_status bt_query_executor_query( |
| 70 | struct bt_query_executor *query_exec, |
| 71 | const struct bt_component_class *comp_cls, |
| 72 | const char *object, const struct bt_value *params, |
| 73 | bt_logging_level log_level, |
| 74 | const struct bt_value **user_result) |
| 75 | { |
| 76 | typedef enum bt_query_status (*method_t)(void *, const void *, |
| 77 | const void *, const void *, bt_logging_level, const void *); |
| 78 | |
| 79 | enum bt_query_status status; |
| 80 | enum bt_query_executor_status exec_status; |
| 81 | method_t method = NULL; |
| 82 | |
| 83 | BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor"); |
| 84 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); |
| 85 | BT_ASSERT_PRE_NON_NULL(object, "Object"); |
| 86 | BT_ASSERT_PRE_NON_NULL(user_result, "Result (output)"); |
| 87 | BT_ASSERT_PRE(!query_exec->canceled, "Query executor is canceled."); |
| 88 | |
| 89 | if (!params) { |
| 90 | params = bt_value_null; |
| 91 | } |
| 92 | |
| 93 | switch (comp_cls->type) { |
| 94 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 95 | { |
| 96 | struct bt_component_class_source *src_cc = (void *) comp_cls; |
| 97 | |
| 98 | method = (method_t) src_cc->methods.query; |
| 99 | break; |
| 100 | } |
| 101 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 102 | { |
| 103 | struct bt_component_class_filter *flt_cc = (void *) comp_cls; |
| 104 | |
| 105 | method = (method_t) flt_cc->methods.query; |
| 106 | break; |
| 107 | } |
| 108 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 109 | { |
| 110 | struct bt_component_class_sink *sink_cc = (void *) comp_cls; |
| 111 | |
| 112 | method = (method_t) sink_cc->methods.query; |
| 113 | break; |
| 114 | } |
| 115 | default: |
| 116 | abort(); |
| 117 | } |
| 118 | |
| 119 | if (!method) { |
| 120 | /* Not an error: nothing to query */ |
| 121 | BT_LIB_LOGD("Component class has no registered query method: " |
| 122 | "%!+C", comp_cls); |
| 123 | exec_status = BT_QUERY_EXECUTOR_STATUS_UNSUPPORTED; |
| 124 | goto end; |
| 125 | } |
| 126 | |
| 127 | BT_LIB_LOGD("Calling user's query method: " |
| 128 | "query-exec-addr=%p, %![cc-]+C, object=\"%s\", %![params-]+v, " |
| 129 | "log-level=%s", |
| 130 | query_exec, comp_cls, object, params, |
| 131 | bt_common_logging_level_string(log_level)); |
| 132 | *user_result = NULL; |
| 133 | status = method((void *) comp_cls, query_exec, object, params, |
| 134 | log_level, user_result); |
| 135 | BT_LIB_LOGD("User method returned: status=%s, %![res-]+v", |
| 136 | bt_query_status_string(status), *user_result); |
| 137 | BT_ASSERT_PRE(status != BT_QUERY_STATUS_OK || *user_result, |
| 138 | "User method returned `BT_QUERY_STATUS_OK` without a result."); |
| 139 | exec_status = (int) status; |
| 140 | if (query_exec->canceled) { |
| 141 | BT_OBJECT_PUT_REF_AND_RESET(*user_result); |
| 142 | exec_status = BT_QUERY_EXECUTOR_STATUS_CANCELED; |
| 143 | goto end; |
| 144 | } |
| 145 | |
| 146 | end: |
| 147 | return exec_status; |
| 148 | } |
| 149 | |
| 150 | enum bt_query_executor_status bt_query_executor_cancel( |
| 151 | struct bt_query_executor *query_exec) |
| 152 | { |
| 153 | BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor"); |
| 154 | query_exec->canceled = BT_TRUE; |
| 155 | BT_LOGI("Canceled query executor: addr=%p", query_exec); |
| 156 | return BT_QUERY_EXECUTOR_STATUS_OK; |
| 157 | } |
| 158 | |
| 159 | bt_bool bt_query_executor_is_canceled(const struct bt_query_executor *query_exec) |
| 160 | { |
| 161 | BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor"); |
| 162 | return query_exec->canceled; |
| 163 | } |
| 164 | |
| 165 | void bt_query_executor_get_ref(const struct bt_query_executor *query_executor) |
| 166 | { |
| 167 | bt_object_get_ref(query_executor); |
| 168 | } |
| 169 | |
| 170 | void bt_query_executor_put_ref(const struct bt_query_executor *query_executor) |
| 171 | { |
| 172 | bt_object_put_ref(query_executor); |
| 173 | } |