78ba1f2caecf75d7824913926af2e6f811945cfd
[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-pre.h"
13 #include "lib/assert-post.h"
14 #include <babeltrace2/graph/query-executor.h>
15 #include <babeltrace2/graph/component-class.h>
16 #include <babeltrace2/graph/query-executor.h>
17 #include <babeltrace2/value.h>
18 #include "lib/object.h"
19 #include "compat/compiler.h"
20
21 #include "component-class.h"
22 #include "query-executor.h"
23 #include "interrupter.h"
24 #include "lib/func-status.h"
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
32 BT_LOGD("Destroying query executor: addr=%p", query_exec);
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
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 }
47
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);
51 g_free(query_exec);
52 }
53
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_NON_NULL(comp_cls, "Component class");
62 BT_ASSERT_PRE_NON_NULL(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 struct bt_query_executor *bt_query_executor_create(
121 const bt_component_class *comp_cls, const char *object,
122 const bt_value *params)
123 {
124 BT_ASSERT_PRE_NO_ERROR();
125 return bt_query_executor_create_with_method_data(comp_cls,
126 object, params, NULL);
127 }
128
129 enum bt_query_executor_query_status bt_query_executor_query(
130 struct bt_query_executor *query_exec,
131 const struct bt_value **user_result)
132 {
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 */,
138 void * /* method data */,
139 const struct bt_value ** /* result */);
140
141 enum bt_query_executor_query_status status;
142 enum bt_component_class_query_method_status query_status;
143 method_t method = NULL;
144
145 BT_ASSERT_PRE_NO_ERROR();
146 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
147 BT_ASSERT_PRE_NON_NULL(user_result, "Result (output)");
148
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",
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));
168 status = BT_FUNC_STATUS_AGAIN;
169 goto end;
170 }
171
172 switch (query_exec->comp_cls->type) {
173 case BT_COMPONENT_CLASS_TYPE_SOURCE:
174 {
175 struct bt_component_class_source *src_cc = (void *)
176 query_exec->comp_cls;
177
178 method = (method_t) src_cc->methods.query;
179 break;
180 }
181 case BT_COMPONENT_CLASS_TYPE_FILTER:
182 {
183 struct bt_component_class_filter *flt_cc = (void *)
184 query_exec->comp_cls;
185
186 method = (method_t) flt_cc->methods.query;
187 break;
188 }
189 case BT_COMPONENT_CLASS_TYPE_SINK:
190 {
191 struct bt_component_class_sink *sink_cc = (void *)
192 query_exec->comp_cls;
193
194 method = (method_t) sink_cc->methods.query;
195 break;
196 }
197 default:
198 bt_common_abort();
199 }
200
201 if (!method) {
202 /* Not an error: nothing to query */
203 BT_LIB_LOGD("Component class has no registered query method: "
204 "%!+C", query_exec->comp_cls);
205 status = BT_FUNC_STATUS_UNKNOWN_OBJECT;
206 goto end;
207 }
208
209 BT_LIB_LOGD("Calling user's query method: "
210 "query-exec-addr=%p, %![cc-]+C, object=\"%s\", %![params-]+v, "
211 "log-level=%s",
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));
215 *user_result = NULL;
216 query_status = method((void *) query_exec->comp_cls,
217 (void *) query_exec, query_exec->object->str,
218 query_exec->params, query_exec->method_data, user_result);
219 BT_LIB_LOGD("User method returned: status=%s, %![res-]+v",
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.");
223 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(query_status);
224 status = (int) query_status;
225
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\", "
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));
234 goto end;
235 }
236
237 end:
238 return status;
239 }
240
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)
244 {
245 BT_ASSERT_PRE_NO_ERROR();
246 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
247 BT_ASSERT_PRE_NON_NULL(intr, "Interrupter");
248 g_ptr_array_add(query_exec->interrupters, (void *) intr);
249 bt_object_get_ref_no_null_check(intr);
250 BT_LIB_LOGD("Added interrupter to query executor: "
251 "query-exec-addr=%p, %![intr-]+z",
252 query_exec, intr);
253 return BT_FUNC_STATUS_OK;
254 }
255
256 bt_bool bt_query_executor_is_interrupted(const struct bt_query_executor *query_exec)
257 {
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
263 struct bt_interrupter *bt_query_executor_borrow_default_interrupter(
264 struct bt_query_executor *query_exec)
265 {
266 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
267 return query_exec->default_interrupter;
268 }
269
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
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 }
This page took 0.041579 seconds and 4 git commands to generate.