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-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"
20 #include "component-class.h"
21 #include "query-executor.h"
22 #include "interrupter.h"
23 #include "lib/func-status.h"
26 void bt_query_executor_destroy(struct bt_object
*obj
)
28 struct bt_query_executor
*query_exec
=
29 container_of(obj
, struct bt_query_executor
, base
);
31 BT_LOGD("Destroying query executor: addr=%p", query_exec
);
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
;
39 BT_LOGD_STR("Putting component class.");
40 BT_OBJECT_PUT_REF_AND_RESET(query_exec
->comp_cls
);
42 if (query_exec
->object
) {
43 g_string_free(query_exec
->object
, TRUE
);
44 query_exec
->object
= NULL
;
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
);
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_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);
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
;
121 struct bt_query_executor
*bt_query_executor_create(
122 const bt_component_class
*comp_cls
, const char *object
,
123 const bt_value
*params
)
125 BT_ASSERT_PRE_NO_ERROR();
126 return bt_query_executor_create_with_method_data(comp_cls
,
127 object
, params
, NULL
);
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
)
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 */);
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
;
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
);
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
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
;
175 switch (query_exec
->comp_cls
->type
) {
176 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
178 struct bt_component_class_source
*src_cc
= (void *)
179 query_exec
->comp_cls
;
181 method
= (method_t
) src_cc
->methods
.query
;
182 method_name
= "bt_component_class_source_query_method";
185 case BT_COMPONENT_CLASS_TYPE_FILTER
:
187 struct bt_component_class_filter
*flt_cc
= (void *)
188 query_exec
->comp_cls
;
190 method
= (method_t
) flt_cc
->methods
.query
;
191 method_name
= "bt_component_class_filter_query_method";
194 case BT_COMPONENT_CLASS_TYPE_SINK
:
196 struct bt_component_class_sink
*sink_cc
= (void *)
197 query_exec
->comp_cls
;
199 method
= (method_t
) sink_cc
->methods
.query
;
200 method_name
= "bt_component_class_sink_query_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
;
215 BT_LIB_LOGD("Calling user's query method: "
216 "query-exec-addr=%p, %![cc-]+C, object=\"%s\", %![params-]+v, "
218 query_exec
, query_exec
->comp_cls
, query_exec
->object
->str
,
220 bt_common_logging_level_string(query_exec
->log_level
));
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
;
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
,
240 bt_common_logging_level_string(query_exec
->log_level
));
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
)
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",
261 return BT_FUNC_STATUS_OK
;
265 bt_bool
bt_query_executor_is_interrupted(const struct bt_query_executor
*query_exec
)
267 BT_ASSERT_PRE_QUERY_EXEC_NON_NULL(query_exec
);
268 return (bt_bool
) bt_interrupter_array_any_is_set(
269 query_exec
->interrupters
);
273 struct bt_interrupter
*bt_query_executor_borrow_default_interrupter(
274 struct bt_query_executor
*query_exec
)
276 BT_ASSERT_PRE_QUERY_EXEC_NON_NULL(query_exec
);
277 return query_exec
->default_interrupter
;
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
)
285 BT_ASSERT_PRE_QUERY_EXEC_NON_NULL(query_exec
);
286 query_exec
->log_level
= log_level
;
287 return BT_FUNC_STATUS_OK
;
291 enum bt_logging_level
bt_query_executor_get_logging_level(
292 const struct bt_query_executor
*query_exec
)
294 BT_ASSERT_PRE_QUERY_EXEC_NON_NULL(query_exec
);
295 return query_exec
->log_level
;
299 void bt_query_executor_get_ref(const struct bt_query_executor
*query_executor
)
301 bt_object_get_ref(query_executor
);
305 void bt_query_executor_put_ref(const struct bt_query_executor
*query_executor
)
307 bt_object_put_ref(query_executor
);