Commit | Line | Data |
---|---|---|
abe30a8f PP |
1 | #ifndef BABELTRACE_GRAPH_GRAPH_INTERNAL_H |
2 | #define BABELTRACE_GRAPH_GRAPH_INTERNAL_H | |
c0418dd9 JG |
3 | |
4 | /* | |
f2b0325d | 5 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
f60c8b34 | 6 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
c0418dd9 | 7 | * |
c0418dd9 JG |
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 | ||
71c5da58 | 27 | #include <babeltrace2/graph/graph.h> |
71c5da58 | 28 | #include <babeltrace2/graph/message-const.h> |
85e7137b | 29 | #include "common/macros.h" |
57952005 MJ |
30 | #include "lib/object.h" |
31 | #include "lib/object-pool.h" | |
32 | #include "common/assert.h" | |
a256a42d | 33 | #include <stdlib.h> |
c0418dd9 JG |
34 | #include <glib.h> |
35 | ||
57952005 MJ |
36 | #include "component.h" |
37 | #include "component-sink.h" | |
38 | #include "connection.h" | |
39 | ||
1bf957a0 PP |
40 | struct bt_component; |
41 | struct bt_port; | |
42 | ||
1043fdea PP |
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, | |
9a84962b | 47 | BT_GRAPH_CONFIGURATION_STATE_FAULTY, |
1043fdea PP |
48 | }; |
49 | ||
f60c8b34 JG |
50 | struct bt_graph { |
51 | /** | |
52 | * A component graph contains components and point-to-point connection | |
53 | * between these components. | |
c0418dd9 | 54 | * |
f60c8b34 JG |
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. | |
c0418dd9 | 60 | */ |
f60c8b34 JG |
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; | |
1bf957a0 | 69 | |
834e9996 PP |
70 | bool canceled; |
71 | bool in_remove_listener; | |
72 | bool has_sink; | |
c3ac0193 PP |
73 | |
74 | /* | |
834e9996 | 75 | * If this is false, then the public API's consuming |
c3ac0193 PP |
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 | * | |
b09a5592 | 80 | * In bt_port_output_message_iterator_create(), on success, |
c3ac0193 PP |
81 | * this flag is cleared so that the iterator remains the only |
82 | * consumer for the graph's lifetime. | |
83 | */ | |
834e9996 | 84 | bool can_consume; |
202a3a13 | 85 | |
1043fdea | 86 | enum bt_graph_configuration_state config_state; |
9cce94e4 | 87 | |
1bf957a0 | 88 | struct { |
834e9996 PP |
89 | GArray *source_output_port_added; |
90 | GArray *filter_output_port_added; | |
91 | GArray *filter_input_port_added; | |
92 | GArray *sink_input_port_added; | |
834e9996 PP |
93 | GArray *source_filter_ports_connected; |
94 | GArray *source_sink_ports_connected; | |
ee976410 | 95 | GArray *filter_filter_ports_connected; |
834e9996 | 96 | GArray *filter_sink_ports_connected; |
1bf957a0 | 97 | } listeners; |
f7c3ac09 | 98 | |
b09a5592 PP |
99 | /* Pool of `struct bt_message_event *` */ |
100 | struct bt_object_pool event_msg_pool; | |
f7c3ac09 | 101 | |
b09a5592 PP |
102 | /* Pool of `struct bt_message_packet_beginning *` */ |
103 | struct bt_object_pool packet_begin_msg_pool; | |
f7c3ac09 | 104 | |
b09a5592 PP |
105 | /* Pool of `struct bt_message_packet_end *` */ |
106 | struct bt_object_pool packet_end_msg_pool; | |
f7c3ac09 PP |
107 | |
108 | /* | |
b09a5592 | 109 | * Array of `struct bt_message *` (weak). |
f7c3ac09 | 110 | * |
b09a5592 | 111 | * This is an array of all the messages ever created from |
f7c3ac09 | 112 | * this graph. Some of them can be in one of the pools above, |
b09a5592 | 113 | * some of them can be at large. Because each message has a |
f7c3ac09 | 114 | * weak pointer to the graph containing its pool, we need to |
b09a5592 | 115 | * notify each message that the graph is gone on graph |
f7c3ac09 PP |
116 | * destruction. |
117 | * | |
118 | * TODO: When we support a maximum size for object pools, | |
b09a5592 | 119 | * add a way for a message to remove itself from this |
f7c3ac09 PP |
120 | * array (on destruction). |
121 | */ | |
b09a5592 | 122 | GPtrArray *messages; |
c0418dd9 JG |
123 | }; |
124 | ||
c3ac0193 | 125 | static inline |
834e9996 | 126 | void _bt_graph_set_can_consume(struct bt_graph *graph, bool can_consume) |
c3ac0193 | 127 | { |
8b45963b | 128 | BT_ASSERT(graph); |
c3ac0193 PP |
129 | graph->can_consume = can_consume; |
130 | } | |
131 | ||
371361a1 PP |
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 | |
c3ac0193 PP |
137 | |
138 | BT_HIDDEN | |
139 | enum bt_graph_status bt_graph_consume_sink_no_check(struct bt_graph *graph, | |
834e9996 | 140 | struct bt_component_sink *sink); |
c3ac0193 | 141 | |
1bf957a0 | 142 | BT_HIDDEN |
e18f3e2b SM |
143 | enum bt_graph_listener_status bt_graph_notify_port_added(struct bt_graph *graph, |
144 | struct bt_port *port); | |
1bf957a0 | 145 | |
1bf957a0 | 146 | BT_HIDDEN |
e18f3e2b SM |
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); | |
1bf957a0 | 150 | |
f167d3c0 PP |
151 | BT_HIDDEN |
152 | void bt_graph_remove_connection(struct bt_graph *graph, | |
153 | struct bt_connection *connection); | |
154 | ||
c3ac0193 PP |
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 | ||
f7c3ac09 | 167 | BT_HIDDEN |
b09a5592 PP |
168 | void bt_graph_add_message(struct bt_graph *graph, |
169 | struct bt_message *msg); | |
f7c3ac09 | 170 | |
262e5473 PP |
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"; | |
262e5473 PP |
183 | case BT_GRAPH_STATUS_ERROR: |
184 | return "BT_GRAPH_STATUS_ERROR"; | |
a256a42d PP |
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"; | |
262e5473 PP |
189 | default: |
190 | return "(unknown)"; | |
191 | } | |
192 | } | |
193 | ||
1043fdea PP |
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 | ||
9a84962b PP |
216 | BT_ASSERT(graph->config_state != BT_GRAPH_CONFIGURATION_STATE_FAULTY); |
217 | ||
85e7137b | 218 | if (G_LIKELY(graph->config_state == |
1043fdea PP |
219 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURED)) { |
220 | goto end; | |
221 | } | |
222 | ||
28273afc FD |
223 | #ifdef BT_ASSERT_PRE |
224 | BT_ASSERT_PRE(graph->has_sink, "Graph has no sink component: %!+g", graph); | |
225 | #endif | |
226 | ||
1043fdea PP |
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) { | |
025502df | 269 | status = BT_GRAPH_STATUS_ERROR; |
1043fdea PP |
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 | ||
e18f3e2b SM |
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 | ||
abe30a8f | 300 | #endif /* BABELTRACE_GRAPH_GRAPH_INTERNAL_H */ |