Commit | Line | Data |
---|---|---|
c7eee084 | 1 | /* |
e2f7325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
c7eee084 PP |
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 | ||
350ad6c1 | 23 | #define BT_LOG_TAG "LIB/QUERY-EXECUTOR" |
c2d9d9cf | 24 | #include "lib/logging.h" |
c7eee084 | 25 | |
578e048b | 26 | #include "common/assert.h" |
f4e38e70 | 27 | #include "common/common.h" |
578e048b | 28 | #include "lib/assert-pre.h" |
f6f301d7 | 29 | #include "lib/assert-post.h" |
3fadfbc0 MJ |
30 | #include <babeltrace2/graph/query-executor-const.h> |
31 | #include <babeltrace2/graph/query-executor.h> | |
3fadfbc0 | 32 | #include <babeltrace2/graph/component-class.h> |
3fadfbc0 MJ |
33 | #include <babeltrace2/value.h> |
34 | #include <babeltrace2/value-const.h> | |
578e048b MJ |
35 | #include "lib/object.h" |
36 | #include "compat/compiler.h" | |
37 | ||
38 | #include "component-class.h" | |
39 | #include "query-executor.h" | |
9b4f9b42 | 40 | #include "interrupter.h" |
d24d5663 | 41 | #include "lib/func-status.h" |
c7eee084 PP |
42 | |
43 | static | |
44 | void bt_query_executor_destroy(struct bt_object *obj) | |
45 | { | |
46 | struct bt_query_executor *query_exec = | |
47 | container_of(obj, struct bt_query_executor, base); | |
48 | ||
d94d92ac | 49 | BT_LOGD("Destroying query executor: addr=%p", query_exec); |
9b4f9b42 PP |
50 | |
51 | if (query_exec->interrupters) { | |
52 | BT_LOGD_STR("Putting interrupters."); | |
53 | g_ptr_array_free(query_exec->interrupters, TRUE); | |
54 | query_exec->interrupters = NULL; | |
55 | } | |
56 | ||
57 | BT_OBJECT_PUT_REF_AND_RESET(query_exec->default_interrupter); | |
58 | ||
c7eee084 PP |
59 | g_free(query_exec); |
60 | } | |
61 | ||
0d72b8c3 | 62 | struct bt_query_executor *bt_query_executor_create(void) |
c7eee084 PP |
63 | { |
64 | struct bt_query_executor *query_exec; | |
65 | ||
66 | BT_LOGD_STR("Creating query executor."); | |
67 | query_exec = g_new0(struct bt_query_executor, 1); | |
68 | if (!query_exec) { | |
870631a2 PP |
69 | BT_LIB_LOGE_APPEND_CAUSE( |
70 | "Failed to allocate one query executor."); | |
c7eee084 PP |
71 | goto end; |
72 | } | |
73 | ||
9b4f9b42 PP |
74 | query_exec->interrupters = g_ptr_array_new_with_free_func( |
75 | (GDestroyNotify) bt_object_put_no_null_check); | |
76 | if (!query_exec->interrupters) { | |
77 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); | |
78 | BT_OBJECT_PUT_REF_AND_RESET(query_exec); | |
79 | goto end; | |
80 | } | |
81 | ||
82 | query_exec->default_interrupter = bt_interrupter_create(); | |
83 | if (!query_exec->default_interrupter) { | |
84 | BT_LIB_LOGE_APPEND_CAUSE( | |
85 | "Failed to create one interrupter object."); | |
86 | BT_OBJECT_PUT_REF_AND_RESET(query_exec); | |
87 | goto end; | |
88 | } | |
89 | ||
90 | bt_query_executor_add_interrupter(query_exec, | |
91 | query_exec->default_interrupter); | |
3fea54f6 PP |
92 | bt_object_init_shared(&query_exec->base, |
93 | bt_query_executor_destroy); | |
c7eee084 PP |
94 | BT_LOGD("Created query executor: addr=%p", query_exec); |
95 | ||
96 | end: | |
d94d92ac | 97 | return (void *) query_exec; |
c7eee084 PP |
98 | } |
99 | ||
d24d5663 | 100 | enum bt_query_executor_query_status bt_query_executor_query( |
0d72b8c3 PP |
101 | struct bt_query_executor *query_exec, |
102 | const struct bt_component_class *comp_cls, | |
05e21286 | 103 | const char *object, const struct bt_value *params, |
d24d5663 | 104 | enum bt_logging_level log_level, |
05e21286 | 105 | const struct bt_value **user_result) |
c7eee084 | 106 | { |
d24d5663 PP |
107 | typedef enum bt_component_class_query_method_status (*method_t)(void *, |
108 | const void *, const void *, const void *, enum bt_logging_level, | |
109 | const void *); | |
c7eee084 | 110 | |
d24d5663 PP |
111 | enum bt_query_executor_query_status status; |
112 | enum bt_component_class_query_method_status query_status; | |
d94d92ac | 113 | method_t method = NULL; |
c7eee084 | 114 | |
d94d92ac PP |
115 | BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor"); |
116 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); | |
117 | BT_ASSERT_PRE_NON_NULL(object, "Object"); | |
118 | BT_ASSERT_PRE_NON_NULL(user_result, "Result (output)"); | |
d94d92ac PP |
119 | |
120 | if (!params) { | |
121 | params = bt_value_null; | |
c7eee084 PP |
122 | } |
123 | ||
d94d92ac PP |
124 | switch (comp_cls->type) { |
125 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
126 | { | |
127 | struct bt_component_class_source *src_cc = (void *) comp_cls; | |
128 | ||
129 | method = (method_t) src_cc->methods.query; | |
130 | break; | |
c7eee084 | 131 | } |
d94d92ac PP |
132 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
133 | { | |
134 | struct bt_component_class_filter *flt_cc = (void *) comp_cls; | |
c7eee084 | 135 | |
d94d92ac PP |
136 | method = (method_t) flt_cc->methods.query; |
137 | break; | |
c7eee084 | 138 | } |
d94d92ac PP |
139 | case BT_COMPONENT_CLASS_TYPE_SINK: |
140 | { | |
141 | struct bt_component_class_sink *sink_cc = (void *) comp_cls; | |
c7eee084 | 142 | |
d94d92ac PP |
143 | method = (method_t) sink_cc->methods.query; |
144 | break; | |
145 | } | |
146 | default: | |
147 | abort(); | |
c7eee084 PP |
148 | } |
149 | ||
d94d92ac | 150 | if (!method) { |
c7eee084 | 151 | /* Not an error: nothing to query */ |
d94d92ac PP |
152 | BT_LIB_LOGD("Component class has no registered query method: " |
153 | "%!+C", comp_cls); | |
2c1d3e91 | 154 | status = BT_FUNC_STATUS_INVALID_OBJECT; |
c7eee084 PP |
155 | goto end; |
156 | } | |
157 | ||
d94d92ac | 158 | BT_LIB_LOGD("Calling user's query method: " |
f4e38e70 PP |
159 | "query-exec-addr=%p, %![cc-]+C, object=\"%s\", %![params-]+v, " |
160 | "log-level=%s", | |
161 | query_exec, comp_cls, object, params, | |
162 | bt_common_logging_level_string(log_level)); | |
d94d92ac | 163 | *user_result = NULL; |
d24d5663 | 164 | query_status = method((void *) comp_cls, query_exec, object, params, |
f4e38e70 | 165 | log_level, user_result); |
d94d92ac | 166 | BT_LIB_LOGD("User method returned: status=%s, %![res-]+v", |
d24d5663 PP |
167 | bt_common_func_status_string(query_status), *user_result); |
168 | BT_ASSERT_POST(query_status != BT_FUNC_STATUS_OK || *user_result, | |
169 | "User method returned `BT_FUNC_STATUS_OK` without a result."); | |
170 | status = (int) query_status; | |
c7eee084 PP |
171 | |
172 | end: | |
d24d5663 | 173 | return status; |
c7eee084 PP |
174 | } |
175 | ||
9b4f9b42 PP |
176 | enum bt_query_executor_add_interrupter_status bt_query_executor_add_interrupter( |
177 | struct bt_query_executor *query_exec, | |
178 | const struct bt_interrupter *intr) | |
c7eee084 | 179 | { |
d94d92ac | 180 | BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor"); |
9b4f9b42 PP |
181 | BT_ASSERT_PRE_NON_NULL(intr, "Interrupter"); |
182 | g_ptr_array_add(query_exec->interrupters, (void *) intr); | |
183 | bt_object_get_no_null_check(intr); | |
184 | BT_LIB_LOGD("Added interrupter to query executor: " | |
185 | "query-exec-addr=%p, %![intr-]+z", | |
186 | query_exec, intr); | |
d24d5663 | 187 | return BT_FUNC_STATUS_OK; |
c7eee084 PP |
188 | } |
189 | ||
9b4f9b42 | 190 | bt_bool bt_query_executor_is_interrupted(const struct bt_query_executor *query_exec) |
c7eee084 | 191 | { |
9b4f9b42 PP |
192 | BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor"); |
193 | return (bt_bool) bt_interrupter_array_any_is_set( | |
194 | query_exec->interrupters); | |
195 | } | |
196 | ||
197 | void bt_query_executor_interrupt(struct bt_query_executor *query_exec) | |
198 | { | |
199 | BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor"); | |
200 | bt_interrupter_set(query_exec->default_interrupter); | |
201 | BT_LIB_LOGI("Interrupted query executor: query-exec-addr=%p", | |
202 | query_exec); | |
c7eee084 | 203 | } |
c5b9b441 PP |
204 | |
205 | void bt_query_executor_get_ref(const struct bt_query_executor *query_executor) | |
206 | { | |
207 | bt_object_get_ref(query_executor); | |
208 | } | |
209 | ||
210 | void bt_query_executor_put_ref(const struct bt_query_executor *query_executor) | |
211 | { | |
212 | bt_object_put_ref(query_executor); | |
213 | } |