1 #ifndef BABELTRACE_GRAPH_GRAPH_INTERNAL_H
2 #define BABELTRACE_GRAPH_GRAPH_INTERNAL_H
5 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
6 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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
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"
36 #include "component.h"
37 #include "component-sink.h"
38 #include "connection.h"
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
,
52 * A component graph contains components and point-to-point connection
53 * between these components.
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.
61 struct bt_object base
;
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
;
71 bool in_remove_listener
;
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.
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.
86 enum bt_graph_configuration_state config_state
;
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
;
99 /* Pool of `struct bt_message_event *` */
100 struct bt_object_pool event_msg_pool
;
102 /* Pool of `struct bt_message_packet_beginning *` */
103 struct bt_object_pool packet_begin_msg_pool
;
105 /* Pool of `struct bt_message_packet_end *` */
106 struct bt_object_pool packet_end_msg_pool
;
109 * Array of `struct bt_message *` (weak).
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
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).
126 void _bt_graph_set_can_consume(struct bt_graph
*graph
, bool can_consume
)
129 graph
->can_consume
= can_consume
;
133 # define bt_graph_set_can_consume _bt_graph_set_can_consume
135 # define bt_graph_set_can_consume(_graph, _can_consume)
139 enum bt_graph_status
bt_graph_consume_sink_no_check(struct bt_graph
*graph
,
140 struct bt_component_sink
*sink
);
143 enum bt_graph_listener_status
bt_graph_notify_port_added(struct bt_graph
*graph
,
144 struct bt_port
*port
);
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
);
152 void bt_graph_remove_connection(struct bt_graph
*graph
,
153 struct bt_connection
*connection
);
156 * This only works with a component which is not connected at this
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.
164 int bt_graph_remove_unconnected_component(struct bt_graph
*graph
,
165 struct bt_component
*component
);
168 void bt_graph_add_message(struct bt_graph
*graph
,
169 struct bt_message
*msg
);
172 const char *bt_graph_status_string(enum bt_graph_status 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";
195 const char *bt_graph_configuration_state_string(
196 enum bt_graph_configuration_state 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";
211 enum bt_graph_status
bt_graph_configure(struct bt_graph
*graph
)
213 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
216 BT_ASSERT(graph
->config_state
!= BT_GRAPH_CONFIGURATION_STATE_FAULTY
);
218 if (G_LIKELY(graph
->config_state
==
219 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
)) {
224 BT_ASSERT_PRE(graph
->has_sink
, "Graph has no sink component: %!+g", graph
);
227 graph
->config_state
= BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED
;
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;
235 if (comp
->class->type
!= BT_COMPONENT_CLASS_TYPE_SINK
) {
239 if (comp_sink
->graph_is_configured_method_called
) {
243 if (comp_cls_sink
->methods
.graph_is_configured
) {
244 enum bt_self_component_status comp_status
;
247 BT_LIB_LOGD("Calling user's \"graph is configured\" method: "
248 "%![graph-]+g, %![comp-]+c",
252 comp_status
= comp_cls_sink
->methods
.graph_is_configured(
256 BT_LIB_LOGD("User method returned: status=%s",
257 bt_self_component_status_string(comp_status
));
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
));
268 if (comp_status
!= BT_SELF_COMPONENT_STATUS_OK
) {
269 status
= BT_GRAPH_STATUS_ERROR
;
271 BT_LIB_LOGW("User's \"graph is configured\" method failed: "
272 "%![comp-]+c, status=%s",
274 bt_self_component_status_string(
282 comp_sink
->graph_is_configured_method_called
= true;
285 graph
->config_state
= BT_GRAPH_CONFIGURATION_STATE_CONFIGURED
;
292 void bt_graph_make_faulty(struct bt_graph
*graph
)
294 graph
->config_state
= BT_GRAPH_CONFIGURATION_STATE_FAULTY
;
296 BT_LIB_LOGI("Set graph's state to faulty: %![graph-]+g", graph
);
300 #endif /* BABELTRACE_GRAPH_GRAPH_INTERNAL_H */