4 * Babeltrace Plugin Component Graph
6 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #define BT_LOG_TAG "GRAPH"
29 #include <babeltrace/lib-logging-internal.h>
31 #include <babeltrace/graph/component-internal.h>
32 #include <babeltrace/graph/graph-internal.h>
33 #include <babeltrace/graph/connection-internal.h>
34 #include <babeltrace/graph/component-sink-internal.h>
35 #include <babeltrace/graph/component-source.h>
36 #include <babeltrace/graph/component-filter.h>
37 #include <babeltrace/graph/port.h>
38 #include <babeltrace/compiler-internal.h>
39 #include <babeltrace/types.h>
43 struct bt_graph_listener
{
49 void bt_graph_destroy(struct bt_object
*obj
)
51 struct bt_graph
*graph
= container_of(obj
,
52 struct bt_graph
, base
);
55 * The graph's reference count is 0 if we're here. Increment
56 * it to avoid a double-destroy (possibly infinitely recursive)
59 * 1. We put and destroy a connection.
60 * 2. This connection's destructor finalizes its active
61 * notification iterators.
62 * 3. A notification iterator's finalization function gets a
63 * new reference on its component (reference count goes from
65 * 4. Since this component's reference count goes to 1, it takes
66 * a reference on its parent (this graph). This graph's
67 * reference count goes from 0 to 1.
68 * 5. The notification iterator's finalization function puts its
69 * component reference (reference count goes from 1 to 0).
70 * 6. Since this component's reference count goes from 1 to 0,
71 * it puts its parent (this graph). This graph's reference
72 * count goes from 1 to 0.
73 * 7. Since this graph's reference count goes from 1 to 0,
74 * its destructor is called (this function).
76 * With the incrementation below, the graph's reference count at
77 * step 4 goes from 1 to 2, and from 2 to 1 at step 6. This
78 * ensures that this function is not called two times.
80 BT_LOGD("Destroying graph: addr=%p", graph
);
81 obj
->ref_count
.count
++;
83 if (graph
->connections
) {
84 BT_LOGD_STR("Destroying connections.");
85 g_ptr_array_free(graph
->connections
, TRUE
);
87 if (graph
->components
) {
88 BT_LOGD_STR("Destroying components.");
89 g_ptr_array_free(graph
->components
, TRUE
);
91 if (graph
->sinks_to_consume
) {
92 g_queue_free(graph
->sinks_to_consume
);
95 if (graph
->listeners
.port_added
) {
96 g_array_free(graph
->listeners
.port_added
, TRUE
);
99 if (graph
->listeners
.port_removed
) {
100 g_array_free(graph
->listeners
.port_removed
, TRUE
);
103 if (graph
->listeners
.ports_connected
) {
104 g_array_free(graph
->listeners
.ports_connected
, TRUE
);
107 if (graph
->listeners
.ports_disconnected
) {
108 g_array_free(graph
->listeners
.ports_disconnected
, TRUE
);
115 int init_listeners_array(GArray
**listeners
)
120 *listeners
= g_array_new(FALSE
, TRUE
, sizeof(struct bt_graph_listener
));
122 BT_LOGE_STR("Failed to allocate one GArray.");
131 struct bt_graph
*bt_graph_create(void)
133 struct bt_graph
*graph
;
136 BT_LOGD_STR("Creating graph object.");
137 graph
= g_new0(struct bt_graph
, 1);
139 BT_LOGE_STR("Failed to allocate one graph.");
143 bt_object_init(graph
, bt_graph_destroy
);
145 graph
->connections
= g_ptr_array_new_with_free_func(bt_object_release
);
146 if (!graph
->connections
) {
147 BT_LOGE_STR("Failed to allocate one GPtrArray.");
150 graph
->components
= g_ptr_array_new_with_free_func(bt_object_release
);
151 if (!graph
->components
) {
152 BT_LOGE_STR("Failed to allocate one GPtrArray.");
155 graph
->sinks_to_consume
= g_queue_new();
156 if (!graph
->sinks_to_consume
) {
157 BT_LOGE_STR("Failed to allocate one GQueue.");
161 ret
= init_listeners_array(&graph
->listeners
.port_added
);
163 BT_LOGE_STR("Cannot create the \"port added\" listener array.");
167 ret
= init_listeners_array(&graph
->listeners
.port_removed
);
169 BT_LOGE_STR("Cannot create the \"port removed\" listener array.");
173 ret
= init_listeners_array(&graph
->listeners
.ports_connected
);
175 BT_LOGE_STR("Cannot create the \"port connected\" listener array.");
179 ret
= init_listeners_array(&graph
->listeners
.ports_disconnected
);
181 BT_LOGE_STR("Cannot create the \"port disconneted\" listener array.");
185 BT_LOGD("Created graph object: addr=%p", graph
);
194 struct bt_connection
*bt_graph_connect_ports(struct bt_graph
*graph
,
195 struct bt_port
*upstream_port
,
196 struct bt_port
*downstream_port
)
198 struct bt_connection
*connection
= NULL
;
199 struct bt_graph
*upstream_graph
= NULL
;
200 struct bt_graph
*downstream_graph
= NULL
;
201 struct bt_component
*upstream_component
= NULL
;
202 struct bt_component
*downstream_component
= NULL
;
203 enum bt_component_status component_status
;
204 bt_bool upstream_was_already_in_graph
;
205 bt_bool downstream_was_already_in_graph
;
208 BT_LOGW_STR("Invalid parameter: graph is NULL.");
212 if (!upstream_port
) {
213 BT_LOGW_STR("Invalid parameter: upstream port is NULL.");
217 if (!downstream_port
) {
218 BT_LOGW_STR("Invalid parameter: downstream port is NULL.");
222 BT_LOGD("Connecting component ports within graph: "
224 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
225 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
226 graph
, upstream_port
, bt_port_get_name(upstream_port
),
227 downstream_port
, bt_port_get_name(downstream_port
));
229 if (graph
->canceled
) {
230 BT_LOGW_STR("Invalid parameter: graph is canceled.");
234 /* Ensure appropriate types for upstream and downstream ports. */
235 if (bt_port_get_type(upstream_port
) != BT_PORT_TYPE_OUTPUT
) {
236 BT_LOGW_STR("Invalid parameter: upstream port is not an output port.");
239 if (bt_port_get_type(downstream_port
) != BT_PORT_TYPE_INPUT
) {
240 BT_LOGW_STR("Invalid parameter: downstream port is not an input port.");
244 /* Ensure that both ports are currently unconnected. */
245 if (bt_port_is_connected(upstream_port
)) {
246 BT_LOGW_STR("Invalid parameter: upstream port is already connected.");
250 if (bt_port_is_connected(downstream_port
)) {
251 BT_LOGW_STR("Invalid parameter: downstream port is already connected.");
256 * Ensure that both ports are still attached to their creating
259 upstream_component
= bt_port_get_component(upstream_port
);
260 if (!upstream_component
) {
261 BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)");
265 downstream_component
= bt_port_get_component(downstream_port
);
266 if (!downstream_component
) {
267 BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)");
271 BT_LOGD("Connecting component ports: "
272 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
273 "downstream-comp-addr=%p, downstream-comp-name=\"%s\"",
274 upstream_component
, bt_component_get_name(upstream_component
),
275 downstream_component
, bt_component_get_name(downstream_component
));
277 /* Ensure the components are not already part of another graph. */
278 upstream_graph
= bt_component_get_graph(upstream_component
);
279 if (upstream_graph
&& (graph
!= upstream_graph
)) {
280 BT_LOGW("Invalid parameter: upstream port's component is already part of another graph: "
281 "other-graph-addr=%p", upstream_graph
);
284 upstream_was_already_in_graph
= (graph
== upstream_graph
);
285 downstream_graph
= bt_component_get_graph(downstream_component
);
286 if (downstream_graph
&& (graph
!= downstream_graph
)) {
287 BT_LOGW("Invalid parameter: downstream port's component is already part of another graph: "
288 "other-graph-addr=%p", downstream_graph
);
291 downstream_was_already_in_graph
= (graph
== downstream_graph
);
294 * At this point the ports are not connected yet. Both
295 * components need to accept an eventual connection to their
296 * port by the other port before we continue.
298 BT_LOGD_STR("Asking upstream component to accept the connection.");
299 component_status
= bt_component_accept_port_connection(
300 upstream_component
, upstream_port
, downstream_port
);
301 if (component_status
!= BT_COMPONENT_STATUS_OK
) {
302 if (component_status
== BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
303 BT_LOGD_STR("Upstream component refused the connection.");
305 BT_LOGW("Cannot ask upstream component to accept the connection: "
306 "status=%s", bt_component_status_string(component_status
));
312 BT_LOGD_STR("Asking downstream component to accept the connection.");
313 component_status
= bt_component_accept_port_connection(
314 downstream_component
, downstream_port
, upstream_port
);
315 if (component_status
!= BT_COMPONENT_STATUS_OK
) {
316 if (component_status
== BT_COMPONENT_STATUS_REFUSE_PORT_CONNECTION
) {
317 BT_LOGD_STR("Downstream component refused the connection.");
319 BT_LOGW("Cannot ask downstream component to accept the connection: "
320 "status=%s", bt_component_status_string(component_status
));
326 BT_LOGD_STR("Creating connection.");
327 connection
= bt_connection_create(graph
, upstream_port
,
330 BT_LOGW("Cannot create connection object.");
334 BT_LOGD("Connection object created: conn-addr=%p", connection
);
337 * Ownership of upstream_component/downstream_component and of
338 * the connection object is transferred to the graph.
340 g_ptr_array_add(graph
->connections
, connection
);
342 if (!upstream_was_already_in_graph
) {
343 g_ptr_array_add(graph
->components
, upstream_component
);
344 bt_component_set_graph(upstream_component
, graph
);
346 if (!downstream_was_already_in_graph
) {
347 g_ptr_array_add(graph
->components
, downstream_component
);
348 bt_component_set_graph(downstream_component
, graph
);
349 if (bt_component_get_class_type(downstream_component
) ==
350 BT_COMPONENT_CLASS_TYPE_SINK
) {
351 g_queue_push_tail(graph
->sinks_to_consume
,
352 downstream_component
);
357 * The graph is now the parent of these components which
358 * garantees their existence for the duration of the graph's
363 * Notify both components that their port is connected.
365 BT_LOGD_STR("Notifying upstream component that its port is connected.");
366 bt_component_port_connected(upstream_component
, upstream_port
,
368 BT_LOGD_STR("Notifying downstream component that its port is connected.");
369 bt_component_port_connected(downstream_component
, downstream_port
,
373 * Notify the graph's creator that both ports are connected.
375 BT_LOGD_STR("Notifying graph's user that new component ports are connected.");
376 bt_graph_notify_ports_connected(graph
, upstream_port
, downstream_port
);
377 BT_LOGD("Connected component ports within graph: "
379 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
380 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
381 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
382 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
384 upstream_component
, bt_component_get_name(upstream_component
),
385 downstream_component
, bt_component_get_name(downstream_component
),
386 upstream_port
, bt_port_get_name(upstream_port
),
387 downstream_port
, bt_port_get_name(downstream_port
));
390 bt_put(upstream_graph
);
391 bt_put(downstream_graph
);
392 bt_put(upstream_component
);
393 bt_put(downstream_component
);
397 BT_PUT(upstream_component
);
398 BT_PUT(downstream_component
);
402 enum bt_graph_status
bt_graph_consume(struct bt_graph
*graph
)
404 struct bt_component
*sink
;
405 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
406 enum bt_component_status comp_status
;
410 BT_LOGW_STR("Invalid parameter: graph is NULL.");
411 status
= BT_GRAPH_STATUS_INVALID
;
415 BT_LOGV("Making sink consume: addr=%p", graph
);
417 if (graph
->canceled
) {
418 BT_LOGW_STR("Invalid parameter: graph is canceled.");
419 status
= BT_GRAPH_STATUS_CANCELED
;
423 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
424 BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
425 status
= BT_GRAPH_STATUS_END
;
429 current_node
= g_queue_pop_head_link(graph
->sinks_to_consume
);
430 sink
= current_node
->data
;
431 BT_LOGV("Chose next sink to consume: comp-addr=%p, comp-name=\"%s\"",
432 sink
, bt_component_get_name(sink
));
433 comp_status
= bt_component_sink_consume(sink
);
434 BT_LOGV("Consumed from sink: status=%s",
435 bt_component_status_string(comp_status
));
436 switch (comp_status
) {
437 case BT_COMPONENT_STATUS_OK
:
439 case BT_COMPONENT_STATUS_END
:
440 status
= BT_GRAPH_STATUS_END
;
442 case BT_COMPONENT_STATUS_AGAIN
:
443 status
= BT_GRAPH_STATUS_AGAIN
;
445 case BT_COMPONENT_STATUS_INVALID
:
446 status
= BT_GRAPH_STATUS_INVALID
;
449 status
= BT_GRAPH_STATUS_ERROR
;
453 if (status
!= BT_GRAPH_STATUS_END
) {
454 g_queue_push_tail_link(graph
->sinks_to_consume
, current_node
);
458 /* End reached, the node is not added back to the queue and free'd. */
459 g_queue_delete_link(graph
->sinks_to_consume
, current_node
);
461 /* Don't forward an END status if there are sinks left to consume. */
462 if (!g_queue_is_empty(graph
->sinks_to_consume
)) {
463 status
= BT_GRAPH_STATUS_OK
;
467 BT_LOGV("Graph consumed: status=%s", bt_graph_status_string(status
));
471 enum bt_graph_status
bt_graph_run(struct bt_graph
*graph
)
473 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
476 BT_LOGW_STR("Invalid parameter: graph is NULL.");
477 status
= BT_GRAPH_STATUS_INVALID
;
481 BT_LOGV("Running graph: addr=%p", graph
);
484 status
= bt_graph_consume(graph
);
485 if (status
== BT_GRAPH_STATUS_AGAIN
) {
487 * If AGAIN is received and there are multiple
488 * sinks, go ahead and consume from the next
491 * However, in the case where a single sink is
492 * left, the caller can decide to busy-wait and
493 * call bt_graph_run() continuously until the
494 * source is ready or it can decide to sleep for
495 * an arbitrary amount of time.
497 if (graph
->sinks_to_consume
->length
> 1) {
498 status
= BT_GRAPH_STATUS_OK
;
501 } while (status
== BT_GRAPH_STATUS_OK
);
503 if (g_queue_is_empty(graph
->sinks_to_consume
)) {
504 status
= BT_GRAPH_STATUS_END
;
508 BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status
));
513 void add_listener(GArray
*listeners
, void *func
, void *data
)
515 struct bt_graph_listener listener
= {
520 g_array_append_val(listeners
, listener
);
523 enum bt_graph_status
bt_graph_add_port_added_listener(
524 struct bt_graph
*graph
,
525 bt_graph_port_added_listener listener
, void *data
)
527 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
530 BT_LOGW_STR("Invalid parameter: graph is NULL.");
531 status
= BT_GRAPH_STATUS_INVALID
;
536 BT_LOGW_STR("Invalid parameter: listener is NULL.");
537 status
= BT_GRAPH_STATUS_INVALID
;
541 add_listener(graph
->listeners
.port_added
, listener
, data
);
542 BT_LOGV("Added \"port added\" listener to graph: "
543 "graph-addr=%p, listener-addr=%p", graph
, listener
);
549 enum bt_graph_status
bt_graph_add_port_removed_listener(
550 struct bt_graph
*graph
,
551 bt_graph_port_removed_listener listener
, void *data
)
553 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
556 BT_LOGW_STR("Invalid parameter: graph is NULL.");
557 status
= BT_GRAPH_STATUS_INVALID
;
562 BT_LOGW_STR("Invalid parameter: listener is NULL.");
563 status
= BT_GRAPH_STATUS_INVALID
;
567 add_listener(graph
->listeners
.port_removed
, listener
, data
);
568 BT_LOGV("Added \"port removed\" listener to graph: "
569 "graph-addr=%p, listener-addr=%p", graph
, listener
);
575 enum bt_graph_status
bt_graph_add_ports_connected_listener(
576 struct bt_graph
*graph
,
577 bt_graph_ports_connected_listener listener
, void *data
)
579 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
582 BT_LOGW_STR("Invalid parameter: graph is NULL.");
583 status
= BT_GRAPH_STATUS_INVALID
;
588 BT_LOGW_STR("Invalid parameter: listener is NULL.");
589 status
= BT_GRAPH_STATUS_INVALID
;
593 add_listener(graph
->listeners
.ports_connected
, listener
, data
);
594 BT_LOGV("Added \"port connected\" listener to graph: "
595 "graph-addr=%p, listener-addr=%p", graph
, listener
);
601 enum bt_graph_status
bt_graph_add_ports_disconnected_listener(
602 struct bt_graph
*graph
,
603 bt_graph_ports_disconnected_listener listener
, void *data
)
605 enum bt_graph_status status
= BT_GRAPH_STATUS_OK
;
608 BT_LOGW_STR("Invalid parameter: graph is NULL.");
609 status
= BT_GRAPH_STATUS_INVALID
;
614 BT_LOGW_STR("Invalid parameter: listener is NULL.");
615 status
= BT_GRAPH_STATUS_INVALID
;
619 add_listener(graph
->listeners
.ports_disconnected
, listener
, data
);
620 BT_LOGV("Added \"port disconnected\" listener to graph: "
621 "graph-addr=%p, listener-addr=%p", graph
, listener
);
628 void bt_graph_notify_port_added(struct bt_graph
*graph
, struct bt_port
*port
)
632 BT_LOGV("Notifying graph listeners that a port was added: "
633 "graph-addr=%p, port-addr=%p, port-name=\"%s\"",
634 graph
, port
, bt_port_get_name(port
));
636 for (i
= 0; i
< graph
->listeners
.port_added
->len
; i
++) {
637 struct bt_graph_listener listener
=
638 g_array_index(graph
->listeners
.port_added
,
639 struct bt_graph_listener
, i
);
640 bt_graph_port_added_listener func
= listener
.func
;
643 func(port
, listener
.data
);
648 void bt_graph_notify_port_removed(struct bt_graph
*graph
,
649 struct bt_component
*comp
, struct bt_port
*port
)
653 BT_LOGV("Notifying graph listeners that a port was removed: "
654 "graph-addr=%p, port-addr=%p, port-name=\"%s\"",
655 graph
, port
, bt_port_get_name(port
));
657 for (i
= 0; i
< graph
->listeners
.port_removed
->len
; i
++) {
658 struct bt_graph_listener listener
=
659 g_array_index(graph
->listeners
.port_removed
,
660 struct bt_graph_listener
, i
);
661 bt_graph_port_removed_listener func
= listener
.func
;
664 func(comp
, port
, listener
.data
);
669 void bt_graph_notify_ports_connected(struct bt_graph
*graph
,
670 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
)
674 BT_LOGV("Notifying graph listeners that two ports were connected: "
676 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
677 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
678 graph
, upstream_port
, bt_port_get_name(upstream_port
),
679 downstream_port
, bt_port_get_name(downstream_port
));
681 for (i
= 0; i
< graph
->listeners
.ports_connected
->len
; i
++) {
682 struct bt_graph_listener listener
=
683 g_array_index(graph
->listeners
.ports_connected
,
684 struct bt_graph_listener
, i
);
685 bt_graph_ports_connected_listener func
= listener
.func
;
688 func(upstream_port
, downstream_port
, listener
.data
);
693 void bt_graph_notify_ports_disconnected(struct bt_graph
*graph
,
694 struct bt_component
*upstream_comp
,
695 struct bt_component
*downstream_comp
,
696 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
)
700 BT_LOGV("Notifying graph listeners that two ports were disconnected: "
702 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
703 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
704 graph
, upstream_port
, bt_port_get_name(upstream_port
),
705 downstream_port
, bt_port_get_name(downstream_port
));
707 for (i
= 0; i
< graph
->listeners
.ports_disconnected
->len
; i
++) {
708 struct bt_graph_listener listener
=
709 g_array_index(graph
->listeners
.ports_disconnected
,
710 struct bt_graph_listener
, i
);
711 bt_graph_ports_disconnected_listener func
= listener
.func
;
714 func(upstream_comp
, downstream_comp
, upstream_port
,
715 downstream_port
, listener
.data
);
719 extern enum bt_graph_status
bt_graph_cancel(struct bt_graph
*graph
)
721 enum bt_graph_status ret
= BT_GRAPH_STATUS_OK
;
724 BT_LOGW_STR("Invalid parameter: graph is NULL.");
725 ret
= BT_GRAPH_STATUS_INVALID
;
729 graph
->canceled
= BT_TRUE
;
730 BT_LOGV("Canceled graph: addr=%p", graph
);
736 extern bt_bool
bt_graph_is_canceled(struct bt_graph
*graph
)
738 return graph
? graph
->canceled
: BT_FALSE
;
742 void bt_graph_remove_connection(struct bt_graph
*graph
,
743 struct bt_connection
*connection
)
747 BT_LOGV("Removing graph's connection: graph-addr=%p, conn-addr=%p",
749 g_ptr_array_remove(graph
->connections
, connection
);