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
);
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
)
57 struct bt_query_executor
*query_exec
;
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);
67 BT_LIB_LOGE_APPEND_CAUSE(
68 "Failed to allocate one query executor.");
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
);
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
);
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
);
95 query_exec
->comp_cls
= comp_cls
;
96 bt_object_get_ref_no_null_check(query_exec
->comp_cls
);
99 query_exec
->params
= bt_value_null
;
101 query_exec
->params
= params
;
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
);
116 return (void *) query_exec
;
119 struct bt_query_executor
*bt_query_executor_create(
120 const bt_component_class
*comp_cls
, const char *object
,
121 const bt_value
*params
)
123 BT_ASSERT_PRE_NO_ERROR();
124 return bt_query_executor_create_with_method_data(comp_cls
,
125 object
, params
, NULL
);
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
)
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 */);
140 enum bt_query_executor_query_status status
;
141 enum bt_component_class_query_method_status query_status
;
142 method_t method
= NULL
;
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)");
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
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
;
171 switch (query_exec
->comp_cls
->type
) {
172 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
174 struct bt_component_class_source
*src_cc
= (void *)
175 query_exec
->comp_cls
;
177 method
= (method_t
) src_cc
->methods
.query
;
180 case BT_COMPONENT_CLASS_TYPE_FILTER
:
182 struct bt_component_class_filter
*flt_cc
= (void *)
183 query_exec
->comp_cls
;
185 method
= (method_t
) flt_cc
->methods
.query
;
188 case BT_COMPONENT_CLASS_TYPE_SINK
:
190 struct bt_component_class_sink
*sink_cc
= (void *)
191 query_exec
->comp_cls
;
193 method
= (method_t
) sink_cc
->methods
.query
;
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
;
208 BT_LIB_LOGD("Calling user's query method: "
209 "query-exec-addr=%p, %![cc-]+C, object=\"%s\", %![params-]+v, "
211 query_exec
, query_exec
->comp_cls
, query_exec
->object
->str
,
213 bt_common_logging_level_string(query_exec
->log_level
));
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
;
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
,
232 bt_common_logging_level_string(query_exec
->log_level
));
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
)
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",
252 return BT_FUNC_STATUS_OK
;
255 bt_bool
bt_query_executor_is_interrupted(const struct bt_query_executor
*query_exec
)
257 BT_ASSERT_PRE_NON_NULL(query_exec
, "Query executor");
258 return (bt_bool
) bt_interrupter_array_any_is_set(
259 query_exec
->interrupters
);
262 struct bt_interrupter
*bt_query_executor_borrow_default_interrupter(
263 struct bt_query_executor
*query_exec
)
265 BT_ASSERT_PRE_NON_NULL(query_exec
, "Query executor");
266 return query_exec
->default_interrupter
;
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
)
273 BT_ASSERT_PRE_NON_NULL(query_exec
, "Query executor");
274 query_exec
->log_level
= log_level
;
275 return BT_FUNC_STATUS_OK
;
278 enum bt_logging_level
bt_query_executor_get_logging_level(
279 const struct bt_query_executor
*query_exec
)
281 BT_ASSERT_PRE_NON_NULL(query_exec
, "Query executor");
282 return query_exec
->log_level
;
285 void bt_query_executor_get_ref(const struct bt_query_executor
*query_executor
)
287 bt_object_get_ref(query_executor
);
290 void bt_query_executor_put_ref(const struct bt_query_executor
*query_executor
)
292 bt_object_put_ref(query_executor
);