2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
7 #define BT_LOG_TAG "LIB/QUERY-EXECUTOR"
8 #include "lib/logging.h"
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"
21 #include "component-class.h"
22 #include "query-executor.h"
23 #include "interrupter.h"
24 #include "lib/func-status.h"
27 void bt_query_executor_destroy(struct bt_object
*obj
)
29 struct bt_query_executor
*query_exec
=
30 container_of(obj
, struct bt_query_executor
, base
);
32 BT_LOGD("Destroying query executor: addr=%p", query_exec
);
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
;
40 BT_LOGD_STR("Putting component class.");
41 BT_OBJECT_PUT_REF_AND_RESET(query_exec
->comp_cls
);
43 if (query_exec
->object
) {
44 g_string_free(query_exec
->object
, TRUE
);
45 query_exec
->object
= NULL
;
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
);
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
)
58 struct bt_query_executor
*query_exec
;
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);
68 BT_LIB_LOGE_APPEND_CAUSE(
69 "Failed to allocate one query executor.");
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
);
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
);
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
);
96 query_exec
->comp_cls
= comp_cls
;
97 bt_object_get_ref_no_null_check(query_exec
->comp_cls
);
100 query_exec
->params
= bt_value_null
;
102 query_exec
->params
= params
;
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
);
117 return (void *) query_exec
;
120 struct bt_query_executor
*bt_query_executor_create(
121 const bt_component_class
*comp_cls
, const char *object
,
122 const bt_value
*params
)
124 BT_ASSERT_PRE_NO_ERROR();
125 return bt_query_executor_create_with_method_data(comp_cls
,
126 object
, params
, NULL
);
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
)
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 */);
141 enum bt_query_executor_query_status status
;
142 enum bt_component_class_query_method_status query_status
;
143 method_t method
= NULL
;
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)");
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
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
;
172 switch (query_exec
->comp_cls
->type
) {
173 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
175 struct bt_component_class_source
*src_cc
= (void *)
176 query_exec
->comp_cls
;
178 method
= (method_t
) src_cc
->methods
.query
;
181 case BT_COMPONENT_CLASS_TYPE_FILTER
:
183 struct bt_component_class_filter
*flt_cc
= (void *)
184 query_exec
->comp_cls
;
186 method
= (method_t
) flt_cc
->methods
.query
;
189 case BT_COMPONENT_CLASS_TYPE_SINK
:
191 struct bt_component_class_sink
*sink_cc
= (void *)
192 query_exec
->comp_cls
;
194 method
= (method_t
) sink_cc
->methods
.query
;
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
;
209 BT_LIB_LOGD("Calling user's query method: "
210 "query-exec-addr=%p, %![cc-]+C, object=\"%s\", %![params-]+v, "
212 query_exec
, query_exec
->comp_cls
, query_exec
->object
->str
,
214 bt_common_logging_level_string(query_exec
->log_level
));
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
;
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
,
233 bt_common_logging_level_string(query_exec
->log_level
));
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
)
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",
253 return BT_FUNC_STATUS_OK
;
256 bt_bool
bt_query_executor_is_interrupted(const struct bt_query_executor
*query_exec
)
258 BT_ASSERT_PRE_NON_NULL(query_exec
, "Query executor");
259 return (bt_bool
) bt_interrupter_array_any_is_set(
260 query_exec
->interrupters
);
263 struct bt_interrupter
*bt_query_executor_borrow_default_interrupter(
264 struct bt_query_executor
*query_exec
)
266 BT_ASSERT_PRE_NON_NULL(query_exec
, "Query executor");
267 return query_exec
->default_interrupter
;
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
)
274 BT_ASSERT_PRE_NON_NULL(query_exec
, "Query executor");
275 query_exec
->log_level
= log_level
;
276 return BT_FUNC_STATUS_OK
;
279 enum bt_logging_level
bt_query_executor_get_logging_level(
280 const struct bt_query_executor
*query_exec
)
282 BT_ASSERT_PRE_NON_NULL(query_exec
, "Query executor");
283 return query_exec
->log_level
;
286 void bt_query_executor_get_ref(const struct bt_query_executor
*query_executor
)
288 bt_object_get_ref(query_executor
);
291 void bt_query_executor_put_ref(const struct bt_query_executor
*query_executor
)
293 bt_object_put_ref(query_executor
);