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 | ||
3c729b9a PP |
57 | BT_LOGD_STR("Putting component class."); |
58 | BT_OBJECT_PUT_REF_AND_RESET(query_exec->comp_cls); | |
59 | ||
60 | if (query_exec->object) { | |
61 | g_string_free(query_exec->object, TRUE); | |
62 | query_exec->object = NULL; | |
63 | } | |
9b4f9b42 | 64 | |
3c729b9a PP |
65 | BT_LOGD_STR("Putting parameters."); |
66 | BT_OBJECT_PUT_REF_AND_RESET(query_exec->params); | |
67 | BT_OBJECT_PUT_REF_AND_RESET(query_exec->default_interrupter); | |
c7eee084 PP |
68 | g_free(query_exec); |
69 | } | |
70 | ||
7c14d641 | 71 | struct bt_query_executor *bt_query_executor_create_with_method_data( |
3c729b9a | 72 | const bt_component_class *comp_cls, const char *object, |
7c14d641 | 73 | const bt_value *params, void *method_data) |
c7eee084 PP |
74 | { |
75 | struct bt_query_executor *query_exec; | |
76 | ||
17f3083a | 77 | BT_ASSERT_PRE_NO_ERROR(); |
3c729b9a PP |
78 | BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class"); |
79 | BT_ASSERT_PRE_NON_NULL(object, "Object"); | |
80 | BT_LIB_LOGD("Creating query executor: " | |
fb739d29 | 81 | "%![comp-cls-]+C, object=\"%s\", %![params-]+v", |
3c729b9a | 82 | comp_cls, object, params); |
c7eee084 PP |
83 | query_exec = g_new0(struct bt_query_executor, 1); |
84 | if (!query_exec) { | |
870631a2 PP |
85 | BT_LIB_LOGE_APPEND_CAUSE( |
86 | "Failed to allocate one query executor."); | |
c7eee084 PP |
87 | goto end; |
88 | } | |
89 | ||
9b4f9b42 | 90 | query_exec->interrupters = g_ptr_array_new_with_free_func( |
6871026b | 91 | (GDestroyNotify) bt_object_put_ref_no_null_check); |
9b4f9b42 PP |
92 | if (!query_exec->interrupters) { |
93 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); | |
94 | BT_OBJECT_PUT_REF_AND_RESET(query_exec); | |
95 | goto end; | |
96 | } | |
97 | ||
98 | query_exec->default_interrupter = bt_interrupter_create(); | |
99 | if (!query_exec->default_interrupter) { | |
100 | BT_LIB_LOGE_APPEND_CAUSE( | |
101 | "Failed to create one interrupter object."); | |
102 | BT_OBJECT_PUT_REF_AND_RESET(query_exec); | |
103 | goto end; | |
104 | } | |
105 | ||
3c729b9a PP |
106 | query_exec->object = g_string_new(object); |
107 | if (!query_exec->object) { | |
108 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString."); | |
109 | BT_OBJECT_PUT_REF_AND_RESET(query_exec); | |
110 | goto end; | |
111 | } | |
112 | ||
113 | query_exec->comp_cls = comp_cls; | |
6871026b | 114 | bt_object_get_ref_no_null_check(query_exec->comp_cls); |
3c729b9a PP |
115 | |
116 | if (!params) { | |
117 | query_exec->params = bt_value_null; | |
118 | } else { | |
119 | query_exec->params = params; | |
120 | } | |
121 | ||
6871026b | 122 | bt_object_get_ref_no_null_check(query_exec->params); |
7c14d641 | 123 | query_exec->method_data = method_data; |
3c729b9a | 124 | query_exec->log_level = BT_LOGGING_LEVEL_NONE; |
9b4f9b42 PP |
125 | bt_query_executor_add_interrupter(query_exec, |
126 | query_exec->default_interrupter); | |
3fea54f6 PP |
127 | bt_object_init_shared(&query_exec->base, |
128 | bt_query_executor_destroy); | |
3c729b9a | 129 | BT_LIB_LOGD("Created query executor: " |
fb739d29 | 130 | "addr=%p, %![comp-cls-]+C, object=\"%s\", %![params-]+v", |
3c729b9a | 131 | query_exec, comp_cls, object, params); |
c7eee084 PP |
132 | |
133 | end: | |
d94d92ac | 134 | return (void *) query_exec; |
c7eee084 PP |
135 | } |
136 | ||
7c14d641 PP |
137 | struct bt_query_executor *bt_query_executor_create( |
138 | const bt_component_class *comp_cls, const char *object, | |
139 | const bt_value *params) | |
140 | { | |
17f3083a | 141 | BT_ASSERT_PRE_NO_ERROR(); |
7c14d641 PP |
142 | return bt_query_executor_create_with_method_data(comp_cls, |
143 | object, params, NULL); | |
144 | } | |
145 | ||
d24d5663 | 146 | enum bt_query_executor_query_status bt_query_executor_query( |
0d72b8c3 | 147 | struct bt_query_executor *query_exec, |
05e21286 | 148 | const struct bt_value **user_result) |
c7eee084 | 149 | { |
3c729b9a PP |
150 | typedef enum bt_component_class_query_method_status (*method_t)( |
151 | void * /* self component class */, | |
152 | void * /* private query executor */, | |
153 | const char * /* object */, | |
154 | const struct bt_value * /* parameters */, | |
7c14d641 | 155 | void * /* method data */, |
3c729b9a | 156 | const struct bt_value ** /* result */); |
c7eee084 | 157 | |
d24d5663 PP |
158 | enum bt_query_executor_query_status status; |
159 | enum bt_component_class_query_method_status query_status; | |
d94d92ac | 160 | method_t method = NULL; |
c7eee084 | 161 | |
17f3083a | 162 | BT_ASSERT_PRE_NO_ERROR(); |
d94d92ac | 163 | BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor"); |
d94d92ac | 164 | BT_ASSERT_PRE_NON_NULL(user_result, "Result (output)"); |
d94d92ac | 165 | |
647c6d45 PP |
166 | /* |
167 | * Initial check: is the query executor already interrupted? If | |
168 | * so, return `BT_FUNC_STATUS_AGAIN`. Returning this status is | |
169 | * harmless: it's not `BT_FUNC_STATUS_OK` (there's no result), | |
170 | * and it's not `BT_FUNC_STATUS_ERROR` either (there's no | |
171 | * legitimate error). Since any query operation could return | |
172 | * `BT_FUNC_STATUS_AGAIN` when interrupted or instead of | |
173 | * blocking, the caller is responsible for checking the | |
174 | * interruption state of the query executor when getting this | |
175 | * status. | |
176 | */ | |
177 | if (bt_query_executor_is_interrupted(query_exec)) { | |
178 | BT_LIB_LOGD("Query executor is interrupted: " | |
179 | "not performing the query operation: " | |
180 | "query-exec-addr=%p, %![cc-]+C, object=\"%s\", " | |
181 | "%![params-]+v, log-level=%s", | |
3c729b9a PP |
182 | query_exec, query_exec->comp_cls, |
183 | query_exec->object->str, query_exec->params, | |
184 | bt_common_logging_level_string(query_exec->log_level)); | |
647c6d45 PP |
185 | status = BT_FUNC_STATUS_AGAIN; |
186 | goto end; | |
187 | } | |
188 | ||
3c729b9a | 189 | switch (query_exec->comp_cls->type) { |
d94d92ac PP |
190 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
191 | { | |
3c729b9a PP |
192 | struct bt_component_class_source *src_cc = (void *) |
193 | query_exec->comp_cls; | |
d94d92ac PP |
194 | |
195 | method = (method_t) src_cc->methods.query; | |
196 | break; | |
c7eee084 | 197 | } |
d94d92ac PP |
198 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
199 | { | |
3c729b9a PP |
200 | struct bt_component_class_filter *flt_cc = (void *) |
201 | query_exec->comp_cls; | |
c7eee084 | 202 | |
d94d92ac PP |
203 | method = (method_t) flt_cc->methods.query; |
204 | break; | |
c7eee084 | 205 | } |
d94d92ac PP |
206 | case BT_COMPONENT_CLASS_TYPE_SINK: |
207 | { | |
3c729b9a PP |
208 | struct bt_component_class_sink *sink_cc = (void *) |
209 | query_exec->comp_cls; | |
c7eee084 | 210 | |
d94d92ac PP |
211 | method = (method_t) sink_cc->methods.query; |
212 | break; | |
213 | } | |
214 | default: | |
498e7994 | 215 | bt_common_abort(); |
c7eee084 PP |
216 | } |
217 | ||
d94d92ac | 218 | if (!method) { |
c7eee084 | 219 | /* Not an error: nothing to query */ |
d94d92ac | 220 | BT_LIB_LOGD("Component class has no registered query method: " |
3c729b9a | 221 | "%!+C", query_exec->comp_cls); |
76b6c2f7 | 222 | status = BT_FUNC_STATUS_UNKNOWN_OBJECT; |
c7eee084 PP |
223 | goto end; |
224 | } | |
225 | ||
d94d92ac | 226 | BT_LIB_LOGD("Calling user's query method: " |
f4e38e70 PP |
227 | "query-exec-addr=%p, %![cc-]+C, object=\"%s\", %![params-]+v, " |
228 | "log-level=%s", | |
3c729b9a PP |
229 | query_exec, query_exec->comp_cls, query_exec->object->str, |
230 | query_exec->params, | |
231 | bt_common_logging_level_string(query_exec->log_level)); | |
d94d92ac | 232 | *user_result = NULL; |
3c729b9a PP |
233 | query_status = method((void *) query_exec->comp_cls, |
234 | (void *) query_exec, query_exec->object->str, | |
7c14d641 | 235 | query_exec->params, query_exec->method_data, user_result); |
d94d92ac | 236 | BT_LIB_LOGD("User method returned: status=%s, %![res-]+v", |
d24d5663 PP |
237 | bt_common_func_status_string(query_status), *user_result); |
238 | BT_ASSERT_POST(query_status != BT_FUNC_STATUS_OK || *user_result, | |
239 | "User method returned `BT_FUNC_STATUS_OK` without a result."); | |
6ecdcca3 | 240 | BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(query_status); |
d24d5663 | 241 | status = (int) query_status; |
c7eee084 | 242 | |
a635e507 SM |
243 | if (status < 0) { |
244 | BT_LIB_LOGW_APPEND_CAUSE( | |
245 | "Component class's \"query\" method failed: " | |
246 | "query-exec-addr=%p, %![cc-]+C, object=\"%s\", " | |
3c729b9a PP |
247 | "%![params-]+v, log-level=%s", query_exec, |
248 | query_exec->comp_cls, query_exec->object->str, | |
249 | query_exec->params, | |
250 | bt_common_logging_level_string(query_exec->log_level)); | |
a635e507 SM |
251 | goto end; |
252 | } | |
253 | ||
c7eee084 | 254 | end: |
d24d5663 | 255 | return status; |
c7eee084 PP |
256 | } |
257 | ||
9b4f9b42 PP |
258 | enum bt_query_executor_add_interrupter_status bt_query_executor_add_interrupter( |
259 | struct bt_query_executor *query_exec, | |
260 | const struct bt_interrupter *intr) | |
c7eee084 | 261 | { |
17f3083a | 262 | BT_ASSERT_PRE_NO_ERROR(); |
d94d92ac | 263 | BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor"); |
9b4f9b42 PP |
264 | BT_ASSERT_PRE_NON_NULL(intr, "Interrupter"); |
265 | g_ptr_array_add(query_exec->interrupters, (void *) intr); | |
6871026b | 266 | bt_object_get_ref_no_null_check(intr); |
9b4f9b42 PP |
267 | BT_LIB_LOGD("Added interrupter to query executor: " |
268 | "query-exec-addr=%p, %![intr-]+z", | |
269 | query_exec, intr); | |
d24d5663 | 270 | return BT_FUNC_STATUS_OK; |
c7eee084 PP |
271 | } |
272 | ||
9b4f9b42 | 273 | bt_bool bt_query_executor_is_interrupted(const struct bt_query_executor *query_exec) |
c7eee084 | 274 | { |
9b4f9b42 PP |
275 | BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor"); |
276 | return (bt_bool) bt_interrupter_array_any_is_set( | |
277 | query_exec->interrupters); | |
278 | } | |
279 | ||
88d1a0b7 SM |
280 | struct bt_interrupter *bt_query_executor_borrow_default_interrupter( |
281 | struct bt_query_executor *query_exec) | |
9b4f9b42 PP |
282 | { |
283 | BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor"); | |
88d1a0b7 | 284 | return query_exec->default_interrupter; |
c7eee084 | 285 | } |
c5b9b441 | 286 | |
3c729b9a PP |
287 | enum bt_query_executor_set_logging_level_status |
288 | bt_query_executor_set_logging_level(struct bt_query_executor *query_exec, | |
289 | enum bt_logging_level log_level) | |
290 | { | |
291 | BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor"); | |
292 | query_exec->log_level = log_level; | |
293 | return BT_FUNC_STATUS_OK; | |
294 | } | |
295 | ||
296 | enum bt_logging_level bt_query_executor_get_logging_level( | |
297 | const struct bt_query_executor *query_exec) | |
298 | { | |
299 | BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor"); | |
300 | return query_exec->log_level; | |
301 | } | |
302 | ||
c5b9b441 PP |
303 | void bt_query_executor_get_ref(const struct bt_query_executor *query_executor) |
304 | { | |
305 | bt_object_get_ref(query_executor); | |
306 | } | |
307 | ||
308 | void bt_query_executor_put_ref(const struct bt_query_executor *query_executor) | |
309 | { | |
310 | bt_object_put_ref(query_executor); | |
311 | } |