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