2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #define BT_LOG_TAG "LIB/QUERY-EXECUTOR"
24 #include "lib/logging.h"
26 #include "common/assert.h"
27 #include "common/common.h"
28 #include "lib/assert-pre.h"
29 #include "lib/assert-post.h"
30 #include <babeltrace2/graph/query-executor-const.h>
31 #include <babeltrace2/graph/query-executor.h>
32 #include <babeltrace2/graph/component-class.h>
33 #include <babeltrace2/value.h>
34 #include <babeltrace2/value-const.h>
35 #include "lib/object.h"
36 #include "compat/compiler.h"
38 #include "component-class.h"
39 #include "query-executor.h"
40 #include "interrupter.h"
41 #include "lib/func-status.h"
44 void bt_query_executor_destroy(struct bt_object
*obj
)
46 struct bt_query_executor
*query_exec
=
47 container_of(obj
, struct bt_query_executor
, base
);
49 BT_LOGD("Destroying query executor: addr=%p", query_exec
);
51 if (query_exec
->interrupters
) {
52 BT_LOGD_STR("Putting interrupters.");
53 g_ptr_array_free(query_exec
->interrupters
, TRUE
);
54 query_exec
->interrupters
= NULL
;
57 BT_LOGD_STR("Putting component class.");
58 BT_OBJECT_PUT_REF_AND_RESET(query_exec
->comp_cls
);
60 if (query_exec
->object
) {
61 g_string_free(query_exec
->object
, TRUE
);
62 query_exec
->object
= NULL
;
65 BT_LOGD_STR("Putting parameters.");
66 BT_OBJECT_PUT_REF_AND_RESET(query_exec
->params
);
67 BT_OBJECT_PUT_REF_AND_RESET(query_exec
->default_interrupter
);
71 struct bt_query_executor
*bt_query_executor_create(
72 const bt_component_class
*comp_cls
, const char *object
,
73 const bt_value
*params
)
75 struct bt_query_executor
*query_exec
;
77 BT_ASSERT_PRE_NON_NULL(comp_cls
, "Component class");
78 BT_ASSERT_PRE_NON_NULL(object
, "Object");
79 BT_LIB_LOGD("Creating query executor: "
80 "%![comp-cls-]+C, object=\"%s\", %![params-]%+v",
81 comp_cls
, object
, params
);
82 query_exec
= g_new0(struct bt_query_executor
, 1);
84 BT_LIB_LOGE_APPEND_CAUSE(
85 "Failed to allocate one query executor.");
89 query_exec
->interrupters
= g_ptr_array_new_with_free_func(
90 (GDestroyNotify
) bt_object_put_no_null_check
);
91 if (!query_exec
->interrupters
) {
92 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
93 BT_OBJECT_PUT_REF_AND_RESET(query_exec
);
97 query_exec
->default_interrupter
= bt_interrupter_create();
98 if (!query_exec
->default_interrupter
) {
99 BT_LIB_LOGE_APPEND_CAUSE(
100 "Failed to create one interrupter object.");
101 BT_OBJECT_PUT_REF_AND_RESET(query_exec
);
105 query_exec
->object
= g_string_new(object
);
106 if (!query_exec
->object
) {
107 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString.");
108 BT_OBJECT_PUT_REF_AND_RESET(query_exec
);
112 query_exec
->comp_cls
= comp_cls
;
113 bt_object_get_no_null_check(query_exec
->comp_cls
);
116 query_exec
->params
= bt_value_null
;
118 query_exec
->params
= params
;
121 bt_object_get_no_null_check(query_exec
->params
);
122 query_exec
->log_level
= BT_LOGGING_LEVEL_NONE
;
123 bt_query_executor_add_interrupter(query_exec
,
124 query_exec
->default_interrupter
);
125 bt_object_init_shared(&query_exec
->base
,
126 bt_query_executor_destroy
);
127 BT_LIB_LOGD("Created query executor: "
128 "addr=%p, %![comp-cls-]+C, object=\"%s\", %![params-]%+v",
129 query_exec
, comp_cls
, object
, params
);
132 return (void *) query_exec
;
135 enum bt_query_executor_query_status
bt_query_executor_query(
136 struct bt_query_executor
*query_exec
,
137 const struct bt_value
**user_result
)
139 typedef enum bt_component_class_query_method_status (*method_t
)(
140 void * /* self component class */,
141 void * /* private query executor */,
142 const char * /* object */,
143 const struct bt_value
* /* parameters */,
144 const struct bt_value
** /* result */);
146 enum bt_query_executor_query_status status
;
147 enum bt_component_class_query_method_status query_status
;
148 method_t method
= NULL
;
150 BT_ASSERT_PRE_NON_NULL(query_exec
, "Query executor");
151 BT_ASSERT_PRE_NON_NULL(user_result
, "Result (output)");
154 * Initial check: is the query executor already interrupted? If
155 * so, return `BT_FUNC_STATUS_AGAIN`. Returning this status is
156 * harmless: it's not `BT_FUNC_STATUS_OK` (there's no result),
157 * and it's not `BT_FUNC_STATUS_ERROR` either (there's no
158 * legitimate error). Since any query operation could return
159 * `BT_FUNC_STATUS_AGAIN` when interrupted or instead of
160 * blocking, the caller is responsible for checking the
161 * interruption state of the query executor when getting this
164 if (bt_query_executor_is_interrupted(query_exec
)) {
165 BT_LIB_LOGD("Query executor is interrupted: "
166 "not performing the query operation: "
167 "query-exec-addr=%p, %![cc-]+C, object=\"%s\", "
168 "%![params-]+v, log-level=%s",
169 query_exec
, query_exec
->comp_cls
,
170 query_exec
->object
->str
, query_exec
->params
,
171 bt_common_logging_level_string(query_exec
->log_level
));
172 status
= BT_FUNC_STATUS_AGAIN
;
176 switch (query_exec
->comp_cls
->type
) {
177 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
179 struct bt_component_class_source
*src_cc
= (void *)
180 query_exec
->comp_cls
;
182 method
= (method_t
) src_cc
->methods
.query
;
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
;
193 case BT_COMPONENT_CLASS_TYPE_SINK
:
195 struct bt_component_class_sink
*sink_cc
= (void *)
196 query_exec
->comp_cls
;
198 method
= (method_t
) sink_cc
->methods
.query
;
206 /* Not an error: nothing to query */
207 BT_LIB_LOGD("Component class has no registered query method: "
208 "%!+C", query_exec
->comp_cls
);
209 status
= BT_FUNC_STATUS_UNKNOWN_OBJECT
;
213 BT_LIB_LOGD("Calling user's query method: "
214 "query-exec-addr=%p, %![cc-]+C, object=\"%s\", %![params-]+v, "
216 query_exec
, query_exec
->comp_cls
, query_exec
->object
->str
,
218 bt_common_logging_level_string(query_exec
->log_level
));
220 query_status
= method((void *) query_exec
->comp_cls
,
221 (void *) query_exec
, query_exec
->object
->str
,
222 query_exec
->params
, user_result
);
223 BT_LIB_LOGD("User method returned: status=%s, %![res-]+v",
224 bt_common_func_status_string(query_status
), *user_result
);
225 BT_ASSERT_POST(query_status
!= BT_FUNC_STATUS_OK
|| *user_result
,
226 "User method returned `BT_FUNC_STATUS_OK` without a result.");
227 status
= (int) query_status
;
230 BT_LIB_LOGW_APPEND_CAUSE(
231 "Component class's \"query\" method failed: "
232 "query-exec-addr=%p, %![cc-]+C, object=\"%s\", "
233 "%![params-]+v, log-level=%s", query_exec
,
234 query_exec
->comp_cls
, query_exec
->object
->str
,
236 bt_common_logging_level_string(query_exec
->log_level
));
244 enum bt_query_executor_add_interrupter_status
bt_query_executor_add_interrupter(
245 struct bt_query_executor
*query_exec
,
246 const struct bt_interrupter
*intr
)
248 BT_ASSERT_PRE_NON_NULL(query_exec
, "Query executor");
249 BT_ASSERT_PRE_NON_NULL(intr
, "Interrupter");
250 g_ptr_array_add(query_exec
->interrupters
, (void *) intr
);
251 bt_object_get_no_null_check(intr
);
252 BT_LIB_LOGD("Added interrupter to query executor: "
253 "query-exec-addr=%p, %![intr-]+z",
255 return BT_FUNC_STATUS_OK
;
258 bt_bool
bt_query_executor_is_interrupted(const struct bt_query_executor
*query_exec
)
260 BT_ASSERT_PRE_NON_NULL(query_exec
, "Query executor");
261 return (bt_bool
) bt_interrupter_array_any_is_set(
262 query_exec
->interrupters
);
265 void bt_query_executor_interrupt(struct bt_query_executor
*query_exec
)
267 BT_ASSERT_PRE_NON_NULL(query_exec
, "Query executor");
268 bt_interrupter_set(query_exec
->default_interrupter
);
269 BT_LIB_LOGI("Interrupted query executor: query-exec-addr=%p",
273 enum bt_query_executor_set_logging_level_status
274 bt_query_executor_set_logging_level(struct bt_query_executor
*query_exec
,
275 enum bt_logging_level log_level
)
277 BT_ASSERT_PRE_NON_NULL(query_exec
, "Query executor");
278 query_exec
->log_level
= log_level
;
279 return BT_FUNC_STATUS_OK
;
282 enum bt_logging_level
bt_query_executor_get_logging_level(
283 const struct bt_query_executor
*query_exec
)
285 BT_ASSERT_PRE_NON_NULL(query_exec
, "Query executor");
286 return query_exec
->log_level
;
289 void bt_query_executor_get_ref(const struct bt_query_executor
*query_executor
)
291 bt_object_get_ref(query_executor
);
294 void bt_query_executor_put_ref(const struct bt_query_executor
*query_executor
)
296 bt_object_put_ref(query_executor
);