lib: rename INVALID_OBJECT status to UNKNOWN_OBJECT
[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
57 BT_OBJECT_PUT_REF_AND_RESET(query_exec->default_interrupter);
58
1286dcbb
PP
59 g_free(query_exec);
60}
61
7b53201c 62struct bt_query_executor *bt_query_executor_create(void)
1286dcbb
PP
63{
64 struct bt_query_executor *query_exec;
65
66 BT_LOGD_STR("Creating query executor.");
67 query_exec = g_new0(struct bt_query_executor, 1);
68 if (!query_exec) {
a8f90e5d
PP
69 BT_LIB_LOGE_APPEND_CAUSE(
70 "Failed to allocate one query executor.");
1286dcbb
PP
71 goto end;
72 }
73
d73bb381
PP
74 query_exec->interrupters = g_ptr_array_new_with_free_func(
75 (GDestroyNotify) bt_object_put_no_null_check);
76 if (!query_exec->interrupters) {
77 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
78 BT_OBJECT_PUT_REF_AND_RESET(query_exec);
79 goto end;
80 }
81
82 query_exec->default_interrupter = bt_interrupter_create();
83 if (!query_exec->default_interrupter) {
84 BT_LIB_LOGE_APPEND_CAUSE(
85 "Failed to create one interrupter object.");
86 BT_OBJECT_PUT_REF_AND_RESET(query_exec);
87 goto end;
88 }
89
90 bt_query_executor_add_interrupter(query_exec,
91 query_exec->default_interrupter);
1d7bf349
PP
92 bt_object_init_shared(&query_exec->base,
93 bt_query_executor_destroy);
1286dcbb
PP
94 BT_LOGD("Created query executor: addr=%p", query_exec);
95
96end:
834e9996 97 return (void *) query_exec;
1286dcbb
PP
98}
99
fb25b9e3 100enum bt_query_executor_query_status bt_query_executor_query(
7b53201c
PP
101 struct bt_query_executor *query_exec,
102 const struct bt_component_class *comp_cls,
ce141536 103 const char *object, const struct bt_value *params,
fb25b9e3 104 enum bt_logging_level log_level,
ce141536 105 const struct bt_value **user_result)
1286dcbb 106{
fb25b9e3
PP
107 typedef enum bt_component_class_query_method_status (*method_t)(void *,
108 const void *, const void *, const void *, enum bt_logging_level,
109 const void *);
1286dcbb 110
fb25b9e3
PP
111 enum bt_query_executor_query_status status;
112 enum bt_component_class_query_method_status query_status;
834e9996 113 method_t method = NULL;
1286dcbb 114
834e9996
PP
115 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
116 BT_ASSERT_PRE_NON_NULL(comp_cls, "Component class");
117 BT_ASSERT_PRE_NON_NULL(object, "Object");
118 BT_ASSERT_PRE_NON_NULL(user_result, "Result (output)");
834e9996 119
c7f8c4e4
PP
120 /*
121 * Initial check: is the query executor already interrupted? If
122 * so, return `BT_FUNC_STATUS_AGAIN`. Returning this status is
123 * harmless: it's not `BT_FUNC_STATUS_OK` (there's no result),
124 * and it's not `BT_FUNC_STATUS_ERROR` either (there's no
125 * legitimate error). Since any query operation could return
126 * `BT_FUNC_STATUS_AGAIN` when interrupted or instead of
127 * blocking, the caller is responsible for checking the
128 * interruption state of the query executor when getting this
129 * status.
130 */
131 if (bt_query_executor_is_interrupted(query_exec)) {
132 BT_LIB_LOGD("Query executor is interrupted: "
133 "not performing the query operation: "
134 "query-exec-addr=%p, %![cc-]+C, object=\"%s\", "
135 "%![params-]+v, log-level=%s",
136 query_exec, comp_cls, object, params,
137 bt_common_logging_level_string(log_level));
138 status = BT_FUNC_STATUS_AGAIN;
139 goto end;
140 }
141
834e9996
PP
142 if (!params) {
143 params = bt_value_null;
1286dcbb
PP
144 }
145
834e9996
PP
146 switch (comp_cls->type) {
147 case BT_COMPONENT_CLASS_TYPE_SOURCE:
148 {
149 struct bt_component_class_source *src_cc = (void *) comp_cls;
150
151 method = (method_t) src_cc->methods.query;
152 break;
1286dcbb 153 }
834e9996
PP
154 case BT_COMPONENT_CLASS_TYPE_FILTER:
155 {
156 struct bt_component_class_filter *flt_cc = (void *) comp_cls;
1286dcbb 157
834e9996
PP
158 method = (method_t) flt_cc->methods.query;
159 break;
1286dcbb 160 }
834e9996
PP
161 case BT_COMPONENT_CLASS_TYPE_SINK:
162 {
163 struct bt_component_class_sink *sink_cc = (void *) comp_cls;
1286dcbb 164
834e9996
PP
165 method = (method_t) sink_cc->methods.query;
166 break;
167 }
168 default:
169 abort();
1286dcbb
PP
170 }
171
834e9996 172 if (!method) {
1286dcbb 173 /* Not an error: nothing to query */
834e9996
PP
174 BT_LIB_LOGD("Component class has no registered query method: "
175 "%!+C", comp_cls);
7dd3e712 176 status = BT_FUNC_STATUS_UNKNOWN_OBJECT;
1286dcbb
PP
177 goto end;
178 }
179
834e9996 180 BT_LIB_LOGD("Calling user's query method: "
83da519a
PP
181 "query-exec-addr=%p, %![cc-]+C, object=\"%s\", %![params-]+v, "
182 "log-level=%s",
183 query_exec, comp_cls, object, params,
184 bt_common_logging_level_string(log_level));
834e9996 185 *user_result = NULL;
fb25b9e3 186 query_status = method((void *) comp_cls, query_exec, object, params,
83da519a 187 log_level, user_result);
834e9996 188 BT_LIB_LOGD("User method returned: status=%s, %![res-]+v",
fb25b9e3
PP
189 bt_common_func_status_string(query_status), *user_result);
190 BT_ASSERT_POST(query_status != BT_FUNC_STATUS_OK || *user_result,
191 "User method returned `BT_FUNC_STATUS_OK` without a result.");
192 status = (int) query_status;
1286dcbb 193
574dea68
SM
194 if (status < 0) {
195 BT_LIB_LOGW_APPEND_CAUSE(
196 "Component class's \"query\" method failed: "
197 "query-exec-addr=%p, %![cc-]+C, object=\"%s\", "
198 "%![params-]+v, log-level=%s", query_exec, comp_cls,
199 object, params, bt_common_logging_level_string(log_level));
200 goto end;
201 }
202
1286dcbb 203end:
fb25b9e3 204 return status;
1286dcbb
PP
205}
206
d73bb381
PP
207enum bt_query_executor_add_interrupter_status bt_query_executor_add_interrupter(
208 struct bt_query_executor *query_exec,
209 const struct bt_interrupter *intr)
1286dcbb 210{
834e9996 211 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
d73bb381
PP
212 BT_ASSERT_PRE_NON_NULL(intr, "Interrupter");
213 g_ptr_array_add(query_exec->interrupters, (void *) intr);
214 bt_object_get_no_null_check(intr);
215 BT_LIB_LOGD("Added interrupter to query executor: "
216 "query-exec-addr=%p, %![intr-]+z",
217 query_exec, intr);
fb25b9e3 218 return BT_FUNC_STATUS_OK;
1286dcbb
PP
219}
220
d73bb381 221bt_bool bt_query_executor_is_interrupted(const struct bt_query_executor *query_exec)
1286dcbb 222{
d73bb381
PP
223 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
224 return (bt_bool) bt_interrupter_array_any_is_set(
225 query_exec->interrupters);
226}
227
228void bt_query_executor_interrupt(struct bt_query_executor *query_exec)
229{
230 BT_ASSERT_PRE_NON_NULL(query_exec, "Query executor");
231 bt_interrupter_set(query_exec->default_interrupter);
232 BT_LIB_LOGI("Interrupted query executor: query-exec-addr=%p",
233 query_exec);
1286dcbb 234}
8c6884d9
PP
235
236void bt_query_executor_get_ref(const struct bt_query_executor *query_executor)
237{
238 bt_object_get_ref(query_executor);
239}
240
241void bt_query_executor_put_ref(const struct bt_query_executor *query_executor)
242{
243 bt_object_put_ref(query_executor);
244}
This page took 0.07758 seconds and 4 git commands to generate.