Commit | Line | Data |
---|---|---|
c7eee084 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
c7eee084 | 3 | * |
0235b0db | 4 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
c7eee084 PP |
5 | */ |
6 | ||
350ad6c1 | 7 | #define BT_LOG_TAG "LIB/QUERY-EXECUTOR" |
c2d9d9cf | 8 | #include "lib/logging.h" |
c7eee084 | 9 | |
f4e38e70 | 10 | #include "common/common.h" |
d98421f2 | 11 | #include "lib/assert-cond.h" |
3fadfbc0 | 12 | #include <babeltrace2/graph/query-executor.h> |
3fadfbc0 | 13 | #include <babeltrace2/graph/component-class.h> |
43c59509 | 14 | #include <babeltrace2/graph/query-executor.h> |
3fadfbc0 | 15 | #include <babeltrace2/value.h> |
578e048b MJ |
16 | #include "lib/object.h" |
17 | #include "compat/compiler.h" | |
18 | ||
19 | #include "component-class.h" | |
20 | #include "query-executor.h" | |
9b4f9b42 | 21 | #include "interrupter.h" |
d24d5663 | 22 | #include "lib/func-status.h" |
c7eee084 PP |
23 | |
24 | static | |
25 | void bt_query_executor_destroy(struct bt_object *obj) | |
26 | { | |
27 | struct bt_query_executor *query_exec = | |
28 | container_of(obj, struct bt_query_executor, base); | |
29 | ||
d94d92ac | 30 | BT_LOGD("Destroying query executor: addr=%p", query_exec); |
9b4f9b42 PP |
31 | |
32 | if (query_exec->interrupters) { | |
33 | BT_LOGD_STR("Putting interrupters."); | |
34 | g_ptr_array_free(query_exec->interrupters, TRUE); | |
35 | query_exec->interrupters = NULL; | |
36 | } | |
37 | ||
3c729b9a PP |
38 | BT_LOGD_STR("Putting component class."); |
39 | BT_OBJECT_PUT_REF_AND_RESET(query_exec->comp_cls); | |
40 | ||
41 | if (query_exec->object) { | |
42 | g_string_free(query_exec->object, TRUE); | |
43 | query_exec->object = NULL; | |
44 | } | |
9b4f9b42 | 45 | |
3c729b9a PP |
46 | BT_LOGD_STR("Putting parameters."); |
47 | BT_OBJECT_PUT_REF_AND_RESET(query_exec->params); | |
48 | BT_OBJECT_PUT_REF_AND_RESET(query_exec->default_interrupter); | |
c7eee084 PP |
49 | g_free(query_exec); |
50 | } | |
51 | ||
1353b066 | 52 | BT_EXPORT |
7c14d641 | 53 | struct bt_query_executor *bt_query_executor_create_with_method_data( |
3c729b9a | 54 | const bt_component_class *comp_cls, const char *object, |
7c14d641 | 55 | const bt_value *params, void *method_data) |
c7eee084 PP |
56 | { |
57 | struct bt_query_executor *query_exec; | |
58 | ||
17f3083a | 59 | BT_ASSERT_PRE_NO_ERROR(); |
d5b13b9b | 60 | BT_ASSERT_PRE_COMP_CLS_NON_NULL(comp_cls); |
1778c2a4 | 61 | BT_ASSERT_PRE_NON_NULL("object", object, "Object"); |
3c729b9a | 62 | BT_LIB_LOGD("Creating query executor: " |
fb739d29 | 63 | "%![comp-cls-]+C, object=\"%s\", %![params-]+v", |
3c729b9a | 64 | comp_cls, object, params); |
c7eee084 PP |
65 | query_exec = g_new0(struct bt_query_executor, 1); |
66 | if (!query_exec) { | |
870631a2 PP |
67 | BT_LIB_LOGE_APPEND_CAUSE( |
68 | "Failed to allocate one query executor."); | |
c7eee084 PP |
69 | goto end; |
70 | } | |
71 | ||
9b4f9b42 | 72 | query_exec->interrupters = g_ptr_array_new_with_free_func( |
6871026b | 73 | (GDestroyNotify) bt_object_put_ref_no_null_check); |
9b4f9b42 PP |
74 | if (!query_exec->interrupters) { |
75 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); | |
76 | BT_OBJECT_PUT_REF_AND_RESET(query_exec); | |
77 | goto end; | |
78 | } | |
79 | ||
80 | query_exec->default_interrupter = bt_interrupter_create(); | |
81 | if (!query_exec->default_interrupter) { | |
82 | BT_LIB_LOGE_APPEND_CAUSE( | |
83 | "Failed to create one interrupter object."); | |
84 | BT_OBJECT_PUT_REF_AND_RESET(query_exec); | |
85 | goto end; | |
86 | } | |
87 | ||
3c729b9a PP |
88 | query_exec->object = g_string_new(object); |
89 | if (!query_exec->object) { | |
90 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString."); | |
91 | BT_OBJECT_PUT_REF_AND_RESET(query_exec); | |
92 | goto end; | |
93 | } | |
94 | ||
95 | query_exec->comp_cls = comp_cls; | |
6871026b | 96 | bt_object_get_ref_no_null_check(query_exec->comp_cls); |
3c729b9a PP |
97 | |
98 | if (!params) { | |
99 | query_exec->params = bt_value_null; | |
100 | } else { | |
101 | query_exec->params = params; | |
102 | } | |
103 | ||
6871026b | 104 | bt_object_get_ref_no_null_check(query_exec->params); |
7c14d641 | 105 | query_exec->method_data = method_data; |
3c729b9a | 106 | query_exec->log_level = BT_LOGGING_LEVEL_NONE; |
9b4f9b42 PP |
107 | bt_query_executor_add_interrupter(query_exec, |
108 | query_exec->default_interrupter); | |
3fea54f6 PP |
109 | bt_object_init_shared(&query_exec->base, |
110 | bt_query_executor_destroy); | |
3c729b9a | 111 | BT_LIB_LOGD("Created query executor: " |
fb739d29 | 112 | "addr=%p, %![comp-cls-]+C, object=\"%s\", %![params-]+v", |
3c729b9a | 113 | query_exec, comp_cls, object, params); |
c7eee084 PP |
114 | |
115 | end: | |
d94d92ac | 116 | return (void *) query_exec; |
c7eee084 PP |
117 | } |
118 | ||
1353b066 | 119 | BT_EXPORT |
7c14d641 PP |
120 | struct bt_query_executor *bt_query_executor_create( |
121 | const bt_component_class *comp_cls, const char *object, | |
122 | const bt_value *params) | |
123 | { | |
17f3083a | 124 | BT_ASSERT_PRE_NO_ERROR(); |
7c14d641 PP |
125 | return bt_query_executor_create_with_method_data(comp_cls, |
126 | object, params, NULL); | |
127 | } | |
128 | ||
1353b066 | 129 | BT_EXPORT |
d24d5663 | 130 | enum bt_query_executor_query_status bt_query_executor_query( |
0d72b8c3 | 131 | struct bt_query_executor *query_exec, |
05e21286 | 132 | const struct bt_value **user_result) |
c7eee084 | 133 | { |
3c729b9a PP |
134 | typedef enum bt_component_class_query_method_status (*method_t)( |
135 | void * /* self component class */, | |
136 | void * /* private query executor */, | |
137 | const char * /* object */, | |
138 | const struct bt_value * /* parameters */, | |
7c14d641 | 139 | void * /* method data */, |
3c729b9a | 140 | const struct bt_value ** /* result */); |
c7eee084 | 141 | |
d24d5663 PP |
142 | enum bt_query_executor_query_status status; |
143 | enum bt_component_class_query_method_status query_status; | |
d94d92ac | 144 | method_t method = NULL; |
1778c2a4 | 145 | const char *method_name = NULL; |
c7eee084 | 146 | |
17f3083a | 147 | BT_ASSERT_PRE_NO_ERROR(); |
d5b13b9b PP |
148 | BT_ASSERT_PRE_QUERY_EXEC_NON_NULL(query_exec); |
149 | BT_ASSERT_PRE_RES_OUT_NON_NULL(user_result); | |
d94d92ac | 150 | |
647c6d45 PP |
151 | /* |
152 | * Initial check: is the query executor already interrupted? If | |
153 | * so, return `BT_FUNC_STATUS_AGAIN`. Returning this status is | |
154 | * harmless: it's not `BT_FUNC_STATUS_OK` (there's no result), | |
155 | * and it's not `BT_FUNC_STATUS_ERROR` either (there's no | |
156 | * legitimate error). Since any query operation could return | |
157 | * `BT_FUNC_STATUS_AGAIN` when interrupted or instead of | |
158 | * blocking, the caller is responsible for checking the | |
159 | * interruption state of the query executor when getting this | |
160 | * status. | |
161 | */ | |
162 | if (bt_query_executor_is_interrupted(query_exec)) { | |
163 | BT_LIB_LOGD("Query executor is interrupted: " | |
164 | "not performing the query operation: " | |
165 | "query-exec-addr=%p, %![cc-]+C, object=\"%s\", " | |
166 | "%![params-]+v, log-level=%s", | |
3c729b9a PP |
167 | query_exec, query_exec->comp_cls, |
168 | query_exec->object->str, query_exec->params, | |
169 | bt_common_logging_level_string(query_exec->log_level)); | |
647c6d45 PP |
170 | status = BT_FUNC_STATUS_AGAIN; |
171 | goto end; | |
172 | } | |
173 | ||
3c729b9a | 174 | switch (query_exec->comp_cls->type) { |
d94d92ac PP |
175 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
176 | { | |
3c729b9a PP |
177 | struct bt_component_class_source *src_cc = (void *) |
178 | query_exec->comp_cls; | |
d94d92ac PP |
179 | |
180 | method = (method_t) src_cc->methods.query; | |
1778c2a4 | 181 | method_name = "bt_component_class_source_query_method"; |
d94d92ac | 182 | break; |
c7eee084 | 183 | } |
d94d92ac PP |
184 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
185 | { | |
3c729b9a PP |
186 | struct bt_component_class_filter *flt_cc = (void *) |
187 | query_exec->comp_cls; | |
c7eee084 | 188 | |
d94d92ac | 189 | method = (method_t) flt_cc->methods.query; |
1778c2a4 | 190 | method_name = "bt_component_class_filter_query_method"; |
d94d92ac | 191 | break; |
c7eee084 | 192 | } |
d94d92ac PP |
193 | case BT_COMPONENT_CLASS_TYPE_SINK: |
194 | { | |
3c729b9a PP |
195 | struct bt_component_class_sink *sink_cc = (void *) |
196 | query_exec->comp_cls; | |
c7eee084 | 197 | |
d94d92ac | 198 | method = (method_t) sink_cc->methods.query; |
1778c2a4 | 199 | method_name = "bt_component_class_sink_query_method"; |
d94d92ac PP |
200 | break; |
201 | } | |
202 | default: | |
498e7994 | 203 | bt_common_abort(); |
c7eee084 PP |
204 | } |
205 | ||
d94d92ac | 206 | if (!method) { |
c7eee084 | 207 | /* Not an error: nothing to query */ |
d94d92ac | 208 | BT_LIB_LOGD("Component class has no registered query method: " |
3c729b9a | 209 | "%!+C", query_exec->comp_cls); |
76b6c2f7 | 210 | status = BT_FUNC_STATUS_UNKNOWN_OBJECT; |
c7eee084 PP |
211 | goto end; |
212 | } | |
213 | ||
d94d92ac | 214 | BT_LIB_LOGD("Calling user's query method: " |
f4e38e70 PP |
215 | "query-exec-addr=%p, %![cc-]+C, object=\"%s\", %![params-]+v, " |
216 | "log-level=%s", | |
3c729b9a PP |
217 | query_exec, query_exec->comp_cls, query_exec->object->str, |
218 | query_exec->params, | |
219 | bt_common_logging_level_string(query_exec->log_level)); | |
d94d92ac | 220 | *user_result = NULL; |
3c729b9a PP |
221 | query_status = method((void *) query_exec->comp_cls, |
222 | (void *) query_exec, query_exec->object->str, | |
7c14d641 | 223 | query_exec->params, query_exec->method_data, user_result); |
d94d92ac | 224 | BT_LIB_LOGD("User method returned: status=%s, %![res-]+v", |
d24d5663 | 225 | bt_common_func_status_string(query_status), *user_result); |
1778c2a4 PP |
226 | BT_ASSERT_POST(method_name, "status-ok-with-result", |
227 | query_status != BT_FUNC_STATUS_OK || *user_result, | |
d24d5663 | 228 | "User method returned `BT_FUNC_STATUS_OK` without a result."); |
1778c2a4 | 229 | BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(method_name, query_status); |
d24d5663 | 230 | status = (int) query_status; |
c7eee084 | 231 | |
a635e507 SM |
232 | if (status < 0) { |
233 | BT_LIB_LOGW_APPEND_CAUSE( | |
234 | "Component class's \"query\" method failed: " | |
235 | "query-exec-addr=%p, %![cc-]+C, object=\"%s\", " | |
3c729b9a PP |
236 | "%![params-]+v, log-level=%s", query_exec, |
237 | query_exec->comp_cls, query_exec->object->str, | |
238 | query_exec->params, | |
239 | bt_common_logging_level_string(query_exec->log_level)); | |
a635e507 SM |
240 | goto end; |
241 | } | |
242 | ||
c7eee084 | 243 | end: |
d24d5663 | 244 | return status; |
c7eee084 PP |
245 | } |
246 | ||
1353b066 | 247 | BT_EXPORT |
9b4f9b42 PP |
248 | enum bt_query_executor_add_interrupter_status bt_query_executor_add_interrupter( |
249 | struct bt_query_executor *query_exec, | |
250 | const struct bt_interrupter *intr) | |
c7eee084 | 251 | { |
17f3083a | 252 | BT_ASSERT_PRE_NO_ERROR(); |
d5b13b9b PP |
253 | BT_ASSERT_PRE_QUERY_EXEC_NON_NULL(query_exec); |
254 | BT_ASSERT_PRE_INTR_NON_NULL(intr); | |
9b4f9b42 | 255 | g_ptr_array_add(query_exec->interrupters, (void *) intr); |
6871026b | 256 | bt_object_get_ref_no_null_check(intr); |
9b4f9b42 PP |
257 | BT_LIB_LOGD("Added interrupter to query executor: " |
258 | "query-exec-addr=%p, %![intr-]+z", | |
259 | query_exec, intr); | |
d24d5663 | 260 | return BT_FUNC_STATUS_OK; |
c7eee084 PP |
261 | } |
262 | ||
1353b066 | 263 | BT_EXPORT |
9b4f9b42 | 264 | bt_bool bt_query_executor_is_interrupted(const struct bt_query_executor *query_exec) |
c7eee084 | 265 | { |
d5b13b9b | 266 | BT_ASSERT_PRE_QUERY_EXEC_NON_NULL(query_exec); |
9b4f9b42 PP |
267 | return (bt_bool) bt_interrupter_array_any_is_set( |
268 | query_exec->interrupters); | |
269 | } | |
270 | ||
1353b066 | 271 | BT_EXPORT |
88d1a0b7 SM |
272 | struct bt_interrupter *bt_query_executor_borrow_default_interrupter( |
273 | struct bt_query_executor *query_exec) | |
9b4f9b42 | 274 | { |
d5b13b9b | 275 | BT_ASSERT_PRE_QUERY_EXEC_NON_NULL(query_exec); |
88d1a0b7 | 276 | return query_exec->default_interrupter; |
c7eee084 | 277 | } |
c5b9b441 | 278 | |
1353b066 | 279 | BT_EXPORT |
3c729b9a PP |
280 | enum bt_query_executor_set_logging_level_status |
281 | bt_query_executor_set_logging_level(struct bt_query_executor *query_exec, | |
282 | enum bt_logging_level log_level) | |
283 | { | |
d5b13b9b | 284 | BT_ASSERT_PRE_QUERY_EXEC_NON_NULL(query_exec); |
3c729b9a PP |
285 | query_exec->log_level = log_level; |
286 | return BT_FUNC_STATUS_OK; | |
287 | } | |
288 | ||
1353b066 | 289 | BT_EXPORT |
3c729b9a PP |
290 | enum bt_logging_level bt_query_executor_get_logging_level( |
291 | const struct bt_query_executor *query_exec) | |
292 | { | |
d5b13b9b | 293 | BT_ASSERT_PRE_QUERY_EXEC_NON_NULL(query_exec); |
3c729b9a PP |
294 | return query_exec->log_level; |
295 | } | |
296 | ||
1353b066 | 297 | BT_EXPORT |
c5b9b441 PP |
298 | void bt_query_executor_get_ref(const struct bt_query_executor *query_executor) |
299 | { | |
300 | bt_object_get_ref(query_executor); | |
301 | } | |
302 | ||
1353b066 | 303 | BT_EXPORT |
c5b9b441 PP |
304 | void bt_query_executor_put_ref(const struct bt_query_executor *query_executor) |
305 | { | |
306 | bt_object_put_ref(query_executor); | |
307 | } |