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