lib: rename `bt_object_{get,put}_no` -> `bt_object_{get,put}_ref_no`
[babeltrace.git] / src / lib / graph / query-executor.c
CommitLineData
1286dcbb 1/*
f2b0325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
1286dcbb
PP
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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
20 * SOFTWARE.
21 */
22
b03487ab 23#define BT_LOG_TAG "LIB/QUERY-EXECUTOR"
1633ef46 24#include "lib/logging.h"
1286dcbb 25
57952005 26#include "common/assert.h"
83da519a 27#include "common/common.h"
57952005 28#include "lib/assert-pre.h"
3b7d3ccc 29#include "lib/assert-post.h"
71c5da58
MJ
30#include <babeltrace2/graph/query-executor-const.h>
31#include <babeltrace2/graph/query-executor.h>
71c5da58 32#include <babeltrace2/graph/component-class.h>
71c5da58
MJ
33#include <babeltrace2/value.h>
34#include <babeltrace2/value-const.h>
57952005
MJ
35#include "lib/object.h"
36#include "compat/compiler.h"
37
38#include "component-class.h"
39#include "query-executor.h"
d73bb381 40#include "interrupter.h"
fb25b9e3 41#include "lib/func-status.h"
1286dcbb
PP
42
43static
44void bt_query_executor_destroy(struct bt_object *obj)
45{
46 struct bt_query_executor *query_exec =
47 container_of(obj, struct bt_query_executor, base);
48
834e9996 49 BT_LOGD("Destroying query executor: addr=%p", query_exec);
d73bb381
PP
50
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;
55 }
56
bf403eb2
PP
57 BT_LOGD_STR("Putting component class.");
58 BT_OBJECT_PUT_REF_AND_RESET(query_exec->comp_cls);
59
60 if (query_exec->object) {
61 g_string_free(query_exec->object, TRUE);
62 query_exec->object = NULL;
63 }
d73bb381 64
bf403eb2
PP
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);
1286dcbb
PP
68 g_free(query_exec);
69}
70
f6e305ce 71struct bt_query_executor *bt_query_executor_create_with_method_data(
bf403eb2 72 const bt_component_class *comp_cls, const char *object,
f6e305ce 73 const bt_value *params, void *method_data)
1286dcbb
PP
74{
75 struct bt_query_executor *query_exec;
76
bf403eb2
PP
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: "
013ff1e5 80 "%![comp-cls-]+C, object=\"%s\", %![params-]+v",
bf403eb2 81 comp_cls, object, params);
1286dcbb
PP
82 query_exec = g_new0(struct bt_query_executor, 1);
83 if (!query_exec) {
a8f90e5d
PP
84 BT_LIB_LOGE_APPEND_CAUSE(
85 "Failed to allocate one query executor.");
1286dcbb
PP
86 goto end;
87 }
88
d73bb381 89 query_exec->interrupters = g_ptr_array_new_with_free_func(
864aa43f 90 (GDestroyNotify) bt_object_put_ref_no_null_check);
d73bb381
PP
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);
94 goto end;
95 }
96
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);
102 goto end;
103 }
104
bf403eb2
PP
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);
109 goto end;
110 }
111
112 query_exec->comp_cls = comp_cls;
864aa43f 113 bt_object_get_ref_no_null_check(query_exec->comp_cls);
bf403eb2
PP
114
115 if (!params) {
116 query_exec->params = bt_value_null;
117 } else {
118 query_exec->params = params;
119 }
120
864aa43f 121 bt_object_get_ref_no_null_check(query_exec->params);
f6e305ce 122 query_exec->method_data = method_data;
bf403eb2 123 query_exec->log_level = BT_LOGGING_LEVEL_NONE;
d73bb381
PP
124 bt_query_executor_add_interrupter(query_exec,
125 query_exec->default_interrupter);
1d7bf349
PP
126 bt_object_init_shared(&query_exec->base,
127 bt_query_executor_destroy);
bf403eb2 128 BT_LIB_LOGD("Created query executor: "
013ff1e5 129 "addr=%p, %![comp-cls-]+C, object=\"%s\", %![params-]+v",
bf403eb2 130 query_exec, comp_cls, object, params);
1286dcbb
PP
131
132end:
834e9996 133 return (void *) query_exec;
1286dcbb
PP
134}
135
f6e305ce
PP
136struct bt_query_executor *bt_query_executor_create(
137 const bt_component_class *comp_cls, const char *object,
138 const bt_value *params)
139{
140 return bt_query_executor_create_with_method_data(comp_cls,
141 object, params, NULL);
142}
143
fb25b9e3 144enum bt_query_executor_query_status bt_query_executor_query(
7b53201c 145 struct bt_query_executor *query_exec,
ce141536 146 const struct bt_value **user_result)
1286dcbb 147{
bf403eb2
PP
148 typedef enum bt_component_class_query_method_status (*method_t)(
149 void * /* self component class */,
150 void * /* private query executor */,
151 const char * /* object */,
152 const struct bt_value * /* parameters */,
f6e305ce 153 void * /* method data */,
bf403eb2 154 const struct bt_value ** /* result */);
1286dcbb 155
fb25b9e3
PP
156 enum bt_query_executor_query_status status;
157 enum bt_component_class_query_method_status query_status;
834e9996 158 method_t method = NULL;
1286dcbb 159
834e9996 160 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
834e9996 161 BT_ASSERT_PRE_NON_NULL(user_result, "Result (output)");
834e9996 162
c7f8c4e4
PP
163 /*
164 * Initial check: is the query executor already interrupted? If
165 * so, return `BT_FUNC_STATUS_AGAIN`. Returning this status is
166 * harmless: it's not `BT_FUNC_STATUS_OK` (there's no result),
167 * and it's not `BT_FUNC_STATUS_ERROR` either (there's no
168 * legitimate error). Since any query operation could return
169 * `BT_FUNC_STATUS_AGAIN` when interrupted or instead of
170 * blocking, the caller is responsible for checking the
171 * interruption state of the query executor when getting this
172 * status.
173 */
174 if (bt_query_executor_is_interrupted(query_exec)) {
175 BT_LIB_LOGD("Query executor is interrupted: "
176 "not performing the query operation: "
177 "query-exec-addr=%p, %![cc-]+C, object=\"%s\", "
178 "%![params-]+v, log-level=%s",
bf403eb2
PP
179 query_exec, query_exec->comp_cls,
180 query_exec->object->str, query_exec->params,
181 bt_common_logging_level_string(query_exec->log_level));
c7f8c4e4
PP
182 status = BT_FUNC_STATUS_AGAIN;
183 goto end;
184 }
185
bf403eb2 186 switch (query_exec->comp_cls->type) {
834e9996
PP
187 case BT_COMPONENT_CLASS_TYPE_SOURCE:
188 {
bf403eb2
PP
189 struct bt_component_class_source *src_cc = (void *)
190 query_exec->comp_cls;
834e9996
PP
191
192 method = (method_t) src_cc->methods.query;
193 break;
1286dcbb 194 }
834e9996
PP
195 case BT_COMPONENT_CLASS_TYPE_FILTER:
196 {
bf403eb2
PP
197 struct bt_component_class_filter *flt_cc = (void *)
198 query_exec->comp_cls;
1286dcbb 199
834e9996
PP
200 method = (method_t) flt_cc->methods.query;
201 break;
1286dcbb 202 }
834e9996
PP
203 case BT_COMPONENT_CLASS_TYPE_SINK:
204 {
bf403eb2
PP
205 struct bt_component_class_sink *sink_cc = (void *)
206 query_exec->comp_cls;
1286dcbb 207
834e9996
PP
208 method = (method_t) sink_cc->methods.query;
209 break;
210 }
211 default:
212 abort();
1286dcbb
PP
213 }
214
834e9996 215 if (!method) {
1286dcbb 216 /* Not an error: nothing to query */
834e9996 217 BT_LIB_LOGD("Component class has no registered query method: "
bf403eb2 218 "%!+C", query_exec->comp_cls);
7dd3e712 219 status = BT_FUNC_STATUS_UNKNOWN_OBJECT;
1286dcbb
PP
220 goto end;
221 }
222
834e9996 223 BT_LIB_LOGD("Calling user's query method: "
83da519a
PP
224 "query-exec-addr=%p, %![cc-]+C, object=\"%s\", %![params-]+v, "
225 "log-level=%s",
bf403eb2
PP
226 query_exec, query_exec->comp_cls, query_exec->object->str,
227 query_exec->params,
228 bt_common_logging_level_string(query_exec->log_level));
834e9996 229 *user_result = NULL;
bf403eb2
PP
230 query_status = method((void *) query_exec->comp_cls,
231 (void *) query_exec, query_exec->object->str,
f6e305ce 232 query_exec->params, query_exec->method_data, user_result);
834e9996 233 BT_LIB_LOGD("User method returned: status=%s, %![res-]+v",
fb25b9e3
PP
234 bt_common_func_status_string(query_status), *user_result);
235 BT_ASSERT_POST(query_status != BT_FUNC_STATUS_OK || *user_result,
236 "User method returned `BT_FUNC_STATUS_OK` without a result.");
237 status = (int) query_status;
1286dcbb 238
574dea68
SM
239 if (status < 0) {
240 BT_LIB_LOGW_APPEND_CAUSE(
241 "Component class's \"query\" method failed: "
242 "query-exec-addr=%p, %![cc-]+C, object=\"%s\", "
bf403eb2
PP
243 "%![params-]+v, log-level=%s", query_exec,
244 query_exec->comp_cls, query_exec->object->str,
245 query_exec->params,
246 bt_common_logging_level_string(query_exec->log_level));
574dea68
SM
247 goto end;
248 }
249
1286dcbb 250end:
fb25b9e3 251 return status;
1286dcbb
PP
252}
253
d73bb381
PP
254enum bt_query_executor_add_interrupter_status bt_query_executor_add_interrupter(
255 struct bt_query_executor *query_exec,
256 const struct bt_interrupter *intr)
1286dcbb 257{
834e9996 258 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
d73bb381
PP
259 BT_ASSERT_PRE_NON_NULL(intr, "Interrupter");
260 g_ptr_array_add(query_exec->interrupters, (void *) intr);
864aa43f 261 bt_object_get_ref_no_null_check(intr);
d73bb381
PP
262 BT_LIB_LOGD("Added interrupter to query executor: "
263 "query-exec-addr=%p, %![intr-]+z",
264 query_exec, intr);
fb25b9e3 265 return BT_FUNC_STATUS_OK;
1286dcbb
PP
266}
267
d73bb381 268bt_bool bt_query_executor_is_interrupted(const struct bt_query_executor *query_exec)
1286dcbb 269{
d73bb381
PP
270 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
271 return (bt_bool) bt_interrupter_array_any_is_set(
272 query_exec->interrupters);
273}
274
275void bt_query_executor_interrupt(struct bt_query_executor *query_exec)
276{
277 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
278 bt_interrupter_set(query_exec->default_interrupter);
279 BT_LIB_LOGI("Interrupted query executor: query-exec-addr=%p",
280 query_exec);
1286dcbb 281}
8c6884d9 282
bf403eb2
PP
283enum bt_query_executor_set_logging_level_status
284bt_query_executor_set_logging_level(struct bt_query_executor *query_exec,
285 enum bt_logging_level log_level)
286{
287 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
288 query_exec->log_level = log_level;
289 return BT_FUNC_STATUS_OK;
290}
291
292enum bt_logging_level bt_query_executor_get_logging_level(
293 const struct bt_query_executor *query_exec)
294{
295 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
296 return query_exec->log_level;
297}
298
8c6884d9
PP
299void bt_query_executor_get_ref(const struct bt_query_executor *query_executor)
300{
301 bt_object_get_ref(query_executor);
302}
303
304void bt_query_executor_put_ref(const struct bt_query_executor *query_executor)
305{
306 bt_object_put_ref(query_executor);
307}
This page took 0.069685 seconds and 4 git commands to generate.