Cleanup: remove private babeltrace.h
[babeltrace.git] / src / lib / graph / graph.h
CommitLineData
6ac74c0c
PP
1#ifndef BABELTRACE_GRAPH_GRAPH_INTERNAL_H
2#define BABELTRACE_GRAPH_GRAPH_INTERNAL_H
c0418dd9
JG
3
4/*
e2f7325d 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
3fadfbc0 27#include <babeltrace2/graph/graph.h>
3fadfbc0 28#include <babeltrace2/graph/message-const.h>
91d81473 29#include "common/macros.h"
578e048b
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
578e048b
MJ
36#include "component.h"
37#include "component-sink.h"
38#include "connection.h"
39
1bf957a0
PP
40struct bt_component;
41struct bt_port;
42
5badd463
PP
43enum bt_graph_configuration_state {
44 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
45 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED,
46 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED,
38cda5da 47 BT_GRAPH_CONFIGURATION_STATE_FAULTY,
5badd463
PP
48};
49
f60c8b34
JG
50struct 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
d94d92ac
PP
70 bool canceled;
71 bool in_remove_listener;
72 bool has_sink;
8ed535b5
PP
73
74 /*
d94d92ac 75 * If this is false, then the public API's consuming
8ed535b5
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 *
d6e69534 80 * In bt_port_output_message_iterator_create(), on success,
8ed535b5
PP
81 * this flag is cleared so that the iterator remains the only
82 * consumer for the graph's lifetime.
83 */
d94d92ac 84 bool can_consume;
202a3a13 85
5badd463 86 enum bt_graph_configuration_state config_state;
4725a201 87
1bf957a0 88 struct {
d94d92ac
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;
d94d92ac
PP
93 GArray *source_filter_ports_connected;
94 GArray *source_sink_ports_connected;
9c0a126a 95 GArray *filter_filter_ports_connected;
d94d92ac 96 GArray *filter_sink_ports_connected;
1bf957a0 97 } listeners;
5c563278 98
d6e69534
PP
99 /* Pool of `struct bt_message_event *` */
100 struct bt_object_pool event_msg_pool;
5c563278 101
d6e69534
PP
102 /* Pool of `struct bt_message_packet_beginning *` */
103 struct bt_object_pool packet_begin_msg_pool;
5c563278 104
d6e69534
PP
105 /* Pool of `struct bt_message_packet_end *` */
106 struct bt_object_pool packet_end_msg_pool;
5c563278
PP
107
108 /*
d6e69534 109 * Array of `struct bt_message *` (weak).
5c563278 110 *
d6e69534 111 * This is an array of all the messages ever created from
5c563278 112 * this graph. Some of them can be in one of the pools above,
d6e69534 113 * some of them can be at large. Because each message has a
5c563278 114 * weak pointer to the graph containing its pool, we need to
d6e69534 115 * notify each message that the graph is gone on graph
5c563278
PP
116 * destruction.
117 *
118 * TODO: When we support a maximum size for object pools,
d6e69534 119 * add a way for a message to remove itself from this
5c563278
PP
120 * array (on destruction).
121 */
d6e69534 122 GPtrArray *messages;
c0418dd9
JG
123};
124
8ed535b5 125static inline
d94d92ac 126void _bt_graph_set_can_consume(struct bt_graph *graph, bool can_consume)
8ed535b5 127{
f6ccaed9 128 BT_ASSERT(graph);
8ed535b5
PP
129 graph->can_consume = can_consume;
130}
131
ad847455
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
8ed535b5
PP
137
138BT_HIDDEN
139enum bt_graph_status bt_graph_consume_sink_no_check(struct bt_graph *graph,
d94d92ac 140 struct bt_component_sink *sink);
8ed535b5 141
1bf957a0 142BT_HIDDEN
8cc56726
SM
143enum bt_graph_listener_status bt_graph_notify_port_added(struct bt_graph *graph,
144 struct bt_port *port);
1bf957a0 145
1bf957a0 146BT_HIDDEN
8cc56726
SM
147enum 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
151BT_HIDDEN
152void bt_graph_remove_connection(struct bt_graph *graph,
153 struct bt_connection *connection);
154
8ed535b5
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 */
163BT_HIDDEN
164int bt_graph_remove_unconnected_component(struct bt_graph *graph,
165 struct bt_component *component);
166
5c563278 167BT_HIDDEN
d6e69534
PP
168void bt_graph_add_message(struct bt_graph *graph,
169 struct bt_message *msg);
5c563278 170
262e5473
PP
171static inline
172const 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
5badd463
PP
194static inline
195const 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
210static inline
211enum 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
38cda5da
PP
216 BT_ASSERT(graph->config_state != BT_GRAPH_CONFIGURATION_STATE_FAULTY);
217
91d81473 218 if (G_LIKELY(graph->config_state ==
5badd463
PP
219 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED)) {
220 goto end;
221 }
222
cd6128ca
FD
223#ifdef BT_ASSERT_PRE
224 BT_ASSERT_PRE(graph->has_sink, "Graph has no sink component: %!+g", graph);
225#endif
226
5badd463
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) {
53719615 269 status = BT_GRAPH_STATUS_ERROR;
5badd463
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
287end:
288 return status;
289}
290
8cc56726
SM
291static inline
292void 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
6ac74c0c 300#endif /* BABELTRACE_GRAPH_GRAPH_INTERNAL_H */
This page took 0.065242 seconds and 4 git commands to generate.