lib: internal: graph.h: require logging and BT_ASSERT_{PRE,POST}()
[babeltrace.git] / src / lib / graph / graph.h
1 #ifndef BABELTRACE_GRAPH_GRAPH_INTERNAL_H
2 #define BABELTRACE_GRAPH_GRAPH_INTERNAL_H
3
4 /*
5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
6 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #include <babeltrace2/graph/graph.h>
28 #include <babeltrace2/graph/message-const.h>
29 #include "common/macros.h"
30 #include "lib/object.h"
31 #include "lib/object-pool.h"
32 #include "common/assert.h"
33 #include "common/common.h"
34 #include <stdlib.h>
35 #include <glib.h>
36
37 #include "component.h"
38 #include "component-sink.h"
39 #include "connection.h"
40
41 /* Protection: this file uses BT_LIB_LOG*() macros directly */
42 #ifndef BT_LIB_LOG_SUPPORTED
43 # error Please include "lib/logging.h" before including this file.
44 #endif
45
46 /* Protection: this file uses BT_ASSERT_PRE*() macros directly */
47 #ifndef BT_ASSERT_PRE_SUPPORTED
48 # error Please include "lib/assert-pre.h" before including this file.
49 #endif
50
51 /* Protection: this file uses BT_ASSERT_POST*() macros directly */
52 #ifndef BT_ASSERT_POST_SUPPORTED
53 # error Please include "lib/assert-post.h" before including this file.
54 #endif
55
56 struct bt_component;
57 struct bt_port;
58
59 enum bt_graph_configuration_state {
60 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
61 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED,
62 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED,
63 BT_GRAPH_CONFIGURATION_STATE_FAULTY,
64 };
65
66 struct bt_graph {
67 /**
68 * A component graph contains components and point-to-point connection
69 * between these components.
70 *
71 * In terms of ownership:
72 * 1) The graph is the components' parent,
73 * 2) The graph is the connnections' parent,
74 * 3) Components share the ownership of their connections,
75 * 4) A connection holds weak references to its two component endpoints.
76 */
77 struct bt_object base;
78
79 /* Array of pointers to bt_connection. */
80 GPtrArray *connections;
81 /* Array of pointers to bt_component. */
82 GPtrArray *components;
83 /* Queue of pointers (weak references) to sink bt_components. */
84 GQueue *sinks_to_consume;
85
86 bool canceled;
87 bool in_remove_listener;
88 bool has_sink;
89
90 /*
91 * If this is false, then the public API's consuming
92 * functions (bt_graph_consume() and bt_graph_run()) return
93 * BT_GRAPH_STATUS_CANNOT_CONSUME. The internal "no check"
94 * functions always work.
95 *
96 * In bt_port_output_message_iterator_create(), on success,
97 * this flag is cleared so that the iterator remains the only
98 * consumer for the graph's lifetime.
99 */
100 bool can_consume;
101
102 enum bt_graph_configuration_state config_state;
103
104 struct {
105 GArray *source_output_port_added;
106 GArray *filter_output_port_added;
107 GArray *filter_input_port_added;
108 GArray *sink_input_port_added;
109 GArray *source_filter_ports_connected;
110 GArray *source_sink_ports_connected;
111 GArray *filter_filter_ports_connected;
112 GArray *filter_sink_ports_connected;
113 } listeners;
114
115 /* Pool of `struct bt_message_event *` */
116 struct bt_object_pool event_msg_pool;
117
118 /* Pool of `struct bt_message_packet_beginning *` */
119 struct bt_object_pool packet_begin_msg_pool;
120
121 /* Pool of `struct bt_message_packet_end *` */
122 struct bt_object_pool packet_end_msg_pool;
123
124 /*
125 * Array of `struct bt_message *` (weak).
126 *
127 * This is an array of all the messages ever created from
128 * this graph. Some of them can be in one of the pools above,
129 * some of them can be at large. Because each message has a
130 * weak pointer to the graph containing its pool, we need to
131 * notify each message that the graph is gone on graph
132 * destruction.
133 *
134 * TODO: When we support a maximum size for object pools,
135 * add a way for a message to remove itself from this
136 * array (on destruction).
137 */
138 GPtrArray *messages;
139 };
140
141 static inline
142 void _bt_graph_set_can_consume(struct bt_graph *graph, bool can_consume)
143 {
144 BT_ASSERT(graph);
145 graph->can_consume = can_consume;
146 }
147
148 #ifdef BT_DEV_MODE
149 # define bt_graph_set_can_consume _bt_graph_set_can_consume
150 #else
151 # define bt_graph_set_can_consume(_graph, _can_consume)
152 #endif
153
154 BT_HIDDEN
155 enum bt_graph_status bt_graph_consume_sink_no_check(struct bt_graph *graph,
156 struct bt_component_sink *sink);
157
158 BT_HIDDEN
159 enum bt_graph_listener_status bt_graph_notify_port_added(struct bt_graph *graph,
160 struct bt_port *port);
161
162 BT_HIDDEN
163 enum bt_graph_listener_status bt_graph_notify_ports_connected(
164 struct bt_graph *graph, struct bt_port *upstream_port,
165 struct bt_port *downstream_port);
166
167 BT_HIDDEN
168 void bt_graph_remove_connection(struct bt_graph *graph,
169 struct bt_connection *connection);
170
171 /*
172 * This only works with a component which is not connected at this
173 * point.
174 *
175 * Also the reference count of `component` should be 0 when you call
176 * this function, which means only `graph` owns the component, so it
177 * is safe to destroy.
178 */
179 BT_HIDDEN
180 int bt_graph_remove_unconnected_component(struct bt_graph *graph,
181 struct bt_component *component);
182
183 BT_HIDDEN
184 void bt_graph_add_message(struct bt_graph *graph,
185 struct bt_message *msg);
186
187 static inline
188 const char *bt_graph_status_string(enum bt_graph_status status)
189 {
190 switch (status) {
191 case BT_GRAPH_STATUS_CANCELED:
192 return "BT_GRAPH_STATUS_CANCELED";
193 case BT_GRAPH_STATUS_AGAIN:
194 return "BT_GRAPH_STATUS_AGAIN";
195 case BT_GRAPH_STATUS_END:
196 return "BT_GRAPH_STATUS_END";
197 case BT_GRAPH_STATUS_OK:
198 return "BT_GRAPH_STATUS_OK";
199 case BT_GRAPH_STATUS_ERROR:
200 return "BT_GRAPH_STATUS_ERROR";
201 case BT_GRAPH_STATUS_NOMEM:
202 return "BT_GRAPH_STATUS_NOMEM";
203 default:
204 return "(unknown)";
205 }
206 }
207
208 static inline
209 const char *bt_graph_configuration_state_string(
210 enum bt_graph_configuration_state state)
211 {
212 switch (state) {
213 case BT_GRAPH_CONFIGURATION_STATE_CONFIGURING:
214 return "BT_GRAPH_CONFIGURATION_STATE_CONFIGURING";
215 case BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED:
216 return "BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED";
217 case BT_GRAPH_CONFIGURATION_STATE_CONFIGURED:
218 return "BT_GRAPH_CONFIGURATION_STATE_CONFIGURED";
219 default:
220 return "(unknown)";
221 }
222 }
223
224 static inline
225 enum bt_graph_status bt_graph_configure(struct bt_graph *graph)
226 {
227 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
228 uint64_t i;
229
230 BT_ASSERT(graph->config_state != BT_GRAPH_CONFIGURATION_STATE_FAULTY);
231
232 if (G_LIKELY(graph->config_state ==
233 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED)) {
234 goto end;
235 }
236
237 BT_ASSERT_PRE(graph->has_sink, "Graph has no sink component: %!+g", graph);
238 graph->config_state = BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED;
239
240 for (i = 0; i < graph->components->len; i++) {
241 struct bt_component *comp = graph->components->pdata[i];
242 struct bt_component_sink *comp_sink = (void *) comp;
243 struct bt_component_class_sink *comp_cls_sink =
244 (void *) comp->class;
245
246 if (comp->class->type != BT_COMPONENT_CLASS_TYPE_SINK) {
247 continue;
248 }
249
250 if (comp_sink->graph_is_configured_method_called) {
251 continue;
252 }
253
254 if (comp_cls_sink->methods.graph_is_configured) {
255 enum bt_self_component_status comp_status;
256
257 BT_LIB_LOGD("Calling user's \"graph is configured\" method: "
258 "%![graph-]+g, %![comp-]+c",
259 graph, comp);
260 comp_status = comp_cls_sink->methods.graph_is_configured(
261 (void *) comp_sink);
262 BT_LIB_LOGD("User method returned: status=%s",
263 bt_self_component_status_string(comp_status));
264 BT_ASSERT_POST(comp_status == BT_SELF_COMPONENT_STATUS_OK ||
265 comp_status == BT_SELF_COMPONENT_STATUS_ERROR ||
266 comp_status == BT_SELF_COMPONENT_STATUS_NOMEM,
267 "Unexpected returned status: status=%s",
268 bt_self_component_status_string(comp_status));
269
270 if (comp_status != BT_SELF_COMPONENT_STATUS_OK) {
271 status = BT_GRAPH_STATUS_ERROR;
272 BT_LIB_LOGW("User's \"graph is configured\" method failed: "
273 "%![comp-]+c, status=%s",
274 comp,
275 bt_self_component_status_string(
276 comp_status));
277 goto end;
278 }
279 }
280
281 comp_sink->graph_is_configured_method_called = true;
282 }
283
284 graph->config_state = BT_GRAPH_CONFIGURATION_STATE_CONFIGURED;
285
286 end:
287 return status;
288 }
289
290 static inline
291 void bt_graph_make_faulty(struct bt_graph *graph)
292 {
293 graph->config_state = BT_GRAPH_CONFIGURATION_STATE_FAULTY;
294 BT_LIB_LOGI("Set graph's state to faulty: %![graph-]+g", graph);
295 }
296
297 #endif /* BABELTRACE_GRAPH_GRAPH_INTERNAL_H */
This page took 0.042377 seconds and 5 git commands to generate.