Re-organize sources
[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/babeltrace.h"
30 #include "lib/object.h"
31 #include "lib/object-pool.h"
32 #include "common/assert.h"
33 #include <stdlib.h>
34 #include <glib.h>
35
36 #include "component.h"
37 #include "component-sink.h"
38 #include "connection.h"
39
40 struct bt_component;
41 struct bt_port;
42
43 enum bt_graph_configuration_state {
44 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
45 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED,
46 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED,
47 BT_GRAPH_CONFIGURATION_STATE_FAULTY,
48 };
49
50 struct bt_graph {
51 /**
52 * A component graph contains components and point-to-point connection
53 * between these components.
54 *
55 * In terms of ownership:
56 * 1) The graph is the components' parent,
57 * 2) The graph is the connnections' parent,
58 * 3) Components share the ownership of their connections,
59 * 4) A connection holds weak references to its two component endpoints.
60 */
61 struct bt_object base;
62
63 /* Array of pointers to bt_connection. */
64 GPtrArray *connections;
65 /* Array of pointers to bt_component. */
66 GPtrArray *components;
67 /* Queue of pointers (weak references) to sink bt_components. */
68 GQueue *sinks_to_consume;
69
70 bool canceled;
71 bool in_remove_listener;
72 bool has_sink;
73
74 /*
75 * If this is false, then the public API's consuming
76 * functions (bt_graph_consume() and bt_graph_run()) return
77 * BT_GRAPH_STATUS_CANNOT_CONSUME. The internal "no check"
78 * functions always work.
79 *
80 * In bt_port_output_message_iterator_create(), on success,
81 * this flag is cleared so that the iterator remains the only
82 * consumer for the graph's lifetime.
83 */
84 bool can_consume;
85
86 enum bt_graph_configuration_state config_state;
87
88 struct {
89 GArray *source_output_port_added;
90 GArray *filter_output_port_added;
91 GArray *filter_input_port_added;
92 GArray *sink_input_port_added;
93 GArray *source_filter_ports_connected;
94 GArray *source_sink_ports_connected;
95 GArray *filter_filter_ports_connected;
96 GArray *filter_sink_ports_connected;
97 } listeners;
98
99 /* Pool of `struct bt_message_event *` */
100 struct bt_object_pool event_msg_pool;
101
102 /* Pool of `struct bt_message_packet_beginning *` */
103 struct bt_object_pool packet_begin_msg_pool;
104
105 /* Pool of `struct bt_message_packet_end *` */
106 struct bt_object_pool packet_end_msg_pool;
107
108 /*
109 * Array of `struct bt_message *` (weak).
110 *
111 * This is an array of all the messages ever created from
112 * this graph. Some of them can be in one of the pools above,
113 * some of them can be at large. Because each message has a
114 * weak pointer to the graph containing its pool, we need to
115 * notify each message that the graph is gone on graph
116 * destruction.
117 *
118 * TODO: When we support a maximum size for object pools,
119 * add a way for a message to remove itself from this
120 * array (on destruction).
121 */
122 GPtrArray *messages;
123 };
124
125 static inline
126 void _bt_graph_set_can_consume(struct bt_graph *graph, bool can_consume)
127 {
128 BT_ASSERT(graph);
129 graph->can_consume = can_consume;
130 }
131
132 #ifdef BT_DEV_MODE
133 # define bt_graph_set_can_consume _bt_graph_set_can_consume
134 #else
135 # define bt_graph_set_can_consume(_graph, _can_consume)
136 #endif
137
138 BT_HIDDEN
139 enum bt_graph_status bt_graph_consume_sink_no_check(struct bt_graph *graph,
140 struct bt_component_sink *sink);
141
142 BT_HIDDEN
143 enum bt_graph_listener_status bt_graph_notify_port_added(struct bt_graph *graph,
144 struct bt_port *port);
145
146 BT_HIDDEN
147 enum bt_graph_listener_status bt_graph_notify_ports_connected(
148 struct bt_graph *graph, struct bt_port *upstream_port,
149 struct bt_port *downstream_port);
150
151 BT_HIDDEN
152 void bt_graph_remove_connection(struct bt_graph *graph,
153 struct bt_connection *connection);
154
155 /*
156 * This only works with a component which is not connected at this
157 * point.
158 *
159 * Also the reference count of `component` should be 0 when you call
160 * this function, which means only `graph` owns the component, so it
161 * is safe to destroy.
162 */
163 BT_HIDDEN
164 int bt_graph_remove_unconnected_component(struct bt_graph *graph,
165 struct bt_component *component);
166
167 BT_HIDDEN
168 void bt_graph_add_message(struct bt_graph *graph,
169 struct bt_message *msg);
170
171 static inline
172 const char *bt_graph_status_string(enum bt_graph_status status)
173 {
174 switch (status) {
175 case BT_GRAPH_STATUS_CANCELED:
176 return "BT_GRAPH_STATUS_CANCELED";
177 case BT_GRAPH_STATUS_AGAIN:
178 return "BT_GRAPH_STATUS_AGAIN";
179 case BT_GRAPH_STATUS_END:
180 return "BT_GRAPH_STATUS_END";
181 case BT_GRAPH_STATUS_OK:
182 return "BT_GRAPH_STATUS_OK";
183 case BT_GRAPH_STATUS_ERROR:
184 return "BT_GRAPH_STATUS_ERROR";
185 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION:
186 return "BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION";
187 case BT_GRAPH_STATUS_NOMEM:
188 return "BT_GRAPH_STATUS_NOMEM";
189 default:
190 return "(unknown)";
191 }
192 }
193
194 static inline
195 const char *bt_graph_configuration_state_string(
196 enum bt_graph_configuration_state state)
197 {
198 switch (state) {
199 case BT_GRAPH_CONFIGURATION_STATE_CONFIGURING:
200 return "BT_GRAPH_CONFIGURATION_STATE_CONFIGURING";
201 case BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED:
202 return "BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED";
203 case BT_GRAPH_CONFIGURATION_STATE_CONFIGURED:
204 return "BT_GRAPH_CONFIGURATION_STATE_CONFIGURED";
205 default:
206 return "(unknown)";
207 }
208 }
209
210 static inline
211 enum bt_graph_status bt_graph_configure(struct bt_graph *graph)
212 {
213 enum bt_graph_status status = BT_GRAPH_STATUS_OK;
214 uint64_t i;
215
216 BT_ASSERT(graph->config_state != BT_GRAPH_CONFIGURATION_STATE_FAULTY);
217
218 if (likely(graph->config_state ==
219 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED)) {
220 goto end;
221 }
222
223 #ifdef BT_ASSERT_PRE
224 BT_ASSERT_PRE(graph->has_sink, "Graph has no sink component: %!+g", graph);
225 #endif
226
227 graph->config_state = BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED;
228
229 for (i = 0; i < graph->components->len; i++) {
230 struct bt_component *comp = graph->components->pdata[i];
231 struct bt_component_sink *comp_sink = (void *) comp;
232 struct bt_component_class_sink *comp_cls_sink =
233 (void *) comp->class;
234
235 if (comp->class->type != BT_COMPONENT_CLASS_TYPE_SINK) {
236 continue;
237 }
238
239 if (comp_sink->graph_is_configured_method_called) {
240 continue;
241 }
242
243 if (comp_cls_sink->methods.graph_is_configured) {
244 enum bt_self_component_status comp_status;
245
246 #ifdef BT_LIB_LOGD
247 BT_LIB_LOGD("Calling user's \"graph is configured\" method: "
248 "%![graph-]+g, %![comp-]+c",
249 graph, comp);
250 #endif
251
252 comp_status = comp_cls_sink->methods.graph_is_configured(
253 (void *) comp_sink);
254
255 #ifdef BT_LIB_LOGD
256 BT_LIB_LOGD("User method returned: status=%s",
257 bt_self_component_status_string(comp_status));
258 #endif
259
260 #ifdef BT_ASSERT_PRE
261 BT_ASSERT_PRE(comp_status == BT_SELF_COMPONENT_STATUS_OK ||
262 comp_status == BT_SELF_COMPONENT_STATUS_ERROR ||
263 comp_status == BT_SELF_COMPONENT_STATUS_NOMEM,
264 "Unexpected returned status: status=%s",
265 bt_self_component_status_string(comp_status));
266 #endif
267
268 if (comp_status != BT_SELF_COMPONENT_STATUS_OK) {
269 status = BT_GRAPH_STATUS_ERROR;
270 #ifdef BT_LIB_LOGW
271 BT_LIB_LOGW("User's \"graph is configured\" method failed: "
272 "%![comp-]+c, status=%s",
273 comp,
274 bt_self_component_status_string(
275 comp_status));
276 #endif
277
278 goto end;
279 }
280 }
281
282 comp_sink->graph_is_configured_method_called = true;
283 }
284
285 graph->config_state = BT_GRAPH_CONFIGURATION_STATE_CONFIGURED;
286
287 end:
288 return status;
289 }
290
291 static inline
292 void bt_graph_make_faulty(struct bt_graph *graph)
293 {
294 graph->config_state = BT_GRAPH_CONFIGURATION_STATE_FAULTY;
295 #ifdef BT_LIB_LOGD
296 BT_LIB_LOGD("Set graph's state to faulty: %![graph-]+g", graph);
297 #endif
298 }
299
300 #endif /* BABELTRACE_GRAPH_GRAPH_INTERNAL_H */
This page took 0.03467 seconds and 4 git commands to generate.