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